@firebase/ai 2.5.0-canary.63167c68a → 2.5.0

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.
@@ -8,7 +8,7 @@ var util = require('@firebase/util');
8
8
  var logger$1 = require('@firebase/logger');
9
9
 
10
10
  var name = "@firebase/ai";
11
- var version = "2.5.0-canary.63167c68a";
11
+ var version = "2.5.0";
12
12
 
13
13
  /**
14
14
  * @license
@@ -38,62 +38,6 @@ const DEFAULT_FETCH_TIMEOUT_MS = 180 * 1000;
38
38
  */
39
39
  const DEFAULT_HYBRID_IN_CLOUD_MODEL = 'gemini-2.0-flash-lite';
40
40
 
41
- /**
42
- * @license
43
- * Copyright 2024 Google LLC
44
- *
45
- * Licensed under the Apache License, Version 2.0 (the "License");
46
- * you may not use this file except in compliance with the License.
47
- * You may obtain a copy of the License at
48
- *
49
- * http://www.apache.org/licenses/LICENSE-2.0
50
- *
51
- * Unless required by applicable law or agreed to in writing, software
52
- * distributed under the License is distributed on an "AS IS" BASIS,
53
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
54
- * See the License for the specific language governing permissions and
55
- * limitations under the License.
56
- */
57
- /**
58
- * Error class for the Firebase AI SDK.
59
- *
60
- * @public
61
- */
62
- class AIError extends util.FirebaseError {
63
- /**
64
- * Constructs a new instance of the `AIError` class.
65
- *
66
- * @param code - The error code from {@link (AIErrorCode:type)}.
67
- * @param message - A human-readable message describing the error.
68
- * @param customErrorData - Optional error data.
69
- */
70
- constructor(code, message, customErrorData) {
71
- // Match error format used by FirebaseError from ErrorFactory
72
- const service = AI_TYPE;
73
- const fullCode = `${service}/${code}`;
74
- const fullMessage = `${service}: ${message} (${fullCode})`;
75
- super(code, fullMessage);
76
- this.code = code;
77
- this.customErrorData = customErrorData;
78
- // FirebaseError initializes a stack trace, but it assumes the error is created from the error
79
- // factory. Since we break this assumption, we set the stack trace to be originating from this
80
- // constructor.
81
- // This is only supported in V8.
82
- if (Error.captureStackTrace) {
83
- // Allows us to initialize the stack trace without including the constructor itself at the
84
- // top level of the stack trace.
85
- Error.captureStackTrace(this, AIError);
86
- }
87
- // Allows instanceof AIError in ES5/ES6
88
- // https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
89
- // TODO(dlarocque): Replace this with `new.target`: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#support-for-newtarget
90
- // which we can now use since we no longer target ES5.
91
- Object.setPrototypeOf(this, AIError.prototype);
92
- // Since Error is an interface, we don't inherit toString and so we define it ourselves.
93
- this.toString = () => fullMessage;
94
- }
95
- }
96
-
97
41
  /**
98
42
  * @license
99
43
  * Copyright 2024 Google LLC
@@ -798,64 +742,6 @@ class VertexAIBackend extends Backend {
798
742
  }
799
743
  }
800
744
 
801
- /**
802
- * @license
803
- * Copyright 2025 Google LLC
804
- *
805
- * Licensed under the Apache License, Version 2.0 (the "License");
806
- * you may not use this file except in compliance with the License.
807
- * You may obtain a copy of the License at
808
- *
809
- * http://www.apache.org/licenses/LICENSE-2.0
810
- *
811
- * Unless required by applicable law or agreed to in writing, software
812
- * distributed under the License is distributed on an "AS IS" BASIS,
813
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
814
- * See the License for the specific language governing permissions and
815
- * limitations under the License.
816
- */
817
- /**
818
- * Encodes a {@link Backend} into a string that will be used to uniquely identify {@link AI}
819
- * instances by backend type.
820
- *
821
- * @internal
822
- */
823
- function encodeInstanceIdentifier(backend) {
824
- if (backend instanceof GoogleAIBackend) {
825
- return `${AI_TYPE}/googleai`;
826
- }
827
- else if (backend instanceof VertexAIBackend) {
828
- return `${AI_TYPE}/vertexai/${backend.location}`;
829
- }
830
- else {
831
- throw new AIError(AIErrorCode.ERROR, `Invalid backend: ${JSON.stringify(backend.backendType)}`);
832
- }
833
- }
834
- /**
835
- * Decodes an instance identifier string into a {@link Backend}.
836
- *
837
- * @internal
838
- */
839
- function decodeInstanceIdentifier(instanceIdentifier) {
840
- const identifierParts = instanceIdentifier.split('/');
841
- if (identifierParts[0] !== AI_TYPE) {
842
- throw new AIError(AIErrorCode.ERROR, `Invalid instance identifier, unknown prefix '${identifierParts[0]}'`);
843
- }
844
- const backendType = identifierParts[1];
845
- switch (backendType) {
846
- case 'vertexai':
847
- const location = identifierParts[2];
848
- if (!location) {
849
- throw new AIError(AIErrorCode.ERROR, `Invalid instance identifier, unknown location '${instanceIdentifier}'`);
850
- }
851
- return new VertexAIBackend(location);
852
- case 'googleai':
853
- return new GoogleAIBackend();
854
- default:
855
- throw new AIError(AIErrorCode.ERROR, `Invalid instance identifier string: '${instanceIdentifier}'`);
856
- }
857
- }
858
-
859
745
  /**
860
746
  * @license
861
747
  * Copyright 2024 Google LLC
@@ -899,6 +785,62 @@ class AIService {
899
785
  }
900
786
  }
901
787
 
788
+ /**
789
+ * @license
790
+ * Copyright 2024 Google LLC
791
+ *
792
+ * Licensed under the Apache License, Version 2.0 (the "License");
793
+ * you may not use this file except in compliance with the License.
794
+ * You may obtain a copy of the License at
795
+ *
796
+ * http://www.apache.org/licenses/LICENSE-2.0
797
+ *
798
+ * Unless required by applicable law or agreed to in writing, software
799
+ * distributed under the License is distributed on an "AS IS" BASIS,
800
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
801
+ * See the License for the specific language governing permissions and
802
+ * limitations under the License.
803
+ */
804
+ /**
805
+ * Error class for the Firebase AI SDK.
806
+ *
807
+ * @public
808
+ */
809
+ class AIError extends util.FirebaseError {
810
+ /**
811
+ * Constructs a new instance of the `AIError` class.
812
+ *
813
+ * @param code - The error code from {@link (AIErrorCode:type)}.
814
+ * @param message - A human-readable message describing the error.
815
+ * @param customErrorData - Optional error data.
816
+ */
817
+ constructor(code, message, customErrorData) {
818
+ // Match error format used by FirebaseError from ErrorFactory
819
+ const service = AI_TYPE;
820
+ const fullCode = `${service}/${code}`;
821
+ const fullMessage = `${service}: ${message} (${fullCode})`;
822
+ super(code, fullMessage);
823
+ this.code = code;
824
+ this.customErrorData = customErrorData;
825
+ // FirebaseError initializes a stack trace, but it assumes the error is created from the error
826
+ // factory. Since we break this assumption, we set the stack trace to be originating from this
827
+ // constructor.
828
+ // This is only supported in V8.
829
+ if (Error.captureStackTrace) {
830
+ // Allows us to initialize the stack trace without including the constructor itself at the
831
+ // top level of the stack trace.
832
+ Error.captureStackTrace(this, AIError);
833
+ }
834
+ // Allows instanceof AIError in ES5/ES6
835
+ // https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
836
+ // TODO(dlarocque): Replace this with `new.target`: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#support-for-newtarget
837
+ // which we can now use since we no longer target ES5.
838
+ Object.setPrototypeOf(this, AIError.prototype);
839
+ // Since Error is an interface, we don't inherit toString and so we define it ourselves.
840
+ this.toString = () => fullMessage;
841
+ }
842
+ }
843
+
902
844
  /**
903
845
  * @license
904
846
  * Copyright 2025 Google LLC
@@ -915,16 +857,46 @@ class AIService {
915
857
  * See the License for the specific language governing permissions and
916
858
  * limitations under the License.
917
859
  */
