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

TextAdapter.md

421 lines11.3 KB

1---
2id: TextAdapter
3title: TextAdapter
4---
5​
6Defined in: [packages/ai/src/activities/chat/adapter.ts:75](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L75)
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### TReasoning
56​
57`TReasoning` *extends* [`ReasoningCapability`](ReasoningCapability.md) = `never`
58​
59## Properties
60​
61### ~types
62​
63```ts
64~types: object;
65```
66​
67Defined in: [packages/ai/src/activities/chat/adapter.ts:133](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L133)
68​
69**`Internal`**
70​
71Type-only properties for inference. Not assigned at runtime.
72​
73#### inputModalities
74​
75```ts
76inputModalities: TInputModalities;
77```
78​
79#### messageMetadataByModality
80​
81```ts
82messageMetadataByModality: TMessageMetadataByModality;
83```
84​
85#### providerOptions
86​
87```ts
88providerOptions: TProviderOptions;
89```
90​
91#### reasoning?
92​
93```ts
94optional reasoning?: TReasoning;
95```
96​
97The model's reasoning levels, for `chat({ reasoning })`. `never`: the
98model does not reason (or the adapter does not map `reasoning`), and
99the option is a type error. Optional, so an adapter written before this
100key existed still type-checks (and gets no `reasoning` option).
101​
102#### systemPromptMetadata
103​
104```ts
105systemPromptMetadata: TSystemPromptMetadata;
106```
107​
108#### toolCallMetadata
109​
110```ts
111toolCallMetadata: TToolCallMetadata;
112```
113​
114#### toolCapabilities
115​
116```ts
117toolCapabilities: TToolCapabilities;
118```
119​
120***
121​
122### api?
123​
124```ts
125readonly optional api?: string;
126```
127​
128Defined in: [packages/ai/src/activities/chat/adapter.ts:90](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L90)
129​
130Wire API identity. Defaults to the adapter kind when omitted.
131​
132***
133​
134### chatStream
135​
136```ts
137chatStream: (options) => AsyncIterable<AdapterYieldChunk>;
138```
139​
140Defined in: [packages/ai/src/activities/chat/adapter.ts:152](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L152)
141​
142Stream text completions from the model
143​
144#### Parameters
145​
146##### options
147​
148[`TextOptions`](TextOptions.md)\<`TProviderOptions`\>
149​
150#### Returns
151​
152`AsyncIterable`\<[`AdapterYieldChunk`](../type-aliases/AdapterYieldChunk.md)\>
153​
154***
155​
156### combinedStructuredOutputSource?
157​
158```ts
159optional combinedStructuredOutputSource?: (modelOptions?) => "text" | "event";
160```
161​
162Defined in: [packages/ai/src/activities/chat/adapter.ts:227](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L227)
163​
164Where native-combined structured output is taken from.
165​
166- `'text'` (default when omitted): the agent loop's accumulated
167 assistant text is schema JSON. The engine parses it after the loop.
168 HTTP adapters use this.
169- `'event'`: the adapter emits `structured-output.complete` during
170 `chatStream`. The engine must not parse accumulated prose. Harness
171 adapters use this.
172​
173#### Parameters
174​
175##### modelOptions?
176​
177`TProviderOptions`
178​
179#### Returns
180​
181`"text"` \| `"event"`
182​
183***
184​
185### compact?
186​
187```ts
188optional compact?: (options) => Promise<ModelMessage<
189 | string
190 | ContentPart<unknown, unknown, unknown, unknown, unknown>[]
191| null>[]>;
192```
193​
194Defined in: [packages/ai/src/activities/chat/adapter.ts:192](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L192)
195​
196Compact the history with the provider's own compaction endpoint.
197Resolves to the history to send from the next call on. It can hold
198provider items that only this provider reads. Optional: adapters
199without native compaction omit it.
200​
201#### Parameters
202​
203##### options
204​
205[`TextCompactOptions`](TextCompactOptions.md)
206​
207#### Returns
208​
209`Promise`\<[`ModelMessage`](ModelMessage.md)\<
210 \| `string`
211 \| [`ContentPart`](../type-aliases/ContentPart.md)\<`unknown`, `unknown`, `unknown`, `unknown`, `unknown`\>[]
212 \| `null`\>[]\>
213​
214***
215​
216### inputModalities?
217​
218```ts
219readonly optional inputModalities?: readonly ("text" | "image" | "audio" | "video" | "document")[];
220```
221​
222Defined in: [packages/ai/src/activities/chat/adapter.ts:120](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L120)
223​
224The input kinds the model reads, at runtime, from the provider's model
225metadata. `undefined` means not known. This mirrors the type-level
226`'~types'.inputModalities`, which has no runtime value. `| undefined`
227keeps model lookups assignable under `exactOptionalPropertyTypes`.
228​
229***
230​
231### kind
232​
233```ts
234readonly kind: "text";
235```
236​
237Defined in: [packages/ai/src/activities/chat/adapter.ts:86](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L86)
238​
239Discriminator for adapter kind
240​
241***
242​
243### midConversationChannels?
244​
245```ts
246readonly optional midConversationChannels?: MidConversationChannels;
247```
248​
249Defined in: [packages/ai/src/activities/chat/adapter.ts:128](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L128)
250​
251The mid-conversation channels of the model: tools and system prompts
252added between model calls can go out with no change to the cached prompt
253prefix. `chat()` passes `TextOptions.midConversationChanges` only when a
254channel is on. `undefined` means no channels.
255​
256***
257​
258### model
259​
260```ts
261readonly model: TModel;
262```
263​
264Defined in: [packages/ai/src/activities/chat/adapter.ts:94](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L94)
265​
266The model this adapter is configured for
267​
268***
269​
270### name
271​
272```ts
273readonly name: string;
274```
275​
276Defined in: [packages/ai/src/activities/chat/adapter.ts:88](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L88)
277​
278Provider name identifier (e.g., 'openai', 'anthropic')
279​
280***
281​
282### provider?
283​
284```ts
285readonly optional provider?: string;
286```
287​
288Defined in: [packages/ai/src/activities/chat/adapter.ts:92](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L92)
289​
290Provider identity. Defaults to `name` when omitted.
291​
292***
293​
294### requires?
295​
296```ts
297readonly optional requires?: readonly CapabilityHandle[];
298```
299​
300Defined in: [packages/ai/src/activities/chat/adapter.ts:103](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L103)
301​
302Capabilities this adapter requires at runtime. `chat()` validates that the
303configured middleware provides each one. Model adapters omit this; harness
304adapters (e.g. a future `claudeCode()`) declare e.g. `[sandboxCapability]`.
305Runtime access to capabilities from inside the adapter is not yet wired —
306this is the declaration/validation surface only.
307​
308***
309​
310### structuredOutput
311​
312```ts
313structuredOutput: (options) => Promise<StructuredOutputResult<unknown>>;
314```
315​
316Defined in: [packages/ai/src/activities/chat/adapter.ts:164](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L164)
317​
318Generate structured output using the provider's native structured output API.
319This method uses stream: false and sends the JSON schema to the provider
320to ensure the response conforms to the expected structure.
321​
322#### Parameters
323​
324##### options
325​
326`StructuredOutputOptions`\<`TProviderOptions`\>
327​
328Structured output options containing chat options and JSON schema
329​
330#### Returns
331​
332`Promise`\<`StructuredOutputResult`\<`unknown`\>\>
333​
334Promise with the raw data (validation is done in the chat function)
335​
336***
337​
338### structuredOutputStream?
339​
340```ts
341optional structuredOutputStream?: (options) => AsyncIterable<AdapterYieldChunk>;
342```
343​
344Defined in: [packages/ai/src/activities/chat/adapter.ts:182](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L182)
345​
346Stream structured output using the provider's native streaming structured
347output API (stream + response_format json_schema in a single request).
348​
349Optional — adapters without native streaming JSON omit this method and the
350activity layer synthesizes a stream around the non-streaming
351`structuredOutput` call.
352​
353Implementations must emit standard AG-UI lifecycle events (RUN_STARTED,
354TEXT_MESSAGE_*, RUN_FINISHED) carrying raw JSON text deltas, plus a final
355`CUSTOM` event named `structured-output.complete` whose `value` is
356`{ object, raw, reasoning? }`. Events must be timestamped when emitted so
357their timestamps follow stream order.
358​
359#### Parameters
360​
361##### options
362​
363`StructuredOutputOptions`\<`TProviderOptions`\>
364​
365#### Returns
366​
367`AsyncIterable`\<[`AdapterYieldChunk`](../type-aliases/AdapterYieldChunk.md)\>
368​
369***
370​
371### supportsCombinedToolsAndSchema?
372​
373```ts
374optional supportsCombinedToolsAndSchema?: (modelOptions?) => boolean;
375```
376​
377Defined in: [packages/ai/src/activities/chat/adapter.ts:213](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L213)
378​
379Declares whether the adapter supports combining `tools` and a
380schema-constrained final answer in a single streaming request.
381​
382When `true`, the engine wires `outputSchema` into the regular
383`chatStream()` call and skips the separate `runStructuredFinalization`
384round-trip. The model's natural final turn carries the
385schema-constrained JSON text and the engine harvests it from the agent
386loop's accumulated content.
387​
388When `false`, `undefined`, or the method is omitted, the engine runs
389the agent loop without `outputSchema` and then issues a separate
390`structuredOutput` / `structuredOutputStream` call against the JSON
391schema for finalization (the legacy path).
392​
393The method receives the per-call `modelOptions` so providers whose
394support depends on the resolved upstream model (e.g. OpenRouter) can
395answer per-request. Most adapters can return a constant.
396​
397#### Parameters
398​
399##### modelOptions?
400​
401`TProviderOptions`
402​
403#### Returns
404​
405`boolean`
406​
407***
408​
409### supportsFileSources?
410​
411```ts
412readonly optional supportsFileSources?: boolean;
413```
414​
415Defined in: [packages/ai/src/activities/chat/adapter.ts:112](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/adapter.ts#L112)
416​
417Declares that this adapter can consume `{ type: 'file' }` content sources
418(provider Files API references). `chat()` rejects file sources in preflight
419for adapters that don't declare this, so an adapter written before the
420file arm existed fails closed instead of silently mis-mapping a reference
421onto its URL/data branch.