-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriter_cli.py
More file actions
65 lines (52 loc) · 1.63 KB
/
writer_cli.py
File metadata and controls
65 lines (52 loc) · 1.63 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
from __future__ import annotations
import argparse
import json
import os
import sys
from dataclasses import asdict
from pathlib import Path
from agents_impl import OpenAIWriter
from env_loader import load_env
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Run the Writer agent only.")
parser.add_argument(
"--input",
required=True,
help="Path to Writer input JSON (use '-' to read from stdin)",
)
return parser.parse_args()
def read_input(path: str) -> str:
if path == "-":
return sys.stdin.read()
file_path = Path(path)
if not file_path.exists():
raise FileNotFoundError(path)
return file_path.read_text()
def main() -> int:
args = parse_args()
load_env(keys=["OPENAI_API_KEY", "OPENAI_MODEL", "OPENAI_TEMPERATURE"])
if not os.getenv("OPENAI_API_KEY"):
print("OPENAI_API_KEY is not set.")
return 1
try:
raw = read_input(args.input)
except FileNotFoundError:
print(f"Input file not found: {args.input}")
return 1
except OSError as exc:
print(f"Failed to read input: {exc}")
return 1
try:
payload = json.loads(raw)
except json.JSONDecodeError as exc:
print(f"Input must be JSON: {exc}")
return 1
print("Writer input:")
print(json.dumps(payload, indent=2, ensure_ascii=True))
writer = OpenAIWriter()
output = writer.run(json.dumps(payload, ensure_ascii=True))
print("Writer output:")
print(json.dumps(asdict(output), indent=2, ensure_ascii=True))
return 0
if __name__ == "__main__":
sys.exit(main())