UNPKG

@tanstack/ai

Version:

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

100 lines (99 loc) • 6.24 kB
import { fromSpecTokenUsage, toSpecTokenUsage } from "./utilities/ag-ui-usage.js"; import { mergeMetadata, tanstackMetadata, withTanstackMetadata } from "./utilities/merge-metadata.js"; import { normalizeStreamChunk } from "./utilities/normalize-stream-chunk.js"; import { canonicalInterruptJson, cloneAndDeepFreezeJson, digestInterruptJson } from "./interrupt-serialization.js"; import { INTERRUPT_BINDING_VERSION, canonicalizeInterruptResolutions } from "./interrupts.js"; import { convertSchemaToJsonSchema, isStandardSchema, parseWithStandardSchema, validateWithStandardSchema } from "./activities/chat/tools/schema-converter.js"; import { hashSchemaInput, normalizeApprovalSchema } from "./activities/chat/tools/approval-schema.js"; import { INTERRUPT_BINDING_METADATA_KEY, readInterruptBinding, readUnopenedInterruptBinding, withInterruptBinding, withoutInterruptBinding } from "./interrupt-resume.js"; import { INTERRUPT_PAYLOAD_METADATA_KEY, defineInterrupt, hashInterruptDefinitionSchema } from "./interrupt-definition.js"; import { INTERRUPT_CONTINUATION_METADATA_KEY, INTERRUPT_CONTINUATION_VERSION, genericInterruptContinuationFromDescriptor, readGenericInterruptContinuation, wrapGenericInterruptContinuation } from "./generic-interrupt-continuation.js"; import { subagentInputOf } from "./utilities/subagent-wire.js"; import { convertMessagesToModelMessages, generateMessageId, modelMessageToUIMessage, modelMessagesToUIMessages, normalizeToUIMessage, uiMessageToModelMessages } from "./activities/chat/messages.js"; import { uiMessagesToWire } from "./utilities/ag-ui-wire.js"; import { restoreInboundChunk } from "./utilities/restore-inbound-chunk.js"; import { defineAgent } from "./activities/chat/agents/define-agent.js"; import { subagentRoute } from "./activities/chat/agents/route.js"; import { fileSourceFromHandle } from "./activities/files/index.js"; import { toolDefinition } from "./activities/chat/tools/tool-definition.js"; import { getChunkRunId, getChunkThreadId } from "./utilities/chunk-ids.js"; import { PartialJSONParser, defaultJSONParser, parsePartialJSON } from "./activities/chat/stream/json-parser.js"; import { BatchStrategy, CompositeStrategy, ImmediateStrategy, PunctuationStrategy, WordBoundaryStrategy } from "./activities/chat/stream/strategies.js"; import { StreamProcessor } from "./activities/chat/stream/processor.js"; import { EventType } from "@ag-ui/core"; //#region src/client.ts var generationKinds = [ "image", "audio", "tts", "voice", "video", "transcription", "world", "liveVideo" ]; function isRecord(value) { return typeof value === "object" && value !== null && !Array.isArray(value); } function hasOwnKey(value, key) { return Object.prototype.hasOwnProperty.call(value, key); } function assertGenerationKind(kind) { if (!generationKinds.includes(kind)) throw new Error(`Unsupported generation kind: ${String(kind)}. Expected one of ${generationKinds.join(", ")}.`); } /** * The input field(s) that identify a generation body for a kind. Most kinds * have exactly one; `voice` accepts either of its two creation modes, so any * one of its keys is enough. */ function requiredKeysForKind(kind) { switch (kind) { case "tts": return ["text"]; case "transcription": return ["audio"]; case "voice": return ["prompt", "referenceAudio"]; case "image": case "audio": case "video": case "world": case "liveVideo": return ["prompt"]; } } function assertInputForKind(kind, input) { if (!isRecord(input)) throw new Error(`Generation ${kind} input must be an object.`); const requiredKeys = requiredKeysForKind(kind); if (!requiredKeys.some((key) => hasOwnKey(input, key))) throw new Error(`Generation ${kind} input must include ${requiredKeys.join(" or ")}.`); } /** * Read a generation request: the AG-UI `RunAgentInput` body that the * generation hooks send. The input is in `forwardedProps`. * * @throws When the body has no `threadId` or `runId`, or its input does not fit * the kind. */ function generationParamsFromBody(kind, body) { assertGenerationKind(kind); if (!isRecord(body)) throw new Error("Generation request body must be a JSON object."); const { threadId, runId, forwardedProps } = body; if (typeof threadId !== "string" || !threadId) throw new Error("Generation request needs a threadId."); if (typeof runId !== "string" || !runId) throw new Error("Generation request needs a runId."); if (!isRecord(forwardedProps)) throw new Error("Generation request forwardedProps must be an object."); assertInputForKind(kind, forwardedProps); return { input: forwardedProps, forwardedProps, threadId, runId }; } async function generationParamsFromRequest(kind, request) { let body; try { body = await request.json(); } catch (error) { throw new Error("Invalid JSON request body.", { cause: error }); } return generationParamsFromBody(kind, body); } //#endregion export { BatchStrategy, CompositeStrategy, EventType, INTERRUPT_BINDING_METADATA_KEY, INTERRUPT_BINDING_VERSION, INTERRUPT_CONTINUATION_METADATA_KEY, INTERRUPT_CONTINUATION_VERSION, INTERRUPT_PAYLOAD_METADATA_KEY, ImmediateStrategy, PartialJSONParser, PunctuationStrategy, StreamProcessor, WordBoundaryStrategy, canonicalInterruptJson, canonicalizeInterruptResolutions, cloneAndDeepFreezeJson, convertMessagesToModelMessages, convertSchemaToJsonSchema, defaultJSONParser, defineAgent, defineInterrupt, digestInterruptJson, fileSourceFromHandle, fromSpecTokenUsage, generateMessageId, generationParamsFromBody, generationParamsFromRequest, genericInterruptContinuationFromDescriptor, getChunkRunId, getChunkThreadId, hashInterruptDefinitionSchema, hashSchemaInput, isStandardSchema, mergeMetadata, modelMessageToUIMessage, modelMessagesToUIMessages, normalizeApprovalSchema, normalizeStreamChunk, normalizeToUIMessage, parsePartialJSON, parseWithStandardSchema, readGenericInterruptContinuation, readInterruptBinding, readUnopenedInterruptBinding, restoreInboundChunk, subagentInputOf, subagentRoute, tanstackMetadata, toSpecTokenUsage, toolDefinition, uiMessageToModelMessages, uiMessagesToWire, validateWithStandardSchema, withInterruptBinding, withTanstackMetadata, withoutInterruptBinding, wrapGenericInterruptContinuation }; //# sourceMappingURL=client.js.map