@lucascouts/claude-agent-tui 0.14.13 → 0.15.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/dist/acp-agent.d.ts +85 -23
- package/dist/acp-agent.js +242 -86
- package/dist/background-task-end.d.ts +11 -0
- package/dist/background-task-end.js +92 -0
- package/dist/diff-enriched-reader.d.ts +6 -0
- package/dist/diff-enriched-reader.js +1 -0
- package/dist/gate/settings-writer.d.ts +5 -7
- package/dist/gate/settings-writer.js +14 -17
- package/dist/live-model-catalog.d.ts +80 -2
- package/dist/live-model-catalog.js +132 -17
- package/dist/model-catalog.d.ts +57 -21
- package/dist/model-catalog.js +141 -25
- package/dist/resumed-permission-mode.d.ts +6 -0
- package/dist/resumed-permission-mode.js +104 -0
- package/dist/tools.d.ts +16 -2
- package/dist/tools.js +45 -6
- package/package.json +2 -2
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// Item 92 (upstream #1312) — when a backgrounded command's task ended, read off the raw transcript.
|
|
2
|
+
//
|
|
3
|
+
// A `run_in_background` Bash returns its tool result as soon as the command detaches, so the result
|
|
4
|
+
// cannot carry the command's exit. Claude Code reports the end later, as a `<task-notification>` block
|
|
5
|
+
// naming the Bash call's `tool_use_id`. Measured on CLI 2.1.280 (`sdk-cli`) and on an interactive
|
|
6
|
+
// (`cli`) transcript: the block is written into a `queue-operation` (`content`) and a `queued_command`
|
|
7
|
+
// attachment (`prompt`), and can also reach the transcript as the text of a user message. None of those
|
|
8
|
+
// records reaches `getSessionMessages`' reduced shape, so the raw lines are the only source.
|
|
9
|
+
//
|
|
10
|
+
// The block carries no structured code. Its summary names one ("… completed (exit code 0)", "… failed
|
|
11
|
+
// with exit code 3"); without one, `completed` reads 0 and anything else 1, because Zeo marks a command
|
|
12
|
+
// failed only on a non-null non-zero code and a stopped or failed command is not a success.
|
|
13
|
+
const BLOCK = /<task-notification>([\s\S]*?)<\/task-notification>/g;
|
|
14
|
+
const SUMMARY_CODE = /exit code (\d+)/i;
|
|
15
|
+
function tag(block, name) {
|
|
16
|
+
const open = `<${name}>`;
|
|
17
|
+
const start = block.indexOf(open);
|
|
18
|
+
if (start === -1)
|
|
19
|
+
return undefined;
|
|
20
|
+
const end = block.indexOf(`</${name}>`, start + open.length);
|
|
21
|
+
if (end === -1)
|
|
22
|
+
return undefined;
|
|
23
|
+
return block.slice(start + open.length, end).trim();
|
|
24
|
+
}
|
|
25
|
+
function endOf(status, summary) {
|
|
26
|
+
const named = summary?.match(SUMMARY_CODE);
|
|
27
|
+
if (named)
|
|
28
|
+
return { status, exitCode: Number(named[1]) };
|
|
29
|
+
return { status, exitCode: status === "completed" ? 0 : 1 };
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The texts of one transcript record that may hold a notification Claude Code wrote. A tool_result's
|
|
33
|
+
* content and an assistant's text are deliberately excluded: `cat` of a transcript, or a model quoting
|
|
34
|
+
* one, would otherwise end a command that is still running.
|
|
35
|
+
*/
|
|
36
|
+
function notificationTexts(record) {
|
|
37
|
+
const texts = [];
|
|
38
|
+
if (record.type === "queue-operation" && typeof record.content === "string") {
|
|
39
|
+
texts.push(record.content);
|
|
40
|
+
}
|
|
41
|
+
const attachment = record.attachment;
|
|
42
|
+
if (record.type === "attachment" && attachment?.type === "queued_command") {
|
|
43
|
+
if (typeof attachment.prompt === "string")
|
|
44
|
+
texts.push(attachment.prompt);
|
|
45
|
+
}
|
|
46
|
+
const message = record.message;
|
|
47
|
+
if (record.type === "user" && message?.role === "user") {
|
|
48
|
+
if (typeof message.content === "string")
|
|
49
|
+
texts.push(message.content);
|
|
50
|
+
else if (Array.isArray(message.content)) {
|
|
51
|
+
for (const block of message.content) {
|
|
52
|
+
const b = block;
|
|
53
|
+
if (b?.type === "text" && typeof b.text === "string")
|
|
54
|
+
texts.push(b.text);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return texts;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Index every ended background task in the raw transcript lines by the `tool_use_id` of the call that
|
|
62
|
+
* started it. Lines that are not JSON, or carry no notification, are skipped; the last notification for
|
|
63
|
+
* an id wins. Pure; never throws.
|
|
64
|
+
*/
|
|
65
|
+
export function backgroundTaskEnds(lines) {
|
|
66
|
+
const ends = new Map();
|
|
67
|
+
for (const line of lines) {
|
|
68
|
+
// Cheap pre-filter: nearly every line carries no notification, and parsing them all is the cost.
|
|
69
|
+
if (!line.includes("task-notification"))
|
|
70
|
+
continue;
|
|
71
|
+
let record;
|
|
72
|
+
try {
|
|
73
|
+
record = JSON.parse(line);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (!record || typeof record !== "object")
|
|
79
|
+
continue;
|
|
80
|
+
for (const text of notificationTexts(record)) {
|
|
81
|
+
for (const match of text.matchAll(BLOCK)) {
|
|
82
|
+
const block = match[1];
|
|
83
|
+
const toolUseId = tag(block, "tool-use-id");
|
|
84
|
+
const status = tag(block, "status");
|
|
85
|
+
if (!toolUseId || !status)
|
|
86
|
+
continue;
|
|
87
|
+
ends.set(toolUseId, endOf(status, tag(block, "summary")));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return ends;
|
|
92
|
+
}
|
|
@@ -24,6 +24,12 @@ export interface DiffEnrichedReaderOptions {
|
|
|
24
24
|
* unreadable transcript for the R1.3 fallback) without touching the filesystem.
|
|
25
25
|
*/
|
|
26
26
|
readRawLines?: (path: string) => string[];
|
|
27
|
+
/**
|
|
28
|
+
* Item 92 (upstream #1312): handed the raw lines of every successful read, so a caller can derive
|
|
29
|
+
* more from the same read (the agent indexes ended background tasks) without reading the transcript
|
|
30
|
+
* twice. Not called when the transcript cannot be located or read.
|
|
31
|
+
*/
|
|
32
|
+
onRawLines?: (sessionId: string, lines: string[]) => void;
|
|
27
33
|
}
|
|
28
34
|
/**
|
|
29
35
|
* Wrap a base `getSessionMessages`-shaped reader so each returned message gains its `toolUseResult`
|
|
@@ -145,15 +145,13 @@ export declare function injectHook(opts: {
|
|
|
145
145
|
*/
|
|
146
146
|
export declare function restore(backup: Backup): Promise<RestoreResult>;
|
|
147
147
|
/**
|
|
148
|
-
*
|
|
149
|
-
* gate wrote
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
* mirroring {@link injectHook}. A missing/empty file is treated as an empty object. No-op-safe and
|
|
153
|
-
* idempotent: re-applying the same `active` produces a byte-equivalent document.
|
|
148
|
+
* Item 84 — set (`active`) or remove the `ultracode` key in the per-session SCRATCH settings file the
|
|
149
|
+
* gate wrote, preserving the hook and EVERY other key. Read tolerantly (JSONC) and written durably
|
|
150
|
+
* (temp+fsync+rename), mirroring {@link injectHook}. A missing/empty file is treated as an empty
|
|
151
|
+
* object. Idempotent: re-applying the same `active` produces a byte-equivalent document.
|
|
154
152
|
*
|
|
155
153
|
* @param settingsPath absolute path to the per-session scratch `settings.local.json` (the gate's file).
|
|
156
|
-
* @param active whether
|
|
154
|
+
* @param active whether Ultracode is on (set the key) or off (remove it).
|
|
157
155
|
* @throws {Error} only on a genuine read/parse/write failure — NEVER on the benign absence of the file
|
|
158
156
|
* (ENOENT → treated as `{}`), mirroring the rest of the module's discipline.
|
|
159
157
|
*/
|
|
@@ -354,26 +354,25 @@ export async function restore(backup) {
|
|
|
354
354
|
}
|
|
355
355
|
}
|
|
356
356
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
357
|
-
//
|
|
357
|
+
// Item 84 — mirror the Ultracode switch into the per-session SCRATCH settings file.
|
|
358
358
|
//
|
|
359
|
-
// The
|
|
360
|
-
//
|
|
361
|
-
//
|
|
362
|
-
//
|
|
359
|
+
// The switch is applied live with `/effort ultracode on|off`, which the CLI keeps for "this session
|
|
360
|
+
// only" — a re-spawn of the PTY (mode/agent change, resume) starts a new CLI process without it. The
|
|
361
|
+
// scratch file is that process's `--settings`, so `ultracode:true` here carries the switch across a
|
|
362
|
+
// re-spawn. It is NOT a re-spawn trigger. Story 060 also wrote `ultracodeKeywordTrigger:true`; that is
|
|
363
|
+
// not a settings key: CLI 2.1.261, 2.1.285, 2.1.296 (parity audit, round 21) and 2.1.299 use it only
|
|
364
|
+
// as a telemetry label — the real key is `workflowKeywordTriggerEnabled` — so it was dropped.
|
|
363
365
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
364
|
-
/** The
|
|
366
|
+
/** The settings key that keeps Ultracode on for a (re-)spawned session. */
|
|
365
367
|
const ULTRACODE_SETTINGS_KEY = "ultracode";
|
|
366
|
-
const ULTRACODE_KEYWORD_TRIGGER_KEY = "ultracodeKeywordTrigger";
|
|
367
368
|
/**
|
|
368
|
-
*
|
|
369
|
-
* gate wrote
|
|
370
|
-
*
|
|
371
|
-
*
|
|
372
|
-
* mirroring {@link injectHook}. A missing/empty file is treated as an empty object. No-op-safe and
|
|
373
|
-
* idempotent: re-applying the same `active` produces a byte-equivalent document.
|
|
369
|
+
* Item 84 — set (`active`) or remove the `ultracode` key in the per-session SCRATCH settings file the
|
|
370
|
+
* gate wrote, preserving the hook and EVERY other key. Read tolerantly (JSONC) and written durably
|
|
371
|
+
* (temp+fsync+rename), mirroring {@link injectHook}. A missing/empty file is treated as an empty
|
|
372
|
+
* object. Idempotent: re-applying the same `active` produces a byte-equivalent document.
|
|
374
373
|
*
|
|
375
374
|
* @param settingsPath absolute path to the per-session scratch `settings.local.json` (the gate's file).
|
|
376
|
-
* @param active whether
|
|
375
|
+
* @param active whether Ultracode is on (set the key) or off (remove it).
|
|
377
376
|
* @throws {Error} only on a genuine read/parse/write failure — NEVER on the benign absence of the file
|
|
378
377
|
* (ENOENT → treated as `{}`), mirroring the rest of the module's discipline.
|
|
379
378
|
*/
|
|
@@ -388,16 +387,14 @@ export async function applyUltracodeSettings(settingsPath, active) {
|
|
|
388
387
|
current = parsePriorSettings(text);
|
|
389
388
|
}
|
|
390
389
|
}
|
|
391
|
-
// 2) Clone (never mutate the parsed prior) and toggle ONLY the
|
|
390
|
+
// 2) Clone (never mutate the parsed prior) and toggle ONLY the ultracode key — every other key
|
|
392
391
|
// (the gate hook, the user's own config) is preserved verbatim.
|
|
393
392
|
const next = structuredClone(current);
|
|
394
393
|
if (active) {
|
|
395
394
|
next[ULTRACODE_SETTINGS_KEY] = true;
|
|
396
|
-
next[ULTRACODE_KEYWORD_TRIGGER_KEY] = true;
|
|
397
395
|
}
|
|
398
396
|
else {
|
|
399
397
|
delete next[ULTRACODE_SETTINGS_KEY];
|
|
400
|
-
delete next[ULTRACODE_KEYWORD_TRIGGER_KEY];
|
|
401
398
|
}
|
|
402
399
|
// 3) Durably write (temp+fsync+rename) so a concurrent (re-)spawn read never sees a half-written file.
|
|
403
400
|
await durableWrite(settingsPath, `${JSON.stringify(next, null, 2)}\n`);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ModelInfo } from "@anthropic-ai/claude-agent-sdk";
|
|
1
|
+
import type { ModelInfo, Options, Query, SDKUserMessage } from "@anthropic-ai/claude-agent-sdk";
|
|
2
2
|
/**
|
|
3
3
|
* The model picker, read from the CLI at runtime instead of curated by hand.
|
|
4
4
|
*
|
|
@@ -43,6 +43,84 @@ interface CatalogLogger {
|
|
|
43
43
|
}
|
|
44
44
|
/** Drop the memoised catalogue AND the once-per-process report latch. Tests use it; production does not. */
|
|
45
45
|
export declare function resetLiveModelCatalogCache(): void;
|
|
46
|
+
/**
|
|
47
|
+
* Environment variable that selects where the default source reads from.
|
|
48
|
+
* `builtin` serves {@link MODEL_CATALOG} without spawning anything — an offline
|
|
49
|
+
* knob for production, and what the test harness sets on every adapter it spawns
|
|
50
|
+
* as a child process. Unset (or any other value) means the live CLI read.
|
|
51
|
+
*/
|
|
52
|
+
export declare const MODEL_CATALOG_ENV = "CLAUDE_TUI_MODEL_CATALOG";
|
|
53
|
+
/**
|
|
54
|
+
* Opt-in for a test that genuinely needs the live CLI read. Without it, the live
|
|
55
|
+
* source refuses to run under the node test runner — see {@link LiveCatalogInTestsError}.
|
|
56
|
+
*/
|
|
57
|
+
export declare const LIVE_CATALOG_TESTS_ENV = "CLAUDE_TUI_LIVE_CATALOG_TESTS";
|
|
58
|
+
/**
|
|
59
|
+
* Raised when the LIVE source is reached from inside `node --test` without the
|
|
60
|
+
* {@link LIVE_CATALOG_TESTS_ENV} opt-in.
|
|
61
|
+
*
|
|
62
|
+
* WHY IT EXISTS. Each test file runs in its own process with an empty cache, and
|
|
63
|
+
* most suites create sessions without injecting a catalogue. Before this guard,
|
|
64
|
+
* one `npm run test:fork` spawned ~50 real CLI sessions — each loading the
|
|
65
|
+
* user's hooks, plugins and MCP servers — and five runs made 248 SDK sessions in
|
|
66
|
+
* 17 minutes (measured 2026-10-14). The test setup module installs a stub as the
|
|
67
|
+
* default source; this error is the tripwire for anything that bypasses it.
|
|
68
|
+
*
|
|
69
|
+
* {@link resolveModelCatalog} rethrows it instead of falling back, so the test
|
|
70
|
+
* that reached the live read FAILS rather than passing on the built-in list.
|
|
71
|
+
* `NODE_TEST_CONTEXT` is set only by node's test runner, so production never
|
|
72
|
+
* sees this.
|
|
73
|
+
*/
|
|
74
|
+
export declare class LiveCatalogInTestsError extends Error {
|
|
75
|
+
constructor();
|
|
76
|
+
}
|
|
77
|
+
/** Throws {@link LiveCatalogInTestsError} under `node --test` unless the live opt-in is set. */
|
|
78
|
+
export declare function assertLiveCatalogAllowed(env?: NodeJS.ProcessEnv): void;
|
|
79
|
+
/**
|
|
80
|
+
* Replace the source used when a caller passes none. Only the test setup module
|
|
81
|
+
* calls this (test/helpers/test-setup.ts, preloaded by `npm run test:fork`), so
|
|
82
|
+
* every session a test creates reads a fixed catalogue instead of the host CLI.
|
|
83
|
+
* Pass `undefined` to restore the live read. Production never calls it.
|
|
84
|
+
*/
|
|
85
|
+
export declare function setDefaultModelCatalogSource(source: SupportedModelsSource | undefined): void;
|
|
86
|
+
/**
|
|
87
|
+
* A prompt stream that never yields a message and ends only when `signal` aborts.
|
|
88
|
+
*
|
|
89
|
+
* This is what keeps the catalogue read INERT. `query({ prompt: "" })` does not
|
|
90
|
+
* mean "no prompt": the SDK submits the empty string as a real user turn, the
|
|
91
|
+
* model answers it, and that answer is billed. Measured 2026-10-14: the session
|
|
92
|
+
* transcript held a user record with a `promptId`, and 51 Opus responses were
|
|
93
|
+
* billed across one day's test runs. With streaming input and no message, the
|
|
94
|
+
* CLI starts, answers control requests such as `supportedModels()`, and never
|
|
95
|
+
* has a turn to run.
|
|
96
|
+
*/
|
|
97
|
+
export declare function silentPrompt(signal: AbortSignal): AsyncIterable<SDKUserMessage>;
|
|
98
|
+
/**
|
|
99
|
+
* The SDK options of the catalogue read, and why each one is there.
|
|
100
|
+
*
|
|
101
|
+
* - `settingSources: ["user"]` — the user's own settings still shape the list
|
|
102
|
+
* (`availableModels`, `env`, a model override), so the picker matches what
|
|
103
|
+
* their CLI would offer.
|
|
104
|
+
* - `settings: { disableAllHooks: true }` — the inline settings are the flag
|
|
105
|
+
* layer, which outranks the user file, so the user's hooks (SessionStart,
|
|
106
|
+
* SessionEnd, statusLine…) do not run for a read that is not a session.
|
|
107
|
+
* Without it, every catalogue read fired them like a real session.
|
|
108
|
+
* - `strictMcpConfig` with no `mcpServers` — no MCP server is started.
|
|
109
|
+
* - `persistSession: false` — nothing is written under `~/.claude/projects/`.
|
|
110
|
+
*/
|
|
111
|
+
export declare function catalogQueryOptions(pathToClaudeCodeExecutable: string): Options;
|
|
112
|
+
/** The slice of the SDK's `query` the catalogue read uses; injectable so tests need no spawn. */
|
|
113
|
+
export type CatalogQueryFn = (params: {
|
|
114
|
+
prompt: AsyncIterable<SDKUserMessage>;
|
|
115
|
+
options: Options;
|
|
116
|
+
}) => Pick<Query, "supportedModels" | "close">;
|
|
117
|
+
/**
|
|
118
|
+
* Read the model list from a CLI started with {@link catalogQueryOptions} and
|
|
119
|
+
* {@link silentPrompt}, then close it. `supportedModels()` is a control request,
|
|
120
|
+
* not a turn: with no message on the prompt stream, no model call is made and
|
|
121
|
+
* no quota is used.
|
|
122
|
+
*/
|
|
123
|
+
export declare function readSupportedModels(queryFn: CatalogQueryFn, pathToClaudeCodeExecutable: string): Promise<ModelInfo[]>;
|
|
46
124
|
/**
|
|
47
125
|
* Rewrite the `default` row's description to NAME the model it resolves to.
|
|
48
126
|
*
|
|
@@ -73,7 +151,7 @@ export declare function nameDefaultRow(models: ModelInfo[]): ModelInfo[];
|
|
|
73
151
|
* for the rest of the process.
|
|
74
152
|
*
|
|
75
153
|
* @param logger - Where a fallback is reported. Optional, for library callers.
|
|
76
|
-
* @param source - Test seam; defaults to spawning the PATH `claude
|
|
154
|
+
* @param source - Test seam; defaults to spawning the PATH `claude` (see {@link defaultSource}).
|
|
77
155
|
*/
|
|
78
156
|
export declare function resolveModelCatalog(logger?: CatalogLogger, source?: SupportedModelsSource): Promise<ModelInfo[]>;
|
|
79
157
|
export {};
|
|
@@ -47,20 +47,120 @@ export function resetLiveModelCatalogCache() {
|
|
|
47
47
|
fallbackReported = false;
|
|
48
48
|
}
|
|
49
49
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
50
|
+
* Environment variable that selects where the default source reads from.
|
|
51
|
+
* `builtin` serves {@link MODEL_CATALOG} without spawning anything — an offline
|
|
52
|
+
* knob for production, and what the test harness sets on every adapter it spawns
|
|
53
|
+
* as a child process. Unset (or any other value) means the live CLI read.
|
|
53
54
|
*/
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
55
|
+
export const MODEL_CATALOG_ENV = "CLAUDE_TUI_MODEL_CATALOG";
|
|
56
|
+
/**
|
|
57
|
+
* Opt-in for a test that genuinely needs the live CLI read. Without it, the live
|
|
58
|
+
* source refuses to run under the node test runner — see {@link LiveCatalogInTestsError}.
|
|
59
|
+
*/
|
|
60
|
+
export const LIVE_CATALOG_TESTS_ENV = "CLAUDE_TUI_LIVE_CATALOG_TESTS";
|
|
61
|
+
/**
|
|
62
|
+
* Raised when the LIVE source is reached from inside `node --test` without the
|
|
63
|
+
* {@link LIVE_CATALOG_TESTS_ENV} opt-in.
|
|
64
|
+
*
|
|
65
|
+
* WHY IT EXISTS. Each test file runs in its own process with an empty cache, and
|
|
66
|
+
* most suites create sessions without injecting a catalogue. Before this guard,
|
|
67
|
+
* one `npm run test:fork` spawned ~50 real CLI sessions — each loading the
|
|
68
|
+
* user's hooks, plugins and MCP servers — and five runs made 248 SDK sessions in
|
|
69
|
+
* 17 minutes (measured 2026-10-14). The test setup module installs a stub as the
|
|
70
|
+
* default source; this error is the tripwire for anything that bypasses it.
|
|
71
|
+
*
|
|
72
|
+
* {@link resolveModelCatalog} rethrows it instead of falling back, so the test
|
|
73
|
+
* that reached the live read FAILS rather than passing on the built-in list.
|
|
74
|
+
* `NODE_TEST_CONTEXT` is set only by node's test runner, so production never
|
|
75
|
+
* sees this.
|
|
76
|
+
*/
|
|
77
|
+
export class LiveCatalogInTestsError extends Error {
|
|
78
|
+
constructor() {
|
|
79
|
+
super("the live model catalogue (a real `claude` spawn) was requested under the node test runner. " +
|
|
80
|
+
"Run tests through `npm run test:fork` (it preloads test/helpers/test-setup.ts, which installs " +
|
|
81
|
+
"the stub), inject `deps.modelCatalogSource`, or opt in with " +
|
|
82
|
+
`${LIVE_CATALOG_TESTS_ENV}=1 for a test that truly needs the CLI.`);
|
|
83
|
+
this.name = "LiveCatalogInTestsError";
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** Throws {@link LiveCatalogInTestsError} under `node --test` unless the live opt-in is set. */
|
|
87
|
+
export function assertLiveCatalogAllowed(env = process.env) {
|
|
88
|
+
if (env.NODE_TEST_CONTEXT && env[LIVE_CATALOG_TESTS_ENV] !== "1") {
|
|
89
|
+
throw new LiveCatalogInTestsError();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** Set by {@link setDefaultModelCatalogSource}; `undefined` means the live CLI read. */
|
|
93
|
+
let defaultOverride;
|
|
94
|
+
/**
|
|
95
|
+
* Replace the source used when a caller passes none. Only the test setup module
|
|
96
|
+
* calls this (test/helpers/test-setup.ts, preloaded by `npm run test:fork`), so
|
|
97
|
+
* every session a test creates reads a fixed catalogue instead of the host CLI.
|
|
98
|
+
* Pass `undefined` to restore the live read. Production never calls it.
|
|
99
|
+
*/
|
|
100
|
+
export function setDefaultModelCatalogSource(source) {
|
|
101
|
+
defaultOverride = source;
|
|
102
|
+
cache = null;
|
|
103
|
+
}
|
|
104
|
+
/** The source a caller without its own gets: the test override, or the live read. */
|
|
105
|
+
function defaultSource() {
|
|
106
|
+
return defaultOverride ? defaultOverride() : liveSource();
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* A prompt stream that never yields a message and ends only when `signal` aborts.
|
|
110
|
+
*
|
|
111
|
+
* This is what keeps the catalogue read INERT. `query({ prompt: "" })` does not
|
|
112
|
+
* mean "no prompt": the SDK submits the empty string as a real user turn, the
|
|
113
|
+
* model answers it, and that answer is billed. Measured 2026-10-14: the session
|
|
114
|
+
* transcript held a user record with a `promptId`, and 51 Opus responses were
|
|
115
|
+
* billed across one day's test runs. With streaming input and no message, the
|
|
116
|
+
* CLI starts, answers control requests such as `supportedModels()`, and never
|
|
117
|
+
* has a turn to run.
|
|
118
|
+
*/
|
|
119
|
+
export function silentPrompt(signal) {
|
|
120
|
+
const ended = new Promise((resolve) => {
|
|
121
|
+
const end = () => resolve({ value: undefined, done: true });
|
|
122
|
+
if (signal.aborted)
|
|
123
|
+
end();
|
|
124
|
+
else
|
|
125
|
+
signal.addEventListener("abort", end, { once: true });
|
|
126
|
+
});
|
|
127
|
+
// Every `next()` waits for the abort and then reports the end: no message is ever produced.
|
|
128
|
+
return { [Symbol.asyncIterator]: () => ({ next: () => ended }) };
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* The SDK options of the catalogue read, and why each one is there.
|
|
132
|
+
*
|
|
133
|
+
* - `settingSources: ["user"]` — the user's own settings still shape the list
|
|
134
|
+
* (`availableModels`, `env`, a model override), so the picker matches what
|
|
135
|
+
* their CLI would offer.
|
|
136
|
+
* - `settings: { disableAllHooks: true }` — the inline settings are the flag
|
|
137
|
+
* layer, which outranks the user file, so the user's hooks (SessionStart,
|
|
138
|
+
* SessionEnd, statusLine…) do not run for a read that is not a session.
|
|
139
|
+
* Without it, every catalogue read fired them like a real session.
|
|
140
|
+
* - `strictMcpConfig` with no `mcpServers` — no MCP server is started.
|
|
141
|
+
* - `persistSession: false` — nothing is written under `~/.claude/projects/`.
|
|
142
|
+
*/
|
|
143
|
+
export function catalogQueryOptions(pathToClaudeCodeExecutable) {
|
|
144
|
+
return {
|
|
145
|
+
pathToClaudeCodeExecutable,
|
|
146
|
+
settingSources: ["user"],
|
|
147
|
+
settings: { disableAllHooks: true },
|
|
148
|
+
strictMcpConfig: true,
|
|
149
|
+
mcpServers: {},
|
|
150
|
+
persistSession: false,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Read the model list from a CLI started with {@link catalogQueryOptions} and
|
|
155
|
+
* {@link silentPrompt}, then close it. `supportedModels()` is a control request,
|
|
156
|
+
* not a turn: with no message on the prompt stream, no model call is made and
|
|
157
|
+
* no quota is used.
|
|
158
|
+
*/
|
|
159
|
+
export async function readSupportedModels(queryFn, pathToClaudeCodeExecutable) {
|
|
160
|
+
const stop = new AbortController();
|
|
161
|
+
const q = queryFn({
|
|
162
|
+
prompt: silentPrompt(stop.signal),
|
|
163
|
+
options: catalogQueryOptions(pathToClaudeCodeExecutable),
|
|
64
164
|
});
|
|
65
165
|
try {
|
|
66
166
|
return await q.supportedModels();
|
|
@@ -68,13 +168,21 @@ async function defaultSource() {
|
|
|
68
168
|
finally {
|
|
69
169
|
// Never let a failed teardown mask a successful read.
|
|
70
170
|
try {
|
|
71
|
-
q.
|
|
171
|
+
q.close();
|
|
72
172
|
}
|
|
73
173
|
catch {
|
|
74
174
|
/* the process is going away regardless */
|
|
75
175
|
}
|
|
176
|
+
// Release the prompt stream, so nothing waits on it after the CLI is gone.
|
|
177
|
+
stop.abort();
|
|
76
178
|
}
|
|
77
179
|
}
|
|
180
|
+
/** The production source: the PATH `claude`, read inertly. See {@link readSupportedModels}. */
|
|
181
|
+
async function liveSource() {
|
|
182
|
+
assertLiveCatalogAllowed();
|
|
183
|
+
const { query } = await import("@anthropic-ai/claude-agent-sdk");
|
|
184
|
+
return readSupportedModels(query, resolveClaudePath());
|
|
185
|
+
}
|
|
78
186
|
/** Reject after {@link CATALOG_TIMEOUT_MS} so a hung control request cannot stall session creation. */
|
|
79
187
|
function withTimeout(promise, ms) {
|
|
80
188
|
return new Promise((resolve, reject) => {
|
|
@@ -126,10 +234,14 @@ export function nameDefaultRow(models) {
|
|
|
126
234
|
* for the rest of the process.
|
|
127
235
|
*
|
|
128
236
|
* @param logger - Where a fallback is reported. Optional, for library callers.
|
|
129
|
-
* @param source - Test seam; defaults to spawning the PATH `claude
|
|
237
|
+
* @param source - Test seam; defaults to spawning the PATH `claude` (see {@link defaultSource}).
|
|
130
238
|
*/
|
|
131
239
|
export async function resolveModelCatalog(logger, source = defaultSource) {
|
|
132
|
-
|
|
240
|
+
// The offline knob: the built-in list, served as-is, with nothing spawned.
|
|
241
|
+
if (source === defaultSource && !defaultOverride && process.env[MODEL_CATALOG_ENV] === "builtin")
|
|
242
|
+
return MODEL_CATALOG;
|
|
243
|
+
// Keyed by source, so an injected catalogue never answers for the CLI and vice versa (item 93).
|
|
244
|
+
if (cache && cache.source === source && Date.now() - cache.at < CATALOG_TTL_MS)
|
|
133
245
|
return cache.models;
|
|
134
246
|
try {
|
|
135
247
|
const raw = await withTimeout(source(), CATALOG_TIMEOUT_MS);
|
|
@@ -141,10 +253,13 @@ export async function resolveModelCatalog(logger, source = defaultSource) {
|
|
|
141
253
|
// was empty — already excluded above. Guard anyway: an empty picker is worse
|
|
142
254
|
// than a stale one, and this function must never return one.
|
|
143
255
|
const resolved = models.length > 0 ? models : raw;
|
|
144
|
-
cache = { models: resolved, at: Date.now() };
|
|
256
|
+
cache = { models: resolved, at: Date.now(), source };
|
|
145
257
|
return resolved;
|
|
146
258
|
}
|
|
147
259
|
catch (error) {
|
|
260
|
+
// The test tripwire must fail the test that reached it, not degrade quietly.
|
|
261
|
+
if (error instanceof LiveCatalogInTestsError)
|
|
262
|
+
throw error;
|
|
148
263
|
// `log`, NOT `error`, and ONCE per process. Both halves are deliberate.
|
|
149
264
|
//
|
|
150
265
|
// Level: this path is degraded-but-recovered — the picker still works, from
|
package/dist/model-catalog.d.ts
CHANGED
|
@@ -2,21 +2,10 @@ import type { ModelInfo } from "@anthropic-ai/claude-agent-sdk";
|
|
|
2
2
|
import type { ClientCapabilities, SessionConfigOption } from "@agentclientprotocol/sdk";
|
|
3
3
|
/**
|
|
4
4
|
* Effort levels offered for reasoning-capable models — the SDK's `supportedEffortLevels` vocabulary.
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* Exactly the real `--effort` enum. Ultracode is NOT a level (item 84): it is its own switch, see
|
|
6
|
+
* {@link ULTRACODE_CONFIG_ID}.
|
|
7
7
|
*/
|
|
8
8
|
export declare const REASONING_EFFORT_LEVELS: NonNullable<ModelInfo["supportedEffortLevels"]>;
|
|
9
|
-
/**
|
|
10
|
-
* Story 060 — the "ultracode" pseudo-level sentinel for the effort selector. NOT a real `--effort`
|
|
11
|
-
* value (claude 2.1.195 rejects `--effort ultracode`; the enum stays {@link REASONING_EFFORT_LEVELS}).
|
|
12
|
-
* Selecting it maps to `--effort xhigh` PLUS activating the ultracode keyword-trigger for the session
|
|
13
|
-
* (story 060 Task 3). Kept OUT of REASONING_EFFORT_LEVELS so no real-effort code path ever emits it.
|
|
14
|
-
*/
|
|
15
|
-
export declare const ULTRACODE_EFFORT = "ultracode";
|
|
16
|
-
/** The real effort level the ultracode pseudo-level maps to (its documented effort component). */
|
|
17
|
-
export declare const ULTRACODE_EFFORT_LEVEL = "xhigh";
|
|
18
|
-
/** Selector label — makes clear it is xhigh + orchestration, NOT a duplicate of the plain `xhigh` entry. */
|
|
19
|
-
export declare const ULTRACODE_EFFORT_LABEL = "ultracode (xhigh + orchestration)";
|
|
20
9
|
/**
|
|
21
10
|
* The FALLBACK model list — no longer what the picker normally shows.
|
|
22
11
|
*
|
|
@@ -52,18 +41,20 @@ export declare const ULTRACODE_EFFORT_LABEL = "ultracode (xhigh + orchestration)
|
|
|
52
41
|
* A tier alias always points at the newest member of its family, so these rows do NOT age out on a
|
|
53
42
|
* model launch — only the version text in the row does. Verified 2026-09-23:
|
|
54
43
|
* `opus` → `claude-opus-5-5`, `fable` → `claude-fable-5-1`, `sonnet` → `claude-sonnet-5`,
|
|
55
|
-
* `haiku` → `claude-haiku-4-5-20251001`.
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* `
|
|
44
|
+
* `haiku` → `claude-haiku-4-5-20251001`. Re-read 2026-10-14 from CLI 2.1.299's live
|
|
45
|
+
* `supportedModels()`: `sonnet` → `claude-sonnet-5-5`, `haiku` → `claude-haiku-5-5` (both moved in
|
|
46
|
+
* CLI 2.1.293).
|
|
47
|
+
*
|
|
48
|
+
* Every row is effort-capable (`supportsEffort` + `supportedEffortLevels`) and carries
|
|
49
|
+
* `supportsAutoMode: true`, which surfaces the `auto` permission mode (a model classifier). `haiku`
|
|
50
|
+
* used to advertise neither: Haiku 4.5 had no effort levels and the original drops `auto` on it.
|
|
51
|
+
* Haiku 5.5 has both, per the live CLI rows.
|
|
61
52
|
*/
|
|
62
53
|
export declare const MODEL_CATALOG: ModelInfo[];
|
|
63
54
|
/**
|
|
64
55
|
* Story 068 (R1, R1.1, R2) — the REAL per-alias context window, keyed by the EXACT {@link MODEL_CATALOG}
|
|
65
|
-
* `value`.
|
|
66
|
-
* is
|
|
56
|
+
* `value`. Every alias is natively 1M today; `haiku` was 200K until it moved to Haiku 5.5 (item 93).
|
|
57
|
+
* Older generations are NOT uniform, which is why {@link MODEL_ID_CONTEXT_WINDOWS} refines per turn. This map is the single source of truth that `inferContextWindowFromModel` (acp-agent.ts)
|
|
67
58
|
* consults BEFORE the `\b1m\b` regex fallback — the bug it fixes is `opus` having wrongly reported 200K
|
|
68
59
|
* (the regex only ever matched the literal `1m` token).
|
|
69
60
|
*
|
|
@@ -185,5 +176,50 @@ export type FastModeSignal = "available" | "unavailable" | "pending";
|
|
|
185
176
|
* `pending`, and the caller fails closed on timeout (R1.2).
|
|
186
177
|
*/
|
|
187
178
|
export declare function classifyFastModeSignal(raw: string): FastModeSignal;
|
|
179
|
+
/** Item 84 — the Ultracode config-option id. */
|
|
180
|
+
export declare const ULTRACODE_CONFIG_ID = "ultracode";
|
|
181
|
+
/**
|
|
182
|
+
* Item 84 — the CLI command that turns Ultracode on or off for the session. Always the explicit
|
|
183
|
+
* `on|off` form: never a bare `/effort ultracode`, whose meaning the CLI has changed between builds.
|
|
184
|
+
*/
|
|
185
|
+
export declare function ultracodeCommand(on: boolean): string;
|
|
186
|
+
/**
|
|
187
|
+
* Item 84 — can this model run Ultracode? The CLI's own gate is "workflows are enabled AND the model
|
|
188
|
+
* supports xhigh"; the first half is not knowable from here (managed settings, org policy, env), so
|
|
189
|
+
* this checks the second and the CLI's refusal reply ({@link classifyUltracodeReply}) covers the rest.
|
|
190
|
+
*/
|
|
191
|
+
export declare function isUltracodeCapableModel(info: ModelInfo | undefined): boolean;
|
|
192
|
+
/** Item 84 — the switch is on while this is true; a boolean payload or the on/off select string. */
|
|
193
|
+
export declare function resolveUltracodeEnabled(value: unknown): boolean;
|
|
194
|
+
/**
|
|
195
|
+
* Item 84 — build the `ultracode` config option: `type:"boolean"` when the client advertised boolean
|
|
196
|
+
* config options, else an on/off select, exactly as {@link createFastModeConfigOption} does. When the
|
|
197
|
+
* CLI refused the last attempt, `refusal` is appended to the description so the reason the switch
|
|
198
|
+
* went back off is visible where the switch is.
|
|
199
|
+
*/
|
|
200
|
+
export declare function createUltracodeConfigOption(enabled: boolean, useBooleanOption: boolean, refusal?: string): SessionConfigOption;
|
|
201
|
+
/**
|
|
202
|
+
* Item 84 — what the CLI answered to `/effort ultracode on`:
|
|
203
|
+
* - `refused` — one of its two refusals; `reason` is the sentence to show the user.
|
|
204
|
+
* - `on` — "Ultracode on (this session only): …".
|
|
205
|
+
* - `pending` — neither yet; keep reading until the caller's window closes.
|
|
206
|
+
*/
|
|
207
|
+
export type UltracodeReply = {
|
|
208
|
+
kind: "refused";
|
|
209
|
+
reason: string;
|
|
210
|
+
} | {
|
|
211
|
+
kind: "on";
|
|
212
|
+
} | {
|
|
213
|
+
kind: "pending";
|
|
214
|
+
};
|
|
215
|
+
/**
|
|
216
|
+
* Item 84 — classify the CLI's reply to `/effort ultracode on`. The two refusals are the CLI's own
|
|
217
|
+
* strings (claude 2.1.299, `/effort` handler):
|
|
218
|
+
* `Ultracode needs dynamic workflows enabled (see /config). Valid options are: …`
|
|
219
|
+
* `Ultracode isn't available on <model>. Valid options are: …`
|
|
220
|
+
* and its confirmation is `Ultracode on (this session only): …`. When the captured text holds more
|
|
221
|
+
* than one (a repaint can carry an older answer), the one written LAST is the answer to this command.
|
|
222
|
+
*/
|
|
223
|
+
export declare function classifyUltracodeReply(raw: string): UltracodeReply;
|
|
188
224
|
/** The safe fallback entry, kept as a named export so callers can seed/anchor on it without a lookup. */
|
|
189
225
|
export declare const DEFAULT_MODEL_INFO: ModelInfo;
|