ai 5.1.0-beta.9 → 6.0.0-beta.100

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/README.md CHANGED
@@ -2,65 +2,170 @@
2
2
 
3
3
  # AI SDK
4
4
 
5
- The [AI SDK](https://ai-sdk.dev/docs) is a TypeScript toolkit designed to help you build AI-powered applications using popular frameworks like Next.js, React, Svelte, Vue and runtimes like Node.js.
5
+ The [AI SDK](https://ai-sdk.dev/docs) is a TypeScript toolkit designed to help you build AI-powered applications and agents using popular frameworks like Next.js, React, Svelte, Vue and runtimes like Node.js.
6
6
 
7
7
  To learn more about how to use the AI SDK, check out our [API Reference](https://ai-sdk.dev/docs/reference) and [Documentation](https://ai-sdk.dev/docs).
8
8
 
9
9
  ## Installation
10
10
 
11
- You will need Node.js 18+ and pnpm installed on your local development machine.
11
+ You will need Node.js 18+ and npm (or another package manager) installed on your local development machine.
12
12
 
13
13
  ```shell
14
14
  npm install ai
15
15
  ```
16
16
 
17
- ## Usage
18
-
19
- ### AI SDK Core
17
+ ## Unified Provider Architecture
20
18
 
21
- The [AI SDK Core](https://ai-sdk.dev/docs/ai-sdk-core/overview) module provides a unified API to interact with model providers like [OpenAI](https://ai-sdk.dev/providers/ai-sdk-providers/openai), [Anthropic](https://ai-sdk.dev/providers/ai-sdk-providers/anthropic), [Google](https://ai-sdk.dev/providers/ai-sdk-providers/google-generative-ai), and more.
22
-
23
- You will then install the model provider of your choice.
19
+ The AI SDK provides a [unified API](https://ai-sdk.dev/docs/foundations/providers-and-models) to interact with model providers like [OpenAI](https://ai-sdk.dev/providers/ai-sdk-providers/openai), [Anthropic](https://ai-sdk.dev/providers/ai-sdk-providers/anthropic), [Google](https://ai-sdk.dev/providers/ai-sdk-providers/google-generative-ai), and [more](https://ai-sdk.dev/providers/ai-sdk-providers).
24
20
 
25
21
  ```shell
26
- npm install @ai-sdk/openai
22
+ npm install @ai-sdk/openai @ai-sdk/anthropic @ai-sdk/google
27
23
  ```
28
24
 
29
- ###### @/index.ts (Node.js Runtime)
25
+ Alternatively you can use the [Vercel AI Gateway](https://vercel.com/docs/ai-gateway).
26
+
27
+ ## Usage
28
+
29
+ ### Generating Text
30
30
 
31
31
  ```ts
32
32
  import { generateText } from 'ai';
33
- import { openai } from '@ai-sdk/openai'; // Ensure OPENAI_API_KEY environment variable is set
34
33
 
35
34
  const { text } = await generateText({
36
- model: openai('gpt-4o'),
37
- system: 'You are a friendly assistant!',
38
- prompt: 'Why is the sky blue?',
35
+ model: 'openai/gpt-5', // use Vercel AI Gateway
36
+ prompt: 'What is an agent?',
39
37
  });
38
+ ```
39
+
40
+ ```ts
41
+ import { generateText } from 'ai';
42
+ import { openai } from '@ai-sdk/openai';
40
43
 
41
- console.log(text);
44
+ const { text } = await generateText({
45
+ model: openai('gpt-5'), // use OpenAI Responses API
46
+ prompt: 'What is an agent?',
47
+ });
42
48
  ```
43
49
 
44
- ### AI SDK UI
50
+ ### Generating Structured Data
51
+
52
+ ```ts
53
+ import { generateObject } from 'ai';
54
+ import { z } from 'zod';
55
+
56
+ const { object } = await generateObject({
57
+ model: 'openai/gpt-4.1',
58
+ schema: z.object({
59
+ recipe: z.object({
60
+ name: z.string(),
61
+ ingredients: z.array(z.object({ name: z.string(), amount: z.string() })),
62
+ steps: z.array(z.string()),
63
+ }),
64
+ }),
65
+ prompt: 'Generate a lasagna recipe.',
66
+ });
67
+ ```
68
+
69
+ ### Agents
70
+
71
+ ```ts
72
+ import { ToolLoopAgent } from 'ai';
73
+
74
+ const sandboxAgent = new ToolLoopAgent({
75
+ model: 'openai/gpt-5-codex',
76
+ system: 'You are an agent with access to a shell environment.',
77
+ tools: {
78
+ local_shell: openai.tools.localShell({
79
+ execute: async ({ action }) => {
80
+ const [cmd, ...args] = action.command;
81
+ const sandbox = await getSandbox(); // Vercel Sandbox
82
+ const command = await sandbox.runCommand({ cmd, args });
83
+ return { output: await command.stdout() };
84
+ },
85
+ }),
86
+ },
87
+ });
88
+ ```
89
+
90
+ ### UI Integration
45
91
 
46
92
  The [AI SDK UI](https://ai-sdk.dev/docs/ai-sdk-ui/overview) module provides a set of hooks that help you build chatbots and generative user interfaces. These hooks are framework agnostic, so they can be used in Next.js, React, Svelte, and Vue.
47
93
 
48
- You need to install the package for your framework:
94
+ You need to install the package for your framework, e.g.:
49
95
 
50
96
  ```shell
51
97
  npm install @ai-sdk/react
52
98
  ```
53
99
 
54
- ###### @/app/page.tsx (Next.js App Router)
100
+ #### Agent @/agent/image-generation-agent.ts
101
+
102
+ ```ts
103
+ import { openai } from '@ai-sdk/openai';
104
+ import { ToolLoopAgent, InferAgentUIMessage } from 'ai';
105
+
106
+ export const imageGenerationAgent = new ToolLoopAgent({
107
+ model: openai('gpt-5'),
108
+ tools: {
109
+ image_generation: openai.tools.imageGeneration({
110
+ partialImages: 3,
111
+ }),
112
+ },
113
+ });
114
+
115
+ export type ImageGenerationAgentMessage = InferAgentUIMessage<
116
+ typeof imageGenerationAgent
117
+ >;
118
+ ```
119
+
120
+ #### Route (Next.js App Router) @/app/api/chat/route.ts
121
+
122
+ ```tsx
123
+ import { imageGenerationAgent } from '@/agent/image-generation-agent';
124
+ import { createAgentUIStreamResponse } from 'ai';
125
+
126
+ export async function POST(req: Request) {
127
+ const { messages } = await req.json();
128
+
129
+ return createAgentUIStreamResponse({
130
+ agent: imageGenerationAgent,
131
+ messages,
132
+ });
133
+ }
134
+ ```
135
+
136
+ #### UI Component for Tool @/component/image-generation-view.tsx
137
+
138
+ ```tsx
139
+ import { openai } from '@ai-sdk/openai';
140
+ import { UIToolInvocation } from 'ai';
141
+
142
+ export default function ImageGenerationView({
143
+ invocation,
144
+ }: {
145
+ invocation: UIToolInvocation<ReturnType<typeof openai.tools.imageGeneration>>;
146
+ }) {
147
+ switch (invocation.state) {
148
+ case 'input-available':
149
+ return <div>Generating image...</div>;
150
+ case 'output-available':
151
+ return <img src={`data:image/png;base64,${invocation.output.result}`} />;
152
+ }
153
+ }
154
+ ```
155
+
156
+ #### Page @/app/page.tsx
55
157
 
56
158
  ```tsx
57
159
  'use client';
58
160
 
59
- import { useState } from 'react';
161
+ import { ImageGenerationAgentMessage } from '@/agent/image-generation-agent';
162
+ import ImageGenerationView from '@/component/image-generation-view';
60
163
  import { useChat } from '@ai-sdk/react';
61
164
 
62
165
  export default function Page() {
63
- const { messages, status, sendMessage } = useChat();
166
+ const { messages, status, sendMessage } =
167
+ useChat<ImageGenerationAgentMessage>();
168
+
64
169
  const [input, setInput] = useState('');
65
170
  const handleSubmit = e => {
66
171
  e.preventDefault();
@@ -76,9 +181,9 @@ export default function Page() {
76
181
  {message.parts.map((part, index) => {
77
182
  switch (part.type) {
78
183
  case 'text':
79
- return <span key={index}>{part.text}</span>;
80
-
81
- // other cases can handle images, tool calls, etc
184
+ return <div key={index}>{part.text}</div>;
185
+ case 'tool-image_generation':
186
+ return <ImageGenerationView key={index} invocation={part} />;
82
187
  }
83
188
  })}
84
189
  </div>
@@ -87,7 +192,6 @@ export default function Page() {
87
192
  <form onSubmit={handleSubmit}>
88
193
  <input
89
194
  value={input}
90
- placeholder="Send a message..."
91
195
  onChange={e => setInput(e.target.value)}
92
196
  disabled={status !== 'ready'}
93
197
  />
@@ -97,32 +201,13 @@ export default function Page() {
97
201
  }
98
202
  ```
99
203
 
100
- ###### @/app/api/chat/route.ts (Next.js App Router)
101
-
102
- ```ts
103
- import { streamText } from 'ai';
104
- import { openai } from '@ai-sdk/openai';
105
-
106
- export async function POST(req: Request) {
107
- const { messages } = await req.json();
108
-
109
- const result = streamText({
110
- model: openai('gpt-4o'),
111
- system: 'You are a helpful assistant.',
112
- messages,
113
- });
114
-
115
- return result.toUIMessageStreamResponse();
116
- }
117
- ```
118
-
119
204
  ## Templates
120
205
 
121
- We've built [templates](https://vercel.com/templates?type=ai) that include AI SDK integrations for different use cases, providers, and frameworks. You can use these templates to get started with your AI-powered application.
206
+ We've built [templates](https://ai-sdk.dev/docs/introduction#templates) that include AI SDK integrations for different use cases, providers, and frameworks. You can use these templates to get started with your AI-powered application.
122
207
 
123
208
  ## Community
124
209
 
125
- The AI SDK community can be found on [GitHub Discussions](https://github.com/vercel/ai/discussions) where you can ask questions, voice ideas, and share your projects with other people.
210
+ The AI SDK community can be found on [the Vercel Community](https://community.vercel.com/c/ai-sdk/62) where you can ask questions, voice ideas, and share your projects with other people.
126
211
 
127
212
  ## Contributing
128
213