@langchain/google-common 0.2.17 → 1.0.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.
Files changed (169) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +6 -6
  3. package/dist/_virtual/rolldown_runtime.cjs +25 -0
  4. package/dist/auth.cjs +82 -116
  5. package/dist/auth.cjs.map +1 -0
  6. package/dist/auth.d.cts +46 -0
  7. package/dist/auth.d.cts.map +1 -0
  8. package/dist/auth.d.ts +41 -36
  9. package/dist/auth.d.ts.map +1 -0
  10. package/dist/auth.js +80 -110
  11. package/dist/auth.js.map +1 -0
  12. package/dist/chat_models.cjs +251 -466
  13. package/dist/chat_models.cjs.map +1 -0
  14. package/dist/chat_models.d.cts +98 -0
  15. package/dist/chat_models.d.cts.map +1 -0
  16. package/dist/chat_models.d.ts +87 -73
  17. package/dist/chat_models.d.ts.map +1 -0
  18. package/dist/chat_models.js +245 -457
  19. package/dist/chat_models.js.map +1 -0
  20. package/dist/connection.cjs +321 -466
  21. package/dist/connection.cjs.map +1 -0
  22. package/dist/connection.d.cts +109 -0
  23. package/dist/connection.d.cts.map +1 -0
  24. package/dist/connection.d.ts +98 -91
  25. package/dist/connection.d.ts.map +1 -0
  26. package/dist/connection.js +317 -459
  27. package/dist/connection.js.map +1 -0
  28. package/dist/embeddings.cjs +135 -186
  29. package/dist/embeddings.cjs.map +1 -0
  30. package/dist/embeddings.d.cts +44 -0
  31. package/dist/embeddings.d.cts.map +1 -0
  32. package/dist/embeddings.d.ts +38 -32
  33. package/dist/embeddings.d.ts.map +1 -0
  34. package/dist/embeddings.js +133 -181
  35. package/dist/embeddings.js.map +1 -0
  36. package/dist/experimental/media.cjs +380 -482
  37. package/dist/experimental/media.cjs.map +1 -0
  38. package/dist/experimental/media.d.cts +198 -0
  39. package/dist/experimental/media.d.cts.map +1 -0
  40. package/dist/experimental/media.d.ts +190 -202
  41. package/dist/experimental/media.d.ts.map +1 -0
  42. package/dist/experimental/media.js +369 -468
  43. package/dist/experimental/media.js.map +1 -0
  44. package/dist/experimental/utils/media_core.cjs +403 -517
  45. package/dist/experimental/utils/media_core.cjs.map +1 -0
  46. package/dist/experimental/utils/media_core.d.cts +215 -0
  47. package/dist/experimental/utils/media_core.d.cts.map +1 -0
  48. package/dist/experimental/utils/media_core.d.ts +171 -165
  49. package/dist/experimental/utils/media_core.d.ts.map +1 -0
  50. package/dist/experimental/utils/media_core.js +395 -506
  51. package/dist/experimental/utils/media_core.js.map +1 -0
  52. package/dist/index.cjs +58 -27
  53. package/dist/index.d.cts +13 -0
  54. package/dist/index.d.ts +13 -11
  55. package/dist/index.js +13 -11
  56. package/dist/llms.cjs +157 -244
  57. package/dist/llms.cjs.map +1 -0
  58. package/dist/llms.d.cts +72 -0
  59. package/dist/llms.d.cts.map +1 -0
  60. package/dist/llms.d.ts +64 -54
  61. package/dist/llms.d.ts.map +1 -0
  62. package/dist/llms.js +154 -238
  63. package/dist/llms.js.map +1 -0
  64. package/dist/output_parsers.cjs +148 -173
  65. package/dist/output_parsers.cjs.map +1 -0
  66. package/dist/output_parsers.d.cts +53 -0
  67. package/dist/output_parsers.d.cts.map +1 -0
  68. package/dist/output_parsers.d.ts +46 -42
  69. package/dist/output_parsers.d.ts.map +1 -0
  70. package/dist/output_parsers.js +146 -168
  71. package/dist/output_parsers.js.map +1 -0
  72. package/dist/types-anthropic.d.cts +229 -0
  73. package/dist/types-anthropic.d.cts.map +1 -0
  74. package/dist/types-anthropic.d.ts +221 -215
  75. package/dist/types-anthropic.d.ts.map +1 -0
  76. package/dist/types.cjs +51 -62
  77. package/dist/types.cjs.map +1 -0
  78. package/dist/types.d.cts +748 -0
  79. package/dist/types.d.cts.map +1 -0
  80. package/dist/types.d.ts +669 -656
  81. package/dist/types.d.ts.map +1 -0
  82. package/dist/types.js +46 -45
  83. package/dist/types.js.map +1 -0
  84. package/dist/utils/anthropic.cjs +598 -821
  85. package/dist/utils/anthropic.cjs.map +1 -0
  86. package/dist/utils/anthropic.js +597 -818
  87. package/dist/utils/anthropic.js.map +1 -0
  88. package/dist/utils/common.cjs +130 -211
  89. package/dist/utils/common.cjs.map +1 -0
  90. package/dist/utils/common.d.cts +13 -0
  91. package/dist/utils/common.d.cts.map +1 -0
  92. package/dist/utils/common.d.ts +12 -7
  93. package/dist/utils/common.d.ts.map +1 -0
  94. package/dist/utils/common.js +128 -207
  95. package/dist/utils/common.js.map +1 -0
  96. package/dist/utils/failed_handler.cjs +28 -30
  97. package/dist/utils/failed_handler.cjs.map +1 -0
  98. package/dist/utils/failed_handler.d.cts +9 -0
  99. package/dist/utils/failed_handler.d.cts.map +1 -0
  100. package/dist/utils/failed_handler.d.ts +8 -2
  101. package/dist/utils/failed_handler.d.ts.map +1 -0
  102. package/dist/utils/failed_handler.js +28 -28
  103. package/dist/utils/failed_handler.js.map +1 -0
  104. package/dist/utils/gemini.cjs +1020 -1488
  105. package/dist/utils/gemini.cjs.map +1 -0
  106. package/dist/utils/gemini.d.cts +51 -0
  107. package/dist/utils/gemini.d.cts.map +1 -0
  108. package/dist/utils/gemini.d.ts +51 -48
  109. package/dist/utils/gemini.d.ts.map +1 -0
  110. package/dist/utils/gemini.js +1015 -1479
  111. package/dist/utils/gemini.js.map +1 -0
  112. package/dist/utils/index.cjs +38 -23
  113. package/dist/utils/index.d.cts +8 -0
  114. package/dist/utils/index.d.ts +8 -7
  115. package/dist/utils/index.js +8 -7
  116. package/dist/utils/palm.d.cts +11 -0
  117. package/dist/utils/palm.d.cts.map +1 -0
  118. package/dist/utils/palm.d.ts +9 -4
  119. package/dist/utils/palm.d.ts.map +1 -0
  120. package/dist/utils/safety.cjs +13 -22
  121. package/dist/utils/safety.cjs.map +1 -0
  122. package/dist/utils/safety.d.cts +12 -0
  123. package/dist/utils/safety.d.cts.map +1 -0
  124. package/dist/utils/safety.d.ts +10 -4
  125. package/dist/utils/safety.d.ts.map +1 -0
  126. package/dist/utils/safety.js +13 -19
  127. package/dist/utils/safety.js.map +1 -0
  128. package/dist/utils/stream.cjs +296 -475
  129. package/dist/utils/stream.cjs.map +1 -0
  130. package/dist/utils/stream.d.cts +165 -0
  131. package/dist/utils/stream.d.cts.map +1 -0
  132. package/dist/utils/stream.d.ts +156 -131
  133. package/dist/utils/stream.d.ts.map +1 -0
  134. package/dist/utils/stream.js +293 -469
  135. package/dist/utils/stream.js.map +1 -0
  136. package/dist/utils/zod_to_gemini_parameters.cjs +43 -81
  137. package/dist/utils/zod_to_gemini_parameters.cjs.map +1 -0
  138. package/dist/utils/zod_to_gemini_parameters.d.cts +22 -0
  139. package/dist/utils/zod_to_gemini_parameters.d.cts.map +1 -0
  140. package/dist/utils/zod_to_gemini_parameters.d.ts +21 -6
  141. package/dist/utils/zod_to_gemini_parameters.d.ts.map +1 -0
  142. package/dist/utils/zod_to_gemini_parameters.js +40 -76
  143. package/dist/utils/zod_to_gemini_parameters.js.map +1 -0
  144. package/package.json +69 -85
  145. package/dist/types-anthropic.cjs +0 -2
  146. package/dist/types-anthropic.js +0 -1
  147. package/dist/utils/anthropic.d.ts +0 -4
  148. package/dist/utils/palm.cjs +0 -2
  149. package/dist/utils/palm.js +0 -1
  150. package/experimental/media.cjs +0 -1
  151. package/experimental/media.d.cts +0 -1
  152. package/experimental/media.d.ts +0 -1
  153. package/experimental/media.js +0 -1
  154. package/experimental/utils/media_core.cjs +0 -1
  155. package/experimental/utils/media_core.d.cts +0 -1
  156. package/experimental/utils/media_core.d.ts +0 -1
  157. package/experimental/utils/media_core.js +0 -1
  158. package/index.cjs +0 -1
  159. package/index.d.cts +0 -1
  160. package/index.d.ts +0 -1
  161. package/index.js +0 -1
  162. package/types.cjs +0 -1
  163. package/types.d.cts +0 -1
  164. package/types.d.ts +0 -1
  165. package/types.js +0 -1
  166. package/utils.cjs +0 -1
  167. package/utils.d.cts +0 -1
  168. package/utils.d.ts +0 -1
  169. package/utils.js +0 -1
