trefoil-cli 0.6.0__tar.gz → 0.8.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trefoil-cli
3
- Version: 0.6.0
3
+ Version: 0.8.0
4
4
  Summary: CLI for the trefoil agent server
5
5
  Requires-Python: >=3.10
6
6
  Requires-Dist: pyyaml>=6
@@ -16,15 +16,19 @@ 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-bundle --agent=NAME [-o FILE]
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]
30
+ trefoil secrets list
31
+ trefoil secrets set --name=NAME
28
32
  trefoil jobs list [--agent=NAME] [--status=STATUS]
29
33
  trefoil jobs status ID [-f] [--logs]
30
34
  """
@@ -49,6 +53,7 @@ import yaml
49
53
 
50
54
  DEFAULT_SERVER = "http://localhost:8080"
51
55
  EXAMPLES_API = "https://api.github.com/repos/alto-se/trefoil/contents/examples"
56
+ ENGINE_IMAGE = "ghcr.io/alto-se/trefoil-api:latest"
52
57
 
53
58
 
54
59
  class Ref:
@@ -201,9 +206,147 @@ def cmd_update(args):
201
206
  sys.exit(subprocess.run(cmd).returncode)
202
207
 
203
208
 
209
+ def aws_credentials(profile):
210
+ """Resolves a profile (SSO, assumed role, ...) to AWS_* variables with the AWS CLI."""
211
+ if not shutil.which("aws"):
212
+ sys.exit("trefoil: --aws-profile needs the AWS CLI (aws) on PATH")
213
+ out = subprocess.run(["aws", "configure", "export-credentials", "--profile", profile, "--format", "process"],
214
+ capture_output=True, text=True)
215
+ if out.returncode != 0:
216
+ sys.exit(f"trefoil: aws profile {profile}: {out.stderr.strip() or 'no credentials'}"
217
+ f" (try: aws sso login --profile {profile})")
218
+ creds = json.loads(out.stdout)
219
+ env = {"AWS_ACCESS_KEY_ID": creds["AccessKeyId"], "AWS_SECRET_ACCESS_KEY": creds["SecretAccessKey"]}
220
+ if creds.get("SessionToken"):
221
+ env["AWS_SESSION_TOKEN"] = creds["SessionToken"]
222
+ if creds.get("Expiration"):
223
+ print(f"trefoil: credentials from profile {profile} expire at {creds['Expiration']}; "
224
+ "restart the engine after that", file=sys.stderr)
225
+ region = subprocess.run(["aws", "configure", "get", "region", "--profile", profile],
226
+ capture_output=True, text=True).stdout.strip()
227
+ if region:
228
+ env["AWS_REGION"] = region
229
+ return env
230
+
231
+
232
+ def provider_env(args):
233
+ """Returns the variables the engine container needs for args.provider.
234
+
235
+ Values go in the docker process's environment and are passed by name
236
+ ("-e NAME"), so they don't show up in the printed command or ps."""
237
+ env = {"LLM_PROVIDER": args.provider}
238
+ if args.provider == "bedrock":
239
+ profile = args.aws_profile or (os.environ.get("AWS_PROFILE")
240
+ if not os.environ.get("AWS_ACCESS_KEY_ID") else None)
241
+ if profile:
242
+ env.update(aws_credentials(profile))
243
+ else:
244
+ for name in ("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN", "AWS_BEARER_TOKEN_BEDROCK"):
245
+ if os.environ.get(name):
246
+ env[name] = os.environ[name]
247
+ if not (env.get("AWS_ACCESS_KEY_ID") or env.get("AWS_BEARER_TOKEN_BEDROCK")):
248
+ sys.exit("trefoil: no AWS credentials for bedrock: pass --aws-profile or set "
249
+ "AWS_PROFILE, AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY or AWS_BEARER_TOKEN_BEDROCK")
250
+ region = args.aws_region or os.environ.get("AWS_REGION") or os.environ.get("AWS_DEFAULT_REGION") \
251
+ or env.get("AWS_REGION")
252
+ if not region:
253
+ sys.exit("trefoil: no AWS region for bedrock: pass --aws-region or set AWS_REGION")
254
+ env["AWS_REGION"] = region
255
+ elif args.provider == "anthropic":
256
+ key = os.environ.get("ANTHROPIC_API_KEY")
257
+ if not key and sys.stdin.isatty():
258
+ key = getpass.getpass("ANTHROPIC_API_KEY: ").strip()
259
+ if not key:
260
+ sys.exit("trefoil: set ANTHROPIC_API_KEY to use the anthropic provider")
261
+ env["ANTHROPIC_API_KEY"] = key
262
+ if os.environ.get("ANTHROPIC_BASE_URL"):
263
+ env["ANTHROPIC_BASE_URL"] = os.environ["ANTHROPIC_BASE_URL"]
264
+ return env
265
+
266
+
267
+ ENGINE_FILE = Path.home() / ".trefoil" / "engine.json"
268
+
269
+
270
+ def ask(question, default=None, choices=None):
271
+ """Prompts on stderr (stdout stays for results) until the answer is valid."""
272
+ hint = f" [{default}]" if default else ""
273
+ while True:
274
+ sys.stderr.write(f"{question}{hint}: ")
275
+ sys.stderr.flush()
276
+ try:
277
+ answer = input().strip() or default or ""
278
+ except EOFError:
279
+ sys.exit("\ntrefoil: setup cancelled")
280
+ if choices is None and answer:
281
+ return answer
282
+ if choices and answer.isdigit() and 1 <= int(answer) <= len(choices):
283
+ return choices[int(answer) - 1]
284
+ if choices and answer in choices:
285
+ return answer
286
+
287
+
288
+ def pick(question, options, default=None):
289
+ """Asks for one of options (shown numbered); returns the chosen one."""
290
+ for i, option in enumerate(options, 1):
291
+ print(f" {i}) {option}", file=sys.stderr)
292
+ return ask(question, default if default in options else None, options)
293
+
294
+
295
+ def engine_setup(saved):
296
+ """Asks which LLM backend the engine uses; returns the (non-secret) answers."""
297
+ print("trefoil engine setup (saved to {}, rerun with --setup)".format(ENGINE_FILE), file=sys.stderr)
298
+ labels = {"bedrock": "bedrock (AWS)", "anthropic": "anthropic (API key)"}
299
+ label = pick("LLM backend", list(labels.values()), labels.get(saved.get("provider"), labels["bedrock"]))
300
+ provider = next(k for k, v in labels.items() if v == label)
301
+ choice = {"provider": provider}
302
+ if provider == "bedrock":
303
+ profiles = []
304
+ if shutil.which("aws"):
305
+ profiles = subprocess.run(["aws", "configure", "list-profiles"],
306
+ capture_output=True, text=True).stdout.split()
307
+ env_creds = "credentials from environment (AWS_ACCESS_KEY_ID, ...)"
308
+ options = profiles + [env_creds]
309
+ default = saved.get("aws_profile") or os.environ.get("AWS_PROFILE") or ("default" if "default" in profiles else None)
310
+ if not profiles:
311
+ print(" (no AWS CLI profiles found; set AWS_* variables before engine start)", file=sys.stderr)
312
+ profile = pick("AWS credentials", options, default or options[0])
313
+ region = None
314
+ if profile != env_creds:
315
+ choice["aws_profile"] = profile
316
+ region = subprocess.run(["aws", "configure", "get", "region", "--profile", profile],
317
+ capture_output=True, text=True).stdout.strip() or None
318
+ choice["aws_region"] = ask("AWS region", saved.get("aws_region") or region
319
+ or os.environ.get("AWS_REGION") or "eu-west-1")
320
+ elif not os.environ.get("ANTHROPIC_API_KEY"):
321
+ print(" ANTHROPIC_API_KEY isn't set; you'll be asked for it on each start "
322
+ "(export it to skip that). It is never saved.", file=sys.stderr)
323
+ ENGINE_FILE.parent.mkdir(parents=True, exist_ok=True)
324
+ ENGINE_FILE.write_text(json.dumps(choice, indent=2) + "\n")
325
+ return choice
326
+
327
+
328
+ def apply_engine_choice(args):
329
+ """Fills args.provider/aws_* from ~/.trefoil/engine.json, running setup on
330
+ first use (or with --setup). Flags and --settings win over saved answers."""
331
+ if args.provider and not args.setup:
332
+ return
333
+ try:
334
+ saved = json.loads(ENGINE_FILE.read_text())
335
+ except (OSError, ValueError):
336
+ saved = {}
337
+ if args.setup or (not saved and not args.settings and sys.stdin.isatty()):
338
+ saved = engine_setup(saved)
339
+ if args.settings and not args.setup:
340
+ return
341
+ args.provider = args.provider or saved.get("provider")
342
+ args.aws_profile = args.aws_profile or saved.get("aws_profile")
343
+ args.aws_region = args.aws_region or saved.get("aws_region")
344
+
345
+
204
346
  def cmd_engine_start(args):
