abort route

This commit is contained in:
Harivansh Rathi 2026-03-12 15:21:04 -04:00
parent e19f575229
commit a5e8a69cd4
3 changed files with 127 additions and 15 deletions

View file

@ -282,9 +282,20 @@ export class GatewayRuntime {
abortSession(sessionKey: string): boolean {
const managedSession = this.sessions.get(sessionKey);
if (!managedSession?.processing) {
if (!managedSession) {
return false;
}
const hadQueuedMessages = managedSession.queue.length > 0;
if (hadQueuedMessages) {
this.rejectQueuedMessages(managedSession, "Session aborted");
this.emitState(managedSession);
}
if (!managedSession.processing) {
return hadQueuedMessages;
}
void managedSession.session.abort().catch((error) => {
this.emit(managedSession, {
type: "error",

View file

@ -1,4 +1,5 @@
import { describe, expect, it, vi } from "vitest";
import type { ManagedGatewaySession } from "../src/core/gateway/internal-types.js";
import { GatewayRuntime } from "../src/core/gateway/runtime.js";
function createMockSession() {
@ -43,9 +44,9 @@ function addManagedSession(
session: ReturnType<typeof createMockSession>,
processing: boolean,
) {
const managedSession = {
const managedSession: ManagedGatewaySession = {
sessionKey,
session,
session: session as never,
queue: [],
processing,
activeAssistantMessage: null,
@ -124,4 +125,36 @@ describe("GatewayRuntime steer handling", () => {
});
});
});
it("abort clears queued follow-ups before aborting the active session", () => {
const session = createMockSession();
const runtime = createRuntime(session);
const managedSession = addManagedSession(runtime, "chat", session, true);
const resolve = vi.fn();
managedSession.queue.push({
request: {
sessionKey: "chat",
text: "stale follow-up",
source: "extension",
},
resolve,
});
const result = (
runtime as unknown as {
abortSession: (sessionKey: string) => boolean;
}
).abortSession("chat");
expect(result).toBe(true);
expect(managedSession.queue).toHaveLength(0);
expect(resolve).toHaveBeenCalledWith({
ok: false,
response: "",
error: "Session aborted",
sessionKey: "chat",
});
expect(session.abort).toHaveBeenCalledTimes(1);
});
});