|
| 1 | +import { ipcMain } from "electron"; |
| 2 | +import { |
| 3 | + NATIVE_BRIDGE_CHANNEL, |
| 4 | + NATIVE_BRIDGE_VERSION, |
| 5 | + type NativeBridgeErrorCode, |
| 6 | + type NativeBridgeRequest, |
| 7 | + type NativeBridgeResponse, |
| 8 | + type NativePlatform, |
| 9 | + type ProjectFileResult, |
| 10 | + type ProjectPathResult, |
| 11 | +} from "../../src/native/contracts"; |
| 12 | +import type { CursorTelemetryLoadResult } from "../native-bridge/cursor/adapter"; |
| 13 | +import { TelemetryCursorAdapter } from "../native-bridge/cursor/telemetryCursorAdapter"; |
| 14 | +import { CursorService } from "../native-bridge/services/cursorService"; |
| 15 | +import { ProjectService } from "../native-bridge/services/projectService"; |
| 16 | +import { SystemService } from "../native-bridge/services/systemService"; |
| 17 | +import { NativeBridgeStateStore } from "../native-bridge/store"; |
| 18 | + |
| 19 | +export interface NativeBridgeContext { |
| 20 | + getPlatform: () => NodeJS.Platform; |
| 21 | + getCurrentProjectPath: () => string | null; |
| 22 | + getCurrentVideoPath: () => string | null; |
| 23 | + saveProjectFile: ( |
| 24 | + projectData: unknown, |
| 25 | + suggestedName?: string, |
| 26 | + existingProjectPath?: string, |
| 27 | + ) => Promise<ProjectFileResult>; |
| 28 | + loadProjectFile: () => Promise<ProjectFileResult>; |
| 29 | + loadCurrentProjectFile: () => Promise<ProjectFileResult>; |
| 30 | + setCurrentVideoPath: (path: string) => ProjectPathResult; |
| 31 | + getCurrentVideoPathResult: () => ProjectPathResult; |
| 32 | + clearCurrentVideoPath: () => ProjectPathResult; |
| 33 | + resolveAssetBasePath: () => string | null; |
| 34 | + resolveVideoPath: (videoPath?: string | null) => string | null; |
| 35 | + loadCursorRecordingData: ( |
| 36 | + videoPath: string, |
| 37 | + ) => Promise<import("../../src/native/contracts").CursorRecordingData>; |
| 38 | + loadCursorTelemetry: (videoPath: string) => Promise<CursorTelemetryLoadResult>; |
| 39 | +} |
| 40 | + |
| 41 | +function normalizePlatform(platform: NodeJS.Platform): NativePlatform { |
| 42 | + if (platform === "darwin" || platform === "win32") { |
| 43 | + return platform; |
| 44 | + } |
| 45 | + |
| 46 | + return "linux"; |
| 47 | +} |
| 48 | + |
| 49 | +function createMeta(requestId?: string) { |
| 50 | + return { |
| 51 | + version: NATIVE_BRIDGE_VERSION, |
| 52 | + requestId: requestId || `native-${Date.now()}`, |
| 53 | + timestampMs: Date.now(), |
| 54 | + } as const; |
| 55 | +} |
| 56 | + |
| 57 | +function createSuccessResponse<TData>(requestId: string | undefined, data: TData) { |
| 58 | + return { |
| 59 | + ok: true, |
| 60 | + data, |
| 61 | + meta: createMeta(requestId), |
| 62 | + } satisfies NativeBridgeResponse<TData>; |
| 63 | +} |
| 64 | + |
| 65 | +function createErrorResponse( |
| 66 | + requestId: string | undefined, |
| 67 | + code: NativeBridgeErrorCode, |
| 68 | + message: string, |
| 69 | + retryable = false, |
| 70 | +) { |
| 71 | + return { |
| 72 | + ok: false, |
| 73 | + error: { |
| 74 | + code, |
| 75 | + message, |
| 76 | + retryable, |
| 77 | + }, |
| 78 | + meta: createMeta(requestId), |
| 79 | + } satisfies NativeBridgeResponse; |
| 80 | +} |
| 81 | + |
| 82 | +function isBridgeRequest(value: unknown): value is NativeBridgeRequest { |
| 83 | + if (!value || typeof value !== "object") { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + const candidate = value as Partial<NativeBridgeRequest>; |
| 88 | + return typeof candidate.domain === "string" && typeof candidate.action === "string"; |
| 89 | +} |
| 90 | + |
| 91 | +export function registerNativeBridgeHandlers(context: NativeBridgeContext) { |
| 92 | + ipcMain.removeHandler(NATIVE_BRIDGE_CHANNEL); |
| 93 | + |
| 94 | + const platform = normalizePlatform(context.getPlatform()); |
| 95 | + const store = new NativeBridgeStateStore(platform); |
| 96 | + const projectService = new ProjectService({ |
| 97 | + store, |
| 98 | + getCurrentProjectPath: context.getCurrentProjectPath, |
| 99 | + getCurrentVideoPath: context.getCurrentVideoPath, |
| 100 | + saveProjectFile: context.saveProjectFile, |
| 101 | + loadProjectFile: context.loadProjectFile, |
| 102 | + loadCurrentProjectFile: context.loadCurrentProjectFile, |
| 103 | + setCurrentVideoPath: context.setCurrentVideoPath, |
| 104 | + getCurrentVideoPathResult: context.getCurrentVideoPathResult, |
| 105 | + clearCurrentVideoPath: context.clearCurrentVideoPath, |
| 106 | + }); |
| 107 | + const cursorService = new CursorService({ |
| 108 | + store, |
| 109 | + adapter: new TelemetryCursorAdapter({ |
| 110 | + loadRecordingData: context.loadCursorRecordingData, |
| 111 | + resolveVideoPath: context.resolveVideoPath, |
| 112 | + loadTelemetry: context.loadCursorTelemetry, |
| 113 | + }), |
| 114 | + }); |
| 115 | + const systemService = new SystemService({ |
| 116 | + store, |
| 117 | + getPlatform: () => platform, |
| 118 | + getAssetBasePath: context.resolveAssetBasePath, |
| 119 | + getCursorCapabilities: () => cursorService.getCapabilities(), |
| 120 | + }); |
| 121 | + |
| 122 | + ipcMain.handle(NATIVE_BRIDGE_CHANNEL, async (_, request: unknown) => { |
| 123 | + if (!isBridgeRequest(request)) { |
| 124 | + return createErrorResponse(undefined, "INVALID_REQUEST", "Invalid native bridge request."); |
| 125 | + } |
| 126 | + |
| 127 | + const requestId = request.requestId; |
| 128 | + const domain = request.domain as string; |
| 129 | + |
| 130 | + try { |
| 131 | + switch (request.domain) { |
| 132 | + case "system": { |
| 133 | + const action = request.action as string; |
| 134 | + switch (request.action) { |
| 135 | + case "getPlatform": |
| 136 | + return createSuccessResponse(requestId, systemService.getPlatform()); |
| 137 | + case "getAssetBasePath": |
| 138 | + return createSuccessResponse(requestId, systemService.getAssetBasePath()); |
| 139 | + case "getCapabilities": |
| 140 | + return createSuccessResponse(requestId, await systemService.getCapabilities()); |
| 141 | + default: |
| 142 | + return createErrorResponse( |
| 143 | + requestId, |
| 144 | + "UNSUPPORTED_ACTION", |
| 145 | + `Unsupported system action: ${action}`, |
| 146 | + ); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + case "project": { |
| 151 | + const action = request.action as string; |
| 152 | + switch (request.action) { |
| 153 | + case "getCurrentContext": |
| 154 | + return createSuccessResponse(requestId, projectService.getCurrentContext()); |
| 155 | + case "saveProjectFile": |
| 156 | + return createSuccessResponse( |
| 157 | + requestId, |
| 158 | + await projectService.saveProjectFile( |
| 159 | + request.payload.projectData, |
| 160 | + request.payload.suggestedName, |
| 161 | + request.payload.existingProjectPath, |
| 162 | + ), |
| 163 | + ); |
| 164 | + case "loadProjectFile": |
| 165 | + return createSuccessResponse(requestId, await projectService.loadProjectFile()); |
| 166 | + case "loadCurrentProjectFile": |
| 167 | + return createSuccessResponse( |
| 168 | + requestId, |
| 169 | + await projectService.loadCurrentProjectFile(), |
| 170 | + ); |
| 171 | + case "setCurrentVideoPath": |
| 172 | + return createSuccessResponse( |
| 173 | + requestId, |
| 174 | + projectService.setCurrentVideoPath(request.payload.path), |
| 175 | + ); |
| 176 | + case "getCurrentVideoPath": |
| 177 | + return createSuccessResponse(requestId, projectService.getCurrentVideoPath()); |
| 178 | + case "clearCurrentVideoPath": |
| 179 | + return createSuccessResponse(requestId, projectService.clearCurrentVideoPath()); |
| 180 | + default: |
| 181 | + return createErrorResponse( |
| 182 | + requestId, |
| 183 | + "UNSUPPORTED_ACTION", |
| 184 | + `Unsupported project action: ${action}`, |
| 185 | + ); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + case "cursor": { |
| 190 | + const action = request.action as string; |
| 191 | + switch (request.action) { |
| 192 | + case "getCapabilities": |
| 193 | + return createSuccessResponse(requestId, await cursorService.getCapabilities()); |
| 194 | + case "getTelemetry": |
| 195 | + return createSuccessResponse( |
| 196 | + requestId, |
| 197 | + await cursorService.getTelemetry(request.payload?.videoPath), |
| 198 | + ); |
| 199 | + case "getRecordingData": |
| 200 | + return createSuccessResponse( |
| 201 | + requestId, |
| 202 | + await cursorService.getRecordingData(request.payload?.videoPath), |
| 203 | + ); |
| 204 | + default: |
| 205 | + return createErrorResponse( |
| 206 | + requestId, |
| 207 | + "UNSUPPORTED_ACTION", |
| 208 | + `Unsupported cursor action: ${action}`, |
| 209 | + ); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + default: |
| 214 | + return createErrorResponse( |
| 215 | + requestId, |
| 216 | + "UNSUPPORTED_ACTION", |
| 217 | + `Unsupported bridge domain: ${domain}`, |
| 218 | + ); |
| 219 | + } |
| 220 | + } catch (error) { |
| 221 | + return createErrorResponse( |
| 222 | + requestId, |
| 223 | + "INTERNAL_ERROR", |
| 224 | + error instanceof Error ? error.message : "Unknown native bridge error.", |
| 225 | + true, |
| 226 | + ); |
| 227 | + } |
| 228 | + }); |
| 229 | +} |
0 commit comments