@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/cron.js CHANGED
@@ -10952,6 +10952,115 @@ function toOllamaTools(tools) {
10952
10952
  }
10953
10953
  }));
10954
10954
  }
10955
+ var cachedJsonrepair;
10956
+ function loadJsonrepair() {
10957
+ if (cachedJsonrepair === void 0) {
10958
+ const req = createRequire(import.meta.url);
10959
+ cachedJsonrepair = req("jsonrepair").jsonrepair;
10960
+ }
10961
+ return cachedJsonrepair;
10962
+ }
10963
+ function isPlainObject(v) {
10964
+ return v !== null && typeof v === "object" && !Array.isArray(v);
10965
+ }
10966
+ function toFiniteNumber(raw) {
10967
+ if (raw === "") return void 0;
10968
+ const n = Number(raw);
10969
+ return Number.isFinite(n) && String(n) === raw ? n : void 0;
10970
+ }
10971
+ function tryJson(raw, repair) {
10972
+ const t = raw.trimStart();
10973
+ if (!(t.startsWith("{") || t.startsWith("["))) return void 0;
10974
+ try {
10975
+ return JSON.parse(repair ? loadJsonrepair()(t) : t);
10976
+ } catch {
10977
+ return void 0;
10978
+ }
10979
+ }
10980
+ function heuristicCoerce(raw, repairJson) {
10981
+ if (raw === "true") return true;
10982
+ if (raw === "false") return false;
10983
+ if (raw === "null") return null;
10984
+ const n = toFiniteNumber(raw);
10985
+ if (n !== void 0) return n;
10986
+ const json = tryJson(raw, false) ?? (repairJson ? tryJson(raw, true) : void 0);
10987
+ return json === void 0 ? raw : json;
10988
+ }
10989
+ function coerceCandidates(raw, repairJson) {
10990
+ const out = [];
10991
+ if (raw === "true") out.push(true);
10992
+ else if (raw === "false") out.push(false);
10993
+ else if (raw === "null") out.push(null);
10994
+ const n = toFiniteNumber(raw);
10995
+ if (n !== void 0) out.push(n);
10996
+ const json = tryJson(raw, false) ?? (repairJson ? tryJson(raw, true) : void 0);
10997
+ if (json !== void 0) out.push(json);
10998
+ out.push(raw);
10999
+ return out;
11000
+ }
11001
+ function objectShape(schema) {
11002
+ const shape = schema?.shape;
11003
+ return shape !== null && typeof shape === "object" ? shape : void 0;
11004
+ }
11005
+
11006
+ // src/sanitize/sanitize-tool-input.ts
11007
+ function applyTrim(key, value, ctx) {
11008
+ const trimmed = value.trim();
11009
+ if (trimmed !== value) ctx.notes.push(`trimmed "${key}"`);
11010
+ return trimmed;
11011
+ }
11012
+ function applyCoerce(key, raw, ctx) {
11013
+ const field = ctx.shape?.[key];
11014
+ let coerced = raw;
11015
+ if (field) {
11016
+ for (const candidate of coerceCandidates(raw, ctx.repairJson)) {
11017
+ if (field.safeParse(candidate).success) {
11018
+ coerced = candidate;
11019
+ break;
11020
+ }
11021
+ }
11022
+ } else {
11023
+ coerced = heuristicCoerce(raw, ctx.repairJson);
11024
+ }
11025
+ if (coerced !== raw) ctx.notes.push(`coerced "${key}"`);
11026
+ return coerced;
11027
+ }
11028
+ function applyRepair(key, value, ctx) {
11029
+ const repaired = tryJson(value, true);
11030
+ if (repaired === void 0) return value;
11031
+ ctx.notes.push(`repaired json "${key}"`);
11032
+ return repaired;
11033
+ }
11034
+ function sanitizeString(key, value, ctx) {
11035
+ let out = ctx.trim ? applyTrim(key, value, ctx) : value;
11036
+ if (ctx.coerce && typeof out === "string") out = applyCoerce(key, out, ctx);
11037
+ if (ctx.repairJson && !ctx.coerce && typeof out === "string") out = applyRepair(key, out, ctx);
11038
+ return out;
11039
+ }
11040
+ function walk(input, ctx, depth) {
11041
+ const out = {};
11042
+ for (const [key, value] of Object.entries(input)) {
11043
+ if (typeof value === "string") out[key] = sanitizeString(key, value, ctx);
11044
+ else if (ctx.deep && depth < ctx.maxDepth && isPlainObject(value))
11045
+ out[key] = walk(value, ctx, depth + 1);
11046
+ else out[key] = value;
11047
+ }
11048
+ return out;
11049
+ }
11050
+ function sanitizeToolInput(input, options) {
11051
+ if (!isPlainObject(input)) return { value: input, changed: false, notes: [] };
11052
+ const ctx = {
11053
+ trim: options?.trim,
11054
+ coerce: options?.coerce ?? false,
11055
+ repairJson: options?.repairJson ?? false,
11056
+ deep: options?.deep ?? false,
11057
+ maxDepth: options?.maxDepth ?? 8,
11058
+ shape: objectShape(options?.schema),
11059
+ notes: []
11060
+ };
11061
+ const value = walk(input, ctx, 0);
11062
+ return { value, changed: ctx.notes.length > 0, notes: ctx.notes };
11063
+ }
10955
11064
 
10956
11065
  // src/internal/llm/hermes-tool-extract.ts
10957
11066
  var HERMES_BLOCK = /<function=\s*([^>\s]+)\s*>([\s\S]*?)<\/tool_call>/g;
@@ -10979,7 +11088,7 @@ function parseHermesParams(inner) {
10979
11088
  if (key === void 0 || value === void 0) continue;
10980
11089
  input[key.trim()] = value;
10981
11090
  }
10982
- return input;
11091
+ return sanitizeToolInput(input, { trim: true }).value;
10983
11092
  }
10984
11093
 
10985
11094
  // src/internal/llm/openai.ts