@stage-labs/metro 0.1.0-beta.259 → 0.1.0-beta.260

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 (79) hide show
  1. package/dist/agent.js +128 -0
  2. package/dist/background.js +19 -1
  3. package/dist/claude.js +11 -7
  4. package/dist/cli.js +8 -0
  5. package/dist/route.js +17 -9
  6. package/dist/runtime-install.js +3 -2
  7. package/package.json +1 -1
  8. package/runtime/marketplace/plugin/.claude-plugin/plugin.json +1 -1
  9. package/runtime/node_modules/@metro-labs/core/package.json +1 -0
  10. package/runtime/node_modules/@metro-labs/core/src/ids.ts +5 -1
  11. package/runtime/node_modules/@metro-labs/core/src/runner-activity.ts +103 -0
  12. package/runtime/node_modules/@metro-labs/daemon/src/agent-user/agent-fs.ts +3 -3
  13. package/runtime/node_modules/@metro-labs/daemon/src/agent-user/view.ts +1 -1
  14. package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +3 -2
  15. package/runtime/node_modules/@metro-labs/daemon/src/claude/api.ts +17 -120
  16. package/runtime/node_modules/@metro-labs/daemon/src/claude/runner-activity.ts +17 -0
  17. package/runtime/node_modules/@metro-labs/daemon/src/claude/runner-process.ts +50 -0
  18. package/runtime/node_modules/@metro-labs/daemon/src/claude/runner.ts +45 -0
  19. package/runtime/node_modules/@metro-labs/daemon/src/claude/session-continuity.ts +48 -0
  20. package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +90 -31
  21. package/runtime/node_modules/@metro-labs/daemon/src/claude/setup-api.ts +161 -0
  22. package/runtime/node_modules/@metro-labs/daemon/src/claude/setup-state.ts +18 -0
  23. package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +21 -32
  24. package/runtime/node_modules/@metro-labs/daemon/src/gateway/forward.ts +1 -0
  25. package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +12 -0
  26. package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +43 -5
  27. package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-store.ts +2 -0
  28. package/runtime/node_modules/@metro-labs/daemon/src/mcp/index.ts +15 -1
  29. package/runtime/node_modules/@metro-labs/daemon/src/routes/bearer.ts +4 -1
  30. package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +2 -1
  31. package/runtime/node_modules/@metro-labs/daemon/src/voice/approvals.ts +1 -6
  32. package/runtime/node_modules/@metro-labs/http/src/api-http.ts +1 -0
  33. package/runtime/runtime.json +1 -1
  34. package/runtime/sdk-runner/node_modules/@metro-labs/core/package.json +52 -0
  35. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
  36. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/events.ts +121 -0
  37. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/ids.ts +28 -0
  38. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/is-record.ts +2 -0
  39. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/lines.ts +78 -0
  40. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/log.ts +42 -0
  41. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/protocol.ts +227 -0
  42. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/runner-activity.ts +103 -0
  43. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/secure-fs.ts +74 -0
  44. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/station-names.ts +14 -0
  45. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/account-files.ts +41 -0
  46. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/account-store.ts +83 -0
  47. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/attachments.ts +195 -0
  48. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/mail-text.ts +72 -0
  49. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/mail-trust.ts +29 -0
  50. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/mail.ts +212 -0
  51. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +16 -0
  52. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/oauth.ts +126 -0
  53. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/profile.ts +58 -0
  54. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/sender-profile.ts +64 -0
  55. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/station-runtime.ts +59 -0
  56. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/train-boot.ts +42 -0
  57. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/train-events.ts +84 -0
  58. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/stations/types.ts +125 -0
  59. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/str.ts +5 -0
  60. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/tickets.ts +37 -0
  61. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/train-error.ts +53 -0
  62. package/runtime/sdk-runner/node_modules/@metro-labs/core/src/version.ts +2 -0
  63. package/runtime/sdk-runner/runner.json +10 -0
  64. package/runtime/sdk-runner/src/activity-tasks.ts +142 -0
  65. package/runtime/sdk-runner/src/activity.ts +293 -0
  66. package/runtime/sdk-runner/src/app.ts +77 -0
  67. package/runtime/sdk-runner/src/approvals.ts +28 -0
  68. package/runtime/sdk-runner/src/channel-text.ts +26 -0
  69. package/runtime/sdk-runner/src/config.ts +50 -0
  70. package/runtime/sdk-runner/src/inbox.ts +94 -0
  71. package/runtime/sdk-runner/src/link.ts +130 -0
  72. package/runtime/sdk-runner/src/main.ts +44 -0
  73. package/runtime/sdk-runner/src/rules.ts +11 -0
  74. package/runtime/sdk-runner/src/runner.ts +251 -0
  75. package/runtime/sdk-runner/src/session-store.ts +71 -0
  76. package/runtime/sdk-runner/src/session-watch.ts +111 -0
  77. package/runtime/sdk-runner/src/singleton.ts +15 -0
  78. package/runtime/sdk-runner/src/speech.ts +79 -0
  79. package/runtime/sdk-runner/src/tool-proxy.ts +40 -0
