避免abort后创建新的session
This commit is contained in:
@@ -31,6 +31,7 @@ export class ChatSessionBridge {
|
|||||||
// runtime session 仅在单次请求生命周期内有效;线程连续性由 clientSessionId 对应的持久状态承担。
|
// runtime session 仅在单次请求生命周期内有效;线程连续性由 clientSessionId 对应的持久状态承担。
|
||||||
private readonly activeRuntimeSessions = new Map<string, string>();
|
private readonly activeRuntimeSessions = new Map<string, string>();
|
||||||
private readonly activeSensitiveContexts = new Map<string, ChatRequestContext>();
|
private readonly activeSensitiveContexts = new Map<string, ChatRequestContext>();
|
||||||
|
private readonly abortControllers = new Map<string, AbortController>();
|
||||||
private readonly toolContextStore = new ToolSessionContextStore();
|
private readonly toolContextStore = new ToolSessionContextStore();
|
||||||
|
|
||||||
constructor(private readonly runtime: OpencodeRuntimeAdapter) {}
|
constructor(private readonly runtime: OpencodeRuntimeAdapter) {}
|
||||||
@@ -89,6 +90,14 @@ export class ChatSessionBridge {
|
|||||||
return this.activeSensitiveContexts.get(sessionScopeKey) ?? null;
|
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: {
|
async abort(context: {
|
||||||
clientSessionId?: string;
|
clientSessionId?: string;
|
||||||
}): Promise<SessionBinding | null> {
|
}): Promise<SessionBinding | null> {
|
||||||
@@ -144,11 +153,18 @@ export class ChatSessionBridge {
|
|||||||
|
|
||||||
private async abortActiveRuntime(clientSessionId: string) {
|
private async abortActiveRuntime(clientSessionId: string) {
|
||||||
const activeSessionId = this.activeRuntimeSessions.get(clientSessionId);
|
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) {
|
if (!activeSessionId) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.activeRuntimeSessions.delete(clientSessionId);
|
|
||||||
this.activeSensitiveContexts.delete(findScopeKey(this.activeSensitiveContexts, clientSessionId));
|
|
||||||
await this.toolContextStore.remove(activeSessionId).catch(() => undefined);
|
await this.toolContextStore.remove(activeSessionId).catch(() => undefined);
|
||||||
await this.runtime.abortSession(activeSessionId).catch((error) => {
|
await this.runtime.abortSession(activeSessionId).catch((error) => {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
|
|||||||
@@ -505,6 +505,7 @@ export const buildChatRouter = (
|
|||||||
const clientSessionId = requestContext.clientSessionId;
|
const clientSessionId = requestContext.clientSessionId;
|
||||||
let streamClosed = false;
|
let streamClosed = false;
|
||||||
const abortController = new AbortController();
|
const abortController = new AbortController();
|
||||||
|
sessionBridge.registerAbortController(clientSessionId, abortController);
|
||||||
const handleClientClose = () => {
|
const handleClientClose = () => {
|
||||||
if (streamClosed || abortController.signal.aborted) {
|
if (streamClosed || abortController.signal.aborted) {
|
||||||
return;
|
return;
|
||||||
@@ -606,6 +607,7 @@ export const buildChatRouter = (
|
|||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
await sessionBridge.releaseRuntimeSession(clientSessionId, binding.sessionId);
|
await sessionBridge.releaseRuntimeSession(clientSessionId, binding.sessionId);
|
||||||
|
sessionBridge.deleteAbortController(clientSessionId);
|
||||||
streamClosed = true;
|
streamClosed = true;
|
||||||
req.off("close", handleClientClose);
|
req.off("close", handleClientClose);
|
||||||
res.off("close", handleClientClose);
|
res.off("close", handleClientClose);
|
||||||
|
|||||||
Reference in New Issue
Block a user