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

GenerationMiddlewareContext.md

189 lines3.7 KB

1---
2id: GenerationMiddlewareContext
3title: GenerationMiddlewareContext
4---
5​
6Defined in: packages/ai/src/activities/middleware/types.ts:61
7​
8Stable context passed to every [GenerationMiddleware](GenerationMiddleware.md) hook. Created
9once per activity call and shared across the hooks of that call.
10​
11Carries only fields every activity can honor. `ChatMiddlewareContext`
12structurally includes all of these plus chat-only state (messages,
13iteration, capabilities, …), which is why a chat middleware that reads those
14extra fields is not assignable to `GenerationMiddleware`.
15​
16## Type Parameters
17​
18### TContext
19​
20`TContext` = `unknown`
21​
22## Properties
23​
24### activity
25​
26```ts
27activity: GenerationActivity;
28```
29​
30Defined in: packages/ai/src/activities/middleware/types.ts:68
31​
32Which activity this call is. Discriminates media from chat.
33​
34***
35​
36### artifactInputs?
37​
38```ts
39optional artifactInputs?: unknown;
40```
41​
42Defined in: packages/ai/src/activities/middleware/types.ts:108
43​
44Activity inputs captured for middleware that needs to transform or persist
45the result together with reconstructable request metadata.
46​
47***
48​
49### context
50​
51```ts
52context: TContext;
53```
54​
55Defined in: packages/ai/src/activities/middleware/types.ts:88
56​
57Runtime context provided by the activity options, if any.
58​
59***
60​
61### createId
62​
63```ts
64createId: (prefix) => string;
65```
66​
67Defined in: packages/ai/src/activities/middleware/types.ts:86
68​
69Generate a unique id with the given prefix.
70​
71#### Parameters
72​
73##### prefix
74​
75`string`
76​
77#### Returns
78​
79`string`
80​
81***
82​
83### model
84​
85```ts
86model: string;
87```
88​
89Defined in: packages/ai/src/activities/middleware/types.ts:72
90​
91Model id. Emitted as `gen_ai.request.model`.
92​
93***
94​
95### modelOptions?
96​
97```ts
98optional modelOptions?: unknown;
99```
100​
101Defined in: packages/ai/src/activities/middleware/types.ts:82
102​
103Provider-specific options passed to the activity, if any. Typed `unknown`
104because each activity's options are strongly typed per model; a supertype
105of `ChatMiddlewareContext`'s `modelOptions`.
106​
107***
108​
109### provider
110​
111```ts
112provider: string;
113```
114​
115Defined in: packages/ai/src/activities/middleware/types.ts:70
116​
117Provider/adapter name (e.g. `"openai"`). Emitted as `gen_ai.system`.
118​
119***
120​
121### requestId
122​
123```ts
124requestId: string;
125```
126​
127Defined in: packages/ai/src/activities/middleware/types.ts:66
128​
129Stable id correlating the `onStart` / `onFinish` / `onError` / `onAbort`
130hooks of a single activity call.
131​
132***
133​
134### resultTransforms
135​
136```ts
137resultTransforms: GenerationResultTransform<any, TContext>[];
138```
139​
140Defined in: packages/ai/src/activities/middleware/types.ts:103
141​
142Result transforms registered by middleware during this activity call.
143Transforms run after the raw adapter result exists and before the final
144result is returned or streamed. Push multiple transforms to run them in
145registration order.
146​
147REQUIRED (always an array, empty when nothing registered): middleware
148registers by pushing onto it, so an optional array would let a host that
149builds its own context omit it and silently no-op every registration —
150generation persistence would then mark a run completed with neither its
151result nor its artifacts written, with nothing to observe but the missing
152data. Every context the library builds comes from
153`createGenerationContext`, which always sets `[]`.
154​
155***
156​
157### runId?
158​
159```ts
160optional runId?: string;
161```
162​
163Defined in: packages/ai/src/activities/middleware/types.ts:76
164​
165Stable run id, when supplied by the caller.
166​
167***
168​
169### source
170​
171```ts
172source: "server" | "client";
173```
174​
175Defined in: packages/ai/src/activities/middleware/types.ts:84
176​
177Where the call originates. Always `'server'` for media activities.
178​
179***
180​
181### threadId?
182​
183```ts
184optional threadId?: string;
185```
186​
187Defined in: packages/ai/src/activities/middleware/types.ts:74
188​
189Stable conversation/thread id. Always set, except for `getVideoJobStatus` polls.