dsh-pick 0.6.2 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/ai-pick.js ADDED
@@ -0,0 +1,172 @@
1
+ // lib/ai-pick.js — 面板里的「AI 精选」:用 DSH 当前的大模型(和对话里同一个)挑插件
2
+ //
3
+ // 关键字搜索只会字面匹配,不懂需求;对话里的大模型会读懂意思、看简介再判断,所以推荐得更准。
4
+ // 这里把同样的能力搬进面板,分两步:
5
+ // 1. 查全:让模型把需求展开成一组关键字(中英文、同义说法),每个关键字在全目录里召回候选,合并成 ≤40 个
6
+ // 2. 挑准:把候选的简介和数据交给模型,让它按「是不是真能解决这个需求」挑出 ≤8 个,每个写一句理由
7
+ // 模型给的包名逐个核对:不在候选里的丢掉(防止编造);数字只用我们给的,不让模型自己推算。
8
+ // 点了按钮才调用(花用户的 token),打开面板不花。
9
+ //
10
+ // 调模型的写法照 dsh-vps-manager:DSH 的 llm 服务 + agentDefaultModel(默认模型选择)。
11
+
12
+ import { randomUUID } from 'node:crypto'
13
+ import { searchByNeed } from './engine.js'
14
+
15
+ const MAX_CANDIDATES = 40
16
+ const MAX_PICKS = 8
17
+
18
+ /** 调一次模型,返回整段文字 */
19
+ export async function callModel({ llm, defaultModel, system, user, maxTokens = 1500, signal, purpose = 'dsh-pick' }) {
20
+ if (!llm || typeof llm.stream !== 'function' || !defaultModel || typeof defaultModel.currentSelection !== 'function') {
21
+ throw Object.assign(new Error('这个版本的 DSH 不让插件直接调用模型'), { code: 'no_llm' })
22
+ }
23
+ const route = defaultModel.currentSelection()
24
+ const timeout = AbortSignal.timeout(120000)
25
+ let text = ''
26
+ let finish = { kind: 'stop' }
27
+ for await (const chunk of llm.stream({
28
+ provider: route.provider,
29
+ model: route.model,
30
+ system,
31
+ messages: [{ role: 'user', id: randomUUID(), content: [{ type: 'text', text: user }], source: { kind: 'plugin:dsh-pick' } }],
32
+ maxTokens,
33
+ purpose,
34
+ signal: signal ? AbortSignal.any([signal, timeout]) : timeout,
35
+ })) {
36
+ if (chunk.type === 'text-delta') text += chunk.text
37
+ else if (chunk.type === 'finish') finish = chunk.reason ?? finish
38
+ }
39
+ if (finish && (finish.kind === 'error' || finish.kind === 'aborted')) {
40
+ throw Object.assign(new Error((finish.failure && finish.failure.message) || '模型调用失败'), { code: finish.failure && finish.failure.code })
41
+ }
42
+ return { text: text.trim(), model: route.model }
43
+ }
44
+
45
+ /** 从模型的回答里取出第一个 JSON 对象(模型常在前后加说明或代码块) */
46
+ export function extractJson(text) {
47
+ const s = String(text || '')
48
+ const start = s.indexOf('{')
49
+ if (start < 0) return null
50
+ let depth = 0, inStr = false, esc = false
51
+ for (let i = start; i < s.length; i++) {
52
+ const c = s[i]
53
+ if (inStr) {
54
+ if (esc) esc = false
55
+ else if (c === '\\') esc = true
56
+ else if (c === '"') inStr = false
57
+ continue
58
+ }
59
+ if (c === '"') inStr = true
60
+ else if (c === '{') depth++
61
+ else if (c === '}' && --depth === 0) {
62
+ try { return JSON.parse(s.slice(start, i + 1)) } catch { return null }
63
+ }
64
+ }
65
+ return null
66
+ }
67
+
68
+ const KEYWORD_SYSTEM = [
69
+ '你帮用户在 DeepSeek Harness(DSH)的插件目录里找插件。目录按包名、关键字、简介做字面匹配。',
70
+ '把用户的需求展开成 4~10 个用来搜索的关键字:中文和英文都要有,包括同义说法和插件作者常用的英文词(比如 database、sql、notify、wechat、memory、theme)。',
71
+ '只回答一个 JSON 对象,形如 {"keywords":["...","..."]},不要别的文字。',
72
+ ].join('\n')
73
+
74
+ const PICK_SYSTEM = [
75
+ '你是 DeepSeek Harness(DSH)插件选型顾问,目标是给用户推荐真正好用、真能解决他需求的插件。',
76
+ '下面给出用户的需求和一批候选插件(简介 + 数据)。请挑出真正适合的,最多 8 个,按合适程度排序;只是沾边、功能不对、装不了的不要选。',
77
+ '判断依据:先看功能是否对得上需求,再看数据:日常基线(去噪后的每日使用量,比周下载可靠)、最近更新、是否被精选或官方收录、GitHub 星标。周下载含 CI 和镜像流量,不等于用户数。',
78
+ '只能用给出的信息,不要编造插件或数字。理由用中文,每条不超过 40 个字,说清楚为什么适合;有明显的坑(比如很久没更新、只支持某个平台)写在 caveat 里。',
79
+ '只回答一个 JSON 对象,形如 {"picks":[{"name":"包名","fit":0到100,"reason":"理由","caveat":"注意事项,可省略"}],"note":"一句话总结,可省略"},不要别的文字。一个都不合适时 picks 为空数组,并在 note 里说明。',
80
+ ].join('\n')
81
+
82
+ const num = n => (n == null ? '—' : Number(n).toLocaleString('en-US'))
83
+
84
+ /** 一个候选给模型看的一行 */
85
+ function candidateLine(i, rec, extra) {
86
+ const d = rec.deep
87
+ const days = d && d.age != null ? `${d.age} 天前更新` : rec.t ? `最近发版 ${String(rec.t).slice(0, 10)}` : ''
88
+ return [
89
+ `${i + 1}. ${rec.n}`,
90
+ rec.cat ? `分类:${rec.cat}` : '',
91
+ `周下载 ${num(rec.w)}`,
92
+ d ? `日常基线 ${num(d.base)}/天` : '',
93
+ rec.gh && rec.gh.s != null ? `GitHub ★${num(rec.gh.s)}` : '',
94
+ rec.gh && rec.gh.a ? '仓库已归档' : '',
95
+ days,
96
+ rec.src && rec.src.length ? `收录于 ${rec.src.join('、')}` : '',
97
+ extra,
98
+ `简介:${String(rec.d || '').slice(0, 160)}`,
99
+ ].filter(Boolean).join(' · ')
100
+ }
101
+
102
+ /**
103
+ * 用关键字召回候选:每个关键字各取一批,按「最好的相关度」合并,再按相关度为主、使用量为辅排序
104
+ * @returns {object[]} 目录记录
105
+ */
106
+ export function recall(catalog, need, keywords) {
107
+ const best = new Map()
108
+ const add = (list, weight) => {
109
+ for (const r of list) {
110
+ const score = r.relevance * weight
111
+ if (!best.has(r.name) || best.get(r.name) < score) best.set(r.name, score)
112
+ }
113
+ }
114
+ add(searchByNeed(catalog, need, { limit: 20 }), 1)
115
+ for (const k of keywords) add(searchByNeed(catalog, k, { limit: 12 }), 0.9)
116
+ return [...best.entries()]
117
+ .map(([name, score]) => ({ rec: catalog.packages[name], score }))
118
+ .filter(x => x.rec)
119
+ .sort((a, b) => b.score + Math.log10((b.rec.w || 0) + 1) * 3 - (a.score + Math.log10((a.rec.w || 0) + 1) * 3))
120
+ .slice(0, MAX_CANDIDATES)
121
+ .map(x => x.rec)
122
+ }
123
+
124
+ /**
125
+ * AI 精选
126
+ * @param {object} opt
127
+ * @param {string} opt.need 用户的需求(原话)
128
+ * @param {object} opt.catalog 目录(loadCatalog 的结果)
129
+ * @param {object} opt.llm DSH 的 llm 服务
130
+ * @param {object} opt.defaultModel DSH 的 agentDefaultModel 服务
131
+ * @param {Set<string>} [opt.installed] 已装的插件
132
+ * @returns {Promise<{picks: object[], note: string, keywords: string[], candidates: number, model: string}>}
133
+ */
134
+ export async function aiPick({ need, catalog, llm, defaultModel, installed = new Set(), signal }) {
135
+ const q = String(need || '').trim().slice(0, 300)
136
+ if (!q) throw Object.assign(new Error('先写下你想解决什么问题'), { code: 'empty' })
137
+
138
+ // 1. 查全
139
+ const k = await callModel({ llm, defaultModel, system: KEYWORD_SYSTEM, user: q, maxTokens: 300, signal, purpose: 'dsh-pick-keywords' })
140
+ const parsed = extractJson(k.text)
141
+ const keywords = [...new Set((parsed && Array.isArray(parsed.keywords) ? parsed.keywords : [])
142
+ .filter(w => typeof w === 'string').map(w => w.trim().slice(0, 40)).filter(Boolean))].slice(0, 12)
143
+ const cands = recall(catalog, q, keywords)
144
+ if (!cands.length) return { picks: [], note: '目录里没找到相关的插件,换个说法试试。', keywords, candidates: 0, model: k.model }
145
+
146
+ // 2. 挑准
147
+ const lines = cands.map((rec, i) => candidateLine(i, rec, [
148
+ installed.has(rec.n) ? '已安装' : '',
149
+ rec.inst && rec.inst.b === false ? '没声明 dsh.bundle,DSH 装不了' : '',
150
+ rec.inst && rec.inst.p && rec.inst.p !== 'web' ? `${rec.inst.p} 客户端插件` : '',
151
+ ].filter(Boolean).join(' · ')))
152
+ const p = await callModel({
153
+ llm, defaultModel, system: PICK_SYSTEM,
154
+ user: `用户的需求:${q}\n\n候选插件(共 ${cands.length} 个):\n${lines.join('\n')}`,
155
+ maxTokens: 1800, signal, purpose: 'dsh-pick-rerank',
156
+ })
157
+ const out = extractJson(p.text) || {}
158
+ const names = new Set(cands.map(r => r.n))
159
+ const seen = new Set()
160
+ const picks = (Array.isArray(out.picks) ? out.picks : [])
161
+ .filter(x => x && typeof x.name === 'string' && names.has(x.name) && !seen.has(x.name) && seen.add(x.name))
162
+ .slice(0, MAX_PICKS)
163
+ .map(x => ({
164
+ name: x.name,
165
+ fit: Math.max(0, Math.min(100, Math.round(Number(x.fit) || 0))),
166
+ reason: typeof x.reason === 'string' ? x.reason.trim().slice(0, 80) : '',
167
+ caveat: typeof x.caveat === 'string' ? x.caveat.trim().slice(0, 80) : '',
168
+ }))
169
+ const note = typeof out.note === 'string' ? out.note.trim().slice(0, 160) : ''
170
+ if (!picks.length && !note && !extractJson(p.text)) throw new Error('模型没按格式回答,再试一次')
171
+ return { picks, note, keywords, candidates: cands.length, model: p.model }
172
+ }
package/lib/engine.js CHANGED
@@ -564,6 +564,20 @@ export async function loadCatalog({ env = process.env } = {}) {
564
564
  // 每日目录是旧代码构建的、还没有某个榜(比如刚加的综合榜):先用随包自带的那份顶上,下次每日构建后就齐了
565
565
  if (pick && pick.source === 'remote' && bundled && bundled.boards) {
566
566
  pick.boards = { ...bundled.boards, ...(pick.boards || {}) }
567
+ // 每日目录的综合榜是旧代码算的(没有分角度的分数),自带的有:用自带的
568
+ const hasAngles = o => Boolean(o && o.rows && o.rows[0] && o.rows[0].a)
569
+ if (!hasAngles(pick.boards.overall) && hasAngles(bundled.boards.overall)) pick.boards.overall = bundled.boards.overall
570
+ // 每日目录的包记录上没有六个角度的分数(rec.oa,面板在全部插件里排名要用),自带的有:
571
+ // 综合榜整份换成自带的,分数从自带的抄到同名包上,两边口径一致
572
+ const hasOA = c => Object.values((c && c.packages) || {}).some(r => r && r.oa)
573
+ if (!hasOA(pick) && hasOA(bundled) && hasAngles(bundled.boards.overall)) {
574
+ pick.boards.overall = bundled.boards.overall
575
+ pick.packages = { ...pick.packages }
576
+ for (const [n, r] of Object.entries(pick.packages)) {
577
+ const b = bundled.packages[n]
578
+ if (r && b && b.oa) pick.packages[n] = { ...r, oa: b.oa, ...(b.ad ? { ad: b.ad } : {}), ...(b.src ? { src: b.src } : {}) }
579
+ }
580
+ }
567
581
  }
568
582
  cache = pick ? normalize(pick) : { asOf: null, anchor: null, packages: {}, total: 0, source: 'none' }
569
583
  return cache
@@ -744,7 +758,9 @@ export async function liveStats(allNames, { fetchImpl } = {}) {
744
758
  const rows = await pool(names, 2, async name => {
745
759
  const [r, dl] = await Promise.all([fetchLive(name, anchor), searchDownloads(name)])
746
760
  if (!r && !dl) return { name, found: false }
747
- const { daily, keywords, ...rest } = r || { name, deep: null }
761
+ // 30 天每日下载留着(「我的监控」画走势图;缺数据的日子是 null),关键字用不上
762
+ const { keywords, ...rest } = r || { name, deep: null }
763
+ if (rest.daily) rest.dailyStart = shiftDate(anchor, -29)
748
764
  // 周下载 / 月下载用搜索接口的:和目录同一口径(目录就是从它抓的),而且比下载统计接口新——
749
765
  // 下载统计接口常常只到几天前,拿它和目录比会凭空「跌」一截
750
766
  const gh = ghLive.get(repoOf(name)) || null
package/lib/overall.js CHANGED
@@ -7,12 +7,18 @@
7
7
  // official DeepSeek 官方的可选 / 按需插件
8
8
  // npm 本身不算市场:它只是发布渠道。
9
9
  //
10
- // 综合分 0~100,五项加权;使用、关注两项先换成百分位,免得一个几十万星的仓库压倒一切:
11
- // 使用 40% 日常基线(去噪);没有去噪数据的,用周下载按全目录的「基线 / 日均下载」比例折算
12
- // 关注 20% GitHub 星标
13
- // 维护 20% 最近一次发版或提交:今天满分,半年以上 0 分
14
- // 共识 10% 被几个市场收录:多个市场都收,说明不止一个人觉得它值得列出来
15
- // 健康 10% 健康度(去噪指标里的综合分);没有的话看有没有许可证
10
+ // 排名有六个角度(面板里切换;同一份数据,权重不同,每个角度的算法写在页面上):
11
+ // rec 综合推荐 使用 30 + 关注 15 + 维护 15 + 精选 15 + 稳定 15 + 覆盖 10
12
+ // use 最多人用 只看日常基线(去噪),没有去噪数据的不参与
13
+ // rep 口碑 GitHub 星标 65% + 被精选或官方收录 35%
14
+ // stable 稳定可靠 上架满 60 天;版本节奏平稳、有许可证、未关闭问题少、还在维护,再加一点使用量
15
+ // rise 上升最快 有一定使用量(日常基线 ≥ 20/天)的前提下看近 7 日增速
16
+ // new 新上架 最近 30 天才被市场收录的
17
+ // 和第一版(使用 40% 起步)相比改了三处,都是为了少奖励「刷出来的数字」:
18
+ // - 下载量从 40% 降到 30%:npm 下载里有 CI、镜像、自动更新的拉取,发版越勤数字越高
19
+ // - 维护只看「还在不在维护」(60 天内有发版或提交就满分),不再按发版频率加分,免得和下载量重复奖励频繁发版
20
+ // - 「共识」拆成两项:精选(dsh-m 精选、DeepSeek 官方,有人挑过)15%;覆盖(被几个市场收录,多是按关键字自动收录)10%
21
+ // 使用、关注两项取「百分位」和「对数比例」的平均,不让一个几十万星的仓库压倒一切。
16
22
  // 一票否决(不进榜):仓库已归档、没声明 dsh.bundle(DSH 装不了)、终端版插件。
17
23
  //
18
24
  // 只在 GitHub 上、没发到 npm 的插件也收:按 gh:owner/repo 合并,没有下载数据,使用项是 0。
@@ -20,8 +26,9 @@
20
26
  export const MARKETS = ['dshmarket', 'dsh-m', 'shop', 'official']
21
27
  /** 市场 → buildBoards 交出来的名单的 key */
22
28
  const MEMBER_KEY = { dshmarket: 'community', 'dsh-m': 'picks', shop: 'shop', official: 'official' }
23
- export const WEIGHTS = { use: 40, stars: 20, maint: 20, consensus: 10, health: 10 }
24
- const KEEP = 300 // 落盘的行数(面板显示前面的,AI 工具也用它)
29
+ export const WEIGHTS = { use: 30, stars: 15, maint: 15, curated: 15, stable: 15, reach: 10 }
30
+ export const ANGLES = ['rec', 'use', 'rep', 'stable', 'rise', 'new']
31
+ const KEEP_PER_ANGLE = 120 // 每个角度落盘前多少名(合并去重后一起存)
25
32
  const DAY = 86400000
26
33
 
27
34
  const repoKey = url => {
@@ -61,12 +68,28 @@ export function spread(logs) {
61
68
  return logs.map((v, i) => (max > 0 ? (p[i] + Math.max(0, v) / max) / 2 : 0))
62
69
  }
63
70
 
64
- /** 维护分:最近一次发版或提交距今多少天 → 0~1 */
71
+ /** 维护分:最近一次发版或提交距今多少天 → 0~1。60 天内都算「还在维护」给满分,之后到 180 天线性降到 0 */
65
72
  export function maintScore(dates, now = Date.now()) {
66
73
  const ts = dates.map(d => Date.parse(d || '')).filter(Number.isFinite)
67
74
  if (!ts.length) return null
68
75
  const days = Math.max(0, (now - Math.max(...ts)) / DAY)
69
- return Math.max(0, 1 - days / 180)
76
+ return days <= 60 ? 1 : Math.max(0, 1 - (days - 60) / 120)
77
+ }
78
+
79
+ const clamp01 = x => Math.max(0, Math.min(1, x))
80
+
81
+ /**
82
+ * 稳定分 0~1:上架时间、版本节奏是否平稳、有没有许可证、未关闭问题多不多
83
+ * @param {{firstDays: number|null, cadence: number|null, license: boolean|null, issues: number|null, stars: number|null}} f
84
+ */
85
+ export function stableScore({ firstDays, cadence, license, issues, stars }) {
86
+ const age = firstDays == null ? 0.5 : clamp01(firstDays / 60)
87
+ // 0.5 天一个版本以下算「发得太勤」(0 分),3 天以上算平稳(满分)
88
+ const calm = cadence == null ? 0.5 : clamp01((cadence - 0.5) / 2.5)
89
+ const lic = license == null ? 0.3 : license ? 1 : 0
90
+ // 每 10 个星标里有几个没关的 issue/PR:越少越好
91
+ const iss = issues == null ? 0.5 : 1 - clamp01(issues / ((stars || 0) + 10) * 2)
92
+ return 0.35 * age + 0.25 * calm + 0.2 * lic + 0.2 * iss
70
93
  }
71
94
 
72
95
  /**
@@ -116,7 +139,8 @@ export function buildOverall({ packages, members, previousSrc = {}, now = Date.n
116
139
  if (!c) continue // npm 名字不在目录里(多半是 npm 上已经没有了)
117
140
  c.src.add(market)
118
141
  if (c.stars == null && Number.isFinite(m.s)) c.stars = m.s
119
- if (!c.n && m.added && (!c.added || m.added < c.added)) c.added = m.added
142
+ // 第一次被市场收录的日期(「新上架」「稳定可靠」要用)
143
+ if (m.added && (!c.added || m.added < c.added)) c.added = m.added
120
144
  }
121
145
  }
122
146
 
@@ -139,52 +163,93 @@ export function buildOverall({ packages, members, previousSrc = {}, now = Date.n
139
163
  const useOf = c => (c.rec ? (c.rec.deep ? c.rec.deep.base : (c.rec.w || 0) / 7 * ratio) : 0)
140
164
  const useP = spread(out.map(c => Math.log1p(useOf(c))))
141
165
  const starP = spread(out.map(c => Math.log1p(c.stars || 0)))
166
+ // 「最多人用」只认真实的去噪基线
167
+ const deepIdx = out.map((c, i) => (c.rec && c.rec.deep ? i : -1)).filter(i => i >= 0)
168
+ const deepP = spread(deepIdx.map(i => Math.log1p(out[i].rec.deep.base)))
169
+ const deepUse = new Map(deepIdx.map((i, k) => [i, deepP[k]]))
170
+ // 「上升最快」:日常基线 ≥ 20/天、有增速的,按增速排百分位
171
+ const riseIdx = out.map((c, i) => (c.rec && c.rec.deep && c.rec.deep.base >= 20 && Number.isFinite(c.rec.deep.growth) ? i : -1)).filter(i => i >= 0)
172
+ const riseP = percentiles(riseIdx.map(i => out[i].rec.deep.growth + 1)) // +1:增速可能是负数,百分位只认正数
173
+ const riseOf = new Map(riseIdx.map((i, k) => [i, riseP[k]]))
142
174
 
175
+ const curatedSet = new Set(['dsh-m', 'official'])
143
176
  out.forEach((c, i) => {
144
177
  const rec = c.rec
145
178
  const d = rec && rec.deep
146
- const m = maintScore([d && d.lastRelease, rec && rec.t, rec && rec.gh && rec.gh.p], now)
179
+ const gh = rec && rec.gh
180
+ const firstDays = c.added ? Math.max(0, Math.floor((now - Date.parse(c.added)) / DAY)) : null
181
+ const maint = maintScore([d && d.lastRelease, rec && rec.t, gh && gh.p], now)
182
+ const stable = stableScore({ firstDays, cadence: d ? d.cadence : null, license: gh ? Boolean(gh.l) : null, issues: gh ? gh.i : null, stars: c.stars })
183
+ const curated = [...c.src].some(m => curatedSet.has(m)) ? 1 : 0
147
184
  const parts = {
148
185
  use: useP[i],
149
186
  stars: starP[i],
150
- maint: m ?? 0.4, // 不知道就给个中间偏下的分
151
- consensus: c.src.size / MARKETS.length,
152
- health: d && Number.isFinite(d.s) ? d.s / 100 : rec && rec.gh && rec.gh.l ? 0.6 : 0.4,
187
+ maint: maint ?? 0.4, // 不知道就给个中间偏下的分
188
+ curated,
189
+ stable,
190
+ reach: c.src.size / MARKETS.length,
153
191
  }
154
192
  c.parts = parts
155
- c.score = Math.round(Object.entries(WEIGHTS).reduce((sum, [k, w]) => sum + parts[k] * w, 0))
156
- if (rec) {
157
- rec.sc = c.score
193
+ c.firstDays = firstDays
194
+ const rec100 = Math.round(Object.entries(WEIGHTS).reduce((sum, [k, w]) => sum + parts[k] * w, 0))
195
+ c.a = {
196
+ rec: rec100,
197
+ use: deepUse.has(i) ? Math.round(deepUse.get(i) * 100) : null,
198
+ rep: c.stars || curated ? Math.round((0.65 * starP[i] + 0.35 * curated) * 100) : null,
199
+ stable: firstDays != null && firstDays >= 60 && (maint ?? 0) > 0 ? Math.round((0.7 * stable + 0.3 * useP[i]) * 100) : null,
200
+ rise: riseOf.has(i) ? Math.round(riseOf.get(i) * 100) : null,
201
+ new: firstDays != null && firstDays <= 30 ? Math.round((1 - firstDays / 30) * 60 + useP[i] * 40) : null,
202
+ }
203
+ c.score = rec100
204
+ if (rec && !c.outside) {
205
+ rec.sc = rec100
158
206
  rec.src = [...c.src]
207
+ // 每个 npm 包都存一份六个角度的分数(顺序同 ANGLES,不参与的为 null)和第一次被收录的日期:
208
+ // 面板按分类筛选、给「我的监控」里的插件算名次,都要在全部插件里排,不能只看存下来的头部
209
+ rec.oa = ANGLES.map(k => c.a[k])
210
+ if (c.added) rec.ad = c.added
159
211
  }
160
212
  })
161
- out.sort((a, b) => b.score - a.score || (b.rec ? b.rec.w || 0 : 0) - (a.rec ? a.rec.w || 0 : 0))
213
+
214
+ // 每个角度取前 KEEP_PER_ANGLE 名,合并去重;按综合推荐排
215
+ const keep = new Set()
216
+ for (const angle of ANGLES) {
217
+ out.filter(c => c.a[angle] != null)
218
+ .sort((x, y) => y.a[angle] - x.a[angle] || (y.rec ? y.rec.w || 0 : 0) - (x.rec ? x.rec.w || 0 : 0))
219
+ .slice(0, KEEP_PER_ANGLE)
220
+ .forEach(c => keep.add(c))
221
+ }
222
+ const rows = [...keep].sort((x, y) => y.score - x.score || (y.rec ? y.rec.w || 0 : 0) - (x.rec ? x.rec.w || 0 : 0))
162
223
 
163
224
  const pct = v => Math.round(v * 100)
164
225
  return {
165
226
  total: out.length,
166
227
  excluded,
167
228
  weights: WEIGHTS,
229
+ angles: ANGLES,
168
230
  markets: MARKETS,
169
231
  useRatio: Math.round(ratio * 1000) / 1000,
170
- rows: out.slice(0, KEEP).map(c => {
232
+ rows: rows.map(c => {
171
233
  const rec = c.rec
234
+ const d = rec && rec.deep
172
235
  const row = {
173
236
  k: c.n || c.key, // npm 包名,或 gh:owner/repo
174
237
  n: c.n,
175
238
  sc: c.score,
176
- p: [pct(c.parts.use), pct(c.parts.stars), pct(c.parts.maint), pct(c.parts.consensus), pct(c.parts.health)],
239
+ a: c.a, // 六个角度各自的分数(不参与的为 null)
240
+ p: [pct(c.parts.use), pct(c.parts.stars), pct(c.parts.maint), pct(c.parts.curated), pct(c.parts.stable), pct(c.parts.reach)],
177
241
  src: [...c.src],
178
242
  }
179
243
  if (!c.n) {
180
244
  row.name = c.name
181
245
  row.repo = c.repo
182
- if (c.added) row.added = c.added
183
246
  }
247
+ if (c.added) row.added = c.added
184
248
  // 不在 npm 目录里的(官方插件):面板查不到它的记录,名字、版本、下载量跟着行走
185
249
  if (c.outside) Object.assign(row, { name: rec.n, v: rec.v, d: rec.d, w: rec.w })
186
250
  if (c.stars != null) row.s = c.stars
187
- if (rec && rec.deep && rec.deep.lastRelease) row.last = rec.deep.lastRelease
251
+ if (d && Number.isFinite(d.growth)) row.g = Math.round(d.growth * 100) / 100
252
+ if (d && d.lastRelease) row.last = d.lastRelease
188
253
  else if (rec && rec.t) row.last = String(rec.t).slice(0, 10)
189
254
  return row
190
255
  }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-pick",
3
- "version": "0.6.2",
3
+ "version": "0.7.1",
4
4
  "description": "帮你在 DSH 生态里选对插件:说需求 → 找候选 → 多维比较 → 出结论。下载量是证据,AI 是推理。",
5
5
  "repository": {
6
6
  "type": "git",
package/plugin/index.js CHANGED
@@ -15,6 +15,8 @@
15
15
  // POST /api-dsh-pick/uninstall 从 DSH 卸载本插件
16
16
  // GET /api-dsh-pick/installed DSH 里已装的插件名(面板的「已安装」标记)
17
17
  // GET /api-dsh-pick/live?names=a,b 实时查 npm(「我的监控」的刷新,最多 30 个)
18
+ // GET /api-dsh-pick/ai-pick 能不能用「AI 精选」(DSH 给不给插件调模型)
19
+ // POST /api-dsh-pick/ai-pick AI 精选:用 DSH 当前的模型按需求挑插件 { need }
18
20
  // GET /api-dsh-pick/market dsh-pick 的插件索引在不在 DSH「插件 → 发现」的索引源里
19
21
  // POST /api-dsh-pick/market 把它加进去(设置页的按钮)
20
22
  //
@@ -32,6 +34,7 @@ import { registerTools } from './tools.js'
32
34
  import { loadCatalog, liveStats, parseWatchNames } from '../lib/engine.js'
33
35
  import { refreshCatalog, remoteState, CHECK_EVERY_MS } from '../lib/remote.js'
34
36
  import { ensureMarketSource, marketStatus } from '../lib/market-source.js'
37
+ import { aiPick } from '../lib/ai-pick.js'
35
38
 
36
39
  /** 本包根目录:插件就是完整的 dsh-pick 包 */
37
40
  const PKG_ROOT = fileURLToPath(new URL('..', import.meta.url))
@@ -259,6 +262,12 @@ export function apply(ctx) {
259
262
  profile = ctx.get && ctx.get('profileContext') && ctx.get('profileContext').name
260
263
  } catch { profile = undefined }
261
264
 
265
+ // AI 精选:DSH 的模型(llm 服务)和默认模型选择。都是可选的,老版本 DSH 没有就不显示这个按钮
266
+ let llm, defaultModel
267
+ ctx.inject(['llm'], (c) => { llm = c.llm || (c.get && c.get('llm')) })
268
+ ctx.inject(['agentDefaultModel'], (c) => { defaultModel = c.agentDefaultModel || (c.get && c.get('agentDefaultModel')) })
269
+ const canAiPick = () => Boolean(llm && typeof llm.stream === 'function' && defaultModel && typeof defaultModel.currentSelection === 'function')
270
+
262
271
  // DSH「插件 → 发现」的索引源(实验功能「自定义插件索引」打开时才有这个服务):自动加一次 dsh-pick 的插件索引
263
272
  let pluginMarket
264
273
  ctx.inject(['pluginMarket'], (mCtx) => {
@@ -420,6 +429,27 @@ export function apply(ctx) {
420
429
  },
421
430
  }))
422
431
 
432
+ // AI 精选(点了才调用模型,花的是用户的 token)
433
+ track(ws.register({
434
+ kind: 'exact',
435
+ path: '/api-dsh-pick/ai-pick',
436
+ handler: async (req, res) => {
437
+ if (req.method === 'GET') return sendJson(res, 200, { available: canAiPick() })
438
+ if (!isSafePost(req)) return sendJson(res, 403, { ok: false, message: '只接受本页面发出的请求' })
439
+ if (!canAiPick()) return sendJson(res, 200, { ok: false, message: '这个版本的 DSH 不让插件直接调用模型,可以点「+AI 帮我挑」在对话里问' })
440
+ const { need } = await readJsonBody(req)
441
+ try {
442
+ const result = await aiPick({
443
+ need, catalog: await loadCatalog(), llm, defaultModel,
444
+ installed: (await installedNames(pluginManager)) || new Set(),
445
+ })
446
+ sendJson(res, 200, { ok: true, ...result })
447
+ } catch (e) {
448
+ sendJson(res, 200, { ok: false, message: e.code === 'empty' ? e.message : `AI 精选失败:${e.message}` })
449
+ }
450
+ },
451
+ }))
452
+
423
453
  // 「发现」的索引源:查状态 / 用户点按钮加进去
424
454
  track(ws.register({
425
455
  kind: 'exact',
package/plugin/tools.js CHANGED
@@ -246,6 +246,7 @@ export async function buildToolDefinitions(deps = {}) {
246
246
  metric: { type: 'string', enum: ['weekly', 'baseline', 'growth', 'score', 'stars', 'pushed', 'downloads', 'newest'], description: '排序指标,默认 baseline(日常基线,最接近真实使用量);stars = GitHub 星标,pushed = GitHub 最近提交' },
247
247
  category: { type: 'string', enum: CATS, description: '只看某一类(可省略)' },
248
248
  limit: { type: 'integer', description: '前几名,默认 10,最多 30' },
249
+ angle: { type: 'string', enum: ['rec', 'use', 'rep', 'stable', 'rise', 'new'], description: 'board=overall 时的排序角度:rec 综合推荐(默认)· use 最多人用(只看去噪基线)· rep 口碑(星标 + 精选/官方)· stable 稳定可靠 · rise 上升最快 · new 新上架' },
249
250
  board: { type: 'string', enum: ['overall', 'official', 'community', 'shop'], description: '看别的榜单(可省略,省略就是 npm / GitHub):overall = 综合榜(几个插件市场合并去重,按综合分排,用户问「最值得装的」先用它);official = DeepSeek 官方插件;community = awesome-dsh-plugin 社区目录(dshmarket 用的就是它);shop = dsh-plugin-shop 插件商店(第三方)。community / shop 时 metric 可填 stars / downloads / newest' },
250
251
  },
251
252
  output: {
@@ -263,16 +264,29 @@ export async function buildToolDefinitions(deps = {}) {
263
264
  if (!o) return plain({ header: header(cat), board: '综合榜:还没有数据', rows: [] })
264
265
  const W = o.weights
265
266
  const LABEL = { dshmarket: 'dshmarket', 'dsh-m': 'dsh-m 精选', shop: 'dsh-plugin-shop', official: 'DeepSeek 官方' }
267
+ const ANGLE = { rec: '综合推荐', use: '最多人用(去噪基线)', rep: '口碑(星标 + 精选/官方)', stable: '稳定可靠', rise: '上升最快(近 7 日增速)', new: '新上架(30 天内被收录)' }
268
+ const angle = ANGLE[args.angle] && o.rows[0] && o.rows[0].a ? args.angle : 'rec'
269
+ const val = r => (r.a ? r.a[angle] : r.sc)
266
270
  const category = CATS.includes(args.category) ? args.category : null
267
271
  const have = await installed()
268
- const rows = o.rows.filter(r => !category || (r.n && cat.packages[r.n] && cat.packages[r.n].cat === category)).slice(0, limit)
272
+ const rows = o.rows
273
+ .filter(r => val(r) != null && (!category || (r.n && cat.packages[r.n] && cat.packages[r.n].cat === category)))
274
+ .sort((x, y) => angle === 'new' ? String(y.added || '').localeCompare(String(x.added || '')) || val(y) - val(x) : val(y) - val(x))
275
+ .slice(0, limit)
276
+ const parts = r => (r.p && r.p.length >= 6
277
+ ? `使用 ${r.p[0]} · 关注 ${r.p[1]} · 维护 ${r.p[2]} · 精选 ${r.p[3]} · 稳定 ${r.p[4]} · 覆盖 ${r.p[5]}`
278
+ : (r.p || []).join(' · '))
279
+ const formula = W.curated != null
280
+ ? `综合推荐 = 使用 ${W.use}% + 关注 ${W.stars}% + 维护 ${W.maint}% + 精选 ${W.curated}% + 稳定 ${W.stable}% + 覆盖 ${W.reach}%`
281
+ : `综合分 = ${Object.entries(W).map(([k, v]) => `${k} ${v}%`).join(' + ')}`
269
282
  return plain({
270
283
  header: header(cat),
271
- board: `综合榜(${num(o.total)} 个插件参与排名;综合分 = 使用 ${W.use}% + 关注 ${W.stars}% + 维护 ${W.maint}% + 共识 ${W.consensus}% + 健康 ${W.health}%,各项 0~100)`,
284
+ board: `综合榜 · ${ANGLE[angle]}(${num(o.total)} 个插件参与排名;${formula},各项 0~100)`,
272
285
  rows: rows.map((r, i) => {
273
286
  const rec = r.n ? cat.packages[r.n] : null
274
287
  const name = r.n || r.name
275
- return `${i + 1}. ${name} · 综合分 ${r.sc}(使用 ${r.p[0]} · 关注 ${r.p[1]} · 维护 ${r.p[2]} · 共识 ${r.p[3]} · 健康 ${r.p[4]})` +
288
+ const angleNote = angle === 'rec' ? '' : angle === 'rise' && r.g != null ? ` · 近 7 日增速 ${Math.round(r.g * 100)}%` : angle === 'new' && r.added ? ` · ${r.added} 被收录` : ` · ${ANGLE[angle]}分 ${val(r)}`
289
+ return `${i + 1}. ${name} · 综合推荐 ${r.sc}(${parts(r)})${angleNote}` +
276
290
  ` · 收录于 ${r.src.map(k => LABEL[k] || k).join('、') || '(只在 npm)'}` +
277
291
  (rec ? ` · 周下载 ${num(rec.w)}` : r.w != null ? ` · 周下载 ${num(r.w)}` : '') +
278
292
  (r.s != null ? ` · ★${num(r.s)}` : '') +
package/web/panel.html CHANGED
@@ -115,6 +115,33 @@ h1{margin:0;font-size:19px;font-weight:650;letter-spacing:-.2px}
115
115
  table.rank td.nm .sub{font-size:10.5px;color:var(--t3);font-weight:400;margin-top:2px}
116
116
  table.rank td.nm .desc{min-width:150px;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden}
117
117
  /* 综合榜:收录于哪几个市场 */
118
+ /* 比较清单 */
119
+ .pickbar{display:flex;flex-wrap:wrap;align-items:center;gap:6px 8px;margin-top:14px;padding-top:12px;border-top:1px dashed var(--line);font-size:12px}
120
+ .pickbar .lbl{color:var(--t3);font-weight:600}
121
+ .pickbar .none{color:var(--t3)}
122
+ .pickchip{display:inline-flex;align-items:center;gap:4px;background:var(--accent-bg);color:var(--accent);border-radius:12px;padding:2px 4px 2px 9px;font-weight:600}
123
+ .pickchip button{appearance:none;border:none;background:none;color:inherit;cursor:pointer;font-size:14px;line-height:1;padding:0 3px}
124
+ .addwrap{position:relative;flex:1;min-width:160px}
125
+ .addwrap input{width:100%;box-sizing:border-box;border:1px solid var(--line);border-radius:8px;padding:6px 10px;font-size:12.5px;font-family:inherit;background:var(--input);color:var(--text)}
126
+ .addwrap input:focus{outline:none;border-color:var(--accent)}
127
+ .sug{position:absolute;left:0;right:0;top:100%;margin-top:4px;z-index:50;background:var(--panel);border:1px solid var(--line);border-radius:9px;box-shadow:0 6px 18px var(--shadow);max-height:300px;overflow:auto}
128
+ .sug .opt{padding:7px 10px;cursor:pointer;border-bottom:1px solid var(--soft)}
129
+ .sug .opt:last-child{border-bottom:none}
130
+ .sug .opt.on,.sug .opt:hover{background:var(--accent-bg)}
131
+ .sug .opt b{color:var(--text);font-size:12.5px}
132
+ .sug .opt span{display:block;color:var(--t3);font-size:11px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
133
+ /* AI 精选 */
134
+ .aip{border:1px solid var(--line);border-radius:11px;padding:11px 13px;margin-bottom:9px;background:var(--input)}
135
+ .aip .top{display:flex;align-items:center;gap:8px;flex-wrap:wrap}
136
+ .aip .nm{font-weight:650;font-size:13.5px;color:var(--text)}
137
+ .aip .fit{font-size:11px;font-weight:700;color:var(--ai);background:color-mix(in srgb,var(--ai) 14%,transparent);border-radius:10px;padding:1px 8px}
138
+ .aip .why{font-size:12.5px;color:var(--text);margin:6px 0 2px;line-height:1.55}
139
+ .aip .cav{font-size:11.5px;color:var(--warn-text)}
140
+ .aip .data{font-size:11px;color:var(--t3);margin-top:5px;font-variant-numeric:tabular-nums}
141
+ .aip .acts{display:flex;align-items:center;gap:6px;margin-top:8px;flex-wrap:wrap}
142
+ .aip .cmpbtn{appearance:none;font-family:inherit;font-size:11px;font-weight:600;border:1px solid var(--line);border-radius:6px;padding:2px 9px;background:var(--panel);color:var(--t2);cursor:pointer}
143
+ .aip .cmpbtn.on{background:var(--accent-bg);color:var(--accent);border-color:var(--accent)}
144
+ .ai-wait{color:var(--t3);font-size:12.5px;padding:8px 0}
118
145
  .srcchip{display:inline-block;font-size:10px;font-weight:500;color:var(--t2);background:var(--soft);border-radius:4px;padding:0 5px;margin:2px 3px 0 0;white-space:nowrap}
119
146
  .srcchip.off{color:var(--accent);background:var(--accent-bg)}
120
147
  .srcchip.gh{color:var(--t3)}
@@ -205,6 +232,14 @@ table.cmp th:first-child,table.cmp td:first-child{text-align:left;color:var(--t3
205
232
  table.cmp thead th{color:var(--text);font-weight:650;font-size:12.5px}
206
233
  table.cmp thead th i{display:inline-block;width:8px;height:8px;border-radius:2px;margin-right:6px}
207
234
  table.cmp tbody tr:last-child td{border-bottom:none}
235
+ /* 监控详情:第一列(指标名)固定,插件多了横向滚动 */
236
+ table.cmp.wd{min-width:0}
237
+ table.cmp.wd th:first-child,table.cmp.wd td:first-child{position:sticky;left:0;background:var(--panel);z-index:1}
238
+ table.cmp.wd td{font-size:12px;padding:7px 9px}
239
+ table.cmp.wd .ch{display:block;font-size:10.5px;font-weight:600}
240
+ table.cmp.wd .ch.up{color:var(--ok)}table.cmp.wd .ch.down{color:var(--bad)}table.cmp.wd .ch.flat{color:var(--t3);font-weight:400}
241
+ table.cmp.wd .grp td{background:var(--soft);color:var(--t3);font-size:10.5px;font-weight:700;padding:4px 9px;text-align:left}
242
+ table.cmp.wd svg.spark{display:block;margin-left:auto}
208
243
  td.best{color:var(--ok);font-weight:700}
209
244
  td.best::after{content:' ★';font-size:9px}
210
245
  .miss{color:var(--mute)}
@@ -238,6 +273,15 @@ table.rank td.mt .stale{color:var(--warn-text);font-weight:600}
238
273
  /* 只有图标的外链(排行榜) */
239
274
  table.rank td.lks{white-space:nowrap;width:1%;position:sticky;right:0;background:var(--panel);box-shadow:-6px 0 6px -6px var(--shadow)}
240
275
  table.rank tbody tr:hover td.lks{background:var(--panel)}
276
+ /* 名次;我的监控里的插件高亮(固定的操作列也要跟着染色,不然盖住一块) */
277
+ table.rank td.rk{color:var(--t3);font-variant-numeric:tabular-nums;font-size:11.5px;letter-spacing:-.3px}
278
+ table.rank tr.watched td,table.rank tr.watched:hover td.lks{background:color-mix(in srgb,var(--accent-bg) 70%,var(--panel))}
279
+ table.rank tr.watched td.rk{color:var(--accent);font-weight:700}
280
+ table.rank tr.watched td:first-child{box-shadow:inset 3px 0 0 var(--accent)}
281
+ table.rank tr.miss td.nm{color:var(--t2,var(--t3))}
282
+ table.rank tr.wsep td{padding:10px 8px 4px;font-size:11.5px;font-weight:650;color:var(--accent);background:var(--panel);border-top:1px dashed var(--accent-line)}
283
+ table.rank tr.more td{text-align:center;padding:10px 0;background:var(--panel)}
284
+ table.rank tr.more .btn{font-size:12px;padding:5px 14px}
241
285
  /* 彩色 + 号:一键加入我的监控;已加入显示 ✓,再点取消 */
242
286
  .wadd{appearance:none;display:inline-flex;align-items:center;justify-content:center;width:24px;height:22px;padding:0;margin-right:4px;
243
287
  border:none;border-radius:6px;cursor:pointer;font-family:inherit;font-size:15px;font-weight:700;line-height:1;color:#fff;vertical-align:top;
@@ -358,6 +402,7 @@ td.down{color:var(--bad);font-weight:600}
358
402
  <div class="row">
359
403
  <input id="need" placeholder="例如:我想装个数据库 / 我想让 DSH 能连微信 / 记忆能力" autocomplete="off">
360
404
  <button class="btn" id="btnFind">找候选</button>
405
+ <button class="btn ghost hidden" id="btnAi" title="用 DSH 当前的大模型读懂你的需求,再从目录里挑真正合适的(会消耗你的模型额度)">AI 精选</button>
361
406
  </div>
362
407
  <div class="examples">
363
408
  <span>试试:</span>
@@ -369,6 +414,21 @@ td.down{color:var(--bad);font-weight:600}
369
414
  <button class="chip">费用统计</button>
370
415
  <button class="chip">远程服务器</button>
371
416
  </div>
417
+ <!-- 比较清单:搜索结果之外的插件,输入名字也能加进来比较 -->
418
+ <div class="pickbar">
419
+ <span class="lbl">比较清单</span>
420
+ <span id="pickChips"><span class="none">还没有,点候选卡片或在右边输入插件名</span></span>
421
+ <span class="addwrap">
422
+ <input id="addCmp" placeholder="输入插件名,加入比较" autocomplete="off" aria-label="输入插件名,加入比较">
423
+ <div id="addSug" class="sug hidden" role="listbox"></div>
424
+ </span>
425
+ </div>
426
+ </div>
427
+
428
+ <div class="section hidden" id="secAi">
429
+ <h2><span class="n">✦</span>AI 精选 <span style="font-size:11.5px;color:var(--t3);font-weight:500" id="aiMeta"></span></h2>
430
+ <p class="cap">用 DSH 当前的大模型:先把需求展开成一组关键字,从全部目录里召回候选,再逐个读简介和数据,挑出真正能解决问题的。理由是模型写的,数字来自目录。</p>
431
+ <div id="aiList"></div>
372
432
  </div>
373
433
 
374
434
  <div class="section hidden" id="secCand">
@@ -412,6 +472,12 @@ td.down{color:var(--bad);font-weight:600}
412
472
  </div>
413
473
  <p class="cap" id="rankCap"></p>
414
474
  <div class="filters" id="rankFilters">
475
+ <button class="chip on" data-board="overall" data-m="rec">综合推荐</button>
476
+ <button class="chip" data-board="overall" data-m="use">最多人用</button>
477
+ <button class="chip" data-board="overall" data-m="rep">口碑</button>
478
+ <button class="chip" data-board="overall" data-m="stable">稳定可靠</button>
479
+ <button class="chip" data-board="overall" data-m="rise">上升最快</button>
480
+ <button class="chip" data-board="overall" data-m="new">新上架</button>
415
481
  <button class="chip on hidden" data-board="npm" data-m="base">日常基线</button>
416
482
  <button class="chip hidden" data-board="npm" data-m="w">周下载</button>
417
483
  <button class="chip hidden" data-board="npm" data-m="growth">增速最快</button>
@@ -443,6 +509,11 @@ td.down{color:var(--bad);font-weight:600}
443
509
  <p class="cap"><span id="watchAsOf"></span>监控列表存在浏览器本地,不上传。点「刷新」去 npm 和 GitHub 查最新:版本号、发版时间、GitHub 数据是即时的;周下载 npm 大约一天更新一次;日常基线和环比按 npm 已经出数据的最近 30 天算(npm 只在一天结束后才汇总,最近还常晚几天)。</p>
444
510
  <div id="watchList"></div>
445
511
  </div>
512
+ <div class="section" id="secWatchDetail">
513
+ <h2>监控详情 <span style="font-size:11.5px;color:var(--t3);font-weight:500" id="wdAsOf"></span></h2>
514
+ <p class="cap">每个插件一列,下载、版本、GitHub、市场位置都摆出来。「较上次」是和你上一次点刷新时比:每次刷新都会在本机存一份快照,盯自家产品和竞品的变化。</p>
515
+ <div class="tblwrap"><table class="cmp wd"><thead id="wdHead"></thead><tbody id="wdBody"></tbody></table></div>
516
+ </div>
446
517
  <div class="section">
447
518
  <h2>对比图</h2>
448
519
  <p class="cap">对数刻度,否则头部插件会把其余压成一条线。</p>