forked from openai/chatkit-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.py
More file actions
999 lines (646 loc) · 24.5 KB
/
Copy pathtypes.py
File metadata and controls
999 lines (646 loc) · 24.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
from __future__ import annotations
from datetime import datetime
from typing import Any, Generic, Literal
from pydantic import (
AnyUrl,
BaseModel,
Field,
SerializationInfo,
model_serializer,
)
from typing_extensions import Annotated, TypeIs, TypeVar
from chatkit.errors import ErrorCode
from .actions import Action
from .icons import IconName
from .widgets import WidgetComponent, WidgetRoot
T = TypeVar("T")
class Page(BaseModel, Generic[T]):
"""Paginated collection of records returned from the API."""
data: list[T] = []
has_more: bool = False
after: str | None = None
### REQUEST TYPES
class BaseReq(BaseModel):
"""Base class for all request payloads."""
metadata: dict[str, Any] = Field(default_factory=dict)
"""Arbitrary integration-specific metadata."""
class ThreadsGetByIdReq(BaseReq):
"""Request to fetch a single thread by its identifier."""
type: Literal["threads.get_by_id"] = "threads.get_by_id"
params: ThreadGetByIdParams
class ThreadGetByIdParams(BaseModel):
"""Parameters for retrieving a thread by id."""
thread_id: str
class ThreadsCreateReq(BaseReq):
"""Request to create a new thread from a user message."""
type: Literal["threads.create"] = "threads.create"
params: ThreadCreateParams
class ThreadCreateParams(BaseModel):
"""User input required to create a thread."""
input: UserMessageInput
class ThreadListParams(BaseModel):
"""Pagination parameters for listing threads."""
limit: int | None = None
order: Literal["asc", "desc"] = "desc"
after: str | None = None
class ThreadsListReq(BaseReq):
"""Request to list threads."""
type: Literal["threads.list"] = "threads.list"
params: ThreadListParams
class ThreadsAddUserMessageReq(BaseReq):
"""Request to append a user message to a thread."""
type: Literal["threads.add_user_message"] = "threads.add_user_message"
params: ThreadAddUserMessageParams
class ThreadAddUserMessageParams(BaseModel):
"""Parameters for adding a user message to a thread."""
input: UserMessageInput
thread_id: str
class ThreadsAddClientToolOutputReq(BaseReq):
"""Request to add a client tool's output to a thread."""
type: Literal["threads.add_client_tool_output"] = "threads.add_client_tool_output"
params: ThreadAddClientToolOutputParams
class ThreadAddClientToolOutputParams(BaseModel):
"""Parameters for recording tool output in a thread."""
thread_id: str
result: Any
class ThreadsCustomActionReq(BaseReq):
"""Request to execute a custom action within a thread."""
type: Literal["threads.custom_action"] = "threads.custom_action"
params: ThreadCustomActionParams
class ThreadsSyncCustomActionReq(BaseReq):
"""Request to execute a custom action and return a single item update."""
type: Literal["threads.sync_custom_action"] = "threads.sync_custom_action"
params: ThreadCustomActionParams
class ThreadCustomActionParams(BaseModel):
"""Parameters describing the custom action to execute."""
thread_id: str
item_id: str | None = None
action: Action[str, Any]
class ThreadsRetryAfterItemReq(BaseReq):
"""Request to retry processing after a specific thread item."""
type: Literal["threads.retry_after_item"] = "threads.retry_after_item"
params: ThreadRetryAfterItemParams
class ThreadRetryAfterItemParams(BaseModel):
"""Parameters specifying which item to retry."""
thread_id: str
item_id: str
class ItemsFeedbackReq(BaseReq):
"""Request to submit feedback on specific items."""
type: Literal["items.feedback"] = "items.feedback"
params: ItemFeedbackParams
class ItemFeedbackParams(BaseModel):
"""Parameters describing feedback targets and sentiment."""
thread_id: str
item_ids: list[str]
kind: FeedbackKind
class AttachmentsDeleteReq(BaseReq):
"""Request to remove an attachment."""
type: Literal["attachments.delete"] = "attachments.delete"
params: AttachmentDeleteParams
class AttachmentDeleteParams(BaseModel):
"""Parameters identifying an attachment to delete."""
attachment_id: str
class AttachmentsCreateReq(BaseReq):
"""Request to register a new attachment."""
type: Literal["attachments.create"] = "attachments.create"
params: AttachmentCreateParams
class AttachmentCreateParams(BaseModel):
"""Metadata needed to initialize an attachment."""
name: str
size: int
mime_type: str
class InputTranscribeReq(BaseReq):
"""Request to transcribe an audio payload into text."""
type: Literal["input.transcribe"] = "input.transcribe"
params: InputTranscribeParams
class InputTranscribeParams(BaseModel):
"""Parameters for speech transcription."""
audio_base64: str
"""Base64-encoded audio bytes."""
mime_type: str
"""Raw MIME type for the audio payload, e.g. "audio/webm;codecs=opus"."""
class AudioInput(BaseModel):
"""Audio input data for transcription."""
data: bytes
"""Audio data bytes."""
mime_type: str
"""Raw MIME type for the audio payload, e.g. "audio/webm;codecs=opus"."""
@property
def media_type(self) -> str:
"""Media type for the audio payload, e.g. "audio/webm"."""
return self.mime_type.split(";", 1)[0]
class TranscriptionResult(BaseModel):
"""Input speech transcription result."""
text: str
class ItemsListReq(BaseReq):
"""Request to list items inside a thread."""
type: Literal["items.list"] = "items.list"
params: ItemsListParams
class ItemsListParams(BaseModel):
"""Pagination parameters for listing thread items."""
thread_id: str
limit: int | None = None
order: Literal["asc", "desc"] = "desc"
after: str | None = None
class ThreadsUpdateReq(BaseReq):
"""Request to update thread metadata."""
type: Literal["threads.update"] = "threads.update"
params: ThreadUpdateParams
class ThreadUpdateParams(BaseModel):
"""Parameters for updating a thread's properties."""
thread_id: str
title: str
class ThreadsDeleteReq(BaseReq):
"""Request to delete a thread."""
type: Literal["threads.delete"] = "threads.delete"
params: ThreadDeleteParams
class ThreadDeleteParams(BaseModel):
"""Parameters identifying a thread to delete."""
thread_id: str
StreamingReq = (
ThreadsCreateReq
| ThreadsAddUserMessageReq
| ThreadsAddClientToolOutputReq
| ThreadsRetryAfterItemReq
| ThreadsCustomActionReq
)
"""Union of request types that produce streaming responses."""
NonStreamingReq = (
ThreadsGetByIdReq
| ThreadsListReq
| ItemsListReq
| ItemsFeedbackReq
| AttachmentsCreateReq
| AttachmentsDeleteReq
| ThreadsUpdateReq
| ThreadsDeleteReq
| InputTranscribeReq
| ThreadsSyncCustomActionReq
)
"""Union of request types that yield immediate responses."""
ChatKitReq = Annotated[
StreamingReq | NonStreamingReq,
Field(discriminator="type"),
]
def is_streaming_req(request: ChatKitReq) -> TypeIs[StreamingReq]:
"""Return True if the given request should be processed as streaming."""
return isinstance(
request,
(
ThreadsCreateReq,
ThreadsAddUserMessageReq,
ThreadsRetryAfterItemReq,
ThreadsAddClientToolOutputReq,
ThreadsCustomActionReq,
),
)
### THREAD STREAM EVENT TYPES
class ThreadCreatedEvent(BaseModel):
"""Event emitted when a thread is created."""
type: Literal["thread.created"] = "thread.created"
thread: Thread
class ThreadUpdatedEvent(BaseModel):
"""Event emitted when a thread is updated."""
type: Literal["thread.updated"] = "thread.updated"
thread: Thread
class ThreadItemAddedEvent(BaseModel):
"""Event emitted when a new item is added to a thread."""
type: Literal["thread.item.added"] = "thread.item.added"
item: ThreadItem
class ThreadItemUpdatedEvent(BaseModel):
"""Event describing an update to an existing thread item."""
type: Literal["thread.item.updated"] = "thread.item.updated"
item_id: str
update: ThreadItemUpdate
# Type alias for backwards compatibility
ThreadItemUpdated = ThreadItemUpdatedEvent
class ThreadItemDoneEvent(BaseModel):
"""Event emitted when a thread item is marked complete."""
type: Literal["thread.item.done"] = "thread.item.done"
item: ThreadItem
class ThreadItemRemovedEvent(BaseModel):
"""Event emitted when a thread item is removed."""
type: Literal["thread.item.removed"] = "thread.item.removed"
item_id: str
class ThreadItemReplacedEvent(BaseModel):
"""Event emitted when a thread item is replaced."""
type: Literal["thread.item.replaced"] = "thread.item.replaced"
item: ThreadItem
class StreamOptions(BaseModel):
"""Settings that control runtime stream behavior."""
allow_cancel: bool
"""Allow the client to request cancellation mid-stream."""
class StreamOptionsEvent(BaseModel):
"""Event emitted to set stream options at runtime."""
type: Literal["stream_options"] = "stream_options"
stream_options: StreamOptions
class ProgressUpdateEvent(BaseModel):
"""Event providing incremental progress from the assistant."""
type: Literal["progress_update"] = "progress_update"
icon: IconName | None = None
text: str
class ClientEffectEvent(BaseModel):
"""Event emitted to trigger a client side-effect."""
type: Literal["client_effect"] = "client_effect"
name: str
data: dict[str, Any] = Field(default_factory=dict)
class ErrorEvent(BaseModel):
"""Event indicating an error occurred while processing a thread."""
type: Literal["error"] = "error"
code: ErrorCode | Literal["custom"] = Field(default="custom")
message: str | None = None
allow_retry: bool = Field(default=False)
class NoticeEvent(BaseModel):
"""Event conveying a user-facing notice."""
type: Literal["notice"] = "notice"
level: Literal["info", "warning", "danger"]
message: str
"""
Supports markdown e.g. "You've reached your limit of 100 messages. [Upgrade](https://...) to a paid plan."
"""
title: str | None = None
ThreadStreamEvent = Annotated[
ThreadCreatedEvent
| ThreadUpdatedEvent
| ThreadItemDoneEvent
| ThreadItemAddedEvent
| ThreadItemUpdated
| ThreadItemRemovedEvent
| ThreadItemReplacedEvent
| StreamOptionsEvent
| ProgressUpdateEvent
| ClientEffectEvent
| ErrorEvent
| NoticeEvent,
Field(discriminator="type"),
]
"""Union of all streaming events emitted to clients."""
### THREAD ITEM UPDATE TYPES
class AssistantMessageContentPartAdded(BaseModel):
"""Event emitted when new assistant content is appended."""
type: Literal["assistant_message.content_part.added"] = (
"assistant_message.content_part.added"
)
content_index: int
content: AssistantMessageContent
class AssistantMessageContentPartTextDelta(BaseModel):
"""Event carrying incremental assistant text output."""
type: Literal["assistant_message.content_part.text_delta"] = (
"assistant_message.content_part.text_delta"
)
content_index: int
delta: str
class AssistantMessageContentPartAnnotationAdded(BaseModel):
"""Event announcing a new annotation on assistant content."""
type: Literal["assistant_message.content_part.annotation_added"] = (
"assistant_message.content_part.annotation_added"
)
content_index: int
annotation_index: int
annotation: Annotation
class AssistantMessageContentPartDone(BaseModel):
"""Event indicating an assistant content part is finalized."""
type: Literal["assistant_message.content_part.done"] = (
"assistant_message.content_part.done"
)
content_index: int
content: AssistantMessageContent
class WidgetStreamingTextValueDelta(BaseModel):
"""Event streaming widget text deltas."""
type: Literal["widget.streaming_text.value_delta"] = (
"widget.streaming_text.value_delta"
)
component_id: str
delta: str
done: bool
class WidgetRootUpdated(BaseModel):
"""Event published when the widget root changes."""
type: Literal["widget.root.updated"] = "widget.root.updated"
widget: WidgetRoot
class WidgetComponentUpdated(BaseModel):
"""Event emitted when a widget component updates."""
type: Literal["widget.component.updated"] = "widget.component.updated"
component_id: str
component: WidgetComponent
class WorkflowTaskAdded(BaseModel):
"""Event emitted when a workflow task is added."""
type: Literal["workflow.task.added"] = "workflow.task.added"
task_index: int
task: Task
class WorkflowTaskUpdated(BaseModel):
"""Event emitted when a workflow task is updated."""
type: Literal["workflow.task.updated"] = "workflow.task.updated"
task_index: int
task: Task
class GeneratedImageUpdated(BaseModel):
"""Event emitted when a generated image is updated."""
type: Literal["generated_image.updated"] = "generated_image.updated"
image: GeneratedImage
progress: float | None = None
ThreadItemUpdate = (
AssistantMessageContentPartAdded
| AssistantMessageContentPartTextDelta
| AssistantMessageContentPartAnnotationAdded
| AssistantMessageContentPartDone
| WidgetStreamingTextValueDelta
| WidgetComponentUpdated
| WidgetRootUpdated
| WorkflowTaskAdded
| WorkflowTaskUpdated
| GeneratedImageUpdated
)
"""Union of possible updates applied to thread items."""
class SyncCustomActionResponse(BaseModel):
"""Single thread item update returned by a sync custom action."""
updated_item: ThreadItem | None = None
### THREAD TYPES
class ThreadMetadata(BaseModel):
"""Metadata describing a thread without its items."""
title: str | None = None
id: str
created_at: datetime
status: ThreadStatus = Field(default_factory=lambda: ActiveStatus())
allowed_image_domains: list[str] | None = None
metadata: dict[str, Any] = Field(default_factory=dict)
class ActiveStatus(BaseModel):
"""Status indicating the thread is active."""
type: Literal["active"] = Field(default="active", frozen=True)
class LockedStatus(BaseModel):
"""Status indicating the thread is locked."""
type: Literal["locked"] = Field(default="locked", frozen=True)
reason: str | None = None
class ClosedStatus(BaseModel):
"""Status indicating the thread is closed."""
type: Literal["closed"] = Field(default="closed", frozen=True)
reason: str | None = None
ThreadStatus = Annotated[
ActiveStatus | LockedStatus | ClosedStatus,
Field(discriminator="type"),
]
"""Union of lifecycle states for a thread."""
class Thread(ThreadMetadata):
"""Thread with its paginated items."""
items: Page[ThreadItem]
### THREAD ITEM TYPES
class ThreadItemBase(BaseModel):
"""Base fields shared by all thread items."""
id: str
thread_id: str
created_at: datetime
class UserMessageItem(ThreadItemBase):
"""Thread item representing a user message."""
type: Literal["user_message"] = "user_message"
content: list[UserMessageContent]
attachments: list[Attachment] = Field(default_factory=list)
quoted_text: str | None = None
inference_options: InferenceOptions
class AssistantMessageItem(ThreadItemBase):
"""Thread item representing an assistant message."""
type: Literal["assistant_message"] = "assistant_message"
content: list[AssistantMessageContent]
class ClientToolCallItem(ThreadItemBase):
"""Thread item capturing a client tool call."""
type: Literal["client_tool_call"] = "client_tool_call"
status: Literal["pending", "completed"] = "pending"
call_id: str
name: str
arguments: dict[str, Any]
output: Any | None = None
class WidgetItem(ThreadItemBase):
"""Thread item containing widget content."""
type: Literal["widget"] = "widget"
widget: WidgetRoot
copy_text: str | None = None
class GeneratedImage(BaseModel):
"""Generated image."""
id: str
url: str
class GeneratedImageItem(ThreadItemBase):
"""Thread item containing a generated image."""
type: Literal["generated_image"] = "generated_image"
image: GeneratedImage | None = None
class TaskItem(ThreadItemBase):
"""Thread item containing a task."""
type: Literal["task"] = "task"
task: Task
class WorkflowItem(ThreadItemBase):
"""Thread item representing a workflow."""
type: Literal["workflow"] = "workflow"
workflow: Workflow
class EndOfTurnItem(ThreadItemBase):
"""Marker item indicating the assistant ends its turn."""
type: Literal["end_of_turn"] = "end_of_turn"
class HiddenContextItem(ThreadItemBase):
"""
HiddenContext is never sent to the client. It's not officially part of ChatKit.js.
It is only used internally to store additional context in a specific place in the thread.
"""
type: Literal["hidden_context_item"] = "hidden_context_item"
content: Any
class SDKHiddenContextItem(ThreadItemBase):
"""
Hidden context that is used by the ChatKit Python SDK for storing additional context
for internal operations.
"""
type: Literal["sdk_hidden_context"] = "sdk_hidden_context"
content: str
ThreadItem = Annotated[
UserMessageItem
| AssistantMessageItem
| ClientToolCallItem
| WidgetItem
| GeneratedImageItem
| WorkflowItem
| TaskItem
| HiddenContextItem
| SDKHiddenContextItem
| EndOfTurnItem,
Field(discriminator="type"),
]
"""Union of all thread item variants."""
### ASSISTANT MESSAGE TYPES
class AssistantMessageContent(BaseModel):
"""Assistant message content consisting of text and annotations."""
annotations: list[Annotation] = Field(default_factory=list)
text: str
type: Literal["output_text"] = "output_text"
class Annotation(BaseModel):
"""Reference to supporting context attached to assistant output."""
type: Literal["annotation"] = "annotation"
source: URLSource | FileSource | EntitySource
index: int | None = None
### USER MESSAGE TYPES
class UserMessageInput(BaseModel):
"""Payload describing a user message submission."""
content: list[UserMessageContent]
attachments: list[str]
quoted_text: str | None = None
inference_options: InferenceOptions
class UserMessageTextContent(BaseModel):
"""User message content containing plaintext."""
type: Literal["input_text"] = "input_text"
text: str
class UserMessageTagContent(BaseModel):
"""User message content representing an interactive tag."""
type: Literal["input_tag"] = "input_tag"
id: str
text: str
data: dict[str, Any]
group: str | None = None
interactive: bool = False
UserMessageContent = Annotated[
UserMessageTextContent | UserMessageTagContent, Field(discriminator="type")
]
"""Union of allowed user message content payloads."""
class InferenceOptions(BaseModel):
"""Model and tool configuration for message processing."""
tool_choice: ToolChoice | None = None
model: str | None = None
class ToolChoice(BaseModel):
"""Explicit tool selection for the assistant to invoke."""
id: str
class AttachmentUploadDescriptor(BaseModel):
"""Two-phase upload instructions."""
url: AnyUrl
method: Literal["POST", "PUT"]
"""The HTTP method to use when uploading the file for two-phase upload."""
headers: dict[str, str] = Field(default_factory=dict)
"""Optional headers to include in the upload request."""
class AttachmentBase(BaseModel):
"""Base metadata shared by all attachments."""
id: str
name: str
mime_type: str
upload_descriptor: AttachmentUploadDescriptor | None = None
"""
Two-phase upload instructions.
Should be set to None after upload is complete or when using direct upload
where uploading happens when creating the attachment object.
"""
thread_id: str | None = None
"""
The thread the attachment belongs to, if any.
Added when the user message that contains the attachment is saved to store.
"""
metadata: dict[str, Any] | None = None
"""
Integration-only metadata stored with the attachment. Ignored by ChatKit and not
returned in ChatKitServer responses. If you serialize attachments from a custom
direct-upload endpoint and want to omit this field, pass context={"exclude_metadata": True}.
"""
@model_serializer(mode="wrap")
def _serialize(self, serializer, info: SerializationInfo):
data = serializer(self)
if isinstance(data, dict) and (info.context or {}).get("exclude_metadata"):
data.pop("metadata", None)
return data
class FileAttachment(AttachmentBase):
"""Attachment representing a generic file."""
type: Literal["file"] = "file"
class ImageAttachment(AttachmentBase):
"""Attachment representing an image resource."""
type: Literal["image"] = "image"
preview_url: AnyUrl
Attachment = Annotated[
FileAttachment | ImageAttachment,
Field(discriminator="type"),
]
"""Union of supported attachment types."""
### WORKFLOW TYPES
class Workflow(BaseModel):
"""Workflow attached to a thread with optional summary."""
type: Literal["custom", "reasoning"]
tasks: list[Task]
summary: WorkflowSummary | None = None
expanded: bool = False
class CustomSummary(BaseModel):
"""Custom summary for a workflow."""
title: str
icon: IconName | None = None
class DurationSummary(BaseModel):
"""Summary providing total workflow duration."""
duration: int
"""The duration of the workflow in seconds"""
WorkflowSummary = CustomSummary | DurationSummary
"""Summary variants available for workflows."""
### TASK TYPES
class BaseTask(BaseModel):
"""Base fields common to all workflow tasks."""
status_indicator: Literal["none", "loading", "complete"] = "none"
"""Only used when rendering the task as part of a workflow. Indicates the status of the task."""
class CustomTask(BaseTask):
"""Workflow task displaying custom content."""
type: Literal["custom"] = "custom"
title: str | None = None
icon: IconName | None = None
content: str | None = None
class SearchTask(BaseTask):
"""Workflow task representing a web search."""
type: Literal["web_search"] = "web_search"
title: str | None = None
title_query: str | None = None
queries: list[str] = Field(default_factory=list)
sources: list[URLSource] = Field(default_factory=list)
class ThoughtTask(BaseTask):
"""Workflow task capturing assistant reasoning."""
type: Literal["thought"] = "thought"
title: str | None = None
content: str
class FileTask(BaseTask):
"""Workflow task referencing file sources."""
type: Literal["file"] = "file"
title: str | None = None
sources: list[FileSource] = Field(default_factory=list)
class ImageTask(BaseTask):
"""Workflow task rendering image content."""
type: Literal["image"] = "image"
title: str | None = None
Task = Annotated[
CustomTask | SearchTask | ThoughtTask | FileTask | ImageTask,
Field(discriminator="type"),
]
"""Union of workflow task variants."""
### SOURCE TYPES
class SourceBase(BaseModel):
"""Base class for sources displayed to users."""
title: str
description: str | None = None
timestamp: str | None = None
group: str | None = None
class FileSource(SourceBase):
"""Source metadata for file-based references."""
type: Literal["file"] = "file"
filename: str
class URLSource(SourceBase):
"""Source metadata for external URLs."""
type: Literal["url"] = "url"
url: str
attribution: str | None = None
class EntitySource(SourceBase):
"""Source metadata for entity references."""
type: Literal["entity"] = "entity"
id: str
icon: IconName | None = None
label: str | None = None
"""Optional label shown with the icon in the default entity hover header
when no preview callback is provided.
"""
inline_label: str | None = None
"""Optional label for the inline annotation view. When not provided, the icon is used instead."""
interactive: bool = False
"""Per-entity toggle to wire client callbacks and render this entity as interactive."""
data: dict[str, Any] = Field(default_factory=dict)
"""Additional data for the entity source that is passed to client entity callbacks."""
preview: Literal["lazy"] | None = Field(
default=None,
deprecated=True,
description="This field is ignored. Please use the entities.onRequestPreview ChatKit.js option instead.",
)
Source = Annotated[
URLSource | FileSource | EntitySource,
Field(discriminator="type"),
]
"""Union of supported source types."""
### MISC TYPES
FeedbackKind = Literal["positive", "negative"]
"""Literal type for feedback sentiment."""