@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.
Files changed (227) hide show
  1. package/README.md +3 -1234
  2. package/README.zh.md +3 -1191
  3. package/dist/acp.js +1 -1
  4. package/dist/api.js +1 -404
  5. package/dist/benchmark/checkpoint-resume-cost-benchmark.js +1 -55
  6. package/dist/benchmark/deepagent-local-model-benchmark.js +2 -35
  7. package/dist/benchmark/upstream-runtime-ab-benchmark.js +1 -179
  8. package/dist/cli/chat-interactive.js +25 -244
  9. package/dist/cli/chat-rendering.js +6 -100
  10. package/dist/cli/chat-stream.js +23 -512
  11. package/dist/cli/chat-ui.js +21 -199
  12. package/dist/cli/chat-workspace.js +2 -210
  13. package/dist/cli/main.js +21 -428
  14. package/dist/cli/managed-service-commands.js +9 -63
  15. package/dist/cli/managed-service.js +2 -137
  16. package/dist/cli/options-init-chat.js +1 -108
  17. package/dist/cli/options-runtime.js +1 -158
  18. package/dist/cli/options-serve.js +1 -282
  19. package/dist/cli/options.js +2 -19
  20. package/dist/cli/process-guards.js +1 -139
  21. package/dist/cli/request-tree.js +7 -296
  22. package/dist/cli/runtime-commands.js +12 -258
  23. package/dist/cli/runtime-output.js +16 -155
  24. package/dist/cli/server-commands.js +16 -270
  25. package/dist/cli/workspace.js +1 -67
  26. package/dist/cli.js +1 -7
  27. package/dist/client/acp.js +1 -1
  28. package/dist/client/in-process.js +1 -67
  29. package/dist/client/index.js +1 -2
  30. package/dist/client/types.js +0 -1
  31. package/dist/client.js +1 -1
  32. package/dist/contracts/core.js +1 -1
  33. package/dist/contracts/runtime-evaluation.js +0 -1
  34. package/dist/contracts/runtime-memory.js +0 -1
  35. package/dist/contracts/runtime-observability.js +0 -1
  36. package/dist/contracts/runtime-requests.js +0 -1
  37. package/dist/contracts/runtime-scheduling.js +0 -1
  38. package/dist/contracts/runtime.js +1 -27
  39. package/dist/contracts/types.js +1 -3
  40. package/dist/contracts/workspace.js +0 -1
  41. package/dist/flow/build-flow-graph.js +1 -50
  42. package/dist/flow/export-mermaid.js +2 -464
  43. package/dist/flow/export-sequence-mermaid.js +2 -325
  44. package/dist/flow/flow-graph-normalization.js +1 -214
  45. package/dist/flow/flow-graph-runtime.js +1 -107
  46. package/dist/flow/flow-graph-upstream.js +1 -494
  47. package/dist/flow/index.js +1 -3
  48. package/dist/flow/types.js +0 -1
  49. package/dist/index.js +1 -5
  50. package/dist/init-project.js +1 -1
  51. package/dist/knowledge/config.js +1 -32
  52. package/dist/knowledge/contracts.js +0 -1
  53. package/dist/knowledge/index.js +1 -2
  54. package/dist/knowledge/module.js +12 -909
  55. package/dist/knowledge/procedural/config.js +1 -125
  56. package/dist/knowledge/procedural/index.js +1 -2
  57. package/dist/knowledge/procedural/manager.js +9 -345
  58. package/dist/mcp.js +1 -2
  59. package/dist/package-version.d.ts +1 -1
  60. package/dist/package-version.js +1 -2
  61. package/dist/persistence/file-store.js +3 -758
  62. package/dist/persistence/sqlite-request-context-store.js +5 -54
  63. package/dist/persistence/sqlite-request-queue-store.js +10 -108
  64. package/dist/persistence/sqlite-runtime.js +1 -86
  65. package/dist/persistence/sqlite-store.js +62 -810
  66. package/dist/persistence/types.js +0 -1
  67. package/dist/projections/presentation.js +37 -206
  68. package/dist/projections/request-events.js +2 -502
  69. package/dist/projections/upstream-events.js +1 -201
  70. package/dist/protocol/a2a/http-discovery.js +1 -178
  71. package/dist/protocol/a2a/http-rpc.js +6 -622
  72. package/dist/protocol/a2a/http.js +1 -138
  73. package/dist/protocol/a2a/task-state.js +3 -317
  74. package/dist/protocol/acp/client.js +8 -294
  75. package/dist/protocol/acp/harness-client.js +1 -218
  76. package/dist/protocol/acp/http.js +5 -130
  77. package/dist/protocol/acp/server.js +1 -310
  78. package/dist/protocol/acp/stdio.js +2 -69
  79. package/dist/protocol/ag-ui/http.js +3 -378
  80. package/dist/protocol/mcp/server.js +1 -428
  81. package/dist/resource/backend/workspace-scoped-backend.js +1 -319
  82. package/dist/resource/isolation.js +1 -237
  83. package/dist/resource/mcp/tool-support.js +3 -296
  84. package/dist/resource/mcp-tool-support.js +1 -2
  85. package/dist/resource/providers/resource-provider.js +1 -215
  86. package/dist/resource/resource-impl.js +1 -3
  87. package/dist/resource/resource-types.js +0 -1
  88. package/dist/resource/resource.js +1 -1
  89. package/dist/resource/sources.js +1 -247
  90. package/dist/resource/tools/function-tool-resolver.js +2 -272
  91. package/dist/runtime/adapter/compat/deepagent-compat.js +1 -29
  92. package/dist/runtime/adapter/compat/openai-compatible.js +1 -55
  93. package/dist/runtime/adapter/direct-builtin-utility.js +2 -90
  94. package/dist/runtime/adapter/flow/execution-context.js +1 -71
  95. package/dist/runtime/adapter/flow/invocation-flow.js +8 -425
  96. package/dist/runtime/adapter/flow/invoke-runtime.js +1 -20
  97. package/dist/runtime/adapter/flow/stream-runtime.js +11 -1395
  98. package/dist/runtime/adapter/invocation-result.js +2 -473
  99. package/dist/runtime/adapter/local-tool-invocation.js +6 -638
  100. package/dist/runtime/adapter/middleware/context-hygiene.js +1 -83
  101. package/dist/runtime/adapter/middleware-assembly.js +5 -477
  102. package/dist/runtime/adapter/model/invocation-request.js +3 -183
  103. package/dist/runtime/adapter/model/message-assembly.js +1 -28
  104. package/dist/runtime/adapter/model/model-providers.js +23 -1115
  105. package/dist/runtime/adapter/model/prompted-json-tool-call-capture.js +1 -40
  106. package/dist/runtime/adapter/model/prompted-json-tool-policy.js +1 -22
  107. package/dist/runtime/adapter/resilience.js +1 -104
  108. package/dist/runtime/adapter/runtime-adapter-support.js +3 -141
  109. package/dist/runtime/adapter/runtime-shell.js +5 -166
  110. package/dist/runtime/adapter/stream-event-projection.js +2 -622
  111. package/dist/runtime/adapter/stream-text-consumption.js +1 -18
  112. package/dist/runtime/adapter/terminal-status.js +2 -67
  113. package/dist/runtime/adapter/tool/builtin-middleware-tools.js +6 -627
  114. package/dist/runtime/adapter/tool/declared-middleware.js +1 -154
  115. package/dist/runtime/adapter/tool/interrupt-policy.js +1 -34
  116. package/dist/runtime/adapter/tool/provider-tool.js +1 -25
  117. package/dist/runtime/adapter/tool/resolved-tool.js +1 -225
  118. package/dist/runtime/adapter/tool/tool-arguments.js +3 -486
  119. package/dist/runtime/adapter/tool/tool-hitl.js +1 -346
  120. package/dist/runtime/adapter/tool/tool-name-mapping.js +1 -128
  121. package/dist/runtime/adapter/tool/tool-output-artifacts.js +2 -88
  122. package/dist/runtime/adapter/tool/tool-replay.js +1 -37
  123. package/dist/runtime/adapter/tool-resolution.js +1 -86
  124. package/dist/runtime/adapter/upstream-configurable-keys.js +1 -2
  125. package/dist/runtime/agent-runtime-adapter.js +60 -2338
  126. package/dist/runtime/agent-runtime-assembly.js +7 -249
  127. package/dist/runtime/env/runtime-env.js +1 -62
  128. package/dist/runtime/harness/background-runtime.js +1 -8
  129. package/dist/runtime/harness/bindings.js +1 -58
  130. package/dist/runtime/harness/events/event-bus.js +1 -16
  131. package/dist/runtime/harness/events/event-sink.js +1 -61
  132. package/dist/runtime/harness/events/events.js +1 -80
  133. package/dist/runtime/harness/events/listener-runtime.js +1 -13
  134. package/dist/runtime/harness/events/runtime-event-operations.js +1 -9
  135. package/dist/runtime/harness/events/streaming.js +1 -100
  136. package/dist/runtime/harness/events/timeline.js +1 -52
  137. package/dist/runtime/harness/public-shapes.js +1 -186
  138. package/dist/runtime/harness/run/artifact-paths.js +1 -15
  139. package/dist/runtime/harness/run/governance.js +1 -295
  140. package/dist/runtime/harness/run/helpers.js +1 -71
  141. package/dist/runtime/harness/run/inspection.js +1 -409
  142. package/dist/runtime/harness/run/operator-overview.js +1 -80
  143. package/dist/runtime/harness/run/queue-diagnostics.js +1 -15
  144. package/dist/runtime/harness/run/recovery.js +1 -162
  145. package/dist/runtime/harness/run/resources.js +1 -60
  146. package/dist/runtime/harness/run/resume.js +1 -56
  147. package/dist/runtime/harness/run/routing.js +1 -48
  148. package/dist/runtime/harness/run/run-lifecycle.js +1 -66
  149. package/dist/runtime/harness/run/run-operations.js +1 -217
  150. package/dist/runtime/harness/run/run-queue.js +1 -43
  151. package/dist/runtime/harness/run/run-slot-acquisition.js +1 -157
  152. package/dist/runtime/harness/run/session-records.js +1 -97
  153. package/dist/runtime/harness/run/start-run.js +1 -120
  154. package/dist/runtime/harness/run/startup-runtime.js +1 -69
  155. package/dist/runtime/harness/run/stream-run.js +8 -1418
  156. package/dist/runtime/harness/run/surface-semantics.js +1 -79
  157. package/dist/runtime/harness/runtime-defaults.js +1 -39
  158. package/dist/runtime/harness/system/boundary-analysis.js +1 -234
  159. package/dist/runtime/harness/system/health-monitor.js +1 -258
  160. package/dist/runtime/harness/system/inventory.js +1 -129
  161. package/dist/runtime/harness/system/mem0-ingestion-sync.js +5 -345
  162. package/dist/runtime/harness/system/policy-engine.js +1 -175
  163. package/dist/runtime/harness/system/runtime-memory-candidates.js +4 -110
  164. package/dist/runtime/harness/system/runtime-memory-consolidation.js +1 -51
  165. package/dist/runtime/harness/system/runtime-memory-manager.js +10 -693
  166. package/dist/runtime/harness/system/runtime-memory-policy.js +1 -155
  167. package/dist/runtime/harness/system/runtime-memory-records.js +11 -577
  168. package/dist/runtime/harness/system/runtime-memory-sync.js +5 -206
  169. package/dist/runtime/harness/system/session-memory-sync.js +3 -113
  170. package/dist/runtime/harness/system/skill-requirements.js +1 -112
  171. package/dist/runtime/harness/system/store.js +9 -365
  172. package/dist/runtime/harness/tool-gateway/index.js +1 -2
  173. package/dist/runtime/harness/tool-gateway/policy.js +1 -45
  174. package/dist/runtime/harness/tool-gateway/validation.js +1 -176
  175. package/dist/runtime/harness/tool-schema.js +1 -3
  176. package/dist/runtime/harness.js +3 -1490
  177. package/dist/runtime/index.js +1 -3
  178. package/dist/runtime/layout/runtime-layout.js +1 -31
  179. package/dist/runtime/maintenance/checkpoint-maintenance.js +2 -178
  180. package/dist/runtime/maintenance/file-checkpoint-saver.js +1 -106
  181. package/dist/runtime/maintenance/runtime-record-maintenance.js +2 -169
  182. package/dist/runtime/maintenance/sqlite-checkpoint-saver.js +4 -289
  183. package/dist/runtime/parsing/output-content.js +10 -550
  184. package/dist/runtime/parsing/output-parsing.js +1 -4
  185. package/dist/runtime/parsing/output-recovery.js +3 -213
  186. package/dist/runtime/parsing/output-tool-args.js +7 -663
  187. package/dist/runtime/parsing/stream-event-parsing.js +3 -362
  188. package/dist/runtime/prompts/runtime-prompts.js +4 -73
  189. package/dist/runtime/scheduling/system-schedule-manager.js +11 -532
  190. package/dist/runtime/skills/skill-metadata.js +1 -197
  191. package/dist/runtime/startup-tracing.js +2 -37
  192. package/dist/runtime/support/compiled-binding.js +1 -290
  193. package/dist/runtime/support/embedding-models.js +1 -118
  194. package/dist/runtime/support/harness-support.js +5 -137
  195. package/dist/runtime/support/llamaindex.js +1 -108
  196. package/dist/runtime/support/runtime-adapter-options.js +1 -29
  197. package/dist/runtime/support/runtime-factories.js +1 -51
  198. package/dist/runtime/support/vector-stores.js +9 -270
  199. package/dist/scaffold/init-project.js +54 -233
  200. package/dist/tooling/extensions.js +1 -311
  201. package/dist/tooling/module-loader.js +1 -55
  202. package/dist/tools.js +1 -176
  203. package/dist/utils/agent-display.js +1 -18
  204. package/dist/utils/bundled-text.js +4 -39
  205. package/dist/utils/compiled-binding.js +1 -33
  206. package/dist/utils/fs.js +2 -45
  207. package/dist/utils/id.js +1 -9
  208. package/dist/utils/message-content.js +1 -30
  209. package/dist/utils/object.js +1 -6
  210. package/dist/workspace/agent-binding-compiler.js +3 -613
  211. package/dist/workspace/compile.js +1 -472
  212. package/dist/workspace/framework-contract-validation.js +2 -322
  213. package/dist/workspace/index.js +1 -1
  214. package/dist/workspace/object-loader-paths.js +1 -71
  215. package/dist/workspace/object-loader-readers.js +1 -187
  216. package/dist/workspace/object-loader.js +1 -754
  217. package/dist/workspace/resource-compilers.js +1 -374
  218. package/dist/workspace/support/agent-capabilities.js +1 -37
  219. package/dist/workspace/support/agent-execution-config.js +1 -44
  220. package/dist/workspace/support/discovery.js +1 -147
  221. package/dist/workspace/support/source-collectors.js +1 -30
  222. package/dist/workspace/support/source-protocols.js +2 -192
  223. package/dist/workspace/support/workspace-ref-utils.js +1 -362
  224. package/dist/workspace/tool-hydration.js +1 -280
  225. package/dist/workspace/validate.js +1 -99
  226. package/dist/workspace/yaml-object-reader.js +1 -285
  227. package/package.json +7 -3
