@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
@@ -1,472 +1,327 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GoogleRequestRecorder = exports.GoogleRequestLogger = exports.GoogleRequestCallbackHandler = exports.AbstractGoogleLLMConnection = exports.GoogleAIConnection = exports.GoogleRawConnection = exports.GoogleHostConnection = exports.GoogleConnection = void 0;
4
- const env_1 = require("@langchain/core/utils/env");
5
- const base_1 = require("@langchain/core/callbacks/base");
6
- const index_js_1 = require("./utils/index.cjs");
7
- const anthropic_js_1 = require("./utils/anthropic.cjs");
8
- class GoogleConnection {
9
- constructor(caller, client, streaming) {
10
- Object.defineProperty(this, "caller", {
11
- enumerable: true,
12
- configurable: true,
13
- writable: true,
14
- value: void 0
15
- });
16
- Object.defineProperty(this, "client", {
17
- enumerable: true,
18
- configurable: true,
19
- writable: true,
20
- value: void 0
21
- });
22
- Object.defineProperty(this, "streaming", {
23
- enumerable: true,
24
- configurable: true,
25
- writable: true,
26
- value: void 0
27
- });
28
- this.caller = caller;
29
- this.client = client;
30
- this.streaming = streaming ?? false;
31
- }
32
- async _clientInfoHeaders() {
33
- const { userAgent, clientLibraryVersion } = await this._getClientInfo();
34
- return {
35
- "User-Agent": userAgent,
36
- "Client-Info": clientLibraryVersion,
37
- };
38
- }
39
- async _getClientInfo() {
40
- const env = await (0, env_1.getRuntimeEnvironment)();
41
- const langchain = env?.library ?? "langchain-js";
42
- // TODO: Add an API for getting the current LangChain version
43
- const langchainVersion = "0";
44
- const moduleName = await this._moduleName();
45
- let clientLibraryVersion = `${langchain}/${langchainVersion}`;
46
- if (moduleName && moduleName.length) {
47
- clientLibraryVersion = `${clientLibraryVersion}-${moduleName}`;
48
- }
49
- return {
50
- userAgent: clientLibraryVersion,
51
- clientLibraryVersion: `${langchainVersion}-${moduleName}`,
52
- };
53
- }
54
- async _moduleName() {
55
- return this.constructor.name;
56
- }
57
- async additionalHeaders() {
58
- return {};
59
- }
60
- async _buildOpts(data, _options, requestHeaders = {}) {
61
- const url = await this.buildUrl();
62
- const method = this.buildMethod();
63
- const infoHeaders = (await this._clientInfoHeaders()) ?? {};
64
- const additionalHeaders = (await this.additionalHeaders()) ?? {};
65
- const headers = {
66
- ...infoHeaders,
67
- ...additionalHeaders,
68
- ...requestHeaders,
69
- };
70
- const opts = {
71
- url,
72
- method,
73
- headers,
74
- };
75
- if (data && method === "POST") {
76
- opts.data = data;
77
- }
78
- if (this.streaming) {
79
- opts.responseType = "stream";
80
- }
81
- else {
82
- opts.responseType = "json";
83
- }
84
- return opts;
85
- }
86
- async _request(data, options, requestHeaders = {}) {
87
- const opts = await this._buildOpts(data, options, requestHeaders);
88
- const callResponse = await this.caller.callWithOptions({ signal: options?.signal }, async () => this.client.request(opts));
89
- const response = callResponse; // Done for typecast safety, I guess
90
- return response;
91
- }
92
- }
1
+ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
+ const require_gemini = require('./utils/gemini.cjs');
3
+ const require_anthropic = require('./utils/anthropic.cjs');
4
+ const require_common = require('./utils/common.cjs');
5
+ require('./utils/index.cjs');
6
+ const __langchain_core_utils_env = require_rolldown_runtime.__toESM(require("@langchain/core/utils/env"));
7
+ const __langchain_core_callbacks_base = require_rolldown_runtime.__toESM(require("@langchain/core/callbacks/base"));
8
+
9
+ //#region src/connection.ts
10
+ var GoogleConnection = class {
11
+ caller;
12
+ client;
13
+ streaming;
14
+ constructor(caller, client, streaming) {
15
+ this.caller = caller;
16
+ this.client = client;
17
+ this.streaming = streaming ?? false;
18
+ }
19
+ async _clientInfoHeaders() {
20
+ const { userAgent, clientLibraryVersion } = await this._getClientInfo();
21
+ return {
22
+ "User-Agent": userAgent,
23
+ "Client-Info": clientLibraryVersion
24
+ };
25
+ }
26
+ async _getClientInfo() {
27
+ const env = await (0, __langchain_core_utils_env.getRuntimeEnvironment)();
28
+ const langchain = env?.library ?? "langchain-js";
29
+ const langchainVersion = "0";
30
+ const moduleName = await this._moduleName();
31
+ let clientLibraryVersion = `${langchain}/${langchainVersion}`;
32
+ if (moduleName && moduleName.length) clientLibraryVersion = `${clientLibraryVersion}-${moduleName}`;
33
+ return {
34
+ userAgent: clientLibraryVersion,
35
+ clientLibraryVersion: `${langchainVersion}-${moduleName}`
36
+ };
37
+ }
38
+ async _moduleName() {
39
+ return this.constructor.name;
40
+ }
41
+ async additionalHeaders() {
42
+ return {};
43
+ }
44
+ async _buildOpts(data, _options, requestHeaders = {}) {
45
+ const url = await this.buildUrl();
46
+ const method = this.buildMethod();
47
+ const infoHeaders = await this._clientInfoHeaders() ?? {};
48
+ const additionalHeaders = await this.additionalHeaders() ?? {};
49
+ const headers = {
50
+ ...infoHeaders,
51
+ ...additionalHeaders,
52
+ ...requestHeaders
53
+ };
54
+ const opts = {
55
+ url,
56
+ method,
57
+ headers
58
+ };
59
+ if (data && method === "POST") opts.data = data;
60
+ if (this.streaming) opts.responseType = "stream";
61
+ else opts.responseType = "json";
62
+ return opts;
63
+ }
64
+ async _request(data, options, requestHeaders = {}) {
65
+ const opts = await this._buildOpts(data, options, requestHeaders);
66
+ const callResponse = await this.caller.callWithOptions({ signal: options?.signal }, async () => this.client.request(opts));
67
+ const response = callResponse;
68
+ return response;
69
+ }
70
+ };
71
+ var GoogleHostConnection = class extends GoogleConnection {
72
+ platformType;
73
+ _endpoint;
74
+ _location;
75
+ _apiVersion;
76
+ constructor(fields, caller, client, streaming) {
77
+ super(caller, client, streaming);
78
+ this.caller = caller;
79
+ this.platformType = this.fieldPlatformType(fields);
80
+ this._endpoint = fields?.endpoint;
81
+ this._location = fields?.location;
82
+ this._apiVersion = fields?.apiVersion;
83
+ this.client = client;
84
+ }
85
+ fieldPlatformType(fields) {
86
+ if (typeof fields === "undefined") return void 0;
87
+ if (typeof fields.platformType !== "undefined") return fields.platformType;
88
+ if (fields.vertexai === true) return "gcp";
89
+ return void 0;
90
+ }
91
+ get platform() {
92
+ return this.platformType ?? this.computedPlatformType;
93
+ }
94
+ get computedPlatformType() {
95
+ return "gcp";
96
+ }
97
+ get computedApiVersion() {
98
+ return "v1";
99
+ }
100
+ get apiVersion() {
101
+ return this._apiVersion ?? this.computedApiVersion;
102
+ }
103
+ get location() {
104
+ return this._location ?? this.computedLocation;
105
+ }
106
+ get computedLocation() {
107
+ return "us-central1";
108
+ }
109
+ get endpoint() {
110
+ return this._endpoint ?? this.computedEndpoint;
111
+ }
112
+ get computedEndpoint() {
113
+ if (this.location === "global") return "aiplatform.googleapis.com";
114
+ else return `${this.location}-aiplatform.googleapis.com`;
115
+ }
116
+ buildMethod() {
117
+ return "POST";
118
+ }
119
+ };
120
+ var GoogleRawConnection = class extends GoogleHostConnection {
121
+ async _buildOpts(data, _options, requestHeaders = {}) {
122
+ const opts = await super._buildOpts(data, _options, requestHeaders);
123
+ opts.responseType = "blob";
124
+ return opts;
125
+ }
126
+ };
127
+ var GoogleAIConnection = class extends GoogleHostConnection {
128
+ model;
129
+ modelName;
130
+ client;
131
+ _apiName;
132
+ apiConfig;
133
+ constructor(fields, caller, client, streaming) {
134
+ super(fields, caller, client, streaming);
135
+ this.client = client;
136
+ this.modelName = fields?.model ?? fields?.modelName ?? this.model;
137
+ this.model = this.modelName;
138
+ this._apiName = fields?.apiName;
139
+ this.apiConfig = {
140
+ safetyHandler: fields?.safetyHandler,
141
+ ...fields?.apiConfig
142
+ };
143
+ }
144
+ get modelFamily() {
145
+ return require_common.modelToFamily(this.model);
146
+ }
147
+ get modelPublisher() {
148
+ return require_common.modelToPublisher(this.model);
149
+ }
150
+ get computedAPIName() {
151
+ return this.modelPublisher;
152
+ }
153
+ get apiName() {
154
+ return this._apiName ?? this.computedAPIName;
155
+ }
156
+ get api() {
157
+ switch (this.apiName) {
158
+ case "google":
159
+ case "gemma": return require_gemini.getGeminiAPI(this.apiConfig);
160
+ case "anthropic": return require_anthropic.getAnthropicAPI(this.apiConfig);
161
+ default: throw new Error(`Unknown API: ${this.apiName}`);
162
+ }
163
+ }
164
+ get isApiKey() {
165
+ return this.client.clientType === "apiKey";
166
+ }
167
+ fieldPlatformType(fields) {
168
+ const ret = super.fieldPlatformType(fields);
169
+ if (typeof ret !== "undefined") return ret;
170
+ if (fields?.vertexai === false) return "gai";
171
+ return void 0;
172
+ }
173
+ get computedPlatformType() {
174
+ if (this.isApiKey) return "gai";
175
+ else return "gcp";
176
+ }
177
+ get computedApiVersion() {
178
+ switch (this.platform) {
179
+ case "gai": return "v1beta";
180
+ default: return "v1";
181
+ }
182
+ }
183
+ get computedLocation() {
184
+ switch (this.apiName) {
185
+ case "google": return super.computedLocation;
186
+ case "anthropic": return "us-east5";
187
+ default: throw new Error(`Unknown apiName: ${this.apiName}. Can't get location.`);
188
+ }
189
+ }
190
+ async buildUrlGenerativeLanguage() {
191
+ const method = await this.buildUrlMethod();
192
+ const url = `https://generativelanguage.googleapis.com/${this.apiVersion}/models/${this.model}:${method}`;
193
+ return url;
194
+ }
195
+ async buildUrlVertexExpress() {
196
+ const method = await this.buildUrlMethod();
197
+ const publisher = this.modelPublisher;
198
+ const url = `https://aiplatform.googleapis.com/${this.apiVersion}/publishers/${publisher}/models/${this.model}:${method}`;
199
+ return url;
200
+ }
201
+ async buildUrlVertexLocation() {
202
+ const projectId = await this.client.getProjectId();
203
+ const method = await this.buildUrlMethod();
204
+ const publisher = this.modelPublisher;
205
+ const url = `https://${this.endpoint}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/publishers/${publisher}/models/${this.model}:${method}`;
206
+ return url;
207
+ }
208
+ async buildUrlVertex() {
209
+ if (this.isApiKey) return this.buildUrlVertexExpress();
210
+ else return this.buildUrlVertexLocation();
211
+ }
212
+ async buildUrl() {
213
+ switch (this.platform) {
214
+ case "gai": return this.buildUrlGenerativeLanguage();
215
+ default: return this.buildUrlVertex();
216
+ }
217
+ }
218
+ async request(input, parameters, options, runManager) {
219
+ const moduleName = this.constructor.name;
220
+ const streamingParameters = {
221
+ ...parameters,
222
+ streaming: this.streaming
223
+ };
224
+ const data = await this.formatData(input, streamingParameters);
225
+ await runManager?.handleCustomEvent(`google-request-${moduleName}`, {
226
+ data,
227
+ parameters: streamingParameters,
228
+ options,
229
+ connection: {
230
+ ...this,
231
+ url: await this.buildUrl(),
232
+ urlMethod: await this.buildUrlMethod(),
233
+ modelFamily: this.modelFamily,
234
+ modelPublisher: this.modelPublisher,
235
+ computedPlatformType: this.computedPlatformType
236
+ }
237
+ });
238
+ const response = await this._request(data, options);
239
+ await runManager?.handleCustomEvent(`google-response-${moduleName}`, { response });
240
+ return response;
241
+ }
242
+ };
243
+ var AbstractGoogleLLMConnection = class extends GoogleAIConnection {
244
+ async buildUrlMethodGemini() {
245
+ return this.streaming ? "streamGenerateContent" : "generateContent";
246
+ }
247
+ async buildUrlMethodClaude() {
248
+ return this.streaming ? "streamRawPredict" : "rawPredict";
249
+ }
250
+ async buildUrlMethod() {
251
+ switch (this.modelFamily) {
252
+ case "gemini":
253
+ case "gemma": return this.buildUrlMethodGemini();
254
+ case "claude": return this.buildUrlMethodClaude();
255
+ default: throw new Error(`Unknown model family: ${this.modelFamily}`);
256
+ }
257
+ }
258
+ async formatData(input, parameters) {
259
+ let filteredParameters = parameters;
260
+ if (parameters.labels && this.platform !== "gcp") {
261
+ const { labels,...paramsWithoutLabels } = parameters;
262
+ filteredParameters = paramsWithoutLabels;
263
+ }
264
+ return this.api.formatData(input, filteredParameters);
265
+ }
266
+ };
267
+ var GoogleRequestCallbackHandler = class extends __langchain_core_callbacks_base.BaseCallbackHandler {
268
+ customEventInfo(eventName) {
269
+ const names = eventName.split("-");
270
+ return {
271
+ subEvent: names[1],
272
+ module: names[2]
273
+ };
274
+ }
275
+ handleCustomEvent(eventName, data, runId, tags, metadata) {
276
+ if (!eventName) return void 0;
277
+ const eventInfo = this.customEventInfo(eventName);
278
+ switch (eventInfo.subEvent) {
279
+ case "request": return this.handleCustomRequestEvent(eventName, eventInfo, data, runId, tags, metadata);
280
+ case "response": return this.handleCustomResponseEvent(eventName, eventInfo, data, runId, tags, metadata);
281
+ case "chunk": return this.handleCustomChunkEvent(eventName, eventInfo, data, runId, tags, metadata);
282
+ default: console.error(`Unexpected eventInfo for ${eventName} ${JSON.stringify(eventInfo, null, 1)}`);
283
+ }
284
+ }
285
+ };
286
+ var GoogleRequestLogger = class extends GoogleRequestCallbackHandler {
287
+ name = "GoogleRequestLogger";
288
+ log(eventName, data, tags) {
289
+ const tagStr = tags ? `[${tags}]` : "[]";
290
+ console.log(`${eventName} ${tagStr} ${JSON.stringify(data, null, 1)}`);
291
+ }
292
+ handleCustomRequestEvent(eventName, _eventInfo, data, _runId, tags, _metadata) {
293
+ this.log(eventName, data, tags);
294
+ }
295
+ handleCustomResponseEvent(eventName, _eventInfo, data, _runId, tags, _metadata) {
296
+ this.log(eventName, data, tags);
297
+ }
298
+ handleCustomChunkEvent(eventName, _eventInfo, data, _runId, tags, _metadata) {
299
+ this.log(eventName, data, tags);
300
+ }
301
+ };
302
+ var GoogleRequestRecorder = class extends GoogleRequestCallbackHandler {
303
+ name = "GoogleRequestRecorder";
304
+ request = {};
305
+ response = {};
306
+ chunk = [];
307
+ handleCustomRequestEvent(_eventName, _eventInfo, data, _runId, _tags, _metadata) {
308
+ this.request = data;
309
+ }
310
+ handleCustomResponseEvent(_eventName, _eventInfo, data, _runId, _tags, _metadata) {
311
+ this.response = data;
312
+ }
313
+ handleCustomChunkEvent(_eventName, _eventInfo, data, _runId, _tags, _metadata) {
314
+ this.chunk.push(data);
315
+ }
316
+ };
317
+
318
+ //#endregion
319
+ exports.AbstractGoogleLLMConnection = AbstractGoogleLLMConnection;
320
+ exports.GoogleAIConnection = GoogleAIConnection;
93
321
  exports.GoogleConnection = GoogleConnection;
