UNPKG

@tanstack/ai

Version:

Type-safe TypeScript AI SDK for streaming chat, tool calling, agents, structured outputs, and multimodal generation.

85 lines (84 loc) • 3.12 kB
import { appMetadata } from "../../../utilities/merge-metadata.js"; import { errorMessage } from "../../../utilities/errors.js"; import { StandardSchemaValidationError, isStandardSchema, validateWithStandardSchema } from "./schema-converter.js"; // src/activities/chat/tools/result-metadata.ts /** * Collect the result metadata that one tool call sets. * * `set` merges the top-level keys. A later key replaces an earlier key. * A key set to `undefined` is removed. The reserved `tanstack` key is dropped. * `get` returns `undefined` when no key is left. */ function createResultMetadataCollector() { let current; return { set(value) { const entries = Object.entries({ ...current, ...value }); current = appMetadata(Object.fromEntries(entries.filter(([, entry]) => entry !== void 0))); }, get() { return current; } }; } function reasonOf(error) { return errorMessage(error) ?? String(error); } /** * A plain JSON copy of `value`. When it cannot become JSON, logs the reason * and throws an error with only the tool name, because the model can see it. */ function jsonCopy(toolName, value) { try { return JSON.parse(JSON.stringify(value)); } catch (error) { console.warn(`Result metadata for tool ${toolName} is not JSON: ${reasonOf(error)}`); throw new Error(`Result metadata for tool ${toolName} is not JSON`); } } /** * Check the result metadata of a tool call before it leaves the engine. * * Returns a plain JSON copy of `value` without the reserved `tanstack` key. * When the tool has a Standard Schema `resultMetadataSchema`, returns the * parsed value. A sync or an async schema works. A JSON Schema is not * checked at runtime, the same as `outputSchema`. * * Throws an `Error` when the value cannot become JSON or the schema check * fails. The error has only the tool name, because the model sees it. The * details go to the server log. */ async function finalizeResultMetadata(tool, value) { if (value === void 0) return void 0; const json = jsonCopy(tool.name, value); if (!isStandardSchema(tool.resultMetadataSchema)) return appMetadata(json); let parsed; try { const validation = await validateWithStandardSchema(tool.resultMetadataSchema, json); if (!validation.success) throw new StandardSchemaValidationError(validation.issues); parsed = validation.data; } catch (error) { console.warn(`Result metadata validation failed for tool ${tool.name}: ${reasonOf(error)}`); throw new Error(`Result metadata validation failed for tool ${tool.name}`); } return appMetadata(jsonCopy(tool.name, parsed)); } /** * Check the result metadata of a middleware decision (`skip` or * `replaceResult`). Returns a plain JSON copy without the reserved * `tanstack` key. Metadata that cannot become JSON is dropped with a * warning, so the result stays. */ function middlewareResultMetadata(toolName, value) { if (value === void 0) return void 0; try { return appMetadata(jsonCopy(toolName, value)); } catch { return; } } export { createResultMetadataCollector, finalizeResultMetadata, middlewareResultMetadata }; //# sourceMappingURL=result-metadata.js.map