@@ -1,622 +1,2 @@
1
- import { extractToolFallbackContext, extractVisibleOutput, readTextContent, sanitizeVisibleText } from "../parsing/output-parsing.js";
2
- import { salvageFunctionLikeToolCall } from "../parsing/output-tool-args.js";
3
- import { computeIncrementalOutput, extractInterruptPayload, extractReasoningStreamOutput, sanitizeRetainedUpstreamEvent, extractStateStreamOutput, extractTerminalStreamOutput, extractToolResult, extractVisibleStreamOutput, normalizeTerminalOutputKey, } from "../parsing/stream-event-parsing.js";
4
- import { resolveModelFacingToolName } from "./tool/tool-name-mapping.js";
5
- export function createStreamEventProjectionState() {
6
- return {
7
- emittedOutput: "",
8
- emittedToolError: false,
9
- emittedToolResult: false,
10
- emittedSuccessfulToolResult: false,
11
- emittedSuccessfulTaskResult: false,
12
- emittedPlaceholderTaskResult: false,
13
- hasFailedTaskDelegation: false,
14
- emittedNonTodoToolResult: false,
15
- emittedSuccessfulNonTodoToolResult: false,
16
- sawDelegatedAgentWithConfiguredTools: false,
17
- emittedDelegatedExecutionToolResult: false,
18
- emittedSuccessfulDelegatedExecutionToolResult: false,
19
- emittedDelegatedTerminalOutput: false,
20
- sawPlanState: false,
21
- hasIncompletePlanState: false,
22
- hasFailedPlanState: false,
23
- openTaskDelegations: 0,
24
- openToolCapableTaskDelegations: 0,
25
- taskDelegationHasToolsStack: [],
26
- taskDelegationFindingsStack: [],
27
- lastCompletedTaskDelegationFindings: "",
28
- seenTerminalOutputs: new Set(),
29
- };
30
- }
31
- function shouldSuppressVisibleToolCallText(value) {
32
- const trimmed = value.trim();
33
- if (!trimmed) {
34
- return false;
35
- }
36
- if (/^(?:model_request|tool_call|call_tool)\b/iu.test(trimmed)) {
37
- return true;
38
- }
39
- if (/^(?:name|tool_call_id)\s*=/iu.test(trimmed)) {
40
- return true;
41
- }
42
- if (/^(?:we\s+need\s+to|so\s+next\s+step\b)/iu.test(trimmed)) {
43
- return true;
44
- }
45
- if (/\b(?:must|need|needs|should|will)\s+(?:now\s+)?(?:call|use|run|produce)\s+[A-Za-z_][A-Za-z0-9_]*\b/iu.test(trimmed)) {
46
- return true;
47
- }
48
- if (/^\{\s*"(?:name|arguments|args|argv|todos|symbol|query|market|count|stdout|stderr|exitCode)"\s*:/iu.test(trimmed)) {
49
- return true;
50
- }
51
- try {
52
- const parsed = JSON.parse(trimmed);
53
- if (typeof parsed === "object"
54
- && parsed !== null
55
- && ("symbol" in parsed
56
- || "todos" in parsed
57
- || "query" in parsed
58
- || "market" in parsed
59
- || "count" in parsed
60
- || "arguments" in parsed
61
- || "args" in parsed
62
- || "argv" in parsed
63
- || "name" in parsed
64
- || "stdout" in parsed
65
- || "stderr" in parsed
66
- || "exitCode" in parsed)) {
67
- return true;
68
- }
69
- }
70
- catch {
71
- // Not JSON; continue with function-like tool call suppression.
72
- }
73
- if (salvageFunctionLikeToolCall(trimmed)) {
74
- return true;
75
- }
76
- const prefixedToolCallMatch = /^(?:\s*(?:Ready|Understood|Okay|Ok|Got it|Sure|All set)[.:!]?\s*)+([A-Za-z_][A-Za-z0-9_]*\([\s\S]*\))\s*$/u.exec(trimmed);
77
- if (!prefixedToolCallMatch) {
78
- return false;
79
- }
80
- return salvageFunctionLikeToolCall(prefixedToolCallMatch[1]) !== null;
81
- }
82
- function stripVisibleToolCallResidue(value) {
83
- const lines = value.split(/\r?\n/);
84
- let changed = false;
85
- const kept = lines.filter((line) => {
86
- if (shouldSuppressVisibleToolCallText(line)) {
87
- changed = true;
88
- return false;
89
- }
90
- return true;
91
- });
92
- if (changed) {
93
- return kept.join("\n").trim();
94
- }
95
- const trimmedStart = value.trimStart();
96
- if (!trimmedStart.startsWith("{")) {
97
- return value;
98
- }
99
- const parsedPrefix = extractLeadingJsonObject(trimmedStart);
100
- if (!parsedPrefix || !shouldSuppressVisibleToolCallText(parsedPrefix.json)) {
101
- return value;
102
- }
103
- return parsedPrefix.rest.trimStart();
104
- }
105
- function extractLeadingJsonObject(value) {
106
- let depth = 0;
107
- let inString = false;
108
- let escaped = false;
109
- for (let index = 0; index < value.length; index += 1) {
110
- const char = value[index];
111
- if (escaped) {
112
- escaped = false;
113
- continue;
114
- }
115
- if (char === "\\") {
116
- escaped = inString;
117
- continue;
118
- }
119
- if (char === "\"") {
120
- inString = !inString;
121
- continue;
122
- }
123
- if (inString) {
124
- continue;
125
- }
126
- if (char === "{") {
127
- depth += 1;
128
- continue;
129
- }
130
- if (char === "}") {
131
- depth -= 1;
132
- if (depth === 0) {
133
- return { json: value.slice(0, index + 1), rest: value.slice(index + 1) };
134
- }
135
- }
136
- }
137
- return null;
138
- }
139
- function readSummaryCounts(summary) {
140
- if (typeof summary !== "object" || summary === null) {
141
- return null;
142
- }
143
- const typed = summary;
144
- if (typeof typed.total === "number" && typed.total <= 0) {
145
- return null;
146
- }
147
- const hasAnyCountField = typeof typed.pending === "number"
148
- || typeof typed.inProgress === "number";
149
- if (!hasAnyCountField) {
150
- return null;
151
- }
152
- return {
153
- pending: typeof typed.pending === "number" ? typed.pending : 0,
154
- inProgress: typeof typed.inProgress === "number" ? typed.inProgress : 0,
155
- };
156
- }
157
- function readSummaryFailureCount(summary) {
158
- if (typeof summary !== "object" || summary === null) {
159
- return null;
160
- }
161
- const typed = summary;
162
- if (typeof typed.total === "number" && typed.total <= 0) {
163
- return null;
164
- }
165
- return typeof typed.failed === "number" ? typed.failed : null;
166
- }
167
- function hasIncompleteTodosArray(value) {
168
- if (!Array.isArray(value)) {
169
- return null;
170
- }
171
- if (value.length === 0) {
172
- return null;
173
- }
174
- return value.some((item) => {
175
- if (typeof item !== "object" || item === null) {
176
- return false;
177
- }
178
- const status = item.status;
179
- return status === "pending" || status === "in_progress";
180
- });
181
- }
182
- function hasFailedTodosArray(value) {
183
- if (!Array.isArray(value)) {
184
- return null;
185
- }
186
- if (value.length === 0) {
187
- return null;
188
- }
189
- return value.some((item) => {
190
- if (typeof item !== "object" || item === null) {
191
- return false;
192
- }
193
- return item.status === "failed";
194
- });
195
- }
196
- function getPlanStateCompleteness(value) {
197
- if (typeof value !== "object" || value === null) {
198
- return null;
199
- }
200
- const typed = value;
201
- const summaryCounts = readSummaryCounts(typed.summary);
202
- if (summaryCounts) {
203
- return summaryCounts.pending > 0 || summaryCounts.inProgress > 0;
204
- }
205
- if (typeof typed.summary === "object" && typed.summary !== null) {
206
- const nestedSummary = getPlanStateCompleteness(typed.summary);
207
- if (nestedSummary !== null) {
208
- return nestedSummary;
209
- }
210
- }
211
- const directTodos = hasIncompleteTodosArray(typed.todos);
212
- if (directTodos !== null) {
213
- return directTodos;
214
- }
215
- if (typeof typed.update === "object" && typed.update !== null) {
216
- const nestedTodos = hasIncompleteTodosArray(typed.update.todos);
217
- if (nestedTodos !== null) {
218
- return nestedTodos;
219
- }
220
- }
221
- if (typeof typed.output === "object" && typed.output !== null) {
222
- const nestedOutput = getPlanStateCompleteness(typed.output);
223
- if (nestedOutput !== null) {
224
- return nestedOutput;
225
- }
226
- }
227
- if (typeof typed.data === "object" && typed.data !== null) {
228
- const nestedData = getPlanStateCompleteness(typed.data);
229
- if (nestedData !== null) {
230
- return nestedData;
231
- }
232
- }
233
- return null;
234
- }
235
- function getPlanStateFailure(value) {
236
- if (typeof value !== "object" || value === null) {
237
- return null;
238
- }
239
- const typed = value;
240
- const summaryFailed = readSummaryFailureCount(typed.summary);
241
- if (summaryFailed !== null) {
242
- return summaryFailed > 0;
243
- }
244
- if (typeof typed.summary === "object" && typed.summary !== null) {
245
- const nestedSummary = getPlanStateFailure(typed.summary);
246
- if (nestedSummary !== null) {
247
- return nestedSummary;
248
- }
249
- }
250
- const directTodos = hasFailedTodosArray(typed.todos);
251
- if (directTodos !== null) {
252
- return directTodos;
253
- }
254
- if (typeof typed.update === "object" && typed.update !== null) {
255
- const nestedTodos = hasFailedTodosArray(typed.update.todos);
256
- if (nestedTodos !== null) {
257
- return nestedTodos;
258
- }
259
- }
260
- if (typeof typed.output === "object" && typed.output !== null) {
261
- const nestedOutput = getPlanStateFailure(typed.output);
262
- if (nestedOutput !== null) {
263
- return nestedOutput;
264
- }
265
- }
266
- if (typeof typed.data === "object" && typed.data !== null) {
267
- const nestedData = getPlanStateFailure(typed.data);
268
- if (nestedData !== null) {
269
- return nestedData;
270
- }
271
- }
272
- return null;
273
- }
274
- function parseMaybeJsonString(value) {
275
- const trimmed = value.trim();
276
- if (!trimmed || (!trimmed.startsWith("{") && !trimmed.startsWith("["))) {
277
- return value;
278
- }
279
- try {
280
- return JSON.parse(trimmed);
281
- }
282
- catch {
283
- return value;
284
- }
285
- }
286
- function unwrapPossibleToolInput(value) {
287
- if (typeof value === "string") {
288
- return parseMaybeJsonString(value);
289
- }
290
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
291
- return value;
292
- }
293
- const typed = value;
294
- if (typed.args !== undefined) {
295
- return unwrapPossibleToolInput(typed.args);
296
- }
297
- if (typed.input !== undefined) {
298
- return unwrapPossibleToolInput(typed.input);
299
- }
300
- return value;
301
- }
302
- function readTaskSubagentType(value) {
303
- const unwrapped = unwrapPossibleToolInput(value);
304
- if (typeof unwrapped !== "object" || unwrapped === null || Array.isArray(unwrapped)) {
305
- return null;
306
- }
307
- const typed = unwrapped;
308
- return typeof typed.subagent_type === "string" && typed.subagent_type.trim().length > 0
309
- ? typed.subagent_type.trim()
310
- : null;
311
- }
312
- function isUpstreamPlaceholderTaskResult(value) {
313
- if (typeof value === "string") {
314
- return value.trim() === "Task completed";
315
- }
316
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
317
- return false;
318
- }
319
- const typed = value;
320
- const messages = Array.isArray(typed.update?.messages) ? typed.update.messages : [];
321
- if (messages.length !== 1) {
322
- return false;
323
- }
324
- const message = messages[0];
325
- return typed.lg_name === "Command"
326
- && typed.lc_direct_tool_output === true
327
- && message?.name === "task"
328
- && message?.type === "tool"
329
- && typeof message?.tool_call_id === "string"
330
- && message?.content === "Task completed";
331
- }
332
- function extractDelegatedFindingsCandidateText(value, depth = 0) {
333
- if (depth > 6 || value === null || value === undefined) {
334
- return "";
335
- }
336
- if (typeof value === "string") {
337
- return value;
338
- }
339
- const visibleOutput = extractVisibleOutput(value);
340
- if (visibleOutput) {
341
- return visibleOutput;
342
- }
343
- const toolFallback = extractToolFallbackContext(value);
344
- if (toolFallback) {
345
- return toolFallback;
346
- }
347
- const directText = readTextContent(value);
348
- if (directText) {
349
- return directText;
350
- }
351
- if (Array.isArray(value)) {
352
- for (let index = value.length - 1; index >= 0; index -= 1) {
353
- const nested = extractDelegatedFindingsCandidateText(value[index], depth + 1);
354
- if (nested) {
355
- return nested;
356
- }
357
- }
358
- return "";
359
- }
360
- if (typeof value === "object") {
361
- const typed = value;
362
- for (const key of ["messages", "update", "output", "content", "data", "summary"]) {
363
- const nested = extractDelegatedFindingsCandidateText(typed[key], depth + 1);
364
- if (nested) {
365
- return nested;
366
- }
367
- }
368
- }
369
- return "";
370
- }
371
- function normalizeDelegatedFindingsText(value) {
372
- const directText = extractDelegatedFindingsCandidateText(value);
373
- if (!directText) {
374
- return "";
375
- }
376
- const normalized = sanitizeVisibleText(directText).trim();
377
- if (normalized === "Task completed") {
378
- return "";
379
- }
380
- if (/Status:\s*completed[\s\S]*Summary:\s*-\s*none[\s\S]*Likely Causes:\s*-\s*none[\s\S]*Blockers:\s*-\s*none[\s\S]*Next Commands:\s*-\s*none/i.test(normalized)) {
381
- return "";
382
- }
383
- return normalized;
384
- }
385
- function isInternalRuntimeSpillPathErrorValue(value) {
386
- const directText = typeof value === "string"
387
- ? value
388
- : extractToolFallbackContext(value) || extractVisibleOutput(value) || readTextContent(value);
389
- if (directText && /\/large_tool_results\/|internal runtime spill path/i.test(directText)) {
390
- return true;
391
- }
392
- if (typeof value === "object" && value !== null) {
393
- try {
394
- return /\/large_tool_results\/|internal runtime spill path/i.test(JSON.stringify(value));
395
- }
396
- catch {
397
- return false;
398
- }
399
- }
400
- return false;
401
- }
402
- function recordDelegatedFindings(state, value, source = "tool") {
403
- if (state.taskDelegationFindingsStack.length === 0) {
404
- return false;
405
- }
406
- const normalized = normalizeDelegatedFindingsText(value);
407
- if (!normalized) {
408
- return false;
409
- }
410
- const current = state.taskDelegationFindingsStack[state.taskDelegationFindingsStack.length - 1] ?? "";
411
- if (source === "terminal" && current) {
412
- return true;
413
- }
414
- state.taskDelegationFindingsStack[state.taskDelegationFindingsStack.length - 1] = normalized;
415
- return true;
416
- }
417
- function updateDelegationState(state, event, countConfiguredToolsForAgentId) {
418
- if (typeof event !== "object" || event === null) {
419
- return;
420
- }
421
- const typed = event;
422
- const eventName = typeof typed.event === "string" ? typed.event : "";
423
- const toolName = typeof typed.name === "string" ? typed.name : "";
424
- const runType = typeof typed.run_type === "string" ? typed.run_type : "";
425
- const isTaskStart = toolName === "task" && (eventName === "on_tool_start" || (eventName === "on_chain_start" && runType === "tool"));
426
- const isTaskEnd = toolName === "task" && (eventName === "on_tool_end" || (eventName === "on_chain_end" && runType === "tool"));
427
- const isTaskError = toolName === "task" && (eventName === "on_tool_error" || (eventName === "on_chain_error" && runType === "tool"));
428
- if (isTaskStart) {
429
- state.openTaskDelegations += 1;
430
- const delegatedSubagentType = readTaskSubagentType(typed.data?.input);
431
- const delegatedTaskHasTools = !!delegatedSubagentType
432
- && !!countConfiguredToolsForAgentId
433
- && countConfiguredToolsForAgentId(delegatedSubagentType) > 0;
434
- state.taskDelegationHasToolsStack.push(delegatedTaskHasTools);
435
- if (delegatedTaskHasTools) {
436
- state.sawDelegatedAgentWithConfiguredTools = true;
437
- state.openToolCapableTaskDelegations += 1;
438
- }
439
- state.taskDelegationFindingsStack.push("");
440
- return;
441
- }
442
- if (isTaskEnd || isTaskError) {
443
- state.openTaskDelegations = Math.max(0, state.openTaskDelegations - 1);
444
- const delegatedTaskHadTools = state.taskDelegationHasToolsStack.pop() === true;
445
- state.lastCompletedTaskDelegationFindings = state.taskDelegationFindingsStack.pop() ?? "";
446
- if (delegatedTaskHadTools) {
447
- state.openToolCapableTaskDelegations = Math.max(0, state.openToolCapableTaskDelegations - 1);
448
- }
449
- if (isTaskError) {
450
- state.hasFailedTaskDelegation = true;
451
- }
452
- }
453
- }
454
- function normalizePlanToolName(toolName) {
455
- return typeof toolName === "string" ? toolName.trim().toLowerCase().replace(/[\s-]+/gu, "_") : "";
456
- }
457
- function isPlanToolName(toolName) {
458
- const normalized = normalizePlanToolName(toolName);
459
- return normalized === "write_todos"
460
- || normalized === "read_todos"
461
- || normalized === "tool_call_write_todos"
462
- || normalized === "tool_call_read_todos"
463
- || normalized === "call_write_todos"
464
- || normalized === "call_read_todos";
465
- }
466
- function extractTodoToolStart(event) {
467
- if (typeof event !== "object" || event === null) {
468
- return null;
469
- }
470
- const typed = event;
471
- const eventName = typeof typed.event === "string" ? typed.event : "";
472
- const runType = typeof typed.run_type === "string" ? typed.run_type : "";
473
- const toolName = typeof typed.name === "string" ? typed.name : "";
474
- const isToolStart = eventName === "on_tool_start" || (eventName === "on_chain_start" && runType === "tool");
475
- if (!isToolStart || !isPlanToolName(toolName)) {
476
- return null;
477
- }
478
- const input = unwrapPossibleToolInput(typed.data?.input);
479
- return { toolName, input };
480
- }
481
- export function projectRuntimeStreamEvent(params) {
482
- const { event, allowVisibleStreamDeltas, includeStateStreamOutput, rootAgentId, countConfiguredToolsForAgentId, toolNameMapping, primaryTools, state, } = params;
483
- const chunks = [{
484
- kind: "upstream-event",
485
- event: sanitizeRetainedUpstreamEvent(event),
486
- }];
487
- updateDelegationState(state, event, countConfiguredToolsForAgentId);
488
- const interruptPayload = extractInterruptPayload(event);
489
- if (interruptPayload) {
490
- chunks.push({ kind: "interrupt", content: interruptPayload });
491
- return chunks;
492
- }
493
- const reasoning = extractReasoningStreamOutput(event);
494
- if (reasoning) {
495
- chunks.push({ kind: "reasoning", content: reasoning });
496
- }
497
- const planStateCompleteness = getPlanStateCompleteness(event);
498
- if (planStateCompleteness !== null) {
499
- state.sawPlanState = true;
500
- state.hasIncompletePlanState = planStateCompleteness;
501
- }
502
- const planStateFailure = getPlanStateFailure(event);
503
- if (planStateFailure !== null) {
504
- state.sawPlanState = true;
505
- state.hasFailedPlanState = planStateFailure;
506
- }
507
- const eventAgentId = typeof event === "object" && event !== null && typeof event.agentId === "string"
508
- ? event.agentId.trim()
509
- : "";
510
- const isDelegatedAgentEvent = !!rootAgentId && !!eventAgentId && eventAgentId !== rootAgentId;
511
- if (isDelegatedAgentEvent
512
- && countConfiguredToolsForAgentId
513
- && countConfiguredToolsForAgentId(eventAgentId) > 0) {
514
- state.sawDelegatedAgentWithConfiguredTools = true;
515
- }
516
- const isRootVisibleEvent = !rootAgentId || !eventAgentId || eventAgentId === rootAgentId;
517
- const allowVisibleContent = isRootVisibleEvent && state.openTaskDelegations === 0;
518
- const allowStreamedVisibleContent = allowVisibleContent && !state.emittedToolResult && !state.emittedToolError;
519
- if (allowVisibleStreamDeltas && allowStreamedVisibleContent) {
520
- const visibleStreamOutput = stripVisibleToolCallResidue(extractVisibleStreamOutput(event));
521
- if (visibleStreamOutput && !shouldSuppressVisibleToolCallText(visibleStreamOutput)) {
522
- const nextOutput = computeIncrementalOutput(state.emittedOutput, visibleStreamOutput);
523
- state.emittedOutput = nextOutput.accumulated;
524
- if (nextOutput.delta) {
525
- chunks.push({ kind: "content", content: nextOutput.delta });
526
- }
527
- }
528
- }
529
- if (includeStateStreamOutput && allowVisibleContent) {
530
- const stateStreamOutput = stripVisibleToolCallResidue(extractStateStreamOutput(event));
531
- if (stateStreamOutput && !shouldSuppressVisibleToolCallText(stateStreamOutput)) {
532
- const nextOutput = computeIncrementalOutput(state.emittedOutput, stateStreamOutput);
533
- state.emittedOutput = nextOutput.accumulated;
534
- if (nextOutput.delta) {
535
- chunks.push({ kind: "content", content: nextOutput.delta });
536
- }
537
- }
538
- }
539
- const toolResult = extractToolResult(event);
540
- const todoToolStart = toolResult ? null : extractTodoToolStart(event);
541
- if (todoToolStart) {
542
- state.emittedToolResult = true;
543
- state.emittedSuccessfulToolResult = true;
544
- chunks.push({
545
- kind: "tool-result",
546
- toolName: resolveModelFacingToolName(todoToolStart.toolName, toolNameMapping, primaryTools),
547
- output: todoToolStart.input,
548
- isError: false,
549
- });
550
- }
551
- if (toolResult) {
552
- const isTodoTool = isPlanToolName(toolResult.toolName);
553
- const salvagedTaskErrorFindings = toolResult.toolName === "task"
554
- && toolResult.isError === true
555
- && !!state.lastCompletedTaskDelegationFindings
556
- && isInternalRuntimeSpillPathErrorValue(toolResult.output)
557
- ? state.lastCompletedTaskDelegationFindings
558
- : "";
559
- const effectiveToolOutput = salvagedTaskErrorFindings || toolResult.output;
560
- const effectiveToolIsError = salvagedTaskErrorFindings
561
- ? false
562
- : toolResult.isError === true;
563
- const isSuccessfulTaskResult = toolResult.toolName === "task" && effectiveToolIsError !== true;
564
- const isDelegatedExecutionTool = (isDelegatedAgentEvent || state.openToolCapableTaskDelegations > 0)
565
- && !isPlanToolName(toolResult.toolName)
566
- && toolResult.toolName !== "task";
567
- if (isDelegatedExecutionTool && toolResult.isError !== true) {
568
- recordDelegatedFindings(state, toolResult.output, "tool");
569
- }
570
- const delegatedTaskFindings = isSuccessfulTaskResult && state.lastCompletedTaskDelegationFindings
571
- ? state.lastCompletedTaskDelegationFindings
572
- : "";
573
- const resolvedToolOutput = delegatedTaskFindings || effectiveToolOutput;
574
- const isPlaceholderTaskResult = isSuccessfulTaskResult
575
- && !delegatedTaskFindings
576
- && isUpstreamPlaceholderTaskResult(toolResult.output);
577
- state.emittedToolResult = true;
578
- state.emittedToolError = state.emittedToolError || effectiveToolIsError === true;
579
- state.emittedSuccessfulToolResult = state.emittedSuccessfulToolResult || effectiveToolIsError !== true;
580
- state.emittedSuccessfulTaskResult = state.emittedSuccessfulTaskResult || isSuccessfulTaskResult;
581
- state.emittedPlaceholderTaskResult = state.emittedPlaceholderTaskResult || isPlaceholderTaskResult;
582
- state.emittedNonTodoToolResult = state.emittedNonTodoToolResult || !isTodoTool;
583
- state.emittedSuccessfulNonTodoToolResult = state.emittedSuccessfulNonTodoToolResult || (!isTodoTool && effectiveToolIsError !== true);
584
- state.emittedDelegatedExecutionToolResult = state.emittedDelegatedExecutionToolResult || isDelegatedExecutionTool || !!salvagedTaskErrorFindings;
585
- state.emittedSuccessfulDelegatedExecutionToolResult =
586
- state.emittedSuccessfulDelegatedExecutionToolResult || (isDelegatedExecutionTool && effectiveToolIsError !== true) || !!salvagedTaskErrorFindings;
587
- if (salvagedTaskErrorFindings) {
588
- state.hasFailedTaskDelegation = false;
589
- }
590
- chunks.push({
591
- kind: "tool-result",
592
- toolName: resolveModelFacingToolName(toolResult.toolName, toolNameMapping, primaryTools),
593
- output: resolvedToolOutput,
594
- isError: effectiveToolIsError,
595
- });
596
- if (toolResult.toolName === "task") {
597
- state.lastCompletedTaskDelegationFindings = "";
598
- }
599
- }
600
- const output = allowVisibleContent ? stripVisibleToolCallResidue(extractTerminalStreamOutput(event)) : "";
601
- if (!allowVisibleContent) {
602
- const delegatedTerminalOutput = extractTerminalStreamOutput(event);
603
- if (delegatedTerminalOutput) {
604
- state.emittedDelegatedTerminalOutput =
605
- state.emittedDelegatedTerminalOutput || recordDelegatedFindings(state, delegatedTerminalOutput, "terminal");
606
- }
607
- }
608
- if (output && !shouldSuppressVisibleToolCallText(output)) {
609
- const outputKey = normalizeTerminalOutputKey(output);
610
- if (!outputKey || !state.seenTerminalOutputs.has(outputKey)) {
611
- if (outputKey) {
612
- state.seenTerminalOutputs.add(outputKey);
613
- }
614
- const nextOutput = computeIncrementalOutput(state.emittedOutput, sanitizeVisibleText(output));
615
- state.emittedOutput = nextOutput.accumulated;
616
- if (nextOutput.delta) {
617
- chunks.push({ kind: "content", content: nextOutput.delta });
618
- }
619
- }
620
- }
621
- return chunks;
622
- }
1
+ import{extractToolFallbackContext as w,extractVisibleOutput as z,readTextContent as I,sanitizeVisibleText as V}from"../parsing/output-parsing.js";import{salvageFunctionLikeToolCall as M}from"../parsing/output-tool-args.js";import{computeIncrementalOutput as O,extractInterruptPayload as G,extractReasoningStreamOutput as $,sanitizeRetainedUpstreamEvent as Q,extractStateStreamOutput as X,extractTerminalStreamOutput as W,extractToolResult as Y,extractVisibleStreamOutput as v,normalizeTerminalOutputKey as tt}from"../parsing/stream-event-parsing.js";import{resolveModelFacingToolName as H}from"./tool/tool-name-mapping.js";function yt(){return{emittedOutput:"",emittedToolError:!1,emittedToolResult:!1,emittedSuccessfulToolResult:!1,emittedSuccessfulTaskResult:!1,emittedPlaceholderTaskResult:!1,hasFailedTaskDelegation:!1,emittedNonTodoToolResult:!1,emittedSuccessfulNonTodoToolResult:!1,sawDelegatedAgentWithConfiguredTools:!1,emittedDelegatedExecutionToolResult:!1,emittedSuccessfulDelegatedExecutionToolResult:!1,emittedDelegatedTerminalOutput:!1,sawPlanState:!1,hasIncompletePlanState:!1,hasFailedPlanState:!1,openTaskDelegations:0,openToolCapableTaskDelegations:0,taskDelegationHasToolsStack:[],taskDelegationFindingsStack:[],lastCompletedTaskDelegationFindings:"",seenTerminalOutputs:new Set}}function y(e){const t=e.trim();if(!t)return!1;if(/^(?:model_request|tool_call|call_tool)\b/iu.test(t)||/^(?:name|tool_call_id)\s*=/iu.test(t)||/^(?:we\s+need\s+to|so\s+next\s+step\b)/iu.test(t)||/\b(?:must|need|needs|should|will)\s+(?:now\s+)?(?:call|use|run|produce)\s+[A-Za-z_][A-Za-z0-9_]*\b/iu.test(t)||/^\{\s*"(?:name|arguments|args|argv|todos|symbol|query|market|count|stdout|stderr|exitCode)"\s*:/iu.test(t))return!0;try{const o=JSON.parse(t);if(typeof o=="object"&&o!==null&&("symbol"in o||"todos"in o||"query"in o||"market"in o||"count"in o||"arguments"in o||"args"in o||"argv"in o||"name"in o||"stdout"in o||"stderr"in o||"exitCode"in o))return!0}catch{}if(M(t))return!0;const r=/^(?:\s*(?:Ready|Understood|Okay|Ok|Got it|Sure|All set)[.:!]?\s*)+([A-Za-z_][A-Za-z0-9_]*\([\s\S]*\))\s*$/u.exec(t);return r?M(r[1])!==null:!1}function C(e){const t=e.split(/\r?\n/);let r=!1;const o=t.filter(c=>y(c)?(r=!0,!1):!0);if(r)return o.join(`
2
+ `).trim();const n=e.trimStart();if(!n.startsWith("{"))return e;const l=et(n);return!l||!y(l.json)?e:l.rest.trimStart()}function et(e){let t=0,r=!1,o=!1;for(let n=0;n<e.length;n+=1){const l=e[n];if(o){o=!1;continue}if(l==="\\"){o=r;continue}if(l==='"'){r=!r;continue}if(!r){if(l==="{"){t+=1;continue}if(l==="}"&&(t-=1,t===0))return{json:e.slice(0,n+1),rest:e.slice(n+1)}}}return null}function nt(e){if(typeof e!="object"||e===null)return null;const t=e;return typeof t.total=="number"&&t.total<=0||!(typeof t.pending=="number"||typeof t.inProgress=="number")?null:{pending:typeof t.pending=="number"?t.pending:0,inProgress:typeof t.inProgress=="number"?t.inProgress:0}}function ot(e){if(typeof e!="object"||e===null)return null;const t=e;return typeof t.total=="number"&&t.total<=0?null:typeof t.failed=="number"?t.failed:null}function J(e){return!Array.isArray(e)||e.length===0?null:e.some(t=>{if(typeof t!="object"||t===null)return!1;const r=t.status;return r==="pending"||r==="in_progress"})}function L(e){return!Array.isArray(e)||e.length===0?null:e.some(t=>typeof t!="object"||t===null?!1:t.status==="failed")}function b(e){if(typeof e!="object"||e===null)return null;const t=e,r=nt(t.summary);if(r)return r.pending>0||r.inProgress>0;if(typeof t.summary=="object"&&t.summary!==null){const n=b(t.summary);if(n!==null)return n}const o=J(t.todos);if(o!==null)return o;if(typeof t.update=="object"&&t.update!==null){const n=J(t.update.todos);if(n!==null)return n}if(typeof t.output=="object"&&t.output!==null){const n=b(t.output);if(n!==null)return n}if(typeof t.data=="object"&&t.data!==null){const n=b(t.data);if(n!==null)return n}return null}function h(e){if(typeof e!="object"||e===null)return null;const t=e,r=ot(t.summary);if(r!==null)return r>0;if(typeof t.summary=="object"&&t.summary!==null){const n=h(t.summary);if(n!==null)return n}const o=L(t.todos);if(o!==null)return o;if(typeof t.update=="object"&&t.update!==null){const n=L(t.update.todos);if(n!==null)return n}if(typeof t.output=="object"&&t.output!==null){const n=h(t.output);if(n!==null)return n}if(typeof t.data=="object"&&t.data!==null){const n=h(t.data);if(n!==null)return n}return null}function st(e){const t=e.trim();if(!t||!t.startsWith("{")&&!t.startsWith("["))return e;try{return JSON.parse(t)}catch{return e}}function _(e){if(typeof e=="string")return st(e);if(typeof e!="object"||e===null||Array.isArray(e))return e;const t=e;return t.args!==void 0?_(t.args):t.input!==void 0?_(t.input):e}function rt(e){const t=_(e);if(typeof t!="object"||t===null||Array.isArray(t))return null;const r=t;return typeof r.subagent_type=="string"&&r.subagent_type.trim().length>0?r.subagent_type.trim():null}function lt(e){if(typeof e=="string")return e.trim()==="Task completed";if(typeof e!="object"||e===null||Array.isArray(e))return!1;const t=e,r=Array.isArray(t.update?.messages)?t.update.messages:[];if(r.length!==1)return!1;const o=r[0];return t.lg_name==="Command"&&t.lc_direct_tool_output===!0&&o?.name==="task"&&o?.type==="tool"&&typeof o?.tool_call_id=="string"&&o?.content==="Task completed"}function F(e,t=0){if(t>6||e===null||e===void 0)return"";if(typeof e=="string")return e;const r=z(e);if(r)return r;const o=w(e);if(o)return o;const n=I(e);if(n)return n;if(Array.isArray(e)){for(let l=e.length-1;l>=0;l-=1){const c=F(e[l],t+1);if(c)return c}return""}if(typeof e=="object"){const l=e;for(const c of["messages","update","output","content","data","summary"]){const f=F(l[c],t+1);if(f)return f}}return""}function it(e){const t=F(e);if(!t)return"";const r=V(t).trim();return r==="Task completed"||/Status:\s*completed[\s\S]*Summary:\s*-\s*none[\s\S]*Likely Causes:\s*-\s*none[\s\S]*Blockers:\s*-\s*none[\s\S]*Next Commands:\s*-\s*none/i.test(r)?"":r}function at(e){const t=typeof e=="string"?e:w(e)||z(e)||I(e);if(t&&/\/large_tool_results\/|internal runtime spill path/i.test(t))return!0;if(typeof e=="object"&&e!==null)try{return/\/large_tool_results\/|internal runtime spill path/i.test(JSON.stringify(e))}catch{return!1}return!1}function Z(e,t,r="tool"){if(e.taskDelegationFindingsStack.length===0)return!1;const o=it(t);if(!o)return!1;const n=e.taskDelegationFindingsStack[e.taskDelegationFindingsStack.length-1]??"";return r==="terminal"&&n||(e.taskDelegationFindingsStack[e.taskDelegationFindingsStack.length-1]=o),!0}function ut(e,t,r){if(typeof t!="object"||t===null)return;const o=t,n=typeof o.event=="string"?o.event:"",l=typeof o.name=="string"?o.name:"",c=typeof o.run_type=="string"?o.run_type:"",f=l==="task"&&(n==="on_tool_start"||n==="on_chain_start"&&c==="tool"),s=l==="task"&&(n==="on_tool_end"||n==="on_chain_end"&&c==="tool"),d=l==="task"&&(n==="on_tool_error"||n==="on_chain_error"&&c==="tool");if(f){e.openTaskDelegations+=1;const p=rt(o.data?.input),g=!!p&&!!r&&r(p)>0;e.taskDelegationHasToolsStack.push(g),g&&(e.sawDelegatedAgentWithConfiguredTools=!0,e.openToolCapableTaskDelegations+=1),e.taskDelegationFindingsStack.push("");return}if(s||d){e.openTaskDelegations=Math.max(0,e.openTaskDelegations-1);const p=e.taskDelegationHasToolsStack.pop()===!0;e.lastCompletedTaskDelegationFindings=e.taskDelegationFindingsStack.pop()??"",p&&(e.openToolCapableTaskDelegations=Math.max(0,e.openToolCapableTaskDelegations-1)),d&&(e.hasFailedTaskDelegation=!0)}}function ct(e){return typeof e=="string"?e.trim().toLowerCase().replace(/[\s-]+/gu,"_"):""}function j(e){const t=ct(e);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 dt(e){if(typeof e!="object"||e===null)return null;const t=e,r=typeof t.event=="string"?t.event:"",o=typeof t.run_type=="string"?t.run_type:"",n=typeof t.name=="string"?t.name:"";if(!(r==="on_tool_start"||r==="on_chain_start"&&o==="tool")||!j(n))return null;const c=_(t.data?.input);return{toolName:n,input:c}}function kt(e){const{event:t,allowVisibleStreamDeltas:r,includeStateStreamOutput:o,rootAgentId:n,countConfiguredToolsForAgentId:l,toolNameMapping:c,primaryTools:f,state:s}=e,d=[{kind:"upstream-event",event:Q(t)}];ut(s,t,l);const p=G(t);if(p)return d.push({kind:"interrupt",content:p}),d;const g=$(t);g&&d.push({kind:"reasoning",content:g});const A=b(t);A!==null&&(s.sawPlanState=!0,s.hasIncompletePlanState=A);const E=h(t);E!==null&&(s.sawPlanState=!0,s.hasFailedPlanState=E);const T=typeof t=="object"&&t!==null&&typeof t.agentId=="string"?t.agentId.trim():"",N=!!n&&!!T&&T!==n;N&&l&&l(T)>0&&(s.sawDelegatedAgentWithConfiguredTools=!0);const k=(!n||!T||T===n)&&s.openTaskDelegations===0,q=k&&!s.emittedToolResult&&!s.emittedToolError;if(r&&q){const i=C(v(t));if(i&&!y(i)){const a=O(s.emittedOutput,i);s.emittedOutput=a.accumulated,a.delta&&d.push({kind:"content",content:a.delta})}}if(o&&k){const i=C(X(t));if(i&&!y(i)){const a=O(s.emittedOutput,i);s.emittedOutput=a.accumulated,a.delta&&d.push({kind:"content",content:a.delta})}}const u=Y(t),D=u?null:dt(t);if(D&&(s.emittedToolResult=!0,s.emittedSuccessfulToolResult=!0,d.push({kind:"tool-result",toolName:H(D.toolName,c,f),output:D.input,isError:!1})),u){const i=j(u.toolName),a=u.toolName==="task"&&u.isError===!0&&s.lastCompletedTaskDelegationFindings&&at(u.output)?s.lastCompletedTaskDelegationFindings:"",U=a||u.output,m=a?!1:u.isError===!0,x=u.toolName==="task"&&m!==!0,R=(N||s.openToolCapableTaskDelegations>0)&&!j(u.toolName)&&u.toolName!=="task";R&&u.isError!==!0&&Z(s,u.output,"tool");const P=x&&s.lastCompletedTaskDelegationFindings?s.lastCompletedTaskDelegationFindings:"",K=P||U,B=x&&!P&&lt(u.output);s.emittedToolResult=!0,s.emittedToolError=s.emittedToolError||m===!0,s.emittedSuccessfulToolResult=s.emittedSuccessfulToolResult||m!==!0,s.emittedSuccessfulTaskResult=s.emittedSuccessfulTaskResult||x,s.emittedPlaceholderTaskResult=s.emittedPlaceholderTaskResult||B,s.emittedNonTodoToolResult=s.emittedNonTodoToolResult||!i,s.emittedSuccessfulNonTodoToolResult=s.emittedSuccessfulNonTodoToolResult||!i&&m!==!0,s.emittedDelegatedExecutionToolResult=s.emittedDelegatedExecutionToolResult||R||!!a,s.emittedSuccessfulDelegatedExecutionToolResult=s.emittedSuccessfulDelegatedExecutionToolResult||R&&m!==!0||!!a,a&&(s.hasFailedTaskDelegation=!1),d.push({kind:"tool-result",toolName:H(u.toolName,c,f),output:K,isError:m}),u.toolName==="task"&&(s.lastCompletedTaskDelegationFindings="")}const S=k?C(W(t)):"";if(!k){const i=W(t);i&&(s.emittedDelegatedTerminalOutput=s.emittedDelegatedTerminalOutput||Z(s,i,"terminal"))}if(S&&!y(S)){const i=tt(S);if(!i||!s.seenTerminalOutputs.has(i)){i&&s.seenTerminalOutputs.add(i);const a=O(s.emittedOutput,V(S));s.emittedOutput=a.accumulated,a.delta&&d.push({kind:"content",content:a.delta})}}return d}export{yt as createStreamEventProjectionState,kt as projectRuntimeStreamEvent};
@@ -1,18 +1 @@
1
- import { extractReasoningText } from "../parsing/output-parsing.js";
2
- import { readStreamDelta } from "../parsing/stream-event-parsing.js";
3
- export async function* projectTextStreamChunks(stream) {
4
- let emittedContent = false;
5
- for await (const chunk of stream) {
6
- const delta = readStreamDelta(chunk);
7
- if (delta) {
8
- emittedContent = true;
9
- yield { kind: "content", content: delta };
10
- continue;
11
- }
12
- const reasoning = extractReasoningText(chunk);
13
- if (reasoning) {
14
- yield { kind: "reasoning", content: reasoning };
15
- }
16
- }
17
- return { emittedContent };
18
- }
1
+ import{extractReasoningText as i}from"../parsing/output-parsing.js";import{readStreamDelta as a}from"../parsing/stream-event-parsing.js";async function*f(r){let t=!1;for await(const n of r){const e=a(n);if(e){t=!0,yield{kind:"content",content:e};continue}const o=i(n);o&&(yield{kind:"reasoning",content:o})}return{emittedContent:t}}export{f as projectTextStreamChunks};