ai 6.0.31 → 6.0.33
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 +12 -0
- package/dist/index.js +12 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +12 -2
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +1 -1
- package/dist/internal/index.mjs +1 -1
- 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 +163 -0
- package/docs/02-foundations/03-prompts.mdx +620 -0
- package/docs/02-foundations/04-tools.mdx +160 -0
- package/docs/02-foundations/05-streaming.mdx +62 -0
- package/docs/02-foundations/index.mdx +43 -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 +556 -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/index.mdx +44 -0
- package/docs/03-agents/01-overview.mdx +96 -0
- package/docs/03-agents/02-building-agents.mdx +367 -0
- package/docs/03-agents/03-workflows.mdx +370 -0
- package/docs/03-agents/04-loop-control.mdx +350 -0
- package/docs/03-agents/05-configuring-call-options.mdx +286 -0
- package/docs/03-agents/index.mdx +40 -0
- package/docs/03-ai-sdk-core/01-overview.mdx +33 -0
- package/docs/03-ai-sdk-core/05-generating-text.mdx +600 -0
- package/docs/03-ai-sdk-core/10-generating-structured-data.mdx +662 -0
- package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +1102 -0
- package/docs/03-ai-sdk-core/16-mcp-tools.mdx +375 -0
- package/docs/03-ai-sdk-core/20-prompt-engineering.mdx +144 -0
- package/docs/03-ai-sdk-core/25-settings.mdx +198 -0
- package/docs/03-ai-sdk-core/30-embeddings.mdx +247 -0
- package/docs/03-ai-sdk-core/31-reranking.mdx +218 -0
- package/docs/03-ai-sdk-core/35-image-generation.mdx +341 -0
- package/docs/03-ai-sdk-core/36-transcription.mdx +173 -0
- package/docs/03-ai-sdk-core/37-speech.mdx +167 -0
- package/docs/03-ai-sdk-core/40-middleware.mdx +480 -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 +218 -0
- package/docs/03-ai-sdk-core/60-telemetry.mdx +313 -0
- package/docs/03-ai-sdk-core/65-devtools.mdx +107 -0
- package/docs/03-ai-sdk-core/index.mdx +88 -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 +186 -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 +158 -0
- package/docs/05-ai-sdk-rsc/06-loading-state.mdx +273 -0
- package/docs/05-ai-sdk-rsc/08-error-handling.mdx +96 -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 +58 -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 +213 -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 +2142 -0
- package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +3215 -0
- package/docs/07-reference/01-ai-sdk-core/03-generate-object.mdx +780 -0
- package/docs/07-reference/01-ai-sdk-core/04-stream-object.mdx +1140 -0
- package/docs/07-reference/01-ai-sdk-core/05-embed.mdx +190 -0
- package/docs/07-reference/01-ai-sdk-core/06-embed-many.mdx +171 -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 +227 -0
- package/docs/07-reference/01-ai-sdk-core/11-transcribe.mdx +138 -0
- package/docs/07-reference/01-ai-sdk-core/12-generate-speech.mdx +214 -0
- package/docs/07-reference/01-ai-sdk-core/15-agent.mdx +203 -0
- package/docs/07-reference/01-ai-sdk-core/16-tool-loop-agent.mdx +449 -0
- package/docs/07-reference/01-ai-sdk-core/17-create-agent-ui-stream.mdx +148 -0
- package/docs/07-reference/01-ai-sdk-core/18-create-agent-ui-stream-response.mdx +168 -0
- package/docs/07-reference/01-ai-sdk-core/18-pipe-agent-ui-stream-to-response.mdx +144 -0
- package/docs/07-reference/01-ai-sdk-core/20-tool.mdx +196 -0
- package/docs/07-reference/01-ai-sdk-core/22-dynamic-tool.mdx +175 -0
- package/docs/07-reference/01-ai-sdk-core/23-create-mcp-client.mdx +410 -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 +55 -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 +182 -0
- package/docs/07-reference/01-ai-sdk-core/42-custom-provider.mdx +121 -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 +46 -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 +43 -0
- package/docs/07-reference/01-ai-sdk-core/91-create-id-generator.mdx +89 -0
- package/docs/07-reference/01-ai-sdk-core/index.mdx +159 -0
- package/docs/07-reference/02-ai-sdk-ui/01-use-chat.mdx +446 -0
- package/docs/07-reference/02-ai-sdk-ui/02-use-completion.mdx +179 -0
- package/docs/07-reference/02-ai-sdk-ui/03-use-object.mdx +178 -0
- package/docs/07-reference/02-ai-sdk-ui/31-convert-to-model-messages.mdx +230 -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 +151 -0
- package/docs/07-reference/02-ai-sdk-ui/41-create-ui-message-stream-response.mdx +113 -0
- package/docs/07-reference/02-ai-sdk-ui/42-pipe-ui-message-stream-to-response.mdx +73 -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 +48 -0
- package/docs/07-reference/03-ai-sdk-rsc/05-read-streamable-value.mdx +78 -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 +262 -0
- package/docs/07-reference/03-ai-sdk-rsc/index.mdx +67 -0
- package/docs/07-reference/04-stream-helpers/01-ai-stream.mdx +89 -0
- package/docs/07-reference/04-stream-helpers/02-streaming-text-response.mdx +79 -0
- package/docs/07-reference/04-stream-helpers/05-stream-to-response.mdx +108 -0
- package/docs/07-reference/04-stream-helpers/07-openai-stream.mdx +77 -0
- package/docs/07-reference/04-stream-helpers/08-anthropic-stream.mdx +79 -0
- package/docs/07-reference/04-stream-helpers/09-aws-bedrock-stream.mdx +91 -0
- package/docs/07-reference/04-stream-helpers/10-aws-bedrock-anthropic-stream.mdx +96 -0
- package/docs/07-reference/04-stream-helpers/10-aws-bedrock-messages-stream.mdx +96 -0
- package/docs/07-reference/04-stream-helpers/11-aws-bedrock-cohere-stream.mdx +93 -0
- package/docs/07-reference/04-stream-helpers/12-aws-bedrock-llama-2-stream.mdx +93 -0
- package/docs/07-reference/04-stream-helpers/13-cohere-stream.mdx +78 -0
- package/docs/07-reference/04-stream-helpers/14-google-generative-ai-stream.mdx +85 -0
- package/docs/07-reference/04-stream-helpers/15-hugging-face-stream.mdx +84 -0
- package/docs/07-reference/04-stream-helpers/16-langchain-adapter.mdx +98 -0
- package/docs/07-reference/04-stream-helpers/16-llamaindex-adapter.mdx +70 -0
- package/docs/07-reference/04-stream-helpers/17-mistral-stream.mdx +81 -0
- package/docs/07-reference/04-stream-helpers/18-replicate-stream.mdx +83 -0
- package/docs/07-reference/04-stream-helpers/19-inkeep-stream.mdx +80 -0
- package/docs/07-reference/04-stream-helpers/index.mdx +103 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-api-call-error.mdx +30 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-download-error.mdx +27 -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 +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-data-content.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 +25 -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-speech-generated-error.mdx +25 -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 +25 -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 +26 -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-unsupported-functionality-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/index.mdx +38 -0
- package/docs/07-reference/index.mdx +34 -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/02-client-side-function-calls-not-invoked.mdx +22 -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 +72 -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/index.mdx +11 -0
- package/package.json +8 -4
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: createAgentUIStream
|
|
3
|
+
description: API Reference for the createAgentUIStream utility.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# `createAgentUIStream`
|
|
7
|
+
|
|
8
|
+
The `createAgentUIStream` function executes an [Agent](/docs/reference/ai-sdk-core/agent), consumes an array of UI messages, and streams the agent's output as UI message chunks via an async iterable. This enables real-time, incremental rendering of AI assistant output with full access to tool use, intermediate reasoning, and interactive UI features in your own runtime—perfect for building chat APIs, dashboards, or bots powered by agents.
|
|
9
|
+
|
|
10
|
+
## Import
|
|
11
|
+
|
|
12
|
+
<Snippet text={`import { createAgentUIStream } from "ai"`} prompt={false} />
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { ToolLoopAgent, createAgentUIStream } from 'ai';
|
|
18
|
+
__PROVIDER_IMPORT__;
|
|
19
|
+
|
|
20
|
+
const agent = new ToolLoopAgent({
|
|
21
|
+
model: __MODEL__,
|
|
22
|
+
instructions: 'You are a helpful assistant.',
|
|
23
|
+
tools: { weather: weatherTool, calculator: calculatorTool },
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export async function* streamAgent(
|
|
27
|
+
uiMessages: unknown[],
|
|
28
|
+
abortSignal?: AbortSignal,
|
|
29
|
+
) {
|
|
30
|
+
const stream = await createAgentUIStream({
|
|
31
|
+
agent,
|
|
32
|
+
uiMessages,
|
|
33
|
+
abortSignal,
|
|
34
|
+
// ...other options (see below)
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
for await (const chunk of stream) {
|
|
38
|
+
yield chunk; // Each chunk is a UI message output from the agent.
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Parameters
|
|
44
|
+
|
|
45
|
+
<PropertiesTable
|
|
46
|
+
content={[
|
|
47
|
+
{
|
|
48
|
+
name: 'agent',
|
|
49
|
+
type: 'Agent',
|
|
50
|
+
isRequired: true,
|
|
51
|
+
description:
|
|
52
|
+
'The agent to run. Must define its `tools` and implement `.stream({ prompt, ... })`.',
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: 'uiMessages',
|
|
56
|
+
type: 'unknown[]',
|
|
57
|
+
isRequired: true,
|
|
58
|
+
description:
|
|
59
|
+
'Array of input UI message objects (e.g., user/assistant/chat history). These will be validated and converted for the agent.',
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: 'abortSignal',
|
|
63
|
+
type: 'AbortSignal',
|
|
64
|
+
isRequired: false,
|
|
65
|
+
description:
|
|
66
|
+
'Optional abort signal to cancel the stream early (for example, if the client disconnects).',
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
name: 'timeout',
|
|
70
|
+
type: 'number | { totalMs?: number }',
|
|
71
|
+
isRequired: false,
|
|
72
|
+
description:
|
|
73
|
+
'Timeout in milliseconds. Can be specified as a number or as an object with a totalMs property. The call will be aborted if it takes longer than the specified timeout. Can be used alongside abortSignal.',
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
name: 'options',
|
|
77
|
+
type: 'CALL_OPTIONS',
|
|
78
|
+
isRequired: false,
|
|
79
|
+
description:
|
|
80
|
+
'Optional agent call options, only needed if your agent expects extra configuration (see agent generic parameters).',
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
name: 'experimental_transform',
|
|
84
|
+
type: 'StreamTextTransform | StreamTextTransform[]',
|
|
85
|
+
isRequired: false,
|
|
86
|
+
description:
|
|
87
|
+
'Optional transformations to apply to the agent output stream (experimental).',
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
name: '...UIMessageStreamOptions',
|
|
91
|
+
type: 'UIMessageStreamOptions',
|
|
92
|
+
isRequired: false,
|
|
93
|
+
description:
|
|
94
|
+
'Additional options to control the output stream, such as including sources or usage data.',
|
|
95
|
+
},
|
|
96
|
+
]}
|
|
97
|
+
/>
|
|
98
|
+
|
|
99
|
+
## Returns
|
|
100
|
+
|
|
101
|
+
A `Promise<AsyncIterableStream<UIMessageChunk>>`, where each yielded chunk is a UI message output from the agent (see [`UIMessage`](/docs/reference/ai-sdk-core/ui-message)). This can be consumed with any async iterator loop, or piped to a streaming HTTP response, socket, or any other sink.
|
|
102
|
+
|
|
103
|
+
## Example
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import { createAgentUIStream } from 'ai';
|
|
107
|
+
|
|
108
|
+
const controller = new AbortController();
|
|
109
|
+
|
|
110
|
+
const stream = await createAgentUIStream({
|
|
111
|
+
agent,
|
|
112
|
+
uiMessages: [{ role: 'user', content: 'What is the weather in SF today?' }],
|
|
113
|
+
abortSignal: controller.signal,
|
|
114
|
+
sendStart: true,
|
|
115
|
+
// ...other UIMessageStreamOptions
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
for await (const chunk of stream) {
|
|
119
|
+
// Each chunk is a UI message update — stream it to your client, dashboard, logs, etc.
|
|
120
|
+
console.log(chunk);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Call controller.abort() to cancel the agent operation early.
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## How It Works
|
|
127
|
+
|
|
128
|
+
1. **UI Message Validation:** The input `uiMessages` array is validated and normalized using the agent's `tools` definition. Any invalid messages cause an error.
|
|
129
|
+
2. **Conversion to Model Messages:** The validated UI messages are converted into model-specific message format, as required by the agent.
|
|
130
|
+
3. **Agent Streaming:** The agent's `.stream({ prompt, ... })` method is invoked with the converted model messages, optional call options, abort signal, and any experimental transforms.
|
|
131
|
+
4. **UI Message Stream Building:** The result stream is converted and exposed as a streaming async iterable of UI message chunks for you to consume.
|
|
132
|
+
|
|
133
|
+
## Notes
|
|
134
|
+
|
|
135
|
+
- The agent **must** implement the `.stream({ prompt, ... })` method and define its supported `tools` property.
|
|
136
|
+
- This utility returns an async iterable for maximal streaming flexibility. For HTTP responses, see [`createAgentUIStreamResponse`](/docs/reference/ai-sdk-core/create-agent-ui-stream-response) (Web) or [`pipeAgentUIStreamToResponse`](/docs/reference/ai-sdk-core/pipe-agent-ui-stream-to-response) (Node.js).
|
|
137
|
+
- The `uiMessages` parameter is named `uiMessages`, **not** just `messages`.
|
|
138
|
+
- You can provide advanced options via [`UIMessageStreamOptions`](/docs/reference/ai-sdk-core/ui-message-stream-options) (for example, to include sources or usage).
|
|
139
|
+
- To cancel the stream, pass an [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) via the `abortSignal` parameter.
|
|
140
|
+
|
|
141
|
+
## See Also
|
|
142
|
+
|
|
143
|
+
- [`Agent`](/docs/reference/ai-sdk-core/agent)
|
|
144
|
+
- [`ToolLoopAgent`](/docs/reference/ai-sdk-core/tool-loop-agent)
|
|
145
|
+
- [`UIMessage`](/docs/reference/ai-sdk-core/ui-message)
|
|
146
|
+
- [`UIMessageStreamOptions`](/docs/reference/ai-sdk-core/ui-message-stream-options)
|
|
147
|
+
- [`createAgentUIStreamResponse`](/docs/reference/ai-sdk-core/create-agent-ui-stream-response)
|
|
148
|
+
- [`pipeAgentUIStreamToResponse`](/docs/reference/ai-sdk-core/pipe-agent-ui-stream-to-response)
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: createAgentUIStreamResponse
|
|
3
|
+
description: API Reference for the createAgentUIStreamResponse utility.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# `createAgentUIStreamResponse`
|
|
7
|
+
|
|
8
|
+
The `createAgentUIStreamResponse` function executes an [Agent](/docs/reference/ai-sdk-core/agent), runs its streaming output as a UI message stream, and returns an HTTP [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) object whose body is the live, streaming UI message output. This is designed for API routes that deliver real-time agent results, such as chat endpoints or streaming tool-use operations.
|
|
9
|
+
|
|
10
|
+
## Import
|
|
11
|
+
|
|
12
|
+
<Snippet
|
|
13
|
+
text={`import { createAgentUIStreamResponse } from "ai"`}
|
|
14
|
+
prompt={false}
|
|
15
|
+
/>
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { ToolLoopAgent, createAgentUIStreamResponse } from 'ai';
|
|
21
|
+
__PROVIDER_IMPORT__;
|
|
22
|
+
|
|
23
|
+
const agent = new ToolLoopAgent({
|
|
24
|
+
model: __MODEL__,
|
|
25
|
+
instructions: 'You are a helpful assistant.',
|
|
26
|
+
tools: { weather: weatherTool, calculator: calculatorTool },
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export async function POST(request: Request) {
|
|
30
|
+
const { messages } = await request.json();
|
|
31
|
+
|
|
32
|
+
// Optional: support cancellation (aborts on disconnect, etc.)
|
|
33
|
+
const abortController = new AbortController();
|
|
34
|
+
|
|
35
|
+
return createAgentUIStreamResponse({
|
|
36
|
+
agent,
|
|
37
|
+
uiMessages: messages,
|
|
38
|
+
abortSignal: abortController.signal, // optional
|
|
39
|
+
// ...other UIMessageStreamOptions like sendSources, includeUsage, experimental_transform, etc.
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Parameters
|
|
45
|
+
|
|
46
|
+
<PropertiesTable
|
|
47
|
+
content={[
|
|
48
|
+
{
|
|
49
|
+
name: 'agent',
|
|
50
|
+
type: 'Agent',
|
|
51
|
+
isRequired: true,
|
|
52
|
+
description:
|
|
53
|
+
'The agent instance to stream responses from. Must implement `.stream({ prompt, ... })` and define the `tools` property.',
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
name: 'uiMessages',
|
|
57
|
+
type: 'unknown[]',
|
|
58
|
+
isRequired: true,
|
|
59
|
+
description:
|
|
60
|
+
'Array of input UI messages provided to the agent (e.g., user and assistant messages).',
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name: 'abortSignal',
|
|
64
|
+
type: 'AbortSignal',
|
|
65
|
+
isRequired: false,
|
|
66
|
+
description:
|
|
67
|
+
'Optional abort signal to cancel streaming, e.g., on client disconnect. This should be an [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) instance.',
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
name: 'timeout',
|
|
71
|
+
type: 'number | { totalMs?: number }',
|
|
72
|
+
isRequired: false,
|
|
73
|
+
description:
|
|
74
|
+
'Timeout in milliseconds. Can be specified as a number or as an object with a totalMs property. The call will be aborted if it takes longer than the specified timeout. Can be used alongside abortSignal.',
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
name: 'options',
|
|
78
|
+
type: 'CALL_OPTIONS',
|
|
79
|
+
isRequired: false,
|
|
80
|
+
description:
|
|
81
|
+
'Optional agent call options, for agents with generic parameter `CALL_OPTIONS`.',
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
name: 'experimental_transform',
|
|
85
|
+
type: 'StreamTextTransform | StreamTextTransform[]',
|
|
86
|
+
isRequired: false,
|
|
87
|
+
description:
|
|
88
|
+
'Optional stream transforms to post-process text output—the same as in lower-level streaming APIs.',
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
name: '...UIMessageStreamOptions',
|
|
92
|
+
type: 'UIMessageStreamOptions',
|
|
93
|
+
isRequired: false,
|
|
94
|
+
description:
|
|
95
|
+
'Other UI message output options—such as `sendSources`, `includeUsage`, and more. See [`UIMessageStreamOptions`](/docs/reference/ai-sdk-core/ui-message-stream-options).',
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
name: 'headers',
|
|
99
|
+
type: 'HeadersInit',
|
|
100
|
+
isRequired: false,
|
|
101
|
+
description: 'Optional HTTP headers to include in the Response object.',
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
name: 'status',
|
|
105
|
+
type: 'number',
|
|
106
|
+
isRequired: false,
|
|
107
|
+
description: 'Optional HTTP status code.',
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
name: 'statusText',
|
|
111
|
+
type: 'string',
|
|
112
|
+
isRequired: false,
|
|
113
|
+
description: 'Optional HTTP status text.',
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
name: 'consumeSseStream',
|
|
117
|
+
type: 'boolean',
|
|
118
|
+
isRequired: false,
|
|
119
|
+
description:
|
|
120
|
+
'If true, consume the stream as SSE (server-sent events) instead of default streaming.',
|
|
121
|
+
},
|
|
122
|
+
]}
|
|
123
|
+
/>
|
|
124
|
+
|
|
125
|
+
## Returns
|
|
126
|
+
|
|
127
|
+
A `Promise<Response>` whose `body` is a streaming UI message output from the agent. Use this as the return value of API/server handlers in serverless, Next.js, Express, Hono, or edge runtime contexts.
|
|
128
|
+
|
|
129
|
+
## Example: Next.js API Route Handler
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
import { createAgentUIStreamResponse } from 'ai';
|
|
133
|
+
import { MyCustomAgent } from '@/agent/my-custom-agent';
|
|
134
|
+
|
|
135
|
+
export async function POST(request: Request) {
|
|
136
|
+
const { messages } = await request.json();
|
|
137
|
+
|
|
138
|
+
return createAgentUIStreamResponse({
|
|
139
|
+
agent: MyCustomAgent,
|
|
140
|
+
uiMessages: messages,
|
|
141
|
+
sendSources: true, // (optional)
|
|
142
|
+
includeUsage: true, // (optional)
|
|
143
|
+
// headers, status, abortSignal, and other UIMessageStreamOptions also supported
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## How It Works
|
|
149
|
+
|
|
150
|
+
- 1. **UI Message Validation:** Validates the incoming `uiMessages` array according to the agent's specified tools and requirements.
|
|
151
|
+
- 2. **Model Message Conversion:** Converts validated UI messages into the internal model message format for the agent.
|
|
152
|
+
- 3. **Streaming Agent Output:** Invokes the agent’s `.stream({ prompt, ... })` to get a stream of chunks (steps/UI messages).
|
|
153
|
+
- 4. **HTTP Response Creation:** Wraps the output stream as a readable HTTP `Response` object that streams UI message chunks to the client.
|
|
154
|
+
|
|
155
|
+
## Notes
|
|
156
|
+
|
|
157
|
+
- Your agent **must** implement `.stream({ prompt, ... })` and define a `tools` property (even if it's just `{}`) to work with this function.
|
|
158
|
+
- **Server Only:** This API should only be called in backend/server-side contexts (API routes, edge/serverless/server route handlers, etc.). Not for browser use.
|
|
159
|
+
- Additional options (`headers`, `status`, UI stream options, transforms, etc.) are available for advanced scenarios.
|
|
160
|
+
- This leverages [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) so your platform/client must support HTTP streaming consumption.
|
|
161
|
+
|
|
162
|
+
## See Also
|
|
163
|
+
|
|
164
|
+
- [`Agent`](/docs/reference/ai-sdk-core/agent)
|
|
165
|
+
- [`ToolLoopAgent`](/docs/reference/ai-sdk-core/tool-loop-agent)
|
|
166
|
+
- [`UIMessage`](/docs/reference/ai-sdk-core/ui-message)
|
|
167
|
+
- [`UIMessageStreamOptions`](/docs/reference/ai-sdk-core/ui-message-stream-options)
|
|
168
|
+
- [`createAgentUIStream`](/docs/reference/ai-sdk-core/create-agent-ui-stream)
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: pipeAgentUIStreamToResponse
|
|
3
|
+
description: API Reference for the pipeAgentUIStreamToResponse utility.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# `pipeAgentUIStreamToResponse`
|
|
7
|
+
|
|
8
|
+
The `pipeAgentUIStreamToResponse` function runs an [Agent](/docs/reference/ai-sdk-core/agent) and streams the resulting UI message output directly to a Node.js [`ServerResponse`](https://nodejs.org/api/http.html#class-httpserverresponse) object. This is ideal for building real-time streaming API endpoints (for chat, tool use, etc.) in Node.js-based frameworks like Express, Hono, or custom Node servers.
|
|
9
|
+
|
|
10
|
+
## Import
|
|
11
|
+
|
|
12
|
+
<Snippet
|
|
13
|
+
text={`import { pipeAgentUIStreamToResponse } from "ai"`}
|
|
14
|
+
prompt={false}
|
|
15
|
+
/>
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { pipeAgentUIStreamToResponse } from 'ai';
|
|
21
|
+
import { MyAgent } from './agent';
|
|
22
|
+
|
|
23
|
+
export async function handler(req, res) {
|
|
24
|
+
const { messages } = JSON.parse(req.body);
|
|
25
|
+
|
|
26
|
+
await pipeAgentUIStreamToResponse({
|
|
27
|
+
response: res, // Node.js ServerResponse
|
|
28
|
+
agent: MyAgent,
|
|
29
|
+
uiMessages: messages, // Required: array of input UI messages
|
|
30
|
+
// abortSignal: optional AbortSignal for cancellation
|
|
31
|
+
// status: 200,
|
|
32
|
+
// headers: { ... },
|
|
33
|
+
// ...other optional UI message stream options
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Parameters
|
|
39
|
+
|
|
40
|
+
<PropertiesTable
|
|
41
|
+
content={[
|
|
42
|
+
{
|
|
43
|
+
name: 'response',
|
|
44
|
+
type: 'ServerResponse',
|
|
45
|
+
isRequired: true,
|
|
46
|
+
description:
|
|
47
|
+
'The Node.js ServerResponse object to pipe UI message stream output into.',
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
name: 'agent',
|
|
51
|
+
type: 'Agent',
|
|
52
|
+
isRequired: true,
|
|
53
|
+
description:
|
|
54
|
+
'An agent instance implementing `.stream({ prompt, ... })` and defining a `tools` property.',
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
name: 'uiMessages',
|
|
58
|
+
type: 'unknown[]',
|
|
59
|
+
isRequired: true,
|
|
60
|
+
description:
|
|
61
|
+
'Array of input UI messages sent to the agent (such as user/assistant message objects).',
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
name: 'abortSignal',
|
|
65
|
+
type: 'AbortSignal',
|
|
66
|
+
isRequired: false,
|
|
67
|
+
description:
|
|
68
|
+
'Optional abort signal to cancel streaming (e.g., on client disconnect). Supply an [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal), for example from an `AbortController`.',
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: 'timeout',
|
|
72
|
+
type: 'number | { totalMs?: number }',
|
|
73
|
+
isRequired: false,
|
|
74
|
+
description:
|
|
75
|
+
'Timeout in milliseconds. Can be specified as a number or as an object with a totalMs property. The call will be aborted if it takes longer than the specified timeout. Can be used alongside abortSignal.',
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
name: 'options',
|
|
79
|
+
type: 'CALL_OPTIONS',
|
|
80
|
+
isRequired: false,
|
|
81
|
+
description:
|
|
82
|
+
'Optional agent call options, for agents configured with generic parameter `CALL_OPTIONS`.',
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
name: 'experimental_transform',
|
|
86
|
+
type: 'StreamTextTransform | StreamTextTransform[]',
|
|
87
|
+
isRequired: false,
|
|
88
|
+
description:
|
|
89
|
+
'Optional stream text transformation(s) applied to agent output.',
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
name: '...UIMessageStreamResponseInit & UIMessageStreamOptions',
|
|
93
|
+
type: 'object',
|
|
94
|
+
isRequired: false,
|
|
95
|
+
description:
|
|
96
|
+
'Options for streaming headers, status, SSE stream config, and additional UI message stream control.',
|
|
97
|
+
},
|
|
98
|
+
]}
|
|
99
|
+
/>
|
|
100
|
+
|
|
101
|
+
## Returns
|
|
102
|
+
|
|
103
|
+
A `Promise<void>`. The function completes when the UI message stream has been fully sent to the provided ServerResponse.
|
|
104
|
+
|
|
105
|
+
## Example: Express Route Handler
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import { pipeAgentUIStreamToResponse } from 'ai';
|
|
109
|
+
import { openaiWebSearchAgent } from './openai-web-search-agent';
|
|
110
|
+
|
|
111
|
+
app.post('/chat', async (req, res) => {
|
|
112
|
+
// Use req.body.messages as input UI messages
|
|
113
|
+
await pipeAgentUIStreamToResponse({
|
|
114
|
+
response: res,
|
|
115
|
+
agent: openaiWebSearchAgent,
|
|
116
|
+
uiMessages: req.body.messages,
|
|
117
|
+
// abortSignal: yourController.signal
|
|
118
|
+
// status: 200,
|
|
119
|
+
// headers: { ... },
|
|
120
|
+
// ...more options
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## How It Works
|
|
126
|
+
|
|
127
|
+
1. **Runs the Agent:** Calls the agent’s `.stream` method with the provided UI messages and options, converting them into model messages as needed.
|
|
128
|
+
2. **Streams UI Message Output:** Pipes the agent output as a UI message stream to the `ServerResponse`, sending data via streaming HTTP responses (including appropriate headers).
|
|
129
|
+
3. **Abort Signal Handling:** If `abortSignal` is supplied, streaming is cancelled as soon as the signal is triggered (such as on client disconnect).
|
|
130
|
+
4. **No Response Return:** Unlike Edge/serverless APIs that return a `Response`, this function writes bytes directly to the ServerResponse and does not return a response object.
|
|
131
|
+
|
|
132
|
+
## Notes
|
|
133
|
+
|
|
134
|
+
- **Abort Handling:** For best robustness, use an `AbortSignal` (for example, wired to Express/Hono client disconnects) to ensure quick cancellation of agent computation and streaming.
|
|
135
|
+
- **Node.js Only:** Only works with Node.js [ServerResponse](https://nodejs.org/api/http.html#class-httpserverresponse) objects (e.g., in Express, Hono’s node adapter, etc.), not Edge/serverless/web Response APIs.
|
|
136
|
+
- **Streaming Support:** Make sure your client (and any proxies) correctly support streaming HTTP responses for full effect.
|
|
137
|
+
- **Parameter Names:** The property for input messages is `uiMessages` (not `messages`) for consistency with SDK agent utilities.
|
|
138
|
+
|
|
139
|
+
## See Also
|
|
140
|
+
|
|
141
|
+
- [`createAgentUIStreamResponse`](/docs/reference/ai-sdk-core/create-agent-ui-stream-response)
|
|
142
|
+
- [`Agent`](/docs/reference/ai-sdk-core/agent)
|
|
143
|
+
- [`UIMessageStreamOptions`](/docs/reference/ai-sdk-core/ui-message-stream-options)
|
|
144
|
+
- [`UIMessage`](/docs/reference/ai-sdk-core/ui-message)
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: tool
|
|
3
|
+
description: Helper function for tool type inference
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# `tool()`
|
|
7
|
+
|
|
8
|
+
Tool is a helper function that infers the tool input for its `execute` method.
|
|
9
|
+
|
|
10
|
+
It does not have any runtime behavior, but it helps TypeScript infer the types of the input for the `execute` method.
|
|
11
|
+
|
|
12
|
+
Without this helper function, TypeScript is unable to connect the `inputSchema` property to the `execute` method,
|
|
13
|
+
and the argument types of `execute` cannot be inferred.
|
|
14
|
+
|
|
15
|
+
```ts highlight={"1,4,9,10"}
|
|
16
|
+
import { tool } from 'ai';
|
|
17
|
+
import { z } from 'zod';
|
|
18
|
+
|
|
19
|
+
export const weatherTool = tool({
|
|
20
|
+
description: 'Get the weather in a location',
|
|
21
|
+
inputSchema: z.object({
|
|
22
|
+
location: z.string().describe('The location to get the weather for'),
|
|
23
|
+
}),
|
|
24
|
+
// location below is inferred to be a string:
|
|
25
|
+
execute: async ({ location }) => ({
|
|
26
|
+
location,
|
|
27
|
+
temperature: 72 + Math.floor(Math.random() * 21) - 10,
|
|
28
|
+
}),
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Import
|
|
33
|
+
|
|
34
|
+
<Snippet text={`import { tool } from "ai"`} prompt={false} />
|
|
35
|
+
|
|
36
|
+
## API Signature
|
|
37
|
+
|
|
38
|
+
### Parameters
|
|
39
|
+
|
|
40
|
+
<PropertiesTable
|
|
41
|
+
content={[
|
|
42
|
+
{
|
|
43
|
+
name: 'tool',
|
|
44
|
+
type: 'Tool',
|
|
45
|
+
description: 'The tool definition.',
|
|
46
|
+
properties: [
|
|
47
|
+
{
|
|
48
|
+
type: 'Tool',
|
|
49
|
+
parameters: [
|
|
50
|
+
{
|
|
51
|
+
name: 'description',
|
|
52
|
+
isOptional: true,
|
|
53
|
+
type: 'string',
|
|
54
|
+
description:
|
|
55
|
+
'Information about the purpose of the tool including details on how and when it can be used by the model.',
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: 'inputSchema',
|
|
59
|
+
type: 'Zod Schema | JSON Schema',
|
|
60
|
+
description:
|
|
61
|
+
'The schema of the input that the tool expects. The language model will use this to generate the input. It is also used to validate the output of the language model. Use descriptions to make the input understandable for the language model. You can either pass in a Zod schema or a JSON schema (using the `jsonSchema` function).',
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
name: 'inputExamples',
|
|
65
|
+
isOptional: true,
|
|
66
|
+
type: 'Array<{ input: INPUT }>',
|
|
67
|
+
description:
|
|
68
|
+
'An optional list of input examples that show the language model what the input should look like.',
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: 'strict',
|
|
72
|
+
isOptional: true,
|
|
73
|
+
type: 'boolean',
|
|
74
|
+
description:
|
|
75
|
+
'Strict mode setting for the tool. Providers that support strict mode will use this setting to determine how the input should be generated. Strict mode will always produce valid inputs, but it might limit what input schemas are supported.',
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
name: 'execute',
|
|
79
|
+
isOptional: true,
|
|
80
|
+
type: 'async (input: INPUT, options: ToolExecutionOptions) => RESULT | Promise<RESULT> | AsyncIterable<RESULT>',
|
|
81
|
+
description:
|
|
82
|
+
'An async function that is called with the arguments from the tool call and produces a result or a results iterable. If an iterable is provided, all results but the last one are considered preliminary. If not provided, the tool will not be executed automatically.',
|
|
83
|
+
properties: [
|
|
84
|
+
{
|
|
85
|
+
type: 'ToolExecutionOptions',
|
|
86
|
+
parameters: [
|
|
87
|
+
{
|
|
88
|
+
name: 'toolCallId',
|
|
89
|
+
type: 'string',
|
|
90
|
+
description:
|
|
91
|
+
'The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.',
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
name: 'messages',
|
|
95
|
+
type: 'ModelMessage[]',
|
|
96
|
+
description:
|
|
97
|
+
'Messages that were sent to the language model to initiate the response that contained the tool call. The messages do not include the system prompt nor the assistant response that contained the tool call.',
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
name: 'abortSignal',
|
|
101
|
+
type: 'AbortSignal',
|
|
102
|
+
isOptional: true,
|
|
103
|
+
description:
|
|
104
|
+
'An optional abort signal that indicates that the overall operation should be aborted.',
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
name: 'experimental_context',
|
|
108
|
+
type: 'unknown',
|
|
109
|
+
isOptional: true,
|
|
110
|
+
description:
|
|
111
|
+
'Context that is passed into tool execution. Experimental (can break in patch releases).',
|
|
112
|
+
},
|
|
113
|
+
],
|
|
114
|
+
},
|
|
115
|
+
],
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
name: 'outputSchema',
|
|
119
|
+
isOptional: true,
|
|
120
|
+
type: 'Zod Schema | JSON Schema',
|
|
121
|
+
description:
|
|
122
|
+
'The schema of the output that the tool produces. Used for validation and type inference.',
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
name: 'toModelOutput',
|
|
126
|
+
isOptional: true,
|
|
127
|
+
type: '({toolCallId: string; input: INPUT; output: OUTPUT}) => ToolResultOutput | PromiseLike<ToolResultOutput>',
|
|
128
|
+
description:
|
|
129
|
+
'Optional conversion function that maps the tool result to an output that can be used by the language model. If not provided, the tool result will be sent as a JSON object.',
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
name: 'onInputStart',
|
|
133
|
+
isOptional: true,
|
|
134
|
+
type: '(options: ToolExecutionOptions) => void | PromiseLike<void>',
|
|
135
|
+
description:
|
|
136
|
+
'Optional function that is called when the argument streaming starts. Only called when the tool is used in a streaming context.',
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
name: 'onInputDelta',
|
|
140
|
+
isOptional: true,
|
|
141
|
+
type: '(options: { inputTextDelta: string } & ToolExecutionOptions) => void | PromiseLike<void>',
|
|
142
|
+
description:
|
|
143
|
+
'Optional function that is called when an argument streaming delta is available. Only called when the tool is used in a streaming context.',
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
name: 'onInputAvailable',
|
|
147
|
+
isOptional: true,
|
|
148
|
+
type: '(options: { input: INPUT } & ToolExecutionOptions) => void | PromiseLike<void>',
|
|
149
|
+
description:
|
|
150
|
+
'Optional function that is called when a tool call can be started, even if the execute function is not provided.',
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
name: 'providerOptions',
|
|
154
|
+
isOptional: true,
|
|
155
|
+
type: 'ProviderOptions',
|
|
156
|
+
description:
|
|
157
|
+
'Additional provider-specific metadata. They are passed through to the provider from the AI SDK and enable provider-specific functionality that can be fully encapsulated in the provider.',
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
name: 'type',
|
|
161
|
+
isOptional: true,
|
|
162
|
+
type: "'function' | 'provider-defined'",
|
|
163
|
+
description:
|
|
164
|
+
'The type of the tool. Defaults to "function" for regular tools. Use "provider-defined" for provider-specific tools.',
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
name: 'id',
|
|
168
|
+
isOptional: true,
|
|
169
|
+
type: 'string',
|
|
170
|
+
description:
|
|
171
|
+
'The ID of the tool for provider-defined tools. Should follow the format `<provider-name>.<unique-tool-name>`. Required when type is "provider-defined".',
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
name: 'name',
|
|
175
|
+
isOptional: true,
|
|
176
|
+
type: 'string',
|
|
177
|
+
description:
|
|
178
|
+
'The name of the tool that the user must use in the tool set. Required when type is "provider-defined".',
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
name: 'args',
|
|
182
|
+
isOptional: true,
|
|
183
|
+
type: 'Record<string, unknown>',
|
|
184
|
+
description:
|
|
185
|
+
'The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool. Required when type is "provider-defined".',
|
|
186
|
+
},
|
|
187
|
+
],
|
|
188
|
+
},
|
|
189
|
+
],
|
|
190
|
+
},
|
|
191
|
+
]}
|
|
192
|
+
/>
|
|
193
|
+
|
|
194
|
+
### Returns
|
|
195
|
+
|
|
196
|
+
The tool that was passed in.
|