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