Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/core/src/core/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,23 @@ describe('Gemini Client (client.ts)', () => {
);
});

it('yields UserCancelled when processTurn throws AbortError', async () => {
const abortError = new Error('Aborted');
abortError.name = 'AbortError';
vi.spyOn(client['loopDetector'], 'turnStarted').mockRejectedValueOnce(
abortError,
);

const stream = client.sendMessageStream(
[{ text: 'Hi' }],
new AbortController().signal,
'prompt-id-abort-error',
);
const events = await fromAsync(stream);

expect(events).toEqual([{ type: GeminiEventType.UserCancelled }]);
});

it.each([
{
compressionStatus:
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
type RetryAvailabilityContext,
} from '../utils/retry.js';
import type { ValidationRequiredError } from '../utils/googleQuotaErrors.js';
import { getErrorMessage } from '../utils/errors.js';
import { getErrorMessage, isAbortError } from '../utils/errors.js';
import { tokenLimit } from './tokenLimits.js';
import type {
ChatRecordingService,
Expand Down Expand Up @@ -957,6 +957,12 @@ export class GeminiClient {
);
}
}
} catch (error) {
if (signal?.aborted || isAbortError(error)) {
yield { type: GeminiEventType.UserCancelled };
return turn;
}
throw error;
} finally {
const hookState = this.hookStateMap.get(prompt_id);
if (hookState) {
Expand Down
Loading