-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05e9da2
commit 4077a58
Showing
15 changed files
with
500 additions
and
306 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json", | ||
"lib": { | ||
"entryFile": "public_api.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"$schema": "../../node_modules/ng-packagr/package.schema.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { VertexAI, VertexAIInstances, vertexAIInstance$ } from './vertexai'; | ||
export { provideVertexAI, VertexAIModule } from './vertexai.module'; | ||
export * from './firebase'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { | ||
EnvironmentProviders, | ||
InjectionToken, | ||
Injector, | ||
NgModule, | ||
NgZone, | ||
Optional, | ||
makeEnvironmentProviders, | ||
} from '@angular/core'; | ||
import { VERSION, ɵAngularFireSchedulers, ɵgetDefaultInstanceOf } from '@angular/fire'; | ||
import { ɵAppCheckInstances } from '@angular/fire'; | ||
import { FirebaseApp, FirebaseApps } from '@angular/fire/app'; | ||
import { registerVersion } from 'firebase/app'; | ||
import { VertexAI as FirebaseVertexAI } from 'firebase/vertexai-preview'; | ||
import { VERTEX_AI_PROVIDER_NAME, VertexAI, VertexAIInstances } from './vertexai'; | ||
|
||
export const PROVIDED_VERTEX_AI_INSTANCES = new InjectionToken<VertexAI[]>('angularfire2.vertexai-instances'); | ||
|
||
export function defaultVertexAIInstanceFactory(provided: FirebaseVertexAI[]|undefined, defaultApp: FirebaseApp) { | ||
const defaultVertexAI = ɵgetDefaultInstanceOf<FirebaseVertexAI>(VERTEX_AI_PROVIDER_NAME, provided, defaultApp); | ||
return defaultVertexAI && new VertexAI(defaultVertexAI); | ||
} | ||
|
||
export function vertexAIInstanceFactory(fn: (injector: Injector) => FirebaseVertexAI) { | ||
return (zone: NgZone, injector: Injector) => { | ||
const vertexAI = zone.runOutsideAngular(() => fn(injector)); | ||
return new VertexAI(vertexAI); | ||
}; | ||
} | ||
|
||
const VERTEX_AI_INSTANCES_PROVIDER = { | ||
provide: VertexAIInstances, | ||
deps: [ | ||
[new Optional(), PROVIDED_VERTEX_AI_INSTANCES ], | ||
] | ||
}; | ||
|
||
const DEFAULT_VERTEX_AI_INSTANCE_PROVIDER = { | ||
provide: VertexAI, | ||
useFactory: defaultVertexAIInstanceFactory, | ||
deps: [ | ||
[new Optional(), PROVIDED_VERTEX_AI_INSTANCES ], | ||
FirebaseApp, | ||
] | ||
}; | ||
|
||
@NgModule({ | ||
providers: [ | ||
DEFAULT_VERTEX_AI_INSTANCE_PROVIDER, | ||
VERTEX_AI_INSTANCES_PROVIDER, | ||
] | ||
}) | ||
export class VertexAIModule { | ||
constructor() { | ||
registerVersion('angularfire', VERSION.full, 'vertexai'); | ||
} | ||
} | ||
|
||
export function provideVertexAI(fn: (injector: Injector) => FirebaseVertexAI, ...deps: any[]): EnvironmentProviders { | ||
registerVersion('angularfire', VERSION.full, 'vertexai'); | ||
|
||
return makeEnvironmentProviders([ | ||
DEFAULT_VERTEX_AI_INSTANCE_PROVIDER, | ||
VERTEX_AI_INSTANCES_PROVIDER, | ||
{ | ||
provide: PROVIDED_VERTEX_AI_INSTANCES, | ||
useFactory: vertexAIInstanceFactory(fn), | ||
multi: true, | ||
deps: [ | ||
NgZone, | ||
Injector, | ||
ɵAngularFireSchedulers, | ||
FirebaseApps, | ||
[new Optional(), ɵAppCheckInstances ], | ||
...deps, | ||
] | ||
} | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { FirebaseApp, getApp, initializeApp, provideFirebaseApp } from '@angular/fire/app'; | ||
import { VertexAI, getVertexAI, provideVertexAI } from '@angular/fire/vertexai-preview'; | ||
import { COMMON_CONFIG } from '../test-config'; | ||
import { rando } from '../utils'; | ||
|
||
describe('VertexAI', () => { | ||
let app: FirebaseApp; | ||
let vertexAI: VertexAI; | ||
let providedVertexAI: VertexAI; | ||
let appName: string; | ||
|
||
describe('single injection', () => { | ||
|
||
beforeEach(() => { | ||
appName = rando(); | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
provideFirebaseApp(() => initializeApp(COMMON_CONFIG, appName)), | ||
provideVertexAI(() => { | ||
providedVertexAI = getVertexAI(getApp(appName)); | ||
return providedVertexAI; | ||
}), | ||
], | ||
}); | ||
app = TestBed.inject(FirebaseApp); | ||
vertexAI = TestBed.inject(VertexAI); | ||
}); | ||
|
||
it('should be injectable', () => { | ||
expect(providedVertexAI).toBeTruthy(); | ||
expect(vertexAI).toEqual(providedVertexAI); | ||
expect(vertexAI.app).toEqual(app); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { ɵgetAllInstancesOf } from '@angular/fire'; | ||
import { VertexAI as FirebaseVertexAI } from 'firebase/vertexai-preview'; | ||
import { from, timer } from 'rxjs'; | ||
import { concatMap, distinct } from 'rxjs/operators'; | ||
|
||
// see notes in core/firebase.app.module.ts for why we're building the class like this | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
export interface VertexAI extends FirebaseVertexAI {} | ||
|
||
export class VertexAI { | ||
constructor(vertexai: FirebaseVertexAI) { | ||
return vertexai; | ||
} | ||
} | ||
|
||
export const VERTEX_AI_PROVIDER_NAME = 'vertexai'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
export interface VertexAIInstances extends Array<FirebaseVertexAI> {} | ||
|
||
export class VertexAIInstances { | ||
constructor() { | ||
return ɵgetAllInstancesOf<FirebaseVertexAI>(VERTEX_AI_PROVIDER_NAME); | ||
} | ||
} | ||
|
||
export const vertexAIInstance$ = timer(0, 300).pipe( | ||
concatMap(() => from(ɵgetAllInstancesOf<FirebaseVertexAI>(VERTEX_AI_PROVIDER_NAME))), | ||
distinct(), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters