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,174 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Transport
|
|
3
|
+
description: Learn how to use custom transports with useChat.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Transport
|
|
7
|
+
|
|
8
|
+
The `useChat` transport system provides fine-grained control over how messages are sent to your API endpoints and how responses are processed. This is particularly useful for alternative communication protocols like WebSockets, custom authentication patterns, or specialized backend integrations.
|
|
9
|
+
|
|
10
|
+
## Default Transport
|
|
11
|
+
|
|
12
|
+
By default, `useChat` uses HTTP POST requests to send messages to `/api/chat`:
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { useChat } from '@ai-sdk/react';
|
|
16
|
+
|
|
17
|
+
// Uses default HTTP transport
|
|
18
|
+
const { messages, sendMessage } = useChat();
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
This is equivalent to:
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
import { useChat } from '@ai-sdk/react';
|
|
25
|
+
import { DefaultChatTransport } from 'ai';
|
|
26
|
+
|
|
27
|
+
const { messages, sendMessage } = useChat({
|
|
28
|
+
transport: new DefaultChatTransport({
|
|
29
|
+
api: '/api/chat',
|
|
30
|
+
}),
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Custom Transport Configuration
|
|
35
|
+
|
|
36
|
+
Configure the default transport with custom options:
|
|
37
|
+
|
|
38
|
+
```tsx
|
|
39
|
+
import { useChat } from '@ai-sdk/react';
|
|
40
|
+
import { DefaultChatTransport } from 'ai';
|
|
41
|
+
|
|
42
|
+
const { messages, sendMessage } = useChat({
|
|
43
|
+
transport: new DefaultChatTransport({
|
|
44
|
+
api: '/api/custom-chat',
|
|
45
|
+
headers: {
|
|
46
|
+
Authorization: 'Bearer your-token',
|
|
47
|
+
'X-API-Version': '2024-01',
|
|
48
|
+
},
|
|
49
|
+
credentials: 'include',
|
|
50
|
+
}),
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Dynamic Configuration
|
|
55
|
+
|
|
56
|
+
You can also provide functions that return configuration values. This is useful for authentication tokens that need to be refreshed, or for configuration that depends on runtime conditions:
|
|
57
|
+
|
|
58
|
+
```tsx
|
|
59
|
+
const { messages, sendMessage } = useChat({
|
|
60
|
+
transport: new DefaultChatTransport({
|
|
61
|
+
api: '/api/chat',
|
|
62
|
+
headers: () => ({
|
|
63
|
+
Authorization: `Bearer ${getAuthToken()}`,
|
|
64
|
+
'X-User-ID': getCurrentUserId(),
|
|
65
|
+
}),
|
|
66
|
+
body: () => ({
|
|
67
|
+
sessionId: getCurrentSessionId(),
|
|
68
|
+
preferences: getUserPreferences(),
|
|
69
|
+
}),
|
|
70
|
+
credentials: () => 'include',
|
|
71
|
+
}),
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Request Transformation
|
|
76
|
+
|
|
77
|
+
Transform requests before sending to your API:
|
|
78
|
+
|
|
79
|
+
```tsx
|
|
80
|
+
const { messages, sendMessage } = useChat({
|
|
81
|
+
transport: new DefaultChatTransport({
|
|
82
|
+
api: '/api/chat',
|
|
83
|
+
prepareSendMessagesRequest: ({ id, messages, trigger, messageId }) => {
|
|
84
|
+
return {
|
|
85
|
+
headers: {
|
|
86
|
+
'X-Session-ID': id,
|
|
87
|
+
},
|
|
88
|
+
body: {
|
|
89
|
+
messages: messages.slice(-10), // Only send last 10 messages
|
|
90
|
+
trigger,
|
|
91
|
+
messageId,
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
},
|
|
95
|
+
}),
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Direct Agent Transport
|
|
100
|
+
|
|
101
|
+
For scenarios where you want to communicate directly with an [Agent](/docs/reference/ai-sdk-core/agent) without going through HTTP, you can use `DirectChatTransport`. This transport invokes the agent's `stream()` method directly in-process.
|
|
102
|
+
|
|
103
|
+
This is useful for:
|
|
104
|
+
|
|
105
|
+
- **Server-side rendering**: Run the agent on the server without an API endpoint
|
|
106
|
+
- **Testing**: Test chat functionality without network requests
|
|
107
|
+
- **Single-process applications**: Desktop or CLI apps where client and agent run together
|
|
108
|
+
|
|
109
|
+
```tsx
|
|
110
|
+
import { useChat } from '@ai-sdk/react';
|
|
111
|
+
import { DirectChatTransport, ToolLoopAgent } from 'ai';
|
|
112
|
+
__PROVIDER_IMPORT__;
|
|
113
|
+
|
|
114
|
+
const agent = new ToolLoopAgent({
|
|
115
|
+
model: __MODEL__,
|
|
116
|
+
instructions: 'You are a helpful assistant.',
|
|
117
|
+
tools: {
|
|
118
|
+
weather: weatherTool,
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const { messages, sendMessage } = useChat({
|
|
123
|
+
transport: new DirectChatTransport({ agent }),
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### How It Works
|
|
128
|
+
|
|
129
|
+
Unlike `DefaultChatTransport` which sends HTTP requests:
|
|
130
|
+
|
|
131
|
+
1. `DirectChatTransport` validates incoming UI messages
|
|
132
|
+
2. Converts them to model messages using `convertToModelMessages`
|
|
133
|
+
3. Calls the agent's `stream()` method directly
|
|
134
|
+
4. Returns the result as a UI message stream via `toUIMessageStream()`
|
|
135
|
+
|
|
136
|
+
### Configuration Options
|
|
137
|
+
|
|
138
|
+
You can pass additional options to customize the stream output:
|
|
139
|
+
|
|
140
|
+
```tsx
|
|
141
|
+
const transport = new DirectChatTransport({
|
|
142
|
+
agent,
|
|
143
|
+
// Pass options to the agent
|
|
144
|
+
options: { customOption: 'value' },
|
|
145
|
+
// Configure what's sent to the client
|
|
146
|
+
sendReasoning: true,
|
|
147
|
+
sendSources: true,
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
<Note>
|
|
152
|
+
`DirectChatTransport` does not support stream reconnection since there is no
|
|
153
|
+
persistent server-side stream. The `reconnectToStream()` method always returns
|
|
154
|
+
`null`.
|
|
155
|
+
</Note>
|
|
156
|
+
|
|
157
|
+
For complete API details, see the [DirectChatTransport reference](/docs/reference/ai-sdk-ui/direct-chat-transport).
|
|
158
|
+
|
|
159
|
+
## Building Custom Transports
|
|
160
|
+
|
|
161
|
+
To understand how to build your own transport, refer to the source code of the default implementation:
|
|
162
|
+
|
|
163
|
+
- **[DefaultChatTransport](https://github.com/vercel/ai/blob/main/packages/ai/src/ui/default-chat-transport.ts)** - The complete default HTTP transport implementation
|
|
164
|
+
- **[HttpChatTransport](https://github.com/vercel/ai/blob/main/packages/ai/src/ui/http-chat-transport.ts)** - Base HTTP transport with request handling
|
|
165
|
+
- **[ChatTransport Interface](https://github.com/vercel/ai/blob/main/packages/ai/src/ui/chat-transport.ts)** - The transport interface you need to implement
|
|
166
|
+
|
|
167
|
+
These implementations show you exactly how to:
|
|
168
|
+
|
|
169
|
+
- Handle the `sendMessages` method
|
|
170
|
+
- Process UI message streams
|
|
171
|
+
- Transform requests and responses
|
|
172
|
+
- Handle errors and connection management
|
|
173
|
+
|
|
174
|
+
The transport system gives you complete control over how your chat application communicates, enabling integration with any backend protocol or service.
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Reading UIMessage Streams
|
|
3
|
+
description: Learn how to read UIMessage streams.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Reading UI Message Streams
|
|
7
|
+
|
|
8
|
+
`UIMessage` streams are useful outside of traditional chat use cases. You can consume them for terminal UIs, custom stream processing on the client, or React Server Components (RSC).
|
|
9
|
+
|
|
10
|
+
The `readUIMessageStream` helper transforms a stream of `UIMessageChunk` objects into an `AsyncIterableStream` of `UIMessage` objects, allowing you to process messages as they're being constructed.
|
|
11
|
+
|
|
12
|
+
## Basic Usage
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { readUIMessageStream, streamText } from 'ai';
|
|
16
|
+
__PROVIDER_IMPORT__;
|
|
17
|
+
|
|
18
|
+
async function main() {
|
|
19
|
+
const result = streamText({
|
|
20
|
+
model: __MODEL__,
|
|
21
|
+
prompt: 'Write a short story about a robot.',
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
for await (const uiMessage of readUIMessageStream({
|
|
25
|
+
stream: result.toUIMessageStream(),
|
|
26
|
+
})) {
|
|
27
|
+
console.log('Current message state:', uiMessage);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Tool Calls Integration
|
|
33
|
+
|
|
34
|
+
Handle streaming responses that include tool calls:
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
import { readUIMessageStream, streamText, tool } from 'ai';
|
|
38
|
+
__PROVIDER_IMPORT__;
|
|
39
|
+
import { z } from 'zod';
|
|
40
|
+
|
|
41
|
+
async function handleToolCalls() {
|
|
42
|
+
const result = streamText({
|
|
43
|
+
model: __MODEL__,
|
|
44
|
+
tools: {
|
|
45
|
+
weather: tool({
|
|
46
|
+
description: 'Get the weather in a location',
|
|
47
|
+
inputSchema: z.object({
|
|
48
|
+
location: z.string().describe('The location to get the weather for'),
|
|
49
|
+
}),
|
|
50
|
+
execute: ({ location }) => ({
|
|
51
|
+
location,
|
|
52
|
+
temperature: 72 + Math.floor(Math.random() * 21) - 10,
|
|
53
|
+
}),
|
|
54
|
+
}),
|
|
55
|
+
},
|
|
56
|
+
prompt: 'What is the weather in Tokyo?',
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
for await (const uiMessage of readUIMessageStream({
|
|
60
|
+
stream: result.toUIMessageStream(),
|
|
61
|
+
})) {
|
|
62
|
+
// Handle different part types
|
|
63
|
+
uiMessage.parts.forEach(part => {
|
|
64
|
+
switch (part.type) {
|
|
65
|
+
case 'text':
|
|
66
|
+
console.log('Text:', part.text);
|
|
67
|
+
break;
|
|
68
|
+
case 'tool-call':
|
|
69
|
+
console.log('Tool called:', part.toolName, 'with args:', part.args);
|
|
70
|
+
break;
|
|
71
|
+
case 'tool-result':
|
|
72
|
+
console.log('Tool result:', part.result);
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Resuming Conversations
|
|
81
|
+
|
|
82
|
+
Resume streaming from a previous message state:
|
|
83
|
+
|
|
84
|
+
```tsx
|
|
85
|
+
import { readUIMessageStream, streamText } from 'ai';
|
|
86
|
+
__PROVIDER_IMPORT__;
|
|
87
|
+
|
|
88
|
+
async function resumeConversation(lastMessage: UIMessage) {
|
|
89
|
+
const result = streamText({
|
|
90
|
+
model: __MODEL__,
|
|
91
|
+
messages: [
|
|
92
|
+
{ role: 'user', content: 'Continue our previous conversation.' },
|
|
93
|
+
],
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// Resume from the last message
|
|
97
|
+
for await (const uiMessage of readUIMessageStream({
|
|
98
|
+
stream: result.toUIMessageStream(),
|
|
99
|
+
message: lastMessage, // Resume from this message
|
|
100
|
+
})) {
|
|
101
|
+
console.log('Resumed message:', uiMessage);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
```
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Message Metadata
|
|
3
|
+
description: Learn how to attach and use metadata with messages in AI SDK UI
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Message Metadata
|
|
7
|
+
|
|
8
|
+
Message metadata allows you to attach custom information to messages at the message level. This is useful for tracking timestamps, model information, token usage, user context, and other message-level data.
|
|
9
|
+
|
|
10
|
+
## Overview
|
|
11
|
+
|
|
12
|
+
Message metadata differs from [data parts](/docs/ai-sdk-ui/streaming-data) in that it's attached at the message level rather than being part of the message content. While data parts are ideal for dynamic content that forms part of the message, metadata is perfect for information about the message itself.
|
|
13
|
+
|
|
14
|
+
## Getting Started
|
|
15
|
+
|
|
16
|
+
Here's a simple example of using message metadata to track timestamps and model information:
|
|
17
|
+
|
|
18
|
+
### Defining Metadata Types
|
|
19
|
+
|
|
20
|
+
First, define your metadata type for type safety:
|
|
21
|
+
|
|
22
|
+
```tsx filename="app/types.ts"
|
|
23
|
+
import { UIMessage } from 'ai';
|
|
24
|
+
import { z } from 'zod';
|
|
25
|
+
|
|
26
|
+
// Define your metadata schema
|
|
27
|
+
export const messageMetadataSchema = z.object({
|
|
28
|
+
createdAt: z.number().optional(),
|
|
29
|
+
model: z.string().optional(),
|
|
30
|
+
totalTokens: z.number().optional(),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
export type MessageMetadata = z.infer<typeof messageMetadataSchema>;
|
|
34
|
+
|
|
35
|
+
// Create a typed UIMessage
|
|
36
|
+
export type MyUIMessage = UIMessage<MessageMetadata>;
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Sending Metadata from the Server
|
|
40
|
+
|
|
41
|
+
Use the `messageMetadata` callback in `toUIMessageStreamResponse` to send metadata at different streaming stages:
|
|
42
|
+
|
|
43
|
+
```ts filename="app/api/chat/route.ts" highlight="11-20"
|
|
44
|
+
import { convertToModelMessages, streamText } from 'ai';
|
|
45
|
+
__PROVIDER_IMPORT__;
|
|
46
|
+
import type { MyUIMessage } from '@/types';
|
|
47
|
+
|
|
48
|
+
export async function POST(req: Request) {
|
|
49
|
+
const { messages }: { messages: MyUIMessage[] } = await req.json();
|
|
50
|
+
|
|
51
|
+
const result = streamText({
|
|
52
|
+
model: __MODEL__,
|
|
53
|
+
messages: await convertToModelMessages(messages),
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
return result.toUIMessageStreamResponse({
|
|
57
|
+
originalMessages: messages, // pass this in for type-safe return objects
|
|
58
|
+
messageMetadata: ({ part }) => {
|
|
59
|
+
// Send metadata when streaming starts
|
|
60
|
+
if (part.type === 'start') {
|
|
61
|
+
return {
|
|
62
|
+
createdAt: Date.now(),
|
|
63
|
+
model: 'your-model-id',
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Send additional metadata when streaming completes
|
|
68
|
+
if (part.type === 'finish') {
|
|
69
|
+
return {
|
|
70
|
+
totalTokens: part.totalUsage.totalTokens,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
<Note>
|
|
79
|
+
To enable type-safe metadata return object in `messageMetadata`, pass in the
|
|
80
|
+
`originalMessages` parameter typed to your UIMessage type.
|
|
81
|
+
</Note>
|
|
82
|
+
|
|
83
|
+
### Accessing Metadata on the Client
|
|
84
|
+
|
|
85
|
+
Access metadata through the `message.metadata` property:
|
|
86
|
+
|
|
87
|
+
```tsx filename="app/page.tsx" highlight="8,18-23"
|
|
88
|
+
'use client';
|
|
89
|
+
|
|
90
|
+
import { useChat } from '@ai-sdk/react';
|
|
91
|
+
import { DefaultChatTransport } from 'ai';
|
|
92
|
+
import type { MyUIMessage } from '@/types';
|
|
93
|
+
|
|
94
|
+
export default function Chat() {
|
|
95
|
+
const { messages } = useChat<MyUIMessage>({
|
|
96
|
+
transport: new DefaultChatTransport({
|
|
97
|
+
api: '/api/chat',
|
|
98
|
+
}),
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
return (
|
|
102
|
+
<div>
|
|
103
|
+
{messages.map(message => (
|
|
104
|
+
<div key={message.id}>
|
|
105
|
+
<div>
|
|
106
|
+
{message.role === 'user' ? 'User: ' : 'AI: '}
|
|
107
|
+
{message.metadata?.createdAt && (
|
|
108
|
+
<span className="text-sm text-gray-500">
|
|
109
|
+
{new Date(message.metadata.createdAt).toLocaleTimeString()}
|
|
110
|
+
</span>
|
|
111
|
+
)}
|
|
112
|
+
</div>
|
|
113
|
+
|
|
114
|
+
{/* Render message content */}
|
|
115
|
+
{message.parts.map((part, index) =>
|
|
116
|
+
part.type === 'text' ? <div key={index}>{part.text}</div> : null,
|
|
117
|
+
)}
|
|
118
|
+
|
|
119
|
+
{/* Display additional metadata */}
|
|
120
|
+
{message.metadata?.totalTokens && (
|
|
121
|
+
<div className="text-xs text-gray-400">
|
|
122
|
+
{message.metadata.totalTokens} tokens
|
|
123
|
+
</div>
|
|
124
|
+
)}
|
|
125
|
+
</div>
|
|
126
|
+
))}
|
|
127
|
+
</div>
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
<Note>
|
|
133
|
+
For streaming arbitrary data that changes during generation, consider using
|
|
134
|
+
[data parts](/docs/ai-sdk-ui/streaming-data) instead.
|
|
135
|
+
</Note>
|
|
136
|
+
|
|
137
|
+
## Common Use Cases
|
|
138
|
+
|
|
139
|
+
Message metadata is ideal for:
|
|
140
|
+
|
|
141
|
+
- **Timestamps**: When messages were created or completed
|
|
142
|
+
- **Model Information**: Which AI model was used
|
|
143
|
+
- **Token Usage**: Track costs and usage limits
|
|
144
|
+
- **User Context**: User IDs, session information
|
|
145
|
+
- **Performance Metrics**: Generation time, time to first token
|
|
146
|
+
- **Quality Indicators**: Finish reason, confidence scores
|
|
147
|
+
|
|
148
|
+
## See Also
|
|
149
|
+
|
|
150
|
+
- [Chatbot Guide](/docs/ai-sdk-ui/chatbot#message-metadata) - Message metadata in the context of building chatbots
|
|
151
|
+
- [Streaming Data](/docs/ai-sdk-ui/streaming-data#message-metadata-vs-data-parts) - Comparison with data parts
|
|
152
|
+
- [UIMessage Reference](/docs/reference/ai-sdk-core/ui-message) - Complete UIMessage type reference
|