From 122527b22a957056b26645014f2f205394b2ca11 Mon Sep 17 00:00:00 2001 From: Cristina Poncela Cubeiro <140309543+cristinaponcela@users.noreply.github.com> Date: Fri, 26 Jun 2026 10:01:16 +0200 Subject: [PATCH] fix: rpc-entry and bun support --- packages/coding-agent/package.json | 5 ++++- packages/coding-agent/src/rpc-entry.ts | 12 +++++++++++ packages/orchestrator/package.json | 3 --- packages/orchestrator/src/config.ts | 7 +++++++ packages/orchestrator/src/rpc-process.ts | 26 +++++++++++++++--------- 5 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 packages/coding-agent/src/rpc-entry.ts diff --git a/packages/coding-agent/package.json b/packages/coding-agent/package.json index 8175de4f..c75ccb3a 100644 --- a/packages/coding-agent/package.json +++ b/packages/coding-agent/package.json @@ -15,6 +15,9 @@ ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" + }, + "./rpc-entry": { + "import": "./dist/rpc-entry.js" } }, "files": [ @@ -27,7 +30,7 @@ ], "scripts": { "clean": "shx rm -rf dist", - "build": "tsgo -p tsconfig.build.json && shx chmod +x dist/cli.js && npm run copy-assets", + "build": "tsgo -p tsconfig.build.json && shx chmod +x dist/cli.js dist/rpc-entry.js && npm run copy-assets", "build:binary": "npm --prefix ../tui run build && npm --prefix ../ai run build && npm --prefix ../agent run build && npm run build && bun build --compile ./dist/bun/cli.js ./src/utils/image-resize-worker.ts --outfile dist/pi && npm run copy-binary-assets", "copy-assets": "shx mkdir -p dist/modes/interactive/theme && shx cp src/modes/interactive/theme/*.json dist/modes/interactive/theme/ && shx mkdir -p dist/modes/interactive/assets && shx cp src/modes/interactive/assets/*.png dist/modes/interactive/assets/ && shx mkdir -p dist/core/export-html/vendor && shx cp src/core/export-html/template.html src/core/export-html/template.css src/core/export-html/template.js dist/core/export-html/ && shx cp src/core/export-html/vendor/*.js dist/core/export-html/vendor/", "copy-binary-assets": "shx cp package.json dist/ && shx cp README.md dist/ && shx cp CHANGELOG.md dist/ && shx mkdir -p dist/theme && shx cp src/modes/interactive/theme/*.json dist/theme/ && shx mkdir -p dist/assets && shx cp src/modes/interactive/assets/*.png dist/assets/ && shx mkdir -p dist/export-html/vendor && shx cp src/core/export-html/template.html dist/export-html/ && shx cp src/core/export-html/vendor/*.js dist/export-html/vendor/ && shx cp -r docs dist/ && shx cp -r examples dist/ && shx cp ../../node_modules/@silvia-odwyer/photon-node/photon_rs_bg.wasm dist/", diff --git a/packages/coding-agent/src/rpc-entry.ts b/packages/coding-agent/src/rpc-entry.ts new file mode 100644 index 00000000..a07f9b33 --- /dev/null +++ b/packages/coding-agent/src/rpc-entry.ts @@ -0,0 +1,12 @@ +#!/usr/bin/env node +import { APP_NAME } from "./config.ts"; +import { configureHttpDispatcher } from "./core/http-dispatcher.ts"; +import { main } from "./main.ts"; + +process.title = `${APP_NAME}-rpc`; +process.env.PI_CODING_AGENT = "true"; +process.emitWarning = (() => {}) as typeof process.emitWarning; + +configureHttpDispatcher(); + +main(["--mode", "rpc", ...process.argv.slice(2)]); diff --git a/packages/orchestrator/package.json b/packages/orchestrator/package.json index 297bf452..b93e0800 100644 --- a/packages/orchestrator/package.json +++ b/packages/orchestrator/package.json @@ -3,9 +3,6 @@ "version": "0.80.2", "description": "experimental orchestrator package for pi", "type": "module", - "bin": { - "orchestrator": "dist/cli.js" - }, "main": "./dist/index.js", "types": "./dist/index.d.ts", "exports": { diff --git a/packages/orchestrator/src/config.ts b/packages/orchestrator/src/config.ts index fab11bed..d39131a1 100644 --- a/packages/orchestrator/src/config.ts +++ b/packages/orchestrator/src/config.ts @@ -4,6 +4,13 @@ import { join } from "node:path"; const CONFIG_DIR_NAME = ".pi"; const ENV_ORCHESTRATOR_DIR = "PI_ORCHESTRATOR_DIR"; +/** + * Detect if we're running as a Bun compiled binary. + * Bun binaries have import.meta.url containing "$bunfs", "~BUN", or "%7EBUN" (Bun's virtual filesystem path) + */ +export const isBunBinary = + import.meta.url.includes("$bunfs") || import.meta.url.includes("~BUN") || import.meta.url.includes("%7EBUN"); + export function getOrchestratorDir(): string { const envDir = process.env[ENV_ORCHESTRATOR_DIR]; if (envDir) { diff --git a/packages/orchestrator/src/rpc-process.ts b/packages/orchestrator/src/rpc-process.ts index 4c37d734..3983a03c 100644 --- a/packages/orchestrator/src/rpc-process.ts +++ b/packages/orchestrator/src/rpc-process.ts @@ -1,8 +1,7 @@ import { type ChildProcess, spawn } from "node:child_process"; import { randomUUID } from "node:crypto"; -import { existsSync } from "node:fs"; +import { createRequire } from "node:module"; import { dirname, join } from "node:path"; -import { fileURLToPath } from "node:url"; import type { AgentSessionEvent, RpcCommand, @@ -10,6 +9,7 @@ import type { RpcExtensionUIResponse, RpcResponse, } from "@earendil-works/pi-coding-agent"; +import { isBunBinary } from "./config.ts"; interface PendingRequest { resolve(response: RpcResponse): void; @@ -26,13 +26,19 @@ export interface RpcProcessInstance { dispose(): Promise; } -function resolveCodingAgentCli(): string { - const packageDir = dirname(fileURLToPath(import.meta.url)); - const workspaceCli = join(packageDir, "../../coding-agent/dist/cli.js"); - if (existsSync(workspaceCli)) { - return workspaceCli; +const require = createRequire(import.meta.url); + +function getRpcSpawnCommand(): { command: string; args: string[] } { + if (isBunBinary) { + return { + command: join(dirname(process.execPath), process.platform === "win32" ? "pi.exe" : "pi"), + args: ["--mode", "rpc"], + }; } - throw new Error(`Unable to find coding-agent RPC CLI: ${workspaceCli}`); + return { + command: process.execPath, + args: [require.resolve("@earendil-works/pi-coding-agent/rpc-entry")], + }; } function toError(error: unknown): Error { @@ -40,8 +46,8 @@ function toError(error: unknown): Error { } export function createRpcProcessInstance(options: { cwd: string }): RpcProcessInstance { - const cliPath = resolveCodingAgentCli(); - const child = spawn(process.execPath, [cliPath, "--mode", "rpc"], { + const rpcCommand = getRpcSpawnCommand(); + const child = spawn(rpcCommand.command, rpcCommand.args, { cwd: options.cwd, env: process.env, stdio: ["pipe", "pipe", "pipe"],