@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,486 +1,3 @@
1
- import { salvageToolArgs } from "../../parsing/output-parsing.js";
2
- import { normalizeKnownToolArgs, salvageJsonToolCalls, salvageMarkdownNamedToolCall, salvageResultLabeledToolCall } from "../../parsing/output-tool-args.js";
3
- import { isRecord } from "../../../utils/object.js";
4
- import { extractExplicitResourceReferences, hasExplicitResourceReference } from "../../harness/system/runtime-memory-policy.js";
5
- import { readCapturedPromptedJsonToolCalls } from "../model/prompted-json-tool-call-capture.js";
6
- function isObject(value) {
7
- return isRecord(value);
8
- }
9
- export function stringifyToolOutput(output) {
10
- let rendered;
11
- if (typeof output === "string") {
12
- rendered = output;
13
- }
14
- else {
15
- try {
16
- rendered = JSON.stringify(output);
17
- }
18
- catch {
19
- rendered = `${String(output)}`;
20
- }
21
- }
22
- const maxChars = 4_000;
23
- if (rendered.length <= maxChars) {
24
- return rendered;
25
- }
26
- try {
27
- return `${rendered.slice(0, maxChars - 15)}\n...[truncated]`;
28
- }
29
- catch {
30
- return rendered;
31
- }
32
- }
33
- function mapSingleFieldScalarArg(args, expectedKey, rawArgsInput) {
34
- if (expectedKey in args) {
35
- return args;
36
- }
37
- if (typeof rawArgsInput === "string") {
38
- const trimmed = rawArgsInput.trim();
39
- if (trimmed.length === 0) {
40
- return args;
41
- }
42
- return {
43
- ...args,
44
- [expectedKey]: trimmed,
45
- };
46
- }
47
- if (typeof rawArgsInput === "number" || typeof rawArgsInput === "boolean") {
48
- return {
49
- ...args,
50
- [expectedKey]: rawArgsInput,
51
- };
52
- }
53
- return args;
54
- }
55
- function isScalarLikeValue(value) {
56
- return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
57
- }
58
- function mapSingleRemainingScalarArg(args, expectedKey) {
59
- if (expectedKey in args) {
60
- return args;
61
- }
62
- const entries = Object.entries(args).filter(([, value]) => isScalarLikeValue(value));
63
- if (entries.length !== 1) {
64
- return args;
65
- }
66
- const [, value] = entries[0];
67
- if (typeof value === "string" && value.trim().length === 0) {
68
- return args;
69
- }
70
- return {
71
- ...args,
72
- [expectedKey]: value,
73
- };
74
- }
75
- function readSchemaDescription(schemaPart) {
76
- if (!isObject(schemaPart)) {
77
- return "";
78
- }
79
- const direct = schemaPart.description;
80
- if (typeof direct === "string") {
81
- return direct;
82
- }
83
- const def = schemaPart._def;
84
- if (typeof def?.description === "string") {
85
- return def.description;
86
- }
87
- return readSchemaDescription(def?.innerType);
88
- }
89
- function schemaPartExpectsString(schemaPart) {
90
- if (!isObject(schemaPart)) {
91
- return false;
92
- }
93
- if (schemaPart.type === "string") {
94
- return true;
95
- }
96
- const def = schemaPart._def ?? schemaPart.def;
97
- if (def?.typeName === "ZodString" || def?.type === "string") {
98
- return true;
99
- }
100
- return schemaPartExpectsString(def?.innerType);
101
- }
102
- function fillLatestUserInputForQueryLikeFields(args, shape, latestUserInput) {
103
- const userInput = typeof latestUserInput === "string" ? latestUserInput.trim() : "";
104
- if (!userInput) {
105
- return args;
106
- }
107
- let next = args;
108
- for (const [key, schemaPart] of Object.entries(shape)) {
109
- if (key in next) {
110
- continue;
111
- }
112
- const normalizedKey = key.trim().toLowerCase();
113
- const description = readSchemaDescription(schemaPart);
114
- const keyIsQueryLike = ["query", "question", "prompt", "input", "text"].includes(normalizedKey);
115
- const descriptionIsQueryLike = /\b(?:query|question|prompt|input|text)\b/iu.test(description);
116
- if ((!keyIsQueryLike && !descriptionIsQueryLike) || !schemaPartExpectsString(schemaPart)) {
117
- continue;
118
- }
119
- next = {
120
- ...next,
121
- [key]: userInput,
122
- };
123
- }
124
- return next;
125
- }
126
- function fillLatestUserInputForResourceArrayFields(args, shape, latestUserInput) {
127
- const userInput = typeof latestUserInput === "string" ? latestUserInput.trim() : "";
128
- if (!userInput || !hasExplicitResourceReference(userInput)) {
129
- return args;
130
- }
131
- const resourceRefs = extractExplicitResourceReferences(userInput);
132
- if (resourceRefs.length === 0) {
133
- return args;
134
- }
135
- let next = args;
136
- for (const [key, schemaPart] of Object.entries(shape)) {
137
- if (key in next || !schemaPartExpectsArray(schemaPart)) {
138
- continue;
139
- }
140
- const mapped = resourceRefs.map((ref) => inferDiscriminatedArrayObjectFromString(schemaPart, ref));
141
- if (mapped.length > 0 && mapped.every((item) => item !== null)) {
142
- next = { ...next, [key]: mapped };
143
- }
144
- }
145
- return next;
146
- }
147
- function mapCommonArgumentAliases(args, shape) {
148
- let next = args;
149
- if ("args" in shape && !("args" in next) && Array.isArray(next.argv)) {
150
- next = { ...next, args: next.argv };
151
- }
152
- if ("arguments" in shape && !("arguments" in next) && Array.isArray(next.args)) {
153
- next = { ...next, arguments: next.args };
154
- }
155
- return next;
156
- }
157
- function schemaPartExpectsArray(schemaPart) {
158
- if (!isObject(schemaPart)) {
159
- return false;
160
- }
161
- if (schemaPart.type === "array") {
162
- return true;
163
- }
164
- const def = schemaPart._def;
165
- if (def?.typeName === "ZodArray" || def?.type === "array") {
166
- return true;
167
- }
168
- return schemaPartExpectsArray(def?.innerType);
169
- }
170
- function readObjectShape(schemaPart) {
171
- if (!isObject(schemaPart)) {
172
- return undefined;
173
- }
174
- const def = schemaPart._def ?? schemaPart.def;
175
- if (!def) {
176
- return undefined;
177
- }
178
- const shape = typeof def.shape === "function" ? def.shape() : def.shape;
179
- return isRecord(shape) ? shape : readObjectShape(def.innerType);
180
- }
181
- function readLiteralValue(schemaPart) {
182
- if (!isObject(schemaPart)) {
183
- return undefined;
184
- }
185
- const def = schemaPart._def ?? schemaPart.def;
186
- if ((def?.typeName === "ZodLiteral" || def?.type === "literal") && typeof def.value === "string") {
187
- return def.value;
188
- }
189
- if ((def?.typeName === "ZodLiteral" || def?.type === "literal") && Array.isArray(def.values) && typeof def.values[0] === "string") {
190
- return def.values[0];
191
- }
192
- return readLiteralValue(def?.innerType);
193
- }
194
- function readArrayElementSchema(schemaPart) {
195
- if (!isObject(schemaPart)) {
196
- return undefined;
197
- }
198
- const def = schemaPart._def ?? schemaPart.def;
199
- if (def?.typeName === "ZodArray" || def?.type === "array") {
200
- return def.element ?? def.type;
201
- }
202
- return readArrayElementSchema(def?.innerType);
203
- }
204
- function inferDiscriminatedArrayObjectFromString(schemaPart, value) {
205
- const raw = value.trim();
206
- if (!raw) {
207
- return null;
208
- }
209
- const elementSchema = readArrayElementSchema(schemaPart);
210
- if (!isObject(elementSchema)) {
211
- return null;
212
- }
213
- const def = elementSchema._def ?? elementSchema.def;
214
- const options = Array.isArray(def?.options)
215
- ? def.options
216
- : Array.isArray(elementSchema.options)
217
- ? elementSchema.options
218
- : undefined;
219
- if (def?.typeName !== "ZodDiscriminatedUnion"
220
- && !(def?.type === "union" && typeof def.discriminator === "string")) {
221
- return null;
222
- }
223
- if (typeof def.discriminator !== "string" || !options) {
224
- return null;
225
- }
226
- const looksLikeUrl = /^https?:\/\/\S+$/iu.test(raw);
227
- for (const option of options) {
228
- const optionShape = readObjectShape(option);
229
- if (!optionShape) {
230
- continue;
231
- }
232
- const discriminatorValue = readLiteralValue(optionShape[def.discriminator]);
233
- if (!discriminatorValue) {
234
- continue;
235
- }
236
- if (looksLikeUrl && "url" in optionShape) {
237
- return { [def.discriminator]: discriminatorValue, url: raw };
238
- }
239
- const looksLikePath = /^(?:\.{1,2}\/|\/|~\/|[A-Za-z]:[\\/])|\\|\.[A-Za-z0-9]{1,12}$/u.test(raw);
240
- if (!looksLikeUrl && looksLikePath && "path" in optionShape && !raw.includes("\n")) {
241
- return { [def.discriminator]: discriminatorValue, path: raw };
242
- }
243
- if ("text" in optionShape) {
244
- return { [def.discriminator]: discriminatorValue, text: raw };
245
- }
246
- }
247
- return null;
248
- }
249
- function mapStringArrayFields(args, shape) {
250
- let next = args;
251
- for (const [key, schemaPart] of Object.entries(shape)) {
252
- const value = next[key];
253
- if (Array.isArray(value) && value.every((item) => typeof item === "string")) {
254
- const mapped = value.map((item) => inferDiscriminatedArrayObjectFromString(schemaPart, item));
255
- if (mapped.length > 0 && mapped.every((item) => item !== null)) {
256
- next = { ...next, [key]: mapped };
257
- }
258
- continue;
259
- }
260
- if (typeof value !== "string") {
261
- continue;
262
- }
263
- const raw = value.trim();
264
- if (!raw) {
265
- continue;
266
- }
267
- const looksLikeDelimitedList = /[,;\n]/u.test(raw) && /(?:s|list|items|areas|paths|files)$/iu.test(key);
268
- if (!schemaPartExpectsArray(schemaPart) && !looksLikeDelimitedList) {
269
- continue;
270
- }
271
- const items = raw
272
- .split(/[,;\n]/u)
273
- .map((item) => item.trim())
274
- .filter(Boolean);
275
- if (items.length > 0) {
276
- next = { ...next, [key]: items };
277
- }
278
- }
279
- return next;
280
- }
281
- function mapDelimitedListLikeArgs(args) {
282
- let next = args;
283
- for (const [key, value] of Object.entries(args)) {
284
- if (typeof value !== "string") {
285
- continue;
286
- }
287
- const raw = value.trim();
288
- if (!/[,;\n]/u.test(raw) || !/(?:s|list|items|areas|paths|files)$/iu.test(key)) {
289
- continue;
290
- }
291
- const items = raw.split(/[,;\n]/u).map((item) => item.trim()).filter(Boolean);
292
- if (items.length > 0) {
293
- next = { ...next, [key]: items };
294
- }
295
- }
296
- return next;
297
- }
298
- function stripArgsToShape(args, shape) {
299
- return Object.fromEntries(Object.entries(args).filter(([key]) => key in shape));
300
- }
301
- function dropDelimitedScalarPathArgs(args, shape) {
302
- let next = args;
303
- for (const [key, schemaPart] of Object.entries(shape)) {
304
- const value = next[key];
305
- if (typeof value !== "string") {
306
- continue;
307
- }
308
- const normalizedKey = key.trim().toLowerCase();
309
- if (!/(?:^path$|path$|^filepath$|^targetpath$)/u.test(normalizedKey)) {
310
- continue;
311
- }
312
- if (schemaPartExpectsArray(schemaPart)) {
313
- continue;
314
- }
315
- const raw = value.trim();
316
- if (!/[,;\n]/u.test(raw)) {
317
- continue;
318
- }
319
- const { [key]: _dropped, ...rest } = next;
320
- next = rest;
321
- }
322
- return next;
323
- }
324
- export function normalizeToolArgsForSchema(args, schema, rawArgsInput, options = {}) {
325
- const schemaDef = isObject(schema)
326
- ? (schema._def
327
- ?? schema.def)
328
- : undefined;
329
- const zodShape = schemaDef
330
- ? isRecord(schemaDef.shape)
331
- ? schemaDef.shape
332
- : typeof schemaDef.shape === "function"
333
- ? schemaDef.shape()
334
- : undefined
335
- : undefined;
336
- const jsonShape = isObject(schema) && isRecord(schema.properties)
337
- ? (schema.properties ?? undefined)
338
- : undefined;
339
- const plainShape = isObject(schema)
340
- && !("_def" in schema)
341
- && !("properties" in schema)
342
- && Object.values(schema).every((value) => isObject(value))
343
- ? schema
344
- : undefined;
345
- const shape = zodShape && isRecord(zodShape) ? zodShape : jsonShape ?? plainShape;
346
- if (!shape || !isRecord(shape)) {
347
- return mapDelimitedListLikeArgs(args);
348
- }
349
- const stripUnknownArgs = plainShape !== undefined && !zodShape && !jsonShape;
350
- const aliasMappedArgs = dropDelimitedScalarPathArgs(fillLatestUserInputForResourceArrayFields(mapStringArrayFields(mapCommonArgumentAliases(args, shape), shape), shape, options.latestUserInput), shape);
351
- const keys = Object.keys(shape);
352
- if (keys.length !== 1) {
353
- const filled = fillLatestUserInputForQueryLikeFields(aliasMappedArgs, shape, options.latestUserInput);
354
- return stripUnknownArgs ? stripArgsToShape(filled, shape) : filled;
355
- }
356
- const [expectedKey] = keys;
357
- if (expectedKey in aliasMappedArgs) {
358
- return stripUnknownArgs ? stripArgsToShape(aliasMappedArgs, shape) : aliasMappedArgs;
359
- }
360
- const scalarMappedArgs = mapSingleFieldScalarArg(aliasMappedArgs, expectedKey, rawArgsInput);
361
- if (expectedKey in scalarMappedArgs) {
362
- return stripUnknownArgs ? stripArgsToShape(scalarMappedArgs, shape) : scalarMappedArgs;
363
- }
364
- if (Object.keys(scalarMappedArgs).length === 0 && typeof options.latestUserInput === "string") {
365
- const userInput = options.latestUserInput.trim();
366
- const resourceRefs = extractExplicitResourceReferences(userInput);
367
- if (resourceRefs.length === 1 && hasExplicitResourceReference(userInput)) {
368
- const filled = {
369
- ...scalarMappedArgs,
370
- [expectedKey]: resourceRefs[0],
371
- };
372
- return stripUnknownArgs ? stripArgsToShape(filled, shape) : filled;
373
- }
374
- }
375
- const genericScalarMappedArgs = mapSingleRemainingScalarArg(scalarMappedArgs, expectedKey);
376
- if (expectedKey in genericScalarMappedArgs) {
377
- return stripUnknownArgs ? stripArgsToShape(genericScalarMappedArgs, shape) : genericScalarMappedArgs;
378
- }
379
- return stripUnknownArgs ? stripArgsToShape(genericScalarMappedArgs, shape) : genericScalarMappedArgs;
380
- }
381
- export function extractToolCallsFromResult(result) {
382
- const capturedToolCalls = readCapturedPromptedJsonToolCalls(result);
383
- if (capturedToolCalls.length > 0) {
384
- return [capturedToolCalls[capturedToolCalls.length - 1]];
385
- }
386
- const messages = isRecord(result) && Array.isArray(result.messages) ? result.messages : [];
387
- const answeredToolCallIds = new Set();
388
- for (const message of messages) {
389
- if (!isObject(message)) {
390
- continue;
391
- }
392
- const id = message.tool_call_id;
393
- if (typeof id === "string" && id.length > 0) {
394
- answeredToolCallIds.add(id);
395
- }
396
- }
397
- for (let index = messages.length - 1; index >= 0; index -= 1) {
398
- const message = messages[index];
399
- if (!isObject(message)) {
400
- continue;
401
- }
402
- const messageKwargs = isObject(message.kwargs) ? message.kwargs : undefined;
403
- const rawToolCalls = Array.isArray(message.tool_calls)
404
- ? (message.tool_calls ?? [])
405
- : Array.isArray(messageKwargs?.tool_calls)
406
- ? messageKwargs.tool_calls
407
- : [];
408
- const extracted = rawToolCalls
409
- .map((toolCall) => {
410
- if (!isObject(toolCall)) {
411
- return null;
412
- }
413
- const functionPayload = isObject(toolCall.function) ? toolCall.function : undefined;
414
- const name = typeof toolCall.name === "string"
415
- ? toolCall.name
416
- : typeof functionPayload?.name === "string"
417
- ? functionPayload.name
418
- : null;
419
- if (!name) {
420
- return null;
421
- }
422
- const rawArgsInput = toolCall.args ?? functionPayload?.arguments;
423
- const rawArgs = salvageToolArgs(rawArgsInput) ?? {};
424
- if (!isObject(rawArgs)) {
425
- return null;
426
- }
427
- const id = typeof toolCall.id === "string" ? toolCall.id : undefined;
428
- if (id && answeredToolCallIds.has(id)) {
429
- return null;
430
- }
431
- return { id, name, args: normalizeKnownToolArgs(name, rawArgs), rawArgsInput };
432
- })
433
- .filter((item) => item !== null);
434
- if (extracted.length > 0) {
435
- return extracted;
436
- }
437
- const directRole = typeof message.role === "string"
438
- ? message.role.trim().toLowerCase()
439
- : undefined;
440
- const messageType = typeof message._getType === "function"
441
- ? String(message._getType())
442
- : undefined;
443
- const constructorType = Array.isArray(message.id)
444
- ? message.id.at(-1)
445
- : undefined;
446
- if (directRole === "tool" || messageType === "tool" || constructorType === "ToolMessage") {
447
- continue;
448
- }
449
- const content = typeof message.content === "string"
450
- ? message.content
451
- : typeof messageKwargs?.content === "string"
452
- ? messageKwargs.content
453
- : "";
454
- if (!content.trim()) {
455
- continue;
456
- }
457
- const resultLabeledToolCall = salvageResultLabeledToolCall(content);
458
- if (resultLabeledToolCall) {
459
- return [{
460
- id: "salvaged-result-label-1",
461
- name: resultLabeledToolCall.name,
462
- args: resultLabeledToolCall.args,
463
- rawArgsInput: content,
464
- }];
465
- }
466
- const markdownNamedToolCall = salvageMarkdownNamedToolCall(content);
467
- if (markdownNamedToolCall) {
468
- return [{
469
- id: "salvaged-markdown-label-1",
470
- name: markdownNamedToolCall.name,
471
- args: markdownNamedToolCall.args,
472
- rawArgsInput: content,
473
- }];
474
- }
475
- const salvaged = salvageJsonToolCalls(content);
476
- if (salvaged.length > 0) {
477
- return salvaged.map((toolCall, salvageIndex) => ({
478
- id: `salvaged-json-${salvageIndex + 1}`,
479
- name: toolCall.name,
480
- args: toolCall.args,
481
- rawArgsInput: content,
482
- }));
483
- }
484
- }
485
- return [];
486
- }
1
+ import{salvageToolArgs as F}from"../../parsing/output-parsing.js";import{normalizeKnownToolArgs as N,salvageJsonToolCalls as Z,salvageMarkdownNamedToolCall as z,salvageResultLabeledToolCall as D}from"../../parsing/output-tool-args.js";import{isRecord as h}from"../../../utils/object.js";import{extractExplicitResourceReferences as L,hasExplicitResourceReference as _}from"../../harness/system/runtime-memory-policy.js";import{readCapturedPromptedJsonToolCalls as U}from"../model/prompted-json-tool-call-capture.js";function u(t){return h(t)}function ee(t){let e;if(typeof t=="string")e=t;else try{e=JSON.stringify(t)}catch{e=`${String(t)}`}const n=4e3;if(e.length<=n)return e;try{return`${e.slice(0,n-15)}
2
+ ...[truncated]`}catch{return e}}function E(t,e,n){if(e in t)return t;if(typeof n=="string"){const o=n.trim();return o.length===0?t:{...t,[e]:o}}return typeof n=="number"||typeof n=="boolean"?{...t,[e]:n}:t}function M(t){return typeof t=="string"||typeof t=="number"||typeof t=="boolean"}function q(t,e){if(e in t)return t;const n=Object.entries(t).filter(([,i])=>M(i));if(n.length!==1)return t;const[,o]=n[0];return typeof o=="string"&&o.trim().length===0?t:{...t,[e]:o}}function j(t){if(!u(t))return"";const e=t.description;if(typeof e=="string")return e;const n=t._def;return typeof n?.description=="string"?n.description:j(n?.innerType)}function O(t){if(!u(t))return!1;if(t.type==="string")return!0;const e=t._def??t.def;return e?.typeName==="ZodString"||e?.type==="string"?!0:O(e?.innerType)}function J(t,e,n){const o=typeof n=="string"?n.trim():"";if(!o)return t;let i=t;for(const[r,a]of Object.entries(e)){if(r in i)continue;const p=r.trim().toLowerCase(),s=j(a),f=["query","question","prompt","input","text"].includes(p),l=/\b(?:query|question|prompt|input|text)\b/iu.test(s);!f&&!l||!O(a)||(i={...i,[r]:o})}return i}function Q(t,e,n){const o=typeof n=="string"?n.trim():"";if(!o||!_(o))return t;const i=L(o);if(i.length===0)return t;let r=t;for(const[a,p]of Object.entries(e)){if(a in r||!S(p))continue;const s=i.map(f=>C(p,f));s.length>0&&s.every(f=>f!==null)&&(r={...r,[a]:s})}return r}function V(t,e){let n=t;return"args"in e&&!("args"in n)&&Array.isArray(n.argv)&&(n={...n,args:n.argv}),"arguments"in e&&!("arguments"in n)&&Array.isArray(n.args)&&(n={...n,arguments:n.args}),n}function S(t){if(!u(t))return!1;if(t.type==="array")return!0;const e=t._def;return e?.typeName==="ZodArray"||e?.type==="array"?!0:S(e?.innerType)}function R(t){if(!u(t))return;const e=t._def??t.def;if(!e)return;const n=typeof e.shape=="function"?e.shape():e.shape;return h(n)?n:R(e.innerType)}function $(t){if(!u(t))return;const e=t._def??t.def;return(e?.typeName==="ZodLiteral"||e?.type==="literal")&&typeof e.value=="string"?e.value:(e?.typeName==="ZodLiteral"||e?.type==="literal")&&Array.isArray(e.values)&&typeof e.values[0]=="string"?e.values[0]:$(e?.innerType)}function I(t){if(!u(t))return;const e=t._def??t.def;return e?.typeName==="ZodArray"||e?.type==="array"?e.element??e.type:I(e?.innerType)}function C(t,e){const n=e.trim();if(!n)return null;const o=I(t);if(!u(o))return null;const i=o._def??o.def,r=Array.isArray(i?.options)?i.options:Array.isArray(o.options)?o.options:void 0;if(i?.typeName!=="ZodDiscriminatedUnion"&&!(i?.type==="union"&&typeof i.discriminator=="string")||typeof i.discriminator!="string"||!r)return null;const a=/^https?:\/\/\S+$/iu.test(n);for(const p of r){const s=R(p);if(!s)continue;const f=$(s[i.discriminator]);if(!f)continue;if(a&&"url"in s)return{[i.discriminator]:f,url:n};const l=/^(?:\.{1,2}\/|\/|~\/|[A-Za-z]:[\\/])|\\|\.[A-Za-z0-9]{1,12}$/u.test(n);if(!a&&l&&"path"in s&&!n.includes(`
3
+ `))return{[i.discriminator]:f,path:n};if("text"in s)return{[i.discriminator]:f,text:n}}return null}function B(t,e){let n=t;for(const[o,i]of Object.entries(e)){const r=n[o];if(Array.isArray(r)&&r.every(f=>typeof f=="string")){const f=r.map(l=>C(i,l));f.length>0&&f.every(l=>l!==null)&&(n={...n,[o]:f});continue}if(typeof r!="string")continue;const a=r.trim();if(!a)continue;const p=/[,;\n]/u.test(a)&&/(?:s|list|items|areas|paths|files)$/iu.test(o);if(!S(i)&&!p)continue;const s=a.split(/[,;\n]/u).map(f=>f.trim()).filter(Boolean);s.length>0&&(n={...n,[o]:s})}return n}function K(t){let e=t;for(const[n,o]of Object.entries(t)){if(typeof o!="string")continue;const i=o.trim();if(!/[,;\n]/u.test(i)||!/(?:s|list|items|areas|paths|files)$/iu.test(n))continue;const r=i.split(/[,;\n]/u).map(a=>a.trim()).filter(Boolean);r.length>0&&(e={...e,[n]:r})}return e}function T(t,e){return Object.fromEntries(Object.entries(t).filter(([n])=>n in e))}function G(t,e){let n=t;for(const[o,i]of Object.entries(e)){const r=n[o];if(typeof r!="string")continue;const a=o.trim().toLowerCase();if(!/(?:^path$|path$|^filepath$|^targetpath$)/u.test(a)||S(i))continue;const p=r.trim();if(!/[,;\n]/u.test(p))continue;const{[o]:s,...f}=n;n=f}return n}function te(t,e,n,o={}){const i=u(e)?e._def??e.def:void 0,r=i?h(i.shape)?i.shape:typeof i.shape=="function"?i.shape():void 0:void 0,a=u(e)&&h(e.properties)?e.properties??void 0:void 0,p=u(e)&&!("_def"in e)&&!("properties"in e)&&Object.values(e).every(g=>u(g))?e:void 0,s=r&&h(r)?r:a??p;if(!s||!h(s))return K(t);const f=p!==void 0&&!r&&!a,l=G(Q(B(V(t,s),s),s,o.latestUserInput),s),k=Object.keys(s);if(k.length!==1){const g=J(l,s,o.latestUserInput);return f?T(g,s):g}const[d]=k;if(d in l)return f?T(l,s):l;const y=E(l,d,n);if(d in y)return f?T(y,s):y;if(Object.keys(y).length===0&&typeof o.latestUserInput=="string"){const g=o.latestUserInput.trim(),c=L(g);if(c.length===1&&_(g)){const A={...y,[d]:c[0]};return f?T(A,s):A}}const m=q(y,d);return d in m,f?T(m,s):m}function ne(t){const e=U(t);if(e.length>0)return[e[e.length-1]];const n=h(t)&&Array.isArray(t.messages)?t.messages:[],o=new Set;for(const i of n){if(!u(i))continue;const r=i.tool_call_id;typeof r=="string"&&r.length>0&&o.add(r)}for(let i=n.length-1;i>=0;i-=1){const r=n[i];if(!u(r))continue;const a=u(r.kwargs)?r.kwargs:void 0,s=(Array.isArray(r.tool_calls)?r.tool_calls??[]:Array.isArray(a?.tool_calls)?a.tool_calls:[]).map(c=>{if(!u(c))return null;const A=u(c.function)?c.function:void 0,b=typeof c.name=="string"?c.name:typeof A?.name=="string"?A.name:null;if(!b)return null;const w=c.args??A?.arguments,x=F(w)??{};if(!u(x))return null;const v=typeof c.id=="string"?c.id:void 0;return v&&o.has(v)?null:{id:v,name:b,args:N(b,x),rawArgsInput:w}}).filter(c=>c!==null);if(s.length>0)return s;const f=typeof r.role=="string"?r.role.trim().toLowerCase():void 0,l=typeof r._getType=="function"?String(r._getType()):void 0,k=Array.isArray(r.id)?r.id.at(-1):void 0;if(f==="tool"||l==="tool"||k==="ToolMessage")continue;const d=typeof r.content=="string"?r.content:typeof a?.content=="string"?a.content:"";if(!d.trim())continue;const y=D(d);if(y)return[{id:"salvaged-result-label-1",name:y.name,args:y.args,rawArgsInput:d}];const m=z(d);if(m)return[{id:"salvaged-markdown-label-1",name:m.name,args:m.args,rawArgsInput:d}];const g=Z(d);if(g.length>0)return g.map((c,A)=>({id:`salvaged-json-${A+1}`,name:c.name,args:c.args,rawArgsInput:d}))}return[]}export{ne as extractToolCallsFromResult,te as normalizeToolArgsForSchema,ee as stringifyToolOutput};