agent-office-cli 0.1.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.
- agent_office/__init__.py +73 -0
- agent_office/adapters/__init__.py +11 -0
- agent_office/adapters/base.py +76 -0
- agent_office/adapters/discord.py +254 -0
- agent_office/adapters/memory.py +253 -0
- agent_office/cli.py +352 -0
- agent_office/errors.py +66 -0
- agent_office/formatter.py +204 -0
- agent_office/models.py +303 -0
- agent_office/service.py +551 -0
- agent_office/store.py +624 -0
- agent_office_cli-0.1.0.dist-info/METADATA +93 -0
- agent_office_cli-0.1.0.dist-info/RECORD +16 -0
- agent_office_cli-0.1.0.dist-info/WHEEL +5 -0
- agent_office_cli-0.1.0.dist-info/entry_points.txt +2 -0
- agent_office_cli-0.1.0.dist-info/top_level.txt +1 -0
agent_office/service.py
ADDED
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
"""Workflow service coordinating desks, actors, requests, and conversation adapters."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .adapters.base import ConversationAdapter
|
|
6
|
+
from .errors import InvalidArgumentsError
|
|
7
|
+
from .models import (
|
|
8
|
+
VALID_REQUEST_STATUSES,
|
|
9
|
+
Actor,
|
|
10
|
+
ActorRole,
|
|
11
|
+
AskResult,
|
|
12
|
+
Desk,
|
|
13
|
+
InboxPullResult,
|
|
14
|
+
RepliesResult,
|
|
15
|
+
ReplyResult,
|
|
16
|
+
Request,
|
|
17
|
+
RequestEvent,
|
|
18
|
+
RequestStatus,
|
|
19
|
+
)
|
|
20
|
+
from .store import SQLiteStore
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _derive_title(content: str, fallback: str) -> str:
|
|
24
|
+
for line in content.splitlines():
|
|
25
|
+
cleaned = line.strip()
|
|
26
|
+
if cleaned:
|
|
27
|
+
return cleaned[:80]
|
|
28
|
+
return fallback[:80]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _default_thread_name(req: Request) -> str:
|
|
32
|
+
raw = f"{req.req_id}: {req.title}".strip()
|
|
33
|
+
return raw[:90]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class AgentOfficeService:
|
|
37
|
+
"""High-level Agent Office operations built on SQLiteStore + ConversationAdapter."""
|
|
38
|
+
|
|
39
|
+
def __init__(self, store: SQLiteStore, adapter: ConversationAdapter) -> None:
|
|
40
|
+
self.store = store
|
|
41
|
+
self.adapter = adapter
|
|
42
|
+
self._sync_allowlists()
|
|
43
|
+
|
|
44
|
+
def _sync_allowlists(self) -> None:
|
|
45
|
+
for channel_id in self.store.list_allowed_channels():
|
|
46
|
+
self.adapter.allow_channel(channel_id)
|
|
47
|
+
for thread_id in self.store.list_allowed_threads():
|
|
48
|
+
self.adapter.allow_thread(thread_id)
|
|
49
|
+
|
|
50
|
+
def _format_outbound(self, text: str, actor_id: str | None) -> str:
|
|
51
|
+
body = text.strip()
|
|
52
|
+
if not body:
|
|
53
|
+
raise InvalidArgumentsError("Message content must not be empty.")
|
|
54
|
+
if not actor_id:
|
|
55
|
+
return body
|
|
56
|
+
actor = self.store.get_actor(actor_id)
|
|
57
|
+
return f"[{actor.display_name}] {body}"
|
|
58
|
+
|
|
59
|
+
def _ensure_thread(
|
|
60
|
+
self,
|
|
61
|
+
req: Request,
|
|
62
|
+
*,
|
|
63
|
+
thread_name: str | None = None,
|
|
64
|
+
allow_mentions: bool = False,
|
|
65
|
+
) -> tuple[Request, bool]:
|
|
66
|
+
if req.thread_id:
|
|
67
|
+
self.adapter.allow_thread(req.thread_id)
|
|
68
|
+
return req, False
|
|
69
|
+
|
|
70
|
+
self.adapter.allow_channel(req.channel_id)
|
|
71
|
+
source_msg_id = req.source_message_id
|
|
72
|
+
if not source_msg_id:
|
|
73
|
+
anchor_text = f"[{req.req_id}] {req.title}"
|
|
74
|
+
if req.body and req.body != req.title:
|
|
75
|
+
anchor_text = f"{anchor_text}\n{req.body}"
|
|
76
|
+
posted_anchor = self.adapter.post_message(
|
|
77
|
+
req.channel_id,
|
|
78
|
+
anchor_text[:1900],
|
|
79
|
+
allow_mentions=allow_mentions,
|
|
80
|
+
)
|
|
81
|
+
source_msg_id = posted_anchor.message_id
|
|
82
|
+
req = self.store.update_request(req.req_id, source_message_id=source_msg_id)
|
|
83
|
+
|
|
84
|
+
created = self.adapter.create_thread(
|
|
85
|
+
req.channel_id,
|
|
86
|
+
source_msg_id,
|
|
87
|
+
(thread_name or _default_thread_name(req))[:100],
|
|
88
|
+
)
|
|
89
|
+
req = self.store.update_request(req.req_id, thread_id=created.thread_id)
|
|
90
|
+
self.store.add_event(
|
|
91
|
+
req.req_id,
|
|
92
|
+
"THREAD_CREATED",
|
|
93
|
+
content=created.name,
|
|
94
|
+
metadata={"thread_id": created.thread_id, "source_message_id": source_msg_id},
|
|
95
|
+
)
|
|
96
|
+
return req, True
|
|
97
|
+
|
|
98
|
+
def _resolve_reply_filter(
|
|
99
|
+
self,
|
|
100
|
+
req: Request,
|
|
101
|
+
*,
|
|
102
|
+
any_user: bool,
|
|
103
|
+
author_id: str | None,
|
|
104
|
+
) -> tuple[str | None, bool]:
|
|
105
|
+
if author_id is not None and author_id.strip():
|
|
106
|
+
return author_id.strip(), False
|
|
107
|
+
if any_user:
|
|
108
|
+
return None, False
|
|
109
|
+
if req.requester_external_id:
|
|
110
|
+
return req.requester_external_id, False
|
|
111
|
+
return None, True
|
|
112
|
+
|
|
113
|
+
# --- Desk & Actor management ---
|
|
114
|
+
|
|
115
|
+
def add_desk(
|
|
116
|
+
self,
|
|
117
|
+
desk_id: str,
|
|
118
|
+
channel_id: str,
|
|
119
|
+
*,
|
|
120
|
+
name: str | None = None,
|
|
121
|
+
adapter_type: str | None = None,
|
|
122
|
+
default_assignee_id: str | None = None,
|
|
123
|
+
) -> Desk:
|
|
124
|
+
desk = self.store.create_desk(
|
|
125
|
+
desk_id,
|
|
126
|
+
channel_id,
|
|
127
|
+
name=name,
|
|
128
|
+
adapter_type=adapter_type or self.adapter.adapter_type,
|
|
129
|
+
default_assignee_id=default_assignee_id,
|
|
130
|
+
)
|
|
131
|
+
self.adapter.allow_channel(desk.channel_id)
|
|
132
|
+
return desk
|
|
133
|
+
|
|
134
|
+
def list_desks(self) -> list[Desk]:
|
|
135
|
+
return self.store.list_desks()
|
|
136
|
+
|
|
137
|
+
def add_actor(
|
|
138
|
+
self,
|
|
139
|
+
actor_id: str,
|
|
140
|
+
role: ActorRole,
|
|
141
|
+
*,
|
|
142
|
+
display_name: str | None = None,
|
|
143
|
+
desk_id: str | None = None,
|
|
144
|
+
external_user_id: str | None = None,
|
|
145
|
+
is_bot: bool | None = None,
|
|
146
|
+
) -> Actor:
|
|
147
|
+
return self.store.create_actor(
|
|
148
|
+
actor_id,
|
|
149
|
+
role,
|
|
150
|
+
display_name=display_name,
|
|
151
|
+
desk_id=desk_id,
|
|
152
|
+
external_user_id=external_user_id,
|
|
153
|
+
is_bot=is_bot,
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
def list_actors(
|
|
157
|
+
self,
|
|
158
|
+
*,
|
|
159
|
+
desk_id: str | None = None,
|
|
160
|
+
role: str | None = None,
|
|
161
|
+
) -> list[Actor]:
|
|
162
|
+
return self.store.list_actors(desk_id=desk_id, role=role)
|
|
163
|
+
|
|
164
|
+
# --- Inbox intake & Request creation ---
|
|
165
|
+
|
|
166
|
+
def pull_inbox(
|
|
167
|
+
self,
|
|
168
|
+
desk_id: str,
|
|
169
|
+
*,
|
|
170
|
+
prefix: str | None = None,
|
|
171
|
+
limit: int = 50,
|
|
172
|
+
exclude_bots: bool = True,
|
|
173
|
+
) -> InboxPullResult:
|
|
174
|
+
desk = self.store.get_desk(desk_id)
|
|
175
|
+
self.adapter.allow_channel(desk.channel_id)
|
|
176
|
+
previous_cursor = desk.inbox_cursor
|
|
177
|
+
|
|
178
|
+
batch = self.adapter.list_messages(
|
|
179
|
+
channel_id=desk.channel_id,
|
|
180
|
+
after=previous_cursor,
|
|
181
|
+
limit=limit,
|
|
182
|
+
exclude_bots=exclude_bots,
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
created: list[Request] = []
|
|
186
|
+
skipped = 0
|
|
187
|
+
for msg in batch.messages:
|
|
188
|
+
if prefix is not None and not msg.content.startswith(prefix):
|
|
189
|
+
skipped += 1
|
|
190
|
+
continue
|
|
191
|
+
existing = self.store.find_request_by_source_message(desk.channel_id, msg.message_id)
|
|
192
|
+
if existing is not None:
|
|
193
|
+
skipped += 1
|
|
194
|
+
continue
|
|
195
|
+
|
|
196
|
+
actor = self.store.find_actor_by_external_id(msg.author_id)
|
|
197
|
+
title = _derive_title(msg.content, fallback=f"Request from {msg.author_name}")
|
|
198
|
+
req = self.store.create_request(
|
|
199
|
+
desk_id=desk.desk_id,
|
|
200
|
+
title=title,
|
|
201
|
+
body=msg.content,
|
|
202
|
+
channel_id=desk.channel_id,
|
|
203
|
+
requester_actor_id=actor.actor_id if actor else None,
|
|
204
|
+
requester_external_id=msg.author_id,
|
|
205
|
+
assignee_actor_id=desk.default_assignee_id,
|
|
206
|
+
source_message_id=msg.message_id,
|
|
207
|
+
)
|
|
208
|
+
self.store.add_event(
|
|
209
|
+
req.req_id,
|
|
210
|
+
"CREATED",
|
|
211
|
+
actor_id=actor.actor_id if actor else None,
|
|
212
|
+
external_user_id=msg.author_id,
|
|
213
|
+
external_message_id=msg.message_id,
|
|
214
|
+
content=msg.content,
|
|
215
|
+
metadata={"source": "inbox_pull"},
|
|
216
|
+
)
|
|
217
|
+
created.append(req)
|
|
218
|
+
|
|
219
|
+
new_cursor = batch.cursor or previous_cursor
|
|
220
|
+
if new_cursor != previous_cursor:
|
|
221
|
+
self.store.update_desk_inbox_cursor(desk.desk_id, new_cursor)
|
|
222
|
+
|
|
223
|
+
return InboxPullResult(
|
|
224
|
+
desk_id=desk.desk_id,
|
|
225
|
+
channel_id=desk.channel_id,
|
|
226
|
+
previous_cursor=previous_cursor,
|
|
227
|
+
cursor=new_cursor,
|
|
228
|
+
created_requests=tuple(created),
|
|
229
|
+
skipped_count=skipped,
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
def create_request(
|
|
233
|
+
self,
|
|
234
|
+
desk_id: str,
|
|
235
|
+
title: str,
|
|
236
|
+
body: str = "",
|
|
237
|
+
*,
|
|
238
|
+
parent_req_id: str | None = None,
|
|
239
|
+
requester_actor_id: str | None = None,
|
|
240
|
+
requester_external_id: str | None = None,
|
|
241
|
+
assignee_actor_id: str | None = None,
|
|
242
|
+
post_to_channel: bool = False,
|
|
243
|
+
allow_mentions: bool = False,
|
|
244
|
+
) -> Request:
|
|
245
|
+
desk = self.store.get_desk(desk_id)
|
|
246
|
+
cleaned_title = title.strip()
|
|
247
|
+
if not cleaned_title:
|
|
248
|
+
raise InvalidArgumentsError("Request title must not be empty.")
|
|
249
|
+
cleaned_body = body.strip() or cleaned_title
|
|
250
|
+
|
|
251
|
+
resolved_ext_id = requester_external_id
|
|
252
|
+
if resolved_ext_id is None and requester_actor_id is not None:
|
|
253
|
+
requester = self.store.get_actor(requester_actor_id)
|
|
254
|
+
resolved_ext_id = requester.external_user_id
|
|
255
|
+
|
|
256
|
+
resolved_assignee = assignee_actor_id or desk.default_assignee_id
|
|
257
|
+
req_id = self.store.next_req_id()
|
|
258
|
+
source_msg_id: str | None = None
|
|
259
|
+
|
|
260
|
+
if post_to_channel:
|
|
261
|
+
self.adapter.allow_channel(desk.channel_id)
|
|
262
|
+
header = f"[{req_id}] {cleaned_title}"
|
|
263
|
+
if parent_req_id:
|
|
264
|
+
header = f"[{req_id} <- {parent_req_id}] {cleaned_title}"
|
|
265
|
+
payload = f"{header}\n{cleaned_body}" if cleaned_body != cleaned_title else header
|
|
266
|
+
posted = self.adapter.post_message(
|
|
267
|
+
desk.channel_id,
|
|
268
|
+
payload[:1900],
|
|
269
|
+
allow_mentions=allow_mentions,
|
|
270
|
+
)
|
|
271
|
+
source_msg_id = posted.message_id
|
|
272
|
+
|
|
273
|
+
req = self.store.create_request(
|
|
274
|
+
req_id=req_id,
|
|
275
|
+
desk_id=desk.desk_id,
|
|
276
|
+
title=cleaned_title,
|
|
277
|
+
body=cleaned_body,
|
|
278
|
+
channel_id=desk.channel_id,
|
|
279
|
+
parent_req_id=parent_req_id,
|
|
280
|
+
requester_actor_id=requester_actor_id,
|
|
281
|
+
requester_external_id=resolved_ext_id,
|
|
282
|
+
assignee_actor_id=resolved_assignee,
|
|
283
|
+
source_message_id=source_msg_id,
|
|
284
|
+
)
|
|
285
|
+
self.store.add_event(
|
|
286
|
+
req.req_id,
|
|
287
|
+
"CREATED",
|
|
288
|
+
actor_id=requester_actor_id,
|
|
289
|
+
external_user_id=resolved_ext_id,
|
|
290
|
+
external_message_id=source_msg_id,
|
|
291
|
+
content=cleaned_body,
|
|
292
|
+
metadata={"source": "manual", "parent_req_id": parent_req_id},
|
|
293
|
+
)
|
|
294
|
+
return req
|
|
295
|
+
|
|
296
|
+
def list_requests(
|
|
297
|
+
self,
|
|
298
|
+
*,
|
|
299
|
+
desk_id: str | None = None,
|
|
300
|
+
status: str | None = None,
|
|
301
|
+
parent_req_id: str | None = None,
|
|
302
|
+
assignee_actor_id: str | None = None,
|
|
303
|
+
limit: int = 50,
|
|
304
|
+
) -> list[Request]:
|
|
305
|
+
return self.store.list_requests(
|
|
306
|
+
desk_id=desk_id,
|
|
307
|
+
status=status,
|
|
308
|
+
parent_req_id=parent_req_id,
|
|
309
|
+
assignee_actor_id=assignee_actor_id,
|
|
310
|
+
limit=limit,
|
|
311
|
+
)
|
|
312
|
+
|
|
313
|
+
def get_request_detail(
|
|
314
|
+
self,
|
|
315
|
+
req_id: str,
|
|
316
|
+
*,
|
|
317
|
+
event_limit: int = 50,
|
|
318
|
+
) -> tuple[Request, list[RequestEvent]]:
|
|
319
|
+
req = self.store.get_request(req_id)
|
|
320
|
+
events = self.store.list_events(req.req_id, limit=event_limit)
|
|
321
|
+
return req, events
|
|
322
|
+
|
|
323
|
+
# --- Thread conversation workflow ---
|
|
324
|
+
|
|
325
|
+
def ask(
|
|
326
|
+
self,
|
|
327
|
+
req_id: str,
|
|
328
|
+
question: str,
|
|
329
|
+
*,
|
|
330
|
+
actor_id: str | None = None,
|
|
331
|
+
thread_name: str | None = None,
|
|
332
|
+
allow_mentions: bool = False,
|
|
333
|
+
) -> AskResult:
|
|
334
|
+
req = self.store.get_request(req_id)
|
|
335
|
+
formatted = self._format_outbound(question, actor_id)
|
|
336
|
+
req, thread_created = self._ensure_thread(
|
|
337
|
+
req,
|
|
338
|
+
thread_name=thread_name,
|
|
339
|
+
allow_mentions=allow_mentions,
|
|
340
|
+
)
|
|
341
|
+
assert req.thread_id is not None
|
|
342
|
+
|
|
343
|
+
posted = self.adapter.send_to_thread(
|
|
344
|
+
req.thread_id,
|
|
345
|
+
formatted,
|
|
346
|
+
allow_mentions=allow_mentions,
|
|
347
|
+
)
|
|
348
|
+
req = self.store.update_request(
|
|
349
|
+
req.req_id,
|
|
350
|
+
status="CLARIFYING",
|
|
351
|
+
thread_cursor=posted.message_id,
|
|
352
|
+
)
|
|
353
|
+
event = self.store.add_event(
|
|
354
|
+
req.req_id,
|
|
355
|
+
"ASK",
|
|
356
|
+
actor_id=actor_id,
|
|
357
|
+
external_message_id=posted.message_id,
|
|
358
|
+
content=question.strip(),
|
|
359
|
+
metadata={"thread_id": req.thread_id},
|
|
360
|
+
)
|
|
361
|
+
return AskResult(
|
|
362
|
+
request=req,
|
|
363
|
+
thread_created=thread_created,
|
|
364
|
+
posted=posted,
|
|
365
|
+
event=event,
|
|
366
|
+
)
|
|
367
|
+
|
|
368
|
+
def read_replies(
|
|
369
|
+
self,
|
|
370
|
+
req_id: str,
|
|
371
|
+
*,
|
|
372
|
+
any_user: bool = False,
|
|
373
|
+
author_id: str | None = None,
|
|
374
|
+
limit: int = 50,
|
|
375
|
+
) -> RepliesResult:
|
|
376
|
+
req = self.store.get_request(req_id)
|
|
377
|
+
if not req.thread_id:
|
|
378
|
+
raise InvalidArgumentsError(
|
|
379
|
+
f"Request '{req.req_id}' does not have a thread yet. Run 'req ask' or 'req reply' first."
|
|
380
|
+
)
|
|
381
|
+
self.adapter.allow_thread(req.thread_id)
|
|
382
|
+
previous_cursor = req.thread_cursor
|
|
383
|
+
target_author, exclude_bots = self._resolve_reply_filter(
|
|
384
|
+
req,
|
|
385
|
+
any_user=any_user,
|
|
386
|
+
author_id=author_id,
|
|
387
|
+
)
|
|
388
|
+
|
|
389
|
+
batch = self.adapter.list_messages(
|
|
390
|
+
thread_id=req.thread_id,
|
|
391
|
+
after=previous_cursor,
|
|
392
|
+
limit=limit,
|
|
393
|
+
author_id=target_author,
|
|
394
|
+
exclude_bots=exclude_bots,
|
|
395
|
+
)
|
|
396
|
+
|
|
397
|
+
events: list[RequestEvent] = []
|
|
398
|
+
for msg in batch.messages:
|
|
399
|
+
actor = self.store.find_actor_by_external_id(msg.author_id)
|
|
400
|
+
ev = self.store.add_event(
|
|
401
|
+
req.req_id,
|
|
402
|
+
"ANSWER",
|
|
403
|
+
actor_id=actor.actor_id if actor else None,
|
|
404
|
+
external_user_id=msg.author_id,
|
|
405
|
+
external_message_id=msg.message_id,
|
|
406
|
+
content=msg.content,
|
|
407
|
+
metadata={"author_name": msg.author_name},
|
|
408
|
+
)
|
|
409
|
+
events.append(ev)
|
|
410
|
+
|
|
411
|
+
new_cursor = batch.cursor or previous_cursor
|
|
412
|
+
if new_cursor != previous_cursor:
|
|
413
|
+
req = self.store.update_request(req.req_id, thread_cursor=new_cursor)
|
|
414
|
+
|
|
415
|
+
return RepliesResult(
|
|
416
|
+
request=req,
|
|
417
|
+
previous_cursor=previous_cursor,
|
|
418
|
+
cursor=new_cursor,
|
|
419
|
+
messages=batch.messages,
|
|
420
|
+
events=tuple(events),
|
|
421
|
+
timed_out=False,
|
|
422
|
+
polls=1,
|
|
423
|
+
)
|
|
424
|
+
|
|
425
|
+
def wait_for_answer(
|
|
426
|
+
self,
|
|
427
|
+
req_id: str,
|
|
428
|
+
*,
|
|
429
|
+
timeout: float = 60.0,
|
|
430
|
+
poll_interval: float = 2.0,
|
|
431
|
+
any_user: bool = False,
|
|
432
|
+
author_id: str | None = None,
|
|
433
|
+
) -> RepliesResult:
|
|
434
|
+
req = self.store.get_request(req_id)
|
|
435
|
+
if not req.thread_id:
|
|
436
|
+
raise InvalidArgumentsError(
|
|
437
|
+
f"Request '{req.req_id}' does not have a thread yet. Run 'req ask' or 'req reply' first."
|
|
438
|
+
)
|
|
439
|
+
self.adapter.allow_thread(req.thread_id)
|
|
440
|
+
previous_cursor = req.thread_cursor
|
|
441
|
+
target_author, exclude_bots = self._resolve_reply_filter(
|
|
442
|
+
req,
|
|
443
|
+
any_user=any_user,
|
|
444
|
+
author_id=author_id,
|
|
445
|
+
)
|
|
446
|
+
|
|
447
|
+
batch = self.adapter.wait_for_messages(
|
|
448
|
+
thread_id=req.thread_id,
|
|
449
|
+
after=previous_cursor,
|
|
450
|
+
timeout=timeout,
|
|
451
|
+
poll_interval=poll_interval,
|
|
452
|
+
author_id=target_author,
|
|
453
|
+
exclude_bots=exclude_bots,
|
|
454
|
+
)
|
|
455
|
+
|
|
456
|
+
events: list[RequestEvent] = []
|
|
457
|
+
for msg in batch.messages:
|
|
458
|
+
actor = self.store.find_actor_by_external_id(msg.author_id)
|
|
459
|
+
ev = self.store.add_event(
|
|
460
|
+
req.req_id,
|
|
461
|
+
"ANSWER",
|
|
462
|
+
actor_id=actor.actor_id if actor else None,
|
|
463
|
+
external_user_id=msg.author_id,
|
|
464
|
+
external_message_id=msg.message_id,
|
|
465
|
+
content=msg.content,
|
|
466
|
+
metadata={"author_name": msg.author_name},
|
|
467
|
+
)
|
|
468
|
+
events.append(ev)
|
|
469
|
+
|
|
470
|
+
new_cursor = batch.cursor or previous_cursor
|
|
471
|
+
if new_cursor != previous_cursor:
|
|
472
|
+
req = self.store.update_request(req.req_id, thread_cursor=new_cursor)
|
|
473
|
+
|
|
474
|
+
return RepliesResult(
|
|
475
|
+
request=req,
|
|
476
|
+
previous_cursor=previous_cursor,
|
|
477
|
+
cursor=new_cursor,
|
|
478
|
+
messages=batch.messages,
|
|
479
|
+
events=tuple(events),
|
|
480
|
+
timed_out=batch.timed_out,
|
|
481
|
+
polls=batch.polls,
|
|
482
|
+
)
|
|
483
|
+
|
|
484
|
+
def reply(
|
|
485
|
+
self,
|
|
486
|
+
req_id: str,
|
|
487
|
+
message: str,
|
|
488
|
+
*,
|
|
489
|
+
actor_id: str | None = None,
|
|
490
|
+
status: RequestStatus | None = None,
|
|
491
|
+
allow_mentions: bool = False,
|
|
492
|
+
) -> ReplyResult:
|
|
493
|
+
if status is not None and status not in VALID_REQUEST_STATUSES:
|
|
494
|
+
raise InvalidArgumentsError(f"Invalid request status '{status}'.")
|
|
495
|
+
|
|
496
|
+
req = self.store.get_request(req_id)
|
|
497
|
+
formatted = self._format_outbound(message, actor_id)
|
|
498
|
+
req, _ = self._ensure_thread(req, allow_mentions=allow_mentions)
|
|
499
|
+
assert req.thread_id is not None
|
|
500
|
+
|
|
501
|
+
posted = self.adapter.send_to_thread(
|
|
502
|
+
req.thread_id,
|
|
503
|
+
formatted,
|
|
504
|
+
allow_mentions=allow_mentions,
|
|
505
|
+
)
|
|
506
|
+
previous_status = req.status
|
|
507
|
+
if status is not None:
|
|
508
|
+
req = self.store.update_request(
|
|
509
|
+
req.req_id,
|
|
510
|
+
status=status,
|
|
511
|
+
thread_cursor=posted.message_id,
|
|
512
|
+
)
|
|
513
|
+
else:
|
|
514
|
+
req = self.store.update_request(
|
|
515
|
+
req.req_id,
|
|
516
|
+
thread_cursor=posted.message_id,
|
|
517
|
+
)
|
|
518
|
+
|
|
519
|
+
event = self.store.add_event(
|
|
520
|
+
req.req_id,
|
|
521
|
+
"REPLY",
|
|
522
|
+
actor_id=actor_id,
|
|
523
|
+
external_message_id=posted.message_id,
|
|
524
|
+
content=message.strip(),
|
|
525
|
+
metadata={"status_before": previous_status, "status_after": req.status},
|
|
526
|
+
)
|
|
527
|
+
return ReplyResult(request=req, posted=posted, event=event)
|
|
528
|
+
|
|
529
|
+
def set_status(
|
|
530
|
+
self,
|
|
531
|
+
req_id: str,
|
|
532
|
+
status: RequestStatus,
|
|
533
|
+
*,
|
|
534
|
+
actor_id: str | None = None,
|
|
535
|
+
note: str = "",
|
|
536
|
+
) -> tuple[Request, RequestEvent]:
|
|
537
|
+
normalized = status.strip().upper()
|
|
538
|
+
if normalized not in VALID_REQUEST_STATUSES:
|
|
539
|
+
raise InvalidArgumentsError(
|
|
540
|
+
f"Invalid status '{status}'. Expected one of: {', '.join(VALID_REQUEST_STATUSES)}."
|
|
541
|
+
)
|
|
542
|
+
before = self.store.get_request(req_id)
|
|
543
|
+
updated = self.store.update_request(before.req_id, status=normalized)
|
|
544
|
+
event = self.store.add_event(
|
|
545
|
+
updated.req_id,
|
|
546
|
+
"STATUS_CHANGED",
|
|
547
|
+
actor_id=actor_id,
|
|
548
|
+
content=note.strip(),
|
|
549
|
+
metadata={"from": before.status, "to": updated.status},
|
|
550
|
+
)
|
|
551
|
+
return updated, event
|