@wenaixi/searchfuse 0.0.0-stage → 0.1.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.
Files changed (52) hide show
  1. package/CHANGELOG.md +131 -0
  2. package/LICENSE +21 -0
  3. package/README.md +169 -2
  4. package/README.zh-CN.md +170 -0
  5. package/SECURITY.md +20 -0
  6. package/claude/.claude-plugin/plugin.json +17 -0
  7. package/claude/SKILL.md +53 -0
  8. package/claude/hooks/hooks.json +3 -0
  9. package/claude/hooks/register.ts +552 -0
  10. package/cordis.patch.yml +16 -0
  11. package/dist/config-schema.js +40 -0
  12. package/dist/errorCause-BY0A0XKN.js +17 -0
  13. package/dist/kernel.js +62 -0
  14. package/dist/main.js +1371 -0
  15. package/dist/mcp-protocol.js +1 -0
  16. package/dist/mcp-registry.js +155 -0
  17. package/dist/protocol-ClnjMnGV.js +259 -0
  18. package/dist/proxy-DodOSGiI.js +50 -0
  19. package/dist/search-BIW13Csk.js +4061 -0
  20. package/dist/serve-Bfqz-nCB.js +279 -0
  21. package/dist/serve-ensure.js +140 -0
  22. package/docs/commit.md +77 -0
  23. package/docs/dsh.md +205 -0
  24. package/docs/harness-setup.md +78 -0
  25. package/docs/parity-matrix.md +61 -0
  26. package/docs/searxng-setup.md +117 -0
  27. package/docs/security.md +59 -0
  28. package/docs/settings-redesign-decisions.md +407 -0
  29. package/docs/superpowers/plans/2026-11-24-serve-lifecycle.md +880 -0
  30. package/docs/superpowers/reports/2026-11-24-serve-lifecycle-ledger.md +211 -0
  31. package/docs/superpowers/specs/2026-11-20-searchfuse-dual-host-design.md +172 -0
  32. package/docs/superpowers/specs/2026-11-21-searchfuse-tri-host-design.md +659 -0
  33. package/docs/superpowers/specs/2026-11-21-tri-host-takeover-design.md +363 -0
  34. package/docs/superpowers/specs/2026-11-22-settings-card-refactor-design.md +117 -0
  35. package/docs/superpowers/specs/2026-11-24-serve-lifecycle-design.md +155 -0
  36. package/docs/testing.md +92 -0
  37. package/docs/troubleshooting.md +258 -0
  38. package/dsh/client.js +2917 -0
  39. package/dsh/fetch-schema.json +1 -0
  40. package/dsh/index.js +2640 -0
  41. package/dsh/search-schema.json +65 -0
  42. package/dsh/spawnHidden.d.ts +7 -0
  43. package/dsh/spawnHidden.js +8 -0
  44. package/omp/extension.ts +331 -0
  45. package/package.json +95 -4
  46. package/skills/searchfuse/SKILL.md +139 -0
  47. package/skills/searchfuse/references/cli.md +78 -0
  48. package/skills/searchfuse/references/configure.md +405 -0
  49. package/skills/searchfuse/references/output-schema.md +182 -0
  50. package/skills/searchfuse/references/runtime.md +58 -0
  51. package/skills/searchfuse/scripts/run.ps1 +279 -0
  52. package/skills/searchfuse/scripts/run.sh +315 -0
