pi-crew 0.1.37 → 0.1.39

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 (162) hide show
  1. package/AGENTS.md +1 -1
  2. package/CHANGELOG.md +27 -0
  3. package/README.md +5 -0
  4. package/agents/analyst.md +11 -11
  5. package/agents/critic.md +11 -11
  6. package/agents/executor.md +11 -11
  7. package/agents/explorer.md +11 -11
  8. package/agents/planner.md +11 -11
  9. package/agents/reviewer.md +11 -11
  10. package/agents/security-reviewer.md +11 -11
  11. package/agents/test-engineer.md +11 -11
  12. package/agents/verifier.md +11 -11
  13. package/agents/writer.md +11 -11
  14. package/docs/refactor-tasks-phase3.md +394 -394
  15. package/docs/refactor-tasks-phase4.md +564 -564
  16. package/docs/refactor-tasks-phase5.md +402 -402
  17. package/docs/refactor-tasks-phase6.md +662 -662
  18. package/docs/research-extension-examples.md +297 -297
  19. package/docs/research-extension-system.md +324 -324
  20. package/docs/research-optimization-plan.md +548 -548
  21. package/docs/research-pi-coding-agent.md +357 -357
  22. package/docs/research-source-pi-crew-reference.md +174 -174
  23. package/docs/resource-formats.md +10 -8
  24. package/docs/runtime-flow.md +148 -148
  25. package/docs/source-runtime-refactor-map.md +83 -83
  26. package/docs/usage.md +6 -0
  27. package/index.ts +6 -6
  28. package/package.json +3 -3
  29. package/schema.json +2 -2
  30. package/src/agents/agent-serializer.ts +34 -34
  31. package/src/config/config.ts +8 -4
  32. package/src/extension/cross-extension-rpc.ts +82 -82
  33. package/src/extension/import-index.ts +18 -2
  34. package/src/extension/register.ts +11 -1
  35. package/src/extension/registration/compaction-guard.ts +125 -125
  36. package/src/extension/registration/subagent-helpers.ts +30 -6
  37. package/src/extension/registration/subagent-tools.ts +8 -3
  38. package/src/extension/result-watcher.ts +98 -98
  39. package/src/extension/run-import.ts +12 -2
  40. package/src/extension/run-index.ts +12 -2
  41. package/src/extension/run-maintenance.ts +24 -24
  42. package/src/extension/team-tool/api.ts +54 -14
  43. package/src/extension/team-tool/cancel.ts +31 -31
  44. package/src/extension/team-tool/doctor.ts +179 -179
  45. package/src/extension/team-tool/inspect.ts +41 -41
  46. package/src/extension/team-tool/lifecycle-actions.ts +79 -79
  47. package/src/extension/team-tool/plan.ts +19 -19
  48. package/src/extension/team-tool/status.ts +73 -73
  49. package/src/observability/correlation.ts +35 -35
  50. package/src/observability/event-to-metric.ts +54 -54
  51. package/src/observability/exporters/adapter.ts +24 -24
  52. package/src/observability/exporters/otlp-exporter.ts +65 -65
  53. package/src/observability/exporters/prometheus-exporter.ts +47 -47
  54. package/src/observability/metric-registry.ts +72 -72
  55. package/src/observability/metric-retention.ts +46 -46
  56. package/src/observability/metric-sink.ts +51 -51
  57. package/src/observability/metrics-primitives.ts +166 -166
  58. package/src/prompt/prompt-runtime.ts +68 -68
  59. package/src/runtime/agent-control.ts +64 -64
  60. package/src/runtime/agent-memory.ts +72 -72
  61. package/src/runtime/agent-observability.ts +114 -113
  62. package/src/runtime/async-marker.ts +26 -26
  63. package/src/runtime/background-runner.ts +53 -53
  64. package/src/runtime/crash-recovery.ts +56 -56
  65. package/src/runtime/crew-agent-records.ts +54 -9
  66. package/src/runtime/crew-agent-runtime.ts +58 -58
  67. package/src/runtime/deadletter.ts +36 -36
  68. package/src/runtime/direct-run.ts +35 -35
  69. package/src/runtime/foreground-control.ts +82 -82
  70. package/src/runtime/green-contract.ts +46 -46
  71. package/src/runtime/group-join.ts +88 -88
  72. package/src/runtime/heartbeat-gradient.ts +28 -28
  73. package/src/runtime/heartbeat-watcher.ts +80 -80
  74. package/src/runtime/live-agent-control.ts +87 -78
  75. package/src/runtime/live-agent-manager.ts +85 -85
  76. package/src/runtime/live-control-realtime.ts +36 -36
  77. package/src/runtime/live-session-runtime.ts +299 -299
  78. package/src/runtime/manifest-cache.ts +248 -212
  79. package/src/runtime/model-fallback.ts +261 -261
  80. package/src/runtime/parallel-research.ts +44 -44
  81. package/src/runtime/parallel-utils.ts +99 -99
  82. package/src/runtime/pi-json-output.ts +111 -111
  83. package/src/runtime/policy-engine.ts +78 -78
  84. package/src/runtime/post-exit-stdio-guard.ts +86 -86
  85. package/src/runtime/process-status.ts +56 -56
  86. package/src/runtime/progress-event-coalescer.ts +43 -43
  87. package/src/runtime/recovery-recipes.ts +74 -74
  88. package/src/runtime/retry-executor.ts +59 -59
  89. package/src/runtime/role-permission.ts +39 -39
  90. package/src/runtime/session-usage.ts +79 -79
  91. package/src/runtime/sidechain-output.ts +28 -28
  92. package/src/runtime/subagent-manager.ts +80 -12
  93. package/src/runtime/task-display.ts +38 -38
  94. package/src/runtime/task-output-context.ts +127 -106
  95. package/src/runtime/task-runner/live-executor.ts +98 -98
  96. package/src/runtime/task-runner/progress.ts +111 -111
  97. package/src/runtime/task-runner/result-utils.ts +14 -14
  98. package/src/runtime/task-runner/state-helpers.ts +22 -22
  99. package/src/runtime/team-runner.ts +1 -1
  100. package/src/runtime/worker-heartbeat.ts +21 -21
  101. package/src/runtime/worker-startup.ts +57 -57
  102. package/src/schema/config-schema.ts +21 -21
  103. package/src/schema/team-tool-schema.ts +100 -100
  104. package/src/state/artifact-store.ts +122 -108
  105. package/src/state/contracts.ts +105 -105
  106. package/src/state/jsonl-writer.ts +77 -77
  107. package/src/state/mailbox.ts +67 -22
  108. package/src/state/state-store.ts +36 -5
  109. package/src/state/task-claims.ts +42 -42
  110. package/src/state/usage.ts +29 -29
  111. package/src/subagents/async-entry.ts +1 -1
  112. package/src/subagents/index.ts +3 -3
  113. package/src/subagents/live/control.ts +1 -1
  114. package/src/subagents/live/manager.ts +1 -1
  115. package/src/subagents/live/realtime.ts +1 -1
  116. package/src/subagents/live/session-runtime.ts +1 -1
  117. package/src/subagents/manager.ts +1 -1
  118. package/src/subagents/spawn.ts +1 -1
  119. package/src/teams/discover-teams.ts +27 -5
  120. package/src/teams/team-serializer.ts +38 -36
  121. package/src/types/diff.d.ts +18 -18
  122. package/src/ui/crew-footer.ts +101 -101
  123. package/src/ui/crew-select-list.ts +111 -111
  124. package/src/ui/dashboard-panes/metrics-pane.ts +34 -34
  125. package/src/ui/dynamic-border.ts +25 -25
  126. package/src/ui/layout-primitives.ts +106 -106
  127. package/src/ui/loaders.ts +158 -158
  128. package/src/ui/mascot.ts +441 -441
  129. package/src/ui/render-diff.ts +119 -119
  130. package/src/ui/run-dashboard.ts +5 -2
  131. package/src/ui/run-snapshot-cache.ts +19 -8
  132. package/src/ui/spinner.ts +17 -17
  133. package/src/ui/status-colors.ts +54 -54
  134. package/src/ui/syntax-highlight.ts +116 -116
  135. package/src/ui/transcript-viewer.ts +15 -1
  136. package/src/utils/completion-dedupe.ts +63 -63
  137. package/src/utils/file-coalescer.ts +84 -84
  138. package/src/utils/frontmatter.ts +36 -36
  139. package/src/utils/fs-watch.ts +31 -31
  140. package/src/utils/git.ts +262 -262
  141. package/src/utils/ids.ts +12 -12
  142. package/src/utils/names.ts +26 -26
  143. package/src/utils/paths.ts +3 -2
  144. package/src/utils/safe-paths.ts +34 -0
  145. package/src/utils/sleep.ts +32 -32
  146. package/src/utils/timings.ts +31 -31
  147. package/src/utils/visual.ts +159 -159
  148. package/src/workflows/discover-workflows.ts +30 -3
  149. package/src/workflows/validate-workflow.ts +40 -40
  150. package/src/worktree/branch-freshness.ts +45 -45
  151. package/teams/default.team.md +12 -12
  152. package/teams/fast-fix.team.md +11 -11
  153. package/teams/implementation.team.md +18 -18
  154. package/teams/parallel-research.team.md +14 -14
  155. package/teams/research.team.md +11 -11
  156. package/teams/review.team.md +12 -12
  157. package/workflows/default.workflow.md +29 -29
  158. package/workflows/fast-fix.workflow.md +22 -22
  159. package/workflows/implementation.workflow.md +38 -38
  160. package/workflows/parallel-research.workflow.md +46 -46
  161. package/workflows/research.workflow.md +22 -22
  162. package/workflows/review.workflow.md +30 -30
