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

ChatMiddlewareContext.md

586 lines9.0 KB

1---
2id: ChatMiddlewareContext
3title: ChatMiddlewareContext
4---
5​
6Defined in: packages/ai/src/activities/chat/middleware/types.ts:193
7​
8Stable context object passed to all middleware hooks.
9Created once per chat() invocation and shared across all hooks.
10​
11## Type Parameters
12​
13### TContext
14​
15`TContext` = `unknown`
16​
17## Properties
18​
19### abort
20​
21```ts
22abort: (reason?) => void;
23```
24​
25Defined in: packages/ai/src/activities/chat/middleware/types.ts:226
26​
27Abort the chat run with a reason
28​
29#### Parameters
30​
31##### reason?
32​
33`string`
34​
35#### Returns
36​
37`void`
38​
39***
40​
41### accumulatedContent
42​
43```ts
44accumulatedContent: string;
45```
46​
47Defined in: packages/ai/src/activities/chat/middleware/types.ts:293
48​
49Accumulated text content for the current iteration
50​
51***
52​
53### activities?
54​
55```ts
56optional activities?: readonly ActivityRecord[];
57```
58​
59Defined in: packages/ai/src/activities/chat/middleware/types.ts:304
60​
61Frontend-only AG-UI activity sidecar. Never model input. Persistence
62writes this when an ActivityStore is configured. Optional so code that
63builds its own context keeps working; the engine always sets it.
64​
65***
66​
67### activity
68​
69```ts
70activity: "chat";
71```
72​
73Defined in: packages/ai/src/activities/chat/middleware/types.ts:260
74​
75Which activity this context describes — always `'chat'`. Present so the
76chat context structurally satisfies the base `GenerationMiddlewareContext`,
77letting an observe-only middleware authored against the base (e.g.
78`otelMiddleware`) run on both chat and media activities.
79​
80***
81​
82### capabilities
83​
84```ts
85capabilities: CapabilityRegistry;
86```
87​
88Defined in: packages/ai/src/activities/chat/middleware/types.ts:313
89​
90Capability bookkeeping for this request. Populated by middleware `setup`
91hooks (via `provide` accessors) and read by later middleware (via `get`
92accessors). Prefer the accessors returned by `createCapability` over using
93this directly. Orthogonal to `context` (the user runtime context).
94​
95***
96​
97### chunkIndex
98​
99```ts
100chunkIndex: number;
101```
102​
103Defined in: packages/ai/src/activities/chat/middleware/types.ts:222
104​
105Running count of chunks yielded so far
106​
107***
108​
109### context
110​
111```ts
112context: TContext;
113```
114​
115Defined in: packages/ai/src/activities/chat/middleware/types.ts:244
116​
117Runtime context provided by chat() options
118​
119***
120​
121### createId
122​
123```ts
124createId: (prefix) => string;
125```
126​
127Defined in: packages/ai/src/activities/chat/middleware/types.ts:306
128​
129Generate a unique ID with the given prefix
130​
131#### Parameters
132​
133##### prefix
134​
135`string`
136​
137#### Returns
138​
139`string`
140​
141***
142​
143### currentMessageId
144​
145```ts
146currentMessageId: string | null;
147```
148​
149Defined in: packages/ai/src/activities/chat/middleware/types.ts:291
150​
151Current assistant message ID (changes per iteration)
152​
153***
154​
155### defer
156​
157```ts
158defer: (promise) => void;
159```
160​
161Defined in: packages/ai/src/activities/chat/middleware/types.ts:250
162​
163Defer a non-blocking side-effect promise.
164Deferred promises do not block streaming and are awaited
165after the terminal hook (onFinish/onAbort/onError).
166​
167#### Parameters
168​
169##### promise
170​
171`Promise`\<`unknown`\>
172​
173#### Returns
174​
175`void`
176​
177***
178​
179### emitCustomEvent
180​
181```ts
182emitCustomEvent: (name, value, options?) => void;
183```
184​
185Defined in: packages/ai/src/activities/chat/middleware/types.ts:238
186​
187Push a `CUSTOM` chunk onto the chat stream immediately.
188The engine yields it as soon as it can (including while `onConfig`
189is still awaiting work such as a summarize call). Durability then
190flushes the event on its own, unless you pass `{ batch: true }`.
191​
192#### Parameters
193​
194##### name
195​
196`string`
197​
198##### value
199​
200`Record`\<`string`, `any`\>
201​
202##### options?
203​
204[`EmitCustomEventOptions`](EmitCustomEventOptions.md)
205​
206#### Returns
207​
208`void`
209​
210***
211​
212### get
213​
214```ts
215get: <TValue>(capability) => TValue;
216```
217​
218Defined in: packages/ai/src/activities/chat/middleware/types.ts:318
219​
220Read a provided capability by its handle. Equivalent to the handle's own
221`get` accessor (`getX(ctx)`); throws if the capability was never provided.
222​
223#### Type Parameters
224​
225##### TValue
226​
227`TValue`
228​
229#### Parameters
230​
231##### capability
232​
233[`Capability`](../type-aliases/Capability.md)\<`TValue`\>
234​
235#### Returns
236​
237`TValue`
238​
239***
240​
241### getOptional
242​
243```ts
244getOptional: <TValue>(capability) => TValue | undefined;
245```
246​
247Defined in: packages/ai/src/activities/chat/middleware/types.ts:323
248​
249Read a capability by its handle, returning `undefined` if it was never
250provided (never throws).
251​
252#### Type Parameters
253​
254##### TValue
255​
256`TValue`
257​
258#### Parameters
259​
260##### capability
261​
262[`Capability`](../type-aliases/Capability.md)\<`TValue`\>
263​
264#### Returns
265​
266`TValue` \| `undefined`
267​
268***
269​
270### hasTools
271​
272```ts
273hasTools: boolean;
274```
275​
276Defined in: packages/ai/src/activities/chat/middleware/types.ts:286
277​
278Whether tools are configured
279​
280***
281​
282### iteration
283​
284```ts
285iteration: number;
286```
287​
288Defined in: packages/ai/src/activities/chat/middleware/types.ts:220
289​
290Current agent loop iteration (0-indexed)
291​
292***
293​
294### messageCount
295​
296```ts
297messageCount: number;
298```
299​
300Defined in: packages/ai/src/activities/chat/middleware/types.ts:284
301​
302Number of messages at the start of the request
303​
304***
305​
306### messages
307​
308```ts
309messages: readonly ModelMessage<
310 | string
311 | ContentPart<unknown, unknown, unknown, unknown, unknown>[]
312 | null>[];
313```
314​
315Defined in: packages/ai/src/activities/chat/middleware/types.ts:298
316​
317Current messages array (read-only view)
318​
319***
320​
321### model
322​
323```ts
324model: string;
325```
326​
327Defined in: packages/ai/src/activities/chat/middleware/types.ts:264
328​
329Model identifier (e.g., 'gpt-5.5')
330​
331***
332​
333### modelOptions?
334​
335```ts
336optional modelOptions?: Record<string, unknown>;
337```
338​
339Defined in: packages/ai/src/activities/chat/middleware/types.ts:279
340​
341Provider-specific model options
342​
343***
344​
345### options?
346​
347```ts
348optional options?: Record<string, unknown>;
349```
350​
351Defined in: packages/ai/src/activities/chat/middleware/types.ts:277
352​
353Flattened generation options (metadata)
354​
355***
356​
357### parentRunId?
358​
359```ts
360optional parentRunId?: string;
361```
362​
363Defined in: packages/ai/src/activities/chat/middleware/types.ts:201
364​
365Interrupted or parent run correlated with this continuation.
366​
367***
368​
369### parentSubagentRunId?
370​
371```ts
372optional parentSubagentRunId?: string;
373```
374​
375Defined in: packages/ai/src/activities/chat/middleware/types.ts:210
376​
377The subagentRunId of the child that started this one, for a nested child.
378​
379***
380​
381### phase
382​
383```ts
384phase: ChatMiddlewarePhase;
385```
386​
387Defined in: packages/ai/src/activities/chat/middleware/types.ts:218
388​
389Current lifecycle phase
390​
391***
392​
393### provide
394​
395```ts
396provide: <TValue>(capability, value) => void;
397```
398​
399Defined in: packages/ai/src/activities/chat/middleware/types.ts:328
400​
401Provide a capability value. Equivalent to the handle's own `provide`
402accessor (`provideX(ctx, value)`). Typically called from `setup`.
403​
404#### Type Parameters
405​
406##### TValue
407​
408`TValue`
409​
410#### Parameters
411​
412##### capability
413​
414[`Capability`](../type-aliases/Capability.md)\<`TValue`\>
415​
416##### value
417​
418`TValue`
419​
420#### Returns
421​
422`void`
423​
424***
425​
426### provider
427​
428```ts
429provider: string;
430```
431​
432Defined in: packages/ai/src/activities/chat/middleware/types.ts:262
433​
434Provider name (e.g., 'openai', 'anthropic')
435​
436***
437​
438### requestId
439​
440```ts
441requestId: string;
442```
443​
444Defined in: packages/ai/src/activities/chat/middleware/types.ts:195
445​
446Unique identifier for this chat request
447​
448***
449​
450### retry
451​
452```ts
453retry: () => void;
454```
455​
456Defined in: packages/ai/src/activities/chat/middleware/types.ts:231
457​
458Call the model again. Works only in `onModelError`, and throws in any
459other hook. `onConfig` runs again before the new call.
460​
461#### Returns
462​
463`void`
464​
465***
466​
467### runId
468​
469```ts
470runId: string;
471```
472​
473Defined in: packages/ai/src/activities/chat/middleware/types.ts:199
474​
475AG-UI run identifier for correlating client and server events
476​
477***
478​
479### signal?
480​
481```ts
482optional signal?: AbortSignal;
483```
484​
485Defined in: packages/ai/src/activities/chat/middleware/types.ts:224
486​
487Abort signal from the chat request
488​
489***
490​
491### source
492​
493```ts
494source: "server" | "client";
495```
496​
497Defined in: packages/ai/src/activities/chat/middleware/types.ts:266
498​
499Source of the chat invocation — always 'server' for server-side chat
500​
501***
502​
503### streamId
504​
505```ts
506streamId: string;
507```
508​
509Defined in: packages/ai/src/activities/chat/middleware/types.ts:197
510​
511Unique identifier for this stream
512​
513***
514​
515### streaming
516​
517```ts
518streaming: boolean;
519```
520​
521Defined in: packages/ai/src/activities/chat/middleware/types.ts:268
522​
523Whether the chat is streaming
524​
525***
526​
527### subagentName?
528​
529```ts
530optional subagentName?: string;
531```
532​
533Defined in: packages/ai/src/activities/chat/middleware/types.ts:208
534​
535The agent name when this run is a subagent.
536​
537***
538​
539### subagentRunId?
540​
541```ts
542optional subagentRunId?: string;
543```
544​
545Defined in: packages/ai/src/activities/chat/middleware/types.ts:206
546​
547Set when this run is a subagent. The id on the child's `SUBAGENT_STARTED`
548and on every chunk it streams. Absent on a top-level run.
549​
550***
551​
552### systemPrompts
553​
554```ts
555systemPrompts: SystemPrompt[];
556```
557​
558Defined in: packages/ai/src/activities/chat/middleware/types.ts:273
559​
560System prompts configured for this chat
561​
562***
563​
564### threadId
565​
566```ts
567threadId: string;
568```
569​
570Defined in: packages/ai/src/activities/chat/middleware/types.ts:216
571​
572AG-UI thread identifier — a stable per-conversation ID used to
573correlate client and server devtools events. It is the caller's
574`threadId`.
575​
576***
577​
578### toolNames?
579​
580```ts
581optional toolNames?: string[];
582```
583​
584Defined in: packages/ai/src/activities/chat/middleware/types.ts:275
585​
586Names of configured tools, if any