@depup/ai 6.0.116-depup.0
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/CHANGELOG.md +7350 -0
- package/LICENSE +13 -0
- package/README.md +25 -0
- package/changes.json +5 -0
- package/dist/index.d.mts +6429 -0
- package/dist/index.d.ts +6429 -0
- package/dist/index.js +13548 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +13514 -0
- package/dist/index.mjs.map +1 -0
- package/dist/internal/index.d.mts +314 -0
- package/dist/internal/index.d.ts +314 -0
- package/dist/internal/index.js +1316 -0
- package/dist/internal/index.js.map +1 -0
- package/dist/internal/index.mjs +1300 -0
- package/dist/internal/index.mjs.map +1 -0
- package/dist/test/index.d.mts +133 -0
- package/dist/test/index.d.ts +133 -0
- package/dist/test/index.js +275 -0
- package/dist/test/index.js.map +1 -0
- package/dist/test/index.mjs +245 -0
- package/dist/test/index.mjs.map +1 -0
- package/docs/00-introduction/index.mdx +76 -0
- package/docs/02-foundations/01-overview.mdx +43 -0
- package/docs/02-foundations/02-providers-and-models.mdx +156 -0
- package/docs/02-foundations/03-prompts.mdx +616 -0
- package/docs/02-foundations/04-tools.mdx +251 -0
- package/docs/02-foundations/05-streaming.mdx +62 -0
- package/docs/02-foundations/06-provider-options.mdx +337 -0
- package/docs/02-foundations/index.mdx +49 -0
- package/docs/02-getting-started/00-choosing-a-provider.mdx +110 -0
- package/docs/02-getting-started/01-navigating-the-library.mdx +85 -0
- package/docs/02-getting-started/02-nextjs-app-router.mdx +559 -0
- package/docs/02-getting-started/03-nextjs-pages-router.mdx +542 -0
- package/docs/02-getting-started/04-svelte.mdx +627 -0
- package/docs/02-getting-started/05-nuxt.mdx +566 -0
- package/docs/02-getting-started/06-nodejs.mdx +512 -0
- package/docs/02-getting-started/07-expo.mdx +766 -0
- package/docs/02-getting-started/08-tanstack-start.mdx +583 -0
- package/docs/02-getting-started/09-coding-agents.mdx +179 -0
- package/docs/02-getting-started/index.mdx +44 -0
- package/docs/03-agents/01-overview.mdx +96 -0
- package/docs/03-agents/02-building-agents.mdx +449 -0
- package/docs/03-agents/03-workflows.mdx +386 -0
- package/docs/03-agents/04-loop-control.mdx +394 -0
- package/docs/03-agents/05-configuring-call-options.mdx +286 -0
- package/docs/03-agents/06-memory.mdx +222 -0
- package/docs/03-agents/06-subagents.mdx +362 -0
- package/docs/03-agents/index.mdx +46 -0
- package/docs/03-ai-sdk-core/01-overview.mdx +31 -0
- package/docs/03-ai-sdk-core/05-generating-text.mdx +707 -0
- package/docs/03-ai-sdk-core/10-generating-structured-data.mdx +498 -0
- package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +1144 -0
- package/docs/03-ai-sdk-core/16-mcp-tools.mdx +377 -0
- package/docs/03-ai-sdk-core/20-prompt-engineering.mdx +146 -0
- package/docs/03-ai-sdk-core/25-settings.mdx +198 -0
- package/docs/03-ai-sdk-core/30-embeddings.mdx +245 -0
- package/docs/03-ai-sdk-core/31-reranking.mdx +218 -0
- package/docs/03-ai-sdk-core/35-image-generation.mdx +342 -0
- package/docs/03-ai-sdk-core/36-transcription.mdx +227 -0
- package/docs/03-ai-sdk-core/37-speech.mdx +169 -0
- package/docs/03-ai-sdk-core/38-video-generation.mdx +366 -0
- package/docs/03-ai-sdk-core/40-middleware.mdx +485 -0
- package/docs/03-ai-sdk-core/45-provider-management.mdx +349 -0
- package/docs/03-ai-sdk-core/50-error-handling.mdx +149 -0
- package/docs/03-ai-sdk-core/55-testing.mdx +219 -0
- package/docs/03-ai-sdk-core/60-telemetry.mdx +391 -0
- package/docs/03-ai-sdk-core/65-devtools.mdx +107 -0
- package/docs/03-ai-sdk-core/65-event-listeners.mdx +915 -0
- package/docs/03-ai-sdk-core/index.mdx +93 -0
- package/docs/04-ai-sdk-ui/01-overview.mdx +44 -0
- package/docs/04-ai-sdk-ui/02-chatbot.mdx +1313 -0
- package/docs/04-ai-sdk-ui/03-chatbot-message-persistence.mdx +535 -0
- package/docs/04-ai-sdk-ui/03-chatbot-resume-streams.mdx +263 -0
- package/docs/04-ai-sdk-ui/03-chatbot-tool-usage.mdx +682 -0
- package/docs/04-ai-sdk-ui/04-generative-user-interfaces.mdx +389 -0
- package/docs/04-ai-sdk-ui/05-completion.mdx +181 -0
- package/docs/04-ai-sdk-ui/08-object-generation.mdx +344 -0
- package/docs/04-ai-sdk-ui/20-streaming-data.mdx +397 -0
- package/docs/04-ai-sdk-ui/21-error-handling.mdx +190 -0
- package/docs/04-ai-sdk-ui/21-transport.mdx +174 -0
- package/docs/04-ai-sdk-ui/24-reading-ui-message-streams.mdx +104 -0
- package/docs/04-ai-sdk-ui/25-message-metadata.mdx +152 -0
- package/docs/04-ai-sdk-ui/50-stream-protocol.mdx +477 -0
- package/docs/04-ai-sdk-ui/index.mdx +64 -0
- package/docs/05-ai-sdk-rsc/01-overview.mdx +45 -0
- package/docs/05-ai-sdk-rsc/02-streaming-react-components.mdx +209 -0
- package/docs/05-ai-sdk-rsc/03-generative-ui-state.mdx +279 -0
- package/docs/05-ai-sdk-rsc/03-saving-and-restoring-states.mdx +105 -0
- package/docs/05-ai-sdk-rsc/04-multistep-interfaces.mdx +282 -0
- package/docs/05-ai-sdk-rsc/05-streaming-values.mdx +157 -0
- package/docs/05-ai-sdk-rsc/06-loading-state.mdx +273 -0
- package/docs/05-ai-sdk-rsc/08-error-handling.mdx +94 -0
- package/docs/05-ai-sdk-rsc/09-authentication.mdx +42 -0
- package/docs/05-ai-sdk-rsc/10-migrating-to-ui.mdx +722 -0
- package/docs/05-ai-sdk-rsc/index.mdx +63 -0
- package/docs/06-advanced/01-prompt-engineering.mdx +96 -0
- package/docs/06-advanced/02-stopping-streams.mdx +184 -0
- package/docs/06-advanced/03-backpressure.mdx +173 -0
- package/docs/06-advanced/04-caching.mdx +169 -0
- package/docs/06-advanced/05-multiple-streamables.mdx +68 -0
- package/docs/06-advanced/06-rate-limiting.mdx +60 -0
- package/docs/06-advanced/07-rendering-ui-with-language-models.mdx +225 -0
- package/docs/06-advanced/08-model-as-router.mdx +120 -0
- package/docs/06-advanced/09-multistep-interfaces.mdx +115 -0
- package/docs/06-advanced/09-sequential-generations.mdx +55 -0
- package/docs/06-advanced/10-vercel-deployment-guide.mdx +117 -0
- package/docs/06-advanced/index.mdx +11 -0
- package/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx +2715 -0
- package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +3656 -0
- package/docs/07-reference/01-ai-sdk-core/05-embed.mdx +197 -0
- package/docs/07-reference/01-ai-sdk-core/06-embed-many.mdx +191 -0
- package/docs/07-reference/01-ai-sdk-core/06-rerank.mdx +309 -0
- package/docs/07-reference/01-ai-sdk-core/10-generate-image.mdx +251 -0
- package/docs/07-reference/01-ai-sdk-core/11-transcribe.mdx +152 -0
- package/docs/07-reference/01-ai-sdk-core/12-generate-speech.mdx +221 -0
- package/docs/07-reference/01-ai-sdk-core/13-generate-video.mdx +264 -0
- package/docs/07-reference/01-ai-sdk-core/15-agent.mdx +235 -0
- package/docs/07-reference/01-ai-sdk-core/16-tool-loop-agent.mdx +973 -0
- package/docs/07-reference/01-ai-sdk-core/17-create-agent-ui-stream.mdx +154 -0
- package/docs/07-reference/01-ai-sdk-core/18-create-agent-ui-stream-response.mdx +173 -0
- package/docs/07-reference/01-ai-sdk-core/18-pipe-agent-ui-stream-to-response.mdx +150 -0
- package/docs/07-reference/01-ai-sdk-core/20-tool.mdx +209 -0
- package/docs/07-reference/01-ai-sdk-core/22-dynamic-tool.mdx +223 -0
- package/docs/07-reference/01-ai-sdk-core/23-create-mcp-client.mdx +416 -0
- package/docs/07-reference/01-ai-sdk-core/24-mcp-stdio-transport.mdx +68 -0
- package/docs/07-reference/01-ai-sdk-core/25-json-schema.mdx +94 -0
- package/docs/07-reference/01-ai-sdk-core/26-zod-schema.mdx +109 -0
- package/docs/07-reference/01-ai-sdk-core/27-valibot-schema.mdx +58 -0
- package/docs/07-reference/01-ai-sdk-core/28-output.mdx +342 -0
- package/docs/07-reference/01-ai-sdk-core/30-model-message.mdx +415 -0
- package/docs/07-reference/01-ai-sdk-core/31-ui-message.mdx +246 -0
- package/docs/07-reference/01-ai-sdk-core/32-validate-ui-messages.mdx +101 -0
- package/docs/07-reference/01-ai-sdk-core/33-safe-validate-ui-messages.mdx +113 -0
- package/docs/07-reference/01-ai-sdk-core/40-provider-registry.mdx +198 -0
- package/docs/07-reference/01-ai-sdk-core/42-custom-provider.mdx +157 -0
- package/docs/07-reference/01-ai-sdk-core/50-cosine-similarity.mdx +52 -0
- package/docs/07-reference/01-ai-sdk-core/60-wrap-language-model.mdx +59 -0
- package/docs/07-reference/01-ai-sdk-core/61-wrap-image-model.mdx +64 -0
- package/docs/07-reference/01-ai-sdk-core/65-language-model-v2-middleware.mdx +74 -0
- package/docs/07-reference/01-ai-sdk-core/66-extract-reasoning-middleware.mdx +68 -0
- package/docs/07-reference/01-ai-sdk-core/67-simulate-streaming-middleware.mdx +71 -0
- package/docs/07-reference/01-ai-sdk-core/68-default-settings-middleware.mdx +80 -0
- package/docs/07-reference/01-ai-sdk-core/69-add-tool-input-examples-middleware.mdx +155 -0
- package/docs/07-reference/01-ai-sdk-core/70-extract-json-middleware.mdx +147 -0
- package/docs/07-reference/01-ai-sdk-core/70-step-count-is.mdx +84 -0
- package/docs/07-reference/01-ai-sdk-core/71-has-tool-call.mdx +120 -0
- package/docs/07-reference/01-ai-sdk-core/75-simulate-readable-stream.mdx +94 -0
- package/docs/07-reference/01-ai-sdk-core/80-smooth-stream.mdx +145 -0
- package/docs/07-reference/01-ai-sdk-core/90-generate-id.mdx +30 -0
- package/docs/07-reference/01-ai-sdk-core/91-create-id-generator.mdx +89 -0
- package/docs/07-reference/01-ai-sdk-core/92-default-generated-file.mdx +68 -0
- package/docs/07-reference/01-ai-sdk-core/index.mdx +160 -0
- package/docs/07-reference/02-ai-sdk-ui/01-use-chat.mdx +493 -0
- package/docs/07-reference/02-ai-sdk-ui/02-use-completion.mdx +185 -0
- package/docs/07-reference/02-ai-sdk-ui/03-use-object.mdx +196 -0
- package/docs/07-reference/02-ai-sdk-ui/31-convert-to-model-messages.mdx +231 -0
- package/docs/07-reference/02-ai-sdk-ui/32-prune-messages.mdx +108 -0
- package/docs/07-reference/02-ai-sdk-ui/40-create-ui-message-stream.mdx +162 -0
- package/docs/07-reference/02-ai-sdk-ui/41-create-ui-message-stream-response.mdx +119 -0
- package/docs/07-reference/02-ai-sdk-ui/42-pipe-ui-message-stream-to-response.mdx +77 -0
- package/docs/07-reference/02-ai-sdk-ui/43-read-ui-message-stream.mdx +57 -0
- package/docs/07-reference/02-ai-sdk-ui/46-infer-ui-tools.mdx +99 -0
- package/docs/07-reference/02-ai-sdk-ui/47-infer-ui-tool.mdx +75 -0
- package/docs/07-reference/02-ai-sdk-ui/50-direct-chat-transport.mdx +333 -0
- package/docs/07-reference/02-ai-sdk-ui/index.mdx +89 -0
- package/docs/07-reference/03-ai-sdk-rsc/01-stream-ui.mdx +767 -0
- package/docs/07-reference/03-ai-sdk-rsc/02-create-ai.mdx +90 -0
- package/docs/07-reference/03-ai-sdk-rsc/03-create-streamable-ui.mdx +91 -0
- package/docs/07-reference/03-ai-sdk-rsc/04-create-streamable-value.mdx +78 -0
- package/docs/07-reference/03-ai-sdk-rsc/05-read-streamable-value.mdx +79 -0
- package/docs/07-reference/03-ai-sdk-rsc/06-get-ai-state.mdx +50 -0
- package/docs/07-reference/03-ai-sdk-rsc/07-get-mutable-ai-state.mdx +70 -0
- package/docs/07-reference/03-ai-sdk-rsc/08-use-ai-state.mdx +26 -0
- package/docs/07-reference/03-ai-sdk-rsc/09-use-actions.mdx +42 -0
- package/docs/07-reference/03-ai-sdk-rsc/10-use-ui-state.mdx +35 -0
- package/docs/07-reference/03-ai-sdk-rsc/11-use-streamable-value.mdx +46 -0
- package/docs/07-reference/03-ai-sdk-rsc/20-render.mdx +266 -0
- package/docs/07-reference/03-ai-sdk-rsc/index.mdx +67 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-api-call-error.mdx +31 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-download-error.mdx +28 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-empty-response-body-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-argument-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-data-content-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-message-role-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-prompt-error.mdx +47 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-response-data-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-approval-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-input-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-json-parse-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-load-api-key-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-load-setting-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-message-conversion-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-content-generated-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-image-generated-error.mdx +36 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-object-generated-error.mdx +43 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-output-generated-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-speech-generated-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-such-model-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-such-provider-error.mdx +28 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-such-tool-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-transcript-generated-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-video-generated-error.mdx +39 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-retry-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-too-many-embedding-values-for-call-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-tool-call-not-found-for-approval-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-tool-call-repair-error.mdx +28 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-type-validation-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-ui-message-stream-error.mdx +67 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-unsupported-functionality-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/index.mdx +39 -0
- package/docs/07-reference/index.mdx +28 -0
- package/docs/08-migration-guides/00-versioning.mdx +46 -0
- package/docs/08-migration-guides/24-migration-guide-6-0.mdx +823 -0
- package/docs/08-migration-guides/25-migration-guide-5-0-data.mdx +882 -0
- package/docs/08-migration-guides/26-migration-guide-5-0.mdx +3427 -0
- package/docs/08-migration-guides/27-migration-guide-4-2.mdx +99 -0
- package/docs/08-migration-guides/28-migration-guide-4-1.mdx +14 -0
- package/docs/08-migration-guides/29-migration-guide-4-0.mdx +1157 -0
- package/docs/08-migration-guides/36-migration-guide-3-4.mdx +14 -0
- package/docs/08-migration-guides/37-migration-guide-3-3.mdx +64 -0
- package/docs/08-migration-guides/38-migration-guide-3-2.mdx +46 -0
- package/docs/08-migration-guides/39-migration-guide-3-1.mdx +168 -0
- package/docs/08-migration-guides/index.mdx +22 -0
- package/docs/09-troubleshooting/01-azure-stream-slow.mdx +33 -0
- package/docs/09-troubleshooting/03-server-actions-in-client-components.mdx +40 -0
- package/docs/09-troubleshooting/04-strange-stream-output.mdx +36 -0
- package/docs/09-troubleshooting/05-streamable-ui-errors.mdx +16 -0
- package/docs/09-troubleshooting/05-tool-invocation-missing-result.mdx +106 -0
- package/docs/09-troubleshooting/06-streaming-not-working-when-deployed.mdx +31 -0
- package/docs/09-troubleshooting/06-streaming-not-working-when-proxied.mdx +31 -0
- package/docs/09-troubleshooting/06-timeout-on-vercel.mdx +60 -0
- package/docs/09-troubleshooting/07-unclosed-streams.mdx +34 -0
- package/docs/09-troubleshooting/08-use-chat-failed-to-parse-stream.mdx +26 -0
- package/docs/09-troubleshooting/09-client-stream-error.mdx +25 -0
- package/docs/09-troubleshooting/10-use-chat-tools-no-response.mdx +32 -0
- package/docs/09-troubleshooting/11-use-chat-custom-request-options.mdx +149 -0
- package/docs/09-troubleshooting/12-typescript-performance-zod.mdx +46 -0
- package/docs/09-troubleshooting/12-use-chat-an-error-occurred.mdx +59 -0
- package/docs/09-troubleshooting/13-repeated-assistant-messages.mdx +73 -0
- package/docs/09-troubleshooting/14-stream-abort-handling.mdx +73 -0
- package/docs/09-troubleshooting/14-tool-calling-with-structured-outputs.mdx +48 -0
- package/docs/09-troubleshooting/15-abort-breaks-resumable-streams.mdx +55 -0
- package/docs/09-troubleshooting/15-stream-text-not-working.mdx +33 -0
- package/docs/09-troubleshooting/16-streaming-status-delay.mdx +63 -0
- package/docs/09-troubleshooting/17-use-chat-stale-body-data.mdx +141 -0
- package/docs/09-troubleshooting/18-ontoolcall-type-narrowing.mdx +66 -0
- package/docs/09-troubleshooting/19-unsupported-model-version.mdx +50 -0
- package/docs/09-troubleshooting/20-no-object-generated-content-filter.mdx +76 -0
- package/docs/09-troubleshooting/21-missing-tool-results-error.mdx +82 -0
- package/docs/09-troubleshooting/30-model-is-not-assignable-to-type.mdx +21 -0
- package/docs/09-troubleshooting/40-typescript-cannot-find-namespace-jsx.mdx +24 -0
- package/docs/09-troubleshooting/50-react-maximum-update-depth-exceeded.mdx +39 -0
- package/docs/09-troubleshooting/60-jest-cannot-find-module-ai-rsc.mdx +22 -0
- package/docs/09-troubleshooting/70-high-memory-usage-with-images.mdx +108 -0
- package/docs/09-troubleshooting/index.mdx +11 -0
- package/internal.d.ts +1 -0
- package/package.json +126 -0
- package/src/agent/agent.ts +156 -0
- package/src/agent/create-agent-ui-stream-response.ts +61 -0
- package/src/agent/create-agent-ui-stream.ts +84 -0
- package/src/agent/index.ts +37 -0
- package/src/agent/infer-agent-tools.ts +7 -0
- package/src/agent/infer-agent-ui-message.ts +11 -0
- package/src/agent/pipe-agent-ui-stream-to-response.ts +64 -0
- package/src/agent/tool-loop-agent-settings.ts +244 -0
- package/src/agent/tool-loop-agent.ts +206 -0
- package/src/embed/embed-many-result.ts +53 -0
- package/src/embed/embed-many.ts +386 -0
- package/src/embed/embed-result.ts +50 -0
- package/src/embed/embed.ts +217 -0
- package/src/embed/index.ts +4 -0
- package/src/error/index.ts +37 -0
- package/src/error/invalid-argument-error.ts +34 -0
- package/src/error/invalid-stream-part-error.ts +28 -0
- package/src/error/invalid-tool-approval-error.ts +26 -0
- package/src/error/invalid-tool-input-error.ts +33 -0
- package/src/error/missing-tool-result-error.ts +28 -0
- package/src/error/no-image-generated-error.ts +39 -0
- package/src/error/no-object-generated-error.ts +70 -0
- package/src/error/no-output-generated-error.ts +26 -0
- package/src/error/no-speech-generated-error.ts +28 -0
- package/src/error/no-such-tool-error.ts +35 -0
- package/src/error/no-transcript-generated-error.ts +30 -0
- package/src/error/no-video-generated-error.ts +57 -0
- package/src/error/tool-call-not-found-for-approval-error.ts +32 -0
- package/src/error/tool-call-repair-error.ts +30 -0
- package/src/error/ui-message-stream-error.ts +48 -0
- package/src/error/unsupported-model-version-error.ts +23 -0
- package/src/error/verify-no-object-generated-error.ts +27 -0
- package/src/generate-image/generate-image-result.ts +42 -0
- package/src/generate-image/generate-image.ts +361 -0
- package/src/generate-image/index.ts +18 -0
- package/src/generate-object/generate-object-result.ts +67 -0
- package/src/generate-object/generate-object.ts +517 -0
- package/src/generate-object/index.ts +9 -0
- package/src/generate-object/inject-json-instruction.ts +30 -0
- package/src/generate-object/output-strategy.ts +415 -0
- package/src/generate-object/parse-and-validate-object-result.ts +111 -0
- package/src/generate-object/repair-text.ts +12 -0
- package/src/generate-object/stream-object-result.ts +120 -0
- package/src/generate-object/stream-object.ts +985 -0
- package/src/generate-object/validate-object-generation-input.ts +144 -0
- package/src/generate-speech/generate-speech-result.ts +30 -0
- package/src/generate-speech/generate-speech.ts +191 -0
- package/src/generate-speech/generated-audio-file.ts +65 -0
- package/src/generate-speech/index.ts +3 -0
- package/src/generate-text/callback-events.ts +332 -0
- package/src/generate-text/collect-tool-approvals.ts +116 -0
- package/src/generate-text/content-part.ts +25 -0
- package/src/generate-text/execute-tool-call.ts +197 -0
- package/src/generate-text/extract-reasoning-content.ts +17 -0
- package/src/generate-text/extract-text-content.ts +15 -0
- package/src/generate-text/generate-text-result.ts +168 -0
- package/src/generate-text/generate-text.ts +1535 -0
- package/src/generate-text/generated-file.ts +70 -0
- package/src/generate-text/index.ts +74 -0
- package/src/generate-text/is-approval-needed.ts +29 -0
- package/src/generate-text/output-utils.ts +23 -0
- package/src/generate-text/output.ts +590 -0
- package/src/generate-text/parse-tool-call.ts +188 -0
- package/src/generate-text/prepare-step.ts +103 -0
- package/src/generate-text/prune-messages.ts +167 -0
- package/src/generate-text/reasoning-output.ts +20 -0
- package/src/generate-text/reasoning.ts +8 -0
- package/src/generate-text/response-message.ts +10 -0
- package/src/generate-text/run-tools-transformation.ts +442 -0
- package/src/generate-text/smooth-stream.ts +162 -0
- package/src/generate-text/step-result.ts +290 -0
- package/src/generate-text/stop-condition.ts +29 -0
- package/src/generate-text/stream-text-result.ts +463 -0
- package/src/generate-text/stream-text.ts +2807 -0
- package/src/generate-text/to-response-messages.ts +163 -0
- package/src/generate-text/tool-approval-request-output.ts +21 -0
- package/src/generate-text/tool-call-repair-function.ts +27 -0
- package/src/generate-text/tool-call.ts +47 -0
- package/src/generate-text/tool-error.ts +34 -0
- package/src/generate-text/tool-output-denied.ts +21 -0
- package/src/generate-text/tool-output.ts +7 -0
- package/src/generate-text/tool-result.ts +36 -0
- package/src/generate-text/tool-set.ts +14 -0
- package/src/generate-video/generate-video-result.ts +36 -0
- package/src/generate-video/generate-video.ts +402 -0
- package/src/generate-video/index.ts +3 -0
- package/src/global.ts +36 -0
- package/src/index.ts +49 -0
- package/src/logger/index.ts +6 -0
- package/src/logger/log-warnings.ts +140 -0
- package/src/middleware/add-tool-input-examples-middleware.ts +90 -0
- package/src/middleware/default-embedding-settings-middleware.ts +22 -0
- package/src/middleware/default-settings-middleware.ts +33 -0
- package/src/middleware/extract-json-middleware.ts +197 -0
- package/src/middleware/extract-reasoning-middleware.ts +249 -0
- package/src/middleware/index.ts +10 -0
- package/src/middleware/simulate-streaming-middleware.ts +79 -0
- package/src/middleware/wrap-embedding-model.ts +86 -0
- package/src/middleware/wrap-image-model.ts +85 -0
- package/src/middleware/wrap-language-model.ts +104 -0
- package/src/middleware/wrap-provider.ts +51 -0
- package/src/model/as-embedding-model-v3.ts +24 -0
- package/src/model/as-image-model-v3.ts +24 -0
- package/src/model/as-language-model-v3.ts +103 -0
- package/src/model/as-provider-v3.ts +36 -0
- package/src/model/as-speech-model-v3.ts +24 -0
- package/src/model/as-transcription-model-v3.ts +24 -0
- package/src/model/resolve-model.ts +159 -0
- package/src/prompt/call-settings.ts +148 -0
- package/src/prompt/content-part.ts +215 -0
- package/src/prompt/convert-to-language-model-prompt.ts +526 -0
- package/src/prompt/create-tool-model-output.ts +34 -0
- package/src/prompt/data-content.ts +134 -0
- package/src/prompt/index.ts +27 -0
- package/src/prompt/invalid-data-content-error.ts +29 -0
- package/src/prompt/invalid-message-role-error.ts +27 -0
- package/src/prompt/message-conversion-error.ts +28 -0
- package/src/prompt/message.ts +68 -0
- package/src/prompt/prepare-call-settings.ts +108 -0
- package/src/prompt/prepare-tools-and-tool-choice.ts +86 -0
- package/src/prompt/prompt.ts +43 -0
- package/src/prompt/split-data-url.ts +17 -0
- package/src/prompt/standardize-prompt.ts +99 -0
- package/src/prompt/wrap-gateway-error.ts +29 -0
- package/src/registry/custom-provider.ts +176 -0
- package/src/registry/index.ts +7 -0
- package/src/registry/no-such-provider-error.ts +41 -0
- package/src/registry/provider-registry.ts +328 -0
- package/src/rerank/index.ts +2 -0
- package/src/rerank/rerank-result.ts +70 -0
- package/src/rerank/rerank.ts +239 -0
- package/src/telemetry/assemble-operation-name.ts +21 -0
- package/src/telemetry/get-base-telemetry-attributes.ts +53 -0
- package/src/telemetry/get-global-telemetry-integration.ts +85 -0
- package/src/telemetry/get-tracer.ts +20 -0
- package/src/telemetry/index.ts +4 -0
- package/src/telemetry/noop-tracer.ts +69 -0
- package/src/telemetry/record-span.ts +75 -0
- package/src/telemetry/select-telemetry-attributes.ts +78 -0
- package/src/telemetry/stringify-for-telemetry.ts +33 -0
- package/src/telemetry/telemetry-integration-registry.ts +17 -0
- package/src/telemetry/telemetry-integration.ts +24 -0
- package/src/telemetry/telemetry-settings.ts +53 -0
- package/src/test/mock-embedding-model-v2.ts +35 -0
- package/src/test/mock-embedding-model-v3.ts +48 -0
- package/src/test/mock-image-model-v2.ts +28 -0
- package/src/test/mock-image-model-v3.ts +28 -0
- package/src/test/mock-language-model-v2.ts +72 -0
- package/src/test/mock-language-model-v3.ts +77 -0
- package/src/test/mock-provider-v2.ts +68 -0
- package/src/test/mock-provider-v3.ts +80 -0
- package/src/test/mock-reranking-model-v3.ts +25 -0
- package/src/test/mock-server-response.ts +69 -0
- package/src/test/mock-speech-model-v2.ts +24 -0
- package/src/test/mock-speech-model-v3.ts +24 -0
- package/src/test/mock-tracer.ts +156 -0
- package/src/test/mock-transcription-model-v2.ts +24 -0
- package/src/test/mock-transcription-model-v3.ts +24 -0
- package/src/test/mock-values.ts +4 -0
- package/src/test/mock-video-model-v3.ts +28 -0
- package/src/test/not-implemented.ts +3 -0
- package/src/text-stream/create-text-stream-response.ts +30 -0
- package/src/text-stream/index.ts +2 -0
- package/src/text-stream/pipe-text-stream-to-response.ts +38 -0
- package/src/transcribe/index.ts +2 -0
- package/src/transcribe/transcribe-result.ts +60 -0
- package/src/transcribe/transcribe.ts +187 -0
- package/src/types/embedding-model-middleware.ts +3 -0
- package/src/types/embedding-model.ts +18 -0
- package/src/types/image-model-middleware.ts +3 -0
- package/src/types/image-model-response-metadata.ts +16 -0
- package/src/types/image-model.ts +19 -0
- package/src/types/index.ts +29 -0
- package/src/types/json-value.ts +15 -0
- package/src/types/language-model-middleware.ts +3 -0
- package/src/types/language-model-request-metadata.ts +6 -0
- package/src/types/language-model-response-metadata.ts +21 -0
- package/src/types/language-model.ts +104 -0
- package/src/types/provider-metadata.ts +16 -0
- package/src/types/provider.ts +55 -0
- package/src/types/reranking-model.ts +6 -0
- package/src/types/speech-model-response-metadata.ts +21 -0
- package/src/types/speech-model.ts +6 -0
- package/src/types/transcription-model-response-metadata.ts +16 -0
- package/src/types/transcription-model.ts +9 -0
- package/src/types/usage.ts +200 -0
- package/src/types/video-model-response-metadata.ts +28 -0
- package/src/types/video-model.ts +11 -0
- package/src/types/warning.ts +7 -0
- package/src/ui/call-completion-api.ts +157 -0
- package/src/ui/chat-transport.ts +83 -0
- package/src/ui/chat.ts +751 -0
- package/src/ui/convert-file-list-to-file-ui-parts.ts +36 -0
- package/src/ui/convert-to-model-messages.ts +376 -0
- package/src/ui/default-chat-transport.ts +36 -0
- package/src/ui/direct-chat-transport.ts +118 -0
- package/src/ui/http-chat-transport.ts +284 -0
- package/src/ui/index.ts +71 -0
- package/src/ui/last-assistant-message-is-complete-with-approval-responses.ts +44 -0
- package/src/ui/last-assistant-message-is-complete-with-tool-calls.ts +39 -0
- package/src/ui/process-text-stream.ts +16 -0
- package/src/ui/process-ui-message-stream.ts +793 -0
- package/src/ui/text-stream-chat-transport.ts +23 -0
- package/src/ui/transform-text-to-ui-message-stream.ts +27 -0
- package/src/ui/ui-messages.ts +536 -0
- package/src/ui/use-completion.ts +84 -0
- package/src/ui/validate-ui-messages.ts +506 -0
- package/src/ui-message-stream/create-ui-message-stream-response.ts +44 -0
- package/src/ui-message-stream/create-ui-message-stream.ts +145 -0
- package/src/ui-message-stream/get-response-ui-message-id.ts +35 -0
- package/src/ui-message-stream/handle-ui-message-stream-finish.ts +170 -0
- package/src/ui-message-stream/index.ts +14 -0
- package/src/ui-message-stream/json-to-sse-transform-stream.ts +17 -0
- package/src/ui-message-stream/pipe-ui-message-stream-to-response.ts +51 -0
- package/src/ui-message-stream/read-ui-message-stream.ts +87 -0
- package/src/ui-message-stream/ui-message-chunks.ts +346 -0
- package/src/ui-message-stream/ui-message-stream-headers.ts +7 -0
- package/src/ui-message-stream/ui-message-stream-on-finish-callback.ts +32 -0
- package/src/ui-message-stream/ui-message-stream-on-step-finish-callback.ts +25 -0
- package/src/ui-message-stream/ui-message-stream-response-init.ts +14 -0
- package/src/ui-message-stream/ui-message-stream-writer.ts +24 -0
- package/src/util/as-array.ts +3 -0
- package/src/util/async-iterable-stream.ts +94 -0
- package/src/util/consume-stream.ts +31 -0
- package/src/util/cosine-similarity.ts +46 -0
- package/src/util/create-resolvable-promise.ts +30 -0
- package/src/util/create-stitchable-stream.ts +112 -0
- package/src/util/data-url.ts +17 -0
- package/src/util/deep-partial.ts +84 -0
- package/src/util/detect-media-type.ts +226 -0
- package/src/util/download/create-download.ts +13 -0
- package/src/util/download/download-function.ts +45 -0
- package/src/util/download/download.ts +69 -0
- package/src/util/error-handler.ts +1 -0
- package/src/util/fix-json.ts +401 -0
- package/src/util/get-potential-start-index.ts +39 -0
- package/src/util/index.ts +12 -0
- package/src/util/is-deep-equal-data.ts +48 -0
- package/src/util/is-non-empty-object.ts +5 -0
- package/src/util/job.ts +1 -0
- package/src/util/log-v2-compatibility-warning.ts +21 -0
- package/src/util/merge-abort-signals.ts +43 -0
- package/src/util/merge-objects.ts +79 -0
- package/src/util/notify.ts +22 -0
- package/src/util/now.ts +4 -0
- package/src/util/parse-partial-json.ts +30 -0
- package/src/util/prepare-headers.ts +14 -0
- package/src/util/prepare-retries.ts +47 -0
- package/src/util/retry-error.ts +41 -0
- package/src/util/retry-with-exponential-backoff.ts +154 -0
- package/src/util/serial-job-executor.ts +36 -0
- package/src/util/simulate-readable-stream.ts +39 -0
- package/src/util/split-array.ts +20 -0
- package/src/util/value-of.ts +65 -0
- package/src/util/write-to-server-response.ts +49 -0
- package/src/version.ts +5 -0
- package/test.d.ts +1 -0
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
import { LanguageModelV3StreamPart, SharedV3Warning } from '@ai-sdk/provider';
|
|
2
|
+
import {
|
|
3
|
+
getErrorMessage,
|
|
4
|
+
IdGenerator,
|
|
5
|
+
ModelMessage,
|
|
6
|
+
SystemModelMessage,
|
|
7
|
+
} from '@ai-sdk/provider-utils';
|
|
8
|
+
import { Tracer } from '@opentelemetry/api';
|
|
9
|
+
import { ToolCallNotFoundForApprovalError } from '../error/tool-call-not-found-for-approval-error';
|
|
10
|
+
import { TelemetrySettings } from '../telemetry/telemetry-settings';
|
|
11
|
+
import { FinishReason, LanguageModelUsage, ProviderMetadata } from '../types';
|
|
12
|
+
import { Source } from '../types/language-model';
|
|
13
|
+
import { asLanguageModelUsage } from '../types/usage';
|
|
14
|
+
import { executeToolCall } from './execute-tool-call';
|
|
15
|
+
import {
|
|
16
|
+
StreamTextOnToolCallFinishCallback,
|
|
17
|
+
StreamTextOnToolCallStartCallback,
|
|
18
|
+
} from './stream-text';
|
|
19
|
+
import { DefaultGeneratedFileWithType, GeneratedFile } from './generated-file';
|
|
20
|
+
import { isApprovalNeeded } from './is-approval-needed';
|
|
21
|
+
import { parseToolCall } from './parse-tool-call';
|
|
22
|
+
import { ToolApprovalRequestOutput } from './tool-approval-request-output';
|
|
23
|
+
import { TypedToolCall } from './tool-call';
|
|
24
|
+
import { ToolCallRepairFunction } from './tool-call-repair-function';
|
|
25
|
+
import { TypedToolError } from './tool-error';
|
|
26
|
+
import { TypedToolResult } from './tool-result';
|
|
27
|
+
import { ToolSet } from './tool-set';
|
|
28
|
+
|
|
29
|
+
export type SingleRequestTextStreamPart<TOOLS extends ToolSet> =
|
|
30
|
+
// Text blocks:
|
|
31
|
+
| {
|
|
32
|
+
type: 'text-start';
|
|
33
|
+
providerMetadata?: ProviderMetadata;
|
|
34
|
+
id: string;
|
|
35
|
+
}
|
|
36
|
+
| {
|
|
37
|
+
type: 'text-delta';
|
|
38
|
+
id: string;
|
|
39
|
+
providerMetadata?: ProviderMetadata;
|
|
40
|
+
delta: string;
|
|
41
|
+
}
|
|
42
|
+
| {
|
|
43
|
+
type: 'text-end';
|
|
44
|
+
providerMetadata?: ProviderMetadata;
|
|
45
|
+
id: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Reasoning blocks:
|
|
49
|
+
| {
|
|
50
|
+
type: 'reasoning-start';
|
|
51
|
+
providerMetadata?: ProviderMetadata;
|
|
52
|
+
id: string;
|
|
53
|
+
}
|
|
54
|
+
| {
|
|
55
|
+
type: 'reasoning-delta';
|
|
56
|
+
id: string;
|
|
57
|
+
providerMetadata?: ProviderMetadata;
|
|
58
|
+
delta: string;
|
|
59
|
+
}
|
|
60
|
+
| {
|
|
61
|
+
type: 'reasoning-end';
|
|
62
|
+
id: string;
|
|
63
|
+
providerMetadata?: ProviderMetadata;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Tool calls:
|
|
67
|
+
| {
|
|
68
|
+
type: 'tool-input-start';
|
|
69
|
+
id: string;
|
|
70
|
+
toolName: string;
|
|
71
|
+
providerMetadata?: ProviderMetadata;
|
|
72
|
+
dynamic?: boolean;
|
|
73
|
+
title?: string;
|
|
74
|
+
}
|
|
75
|
+
| {
|
|
76
|
+
type: 'tool-input-delta';
|
|
77
|
+
id: string;
|
|
78
|
+
delta: string;
|
|
79
|
+
providerMetadata?: ProviderMetadata;
|
|
80
|
+
}
|
|
81
|
+
| {
|
|
82
|
+
type: 'tool-input-end';
|
|
83
|
+
id: string;
|
|
84
|
+
providerMetadata?: ProviderMetadata;
|
|
85
|
+
}
|
|
86
|
+
| ToolApprovalRequestOutput<TOOLS>
|
|
87
|
+
|
|
88
|
+
// Other types:
|
|
89
|
+
| ({ type: 'source' } & Source)
|
|
90
|
+
| { type: 'file'; file: GeneratedFile; providerMetadata?: ProviderMetadata } // different because of GeneratedFile object
|
|
91
|
+
| ({ type: 'tool-call' } & TypedToolCall<TOOLS>)
|
|
92
|
+
| ({ type: 'tool-result' } & TypedToolResult<TOOLS>)
|
|
93
|
+
| ({ type: 'tool-error' } & TypedToolError<TOOLS>)
|
|
94
|
+
| { type: 'stream-start'; warnings: SharedV3Warning[] }
|
|
95
|
+
| {
|
|
96
|
+
type: 'response-metadata';
|
|
97
|
+
id?: string;
|
|
98
|
+
timestamp?: Date;
|
|
99
|
+
modelId?: string;
|
|
100
|
+
}
|
|
101
|
+
| {
|
|
102
|
+
type: 'finish';
|
|
103
|
+
finishReason: FinishReason;
|
|
104
|
+
rawFinishReason: string | undefined;
|
|
105
|
+
usage: LanguageModelUsage;
|
|
106
|
+
providerMetadata?: ProviderMetadata;
|
|
107
|
+
}
|
|
108
|
+
| { type: 'error'; error: unknown }
|
|
109
|
+
| { type: 'raw'; rawValue: unknown };
|
|
110
|
+
|
|
111
|
+
export function runToolsTransformation<TOOLS extends ToolSet>({
|
|
112
|
+
tools,
|
|
113
|
+
generatorStream,
|
|
114
|
+
tracer,
|
|
115
|
+
telemetry,
|
|
116
|
+
system,
|
|
117
|
+
messages,
|
|
118
|
+
abortSignal,
|
|
119
|
+
repairToolCall,
|
|
120
|
+
experimental_context,
|
|
121
|
+
generateId,
|
|
122
|
+
stepNumber,
|
|
123
|
+
model,
|
|
124
|
+
onToolCallStart,
|
|
125
|
+
onToolCallFinish,
|
|
126
|
+
}: {
|
|
127
|
+
tools: TOOLS | undefined;
|
|
128
|
+
generatorStream: ReadableStream<LanguageModelV3StreamPart>;
|
|
129
|
+
tracer: Tracer;
|
|
130
|
+
telemetry: TelemetrySettings | undefined;
|
|
131
|
+
system: string | SystemModelMessage | Array<SystemModelMessage> | undefined;
|
|
132
|
+
messages: ModelMessage[];
|
|
133
|
+
abortSignal: AbortSignal | undefined;
|
|
134
|
+
repairToolCall: ToolCallRepairFunction<TOOLS> | undefined;
|
|
135
|
+
experimental_context: unknown;
|
|
136
|
+
generateId: IdGenerator;
|
|
137
|
+
stepNumber?: number;
|
|
138
|
+
model?: { provider: string; modelId: string };
|
|
139
|
+
onToolCallStart?:
|
|
140
|
+
| StreamTextOnToolCallStartCallback<TOOLS>
|
|
141
|
+
| Array<StreamTextOnToolCallStartCallback<TOOLS> | undefined | null>;
|
|
142
|
+
onToolCallFinish?:
|
|
143
|
+
| StreamTextOnToolCallFinishCallback<TOOLS>
|
|
144
|
+
| Array<StreamTextOnToolCallFinishCallback<TOOLS> | undefined | null>;
|
|
145
|
+
}): ReadableStream<SingleRequestTextStreamPart<TOOLS>> {
|
|
146
|
+
// tool results stream
|
|
147
|
+
let toolResultsStreamController: ReadableStreamDefaultController<
|
|
148
|
+
SingleRequestTextStreamPart<TOOLS>
|
|
149
|
+
> | null = null;
|
|
150
|
+
const toolResultsStream = new ReadableStream<
|
|
151
|
+
SingleRequestTextStreamPart<TOOLS>
|
|
152
|
+
>({
|
|
153
|
+
start(controller) {
|
|
154
|
+
toolResultsStreamController = controller;
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// keep track of outstanding tool results for stream closing:
|
|
159
|
+
const outstandingToolResults = new Set<string>();
|
|
160
|
+
|
|
161
|
+
// keep track of tool inputs for provider-side tool results
|
|
162
|
+
const toolInputs = new Map<string, unknown>();
|
|
163
|
+
|
|
164
|
+
// keep track of parsed tool calls so provider-emitted approval requests can reference them
|
|
165
|
+
const toolCallsByToolCallId = new Map<string, TypedToolCall<TOOLS>>();
|
|
166
|
+
|
|
167
|
+
let canClose = false;
|
|
168
|
+
let finishChunk:
|
|
169
|
+
| (SingleRequestTextStreamPart<TOOLS> & { type: 'finish' })
|
|
170
|
+
| undefined = undefined;
|
|
171
|
+
|
|
172
|
+
function attemptClose() {
|
|
173
|
+
// close the tool results controller if no more outstanding tool calls
|
|
174
|
+
if (canClose && outstandingToolResults.size === 0) {
|
|
175
|
+
// we delay sending the finish chunk until all tool results (incl. delayed ones)
|
|
176
|
+
// are received to ensure that the frontend receives tool results before a message
|
|
177
|
+
// finish event arrives.
|
|
178
|
+
if (finishChunk != null) {
|
|
179
|
+
toolResultsStreamController!.enqueue(finishChunk);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
toolResultsStreamController!.close();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// forward stream
|
|
187
|
+
const forwardStream = new TransformStream<
|
|
188
|
+
LanguageModelV3StreamPart,
|
|
189
|
+
SingleRequestTextStreamPart<TOOLS>
|
|
190
|
+
>({
|
|
191
|
+
async transform(
|
|
192
|
+
chunk: LanguageModelV3StreamPart,
|
|
193
|
+
controller: TransformStreamDefaultController<
|
|
194
|
+
SingleRequestTextStreamPart<TOOLS>
|
|
195
|
+
>,
|
|
196
|
+
) {
|
|
197
|
+
const chunkType = chunk.type;
|
|
198
|
+
|
|
199
|
+
switch (chunkType) {
|
|
200
|
+
// forward:
|
|
201
|
+
case 'stream-start':
|
|
202
|
+
case 'text-start':
|
|
203
|
+
case 'text-delta':
|
|
204
|
+
case 'text-end':
|
|
205
|
+
case 'reasoning-start':
|
|
206
|
+
case 'reasoning-delta':
|
|
207
|
+
case 'reasoning-end':
|
|
208
|
+
case 'tool-input-start':
|
|
209
|
+
case 'tool-input-delta':
|
|
210
|
+
case 'tool-input-end':
|
|
211
|
+
case 'source':
|
|
212
|
+
case 'response-metadata':
|
|
213
|
+
case 'error':
|
|
214
|
+
case 'raw': {
|
|
215
|
+
controller.enqueue(chunk);
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
case 'file': {
|
|
220
|
+
controller.enqueue({
|
|
221
|
+
type: 'file',
|
|
222
|
+
file: new DefaultGeneratedFileWithType({
|
|
223
|
+
data: chunk.data,
|
|
224
|
+
mediaType: chunk.mediaType,
|
|
225
|
+
}),
|
|
226
|
+
...(chunk.providerMetadata != null
|
|
227
|
+
? { providerMetadata: chunk.providerMetadata }
|
|
228
|
+
: {}),
|
|
229
|
+
});
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
case 'finish': {
|
|
234
|
+
finishChunk = {
|
|
235
|
+
type: 'finish',
|
|
236
|
+
finishReason: chunk.finishReason.unified,
|
|
237
|
+
rawFinishReason: chunk.finishReason.raw,
|
|
238
|
+
usage: asLanguageModelUsage(chunk.usage),
|
|
239
|
+
providerMetadata: chunk.providerMetadata,
|
|
240
|
+
};
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
case 'tool-approval-request': {
|
|
245
|
+
const toolCall = toolCallsByToolCallId.get(chunk.toolCallId);
|
|
246
|
+
if (toolCall == null) {
|
|
247
|
+
toolResultsStreamController!.enqueue({
|
|
248
|
+
type: 'error',
|
|
249
|
+
error: new ToolCallNotFoundForApprovalError({
|
|
250
|
+
toolCallId: chunk.toolCallId,
|
|
251
|
+
approvalId: chunk.approvalId,
|
|
252
|
+
}),
|
|
253
|
+
});
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
controller.enqueue({
|
|
258
|
+
type: 'tool-approval-request',
|
|
259
|
+
approvalId: chunk.approvalId,
|
|
260
|
+
toolCall,
|
|
261
|
+
});
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// process tool call:
|
|
266
|
+
case 'tool-call': {
|
|
267
|
+
try {
|
|
268
|
+
const toolCall = await parseToolCall({
|
|
269
|
+
toolCall: chunk,
|
|
270
|
+
tools,
|
|
271
|
+
repairToolCall,
|
|
272
|
+
system,
|
|
273
|
+
messages,
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
toolCallsByToolCallId.set(toolCall.toolCallId, toolCall);
|
|
277
|
+
controller.enqueue(toolCall);
|
|
278
|
+
|
|
279
|
+
if (toolCall.invalid) {
|
|
280
|
+
toolResultsStreamController!.enqueue({
|
|
281
|
+
type: 'tool-error',
|
|
282
|
+
toolCallId: toolCall.toolCallId,
|
|
283
|
+
toolName: toolCall.toolName,
|
|
284
|
+
input: toolCall.input,
|
|
285
|
+
error: getErrorMessage(toolCall.error!),
|
|
286
|
+
dynamic: true,
|
|
287
|
+
title: toolCall.title,
|
|
288
|
+
});
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const tool = tools?.[toolCall.toolName];
|
|
293
|
+
|
|
294
|
+
if (tool == null) {
|
|
295
|
+
// ignore tool calls for tools that are not available,
|
|
296
|
+
// e.g. provider-executed dynamic tools
|
|
297
|
+
break;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (tool.onInputAvailable != null) {
|
|
301
|
+
await tool.onInputAvailable({
|
|
302
|
+
input: toolCall.input,
|
|
303
|
+
toolCallId: toolCall.toolCallId,
|
|
304
|
+
messages,
|
|
305
|
+
abortSignal,
|
|
306
|
+
experimental_context,
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (
|
|
311
|
+
await isApprovalNeeded({
|
|
312
|
+
tool,
|
|
313
|
+
toolCall,
|
|
314
|
+
messages,
|
|
315
|
+
experimental_context,
|
|
316
|
+
})
|
|
317
|
+
) {
|
|
318
|
+
toolResultsStreamController!.enqueue({
|
|
319
|
+
type: 'tool-approval-request',
|
|
320
|
+
approvalId: generateId(),
|
|
321
|
+
toolCall,
|
|
322
|
+
});
|
|
323
|
+
break;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
toolInputs.set(toolCall.toolCallId, toolCall.input);
|
|
327
|
+
|
|
328
|
+
// Only execute tools that are not provider-executed:
|
|
329
|
+
if (tool.execute != null && toolCall.providerExecuted !== true) {
|
|
330
|
+
const toolExecutionId = generateId(); // use our own id to guarantee uniqueness
|
|
331
|
+
outstandingToolResults.add(toolExecutionId);
|
|
332
|
+
|
|
333
|
+
// Note: we don't await the tool execution here (by leaving out 'await' on recordSpan),
|
|
334
|
+
// because we want to process the next chunk as soon as possible.
|
|
335
|
+
// This is important for the case where the tool execution takes a long time.
|
|
336
|
+
executeToolCall({
|
|
337
|
+
toolCall,
|
|
338
|
+
tools,
|
|
339
|
+
tracer,
|
|
340
|
+
telemetry,
|
|
341
|
+
messages,
|
|
342
|
+
abortSignal,
|
|
343
|
+
experimental_context,
|
|
344
|
+
stepNumber,
|
|
345
|
+
model,
|
|
346
|
+
onToolCallStart,
|
|
347
|
+
onToolCallFinish,
|
|
348
|
+
onPreliminaryToolResult: result => {
|
|
349
|
+
toolResultsStreamController!.enqueue(result);
|
|
350
|
+
},
|
|
351
|
+
})
|
|
352
|
+
.then(result => {
|
|
353
|
+
toolResultsStreamController!.enqueue(result);
|
|
354
|
+
})
|
|
355
|
+
.catch(error => {
|
|
356
|
+
toolResultsStreamController!.enqueue({
|
|
357
|
+
type: 'error',
|
|
358
|
+
error,
|
|
359
|
+
});
|
|
360
|
+
})
|
|
361
|
+
.finally(() => {
|
|
362
|
+
outstandingToolResults.delete(toolExecutionId);
|
|
363
|
+
attemptClose();
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
} catch (error) {
|
|
367
|
+
toolResultsStreamController!.enqueue({ type: 'error', error });
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
break;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
case 'tool-result': {
|
|
374
|
+
const toolName = chunk.toolName as keyof TOOLS & string;
|
|
375
|
+
|
|
376
|
+
if (chunk.isError) {
|
|
377
|
+
toolResultsStreamController!.enqueue({
|
|
378
|
+
type: 'tool-error',
|
|
379
|
+
toolCallId: chunk.toolCallId,
|
|
380
|
+
toolName,
|
|
381
|
+
input: toolInputs.get(chunk.toolCallId),
|
|
382
|
+
providerExecuted: true,
|
|
383
|
+
error: chunk.result,
|
|
384
|
+
dynamic: chunk.dynamic,
|
|
385
|
+
} as TypedToolError<TOOLS>);
|
|
386
|
+
} else {
|
|
387
|
+
controller.enqueue({
|
|
388
|
+
type: 'tool-result',
|
|
389
|
+
toolCallId: chunk.toolCallId,
|
|
390
|
+
toolName,
|
|
391
|
+
input: toolInputs.get(chunk.toolCallId),
|
|
392
|
+
output: chunk.result,
|
|
393
|
+
providerExecuted: true,
|
|
394
|
+
dynamic: chunk.dynamic,
|
|
395
|
+
} as TypedToolResult<TOOLS>);
|
|
396
|
+
}
|
|
397
|
+
break;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
default: {
|
|
401
|
+
const _exhaustiveCheck: never = chunkType;
|
|
402
|
+
throw new Error(`Unhandled chunk type: ${_exhaustiveCheck}`);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
},
|
|
406
|
+
|
|
407
|
+
flush() {
|
|
408
|
+
canClose = true;
|
|
409
|
+
attemptClose();
|
|
410
|
+
},
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
// combine the generator stream and the tool results stream
|
|
414
|
+
return new ReadableStream<SingleRequestTextStreamPart<TOOLS>>({
|
|
415
|
+
async start(controller) {
|
|
416
|
+
// need to wait for both pipes so there are no dangling promises that
|
|
417
|
+
// can cause uncaught promise rejections when the stream is aborted
|
|
418
|
+
return Promise.all([
|
|
419
|
+
generatorStream.pipeThrough(forwardStream).pipeTo(
|
|
420
|
+
new WritableStream({
|
|
421
|
+
write(chunk) {
|
|
422
|
+
controller.enqueue(chunk);
|
|
423
|
+
},
|
|
424
|
+
close() {
|
|
425
|
+
// the generator stream controller is automatically closed when it's consumed
|
|
426
|
+
},
|
|
427
|
+
}),
|
|
428
|
+
),
|
|
429
|
+
toolResultsStream.pipeTo(
|
|
430
|
+
new WritableStream({
|
|
431
|
+
write(chunk) {
|
|
432
|
+
controller.enqueue(chunk);
|
|
433
|
+
},
|
|
434
|
+
close() {
|
|
435
|
+
controller.close();
|
|
436
|
+
},
|
|
437
|
+
}),
|
|
438
|
+
),
|
|
439
|
+
]);
|
|
440
|
+
},
|
|
441
|
+
});
|
|
442
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { delay as originalDelay } from '@ai-sdk/provider-utils';
|
|
2
|
+
import { SharedV3ProviderMetadata } from '@ai-sdk/provider';
|
|
3
|
+
import { TextStreamPart } from './stream-text-result';
|
|
4
|
+
import { ToolSet } from './tool-set';
|
|
5
|
+
import { InvalidArgumentError } from '@ai-sdk/provider';
|
|
6
|
+
|
|
7
|
+
const CHUNKING_REGEXPS = {
|
|
8
|
+
word: /\S+\s+/m,
|
|
9
|
+
line: /\n+/m,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Detects the first chunk in a buffer.
|
|
14
|
+
*
|
|
15
|
+
* @param buffer - The buffer to detect the first chunk in.
|
|
16
|
+
*
|
|
17
|
+
* @returns The first detected chunk, or `undefined` if no chunk was detected.
|
|
18
|
+
*/
|
|
19
|
+
export type ChunkDetector = (buffer: string) => string | undefined | null;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Smooths text and reasoning streaming output.
|
|
23
|
+
*
|
|
24
|
+
* @param delayInMs - The delay in milliseconds between each chunk. Defaults to 10ms. Can be set to `null` to skip the delay.
|
|
25
|
+
* @param chunking - Controls how the text is chunked for streaming. Use "word" to stream word by word (default), "line" to stream line by line, provide a custom RegExp pattern for custom chunking, provide an Intl.Segmenter for locale-aware word segmentation (recommended for CJK languages), or provide a custom ChunkDetector function.
|
|
26
|
+
*
|
|
27
|
+
* @returns A transform stream that smooths text streaming output.
|
|
28
|
+
*/
|
|
29
|
+
export function smoothStream<TOOLS extends ToolSet>({
|
|
30
|
+
delayInMs = 10,
|
|
31
|
+
chunking = 'word',
|
|
32
|
+
_internal: { delay = originalDelay } = {},
|
|
33
|
+
}: {
|
|
34
|
+
delayInMs?: number | null;
|
|
35
|
+
chunking?: 'word' | 'line' | RegExp | ChunkDetector | Intl.Segmenter;
|
|
36
|
+
/**
|
|
37
|
+
* Internal. For test use only. May change without notice.
|
|
38
|
+
*/
|
|
39
|
+
_internal?: {
|
|
40
|
+
delay?: (delayInMs: number | null) => Promise<void>;
|
|
41
|
+
};
|
|
42
|
+
} = {}): (options: {
|
|
43
|
+
tools: TOOLS;
|
|
44
|
+
}) => TransformStream<TextStreamPart<TOOLS>, TextStreamPart<TOOLS>> {
|
|
45
|
+
let detectChunk: ChunkDetector;
|
|
46
|
+
|
|
47
|
+
// Check if chunking is an Intl.Segmenter (duck-typing for segment method)
|
|
48
|
+
if (
|
|
49
|
+
chunking != null &&
|
|
50
|
+
typeof chunking === 'object' &&
|
|
51
|
+
'segment' in chunking &&
|
|
52
|
+
typeof chunking.segment === 'function'
|
|
53
|
+
) {
|
|
54
|
+
const segmenter = chunking as Intl.Segmenter;
|
|
55
|
+
detectChunk = (buffer: string) => {
|
|
56
|
+
if (buffer.length === 0) return null;
|
|
57
|
+
const iterator = segmenter.segment(buffer)[Symbol.iterator]();
|
|
58
|
+
const first = iterator.next().value;
|
|
59
|
+
return first?.segment || null;
|
|
60
|
+
};
|
|
61
|
+
} else if (typeof chunking === 'function') {
|
|
62
|
+
detectChunk = buffer => {
|
|
63
|
+
const match = chunking(buffer);
|
|
64
|
+
|
|
65
|
+
if (match == null) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (!match.length) {
|
|
70
|
+
throw new Error(`Chunking function must return a non-empty string.`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (!buffer.startsWith(match)) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
`Chunking function must return a match that is a prefix of the buffer. Received: "${match}" expected to start with "${buffer}"`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return match;
|
|
80
|
+
};
|
|
81
|
+
} else {
|
|
82
|
+
const chunkingRegex =
|
|
83
|
+
typeof chunking === 'string'
|
|
84
|
+
? CHUNKING_REGEXPS[chunking]
|
|
85
|
+
: chunking instanceof RegExp
|
|
86
|
+
? chunking
|
|
87
|
+
: undefined;
|
|
88
|
+
|
|
89
|
+
if (chunkingRegex == null) {
|
|
90
|
+
throw new InvalidArgumentError({
|
|
91
|
+
argument: 'chunking',
|
|
92
|
+
message: `Chunking must be "word", "line", a RegExp, an Intl.Segmenter, or a ChunkDetector function. Received: ${chunking}`,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
detectChunk = buffer => {
|
|
97
|
+
const match = chunkingRegex.exec(buffer);
|
|
98
|
+
|
|
99
|
+
if (!match) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return buffer.slice(0, match.index) + match?.[0];
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return () => {
|
|
108
|
+
let buffer = '';
|
|
109
|
+
let id = '';
|
|
110
|
+
let type: 'text-delta' | 'reasoning-delta' | undefined = undefined;
|
|
111
|
+
let providerMetadata: SharedV3ProviderMetadata | undefined = undefined;
|
|
112
|
+
|
|
113
|
+
function flushBuffer(
|
|
114
|
+
controller: TransformStreamDefaultController<TextStreamPart<TOOLS>>,
|
|
115
|
+
) {
|
|
116
|
+
if (buffer.length > 0 && type !== undefined) {
|
|
117
|
+
controller.enqueue({
|
|
118
|
+
type,
|
|
119
|
+
text: buffer,
|
|
120
|
+
id,
|
|
121
|
+
...(providerMetadata != null ? { providerMetadata } : {}),
|
|
122
|
+
});
|
|
123
|
+
buffer = '';
|
|
124
|
+
providerMetadata = undefined;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return new TransformStream<TextStreamPart<TOOLS>, TextStreamPart<TOOLS>>({
|
|
129
|
+
async transform(chunk, controller) {
|
|
130
|
+
// Handle non-smoothable chunks: flush buffer and pass through
|
|
131
|
+
if (chunk.type !== 'text-delta' && chunk.type !== 'reasoning-delta') {
|
|
132
|
+
flushBuffer(controller);
|
|
133
|
+
controller.enqueue(chunk);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Flush buffer when type or id changes
|
|
138
|
+
if ((chunk.type !== type || chunk.id !== id) && buffer.length > 0) {
|
|
139
|
+
flushBuffer(controller);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
buffer += chunk.text;
|
|
143
|
+
id = chunk.id;
|
|
144
|
+
type = chunk.type;
|
|
145
|
+
|
|
146
|
+
// Preserve providerMetadata (e.g., Anthropic thinking signatures)
|
|
147
|
+
if (chunk.providerMetadata != null) {
|
|
148
|
+
providerMetadata = chunk.providerMetadata;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
let match;
|
|
152
|
+
|
|
153
|
+
while ((match = detectChunk(buffer)) != null) {
|
|
154
|
+
controller.enqueue({ type, text: match, id });
|
|
155
|
+
buffer = buffer.slice(match.length);
|
|
156
|
+
|
|
157
|
+
await delay(delayInMs);
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
};
|
|
162
|
+
}
|