-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat: xai server-side tool calling #9896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+5,868
−5
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
90f124d
feat: xai server-side tool calling
dancer 40819d0
prettier
dancer 836fb21
changesets + tests(pending)
dancer 827bcfa
fixtures + tests, e2e frontend
dancer 9a87794
fixes
dancer 8764cf9
prettier
dancer ecd94e1
prettier
dancer cd6a81d
documentation + vid / image search examples
dancer 560906c
prettier
dancer 26836cf
spec update: toll result `.result` is unknown, but not nullable
gr2m bda7a7d
Merge branch 'main' into josh/xx
dancer 08d70e0
provider exec
dancer 158cb12
multi turn
dancer d3596ec
fixtures
dancer d795aca
prettier
dancer beeff40
fix warnings on view x video and x images
dancer f29f9c7
tests
dancer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@ai-sdk/xai': patch | ||
| --- | ||
|
|
||
| feat: xai server-side tool calling |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { xai } from '@ai-sdk/xai'; | ||
| import { ToolLoopAgent } from 'ai'; | ||
| import { run } from '../lib/run'; | ||
|
|
||
| const agent = new ToolLoopAgent({ | ||
| model: xai.responses('grok-4-fast'), | ||
| instructions: 'you are a helpful research assistant', | ||
| tools: { | ||
| web_search: xai.tools.webSearch(), | ||
| x_search: xai.tools.xSearch(), | ||
| code_execution: xai.tools.codeExecution(), | ||
| }, | ||
| }); | ||
|
|
||
| run(async () => { | ||
| const result = await agent.stream({ | ||
| prompt: 'research prompt caching in llms and explain how it reduces costs', | ||
| }); | ||
|
|
||
| for await (const textPart of result.textStream) { | ||
| process.stdout.write(textPart); | ||
| } | ||
| }); |
39 changes: 39 additions & 0 deletions
39
examples/ai-core/src/generate-text/xai-responses-web-search.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { xai } from '@ai-sdk/xai'; | ||
| import { generateText } from 'ai'; | ||
| import 'dotenv/config'; | ||
|
|
||
| async function main() { | ||
| const result = await generateText({ | ||
| model: xai.responses('grok-4-fast'), | ||
| tools: { | ||
| web_search: xai.tools.webSearch(), | ||
| }, | ||
| prompt: | ||
| 'What are the latest developments in AI from the past week? Search and summarize.', | ||
| }); | ||
|
|
||
| console.log('Text:', result.text); | ||
| console.log(); | ||
| console.log('Tool calls made:'); | ||
| for (const content of result.content) { | ||
| if (content.type === 'tool-call') { | ||
| console.log( | ||
| ` - ${content.toolName} (${content.providerExecuted ? 'server-side' : 'client-side'})`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| console.log(); | ||
| console.log('Sources cited:'); | ||
| for (const content of result.content) { | ||
| if (content.type === 'source' && content.sourceType === 'url') { | ||
| console.log(` - ${content.url}`); | ||
| } | ||
| } | ||
|
|
||
| console.log(); | ||
| console.log('Finish reason:', result.finishReason); | ||
| console.log('Usage:', result.usage); | ||
| } | ||
|
|
||
| main().catch(console.error); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { xai } from '@ai-sdk/xai'; | ||
| import { streamText } from 'ai'; | ||
| import 'dotenv/config'; | ||
|
|
||
| async function main() { | ||
| const { fullStream } = streamText({ | ||
| model: xai.responses('grok-4-fast'), | ||
| tools: { | ||
| web_search: xai.tools.webSearch(), | ||
| x_search: xai.tools.xSearch(), | ||
| code_execution: xai.tools.codeExecution(), | ||
| }, | ||
| prompt: 'Can you research about Vercel AI Gateway?', | ||
| }); | ||
|
|
||
| let toolCallCount = 0; | ||
|
|
||
| for await (const event of fullStream) { | ||
| if (event.type === 'tool-call') { | ||
| toolCallCount++; | ||
| console.log( | ||
| `\n[Tool Call ${toolCallCount}] ${event.toolName}${event.providerExecuted ? ' (server-side)' : ' (client)'}`, | ||
| ); | ||
| } else if (event.type === 'text-delta') { | ||
| process.stdout.write(event.text); | ||
| } else if (event.type === 'source' && event.sourceType === 'url') { | ||
| console.log(`\n[Citation] ${event.url}`); | ||
| } | ||
| } | ||
|
|
||
| console.log('\n'); | ||
| } | ||
|
|
||
| main().catch(console.error); |
43 changes: 43 additions & 0 deletions
43
examples/ai-core/src/stream-text/xai-web-search-image-understanding.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { xai } from '@ai-sdk/xai'; | ||
| import { streamText } from 'ai'; | ||
| import 'dotenv/config'; | ||
|
|
||
| async function main() { | ||
| const { fullStream } = streamText({ | ||
| model: xai.responses('grok-4-fast'), | ||
| tools: { | ||
| web_search: xai.tools.webSearch({ | ||
| allowedDomains: ['x.ai'], | ||
| enableImageUnderstanding: true, | ||
| }), | ||
| }, | ||
| prompt: | ||
| 'search x.ai website and describe any images you find on the homepage', | ||
| }); | ||
|
|
||
| console.log('searching x.ai with image understanding...\n'); | ||
|
|
||
| for await (const part of fullStream) { | ||
| switch (part.type) { | ||
| case 'tool-call': | ||
| if (part.providerExecuted) { | ||
| console.log(`[tool: ${part.toolName}]`); | ||
| } | ||
| break; | ||
|
|
||
| case 'text-delta': | ||
| process.stdout.write(part.text); | ||
| break; | ||
|
|
||
| case 'source': | ||
| if (part.sourceType === 'url') { | ||
| console.log(`\n[source: ${part.url}]`); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| console.log('\n'); | ||
| } | ||
|
|
||
| main().catch(console.error); |
44 changes: 44 additions & 0 deletions
44
examples/ai-core/src/stream-text/xai-x-search-video-understanding.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { xai } from '@ai-sdk/xai'; | ||
| import { streamText } from 'ai'; | ||
| import 'dotenv/config'; | ||
|
|
||
| async function main() { | ||
| const { fullStream } = streamText({ | ||
| model: xai.responses('grok-4-fast'), | ||
| tools: { | ||
| x_search: xai.tools.xSearch({ | ||
| allowedXHandles: ['xai', 'elonmusk'], | ||
| enableImageUnderstanding: true, | ||
| enableVideoUnderstanding: true, | ||
| }), | ||
| }, | ||
| prompt: | ||
| 'what are the latest videos and images from xai showing their products or announcements', | ||
| }); | ||
|
|
||
| console.log('searching x for videos and images from xai...\n'); | ||
|
|
||
| for await (const part of fullStream) { | ||
| switch (part.type) { | ||
| case 'tool-call': | ||
| if (part.providerExecuted) { | ||
| console.log(`[tool: ${part.toolName}]`); | ||
| } | ||
| break; | ||
|
|
||
| case 'text-delta': | ||
| process.stdout.write(part.text); | ||
| break; | ||
|
|
||
| case 'source': | ||
| if (part.sourceType === 'url') { | ||
| console.log(`\n[source: ${part.url}]`); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| console.log('\n'); | ||
| } | ||
|
|
||
| main().catch(console.error); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { xai } from '@ai-sdk/xai'; | ||
| import { ToolLoopAgent, InferAgentUIMessage } from 'ai'; | ||
|
|
||
| export const xaiWebSearchAgent = new ToolLoopAgent({ | ||
| model: xai.responses('grok-4-fast'), | ||
| tools: { | ||
| web_search: xai.tools.webSearch({ | ||
| enableImageUnderstanding: true, | ||
| }), | ||
| x_search: xai.tools.xSearch({ | ||
| enableImageUnderstanding: true, | ||
| }), | ||
| }, | ||
| onStepFinish: ({ request }) => { | ||
| console.dir(request.body, { depth: Infinity }); | ||
| }, | ||
| }); | ||
|
|
||
| export type XaiWebSearchMessage = InferAgentUIMessage<typeof xaiWebSearchAgent>; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.