continuum-task-server-bundle 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- continuum_task_server_bundle/__init__.py +1 -0
- continuum_task_server_bundle/daytona/__init__.py +4 -0
- continuum_task_server_bundle/daytona/common.py +534 -0
- continuum_task_server_bundle/daytona/create_sandbox.py +166 -0
- continuum_task_server_bundle/daytona/dispatch.py +125 -0
- continuum_task_server_bundle/daytona/execute_sandbox.py +243 -0
- continuum_task_server_bundle/daytona/filesystem.py +330 -0
- continuum_task_server_bundle/daytona/fs_edit.py +127 -0
- continuum_task_server_bundle/daytona/git_ops.py +585 -0
- continuum_task_server_bundle/daytona/github_app.py +207 -0
- continuum_task_server_bundle/daytona/manage_sandbox.py +104 -0
- continuum_task_server_bundle/daytona/plugin.py +18 -0
- continuum_task_server_bundle/daytona/s3_transfer.py +207 -0
- continuum_task_server_bundle/discord/__init__.py +4 -0
- continuum_task_server_bundle/discord/attachment_registry_db.py +127 -0
- continuum_task_server_bundle/discord/attachment_resolve.py +213 -0
- continuum_task_server_bundle/discord/await_cleanup.py +105 -0
- continuum_task_server_bundle/discord/discord_ready.py +39 -0
- continuum_task_server_bundle/discord/message_await_apply.py +143 -0
- continuum_task_server_bundle/discord/message_await_db.py +197 -0
- continuum_task_server_bundle/discord/message_await_inbound.py +54 -0
- continuum_task_server_bundle/discord/message_await_logic.py +133 -0
- continuum_task_server_bundle/discord/message_await_poller.py +122 -0
- continuum_task_server_bundle/discord/message_chunking.py +45 -0
- continuum_task_server_bundle/discord/methods.py +408 -0
- continuum_task_server_bundle/discord/plugin.py +105 -0
- continuum_task_server_bundle/discord/reaction_await_apply.py +238 -0
- continuum_task_server_bundle/discord/reaction_await_db.py +174 -0
- continuum_task_server_bundle/discord/reaction_await_inbound.py +47 -0
- continuum_task_server_bundle/discord/reaction_await_logic.py +118 -0
- continuum_task_server_bundle/discord/reaction_await_poller.py +131 -0
- continuum_task_server_bundle/discord/s3_attachment_config.py +25 -0
- continuum_task_server_bundle/discord/s3_message_attachments.py +139 -0
- continuum_task_server_bundle/discord/sql/001_message_awaits.sql +112 -0
- continuum_task_server_bundle/discord/sql/002_reaction_awaits.sql +61 -0
- continuum_task_server_bundle/github/__init__.py +4 -0
- continuum_task_server_bundle/github/awaits/__init__.py +1 -0
- continuum_task_server_bundle/github/awaits/apply.py +92 -0
- continuum_task_server_bundle/github/awaits/cleanup.py +94 -0
- continuum_task_server_bundle/github/awaits/db.py +220 -0
- continuum_task_server_bundle/github/awaits/evaluators.py +325 -0
- continuum_task_server_bundle/github/awaits/fetch.py +192 -0
- continuum_task_server_bundle/github/awaits/poller.py +82 -0
- continuum_task_server_bundle/github/awaits/register.py +37 -0
- continuum_task_server_bundle/github/ci_status.py +70 -0
- continuum_task_server_bundle/github/comment_normalize.py +76 -0
- continuum_task_server_bundle/github/errors.py +52 -0
- continuum_task_server_bundle/github/github_app.py +291 -0
- continuum_task_server_bundle/github/github_http.py +377 -0
- continuum_task_server_bundle/github/methods/__init__.py +90 -0
- continuum_task_server_bundle/github/methods/_common.py +183 -0
- continuum_task_server_bundle/github/methods/awaits.py +107 -0
- continuum_task_server_bundle/github/methods/ci.py +158 -0
- continuum_task_server_bundle/github/methods/comments.py +214 -0
- continuum_task_server_bundle/github/methods/credentials.py +59 -0
- continuum_task_server_bundle/github/methods/issues.py +131 -0
- continuum_task_server_bundle/github/methods/pulls.py +329 -0
- continuum_task_server_bundle/github/methods/repos.py +424 -0
- continuum_task_server_bundle/github/methods/search.py +109 -0
- continuum_task_server_bundle/github/plugin.py +91 -0
- continuum_task_server_bundle/github/sql/001_github_awaits.sql +65 -0
- continuum_task_server_bundle/github/webhook_server.py +126 -0
- continuum_task_server_bundle/jira/__init__.py +4 -0
- continuum_task_server_bundle/jira/await_poller.py +236 -0
- continuum_task_server_bundle/jira/await_store.py +271 -0
- continuum_task_server_bundle/jira/config.py +68 -0
- continuum_task_server_bundle/jira/jira_client.py +382 -0
- continuum_task_server_bundle/jira/methods.py +246 -0
- continuum_task_server_bundle/jira/plugin.py +86 -0
- continuum_task_server_bundle/jira/sql/001_jira_issue_comment_waits.sql +19 -0
- continuum_task_server_bundle/jira/webhook.py +70 -0
- continuum_task_server_bundle/openrouter/__init__.py +4 -0
- continuum_task_server_bundle/openrouter/chat_service.py +1037 -0
- continuum_task_server_bundle/openrouter/chat_store.py +849 -0
- continuum_task_server_bundle/openrouter/environment.py +29 -0
- continuum_task_server_bundle/openrouter/handler.py +59 -0
- continuum_task_server_bundle/openrouter/image_context.py +207 -0
- continuum_task_server_bundle/openrouter/openrouter_client.py +324 -0
- continuum_task_server_bundle/openrouter/plugin.py +37 -0
- continuum_task_server_bundle/openrouter/prompt_projection.py +417 -0
- continuum_task_server_bundle/openrouter/sql/chat.sql +50 -0
- continuum_task_server_bundle/openrouter/sql/migrate_prune_then_chunk_v1.sql +11 -0
- continuum_task_server_bundle/roblox/__init__.py +4 -0
- continuum_task_server_bundle/roblox/open_cloud.py +335 -0
- continuum_task_server_bundle/roblox/plugin.py +30 -0
- continuum_task_server_bundle/roblox/storage_s3.py +55 -0
- continuum_task_server_bundle-1.0.0.dist-info/METADATA +109 -0
- continuum_task_server_bundle-1.0.0.dist-info/RECORD +90 -0
- continuum_task_server_bundle-1.0.0.dist-info/WHEEL +5 -0
- continuum_task_server_bundle-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,849 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from datetime import datetime, timezone
|
|
3
|
+
from json import dumps, loads
|
|
4
|
+
from threading import Lock
|
|
5
|
+
from uuid import UUID, uuid4
|
|
6
|
+
|
|
7
|
+
from .prompt_projection import bind_snapshot_anchor
|
|
8
|
+
|
|
9
|
+
# Blank-root sends have no parent message. Postgres primary keys cannot use NULL,
|
|
10
|
+
# so those rows store this sentinel in chat_sends.parent_key.
|
|
11
|
+
NIL_PARENT = UUID("00000000-0000-0000-0000-000000000000")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class IdempotencyConflict(Exception):
|
|
15
|
+
"""Another send already committed this key. The caller's inserts must roll back."""
|
|
16
|
+
|
|
17
|
+
def __init__(self, chat_id, assistant_message_id):
|
|
18
|
+
super().__init__(chat_id, assistant_message_id)
|
|
19
|
+
self.chat_id = chat_id
|
|
20
|
+
self.assistant_message_id = assistant_message_id
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass(frozen=True)
|
|
24
|
+
class StoredMessage:
|
|
25
|
+
id: UUID
|
|
26
|
+
chat_id: UUID
|
|
27
|
+
parent_id: UUID | None
|
|
28
|
+
body: dict
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True)
|
|
32
|
+
class CommittedSend:
|
|
33
|
+
assistant: StoredMessage
|
|
34
|
+
model: str | None
|
|
35
|
+
finish_reason: str | None
|
|
36
|
+
compacted_from: str | None = None
|
|
37
|
+
usage: dict | None = None
|
|
38
|
+
compaction_usage: dict | None = None
|
|
39
|
+
context_edit: dict | None = None
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass(frozen=True)
|
|
43
|
+
class ChatOptions:
|
|
44
|
+
tools: list | None
|
|
45
|
+
tool_choice: object
|
|
46
|
+
max_input_tokens: int | None = None
|
|
47
|
+
compacted_from: str | None = None
|
|
48
|
+
compact_target_tokens: int | None = None
|
|
49
|
+
context_policy: dict | None = None
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass(frozen=True)
|
|
53
|
+
class MessageRef:
|
|
54
|
+
id: UUID
|
|
55
|
+
parent_id: UUID | None
|
|
56
|
+
size: int
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@dataclass
|
|
60
|
+
class _SendRecord:
|
|
61
|
+
assistant_id: UUID
|
|
62
|
+
assistant_chat_id: UUID
|
|
63
|
+
model: str | None
|
|
64
|
+
finish_reason: str | None
|
|
65
|
+
result_cursor: str | None = None
|
|
66
|
+
usage: dict | None = None
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _usage_blob(usage, compaction_usage, context_edit=None):
|
|
70
|
+
blob = {}
|
|
71
|
+
if usage:
|
|
72
|
+
blob["usage"] = usage
|
|
73
|
+
if compaction_usage is not None:
|
|
74
|
+
blob["compaction_usage"] = compaction_usage
|
|
75
|
+
if context_edit is not None:
|
|
76
|
+
blob["context_edit"] = context_edit
|
|
77
|
+
return blob or None
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _split_usage_blob(blob):
|
|
81
|
+
if not isinstance(blob, dict):
|
|
82
|
+
return None, None
|
|
83
|
+
usage = blob.get("usage")
|
|
84
|
+
compaction_usage = blob.get("compaction_usage")
|
|
85
|
+
return (
|
|
86
|
+
usage if isinstance(usage, dict) else None,
|
|
87
|
+
compaction_usage if isinstance(compaction_usage, dict) else None,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def parent_key(parent_id):
|
|
92
|
+
if parent_id is None:
|
|
93
|
+
return NIL_PARENT
|
|
94
|
+
return parent_id
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _cursor_text(chat_id, message_id):
|
|
98
|
+
if message_id is None:
|
|
99
|
+
return f"{chat_id}:"
|
|
100
|
+
return f"{chat_id}:{message_id}"
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _parse_result_cursor(cursor):
|
|
104
|
+
chat_text, message_text = cursor.split(":", 1)
|
|
105
|
+
return UUID(chat_text), UUID(message_text)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _copy_body(body):
|
|
109
|
+
return loads(dumps(body))
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _as_body(value):
|
|
113
|
+
if isinstance(value, str):
|
|
114
|
+
return loads(value)
|
|
115
|
+
return value
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _json_copy(value):
|
|
119
|
+
if isinstance(value, str):
|
|
120
|
+
stripped = value.lstrip()
|
|
121
|
+
if stripped[:1] in '{["':
|
|
122
|
+
try:
|
|
123
|
+
value = loads(value)
|
|
124
|
+
except ValueError:
|
|
125
|
+
return value
|
|
126
|
+
else:
|
|
127
|
+
return value
|
|
128
|
+
if value is None or isinstance(value, (str, int, float, bool)):
|
|
129
|
+
return value
|
|
130
|
+
return loads(dumps(value))
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
class _Chat:
|
|
134
|
+
def __init__(
|
|
135
|
+
self,
|
|
136
|
+
tools,
|
|
137
|
+
tool_choice,
|
|
138
|
+
max_input_tokens=None,
|
|
139
|
+
compacted_from=None,
|
|
140
|
+
compact_target_tokens=None,
|
|
141
|
+
context_policy=None,
|
|
142
|
+
):
|
|
143
|
+
self.context_policy = context_policy
|
|
144
|
+
self.last_activity_at = datetime.now(timezone.utc)
|
|
145
|
+
self.tools = tools
|
|
146
|
+
self.tool_choice = tool_choice
|
|
147
|
+
self.max_input_tokens = max_input_tokens
|
|
148
|
+
self.compacted_from = compacted_from
|
|
149
|
+
self.compact_target_tokens = compact_target_tokens
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
class MemoryChatStore:
|
|
153
|
+
"""In-process store used by tests. Semantics match PostgresChatStore."""
|
|
154
|
+
|
|
155
|
+
def __init__(self):
|
|
156
|
+
self._lock = Lock()
|
|
157
|
+
self._chats = {}
|
|
158
|
+
self._messages = {}
|
|
159
|
+
self._sends = {}
|
|
160
|
+
self._projections = {}
|
|
161
|
+
self.load_count = 0
|
|
162
|
+
|
|
163
|
+
def create_chat(
|
|
164
|
+
self,
|
|
165
|
+
tools=None,
|
|
166
|
+
tool_choice=None,
|
|
167
|
+
max_input_tokens=None,
|
|
168
|
+
compacted_from=None,
|
|
169
|
+
compact_target_tokens=None,
|
|
170
|
+
context_policy=None,
|
|
171
|
+
):
|
|
172
|
+
chat_id = uuid4()
|
|
173
|
+
with self._lock:
|
|
174
|
+
self._chats[chat_id] = _Chat(
|
|
175
|
+
_json_copy(tools),
|
|
176
|
+
_json_copy(tool_choice),
|
|
177
|
+
max_input_tokens,
|
|
178
|
+
compacted_from,
|
|
179
|
+
compact_target_tokens,
|
|
180
|
+
_json_copy(context_policy),
|
|
181
|
+
)
|
|
182
|
+
self._messages[chat_id] = []
|
|
183
|
+
return chat_id
|
|
184
|
+
|
|
185
|
+
def get_options(self, chat_id):
|
|
186
|
+
with self._lock:
|
|
187
|
+
chat = self._chats.get(chat_id)
|
|
188
|
+
if chat is None:
|
|
189
|
+
return None
|
|
190
|
+
return ChatOptions(
|
|
191
|
+
_json_copy(chat.tools),
|
|
192
|
+
_json_copy(chat.tool_choice),
|
|
193
|
+
chat.max_input_tokens,
|
|
194
|
+
chat.compacted_from,
|
|
195
|
+
chat.compact_target_tokens,
|
|
196
|
+
_json_copy(chat.context_policy),
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
def set_options(self, chat_id, tools, tool_choice):
|
|
200
|
+
with self._lock:
|
|
201
|
+
chat = self._chats.get(chat_id)
|
|
202
|
+
if chat is None:
|
|
203
|
+
return False
|
|
204
|
+
chat.tools = _json_copy(tools)
|
|
205
|
+
chat.tool_choice = _json_copy(tool_choice)
|
|
206
|
+
return True
|
|
207
|
+
|
|
208
|
+
def load_messages(self, chat_id):
|
|
209
|
+
with self._lock:
|
|
210
|
+
self.load_count += 1
|
|
211
|
+
if chat_id not in self._chats:
|
|
212
|
+
return None
|
|
213
|
+
return list(self._messages[chat_id])
|
|
214
|
+
|
|
215
|
+
def load_message_index(self, chat_id):
|
|
216
|
+
with self._lock:
|
|
217
|
+
if chat_id not in self._chats:
|
|
218
|
+
return None
|
|
219
|
+
return [
|
|
220
|
+
MessageRef(message.id, message.parent_id, len(dumps(message.body).encode()))
|
|
221
|
+
for message in self._messages[chat_id]
|
|
222
|
+
]
|
|
223
|
+
|
|
224
|
+
def load_messages_by_ids(self, chat_id, message_ids):
|
|
225
|
+
wanted = set(message_ids)
|
|
226
|
+
with self._lock:
|
|
227
|
+
if chat_id not in self._chats:
|
|
228
|
+
return None
|
|
229
|
+
return [
|
|
230
|
+
message
|
|
231
|
+
for message in self._messages[chat_id]
|
|
232
|
+
if message.id in wanted
|
|
233
|
+
]
|
|
234
|
+
|
|
235
|
+
def load_projection(self, chat_id, message_id):
|
|
236
|
+
with self._lock:
|
|
237
|
+
return _json_copy(self._projections.get((chat_id, message_id)))
|
|
238
|
+
|
|
239
|
+
def find_send(self, chat_id, parent_id, idempotency_key):
|
|
240
|
+
with self._lock:
|
|
241
|
+
found = self._sends.get((chat_id, parent_key(parent_id), idempotency_key))
|
|
242
|
+
if found is None:
|
|
243
|
+
return None
|
|
244
|
+
compacted_from = None
|
|
245
|
+
assistant_chat_id = found.assistant_chat_id
|
|
246
|
+
if found.result_cursor is not None:
|
|
247
|
+
compacted_from = _cursor_text(chat_id, parent_id)
|
|
248
|
+
usage, compaction_usage = _split_usage_blob(found.usage)
|
|
249
|
+
context_edit = found.usage.get('context_edit') if isinstance(found.usage, dict) else None
|
|
250
|
+
return self._committed_unlocked(
|
|
251
|
+
assistant_chat_id,
|
|
252
|
+
found.assistant_id,
|
|
253
|
+
found.model,
|
|
254
|
+
found.finish_reason,
|
|
255
|
+
compacted_from,
|
|
256
|
+
usage,
|
|
257
|
+
compaction_usage,
|
|
258
|
+
context_edit,
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
def get_send(self, chat_id, assistant_message_id):
|
|
262
|
+
with self._lock:
|
|
263
|
+
if chat_id not in self._chats:
|
|
264
|
+
return None
|
|
265
|
+
for (send_chat, _parent, _key), found in self._sends.items():
|
|
266
|
+
if send_chat == chat_id and found.assistant_id == assistant_message_id:
|
|
267
|
+
return self._committed_unlocked(
|
|
268
|
+
chat_id,
|
|
269
|
+
found.assistant_id,
|
|
270
|
+
found.model,
|
|
271
|
+
found.finish_reason,
|
|
272
|
+
)
|
|
273
|
+
return self._committed_unlocked(chat_id, assistant_message_id, None, None)
|
|
274
|
+
|
|
275
|
+
def commit_send(
|
|
276
|
+
self,
|
|
277
|
+
chat_id,
|
|
278
|
+
parent_id,
|
|
279
|
+
delta_bodies,
|
|
280
|
+
assistant_body,
|
|
281
|
+
idempotency_key,
|
|
282
|
+
model,
|
|
283
|
+
finish_reason,
|
|
284
|
+
usage=None,
|
|
285
|
+
compaction_usage=None,
|
|
286
|
+
projection=None,
|
|
287
|
+
context_edit=None,
|
|
288
|
+
):
|
|
289
|
+
with self._lock:
|
|
290
|
+
if chat_id not in self._chats:
|
|
291
|
+
return None
|
|
292
|
+
key = None
|
|
293
|
+
if idempotency_key:
|
|
294
|
+
key = (chat_id, parent_key(parent_id), idempotency_key)
|
|
295
|
+
if key in self._sends:
|
|
296
|
+
raise IdempotencyConflict(chat_id, self._sends[key].assistant_id)
|
|
297
|
+
|
|
298
|
+
prev = parent_id
|
|
299
|
+
for body in delta_bodies:
|
|
300
|
+
message_id = uuid4()
|
|
301
|
+
self._messages[chat_id].append(
|
|
302
|
+
StoredMessage(message_id, chat_id, prev, _copy_body(body))
|
|
303
|
+
)
|
|
304
|
+
prev = message_id
|
|
305
|
+
|
|
306
|
+
assistant_id = uuid4()
|
|
307
|
+
assistant = StoredMessage(
|
|
308
|
+
assistant_id,
|
|
309
|
+
chat_id,
|
|
310
|
+
prev,
|
|
311
|
+
_copy_body(assistant_body),
|
|
312
|
+
)
|
|
313
|
+
self._messages[chat_id].append(assistant)
|
|
314
|
+
if projection is not None:
|
|
315
|
+
self._projections[(chat_id, assistant_id)] = bind_snapshot_anchor(projection, assistant_id)
|
|
316
|
+
self._chats[chat_id].last_activity_at = datetime.now(timezone.utc)
|
|
317
|
+
if key is not None:
|
|
318
|
+
self._sends[key] = _SendRecord(
|
|
319
|
+
assistant_id,
|
|
320
|
+
chat_id,
|
|
321
|
+
model,
|
|
322
|
+
finish_reason,
|
|
323
|
+
usage=_usage_blob(usage, compaction_usage, context_edit),
|
|
324
|
+
)
|
|
325
|
+
return CommittedSend(assistant, model, finish_reason, None, usage, compaction_usage, context_edit)
|
|
326
|
+
|
|
327
|
+
def commit_compacted_send(
|
|
328
|
+
self,
|
|
329
|
+
chat_id,
|
|
330
|
+
parent_id,
|
|
331
|
+
compacted_from,
|
|
332
|
+
tools,
|
|
333
|
+
tool_choice,
|
|
334
|
+
max_input_tokens,
|
|
335
|
+
prior_bodies,
|
|
336
|
+
assistant_body,
|
|
337
|
+
idempotency_key,
|
|
338
|
+
model,
|
|
339
|
+
finish_reason,
|
|
340
|
+
compact_target_tokens=None,
|
|
341
|
+
usage=None,
|
|
342
|
+
compaction_usage=None,
|
|
343
|
+
):
|
|
344
|
+
with self._lock:
|
|
345
|
+
if chat_id not in self._chats:
|
|
346
|
+
return None
|
|
347
|
+
key = None
|
|
348
|
+
if idempotency_key:
|
|
349
|
+
key = (chat_id, parent_key(parent_id), idempotency_key)
|
|
350
|
+
if key in self._sends:
|
|
351
|
+
raise IdempotencyConflict(chat_id, self._sends[key].assistant_id)
|
|
352
|
+
|
|
353
|
+
new_chat_id = uuid4()
|
|
354
|
+
self._chats[new_chat_id] = _Chat(
|
|
355
|
+
_json_copy(tools),
|
|
356
|
+
_json_copy(tool_choice),
|
|
357
|
+
max_input_tokens,
|
|
358
|
+
compacted_from,
|
|
359
|
+
compact_target_tokens,
|
|
360
|
+
)
|
|
361
|
+
self._messages[new_chat_id] = []
|
|
362
|
+
prev = None
|
|
363
|
+
for body in prior_bodies:
|
|
364
|
+
message_id = uuid4()
|
|
365
|
+
self._messages[new_chat_id].append(
|
|
366
|
+
StoredMessage(message_id, new_chat_id, prev, _copy_body(body))
|
|
367
|
+
)
|
|
368
|
+
prev = message_id
|
|
369
|
+
assistant_id = uuid4()
|
|
370
|
+
assistant = StoredMessage(
|
|
371
|
+
assistant_id,
|
|
372
|
+
new_chat_id,
|
|
373
|
+
prev,
|
|
374
|
+
_copy_body(assistant_body),
|
|
375
|
+
)
|
|
376
|
+
self._messages[new_chat_id].append(assistant)
|
|
377
|
+
self._chats[chat_id].last_activity_at = datetime.now(timezone.utc)
|
|
378
|
+
result_cursor = f"{new_chat_id}:{assistant_id}"
|
|
379
|
+
if key is not None:
|
|
380
|
+
self._sends[key] = _SendRecord(
|
|
381
|
+
assistant_id,
|
|
382
|
+
new_chat_id,
|
|
383
|
+
model,
|
|
384
|
+
finish_reason,
|
|
385
|
+
result_cursor,
|
|
386
|
+
_usage_blob(usage, compaction_usage),
|
|
387
|
+
)
|
|
388
|
+
return CommittedSend(
|
|
389
|
+
assistant,
|
|
390
|
+
model,
|
|
391
|
+
finish_reason,
|
|
392
|
+
compacted_from,
|
|
393
|
+
usage,
|
|
394
|
+
compaction_usage,
|
|
395
|
+
)
|
|
396
|
+
|
|
397
|
+
def delete_expired(self, cutoff):
|
|
398
|
+
with self._lock:
|
|
399
|
+
expired = [
|
|
400
|
+
chat_id
|
|
401
|
+
for chat_id, chat in self._chats.items()
|
|
402
|
+
if chat.last_activity_at < cutoff
|
|
403
|
+
]
|
|
404
|
+
for chat_id in expired:
|
|
405
|
+
del self._chats[chat_id]
|
|
406
|
+
self._messages.pop(chat_id, None)
|
|
407
|
+
for key in [key for key in self._projections if key[0] == chat_id]:
|
|
408
|
+
del self._projections[key]
|
|
409
|
+
for key in [key for key in self._sends if key[0] == chat_id]:
|
|
410
|
+
del self._sends[key]
|
|
411
|
+
return len(expired)
|
|
412
|
+
|
|
413
|
+
def _committed_unlocked(
|
|
414
|
+
self,
|
|
415
|
+
chat_id,
|
|
416
|
+
assistant_id,
|
|
417
|
+
model,
|
|
418
|
+
finish_reason,
|
|
419
|
+
compacted_from=None,
|
|
420
|
+
usage=None,
|
|
421
|
+
compaction_usage=None,
|
|
422
|
+
context_edit=None,
|
|
423
|
+
):
|
|
424
|
+
assistant = next(
|
|
425
|
+
(message for message in self._messages.get(chat_id, []) if message.id == assistant_id),
|
|
426
|
+
None,
|
|
427
|
+
)
|
|
428
|
+
if assistant is None:
|
|
429
|
+
return None
|
|
430
|
+
return CommittedSend(
|
|
431
|
+
assistant,
|
|
432
|
+
model,
|
|
433
|
+
finish_reason,
|
|
434
|
+
compacted_from,
|
|
435
|
+
usage,
|
|
436
|
+
compaction_usage,
|
|
437
|
+
context_edit,
|
|
438
|
+
)
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
class PostgresChatStore:
|
|
442
|
+
def __init__(self, database_url, *, min_size=1, max_size=8):
|
|
443
|
+
from psycopg_pool import ConnectionPool
|
|
444
|
+
|
|
445
|
+
self._pool = ConnectionPool(
|
|
446
|
+
database_url,
|
|
447
|
+
min_size=min_size,
|
|
448
|
+
max_size=max_size,
|
|
449
|
+
kwargs={"autocommit": True},
|
|
450
|
+
open=True,
|
|
451
|
+
)
|
|
452
|
+
|
|
453
|
+
def close(self):
|
|
454
|
+
self._pool.close(timeout=5)
|
|
455
|
+
|
|
456
|
+
def create_chat(
|
|
457
|
+
self,
|
|
458
|
+
tools=None,
|
|
459
|
+
tool_choice=None,
|
|
460
|
+
max_input_tokens=None,
|
|
461
|
+
compacted_from=None,
|
|
462
|
+
compact_target_tokens=None,
|
|
463
|
+
context_policy=None,
|
|
464
|
+
):
|
|
465
|
+
from psycopg.types.json import Jsonb
|
|
466
|
+
|
|
467
|
+
chat_id = uuid4()
|
|
468
|
+
with self._pool.connection() as conn:
|
|
469
|
+
conn.execute(
|
|
470
|
+
"""
|
|
471
|
+
INSERT INTO chats (
|
|
472
|
+
id, tools, tool_choice, max_input_tokens, compacted_from,
|
|
473
|
+
compact_target_tokens, context_policy
|
|
474
|
+
)
|
|
475
|
+
VALUES (%s, %s, %s, %s, %s, %s, %s)
|
|
476
|
+
""",
|
|
477
|
+
(
|
|
478
|
+
chat_id,
|
|
479
|
+
Jsonb(tools) if tools is not None else None,
|
|
480
|
+
Jsonb(tool_choice) if tool_choice is not None else None,
|
|
481
|
+
max_input_tokens,
|
|
482
|
+
compacted_from,
|
|
483
|
+
compact_target_tokens,
|
|
484
|
+
Jsonb(context_policy) if context_policy is not None else None,
|
|
485
|
+
),
|
|
486
|
+
)
|
|
487
|
+
return chat_id
|
|
488
|
+
|
|
489
|
+
def get_options(self, chat_id):
|
|
490
|
+
with self._pool.connection() as conn:
|
|
491
|
+
row = conn.execute(
|
|
492
|
+
"""
|
|
493
|
+
SELECT tools, tool_choice, max_input_tokens, compacted_from,
|
|
494
|
+
compact_target_tokens, context_policy
|
|
495
|
+
FROM chats WHERE id = %s
|
|
496
|
+
""",
|
|
497
|
+
(chat_id,),
|
|
498
|
+
).fetchone()
|
|
499
|
+
if row is None:
|
|
500
|
+
return None
|
|
501
|
+
return ChatOptions(
|
|
502
|
+
_json_copy(row[0]),
|
|
503
|
+
_json_copy(row[1]),
|
|
504
|
+
row[2],
|
|
505
|
+
row[3],
|
|
506
|
+
row[4],
|
|
507
|
+
_json_copy(row[5]),
|
|
508
|
+
)
|
|
509
|
+
|
|
510
|
+
def set_options(self, chat_id, tools, tool_choice):
|
|
511
|
+
from psycopg.types.json import Jsonb
|
|
512
|
+
|
|
513
|
+
with self._pool.connection() as conn:
|
|
514
|
+
cursor = conn.execute(
|
|
515
|
+
"UPDATE chats SET tools = %s, tool_choice = %s WHERE id = %s",
|
|
516
|
+
(
|
|
517
|
+
Jsonb(tools) if tools is not None else None,
|
|
518
|
+
Jsonb(tool_choice) if tool_choice is not None else None,
|
|
519
|
+
chat_id,
|
|
520
|
+
),
|
|
521
|
+
)
|
|
522
|
+
return cursor.rowcount > 0
|
|
523
|
+
|
|
524
|
+
def load_messages(self, chat_id):
|
|
525
|
+
with self._pool.connection() as conn:
|
|
526
|
+
exists = conn.execute("SELECT 1 FROM chats WHERE id = %s", (chat_id,)).fetchone()
|
|
527
|
+
if exists is None:
|
|
528
|
+
return None
|
|
529
|
+
rows = conn.execute(
|
|
530
|
+
"""
|
|
531
|
+
SELECT id, parent_id, body
|
|
532
|
+
FROM chat_messages
|
|
533
|
+
WHERE chat_id = %s
|
|
534
|
+
""",
|
|
535
|
+
(chat_id,),
|
|
536
|
+
).fetchall()
|
|
537
|
+
return [
|
|
538
|
+
StoredMessage(row[0], chat_id, row[1], _as_body(row[2]))
|
|
539
|
+
for row in rows
|
|
540
|
+
]
|
|
541
|
+
|
|
542
|
+
def load_message_index(self, chat_id):
|
|
543
|
+
with self._pool.connection() as conn:
|
|
544
|
+
exists = conn.execute("SELECT 1 FROM chats WHERE id = %s", (chat_id,)).fetchone()
|
|
545
|
+
if exists is None:
|
|
546
|
+
return None
|
|
547
|
+
rows = conn.execute(
|
|
548
|
+
"""
|
|
549
|
+
SELECT id, parent_id, octet_length(body::text)
|
|
550
|
+
FROM chat_messages
|
|
551
|
+
WHERE chat_id = %s
|
|
552
|
+
""",
|
|
553
|
+
(chat_id,),
|
|
554
|
+
).fetchall()
|
|
555
|
+
return [MessageRef(row[0], row[1], row[2] or 0) for row in rows]
|
|
556
|
+
|
|
557
|
+
def load_messages_by_ids(self, chat_id, message_ids):
|
|
558
|
+
if not message_ids:
|
|
559
|
+
return []
|
|
560
|
+
with self._pool.connection() as conn:
|
|
561
|
+
rows = conn.execute(
|
|
562
|
+
"""
|
|
563
|
+
SELECT id, parent_id, body
|
|
564
|
+
FROM chat_messages
|
|
565
|
+
WHERE chat_id = %s AND id = ANY(%s)
|
|
566
|
+
""",
|
|
567
|
+
(chat_id, list(message_ids)),
|
|
568
|
+
).fetchall()
|
|
569
|
+
return [
|
|
570
|
+
StoredMessage(row[0], chat_id, row[1], _as_body(row[2]))
|
|
571
|
+
for row in rows
|
|
572
|
+
]
|
|
573
|
+
|
|
574
|
+
def load_projection(self, chat_id, message_id):
|
|
575
|
+
with self._pool.connection() as conn:
|
|
576
|
+
row = conn.execute(
|
|
577
|
+
'SELECT projection FROM chat_messages WHERE chat_id = %s AND id = %s',
|
|
578
|
+
(chat_id, message_id),
|
|
579
|
+
).fetchone()
|
|
580
|
+
return _json_copy(row[0]) if row is not None else None
|
|
581
|
+
|
|
582
|
+
def find_send(self, chat_id, parent_id, idempotency_key):
|
|
583
|
+
with self._pool.connection() as conn:
|
|
584
|
+
row = conn.execute(
|
|
585
|
+
"""
|
|
586
|
+
SELECT assistant_message_id, model, finish_reason, result_cursor, usage
|
|
587
|
+
FROM chat_sends
|
|
588
|
+
WHERE chat_id = %s AND parent_key = %s AND idempotency_key = %s
|
|
589
|
+
""",
|
|
590
|
+
(chat_id, parent_key(parent_id), idempotency_key),
|
|
591
|
+
).fetchone()
|
|
592
|
+
if row is None:
|
|
593
|
+
return None
|
|
594
|
+
result_cursor = row[3]
|
|
595
|
+
blob = _json_copy(row[4])
|
|
596
|
+
usage, compaction_usage = _split_usage_blob(blob)
|
|
597
|
+
context_edit = blob.get('context_edit') if isinstance(blob, dict) else None
|
|
598
|
+
if result_cursor:
|
|
599
|
+
assistant_chat_id, assistant_id = _parse_result_cursor(result_cursor)
|
|
600
|
+
compacted_from = _cursor_text(chat_id, parent_id)
|
|
601
|
+
else:
|
|
602
|
+
assistant_chat_id = chat_id
|
|
603
|
+
assistant_id = row[0]
|
|
604
|
+
compacted_from = None
|
|
605
|
+
message = conn.execute(
|
|
606
|
+
"""
|
|
607
|
+
SELECT parent_id, body
|
|
608
|
+
FROM chat_messages
|
|
609
|
+
WHERE chat_id = %s AND id = %s
|
|
610
|
+
""",
|
|
611
|
+
(assistant_chat_id, assistant_id),
|
|
612
|
+
).fetchone()
|
|
613
|
+
if message is None:
|
|
614
|
+
return None
|
|
615
|
+
assistant = StoredMessage(
|
|
616
|
+
assistant_id, assistant_chat_id, message[0], _as_body(message[1])
|
|
617
|
+
)
|
|
618
|
+
return CommittedSend(
|
|
619
|
+
assistant, row[1], row[2], compacted_from, usage, compaction_usage, context_edit
|
|
620
|
+
)
|
|
621
|
+
|
|
622
|
+
def get_send(self, chat_id, assistant_message_id):
|
|
623
|
+
with self._pool.connection() as conn:
|
|
624
|
+
row = conn.execute(
|
|
625
|
+
"""
|
|
626
|
+
SELECT m.parent_id, m.body, s.model, s.finish_reason
|
|
627
|
+
FROM chat_messages m
|
|
628
|
+
LEFT JOIN chat_sends s
|
|
629
|
+
ON s.chat_id = m.chat_id AND s.assistant_message_id = m.id
|
|
630
|
+
WHERE m.chat_id = %s AND m.id = %s
|
|
631
|
+
""",
|
|
632
|
+
(chat_id, assistant_message_id),
|
|
633
|
+
).fetchone()
|
|
634
|
+
if row is None:
|
|
635
|
+
return None
|
|
636
|
+
assistant = StoredMessage(assistant_message_id, chat_id, row[0], _as_body(row[1]))
|
|
637
|
+
return CommittedSend(assistant, row[2], row[3])
|
|
638
|
+
|
|
639
|
+
def commit_send(
|
|
640
|
+
self,
|
|
641
|
+
chat_id,
|
|
642
|
+
parent_id,
|
|
643
|
+
delta_bodies,
|
|
644
|
+
assistant_body,
|
|
645
|
+
idempotency_key,
|
|
646
|
+
model,
|
|
647
|
+
finish_reason,
|
|
648
|
+
usage=None,
|
|
649
|
+
compaction_usage=None,
|
|
650
|
+
projection=None,
|
|
651
|
+
context_edit=None,
|
|
652
|
+
):
|
|
653
|
+
from psycopg.types.json import Jsonb
|
|
654
|
+
|
|
655
|
+
usage_blob = _usage_blob(usage, compaction_usage, context_edit)
|
|
656
|
+
with self._pool.connection() as conn:
|
|
657
|
+
with conn.transaction():
|
|
658
|
+
exists = conn.execute(
|
|
659
|
+
"SELECT 1 FROM chats WHERE id = %s",
|
|
660
|
+
(chat_id,),
|
|
661
|
+
).fetchone()
|
|
662
|
+
if exists is None:
|
|
663
|
+
return None
|
|
664
|
+
|
|
665
|
+
prev = parent_id
|
|
666
|
+
for body in delta_bodies:
|
|
667
|
+
message_id = uuid4()
|
|
668
|
+
conn.execute(
|
|
669
|
+
"""
|
|
670
|
+
INSERT INTO chat_messages (id, chat_id, parent_id, body)
|
|
671
|
+
VALUES (%s, %s, %s, %s)
|
|
672
|
+
""",
|
|
673
|
+
(message_id, chat_id, prev, Jsonb(body)),
|
|
674
|
+
)
|
|
675
|
+
prev = message_id
|
|
676
|
+
|
|
677
|
+
assistant_id = uuid4()
|
|
678
|
+
conn.execute(
|
|
679
|
+
"""
|
|
680
|
+
INSERT INTO chat_messages (id, chat_id, parent_id, body, projection)
|
|
681
|
+
VALUES (%s, %s, %s, %s, %s)
|
|
682
|
+
""",
|
|
683
|
+
(assistant_id, chat_id, prev, Jsonb(assistant_body), Jsonb(bind_snapshot_anchor(projection, assistant_id)) if projection is not None else None),
|
|
684
|
+
)
|
|
685
|
+
conn.execute(
|
|
686
|
+
"UPDATE chats SET last_activity_at = now() WHERE id = %s",
|
|
687
|
+
(chat_id,),
|
|
688
|
+
)
|
|
689
|
+
if idempotency_key:
|
|
690
|
+
inserted = conn.execute(
|
|
691
|
+
"""
|
|
692
|
+
INSERT INTO chat_sends (
|
|
693
|
+
chat_id, parent_key, idempotency_key,
|
|
694
|
+
assistant_message_id, model, finish_reason,
|
|
695
|
+
result_cursor, usage
|
|
696
|
+
)
|
|
697
|
+
VALUES (%s, %s, %s, %s, %s, %s, NULL, %s)
|
|
698
|
+
ON CONFLICT (chat_id, parent_key, idempotency_key) DO NOTHING
|
|
699
|
+
RETURNING assistant_message_id
|
|
700
|
+
""",
|
|
701
|
+
(
|
|
702
|
+
chat_id,
|
|
703
|
+
parent_key(parent_id),
|
|
704
|
+
idempotency_key,
|
|
705
|
+
assistant_id,
|
|
706
|
+
model,
|
|
707
|
+
finish_reason,
|
|
708
|
+
Jsonb(usage_blob) if usage_blob is not None else None,
|
|
709
|
+
),
|
|
710
|
+
).fetchone()
|
|
711
|
+
if inserted is None:
|
|
712
|
+
winner = conn.execute(
|
|
713
|
+
"""
|
|
714
|
+
SELECT assistant_message_id
|
|
715
|
+
FROM chat_sends
|
|
716
|
+
WHERE chat_id = %s AND parent_key = %s AND idempotency_key = %s
|
|
717
|
+
""",
|
|
718
|
+
(chat_id, parent_key(parent_id), idempotency_key),
|
|
719
|
+
).fetchone()
|
|
720
|
+
winner_id = winner[0] if winner else None
|
|
721
|
+
raise IdempotencyConflict(chat_id, winner_id)
|
|
722
|
+
|
|
723
|
+
assistant = StoredMessage(assistant_id, chat_id, prev, _copy_body(assistant_body))
|
|
724
|
+
return CommittedSend(assistant, model, finish_reason, None, usage, compaction_usage, context_edit)
|
|
725
|
+
|
|
726
|
+
def commit_compacted_send(
|
|
727
|
+
self,
|
|
728
|
+
chat_id,
|
|
729
|
+
parent_id,
|
|
730
|
+
compacted_from,
|
|
731
|
+
tools,
|
|
732
|
+
tool_choice,
|
|
733
|
+
max_input_tokens,
|
|
734
|
+
prior_bodies,
|
|
735
|
+
assistant_body,
|
|
736
|
+
idempotency_key,
|
|
737
|
+
model,
|
|
738
|
+
finish_reason,
|
|
739
|
+
compact_target_tokens=None,
|
|
740
|
+
usage=None,
|
|
741
|
+
compaction_usage=None,
|
|
742
|
+
):
|
|
743
|
+
from psycopg.types.json import Jsonb
|
|
744
|
+
|
|
745
|
+
usage_blob = _usage_blob(usage, compaction_usage)
|
|
746
|
+
new_chat_id = uuid4()
|
|
747
|
+
with self._pool.connection() as conn:
|
|
748
|
+
with conn.transaction():
|
|
749
|
+
exists = conn.execute(
|
|
750
|
+
"SELECT 1 FROM chats WHERE id = %s",
|
|
751
|
+
(chat_id,),
|
|
752
|
+
).fetchone()
|
|
753
|
+
if exists is None:
|
|
754
|
+
return None
|
|
755
|
+
conn.execute(
|
|
756
|
+
"""
|
|
757
|
+
INSERT INTO chats (
|
|
758
|
+
id, tools, tool_choice, max_input_tokens, compacted_from,
|
|
759
|
+
compact_target_tokens
|
|
760
|
+
)
|
|
761
|
+
VALUES (%s, %s, %s, %s, %s, %s)
|
|
762
|
+
""",
|
|
763
|
+
(
|
|
764
|
+
new_chat_id,
|
|
765
|
+
Jsonb(tools) if tools is not None else None,
|
|
766
|
+
Jsonb(tool_choice) if tool_choice is not None else None,
|
|
767
|
+
max_input_tokens,
|
|
768
|
+
compacted_from,
|
|
769
|
+
compact_target_tokens,
|
|
770
|
+
),
|
|
771
|
+
)
|
|
772
|
+
prev = None
|
|
773
|
+
for body in prior_bodies:
|
|
774
|
+
message_id = uuid4()
|
|
775
|
+
conn.execute(
|
|
776
|
+
"""
|
|
777
|
+
INSERT INTO chat_messages (id, chat_id, parent_id, body)
|
|
778
|
+
VALUES (%s, %s, %s, %s)
|
|
779
|
+
""",
|
|
780
|
+
(message_id, new_chat_id, prev, Jsonb(body)),
|
|
781
|
+
)
|
|
782
|
+
prev = message_id
|
|
783
|
+
assistant_id = uuid4()
|
|
784
|
+
conn.execute(
|
|
785
|
+
"""
|
|
786
|
+
INSERT INTO chat_messages (id, chat_id, parent_id, body)
|
|
787
|
+
VALUES (%s, %s, %s, %s)
|
|
788
|
+
""",
|
|
789
|
+
(assistant_id, new_chat_id, prev, Jsonb(assistant_body)),
|
|
790
|
+
)
|
|
791
|
+
conn.execute(
|
|
792
|
+
"UPDATE chats SET last_activity_at = now() WHERE id = %s",
|
|
793
|
+
(chat_id,),
|
|
794
|
+
)
|
|
795
|
+
result_cursor = f"{new_chat_id}:{assistant_id}"
|
|
796
|
+
if idempotency_key:
|
|
797
|
+
inserted = conn.execute(
|
|
798
|
+
"""
|
|
799
|
+
INSERT INTO chat_sends (
|
|
800
|
+
chat_id, parent_key, idempotency_key,
|
|
801
|
+
assistant_message_id, model, finish_reason,
|
|
802
|
+
result_cursor, usage
|
|
803
|
+
)
|
|
804
|
+
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
|
805
|
+
ON CONFLICT (chat_id, parent_key, idempotency_key) DO NOTHING
|
|
806
|
+
RETURNING assistant_message_id
|
|
807
|
+
""",
|
|
808
|
+
(
|
|
809
|
+
chat_id,
|
|
810
|
+
parent_key(parent_id),
|
|
811
|
+
idempotency_key,
|
|
812
|
+
assistant_id,
|
|
813
|
+
model,
|
|
814
|
+
finish_reason,
|
|
815
|
+
result_cursor,
|
|
816
|
+
Jsonb(usage_blob) if usage_blob is not None else None,
|
|
817
|
+
),
|
|
818
|
+
).fetchone()
|
|
819
|
+
if inserted is None:
|
|
820
|
+
winner = conn.execute(
|
|
821
|
+
"""
|
|
822
|
+
SELECT assistant_message_id
|
|
823
|
+
FROM chat_sends
|
|
824
|
+
WHERE chat_id = %s AND parent_key = %s AND idempotency_key = %s
|
|
825
|
+
""",
|
|
826
|
+
(chat_id, parent_key(parent_id), idempotency_key),
|
|
827
|
+
).fetchone()
|
|
828
|
+
winner_id = winner[0] if winner else None
|
|
829
|
+
raise IdempotencyConflict(chat_id, winner_id)
|
|
830
|
+
|
|
831
|
+
assistant = StoredMessage(
|
|
832
|
+
assistant_id, new_chat_id, prev, _copy_body(assistant_body)
|
|
833
|
+
)
|
|
834
|
+
return CommittedSend(
|
|
835
|
+
assistant,
|
|
836
|
+
model,
|
|
837
|
+
finish_reason,
|
|
838
|
+
compacted_from,
|
|
839
|
+
usage,
|
|
840
|
+
compaction_usage,
|
|
841
|
+
)
|
|
842
|
+
|
|
843
|
+
def delete_expired(self, cutoff):
|
|
844
|
+
with self._pool.connection() as conn:
|
|
845
|
+
cursor = conn.execute(
|
|
846
|
+
"DELETE FROM chats WHERE last_activity_at < %s",
|
|
847
|
+
(cutoff,),
|
|
848
|
+
)
|
|
849
|
+
return cursor.rowcount
|