@@ -0,0 +1,111 @@
1
+ import { isRecord } from '@metro-labs/core/is-record';
2
+
3
+ const METRO_TOOL = /^mcp__metro__(.+)$/;
4
+ const GATE_CAP_MS = 5_000;
5
+ const COMPACT_CAP_MS = 20_000;
6
+
7
+ export const uuidsOf = (m: Record<string, unknown>): string[] | null => {
8
+ if (Array.isArray(m.user_message_uuids)) return m.user_message_uuids.filter((u): u is string => typeof u === 'string');
9
+ return typeof m.user_message_uuid === 'string' ? [m.user_message_uuid] : null;
10
+ };
11
+
12
+ export const startedCommand = (m: Record<string, unknown>): string | null =>
13
+ m.type === 'command_lifecycle' && m.state === 'started' && typeof m.command_uuid === 'string' ? m.command_uuid : null;
14
+
15
+ const num = (value: unknown): number => (typeof value === 'number' ? value : 0);
16
+
17
+ function contextOf(usage: unknown): number {
18
+ if (!isRecord(usage)) return 0;
19
+ return num(usage.input_tokens) + num(usage.cache_read_input_tokens) + num(usage.cache_creation_input_tokens);
20
+ }
21
+
22
+ function toolResultIds(message: unknown): string[] {
23
+ if (!isRecord(message) || !Array.isArray(message.content)) return [];
24
+ return message.content.filter(isRecord).filter((b) => b.type === 'tool_result' && typeof b.tool_use_id === 'string').map((b) => String(b.tool_use_id));
25
+ }
26
+
27
+ function capped(waiters: (() => void)[], capMs: number): Promise<void> {
28
+ return new Promise((resolve) => {
29
+ const timer = setTimeout(resolve, capMs);
30
+ waiters.push(() => {
31
+ clearTimeout(timer);
32
+ resolve();
33
+ });
34
+ });
35
+ }
36
+
37
+ export class SessionWatch {
38
+ private readonly writes = new Set<string>();
39
+ private readonly idle: (() => void)[] = [];
40
+ private readonly compacted: (() => void)[] = [];
41
+ private compactingNow = false;
42
+ private lastContext = 0;
43
+
44
+ constructor(private readonly readOnly: (tool: string) => boolean) {}
45
+
46
+ get compacting(): boolean {
47
+ return this.compactingNow;
48
+ }
49
+
50
+ get context(): number {
51
+ return this.lastContext;
52
+ }
53
+
54
+ get writing(): boolean {
55
+ return this.writes.size > 0;
56
+ }
57
+
58
+ observe(m: Record<string, unknown>): void {
59
+ if (m.type === 'system') this.system(m);
60
+ else if (m.type === 'result') this.settle();
61
+ if (m.parent_tool_use_id !== null) return;
62
+ if (m.type === 'assistant' && isRecord(m.message)) this.assistant(m.message);
63
+ else if (m.type === 'user') for (const id of toolResultIds(m.message)) this.done(id);
64
+ }
65
+
66
+ whenNotWriting(capMs = GATE_CAP_MS): Promise<void> {
67
+ return this.writing ? capped(this.idle, capMs) : Promise.resolve();
68
+ }
69
+
70
+ whenNotCompacting(capMs = COMPACT_CAP_MS): Promise<void> {
71
+ return this.compactingNow ? capped(this.compacted, capMs) : Promise.resolve();
72
+ }
73
+
74
+ private system(m: Record<string, unknown>): void {
75
+ if (m.subtype === 'init') this.settle();
76
+ else if (m.subtype === 'status') this.setCompacting(m.status === 'compacting');
77
+ else if (m.subtype === 'compact_boundary') {
78
+ this.setCompacting(false);
79
+ this.lastContext = 0;
80
+ }
81
+ }
82
+
83
+ private setCompacting(on: boolean): void {
84
+ this.compactingNow = on;
85
+ if (!on) for (const resume of this.compacted.splice(0)) resume();
86
+ }
87
+
88
+ private assistant(message: Record<string, unknown>): void {
89
+ this.lastContext = contextOf(message.usage) || this.lastContext;
90
+ if (!Array.isArray(message.content)) return;
91
+ for (const block of message.content.filter(isRecord)) {
92
+ if (block.type !== 'tool_use' || typeof block.id !== 'string' || typeof block.name !== 'string') continue;
93
+ const tool = METRO_TOOL.exec(block.name)?.[1];
94
+ if (tool !== undefined && !this.readOnly(tool)) this.writes.add(block.id);
95
+ }
96
+ }
97
+
98
+ private done(id: string): void {
99
+ if (this.writes.delete(id) && this.writes.size === 0) this.wake();
100
+ }
101
+
102
+ private settle(): void {
103
+ if (this.writes.size === 0) return;
104
+ this.writes.clear();
105
+ this.wake();
106
+ }
107
+
108
+ private wake(): void {
109
+ for (const resume of this.idle.splice(0)) resume();
110
+ }
111
+ }
@@ -0,0 +1,15 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { createServer } from 'node:net';
3
+
4
+ export async function claimRunner(home: string): Promise<() => Promise<void>> {
5
+ const hash = createHash('sha256').update(home).digest('hex');
6
+ const server = createServer((socket) => { socket.destroy(); });
7
+ const address = process.platform === 'linux' ? { path: `\0metro-sdk-${hash}` } : { host: '127.0.0.1', port: 30_000 + parseInt(hash.slice(0, 4), 16) % 30_000 };
8
+ await new Promise<void>((resolve, reject) => {
9
+ server.once('error', () => { reject(new Error('The Agent SDK runner is already running, or its local process lock is unavailable.')); });
10
+ server.listen(address, resolve);
11
+ });
12
+ return () => new Promise<void>((resolve, reject) => {
13
+ server.close((err) => { if (err === undefined) resolve(); else reject(err); });
14
+ });
15
+ }
@@ -0,0 +1,79 @@
1
+ import type { SDKMessage } from '@anthropic-ai/claude-agent-sdk';
2
+ import { isRecord } from '@metro-labs/core/is-record';
3
+ import type { InputKind } from './inbox.js';
4
+ import { startedCommand, uuidsOf } from './session-watch.js';
5
+
6
+ export interface SpeechSink {
7
+ say(text: string): void;
8
+ done(): void;
9
+ stalled?(): void;
10
+ }
11
+
12
+
13
+ function textDelta(event: unknown): string | null {
14
+ if (!isRecord(event) || event.type !== 'content_block_delta' || !isRecord(event.delta)) return null;
15
+ return event.delta.type === 'text_delta' && typeof event.delta.text === 'string' ? event.delta.text : null;
16
+ }
17
+
18
+ export class SpeechRouter {
19
+ private answering: string[] = [];
20
+ private speaking = false;
21
+ private live = false;
22
+
23
+ constructor(
24
+ private readonly kindOf: (uuid: string) => InputKind | undefined,
25
+ private readonly sink: SpeechSink,
26
+ ) {}
27
+
28
+ get callLive(): boolean {
29
+ return this.live;
30
+ }
31
+
32
+ get talking(): boolean {
33
+ return this.speaking;
34
+ }
35
+
36
+ setCallLive(on: boolean): void {
37
+ this.live = on;
38
+ if (!on) this.endTurn();
39
+ }
40
+
41
+ observe(message: SDKMessage): void {
42
+ const m: Record<string, unknown> = { ...message };
43
+ if (m.type === 'result') {
44
+ this.endTurn();
45
+ this.answering = [];
46
+ return;
47
+ }
48
+ if (m.type === 'system' && m.subtype === 'init') this.endTurn();
49
+ const started = startedCommand(m);
50
+ if (started !== null) this.answer([started]);
51
+ if (m.parent_tool_use_id === null) this.mainThread(m);
52
+ }
53
+
54
+ private answer(uuids: readonly string[]): void {
55
+ this.answering = [...new Set([...this.answering, ...uuids])];
56
+ }
57
+
58
+ private mainThread(m: Record<string, unknown>): void {
59
+ if (m.type !== 'stream_event' && m.type !== 'assistant') return;
60
+ const uuids = uuidsOf(m);
61
+ if (uuids !== null) this.answer(uuids);
62
+ const text = m.type === 'stream_event' ? textDelta(m.event) : null;
63
+ if (text === null || text === '' || !this.speaks()) return;
64
+ this.speaking = true;
65
+ this.sink.say(text);
66
+ }
67
+
68
+ private speaks(): boolean {
69
+ if (!this.live) return false;
70
+ if (this.answering.length === 0) return true;
71
+ return this.answering.some((uuid) => this.kindOf(uuid) === 'call');
72
+ }
73
+
74
+ private endTurn(): void {
75
+ if (!this.speaking) return;
76
+ this.speaking = false;
77
+ this.sink.done();
78
+ }
79
+ }
@@ -0,0 +1,40 @@
1
+ import type { McpSdkServerConfigWithInstance } from '@anthropic-ai/claude-agent-sdk';
2
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
3
+ import { CallToolRequestSchema, ListToolsRequestSchema, type CallToolRequest, type CallToolResult, type ListToolsResult } from '@modelcontextprotocol/sdk/types.js';
4
+
5
+ export const METRO_SERVER = 'metro';
6
+
7
+ export interface ToolSource {
8
+ readonly instructions: string | undefined;
9
+ listTools(): Promise<ListToolsResult>;
10
+ callTool(params: CallToolRequest['params'], signal: AbortSignal): Promise<CallToolResult>;
11
+ }
12
+
13
+ export interface MetroTools {
14
+ config: McpSdkServerConfigWithInstance;
15
+ readOnly(tool: string): boolean;
16
+ changed(): void;
17
+ }
18
+
19
+ export function metroTools(source: ToolSource): MetroTools {
20
+ const instructions = source.instructions;
21
+ const reads = new Set<string>();
22
+ const server = new McpServer(
23
+ { name: METRO_SERVER, version: '0.1.0' },
24
+ { capabilities: { tools: { listChanged: true } }, ...(instructions === undefined ? {} : { instructions }) },
25
+ );
26
+ server.server.setRequestHandler(ListToolsRequestSchema, async () => {
27
+ const listed = await source.listTools();
28
+ reads.clear();
29
+ for (const tool of listed.tools) if (tool.annotations?.readOnlyHint === true) reads.add(tool.name);
30
+ return listed;
31
+ });
32
+ server.server.setRequestHandler(CallToolRequestSchema, (request, extra) => source.callTool(request.params, extra.signal));
33
+ return {
34
+ config: { type: 'sdk', name: METRO_SERVER, instance: server },
35
+ readOnly: (tool) => reads.has(tool),
36
+ changed: () => {
37
+ server.sendToolListChanged();
38
+ },
39
+ };
40
+ }