@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/CHANGELOG.md +6 -0
- package/dist/a2a/index.cjs +121 -2
- package/dist/a2a/index.cjs.map +1 -1
- package/dist/a2a/index.js +121 -2
- package/dist/a2a/index.js.map +1 -1
- package/dist/cron.cjs +111 -2
- package/dist/cron.cjs.map +1 -1
- package/dist/cron.js +111 -2
- package/dist/cron.js.map +1 -1
- package/dist/define-tool.d.ts +8 -0
- package/dist/eval.cjs +114 -5
- package/dist/eval.cjs.map +1 -1
- package/dist/eval.js +114 -5
- package/dist/eval.js.map +1 -1
- package/dist/index.cjs +116 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +35 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +116 -3
- package/dist/index.js.map +1 -1
- package/dist/internal/llm/hermes-tool-extract.d.ts +1 -0
- package/dist/sanitize/coerce.d.cts +1 -0
- package/dist/sanitize/coerce.d.ts +1 -0
- package/dist/sanitize/index.cjs +119 -0
- package/dist/sanitize/index.cjs.map +1 -0
- package/dist/sanitize/index.d.cts +9 -0
- package/dist/sanitize/index.d.ts +9 -0
- package/dist/sanitize/index.js +116 -0
- package/dist/sanitize/index.js.map +1 -0
- package/dist/sanitize/sanitize-tool-input.d.cts +11 -0
- package/dist/sanitize/sanitize-tool-input.d.ts +11 -0
- package/dist/sanitize/types.d.cts +39 -0
- package/dist/sanitize/types.d.ts +39 -0
- package/package.json +13 -2
package/dist/define-tool.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { z as ZodNamespace, ZodType } from "zod";
|
|
2
|
+
import type { SanitizeOptions } from "./sanitize/types.js";
|
|
2
3
|
import type { CustomTool } from "./types/agent.js";
|
|
3
4
|
/**
|
|
4
5
|
* Spec accepted by {@link defineTool}. `inputSchema` is a Zod schema; the
|
|
@@ -15,6 +16,13 @@ export interface DefineToolSpec<T extends ZodType> {
|
|
|
15
16
|
inputSchema: T;
|
|
16
17
|
/** Handler invoked with the parsed input. Type is inferred via `z.infer<T>`. */
|
|
17
18
|
handler: (input: ZodNamespace.infer<T>) => string | Promise<string>;
|
|
19
|
+
/**
|
|
20
|
+
* Sanitize the raw model-emitted args BEFORE schema validation (`@theokit/sdk/sanitize`).
|
|
21
|
+
* `true` trims whitespace; an object opts into coercion / JSON-repair. Coercion is schema-aware
|
|
22
|
+
* against this tool's `inputSchema`. Absent ⇒ args reach validation untouched. Sanitize is
|
|
23
|
+
* hygiene, not a validity bypass — a genuinely invalid arg still raises `ZodError`.
|
|
24
|
+
*/
|
|
25
|
+
sanitize?: boolean | SanitizeOptions;
|
|
18
26
|
}
|
|
19
27
|
/**
|
|
20
28
|
* Type-safe builder for {@link CustomTool}. Converts a Zod schema to JSON
|
package/dist/eval.cjs
CHANGED
|
@@ -10950,6 +10950,115 @@ function toOllamaTools(tools) {
|
|
|
10950
10950
|
}
|
|
10951
10951
|
}));
|
|
10952
10952
|
}
|
|
10953
|
+
var cachedJsonrepair;
|
|
10954
|
+
function loadJsonrepair() {
|
|
10955
|
+
if (cachedJsonrepair === void 0) {
|
|
10956
|
+
const req = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('eval.cjs', document.baseURI).href)));
|
|
10957
|
+
cachedJsonrepair = req("jsonrepair").jsonrepair;
|
|
10958
|
+
}
|
|
10959
|
+
return cachedJsonrepair;
|
|
10960
|
+
}
|
|
10961
|
+
function isPlainObject(v) {
|
|
10962
|
+
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
10963
|
+
}
|
|
10964
|
+
function toFiniteNumber(raw) {
|
|
10965
|
+
if (raw === "") return void 0;
|
|
10966
|
+
const n = Number(raw);
|
|
10967
|
+
return Number.isFinite(n) && String(n) === raw ? n : void 0;
|
|
10968
|
+
}
|
|
10969
|
+
function tryJson(raw, repair) {
|
|
10970
|
+
const t = raw.trimStart();
|
|
10971
|
+
if (!(t.startsWith("{") || t.startsWith("["))) return void 0;
|
|
10972
|
+
try {
|
|
10973
|
+
return JSON.parse(repair ? loadJsonrepair()(t) : t);
|
|
10974
|
+
} catch {
|
|
10975
|
+
return void 0;
|
|
10976
|
+
}
|
|
10977
|
+
}
|
|
10978
|
+
function heuristicCoerce(raw, repairJson) {
|
|
10979
|
+
if (raw === "true") return true;
|
|
10980
|
+
if (raw === "false") return false;
|
|
10981
|
+
if (raw === "null") return null;
|
|
10982
|
+
const n = toFiniteNumber(raw);
|
|
10983
|
+
if (n !== void 0) return n;
|
|
10984
|
+
const json = tryJson(raw, false) ?? (repairJson ? tryJson(raw, true) : void 0);
|
|
10985
|
+
return json === void 0 ? raw : json;
|
|
10986
|
+
}
|
|
10987
|
+
function coerceCandidates(raw, repairJson) {
|
|
10988
|
+
const out = [];
|
|
10989
|
+
if (raw === "true") out.push(true);
|
|
10990
|
+
else if (raw === "false") out.push(false);
|
|
10991
|
+
else if (raw === "null") out.push(null);
|
|
10992
|
+
const n = toFiniteNumber(raw);
|
|
10993
|
+
if (n !== void 0) out.push(n);
|
|
10994
|
+
const json = tryJson(raw, false) ?? (repairJson ? tryJson(raw, true) : void 0);
|
|
10995
|
+
if (json !== void 0) out.push(json);
|
|
10996
|
+
out.push(raw);
|
|
10997
|
+
return out;
|
|
10998
|
+
}
|
|
10999
|
+
function objectShape(schema) {
|
|
11000
|
+
const shape = schema?.shape;
|
|
11001
|
+
return shape !== null && typeof shape === "object" ? shape : void 0;
|
|
11002
|
+
}
|
|
11003
|
+
|
|
11004
|
+
// src/sanitize/sanitize-tool-input.ts
|
|
11005
|
+
function applyTrim(key, value, ctx) {
|
|
11006
|
+
const trimmed = value.trim();
|
|
11007
|
+
if (trimmed !== value) ctx.notes.push(`trimmed "${key}"`);
|
|
11008
|
+
return trimmed;
|
|
11009
|
+
}
|
|
11010
|
+
function applyCoerce(key, raw, ctx) {
|
|
11011
|
+
const field = ctx.shape?.[key];
|
|
11012
|
+
let coerced = raw;
|
|
11013
|
+
if (field) {
|
|
11014
|
+
for (const candidate of coerceCandidates(raw, ctx.repairJson)) {
|
|
11015
|
+
if (field.safeParse(candidate).success) {
|
|
11016
|
+
coerced = candidate;
|
|
11017
|
+
break;
|
|
11018
|
+
}
|
|
11019
|
+
}
|
|
11020
|
+
} else {
|
|
11021
|
+
coerced = heuristicCoerce(raw, ctx.repairJson);
|
|
11022
|
+
}
|
|
11023
|
+
if (coerced !== raw) ctx.notes.push(`coerced "${key}"`);
|
|
11024
|
+
return coerced;
|
|
11025
|
+
}
|
|
11026
|
+
function applyRepair(key, value, ctx) {
|
|
11027
|
+
const repaired = tryJson(value, true);
|
|
11028
|
+
if (repaired === void 0) return value;
|
|
11029
|
+
ctx.notes.push(`repaired json "${key}"`);
|
|
11030
|
+
return repaired;
|
|
11031
|
+
}
|
|
11032
|
+
function sanitizeString(key, value, ctx) {
|
|
11033
|
+
let out = ctx.trim ? applyTrim(key, value, ctx) : value;
|
|
11034
|
+
if (ctx.coerce && typeof out === "string") out = applyCoerce(key, out, ctx);
|
|
11035
|
+
if (ctx.repairJson && !ctx.coerce && typeof out === "string") out = applyRepair(key, out, ctx);
|
|
11036
|
+
return out;
|
|
11037
|
+
}
|
|
11038
|
+
function walk(input, ctx, depth) {
|
|
11039
|
+
const out = {};
|
|
11040
|
+
for (const [key, value] of Object.entries(input)) {
|
|
11041
|
+
if (typeof value === "string") out[key] = sanitizeString(key, value, ctx);
|
|
11042
|
+
else if (ctx.deep && depth < ctx.maxDepth && isPlainObject(value))
|
|
11043
|
+
out[key] = walk(value, ctx, depth + 1);
|
|
11044
|
+
else out[key] = value;
|
|
11045
|
+
}
|
|
11046
|
+
return out;
|
|
11047
|
+
}
|
|
11048
|
+
function sanitizeToolInput(input, options) {
|
|
11049
|
+
if (!isPlainObject(input)) return { value: input, changed: false, notes: [] };
|
|
11050
|
+
const ctx = {
|
|
11051
|
+
trim: options?.trim,
|
|
11052
|
+
coerce: options?.coerce ?? false,
|
|
11053
|
+
repairJson: options?.repairJson ?? false,
|
|
11054
|
+
deep: options?.deep ?? false,
|
|
11055
|
+
maxDepth: options?.maxDepth ?? 8,
|
|
11056
|
+
shape: objectShape(options?.schema),
|
|
11057
|
+
notes: []
|
|
11058
|
+
};
|
|
11059
|
+
const value = walk(input, ctx, 0);
|
|
11060
|
+
return { value, changed: ctx.notes.length > 0, notes: ctx.notes };
|
|
11061
|
+
}
|
|
10953
11062
|
|
|
10954
11063
|
// src/internal/llm/hermes-tool-extract.ts
|
|
10955
11064
|
var HERMES_BLOCK = /<function=\s*([^>\s]+)\s*>([\s\S]*?)<\/tool_call>/g;
|
|
@@ -10975,9 +11084,9 @@ function parseHermesParams(inner) {
|
|
|
10975
11084
|
const key = param[1];
|
|
10976
11085
|
const value = param[2];
|
|
10977
11086
|
if (key === void 0 || value === void 0) continue;
|
|
10978
|
-
input[key.trim()] = value
|
|
11087
|
+
input[key.trim()] = value;
|
|
10979
11088
|
}
|
|
10980
|
-
return input;
|
|
11089
|
+
return sanitizeToolInput(input, { trim: true }).value;
|
|
10981
11090
|
}
|
|
10982
11091
|
|
|
10983
11092
|
// src/internal/llm/openai.ts
|
|
@@ -15787,7 +15896,7 @@ var JsonlParseError = class extends Error {
|
|
|
15787
15896
|
}
|
|
15788
15897
|
line;
|
|
15789
15898
|
};
|
|
15790
|
-
function
|
|
15899
|
+
function isPlainObject2(value) {
|
|
15791
15900
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
15792
15901
|
}
|
|
15793
15902
|
function tryParseObjectLine(line) {
|
|
@@ -15798,7 +15907,7 @@ function tryParseObjectLine(line) {
|
|
|
15798
15907
|
} catch {
|
|
15799
15908
|
return void 0;
|
|
15800
15909
|
}
|
|
15801
|
-
return
|
|
15910
|
+
return isPlainObject2(parsed) ? parsed : void 0;
|
|
15802
15911
|
}
|
|
15803
15912
|
function loadJsonl(path, opts = {}) {
|
|
15804
15913
|
const text = fs.readFileSync(path, "utf8");
|
|
@@ -15814,7 +15923,7 @@ function loadJsonl(path, opts = {}) {
|
|
|
15814
15923
|
} catch {
|
|
15815
15924
|
throw new JsonlParseError(`line ${lineNumber}: invalid JSON`, lineNumber);
|
|
15816
15925
|
}
|
|
15817
|
-
if (!
|
|
15926
|
+
if (!isPlainObject2(parsed)) {
|
|
15818
15927
|
throw new JsonlParseError(`line ${lineNumber}: not a JSON object`, lineNumber);
|
|
15819
15928
|
}
|
|
15820
15929
|
out.push(opts.map ? opts.map(parsed, lineNumber) : parsed);
|