UNPKG

@code-yeongyu/senpi

Version:

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

114 lines • 5.74 kB
import { constants } from "node:fs"; import { access as fsAccess } from "node:fs/promises"; import { constants as osConstants } from "node:os"; import { annotateShellTrust } from "../../engine-trust/shell-trust.js"; import { getShellConfig, getShellEnv, noteDetachedChildExited, pruneTrackedDetachedChildren, trackDetachedChildPid, } from "../../utils/shell.js"; import { attestSessionSpawn } from "../session-spawn-attestation.js"; import { createShellToolCore } from "./bash-core.js"; import { bashToolConfig, } from "./bash-schema.js"; import { defaultToolMachine } from "./machine.js"; import { createTempFileSpill } from "./output-accumulator.js"; import { createShellRenderers } from "./renderers/bash.js"; import { createShellOperations } from "./shell-operations.js"; import { wrapToolDefinition } from "./tool-definition-wrapper.js"; export { bashToolSystemPromptContribution, } from "./bash-schema.js"; /** * Shared process execution used by the built-in shell tools: one `process.exec` of the operation layer (own * process group, scope token, abort/timeout kill the group). The tool keeps its own output handling. */ export function createLocalShellOperations(shellName, resolveShellConfig, machine = defaultToolMachine()) { return createShellOperations(shellName, machine, { resolveShell: () => { const shellConfig = resolveShellConfig(); return { path: shellConfig.shell, args: shellConfig.args, commandTransport: shellConfig.commandTransport === "stdin" ? "stdin" : "argv", }; }, checkCwd: (cwd) => fsAccess(cwd, constants.F_OK), envMode: "exact", defaultEnv: getShellEnv, signalNumber: (signal) => osConstants.signals[signal], // Tracked as a process group: whatever the shell backgrounds keeps running in it, and ownership // is released only once that group is empty (senpi#1697). onSpawn: trackDetachedChildPid, onExit: noteDetachedChildExited, attestSessionSpawn, // Groups kept past their shell's exit must not accumulate across a session. afterExec: pruneTrackedDetachedChildren, }); } /** * Create bash operations using pi's built-in local shell execution backend. * * This is useful for extensions that intercept user_bash and still want pi's * standard local shell behavior while wrapping or rewriting commands. */ export function createLocalBashOperations(options) { return createLocalShellOperations("bash", () => getShellConfig(options?.shellPath), options?.machine); } /** A tool routed to the session's device by OMO_TOOLS_VIA_DAEMON found no device to run on; it never falls back to local. */ export class DeviceUnavailableError extends Error { constructor(tool) { super(`${tool}: the session's device is unavailable, and OMO_TOOLS_VIA_DAEMON routes ${tool} there, so it does not run locally`); this.code = "device_unavailable"; this.name = "DeviceUnavailableError"; this.tool = tool; } } const toolsViaDaemon = (tool, env = process.env) => (env.OMO_TOOLS_VIA_DAEMON ?? "") .split(",") .map((name) => name.trim()) .includes(tool); export function createShellToolDefinition(cwd, config, options) { const machine = options?.machine ?? defaultToolMachine(); // OMO_TOOLS_VIA_DAEMON=bash (default off): run on the session's device, never on this machine. The flag decides even // when the session injects its dispatcher's operations (bootstrap.ts always does), so the promise holds in a real session. // Only the bash tool: powershell shares this definition with its own local operations and stays on them. const viaDevice = config.name === "bash" && toolsViaDaemon("bash"); const deviceMachine = options?.deviceMachine; // Resolved when the command runs, not when the tool is built: an extension may register the device later. const resolveDevice = typeof deviceMachine === "function" ? deviceMachine : () => deviceMachine; const ops = viaDevice ? { exec: (command, execCwd, execOptions) => { const device = resolveDevice(); if (device === undefined) throw new DeviceUnavailableError(config.name); return createLocalBashOperations({ shellPath: options?.shellPath, machine: device }).exec(command, execCwd, execOptions); }, } : (options?.operations ?? createLocalBashOperations({ shellPath: options?.shellPath, machine })); return { ...(options?.operations !== undefined || options?.spawnHook !== undefined ? { serverPinned: true } : {}), ...createShellToolCore(cwd, config, { machine, operations: ops, outputSpill: createTempFileSpill, shellEnv: getShellEnv, annotateTrust: annotateShellTrust, redactSecretsInOutput: options?.redactSecretsInOutput, commandPrefix: options?.commandPrefix, exposeSessionEnvironment: options?.exposeSessionEnvironment, spawnHook: options?.spawnHook, }), ...createShellRenderers(config.prompt), }; } export function createBashToolDefinition(cwd, options) { return { ...createShellToolDefinition(cwd, bashToolConfig, options), exposure: "eval", }; } export function createBashTool(cwd, options) { const definition = createBashToolDefinition(cwd, options); const tool = wrapToolDefinition(definition); Object.assign(tool, { promptSnippet: definition.promptSnippet, promptGuidelines: definition.promptGuidelines, }); return tool; } //# sourceMappingURL=bash.js.map