openai/chatkit-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.5.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

chatkit/store.py

162lines · modeblame

f688d870victor-openai8 months ago1import uuid
2from abc import ABC, abstractmethod
3from typing import Any, Generic, Literal
4
5from typing_extensions import TypeVar
6
7from .types import (
8Attachment,
9AttachmentCreateParams,
10Page,
11ThreadItem,
12ThreadMetadata,
13)
14
15TContext = TypeVar("TContext", default=Any)
16
17StoreItemType = Literal[
5a7244baJiwon Kim7 months ago18"thread",
19"message",
20"tool_call",
21"task",
22"workflow",
23"attachment",
24"sdk_hidden_context",
f688d870victor-openai8 months ago25]
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 ago35"sdk_hidden_context": "shcx",
f688d870victor-openai8 months ago36}
37
38
39def default_generate_id(item_type: StoreItemType) -> str:
40prefix = _ID_PREFIXES[item_type]
41return f"{prefix}_{uuid.uuid4().hex[:8]}"
42
43
44class NotFoundError(Exception):
45pass
46
47
48class AttachmentStore(ABC, Generic[TContext]):
49@abstractmethod
50async def delete_attachment(self, attachment_id: str, context: TContext) -> None:
3b20c3d1Jiwon Kim6 months ago51"""Delete an attachment by id."""
f688d870victor-openai8 months ago52pass
53
54async def create_attachment(
55self, input: AttachmentCreateParams, context: TContext
56) -> Attachment:
3b20c3d1Jiwon Kim6 months ago57"""Create an attachment record from upload metadata."""
f688d870victor-openai8 months ago58raise NotImplementedError(
59f"{type(self).__name__} must override create_attachment() to support two-phase file upload"
60)
61
62def 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
65return default_generate_id("attachment")
66
67
68class Store(ABC, Generic[TContext]):
69def generate_thread_id(self, context: TContext) -> str:
70"""Return a new identifier for a thread. Override this method to customize thread ID generation."""
71
72return default_generate_id("thread")
73
74def generate_item_id(
75self, 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
79return default_generate_id(item_type)
80
81@abstractmethod
82async def load_thread(self, thread_id: str, context: TContext) -> ThreadMetadata:
3b20c3d1Jiwon Kim6 months ago83"""Load a thread's metadata by id."""
f688d870victor-openai8 months ago84pass
85
86@abstractmethod
87async def save_thread(self, thread: ThreadMetadata, context: TContext) -> None:
3b20c3d1Jiwon Kim6 months ago88"""Persist thread metadata (title, status, etc.)."""
f688d870victor-openai8 months ago89pass
90
91@abstractmethod
92async def load_thread_items(
93self,
94thread_id: str,
95after: str | None,
96limit: int,
97order: str,
98context: TContext,
99) -> Page[ThreadItem]:
3b20c3d1Jiwon Kim6 months ago100"""Load a page of thread items with pagination controls."""
f688d870victor-openai8 months ago101pass
102
103@abstractmethod
104async def save_attachment(self, attachment: Attachment, context: TContext) -> None:
3b20c3d1Jiwon Kim6 months ago105"""Persist attachment metadata."""
f688d870victor-openai8 months ago106pass
107
108@abstractmethod
109async def load_attachment(
110self, attachment_id: str, context: TContext
111) -> Attachment:
3b20c3d1Jiwon Kim6 months ago112"""Load attachment metadata by id."""
f688d870victor-openai8 months ago113pass
114
115@abstractmethod
116async def delete_attachment(self, attachment_id: str, context: TContext) -> None:
3b20c3d1Jiwon Kim6 months ago117"""Delete attachment metadata by id."""
f688d870victor-openai8 months ago118pass
119
120@abstractmethod
121async def load_threads(
122self,
123limit: int,
124after: str | None,
125order: str,
126context: TContext,
127) -> Page[ThreadMetadata]:
3b20c3d1Jiwon Kim6 months ago128"""Load a page of threads with pagination controls."""
f688d870victor-openai8 months ago129pass
130
131@abstractmethod
132async def add_thread_item(
133self, thread_id: str, item: ThreadItem, context: TContext
134) -> None:
3b20c3d1Jiwon Kim6 months ago135"""Persist a newly created thread item."""
f688d870victor-openai8 months ago136pass
137
138@abstractmethod
139async def save_item(
140self, thread_id: str, item: ThreadItem, context: TContext
141) -> None:
3b20c3d1Jiwon Kim6 months ago142"""Upsert a thread item by id."""
f688d870victor-openai8 months ago143pass
144
145@abstractmethod
146async def load_item(
147self, thread_id: str, item_id: str, context: TContext
148) -> ThreadItem:
3b20c3d1Jiwon Kim6 months ago149"""Load a thread item by id."""
f688d870victor-openai8 months ago150pass
151
152@abstractmethod
153async def delete_thread(self, thread_id: str, context: TContext) -> None:
3b20c3d1Jiwon Kim6 months ago154"""Delete a thread and its items."""
f688d870victor-openai8 months ago155pass
156
157@abstractmethod
158async def delete_thread_item(
159self, thread_id: str, item_id: str, context: TContext
160) -> None:
3b20c3d1Jiwon Kim6 months ago161"""Delete a thread item by id."""
f688d870victor-openai8 months ago162pass