@asm-agent/ai 0.8.2
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/README.md +1333 -0
- package/dist/api-registry.d.ts +20 -0
- package/dist/api-registry.d.ts.map +1 -0
- package/dist/api-registry.js +44 -0
- package/dist/api-registry.js.map +1 -0
- package/dist/bedrock-provider.d.ts +5 -0
- package/dist/bedrock-provider.d.ts.map +1 -0
- package/dist/bedrock-provider.js +6 -0
- package/dist/bedrock-provider.js.map +1 -0
- package/dist/cache-pricing.d.ts +13 -0
- package/dist/cache-pricing.d.ts.map +1 -0
- package/dist/cache-pricing.js +38 -0
- package/dist/cache-pricing.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +116 -0
- package/dist/cli.js.map +1 -0
- package/dist/env-api-keys.d.ts +19 -0
- package/dist/env-api-keys.d.ts.map +1 -0
- package/dist/env-api-keys.js +180 -0
- package/dist/env-api-keys.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/log.d.ts +27 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +69 -0
- package/dist/log.js.map +1 -0
- package/dist/mcp/catalog.d.ts +19 -0
- package/dist/mcp/catalog.d.ts.map +1 -0
- package/dist/mcp/catalog.js +42 -0
- package/dist/mcp/catalog.js.map +1 -0
- package/dist/mcp/index.d.ts +5 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +3 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/oauth.d.ts +15 -0
- package/dist/mcp/oauth.d.ts.map +1 -0
- package/dist/mcp/oauth.js +489 -0
- package/dist/mcp/oauth.js.map +1 -0
- package/dist/mcp.d.ts +2 -0
- package/dist/mcp.d.ts.map +1 -0
- package/dist/mcp.js +2 -0
- package/dist/mcp.js.map +1 -0
- package/dist/models.d.ts +18 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.generated.d.ts +26696 -0
- package/dist/models.generated.d.ts.map +1 -0
- package/dist/models.generated.js +22192 -0
- package/dist/models.generated.js.map +1 -0
- package/dist/models.js +72 -0
- package/dist/models.js.map +1 -0
- package/dist/oauth.d.ts +2 -0
- package/dist/oauth.d.ts.map +1 -0
- package/dist/oauth.js +2 -0
- package/dist/oauth.js.map +1 -0
- package/dist/openrouter-reasoning.d.ts +11 -0
- package/dist/openrouter-reasoning.d.ts.map +1 -0
- package/dist/openrouter-reasoning.js +52 -0
- package/dist/openrouter-reasoning.js.map +1 -0
- package/dist/providers/amazon-bedrock.d.ts +38 -0
- package/dist/providers/amazon-bedrock.d.ts.map +1 -0
- package/dist/providers/amazon-bedrock.js +767 -0
- package/dist/providers/amazon-bedrock.js.map +1 -0
- package/dist/providers/anthropic.d.ts +54 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +999 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/azure-openai-responses.d.ts +12 -0
- package/dist/providers/azure-openai-responses.d.ts.map +1 -0
- package/dist/providers/azure-openai-responses.js +206 -0
- package/dist/providers/azure-openai-responses.js.map +1 -0
- package/dist/providers/cloudflare.d.ts +10 -0
- package/dist/providers/cloudflare.d.ts.map +1 -0
- package/dist/providers/cloudflare.js +23 -0
- package/dist/providers/cloudflare.js.map +1 -0
- package/dist/providers/faux.d.ts +56 -0
- package/dist/providers/faux.d.ts.map +1 -0
- package/dist/providers/faux.js +368 -0
- package/dist/providers/faux.js.map +1 -0
- package/dist/providers/github-copilot-headers.d.ts +8 -0
- package/dist/providers/github-copilot-headers.d.ts.map +1 -0
- package/dist/providers/github-copilot-headers.js +26 -0
- package/dist/providers/github-copilot-headers.js.map +1 -0
- package/dist/providers/google-shared.d.ts +55 -0
- package/dist/providers/google-shared.d.ts.map +1 -0
- package/dist/providers/google-shared.js +312 -0
- package/dist/providers/google-shared.js.map +1 -0
- package/dist/providers/google-vertex.d.ts +15 -0
- package/dist/providers/google-vertex.d.ts.map +1 -0
- package/dist/providers/google-vertex.js +421 -0
- package/dist/providers/google-vertex.js.map +1 -0
- package/dist/providers/google.d.ts +13 -0
- package/dist/providers/google.d.ts.map +1 -0
- package/dist/providers/google.js +370 -0
- package/dist/providers/google.js.map +1 -0
- package/dist/providers/mistral.d.ts +20 -0
- package/dist/providers/mistral.d.ts.map +1 -0
- package/dist/providers/mistral.js +536 -0
- package/dist/providers/mistral.js.map +1 -0
- package/dist/providers/openai-codex-responses.d.ts +30 -0
- package/dist/providers/openai-codex-responses.d.ts.map +1 -0
- package/dist/providers/openai-codex-responses.js +1013 -0
- package/dist/providers/openai-codex-responses.js.map +1 -0
- package/dist/providers/openai-completions.d.ts +21 -0
- package/dist/providers/openai-completions.d.ts.map +1 -0
- package/dist/providers/openai-completions.js +1019 -0
- package/dist/providers/openai-completions.js.map +1 -0
- package/dist/providers/openai-responses-shared.d.ts +18 -0
- package/dist/providers/openai-responses-shared.d.ts.map +1 -0
- package/dist/providers/openai-responses-shared.js +488 -0
- package/dist/providers/openai-responses-shared.js.map +1 -0
- package/dist/providers/openai-responses.d.ts +10 -0
- package/dist/providers/openai-responses.d.ts.map +1 -0
- package/dist/providers/openai-responses.js +218 -0
- package/dist/providers/openai-responses.js.map +1 -0
- package/dist/providers/register-builtins.d.ts +35 -0
- package/dist/providers/register-builtins.d.ts.map +1 -0
- package/dist/providers/register-builtins.js +243 -0
- package/dist/providers/register-builtins.js.map +1 -0
- package/dist/providers/simple-options.d.ts +8 -0
- package/dist/providers/simple-options.d.ts.map +1 -0
- package/dist/providers/simple-options.js +40 -0
- package/dist/providers/simple-options.js.map +1 -0
- package/dist/providers/transform-messages.d.ts +8 -0
- package/dist/providers/transform-messages.d.ts.map +1 -0
- package/dist/providers/transform-messages.js +174 -0
- package/dist/providers/transform-messages.js.map +1 -0
- package/dist/session-resources.d.ts +4 -0
- package/dist/session-resources.d.ts.map +1 -0
- package/dist/session-resources.js +22 -0
- package/dist/session-resources.js.map +1 -0
- package/dist/stream.d.ts +8 -0
- package/dist/stream.d.ts.map +1 -0
- package/dist/stream.js +27 -0
- package/dist/stream.js.map +1 -0
- package/dist/types.d.ts +411 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/diagnostics.d.ts +19 -0
- package/dist/utils/diagnostics.d.ts.map +1 -0
- package/dist/utils/diagnostics.js +25 -0
- package/dist/utils/diagnostics.js.map +1 -0
- package/dist/utils/event-stream.d.ts +21 -0
- package/dist/utils/event-stream.d.ts.map +1 -0
- package/dist/utils/event-stream.js +78 -0
- package/dist/utils/event-stream.js.map +1 -0
- package/dist/utils/hash.d.ts +2 -0
- package/dist/utils/hash.d.ts.map +1 -0
- package/dist/utils/hash.js +13 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/headers.d.ts +2 -0
- package/dist/utils/headers.d.ts.map +1 -0
- package/dist/utils/headers.js +8 -0
- package/dist/utils/headers.js.map +1 -0
- package/dist/utils/json-parse.d.ts +16 -0
- package/dist/utils/json-parse.d.ts.map +1 -0
- package/dist/utils/json-parse.js +113 -0
- package/dist/utils/json-parse.js.map +1 -0
- package/dist/utils/oauth/anthropic.d.ts +25 -0
- package/dist/utils/oauth/anthropic.d.ts.map +1 -0
- package/dist/utils/oauth/anthropic.js +335 -0
- package/dist/utils/oauth/anthropic.js.map +1 -0
- package/dist/utils/oauth/github-copilot.d.ts +24 -0
- package/dist/utils/oauth/github-copilot.d.ts.map +1 -0
- package/dist/utils/oauth/github-copilot.js +279 -0
- package/dist/utils/oauth/github-copilot.js.map +1 -0
- package/dist/utils/oauth/index.d.ts +45 -0
- package/dist/utils/oauth/index.d.ts.map +1 -0
- package/dist/utils/oauth/index.js +99 -0
- package/dist/utils/oauth/index.js.map +1 -0
- package/dist/utils/oauth/oauth-page.d.ts +3 -0
- package/dist/utils/oauth/oauth-page.d.ts.map +1 -0
- package/dist/utils/oauth/oauth-page.js +134 -0
- package/dist/utils/oauth/oauth-page.js.map +1 -0
- package/dist/utils/oauth/openai-codex.d.ts +34 -0
- package/dist/utils/oauth/openai-codex.d.ts.map +1 -0
- package/dist/utils/oauth/openai-codex.js +385 -0
- package/dist/utils/oauth/openai-codex.js.map +1 -0
- package/dist/utils/oauth/pkce.d.ts +13 -0
- package/dist/utils/oauth/pkce.d.ts.map +1 -0
- package/dist/utils/oauth/pkce.js +26 -0
- package/dist/utils/oauth/pkce.js.map +1 -0
- package/dist/utils/oauth/types.d.ts +57 -0
- package/dist/utils/oauth/types.d.ts.map +1 -0
- package/dist/utils/oauth/types.js +2 -0
- package/dist/utils/oauth/types.js.map +1 -0
- package/dist/utils/overflow.d.ts +55 -0
- package/dist/utils/overflow.d.ts.map +1 -0
- package/dist/utils/overflow.js +144 -0
- package/dist/utils/overflow.js.map +1 -0
- package/dist/utils/sanitize-unicode.d.ts +22 -0
- package/dist/utils/sanitize-unicode.d.ts.map +1 -0
- package/dist/utils/sanitize-unicode.js +26 -0
- package/dist/utils/sanitize-unicode.js.map +1 -0
- package/dist/utils/stream-failure.d.ts +55 -0
- package/dist/utils/stream-failure.d.ts.map +1 -0
- package/dist/utils/stream-failure.js +179 -0
- package/dist/utils/stream-failure.js.map +1 -0
- package/dist/utils/typebox-helpers.d.ts +17 -0
- package/dist/utils/typebox-helpers.d.ts.map +1 -0
- package/dist/utils/typebox-helpers.js +21 -0
- package/dist/utils/typebox-helpers.js.map +1 -0
- package/dist/utils/validation.d.ts +18 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +281 -0
- package/dist/utils/validation.js.map +1 -0
- package/package.json +113 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-failure.js","sourceRoot":"","sources":["../../src/utils/stream-failure.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,OAAO,EAAE,gCA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SAAS,CAAC,aAAa,CAAC,CAAC;AAErC;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAClC,KAAoD,EACpD,MAAwB,EACxB,KAAc,EACP;IACP,IAAI,MAAM,CAAC,UAAU,KAAK,OAAO;QAAE,OAAO;IAC1C,MAAM,IAAI,GAAG,wBAAwB,CAAC,KAAK,CAAC,CAAC;IAC7C,gCAAgC,CAAC,MAAM,EAAE;QACxC,IAAI,EAAE,yBAAyB;QAC/B,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;QACrB,KAAK,EAAE,sBAAsB,CAAC,KAAK,CAAC;QACpC,OAAO,EAAE,EAAE,GAAG,IAAI,EAAE;KACpB,CAAC,CAAC;IACH,MAAM,UAAU,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC1E,GAAG,CAAC,KAAK,CAAC,yBAAyB,EAAE;QACpC,QAAQ,EAAE,KAAK,CAAC,QAAQ;QACxB,KAAK,EAAE,KAAK,CAAC,EAAE;QACf,GAAG,EAAE,KAAK,CAAC,GAAG;QACd,IAAI,EAAE,IAAI,CAAC,IAAI;QACf,iBAAiB,EAAE,IAAI,CAAC,iBAAiB;QACzC,MAAM,EAAE,IAAI,CAAC,MAAM;QACnB,SAAS,EAAE,IAAI,CAAC,SAAS;QACzB,OAAO,EAAE,MAAM,CAAC,YAAY;QAC5B,6EAA6E;QAC7E,KAAK,EAAE,UAAU,KAAK,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,kBAAkB,CAAC,UAAU,CAAC;KACtF,CAAC,CAAC;AAAA,CACH","sourcesContent":["import { getLogger } from \"../log.js\";\nimport type { AssistantMessage } from \"../types.js\";\nimport { appendAssistantMessageDiagnostic, extractDiagnosticError } from \"./diagnostics.js\";\n\n/**\n * Shared classification and reporting for provider stream failures, so no\n * provider collapses a specific cause (refusal, safety filter, overload, ...)\n * into a generic string before it is logged and persisted.\n */\n\nexport type StreamFailureKind =\n\t| \"refusal\"\n\t| \"safety\"\n\t| \"overloaded\"\n\t| \"rate_limit\"\n\t| \"server_error\"\n\t| \"auth\"\n\t| \"invalid_request\"\n\t| \"malformed_response\"\n\t| \"unknown\";\n\nexport interface StreamFailureInfo {\n\tkind: StreamFailureKind;\n\t/** Provider's own error/stop identifier, e.g. \"overloaded_error\" or \"SAFETY\". */\n\tproviderErrorType?: string;\n\tstatus?: number;\n\trequestId?: string;\n\t/** Truncated raw provider payload for post-mortems. */\n\traw?: string;\n}\n\nexport class StreamFailureError extends Error {\n\treadonly info: StreamFailureInfo;\n\n\tconstructor(message: string, info: StreamFailureInfo) {\n\t\tsuper(message);\n\t\tthis.name = \"StreamFailureError\";\n\t\tthis.info = info;\n\t}\n}\n\nconst KIND_MESSAGES: Record<StreamFailureKind, string> = {\n\trefusal: \"Model refused to respond\",\n\tsafety: \"Response blocked by provider safety filters\",\n\toverloaded: \"Provider overloaded\",\n\trate_limit: \"Provider rate limit exceeded\",\n\tserver_error: \"Provider server error\",\n\tauth: \"Provider authentication failed\",\n\tinvalid_request: \"Provider rejected the request\",\n\tmalformed_response: \"Provider returned a malformed response\",\n\tunknown: \"Provider stream failed\",\n};\n\n/** Build a user-facing message like \"Provider overloaded (overloaded_error, 529) [request_id: req_abc]\". */\nexport function streamFailureMessage(info: StreamFailureInfo, detail?: string): string {\n\tconst qualifiers = [info.providerErrorType, info.status !== undefined ? String(info.status) : undefined]\n\t\t.filter(Boolean)\n\t\t.join(\", \");\n\tlet message = KIND_MESSAGES[info.kind];\n\tif (qualifiers) message += ` (${qualifiers})`;\n\tif (detail) message += `: ${detail}`;\n\tif (info.requestId) message += ` [request_id: ${info.requestId}]`;\n\treturn message;\n}\n\nexport function classifyStreamFailure(providerErrorType?: string, status?: number): StreamFailureKind {\n\tconst type = providerErrorType?.toLowerCase() ?? \"\";\n\tif (type === \"refusal\") return \"refusal\";\n\tif (/sensitive|safety|prohibited_content|blocklist|spii|recitation|content.?filter|guardrail|flagged/.test(type)) {\n\t\treturn \"safety\";\n\t}\n\tif (type.includes(\"overloaded\") || status === 529) return \"overloaded\";\n\tif (type.includes(\"rate_limit\") || type.includes(\"throttl\") || status === 429) return \"rate_limit\";\n\tif (/authentication|permission|unauthorized/.test(type) || status === 401 || status === 403) return \"auth\";\n\tif (type.includes(\"invalid_request\") || type.includes(\"not_found_error\") || status === 400 || status === 404) {\n\t\treturn \"invalid_request\";\n\t}\n\tif (type.includes(\"malformed\")) return \"malformed_response\";\n\tif (\n\t\ttype.includes(\"api_error\") ||\n\t\ttype.includes(\"server_error\") ||\n\t\ttype.includes(\"unavailable\") ||\n\t\t(status !== undefined && status >= 500)\n\t) {\n\t\treturn \"server_error\";\n\t}\n\treturn \"unknown\";\n}\n\n/**\n * Failure for a stream that terminated with a provider stop/finish reason that\n * maps to \"error\" (e.g. Anthropic \"refusal\", Gemini \"SAFETY\"). Providers call\n * this instead of throwing a generic error, so the raw reason survives.\n */\nexport function streamFailureFromStopReason(\n\trawStopReason: string | undefined,\n\textra?: Pick<StreamFailureInfo, \"requestId\">,\n): StreamFailureError {\n\tconst info: StreamFailureInfo = {\n\t\tkind: rawStopReason ? classifyStreamFailure(rawStopReason) : \"unknown\",\n\t\tproviderErrorType: rawStopReason,\n\t\trequestId: extra?.requestId,\n\t};\n\tif (info.kind === \"unknown\" && /malformed/i.test(rawStopReason ?? \"\")) info.kind = \"malformed_response\";\n\tconst message = rawStopReason\n\t\t? streamFailureMessage(info)\n\t\t: streamFailureMessage(info, \"stream ended with an error and no stop reason\");\n\treturn new StreamFailureError(message, info);\n}\n\nconst MAX_RAW_LENGTH = 2000;\n\nexport function truncateRawPayload(raw: string): string {\n\treturn raw.length > MAX_RAW_LENGTH ? `${raw.slice(0, MAX_RAW_LENGTH)}…` : raw;\n}\n\nfunction extractStreamFailureParts(error: unknown): { info: StreamFailureInfo; detail?: string } {\n\tif (error instanceof StreamFailureError) return { info: error.info };\n\tif (!(error instanceof Error)) return { info: { kind: \"unknown\" } };\n\n\tconst err = error as Error & {\n\t\tstatus?: unknown;\n\t\tstatusCode?: unknown;\n\t\tcode?: unknown;\n\t\trequestID?: unknown;\n\t\trequest_id?: unknown;\n\t\theaders?: unknown;\n\t\terror?: unknown;\n\t\t$metadata?: { requestId?: unknown };\n\t};\n\n\tconst status =\n\t\ttypeof err.status === \"number\" ? err.status : typeof err.statusCode === \"number\" ? err.statusCode : undefined;\n\n\t// Error bodies come nested differently per SDK: Anthropic/OpenAI expose\n\t// `error.error = {type|code, message}` (sometimes doubly nested).\n\tlet body = err.error as { type?: unknown; code?: unknown; message?: unknown; error?: unknown } | undefined;\n\tif (body && typeof body === \"object\" && body.error && typeof body.error === \"object\") {\n\t\tbody = body.error as { type?: unknown; code?: unknown; message?: unknown };\n\t}\n\tconst bodyType = body && typeof body === \"object\" ? (body.type ?? body.code) : undefined;\n\tconst bodyMessage = body && typeof body === \"object\" ? body.message : undefined;\n\tconst providerErrorType =\n\t\ttypeof bodyType === \"string\"\n\t\t\t? bodyType\n\t\t\t: typeof err.code === \"string\"\n\t\t\t\t? err.code\n\t\t\t\t: err.name !== \"Error\" && err.name !== \"StreamFailureError\"\n\t\t\t\t\t? err.name\n\t\t\t\t\t: undefined;\n\n\tconst headers = err.headers;\n\tconst headerRequestId =\n\t\theaders && typeof (headers as Headers).get === \"function\"\n\t\t\t? ((headers as Headers).get(\"request-id\") ?? (headers as Headers).get(\"x-request-id\"))\n\t\t\t: headers && typeof headers === \"object\"\n\t\t\t\t? ((headers as Record<string, unknown>)[\"request-id\"] ??\n\t\t\t\t\t(headers as Record<string, unknown>)[\"x-request-id\"])\n\t\t\t\t: undefined;\n\tconst rawRequestId = err.requestID ?? err.request_id ?? err.$metadata?.requestId ?? headerRequestId;\n\tconst requestId = typeof rawRequestId === \"string\" ? rawRequestId : undefined;\n\n\treturn {\n\t\tinfo: {\n\t\t\tkind: classifyStreamFailure(providerErrorType ?? error.message, status),\n\t\t\tproviderErrorType,\n\t\t\tstatus,\n\t\t\trequestId,\n\t\t},\n\t\tdetail: typeof bodyMessage === \"string\" ? bodyMessage : undefined,\n\t};\n}\n\n/**\n * Best-effort extraction of structured failure info from any thrown value:\n * StreamFailureError, provider SDK errors (Anthropic/OpenAI APIError, AWS SDK\n * exceptions, Google ApiError), or plain errors.\n */\nexport function extractStreamFailureInfo(error: unknown): StreamFailureInfo {\n\treturn extractStreamFailureParts(error).info;\n}\n\n/**\n * User-facing message for a thrown stream error: a classified one-liner with\n * the provider's own short message, never the raw payload/trace. Unrecognized\n * errors pass through verbatim so their text (which downstream retry matching\n * may depend on) is preserved.\n */\nexport function formatStreamFailureMessage(error: unknown): string {\n\tif (error instanceof StreamFailureError) return error.message;\n\tconst { info, detail } = extractStreamFailureParts(error);\n\tif (info.kind === \"unknown\") {\n\t\treturn error instanceof Error ? error.message : JSON.stringify(error);\n\t}\n\treturn streamFailureMessage(info, detail);\n}\n\nconst log = getLogger(\"ai.provider\");\n\n/**\n * Record a terminal stream failure on the message (structured diagnostic that\n * persists to session JSONL) and emit one structured log line. Call from the\n * provider's terminal catch after stopReason/errorMessage are set; no-op for\n * user-initiated aborts.\n */\nexport function recordStreamFailure(\n\tmodel: { provider: string; id: string; api: string },\n\toutput: AssistantMessage,\n\terror: unknown,\n): void {\n\tif (output.stopReason !== \"error\") return;\n\tconst info = extractStreamFailureInfo(error);\n\tappendAssistantMessageDiagnostic(output, {\n\t\ttype: \"provider_stream_failure\",\n\t\ttimestamp: Date.now(),\n\t\terror: extractDiagnosticError(error),\n\t\tdetails: { ...info },\n\t});\n\tconst rawMessage = error instanceof Error ? error.message : String(error);\n\tlog.error(\"provider stream failure\", {\n\t\tprovider: model.provider,\n\t\tmodel: model.id,\n\t\tapi: model.api,\n\t\tkind: info.kind,\n\t\tproviderErrorType: info.providerErrorType,\n\t\tstatus: info.status,\n\t\trequestId: info.requestId,\n\t\tmessage: output.errorMessage,\n\t\t// errorMessage is user-facing and concise; keep the raw cause for debugging.\n\t\tcause: rawMessage === output.errorMessage ? undefined : truncateRawPayload(rawMessage),\n\t});\n}\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type TUnsafe } from "typebox";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a string enum schema compatible with Google's API and other providers
|
|
4
|
+
* that don't support anyOf/const patterns.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const OperationSchema = StringEnum(["add", "subtract", "multiply", "divide"], {
|
|
8
|
+
* description: "The operation to perform"
|
|
9
|
+
* });
|
|
10
|
+
*
|
|
11
|
+
* type Operation = Static<typeof OperationSchema>; // "add" | "subtract" | "multiply" | "divide"
|
|
12
|
+
*/
|
|
13
|
+
export declare function StringEnum<T extends readonly string[]>(values: T, options?: {
|
|
14
|
+
description?: string;
|
|
15
|
+
default?: T[number];
|
|
16
|
+
}): TUnsafe<T[number]>;
|
|
17
|
+
//# sourceMappingURL=typebox-helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typebox-helpers.d.ts","sourceRoot":"","sources":["../../src/utils/typebox-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAQ,MAAM,SAAS,CAAC;AAE7C;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,SAAS,MAAM,EAAE,EACrD,MAAM,EAAE,CAAC,EACT,OAAO,CAAC,EAAE;IAAE,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAA;CAAE,GACrD,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAOpB","sourcesContent":["import { type TUnsafe, Type } from \"typebox\";\n\n/**\n * Creates a string enum schema compatible with Google's API and other providers\n * that don't support anyOf/const patterns.\n *\n * @example\n * const OperationSchema = StringEnum([\"add\", \"subtract\", \"multiply\", \"divide\"], {\n * description: \"The operation to perform\"\n * });\n *\n * type Operation = Static<typeof OperationSchema>; // \"add\" | \"subtract\" | \"multiply\" | \"divide\"\n */\nexport function StringEnum<T extends readonly string[]>(\n\tvalues: T,\n\toptions?: { description?: string; default?: T[number] },\n): TUnsafe<T[number]> {\n\treturn Type.Unsafe<T[number]>({\n\t\ttype: \"string\",\n\t\tenum: values as any,\n\t\t...(options?.description && { description: options.description }),\n\t\t...(options?.default && { default: options.default }),\n\t});\n}\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Type } from "typebox";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a string enum schema compatible with Google's API and other providers
|
|
4
|
+
* that don't support anyOf/const patterns.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const OperationSchema = StringEnum(["add", "subtract", "multiply", "divide"], {
|
|
8
|
+
* description: "The operation to perform"
|
|
9
|
+
* });
|
|
10
|
+
*
|
|
11
|
+
* type Operation = Static<typeof OperationSchema>; // "add" | "subtract" | "multiply" | "divide"
|
|
12
|
+
*/
|
|
13
|
+
export function StringEnum(values, options) {
|
|
14
|
+
return Type.Unsafe({
|
|
15
|
+
type: "string",
|
|
16
|
+
enum: values,
|
|
17
|
+
...(options?.description && { description: options.description }),
|
|
18
|
+
...(options?.default && { default: options.default }),
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=typebox-helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typebox-helpers.js","sourceRoot":"","sources":["../../src/utils/typebox-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,IAAI,EAAE,MAAM,SAAS,CAAC;AAE7C;;;;;;;;;;GAUG;AACH,MAAM,UAAU,UAAU,CACzB,MAAS,EACT,OAAuD,EAClC;IACrB,OAAO,IAAI,CAAC,MAAM,CAAY;QAC7B,IAAI,EAAE,QAAQ;QACd,IAAI,EAAE,MAAa;QACnB,GAAG,CAAC,OAAO,EAAE,WAAW,IAAI,EAAE,WAAW,EAAE,OAAO,CAAC,WAAW,EAAE,CAAC;QACjE,GAAG,CAAC,OAAO,EAAE,OAAO,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,CAAC;KACrD,CAAC,CAAC;AAAA,CACH","sourcesContent":["import { type TUnsafe, Type } from \"typebox\";\n\n/**\n * Creates a string enum schema compatible with Google's API and other providers\n * that don't support anyOf/const patterns.\n *\n * @example\n * const OperationSchema = StringEnum([\"add\", \"subtract\", \"multiply\", \"divide\"], {\n * description: \"The operation to perform\"\n * });\n *\n * type Operation = Static<typeof OperationSchema>; // \"add\" | \"subtract\" | \"multiply\" | \"divide\"\n */\nexport function StringEnum<T extends readonly string[]>(\n\tvalues: T,\n\toptions?: { description?: string; default?: T[number] },\n): TUnsafe<T[number]> {\n\treturn Type.Unsafe<T[number]>({\n\t\ttype: \"string\",\n\t\tenum: values as any,\n\t\t...(options?.description && { description: options.description }),\n\t\t...(options?.default && { default: options.default }),\n\t});\n}\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Tool, ToolCall } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Finds a tool by name and validates the tool call arguments against its TypeBox schema
|
|
4
|
+
* @param tools Array of tool definitions
|
|
5
|
+
* @param toolCall The tool call from the LLM
|
|
6
|
+
* @returns The validated arguments
|
|
7
|
+
* @throws Error if tool is not found or validation fails
|
|
8
|
+
*/
|
|
9
|
+
export declare function validateToolCall(tools: Tool[], toolCall: ToolCall): any;
|
|
10
|
+
/**
|
|
11
|
+
* Validates tool call arguments against the tool's TypeBox schema
|
|
12
|
+
* @param tool The tool definition with TypeBox schema
|
|
13
|
+
* @param toolCall The tool call from the LLM
|
|
14
|
+
* @returns The validated (and potentially coerced) arguments
|
|
15
|
+
* @throws Error with formatted message if validation fails
|
|
16
|
+
*/
|
|
17
|
+
export declare function validateToolArguments(tool: Tool, toolCall: ToolCall): any;
|
|
18
|
+
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/utils/validation.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AA0QlD;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,QAAQ,EAAE,QAAQ,GAAG,GAAG,CAMvE;AAED;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,GAAG,GAAG,CAgCzE","sourcesContent":["import { Compile } from \"typebox/compile\";\nimport type { TLocalizedValidationError } from \"typebox/error\";\nimport { Value } from \"typebox/value\";\nimport type { Tool, ToolCall } from \"../types.js\";\n\nconst validatorCache = new WeakMap<object, ReturnType<typeof Compile>>();\nconst TYPEBOX_KIND = Symbol.for(\"TypeBox.Kind\");\n\ninterface JsonSchemaObject {\n\ttype?: string | string[];\n\tproperties?: Record<string, JsonSchemaObject>;\n\titems?: JsonSchemaObject | JsonSchemaObject[];\n\tadditionalProperties?: boolean | JsonSchemaObject;\n\tallOf?: JsonSchemaObject[];\n\tanyOf?: JsonSchemaObject[];\n\toneOf?: JsonSchemaObject[];\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === \"object\" && value !== null;\n}\n\nfunction isJsonSchemaObject(value: unknown): value is JsonSchemaObject {\n\treturn isRecord(value);\n}\n\nfunction hasTypeBoxMetadata(schema: unknown): boolean {\n\treturn isRecord(schema) && Object.getOwnPropertySymbols(schema).includes(TYPEBOX_KIND);\n}\n\nfunction getSchemaTypes(schema: JsonSchemaObject): string[] {\n\tif (typeof schema.type === \"string\") {\n\t\treturn [schema.type];\n\t}\n\tif (Array.isArray(schema.type)) {\n\t\treturn schema.type.filter((type): type is string => typeof type === \"string\");\n\t}\n\treturn [];\n}\n\nfunction matchesJsonType(value: unknown, type: string): boolean {\n\tswitch (type) {\n\t\tcase \"number\":\n\t\t\treturn typeof value === \"number\";\n\t\tcase \"integer\":\n\t\t\treturn typeof value === \"number\" && Number.isInteger(value);\n\t\tcase \"boolean\":\n\t\t\treturn typeof value === \"boolean\";\n\t\tcase \"string\":\n\t\t\treturn typeof value === \"string\";\n\t\tcase \"null\":\n\t\t\treturn value === null;\n\t\tcase \"array\":\n\t\t\treturn Array.isArray(value);\n\t\tcase \"object\":\n\t\t\treturn isRecord(value) && !Array.isArray(value);\n\t\tdefault:\n\t\t\treturn false;\n\t}\n}\n\nfunction isValidatorSchema(value: unknown): value is Tool[\"parameters\"] {\n\treturn isRecord(value);\n}\n\nfunction getSubSchemaValidator(schema: JsonSchemaObject): ReturnType<typeof Compile> | undefined {\n\tif (!isValidatorSchema(schema)) {\n\t\treturn undefined;\n\t}\n\ttry {\n\t\treturn getValidator(schema);\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction coercePrimitiveByType(value: unknown, type: string): unknown {\n\tswitch (type) {\n\t\tcase \"number\": {\n\t\t\tif (value === null) {\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t\tif (typeof value === \"string\" && value.trim() !== \"\") {\n\t\t\t\tconst parsed = Number(value);\n\t\t\t\tif (Number.isFinite(parsed)) {\n\t\t\t\t\treturn parsed;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (typeof value === \"boolean\") {\n\t\t\t\treturn value ? 1 : 0;\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tcase \"integer\": {\n\t\t\tif (value === null) {\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t\tif (typeof value === \"string\" && value.trim() !== \"\") {\n\t\t\t\tconst parsed = Number(value);\n\t\t\t\tif (Number.isInteger(parsed)) {\n\t\t\t\t\treturn parsed;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (typeof value === \"boolean\") {\n\t\t\t\treturn value ? 1 : 0;\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tcase \"boolean\": {\n\t\t\tif (value === null) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tif (typeof value === \"string\") {\n\t\t\t\tif (value === \"true\") {\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tif (value === \"false\") {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (typeof value === \"number\") {\n\t\t\t\tif (value === 1) {\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tif (value === 0) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tcase \"string\": {\n\t\t\tif (value === null) {\n\t\t\t\treturn \"\";\n\t\t\t}\n\t\t\tif (typeof value === \"number\" || typeof value === \"boolean\") {\n\t\t\t\treturn String(value);\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tcase \"null\": {\n\t\t\tif (value === \"\" || value === 0 || value === false) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tdefault:\n\t\t\treturn value;\n\t}\n}\n\nfunction applySchemaObjectCoercion(value: Record<string, unknown>, schema: JsonSchemaObject): void {\n\tconst properties = schema.properties;\n\tconst definedKeys = new Set<string>(properties ? Object.keys(properties) : []);\n\n\tif (properties) {\n\t\tfor (const [key, propertySchema] of Object.entries(properties)) {\n\t\t\tif (!(key in value)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tvalue[key] = coerceWithJsonSchema(value[key], propertySchema);\n\t\t}\n\t}\n\n\tif (schema.additionalProperties && isJsonSchemaObject(schema.additionalProperties)) {\n\t\tfor (const [key, propertyValue] of Object.entries(value)) {\n\t\t\tif (definedKeys.has(key)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tvalue[key] = coerceWithJsonSchema(propertyValue, schema.additionalProperties);\n\t\t}\n\t}\n}\n\nfunction applySchemaArrayCoercion(value: unknown[], schema: JsonSchemaObject): void {\n\tif (Array.isArray(schema.items)) {\n\t\tfor (let index = 0; index < value.length; index++) {\n\t\t\tconst itemSchema = schema.items[index];\n\t\t\tif (!itemSchema) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tvalue[index] = coerceWithJsonSchema(value[index], itemSchema);\n\t\t}\n\t\treturn;\n\t}\n\n\tif (isJsonSchemaObject(schema.items)) {\n\t\tfor (let index = 0; index < value.length; index++) {\n\t\t\tvalue[index] = coerceWithJsonSchema(value[index], schema.items);\n\t\t}\n\t}\n}\n\nfunction coerceWithUnionSchema(value: unknown, schemas: JsonSchemaObject[]): unknown {\n\tfor (const schema of schemas) {\n\t\tconst candidate = structuredClone(value);\n\t\tconst coerced = coerceWithJsonSchema(candidate, schema);\n\t\tconst validator = getSubSchemaValidator(schema);\n\t\tif (validator?.Check(coerced)) {\n\t\t\treturn coerced;\n\t\t}\n\t}\n\treturn value;\n}\n\nfunction coerceWithJsonSchema(value: unknown, schema: JsonSchemaObject): unknown {\n\tlet nextValue = value;\n\n\tif (Array.isArray(schema.allOf)) {\n\t\tfor (const nested of schema.allOf) {\n\t\t\tnextValue = coerceWithJsonSchema(nextValue, nested);\n\t\t}\n\t}\n\n\tif (Array.isArray(schema.anyOf)) {\n\t\tnextValue = coerceWithUnionSchema(nextValue, schema.anyOf);\n\t}\n\n\tif (Array.isArray(schema.oneOf)) {\n\t\tnextValue = coerceWithUnionSchema(nextValue, schema.oneOf);\n\t}\n\n\tconst schemaTypes = getSchemaTypes(schema);\n\tconst matchesUnionMember =\n\t\tschemaTypes.length > 1 && schemaTypes.some((schemaType) => matchesJsonType(nextValue, schemaType));\n\tif (schemaTypes.length > 0 && !matchesUnionMember) {\n\t\tfor (const schemaType of schemaTypes) {\n\t\t\tconst candidate = coercePrimitiveByType(nextValue, schemaType);\n\t\t\tif (candidate !== nextValue) {\n\t\t\t\tnextValue = candidate;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (schemaTypes.includes(\"object\") && isRecord(nextValue) && !Array.isArray(nextValue)) {\n\t\tapplySchemaObjectCoercion(nextValue, schema);\n\t}\n\n\tif (schemaTypes.includes(\"array\") && Array.isArray(nextValue)) {\n\t\tapplySchemaArrayCoercion(nextValue, schema);\n\t}\n\n\treturn nextValue;\n}\n\nfunction getValidator(schema: Tool[\"parameters\"]): ReturnType<typeof Compile> {\n\tconst key = schema as object;\n\tconst cached = validatorCache.get(key);\n\tif (cached) {\n\t\treturn cached;\n\t}\n\tconst validator = Compile(schema);\n\tvalidatorCache.set(key, validator);\n\treturn validator;\n}\n\nfunction formatValidationPath(error: TLocalizedValidationError): string {\n\tif (error.keyword === \"required\") {\n\t\tconst requiredProperties = (error.params as { requiredProperties?: string[] }).requiredProperties;\n\t\tconst requiredProperty = requiredProperties?.[0];\n\t\tif (requiredProperty) {\n\t\t\tconst basePath = error.instancePath.replace(/^\\//, \"\").replace(/\\//g, \".\");\n\t\t\treturn basePath ? `${basePath}.${requiredProperty}` : requiredProperty;\n\t\t}\n\t}\n\tconst path = error.instancePath.replace(/^\\//, \"\").replace(/\\//g, \".\");\n\treturn path || \"root\";\n}\n\n/**\n * Finds a tool by name and validates the tool call arguments against its TypeBox schema\n * @param tools Array of tool definitions\n * @param toolCall The tool call from the LLM\n * @returns The validated arguments\n * @throws Error if tool is not found or validation fails\n */\nexport function validateToolCall(tools: Tool[], toolCall: ToolCall): any {\n\tconst tool = tools.find((t) => t.name === toolCall.name);\n\tif (!tool) {\n\t\tthrow new Error(`Tool \"${toolCall.name}\" not found`);\n\t}\n\treturn validateToolArguments(tool, toolCall);\n}\n\n/**\n * Validates tool call arguments against the tool's TypeBox schema\n * @param tool The tool definition with TypeBox schema\n * @param toolCall The tool call from the LLM\n * @returns The validated (and potentially coerced) arguments\n * @throws Error with formatted message if validation fails\n */\nexport function validateToolArguments(tool: Tool, toolCall: ToolCall): any {\n\tconst args = structuredClone(toolCall.arguments);\n\tValue.Convert(tool.parameters, args);\n\n\tconst validator = getValidator(tool.parameters);\n\tif (!hasTypeBoxMetadata(tool.parameters) && isJsonSchemaObject(tool.parameters)) {\n\t\tconst coerced = coerceWithJsonSchema(args, tool.parameters);\n\t\tif (coerced !== args) {\n\t\t\tif (isRecord(args) && isRecord(coerced)) {\n\t\t\t\tfor (const key of Object.keys(args)) {\n\t\t\t\t\tdelete args[key];\n\t\t\t\t}\n\t\t\t\tObject.assign(args, coerced);\n\t\t\t} else {\n\t\t\t\treturn validator.Check(coerced) ? coerced : args;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (validator.Check(args)) {\n\t\treturn args;\n\t}\n\n\tconst errors =\n\t\tvalidator\n\t\t\t.Errors(args)\n\t\t\t.map((error) => ` - ${formatValidationPath(error)}: ${error.message}`)\n\t\t\t.join(\"\\n\") || \"Unknown validation error\";\n\n\tconst errorMessage = `Validation failed for tool \"${toolCall.name}\":\\n${errors}\\n\\nReceived arguments:\\n${JSON.stringify(toolCall.arguments, null, 2)}`;\n\n\tthrow new Error(errorMessage);\n}\n"]}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { Compile } from "typebox/compile";
|
|
2
|
+
import { Value } from "typebox/value";
|
|
3
|
+
const validatorCache = new WeakMap();
|
|
4
|
+
const TYPEBOX_KIND = Symbol.for("TypeBox.Kind");
|
|
5
|
+
function isRecord(value) {
|
|
6
|
+
return typeof value === "object" && value !== null;
|
|
7
|
+
}
|
|
8
|
+
function isJsonSchemaObject(value) {
|
|
9
|
+
return isRecord(value);
|
|
10
|
+
}
|
|
11
|
+
function hasTypeBoxMetadata(schema) {
|
|
12
|
+
return isRecord(schema) && Object.getOwnPropertySymbols(schema).includes(TYPEBOX_KIND);
|
|
13
|
+
}
|
|
14
|
+
function getSchemaTypes(schema) {
|
|
15
|
+
if (typeof schema.type === "string") {
|
|
16
|
+
return [schema.type];
|
|
17
|
+
}
|
|
18
|
+
if (Array.isArray(schema.type)) {
|
|
19
|
+
return schema.type.filter((type) => typeof type === "string");
|
|
20
|
+
}
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
function matchesJsonType(value, type) {
|
|
24
|
+
switch (type) {
|
|
25
|
+
case "number":
|
|
26
|
+
return typeof value === "number";
|
|
27
|
+
case "integer":
|
|
28
|
+
return typeof value === "number" && Number.isInteger(value);
|
|
29
|
+
case "boolean":
|
|
30
|
+
return typeof value === "boolean";
|
|
31
|
+
case "string":
|
|
32
|
+
return typeof value === "string";
|
|
33
|
+
case "null":
|
|
34
|
+
return value === null;
|
|
35
|
+
case "array":
|
|
36
|
+
return Array.isArray(value);
|
|
37
|
+
case "object":
|
|
38
|
+
return isRecord(value) && !Array.isArray(value);
|
|
39
|
+
default:
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function isValidatorSchema(value) {
|
|
44
|
+
return isRecord(value);
|
|
45
|
+
}
|
|
46
|
+
function getSubSchemaValidator(schema) {
|
|
47
|
+
if (!isValidatorSchema(schema)) {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
return getValidator(schema);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function coercePrimitiveByType(value, type) {
|
|
58
|
+
switch (type) {
|
|
59
|
+
case "number": {
|
|
60
|
+
if (value === null) {
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
64
|
+
const parsed = Number(value);
|
|
65
|
+
if (Number.isFinite(parsed)) {
|
|
66
|
+
return parsed;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (typeof value === "boolean") {
|
|
70
|
+
return value ? 1 : 0;
|
|
71
|
+
}
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
case "integer": {
|
|
75
|
+
if (value === null) {
|
|
76
|
+
return 0;
|
|
77
|
+
}
|
|
78
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
79
|
+
const parsed = Number(value);
|
|
80
|
+
if (Number.isInteger(parsed)) {
|
|
81
|
+
return parsed;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (typeof value === "boolean") {
|
|
85
|
+
return value ? 1 : 0;
|
|
86
|
+
}
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
case "boolean": {
|
|
90
|
+
if (value === null) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
if (typeof value === "string") {
|
|
94
|
+
if (value === "true") {
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
if (value === "false") {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (typeof value === "number") {
|
|
102
|
+
if (value === 1) {
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
if (value === 0) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return value;
|
|
110
|
+
}
|
|
111
|
+
case "string": {
|
|
112
|
+
if (value === null) {
|
|
113
|
+
return "";
|
|
114
|
+
}
|
|
115
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
116
|
+
return String(value);
|
|
117
|
+
}
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
case "null": {
|
|
121
|
+
if (value === "" || value === 0 || value === false) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
return value;
|
|
125
|
+
}
|
|
126
|
+
default:
|
|
127
|
+
return value;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function applySchemaObjectCoercion(value, schema) {
|
|
131
|
+
const properties = schema.properties;
|
|
132
|
+
const definedKeys = new Set(properties ? Object.keys(properties) : []);
|
|
133
|
+
if (properties) {
|
|
134
|
+
for (const [key, propertySchema] of Object.entries(properties)) {
|
|
135
|
+
if (!(key in value)) {
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
value[key] = coerceWithJsonSchema(value[key], propertySchema);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (schema.additionalProperties && isJsonSchemaObject(schema.additionalProperties)) {
|
|
142
|
+
for (const [key, propertyValue] of Object.entries(value)) {
|
|
143
|
+
if (definedKeys.has(key)) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
value[key] = coerceWithJsonSchema(propertyValue, schema.additionalProperties);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function applySchemaArrayCoercion(value, schema) {
|
|
151
|
+
if (Array.isArray(schema.items)) {
|
|
152
|
+
for (let index = 0; index < value.length; index++) {
|
|
153
|
+
const itemSchema = schema.items[index];
|
|
154
|
+
if (!itemSchema) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
value[index] = coerceWithJsonSchema(value[index], itemSchema);
|
|
158
|
+
}
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (isJsonSchemaObject(schema.items)) {
|
|
162
|
+
for (let index = 0; index < value.length; index++) {
|
|
163
|
+
value[index] = coerceWithJsonSchema(value[index], schema.items);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
function coerceWithUnionSchema(value, schemas) {
|
|
168
|
+
for (const schema of schemas) {
|
|
169
|
+
const candidate = structuredClone(value);
|
|
170
|
+
const coerced = coerceWithJsonSchema(candidate, schema);
|
|
171
|
+
const validator = getSubSchemaValidator(schema);
|
|
172
|
+
if (validator?.Check(coerced)) {
|
|
173
|
+
return coerced;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return value;
|
|
177
|
+
}
|
|
178
|
+
function coerceWithJsonSchema(value, schema) {
|
|
179
|
+
let nextValue = value;
|
|
180
|
+
if (Array.isArray(schema.allOf)) {
|
|
181
|
+
for (const nested of schema.allOf) {
|
|
182
|
+
nextValue = coerceWithJsonSchema(nextValue, nested);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (Array.isArray(schema.anyOf)) {
|
|
186
|
+
nextValue = coerceWithUnionSchema(nextValue, schema.anyOf);
|
|
187
|
+
}
|
|
188
|
+
if (Array.isArray(schema.oneOf)) {
|
|
189
|
+
nextValue = coerceWithUnionSchema(nextValue, schema.oneOf);
|
|
190
|
+
}
|
|
191
|
+
const schemaTypes = getSchemaTypes(schema);
|
|
192
|
+
const matchesUnionMember = schemaTypes.length > 1 && schemaTypes.some((schemaType) => matchesJsonType(nextValue, schemaType));
|
|
193
|
+
if (schemaTypes.length > 0 && !matchesUnionMember) {
|
|
194
|
+
for (const schemaType of schemaTypes) {
|
|
195
|
+
const candidate = coercePrimitiveByType(nextValue, schemaType);
|
|
196
|
+
if (candidate !== nextValue) {
|
|
197
|
+
nextValue = candidate;
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (schemaTypes.includes("object") && isRecord(nextValue) && !Array.isArray(nextValue)) {
|
|
203
|
+
applySchemaObjectCoercion(nextValue, schema);
|
|
204
|
+
}
|
|
205
|
+
if (schemaTypes.includes("array") && Array.isArray(nextValue)) {
|
|
206
|
+
applySchemaArrayCoercion(nextValue, schema);
|
|
207
|
+
}
|
|
208
|
+
return nextValue;
|
|
209
|
+
}
|
|
210
|
+
function getValidator(schema) {
|
|
211
|
+
const key = schema;
|
|
212
|
+
const cached = validatorCache.get(key);
|
|
213
|
+
if (cached) {
|
|
214
|
+
return cached;
|
|
215
|
+
}
|
|
216
|
+
const validator = Compile(schema);
|
|
217
|
+
validatorCache.set(key, validator);
|
|
218
|
+
return validator;
|
|
219
|
+
}
|
|
220
|
+
function formatValidationPath(error) {
|
|
221
|
+
if (error.keyword === "required") {
|
|
222
|
+
const requiredProperties = error.params.requiredProperties;
|
|
223
|
+
const requiredProperty = requiredProperties?.[0];
|
|
224
|
+
if (requiredProperty) {
|
|
225
|
+
const basePath = error.instancePath.replace(/^\//, "").replace(/\//g, ".");
|
|
226
|
+
return basePath ? `${basePath}.${requiredProperty}` : requiredProperty;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
const path = error.instancePath.replace(/^\//, "").replace(/\//g, ".");
|
|
230
|
+
return path || "root";
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Finds a tool by name and validates the tool call arguments against its TypeBox schema
|
|
234
|
+
* @param tools Array of tool definitions
|
|
235
|
+
* @param toolCall The tool call from the LLM
|
|
236
|
+
* @returns The validated arguments
|
|
237
|
+
* @throws Error if tool is not found or validation fails
|
|
238
|
+
*/
|
|
239
|
+
export function validateToolCall(tools, toolCall) {
|
|
240
|
+
const tool = tools.find((t) => t.name === toolCall.name);
|
|
241
|
+
if (!tool) {
|
|
242
|
+
throw new Error(`Tool "${toolCall.name}" not found`);
|
|
243
|
+
}
|
|
244
|
+
return validateToolArguments(tool, toolCall);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Validates tool call arguments against the tool's TypeBox schema
|
|
248
|
+
* @param tool The tool definition with TypeBox schema
|
|
249
|
+
* @param toolCall The tool call from the LLM
|
|
250
|
+
* @returns The validated (and potentially coerced) arguments
|
|
251
|
+
* @throws Error with formatted message if validation fails
|
|
252
|
+
*/
|
|
253
|
+
export function validateToolArguments(tool, toolCall) {
|
|
254
|
+
const args = structuredClone(toolCall.arguments);
|
|
255
|
+
Value.Convert(tool.parameters, args);
|
|
256
|
+
const validator = getValidator(tool.parameters);
|
|
257
|
+
if (!hasTypeBoxMetadata(tool.parameters) && isJsonSchemaObject(tool.parameters)) {
|
|
258
|
+
const coerced = coerceWithJsonSchema(args, tool.parameters);
|
|
259
|
+
if (coerced !== args) {
|
|
260
|
+
if (isRecord(args) && isRecord(coerced)) {
|
|
261
|
+
for (const key of Object.keys(args)) {
|
|
262
|
+
delete args[key];
|
|
263
|
+
}
|
|
264
|
+
Object.assign(args, coerced);
|
|
265
|
+
}
|
|
266
|
+
else {
|
|
267
|
+
return validator.Check(coerced) ? coerced : args;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
if (validator.Check(args)) {
|
|
272
|
+
return args;
|
|
273
|
+
}
|
|
274
|
+
const errors = validator
|
|
275
|
+
.Errors(args)
|
|
276
|
+
.map((error) => ` - ${formatValidationPath(error)}: ${error.message}`)
|
|
277
|
+
.join("\n") || "Unknown validation error";
|
|
278
|
+
const errorMessage = `Validation failed for tool "${toolCall.name}":\n${errors}\n\nReceived arguments:\n${JSON.stringify(toolCall.arguments, null, 2)}`;
|
|
279
|
+
throw new Error(errorMessage);
|
|
280
|
+
}
|
|
281
|
+
//# sourceMappingURL=validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/utils/validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAGtC,MAAM,cAAc,GAAG,IAAI,OAAO,EAAsC,CAAC;AACzE,MAAM,YAAY,GAAG,MAAM,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;AAYhD,SAAS,QAAQ,CAAC,KAAc,EAAoC;IACnE,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,CAAC;AAAA,CACnD;AAED,SAAS,kBAAkB,CAAC,KAAc,EAA6B;IACtE,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC;AAAA,CACvB;AAED,SAAS,kBAAkB,CAAC,MAAe,EAAW;IACrD,OAAO,QAAQ,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,qBAAqB,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC;AAAA,CACvF;AAED,SAAS,cAAc,CAAC,MAAwB,EAAY;IAC3D,IAAI,OAAO,MAAM,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;QACrC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IACD,IAAI,KAAK,CA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,KAAK,CAAC,KAAK,KAAK,CAAC,OAAO,EAAE,CAAC;SACtE,IAAI,CAAC,IAAI,CAAC,IAAI,0BAA0B,CAAC;IAE5C,MAAM,YAAY,GAAG,+BAA+B,QAAQ,CAAC,IAAI,OAAO,MAAM,4BAA4B,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;IAExJ,MAAM,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC;AAAA,CAC9B","sourcesContent":["import { Compile } from \"typebox/compile\";\nimport type { TLocalizedValidationError } from \"typebox/error\";\nimport { Value } from \"typebox/value\";\nimport type { Tool, ToolCall } from \"../types.js\";\n\nconst validatorCache = new WeakMap<object, ReturnType<typeof Compile>>();\nconst TYPEBOX_KIND = Symbol.for(\"TypeBox.Kind\");\n\ninterface JsonSchemaObject {\n\ttype?: string | string[];\n\tproperties?: Record<string, JsonSchemaObject>;\n\titems?: JsonSchemaObject | JsonSchemaObject[];\n\tadditionalProperties?: boolean | JsonSchemaObject;\n\tallOf?: JsonSchemaObject[];\n\tanyOf?: JsonSchemaObject[];\n\toneOf?: JsonSchemaObject[];\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === \"object\" && value !== null;\n}\n\nfunction isJsonSchemaObject(value: unknown): value is JsonSchemaObject {\n\treturn isRecord(value);\n}\n\nfunction hasTypeBoxMetadata(schema: unknown): boolean {\n\treturn isRecord(schema) && Object.getOwnPropertySymbols(schema).includes(TYPEBOX_KIND);\n}\n\nfunction getSchemaTypes(schema: JsonSchemaObject): string[] {\n\tif (typeof schema.type === \"string\") {\n\t\treturn [schema.type];\n\t}\n\tif (Array.isArray(schema.type)) {\n\t\treturn schema.type.filter((type): type is string => typeof type === \"string\");\n\t}\n\treturn [];\n}\n\nfunction matchesJsonType(value: unknown, type: string): boolean {\n\tswitch (type) {\n\t\tcase \"number\":\n\t\t\treturn typeof value === \"number\";\n\t\tcase \"integer\":\n\t\t\treturn typeof value === \"number\" && Number.isInteger(value);\n\t\tcase \"boolean\":\n\t\t\treturn typeof value === \"boolean\";\n\t\tcase \"string\":\n\t\t\treturn typeof value === \"string\";\n\t\tcase \"null\":\n\t\t\treturn value === null;\n\t\tcase \"array\":\n\t\t\treturn Array.isArray(value);\n\t\tcase \"object\":\n\t\t\treturn isRecord(value) && !Array.isArray(value);\n\t\tdefault:\n\t\t\treturn false;\n\t}\n}\n\nfunction isValidatorSchema(value: unknown): value is Tool[\"parameters\"] {\n\treturn isRecord(value);\n}\n\nfunction getSubSchemaValidator(schema: JsonSchemaObject): ReturnType<typeof Compile> | undefined {\n\tif (!isValidatorSchema(schema)) {\n\t\treturn undefined;\n\t}\n\ttry {\n\t\treturn getValidator(schema);\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction coercePrimitiveByType(value: unknown, type: string): unknown {\n\tswitch (type) {\n\t\tcase \"number\": {\n\t\t\tif (value === null) {\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t\tif (typeof value === \"string\" && value.trim() !== \"\") {\n\t\t\t\tconst parsed = Number(value);\n\t\t\t\tif (Number.isFinite(parsed)) {\n\t\t\t\t\treturn parsed;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (typeof value === \"boolean\") {\n\t\t\t\treturn value ? 1 : 0;\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tcase \"integer\": {\n\t\t\tif (value === null) {\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t\tif (typeof value === \"string\" && value.trim() !== \"\") {\n\t\t\t\tconst parsed = Number(value);\n\t\t\t\tif (Number.isInteger(parsed)) {\n\t\t\t\t\treturn parsed;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (typeof value === \"boolean\") {\n\t\t\t\treturn value ? 1 : 0;\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tcase \"boolean\": {\n\t\t\tif (value === null) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tif (typeof value === \"string\") {\n\t\t\t\tif (value === \"true\") {\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tif (value === \"false\") {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (typeof value === \"number\") {\n\t\t\t\tif (value === 1) {\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tif (value === 0) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tcase \"string\": {\n\t\t\tif (value === null) {\n\t\t\t\treturn \"\";\n\t\t\t}\n\t\t\tif (typeof value === \"number\" || typeof value === \"boolean\") {\n\t\t\t\treturn String(value);\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tcase \"null\": {\n\t\t\tif (value === \"\" || value === 0 || value === false) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\tdefault:\n\t\t\treturn value;\n\t}\n}\n\nfunction applySchemaObjectCoercion(value: Record<string, unknown>, schema: JsonSchemaObject): void {\n\tconst properties = schema.properties;\n\tconst definedKeys = new Set<string>(properties ? Object.keys(properties) : []);\n\n\tif (properties) {\n\t\tfor (const [key, propertySchema] of Object.entries(properties)) {\n\t\t\tif (!(key in value)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tvalue[key] = coerceWithJsonSchema(value[key], propertySchema);\n\t\t}\n\t}\n\n\tif (schema.additionalProperties && isJsonSchemaObject(schema.additionalProperties)) {\n\t\tfor (const [key, propertyValue] of Object.entries(value)) {\n\t\t\tif (definedKeys.has(key)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tvalue[key] = coerceWithJsonSchema(propertyValue, schema.additionalProperties);\n\t\t}\n\t}\n}\n\nfunction applySchemaArrayCoercion(value: unknown[], schema: JsonSchemaObject): void {\n\tif (Array.isArray(schema.items)) {\n\t\tfor (let index = 0; index < value.length; index++) {\n\t\t\tconst itemSchema = schema.items[index];\n\t\t\tif (!itemSchema) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tvalue[index] = coerceWithJsonSchema(value[index], itemSchema);\n\t\t}\n\t\treturn;\n\t}\n\n\tif (isJsonSchemaObject(schema.items)) {\n\t\tfor (let index = 0; index < value.length; index++) {\n\t\t\tvalue[index] = coerceWithJsonSchema(value[index], schema.items);\n\t\t}\n\t}\n}\n\nfunction coerceWithUnionSchema(value: unknown, schemas: JsonSchemaObject[]): unknown {\n\tfor (const schema of schemas) {\n\t\tconst candidate = structuredClone(value);\n\t\tconst coerced = coerceWithJsonSchema(candidate, schema);\n\t\tconst validator = getSubSchemaValidator(schema);\n\t\tif (validator?.Check(coerced)) {\n\t\t\treturn coerced;\n\t\t}\n\t}\n\treturn value;\n}\n\nfunction coerceWithJsonSchema(value: unknown, schema: JsonSchemaObject): unknown {\n\tlet nextValue = value;\n\n\tif (Array.isArray(schema.allOf)) {\n\t\tfor (const nested of schema.allOf) {\n\t\t\tnextValue = coerceWithJsonSchema(nextValue, nested);\n\t\t}\n\t}\n\n\tif (Array.isArray(schema.anyOf)) {\n\t\tnextValue = coerceWithUnionSchema(nextValue, schema.anyOf);\n\t}\n\n\tif (Array.isArray(schema.oneOf)) {\n\t\tnextValue = coerceWithUnionSchema(nextValue, schema.oneOf);\n\t}\n\n\tconst schemaTypes = getSchemaTypes(schema);\n\tconst matchesUnionMember =\n\t\tschemaTypes.length > 1 && schemaTypes.some((schemaType) => matchesJsonType(nextValue, schemaType));\n\tif (schemaTypes.length > 0 && !matchesUnionMember) {\n\t\tfor (const schemaType of schemaTypes) {\n\t\t\tconst candidate = coercePrimitiveByType(nextValue, schemaType);\n\t\t\tif (candidate !== nextValue) {\n\t\t\t\tnextValue = candidate;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (schemaTypes.includes(\"object\") && isRecord(nextValue) && !Array.isArray(nextValue)) {\n\t\tapplySchemaObjectCoercion(nextValue, schema);\n\t}\n\n\tif (schemaTypes.includes(\"array\") && Array.isArray(nextValue)) {\n\t\tapplySchemaArrayCoercion(nextValue, schema);\n\t}\n\n\treturn nextValue;\n}\n\nfunction getValidator(schema: Tool[\"parameters\"]): ReturnType<typeof Compile> {\n\tconst key = schema as object;\n\tconst cached = validatorCache.get(key);\n\tif (cached) {\n\t\treturn cached;\n\t}\n\tconst validator = Compile(schema);\n\tvalidatorCache.set(key, validator);\n\treturn validator;\n}\n\nfunction formatValidationPath(error: TLocalizedValidationError): string {\n\tif (error.keyword === \"required\") {\n\t\tconst requiredProperties = (error.params as { requiredProperties?: string[] }).requiredProperties;\n\t\tconst requiredProperty = requiredProperties?.[0];\n\t\tif (requiredProperty) {\n\t\t\tconst basePath = error.instancePath.replace(/^\\//, \"\").replace(/\\//g, \".\");\n\t\t\treturn basePath ? `${basePath}.${requiredProperty}` : requiredProperty;\n\t\t}\n\t}\n\tconst path = error.instancePath.replace(/^\\//, \"\").replace(/\\//g, \".\");\n\treturn path || \"root\";\n}\n\n/**\n * Finds a tool by name and validates the tool call arguments against its TypeBox schema\n * @param tools Array of tool definitions\n * @param toolCall The tool call from the LLM\n * @returns The validated arguments\n * @throws Error if tool is not found or validation fails\n */\nexport function validateToolCall(tools: Tool[], toolCall: ToolCall): any {\n\tconst tool = tools.find((t) => t.name === toolCall.name);\n\tif (!tool) {\n\t\tthrow new Error(`Tool \"${toolCall.name}\" not found`);\n\t}\n\treturn validateToolArguments(tool, toolCall);\n}\n\n/**\n * Validates tool call arguments against the tool's TypeBox schema\n * @param tool The tool definition with TypeBox schema\n * @param toolCall The tool call from the LLM\n * @returns The validated (and potentially coerced) arguments\n * @throws Error with formatted message if validation fails\n */\nexport function validateToolArguments(tool: Tool, toolCall: ToolCall): any {\n\tconst args = structuredClone(toolCall.arguments);\n\tValue.Convert(tool.parameters, args);\n\n\tconst validator = getValidator(tool.parameters);\n\tif (!hasTypeBoxMetadata(tool.parameters) && isJsonSchemaObject(tool.parameters)) {\n\t\tconst coerced = coerceWithJsonSchema(args, tool.parameters);\n\t\tif (coerced !== args) {\n\t\t\tif (isRecord(args) && isRecord(coerced)) {\n\t\t\t\tfor (const key of Object.keys(args)) {\n\t\t\t\t\tdelete args[key];\n\t\t\t\t}\n\t\t\t\tObject.assign(args, coerced);\n\t\t\t} else {\n\t\t\t\treturn validator.Check(coerced) ? coerced : args;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (validator.Check(args)) {\n\t\treturn args;\n\t}\n\n\tconst errors =\n\t\tvalidator\n\t\t\t.Errors(args)\n\t\t\t.map((error) => ` - ${formatValidationPath(error)}: ${error.message}`)\n\t\t\t.join(\"\\n\") || \"Unknown validation error\";\n\n\tconst errorMessage = `Validation failed for tool \"${toolCall.name}\":\\n${errors}\\n\\nReceived arguments:\\n${JSON.stringify(toolCall.arguments, null, 2)}`;\n\n\tthrow new Error(errorMessage);\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@asm-agent/ai",
|
|
3
|
+
"version": "0.8.2",
|
|
4
|
+
"description": "Unified LLM API with automatic model discovery and provider configuration",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./anthropic": {
|
|
14
|
+
"types": "./dist/providers/anthropic.d.ts",
|
|
15
|
+
"import": "./dist/providers/anthropic.js"
|
|
16
|
+
},
|
|
17
|
+
"./azure-openai-responses": {
|
|
18
|
+
"types": "./dist/providers/azure-openai-responses.d.ts",
|
|
19
|
+
"import": "./dist/providers/azure-openai-responses.js"
|
|
20
|
+
},
|
|
21
|
+
"./google": {
|
|
22
|
+
"types": "./dist/providers/google.d.ts",
|
|
23
|
+
"import": "./dist/providers/google.js"
|
|
24
|
+
},
|
|
25
|
+
"./google-vertex": {
|
|
26
|
+
"types": "./dist/providers/google-vertex.d.ts",
|
|
27
|
+
"import": "./dist/providers/google-vertex.js"
|
|
28
|
+
},
|
|
29
|
+
"./mistral": {
|
|
30
|
+
"types": "./dist/providers/mistral.d.ts",
|
|
31
|
+
"import": "./dist/providers/mistral.js"
|
|
32
|
+
},
|
|
33
|
+
"./openai-codex-responses": {
|
|
34
|
+
"types": "./dist/providers/openai-codex-responses.d.ts",
|
|
35
|
+
"import": "./dist/providers/openai-codex-responses.js"
|
|
36
|
+
},
|
|
37
|
+
"./openai-completions": {
|
|
38
|
+
"types": "./dist/providers/openai-completions.d.ts",
|
|
39
|
+
"import": "./dist/providers/openai-completions.js"
|
|
40
|
+
},
|
|
41
|
+
"./openai-responses": {
|
|
42
|
+
"types": "./dist/providers/openai-responses.d.ts",
|
|
43
|
+
"import": "./dist/providers/openai-responses.js"
|
|
44
|
+
},
|
|
45
|
+
"./oauth": {
|
|
46
|
+
"types": "./dist/oauth.d.ts",
|
|
47
|
+
"import": "./dist/oauth.js"
|
|
48
|
+
},
|
|
49
|
+
"./mcp": {
|
|
50
|
+
"types": "./dist/mcp.d.ts",
|
|
51
|
+
"import": "./dist/mcp.js"
|
|
52
|
+
},
|
|
53
|
+
"./bedrock-provider": {
|
|
54
|
+
"types": "./dist/bedrock-provider.d.ts",
|
|
55
|
+
"import": "./dist/bedrock-provider.js"
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"bin": {
|
|
59
|
+
"asm-ai": "./dist/cli.js"
|
|
60
|
+
},
|
|
61
|
+
"files": [
|
|
62
|
+
"dist",
|
|
63
|
+
"README.md"
|
|
64
|
+
],
|
|
65
|
+
"scripts": {
|
|
66
|
+
"clean": "shx rm -rf dist",
|
|
67
|
+
"generate-models": "npx tsx scripts/generate-models.ts",
|
|
68
|
+
"build": "npm run generate-models && tsgo -p tsconfig.build.json",
|
|
69
|
+
"dev": "tsgo -p tsconfig.build.json --watch --preserveWatchOutput",
|
|
70
|
+
"dev:tsc": "tsgo -p tsconfig.build.json --watch --preserveWatchOutput",
|
|
71
|
+
"test": "vitest --run",
|
|
72
|
+
"prepublishOnly": "npm run clean && npm run build"
|
|
73
|
+
},
|
|
74
|
+
"dependencies": {
|
|
75
|
+
"@anthropic-ai/sdk": "^0.91.1",
|
|
76
|
+
"@aws-sdk/client-bedrock-runtime": "^3.1095.0",
|
|
77
|
+
"@google/genai": "^1.40.0",
|
|
78
|
+
"@mistralai/mistralai": "^2.2.0",
|
|
79
|
+
"@opentelemetry/api": "^1.9.1",
|
|
80
|
+
"chalk": "^5.6.2",
|
|
81
|
+
"openai": "6.47.0",
|
|
82
|
+
"partial-json": "^0.1.7",
|
|
83
|
+
"proxy-agent": "^6.5.0",
|
|
84
|
+
"typebox": "^1.3.9",
|
|
85
|
+
"undici": "^7.29.0",
|
|
86
|
+
"zod-to-json-schema": "^3.24.6"
|
|
87
|
+
},
|
|
88
|
+
"keywords": [
|
|
89
|
+
"ai",
|
|
90
|
+
"llm",
|
|
91
|
+
"openai",
|
|
92
|
+
"anthropic",
|
|
93
|
+
"gemini",
|
|
94
|
+
"bedrock",
|
|
95
|
+
"unified",
|
|
96
|
+
"api"
|
|
97
|
+
],
|
|
98
|
+
"author": "Mario Zechner",
|
|
99
|
+
"license": "(AGPL-3.0-only OR LicenseRef-ASM-Commercial)",
|
|
100
|
+
"repository": {
|
|
101
|
+
"type": "git",
|
|
102
|
+
"url": "git+https://github.com/AletheionAGI/asm-agent.git",
|
|
103
|
+
"directory": "packages/ai"
|
|
104
|
+
},
|
|
105
|
+
"engines": {
|
|
106
|
+
"node": ">=20.0.0"
|
|
107
|
+
},
|
|
108
|
+
"devDependencies": {
|
|
109
|
+
"@types/node": "^24.3.0",
|
|
110
|
+
"canvas": "^3.2.0",
|
|
111
|
+
"vitest": "^4.1.10"
|
|
112
|
+
}
|
|
113
|
+
}
|