|
| 1 | +// TanStack AI adapter. The same generated registry + bridge router as the Vercel |
| 2 | +// `/ai-sdk` adapter, in TanStack's isomorphic tool shape: server-registerable |
| 3 | +// definitions for `chat({ tools })`, and browser `.client()` tools bound to the live |
| 4 | +// editor for `clientTools(...)` -> `useChat({ tools })`. The zod input schemas drop |
| 5 | +// in directly (TanStack accepts any Standard Schema); `@tanstack/ai` is the only |
| 6 | +// added peer, pulled solely by this subpath. |
| 7 | + |
| 8 | +import { type AnyClientTool, toolDefinition } from '@tanstack/ai' |
| 9 | +import { TOOL_DEFINITIONS, type SimplePDFToolName } from './generated/tools' |
| 10 | +import { isSimplePDFToolName, routeToolCall } from './tools' |
| 11 | +import type { Embed } from './types' |
| 12 | + |
| 13 | +export type { SimplePDFToolName } from './generated/tools' |
| 14 | + |
| 15 | +const TOOL_NAMES: readonly SimplePDFToolName[] = Object.keys(TOOL_DEFINITIONS).filter(isSimplePDFToolName) |
| 16 | + |
| 17 | +// One shared definition per tool (name + description + zod input schema): the unit |
| 18 | +// `chat({ tools })` registers and `.client()` / `.server()` instantiate from. |
| 19 | +const define = (name: SimplePDFToolName) => |
| 20 | + toolDefinition({ |
| 21 | + name, |
| 22 | + description: TOOL_DEFINITIONS[name].description, |
| 23 | + inputSchema: TOOL_DEFINITIONS[name].inputSchema, |
| 24 | + }) |
| 25 | + |
| 26 | +// Server: execute-less definitions for `chat({ tools })`, so the model is aware of |
| 27 | +// the tools. A fresh array each call so the host can pick/omit (e.g. gate submit XOR |
| 28 | +// download) without mutating shared state. |
| 29 | +export const simplePDFTanstackToolDefinitions = (): ReturnType<typeof define>[] => TOOL_NAMES.map(define) |
| 30 | + |
| 31 | +// Browser: the same definitions bound to the live editor via `.client()`, for |
| 32 | +// `clientTools(...)` -> `useChat({ tools })`. Each call validates input against the |
| 33 | +// tool schema and dispatches to the matching editor action, resolving to a BridgeResult. |
| 34 | +export const createSimplePDFTanstackTools = ({ embed }: { embed: Embed }): AnyClientTool[] => |
| 35 | + TOOL_NAMES.map((name) => define(name).client((input) => routeToolCall(embed.actions, name, input))) |
0 commit comments