@stage-labs/metro 0.1.0-beta.265 → 0.1.0-beta.267
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/package.json +1 -1
- package/runtime/marketplace/plugin/.claude-plugin/plugin.json +1 -1
- package/runtime/node_modules/@metro-labs/core/src/runner-activity.ts +13 -0
- package/runtime/runtime.json +1 -1
- package/runtime/sdk-runner/node_modules/@metro-labs/core/src/runner-activity.ts +13 -0
- package/runtime/sdk-runner/runner.json +1 -1
- package/runtime/sdk-runner/src/activity-tasks.ts +52 -7
- package/runtime/sdk-runner/src/activity.ts +11 -5
- package/runtime/sdk-runner/src/inbox.ts +4 -4
- package/runtime/sdk-runner/src/input-queue.ts +7 -2
- package/runtime/sdk-runner/src/runner.ts +6 -4
- package/runtime/sdk-runner/src/session-watch.ts +31 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "metro",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.267",
|
|
4
4
|
"description": "Metro for Claude Code, installed by the metro daemon on this machine: every connector the agent holds as its own MCP server relayed through the daemon, the orchestrator-only guard on the main thread, and the standing rules loaded at each session start. No vendor credential ever sits in a config file.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Bonustrack",
|
|
@@ -41,6 +41,8 @@ export interface RunnerTask {
|
|
|
41
41
|
id: string;
|
|
42
42
|
kind: string | null;
|
|
43
43
|
agent: string | null;
|
|
44
|
+
description?: string | null;
|
|
45
|
+
lastObservedModel?: string | null;
|
|
44
46
|
status: RunnerTaskState;
|
|
45
47
|
background: boolean;
|
|
46
48
|
startedAt: number;
|
|
@@ -116,12 +118,23 @@ const eventKind = (value: unknown): value is RunnerEventKind => EVENTS.some((e)
|
|
|
116
118
|
const name = (value: unknown): string | null => typeof value === 'string' && /^[A-Za-z0-9_.:-]{1,128}$/.test(value) ? value : null;
|
|
117
119
|
const sessionId = (value: unknown): string | null => typeof value === 'string' && /^[0-9a-f-]{36}$/.test(value) ? value : null;
|
|
118
120
|
|
|
121
|
+
export function runnerTaskDescription(value: unknown): string | null {
|
|
122
|
+
if (typeof value !== 'string') return null;
|
|
123
|
+
const line = value.split(/\r|\n|\p{Zl}|\p{Zp}/u, 1)[0] ?? '';
|
|
124
|
+
const description = line.replace(/[\p{Cc}\p{Cf}]/gu, ' ').replace(/\s+/g, ' ').trim().slice(0, 160).trim();
|
|
125
|
+
return description.length > 0 ? description : null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export const runnerTaskModel = (value: unknown): string | null => typeof value === 'string' && /^[A-Za-z0-9][A-Za-z0-9_.:/@+[\]-]{0,127}$/.test(value) ? value : null;
|
|
129
|
+
|
|
119
130
|
function task(raw: unknown): RunnerTask | null {
|
|
120
131
|
if (!isRecord(raw) || !name(raw.id) || !taskState(raw.status)) return null;
|
|
121
132
|
return {
|
|
122
133
|
id: String(raw.id), kind: name(raw.kind), agent: name(raw.agent), status: raw.status, background: raw.background === true,
|
|
123
134
|
startedAt: count(raw.startedAt), updatedAt: count(raw.updatedAt), endedAt: count(raw.endedAt) || null,
|
|
124
135
|
lastTool: name(raw.lastTool), toolUses: count(raw.toolUses), durationMs: count(raw.durationMs),
|
|
136
|
+
...(raw.description === undefined ? {} : { description: runnerTaskDescription(raw.description) }),
|
|
137
|
+
...(raw.lastObservedModel === undefined ? {} : { lastObservedModel: runnerTaskModel(raw.lastObservedModel) }),
|
|
125
138
|
};
|
|
126
139
|
}
|
|
127
140
|
|
package/runtime/runtime.json
CHANGED
|
@@ -41,6 +41,8 @@ export interface RunnerTask {
|
|
|
41
41
|
id: string;
|
|
42
42
|
kind: string | null;
|
|
43
43
|
agent: string | null;
|
|
44
|
+
description?: string | null;
|
|
45
|
+
lastObservedModel?: string | null;
|
|
44
46
|
status: RunnerTaskState;
|
|
45
47
|
background: boolean;
|
|
46
48
|
startedAt: number;
|
|
@@ -116,12 +118,23 @@ const eventKind = (value: unknown): value is RunnerEventKind => EVENTS.some((e)
|
|
|
116
118
|
const name = (value: unknown): string | null => typeof value === 'string' && /^[A-Za-z0-9_.:-]{1,128}$/.test(value) ? value : null;
|
|
117
119
|
const sessionId = (value: unknown): string | null => typeof value === 'string' && /^[0-9a-f-]{36}$/.test(value) ? value : null;
|
|
118
120
|
|
|
121
|
+
export function runnerTaskDescription(value: unknown): string | null {
|
|
122
|
+
if (typeof value !== 'string') return null;
|
|
123
|
+
const line = value.split(/\r|\n|\p{Zl}|\p{Zp}/u, 1)[0] ?? '';
|
|
124
|
+
const description = line.replace(/[\p{Cc}\p{Cf}]/gu, ' ').replace(/\s+/g, ' ').trim().slice(0, 160).trim();
|
|
125
|
+
return description.length > 0 ? description : null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export const runnerTaskModel = (value: unknown): string | null => typeof value === 'string' && /^[A-Za-z0-9][A-Za-z0-9_.:/@+[\]-]{0,127}$/.test(value) ? value : null;
|
|
129
|
+
|
|
119
130
|
function task(raw: unknown): RunnerTask | null {
|
|
120
131
|
if (!isRecord(raw) || !name(raw.id) || !taskState(raw.status)) return null;
|
|
121
132
|
return {
|
|
122
133
|
id: String(raw.id), kind: name(raw.kind), agent: name(raw.agent), status: raw.status, background: raw.background === true,
|
|
123
134
|
startedAt: count(raw.startedAt), updatedAt: count(raw.updatedAt), endedAt: count(raw.endedAt) || null,
|
|
124
135
|
lastTool: name(raw.lastTool), toolUses: count(raw.toolUses), durationMs: count(raw.durationMs),
|
|
136
|
+
...(raw.description === undefined ? {} : { description: runnerTaskDescription(raw.description) }),
|
|
137
|
+
...(raw.lastObservedModel === undefined ? {} : { lastObservedModel: runnerTaskModel(raw.lastObservedModel) }),
|
|
125
138
|
};
|
|
126
139
|
}
|
|
127
140
|
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import type { SDKMessage } from '@anthropic-ai/claude-agent-sdk';
|
|
2
|
-
import
|
|
2
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
3
|
+
import { runnerTaskDescription, runnerTaskModel, type RunnerEventKind, type RunnerTask, type RunnerTaskState } from '@metro-labs/core/runner-activity';
|
|
3
4
|
|
|
4
5
|
export const activityName = (name: unknown): string | null => typeof name === 'string' && /^[A-Za-z0-9_.:-]{1,128}$/.test(name) ? name : null;
|
|
5
6
|
const live = (task: RunnerTask): boolean => ['running', 'pending', 'paused'].includes(task.status);
|
|
6
7
|
const terminal = (status: RunnerTaskState): boolean => ['completed', 'failed', 'stopped'].includes(status);
|
|
7
8
|
type System = Extract<SDKMessage, { type: 'system' }>;
|
|
8
9
|
type Emit = (kind: RunnerEventKind, tool?: string | null, taskId?: string | null) => void;
|
|
10
|
+
type Metadata = Pick<RunnerTask, 'description' | 'lastObservedModel'>;
|
|
9
11
|
|
|
10
12
|
export class ActivityTasks {
|
|
11
13
|
private readonly tasks = new Map<string, RunnerTask>();
|
|
12
14
|
private readonly parents = new Map<string, string>();
|
|
13
15
|
private readonly hidden = new Map<string, string>();
|
|
16
|
+
private readonly pending = new Map<string, Metadata>();
|
|
14
17
|
constructor(private readonly emit: Emit) {}
|
|
15
18
|
|
|
16
19
|
snapshot(): RunnerTask[] {
|
|
@@ -34,6 +37,41 @@ export class ActivityTasks {
|
|
|
34
37
|
}
|
|
35
38
|
}
|
|
36
39
|
|
|
40
|
+
launch(id: string, input: unknown): void {
|
|
41
|
+
if (!isRecord(input)) return;
|
|
42
|
+
const description = runnerTaskDescription(input.description);
|
|
43
|
+
if (description !== null) this.metadata(id, { description });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
model(parent: string, value: unknown): void {
|
|
47
|
+
const lastObservedModel = runnerTaskModel(value);
|
|
48
|
+
if (lastObservedModel !== null) this.metadata(parent, { lastObservedModel });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private metadata(parent: string, fields: Metadata): void {
|
|
52
|
+
if (!activityName(parent) || this.isHidden(parent)) return;
|
|
53
|
+
const id = this.taskFor(parent);
|
|
54
|
+
const task = id === null ? undefined : this.tasks.get(id);
|
|
55
|
+
if (task !== undefined) { Object.assign(task, fields); return; }
|
|
56
|
+
const known = this.pending.get(parent);
|
|
57
|
+
this.pending.delete(parent);
|
|
58
|
+
const oldest = this.pending.keys().next();
|
|
59
|
+
if (this.pending.size >= 200 && !oldest.done) this.pending.delete(oldest.value);
|
|
60
|
+
this.pending.set(parent, { ...known, ...fields });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private link(parent: string, id: string): void {
|
|
64
|
+
this.parents.set(parent, id);
|
|
65
|
+
const task = this.tasks.get(id);
|
|
66
|
+
if (task !== undefined) Object.assign(task, this.pending.get(parent));
|
|
67
|
+
this.pending.delete(parent);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private describe(task: RunnerTask, value: unknown): void {
|
|
71
|
+
const description = runnerTaskDescription(value);
|
|
72
|
+
if (description !== null) task.description = description;
|
|
73
|
+
}
|
|
74
|
+
|
|
37
75
|
stop(): void {
|
|
38
76
|
for (const task of this.tasks.values()) if (live(task)) this.status(task, 'stopped');
|
|
39
77
|
}
|
|
@@ -61,6 +99,7 @@ export class ActivityTasks {
|
|
|
61
99
|
const oldest = this.hidden.keys().next();
|
|
62
100
|
if (this.hidden.size >= 400 && !oldest.done) this.hidden.delete(oldest.value);
|
|
63
101
|
this.hidden.set(key, id);
|
|
102
|
+
this.pending.delete(key);
|
|
64
103
|
}
|
|
65
104
|
}
|
|
66
105
|
|
|
@@ -71,7 +110,7 @@ export class ActivityTasks {
|
|
|
71
110
|
for (const [key, taskId] of this.hidden) {
|
|
72
111
|
if (taskId !== id) continue;
|
|
73
112
|
this.hidden.delete(key);
|
|
74
|
-
this.
|
|
113
|
+
this.link(key, id);
|
|
75
114
|
}
|
|
76
115
|
return task;
|
|
77
116
|
}
|
|
@@ -80,18 +119,20 @@ export class ActivityTasks {
|
|
|
80
119
|
if (message.ambient || message.skip_transcript) { this.hide(message.task_id, message.tool_use_id); return; }
|
|
81
120
|
const task = this.get(message.task_id);
|
|
82
121
|
if (!task) return;
|
|
83
|
-
task.kind = activityName(message.task_type);
|
|
84
|
-
task.agent = activityName(message.subagent_type);
|
|
122
|
+
task.kind = activityName(message.task_type) ?? task.kind;
|
|
123
|
+
task.agent = activityName(message.subagent_type) ?? task.agent;
|
|
85
124
|
task.background = message.is_backgrounded === true;
|
|
86
125
|
task.startedAt = Date.now();
|
|
87
126
|
task.endedAt = null;
|
|
88
|
-
if (message.tool_use_id) this.
|
|
127
|
+
if (message.tool_use_id) this.link(message.tool_use_id, task.id);
|
|
128
|
+
this.describe(task, message.description);
|
|
89
129
|
this.status(task, 'running');
|
|
90
130
|
}
|
|
91
131
|
|
|
92
132
|
private progress(message: Extract<System, { subtype: 'task_progress' }>): void {
|
|
93
133
|
const task = this.tasks.get(message.task_id);
|
|
94
134
|
if (!task || terminal(task.status)) return;
|
|
135
|
+
if (message.tool_use_id) this.link(message.tool_use_id, task.id);
|
|
95
136
|
task.lastTool = activityName(message.last_tool_name);
|
|
96
137
|
task.toolUses = message.usage.tool_uses;
|
|
97
138
|
task.durationMs = message.usage.duration_ms;
|
|
@@ -101,15 +142,17 @@ export class ActivityTasks {
|
|
|
101
142
|
private patch(message: Extract<System, { subtype: 'task_updated' }>): void {
|
|
102
143
|
const task = this.tasks.get(message.task_id);
|
|
103
144
|
if (!task) return;
|
|
145
|
+
this.describe(task, message.patch.description);
|
|
104
146
|
if (message.patch.is_backgrounded !== undefined) task.background = message.patch.is_backgrounded;
|
|
105
147
|
if (message.patch.status) this.status(task, message.patch.status === 'killed' ? 'stopped' : message.patch.status);
|
|
106
148
|
task.updatedAt = Date.now();
|
|
107
149
|
}
|
|
108
150
|
|
|
109
151
|
private end(message: Extract<System, { subtype: 'task_notification' }>): void {
|
|
110
|
-
if (message.ambient || message.skip_transcript) { this.hide(message.task_id); return; }
|
|
152
|
+
if (message.ambient || message.skip_transcript) { this.hide(message.task_id, message.tool_use_id); return; }
|
|
111
153
|
const task = this.get(message.task_id);
|
|
112
154
|
if (!task) return;
|
|
155
|
+
if (message.tool_use_id) this.link(message.tool_use_id, task.id);
|
|
113
156
|
if (message.usage) {
|
|
114
157
|
task.durationMs = message.usage.duration_ms;
|
|
115
158
|
task.toolUses = message.usage.tool_uses;
|
|
@@ -123,7 +166,9 @@ export class ActivityTasks {
|
|
|
123
166
|
for (const row of message.tasks) {
|
|
124
167
|
if (row.ambient) { this.hide(row.task_id); continue; }
|
|
125
168
|
const task = this.show(row.task_id);
|
|
126
|
-
if (!task
|
|
169
|
+
if (!task) continue;
|
|
170
|
+
this.describe(task, row.description);
|
|
171
|
+
if (terminal(task.status)) continue;
|
|
127
172
|
task.kind = activityName(row.task_type);
|
|
128
173
|
task.background = true;
|
|
129
174
|
this.status(task, 'running');
|
|
@@ -251,15 +251,21 @@ export class Activity {
|
|
|
251
251
|
private content(message: Extract<SDKMessage, { type: 'assistant' | 'user' }>): void {
|
|
252
252
|
const content = message.message.content;
|
|
253
253
|
const parent = message.parent_tool_use_id ?? null;
|
|
254
|
-
if (
|
|
255
|
-
this.turn();
|
|
256
|
-
if (message.error !== undefined) this.providerCode = message.error === 'unknown' ? null : PROVIDER_ERRORS[message.error];
|
|
257
|
-
}
|
|
254
|
+
if (message.type === 'assistant') this.assistant(message, parent);
|
|
258
255
|
if (Array.isArray(content)) for (const block of content) if (isRecord(block)) this.block(block, parent);
|
|
259
256
|
}
|
|
260
257
|
|
|
258
|
+
private assistant(message: Extract<SDKMessage, { type: 'assistant' }>, parent: string | null): void {
|
|
259
|
+
if (parent !== null) { this.tasks.model(parent, message.message.model); return; }
|
|
260
|
+
this.turn();
|
|
261
|
+
if (message.error !== undefined) this.providerCode = message.error === 'unknown' ? null : PROVIDER_ERRORS[message.error];
|
|
262
|
+
}
|
|
263
|
+
|
|
261
264
|
private block(block: Record<string, unknown>, parent: string | null): void {
|
|
262
|
-
if (block.type === 'tool_use' && typeof block.id === 'string')
|
|
265
|
+
if (block.type === 'tool_use' && typeof block.id === 'string') {
|
|
266
|
+
if (block.name === 'Agent' || block.name === 'Task') this.tasks.launch(block.id, block.input);
|
|
267
|
+
this.tool(block.id, block.name, parent);
|
|
268
|
+
}
|
|
263
269
|
else if (block.type === 'tool_result' && typeof block.tool_use_id === 'string') this.finish(block.tool_use_id, block.is_error === true, parent, block.content);
|
|
264
270
|
}
|
|
265
271
|
|
|
@@ -17,7 +17,7 @@ export interface Unanswered {
|
|
|
17
17
|
|
|
18
18
|
interface InboxHooks {
|
|
19
19
|
ready?(): boolean;
|
|
20
|
-
dispatch?(): void;
|
|
20
|
+
dispatch?(message: SDKUserMessage): void;
|
|
21
21
|
queue?(count: number, oldest: number | null): void;
|
|
22
22
|
input?: InputWatch;
|
|
23
23
|
}
|
|
@@ -94,8 +94,8 @@ export class Inbox implements AsyncIterable<SDKUserMessage> {
|
|
|
94
94
|
|
|
95
95
|
cancel(uuids: readonly string[]): void {
|
|
96
96
|
const ephemeral = uuids.filter((uuid) => this.kindOf(uuid) !== 'chat');
|
|
97
|
-
this.queue.cancel(new Set(ephemeral));
|
|
98
|
-
this.timing.finish(
|
|
97
|
+
const removed = this.queue.cancel(new Set(ephemeral));
|
|
98
|
+
this.timing.finish(removed, true);
|
|
99
99
|
this.notify();
|
|
100
100
|
}
|
|
101
101
|
|
|
@@ -121,7 +121,7 @@ export class Inbox implements AsyncIterable<SDKUserMessage> {
|
|
|
121
121
|
while (!this.closed) {
|
|
122
122
|
const next = (this.hooks.ready?.() ?? true) ? this.queue.take() : undefined;
|
|
123
123
|
if (next !== undefined) {
|
|
124
|
-
this.hooks.dispatch?.();
|
|
124
|
+
this.hooks.dispatch?.(next);
|
|
125
125
|
if (next.uuid !== undefined) this.timing.dispatch(next.uuid);
|
|
126
126
|
this.notify();
|
|
127
127
|
yield next;
|
|
@@ -55,11 +55,16 @@ export class InputQueue {
|
|
|
55
55
|
return undefined;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
cancel(uuids: ReadonlySet<string>):
|
|
58
|
+
cancel(uuids: ReadonlySet<string>): string[] {
|
|
59
|
+
const removed: string[] = [];
|
|
59
60
|
for (let index = this.items.length - 1; index >= 0; index -= 1) {
|
|
60
61
|
const uuid = this.items[index]?.message.uuid;
|
|
61
|
-
if (uuid !== undefined && uuids.has(uuid))
|
|
62
|
+
if (uuid !== undefined && uuids.has(uuid)) {
|
|
63
|
+
this.remove(index);
|
|
64
|
+
removed.push(uuid);
|
|
65
|
+
}
|
|
62
66
|
}
|
|
67
|
+
return removed;
|
|
63
68
|
}
|
|
64
69
|
|
|
65
70
|
private remove(index: number): Queued | undefined {
|
|
@@ -19,7 +19,9 @@ const MODEL_PINS: ReadonlySet<string> = new Set(['ANTHROPIC_MODEL', 'CLAUDE_CODE
|
|
|
19
19
|
|
|
20
20
|
export function runnerEnv(env: NodeJS.ProcessEnv): Record<string, string | undefined> {
|
|
21
21
|
const unpinned = Object.fromEntries(Object.entries(env).filter(([name]) => !MODEL_PINS.has(name)));
|
|
22
|
-
|
|
22
|
+
const terminals = new Set((env.CLAUDE_CODE_TERMINAL_MCP_TOOLS ?? '').split(',').map((name) => name.trim()).filter(Boolean));
|
|
23
|
+
terminals.add('mcp__metro__send');
|
|
24
|
+
return { [STARTUP_WAIT]: '0', ...unpinned, DISABLE_AUTOUPDATER: '1', CLAUDE_CODE_TERMINAL_MCP_TOOLS: [...terminals].join(',') };
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
export type ModelSettings = Parameters<Query['applyFlagSettings']>[0];
|
|
@@ -97,8 +99,8 @@ export class Runner {
|
|
|
97
99
|
constructor(private readonly parts: RunnerParts) {
|
|
98
100
|
this.watch = new SessionWatch(parts.readOnly);
|
|
99
101
|
this.inbox = new Inbox((unanswered) => { this.keep(unanswered); }, {
|
|
100
|
-
ready: () => this.watch.
|
|
101
|
-
dispatch: () => { this.watch.dispatched(); },
|
|
102
|
+
ready: () => this.watch.admitting,
|
|
103
|
+
dispatch: (message) => { this.watch.dispatched(message.uuid); },
|
|
102
104
|
input: (input) => { parts.activity?.input(input); },
|
|
103
105
|
queue: (count, oldest) => { parts.activity?.pending(count, oldest); },
|
|
104
106
|
});
|
|
@@ -241,7 +243,7 @@ export class Runner {
|
|
|
241
243
|
}
|
|
242
244
|
|
|
243
245
|
private maybeCompact(resuming = false): void {
|
|
244
|
-
const busy = this.compactAsked !== null || !this.watch.safe || (!resuming && (this.calls.live || this.inbox.pending > 0));
|
|
246
|
+
const busy = this.compactAsked !== null || !this.watch.safe || !this.watch.admitting || (!resuming && (this.calls.live || this.inbox.pending > 0));
|
|
245
247
|
if (!compactDue({ context: this.watch.context, floor: this.compactFloor, limit: this.parts.compactAt ?? COMPACT_AT, busy })) return;
|
|
246
248
|
this.compactAsked = this.inbox.compact();
|
|
247
249
|
log.info({ context: this.watch.context, resuming }, 'sdk-runner: compacting at a safe idle boundary');
|
|
@@ -22,7 +22,6 @@ function toolResultIds(message: unknown): string[] {
|
|
|
22
22
|
return message.content.filter(isRecord).filter((b) => b.type === 'tool_result' && typeof b.tool_use_id === 'string').map((b) => String(b.tool_use_id));
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
|
|
26
25
|
export class SessionWatch {
|
|
27
26
|
private readonly writes = new Set<string>();
|
|
28
27
|
private readonly idle: (() => void)[] = [];
|
|
@@ -30,6 +29,8 @@ export class SessionWatch {
|
|
|
30
29
|
private compactingNow = false;
|
|
31
30
|
private busy = false;
|
|
32
31
|
private sessionStateSeen = false;
|
|
32
|
+
private requiresAction = false;
|
|
33
|
+
private awaitingInput: string | null = null;
|
|
33
34
|
private lastContext = 0;
|
|
34
35
|
private streamUsage: Record<string, unknown> = {};
|
|
35
36
|
|
|
@@ -49,11 +50,20 @@ export class SessionWatch {
|
|
|
49
50
|
|
|
50
51
|
get safe(): boolean { return !this.busy && !this.writing && !this.compacting; }
|
|
51
52
|
|
|
52
|
-
|
|
53
|
+
get admitting(): boolean {
|
|
54
|
+
return this.awaitingInput === null && !this.requiresAction && !this.compacting;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
dispatched(uuid?: string): void {
|
|
58
|
+
this.busy = true;
|
|
59
|
+
this.awaitingInput = uuid ?? null;
|
|
60
|
+
}
|
|
61
|
+
|
|
53
62
|
restoreContext(context: number): void { this.lastContext = context; }
|
|
54
63
|
|
|
55
64
|
observe(m: Record<string, unknown>): void {
|
|
56
65
|
if (m.parent_tool_use_id !== null && m.parent_tool_use_id !== undefined) return;
|
|
66
|
+
this.acknowledge(m);
|
|
57
67
|
this.lifecycle(m);
|
|
58
68
|
if (m.type === 'assistant' && isRecord(m.message)) this.assistant(m.message);
|
|
59
69
|
else if (m.type === 'stream_event' && isRecord(m.event)) this.stream(m.event);
|
|
@@ -68,6 +78,11 @@ export class SessionWatch {
|
|
|
68
78
|
return this.compactingNow ? new Promise((resolve) => { this.compacted.push(resolve); }) : Promise.resolve();
|
|
69
79
|
}
|
|
70
80
|
|
|
81
|
+
private acknowledge(m: Record<string, unknown>): void {
|
|
82
|
+
if (this.awaitingInput === null) return;
|
|
83
|
+
if (startedCommand(m) === this.awaitingInput || uuidsOf(m)?.includes(this.awaitingInput)) this.awaitingInput = null;
|
|
84
|
+
}
|
|
85
|
+
|
|
71
86
|
private lifecycle(m: Record<string, unknown>): void {
|
|
72
87
|
if (m.type === 'system') this.system(m);
|
|
73
88
|
else if (m.type === 'result' && !this.sessionStateSeen) {
|
|
@@ -78,11 +93,8 @@ export class SessionWatch {
|
|
|
78
93
|
}
|
|
79
94
|
|
|
80
95
|
private system(m: Record<string, unknown>): void {
|
|
81
|
-
if (m.subtype === 'session_state_changed')
|
|
82
|
-
|
|
83
|
-
if (m.state === 'running' || m.state === 'requires_action') this.busy = true;
|
|
84
|
-
else if (m.state === 'idle') { this.busy = false; this.settle(); }
|
|
85
|
-
} else if (m.subtype === 'status') this.setCompacting(m.status === 'compacting');
|
|
96
|
+
if (m.subtype === 'session_state_changed') this.sessionState(m.state);
|
|
97
|
+
else if (m.subtype === 'status') this.setCompacting(m.status === 'compacting');
|
|
86
98
|
else if (m.subtype === 'compact_boundary') {
|
|
87
99
|
this.setCompacting(false);
|
|
88
100
|
this.lastContext = 0;
|
|
@@ -90,6 +102,18 @@ export class SessionWatch {
|
|
|
90
102
|
}
|
|
91
103
|
}
|
|
92
104
|
|
|
105
|
+
private sessionState(state: unknown): void {
|
|
106
|
+
this.sessionStateSeen = true;
|
|
107
|
+
if (state === 'running' || state === 'requires_action') {
|
|
108
|
+
this.busy = true;
|
|
109
|
+
this.requiresAction = state === 'requires_action';
|
|
110
|
+
} else if (state === 'idle') {
|
|
111
|
+
this.busy = false;
|
|
112
|
+
this.requiresAction = false;
|
|
113
|
+
this.settle();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
93
117
|
private stream(event: Record<string, unknown>): void {
|
|
94
118
|
if (event.type === 'message_start' && isRecord(event.message)) {
|
|
95
119
|
this.streamUsage = isRecord(event.message.usage) ? event.message.usage : {};
|