moi-computer 0.11.1-next.0 → 0.11.1-next.2

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 (211) hide show
  1. package/client/AGENTS.md +2 -2
  2. package/client/components/ui/button.tsx +1 -53
  3. package/client/components/ui/collapsible.tsx +1 -15
  4. package/client/components/ui/dialog.tsx +1 -58
  5. package/client/components/ui/dropdown-menu.tsx +1 -254
  6. package/client/components/ui/hover-card.tsx +1 -44
  7. package/client/components/ui/input.tsx +1 -21
  8. package/client/components/ui/popover.tsx +1 -88
  9. package/client/components/ui/resizable.tsx +1 -40
  10. package/client/components/ui/skeleton.tsx +1 -13
  11. package/client/components/ui/slider.tsx +1 -70
  12. package/client/components/ui/spinner.tsx +1 -18
  13. package/client/components/ui/switch.tsx +1 -23
  14. package/client/components/ui/tabs.tsx +1 -0
  15. package/client/components/ui/textarea.tsx +1 -18
  16. package/client/components/ui/tooltip.tsx +1 -51
  17. package/client/features/chat/ChatPanel.tsx +6 -6
  18. package/client/features/chat/ChatPopup.tsx +3 -4
  19. package/client/features/chat/TurnView.tsx +5 -4
  20. package/client/features/chat/api.ts +5 -14
  21. package/client/features/chat/chat-frames.ts +9 -7
  22. package/client/features/chat/chat-send.ts +13 -2
  23. package/client/features/chat/chat-store.ts +1 -3
  24. package/client/features/chat/composer/ChatComposer.tsx +5 -1
  25. package/client/features/chat/composer/ModelPicker.tsx +44 -44
  26. package/client/features/chat/composer/banners/ErrorBanner.tsx +20 -12
  27. package/client/features/chat/composer/model-order.ts +15 -0
  28. package/client/features/chat/composer/useWorkspaceComposerState.tsx +15 -7
  29. package/client/features/chat/group-turns.ts +12 -0
  30. package/client/features/chat/interleave-notices.ts +7 -11
  31. package/client/features/chat/session-view.ts +53 -0
  32. package/client/features/chat/tool-group/ToolCallGroup.tsx +1 -4
  33. package/client/features/chat/tool-group/format/codex.ts +4 -1
  34. package/client/features/chat/useChat.ts +10 -9
  35. package/client/features/dev/ChatStatesPage.tsx +3 -1
  36. package/client/features/dev/DevIndexPage.tsx +5 -0
  37. package/client/features/dev/DevRoutes.tsx +2 -0
  38. package/client/features/dev/HarnessDebugPage.tsx +1 -1
  39. package/client/features/dev/UiComponentPreviews.tsx +834 -0
  40. package/client/features/dev/UiComponentsPage.tsx +60 -0
  41. package/client/features/home/WorkspacePreview.tsx +1 -1
  42. package/client/features/home/workspace-setup/DemoDialog.tsx +3 -2
  43. package/client/features/home/workspace-setup/ImportWorkspaceDialog.tsx +5 -8
  44. package/client/features/home/workspace-setup/WorkspaceDialogContent.tsx +7 -20
  45. package/client/features/settings/SettingsLayout.tsx +1 -1
  46. package/client/features/settings/WorkspaceSettings.tsx +4 -1
  47. package/client/features/views/ViewManager.tsx +14 -9
  48. package/client/features/widgets/WidgetFrame.tsx +1 -1
  49. package/client/features/widgets/Widgets.tsx +1 -3
  50. package/client/features/workspace/WorkspaceScreen.tsx +5 -1
  51. package/client/features/workspace/WorkspaceSplitLayout.tsx +6 -1
  52. package/client/features/workspace/api.ts +2 -4
  53. package/client/index.css +22 -29
  54. package/dist/chunk-245wt82v.css +1 -0
  55. package/dist/{chunk-tny85yt2.js → chunk-4s15axjs.js} +2 -2
  56. package/dist/{chunk-c9pjqx9m.js → chunk-53a7pxcz.js} +1 -1
  57. package/dist/{chunk-104m57qw.js → chunk-87thzbtw.js} +2 -2
  58. package/dist/chunk-a6jad1a6.js +1 -0
  59. package/dist/chunk-dghwstd2.js +9 -0
  60. package/dist/chunk-eg120aca.js +1 -0
  61. package/dist/{chunk-v7wr1df4.js → chunk-j3wvxjr7.js} +1 -1
  62. package/dist/chunk-j7hh2ab4.js +70 -0
  63. package/dist/chunk-w482ew2c.js +546 -0
  64. package/dist/chunk-y8eyahwx.js +1 -0
  65. package/dist/index.html +1 -1
  66. package/lib/format.ts +55 -6
  67. package/lib/themes.ts +6 -6
  68. package/lib/types.ts +10 -9
  69. package/package.json +12 -5
  70. package/registry.json +643 -0
  71. package/server/agent.ts +1 -1
  72. package/server/api.ts +26 -30
  73. package/server/applets.ts +30 -29
  74. package/server/bundler/applet-css.ts +7 -107
  75. package/server/bundler/build-applet.ts +5 -1
  76. package/server/bundler/build-worker.ts +178 -0
  77. package/server/cli-ui-components.ts +28 -33
  78. package/server/cli.ts +6 -11
  79. package/server/data-dir.ts +1 -1
  80. package/server/harness/README.md +41 -48
  81. package/server/harness/acp/NOTES.md +426 -0
  82. package/server/harness/acp/discovery.ts +27 -30
  83. package/server/harness/acp/model-state.ts +57 -0
  84. package/server/harness/acp/session.ts +18 -6
  85. package/server/harness/claude-code/NOTES.md +47 -12
  86. package/server/harness/claude-code/cli.ts +40 -0
  87. package/server/harness/claude-code/index.ts +11 -5
  88. package/server/harness/claude-code/models.ts +72 -19
  89. package/server/harness/claude-code/session.ts +408 -299
  90. package/server/harness/codex/NOTES.md +222 -350
  91. package/server/harness/codex/adapter.ts +94 -45
  92. package/server/harness/codex/auth.ts +1 -3
  93. package/server/harness/codex/client.ts +169 -249
  94. package/server/harness/codex/discovery.ts +9 -17
  95. package/server/harness/codex/index.ts +2 -1
  96. package/server/harness/codex/pagination.ts +24 -0
  97. package/server/harness/codex/permissions.ts +31 -7
  98. package/server/harness/codex/probe.ts +10 -13
  99. package/server/harness/codex/session.ts +549 -136
  100. package/server/harness/codex/transport.ts +184 -0
  101. package/server/harness/debug.ts +1 -1
  102. package/server/harness/hermes/NOTES.md +18 -0
  103. package/server/harness/hermes/discovery.ts +20 -0
  104. package/server/harness/hermes/index.ts +9 -0
  105. package/server/harness/hermes/models.ts +28 -8
  106. package/server/harness/types.ts +4 -2
  107. package/server/moi-scaffold.ts +1 -1
  108. package/server/preview.ts +3 -1
  109. package/server/registry.ts +63 -41
  110. package/server/tldraw-package.d.ts +4 -0
  111. package/server/ui-components.ts +101 -434
  112. package/server/view-builders.ts +2 -1
  113. package/server/web.ts +12 -5
  114. package/server/workspace-env.ts +3 -2
  115. package/tsconfig.json +0 -1
  116. package/ui-components/accordion.tsx +71 -0
  117. package/ui-components/alert-dialog.tsx +165 -0
  118. package/ui-components/alert.tsx +68 -0
  119. package/ui-components/attachment.tsx +195 -0
  120. package/ui-components/avatar.tsx +90 -0
  121. package/ui-components/badge.tsx +49 -0
  122. package/ui-components/button-group.tsx +78 -0
  123. package/ui-components/button.tsx +55 -0
  124. package/ui-components/calendar.tsx +180 -0
  125. package/ui-components/card.tsx +84 -0
  126. package/ui-components/carousel.tsx +235 -0
  127. package/ui-components/chart.tsx +338 -0
  128. package/ui-components/checkbox.tsx +28 -0
  129. package/ui-components/collapsible.tsx +15 -0
  130. package/ui-components/combobox.tsx +267 -0
  131. package/ui-components/context-menu.tsx +244 -0
  132. package/ui-components/dialog.tsx +138 -0
  133. package/ui-components/docs/accordion.md +463 -0
  134. package/ui-components/docs/alert-dialog.md +481 -0
  135. package/ui-components/docs/alert.md +301 -0
  136. package/ui-components/docs/attachment.md +689 -0
  137. package/ui-components/docs/avatar.md +495 -0
  138. package/ui-components/docs/badge.md +258 -0
  139. package/ui-components/docs/button-group.md +879 -0
  140. package/ui-components/docs/button.md +26 -0
  141. package/ui-components/docs/calendar.md +862 -0
  142. package/ui-components/docs/card.md +265 -0
  143. package/ui-components/docs/carousel.md +627 -0
  144. package/ui-components/docs/chart.md +1256 -0
  145. package/ui-components/docs/checkbox.md +477 -0
  146. package/ui-components/docs/collapsible.md +446 -0
  147. package/ui-components/docs/combobox.md +1003 -0
  148. package/ui-components/docs/context-menu.md +860 -0
  149. package/ui-components/docs/data-table.md +1580 -0
  150. package/ui-components/docs/date-picker.md +614 -0
  151. package/ui-components/docs/dialog.md +414 -0
  152. package/ui-components/docs/drawer.md +51 -0
  153. package/ui-components/docs/dropdown-menu.md +1000 -0
  154. package/ui-components/docs/field.md +1192 -0
  155. package/ui-components/docs/hover-card.md +275 -0
  156. package/ui-components/docs/input-group.md +866 -0
  157. package/ui-components/docs/input.md +456 -0
  158. package/ui-components/docs/label.md +295 -0
  159. package/ui-components/docs/pagination.md +416 -0
  160. package/ui-components/docs/popover.md +348 -0
  161. package/ui-components/docs/progress.md +214 -0
  162. package/ui-components/docs/radio-group.md +389 -0
  163. package/ui-components/docs/resizable.md +265 -0
  164. package/ui-components/docs/select.md +616 -0
  165. package/ui-components/docs/separator.md +216 -0
  166. package/ui-components/docs/skeleton.md +205 -0
  167. package/ui-components/docs/slider.md +226 -0
  168. package/ui-components/docs/spinner.md +332 -0
  169. package/ui-components/docs/switch.md +236 -0
  170. package/ui-components/docs/table.md +551 -0
  171. package/ui-components/docs/tabs.md +379 -0
  172. package/ui-components/docs/textarea.md +187 -0
  173. package/ui-components/docs/toggle-group.md +370 -0
  174. package/ui-components/docs/toggle.md +91 -0
  175. package/ui-components/docs/tooltip.md +290 -0
  176. package/ui-components/drawer.tsx +213 -0
  177. package/ui-components/dropdown-menu.tsx +263 -0
  178. package/ui-components/field.tsx +224 -0
  179. package/ui-components/hover-card.tsx +46 -0
  180. package/ui-components/input-group.tsx +146 -0
  181. package/ui-components/input.tsx +20 -0
  182. package/ui-components/label.tsx +17 -0
  183. package/ui-components/pagination.tsx +124 -0
  184. package/ui-components/popover.tsx +81 -0
  185. package/ui-components/progress.tsx +63 -0
  186. package/ui-components/radio-group.tsx +38 -0
  187. package/ui-components/resizable.tsx +40 -0
  188. package/ui-components/scoped-portal.tsx +31 -0
  189. package/ui-components/select.tsx +195 -0
  190. package/ui-components/separator.tsx +18 -0
  191. package/ui-components/skeleton.tsx +15 -0
  192. package/ui-components/slider.tsx +75 -0
  193. package/ui-components/spinner.tsx +20 -0
  194. package/ui-components/switch.tsx +27 -0
  195. package/ui-components/table.tsx +86 -0
  196. package/ui-components/tabs.tsx +75 -0
  197. package/ui-components/textarea.tsx +17 -0
  198. package/ui-components/toggle-group.tsx +85 -0
  199. package/ui-components/toggle.tsx +42 -0
  200. package/ui-components/tooltip.tsx +61 -0
  201. package/ui-components/utils.ts +8 -0
  202. package/workspace/.claude/skills/moi-workspace/SKILL.md +11 -20
  203. package/workspace/.claude/skills/moi-workspace/references/DESIGN.md +4 -3
  204. package/workspace/.claude/skills/moi-workspace/references/UI-COMPONENTS.md +67 -203
  205. package/dist/chunk-59zf0m6n.js +0 -1
  206. package/dist/chunk-bj3akjs2.js +0 -1
  207. package/dist/chunk-j2w2vak4.js +0 -546
  208. package/dist/chunk-k2r9c95m.js +0 -10
  209. package/dist/chunk-kmbxhgwv.js +0 -1
  210. package/dist/chunk-p1yqa71k.js +0 -83
  211. package/dist/chunk-r5xrenjy.css +0 -1
