UNPKG

@code-yeongyu/senpi

Version:

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

71 lines • 3.77 kB
import { writtenDeliveryIds } from "../../core/external-admission.js"; import { admitInputChain } from "./chain-admission.js"; import { RPC_ERROR_SESSION_CLOSING } from "./rpc-error-types.js"; const SENDER_NOT_ALLOWED = "sender_not_allowed"; const DELEGATED_ORIGIN_UNKNOWN = "delegated_origin_unknown"; const CANONICAL_TURN_ID = /^(0|[1-9]\d{0,15})$/; /** The ledger key: a remote device's ids live under that device, a local connection's under its own prefix. */ function ledgerId(deliveryId, origin) { return origin === undefined ? `rpc:local/${deliveryId}` : `device:${encodeURIComponent(origin.deviceId)}/${deliveryId}`; } /** The turn a delivery opens: a steer joins the running turn; anything else counts as the connection's turn origin. */ function deliveryTurnMark(command, peer) { return command.deliverAs === "steer" ? "join" : (peer.turnOrigin ?? peer.connectionOrigin); } export function admitExternalMessage(session, command, peer) { const chain = admitInputChain(command.chainCount, session.settingsManager); if (chain.kind === "refused") return { ok: false, refusal: chain.refusal }; const { origin } = peer; // A delegated connection that names no device is never treated as this machine's own: it is refused (fail closed). if (peer.connectionOrigin === "delegated" && origin === undefined) return { ok: false, refusal: { message: `${DELEGATED_ORIGIN_UNKNOWN}: a delegated connection that names no remote device cannot admit a delivery`, errorCode: DELEGATED_ORIGIN_UNKNOWN, }, }; if (origin !== undefined && command.sender?.kind === "command_line") return { ok: false, refusal: { message: `${SENDER_NOT_ALLOWED}: a remote device cannot send as this machine's command line`, errorCode: SENDER_NOT_ALLOWED, }, }; const admission = session.externalAdmission; // A release or a graceful close ended admission (oh-my-openagent#14313): the sender keeps the delivery, typed. if (admission.closed !== undefined) return { ok: false, refusal: { message: admission.closed, errorCode: RPC_ERROR_SESSION_CLOSING } }; const answer = (kind, epoch) => ({ ok: true, data: { kind, turnId: String(epoch) }, }); const id = ledgerId(command.deliveryId, origin); if (session.sessionManager.reduceHistory(writtenDeliveryIds).has(id)) return answer("already_admitted", admission.turnEpoch); const expected = command.expectedTurnId; if (expected !== undefined && !CANONICAL_TURN_ID.test(expected)) return answer("turn_conflict", admission.turnEpoch); const displayText = command.displayText ?? (origin === undefined ? undefined : command.text); const result = admission.admit({ delivery_id: id, text: command.text, deliverAs: command.deliverAs, ...(command.sender === undefined ? {} : { sender: command.sender }), ...(displayText === undefined ? {} : { display_text: displayText }), ...(expected === undefined ? {} : { expected_turn_id: Number(expected) }), ...(command.whenIdle === undefined ? {} : { whenIdle: command.whenIdle }), }, { ...(origin === undefined ? {} : { remoteOrigin: origin }), ...(chain.chainCount === undefined ? {} : { chainCount: chain.chainCount }), turnMark: deliveryTurnMark(command, peer), }); if (result.holds === undefined) return answer(result.kind, result.turn_epoch); return { ok: true, data: { kind: result.kind, turnId: String(result.turn_epoch), holds: [...result.holds] } }; } //# sourceMappingURL=admit-external-message-command.js.map