-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_agent.py
More file actions
101 lines (86 loc) · 3.65 KB
/
create_agent.py
File metadata and controls
101 lines (86 loc) · 3.65 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
import argparse
import json
from pathlib import Path
from render_utils import create_agent
from tutorial_renderer import render_tutorial
from tutorial_utils import strip_markers_in_directory
from rich import print
# Mapping from configuration subdirectories to template names - NO LONGER NEEDED
# The configuration parent directory name now matches the template name.
def main():
parser = argparse.ArgumentParser(
description="Create an agent from a JSON configuration."
)
parser.add_argument(
"config",
type=Path,
help="Path to a JSON config file (e.g. agent-configurations/ts_langchain/gmail-slack.json)",
)
parser.add_argument(
"--template",
type=str,
help="The template to use. If not provided, it will be inferred from the config path.",
)
parser.add_argument(
"--output-dir",
type=Path,
help="The directory where the agent will be created. Defaults to real_agents/<template>/<config_stem>.",
)
parser.add_argument(
"--tutorial",
action="store_true",
help="Also render the tutorial alongside the agent.",
)
args = parser.parse_args()
config_path = args.config.expanduser()
if not config_path.exists():
raise SystemExit(f"Config file not found: {config_path}")
try:
configuration = json.loads(config_path.read_text())
except json.JSONDecodeError as e:
raise SystemExit(f"Invalid JSON in config file: {config_path}\n{e}")
if not isinstance(configuration, dict):
raise SystemExit(f"Config JSON must be an object/dict at top-level: {config_path}")
# Determine the template
template_name = args.template
if not template_name:
# Infer template from the parent directory name of the config file
template_name = config_path.parent.name
# Verify if it's a valid template (must exist in templates/)
if not (Path(__file__).parent / "templates" / template_name).exists():
# Check if the parent of the parent is agent-configurations
if config_path.parent.parent.name != "agent-configurations":
raise SystemExit(
f"Could not infer template from config path: {config_path}. "
"Please specify it with --template or place the config in agent-configurations/<template_name>/."
)
template_dir = Path(__file__).parent / "templates" / template_name
if not template_dir.exists():
raise SystemExit(f"Template directory not found: {template_dir}")
# Determine the output directory
output_dir = args.output_dir
if not output_dir:
output_dir = Path(__file__).parent / "real_agents" / template_name / config_path.stem
print(f"Creating agent using template: [bold blue]{template_name}[/bold blue]")
print(f"Config path: [yellow]{config_path}[/yellow]")
print(f"Output directory: [green]{output_dir}[/green]")
print("\nConfiguration:")
print(configuration)
print("")
create_agent(output_dir, template_dir, configuration)
if args.tutorial:
tutorial_md = render_tutorial(
template_dir=template_dir,
code_dir=output_dir,
context=configuration,
)
if tutorial_md:
tutorial_out = output_dir / "TUTORIAL.md"
tutorial_out.write_text(tutorial_md, encoding="utf-8")
print(f"Tutorial rendered to: [green]{tutorial_out}[/green]")
else:
print("[yellow]No tutorial template found for this template.[/yellow]")
# Strip snippet markers from generated code
strip_markers_in_directory(output_dir)
if __name__ == "__main__":
main()