918
- function factory(container, { instanceIdentifier }) {
919
- if (!instanceIdentifier) {
920
- throw new AIError(AIErrorCode.ERROR, 'AIService instance identifier is undefined.');
860
+ /**
861
+ * Encodes a {@link Backend} into a string that will be used to uniquely identify {@link AI}
862
+ * instances by backend type.
863
+ *
864
+ * @internal
865
+ */
866
+ function encodeInstanceIdentifier(backend) {
867
+ if (backend instanceof GoogleAIBackend) {
868
+ return `${AI_TYPE}/googleai`;
869
+ }
870
+ else if (backend instanceof VertexAIBackend) {
871
+ return `${AI_TYPE}/vertexai/${backend.location}`;
872
+ }
873
+ else {
874
+ throw new AIError(AIErrorCode.ERROR, `Invalid backend: ${JSON.stringify(backend.backendType)}`);
875
+ }
876
+ }
877
+ /**
878
+ * Decodes an instance identifier string into a {@link Backend}.
879
+ *
880
+ * @internal
881
+ */
882
+ function decodeInstanceIdentifier(instanceIdentifier) {
883
+ const identifierParts = instanceIdentifier.split('/');
884
+ if (identifierParts[0] !== AI_TYPE) {
885
+ throw new AIError(AIErrorCode.ERROR, `Invalid instance identifier, unknown prefix '${identifierParts[0]}'`);
886
+ }
887
+ const backendType = identifierParts[1];
888
+ switch (backendType) {
889
+ case 'vertexai':
890
+ const location = identifierParts[2];
891
+ if (!location) {
892
+ throw new AIError(AIErrorCode.ERROR, `Invalid instance identifier, unknown location '${instanceIdentifier}'`);
893
+ }
894
+ return new VertexAIBackend(location);
895
+ case 'googleai':
896
+ return new GoogleAIBackend();
897
+ default:
898
+ throw new AIError(AIErrorCode.ERROR, `Invalid instance identifier string: '${instanceIdentifier}'`);
921
899
  }
922
- const backend = decodeInstanceIdentifier(instanceIdentifier);
923
- // getImmediate for FirebaseApp will always succeed
924
- const app = container.getProvider('app').getImmediate();
925
- const auth = container.getProvider('auth-internal');
926
- const appCheckProvider = container.getProvider('app-check-internal');
927
- return new AIService(app, backend, auth, appCheckProvider);
928
900
  }
929
901
 
930
902
  /**
@@ -3932,7 +3904,17 @@ function getLiveGenerativeModel(ai, modelParams) {
3932
3904
  * @packageDocumentation
3933
3905
  */
3934
3906
  function registerAI() {
3935
- app._registerComponent(new component.Component(AI_TYPE, factory, "PUBLIC" /* ComponentType.PUBLIC */).setMultipleInstances(true));
3907
+ app._registerComponent(new component.Component(AI_TYPE, (container, { instanceIdentifier }) => {
3908
+ if (!instanceIdentifier) {
3909
+ throw new AIError(AIErrorCode.ERROR, 'AIService instance identifier is undefined.');
3910
+ }
3911
+ const backend = decodeInstanceIdentifier(instanceIdentifier);
3912
+ // getImmediate for FirebaseApp will always succeed
3913
+ const app = container.getProvider('app').getImmediate();
3914
+ const auth = container.getProvider('auth-internal');
3915
+ const appCheckProvider = container.getProvider('app-check-internal');
3916
+ return new AIService(app, backend, auth, appCheckProvider);
3917
+ }, "PUBLIC" /* ComponentType.PUBLIC */).setMultipleInstances(true));
3936
3918
  app.registerVersion(name, version, 'node');
3937
3919
  // BUILD_TARGET will be replaced by values like esm, cjs, etc during the compilation
3938
3920
  app.registerVersion(name, version, 'cjs2020');