|
| 1 | +export * as Data from "./data.js" |
| 2 | + |
| 3 | +import type { DiagnosticKind } from "./codemode.js" |
| 4 | +import { Values } from "./values.js" |
| 5 | + |
| 6 | +/** A null-prototype object owned by the program. */ |
| 7 | +export type SafeObject = Record<string, unknown> |
| 8 | + |
| 9 | +const MAX_VALUE_DEPTH = 32 |
| 10 | + |
| 11 | +export class ToolRuntimeError extends Error { |
| 12 | + constructor( |
| 13 | + readonly kind: Extract< |
| 14 | + DiagnosticKind, |
| 15 | + "UnknownTool" | "InvalidToolInput" | "InvalidToolOutput" | "InvalidDataValue" | "ToolCallLimitExceeded" |
| 16 | + >, |
| 17 | + message: string, |
| 18 | + readonly suggestions: ReadonlyArray<string> = [], |
| 19 | + ) { |
| 20 | + super(message) |
| 21 | + this.name = "ToolRuntimeError" |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +const blockedMemberNames = new Set(["__proto__", "constructor", "prototype"]) |
| 26 | + |
| 27 | +export const isBlockedMember = (name: string): boolean => blockedMemberNames.has(name) |
| 28 | + |
| 29 | +/** |
| 30 | + * Brings a host-produced runtime value into the program: runtime values pass through, their host |
| 31 | + * counterparts (Date, RegExp, Map, Set, URL, URLSearchParams) are wrapped, and objects become |
| 32 | + * null-prototype copies. Arrays keep extra enumerable properties such as `index` and `groups`. |
| 33 | + */ |
| 34 | +export const toProgram = (value: unknown, label: string): unknown => copy(value, label, "program", 0, new Set()) |
| 35 | + |
| 36 | +/** |
| 37 | + * Brings host data into the program: Date and URL become strings, other host collections become |
| 38 | + * empty objects, and objects become null-prototype copies. Used for tool results and parsed JSON. |
| 39 | + */ |
| 40 | +export const fromData = (value: unknown, label: string): unknown => copy(value, label, "data", 0, new Set()) |
| 41 | + |
| 42 | +/** |
| 43 | + * Takes a program value out as plain JSON: runtime values serialize like `JSON.stringify` would, |
| 44 | + * non-finite numbers become null, and array holes become null. `undefined` object properties are |
| 45 | + * dropped ("json") or become null ("result", for program results where the consumer must never see |
| 46 | + * undefined); a bare `undefined` follows the same rule. |
| 47 | + */ |
| 48 | +export const toData = (value: unknown, label: string, undefinedAs: "json" | "result" = "json"): unknown => |
| 49 | + copy(value, label, undefinedAs, 0, new Set()) |
| 50 | + |
| 51 | +// "program" and "data" build program-owned null-prototype objects; "json" and "result" build |
| 52 | +// ordinary objects for the host. |
| 53 | +type Mode = "program" | "data" | "json" | "result" |
| 54 | + |
| 55 | +const copy = (value: unknown, label: string, mode: Mode, depth: number, seen: Set<object>): unknown => { |
| 56 | + if (depth > MAX_VALUE_DEPTH) { |
| 57 | + throw new ToolRuntimeError("InvalidDataValue", `${label} exceeds the maximum value depth of ${MAX_VALUE_DEPTH}.`) |
| 58 | + } |
| 59 | + if (value === undefined) return mode === "result" ? null : undefined |
| 60 | + if (typeof value === "number") return (mode === "json" || mode === "result") && !Number.isFinite(value) ? null : value |
| 61 | + if (value === null || typeof value === "string" || typeof value === "boolean") return value |
| 62 | + if (typeof value !== "object") { |
| 63 | + throw new ToolRuntimeError("InvalidDataValue", `${label} must contain data only.`) |
| 64 | + } |
| 65 | + if (value instanceof Values.Promise) { |
| 66 | + throw new ToolRuntimeError( |
| 67 | + "InvalidDataValue", |
| 68 | + `${label} contains an un-awaited Promise; await tool calls (e.g. \`const result = await tools.ns.tool(...)\`) before using their results.`, |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + if (mode === "program") { |
| 73 | + if (Values.isValue(value)) return value |
| 74 | + if (value instanceof Date) return new Values.Date(value.getTime()) |
| 75 | + if (value instanceof RegExp) return new Values.RegExp(value.source, value.flags) |
| 76 | + if (value instanceof Map) { |
| 77 | + const wrapped = new Values.Map() |
| 78 | + for (const [key, item] of value.entries()) { |
| 79 | + wrapped.map.set(copy(key, label, mode, depth + 1, seen), copy(item, label, mode, depth + 1, seen)) |
| 80 | + } |
| 81 | + return wrapped |
| 82 | + } |
| 83 | + if (value instanceof Set) { |
| 84 | + const wrapped = new Values.Set() |
| 85 | + for (const item of value.values()) wrapped.set.add(copy(item, label, mode, depth + 1, seen)) |
| 86 | + return wrapped |
| 87 | + } |
| 88 | + if (value instanceof URL) return new Values.URL(new URL(value.href)) |
| 89 | + if (value instanceof URLSearchParams) return new Values.URLSearchParams(new URLSearchParams(value)) |
| 90 | + } |
| 91 | + |
| 92 | + const plain = mode === "program" || mode === "data" |
| 93 | + if (value instanceof Values.Date) return Number.isFinite(value.time) ? new Date(value.time).toISOString() : null |
| 94 | + if (value instanceof Date) return Number.isFinite(value.getTime()) ? value.toISOString() : null |
| 95 | + if (value instanceof Values.URL) return value.url.href |
| 96 | + if (value instanceof URL) return value.href |
| 97 | + // Remaining runtime values and their host counterparts serialize as empty objects, like JSON.stringify. |
| 98 | + if ( |
| 99 | + Values.isValue(value) || |
| 100 | + value instanceof RegExp || |
| 101 | + value instanceof Map || |
| 102 | + value instanceof Set || |
| 103 | + value instanceof URLSearchParams |
| 104 | + ) { |
| 105 | + return plain ? (Object.create(null) as SafeObject) : {} |
| 106 | + } |
| 107 | + |
| 108 | + if (seen.has(value)) { |
| 109 | + throw new ToolRuntimeError("InvalidDataValue", `${label} contains a circular value.`) |
| 110 | + } |
| 111 | + seen.add(value) |
| 112 | + |
| 113 | + if (Array.isArray(value)) { |
| 114 | + // Host output densifies holes to null like JSON; program copies keep them. |
| 115 | + const copied = plain |
| 116 | + ? value.map((item) => copy(item, label, mode, depth + 1, seen)) |
| 117 | + : Array.from(value, (item) => copy(item, label, mode, depth + 1, seen) ?? null) |
| 118 | + if (mode === "program") { |
| 119 | + for (const [key, item] of Object.entries(value)) { |
| 120 | + if (Object.hasOwn(copied, key)) continue |
| 121 | + if (isBlockedMember(key)) { |
| 122 | + throw new ToolRuntimeError("InvalidDataValue", `${label} contains blocked property '${key}'.`) |
| 123 | + } |
| 124 | + Reflect.set(copied, key, copy(item, label, mode, depth + 1, seen)) |
| 125 | + } |
| 126 | + } |
| 127 | + seen.delete(value) |
| 128 | + return copied |
| 129 | + } |
| 130 | + |
| 131 | + const prototype = Object.getPrototypeOf(value) |
| 132 | + if (prototype !== Object.prototype && prototype !== null) { |
| 133 | + throw new ToolRuntimeError("InvalidDataValue", `${label} must contain plain objects only.`) |
| 134 | + } |
| 135 | + |
| 136 | + const copied: SafeObject = plain ? (Object.create(null) as SafeObject) : {} |
| 137 | + for (const [key, item] of Object.entries(value)) { |
| 138 | + if (isBlockedMember(key)) { |
| 139 | + throw new ToolRuntimeError("InvalidDataValue", `${label} contains blocked property '${key}'.`) |
| 140 | + } |
| 141 | + const next = copy(item, label, mode, depth + 1, seen) |
| 142 | + if (next === undefined && mode === "json") continue |
| 143 | + copied[key] = next |
| 144 | + } |
| 145 | + seen.delete(value) |
| 146 | + return copied |
| 147 | +} |
0 commit comments