-
-
Notifications
You must be signed in to change notification settings - Fork 832
Expand file tree
/
Copy pathcodex-flow-wrapper
More file actions
executable file
·125 lines (96 loc) · 3.27 KB
/
Copy pathcodex-flow-wrapper
File metadata and controls
executable file
·125 lines (96 loc) · 3.27 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
#!/usr/bin/env python3
from __future__ import annotations
import json
import os
import signal
import shutil
import subprocess
import sys
from pathlib import Path
RUNTIME_PREFIX = "flow-runtime-"
DEFAULT_J_BIN = Path.home() / "bin" / "j"
def real_codex_bin() -> str:
value = os.environ.get("FLOW_CODEX_REAL_BIN", "").strip()
if value:
return value
return str(DEFAULT_J_BIN)
def validate_real_codex_bin(path: str) -> None:
expanded = Path(path).expanduser()
if expanded.is_absolute():
if expanded.exists():
return
raise SystemExit(f"Flow Codex binary not found: {expanded}")
if shutil.which(path):
return
raise SystemExit(f"Flow Codex binary not found on PATH: {path}")
def agents_skill_root() -> Path:
return Path.home() / ".agents" / "skills"
def load_runtime_state() -> dict | None:
raw_path = os.environ.get("FLOW_CODEX_RUNTIME_STATE", "").strip()
if not raw_path:
return None
path = Path(raw_path).expanduser()
if not path.is_file():
return None
return json.loads(path.read_text(encoding="utf-8"))
def remove_path(path: Path) -> None:
try:
if path.is_symlink() or path.is_file():
path.unlink()
elif path.is_dir():
for child in path.iterdir():
remove_path(child)
path.rmdir()
except FileNotFoundError:
pass
def materialize_runtime_skills(state: dict) -> list[Path]:
token = str(state.get("token", "")).strip()
skills = state.get("skills", [])
if not token or not isinstance(skills, list) or not skills:
return []
root = agents_skill_root()
root.mkdir(parents=True, exist_ok=True)
created: list[Path] = []
for skill in skills:
if not isinstance(skill, dict):
continue
name = str(skill.get("name", "")).strip()
source = str(skill.get("path", "")).strip()
if not name or not source:
continue
source_path = Path(source).expanduser()
if not source_path.is_dir():
continue
target = root / name
if target.exists() or target.is_symlink():
remove_path(target)
os.symlink(source_path, target, target_is_directory=True)
created.append(target)
return created
def cleanup_runtime_symlinks(paths: list[Path]) -> None:
for path in paths:
remove_path(path)
def main() -> int:
state = load_runtime_state()
created = materialize_runtime_skills(state) if state else []
codex_bin = real_codex_bin()
validate_real_codex_bin(codex_bin)
env = dict(os.environ)
runtime_state_path = env.get("FLOW_CODEX_RUNTIME_STATE", "").strip()
if runtime_state_path:
env["FLOW_CODEX_RUNTIME_STATE_PATH"] = runtime_state_path
env.pop("FLOW_CODEX_RUNTIME_STATE", None)
proc = None
def forward_signal(signum: int, _frame) -> None:
nonlocal proc
if proc is not None:
proc.send_signal(signum)
for signum in (signal.SIGINT, signal.SIGTERM):
signal.signal(signum, forward_signal)
try:
proc = subprocess.Popen([codex_bin, *sys.argv[1:]], env=env)
return proc.wait()
finally:
cleanup_runtime_symlinks(created)
if __name__ == "__main__":
raise SystemExit(main())