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

TextAdapter.md

321 lines8.5 KB

1---
2id: TextAdapter
3title: TextAdapter
4---
5​
6Defined in: [packages/ai/src/activities/chat/adapter.ts:67](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L67)
7​
8Text adapter interface with pre-resolved generics.
9​
10An adapter is created by a provider function: `provider('model')` → `adapter`
11All type resolution happens at the provider call site, not in this interface.
12​
13Generic parameters:
14- TModel: The specific model name (e.g., 'gpt-4o')
15- TProviderOptions: Provider-specific options for this model (already resolved)
16- TInputModalities: Supported input modalities for this model (already resolved)
17- TMessageMetadata: Metadata types for content parts (already resolved)
18- TToolCapabilities: Tuple of tool-kind strings supported by this model, resolved from `supports.tools`
19- TToolCallMetadata: Metadata type that round-trips with tool calls (e.g. Gemini's `thoughtSignature`)
20- TSystemPromptMetadata: Provider-typed metadata accepted on each
21 `systemPrompts[i]` entry (e.g. Anthropic `cache_control`). Defaults to
22 `never` — adapters without per-prompt metadata reject the `metadata`
23 field at the call site.
24​
25## Type Parameters
26​
27### TModel
28​
29`TModel` *extends* `string`
30​
31### TProviderOptions
32​
33`TProviderOptions` *extends* `Record`\<`string`, `any`\>
34​
35### TInputModalities
36​
37`TInputModalities` *extends* `ReadonlyArray`\<[`Modality`](../type-aliases/Modality.md)\>
38​
39### TMessageMetadataByModality
40​
41`TMessageMetadataByModality` *extends* [`DefaultMessageMetadataByModality`](DefaultMessageMetadataByModality.md)
42​
43### TToolCapabilities
44​
45`TToolCapabilities` *extends* `ReadonlyArray`\<`string`\> = `ReadonlyArray`\<`string`\>
46​
47### TToolCallMetadata
48​
49`TToolCallMetadata` = `unknown`
50​
51### TSystemPromptMetadata
52​
53`TSystemPromptMetadata` = `never`
54​
55## Properties
56​
57### ~types
58​
59```ts
60~types: object;
61```
62​
63Defined in: [packages/ai/src/activities/chat/adapter.ts:104](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L104)
64​
65**`Internal`**
66​
67Type-only properties for inference. Not assigned at runtime.
68​
69#### inputModalities
70​
71```ts
72inputModalities: TInputModalities;
73```
74​
75#### messageMetadataByModality
76​
77```ts
78messageMetadataByModality: TMessageMetadataByModality;
79```
80​
81#### providerOptions
82​
83```ts
84providerOptions: TProviderOptions;
85```
86​
87#### systemPromptMetadata
88​
89```ts
90systemPromptMetadata: TSystemPromptMetadata;
91```
92​
93#### toolCallMetadata
94​
95```ts
96toolCallMetadata: TToolCallMetadata;
97```
98​
99#### toolCapabilities
100​
101```ts
102toolCapabilities: TToolCapabilities;
103```
104​
105***
106​
107### chatStream
108​
109```ts
110chatStream: (options) => AsyncIterable<AdapterYieldChunk>;
111```
112​
113Defined in: [packages/ai/src/activities/chat/adapter.ts:116](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L116)
114​
115Stream text completions from the model
116​
117#### Parameters
118​
119##### options
120​
121[`TextOptions`](TextOptions.md)\<`TProviderOptions`\>
122​
123#### Returns
124​
125`AsyncIterable`\<[`AdapterYieldChunk`](../type-aliases/AdapterYieldChunk.md)\>
126​
127***
128​
129### combinedStructuredOutputSource?
130​
131```ts
132optional combinedStructuredOutputSource?: (modelOptions?) => "text" | "event";
133```
134​
135Defined in: [packages/ai/src/activities/chat/adapter.ts:183](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L183)
136​
137Where native-combined structured output is taken from.
138​
139- `'text'` (default when omitted): the agent loop's accumulated
140 assistant text is schema JSON. The engine parses it after the loop.
141 HTTP adapters use this.
142- `'event'`: the adapter emits `structured-output.complete` during
143 `chatStream`. The engine must not parse accumulated prose. Harness
144 adapters use this.
145​
146#### Parameters
147​
148##### modelOptions?
149​
150`TProviderOptions`
151​
152#### Returns
153​
154`"text"` \| `"event"`
155​
156***
157​
158### kind
159​
160```ts
161readonly kind: "text";
162```
163​
164Defined in: [packages/ai/src/activities/chat/adapter.ts:77](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L77)
165​
166Discriminator for adapter kind
167​
168***
169​
170### model
171​
172```ts
173readonly model: TModel;
174```
175​
176Defined in: [packages/ai/src/activities/chat/adapter.ts:81](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L81)
177​
178The model this adapter is configured for
179​
180***
181​
182### name
183​
184```ts
185readonly name: string;
186```
187​
188Defined in: [packages/ai/src/activities/chat/adapter.ts:79](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L79)
189​
190Provider name identifier (e.g., 'openai', 'anthropic')
191​
192***
193​
194### requires?
195​
196```ts
197readonly optional requires?: readonly CapabilityHandle[];
198```
199​
200Defined in: [packages/ai/src/activities/chat/adapter.ts:90](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L90)
201​
202Capabilities this adapter requires at runtime. `chat()` validates that the
203configured middleware provides each one. Model adapters omit this; harness
204adapters (e.g. a future `claudeCode()`) declare e.g. `[sandboxCapability]`.
205Runtime access to capabilities from inside the adapter is not yet wired —
206this is the declaration/validation surface only.
207​
208***
209​
210### structuredOutput
211​
212```ts
213structuredOutput: (options) => Promise<StructuredOutputResult<unknown>>;
214```
215​
216Defined in: [packages/ai/src/activities/chat/adapter.ts:128](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L128)
217​
218Generate structured output using the provider's native structured output API.
219This method uses stream: false and sends the JSON schema to the provider
220to ensure the response conforms to the expected structure.
221​
222#### Parameters
223​
224##### options
225​
226`StructuredOutputOptions`\<`TProviderOptions`\>
227​
228Structured output options containing chat options and JSON schema
229​
230#### Returns
231​
232`Promise`\<`StructuredOutputResult`\<`unknown`\>\>
233​
234Promise with the raw data (validation is done in the chat function)
235​
236***
237​
238### structuredOutputStream?
239​
240```ts
241optional structuredOutputStream?: (options) => AsyncIterable<AdapterYieldChunk>;
242```
243​
244Defined in: [packages/ai/src/activities/chat/adapter.ts:146](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L146)
245​
246Stream structured output using the provider's native streaming structured
247output API (stream + response_format json_schema in a single request).
248​
249Optional — adapters without native streaming JSON omit this method and the
250activity layer synthesizes a stream around the non-streaming
251`structuredOutput` call.
252​
253Implementations must emit standard AG-UI lifecycle events (RUN_STARTED,
254TEXT_MESSAGE_*, RUN_FINISHED) carrying raw JSON text deltas, plus a final
255`CUSTOM` event named `structured-output.complete` whose `value` is
256`{ object, raw, reasoning? }`. Events must be timestamped when emitted so
257their timestamps follow stream order.
258​
259#### Parameters
260​
261##### options
262​
263`StructuredOutputOptions`\<`TProviderOptions`\>
264​
265#### Returns
266​
267`AsyncIterable`\<[`AdapterYieldChunk`](../type-aliases/AdapterYieldChunk.md)\>
268​
269***
270​
271### supportsCombinedToolsAndSchema?
272​
273```ts
274optional supportsCombinedToolsAndSchema?: (modelOptions?) => boolean;
275```
276​
277Defined in: [packages/ai/src/activities/chat/adapter.ts:169](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L169)
278​
279Declares whether the adapter supports combining `tools` and a
280schema-constrained final answer in a single streaming request.
281​
282When `true`, the engine wires `outputSchema` into the regular
283`chatStream()` call and skips the separate `runStructuredFinalization`
284round-trip. The model's natural final turn carries the
285schema-constrained JSON text and the engine harvests it from the agent
286loop's accumulated content.
287​
288When `false`, `undefined`, or the method is omitted, the engine runs
289the agent loop without `outputSchema` and then issues a separate
290`structuredOutput` / `structuredOutputStream` call against the JSON
291schema for finalization (the legacy path).
292​
293The method receives the per-call `modelOptions` so providers whose
294support depends on the resolved upstream model (e.g. OpenRouter) can
295answer per-request. Most adapters can return a constant.
296​
297#### Parameters
298​
299##### modelOptions?
300​
301`TProviderOptions`
302​
303#### Returns
304​
305`boolean`
306​
307***
308​
309### supportsFileSources?
310​
311```ts
312readonly optional supportsFileSources?: boolean;
313```
314​
315Defined in: [packages/ai/src/activities/chat/adapter.ts:99](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L99)
316​
317Declares that this adapter can consume `{ type: 'file' }` content sources
318(provider Files API references). `chat()` rejects file sources in preflight
319for adapters that don't declare this, so an adapter written before the
320file arm existed fails closed instead of silently mis-mapping a reference
321onto its URL/data branch.