import sys from pathlib import Path from gx_compiler.compiler import GXCompiler, CompilerError from runtime_executor.integration import run_gx_with_summary from runtime_executor.gx_loader import load_gx, GXLoaderError from gx_lain.runtime import execute_gx_path as lain_execute_gx_path def cmd_compile(source_path: str, output_path: str = None) -> int: try: source = Path(source_path) if not source.exists(): print(f"Error: Source file not found: {source_path}", file=sys.stderr) return 1 if output_path is None: output_path = str(source.with_suffix(".gx")) print(f"Compiling {source_path} → {output_path}") GXCompiler.compile_to_gx( source_path=source_path, output_path=output_path, verbose=False ) output = Path(output_path) size = output.stat().st_size print(f"Success: {output_path} ({size} bytes)") return 0 except CompilerError as e: print(f"Compilation error: {e}", file=sys.stderr) return 1 except Exception as e: print(f"Error: {e}", file=sys.stderr) return 1 def cmd_run(gx_path: str) -> int: try: gx = Path(gx_path) if not gx.exists(): print(f"Error: File not found: {gx_path}", file=sys.stderr) return 1 print(f"Running {gx_path}") summary = run_gx_with_summary(gx_path) if summary['success']: print(f"Success: Executed {summary['segments_executed']} segments in {summary['duration']:.4f}s") return 0 else: print(f"Failed: {len(summary['errors'])} error(s)") for err in summary['errors']: print(f" - {err}", file=sys.stderr) return 1 except Exception as e: print(f"Error: {e}", file=sys.stderr) return 1 def cmd_inspect(gx_path: str) -> int: try: gx = Path(gx_path) if not gx.exists(): print(f"Error: File not found: {gx_path}", file=sys.stderr) return 1 manifest, payload = load_gx(gx_path) print("\n[Manifest]") print(f" version: {manifest.get('version')}") print(f" source_file: {manifest.get('source_file')}") print(f" source_type: {manifest.get('source_type')}") print(f" origin: {manifest.get('origin')}") print(f" compression_model: {manifest.get('compression_model')}") print(f" contributor: {manifest.get('contributor')}") segments = manifest.get('codex_lineage', {}).get('segments', []) print(f"\n[Segments] ({len(segments)})") for seg in segments: seg_id = seg.get('id') start = seg.get('start') end = seg.get('end') print(f" {seg_id}: lines {start}-{end}") print(f"\n[Payload] {len(payload)} bytes (compressed)") return 0 except GXLoaderError as e: print(f"Load error: {e}", file=sys.stderr) return 1 except Exception as e: print(f"Error: {e}", file=sys.stderr) return 1 def cmd_summary(gx_path: str) -> int: try: gx = Path(gx_path) if not gx.exists(): print(f"Error: File not found: {gx_path}", file=sys.stderr) return 1 manifest, payload = load_gx(gx_path) segments = manifest.get('codex_lineage', {}).get('segments', []) print(f"GX File: {gx_path}") print(f"Source: {manifest.get('source_file')}") print(f"Type: {manifest.get('source_type')}") print(f"Segments: {len(segments)}") print(f"Compressed: {len(payload)} bytes") print(f"Version: {manifest.get('version')}") return 0 except GXLoaderError as e: print(f"Load error: {e}", file=sys.stderr) return 1 except Exception as e: print(f"Error: {e}", file=sys.stderr) return 1 def cmd_lain(gx_path: str, mode: str = "analyze") -> int: try: gx = Path(gx_path) if not gx.exists(): print(f"Error: File not found: {gx_path}", file=sys.stderr) return 1 print(f"Executing {gx_path} through GX→LAIN runtime (mode: {mode})") result = lain_execute_gx_path( gx_path, context={"cognitive_mode": mode} ) # Print output text print() print(result["output_text"]) print() # Print fused symbol print("[Fused Symbol]") print(f" {result['fused_symbol']['summary']}") if result['fused_symbol']['key_points']: print(f" Key points: {', '.join(result['fused_symbol']['key_points'])}") print() # Print diagnostics summary print("[Diagnostics]") elapsed = result['diagnostics']['elapsed'] print(f" Elapsed: {elapsed:.4f}s") print(f" Interface: v{result['diagnostics']['interface_version']}") if result['diagnostics']['errors']: print(f" Errors: {len(result['diagnostics']['errors'])}") for err in result['diagnostics']['errors']: print(f" - {err['type']}: {err['message']}") return 1 return 0 except Exception as e: print(f"Error: {e}", file=sys.stderr) return 1