Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AgentDeviceShim.ts

63 lines3.0 KB

1// @effect-diagnostics preferSchemaOverJson:off - JSON string literals embed paths safely into generated JavaScript.
2/**
3 * A directory holding an `agent-device` launcher that runs the pinned install
4 * with a Node runtime. Prepended to provider subprocess PATHs so the agent
5 * types `agent-device …` and gets the version the injected instructions were
6 * written for, regardless of what is or is not globally installed.
7 */
8import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
9import { resolveNodeExecutable } from "@t3tools/shared/nodeRuntime";
10import * as Effect from "effect/Effect";
11import * as FileSystem from "effect/FileSystem";
12import * as Path from "effect/Path";
13​
14const SHIM_DIR = "device/bin";
15​
16export const ensureAgentDeviceShim = Effect.fn("AgentDeviceShim.ensure")(function* (input: {
17 readonly entryPath: string;
18 readonly stateDir: string;
19}) {
20 const { entryPath } = input;
21 const fs = yield* FileSystem.FileSystem;
22 const path = yield* Path.Path;
23 const platform = yield* HostProcessPlatform;
24 const node = yield* resolveNodeExecutable("Device automation");
25 const shimDir = path.join(input.stateDir, SHIM_DIR);
26 yield* fs.makeDirectory(shimDir, { recursive: true });
27 const launcherPath = path.join(shimDir, "agent-device-launcher.mjs");
28 // Desktop resolveNodeExecutable returns the Electron binary. Without
29 // ELECTRON_RUN_AS_NODE the shim starts a second T3 GUI/backend, which
30 // reconciles provider sessions and kills OpenCode/Cursor threads.
31 yield* fs.writeFileString(
32 launcherPath,
33 `import { spawn } from "node:child_process";
34const args = process.argv.slice(2);
35const informational = args.length === 1 && ["help", "--help", "-h", "--version", "version"].includes(args[0]);
36const hasValue = flag => { const index = args.indexOf(flag); return index >= 0 && !!args[index + 1] && !args[index + 1].startsWith("--"); };
37if (!informational && !(hasValue("--config") && hasValue("--session"))) {
38 console.error("Call device_open first and include its --config and --session flags.");
39 process.exit(1);
41const env = { ...process.env, ELECTRON_RUN_AS_NODE: "1" };
42delete env.AGENT_DEVICE_DAEMON_BASE_URL;
43delete env.AGENT_DEVICE_DAEMON_AUTH_TOKEN;
44delete env.AGENT_DEVICE_CONFIG;
45const child = spawn(${JSON.stringify(node)}, [${JSON.stringify(entryPath)}, ...args], { stdio: "inherit", env });
46child.on("error", error => { console.error(error.message); process.exitCode = 1; });
47child.on("exit", code => { process.exitCode = code ?? 1; });
48`,
49 );
50 if (platform === "win32") {
51 const script = `@echo off\r\nset ELECTRON_RUN_AS_NODE=1\r\n"${node}" "${launcherPath}" %*\r\n`;
52 yield* fs.writeFileString(path.join(shimDir, "agent-device.cmd"), script);
53 } else {
54 const command = [node, launcherPath]
55 .map((value) => "'" + value.replaceAll("'", "'\"'\"'") + "'")
56 .join(" ");
57 const script = `#!/bin/sh\nexport ELECTRON_RUN_AS_NODE=1\nexec ${command} "$@"\n`;
58 const shimPath = path.join(shimDir, "agent-device");
59 yield* fs.writeFileString(shimPath, script);
60 yield* fs.chmod(shimPath, 0o755);
61 }
62 return shimDir;
63});