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/__init__.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""Agent Office — Human-centered multi-department workflow and conversation adapter library."""
|
|
2
|
+
|
|
3
|
+
from .adapters import (
|
|
4
|
+
ConversationAdapter,
|
|
5
|
+
DiscordConversationAdapter,
|
|
6
|
+
InMemoryConversationAdapter,
|
|
7
|
+
)
|
|
8
|
+
from .errors import (
|
|
9
|
+
AdapterError,
|
|
10
|
+
AgentOfficeError,
|
|
11
|
+
ChannelNotAllowedError,
|
|
12
|
+
ConflictError,
|
|
13
|
+
ExitCode,
|
|
14
|
+
InvalidArgumentsError,
|
|
15
|
+
NotFoundError,
|
|
16
|
+
StorageError,
|
|
17
|
+
)
|
|
18
|
+
from .models import (
|
|
19
|
+
Actor,
|
|
20
|
+
ActorRole,
|
|
21
|
+
AskResult,
|
|
22
|
+
CreatedThread,
|
|
23
|
+
Desk,
|
|
24
|
+
EventType,
|
|
25
|
+
InboxPullResult,
|
|
26
|
+
IncomingMessage,
|
|
27
|
+
MessageBatch,
|
|
28
|
+
PostedMessage,
|
|
29
|
+
RepliesResult,
|
|
30
|
+
ReplyResult,
|
|
31
|
+
Request,
|
|
32
|
+
RequestEvent,
|
|
33
|
+
RequestStatus,
|
|
34
|
+
WaitBatch,
|
|
35
|
+
)
|
|
36
|
+
from .service import AgentOfficeService
|
|
37
|
+
from .store import SQLiteStore, default_db_path
|
|
38
|
+
|
|
39
|
+
__version__ = "0.1.0"
|
|
40
|
+
|
|
41
|
+
__all__ = [
|
|
42
|
+
"__version__",
|
|
43
|
+
"AgentOfficeService",
|
|
44
|
+
"SQLiteStore",
|
|
45
|
+
"default_db_path",
|
|
46
|
+
"ConversationAdapter",
|
|
47
|
+
"DiscordConversationAdapter",
|
|
48
|
+
"InMemoryConversationAdapter",
|
|
49
|
+
"Desk",
|
|
50
|
+
"Actor",
|
|
51
|
+
"ActorRole",
|
|
52
|
+
"Request",
|
|
53
|
+
"RequestStatus",
|
|
54
|
+
"RequestEvent",
|
|
55
|
+
"EventType",
|
|
56
|
+
"PostedMessage",
|
|
57
|
+
"CreatedThread",
|
|
58
|
+
"IncomingMessage",
|
|
59
|
+
"MessageBatch",
|
|
60
|
+
"WaitBatch",
|
|
61
|
+
"InboxPullResult",
|
|
62
|
+
"AskResult",
|
|
63
|
+
"RepliesResult",
|
|
64
|
+
"ReplyResult",
|
|
65
|
+
"ExitCode",
|
|
66
|
+
"AgentOfficeError",
|
|
67
|
+
"InvalidArgumentsError",
|
|
68
|
+
"NotFoundError",
|
|
69
|
+
"ConflictError",
|
|
70
|
+
"ChannelNotAllowedError",
|
|
71
|
+
"AdapterError",
|
|
72
|
+
"StorageError",
|
|
73
|
+
]
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""Conversation adapter implementations for Agent Office."""
|
|
2
|
+
|
|
3
|
+
from .base import ConversationAdapter
|
|
4
|
+
from .discord import DiscordConversationAdapter
|
|
5
|
+
from .memory import InMemoryConversationAdapter
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"ConversationAdapter",
|
|
9
|
+
"DiscordConversationAdapter",
|
|
10
|
+
"InMemoryConversationAdapter",
|
|
11
|
+
]
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""Abstract conversation adapter protocol for Agent Office."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Protocol
|
|
6
|
+
|
|
7
|
+
from ..models import CreatedThread, MessageBatch, PostedMessage, WaitBatch
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ConversationAdapter(Protocol):
|
|
11
|
+
"""Platform-agnostic messaging interface used by AgentOfficeService."""
|
|
12
|
+
|
|
13
|
+
adapter_type: str
|
|
14
|
+
|
|
15
|
+
def allow_channel(self, channel_id: str) -> None:
|
|
16
|
+
"""Register a channel ID in the adapter's allowlist (if enforced)."""
|
|
17
|
+
...
|
|
18
|
+
|
|
19
|
+
def allow_thread(self, thread_id: str) -> None:
|
|
20
|
+
"""Register a thread ID in the adapter's allowlist (if enforced)."""
|
|
21
|
+
...
|
|
22
|
+
|
|
23
|
+
def post_message(
|
|
24
|
+
self,
|
|
25
|
+
channel_id: str,
|
|
26
|
+
content: str,
|
|
27
|
+
*,
|
|
28
|
+
allow_mentions: bool = False,
|
|
29
|
+
) -> PostedMessage:
|
|
30
|
+
"""Post a message to a top-level channel."""
|
|
31
|
+
...
|
|
32
|
+
|
|
33
|
+
def create_thread(
|
|
34
|
+
self,
|
|
35
|
+
channel_id: str,
|
|
36
|
+
message_id: str,
|
|
37
|
+
name: str,
|
|
38
|
+
) -> CreatedThread:
|
|
39
|
+
"""Create a thread anchored to an existing channel message."""
|
|
40
|
+
...
|
|
41
|
+
|
|
42
|
+
def send_to_thread(
|
|
43
|
+
self,
|
|
44
|
+
thread_id: str,
|
|
45
|
+
content: str,
|
|
46
|
+
*,
|
|
47
|
+
allow_mentions: bool = False,
|
|
48
|
+
) -> PostedMessage:
|
|
49
|
+
"""Post a message inside an existing thread."""
|
|
50
|
+
...
|
|
51
|
+
|
|
52
|
+
def list_messages(
|
|
53
|
+
self,
|
|
54
|
+
*,
|
|
55
|
+
channel_id: str | None = None,
|
|
56
|
+
thread_id: str | None = None,
|
|
57
|
+
after: str | None = None,
|
|
58
|
+
limit: int = 50,
|
|
59
|
+
author_id: str | None = None,
|
|
60
|
+
exclude_bots: bool = False,
|
|
61
|
+
) -> MessageBatch:
|
|
62
|
+
"""List messages in a channel or thread after an optional cursor."""
|
|
63
|
+
...
|
|
64
|
+
|
|
65
|
+
def wait_for_messages(
|
|
66
|
+
self,
|
|
67
|
+
*,
|
|
68
|
+
thread_id: str,
|
|
69
|
+
after: str | None = None,
|
|
70
|
+
timeout: float = 60.0,
|
|
71
|
+
poll_interval: float = 2.0,
|
|
72
|
+
author_id: str | None = None,
|
|
73
|
+
exclude_bots: bool = False,
|
|
74
|
+
) -> WaitBatch:
|
|
75
|
+
"""Wait for new matching messages in a thread until timeout."""
|
|
76
|
+
...
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
"""Discord ConversationAdapter backed by discord-message-cli (discord_msg)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from collections.abc import Collection, Mapping
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from discord_msg import (
|
|
10
|
+
DiscordClient,
|
|
11
|
+
DiscordMsgError,
|
|
12
|
+
HttpTransport,
|
|
13
|
+
MessageItem,
|
|
14
|
+
)
|
|
15
|
+
from discord_msg.auth import DEFAULT_TOKEN_ENV, resolve_token
|
|
16
|
+
|
|
17
|
+
from ..errors import AdapterError, ChannelNotAllowedError, InvalidArgumentsError
|
|
18
|
+
from ..models import (
|
|
19
|
+
CreatedThread,
|
|
20
|
+
IncomingMessage,
|
|
21
|
+
MessageBatch,
|
|
22
|
+
PostedMessage,
|
|
23
|
+
WaitBatch,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _to_incoming(item: MessageItem) -> IncomingMessage:
|
|
28
|
+
return IncomingMessage(
|
|
29
|
+
message_id=item.message_id,
|
|
30
|
+
channel_id=item.channel_id,
|
|
31
|
+
author_id=item.author_id,
|
|
32
|
+
author_name=item.author_display_name or item.author_username or item.author_id,
|
|
33
|
+
author_bot=item.author_bot,
|
|
34
|
+
timestamp=item.timestamp,
|
|
35
|
+
content=item.content,
|
|
36
|
+
referenced_message_id=item.referenced_message_id,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class DiscordConversationAdapter:
|
|
41
|
+
"""ConversationAdapter implementation delegating to discord_msg.DiscordClient."""
|
|
42
|
+
|
|
43
|
+
adapter_type: str = "discord"
|
|
44
|
+
|
|
45
|
+
def __init__(
|
|
46
|
+
self,
|
|
47
|
+
*,
|
|
48
|
+
client: DiscordClient | None = None,
|
|
49
|
+
token: str | None = None,
|
|
50
|
+
transport: HttpTransport | None = None,
|
|
51
|
+
env: Mapping[str, str] | None = None,
|
|
52
|
+
allowed_channels: Collection[str] | None = None,
|
|
53
|
+
allowed_threads: Collection[str] | None = None,
|
|
54
|
+
enforce_allowlist: bool = True,
|
|
55
|
+
) -> None:
|
|
56
|
+
self._client: DiscordClient | None = client
|
|
57
|
+
self._token = token
|
|
58
|
+
self._transport = transport
|
|
59
|
+
self._env = env
|
|
60
|
+
self._enforce_allowlist = enforce_allowlist
|
|
61
|
+
self._allowed_channels: set[str] = set(allowed_channels or ())
|
|
62
|
+
self._allowed_threads: set[str] = set(allowed_threads or ())
|
|
63
|
+
|
|
64
|
+
def _get_client(self) -> DiscordClient:
|
|
65
|
+
if self._client is not None:
|
|
66
|
+
return self._client
|
|
67
|
+
try:
|
|
68
|
+
if self._token is not None and self._token.strip():
|
|
69
|
+
resolved_token = self._token.strip()
|
|
70
|
+
if resolved_token.lower().startswith("bot "):
|
|
71
|
+
resolved_token = resolved_token[4:].strip()
|
|
72
|
+
else:
|
|
73
|
+
environ = self._env if self._env is not None else os.environ
|
|
74
|
+
resolved_token = resolve_token(
|
|
75
|
+
prompt_token=False,
|
|
76
|
+
token_stdin=False,
|
|
77
|
+
env_var=DEFAULT_TOKEN_ENV,
|
|
78
|
+
env=environ,
|
|
79
|
+
stdin=None,
|
|
80
|
+
)
|
|
81
|
+
self._client = DiscordClient(resolved_token, transport=self._transport)
|
|
82
|
+
return self._client
|
|
83
|
+
except DiscordMsgError as exc:
|
|
84
|
+
raise AdapterError(f"Discord auth/init failed ({exc.code}): {exc.message}") from exc
|
|
85
|
+
|
|
86
|
+
def allow_channel(self, channel_id: str) -> None:
|
|
87
|
+
cleaned = channel_id.strip()
|
|
88
|
+
if cleaned:
|
|
89
|
+
self._allowed_channels.add(cleaned)
|
|
90
|
+
|
|
91
|
+
def allow_thread(self, thread_id: str) -> None:
|
|
92
|
+
cleaned = thread_id.strip()
|
|
93
|
+
if cleaned:
|
|
94
|
+
self._allowed_threads.add(cleaned)
|
|
95
|
+
|
|
96
|
+
def _check_channel(self, channel_id: str) -> str:
|
|
97
|
+
cleaned = channel_id.strip()
|
|
98
|
+
if not cleaned:
|
|
99
|
+
raise InvalidArgumentsError("channel_id must not be empty.")
|
|
100
|
+
if self._enforce_allowlist and cleaned not in self._allowed_channels:
|
|
101
|
+
raise ChannelNotAllowedError(
|
|
102
|
+
f"Channel '{cleaned}' is not in the Agent Office allowlist."
|
|
103
|
+
)
|
|
104
|
+
return cleaned
|
|
105
|
+
|
|
106
|
+
def _check_thread(self, thread_id: str) -> str:
|
|
107
|
+
cleaned = thread_id.strip()
|
|
108
|
+
if not cleaned:
|
|
109
|
+
raise InvalidArgumentsError("thread_id must not be empty.")
|
|
110
|
+
if (
|
|
111
|
+
self._enforce_allowlist
|
|
112
|
+
and cleaned not in self._allowed_threads
|
|
113
|
+
and cleaned not in self._allowed_channels
|
|
114
|
+
):
|
|
115
|
+
raise ChannelNotAllowedError(
|
|
116
|
+
f"Thread '{cleaned}' is not in the Agent Office allowlist."
|
|
117
|
+
)
|
|
118
|
+
return cleaned
|
|
119
|
+
|
|
120
|
+
def post_message(
|
|
121
|
+
self,
|
|
122
|
+
channel_id: str,
|
|
123
|
+
content: str,
|
|
124
|
+
*,
|
|
125
|
+
allow_mentions: bool = False,
|
|
126
|
+
) -> PostedMessage:
|
|
127
|
+
cid = self._check_channel(channel_id)
|
|
128
|
+
try:
|
|
129
|
+
res = self._get_client().send_message(
|
|
130
|
+
cid,
|
|
131
|
+
content,
|
|
132
|
+
allow_mentions=allow_mentions,
|
|
133
|
+
)
|
|
134
|
+
except DiscordMsgError as exc:
|
|
135
|
+
raise AdapterError(f"Discord post_message failed ({exc.code}): {exc.message}") from exc
|
|
136
|
+
return PostedMessage(
|
|
137
|
+
channel_id=res.channel_id,
|
|
138
|
+
message_id=res.message_id,
|
|
139
|
+
timestamp=res.timestamp,
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
def create_thread(
|
|
143
|
+
self,
|
|
144
|
+
channel_id: str,
|
|
145
|
+
message_id: str,
|
|
146
|
+
name: str,
|
|
147
|
+
) -> CreatedThread:
|
|
148
|
+
cid = self._check_channel(channel_id)
|
|
149
|
+
try:
|
|
150
|
+
res = self._get_client().create_thread(cid, message_id, name)
|
|
151
|
+
except DiscordMsgError as exc:
|
|
152
|
+
raise AdapterError(f"Discord create_thread failed ({exc.code}): {exc.message}") from exc
|
|
153
|
+
self._allowed_threads.add(res.thread_id)
|
|
154
|
+
return CreatedThread(
|
|
155
|
+
thread_id=res.thread_id,
|
|
156
|
+
parent_channel_id=res.parent_channel_id,
|
|
157
|
+
source_message_id=res.source_message_id,
|
|
158
|
+
name=res.name,
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
def send_to_thread(
|
|
162
|
+
self,
|
|
163
|
+
thread_id: str,
|
|
164
|
+
content: str,
|
|
165
|
+
*,
|
|
166
|
+
allow_mentions: bool = False,
|
|
167
|
+
) -> PostedMessage:
|
|
168
|
+
tid = self._check_thread(thread_id)
|
|
169
|
+
try:
|
|
170
|
+
res = self._get_client().send_message(
|
|
171
|
+
thread_id=tid,
|
|
172
|
+
content=content,
|
|
173
|
+
allow_mentions=allow_mentions,
|
|
174
|
+
)
|
|
175
|
+
except DiscordMsgError as exc:
|
|
176
|
+
raise AdapterError(f"Discord send_to_thread failed ({exc.code}): {exc.message}") from exc
|
|
177
|
+
return PostedMessage(
|
|
178
|
+
channel_id=res.channel_id,
|
|
179
|
+
message_id=res.message_id,
|
|
180
|
+
timestamp=res.timestamp,
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
def list_messages(
|
|
184
|
+
self,
|
|
185
|
+
*,
|
|
186
|
+
channel_id: str | None = None,
|
|
187
|
+
thread_id: str | None = None,
|
|
188
|
+
after: str | None = None,
|
|
189
|
+
limit: int = 50,
|
|
190
|
+
author_id: str | None = None,
|
|
191
|
+
exclude_bots: bool = False,
|
|
192
|
+
) -> MessageBatch:
|
|
193
|
+
if channel_id and thread_id:
|
|
194
|
+
raise InvalidArgumentsError("Specify either channel_id or thread_id, not both.")
|
|
195
|
+
if not channel_id and not thread_id:
|
|
196
|
+
raise InvalidArgumentsError("Either channel_id or thread_id is required.")
|
|
197
|
+
|
|
198
|
+
kwargs: dict[str, Any] = {
|
|
199
|
+
"after": after,
|
|
200
|
+
"limit": limit,
|
|
201
|
+
"author_id": author_id,
|
|
202
|
+
"exclude_bots": exclude_bots,
|
|
203
|
+
}
|
|
204
|
+
if channel_id:
|
|
205
|
+
kwargs["channel_id"] = self._check_channel(channel_id)
|
|
206
|
+
else:
|
|
207
|
+
assert thread_id is not None
|
|
208
|
+
kwargs["thread_id"] = self._check_thread(thread_id)
|
|
209
|
+
|
|
210
|
+
try:
|
|
211
|
+
res = self._get_client().list_messages(**kwargs)
|
|
212
|
+
except DiscordMsgError as exc:
|
|
213
|
+
raise AdapterError(f"Discord list_messages failed ({exc.code}): {exc.message}") from exc
|
|
214
|
+
|
|
215
|
+
return MessageBatch(
|
|
216
|
+
channel_id=res.channel_id,
|
|
217
|
+
after=res.after,
|
|
218
|
+
cursor=res.cursor,
|
|
219
|
+
messages=tuple(_to_incoming(m) for m in res.messages),
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
def wait_for_messages(
|
|
223
|
+
self,
|
|
224
|
+
*,
|
|
225
|
+
thread_id: str,
|
|
226
|
+
after: str | None = None,
|
|
227
|
+
timeout: float = 60.0,
|
|
228
|
+
poll_interval: float = 2.0,
|
|
229
|
+
author_id: str | None = None,
|
|
230
|
+
exclude_bots: bool = False,
|
|
231
|
+
) -> WaitBatch:
|
|
232
|
+
tid = self._check_thread(thread_id)
|
|
233
|
+
try:
|
|
234
|
+
res = self._get_client().wait_for_messages(
|
|
235
|
+
thread_id=tid,
|
|
236
|
+
after=after,
|
|
237
|
+
timeout=timeout,
|
|
238
|
+
poll_interval=poll_interval,
|
|
239
|
+
author_id=author_id,
|
|
240
|
+
exclude_bots=exclude_bots,
|
|
241
|
+
)
|
|
242
|
+
except DiscordMsgError as exc:
|
|
243
|
+
raise AdapterError(
|
|
244
|
+
f"Discord wait_for_messages failed ({exc.code}): {exc.message}"
|
|
245
|
+
) from exc
|
|
246
|
+
|
|
247
|
+
return WaitBatch(
|
|
248
|
+
channel_id=res.channel_id,
|
|
249
|
+
after=res.after,
|
|
250
|
+
cursor=res.cursor,
|
|
251
|
+
messages=tuple(_to_incoming(m) for m in res.messages),
|
|
252
|
+
timed_out=res.timed_out,
|
|
253
|
+
polls=res.polls,
|
|
254
|
+
)
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
"""In-memory ConversationAdapter for deterministic offline testing."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Callable, Collection
|
|
6
|
+
from datetime import datetime, timezone
|
|
7
|
+
|
|
8
|
+
from ..errors import ChannelNotAllowedError, InvalidArgumentsError
|
|
9
|
+
from ..models import (
|
|
10
|
+
CreatedThread,
|
|
11
|
+
IncomingMessage,
|
|
12
|
+
MessageBatch,
|
|
13
|
+
PostedMessage,
|
|
14
|
+
WaitBatch,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _utc_now_iso() -> str:
|
|
19
|
+
return datetime.now(timezone.utc).isoformat()
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class InMemoryConversationAdapter:
|
|
23
|
+
"""In-memory ConversationAdapter implementing channels, threads, and cursor filtering."""
|
|
24
|
+
|
|
25
|
+
adapter_type: str = "memory"
|
|
26
|
+
|
|
27
|
+
def __init__(
|
|
28
|
+
self,
|
|
29
|
+
*,
|
|
30
|
+
bot_author_id: str = "bot-agent-office",
|
|
31
|
+
bot_author_name: str = "AgentOfficeBot",
|
|
32
|
+
allowed_channels: Collection[str] | None = None,
|
|
33
|
+
allowed_threads: Collection[str] | None = None,
|
|
34
|
+
enforce_allowlist: bool = False,
|
|
35
|
+
on_wait_poll: Callable[[int, str, str | None], None] | None = None,
|
|
36
|
+
) -> None:
|
|
37
|
+
self._bot_author_id = bot_author_id
|
|
38
|
+
self._bot_author_name = bot_author_name
|
|
39
|
+
self._enforce_allowlist = enforce_allowlist
|
|
40
|
+
self._allowed_channels: set[str] = set(allowed_channels or ())
|
|
41
|
+
self._allowed_threads: set[str] = set(allowed_threads or ())
|
|
42
|
+
self._on_wait_poll = on_wait_poll
|
|
43
|
+
self._next_id = 1000
|
|
44
|
+
self._messages: dict[str, list[IncomingMessage]] = {}
|
|
45
|
+
self._threads: dict[str, CreatedThread] = {}
|
|
46
|
+
|
|
47
|
+
def _alloc_id(self) -> str:
|
|
48
|
+
self._next_id += 1
|
|
49
|
+
return str(self._next_id)
|
|
50
|
+
|
|
51
|
+
def allow_channel(self, channel_id: str) -> None:
|
|
52
|
+
cleaned = channel_id.strip()
|
|
53
|
+
if cleaned:
|
|
54
|
+
self._allowed_channels.add(cleaned)
|
|
55
|
+
|
|
56
|
+
def allow_thread(self, thread_id: str) -> None:
|
|
57
|
+
cleaned = thread_id.strip()
|
|
58
|
+
if cleaned:
|
|
59
|
+
self._allowed_threads.add(cleaned)
|
|
60
|
+
|
|
61
|
+
def _check_channel(self, channel_id: str) -> str:
|
|
62
|
+
cleaned = channel_id.strip()
|
|
63
|
+
if not cleaned:
|
|
64
|
+
raise InvalidArgumentsError("channel_id must not be empty.")
|
|
65
|
+
if self._enforce_allowlist and cleaned not in self._allowed_channels:
|
|
66
|
+
raise ChannelNotAllowedError(f"Channel '{cleaned}' is not allowed.")
|
|
67
|
+
return cleaned
|
|
68
|
+
|
|
69
|
+
def _check_thread(self, thread_id: str) -> str:
|
|
70
|
+
cleaned = thread_id.strip()
|
|
71
|
+
if not cleaned:
|
|
72
|
+
raise InvalidArgumentsError("thread_id must not be empty.")
|
|
73
|
+
if (
|
|
74
|
+
self._enforce_allowlist
|
|
75
|
+
and cleaned not in self._allowed_threads
|
|
76
|
+
and cleaned not in self._allowed_channels
|
|
77
|
+
):
|
|
78
|
+
raise ChannelNotAllowedError(f"Thread '{cleaned}' is not allowed.")
|
|
79
|
+
return cleaned
|
|
80
|
+
|
|
81
|
+
def inject_message(
|
|
82
|
+
self,
|
|
83
|
+
target_id: str,
|
|
84
|
+
content: str,
|
|
85
|
+
*,
|
|
86
|
+
author_id: str = "user-human-1",
|
|
87
|
+
author_name: str = "Owner",
|
|
88
|
+
author_bot: bool = False,
|
|
89
|
+
referenced_message_id: str | None = None,
|
|
90
|
+
) -> IncomingMessage:
|
|
91
|
+
"""Test helper to simulate an external human or agent posting a message."""
|
|
92
|
+
msg = IncomingMessage(
|
|
93
|
+
message_id=self._alloc_id(),
|
|
94
|
+
channel_id=target_id,
|
|
95
|
+
author_id=author_id,
|
|
96
|
+
author_name=author_name,
|
|
97
|
+
author_bot=author_bot,
|
|
98
|
+
timestamp=_utc_now_iso(),
|
|
99
|
+
content=content,
|
|
100
|
+
referenced_message_id=referenced_message_id,
|
|
101
|
+
)
|
|
102
|
+
self._messages.setdefault(target_id, []).append(msg)
|
|
103
|
+
return msg
|
|
104
|
+
|
|
105
|
+
def post_message(
|
|
106
|
+
self,
|
|
107
|
+
channel_id: str,
|
|
108
|
+
content: str,
|
|
109
|
+
*,
|
|
110
|
+
allow_mentions: bool = False,
|
|
111
|
+
) -> PostedMessage:
|
|
112
|
+
_ = allow_mentions
|
|
113
|
+
cid = self._check_channel(channel_id)
|
|
114
|
+
msg = self.inject_message(
|
|
115
|
+
cid,
|
|
116
|
+
content,
|
|
117
|
+
author_id=self._bot_author_id,
|
|
118
|
+
author_name=self._bot_author_name,
|
|
119
|
+
author_bot=True,
|
|
120
|
+
)
|
|
121
|
+
return PostedMessage(
|
|
122
|
+
channel_id=cid,
|
|
123
|
+
message_id=msg.message_id,
|
|
124
|
+
timestamp=msg.timestamp,
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
def create_thread(
|
|
128
|
+
self,
|
|
129
|
+
channel_id: str,
|
|
130
|
+
message_id: str,
|
|
131
|
+
name: str,
|
|
132
|
+
) -> CreatedThread:
|
|
133
|
+
cid = self._check_channel(channel_id)
|
|
134
|
+
tid = f"9{self._alloc_id()}"
|
|
135
|
+
thread = CreatedThread(
|
|
136
|
+
thread_id=tid,
|
|
137
|
+
parent_channel_id=cid,
|
|
138
|
+
source_message_id=message_id,
|
|
139
|
+
name=name,
|
|
140
|
+
)
|
|
141
|
+
self._threads[tid] = thread
|
|
142
|
+
self._messages.setdefault(tid, [])
|
|
143
|
+
self._allowed_threads.add(tid)
|
|
144
|
+
return thread
|
|
145
|
+
|
|
146
|
+
def send_to_thread(
|
|
147
|
+
self,
|
|
148
|
+
thread_id: str,
|
|
149
|
+
content: str,
|
|
150
|
+
*,
|
|
151
|
+
allow_mentions: bool = False,
|
|
152
|
+
) -> PostedMessage:
|
|
153
|
+
_ = allow_mentions
|
|
154
|
+
tid = self._check_thread(thread_id)
|
|
155
|
+
msg = self.inject_message(
|
|
156
|
+
tid,
|
|
157
|
+
content,
|
|
158
|
+
author_id=self._bot_author_id,
|
|
159
|
+
author_name=self._bot_author_name,
|
|
160
|
+
author_bot=True,
|
|
161
|
+
)
|
|
162
|
+
return PostedMessage(
|
|
163
|
+
channel_id=tid,
|
|
164
|
+
message_id=msg.message_id,
|
|
165
|
+
timestamp=msg.timestamp,
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
def list_messages(
|
|
169
|
+
self,
|
|
170
|
+
*,
|
|
171
|
+
channel_id: str | None = None,
|
|
172
|
+
thread_id: str | None = None,
|
|
173
|
+
after: str | None = None,
|
|
174
|
+
limit: int = 50,
|
|
175
|
+
author_id: str | None = None,
|
|
176
|
+
exclude_bots: bool = False,
|
|
177
|
+
) -> MessageBatch:
|
|
178
|
+
if channel_id and thread_id:
|
|
179
|
+
raise InvalidArgumentsError("Specify either channel_id or thread_id, not both.")
|
|
180
|
+
if not channel_id and not thread_id:
|
|
181
|
+
raise InvalidArgumentsError("Either channel_id or thread_id is required.")
|
|
182
|
+
|
|
183
|
+
target_id = self._check_channel(channel_id) if channel_id else self._check_thread(thread_id or "")
|
|
184
|
+
all_msgs = list(self._messages.get(target_id, []))
|
|
185
|
+
|
|
186
|
+
after_int = int(after) if after and after.isdigit() else None
|
|
187
|
+
if after_int is not None:
|
|
188
|
+
scanned = [m for m in all_msgs if int(m.message_id) > after_int]
|
|
189
|
+
else:
|
|
190
|
+
scanned = all_msgs
|
|
191
|
+
|
|
192
|
+
cursor = after
|
|
193
|
+
if scanned:
|
|
194
|
+
cursor = scanned[-1].message_id
|
|
195
|
+
|
|
196
|
+
filtered: list[IncomingMessage] = []
|
|
197
|
+
for m in scanned:
|
|
198
|
+
if exclude_bots and m.author_bot:
|
|
199
|
+
continue
|
|
200
|
+
if author_id is not None and m.author_id != author_id:
|
|
201
|
+
continue
|
|
202
|
+
filtered.append(m)
|
|
203
|
+
if len(filtered) >= limit:
|
|
204
|
+
break
|
|
205
|
+
|
|
206
|
+
return MessageBatch(
|
|
207
|
+
channel_id=target_id,
|
|
208
|
+
after=after,
|
|
209
|
+
cursor=cursor,
|
|
210
|
+
messages=tuple(filtered),
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
def wait_for_messages(
|
|
214
|
+
self,
|
|
215
|
+
*,
|
|
216
|
+
thread_id: str,
|
|
217
|
+
after: str | None = None,
|
|
218
|
+
timeout: float = 60.0,
|
|
219
|
+
poll_interval: float = 2.0,
|
|
220
|
+
author_id: str | None = None,
|
|
221
|
+
exclude_bots: bool = False,
|
|
222
|
+
) -> WaitBatch:
|
|
223
|
+
tid = self._check_thread(thread_id)
|
|
224
|
+
max_polls = max(1, int(timeout // max(poll_interval, 0.01)) + 1) if timeout > 0 else 1
|
|
225
|
+
max_polls = min(max_polls, 5)
|
|
226
|
+
cursor = after
|
|
227
|
+
for poll in range(1, max_polls + 1):
|
|
228
|
+
if self._on_wait_poll is not None:
|
|
229
|
+
self._on_wait_poll(poll, tid, cursor)
|
|
230
|
+
batch = self.list_messages(
|
|
231
|
+
thread_id=tid,
|
|
232
|
+
after=cursor,
|
|
233
|
+
author_id=author_id,
|
|
234
|
+
exclude_bots=exclude_bots,
|
|
235
|
+
)
|
|
236
|
+
cursor = batch.cursor or cursor
|
|
237
|
+
if batch.messages:
|
|
238
|
+
return WaitBatch(
|
|
239
|
+
channel_id=tid,
|
|
240
|
+
after=after,
|
|
241
|
+
cursor=cursor,
|
|
242
|
+
messages=batch.messages,
|
|
243
|
+
timed_out=False,
|
|
244
|
+
polls=poll,
|
|
245
|
+
)
|
|
246
|
+
return WaitBatch(
|
|
247
|
+
channel_id=tid,
|
|
248
|
+
after=after,
|
|
249
|
+
cursor=cursor,
|
|
250
|
+
messages=(),
|
|
251
|
+
timed_out=True,
|
|
252
|
+
polls=max_polls,
|
|
253
|
+
)
|