@botbotgo/agent-harness 0.0.475 → 0.0.476
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/README.md +3 -1234
- package/README.zh.md +3 -1191
- package/dist/acp.js +1 -1
- package/dist/api.js +1 -404
- package/dist/benchmark/checkpoint-resume-cost-benchmark.js +1 -55
- package/dist/benchmark/deepagent-local-model-benchmark.js +2 -35
- package/dist/benchmark/upstream-runtime-ab-benchmark.js +1 -179
- package/dist/cli/chat-interactive.js +25 -244
- package/dist/cli/chat-rendering.js +6 -100
- package/dist/cli/chat-stream.js +23 -512
- package/dist/cli/chat-ui.js +21 -199
- package/dist/cli/chat-workspace.js +2 -210
- package/dist/cli/main.js +21 -428
- package/dist/cli/managed-service-commands.js +9 -63
- package/dist/cli/managed-service.js +2 -137
- package/dist/cli/options-init-chat.js +1 -108
- package/dist/cli/options-runtime.js +1 -158
- package/dist/cli/options-serve.js +1 -282
- package/dist/cli/options.js +2 -19
- package/dist/cli/process-guards.js +1 -139
- package/dist/cli/request-tree.js +7 -296
- package/dist/cli/runtime-commands.js +12 -258
- package/dist/cli/runtime-output.js +16 -155
- package/dist/cli/server-commands.js +16 -270
- package/dist/cli/workspace.js +1 -67
- package/dist/cli.js +1 -7
- package/dist/client/acp.js +1 -1
- package/dist/client/in-process.js +1 -67
- package/dist/client/index.js +1 -2
- package/dist/client/types.js +0 -1
- package/dist/client.js +1 -1
- package/dist/contracts/core.js +1 -1
- package/dist/contracts/runtime-evaluation.js +0 -1
- package/dist/contracts/runtime-memory.js +0 -1
- package/dist/contracts/runtime-observability.js +0 -1
- package/dist/contracts/runtime-requests.js +0 -1
- package/dist/contracts/runtime-scheduling.js +0 -1
- package/dist/contracts/runtime.js +1 -27
- package/dist/contracts/types.js +1 -3
- package/dist/contracts/workspace.js +0 -1
- package/dist/flow/build-flow-graph.js +1 -50
- package/dist/flow/export-mermaid.js +2 -464
- package/dist/flow/export-sequence-mermaid.js +2 -325
- package/dist/flow/flow-graph-normalization.js +1 -214
- package/dist/flow/flow-graph-runtime.js +1 -107
- package/dist/flow/flow-graph-upstream.js +1 -494
- package/dist/flow/index.js +1 -3
- package/dist/flow/types.js +0 -1
- package/dist/index.js +1 -5
- package/dist/init-project.js +1 -1
- package/dist/knowledge/config.js +1 -32
- package/dist/knowledge/contracts.js +0 -1
- package/dist/knowledge/index.js +1 -2
- package/dist/knowledge/module.js +12 -909
- package/dist/knowledge/procedural/config.js +1 -125
- package/dist/knowledge/procedural/index.js +1 -2
- package/dist/knowledge/procedural/manager.js +9 -345
- package/dist/mcp.js +1 -2
- package/dist/package-version.d.ts +1 -1
- package/dist/package-version.js +1 -2
- package/dist/persistence/file-store.js +3 -758
- package/dist/persistence/sqlite-request-context-store.js +5 -54
- package/dist/persistence/sqlite-request-queue-store.js +10 -108
- package/dist/persistence/sqlite-runtime.js +1 -86
- package/dist/persistence/sqlite-store.js +62 -810
- package/dist/persistence/types.js +0 -1
- package/dist/projections/presentation.js +37 -206
- package/dist/projections/request-events.js +2 -502
- package/dist/projections/upstream-events.js +1 -201
- package/dist/protocol/a2a/http-discovery.js +1 -178
- package/dist/protocol/a2a/http-rpc.js +6 -622
- package/dist/protocol/a2a/http.js +1 -138
- package/dist/protocol/a2a/task-state.js +3 -317
- package/dist/protocol/acp/client.js +8 -294
- package/dist/protocol/acp/harness-client.js +1 -218
- package/dist/protocol/acp/http.js +5 -130
- package/dist/protocol/acp/server.js +1 -310
- package/dist/protocol/acp/stdio.js +2 -69
- package/dist/protocol/ag-ui/http.js +3 -378
- package/dist/protocol/mcp/server.js +1 -428
- package/dist/resource/backend/workspace-scoped-backend.js +1 -319
- package/dist/resource/isolation.js +1 -237
- package/dist/resource/mcp/tool-support.js +3 -296
- package/dist/resource/mcp-tool-support.js +1 -2
- package/dist/resource/providers/resource-provider.js +1 -215
- package/dist/resource/resource-impl.js +1 -3
- package/dist/resource/resource-types.js +0 -1
- package/dist/resource/resource.js +1 -1
- package/dist/resource/sources.js +1 -247
- package/dist/resource/tools/function-tool-resolver.js +2 -272
- package/dist/runtime/adapter/compat/deepagent-compat.js +1 -29
- package/dist/runtime/adapter/compat/openai-compatible.js +1 -55
- package/dist/runtime/adapter/direct-builtin-utility.js +2 -90
- package/dist/runtime/adapter/flow/execution-context.js +1 -71
- package/dist/runtime/adapter/flow/invocation-flow.js +8 -425
- package/dist/runtime/adapter/flow/invoke-runtime.js +1 -20
- package/dist/runtime/adapter/flow/stream-runtime.js +11 -1395
- package/dist/runtime/adapter/invocation-result.js +2 -473
- package/dist/runtime/adapter/local-tool-invocation.js +6 -638
- package/dist/runtime/adapter/middleware/context-hygiene.js +1 -83
- package/dist/runtime/adapter/middleware-assembly.js +5 -477
- package/dist/runtime/adapter/model/invocation-request.js +3 -183
- package/dist/runtime/adapter/model/message-assembly.js +1 -28
- package/dist/runtime/adapter/model/model-providers.js +23 -1115
- package/dist/runtime/adapter/model/prompted-json-tool-call-capture.js +1 -40
- package/dist/runtime/adapter/model/prompted-json-tool-policy.js +1 -22
- package/dist/runtime/adapter/resilience.js +1 -104
- package/dist/runtime/adapter/runtime-adapter-support.js +3 -141
- package/dist/runtime/adapter/runtime-shell.js +5 -166
- package/dist/runtime/adapter/stream-event-projection.js +2 -622
- package/dist/runtime/adapter/stream-text-consumption.js +1 -18
- package/dist/runtime/adapter/terminal-status.js +2 -67
- package/dist/runtime/adapter/tool/builtin-middleware-tools.js +6 -627
- package/dist/runtime/adapter/tool/declared-middleware.js +1 -154
- package/dist/runtime/adapter/tool/interrupt-policy.js +1 -34
- package/dist/runtime/adapter/tool/provider-tool.js +1 -25
- package/dist/runtime/adapter/tool/resolved-tool.js +1 -225
- package/dist/runtime/adapter/tool/tool-arguments.js +3 -486
- package/dist/runtime/adapter/tool/tool-hitl.js +1 -346
- package/dist/runtime/adapter/tool/tool-name-mapping.js +1 -128
- package/dist/runtime/adapter/tool/tool-output-artifacts.js +2 -88
- package/dist/runtime/adapter/tool/tool-replay.js +1 -37
- package/dist/runtime/adapter/tool-resolution.js +1 -86
- package/dist/runtime/adapter/upstream-configurable-keys.js +1 -2
- package/dist/runtime/agent-runtime-adapter.js +60 -2338
- package/dist/runtime/agent-runtime-assembly.js +7 -249
- package/dist/runtime/env/runtime-env.js +1 -62
- package/dist/runtime/harness/background-runtime.js +1 -8
- package/dist/runtime/harness/bindings.js +1 -58
- package/dist/runtime/harness/events/event-bus.js +1 -16
- package/dist/runtime/harness/events/event-sink.js +1 -61
- package/dist/runtime/harness/events/events.js +1 -80
- package/dist/runtime/harness/events/listener-runtime.js +1 -13
- package/dist/runtime/harness/events/runtime-event-operations.js +1 -9
- package/dist/runtime/harness/events/streaming.js +1 -100
- package/dist/runtime/harness/events/timeline.js +1 -52
- package/dist/runtime/harness/public-shapes.js +1 -186
- package/dist/runtime/harness/run/artifact-paths.js +1 -15
- package/dist/runtime/harness/run/governance.js +1 -295
- package/dist/runtime/harness/run/helpers.js +1 -71
- package/dist/runtime/harness/run/inspection.js +1 -409
- package/dist/runtime/harness/run/operator-overview.js +1 -80
- package/dist/runtime/harness/run/queue-diagnostics.js +1 -15
- package/dist/runtime/harness/run/recovery.js +1 -162
- package/dist/runtime/harness/run/resources.js +1 -60
- package/dist/runtime/harness/run/resume.js +1 -56
- package/dist/runtime/harness/run/routing.js +1 -48
- package/dist/runtime/harness/run/run-lifecycle.js +1 -66
- package/dist/runtime/harness/run/run-operations.js +1 -217
- package/dist/runtime/harness/run/run-queue.js +1 -43
- package/dist/runtime/harness/run/run-slot-acquisition.js +1 -157
- package/dist/runtime/harness/run/session-records.js +1 -97
- package/dist/runtime/harness/run/start-run.js +1 -120
- package/dist/runtime/harness/run/startup-runtime.js +1 -69
- package/dist/runtime/harness/run/stream-run.js +8 -1418
- package/dist/runtime/harness/run/surface-semantics.js +1 -79
- package/dist/runtime/harness/runtime-defaults.js +1 -39
- package/dist/runtime/harness/system/boundary-analysis.js +1 -234
- package/dist/runtime/harness/system/health-monitor.js +1 -258
- package/dist/runtime/harness/system/inventory.js +1 -129
- package/dist/runtime/harness/system/mem0-ingestion-sync.js +5 -345
- package/dist/runtime/harness/system/policy-engine.js +1 -175
- package/dist/runtime/harness/system/runtime-memory-candidates.js +4 -110
- package/dist/runtime/harness/system/runtime-memory-consolidation.js +1 -51
- package/dist/runtime/harness/system/runtime-memory-manager.js +10 -693
- package/dist/runtime/harness/system/runtime-memory-policy.js +1 -155
- package/dist/runtime/harness/system/runtime-memory-records.js +11 -577
- package/dist/runtime/harness/system/runtime-memory-sync.js +5 -206
- package/dist/runtime/harness/system/session-memory-sync.js +3 -113
- package/dist/runtime/harness/system/skill-requirements.js +1 -112
- package/dist/runtime/harness/system/store.js +9 -365
- package/dist/runtime/harness/tool-gateway/index.js +1 -2
- package/dist/runtime/harness/tool-gateway/policy.js +1 -45
- package/dist/runtime/harness/tool-gateway/validation.js +1 -176
- package/dist/runtime/harness/tool-schema.js +1 -3
- package/dist/runtime/harness.js +3 -1490
- package/dist/runtime/index.js +1 -3
- package/dist/runtime/layout/runtime-layout.js +1 -31
- package/dist/runtime/maintenance/checkpoint-maintenance.js +2 -178
- package/dist/runtime/maintenance/file-checkpoint-saver.js +1 -106
- package/dist/runtime/maintenance/runtime-record-maintenance.js +2 -169
- package/dist/runtime/maintenance/sqlite-checkpoint-saver.js +4 -289
- package/dist/runtime/parsing/output-content.js +10 -550
- package/dist/runtime/parsing/output-parsing.js +1 -4
- package/dist/runtime/parsing/output-recovery.js +3 -213
- package/dist/runtime/parsing/output-tool-args.js +7 -663
- package/dist/runtime/parsing/stream-event-parsing.js +3 -362
- package/dist/runtime/prompts/runtime-prompts.js +4 -73
- package/dist/runtime/scheduling/system-schedule-manager.js +11 -532
- package/dist/runtime/skills/skill-metadata.js +1 -197
- package/dist/runtime/startup-tracing.js +2 -37
- package/dist/runtime/support/compiled-binding.js +1 -290
- package/dist/runtime/support/embedding-models.js +1 -118
- package/dist/runtime/support/harness-support.js +5 -137
- package/dist/runtime/support/llamaindex.js +1 -108
- package/dist/runtime/support/runtime-adapter-options.js +1 -29
- package/dist/runtime/support/runtime-factories.js +1 -51
- package/dist/runtime/support/vector-stores.js +9 -270
- package/dist/scaffold/init-project.js +54 -233
- package/dist/tooling/extensions.js +1 -311
- package/dist/tooling/module-loader.js +1 -55
- package/dist/tools.js +1 -176
- package/dist/utils/agent-display.js +1 -18
- package/dist/utils/bundled-text.js +4 -39
- package/dist/utils/compiled-binding.js +1 -33
- package/dist/utils/fs.js +2 -45
- package/dist/utils/id.js +1 -9
- package/dist/utils/message-content.js +1 -30
- package/dist/utils/object.js +1 -6
- package/dist/workspace/agent-binding-compiler.js +3 -613
- package/dist/workspace/compile.js +1 -472
- package/dist/workspace/framework-contract-validation.js +2 -322
- package/dist/workspace/index.js +1 -1
- package/dist/workspace/object-loader-paths.js +1 -71
- package/dist/workspace/object-loader-readers.js +1 -187
- package/dist/workspace/object-loader.js +1 -754
- package/dist/workspace/resource-compilers.js +1 -374
- package/dist/workspace/support/agent-capabilities.js +1 -37
- package/dist/workspace/support/agent-execution-config.js +1 -44
- package/dist/workspace/support/discovery.js +1 -147
- package/dist/workspace/support/source-collectors.js +1 -30
- package/dist/workspace/support/source-protocols.js +2 -192
- package/dist/workspace/support/workspace-ref-utils.js +1 -362
- package/dist/workspace/tool-hydration.js +1 -280
- package/dist/workspace/validate.js +1 -99
- package/dist/workspace/yaml-object-reader.js +1 -285
- package/package.json +7 -3
|
@@ -1,1418 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
import { createContentBlocksItem, createToolResultKey, } from "../events/streaming.js";
|
|
10
|
-
import { projectRuntimeSurfaceFromSingleUpstreamEvent } from "./inspection.js";
|
|
11
|
-
import { formatAgentName } from "../../../utils/agent-display.js";
|
|
12
|
-
function createInitialPlanState(sessionId, requestId, updatedAt) {
|
|
13
|
-
return {
|
|
14
|
-
sessionId,
|
|
15
|
-
requestId,
|
|
16
|
-
version: 0,
|
|
17
|
-
updatedAt,
|
|
18
|
-
items: [],
|
|
19
|
-
summary: {
|
|
20
|
-
total: 0,
|
|
21
|
-
pending: 0,
|
|
22
|
-
inProgress: 0,
|
|
23
|
-
completed: 0,
|
|
24
|
-
failed: 0,
|
|
25
|
-
cancelled: 0,
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
function planStateHasUnfinishedItems(planState) {
|
|
30
|
-
if (!planState) {
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
return planState.summary.pending > 0 || planState.summary.inProgress > 0;
|
|
34
|
-
}
|
|
35
|
-
function planStateHasActiveItems(planState) {
|
|
36
|
-
return planStateHasUnfinishedItems(planState);
|
|
37
|
-
}
|
|
38
|
-
function isPlanToolName(toolName) {
|
|
39
|
-
const normalized = typeof toolName === "string" ? toolName.trim().toLowerCase().replace(/[\s-]+/gu, "_") : "";
|
|
40
|
-
return normalized === "write_todos"
|
|
41
|
-
|| normalized === "read_todos"
|
|
42
|
-
|| normalized === "tool_call_write_todos"
|
|
43
|
-
|| normalized === "tool_call_read_todos"
|
|
44
|
-
|| normalized === "call_write_todos"
|
|
45
|
-
|| normalized === "call_read_todos";
|
|
46
|
-
}
|
|
47
|
-
function isSubstantiveTerminalAssistantOutput(value) {
|
|
48
|
-
const normalized = sanitizeVisibleText(value).trim();
|
|
49
|
-
if (normalized.length < 80) {
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
function inferPlanItemStatusFromTerminalAssistantOutput(value, options = {}) {
|
|
55
|
-
const terminalStatus = readTerminalExecutionStatus(value);
|
|
56
|
-
if (terminalStatus) {
|
|
57
|
-
return mapTerminalStatusToPlanItemStatus(terminalStatus);
|
|
58
|
-
}
|
|
59
|
-
const normalized = sanitizeVisibleText(value).trim().toLowerCase();
|
|
60
|
-
if (!normalized) {
|
|
61
|
-
return null;
|
|
62
|
-
}
|
|
63
|
-
if (normalized.startsWith("runtime_error=")) {
|
|
64
|
-
return "failed";
|
|
65
|
-
}
|
|
66
|
-
if (options.hasSuccessfulExecutionEvidence !== true) {
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
return isSubstantiveTerminalAssistantOutput(value) ? "completed" : null;
|
|
70
|
-
}
|
|
71
|
-
function mapTerminalStatusToObservedPlanItemStatus(status, sawSuccessfulToolResult) {
|
|
72
|
-
if (status === "blocked" && sawSuccessfulToolResult) {
|
|
73
|
-
return "completed";
|
|
74
|
-
}
|
|
75
|
-
return mapTerminalStatusToPlanItemStatus(status);
|
|
76
|
-
}
|
|
77
|
-
function reconcilePlanStateToTerminalStatus(planState, status, updatedAt) {
|
|
78
|
-
const items = planState.items.map((item) => ({
|
|
79
|
-
...item,
|
|
80
|
-
status: item.status === "pending" || item.status === "in_progress"
|
|
81
|
-
? status
|
|
82
|
-
: item.status,
|
|
83
|
-
}));
|
|
84
|
-
const summary = {
|
|
85
|
-
total: items.length,
|
|
86
|
-
pending: 0,
|
|
87
|
-
inProgress: 0,
|
|
88
|
-
completed: items.filter((item) => item.status === "completed").length,
|
|
89
|
-
failed: items.filter((item) => item.status === "failed").length,
|
|
90
|
-
cancelled: items.filter((item) => item.status === "cancelled").length,
|
|
91
|
-
};
|
|
92
|
-
return {
|
|
93
|
-
...planState,
|
|
94
|
-
version: planState.version + 1,
|
|
95
|
-
updatedAt,
|
|
96
|
-
items,
|
|
97
|
-
summary,
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
function getPlanStateFromToolResult(input) {
|
|
101
|
-
if (typeof input.output !== "object" || input.output === null) {
|
|
102
|
-
return null;
|
|
103
|
-
}
|
|
104
|
-
const summary = input.output.summary;
|
|
105
|
-
if (summary && typeof summary === "object" && Array.isArray(summary.items)) {
|
|
106
|
-
const typedSummary = summary;
|
|
107
|
-
if (typedSummary.summary && typeof typedSummary.summary === "object") {
|
|
108
|
-
return {
|
|
109
|
-
sessionId: input.sessionId,
|
|
110
|
-
requestId: input.requestId,
|
|
111
|
-
version: input.version,
|
|
112
|
-
updatedAt: input.updatedAt,
|
|
113
|
-
items: typedSummary.items,
|
|
114
|
-
summary: typedSummary.summary,
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
const snapshot = summarizeBuiltinWriteTodosArgs(input.output);
|
|
119
|
-
return buildRequestPlanState({
|
|
120
|
-
sessionId: input.sessionId,
|
|
121
|
-
requestId: input.requestId,
|
|
122
|
-
updatedAt: input.updatedAt,
|
|
123
|
-
version: input.version,
|
|
124
|
-
snapshot,
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
function getStructuredPlanStateFromToolResult(input) {
|
|
128
|
-
if (typeof input.output !== "object" || input.output === null) {
|
|
129
|
-
return null;
|
|
130
|
-
}
|
|
131
|
-
const summary = input.output.summary;
|
|
132
|
-
if (!summary || typeof summary !== "object" || !Array.isArray(summary.items)) {
|
|
133
|
-
return null;
|
|
134
|
-
}
|
|
135
|
-
const typedSummary = summary;
|
|
136
|
-
if (!typedSummary.summary || typeof typedSummary.summary !== "object") {
|
|
137
|
-
return null;
|
|
138
|
-
}
|
|
139
|
-
return {
|
|
140
|
-
sessionId: input.sessionId,
|
|
141
|
-
requestId: input.requestId,
|
|
142
|
-
version: input.version,
|
|
143
|
-
updatedAt: input.updatedAt,
|
|
144
|
-
items: typedSummary.items,
|
|
145
|
-
summary: typedSummary.summary,
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
function buildPlanStateSignature(planState) {
|
|
149
|
-
return JSON.stringify({
|
|
150
|
-
items: planState.items,
|
|
151
|
-
summary: planState.summary,
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
function recomputePlanSummary(items) {
|
|
155
|
-
return {
|
|
156
|
-
total: items.length,
|
|
157
|
-
pending: items.filter((item) => item.status === "pending").length,
|
|
158
|
-
inProgress: items.filter((item) => item.status === "in_progress").length,
|
|
159
|
-
completed: items.filter((item) => item.status === "completed").length,
|
|
160
|
-
failed: items.filter((item) => item.status === "failed").length,
|
|
161
|
-
cancelled: items.filter((item) => item.status === "cancelled").length,
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
function normalizePlanItemKey(item) {
|
|
165
|
-
return typeof item.id === "string" && item.id.trim().length > 0
|
|
166
|
-
? `id:${item.id.trim()}`
|
|
167
|
-
: `content:${item.content.trim().toLowerCase()}`;
|
|
168
|
-
}
|
|
169
|
-
function mergePartialPlanState(currentPlanState, incomingPlanState) {
|
|
170
|
-
if (!currentPlanState || incomingPlanState.items.length >= currentPlanState.items.length) {
|
|
171
|
-
return incomingPlanState;
|
|
172
|
-
}
|
|
173
|
-
const currentByKey = new Map(currentPlanState.items.map((item, index) => [normalizePlanItemKey(item), { item, index }]));
|
|
174
|
-
const incomingByKey = new Map();
|
|
175
|
-
for (const incomingItem of incomingPlanState.items) {
|
|
176
|
-
const key = normalizePlanItemKey(incomingItem);
|
|
177
|
-
if (!currentByKey.has(key)) {
|
|
178
|
-
const currentStructuredIds = countStructuredTodoIds(currentPlanState.items);
|
|
179
|
-
const incomingStructuredIds = countStructuredTodoIds(incomingPlanState.items);
|
|
180
|
-
if (currentStructuredIds > 0 && incomingStructuredIds === 0) {
|
|
181
|
-
return currentPlanState;
|
|
182
|
-
}
|
|
183
|
-
const mergedItems = currentPlanState.items.map((item, index) => {
|
|
184
|
-
const incomingByIndex = incomingPlanState.items[index];
|
|
185
|
-
return incomingByIndex ? { ...item, status: incomingByIndex.status } : item;
|
|
186
|
-
});
|
|
187
|
-
return {
|
|
188
|
-
...incomingPlanState,
|
|
189
|
-
items: mergedItems,
|
|
190
|
-
summary: recomputePlanSummary(mergedItems),
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
incomingByKey.set(key, incomingItem);
|
|
194
|
-
}
|
|
195
|
-
const mergedItems = currentPlanState.items.map((item) => {
|
|
196
|
-
const incomingItem = incomingByKey.get(normalizePlanItemKey(item));
|
|
197
|
-
return incomingItem ? { ...item, ...incomingItem } : item;
|
|
198
|
-
});
|
|
199
|
-
return {
|
|
200
|
-
...incomingPlanState,
|
|
201
|
-
items: mergedItems,
|
|
202
|
-
summary: recomputePlanSummary(mergedItems),
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
function countStructuredTodoIds(items) {
|
|
206
|
-
return items.filter((item) => typeof item.id === "string" && item.id.length > 0).length;
|
|
207
|
-
}
|
|
208
|
-
async function emitPlanStateUpdate(options, agentId, planState) {
|
|
209
|
-
await options.saveRequestPlanState?.(options.sessionId, options.requestId, planState);
|
|
210
|
-
return [{
|
|
211
|
-
type: "plan-state",
|
|
212
|
-
sessionId: options.sessionId,
|
|
213
|
-
requestId: options.requestId,
|
|
214
|
-
agentId,
|
|
215
|
-
planState,
|
|
216
|
-
}];
|
|
217
|
-
}
|
|
218
|
-
async function refreshPlanStateFromPersistence(options, currentPlanState) {
|
|
219
|
-
const persistedPlanState = await options.loadRequestPlanState?.(options.sessionId, options.requestId);
|
|
220
|
-
if (!persistedPlanState) {
|
|
221
|
-
return currentPlanState;
|
|
222
|
-
}
|
|
223
|
-
if (!currentPlanState || persistedPlanState.version >= currentPlanState.version) {
|
|
224
|
-
return persistedPlanState;
|
|
225
|
-
}
|
|
226
|
-
return currentPlanState;
|
|
227
|
-
}
|
|
228
|
-
function shouldEmitPlanState(currentPlanState, nextPlanState) {
|
|
229
|
-
if (!currentPlanState || currentPlanState.items.length === 0) {
|
|
230
|
-
return true;
|
|
231
|
-
}
|
|
232
|
-
if (nextPlanState.items.length === 0) {
|
|
233
|
-
return false;
|
|
234
|
-
}
|
|
235
|
-
const currentStructuredIds = countStructuredTodoIds(currentPlanState.items);
|
|
236
|
-
const nextStructuredIds = countStructuredTodoIds(nextPlanState.items);
|
|
237
|
-
if (currentStructuredIds > 0
|
|
238
|
-
&& nextStructuredIds === 0
|
|
239
|
-
&& nextPlanState.items.length < currentPlanState.items.length) {
|
|
240
|
-
return false;
|
|
241
|
-
}
|
|
242
|
-
return true;
|
|
243
|
-
}
|
|
244
|
-
function extractTodosArray(value) {
|
|
245
|
-
if (typeof value !== "object" || value === null) {
|
|
246
|
-
return null;
|
|
247
|
-
}
|
|
248
|
-
const record = value;
|
|
249
|
-
if (Array.isArray(record.todos)) {
|
|
250
|
-
return record.todos;
|
|
251
|
-
}
|
|
252
|
-
if (typeof record.output === "object" && record.output !== null) {
|
|
253
|
-
const nested = record.output.todos;
|
|
254
|
-
if (Array.isArray(nested)) {
|
|
255
|
-
return nested;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
return null;
|
|
259
|
-
}
|
|
260
|
-
function getPlanStateFromUpstreamEvent(input) {
|
|
261
|
-
if (typeof input.event !== "object" || input.event === null) {
|
|
262
|
-
return null;
|
|
263
|
-
}
|
|
264
|
-
const typed = input.event;
|
|
265
|
-
const isWriteTodosStart = typed.event === "on_tool_start"
|
|
266
|
-
&& typed.name === "write_todos";
|
|
267
|
-
const todos = (isWriteTodosStart ? extractTodosArray(typed.data?.input) : null)
|
|
268
|
-
?? extractTodosArray(typed.data?.output)
|
|
269
|
-
?? extractTodosArray(typed.data?.chunk);
|
|
270
|
-
if (!todos) {
|
|
271
|
-
return null;
|
|
272
|
-
}
|
|
273
|
-
return buildRequestPlanState({
|
|
274
|
-
sessionId: input.sessionId,
|
|
275
|
-
requestId: input.requestId,
|
|
276
|
-
updatedAt: input.updatedAt,
|
|
277
|
-
version: input.version,
|
|
278
|
-
snapshot: summarizeBuiltinWriteTodosArgs({ todos }),
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
function getLatestPlanStateFromExecutedToolResults(input) {
|
|
282
|
-
const executedToolResults = Array.isArray(input.metadata?.executedToolResults)
|
|
283
|
-
? input.metadata.executedToolResults
|
|
284
|
-
: [];
|
|
285
|
-
for (const latest of [...executedToolResults].reverse()) {
|
|
286
|
-
const structured = getStructuredPlanStateFromToolResult({
|
|
287
|
-
sessionId: input.sessionId,
|
|
288
|
-
requestId: input.requestId,
|
|
289
|
-
output: latest.output,
|
|
290
|
-
version: input.version,
|
|
291
|
-
updatedAt: input.updatedAt,
|
|
292
|
-
});
|
|
293
|
-
if (structured) {
|
|
294
|
-
return structured;
|
|
295
|
-
}
|
|
296
|
-
if (latest.toolName === "write_todos") {
|
|
297
|
-
return getPlanStateFromToolResult({
|
|
298
|
-
sessionId: input.sessionId,
|
|
299
|
-
requestId: input.requestId,
|
|
300
|
-
output: latest.output,
|
|
301
|
-
version: input.version,
|
|
302
|
-
updatedAt: input.updatedAt,
|
|
303
|
-
});
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
return null;
|
|
307
|
-
}
|
|
308
|
-
function normalizeStreamChunk(chunk) {
|
|
309
|
-
if (typeof chunk === "string") {
|
|
310
|
-
if (chunk.startsWith(AGENT_INTERRUPT_SENTINEL_PREFIX)) {
|
|
311
|
-
return { kind: "interrupt", content: chunk.slice(AGENT_INTERRUPT_SENTINEL_PREFIX.length) };
|
|
312
|
-
}
|
|
313
|
-
return { kind: "content", content: chunk };
|
|
314
|
-
}
|
|
315
|
-
if (chunk.kind === "commentary") {
|
|
316
|
-
return { kind: "commentary", content: chunk.content ?? "", agentId: chunk.agentId };
|
|
317
|
-
}
|
|
318
|
-
if (chunk.kind === "upstream-event") {
|
|
319
|
-
return { kind: "upstream-event", event: (chunk.event ?? {}), agentId: chunk.agentId };
|
|
320
|
-
}
|
|
321
|
-
if (chunk.kind === "interrupt") {
|
|
322
|
-
return { kind: "interrupt", content: chunk.content, agentId: chunk.agentId };
|
|
323
|
-
}
|
|
324
|
-
if (chunk.kind === "reasoning") {
|
|
325
|
-
return { kind: "reasoning", content: chunk.content ?? "", agentId: chunk.agentId };
|
|
326
|
-
}
|
|
327
|
-
if (chunk.kind === "tool-result") {
|
|
328
|
-
return {
|
|
329
|
-
kind: "tool-result",
|
|
330
|
-
toolName: chunk.toolName ?? "unknown_tool",
|
|
331
|
-
output: chunk.output,
|
|
332
|
-
isError: chunk.isError,
|
|
333
|
-
agentId: chunk.agentId,
|
|
334
|
-
};
|
|
335
|
-
}
|
|
336
|
-
if (chunk.kind === "profile") {
|
|
337
|
-
return {
|
|
338
|
-
kind: "profile",
|
|
339
|
-
step: chunk.step,
|
|
340
|
-
agentId: chunk.agentId,
|
|
341
|
-
};
|
|
342
|
-
}
|
|
343
|
-
return { kind: "content", content: chunk.content ?? "", agentId: chunk.agentId };
|
|
344
|
-
}
|
|
345
|
-
function normalizeCommentaryText(value) {
|
|
346
|
-
return value
|
|
347
|
-
.split("\n")
|
|
348
|
-
.map((line) => {
|
|
349
|
-
const leading = /^\s*/.exec(line)?.[0] ?? "";
|
|
350
|
-
const body = line.slice(leading.length).replace(/[^\S\n]+/g, " ").trimEnd();
|
|
351
|
-
return `${leading}${body}`;
|
|
352
|
-
})
|
|
353
|
-
.join("\n")
|
|
354
|
-
.replace(/\n{3,}/g, "\n\n")
|
|
355
|
-
.trim();
|
|
356
|
-
}
|
|
357
|
-
function ensureCommentarySentence(value) {
|
|
358
|
-
const normalized = normalizeCommentaryText(value);
|
|
359
|
-
if (!normalized) {
|
|
360
|
-
return normalized;
|
|
361
|
-
}
|
|
362
|
-
return /[.!?]$/.test(normalized) ? normalized : `${normalized}.`;
|
|
363
|
-
}
|
|
364
|
-
function summarizePlanState(planState) {
|
|
365
|
-
if (planState.summary.total <= 0) {
|
|
366
|
-
return null;
|
|
367
|
-
}
|
|
368
|
-
const statusMarker = (status) => {
|
|
369
|
-
switch (status) {
|
|
370
|
-
case "completed":
|
|
371
|
-
return "[x]";
|
|
372
|
-
case "in_progress":
|
|
373
|
-
return "[~]";
|
|
374
|
-
case "failed":
|
|
375
|
-
return "[!]";
|
|
376
|
-
case "cancelled":
|
|
377
|
-
return "[-]";
|
|
378
|
-
default:
|
|
379
|
-
return "[ ]";
|
|
380
|
-
}
|
|
381
|
-
};
|
|
382
|
-
const items = planState.items.slice(0, 6).map((item) => ` ${statusMarker(item.status)} ${item.content}`);
|
|
383
|
-
const suffix = planState.items.length > items.length ? [` ... ${planState.items.length - items.length} more`] : [];
|
|
384
|
-
return ["TODO", ...items, ...suffix].join("\n");
|
|
385
|
-
}
|
|
386
|
-
function summarizePlanStateTerminalTransitionEntries(previousPlanState, nextPlanState) {
|
|
387
|
-
const previousByKey = new Map((previousPlanState?.items ?? []).map((item) => [normalizePlanItemKey(item), item]));
|
|
388
|
-
const terminalLabel = (status) => {
|
|
389
|
-
switch (status) {
|
|
390
|
-
case "completed":
|
|
391
|
-
return "completed";
|
|
392
|
-
case "failed":
|
|
393
|
-
return "failed";
|
|
394
|
-
case "cancelled":
|
|
395
|
-
return "cancelled";
|
|
396
|
-
default:
|
|
397
|
-
return null;
|
|
398
|
-
}
|
|
399
|
-
};
|
|
400
|
-
return nextPlanState.items.flatMap((item) => {
|
|
401
|
-
const label = terminalLabel(item.status);
|
|
402
|
-
if (!label) {
|
|
403
|
-
return [];
|
|
404
|
-
}
|
|
405
|
-
const previousStatus = previousByKey.get(normalizePlanItemKey(item))?.status;
|
|
406
|
-
if (previousStatus === item.status) {
|
|
407
|
-
return [];
|
|
408
|
-
}
|
|
409
|
-
return [{
|
|
410
|
-
key: normalizePlanItemKey(item),
|
|
411
|
-
text: `TODO ${label}: ${item.content}.`,
|
|
412
|
-
item,
|
|
413
|
-
}];
|
|
414
|
-
});
|
|
415
|
-
}
|
|
416
|
-
function buildPlanStateProgression(previousPlanState, nextPlanState) {
|
|
417
|
-
if (!previousPlanState) {
|
|
418
|
-
return [{ planState: nextPlanState }];
|
|
419
|
-
}
|
|
420
|
-
const terminalEntries = summarizePlanStateTerminalTransitionEntries(previousPlanState, nextPlanState);
|
|
421
|
-
if (terminalEntries.length === 0) {
|
|
422
|
-
return [{ planState: nextPlanState }];
|
|
423
|
-
}
|
|
424
|
-
const mergedPlanState = mergePartialPlanState(previousPlanState, nextPlanState);
|
|
425
|
-
const workingItems = [];
|
|
426
|
-
const workingItemByKey = new Map();
|
|
427
|
-
const previousItems = previousPlanState?.items ?? [];
|
|
428
|
-
const nextItems = mergedPlanState?.items ?? [];
|
|
429
|
-
for (const item of previousItems) {
|
|
430
|
-
workingItemByKey.set(normalizePlanItemKey(item), workingItems.length);
|
|
431
|
-
workingItems.push({ ...item });
|
|
432
|
-
}
|
|
433
|
-
for (const item of nextItems) {
|
|
434
|
-
const key = normalizePlanItemKey(item);
|
|
435
|
-
const index = workingItemByKey.get(key);
|
|
436
|
-
if (index === undefined) {
|
|
437
|
-
workingItemByKey.set(key, workingItems.length);
|
|
438
|
-
workingItems.push({ ...item });
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
const seenTransitionKeys = new Set();
|
|
442
|
-
const result = [];
|
|
443
|
-
for (const entry of terminalEntries) {
|
|
444
|
-
if (!entry.item || seenTransitionKeys.has(entry.key)) {
|
|
445
|
-
continue;
|
|
446
|
-
}
|
|
447
|
-
seenTransitionKeys.add(entry.key);
|
|
448
|
-
const index = workingItems.findIndex((item) => normalizePlanItemKey(item) === entry.key);
|
|
449
|
-
const nextItem = nextPlanState.items.find((item) => normalizePlanItemKey(item) === entry.key);
|
|
450
|
-
if (!nextItem) {
|
|
451
|
-
continue;
|
|
452
|
-
}
|
|
453
|
-
if (index >= 0) {
|
|
454
|
-
workingItems[index] = { ...workingItems[index], ...nextItem };
|
|
455
|
-
}
|
|
456
|
-
else {
|
|
457
|
-
workingItems.push({ ...nextItem });
|
|
458
|
-
}
|
|
459
|
-
result.push({
|
|
460
|
-
planState: {
|
|
461
|
-
...nextPlanState,
|
|
462
|
-
items: workingItems.map((item) => ({ ...item })),
|
|
463
|
-
summary: recomputePlanSummary(workingItems),
|
|
464
|
-
},
|
|
465
|
-
commentary: entry.text,
|
|
466
|
-
});
|
|
467
|
-
}
|
|
468
|
-
if (result.length === 0) {
|
|
469
|
-
return [{ planState: nextPlanState }];
|
|
470
|
-
}
|
|
471
|
-
return result;
|
|
472
|
-
}
|
|
473
|
-
function createSurfaceCommentary(surfaceItem) {
|
|
474
|
-
const name = normalizeCommentaryText(surfaceItem.name);
|
|
475
|
-
if (!name) {
|
|
476
|
-
return null;
|
|
477
|
-
}
|
|
478
|
-
if (surfaceItem.kind === "tool") {
|
|
479
|
-
if (surfaceItem.status === "started") {
|
|
480
|
-
return `Running tool ${name}.`;
|
|
481
|
-
}
|
|
482
|
-
if (surfaceItem.status === "completed") {
|
|
483
|
-
if (name.toLowerCase() === "task") {
|
|
484
|
-
return null;
|
|
485
|
-
}
|
|
486
|
-
return `Tool ${name} completed.`;
|
|
487
|
-
}
|
|
488
|
-
if (surfaceItem.status === "failed") {
|
|
489
|
-
return `Tool ${name} failed.`;
|
|
490
|
-
}
|
|
491
|
-
return null;
|
|
492
|
-
}
|
|
493
|
-
if (surfaceItem.kind === "skill") {
|
|
494
|
-
if (surfaceItem.status === "started") {
|
|
495
|
-
return `Applying skill ${name}.`;
|
|
496
|
-
}
|
|
497
|
-
if (surfaceItem.status === "completed") {
|
|
498
|
-
return `Skill ${name} applied.`;
|
|
499
|
-
}
|
|
500
|
-
if (surfaceItem.status === "failed") {
|
|
501
|
-
return `Skill ${name} failed.`;
|
|
502
|
-
}
|
|
503
|
-
return null;
|
|
504
|
-
}
|
|
505
|
-
if (surfaceItem.kind === "agent") {
|
|
506
|
-
if (surfaceItem.status === "started") {
|
|
507
|
-
return `Delegating work to ${name}.`;
|
|
508
|
-
}
|
|
509
|
-
if (surfaceItem.status === "completed") {
|
|
510
|
-
return null;
|
|
511
|
-
}
|
|
512
|
-
if (surfaceItem.status === "failed") {
|
|
513
|
-
return `Delegation to ${name} failed.`;
|
|
514
|
-
}
|
|
515
|
-
return null;
|
|
516
|
-
}
|
|
517
|
-
if (surfaceItem.kind === "llm") {
|
|
518
|
-
if (surfaceItem.status === "started") {
|
|
519
|
-
return `Running model ${name}.`;
|
|
520
|
-
}
|
|
521
|
-
if (surfaceItem.status === "completed") {
|
|
522
|
-
return `Model ${name} completed.`;
|
|
523
|
-
}
|
|
524
|
-
if (surfaceItem.status === "failed") {
|
|
525
|
-
return `Model ${name} failed.`;
|
|
526
|
-
}
|
|
527
|
-
return null;
|
|
528
|
-
}
|
|
529
|
-
if (surfaceItem.kind === "memory") {
|
|
530
|
-
if (surfaceItem.status === "started") {
|
|
531
|
-
return `Checking memory ${name}.`;
|
|
532
|
-
}
|
|
533
|
-
if (surfaceItem.status === "completed") {
|
|
534
|
-
return ensureCommentarySentence(`Finished checking memory ${name}`);
|
|
535
|
-
}
|
|
536
|
-
if (surfaceItem.status === "failed") {
|
|
537
|
-
return `Memory ${name} failed.`;
|
|
538
|
-
}
|
|
539
|
-
return null;
|
|
540
|
-
}
|
|
541
|
-
return null;
|
|
542
|
-
}
|
|
543
|
-
function createProfileStepCommentary(step) {
|
|
544
|
-
const name = normalizeCommentaryText(step.name);
|
|
545
|
-
if (!name || step.status !== "started") {
|
|
546
|
-
return null;
|
|
547
|
-
}
|
|
548
|
-
if (step.kind === "memory") {
|
|
549
|
-
return `Checking memory ${name}.`;
|
|
550
|
-
}
|
|
551
|
-
if (step.kind === "agent" && step.action === "invoke") {
|
|
552
|
-
return "Running model invocation.";
|
|
553
|
-
}
|
|
554
|
-
if (step.kind === "agent" && step.action === "start") {
|
|
555
|
-
return "Starting runtime stream.";
|
|
556
|
-
}
|
|
557
|
-
if (step.kind === "agent" && step.action === "startup") {
|
|
558
|
-
return `Preparing ${name}.`;
|
|
559
|
-
}
|
|
560
|
-
if (step.kind === "tool") {
|
|
561
|
-
if (step.status === "started") {
|
|
562
|
-
return `Running tool ${name}.`;
|
|
563
|
-
}
|
|
564
|
-
if (step.status === "completed") {
|
|
565
|
-
return `Tool ${name} completed.`;
|
|
566
|
-
}
|
|
567
|
-
if (step.status === "failed") {
|
|
568
|
-
return `Tool ${name} failed.`;
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
return null;
|
|
572
|
-
}
|
|
573
|
-
function isOpenAICompatibleStreamingCompatibilityError(binding, error) {
|
|
574
|
-
const primaryModel = getBindingPrimaryModel(binding);
|
|
575
|
-
if (primaryModel?.provider !== "openai-compatible") {
|
|
576
|
-
return false;
|
|
577
|
-
}
|
|
578
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
579
|
-
return message.toLowerCase().includes("received empty response from chat model call");
|
|
580
|
-
}
|
|
581
|
-
function createRuntimeMemoryRecallSteps(sessionId, requestId, items) {
|
|
582
|
-
if (items.length === 0) {
|
|
583
|
-
return [];
|
|
584
|
-
}
|
|
585
|
-
const timestamp = new Date().toISOString();
|
|
586
|
-
const scopes = Array.from(new Set(items.map((item) => item.scope)));
|
|
587
|
-
const primaryScope = scopes.length === 1 ? scopes[0] : "mixed";
|
|
588
|
-
const primarySummary = items[0]?.summary;
|
|
589
|
-
const name = scopes.length === 1 ? `durable ${primaryScope} memory` : "durable memory";
|
|
590
|
-
const detail = {
|
|
591
|
-
source: "runtime-durable-memory",
|
|
592
|
-
scope: primaryScope,
|
|
593
|
-
scopes,
|
|
594
|
-
count: items.length,
|
|
595
|
-
summaries: items.map((item) => item.summary).slice(0, 5),
|
|
596
|
-
...(primarySummary ? { summary: primarySummary } : {}),
|
|
597
|
-
};
|
|
598
|
-
const startedStep = {
|
|
599
|
-
id: `runtime-memory-recall:${requestId}:started`,
|
|
600
|
-
kind: "memory",
|
|
601
|
-
name,
|
|
602
|
-
action: "recall",
|
|
603
|
-
status: "started",
|
|
604
|
-
occurredAt: timestamp,
|
|
605
|
-
detail,
|
|
606
|
-
};
|
|
607
|
-
const completedStep = {
|
|
608
|
-
...startedStep,
|
|
609
|
-
id: `runtime-memory-recall:${requestId}:completed`,
|
|
610
|
-
status: "completed",
|
|
611
|
-
};
|
|
612
|
-
return [
|
|
613
|
-
{
|
|
614
|
-
type: "profile-step",
|
|
615
|
-
sessionId,
|
|
616
|
-
requestId,
|
|
617
|
-
step: startedStep,
|
|
618
|
-
},
|
|
619
|
-
{
|
|
620
|
-
type: "profile-step",
|
|
621
|
-
sessionId,
|
|
622
|
-
requestId,
|
|
623
|
-
step: completedStep,
|
|
624
|
-
},
|
|
625
|
-
];
|
|
626
|
-
}
|
|
627
|
-
function readObject(value) {
|
|
628
|
-
return typeof value === "object" && value !== null ? value : null;
|
|
629
|
-
}
|
|
630
|
-
function isFallbackSafeUpstreamStartupEvent(event, rootAgentId) {
|
|
631
|
-
const typed = readObject(event);
|
|
632
|
-
const eventName = typeof typed?.event === "string" ? typed.event : "";
|
|
633
|
-
if (eventName.startsWith("on_chat_model_") || eventName.startsWith("on_llm_")) {
|
|
634
|
-
return true;
|
|
635
|
-
}
|
|
636
|
-
if (!eventName.startsWith("on_chain_")) {
|
|
637
|
-
return false;
|
|
638
|
-
}
|
|
639
|
-
if (typed?.run_type === "tool") {
|
|
640
|
-
return false;
|
|
641
|
-
}
|
|
642
|
-
const name = typeof typed?.name === "string" ? typed.name : "";
|
|
643
|
-
const metadata = readObject(typed?.metadata);
|
|
644
|
-
const langgraphNode = typeof metadata?.langgraph_node === "string" ? metadata.langgraph_node : "";
|
|
645
|
-
const langchainAgentName = typeof metadata?.lc_agent_name === "string" ? metadata.lc_agent_name : "";
|
|
646
|
-
return (name === "__start__"
|
|
647
|
-
|| langgraphNode === "__start__"
|
|
648
|
-
|| langgraphNode === "model_request"
|
|
649
|
-
|| name.endsWith(".before_agent")
|
|
650
|
-
|| name.endsWith(".before_model")
|
|
651
|
-
|| (name === rootAgentId && langchainAgentName === rootAgentId));
|
|
652
|
-
}
|
|
653
|
-
function planStateHasAnyItems(planState) {
|
|
654
|
-
return (planState?.items.length ?? 0) > 0;
|
|
655
|
-
}
|
|
656
|
-
function isStartupRuntimeFailureOutput(value) {
|
|
657
|
-
const normalized = value.trim().toLowerCase();
|
|
658
|
-
if (!normalized.startsWith("runtime_error=")) {
|
|
659
|
-
return false;
|
|
660
|
-
}
|
|
661
|
-
return ((normalized.includes("terminated") && normalized.includes("other side closed"))
|
|
662
|
-
|| normalized.includes("socket hang up")
|
|
663
|
-
|| normalized.includes("connection reset")
|
|
664
|
-
|| normalized.includes("econnreset"));
|
|
665
|
-
}
|
|
666
|
-
export async function* streamHarnessRun(options) {
|
|
667
|
-
const priorHistoryPromise = Promise.resolve(options.isNewSession ? [] : undefined).then((historyHint) => historyHint ?? options.loadPriorHistory(options.sessionId, options.requestId));
|
|
668
|
-
yield { type: "event", event: await options.requestCreatedEventPromise };
|
|
669
|
-
let planStateVersion = 0;
|
|
670
|
-
let lastPlanStateSignature;
|
|
671
|
-
let currentPlanState = null;
|
|
672
|
-
let releaseRunSlot = async () => undefined;
|
|
673
|
-
let emitted = false;
|
|
674
|
-
let streamActivityObserved = false;
|
|
675
|
-
let nonUpstreamStreamActivityObserved = false;
|
|
676
|
-
let observedOnlyFallbackSafeUpstreamEvents = true;
|
|
677
|
-
let assistantOutput = "";
|
|
678
|
-
let assistantOutputCameFromInvokeFallback = false;
|
|
679
|
-
const bufferAssistantTextUntilCompletion = true;
|
|
680
|
-
let currentAgentId = options.selectedAgentId;
|
|
681
|
-
let currentAgentName = formatAgentName(options.selectedAgentId);
|
|
682
|
-
let delegationChain = [options.selectedAgentId];
|
|
683
|
-
let upstreamEventOrdinal = 0;
|
|
684
|
-
let syntheticFallback;
|
|
685
|
-
const toolErrors = [];
|
|
686
|
-
let sawSuccessfulToolResult = false;
|
|
687
|
-
let sawSuccessfulExecutionToolResult = false;
|
|
688
|
-
let lastToolResultKey = null;
|
|
689
|
-
const executedToolResults = [];
|
|
690
|
-
const emittedCommentary = new Set();
|
|
691
|
-
const emitCommentary = function* (content, agentIdOverride) {
|
|
692
|
-
const normalized = normalizeCommentaryText(content);
|
|
693
|
-
if (!normalized || emittedCommentary.has(normalized)) {
|
|
694
|
-
return;
|
|
695
|
-
}
|
|
696
|
-
emittedCommentary.add(normalized);
|
|
697
|
-
yield {
|
|
698
|
-
type: "commentary",
|
|
699
|
-
sessionId: options.sessionId,
|
|
700
|
-
requestId: options.requestId,
|
|
701
|
-
agentId: agentIdOverride ?? currentAgentId,
|
|
702
|
-
content: normalized,
|
|
703
|
-
};
|
|
704
|
-
};
|
|
705
|
-
try {
|
|
706
|
-
const [priorHistory, acquiredReleaseRunSlot] = await Promise.all([
|
|
707
|
-
priorHistoryPromise,
|
|
708
|
-
options.releaseRequestSlotPromise,
|
|
709
|
-
]).then(([loadedPriorHistory, resolvedReleaseRunSlot]) => [loadedPriorHistory, resolvedReleaseRunSlot]);
|
|
710
|
-
releaseRunSlot = acquiredReleaseRunSlot;
|
|
711
|
-
const recalledMemories = options.invocation.memoryRecall?.items ?? [];
|
|
712
|
-
for (const item of createRuntimeMemoryRecallSteps(options.sessionId, options.requestId, recalledMemories)) {
|
|
713
|
-
yield item;
|
|
714
|
-
if (item.type === "profile-step") {
|
|
715
|
-
const commentary = createProfileStepCommentary(item.step);
|
|
716
|
-
if (commentary) {
|
|
717
|
-
yield* emitCommentary(commentary);
|
|
718
|
-
}
|
|
719
|
-
}
|
|
720
|
-
}
|
|
721
|
-
for await (const rawChunk of options.stream(options.binding, options.input, options.sessionId, priorHistory, {
|
|
722
|
-
context: options.invocation.context,
|
|
723
|
-
state: options.invocation.state,
|
|
724
|
-
files: options.invocation.files,
|
|
725
|
-
requestId: options.requestId,
|
|
726
|
-
memoryContext: options.invocation.memoryRecall?.prompt,
|
|
727
|
-
})) {
|
|
728
|
-
if (!rawChunk) {
|
|
729
|
-
continue;
|
|
730
|
-
}
|
|
731
|
-
streamActivityObserved = true;
|
|
732
|
-
const normalizedChunk = normalizeStreamChunk(rawChunk);
|
|
733
|
-
if (normalizedChunk.kind === "upstream-event") {
|
|
734
|
-
if (!isFallbackSafeUpstreamStartupEvent(normalizedChunk.event, options.selectedAgentId)) {
|
|
735
|
-
observedOnlyFallbackSafeUpstreamEvents = false;
|
|
736
|
-
}
|
|
737
|
-
const upstreamPlanState = getPlanStateFromUpstreamEvent({
|
|
738
|
-
sessionId: options.sessionId,
|
|
739
|
-
requestId: options.requestId,
|
|
740
|
-
event: normalizedChunk.event,
|
|
741
|
-
version: planStateVersion + 1,
|
|
742
|
-
updatedAt: new Date().toISOString(),
|
|
743
|
-
});
|
|
744
|
-
if (upstreamPlanState) {
|
|
745
|
-
const mergedPlanState = mergePartialPlanState(currentPlanState, upstreamPlanState);
|
|
746
|
-
const signature = buildPlanStateSignature(mergedPlanState);
|
|
747
|
-
if (signature !== lastPlanStateSignature && shouldEmitPlanState(currentPlanState, mergedPlanState)) {
|
|
748
|
-
const previousPlanState = currentPlanState;
|
|
749
|
-
planStateVersion = mergedPlanState.version;
|
|
750
|
-
lastPlanStateSignature = signature;
|
|
751
|
-
currentPlanState = mergedPlanState;
|
|
752
|
-
const progression = buildPlanStateProgression(previousPlanState, currentPlanState);
|
|
753
|
-
for (const progressionStep of progression) {
|
|
754
|
-
for (const item of await emitPlanStateUpdate(options, currentAgentId, progressionStep.planState)) {
|
|
755
|
-
yield item;
|
|
756
|
-
}
|
|
757
|
-
if (progressionStep.commentary) {
|
|
758
|
-
yield* emitCommentary(progressionStep.commentary);
|
|
759
|
-
}
|
|
760
|
-
}
|
|
761
|
-
const commentary = summarizePlanState(currentPlanState);
|
|
762
|
-
if (commentary) {
|
|
763
|
-
yield* emitCommentary(commentary);
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
upstreamEventOrdinal += 1;
|
|
768
|
-
const projectionBinding = options.getBinding(currentAgentId) ?? options.binding;
|
|
769
|
-
const surfaceProjection = projectRuntimeSurfaceFromSingleUpstreamEvent({
|
|
770
|
-
event: {
|
|
771
|
-
agentId: currentAgentId,
|
|
772
|
-
agentName: currentAgentName,
|
|
773
|
-
event: normalizedChunk.event,
|
|
774
|
-
},
|
|
775
|
-
binding: projectionBinding,
|
|
776
|
-
currentAgentId,
|
|
777
|
-
currentAgentName,
|
|
778
|
-
delegationChain,
|
|
779
|
-
sourceEventId: `upstream:${upstreamEventOrdinal}`,
|
|
780
|
-
});
|
|
781
|
-
currentAgentId = surfaceProjection.currentAgentId;
|
|
782
|
-
currentAgentName = surfaceProjection.currentAgentName;
|
|
783
|
-
delegationChain = surfaceProjection.delegationChain;
|
|
784
|
-
await options.updateRequestInspection(options.sessionId, options.requestId, {
|
|
785
|
-
currentAgentId,
|
|
786
|
-
delegationChain,
|
|
787
|
-
});
|
|
788
|
-
if (surfaceProjection.items.length === 0) {
|
|
789
|
-
await options.appendRequestTraceItem(options.sessionId, options.requestId, {
|
|
790
|
-
agentId: currentAgentId,
|
|
791
|
-
agentName: currentAgentName,
|
|
792
|
-
event: normalizedChunk.event,
|
|
793
|
-
});
|
|
794
|
-
yield {
|
|
795
|
-
type: "upstream-event",
|
|
796
|
-
sessionId: options.sessionId,
|
|
797
|
-
requestId: options.requestId,
|
|
798
|
-
event: normalizedChunk.event,
|
|
799
|
-
};
|
|
800
|
-
continue;
|
|
801
|
-
}
|
|
802
|
-
for (const surfaceItem of surfaceProjection.items) {
|
|
803
|
-
await options.appendRequestTraceItem(options.sessionId, options.requestId, {
|
|
804
|
-
agentId: surfaceItem.agentId ?? currentAgentId,
|
|
805
|
-
agentName: surfaceItem.agentName ?? currentAgentName,
|
|
806
|
-
surfaceItem,
|
|
807
|
-
event: normalizedChunk.event,
|
|
808
|
-
});
|
|
809
|
-
yield {
|
|
810
|
-
type: "upstream-event",
|
|
811
|
-
sessionId: options.sessionId,
|
|
812
|
-
requestId: options.requestId,
|
|
813
|
-
surfaceItem,
|
|
814
|
-
event: normalizedChunk.event,
|
|
815
|
-
};
|
|
816
|
-
const commentary = createSurfaceCommentary(surfaceItem);
|
|
817
|
-
if (commentary) {
|
|
818
|
-
yield* emitCommentary(commentary);
|
|
819
|
-
}
|
|
820
|
-
}
|
|
821
|
-
continue;
|
|
822
|
-
}
|
|
823
|
-
nonUpstreamStreamActivityObserved = true;
|
|
824
|
-
if (normalizedChunk.kind === "interrupt") {
|
|
825
|
-
const checkpointRef = `checkpoints/${options.sessionId}/${options.requestId}/cp-1`;
|
|
826
|
-
const waitingEvent = await options.setRequestStateAndEmit(options.sessionId, options.requestId, 6, "waiting_for_approval", {
|
|
827
|
-
previousState: "running",
|
|
828
|
-
checkpointRef,
|
|
829
|
-
});
|
|
830
|
-
const approvalRequest = await options.requestApprovalAndEmit(options.sessionId, options.requestId, options.input, normalizedChunk.content, checkpointRef, 7);
|
|
831
|
-
yield { type: "event", event: waitingEvent };
|
|
832
|
-
yield { type: "event", event: approvalRequest.event };
|
|
833
|
-
yield {
|
|
834
|
-
type: "result",
|
|
835
|
-
result: {
|
|
836
|
-
sessionId: options.sessionId,
|
|
837
|
-
requestId: options.requestId,
|
|
838
|
-
agentId: currentAgentId,
|
|
839
|
-
state: "waiting_for_approval",
|
|
840
|
-
output: assistantOutput,
|
|
841
|
-
finalMessageText: assistantOutput,
|
|
842
|
-
interruptContent: normalizedChunk.content,
|
|
843
|
-
approvalId: approvalRequest.approval.approvalId,
|
|
844
|
-
pendingActionId: approvalRequest.approval.pendingActionId,
|
|
845
|
-
},
|
|
846
|
-
};
|
|
847
|
-
return;
|
|
848
|
-
}
|
|
849
|
-
if (normalizedChunk.kind === "commentary") {
|
|
850
|
-
yield* emitCommentary(normalizedChunk.content, normalizedChunk.agentId);
|
|
851
|
-
continue;
|
|
852
|
-
}
|
|
853
|
-
if (normalizedChunk.kind === "reasoning") {
|
|
854
|
-
continue;
|
|
855
|
-
}
|
|
856
|
-
if (normalizedChunk.kind === "profile") {
|
|
857
|
-
yield {
|
|
858
|
-
type: "profile-step",
|
|
859
|
-
sessionId: options.sessionId,
|
|
860
|
-
requestId: options.requestId,
|
|
861
|
-
step: normalizedChunk.step,
|
|
862
|
-
};
|
|
863
|
-
const commentary = createProfileStepCommentary(normalizedChunk.step);
|
|
864
|
-
if (commentary) {
|
|
865
|
-
yield* emitCommentary(commentary);
|
|
866
|
-
}
|
|
867
|
-
continue;
|
|
868
|
-
}
|
|
869
|
-
if (normalizedChunk.kind === "tool-result") {
|
|
870
|
-
const chunkAgentId = normalizedChunk.agentId ?? currentAgentId;
|
|
871
|
-
const toolResultKey = createToolResultKey(normalizedChunk.toolName, normalizedChunk.output, normalizedChunk.isError);
|
|
872
|
-
if (toolResultKey === lastToolResultKey) {
|
|
873
|
-
continue;
|
|
874
|
-
}
|
|
875
|
-
lastToolResultKey = toolResultKey;
|
|
876
|
-
executedToolResults.push({
|
|
877
|
-
toolName: normalizedChunk.toolName,
|
|
878
|
-
output: normalizedChunk.output,
|
|
879
|
-
isError: normalizedChunk.isError,
|
|
880
|
-
});
|
|
881
|
-
if (normalizedChunk.isError) {
|
|
882
|
-
toolErrors.push(renderToolFailure(normalizedChunk.toolName, normalizedChunk.output));
|
|
883
|
-
}
|
|
884
|
-
else {
|
|
885
|
-
sawSuccessfulToolResult = true;
|
|
886
|
-
if (!isPlanToolName(normalizedChunk.toolName) && normalizedChunk.toolName !== "task") {
|
|
887
|
-
sawSuccessfulExecutionToolResult = true;
|
|
888
|
-
}
|
|
889
|
-
}
|
|
890
|
-
yield {
|
|
891
|
-
type: "tool-result",
|
|
892
|
-
sessionId: options.sessionId,
|
|
893
|
-
requestId: options.requestId,
|
|
894
|
-
agentId: chunkAgentId,
|
|
895
|
-
toolName: normalizedChunk.toolName,
|
|
896
|
-
output: normalizedChunk.output,
|
|
897
|
-
isError: normalizedChunk.isError,
|
|
898
|
-
};
|
|
899
|
-
const planState = normalizedChunk.toolName === "write_todos"
|
|
900
|
-
? getPlanStateFromToolResult({
|
|
901
|
-
sessionId: options.sessionId,
|
|
902
|
-
requestId: options.requestId,
|
|
903
|
-
output: normalizedChunk.output,
|
|
904
|
-
version: ++planStateVersion,
|
|
905
|
-
updatedAt: new Date().toISOString(),
|
|
906
|
-
})
|
|
907
|
-
: getStructuredPlanStateFromToolResult({
|
|
908
|
-
sessionId: options.sessionId,
|
|
909
|
-
requestId: options.requestId,
|
|
910
|
-
output: normalizedChunk.output,
|
|
911
|
-
version: ++planStateVersion,
|
|
912
|
-
updatedAt: new Date().toISOString(),
|
|
913
|
-
});
|
|
914
|
-
if (planState) {
|
|
915
|
-
const mergedPlanState = mergePartialPlanState(currentPlanState, planState);
|
|
916
|
-
const signature = buildPlanStateSignature(mergedPlanState);
|
|
917
|
-
if (signature !== lastPlanStateSignature && shouldEmitPlanState(currentPlanState, mergedPlanState)) {
|
|
918
|
-
const previousPlanState = currentPlanState;
|
|
919
|
-
lastPlanStateSignature = signature;
|
|
920
|
-
currentPlanState = mergedPlanState;
|
|
921
|
-
const progression = buildPlanStateProgression(previousPlanState, currentPlanState);
|
|
922
|
-
for (const progressionStep of progression) {
|
|
923
|
-
for (const item of await emitPlanStateUpdate(options, chunkAgentId, progressionStep.planState)) {
|
|
924
|
-
yield item;
|
|
925
|
-
}
|
|
926
|
-
if (progressionStep.commentary) {
|
|
927
|
-
yield* emitCommentary(progressionStep.commentary, chunkAgentId);
|
|
928
|
-
}
|
|
929
|
-
}
|
|
930
|
-
const commentary = summarizePlanState(currentPlanState);
|
|
931
|
-
if (commentary) {
|
|
932
|
-
yield* emitCommentary(commentary, chunkAgentId);
|
|
933
|
-
}
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
const terminalStructuredStatus = normalizedChunk.toolName === "task"
|
|
937
|
-
? readTerminalExecutionStatus(normalizedChunk.output)
|
|
938
|
-
: null;
|
|
939
|
-
if (terminalStructuredStatus && currentPlanState && planStateHasActiveItems(currentPlanState)) {
|
|
940
|
-
const reconciledPlanState = reconcilePlanStateToTerminalStatus(currentPlanState, mapTerminalStatusToPlanItemStatus(terminalStructuredStatus), new Date().toISOString());
|
|
941
|
-
const signature = buildPlanStateSignature(reconciledPlanState);
|
|
942
|
-
if (signature !== lastPlanStateSignature) {
|
|
943
|
-
const previousPlanState = currentPlanState;
|
|
944
|
-
planStateVersion = reconciledPlanState.version;
|
|
945
|
-
lastPlanStateSignature = signature;
|
|
946
|
-
currentPlanState = reconciledPlanState;
|
|
947
|
-
const progression = buildPlanStateProgression(previousPlanState, currentPlanState);
|
|
948
|
-
for (const progressionStep of progression) {
|
|
949
|
-
for (const item of await emitPlanStateUpdate(options, chunkAgentId, progressionStep.planState)) {
|
|
950
|
-
yield item;
|
|
951
|
-
}
|
|
952
|
-
if (progressionStep.commentary) {
|
|
953
|
-
yield* emitCommentary(progressionStep.commentary, chunkAgentId);
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
const commentary = summarizePlanState(currentPlanState);
|
|
957
|
-
if (commentary) {
|
|
958
|
-
yield* emitCommentary(commentary, chunkAgentId);
|
|
959
|
-
}
|
|
960
|
-
}
|
|
961
|
-
}
|
|
962
|
-
continue;
|
|
963
|
-
}
|
|
964
|
-
emitted = true;
|
|
965
|
-
assistantOutput += normalizedChunk.content;
|
|
966
|
-
if (bufferAssistantTextUntilCompletion) {
|
|
967
|
-
continue;
|
|
968
|
-
}
|
|
969
|
-
yield {
|
|
970
|
-
type: "event",
|
|
971
|
-
event: await options.emit(options.sessionId, options.requestId, 3, "output.delta", { content: normalizedChunk.content }),
|
|
972
|
-
};
|
|
973
|
-
yield {
|
|
974
|
-
type: "content",
|
|
975
|
-
sessionId: options.sessionId,
|
|
976
|
-
requestId: options.requestId,
|
|
977
|
-
agentId: currentAgentId,
|
|
978
|
-
content: normalizedChunk.content,
|
|
979
|
-
};
|
|
980
|
-
}
|
|
981
|
-
if (!assistantOutput && toolErrors.length > 0 && !sawSuccessfulToolResult) {
|
|
982
|
-
assistantOutput = toolErrors.join("\n\n");
|
|
983
|
-
emitted = true;
|
|
984
|
-
}
|
|
985
|
-
const resolvedAssistantOutput = resolveDeterministicFinalOutput({
|
|
986
|
-
visibleOutput: assistantOutput,
|
|
987
|
-
executedToolResults,
|
|
988
|
-
});
|
|
989
|
-
if (sanitizeVisibleText(resolvedAssistantOutput) !== sanitizeVisibleText(assistantOutput)) {
|
|
990
|
-
assistantOutput = resolvedAssistantOutput;
|
|
991
|
-
emitted = emitted || assistantOutput.length > 0;
|
|
992
|
-
}
|
|
993
|
-
currentPlanState = await refreshPlanStateFromPersistence(options, currentPlanState);
|
|
994
|
-
if (assistantOutput
|
|
995
|
-
&& isStartupRuntimeFailureOutput(assistantOutput)
|
|
996
|
-
&& executedToolResults.length === 0
|
|
997
|
-
&& !sawSuccessfulToolResult
|
|
998
|
-
&& observedOnlyFallbackSafeUpstreamEvents
|
|
999
|
-
&& !planStateHasAnyItems(currentPlanState)) {
|
|
1000
|
-
assistantOutput = "";
|
|
1001
|
-
emitted = false;
|
|
1002
|
-
}
|
|
1003
|
-
if (!assistantOutput) {
|
|
1004
|
-
const actual = await options.invokeWithHistory(options.binding, options.input, options.sessionId, options.requestId);
|
|
1005
|
-
if (Array.isArray(actual.contentBlocks) && actual.contentBlocks.length > 0) {
|
|
1006
|
-
yield createContentBlocksItem(options.sessionId, options.requestId, options.selectedAgentId, actual.contentBlocks);
|
|
1007
|
-
}
|
|
1008
|
-
if (actual.output) {
|
|
1009
|
-
assistantOutput = actual.output;
|
|
1010
|
-
emitted = true;
|
|
1011
|
-
assistantOutputCameFromInvokeFallback = true;
|
|
1012
|
-
}
|
|
1013
|
-
const finalPlanState = getLatestPlanStateFromExecutedToolResults({
|
|
1014
|
-
sessionId: options.sessionId,
|
|
1015
|
-
requestId: options.requestId,
|
|
1016
|
-
metadata: actual.metadata,
|
|
1017
|
-
version: planStateVersion + 1,
|
|
1018
|
-
updatedAt: new Date().toISOString(),
|
|
1019
|
-
});
|
|
1020
|
-
if (finalPlanState) {
|
|
1021
|
-
const mergedPlanState = mergePartialPlanState(currentPlanState, finalPlanState);
|
|
1022
|
-
const signature = buildPlanStateSignature(mergedPlanState);
|
|
1023
|
-
if (signature !== lastPlanStateSignature && shouldEmitPlanState(currentPlanState, mergedPlanState)) {
|
|
1024
|
-
const previousPlanState = currentPlanState;
|
|
1025
|
-
planStateVersion = mergedPlanState.version;
|
|
1026
|
-
lastPlanStateSignature = signature;
|
|
1027
|
-
currentPlanState = mergedPlanState;
|
|
1028
|
-
const progression = buildPlanStateProgression(previousPlanState, currentPlanState);
|
|
1029
|
-
for (const progressionStep of progression) {
|
|
1030
|
-
for (const item of await emitPlanStateUpdate(options, currentAgentId, progressionStep.planState)) {
|
|
1031
|
-
yield item;
|
|
1032
|
-
}
|
|
1033
|
-
if (progressionStep.commentary) {
|
|
1034
|
-
yield* emitCommentary(progressionStep.commentary);
|
|
1035
|
-
}
|
|
1036
|
-
}
|
|
1037
|
-
const commentary = summarizePlanState(currentPlanState);
|
|
1038
|
-
if (commentary) {
|
|
1039
|
-
yield* emitCommentary(commentary);
|
|
1040
|
-
}
|
|
1041
|
-
}
|
|
1042
|
-
}
|
|
1043
|
-
currentPlanState = await refreshPlanStateFromPersistence(options, currentPlanState);
|
|
1044
|
-
const terminalStructuredStatus = readTerminalExecutionStatus(actual.structuredResponse);
|
|
1045
|
-
if (terminalStructuredStatus && currentPlanState && planStateHasActiveItems(currentPlanState)) {
|
|
1046
|
-
const reconciledPlanState = reconcilePlanStateToTerminalStatus(currentPlanState, mapTerminalStatusToObservedPlanItemStatus(terminalStructuredStatus, sawSuccessfulToolResult), new Date().toISOString());
|
|
1047
|
-
const signature = buildPlanStateSignature(reconciledPlanState);
|
|
1048
|
-
if (signature !== lastPlanStateSignature) {
|
|
1049
|
-
const previousPlanState = currentPlanState;
|
|
1050
|
-
planStateVersion = reconciledPlanState.version;
|
|
1051
|
-
lastPlanStateSignature = signature;
|
|
1052
|
-
currentPlanState = reconciledPlanState;
|
|
1053
|
-
const progression = buildPlanStateProgression(previousPlanState, currentPlanState);
|
|
1054
|
-
for (const progressionStep of progression) {
|
|
1055
|
-
for (const item of await emitPlanStateUpdate(options, currentAgentId, progressionStep.planState)) {
|
|
1056
|
-
yield item;
|
|
1057
|
-
}
|
|
1058
|
-
if (progressionStep.commentary) {
|
|
1059
|
-
yield* emitCommentary(progressionStep.commentary);
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
const commentary = summarizePlanState(currentPlanState);
|
|
1063
|
-
if (commentary) {
|
|
1064
|
-
yield* emitCommentary(commentary);
|
|
1065
|
-
}
|
|
1066
|
-
}
|
|
1067
|
-
}
|
|
1068
|
-
}
|
|
1069
|
-
currentPlanState = await refreshPlanStateFromPersistence(options, currentPlanState);
|
|
1070
|
-
const explicitTerminalAssistantStatus = readTerminalExecutionStatus(assistantOutput);
|
|
1071
|
-
let terminalAssistantPlanItemStatus = inferPlanItemStatusFromTerminalAssistantOutput(assistantOutput, {
|
|
1072
|
-
hasSuccessfulExecutionEvidence: sawSuccessfulExecutionToolResult,
|
|
1073
|
-
});
|
|
1074
|
-
if (explicitTerminalAssistantStatus === "blocked" && sawSuccessfulExecutionToolResult) {
|
|
1075
|
-
terminalAssistantPlanItemStatus = "completed";
|
|
1076
|
-
}
|
|
1077
|
-
if (terminalAssistantPlanItemStatus === "failed"
|
|
1078
|
-
&& sawSuccessfulExecutionToolResult
|
|
1079
|
-
&& !explicitTerminalAssistantStatus
|
|
1080
|
-
&& !sanitizeVisibleText(assistantOutput).trim().toLowerCase().startsWith("runtime_error=")
|
|
1081
|
-
&& assistantOutput.trim()) {
|
|
1082
|
-
terminalAssistantPlanItemStatus = "completed";
|
|
1083
|
-
}
|
|
1084
|
-
if (terminalAssistantPlanItemStatus && currentPlanState && planStateHasActiveItems(currentPlanState)) {
|
|
1085
|
-
const reconciledPlanState = reconcilePlanStateToTerminalStatus(currentPlanState, terminalAssistantPlanItemStatus, new Date().toISOString());
|
|
1086
|
-
const signature = buildPlanStateSignature(reconciledPlanState);
|
|
1087
|
-
if (signature !== lastPlanStateSignature) {
|
|
1088
|
-
const previousPlanState = currentPlanState;
|
|
1089
|
-
lastPlanStateSignature = signature;
|
|
1090
|
-
currentPlanState = reconciledPlanState;
|
|
1091
|
-
const progression = buildPlanStateProgression(previousPlanState, currentPlanState);
|
|
1092
|
-
for (const progressionStep of progression) {
|
|
1093
|
-
for (const item of await emitPlanStateUpdate(options, currentAgentId, progressionStep.planState)) {
|
|
1094
|
-
yield item;
|
|
1095
|
-
}
|
|
1096
|
-
if (progressionStep.commentary) {
|
|
1097
|
-
yield* emitCommentary(progressionStep.commentary);
|
|
1098
|
-
}
|
|
1099
|
-
}
|
|
1100
|
-
const commentary = summarizePlanState(currentPlanState);
|
|
1101
|
-
if (commentary) {
|
|
1102
|
-
yield* emitCommentary(commentary);
|
|
1103
|
-
}
|
|
1104
|
-
}
|
|
1105
|
-
}
|
|
1106
|
-
if (assistantOutputCameFromInvokeFallback
|
|
1107
|
-
&& nonUpstreamStreamActivityObserved
|
|
1108
|
-
&& planStateHasActiveItems(currentPlanState)) {
|
|
1109
|
-
throw new ExecutionReconciliationError("Agent ended while the streamed plan state still had unfinished work.");
|
|
1110
|
-
}
|
|
1111
|
-
if (planStateHasActiveItems(currentPlanState)) {
|
|
1112
|
-
throw new ExecutionReconciliationError("Agent ended while the streamed plan state still had unfinished work.");
|
|
1113
|
-
}
|
|
1114
|
-
if (assistantOutput && bufferAssistantTextUntilCompletion) {
|
|
1115
|
-
yield {
|
|
1116
|
-
type: "event",
|
|
1117
|
-
event: await options.emit(options.sessionId, options.requestId, 3, "output.delta", { content: assistantOutput }),
|
|
1118
|
-
};
|
|
1119
|
-
yield {
|
|
1120
|
-
type: "content",
|
|
1121
|
-
sessionId: options.sessionId,
|
|
1122
|
-
requestId: options.requestId,
|
|
1123
|
-
agentId: currentAgentId,
|
|
1124
|
-
content: assistantOutput,
|
|
1125
|
-
};
|
|
1126
|
-
}
|
|
1127
|
-
const terminalStatus = readTerminalExecutionStatus(assistantOutput);
|
|
1128
|
-
const terminalRequestState = mapTerminalStatusToRequestState(terminalStatus);
|
|
1129
|
-
await options.appendAssistantMessage(options.sessionId, options.requestId, assistantOutput);
|
|
1130
|
-
const completedEvent = await options.setRequestStateAndEmit(options.sessionId, options.requestId, 6, terminalRequestState, {
|
|
1131
|
-
previousState: "running",
|
|
1132
|
-
});
|
|
1133
|
-
yield {
|
|
1134
|
-
type: "event",
|
|
1135
|
-
event: completedEvent,
|
|
1136
|
-
};
|
|
1137
|
-
yield {
|
|
1138
|
-
type: "result",
|
|
1139
|
-
result: {
|
|
1140
|
-
sessionId: options.sessionId,
|
|
1141
|
-
requestId: options.requestId,
|
|
1142
|
-
agentId: currentAgentId,
|
|
1143
|
-
state: terminalRequestState,
|
|
1144
|
-
output: assistantOutput,
|
|
1145
|
-
finalMessageText: assistantOutput,
|
|
1146
|
-
...(terminalStatus ? { metadata: { terminalStatus } } : {}),
|
|
1147
|
-
},
|
|
1148
|
-
};
|
|
1149
|
-
}
|
|
1150
|
-
catch (error) {
|
|
1151
|
-
const deterministicToolEvidenceOutput = resolveDeterministicFinalOutput({
|
|
1152
|
-
visibleOutput: assistantOutput,
|
|
1153
|
-
executedToolResults,
|
|
1154
|
-
});
|
|
1155
|
-
const terminalStructuredStatus = readTerminalExecutionStatus(deterministicToolEvidenceOutput);
|
|
1156
|
-
const canUseDeterministicToolEvidenceOutput = !currentPlanState || !planStateHasActiveItems(currentPlanState) || Boolean(terminalStructuredStatus);
|
|
1157
|
-
if (!assistantOutput && sawSuccessfulToolResult && deterministicToolEvidenceOutput && canUseDeterministicToolEvidenceOutput) {
|
|
1158
|
-
if (terminalStructuredStatus && currentPlanState && planStateHasActiveItems(currentPlanState)) {
|
|
1159
|
-
const reconciledPlanState = reconcilePlanStateToTerminalStatus(currentPlanState, mapTerminalStatusToObservedPlanItemStatus(terminalStructuredStatus, sawSuccessfulToolResult), new Date().toISOString());
|
|
1160
|
-
const signature = buildPlanStateSignature(reconciledPlanState);
|
|
1161
|
-
if (signature !== lastPlanStateSignature) {
|
|
1162
|
-
const previousPlanState = currentPlanState;
|
|
1163
|
-
lastPlanStateSignature = signature;
|
|
1164
|
-
currentPlanState = reconciledPlanState;
|
|
1165
|
-
const progression = buildPlanStateProgression(previousPlanState, currentPlanState);
|
|
1166
|
-
for (const progressionStep of progression) {
|
|
1167
|
-
for (const item of await emitPlanStateUpdate(options, currentAgentId, progressionStep.planState)) {
|
|
1168
|
-
yield item;
|
|
1169
|
-
}
|
|
1170
|
-
if (progressionStep.commentary) {
|
|
1171
|
-
yield* emitCommentary(progressionStep.commentary);
|
|
1172
|
-
}
|
|
1173
|
-
}
|
|
1174
|
-
const commentary = summarizePlanState(currentPlanState);
|
|
1175
|
-
if (commentary) {
|
|
1176
|
-
yield* emitCommentary(commentary);
|
|
1177
|
-
}
|
|
1178
|
-
}
|
|
1179
|
-
}
|
|
1180
|
-
await options.appendAssistantMessage(options.sessionId, options.requestId, deterministicToolEvidenceOutput);
|
|
1181
|
-
yield {
|
|
1182
|
-
type: "event",
|
|
1183
|
-
event: await options.emit(options.sessionId, options.requestId, 3, "output.delta", { content: deterministicToolEvidenceOutput }),
|
|
1184
|
-
};
|
|
1185
|
-
yield {
|
|
1186
|
-
type: "content",
|
|
1187
|
-
sessionId: options.sessionId,
|
|
1188
|
-
requestId: options.requestId,
|
|
1189
|
-
agentId: currentAgentId,
|
|
1190
|
-
content: deterministicToolEvidenceOutput,
|
|
1191
|
-
};
|
|
1192
|
-
const terminalStatus = readTerminalExecutionStatus(deterministicToolEvidenceOutput);
|
|
1193
|
-
const terminalRequestState = mapTerminalStatusToRequestState(terminalStatus);
|
|
1194
|
-
const completedEvent = await options.setRequestStateAndEmit(options.sessionId, options.requestId, 6, terminalRequestState, {
|
|
1195
|
-
previousState: "running",
|
|
1196
|
-
});
|
|
1197
|
-
yield {
|
|
1198
|
-
type: "event",
|
|
1199
|
-
event: completedEvent,
|
|
1200
|
-
};
|
|
1201
|
-
yield {
|
|
1202
|
-
type: "result",
|
|
1203
|
-
result: {
|
|
1204
|
-
sessionId: options.sessionId,
|
|
1205
|
-
requestId: options.requestId,
|
|
1206
|
-
agentId: currentAgentId,
|
|
1207
|
-
state: terminalRequestState,
|
|
1208
|
-
output: deterministicToolEvidenceOutput,
|
|
1209
|
-
finalMessageText: deterministicToolEvidenceOutput,
|
|
1210
|
-
metadata: {
|
|
1211
|
-
executedToolResults,
|
|
1212
|
-
...(terminalStatus ? { terminalStatus } : {}),
|
|
1213
|
-
},
|
|
1214
|
-
},
|
|
1215
|
-
};
|
|
1216
|
-
return;
|
|
1217
|
-
}
|
|
1218
|
-
const shouldRetryAfterStreamingCompatibilityError = !assistantOutput &&
|
|
1219
|
-
isOpenAICompatibleStreamingCompatibilityError(options.binding, error);
|
|
1220
|
-
const streamHadExecutionSideEffects = emitted
|
|
1221
|
-
|| nonUpstreamStreamActivityObserved
|
|
1222
|
-
|| sawSuccessfulToolResult
|
|
1223
|
-
|| executedToolResults.length > 0
|
|
1224
|
-
|| !observedOnlyFallbackSafeUpstreamEvents;
|
|
1225
|
-
if (streamHadExecutionSideEffects && !shouldRetryAfterStreamingCompatibilityError) {
|
|
1226
|
-
const runtimeFailure = renderRuntimeFailure(error);
|
|
1227
|
-
const detailedError = describeRuntimeError(error);
|
|
1228
|
-
yield {
|
|
1229
|
-
type: "event",
|
|
1230
|
-
event: await options.setRequestStateAndEmit(options.sessionId, options.requestId, 6, "failed", {
|
|
1231
|
-
previousState: "running",
|
|
1232
|
-
error: detailedError,
|
|
1233
|
-
}),
|
|
1234
|
-
};
|
|
1235
|
-
yield {
|
|
1236
|
-
type: "content",
|
|
1237
|
-
sessionId: options.sessionId,
|
|
1238
|
-
requestId: options.requestId,
|
|
1239
|
-
agentId: options.selectedAgentId,
|
|
1240
|
-
content: runtimeFailure,
|
|
1241
|
-
};
|
|
1242
|
-
yield {
|
|
1243
|
-
type: "result",
|
|
1244
|
-
result: {
|
|
1245
|
-
sessionId: options.sessionId,
|
|
1246
|
-
requestId: options.requestId,
|
|
1247
|
-
agentId: currentAgentId,
|
|
1248
|
-
state: "failed",
|
|
1249
|
-
output: runtimeFailure,
|
|
1250
|
-
finalMessageText: runtimeFailure,
|
|
1251
|
-
},
|
|
1252
|
-
};
|
|
1253
|
-
return;
|
|
1254
|
-
}
|
|
1255
|
-
if (error instanceof RuntimeOperationTimeoutError && error.stage === "invoke") {
|
|
1256
|
-
const runtimeFailure = renderRuntimeFailure(error);
|
|
1257
|
-
const detailedError = describeRuntimeError(error);
|
|
1258
|
-
yield {
|
|
1259
|
-
type: "event",
|
|
1260
|
-
event: await options.setRequestStateAndEmit(options.sessionId, options.requestId, 6, "failed", {
|
|
1261
|
-
previousState: "running",
|
|
1262
|
-
error: detailedError,
|
|
1263
|
-
}),
|
|
1264
|
-
};
|
|
1265
|
-
yield {
|
|
1266
|
-
type: "content",
|
|
1267
|
-
sessionId: options.sessionId,
|
|
1268
|
-
requestId: options.requestId,
|
|
1269
|
-
agentId: options.selectedAgentId,
|
|
1270
|
-
content: runtimeFailure,
|
|
1271
|
-
};
|
|
1272
|
-
yield {
|
|
1273
|
-
type: "result",
|
|
1274
|
-
result: {
|
|
1275
|
-
sessionId: options.sessionId,
|
|
1276
|
-
requestId: options.requestId,
|
|
1277
|
-
agentId: options.selectedAgentId,
|
|
1278
|
-
state: "failed",
|
|
1279
|
-
output: runtimeFailure,
|
|
1280
|
-
finalMessageText: runtimeFailure,
|
|
1281
|
-
},
|
|
1282
|
-
};
|
|
1283
|
-
return;
|
|
1284
|
-
}
|
|
1285
|
-
if (error instanceof ExecutionReconciliationError) {
|
|
1286
|
-
const runtimeFailure = renderRuntimeFailure(error);
|
|
1287
|
-
const detailedError = describeRuntimeError(error);
|
|
1288
|
-
yield {
|
|
1289
|
-
type: "event",
|
|
1290
|
-
event: await options.setRequestStateAndEmit(options.sessionId, options.requestId, 6, "failed", {
|
|
1291
|
-
previousState: "running",
|
|
1292
|
-
error: detailedError,
|
|
1293
|
-
}),
|
|
1294
|
-
};
|
|
1295
|
-
yield {
|
|
1296
|
-
type: "content",
|
|
1297
|
-
sessionId: options.sessionId,
|
|
1298
|
-
requestId: options.requestId,
|
|
1299
|
-
agentId: options.selectedAgentId,
|
|
1300
|
-
content: runtimeFailure,
|
|
1301
|
-
};
|
|
1302
|
-
yield {
|
|
1303
|
-
type: "result",
|
|
1304
|
-
result: {
|
|
1305
|
-
sessionId: options.sessionId,
|
|
1306
|
-
requestId: options.requestId,
|
|
1307
|
-
agentId: currentAgentId,
|
|
1308
|
-
state: "failed",
|
|
1309
|
-
output: runtimeFailure,
|
|
1310
|
-
finalMessageText: runtimeFailure,
|
|
1311
|
-
},
|
|
1312
|
-
};
|
|
1313
|
-
return;
|
|
1314
|
-
}
|
|
1315
|
-
try {
|
|
1316
|
-
syntheticFallback = {
|
|
1317
|
-
strategy: "stream-to-invoke",
|
|
1318
|
-
sourceStage: "stream",
|
|
1319
|
-
reason: error instanceof Error ? error.message : String(error),
|
|
1320
|
-
outcome: "recovered",
|
|
1321
|
-
};
|
|
1322
|
-
await options.emitSyntheticFallback(options.sessionId, options.requestId, options.selectedAgentId, syntheticFallback);
|
|
1323
|
-
const actual = await options.invokeWithHistory(options.binding, options.input, options.sessionId, options.requestId);
|
|
1324
|
-
await options.appendAssistantMessage(options.sessionId, options.requestId, actual.output);
|
|
1325
|
-
if (Array.isArray(actual.contentBlocks) && actual.contentBlocks.length > 0) {
|
|
1326
|
-
yield createContentBlocksItem(options.sessionId, options.requestId, options.selectedAgentId, actual.contentBlocks);
|
|
1327
|
-
}
|
|
1328
|
-
if (actual.output) {
|
|
1329
|
-
yield {
|
|
1330
|
-
type: "event",
|
|
1331
|
-
event: await options.emit(options.sessionId, options.requestId, 3, "output.delta", { content: actual.output }),
|
|
1332
|
-
};
|
|
1333
|
-
yield {
|
|
1334
|
-
type: "content",
|
|
1335
|
-
sessionId: options.sessionId,
|
|
1336
|
-
requestId: options.requestId,
|
|
1337
|
-
agentId: currentAgentId,
|
|
1338
|
-
content: actual.output,
|
|
1339
|
-
};
|
|
1340
|
-
}
|
|
1341
|
-
const terminalEvent = await options.setRequestStateAndEmit(options.sessionId, options.requestId, 6, actual.state, {
|
|
1342
|
-
previousState: "running",
|
|
1343
|
-
});
|
|
1344
|
-
yield {
|
|
1345
|
-
type: "event",
|
|
1346
|
-
event: terminalEvent,
|
|
1347
|
-
};
|
|
1348
|
-
yield {
|
|
1349
|
-
type: "result",
|
|
1350
|
-
result: {
|
|
1351
|
-
state: actual.state,
|
|
1352
|
-
output: actual.output,
|
|
1353
|
-
finalMessageText: actual.finalMessageText,
|
|
1354
|
-
outputContent: actual.outputContent,
|
|
1355
|
-
contentBlocks: actual.contentBlocks,
|
|
1356
|
-
structuredResponse: actual.structuredResponse,
|
|
1357
|
-
interruptContent: actual.interruptContent,
|
|
1358
|
-
approvalId: actual.approvalId,
|
|
1359
|
-
pendingActionId: actual.pendingActionId,
|
|
1360
|
-
delegationId: actual.delegationId,
|
|
1361
|
-
artifacts: actual.artifacts,
|
|
1362
|
-
sessionId: options.sessionId,
|
|
1363
|
-
requestId: options.requestId,
|
|
1364
|
-
agentId: currentAgentId,
|
|
1365
|
-
metadata: {
|
|
1366
|
-
...(actual.metadata ?? {}),
|
|
1367
|
-
syntheticFallback,
|
|
1368
|
-
},
|
|
1369
|
-
},
|
|
1370
|
-
};
|
|
1371
|
-
return;
|
|
1372
|
-
}
|
|
1373
|
-
catch (invokeError) {
|
|
1374
|
-
syntheticFallback = {
|
|
1375
|
-
strategy: "stream-to-invoke",
|
|
1376
|
-
sourceStage: "stream",
|
|
1377
|
-
reason: invokeError instanceof Error ? invokeError.message : String(invokeError),
|
|
1378
|
-
outcome: "failed",
|
|
1379
|
-
};
|
|
1380
|
-
await options.emitSyntheticFallback(options.sessionId, options.requestId, options.selectedAgentId, syntheticFallback);
|
|
1381
|
-
const runtimeFailure = renderRuntimeFailure(invokeError);
|
|
1382
|
-
const detailedError = describeRuntimeError(invokeError);
|
|
1383
|
-
yield {
|
|
1384
|
-
type: "event",
|
|
1385
|
-
event: await options.setRequestStateAndEmit(options.sessionId, options.requestId, 6, "failed", {
|
|
1386
|
-
previousState: "running",
|
|
1387
|
-
error: detailedError,
|
|
1388
|
-
}),
|
|
1389
|
-
};
|
|
1390
|
-
yield {
|
|
1391
|
-
type: "content",
|
|
1392
|
-
sessionId: options.sessionId,
|
|
1393
|
-
requestId: options.requestId,
|
|
1394
|
-
agentId: options.selectedAgentId,
|
|
1395
|
-
content: runtimeFailure,
|
|
1396
|
-
};
|
|
1397
|
-
yield {
|
|
1398
|
-
type: "result",
|
|
1399
|
-
result: {
|
|
1400
|
-
sessionId: options.sessionId,
|
|
1401
|
-
requestId: options.requestId,
|
|
1402
|
-
agentId: currentAgentId,
|
|
1403
|
-
state: "failed",
|
|
1404
|
-
output: runtimeFailure,
|
|
1405
|
-
finalMessageText: runtimeFailure,
|
|
1406
|
-
metadata: {
|
|
1407
|
-
syntheticFallback,
|
|
1408
|
-
},
|
|
1409
|
-
},
|
|
1410
|
-
};
|
|
1411
|
-
return;
|
|
1412
|
-
}
|
|
1413
|
-
}
|
|
1414
|
-
finally {
|
|
1415
|
-
await options.clearRequestInput(options.sessionId, options.requestId);
|
|
1416
|
-
await releaseRunSlot();
|
|
1417
|
-
}
|
|
1418
|
-
}
|
|
1
|
+
import{resolveDeterministicFinalOutput as fe}from"../../adapter/invocation-result.js";import{AGENT_INTERRUPT_SENTINEL_PREFIX as Ie,RuntimeOperationTimeoutError as Ee}from"../../agent-runtime-adapter.js";import{ExecutionReconciliationError as se}from"../../adapter/flow/stream-runtime.js";import{buildRequestPlanState as ye,summarizeBuiltinWriteTodosArgs as ge}from"../../adapter/runtime-adapter-support.js";import{mapTerminalStatusToPlanItemStatus as ae,mapTerminalStatusToRequestState as pe,readTerminalExecutionStatus as O}from"../../adapter/terminal-status.js";import{sanitizeVisibleText as L}from"../../parsing/output-parsing.js";import{describeRuntimeError as G,renderRuntimeFailure as J,renderToolFailure as ke}from"../../support/harness-support.js";import{getBindingPrimaryModel as Te}from"../../support/compiled-binding.js";import{createContentBlocksItem as ve,createToolResultKey as Re}from"../events/streaming.js";import{projectRuntimeSurfaceFromSingleUpstreamEvent as _e}from"./inspection.js";import{formatAgentName as be}from"../../../utils/agent-display.js";function nt(e,t,r){return{sessionId:e,requestId:t,version:0,updatedAt:r,items:[],summary:{total:0,pending:0,inProgress:0,completed:0,failed:0,cancelled:0}}}function xe(e){return e?e.summary.pending>0||e.summary.inProgress>0:!1}function N(e){return xe(e)}function $e(e){const t=typeof e=="string"?e.trim().toLowerCase().replace(/[\s-]+/gu,"_"):"";return t==="write_todos"||t==="read_todos"||t==="tool_call_write_todos"||t==="tool_call_read_todos"||t==="call_write_todos"||t==="call_read_todos"}function Pe(e){return!(L(e).trim().length<80)}function Ce(e,t={}){const r=O(e);if(r)return ae(r);const a=L(e).trim().toLowerCase();return a?a.startsWith("runtime_error=")?"failed":t.hasSuccessfulExecutionEvidence!==!0?null:Pe(e)?"completed":null:null}function Se(e,t){return e==="blocked"&&t?"completed":ae(e)}function X(e,t,r){const a=e.items.map(u=>({...u,status:u.status==="pending"||u.status==="in_progress"?t:u.status})),n={total:a.length,pending:0,inProgress:0,completed:a.filter(u=>u.status==="completed").length,failed:a.filter(u=>u.status==="failed").length,cancelled:a.filter(u=>u.status==="cancelled").length};return{...e,version:e.version+1,updatedAt:r,items:a,summary:n}}function qe(e){if(typeof e.output!="object"||e.output===null)return null;const t=e.output.summary;if(t&&typeof t=="object"&&Array.isArray(t.items)){const a=t;if(a.summary&&typeof a.summary=="object")return{sessionId:e.sessionId,requestId:e.requestId,version:e.version,updatedAt:e.updatedAt,items:a.items,summary:a.summary}}const r=ge(e.output);return ye({sessionId:e.sessionId,requestId:e.requestId,updatedAt:e.updatedAt,version:e.version,snapshot:r})}function we(e){if(typeof e.output!="object"||e.output===null)return null;const t=e.output.summary;if(!t||typeof t!="object"||!Array.isArray(t.items))return null;const r=t;return!r.summary||typeof r.summary!="object"?null:{sessionId:e.sessionId,requestId:e.requestId,version:e.version,updatedAt:e.updatedAt,items:r.items,summary:r.summary}}function M(e){return JSON.stringify({items:e.items,summary:e.summary})}function ie(e){return{total:e.length,pending:e.filter(t=>t.status==="pending").length,inProgress:e.filter(t=>t.status==="in_progress").length,completed:e.filter(t=>t.status==="completed").length,failed:e.filter(t=>t.status==="failed").length,cancelled:e.filter(t=>t.status==="cancelled").length}}function b(e){return typeof e.id=="string"&&e.id.trim().length>0?`id:${e.id.trim()}`:`content:${e.content.trim().toLowerCase()}`}function Q(e,t){if(!e||t.items.length>=e.items.length)return t;const r=new Map(e.items.map((u,c)=>[b(u),{item:u,index:c}])),a=new Map;for(const u of t.items){const c=b(u);if(!r.has(c)){const x=Y(e.items),k=Y(t.items);if(x>0&&k===0)return e;const w=e.items.map((i,q)=>{const T=t.items[q];return T?{...i,status:T.status}:i});return{...t,items:w,summary:ie(w)}}a.set(c,u)}const n=e.items.map(u=>{const c=a.get(b(u));return c?{...u,...c}:u});return{...t,items:n,summary:ie(n)}}function Y(e){return e.filter(t=>typeof t.id=="string"&&t.id.length>0).length}async function F(e,t,r){return await e.saveRequestPlanState?.(e.sessionId,e.requestId,r),[{type:"plan-state",sessionId:e.sessionId,requestId:e.requestId,agentId:t,planState:r}]}async function oe(e,t){const r=await e.loadRequestPlanState?.(e.sessionId,e.requestId);return r&&(!t||r.version>=t.version)?r:t}function ue(e,t){if(!e||e.items.length===0)return!0;if(t.items.length===0)return!1;const r=Y(e.items),a=Y(t.items);return!(r>0&&a===0&&t.items.length<e.items.length)}function de(e){if(typeof e!="object"||e===null)return null;const t=e;if(Array.isArray(t.todos))return t.todos;if(typeof t.output=="object"&&t.output!==null){const r=t.output.todos;if(Array.isArray(r))return r}return null}function Oe(e){if(typeof e.event!="object"||e.event===null)return null;const t=e.event,a=(t.event==="on_tool_start"&&t.name==="write_todos"?de(t.data?.input):null)??de(t.data?.output)??de(t.data?.chunk);return a?ye({sessionId:e.sessionId,requestId:e.requestId,updatedAt:e.updatedAt,version:e.version,snapshot:ge({todos:a})}):null}function Ne(e){const t=Array.isArray(e.metadata?.executedToolResults)?e.metadata.executedToolResults:[];for(const r of[...t].reverse()){const a=we({sessionId:e.sessionId,requestId:e.requestId,output:r.output,version:e.version,updatedAt:e.updatedAt});if(a)return a;if(r.toolName==="write_todos")return qe({sessionId:e.sessionId,requestId:e.requestId,output:r.output,version:e.version,updatedAt:e.updatedAt})}return null}function Me(e){return typeof e=="string"?e.startsWith(Ie)?{kind:"interrupt",content:e.slice(Ie.length)}:{kind:"content",content:e}:e.kind==="commentary"?{kind:"commentary",content:e.content??"",agentId:e.agentId}:e.kind==="upstream-event"?{kind:"upstream-event",event:e.event??{},agentId:e.agentId}:e.kind==="interrupt"?{kind:"interrupt",content:e.content,agentId:e.agentId}:e.kind==="reasoning"?{kind:"reasoning",content:e.content??"",agentId:e.agentId}:e.kind==="tool-result"?{kind:"tool-result",toolName:e.toolName??"unknown_tool",output:e.output,isError:e.isError,agentId:e.agentId}:e.kind==="profile"?{kind:"profile",step:e.step,agentId:e.agentId}:{kind:"content",content:e.content??"",agentId:e.agentId}}function Z(e){return e.split(`
|
|
2
|
+
`).map(t=>{const r=/^\s*/.exec(t)?.[0]??"",a=t.slice(r.length).replace(/[^\S\n]+/g," ").trimEnd();return`${r}${a}`}).join(`
|
|
3
|
+
`).replace(/\n{3,}/g,`
|
|
4
|
+
|
|
5
|
+
`).trim()}function Fe(e){const t=Z(e);return t&&(/[.!?]$/.test(t)?t:`${t}.`)}function B(e){if(e.summary.total<=0)return null;const t=n=>{switch(n){case"completed":return"[x]";case"in_progress":return"[~]";case"failed":return"[!]";case"cancelled":return"[-]";default:return"[ ]"}},r=e.items.slice(0,6).map(n=>` ${t(n.status)} ${n.content}`),a=e.items.length>r.length?[` ... ${e.items.length-r.length} more`]:[];return["TODO",...r,...a].join(`
|
|
6
|
+
`)}function Be(e,t){const r=new Map((e?.items??[]).map(n=>[b(n),n])),a=n=>{switch(n){case"completed":return"completed";case"failed":return"failed";case"cancelled":return"cancelled";default:return null}};return t.items.flatMap(n=>{const u=a(n.status);return u?r.get(b(n))?.status===n.status?[]:[{key:b(n),text:`TODO ${u}: ${n.content}.`,item:n}]:[]})}function j(e,t){if(!e)return[{planState:t}];const r=Be(e,t);if(r.length===0)return[{planState:t}];const a=Q(e,t),n=[],u=new Map,c=e?.items??[],x=a?.items??[];for(const i of c)u.set(b(i),n.length),n.push({...i});for(const i of x){const q=b(i);u.get(q)===void 0&&(u.set(q,n.length),n.push({...i}))}const k=new Set,w=[];for(const i of r){if(!i.item||k.has(i.key))continue;k.add(i.key);const q=n.findIndex(d=>b(d)===i.key),T=t.items.find(d=>b(d)===i.key);T&&(q>=0?n[q]={...n[q],...T}:n.push({...T}),w.push({planState:{...t,items:n.map(d=>({...d})),summary:ie(n)},commentary:i.text}))}return w.length===0?[{planState:t}]:w}function je(e){const t=Z(e.name);return t?e.kind==="tool"?e.status==="started"?`Running tool ${t}.`:e.status==="completed"?t.toLowerCase()==="task"?null:`Tool ${t} completed.`:e.status==="failed"?`Tool ${t} failed.`:null:e.kind==="skill"?e.status==="started"?`Applying skill ${t}.`:e.status==="completed"?`Skill ${t} applied.`:e.status==="failed"?`Skill ${t} failed.`:null:e.kind==="agent"?e.status==="started"?`Delegating work to ${t}.`:e.status==="completed"?null:e.status==="failed"?`Delegation to ${t} failed.`:null:e.kind==="llm"?e.status==="started"?`Running model ${t}.`:e.status==="completed"?`Model ${t} completed.`:e.status==="failed"?`Model ${t} failed.`:null:e.kind==="memory"?e.status==="started"?`Checking memory ${t}.`:e.status==="completed"?Fe(`Finished checking memory ${t}`):e.status==="failed"?`Memory ${t} failed.`:null:null:null}function he(e){const t=Z(e.name);if(!t||e.status!=="started")return null;if(e.kind==="memory")return`Checking memory ${t}.`;if(e.kind==="agent"&&e.action==="invoke")return"Running model invocation.";if(e.kind==="agent"&&e.action==="start")return"Starting runtime stream.";if(e.kind==="agent"&&e.action==="startup")return`Preparing ${t}.`;if(e.kind==="tool"){if(e.status==="started")return`Running tool ${t}.`;if(e.status==="completed")return`Tool ${t} completed.`;if(e.status==="failed")return`Tool ${t} failed.`}return null}function ze(e,t){return Te(e)?.provider!=="openai-compatible"?!1:(t instanceof Error?t.message:String(t)).toLowerCase().includes("received empty response from chat model call")}function De(e,t,r){if(r.length===0)return[];const a=new Date().toISOString(),n=Array.from(new Set(r.map(q=>q.scope))),u=n.length===1?n[0]:"mixed",c=r[0]?.summary,x=n.length===1?`durable ${u} memory`:"durable memory",k={source:"runtime-durable-memory",scope:u,scopes:n,count:r.length,summaries:r.map(q=>q.summary).slice(0,5),...c?{summary:c}:{}},w={id:`runtime-memory-recall:${t}:started`,kind:"memory",name:x,action:"recall",status:"started",occurredAt:a,detail:k},i={...w,id:`runtime-memory-recall:${t}:completed`,status:"completed"};return[{type:"profile-step",sessionId:e,requestId:t,step:w},{type:"profile-step",sessionId:e,requestId:t,step:i}]}function Ae(e){return typeof e=="object"&&e!==null?e:null}function We(e,t){const r=Ae(e),a=typeof r?.event=="string"?r.event:"";if(a.startsWith("on_chat_model_")||a.startsWith("on_llm_"))return!0;if(!a.startsWith("on_chain_")||r?.run_type==="tool")return!1;const n=typeof r?.name=="string"?r.name:"",u=Ae(r?.metadata),c=typeof u?.langgraph_node=="string"?u.langgraph_node:"",x=typeof u?.lc_agent_name=="string"?u.lc_agent_name:"";return n==="__start__"||c==="__start__"||c==="model_request"||n.endsWith(".before_agent")||n.endsWith(".before_model")||n===t&&x===t}function He(e){return(e?.items.length??0)>0}function Le(e){const t=e.trim().toLowerCase();return t.startsWith("runtime_error=")?t.includes("terminated")&&t.includes("other side closed")||t.includes("socket hang up")||t.includes("connection reset")||t.includes("econnreset"):!1}async function*rt(e){const t=Promise.resolve(e.isNewSession?[]:void 0).then(g=>g??e.loadPriorHistory(e.sessionId,e.requestId));yield{type:"event",event:await e.requestCreatedEventPromise};let r=0,a,n=null,u=async()=>{},c=!1,x=!1,k=!1,w=!0,i="",q=!1;const T=!0;let d=e.selectedAgentId,H=be(e.selectedAgentId),ee=[e.selectedAgentId],le=0,z;const te=[];let C=!1,U=!1,ce=null;const D=[],me=new Set,v=function*(g,E){const R=Z(g);!R||me.has(R)||(me.add(R),yield{type:"commentary",sessionId:e.sessionId,requestId:e.requestId,agentId:E??d,content:R})};try{const[g,E]=await Promise.all([t,e.releaseRequestSlotPromise]).then(([I,s])=>[I,s]);u=E;const R=e.invocation.memoryRecall?.items??[];for(const I of De(e.sessionId,e.requestId,R))if(yield I,I.type==="profile-step"){const s=he(I.step);s&&(yield*v(s))}for await(const I of e.stream(e.binding,e.input,e.sessionId,g,{context:e.invocation.context,state:e.invocation.state,files:e.invocation.files,requestId:e.requestId,memoryContext:e.invocation.memoryRecall?.prompt})){if(!I)continue;x=!0;const s=Me(I);if(s.kind==="upstream-event"){We(s.event,e.selectedAgentId)||(w=!1);const f=Oe({sessionId:e.sessionId,requestId:e.requestId,event:s.event,version:r+1,updatedAt:new Date().toISOString()});if(f){const l=Q(n,f),y=M(l);if(y!==a&&ue(n,l)){const h=n;r=l.version,a=y,n=l;const A=j(h,n);for(const $ of A){for(const P of await F(e,d,$.planState))yield P;$.commentary&&(yield*v($.commentary))}const _=B(n);_&&(yield*v(_))}}le+=1;const p=e.getBinding(d)??e.binding,m=_e({event:{agentId:d,agentName:H,event:s.event},binding:p,currentAgentId:d,currentAgentName:H,delegationChain:ee,sourceEventId:`upstream:${le}`});if(d=m.currentAgentId,H=m.currentAgentName,ee=m.delegationChain,await e.updateRequestInspection(e.sessionId,e.requestId,{currentAgentId:d,delegationChain:ee}),m.items.length===0){await e.appendRequestTraceItem(e.sessionId,e.requestId,{agentId:d,agentName:H,event:s.event}),yield{type:"upstream-event",sessionId:e.sessionId,requestId:e.requestId,event:s.event};continue}for(const l of m.items){await e.appendRequestTraceItem(e.sessionId,e.requestId,{agentId:l.agentId??d,agentName:l.agentName??H,surfaceItem:l,event:s.event}),yield{type:"upstream-event",sessionId:e.sessionId,requestId:e.requestId,surfaceItem:l,event:s.event};const y=je(l);y&&(yield*v(y))}continue}if(k=!0,s.kind==="interrupt"){const f=`checkpoints/${e.sessionId}/${e.requestId}/cp-1`,p=await e.setRequestStateAndEmit(e.sessionId,e.requestId,6,"waiting_for_approval",{previousState:"running",checkpointRef:f}),m=await e.requestApprovalAndEmit(e.sessionId,e.requestId,e.input,s.content,f,7);yield{type:"event",event:p},yield{type:"event",event:m.event},yield{type:"result",result:{sessionId:e.sessionId,requestId:e.requestId,agentId:d,state:"waiting_for_approval",output:i,finalMessageText:i,interruptContent:s.content,approvalId:m.approval.approvalId,pendingActionId:m.approval.pendingActionId}};return}if(s.kind==="commentary"){yield*v(s.content,s.agentId);continue}if(s.kind!=="reasoning"){if(s.kind==="profile"){yield{type:"profile-step",sessionId:e.sessionId,requestId:e.requestId,step:s.step};const f=he(s.step);f&&(yield*v(f));continue}if(s.kind==="tool-result"){const f=s.agentId??d,p=Re(s.toolName,s.output,s.isError);if(p===ce)continue;ce=p,D.push({toolName:s.toolName,output:s.output,isError:s.isError}),s.isError?te.push(ke(s.toolName,s.output)):(C=!0,!$e(s.toolName)&&s.toolName!=="task"&&(U=!0)),yield{type:"tool-result",sessionId:e.sessionId,requestId:e.requestId,agentId:f,toolName:s.toolName,output:s.output,isError:s.isError};const m=s.toolName==="write_todos"?qe({sessionId:e.sessionId,requestId:e.requestId,output:s.output,version:++r,updatedAt:new Date().toISOString()}):we({sessionId:e.sessionId,requestId:e.requestId,output:s.output,version:++r,updatedAt:new Date().toISOString()});if(m){const y=Q(n,m),h=M(y);if(h!==a&&ue(n,y)){const A=n;a=h,n=y;const _=j(A,n);for(const P of _){for(const re of await F(e,f,P.planState))yield re;P.commentary&&(yield*v(P.commentary,f))}const $=B(n);$&&(yield*v($,f))}}const l=s.toolName==="task"?O(s.output):null;if(l&&n&&N(n)){const y=X(n,ae(l),new Date().toISOString()),h=M(y);if(h!==a){const A=n;r=y.version,a=h,n=y;const _=j(A,n);for(const P of _){for(const re of await F(e,f,P.planState))yield re;P.commentary&&(yield*v(P.commentary,f))}const $=B(n);$&&(yield*v($,f))}}continue}c=!0,i+=s.content,!T&&(yield{type:"event",event:await e.emit(e.sessionId,e.requestId,3,"output.delta",{content:s.content})},yield{type:"content",sessionId:e.sessionId,requestId:e.requestId,agentId:d,content:s.content})}}!i&&te.length>0&&!C&&(i=te.join(`
|
|
7
|
+
|
|
8
|
+
`),c=!0);const K=fe({visibleOutput:i,executedToolResults:D});if(L(K)!==L(i)&&(i=K,c=c||i.length>0),n=await oe(e,n),i&&Le(i)&&D.length===0&&!C&&w&&!He(n)&&(i="",c=!1),!i){const I=await e.invokeWithHistory(e.binding,e.input,e.sessionId,e.requestId);Array.isArray(I.contentBlocks)&&I.contentBlocks.length>0&&(yield ve(e.sessionId,e.requestId,e.selectedAgentId,I.contentBlocks)),I.output&&(i=I.output,c=!0,q=!0);const s=Ne({sessionId:e.sessionId,requestId:e.requestId,metadata:I.metadata,version:r+1,updatedAt:new Date().toISOString()});if(s){const p=Q(n,s),m=M(p);if(m!==a&&ue(n,p)){const l=n;r=p.version,a=m,n=p;const y=j(l,n);for(const A of y){for(const _ of await F(e,d,A.planState))yield _;A.commentary&&(yield*v(A.commentary))}const h=B(n);h&&(yield*v(h))}}n=await oe(e,n);const f=O(I.structuredResponse);if(f&&n&&N(n)){const p=X(n,Se(f,C),new Date().toISOString()),m=M(p);if(m!==a){const l=n;r=p.version,a=m,n=p;const y=j(l,n);for(const A of y){for(const _ of await F(e,d,A.planState))yield _;A.commentary&&(yield*v(A.commentary))}const h=B(n);h&&(yield*v(h))}}}n=await oe(e,n);const V=O(i);let W=Ce(i,{hasSuccessfulExecutionEvidence:U});if(V==="blocked"&&U&&(W="completed"),W==="failed"&&U&&!V&&!L(i).trim().toLowerCase().startsWith("runtime_error=")&&i.trim()&&(W="completed"),W&&n&&N(n)){const I=X(n,W,new Date().toISOString()),s=M(I);if(s!==a){const f=n;a=s,n=I;const p=j(f,n);for(const l of p){for(const y of await F(e,d,l.planState))yield y;l.commentary&&(yield*v(l.commentary))}const m=B(n);m&&(yield*v(m))}}if(q&&k&&N(n))throw new se("Agent ended while the streamed plan state still had unfinished work.");if(N(n))throw new se("Agent ended while the streamed plan state still had unfinished work.");i&&T&&(yield{type:"event",event:await e.emit(e.sessionId,e.requestId,3,"output.delta",{content:i})},yield{type:"content",sessionId:e.sessionId,requestId:e.requestId,agentId:d,content:i});const o=O(i),S=pe(o);await e.appendAssistantMessage(e.sessionId,e.requestId,i),yield{type:"event",event:await e.setRequestStateAndEmit(e.sessionId,e.requestId,6,S,{previousState:"running"})},yield{type:"result",result:{sessionId:e.sessionId,requestId:e.requestId,agentId:d,state:S,output:i,finalMessageText:i,...o?{metadata:{terminalStatus:o}}:{}}}}catch(g){const E=fe({visibleOutput:i,executedToolResults:D}),R=O(E),K=!n||!N(n)||!!R;if(!i&&C&&E&&K){if(R&&n&&N(n)){const I=X(n,Se(R,C),new Date().toISOString()),s=M(I);if(s!==a){const f=n;a=s,n=I;const p=j(f,n);for(const l of p){for(const y of await F(e,d,l.planState))yield y;l.commentary&&(yield*v(l.commentary))}const m=B(n);m&&(yield*v(m))}}await e.appendAssistantMessage(e.sessionId,e.requestId,E),yield{type:"event",event:await e.emit(e.sessionId,e.requestId,3,"output.delta",{content:E})},yield{type:"content",sessionId:e.sessionId,requestId:e.requestId,agentId:d,content:E};const o=O(E),S=pe(o);yield{type:"event",event:await e.setRequestStateAndEmit(e.sessionId,e.requestId,6,S,{previousState:"running"})},yield{type:"result",result:{sessionId:e.sessionId,requestId:e.requestId,agentId:d,state:S,output:E,finalMessageText:E,metadata:{executedToolResults:D,...o?{terminalStatus:o}:{}}}};return}const V=!i&&ze(e.binding,g);if((c||k||C||D.length>0||!w)&&!V){const o=J(g),S=G(g);yield{type:"event",event:await e.setRequestStateAndEmit(e.sessionId,e.requestId,6,"failed",{previousState:"running",error:S})},yield{type:"content",sessionId:e.sessionId,requestId:e.requestId,agentId:e.selectedAgentId,content:o},yield{type:"result",result:{sessionId:e.sessionId,requestId:e.requestId,agentId:d,state:"failed",output:o,finalMessageText:o}};return}if(g instanceof Ee&&g.stage==="invoke"){const o=J(g),S=G(g);yield{type:"event",event:await e.setRequestStateAndEmit(e.sessionId,e.requestId,6,"failed",{previousState:"running",error:S})},yield{type:"content",sessionId:e.sessionId,requestId:e.requestId,agentId:e.selectedAgentId,content:o},yield{type:"result",result:{sessionId:e.sessionId,requestId:e.requestId,agentId:e.selectedAgentId,state:"failed",output:o,finalMessageText:o}};return}if(g instanceof se){const o=J(g),S=G(g);yield{type:"event",event:await e.setRequestStateAndEmit(e.sessionId,e.requestId,6,"failed",{previousState:"running",error:S})},yield{type:"content",sessionId:e.sessionId,requestId:e.requestId,agentId:e.selectedAgentId,content:o},yield{type:"result",result:{sessionId:e.sessionId,requestId:e.requestId,agentId:d,state:"failed",output:o,finalMessageText:o}};return}try{z={strategy:"stream-to-invoke",sourceStage:"stream",reason:g instanceof Error?g.message:String(g),outcome:"recovered"},await e.emitSyntheticFallback(e.sessionId,e.requestId,e.selectedAgentId,z);const o=await e.invokeWithHistory(e.binding,e.input,e.sessionId,e.requestId);await e.appendAssistantMessage(e.sessionId,e.requestId,o.output),Array.isArray(o.contentBlocks)&&o.contentBlocks.length>0&&(yield ve(e.sessionId,e.requestId,e.selectedAgentId,o.contentBlocks)),o.output&&(yield{type:"event",event:await e.emit(e.sessionId,e.requestId,3,"output.delta",{content:o.output})},yield{type:"content",sessionId:e.sessionId,requestId:e.requestId,agentId:d,content:o.output}),yield{type:"event",event:await e.setRequestStateAndEmit(e.sessionId,e.requestId,6,o.state,{previousState:"running"})},yield{type:"result",result:{state:o.state,output:o.output,finalMessageText:o.finalMessageText,outputContent:o.outputContent,contentBlocks:o.contentBlocks,structuredResponse:o.structuredResponse,interruptContent:o.interruptContent,approvalId:o.approvalId,pendingActionId:o.pendingActionId,delegationId:o.delegationId,artifacts:o.artifacts,sessionId:e.sessionId,requestId:e.requestId,agentId:d,metadata:{...o.metadata??{},syntheticFallback:z}}};return}catch(o){z={strategy:"stream-to-invoke",sourceStage:"stream",reason:o instanceof Error?o.message:String(o),outcome:"failed"},await e.emitSyntheticFallback(e.sessionId,e.requestId,e.selectedAgentId,z);const S=J(o),ne=G(o);yield{type:"event",event:await e.setRequestStateAndEmit(e.sessionId,e.requestId,6,"failed",{previousState:"running",error:ne})},yield{type:"content",sessionId:e.sessionId,requestId:e.requestId,agentId:e.selectedAgentId,content:S},yield{type:"result",result:{sessionId:e.sessionId,requestId:e.requestId,agentId:d,state:"failed",output:S,finalMessageText:S,metadata:{syntheticFallback:z}}};return}}finally{await e.clearRequestInput(e.sessionId,e.requestId),await u()}}export{rt as streamHarnessRun};
|