@botbotgo/agent-harness 0.0.474 → 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 +2 -2
- 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 -411
- 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 -1089
- 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
package/dist/cli/chat-stream.js
CHANGED
|
@@ -1,512 +1,23 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
let todoPanelVisible = false;
|
|
25
|
-
let lastRenderedTodoPanelLineCount = 0;
|
|
26
|
-
let liveRequestAnnotations = [];
|
|
27
|
-
let persistedRequestTreeEventCount = 0;
|
|
28
|
-
let persistedRequestTreeTodoSignature = "";
|
|
29
|
-
let previousTodoStatuses = new Map();
|
|
30
|
-
const renderedTodoTransitionLines = new Set();
|
|
31
|
-
const requestTreeRenderThrottleMs = 75;
|
|
32
|
-
let suppressRequestTreeRendering = false;
|
|
33
|
-
let lastStableRequestTreeKey;
|
|
34
|
-
let idleProgressTimer;
|
|
35
|
-
let lastRuntimeProgressAt = requestStartedAt;
|
|
36
|
-
let stdoutWriteChain = Promise.resolve();
|
|
37
|
-
let stderrWriteChain = Promise.resolve();
|
|
38
|
-
const enqueueChatWrite = (sink, _stream, chain, message) => chain.then(async () => {
|
|
39
|
-
sink(message);
|
|
40
|
-
});
|
|
41
|
-
const summarizeRequestTreeSteps = (steps) => steps
|
|
42
|
-
.map((step) => [
|
|
43
|
-
step.id ?? "",
|
|
44
|
-
step.kind,
|
|
45
|
-
step.name,
|
|
46
|
-
step.status,
|
|
47
|
-
step.startedAt ?? "",
|
|
48
|
-
step.endedAt ?? "",
|
|
49
|
-
].join("|"))
|
|
50
|
-
.join(";");
|
|
51
|
-
const buildRequestSnapshotRenderKey = (snapshot) => {
|
|
52
|
-
const todoSignatures = snapshot.plan.items
|
|
53
|
-
.map((item) => [
|
|
54
|
-
item.id ?? "",
|
|
55
|
-
item.content,
|
|
56
|
-
item.status,
|
|
57
|
-
item.ownerAgentId ?? "",
|
|
58
|
-
item.startedAt ?? "",
|
|
59
|
-
item.endedAt ?? "",
|
|
60
|
-
item.result === undefined ? "" : String(item.result),
|
|
61
|
-
summarizeRequestTreeSteps(item.events),
|
|
62
|
-
].join("|"))
|
|
63
|
-
.join(";");
|
|
64
|
-
const approvalSignature = snapshot.approval
|
|
65
|
-
? [
|
|
66
|
-
snapshot.approval.approvalId,
|
|
67
|
-
snapshot.approval.status,
|
|
68
|
-
snapshot.approval.toolName ?? "",
|
|
69
|
-
].join("|")
|
|
70
|
-
: "";
|
|
71
|
-
return [
|
|
72
|
-
snapshot.sessionId,
|
|
73
|
-
snapshot.requestId,
|
|
74
|
-
snapshot.state,
|
|
75
|
-
snapshot.agentId ?? "",
|
|
76
|
-
snapshot.plan.version,
|
|
77
|
-
snapshot.plan.updatedAt,
|
|
78
|
-
snapshot.plan.summary.total,
|
|
79
|
-
snapshot.plan.summary.inProgress,
|
|
80
|
-
snapshot.plan.summary.completed,
|
|
81
|
-
snapshot.plan.summary.failed,
|
|
82
|
-
approvalSignature,
|
|
83
|
-
summarizeRequestTreeSteps(snapshot.events),
|
|
84
|
-
todoSignatures,
|
|
85
|
-
].join("||");
|
|
86
|
-
};
|
|
87
|
-
const formatPerfClock = (timestamp) => {
|
|
88
|
-
const value = new Date(timestamp);
|
|
89
|
-
return value.toLocaleTimeString("en-US", {
|
|
90
|
-
hour12: false,
|
|
91
|
-
hour: "2-digit",
|
|
92
|
-
minute: "2-digit",
|
|
93
|
-
second: "2-digit",
|
|
94
|
-
fractionalSecondDigits: 3,
|
|
95
|
-
});
|
|
96
|
-
};
|
|
97
|
-
const formatElapsed = (timestamp) => `${((timestamp - requestStartedAt) / 1000).toFixed(3)}s`;
|
|
98
|
-
const buildTimingSummary = (completedAt) => {
|
|
99
|
-
const parts = [`start ${formatPerfClock(requestStartedAt)}`];
|
|
100
|
-
if (firstSnapshotAt) {
|
|
101
|
-
parts.push(`first event +${formatElapsed(firstSnapshotAt)}`);
|
|
102
|
-
}
|
|
103
|
-
if (firstDataAt) {
|
|
104
|
-
parts.push(`first data +${formatElapsed(firstDataAt)}`);
|
|
105
|
-
}
|
|
106
|
-
if (completedAt) {
|
|
107
|
-
parts.push(`done +${formatElapsed(completedAt)}`);
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
parts.push(`live +${formatElapsed(Date.now())}`);
|
|
111
|
-
}
|
|
112
|
-
return parts.join(" · ");
|
|
113
|
-
};
|
|
114
|
-
const buildLiveRequestTreeBlock = () => {
|
|
115
|
-
if (!lastRenderedRequestTree) {
|
|
116
|
-
return "";
|
|
117
|
-
}
|
|
118
|
-
const annotationBlock = liveRequestAnnotations.length > 0
|
|
119
|
-
? `\n${liveRequestAnnotations.join("")}`
|
|
120
|
-
: "";
|
|
121
|
-
return `${lastRenderedRequestTree}${annotationBlock}`;
|
|
122
|
-
};
|
|
123
|
-
const clearLiveRequestTree = () => {
|
|
124
|
-
if (!input.requestEvents || !input.liveRequestTree || !requestTreeVisible || lastRenderedRequestTreeLineCount <= 0) {
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
stderrWriteChain = enqueueChatWrite(input.stderr, input.stderrStream, stderrWriteChain, `\x1b[${lastRenderedRequestTreeLineCount}F\x1b[0J`);
|
|
128
|
-
requestTreeVisible = false;
|
|
129
|
-
};
|
|
130
|
-
const drawLiveRequestTree = () => {
|
|
131
|
-
if (!input.requestEvents || !input.liveRequestTree || !lastRenderedRequestTree) {
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
const block = buildLiveRequestTreeBlock();
|
|
135
|
-
stderrWriteChain = enqueueChatWrite(input.stderr, input.stderrStream, stderrWriteChain, block);
|
|
136
|
-
lastRenderedRequestTreeLineCount = countRenderedLines(block);
|
|
137
|
-
requestTreeVisible = true;
|
|
138
|
-
};
|
|
139
|
-
const persistLiveRequestTree = () => {
|
|
140
|
-
if (!input.requestEvents || !input.liveRequestTree || requestTreePersisted || !lastRenderedRequestTree) {
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
if (requestTreeVisible && lastRenderedRequestTreeLineCount > 0) {
|
|
144
|
-
stderrWriteChain = enqueueChatWrite(input.stderr, input.stderrStream, stderrWriteChain, `\x1b[${lastRenderedRequestTreeLineCount}F\x1b[0J`);
|
|
145
|
-
requestTreeVisible = false;
|
|
146
|
-
}
|
|
147
|
-
const combinedBlock = buildLiveRequestTreeBlock();
|
|
148
|
-
const treeBlock = combinedBlock.endsWith("\n")
|
|
149
|
-
? combinedBlock
|
|
150
|
-
: `${combinedBlock}\n`;
|
|
151
|
-
stderrWriteChain = enqueueChatWrite(input.stderr, input.stderrStream, stderrWriteChain, treeBlock);
|
|
152
|
-
persistedRequestTreeEventCount = latestSnapshot ? flattenRequestExecutionSteps(latestSnapshot).length : 0;
|
|
153
|
-
persistedRequestTreeTodoSignature = latestSnapshot ? buildTodoContinuationSignature(latestSnapshot) : "";
|
|
154
|
-
requestTreePersisted = true;
|
|
155
|
-
};
|
|
156
|
-
const enqueueOrderedStdout = (message) => {
|
|
157
|
-
const barrier = Promise.allSettled([stdoutWriteChain, stderrWriteChain]).then(() => undefined);
|
|
158
|
-
stdoutWriteChain = enqueueChatWrite(input.stdout, input.stdoutStream, barrier, message);
|
|
159
|
-
};
|
|
160
|
-
const writeChatStdout = (message) => {
|
|
161
|
-
todoPanelVisible = false;
|
|
162
|
-
if (input.requestEvents && input.liveRequestTree && !suppressRequestTreeRendering) {
|
|
163
|
-
clearLiveRequestTree();
|
|
164
|
-
enqueueOrderedStdout(message);
|
|
165
|
-
drawLiveRequestTree();
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
if (input.requestEvents && input.liveRequestTree) {
|
|
169
|
-
enqueueOrderedStdout(message);
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
stdoutWriteChain = enqueueChatWrite(input.stdout, input.stdoutStream, stdoutWriteChain, message);
|
|
173
|
-
};
|
|
174
|
-
const writeChatStderr = (message) => {
|
|
175
|
-
todoPanelVisible = false;
|
|
176
|
-
if (input.requestEvents && input.liveRequestTree && !suppressRequestTreeRendering) {
|
|
177
|
-
clearLiveRequestTree();
|
|
178
|
-
stderrWriteChain = enqueueChatWrite(input.stderr, input.stderrStream, stderrWriteChain, message);
|
|
179
|
-
drawLiveRequestTree();
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
stderrWriteChain = enqueueChatWrite(input.stderr, input.stderrStream, stderrWriteChain, message);
|
|
183
|
-
};
|
|
184
|
-
const parseTerminalTodoPanel = (rawText) => {
|
|
185
|
-
const lines = rawText.split("\n");
|
|
186
|
-
if (lines[0]?.trim() !== "TODO") {
|
|
187
|
-
return null;
|
|
188
|
-
}
|
|
189
|
-
const items = lines.slice(1).map((line) => {
|
|
190
|
-
const match = line.match(/^\s*\[([ x~!-])\]\s+(.+?)\s*$/);
|
|
191
|
-
if (!match) {
|
|
192
|
-
return null;
|
|
193
|
-
}
|
|
194
|
-
const marker = match[1];
|
|
195
|
-
const status = marker === "x"
|
|
196
|
-
? "completed"
|
|
197
|
-
: marker === "~"
|
|
198
|
-
? "in_progress"
|
|
199
|
-
: marker === "!"
|
|
200
|
-
? "failed"
|
|
201
|
-
: marker === "-"
|
|
202
|
-
? "cancelled"
|
|
203
|
-
: "pending";
|
|
204
|
-
return {
|
|
205
|
-
marker: marker === " " ? " " : marker,
|
|
206
|
-
content: match[2],
|
|
207
|
-
status,
|
|
208
|
-
};
|
|
209
|
-
}).filter((item) => item !== null);
|
|
210
|
-
return items.length > 0 ? items : null;
|
|
211
|
-
};
|
|
212
|
-
const renderTerminalTodoPanel = (items, agentId) => {
|
|
213
|
-
const now = Date.now();
|
|
214
|
-
const completed = items.filter((item) => item.status === "completed").length;
|
|
215
|
-
const inProgress = items.filter((item) => item.status === "in_progress").length;
|
|
216
|
-
const failed = items.filter((item) => item.status === "failed").length;
|
|
217
|
-
const cancelled = items.filter((item) => item.status === "cancelled").length;
|
|
218
|
-
const pending = items.filter((item) => item.status === "pending").length;
|
|
219
|
-
const summary = [
|
|
220
|
-
`${completed}/${items.length} done`,
|
|
221
|
-
`${inProgress} active`,
|
|
222
|
-
`${pending} pending`,
|
|
223
|
-
failed > 0 ? `${failed} failed` : null,
|
|
224
|
-
cancelled > 0 ? `${cancelled} cancelled` : null,
|
|
225
|
-
].filter((part) => part !== null).join(" | ");
|
|
226
|
-
const header = `[${formatPerfClock(now)} +${formatElapsed(now)}]${formatAgentProgressLabel(agentId || latestAgentId)} TODO Burn Down | ${summary}`;
|
|
227
|
-
const rows = items.map((item) => ` [${item.marker}] ${item.content}`);
|
|
228
|
-
return `${header}\n${rows.join("\n")}\n`;
|
|
229
|
-
};
|
|
230
|
-
const drawTerminalTodoPanel = (items, agentId) => {
|
|
231
|
-
const panel = renderTerminalTodoPanel(items, agentId);
|
|
232
|
-
const clearPreviousPanel = todoPanelVisible && lastRenderedTodoPanelLineCount > 0
|
|
233
|
-
? `\x1b[${lastRenderedTodoPanelLineCount}F\x1b[0J`
|
|
234
|
-
: "";
|
|
235
|
-
stderrWriteChain = enqueueChatWrite(input.stderr, input.stderrStream, stderrWriteChain, `${clearPreviousPanel}${panel}`);
|
|
236
|
-
lastRenderedTodoPanelLineCount = countRenderedLines(panel);
|
|
237
|
-
todoPanelVisible = true;
|
|
238
|
-
};
|
|
239
|
-
const emitProgressCommentary = (rawText, agentId) => {
|
|
240
|
-
const lines = rawText.split("\n").filter((line) => line.length > 0);
|
|
241
|
-
if (lines.length === 0) {
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
244
|
-
const now = Date.now();
|
|
245
|
-
const linePrefix = `[${formatPerfClock(now)} +${formatElapsed(now)}]${formatAgentProgressLabel(agentId || latestAgentId)}`;
|
|
246
|
-
const serializedLines = lines
|
|
247
|
-
.map((line) => {
|
|
248
|
-
const signature = `${agentId || latestAgentId}|${line}`;
|
|
249
|
-
if (renderedTodoTransitionLines.has(signature)) {
|
|
250
|
-
return null;
|
|
251
|
-
}
|
|
252
|
-
renderedTodoTransitionLines.add(signature);
|
|
253
|
-
return `${linePrefix} ${line}\n`;
|
|
254
|
-
})
|
|
255
|
-
.filter((line) => line !== null);
|
|
256
|
-
if (serializedLines.length === 0) {
|
|
257
|
-
return;
|
|
258
|
-
}
|
|
259
|
-
if (input.requestEvents && input.liveRequestTree && !suppressRequestTreeRendering && lastRenderedRequestTree) {
|
|
260
|
-
liveRequestAnnotations.push(...serializedLines);
|
|
261
|
-
clearLiveRequestTree();
|
|
262
|
-
drawLiveRequestTree();
|
|
263
|
-
return;
|
|
264
|
-
}
|
|
265
|
-
writeChatStderr(serializedLines.join(""));
|
|
266
|
-
};
|
|
267
|
-
const clearIdleProgressTimer = () => {
|
|
268
|
-
if (idleProgressTimer) {
|
|
269
|
-
clearTimeout(idleProgressTimer);
|
|
270
|
-
idleProgressTimer = undefined;
|
|
271
|
-
}
|
|
272
|
-
};
|
|
273
|
-
const scheduleIdleProgress = () => {
|
|
274
|
-
clearIdleProgressTimer();
|
|
275
|
-
if (idleProgressMs <= 0) {
|
|
276
|
-
return;
|
|
277
|
-
}
|
|
278
|
-
idleProgressTimer = setTimeout(() => {
|
|
279
|
-
const idleSeconds = Math.max(1, Math.round((Date.now() - lastRuntimeProgressAt) / 1000));
|
|
280
|
-
writeChatStderr(`[${formatPerfClock(Date.now())} +${formatElapsed(Date.now())}]${formatAgentProgressLabel(latestAgentId)} Still working; no runtime update for ${idleSeconds}s.\n`);
|
|
281
|
-
scheduleIdleProgress();
|
|
282
|
-
}, idleProgressMs);
|
|
283
|
-
};
|
|
284
|
-
const markRuntimeProgress = () => {
|
|
285
|
-
lastRuntimeProgressAt = Date.now();
|
|
286
|
-
scheduleIdleProgress();
|
|
287
|
-
};
|
|
288
|
-
const suspendRequestTreeRendering = () => {
|
|
289
|
-
if (!input.requestEvents || !input.liveRequestTree) {
|
|
290
|
-
return;
|
|
291
|
-
}
|
|
292
|
-
persistLiveRequestTree();
|
|
293
|
-
suppressRequestTreeRendering = true;
|
|
294
|
-
clearLiveRequestTree();
|
|
295
|
-
};
|
|
296
|
-
const writeAssistantOutput = (nextOutput) => {
|
|
297
|
-
const incremental = computeIncrementalOutput(renderedAssistantOutput, nextOutput);
|
|
298
|
-
renderedAssistantOutput = incremental.accumulated;
|
|
299
|
-
if (!incremental.delta) {
|
|
300
|
-
return false;
|
|
301
|
-
}
|
|
302
|
-
if (input.requestEvents) {
|
|
303
|
-
suspendRequestTreeRendering();
|
|
304
|
-
}
|
|
305
|
-
writeChatStdout(renderChatTextChunk(incremental.delta, input.modelInfo));
|
|
306
|
-
wroteContent = true;
|
|
307
|
-
return true;
|
|
308
|
-
};
|
|
309
|
-
const formatAgentProgressLabel = (agentId) => agentId && agentId.trim().length > 0 ? ` agent:${agentId}` : "";
|
|
310
|
-
const buildTodoTransitionKey = (todo) => todo.key || todo.id || `content:${todo.content.trim().toLowerCase()}`;
|
|
311
|
-
const readTerminalTodoTransitionLabel = (status) => {
|
|
312
|
-
if (status === "completed") {
|
|
313
|
-
return "completed";
|
|
314
|
-
}
|
|
315
|
-
if (status === "failed") {
|
|
316
|
-
return "failed";
|
|
317
|
-
}
|
|
318
|
-
if (status === "cancelled") {
|
|
319
|
-
return "cancelled";
|
|
320
|
-
}
|
|
321
|
-
return null;
|
|
322
|
-
};
|
|
323
|
-
const writeTodoTransitionProgress = (snapshot) => {
|
|
324
|
-
const nextStatuses = new Map();
|
|
325
|
-
const lines = [];
|
|
326
|
-
for (const todo of snapshot.plan.items) {
|
|
327
|
-
const key = buildTodoTransitionKey(todo);
|
|
328
|
-
nextStatuses.set(key, todo.status);
|
|
329
|
-
const label = readTerminalTodoTransitionLabel(todo.status);
|
|
330
|
-
if (!label) {
|
|
331
|
-
continue;
|
|
332
|
-
}
|
|
333
|
-
const previousStatus = previousTodoStatuses.get(key);
|
|
334
|
-
if (!previousStatus || previousStatus === todo.status) {
|
|
335
|
-
continue;
|
|
336
|
-
}
|
|
337
|
-
const text = `TODO ${label}: ${todo.content}.`;
|
|
338
|
-
const signature = `${snapshot.agentId || latestAgentId || ""}|${text}`;
|
|
339
|
-
if (renderedTodoTransitionLines.has(signature)) {
|
|
340
|
-
continue;
|
|
341
|
-
}
|
|
342
|
-
renderedTodoTransitionLines.add(signature);
|
|
343
|
-
lines.push(`[${formatPerfClock(Date.now())} +${formatElapsed(Date.now())}]${formatAgentProgressLabel(snapshot.agentId)} ${text}\n`);
|
|
344
|
-
}
|
|
345
|
-
previousTodoStatuses = nextStatuses;
|
|
346
|
-
if (lines.length === 0) {
|
|
347
|
-
return;
|
|
348
|
-
}
|
|
349
|
-
writeChatStderr(lines.join(""));
|
|
350
|
-
};
|
|
351
|
-
const renderContentBlocks = (contentBlocks, agentId) => {
|
|
352
|
-
entryAgentId ??= agentId;
|
|
353
|
-
latestAgentId = agentId || latestAgentId;
|
|
354
|
-
const rendered = contentBlocks
|
|
355
|
-
.map((block) => {
|
|
356
|
-
if (typeof block === "string") {
|
|
357
|
-
return block;
|
|
358
|
-
}
|
|
359
|
-
if (block && typeof block === "object" && "text" in block && typeof block.text === "string") {
|
|
360
|
-
return block.text;
|
|
361
|
-
}
|
|
362
|
-
return "";
|
|
363
|
-
})
|
|
364
|
-
.filter((block) => block.trim().length > 0)
|
|
365
|
-
.join("");
|
|
366
|
-
if (rendered && writeAssistantOutput(rendered)) {
|
|
367
|
-
wroteRenderableBlocks = true;
|
|
368
|
-
}
|
|
369
|
-
};
|
|
370
|
-
const result = await input.client.request({
|
|
371
|
-
...(input.agentId ? { agentId: input.agentId } : {}),
|
|
372
|
-
...(input.sessionId ? { sessionId: input.sessionId } : {}),
|
|
373
|
-
input: input.message,
|
|
374
|
-
eventListener(snapshot) {
|
|
375
|
-
markRuntimeProgress();
|
|
376
|
-
latestSessionId = snapshot.sessionId || latestSessionId;
|
|
377
|
-
latestRequestId = snapshot.requestId || latestRequestId;
|
|
378
|
-
entryAgentId ??= snapshot.agentId;
|
|
379
|
-
latestAgentId = snapshot.agentId || latestAgentId;
|
|
380
|
-
latestSnapshot = snapshot;
|
|
381
|
-
firstSnapshotAt ??= Date.now();
|
|
382
|
-
if (!input.requestEvents) {
|
|
383
|
-
writeTodoTransitionProgress(snapshot);
|
|
384
|
-
}
|
|
385
|
-
if (input.requestEvents && !suppressRequestTreeRendering) {
|
|
386
|
-
const now = Date.now();
|
|
387
|
-
const nextTreeKey = buildRequestSnapshotRenderKey(snapshot);
|
|
388
|
-
const terminalSnapshot = snapshot.state !== "queued" && snapshot.state !== "claimed" && snapshot.state !== "running";
|
|
389
|
-
const snapshotCarriesRenderableOutput = snapshot.output.trim().length > 0;
|
|
390
|
-
const shouldRenderSnapshot = !snapshotCarriesRenderableOutput
|
|
391
|
-
&& nextTreeKey !== lastRenderedRequestTreeKey
|
|
392
|
-
&& nextTreeKey !== lastStableRequestTreeKey
|
|
393
|
-
&& ((terminalSnapshot && !input.liveRequestTree)
|
|
394
|
-
|| !input.liveRequestTree
|
|
395
|
-
|| now - lastRenderedRequestTreeAt >= requestTreeRenderThrottleMs);
|
|
396
|
-
if (shouldRenderSnapshot) {
|
|
397
|
-
lastRenderedRequestTree = renderRequestSnapshotTree(snapshot, input.colorRequestTree === true, buildTimingSummary());
|
|
398
|
-
if (input.liveRequestTree) {
|
|
399
|
-
clearLiveRequestTree();
|
|
400
|
-
drawLiveRequestTree();
|
|
401
|
-
}
|
|
402
|
-
else {
|
|
403
|
-
input.stderr(`\n${lastRenderedRequestTree}`);
|
|
404
|
-
}
|
|
405
|
-
lastRenderedRequestTreeKey = nextTreeKey;
|
|
406
|
-
if (!snapshot.activeEventId && snapshot.state !== "running") {
|
|
407
|
-
lastStableRequestTreeKey = nextTreeKey;
|
|
408
|
-
}
|
|
409
|
-
lastRenderedRequestTreeAt = now;
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
if ((input.showRunningState ?? true) && !input.requestEvents && !announcedRunningState && !wroteContent && !wroteRenderableBlocks) {
|
|
413
|
-
input.stderr(renderChatRequestRunning({
|
|
414
|
-
sessionId: snapshot.sessionId,
|
|
415
|
-
requestId: snapshot.requestId,
|
|
416
|
-
agentId: snapshot.agentId,
|
|
417
|
-
}));
|
|
418
|
-
announcedRunningState = true;
|
|
419
|
-
}
|
|
420
|
-
},
|
|
421
|
-
dataListener(delta) {
|
|
422
|
-
markRuntimeProgress();
|
|
423
|
-
latestSessionId = delta.sessionId || latestSessionId;
|
|
424
|
-
latestRequestId = delta.requestId || latestRequestId;
|
|
425
|
-
firstDataAt ??= Date.now();
|
|
426
|
-
if (delta.type === "output.text.delta") {
|
|
427
|
-
entryAgentId ??= delta.agentId;
|
|
428
|
-
latestAgentId = delta.agentId || latestAgentId;
|
|
429
|
-
writeAssistantOutput(delta.text);
|
|
430
|
-
return;
|
|
431
|
-
}
|
|
432
|
-
if (delta.type === "output.content-blocks") {
|
|
433
|
-
entryAgentId ??= delta.agentId;
|
|
434
|
-
suspendRequestTreeRendering();
|
|
435
|
-
renderContentBlocks(delta.contentBlocks, delta.agentId);
|
|
436
|
-
return;
|
|
437
|
-
}
|
|
438
|
-
if (delta.type === "plan.step") {
|
|
439
|
-
entryAgentId ??= delta.agentId;
|
|
440
|
-
latestAgentId = delta.agentId || latestAgentId;
|
|
441
|
-
const item = delta.item;
|
|
442
|
-
const status = typeof item?.status === "string" ? item.status : "unknown";
|
|
443
|
-
const content = typeof item?.content === "string" ? item.content : "";
|
|
444
|
-
if ((input.showToolResults ?? true) && !input.requestEvents && content.trim().length > 0) {
|
|
445
|
-
writeChatStderr(`\n[${formatPerfClock(Date.now())} +${formatElapsed(Date.now())}]${formatAgentProgressLabel(delta.agentId)} [todo:${status}] ${content}\n`);
|
|
446
|
-
}
|
|
447
|
-
return;
|
|
448
|
-
}
|
|
449
|
-
if (delta.type === "tool.result") {
|
|
450
|
-
entryAgentId ??= delta.agentId;
|
|
451
|
-
latestAgentId = delta.agentId || latestAgentId;
|
|
452
|
-
if ((input.showToolResults ?? true) && !input.requestEvents) {
|
|
453
|
-
writeChatStderr(`\n[${formatPerfClock(Date.now())} +${formatElapsed(Date.now())}]${formatAgentProgressLabel(delta.agentId)} [tool:${delta.toolName}] ${summarizeChatToolResult(delta.output, delta.isError === true)}${delta.isError ? " (error)" : ""}\n`);
|
|
454
|
-
}
|
|
455
|
-
return;
|
|
456
|
-
}
|
|
457
|
-
if (delta.type === "progress.commentary") {
|
|
458
|
-
entryAgentId ??= delta.agentId;
|
|
459
|
-
latestAgentId = delta.agentId || latestAgentId;
|
|
460
|
-
if (wroteContent || wroteRenderableBlocks) {
|
|
461
|
-
return;
|
|
462
|
-
}
|
|
463
|
-
const todoItems = !input.requestEvents ? parseTerminalTodoPanel(delta.text) : null;
|
|
464
|
-
if (todoItems) {
|
|
465
|
-
drawTerminalTodoPanel(todoItems, delta.agentId);
|
|
466
|
-
return;
|
|
467
|
-
}
|
|
468
|
-
emitProgressCommentary(delta.text, delta.agentId);
|
|
469
|
-
}
|
|
470
|
-
},
|
|
471
|
-
});
|
|
472
|
-
clearIdleProgressTimer();
|
|
473
|
-
if (!result) {
|
|
474
|
-
throw new Error("chat request completed without a terminal result");
|
|
475
|
-
}
|
|
476
|
-
latestSessionId = result.sessionId;
|
|
477
|
-
latestRequestId = result.requestId;
|
|
478
|
-
latestAgentId = result.agentId ?? latestAgentId;
|
|
479
|
-
const completedAt = Date.now();
|
|
480
|
-
if (input.requestEvents && latestSnapshot) {
|
|
481
|
-
const terminalSnapshot = buildTerminalRequestSnapshot(latestSnapshot, { state: result.state, output: result.output });
|
|
482
|
-
lastRenderedRequestTree = renderRequestSnapshotTree(terminalSnapshot, input.colorRequestTree === true, buildTimingSummary(completedAt));
|
|
483
|
-
if (input.liveRequestTree && !requestTreePersisted) {
|
|
484
|
-
clearLiveRequestTree();
|
|
485
|
-
drawLiveRequestTree();
|
|
486
|
-
}
|
|
487
|
-
else if (input.liveRequestTree && requestTreePersisted) {
|
|
488
|
-
const todoContinuation = renderRequestTodoContinuation(terminalSnapshot, persistedRequestTreeTodoSignature, input.colorRequestTree === true);
|
|
489
|
-
const continuation = renderRequestEventContinuation(terminalSnapshot, persistedRequestTreeEventCount, input.colorRequestTree === true);
|
|
490
|
-
if (todoContinuation) {
|
|
491
|
-
stderrWriteChain = enqueueChatWrite(input.stderr, input.stderrStream, stderrWriteChain, `\n${todoContinuation}`);
|
|
492
|
-
}
|
|
493
|
-
if (continuation) {
|
|
494
|
-
stderrWriteChain = enqueueChatWrite(input.stderr, input.stderrStream, stderrWriteChain, `\n${continuation}`);
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
else if (!input.liveRequestTree) {
|
|
498
|
-
input.stderr(`\n${lastRenderedRequestTree}`);
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
if (!wroteContent && !wroteRenderableBlocks && result.output.trim().length > 0) {
|
|
502
|
-
writeAssistantOutput(result.output);
|
|
503
|
-
}
|
|
504
|
-
if (result.state === "waiting_for_approval") {
|
|
505
|
-
writeChatStderr(`\nRequest is waiting for approval${result.approvalId ? ` (${result.approvalId})` : ""}.\n`);
|
|
506
|
-
}
|
|
507
|
-
else if (wroteContent || wroteRenderableBlocks || result.output.trim().length > 0) {
|
|
508
|
-
writeChatStdout("\n");
|
|
509
|
-
}
|
|
510
|
-
await Promise.allSettled([stdoutWriteChain, stderrWriteChain]);
|
|
511
|
-
return { sessionId: latestSessionId, requestId: latestRequestId, agentId: entryAgentId ?? latestAgentId };
|
|
512
|
-
}
|
|
1
|
+
import{computeIncrementalOutput as he}from"../runtime/parsing/stream-event-parsing.js";import{countRenderedLines as le,renderChatRequestRunning as ye,renderChatTextChunk as Pe,summarizeChatToolResult as De}from"./chat-rendering.js";import{buildTerminalRequestSnapshot as xe,buildTodoContinuationSignature as Ce,flattenRequestExecutionSteps as Ee,renderRequestEventContinuation as be,renderRequestSnapshotTree as ie,renderRequestTodoContinuation as Ae}from"./request-tree.js";async function Oe(t){const B=Date.now(),W=t.idleProgressMs??3e4;let M,p,T,v=t.sessionId,I,o=t.agentId,q=t.agentId,y=!1,P=!1,z="",J,m="",D=0,N=0,V=!1,x=!1,A=!1,L=!1,_=0,K=[],U=0,G="",H=new Map;const j=new Set,ue=75;let C=!1,Q,k,X=B,E=Promise.resolve(),a=Promise.resolve();const c=(e,r,s,n)=>s.then(async()=>{e(n)}),Y=e=>e.map(r=>[r.id??"",r.kind,r.name,r.status,r.startedAt??"",r.endedAt??""].join("|")).join(";"),de=e=>{const r=e.plan.items.map(n=>[n.id??"",n.content,n.status,n.ownerAgentId??"",n.startedAt??"",n.endedAt??"",n.result===void 0?"":String(n.result),Y(n.events)].join("|")).join(";"),s=e.approval?[e.approval.approvalId,e.approval.status,e.approval.toolName??""].join("|"):"";return[e.sessionId,e.requestId,e.state,e.agentId??"",e.plan.version,e.plan.updatedAt,e.plan.summary.total,e.plan.summary.inProgress,e.plan.summary.completed,e.plan.summary.failed,s,Y(e.events),r].join("||")},$=e=>new Date(e).toLocaleTimeString("en-US",{hour12:!1,hour:"2-digit",minute:"2-digit",second:"2-digit",fractionalSecondDigits:3}),g=e=>`${((e-B)/1e3).toFixed(3)}s`,Z=e=>{const r=[`start ${$(B)}`];return M&&r.push(`first event +${g(M)}`),p&&r.push(`first data +${g(p)}`),e?r.push(`done +${g(e)}`):r.push(`live +${g(Date.now())}`),r.join(" \xB7 ")},ee=()=>{if(!m)return"";const e=K.length>0?`
|
|
2
|
+
${K.join("")}`:"";return`${m}${e}`},w=()=>{!t.requestEvents||!t.liveRequestTree||!x||D<=0||(a=c(t.stderr,t.stderrStream,a,`\x1B[${D}F\x1B[0J`),x=!1)},b=()=>{if(!t.requestEvents||!t.liveRequestTree||!m)return;const e=ee();a=c(t.stderr,t.stderrStream,a,e),D=le(e),x=!0},ce=()=>{if(!t.requestEvents||!t.liveRequestTree||A||!m)return;x&&D>0&&(a=c(t.stderr,t.stderrStream,a,`\x1B[${D}F\x1B[0J`),x=!1);const e=ee(),r=e.endsWith(`
|
|
3
|
+
`)?e:`${e}
|
|
4
|
+
`;a=c(t.stderr,t.stderrStream,a,r),U=T?Ee(T).length:0,G=T?Ce(T):"",A=!0},te=e=>{const r=Promise.allSettled([E,a]).then(()=>{});E=c(t.stdout,t.stdoutStream,r,e)},re=e=>{if(L=!1,t.requestEvents&&t.liveRequestTree&&!C){w(),te(e),b();return}if(t.requestEvents&&t.liveRequestTree){te(e);return}E=c(t.stdout,t.stdoutStream,E,e)},S=e=>{if(L=!1,t.requestEvents&&t.liveRequestTree&&!C){w(),a=c(t.stderr,t.stderrStream,a,e),b();return}a=c(t.stderr,t.stderrStream,a,e)},fe=e=>{const r=e.split(`
|
|
5
|
+
`);if(r[0]?.trim()!=="TODO")return null;const s=r.slice(1).map(n=>{const f=n.match(/^\s*\[([ x~!-])\]\s+(.+?)\s*$/);if(!f)return null;const l=f[1],i=l==="x"?"completed":l==="~"?"in_progress":l==="!"?"failed":l==="-"?"cancelled":"pending";return{marker:l===" "?" ":l,content:f[2],status:i}}).filter(n=>n!==null);return s.length>0?s:null},ge=(e,r)=>{const s=Date.now(),n=e.filter(u=>u.status==="completed").length,f=e.filter(u=>u.status==="in_progress").length,l=e.filter(u=>u.status==="failed").length,i=e.filter(u=>u.status==="cancelled").length,R=e.filter(u=>u.status==="pending").length,O=[`${n}/${e.length} done`,`${f} active`,`${R} pending`,l>0?`${l} failed`:null,i>0?`${i} cancelled`:null].filter(u=>u!==null).join(" | "),we=`[${$(s)} +${g(s)}]${h(r||o)} TODO Burn Down | ${O}`,Se=e.map(u=>` [${u.marker}] ${u.content}`);return`${we}
|
|
6
|
+
${Se.join(`
|
|
7
|
+
`)}
|
|
8
|
+
`},me=(e,r)=>{const s=ge(e,r),n=L&&_>0?`\x1B[${_}F\x1B[0J`:"";a=c(t.stderr,t.stderrStream,a,`${n}${s}`),_=le(s),L=!0},qe=(e,r)=>{const s=e.split(`
|
|
9
|
+
`).filter(i=>i.length>0);if(s.length===0)return;const n=Date.now(),f=`[${$(n)} +${g(n)}]${h(r||o)}`,l=s.map(i=>{const R=`${r||o}|${i}`;return j.has(R)?null:(j.add(R),`${f} ${i}
|
|
10
|
+
`)}).filter(i=>i!==null);if(l.length!==0){if(t.requestEvents&&t.liveRequestTree&&!C&&m){K.push(...l),w(),b();return}S(l.join(""))}},ne=()=>{k&&(clearTimeout(k),k=void 0)},se=()=>{ne(),!(W<=0)&&(k=setTimeout(()=>{const e=Math.max(1,Math.round((Date.now()-X)/1e3));S(`[${$(Date.now())} +${g(Date.now())}]${h(o)} Still working; no runtime update for ${e}s.
|
|
11
|
+
`),se()},W))},oe=()=>{X=Date.now(),se()},ae=()=>{!t.requestEvents||!t.liveRequestTree||(ce(),C=!0,w())},F=e=>{const r=he(z,e);return z=r.accumulated,r.delta?(t.requestEvents&&ae(),re(Pe(r.delta,t.modelInfo)),y=!0,!0):!1},h=e=>e&&e.trim().length>0?` agent:${e}`:"",Te=e=>e.key||e.id||`content:${e.content.trim().toLowerCase()}`,$e=e=>e==="completed"?"completed":e==="failed"?"failed":e==="cancelled"?"cancelled":null,Re=e=>{const r=new Map,s=[];for(const n of e.plan.items){const f=Te(n);r.set(f,n.status);const l=$e(n.status);if(!l)continue;const i=H.get(f);if(!i||i===n.status)continue;const R=`TODO ${l}: ${n.content}.`,O=`${e.agentId||o||""}|${R}`;j.has(O)||(j.add(O),s.push(`[${$(Date.now())} +${g(Date.now())}]${h(e.agentId)} ${R}
|
|
12
|
+
`))}H=r,s.length!==0&&S(s.join(""))},ve=(e,r)=>{q??=r,o=r||o;const s=e.map(n=>typeof n=="string"?n:n&&typeof n=="object"&&"text"in n&&typeof n.text=="string"?n.text:"").filter(n=>n.trim().length>0).join("");s&&F(s)&&(P=!0)},d=await t.client.request({...t.agentId?{agentId:t.agentId}:{},...t.sessionId?{sessionId:t.sessionId}:{},input:t.message,eventListener(e){if(oe(),v=e.sessionId||v,I=e.requestId||I,q??=e.agentId,o=e.agentId||o,T=e,M??=Date.now(),t.requestEvents||Re(e),t.requestEvents&&!C){const r=Date.now(),s=de(e),n=e.state!=="queued"&&e.state!=="claimed"&&e.state!=="running";!(e.output.trim().length>0)&&s!==J&&s!==Q&&(n&&!t.liveRequestTree||!t.liveRequestTree||r-N>=ue)&&(m=ie(e,t.colorRequestTree===!0,Z()),t.liveRequestTree?(w(),b()):t.stderr(`
|
|
13
|
+
${m}`),J=s,!e.activeEventId&&e.state!=="running"&&(Q=s),N=r)}(t.showRunningState??!0)&&!t.requestEvents&&!V&&!y&&!P&&(t.stderr(ye({sessionId:e.sessionId,requestId:e.requestId,agentId:e.agentId})),V=!0)},dataListener(e){if(oe(),v=e.sessionId||v,I=e.requestId||I,p??=Date.now(),e.type==="output.text.delta"){q??=e.agentId,o=e.agentId||o,F(e.text);return}if(e.type==="output.content-blocks"){q??=e.agentId,ae(),ve(e.contentBlocks,e.agentId);return}if(e.type==="plan.step"){q??=e.agentId,o=e.agentId||o;const r=e.item,s=typeof r?.status=="string"?r.status:"unknown",n=typeof r?.content=="string"?r.content:"";(t.showToolResults??!0)&&!t.requestEvents&&n.trim().length>0&&S(`
|
|
14
|
+
[${$(Date.now())} +${g(Date.now())}]${h(e.agentId)} [todo:${s}] ${n}
|
|
15
|
+
`);return}if(e.type==="tool.result"){q??=e.agentId,o=e.agentId||o,(t.showToolResults??!0)&&!t.requestEvents&&S(`
|
|
16
|
+
[${$(Date.now())} +${g(Date.now())}]${h(e.agentId)} [tool:${e.toolName}] ${De(e.output,e.isError===!0)}${e.isError?" (error)":""}
|
|
17
|
+
`);return}if(e.type==="progress.commentary"){if(q??=e.agentId,o=e.agentId||o,y||P)return;const r=t.requestEvents?null:fe(e.text);if(r){me(r,e.agentId);return}qe(e.text,e.agentId)}}});if(ne(),!d)throw new Error("chat request completed without a terminal result");v=d.sessionId,I=d.requestId,o=d.agentId??o;const Ie=Date.now();if(t.requestEvents&&T){const e=xe(T,{state:d.state,output:d.output});if(m=ie(e,t.colorRequestTree===!0,Z(Ie)),t.liveRequestTree&&!A)w(),b();else if(t.liveRequestTree&&A){const r=Ae(e,G,t.colorRequestTree===!0),s=be(e,U,t.colorRequestTree===!0);r&&(a=c(t.stderr,t.stderrStream,a,`
|
|
18
|
+
${r}`)),s&&(a=c(t.stderr,t.stderrStream,a,`
|
|
19
|
+
${s}`))}else t.liveRequestTree||t.stderr(`
|
|
20
|
+
${m}`)}return!y&&!P&&d.output.trim().length>0&&F(d.output),d.state==="waiting_for_approval"?S(`
|
|
21
|
+
Request is waiting for approval${d.approvalId?` (${d.approvalId})`:""}.
|
|
22
|
+
`):(y||P||d.output.trim().length>0)&&re(`
|
|
23
|
+
`),await Promise.allSettled([E,a]),{sessionId:v,requestId:I,agentId:q??o}}export{Oe as streamChatMessage};
|