@@ -5,16 +5,13 @@ display abstraction (`lib/format.ts`, implemented by `adapter.ts` here).
5
5
  Originally research for the message-display abstraction; kept as the reference
6
6
  spec for the CC adapter.
7
7
 
8
- > **Status note.** The SDK wire-format sections (message kinds, content
9
- > blocks, subagent/skill mechanics) remain the reference. But the "current
10
- > implementation" critiques and §14's migration plan describe the
11
- > **pre-refactor** code and have since shipped: chat moved from
12
- > `models.ts` to `session.ts` (both in this folder), `state.ts`'s
13
- > `transformMessage` and the 7-variant `Message` union were replaced by
14
- > `StreamEvent`/`Turn`/`Part` in `lib/format.ts` (built by
15
- > `adapter.ts`), and `MessageBlock.tsx` was replaced by
16
- > `client/components/TurnView.tsx`. Read those sections as historical
17
- > context, not as descriptions of today's code.
8
+ > **Status note.** The runtime section below describes the current lifecycle.
9
+ > The numbered sections preserve the original display-abstraction research;
10
+ > their wire examples are not an exhaustive list of today's SDK messages, and
11
+ > the migration proposed in §14 has shipped. Current display types live in
12
+ > `lib/format.ts`, mapping in `adapter.ts`, and rendering in
13
+ > `client/features/chat/TurnView.tsx`. Use the installed `sdk.d.ts` for current
14
+ > wire types.
18
15
 