94
- class GoogleHostConnection extends GoogleConnection {
95
- constructor(fields, caller, client, streaming) {
96
- super(caller, client, streaming);
97
- // This does not default to a value intentionally.
98
- // Use the "platform" getter if you need this.
99
- Object.defineProperty(this, "platformType", {
100
- enumerable: true,
101
- configurable: true,
102
- writable: true,
103
- value: void 0
104
- });
105
- Object.defineProperty(this, "_endpoint", {
106
- enumerable: true,
107
- configurable: true,
108
- writable: true,
109
- value: void 0
110
- });
111
- Object.defineProperty(this, "_location", {
112
- enumerable: true,
113
- configurable: true,
114
- writable: true,
115
- value: void 0
116
- });
117
- Object.defineProperty(this, "_apiVersion", {
118
- enumerable: true,
119
- configurable: true,
120
- writable: true,
121
- value: void 0
122
- });
123
- this.caller = caller;
124
- this.platformType = this.fieldPlatformType(fields);
125
- this._endpoint = fields?.endpoint;
126
- this._location = fields?.location;
127
- this._apiVersion = fields?.apiVersion;
128
- this.client = client;
129
- }
130
- fieldPlatformType(fields) {
131
- if (typeof fields === "undefined") {
132
- return undefined;
133
- }
134
- if (typeof fields.platformType !== "undefined") {
135
- return fields.platformType;
136
- }
137
- if (fields.vertexai === true) {
138
- return "gcp";
139
- }
140
- return undefined;
141
- }
142
- get platform() {
143
- return this.platformType ?? this.computedPlatformType;
144
- }
145
- get computedPlatformType() {
146
- return "gcp";
147
- }
148
- get computedApiVersion() {
149
- return "v1";
150
- }
151
- get apiVersion() {
152
- return this._apiVersion ?? this.computedApiVersion;
153
- }
154
- get location() {
155
- return this._location ?? this.computedLocation;
156
- }
157
- get computedLocation() {
158
- return "us-central1";
159
- }
160
- get endpoint() {
161
- return this._endpoint ?? this.computedEndpoint;
162
- }
163
- get computedEndpoint() {
164
- if (this.location === "global") {
165
- return "aiplatform.googleapis.com";
166
- }
167
- else {
168
- return `${this.location}-aiplatform.googleapis.com`;
169
- }
170
- }
171
- buildMethod() {
172
- return "POST";
173
- }
174
- }
175
322
  exports.GoogleHostConnection = GoogleHostConnection;
