Skip to content

Commit 8c9c1c2

Browse files
committed
fix prefer_on_cloud fallback criteria
1 parent f929d6c commit 8c9c1c2

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

packages/ai/src/methods/generate-content.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { ApiSettings } from '../types/internal';
2929
import * as GoogleAIMapper from '../googleai-mappers';
3030
import { BackendType } from '../public-types';
3131
import { ChromeAdapter } from '../types/chrome-adapter';
32-
import { callCloudOrDevice } from './helpers';
32+
import { callCloudOrDevice } from '../requests/hybrid-helpers';
3333

3434
async function generateContentStreamOnCloud(
3535
apiSettings: ApiSettings,

packages/ai/src/methods/helpers.test.ts renamed to packages/ai/src/requests/hybrid-helpers.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
import { use, expect } from 'chai';
1919
import { SinonStub, SinonStubbedInstance, restore, stub } from 'sinon';
20-
import { callCloudOrDevice } from './helpers';
20+
import { callCloudOrDevice } from './hybrid-helpers';
2121
import { GenerateContentRequest, InferenceMode, AIErrorCode } from '../types';
2222
import { AIError } from '../errors';
2323
import sinonChai from 'sinon-chai';
2424
import chaiAsPromised from 'chai-as-promised';
25-
import { ChromeAdapterImpl } from './chrome-adapter';
25+
import { ChromeAdapterImpl } from '../methods/chrome-adapter';
2626

2727
use(sinonChai);
2828
use(chaiAsPromised);
@@ -159,7 +159,7 @@ describe('callCloudOrDevice', () => {
159159
expect(onDeviceCall).to.not.have.been.called;
160160
});
161161

162-
it('should fall back to onDeviceCall if inCloudCall fails with AIError', async () => {
162+
it('should fall back to onDeviceCall if inCloudCall fails with AIErrorCode.FETCH_ERROR', async () => {
163163
inCloudCall.rejects(
164164
new AIError(AIErrorCode.FETCH_ERROR, 'Network error')
165165
);
@@ -175,7 +175,7 @@ describe('callCloudOrDevice', () => {
175175
});
176176

177177
it('should re-throw other errors from inCloudCall', async () => {
178-
const error = new Error('Some other error');
178+
const error = new AIError(AIErrorCode.RESPONSE_ERROR, 'safety problem');
179179
inCloudCall.rejects(error);
180180
await expect(
181181
callCloudOrDevice(request, chromeAdapter, onDeviceCall, inCloudCall)

packages/ai/src/methods/helpers.ts renamed to packages/ai/src/requests/hybrid-helpers.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,16 @@ import {
2222
AIErrorCode,
2323
ChromeAdapter
2424
} from '../types';
25-
import { ChromeAdapterImpl } from './chrome-adapter';
25+
import { ChromeAdapterImpl } from '../methods/chrome-adapter';
26+
27+
const errorsCausingFallback: AIErrorCode[] = [
28+
// most network errors
29+
AIErrorCode.FETCH_ERROR,
30+
// fallback code for all other errors in makeRequest
31+
AIErrorCode.ERROR,
32+
// error due to API not being enabled in project
33+
AIErrorCode.API_NOT_ENABLED
34+
];
2635

2736
/**
2837
* Dispatches a request to the appropriate backend (on-device or in-cloud)
@@ -58,7 +67,7 @@ export async function callCloudOrDevice<Response>(
5867
try {
5968
return await inCloudCall();
6069
} catch (e) {
61-
if (e instanceof AIError) {
70+
if (e instanceof AIError && errorsCausingFallback.includes(e.code)) {
6271
return onDeviceCall();
6372
}
6473
throw e;

0 commit comments

Comments
 (0)