205
347
  if not shutil.which("docker"):
206
348
  sys.exit("trefoil: docker is not installed or not on PATH")
349
+ apply_engine_choice(args)
207
350
  cmd = ["docker", "run", "--rm", "--name", args.name, "-p", f"{args.port}:8080",
208
351
  "-v", "/var/run/docker.sock:/var/run/docker.sock", "-v", f"{args.volume}:/data"]
209
352
  if args.settings:
@@ -224,11 +367,15 @@ def cmd_engine_start(args):
224
367
  sys.exit(f"trefoil: env_file {host} (from {args.settings}) not found")
225
368
  target = env_file if os.path.isabs(env_file) else os.path.normpath(f"/trefoil/settings/{env_file}")
226
369
  cmd += ["-v", f"{host}:{target}:ro"]
370
+ env = provider_env(args) if args.provider else {}
371
+ for name in env:
372
+ cmd += ["-e", name]
227
373
  if not args.foreground:
228
374
  cmd.append("-d")
229
375
  cmd.append(args.image)
230
376
  print(f"trefoil: {' '.join(cmd)}", file=sys.stderr)
231
- code = subprocess.run(cmd, stdout=subprocess.DEVNULL if not args.foreground else None).returncode
377
+ code = subprocess.run(cmd, env={**os.environ, **env},
378
+ stdout=subprocess.DEVNULL if not args.foreground else None).returncode
232
379
  if code == 0 and not args.foreground:
