pi-crew 0.1.44 → 0.1.45

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 (142) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/agents/analyst.md +11 -11
  3. package/agents/critic.md +11 -11
  4. package/agents/executor.md +11 -11
  5. package/agents/explorer.md +11 -11
  6. package/agents/planner.md +11 -11
  7. package/agents/reviewer.md +11 -11
  8. package/agents/security-reviewer.md +11 -11
  9. package/agents/test-engineer.md +11 -11
  10. package/agents/verifier.md +11 -11
  11. package/agents/writer.md +11 -11
  12. package/docs/refactor-tasks-phase3.md +394 -394
  13. package/docs/refactor-tasks-phase4.md +564 -564
  14. package/docs/refactor-tasks-phase5.md +402 -402
  15. package/docs/refactor-tasks-phase6.md +662 -662
  16. package/docs/research-extension-examples.md +297 -297
  17. package/docs/research-extension-system.md +324 -324
  18. package/docs/research-optimization-plan.md +548 -548
  19. package/docs/research-phase10-distillation.md +198 -198
  20. package/docs/research-phase11-distillation.md +201 -201
  21. package/docs/research-pi-coding-agent.md +357 -357
  22. package/docs/research-source-pi-crew-reference.md +174 -174
  23. package/docs/runtime-flow.md +148 -148
  24. package/docs/source-runtime-refactor-map.md +83 -83
  25. package/index.ts +6 -6
  26. package/package.json +1 -1
  27. package/src/agents/agent-serializer.ts +34 -34
  28. package/src/extension/cross-extension-rpc.ts +82 -82
  29. package/src/extension/register.ts +8 -1
  30. package/src/extension/registration/commands.ts +18 -2
  31. package/src/extension/registration/compaction-guard.ts +125 -125
  32. package/src/extension/registration/subagent-tools.ts +148 -148
  33. package/src/extension/registration/team-tool.ts +26 -8
  34. package/src/extension/run-bundle-schema.ts +89 -89
  35. package/src/extension/run-maintenance.ts +43 -43
  36. package/src/extension/team-tool/cancel.ts +105 -102
  37. package/src/extension/team-tool/context.ts +1 -0
  38. package/src/extension/team-tool/handle-settings.ts +188 -188
  39. package/src/extension/team-tool/inspect.ts +41 -41
  40. package/src/extension/team-tool/lifecycle-actions.ts +79 -79
  41. package/src/extension/team-tool/plan.ts +19 -19
  42. package/src/extension/team-tool/respond.ts +83 -66
  43. package/src/extension/team-tool/run.ts +1 -0
  44. package/src/i18n.ts +184 -184
  45. package/src/observability/exporters/otlp-exporter.ts +77 -77
  46. package/src/prompt/prompt-runtime.ts +72 -72
  47. package/src/runtime/agent-control.ts +63 -63
  48. package/src/runtime/agent-memory.ts +72 -72
  49. package/src/runtime/agent-observability.ts +114 -114
  50. package/src/runtime/async-marker.ts +26 -26
  51. package/src/runtime/attention-events.ts +28 -28
  52. package/src/runtime/background-runner.ts +53 -53
  53. package/src/runtime/child-pi.ts +444 -444
  54. package/src/runtime/completion-guard.ts +190 -190
  55. package/src/runtime/crew-agent-records.ts +8 -0
  56. package/src/runtime/delivery-coordinator.ts +153 -142
  57. package/src/runtime/direct-run.ts +35 -35
  58. package/src/runtime/foreground-control.ts +82 -82
  59. package/src/runtime/green-contract.ts +46 -46
  60. package/src/runtime/group-join.ts +106 -106
  61. package/src/runtime/heartbeat-gradient.ts +28 -28
  62. package/src/runtime/heartbeat-watcher.ts +124 -124
  63. package/src/runtime/live-agent-control.ts +87 -87
  64. package/src/runtime/live-agent-manager.ts +85 -85
  65. package/src/runtime/live-control-realtime.ts +36 -36
  66. package/src/runtime/live-session-runtime.ts +305 -305
  67. package/src/runtime/overflow-recovery.ts +175 -156
  68. package/src/runtime/parallel-research.ts +44 -44
  69. package/src/runtime/pi-json-output.ts +111 -111
  70. package/src/runtime/policy-engine.ts +79 -79
  71. package/src/runtime/progress-event-coalescer.ts +43 -43
  72. package/src/runtime/recovery-recipes.ts +74 -74
  73. package/src/runtime/retry-executor.ts +64 -64
  74. package/src/runtime/role-permission.ts +39 -39
  75. package/src/runtime/session-resources.ts +25 -25
  76. package/src/runtime/session-snapshot.ts +59 -59
  77. package/src/runtime/session-usage.ts +79 -79
  78. package/src/runtime/sidechain-output.ts +29 -29
  79. package/src/runtime/stale-reconciler.ts +199 -179
  80. package/src/runtime/supervisor-contact.ts +59 -59
  81. package/src/runtime/task-display.ts +38 -38
  82. package/src/runtime/task-output-context.ts +127 -127
  83. package/src/runtime/task-runner/live-executor.ts +101 -101
  84. package/src/runtime/task-runner/progress.ts +119 -119
  85. package/src/runtime/task-runner/result-utils.ts +14 -14
  86. package/src/runtime/task-runner/state-helpers.ts +22 -22
  87. package/src/runtime/team-runner.ts +13 -4
  88. package/src/runtime/worker-heartbeat.ts +21 -21
  89. package/src/runtime/worker-startup.ts +57 -57
  90. package/src/state/state-store.ts +43 -0
  91. package/src/state/task-claims.ts +44 -44
  92. package/src/state/types.ts +2 -0
  93. package/src/state/usage.ts +29 -29
  94. package/src/subagents/async-entry.ts +1 -1
  95. package/src/subagents/index.ts +3 -3
  96. package/src/subagents/live/control.ts +1 -1
  97. package/src/subagents/live/manager.ts +1 -1
  98. package/src/subagents/live/realtime.ts +1 -1
  99. package/src/subagents/live/session-runtime.ts +1 -1
  100. package/src/subagents/manager.ts +1 -1
  101. package/src/subagents/spawn.ts +1 -1
  102. package/src/teams/team-serializer.ts +38 -38
  103. package/src/types/diff.d.ts +18 -18
  104. package/src/ui/crew-footer.ts +101 -101
  105. package/src/ui/crew-select-list.ts +111 -111
  106. package/src/ui/crew-widget.ts +5 -1
  107. package/src/ui/dashboard-panes/mailbox-pane.ts +2 -1
  108. package/src/ui/dashboard-panes/metrics-pane.ts +34 -34
  109. package/src/ui/dynamic-border.ts +25 -25
  110. package/src/ui/layout-primitives.ts +106 -106
  111. package/src/ui/loaders.ts +158 -158
  112. package/src/ui/powerbar-publisher.ts +1 -1
  113. package/src/ui/render-diff.ts +119 -119
  114. package/src/ui/render-scheduler.ts +143 -143
  115. package/src/ui/run-snapshot-cache.ts +56 -37
  116. package/src/ui/snapshot-types.ts +5 -0
  117. package/src/ui/spinner.ts +17 -17
  118. package/src/ui/status-colors.ts +58 -58
  119. package/src/ui/syntax-highlight.ts +116 -116
  120. package/src/utils/atomic-write.ts +33 -33
  121. package/src/utils/completion-dedupe.ts +63 -63
  122. package/src/utils/frontmatter.ts +68 -68
  123. package/src/utils/git.ts +262 -262
  124. package/src/utils/ids.ts +12 -12
  125. package/src/utils/names.ts +27 -27
  126. package/src/utils/redaction.ts +44 -44
  127. package/src/utils/safe-paths.ts +47 -47
  128. package/src/utils/sleep.ts +32 -32
  129. package/src/workflows/validate-workflow.ts +40 -40
  130. package/src/worktree/branch-freshness.ts +45 -45
  131. package/teams/default.team.md +12 -12
  132. package/teams/fast-fix.team.md +11 -11
  133. package/teams/implementation.team.md +18 -18
  134. package/teams/parallel-research.team.md +14 -14
  135. package/teams/research.team.md +11 -11
  136. package/teams/review.team.md +12 -12
  137. package/workflows/default.workflow.md +29 -29
  138. package/workflows/fast-fix.workflow.md +22 -22
  139. package/workflows/implementation.workflow.md +38 -38
  140. package/workflows/parallel-research.workflow.md +46 -46
  141. package/workflows/research.workflow.md +22 -22
  142. package/workflows/review.workflow.md +30 -30
@@ -1,87 +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
- 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
+ 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
+ }