-
-
Notifications
You must be signed in to change notification settings - Fork 186
feat(ai): add type-safe tool call events to chat() stream #452
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
b8c1615
a2c2f9c
5a37cb8
5e3ebd7
9a1df7e
d46cd6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,41 @@ title: StreamChunk | |
| type StreamChunk = AGUIEvent; | ||
| ``` | ||
|
|
||
| Defined in: [types.ts:976](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/types.ts#L976) | ||
| Defined in: [types.ts:989](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/types.ts#L989) | ||
|
|
||
| Chunk returned by the SDK during streaming chat completions. | ||
| Uses the AG-UI protocol event format. | ||
|
|
||
| # Type Alias: TypedStreamChunk | ||
|
|
||
| ```ts | ||
| type TypedStreamChunk<TTools extends ReadonlyArray<Tool<any, any, any>> = ReadonlyArray<Tool<any, any, any>>> | ||
| ``` | ||
|
|
||
| Defined in: [types.ts:1033](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/types.ts#L1033) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the This 🤖 Prompt for AI Agents |
||
|
|
||
| A variant of `StreamChunk` parameterized by the tools array. When specific tool types are provided (e.g. from `chat({ tools: [myTool] })`): | ||
|
|
||
| - `TOOL_CALL_START` and `TOOL_CALL_END` events have `toolName` narrowed to the union of known tool name literals. | ||
| - `TOOL_CALL_END` events have `input` typed as the union of tool input types. | ||
|
|
||
| When tools are untyped or absent, `TypedStreamChunk` degrades to the same type as `StreamChunk`. | ||
|
|
||
| This is the type returned by `chat()` when streaming is enabled (the default). You don't typically need to reference it directly unless annotating function parameters or return types. | ||
|
|
||
| ```ts | ||
| import type { TypedStreamChunk } from "@tanstack/ai"; | ||
| import { toolDefinition } from "@tanstack/ai"; | ||
|
|
||
| // Given tools created with toolDefinition(): | ||
| const weatherTool = toolDefinition({ name: "get_weather", description: "...", inputSchema: /* Zod schema */ }); | ||
| const searchTool = toolDefinition({ name: "search", description: "...", inputSchema: /* Zod schema */ }); | ||
|
|
||
| // Without type args — equivalent to StreamChunk | ||
| type Chunk = TypedStreamChunk; | ||
|
|
||
| // With specific tools — tool call events are typed | ||
| type TypedChunk = TypedStreamChunk<[typeof weatherTool, typeof searchTool]>; | ||
| ``` | ||
|
|
||
| See [Streaming - Type-Safe Tool Call Events](../../chat/streaming) for a practical walkthrough. | ||
Uh oh!
There was an error while loading. Please reload this page.