Skip to content

Commit 2465e38

Browse files
MumuTWgsquared94
authored andcommitted
fix(core): handle AbortError thrown during processTurn (google-gemini#21296)
Co-authored-by: Gaurav <39389231+gsquared94@users.noreply.github.com>
1 parent 5d7a0cb commit 2465e38

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

packages/core/src/core/client.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,23 @@ describe('Gemini Client (client.ts)', () => {
728728
);
729729
});
730730

731+
it('yields UserCancelled when processTurn throws AbortError', async () => {
732+
const abortError = new Error('Aborted');
733+
abortError.name = 'AbortError';
734+
vi.spyOn(client['loopDetector'], 'turnStarted').mockRejectedValueOnce(
735+
abortError,
736+
);
737+
738+
const stream = client.sendMessageStream(
739+
[{ text: 'Hi' }],
740+
new AbortController().signal,
741+
'prompt-id-abort-error',
742+
);
743+
const events = await fromAsync(stream);
744+
745+
expect(events).toEqual([{ type: GeminiEventType.UserCancelled }]);
746+
});
747+
731748
it.each([
732749
{
733750
compressionStatus:

packages/core/src/core/client.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
type RetryAvailabilityContext,
3535
} from '../utils/retry.js';
3636
import type { ValidationRequiredError } from '../utils/googleQuotaErrors.js';
37-
import { getErrorMessage } from '../utils/errors.js';
37+
import { getErrorMessage, isAbortError } from '../utils/errors.js';
3838
import { tokenLimit } from './tokenLimits.js';
3939
import type {
4040
ChatRecordingService,
@@ -957,6 +957,12 @@ export class GeminiClient {
957957
);
958958
}
959959
}
960+
} catch (error) {
961+
if (signal?.aborted || isAbortError(error)) {
962+
yield { type: GeminiEventType.UserCancelled };
963+
return turn;
964+
}
965+
throw error;
960966
} finally {
961967
const hookState = this.hookStateMap.get(prompt_id);
962968
if (hookState) {

0 commit comments

Comments
 (0)