@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
74 lines • 3.61 kB
JavaScript
import { openReplyPermissionMode } from "../../core/permission-mode.js";
import { buildRpcSessionState } from "./connection-handler.js";
import { parkedEntryFromProfile } from "./host-handover-parked-open.js";
/** A dropped opener waits until its open or claim has either transferred or released ownership. */
export function holdOpenConnection(host, owner) {
const barrier = Promise.withResolvers();
const opens = host.opensByConnection.get(owner) ?? new Set();
opens.add(barrier.promise);
host.opensByConnection.set(owner, opens);
return () => {
barrier.resolve();
opens.delete(barrier.promise);
if (opens.size === 0)
host.opensByConnection.delete(owner);
};
}
/** One ownership transition for both a freshly built open and a claimed speculative open. */
export function ownOpen(host, opened, owner, compact = false) {
if (owner === undefined)
return;
if (!host.writer.hasRegisteredConnectionCapabilities(owner))
host.writer.setConnectionCapabilities(owner, host.pendingCapabilities.get(owner) ?? host.connectionOptions?.capabilities ?? []);
host.writer.attachConnectionToSession(owner, opened.sessionId, compact);
const owned = host.sessionsByConnection.get(owner) ?? new Map();
owned.set(opened.sessionId, (owned.get(opened.sessionId) ?? 0) + 1);
host.sessionsByConnection.set(owner, owned);
host.attachOrder.attached(opened.sessionId, owner);
}
export function publishOpen(host, opened, entry) {
if (opened.attached)
host.writer.status.publish(opened.sessionId, "attach", "client");
else {
host.writer.publishSessionOpened({
type: "session_opened",
sessionId: opened.sessionId,
durableSessionId: opened.durableSessionId,
...(opened.sessionPath !== undefined ? { sessionPath: opened.sessionPath } : {}),
kind: entry.kind,
cwd: entry.cwd,
context: entry.context,
});
host.writer.status.publish(opened.sessionId, "open", "client");
}
}
export function replyOpen(host, opened, entry, command, reply = true, beside = {}) {
const state = entry.worker?.snapshot?.state ?? (entry.runtime ? buildRpcSessionState(entry.runtime.session) : undefined);
if (!state)
throw new Error("Session state was not created");
if (reply)
host.writer.enqueue(opened.sessionId, {
id: command.id,
type: "response",
command: command.type,
success: true,
data: {
...beside,
sessionId: opened.sessionId,
state,
...(host.writer.currentConnection()?.startsWith("generation-route-") && opened.sessionPath
? {
routeRow: host.registry.list().find((row) => row.sessionId === opened.sessionId),
routePark: {
...parkedEntryFromProfile({ ...opened, sessionPath: opened.sessionPath, contextOverlay: entry.contextOverlay }, entry.profile, Object.keys(host.defaults.hostContext ?? {})),
retainOnDisconnect: entry.retainOnDisconnect === true,
},
}
: {}),
...openReplyPermissionMode(state),
...(opened.attached ? { attached: true } : {}),
...(host.defaults.sessionPresence ? { presence: host.defaults.sessionPresence.view(state.sessionId) } : {}),
},
});
}
//# sourceMappingURL=session-router-open-result.js.map