|
| 1 | +/** |
| 2 | + * Feature Flag: Antigravity Panel |
| 3 | + * |
| 4 | + * All Antigravity connection/diagnostic UI or workflow changes must be wrapped |
| 5 | + * with this flag, per Operational-Workflow-Rules. This enables safe, incremental |
| 6 | + * rollout and easy toggling without redeployment. |
| 7 | + * |
| 8 | + * Control via environment variable (recommended): |
| 9 | + * VITE_FEATURE_ANTIGRAVITY=true — enable |
| 10 | + * VITE_FEATURE_ANTIGRAVITY=false — disable (default) |
| 11 | + * |
| 12 | + * See env.main-site.example and APPLY_TO_MAIN_SITE.md for conventions. |
| 13 | + */ |
| 14 | +export const FEATURE_ANTIGRAVITY: boolean = |
| 15 | + typeof import.meta !== "undefined" && |
| 16 | + import.meta.env && |
| 17 | + typeof import.meta.env.VITE_FEATURE_ANTIGRAVITY !== "undefined" |
| 18 | + ? String(import.meta.env.VITE_FEATURE_ANTIGRAVITY).toLowerCase() === "true" |
| 19 | + : false; |
| 20 | + |
| 21 | +/** |
| 22 | + * Run the Antigravity diagnostic PowerShell script and return a structured result. |
| 23 | + * |
| 24 | + * - Only executes when FEATURE_ANTIGRAVITY is true. |
| 25 | + * - Requires a Node.js / Deno-capable runtime (not available in the browser). |
| 26 | + * - Runs `scripts/antigravity-diagnostic.ps1` via PowerShell. |
| 27 | + * - Returns `{ status, details }` for downstream UI or workflow handling. |
| 28 | + * - Follows Monitoring-Quality-Rules: errors produce an escalatable object. |
| 29 | + * |
| 30 | + * Possible status values: |
| 31 | + * "success" — language server process was found. |
| 32 | + * "no_process" — script ran but found no language server process. |
| 33 | + * "error" — flag disabled, wrong environment, or script failed. |
| 34 | + * "escalate" — script output signals uncertainty; human review needed. |
| 35 | + */ |
| 36 | +export async function runAntigravityDiagnostic(): Promise<{ |
| 37 | + status: "success" | "no_process" | "error" | "escalate"; |
| 38 | + details: string; |
| 39 | +}> { |
| 40 | + if (!FEATURE_ANTIGRAVITY) { |
| 41 | + return { |
| 42 | + status: "error", |
| 43 | + details: "Antigravity diagnostics are disabled by feature flag.", |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + // Guard: cannot run PowerShell in a pure browser environment. |
| 48 | + if (typeof window !== "undefined" && typeof (window as Window & { process?: unknown }).process === "undefined") { |
| 49 | + return { |
| 50 | + status: "error", |
| 51 | + details: "Cannot run PowerShell diagnostics in browser environment.", |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + try { |
| 56 | + // Dynamic imports so this module remains safe to bundle in browser contexts |
| 57 | + // even though the runtime branch above will never reach here in a browser. |
| 58 | + const { exec } = await import("child_process"); |
| 59 | + const util = await import("util"); |
| 60 | + const path = await import("path"); |
| 61 | + const execAsync = util.promisify(exec); |
| 62 | + |
| 63 | + const scriptPath = path.join("scripts", "antigravity-diagnostic.ps1"); |
| 64 | + const { stdout } = await execAsync( |
| 65 | + `powershell -NoProfile -ExecutionPolicy Bypass -File "${scriptPath}"` |
| 66 | + ); |
| 67 | + |
| 68 | + if ( |
| 69 | + stdout?.includes("no language_server_windows_x64") || |
| 70 | + stdout?.toLowerCase().includes("no_process") |
| 71 | + ) { |
| 72 | + return { status: "no_process", details: stdout.trim() }; |
| 73 | + } |
| 74 | + |
| 75 | + return { status: "success", details: stdout?.trim() || "Diagnostic succeeded." }; |
| 76 | + } catch (err: unknown) { |
| 77 | + const e = err as { stderr?: string; message?: string }; |
| 78 | + const detail = e?.stderr || e?.message || "Unknown error in diagnosis script"; |
| 79 | + return { |
| 80 | + status: |
| 81 | + detail.includes("uncertain") || detail.includes("cannot detect") |
| 82 | + ? "escalate" |
| 83 | + : "error", |
| 84 | + details: detail, |
| 85 | + }; |
| 86 | + } |
| 87 | +} |
0 commit comments