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/cli.py
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
"""Command-line entry point for Agent Office."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import sys
|
|
7
|
+
from collections.abc import Sequence
|
|
8
|
+
from typing import TextIO
|
|
9
|
+
|
|
10
|
+
from . import __version__
|
|
11
|
+
from .adapters.base import ConversationAdapter
|
|
12
|
+
from .adapters.discord import DiscordConversationAdapter
|
|
13
|
+
from .errors import AgentOfficeError, ExitCode, InvalidArgumentsError
|
|
14
|
+
from .formatter import (
|
|
15
|
+
format_actor,
|
|
16
|
+
format_actors,
|
|
17
|
+
format_ask,
|
|
18
|
+
format_desk,
|
|
19
|
+
format_desks,
|
|
20
|
+
format_inbox_pull,
|
|
21
|
+
format_replies,
|
|
22
|
+
format_reply,
|
|
23
|
+
format_request,
|
|
24
|
+
format_request_detail,
|
|
25
|
+
format_requests,
|
|
26
|
+
)
|
|
27
|
+
from .models import VALID_ACTOR_ROLES, VALID_REQUEST_STATUSES
|
|
28
|
+
from .service import AgentOfficeService
|
|
29
|
+
from .store import SQLiteStore
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class _RaisingArgumentParser(argparse.ArgumentParser):
|
|
33
|
+
def error(self, message: str) -> None:
|
|
34
|
+
raise InvalidArgumentsError(message)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _add_output_options(parser: argparse.ArgumentParser) -> None:
|
|
38
|
+
parser.add_argument(
|
|
39
|
+
"--db",
|
|
40
|
+
help="Path to SQLite database file (defaults to AGENT_OFFICE_DB or OS app config path).",
|
|
41
|
+
)
|
|
42
|
+
parser.add_argument(
|
|
43
|
+
"--format",
|
|
44
|
+
choices=("table", "json"),
|
|
45
|
+
default="table",
|
|
46
|
+
help="Output format on stdout: 'table' (default) or 'json'.",
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _read_message_arg(positional: str | None, option: str | None, use_stdin: bool, stdin: TextIO) -> str:
|
|
51
|
+
if use_stdin:
|
|
52
|
+
text = stdin.read().strip()
|
|
53
|
+
if not text:
|
|
54
|
+
raise InvalidArgumentsError("Message content from stdin is empty.")
|
|
55
|
+
return text
|
|
56
|
+
text = (option if option is not None else positional) or ""
|
|
57
|
+
if not text.strip():
|
|
58
|
+
raise InvalidArgumentsError("Message content is required (positional, --message, or --stdin).")
|
|
59
|
+
return text.strip()
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
63
|
+
parser = _RaisingArgumentParser(
|
|
64
|
+
prog="agent-office",
|
|
65
|
+
description="Human-centered multi-department office workflow CLI for AI agents.",
|
|
66
|
+
)
|
|
67
|
+
parser.add_argument("--version", action="version", version=f"agent-office {__version__}")
|
|
68
|
+
|
|
69
|
+
top = parser.add_subparsers(dest="group")
|
|
70
|
+
|
|
71
|
+
# --- desk ---
|
|
72
|
+
desk_parser = top.add_parser("desk", help="Manage department desks (channels/allowlist).")
|
|
73
|
+
desk_sub = desk_parser.add_subparsers(dest="action")
|
|
74
|
+
|
|
75
|
+
desk_add = desk_sub.add_parser("add", help="Register a department desk and allow its channel.")
|
|
76
|
+
desk_add.add_argument("desk_id", help="Short desk identifier (e.g. dev, research).")
|
|
77
|
+
desk_add.add_argument("--channel", required=True, help="Target channel ID.")
|
|
78
|
+
desk_add.add_argument("--name", help="Human-readable desk name.")
|
|
79
|
+
desk_add.add_argument("--default-assignee", help="Default assignee actor_id.")
|
|
80
|
+
_add_output_options(desk_add)
|
|
81
|
+
|
|
82
|
+
desk_list = desk_sub.add_parser("list", help="List registered desks.")
|
|
83
|
+
_add_output_options(desk_list)
|
|
84
|
+
|
|
85
|
+
# --- actor ---
|
|
86
|
+
actor_parser = top.add_parser("actor", help="Manage humans, manager agents, and worker agents.")
|
|
87
|
+
actor_sub = actor_parser.add_subparsers(dest="action")
|
|
88
|
+
|
|
89
|
+
actor_add = actor_sub.add_parser("add", help="Register an actor.")
|
|
90
|
+
actor_add.add_argument("actor_id", help="Unique actor identifier (e.g. owner, dev-manager).")
|
|
91
|
+
actor_add.add_argument("--role", required=True, choices=VALID_ACTOR_ROLES, help="Actor role.")
|
|
92
|
+
actor_add.add_argument("--name", help="Display name.")
|
|
93
|
+
actor_add.add_argument("--desk", help="Affiliated desk_id.")
|
|
94
|
+
actor_add.add_argument("--external-user-id", help="External chat user ID (e.g. Discord author_id).")
|
|
95
|
+
_add_output_options(actor_add)
|
|
96
|
+
|
|
97
|
+
actor_list = actor_sub.add_parser("list", help="List registered actors.")
|
|
98
|
+
actor_list.add_argument("--desk", help="Filter by desk_id.")
|
|
99
|
+
actor_list.add_argument("--role", choices=VALID_ACTOR_ROLES, help="Filter by role.")
|
|
100
|
+
_add_output_options(actor_list)
|
|
101
|
+
|
|
102
|
+
# --- inbox ---
|
|
103
|
+
inbox_parser = top.add_parser("inbox", help="Pull incoming requests from a desk channel.")
|
|
104
|
+
inbox_sub = inbox_parser.add_subparsers(dest="action")
|
|
105
|
+
|
|
106
|
+
inbox_pull = inbox_sub.add_parser("pull", help="Fetch new channel messages and issue REQ-IDs.")
|
|
107
|
+
inbox_pull.add_argument("--desk", required=True, help="Target desk_id.")
|
|
108
|
+
inbox_pull.add_argument("--prefix", help="Only import messages starting with this prefix.")
|
|
109
|
+
inbox_pull.add_argument("--limit", type=int, default=50, help="Maximum messages to pull.")
|
|
110
|
+
inbox_pull.add_argument("--include-bots", action="store_true", help="Also import bot messages.")
|
|
111
|
+
_add_output_options(inbox_pull)
|
|
112
|
+
|
|
113
|
+
# --- req ---
|
|
114
|
+
req_parser = top.add_parser("req", help="Manage requests, questions, replies, and status.")
|
|
115
|
+
req_sub = req_parser.add_subparsers(dest="action")
|
|
116
|
+
|
|
117
|
+
req_create = req_sub.add_parser("create", help="Create a new request (or sub-request).")
|
|
118
|
+
req_create.add_argument("--desk", required=True, help="Target desk_id.")
|
|
119
|
+
req_create.add_argument("--title", required=True, help="Short request title.")
|
|
120
|
+
req_create.add_argument("--body", default="", help="Detailed request description.")
|
|
121
|
+
req_create.add_argument("--parent", help="Parent REQ-ID for sub-tasks / delegation.")
|
|
122
|
+
req_create.add_argument("--requester", help="Requester actor_id.")
|
|
123
|
+
req_create.add_argument("--assignee", help="Assignee actor_id.")
|
|
124
|
+
req_create.add_argument("--post", action="store_true", help="Also post the request to the desk channel.")
|
|
125
|
+
req_create.add_argument("--allow-mentions", action="store_true", help="Allow Discord mentions.")
|
|
126
|
+
_add_output_options(req_create)
|
|
127
|
+
|
|
128
|
+
req_list = req_sub.add_parser("list", help="List requests.")
|
|
129
|
+
req_list.add_argument("--desk", help="Filter by desk_id.")
|
|
130
|
+
req_list.add_argument("--status", choices=VALID_REQUEST_STATUSES, help="Filter by status.")
|
|
131
|
+
req_list.add_argument("--parent", help="Filter by parent REQ-ID.")
|
|
132
|
+
req_list.add_argument("--assignee", help="Filter by assignee actor_id.")
|
|
133
|
+
req_list.add_argument("--limit", type=int, default=50, help="Maximum requests to show.")
|
|
134
|
+
_add_output_options(req_list)
|
|
135
|
+
|
|
136
|
+
req_show = req_sub.add_parser("show", help="Show request details and event history.")
|
|
137
|
+
req_show.add_argument("req_id", help="REQ-ID (e.g. REQ-0001).")
|
|
138
|
+
req_show.add_argument("--events", type=int, default=50, help="Maximum events to include.")
|
|
139
|
+
_add_output_options(req_show)
|
|
140
|
+
|
|
141
|
+
req_ask = req_sub.add_parser("ask", help="Create thread if needed and post a question.")
|
|
142
|
+
req_ask.add_argument("req_id", help="REQ-ID.")
|
|
143
|
+
req_ask.add_argument("text", nargs="?", help="Question text.")
|
|
144
|
+
req_ask.add_argument("-m", "--message", help="Question text.")
|
|
145
|
+
req_ask.add_argument("--stdin", action="store_true", help="Read question text from stdin.")
|
|
146
|
+
req_ask.add_argument("--actor", help="Sending actor_id.")
|
|
147
|
+
req_ask.add_argument("--thread-name", help="Custom thread name when creating a new thread.")
|
|
148
|
+
req_ask.add_argument("--allow-mentions", action="store_true", help="Allow Discord mentions.")
|
|
149
|
+
_add_output_options(req_ask)
|
|
150
|
+
|
|
151
|
+
req_read = req_sub.add_parser("read", help="Fetch new replies in the request thread.")
|
|
152
|
+
req_read.add_argument("req_id", help="REQ-ID.")
|
|
153
|
+
req_read.add_argument("--author-id", help="Filter by specific external author_id.")
|
|
154
|
+
req_read.add_argument("--any-user", action="store_true", help="Include messages from any user/bot.")
|
|
155
|
+
req_read.add_argument("--limit", type=int, default=50, help="Maximum replies to fetch.")
|
|
156
|
+
_add_output_options(req_read)
|
|
157
|
+
|
|
158
|
+
req_wait = req_sub.add_parser("wait", help="Wait for new replies in the request thread.")
|
|
159
|
+
req_wait.add_argument("req_id", help="REQ-ID.")
|
|
160
|
+
req_wait.add_argument("--timeout", type=float, default=60.0, help="Timeout in seconds (default: 60).")
|
|
161
|
+
req_wait.add_argument("--poll-interval", type=float, default=2.0, help="Polling interval in seconds (default: 2).")
|
|
162
|
+
req_wait.add_argument("--author-id", help="Filter by specific external author_id.")
|
|
163
|
+
req_wait.add_argument("--any-user", action="store_true", help="Include messages from any user/bot.")
|
|
164
|
+
_add_output_options(req_wait)
|
|
165
|
+
|
|
166
|
+
req_reply = req_sub.add_parser("reply", help="Post a reply, progress update, or final report.")
|
|
167
|
+
req_reply.add_argument("req_id", help="REQ-ID.")
|
|
168
|
+
req_reply.add_argument("text", nargs="?", help="Reply text.")
|
|
169
|
+
req_reply.add_argument("-m", "--message", help="Reply text.")
|
|
170
|
+
req_reply.add_argument("--stdin", action="store_true", help="Read reply text from stdin.")
|
|
171
|
+
req_reply.add_argument("--actor", help="Sending actor_id.")
|
|
172
|
+
req_reply.add_argument("--status", choices=VALID_REQUEST_STATUSES, help="Also update request status.")
|
|
173
|
+
req_reply.add_argument("--allow-mentions", action="store_true", help="Allow Discord mentions.")
|
|
174
|
+
_add_output_options(req_reply)
|
|
175
|
+
|
|
176
|
+
req_status = req_sub.add_parser("status", help="Update request status.")
|
|
177
|
+
req_status.add_argument("req_id", help="REQ-ID.")
|
|
178
|
+
req_status.add_argument("status", choices=VALID_REQUEST_STATUSES, help="New status.")
|
|
179
|
+
req_status.add_argument("--actor", help="Actor making the change.")
|
|
180
|
+
req_status.add_argument("--note", default="", help="Optional note for the audit log.")
|
|
181
|
+
_add_output_options(req_status)
|
|
182
|
+
|
|
183
|
+
return parser
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def run_cli(
|
|
187
|
+
argv: Sequence[str] | None = None,
|
|
188
|
+
*,
|
|
189
|
+
adapter: ConversationAdapter | None = None,
|
|
190
|
+
stdout: TextIO | None = None,
|
|
191
|
+
stderr: TextIO | None = None,
|
|
192
|
+
stdin: TextIO | None = None,
|
|
193
|
+
) -> int:
|
|
194
|
+
out_stream = stdout if stdout is not None else sys.stdout
|
|
195
|
+
err_stream = stderr if stderr is not None else sys.stderr
|
|
196
|
+
in_stream = stdin if stdin is not None else sys.stdin
|
|
197
|
+
|
|
198
|
+
parser = build_parser()
|
|
199
|
+
try:
|
|
200
|
+
args = parser.parse_args(list(argv) if argv is not None else None)
|
|
201
|
+
if not args.group or not getattr(args, "action", None):
|
|
202
|
+
parser.print_help(file=err_stream)
|
|
203
|
+
return int(ExitCode.INVALID_ARGS)
|
|
204
|
+
|
|
205
|
+
resolved_adapter: ConversationAdapter = (
|
|
206
|
+
adapter if adapter is not None else DiscordConversationAdapter()
|
|
207
|
+
)
|
|
208
|
+
fmt = args.format
|
|
209
|
+
|
|
210
|
+
with SQLiteStore(args.db) as store:
|
|
211
|
+
svc = AgentOfficeService(store, resolved_adapter)
|
|
212
|
+
|
|
213
|
+
if args.group == "desk":
|
|
214
|
+
if args.action == "add":
|
|
215
|
+
desk = svc.add_desk(
|
|
216
|
+
args.desk_id,
|
|
217
|
+
args.channel,
|
|
218
|
+
name=args.name,
|
|
219
|
+
default_assignee_id=args.default_assignee,
|
|
220
|
+
)
|
|
221
|
+
print(format_desk(desk, fmt), file=out_stream)
|
|
222
|
+
return int(ExitCode.OK)
|
|
223
|
+
if args.action == "list":
|
|
224
|
+
print(format_desks(svc.list_desks(), fmt), file=out_stream)
|
|
225
|
+
return int(ExitCode.OK)
|
|
226
|
+
|
|
227
|
+
if args.group == "actor":
|
|
228
|
+
if args.action == "add":
|
|
229
|
+
actor = svc.add_actor(
|
|
230
|
+
args.actor_id,
|
|
231
|
+
args.role,
|
|
232
|
+
display_name=args.name,
|
|
233
|
+
desk_id=args.desk,
|
|
234
|
+
external_user_id=args.external_user_id,
|
|
235
|
+
)
|
|
236
|
+
print(format_actor(actor, fmt), file=out_stream)
|
|
237
|
+
return int(ExitCode.OK)
|
|
238
|
+
if args.action == "list":
|
|
239
|
+
print(
|
|
240
|
+
format_actors(svc.list_actors(desk_id=args.desk, role=args.role), fmt),
|
|
241
|
+
file=out_stream,
|
|
242
|
+
)
|
|
243
|
+
return int(ExitCode.OK)
|
|
244
|
+
|
|
245
|
+
if args.group == "inbox" and args.action == "pull":
|
|
246
|
+
pull_res = svc.pull_inbox(
|
|
247
|
+
args.desk,
|
|
248
|
+
prefix=args.prefix,
|
|
249
|
+
limit=args.limit,
|
|
250
|
+
exclude_bots=not bool(args.include_bots),
|
|
251
|
+
)
|
|
252
|
+
print(format_inbox_pull(pull_res, fmt), file=out_stream)
|
|
253
|
+
return int(ExitCode.OK)
|
|
254
|
+
|
|
255
|
+
if args.group == "req":
|
|
256
|
+
if args.action == "create":
|
|
257
|
+
req = svc.create_request(
|
|
258
|
+
args.desk,
|
|
259
|
+
args.title,
|
|
260
|
+
args.body,
|
|
261
|
+
parent_req_id=args.parent,
|
|
262
|
+
requester_actor_id=args.requester,
|
|
263
|
+
assignee_actor_id=args.assignee,
|
|
264
|
+
post_to_channel=bool(args.post),
|
|
265
|
+
allow_mentions=bool(args.allow_mentions),
|
|
266
|
+
)
|
|
267
|
+
print(format_request(req, fmt), file=out_stream)
|
|
268
|
+
return int(ExitCode.OK)
|
|
269
|
+
|
|
270
|
+
if args.action == "list":
|
|
271
|
+
reqs = svc.list_requests(
|
|
272
|
+
desk_id=args.desk,
|
|
273
|
+
status=args.status,
|
|
274
|
+
parent_req_id=args.parent,
|
|
275
|
+
assignee_actor_id=args.assignee,
|
|
276
|
+
limit=args.limit,
|
|
277
|
+
)
|
|
278
|
+
print(format_requests(reqs, fmt), file=out_stream)
|
|
279
|
+
return int(ExitCode.OK)
|
|
280
|
+
|
|
281
|
+
if args.action == "show":
|
|
282
|
+
req, events = svc.get_request_detail(args.req_id, event_limit=args.events)
|
|
283
|
+
print(format_request_detail(req, events, fmt), file=out_stream)
|
|
284
|
+
return int(ExitCode.OK)
|
|
285
|
+
|
|
286
|
+
if args.action == "ask":
|
|
287
|
+
content = _read_message_arg(args.text, args.message, bool(args.stdin), in_stream)
|
|
288
|
+
ask_res = svc.ask(
|
|
289
|
+
args.req_id,
|
|
290
|
+
content,
|
|
291
|
+
actor_id=args.actor,
|
|
292
|
+
thread_name=args.thread_name,
|
|
293
|
+
allow_mentions=bool(args.allow_mentions),
|
|
294
|
+
)
|
|
295
|
+
print(format_ask(ask_res, fmt), file=out_stream)
|
|
296
|
+
return int(ExitCode.OK)
|
|
297
|
+
|
|
298
|
+
if args.action == "read":
|
|
299
|
+
read_res = svc.read_replies(
|
|
300
|
+
args.req_id,
|
|
301
|
+
any_user=bool(args.any_user),
|
|
302
|
+
author_id=args.author_id,
|
|
303
|
+
limit=args.limit,
|
|
304
|
+
)
|
|
305
|
+
print(format_replies(read_res, fmt), file=out_stream)
|
|
306
|
+
return int(ExitCode.OK)
|
|
307
|
+
|
|
308
|
+
if args.action == "wait":
|
|
309
|
+
wait_res = svc.wait_for_answer(
|
|
310
|
+
args.req_id,
|
|
311
|
+
timeout=args.timeout,
|
|
312
|
+
poll_interval=args.poll_interval,
|
|
313
|
+
any_user=bool(args.any_user),
|
|
314
|
+
author_id=args.author_id,
|
|
315
|
+
)
|
|
316
|
+
print(format_replies(wait_res, fmt), file=out_stream)
|
|
317
|
+
return int(ExitCode.OK)
|
|
318
|
+
|
|
319
|
+
if args.action == "reply":
|
|
320
|
+
content = _read_message_arg(args.text, args.message, bool(args.stdin), in_stream)
|
|
321
|
+
reply_res = svc.reply(
|
|
322
|
+
args.req_id,
|
|
323
|
+
content,
|
|
324
|
+
actor_id=args.actor,
|
|
325
|
+
status=args.status,
|
|
326
|
+
allow_mentions=bool(args.allow_mentions),
|
|
327
|
+
)
|
|
328
|
+
print(format_reply(reply_res, fmt), file=out_stream)
|
|
329
|
+
return int(ExitCode.OK)
|
|
330
|
+
|
|
331
|
+
if args.action == "status":
|
|
332
|
+
updated, _ = svc.set_status(
|
|
333
|
+
args.req_id,
|
|
334
|
+
args.status,
|
|
335
|
+
actor_id=args.actor,
|
|
336
|
+
note=args.note,
|
|
337
|
+
)
|
|
338
|
+
print(format_request(updated, fmt), file=out_stream)
|
|
339
|
+
return int(ExitCode.OK)
|
|
340
|
+
|
|
341
|
+
raise InvalidArgumentsError("Unknown command.")
|
|
342
|
+
except AgentOfficeError as exc:
|
|
343
|
+
print(f"[agent-office] error ({exc.code}): {exc.message}", file=err_stream)
|
|
344
|
+
return exc.exit_code
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def main() -> None:
|
|
348
|
+
sys.exit(run_cli())
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
if __name__ == "__main__":
|
|
352
|
+
main()
|
agent_office/errors.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""Domain errors and exit codes for Agent Office."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from enum import IntEnum
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ExitCode(IntEnum):
|
|
9
|
+
OK = 0
|
|
10
|
+
INVALID_ARGS = 2
|
|
11
|
+
NOT_FOUND = 3
|
|
12
|
+
CONFLICT = 4
|
|
13
|
+
PERMISSION_DENIED = 5
|
|
14
|
+
ADAPTER_ERROR = 6
|
|
15
|
+
STORAGE_ERROR = 7
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class AgentOfficeError(Exception):
|
|
19
|
+
"""Base domain error with stable error code and CLI exit code."""
|
|
20
|
+
|
|
21
|
+
code: str = "ERROR"
|
|
22
|
+
exit_code: int = int(ExitCode.STORAGE_ERROR)
|
|
23
|
+
|
|
24
|
+
def __init__(
|
|
25
|
+
self,
|
|
26
|
+
message: str,
|
|
27
|
+
*,
|
|
28
|
+
code: str | None = None,
|
|
29
|
+
exit_code: int | None = None,
|
|
30
|
+
) -> None:
|
|
31
|
+
super().__init__(message)
|
|
32
|
+
self.message = message
|
|
33
|
+
if code is not None:
|
|
34
|
+
self.code = code
|
|
35
|
+
if exit_code is not None:
|
|
36
|
+
self.exit_code = int(exit_code)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class InvalidArgumentsError(AgentOfficeError):
|
|
40
|
+
code = "INVALID_ARGS"
|
|
41
|
+
exit_code = int(ExitCode.INVALID_ARGS)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class NotFoundError(AgentOfficeError):
|
|
45
|
+
code = "NOT_FOUND"
|
|
46
|
+
exit_code = int(ExitCode.NOT_FOUND)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class ConflictError(AgentOfficeError):
|
|
50
|
+
code = "CONFLICT"
|
|
51
|
+
exit_code = int(ExitCode.CONFLICT)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class ChannelNotAllowedError(AgentOfficeError):
|
|
55
|
+
code = "PERMISSION_DENIED"
|
|
56
|
+
exit_code = int(ExitCode.PERMISSION_DENIED)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class AdapterError(AgentOfficeError):
|
|
60
|
+
code = "ADAPTER_ERROR"
|
|
61
|
+
exit_code = int(ExitCode.ADAPTER_ERROR)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class StorageError(AgentOfficeError):
|
|
65
|
+
code = "STORAGE_ERROR"
|
|
66
|
+
exit_code = int(ExitCode.STORAGE_ERROR)
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"""Table and JSON output formatters for Agent Office CLI."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from .models import (
|
|
9
|
+
Actor,
|
|
10
|
+
AskResult,
|
|
11
|
+
Desk,
|
|
12
|
+
InboxPullResult,
|
|
13
|
+
RepliesResult,
|
|
14
|
+
ReplyResult,
|
|
15
|
+
Request,
|
|
16
|
+
RequestEvent,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _dump_json(payload: dict[str, Any]) -> str:
|
|
21
|
+
return json.dumps(payload, ensure_ascii=False, indent=2)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _render_table(headers: tuple[str, ...], rows: list[tuple[str, ...]], empty_msg: str) -> str:
|
|
25
|
+
if not rows:
|
|
26
|
+
return empty_msg
|
|
27
|
+
widths = [
|
|
28
|
+
max(len(headers[i]), *(len(row[i]) for row in rows))
|
|
29
|
+
for i in range(len(headers))
|
|
30
|
+
]
|
|
31
|
+
sep = "+-" + "-+-".join("-" * w for w in widths) + "-+"
|
|
32
|
+
lines = [
|
|
33
|
+
sep,
|
|
34
|
+
"| " + " | ".join(headers[i].ljust(widths[i]) for i in range(len(headers))) + " |",
|
|
35
|
+
sep,
|
|
36
|
+
]
|
|
37
|
+
for row in rows:
|
|
38
|
+
lines.append("| " + " | ".join(row[i].ljust(widths[i]) for i in range(len(headers))) + " |")
|
|
39
|
+
lines.append(sep)
|
|
40
|
+
return "\n".join(lines)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def format_desks(desks: list[Desk], fmt: str) -> str:
|
|
44
|
+
if fmt == "json":
|
|
45
|
+
return _dump_json({"ok": True, "count": len(desks), "desks": [d.to_dict() for d in desks]})
|
|
46
|
+
rows = [
|
|
47
|
+
(d.desk_id, d.name, d.adapter_type, d.channel_id, d.inbox_cursor or "-")
|
|
48
|
+
for d in desks
|
|
49
|
+
]
|
|
50
|
+
return _render_table(("Desk ID", "Name", "Adapter", "Channel ID", "Inbox Cursor"), rows, "No desks registered.")
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def format_desk(desk: Desk, fmt: str) -> str:
|
|
54
|
+
if fmt == "json":
|
|
55
|
+
return _dump_json({"ok": True, "desk": desk.to_dict()})
|
|
56
|
+
return "\n".join(
|
|
57
|
+
[
|
|
58
|
+
f"Desk ID : {desk.desk_id}",
|
|
59
|
+
f"Name : {desk.name}",
|
|
60
|
+
f"Adapter : {desk.adapter_type}",
|
|
61
|
+
f"Channel ID : {desk.channel_id}",
|
|
62
|
+
f"Inbox Cursor : {desk.inbox_cursor or '-'}",
|
|
63
|
+
]
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def format_actors(actors: list[Actor], fmt: str) -> str:
|
|
68
|
+
if fmt == "json":
|
|
69
|
+
return _dump_json({"ok": True, "count": len(actors), "actors": [a.to_dict() for a in actors]})
|
|
70
|
+
rows = [
|
|
71
|
+
(a.actor_id, a.display_name, a.role, a.desk_id or "-", a.external_user_id or "-")
|
|
72
|
+
for a in actors
|
|
73
|
+
]
|
|
74
|
+
return _render_table(("Actor ID", "Name", "Role", "Desk", "External ID"), rows, "No actors registered.")
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def format_actor(actor: Actor, fmt: str) -> str:
|
|
78
|
+
if fmt == "json":
|
|
79
|
+
return _dump_json({"ok": True, "actor": actor.to_dict()})
|
|
80
|
+
return "\n".join(
|
|
81
|
+
[
|
|
82
|
+
f"Actor ID : {actor.actor_id}",
|
|
83
|
+
f"Name : {actor.display_name}",
|
|
84
|
+
f"Role : {actor.role}",
|
|
85
|
+
f"Desk : {actor.desk_id or '-'}",
|
|
86
|
+
f"External ID : {actor.external_user_id or '-'}",
|
|
87
|
+
]
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def format_inbox_pull(result: InboxPullResult, fmt: str) -> str:
|
|
92
|
+
if fmt == "json":
|
|
93
|
+
return _dump_json(result.to_dict())
|
|
94
|
+
lines = [
|
|
95
|
+
f"Desk ID : {result.desk_id}",
|
|
96
|
+
f"Channel ID : {result.channel_id}",
|
|
97
|
+
f"Cursor : {result.cursor or '-'}",
|
|
98
|
+
f"Created : {len(result.created_requests)}",
|
|
99
|
+
f"Skipped : {result.skipped_count}",
|
|
100
|
+
]
|
|
101
|
+
for req in result.created_requests:
|
|
102
|
+
lines.append(f" - {req.req_id} [{req.status}] {req.title}")
|
|
103
|
+
return "\n".join(lines)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def format_requests(requests: list[Request], fmt: str) -> str:
|
|
107
|
+
if fmt == "json":
|
|
108
|
+
return _dump_json({"ok": True, "count": len(requests), "requests": [r.to_dict() for r in requests]})
|
|
109
|
+
rows = [
|
|
110
|
+
(
|
|
111
|
+
r.req_id,
|
|
112
|
+
r.desk_id,
|
|
113
|
+
r.status,
|
|
114
|
+
r.parent_req_id or "-",
|
|
115
|
+
r.assignee_actor_id or "-",
|
|
116
|
+
r.title[:40],
|
|
117
|
+
)
|
|
118
|
+
for r in requests
|
|
119
|
+
]
|
|
120
|
+
return _render_table(
|
|
121
|
+
("REQ ID", "Desk", "Status", "Parent", "Assignee", "Title"),
|
|
122
|
+
rows,
|
|
123
|
+
"No requests found.",
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def format_request(req: Request, fmt: str) -> str:
|
|
128
|
+
if fmt == "json":
|
|
129
|
+
return _dump_json({"ok": True, "request": req.to_dict()})
|
|
130
|
+
return "\n".join(
|
|
131
|
+
[
|
|
132
|
+
f"REQ ID : {req.req_id}",
|
|
133
|
+
f"Parent REQ : {req.parent_req_id or '-'}",
|
|
134
|
+
f"Desk : {req.desk_id}",
|
|
135
|
+
f"Status : {req.status}",
|
|
136
|
+
f"Title : {req.title}",
|
|
137
|
+
f"Requester : {req.requester_actor_id or req.requester_external_id or '-'}",
|
|
138
|
+
f"Assignee : {req.assignee_actor_id or '-'}",
|
|
139
|
+
f"Channel ID : {req.channel_id}",
|
|
140
|
+
f"Thread ID : {req.thread_id or '-'}",
|
|
141
|
+
f"Thread Cursor : {req.thread_cursor or '-'}",
|
|
142
|
+
]
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def format_request_detail(req: Request, events: list[RequestEvent], fmt: str) -> str:
|
|
147
|
+
if fmt == "json":
|
|
148
|
+
return _dump_json(
|
|
149
|
+
{
|
|
150
|
+
"ok": True,
|
|
151
|
+
"request": req.to_dict(),
|
|
152
|
+
"events": [e.to_dict() for e in events],
|
|
153
|
+
}
|
|
154
|
+
)
|
|
155
|
+
lines = [format_request(req, "table"), "", "Body:", req.body, "", f"Events ({len(events)}):"]
|
|
156
|
+
for ev in events:
|
|
157
|
+
who = ev.actor_id or ev.external_user_id or "-"
|
|
158
|
+
snippet = ev.content.replace("\n", " ")[:70]
|
|
159
|
+
lines.append(f" #{ev.event_id} [{ev.event_type}] ({who}) {snippet}")
|
|
160
|
+
return "\n".join(lines)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def format_ask(result: AskResult, fmt: str) -> str:
|
|
164
|
+
if fmt == "json":
|
|
165
|
+
return _dump_json(result.to_dict())
|
|
166
|
+
return "\n".join(
|
|
167
|
+
[
|
|
168
|
+
f"REQ ID : {result.request.req_id}",
|
|
169
|
+
f"Status : {result.request.status}",
|
|
170
|
+
f"Thread ID : {result.request.thread_id}",
|
|
171
|
+
f"Thread Created : {result.thread_created}",
|
|
172
|
+
f"Message ID : {result.posted.message_id}",
|
|
173
|
+
f"Thread Cursor : {result.request.thread_cursor}",
|
|
174
|
+
]
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def format_replies(result: RepliesResult, fmt: str) -> str:
|
|
179
|
+
if fmt == "json":
|
|
180
|
+
return _dump_json(result.to_dict())
|
|
181
|
+
lines = [
|
|
182
|
+
f"REQ ID : {result.request.req_id}",
|
|
183
|
+
f"Thread ID : {result.request.thread_id}",
|
|
184
|
+
f"Timed Out : {result.timed_out}",
|
|
185
|
+
f"Cursor : {result.cursor or '-'}",
|
|
186
|
+
f"Messages : {len(result.messages)}",
|
|
187
|
+
]
|
|
188
|
+
for msg in result.messages:
|
|
189
|
+
lines.append(f" - [{msg.message_id}] {msg.author_name}: {msg.content}")
|
|
190
|
+
return "\n".join(lines)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def format_reply(result: ReplyResult, fmt: str) -> str:
|
|
194
|
+
if fmt == "json":
|
|
195
|
+
return _dump_json(result.to_dict())
|
|
196
|
+
return "\n".join(
|
|
197
|
+
[
|
|
198
|
+
f"REQ ID : {result.request.req_id}",
|
|
199
|
+
f"Status : {result.request.status}",
|
|
200
|
+
f"Thread ID : {result.request.thread_id}",
|
|
201
|
+
f"Message ID : {result.posted.message_id}",
|
|
202
|
+
f"Thread Cursor : {result.request.thread_cursor}",
|
|
203
|
+
]
|
|
204
|
+
)
|