@@ -1,78 +1,87 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import type { TeamRunManifest } from "../state/types.ts";
4
-
5
- export type LiveAgentControlOperation = "steer" | "stop" | "resume";
6
-
7
- export interface LiveAgentControlRequest {
8
- id: string;
9
- runId: string;
10
- taskId: string;
11
- agentId?: string;
12
- operation: LiveAgentControlOperation;
13
- message?: string;
14
- createdAt: string;
15
- processedAt?: string;
16
- error?: string;
17
- }
18
-
19
- export interface LiveAgentControlCursor {
20
- offset: number;
21
- }
22
-
23
- export function liveAgentControlPath(manifest: TeamRunManifest, taskId: string): string {
24
- return path.join(manifest.stateRoot, "agents", taskId, "live-control.jsonl");
25
- }
26
-
27
- function requestId(): string {
28
- return `ctrl_${Date.now().toString(36)}_${Math.random().toString(16).slice(2, 10)}`;
29
- }
30
-
31
- export function appendLiveAgentControlRequest(manifest: TeamRunManifest, input: { taskId: string; agentId?: string; operation: LiveAgentControlOperation; message?: string }): LiveAgentControlRequest {
32
- const request: LiveAgentControlRequest = {
33
- id: requestId(),
34
- runId: manifest.runId,
35
- taskId: input.taskId,
36
- agentId: input.agentId,
37
- operation: input.operation,
38
- message: input.message,
39
- createdAt: new Date().toISOString(),
40
- };
41
- const filePath = liveAgentControlPath(manifest, input.taskId);
42
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
43
- fs.appendFileSync(filePath, `${JSON.stringify(request)}\n`, "utf-8");
44
- return request;
45
- }
46
-
47
- export function readLiveAgentControlRequests(manifest: TeamRunManifest, taskId: string, cursor: LiveAgentControlCursor = { offset: 0 }): { requests: LiveAgentControlRequest[]; cursor: LiveAgentControlCursor } {
48
- const filePath = liveAgentControlPath(manifest, taskId);
49
- if (!fs.existsSync(filePath)) return { requests: [], cursor };
50
- const text = fs.readFileSync(filePath, "utf-8");
51
- const lines = text.split(/\r?\n/).filter(Boolean);
52
- const requests = lines.slice(cursor.offset).flatMap((line) => {
53
- try {
54
- const parsed = JSON.parse(line) as LiveAgentControlRequest;
55
- return parsed && parsed.runId === manifest.runId && parsed.taskId === taskId ? [parsed] : [];
56
- } catch {
57
- return [];
58
- }
59
- });
60
- return { requests, cursor: { offset: lines.length } };
61
- }
62
-
63
- export async function applyLiveAgentControlRequest(input: { request: LiveAgentControlRequest; taskId: string; agentId: string; session: { steer?: (text: string) => Promise<void>; prompt?: (text: string, options?: Record<string, unknown>) => Promise<void>; abort?: () => Promise<void> | void }; seenRequestIds?: Set<string> }): Promise<boolean> {
64
- const { request, taskId, agentId, session, seenRequestIds } = input;
65
- if (seenRequestIds?.has(request.id)) return false;
66
- if (request.agentId && request.agentId !== agentId && request.agentId !== taskId) return false;
67
- seenRequestIds?.add(request.id);
68
- if (request.operation === "steer") await session.steer?.(request.message ?? "Please report current status and wrap up if possible.");
69
- else if (request.operation === "resume") await session.prompt?.(request.message ?? "Please resume and report final status.", { source: "api", expandPromptTemplates: false });
70
- else if (request.operation === "stop") await session.abort?.();
71
- return true;
72
- }
73
-
74
- export async function applyLiveAgentControlRequests(input: { manifest: TeamRunManifest; taskId: string; agentId: string; session: { steer?: (text: string) => Promise<void>; prompt?: (text: string, options?: Record<string, unknown>) => Promise<void>; abort?: () => Promise<void> | void }; cursor: LiveAgentControlCursor; seenRequestIds?: Set<string> }): Promise<LiveAgentControlCursor> {
75
- const batch = readLiveAgentControlRequests(input.manifest, input.taskId, input.cursor);
76
- for (const request of batch.requests) await applyLiveAgentControlRequest({ request, taskId: input.taskId, agentId: input.agentId, session: input.session, seenRequestIds: input.seenRequestIds });
77
- return batch.cursor;
78
- }
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import type { TeamRunManifest } from "../state/types.ts";
4
+ import { agentStateFile, ensureAgentStateDir } from "./crew-agent-records.ts";
5
+
6
+ export type LiveAgentControlOperation = "steer" | "stop" | "resume";
7
+
8
+ export interface LiveAgentControlRequest {
9
+ id: string;
10
+ runId: string;
11
+ taskId: string;
12
+ agentId?: string;
13
+ operation: LiveAgentControlOperation;
14
+ message?: string;
15
+ createdAt: string;
16
+ processedAt?: string;
17
+ error?: string;
18
+ }
19
+
20
+ export interface LiveAgentControlCursor {
21
+ offset: number;
22
+ }
23
+
24
+ export function liveAgentControlPath(manifest: TeamRunManifest, taskId: string): string {
25
+ return path.join(ensureAgentStateDir(manifest, taskId), "live-control.jsonl");
26
+ }
27
+
28
+ function liveAgentControlFile(manifest: TeamRunManifest, taskId: string): string {
29
+ return agentStateFile(manifest, taskId, "live-control.jsonl");
30
+ }
31
+
32
+ function requestId(): string {
33
+ return `ctrl_${Date.now().toString(36)}_${Math.random().toString(16).slice(2, 10)}`;
34
+ }
35
+
36
+ export function appendLiveAgentControlRequest(manifest: TeamRunManifest, input: { taskId: string; agentId?: string; operation: LiveAgentControlOperation; message?: string }): LiveAgentControlRequest {
37
+ const request: LiveAgentControlRequest = {
38
+ id: requestId(),
39
+ runId: manifest.runId,
40
+ taskId: input.taskId,
41
+ agentId: input.agentId,
42
+ operation: input.operation,
43
+ message: input.message,
44
+ createdAt: new Date().toISOString(),
45
+ };
46
+ const filePath = liveAgentControlFile(manifest, input.taskId);
47
+ fs.appendFileSync(filePath, `${JSON.stringify(request)}\n`, "utf-8");
48
+ return request;
49
+ }
50
+
51
+ export function readLiveAgentControlRequests(manifest: TeamRunManifest, taskId: string, cursor: LiveAgentControlCursor = { offset: 0 }): { requests: LiveAgentControlRequest[]; cursor: LiveAgentControlCursor } {
52
+ let filePath: string;
53
+ try {
54
+ filePath = liveAgentControlFile(manifest, taskId);
55
+ } catch {
56
+ return { requests: [], cursor };
57
+ }
58
+ if (!fs.existsSync(filePath)) return { requests: [], cursor };
59
+ const text = fs.readFileSync(filePath, "utf-8");
60
+ const lines = text.split(/\r?\n/).filter(Boolean);
61
+ const requests = lines.slice(cursor.offset).flatMap((line) => {
62
+ try {
63
+ const parsed = JSON.parse(line) as LiveAgentControlRequest;
64
+ return parsed && parsed.runId === manifest.runId && parsed.taskId === taskId ? [parsed] : [];
65
+ } catch {
66
+ return [];
67
+ }
68
+ });
69
+ return { requests, cursor: { offset: lines.length } };
70
+ }
71
+
72
+ export async function applyLiveAgentControlRequest(input: { request: LiveAgentControlRequest; taskId: string; agentId: string; session: { steer?: (text: string) => Promise<void>; prompt?: (text: string, options?: Record<string, unknown>) => Promise<void>; abort?: () => Promise<void> | void }; seenRequestIds?: Set<string> }): Promise<boolean> {
73
+ const { request, taskId, agentId, session, seenRequestIds } = input;
74
+ if (seenRequestIds?.has(request.id)) return false;
75
+ if (request.agentId && request.agentId !== agentId && request.agentId !== taskId) return false;
76
+ seenRequestIds?.add(request.id);
77
+ if (request.operation === "steer") await session.steer?.(request.message ?? "Please report current status and wrap up if possible.");
78
+ else if (request.operation === "resume") await session.prompt?.(request.message ?? "Please resume and report final status.", { source: "api", expandPromptTemplates: false });
79
+ else if (request.operation === "stop") await session.abort?.();
80
+ return true;
81
+ }
82
+
83
+ export async function applyLiveAgentControlRequests(input: { manifest: TeamRunManifest; taskId: string; agentId: string; session: { steer?: (text: string) => Promise<void>; prompt?: (text: string, options?: Record<string, unknown>) => Promise<void>; abort?: () => Promise<void> | void }; cursor: LiveAgentControlCursor; seenRequestIds?: Set<string> }): Promise<LiveAgentControlCursor> {
84
+ const batch = readLiveAgentControlRequests(input.manifest, input.taskId, input.cursor);
85
+ for (const request of batch.requests) await applyLiveAgentControlRequest({ request, taskId: input.taskId, agentId: input.agentId, session: input.session, seenRequestIds: input.seenRequestIds });
86
+ return batch.cursor;
87
+ }
@@ -1,85 +1,85 @@
1
- import type { CrewAgentRecord } from "./crew-agent-runtime.ts";
2
-
3
- type LiveSessionHandle = {
4
- steer?: (text: string) => Promise<void>;
5
- prompt?: (text: string, options?: Record<string, unknown>) => Promise<void>;
6
- abort?: () => Promise<void> | void;
7
- };
8
-
9
- export interface LiveAgentHandle {
10
- agentId: string;
11
- taskId: string;
12
- runId: string;
13
- session: LiveSessionHandle;
14
- createdAt: string;
15
- updatedAt: string;
16
- status: CrewAgentRecord["status"];
17
- pendingSteers: string[];
18
- }
19
-
20
- const liveAgents = new Map<string, LiveAgentHandle>();
21
-
22
- export function registerLiveAgent(input: Omit<LiveAgentHandle, "createdAt" | "updatedAt" | "pendingSteers">): LiveAgentHandle {
23
- const now = new Date().toISOString();
24
- const existing = liveAgents.get(input.agentId);
25
- const handle: LiveAgentHandle = { ...input, createdAt: existing?.createdAt ?? now, updatedAt: now, pendingSteers: existing?.pendingSteers ?? [] };
26
- liveAgents.set(input.agentId, handle);
27
- if (handle.pendingSteers.length && typeof handle.session.steer === "function") {
28
- const pending = [...handle.pendingSteers];
29
- handle.pendingSteers.length = 0;
30
- for (const message of pending) void handle.session.steer(message).catch(() => {});
31
- }
32
- return handle;
33
- }
34
-
35
- export function updateLiveAgentStatus(agentId: string, status: CrewAgentRecord["status"]): void {
36
- const handle = liveAgents.get(agentId);
37
- if (!handle) return;
38
- handle.status = status;
39
- handle.updatedAt = new Date().toISOString();
40
- }
41
-
42
- export function getLiveAgent(agentIdOrTaskId: string): LiveAgentHandle | undefined {
43
- return liveAgents.get(agentIdOrTaskId) ?? [...liveAgents.values()].find((entry) => entry.taskId === agentIdOrTaskId);
44
- }
45
-
46
- export function listLiveAgents(): LiveAgentHandle[] {
47
- return [...liveAgents.values()].sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
48
- }
49
-
50
- export async function steerLiveAgent(agentIdOrTaskId: string, message: string): Promise<LiveAgentHandle> {
51
- const handle = getLiveAgent(agentIdOrTaskId);
52
- if (!handle) throw new Error(`Live agent '${agentIdOrTaskId}' is not registered in this process.`);
53
- if (typeof handle.session.steer !== "function") {
54
- handle.pendingSteers.push(message);
55
- return handle;
56
- }
57
- await handle.session.steer(message);
58
- handle.updatedAt = new Date().toISOString();
59
- return handle;
60
- }
61
-
62
- export async function stopLiveAgent(agentIdOrTaskId: string): Promise<LiveAgentHandle> {
63
- const handle = getLiveAgent(agentIdOrTaskId);
64
- if (!handle) throw new Error(`Live agent '${agentIdOrTaskId}' is not registered in this process.`);
65
- if (typeof handle.session.abort !== "function") throw new Error(`Live agent '${agentIdOrTaskId}' does not expose abort().`);
66
- await handle.session.abort();
67
- handle.status = "stopped";
68
- handle.updatedAt = new Date().toISOString();
69
- return handle;
70
- }
71
-
72
- export async function resumeLiveAgent(agentIdOrTaskId: string, prompt: string): Promise<LiveAgentHandle> {
73
- const handle = getLiveAgent(agentIdOrTaskId);
74
- if (!handle) throw new Error(`Live agent '${agentIdOrTaskId}' is not registered in this process.`);
75
- if (typeof handle.session.prompt !== "function") throw new Error(`Live agent '${agentIdOrTaskId}' does not expose prompt().`);
76
- handle.status = "running";
77
- await handle.session.prompt(prompt, { source: "api", expandPromptTemplates: false });
78
- handle.status = "completed";
79
- handle.updatedAt = new Date().toISOString();
80
- return handle;
81
- }
82
-
83
- export function clearLiveAgentsForTest(): void {
84
- liveAgents.clear();
85
- }
1
+ import type { CrewAgentRecord } from "./crew-agent-runtime.ts";
2
+
3
+ type LiveSessionHandle = {
4
+ steer?: (text: string) => Promise<void>;
5
+ prompt?: (text: string, options?: Record<string, unknown>) => Promise<void>;
6
+ abort?: () => Promise<void> | void;
7
+ };
8
+
9
+ export interface LiveAgentHandle {
10
+ agentId: string;
11
+ taskId: string;
12
+ runId: string;
13
+ session: LiveSessionHandle;
14
+ createdAt: string;
15
+ updatedAt: string;
16
+ status: CrewAgentRecord["status"];
17
+ pendingSteers: string[];
18
+ }
19
+
20
+ const liveAgents = new Map<string, LiveAgentHandle>();
21
+
22
+ export function registerLiveAgent(input: Omit<LiveAgentHandle, "createdAt" | "updatedAt" | "pendingSteers">): LiveAgentHandle {
23
+ const now = new Date().toISOString();
24
+ const existing = liveAgents.get(input.agentId);
25
+ const handle: LiveAgentHandle = { ...input, createdAt: existing?.createdAt ?? now, updatedAt: now, pendingSteers: existing?.pendingSteers ?? [] };
26
+ liveAgents.set(input.agentId, handle);
27
+ if (handle.pendingSteers.length && typeof handle.session.steer === "function") {
28
+ const pending = [...handle.pendingSteers];
29
+ handle.pendingSteers.length = 0;
30
+ for (const message of pending) void handle.session.steer(message).catch(() => {});
31
+ }
32
+ return handle;
33
+ }
34
+
35
+ export function updateLiveAgentStatus(agentId: string, status: CrewAgentRecord["status"]): void {
36
+ const handle = liveAgents.get(agentId);
37
+ if (!handle) return;
38
+ handle.status = status;
39
+ handle.updatedAt = new Date().toISOString();
40
+ }
41
+
42
+ export function getLiveAgent(agentIdOrTaskId: string): LiveAgentHandle | undefined {
43
+ return liveAgents.get(agentIdOrTaskId) ?? [...liveAgents.values()].find((entry) => entry.taskId === agentIdOrTaskId);
44
+ }
45
+
46
+ export function listLiveAgents(): LiveAgentHandle[] {
47
+ return [...liveAgents.values()].sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
48
+ }
49
+
50
+ export async function steerLiveAgent(agentIdOrTaskId: string, message: string): Promise<LiveAgentHandle> {
51
+ const handle = getLiveAgent(agentIdOrTaskId);
52
+ if (!handle) throw new Error(`Live agent '${agentIdOrTaskId}' is not registered in this process.`);
53
+ if (typeof handle.session.steer !== "function") {
54
+ handle.pendingSteers.push(message);
55
+ return handle;
56
+ }
57
+ await handle.session.steer(message);
58
+ handle.updatedAt = new Date().toISOString();
59
+ return handle;
60
+ }
61
+
62
+ export async function stopLiveAgent(agentIdOrTaskId: string): Promise<LiveAgentHandle> {
63
+ const handle = getLiveAgent(agentIdOrTaskId);
64
+ if (!handle) throw new Error(`Live agent '${agentIdOrTaskId}' is not registered in this process.`);
65
+ if (typeof handle.session.abort !== "function") throw new Error(`Live agent '${agentIdOrTaskId}' does not expose abort().`);
66
+ await handle.session.abort();
67
+ handle.status = "stopped";
68
+ handle.updatedAt = new Date().toISOString();
69
+ return handle;
70
+ }
71
+
72
+ export async function resumeLiveAgent(agentIdOrTaskId: string, prompt: string): Promise<LiveAgentHandle> {
73
+ const handle = getLiveAgent(agentIdOrTaskId);
74
+ if (!handle) throw new Error(`Live agent '${agentIdOrTaskId}' is not registered in this process.`);
75
+ if (typeof handle.session.prompt !== "function") throw new Error(`Live agent '${agentIdOrTaskId}' does not expose prompt().`);
76
+ handle.status = "running";
77
+ await handle.session.prompt(prompt, { source: "api", expandPromptTemplates: false });
78
+ handle.status = "completed";
79
+ handle.updatedAt = new Date().toISOString();
80
+ return handle;
81
+ }
82
+
83
+ export function clearLiveAgentsForTest(): void {
84
+ liveAgents.clear();
85
+ }
@@ -1,36 +1,36 @@
1
- import type { LiveAgentControlRequest } from "./live-agent-control.ts";
2
-
3
- export interface LiveControlRealtimeMessage {
4
- type: "live-control";
5
- version: 1;
6
- request: LiveAgentControlRequest;
7
- }
8
-
9
- type Listener = (request: LiveAgentControlRequest) => void | Promise<void>;
10
-
11
- const listeners = new Set<Listener>();
12
-
13
- export function publishLiveControlRealtime(request: LiveAgentControlRequest): void {
14
- for (const listener of [...listeners]) void listener(request);
15
- }
16
-
17
- export function subscribeLiveControlRealtime(listener: Listener): () => void {
18
- listeners.add(listener);
19
- return () => listeners.delete(listener);
20
- }
21
-
22
- export function liveControlRealtimeMessage(request: LiveAgentControlRequest): LiveControlRealtimeMessage {
23
- return { type: "live-control", version: 1, request };
24
- }
25
-
26
- export function parseLiveControlRealtimeMessage(raw: unknown): LiveAgentControlRequest | undefined {
27
- if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined;
28
- const message = raw as { type?: unknown; version?: unknown; request?: unknown };
29
- if (message.type !== "live-control" || message.version !== 1 || !message.request || typeof message.request !== "object" || Array.isArray(message.request)) return undefined;
30
- const request = message.request as Partial<LiveAgentControlRequest>;
31
- return typeof request.id === "string" && typeof request.runId === "string" && typeof request.taskId === "string" && (request.operation === "steer" || request.operation === "stop" || request.operation === "resume") && typeof request.createdAt === "string" ? request as LiveAgentControlRequest : undefined;
32
- }
33
-
34
- export function clearLiveControlRealtimeForTest(): void {
35
- listeners.clear();
36
- }
1
+ import type { LiveAgentControlRequest } from "./live-agent-control.ts";
2
+
3
+ export interface LiveControlRealtimeMessage {
4
+ type: "live-control";
5
+ version: 1;
6
+ request: LiveAgentControlRequest;
7
+ }
8
+
9
+ type Listener = (request: LiveAgentControlRequest) => void | Promise<void>;
10
+
11
+ const listeners = new Set<Listener>();
12
+
13
+ export function publishLiveControlRealtime(request: LiveAgentControlRequest): void {
14
+ for (const listener of [...listeners]) void listener(request);
15
+ }
16
+
17
+ export function subscribeLiveControlRealtime(listener: Listener): () => void {
18
+ listeners.add(listener);
19
+ return () => listeners.delete(listener);
20
+ }
21
+
22
+ export function liveControlRealtimeMessage(request: LiveAgentControlRequest): LiveControlRealtimeMessage {
23
+ return { type: "live-control", version: 1, request };
24
+ }
25
+
26
+ export function parseLiveControlRealtimeMessage(raw: unknown): LiveAgentControlRequest | undefined {
27
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined;
28
+ const message = raw as { type?: unknown; version?: unknown; request?: unknown };
29
+ if (message.type !== "live-control" || message.version !== 1 || !message.request || typeof message.request !== "object" || Array.isArray(message.request)) return undefined;
30
+ const request = message.request as Partial<LiveAgentControlRequest>;
31
+ return typeof request.id === "string" && typeof request.runId === "string" && typeof request.taskId === "string" && (request.operation === "steer" || request.operation === "stop" || request.operation === "resume") && typeof request.createdAt === "string" ? request as LiveAgentControlRequest : undefined;
32
+ }
33
+
34
+ export function clearLiveControlRealtimeForTest(): void {
35
+ listeners.clear();
36
+ }