-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathbridge.py
More file actions
executable file
·266 lines (215 loc) · 7.47 KB
/
bridge.py
File metadata and controls
executable file
·266 lines (215 loc) · 7.47 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
#!/usr/bin/env python3
"""
Codex CLI Bridge - Main Orchestrator
Coordinates all components to generate AGENTS.md from Claude Code projects.
Usage:
python bridge.py # Generate AGENTS.md for current directory
python bridge.py --project /path # Generate for specific project
python bridge.py --validate # Validate environment only
"""
import sys
import argparse
from pathlib import Path
from typing import Optional
from safety_mechanism import SafetyMechanism
from project_analyzer import ProjectAnalyzer
from agents_md_generator import AgentsMdGenerator
class CodexCliBridge:
"""
Main orchestrator for Codex CLI Bridge skill.
Coordinates:
1. Safety validation (Codex CLI + CLAUDE.md checks)
2. Project analysis (parse CLAUDE.md, skills, agents)
3. AGENTS.md generation (template-based, reference approach)
"""
def __init__(self, project_root: str, auto_init: bool = True):
"""
Initialize bridge.
Args:
project_root: Path to project root directory
auto_init: Auto-run /init if CLAUDE.md missing (default: True)
"""
self.project_root = Path(project_root).resolve()
self.auto_init = auto_init
# Components
self.safety = SafetyMechanism(str(self.project_root))
self.analyzer: Optional[ProjectAnalyzer] = None
self.generator: Optional[AgentsMdGenerator] = None
def run(self) -> bool:
"""
Execute complete AGENTS.md generation workflow.
Returns:
True if successful, False otherwise
"""
print("=" * 64)
print("CODEX CLI BRIDGE - AGENTS.MD GENERATOR")
print("=" * 64)
print()
# Step 1: Safety validation
print("STEP 1: Environment Validation")
print("-" * 64)
success, message = self.safety.validate_all(auto_init=self.auto_init)
if not success:
print()
print("=" * 64)
print("❌ VALIDATION FAILED")
print("=" * 64)
print(f"\nError: {message}")
print("\nPlease fix the issues above and try again.")
return False
print()
# Step 2: Project analysis
print("STEP 2: Project Analysis")
print("-" * 64)
self.analyzer = ProjectAnalyzer(str(self.project_root))
analysis = self.analyzer.analyze()
print()
# Step 3: AGENTS.md generation
print("STEP 3: AGENTS.md Generation")
print("-" * 64)
self.generator = AgentsMdGenerator(str(self.project_root))
# Convert analysis dict values to objects
from project_analyzer import ProjectMetadata, ProjectStructure
# Reconstruct metadata and structure from dicts
metadata_dict = analysis["metadata"]
structure_dict = analysis["structure"]
metadata = ProjectMetadata(**metadata_dict)
structure = ProjectStructure(**structure_dict)
agents_md_content = self.generator.generate(
metadata=metadata,
structure=structure,
skills=analysis["parsed_data"]["skills"],
agents=analysis["parsed_data"]["agents"],
claude_md_sections=analysis["parsed_data"]["claude_md_sections"],
mcp_servers=analysis["parsed_data"].get("mcp_servers", [])
)
print()
# Step 4: Write AGENTS.md
print("STEP 4: Writing AGENTS.md")
print("-" * 64)
agents_md_path = self.project_root / "AGENTS.md"
try:
with open(agents_md_path, "w", encoding="utf-8") as f:
f.write(agents_md_content)
print(f" ✅ Written to: {agents_md_path}")
print()
# Show summary
print("=" * 64)
print("✅ SUCCESS - AGENTS.MD GENERATED")
print("=" * 64)
print()
print(f"📄 Output: {agents_md_path}")
print(f"📊 Skills documented: {len(analysis['parsed_data']['skills'])}")
print(f"🤖 Agents documented: {len(analysis['parsed_data']['agents'])}")
print()
print("Next steps:")
print(" 1. Review AGENTS.md")
print(" 2. Test with Codex CLI")
print(" 3. Share with team (works in both Claude Code and Codex CLI)")
print()
return True
except Exception as e:
print(f" ❌ Failed to write AGENTS.md: {e}")
print()
print("=" * 64)
print("❌ GENERATION FAILED")
print("=" * 64)
return False
def validate_only(self) -> bool:
"""
Run validation checks only (no generation).
Returns:
True if validation passed, False otherwise
"""
print("=" * 64)
print("CODEX CLI BRIDGE - VALIDATION ONLY")
print("=" * 64)
print()
success, message = self.safety.validate_all(auto_init=self.auto_init)
print()
print("=" * 64)
if success:
print("✅ VALIDATION PASSED")
print("=" * 64)
print()
print("Environment is ready for AGENTS.md generation.")
print()
print("Run without --validate to generate AGENTS.md:")
print(f" python bridge.py --project {self.project_root}")
else:
print("❌ VALIDATION FAILED")
print("=" * 64)
print(f"\nError: {message}")
print()
return success
def get_status(self) -> dict:
"""
Get current status report.
Returns:
Status dictionary
"""
return self.safety.get_status_report()
def main():
"""CLI entry point."""
parser = argparse.ArgumentParser(
description="Codex CLI Bridge - Generate AGENTS.md from CLAUDE.md",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Generate AGENTS.md for current directory
python bridge.py
# Generate for specific project
python bridge.py --project /path/to/project
# Validate environment only (no generation)
python bridge.py --validate
# Don't auto-run /init if CLAUDE.md missing
python bridge.py --no-auto-init
# Show status report
python bridge.py --status
"""
)
parser.add_argument(
"--project",
default=".",
help="Project root directory (default: current directory)"
)
parser.add_argument(
"--validate",
action="store_true",
help="Validate environment only (don't generate AGENTS.md)"
)
parser.add_argument(
"--no-auto-init",
action="store_true",
help="Don't auto-run /init if CLAUDE.md missing"
)
parser.add_argument(
"--status",
action="store_true",
help="Show status report only"
)
args = parser.parse_args()
# Create bridge
bridge = CodexCliBridge(
project_root=args.project,
auto_init=not args.no_auto_init
)
# Execute requested action
if args.status:
status = bridge.get_status()
print("=" * 64)
print("STATUS REPORT")
print("=" * 64)
for key, value in status.items():
print(f"{key}: {value}")
print("=" * 64)
sys.exit(0)
elif args.validate:
success = bridge.validate_only()
sys.exit(0 if success else 1)
else:
# Full generation
success = bridge.run()
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()