diff --git a/packages/orchestrator/src/config.ts b/packages/orchestrator/src/config.ts index d39131a1..8bacdb73 100644 --- a/packages/orchestrator/src/config.ts +++ b/packages/orchestrator/src/config.ts @@ -1,9 +1,14 @@ +import { existsSync, readFileSync } from "node:fs"; import { homedir } from "node:os"; -import { join } from "node:path"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; const CONFIG_DIR_NAME = ".pi"; const ENV_ORCHESTRATOR_DIR = "PI_ORCHESTRATOR_DIR"; +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + /** * 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) @@ -11,6 +16,32 @@ const ENV_ORCHESTRATOR_DIR = "PI_ORCHESTRATOR_DIR"; export const isBunBinary = import.meta.url.includes("$bunfs") || import.meta.url.includes("~BUN") || import.meta.url.includes("%7EBUN"); +interface PackageJson { + version?: string; +} + +function getPackageJsonPath(): string { + let dir = __dirname; + while (dir !== dirname(dir)) { + const packageJsonPath = join(dir, "package.json"); + if (existsSync(packageJsonPath)) { + return packageJsonPath; + } + dir = dirname(dir); + } + return join(__dirname, "package.json"); +} + +let pkg: PackageJson = {}; +try { + pkg = JSON.parse(readFileSync(getPackageJsonPath(), "utf-8")) as PackageJson; +} catch (e: unknown) { + const err = e as NodeJS.ErrnoException; + if (err.code !== "ENOENT") throw e; +} + +export const VERSION: string = pkg.version || "0.0.0"; + export function getOrchestratorDir(): string { const envDir = process.env[ENV_ORCHESTRATOR_DIR]; if (envDir) { diff --git a/packages/orchestrator/src/radius.ts b/packages/orchestrator/src/radius.ts index aacd7130..2d6347f9 100644 --- a/packages/orchestrator/src/radius.ts +++ b/packages/orchestrator/src/radius.ts @@ -1,12 +1,11 @@ import { hostname, platform } from "node:os"; import { AuthStorage, type OAuthCredential } from "@earendil-works/pi-coding-agent"; -import { getOrchestratorDir, getSocketPath } from "./config.ts"; +import { getOrchestratorDir, getSocketPath, VERSION } from "./config.ts"; import { loadMachine, saveMachine } from "./storage.ts"; import type { InstanceRecord, MachineRecord, RadiusRegistration } from "./types.ts"; const DEFAULT_RADIUS_URL = "https://radius.pi.dev/"; const DEFAULT_ORCHESTRATOR_BASE_PATH = "/v1/"; -const ORCHESTRATOR_VERSION = "0.79.6"; const NOT_FOUND_RETRY_THRESHOLD = 3; const HEARTBEAT_BACKOFF_BASE_MS = 1_000; const HEARTBEAT_BACKOFF_MAX_MS = 30_000; @@ -243,7 +242,7 @@ export class RadiusPresence { hostname: hostname(), platform: platform(), arch: process.arch, - version: ORCHESTRATOR_VERSION, + version: VERSION, capabilities: { spawn: true, relay: false, iroh: false }, });