Skip to content

Commit 90434ab

Browse files
committed
Declare module for types, to fix build error
1 parent 406a67c commit 90434ab

File tree

4 files changed

+68
-33
lines changed

4 files changed

+68
-33
lines changed

packages/ai/src/api.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
// Imports navigator.userAgentData types.
18-
// The user-agent-data-types package isn't intended for modular imports.
19-
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
20-
/// <reference types="user-agent-data-types" />
17+
2118
import { FirebaseApp, getApp, _getProvider } from '@firebase/app';
2219
import { Provider } from '@firebase/component';
2320
import { getModularInstance } from '@firebase/util';
@@ -37,6 +34,7 @@ import { encodeInstanceIdentifier } from './helpers';
3734
import { GoogleAIBackend, VertexAIBackend } from './backend';
3835
import { ChromeAdapter } from './methods/chrome-adapter';
3936
import { LanguageModel } from './types/language-model';
37+
import { NavigatorUA } from './types/user-agent-data';
4038

4139
export { ChatSession } from './methods/chat-session';
4240
export * from './requests/schema-builder';
@@ -178,7 +176,7 @@ export function getGenerativeModel(
178176
inCloudParams,
179177
new ChromeAdapter(
180178
window.LanguageModel as LanguageModel,
181-
window.navigator.userAgentData as UADataValues,
179+
(window.navigator as NavigatorUA).userAgentData,
182180
hybridParams.mode,
183181
hybridParams.onDeviceParams
184182
),

packages/ai/src/methods/chrome-adapter.test.ts

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
// Imports navigator.userAgentData types.
18-
// The user-agent-data-types package isn't intended for modular imports.
19-
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
20-
/// <reference types="user-agent-data-types" />
17+
2118
import { AIError } from '../errors';
2219
import { expect, use } from 'chai';
2320
import sinonChai from 'sinon-chai';
@@ -29,6 +26,7 @@ import {
2926
LanguageModelCreateOptions,
3027
LanguageModelMessage
3128
} from '../types/language-model';
29+
import { UADataValues } from '../types/user-agent-data';
3230
import { match, stub } from 'sinon';
3331
import { GenerateContentRequest, AIErrorCode } from '../types';
3432
import { Schema } from '../api';
@@ -137,7 +135,9 @@ describe('ChromeAdapter', () => {
137135
availability: async () => Availability.available
138136
} as LanguageModel,
139137
// Defines user agent, but no supported browser.
140-
{ brands: [] } as UADataValues,
138+
{
139+
brands: []
140+
} as UADataValues,
141141
'prefer_on_device'
142142
);
143143
expect(
@@ -152,29 +152,25 @@ describe('ChromeAdapter', () => {
152152
availability: async () => Availability.available
153153
} as LanguageModel,
154154
{
155+
// Defines supported browser.
155156
brands: [{ brand: 'Google Chrome', version: '138' }]
156157
} as UADataValues,
157158
'prefer_on_device'
158159
);
159-
for (const mimeType of ChromeAdapter.SUPPORTED_MIME_TYPES) {
160-
expect(
161-
await adapter.isAvailable({
162-
contents: [
163-
{
164-
role: 'user',
165-
parts: [
166-
{
167-
inlineData: {
168-
mimeType,
169-
data: ''
170-
}
171-
}
172-
]
173-
}
174-
]
175-
})
176-
).to.be.true;
177-
}
160+
expect(
161+
await adapter.isAvailable({
162+
contents: [
163+
{
164+
role: 'user',
165+
parts: [
166+
{
167+
text: 'hi'
168+
}
169+
]
170+
}
171+
]
172+
})
173+
).to.be.true;
178174
});
179175
it('returns false if request content has "function" role', async () => {
180176
const adapter = new ChromeAdapter(

packages/ai/src/methods/chrome-adapter.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
// Imports navigator.userAgentData types.
18-
// The user-agent-data-types package isn't intended for modular imports.
19-
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
20-
/// <reference types="user-agent-data-types" />
17+
2118
import { AIError } from '../errors';
2219
import { logger } from '../logger';
2320
import {
@@ -40,6 +37,7 @@ import {
4037
LanguageModelMessageRole,
4138
LanguageModelMessageType
4239
} from '../types/language-model';
40+
import { UADataValues } from '../types/user-agent-data';
4341
import deepMerge from 'deepmerge';
4442

4543
/**
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* Exports the minimal subset of
20+
* https://github.com/lukewarlow/user-agent-data-types
21+
* required by this SDK. That package is designed to be
22+
* imported using triple slash references, which are
23+
* prohibited in this SDK by TSLint.
24+
*/
25+
26+
export interface NavigatorUA {
27+
readonly userAgentData?: NavigatorUAData;
28+
}
29+
30+
interface NavigatorUABrandVersion {
31+
readonly brand: string;
32+
readonly version: string;
33+
}
34+
35+
export interface UADataValues {
36+
readonly brands?: NavigatorUABrandVersion[];
37+
}
38+
39+
interface UALowEntropyJSON {
40+
readonly brands: NavigatorUABrandVersion[];
41+
}
42+
43+
interface NavigatorUAData extends UALowEntropyJSON {}

0 commit comments

Comments
 (0)