Created
December 15, 2025 04:50
-
-
Save slashthinking/235b7a09d5ac3d5f3967a6c407e8397c to your computer and use it in GitHub Desktop.
使用ProviderTool和AI Gateway的例子
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dotenv/config'; | |
| import { streamText } from 'ai'; | |
| import { anthropicTools } from '@ai-sdk/anthropic/internal'; | |
| const result = await streamText({ | |
| model: 'anthropic/claude-sonnet-4.5', | |
| tools: { | |
| codeExecution: anthropicTools.codeExecution_20250522(), | |
| }, | |
| messages: [{ role: 'user', content: 'Run: print(1+1)' }], | |
| }); | |
| for await (const textPart of result.textStream) { | |
| process.stdout.write(textPart); | |
| } | |
| console.log(); | |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| import 'dotenv/config'; | |
| import { streamText } from 'ai'; | |
| import { anthropicTools } from '@ai-sdk/anthropic/internal'; | |
| async function testMemory() { | |
| const result = await streamText({ | |
| model: 'anthropic/claude-sonnet-4.5', | |
| headers: { | |
| 'anthropic-beta': 'context-management-2025-06-27', | |
| }, | |
| tools: { | |
| memory: anthropicTools.memory_20250818({}), | |
| }, | |
| messages: [ | |
| { | |
| role: 'user', | |
| content: `Please do the following: | |
| 1. Create a file called "notes.txt" with content "My notes" | |
| 2. View the file | |
| 3. Add a line "- First note" at line 2`, | |
| }, | |
| ], | |
| }); | |
| for await (const part of result.fullStream) { | |
| if (part.type === 'text-delta') { | |
| process.stdout.write(part.text); | |
| } else if (part.type === 'tool-call') { | |
| console.log('\n📝 Tool call:', part.toolName); | |
| console.log(' Input:', JSON.stringify(part.input, null, 2)); | |
| } else if (part.type === 'tool-result') { | |
| console.log(' Result:', JSON.stringify(part.output, null, 2)); | |
| } | |
| } | |
| console.log('\n'); | |
| } | |
| testMemory(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment