@ekairos/events 1.22.32-beta.development.0 → 1.22.34-beta.development.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,241 @@
1
+ import { z } from "zod";
2
+ export declare const contextPartContentSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
3
+ type: z.ZodLiteral<"text">;
4
+ text: z.ZodString;
5
+ }, z.core.$strip>, z.ZodObject<{
6
+ type: z.ZodLiteral<"file">;
7
+ mediaType: z.ZodString;
8
+ filename: z.ZodOptional<z.ZodString>;
9
+ data: z.ZodOptional<z.ZodString>;
10
+ url: z.ZodOptional<z.ZodString>;
11
+ fileId: z.ZodOptional<z.ZodString>;
12
+ }, z.core.$strip>, z.ZodObject<{
13
+ type: z.ZodLiteral<"json">;
14
+ value: z.ZodUnknown;
15
+ }, z.core.$strip>, z.ZodObject<{
16
+ type: z.ZodLiteral<"source-url">;
17
+ sourceId: z.ZodString;
18
+ url: z.ZodString;
19
+ title: z.ZodOptional<z.ZodString>;
20
+ }, z.core.$strip>, z.ZodObject<{
21
+ type: z.ZodLiteral<"source-document">;
22
+ sourceId: z.ZodString;
23
+ mediaType: z.ZodString;
24
+ title: z.ZodString;
25
+ filename: z.ZodOptional<z.ZodString>;
26
+ }, z.core.$strip>], "type">;
27
+ declare const contextInlineContentSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
28
+ type: z.ZodLiteral<"text">;
29
+ text: z.ZodString;
30
+ }, z.core.$strip>, z.ZodObject<{
31
+ type: z.ZodLiteral<"file">;
32
+ mediaType: z.ZodString;
33
+ filename: z.ZodOptional<z.ZodString>;
34
+ data: z.ZodOptional<z.ZodString>;
35
+ url: z.ZodOptional<z.ZodString>;
36
+ fileId: z.ZodOptional<z.ZodString>;
37
+ }, z.core.$strip>, z.ZodObject<{
38
+ type: z.ZodLiteral<"json">;
39
+ value: z.ZodUnknown;
40
+ }, z.core.$strip>], "type">;
41
+ export declare const contextPartSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
42
+ type: z.ZodLiteral<"content">;
43
+ content: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
44
+ type: z.ZodLiteral<"text">;
45
+ text: z.ZodString;
46
+ }, z.core.$strip>, z.ZodObject<{
47
+ type: z.ZodLiteral<"file">;
48
+ mediaType: z.ZodString;
49
+ filename: z.ZodOptional<z.ZodString>;
50
+ data: z.ZodOptional<z.ZodString>;
51
+ url: z.ZodOptional<z.ZodString>;
52
+ fileId: z.ZodOptional<z.ZodString>;
53
+ }, z.core.$strip>, z.ZodObject<{
54
+ type: z.ZodLiteral<"json">;
55
+ value: z.ZodUnknown;
56
+ }, z.core.$strip>], "type">>;
57
+ state: z.ZodOptional<z.ZodEnum<{
58
+ streaming: "streaming";
59
+ done: "done";
60
+ }>>;
61
+ }, z.core.$strip>, z.ZodObject<{
62
+ type: z.ZodLiteral<"reasoning">;
63
+ content: z.ZodArray<z.ZodObject<{
64
+ type: z.ZodLiteral<"text">;
65
+ text: z.ZodString;
66
+ }, z.core.$strip>>;
67
+ state: z.ZodOptional<z.ZodEnum<{
68
+ streaming: "streaming";
69
+ done: "done";
70
+ }>>;
71
+ }, z.core.$strip>, z.ZodObject<{
72
+ type: z.ZodLiteral<"source">;
73
+ content: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
74
+ type: z.ZodLiteral<"source-url">;
75
+ sourceId: z.ZodString;
76
+ url: z.ZodString;
77
+ title: z.ZodOptional<z.ZodString>;
78
+ }, z.core.$strip>, z.ZodObject<{
79
+ type: z.ZodLiteral<"source-document">;
80
+ sourceId: z.ZodString;
81
+ mediaType: z.ZodString;
82
+ title: z.ZodString;
83
+ filename: z.ZodOptional<z.ZodString>;
84
+ }, z.core.$strip>], "type">>;
85
+ }, z.core.$strip>, z.ZodObject<{
86
+ type: z.ZodLiteral<"tool-call">;
87
+ toolCallId: z.ZodString;
88
+ toolName: z.ZodString;
89
+ content: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
90
+ type: z.ZodLiteral<"text">;
91
+ text: z.ZodString;
92
+ }, z.core.$strip>, z.ZodObject<{
93
+ type: z.ZodLiteral<"file">;
94
+ mediaType: z.ZodString;
95
+ filename: z.ZodOptional<z.ZodString>;
96
+ data: z.ZodOptional<z.ZodString>;
97
+ url: z.ZodOptional<z.ZodString>;
98
+ fileId: z.ZodOptional<z.ZodString>;
99
+ }, z.core.$strip>, z.ZodObject<{
100
+ type: z.ZodLiteral<"json">;
101
+ value: z.ZodUnknown;
102
+ }, z.core.$strip>], "type">>;
103
+ state: z.ZodOptional<z.ZodEnum<{
104
+ "input-streaming": "input-streaming";
105
+ "input-available": "input-available";
106
+ }>>;
107
+ }, z.core.$strip>, z.ZodObject<{
108
+ type: z.ZodLiteral<"tool-result">;
109
+ toolCallId: z.ZodString;
110
+ toolName: z.ZodString;
111
+ content: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
112
+ type: z.ZodLiteral<"text">;
113
+ text: z.ZodString;
114
+ }, z.core.$strip>, z.ZodObject<{
115
+ type: z.ZodLiteral<"file">;
116
+ mediaType: z.ZodString;
117
+ filename: z.ZodOptional<z.ZodString>;
118
+ data: z.ZodOptional<z.ZodString>;
119
+ url: z.ZodOptional<z.ZodString>;
120
+ fileId: z.ZodOptional<z.ZodString>;
121
+ }, z.core.$strip>, z.ZodObject<{
122
+ type: z.ZodLiteral<"json">;
123
+ value: z.ZodUnknown;
124
+ }, z.core.$strip>], "type">>;
125
+ state: z.ZodOptional<z.ZodEnum<{
126
+ "output-available": "output-available";
127
+ "output-error": "output-error";
128
+ }>>;
129
+ }, z.core.$strip>], "type">;
130
+ export declare const contextPartEnvelopeSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
131
+ type: z.ZodLiteral<"content">;
132
+ content: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
133
+ type: z.ZodLiteral<"text">;
134
+ text: z.ZodString;
135
+ }, z.core.$strip>, z.ZodObject<{
136
+ type: z.ZodLiteral<"file">;
137
+ mediaType: z.ZodString;
138
+ filename: z.ZodOptional<z.ZodString>;
139
+ data: z.ZodOptional<z.ZodString>;
140
+ url: z.ZodOptional<z.ZodString>;
141
+ fileId: z.ZodOptional<z.ZodString>;
142
+ }, z.core.$strip>, z.ZodObject<{
143
+ type: z.ZodLiteral<"json">;
144
+ value: z.ZodUnknown;
145
+ }, z.core.$strip>], "type">>;
146
+ state: z.ZodOptional<z.ZodEnum<{
147
+ streaming: "streaming";
148
+ done: "done";
149
+ }>>;
150
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
151
+ }, z.core.$strip>, z.ZodObject<{
152
+ type: z.ZodLiteral<"reasoning">;
153
+ content: z.ZodArray<z.ZodObject<{
154
+ type: z.ZodLiteral<"text">;
155
+ text: z.ZodString;
156
+ }, z.core.$strip>>;
157
+ state: z.ZodOptional<z.ZodEnum<{
158
+ streaming: "streaming";
159
+ done: "done";
160
+ }>>;
161
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
162
+ }, z.core.$strip>, z.ZodObject<{
163
+ type: z.ZodLiteral<"source">;
164
+ content: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
165
+ type: z.ZodLiteral<"source-url">;
166
+ sourceId: z.ZodString;
167
+ url: z.ZodString;
168
+ title: z.ZodOptional<z.ZodString>;
169
+ }, z.core.$strip>, z.ZodObject<{
170
+ type: z.ZodLiteral<"source-document">;
171
+ sourceId: z.ZodString;
172
+ mediaType: z.ZodString;
173
+ title: z.ZodString;
174
+ filename: z.ZodOptional<z.ZodString>;
175
+ }, z.core.$strip>], "type">>;
176
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
177
+ }, z.core.$strip>, z.ZodObject<{
178
+ type: z.ZodLiteral<"tool-call">;
179
+ toolCallId: z.ZodString;
180
+ toolName: z.ZodString;
181
+ content: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
182
+ type: z.ZodLiteral<"text">;
183
+ text: z.ZodString;
184
+ }, z.core.$strip>, z.ZodObject<{
185
+ type: z.ZodLiteral<"file">;
186
+ mediaType: z.ZodString;
187
+ filename: z.ZodOptional<z.ZodString>;
188
+ data: z.ZodOptional<z.ZodString>;
189
+ url: z.ZodOptional<z.ZodString>;
190
+ fileId: z.ZodOptional<z.ZodString>;
191
+ }, z.core.$strip>, z.ZodObject<{
192
+ type: z.ZodLiteral<"json">;
193
+ value: z.ZodUnknown;
194
+ }, z.core.$strip>], "type">>;
195
+ state: z.ZodOptional<z.ZodEnum<{
196
+ "input-streaming": "input-streaming";
197
+ "input-available": "input-available";
198
+ }>>;
199
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
200
+ }, z.core.$strip>, z.ZodObject<{
201
+ type: z.ZodLiteral<"tool-result">;
202
+ toolCallId: z.ZodString;
203
+ toolName: z.ZodString;
204
+ content: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
205
+ type: z.ZodLiteral<"text">;
206
+ text: z.ZodString;
207
+ }, z.core.$strip>, z.ZodObject<{
208
+ type: z.ZodLiteral<"file">;
209
+ mediaType: z.ZodString;
210
+ filename: z.ZodOptional<z.ZodString>;
211
+ data: z.ZodOptional<z.ZodString>;
212
+ url: z.ZodOptional<z.ZodString>;
213
+ fileId: z.ZodOptional<z.ZodString>;
214
+ }, z.core.$strip>, z.ZodObject<{
215
+ type: z.ZodLiteral<"json">;
216
+ value: z.ZodUnknown;
217
+ }, z.core.$strip>], "type">>;
218
+ state: z.ZodOptional<z.ZodEnum<{
219
+ "output-available": "output-available";
220
+ "output-error": "output-error";
221
+ }>>;
222
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
223
+ }, z.core.$strip>], "type">;
224
+ export type ContextPartContent = z.infer<typeof contextPartContentSchema>;
225
+ export type ContextInlineContent = z.infer<typeof contextInlineContentSchema>;
226
+ export type ContextPart = z.infer<typeof contextPartSchema>;
227
+ export type ContextPartEnvelope = z.infer<typeof contextPartEnvelopeSchema>;
228
+ export declare function isContextPartEnvelope(value: unknown): value is ContextPartEnvelope;
229
+ export declare function parseContextPartEnvelope(value: unknown): ContextPartEnvelope;
230
+ export declare function splitContextPartEnvelope(value: ContextPartEnvelope): {
231
+ part: ContextPart;
232
+ metadata?: Record<string, unknown>;
233
+ };
234
+ export declare function mergeContextPartEnvelope(params: {
235
+ part: unknown;
236
+ metadata?: unknown;
237
+ }): ContextPartEnvelope;
238
+ export declare function normalizeToolResultContentToBlocks(value: unknown): ContextInlineContent[];
239
+ export declare function normalizeUiPartToContextPartEnvelopes(value: unknown): ContextPartEnvelope[];
240
+ export declare function normalizePartsForPersistence(parts: unknown[]): ContextPartEnvelope[];
241
+ export {};
@@ -0,0 +1,360 @@
1
+ import { z } from "zod";
2
+ const metadataSchema = z.record(z.string(), z.unknown()).optional();
3
+ const textContentSchema = z.object({
4
+ type: z.literal("text"),
5
+ text: z.string(),
6
+ });
7
+ const fileContentSchema = z
8
+ .object({
9
+ type: z.literal("file"),
10
+ mediaType: z.string().min(1),
11
+ filename: z.string().optional(),
12
+ data: z.string().optional(),
13
+ url: z.string().optional(),
14
+ fileId: z.string().optional(),
15
+ })
16
+ .superRefine((value, ctx) => {
17
+ if (!value.data && !value.url && !value.fileId) {
18
+ ctx.addIssue({
19
+ code: z.ZodIssueCode.custom,
20
+ message: "file content requires one of data, url, or fileId",
21
+ });
22
+ }
23
+ });
24
+ const jsonContentSchema = z.object({
25
+ type: z.literal("json"),
26
+ value: z.unknown(),
27
+ });
28
+ const sourceUrlContentSchema = z.object({
29
+ type: z.literal("source-url"),
30
+ sourceId: z.string(),
31
+ url: z.string().min(1),
32
+ title: z.string().optional(),
33
+ });
34
+ const sourceDocumentContentSchema = z.object({
35
+ type: z.literal("source-document"),
36
+ sourceId: z.string(),
37
+ mediaType: z.string().min(1),
38
+ title: z.string().min(1),
39
+ filename: z.string().optional(),
40
+ });
41
+ export const contextPartContentSchema = z.discriminatedUnion("type", [
42
+ textContentSchema,
43
+ fileContentSchema,
44
+ jsonContentSchema,
45
+ sourceUrlContentSchema,
46
+ sourceDocumentContentSchema,
47
+ ]);
48
+ const contextInlineContentSchema = z.discriminatedUnion("type", [
49
+ textContentSchema,
50
+ fileContentSchema,
51
+ jsonContentSchema,
52
+ ]);
53
+ const contextContentPartSchema = z.object({
54
+ type: z.literal("content"),
55
+ content: z.array(contextInlineContentSchema),
56
+ state: z.enum(["streaming", "done"]).optional(),
57
+ });
58
+ const contextReasoningPartSchema = z.object({
59
+ type: z.literal("reasoning"),
60
+ content: z.array(textContentSchema),
61
+ state: z.enum(["streaming", "done"]).optional(),
62
+ });
63
+ const contextSourcePartSchema = z.object({
64
+ type: z.literal("source"),
65
+ content: z.array(z.discriminatedUnion("type", [sourceUrlContentSchema, sourceDocumentContentSchema])),
66
+ });
67
+ const contextToolCallPartSchema = z.object({
68
+ type: z.literal("tool-call"),
69
+ toolCallId: z.string().min(1),
70
+ toolName: z.string().min(1),
71
+ content: z.array(contextInlineContentSchema),
72
+ state: z.enum(["input-streaming", "input-available"]).optional(),
73
+ });
74
+ const contextToolResultPartSchema = z.object({
75
+ type: z.literal("tool-result"),
76
+ toolCallId: z.string().min(1),
77
+ toolName: z.string().min(1),
78
+ content: z.array(contextInlineContentSchema),
79
+ state: z.enum(["output-available", "output-error"]).optional(),
80
+ });
81
+ export const contextPartSchema = z.discriminatedUnion("type", [
82
+ contextContentPartSchema,
83
+ contextReasoningPartSchema,
84
+ contextSourcePartSchema,
85
+ contextToolCallPartSchema,
86
+ contextToolResultPartSchema,
87
+ ]);
88
+ export const contextPartEnvelopeSchema = z.discriminatedUnion("type", [
89
+ contextContentPartSchema.extend({
90
+ metadata: metadataSchema,
91
+ }),
92
+ contextReasoningPartSchema.extend({
93
+ metadata: metadataSchema,
94
+ }),
95
+ contextSourcePartSchema.extend({
96
+ metadata: metadataSchema,
97
+ }),
98
+ contextToolCallPartSchema.extend({
99
+ metadata: metadataSchema,
100
+ }),
101
+ contextToolResultPartSchema.extend({
102
+ metadata: metadataSchema,
103
+ }),
104
+ ]);
105
+ function asRecord(value) {
106
+ if (!value || typeof value !== "object")
107
+ return null;
108
+ return value;
109
+ }
110
+ function cleanRecord(value) {
111
+ return Object.fromEntries(Object.entries(value).filter(([, item]) => item !== undefined));
112
+ }
113
+ function normalizeMetadata(value) {
114
+ if (!value || typeof value !== "object")
115
+ return undefined;
116
+ return value;
117
+ }
118
+ export function isContextPartEnvelope(value) {
119
+ return contextPartEnvelopeSchema.safeParse(value).success;
120
+ }
121
+ export function parseContextPartEnvelope(value) {
122
+ return contextPartEnvelopeSchema.parse(value);
123
+ }
124
+ export function splitContextPartEnvelope(value) {
125
+ const { metadata, ...part } = value;
126
+ return {
127
+ part,
128
+ metadata,
129
+ };
130
+ }
131
+ export function mergeContextPartEnvelope(params) {
132
+ return parseContextPartEnvelope({
133
+ ...(asRecord(params.part) ?? {}),
134
+ metadata: normalizeMetadata(params.metadata),
135
+ });
136
+ }
137
+ function normalizeFileContentBlock(value) {
138
+ return contextInlineContentSchema.parse(cleanRecord({
139
+ type: "file",
140
+ mediaType: typeof value.mediaType === "string" ? value.mediaType : "application/octet-stream",
141
+ filename: typeof value.filename === "string" ? value.filename : undefined,
142
+ data: typeof value.data === "string" ? value.data : undefined,
143
+ url: typeof value.url === "string" ? value.url : undefined,
144
+ fileId: typeof value.fileId === "string" ? value.fileId : undefined,
145
+ }));
146
+ }
147
+ export function normalizeToolResultContentToBlocks(value) {
148
+ if (value === undefined || value === null) {
149
+ return [];
150
+ }
151
+ if (typeof value === "string") {
152
+ return [{ type: "text", text: value }];
153
+ }
154
+ const record = asRecord(value);
155
+ if (!record) {
156
+ return [{ type: "json", value }];
157
+ }
158
+ if (record.type === "json") {
159
+ return [{ type: "json", value: record.value }];
160
+ }
161
+ if (record.type === "content" && Array.isArray(record.value)) {
162
+ const blocks = [];
163
+ for (const entry of record.value) {
164
+ const contentRecord = asRecord(entry);
165
+ if (!contentRecord || typeof contentRecord.type !== "string") {
166
+ blocks.push({ type: "json", value: entry });
167
+ continue;
168
+ }
169
+ if (contentRecord.type === "text" && typeof contentRecord.text === "string") {
170
+ blocks.push({ type: "text", text: contentRecord.text });
171
+ continue;
172
+ }
173
+ if (contentRecord.type === "image-data") {
174
+ blocks.push(cleanRecord({
175
+ type: "file",
176
+ mediaType: typeof contentRecord.mediaType === "string"
177
+ ? contentRecord.mediaType
178
+ : "application/octet-stream",
179
+ filename: typeof contentRecord.filename === "string"
180
+ ? contentRecord.filename
181
+ : undefined,
182
+ data: typeof contentRecord.data === "string" ? contentRecord.data : undefined,
183
+ }));
184
+ continue;
185
+ }
186
+ if (contentRecord.type === "file") {
187
+ blocks.push(normalizeFileContentBlock(contentRecord));
188
+ continue;
189
+ }
190
+ blocks.push({ type: "json", value: entry });
191
+ }
192
+ return blocks;
193
+ }
194
+ if (record.type === "file") {
195
+ return [normalizeFileContentBlock(record)];
196
+ }
197
+ return [{ type: "json", value }];
198
+ }
199
+ function metadataFromUiPart(part) {
200
+ const metadata = cleanRecord({
201
+ provider: normalizeMetadata(part.providerMetadata),
202
+ providerCall: normalizeMetadata(part.callProviderMetadata),
203
+ });
204
+ return Object.keys(metadata).length > 0 ? metadata : undefined;
205
+ }
206
+ export function normalizeUiPartToContextPartEnvelopes(value) {
207
+ const record = asRecord(value);
208
+ if (!record || typeof record.type !== "string") {
209
+ return [];
210
+ }
211
+ const metadata = metadataFromUiPart(record);
212
+ if (record.type === "text" && typeof record.text === "string") {
213
+ return [
214
+ {
215
+ type: "content",
216
+ content: [{ type: "text", text: record.text }],
217
+ state: record.state === "streaming" ? "streaming" : "done",
218
+ metadata,
219
+ },
220
+ ];
221
+ }
222
+ if (record.type === "reasoning" && typeof record.text === "string") {
223
+ return [
224
+ {
225
+ type: "reasoning",
226
+ content: [{ type: "text", text: record.text }],
227
+ state: record.state === "streaming" ? "streaming" : "done",
228
+ metadata,
229
+ },
230
+ ];
231
+ }
232
+ if (record.type === "file") {
233
+ return [
234
+ {
235
+ type: "content",
236
+ content: [
237
+ cleanRecord({
238
+ type: "file",
239
+ mediaType: typeof record.mediaType === "string"
240
+ ? record.mediaType
241
+ : "application/octet-stream",
242
+ filename: typeof record.filename === "string" ? record.filename : undefined,
243
+ url: typeof record.url === "string" ? record.url : undefined,
244
+ data: typeof record.data === "string" ? record.data : undefined,
245
+ fileId: typeof record.fileId === "string" ? record.fileId : undefined,
246
+ }),
247
+ ],
248
+ metadata,
249
+ },
250
+ ];
251
+ }
252
+ if (record.type === "source-url") {
253
+ return [
254
+ {
255
+ type: "source",
256
+ content: [
257
+ {
258
+ type: "source-url",
259
+ sourceId: typeof record.sourceId === "string" ? record.sourceId : "source-url",
260
+ url: typeof record.url === "string" ? record.url : "",
261
+ title: typeof record.title === "string" ? record.title : undefined,
262
+ },
263
+ ],
264
+ metadata,
265
+ },
266
+ ];
267
+ }
268
+ if (record.type === "source-document") {
269
+ return [
270
+ {
271
+ type: "source",
272
+ content: [
273
+ {
274
+ type: "source-document",
275
+ sourceId: typeof record.sourceId === "string"
276
+ ? record.sourceId
277
+ : "source-document",
278
+ mediaType: typeof record.mediaType === "string"
279
+ ? record.mediaType
280
+ : "application/octet-stream",
281
+ title: typeof record.title === "string" ? record.title : "Document",
282
+ filename: typeof record.filename === "string" ? record.filename : undefined,
283
+ },
284
+ ],
285
+ metadata,
286
+ },
287
+ ];
288
+ }
289
+ if (record.type.startsWith("data-")) {
290
+ return [
291
+ {
292
+ type: "content",
293
+ content: [
294
+ {
295
+ type: "json",
296
+ value: record.data,
297
+ },
298
+ ],
299
+ metadata: cleanRecord({
300
+ ...(metadata ?? {}),
301
+ app: {
302
+ dataPartType: record.type.slice("data-".length),
303
+ },
304
+ }),
305
+ },
306
+ ];
307
+ }
308
+ if (record.type.startsWith("tool-")) {
309
+ const toolName = record.type.slice("tool-".length);
310
+ const toolCallId = typeof record.toolCallId === "string" ? record.toolCallId : "";
311
+ if (!toolName || !toolCallId) {
312
+ return [];
313
+ }
314
+ const callPart = {
315
+ type: "tool-call",
316
+ toolCallId,
317
+ toolName,
318
+ state: record.state === "input-streaming"
319
+ ? "input-streaming"
320
+ : "input-available",
321
+ content: "input" in record && record.input !== undefined
322
+ ? [{ type: "json", value: record.input }]
323
+ : [],
324
+ metadata,
325
+ };
326
+ if (record.state === "output-available" || record.state === "output-error") {
327
+ return [
328
+ callPart,
329
+ {
330
+ type: "tool-result",
331
+ toolCallId,
332
+ toolName,
333
+ state: record.state,
334
+ content: record.state === "output-error"
335
+ ? [
336
+ {
337
+ type: "text",
338
+ text: typeof record.errorText === "string" && record.errorText.length > 0
339
+ ? record.errorText
340
+ : "Tool execution failed.",
341
+ },
342
+ ]
343
+ : normalizeToolResultContentToBlocks(record.output),
344
+ metadata,
345
+ },
346
+ ];
347
+ }
348
+ return [callPart];
349
+ }
350
+ return [];
351
+ }
352
+ export function normalizePartsForPersistence(parts) {
353
+ const normalized = parts.flatMap((part) => {
354
+ if (isContextPartEnvelope(part)) {
355
+ return [parseContextPartEnvelope(part)];
356
+ }
357
+ return normalizeUiPartToContextPartEnvelopes(part);
358
+ });
359
+ return normalized;
360
+ }
@@ -24,6 +24,11 @@ export type ContextItem = {
24
24
  createdAt: string;
25
25
  status?: ItemStatus;
26
26
  content: {
27
+ /**
28
+ * Deprecated as the source of truth for output items.
29
+ * The engine maintains this field as a compatibility mirror, but
30
+ * step-level `event_parts` should be used for replay and inspection.
31
+ */
27
32
  parts?: unknown[];
28
33
  [key: string]: unknown;
29
34
  };