UNPKG

@code-yeongyu/senpi

Version:

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

138 lines • 5.16 kB
import { randomUUID } from "node:crypto"; import { attachJsonlLineReader, MAX_RPC_LINE_CHARACTERS, serializeJsonLine } from "../rpc/jsonl.js"; import { connectRpcClientSocket } from "../rpc/rpc-client-socket.js"; import { requestDeadlineMs } from "../rpc/rpc-request-deadline.js"; /** The endpoint refused an attachment the presentation asked for; `code` is machine-readable. */ export class AttachRefusedError extends Error { constructor(code, message) { super(message); this.name = "AttachRefusedError"; this.code = code; } } /** The endpoint answered a dialog response with a refusal; `code` is the endpoint's error. */ export class AnswerRefusedError extends Error { constructor(code) { super(`Answer refused: ${code}`); this.name = "AnswerRefusedError"; this.code = code; } } const ANSWER_DEADLINE_MS = 30_000; /** A presentation-only socket client: it cannot spawn or fall back to an in-process runtime. */ export class AttachClient { constructor(onRecord, onDisconnect, options = {}) { this.pending = new Map(); this.closed = false; this.onRecord = onRecord; this.onDisconnect = onDisconnect; this.answerDeadlineMs = options.answerDeadlineMs ?? ANSWER_DEADLINE_MS; } async connect(path) { const socket = await connectRpcClientSocket(path, {}); if (this.closed) { socket.destroy(); throw new Error("Disconnected"); } this.socket = socket; this.stopReading = attachJsonlLineReader(socket, (line) => this.receive(socket, line), { maxLineLength: MAX_RPC_LINE_CHARACTERS, onOversizedLine: () => this.fail(socket, "the endpoint sent a record over the line limit"), }); socket.once("error", () => socket.destroy()); socket.once("close", () => { this.socket = undefined; this.rejectPending(); if (!this.closed) this.onDisconnect(this.failure); }); } request(command) { const id = randomUUID(); return this.track(id, requestDeadlineMs(command), `No acknowledgement for ${command.type}; its outcome is unknown`, { ...command, id, }); } /** Sends a dialog answer and settles when the endpoint accepts it; a refusal rejects with its code. */ async answer(sessionId, response) { await this.track(response.id, this.answerDeadlineMs, "No acknowledgement for the answer", { ...response, sessionId, answerId: randomUUID(), }); } close() { this.closed = true; this.stopReading?.(); this.socket?.destroy(); this.socket = undefined; this.rejectPending(); } track(id, deadlineMs, unanswered, record) { return new Promise((resolve, reject) => { const timer = setTimeout(() => { this.pending.delete(id); reject(new Error(unanswered)); }, deadlineMs); this.pending.set(id, { resolve: (answer) => { clearTimeout(timer); resolve(answer); }, reject: (error) => { clearTimeout(timer); reject(error); }, }); if (this.socket?.writable && !this.socket.destroyed) { this.socket.write(serializeJsonLine(record)); return; } clearTimeout(timer); this.pending.delete(id); reject(new Error("Disconnected")); }); } /** One endpoint line. A record it cannot read or render ends the connection; it never throws to the socket. */ receive(socket, line) { let record; try { record = JSON.parse(line); } catch { this.fail(socket, "the endpoint sent an unreadable record"); return; } if (record.type === "response") { const pending = record.id ? this.pending.get(record.id) : undefined; if (!pending) return; if (record.id) this.pending.delete(record.id); if (record.success) pending.resolve(record); else if (record.command === "extension_ui_response") pending.reject(new AnswerRefusedError(record.error)); else pending.reject(new Error(record.error)); return; } try { this.onRecord(record); } catch (error) { this.fail(socket, `a ${record.type} record could not be shown (${error instanceof Error ? error.message : String(error)})`); } } fail(socket, reason) { this.failure ??= reason; socket.destroy(); } rejectPending() { for (const request of this.pending.values()) request.reject(new Error("Disconnected; pending command outcome is unknown")); this.pending.clear(); } } //# sourceMappingURL=attach-client.js.map