Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(agent-runtime): abort the in-flight model call when a server run is interrupted (#20217)

Inneicf19e4b971ad
diff --git a/apps/server/src/modules/AgentRuntime/adapters/serverCallLlmAttempt.test.ts b/apps/server/src/modules/AgentRuntime/adapters/serverCallLlmAttempt.test.ts
index a75050dfdb8..a3cdab3d53e 100644
--- a/apps/server/src/modules/AgentRuntime/adapters/serverCallLlmAttempt.test.ts
+++ b/apps/server/src/modules/AgentRuntime/adapters/serverCallLlmAttempt.test.ts
@@ -71,6 +71,7 @@ const createAttempt = (
agentShareVisitorIds?: { agentId: string; shareId: string; visitorUserId: string };
userAgent?: string;
},
+ abortSignal?: AbortSignal,
) => {
const publishStreamChunk = vi.fn().mockResolvedValue('event-1');
const streamManager = {
@@ -78,6 +79,7 @@ const createAttempt = (
publishStreamEvent: vi.fn().mockResolvedValue('event-2'),
} as unknown as RuntimeExecutorContext['streamManager'];
const ctx = {
+ abortSignal,
messageModel: {} as RuntimeExecutorContext['messageModel'],
operationId: 'operation-1',
serverDB: {} as RuntimeExecutorContext['serverDB'],
@@ -336,6 +338,34 @@ describe('ServerCallLlmAttempt', () => {
expect(recordModelCompletionFailureMock).not.toHaveBeenCalled();
});
+ it('hands the operation abort signal to the provider so an interrupt stops the stream', async () => {
+ const interrupt = new AbortController();
+ const { attempt } = createAttempt(
+ async ({ callback, signal }) => {
+ await callback?.onThinking?.('Planning the article');
+ interrupt.abort();
+ if (signal?.aborted) {
+ await callback?.onCompletion?.({ finishReason: 'abort', text: '' });
+ return;
+ }
+ await callback?.onText?.('Full article of the cancelled request');
+ await callback?.onCompletion?.({ text: '', usage: { totalOutputTokens: 800 } });
+ },
+ undefined,
+ undefined,
+ interrupt.signal,
+ );
+
+ await attempt.execute();
+
+ expect(attempt.snapshot()).toMatchObject({
+ content: '',
+ finishReason: 'abort',
+ thinkingContent: 'Planning the article',
+ usage: undefined,
+ });
+ });
+
it('stores an in-band stream error with the trace id attached by the error hook', async () => {
const { attempt, handleChatStreamError } = createAttempt(async ({ callback }) => {
await callback?.onError?.({ errorType: 'ProviderBizError', message: 'Provider timed out' });
diff --git a/apps/server/src/modules/AgentRuntime/adapters/serverCallLlmAttempt.ts b/apps/server/src/modules/AgentRuntime/adapters/serverCallLlmAttempt.ts
index 20d23c99d41..e2d3f8ff505 100644
--- a/apps/server/src/modules/AgentRuntime/adapters/serverCallLlmAttempt.ts
+++ b/apps/server/src/modules/AgentRuntime/adapters/serverCallLlmAttempt.ts
@@ -332,6 +332,7 @@ export class ServerCallLlmAttempt {
},
diagnostics: this.runtimeDiagnostics,
metadata: this.runtimeMetadata,
+ signal: this.ctx.abortSignal,
user: this.ctx.userId,
};
const response = await this.modelRuntime.chat(this.chatPayload, chatOptions);