{"version":3,"file":"ai-sdk.cjs","sources":["../src/ai-sdk.tsx"],"sourcesContent":["// The agentic surface for @simplepdf/react-embed-pdf: the React mirror of\n// @simplepdf/embed's /tools + /ai-sdk subpaths. Importing THIS module (not the package\n// root) is what pulls in `zod`, so a <EmbedPDF>-only app never loads it — the same\n// pay-for-use contract the core has. Pair it with useEmbed():\n//\n//   const { embedRef } = useEmbed()\n//   const tools = useEmbedTools(embedRef) // bound to the live editor\n//   useChat({ tools })\n\nimport * as React from 'react';\nimport type { RefObject } from 'react';\nimport type { BridgeResult } from '@simplepdf/embed';\nimport { isSimplePDFToolName, routeToolCall, SIMPLEPDF_TOOLS, type SimplePDFToolName } from '@simplepdf/embed/tools';\nimport type { EmbedActions } from './embed-pdf';\nimport { notMounted } from './not-mounted';\n\n// Re-export the browser-side executor + the tool-name guard for React consumers. The\n// server-side `simplePDFToolDefinitions` is deliberately NOT re-exported: this module\n// imports React (for the hook), so re-exporting the defs would pull React into a server\n// `streamText` route. Import those from the React-free core `@simplepdf/embed/ai-sdk`.\nexport { createSimplePDFExecutor } from '@simplepdf/embed/ai-sdk';\nexport { isSimplePDFToolName };\nexport type { SimplePDFToolName };\n\ntype ToolInputSchema = (typeof SIMPLEPDF_TOOLS)[SimplePDFToolName]['inputSchema'];\n\n// One agentic tool, bound to the live editor. The shape ({ description, inputSchema,\n// execute }) is exactly what the AI SDK consumes, so the whole `tools` record drops\n// straight into useChat({ tools }) / streamText — no executor to wire.\nexport type EmbedTool = {\n  description: string;\n  inputSchema: ToolInputSchema;\n  execute: (input: unknown) => Promise<BridgeResult<unknown>>;\n};\n\n// Record<string, …> (not Record<SimplePDFToolName, …>) matches the AI SDK's string-keyed\n// `tools` param. A literal-keyed Record can't be built here without an `as` cast (Object\n// .fromEntries / reduce both erase the key type), and `Partial<Record<…>>` would break that\n// AI-SDK assignability — so the registry stays string-keyed (it is complete at runtime).\nexport type EmbedTools = Record<string, EmbedTool>;\n\n// The agentic registry bound to the live editor via useEmbed().embedRef. Stable and\n// null-safe before the editor mounts (each `execute` reads embedRef.current at call time).\nexport const useEmbedTools = (embedRef: RefObject<EmbedActions | null>): EmbedTools =>\n  React.useMemo<EmbedTools>(\n    () =>\n      Object.keys(SIMPLEPDF_TOOLS)\n        .filter(isSimplePDFToolName)\n        .reduce<EmbedTools>((accumulator, name) => {\n          accumulator[name] = {\n            description: SIMPLEPDF_TOOLS[name].description,\n            inputSchema: SIMPLEPDF_TOOLS[name].inputSchema,\n            execute: (input) =>\n              embedRef.current === null ? notMounted() : routeToolCall(embedRef.current, name, input),\n          };\n          return accumulator;\n        }, {}),\n    [embedRef],\n  );\n"],"names":["embedRef","React","useMemo","Object","keys","SIMPLEPDF_TOOLS","filter","isSimplePDFToolName","reduce","accumulator","name","description","inputSchema","execute","input","current","notMounted","routeToolCall"],"mappings":"0qBA2C6B,SAACA,GAC5B,OAAAC,EAAMC,QACJ,WACE,OAAAC,OAAOC,KAAKC,EAAAA,iBACTC,OAAOC,EAAAA,qBACPC,OAAmB,SAACC,EAAaC,GAOhC,OANAD,EAAYC,GAAQ,CAClBC,YAAaN,EAAAA,gBAAgBK,GAAMC,YACnCC,YAAaP,EAAAA,gBAAgBK,GAAME,YACnCC,QAAS,SAACC,GACR,OAAqB,OAArBd,EAASe,QAAmBC,EAAAA,aAAeC,EAAAA,cAAcjB,EAASe,QAASL,EAAMI,EAAjF,GAEGL,CACT,EAAG,CAAA,EAVL,EAWF,CAACT,GAbH"}