chatkit-bun 0.0.2

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.
Files changed (52) hide show
  1. package/README.md +202 -0
  2. package/package.json +40 -0
  3. package/src/actions.ts +39 -0
  4. package/src/agents/accumulate.ts +43 -0
  5. package/src/agents/annotations.ts +157 -0
  6. package/src/agents/context.ts +190 -0
  7. package/src/agents/converter.ts +290 -0
  8. package/src/agents/index.ts +25 -0
  9. package/src/agents/stream.ts +1053 -0
  10. package/src/agents/types.ts +30 -0
  11. package/src/agents/workflows.ts +220 -0
  12. package/src/errors.ts +19 -0
  13. package/src/http.ts +60 -0
  14. package/src/index.ts +11 -0
  15. package/src/serialization.ts +75 -0
  16. package/src/server.ts +874 -0
  17. package/src/sqlite-store.ts +400 -0
  18. package/src/store.ts +98 -0
  19. package/src/types/core.ts +322 -0
  20. package/src/types/server.ts +396 -0
  21. package/src/widgets/components.ts +188 -0
  22. package/src/widgets/diff.ts +151 -0
  23. package/src/widgets/index.ts +6 -0
  24. package/src/widgets/serialization.ts +46 -0
  25. package/src/widgets/stream.ts +104 -0
  26. package/src/widgets/template.ts +180 -0
  27. package/src/widgets/types.ts +52 -0
  28. package/types/actions.d.ts +19 -0
  29. package/types/agents/accumulate.d.ts +4 -0
  30. package/types/agents/annotations.d.ts +21 -0
  31. package/types/agents/context.d.ts +35 -0
  32. package/types/agents/converter.d.ts +60 -0
  33. package/types/agents/index.d.ts +9 -0
  34. package/types/agents/stream.d.ts +4 -0
  35. package/types/agents/types.d.ts +26 -0
  36. package/types/agents/workflows.d.ts +34 -0
  37. package/types/errors.d.ts +11 -0
  38. package/types/http.d.ts +6 -0
  39. package/types/index.d.ts +11 -0
  40. package/types/serialization.d.ts +8 -0
  41. package/types/server.d.ts +73 -0
  42. package/types/sqlite-store.d.ts +43 -0
  43. package/types/store.d.ts +45 -0
  44. package/types/types/core.d.ts +1220 -0
  45. package/types/types/server.d.ts +5841 -0
  46. package/types/widgets/components.d.ts +144 -0
  47. package/types/widgets/diff.d.ts +7 -0
  48. package/types/widgets/index.d.ts +6 -0
  49. package/types/widgets/serialization.d.ts +2 -0
  50. package/types/widgets/stream.d.ts +10 -0
  51. package/types/widgets/template.d.ts +19 -0
  52. package/types/widgets/types.d.ts +24 -0
