ai-sdk-provider-claude-code 0.2.1 → 1.0.0-beta.1
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 +68 -10
- package/dist/index.cjs +90 -48
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +21 -19
- package/dist/index.d.ts +21 -19
- package/dist/index.js +90 -48
- package/dist/index.js.map +1 -1
- package/docs/{DEVELOPMENT-STATUS.md → ai-sdk-v4/DEVELOPMENT-STATUS.md} +2 -2
- package/docs/{GUIDE.md → ai-sdk-v4/GUIDE.md} +4 -4
- package/docs/{TROUBLESHOOTING.md → ai-sdk-v4/TROUBLESHOOTING.md} +1 -1
- package/docs/ai-sdk-v5/DEVELOPMENT-STATUS.md +280 -0
- package/docs/ai-sdk-v5/GUIDE.md +912 -0
- package/docs/ai-sdk-v5/TROUBLESHOOTING.md +365 -0
- package/docs/ai-sdk-v5/V5_BREAKING_CHANGES.md +157 -0
- package/docs/ai-sdk-v5/V5_MIGRATION_PLAN.md +22 -0
- package/docs/ai-sdk-v5/V5_MIGRATION_SUMMARY.md +69 -0
- package/docs/ai-sdk-v5/V5_MIGRATION_TASKS.md +46 -0
- package/package.json +10 -9
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
# Troubleshooting Guide (AI SDK v5-beta)
|
|
2
|
+
|
|
3
|
+
This guide documents common issues and solutions for the Claude Code AI SDK Provider with v5-beta.
|
|
4
|
+
|
|
5
|
+
## Common Issues
|
|
6
|
+
|
|
7
|
+
### 1. Authentication Errors
|
|
8
|
+
|
|
9
|
+
**Problem**: Getting authentication errors when trying to use the provider.
|
|
10
|
+
|
|
11
|
+
**Solution**:
|
|
12
|
+
```bash
|
|
13
|
+
# Install Claude Code SDK globally
|
|
14
|
+
npm install -g @anthropic-ai/claude-code
|
|
15
|
+
|
|
16
|
+
# Authenticate with your Claude account
|
|
17
|
+
claude login
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
**Verification**:
|
|
21
|
+
```bash
|
|
22
|
+
# Check if authenticated
|
|
23
|
+
npx tsx ../examples/check-cli.ts
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### 2. SDK Not Found
|
|
27
|
+
|
|
28
|
+
**Problem**: Error about `@anthropic-ai/claude-code` module not found.
|
|
29
|
+
|
|
30
|
+
**Solution**: The SDK is a dependency of this provider and should be installed automatically. If not:
|
|
31
|
+
```bash
|
|
32
|
+
npm install @anthropic-ai/claude-code
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### 3. v5 Message Format Errors
|
|
36
|
+
|
|
37
|
+
**Problem**: Error about invalid message content format.
|
|
38
|
+
|
|
39
|
+
**Solution**: In v5, user messages must have content as an array of parts:
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
// ❌ Wrong (v4 format)
|
|
43
|
+
{ role: 'user', content: 'Hello' }
|
|
44
|
+
|
|
45
|
+
// ✅ Correct (v5 format)
|
|
46
|
+
{ role: 'user', content: [{ type: 'text', text: 'Hello' }] }
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### 4. Streaming API Changes
|
|
50
|
+
|
|
51
|
+
**Problem**: Streaming code from v4 doesn't work.
|
|
52
|
+
|
|
53
|
+
**Solution**: v5 uses a new streaming pattern with promises:
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
// ❌ Old v4 pattern
|
|
57
|
+
const { textStream } = await streamText({
|
|
58
|
+
model: claudeCode('sonnet'),
|
|
59
|
+
prompt: 'Hello',
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// ✅ New v5 pattern
|
|
63
|
+
const result = streamText({
|
|
64
|
+
model: claudeCode('sonnet'),
|
|
65
|
+
prompt: 'Hello',
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Access parts as promises
|
|
69
|
+
const text = await result.text;
|
|
70
|
+
const usage = await result.usage;
|
|
71
|
+
|
|
72
|
+
// Or stream chunks
|
|
73
|
+
for await (const chunk of result.textStream) {
|
|
74
|
+
process.stdout.write(chunk);
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### 5. Token Usage Property Names
|
|
79
|
+
|
|
80
|
+
**Problem**: Getting undefined for `promptTokens` or `completionTokens`.
|
|
81
|
+
|
|
82
|
+
**Solution**: Token properties have been renamed in v5:
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
// ❌ Old v4 names
|
|
86
|
+
console.log(usage.promptTokens);
|
|
87
|
+
console.log(usage.completionTokens);
|
|
88
|
+
|
|
89
|
+
// ✅ New v5 names
|
|
90
|
+
console.log(usage.inputTokens);
|
|
91
|
+
console.log(usage.outputTokens);
|
|
92
|
+
console.log(usage.totalTokens); // New in v5
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### 6. Handling Long-Running Tasks
|
|
96
|
+
|
|
97
|
+
**Problem**: Complex queries with Claude Opus 4 may take longer due to extended thinking mode.
|
|
98
|
+
|
|
99
|
+
**Solution**: Use AbortSignal with custom timeouts:
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
import { generateText } from 'ai';
|
|
103
|
+
import { claudeCode } from 'ai-sdk-provider-claude-code';
|
|
104
|
+
|
|
105
|
+
// Create a custom timeout (5 minutes for complex tasks)
|
|
106
|
+
const controller = new AbortController();
|
|
107
|
+
const timeoutId = setTimeout(() => {
|
|
108
|
+
controller.abort(new Error('Request timeout'));
|
|
109
|
+
}, 300000);
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
const result = await generateText({
|
|
113
|
+
model: claudeCode('opus'),
|
|
114
|
+
prompt: 'Complex reasoning task...',
|
|
115
|
+
abortSignal: controller.signal,
|
|
116
|
+
});
|
|
117
|
+
clearTimeout(timeoutId);
|
|
118
|
+
console.log(result.text);
|
|
119
|
+
} catch (error) {
|
|
120
|
+
if (error.name === 'AbortError') {
|
|
121
|
+
console.log('Request timed out');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
**Guidelines**:
|
|
127
|
+
- Simple queries: Default timeout is sufficient
|
|
128
|
+
- Complex reasoning: 5-10 minutes may be needed
|
|
129
|
+
- Very long tasks: Consider breaking into smaller chunks
|
|
130
|
+
|
|
131
|
+
### 7. Object Generation Issues
|
|
132
|
+
|
|
133
|
+
**Problem**: Claude returns text instead of valid JSON when using `generateObject`.
|
|
134
|
+
|
|
135
|
+
**Solutions**:
|
|
136
|
+
1. **Simplify your schema** - Start with fewer fields
|
|
137
|
+
2. **Clear prompts** - Be explicit: "Generate a user profile with name and age"
|
|
138
|
+
3. **Use descriptions** - Add `.describe()` to schema fields
|
|
139
|
+
4. **Retry logic** - Implement retries for production:
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
async function generateWithRetry(schema, prompt, maxRetries = 3) {
|
|
143
|
+
for (let i = 0; i < maxRetries; i++) {
|
|
144
|
+
try {
|
|
145
|
+
return await generateObject({
|
|
146
|
+
model: claudeCode('sonnet'),
|
|
147
|
+
schema,
|
|
148
|
+
prompt
|
|
149
|
+
});
|
|
150
|
+
} catch (error) {
|
|
151
|
+
if (i === maxRetries - 1) throw error;
|
|
152
|
+
await new Promise(r => setTimeout(r, 1000 * Math.pow(2, i)));
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### 8. Session Management
|
|
159
|
+
|
|
160
|
+
**Problem**: Conversations don't maintain context.
|
|
161
|
+
|
|
162
|
+
**Solution**: Use message history with v5 format:
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
import { ModelMessage } from 'ai';
|
|
166
|
+
|
|
167
|
+
const messages: ModelMessage[] = [
|
|
168
|
+
{ role: 'user', content: [{ type: 'text', text: 'My name is Alice' }] },
|
|
169
|
+
{ role: 'assistant', content: [{ type: 'text', text: 'Nice to meet you, Alice!' }] },
|
|
170
|
+
{ role: 'user', content: [{ type: 'text', text: 'What is my name?' }] }
|
|
171
|
+
];
|
|
172
|
+
|
|
173
|
+
const result = await generateText({
|
|
174
|
+
model: claudeCode('sonnet'),
|
|
175
|
+
messages, // Pass full conversation history
|
|
176
|
+
});
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
**Note**: While the provider returns session IDs in metadata, the recommended approach is to use message history for conversation continuity.
|
|
180
|
+
|
|
181
|
+
### 9. Error Handling
|
|
182
|
+
|
|
183
|
+
**Problem**: Need to handle different types of errors appropriately.
|
|
184
|
+
|
|
185
|
+
**Solution**: Use the provider's error utilities:
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
import { isAuthenticationError, getErrorMetadata } from 'ai-sdk-provider-claude-code';
|
|
189
|
+
|
|
190
|
+
try {
|
|
191
|
+
const result = await generateText({
|
|
192
|
+
model: claudeCode('sonnet'),
|
|
193
|
+
prompt: 'Hello',
|
|
194
|
+
});
|
|
195
|
+
} catch (error) {
|
|
196
|
+
if (isAuthenticationError(error)) {
|
|
197
|
+
console.error('Please run: claude login');
|
|
198
|
+
} else if (error.name === 'AbortError') {
|
|
199
|
+
console.error('Request was cancelled or timed out');
|
|
200
|
+
} else {
|
|
201
|
+
// Get additional error details
|
|
202
|
+
const metadata = getErrorMetadata(error);
|
|
203
|
+
console.error('Error details:', metadata);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## v5-Specific Migration Issues
|
|
209
|
+
|
|
210
|
+
### 1. Result Structure Changes
|
|
211
|
+
|
|
212
|
+
**Problem**: Code expecting v4 result structure fails.
|
|
213
|
+
|
|
214
|
+
**Solution**: Update to v5 result structure:
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
// ❌ v4 pattern
|
|
218
|
+
const { text, usage } = await generateText(...);
|
|
219
|
+
|
|
220
|
+
// ✅ v5 pattern
|
|
221
|
+
const result = await generateText(...);
|
|
222
|
+
console.log(result.text);
|
|
223
|
+
console.log(result.usage);
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### 2. Stream Event Changes
|
|
227
|
+
|
|
228
|
+
**Problem**: Stream events have different names.
|
|
229
|
+
|
|
230
|
+
**Solution**: v5 includes a `stream-start` event:
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
const result = streamText({
|
|
234
|
+
model: claudeCode('sonnet'),
|
|
235
|
+
prompt: 'Hello',
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
// v5 stream includes: stream-start, text-delta, finish
|
|
239
|
+
for await (const chunk of result.fullStream) {
|
|
240
|
+
switch (chunk.type) {
|
|
241
|
+
case 'stream-start':
|
|
242
|
+
console.log('Stream started');
|
|
243
|
+
break;
|
|
244
|
+
case 'text-delta':
|
|
245
|
+
process.stdout.write(chunk.textDelta);
|
|
246
|
+
break;
|
|
247
|
+
case 'finish':
|
|
248
|
+
console.log('Stream finished');
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### 3. Unsupported Settings
|
|
255
|
+
|
|
256
|
+
**Problem**: Getting warnings about unsupported settings.
|
|
257
|
+
|
|
258
|
+
**Solution**: Some v4 settings are renamed or removed in v5:
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
// ❌ v4 setting names
|
|
262
|
+
{ maxTokens: 1000 }
|
|
263
|
+
|
|
264
|
+
// ✅ v5 setting names
|
|
265
|
+
{ maxOutputTokens: 1000 }
|
|
266
|
+
|
|
267
|
+
// Note: Many settings like temperature, topP, etc. are still not supported by Claude Code SDK
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## Debugging Tips
|
|
271
|
+
|
|
272
|
+
### 1. Verify Setup
|
|
273
|
+
```bash
|
|
274
|
+
# Check Claude CLI version
|
|
275
|
+
claude --version
|
|
276
|
+
|
|
277
|
+
# Test authentication
|
|
278
|
+
npx tsx ../examples/check-cli.ts
|
|
279
|
+
|
|
280
|
+
# Run integration tests
|
|
281
|
+
npx tsx ../examples/integration-test.ts
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
### 2. Enable Debug Logging
|
|
285
|
+
```typescript
|
|
286
|
+
// Log provider metadata to debug issues
|
|
287
|
+
const result = await generateText({
|
|
288
|
+
model: claudeCode('sonnet'),
|
|
289
|
+
prompt: 'Test',
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
console.log('Metadata:', result.providerMetadata);
|
|
293
|
+
// Shows: sessionId, costUsd, durationMs, rawUsage
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
### 3. Test Specific Features
|
|
297
|
+
```bash
|
|
298
|
+
# Test streaming
|
|
299
|
+
npx tsx ../examples/streaming.ts
|
|
300
|
+
|
|
301
|
+
# Test object generation
|
|
302
|
+
npx tsx ../examples/generate-object-basic.ts
|
|
303
|
+
|
|
304
|
+
# Test conversations
|
|
305
|
+
npx tsx ../examples/conversation-history.ts
|
|
306
|
+
|
|
307
|
+
# Test long-running tasks
|
|
308
|
+
npx tsx ../examples/long-running-tasks.ts
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
## Platform-Specific Issues
|
|
312
|
+
|
|
313
|
+
### Windows
|
|
314
|
+
- Ensure Claude CLI is in your PATH
|
|
315
|
+
- Use PowerShell or Command Prompt (not WSL) for installation
|
|
316
|
+
|
|
317
|
+
### macOS
|
|
318
|
+
- May need to allow CLI in Security & Privacy settings
|
|
319
|
+
- Use Homebrew or direct npm installation
|
|
320
|
+
|
|
321
|
+
### Linux
|
|
322
|
+
- Ensure Node.js ≥ 18 is installed
|
|
323
|
+
- May need to use `sudo` for global npm installs
|
|
324
|
+
|
|
325
|
+
## Performance Tips
|
|
326
|
+
|
|
327
|
+
1. **Model Selection**
|
|
328
|
+
- Use `sonnet` for faster responses
|
|
329
|
+
- Use `opus` for complex reasoning tasks
|
|
330
|
+
|
|
331
|
+
2. **Request Optimization**
|
|
332
|
+
- Keep prompts concise and clear
|
|
333
|
+
- Use streaming for better UX
|
|
334
|
+
- Implement proper error handling
|
|
335
|
+
|
|
336
|
+
3. **Resource Management**
|
|
337
|
+
- The provider manages concurrent requests automatically
|
|
338
|
+
- Use AbortSignal for cancellable requests
|
|
339
|
+
- Clean up timeouts in finally blocks
|
|
340
|
+
|
|
341
|
+
## Known Limitations
|
|
342
|
+
|
|
343
|
+
1. **No Image Support**: The Claude Code SDK doesn't support image inputs
|
|
344
|
+
2. **No AI SDK Tool Calling**: The AI SDK's function calling interface isn't implemented, but Claude can use tools via MCP servers and built-in CLI tools
|
|
345
|
+
3. **Object Generation**: Relies on prompt engineering rather than native JSON mode
|
|
346
|
+
4. **Model Options**: Limited to 'opus' and 'sonnet' models
|
|
347
|
+
|
|
348
|
+
## Getting Help
|
|
349
|
+
|
|
350
|
+
1. **Check Examples**: Review the `../../examples` directory for working code
|
|
351
|
+
2. **Integration Test**: Run `npx tsx ../../examples/integration-test.ts` to verify setup
|
|
352
|
+
3. **GitHub Issues**: Report bugs at https://github.com/ben-vargas/ai-sdk-provider-claude-code/issues
|
|
353
|
+
4. **Documentation**: See the README.md and GUIDE.md for detailed API documentation
|
|
354
|
+
|
|
355
|
+
## Common Error Messages
|
|
356
|
+
|
|
357
|
+
| Error | Cause | Solution |
|
|
358
|
+
|-------|-------|----------|
|
|
359
|
+
| "Claude Code executable not found" | CLI not installed | Run `npm install -g @anthropic-ai/claude-code` |
|
|
360
|
+
| "Authentication required" | Not logged in | Run `claude login` |
|
|
361
|
+
| "No object generated" | Invalid JSON response | Simplify schema, improve prompt |
|
|
362
|
+
| "Request timeout" | Task took too long | Increase timeout with AbortSignal |
|
|
363
|
+
| "Session not found" | Invalid session ID | Use message history instead |
|
|
364
|
+
| "Invalid message content" | v4 message format | Update to v5 array format |
|
|
365
|
+
| "promptTokens is undefined" | v4 property names | Use inputTokens/outputTokens |
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# Breaking Changes: V4 to V5-Beta Migration
|
|
2
|
+
|
|
3
|
+
This document outlines the breaking changes users will encounter when upgrading from v4 to v5-beta of the ai-sdk-provider-claude-code.
|
|
4
|
+
|
|
5
|
+
⚠️ **IMPORTANT**: This is a complete breaking change. The v5-beta version of this provider ONLY works with AI SDK v5-beta. You cannot use:
|
|
6
|
+
- v5-beta provider with AI SDK v4 ❌
|
|
7
|
+
- v4 provider with AI SDK v5-beta ❌
|
|
8
|
+
|
|
9
|
+
You must upgrade both together.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Update your dependencies to use the beta versions:
|
|
14
|
+
|
|
15
|
+
```json
|
|
16
|
+
{
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"ai": "beta",
|
|
19
|
+
"ai-sdk-provider-claude-code": "latest"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## API Changes
|
|
25
|
+
|
|
26
|
+
### 1. Token Usage Properties
|
|
27
|
+
|
|
28
|
+
The token usage properties have been renamed:
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
// V4
|
|
32
|
+
const { usage } = await generateText(...);
|
|
33
|
+
console.log(usage.promptTokens); // ❌ Old
|
|
34
|
+
console.log(usage.completionTokens); // ❌ Old
|
|
35
|
+
|
|
36
|
+
// V5
|
|
37
|
+
const { usage } = await generateText(...);
|
|
38
|
+
console.log(usage.inputTokens); // ✅ New
|
|
39
|
+
console.log(usage.outputTokens); // ✅ New
|
|
40
|
+
console.log(usage.totalTokens); // ✅ New
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### 2. Streaming Response Pattern
|
|
44
|
+
|
|
45
|
+
The streamText function now returns a result object with promises:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
// V4
|
|
49
|
+
const { text, usage } = await streamText({
|
|
50
|
+
model: claudeCode('opus'),
|
|
51
|
+
prompt: 'Hello'
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// V5
|
|
55
|
+
const result = streamText({
|
|
56
|
+
model: claudeCode('opus'),
|
|
57
|
+
prompt: 'Hello'
|
|
58
|
+
});
|
|
59
|
+
const text = await result.text;
|
|
60
|
+
const usage = await result.usage;
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 3. Message Types
|
|
64
|
+
|
|
65
|
+
In v5, messages sent to the model should use `ModelMessage` type:
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
// V4
|
|
69
|
+
import type { Message } from 'ai';
|
|
70
|
+
const messages: Message[] = [];
|
|
71
|
+
|
|
72
|
+
// V5
|
|
73
|
+
import type { ModelMessage } from 'ai';
|
|
74
|
+
const messages: ModelMessage[] = [];
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
**Note**: `UIMessage` is for UI state management, while `ModelMessage` is for sending to language models.
|
|
78
|
+
|
|
79
|
+
### 4. Parameter Names
|
|
80
|
+
|
|
81
|
+
Some parameter names have changed:
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
// V4
|
|
85
|
+
generateText({
|
|
86
|
+
model: claudeCode('opus'),
|
|
87
|
+
prompt: 'Hello',
|
|
88
|
+
maxTokens: 100 // ❌ Old
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// V5
|
|
92
|
+
generateText({
|
|
93
|
+
model: claudeCode('opus'),
|
|
94
|
+
prompt: 'Hello',
|
|
95
|
+
maxOutputTokens: 100 // ✅ New
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### 5. Stream Events
|
|
100
|
+
|
|
101
|
+
V5 streams now emit additional events:
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
// V5 stream includes these events:
|
|
105
|
+
// - stream-start (with warnings)
|
|
106
|
+
// - response-metadata
|
|
107
|
+
// - text-delta (with 'delta' property, not 'textDelta')
|
|
108
|
+
// - finish
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Code Examples
|
|
112
|
+
|
|
113
|
+
### Basic Text Generation
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
// V5 Pattern
|
|
117
|
+
import { streamText } from 'ai';
|
|
118
|
+
import { claudeCode } from 'ai-sdk-provider-claude-code';
|
|
119
|
+
|
|
120
|
+
const result = streamText({
|
|
121
|
+
model: claudeCode('opus'),
|
|
122
|
+
prompt: 'Write a haiku'
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// Await the results
|
|
126
|
+
const text = await result.text;
|
|
127
|
+
const usage = await result.usage;
|
|
128
|
+
const metadata = await result.providerMetadata;
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Conversation with History
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
// V5 Pattern
|
|
135
|
+
import { streamText } from 'ai';
|
|
136
|
+
import { claudeCode } from 'ai-sdk-provider-claude-code';
|
|
137
|
+
import type { CoreMessage } from 'ai';
|
|
138
|
+
|
|
139
|
+
const messages: CoreMessage[] = [
|
|
140
|
+
{ role: 'user', content: 'Hello!' },
|
|
141
|
+
{ role: 'assistant', content: 'Hi there!' },
|
|
142
|
+
{ role: 'user', content: 'How are you?' }
|
|
143
|
+
];
|
|
144
|
+
|
|
145
|
+
const result = streamText({
|
|
146
|
+
model: claudeCode('opus'),
|
|
147
|
+
messages
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const response = await result.text;
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Notes
|
|
154
|
+
|
|
155
|
+
- All Claude Code SDK-specific limitations still apply (no temperature control, no output length limits, etc.)
|
|
156
|
+
- TypeScript users will get compile-time errors for most breaking changes
|
|
157
|
+
- This is a complete rewrite for v5-beta compatibility - no v4 compatibility is maintained
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# V5-Beta Migration Plan
|
|
2
|
+
|
|
3
|
+
The goal of this migration is to update the `ai-sdk-provider-claude-code` to be compatible with the Vercel AI SDK v5-beta. This document outlines the plan that was followed to achieve this.
|
|
4
|
+
|
|
5
|
+
## Key Areas of Change
|
|
6
|
+
|
|
7
|
+
The migration focused on the following key areas:
|
|
8
|
+
|
|
9
|
+
1. **Dependency Upgrades:** The first step was to update all relevant `@ai-sdk` and `ai` packages to their `@beta` versions. This brought in the new `LanguageModelV2` architecture and other breaking changes.
|
|
10
|
+
|
|
11
|
+
2. **`LanguageModelV2` Adoption:** The core of the migration was to rewrite the `ClaudeCodeLanguageModel` to conform to the new `LanguageModelV2` interface. This involved the following:
|
|
12
|
+
* Changing the `doStream` and `doGenerate` methods to handle the new `UIMessage` and `ModelMessage` formats.
|
|
13
|
+
* Adopting the "content-first" design, where all outputs are treated as ordered content parts.
|
|
14
|
+
* Ensuring type safety with the new `LanguageModelV2` types.
|
|
15
|
+
|
|
16
|
+
3. **Message Overhaul:** The provider was updated to handle the new `UIMessage` and `ModelMessage` types. This meant that the `convertToClaudeCodeMessages` function was updated to accept `ModelMessage`s and convert them to the format that the Claude API expects.
|
|
17
|
+
|
|
18
|
+
4. **Server-Sent Events (SSE):** The provider was updated to work with the new SSE-based streaming protocol. This required changes to how the streaming responses are handled and formatted.
|
|
19
|
+
|
|
20
|
+
5. **Tool Handling:** The provider was updated to handle the new type-safe tool calls. This involved changes to how tools are defined and how tool calls are processed.
|
|
21
|
+
|
|
22
|
+
6. **Examples and Tests:** All examples and tests were updated to use the new v5-beta APIs. This was a good way to validate the migration and ensure that everything was working as expected.
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# V5-Beta Migration Summary
|
|
2
|
+
|
|
3
|
+
This document summarizes the migration of the `ai-sdk-provider-claude-code` to be compatible with the Vercel AI SDK v5-beta.
|
|
4
|
+
|
|
5
|
+
## Current Status
|
|
6
|
+
|
|
7
|
+
The migration is complete. The codebase is now compatible with the Vercel AI SDK v5-beta. All tests are passing, TypeScript compilation is successful, and the examples have been updated to use the new APIs.
|
|
8
|
+
|
|
9
|
+
## Modified Files
|
|
10
|
+
|
|
11
|
+
The following files were modified during the migration:
|
|
12
|
+
|
|
13
|
+
* `package.json`: Updated to use the `@beta` versions of the `ai`, `@ai-sdk/provider`, and `@ai-sdk/provider-utils` packages.
|
|
14
|
+
* `src/claude-code-language-model.ts`: Updated to implement the `LanguageModelV2` interface, and the `doGenerate` and `doStream` methods were updated to handle the new message format.
|
|
15
|
+
* `src/convert-to-claude-code-messages.ts`: Updated to handle the new `ModelMessage` format and added v4 compatibility.
|
|
16
|
+
* `src/map-claude-code-finish-reason.ts`: Updated to use the `LanguageModelV2FinishReason` type.
|
|
17
|
+
* `src/claude-code-provider.ts`: Updated to use the `ProviderV2` and `LanguageModelV2` types.
|
|
18
|
+
* `examples/basic-usage.ts`: Updated to use the new `streamText` API with proper async handling.
|
|
19
|
+
* `examples/conversation-history.ts`: Updated to use the new `streamText` API and `CoreMessage` type.
|
|
20
|
+
* `examples/tool-management.ts`: Updated to use the new `streamText` API with promises.
|
|
21
|
+
* `examples/limitations.ts`: Updated parameter names (`maxOutputTokens` instead of `maxTokens`).
|
|
22
|
+
* `src/claude-code-language-model.test.ts`: Updated to use the new v5-beta APIs and correct assertions.
|
|
23
|
+
* `src/convert-to-claude-code-messages.test.ts`: Updated to use type-only import for `CoreMessage`.
|
|
24
|
+
|
|
25
|
+
## Key Migration Challenges and Solutions
|
|
26
|
+
|
|
27
|
+
### 1. Token Usage Property Names
|
|
28
|
+
**Issue**: V4 used `promptTokens`/`completionTokens` while V5 uses `inputTokens`/`outputTokens`/`totalTokens`.
|
|
29
|
+
**Solution**: Updated all token usage calculations and test assertions to use the new property names.
|
|
30
|
+
|
|
31
|
+
### 2. Message Content Format
|
|
32
|
+
**Issue**: V5 requires user messages to have content as an array of parts, not a string.
|
|
33
|
+
**Solution**: Updated message handling to check for array content and extract text from parts.
|
|
34
|
+
|
|
35
|
+
### 3. Stream Response Format
|
|
36
|
+
**Issue**: V5 streams use different event types and structure.
|
|
37
|
+
**Solutions**:
|
|
38
|
+
- Added `stream-start` event emission with warnings
|
|
39
|
+
- Changed `textDelta` to `delta` in stream parts
|
|
40
|
+
- Removed `warnings` from doStream return type (now included in stream-start)
|
|
41
|
+
|
|
42
|
+
### 4. Example Code Patterns
|
|
43
|
+
**Issue**: Examples were using destructuring pattern that doesn't work with v5's promise-based API.
|
|
44
|
+
**Solution**: Updated all examples to call streamText first, then await individual properties.
|
|
45
|
+
|
|
46
|
+
### 5. TypeScript Strict Mode Issues
|
|
47
|
+
**Issues**:
|
|
48
|
+
- Lexical declarations in switch cases
|
|
49
|
+
- Type-only imports required for `CoreMessage`
|
|
50
|
+
- Invalid properties in API calls
|
|
51
|
+
**Solutions**:
|
|
52
|
+
- Added block scope to switch cases
|
|
53
|
+
- Changed to type-only imports where needed
|
|
54
|
+
- Removed mode parameter from low-level API calls
|
|
55
|
+
|
|
56
|
+
### 6. Tool Result Format
|
|
57
|
+
**Issue**: Tests were using v4 format with `result` property, but v5 uses `output`.
|
|
58
|
+
**Solution**: Added handling for test data that was still using v4 format. In production, only v5 format messages will be received.
|
|
59
|
+
|
|
60
|
+
## Rationale for Changes
|
|
61
|
+
|
|
62
|
+
The changes were made to align the provider with the new `LanguageModelV2` architecture and the other breaking changes introduced in the Vercel AI SDK v5-beta. This is a complete breaking change - the updated provider ONLY works with AI SDK v5-beta and is NOT compatible with v4.
|
|
63
|
+
|
|
64
|
+
## Testing and Validation
|
|
65
|
+
|
|
66
|
+
- All 230 tests are passing
|
|
67
|
+
- TypeScript compilation is successful with no errors
|
|
68
|
+
- ESLint passes with only minor warnings about `any` types in tests
|
|
69
|
+
- Examples have been updated and are syntactically correct
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# V5-Beta Migration Tasks
|
|
2
|
+
|
|
3
|
+
This document outlines the sequential tasks that were executed to migrate the `ai-sdk-provider-claude-code` to be compatible with the Vercel AI SDK v5-beta.
|
|
4
|
+
|
|
5
|
+
## Phase 1: Setup and Dependency Updates
|
|
6
|
+
|
|
7
|
+
1. **Update `package.json`:**
|
|
8
|
+
* Update `ai` to `@beta`.
|
|
9
|
+
* Update `@ai-sdk/provider` to `@beta`.
|
|
10
|
+
* Update `@ai-sdk/provider-utils` to `@beta`.
|
|
11
|
+
* Run `npm install` to install the new dependencies.
|
|
12
|
+
|
|
13
|
+
## Phase 2: Core Provider Migration
|
|
14
|
+
|
|
15
|
+
1. **Update `ClaudeCodeLanguageModel`:**
|
|
16
|
+
* Modify the class to implement the `LanguageModelV2` interface.
|
|
17
|
+
* Update the `doStream` and `doGenerate` methods to handle the new message formats and streaming protocol.
|
|
18
|
+
|
|
19
|
+
2. **Update `convertToClaudeCodeMessages`:**
|
|
20
|
+
* Modify the function to accept `ModelMessage`s as input.
|
|
21
|
+
* Update the conversion logic to correctly transform `ModelMessage`s into the Claude API format.
|
|
22
|
+
|
|
23
|
+
3. **Update `mapClaudeCodeFinishReason`:**
|
|
24
|
+
* Review and update the finish reason mapping to align with any changes in the v5-beta API.
|
|
25
|
+
|
|
26
|
+
## Phase 3: Tooling and Error Handling
|
|
27
|
+
|
|
28
|
+
1. **Update Tool Handling:**
|
|
29
|
+
* Review the new type-safe tool call mechanism.
|
|
30
|
+
* Update any tool-related logic in the provider to use the new APIs.
|
|
31
|
+
|
|
32
|
+
2. **Update Error Handling:**
|
|
33
|
+
* Review the error handling in the provider and update it to align with any changes in the v5-beta API.
|
|
34
|
+
|
|
35
|
+
## Phase 4: Validation and Testing
|
|
36
|
+
|
|
37
|
+
1. **Update Examples:**
|
|
38
|
+
* Go through each example in the `examples` directory and update it to use the new v5-beta APIs.
|
|
39
|
+
* This involved updating how the `useChat` hook is used, how messages are handled, and how the provider is instantiated.
|
|
40
|
+
|
|
41
|
+
2. **Update Tests:**
|
|
42
|
+
* Go through each test file in the `src` directory and update it to use the new v5-beta APIs.
|
|
43
|
+
* This involved updating how the language model is mocked and how the test assertions are written.
|
|
44
|
+
|
|
45
|
+
3. **Run all tests and examples:**
|
|
46
|
+
* Execute all tests and examples to ensure that the migration was successful and that there are no regressions.
|