openai/chatkit-python

Public

mirrored from https://github.com/openai/chatkit-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1860286bf7fca86e6b70e7e87ef4681a0f482145

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

chatkit/store.py

148lines · modecode

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