@@ -0,0 +1,322 @@
1
+ import { z } from "zod";
2
+
3
+ export function PageSchema<T extends z.ZodTypeAny>(item: T) {
4
+ return z.object({
5
+ data: z.array(item).default([]),
6
+ has_more: z.boolean().default(false),
7
+ after: z.string().nullable().optional(),
8
+ });
9
+ }
10
+
11
+ export type Page<T> = {
12
+ data: T[];
13
+ has_more: boolean;
14
+ after?: string | null;
15
+ };
16
+
17
+ export const ThreadStatusSchema = z.discriminatedUnion("type", [
18
+ z.object({ type: z.literal("active") }),
19
+ z.object({ type: z.literal("locked"), reason: z.string().nullable().optional() }),
20
+ z.object({ type: z.literal("closed"), reason: z.string().nullable().optional() }),
21
+ ]);
22
+ export type ThreadStatus = z.infer<typeof ThreadStatusSchema>;
23
+
24
+ export const ThreadMetadataSchema = z.object({
25
+ title: z.string().nullable().optional(),
26
+ id: z.string(),
27
+ created_at: z.string().datetime(),
28
+ status: ThreadStatusSchema.default({ type: "active" }),
29
+ allowed_image_domains: z.array(z.string()).nullable().optional(),
30
+ metadata: z.record(z.string(), z.unknown()).default({}),
31
+ });
32
+ export type ThreadMetadata = z.infer<typeof ThreadMetadataSchema>;
33
+
34
+ export const AttachmentUploadDescriptorSchema = z.object({
35
+ url: z.string().url(),
36
+ method: z.enum(["POST", "PUT"]),
37
+ headers: z.record(z.string(), z.string()).default({}),
38
+ });
39
+ export type AttachmentUploadDescriptor = z.infer<typeof AttachmentUploadDescriptorSchema>;
40
+
41
+ const AttachmentBaseSchema = z.object({
42
+ id: z.string(),
43
+ mime_type: z.string(),
44
+ name: z.string(),
45
+ metadata: z.record(z.string(), z.unknown()).nullable().optional(),
46
+ upload_descriptor: AttachmentUploadDescriptorSchema.nullable().optional(),
47
+ thread_id: z.string().nullable().optional(),
48
+ });
49
+
50
+ export const FileAttachmentSchema = AttachmentBaseSchema.extend({
51
+ type: z.literal("file"),
52
+ });
53
+ export type FileAttachment = z.infer<typeof FileAttachmentSchema>;
54
+
55
+ export const ImageAttachmentSchema = AttachmentBaseSchema.extend({
56
+ type: z.literal("image"),
57
+ preview_url: z.string().url(),
58
+ });
59
+ export type ImageAttachment = z.infer<typeof ImageAttachmentSchema>;
60
+
61
+ export const AttachmentSchema = z.discriminatedUnion("type", [
62
+ FileAttachmentSchema,
63
+ ImageAttachmentSchema,
64
+ ]);
65
+ export type Attachment = z.infer<typeof AttachmentSchema>;
66
+
67
+ export const SourceBaseSchema = z.object({
68
+ title: z.string(),
69
+ description: z.string().nullable().optional(),
70
+ timestamp: z.string().nullable().optional(),
71
+ group: z.string().nullable().optional(),
72
+ });
73
+
74
+ export const FileSourceSchema = SourceBaseSchema.extend({
75
+ type: z.literal("file"),
76
+ filename: z.string(),
77
+ });
78
+
79
+ export const UrlSourceSchema = SourceBaseSchema.extend({
80
+ type: z.literal("url"),
81
+ url: z.string(),
82
+ attribution: z.string().nullable().optional(),
83
+ });
84
+
85
+ export const EntitySourceSchema = SourceBaseSchema.extend({
86
+ type: z.literal("entity"),
87
+ id: z.string(),
88
+ icon: z.string().nullable().optional(),
89
+ label: z.string().nullable().optional(),
90
+ inline_label: z.string().nullable().optional(),
91
+ interactive: z.boolean().default(false),
92
+ data: z.record(z.string(), z.unknown()).default({}),
93
+ preview: z.literal("lazy").nullable().optional(),
94
+ });
95
+
96
+ export const SourceSchema = z.discriminatedUnion("type", [
97
+ FileSourceSchema,
98
+ UrlSourceSchema,
99
+ EntitySourceSchema,
100
+ ]);
101
+ export type Source = z.infer<typeof SourceSchema>;
102
+
103
+ export const AnnotationSchema = z.object({
104
+ type: z.literal("annotation").default("annotation"),
105
+ source: SourceSchema,
106
+ index: z.number().int().nullable().optional(),
107
+ });
108
+ export type Annotation = z.infer<typeof AnnotationSchema>;
109
+
110
+ export const AssistantMessageContentSchema = z.object({
111
+ type: z.literal("output_text").default("output_text"),
112
+ text: z.string(),
113
+ annotations: z.array(AnnotationSchema).default([]),
114
+ });
115
+ export type AssistantMessageContent = z.infer<typeof AssistantMessageContentSchema>;
116
+
117
+ export const UserMessageTextContentSchema = z.object({
118
+ type: z.literal("input_text"),
119
+ text: z.string(),
120
+ });
121
+
122
+ export const UserMessageTagContentSchema = z.object({
123
+ type: z.literal("input_tag"),
124
+ id: z.string(),
125
+ text: z.string(),
126
+ data: z.record(z.string(), z.unknown()),
127
+ group: z.string().nullable().optional(),
128
+ interactive: z.boolean().default(false),
129
+ });
130
+
131
+ export const UserMessageContentSchema = z.discriminatedUnion("type", [
132
+ UserMessageTextContentSchema,
133
+ UserMessageTagContentSchema,
134
+ ]);
135
+ export type UserMessageContent = z.infer<typeof UserMessageContentSchema>;
136
+
137
+ export const ToolChoiceSchema = z.object({ id: z.string() });
138
+ export type ToolChoice = z.infer<typeof ToolChoiceSchema>;
139
+
140
+ export const InferenceOptionsSchema = z.object({
141
+ tool_choice: ToolChoiceSchema.nullable().optional(),
142
+ model: z.string().nullable().optional(),
143
+ }).catchall(z.unknown());
144
+ export type InferenceOptions = z.infer<typeof InferenceOptionsSchema>;
145
+
146
+ const BaseTaskSchema = z.object({
147
+ status_indicator: z.enum(["none", "loading", "complete"]).default("none"),
148
+ });
149
+
150
+ export const CustomTaskSchema = BaseTaskSchema.extend({
151
+ type: z.literal("custom"),
152
+ title: z.string().nullable().optional(),
153
+ icon: z.string().nullable().optional(),
154
+ content: z.string().nullable().optional(),
155
+ });
156
+
157
+ export const SearchTaskSchema = BaseTaskSchema.extend({
158
+ type: z.literal("web_search"),
159
+ title: z.string().nullable().optional(),
160
+ title_query: z.string().nullable().optional(),
161
+ queries: z.array(z.string()).default([]),
162
+ sources: z.array(UrlSourceSchema).default([]),
163
+ });
164
+
165
+ export const ThoughtTaskSchema = BaseTaskSchema.extend({
166
+ type: z.literal("thought"),
167
+ title: z.string().nullable().optional(),
168
+ content: z.string(),
169
+ });
170
+
171
+ export const FileTaskSchema = BaseTaskSchema.extend({
172
+ type: z.literal("file"),
173
+ title: z.string().nullable().optional(),
174
+ sources: z.array(FileSourceSchema).default([]),
175
+ });
176
+
177
+ export const ImageTaskSchema = BaseTaskSchema.extend({
178
+ type: z.literal("image"),
179
+ title: z.string().nullable().optional(),
180
+ });
181
+
182
+ export const TaskSchema = z.discriminatedUnion("type", [
183
+ CustomTaskSchema,
184
+ SearchTaskSchema,
185
+ ThoughtTaskSchema,
186
+ FileTaskSchema,
187
+ ImageTaskSchema,
188
+ ]);
189
+ export type Task = z.infer<typeof TaskSchema>;
190
+
191
+ export const WorkflowSummarySchema = z.union([
192
+ z.object({ title: z.string(), icon: z.string().nullable().optional() }),
193
+ z.object({ duration: z.number().int() }),
194
+ ]);
195
+ export type WorkflowSummary = z.infer<typeof WorkflowSummarySchema>;
196
+
197
+ export const WorkflowSchema = z.object({
198
+ type: z.enum(["custom", "reasoning"]),
199
+ tasks: z.array(TaskSchema),
200
+ summary: WorkflowSummarySchema.nullable().optional(),
201
+ expanded: z.boolean().default(false),
202
+ });
203
+ export type Workflow = z.infer<typeof WorkflowSchema>;
204
+
205
+ export const GeneratedImageSchema = z.object({
206
+ id: z.string(),
207
+ url: z.string(),
208
+ });
209
+ export type GeneratedImage = z.infer<typeof GeneratedImageSchema>;
210
+
211
+ export const StructuredInputAnswerSchema = z.object({
212
+ values: z.array(z.string()).default([]),
213
+ skipped: z.boolean().default(false),
214
+ });
215
+ export type StructuredInputAnswer = z.infer<typeof StructuredInputAnswerSchema>;
216
+
217
+ const StructuredInputBaseSchema = z.object({
218
+ id: z.string(),
219
+ question: z.string(),
220
+ answer: StructuredInputAnswerSchema.nullable().optional(),
221
+ });
222
+
223
+ export const StructuredInputMultipleChoiceSchema = StructuredInputBaseSchema.extend({
224
+ type: z.literal("multiple_choice"),
225
+ options: z.array(z.object({ value: z.string() })),
226
+ multiple: z.boolean().default(false),
227
+ });
228
+
229
+ export const StructuredInputFreeformSchema = StructuredInputBaseSchema.extend({
230
+ type: z.literal("freeform"),
231
+ description: z.string().nullable().optional(),
232
+ });
233
+
234
+ export const StructuredInputSchema = z.discriminatedUnion("type", [
235
+ StructuredInputMultipleChoiceSchema,
236
+ StructuredInputFreeformSchema,
237
+ ]);
238
+ export type StructuredInput = z.infer<typeof StructuredInputSchema>;
239
+
240
+ export const ThreadItemBaseSchema = z.object({
241
+ id: z.string(),
242
+ thread_id: z.string(),
243
+ created_at: z.string().datetime(),
244
+ });
245
+
246
+ export const UserMessageItemSchema = ThreadItemBaseSchema.extend({
247
+ type: z.literal("user_message"),
248
+ content: z.array(UserMessageContentSchema),
249
+ attachments: z.array(AttachmentSchema).default([]),
250
+ quoted_text: z.string().nullable().optional(),
251
+ inference_options: InferenceOptionsSchema.default({}),
252
+ });
253
+
254
+ export const AssistantMessageItemSchema = ThreadItemBaseSchema.extend({
255
+ type: z.literal("assistant_message"),
256
+ content: z.array(AssistantMessageContentSchema),
257
+ });
258
+
259
+ export const ClientToolCallItemSchema = ThreadItemBaseSchema.extend({
260
+ type: z.literal("client_tool_call"),
261
+ status: z.union([z.literal("pending"), z.literal("completed")]).default("pending"),
262
+ call_id: z.string(),
263
+ name: z.string(),
264
+ arguments: z.record(z.string(), z.unknown()),
265
+ output: z.unknown().optional(),
266
+ });
267
+
268
+ export const WidgetItemSchema = ThreadItemBaseSchema.extend({
269
+ type: z.literal("widget"),
270
+ widget: z.record(z.string(), z.unknown()),
271
+ copy_text: z.string().nullable().optional(),
272
+ });
273
+
274
+ export const GeneratedImageItemSchema = ThreadItemBaseSchema.extend({
275
+ type: z.literal("generated_image"),
276
+ image: GeneratedImageSchema.nullable().optional(),
277
+ });
278
+
279
+ export const StructuredInputItemSchema = ThreadItemBaseSchema.extend({
280
+ type: z.literal("structured_input"),
281
+ status: z.enum(["pending", "answered", "skipped"]).default("pending"),
282
+ inputs: z.array(StructuredInputSchema),
283
+ });
284
+
285
+ export const TaskItemSchema = ThreadItemBaseSchema.extend({
286
+ type: z.literal("task"),
287
+ task: TaskSchema,
288
+ });
289
+
290
+ export const WorkflowItemSchema = ThreadItemBaseSchema.extend({
291
+ type: z.literal("workflow"),
292
+ workflow: WorkflowSchema,
293
+ });
294
+
295
+ export const HiddenContextItemSchema = ThreadItemBaseSchema.extend({
296
+ type: z.literal("hidden_context_item"),
297
+ content: z.unknown(),
298
+ });
299
+
300
+ export const SDKHiddenContextItemSchema = ThreadItemBaseSchema.extend({
301
+ type: z.literal("sdk_hidden_context"),
302
+ content: z.string(),
303
+ });
304
+
305
+ export const EndOfTurnItemSchema = ThreadItemBaseSchema.extend({
306
+ type: z.literal("end_of_turn"),
307
+ });
308
+
309
+ export const ThreadItemSchema = z.discriminatedUnion("type", [
310
+ UserMessageItemSchema,
311
+ AssistantMessageItemSchema,
312
+ ClientToolCallItemSchema,
313
+ WidgetItemSchema,
314
+ GeneratedImageItemSchema,
315
+ StructuredInputItemSchema,
316
+ TaskItemSchema,
317
+ WorkflowItemSchema,
318
+ HiddenContextItemSchema,
319
+ SDKHiddenContextItemSchema,
320
+ EndOfTurnItemSchema,
321
+ ]);
322
+ export type ThreadItem = z.infer<typeof ThreadItemSchema>;
@@ -0,0 +1,396 @@
1
+ import { z } from "zod";
2
+
3
+ import { ActionConfigSchema } from "../actions";
4
+ import {
5
+ AnnotationSchema,
6
+ AssistantMessageContentSchema,
7
+ GeneratedImageSchema,
8
+ InferenceOptionsSchema,
9
+ PageSchema,
10
+ TaskSchema,
11
+ ThreadItemSchema,
12
+ ThreadMetadataSchema,
13
+ UserMessageContentSchema,
14
+ } from "./core";
15
+
16
+ export const DEFAULT_PAGE_SIZE = 20;
17
+
18
+ const JsonRecordSchema = z.record(z.string(), z.unknown());
19
+
20
+ export const FeedbackKindSchema = z.enum(["positive", "negative"]);
21
+ export type FeedbackKind = z.infer<typeof FeedbackKindSchema>;
22
+
23
+ export const StreamOptionsSchema = z.object({
24
+ allow_cancel: z.boolean(),
25
+ });
26
+ export type StreamOptions = z.infer<typeof StreamOptionsSchema>;
27
+
28
+ export const UserMessageInputSchema = z.object({
29
+ content: z.array(UserMessageContentSchema),
30
+ attachments: z.array(z.string()),
31
+ quoted_text: z.string().nullable().optional(),
32
+ inference_options: InferenceOptionsSchema,
33
+ });
34
+ export type UserMessageInput = z.infer<typeof UserMessageInputSchema>;
35
+
36
+ export const PageParamsSchema = z.object({
37
+ limit: z.number().int().positive().nullable().optional(),
38
+ order: z.enum(["asc", "desc"]).default("desc"),
39
+ after: z.string().nullable().optional(),
40
+ });
41
+ export type PageParams = z.infer<typeof PageParamsSchema>;
42
+
43
+ export const StructuredInputAnswerSubmissionSchema = z.object({
44
+ values: z.array(z.string()).optional(),
45
+ skipped: z.boolean().optional(),
46
+ });
47
+ export type StructuredInputAnswerSubmission = z.infer<
48
+ typeof StructuredInputAnswerSubmissionSchema
49
+ >;
50
+
51
+ export const StructuredInputSubmissionSchema = z.object({
52
+ answers: z.record(z.string(), StructuredInputAnswerSubmissionSchema).default({}),
53
+ status: z.enum(["answered", "skipped"]).default("answered"),
54
+ });
55
+ export type StructuredInputSubmission = z.infer<typeof StructuredInputSubmissionSchema>;
56
+
57
+ export const ThreadCustomActionParamsSchema = z.object({
58
+ thread_id: z.string(),
59
+ item_id: z.string().nullable().optional(),
60
+ action: ActionConfigSchema,
61
+ });
62
+ export type ThreadCustomActionParams = z.infer<typeof ThreadCustomActionParamsSchema>;
63
+
64
+ export const AudioInputSchema = z.object({
65
+ data: z.instanceof(Uint8Array),
66
+ mime_type: z.string(),
67
+ mediaType: z.string(),
68
+ });
69
+ export type AudioInput = z.infer<typeof AudioInputSchema>;
70
+
71
+ export const TranscriptionResultSchema = z.object({
72
+ text: z.string(),
73
+ });
74
+ export type TranscriptionResult = z.infer<typeof TranscriptionResultSchema>;
75
+
76
+ export const BaseRequestSchema = z.object({
77
+ metadata: JsonRecordSchema.default({}),
78
+ });
79
+ export type BaseRequest = z.infer<typeof BaseRequestSchema>;
80
+
81
+ const ThreadCreateParamsSchema = z.object({
82
+ input: UserMessageInputSchema,
83
+ });
84
+
85
+ const ThreadIdParamsSchema = z.object({
86
+ thread_id: z.string(),
87
+ });
88
+
89
+ const AddUserMessageParamsSchema = ThreadIdParamsSchema.extend({
90
+ input: UserMessageInputSchema,
91
+ });
92
+
93
+ const AddClientToolOutputParamsSchema = ThreadIdParamsSchema.extend({
94
+ result: z.unknown(),
95
+ });
96
+
97
+ const AddStructuredInputParamsSchema = ThreadIdParamsSchema.extend({
98
+ item_id: z.string(),
99
+ input: StructuredInputSubmissionSchema,
100
+ });
101
+
102
+ const RetryAfterItemParamsSchema = ThreadIdParamsSchema.extend({
103
+ item_id: z.string(),
104
+ });
105
+
106
+ const ListItemsParamsSchema = ThreadIdParamsSchema.merge(PageParamsSchema);
107
+
108
+ const ItemsFeedbackParamsSchema = ThreadIdParamsSchema.extend({
109
+ item_ids: z.array(z.string()),
110
+ kind: FeedbackKindSchema,
111
+ });
112
+
113
+ const AttachmentsCreateParamsSchema = z.object({
114
+ name: z.string(),
115
+ size: z.number().int(),
116
+ mime_type: z.string(),
117
+ });
118
+
119
+ const AttachmentsDeleteParamsSchema = z.object({
120
+ attachment_id: z.string(),
121
+ });
122
+
123
+ const ThreadsUpdateParamsSchema = ThreadIdParamsSchema.extend({
124
+ title: z.string(),
125
+ });
126
+
127
+ const InputTranscribeParamsSchema = z.object({
128
+ audio_base64: z.string(),
129
+ mime_type: z.string(),
130
+ });
131
+
132
+ function requestSchema<TType extends string, TParams extends z.ZodType>(
133
+ type: TType,
134
+ params: TParams,
135
+ ) {
136
+ return BaseRequestSchema.extend({
137
+ type: z.literal(type),
138
+ params,
139
+ });
140
+ }
141
+
142
+ export const ThreadsCreateRequestSchema = requestSchema("threads.create", ThreadCreateParamsSchema);
143
+ export const ThreadsAddUserMessageRequestSchema = requestSchema(
144
+ "threads.add_user_message",
145
+ AddUserMessageParamsSchema,
146
+ );
147
+ export const ThreadsAddClientToolOutputRequestSchema = requestSchema(
148
+ "threads.add_client_tool_output",
149
+ AddClientToolOutputParamsSchema,
150
+ );
151
+ export const ThreadsAddStructuredInputRequestSchema = requestSchema(
152
+ "threads.add_structured_input",
153
+ AddStructuredInputParamsSchema,
154
+ );
155
+ export const ThreadsRetryAfterItemRequestSchema = requestSchema(
156
+ "threads.retry_after_item",
157
+ RetryAfterItemParamsSchema,
158
+ );
159
+ export const ThreadsCustomActionRequestSchema = requestSchema(
160
+ "threads.custom_action",
161
+ ThreadCustomActionParamsSchema,
162
+ );
163
+
164
+ export const ThreadsGetByIdRequestSchema = requestSchema("threads.get_by_id", ThreadIdParamsSchema);
165
+ export const ThreadsListRequestSchema = requestSchema("threads.list", PageParamsSchema);
166
+ export const ItemsListRequestSchema = requestSchema("items.list", ListItemsParamsSchema);
167
+ export const ItemsFeedbackRequestSchema = requestSchema("items.feedback", ItemsFeedbackParamsSchema);
168
+ export const AttachmentsCreateRequestSchema = requestSchema(
169
+ "attachments.create",
170
+ AttachmentsCreateParamsSchema,
171
+ );
172
+ export const AttachmentsDeleteRequestSchema = requestSchema(
173
+ "attachments.delete",
174
+ AttachmentsDeleteParamsSchema,
175
+ );
176
+ export const ThreadsUpdateRequestSchema = requestSchema("threads.update", ThreadsUpdateParamsSchema);
177
+ export const ThreadsDeleteRequestSchema = requestSchema("threads.delete", ThreadIdParamsSchema);
178
+ export const InputTranscribeRequestSchema = requestSchema(
179
+ "input.transcribe",
180
+ InputTranscribeParamsSchema,
181
+ );
182
+ export const ThreadsSyncCustomActionRequestSchema = requestSchema(
183
+ "threads.sync_custom_action",
184
+ ThreadCustomActionParamsSchema,
185
+ );
186
+
187
+ export const StreamingRequestSchema = z.discriminatedUnion("type", [
188
+ ThreadsCreateRequestSchema,
189
+ ThreadsAddUserMessageRequestSchema,
190
+ ThreadsAddClientToolOutputRequestSchema,
191
+ ThreadsAddStructuredInputRequestSchema,
192
+ ThreadsRetryAfterItemRequestSchema,
193
+ ThreadsCustomActionRequestSchema,
194
+ ]);
195
+ export type StreamingRequest = z.infer<typeof StreamingRequestSchema>;
196
+
197
+ export const NonStreamingRequestSchema = z.discriminatedUnion("type", [
198
+ ThreadsGetByIdRequestSchema,
199
+ ThreadsListRequestSchema,
200
+ ItemsListRequestSchema,
201
+ ItemsFeedbackRequestSchema,
202
+ AttachmentsCreateRequestSchema,
203
+ AttachmentsDeleteRequestSchema,
204
+ ThreadsUpdateRequestSchema,
205
+ ThreadsDeleteRequestSchema,
206
+ InputTranscribeRequestSchema,
207
+ ThreadsSyncCustomActionRequestSchema,
208
+ ]);
209
+ export type NonStreamingRequest = z.infer<typeof NonStreamingRequestSchema>;
210
+
211
+ export const ChatKitRequestSchema = z.discriminatedUnion("type", [
212
+ ...StreamingRequestSchema.options,
213
+ ...NonStreamingRequestSchema.options,
214
+ ]);
215
+ export type ChatKitRequest = z.infer<typeof ChatKitRequestSchema>;
216
+
217
+ const STREAMING_REQUEST_TYPES = [
218
+ "threads.create",
219
+ "threads.add_user_message",
220
+ "threads.add_client_tool_output",
221
+ "threads.add_structured_input",
222
+ "threads.retry_after_item",
223
+ "threads.custom_action",
224
+ ] as const;
225
+
226
+ export function isStreamingRequest(request: ChatKitRequest): request is StreamingRequest {
227
+ return (STREAMING_REQUEST_TYPES as readonly string[]).includes(request.type);
228
+ }
229
+
230
+ export const ThreadSchema = ThreadMetadataSchema.extend({
231
+ items: PageSchema(ThreadItemSchema),
232
+ });
233
+ export type Thread = z.infer<typeof ThreadSchema>;
234
+
235
+ export const ThreadCreatedEventSchema = z.object({
236
+ type: z.literal("thread.created"),
237
+ thread: ThreadSchema,
238
+ });
239
+
240
+ export const ThreadUpdatedEventSchema = z.object({
241
+ type: z.literal("thread.updated"),
242
+ thread: ThreadSchema,
243
+ });
244
+
245
+ export const ThreadItemAddedEventSchema = z.object({
246
+ type: z.literal("thread.item.added"),
247
+ item: ThreadItemSchema,
248
+ });
249
+
250
+ export const ThreadItemDoneEventSchema = z.object({
251
+ type: z.literal("thread.item.done"),
252
+ item: ThreadItemSchema,
253
+ });
254
+
255
+ export const ThreadItemRemovedEventSchema = z.object({
256
+ type: z.literal("thread.item.removed"),
257
+ item_id: z.string(),
258
+ });
259
+
260
+ export const ThreadItemReplacedEventSchema = z.object({
261
+ type: z.literal("thread.item.replaced"),
262
+ item: ThreadItemSchema,
263
+ });
264
+
265
+ export const AssistantMessageContentPartAddedSchema = z.object({
266
+ type: z.literal("assistant_message.content_part.added"),
267
+ content_index: z.number().int().nonnegative(),
268
+ content: AssistantMessageContentSchema,
269
+ });
270
+
271
+ export const AssistantMessageContentPartTextDeltaSchema = z.object({
272
+ type: z.literal("assistant_message.content_part.text_delta"),
273
+ content_index: z.number().int().nonnegative(),
274
+ delta: z.string(),
275
+ });
276
+
277
+ export const AssistantMessageContentPartAnnotationAddedSchema = z.object({
278
+ type: z.literal("assistant_message.content_part.annotation_added"),
279
+ content_index: z.number().int().nonnegative(),
280
+ annotation_index: z.number().int().nonnegative(),
281
+ annotation: AnnotationSchema,
282
+ });
283
+
284
+ export const AssistantMessageContentPartDoneSchema = z.object({
285
+ type: z.literal("assistant_message.content_part.done"),
286
+ content_index: z.number().int().nonnegative(),
287
+ content: AssistantMessageContentSchema,
288
+ });
289
+
290
+ export const WorkflowTaskAddedSchema = z.object({
291
+ type: z.literal("workflow.task.added"),
292
+ task_index: z.number().int().nonnegative(),
293
+ task: TaskSchema,
294
+ });
295
+
296
+ export const WorkflowTaskUpdatedSchema = z.object({
297
+ type: z.literal("workflow.task.updated"),
298
+ task_index: z.number().int().nonnegative(),
299
+ task: TaskSchema,
300
+ });
301
+
302
+ export const GeneratedImageUpdatedSchema = z.object({
303
+ type: z.literal("generated_image.updated"),
304
+ image: GeneratedImageSchema,
305
+ progress: z.number().nullable().optional(),
306
+ });
307
+
308
+ export const WidgetRootUpdatedSchema = z.object({
309
+ type: z.literal("widget.root.updated"),
310
+ widget: JsonRecordSchema,
311
+ });
312
+
313
+ export const WidgetComponentUpdatedSchema = z.object({
314
+ type: z.literal("widget.component.updated"),
315
+ component_id: z.string(),
316
+ component: JsonRecordSchema,
317
+ });
318
+
319
+ export const WidgetStreamingTextValueDeltaSchema = z.object({
320
+ type: z.literal("widget.streaming_text.value_delta"),
321
+ component_id: z.string(),
322
+ delta: z.string(),
323
+ done: z.boolean(),
324
+ });
325
+
326
+ export const ThreadItemUpdateSchema = z.discriminatedUnion("type", [
327
+ AssistantMessageContentPartAddedSchema,
328
+ AssistantMessageContentPartTextDeltaSchema,
329
+ AssistantMessageContentPartAnnotationAddedSchema,
330
+ AssistantMessageContentPartDoneSchema,
331
+ WorkflowTaskAddedSchema,
332
+ WorkflowTaskUpdatedSchema,
333
+ GeneratedImageUpdatedSchema,
334
+ WidgetRootUpdatedSchema,
335
+ WidgetComponentUpdatedSchema,
336
+ WidgetStreamingTextValueDeltaSchema,
337
+ ]);
338
+ export type ThreadItemUpdate = z.infer<typeof ThreadItemUpdateSchema>;
339
+
340
+ export const ThreadItemUpdatedEventSchema = z.object({
341
+ type: z.literal("thread.item.updated"),
342
+ item_id: z.string(),
343
+ update: ThreadItemUpdateSchema,
344
+ });
345
+
346
+ export const StreamOptionsEventSchema = z.object({
347
+ type: z.literal("stream_options"),
348
+ stream_options: StreamOptionsSchema,
349
+ });
350
+
351
+ export const ProgressUpdateEventSchema = z.object({
352
+ type: z.literal("progress_update"),
353
+ icon: z.string().nullable().optional(),
354
+ text: z.string(),
355
+ });
356
+
357
+ export const ClientEffectEventSchema = z.object({
358
+ type: z.literal("client_effect"),
359
+ name: z.string(),
360
+ data: JsonRecordSchema.default({}),
361
+ });
362
+
363
+ export const ErrorEventSchema = z.object({
364
+ type: z.literal("error"),
365
+ code: z.enum(["stream.error", "custom"]).default("custom"),
366
+ message: z.string().nullable().optional(),
367
+ allow_retry: z.boolean().default(false),
368
+ });
369
+
370
+ export const NoticeEventSchema = z.object({
371
+ type: z.literal("notice"),
372
+ level: z.enum(["info", "warning", "danger"]),
373
+ message: z.string(),
374
+ title: z.string().nullable().optional(),
375
+ });
376
+
377
+ export const ThreadStreamEventSchema = z.discriminatedUnion("type", [
378
+ ThreadCreatedEventSchema,
379
+ ThreadUpdatedEventSchema,
380
+ ThreadItemAddedEventSchema,
381
+ ThreadItemDoneEventSchema,
382
+ ThreadItemRemovedEventSchema,
383
+ ThreadItemReplacedEventSchema,
384
+ ThreadItemUpdatedEventSchema,
385
+ StreamOptionsEventSchema,
386
+ ProgressUpdateEventSchema,
387
+ ClientEffectEventSchema,
388
+ ErrorEventSchema,
389
+ NoticeEventSchema,
390
+ ]);
391
+ export type ThreadStreamEvent = z.infer<typeof ThreadStreamEventSchema>;
392
+
393
+ export const SyncCustomActionResponseSchema = z.object({
394
+ updated_item: ThreadItemSchema.nullable().optional(),
395
+ });
396
+ export type SyncCustomActionResponse = z.infer<typeof SyncCustomActionResponseSchema>;