UNPKG

@code-yeongyu/senpi

Version:

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

76 lines • 3.42 kB
import { chmodSync, mkdtempSync, readFileSync, renameSync, rmSync, watch, writeFileSync, } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { getAgentDir } from "../../../../config.js"; import { createSessionLogger } from "../../../session-log.js"; function shSingleQuoted(value) { return `'${value.replaceAll("'", `'\\''`)}'`; } function helperScript(mode, sinkPath, platform) { if (platform === "win32") { // Delayed expansion keeps `&` and `?` in the URL literal; `echo(` prints even an empty argument. The CLI starts // the helper in its own cwd (the account dir) and does not wait for it; Windows cannot delete a directory that a // live process has as its cwd, so the helper leaves it before setlocal. The implicit endlocal restores %SystemRoot%, // and the helper never holds the account dir after senpi sees the URL (senpi#4654). return mode === "discard" ? "@exit /b 0\r\n" : `@echo off\r\ncd /d "%SystemRoot%"\r\nsetlocal EnableDelayedExpansion\r\nset "u=%~1"\r\n>>"${sinkPath}" echo(!u!\r\n`; } return mode === "discard" ? "#!/bin/sh\nexit 0\n" : `#!/bin/sh\nprintf '%s\\n' "$1" >> ${shSingleQuoted(sinkPath)}\n`; } export function createClaudeCliBrowserCapture(mode, onUrl, platform = process.platform) { const logger = createSessionLogger(getAgentDir()); const dir = mkdtempSync(join(tmpdir(), "senpi-claude-browser-")); const sinkPath = join(dir, "urls"); const helperPath = join(dir, platform === "win32" ? "browser.cmd" : "browser"); writeFileSync(helperPath, helperScript(mode, sinkPath, platform), { mode: 0o700 }); chmodSync(helperPath, 0o700); let consumed = 0; const flush = () => { let text; try { text = readFileSync(sinkPath, "utf8"); } catch { return; // nothing written yet } const complete = text.slice(0, text.lastIndexOf("\n") + 1); const lines = complete.split(/\r?\n/).filter((line) => line.trim().length > 0); for (const line of lines.slice(consumed)) onUrl(line.trim()); consumed = lines.length; }; let watcher; if (mode === "capture") { watcher = watch(dir, flush); watcher.on("error", () => watcher?.close()); } return { helperPath, flush, dispose: () => { watcher?.close(); let cleanupDir = dir; try { // A browser helper can outlive the CLI even after its stdio closes. Revoke its // absolute sink path before deletion: late opens fail, already-open files unlink safely. const disposedDir = `${dir}-disposed`; renameSync(dir, disposedDir); cleanupDir = disposedDir; } catch { logger.debug("claude_cli_browser_capture_rename_failed", { source: dir }); } try { rmSync(cleanupDir, { recursive: true, force: true, maxRetries: 3 }); } catch { // Private scratch cleanup must never prevent the login outcome from settling. logger.debug("claude_cli_browser_capture_cleanup_failed", { source: cleanupDir }); } }, }; } //# sourceMappingURL=claude-cli-browser-capture.js.map