@firebase/ai 2.4.0-canary.b7e18d0ff → 2.4.0-canary.c8263c471
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai-public.d.ts +23 -0
- package/dist/ai.d.ts +23 -0
- package/dist/esm/index.esm.js +54 -23
- package/dist/esm/index.esm.js.map +1 -1
- package/dist/esm/src/requests/hybrid-helpers.d.ts +7 -2
- package/dist/esm/src/requests/response-helpers.d.ts +2 -2
- package/dist/esm/src/requests/stream-reader.d.ts +2 -1
- package/dist/esm/src/types/enums.d.ts +15 -0
- package/dist/esm/src/types/responses.d.ts +7 -1
- package/dist/index.cjs.js +54 -22
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.node.cjs.js +54 -22
- package/dist/index.node.cjs.js.map +1 -1
- package/dist/index.node.mjs +54 -23
- package/dist/index.node.mjs.map +1 -1
- package/dist/src/requests/hybrid-helpers.d.ts +7 -2
- package/dist/src/requests/response-helpers.d.ts +2 -2
- package/dist/src/requests/stream-reader.d.ts +2 -1
- package/dist/src/types/enums.d.ts +15 -0
- package/dist/src/types/responses.d.ts +7 -1
- package/package.json +8 -8
package/dist/ai-public.d.ts
CHANGED
|
@@ -558,6 +558,12 @@ export declare interface EnhancedGenerateContentResponse extends GenerateContent
|
|
|
558
558
|
* set to `true`.
|
|
559
559
|
*/
|
|
560
560
|
thoughtSummary: () => string | undefined;
|
|
561
|
+
/**
|
|
562
|
+
* Indicates whether inference happened on-device or in-cloud.
|
|
563
|
+
*
|
|
564
|
+
* @beta
|
|
565
|
+
*/
|
|
566
|
+
inferenceSource?: InferenceSource;
|
|
561
567
|
}
|
|
562
568
|
|
|
563
569
|
/**
|
|
@@ -1833,6 +1839,23 @@ export declare const InferenceMode: {
|
|
|
1833
1839
|
*/
|
|
1834
1840
|
export declare type InferenceMode = (typeof InferenceMode)[keyof typeof InferenceMode];
|
|
1835
1841
|
|
|
1842
|
+
/**
|
|
1843
|
+
* Indicates whether inference happened on-device or in-cloud.
|
|
1844
|
+
*
|
|
1845
|
+
* @beta
|
|
1846
|
+
*/
|
|
1847
|
+
export declare const InferenceSource: {
|
|
1848
|
+
readonly ON_DEVICE: "on_device";
|
|
1849
|
+
readonly IN_CLOUD: "in_cloud";
|
|
1850
|
+
};
|
|
1851
|
+
|
|
1852
|
+
/**
|
|
1853
|
+
* Indicates whether inference happened on-device or in-cloud.
|
|
1854
|
+
*
|
|
1855
|
+
* @beta
|
|
1856
|
+
*/
|
|
1857
|
+
export declare type InferenceSource = (typeof InferenceSource)[keyof typeof InferenceSource];
|
|
1858
|
+
|
|
1836
1859
|
/**
|
|
1837
1860
|
* Content part interface if the part represents an image.
|
|
1838
1861
|
* @public
|
package/dist/ai.d.ts
CHANGED
|
@@ -604,6 +604,12 @@ export declare interface EnhancedGenerateContentResponse extends GenerateContent
|
|
|
604
604
|
* set to `true`.
|
|
605
605
|
*/
|
|
606
606
|
thoughtSummary: () => string | undefined;
|
|
607
|
+
/**
|
|
608
|
+
* Indicates whether inference happened on-device or in-cloud.
|
|
609
|
+
*
|
|
610
|
+
* @beta
|
|
611
|
+
*/
|
|
612
|
+
inferenceSource?: InferenceSource;
|
|
607
613
|
}
|
|
608
614
|
|
|
609
615
|
/**
|
|
@@ -1945,6 +1951,23 @@ export declare const InferenceMode: {
|
|
|
1945
1951
|
*/
|
|
1946
1952
|
export declare type InferenceMode = (typeof InferenceMode)[keyof typeof InferenceMode];
|
|
1947
1953
|
|
|
1954
|
+
/**
|
|
1955
|
+
* Indicates whether inference happened on-device or in-cloud.
|
|
1956
|
+
*
|
|
1957
|
+
* @beta
|
|
1958
|
+
*/
|
|
1959
|
+
export declare const InferenceSource: {
|
|
1960
|
+
readonly ON_DEVICE: "on_device";
|
|
1961
|
+
readonly IN_CLOUD: "in_cloud";
|
|
1962
|
+
};
|
|
1963
|
+
|
|
1964
|
+
/**
|
|
1965
|
+
* Indicates whether inference happened on-device or in-cloud.
|
|
1966
|
+
*
|
|
1967
|
+
* @beta
|
|
1968
|
+
*/
|
|
1969
|
+
export declare type InferenceSource = (typeof InferenceSource)[keyof typeof InferenceSource];
|
|
1970
|
+
|
|
1948
1971
|
/**
|
|
1949
1972
|
* Content part interface if the part represents an image.
|
|
1950
1973
|
* @public
|
package/dist/esm/index.esm.js
CHANGED
|
@@ -4,7 +4,7 @@ import { FirebaseError, Deferred, getModularInstance } from '@firebase/util';
|
|
|
4
4
|
import { Logger } from '@firebase/logger';
|
|
5
5
|
|
|
6
6
|
var name = "@firebase/ai";
|
|
7
|
-
var version = "2.4.0-canary.
|
|
7
|
+
var version = "2.4.0-canary.c8263c471";
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* @license
|
|
@@ -379,6 +379,15 @@ const InferenceMode = {
|
|
|
379
379
|
'ONLY_IN_CLOUD': 'only_in_cloud',
|
|
380
380
|
'PREFER_IN_CLOUD': 'prefer_in_cloud'
|
|
381
381
|
};
|
|
382
|
+
/**
|
|
383
|
+
* Indicates whether inference happened on-device or in-cloud.
|
|
384
|
+
*
|
|
385
|
+
* @beta
|
|
386
|
+
*/
|
|
387
|
+
const InferenceSource = {
|
|
388
|
+
'ON_DEVICE': 'on_device',
|
|
389
|
+
'IN_CLOUD': 'in_cloud'
|
|
390
|
+
};
|
|
382
391
|
/**
|
|
383
392
|
* Represents the result of the code execution.
|
|
384
393
|
*
|
|
@@ -1592,7 +1601,7 @@ function hasValidCandidates(response) {
|
|
|
1592
1601
|
* Creates an EnhancedGenerateContentResponse object that has helper functions and
|
|
1593
1602
|
* other modifications that improve usability.
|
|
1594
1603
|
*/
|
|
1595
|
-
function createEnhancedContentResponse(response) {
|
|
1604
|
+
function createEnhancedContentResponse(response, inferenceSource = InferenceSource.IN_CLOUD) {
|
|
1596
1605
|
/**
|
|
1597
1606
|
* The Vertex AI backend omits default values.
|
|
1598
1607
|
* This causes the `index` property to be omitted from the first candidate in the
|
|
@@ -1603,6 +1612,7 @@ function createEnhancedContentResponse(response) {
|
|
|
1603
1612
|
response.candidates[0].index = 0;
|
|
1604
1613
|
}
|
|
1605
1614
|
const responseWithHelpers = addHelpers(response);
|
|
1615
|
+
responseWithHelpers.inferenceSource = inferenceSource;
|
|
1606
1616
|
return responseWithHelpers;
|
|
1607
1617
|
}
|
|
1608
1618
|
/**
|
|
@@ -1979,16 +1989,16 @@ const responseLineRE = /^data\: (.*)(?:\n\n|\r\r|\r\n\r\n)/;
|
|
|
1979
1989
|
*
|
|
1980
1990
|
* @param response - Response from a fetch call
|
|
1981
1991
|
*/
|
|
1982
|
-
function processStream(response, apiSettings) {
|
|
1992
|
+
function processStream(response, apiSettings, inferenceSource) {
|
|
1983
1993
|
const inputStream = response.body.pipeThrough(new TextDecoderStream('utf8', { fatal: true }));
|
|
1984
1994
|
const responseStream = getResponseStream(inputStream);
|
|
1985
1995
|
const [stream1, stream2] = responseStream.tee();
|
|
1986
1996
|
return {
|
|
1987
|
-
stream: generateResponseSequence(stream1, apiSettings),
|
|
1988
|
-
response: getResponsePromise(stream2, apiSettings)
|
|
1997
|
+
stream: generateResponseSequence(stream1, apiSettings, inferenceSource),
|
|
1998
|
+
response: getResponsePromise(stream2, apiSettings, inferenceSource)
|
|
1989
1999
|
};
|
|
1990
2000
|
}
|
|
1991
|
-
async function getResponsePromise(stream, apiSettings) {
|
|
2001
|
+
async function getResponsePromise(stream, apiSettings, inferenceSource) {
|
|
1992
2002
|
const allResponses = [];
|
|
1993
2003
|
const reader = stream.getReader();
|
|
1994
2004
|
while (true) {
|
|
@@ -1998,12 +2008,12 @@ async function getResponsePromise(stream, apiSettings) {
|
|
|
1998
2008
|
if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
|
|
1999
2009
|
generateContentResponse = mapGenerateContentResponse(generateContentResponse);
|
|
2000
2010
|
}
|
|
2001
|
-
return createEnhancedContentResponse(generateContentResponse);
|
|
2011
|
+
return createEnhancedContentResponse(generateContentResponse, inferenceSource);
|
|
2002
2012
|
}
|
|
2003
2013
|
allResponses.push(value);
|
|
2004
2014
|
}
|
|
2005
2015
|
}
|
|
2006
|
-
async function* generateResponseSequence(stream, apiSettings) {
|
|
2016
|
+
async function* generateResponseSequence(stream, apiSettings, inferenceSource) {
|
|
2007
2017
|
const reader = stream.getReader();
|
|
2008
2018
|
while (true) {
|
|
2009
2019
|
const { value, done } = await reader.read();
|
|
@@ -2012,10 +2022,10 @@ async function* generateResponseSequence(stream, apiSettings) {
|
|
|
2012
2022
|
}
|
|
2013
2023
|
let enhancedResponse;
|
|
2014
2024
|
if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
|
|
2015
|
-
enhancedResponse = createEnhancedContentResponse(mapGenerateContentResponse(value));
|
|
2025
|
+
enhancedResponse = createEnhancedContentResponse(mapGenerateContentResponse(value), inferenceSource);
|
|
2016
2026
|
}
|
|
2017
2027
|
else {
|
|
2018
|
-
enhancedResponse = createEnhancedContentResponse(value);
|
|
2028
|
+
enhancedResponse = createEnhancedContentResponse(value, inferenceSource);
|
|
2019
2029
|
}
|
|
2020
2030
|
const firstCandidate = enhancedResponse.candidates?.[0];
|
|
2021
2031
|
// Don't yield a response with no useful data for the developer.
|
|
@@ -2185,31 +2195,52 @@ const errorsCausingFallback = [
|
|
|
2185
2195
|
*/
|
|
2186
2196
|
async function callCloudOrDevice(request, chromeAdapter, onDeviceCall, inCloudCall) {
|
|
2187
2197
|
if (!chromeAdapter) {
|
|
2188
|
-
return
|
|
2198
|
+
return {
|
|
2199
|
+
response: await inCloudCall(),
|
|
2200
|
+
inferenceSource: InferenceSource.IN_CLOUD
|
|
2201
|
+
};
|
|
2189
2202
|
}
|
|
2190
2203
|
switch (chromeAdapter.mode) {
|
|
2191
2204
|
case InferenceMode.ONLY_ON_DEVICE:
|
|
2192
2205
|
if (await chromeAdapter.isAvailable(request)) {
|
|
2193
|
-
return
|
|
2206
|
+
return {
|
|
2207
|
+
response: await onDeviceCall(),
|
|
2208
|
+
inferenceSource: InferenceSource.ON_DEVICE
|
|
2209
|
+
};
|
|
2194
2210
|
}
|
|
2195
2211
|
throw new AIError(AIErrorCode.UNSUPPORTED, 'Inference mode is ONLY_ON_DEVICE, but an on-device model is not available.');
|
|
2196
2212
|
case InferenceMode.ONLY_IN_CLOUD:
|
|
2197
|
-
return
|
|
2213
|
+
return {
|
|
2214
|
+
response: await inCloudCall(),
|
|
2215
|
+
inferenceSource: InferenceSource.IN_CLOUD
|
|
2216
|
+
};
|
|
2198
2217
|
case InferenceMode.PREFER_IN_CLOUD:
|
|
2199
2218
|
try {
|
|
2200
|
-
return
|
|
2219
|
+
return {
|
|
2220
|
+
response: await inCloudCall(),
|
|
2221
|
+
inferenceSource: InferenceSource.IN_CLOUD
|
|
2222
|
+
};
|
|
2201
2223
|
}
|
|
2202
2224
|
catch (e) {
|
|
2203
2225
|
if (e instanceof AIError && errorsCausingFallback.includes(e.code)) {
|
|
2204
|
-
return
|
|
2226
|
+
return {
|
|
2227
|
+
response: await onDeviceCall(),
|
|
2228
|
+
inferenceSource: InferenceSource.ON_DEVICE
|
|
2229
|
+
};
|
|
2205
2230
|
}
|
|
2206
2231
|
throw e;
|
|
2207
2232
|
}
|
|
2208
2233
|
case InferenceMode.PREFER_ON_DEVICE:
|
|
2209
2234
|
if (await chromeAdapter.isAvailable(request)) {
|
|
2210
|
-
return
|
|
2235
|
+
return {
|
|
2236
|
+
response: await onDeviceCall(),
|
|
2237
|
+
inferenceSource: InferenceSource.ON_DEVICE
|
|
2238
|
+
};
|
|
2211
2239
|
}
|
|
2212
|
-
return
|
|
2240
|
+
return {
|
|
2241
|
+
response: await inCloudCall(),
|
|
2242
|
+
inferenceSource: InferenceSource.IN_CLOUD
|
|
2243
|
+
};
|
|
2213
2244
|
default:
|
|
2214
2245
|
throw new AIError(AIErrorCode.ERROR, `Unexpected infererence mode: ${chromeAdapter.mode}`);
|
|
2215
2246
|
}
|
|
@@ -2239,8 +2270,8 @@ async function generateContentStreamOnCloud(apiSettings, model, params, requestO
|
|
|
2239
2270
|
/* stream */ true, JSON.stringify(params), requestOptions);
|
|
2240
2271
|
}
|
|
2241
2272
|
async function generateContentStream(apiSettings, model, params, chromeAdapter, requestOptions) {
|
|
2242
|
-
const
|
|
2243
|
-
return processStream(response, apiSettings); // TODO: Map streaming responses
|
|
2273
|
+
const callResult = await callCloudOrDevice(params, chromeAdapter, () => chromeAdapter.generateContentStream(params), () => generateContentStreamOnCloud(apiSettings, model, params, requestOptions));
|
|
2274
|
+
return processStream(callResult.response, apiSettings); // TODO: Map streaming responses
|
|
2244
2275
|
}
|
|
2245
2276
|
async function generateContentOnCloud(apiSettings, model, params, requestOptions) {
|
|
2246
2277
|
if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
|
|
@@ -2250,9 +2281,9 @@ async function generateContentOnCloud(apiSettings, model, params, requestOptions
|
|
|
2250
2281
|
/* stream */ false, JSON.stringify(params), requestOptions);
|
|
2251
2282
|
}
|
|
2252
2283
|
async function generateContent(apiSettings, model, params, chromeAdapter, requestOptions) {
|
|
2253
|
-
const
|
|
2254
|
-
const generateContentResponse = await processGenerateContentResponse(response, apiSettings);
|
|
2255
|
-
const enhancedResponse = createEnhancedContentResponse(generateContentResponse);
|
|
2284
|
+
const callResult = await callCloudOrDevice(params, chromeAdapter, () => chromeAdapter.generateContent(params), () => generateContentOnCloud(apiSettings, model, params, requestOptions));
|
|
2285
|
+
const generateContentResponse = await processGenerateContentResponse(callResult.response, apiSettings);
|
|
2286
|
+
const enhancedResponse = createEnhancedContentResponse(generateContentResponse, callResult.inferenceSource);
|
|
2256
2287
|
return {
|
|
2257
2288
|
response: enhancedResponse
|
|
2258
2289
|
};
|
|
@@ -4109,5 +4140,5 @@ function registerAI() {
|
|
|
4109
4140
|
}
|
|
4110
4141
|
registerAI();
|
|
4111
4142
|
|
|
4112
|
-
export { AIError, AIErrorCode, AIModel, AnyOfSchema, ArraySchema, Backend, BackendType, BlockReason, BooleanSchema, ChatSession, FinishReason, FunctionCallingMode, GenerativeModel, GoogleAIBackend, HarmBlockMethod, HarmBlockThreshold, HarmCategory, HarmProbability, HarmSeverity, ImagenAspectRatio, ImagenImageFormat, ImagenModel, ImagenPersonFilterLevel, ImagenSafetyFilterLevel, InferenceMode, IntegerSchema, Language, LiveGenerativeModel, LiveResponseType, LiveSession, Modality, NumberSchema, ObjectSchema, Outcome, POSSIBLE_ROLES, ResponseModality, Schema, SchemaType, StringSchema, URLRetrievalStatus, VertexAIBackend, getAI, getGenerativeModel, getImagenModel, getLiveGenerativeModel, startAudioConversation };
|
|
4143
|
+
export { AIError, AIErrorCode, AIModel, AnyOfSchema, ArraySchema, Backend, BackendType, BlockReason, BooleanSchema, ChatSession, FinishReason, FunctionCallingMode, GenerativeModel, GoogleAIBackend, HarmBlockMethod, HarmBlockThreshold, HarmCategory, HarmProbability, HarmSeverity, ImagenAspectRatio, ImagenImageFormat, ImagenModel, ImagenPersonFilterLevel, ImagenSafetyFilterLevel, InferenceMode, InferenceSource, IntegerSchema, Language, LiveGenerativeModel, LiveResponseType, LiveSession, Modality, NumberSchema, ObjectSchema, Outcome, POSSIBLE_ROLES, ResponseModality, Schema, SchemaType, StringSchema, URLRetrievalStatus, VertexAIBackend, getAI, getGenerativeModel, getImagenModel, getLiveGenerativeModel, startAudioConversation };
|
|
4113
4144
|
//# sourceMappingURL=index.esm.js.map
|