trefoil-cli 0.0.2__tar.gz
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.
- trefoil_cli-0.0.2/PKG-INFO +6 -0
- trefoil_cli-0.0.2/package.py +547 -0
- trefoil_cli-0.0.2/pyproject.toml +16 -0
- trefoil_cli-0.0.2/setup.cfg +4 -0
- trefoil_cli-0.0.2/trefoil_cli.egg-info/PKG-INFO +6 -0
- trefoil_cli-0.0.2/trefoil_cli.egg-info/SOURCES.txt +8 -0
- trefoil_cli-0.0.2/trefoil_cli.egg-info/dependency_links.txt +1 -0
- trefoil_cli-0.0.2/trefoil_cli.egg-info/entry_points.txt +2 -0
- trefoil_cli-0.0.2/trefoil_cli.egg-info/requires.txt +1 -0
- trefoil_cli-0.0.2/trefoil_cli.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,547 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Bundle a trefoil config.yml into a single JSON file.
|
|
3
|
+
|
|
4
|
+
Resolves:
|
|
5
|
+
- `!Ref file://path` and `!Ref https://url` tags
|
|
6
|
+
- bare `file://path` strings (bare https:// strings are left as-is)
|
|
7
|
+
|
|
8
|
+
Referenced .yml/.yaml/.json files are parsed and resolved recursively;
|
|
9
|
+
a referenced folder becomes {relative path: contents} and a .zip a
|
|
10
|
+
base64 data: URL (the two ways to package a local skill); anything else is
|
|
11
|
+
inlined as text. Relative paths resolve against the
|
|
12
|
+
directory of the file that references them.
|
|
13
|
+
|
|
14
|
+
Usage: trefoil <domain> <command> [args]
|
|
15
|
+
|
|
16
|
+
trefoil config package path/to/config.yml [-o bundle.json]
|
|
17
|
+
trefoil config sync [path/to/config.yml] [--server URL]
|
|
18
|
+
trefoil agents list
|
|
19
|
+
trefoil agents build --agent=NAME [-d] [--dockerfile]
|
|
20
|
+
trefoil agents get-bundle --agent=NAME [-o FILE]
|
|
21
|
+
trefoil agents run --agent=NAME [TASK | --input=JSON|file://PATH] [-d]
|
|
22
|
+
trefoil sessions status|logs|result --agent=NAME ID
|
|
23
|
+
trefoil sessions artifacts --agent=NAME ID [-o DIR]
|
|
24
|
+
trefoil jobs list [--agent=NAME] [--status=STATUS]
|
|
25
|
+
trefoil jobs status ID [-f] [--logs]
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
import argparse
|
|
29
|
+
import base64
|
|
30
|
+
import getpass
|
|
31
|
+
import json
|
|
32
|
+
import os
|
|
33
|
+
import sys
|
|
34
|
+
import time
|
|
35
|
+
import urllib.error
|
|
36
|
+
import urllib.request
|
|
37
|
+
from urllib.parse import quote, urlencode
|
|
38
|
+
from pathlib import Path
|
|
39
|
+
|
|
40
|
+
import yaml
|
|
41
|
+
|
|
42
|
+
DEFAULT_SERVER = "http://localhost:8080"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class Ref:
|
|
46
|
+
def __init__(self, url):
|
|
47
|
+
self.url = url
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class Loader(yaml.SafeLoader):
|
|
51
|
+
pass
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
Loader.add_constructor("!Ref", lambda loader, node: Ref(loader.construct_scalar(node)))
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class Packager:
|
|
58
|
+
def __init__(self):
|
|
59
|
+
self.errors = []
|
|
60
|
+
self.stack = [] # files currently being resolved, for cycle detection
|
|
61
|
+
|
|
62
|
+
def resolve(self, value, base):
|
|
63
|
+
if isinstance(value, dict):
|
|
64
|
+
return {k: self.resolve(v, base) for k, v in value.items()}
|
|
65
|
+
if isinstance(value, list):
|
|
66
|
+
return [self.resolve(v, base) for v in value]
|
|
67
|
+
if isinstance(value, Ref):
|
|
68
|
+
return self.load(value.url, base)
|
|
69
|
+
if isinstance(value, str) and value.startswith("file://"):
|
|
70
|
+
return self.load(value, base)
|
|
71
|
+
return value
|
|
72
|
+
|
|
73
|
+
def load(self, url, base):
|
|
74
|
+
try:
|
|
75
|
+
if url.startswith("file://"):
|
|
76
|
+
path = (base / url.removeprefix("file://")).resolve()
|
|
77
|
+
if path in self.stack:
|
|
78
|
+
raise ValueError(f"circular reference: {path}")
|
|
79
|
+
if path.is_dir():
|
|
80
|
+
return read_folder(path)
|
|
81
|
+
if path.suffix == ".zip":
|
|
82
|
+
return "data:application/zip;base64," + base64.b64encode(path.read_bytes()).decode()
|
|
83
|
+
text = path.read_text()
|
|
84
|
+
name, next_base, key = path.name, path.parent, path
|
|
85
|
+
elif url.startswith(("http://", "https://")):
|
|
86
|
+
with urllib.request.urlopen(url, timeout=30) as resp:
|
|
87
|
+
text = resp.read().decode()
|
|
88
|
+
name, next_base, key = url, base, url
|
|
89
|
+
else:
|
|
90
|
+
raise ValueError(f"unsupported ref: {url}")
|
|
91
|
+
except Exception as e:
|
|
92
|
+
self.errors.append(f"{url}: {e}")
|
|
93
|
+
return None
|
|
94
|
+
|
|
95
|
+
if name.endswith((".yml", ".yaml", ".json")):
|
|
96
|
+
self.stack.append(key)
|
|
97
|
+
try:
|
|
98
|
+
return self.resolve(yaml.load(text, Loader=Loader), next_base)
|
|
99
|
+
finally:
|
|
100
|
+
self.stack.pop()
|
|
101
|
+
return text
|
|
102
|
+
|
|
103
|
+
def package(self, config_path):
|
|
104
|
+
config_path = config_path.resolve()
|
|
105
|
+
self.stack.append(config_path)
|
|
106
|
+
return self.resolve(yaml.load(config_path.read_text(), Loader=Loader), config_path.parent)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def read_folder(root):
|
|
110
|
+
"""Every file under root as {relative path: text}, skipping hidden files
|
|
111
|
+
and __pycache__, like shared bricks on the server."""
|
|
112
|
+
files = {}
|
|
113
|
+
for path in sorted(root.rglob("*")):
|
|
114
|
+
rel = path.relative_to(root)
|
|
115
|
+
if any(p.startswith(".") or p == "__pycache__" for p in rel.parts) or not path.is_file():
|
|
116
|
+
continue
|
|
117
|
+
try:
|
|
118
|
+
files[rel.as_posix()] = path.read_text()
|
|
119
|
+
except UnicodeDecodeError:
|
|
120
|
+
raise ValueError(f"{rel}: only text files are supported")
|
|
121
|
+
return files
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def load_bundle(config_path):
|
|
125
|
+
if not config_path.is_file():
|
|
126
|
+
sys.exit(f"trefoil: config not found: {config_path}")
|
|
127
|
+
packager = Packager()
|
|
128
|
+
bundle = packager.package(config_path)
|
|
129
|
+
if packager.errors:
|
|
130
|
+
print("failed to resolve:", file=sys.stderr)
|
|
131
|
+
for err in packager.errors:
|
|
132
|
+
print(f" {err}", file=sys.stderr)
|
|
133
|
+
sys.exit(1)
|
|
134
|
+
return bundle
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def cmd_package(args):
|
|
138
|
+
out = json.dumps(load_bundle(args.config), indent=2)
|
|
139
|
+
if args.output:
|
|
140
|
+
args.output.write_text(out + "\n")
|
|
141
|
+
else:
|
|
142
|
+
print(out)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def api_request(server, method, path, body=None, timeout=30, raw=False):
|
|
146
|
+
"""Calls the API and returns the decoded JSON, or the bytes with raw=True."""
|
|
147
|
+
req = urllib.request.Request(server.rstrip("/") + path, data=body, method=method)
|
|
148
|
+
if body is not None:
|
|
149
|
+
req.add_header("Content-Type", "application/json")
|
|
150
|
+
try:
|
|
151
|
+
with urllib.request.urlopen(req, timeout=timeout) as resp:
|
|
152
|
+
data = resp.read()
|
|
153
|
+
return data if raw else json.loads(data)
|
|
154
|
+
except urllib.error.HTTPError as e:
|
|
155
|
+
try:
|
|
156
|
+
msg = json.loads(e.read()).get("error", e.reason)
|
|
157
|
+
except ValueError:
|
|
158
|
+
# Our handlers always answer in JSON; a plain-text 404 means the
|
|
159
|
+
# server has no such route, usually because it's an older build.
|
|
160
|
+
if e.code == 404:
|
|
161
|
+
msg = f"{method} {path} not found on {server}; is the server up to date? (restart `make dev`)"
|
|
162
|
+
else:
|
|
163
|
+
msg = e.reason
|
|
164
|
+
sys.exit(f"trefoil: {msg}")
|
|
165
|
+
except urllib.error.URLError as e:
|
|
166
|
+
sys.exit(f"trefoil: cannot reach server at {server}: {e.reason}")
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def cmd_sync(args):
|
|
170
|
+
bundle = load_bundle(args.config)
|
|
171
|
+
result = api_request(args.server, "POST", "/conf", json.dumps(bundle).encode())
|
|
172
|
+
print(f"synced {args.config} to {args.server} ({len(bundle.get('agents', []))} agents)")
|
|
173
|
+
dockerfiles = [f for f in result.get("files", []) if f.endswith("/Dockerfile")]
|
|
174
|
+
for f in dockerfiles:
|
|
175
|
+
print(f" generated {f}")
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def follow_job(server, job_id, interval=1.0):
|
|
179
|
+
"""Streams a job's logs to stderr until it finishes; exits 1 if it failed."""
|
|
180
|
+
printed = 0
|
|
181
|
+
while True:
|
|
182
|
+
job = api_request(server, "GET", f"/jobs/{quote(job_id)}")
|
|
183
|
+
sys.stderr.write(job["logs"][printed:])
|
|
184
|
+
sys.stderr.flush()
|
|
185
|
+
printed = len(job["logs"])
|
|
186
|
+
if job["status"] in ("succeeded", "failed"):
|
|
187
|
+
break
|
|
188
|
+
time.sleep(interval)
|
|
189
|
+
if job["status"] == "failed":
|
|
190
|
+
sys.exit(f"trefoil: job {job_id} failed: {job.get('error', '')}")
|
|
191
|
+
print(f"job {job_id} succeeded: {job['agent']} -> {job['image']}")
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def cmd_agents_build(args):
|
|
195
|
+
job = api_request(args.server, "POST", f"/agents/{quote(args.agent)}/build", b"")
|
|
196
|
+
if "id" not in job:
|
|
197
|
+
# Servers from before build jobs built synchronously and returned no job id.
|
|
198
|
+
sys.exit(f"trefoil: server at {args.server} doesn't return build jobs; is it up to date? (restart `make dev`)")
|
|
199
|
+
print(f"job {job['id']}: building {job['agent']} -> {job['image']}", file=sys.stderr)
|
|
200
|
+
if args.dockerfile:
|
|
201
|
+
print(job["dockerfile"], file=sys.stderr)
|
|
202
|
+
if args.detach:
|
|
203
|
+
print(job["id"])
|
|
204
|
+
return
|
|
205
|
+
follow_job(args.server, job["id"])
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def cmd_agents_context(args):
|
|
209
|
+
data = api_request(args.server, "GET", f"/agents/{quote(args.agent)}/context", timeout=300, raw=True)
|
|
210
|
+
output = args.output or Path(f"{args.agent}-context.zip")
|
|
211
|
+
output.write_bytes(data)
|
|
212
|
+
print(output)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def cmd_jobs_status(args):
|
|
216
|
+
if args.follow:
|
|
217
|
+
follow_job(args.server, args.id)
|
|
218
|
+
return
|
|
219
|
+
job = api_request(args.server, "GET", f"/jobs/{quote(args.id)}")
|
|
220
|
+
print(f"job {job['id']} {job['kind']} {job['agent']} {job['status']}")
|
|
221
|
+
if job.get("context_dir"):
|
|
222
|
+
print(f"context: {job['context_dir']}")
|
|
223
|
+
if job.get("error"):
|
|
224
|
+
print(f"error: {job['error']}")
|
|
225
|
+
if args.logs:
|
|
226
|
+
sys.stdout.write(job["logs"])
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def cmd_jobs_list(args):
|
|
230
|
+
rows = [(j["id"], j["kind"], j["agent"], j["status"], j["created_at"][:19].replace("T", " "))
|
|
231
|
+
for j in api_request(args.server, "GET", "/jobs?" + urlencode(
|
|
232
|
+
{k: v for k, v in (("agent", args.agent), ("status", args.status)) if v}))]
|
|
233
|
+
print_table(("ID", "KIND", "AGENT", "STATUS", "CREATED"), rows, empty="no jobs")
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def read_input(value):
|
|
237
|
+
"""--input value: file://path to a JSON file, or inline JSON."""
|
|
238
|
+
if value.startswith("file://"):
|
|
239
|
+
path = Path(value.removeprefix("file://")).expanduser()
|
|
240
|
+
try:
|
|
241
|
+
value = path.read_text()
|
|
242
|
+
except OSError as e:
|
|
243
|
+
sys.exit(f"trefoil: --input: {e}")
|
|
244
|
+
try:
|
|
245
|
+
data = json.loads(value)
|
|
246
|
+
except ValueError as e:
|
|
247
|
+
sys.exit(f"trefoil: --input is not valid JSON: {e}")
|
|
248
|
+
if not isinstance(data, dict):
|
|
249
|
+
sys.exit("trefoil: --input must be a JSON object")
|
|
250
|
+
return data
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def cmd_agents_run(args):
|
|
254
|
+
if args.input is not None:
|
|
255
|
+
if args.task is not None:
|
|
256
|
+
sys.exit("trefoil: pass either a task or --input, not both")
|
|
257
|
+
body = json.dumps(read_input(args.input)).encode()
|
|
258
|
+
else:
|
|
259
|
+
task = args.task if args.task is not None else sys.stdin.read()
|
|
260
|
+
body = json.dumps({"task": task}).encode()
|
|
261
|
+
started = api_request(args.server, "POST", f"/agents/{quote(args.agent)}/run", body)
|
|
262
|
+
if "session_id" not in started:
|
|
263
|
+
sys.exit(f"trefoil: server at {args.server} doesn't return sessions; is it up to date? (restart `make dev`)")
|
|
264
|
+
session_id = started["session_id"]
|
|
265
|
+
print(f"session {session_id}: running {args.agent}", file=sys.stderr)
|
|
266
|
+
if args.detach:
|
|
267
|
+
print(session_id)
|
|
268
|
+
return
|
|
269
|
+
follow_session(args.server, args.agent, session_id)
|
|
270
|
+
print_result(args.server, args.agent, session_id)
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def cmd_secrets_set(args):
|
|
274
|
+
# Read from a prompt or stdin, never argv, so the value stays out of shell history.
|
|
275
|
+
if sys.stdin.isatty():
|
|
276
|
+
value = getpass.getpass(f"{args.name}: ")
|
|
277
|
+
else:
|
|
278
|
+
value = sys.stdin.read().rstrip("\n")
|
|
279
|
+
if not value:
|
|
280
|
+
sys.exit("trefoil: secret value is empty")
|
|
281
|
+
api_request(args.server, "POST", f"/secret/{quote(args.name)}", json.dumps({"value": value}).encode())
|
|
282
|
+
print(f"stored secret {args.name} on {args.server}", file=sys.stderr)
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def cmd_params_list(args):
|
|
286
|
+
params = api_request(args.server, "GET", "/params")
|
|
287
|
+
rows = [
|
|
288
|
+
(p["name"], p["type"] + ("" if p["required"] else "?"),
|
|
289
|
+
json.dumps(p["value"]) if "value" in p else "-",
|
|
290
|
+
p["source"] or ("missing" if p["required"] else "not set"), ", ".join(p["agents"]))
|
|
291
|
+
for p in params
|
|
292
|
+
]
|
|
293
|
+
print_table(("NAME", "TYPE", "VALUE", "SOURCE", "USED BY"), rows, empty="no params")
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def cmd_params_set(args):
|
|
297
|
+
# Sent as a string; the server parses it by the param's type.
|
|
298
|
+
value = args.value if args.value is not None else sys.stdin.read().rstrip("\n")
|
|
299
|
+
api_request(args.server, "POST", f"/param/{quote(args.name)}", json.dumps({"value": value}).encode())
|
|
300
|
+
print(f"set param {args.name} on {args.server}", file=sys.stderr)
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def cmd_params_unset(args):
|
|
304
|
+
api_request(args.server, "DELETE", f"/param/{quote(args.name)}")
|
|
305
|
+
print(f"unset param {args.name} on {args.server}; it falls back to the config's default", file=sys.stderr)
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def session_path(agent, session_id, what):
|
|
309
|
+
return f"/agents/{quote(agent)}/sessions/{quote(session_id)}/{what}"
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
# The harness's timeline events ("x-trefoil-event: <type> <json>") are for
|
|
313
|
+
# the dashboard; the CLI shows the human-readable lines around them.
|
|
314
|
+
EVENT_PREFIX = "x-trefoil-event: "
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def strip_events(logs):
|
|
318
|
+
return "".join(line for line in logs.splitlines(keepends=True) if not line.startswith(EVENT_PREFIX))
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def follow_session(server, agent, session_id, interval=1.0):
|
|
322
|
+
"""Streams a session's logs to stderr until it finishes."""
|
|
323
|
+
offset, partial = 0, ""
|
|
324
|
+
while True:
|
|
325
|
+
page = api_request(server, "GET", session_path(agent, session_id, "logs") + f"?offset={offset}")
|
|
326
|
+
# Hold back an unfinished last line until it is complete.
|
|
327
|
+
text = partial + page["logs"]
|
|
328
|
+
done = page["status"] != "running"
|
|
329
|
+
cut = len(text) if done else text.rfind("\n") + 1
|
|
330
|
+
text, partial = text[:cut], text[cut:]
|
|
331
|
+
sys.stderr.write(strip_events(text))
|
|
332
|
+
sys.stderr.flush()
|
|
333
|
+
offset = page["next_offset"]
|
|
334
|
+
if done:
|
|
335
|
+
return
|
|
336
|
+
time.sleep(interval)
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
def print_result(server, agent, session_id):
|
|
340
|
+
"""Prints a finished session's answer to stdout and exits with its code."""
|
|
341
|
+
status = api_request(server, "GET", session_path(agent, session_id, "status"))
|
|
342
|
+
if status.get("exit_code") is None:
|
|
343
|
+
sys.exit(f"trefoil: session {session_id} failed: {status.get('error', '')}")
|
|
344
|
+
result = api_request(server, "GET", session_path(agent, session_id, "result"))
|
|
345
|
+
if "output" in result:
|
|
346
|
+
print(json.dumps(result["output"], indent=2))
|
|
347
|
+
else:
|
|
348
|
+
sys.stdout.write(result["stdout"])
|
|
349
|
+
if status.get("error"):
|
|
350
|
+
print(f"trefoil: session {session_id}: {status['error']}", file=sys.stderr)
|
|
351
|
+
if result.get("artifacts"):
|
|
352
|
+
names = ", ".join(a["path"] for a in result["artifacts"])
|
|
353
|
+
print(f"artifacts: {names} (download: trefoil sessions artifacts --agent={agent} {session_id} -o DIR)",
|
|
354
|
+
file=sys.stderr)
|
|
355
|
+
sys.exit(result["exit_code"])
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
def cmd_sessions_status(args):
|
|
359
|
+
status = api_request(args.server, "GET", session_path(args.agent, args.id, "status"))
|
|
360
|
+
line = f"session {status['id']} {status['agent']} {status['status']}"
|
|
361
|
+
if status.get("exit_code") is not None:
|
|
362
|
+
line += f" exit {status['exit_code']}"
|
|
363
|
+
print(line)
|
|
364
|
+
if status.get("error"):
|
|
365
|
+
print(f"error: {status['error']}")
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def cmd_sessions_logs(args):
|
|
369
|
+
if args.output is not None:
|
|
370
|
+
data = api_request(args.server, "GET", session_path(args.agent, args.id, "logs") + "?download=1",
|
|
371
|
+
timeout=300, raw=True)
|
|
372
|
+
args.output.write_bytes(data)
|
|
373
|
+
print(args.output)
|
|
374
|
+
return
|
|
375
|
+
if args.follow:
|
|
376
|
+
follow_session(args.server, args.agent, args.id)
|
|
377
|
+
return
|
|
378
|
+
sys.stdout.write(strip_events(api_request(args.server, "GET", session_path(args.agent, args.id, "logs"))["logs"]))
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
def cmd_sessions_result(args):
|
|
382
|
+
print_result(args.server, args.agent, args.id)
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
def cmd_sessions_stop(args):
|
|
386
|
+
api_request(args.server, "POST", session_path(args.agent, args.id, "stop"))
|
|
387
|
+
print(f"stopping session {args.id}; it finishes as failed with error \"stopped\"", file=sys.stderr)
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
def cmd_sessions_artifacts(args):
|
|
391
|
+
artifacts = api_request(args.server, "GET", session_path(args.agent, args.id, "artifacts"))["artifacts"]
|
|
392
|
+
if args.output is None:
|
|
393
|
+
print_table(("PATH", "SIZE"), [(a["path"], str(a["size"])) for a in artifacts], empty="no artifacts")
|
|
394
|
+
return
|
|
395
|
+
for a in artifacts:
|
|
396
|
+
# The server only lists relative paths, but don't trust it to write outside DIR.
|
|
397
|
+
dest = (args.output / a["path"]).resolve()
|
|
398
|
+
if not dest.is_relative_to(args.output.resolve()):
|
|
399
|
+
sys.exit(f"trefoil: refusing artifact path outside {args.output}: {a['path']}")
|
|
400
|
+
data = api_request(args.server, "GET", session_path(args.agent, args.id, "artifacts/" + quote(a["path"])),
|
|
401
|
+
timeout=300, raw=True)
|
|
402
|
+
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
403
|
+
dest.write_bytes(data)
|
|
404
|
+
print(dest)
|
|
405
|
+
if not artifacts:
|
|
406
|
+
print("no artifacts", file=sys.stderr)
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
def cmd_agents_list(args):
|
|
410
|
+
agents = api_request(args.server, "GET", "/agents")
|
|
411
|
+
|
|
412
|
+
rows = [
|
|
413
|
+
(a["name"], a.get("model") or "(default)", a.get("provider") or "anthropic", ", ".join(a["bricks"]),
|
|
414
|
+
format_input_schema(a.get("input_schema")), format_fields(a.get("outputs")), str(a["prompts"]),
|
|
415
|
+
"yes" if a["built"] else "no")
|
|
416
|
+
for a in agents
|
|
417
|
+
]
|
|
418
|
+
print_table(("NAME", "MODEL", "PROVIDER", "BRICKS", "INPUT", "OUTPUTS", "PROMPTS", "BUILT"), rows,
|
|
419
|
+
empty="no agents")
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
def format_input_schema(schema):
|
|
423
|
+
"""{"properties": {"repo": {"type": "string"}}, "required": ["repo"]} -> "repo: string"."""
|
|
424
|
+
schema = schema or {}
|
|
425
|
+
required = schema.get("required") or []
|
|
426
|
+
out = []
|
|
427
|
+
for name, prop in (schema.get("properties") or {}).items():
|
|
428
|
+
typ = prop.get("type") if isinstance(prop.get("type"), str) else "any"
|
|
429
|
+
if typ == "array" and isinstance((prop.get("items") or {}).get("type"), str):
|
|
430
|
+
typ = prop["items"]["type"] + "[]"
|
|
431
|
+
out.append(f"{name}: {typ}" + ("" if name in required else "?"))
|
|
432
|
+
return ", ".join(out)
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
def format_fields(fields):
|
|
436
|
+
"""[{"statement": "string"}] -> "statement: string"."""
|
|
437
|
+
return ", ".join(f"{k}: {v}" for f in fields or [] for k, v in f.items())
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
def print_table(header, rows, empty):
|
|
441
|
+
if not rows:
|
|
442
|
+
print(empty)
|
|
443
|
+
return
|
|
444
|
+
widths = [max(len(r[c]) for r in [header, *rows]) for c in range(len(header))]
|
|
445
|
+
for row in [header, *rows]:
|
|
446
|
+
print(" ".join(cell.ljust(w) for cell, w in zip(row, widths)).rstrip())
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
def main():
|
|
450
|
+
parser = argparse.ArgumentParser(prog="trefoil", description="trefoil <domain> <command> [args]")
|
|
451
|
+
domains = parser.add_subparsers(dest="domain", metavar="<domain>", required=True)
|
|
452
|
+
|
|
453
|
+
server = argparse.ArgumentParser(add_help=False)
|
|
454
|
+
server.add_argument("--server", default=os.environ.get("TREFOIL_SERVER", DEFAULT_SERVER),
|
|
455
|
+
help=f"API URL (default: $TREFOIL_SERVER or {DEFAULT_SERVER})")
|
|
456
|
+
agent = argparse.ArgumentParser(add_help=False)
|
|
457
|
+
agent.add_argument("--agent", required=True, metavar="NAME", help="agent name")
|
|
458
|
+
|
|
459
|
+
def domain(name, help_text):
|
|
460
|
+
return domains.add_parser(name, help=help_text).add_subparsers(
|
|
461
|
+
dest="command", metavar="<command>", required=True)
|
|
462
|
+
|
|
463
|
+
config = domain("config", "package and upload agent configs")
|
|
464
|
+
package = config.add_parser("package", help=__doc__.splitlines()[0])
|
|
465
|
+
package.add_argument("config", type=Path, help="path to config.yml")
|
|
466
|
+
package.add_argument("-o", "--output", type=Path, help="output file (default: stdout)")
|
|
467
|
+
package.set_defaults(func=cmd_package)
|
|
468
|
+
sync = config.add_parser("sync", parents=[server], help="package a config and upload it to the server")
|
|
469
|
+
sync.add_argument("config", type=Path, nargs="?", default=Path("config.yml"),
|
|
470
|
+
help="path to config.yml (default: ./config.yml)")
|
|
471
|
+
sync.set_defaults(func=cmd_sync)
|
|
472
|
+
|
|
473
|
+
agents = domain("agents", "work with agents on the server")
|
|
474
|
+
agents_list = agents.add_parser("list", parents=[server], help="list agents in the uploaded config")
|
|
475
|
+
agents_list.set_defaults(func=cmd_agents_list)
|
|
476
|
+
|
|
477
|
+
agents_build = agents.add_parser("build", parents=[server, agent], help="build an agent's Docker image on the server")
|
|
478
|
+
agents_build.add_argument("-d", "--detach", action="store_true",
|
|
479
|
+
help="print the job id and return instead of following the build")
|
|
480
|
+
agents_build.add_argument("--dockerfile", action="store_true", help="print the generated Dockerfile")
|
|
481
|
+
agents_build.set_defaults(func=cmd_agents_build)
|
|
482
|
+
|
|
483
|
+
agents_context = agents.add_parser("get-bundle", aliases=["context"], parents=[server, agent],
|
|
484
|
+
help="download an agent's build context (Dockerfile and files) as a zip")
|
|
485
|
+
agents_context.add_argument("-o", "--output", type=Path, metavar="FILE",
|
|
486
|
+
help="where to save the zip (default: ./NAME-context.zip)")
|
|
487
|
+
agents_context.set_defaults(func=cmd_agents_context)
|
|
488
|
+
|
|
489
|
+
agents_run = agents.add_parser("run", parents=[server, agent], help="run a built agent with a task")
|
|
490
|
+
agents_run.add_argument("task", nargs="?", help="task for the agent (default: read from stdin)")
|
|
491
|
+
agents_run.add_argument("--input", metavar="JSON|file://PATH",
|
|
492
|
+
help="structured input for agents with an input_schema: inline JSON or file://path.json")
|
|
493
|
+
agents_run.add_argument("-d", "--detach", action="store_true",
|
|
494
|
+
help="print the session id and return instead of following the run")
|
|
495
|
+
agents_run.set_defaults(func=cmd_agents_run)
|
|
496
|
+
|
|
497
|
+
sessions = domain("sessions", "follow agent runs on the server")
|
|
498
|
+
for name, func, help_text in (("status", cmd_sessions_status, "show a session's status"),
|
|
499
|
+
("logs", cmd_sessions_logs, "print a session's logs"),
|
|
500
|
+
("result", cmd_sessions_result, "print a finished session's answer"),
|
|
501
|
+
("stop", cmd_sessions_stop, "stop a running session"),
|
|
502
|
+
("artifacts", cmd_sessions_artifacts, "list or download a session's artifacts")):
|
|
503
|
+
sub = sessions.add_parser(name, parents=[server, agent], help=help_text)
|
|
504
|
+
sub.add_argument("id", help="session id")
|
|
505
|
+
sub.set_defaults(func=func)
|
|
506
|
+
if name == "logs":
|
|
507
|
+
sub.add_argument("-f", "--follow", action="store_true", help="stream logs until the session finishes")
|
|
508
|
+
sub.add_argument("-o", "--output", type=Path, metavar="FILE",
|
|
509
|
+
help="save the full log, timeline events included, to FILE")
|
|
510
|
+
if name == "artifacts":
|
|
511
|
+
sub.add_argument("-o", "--output", type=Path, metavar="DIR",
|
|
512
|
+
help="download every artifact into DIR instead of listing them")
|
|
513
|
+
|
|
514
|
+
secrets = domain("secrets", "provide secrets that agents' bricks require")
|
|
515
|
+
secrets_set = secrets.add_parser("set", parents=[server],
|
|
516
|
+
help="store a secret on the server (value from a prompt or stdin)")
|
|
517
|
+
secrets_set.add_argument("--name", required=True, metavar="NAME", help="secret name, e.g. github_token")
|
|
518
|
+
secrets_set.set_defaults(func=cmd_secrets_set)
|
|
519
|
+
|
|
520
|
+
params = domain("params", "set the config-wide values agents' inputs default to")
|
|
521
|
+
params_list = params.add_parser("list", parents=[server], help="list the config's params and their values")
|
|
522
|
+
params_list.set_defaults(func=cmd_params_list)
|
|
523
|
+
params_set = params.add_parser("set", parents=[server], help="set a param (value as an argument or on stdin)")
|
|
524
|
+
params_set.add_argument("--name", required=True, metavar="NAME", help="param name, e.g. repo")
|
|
525
|
+
params_set.add_argument("value", nargs="?", help="value, parsed by the param's type (JSON for lists and objects)")
|
|
526
|
+
params_set.set_defaults(func=cmd_params_set)
|
|
527
|
+
params_unset = params.add_parser("unset", parents=[server], help="unset a param so it uses the config's default")
|
|
528
|
+
params_unset.add_argument("--name", required=True, metavar="NAME", help="param name")
|
|
529
|
+
params_unset.set_defaults(func=cmd_params_unset)
|
|
530
|
+
|
|
531
|
+
jobs = domain("jobs", "track background jobs on the server")
|
|
532
|
+
jobs_list = jobs.add_parser("list", parents=[server], help="list jobs, newest first")
|
|
533
|
+
jobs_list.add_argument("--agent", metavar="NAME", help="only jobs for this agent")
|
|
534
|
+
jobs_list.add_argument("--status", choices=["queued", "running", "succeeded", "failed"])
|
|
535
|
+
jobs_list.set_defaults(func=cmd_jobs_list)
|
|
536
|
+
jobs_status = jobs.add_parser("status", parents=[server], help="show a job's status")
|
|
537
|
+
jobs_status.add_argument("id", help="job id")
|
|
538
|
+
jobs_status.add_argument("-f", "--follow", action="store_true", help="stream logs until the job finishes")
|
|
539
|
+
jobs_status.add_argument("--logs", action="store_true", help="print the logs so far")
|
|
540
|
+
jobs_status.set_defaults(func=cmd_jobs_status)
|
|
541
|
+
|
|
542
|
+
args = parser.parse_args()
|
|
543
|
+
args.func(args)
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
if __name__ == "__main__":
|
|
547
|
+
main()
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "trefoil-cli"
|
|
3
|
+
version = "0.0.2"
|
|
4
|
+
description = "CLI for the trefoil agent server"
|
|
5
|
+
requires-python = ">=3.10"
|
|
6
|
+
dependencies = ["pyyaml>=6"]
|
|
7
|
+
|
|
8
|
+
[project.scripts]
|
|
9
|
+
trefoil = "package:main"
|
|
10
|
+
|
|
11
|
+
[build-system]
|
|
12
|
+
requires = ["setuptools>=61"]
|
|
13
|
+
build-backend = "setuptools.build_meta"
|
|
14
|
+
|
|
15
|
+
[tool.setuptools]
|
|
16
|
+
py-modules = ["package"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pyyaml>=6
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
package
|