Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

chat.md

133 lines4.1 KB

1---
2id: chat
3title: chat
4---
5​
6```ts
7function chat<TAdapter, TSchema, TStream, TTools, TInterrupts, TContext, TMiddleware, TAgents>(options): TextActivityResult<TSchema, TStream, TTools>;
8```
9​
10Defined in: packages/ai/src/activities/chat/index.ts:5864
11​
12Text activity - handles agentic text generation, one-shot text generation, and agentic structured output.
13​
14This activity supports four modes:
151. **Streaming agentic text**: Stream responses with automatic tool execution
162. **Streaming one-shot text**: Simple streaming request/response without tools
173. **Non-streaming text**: Returns a ChatResult with the text and every chunk (stream: false)
184. **Agentic structured output**: Run tools, then return structured data
19​
20## Type Parameters
21​
22### TAdapter
23​
24`TAdapter` *extends* [`AnyTextAdapter`](../type-aliases/AnyTextAdapter.md)
25​
26### TSchema
27​
28`TSchema` *extends* [`SchemaInput`](../type-aliases/SchemaInput.md) \| `undefined` = `undefined`
29​
30### TStream
31​
32`TStream` *extends* `boolean` = `boolean`
33​
34### TTools
35​
36`TTools` *extends*
37 \| readonly (
38 \| `Omit`\<[`Tool`](../interfaces/Tool.md)\<`any`, `any`, `any`, `any`\>, `"execute"`\> & `object` & `object`
39 \| [`ProviderTool`](../interfaces/ProviderTool.md)\<`string`, `TAdapter`\[`"~types"`\]\[`"toolCapabilities"`\]\[`number`\]\>)[]
40 \| `undefined` =
41 \| readonly (
42 \| `Omit`\<[`Tool`](../interfaces/Tool.md)\<`any`, `any`, `any`, `any`\>, `"execute"`\> & `object` & `object`
43 \| [`ProviderTool`](../interfaces/ProviderTool.md)\<`string`, `TAdapter`\[`"~types"`\]\[`"toolCapabilities"`\]\[`number`\]\>)[]
44 \| `undefined`
45​
46### TInterrupts
47​
48`TInterrupts` *extends* readonly [`InterruptDefinition`](../interfaces/InterruptDefinition.md)\<`any`, `any`, `any`, `any`, `any`\>[] = \[\]
49​
50### TContext
51​
52`TContext` = `unknown`
53​
54### TMiddleware
55​
56`TMiddleware` *extends* `unknown`[] \| `undefined` = `undefined`
57​
58### TAgents
59​
60`TAgents` *extends* readonly [`DefinedAgent`](../interfaces/DefinedAgent.md)\<`string`, readonly `SubagentTool`[], [`SchemaInput`](../type-aliases/SchemaInput.md) \| `undefined`, readonly [`InterruptDefinition`](../interfaces/InterruptDefinition.md)\<`any`, `any`, `any`, `any`, `any`\>[], `any`, `unknown`, [`AgentProduces`](../type-aliases/AgentProduces.md) \| `undefined`\>[] = readonly [`DefinedAgent`](../interfaces/DefinedAgent.md)\<`string`, readonly `SubagentTool`[], [`SchemaInput`](../type-aliases/SchemaInput.md) \| `undefined`, readonly [`InterruptDefinition`](../interfaces/InterruptDefinition.md)\<`any`, `any`, `any`, `any`, `any`\>[], `any`, `unknown`, [`AgentProduces`](../type-aliases/AgentProduces.md) \| `undefined`\>[]
61​
62## Parameters
63​
64### options
65​
66`TextActivityOptionsWithContext`\<`TAdapter`, `TSchema`, `TStream`, `TTools`, `TInterrupts`, `TContext`, `TMiddleware`, `TAgents`\>
67​
68## Returns
69​
70`TextActivityResult`\<`TSchema`, `TStream`, `TTools`\>
71​
72## Examples
73​
74**Full agentic text (streaming with tools)**
75​
76```ts
77import { chat } from '@tanstack/ai'
78import { openaiText } from '@tanstack/ai-openai'
79​
80for await (const chunk of chat({
81 adapter: openaiText('gpt-5.5'),
82 messages: [{ role: 'user', content: 'What is the weather?' }],
83 threadId,
84 tools: [weatherTool]
85})) {
86 if (chunk.type === 'TEXT_MESSAGE_CONTENT') {
87 console.log(chunk.delta)
88 }
90```
91​
92**One-shot text (streaming without tools)**
93​
94```ts
95for await (const chunk of chat({
96 adapter: openaiText('gpt-5.5'),
97 messages: [{ role: 'user', content: 'Hello!' }],
98 threadId,
99})) {
100 console.log(chunk)
102```
103​
104**Non-streaming text (stream: false)**
105​
106```ts
107const { text, chunks } = await chat({
108 adapter: openaiText('gpt-5.5'),
109 messages: [{ role: 'user', content: 'Hello!' }],
110 threadId,
111 stream: false
112})
113// text is a string with the full response
114// chunks is every chunk the run produced, in order
115```
116​
117**Agentic structured output (tools + structured response)**
118​
119```ts
120import { z } from 'zod'
121​
122const result = await chat({
123 adapter: openaiText('gpt-5.5'),
124 messages: [{ role: 'user', content: 'Research and summarize the topic' }],
125 threadId,
126 tools: [researchTool, analyzeTool],
127 outputSchema: z.object({
128 summary: z.string(),
129 keyPoints: z.array(z.string())
130 })
131})
132// result is { summary: string, keyPoints: string[] }
133```