避免abort后创建新的session

This commit is contained in:
2026-06-03 10:04:00 +08:00
parent 96e5d25518
commit 76d4b510f4
2 changed files with 20 additions and 2 deletions
+18 -2
View File
@@ -31,6 +31,7 @@ export class ChatSessionBridge {
// runtime session 仅在单次请求生命周期内有效;线程连续性由 clientSessionId 对应的持久状态承担。
private readonly activeRuntimeSessions = new Map<string, string>();
private readonly activeSensitiveContexts = new Map<string, ChatRequestContext>();
private readonly abortControllers = new Map<string, AbortController>();
private readonly toolContextStore = new ToolSessionContextStore();
constructor(private readonly runtime: OpencodeRuntimeAdapter) {}
@@ -89,6 +90,14 @@ export class ChatSessionBridge {
return this.activeSensitiveContexts.get(sessionScopeKey) ?? null;
}
registerAbortController(clientSessionId: string, controller: AbortController) {
this.abortControllers.set(clientSessionId, controller);
}
deleteAbortController(clientSessionId: string) {
this.abortControllers.delete(clientSessionId);
}
async abort(context: {
clientSessionId?: string;
}): Promise<SessionBinding | null> {
@@ -144,11 +153,18 @@ export class ChatSessionBridge {
private async abortActiveRuntime(clientSessionId: string) {
const activeSessionId = this.activeRuntimeSessions.get(clientSessionId);
this.activeRuntimeSessions.delete(clientSessionId);
this.activeSensitiveContexts.delete(findScopeKey(this.activeSensitiveContexts, clientSessionId));
const controller = this.abortControllers.get(clientSessionId);
if (controller) {
this.abortControllers.delete(clientSessionId);
controller.abort();
}
if (!activeSessionId) {
return;
}
this.activeRuntimeSessions.delete(clientSessionId);
this.activeSensitiveContexts.delete(findScopeKey(this.activeSensitiveContexts, clientSessionId));
await this.toolContextStore.remove(activeSessionId).catch(() => undefined);
await this.runtime.abortSession(activeSessionId).catch((error) => {
logger.warn(
+2
View File
@@ -505,6 +505,7 @@ export const buildChatRouter = (
const clientSessionId = requestContext.clientSessionId;
let streamClosed = false;
const abortController = new AbortController();
sessionBridge.registerAbortController(clientSessionId, abortController);
const handleClientClose = () => {
if (streamClosed || abortController.signal.aborted) {
return;
@@ -606,6 +607,7 @@ export const buildChatRouter = (
}
} finally {
await sessionBridge.releaseRuntimeSession(clientSessionId, binding.sessionId);
sessionBridge.deleteAbortController(clientSessionId);
streamClosed = true;
req.off("close", handleClientClose);
res.off("close", handleClientClose);