-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcli.py
More file actions
312 lines (268 loc) · 11.9 KB
/
Copy pathcli.py
File metadata and controls
312 lines (268 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
from __future__ import annotations
import argparse
import asyncio
import os
import sys
from collections.abc import Sequence
from a2a.types import TaskState
from pydantic import ValidationError
from . import __version__
from .client import A2AClient, load_settings
from .config import Settings
from .server.application import main as serve_main
CLI_BRAND_BANNER = (
" ██████╗ ██████╗ ███████╗███╗ ██╗ ██████╗ ██████╗"
" ██████╗ ███████╗ █████╗ ██████╗ █████╗ \n"
"██╔═══██╗██╔══██╗██╔════╝████╗ ██║██╔════╝██╔═══██╗"
"██╔══██╗██╔════╝ ██╔══██╗╚════██╗██╔══██╗\n"
"██║ ██║██████╔╝█████╗ ██╔██╗ ██║██║ ██║ ██║"
"██║ ██║█████╗█████╗███████║ █████╔╝███████║\n"
"██║ ██║██╔═══╝ ██╔══╝ ██║╚██╗██║██║ ██║ ██║"
"██║ ██║██╔══╝╚════╝██╔══██║██╔═══╝ ██╔══██║\n"
"╚██████╔╝██║ ███████╗██║ ╚████║╚██████╗╚██████╔╝"
"██████╔╝███████╗ ██║ ██║███████╗██║ ██║\n"
" ╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═══╝ ╚═════╝ ╚═════╝"
" ╚═════╝ ╚══════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝"
)
PROJECT_REPOSITORY_URL = "https://github.com/Intelligent-Internet/opencode-a2a"
HELP_FLAGS = frozenset({"-h", "--help"})
ROOT_DESCRIPTION = (
"OpenCode A2A runtime for explicit service startup and peer calls. "
"A2A Protocol 1.0 only.\n"
" opencode-a2a <command> [arguments] [options]"
)
OPENCODE_SETUP_HELP = (
"OpenCode upstream quick start:\n"
" opencode auth login\n"
" opencode models\n"
" opencode serve --hostname 127.0.0.1 --port 4096\n"
"\n"
"OpenCode note:\n"
" Configure provider auth and a default model before starting opencode serve.\n"
" If provider auth comes from environment variables, export them before launch."
)
SERVE_ENVIRONMENT_HELP = (
"Serve required environment:\n"
" A2A_STATIC_AUTH_CREDENTIALS\n"
" JSON array with at least one enabled bearer/basic credential.\n"
"\n"
"Serve common environment:\n"
" OPENCODE_BASE_URL\n"
" Upstream opencode serve URL. Default: http://127.0.0.1:4096\n"
" A2A_HOST\n"
" Bind host. Default: 127.0.0.1\n"
" A2A_PORT\n"
" Bind port. Default: 8000\n"
" A2A_PUBLIC_URL\n"
" Public base URL advertised in the agent card. Default: http://127.0.0.1:8000\n"
" A2A_TASK_STORE_BACKEND\n"
" database or memory. Default: database\n"
" A2A_TASK_STORE_DATABASE_URL\n"
" SQLAlchemy database URL. Default: sqlite+aiosqlite:///./opencode-a2a.db\n"
" OPENCODE_WORKSPACE_ROOT\n"
" Workspace root exposed to OpenCode tool execution.\n"
"\n"
"Serve minimal example:\n"
" DEMO_BEARER_TOKEN=\"$(python3 -c 'import secrets; print(secrets.token_hex(24))')\"\n"
" A2A_STATIC_AUTH_CREDENTIALS="
'\'[{"scheme":"bearer","token":"\'"${DEMO_BEARER_TOKEN}"\'","principal":"automation"}]\''
" \\\n"
" OPENCODE_BASE_URL=http://127.0.0.1:4096 \\\n"
" opencode-a2a serve\n"
"\n"
"Serve durable SQLite example:\n"
" DEMO_BEARER_TOKEN=\"$(python3 -c 'import secrets; print(secrets.token_hex(24))')\"\n"
" A2A_STATIC_AUTH_CREDENTIALS="
'\'[{"scheme":"bearer","token":"\'"${DEMO_BEARER_TOKEN}"\'","principal":"automation"}]\''
" \\\n"
" OPENCODE_BASE_URL=http://127.0.0.1:4096 \\\n"
" A2A_TASK_STORE_DATABASE_URL=sqlite+aiosqlite:///./opencode-a2a.db \\\n"
" OPENCODE_WORKSPACE_ROOT=/abs/path/to/workspace \\\n"
" opencode-a2a serve"
)
CALL_HELP = (
"Call examples:\n"
" A2A_CLIENT_BEARER_TOKEN=peer-token \\\n"
' opencode-a2a call http://other-agent:8000/.well-known/agent-card.json "How are you?"\n'
"\n"
' A2A_CLIENT_BASIC_AUTH="user:pass" \\\n'
' opencode-a2a call http://other-agent:8000/.well-known/agent-card.json "How are you?"\n'
"\n"
"Call note:\n"
" Outbound peer credentials are read from environment variables only.\n"
" Service base URLs also work, but card URLs are the preferred example form."
)
ROOT_HELP_EPILOG = "Command-specific help:\n opencode-a2a serve --help\n opencode-a2a call --help"
SERVE_HELP_EPILOG = f"{OPENCODE_SETUP_HELP}\n\n{SERVE_ENVIRONMENT_HELP}"
class RootHelpFormatter(
argparse.RawDescriptionHelpFormatter,
argparse.ArgumentDefaultsHelpFormatter,
):
"""Preserve banner formatting while keeping argparse defaults."""
class TopLevelArgumentParser(argparse.ArgumentParser):
"""Drop the generated usage line from the top-level help output only."""
def format_help(self) -> str:
help_text = super().format_help()
lines = help_text.splitlines(keepends=True)
if lines and lines[0].startswith("usage:"):
help_text = "".join(lines[1:]).lstrip("\n")
return help_text.replace("\ncommands:\n command\n", "\ncommands:\n", 1)
def _find_subparser(
parser: argparse.ArgumentParser,
name: str,
) -> argparse.ArgumentParser | None:
for action in parser._actions:
if isinstance(action, argparse._SubParsersAction):
return action.choices.get(name)
return None
def _format_settings_errors(exc: ValidationError) -> list[str]:
errors: list[str] = []
for error in exc.errors(include_url=False):
message = str(error.get("msg", "Invalid configuration"))
if message.startswith("Value error, "):
message = message.removeprefix("Value error, ")
location = ".".join(str(part) for part in error.get("loc", ()) if str(part) != "__root__")
errors.append(f"{location}: {message}" if location else message)
return errors or [str(exc)]
def validate_serve_configuration() -> list[str]:
try:
Settings()
except ValidationError as exc:
return _format_settings_errors(exc)
return []
def print_help_with_details(
parser: argparse.ArgumentParser,
*,
errors: Sequence[str] = (),
) -> None:
parser.print_help()
if errors:
print("\nconfiguration errors:")
for error in errors:
print(f" - {error}")
def should_show_call_help(args: Sequence[str]) -> bool:
if not args or args[0] != "call":
return False
if any(token in HELP_FLAGS for token in args[1:]):
return False
positional_count = sum(1 for token in args[1:] if not token.startswith("-"))
return positional_count < 2
async def run_call(agent_url: str, text: str) -> int:
settings = load_settings(os.environ)
client = A2AClient(agent_url, settings=settings)
rendered_artifacts: dict[str, str] = {}
try:
async for event in client.send_message(text):
if event.HasField("message"):
for part in event.message.parts:
text_val = part.text if part.HasField("text") else None
if isinstance(text_val, str):
print(text_val, end="", flush=True)
elif event.HasField("artifact_update"):
artifact = event.artifact_update.artifact
if artifact and artifact.parts:
artifact_id = artifact.artifact_id or ""
for part in artifact.parts:
text_val = part.text if part.HasField("text") else None
if isinstance(text_val, str):
if event.artifact_update.append:
rendered_artifacts[artifact_id] = (
f"{rendered_artifacts.get(artifact_id, '')}{text_val}"
)
print(text_val, end="", flush=True)
continue
previous = rendered_artifacts.get(artifact_id, "")
rendered_artifacts[artifact_id] = text_val
if text_val == previous:
continue
if previous and text_val.startswith(previous):
print(text_val[len(previous) :], end="", flush=True)
continue
print(text_val, end="", flush=True)
elif (
event.HasField("status_update")
and event.status_update.status
and event.status_update.status.state == TaskState.TASK_STATE_FAILED
):
print(f"\n[Failed] {event.status_update.status.message or ''}")
print() # New line after completion
except Exception as exc:
print(f"\n[Error] {exc}", file=sys.stderr)
return 1
finally:
await client.close()
return 0
def build_parser() -> argparse.ArgumentParser:
parser = TopLevelArgumentParser(
prog="opencode-a2a",
description=(
CLI_BRAND_BANNER
+ "\n\n"
+ f"repo: {PROJECT_REPOSITORY_URL}\n"
+ "uv tool install --upgrade opencode-a2a\n\n"
+ ROOT_DESCRIPTION
),
formatter_class=RootHelpFormatter,
epilog=ROOT_HELP_EPILOG,
)
parser.add_argument(
"-v",
"--version",
action="version",
version=f"%(prog)s {__version__}",
help="show program's version number and exit",
)
subparsers = parser.add_subparsers(
dest="command",
title="commands",
metavar="command",
parser_class=argparse.ArgumentParser,
)
subparsers.add_parser(
"serve",
help="Run the A2A service.",
description="Run the OpenCode A2A service. A2A Protocol 1.0 only.",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=SERVE_HELP_EPILOG,
)
call_parser = subparsers.add_parser(
"call",
help="Call an A2A agent.",
description="Call an A2A agent using the A2A protocol. A2A Protocol 1.0 only.",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=CALL_HELP,
)
call_parser.add_argument(
"agent_url",
help="Agent card URL or service base URL of the agent to call.",
)
call_parser.add_argument("text", help="Text message to send.")
return parser
def main(argv: Sequence[str] | None = None) -> int:
args = list(sys.argv[1:] if argv is None else argv)
parser = build_parser()
call_parser = _find_subparser(parser, "call")
serve_parser = _find_subparser(parser, "serve")
if not args:
print_help_with_details(parser)
return 0
if should_show_call_help(args) and call_parser is not None:
print_help_with_details(call_parser)
return 0
namespace = parser.parse_args(args)
if namespace.command == "serve":
configuration_errors = validate_serve_configuration()
if configuration_errors and serve_parser is not None:
print_help_with_details(serve_parser, errors=configuration_errors)
return 0
serve_main()
return 0
if namespace.command == "call":
return asyncio.run(run_call(namespace.agent_url, namespace.text))
if namespace.command is None:
print_help_with_details(parser)
return 0
parser.error(f"Unknown command: {namespace.command}")
return 2
if __name__ == "__main__":
raise SystemExit(main())