-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenloop.py
More file actions
183 lines (164 loc) · 5.34 KB
/
Copy pathopenloop.py
File metadata and controls
183 lines (164 loc) · 5.34 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import argparse
import sys
def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
parser = argparse.ArgumentParser(
prog="openloop",
description="OpenLoop — Orchestration engine for OpenCode",
)
parser.add_argument(
"--cli",
action="store_true",
help="Run in CLI mode (headless, no GUI)",
)
parser.add_argument(
"--workflow",
type=str,
default=None,
help="Path to workflow JSON file (pre-loads in GUI, executes in CLI mode)",
)
parser.add_argument(
"--workdir",
type=str,
default=None,
help="Working directory for agent subprocess (overrides config/workflow)",
)
parser.add_argument(
"--init-script",
type=str,
dest="init_script",
default=None,
help="Init script or command to run before each agent (overrides config/workflow)",
)
parser.add_argument(
"--config",
type=str,
default=None,
help="Path to configuration file (default: openloop.json in CWD, "
"falls back to openloop.json next to openloop.py)",
)
parser.add_argument(
"--opencode-defaults",
type=str,
dest="opencode_defaults",
default=None,
help="JSON string overriding opencode defaults for all agents "
"(e.g., '{\"model\":\"gpt-4o\",\"agent\":\"plan\"}')",
)
parser.add_argument(
"--fullscreen",
action="store_true",
help="Start GUI maximized",
)
parser.add_argument(
"--layout",
choices=["default", "preview", "output"],
default="default",
help="GUI layout preset (default: %(default)s)",
)
parser.add_argument(
"--no-log-file",
action="store_true",
help="Disable file logging",
)
parser.add_argument(
"--verbose", "-v",
action="store_true",
help="Stream agent stdout/stderr to terminal during execution",
)
parser.add_argument(
"--log-file",
type=str,
default=None,
help="Explicit log file path (auto-generated timestamp name by default)",
)
parser.add_argument(
"--timeout",
type=int,
default=None,
help="Subprocess timeout in seconds (0 = no timeout, default: 1800)",
)
return parser.parse_args(argv)
def main(argv: list[str] | None = None) -> None:
args = parse_args(argv)
try:
from core.config import Config as _Config
config = _Config.load(args.config)
except ImportError:
config = None
if args.cli:
_run_cli(args, config)
else:
_run_gui(args, config)
def _run_cli(args: argparse.Namespace, config) -> None:
if not args.workflow:
print("Error: --workflow is required in CLI mode")
sys.exit(1)
try:
from json import loads as json_loads
from pathlib import Path
from core.config import Config
from core.engine import ExecutionEngine
from core.runner import OpenCodeOptions
cfg = config if isinstance(config, Config) else Config.load(args.config)
data = json_loads(Path(args.workflow).read_text(encoding="utf-8"))
if args.workdir:
data["workdir"] = str(Path(args.workdir).resolve())
if args.init_script:
data["init_script"] = args.init_script
if args.opencode_defaults:
try:
raw = json_loads(args.opencode_defaults)
if isinstance(raw, dict):
data.setdefault("opencode_defaults", {})
existing = data["opencode_defaults"]
if isinstance(existing, dict):
existing.update(raw)
else:
data["opencode_defaults"] = raw
except ValueError as exc:
print(f"Error: Invalid --opencode-defaults JSON: {exc}")
sys.exit(1)
engine = ExecutionEngine(
config=cfg,
verbose=args.verbose,
no_log_file=args.no_log_file,
log_file=args.log_file,
timeout=args.timeout,
)
engine.execute_workflow_data(data)
state = engine.state
print(f"\nWorkflow finished: {state.termination_reason}")
print(f" Iterations: {state.iteration}")
print(f" Is complete: {state.is_complete}")
if state.termination_reason == "completed":
sys.exit(0)
else:
sys.exit(1)
except ImportError as exc:
print(f"Error: Missing core module — {exc}")
sys.exit(1)
def _run_gui(args: argparse.Namespace, config) -> None:
try:
from ui.app import WorkflowApp
except ImportError as exc:
print(f"Error: Cannot start GUI — {exc}")
print("Install Tkinter or use --cli mode")
sys.exit(1)
app = WorkflowApp(
config_path=args.config,
workflow_path=args.workflow,
workdir=args.workdir,
init_script=args.init_script,
opencode_defaults_raw=args.opencode_defaults,
fullscreen=args.fullscreen,
layout=args.layout,
no_log_file=args.no_log_file,
log_file=args.log_file,
timeout=args.timeout,
)
try:
app.run()
except KeyboardInterrupt:
app.on_closing()
if __name__ == "__main__":
main()