@pi-agents/orchid 0.1.0-beta.0
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/CHANGELOG.md +41 -0
- package/LICENSE +21 -0
- package/README.md +246 -0
- package/agents/AGENTS-MANIFEST.md +42 -0
- package/agents/brain.md +42 -0
- package/agents/context-builder.md +46 -0
- package/agents/delegate.md +12 -0
- package/agents/dev-1.md +42 -0
- package/agents/oracle.md +73 -0
- package/agents/planner.md +55 -0
- package/agents/researcher.md +52 -0
- package/agents/reviewer.md +79 -0
- package/agents/scout.md +50 -0
- package/agents/tester.md +45 -0
- package/agents/worker.md +55 -0
- package/extensions/ralph.ts +1 -0
- package/extensions/reviewer-extension.ts +125 -0
- package/extensions/task-orchestrator.ts +28 -0
- package/package.json +63 -0
- package/prompts/gather-context-and-clarify.md +13 -0
- package/prompts/parallel-cleanup.md +59 -0
- package/prompts/parallel-context-build.md +53 -0
- package/prompts/parallel-handoff-plan.md +59 -0
- package/prompts/parallel-research.md +50 -0
- package/prompts/parallel-review.md +54 -0
- package/prompts/review-loop.md +41 -0
- package/skills/orchid/SKILL.md +214 -0
- package/skills/orchid/orchid-cleanup/SKILL.md +122 -0
- package/skills/orchid/orchid-converge/SKILL.md +124 -0
- package/skills/orchid/orchid-decompose/SKILL.md +201 -0
- package/skills/orchid/orchid-doctor/SKILL.md +162 -0
- package/skills/orchid/orchid-investigate/SKILL.md +102 -0
- package/skills/orchid/orchid-launch/SKILL.md +147 -0
- package/skills/ralph/SKILL.md +73 -0
- package/skills/subagents/pi-subagents/SKILL.md +813 -0
- package/src/index.ts +7 -0
- package/src/orchestrator/abort.ts +534 -0
- package/src/orchestrator/agent-bridge-extension.ts +1020 -0
- package/src/orchestrator/agent-host.ts +954 -0
- package/src/orchestrator/cleanup.ts +776 -0
- package/src/orchestrator/config-loader.ts +1412 -0
- package/src/orchestrator/config-schema.ts +690 -0
- package/src/orchestrator/config.ts +81 -0
- package/src/orchestrator/context-window.ts +66 -0
- package/src/orchestrator/diagnostic-reports.ts +475 -0
- package/src/orchestrator/diagnostics.ts +394 -0
- package/src/orchestrator/discovery.ts +1833 -0
- package/src/orchestrator/engine-worker.ts +415 -0
- package/src/orchestrator/engine.ts +5940 -0
- package/src/orchestrator/execution.ts +3104 -0
- package/src/orchestrator/extension.ts +5934 -0
- package/src/orchestrator/formatting.ts +785 -0
- package/src/orchestrator/git.ts +88 -0
- package/src/orchestrator/index.ts +28 -0
- package/src/orchestrator/lane-runner.ts +1787 -0
- package/src/orchestrator/mailbox.ts +780 -0
- package/src/orchestrator/merge.ts +3414 -0
- package/src/orchestrator/messages.ts +1062 -0
- package/src/orchestrator/migrations.ts +278 -0
- package/src/orchestrator/naming.ts +117 -0
- package/src/orchestrator/path-resolver.ts +275 -0
- package/src/orchestrator/persistence.ts +2625 -0
- package/src/orchestrator/process-registry.ts +452 -0
- package/src/orchestrator/quality-gate.ts +1085 -0
- package/src/orchestrator/resume.ts +3488 -0
- package/src/orchestrator/sessions.ts +57 -0
- package/src/orchestrator/settings-loader.ts +136 -0
- package/src/orchestrator/settings-tui.ts +2208 -0
- package/src/orchestrator/sidecar-telemetry.ts +267 -0
- package/src/orchestrator/supervisor.ts +4548 -0
- package/src/orchestrator/task-executor-core.ts +675 -0
- package/src/orchestrator/tmux-compat.ts +37 -0
- package/src/orchestrator/tool-allowlist-constants.ts +37 -0
- package/src/orchestrator/types.ts +4465 -0
- package/src/orchestrator/verification.ts +547 -0
- package/src/orchestrator/waves.ts +1564 -0
- package/src/orchestrator/workspace.ts +707 -0
- package/src/orchestrator/worktree.ts +2725 -0
- package/src/ralph/index.ts +825 -0
- package/src/subagents/agents/agent-management.ts +648 -0
- package/src/subagents/agents/agent-scope.ts +6 -0
- package/src/subagents/agents/agent-selection.ts +23 -0
- package/src/subagents/agents/agent-serializer.ts +86 -0
- package/src/subagents/agents/agents.ts +832 -0
- package/src/subagents/agents/chain-serializer.ts +137 -0
- package/src/subagents/agents/frontmatter.ts +29 -0
- package/src/subagents/agents/identity.ts +30 -0
- package/src/subagents/agents/skills.ts +632 -0
- package/src/subagents/extension/config.ts +16 -0
- package/src/subagents/extension/control-notices.ts +92 -0
- package/src/subagents/extension/doctor.ts +199 -0
- package/src/subagents/extension/fanout-child.ts +170 -0
- package/src/subagents/extension/index.ts +573 -0
- package/src/subagents/extension/schemas.ts +168 -0
- package/src/subagents/intercom/intercom-bridge.ts +379 -0
- package/src/subagents/intercom/result-intercom.ts +377 -0
- package/src/subagents/runs/background/async-execution.ts +712 -0
- package/src/subagents/runs/background/async-job-tracker.ts +310 -0
- package/src/subagents/runs/background/async-resume.ts +345 -0
- package/src/subagents/runs/background/async-status.ts +325 -0
- package/src/subagents/runs/background/completion-dedupe.ts +63 -0
- package/src/subagents/runs/background/notify.ts +108 -0
- package/src/subagents/runs/background/parallel-groups.ts +45 -0
- package/src/subagents/runs/background/result-watcher.ts +307 -0
- package/src/subagents/runs/background/run-id-resolver.ts +83 -0
- package/src/subagents/runs/background/run-status.ts +269 -0
- package/src/subagents/runs/background/stale-run-reconciler.ts +336 -0
- package/src/subagents/runs/background/subagent-runner.ts +1808 -0
- package/src/subagents/runs/background/top-level-async.ts +13 -0
- package/src/subagents/runs/foreground/chain-clarify.ts +1333 -0
- package/src/subagents/runs/foreground/chain-execution.ts +938 -0
- package/src/subagents/runs/foreground/execution.ts +918 -0
- package/src/subagents/runs/foreground/subagent-executor.ts +2527 -0
- package/src/subagents/runs/shared/completion-guard.ts +147 -0
- package/src/subagents/runs/shared/long-running-guard.ts +175 -0
- package/src/subagents/runs/shared/mcp-direct-tool-allowlist.ts +365 -0
- package/src/subagents/runs/shared/model-fallback.ts +103 -0
- package/src/subagents/runs/shared/nested-events.ts +819 -0
- package/src/subagents/runs/shared/nested-path.ts +52 -0
- package/src/subagents/runs/shared/nested-render.ts +115 -0
- package/src/subagents/runs/shared/parallel-utils.ts +109 -0
- package/src/subagents/runs/shared/pi-args.ts +220 -0
- package/src/subagents/runs/shared/pi-spawn.ts +115 -0
- package/src/subagents/runs/shared/run-history.ts +60 -0
- package/src/subagents/runs/shared/single-output.ts +164 -0
- package/src/subagents/runs/shared/subagent-control.ts +226 -0
- package/src/subagents/runs/shared/subagent-prompt-runtime.ts +170 -0
- package/src/subagents/runs/shared/worktree.ts +577 -0
- package/src/subagents/shared/artifacts.ts +98 -0
- package/src/subagents/shared/atomic-json.ts +16 -0
- package/src/subagents/shared/file-coalescer.ts +40 -0
- package/src/subagents/shared/fork-context.ts +76 -0
- package/src/subagents/shared/formatters.ts +133 -0
- package/src/subagents/shared/jsonl-writer.ts +81 -0
- package/src/subagents/shared/model-info.ts +78 -0
- package/src/subagents/shared/post-exit-stdio-guard.ts +85 -0
- package/src/subagents/shared/session-identity.ts +10 -0
- package/src/subagents/shared/session-tokens.ts +44 -0
- package/src/subagents/shared/settings.ts +397 -0
- package/src/subagents/shared/status-format.ts +49 -0
- package/src/subagents/shared/types.ts +822 -0
- package/src/subagents/shared/utils.ts +450 -0
- package/src/subagents/slash/prompt-template-bridge.ts +397 -0
- package/src/subagents/slash/slash-bridge.ts +174 -0
- package/src/subagents/slash/slash-commands.ts +528 -0
- package/src/subagents/slash/slash-live-state.ts +292 -0
- package/src/subagents/tui/render-helpers.ts +80 -0
- package/src/subagents/tui/render.ts +1358 -0
- package/templates/agents/local/supervisor.md +33 -0
- package/templates/agents/local/task-merger.md +27 -0
- package/templates/agents/local/task-reviewer.md +30 -0
- package/templates/agents/local/task-worker.md +34 -0
- package/templates/agents/supervisor-routing.md +92 -0
- package/templates/agents/supervisor.md +229 -0
- package/templates/agents/task-merger.md +214 -0
- package/templates/agents/task-reviewer.md +260 -0
- package/templates/agents/task-worker-segment.md +44 -0
- package/templates/agents/task-worker.md +557 -0
- package/templates/tasks/CONTEXT.md +30 -0
- package/templates/tasks/EXAMPLE-001-hello-world/PROMPT.md +98 -0
- package/templates/tasks/EXAMPLE-001-hello-world/STATUS.md +73 -0
- package/templates/tasks/EXAMPLE-002-parallel-smoke/PROMPT.md +97 -0
- package/templates/tasks/EXAMPLE-002-parallel-smoke/STATUS.md +73 -0
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import {
|
|
4
|
+
type Details,
|
|
5
|
+
type IntercomEventBus,
|
|
6
|
+
type NestedRunSummary,
|
|
7
|
+
type PublicNestedRunSummary,
|
|
8
|
+
type SingleResult,
|
|
9
|
+
type SubagentResultIntercomChild,
|
|
10
|
+
type SubagentResultIntercomPayload,
|
|
11
|
+
type SubagentResultStatus,
|
|
12
|
+
type SubagentRunMode,
|
|
13
|
+
SUBAGENT_RESULT_INTERCOM_DELIVERY_EVENT,
|
|
14
|
+
SUBAGENT_RESULT_INTERCOM_EVENT,
|
|
15
|
+
} from "../shared/types.ts";
|
|
16
|
+
|
|
17
|
+
export function resolveSubagentResultStatus(input: {
|
|
18
|
+
exitCode?: number;
|
|
19
|
+
success?: boolean;
|
|
20
|
+
state?: string;
|
|
21
|
+
interrupted?: boolean;
|
|
22
|
+
detached?: boolean;
|
|
23
|
+
}): SubagentResultStatus {
|
|
24
|
+
if (input.detached) return "detached";
|
|
25
|
+
if (input.interrupted || input.state === "paused") return "paused";
|
|
26
|
+
if (typeof input.success === "boolean") return input.success ? "completed" : "failed";
|
|
27
|
+
if (input.state === "complete") return "completed";
|
|
28
|
+
if (input.state === "failed") return "failed";
|
|
29
|
+
if (typeof input.exitCode === "number") return input.exitCode === 0 ? "completed" : "failed";
|
|
30
|
+
return "failed";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function countStatuses(children: SubagentResultIntercomChild[]): Record<SubagentResultStatus, number> {
|
|
34
|
+
const counts: Record<SubagentResultStatus, number> = {
|
|
35
|
+
completed: 0,
|
|
36
|
+
failed: 0,
|
|
37
|
+
paused: 0,
|
|
38
|
+
detached: 0,
|
|
39
|
+
};
|
|
40
|
+
for (const child of children) {
|
|
41
|
+
counts[child.status] += 1;
|
|
42
|
+
}
|
|
43
|
+
return counts;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function formatStatusCounts(counts: Record<SubagentResultStatus, number>): string {
|
|
47
|
+
const parts = [
|
|
48
|
+
counts.completed ? `${counts.completed} completed` : undefined,
|
|
49
|
+
counts.failed ? `${counts.failed} failed` : undefined,
|
|
50
|
+
counts.paused ? `${counts.paused} paused` : undefined,
|
|
51
|
+
counts.detached ? `${counts.detached} detached` : undefined,
|
|
52
|
+
].filter((part): part is string => Boolean(part));
|
|
53
|
+
return parts.length ? parts.join(", ") : "0 results";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function resolveGroupedStatus(children: SubagentResultIntercomChild[]): SubagentResultStatus {
|
|
57
|
+
const counts = countStatuses(children);
|
|
58
|
+
if (counts.failed > 0) return "failed";
|
|
59
|
+
if (counts.paused > 0) return "paused";
|
|
60
|
+
if (counts.completed > 0) return "completed";
|
|
61
|
+
if (counts.detached > 0) return "detached";
|
|
62
|
+
return "failed";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function compactNestedRun(run: NestedRunSummary | PublicNestedRunSummary, depth = 0): PublicNestedRunSummary {
|
|
66
|
+
return {
|
|
67
|
+
id: run.id,
|
|
68
|
+
parentRunId: run.parentRunId,
|
|
69
|
+
...(run.parentStepIndex !== undefined ? { parentStepIndex: run.parentStepIndex } : {}),
|
|
70
|
+
...(run.parentAgent ? { parentAgent: run.parentAgent } : {}),
|
|
71
|
+
depth: run.depth,
|
|
72
|
+
path: run.path.slice(0, 4).map((part) => ({
|
|
73
|
+
runId: part.runId,
|
|
74
|
+
...(part.stepIndex !== undefined ? { stepIndex: part.stepIndex } : {}),
|
|
75
|
+
...(part.agent ? { agent: part.agent } : {}),
|
|
76
|
+
})),
|
|
77
|
+
...(run.asyncDir ? { asyncDir: run.asyncDir } : {}),
|
|
78
|
+
...(run.sessionId ? { sessionId: run.sessionId } : {}),
|
|
79
|
+
...(run.sessionFile ? { sessionFile: run.sessionFile } : {}),
|
|
80
|
+
...(run.intercomTarget ? { intercomTarget: run.intercomTarget } : {}),
|
|
81
|
+
...(run.ownerIntercomTarget ? { ownerIntercomTarget: run.ownerIntercomTarget } : {}),
|
|
82
|
+
...(run.leafIntercomTarget ? { leafIntercomTarget: run.leafIntercomTarget } : {}),
|
|
83
|
+
...(run.ownerState ? { ownerState: run.ownerState } : {}),
|
|
84
|
+
...(run.mode ? { mode: run.mode } : {}),
|
|
85
|
+
state: run.state,
|
|
86
|
+
...(run.agent ? { agent: run.agent } : {}),
|
|
87
|
+
...(run.agents?.length ? { agents: run.agents.slice(0, 12) } : {}),
|
|
88
|
+
...(run.currentStep !== undefined ? { currentStep: run.currentStep } : {}),
|
|
89
|
+
...(run.chainStepCount !== undefined ? { chainStepCount: run.chainStepCount } : {}),
|
|
90
|
+
...(run.parallelGroups?.length ? { parallelGroups: run.parallelGroups.slice(0, 8) } : {}),
|
|
91
|
+
...(run.activityState ? { activityState: run.activityState } : {}),
|
|
92
|
+
...(run.lastActivityAt !== undefined ? { lastActivityAt: run.lastActivityAt } : {}),
|
|
93
|
+
...(run.currentTool ? { currentTool: run.currentTool } : {}),
|
|
94
|
+
...(run.currentToolStartedAt !== undefined ? { currentToolStartedAt: run.currentToolStartedAt } : {}),
|
|
95
|
+
...(run.currentPath ? { currentPath: run.currentPath } : {}),
|
|
96
|
+
...(run.turnCount !== undefined ? { turnCount: run.turnCount } : {}),
|
|
97
|
+
...(run.toolCount !== undefined ? { toolCount: run.toolCount } : {}),
|
|
98
|
+
...(run.totalTokens ? { totalTokens: run.totalTokens } : {}),
|
|
99
|
+
...(run.startedAt !== undefined ? { startedAt: run.startedAt } : {}),
|
|
100
|
+
...(run.endedAt !== undefined ? { endedAt: run.endedAt } : {}),
|
|
101
|
+
...(run.lastUpdate !== undefined ? { lastUpdate: run.lastUpdate } : {}),
|
|
102
|
+
...(run.error ? { error: run.error } : {}),
|
|
103
|
+
...(run.steps?.length ? { steps: run.steps.slice(0, 12).map((step) => ({
|
|
104
|
+
agent: step.agent,
|
|
105
|
+
status: step.status,
|
|
106
|
+
...(step.sessionFile ? { sessionFile: step.sessionFile } : {}),
|
|
107
|
+
...(step.activityState ? { activityState: step.activityState } : {}),
|
|
108
|
+
...(step.lastActivityAt !== undefined ? { lastActivityAt: step.lastActivityAt } : {}),
|
|
109
|
+
...(step.currentTool ? { currentTool: step.currentTool } : {}),
|
|
110
|
+
...(step.currentToolStartedAt !== undefined ? { currentToolStartedAt: step.currentToolStartedAt } : {}),
|
|
111
|
+
...(step.currentPath ? { currentPath: step.currentPath } : {}),
|
|
112
|
+
...(step.turnCount !== undefined ? { turnCount: step.turnCount } : {}),
|
|
113
|
+
...(step.toolCount !== undefined ? { toolCount: step.toolCount } : {}),
|
|
114
|
+
...(step.startedAt !== undefined ? { startedAt: step.startedAt } : {}),
|
|
115
|
+
...(step.endedAt !== undefined ? { endedAt: step.endedAt } : {}),
|
|
116
|
+
...(step.error ? { error: step.error } : {}),
|
|
117
|
+
...(depth < 2 && step.children?.length ? { children: step.children.slice(0, 8).map((child) => compactNestedRun(child, depth + 1)) } : {}),
|
|
118
|
+
})) } : {}),
|
|
119
|
+
...(depth < 2 && run.children?.length ? { children: run.children.slice(0, 8).map((child) => compactNestedRun(child, depth + 1)) } : {}),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function compactNestedResultChildren(children: Array<NestedRunSummary | PublicNestedRunSummary> | undefined): PublicNestedRunSummary[] | undefined {
|
|
124
|
+
if (!children?.length) return undefined;
|
|
125
|
+
return children.slice(0, 16).map((child) => compactNestedRun(child));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function attachNestedChildrenToResultChildren(
|
|
129
|
+
runId: string,
|
|
130
|
+
children: SubagentResultIntercomChild[],
|
|
131
|
+
nestedChildren: NestedRunSummary[] | undefined,
|
|
132
|
+
): SubagentResultIntercomChild[] {
|
|
133
|
+
const compact = compactNestedResultChildren(nestedChildren);
|
|
134
|
+
if (!compact?.length) return children.map((child) => ({ ...child, children: compactNestedResultChildren(child.children) }));
|
|
135
|
+
return children.map((child, index) => {
|
|
136
|
+
const childIndex = child.index ?? index;
|
|
137
|
+
const alreadyAttachedIds = new Set(child.children?.map((nested) => nested.id) ?? []);
|
|
138
|
+
const attached = compact.filter((nested) => nested.parentRunId === runId && nested.parentStepIndex === childIndex && !alreadyAttachedIds.has(nested.id));
|
|
139
|
+
const fallbackAttached = children.length === 1
|
|
140
|
+
? compact.filter((nested) => nested.parentRunId === runId && nested.parentStepIndex === undefined && !alreadyAttachedIds.has(nested.id))
|
|
141
|
+
: [];
|
|
142
|
+
const merged = compactNestedResultChildren([...(child.children ?? []), ...attached, ...fallbackAttached]);
|
|
143
|
+
return merged?.length ? { ...child, children: merged } : { ...child, children: undefined };
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function formatNestedResultLines(children: PublicNestedRunSummary[] | undefined): string[] {
|
|
148
|
+
if (!children?.length) return [];
|
|
149
|
+
const lines = ["Nested subagents:"];
|
|
150
|
+
let remaining = 10;
|
|
151
|
+
const append = (runs: PublicNestedRunSummary[] | undefined, indent: string): void => {
|
|
152
|
+
for (const run of runs ?? []) {
|
|
153
|
+
if (remaining <= 0) {
|
|
154
|
+
lines.push(`${indent}↳ +more nested runs; inspect status for full tree`);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
remaining--;
|
|
158
|
+
const label = run.agent ?? run.agents?.join("+") ?? run.id;
|
|
159
|
+
lines.push(`${indent}↳ ${label} — ${run.state} [${run.id}]`);
|
|
160
|
+
if (run.sessionFile) lines.push(`${indent} Session: ${run.sessionFile}`);
|
|
161
|
+
append(run.children, `${indent} `);
|
|
162
|
+
for (const step of run.steps ?? []) append(step.children, `${indent} `);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
append(children, "");
|
|
166
|
+
return lines;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
interface GroupedResultIntercomMessageInput {
|
|
170
|
+
to: string;
|
|
171
|
+
runId: string;
|
|
172
|
+
mode: SubagentRunMode;
|
|
173
|
+
source: "foreground" | "async";
|
|
174
|
+
children: SubagentResultIntercomChild[];
|
|
175
|
+
asyncId?: string;
|
|
176
|
+
asyncDir?: string;
|
|
177
|
+
chainSteps?: number;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function asyncResumeGuidance(input: {
|
|
181
|
+
source: "foreground" | "async";
|
|
182
|
+
children: SubagentResultIntercomChild[];
|
|
183
|
+
asyncId?: string;
|
|
184
|
+
}): string | undefined {
|
|
185
|
+
if (input.source !== "async" || !input.asyncId) return undefined;
|
|
186
|
+
const resumable = input.children.filter((child) => typeof child.sessionPath === "string" && fs.existsSync(child.sessionPath));
|
|
187
|
+
if (input.children.length === 1 && resumable.length === 1) {
|
|
188
|
+
return `Revive: subagent({ action: "resume", id: "${input.asyncId}", message: "..." })`;
|
|
189
|
+
}
|
|
190
|
+
if (resumable.length > 0) {
|
|
191
|
+
const firstIndex = resumable[0]?.index ?? input.children.indexOf(resumable[0]!);
|
|
192
|
+
return `Revive child: subagent({ action: "resume", id: "${input.asyncId}", index: ${firstIndex}, message: "..." })`;
|
|
193
|
+
}
|
|
194
|
+
return "Resume: unavailable; no child session file was persisted.";
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function formatSubagentResultIntercomMessage(input: {
|
|
198
|
+
runId: string;
|
|
199
|
+
mode: SubagentRunMode;
|
|
200
|
+
status: SubagentResultStatus;
|
|
201
|
+
source: "foreground" | "async";
|
|
202
|
+
children: SubagentResultIntercomChild[];
|
|
203
|
+
asyncId?: string;
|
|
204
|
+
asyncDir?: string;
|
|
205
|
+
chainSteps?: number;
|
|
206
|
+
}): string {
|
|
207
|
+
const counts = countStatuses(input.children);
|
|
208
|
+
const lines: string[] = [
|
|
209
|
+
"subagent results",
|
|
210
|
+
"",
|
|
211
|
+
`Run: ${input.runId}`,
|
|
212
|
+
`Mode: ${input.mode}`,
|
|
213
|
+
`Status: ${input.status}`,
|
|
214
|
+
`Children: ${formatStatusCounts(counts)}`,
|
|
215
|
+
];
|
|
216
|
+
if (input.mode === "chain" && typeof input.chainSteps === "number") {
|
|
217
|
+
lines.push(`Chain steps: ${input.chainSteps}`);
|
|
218
|
+
}
|
|
219
|
+
if (input.asyncId) lines.push(`Async id: ${input.asyncId}`);
|
|
220
|
+
if (input.asyncDir) lines.push(`Async dir: ${input.asyncDir}`);
|
|
221
|
+
const resumeGuidance = asyncResumeGuidance(input);
|
|
222
|
+
if (resumeGuidance) lines.push(resumeGuidance);
|
|
223
|
+
if (input.children.some((child) => child.intercomTarget)) {
|
|
224
|
+
lines.push("");
|
|
225
|
+
lines.push(input.source === "async"
|
|
226
|
+
? "Previous intercom targets below identify child sessions used while they were running. Inspect artifacts or session logs if resume is unavailable."
|
|
227
|
+
: "Intercom targets below identify child sessions used while they were running; completed child sessions may no longer be reachable. Inspect artifacts or session logs for follow-up.");
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
for (let index = 0; index < input.children.length; index++) {
|
|
231
|
+
const child = input.children[index]!;
|
|
232
|
+
lines.push("");
|
|
233
|
+
lines.push(`${index + 1}. ${child.agent} — ${child.status}`);
|
|
234
|
+
if (child.intercomTarget) lines.push(`${input.source === "async" ? "Previous intercom target" : "Run intercom target"}: ${child.intercomTarget}`);
|
|
235
|
+
if (child.artifactPath) lines.push(`Output artifact: ${child.artifactPath}`);
|
|
236
|
+
if (child.sessionPath) lines.push(`Session: ${child.sessionPath}`);
|
|
237
|
+
lines.push(...formatNestedResultLines(child.children));
|
|
238
|
+
lines.push("Summary:");
|
|
239
|
+
lines.push(child.summary);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return lines.join("\n");
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export function buildSubagentResultIntercomPayload(input: GroupedResultIntercomMessageInput): SubagentResultIntercomPayload {
|
|
246
|
+
const children = input.children.map((child) => ({
|
|
247
|
+
...child,
|
|
248
|
+
summary: child.summary.trim() || "(no output)",
|
|
249
|
+
children: compactNestedResultChildren(child.children),
|
|
250
|
+
}));
|
|
251
|
+
const status = resolveGroupedStatus(children);
|
|
252
|
+
const summary = formatStatusCounts(countStatuses(children));
|
|
253
|
+
const firstChild = children[0];
|
|
254
|
+
const payload: SubagentResultIntercomPayload = {
|
|
255
|
+
to: input.to,
|
|
256
|
+
runId: input.runId,
|
|
257
|
+
mode: input.mode,
|
|
258
|
+
status,
|
|
259
|
+
summary,
|
|
260
|
+
source: input.source,
|
|
261
|
+
children,
|
|
262
|
+
...(input.asyncId ? { asyncId: input.asyncId } : {}),
|
|
263
|
+
...(input.asyncDir ? { asyncDir: input.asyncDir } : {}),
|
|
264
|
+
...(typeof input.chainSteps === "number" ? { chainSteps: input.chainSteps } : {}),
|
|
265
|
+
...(firstChild?.agent ? { agent: firstChild.agent } : {}),
|
|
266
|
+
...(firstChild?.index !== undefined ? { index: firstChild.index } : {}),
|
|
267
|
+
...(firstChild?.artifactPath ? { artifactPath: firstChild.artifactPath } : {}),
|
|
268
|
+
...(firstChild?.sessionPath ? { sessionPath: firstChild.sessionPath } : {}),
|
|
269
|
+
message: "",
|
|
270
|
+
};
|
|
271
|
+
payload.message = formatSubagentResultIntercomMessage(payload);
|
|
272
|
+
return payload;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export async function deliverSubagentResultIntercomEvent(
|
|
276
|
+
events: IntercomEventBus,
|
|
277
|
+
payload: SubagentResultIntercomPayload,
|
|
278
|
+
timeoutMs = 500,
|
|
279
|
+
): Promise<boolean> {
|
|
280
|
+
return deliverSubagentIntercomMessageEvent(events, payload.to, payload.message, timeoutMs, payload);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export async function deliverSubagentIntercomMessageEvent(
|
|
284
|
+
events: IntercomEventBus,
|
|
285
|
+
to: string,
|
|
286
|
+
message: string,
|
|
287
|
+
timeoutMs = 500,
|
|
288
|
+
extra: Record<string, unknown> = {},
|
|
289
|
+
): Promise<boolean> {
|
|
290
|
+
if (typeof events.on !== "function" || typeof events.emit !== "function") return false;
|
|
291
|
+
const requestId = typeof extra.requestId === "string" ? extra.requestId : randomUUID();
|
|
292
|
+
return new Promise((resolve) => {
|
|
293
|
+
let settled = false;
|
|
294
|
+
let unsubscribe: (() => void) | undefined;
|
|
295
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
296
|
+
const finish = (delivered: boolean) => {
|
|
297
|
+
if (settled) return;
|
|
298
|
+
settled = true;
|
|
299
|
+
if (timer) clearTimeout(timer);
|
|
300
|
+
unsubscribe?.();
|
|
301
|
+
resolve(delivered);
|
|
302
|
+
};
|
|
303
|
+
unsubscribe = events.on(SUBAGENT_RESULT_INTERCOM_DELIVERY_EVENT, (data) => {
|
|
304
|
+
if (!data || typeof data !== "object") return;
|
|
305
|
+
const delivery = data as { requestId?: unknown; delivered?: unknown };
|
|
306
|
+
if (delivery.requestId !== requestId) return;
|
|
307
|
+
finish(delivery.delivered === true);
|
|
308
|
+
});
|
|
309
|
+
timer = setTimeout(() => finish(false), timeoutMs);
|
|
310
|
+
try {
|
|
311
|
+
events.emit(SUBAGENT_RESULT_INTERCOM_EVENT, { ...extra, to, message, requestId });
|
|
312
|
+
} catch {
|
|
313
|
+
finish(false);
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function stripSingleResultOutputs(result: SingleResult): SingleResult {
|
|
319
|
+
return {
|
|
320
|
+
...result,
|
|
321
|
+
messages: undefined,
|
|
322
|
+
finalOutput: undefined,
|
|
323
|
+
truncation: undefined,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
export function stripDetailsOutputsForIntercomReceipt(details: Details): Details {
|
|
328
|
+
return {
|
|
329
|
+
...details,
|
|
330
|
+
results: details.results.map(stripSingleResultOutputs),
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
export function formatSubagentResultReceipt(input: {
|
|
335
|
+
mode: SubagentRunMode;
|
|
336
|
+
runId: string;
|
|
337
|
+
payload: SubagentResultIntercomPayload;
|
|
338
|
+
}): string {
|
|
339
|
+
const counts = countStatuses(input.payload.children);
|
|
340
|
+
const modeLabel = input.mode === "single"
|
|
341
|
+
? "single subagent result"
|
|
342
|
+
: input.mode === "parallel"
|
|
343
|
+
? "parallel subagent results"
|
|
344
|
+
: "chain subagent results";
|
|
345
|
+
const lines = [
|
|
346
|
+
`Delivered ${modeLabel} via intercom.`,
|
|
347
|
+
`Run: ${input.runId}`,
|
|
348
|
+
`Children: ${formatStatusCounts(counts)}`,
|
|
349
|
+
];
|
|
350
|
+
|
|
351
|
+
const artifacts = input.payload.children.filter((child) => typeof child.artifactPath === "string");
|
|
352
|
+
if (artifacts.length > 0) {
|
|
353
|
+
lines.push("Artifacts:");
|
|
354
|
+
for (const child of artifacts) {
|
|
355
|
+
lines.push(`- ${child.agent} [${child.status}]: ${child.artifactPath}`);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const intercomTargets = input.payload.children.filter((child) => typeof child.intercomTarget === "string");
|
|
360
|
+
if (intercomTargets.length > 0) {
|
|
361
|
+
lines.push("Run intercom targets (may be inactive after completion):");
|
|
362
|
+
for (const child of intercomTargets) {
|
|
363
|
+
lines.push(`- ${child.agent} [${child.status}]: ${child.intercomTarget}`);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const sessions = input.payload.children.filter((child) => typeof child.sessionPath === "string");
|
|
368
|
+
if (sessions.length > 0) {
|
|
369
|
+
lines.push("Sessions:");
|
|
370
|
+
for (const child of sessions) {
|
|
371
|
+
lines.push(`- ${child.agent} [${child.status}]: ${child.sessionPath}`);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
lines.push("Full grouped output was sent over intercom.");
|
|
376
|
+
return lines.join("\n");
|
|
377
|
+
}
|