Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/heavy-teachers-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/ai': patch
---

Refactor component registration.
3 changes: 2 additions & 1 deletion packages/ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"dev": "rollup -c -w",
"update-responses": "../../scripts/update_vertexai_responses.sh",
"testsetup": "yarn update-responses && yarn ts-node ./test-utils/convert-mocks.ts",
"test": "run-p --npm-path npm lint test:browser",
"test": "run-p --npm-path npm lint type-check test:browser",
"test:ci": "yarn testsetup && node ../../scripts/run_tests_in_ci.js -s test",
"test:skip-clone": "karma start",
"test:browser": "yarn testsetup && karma start",
Expand All @@ -44,6 +44,7 @@
"test:integration:node": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' mocha integration/**/*.test.ts --config ../../config/mocharc.node.js",
"api-report": "api-extractor run --local --verbose",
"typings:public": "node ../../scripts/build/use_typings.js ./dist/ai-public.d.ts",
"type-check": "yarn tsc --noEmit",
"trusted-type-check": "tsec -p tsconfig.json --noEmit"
},
"peerDependencies": {
Expand Down
53 changes: 53 additions & 0 deletions packages/ai/src/factory-browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @license
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {
ComponentContainer,
InstanceFactoryOptions
} from '@firebase/component';
import { AIError } from './errors';
import { decodeInstanceIdentifier } from './helpers';
import { chromeAdapterFactory } from './methods/chrome-adapter';
import { AIService } from './service';
import { AIErrorCode } from './types';

export function factory(
container: ComponentContainer,
{ instanceIdentifier }: InstanceFactoryOptions
): AIService {
if (!instanceIdentifier) {
throw new AIError(
AIErrorCode.ERROR,
'AIService instance identifier is undefined.'
);
}

const backend = decodeInstanceIdentifier(instanceIdentifier);

// getImmediate for FirebaseApp will always succeed
const app = container.getProvider('app').getImmediate();
const auth = container.getProvider('auth-internal');
const appCheckProvider = container.getProvider('app-check-internal');

return new AIService(
app,
backend,
auth,
appCheckProvider,
chromeAdapterFactory
);
}
40 changes: 2 additions & 38 deletions packages/ai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,54 +22,18 @@
*/

import { registerVersion, _registerComponent } from '@firebase/app';
import { AIService } from './service';
import { AI_TYPE } from './constants';
import {
Component,
ComponentContainer,
ComponentType,
InstanceFactoryOptions
} from '@firebase/component';
import { Component, ComponentType } from '@firebase/component';
import { name, version } from '../package.json';
import { decodeInstanceIdentifier } from './helpers';
import { AIError } from './api';
import { AIErrorCode } from './types';
import { chromeAdapterFactory } from './methods/chrome-adapter';
import { LanguageModel } from './types/language-model';
import { factory } from './factory-browser';

declare global {
interface Window {
LanguageModel: LanguageModel;
}
}

function factory(
container: ComponentContainer,
{ instanceIdentifier }: InstanceFactoryOptions
): AIService {
if (!instanceIdentifier) {
throw new AIError(
AIErrorCode.ERROR,
'AIService instance identifier is undefined.'
);
}

const backend = decodeInstanceIdentifier(instanceIdentifier);

// getImmediate for FirebaseApp will always succeed
const app = container.getProvider('app').getImmediate();
const auth = container.getProvider('auth-internal');
const appCheckProvider = container.getProvider('app-check-internal');

return new AIService(
app,
backend,
auth,
appCheckProvider,
chromeAdapterFactory
);
}

function registerAI(): void {
_registerComponent(
new Component(AI_TYPE, factory, ComponentType.PUBLIC).setMultipleInstances(
Expand Down
8 changes: 6 additions & 2 deletions packages/ai/test-utils/get-fake-firebase-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import { Component, ComponentType } from '@firebase/component';
import { FirebaseAppCheckInternal } from '@firebase/app-check-interop-types';
import { AI_TYPE } from '../src/constants';
import { factory } from '../src';
import { factory } from '../src/factory-browser';

const fakeConfig = {
projectId: 'projectId',
Expand All @@ -38,7 +38,11 @@ export function getFullApp(fakeAppParams?: {
appId?: string;
apiKey?: string;
}): FirebaseApp {
_registerComponent(new Component(AI_TYPE, factory, ComponentType.PUBLIC));
_registerComponent(
new Component(AI_TYPE, factory, ComponentType.PUBLIC).setMultipleInstances(
true
)
);
_registerComponent(
new Component(
'app-check-internal',
Expand Down
Loading