176
- class GoogleRawConnection extends GoogleHostConnection {
177
- async _buildOpts(data, _options, requestHeaders = {}) {
178
- const opts = await super._buildOpts(data, _options, requestHeaders);
179
- opts.responseType = "blob";
180
- return opts;
181
- }
182
- }
183
323
  exports.GoogleRawConnection = GoogleRawConnection;
184
- class GoogleAIConnection extends GoogleHostConnection {
185
- constructor(fields, caller, client, streaming) {
186
- super(fields, caller, client, streaming);
187
- Object.defineProperty(this, "model", {
188
- enumerable: true,
189
- configurable: true,
190
- writable: true,
191
- value: void 0
192
- });
193
- Object.defineProperty(this, "modelName", {
194
- enumerable: true,
195
- configurable: true,
196
- writable: true,
197
- value: void 0
198
- });
199
- Object.defineProperty(this, "client", {
200
- enumerable: true,
201
- configurable: true,
202
- writable: true,
203
- value: void 0
204
- });
205
- Object.defineProperty(this, "_apiName", {
206
- enumerable: true,
207
- configurable: true,
208
- writable: true,
209
- value: void 0
210
- });
211
- Object.defineProperty(this, "apiConfig", {
212
- enumerable: true,
213
- configurable: true,
214
- writable: true,
215
- value: void 0
216
- });
217
- this.client = client;
218
- this.modelName = fields?.model ?? fields?.modelName ?? this.model;
219
- this.model = this.modelName;
220
- this._apiName = fields?.apiName;
221
- this.apiConfig = {
222
- safetyHandler: fields?.safetyHandler, // For backwards compatibility
223
- ...fields?.apiConfig,
224
- };
225
- }
226
- get modelFamily() {
227
- return (0, index_js_1.modelToFamily)(this.model);
228
- }
229
- get modelPublisher() {
230
- return (0, index_js_1.modelToPublisher)(this.model);
231
- }
232
- get computedAPIName() {
233
- // At least at the moment, model publishers and APIs map the same
234
- return this.modelPublisher;
235
- }
236
- get apiName() {
237
- return this._apiName ?? this.computedAPIName;
238
- }
239
- get api() {
240
- switch (this.apiName) {
241
- case "google":
242
- case "gemma": // TODO: Is this true?
243
- return (0, index_js_1.getGeminiAPI)(this.apiConfig);
244
- case "anthropic":
245
- return (0, anthropic_js_1.getAnthropicAPI)(this.apiConfig);
246
- default:
247
- throw new Error(`Unknown API: ${this.apiName}`);
248
- }
249
- }
250
- get isApiKey() {
251
- return this.client.clientType === "apiKey";
252
- }
253
- fieldPlatformType(fields) {
254
- const ret = super.fieldPlatformType(fields);
255
- if (typeof ret !== "undefined") {
256
- return ret;
257
- }
258
- if (fields?.vertexai === false) {
259
- return "gai";
260
- }
261
- return undefined;
262
- }
263
- get computedPlatformType() {
264
- // This is not a completely correct assumption, since GCP can
265
- // have an API Key. But if so, then people need to set the platform
266
- // type explicitly.
267
- if (this.isApiKey) {
268
- return "gai";
269
- }
270
- else {
271
- return "gcp";
272
- }
273
- }
274
- get computedApiVersion() {
275
- switch (this.platform) {
276
- case "gai":
277
- return "v1beta";
278
- default:
279
- return "v1";
280
- }
281
- }
282
- get computedLocation() {
283
- switch (this.apiName) {
284
- case "google":
285
- return super.computedLocation;
286
- case "anthropic":
287
- return "us-east5";
288
- default:
289
- throw new Error(`Unknown apiName: ${this.apiName}. Can't get location.`);
290
- }
291
- }
292
- async buildUrlGenerativeLanguage() {
293
- const method = await this.buildUrlMethod();
294
- const url = `https://generativelanguage.googleapis.com/${this.apiVersion}/models/${this.model}:${method}`;
295
- return url;
296
- }
297
- async buildUrlVertexExpress() {
298
- const method = await this.buildUrlMethod();
299
- const publisher = this.modelPublisher;
300
- const url = `https://aiplatform.googleapis.com/${this.apiVersion}/publishers/${publisher}/models/${this.model}:${method}`;
301
- return url;
302
- }
303
- async buildUrlVertexLocation() {
304
- const projectId = await this.client.getProjectId();
305
- const method = await this.buildUrlMethod();
306
- const publisher = this.modelPublisher;
307
- const url = `https://${this.endpoint}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/publishers/${publisher}/models/${this.model}:${method}`;
308
- return url;
309
- }
310
- async buildUrlVertex() {
311
- if (this.isApiKey) {
312
- return this.buildUrlVertexExpress();
313
- }
314
- else {
315
- return this.buildUrlVertexLocation();
316
- }
317
- }
318
- async buildUrl() {
319
- switch (this.platform) {
320
- case "gai":
321
- return this.buildUrlGenerativeLanguage();
322
- default:
323
- return this.buildUrlVertex();
324
- }
325
- }
326
- async request(input, parameters, options, runManager) {
327
- const moduleName = this.constructor.name;
328
- const streamingParameters = {
329
- ...parameters,
330
- streaming: this.streaming,
331
- };
332
- const data = await this.formatData(input, streamingParameters);
333
- await runManager?.handleCustomEvent(`google-request-${moduleName}`, {
334
- data,
335
- parameters: streamingParameters,
336
- options,
337
- connection: {
338
- ...this,
339
- url: await this.buildUrl(),
340
- urlMethod: await this.buildUrlMethod(),
341
- modelFamily: this.modelFamily,
342
- modelPublisher: this.modelPublisher,
343
- computedPlatformType: this.computedPlatformType,
344
- },
345
- });
346
- const response = await this._request(data, options);
347
- await runManager?.handleCustomEvent(`google-response-${moduleName}`, {
348
- response,
349
- });
350
- return response;
351
- }
352
- }
353
- exports.GoogleAIConnection = GoogleAIConnection;
354
- class AbstractGoogleLLMConnection extends GoogleAIConnection {
355
- async buildUrlMethodGemini() {
356
- return this.streaming ? "streamGenerateContent" : "generateContent";
357
- }
358
- async buildUrlMethodClaude() {
359
- return this.streaming ? "streamRawPredict" : "rawPredict";
360
- }
361
- async buildUrlMethod() {
362
- switch (this.modelFamily) {
363
- case "gemini":
364
- case "gemma": // TODO: Is this true?
365
- return this.buildUrlMethodGemini();
366
- case "claude":
367
- return this.buildUrlMethodClaude();
368
- default:
369
- throw new Error(`Unknown model family: ${this.modelFamily}`);
370
- }
371
- }
372
- async formatData(input, parameters) {
373
- // Filter out labels for non-Vertex AI platforms (labels are only supported on Vertex AI)
374
- let filteredParameters = parameters;
375
- if (parameters.labels && this.platform !== "gcp") {
376
- const { labels, ...paramsWithoutLabels } = parameters;
377
- filteredParameters = paramsWithoutLabels;
378
- }
379
- return this.api.formatData(input, filteredParameters);
380
- }
381
- }
382
- exports.AbstractGoogleLLMConnection = AbstractGoogleLLMConnection;
383
- class GoogleRequestCallbackHandler extends base_1.BaseCallbackHandler {
384
- customEventInfo(eventName) {
385
- const names = eventName.split("-");
386
- return {
387
- subEvent: names[1],
388
- module: names[2],
389
- };
390
- }
391
- handleCustomEvent(eventName, data, runId, tags, metadata) {
392
- if (!eventName) {
393
- return undefined;
394
- }
395
- const eventInfo = this.customEventInfo(eventName);
396
- switch (eventInfo.subEvent) {
397
- case "request":
398
- return this.handleCustomRequestEvent(eventName, eventInfo, data, runId, tags, metadata);
399
- case "response":
400
- return this.handleCustomResponseEvent(eventName, eventInfo, data, runId, tags, metadata);
401
- case "chunk":
402
- return this.handleCustomChunkEvent(eventName, eventInfo, data, runId, tags, metadata);
403
- default:
404
- console.error(`Unexpected eventInfo for ${eventName} ${JSON.stringify(eventInfo, null, 1)}`);
405
- }
406
- }
407
- }
408
324
  exports.GoogleRequestCallbackHandler = GoogleRequestCallbackHandler;
