openai/chatkit-python
Publicmirrored from https://github.com/openai/chatkit-pythonAvailable
chatkit/types.py
923lines · modeblame
f688d870victor-openai8 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 Kim7 months ago | 12 | from .icons import IconName |
f688d870victor-openai8 months ago | 13 | from .widgets import WidgetComponent, WidgetRoot |
| 14 | | |
| 15 | T = TypeVar("T") | |
| 16 | | |
| 17 | | |
| 18 | class Page(BaseModel, Generic[T]): | |
6988ea0avictor-openai8 months ago | 19 | """Paginated collection of records returned from the API.""" |
| 20 | | |
f688d870victor-openai8 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-openai8 months ago | 30 | """Base class for all request payloads.""" |
| 31 | | |
f688d870victor-openai8 months ago | 32 | metadata: dict[str, Any] = Field(default_factory=dict) |
| 33 | """Arbitrary integration-specific metadata.""" | |
| 34 | | |
| 35 | | |
| 36 | class ThreadsGetByIdReq(BaseReq): | |
6988ea0avictor-openai8 months ago | 37 | """Request to fetch a single thread by its identifier.""" |
| 38 | | |
f688d870victor-openai8 months ago | 39 | type: Literal["threads.get_by_id"] = "threads.get_by_id" |
| 40 | params: ThreadGetByIdParams | |
| 41 | | |
| 42 | | |
| 43 | class ThreadGetByIdParams(BaseModel): | |
6988ea0avictor-openai8 months ago | 44 | """Parameters for retrieving a thread by id.""" |
| 45 | | |
f688d870victor-openai8 months ago | 46 | thread_id: str |
| 47 | | |
| 48 | | |
| 49 | class ThreadsCreateReq(BaseReq): | |
6988ea0avictor-openai8 months ago | 50 | """Request to create a new thread from a user message.""" |
| 51 | | |
f688d870victor-openai8 months ago | 52 | type: Literal["threads.create"] = "threads.create" |
| 53 | params: ThreadCreateParams | |
| 54 | | |
| 55 | | |
| 56 | class ThreadCreateParams(BaseModel): | |
6988ea0avictor-openai8 months ago | 57 | """User input required to create a thread.""" |
| 58 | | |
f688d870victor-openai8 months ago | 59 | input: UserMessageInput |
| 60 | | |
| 61 | | |
| 62 | class ThreadListParams(BaseModel): | |
6988ea0avictor-openai8 months ago | 63 | """Pagination parameters for listing threads.""" |
| 64 | | |
f688d870victor-openai8 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-openai8 months ago | 71 | """Request to list threads.""" |
| 72 | | |
f688d870victor-openai8 months ago | 73 | type: Literal["threads.list"] = "threads.list" |
| 74 | params: ThreadListParams | |
| 75 | | |
| 76 | | |
| 77 | class ThreadsAddUserMessageReq(BaseReq): | |
6988ea0avictor-openai8 months ago | 78 | """Request to append a user message to a thread.""" |
| 79 | | |
f688d870victor-openai8 months ago | 80 | type: Literal["threads.add_user_message"] = "threads.add_user_message" |
| 81 | params: ThreadAddUserMessageParams | |
| 82 | | |
| 83 | | |
| 84 | class ThreadAddUserMessageParams(BaseModel): | |
6988ea0avictor-openai8 months ago | 85 | """Parameters for adding a user message to a thread.""" |
| 86 | | |
f688d870victor-openai8 months ago | 87 | input: UserMessageInput |
| 88 | thread_id: str | |
| 89 | | |
| 90 | | |
| 91 | class ThreadsAddClientToolOutputReq(BaseReq): | |
6988ea0avictor-openai8 months ago | 92 | """Request to add a client tool's output to a thread.""" |
| 93 | | |
f688d870victor-openai8 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-openai8 months ago | 99 | """Parameters for recording tool output in a thread.""" |
| 100 | | |
f688d870victor-openai8 months ago | 101 | thread_id: str |
| 102 | result: Any | |
| 103 | | |
| 104 | | |
| 105 | class ThreadsCustomActionReq(BaseReq): | |
6988ea0avictor-openai8 months ago | 106 | """Request to execute a custom action within a thread.""" |
| 107 | | |
f688d870victor-openai8 months ago | 108 | type: Literal["threads.custom_action"] = "threads.custom_action" |
| 109 | params: ThreadCustomActionParams | |
| 110 | | |
| 111 | | |
| 112 | class ThreadCustomActionParams(BaseModel): | |
6988ea0avictor-openai8 months ago | 113 | """Parameters describing the custom action to execute.""" |
| 114 | | |
f688d870victor-openai8 months ago | 115 | thread_id: str |
| 116 | item_id: str | None = None | |
| 117 | action: Action[str, Any] | |
| 118 | | |
| 119 | | |
| 120 | class ThreadsRetryAfterItemReq(BaseReq): | |
6988ea0avictor-openai8 months ago | 121 | """Request to retry processing after a specific thread item.""" |
| 122 | | |
f688d870victor-openai8 months ago | 123 | type: Literal["threads.retry_after_item"] = "threads.retry_after_item" |
| 124 | params: ThreadRetryAfterItemParams | |
| 125 | | |
| 126 | | |
| 127 | class ThreadRetryAfterItemParams(BaseModel): | |
6988ea0avictor-openai8 months ago | 128 | """Parameters specifying which item to retry.""" |
| 129 | | |
f688d870victor-openai8 months ago | 130 | thread_id: str |
| 131 | item_id: str | |
| 132 | | |
| 133 | | |
| 134 | class ItemsFeedbackReq(BaseReq): | |
6988ea0avictor-openai8 months ago | 135 | """Request to submit feedback on specific items.""" |
| 136 | | |
f688d870victor-openai8 months ago | 137 | type: Literal["items.feedback"] = "items.feedback" |
| 138 | params: ItemFeedbackParams | |
| 139 | | |
| 140 | | |
| 141 | class ItemFeedbackParams(BaseModel): | |
6988ea0avictor-openai8 months ago | 142 | """Parameters describing feedback targets and sentiment.""" |
| 143 | | |
f688d870victor-openai8 months ago | 144 | thread_id: str |
| 145 | item_ids: list[str] | |
| 146 | kind: FeedbackKind | |
| 147 | | |
| 148 | | |
| 149 | class AttachmentsDeleteReq(BaseReq): | |
6988ea0avictor-openai8 months ago | 150 | """Request to remove an attachment.""" |
| 151 | | |
f688d870victor-openai8 months ago | 152 | type: Literal["attachments.delete"] = "attachments.delete" |
| 153 | params: AttachmentDeleteParams | |
| 154 | | |
| 155 | | |
| 156 | class AttachmentDeleteParams(BaseModel): | |
6988ea0avictor-openai8 months ago | 157 | """Parameters identifying an attachment to delete.""" |
| 158 | | |
f688d870victor-openai8 months ago | 159 | attachment_id: str |
| 160 | | |
| 161 | | |
| 162 | class AttachmentsCreateReq(BaseReq): | |
6988ea0avictor-openai8 months ago | 163 | """Request to register a new attachment.""" |
| 164 | | |
f688d870victor-openai8 months ago | 165 | type: Literal["attachments.create"] = "attachments.create" |
| 166 | params: AttachmentCreateParams | |
| 167 | | |
| 168 | | |
| 169 | class AttachmentCreateParams(BaseModel): | |
6988ea0avictor-openai8 months ago | 170 | """Metadata needed to initialize an attachment.""" |
| 171 | | |
f688d870victor-openai8 months ago | 172 | name: str |
| 173 | size: int | |
| 174 | mime_type: str | |
| 175 | | |
| 176 | | |
| 177 | class ItemsListReq(BaseReq): | |
6988ea0avictor-openai8 months ago | 178 | """Request to list items inside a thread.""" |
| 179 | | |
f688d870victor-openai8 months ago | 180 | type: Literal["items.list"] = "items.list" |
| 181 | params: ItemsListParams | |
| 182 | | |
| 183 | | |
| 184 | class ItemsListParams(BaseModel): | |
6988ea0avictor-openai8 months ago | 185 | """Pagination parameters for listing thread items.""" |
| 186 | | |
f688d870victor-openai8 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-openai8 months ago | 194 | """Request to update thread metadata.""" |
| 195 | | |
f688d870victor-openai8 months ago | 196 | type: Literal["threads.update"] = "threads.update" |
| 197 | params: ThreadUpdateParams | |
| 198 | | |
| 199 | | |
| 200 | class ThreadUpdateParams(BaseModel): | |
6988ea0avictor-openai8 months ago | 201 | """Parameters for updating a thread's properties.""" |
| 202 | | |
f688d870victor-openai8 months ago | 203 | thread_id: str |
| 204 | title: str | |
| 205 | | |
| 206 | | |
| 207 | class ThreadsDeleteReq(BaseReq): | |
6988ea0avictor-openai8 months ago | 208 | """Request to delete a thread.""" |
| 209 | | |
f688d870victor-openai8 months ago | 210 | type: Literal["threads.delete"] = "threads.delete" |
| 211 | params: ThreadDeleteParams | |
| 212 | | |
| 213 | | |
| 214 | class ThreadDeleteParams(BaseModel): | |
6988ea0avictor-openai8 months ago | 215 | """Parameters identifying a thread to delete.""" |
| 216 | | |
f688d870victor-openai8 months ago | 217 | thread_id: str |
| 218 | | |
| 219 | | |
| 220 | StreamingReq = ( | |
| 221 | ThreadsCreateReq | |
| 222 | | ThreadsAddUserMessageReq | |
| 223 | | ThreadsAddClientToolOutputReq | |
| 224 | | ThreadsRetryAfterItemReq | |
| 225 | | ThreadsCustomActionReq | |
| 226 | ) | |
6988ea0avictor-openai8 months ago | 227 | """Union of request types that produce streaming responses.""" |
| 228 | | |
f688d870victor-openai8 months ago | 229 | |
| 230 | NonStreamingReq = ( | |
| 231 | ThreadsGetByIdReq | |
| 232 | | ThreadsListReq | |
| 233 | | ItemsListReq | |
| 234 | | ItemsFeedbackReq | |
| 235 | | AttachmentsCreateReq | |
| 236 | | AttachmentsDeleteReq | |
| 237 | | ThreadsUpdateReq | |
| 238 | | ThreadsDeleteReq | |
| 239 | ) | |
6988ea0avictor-openai8 months ago | 240 | """Union of request types that yield immediate responses.""" |
| 241 | | |
f688d870victor-openai8 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-openai8 months ago | 250 | """Return True if the given request should be processed as streaming.""" |
f688d870victor-openai8 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-openai8 months ago | 267 | """Event emitted when a thread is created.""" |
aa7ec072victor-openai8 months ago | 268 | |
f688d870victor-openai8 months ago | 269 | type: Literal["thread.created"] = "thread.created" |
| 270 | thread: Thread | |
| 271 | | |
| 272 | | |
| 273 | class ThreadUpdatedEvent(BaseModel): | |
6988ea0avictor-openai8 months ago | 274 | """Event emitted when a thread is updated.""" |
aa7ec072victor-openai8 months ago | 275 | |
f688d870victor-openai8 months ago | 276 | type: Literal["thread.updated"] = "thread.updated" |
| 277 | thread: Thread | |
| 278 | | |
| 279 | | |
| 280 | class ThreadItemAddedEvent(BaseModel): | |
6988ea0avictor-openai8 months ago | 281 | """Event emitted when a new item is added to a thread.""" |
aa7ec072victor-openai8 months ago | 282 | |
f688d870victor-openai8 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-openai8 months ago | 288 | """Event describing an update to an existing thread item.""" |
aa7ec072victor-openai8 months ago | 289 | |
f688d870victor-openai8 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-openai8 months ago | 299 | class ThreadItemDoneEvent(BaseModel): |
6988ea0avictor-openai8 months ago | 300 | """Event emitted when a thread item is marked complete.""" |
aa7ec072victor-openai8 months ago | 301 | |
f688d870victor-openai8 months ago | 302 | type: Literal["thread.item.done"] = "thread.item.done" |
| 303 | item: ThreadItem | |
| 304 | | |
| 305 | | |
| 306 | class ThreadItemRemovedEvent(BaseModel): | |
6988ea0avictor-openai8 months ago | 307 | """Event emitted when a thread item is removed.""" |
aa7ec072victor-openai8 months ago | 308 | |
f688d870victor-openai8 months ago | 309 | type: Literal["thread.item.removed"] = "thread.item.removed" |
| 310 | item_id: str | |
| 311 | | |
| 312 | | |
| 313 | class ThreadItemReplacedEvent(BaseModel): | |
6988ea0avictor-openai8 months ago | 314 | """Event emitted when a thread item is replaced.""" |
aa7ec072victor-openai8 months ago | 315 | |
f688d870victor-openai8 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-openai8 months ago | 334 | class ProgressUpdateEvent(BaseModel): |
6988ea0avictor-openai8 months ago | 335 | """Event providing incremental progress from the assistant.""" |
aa7ec072victor-openai8 months ago | 336 | |
f688d870victor-openai8 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-openai8 months ago | 350 | class ErrorEvent(BaseModel): |
6988ea0avictor-openai8 months ago | 351 | """Event indicating an error occurred while processing a thread.""" |
aa7ec072victor-openai8 months ago | 352 | |
f688d870victor-openai8 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-openai8 months ago | 360 | """Event conveying a user-facing notice.""" |
aa7ec072victor-openai8 months ago | 361 | |
f688d870victor-openai8 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-openai8 months ago | 380 | | ProgressUpdateEvent |
a3c67d60Jiwon Kim7 months ago | 381 | | ClientEffectEvent |
f688d870victor-openai8 months ago | 382 | | ErrorEvent |
| 383 | | NoticeEvent, | |
| 384 | Field(discriminator="type"), | |
| 385 | ] | |
6988ea0avictor-openai8 months ago | 386 | """Union of all streaming events emitted to clients.""" |
f688d870victor-openai8 months ago | 387 | |
| 388 | ### THREAD ITEM UPDATE TYPES | |
| 389 | | |
| 390 | | |
| 391 | class AssistantMessageContentPartAdded(BaseModel): | |
6988ea0avictor-openai8 months ago | 392 | """Event emitted when new assistant content is appended.""" |
aa7ec072victor-openai8 months ago | 393 | |
f688d870victor-openai8 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-openai8 months ago | 402 | """Event carrying incremental assistant text output.""" |
aa7ec072victor-openai8 months ago | 403 | |
f688d870victor-openai8 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-openai8 months ago | 412 | """Event announcing a new annotation on assistant content.""" |
aa7ec072victor-openai8 months ago | 413 | |
f688d870victor-openai8 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-openai8 months ago | 423 | """Event indicating an assistant content part is finalized.""" |
aa7ec072victor-openai8 months ago | 424 | |
f688d870victor-openai8 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-openai8 months ago | 433 | """Event streaming widget text deltas.""" |
aa7ec072victor-openai8 months ago | 434 | |
f688d870victor-openai8 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-openai8 months ago | 444 | """Event published when the widget root changes.""" |
aa7ec072victor-openai8 months ago | 445 | |
f688d870victor-openai8 months ago | 446 | type: Literal["widget.root.updated"] = "widget.root.updated" |
| 447 | widget: WidgetRoot | |
| 448 | | |
| 449 | | |
| 450 | class WidgetComponentUpdated(BaseModel): | |
6988ea0avictor-openai8 months ago | 451 | """Event emitted when a widget component updates.""" |
aa7ec072victor-openai8 months ago | 452 | |
f688d870victor-openai8 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-openai8 months ago | 459 | """Event emitted when a workflow task is added.""" |
aa7ec072victor-openai8 months ago | 460 | |
f688d870victor-openai8 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-openai8 months ago | 467 | """Event emitted when a workflow task is updated.""" |
aa7ec072victor-openai8 months ago | 468 | |
f688d870victor-openai8 months ago | 469 | type: Literal["workflow.task.updated"] = "workflow.task.updated" |
| 470 | task_index: int | |
| 471 | task: Task | |
| 472 | | |
| 473 | | |
35ee77f0Jiwon Kim6 months ago | 474 | class GeneratedImageUpdated(BaseModel): |
| 475 | """Event emitted when a generated image is updated.""" | |
| 476 | | |
| 477 | type: Literal["generated_image.updated"] = "generated_image.updated" | |
| 478 | image: GeneratedImage | |
| 479 | progress: float | None = None | |
| 480 | | |
| 481 | | |
f688d870victor-openai8 months ago | 482 | ThreadItemUpdate = ( |
| 483 | AssistantMessageContentPartAdded | |
| 484 | | AssistantMessageContentPartTextDelta | |
| 485 | | AssistantMessageContentPartAnnotationAdded | |
| 486 | | AssistantMessageContentPartDone | |
| 487 | | WidgetStreamingTextValueDelta | |
| 488 | | WidgetComponentUpdated | |
| 489 | | WidgetRootUpdated | |
| 490 | | WorkflowTaskAdded | |
| 491 | | WorkflowTaskUpdated | |
35ee77f0Jiwon Kim6 months ago | 492 | | GeneratedImageUpdated |
f688d870victor-openai8 months ago | 493 | ) |
6988ea0avictor-openai8 months ago | 494 | """Union of possible updates applied to thread items.""" |
f688d870victor-openai8 months ago | 495 | |
| 496 | | |
| 497 | ### THREAD TYPES | |
| 498 | | |
| 499 | | |
| 500 | class ThreadMetadata(BaseModel): | |
6988ea0avictor-openai8 months ago | 501 | """Metadata describing a thread without its items.""" |
aa7ec072victor-openai8 months ago | 502 | |
f688d870victor-openai8 months ago | 503 | title: str | None = None |
| 504 | id: str | |
| 505 | created_at: datetime | |
| 506 | status: ThreadStatus = Field(default_factory=lambda: ActiveStatus()) | |
| 507 | # TODO - make not client rendered | |
| 508 | metadata: dict[str, Any] = Field(default_factory=dict) | |
| 509 | | |
| 510 | | |
| 511 | class ActiveStatus(BaseModel): | |
6988ea0avictor-openai8 months ago | 512 | """Status indicating the thread is active.""" |
aa7ec072victor-openai8 months ago | 513 | |
f688d870victor-openai8 months ago | 514 | type: Literal["active"] = Field(default="active", frozen=True) |
| 515 | | |
| 516 | | |
| 517 | class LockedStatus(BaseModel): | |
6988ea0avictor-openai8 months ago | 518 | """Status indicating the thread is locked.""" |
aa7ec072victor-openai8 months ago | 519 | |
f688d870victor-openai8 months ago | 520 | type: Literal["locked"] = Field(default="locked", frozen=True) |
| 521 | reason: str | None = None | |
| 522 | | |
| 523 | | |
| 524 | class ClosedStatus(BaseModel): | |
6988ea0avictor-openai8 months ago | 525 | """Status indicating the thread is closed.""" |
aa7ec072victor-openai8 months ago | 526 | |
f688d870victor-openai8 months ago | 527 | type: Literal["closed"] = Field(default="closed", frozen=True) |
| 528 | reason: str | None = None | |
| 529 | | |
| 530 | | |
| 531 | ThreadStatus = Annotated[ | |
| 532 | ActiveStatus | LockedStatus | ClosedStatus, | |
| 533 | Field(discriminator="type"), | |
| 534 | ] | |
6988ea0avictor-openai8 months ago | 535 | """Union of lifecycle states for a thread.""" |
f688d870victor-openai8 months ago | 536 | |
| 537 | | |
| 538 | class Thread(ThreadMetadata): | |
6988ea0avictor-openai8 months ago | 539 | """Thread with its paginated items.""" |
aa7ec072victor-openai8 months ago | 540 | |
f688d870victor-openai8 months ago | 541 | items: Page[ThreadItem] |
| 542 | | |
| 543 | | |
| 544 | ### THREAD ITEM TYPES | |
| 545 | | |
| 546 | | |
| 547 | class ThreadItemBase(BaseModel): | |
6988ea0avictor-openai8 months ago | 548 | """Base fields shared by all thread items.""" |
aa7ec072victor-openai8 months ago | 549 | |
f688d870victor-openai8 months ago | 550 | id: str |
| 551 | thread_id: str | |
| 552 | created_at: datetime | |
| 553 | | |
| 554 | | |
| 555 | class UserMessageItem(ThreadItemBase): | |
6988ea0avictor-openai8 months ago | 556 | """Thread item representing a user message.""" |
aa7ec072victor-openai8 months ago | 557 | |
f688d870victor-openai8 months ago | 558 | type: Literal["user_message"] = "user_message" |
| 559 | content: list[UserMessageContent] | |
| 560 | attachments: list[Attachment] = Field(default_factory=list) | |
| 561 | quoted_text: str | None = None | |
| 562 | inference_options: InferenceOptions | |
| 563 | | |
| 564 | | |
| 565 | class AssistantMessageItem(ThreadItemBase): | |
6988ea0avictor-openai8 months ago | 566 | """Thread item representing an assistant message.""" |
aa7ec072victor-openai8 months ago | 567 | |
f688d870victor-openai8 months ago | 568 | type: Literal["assistant_message"] = "assistant_message" |
| 569 | content: list[AssistantMessageContent] | |
| 570 | | |
| 571 | | |
| 572 | class ClientToolCallItem(ThreadItemBase): | |
6988ea0avictor-openai8 months ago | 573 | """Thread item capturing a client tool call.""" |
aa7ec072victor-openai8 months ago | 574 | |
f688d870victor-openai8 months ago | 575 | type: Literal["client_tool_call"] = "client_tool_call" |
| 576 | status: Literal["pending", "completed"] = "pending" | |
| 577 | call_id: str | |
| 578 | name: str | |
| 579 | arguments: dict[str, Any] | |
| 580 | output: Any | None = None | |
| 581 | | |
| 582 | | |
| 583 | class WidgetItem(ThreadItemBase): | |
6988ea0avictor-openai8 months ago | 584 | """Thread item containing widget content.""" |
aa7ec072victor-openai8 months ago | 585 | |
f688d870victor-openai8 months ago | 586 | type: Literal["widget"] = "widget" |
| 587 | widget: WidgetRoot | |
| 588 | copy_text: str | None = None | |
| 589 | | |
| 590 | | |
35ee77f0Jiwon Kim6 months ago | 591 | class GeneratedImage(BaseModel): |
| 592 | """Generated image.""" | |
| 593 | | |
| 594 | id: str | |
| 595 | url: str | |
| 596 | | |
| 597 | | |
| 598 | class GeneratedImageItem(ThreadItemBase): | |
| 599 | """Thread item containing a generated image.""" | |
| 600 | | |
| 601 | type: Literal["generated_image"] = "generated_image" | |
| 602 | image: GeneratedImage | None = None | |
| 603 | | |
| 604 | | |
f688d870victor-openai8 months ago | 605 | class TaskItem(ThreadItemBase): |
6988ea0avictor-openai8 months ago | 606 | """Thread item containing a task.""" |
aa7ec072victor-openai8 months ago | 607 | |
f688d870victor-openai8 months ago | 608 | type: Literal["task"] = "task" |
| 609 | task: Task | |
| 610 | | |
| 611 | | |
| 612 | class WorkflowItem(ThreadItemBase): | |
6988ea0avictor-openai8 months ago | 613 | """Thread item representing a workflow.""" |
aa7ec072victor-openai8 months ago | 614 | |
f688d870victor-openai8 months ago | 615 | type: Literal["workflow"] = "workflow" |
| 616 | workflow: Workflow | |
| 617 | | |
| 618 | | |
| 619 | class EndOfTurnItem(ThreadItemBase): | |
6988ea0avictor-openai8 months ago | 620 | """Marker item indicating the assistant ends its turn.""" |
aa7ec072victor-openai8 months ago | 621 | |
f688d870victor-openai8 months ago | 622 | type: Literal["end_of_turn"] = "end_of_turn" |
| 623 | | |
| 624 | | |
| 625 | class HiddenContextItem(ThreadItemBase): | |
5a7244baJiwon Kim7 months ago | 626 | """ |
| 627 | HiddenContext is never sent to the client. It's not officially part of ChatKit.js. | |
| 628 | It is only used internally to store additional context in a specific place in the thread. | |
| 629 | """ | |
f688d870victor-openai8 months ago | 630 | |
| 631 | type: Literal["hidden_context_item"] = "hidden_context_item" | |
| 632 | content: Any | |
| 633 | | |
| 634 | | |
5a7244baJiwon Kim7 months ago | 635 | class SDKHiddenContextItem(ThreadItemBase): |
| 636 | """ | |
| 637 | Hidden context that is used by the ChatKit Python SDK for storing additional context | |
| 638 | for internal operations. | |
| 639 | """ | |
| 640 | | |
| 641 | type: Literal["sdk_hidden_context"] = "sdk_hidden_context" | |
| 642 | content: str | |
| 643 | | |
| 644 | | |
f688d870victor-openai8 months ago | 645 | ThreadItem = Annotated[ |
| 646 | UserMessageItem | |
| 647 | | AssistantMessageItem | |
| 648 | | ClientToolCallItem | |
| 649 | | WidgetItem | |
35ee77f0Jiwon Kim6 months ago | 650 | | GeneratedImageItem |
f688d870victor-openai8 months ago | 651 | | WorkflowItem |
| 652 | | TaskItem | |
| 653 | | HiddenContextItem | |
5a7244baJiwon Kim7 months ago | 654 | | SDKHiddenContextItem |
f688d870victor-openai8 months ago | 655 | | EndOfTurnItem, |
| 656 | Field(discriminator="type"), | |
| 657 | ] | |
6988ea0avictor-openai8 months ago | 658 | """Union of all thread item variants.""" |
f688d870victor-openai8 months ago | 659 | |
| 660 | | |
| 661 | ### ASSISTANT MESSAGE TYPES | |
| 662 | | |
| 663 | | |
| 664 | class AssistantMessageContent(BaseModel): | |
6988ea0avictor-openai8 months ago | 665 | """Assistant message content consisting of text and annotations.""" |
aa7ec072victor-openai8 months ago | 666 | |
f688d870victor-openai8 months ago | 667 | annotations: list[Annotation] = Field(default_factory=list) |
| 668 | text: str | |
| 669 | type: Literal["output_text"] = "output_text" | |
| 670 | | |
| 671 | | |
| 672 | class Annotation(BaseModel): | |
6988ea0avictor-openai8 months ago | 673 | """Reference to supporting context attached to assistant output.""" |
aa7ec072victor-openai8 months ago | 674 | |
f688d870victor-openai8 months ago | 675 | type: Literal["annotation"] = "annotation" |
| 676 | source: URLSource | FileSource | EntitySource | |
| 677 | index: int | None = None | |
| 678 | | |
| 679 | | |
| 680 | ### USER MESSAGE TYPES | |
| 681 | | |
| 682 | | |
| 683 | class UserMessageInput(BaseModel): | |
6988ea0avictor-openai8 months ago | 684 | """Payload describing a user message submission.""" |
aa7ec072victor-openai8 months ago | 685 | |
f688d870victor-openai8 months ago | 686 | content: list[UserMessageContent] |
| 687 | attachments: list[str] | |
| 688 | quoted_text: str | None = None | |
| 689 | inference_options: InferenceOptions | |
| 690 | | |
| 691 | | |
| 692 | class UserMessageTextContent(BaseModel): | |
6988ea0avictor-openai8 months ago | 693 | """User message content containing plaintext.""" |
aa7ec072victor-openai8 months ago | 694 | |
f688d870victor-openai8 months ago | 695 | type: Literal["input_text"] = "input_text" |
| 696 | text: str | |
| 697 | | |
| 698 | | |
| 699 | class UserMessageTagContent(BaseModel): | |
6988ea0avictor-openai8 months ago | 700 | """User message content representing an interactive tag.""" |
aa7ec072victor-openai8 months ago | 701 | |
f688d870victor-openai8 months ago | 702 | type: Literal["input_tag"] = "input_tag" |
| 703 | id: str | |
| 704 | text: str | |
| 705 | data: dict[str, Any] | |
ec580cbaJiwon Kim7 months ago | 706 | group: str | None = None |
f688d870victor-openai8 months ago | 707 | interactive: bool = False |
| 708 | | |
| 709 | | |
| 710 | UserMessageContent = Annotated[ | |
| 711 | UserMessageTextContent | UserMessageTagContent, Field(discriminator="type") | |
| 712 | ] | |
6988ea0avictor-openai8 months ago | 713 | """Union of allowed user message content payloads.""" |
f688d870victor-openai8 months ago | 714 | |
| 715 | | |
| 716 | class InferenceOptions(BaseModel): | |
6988ea0avictor-openai8 months ago | 717 | """Model and tool configuration for message processing.""" |
aa7ec072victor-openai8 months ago | 718 | |
f688d870victor-openai8 months ago | 719 | tool_choice: ToolChoice | None = None |
| 720 | model: str | None = None | |
| 721 | | |
| 722 | | |
| 723 | class ToolChoice(BaseModel): | |
6988ea0avictor-openai8 months ago | 724 | """Explicit tool selection for the assistant to invoke.""" |
aa7ec072victor-openai8 months ago | 725 | |
f688d870victor-openai8 months ago | 726 | id: str |
| 727 | | |
| 728 | | |
ad273215Jiwon Kim6 months ago | 729 | class AttachmentUploadDescriptor(BaseModel): |
| 730 | """Two-phase upload instructions.""" | |
| 731 | | |
| 732 | url: AnyUrl | |
24b53f98Jiwon Kim5 months ago | 733 | method: Literal["POST", "PUT"] |
ad273215Jiwon Kim6 months ago | 734 | """The HTTP method to use when uploading the file for two-phase upload.""" |
| 735 | headers: dict[str, str] = Field(default_factory=dict) | |
| 736 | """Optional headers to include in the upload request.""" | |
| 737 | | |
| 738 | | |
f688d870victor-openai8 months ago | 739 | class AttachmentBase(BaseModel): |
6988ea0avictor-openai8 months ago | 740 | """Base metadata shared by all attachments.""" |
aa7ec072victor-openai8 months ago | 741 | |
f688d870victor-openai8 months ago | 742 | id: str |
| 743 | name: str | |
| 744 | mime_type: str | |
ad273215Jiwon Kim6 months ago | 745 | upload_descriptor: AttachmentUploadDescriptor | None = None |
f688d870victor-openai8 months ago | 746 | """ |
ad273215Jiwon Kim6 months ago | 747 | Two-phase upload instructions. |
| 748 | Should be set to None after upload is complete or when using direct upload | |
| 749 | where uploading happens when creating the attachment object. | |
f688d870victor-openai8 months ago | 750 | """ |
| 751 | | |
| 752 | | |
| 753 | class FileAttachment(AttachmentBase): | |
6988ea0avictor-openai8 months ago | 754 | """Attachment representing a generic file.""" |
aa7ec072victor-openai8 months ago | 755 | |
f688d870victor-openai8 months ago | 756 | type: Literal["file"] = "file" |
| 757 | | |
| 758 | | |
| 759 | class ImageAttachment(AttachmentBase): | |
6988ea0avictor-openai8 months ago | 760 | """Attachment representing an image resource.""" |
aa7ec072victor-openai8 months ago | 761 | |
f688d870victor-openai8 months ago | 762 | type: Literal["image"] = "image" |
| 763 | preview_url: AnyUrl | |
| 764 | | |
| 765 | | |
| 766 | Attachment = Annotated[ | |
| 767 | FileAttachment | ImageAttachment, | |
| 768 | Field(discriminator="type"), | |
| 769 | ] | |
6988ea0avictor-openai8 months ago | 770 | """Union of supported attachment types.""" |
f688d870victor-openai8 months ago | 771 | |
| 772 | | |
| 773 | ### WORKFLOW TYPES | |
| 774 | | |
| 775 | | |
| 776 | class Workflow(BaseModel): | |
6988ea0avictor-openai8 months ago | 777 | """Workflow attached to a thread with optional summary.""" |
aa7ec072victor-openai8 months ago | 778 | |
f688d870victor-openai8 months ago | 779 | type: Literal["custom", "reasoning"] |
| 780 | tasks: list[Task] | |
| 781 | summary: WorkflowSummary | None = None | |
| 782 | expanded: bool = False | |
| 783 | | |
| 784 | | |
| 785 | class CustomSummary(BaseModel): | |
6988ea0avictor-openai8 months ago | 786 | """Custom summary for a workflow.""" |
aa7ec072victor-openai8 months ago | 787 | |
f688d870victor-openai8 months ago | 788 | title: str |
c9f5f09dJiwon Kim8 months ago | 789 | icon: IconName | None = None |
f688d870victor-openai8 months ago | 790 | |
| 791 | | |
| 792 | class DurationSummary(BaseModel): | |
6988ea0avictor-openai8 months ago | 793 | """Summary providing total workflow duration.""" |
aa7ec072victor-openai8 months ago | 794 | |
f688d870victor-openai8 months ago | 795 | duration: int |
| 796 | """The duration of the workflow in seconds""" | |
| 797 | | |
| 798 | | |
| 799 | WorkflowSummary = CustomSummary | DurationSummary | |
6988ea0avictor-openai8 months ago | 800 | """Summary variants available for workflows.""" |
f688d870victor-openai8 months ago | 801 | |
| 802 | ### TASK TYPES | |
| 803 | | |
| 804 | | |
| 805 | class BaseTask(BaseModel): | |
6988ea0avictor-openai8 months ago | 806 | """Base fields common to all workflow tasks.""" |
aa7ec072victor-openai8 months ago | 807 | |
f688d870victor-openai8 months ago | 808 | status_indicator: Literal["none", "loading", "complete"] = "none" |
| 809 | """Only used when rendering the task as part of a workflow. Indicates the status of the task.""" | |
| 810 | | |
| 811 | | |
| 812 | class CustomTask(BaseTask): | |
6988ea0avictor-openai8 months ago | 813 | """Workflow task displaying custom content.""" |
aa7ec072victor-openai8 months ago | 814 | |
f688d870victor-openai8 months ago | 815 | type: Literal["custom"] = "custom" |
| 816 | title: str | None = None | |
c9f5f09dJiwon Kim8 months ago | 817 | icon: IconName | None = None |
f688d870victor-openai8 months ago | 818 | content: str | None = None |
| 819 | | |
| 820 | | |
| 821 | class SearchTask(BaseTask): | |
6988ea0avictor-openai8 months ago | 822 | """Workflow task representing a web search.""" |
aa7ec072victor-openai8 months ago | 823 | |
f688d870victor-openai8 months ago | 824 | type: Literal["web_search"] = "web_search" |
| 825 | title: str | None = None | |
| 826 | title_query: str | None = None | |
| 827 | queries: list[str] = Field(default_factory=list) | |
| 828 | sources: list[URLSource] = Field(default_factory=list) | |
| 829 | | |
| 830 | | |
| 831 | class ThoughtTask(BaseTask): | |
6988ea0avictor-openai8 months ago | 832 | """Workflow task capturing assistant reasoning.""" |
aa7ec072victor-openai8 months ago | 833 | |
f688d870victor-openai8 months ago | 834 | type: Literal["thought"] = "thought" |
| 835 | title: str | None = None | |
| 836 | content: str | |
| 837 | | |
| 838 | | |
| 839 | class FileTask(BaseTask): | |
6988ea0avictor-openai8 months ago | 840 | """Workflow task referencing file sources.""" |
aa7ec072victor-openai8 months ago | 841 | |
f688d870victor-openai8 months ago | 842 | type: Literal["file"] = "file" |
| 843 | title: str | None = None | |
| 844 | sources: list[FileSource] = Field(default_factory=list) | |
| 845 | | |
| 846 | | |
| 847 | class ImageTask(BaseTask): | |
6988ea0avictor-openai8 months ago | 848 | """Workflow task rendering image content.""" |
aa7ec072victor-openai8 months ago | 849 | |
f688d870victor-openai8 months ago | 850 | type: Literal["image"] = "image" |
| 851 | title: str | None = None | |
| 852 | | |
| 853 | | |
| 854 | Task = Annotated[ | |
| 855 | CustomTask | SearchTask | ThoughtTask | FileTask | ImageTask, | |
| 856 | Field(discriminator="type"), | |
| 857 | ] | |
6988ea0avictor-openai8 months ago | 858 | """Union of workflow task variants.""" |
f688d870victor-openai8 months ago | 859 | |
| 860 | | |
| 861 | ### SOURCE TYPES | |
| 862 | | |
| 863 | | |
| 864 | class SourceBase(BaseModel): | |
6988ea0avictor-openai8 months ago | 865 | """Base class for sources displayed to users.""" |
aa7ec072victor-openai8 months ago | 866 | |
f688d870victor-openai8 months ago | 867 | title: str |
| 868 | description: str | None = None | |
| 869 | timestamp: str | None = None | |
| 870 | group: str | None = None | |
| 871 | | |
| 872 | | |
| 873 | class FileSource(SourceBase): | |
6988ea0avictor-openai8 months ago | 874 | """Source metadata for file-based references.""" |
aa7ec072victor-openai8 months ago | 875 | |
f688d870victor-openai8 months ago | 876 | type: Literal["file"] = "file" |
| 877 | filename: str | |
| 878 | | |
| 879 | | |
| 880 | class URLSource(SourceBase): | |
6988ea0avictor-openai8 months ago | 881 | """Source metadata for external URLs.""" |
aa7ec072victor-openai8 months ago | 882 | |
f688d870victor-openai8 months ago | 883 | type: Literal["url"] = "url" |
| 884 | url: str | |
| 885 | attribution: str | None = None | |
| 886 | | |
| 887 | | |
| 888 | class EntitySource(SourceBase): | |
6988ea0avictor-openai8 months ago | 889 | """Source metadata for entity references.""" |
aa7ec072victor-openai8 months ago | 890 | |
f688d870victor-openai8 months ago | 891 | type: Literal["entity"] = "entity" |
| 892 | id: str | |
c9f5f09dJiwon Kim8 months ago | 893 | icon: IconName | None = None |
002dd9feJiwon Kim6 months ago | 894 | label: str | None = None |
| 895 | """Optional label shown with the icon in the default entity hover header | |
| 896 | when no preview callback is provided. | |
| 897 | """ | |
6161a280Jiwon Kim6 months ago | 898 | inline_label: str | None = None |
| 899 | """Optional label for the inline annotation view. When not provided, the icon is used instead.""" | |
67064fb2Jiwon Kim6 months ago | 900 | interactive: bool = False |
| 901 | """Per-entity toggle to wire client callbacks and render this entity as interactive.""" | |
c47e7701jiwon-oai8 months ago | 902 | data: dict[str, Any] = Field(default_factory=dict) |
002dd9feJiwon Kim6 months ago | 903 | """Additional data for the entity source that is passed to client entity callbacks.""" |
f688d870victor-openai8 months ago | 904 | |
ec580cbaJiwon Kim7 months ago | 905 | preview: Literal["lazy"] | None = Field( |
| 906 | default=None, | |
| 907 | deprecated=True, | |
| 908 | description="This field is ignored. Please use the entities.onRequestPreview ChatKit.js option instead.", | |
| 909 | ) | |
| 910 | | |
f688d870victor-openai8 months ago | 911 | |
6988ea0avictor-openai8 months ago | 912 | Source = Annotated[ |
| 913 | URLSource | FileSource | EntitySource, | |
| 914 | Field(discriminator="type"), | |
| 915 | ] | |
| 916 | """Union of supported source types.""" | |
f688d870victor-openai8 months ago | 917 | |
| 918 | | |
| 919 | ### MISC TYPES | |
| 920 | | |
| 921 | | |
| 922 | FeedbackKind = Literal["positive", "negative"] | |
6988ea0avictor-openai8 months ago | 923 | """Literal type for feedback sentiment.""" |