|
| 1 | +/** |
| 2 | + * Extension Host Process |
| 3 | + * |
| 4 | + * This script runs as a separate Node.js process (spawned via fork()). |
| 5 | + * It receives IPC messages from the main cmux process, loads extensions once, |
| 6 | + * maintains a map of workspace runtimes, and dispatches hooks to extensions. |
| 7 | + * |
| 8 | + * A single shared extension host serves all workspaces (VS Code architecture). |
| 9 | + */ |
| 10 | + |
| 11 | +import type { Runtime } from "../../runtime/Runtime"; |
| 12 | +import type { |
| 13 | + Extension, |
| 14 | + ExtensionHostMessage, |
| 15 | + ExtensionHostResponse, |
| 16 | + ExtensionInfo, |
| 17 | +} from "../../types/extensions"; |
| 18 | + |
| 19 | +const workspaceRuntimes = new Map<string, Runtime>(); |
| 20 | +const extensions: Array<{ id: string; module: Extension }> = []; |
| 21 | + |
| 22 | +/** |
| 23 | + * Send a message to the parent process |
| 24 | + */ |
| 25 | +function sendMessage(message: ExtensionHostResponse): void { |
| 26 | + if (process.send) { |
| 27 | + process.send(message); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Load an extension from its entrypoint path |
| 33 | + */ |
| 34 | +async function loadExtension(extInfo: ExtensionInfo): Promise<void> { |
| 35 | + try { |
| 36 | + // Dynamic import to load the extension module |
| 37 | + // Extensions must export a default object with hook handlers |
| 38 | + // eslint-disable-next-line no-restricted-syntax, @typescript-eslint/no-unsafe-assignment -- Dynamic import required for user extensions |
| 39 | + const module = await import(extInfo.path); |
| 40 | + |
| 41 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- User-provided extension module |
| 42 | + if (!module.default) { |
| 43 | + throw new Error(`Extension ${extInfo.id} does not export a default object`); |
| 44 | + } |
| 45 | + |
| 46 | + extensions.push({ |
| 47 | + id: extInfo.id, |
| 48 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- User-provided extension module |
| 49 | + module: module.default as Extension, |
| 50 | + }); |
| 51 | + |
| 52 | + console.log(`[ExtensionHost] Loaded extension: ${extInfo.id}`); |
| 53 | + } catch (error) { |
| 54 | + const errorMsg = error instanceof Error ? error.message : String(error); |
| 55 | + console.error(`[ExtensionHost] Failed to load extension ${extInfo.id}:`, errorMsg); |
| 56 | + sendMessage({ |
| 57 | + type: "extension-load-error", |
| 58 | + id: extInfo.id, |
| 59 | + error: errorMsg, |
| 60 | + }); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Initialize the extension host (load extensions globally) |
| 66 | + */ |
| 67 | +async function handleInit(msg: Extract<ExtensionHostMessage, { type: "init" }>): Promise<void> { |
| 68 | + try { |
| 69 | + const { extensions: extensionList } = msg; |
| 70 | + |
| 71 | + console.log(`[ExtensionHost] Initializing with ${extensionList.length} extension(s)`); |
| 72 | + |
| 73 | + // Load all extensions once |
| 74 | + for (const extInfo of extensionList) { |
| 75 | + await loadExtension(extInfo); |
| 76 | + } |
| 77 | + |
| 78 | + // Send ready message |
| 79 | + sendMessage({ |
| 80 | + type: "ready", |
| 81 | + extensionCount: extensions.length, |
| 82 | + }); |
| 83 | + |
| 84 | + console.log(`[ExtensionHost] Ready with ${extensions.length} loaded extension(s)`); |
| 85 | + } catch (error) { |
| 86 | + console.error("[ExtensionHost] Failed to initialize:", error); |
| 87 | + process.exit(1); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Register a workspace with the extension host |
| 93 | + */ |
| 94 | +async function handleRegisterWorkspace( |
| 95 | + msg: Extract<ExtensionHostMessage, { type: "register-workspace" }> |
| 96 | +): Promise<void> { |
| 97 | + try { |
| 98 | + const { workspaceId, runtimeConfig } = msg; |
| 99 | + |
| 100 | + // Dynamically import createRuntime to avoid bundling issues |
| 101 | + // eslint-disable-next-line no-restricted-syntax -- Required in child process to avoid circular deps |
| 102 | + const { createRuntime } = await import("../../runtime/runtimeFactory"); |
| 103 | + |
| 104 | + // Create runtime for this workspace |
| 105 | + const runtime = createRuntime(runtimeConfig); |
| 106 | + workspaceRuntimes.set(workspaceId, runtime); |
| 107 | + |
| 108 | + console.log(`[ExtensionHost] Registered workspace ${workspaceId}`); |
| 109 | + |
| 110 | + // Send confirmation |
| 111 | + sendMessage({ |
| 112 | + type: "workspace-registered", |
| 113 | + workspaceId, |
| 114 | + }); |
| 115 | + } catch (error) { |
| 116 | + console.error(`[ExtensionHost] Failed to register workspace:`, error); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Unregister a workspace from the extension host |
| 122 | + */ |
| 123 | +function handleUnregisterWorkspace( |
| 124 | + msg: Extract<ExtensionHostMessage, { type: "unregister-workspace" }> |
| 125 | +): void { |
| 126 | + const { workspaceId } = msg; |
| 127 | + |
| 128 | + workspaceRuntimes.delete(workspaceId); |
| 129 | + console.log(`[ExtensionHost] Unregistered workspace ${workspaceId}`); |
| 130 | + |
| 131 | + sendMessage({ |
| 132 | + type: "workspace-unregistered", |
| 133 | + workspaceId, |
| 134 | + }); |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * Dispatch post-tool-use hook to all extensions |
| 139 | + */ |
| 140 | +async function handlePostToolUse( |
| 141 | + msg: Extract<ExtensionHostMessage, { type: "post-tool-use" }> |
| 142 | +): Promise<void> { |
| 143 | + const { payload } = msg; |
| 144 | + |
| 145 | + // Get runtime for this workspace |
| 146 | + const runtime = workspaceRuntimes.get(payload.workspaceId); |
| 147 | + if (!runtime) { |
| 148 | + console.warn( |
| 149 | + `[ExtensionHost] Runtime not found for workspace ${payload.workspaceId}, skipping hook` |
| 150 | + ); |
| 151 | + sendMessage({ |
| 152 | + type: "hook-complete", |
| 153 | + hookType: "post-tool-use", |
| 154 | + }); |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + // Dispatch to all extensions sequentially |
| 159 | + for (const { id, module } of extensions) { |
| 160 | + if (!module.onPostToolUse) { |
| 161 | + continue; |
| 162 | + } |
| 163 | + |
| 164 | + try { |
| 165 | + // Call the extension's hook handler with runtime access |
| 166 | + await module.onPostToolUse({ |
| 167 | + ...payload, |
| 168 | + runtime, |
| 169 | + }); |
| 170 | + } catch (error) { |
| 171 | + const errorMsg = error instanceof Error ? error.message : String(error); |
| 172 | + console.error(`[ExtensionHost] Extension ${id} threw error in onPostToolUse:`, errorMsg); |
| 173 | + sendMessage({ |
| 174 | + type: "extension-error", |
| 175 | + extensionId: id, |
| 176 | + error: errorMsg, |
| 177 | + }); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + // Acknowledge completion |
| 182 | + sendMessage({ |
| 183 | + type: "hook-complete", |
| 184 | + hookType: "post-tool-use", |
| 185 | + }); |
| 186 | +} |
| 187 | + |
| 188 | +/** |
| 189 | + * Handle shutdown request |
| 190 | + */ |
| 191 | +function handleShutdown(): void { |
| 192 | + console.log("[ExtensionHost] Shutting down"); |
| 193 | + process.exit(0); |
| 194 | +} |
| 195 | + |
| 196 | +/** |
| 197 | + * Main message handler |
| 198 | + */ |
| 199 | +process.on("message", (msg: ExtensionHostMessage) => { |
| 200 | + void (async () => { |
| 201 | + try { |
| 202 | + switch (msg.type) { |
| 203 | + case "init": |
| 204 | + await handleInit(msg); |
| 205 | + break; |
| 206 | + case "register-workspace": |
| 207 | + await handleRegisterWorkspace(msg); |
| 208 | + break; |
| 209 | + case "unregister-workspace": |
| 210 | + handleUnregisterWorkspace(msg); |
| 211 | + break; |
| 212 | + case "post-tool-use": |
| 213 | + await handlePostToolUse(msg); |
| 214 | + break; |
| 215 | + case "shutdown": |
| 216 | + handleShutdown(); |
| 217 | + break; |
| 218 | + default: |
| 219 | + console.warn(`[ExtensionHost] Unknown message type:`, msg); |
| 220 | + } |
| 221 | + } catch (error) { |
| 222 | + console.error("[ExtensionHost] Error handling message:", error); |
| 223 | + } |
| 224 | + })(); |
| 225 | +}); |
| 226 | + |
| 227 | +// Handle process errors |
| 228 | +process.on("uncaughtException", (error) => { |
| 229 | + console.error("[ExtensionHost] Uncaught exception:", error); |
| 230 | + process.exit(1); |
| 231 | +}); |
| 232 | + |
| 233 | +process.on("unhandledRejection", (reason) => { |
| 234 | + console.error("[ExtensionHost] Unhandled rejection:", reason); |
| 235 | + process.exit(1); |
| 236 | +}); |
| 237 | + |
| 238 | +console.log("[ExtensionHost] Process started, waiting for init message"); |
0 commit comments