trefoil-cli 0.5.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trefoil-cli
3
- Version: 0.5.0
3
+ Version: 0.7.0
4
4
  Summary: CLI for the trefoil agent server
5
5
  Requires-Python: >=3.10
6
6
  Requires-Dist: pyyaml>=6
@@ -15,15 +15,17 @@ Usage: trefoil <domain> <command> [args]
15
15
 
16
16
  trefoil init [DIR] [--example=NAME]
17
17
  trefoil update
18
- trefoil engine start [--settings=backend.yml] [--port=8080] [-d]
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]
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,19 @@ 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"]
227
- if args.detach:
368
+ env = provider_env(args) if args.provider else {}
369
+ for name in env:
370
+ cmd += ["-e", name]
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
- sys.exit(subprocess.run(cmd).returncode)
375
+ code = subprocess.run(cmd, env={**os.environ, **env},
376
+ stdout=subprocess.DEVNULL if not args.foreground else None).returncode
377
+ if code == 0 and not args.foreground:
378
+ print(f"trefoil: engine running on http://localhost:{args.port} "
379
+ f"(logs: docker logs -f {args.name}, stop: trefoil engine stop)", file=sys.stderr)
380
+ sys.exit(code)
232
381
 
233
382
 
234
383
  def cmd_engine_stop(args):
@@ -359,7 +508,11 @@ def cmd_agents_run(args):
359
508
  started = api_request(args.server, "POST", f"/agents/{quote(args.agent)}/run", body)
360
509
  if "session_id" not in started:
361
510
  sys.exit(f"trefoil: server at {args.server} doesn't return sessions; is it up to date? (restart `make dev`)")
362
- session_id = started["session_id"]
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."""
363
516
  print(f"session {session_id}: running {args.agent}", file=sys.stderr)
364
517
  if args.detach:
365
518
  print(session_id)
@@ -480,6 +633,12 @@ def cmd_sessions_result(args):
480
633
  print_result(args.server, args.agent, args.id)
481
634
 
482
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
+
483
642
  def cmd_sessions_stop(args):
484
643
  api_request(args.server, "POST", session_path(args.agent, args.id, "stop"))
485
644
  print(f"stopping session {args.id}; it finishes as failed with error \"stopped\"", file=sys.stderr)
@@ -579,13 +738,23 @@ def main():
579
738
  engine_start = engine.add_parser("start", help="run the trefoil API server image")
580
739
  engine_start.add_argument("--settings", type=Path, metavar="FILE",
581
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)")
582
751
  engine_start.add_argument("--port", type=int, default=8080, help="host port to publish (default: 8080)")
583
- engine_start.add_argument("--image", default="ghcr.io/alto-se/trefoil-api:latest",
584
- 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})")
585
753
  engine_start.add_argument("--name", default="trefoil-api", help="container name (default: trefoil-api)")
586
754
  engine_start.add_argument("--volume", default="trefoil-data",
587
755
  help="docker volume (or host path) for the data dir (default: trefoil-data)")
588
- engine_start.add_argument("-d", "--detach", action="store_true", help="run in the background")
756
+ engine_start.add_argument("--foreground", action="store_true",
757
+ help="run attached instead of in the background")
589
758
  engine_start.set_defaults(func=cmd_engine_start)
590
759
  engine_stop = engine.add_parser("stop", help="stop the running API server container")
591
760
  engine_stop.add_argument("--name", default="trefoil-api", help="container name (default: trefoil-api)")
@@ -611,12 +780,13 @@ def main():
611
780
  agents_build.add_argument("--dockerfile", action="store_true", help="print the generated Dockerfile")
612
781
  agents_build.set_defaults(func=cmd_agents_build)
613
782
 
614
- agents_context = agents.add_parser("get-bundle", aliases=["context"], parents=[server, agent],
783
+ agents_context = agents.add_parser("get-dockerfile", aliases=["context"], parents=[server, agent],
615
784
  help="download an agent's build context (Dockerfile and files) as a zip")
616
785
  agents_context.add_argument("-o", "--output", type=Path, metavar="FILE",
617
786
  help="where to save the zip (default: ./NAME-context.zip)")
618
787
  agents_context.set_defaults(func=cmd_agents_context)
619
788
 
789
+
620
790
  agents_run = agents.add_parser("run", parents=[server, agent], help="run a built agent with a task")
621
791
  agents_run.add_argument("task", nargs="?", help="task for the agent (default: read from stdin)")
622
792
  agents_run.add_argument("--input", metavar="JSON|file://PATH",
@@ -630,6 +800,7 @@ def main():
630
800
  ("logs", cmd_sessions_logs, "print a session's logs"),
631
801
  ("result", cmd_sessions_result, "print a finished session's answer"),
632
802
  ("stop", cmd_sessions_stop, "stop a running session"),
803
+ ("rerun", cmd_sessions_rerun, "run a session's agent again with the same input"),
633
804
  ("artifacts", cmd_sessions_artifacts, "list or download a session's artifacts")):
634
805
  sub = sessions.add_parser(name, parents=[server, agent], help=help_text)
635
806
  sub.add_argument("id", help="session id")
@@ -641,6 +812,9 @@ def main():
641
812
  if name == "artifacts":
642
813
  sub.add_argument("-o", "--output", type=Path, metavar="DIR",
643
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")
644
818
 
645
819
  secrets = domain("secrets", "provide secrets that agents' modules require")
646
820
  secrets_set = secrets.add_parser("set", parents=[server],
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "trefoil-cli"
3
- version = "0.5.0"
3
+ version = "0.7.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.5.0
3
+ Version: 0.7.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