233
380
  print(f"trefoil: engine running on http://localhost:{args.port} "
234
381
  f"(logs: docker logs -f {args.name}, stop: trefoil engine stop)", file=sys.stderr)
@@ -363,7 +510,11 @@ def cmd_agents_run(args):
363
510
  started = api_request(args.server, "POST", f"/agents/{quote(args.agent)}/run", body)
364
511
  if "session_id" not in started:
365
512
  sys.exit(f"trefoil: server at {args.server} doesn't return sessions; is it up to date? (restart `make dev`)")
366
- session_id = started["session_id"]
513
+ follow_run(args, started["session_id"])
514
+
515
+
516
+ def follow_run(args, session_id):
517
+ """Follows a session that was just started, or prints its id with -d."""
367
518
  print(f"session {session_id}: running {args.agent}", file=sys.stderr)
368
519
  if args.detach:
369
520
  print(session_id)
@@ -384,6 +535,16 @@ def cmd_secrets_set(args):
384
535
  print(f"stored secret {args.name} on {args.server}", file=sys.stderr)
385
536
 
386
537
 
538
+ def cmd_secrets_list(args):
539
+ secrets = api_request(args.server, "GET", "/secrets")
540
+ rows = [
541
+ (s["name"], "yes" if s["required"] else "no",
542
+ s["source"] or ("missing" if s["required"] else "not set"), ", ".join(s["agents"]))
543
+ for s in secrets
544
+ ]
545
+ print_table(("NAME", "REQUIRED", "SOURCE", "USED BY"), rows, empty="no secrets")
546
+
547
+
387
548
  def cmd_params_list(args):
388
549
  params = api_request(args.server, "GET", "/params")
