@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,663 +1,7 @@
1
- export function tryParseJson(value) {
2
- try {
3
- return JSON.parse(value);
4
- }
5
- catch {
6
- return null;
7
- }
8
- }
9
- function splitTopLevelDelimited(value, delimiter) {
10
- const parts = [];
11
- let depthParen = 0;
12
- let depthBracket = 0;
13
- let depthBrace = 0;
14
- let inString = false;
15
- let quoteChar = "";
16
- let escaping = false;
17
- let start = 0;
18
- for (let index = 0; index < value.length; index += 1) {
19
- const char = value[index];
20
- if (inString) {
21
- if (escaping) {
22
- escaping = false;
23
- continue;
24
- }
25
- if (char === "\\") {
26
- escaping = true;
27
- continue;
28
- }
29
- if (char === quoteChar) {
30
- inString = false;
31
- quoteChar = "";
32
- }
33
- continue;
34
- }
35
- if (char === "\"" || char === "'") {
36
- inString = true;
37
- quoteChar = char;
38
- continue;
39
- }
40
- if (char === "(")
41
- depthParen += 1;
42
- else if (char === ")")
43
- depthParen -= 1;
44
- else if (char === "[")
45
- depthBracket += 1;
46
- else if (char === "]")
47
- depthBracket -= 1;
48
- else if (char === "{")
49
- depthBrace += 1;
50
- else if (char === "}")
51
- depthBrace -= 1;
52
- else if (char === delimiter && depthParen === 0 && depthBracket === 0 && depthBrace === 0) {
53
- parts.push(value.slice(start, index).trim());
54
- start = index + 1;
55
- }
56
- }
57
- parts.push(value.slice(start).trim());
58
- return parts.filter(Boolean);
59
- }
60
- function parseFunctionLikeScalar(value) {
61
- const trimmed = value.trim();
62
- if (!trimmed)
63
- return "";
64
- if ((trimmed.startsWith("\"") && trimmed.endsWith("\"")) || (trimmed.startsWith("'") && trimmed.endsWith("'"))) {
65
- try {
66
- return JSON.parse(trimmed.startsWith("'")
67
- ? `"${trimmed.slice(1, -1).replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`
68
- : trimmed);
69
- }
70
- catch {
71
- return trimmed.slice(1, -1);
72
- }
73
- }
74
- if (trimmed === "true")
75
- return true;
76
- if (trimmed === "false")
77
- return false;
78
- if (trimmed === "null")
79
- return null;
80
- if (/^-?\d+(\.\d+)?$/.test(trimmed)) {
81
- return Number(trimmed);
82
- }
83
- const directJson = tryParseJson(trimmed);
84
- if (directJson !== null) {
85
- return directJson;
86
- }
87
- return trimmed;
88
- }
89
- export function salvageFunctionLikeToolCall(value) {
90
- if (typeof value !== "string") {
91
- return null;
92
- }
93
- const trimmed = value.trim();
94
- const match = /^([A-Za-z_][A-Za-z0-9_]*)\(([\s\S]*)\)$/.exec(trimmed);
95
- if (!match) {
96
- return null;
97
- }
98
- const [, name, rawArgList] = match;
99
- const entries = splitTopLevelDelimited(rawArgList, ",");
100
- const args = {};
101
- for (const entry of entries) {
102
- const eqIndex = entry.indexOf("=");
103
- if (eqIndex <= 0) {
104
- return null;
105
- }
106
- const key = entry.slice(0, eqIndex).trim();
107
- const rawValue = entry.slice(eqIndex + 1).trim();
108
- if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) {
109
- return null;
110
- }
111
- args[key] = parseFunctionLikeScalar(rawValue);
112
- }
113
- return { name, args: normalizeKnownToolArgs(name, args) };
114
- }
115
- function normalizeFieldLabel(value) {
116
- return value
117
- .trim()
118
- .toLowerCase()
119
- .split("")
120
- .filter((char) => {
121
- const code = char.charCodeAt(0);
122
- return (code >= 97 && code <= 122) || (code >= 48 && code <= 57);
123
- })
124
- .join("");
125
- }
126
- function splitLabeledLine(line) {
127
- const separatorIndex = line.indexOf(":");
128
- if (separatorIndex <= 0) {
129
- return null;
130
- }
131
- const label = normalizeFieldLabel(line.slice(0, separatorIndex));
132
- const value = line.slice(separatorIndex + 1).trim();
133
- return label ? { label, value } : null;
134
- }
135
- function isToolName(value) {
136
- if (!value) {
137
- return false;
138
- }
139
- const first = value.charCodeAt(0);
140
- if (!((first >= 65 && first <= 90) || (first >= 97 && first <= 122) || first === 95)) {
141
- return false;
142
- }
143
- for (let index = 1; index < value.length; index += 1) {
144
- const code = value.charCodeAt(index);
145
- if (!((code >= 65 && code <= 90) || (code >= 97 && code <= 122) || (code >= 48 && code <= 57) || code === 95)) {
146
- return false;
147
- }
148
- }
149
- return true;
150
- }
151
- function isToolNameLabel(label) {
152
- return label === "toolcall" || label === "tool" || label === "functioncall" || label === "function";
153
- }
154
- function isToolArgsLabel(label) {
155
- return label === "arguments" || label === "args" || label === "parameters" || label === "input";
156
- }
157
- export function salvageLabeledToolCall(value) {
158
- if (typeof value !== "string") {
159
- return null;
160
- }
161
- const lines = value
162
- .split("\n")
163
- .map((line) => line.trim())
164
- .filter(Boolean);
165
- for (let index = 0; index < lines.length; index += 1) {
166
- const toolLine = splitLabeledLine(lines[index]);
167
- if (!toolLine || !isToolNameLabel(toolLine.label) || !isToolName(toolLine.value)) {
168
- continue;
169
- }
170
- for (let argsIndex = index + 1; argsIndex < lines.length; argsIndex += 1) {
171
- const argsLine = splitLabeledLine(lines[argsIndex]);
172
- if (!argsLine || !isToolArgsLabel(argsLine.label)) {
173
- continue;
174
- }
175
- const argsText = [argsLine.value, ...lines.slice(argsIndex + 1)].filter(Boolean).join("\n").trim();
176
- const args = salvageToolArgs(argsText) ?? {};
177
- return { name: toolLine.value, args: normalizeKnownToolArgs(toolLine.value, args) };
178
- }
179
- }
180
- return null;
181
- }
182
- export function salvageResultLabeledToolCall(value) {
183
- if (typeof value !== "string") {
184
- return null;
185
- }
186
- const lines = value
187
- .split("\n")
188
- .map((line) => line.trim())
189
- .filter(Boolean);
190
- const label = lines[0]?.replace(/[*`#]/gu, "").trim() ?? "";
191
- const match = /^([A-Za-z_][A-Za-z0-9_]*)\s+result\b/iu.exec(label);
192
- if (!match || !isToolName(match[1])) {
193
- return null;
194
- }
195
- const args = salvageToolArgs(lines.slice(1).join("\n")) ?? {};
196
- return { name: match[1], args: normalizeKnownToolArgs(match[1], args) };
197
- }
198
- export function salvageMarkdownNamedToolCall(value) {
199
- if (typeof value !== "string") {
200
- return null;
201
- }
202
- const lines = value
203
- .split("\n")
204
- .map((line) => line.trim())
205
- .filter(Boolean);
206
- if (lines.length < 2) {
207
- return null;
208
- }
209
- const label = lines[0]?.replace(/[*`#]/gu, "").trim() ?? "";
210
- if (!isToolName(label) || ["json", "javascript", "typescript", "python", "yaml", "text"].includes(label.toLowerCase())) {
211
- return null;
212
- }
213
- const rest = lines.slice(1).join("\n").trim();
214
- const args = salvageToolArgs(rest);
215
- if (!args) {
216
- return null;
217
- }
218
- return { name: label, args: normalizeKnownToolArgs(label, args) };
219
- }
220
- function extractBalancedJsonValue(value, openChar, closeChar) {
221
- const start = value.indexOf(openChar);
222
- if (start < 0)
223
- return null;
224
- let depth = 0;
225
- let inString = false;
226
- let escaping = false;
227
- for (let index = start; index < value.length; index += 1) {
228
- const char = value[index];
229
- if (inString) {
230
- if (escaping) {
231
- escaping = false;
232
- continue;
233
- }
234
- if (char === "\\") {
235
- escaping = true;
236
- continue;
237
- }
238
- if (char === "\"") {
239
- inString = false;
240
- }
241
- continue;
242
- }
243
- if (char === "\"") {
244
- inString = true;
245
- continue;
246
- }
247
- if (char === openChar) {
248
- depth += 1;
249
- continue;
250
- }
251
- if (char === closeChar) {
252
- depth -= 1;
253
- if (depth === 0) {
254
- return value.slice(start, index + 1);
255
- }
256
- }
257
- }
258
- return null;
259
- }
260
- function extractBalancedJsonObject(value) {
261
- return extractBalancedJsonValue(value, "{", "}");
262
- }
263
- function extractBalancedJsonArray(value) {
264
- return extractBalancedJsonValue(value, "[", "]");
265
- }
266
- function closeJsonContainerSuffix(value) {
267
- const trimmed = value.trim();
268
- if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) {
269
- return null;
270
- }
271
- const stack = [];
272
- let inString = false;
273
- let escaping = false;
274
- for (const char of trimmed) {
275
- if (inString) {
276
- if (escaping) {
277
- escaping = false;
278
- continue;
279
- }
280
- if (char === "\\") {
281
- escaping = true;
282
- continue;
283
- }
284
- if (char === "\"") {
285
- inString = false;
286
- }
287
- continue;
288
- }
289
- if (char === "\"") {
290
- inString = true;
291
- continue;
292
- }
293
- if (char === "{") {
294
- stack.push("}");
295
- continue;
296
- }
297
- if (char === "[") {
298
- stack.push("]");
299
- continue;
300
- }
301
- if (char === "}" || char === "]") {
302
- const expected = stack.pop();
303
- if (expected !== char) {
304
- return null;
305
- }
306
- }
307
- }
308
- if (inString || stack.length === 0) {
309
- return null;
310
- }
311
- return `${trimmed}${stack.reverse().join("")}`;
312
- }
313
- function normalizePythonLikeJson(value) {
314
- const trimmed = value.trim();
315
- if (!trimmed.includes("'")) {
316
- return null;
317
- }
318
- let output = "";
319
- let inSingle = false;
320
- let inDouble = false;
321
- let escaping = false;
322
- for (const char of trimmed) {
323
- if (escaping) {
324
- output += char;
325
- escaping = false;
326
- continue;
327
- }
328
- if (char === "\\") {
329
- output += char;
330
- escaping = true;
331
- continue;
332
- }
333
- if (char === "\"" && !inSingle) {
334
- inDouble = !inDouble;
335
- output += char;
336
- continue;
337
- }
338
- if (char === "'" && !inDouble) {
339
- inSingle = !inSingle;
340
- output += "\"";
341
- continue;
342
- }
343
- output += char;
344
- }
345
- if (inSingle || inDouble) {
346
- return null;
347
- }
348
- return output;
349
- }
350
- function repairMissingArrayObjectOpenBraces(value) {
351
- let output = "";
352
- let changed = false;
353
- let inString = false;
354
- let escaping = false;
355
- const stack = [];
356
- for (let index = 0; index < value.length; index += 1) {
357
- const char = value[index];
358
- if (inString) {
359
- output += char;
360
- if (escaping) {
361
- escaping = false;
362
- continue;
363
- }
364
- if (char === "\\") {
365
- escaping = true;
366
- continue;
367
- }
368
- if (char === "\"") {
369
- inString = false;
370
- }
371
- continue;
372
- }
373
- if (char === "\"") {
374
- output += char;
375
- inString = true;
376
- continue;
377
- }
378
- if (char === "{" || char === "[") {
379
- stack.push(char);
380
- output += char;
381
- continue;
382
- }
383
- if (char === "}" || char === "]") {
384
- const expectedOpen = char === "}" ? "{" : "[";
385
- if (stack.at(-1) === expectedOpen) {
386
- stack.pop();
387
- }
388
- output += char;
389
- continue;
390
- }
391
- if (char !== "," || stack.at(-1) !== "[") {
392
- output += char;
393
- continue;
394
- }
395
- output += char;
396
- let lookahead = index + 1;
397
- while (lookahead < value.length && /\s/u.test(value[lookahead] ?? "")) {
398
- output += value[lookahead];
399
- lookahead += 1;
400
- }
401
- if (value[lookahead] !== "\"") {
402
- index = lookahead - 1;
403
- continue;
404
- }
405
- let cursor = lookahead + 1;
406
- let keyEscaping = false;
407
- while (cursor < value.length) {
408
- const next = value[cursor];
409
- if (keyEscaping) {
410
- keyEscaping = false;
411
- }
412
- else if (next === "\\") {
413
- keyEscaping = true;
414
- }
415
- else if (next === "\"") {
416
- break;
417
- }
418
- cursor += 1;
419
- }
420
- let colonCursor = cursor + 1;
421
- while (colonCursor < value.length && /\s/u.test(value[colonCursor] ?? "")) {
422
- colonCursor += 1;
423
- }
424
- if (value[colonCursor] === ":") {
425
- output += "{";
426
- stack.push("{");
427
- changed = true;
428
- }
429
- index = lookahead - 1;
430
- }
431
- return changed ? output : null;
432
- }
433
- export function salvageToolArgs(value) {
434
- if (typeof value === "object" && value && !Array.isArray(value)) {
435
- return value;
436
- }
437
- if (typeof value !== "string") {
438
- return null;
439
- }
440
- const trimmed = value.trim();
441
- if (!trimmed) {
442
- return {};
443
- }
444
- const direct = tryParseJson(trimmed);
445
- if (typeof direct === "object" && direct && !Array.isArray(direct)) {
446
- return direct;
447
- }
448
- const embedded = extractBalancedJsonObject(trimmed);
449
- if (!embedded) {
450
- return null;
451
- }
452
- const parsed = tryParseJson(embedded);
453
- return typeof parsed === "object" && parsed && !Array.isArray(parsed) ? parsed : null;
454
- }
455
- function normalizeJsonToolCallPayload(payload) {
456
- if (typeof payload !== "object" || payload === null || Array.isArray(payload)) {
457
- return null;
458
- }
459
- const typed = payload;
460
- const functionPayload = typeof typed.function === "object" && typed.function !== null ? typed.function : undefined;
461
- const nameCandidate = typed.name ?? typed.tool ?? functionPayload?.name;
462
- const name = typeof nameCandidate === "string" ? nameCandidate.trim() : "";
463
- if (!name) {
464
- return null;
465
- }
466
- const argsCandidate = typed.arguments ?? typed.args ?? typed.parameters ?? typed.input ?? functionPayload?.arguments ?? {};
467
- const args = Array.isArray(argsCandidate)
468
- ? { args: argsCandidate }
469
- : salvageToolArgs(argsCandidate) ?? {};
470
- return { name, args: normalizeKnownToolArgs(name, args) };
471
- }
472
- export function salvageJsonToolCalls(value) {
473
- const payload = typeof value === "string"
474
- ? (() => {
475
- const trimmed = value.trim();
476
- if (!trimmed) {
477
- return null;
478
- }
479
- const direct = tryParseJson(trimmed);
480
- if (direct) {
481
- return direct;
482
- }
483
- const repairedArrayObjects = repairMissingArrayObjectOpenBraces(trimmed);
484
- if (repairedArrayObjects) {
485
- const parsed = tryParseJson(repairedArrayObjects);
486
- if (parsed) {
487
- return parsed;
488
- }
489
- }
490
- const pythonLike = normalizePythonLikeJson(trimmed);
491
- if (pythonLike) {
492
- const parsed = tryParseJson(pythonLike);
493
- if (parsed) {
494
- return parsed;
495
- }
496
- }
497
- if (pythonLike) {
498
- const repairedPythonLike = repairMissingArrayObjectOpenBraces(pythonLike);
499
- if (repairedPythonLike) {
500
- const parsed = tryParseJson(repairedPythonLike);
501
- if (parsed) {
502
- return parsed;
503
- }
504
- }
505
- }
506
- const closed = closeJsonContainerSuffix(trimmed);
507
- if (closed) {
508
- const parsed = tryParseJson(closed);
509
- if (parsed) {
510
- return parsed;
511
- }
512
- }
513
- if (repairedArrayObjects) {
514
- const closedRepaired = closeJsonContainerSuffix(repairedArrayObjects);
515
- if (closedRepaired) {
516
- const parsed = tryParseJson(closedRepaired);
517
- if (parsed) {
518
- return parsed;
519
- }
520
- }
521
- }
522
- const embeddedObject = extractBalancedJsonObject(trimmed);
523
- if (embeddedObject) {
524
- const parsed = tryParseJson(embeddedObject);
525
- if (parsed) {
526
- return parsed;
527
- }
528
- }
529
- const embeddedArray = extractBalancedJsonArray(trimmed);
530
- if (embeddedArray) {
531
- const parsed = tryParseJson(embeddedArray);
532
- if (parsed) {
533
- return parsed;
534
- }
535
- }
536
- return null;
537
- })()
538
- : value;
539
- const candidates = Array.isArray(payload) ? payload : [payload];
540
- return candidates
541
- .map((item) => normalizeJsonToolCallPayload(item))
542
- .filter((item) => item !== null);
543
- }
544
- function normalizeWriteTodosArgs(args) {
545
- if (typeof args.name === "string" &&
546
- args.name === "write_todos" &&
547
- typeof args.arguments === "object" &&
548
- args.arguments !== null &&
549
- !Array.isArray(args.arguments)) {
550
- return normalizeWriteTodosArgs(args.arguments);
551
- }
552
- if (Array.isArray(args.items) && !Array.isArray(args.todos)) {
553
- return normalizeWriteTodosArgs({ ...args, todos: args.items });
554
- }
555
- if (Array.isArray(args.tasks) && !Array.isArray(args.todos)) {
556
- return normalizeWriteTodosArgs({ ...args, todos: args.tasks });
557
- }
558
- if (Array.isArray(args.todo) && !Array.isArray(args.todos)) {
559
- return normalizeWriteTodosArgs({ ...args, todos: args.todo });
560
- }
561
- if (!Array.isArray(args.todos)) {
562
- return args;
563
- }
564
- const { items: _items, tasks: _tasks, todo: _todo, ...rest } = args;
565
- return {
566
- ...rest,
567
- todos: args.todos.map((todo, index) => {
568
- if (typeof todo === "string") {
569
- const content = todo.trim();
570
- return {
571
- content: content.length > 0 ? content : `Step ${index + 1}`,
572
- status: index === 0 ? "in_progress" : "pending",
573
- };
574
- }
575
- if (typeof todo !== "object" || !todo || Array.isArray(todo)) {
576
- return todo;
577
- }
578
- const record = todo;
579
- const content = typeof record.content === "string" && record.content.trim().length > 0
580
- ? record.content
581
- : typeof record.description === "string" && record.description.trim().length > 0
582
- ? record.description
583
- : typeof record.title === "string" && record.title.trim().length > 0
584
- ? record.title
585
- : typeof record.name === "string" && record.name.trim().length > 0
586
- ? record.name
587
- : typeof record.text === "string" && record.text.trim().length > 0
588
- ? record.text
589
- : typeof record.task === "string" && record.task.trim().length > 0
590
- ? record.task
591
- : typeof record.action === "string" && record.action.trim().length > 0
592
- ? record.action
593
- : typeof record.step === "string" && record.step.trim().length > 0
594
- ? record.step
595
- : `Step ${index + 1}`;
596
- const normalized = {};
597
- if (content !== undefined)
598
- normalized.content = content;
599
- normalized.status = normalizeWriteTodoStatus(record.status);
600
- return Object.keys(normalized).length > 0 ? normalized : todo;
601
- }),
602
- };
603
- }
604
- function normalizeWriteTodoStatus(value) {
605
- if (typeof value !== "string") {
606
- return "pending";
607
- }
608
- const normalized = value.trim().toLowerCase().replace(/[\s-]+/gu, "_");
609
- if (!normalized) {
610
- return "pending";
611
- }
612
- if (normalized === "not_started" || normalized === "todo" || normalized === "open") {
613
- return "pending";
614
- }
615
- if (normalized === "started" || normalized === "active" || normalized === "in_progress") {
616
- return "in_progress";
617
- }
618
- if (normalized === "done" || normalized === "complete" || normalized === "completed") {
619
- return "completed";
620
- }
621
- return normalized;
622
- }
623
- function normalizeTaskArgs(args) {
624
- const description = typeof args.description === "string" && args.description.trim().length > 0
625
- ? args.description
626
- : undefined;
627
- const subagentType = typeof args.subagent_type === "string" && args.subagent_type.trim().length > 0
628
- ? args.subagent_type
629
- : undefined;
630
- return {
631
- ...args,
632
- ...(description !== undefined ? { description } : {}),
633
- ...(subagentType !== undefined ? { subagent_type: subagentType } : {}),
634
- };
635
- }
636
- export function normalizeKnownToolArgs(toolName, args) {
637
- if (toolName === "write_todos") {
638
- return normalizeWriteTodosArgs(args);
639
- }
640
- if (toolName === "task") {
641
- return normalizeTaskArgs(args);
642
- }
643
- return args;
644
- }
645
- export function isLikelyToolArgsObject(value) {
646
- if (typeof value !== "object" || !value || Array.isArray(value)) {
647
- return false;
648
- }
649
- const keys = Object.keys(value);
650
- if (keys.length === 0 || keys.length > 8) {
651
- return false;
652
- }
653
- if (keys.some((key) => ["messages", "output", "content", "todos", "files", "skillsMetadata", "memoryContents"].includes(key))) {
654
- return false;
655
- }
656
- return Object.values(value).every((item) => {
657
- if (item == null)
658
- return true;
659
- if (typeof item === "string" || typeof item === "number" || typeof item === "boolean")
660
- return true;
661
- return Array.isArray(item) && item.every((entry) => typeof entry === "string" || typeof entry === "number" || typeof entry === "boolean");
662
- });
663
- }
1
+ function p(t){try{return JSON.parse(t)}catch{return null}}function L(t,e){const n=[];let r=0,s=0,o=0,l=!1,i="",f=!1,a=0;for(let c=0;c<t.length;c+=1){const u=t[c];if(l){if(f){f=!1;continue}if(u==="\\"){f=!0;continue}u===i&&(l=!1,i="");continue}if(u==='"'||u==="'"){l=!0,i=u;continue}u==="("?r+=1:u===")"?r-=1:u==="["?s+=1:u==="]"?s-=1:u==="{"?o+=1:u==="}"?o-=1:u===e&&r===0&&s===0&&o===0&&(n.push(t.slice(a,c).trim()),a=c+1)}return n.push(t.slice(a).trim()),n.filter(Boolean)}function _(t){const e=t.trim();if(!e)return"";if(e.startsWith('"')&&e.endsWith('"')||e.startsWith("'")&&e.endsWith("'"))try{return JSON.parse(e.startsWith("'")?`"${e.slice(1,-1).replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"`:e)}catch{return e.slice(1,-1)}if(e==="true")return!0;if(e==="false")return!1;if(e==="null")return null;if(/^-?\d+(\.\d+)?$/.test(e))return Number(e);const n=p(e);return n!==null?n:e}function W(t){if(typeof t!="string")return null;const e=t.trim(),n=/^([A-Za-z_][A-Za-z0-9_]*)\(([\s\S]*)\)$/.exec(e);if(!n)return null;const[,r,s]=n,o=L(s,","),l={};for(const i of o){const f=i.indexOf("=");if(f<=0)return null;const a=i.slice(0,f).trim(),c=i.slice(f+1).trim();if(!/^[A-Za-z_][A-Za-z0-9_]*$/.test(a))return null;l[a]=_(c)}return{name:r,args:m(r,l)}}function T(t){return t.trim().toLowerCase().split("").filter(e=>{const n=e.charCodeAt(0);return n>=97&&n<=122||n>=48&&n<=57}).join("")}function A(t){const e=t.indexOf(":");if(e<=0)return null;const n=T(t.slice(0,e)),r=t.slice(e+1).trim();return n?{label:n,value:r}:null}function h(t){if(!t)return!1;const e=t.charCodeAt(0);if(!(e>=65&&e<=90||e>=97&&e<=122||e===95))return!1;for(let n=1;n<t.length;n+=1){const r=t.charCodeAt(n);if(!(r>=65&&r<=90||r>=97&&r<=122||r>=48&&r<=57||r===95))return!1}return!0}function C(t){return t==="toolcall"||t==="tool"||t==="functioncall"||t==="function"}function z(t){return t==="arguments"||t==="args"||t==="parameters"||t==="input"}function $(t){if(typeof t!="string")return null;const e=t.split(`
2
+ `).map(n=>n.trim()).filter(Boolean);for(let n=0;n<e.length;n+=1){const r=A(e[n]);if(!(!r||!C(r.label)||!h(r.value)))for(let s=n+1;s<e.length;s+=1){const o=A(e[s]);if(!o||!z(o.label))continue;const l=[o.value,...e.slice(s+1)].filter(Boolean).join(`
3
+ `).trim(),i=g(l)??{};return{name:r.value,args:m(r.value,i)}}}return null}function P(t){if(typeof t!="string")return null;const e=t.split(`
4
+ `).map(o=>o.trim()).filter(Boolean),n=e[0]?.replace(/[*`#]/gu,"").trim()??"",r=/^([A-Za-z_][A-Za-z0-9_]*)\s+result\b/iu.exec(n);if(!r||!h(r[1]))return null;const s=g(e.slice(1).join(`
5
+ `))??{};return{name:r[1],args:m(r[1],s)}}function Z(t){if(typeof t!="string")return null;const e=t.split(`
6
+ `).map(o=>o.trim()).filter(Boolean);if(e.length<2)return null;const n=e[0]?.replace(/[*`#]/gu,"").trim()??"";if(!h(n)||["json","javascript","typescript","python","yaml","text"].includes(n.toLowerCase()))return null;const r=e.slice(1).join(`
7
+ `).trim(),s=g(r);return s?{name:n,args:m(n,s)}:null}function b(t,e,n){const r=t.indexOf(e);if(r<0)return null;let s=0,o=!1,l=!1;for(let i=r;i<t.length;i+=1){const f=t[i];if(o){if(l){l=!1;continue}if(f==="\\"){l=!0;continue}f==='"'&&(o=!1);continue}if(f==='"'){o=!0;continue}if(f===e){s+=1;continue}if(f===n&&(s-=1,s===0))return t.slice(r,i+1)}return null}function x(t){return b(t,"{","}")}function O(t){return b(t,"[","]")}function k(t){const e=t.trim();if(!e.startsWith("{")&&!e.startsWith("["))return null;const n=[];let r=!1,s=!1;for(const o of e){if(r){if(s){s=!1;continue}if(o==="\\"){s=!0;continue}o==='"'&&(r=!1);continue}if(o==='"'){r=!0;continue}if(o==="{"){n.push("}");continue}if(o==="["){n.push("]");continue}if((o==="}"||o==="]")&&n.pop()!==o)return null}return r||n.length===0?null:`${e}${n.reverse().join("")}`}function S(t){const e=t.trim();if(!e.includes("'"))return null;let n="",r=!1,s=!1,o=!1;for(const l of e){if(o){n+=l,o=!1;continue}if(l==="\\"){n+=l,o=!0;continue}if(l==='"'&&!r){s=!s,n+=l;continue}if(l==="'"&&!s){r=!r,n+='"';continue}n+=l}return r||s?null:n}function j(t){let e="",n=!1,r=!1,s=!1;const o=[];for(let l=0;l<t.length;l+=1){const i=t[l];if(r){if(e+=i,s){s=!1;continue}if(i==="\\"){s=!0;continue}i==='"'&&(r=!1);continue}if(i==='"'){e+=i,r=!0;continue}if(i==="{"||i==="["){o.push(i),e+=i;continue}if(i==="}"||i==="]"){const y=i==="}"?"{":"[";o.at(-1)===y&&o.pop(),e+=i;continue}if(i!==","||o.at(-1)!=="["){e+=i;continue}e+=i;let f=l+1;for(;f<t.length&&/\s/u.test(t[f]??"");)e+=t[f],f+=1;if(t[f]!=='"'){l=f-1;continue}let a=f+1,c=!1;for(;a<t.length;){const y=t[a];if(c)c=!1;else if(y==="\\")c=!0;else if(y==='"')break;a+=1}let u=a+1;for(;u<t.length&&/\s/u.test(t[u]??"");)u+=1;t[u]===":"&&(e+="{",o.push("{"),n=!0),l=f-1}return n?e:null}function g(t){if(typeof t=="object"&&t&&!Array.isArray(t))return t;if(typeof t!="string")return null;const e=t.trim();if(!e)return{};const n=p(e);if(typeof n=="object"&&n&&!Array.isArray(n))return n;const r=x(e);if(!r)return null;const s=p(r);return typeof s=="object"&&s&&!Array.isArray(s)?s:null}function w(t){if(typeof t!="object"||t===null||Array.isArray(t))return null;const e=t,n=typeof e.function=="object"&&e.function!==null?e.function:void 0,r=e.name??e.tool??n?.name,s=typeof r=="string"?r.trim():"";if(!s)return null;const o=e.arguments??e.args??e.parameters??e.input??n?.arguments??{},l=Array.isArray(o)?{args:o}:g(o)??{};return{name:s,args:m(s,l)}}function N(t){const e=typeof t=="string"?(()=>{const r=t.trim();if(!r)return null;const s=p(r);if(s)return s;const o=j(r);if(o){const c=p(o);if(c)return c}const l=S(r);if(l){const c=p(l);if(c)return c}if(l){const c=j(l);if(c){const u=p(c);if(u)return u}}const i=k(r);if(i){const c=p(i);if(c)return c}if(o){const c=k(o);if(c){const u=p(c);if(u)return u}}const f=x(r);if(f){const c=p(f);if(c)return c}const a=O(r);if(a){const c=p(a);if(c)return c}return null})():t;return(Array.isArray(e)?e:[e]).map(r=>w(r)).filter(r=>r!==null)}function d(t){if(typeof t.name=="string"&&t.name==="write_todos"&&typeof t.arguments=="object"&&t.arguments!==null&&!Array.isArray(t.arguments))return d(t.arguments);if(Array.isArray(t.items)&&!Array.isArray(t.todos))return d({...t,todos:t.items});if(Array.isArray(t.tasks)&&!Array.isArray(t.todos))return d({...t,todos:t.tasks});if(Array.isArray(t.todo)&&!Array.isArray(t.todos))return d({...t,todos:t.todo});if(!Array.isArray(t.todos))return t;const{items:e,tasks:n,todo:r,...s}=t;return{...s,todos:t.todos.map((o,l)=>{if(typeof o=="string"){const c=o.trim();return{content:c.length>0?c:`Step ${l+1}`,status:l===0?"in_progress":"pending"}}if(typeof o!="object"||!o||Array.isArray(o))return o;const i=o,f=typeof i.content=="string"&&i.content.trim().length>0?i.content:typeof i.description=="string"&&i.description.trim().length>0?i.description:typeof i.title=="string"&&i.title.trim().length>0?i.title:typeof i.name=="string"&&i.name.trim().length>0?i.name:typeof i.text=="string"&&i.text.trim().length>0?i.text:typeof i.task=="string"&&i.task.trim().length>0?i.task:typeof i.action=="string"&&i.action.trim().length>0?i.action:typeof i.step=="string"&&i.step.trim().length>0?i.step:`Step ${l+1}`,a={};return f!==void 0&&(a.content=f),a.status=B(i.status),Object.keys(a).length>0?a:o})}}function B(t){if(typeof t!="string")return"pending";const e=t.trim().toLowerCase().replace(/[\s-]+/gu,"_");return!e||e==="not_started"||e==="todo"||e==="open"?"pending":e==="started"||e==="active"||e==="in_progress"?"in_progress":e==="done"||e==="complete"||e==="completed"?"completed":e}function J(t){const e=typeof t.description=="string"&&t.description.trim().length>0?t.description:void 0,n=typeof t.subagent_type=="string"&&t.subagent_type.trim().length>0?t.subagent_type:void 0;return{...t,...e!==void 0?{description:e}:{},...n!==void 0?{subagent_type:n}:{}}}function m(t,e){return t==="write_todos"?d(e):t==="task"?J(e):e}function F(t){if(typeof t!="object"||!t||Array.isArray(t))return!1;const e=Object.keys(t);return e.length===0||e.length>8||e.some(n=>["messages","output","content","todos","files","skillsMetadata","memoryContents"].includes(n))?!1:Object.values(t).every(n=>n==null||typeof n=="string"||typeof n=="number"||typeof n=="boolean"?!0:Array.isArray(n)&&n.every(r=>typeof r=="string"||typeof r=="number"||typeof r=="boolean"))}export{F as isLikelyToolArgsObject,m as normalizeKnownToolArgs,W as salvageFunctionLikeToolCall,N as salvageJsonToolCalls,$ as salvageLabeledToolCall,Z as salvageMarkdownNamedToolCall,P as salvageResultLabeledToolCall,g as salvageToolArgs,p as tryParseJson};