@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
39 lines • 2.11 kB
JavaScript
/**
* Where a session's relocation requests go (docs/rpc.md "Continuing a session on another computer"): to ONE connection,
* the one attached to the session that advertised `session_relocation`, and only that connection's answer counts.
*/
import { SESSION_RELOCATION_CAPABILITY } from "./custom-capability.js";
const SOLE_CONNECTION = "sole";
/**
* A classic handler has one client, so its own capabilities decide. A shared host names the offering connection from
* its per-connection capabilities; a binding the host cannot ask (a worker isolate) offers nothing, which is also
* right because `release_session` cannot release such a session.
*/
export function relocationChannel(options) {
const { clientInfo } = options;
if (clientInfo === undefined)
return {
offerer: () => (options.capabilities()?.includes(SESSION_RELOCATION_CAPABILITY) ? SOLE_CONNECTION : undefined),
current: () => SOLE_CONNECTION,
send: (_connection, record, kind) => (kind === "event" ? options.outputEvent(record) : options.output(record)),
};
const { relocationConnection, deliverTo } = clientInfo;
return {
offerer: () => (deliverTo === undefined ? undefined : relocationConnection?.()),
current: () => clientInfo.connectionId(),
send: (connection, record) => deliverTo?.(connection, () => options.output(record)),
};
}
export function answerHostResponse(frame, respond) {
const reply = { id: frame.id, type: "response", command: "host_response" };
if (typeof frame.id !== "string" || frame.id === "")
return { ...reply, success: false, error: "invalid_host_response" };
const outcome = respond?.(frame) ?? "unknown";
if (outcome === "answered")
return { ...reply, success: true };
return { ...reply, success: false, error: outcome === "unknown" ? "unknown_host_request" : outcome };
}
export function isHostResponse(value) {
return typeof value === "object" && value !== null && "type" in value && value.type === "host_response";
}
//# sourceMappingURL=session-relocation-channel.js.map