openai/chatkit-python
Publicmirrored from https://github.com/openai/chatkit-pythonAvailable
chatkit/store.py
162lines · modeblame
f688d870victor-openai8 months ago | 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[ | |
5a7244baJiwon Kim7 months ago | 18 | "thread", |
| 19 | "message", | |
| 20 | "tool_call", | |
| 21 | "task", | |
| 22 | "workflow", | |
| 23 | "attachment", | |
| 24 | "sdk_hidden_context", | |
f688d870victor-openai8 months ago | 25 | ] |
| 26 | | |
| 27 | | |
| 28 | _ID_PREFIXES: dict[StoreItemType, str] = { | |
| 29 | "thread": "thr", | |
| 30 | "message": "msg", | |
| 31 | "tool_call": "tc", | |
| 32 | "workflow": "wf", | |
| 33 | "task": "tsk", | |
| 34 | "attachment": "atc", | |
5a7244baJiwon Kim7 months ago | 35 | "sdk_hidden_context": "shcx", |
f688d870victor-openai8 months ago | 36 | } |
| 37 | | |
| 38 | | |
| 39 | def default_generate_id(item_type: StoreItemType) -> str: | |
| 40 | prefix = _ID_PREFIXES[item_type] | |
| 41 | return f"{prefix}_{uuid.uuid4().hex[:8]}" | |
| 42 | | |
| 43 | | |
| 44 | class NotFoundError(Exception): | |
| 45 | pass | |
| 46 | | |
| 47 | | |
| 48 | class AttachmentStore(ABC, Generic[TContext]): | |
| 49 | @abstractmethod | |
| 50 | async def delete_attachment(self, attachment_id: str, context: TContext) -> None: | |
3b20c3d1Jiwon Kim6 months ago | 51 | """Delete an attachment by id.""" |
f688d870victor-openai8 months ago | 52 | pass |
| 53 | | |
| 54 | async def create_attachment( | |
| 55 | self, input: AttachmentCreateParams, context: TContext | |
| 56 | ) -> Attachment: | |
3b20c3d1Jiwon Kim6 months ago | 57 | """Create an attachment record from upload metadata.""" |
f688d870victor-openai8 months ago | 58 | raise NotImplementedError( |
| 59 | f"{type(self).__name__} must override create_attachment() to support two-phase file upload" | |
| 60 | ) | |
| 61 | | |
| 62 | def generate_attachment_id(self, mime_type: str, context: TContext) -> str: | |
| 63 | """Return a new identifier for a file. Override this method to customize file ID generation.""" | |
| 64 | | |
| 65 | return default_generate_id("attachment") | |
| 66 | | |
| 67 | | |
| 68 | class Store(ABC, Generic[TContext]): | |
| 69 | def generate_thread_id(self, context: TContext) -> str: | |
| 70 | """Return a new identifier for a thread. Override this method to customize thread ID generation.""" | |
| 71 | | |
| 72 | return default_generate_id("thread") | |
| 73 | | |
| 74 | def generate_item_id( | |
| 75 | self, item_type: StoreItemType, thread: ThreadMetadata, context: TContext | |
| 76 | ) -> str: | |
| 77 | """Return a new identifier for a thread item. Override this method to customize item ID generation.""" | |
| 78 | | |
| 79 | return default_generate_id(item_type) | |
| 80 | | |
| 81 | @abstractmethod | |
| 82 | async def load_thread(self, thread_id: str, context: TContext) -> ThreadMetadata: | |
3b20c3d1Jiwon Kim6 months ago | 83 | """Load a thread's metadata by id.""" |
f688d870victor-openai8 months ago | 84 | pass |
| 85 | | |
| 86 | @abstractmethod | |
| 87 | async def save_thread(self, thread: ThreadMetadata, context: TContext) -> None: | |
3b20c3d1Jiwon Kim6 months ago | 88 | """Persist thread metadata (title, status, etc.).""" |
f688d870victor-openai8 months ago | 89 | pass |
| 90 | | |
| 91 | @abstractmethod | |
| 92 | async def load_thread_items( | |
| 93 | self, | |
| 94 | thread_id: str, | |
| 95 | after: str | None, | |
| 96 | limit: int, | |
| 97 | order: str, | |
| 98 | context: TContext, | |
| 99 | ) -> Page[ThreadItem]: | |
3b20c3d1Jiwon Kim6 months ago | 100 | """Load a page of thread items with pagination controls.""" |
f688d870victor-openai8 months ago | 101 | pass |
| 102 | | |
| 103 | @abstractmethod | |
| 104 | async def save_attachment(self, attachment: Attachment, context: TContext) -> None: | |
3b20c3d1Jiwon Kim6 months ago | 105 | """Persist attachment metadata.""" |
f688d870victor-openai8 months ago | 106 | pass |
| 107 | | |
| 108 | @abstractmethod | |
| 109 | async def load_attachment( | |
| 110 | self, attachment_id: str, context: TContext | |
| 111 | ) -> Attachment: | |
3b20c3d1Jiwon Kim6 months ago | 112 | """Load attachment metadata by id.""" |
f688d870victor-openai8 months ago | 113 | pass |
| 114 | | |
| 115 | @abstractmethod | |
| 116 | async def delete_attachment(self, attachment_id: str, context: TContext) -> None: | |
3b20c3d1Jiwon Kim6 months ago | 117 | """Delete attachment metadata by id.""" |
f688d870victor-openai8 months ago | 118 | pass |
| 119 | | |
| 120 | @abstractmethod | |
| 121 | async def load_threads( | |
| 122 | self, | |
| 123 | limit: int, | |
| 124 | after: str | None, | |
| 125 | order: str, | |
| 126 | context: TContext, | |
| 127 | ) -> Page[ThreadMetadata]: | |
3b20c3d1Jiwon Kim6 months ago | 128 | """Load a page of threads with pagination controls.""" |
f688d870victor-openai8 months ago | 129 | pass |
| 130 | | |
| 131 | @abstractmethod | |
| 132 | async def add_thread_item( | |
| 133 | self, thread_id: str, item: ThreadItem, context: TContext | |
| 134 | ) -> None: | |
3b20c3d1Jiwon Kim6 months ago | 135 | """Persist a newly created thread item.""" |
f688d870victor-openai8 months ago | 136 | pass |
| 137 | | |
| 138 | @abstractmethod | |
| 139 | async def save_item( | |
| 140 | self, thread_id: str, item: ThreadItem, context: TContext | |
| 141 | ) -> None: | |
3b20c3d1Jiwon Kim6 months ago | 142 | """Upsert a thread item by id.""" |
f688d870victor-openai8 months ago | 143 | pass |
| 144 | | |
| 145 | @abstractmethod | |
| 146 | async def load_item( | |
| 147 | self, thread_id: str, item_id: str, context: TContext | |
| 148 | ) -> ThreadItem: | |
3b20c3d1Jiwon Kim6 months ago | 149 | """Load a thread item by id.""" |
f688d870victor-openai8 months ago | 150 | pass |
| 151 | | |
| 152 | @abstractmethod | |
| 153 | async def delete_thread(self, thread_id: str, context: TContext) -> None: | |
3b20c3d1Jiwon Kim6 months ago | 154 | """Delete a thread and its items.""" |
f688d870victor-openai8 months ago | 155 | pass |
| 156 | | |
| 157 | @abstractmethod | |
| 158 | async def delete_thread_item( | |
| 159 | self, thread_id: str, item_id: str, context: TContext | |
| 160 | ) -> None: | |
3b20c3d1Jiwon Kim6 months ago | 161 | """Delete a thread item by id.""" |
f688d870victor-openai8 months ago | 162 | pass |