19
16
  Evidence drawn from:
20
17
 
@@ -38,6 +35,44 @@ and version-manager shims. There is no SDK bundled-executable fallback. If the
38
35
  command is missing, setup and existing workspaces report:
39
36
  `Run curl -fsSL https://claude.ai/install.sh | sh in your terminal to install Claude`.
40
37
 
38
+ **In-place updates.** Claude Code can replace its binary at the same path
39
+ while moi keeps running, changing which models are available.
40
+
41
+ - Every spawn resolves the executable afresh, so new sessions run the new
42
+ binary. A live session keeps the subprocess it started with until it is torn
43
+ down (idle TTL, eviction, env change).
44
+ - The model catalog (`models.ts`) is keyed by executable path and the first
45
+ line of `claude --version`. Each lookup probes that identity again. Changes
46
+ refetch the catalog; a failed version probe at the same path retains it.
47
+ - On a changed identity, `index.ts` marks existing subprocesses `staleCli`
48
+ (`retireCCSessionsOnCliChange`). Invalidation never interrupts work. Before
49
+ the next message, the dispatcher waits for the current turn and background
50
+ jobs to finish, replaces the subprocess, and resumes the same session from
51
+ disk. Replacement happens on demand.
52
+ - Pending messages and their model, effort, fast-mode, and streaming settings
53
+ stay in moi's per-session queue. Only one user message enters the SDK per
54
+ turn. Matching result IDs prevent an autonomous background turn from
55
+ releasing a waiting user prompt. When the CLI emits lifecycle events,
56
+ dispatch also waits for `idle`. The queue follows session ID renames and
57
+ survives planned subprocess replacement.
58
+ - Settings apply immediately before dispatch. Rejected setters fail the send.
59
+ Model uses `setModel`; effort and fast mode use `applyFlagSettings` together.
60
+ An absent effort or fast-mode value clears that flag with `null`, restoring
61
+ provider defaults.
62
+ - Background jobs retain their subprocess. Messages requiring a replacement
63
+ wait in moi until those jobs finish, including replacements for streaming
64
+ changes. `background_tasks_changed` replaces the complete task set, including
65
+ backgrounded subagents and ambient watchers. Task start/update/completion
66
+ events are a fallback only until the process emits its first snapshot; their
67
+ ordering relative to snapshots is unspecified. A new process starts with an
68
+ empty set and resets that fallback. Background work alone does not make the
69
+ chat busy.
70
+ - Stop cancels queued messages and waits for interrupt acknowledgement and
71
+ turn completion. An unexpected process exit rejects pending sends.
72
+ - The picker gets the new lineup on the client's next `/agent` refetch (window
73
+ focus, reconnect). `moi status` prints the version the catalog was probed
74
+ for next to the executable path.
75
+
41
76
  ---
