@theokit/sdk 2.13.1 → 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/eval.js CHANGED
@@ -10947,6 +10947,115 @@ function toOllamaTools(tools) {
10947
10947
  }
10948
10948
  }));
10949
10949
  }
10950
+ var cachedJsonrepair;
10951
+ function loadJsonrepair() {
10952
+ if (cachedJsonrepair === void 0) {
10953
+ const req = createRequire(import.meta.url);
10954
+ cachedJsonrepair = req("jsonrepair").jsonrepair;
10955
+ }
10956
+ return cachedJsonrepair;
10957
+ }
10958
+ function isPlainObject(v) {
10959
+ return v !== null && typeof v === "object" && !Array.isArray(v);
10960
+ }
10961
+ function toFiniteNumber(raw) {
10962
+ if (raw === "") return void 0;
10963
+ const n = Number(raw);
10964
+ return Number.isFinite(n) && String(n) === raw ? n : void 0;
10965
+ }
10966
+ function tryJson(raw, repair) {
10967
+ const t = raw.trimStart();
10968
+ if (!(t.startsWith("{") || t.startsWith("["))) return void 0;
10969
+ try {
10970
+ return JSON.parse(repair ? loadJsonrepair()(t) : t);
10971
+ } catch {
10972
+ return void 0;
10973
+ }
10974
+ }
10975
+ function heuristicCoerce(raw, repairJson) {
10976
+ if (raw === "true") return true;
10977
+ if (raw === "false") return false;
10978
+ if (raw === "null") return null;
10979
+ const n = toFiniteNumber(raw);
10980
+ if (n !== void 0) return n;
10981
+ const json = tryJson(raw, false) ?? (repairJson ? tryJson(raw, true) : void 0);
10982
+ return json === void 0 ? raw : json;
10983
+ }
10984
+ function coerceCandidates(raw, repairJson) {
10985
+ const out = [];
10986
+ if (raw === "true") out.push(true);
10987
+ else if (raw === "false") out.push(false);
10988
+ else if (raw === "null") out.push(null);
10989
+ const n = toFiniteNumber(raw);
10990
+ if (n !== void 0) out.push(n);
10991
+ const json = tryJson(raw, false) ?? (repairJson ? tryJson(raw, true) : void 0);
10992
+ if (json !== void 0) out.push(json);
10993
+ out.push(raw);
10994
+ return out;
10995
+ }
10996
+ function objectShape(schema) {
10997
+ const shape = schema?.shape;
10998
+ return shape !== null && typeof shape === "object" ? shape : void 0;
10999
+ }
11000
+
11001
+ // src/sanitize/sanitize-tool-input.ts
11002
+ function applyTrim(key, value, ctx) {
11003
+ const trimmed = value.trim();
11004
+ if (trimmed !== value) ctx.notes.push(`trimmed "${key}"`);
11005
+ return trimmed;
11006
+ }
11007
+ function applyCoerce(key, raw, ctx) {
11008
+ const field = ctx.shape?.[key];
11009
+ let coerced = raw;
11010
+ if (field) {
11011
+ for (const candidate of coerceCandidates(raw, ctx.repairJson)) {
11012
+ if (field.safeParse(candidate).success) {
11013
+ coerced = candidate;
11014
+ break;
11015
+ }
11016
+ }
11017
+ } else {
11018
+ coerced = heuristicCoerce(raw, ctx.repairJson);
11019
+ }
11020
+ if (coerced !== raw) ctx.notes.push(`coerced "${key}"`);
11021
+ return coerced;
11022
+ }
11023
+ function applyRepair(key, value, ctx) {
11024
+ const repaired = tryJson(value, true);
11025
+ if (repaired === void 0) return value;
11026
+ ctx.notes.push(`repaired json "${key}"`);
11027
+ return repaired;
11028
+ }
11029
+ function sanitizeString(key, value, ctx) {
11030
+ let out = ctx.trim ? applyTrim(key, value, ctx) : value;
11031
+ if (ctx.coerce && typeof out === "string") out = applyCoerce(key, out, ctx);
11032
+ if (ctx.repairJson && !ctx.coerce && typeof out === "string") out = applyRepair(key, out, ctx);
11033
+ return out;
11034
+ }
11035
+ function walk(input, ctx, depth) {
11036
+ const out = {};
11037
+ for (const [key, value] of Object.entries(input)) {
11038
+ if (typeof value === "string") out[key] = sanitizeString(key, value, ctx);
11039
+ else if (ctx.deep && depth < ctx.maxDepth && isPlainObject(value))
11040
+ out[key] = walk(value, ctx, depth + 1);
11041
+ else out[key] = value;
11042
+ }
11043
+ return out;
11044
+ }
11045
+ function sanitizeToolInput(input, options) {
11046
+ if (!isPlainObject(input)) return { value: input, changed: false, notes: [] };
11047
+ const ctx = {
11048
+ trim: options?.trim,
11049
+ coerce: options?.coerce ?? false,
11050
+ repairJson: options?.repairJson ?? false,
11051
+ deep: options?.deep ?? false,
11052
+ maxDepth: options?.maxDepth ?? 8,
11053
+ shape: objectShape(options?.schema),
11054
+ notes: []
11055
+ };
11056
+ const value = walk(input, ctx, 0);
11057
+ return { value, changed: ctx.notes.length > 0, notes: ctx.notes };
11058
+ }
10950
11059
 
10951
11060
  // src/internal/llm/hermes-tool-extract.ts
10952
11061
  var HERMES_BLOCK = /<function=\s*([^>\s]+)\s*>([\s\S]*?)<\/tool_call>/g;
@@ -10972,9 +11081,9 @@ function parseHermesParams(inner) {
10972
11081
  const key = param[1];
10973
11082
  const value = param[2];
10974
11083
  if (key === void 0 || value === void 0) continue;
10975
- input[key.trim()] = value.trim();
11084
+ input[key.trim()] = value;
10976
11085
  }
10977
- return input;
11086
+ return sanitizeToolInput(input, { trim: true }).value;
10978
11087
  }
10979
11088
 
10980
11089
  // src/internal/llm/openai.ts
@@ -15784,7 +15893,7 @@ var JsonlParseError = class extends Error {
15784
15893
  }
15785
15894
  line;
15786
15895
  };
15787
- function isPlainObject(value) {
15896
+ function isPlainObject2(value) {
15788
15897
  return typeof value === "object" && value !== null && !Array.isArray(value);
15789
15898
  }
15790
15899
  function tryParseObjectLine(line) {
@@ -15795,7 +15904,7 @@ function tryParseObjectLine(line) {
15795
15904
  } catch {
15796
15905
  return void 0;
15797
15906
  }
15798
- return isPlainObject(parsed) ? parsed : void 0;
15907
+ return isPlainObject2(parsed) ? parsed : void 0;
15799
15908
  }
15800
15909
  function loadJsonl(path, opts = {}) {
15801
15910
  const text = readFileSync(path, "utf8");
@@ -15811,7 +15920,7 @@ function loadJsonl(path, opts = {}) {
15811
15920
  } catch {
15812
15921
  throw new JsonlParseError(`line ${lineNumber}: invalid JSON`, lineNumber);
15813
15922
  }
15814
- if (!isPlainObject(parsed)) {
15923
+ if (!isPlainObject2(parsed)) {
15815
15924
  throw new JsonlParseError(`line ${lineNumber}: not a JSON object`, lineNumber);
15816
15925
  }
15817
15926
  out.push(opts.map ? opts.map(parsed, lineNumber) : parsed);