409
- class GoogleRequestLogger extends GoogleRequestCallbackHandler {
410
- constructor() {
411
- super(...arguments);
412
- Object.defineProperty(this, "name", {
413
- enumerable: true,
414
- configurable: true,
415
- writable: true,
416
- value: "GoogleRequestLogger"
417
- });
418
- }
419
- log(eventName, data, tags) {
420
- const tagStr = tags ? `[${tags}]` : "[]";
421
- console.log(`${eventName} ${tagStr} ${JSON.stringify(data, null, 1)}`);
422
- }
423
- handleCustomRequestEvent(eventName, _eventInfo, data, _runId, tags, _metadata) {
424
- this.log(eventName, data, tags);
425
- }
426
- handleCustomResponseEvent(eventName, _eventInfo, data, _runId, tags, _metadata) {
427
- this.log(eventName, data, tags);
428
- }
429
- handleCustomChunkEvent(eventName, _eventInfo, data, _runId, tags, _metadata) {
430
- this.log(eventName, data, tags);
431
- }
432
- }
433
325
  exports.GoogleRequestLogger = GoogleRequestLogger;
434
- class GoogleRequestRecorder extends GoogleRequestCallbackHandler {
435
- constructor() {
436
- super(...arguments);
437
- Object.defineProperty(this, "name", {
438
- enumerable: true,
439
- configurable: true,
440
- writable: true,
441
- value: "GoogleRequestRecorder"
442
- });
443
- Object.defineProperty(this, "request", {
444
- enumerable: true,
445
- configurable: true,
446
- writable: true,
447
- value: {}
448
- });
449
- Object.defineProperty(this, "response", {
450
- enumerable: true,
451
- configurable: true,
452
- writable: true,
453
- value: {}
454
- });
455
- Object.defineProperty(this, "chunk", {
456
- enumerable: true,
457
- configurable: true,
458
- writable: true,
459
- value: []
460
- });
461
- }
462
- handleCustomRequestEvent(_eventName, _eventInfo, data, _runId, _tags, _metadata) {
463
- this.request = data;
464
- }
465
- handleCustomResponseEvent(_eventName, _eventInfo, data, _runId, _tags, _metadata) {
466
- this.response = data;
467
- }
468
- handleCustomChunkEvent(_eventName, _eventInfo, data, _runId, _tags, _metadata) {
469
- this.chunk.push(data);
470
- }
471
- }
472
326
  exports.GoogleRequestRecorder = GoogleRequestRecorder;
327
+ //# sourceMappingURL=connection.cjs.map