@@ -0,0 +1,182 @@
1
+ # SearchFuse 输出契约
2
+
3
+ CLI 向 stdout 打印一个 JSON 对象。顶层信封对每次运行都一样:
4
+
5
+ ```json
6
+ {
7
+ "mode": "search",
8
+ "query": "current Node.js LTS",
9
+ "url": null,
10
+ "results": [
11
+ {
12
+ "source": "web",
13
+ "requestedSource": "web",
14
+ "engine": "antigravity-cli",
15
+ "model": "gemini-3.8-flash-low",
16
+ "status": "ok",
17
+ "durationSeconds": 5.5,
18
+ "summary": "The current Node.js LTS is v24.19.0 (Krypton), released 2026-08-03.",
19
+ "items": [
20
+ {
21
+ "title": "Node.js v24.19.0 release",
22
+ "url": "https://nodejs.org/en/blog/release/v24.19.0",
23
+ "snippet": "Krypton is the active LTS line.",
24
+ "source": "nodejs.org",
25
+ "published_at": "2026-08-03"
26
+ }
27
+ ],
28
+ "uncertainty": [],
29
+ "warnings": [],
30
+ "attempts": [
31
+ { "engine": "antigravity-cli", "ok": true, "durationSeconds": 5.5 }
32
+ ]
33
+ }
34
+ ],
35
+ "meta": {
36
+ "generatedAt": "2026-08-06T12:00:00.000Z",
37
+ "durationSeconds": 5.6
38
+ }
39
+ }
40
+ ```
41
+
42
+ 要点:
43
+
44
+ - `results` 永远是数组,每个语料一条,所以哪怕单语料运行,结构也不变。
45
+ - **下面的示例信封是链式的形状**(`engine` 是单个引擎名、`attempts` 有记录、条目不带 `foundIn`)。融合是默认模式,它把这三处换掉:`engine` 变成逗号连接的多引擎列表,`attempts` 恒为空数组(失败与跳过写进 `uncertainty`),条目额外带 `score` 与 `foundIn`。字段集合本身不变,只是取值形状不同。
46
+ - `meta` 只记录整次运行何时完成、总共多久。每个语料的耗时、引擎、模型在各自的 `results` 条目里,因为一次运行可能跨多个引擎。
47
+ - 路由事实(`source`、`engine`、`model`、`durationSeconds`)由 searchfuse 在引擎作答后盖章写入,引擎伪造不了是谁答的。
48
+
49
+ ## `results` 条目
50
+
51
+ 每条都以相同的路由字段开头,然后把引擎自己的结果字段平铺在旁边:
52
+
53
+ | 字段 | 含义 |
54
+ | :-- | :-- |
55
+ | `source` | `web` 或 `x`,这条证据实际来自的语料 |
56
+ | `requestedSource` | `web` 或 `x`,被请求的语料。运行降级时与 `source` 不同 |
57
+ | `engine` | 实际作答的引擎。融合模式下是逗号连接的多引擎列表(`firecrawl,keenable,anysearch,...`),因为一份融合答案来自多家;用 `-e` 钉住单个引擎时才是单个名字。语料不可达时为 `null` |
58
+ | `model` | 使用的模型,引擎有模型时才有值(没有时是空字符串) |
59
+ | `status` | `ok`、`degraded` 或 `unavailable`(见下文) |
60
+ | `warnings` | 这个语料的路由与运行警告:一次回退、一条降级说明、一个配置笔误、本地引擎的「无综述」和「已放行私有网络」提示。说的是答案怎么产生的,不是答案里的事实。永远是数组,常为空 |
61
+ | `attempts` | 这个语料按顺序发出的每次请求:`{ engine, keyIndex?, ok, error?, durationSeconds, cost?, credits? }`。同一引擎配置多个 key 时会带零基的 `keyIndex`。单 key 和免 key 请求维持原有结构,不出现该字段。`ok: false` 的条目带失败 `error`。会上报消耗的引擎会附 `cost`(exa,美元)或 `credits`(firecrawl)。两个字段都可选,不上报的引擎上没有。因运行的 `--deadline` 已用完而被跳过的引擎记为 `ok: false`,`error` 写明原因,`durationSeconds` 为 `null`。成功的运行末尾恰有一条 `ok: true` |
62
+ | `durationSeconds` | 这一个语料花了多久,什么都没跑时为 `null` |
63
+
64
+ 其余字段取决于模式。
65
+
66
+ ### `uncertainty` 与 `warnings` 的区别
67
+
68
+ 两个独立的列表,转述结果时这个区分很重要:
69
+
70
+ - `uncertainty` 是引擎对**事实本身**的存疑:填不上的空缺、互相矛盾的来源、可能过期的数字、薄得不可信的页面。转述时把它们作为答案的保留意见。
71
+ - `warnings` 说的是**答案怎么产生的**:一个引擎挂了由另一个顶上、X 请求由网页作答、配置 key 是个笔误、一次抓取跟了重定向或关了私有网络防护。当它影响对路由的信任(尤其是降级)时要转述,但它不是对事实的怀疑。
72
+
73
+ 老版本把两者都塞在 `uncertainty` 里。曾从 `uncertainty` 里解析路由信息的消费者现在应改读 `warnings`。
74
+
75
+ ### `status` 与降级的 X 答案
76
+
77
+ `status` 告诉消费者这条结果把请求的语料服务得怎么样:
78
+
79
+ - `ok`:请求的语料作了答。`source` 等于 `requestedSource`。
80
+ - `degraded`:替补语料作了答。目前只有 X 会降级:Grok Build 缺失、未登录或故障时,由网页引擎回答 X 请求。此时条目是 `requestedSource: "x"`、`source: "web"`、`status: "degraded"`,`warnings` 解释网页数据看不到 X 内部。不要把降级条目当成 X 的覆盖来呈现。
81
+ - `unavailable`:没有任何东西能服务这个语料。`engine` 为 `null`,`items` 为空,`attempts` 列出试过或被跳过的每个引擎(路由一个可试的引擎都没找到时为空),`durationSeconds` 为 `null`,`warnings` 说明原因。`--source web,x` 运行中 X 不可达时,X 槽位就是这样显式存在,而不是无声消失:
82
+
83
+ ```json
84
+ {
85
+ "source": "x",
86
+ "requestedSource": "x",
87
+ "engine": null,
88
+ "status": "unavailable",
89
+ "summary": "",
90
+ "items": [],
91
+ "uncertainty": [],
92
+ "warnings": [
93
+ "X itself was not reachable here (Grok Build missing, signed out, or failing), so this came from the public web, which cannot see inside X."
94
+ ],
95
+ "attempts": [],
96
+ "durationSeconds": null
97
+ }
98
+ ```
99
+
100
+ ### attempt 上的引擎消耗
101
+
102
+ 计量自己用量的引擎会在实际运行的那条 attempt 上报告,调用方可以据此统计一次运行的成本。两个字段都可选:只出现在上报它们的引擎上,都不上报的引擎(agy、tavily、keenable、youcom、本地引擎)上永远没有。
103
+
104
+ - `cost`:美元,来自 exa。
105
+ - `credits`:firecrawl 的 credits。
106
+
107
+ ```json
108
+ {
109
+ "engine": "exa",
110
+ "ok": true,
111
+ "durationSeconds": 1.2,
112
+ "cost": 0.007
113
+ }
114
+ ```
115
+
116
+ ## 搜索模式(`-q`)
117
+
118
+ 平铺进条目的引擎结果:
119
+
120
+ ```json
121
+ {
122
+ "summary": "string",
123
+ "items": [
124
+ {
125
+ "title": "string",
126
+ "url": "string",
127
+ "snippet": "string",
128
+ "source": "string(可选)",
129
+ "published_at": "string(可选)"
130
+ }
131
+ ],
132
+ "uncertainty": ["string"]
133
+ }
134
+ ```
135
+
136
+ - `items` 的顺序就是相关性排序。没有数值型 `relevance` 分数:模型会编造它,顺序本身已经携带了相关性。
137
+ - 融合模式下每条额外带 `score`(倒数排名融合得分)和 `foundIn`(返回过这个页面的引擎名列表)。`foundIn` 只在融合结果里出现;链式运行只由一个引擎作答,所以那里没有这个字段,而不是给一个长度为 1 的列表。
138
+ - `items` 为空且 `uncertainty` 有内容,表示这次搜索没找到可靠的东西。
139
+
140
+ ## 抓取模式(`-u`)
141
+
142
+ `items` 换成 `content` 加 `links`:
143
+
144
+ ```json
145
+ {
146
+ "mode": "fetch",
147
+ "query": null,
148
+ "url": "https://nodejs.org/en/about",
149
+ "results": [
150
+ {
151
+ "source": "web",
152
+ "requestedSource": "web",
153
+ "engine": "antigravity-cli",
154
+ "model": "gemini-3.8-flash-low",
155
+ "status": "ok",
156
+ "durationSeconds": 8.1,
157
+ "summary": "About page for the Node.js project.",
158
+ "content": "# About Node.js\nNode.js is a JavaScript runtime...",
159
+ "links": [
160
+ { "text": "Downloads", "url": "https://nodejs.org/en/download" }
161
+ ],
162
+ "uncertainty": [],
163
+ "warnings": [],
164
+ "attempts": [
165
+ { "engine": "antigravity-cli", "ok": true, "durationSeconds": 8.1 }
166
+ ]
167
+ }
168
+ ],
169
+ "meta": {
170
+ "generatedAt": "2026-08-06T12:00:00.000Z",
171
+ "durationSeconds": 8.2
172
+ }
173
+ }
174
+ ```
175
+
176
+ - `content` 是页面主体内容,agy 给 markdown,本地引擎给原样文本(它不跑 JavaScript,也不做综述)。
177
+ - `links` 是有用的外链,至多 20 条,可以为空。
178
+
179
+ ## 备注
180
+
181
+ - `query` 和 `url` 与请求对应:恰有一个非 null。
182
+ - 上面的示例信封由 `src/output-schema.test.ts` 与真实的 `RunSearchResult` 对照校验,这份文档与代码脱节时测试会失败。
@@ -0,0 +1,58 @@
1
+ # searchfuse 运行时参考
2
+
3
+ skill 如何启动 `searchfuse` CLI、钉在哪个版本、在什么都跑不了的机器上如何给出诊断。启动器 `scripts/run.sh`(macOS / Linux)和 `scripts/run.ps1`(Windows)实现下面的全部内容,除版本常量和各自的 shell 语法外必须逐字一致。
4
+
5
+ ## 钉住的版本
6
+
7
+ - Pinned CLI version: 0.1.0
8
+ - npm 包:`@wenaixi/searchfuse`
9
+ - CLI 命令名:`searchfuse`
10
+
11
+ 上面那行版本号和两个启动器内部的常量由 `scripts/release.mjs` 在发布时从 `package.json` 盖章写入,不要手改。三份副本与 `package.json` 一旦不一致,`scripts/stamp.test.mjs` 会让构建失败。那一行刻意保留英文写法:它是版本戳记脚本的锚点,不是给人读的散文。
12
+
13
+ ## 解析顺序
14
+
15
+ 每次调用按这个顺序找一条能跑 CLI 的路:
16
+
17
+ 1. **`PATH` 上已有兼容的 `searchfuse`**,按名字直接跑。
18
+ 2. **有 `npx`,且 `node` 满足 CLI 的 22.19 下限**,跑 `npx --yes --package @wenaixi/searchfuse@<钉住版本> searchfuse <args>`。骑在老 node 上的 npx 会被跳过:那条路已知会在运行时失败。
19
+ 3. **有 `bunx`**,跑 `bunx --bun @wenaixi/searchfuse@<钉住版本> <args>`。
20
+ 4. **原生产物**,留给 B 阶段。目前没有发布任何产物,这个分支报告 `nativeArtifact.available: false` 后继续往下走。
21
+ 5. **什么都没有**,打印结构化诊断并以 `78`(`EX_CONFIG`)退出。
22
+
23
+ 启动器原样转发 stdin、stdout、stderr 和退出码,所以无论怎么启动,CLI 的 JSON 输出契约完全一致。
24
+
25
+ ## 兼容规则
26
+
27
+ `PATH` 上找到的 `searchfuse` 只有在**与钉住版本同一主版本号,且不老于它**时才算兼容。同主版本让已装了匹配 CLI 的用户不被强制走一遍 `npx` 重新下载(设计里的「不回退」要求)。不老于则拒绝比这份 skill 编写时还旧的全局构建,那种情况下启动器跳过 `PATH`,改用钉住版本的 `npx` / `bunx`。
28
+
29
+ ## 缓存与权限(B 阶段,尚未启用)
30
+
31
+ A 阶段不发原生产物。`npx` 和 `bunx` 路径首次使用时拉取钉住的 npm 包并缓存(这就是这两个运行器的工作方式),除此之外不下载任何东西。B 阶段原生产物落地后,启动器会按用户、按版本缓存它们,并用绝对路径启动:
32
+
33
+ - macOS:`~/Library/Caches/Wenaixi/searchfuse/<version>/`
34
+ - Linux:`${XDG_CACHE_HOME:-$HOME/.cache}/Wenaixi/searchfuse/<version>/`
35
+ - Windows:`%LOCALAPPDATA%\liustack\searchfuse\<version>\`
36
+
37
+ 约束是:不用 `sudo` 或管理员权限,不碰系统目录,不改 `PATH`,先下载到临时文件、校验 SHA-256 后原子移动,失败时不留任何未校验的可执行文件。下载一律用 `curl`(Windows 上写全 `curl.exe`),它不会打隔离标记(Mark-of-the-Web),启动器也从不摘除浏览器本会设置的安全标记。
38
+
39
+ ## 诊断字段
40
+
41
+ `run.sh doctor --json`(以及 `run.ps1 doctor --json`)输出这个结构:
42
+
43
+ - `tool`、`package`、`pinnedVersion`:这份 skill 的目标。
44
+ - `os`、`arch`:归一化的主机标识(`darwin` / `linux` / `windows`,`arm64` / `x64`)。
45
+ - `checked.pathCli`:`PATH` 上 `searchfuse` 的 `{ present, path, version, compatible }`,`compatible` 按上面的规则判定。
46
+ - `checked.npx`:`{ present, path, nodeMeetsFloor }`,`nodeMeetsFloor` 是本机 node 是否满足 CLI 的 22.19 下限,npx 路径的前提。
47
+ - `checked.bunx`:`{ present, path }`。
48
+ - `checked.node`:`{ present, version }`。
49
+ - `nativeArtifact`:`{ available, note }`,A 阶段 `available` 为 `false`。
50
+ - `selected`:选定的路:`path`、`npx`、`bunx` 或 `none`。
51
+ - `nextSteps`:`selected` 为 `none` 时,给用户的一到两条直白建议(装 Node 22.19+ 或 Bun)。其余情况为空。
52
+ - `cliDoctor`:CLI 可用时,把 CLI 自己的 `doctor --json` 报告(引擎与配置诊断)嵌在这里,否则为 `null`。
53
+
54
+ `doctor` 不花额度。启动器自己的诊断是离线的:只检查本地环境,不发任何网络请求。经 npx 或 bunx 转接 CLI 的 `doctor` 时,首次可能会下载钉住的包(这两个运行器就是这样工作的),之后走本地缓存。
55
+
56
+ ## 交付形态:长期保持本地 CLI
57
+
58
+ searchfuse 有意保持本地 CLI 形态,分发设计第 10 节(不需要本地执行的能力迁去远程 MCP)对它**不适用**。原因就是产品本身:搜索引擎的 key 握在用户机器上,消耗的额度是用户自己的,中间没有中心化服务。托管 MCP 会把这三样都从用户机器上挪走,与这个工具的初衷相反。D 阶段可能为将来某个工具退役原生产物,但只要这三条性质还成立,searchfuse 就保持本地 CLI。
@@ -0,0 +1,279 @@
1
+ # searchfuse skill launcher (Windows, PowerShell 5.1 compatible).
2
+ #
3
+ # The Windows twin of run.sh: identical resolution order, identical diagnostic
4
+ # fields, identical exit codes. One stable action for the agent ("run
5
+ # searchfuse"); this script picks a working way to run it here.
6
+ #
7
+ # Invoke it per-process so no global policy is touched:
8
+ # powershell -ExecutionPolicy Bypass -File run.ps1 -q "test"
9
+ #
10
+ # Resolution order (kept identical in run.sh):
11
+ # 1. A compatible searchfuse already on PATH -> run it directly.
12
+ # 2. npx present -> run the pinned npm version.
13
+ # 3. bunx present -> run the pinned version via Bun.
14
+ # 4. (phase B placeholder) a native artifact -> not published yet.
15
+ # 5. Nothing usable -> structured diagnosis, exit 78.
16
+ #
17
+ # It never writes PATH, never needs admin rights, never fetches a second script,
18
+ # and has no postinstall step.
19
+
20
+ $ErrorActionPreference = 'Stop'
21
+
22
+ # --- Version constants: stamped by scripts/release.mjs at release time. --------
23
+ # Do not edit $Pinned by hand; scripts/stamp.test.mjs asserts it equals the
24
+ # package.json version, and the release script rewrites it on every bump.
25
+ $Package = '@wenaixi/searchfuse'
26
+ $Bin = 'searchfuse'
27
+ $Pinned = '0.1.0'
28
+ # -------------------------------------------------------------------------------
29
+
30
+ $NativeNote = 'no native artifact is published for this tool yet; phase A ships npm launch paths only'
31
+
32
+ # Environment snapshot, filled by Collect and read by the emitters.
33
+ $script:Arch = ''
34
+ $script:CliPresent = $false
35
+ $script:CliPath = $null
36
+ $script:CliVer = $null
37
+ $script:CliCompat = $false
38
+ $script:NpxPresent = $false
39
+ $script:NpxPath = $null
40
+ $script:BunxPresent = $false
41
+ $script:BunxPath = $null
42
+ $script:NodePresent = $false
43
+ $script:NodeVer = $null
44
+ $script:NodeFloorOk = $false
45
+ $script:Selected = 'none'
46
+
47
+ # First "X.Y.Z" token printed by `$Bin --version`.
48
+ function Get-CliVersion {
49
+ try { $out = & $Bin --version 2>$null } catch { return '' }
50
+ if (-not $out) { return '' }
51
+ $line = [string]($out | Select-Object -First 1)
52
+ $m = [regex]::Match($line, '[0-9]+\.[0-9]+\.[0-9]+')
53
+ if ($m.Success) { return $m.Value } else { return '' }
54
+ }
55
+
56
+ # Compatible = same major version as $Pinned AND not older than $Pinned.
57
+ # Same major keeps a globally installed CLI usable without a forced re-download;
58
+ # not-older refuses a stale build that predates the version this skill needs.
59
+ function Test-Compatible {
60
+ param([string] $Ver)
61
+ $f = $Ver -split '\.'
62
+ $p = $Pinned -split '\.'
63
+ if ($f.Count -lt 3 -or $p.Count -lt 3) { return $false }
64
+ $fMaj = [int]$f[0]; $fMin = [int]$f[1]; $fPat = [int]$f[2]
65
+ $pMaj = [int]$p[0]; $pMin = [int]$p[1]; $pPat = [int]$p[2]
66
+ if ($fMaj -ne $pMaj) { return $false }
67
+ if ($fMin -gt $pMin) { return $true }
68
+ if ($fMin -lt $pMin) { return $false }
69
+ return ($fPat -ge $pPat)
70
+ }
71
+
72
+ # The npx path runs the CLI on this machine's node, so npx is only usable when
73
+ # node itself meets the CLI's floor. An old node with a working npx used to be
74
+ # selected anyway, a path known to fail at run time.
75
+ $NodeFloor = '22.19.0'
76
+ function Test-NodeMeetsFloor {
77
+ if (-not (Get-Command node -ErrorAction SilentlyContinue)) { return $false }
78
+ try { $nv = ((& node --version 2>$null) -replace '^v', '') } catch { return $false }
79
+ if (-not $nv) { return $false }
80
+ $n = $nv -split '\.'
81
+ $f = $NodeFloor -split '\.'
82
+ if ($n.Count -lt 2) { return $false }
83
+ $nMaj = [int]$n[0]; $nMin = [int]$n[1]
84
+ $fMaj = [int]$f[0]; $fMin = [int]$f[1]
85
+ if ($nMaj -gt $fMaj) { return $true }
86
+ if ($nMaj -lt $fMaj) { return $false }
87
+ return ($nMin -ge $fMin)
88
+ }
89
+
90
+ # Return exactly one word: the chosen launch path.
91
+ function Resolve-LaunchKind {
92
+ $cli = Get-Command $Bin -ErrorAction SilentlyContinue
93
+ if ($cli) {
94
+ $v = Get-CliVersion
95
+ if ($v -and (Test-Compatible $v)) { return 'path' }
96
+ }
97
+ if ((Get-Command npx -ErrorAction SilentlyContinue) -and (Test-NodeMeetsFloor)) { return 'npx' }
98
+ if (Get-Command bunx -ErrorAction SilentlyContinue) { return 'bunx' }
99
+ # Phase B goes here: check a versioned user cache, then download and verify a
100
+ # native artifact into it. Any such download must use curl.exe (written in
101
+ # full so PowerShell 5.1 does not resolve `curl` to its Invoke-WebRequest
102
+ # alias), never Invoke-WebRequest, which stamps Mark-of-the-Web; see
103
+ # design.md 8.3.
104
+ return 'none'
105
+ }
106
+
107
+ # Run the resolved CLI and return its output (used to chain the CLI's own
108
+ # doctor). Passes every argument through untouched.
109
+ function Invoke-Cli {
110
+ param([string[]] $CliArgs)
111
+ switch ($script:Selected) {
112
+ 'path' { & $Bin @CliArgs }
113
+ 'npx' { & npx --yes --package "$Package@$Pinned" $Bin @CliArgs }
114
+ 'bunx' { & bunx --bun "$Package@$Pinned" @CliArgs }
115
+ }
116
+ }
117
+
118
+ function Get-Arch {
119
+ switch ($env:PROCESSOR_ARCHITECTURE) {
120
+ 'AMD64' { return 'x64' }
121
+ 'ARM64' { return 'arm64' }
122
+ 'x86' { return 'x86' }
123
+ default { return $env:PROCESSOR_ARCHITECTURE }
124
+ }
125
+ }
126
+
127
+ # Probe the environment once into the $script:* snapshot.
128
+ function Collect {
129
+ $script:Arch = Get-Arch
130
+
131
+ $cli = Get-Command $Bin -ErrorAction SilentlyContinue
132
+ if ($cli) {
133
+ $script:CliPresent = $true
134
+ $script:CliPath = $cli.Source
135
+ $script:CliVer = Get-CliVersion
136
+ $script:CliCompat = [bool]($script:CliVer -and (Test-Compatible $script:CliVer))
137
+ }
138
+
139
+ $npx = Get-Command npx -ErrorAction SilentlyContinue
140
+ if ($npx) { $script:NpxPresent = $true; $script:NpxPath = $npx.Source }
141
+
142
+ $bunx = Get-Command bunx -ErrorAction SilentlyContinue
143
+ if ($bunx) { $script:BunxPresent = $true; $script:BunxPath = $bunx.Source }
144
+
145
+ $node = Get-Command node -ErrorAction SilentlyContinue
146
+ if ($node) {
147
+ $script:NodePresent = $true
148
+ try { $script:NodeVer = ((& node --version 2>$null) -replace '^v', '') } catch { $script:NodeVer = $null }
149
+ $script:NodeFloorOk = Test-NodeMeetsFloor
150
+ }
151
+
152
+ $script:Selected = Resolve-LaunchKind
153
+ }
154
+
155
+ # Assemble the structured diagnosis. $Chained, when a parsed object, becomes
156
+ # cliDoctor; otherwise cliDoctor is null.
157
+ function Build-DiagnosisJson {
158
+ param($Chained)
159
+ $checked = [ordered]@{
160
+ pathCli = [ordered]@{ present = $script:CliPresent; path = $script:CliPath; version = $script:CliVer; compatible = $script:CliCompat }
161
+ npx = [ordered]@{ present = $script:NpxPresent; path = $script:NpxPath; nodeMeetsFloor = $script:NodeFloorOk }
162
+ bunx = [ordered]@{ present = $script:BunxPresent; path = $script:BunxPath }
163
+ node = [ordered]@{ present = $script:NodePresent; version = $script:NodeVer }
164
+ }
165
+ $steps = @()
166
+ if ($script:Selected -eq 'none') {
167
+ $major = $Pinned.Split('.')[0]
168
+ $first = "Install Node 22.19+ from https://nodejs.org so npx can run $Package@$Pinned, then re-run this launcher."
169
+ if ($script:NpxPresent -and (-not $script:NodeFloorOk)) {
170
+ $first = "npx is present but node $(if ($script:NodeVer) { $script:NodeVer } else { 'missing' }) is below the $NodeFloor floor this CLI needs. Upgrade Node at https://nodejs.org, then re-run this launcher."
171
+ }
172
+ $steps = @(
173
+ $first,
174
+ "No JavaScript runtime? Install Bun from https://bun.sh to use bunx, or put a compatible $Bin (major $major, at or above $Pinned) on PATH."
175
+ )
176
+ }
177
+ $obj = [ordered]@{
178
+ tool = $Bin
179
+ package = $Package
180
+ pinnedVersion = $Pinned
181
+ os = 'windows'
182
+ arch = $script:Arch
183
+ checked = $checked
184
+ nativeArtifact = [ordered]@{ available = $false; note = $NativeNote }
185
+ selected = $script:Selected
186
+ nextSteps = @($steps)
187
+ cliDoctor = $Chained
188
+ }
189
+ return ($obj | ConvertTo-Json -Depth 20)
190
+ }
191
+
192
+ # Human-readable diagnosis for `doctor` without --json.
193
+ function Write-DiagnosisText {
194
+ Write-Output "$Bin launcher diagnosis"
195
+ Write-Output ''
196
+ Write-Output (" os / arch: windows / {0}" -f $script:Arch)
197
+ Write-Output (" pinned version: {0} ({1})" -f $Pinned, $Package)
198
+ if ($script:CliPresent) {
199
+ $verdict = if ($script:CliCompat) { 'compatible' } else { 'incompatible' }
200
+ Write-Output (" {0} on PATH: {1} (version {2}, {3})" -f $Bin, $script:CliPath, $script:CliVer, $verdict)
201
+ }
202
+ else {
203
+ Write-Output (" {0} on PATH: no" -f $Bin)
204
+ }
205
+ $npxDesc = 'no'
206
+ if ($script:NpxPresent) {
207
+ if ($script:NodeFloorOk) { $npxDesc = $script:NpxPath }
208
+ else { $npxDesc = "$($script:NpxPath) (unusable: node $(if ($script:NodeVer) { $script:NodeVer } else { 'missing' }) is below $NodeFloor)" }
209
+ }
210
+ Write-Output (" npx: {0}" -f $npxDesc)
211
+ Write-Output (" bunx: {0}" -f $(if ($script:BunxPresent) { $script:BunxPath } else { 'no' }))
212
+ Write-Output (" node: {0}" -f $(if ($script:NodePresent) { $script:NodeVer } else { 'no' }))
213
+ Write-Output (" selected path: {0}" -f $script:Selected)
214
+ if ($script:Selected -eq 'none') {
215
+ Write-Output ''
216
+ Write-Output ("No runtime can launch {0} here. {1}" -f $Bin, $NativeNote)
217
+ Write-Output 'Next steps:'
218
+ Write-Output ' - Install Node 22.19+ from https://nodejs.org, then re-run this launcher.'
219
+ Write-Output (" - Or install Bun from https://bun.sh, or put a compatible {0} on PATH." -f $Bin)
220
+ }
221
+ }
222
+
223
+ # `doctor [--json] [extra...]`: launcher selection diagnosis. When a CLI is
224
+ # resolvable, chain the CLI's own doctor so one call reports both layers. Extra
225
+ # flags pass through to the chained CLI doctor.
226
+ function Invoke-Doctor {
227
+ param([string[]] $DocArgs)
228
+ Collect
229
+ $json = $false
230
+ foreach ($a in $DocArgs) { if ($a -eq '--json') { $json = $true } }
231
+ if ($json) {
232
+ $chained = $null
233
+ if ($script:Selected -ne 'none') {
234
+ try {
235
+ $raw = (Invoke-Cli -CliArgs (@('doctor') + $DocArgs) 2>$null | Out-String).Trim()
236
+ if ($raw.StartsWith('{')) { $chained = ($raw | ConvertFrom-Json) }
237
+ }
238
+ catch { $chained = $null }
239
+ }
240
+ Write-Output (Build-DiagnosisJson $chained)
241
+ }
242
+ else {
243
+ Write-DiagnosisText
244
+ if ($script:Selected -ne 'none') {
245
+ Write-Output ''
246
+ Write-Output "--- $Bin doctor ---"
247
+ Invoke-Cli -CliArgs (@('doctor') + $DocArgs)
248
+ }
249
+ }
250
+ }
251
+
252
+ # Default action: forward every argument to the resolved CLI and exit with its
253
+ # code. No usable runtime -> structured diagnosis on stderr, exit 78 (EX_CONFIG)
254
+ # so the agent never mistakes the diagnosis for a result.
255
+ function Invoke-Run {
256
+ param([string[]] $CliArgs)
257
+ $sel = Resolve-LaunchKind
258
+ switch ($sel) {
259
+ 'path' { & $Bin @CliArgs; exit $LASTEXITCODE }
260
+ 'npx' { & npx --yes --package "$Package@$Pinned" $Bin @CliArgs; exit $LASTEXITCODE }
261
+ 'bunx' { & bunx --bun "$Package@$Pinned" @CliArgs; exit $LASTEXITCODE }
262
+ 'none' {
263
+ Collect
264
+ [Console]::Error.WriteLine((Build-DiagnosisJson $null))
265
+ exit 78
266
+ }
267
+ }
268
+ }
269
+
270
+ $Command = ''
271
+ if ($args.Count -ge 1) { $Command = [string]$args[0] }
272
+ $Rest = @()
273
+ if ($args.Count -gt 1) { $Rest = $args[1..($args.Count - 1)] }
274
+
275
+ switch ($Command) {
276
+ 'doctor' { Invoke-Doctor -DocArgs $Rest }
277
+ 'where' { Collect; Write-Output $script:Selected }
278
+ default { Invoke-Run -CliArgs $args }
279
+ }