Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/swarm-ui/src/agent/terminal/cancel.ts

41 lines
1.5 KiB
TypeScript

// What the terminal page decides about cancelling a turn, without a DOM. The
// controller answers `POST /api/agents/{name}/cancel` with whether a turn was
// running and was stopped (`acted`), so a cancel is never silently a no-op.
/** Whether to offer the cancel button: while the agent is in a turn. */
export function canCancel(turnState: string | undefined): boolean {
return turnState === "thinking";
}
/** The path a cancel is sent to. */
export function cancelPath(agentName: string): string {
return `/api/agents/${encodeURIComponent(agentName)}/cancel`;
}
/** What to tell the operator once the agent has answered. */
export function cancelNote(acted: boolean): string {
return acted ? "turn cancelled" : "nothing was running";
}
/** What the cancel badge is showing. */
export type CancelStatus = "idle" | "cancelling" | "done" | "error";
/**
* The badge's status once the agent's turn state moves from `prev` to `now`.
* The answer ("turn cancelled") stays up after the turn it stopped ends, and
* clears when the next turn starts, so that turn can be cancelled too. A
* failure clears as soon as its turn ends: with no turn running, there is
* nothing left to retry.
*/
export function statusOnTurnChange(
status: CancelStatus,
prev: string | undefined,
now: string | undefined,
): CancelStatus {
if (status === "cancelling") return status;
const wasIn = canCancel(prev);
const isIn = canCancel(now);
if (isIn && !wasIn) return "idle";
if (!isIn && wasIn && status === "error") return "idle";
return status;
}