@langchain/google-common 0.2.18 → 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.
- package/CHANGELOG.md +17 -0
- package/LICENSE +6 -6
- package/dist/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/auth.cjs +82 -116
- package/dist/auth.cjs.map +1 -0
- package/dist/auth.d.cts +46 -0
- package/dist/auth.d.cts.map +1 -0
- package/dist/auth.d.ts +41 -36
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +80 -110
- package/dist/auth.js.map +1 -0
- package/dist/chat_models.cjs +251 -466
- package/dist/chat_models.cjs.map +1 -0
- package/dist/chat_models.d.cts +98 -0
- package/dist/chat_models.d.cts.map +1 -0
- package/dist/chat_models.d.ts +87 -73
- package/dist/chat_models.d.ts.map +1 -0
- package/dist/chat_models.js +245 -457
- package/dist/chat_models.js.map +1 -0
- package/dist/connection.cjs +321 -466
- package/dist/connection.cjs.map +1 -0
- package/dist/connection.d.cts +109 -0
- package/dist/connection.d.cts.map +1 -0
- package/dist/connection.d.ts +98 -91
- package/dist/connection.d.ts.map +1 -0
- package/dist/connection.js +317 -459
- package/dist/connection.js.map +1 -0
- package/dist/embeddings.cjs +135 -186
- package/dist/embeddings.cjs.map +1 -0
- package/dist/embeddings.d.cts +44 -0
- package/dist/embeddings.d.cts.map +1 -0
- package/dist/embeddings.d.ts +38 -32
- package/dist/embeddings.d.ts.map +1 -0
- package/dist/embeddings.js +133 -181
- package/dist/embeddings.js.map +1 -0
- package/dist/experimental/media.cjs +380 -482
- package/dist/experimental/media.cjs.map +1 -0
- package/dist/experimental/media.d.cts +198 -0
- package/dist/experimental/media.d.cts.map +1 -0
- package/dist/experimental/media.d.ts +190 -202
- package/dist/experimental/media.d.ts.map +1 -0
- package/dist/experimental/media.js +369 -468
- package/dist/experimental/media.js.map +1 -0
- package/dist/experimental/utils/media_core.cjs +403 -517
- package/dist/experimental/utils/media_core.cjs.map +1 -0
- package/dist/experimental/utils/media_core.d.cts +215 -0
- package/dist/experimental/utils/media_core.d.cts.map +1 -0
- package/dist/experimental/utils/media_core.d.ts +171 -165
- package/dist/experimental/utils/media_core.d.ts.map +1 -0
- package/dist/experimental/utils/media_core.js +395 -506
- package/dist/experimental/utils/media_core.js.map +1 -0
- package/dist/index.cjs +58 -27
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +13 -11
- package/dist/index.js +13 -11
- package/dist/llms.cjs +157 -244
- package/dist/llms.cjs.map +1 -0
- package/dist/llms.d.cts +72 -0
- package/dist/llms.d.cts.map +1 -0
- package/dist/llms.d.ts +64 -54
- package/dist/llms.d.ts.map +1 -0
- package/dist/llms.js +154 -238
- package/dist/llms.js.map +1 -0
- package/dist/output_parsers.cjs +148 -173
- package/dist/output_parsers.cjs.map +1 -0
- package/dist/output_parsers.d.cts +53 -0
- package/dist/output_parsers.d.cts.map +1 -0
- package/dist/output_parsers.d.ts +46 -42
- package/dist/output_parsers.d.ts.map +1 -0
- package/dist/output_parsers.js +146 -168
- package/dist/output_parsers.js.map +1 -0
- package/dist/types-anthropic.d.cts +229 -0
- package/dist/types-anthropic.d.cts.map +1 -0
- package/dist/types-anthropic.d.ts +221 -215
- package/dist/types-anthropic.d.ts.map +1 -0
- package/dist/types.cjs +51 -62
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +748 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.ts +669 -656
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +46 -45
- package/dist/types.js.map +1 -0
- package/dist/utils/anthropic.cjs +598 -821
- package/dist/utils/anthropic.cjs.map +1 -0
- package/dist/utils/anthropic.js +597 -818
- package/dist/utils/anthropic.js.map +1 -0
- package/dist/utils/common.cjs +130 -211
- package/dist/utils/common.cjs.map +1 -0
- package/dist/utils/common.d.cts +13 -0
- package/dist/utils/common.d.cts.map +1 -0
- package/dist/utils/common.d.ts +12 -7
- package/dist/utils/common.d.ts.map +1 -0
- package/dist/utils/common.js +128 -207
- package/dist/utils/common.js.map +1 -0
- package/dist/utils/failed_handler.cjs +28 -30
- package/dist/utils/failed_handler.cjs.map +1 -0
- package/dist/utils/failed_handler.d.cts +9 -0
- package/dist/utils/failed_handler.d.cts.map +1 -0
- package/dist/utils/failed_handler.d.ts +8 -2
- package/dist/utils/failed_handler.d.ts.map +1 -0
- package/dist/utils/failed_handler.js +28 -28
- package/dist/utils/failed_handler.js.map +1 -0
- package/dist/utils/gemini.cjs +1020 -1488
- package/dist/utils/gemini.cjs.map +1 -0
- package/dist/utils/gemini.d.cts +51 -0
- package/dist/utils/gemini.d.cts.map +1 -0
- package/dist/utils/gemini.d.ts +51 -48
- package/dist/utils/gemini.d.ts.map +1 -0
- package/dist/utils/gemini.js +1015 -1479
- package/dist/utils/gemini.js.map +1 -0
- package/dist/utils/index.cjs +38 -23
- package/dist/utils/index.d.cts +8 -0
- package/dist/utils/index.d.ts +8 -7
- package/dist/utils/index.js +8 -7
- package/dist/utils/palm.d.cts +11 -0
- package/dist/utils/palm.d.cts.map +1 -0
- package/dist/utils/palm.d.ts +9 -4
- package/dist/utils/palm.d.ts.map +1 -0
- package/dist/utils/safety.cjs +13 -22
- package/dist/utils/safety.cjs.map +1 -0
- package/dist/utils/safety.d.cts +12 -0
- package/dist/utils/safety.d.cts.map +1 -0
- package/dist/utils/safety.d.ts +10 -4
- package/dist/utils/safety.d.ts.map +1 -0
- package/dist/utils/safety.js +13 -19
- package/dist/utils/safety.js.map +1 -0
- package/dist/utils/stream.cjs +296 -475
- package/dist/utils/stream.cjs.map +1 -0
- package/dist/utils/stream.d.cts +165 -0
- package/dist/utils/stream.d.cts.map +1 -0
- package/dist/utils/stream.d.ts +156 -131
- package/dist/utils/stream.d.ts.map +1 -0
- package/dist/utils/stream.js +293 -469
- package/dist/utils/stream.js.map +1 -0
- package/dist/utils/zod_to_gemini_parameters.cjs +43 -81
- package/dist/utils/zod_to_gemini_parameters.cjs.map +1 -0
- package/dist/utils/zod_to_gemini_parameters.d.cts +22 -0
- package/dist/utils/zod_to_gemini_parameters.d.cts.map +1 -0
- package/dist/utils/zod_to_gemini_parameters.d.ts +21 -6
- package/dist/utils/zod_to_gemini_parameters.d.ts.map +1 -0
- package/dist/utils/zod_to_gemini_parameters.js +40 -76
- package/dist/utils/zod_to_gemini_parameters.js.map +1 -0
- package/package.json +69 -85
- package/dist/types-anthropic.cjs +0 -2
- package/dist/types-anthropic.js +0 -1
- package/dist/utils/anthropic.d.ts +0 -4
- package/dist/utils/palm.cjs +0 -2
- package/dist/utils/palm.js +0 -1
- package/experimental/media.cjs +0 -1
- package/experimental/media.d.cts +0 -1
- package/experimental/media.d.ts +0 -1
- package/experimental/media.js +0 -1
- package/experimental/utils/media_core.cjs +0 -1
- package/experimental/utils/media_core.d.cts +0 -1
- package/experimental/utils/media_core.d.ts +0 -1
- package/experimental/utils/media_core.js +0 -1
- package/index.cjs +0 -1
- package/index.d.cts +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -1
- package/types.cjs +0 -1
- package/types.d.cts +0 -1
- package/types.d.ts +0 -1
- package/types.js +0 -1
- package/utils.cjs +0 -1
- package/utils.d.cts +0 -1
- package/utils.d.ts +0 -1
- 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
|
-
|
|
1
|
+
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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 ??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,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"}
|
package/dist/auth.d.cts
ADDED
|
@@ -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,O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BAG4B,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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
interface GoogleAbstractedClient {
|
|
14
|
+
request: (opts: GoogleAbstractedClientOps) => unknown;
|
|
15
|
+
getProjectId: () => Promise<string>;
|
|
16
|
+
get clientType(): string;
|
|
15
17
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
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.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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49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|