trefoil-cli 0.6.0__tar.gz → 0.7.0__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.6.0 → trefoil_cli-0.7.0}/PKG-INFO +1 -1
- {trefoil_cli-0.6.0 → trefoil_cli-0.7.0}/package.py +175 -6
- {trefoil_cli-0.6.0 → trefoil_cli-0.7.0}/pyproject.toml +1 -1
- {trefoil_cli-0.6.0 → trefoil_cli-0.7.0}/trefoil_cli.egg-info/PKG-INFO +1 -1
- {trefoil_cli-0.6.0 → trefoil_cli-0.7.0}/setup.cfg +0 -0
- {trefoil_cli-0.6.0 → trefoil_cli-0.7.0}/trefoil_cli.egg-info/SOURCES.txt +0 -0
- {trefoil_cli-0.6.0 → trefoil_cli-0.7.0}/trefoil_cli.egg-info/dependency_links.txt +0 -0
- {trefoil_cli-0.6.0 → trefoil_cli-0.7.0}/trefoil_cli.egg-info/entry_points.txt +0 -0
- {trefoil_cli-0.6.0 → trefoil_cli-0.7.0}/trefoil_cli.egg-info/requires.txt +0 -0
- {trefoil_cli-0.6.0 → trefoil_cli-0.7.0}/trefoil_cli.egg-info/top_level.txt +0 -0
|
@@ -16,14 +16,16 @@ Usage: trefoil <domain> <command> [args]
|
|
|
16
16
|
trefoil init [DIR] [--example=NAME]
|
|
17
17
|
trefoil update
|
|
18
18
|
trefoil engine start [--settings=backend.yml] [--port=8080] [--foreground]
|
|
19
|
+
[--setup] [--provider=bedrock|anthropic] [--aws-profile=P] [--aws-region=R]
|
|
19
20
|
trefoil engine stop
|
|
20
21
|
trefoil config package path/to/config.yml [-o bundle.json]
|
|
21
22
|
trefoil config sync [path/to/config.yml] [--server URL]
|
|
22
23
|
trefoil agents list
|
|
23
24
|
trefoil agents build --agent=NAME [-d] [--dockerfile]
|
|
24
|
-
trefoil agents get-
|
|
25
|
+
trefoil agents get-dockerfile --agent=NAME [-o FILE]
|
|
25
26
|
trefoil agents run --agent=NAME [TASK | --input=JSON|file://PATH] [-d]
|
|
26
27
|
trefoil sessions status|logs|result --agent=NAME ID
|
|
28
|
+
trefoil sessions rerun --agent=NAME ID [-d]
|
|
27
29
|
trefoil sessions artifacts --agent=NAME ID [-o DIR]
|
|
28
30
|
trefoil jobs list [--agent=NAME] [--status=STATUS]
|
|
29
31
|
trefoil jobs status ID [-f] [--logs]
|
|
@@ -49,6 +51,7 @@ import yaml
|
|
|
49
51
|
|
|
50
52
|
DEFAULT_SERVER = "http://localhost:8080"
|
|
51
53
|
EXAMPLES_API = "https://api.github.com/repos/alto-se/trefoil/contents/examples"
|
|
54
|
+
ENGINE_IMAGE = "ghcr.io/alto-se/trefoil-api:latest"
|
|
52
55
|
|
|
53
56
|
|
|
54
57
|
class Ref:
|
|
@@ -201,9 +204,147 @@ def cmd_update(args):
|
|
|
201
204
|
sys.exit(subprocess.run(cmd).returncode)
|
|
202
205
|
|
|
203
206
|
|
|
207
|
+
def aws_credentials(profile):
|
|
208
|
+
"""Resolves a profile (SSO, assumed role, ...) to AWS_* variables with the AWS CLI."""
|
|
209
|
+
if not shutil.which("aws"):
|
|
210
|
+
sys.exit("trefoil: --aws-profile needs the AWS CLI (aws) on PATH")
|
|
211
|
+
out = subprocess.run(["aws", "configure", "export-credentials", "--profile", profile, "--format", "process"],
|
|
212
|
+
capture_output=True, text=True)
|
|
213
|
+
if out.returncode != 0:
|
|
214
|
+
sys.exit(f"trefoil: aws profile {profile}: {out.stderr.strip() or 'no credentials'}"
|
|
215
|
+
f" (try: aws sso login --profile {profile})")
|
|
216
|
+
creds = json.loads(out.stdout)
|
|
217
|
+
env = {"AWS_ACCESS_KEY_ID": creds["AccessKeyId"], "AWS_SECRET_ACCESS_KEY": creds["SecretAccessKey"]}
|
|
218
|
+
if creds.get("SessionToken"):
|
|
219
|
+
env["AWS_SESSION_TOKEN"] = creds["SessionToken"]
|
|
220
|
+
if creds.get("Expiration"):
|
|
221
|
+
print(f"trefoil: credentials from profile {profile} expire at {creds['Expiration']}; "
|
|
222
|
+
"restart the engine after that", file=sys.stderr)
|
|
223
|
+
region = subprocess.run(["aws", "configure", "get", "region", "--profile", profile],
|
|
224
|
+
capture_output=True, text=True).stdout.strip()
|
|
225
|
+
if region:
|
|
226
|
+
env["AWS_REGION"] = region
|
|
227
|
+
return env
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def provider_env(args):
|
|
231
|
+
"""Returns the variables the engine container needs for args.provider.
|
|
232
|
+
|
|
233
|
+
Values go in the docker process's environment and are passed by name
|
|
234
|
+
("-e NAME"), so they don't show up in the printed command or ps."""
|
|
235
|
+
env = {"LLM_PROVIDER": args.provider}
|
|
236
|
+
if args.provider == "bedrock":
|
|
237
|
+
profile = args.aws_profile or (os.environ.get("AWS_PROFILE")
|
|
238
|
+
if not os.environ.get("AWS_ACCESS_KEY_ID") else None)
|
|
239
|
+
if profile:
|
|
240
|
+
env.update(aws_credentials(profile))
|
|
241
|
+
else:
|
|
242
|
+
for name in ("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN", "AWS_BEARER_TOKEN_BEDROCK"):
|
|
243
|
+
if os.environ.get(name):
|
|
244
|
+
env[name] = os.environ[name]
|
|
245
|
+
if not (env.get("AWS_ACCESS_KEY_ID") or env.get("AWS_BEARER_TOKEN_BEDROCK")):
|
|
246
|
+
sys.exit("trefoil: no AWS credentials for bedrock: pass --aws-profile or set "
|
|
247
|
+
"AWS_PROFILE, AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY or AWS_BEARER_TOKEN_BEDROCK")
|
|
248
|
+
region = args.aws_region or os.environ.get("AWS_REGION") or os.environ.get("AWS_DEFAULT_REGION") \
|
|
249
|
+
or env.get("AWS_REGION")
|
|
250
|
+
if not region:
|
|
251
|
+
sys.exit("trefoil: no AWS region for bedrock: pass --aws-region or set AWS_REGION")
|
|
252
|
+
env["AWS_REGION"] = region
|
|
253
|
+
elif args.provider == "anthropic":
|
|
254
|
+
key = os.environ.get("ANTHROPIC_API_KEY")
|
|
255
|
+
if not key and sys.stdin.isatty():
|
|
256
|
+
key = getpass.getpass("ANTHROPIC_API_KEY: ").strip()
|
|
257
|
+
if not key:
|
|
258
|
+
sys.exit("trefoil: set ANTHROPIC_API_KEY to use the anthropic provider")
|
|
259
|
+
env["ANTHROPIC_API_KEY"] = key
|
|
260
|
+
if os.environ.get("ANTHROPIC_BASE_URL"):
|
|
261
|
+
env["ANTHROPIC_BASE_URL"] = os.environ["ANTHROPIC_BASE_URL"]
|
|
262
|
+
return env
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
ENGINE_FILE = Path.home() / ".trefoil" / "engine.json"
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def ask(question, default=None, choices=None):
|
|
269
|
+
"""Prompts on stderr (stdout stays for results) until the answer is valid."""
|
|
270
|
+
hint = f" [{default}]" if default else ""
|
|
271
|
+
while True:
|
|
272
|
+
sys.stderr.write(f"{question}{hint}: ")
|
|
273
|
+
sys.stderr.flush()
|
|
274
|
+
try:
|
|
275
|
+
answer = input().strip() or default or ""
|
|
276
|
+
except EOFError:
|
|
277
|
+
sys.exit("\ntrefoil: setup cancelled")
|
|
278
|
+
if choices is None and answer:
|
|
279
|
+
return answer
|
|
280
|
+
if choices and answer.isdigit() and 1 <= int(answer) <= len(choices):
|
|
281
|
+
return choices[int(answer) - 1]
|
|
282
|
+
if choices and answer in choices:
|
|
283
|
+
return answer
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
def pick(question, options, default=None):
|
|
287
|
+
"""Asks for one of options (shown numbered); returns the chosen one."""
|
|
288
|
+
for i, option in enumerate(options, 1):
|
|
289
|
+
print(f" {i}) {option}", file=sys.stderr)
|
|
290
|
+
return ask(question, default if default in options else None, options)
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def engine_setup(saved):
|
|
294
|
+
"""Asks which LLM backend the engine uses; returns the (non-secret) answers."""
|
|
295
|
+
print("trefoil engine setup (saved to {}, rerun with --setup)".format(ENGINE_FILE), file=sys.stderr)
|
|
296
|
+
labels = {"bedrock": "bedrock (AWS)", "anthropic": "anthropic (API key)"}
|
|
297
|
+
label = pick("LLM backend", list(labels.values()), labels.get(saved.get("provider"), labels["bedrock"]))
|
|
298
|
+
provider = next(k for k, v in labels.items() if v == label)
|
|
299
|
+
choice = {"provider": provider}
|
|
300
|
+
if provider == "bedrock":
|
|
301
|
+
profiles = []
|
|
302
|
+
if shutil.which("aws"):
|
|
303
|
+
profiles = subprocess.run(["aws", "configure", "list-profiles"],
|
|
304
|
+
capture_output=True, text=True).stdout.split()
|
|
305
|
+
env_creds = "credentials from environment (AWS_ACCESS_KEY_ID, ...)"
|
|
306
|
+
options = profiles + [env_creds]
|
|
307
|
+
default = saved.get("aws_profile") or os.environ.get("AWS_PROFILE") or ("default" if "default" in profiles else None)
|
|
308
|
+
if not profiles:
|
|
309
|
+
print(" (no AWS CLI profiles found; set AWS_* variables before engine start)", file=sys.stderr)
|
|
310
|
+
profile = pick("AWS credentials", options, default or options[0])
|
|
311
|
+
region = None
|
|
312
|
+
if profile != env_creds:
|
|
313
|
+
choice["aws_profile"] = profile
|
|
314
|
+
region = subprocess.run(["aws", "configure", "get", "region", "--profile", profile],
|
|
315
|
+
capture_output=True, text=True).stdout.strip() or None
|
|
316
|
+
choice["aws_region"] = ask("AWS region", saved.get("aws_region") or region
|
|
317
|
+
or os.environ.get("AWS_REGION") or "eu-west-1")
|
|
318
|
+
elif not os.environ.get("ANTHROPIC_API_KEY"):
|
|
319
|
+
print(" ANTHROPIC_API_KEY isn't set; you'll be asked for it on each start "
|
|
320
|
+
"(export it to skip that). It is never saved.", file=sys.stderr)
|
|
321
|
+
ENGINE_FILE.parent.mkdir(parents=True, exist_ok=True)
|
|
322
|
+
ENGINE_FILE.write_text(json.dumps(choice, indent=2) + "\n")
|
|
323
|
+
return choice
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
def apply_engine_choice(args):
|
|
327
|
+
"""Fills args.provider/aws_* from ~/.trefoil/engine.json, running setup on
|
|
328
|
+
first use (or with --setup). Flags and --settings win over saved answers."""
|
|
329
|
+
if args.provider and not args.setup:
|
|
330
|
+
return
|
|
331
|
+
try:
|
|
332
|
+
saved = json.loads(ENGINE_FILE.read_text())
|
|
333
|
+
except (OSError, ValueError):
|
|
334
|
+
saved = {}
|
|
335
|
+
if args.setup or (not saved and not args.settings and sys.stdin.isatty()):
|
|
336
|
+
saved = engine_setup(saved)
|
|
337
|
+
if args.settings and not args.setup:
|
|
338
|
+
return
|
|
339
|
+
args.provider = args.provider or saved.get("provider")
|
|
340
|
+
args.aws_profile = args.aws_profile or saved.get("aws_profile")
|
|
341
|
+
args.aws_region = args.aws_region or saved.get("aws_region")
|
|
342
|
+
|
|
343
|
+
|
|
204
344
|
def cmd_engine_start(args):
|
|
205
345
|
if not shutil.which("docker"):
|
|
206
346
|
sys.exit("trefoil: docker is not installed or not on PATH")
|
|
347
|
+
apply_engine_choice(args)
|
|
207
348
|
cmd = ["docker", "run", "--rm", "--name", args.name, "-p", f"{args.port}:8080",
|
|
208
349
|
"-v", "/var/run/docker.sock:/var/run/docker.sock", "-v", f"{args.volume}:/data"]
|
|
209
350
|
if args.settings:
|
|
@@ -224,11 +365,15 @@ def cmd_engine_start(args):
|
|
|
224
365
|
sys.exit(f"trefoil: env_file {host} (from {args.settings}) not found")
|
|
225
366
|
target = env_file if os.path.isabs(env_file) else os.path.normpath(f"/trefoil/settings/{env_file}")
|
|
226
367
|
cmd += ["-v", f"{host}:{target}:ro"]
|
|
368
|
+
env = provider_env(args) if args.provider else {}
|
|
369
|
+
for name in env:
|
|
370
|
+
cmd += ["-e", name]
|
|
227
371
|
if not args.foreground:
|
|
228
372
|
cmd.append("-d")
|
|
229
373
|
cmd.append(args.image)
|
|
230
374
|
print(f"trefoil: {' '.join(cmd)}", file=sys.stderr)
|
|
231
|
-
code = subprocess.run(cmd,
|
|
375
|
+
code = subprocess.run(cmd, env={**os.environ, **env},
|
|
376
|
+
stdout=subprocess.DEVNULL if not args.foreground else None).returncode
|
|
232
377
|
if code == 0 and not args.foreground:
|
|
233
378
|
print(f"trefoil: engine running on http://localhost:{args.port} "
|
|
234
379
|
f"(logs: docker logs -f {args.name}, stop: trefoil engine stop)", file=sys.stderr)
|
|
@@ -363,7 +508,11 @@ def cmd_agents_run(args):
|
|
|
363
508
|
started = api_request(args.server, "POST", f"/agents/{quote(args.agent)}/run", body)
|
|
364
509
|
if "session_id" not in started:
|
|
365
510
|
sys.exit(f"trefoil: server at {args.server} doesn't return sessions; is it up to date? (restart `make dev`)")
|
|
366
|
-
|
|
511
|
+
follow_run(args, started["session_id"])
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
def follow_run(args, session_id):
|
|
515
|
+
"""Follows a session that was just started, or prints its id with -d."""
|
|
367
516
|
print(f"session {session_id}: running {args.agent}", file=sys.stderr)
|
|
368
517
|
if args.detach:
|
|
369
518
|
print(session_id)
|
|
@@ -484,6 +633,12 @@ def cmd_sessions_result(args):
|
|
|
484
633
|
print_result(args.server, args.agent, args.id)
|
|
485
634
|
|
|
486
635
|
|
|
636
|
+
def cmd_sessions_rerun(args):
|
|
637
|
+
started = api_request(args.server, "POST", session_path(args.agent, args.id, "rerun"))
|
|
638
|
+
print(f"rerunning session {args.id} with the same input", file=sys.stderr)
|
|
639
|
+
follow_run(args, started["session_id"])
|
|
640
|
+
|
|
641
|
+
|
|
487
642
|
def cmd_sessions_stop(args):
|
|
488
643
|
api_request(args.server, "POST", session_path(args.agent, args.id, "stop"))
|
|
489
644
|
print(f"stopping session {args.id}; it finishes as failed with error \"stopped\"", file=sys.stderr)
|
|
@@ -583,9 +738,18 @@ def main():
|
|
|
583
738
|
engine_start = engine.add_parser("start", help="run the trefoil API server image")
|
|
584
739
|
engine_start.add_argument("--settings", type=Path, metavar="FILE",
|
|
585
740
|
help="backend settings file (backend.yml) to mount into the container")
|
|
741
|
+
engine_start.add_argument("--provider", choices=["bedrock", "anthropic"],
|
|
742
|
+
help="LLM backend agents use (overrides llm_provider in --settings); "
|
|
743
|
+
"bedrock takes AWS credentials, anthropic ANTHROPIC_API_KEY")
|
|
744
|
+
engine_start.add_argument("--setup", action="store_true",
|
|
745
|
+
help=f"ask for the LLM backend again (answers are saved to {ENGINE_FILE})")
|
|
746
|
+
engine_start.add_argument("--aws-profile", metavar="PROFILE",
|
|
747
|
+
help="AWS profile to take bedrock credentials and region from "
|
|
748
|
+
"(default: $AWS_PROFILE unless AWS_ACCESS_KEY_ID is set)")
|
|
749
|
+
engine_start.add_argument("--aws-region", metavar="REGION",
|
|
750
|
+
help="AWS region for bedrock (default: $AWS_REGION or the profile's region)")
|
|
586
751
|
engine_start.add_argument("--port", type=int, default=8080, help="host port to publish (default: 8080)")
|
|
587
|
-
engine_start.add_argument("--image", default="
|
|
588
|
-
help="API image (default: ghcr.io/alto-se/trefoil-api:latest)")
|
|
752
|
+
engine_start.add_argument("--image", default=ENGINE_IMAGE, help=f"API image (default: {ENGINE_IMAGE})")
|
|
589
753
|
engine_start.add_argument("--name", default="trefoil-api", help="container name (default: trefoil-api)")
|
|
590
754
|
engine_start.add_argument("--volume", default="trefoil-data",
|
|
591
755
|
help="docker volume (or host path) for the data dir (default: trefoil-data)")
|
|
@@ -616,12 +780,13 @@ def main():
|
|
|
616
780
|
agents_build.add_argument("--dockerfile", action="store_true", help="print the generated Dockerfile")
|
|
617
781
|
agents_build.set_defaults(func=cmd_agents_build)
|
|
618
782
|
|
|
619
|
-
agents_context = agents.add_parser("get-
|
|
783
|
+
agents_context = agents.add_parser("get-dockerfile", aliases=["context"], parents=[server, agent],
|
|
620
784
|
help="download an agent's build context (Dockerfile and files) as a zip")
|
|
621
785
|
agents_context.add_argument("-o", "--output", type=Path, metavar="FILE",
|
|
622
786
|
help="where to save the zip (default: ./NAME-context.zip)")
|
|
623
787
|
agents_context.set_defaults(func=cmd_agents_context)
|
|
624
788
|
|
|
789
|
+
|
|
625
790
|
agents_run = agents.add_parser("run", parents=[server, agent], help="run a built agent with a task")
|
|
626
791
|
agents_run.add_argument("task", nargs="?", help="task for the agent (default: read from stdin)")
|
|
627
792
|
agents_run.add_argument("--input", metavar="JSON|file://PATH",
|
|
@@ -635,6 +800,7 @@ def main():
|
|
|
635
800
|
("logs", cmd_sessions_logs, "print a session's logs"),
|
|
636
801
|
("result", cmd_sessions_result, "print a finished session's answer"),
|
|
637
802
|
("stop", cmd_sessions_stop, "stop a running session"),
|
|
803
|
+
("rerun", cmd_sessions_rerun, "run a session's agent again with the same input"),
|
|
638
804
|
("artifacts", cmd_sessions_artifacts, "list or download a session's artifacts")):
|
|
639
805
|
sub = sessions.add_parser(name, parents=[server, agent], help=help_text)
|
|
640
806
|
sub.add_argument("id", help="session id")
|
|
@@ -646,6 +812,9 @@ def main():
|
|
|
646
812
|
if name == "artifacts":
|
|
647
813
|
sub.add_argument("-o", "--output", type=Path, metavar="DIR",
|
|
648
814
|
help="download every artifact into DIR instead of listing them")
|
|
815
|
+
if name == "rerun":
|
|
816
|
+
sub.add_argument("-d", "--detach", action="store_true",
|
|
817
|
+
help="print the new session id and return instead of following the run")
|
|
649
818
|
|
|
650
819
|
secrets = domain("secrets", "provide secrets that agents' modules require")
|
|
651
820
|
secrets_set = secrets.add_parser("set", parents=[server],
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|