UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

46 lines • 2.36 kB
import { spawn } from "node:child_process"; import { access } from "node:fs/promises"; import { dirname, extname, join } from "node:path"; import { isBunBinary } from "../../../config.js"; import { runtimeExecArgv } from "../../../utils/runtime-exec-argv.js"; import { resolveCliMainPathAsync } from "../host-cli-entry.js"; import { TOOL_CHILD_ROLE_ENV } from "./channel.js"; import { TOOL_CHILD_GENERATION_ENV } from "./entry.js"; import { TOOL_CHILD_LEDGER_ENV, TOOL_CHILD_REENTRY_ENV, TOOL_CHILD_REFS_ROOT_ENV } from "./launch-env-names.js"; /** * How this engine re-enters itself as a tool child: a compiled binary boots its embedded entry with the role in its * environment, an interpreted install runs its own CLI entry under the same runtime. Never a PATH lookup. */ export async function toolChildCommand(compiled = isBunBinary) { if (compiled) return { command: process.execPath, args: [] }; return { command: process.execPath, args: [...runtimeExecArgv(), await toolChildEntryPath()] }; } /** The dedicated role entry beside the CLI entry, so an interpreted child parses none of the CLI graph. */ export async function toolChildEntryPath(cliMainPath) { const entry = cliMainPath ?? (await resolveCliMainPathAsync()); const dedicated = join(dirname(entry), `tool-child-main${extname(entry)}`); return (await access(dedicated).then(() => true, () => false)) ? dedicated : entry; } export function spawnToolChild(options) { return spawn(options.command.command, [...options.command.args], { cwd: options.cwd, env: { ...options.env, ...options.channelEnv, [TOOL_CHILD_ROLE_ENV]: "tool-child", [TOOL_CHILD_GENERATION_ENV]: String(options.generation), [TOOL_CHILD_REFS_ROOT_ENV]: options.refsRoot, // How to re-enter this engine: the Windows launch shim is started the same way the child was. [TOOL_CHILD_REENTRY_ENV]: JSON.stringify(options.command), [TOOL_CHILD_LEDGER_ENV]: options.ledgerPath, }, // POSIX: the child leads its own group, so its whole tree can be signalled as one. Windows: the job is the handle. detached: process.platform !== "win32", stdio: ["ignore", "pipe", "pipe"], windowsHide: true, }); } //# sourceMappingURL=launch.js.map