feat(desktop): add opt-in cua-driver computer use #4354
Closedinjaneity(GitHub profile, opens in a new tab)wants to merge 241 commits into
t3code/codex-turn-mapping fromrefs/pull/4354/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/desktop/src/app/DesktopApp.ts lines 267 to 274OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -247,0 +254,21 @@254+ onSome: (settings) => settings.enableCua,255+ });256+ if (cuaEnabled) {257+ yield* startEmbeddedCuaDriver(258+ environment.appUserModelId,259+ environment.isPackaged ? environment.resourcesPath : undefined,260+ ).pipe(261+ Effect.catch((error) =>262+ Effect.gen(function* () {263+ yield* logStartupError("embedded cua-driver failed to start", {264+ binaryPath: error.binaryPath,265+ cause: String(error.cause),266+ });267+ yield* electronDialog.showMessageBox({268+ type: "error",269+ title: "Computer use unavailable",270+ message: "Cua could not start.",271+ detail:272+ "T3 Code will continue without computer use. Check the logs, then restart to try again.",273+ buttons: ["Continue"],274+ });
- apps/server/src/provider/Layers/codexLaunchArgs.ts line 12OutdatedResolvedby injaneity(GitHub profile, opens in a new tab)@@ -1,8 +1,12 @@11 import { tokenizeCliArgs } from "@t3tools/shared/cliArgs";2233 export const T3CODE_CODEX_LAUNCH_ARGS_ENV = "T3CODE_CODEX_LAUNCH_ARGS";4+export const T3CODE_CODEX_APPEND_LAUNCH_ARGS_ENV = "T3CODE_CODEX_APPEND_LAUNCH_ARGS";4556 export const resolveCodexLaunchArgs = (67 launchArgs?: string,78 environment: NodeJS.ProcessEnv = process.env,8-) => environment[T3CODE_CODEX_LAUNCH_ARGS_ENV]?.trim() || launchArgs?.trim() || "";9+) => {10+ const configured = environment[T3CODE_CODEX_LAUNCH_ARGS_ENV]?.trim() || launchArgs?.trim() || "";11+ const appended = environment[T3CODE_CODEX_APPEND_LAUNCH_ARGS_ENV]?.trim() || "";12+ return [configured, appended].filter((value) => value.length > 0).join(" ");
- apps/desktop/src/cua/CuaDriverEmbedded.ts line 10OutdatedResolvedby injaneity(GitHub profile, opens in a new tab)@@ -0,0 +1,10 @@1+import * as Effect from "effect/Effect";2+import * as Option from "effect/Option";3+import * as Path from "effect/Path";4+import * as Schema from "effect/Schema";5+6+import type { EmbeddedDriverConnection } from "@trycua/cua-driver/embedded";7+8+export const T3CODE_CUA_DRIVER_PATH_ENV = "T3CODE_CUA_DRIVER_PATH";9+export const T3CODE_CODEX_APPEND_LAUNCH_ARGS_ENV = "T3CODE_CODEX_APPEND_LAUNCH_ARGS";10+export const T3CODE_CODEX_APPEND_THREAD_CONFIG_ENV = "T3CODE_CODEX_APPEND_THREAD_CONFIG";
- apps/desktop/src/cua/CuaDriverEmbedded.ts lines 16 to 16OutdatedResolvedby injaneity(GitHub profile, opens in a new tab)@@ -0,0 +1,16 @@1+import * as Effect from "effect/Effect";2+import * as Option from "effect/Option";3+import * as Path from "effect/Path";4+import * as Schema from "effect/Schema";5+6+import type { EmbeddedDriverConnection } from "@trycua/cua-driver/embedded";7+8+export const T3CODE_CUA_DRIVER_PATH_ENV = "T3CODE_CUA_DRIVER_PATH";9+export const T3CODE_CODEX_APPEND_LAUNCH_ARGS_ENV = "T3CODE_CODEX_APPEND_LAUNCH_ARGS";10+export const T3CODE_CODEX_APPEND_THREAD_CONFIG_ENV = "T3CODE_CODEX_APPEND_THREAD_CONFIG";11+12+export class CuaDriverStartError extends Schema.TaggedErrorClass<CuaDriverStartError>()(13+ "CuaDriverStartError",14+ {15+ binaryPath: Schema.String,16+ cause: Schema.Defect(),
- apps/desktop/src/app/DesktopApp.ts lines 259 to 259OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -245,3 +252,8 @@245252 Effect.catchCause((cause) => fatalStartupCause("whenReady", cause)),246253 );247254 yield* logStartupInfo("app ready");255+ const cuaEnabled = Option.match(yield* clientSettings.get, {256+ onNone: () => false,257+ onSome: (settings) => settings.enableCua,258+ });259+ if (cuaEnabled) {
- apps/server/src/serverRuntimeStartup.ts line 418OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -367,3 +368,14 @@367368 );368369369370 const startup = Effect.gen(function* () {371+ yield* runStartupPhase(372+ "cua-driver.start",373+ startEmbeddedCuaDriver().pipe(374+ Effect.catch((error) =>375+ Effect.logWarning("embedded cua-driver failed to start; computer use is unavailable", {376+ binaryPath: error.binaryPath,377+ cause: String(error.cause),378+ }),379+ ),380+ ),381+ );
- apps/server/src/cua/CuaDriverEmbedded.ts line 80OutdatedResolvedby injaneity(GitHub profile, opens in a new tab)@@ -0,0 +82,21 @@82+ onUnexpectedExit: (exit: EmbeddedDriverExit) => Effect.Effect<void>,83+ ) {84+ const exit = yield* Effect.tryPromise(waitForExit);85+ yield* onUnexpectedExit(exit);86+ },87+ Effect.catch((cause) => Effect.logWarning("embedded cua-driver exit monitor failed", { cause })),88+);89+90+const restartServerAfterCuaDriverExit = Effect.fn("server.restartAfterCuaDriverExit")(function* (91+ exit: EmbeddedDriverExit,92+) {93+ yield* Effect.logError("embedded cua-driver exited unexpectedly; restarting server", {94+ component: "embedded-cua-driver",95+ generation: exit.generation,96+ code: exit.code,97+ success: exit.success,98+ });99+ yield* Effect.sync(() => {100+ process.exitCode = 1;101+ process.kill(process.pid, "SIGTERM");102+ });
- apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts line 1225OutdatedResolvedby injaneity(GitHub profile, opens in a new tab)@@ -1208,3 +1213,13 @@12081213 );12091214 }121012151216+const withLiveCodexIntegrationEnvironment = (1217+ environment: NodeJS.ProcessEnv,1218+): NodeJS.ProcessEnv => ({1219+ ...environment,1220+ ...(process.env[T3CODE_CODEX_APPEND_LAUNCH_ARGS_ENV] === undefined1221+ ? {}1222+ : {1223+ [T3CODE_CODEX_APPEND_LAUNCH_ARGS_ENV]: process.env[T3CODE_CODEX_APPEND_LAUNCH_ARGS_ENV],1224+ }),1225+});
- packages/contracts/src/settings.ts line 79Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -76,3 +76,4 @@7676 glassOpacity: GlassOpacity.pipe(7777 Schema.withDecodingDefault(Effect.succeed(DEFAULT_GLASS_OPACITY)),7878 ),79+ enableCua: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),apps/web/src/components/settings/SettingsPanels.tsx line 538OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -528,3 +532,7 @@528532529533 export function AppearanceSettingsPanel() {530534 const { theme, setTheme } = useTheme();535+ const supportsCua =536+ typeof window !== "undefined" &&537+ Boolean(window.desktopBridge) &&538+ isMacPlatform(window.navigator.platform);
- scripts/build-desktop-artifact.ts line 1658Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1564,3 +1616,6 @@15641616 }1565161715661618 if (platform === "mac") {1619+ buildConfig.extraResources = [1620+ { from: "apps/desktop/prod-resources/cua-driver", to: "cua-driver" },1621+ ];apps/desktop/src/cua/CuaDriverServerEnvironment.ts line 110Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +79,21 @@79+80+ const updates: Record<string, string> = {81+ [T3CODE_CUA_DRIVER_PATH_ENV]: driverPath.value,82+ [T3CODE_CUA_DRIVER_HOST_BUNDLE_ID_ENV]: hostBundleId,83+ };84+ if (resourcesPath !== undefined) {85+ const moduleUrl = yield* path86+ .toFileUrl(87+ path.join(88+ resourcesPath,89+ "app.asar.unpacked/node_modules/@trycua/cua-driver/dist/embedded.js",90+ ),91+ )92+ .pipe(93+ Effect.map((url) => url.href),94+ Effect.mapError(95+ (cause) => new CuaDriverConfigurationError({ binaryPath: driverPath.value, cause }),96+ ),97+ );98+ updates[T3CODE_CUA_DRIVER_MODULE_URL_ENV] = moduleUrl;99+ }
- apps/desktop/src/cua/CuaDriverServerEnvironment.ts lines 77 to 77OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +57,21 @@57+/** Prevents inherited development variables from bypassing the desktop opt-in. */58+export const disableCuaDriverServerEnvironment = Effect.fn("disableCuaDriverServerEnvironment")(59+ function* () {60+ yield* replaceCuaDriverServerEnvironment({});61+ },62+);63+64+/**65+ * Configures the local Node server to own cua-driver. The backend inherits66+ * these values when Electron starts it; remote servers can set the same67+ * variables in their own environment.68+ */69+export const configureCuaDriverServerEnvironment = Effect.fn("configureCuaDriverServerEnvironment")(70+ function* (hostBundleId: string, resourcesPath?: string) {71+ const path = yield* Path.Path;72+ const driverPath = resolveEmbeddedDriverPath(73+ process.env,74+ resourcesPath === undefined ? undefined : path.join(resourcesPath, "cua-driver"),75+ );76+ if (Option.isNone(driverPath)) {77+ return yield* new CuaDriverConfigurationError({ binaryPath: "<not configured>" });
injaneity(GitHub profile, opens in a new tab) changed the title
- apps/desktop/src/cua/CuaDriverServerEnvironment.ts lines 14 to 14OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,14 @@1+import * as Effect from "effect/Effect";2+import * as Option from "effect/Option";3+import * as Path from "effect/Path";4+import * as Schema from "effect/Schema";5+6+export const T3CODE_CUA_DRIVER_PATH_ENV = "T3CODE_CUA_DRIVER_PATH";7+export const T3CODE_CUA_DRIVER_HOST_BUNDLE_ID_ENV = "T3CODE_CUA_DRIVER_HOST_BUNDLE_ID";8+export const T3CODE_CUA_DRIVER_MODULE_URL_ENV = "T3CODE_CUA_DRIVER_MODULE_URL";9+10+export class CuaDriverConfigurationError extends Schema.TaggedErrorClass<CuaDriverConfigurationError>()(11+ "CuaDriverConfigurationError",12+ {13+ binaryPath: Schema.String,14+ cause: Schema.optional(Schema.Defect()),
- apps/desktop/src/cua/CuaDriverServerEnvironment.ts lines 100 to 108OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +88,21 @@88+ "cua-driver",89+ platform === "win32" ? "cua-driver.exe" : "cua-driver",90+ );91+ const driverPath = resolveEmbeddedDriverPath(process.env, packagedDriverPath);92+ if (Option.isNone(driverPath)) {93+ return yield* new CuaDriverNotConfiguredError();94+ }95+96+ const updates: Record<string, string> = {97+ [T3CODE_CUA_DRIVER_PATH_ENV]: driverPath.value,98+ [T3CODE_CUA_DRIVER_HOST_BUNDLE_ID_ENV]: hostBundleId,99+ };100+ if (resourcesPath !== undefined) {101+ const moduleUrl = yield* path102+ .toFileUrl(103+ path.join(resourcesPath, "app.asar/node_modules/@trycua/cua-driver/dist/embedded.js"),104+ )105+ .pipe(106+ Effect.map((url) => url.href),107+ Effect.mapError(108+ (cause) => new CuaDriverConfigurationError({ binaryPath: driverPath.value, cause }),apps/desktop/src/app/DesktopApp.ts lines 265 to 274OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -247 +254,21 @@247254 yield* logStartupInfo("app ready");255+ const cuaEnabled = Option.match(yield* clientSettings.get, {256+ onNone: () => false,257+ onSome: (settings) => settings.enableCua,258+ });259+ if (cuaEnabled) {260+ yield* configureCuaDriverServerEnvironment(261+ environment.appUserModelId,262+ environment.platform,263+ environment.isPackaged ? environment.resourcesPath : undefined,264+ ).pipe(265+ Effect.catch((error) =>266+ Effect.gen(function* () {267+ yield* disableCuaDriverServerEnvironment();268+ const context =269+ error._tag === "CuaDriverConfigurationError"270+ ? {271+ binaryPath: error.binaryPath,272+ cause: String(error.cause),273+ }274+ : {};
- apps/server/src/cua/CuaDriverEmbedded.ts lines 86 to 88OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +68,21 @@68+ });69+ yield* Effect.addFinalizer(() => deactivate.pipe(Effect.asVoid));70+ process.env[T3CODE_CODEX_APPEND_LAUNCH_ARGS_ENV] = [previous?.trim() ?? "", launchArgs]71+ .filter((value) => value.length > 0)72+ .join(" ");73+ return () => deactivate;74+});75+76+/**77+ * Starts a server-private driver when T3CODE_CUA_DRIVER_PATH is configured.78+ * The scoped finalizer owns both the native host and its child process.79+ */80+export const startEmbeddedCuaDriver = Effect.fn("server.startEmbeddedCuaDriver")(function* () {81+ const binaryPath = process.env[T3CODE_CUA_DRIVER_PATH_ENV]?.trim();82+ if (!binaryPath) return;83+84+ const hostBundleId = process.env[T3CODE_CUA_DRIVER_HOST_BUNDLE_ID_ENV]?.trim() || "t3-server";85+ const moduleUrl = process.env[T3CODE_CUA_DRIVER_MODULE_URL_ENV]?.trim() || undefined;86+ const { EmbeddedCuaDriverHost } = yield* Effect.tryPromise({87+ try: () => importEmbeddedCuaDriver(moduleUrl),88+ catch: (cause) => new CuaDriverStartError({ binaryPath, cause }),
- scripts/build-desktop-artifact.ts line 203Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -134,0 +185,21 @@185+export function resolveCuaDriverAsset(186+ platform: typeof BuildPlatform.Type,187+ arch: typeof BuildArch.Type,188+) {189+ if (platform === "mac") return resolveCuaDriverMacAsset(arch);190+ if (!EXACT_RELEASE_VERSION_PATTERN.test(CUA_DRIVER_RELEASE_VERSION) || arch === "universal") {191+ throw new Error(`Cua Driver has no release asset for ${platform}/${arch}.`);192+ }193+194+ const os = platform === "win" ? "windows" : "linux";195+ const releaseArch = arch === "x64" ? "x86_64" : "arm64";196+ const extension = platform === "win" ? "zip" : "tar.gz";197+ const archiveName = `cua-driver-rs-${CUA_DRIVER_RELEASE_VERSION}-${os}-${releaseArch}-binary.${extension}`;198+ return {199+ archiveName,200+ executablePath: platform === "win" ? "cua-driver.exe" : "cua-driver",201+ companionPaths:202+ platform === "win" ? ["cua-cursor-theme.exe"] : ["cua-cursor-theme", "wayland-helper"],203+ sha256: CUA_DRIVER_PLATFORM_SHA256[platform][arch],204+ url: `${CUA_DRIVER_RELEASE_BASE_URL}/${archiveName}`,205+ };
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
f658ed38-d3d5-4952-86ae-9c55d232fc24You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share