openai/chatkit-python
Publicmirrored fromhttps://github.com/openai/chatkit-pythonAvailable
chatkit/store.py
141lines · modecode
| 1 | import uuid |
| 2 | from abc import ABC, abstractmethod |
| 3 | from typing import Any, Generic, Literal |
| 4 | |
| 5 | from typing_extensions import TypeVar |
| 6 | |
| 7 | from .types import ( |
| 8 | Attachment, |
| 9 | AttachmentCreateParams, |
| 10 | Page, |
| 11 | ThreadItem, |
| 12 | ThreadMetadata, |
| 13 | ) |
| 14 | |
| 15 | TContext = TypeVar("TContext", default=Any) |
| 16 | |
| 17 | StoreItemType = Literal[ |
| 18 | "thread", "message", "tool_call", "task", "workflow", "attachment" |
| 19 | ] |
| 20 | |
| 21 | |
| 22 | _ID_PREFIXES: dict[StoreItemType, str] = { |
| 23 | "thread": "thr", |
| 24 | "message": "msg", |
| 25 | "tool_call": "tc", |
| 26 | "workflow": "wf", |
| 27 | "task": "tsk", |
| 28 | "attachment": "atc", |
| 29 | } |
| 30 | |
| 31 | |
| 32 | def default_generate_id(item_type: StoreItemType) -> str: |
| 33 | prefix = _ID_PREFIXES[item_type] |
| 34 | return f"{prefix}_{uuid.uuid4().hex[:8]}" |
| 35 | |
| 36 | |
| 37 | class NotFoundError(Exception): |
| 38 | pass |
| 39 | |
| 40 | |
| 41 | class AttachmentStore(ABC, Generic[TContext]): |
| 42 | @abstractmethod |
| 43 | async def delete_attachment(self, attachment_id: str, context: TContext) -> None: |
| 44 | pass |
| 45 | |
| 46 | async def create_attachment( |
| 47 | self, input: AttachmentCreateParams, context: TContext |
| 48 | ) -> Attachment: |
| 49 | raise NotImplementedError( |
| 50 | f"{type(self).__name__} must override create_attachment() to support two-phase file upload" |
| 51 | ) |
| 52 | |
| 53 | def generate_attachment_id(self, mime_type: str, context: TContext) -> str: |
| 54 | """Return a new identifier for a file. Override this method to customize file ID generation.""" |
| 55 | |
| 56 | return default_generate_id("attachment") |
| 57 | |
| 58 | |
| 59 | class Store(ABC, Generic[TContext]): |
| 60 | def generate_thread_id(self, context: TContext) -> str: |
| 61 | """Return a new identifier for a thread. Override this method to customize thread ID generation.""" |
| 62 | |
| 63 | return default_generate_id("thread") |
| 64 | |
| 65 | def generate_item_id( |
| 66 | self, item_type: StoreItemType, thread: ThreadMetadata, context: TContext |
| 67 | ) -> str: |
| 68 | """Return a new identifier for a thread item. Override this method to customize item ID generation.""" |
| 69 | |
| 70 | return default_generate_id(item_type) |
| 71 | |
| 72 | @abstractmethod |
| 73 | async def load_thread(self, thread_id: str, context: TContext) -> ThreadMetadata: |
| 74 | pass |
| 75 | |
| 76 | @abstractmethod |
| 77 | async def save_thread(self, thread: ThreadMetadata, context: TContext) -> None: |
| 78 | pass |
| 79 | |
| 80 | @abstractmethod |
| 81 | async def load_thread_items( |
| 82 | self, |
| 83 | thread_id: str, |
| 84 | after: str | None, |
| 85 | limit: int, |
| 86 | order: str, |
| 87 | context: TContext, |
| 88 | ) -> Page[ThreadItem]: |
| 89 | pass |
| 90 | |
| 91 | @abstractmethod |
| 92 | async def save_attachment(self, attachment: Attachment, context: TContext) -> None: |
| 93 | pass |
| 94 | |
| 95 | @abstractmethod |
| 96 | async def load_attachment( |
| 97 | self, attachment_id: str, context: TContext |
| 98 | ) -> Attachment: |
| 99 | pass |
| 100 | |
| 101 | @abstractmethod |
| 102 | async def delete_attachment(self, attachment_id: str, context: TContext) -> None: |
| 103 | pass |
| 104 | |
| 105 | @abstractmethod |
| 106 | async def load_threads( |
| 107 | self, |
| 108 | limit: int, |
| 109 | after: str | None, |
| 110 | order: str, |
| 111 | context: TContext, |
| 112 | ) -> Page[ThreadMetadata]: |
| 113 | pass |
| 114 | |
| 115 | @abstractmethod |
| 116 | async def add_thread_item( |
| 117 | self, thread_id: str, item: ThreadItem, context: TContext |
| 118 | ) -> None: |
| 119 | pass |
| 120 | |
| 121 | @abstractmethod |
| 122 | async def save_item( |
| 123 | self, thread_id: str, item: ThreadItem, context: TContext |
| 124 | ) -> None: |
| 125 | pass |
| 126 | |
| 127 | @abstractmethod |
| 128 | async def load_item( |
| 129 | self, thread_id: str, item_id: str, context: TContext |
| 130 | ) -> ThreadItem: |
| 131 | pass |
| 132 | |
| 133 | @abstractmethod |
| 134 | async def delete_thread(self, thread_id: str, context: TContext) -> None: |
| 135 | pass |
| 136 | |
| 137 | @abstractmethod |
| 138 | async def delete_thread_item( |
| 139 | self, thread_id: str, item_id: str, context: TContext |
| 140 | ) -> None: |
| 141 | pass |
| 142 | |