@theokit/sdk 2.13.0 → 2.14.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.
package/dist/index.cjs CHANGED
@@ -13427,6 +13427,115 @@ function toOllamaTools(tools) {
13427
13427
  }
13428
13428
  }));
13429
13429
  }
13430
+ var cachedJsonrepair;
13431
+ function loadJsonrepair() {
13432
+ if (cachedJsonrepair === void 0) {
13433
+ const req = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
13434
+ cachedJsonrepair = req("jsonrepair").jsonrepair;
13435
+ }
13436
+ return cachedJsonrepair;
13437
+ }
13438
+ function isPlainObject(v) {
13439
+ return v !== null && typeof v === "object" && !Array.isArray(v);
13440
+ }
13441
+ function toFiniteNumber(raw) {
13442
+ if (raw === "") return void 0;
13443
+ const n = Number(raw);
13444
+ return Number.isFinite(n) && String(n) === raw ? n : void 0;
13445
+ }
13446
+ function tryJson(raw, repair) {
13447
+ const t = raw.trimStart();
13448
+ if (!(t.startsWith("{") || t.startsWith("["))) return void 0;
13449
+ try {
13450
+ return JSON.parse(repair ? loadJsonrepair()(t) : t);
13451
+ } catch {
13452
+ return void 0;
13453
+ }
13454
+ }
13455
+ function heuristicCoerce(raw, repairJson) {
13456
+ if (raw === "true") return true;
13457
+ if (raw === "false") return false;
13458
+ if (raw === "null") return null;
13459
+ const n = toFiniteNumber(raw);
13460
+ if (n !== void 0) return n;
13461
+ const json = tryJson(raw, false) ?? (repairJson ? tryJson(raw, true) : void 0);
13462
+ return json === void 0 ? raw : json;
13463
+ }
13464
+ function coerceCandidates(raw, repairJson) {
13465
+ const out = [];
13466
+ if (raw === "true") out.push(true);
13467
+ else if (raw === "false") out.push(false);
13468
+ else if (raw === "null") out.push(null);
13469
+ const n = toFiniteNumber(raw);
13470
+ if (n !== void 0) out.push(n);
13471
+ const json = tryJson(raw, false) ?? (repairJson ? tryJson(raw, true) : void 0);
13472
+ if (json !== void 0) out.push(json);
13473
+ out.push(raw);
13474
+ return out;
13475
+ }
13476
+ function objectShape(schema) {
13477
+ const shape = schema?.shape;
13478
+ return shape !== null && typeof shape === "object" ? shape : void 0;
13479
+ }
13480
+
13481
+ // src/sanitize/sanitize-tool-input.ts
13482
+ function applyTrim(key2, value, ctx) {
13483
+ const trimmed = value.trim();
13484
+ if (trimmed !== value) ctx.notes.push(`trimmed "${key2}"`);
13485
+ return trimmed;
13486
+ }
13487
+ function applyCoerce(key2, raw, ctx) {
13488
+ const field = ctx.shape?.[key2];
13489
+ let coerced = raw;
13490
+ if (field) {
13491
+ for (const candidate of coerceCandidates(raw, ctx.repairJson)) {
13492
+ if (field.safeParse(candidate).success) {
13493
+ coerced = candidate;
13494
+ break;
13495
+ }
13496
+ }
13497
+ } else {
13498
+ coerced = heuristicCoerce(raw, ctx.repairJson);
13499
+ }
13500
+ if (coerced !== raw) ctx.notes.push(`coerced "${key2}"`);
13501
+ return coerced;
13502
+ }
13503
+ function applyRepair(key2, value, ctx) {
13504
+ const repaired = tryJson(value, true);
13505
+ if (repaired === void 0) return value;
13506
+ ctx.notes.push(`repaired json "${key2}"`);
13507
+ return repaired;
13508
+ }
13509
+ function sanitizeString(key2, value, ctx) {
13510
+ let out = ctx.trim ? applyTrim(key2, value, ctx) : value;
13511
+ if (ctx.coerce && typeof out === "string") out = applyCoerce(key2, out, ctx);
13512
+ if (ctx.repairJson && !ctx.coerce && typeof out === "string") out = applyRepair(key2, out, ctx);
13513
+ return out;
13514
+ }
13515
+ function walk(input, ctx, depth) {
13516
+ const out = {};
13517
+ for (const [key2, value] of Object.entries(input)) {
13518
+ if (typeof value === "string") out[key2] = sanitizeString(key2, value, ctx);
13519
+ else if (ctx.deep && depth < ctx.maxDepth && isPlainObject(value))
13520
+ out[key2] = walk(value, ctx, depth + 1);
13521
+ else out[key2] = value;
13522
+ }
13523
+ return out;
13524
+ }
13525
+ function sanitizeToolInput(input, options) {
13526
+ if (!isPlainObject(input)) return { value: input, changed: false, notes: [] };
13527
+ const ctx = {
13528
+ trim: options?.trim ?? true,
13529
+ coerce: options?.coerce ?? false,
13530
+ repairJson: options?.repairJson ?? false,
13531
+ deep: options?.deep ?? false,
13532
+ maxDepth: options?.maxDepth ?? 8,
13533
+ shape: objectShape(options?.schema),
13534
+ notes: []
13535
+ };
13536
+ const value = walk(input, ctx, 0);
13537
+ return { value, changed: ctx.notes.length > 0, notes: ctx.notes };
13538
+ }
13430
13539
 
13431
13540
  // src/internal/llm/hermes-tool-extract.ts
13432
13541
  var HERMES_BLOCK = /<function=\s*([^>\s]+)\s*>([\s\S]*?)<\/tool_call>/g;
@@ -13454,7 +13563,7 @@ function parseHermesParams(inner) {
13454
13563
  if (key2 === void 0 || value === void 0) continue;
13455
13564
  input[key2.trim()] = value;
13456
13565
  }
13457
- return input;
13566
+ return sanitizeToolInput(input, { trim: true }).value;
13458
13567
  }
13459
13568
 
13460
13569
  // src/internal/llm/openai.ts
@@ -18175,7 +18284,11 @@ function defineTool(spec) {
18175
18284
  description: spec.description,
18176
18285
  inputSchema,
18177
18286
  handler: async (input) => {
18178
- const parsed = spec.inputSchema.parse(input);
18287
+ const raw = spec.sanitize ? sanitizeToolInput(input, {
18288
+ ...spec.sanitize === true ? {} : spec.sanitize,
18289
+ schema: spec.inputSchema
18290
+ }).value : input;
18291
+ const parsed = spec.inputSchema.parse(raw);
18179
18292
  return await spec.handler(parsed);
18180
18293
  }
18181
18294
  };