Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
34 changes: 34 additions & 0 deletions packages/ai/src/factory-browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

import { ComponentContainer, InstanceFactoryOptions } from "@firebase/component";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copyright header.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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