Skip to content

Commit 5afb9c6

Browse files
committed
Add limitedUseToken option to AI SDK
1 parent 4923537 commit 5afb9c6

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed

common/api-review/ai.api.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface AI {
1515
backend: Backend;
1616
// @deprecated (undocumented)
1717
location: string;
18+
options?: AIOptions;
1819
}
1920

2021
// @public
@@ -61,6 +62,7 @@ export abstract class AIModel {
6162

6263
// @public
6364
export interface AIOptions {
65+
appCheck?: AppCheckOptions;
6466
backend: Backend;
6567
}
6668

@@ -75,6 +77,12 @@ export class AnyOfSchema extends Schema {
7577
toJSON(): SchemaRequest;
7678
}
7779

80+
// @public
81+
export interface AppCheckOptions {
82+
// (undocumented)
83+
limitedUseTokens?: boolean;
84+
}
85+
7886
// @public
7987
export class ArraySchema extends Schema {
8088
constructor(schemaParams: SchemaParams, items: TypedSchema);

packages/ai/src/api.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,24 @@ declare module '@firebase/component' {
7272
*
7373
* @public
7474
*/
75-
export function getAI(
76-
app: FirebaseApp = getApp(),
77-
options: AIOptions = { backend: new GoogleAIBackend() }
78-
): AI {
75+
export function getAI(app: FirebaseApp = getApp(), options?: AIOptions): AI {
7976
app = getModularInstance(app);
8077
// Dependencies
8178
const AIProvider: Provider<'AI'> = _getProvider(app, AI_TYPE);
8279

83-
const identifier = encodeInstanceIdentifier(options.backend);
84-
return AIProvider.getImmediate({
80+
const finalOptions = {
81+
backend: options?.backend ?? new GoogleAIBackend(),
82+
appCheck: options?.appCheck ?? { limitedUseTokens: false }
83+
};
84+
85+
const identifier = encodeInstanceIdentifier(finalOptions.backend);
86+
const aiInstance = AIProvider.getImmediate({
8587
identifier
8688
});
89+
90+
aiInstance.options = finalOptions;
91+
92+
return aiInstance;
8793
}
8894

8995
/**

packages/ai/src/models/ai-model.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,13 @@ export abstract class AIModel {
9090
return Promise.resolve({ token });
9191
};
9292
} else if ((ai as AIService).appCheck) {
93-
this._apiSettings.getAppCheckToken = () =>
94-
(ai as AIService).appCheck!.getToken();
93+
if (ai.options?.appCheck?.limitedUseTokens) {
94+
this._apiSettings.getAppCheckToken = () =>
95+
(ai as AIService).appCheck!.getLimitedUseToken();
96+
} else {
97+
this._apiSettings.getAppCheckToken = () =>
98+
(ai as AIService).appCheck!.getToken();
99+
}
95100
}
96101

97102
if ((ai as AIService).auth) {

packages/ai/src/public-types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export interface AI {
3838
* Vertex AI Gemini API (using {@link VertexAIBackend}).
3939
*/
4040
backend: Backend;
41+
/**
42+
* Options applied to this {@link AI} instance.
43+
*/
44+
options?: AIOptions;
4145
/**
4246
* @deprecated use `AI.backend.location` instead.
4347
*
@@ -92,4 +96,15 @@ export interface AIOptions {
9296
* The backend configuration to use for the AI service instance.
9397
*/
9498
backend: Backend;
99+
/**
100+
* Configures App Check usage for this AI service instance.
101+
*/
102+
appCheck?: AppCheckOptions;
103+
}
104+
105+
/**
106+
* Configures App Check usage for this AI service instance.
107+
*/
108+
export interface AppCheckOptions {
109+
limitedUseTokens?: boolean;
95110
}

packages/ai/src/service.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import { FirebaseApp, _FirebaseService } from '@firebase/app';
19-
import { AI } from './public-types';
19+
import { AI, AIOptions } from './public-types';
2020
import {
2121
AppCheckInternalComponentName,
2222
FirebaseAppCheckInternal
@@ -31,6 +31,7 @@ import { Backend, VertexAIBackend } from './backend';
3131
export class AIService implements AI, _FirebaseService {
3232
auth: FirebaseAuthInternal | null;
3333
appCheck: FirebaseAppCheckInternal | null;
34+
_options?: AIOptions;
3435
location: string; // This is here for backwards-compatibility
3536

3637
constructor(
@@ -54,4 +55,12 @@ export class AIService implements AI, _FirebaseService {
5455
_delete(): Promise<void> {
5556
return Promise.resolve();
5657
}
58+
59+
set options(optionsToSet: AIOptions) {
60+
this.options = optionsToSet;
61+
}
62+
63+
get options(): AIOptions {
64+
return this.options;
65+
}
5766
}

0 commit comments

Comments
 (0)