@rebon/cli-linux-x64 0.17.3 → 0.18.0

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 (82) hide show
  1. package/bin/compose-runtime/package.json +17 -0
  2. package/bin/compose-runtime/payload/compose/credentials-runtime.js +38 -0
  3. package/bin/compose-runtime/payload/compose/llm-runtime.js +330 -0
  4. package/bin/compose-runtime/payload/compose/loop-assembly.js +149 -0
  5. package/bin/compose-runtime/payload/compose/serve-dispatch.js +82 -0
  6. package/bin/compose-runtime/payload/compose/shims/dsh-anonymous-user-id.js +10 -0
  7. package/bin/compose-runtime/payload/compose/shims/dsh-credentials.js +13 -0
  8. package/bin/compose-runtime/payload/compose/shims/dsh-launch-environment.js +21 -0
  9. package/bin/compose-runtime/payload/compose/shims/dsh-llm.js +313 -0
  10. package/bin/compose-runtime/payload/compose/shims/dsh-settings.js +43 -0
  11. package/bin/compose-runtime/payload/compose/shims/dsh-tools.js +20 -0
  12. package/bin/compose-runtime/payload/compose/shims/dsh-web.js +10 -0
  13. package/bin/compose-runtime/payload/compose/shims/llm-adapter.js +32 -0
  14. package/bin/compose-runtime/payload/compose/shims/zod-lite.js +49 -0
  15. package/bin/compose-runtime/payload/compose/systemprompt-runtime.js +74 -0
  16. package/bin/compose-runtime/payload/compose/tools-runtime.js +228 -0
  17. package/bin/compose-runtime/payload/compose/web-runtime.js +204 -0
  18. package/bin/compose-runtime/payload/vendor/cordis/index.js +1530 -0
  19. package/bin/compose-runtime/payload/vendor/cosmokit/LICENSE +21 -0
  20. package/bin/compose-runtime/payload/vendor/cosmokit/index.mjs +357 -0
  21. package/bin/compose-runtime/payload/vendor/dsh/LICENSE-agent +21 -0
  22. package/bin/compose-runtime/payload/vendor/dsh/LICENSE-agent-loop +21 -0
  23. package/bin/compose-runtime/payload/vendor/dsh/LICENSE-llm-core +21 -0
  24. package/bin/compose-runtime/payload/vendor/dsh/LICENSE-llm-deepseek +21 -0
  25. package/bin/compose-runtime/payload/vendor/dsh/LICENSE-logger-console +21 -0
  26. package/bin/compose-runtime/payload/vendor/dsh/LICENSE-scope +21 -0
  27. package/bin/compose-runtime/payload/vendor/dsh/LICENSE-session +21 -0
  28. package/bin/compose-runtime/payload/vendor/dsh/LICENSE-system-prompt +21 -0
  29. package/bin/compose-runtime/payload/vendor/dsh/LICENSE-timeout +21 -0
  30. package/bin/compose-runtime/payload/vendor/dsh/LICENSE-timer +21 -0
  31. package/bin/compose-runtime/payload/vendor/dsh/LICENSE-tool-todo +21 -0
  32. package/bin/compose-runtime/payload/vendor/dsh/LICENSE-tool-web +21 -0
  33. package/bin/compose-runtime/payload/vendor/dsh/LICENSE-tools-schema +21 -0
  34. package/bin/compose-runtime/payload/vendor/dsh/LICENSE-web-search-exa +21 -0
  35. package/bin/compose-runtime/payload/vendor/dsh/agent-loop.js +1193 -0
  36. package/bin/compose-runtime/payload/vendor/dsh/agent.js +714 -0
  37. package/bin/compose-runtime/payload/vendor/dsh/llm-core.js +269 -0
  38. package/bin/compose-runtime/payload/vendor/dsh/llm-deepseek.js +672 -0
  39. package/bin/compose-runtime/payload/vendor/dsh/logger-console.js +83 -0
  40. package/bin/compose-runtime/payload/vendor/dsh/scope.js +287 -0
  41. package/bin/compose-runtime/payload/vendor/dsh/session.js +1668 -0
  42. package/bin/compose-runtime/payload/vendor/dsh/system-prompt.js +309 -0
  43. package/bin/compose-runtime/payload/vendor/dsh/timeout.js +100 -0
  44. package/bin/compose-runtime/payload/vendor/dsh/timer.js +128 -0
  45. package/bin/compose-runtime/payload/vendor/dsh/tool-todo.js +143 -0
  46. package/bin/compose-runtime/payload/vendor/dsh/tool-web.js +1527 -0
  47. package/bin/compose-runtime/payload/vendor/dsh/tools-schema.js +849 -0
  48. package/bin/compose-runtime/payload/vendor/dsh/web-search-exa.js +122 -0
  49. package/bin/compose-runtime/payload/vendor/eventsource-parser/LICENSE +21 -0
  50. package/bin/compose-runtime/payload/vendor/eventsource-parser/index.js +177 -0
  51. package/bin/compose-runtime/payload/vendor/eventsource-parser/stream.js +48 -0
  52. package/bin/compose-runtime/payload/vendor/schemastery/index.mjs +656 -0
  53. package/bin/compose-runtime/payload-manifests.json +48 -0
  54. package/bin/compose-runtime/src/bridge.mjs +142 -0
  55. package/bin/compose-runtime/src/credentials-runtime.mjs +50 -0
  56. package/bin/compose-runtime/src/eventsource-stream.mjs +48 -0
  57. package/bin/compose-runtime/src/index.mjs +7 -0
  58. package/bin/compose-runtime/src/llm-runtime.mjs +326 -0
  59. package/bin/compose-runtime/src/loader.mjs +78 -0
  60. package/bin/compose-runtime/src/loop-assembly.mjs +237 -0
  61. package/bin/compose-runtime/src/payload.mjs +49 -0
  62. package/bin/compose-runtime/src/plugin.mjs +59 -0
  63. package/bin/compose-runtime/src/realm.mjs +223 -0
  64. package/bin/compose-runtime/src/registry.mjs +191 -0
  65. package/bin/compose-runtime/src/resolve.mjs +115 -0
  66. package/bin/compose-runtime/src/systemprompt-runtime.mjs +67 -0
  67. package/bin/compose-runtime/src/tools-runtime.mjs +239 -0
  68. package/bin/compose-runtime/src/web-runtime.mjs +209 -0
  69. package/bin/plugin-host/package.json +11 -0
  70. package/bin/plugin-host/src/bridge.mjs +64 -0
  71. package/bin/plugin-host/src/cli.mjs +105 -0
  72. package/bin/plugin-host/src/framing.mjs +113 -0
  73. package/bin/plugin-host/src/host.mjs +624 -0
  74. package/bin/plugin-host/src/json.mjs +196 -0
  75. package/bin/plugin-host/src/lifecycle.mjs +114 -0
  76. package/bin/plugin-host/src/loader.mjs +142 -0
  77. package/bin/plugin-host/src/methods.mjs +256 -0
  78. package/bin/plugin-host/src/protocol.mjs +129 -0
  79. package/bin/plugin-host/src/sdk.mjs +7 -0
  80. package/bin/rebon +0 -0
  81. package/bin/rebon-boa-helper +0 -0
  82. package/package.json +4 -1
@@ -0,0 +1,849 @@
1
+ // packages/core/tools/src/schema.ts
2
+ import { HarnessError as HarnessError2 } from "@deepseek-ai/dsh-llm";
3
+
4
+ // packages/core/tools/src/json-schema.ts
5
+ import { assertNever, HarnessError } from "@deepseek-ai/dsh-llm";
6
+
7
+ // packages/core/session/src/json.ts
8
+ function hasIntrinsicConstructor(prototype, name) {
9
+ const descriptor = Object.getOwnPropertyDescriptor(prototype, "constructor");
10
+ const constructor = descriptor?.value;
11
+ if (typeof constructor !== "function") return false;
12
+ try {
13
+ return constructor.name === name && constructor.prototype === prototype && Function.prototype.toString.call(constructor) === `function ${name}() { [native code] }`;
14
+ } catch {
15
+ return false;
16
+ }
17
+ }
18
+ function isIntrinsicObjectPrototype(value) {
19
+ return Object.getPrototypeOf(value) === null && hasIntrinsicConstructor(value, "Object");
20
+ }
21
+ function hasPlainArrayPrototype(value) {
22
+ const prototype = Object.getPrototypeOf(value);
23
+ if (!Array.isArray(prototype) || !hasIntrinsicConstructor(prototype, "Array")) return false;
24
+ const objectPrototype = Object.getPrototypeOf(prototype);
25
+ return typeof objectPrototype === "object" && objectPrototype !== null && isIntrinsicObjectPrototype(objectPrototype);
26
+ }
27
+ function hasPlainObjectPrototype(value) {
28
+ const prototype = Object.getPrototypeOf(value);
29
+ return prototype === null || typeof prototype === "object" && isIntrinsicObjectPrototype(prototype);
30
+ }
31
+ function enumerableStringKeys(value) {
32
+ const keys = Reflect.ownKeys(value);
33
+ if (keys.some((key) => typeof key !== "string" || !Object.prototype.propertyIsEnumerable.call(value, key))) return void 0;
34
+ return keys;
35
+ }
36
+ function walkJsonValue(value, detach) {
37
+ const ancestors = /* @__PURE__ */ new Set();
38
+ let root;
39
+ const assign = (destination, item) => {
40
+ if (destination === void 0) return;
41
+ if (destination.kind === "root") {
42
+ root = item;
43
+ } else if (destination.kind === "array") {
44
+ destination.target[destination.index] = item;
45
+ } else {
46
+ Object.defineProperty(destination.target, destination.key, {
47
+ value: item,
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true
51
+ });
52
+ }
53
+ };
54
+ const tasks = [{
55
+ kind: "visit",
56
+ value,
57
+ ...detach ? { destination: { kind: "root" } } : {}
58
+ }];
59
+ for (let task = tasks.pop(); task !== void 0; task = tasks.pop()) {
60
+ if (task.kind === "leave") {
61
+ ancestors.delete(task.source);
62
+ continue;
63
+ }
64
+ if (task.kind === "array-item") {
65
+ if (!Object.prototype.hasOwnProperty.call(task.source, task.index)) return void 0;
66
+ tasks.push({
67
+ kind: "visit",
68
+ value: task.source[task.index],
69
+ ...task.target === void 0 ? {} : { destination: { kind: "array", target: task.target, index: task.index } }
70
+ });
71
+ continue;
72
+ }
73
+ if (task.kind === "object-property") {
74
+ tasks.push({
75
+ kind: "visit",
76
+ value: task.source[task.key],
77
+ ...task.target === void 0 ? {} : { destination: { kind: "object", target: task.target, key: task.key } }
78
+ });
79
+ continue;
80
+ }
81
+ const current = task.value;
82
+ if (current === null) {
83
+ assign(task.destination, null);
84
+ continue;
85
+ }
86
+ if (typeof current === "boolean" || typeof current === "string") {
87
+ assign(task.destination, current);
88
+ continue;
89
+ }
90
+ if (typeof current === "number") {
91
+ if (!Number.isFinite(current) || Object.is(current, -0)) return void 0;
92
+ assign(task.destination, current);
93
+ continue;
94
+ }
95
+ if (typeof current !== "object") return void 0;
96
+ if (ancestors.has(current)) return void 0;
97
+ if (Array.isArray(current)) {
98
+ if (!hasPlainArrayPrototype(current)) return void 0;
99
+ const length = current.length;
100
+ if (Reflect.ownKeys(current).length !== length + 1) return void 0;
101
+ const target2 = detach ? [] : void 0;
102
+ if (target2 !== void 0) assign(task.destination, target2);
103
+ ancestors.add(current);
104
+ tasks.push({ kind: "leave", source: current });
105
+ for (let index = length - 1; index >= 0; index--) {
106
+ tasks.push({ kind: "array-item", source: current, index, ...target2 === void 0 ? {} : { target: target2 } });
107
+ }
108
+ continue;
109
+ }
110
+ if (!hasPlainObjectPrototype(current)) return void 0;
111
+ const keys = enumerableStringKeys(current);
112
+ if (keys === void 0) return void 0;
113
+ const target = detach ? {} : void 0;
114
+ if (target !== void 0) assign(task.destination, target);
115
+ ancestors.add(current);
116
+ tasks.push({ kind: "leave", source: current });
117
+ for (let index = keys.length - 1; index >= 0; index--) {
118
+ const key = keys[index];
119
+ if (key === void 0) return void 0;
120
+ tasks.push({ kind: "object-property", source: current, key, ...target === void 0 ? {} : { target } });
121
+ }
122
+ }
123
+ return detach ? root : true;
124
+ }
125
+ function isJsonValue(value) {
126
+ return walkJsonValue(value, false) === true;
127
+ }
128
+
129
+ // packages/core/tools/src/json-schema.ts
130
+ var JsonSchemaError = class extends HarnessError {
131
+ /** Individual schema violations in walk order. */
132
+ violations;
133
+ constructor(violations) {
134
+ super(`unsupported JSON schema: ${violations.join("; ")}`, "UNSUPPORTED_SCHEMA");
135
+ this.name = "JsonSchemaError";
136
+ this.violations = violations;
137
+ }
138
+ };
139
+ var CONSTRAINT_KEYWORDS = /* @__PURE__ */ new Set([
140
+ "type",
141
+ "oneOf",
142
+ "properties",
143
+ "required",
144
+ "additionalProperties",
145
+ "items",
146
+ "enum",
147
+ "const"
148
+ ]);
149
+ var ANNOTATION_KEYWORDS = /* @__PURE__ */ new Set(["description", "title", "default", "examples"]);
150
+ var SCHEMA_TYPES = ["object", "array", "string", "number", "integer", "boolean", "null"];
151
+ function hasIntrinsicConstructor2(prototype, name) {
152
+ const descriptor = Object.getOwnPropertyDescriptor(prototype, "constructor");
153
+ const constructor = descriptor?.value;
154
+ if (typeof constructor !== "function") return false;
155
+ try {
156
+ return constructor.name === name && constructor.prototype === prototype && Function.prototype.toString.call(constructor) === `function ${name}() { [native code] }`;
157
+ } catch {
158
+ return false;
159
+ }
160
+ }
161
+ function isIntrinsicObjectPrototype2(value) {
162
+ return Object.getPrototypeOf(value) === null && hasIntrinsicConstructor2(value, "Object");
163
+ }
164
+ function isPlainJsonRecord(value) {
165
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
166
+ try {
167
+ const prototype = Object.getPrototypeOf(value);
168
+ return prototype === null || typeof prototype === "object" && isIntrinsicObjectPrototype2(prototype);
169
+ } catch {
170
+ return false;
171
+ }
172
+ }
173
+ function hasPlainArrayPrototype2(value) {
174
+ const prototype = Object.getPrototypeOf(value);
175
+ if (!Array.isArray(prototype) || !hasIntrinsicConstructor2(prototype, "Array")) return false;
176
+ const objectPrototype = Object.getPrototypeOf(prototype);
177
+ return typeof objectPrototype === "object" && objectPrototype !== null && isIntrinsicObjectPrototype2(objectPrototype);
178
+ }
179
+ function hasOnlyEnumerableStringKeys(value) {
180
+ try {
181
+ return Reflect.ownKeys(value).every((key) => typeof key === "string" && Object.prototype.propertyIsEnumerable.call(value, key));
182
+ } catch {
183
+ return false;
184
+ }
185
+ }
186
+ function isJsonSchemaRecord(value) {
187
+ return isPlainJsonRecord(value) && hasOnlyEnumerableStringKeys(value);
188
+ }
189
+ function isPlainJsonArray(value) {
190
+ if (!Array.isArray(value)) return false;
191
+ try {
192
+ if (!hasPlainArrayPrototype2(value) || Reflect.ownKeys(value).length !== value.length + 1) return false;
193
+ for (let index = 0; index < value.length; index++) {
194
+ if (!Object.hasOwn(value, index)) return false;
195
+ }
196
+ return true;
197
+ } catch {
198
+ return false;
199
+ }
200
+ }
201
+ function isJsonNumber(value) {
202
+ return typeof value === "number" && Number.isFinite(value) && !Object.is(value, -0);
203
+ }
204
+ function scalarMatches(type, value) {
205
+ switch (type) {
206
+ case "string":
207
+ return typeof value === "string";
208
+ case "number":
209
+ return isJsonNumber(value);
210
+ case "integer":
211
+ return isJsonNumber(value) && Number.isInteger(value);
212
+ case "boolean":
213
+ return typeof value === "boolean";
214
+ case "null":
215
+ return value === null;
216
+ /* v8 ignore next -- JsonSchemaScalarType is closed; this retains compile-time exhaustiveness. */
217
+ default:
218
+ return assertNever(type, "JsonSchemaType");
219
+ }
220
+ }
221
+ var ONE_OF_SIBLING_KEYWORDS = ["properties", "required", "additionalProperties", "items", "enum", "const"];
222
+ function checkObjectSchemaTail(node, path, properties, violations) {
223
+ const hasRequired = Object.hasOwn(node, "required");
224
+ const required = hasRequired ? node.required : void 0;
225
+ if (hasRequired) {
226
+ if (!isPlainJsonArray(required) || required.some((entry) => typeof entry !== "string")) {
227
+ violations.push(`${path}.required must be an array of strings`);
228
+ } else {
229
+ const declared = isJsonSchemaRecord(properties) ? properties : {};
230
+ for (const key of required) {
231
+ if (!Object.hasOwn(declared, key)) violations.push(`${path}.required names "${key}" which is not in properties`);
232
+ }
233
+ }
234
+ }
235
+ if (Object.hasOwn(node, "additionalProperties") && typeof node.additionalProperties !== "boolean") {
236
+ violations.push(`${path}.additionalProperties must be a boolean`);
237
+ }
238
+ }
239
+ function checkSchemaNode(root, rootPath, violations, seen) {
240
+ const tasks = [{ kind: "enter", node: root, path: rootPath }];
241
+ for (let task = tasks.pop(); task !== void 0; task = tasks.pop()) {
242
+ if (task.kind === "leave") {
243
+ seen.delete(task.node);
244
+ continue;
245
+ }
246
+ if (task.kind === "one-of-tail") {
247
+ for (const key of ONE_OF_SIBLING_KEYWORDS) {
248
+ if (Object.hasOwn(task.node, key)) violations.push(`${task.path}.${key} is not supported beside oneOf`);
249
+ }
250
+ continue;
251
+ }
252
+ if (task.kind === "object-tail") {
253
+ checkObjectSchemaTail(task.node, task.path, task.properties, violations);
254
+ continue;
255
+ }
256
+ const { node, path } = task;
257
+ if (!isJsonSchemaRecord(node)) {
258
+ violations.push(`${path} must be a schema object`);
259
+ continue;
260
+ }
261
+ if (seen.has(node)) {
262
+ violations.push(`${path} is circular`);
263
+ continue;
264
+ }
265
+ seen.add(node);
266
+ tasks.push({ kind: "leave", node });
267
+ for (const key of Object.keys(node)) {
268
+ if (CONSTRAINT_KEYWORDS.has(key)) continue;
269
+ if (ANNOTATION_KEYWORDS.has(key)) {
270
+ try {
271
+ if (!isJsonValue(node[key])) violations.push(`${path}.${key} annotation must be lossless JSON data`);
272
+ } catch {
273
+ violations.push(`${path}.${key} annotation must be lossless JSON data`);
274
+ }
275
+ continue;
276
+ }
277
+ violations.push(`${path}.${key} is not a supported keyword (subset: type/oneOf/properties/required/additionalProperties/items/enum/const + annotations)`);
278
+ }
279
+ if (Object.hasOwn(node, "description") && typeof node.description !== "string") {
280
+ violations.push(`${path}.description must be a string`);
281
+ }
282
+ if (Object.hasOwn(node, "title") && typeof node.title !== "string") {
283
+ violations.push(`${path}.title must be a string`);
284
+ }
285
+ const hasType = Object.hasOwn(node, "type");
286
+ const hasOneOf = Object.hasOwn(node, "oneOf");
287
+ if (hasType && hasOneOf) {
288
+ violations.push(`${path} cannot declare both type and oneOf`);
289
+ continue;
290
+ }
291
+ if (!hasType && !hasOneOf) {
292
+ for (const key of ONE_OF_SIBLING_KEYWORDS) {
293
+ if (Object.hasOwn(node, key)) violations.push(`${path}.${key} requires type or oneOf`);
294
+ }
295
+ continue;
296
+ }
297
+ if (hasOneOf) {
298
+ const oneOf = node.oneOf;
299
+ tasks.push({ kind: "one-of-tail", node, path });
300
+ if (!isPlainJsonArray(oneOf) || oneOf.length < 2) {
301
+ violations.push(`${path}.oneOf must be an array of at least two schemas`);
302
+ } else {
303
+ for (let index = oneOf.length - 1; index >= 0; index--) {
304
+ tasks.push({ kind: "enter", node: oneOf[index], path: `${path}.oneOf[${index}]` });
305
+ }
306
+ }
307
+ continue;
308
+ }
309
+ const type = node.type;
310
+ if (typeof type !== "string" || !SCHEMA_TYPES.includes(type)) {
311
+ violations.push(Array.isArray(type) ? `${path}.type must be a single type string (type arrays are not supported)` : `${path}.type must be one of ${SCHEMA_TYPES.join("/")}`);
312
+ continue;
313
+ }
314
+ const schemaType = type;
315
+ const allowedFor = {
316
+ properties: ["object"],
317
+ required: ["object"],
318
+ additionalProperties: ["object"],
319
+ items: ["array"],
320
+ enum: ["string", "number", "integer", "boolean", "null"],
321
+ const: ["string", "number", "integer", "boolean", "null"]
322
+ };
323
+ for (const [key, types] of Object.entries(allowedFor)) {
324
+ if (Object.hasOwn(node, key) && !types.includes(schemaType)) {
325
+ violations.push(`${path}.${key} is not supported on type "${schemaType}"`);
326
+ }
327
+ }
328
+ switch (schemaType) {
329
+ case "object": {
330
+ const properties = Object.hasOwn(node, "properties") ? node.properties : void 0;
331
+ tasks.push({ kind: "object-tail", node, path, properties });
332
+ if (Object.hasOwn(node, "properties")) {
333
+ if (!isJsonSchemaRecord(properties)) {
334
+ violations.push(`${path}.properties must be an object of schemas`);
335
+ } else {
336
+ const entries = Object.entries(properties);
337
+ for (let index = entries.length - 1; index >= 0; index--) {
338
+ const entry = entries[index];
339
+ if (entry === void 0) continue;
340
+ tasks.push({ kind: "enter", node: entry[1], path: `${path}.properties.${entry[0]}` });
341
+ }
342
+ }
343
+ }
344
+ break;
345
+ }
346
+ case "array": {
347
+ if (Object.hasOwn(node, "items")) tasks.push({ kind: "enter", node: node.items, path: `${path}.items` });
348
+ break;
349
+ }
350
+ case "string":
351
+ case "number":
352
+ case "integer":
353
+ case "boolean":
354
+ case "null": {
355
+ const hasEnum = Object.hasOwn(node, "enum");
356
+ const allowed = hasEnum ? node.enum : void 0;
357
+ const enumValid = isPlainJsonArray(allowed) && allowed.length > 0 && allowed.every((entry) => scalarMatches(schemaType, entry));
358
+ if (hasEnum && !enumValid) {
359
+ violations.push(`${path}.enum must be a non-empty array of ${schemaType} values`);
360
+ }
361
+ const hasConst = Object.hasOwn(node, "const");
362
+ const declaredConst = hasConst ? node.const : void 0;
363
+ const constValid = scalarMatches(schemaType, declaredConst);
364
+ if (hasConst) {
365
+ if (!constValid) {
366
+ violations.push(`${path}.const must be a ${schemaType} value`);
367
+ } else if (enumValid && !allowed.includes(declaredConst)) {
368
+ violations.push(`${path}.const must be one of ${path}.enum when both are declared`);
369
+ }
370
+ }
371
+ break;
372
+ }
373
+ /* v8 ignore next -- schemaType was narrowed from the closed SCHEMA_TYPES table above. */
374
+ default:
375
+ assertNever(schemaType, "JsonSchemaType");
376
+ }
377
+ }
378
+ }
379
+ function assertSupportedJsonSchema(schema) {
380
+ const violations = [];
381
+ checkSchemaNode(schema, "schema", violations, /* @__PURE__ */ new Set());
382
+ if (violations.length > 0) throw new JsonSchemaError(violations);
383
+ }
384
+ function safelyIsJsonValue(value) {
385
+ try {
386
+ return isJsonValue(value);
387
+ } catch {
388
+ return false;
389
+ }
390
+ }
391
+ function diagnosticPath(path) {
392
+ return path === "" ? "arguments" : path;
393
+ }
394
+ function propertyPath(path, key) {
395
+ return path === "" ? key : `${path}.${key}`;
396
+ }
397
+ function losslessValueViolation(path) {
398
+ return [`"${diagnosticPath(path)}" must be a lossless JSON value`];
399
+ }
400
+ function appendViolations(target, source) {
401
+ for (const violation of source) target.push(violation);
402
+ }
403
+ function valueFrame(node, value, path) {
404
+ return {
405
+ node,
406
+ value,
407
+ path,
408
+ catches: false,
409
+ phase: "start",
410
+ children: [],
411
+ childIndex: 0,
412
+ violations: [],
413
+ tailViolations: [],
414
+ matches: 0
415
+ };
416
+ }
417
+ function checkScalarValue(node, value, path) {
418
+ const allowed = Object.hasOwn(node, "enum") ? node.enum : void 0;
419
+ if (allowed !== void 0 && !allowed.includes(value)) {
420
+ return [`"${diagnosticPath(path)}" must be one of ${JSON.stringify(allowed)}`];
421
+ }
422
+ if (Object.hasOwn(node, "const") && value !== node.const) {
423
+ return [`"${diagnosticPath(path)}" must be ${JSON.stringify(node.const)}`];
424
+ }
425
+ return [];
426
+ }
427
+ function checkValue(schema, value, path) {
428
+ const frames = [valueFrame(schema, value, path)];
429
+ let rootResult;
430
+ const receive = (result) => {
431
+ const parent = frames.at(-1);
432
+ if (parent === void 0) {
433
+ rootResult = result;
434
+ return;
435
+ }
436
+ if (parent.kind === "oneOf") {
437
+ if (result.length === 0) parent.matches++;
438
+ } else {
439
+ appendViolations(parent.violations, result);
440
+ }
441
+ };
442
+ const finish = (result) => {
443
+ frames.pop();
444
+ receive(result);
445
+ };
446
+ while (frames.length > 0) {
447
+ const frame = frames.at(-1);
448
+ if (frame === void 0) break;
449
+ try {
450
+ if (frame.phase === "children") {
451
+ if (frame.childIndex < frame.children.length) {
452
+ const child = frame.children[frame.childIndex];
453
+ if (child === void 0) throw new Error("missing schema-value child frame");
454
+ frame.childIndex++;
455
+ frames.push(valueFrame(child.node, child.value, child.path));
456
+ continue;
457
+ }
458
+ if (frame.kind === "oneOf") {
459
+ finish(frame.matches === 1 ? [] : [`"${diagnosticPath(frame.path)}" must match exactly one oneOf branch (matched ${frame.matches})`]);
460
+ continue;
461
+ }
462
+ appendViolations(frame.violations, frame.tailViolations);
463
+ if (frame.violations.length > 0) {
464
+ finish(frame.violations);
465
+ } else if (frame.kind === "object") {
466
+ finish(safelyIsJsonValue(frame.value) ? [] : [`"${diagnosticPath(frame.path)}" must be a lossless JSON object`]);
467
+ } else {
468
+ finish(safelyIsJsonValue(frame.value) ? [] : [`"${diagnosticPath(frame.path)}" must be a dense lossless JSON array`]);
469
+ }
470
+ continue;
471
+ }
472
+ const nodeType = Object.hasOwn(frame.node, "type") ? frame.node.type : void 0;
473
+ frame.catches = !(nodeType !== void 0 && !SCHEMA_TYPES.includes(nodeType));
474
+ const oneOf = Object.hasOwn(frame.node, "oneOf") ? frame.node.oneOf : void 0;
475
+ if (oneOf !== void 0) {
476
+ frame.kind = "oneOf";
477
+ frame.children = Array.from(oneOf, (branch) => ({ node: branch, value: frame.value, path: frame.path }));
478
+ frame.childIndex = 0;
479
+ frame.matches = 0;
480
+ frame.phase = "children";
481
+ continue;
482
+ }
483
+ if (nodeType === void 0) {
484
+ finish(safelyIsJsonValue(frame.value) ? [] : losslessValueViolation(frame.path));
485
+ continue;
486
+ }
487
+ switch (nodeType) {
488
+ case "object": {
489
+ if (!isPlainJsonRecord(frame.value)) {
490
+ finish([`"${diagnosticPath(frame.path)}" must be an object`]);
491
+ break;
492
+ }
493
+ const properties = Object.hasOwn(frame.node, "properties") ? frame.node.properties ?? {} : {};
494
+ const violations = [];
495
+ const required = Object.hasOwn(frame.node, "required") ? frame.node.required ?? [] : [];
496
+ for (const key of required) {
497
+ if (!Object.hasOwn(frame.value, key) || frame.value[key] === void 0) {
498
+ violations.push(`missing required property "${propertyPath(frame.path, key)}"`);
499
+ }
500
+ }
501
+ const children = [];
502
+ for (const [key, child] of Object.entries(properties)) {
503
+ if (!Object.hasOwn(frame.value, key) || frame.value[key] === void 0) continue;
504
+ children.push({ node: child, value: frame.value[key], path: propertyPath(frame.path, key) });
505
+ }
506
+ const tailViolations = [];
507
+ if (Object.hasOwn(frame.node, "additionalProperties") && frame.node.additionalProperties === false) {
508
+ for (const key of Object.keys(frame.value)) {
509
+ if (!Object.hasOwn(properties, key)) {
510
+ tailViolations.push(`"${propertyPath(frame.path, key)}" is not a declared property (additionalProperties: false)`);
511
+ }
512
+ }
513
+ }
514
+ frame.kind = "object";
515
+ frame.children = children;
516
+ frame.childIndex = 0;
517
+ frame.violations = violations;
518
+ frame.tailViolations = tailViolations;
519
+ frame.phase = "children";
520
+ break;
521
+ }
522
+ case "array": {
523
+ if (!Array.isArray(frame.value)) {
524
+ finish([`"${diagnosticPath(frame.path)}" must be an array`]);
525
+ break;
526
+ }
527
+ const items = Object.hasOwn(frame.node, "items") ? frame.node.items : void 0;
528
+ const children = items === void 0 ? [] : frame.value.flatMap((entry, index) => [{ node: items, value: entry, path: `${frame.path}[${index}]` }]);
529
+ frame.kind = "array";
530
+ frame.children = children;
531
+ frame.childIndex = 0;
532
+ frame.violations = [];
533
+ frame.phase = "children";
534
+ break;
535
+ }
536
+ case "string":
537
+ finish(typeof frame.value === "string" ? checkScalarValue(frame.node, frame.value, frame.path) : [`"${diagnosticPath(frame.path)}" must be a string`]);
538
+ break;
539
+ case "number":
540
+ finish(typeof frame.value !== "number" ? [`"${diagnosticPath(frame.path)}" must be a number`] : !isJsonNumber(frame.value) ? [`"${diagnosticPath(frame.path)}" must be a finite JSON number`] : checkScalarValue(frame.node, frame.value, frame.path));
541
+ break;
542
+ case "integer":
543
+ finish(!isJsonNumber(frame.value) || !Number.isInteger(frame.value) ? [`"${diagnosticPath(frame.path)}" must be an integer`] : checkScalarValue(frame.node, frame.value, frame.path));
544
+ break;
545
+ case "boolean":
546
+ finish(typeof frame.value === "boolean" ? checkScalarValue(frame.node, frame.value, frame.path) : [`"${diagnosticPath(frame.path)}" must be a boolean`]);
547
+ break;
548
+ case "null":
549
+ finish(frame.value === null ? checkScalarValue(frame.node, frame.value, frame.path) : [`"${diagnosticPath(frame.path)}" must be null`]);
550
+ break;
551
+ default:
552
+ finish(assertNever(nodeType, "JsonSchemaType"));
553
+ }
554
+ } catch (error) {
555
+ let failed = frames.pop();
556
+ while (failed !== void 0 && !failed.catches) failed = frames.pop();
557
+ if (failed === void 0) throw error;
558
+ receive(losslessValueViolation(failed.path));
559
+ }
560
+ }
561
+ return rootResult ?? losslessValueViolation(path);
562
+ }
563
+ function validateJsonSchemaValue(schema, value, path = "value") {
564
+ return checkValue(schema, value, path);
565
+ }
566
+
567
+ // packages/core/tools/src/schema.ts
568
+ var ANNOTATION_KEYS = ["description", "title", "default", "examples"];
569
+ function authorError(message) {
570
+ throw new JsonSchemaError([message]);
571
+ }
572
+ function copyAnnotations(source, target) {
573
+ if (Object.hasOwn(source, "description")) target.description = source.description;
574
+ if (Object.hasOwn(source, "title")) target.title = source.title;
575
+ if (Object.hasOwn(source, "default")) target.default = source.default;
576
+ if (Object.hasOwn(source, "examples")) target.examples = source.examples;
577
+ }
578
+ function assertAuthorKeys(source, path, allowed) {
579
+ for (const key of Object.keys(source)) {
580
+ if (!allowed.includes(key)) authorError(`${path}.${key} is not supported by the value schema DSL`);
581
+ }
582
+ }
583
+ function assignCompiledNode(destination, node) {
584
+ switch (destination.kind) {
585
+ case "root":
586
+ destination.holder.value = node;
587
+ break;
588
+ case "property":
589
+ Object.defineProperty(destination.target, destination.key, {
590
+ value: node,
591
+ enumerable: true,
592
+ configurable: true,
593
+ writable: true
594
+ });
595
+ break;
596
+ case "item":
597
+ destination.target.items = node;
598
+ break;
599
+ case "one-of":
600
+ destination.target[destination.index] = node;
601
+ break;
602
+ }
603
+ }
604
+ function assignCompiledPropertyMap(destination, compiled) {
605
+ if (destination.kind === "root") {
606
+ destination.holder.value = compiled;
607
+ } else {
608
+ destination.target.properties = compiled.properties;
609
+ }
610
+ }
611
+ function runSchemaCompiler(initial) {
612
+ const seen = /* @__PURE__ */ new Set();
613
+ const tasks = [initial];
614
+ for (let task = tasks.pop(); task !== void 0; task = tasks.pop()) {
615
+ if (task.kind === "leave") {
616
+ seen.delete(task.input);
617
+ continue;
618
+ }
619
+ if (task.kind === "property-map-tail") {
620
+ if (task.required.length > 0) {
621
+ task.compiled.required = task.required;
622
+ if (task.destination.kind === "object") task.destination.target.required = task.required;
623
+ }
624
+ continue;
625
+ }
626
+ if (task.kind === "property") {
627
+ if (!isJsonSchemaRecord(task.property)) authorError(`${task.path} must be a value schema object`);
628
+ if (Object.hasOwn(task.property, "required") && task.property.required !== true) {
629
+ authorError(`${task.path}.required must be true when present`);
630
+ }
631
+ if (Object.hasOwn(task.property, "required") && task.property.required === true) task.required.push(task.key);
632
+ tasks.push({
633
+ kind: "value",
634
+ input: task.property,
635
+ path: task.path,
636
+ allowRequired: true,
637
+ destination: { kind: "property", target: task.properties, key: task.key }
638
+ });
639
+ continue;
640
+ }
641
+ if (task.kind === "property-map") {
642
+ if (!isJsonSchemaRecord(task.input)) authorError(`${task.path} must be an object of value schemas`);
643
+ if (seen.has(task.input)) authorError(`${task.path} is circular`);
644
+ seen.add(task.input);
645
+ const compiled = { properties: {} };
646
+ const required = [];
647
+ assignCompiledPropertyMap(task.destination, compiled);
648
+ tasks.push({ kind: "leave", input: task.input });
649
+ tasks.push({ kind: "property-map-tail", compiled, required, destination: task.destination });
650
+ const entries = Object.entries(task.input);
651
+ for (let index = entries.length - 1; index >= 0; index--) {
652
+ const entry = entries[index];
653
+ if (entry === void 0) continue;
654
+ tasks.push({
655
+ kind: "property",
656
+ property: entry[1],
657
+ path: `${task.path}.${entry[0]}`,
658
+ key: entry[0],
659
+ properties: compiled.properties,
660
+ required
661
+ });
662
+ }
663
+ continue;
664
+ }
665
+ const { input, path } = task;
666
+ if (!isJsonSchemaRecord(input)) authorError(`${path} must be a value schema object`);
667
+ if (seen.has(input)) authorError(`${path} is circular`);
668
+ seen.add(input);
669
+ const authorKeys = [...ANNOTATION_KEYS, ...task.allowRequired ? ["required"] : []];
670
+ const node = {};
671
+ assignCompiledNode(task.destination, node);
672
+ tasks.push({ kind: "leave", input });
673
+ if (Object.hasOwn(input, "oneOf")) {
674
+ assertAuthorKeys(input, path, [...authorKeys, "oneOf", "type"]);
675
+ if (Object.hasOwn(input, "type")) authorError(`${path} cannot declare both type and oneOf`);
676
+ if (!isPlainJsonArray(input.oneOf)) authorError(`${path}.oneOf must be an array of at least two value schemas`);
677
+ const branches = [];
678
+ node.oneOf = branches;
679
+ copyAnnotations(input, node);
680
+ for (let index = input.oneOf.length - 1; index >= 0; index--) {
681
+ tasks.push({
682
+ kind: "value",
683
+ input: input.oneOf[index],
684
+ path: `${path}.oneOf[${index}]`,
685
+ allowRequired: false,
686
+ destination: { kind: "one-of", target: branches, index }
687
+ });
688
+ }
689
+ continue;
690
+ }
691
+ const inputType = Object.hasOwn(input, "type") ? input.type : void 0;
692
+ switch (inputType) {
693
+ case "json":
694
+ assertAuthorKeys(input, path, [...authorKeys, "type"]);
695
+ copyAnnotations(input, node);
696
+ break;
697
+ case "object":
698
+ assertAuthorKeys(input, path, [...authorKeys, "type", "properties", "additionalProperties"]);
699
+ if (!Object.hasOwn(input, "additionalProperties") || typeof input.additionalProperties !== "boolean") {
700
+ authorError(`${path}.additionalProperties must be explicitly true or false`);
701
+ }
702
+ node.type = "object";
703
+ copyAnnotations(input, node);
704
+ node.additionalProperties = input.additionalProperties;
705
+ if (Object.hasOwn(input, "properties")) {
706
+ tasks.push({
707
+ kind: "property-map",
708
+ input: input.properties,
709
+ path: `${path}.properties`,
710
+ destination: { kind: "object", target: node }
711
+ });
712
+ }
713
+ break;
714
+ case "array":
715
+ assertAuthorKeys(input, path, [...authorKeys, "type", "items"]);
716
+ node.type = "array";
717
+ copyAnnotations(input, node);
718
+ if (Object.hasOwn(input, "items")) {
719
+ tasks.push({
720
+ kind: "value",
721
+ input: input.items,
722
+ path: `${path}.items`,
723
+ allowRequired: false,
724
+ destination: { kind: "item", target: node }
725
+ });
726
+ }
727
+ break;
728
+ case "string":
729
+ case "number":
730
+ case "integer":
731
+ case "boolean":
732
+ case "null":
733
+ assertAuthorKeys(input, path, [...authorKeys, "type", "enum", "const"]);
734
+ node.type = inputType;
735
+ copyAnnotations(input, node);
736
+ if (Object.hasOwn(input, "enum")) {
737
+ if (!isPlainJsonArray(input.enum)) authorError(`${path}.enum must be a non-empty array of scalar values`);
738
+ node.enum = Array.from(input.enum, (entry) => entry);
739
+ }
740
+ if (Object.hasOwn(input, "const")) node.const = input.const;
741
+ break;
742
+ default:
743
+ authorError(`${path}.type must be string/number/integer/boolean/null/array/object/json, or use oneOf`);
744
+ }
745
+ }
746
+ }
747
+ function compilePropertyMap(input, path) {
748
+ const holder = {};
749
+ runSchemaCompiler({ kind: "property-map", input, path, destination: { kind: "root", holder } });
750
+ return holder.value ?? authorError(`${path} did not compile`);
751
+ }
752
+ function compileValueSchema(input, path) {
753
+ const holder = {};
754
+ runSchemaCompiler({ kind: "value", input, path, allowRequired: false, destination: { kind: "root", holder } });
755
+ return holder.value ?? authorError(`${path} did not compile`);
756
+ }
757
+ function valueSchemaSpecToJsonSchema(spec) {
758
+ const schema = compileValueSchema(spec, "schema");
759
+ assertSupportedJsonSchema(schema);
760
+ return schema;
761
+ }
762
+ function parameterSchemaSpecToJsonSchema(spec) {
763
+ const compiled = compilePropertyMap(spec, "parameters");
764
+ const schema = {
765
+ type: "object",
766
+ properties: compiled.properties,
767
+ ...compiled.required === void 0 ? {} : { required: compiled.required }
768
+ };
769
+ assertSupportedJsonSchema(schema);
770
+ return schema;
771
+ }
772
+ var ToolArgsError = class extends HarnessError2 {
773
+ /** Individual violations in schema-walk order. */
774
+ violations;
775
+ constructor(violations) {
776
+ super(`invalid arguments: ${violations.join("; ")}`, "INVALID_ARGS");
777
+ this.name = "ToolArgsError";
778
+ this.violations = violations;
779
+ }
780
+ };
781
+ function validateArgs(spec, args) {
782
+ return validateJsonSchemaValue(parameterSchemaSpecToJsonSchema(spec), args, "");
783
+ }
784
+ function defineTool(options) {
785
+ const userExecute = options.execute;
786
+ const userFinalizeContent = options.finalizeContent;
787
+ const userRender = options.output.render;
788
+ const userPresentationMeta = options.output.presentationMeta;
789
+ const userPresentCall = options.presentCall;
790
+ const userPresentResult = options.presentResult;
791
+ const userIsConcurrencySafe = options.isConcurrencySafe;
792
+ if (options.timeoutMs !== void 0 && (!Number.isFinite(options.timeoutMs) || options.timeoutMs <= 0)) {
793
+ throw new Error(`defineTool(${options.name}): timeoutMs must be a positive finite number`);
794
+ }
795
+ const parameters = parameterSchemaSpecToJsonSchema(options.parameters);
796
+ const outputSchema = valueSchemaSpecToJsonSchema(options.output.schema);
797
+ const validate = (args) => validateJsonSchemaValue(parameters, args, "");
798
+ const tool = {
799
+ name: options.name,
800
+ description: options.description,
801
+ parameters,
802
+ output: {
803
+ schema: outputSchema,
804
+ render(args, value) {
805
+ return userRender(args, value);
806
+ },
807
+ ...userPresentationMeta !== void 0 ? {
808
+ presentationMeta(args, value) {
809
+ return userPresentationMeta(args, value);
810
+ }
811
+ } : {}
812
+ },
813
+ ...options.timeoutMs !== void 0 ? { timeoutMs: options.timeoutMs } : {},
814
+ async execute(args, exec) {
815
+ const violations = validate(args);
816
+ if (violations.length > 0) throw new ToolArgsError(violations);
817
+ return userExecute(args, exec);
818
+ }
819
+ };
820
+ if (userFinalizeContent) {
821
+ tool.finalizeContent = (exec, result) => userFinalizeContent(exec, result);
822
+ }
823
+ if (userPresentCall) {
824
+ tool.presentCall = (args) => {
825
+ if (validate(args).length > 0) return void 0;
826
+ return userPresentCall(args);
827
+ };
828
+ }
829
+ if (userPresentResult) {
830
+ tool.presentResult = (args, result) => {
831
+ if (validate(args).length > 0) return void 0;
832
+ return userPresentResult(args, result);
833
+ };
834
+ }
835
+ if (userIsConcurrencySafe) {
836
+ tool.isConcurrencySafe = (args) => {
837
+ if (validate(args).length > 0) return false;
838
+ return userIsConcurrencySafe(args);
839
+ };
840
+ }
841
+ return tool;
842
+ }
843
+ export {
844
+ ToolArgsError,
845
+ defineTool,
846
+ parameterSchemaSpecToJsonSchema,
847
+ validateArgs,
848
+ valueSchemaSpecToJsonSchema
849
+ };