argo-search 2.16.0 → 2.17.0
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/AGENTS.md +3 -1
- package/README.es.md +9 -4
- package/README.ja.md +9 -4
- package/README.ko.md +9 -4
- package/README.md +9 -4
- package/README.zh.md +9 -4
- package/SKILL.md +1 -1
- package/assets/readme/counts.json +1 -1
- package/assets/readme/hero.svg +3 -3
- package/assets/readme/hero.template.svg +121 -0
- package/assets/readme/proof-routes.svg +17 -16
- package/backends/domain_profiles.json +115 -18
- package/backends/engine_registry.yaml +11 -0
- package/backends/env_passthrough.txt +2 -0
- package/backends/quota_profiles.json +10 -0
- package/docs/CHANGELOG.md +17 -1
- package/docs/COMMIT_STANDARD.md +10 -0
- package/docs/ENGINE_CATALOG.md +32 -30
- package/docs/RELEASE_NOTES_v2.17.0.md +64 -0
- package/docs/RELEASING.md +1 -1
- package/engines/specs/crossref.yaml +2 -0
- package/engines/specs/hackernews.yaml +2 -0
- package/engines/specs/helve.yaml +49 -0
- package/engines/specs/hf_papers.yaml +2 -0
- package/engines/specs/openalex.yaml +9 -2
- package/package.json +2 -1
- package/packages/dsh-plugin/README.md +5 -2
- package/packages/dsh-plugin/cordis.patch.yml +4 -1
- package/packages/dsh-plugin/dsh/client.js +57 -63
- package/packages/dsh-plugin/dsh/diagnostics.js +25 -4
- package/packages/dsh-plugin/dsh/index.js +109 -21
- package/packages/dsh-plugin/dsh/route-guard.js +95 -0
- package/packages/dsh-plugin/dsh/runner.js +49 -6
- package/packages/dsh-plugin/dsh/wide-research.js +20 -18
- package/packages/dsh-plugin/dsh/workbench-client.js +119 -20
- package/packages/dsh-plugin/dsh/workbench-io.js +163 -0
- package/packages/dsh-plugin/dsh/workbench-search.js +385 -0
- package/packages/dsh-plugin/dsh/workbench-suggestions.js +387 -75
- package/packages/dsh-plugin/dsh/workbench.js +234 -497
- package/packages/dsh-plugin/package.json +4 -3
- package/packages/dsh-plugin/scripts/sync-local.mjs +4 -2
- package/packages/dsh-plugin/scripts/sync-local.sh +4 -2
- package/packages/dsh-plugin/test/client.test.js +26 -0
- package/packages/dsh-plugin/test/diagnostics.test.js +5 -2
- package/packages/dsh-plugin/test/index.test.js +150 -1
- package/packages/dsh-plugin/test/workbench-concurrency.test.js +98 -0
- package/packages/dsh-plugin/test/workbench.test.js +337 -9
- package/scripts/adaptive.py +13 -11
- package/scripts/browser_auth.py +178 -4
- package/scripts/cache.py +68 -69
- package/scripts/check_commit_report.py +46 -0
- package/scripts/chrome_cdp.py +71 -0
- package/scripts/circuit_breaker.py +77 -12
- package/scripts/config_cache.py +4 -3
- package/scripts/daemon.py +9 -2
- package/scripts/engine_add.py +45 -0
- package/scripts/engine_dispatch.py +27 -47
- package/scripts/engine_failure.py +33 -0
- package/scripts/engine_fingerprint.py +114 -0
- package/scripts/engine_health.py +130 -0
- package/scripts/engine_states.py +50 -0
- package/scripts/engines_base.py +7 -16
- package/scripts/engines_builders_search.py +19 -8
- package/scripts/engines_builders_tech.py +16 -16
- package/scripts/engines_fields.py +23 -0
- package/scripts/evidence_grounding.py +182 -0
- package/scripts/fetch_browser.py +25 -10
- package/scripts/gate_manifest_check.py +75 -0
- package/scripts/gen_readme_art.py +264 -0
- package/scripts/mcp_transport.py +24 -5
- package/scripts/numeric_verify.py +9 -0
- package/scripts/pre-commit +57 -8
- package/scripts/query_operators.py +229 -1
- package/scripts/quota.py +19 -37
- package/scripts/release.py +16 -1
- package/scripts/research_gates.py +85 -1
- package/scripts/route_combo.py +13 -1
- package/scripts/source_discovery.py +611 -0
- package/scripts/sync_backends.py +45 -0
- package/sub-skills/ego-search/README.md +9 -0
- package/sub-skills/ego-search/scripts/ego_search.py +182 -15
- package/sub-skills/ego-search/scripts/pacing.py +291 -0
- package/sub-skills/ego-search/scripts/quality.py +262 -6
- package/sub-skills/ego-search/scripts/runtime.py +7 -1
- package/sub-skills/ego-search/scripts/site_profiles.py +157 -0
- package/sub-skills/ego-search/scripts/webbridge_adapter.py +7 -1
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @taxueseek/argo-dsh — 工作台 I/O 与进程执行辅助(CLI 定位、静态资源缓存、定时监控持久化)
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { spawn } from 'node:child_process'
|
|
6
|
+
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from 'node:fs'
|
|
7
|
+
import { homedir } from 'node:os'
|
|
8
|
+
import { dirname, join } from 'node:path'
|
|
9
|
+
import { fileURLToPath } from 'node:url'
|
|
10
|
+
import { resolveStateDir } from './diagnostics.js'
|
|
11
|
+
|
|
12
|
+
const HERE = dirname(fileURLToPath(import.meta.url))
|
|
13
|
+
const MONOREPO_ARGO_BIN = join(HERE, '..', '..', '..', 'bin', 'argo')
|
|
14
|
+
// install.sh 默认安装根(ARGO_HOME 可改,此处只探测默认路径——探测不到就走
|
|
15
|
+
// console script 的 PATH 兜底,不猜 site-packages 里的 wheel _data 布局)
|
|
16
|
+
const INSTALL_VENV_ARGO_BIN = join(homedir(), '.local', 'share', 'argo', '.venv', 'bin', 'argo')
|
|
17
|
+
export const WATCH_STORE_FILE = join(homedir(), '.config', 'argo', 'workbench-watches.json')
|
|
18
|
+
|
|
19
|
+
export function mergedLaunchdPath(env = {}) {
|
|
20
|
+
const extraPath = ['/opt/homebrew/bin', '/usr/local/bin', join(homedir(), '.local', 'bin')].join(':')
|
|
21
|
+
const basePath = env.PATH || process.env.PATH || ''
|
|
22
|
+
return basePath ? `${extraPath}:${basePath}` : extraPath
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// 2026-11-20 审计:npm 发布版插件装在 DSH 缓存目录时 MONOREPO_ARGO_BIN 不存在,
|
|
26
|
+
// 裸名 'argo' 又依赖 PATH——launchd/受限 PATH 下 venv console script 可能不可达。
|
|
27
|
+
// 修法加候选不重排:显式 ARGO_BIN 最优先(用户指路零猜测),其次 monorepo、
|
|
28
|
+
// install.sh 默认 venv,最后裸名走 PATH。
|
|
29
|
+
export function resolveArgoBin(env = {}) {
|
|
30
|
+
const explicit = env.ARGO_BIN || process.env.ARGO_BIN
|
|
31
|
+
if (explicit) return explicit
|
|
32
|
+
if (existsSync(MONOREPO_ARGO_BIN)) return MONOREPO_ARGO_BIN
|
|
33
|
+
if (existsSync(INSTALL_VENV_ARGO_BIN)) return INSTALL_VENV_ARGO_BIN
|
|
34
|
+
return 'argo'
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function runArgoCli(args, { env = {}, stdin = null, timeoutMs = 30000, signal = null } = {}) {
|
|
38
|
+
const mergedPath = mergedLaunchdPath(env)
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
if (signal?.aborted) { reject(new Error('aborted')); return }
|
|
41
|
+
const child = spawn(resolveArgoBin({ ...env, PATH: mergedPath }), args, {
|
|
42
|
+
env: { ...process.env, ...env, PATH: mergedPath },
|
|
43
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
44
|
+
})
|
|
45
|
+
const out = []
|
|
46
|
+
const err = []
|
|
47
|
+
const timer = setTimeout(() => {
|
|
48
|
+
// 超时路径也要摘掉 abort 监听器:长生命周期 AbortSignal 下残留会累积
|
|
49
|
+
signal?.removeEventListener('abort', onAbort)
|
|
50
|
+
child.kill('SIGTERM')
|
|
51
|
+
reject(new Error(`argo ${args[0] || ''} timeout (${timeoutMs}ms)`))
|
|
52
|
+
}, timeoutMs)
|
|
53
|
+
if (typeof timer.unref === 'function') timer.unref()
|
|
54
|
+
const onAbort = () => {
|
|
55
|
+
clearTimeout(timer)
|
|
56
|
+
child.kill('SIGTERM')
|
|
57
|
+
reject(new Error('aborted'))
|
|
58
|
+
}
|
|
59
|
+
signal?.addEventListener('abort', onAbort, { once: true })
|
|
60
|
+
child.stdout.on('data', d => out.push(d))
|
|
61
|
+
child.stderr.on('data', d => err.push(d))
|
|
62
|
+
child.on('error', (e) => {
|
|
63
|
+
clearTimeout(timer)
|
|
64
|
+
signal?.removeEventListener('abort', onAbort)
|
|
65
|
+
reject(e)
|
|
66
|
+
})
|
|
67
|
+
child.on('close', (code) => {
|
|
68
|
+
clearTimeout(timer)
|
|
69
|
+
signal?.removeEventListener('abort', onAbort)
|
|
70
|
+
const text = Buffer.concat(out).toString('utf8').trim()
|
|
71
|
+
if (code !== 0 && !text) {
|
|
72
|
+
reject(new Error(Buffer.concat(err).toString('utf8').trim() || `argo exit ${code}`))
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
resolve(JSON.parse(text || '{}'))
|
|
77
|
+
} catch {
|
|
78
|
+
reject(new Error(`invalid json from argo ${args[0] || ''}`))
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
if (stdin !== null && stdin !== undefined) {
|
|
82
|
+
child.stdin.end(typeof stdin === 'string' ? stdin : JSON.stringify(stdin))
|
|
83
|
+
} else {
|
|
84
|
+
child.stdin.end()
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const staticAssetCache = new Map()
|
|
90
|
+
|
|
91
|
+
export function readStaticAssetCached(filePath) {
|
|
92
|
+
const st = statSync(filePath)
|
|
93
|
+
const hit = staticAssetCache.get(filePath)
|
|
94
|
+
if (hit && hit.mtimeMs === st.mtimeMs && hit.size === st.size) {
|
|
95
|
+
return hit.text
|
|
96
|
+
}
|
|
97
|
+
const text = readFileSync(filePath, 'utf8')
|
|
98
|
+
staticAssetCache.set(filePath, { mtimeMs: st.mtimeMs, size: st.size, text })
|
|
99
|
+
return text
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function loadWatches() {
|
|
103
|
+
try {
|
|
104
|
+
const raw = readFileSync(WATCH_STORE_FILE, 'utf8')
|
|
105
|
+
const parsed = JSON.parse(raw)
|
|
106
|
+
return Array.isArray(parsed) ? parsed : []
|
|
107
|
+
} catch {
|
|
108
|
+
return []
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function saveWatches(items) {
|
|
113
|
+
try {
|
|
114
|
+
mkdirSync(dirname(WATCH_STORE_FILE), { recursive: true })
|
|
115
|
+
const uniq = `${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2, 6)}`
|
|
116
|
+
const tmp = `${WATCH_STORE_FILE}.tmp.${uniq}`
|
|
117
|
+
writeFileSync(tmp, JSON.stringify(items, null, 2), 'utf8')
|
|
118
|
+
renameSync(tmp, WATCH_STORE_FILE)
|
|
119
|
+
} catch { /* ignore */ }
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// 跨进程状态文件互斥(wx 独占创建,语义对齐 scripts/pacing.py 的非阻塞文件锁):
|
|
123
|
+
// 抢不到返回 null 让调用方让路;崩溃残留超 staleMs 强取自愈。
|
|
124
|
+
// 热榜与 watches 共用同一实现,消灭各自手抄(2026-11-21 审计:锁方案四家并存)。
|
|
125
|
+
export function acquireStateFileLock(file, staleMs = 5 * 60 * 1000) {
|
|
126
|
+
try {
|
|
127
|
+
return { fd: openSync(file, 'wx'), lockFile: file }
|
|
128
|
+
} catch (err) {
|
|
129
|
+
if (err?.code !== 'EEXIST') return null
|
|
130
|
+
try {
|
|
131
|
+
if (Date.now() - statSync(file).mtimeMs > staleMs) {
|
|
132
|
+
unlinkSync(file)
|
|
133
|
+
return { fd: openSync(file, 'wx'), lockFile: file }
|
|
134
|
+
}
|
|
135
|
+
} catch { /* 对方刚释放或刚重建,本轮回让路 */ }
|
|
136
|
+
return null
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function releaseStateFileLock(lock) {
|
|
141
|
+
if (!lock) return
|
|
142
|
+
try { closeSync(lock.fd) } catch { /* ignore */ }
|
|
143
|
+
try { unlinkSync(lock.lockFile) } catch { /* ignore */ }
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// watches 唯一真源是磁盘文件:读-改-写全程持锁,内存副本只作缓存。
|
|
147
|
+
// 旧「内存快照 + 全量覆盖写」在多宿主下互相抹改(2026-11-21 审计 P1)。
|
|
148
|
+
// 返回 null 表示锁被占(另一宿主写盘中),调用方按忙处理。
|
|
149
|
+
export function mutateWatches(mutator) {
|
|
150
|
+
const lock = acquireStateFileLock(`${WATCH_STORE_FILE}.lock`)
|
|
151
|
+
if (!lock) return null
|
|
152
|
+
try {
|
|
153
|
+
const next = mutator(loadWatches())
|
|
154
|
+
if (Array.isArray(next)) saveWatches(next)
|
|
155
|
+
return Array.isArray(next) ? next : loadWatches()
|
|
156
|
+
} finally {
|
|
157
|
+
releaseStateFileLock(lock)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function breakerFilePath() {
|
|
162
|
+
return join(resolveStateDir(), 'circuit_breaker.json')
|
|
163
|
+
}
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @taxueseek/argo-dsh — 工作台检索管线:模式规格、卡片归一、CLI 编排与 LLM 直流。
|
|
3
|
+
* 自 workbench.js 拆出以守 1000 行门禁(2026-11-21);HTTP 骨架与宿主 handler 留在 workbench.js。
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { homedir } from 'node:os'
|
|
7
|
+
import { join } from 'node:path'
|
|
8
|
+
import { DEFAULTS, stripBoundary } from './runner.js'
|
|
9
|
+
import { runArgoCli } from './workbench-io.js'
|
|
10
|
+
import { boundedMap } from './wide-research.js'
|
|
11
|
+
|
|
12
|
+
export const CITATION_RULE = [
|
|
13
|
+
'严格基于提供的检索证据回答,所有事实判断、数据与观点必须在句末使用方括号编号引用,例如 [1] 或 [2][3]。',
|
|
14
|
+
'严禁编造未出现在证据中的数字、链接或来源;如果证据不足或存在冲突,直接指出并并列呈现不同来源。',
|
|
15
|
+
'使用清晰的中文 Markdown 结构(小标题、要点列表或对比表格),先给核心结论,再展开细节。',
|
|
16
|
+
].join('\n')
|
|
17
|
+
|
|
18
|
+
export const MODE_SPECS = {
|
|
19
|
+
general: { label: '全网搜索', searchArgs: { max_results: 12, verify: 2, depth: 'balanced' }, prompt: '当前模式为「全网搜索」:先用 2 到 3 句话给出直接结论,再用清晰小标题展开关键依据与延伸信息。' },
|
|
20
|
+
hotspot: { label: '热点分析', searchArgs: { max_results: 14, sort: 'newest', since: '14d', depth: 'balanced', verify: 2 }, socialSupplement: true, prompt: '当前模式为「热点分析」:按「一、核心结论与现状」「二、事件脉络」「三、核心争议与各方立场」「四、影响与后续关注点」组织输出。' },
|
|
21
|
+
verify: { label: '真假核验', searchArgs: { max_results: 12, verify: 5, depth: 'deep', mode: 'deep' }, prompt: '当前模式为「真假核验」:按「一、核验结论(明确给出可信度判断:可信 / 存疑 / 失实 / 证据不足)」「二、关键证据链(优先引用已核验正文)」「三、冲突点与核验边界」组织输出。' },
|
|
22
|
+
research: { label: '深度研究', searchArgs: { max_results: 16, verify: 5, depth: 'deep', mode: 'deep' }, prompt: '当前模式为「深度研究」:按「一、执行摘要」「二、多维拆解与关键发现」「三、数据与案例支撑」「四、风险、局限与待验证项」组织详尽研究报告。' },
|
|
23
|
+
compare: { label: '选型对比', searchArgs: { max_results: 14, verify: 3, depth: 'balanced' }, prompt: '当前模式为「选型对比」:按「一、选型结论与推荐」「二、核心维度对比表(Markdown 表格)」「三、各自优缺点与适用人群」「四、替代方案与踩坑提示」组织输出。' },
|
|
24
|
+
academic: { label: '学术文献', searchArgs: { max_results: 14, domain: 'academic', depth: 'deep', mode: 'deep', verify: 3 }, prompt: '当前模式为「学术文献」:按「一、核心研究结论与共识」「二、代表性论文与方法拆解」「三、实验证据与关键数据」「四、局限性与延伸方向」组织输出。' },
|
|
25
|
+
plugin: { label: '插件评测', searchArgs: { max_results: 14, domain: 'dsh_plugin_search', verify: 3, depth: 'balanced' }, socialSupplement: true, prompt: '当前模式为「插件评测」:按「一、是否值得安装(结论)」「二、核心能力与亮点」「三、社区真实反馈与局限」「四、替代方案与安装配置注意」组织输出。' },
|
|
26
|
+
local: { label: '本机检索', searchArgs: { max_results: 16 }, prompt: '当前模式为「本机检索」:基于本机文件与代码命中证据回答,标明文件路径、行号与上下文关联,禁止编造未命中的本地文件。' },
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const MODES = new Set(Object.keys(MODE_SPECS))
|
|
30
|
+
export const MODE_LABELS = Object.fromEntries(Object.entries(MODE_SPECS).map(([k, v]) => [k, v.label]))
|
|
31
|
+
|
|
32
|
+
function extractDomain(url) {
|
|
33
|
+
try {
|
|
34
|
+
return new URL(url).hostname.replace(/^www\./i, '')
|
|
35
|
+
} catch {
|
|
36
|
+
return ''
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function normalizeCandidates(payload) {
|
|
41
|
+
const rawList = Array.isArray(payload?.results) ? payload.results : []
|
|
42
|
+
const seen = new Set()
|
|
43
|
+
const cards = []
|
|
44
|
+
for (const item of rawList) {
|
|
45
|
+
if (!item || typeof item.url !== 'string' || !item.url.startsWith('http')) continue
|
|
46
|
+
const key = item.url.trim()
|
|
47
|
+
if (seen.has(key)) continue
|
|
48
|
+
seen.add(key)
|
|
49
|
+
const index = cards.length + 1
|
|
50
|
+
const title = stripBoundary(item.title) || key
|
|
51
|
+
const snippet = stripBoundary(item.snippet || item.content || '') || ''
|
|
52
|
+
const verifiedText = stripBoundary(item.verified_excerpt || item.fetched_excerpt || '') || ''
|
|
53
|
+
const source = item.engine || item.source || item.provider || 'web'
|
|
54
|
+
cards.push({
|
|
55
|
+
id: `c${index}`,
|
|
56
|
+
index,
|
|
57
|
+
title,
|
|
58
|
+
url: key,
|
|
59
|
+
domain: extractDomain(key),
|
|
60
|
+
source,
|
|
61
|
+
snippet,
|
|
62
|
+
verifiedText,
|
|
63
|
+
verified: Boolean(item.verified || verifiedText),
|
|
64
|
+
credibility: typeof item.credibility_fast === 'number'
|
|
65
|
+
? item.credibility_fast
|
|
66
|
+
: (typeof item.credibility === 'number' ? item.credibility : null),
|
|
67
|
+
publishedAt: item.published_at || item.publishedAt || item.date || '',
|
|
68
|
+
})
|
|
69
|
+
if (cards.length >= 16) break
|
|
70
|
+
}
|
|
71
|
+
return cards
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function buildSystemPrompt(mode) {
|
|
75
|
+
const spec = MODE_SPECS[mode] || MODE_SPECS.general
|
|
76
|
+
return `${CITATION_RULE}\n${spec.prompt}`
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function normalizeLocalCards(payload) {
|
|
80
|
+
const rawList = Array.isArray(payload?.matches)
|
|
81
|
+
? payload.matches
|
|
82
|
+
: (Array.isArray(payload?.results) ? payload.results : [])
|
|
83
|
+
const seen = new Set()
|
|
84
|
+
const cards = []
|
|
85
|
+
for (const item of rawList) {
|
|
86
|
+
const filePath = String(item?.path || item?.file || item?.url || '').trim()
|
|
87
|
+
if (!filePath) continue
|
|
88
|
+
const line = item?.line ? `:${item.line}` : ''
|
|
89
|
+
const dedupKey = `${filePath}${line}`
|
|
90
|
+
if (seen.has(dedupKey)) continue
|
|
91
|
+
seen.add(dedupKey)
|
|
92
|
+
const index = cards.length + 1
|
|
93
|
+
const snippet = stripBoundary(item?.snippet || item?.text || item?.preview || (Array.isArray(item?.lines) ? item.lines.join('\n') : '')) || ''
|
|
94
|
+
cards.push({
|
|
95
|
+
id: `c${index}`,
|
|
96
|
+
index,
|
|
97
|
+
title: `${filePath.split('/').slice(-2).join('/')}${line}`,
|
|
98
|
+
url: filePath.startsWith('file://') ? filePath : `file://${filePath}`,
|
|
99
|
+
domain: 'local-seek',
|
|
100
|
+
source: 'local-seek',
|
|
101
|
+
snippet,
|
|
102
|
+
verifiedText: '',
|
|
103
|
+
verified: true,
|
|
104
|
+
credibility: 1,
|
|
105
|
+
publishedAt: item?.mtime || '',
|
|
106
|
+
})
|
|
107
|
+
if (cards.length >= 16) break
|
|
108
|
+
}
|
|
109
|
+
return cards
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function buildEvidenceContext(cards, maxChars = 14000) {
|
|
113
|
+
const lines = []
|
|
114
|
+
let used = 0
|
|
115
|
+
for (const c of cards) {
|
|
116
|
+
if (!c || typeof c !== 'object') continue
|
|
117
|
+
const body = c.verifiedText || c.snippet || '(无摘要)'
|
|
118
|
+
const block = [
|
|
119
|
+
`[${c.index}] 标题:${c.title}`,
|
|
120
|
+
`来源:${c.source} (${c.domain})${c.publishedAt ? ` · 时间:${c.publishedAt}` : ''}${c.verified ? ' · [已核验正文]' : ''}`,
|
|
121
|
+
`链接:${c.url}`,
|
|
122
|
+
`内容:${body.slice(0, 900)}`,
|
|
123
|
+
].join('\n')
|
|
124
|
+
if (used + block.length > maxChars) break
|
|
125
|
+
lines.push(block)
|
|
126
|
+
used += block.length
|
|
127
|
+
}
|
|
128
|
+
return lines.join('\n\n')
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function buildFallbackSummary(query, mode, cards, failure = null) {
|
|
132
|
+
const label = MODE_LABELS[mode] || '全网综合'
|
|
133
|
+
if (cards.length === 0) {
|
|
134
|
+
return `### 未检索到相关结果\n\n针对「${query}」在 **${label}** 模式下未命中有效网页结果,建议精简关键词或点击右上角「强制刷新」重试。`
|
|
135
|
+
}
|
|
136
|
+
const rows = cards.slice(0, 8).map((c) => {
|
|
137
|
+
const text = (c.verifiedText || c.snippet || '').slice(0, 180).replace(/\n+/g, ' ')
|
|
138
|
+
return `- **${c.title}** [${c.index}](${c.domain || c.source})\n ${text || '点击右侧卡片查看原文'}`
|
|
139
|
+
})
|
|
140
|
+
const hasRealFailure = failure && failure.reason && failure.reason !== 'no_model'
|
|
141
|
+
const header = hasRealFailure
|
|
142
|
+
? `### ${label} · 聚合检索结果(模型调用未完成,已切回直出视图)\n\n默认模型(\`${failure.model || 'unknown'}\`)暂不可用:${failure.message || failure.reason}。已为你整理最相关的 ${cards.length} 条多源证据,可直接在右侧核对原文:`
|
|
143
|
+
: `### ${label} · 聚合检索结果(零模型直出模式)\n\n当前宿主未配置可用默认模型,已为你整理最相关的 ${cards.length} 条多源证据,可直接在右侧核对原文:`
|
|
144
|
+
return `${header}\n\n${rows.join('\n\n')}`
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function mergeDedupCards(lists, maxCards = 16) {
|
|
148
|
+
const seen = new Set()
|
|
149
|
+
const merged = []
|
|
150
|
+
for (const list of lists) {
|
|
151
|
+
for (const card of list || []) {
|
|
152
|
+
if (!card?.url || seen.has(card.url)) continue
|
|
153
|
+
seen.add(card.url)
|
|
154
|
+
merged.push({ ...card, id: `c${merged.length + 1}`, index: merged.length + 1 })
|
|
155
|
+
if (merged.length >= maxCards) return merged
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return merged
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// 本机检索根目录唯一出口:callLocalSeek 与本机模式建议词必须同源——
|
|
162
|
+
// 旧版建议词读宿主 cwd、实际搜索另一路径,点了搜不到(2026-11-21 审计)
|
|
163
|
+
export function resolveLocalSearchPath(config) {
|
|
164
|
+
return config?.localSearchPath || process.env.ARGO_LOCAL_SEARCH_PATH || join(homedir(), 'Documents')
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// track 成败真章:payload 缺失或带 errors 即失败。旧版恒注入
|
|
168
|
+
// completedTracks/failedTracks: 0,no_completed_tracks、all_tracks_failed、
|
|
169
|
+
// partial_track_failure 三类门禁永久失效(2026-11-21 审计:与热榜假成功
|
|
170
|
+
// 同根因——写章条件与读章消费不对齐,凡盖章处必须有契约对拍)。
|
|
171
|
+
export function summarizeTrackRuns(trackRuns) {
|
|
172
|
+
const total = trackRuns.length
|
|
173
|
+
const failed = trackRuns.filter(r => r.payload === null || r.payload === undefined
|
|
174
|
+
|| (Array.isArray(r.payload.errors) && r.payload.errors.length > 0)).length
|
|
175
|
+
return { total, completed: total - failed, failed }
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export async function callLocalSeek(config, env, query, maxResults = 16, signal) {
|
|
179
|
+
const searchPath = resolveLocalSearchPath(config)
|
|
180
|
+
try {
|
|
181
|
+
const payload = await runArgoCli(
|
|
182
|
+
['seek', '--path', searchPath, '--json', '--max', String(Math.min(20, Math.max(1, maxResults))), '--', query],
|
|
183
|
+
{ env, timeoutMs: 15000, signal },
|
|
184
|
+
)
|
|
185
|
+
return { payload, cards: normalizeLocalCards(payload) }
|
|
186
|
+
} catch (err) {
|
|
187
|
+
return { payload: { errors: [String(err?.message ?? err)] }, cards: [] }
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export async function callArgoSearch(config, env, query, mode, refresh, signal, tracks = null) {
|
|
192
|
+
if (mode === 'local') {
|
|
193
|
+
return callLocalSeek(config, env, query, MODE_SPECS.local.searchArgs.max_results, signal)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const timeoutMs = mode === 'research'
|
|
197
|
+
? Math.max(config.searchTimeoutMs ?? 60000, 90000)
|
|
198
|
+
: (config.searchTimeoutMs ?? DEFAULTS.searchTimeoutMs)
|
|
199
|
+
|
|
200
|
+
if (mode === 'research' && Array.isArray(tracks) && tracks.length > 1) {
|
|
201
|
+
const trackRuns = await boundedMap(tracks, 3, async (track) => {
|
|
202
|
+
const cliArgs = buildSearchCliArgs(track.question || query, 'research', refresh, { max_results: 10, verify: 3 })
|
|
203
|
+
try {
|
|
204
|
+
const payload = await runArgoCli(cliArgs, { env, timeoutMs, signal })
|
|
205
|
+
return { payload, cards: normalizeCandidates(payload) }
|
|
206
|
+
} catch (err) {
|
|
207
|
+
// 失败要留痕:旧 .catch(() => ({})) 把失败伪装成空 payload,
|
|
208
|
+
// track 成败从此不可判(2026-11-21 审计:假章同根因)
|
|
209
|
+
return { payload: null, error: String(err?.message ?? err), cards: [] }
|
|
210
|
+
}
|
|
211
|
+
})
|
|
212
|
+
return {
|
|
213
|
+
payload: trackRuns[0]?.payload || {},
|
|
214
|
+
cards: mergeDedupCards(trackRuns.map(r => r.cards), 16),
|
|
215
|
+
trackStats: summarizeTrackRuns(trackRuns),
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const primaryTask = runArgoCli(buildSearchCliArgs(query, mode, refresh), { env, timeoutMs, signal })
|
|
220
|
+
.then(payload => ({ payload, cards: normalizeCandidates(payload) }))
|
|
221
|
+
|
|
222
|
+
// 热点分析与插件评测并行补充社区/社交域召回,避免单靠网页引擎错过一手讨论
|
|
223
|
+
const socialTask = MODE_SPECS[mode]?.socialSupplement
|
|
224
|
+
? runArgoCli(buildSearchCliArgs(query, 'general', refresh, { domain: 'social', max_results: 6, verify: 0 }), { env, timeoutMs: Math.min(timeoutMs, 15000), signal })
|
|
225
|
+
.then(payload => normalizeCandidates(payload))
|
|
226
|
+
.catch(() => [])
|
|
227
|
+
: Promise.resolve([])
|
|
228
|
+
|
|
229
|
+
const [primary, socialCards] = await Promise.all([primaryTask, socialTask])
|
|
230
|
+
let combined = mergeDedupCards([primary.cards, socialCards], 16)
|
|
231
|
+
|
|
232
|
+
if (mode === 'plugin' && combined.length < 6) {
|
|
233
|
+
const fallbackPayload = await runArgoCli(
|
|
234
|
+
buildSearchCliArgs(`${query} 评测 替代`, 'general', refresh, { verify: 2 }),
|
|
235
|
+
{ env, timeoutMs, signal },
|
|
236
|
+
).catch(() => ({}))
|
|
237
|
+
combined = mergeDedupCards([combined, normalizeCandidates(fallbackPayload)], 16)
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return { payload: primary.payload, cards: combined, trackStats: summarizeTrackRuns([{ payload: primary.payload }]) }
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export async function runClarifyIfShort(query, mode, env, signal) {
|
|
244
|
+
if (mode === 'local' || query.length > 18) return null
|
|
245
|
+
try {
|
|
246
|
+
const data = await runArgoCli(['clarify', '--json', '--', query], { env, timeoutMs: 3000, signal })
|
|
247
|
+
if (!data || (!data.ambiguities?.length && !data.routing?.explanation)) return null
|
|
248
|
+
return {
|
|
249
|
+
ambiguities: data.ambiguities || [],
|
|
250
|
+
explanation: data.routing?.explanation || '',
|
|
251
|
+
recommendedStrategy: data.recommended_strategy || '',
|
|
252
|
+
}
|
|
253
|
+
} catch {
|
|
254
|
+
return null
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export async function runEvidenceScoring(query, cards, env, signal) {
|
|
259
|
+
if (!cards.length) return { cards, evidenceSummary: null }
|
|
260
|
+
try {
|
|
261
|
+
const payload = await runArgoCli(
|
|
262
|
+
['evidence', '--', query],
|
|
263
|
+
{
|
|
264
|
+
env,
|
|
265
|
+
stdin: {
|
|
266
|
+
results: cards.map(c => ({
|
|
267
|
+
title: c.title,
|
|
268
|
+
url: c.url,
|
|
269
|
+
source: c.source,
|
|
270
|
+
snippet: c.verifiedText || c.snippet,
|
|
271
|
+
published_at: c.publishedAt,
|
|
272
|
+
})),
|
|
273
|
+
},
|
|
274
|
+
timeoutMs: 5000,
|
|
275
|
+
signal,
|
|
276
|
+
},
|
|
277
|
+
)
|
|
278
|
+
const scored = Array.isArray(payload?.results) ? payload.results : []
|
|
279
|
+
const enriched = cards.map((c, idx) => {
|
|
280
|
+
const ev = scored[idx]?.credibility
|
|
281
|
+
if (!ev) return c
|
|
282
|
+
return {
|
|
283
|
+
...c,
|
|
284
|
+
credibility: typeof ev.final === 'number' ? ev.final : c.credibility,
|
|
285
|
+
authorityTier: ev.authority?.tier || null,
|
|
286
|
+
authorityReason: ev.authority?.reason || '',
|
|
287
|
+
}
|
|
288
|
+
})
|
|
289
|
+
return {
|
|
290
|
+
cards: enriched,
|
|
291
|
+
evidenceSummary: {
|
|
292
|
+
crossValidation: payload?.cross_validation || null,
|
|
293
|
+
summary: payload?.summary || null,
|
|
294
|
+
},
|
|
295
|
+
}
|
|
296
|
+
} catch {
|
|
297
|
+
return { cards, evidenceSummary: null }
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export async function streamLlmAnswer(ctx, { system, messages, signal, onDelta }) {
|
|
302
|
+
const llm = ctx.get?.('llm')
|
|
303
|
+
const modelService = ctx.get?.('agentDefaultModel')
|
|
304
|
+
if (!llm || !modelService) return { ok: false, reason: 'no_model' }
|
|
305
|
+
|
|
306
|
+
let selection
|
|
307
|
+
try {
|
|
308
|
+
selection = modelService.currentSelection?.()
|
|
309
|
+
} catch {
|
|
310
|
+
selection = null
|
|
311
|
+
}
|
|
312
|
+
if (!selection?.provider || !selection?.model) {
|
|
313
|
+
return { ok: false, reason: 'no_model' }
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
let text = ''
|
|
317
|
+
let usage = null
|
|
318
|
+
let finish = null
|
|
319
|
+
try {
|
|
320
|
+
const stream = llm.stream({
|
|
321
|
+
provider: selection.provider,
|
|
322
|
+
model: selection.model,
|
|
323
|
+
...(selection.reasoningEffort ? { reasoningEffort: selection.reasoningEffort } : {}),
|
|
324
|
+
signal,
|
|
325
|
+
system,
|
|
326
|
+
messages,
|
|
327
|
+
})
|
|
328
|
+
for await (const chunk of stream) {
|
|
329
|
+
if (signal?.aborted) break
|
|
330
|
+
if (chunk?.type === 'text-delta' && typeof chunk.text === 'string') {
|
|
331
|
+
text += chunk.text
|
|
332
|
+
onDelta(chunk.text)
|
|
333
|
+
} else if (chunk?.type === 'usage') {
|
|
334
|
+
usage = chunk.usage
|
|
335
|
+
} else if (chunk?.type === 'finish') {
|
|
336
|
+
finish = chunk.reason
|
|
337
|
+
} else if (chunk?.type === 'error') {
|
|
338
|
+
const msg = chunk.error?.message || chunk.message || 'model stream error'
|
|
339
|
+
return { ok: false, reason: 'error', message: String(msg), text, model: `${selection.provider}/${selection.model}` }
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
if (finish && finish.kind !== 'stop') {
|
|
343
|
+
const msg = finish.failure?.message || finish.failure?.code || `finish=${finish.kind}`
|
|
344
|
+
console.warn(`[argo-workbench] llm finish=${finish.kind} model=${selection.provider}/${selection.model} msg=${msg}`)
|
|
345
|
+
return { ok: false, reason: finish.kind, message: String(msg), text, usage, finish, model: `${selection.provider}/${selection.model}` }
|
|
346
|
+
}
|
|
347
|
+
return { ok: true, text, usage, finish, model: `${selection.provider}/${selection.model}` }
|
|
348
|
+
} catch (err) {
|
|
349
|
+
return { ok: false, reason: err?.name === 'AbortError' ? 'aborted' : 'error', message: String(err?.message ?? err), text }
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export function extractCitations(text, cards) {
|
|
354
|
+
const used = new Set()
|
|
355
|
+
const re = /\[(\d+)\]/g
|
|
356
|
+
let m
|
|
357
|
+
while ((m = re.exec(text)) !== null) {
|
|
358
|
+
const n = Number(m[1])
|
|
359
|
+
if (n >= 1 && n <= cards.length) used.add(n)
|
|
360
|
+
}
|
|
361
|
+
return {
|
|
362
|
+
citedIndices: [...used].sort((a, b) => a - b),
|
|
363
|
+
totalCards: cards.length,
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// 供测试断言「flag 全部前置、query 经 `--` 分隔殿后」的参数形态
|
|
368
|
+
export function buildSearchCliArgs(query, mode, refresh = false, overrides = {}) {
|
|
369
|
+
const spec = MODE_SPECS[mode] || MODE_SPECS.general
|
|
370
|
+
const merged = { ...spec.searchArgs, ...overrides }
|
|
371
|
+
const args = ['search', '--json', '--envelope']
|
|
372
|
+
if (merged.max_results) args.push('-n', String(merged.max_results))
|
|
373
|
+
if (merged.depth) args.push('--depth', String(merged.depth))
|
|
374
|
+
if (merged.mode) args.push('--mode', String(merged.mode))
|
|
375
|
+
if (merged.domain) args.push('--domain', String(merged.domain))
|
|
376
|
+
if (merged.since) args.push('--since', String(merged.since))
|
|
377
|
+
if (merged.sort) args.push('--sort', String(merged.sort))
|
|
378
|
+
if (merged.verify) args.push('--verify', String(merged.verify))
|
|
379
|
+
if (refresh) args.push('--no-cache')
|
|
380
|
+
// `--` 后的全部 token 被 argparse 当位置参数:flag 必须全部前置,否则
|
|
381
|
+
// 以 `-` 开头的 query(如 `--list-engines`)会被 CLI 当 flag 吃掉
|
|
382
|
+
// (2026-11-18 实跑复现:`argo search "--list-engines"` 返回引擎清单)
|
|
383
|
+
args.push('--', query)
|
|
384
|
+
return args
|
|
385
|
+
}
|