389
550
  rows = [
@@ -484,6 +645,12 @@ def cmd_sessions_result(args):
484
645
  print_result(args.server, args.agent, args.id)
485
646
 
486
647
 
648
+ def cmd_sessions_rerun(args):
649
+ started = api_request(args.server, "POST", session_path(args.agent, args.id, "rerun"))
650
+ print(f"rerunning session {args.id} with the same input", file=sys.stderr)
651
+ follow_run(args, started["session_id"])
652
+
653
+
487
654
  def cmd_sessions_stop(args):
488
655
  api_request(args.server, "POST", session_path(args.agent, args.id, "stop"))
489
656
  print(f"stopping session {args.id}; it finishes as failed with error \"stopped\"", file=sys.stderr)
@@ -583,9 +750,18 @@ def main():
583
750
  engine_start = engine.add_parser("start", help="run the trefoil API server image")
584
751
  engine_start.add_argument("--settings", type=Path, metavar="FILE",
585
752
  help="backend settings file (backend.yml) to mount into the container")
753
+ engine_start.add_argument("--provider", choices=["bedrock", "anthropic"],
754
+ help="LLM backend agents use (overrides llm_provider in --settings); "
755
+ "bedrock takes AWS credentials, anthropic ANTHROPIC_API_KEY")
756
+ engine_start.add_argument("--setup", action="store_true",
757
+ help=f"ask for the LLM backend again (answers are saved to {ENGINE_FILE})")
758
+ engine_start.add_argument("--aws-profile", metavar="PROFILE",
759
+ help="AWS profile to take bedrock credentials and region from "
760
+ "(default: $AWS_PROFILE unless AWS_ACCESS_KEY_ID is set)")
761
+ engine_start.add_argument("--aws-region", metavar="REGION",
762
+ help="AWS region for bedrock (default: $AWS_REGION or the profile's region)")
586
763
  engine_start.add_argument("--port", type=int, default=8080, help="host port to publish (default: 8080)")
587
- engine_start.add_argument("--image", default="ghcr.io/alto-se/trefoil-api:latest",
588
- help="API image (default: ghcr.io/alto-se/trefoil-api:latest)")
764
+ engine_start.add_argument("--image", default=ENGINE_IMAGE, help=f"API image (default: {ENGINE_IMAGE})")
589
765
  engine_start.add_argument("--name", default="trefoil-api", help="container name (default: trefoil-api)")
590
766
  engine_start.add_argument("--volume", default="trefoil-data",
591
767
  help="docker volume (or host path) for the data dir (default: trefoil-data)")
@@ -616,12 +792,13 @@ def main():
616
792
  agents_build.add_argument("--dockerfile", action="store_true", help="print the generated Dockerfile")
617
793
  agents_build.set_defaults(func=cmd_agents_build)
618
794
 
619
- agents_context = agents.add_parser("get-bundle", aliases=["context"], parents=[server, agent],
795
+ agents_context = agents.add_parser("get-dockerfile", aliases=["context"], parents=[server, agent],
620
796
  help="download an agent's build context (Dockerfile and files) as a zip")
621
797
  agents_context.add_argument("-o", "--output", type=Path, metavar="FILE",
622
798
  help="where to save the zip (default: ./NAME-context.zip)")
623
799
  agents_context.set_defaults(func=cmd_agents_context)
624
800
 
801
+
625
802
  agents_run = agents.add_parser("run", parents=[server, agent], help="run a built agent with a task")
626
803
  agents_run.add_argument("task", nargs="?", help="task for the agent (default: read from stdin)")
627
804
  agents_run.add_argument("--input", metavar="JSON|file://PATH",
@@ -635,6 +812,7 @@ def main():
635
812
  ("logs", cmd_sessions_logs, "print a session's logs"),
636
813
  ("result", cmd_sessions_result, "print a finished session's answer"),
637
814
  ("stop", cmd_sessions_stop, "stop a running session"),
815
+ ("rerun", cmd_sessions_rerun, "run a session's agent again with the same input"),
638
816
  ("artifacts", cmd_sessions_artifacts, "list or download a session's artifacts")):
639
817
  sub = sessions.add_parser(name, parents=[server, agent], help=help_text)
640
818
  sub.add_argument("id", help="session id")
@@ -646,8 +824,14 @@ def main():
646
824
  if name == "artifacts":
647
825
  sub.add_argument("-o", "--output", type=Path, metavar="DIR",
648
826
  help="download every artifact into DIR instead of listing them")
827
+ if name == "rerun":
828
+ sub.add_argument("-d", "--detach", action="store_true",
829
+ help="print the new session id and return instead of following the run")
649
830
 
650
831
  secrets = domain("secrets", "provide secrets that agents' modules require")
832
+ secrets_list = secrets.add_parser("list", parents=[server],
833
+ help="list the secrets agents need and whether they're set (never values)")
834
+ secrets_list.set_defaults(func=cmd_secrets_list)
651
835
  secrets_set = secrets.add_parser("set", parents=[server],
652
836
  help="store a secret on the server (value from a prompt or stdin)")
653
837
  secrets_set.add_argument("--name", required=True, metavar="NAME", help="secret name, e.g. github_token")
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "trefoil-cli"
3
- version = "0.6.0"
3
+ version = "0.8.0"
4
4
  description = "CLI for the trefoil agent server"
5
5
  requires-python = ">=3.10"
6
6
  dependencies = ["pyyaml>=6"]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trefoil-cli
3
- Version: 0.6.0
3
+ Version: 0.8.0
4
4
  Summary: CLI for the trefoil agent server
5
5
  Requires-Python: >=3.10
6
6
  Requires-Dist: pyyaml>=6
File without changes