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

chat.md

129 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:5843](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/index.ts#L5843)
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 tools: [weatherTool]
84})) {
85 if (chunk.type === 'TEXT_MESSAGE_CONTENT') {
86 console.log(chunk.delta)
87 }
89```
90​
91**One-shot text (streaming without tools)**
92​
93```ts
94for await (const chunk of chat({
95 adapter: openaiText('gpt-5.5'),
96 messages: [{ role: 'user', content: 'Hello!' }]
97})) {
98 console.log(chunk)
100```
101​
102**Non-streaming text (stream: false)**
103​
104```ts
105const { text, chunks } = await chat({
106 adapter: openaiText('gpt-5.5'),
107 messages: [{ role: 'user', content: 'Hello!' }],
108 stream: false
109})
110// text is a string with the full response
111// chunks is every chunk the run produced, in order
112```
113​
114**Agentic structured output (tools + structured response)**
115​
116```ts
117import { z } from 'zod'
118​
119const result = await chat({
120 adapter: openaiText('gpt-5.5'),
121 messages: [{ role: 'user', content: 'Research and summarize the topic' }],
122 tools: [researchTool, analyzeTool],
123 outputSchema: z.object({
124 summary: z.string(),
125 keyPoints: z.array(z.string())
126 })
127})
128// result is { summary: string, keyPoints: string[] }
129```