42
77
 
43
78
  ## 1. Three layers
@@ -55,7 +90,7 @@ Two filters between L1 and L2 are responsible for everything the UI currently ca
55
90
 
56
91
  ---
57
92
 
58
- ## 2. Layer 1 — Full `SDKMessage` union (21 variants)
93
+ ## 2. Layer 1 — Recorded `SDKMessage` variants
59
94
 
60
95
  Discriminated primarily by `type`, then `subtype` for `'system'` and `'result'`.
61
96
 
@@ -283,7 +318,7 @@ CLOSED have since been handled; see adapter.ts / session.ts):
283
318
  - **MCP tool calls proper** (`mcp_tool_use` / `mcp_tool_result` blocks) — dropped. Regular `Task`/`Bash` MCP tools happen to survive because they wear the plain `tool_use` shape.
284
319
  - **Streaming** — CLOSED: `stream_event` drives live token previews (`preview` frames) when the client opts into streaming.
285
320
  - **Session lifecycle signals** — partially CLOSED: `system/init` feeds the session snapshot; `system/session_state_changed` mirrors into `SessionActivity` (note: current CLIs don't emit it in streaming-input mode — `result` is the everyday turn-over fallback); `requires-action` reaches the wire but has no UI yet. `system/status` (`requesting`/`compacting`) still dropped.
286
- - **Sub-agent nesting** — CLOSED: `task_started`/`task_progress`/`task_notification` build nested subagent records; `task_started`/`task_updated`/`task_notification` also track live background tasks for the idle-eviction keep-alive (session.ts `bgTasks`).
321
+ - **Sub-agent nesting** — CLOSED: `task_started`/`task_progress`/`task_notification` build nested subagent records; `background_tasks_changed` snapshots track live background tasks for the idle-eviction keep-alive (session.ts `bgTasks`), with task events as the fallback for older CLIs.
287
322
  - **Hooks / rate limits / api_retry** — CLOSED: surfaced as notices.
288
323
  - **MessageBlock switch exhaustiveness** — the UI layer this referred to (`MessageBlock.tsx`) no longer exists; turns render via `TurnView`/`ToolCallGroup`.
289
324
 
@@ -0,0 +1,40 @@
1
+ // Include the version in CLI identity: auto-updates can replace the binary
2
+ // without changing its path.
3
+
4
+ export type ClaudeCli = {
5
+ executable: string
6
+ // First line of `claude --version` (e.g. `2.1.263 (Claude Code)`), or null
7
+ // when the probe failed or timed out.
8
+ version: string | null
9
+ }
10
+
11
+ const VERSION_PROBE_TIMEOUT_MS = 5_000
12
+
13
+ // Keep the reported line intact for identity comparisons and status output.
14
+ export function parseClaudeVersion(output: string): string | null {
15
+ const line = output.split('\n').find(l => l.trim().length > 0)
16
+ return line ? line.trim() : null
17
+ }
18
+
19
+ export function claudeCliKey(cli: ClaudeCli): string {
20
+ return `${cli.executable}\n${cli.version ?? '?'}`
21
+ }
22
+
23
+ // Probe failures yield an unknown version so callers can retain cached data.
24
+ export async function probeClaudeCli(executable: string): Promise<ClaudeCli> {
25
+ let timeout: ReturnType<typeof setTimeout> | undefined
26
+ try {
27
+ const proc = Bun.spawn([executable, '--version'], {
28
+ stdin: 'ignore',
29
+ stdout: 'pipe',
30
+ stderr: 'ignore'
31
+ })
32
+ timeout = setTimeout(() => proc.kill(), VERSION_PROBE_TIMEOUT_MS)
33
+ const [output, exitCode] = await Promise.all([new Response(proc.stdout).text(), proc.exited])
34
+ return { executable, version: exitCode === 0 ? parseClaudeVersion(output) : null }
35
+ } catch {
36
+ return { executable, version: null }
37
+ } finally {
38
+ clearTimeout(timeout)
39
+ }
40
+ }
@@ -9,7 +9,7 @@ import { findHarnessExecutable, pathHarnessAvailability } from '../executable'
9
9
  import { getClaudeAuthReadiness, startClaudeLogin } from './auth'
10
10
  import { isLinkedGitWorktree } from './git-worktree'
11
11
  import { getMcpStatus } from './mcp'
12
- import { getClaudeModels } from './models'
12
+ import { getClaudeModels, lastProbedClaudeCli, onClaudeCliChanged } from './models'
13
13
  import {
14
14
  SESSION_LIMITS,
15
15
  getCCDebugSnapshot,
@@ -17,6 +17,7 @@ import {
17
17
  interruptCCSession,
18
18
  killAllCCSessions,
19
19
  restartWorkspaceSessions,
20
+ retireCCSessionsOnCliChange,
20
21
  sendCCMessage
21
22
  } from './session'
22
23
  import {
@@ -73,13 +74,17 @@ async function discoverWorkspaces(
73
74
  }
74
75
  }
75
76
 
77
+ // Keep future sends on the CLI version that supplied the model catalog.
78
+ // Active turns and background work finish before the dispatcher replaces it.
79
+ onClaudeCliChanged(() => retireCCSessionsOnCliChange())
80
+
76
81
  export const claudeCodeHarness: Harness = {
77
82
  id: 'claude-code',
78
83
  capabilities: {
79
84
  supportsStreaming: true,
80
85
  imagesInline: 'base64',
81
86
  liveModelSwitch: true,
82
- liveEffortSwitch: false, // construct-time; session.ts drains + rebuilds
87
+ liveEffortSwitch: true,
83
88
  nativeUserEcho: false // streaming-input never echoes; the server synthesizes the turn
84
89
  },
85
90
 
@@ -111,7 +116,8 @@ export const claudeCodeHarness: Harness = {
111
116
  const cc = getCCDebugSnapshot()
112
117
  const busy = cc.sessions.filter(s => s.activity !== 'idle').length
113
118
  const lines = [
114
- `claude executable ${findHarnessExecutable('claude-code') ?? '(not found)'}`,
119
+ `claude executable ${findHarnessExecutable('claude-code') ?? '(not found)'}` +
120
+ ` (${lastProbedClaudeCli()?.version ?? 'version not probed yet'})`,
115
121
  `live CC sessions ${cc.sessions.length}/${SESSION_LIMITS.maxLive} ` +
116
122
  `(${busy} busy, ${cc.sessions.length - busy} idle, ${cc.aliases} alias${cc.aliases === 1 ? '' : 'es'}, ` +
117
123
  `idle TTL ${fmtDuration(SESSION_LIMITS.idleTtlMs)})`
@@ -146,8 +152,8 @@ export const claudeCodeHarness: Harness = {
146
152
  `lastActivity=${fmtAgo(s.lastActivityAt, now)}`
147
153
  ]
148
154
  if (s.bgTasks > 0) bits.push(`bgTasks=${s.bgTasks}`)
149
- if (s.desiredEffort !== s.effort) bits.push(`desiredEffort=${s.desiredEffort ?? 'default'}`)
150
- if (s.desiredStream !== s.stream) bits.push(`desiredStream=${s.desiredStream ? 'on' : 'off'}`)
155
+ if (s.queuedMessages > 0) bits.push(`queued=${s.queuedMessages}`)
156
+ if (s.staleCli) bits.push('CLI update pending')
151
157
  if (s.activity === 'idle' && !s.hasIdleTimer) bits.push('(no idle timer)')
152
158
  let line = ' ' + bits.join(' ')
153
159
  if (s.lastUserText) line += `\n last: ${JSON.stringify(s.lastUserText)}`
@@ -4,20 +4,15 @@ import type { ModelInfo } from '@anthropic-ai/claude-agent-sdk'
4
4
  import type { Model } from '@/lib/types'
5
5
 
6
6
  import { requireHarnessExecutable } from '../executable'
7
+ import { type ClaudeCli, claudeCliKey, probeClaudeCli } from './cli'
7
8
 
8
- // Chat runs live in `cc-session.ts` (streaming-input sessions held per thread).
9
- // MCP status probing lives in `mcp.ts`. This module now only probes the agent
10
- // backend for the model list — spins up a throwaway `query()` and reads metadata.
11
-
12
- // Claude's available models come from the account/CLI, not the workspace, so
13
- // the list is identical everywhere. We still need a `cwd` to spin up a probe
14
- // query, but cache the result process-wide.
15
- async function fetchClaudeModels(cwd: string): Promise<ModelInfo[]> {
9
+ // The metadata query needs a cwd; its catalog is cached across workspaces.
10
+ async function fetchClaudeModels(cwd: string, executable: string): Promise<ModelInfo[]> {
16
11
  const q = query({
17
12
  prompt: '',
18
13
  options: {
19
14
  cwd,
20
- pathToClaudeCodeExecutable: requireHarnessExecutable('claude-code'),
15
+ pathToClaudeCodeExecutable: executable,
21
16
  persistSession: false,
22
17
  settingSources: ['user', 'project'],
23
18
  env: { ...process.env, CLAUDECODE: undefined }
@@ -25,24 +20,81 @@ async function fetchClaudeModels(cwd: string): Promise<ModelInfo[]> {
25
20
  })
26
21
  const models = await q.supportedModels()
27
22
  await q.close()
28
- // Raw SDK shape, passed through to the client as-is.
29
23
  return models
30
24
  }
31
25
 
32
- // One in-flight/settled promise shared across all callers. On failure we clear
33
- // it so a later request can retry instead of caching the rejection forever.
34
- let claudeModelsPromise: Promise<ModelInfo[]> | null = null
26
+ export type ClaudeCliChangeListener = (next: ClaudeCli, previous: ClaudeCli) => void
27
+
28
+ export type ClaudeCatalogOptions = {
29
+ probe: (executable: string) => Promise<ClaudeCli>
30
+ fetch: (cwd: string, executable: string) => Promise<ModelInfo[]>
31
+ }
32
+
33
+ type CatalogEntry = {
34
+ key: string
35
+ cli: ClaudeCli
36
+ models: Promise<ModelInfo[]>
37
+ }
38
+
39
+ // Recheck CLI identity on each lookup. Callers share the model-fetch promise
40
+ // until the identity changes; failed fetches are dropped so they can be retried.
41
+ export function createClaudeCatalog(options: ClaudeCatalogOptions) {
42
+ let entry: CatalogEntry | null = null
43
+ const listeners = new Set<ClaudeCliChangeListener>()
35
44
 
36
- function getCachedClaudeModels(cwd: string): Promise<ModelInfo[]> {
37
- if (!claudeModelsPromise) {
38
- claudeModelsPromise = fetchClaudeModels(cwd).catch(err => {
39
- claudeModelsPromise = null
45
+ function store(cli: ClaudeCli, cwd: string): Promise<ModelInfo[]> {
46
+ const models = options.fetch(cwd, cli.executable).catch(err => {
47
+ if (entry?.models === models) entry = null
40
48
  throw err
41
49
  })
50
+ entry = { key: claudeCliKey(cli), cli, models }
51
+ return models
52
+ }
53
+
54
+ async function models(cwd: string, executable: string): Promise<ModelInfo[]> {
55
+ const cli = await options.probe(executable)
56
+ const current = entry
57
+ if (current) {
58
+ if (current.key === claudeCliKey(cli)) return current.models
59
+ // A failed version probe says nothing about the CLI. Keep serving the
60
+ // catalog we have instead of re-spawning a probe query per request.
61
+ if (cli.version === null && current.cli.executable === executable) return current.models
62
+ }
63
+ const next = store(cli, cwd)
64
+ if (current) {
65
+ console.log(
66
+ `[claude-code] claude changed (${describe(current.cli)} → ${describe(cli)}); refreshing the model catalog`
67
+ )
68
+ for (const listener of listeners) listener(cli, current.cli)
69
+ }
70
+ return next
42
71
  }
43
- return claudeModelsPromise
72
+
73
+ // Initial discovery is not a CLI change.
74
+ function onCliChanged(listener: ClaudeCliChangeListener): () => void {
75
+ listeners.add(listener)
76
+ return () => {
77
+ listeners.delete(listener)
78
+ }
79
+ }
80
+
81
+ // Cached identity for diagnostics; model lookups still need a fresh probe.
82
+ function current(): ClaudeCli | undefined {
83
+ return entry?.cli
84
+ }
85
+
86
+ return { models, onCliChanged, current }
87
+ }
88
+
89
+ function describe(cli: ClaudeCli): string {
90
+ return `${cli.version ?? 'unknown version'} at ${cli.executable}`
44
91
  }
45
92
 
93
+ const catalog = createClaudeCatalog({ probe: probeClaudeCli, fetch: fetchClaudeModels })
94
+
95
+ export const onClaudeCliChanged = catalog.onCliChanged
96
+ export const lastProbedClaudeCli = catalog.current
97
+
46
98
  export function withClaudeFastModeDefault(
47
99
  models: readonly ModelInfo[],
48
100
  defaultFastMode: boolean
@@ -53,8 +105,9 @@ export function withClaudeFastModeDefault(
53
105
  }
54
106
 
55
107
  export async function getClaudeModels(cwd: string): Promise<Model[]> {
108
+ const executable = requireHarnessExecutable('claude-code')
56
109
  const [models, settings] = await Promise.all([
57
- getCachedClaudeModels(cwd),
110
+ catalog.models(cwd, executable),
58
111
  resolveSettings({ cwd, settingSources: ['user', 'project'] })
59
112
  ])
60
113
  const defaultFastMode =