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