package/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # @langchain/google-common
2
+
3
+ ## 1.0.0
4
+
5
+ This release updates the package for compatibility with LangChain v1.0. See the v1.0 [release notes](https://docs.langchain.com/oss/javascript/releases/langchain-v1) for details on what's new.
6
+
7
+ ## 0.2.18
8
+
9
+ ### Patch Changes
10
+
11
+ - 0092a79: compute per-chunk token deltas while streaming
12
+
13
+ ## 0.2.17
14
+
15
+ ### Patch Changes
16
+
17
+ - 4a3f5af: add handling for well known content blocks (#8781)
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
- The MIT License
1
+ MIT License
2
2
 
3
- Copyright (c) 2023 LangChain
3
+ Copyright (c) LangChain, Inc.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
9
  copies of the Software, and to permit persons to whom the Software is
10
10
  furnished to do so, subject to the following conditions:
11
11
 
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
14
 
15
15
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
16
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
17
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,25 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+
23
+ //#endregion
24
+
25
+ exports.__toESM = __toESM;
package/dist/auth.cjs CHANGED
@@ -1,120 +1,86 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiKeyGoogleAuth = exports.GoogleAbstractedFetchClient = void 0;
4
- exports.aiPlatformScope = aiPlatformScope;
5
- exports.ensureAuthOptionScopes = ensureAuthOptionScopes;
6
- const stream_js_1 = require("./utils/stream.cjs");
7
- class GoogleAbstractedFetchClient {
8
- constructor() {
9
- Object.defineProperty(this, "_fetch", {
10
- enumerable: true,
11
- configurable: true,
12
- writable: true,
13
- value: fetch
14
- });
15
- }
16
- async _buildData(res, opts) {
17
- switch (opts.responseType) {
18
- case "json":
19
- return res.json();
20
- case "stream":
21
- return new stream_js_1.ReadableJsonStream(res.body);
22
- default:
23
- return res.blob();
24
- }
25
- }
26
- async _request(url, opts, additionalHeaders) {
27
- if (url == null)
28
- throw new Error("Missing URL");
29
- const fetchOptions = {
30
- method: opts.method,
31
- headers: {
32
- "Content-Type": "application/json",
33
- ...(opts.headers ?? {}),
34
- ...(additionalHeaders ?? {}),
35
- },
36
- };
37
- if (opts.data !== undefined) {
38
- if (typeof opts.data === "string") {
39
- fetchOptions.body = opts.data;
40
- }
41
- else {
42
- fetchOptions.body = JSON.stringify(opts.data);
43
- }
44
- }
45
- const res = await this._fetch(url, fetchOptions);
46
- if (!res.ok) {
47
- const resText = await res.text();
48
- const error = new Error(`Google request failed with status code ${res.status}: ${resText}`);
49
- /* eslint-disable @typescript-eslint/no-explicit-any */
50
- error.response = res;
51
- error.details = {
52
- url,
53
- opts,
54
- fetchOptions,
55
- result: res,
56
- };
57
- /* eslint-enable @typescript-eslint/no-explicit-any */
58
- throw error;
59
- }
60
- const data = await this._buildData(res, opts);
61
- return {
62
- data,
63
- config: {},
64
- status: res.status,
65
- statusText: res.statusText,
66
- headers: res.headers,
67
- request: { responseURL: res.url },
68
- };
69
- }
70
- }
71
- exports.GoogleAbstractedFetchClient = GoogleAbstractedFetchClient;
72
- class ApiKeyGoogleAuth extends GoogleAbstractedFetchClient {
73
- constructor(apiKey) {
74
- super();
75
- Object.defineProperty(this, "apiKey", {
76
- enumerable: true,
77
- configurable: true,
78
- writable: true,
79
- value: void 0
80
- });
81
- this.apiKey = apiKey;
82
- }
83
- get clientType() {
84
- return "apiKey";
85
- }
86
- getProjectId() {
87
- throw new Error("APIs that require a project ID cannot use an API key");
88
- // Perhaps we could implement this if needed:
89
- // https://cloud.google.com/docs/authentication/api-keys#get-info
90
- }
91
- request(opts) {
92
- const authHeader = {
93
- "X-Goog-Api-Key": this.apiKey,
94
- };
95
- return this._request(opts.url, opts, authHeader);
96
- }
97
- }
98
- exports.ApiKeyGoogleAuth = ApiKeyGoogleAuth;
1
+ const require_stream = require('./utils/stream.cjs');
2
+
3
+ //#region src/auth.ts
4
+ var GoogleAbstractedFetchClient = class {
5
+ _fetch = fetch;
6
+ async _buildData(res, opts) {
7
+ switch (opts.responseType) {
8
+ case "json": return res.json();
9
+ case "stream": return new require_stream.ReadableJsonStream(res.body);
10
+ default: return res.blob();
11
+ }
12
+ }
13
+ async _request(url, opts, additionalHeaders) {
14
+ if (url == null) throw new Error("Missing URL");
15
+ const fetchOptions = {
16
+ method: opts.method,
17
+ headers: {
18
+ "Content-Type": "application/json",
19
+ ...opts.headers ?? {},
20
+ ...additionalHeaders ?? {}
21
+ }
22
+ };
23
+ if (opts.data !== void 0) if (typeof opts.data === "string") fetchOptions.body = opts.data;
24
+ else fetchOptions.body = JSON.stringify(opts.data);
25
+ const res = await this._fetch(url, fetchOptions);
26
+ if (!res.ok) {
27
+ const resText = await res.text();
28
+ const error = /* @__PURE__ */ new Error(`Google request failed with status code ${res.status}: ${resText}`);
29
+ error.response = res;
30
+ error.details = {
31
+ url,
32
+ opts,
33
+ fetchOptions,
34
+ result: res
35
+ };
36
+ throw error;
37
+ }
38
+ const data = await this._buildData(res, opts);
39
+ return {
40
+ data,
41
+ config: {},
42
+ status: res.status,
43
+ statusText: res.statusText,
44
+ headers: res.headers,
45
+ request: { responseURL: res.url }
46
+ };
47
+ }
48
+ };
49
+ var ApiKeyGoogleAuth = class extends GoogleAbstractedFetchClient {
50
+ apiKey;
51
+ constructor(apiKey) {
52
+ super();
53
+ this.apiKey = apiKey;
54
+ }
55
+ get clientType() {
56
+ return "apiKey";
57
+ }
58
+ getProjectId() {
59
+ throw new Error("APIs that require a project ID cannot use an API key");
60
+ }
61
+ request(opts) {
62
+ const authHeader = { "X-Goog-Api-Key": this.apiKey };
63
+ return this._request(opts.url, opts, authHeader);
64
+ }
65
+ };
99
66
  function aiPlatformScope(platform) {
100
- switch (platform) {
101
- case "gai":
102
- return ["https://www.googleapis.com/auth/generative-language"];
103
- default:
104
- return ["https://www.googleapis.com/auth/cloud-platform"];
105
- }
67
+ switch (platform) {
68
+ case "gai": return ["https://www.googleapis.com/auth/generative-language"];
69
+ default: return ["https://www.googleapis.com/auth/cloud-platform"];
70
+ }
106
71
  }
107
72
  function ensureAuthOptionScopes(authOption, scopeProperty, scopesOrPlatform) {
108
- // If the property is already set, return it
109
- if (authOption && Object.hasOwn(authOption, scopeProperty)) {
110
- return authOption;
111
- }
112
- // Otherwise add it
113
- const scopes = Array.isArray(scopesOrPlatform)
114
- ? scopesOrPlatform
115
- : aiPlatformScope(scopesOrPlatform ?? "gcp");
116
- return {
117
- [scopeProperty]: scopes,
118
- ...(authOption ?? {}),
119
- };
73
+ if (authOption && Object.hasOwn(authOption, scopeProperty)) return authOption;
74
+ const scopes = Array.isArray(scopesOrPlatform) ? scopesOrPlatform : aiPlatformScope(scopesOrPlatform ?? "gcp");
75
+ return {
76
+ [scopeProperty]: scopes,
77
+ ...authOption ?? {}
78
+ };
120
79
  }
80
+
81
+ //#endregion
82
+ exports.ApiKeyGoogleAuth = ApiKeyGoogleAuth;
83
+ exports.GoogleAbstractedFetchClient = GoogleAbstractedFetchClient;
84
+ exports.aiPlatformScope = aiPlatformScope;
85
+ exports.ensureAuthOptionScopes = ensureAuthOptionScopes;
86
+ //# sourceMappingURL=auth.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.cjs","names":["res: Response","opts: GoogleAbstractedClientOps","ReadableJsonStream","url: string | undefined","additionalHeaders: Record<string, string>","fetchOptions: {\n method?: string;\n headers: Record<string, string>;\n body?: string;\n }","apiKey: string","platform: GooglePlatformType","authOption: AuthOptions | undefined","scopeProperty: string","scopesOrPlatform: string[] | GooglePlatformType | undefined","scopes: string[]"],"sources":["../src/auth.ts"],"sourcesContent":["import { ReadableJsonStream } from \"./utils/stream.js\";\nimport { GooglePlatformType } from \"./types.js\";\n\nexport type GoogleAbstractedClientOpsMethod = \"GET\" | \"POST\" | \"DELETE\";\n\nexport type GoogleAbstractedClientOpsResponseType = \"json\" | \"stream\" | \"blob\";\n\nexport type GoogleAbstractedClientOps = {\n url?: string;\n method?: GoogleAbstractedClientOpsMethod;\n headers?: Record<string, string>;\n data?: unknown;\n responseType?: GoogleAbstractedClientOpsResponseType;\n};\n\nexport interface GoogleAbstractedClient {\n request: (opts: GoogleAbstractedClientOps) => unknown;\n getProjectId: () => Promise<string>;\n get clientType(): string;\n}\n\nexport abstract class GoogleAbstractedFetchClient\n implements GoogleAbstractedClient\n{\n abstract get clientType(): string;\n\n abstract getProjectId(): Promise<string>;\n\n abstract request(opts: GoogleAbstractedClientOps): unknown;\n\n _fetch: typeof fetch = fetch;\n\n async _buildData(res: Response, opts: GoogleAbstractedClientOps) {\n switch (opts.responseType) {\n case \"json\":\n return res.json();\n case \"stream\":\n return new ReadableJsonStream(res.body);\n default:\n return res.blob();\n }\n }\n\n async _request(\n url: string | undefined,\n opts: GoogleAbstractedClientOps,\n additionalHeaders: Record<string, string>\n ) {\n if (url == null) throw new Error(\"Missing URL\");\n const fetchOptions: {\n method?: string;\n headers: Record<string, string>;\n body?: string;\n } = {\n method: opts.method,\n headers: {\n \"Content-Type\": \"application/json\",\n ...(opts.headers ?? {}),\n ...(additionalHeaders ?? {}),\n },\n };\n if (opts.data !== undefined) {\n if (typeof opts.data === \"string\") {\n fetchOptions.body = opts.data;\n } else {\n fetchOptions.body = JSON.stringify(opts.data);\n }\n }\n\n const res = await this._fetch(url, fetchOptions);\n\n if (!res.ok) {\n const resText = await res.text();\n const error = new Error(\n `Google request failed with status code ${res.status}: ${resText}`\n );\n /* eslint-disable @typescript-eslint/no-explicit-any */\n (error as any).response = res;\n (error as any).details = {\n url,\n opts,\n fetchOptions,\n result: res,\n };\n /* eslint-enable @typescript-eslint/no-explicit-any */\n throw error;\n }\n\n const data = await this._buildData(res, opts);\n return {\n data,\n config: {},\n status: res.status,\n statusText: res.statusText,\n headers: res.headers,\n request: { responseURL: res.url },\n };\n }\n}\n\nexport class ApiKeyGoogleAuth extends GoogleAbstractedFetchClient {\n apiKey: string;\n\n constructor(apiKey: string) {\n super();\n this.apiKey = apiKey;\n }\n\n get clientType(): string {\n return \"apiKey\";\n }\n\n getProjectId(): Promise<string> {\n throw new Error(\"APIs that require a project ID cannot use an API key\");\n // Perhaps we could implement this if needed:\n // https://cloud.google.com/docs/authentication/api-keys#get-info\n }\n\n request(opts: GoogleAbstractedClientOps): unknown {\n const authHeader = {\n \"X-Goog-Api-Key\": this.apiKey,\n };\n return this._request(opts.url, opts, authHeader);\n }\n}\n\nexport function aiPlatformScope(platform: GooglePlatformType): string[] {\n switch (platform) {\n case \"gai\":\n return [\"https://www.googleapis.com/auth/generative-language\"];\n default:\n return [\"https://www.googleapis.com/auth/cloud-platform\"];\n }\n}\n\nexport function ensureAuthOptionScopes<AuthOptions>(\n authOption: AuthOptions | undefined,\n scopeProperty: string,\n scopesOrPlatform: string[] | GooglePlatformType | undefined\n): AuthOptions {\n // If the property is already set, return it\n if (authOption && Object.hasOwn(authOption, scopeProperty)) {\n return authOption;\n }\n\n // Otherwise add it\n const scopes: string[] = Array.isArray(scopesOrPlatform)\n ? (scopesOrPlatform as string[])\n : aiPlatformScope(scopesOrPlatform ?? \"gcp\");\n return {\n [scopeProperty]: scopes,\n ...(authOption ?? {}),\n } as AuthOptions;\n}\n"],"mappings":";;;AAqBA,IAAsB,8BAAtB,MAEA;CAOE,SAAuB;CAEvB,MAAM,WAAWA,KAAeC,MAAiC;AAC/D,UAAQ,KAAK,cAAb;GACE,KAAK,OACH,QAAO,IAAI,MAAM;GACnB,KAAK,SACH,QAAO,IAAIC,kCAAmB,IAAI;GACpC,QACE,QAAO,IAAI,MAAM;EACpB;CACF;CAED,MAAM,SACJC,KACAF,MACAG,mBACA;AACA,MAAI,OAAO,KAAM,OAAM,IAAI,MAAM;EACjC,MAAMC,eAIF;GACF,QAAQ,KAAK;GACb,SAAS;IACP,gBAAgB;IAChB,GAAI,KAAK,WAAW,CAAE;IACtB,GAAI,qBAAqB,CAAE;GAC5B;EACF;AACD,MAAI,KAAK,SAAS,OAChB,KAAI,OAAO,KAAK,SAAS,UACvB,aAAa,OAAO,KAAK;OAEzB,aAAa,OAAO,KAAK,UAAU,KAAK,KAAK;EAIjD,MAAM,MAAM,MAAM,KAAK,OAAO,KAAK,aAAa;AAEhD,MAAI,CAAC,IAAI,IAAI;GACX,MAAM,UAAU,MAAM,IAAI,MAAM;GAChC,MAAM,wBAAQ,IAAI,MAChB,CAAC,uCAAuC,EAAE,IAAI,OAAO,EAAE,EAAE,SAAS;GAGnE,MAAc,WAAW;GACzB,MAAc,UAAU;IACvB;IACA;IACA;IACA,QAAQ;GACT;AAED,SAAM;EACP;EAED,MAAM,OAAO,MAAM,KAAK,WAAW,KAAK,KAAK;AAC7C,SAAO;GACL;GACA,QAAQ,CAAE;GACV,QAAQ,IAAI;GACZ,YAAY,IAAI;GAChB,SAAS,IAAI;GACb,SAAS,EAAE,aAAa,IAAI,IAAK;EAClC;CACF;AACF;AAED,IAAa,mBAAb,cAAsC,4BAA4B;CAChE;CAEA,YAAYC,QAAgB;EAC1B,OAAO;EACP,KAAK,SAAS;CACf;CAED,IAAI,aAAqB;AACvB,SAAO;CACR;CAED,eAAgC;AAC9B,QAAM,IAAI,MAAM;CAGjB;CAED,QAAQL,MAA0C;EAChD,MAAM,aAAa,EACjB,kBAAkB,KAAK,OACxB;AACD,SAAO,KAAK,SAAS,KAAK,KAAK,MAAM,WAAW;CACjD;AACF;AAED,SAAgB,gBAAgBM,UAAwC;AACtE,SAAQ,UAAR;EACE,KAAK,MACH,QAAO,CAAC,qDAAsD;EAChE,QACE,QAAO,CAAC,gDAAiD;CAC5D;AACF;AAED,SAAgB,uBACdC,YACAC,eACAC,kBACa;AAEb,KAAI,cAAc,OAAO,OAAO,YAAY,cAAc,CACxD,QAAO;CAIT,MAAMC,SAAmB,MAAM,QAAQ,iBAAiB,GACnD,mBACD,gBAAgB,oBAAoB,MAAM;AAC9C,QAAO;GACJ,gBAAgB;EACjB,GAAI,cAAc,CAAE;CACrB;AACF"}
@@ -0,0 +1,46 @@
1
+ import { GooglePlatformType } from "./types.cjs";
2
+
3
+ //#region src/auth.d.ts
4
+ type GoogleAbstractedClientOpsMethod = "GET" | "POST" | "DELETE";
5
+ type GoogleAbstractedClientOpsResponseType = "json" | "stream" | "blob";
6
+ type GoogleAbstractedClientOps = {
7
+ url?: string;
8
+ method?: GoogleAbstractedClientOpsMethod;
9
+ headers?: Record<string, string>;
10
+ data?: unknown;
11
+ responseType?: GoogleAbstractedClientOpsResponseType;
12
+ };
13
+ interface GoogleAbstractedClient {
14
+ request: (opts: GoogleAbstractedClientOps) => unknown;
15
+ getProjectId: () => Promise<string>;
16
+ get clientType(): string;
17
+ }
18
+ declare abstract class GoogleAbstractedFetchClient implements GoogleAbstractedClient {
19
+ abstract get clientType(): string;
20
+ abstract getProjectId(): Promise<string>;
21
+ abstract request(opts: GoogleAbstractedClientOps): unknown;
22
+ _fetch: typeof fetch;
23
+ _buildData(res: Response, opts: GoogleAbstractedClientOps): Promise<any>;
24
+ _request(url: string | undefined, opts: GoogleAbstractedClientOps, additionalHeaders: Record<string, string>): Promise<{
25
+ data: any;
26
+ config: {};
27
+ status: number;
28
+ statusText: string;
29
+ headers: Headers;
30
+ request: {
31
+ responseURL: string;
32
+ };
33
+ }>;
34
+ }
35
+ declare class ApiKeyGoogleAuth extends GoogleAbstractedFetchClient {
36
+ apiKey: string;
37
+ constructor(apiKey: string);
38
+ get clientType(): string;
39
+ getProjectId(): Promise<string>;
40
+ request(opts: GoogleAbstractedClientOps): unknown;
41
+ }
42
+ declare function aiPlatformScope(platform: GooglePlatformType): string[];
43
+ declare function ensureAuthOptionScopes<AuthOptions>(authOption: AuthOptions | undefined, scopeProperty: string, scopesOrPlatform: string[] | GooglePlatformType | undefined): AuthOptions;
44
+ //#endregion
45
+ export { ApiKeyGoogleAuth, GoogleAbstractedClient, GoogleAbstractedClientOps, GoogleAbstractedClientOpsMethod, GoogleAbstractedClientOpsResponseType, GoogleAbstractedFetchClient, aiPlatformScope, ensureAuthOptionScopes };
46
+ //# sourceMappingURL=auth.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.d.cts","names":["GooglePlatformType","GoogleAbstractedClientOpsMethod","GoogleAbstractedClientOpsResponseType","GoogleAbstractedClientOps","Record","GoogleAbstractedClient","Promise","GoogleAbstractedFetchClient","fetch","Response","Headers","ApiKeyGoogleAuth","aiPlatformScope","ensureAuthOptionScopes","AuthOptions"],"sources":["../src/auth.d.ts"],"sourcesContent":["import { GooglePlatformType } from \"./types.js\";\nexport type GoogleAbstractedClientOpsMethod = \"GET\" | \"POST\" | \"DELETE\";\nexport type GoogleAbstractedClientOpsResponseType = \"json\" | \"stream\" | \"blob\";\nexport type GoogleAbstractedClientOps = {\n url?: string;\n method?: GoogleAbstractedClientOpsMethod;\n headers?: Record<string, string>;\n data?: unknown;\n responseType?: GoogleAbstractedClientOpsResponseType;\n};\nexport interface GoogleAbstractedClient {\n request: (opts: GoogleAbstractedClientOps) => unknown;\n getProjectId: () => Promise<string>;\n get clientType(): string;\n}\nexport declare abstract class GoogleAbstractedFetchClient implements GoogleAbstractedClient {\n abstract get clientType(): string;\n abstract getProjectId(): Promise<string>;\n abstract request(opts: GoogleAbstractedClientOps): unknown;\n _fetch: typeof fetch;\n _buildData(res: Response, opts: GoogleAbstractedClientOps): Promise<any>;\n _request(url: string | undefined, opts: GoogleAbstractedClientOps, additionalHeaders: Record<string, string>): Promise<{\n data: any;\n config: {};\n status: number;\n statusText: string;\n headers: Headers;\n request: {\n responseURL: string;\n };\n }>;\n}\nexport declare class ApiKeyGoogleAuth extends GoogleAbstractedFetchClient {\n apiKey: string;\n constructor(apiKey: string);\n get clientType(): string;\n getProjectId(): Promise<string>;\n request(opts: GoogleAbstractedClientOps): unknown;\n}\nexport declare function aiPlatformScope(platform: GooglePlatformType): string[];\nexport declare function ensureAuthOptionScopes<AuthOptions>(authOption: AuthOptions | undefined, scopeProperty: string, scopesOrPlatform: string[] | GooglePlatformType | undefined): AuthOptions;\n"],"mappings":";;;KACYC,+BAAAA;KACAC,qCAAAA;AADAD,KAEAE,yBAAAA,GAF+B;EAC/BD,GAAAA,CAAAA,EAAAA,MAAAA;EACAC,MAAAA,CAAAA,EAECF,+BAFwB;EAAA,OAAA,CAAA,EAGvBG,MAHuB,CAAA,MAAA,EAAA,MAAA,CAAA;EAAA,IAExBH,CAAAA,EAAAA,OAAAA;EAA+B,YAC9BG,CAAAA,EAEKF,qCAFLE;CAAM;AAEoC,UAEvCC,sBAAAA,CAFuC;EAEvCA,OAAAA,EAAAA,CAAAA,IAAAA,EACGF,yBADmB,EAAA,GAAA,OAAA;EAAA,YAAA,EAAA,GAAA,GAEfG,OAFe,CAAA,MAAA,CAAA;EAAA,IACnBH,UAAAA,EAAAA,EAAAA,MAAAA;;AACW,uBAGDI,2BAAAA,YAAuCF,sBAHtC,CAAA;EAGDE,aAAAA,UAAAA,CAAAA,CAAAA,EAAAA,MAA2B;EAAA,SAAA,YAAA,CAAA,CAAA,EAE5BD,OAF4B,CAAA,MAAA,CAAA;EAAA,SAE5BA,OAAAA,CAAAA,IAAAA,EACFH,yBADEG,CAAAA,EAAAA,OAAAA;EAAO,MACTH,EAAAA,OACRK,KADQL;EAAyB,UACjCK,CAAAA,GAAAA,EACCC,QADDD,EAAAA,IAAAA,EACiBL,yBADjBK,CAAAA,EAC6CF,OAD7CE,CAAAA,GAAAA,CAAAA;EAAK,QACJC,CAAAA,GAAAA,EAAAA,MAAAA,GAAAA,SAAAA,EAAAA,IAAAA,EACwBN,yBADxBM,EAAAA,iBAAAA,EACsEL,MADtEK,CAAAA,MAAAA,EAAAA,MAAAA,CAAAA,CAAAA,EAC+FH,OAD/FG,CAAAA;IAAgBN,IAAAA,EAAAA,GAAAA;IAA4BG,MAAAA,EAAAA,CAAAA,CAAAA;IACpBH,MAAAA,EAAAA,MAAAA;IAA8CC,UAAAA,EAAAA,MAAAA;IAKzEM,OAAAA,EAAAA,OAAAA;IALkGJ,OAAAA,EAAAA;MAN9CD,WAAAA,EAAAA,MAAAA;IAAsB,CAAA;EAiBtEM,CAAAA,CAAAA;;AAIDL,cAJCK,gBAAAA,SAAyBJ,2BAAAA,CAI1BD;EAAO,MACTH,EAAAA,MAAAA;EAAyB,WALGI,CAAAA,MAAAA,EAAAA,MAAAA;EAA2B,IAAA,UAAA,CAAA,CAAA,EAAA,MAAA;EAOjDK,YAAAA,CAAAA,CAAAA,EAHJN,OAGmB,CAAA,MAAWN,CAAAA;EAC1Ba,OAAAA,CAAAA,IAAAA,EAHNV,yBAG4B,CAAA,EAAA,OAAA;;AAA0BW,iBADhDF,eAAAA,CACgDE,QAAAA,EADtBd,kBACsBc,CAAAA,EAAAA,MAAAA,EAAAA;AAA6Ed,iBAA7Ha,sBAA6Hb,CAAAA,WAAAA,CAAAA,CAAAA,UAAAA,EAA7Ec,WAA6Ed,GAAAA,SAAAA,EAAAA,aAAAA,EAAAA,MAAAA,EAAAA,gBAAAA,EAAAA,MAAAA,EAAAA,GAAAA,kBAAAA,GAAAA,SAAAA,CAAAA,EAAiCc,WAAjCd"}
package/dist/auth.d.ts CHANGED
@@ -1,41 +1,46 @@
1
1
  import { GooglePlatformType } from "./types.js";
2
- export type GoogleAbstractedClientOpsMethod = "GET" | "POST" | "DELETE";
3
- export type GoogleAbstractedClientOpsResponseType = "json" | "stream" | "blob";
4
- export type GoogleAbstractedClientOps = {
5
- url?: string;
6
- method?: GoogleAbstractedClientOpsMethod;
7
- headers?: Record<string, string>;
8
- data?: unknown;
9
- responseType?: GoogleAbstractedClientOpsResponseType;
2
+
3
+ //#region src/auth.d.ts
4
+ type GoogleAbstractedClientOpsMethod = "GET" | "POST" | "DELETE";
5
+ type GoogleAbstractedClientOpsResponseType = "json" | "stream" | "blob";
6
+ type GoogleAbstractedClientOps = {
7
+ url?: string;
8
+ method?: GoogleAbstractedClientOpsMethod;
9
+ headers?: Record<string, string>;
10
+ data?: unknown;
11
+ responseType?: GoogleAbstractedClientOpsResponseType;
10
12
  };
11
- export interface GoogleAbstractedClient {
12
- request: (opts: GoogleAbstractedClientOps) => unknown;
13
- getProjectId: () => Promise<string>;
14
- get clientType(): string;
13
+ interface GoogleAbstractedClient {
14
+ request: (opts: GoogleAbstractedClientOps) => unknown;
15
+ getProjectId: () => Promise<string>;
16
+ get clientType(): string;
15
17
  }
16
- export declare abstract class GoogleAbstractedFetchClient implements GoogleAbstractedClient {
17
- abstract get clientType(): string;
18
- abstract getProjectId(): Promise<string>;
19
- abstract request(opts: GoogleAbstractedClientOps): unknown;
20
- _fetch: typeof fetch;
21
- _buildData(res: Response, opts: GoogleAbstractedClientOps): Promise<any>;
22
- _request(url: string | undefined, opts: GoogleAbstractedClientOps, additionalHeaders: Record<string, string>): Promise<{
23
- data: any;
24
- config: {};
25
- status: number;
26
- statusText: string;
27
- headers: Headers;
28
- request: {
29
- responseURL: string;
30
- };
31
- }>;
18
+ declare abstract class GoogleAbstractedFetchClient implements GoogleAbstractedClient {
19
+ abstract get clientType(): string;
20
+ abstract getProjectId(): Promise<string>;
21
+ abstract request(opts: GoogleAbstractedClientOps): unknown;
22
+ _fetch: typeof fetch;
23
+ _buildData(res: Response, opts: GoogleAbstractedClientOps): Promise<any>;
24
+ _request(url: string | undefined, opts: GoogleAbstractedClientOps, additionalHeaders: Record<string, string>): Promise<{
25
+ data: any;
26
+ config: {};
27
+ status: number;
28
+ statusText: string;
29
+ headers: Headers;
30
+ request: {
31
+ responseURL: string;
32
+ };
33
+ }>;
32
34
  }
33
- export declare class ApiKeyGoogleAuth extends GoogleAbstractedFetchClient {
34
- apiKey: string;
35
- constructor(apiKey: string);
36
- get clientType(): string;
37
- getProjectId(): Promise<string>;
38
- request(opts: GoogleAbstractedClientOps): unknown;
35
+ declare class ApiKeyGoogleAuth extends GoogleAbstractedFetchClient {
36
+ apiKey: string;
37
+ constructor(apiKey: string);
38
+ get clientType(): string;
39
+ getProjectId(): Promise<string>;
40
+ request(opts: GoogleAbstractedClientOps): unknown;
39
41
  }
40
- export declare function aiPlatformScope(platform: GooglePlatformType): string[];
41
- export declare function ensureAuthOptionScopes<AuthOptions>(authOption: AuthOptions | undefined, scopeProperty: string, scopesOrPlatform: string[] | GooglePlatformType | undefined): AuthOptions;
42
+ declare function aiPlatformScope(platform: GooglePlatformType): string[];
43
+ declare function ensureAuthOptionScopes<AuthOptions>(authOption: AuthOptions | undefined, scopeProperty: string, scopesOrPlatform: string[] | GooglePlatformType | undefined): AuthOptions;
44
+ //#endregion
45
+ export { ApiKeyGoogleAuth, GoogleAbstractedClient, GoogleAbstractedClientOps, GoogleAbstractedClientOpsMethod, GoogleAbstractedClientOpsResponseType, GoogleAbstractedFetchClient, aiPlatformScope, ensureAuthOptionScopes };
46
+ //# sourceMappingURL=auth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.d.ts","names":["GooglePlatformType","GoogleAbstractedClientOpsMethod","GoogleAbstractedClientOpsResponseType","GoogleAbstractedClientOps","Record","GoogleAbstractedClient","Promise","GoogleAbstractedFetchClient","fetch","Response","Headers","ApiKeyGoogleAuth","aiPlatformScope","ensureAuthOptionScopes","AuthOptions"],"sources":["../src/auth.d.ts"],"sourcesContent":["import { GooglePlatformType } from \"./types.js\";\nexport type GoogleAbstractedClientOpsMethod = \"GET\" | \"POST\" | \"DELETE\";\nexport type GoogleAbstractedClientOpsResponseType = \"json\" | \"stream\" | \"blob\";\nexport type GoogleAbstractedClientOps = {\n url?: string;\n method?: GoogleAbstractedClientOpsMethod;\n headers?: Record<string, string>;\n data?: unknown;\n responseType?: GoogleAbstractedClientOpsResponseType;\n};\nexport interface GoogleAbstractedClient {\n request: (opts: GoogleAbstractedClientOps) => unknown;\n getProjectId: () => Promise<string>;\n get clientType(): string;\n}\nexport declare abstract class GoogleAbstractedFetchClient implements GoogleAbstractedClient {\n abstract get clientType(): string;\n abstract getProjectId(): Promise<string>;\n abstract request(opts: GoogleAbstractedClientOps): unknown;\n _fetch: typeof fetch;\n _buildData(res: Response, opts: GoogleAbstractedClientOps): Promise<any>;\n _request(url: string | undefined, opts: GoogleAbstractedClientOps, additionalHeaders: Record<string, string>): Promise<{\n data: any;\n config: {};\n status: number;\n statusText: string;\n headers: Headers;\n request: {\n responseURL: string;\n };\n }>;\n}\nexport declare class ApiKeyGoogleAuth extends GoogleAbstractedFetchClient {\n apiKey: string;\n constructor(apiKey: string);\n get clientType(): string;\n getProjectId(): Promise<string>;\n request(opts: GoogleAbstractedClientOps): unknown;\n}\nexport declare function aiPlatformScope(platform: GooglePlatformType): string[];\nexport declare function ensureAuthOptionScopes<AuthOptions>(authOption: AuthOptions | undefined, scopeProperty: string, scopesOrPlatform: string[] | GooglePlatformType | undefined): AuthOptions;\n"],"mappings":";;;KACYC,+BAAAA;KACAC,qCAAAA;AADAD,KAEAE,yBAAAA,GAF+B;EAC/BD,GAAAA,CAAAA,EAAAA,MAAAA;EACAC,MAAAA,CAAAA,EAECF,+BAFwB;EAAA,OAAA,CAAA,EAGvBG,MAHuB,CAAA,MAAA,EAAA,MAAA,CAAA;EAAA,IAExBH,CAAAA,EAAAA,OAAAA;EAA+B,YAC9BG,CAAAA,EAEKF,qCAFLE;CAAM;AAEoC,UAEvCC,sBAAAA,CAFuC;EAEvCA,OAAAA,EAAAA,CAAAA,IAAAA,EACGF,yBADmB,EAAA,GAAA,OAAA;EAAA,YAAA,EAAA,GAAA,GAEfG,OAFe,CAAA,MAAA,CAAA;EAAA,IACnBH,UAAAA,EAAAA,EAAAA,MAAAA;;AACW,uBAGDI,2BAAAA,YAAuCF,sBAHtC,CAAA;EAGDE,aAAAA,UAAAA,CAAAA,CAAAA,EAAAA,MAA2B;EAAA,SAAA,YAAA,CAAA,CAAA,EAE5BD,OAF4B,CAAA,MAAA,CAAA;EAAA,SAE5BA,OAAAA,CAAAA,IAAAA,EACFH,yBADEG,CAAAA,EAAAA,OAAAA;EAAO,MACTH,EAAAA,OACRK,KADQL;EAAyB,UACjCK,CAAAA,GAAAA,EACCC,QADDD,EAAAA,IAAAA,EACiBL,yBADjBK,CAAAA,EAC6CF,OAD7CE,CAAAA,GAAAA,CAAAA;EAAK,QACJC,CAAAA,GAAAA,EAAAA,MAAAA,GAAAA,SAAAA,EAAAA,IAAAA,EACwBN,yBADxBM,EAAAA,iBAAAA,EACsEL,MADtEK,CAAAA,MAAAA,EAAAA,MAAAA,CAAAA,CAAAA,EAC+FH,OAD/FG,CAAAA;IAAgBN,IAAAA,EAAAA,GAAAA;IAA4BG,MAAAA,EAAAA,CAAAA,CAAAA;IACpBH,MAAAA,EAAAA,MAAAA;IAA8CC,UAAAA,EAAAA,MAAAA;IAKzEM,OAAAA,EAAAA,OAAAA;IALkGJ,OAAAA,EAAAA;MAN9CD,WAAAA,EAAAA,MAAAA;IAAsB,CAAA;EAiBtEM,CAAAA,CAAAA;;AAIDL,cAJCK,gBAAAA,SAAyBJ,2BAAAA,CAI1BD;EAAO,MACTH,EAAAA,MAAAA;EAAyB,WALGI,CAAAA,MAAAA,EAAAA,MAAAA;EAA2B,IAAA,UAAA,CAAA,CAAA,EAAA,MAAA;EAOjDK,YAAAA,CAAAA,CAAAA,EAHJN,OAGmB,CAAA,MAAWN,CAAAA;EAC1Ba,OAAAA,CAAAA,IAAAA,EAHNV,yBAG4B,CAAA,EAAA,OAAA;;AAA0BW,iBADhDF,eAAAA,CACgDE,QAAAA,EADtBd,kBACsBc,CAAAA,EAAAA,MAAAA,EAAAA;AAA6Ed,iBAA7Ha,sBAA6Hb,CAAAA,WAAAA,CAAAA,CAAAA,UAAAA,EAA7Ec,WAA6Ed,GAAAA,SAAAA,EAAAA,aAAAA,EAAAA,MAAAA,EAAAA,gBAAAA,EAAAA,MAAAA,EAAAA,GAAAA,kBAAAA,GAAAA,SAAAA,CAAAA,EAAiCc,WAAjCd"}
package/dist/auth.js CHANGED
@@ -1,113 +1,83 @@
1
1
  import { ReadableJsonStream } from "./utils/stream.js";
2
- export class GoogleAbstractedFetchClient {
3
- constructor() {
4
- Object.defineProperty(this, "_fetch", {
5
- enumerable: true,
6
- configurable: true,
7
- writable: true,
8
- value: fetch
9
- });
10
- }
11
- async _buildData(res, opts) {
12
- switch (opts.responseType) {
13
- case "json":
14
- return res.json();
15
- case "stream":
16
- return new ReadableJsonStream(res.body);
17
- default:
18
- return res.blob();
19
- }
20
- }
21
- async _request(url, opts, additionalHeaders) {
22
- if (url == null)
23
- throw new Error("Missing URL");
24
- const fetchOptions = {
25
- method: opts.method,
26
- headers: {
27
- "Content-Type": "application/json",
28
- ...(opts.headers ?? {}),
29
- ...(additionalHeaders ?? {}),
30
- },
31
- };
32
- if (opts.data !== undefined) {
33
- if (typeof opts.data === "string") {
34
- fetchOptions.body = opts.data;
35
- }
36
- else {
37
- fetchOptions.body = JSON.stringify(opts.data);
38
- }
39
- }
40
- const res = await this._fetch(url, fetchOptions);
41
- if (!res.ok) {
42
- const resText = await res.text();
43
- const error = new Error(`Google request failed with status code ${res.status}: ${resText}`);
44
- /* eslint-disable @typescript-eslint/no-explicit-any */
45
- error.response = res;
46
- error.details = {
47
- url,
48
- opts,
49
- fetchOptions,
50
- result: res,
51
- };
52
- /* eslint-enable @typescript-eslint/no-explicit-any */
53
- throw error;
54
- }
55
- const data = await this._buildData(res, opts);
56
- return {
57
- data,
58
- config: {},
59
- status: res.status,
60
- statusText: res.statusText,
61
- headers: res.headers,
62
- request: { responseURL: res.url },
63
- };
64
- }
2
+
3
+ //#region src/auth.ts
4
+ var GoogleAbstractedFetchClient = class {
5
+ _fetch = fetch;
6
+ async _buildData(res, opts) {
7
+ switch (opts.responseType) {
8
+ case "json": return res.json();
9
+ case "stream": return new ReadableJsonStream(res.body);
10
+ default: return res.blob();
11
+ }
12
+ }
13
+ async _request(url, opts, additionalHeaders) {
14
+ if (url == null) throw new Error("Missing URL");
15
+ const fetchOptions = {
16
+ method: opts.method,
17
+ headers: {
18
+ "Content-Type": "application/json",
19
+ ...opts.headers ?? {},
20
+ ...additionalHeaders ?? {}
21
+ }
22
+ };
23
+ if (opts.data !== void 0) if (typeof opts.data === "string") fetchOptions.body = opts.data;
24
+ else fetchOptions.body = JSON.stringify(opts.data);
25
+ const res = await this._fetch(url, fetchOptions);
26
+ if (!res.ok) {
27
+ const resText = await res.text();
28
+ const error = /* @__PURE__ */ new Error(`Google request failed with status code ${res.status}: ${resText}`);
29
+ error.response = res;
30
+ error.details = {
31
+ url,
32
+ opts,
33
+ fetchOptions,
34
+ result: res
35
+ };
36
+ throw error;
37
+ }
38
+ const data = await this._buildData(res, opts);
39
+ return {
40
+ data,
41
+ config: {},
42
+ status: res.status,
43
+ statusText: res.statusText,
44
+ headers: res.headers,
45
+ request: { responseURL: res.url }
46
+ };
47
+ }
48
+ };
49
+ var ApiKeyGoogleAuth = class extends GoogleAbstractedFetchClient {
50
+ apiKey;
51
+ constructor(apiKey) {
52
+ super();
53
+ this.apiKey = apiKey;
54
+ }
55
+ get clientType() {
56
+ return "apiKey";
57
+ }
58
+ getProjectId() {
59
+ throw new Error("APIs that require a project ID cannot use an API key");
60
+ }
61
+ request(opts) {
62
+ const authHeader = { "X-Goog-Api-Key": this.apiKey };
63
+ return this._request(opts.url, opts, authHeader);
64
+ }
65
+ };
66
+ function aiPlatformScope(platform) {
67
+ switch (platform) {
68
+ case "gai": return ["https://www.googleapis.com/auth/generative-language"];
69
+ default: return ["https://www.googleapis.com/auth/cloud-platform"];
70
+ }
65
71
  }
66
- export class ApiKeyGoogleAuth extends GoogleAbstractedFetchClient {
67
- constructor(apiKey) {
68
- super();
69
- Object.defineProperty(this, "apiKey", {
70
- enumerable: true,
71
- configurable: true,
72
- writable: true,
73
- value: void 0
74
- });
75
- this.apiKey = apiKey;
76
- }
77
- get clientType() {
78
- return "apiKey";
79
- }
80
- getProjectId() {
81
- throw new Error("APIs that require a project ID cannot use an API key");
82
- // Perhaps we could implement this if needed:
83
- // https://cloud.google.com/docs/authentication/api-keys#get-info
84
- }
85
- request(opts) {
86
- const authHeader = {
87
- "X-Goog-Api-Key": this.apiKey,
88
- };
89
- return this._request(opts.url, opts, authHeader);
90
- }
91
- }
92
- export function aiPlatformScope(platform) {
93
- switch (platform) {
94
- case "gai":
95
- return ["https://www.googleapis.com/auth/generative-language"];
96
- default:
97
- return ["https://www.googleapis.com/auth/cloud-platform"];
98
- }
99
- }
100
- export function ensureAuthOptionScopes(authOption, scopeProperty, scopesOrPlatform) {
101
- // If the property is already set, return it
102
- if (authOption && Object.hasOwn(authOption, scopeProperty)) {
103
- return authOption;
104
- }
105
- // Otherwise add it
106
- const scopes = Array.isArray(scopesOrPlatform)
107
- ? scopesOrPlatform
108
- : aiPlatformScope(scopesOrPlatform ?? "gcp");
109
- return {
110
- [scopeProperty]: scopes,
111
- ...(authOption ?? {}),
112
- };
72
+ function ensureAuthOptionScopes(authOption, scopeProperty, scopesOrPlatform) {
73
+ if (authOption && Object.hasOwn(authOption, scopeProperty)) return authOption;
74
+ const scopes = Array.isArray(scopesOrPlatform) ? scopesOrPlatform : aiPlatformScope(scopesOrPlatform ?? "gcp");
75
+ return {
76
+ [scopeProperty]: scopes,
77
+ ...authOption ?? {}
78
+ };
113
79
  }
80
+
81
+ //#endregion
82
+ export { ApiKeyGoogleAuth, GoogleAbstractedFetchClient, aiPlatformScope, ensureAuthOptionScopes };
83
+ //# sourceMappingURL=auth.js.map