@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
94 lines • 3.45 kB
JavaScript
import { HOST_EXIT_ERROR, HOST_EXIT_OK } from "./host-outcome.js";
export function waitForHostSession(client, sessionId, request, onTransportLoss) {
const started = Date.now();
return new Promise((resolveWait, reject) => {
let finished = false;
let observed = false;
let state;
let timer;
const cleanup = () => {
finished = true;
clearTimeout(timer);
unsubscribe();
onTransportLoss(undefined);
};
const fail = (error) => {
if (finished)
return;
cleanup();
reject(error);
};
const finish = (outcome) => {
if (finished)
return;
cleanup();
resolveWait({
exitCode: HOST_EXIT_OK,
payload: {
action: "wait",
sessionId,
until: request.until,
outcome,
waited_ms: Date.now() - started,
state: {
isStreaming: state?.isStreaming ?? false,
pendingMessageCount: state?.pendingMessageCount ?? 0,
lastAbortSource: state?.lastAbortSource ?? null,
},
},
});
};
// Subscribe BEFORE the snapshot, including when settlement arrives with that reply.
const unsubscribe = client.onEvent((record) => {
const event = record;
if (event.type === "session_closed" || event.type === "session_parked") {
finish(event.type);
return;
}
if (event.type === "agent_settled" && "reason" in event && event.reason === "session_closed") {
finish("session_closed");
return;
}
if (event.type !== "agent_idle" && !(request.until === "done" && event.type === "agent_settled"))
return;
observed = true;
void client.getState().then((snapshot) => {
state = snapshot;
if (request.until === "done")
finish("done");
else if (isDrained(snapshot))
finish("idle");
}, fail);
});
onTransportLoss(fail);
timer = setTimeout(() => {
if (finished)
return;
cleanup();
resolveWait({
exitCode: HOST_EXIT_ERROR,
payload: {
action: "error",
reason: "wait_timeout",
sessionId,
until: request.until,
timeoutMs: request.timeoutMs,
},
});
}, request.timeoutMs);
void client.getState().then((snapshot) => {
state = snapshot;
// A parked steer keeps `idle` waiting: it only drains with the next prompt.
if (!observed && (request.until === "done" ? !snapshot.isStreaming : isDrained(snapshot)))
finish("already_idle");
}, fail);
});
}
function isDrained(state) {
return (!state.isStreaming &&
state.steering.length === 0 &&
state.followUp.length === 0 &&
state.ordered.length === 0 &&
state.pendingMessageCount === 0);
}
//# sourceMappingURL=host-session-wait.js.map