-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodex_scan.py
More file actions
282 lines (242 loc) · 9.33 KB
/
Copy pathcodex_scan.py
File metadata and controls
282 lines (242 loc) · 9.33 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from __future__ import annotations
import json
import os
import re
import tomllib
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Callable
from urllib.parse import urlparse
CODEX_HOME = Path(
os.environ.get("CODEX_HOME", Path.home() / ".codex")
).expanduser()
def load_toml(path: Path) -> dict[str, Any]:
if not path.is_file():
return {}
with path.open("rb") as file:
return tomllib.load(file)
def read_skill_frontmatter(path: Path) -> dict[str, str]:
"""Read the simple name and description fields from SKILL.md."""
text = path.read_text(encoding="utf-8", errors="replace")
result = {"name": path.parent.name, "description": ""}
if not text.startswith("---"):
return result
parts = text.split("---", 2)
if len(parts) < 3:
return result
for key in ("name", "description"):
match = re.search(
rf"(?m)^\s*{key}\s*:\s*[\"']?(.*?)[\"']?\s*$",
parts[1],
)
if match:
result[key] = match.group(1).strip()
return result
def _relative_path(path: Path, root: Path) -> str:
try:
return str(path.resolve().relative_to(root.resolve()))
except (OSError, ValueError):
return path.name
def _skill_source(path: Path) -> str:
lowered = {part.lower() for part in path.parts}
if "plugins" in lowered:
return "plugin"
if ".system" in lowered:
return "system"
return "user"
def scan_skills(codex_home: Path = CODEX_HOME) -> list[dict[str, Any]]:
skill_files: set[Path] = set()
for root in (codex_home / "skills", codex_home / "plugins"):
if not root.is_dir():
continue
try:
skill_files.update(root.rglob("SKILL.md"))
except OSError:
continue
skills = []
for skill_file in skill_files:
try:
skills.append({
**read_skill_frontmatter(skill_file),
"source": _skill_source(skill_file),
"path": _relative_path(skill_file, codex_home),
"status": "ready",
})
except OSError as exc:
skills.append({
"name": skill_file.parent.name,
"description": "",
"source": _skill_source(skill_file),
"path": _relative_path(skill_file, codex_home),
"status": "error",
"error": str(exc),
})
return sorted(skills, key=lambda item: item["name"].casefold())
def _url_label(value: Any) -> str | None:
if not isinstance(value, str) or not value:
return None
parsed = urlparse(value)
return parsed.netloc or parsed.path or None
def scan_mcp_servers(codex_home: Path = CODEX_HOME) -> list[dict[str, Any]]:
servers = load_toml(codex_home / "config.toml").get("mcp_servers", {})
if not isinstance(servers, dict):
return []
result = []
for name, settings in servers.items():
if not isinstance(settings, dict):
continue
env = settings.get("env", {})
command = settings.get("command")
enabled = bool(settings.get("enabled", True))
result.append({
"name": str(name),
"transport": "http" if settings.get("url") else "stdio",
"endpoint": _url_label(settings.get("url")),
"command": Path(command).name if isinstance(command, str) else None,
"enabled": enabled,
"env_keys": sorted(env.keys()) if isinstance(env, dict) else [],
"status": "ready" if enabled else "disabled",
})
return sorted(result, key=lambda item: item["name"].casefold())
def scan_plugins(codex_home: Path = CODEX_HOME) -> list[dict[str, Any]]:
plugin_settings = load_toml(codex_home / "config.toml").get("plugins", {})
if not isinstance(plugin_settings, dict):
plugin_settings = {}
root = codex_home / "plugins"
if not root.is_dir():
return []
plugins = []
try:
manifests = root.glob("**/.codex-plugin/plugin.json")
for manifest_path in manifests:
try:
manifest = json.loads(
manifest_path.read_text(encoding="utf-8", errors="replace")
)
name = str(manifest.get("name") or manifest_path.parent.parent.name)
matching = plugin_settings.get(name, {})
enabled = matching.get("enabled") if isinstance(matching, dict) else None
plugins.append({
"name": name,
"version": manifest.get("version"),
"description": manifest.get("description", ""),
"enabled": enabled,
"path": _relative_path(manifest_path, codex_home),
"status": "ready" if enabled is not False else "disabled",
})
except (OSError, json.JSONDecodeError) as exc:
plugins.append({
"name": manifest_path.parent.parent.name,
"version": None,
"description": "",
"enabled": None,
"path": _relative_path(manifest_path, codex_home),
"status": "error",
"error": str(exc),
})
except OSError:
return []
return sorted(plugins, key=lambda item: item["name"].casefold())
def scan_automations(codex_home: Path = CODEX_HOME) -> list[dict[str, Any]]:
root = codex_home / "automations"
if not root.is_dir():
return []
automations = []
try:
configs = root.glob("*/automation.toml")
for config_path in configs:
try:
config = load_toml(config_path)
automations.append({
"id": str(config.get("id") or config_path.parent.name),
"name": str(config.get("name") or config_path.parent.name),
"status": str(config.get("status") or "active"),
"kind": str(config.get("kind") or "scheduled"),
"schedule": config.get("rrule"),
"path": _relative_path(config_path, codex_home),
})
except (OSError, tomllib.TOMLDecodeError) as exc:
automations.append({
"id": config_path.parent.name,
"name": config_path.parent.name,
"status": "error",
"kind": "unknown",
"schedule": None,
"path": _relative_path(config_path, codex_home),
"error": str(exc),
})
except OSError:
return []
return sorted(automations, key=lambda item: item["name"].casefold())
def find_agents_files(
project_root: Path | None = None,
target_dir: Path | None = None,
codex_home: Path = CODEX_HOME,
) -> list[dict[str, str]]:
"""Find applicable AGENTS.md files without returning their contents."""
result = []
global_agents = codex_home / "AGENTS.md"
if global_agents.is_file():
result.append({
"scope": "global",
"name": "Global rules",
"path": _relative_path(global_agents, codex_home),
})
if project_root is None:
return result
project_root = project_root.resolve()
target_dir = (target_dir or project_root).resolve()
try:
relative = target_dir.relative_to(project_root)
except ValueError as exc:
raise ValueError("target_dir must be inside project_root") from exc
directories = [project_root]
current = project_root
for part in relative.parts:
current = current / part
directories.append(current)
for directory in directories:
agents_file = directory / "AGENTS.md"
if agents_file.is_file():
result.append({
"scope": "project",
"name": directory.name or "Project rules",
"path": str(agents_file.relative_to(project_root)),
})
return result
def _capture(
label: str,
operation: Callable[[], list[dict[str, Any]]],
errors: list[dict[str, str]],
) -> list[dict[str, Any]]:
try:
return operation()
except (OSError, tomllib.TOMLDecodeError, ValueError) as exc:
errors.append({"source": label, "message": str(exc)})
return []
def scan_codex(
project_root: Path | None = None,
codex_home: Path = CODEX_HOME,
) -> dict[str, Any]:
errors: list[dict[str, str]] = []
skills = _capture("skills", lambda: scan_skills(codex_home), errors)
mcp_servers = _capture("mcp_servers", lambda: scan_mcp_servers(codex_home), errors)
plugins = _capture("plugins", lambda: scan_plugins(codex_home), errors)
automations = _capture("automations", lambda: scan_automations(codex_home), errors)
agents = _capture(
"agents",
lambda: find_agents_files(project_root, codex_home=codex_home),
errors,
)
return {
"scanned_at": datetime.now(timezone.utc).isoformat(),
"project": project_root.name if project_root else None,
"skills": skills,
"mcp_servers": mcp_servers,
"plugins": plugins,
"automations": automations,
"agents": agents,
"errors": errors,
}
if __name__ == "__main__":
print(json.dumps(scan_codex(Path.cwd()), ensure_ascii=False, indent=2, default=str))