@posthog/ai 6.5.1 → 7.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/dist/anthropic/index.cjs +13 -3
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.mjs +13 -3
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +74 -5
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.mjs +74 -5
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/index.cjs +590 -373
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +590 -373
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +330 -346
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.mjs +330 -346
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/openai/index.cjs +152 -15
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.d.ts +1 -1
- package/dist/openai/index.mjs +152 -15
- package/dist/openai/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +157 -8
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +157 -8
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +10 -10
package/dist/langchain/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import 'buffer';
|
|
2
2
|
import * as uuid from 'uuid';
|
|
3
3
|
|
|
4
|
-
var version = "
|
|
4
|
+
var version = "7.0.0";
|
|
5
5
|
|
|
6
6
|
// Type guards for safer type checking
|
|
7
7
|
|
|
@@ -54,6 +54,15 @@ const withPrivacyMode = (client, privacyMode, input) => {
|
|
|
54
54
|
return client.privacy_mode || privacyMode ? null : input;
|
|
55
55
|
};
|
|
56
56
|
|
|
57
|
+
//#region rolldown:runtime
|
|
58
|
+
var __defProp = Object.defineProperty;
|
|
59
|
+
var __export = (target, all) => {
|
|
60
|
+
for (var name in all) __defProp(target, name, {
|
|
61
|
+
get: all[name],
|
|
62
|
+
enumerable: true
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
|
|
57
66
|
function getDefaultExportFromCjs (x) {
|
|
58
67
|
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
59
68
|
}
|
|
@@ -206,370 +215,304 @@ function requireCamelcase () {
|
|
|
206
215
|
|
|
207
216
|
requireCamelcase();
|
|
208
217
|
|
|
218
|
+
//#region src/load/map_keys.ts
|
|
209
219
|
function keyToJson(key, map) {
|
|
210
|
-
|
|
220
|
+
return map?.[key] || snakeCase(key);
|
|
211
221
|
}
|
|
212
222
|
function mapKeys(fields, mapper, map) {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
mapped[mapper(key, map)] = fields[key];
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
return mapped;
|
|
223
|
+
const mapped = {};
|
|
224
|
+
for (const key in fields) if (Object.hasOwn(fields, key)) mapped[mapper(key, map)] = fields[key];
|
|
225
|
+
return mapped;
|
|
220
226
|
}
|
|
221
227
|
|
|
228
|
+
//#region src/load/serializable.ts
|
|
229
|
+
var serializable_exports = {};
|
|
230
|
+
__export(serializable_exports, {
|
|
231
|
+
Serializable: () => Serializable,
|
|
232
|
+
get_lc_unique_name: () => get_lc_unique_name
|
|
233
|
+
});
|
|
222
234
|
function shallowCopy(obj) {
|
|
223
|
-
|
|
235
|
+
return Array.isArray(obj) ? [...obj] : { ...obj };
|
|
224
236
|
}
|
|
225
237
|
function replaceSecrets(root, secretsMap) {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
id: [secretId],
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
return result;
|
|
238
|
+
const result = shallowCopy(root);
|
|
239
|
+
for (const [path, secretId] of Object.entries(secretsMap)) {
|
|
240
|
+
const [last, ...partsReverse] = path.split(".").reverse();
|
|
241
|
+
let current = result;
|
|
242
|
+
for (const part of partsReverse.reverse()) {
|
|
243
|
+
if (current[part] === void 0) break;
|
|
244
|
+
current[part] = shallowCopy(current[part]);
|
|
245
|
+
current = current[part];
|
|
246
|
+
}
|
|
247
|
+
if (current[last] !== void 0) current[last] = {
|
|
248
|
+
lc: 1,
|
|
249
|
+
type: "secret",
|
|
250
|
+
id: [secretId]
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
return result;
|
|
247
254
|
}
|
|
248
255
|
/**
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
function get_lc_unique_name(
|
|
253
|
-
|
|
254
|
-
serializableClass)
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
const parentClass = Object.getPrototypeOf(serializableClass);
|
|
258
|
-
const lcNameIsSubclassed = typeof serializableClass.lc_name === "function" &&
|
|
259
|
-
(typeof parentClass.lc_name !== "function" ||
|
|
260
|
-
serializableClass.lc_name() !== parentClass.lc_name());
|
|
261
|
-
if (lcNameIsSubclassed) {
|
|
262
|
-
return serializableClass.lc_name();
|
|
263
|
-
}
|
|
264
|
-
else {
|
|
265
|
-
return serializableClass.name;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
class Serializable {
|
|
269
|
-
/**
|
|
270
|
-
* The name of the serializable. Override to provide an alias or
|
|
271
|
-
* to preserve the serialized module name in minified environments.
|
|
272
|
-
*
|
|
273
|
-
* Implemented as a static method to support loading logic.
|
|
274
|
-
*/
|
|
275
|
-
static lc_name() {
|
|
276
|
-
return this.name;
|
|
277
|
-
}
|
|
278
|
-
/**
|
|
279
|
-
* The final serialized identifier for the module.
|
|
280
|
-
*/
|
|
281
|
-
get lc_id() {
|
|
282
|
-
return [
|
|
283
|
-
...this.lc_namespace,
|
|
284
|
-
get_lc_unique_name(this.constructor),
|
|
285
|
-
];
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* A map of secrets, which will be omitted from serialization.
|
|
289
|
-
* Keys are paths to the secret in constructor args, e.g. "foo.bar.baz".
|
|
290
|
-
* Values are the secret ids, which will be used when deserializing.
|
|
291
|
-
*/
|
|
292
|
-
get lc_secrets() {
|
|
293
|
-
return undefined;
|
|
294
|
-
}
|
|
295
|
-
/**
|
|
296
|
-
* A map of additional attributes to merge with constructor args.
|
|
297
|
-
* Keys are the attribute names, e.g. "foo".
|
|
298
|
-
* Values are the attribute values, which will be serialized.
|
|
299
|
-
* These attributes need to be accepted by the constructor as arguments.
|
|
300
|
-
*/
|
|
301
|
-
get lc_attributes() {
|
|
302
|
-
return undefined;
|
|
303
|
-
}
|
|
304
|
-
/**
|
|
305
|
-
* A map of aliases for constructor args.
|
|
306
|
-
* Keys are the attribute names, e.g. "foo".
|
|
307
|
-
* Values are the alias that will replace the key in serialization.
|
|
308
|
-
* This is used to eg. make argument names match Python.
|
|
309
|
-
*/
|
|
310
|
-
get lc_aliases() {
|
|
311
|
-
return undefined;
|
|
312
|
-
}
|
|
313
|
-
/**
|
|
314
|
-
* A manual list of keys that should be serialized.
|
|
315
|
-
* If not overridden, all fields passed into the constructor will be serialized.
|
|
316
|
-
*/
|
|
317
|
-
get lc_serializable_keys() {
|
|
318
|
-
return undefined;
|
|
319
|
-
}
|
|
320
|
-
constructor(kwargs, ..._args) {
|
|
321
|
-
Object.defineProperty(this, "lc_serializable", {
|
|
322
|
-
enumerable: true,
|
|
323
|
-
configurable: true,
|
|
324
|
-
writable: true,
|
|
325
|
-
value: false
|
|
326
|
-
});
|
|
327
|
-
Object.defineProperty(this, "lc_kwargs", {
|
|
328
|
-
enumerable: true,
|
|
329
|
-
configurable: true,
|
|
330
|
-
writable: true,
|
|
331
|
-
value: void 0
|
|
332
|
-
});
|
|
333
|
-
if (this.lc_serializable_keys !== undefined) {
|
|
334
|
-
this.lc_kwargs = Object.fromEntries(Object.entries(kwargs || {}).filter(([key]) => this.lc_serializable_keys?.includes(key)));
|
|
335
|
-
}
|
|
336
|
-
else {
|
|
337
|
-
this.lc_kwargs = kwargs ?? {};
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
toJSON() {
|
|
341
|
-
if (!this.lc_serializable) {
|
|
342
|
-
return this.toJSONNotImplemented();
|
|
343
|
-
}
|
|
344
|
-
if (
|
|
345
|
-
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
346
|
-
this.lc_kwargs instanceof Serializable ||
|
|
347
|
-
typeof this.lc_kwargs !== "object" ||
|
|
348
|
-
Array.isArray(this.lc_kwargs)) {
|
|
349
|
-
// We do not support serialization of classes with arg not a POJO
|
|
350
|
-
// I'm aware the check above isn't as strict as it could be
|
|
351
|
-
return this.toJSONNotImplemented();
|
|
352
|
-
}
|
|
353
|
-
const aliases = {};
|
|
354
|
-
const secrets = {};
|
|
355
|
-
const kwargs = Object.keys(this.lc_kwargs).reduce((acc, key) => {
|
|
356
|
-
acc[key] = key in this ? this[key] : this.lc_kwargs[key];
|
|
357
|
-
return acc;
|
|
358
|
-
}, {});
|
|
359
|
-
// get secrets, attributes and aliases from all superclasses
|
|
360
|
-
for (
|
|
361
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
362
|
-
let current = Object.getPrototypeOf(this); current; current = Object.getPrototypeOf(current)) {
|
|
363
|
-
Object.assign(aliases, Reflect.get(current, "lc_aliases", this));
|
|
364
|
-
Object.assign(secrets, Reflect.get(current, "lc_secrets", this));
|
|
365
|
-
Object.assign(kwargs, Reflect.get(current, "lc_attributes", this));
|
|
366
|
-
}
|
|
367
|
-
// include all secrets used, even if not in kwargs,
|
|
368
|
-
// will be replaced with sentinel value in replaceSecrets
|
|
369
|
-
Object.keys(secrets).forEach((keyPath) => {
|
|
370
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias, @typescript-eslint/no-explicit-any
|
|
371
|
-
let read = this;
|
|
372
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
373
|
-
let write = kwargs;
|
|
374
|
-
const [last, ...partsReverse] = keyPath.split(".").reverse();
|
|
375
|
-
for (const key of partsReverse.reverse()) {
|
|
376
|
-
if (!(key in read) || read[key] === undefined)
|
|
377
|
-
return;
|
|
378
|
-
if (!(key in write) || write[key] === undefined) {
|
|
379
|
-
if (typeof read[key] === "object" && read[key] != null) {
|
|
380
|
-
write[key] = {};
|
|
381
|
-
}
|
|
382
|
-
else if (Array.isArray(read[key])) {
|
|
383
|
-
write[key] = [];
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
read = read[key];
|
|
387
|
-
write = write[key];
|
|
388
|
-
}
|
|
389
|
-
if (last in read && read[last] !== undefined) {
|
|
390
|
-
write[last] = write[last] || read[last];
|
|
391
|
-
}
|
|
392
|
-
});
|
|
393
|
-
return {
|
|
394
|
-
lc: 1,
|
|
395
|
-
type: "constructor",
|
|
396
|
-
id: this.lc_id,
|
|
397
|
-
kwargs: mapKeys(Object.keys(secrets).length ? replaceSecrets(kwargs, secrets) : kwargs, keyToJson, aliases),
|
|
398
|
-
};
|
|
399
|
-
}
|
|
400
|
-
toJSONNotImplemented() {
|
|
401
|
-
return {
|
|
402
|
-
lc: 1,
|
|
403
|
-
type: "not_implemented",
|
|
404
|
-
id: this.lc_id,
|
|
405
|
-
};
|
|
406
|
-
}
|
|
256
|
+
* Get a unique name for the module, rather than parent class implementations.
|
|
257
|
+
* Should not be subclassed, subclass lc_name above instead.
|
|
258
|
+
*/
|
|
259
|
+
function get_lc_unique_name(serializableClass) {
|
|
260
|
+
const parentClass = Object.getPrototypeOf(serializableClass);
|
|
261
|
+
const lcNameIsSubclassed = typeof serializableClass.lc_name === "function" && (typeof parentClass.lc_name !== "function" || serializableClass.lc_name() !== parentClass.lc_name());
|
|
262
|
+
if (lcNameIsSubclassed) return serializableClass.lc_name();
|
|
263
|
+
else return serializableClass.name;
|
|
407
264
|
}
|
|
265
|
+
var Serializable = class Serializable {
|
|
266
|
+
lc_serializable = false;
|
|
267
|
+
lc_kwargs;
|
|
268
|
+
/**
|
|
269
|
+
* The name of the serializable. Override to provide an alias or
|
|
270
|
+
* to preserve the serialized module name in minified environments.
|
|
271
|
+
*
|
|
272
|
+
* Implemented as a static method to support loading logic.
|
|
273
|
+
*/
|
|
274
|
+
static lc_name() {
|
|
275
|
+
return this.name;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* The final serialized identifier for the module.
|
|
279
|
+
*/
|
|
280
|
+
get lc_id() {
|
|
281
|
+
return [...this.lc_namespace, get_lc_unique_name(this.constructor)];
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* A map of secrets, which will be omitted from serialization.
|
|
285
|
+
* Keys are paths to the secret in constructor args, e.g. "foo.bar.baz".
|
|
286
|
+
* Values are the secret ids, which will be used when deserializing.
|
|
287
|
+
*/
|
|
288
|
+
get lc_secrets() {
|
|
289
|
+
return void 0;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* A map of additional attributes to merge with constructor args.
|
|
293
|
+
* Keys are the attribute names, e.g. "foo".
|
|
294
|
+
* Values are the attribute values, which will be serialized.
|
|
295
|
+
* These attributes need to be accepted by the constructor as arguments.
|
|
296
|
+
*/
|
|
297
|
+
get lc_attributes() {
|
|
298
|
+
return void 0;
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* A map of aliases for constructor args.
|
|
302
|
+
* Keys are the attribute names, e.g. "foo".
|
|
303
|
+
* Values are the alias that will replace the key in serialization.
|
|
304
|
+
* This is used to eg. make argument names match Python.
|
|
305
|
+
*/
|
|
306
|
+
get lc_aliases() {
|
|
307
|
+
return void 0;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* A manual list of keys that should be serialized.
|
|
311
|
+
* If not overridden, all fields passed into the constructor will be serialized.
|
|
312
|
+
*/
|
|
313
|
+
get lc_serializable_keys() {
|
|
314
|
+
return void 0;
|
|
315
|
+
}
|
|
316
|
+
constructor(kwargs, ..._args) {
|
|
317
|
+
if (this.lc_serializable_keys !== void 0) this.lc_kwargs = Object.fromEntries(Object.entries(kwargs || {}).filter(([key]) => this.lc_serializable_keys?.includes(key)));
|
|
318
|
+
else this.lc_kwargs = kwargs ?? {};
|
|
319
|
+
}
|
|
320
|
+
toJSON() {
|
|
321
|
+
if (!this.lc_serializable) return this.toJSONNotImplemented();
|
|
322
|
+
if (this.lc_kwargs instanceof Serializable || typeof this.lc_kwargs !== "object" || Array.isArray(this.lc_kwargs)) return this.toJSONNotImplemented();
|
|
323
|
+
const aliases = {};
|
|
324
|
+
const secrets = {};
|
|
325
|
+
const kwargs = Object.keys(this.lc_kwargs).reduce((acc, key) => {
|
|
326
|
+
acc[key] = key in this ? this[key] : this.lc_kwargs[key];
|
|
327
|
+
return acc;
|
|
328
|
+
}, {});
|
|
329
|
+
for (let current = Object.getPrototypeOf(this); current; current = Object.getPrototypeOf(current)) {
|
|
330
|
+
Object.assign(aliases, Reflect.get(current, "lc_aliases", this));
|
|
331
|
+
Object.assign(secrets, Reflect.get(current, "lc_secrets", this));
|
|
332
|
+
Object.assign(kwargs, Reflect.get(current, "lc_attributes", this));
|
|
333
|
+
}
|
|
334
|
+
Object.keys(secrets).forEach((keyPath) => {
|
|
335
|
+
let read = this;
|
|
336
|
+
let write = kwargs;
|
|
337
|
+
const [last, ...partsReverse] = keyPath.split(".").reverse();
|
|
338
|
+
for (const key of partsReverse.reverse()) {
|
|
339
|
+
if (!(key in read) || read[key] === void 0) return;
|
|
340
|
+
if (!(key in write) || write[key] === void 0) {
|
|
341
|
+
if (typeof read[key] === "object" && read[key] != null) write[key] = {};
|
|
342
|
+
else if (Array.isArray(read[key])) write[key] = [];
|
|
343
|
+
}
|
|
344
|
+
read = read[key];
|
|
345
|
+
write = write[key];
|
|
346
|
+
}
|
|
347
|
+
if (last in read && read[last] !== void 0) write[last] = write[last] || read[last];
|
|
348
|
+
});
|
|
349
|
+
return {
|
|
350
|
+
lc: 1,
|
|
351
|
+
type: "constructor",
|
|
352
|
+
id: this.lc_id,
|
|
353
|
+
kwargs: mapKeys(Object.keys(secrets).length ? replaceSecrets(kwargs, secrets) : kwargs, keyToJson, aliases)
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
toJSONNotImplemented() {
|
|
357
|
+
return {
|
|
358
|
+
lc: 1,
|
|
359
|
+
type: "not_implemented",
|
|
360
|
+
id: this.lc_id
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
};
|
|
408
364
|
|
|
409
|
-
|
|
410
|
-
|
|
365
|
+
//#region src/utils/env.ts
|
|
366
|
+
var env_exports = {};
|
|
367
|
+
__export(env_exports, {
|
|
368
|
+
getEnv: () => getEnv,
|
|
369
|
+
getEnvironmentVariable: () => getEnvironmentVariable,
|
|
370
|
+
getRuntimeEnvironment: () => getRuntimeEnvironment,
|
|
371
|
+
isBrowser: () => isBrowser,
|
|
372
|
+
isDeno: () => isDeno,
|
|
373
|
+
isJsDom: () => isJsDom,
|
|
374
|
+
isNode: () => isNode,
|
|
375
|
+
isWebWorker: () => isWebWorker
|
|
376
|
+
});
|
|
377
|
+
const isBrowser = () => typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
378
|
+
const isWebWorker = () => typeof globalThis === "object" && globalThis.constructor && globalThis.constructor.name === "DedicatedWorkerGlobalScope";
|
|
379
|
+
const isJsDom = () => typeof window !== "undefined" && window.name === "nodejs" || typeof navigator !== "undefined" && navigator.userAgent.includes("jsdom");
|
|
411
380
|
const isDeno = () => typeof Deno !== "undefined";
|
|
381
|
+
const isNode = () => typeof process !== "undefined" && typeof process.versions !== "undefined" && typeof process.versions.node !== "undefined" && !isDeno();
|
|
382
|
+
const getEnv = () => {
|
|
383
|
+
let env;
|
|
384
|
+
if (isBrowser()) env = "browser";
|
|
385
|
+
else if (isNode()) env = "node";
|
|
386
|
+
else if (isWebWorker()) env = "webworker";
|
|
387
|
+
else if (isJsDom()) env = "jsdom";
|
|
388
|
+
else if (isDeno()) env = "deno";
|
|
389
|
+
else env = "other";
|
|
390
|
+
return env;
|
|
391
|
+
};
|
|
392
|
+
let runtimeEnvironment;
|
|
393
|
+
function getRuntimeEnvironment() {
|
|
394
|
+
if (runtimeEnvironment === void 0) {
|
|
395
|
+
const env = getEnv();
|
|
396
|
+
runtimeEnvironment = {
|
|
397
|
+
library: "langchain-js",
|
|
398
|
+
runtime: env
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
return runtimeEnvironment;
|
|
402
|
+
}
|
|
412
403
|
function getEnvironmentVariable(name) {
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
else if (isDeno()) {
|
|
421
|
-
return Deno?.env.get(name);
|
|
422
|
-
}
|
|
423
|
-
else {
|
|
424
|
-
return undefined;
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
catch (e) {
|
|
428
|
-
return undefined;
|
|
429
|
-
}
|
|
404
|
+
try {
|
|
405
|
+
if (typeof process !== "undefined") return process.env?.[name];
|
|
406
|
+
else if (isDeno()) return Deno?.env.get(name);
|
|
407
|
+
else return void 0;
|
|
408
|
+
} catch {
|
|
409
|
+
return void 0;
|
|
410
|
+
}
|
|
430
411
|
}
|
|
431
412
|
|
|
413
|
+
//#region src/callbacks/base.ts
|
|
414
|
+
var base_exports = {};
|
|
415
|
+
__export(base_exports, {
|
|
416
|
+
BaseCallbackHandler: () => BaseCallbackHandler,
|
|
417
|
+
callbackHandlerPrefersStreaming: () => callbackHandlerPrefersStreaming,
|
|
418
|
+
isBaseCallbackHandler: () => isBaseCallbackHandler
|
|
419
|
+
});
|
|
432
420
|
/**
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
421
|
+
* Abstract class that provides a set of optional methods that can be
|
|
422
|
+
* overridden in derived classes to handle various events during the
|
|
423
|
+
* execution of a LangChain application.
|
|
424
|
+
*/
|
|
425
|
+
var BaseCallbackHandlerMethodsClass = class {};
|
|
426
|
+
function callbackHandlerPrefersStreaming(x) {
|
|
427
|
+
return "lc_prefer_streaming" in x && x.lc_prefer_streaming;
|
|
438
428
|
}
|
|
439
429
|
/**
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
writable: true,
|
|
527
|
-
value: false
|
|
528
|
-
});
|
|
529
|
-
Object.defineProperty(this, "awaitHandlers", {
|
|
530
|
-
enumerable: true,
|
|
531
|
-
configurable: true,
|
|
532
|
-
writable: true,
|
|
533
|
-
value: getEnvironmentVariable("LANGCHAIN_CALLBACKS_BACKGROUND") === "false"
|
|
534
|
-
});
|
|
535
|
-
this.lc_kwargs = input || {};
|
|
536
|
-
if (input) {
|
|
537
|
-
this.ignoreLLM = input.ignoreLLM ?? this.ignoreLLM;
|
|
538
|
-
this.ignoreChain = input.ignoreChain ?? this.ignoreChain;
|
|
539
|
-
this.ignoreAgent = input.ignoreAgent ?? this.ignoreAgent;
|
|
540
|
-
this.ignoreRetriever = input.ignoreRetriever ?? this.ignoreRetriever;
|
|
541
|
-
this.ignoreCustomEvent =
|
|
542
|
-
input.ignoreCustomEvent ?? this.ignoreCustomEvent;
|
|
543
|
-
this.raiseError = input.raiseError ?? this.raiseError;
|
|
544
|
-
this.awaitHandlers =
|
|
545
|
-
this.raiseError || (input._awaitHandler ?? this.awaitHandlers);
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
copy() {
|
|
549
|
-
return new this.constructor(this);
|
|
550
|
-
}
|
|
551
|
-
toJSON() {
|
|
552
|
-
return Serializable.prototype.toJSON.call(this);
|
|
553
|
-
}
|
|
554
|
-
toJSONNotImplemented() {
|
|
555
|
-
return Serializable.prototype.toJSONNotImplemented.call(this);
|
|
556
|
-
}
|
|
557
|
-
static fromMethods(methods) {
|
|
558
|
-
class Handler extends BaseCallbackHandler {
|
|
559
|
-
constructor() {
|
|
560
|
-
super();
|
|
561
|
-
Object.defineProperty(this, "name", {
|
|
562
|
-
enumerable: true,
|
|
563
|
-
configurable: true,
|
|
564
|
-
writable: true,
|
|
565
|
-
value: uuid.v4()
|
|
566
|
-
});
|
|
567
|
-
Object.assign(this, methods);
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
return new Handler();
|
|
571
|
-
}
|
|
572
|
-
}
|
|
430
|
+
* Abstract base class for creating callback handlers in the LangChain
|
|
431
|
+
* framework. It provides a set of optional methods that can be overridden
|
|
432
|
+
* in derived classes to handle various events during the execution of a
|
|
433
|
+
* LangChain application.
|
|
434
|
+
*/
|
|
435
|
+
var BaseCallbackHandler = class extends BaseCallbackHandlerMethodsClass {
|
|
436
|
+
lc_serializable = false;
|
|
437
|
+
get lc_namespace() {
|
|
438
|
+
return [
|
|
439
|
+
"langchain_core",
|
|
440
|
+
"callbacks",
|
|
441
|
+
this.name
|
|
442
|
+
];
|
|
443
|
+
}
|
|
444
|
+
get lc_secrets() {
|
|
445
|
+
return void 0;
|
|
446
|
+
}
|
|
447
|
+
get lc_attributes() {
|
|
448
|
+
return void 0;
|
|
449
|
+
}
|
|
450
|
+
get lc_aliases() {
|
|
451
|
+
return void 0;
|
|
452
|
+
}
|
|
453
|
+
get lc_serializable_keys() {
|
|
454
|
+
return void 0;
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* The name of the serializable. Override to provide an alias or
|
|
458
|
+
* to preserve the serialized module name in minified environments.
|
|
459
|
+
*
|
|
460
|
+
* Implemented as a static method to support loading logic.
|
|
461
|
+
*/
|
|
462
|
+
static lc_name() {
|
|
463
|
+
return this.name;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* The final serialized identifier for the module.
|
|
467
|
+
*/
|
|
468
|
+
get lc_id() {
|
|
469
|
+
return [...this.lc_namespace, get_lc_unique_name(this.constructor)];
|
|
470
|
+
}
|
|
471
|
+
lc_kwargs;
|
|
472
|
+
ignoreLLM = false;
|
|
473
|
+
ignoreChain = false;
|
|
474
|
+
ignoreAgent = false;
|
|
475
|
+
ignoreRetriever = false;
|
|
476
|
+
ignoreCustomEvent = false;
|
|
477
|
+
raiseError = false;
|
|
478
|
+
awaitHandlers = getEnvironmentVariable("LANGCHAIN_CALLBACKS_BACKGROUND") === "false";
|
|
479
|
+
constructor(input) {
|
|
480
|
+
super();
|
|
481
|
+
this.lc_kwargs = input || {};
|
|
482
|
+
if (input) {
|
|
483
|
+
this.ignoreLLM = input.ignoreLLM ?? this.ignoreLLM;
|
|
484
|
+
this.ignoreChain = input.ignoreChain ?? this.ignoreChain;
|
|
485
|
+
this.ignoreAgent = input.ignoreAgent ?? this.ignoreAgent;
|
|
486
|
+
this.ignoreRetriever = input.ignoreRetriever ?? this.ignoreRetriever;
|
|
487
|
+
this.ignoreCustomEvent = input.ignoreCustomEvent ?? this.ignoreCustomEvent;
|
|
488
|
+
this.raiseError = input.raiseError ?? this.raiseError;
|
|
489
|
+
this.awaitHandlers = this.raiseError || (input._awaitHandler ?? this.awaitHandlers);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
copy() {
|
|
493
|
+
return new this.constructor(this);
|
|
494
|
+
}
|
|
495
|
+
toJSON() {
|
|
496
|
+
return Serializable.prototype.toJSON.call(this);
|
|
497
|
+
}
|
|
498
|
+
toJSONNotImplemented() {
|
|
499
|
+
return Serializable.prototype.toJSONNotImplemented.call(this);
|
|
500
|
+
}
|
|
501
|
+
static fromMethods(methods) {
|
|
502
|
+
class Handler extends BaseCallbackHandler {
|
|
503
|
+
name = uuid.v4();
|
|
504
|
+
constructor() {
|
|
505
|
+
super();
|
|
506
|
+
Object.assign(this, methods);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
return new Handler();
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
const isBaseCallbackHandler = (x) => {
|
|
513
|
+
const callbackHandler = x;
|
|
514
|
+
return callbackHandler !== void 0 && typeof callbackHandler.copy === "function" && typeof callbackHandler.name === "string" && typeof callbackHandler.awaitHandlers === "boolean";
|
|
515
|
+
};
|
|
573
516
|
|
|
574
517
|
const REDACTED_IMAGE_PLACEHOLDER = '[base64 image redacted]';
|
|
575
518
|
|
|
@@ -1001,6 +944,9 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
1001
944
|
if (additionalTokenData.reasoningTokens) {
|
|
1002
945
|
eventProperties['$ai_reasoning_tokens'] = additionalTokenData.reasoningTokens;
|
|
1003
946
|
}
|
|
947
|
+
if (additionalTokenData.webSearchCount !== undefined) {
|
|
948
|
+
eventProperties['$ai_web_search_count'] = additionalTokenData.webSearchCount;
|
|
949
|
+
}
|
|
1004
950
|
|
|
1005
951
|
// Handle generations/completions
|
|
1006
952
|
let completions;
|
|
@@ -1172,6 +1118,44 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
1172
1118
|
additionalTokenData.reasoningTokens = usage.reasoningTokens;
|
|
1173
1119
|
}
|
|
1174
1120
|
|
|
1121
|
+
// Extract web search counts from various provider formats
|
|
1122
|
+
let webSearchCount;
|
|
1123
|
+
|
|
1124
|
+
// Priority 1: Exact Count
|
|
1125
|
+
// Check Anthropic format (server_tool_use.web_search_requests)
|
|
1126
|
+
if (usage.server_tool_use?.web_search_requests !== undefined) {
|
|
1127
|
+
webSearchCount = usage.server_tool_use.web_search_requests;
|
|
1128
|
+
}
|
|
1129
|
+
// Priority 2: Binary Detection (1 or 0)
|
|
1130
|
+
// Check for citations array (Perplexity)
|
|
1131
|
+
else if (usage.citations && Array.isArray(usage.citations) && usage.citations.length > 0) {
|
|
1132
|
+
webSearchCount = 1;
|
|
1133
|
+
}
|
|
1134
|
+
// Check for search_results array (Perplexity via OpenRouter)
|
|
1135
|
+
else if (usage.search_results && Array.isArray(usage.search_results) && usage.search_results.length > 0) {
|
|
1136
|
+
webSearchCount = 1;
|
|
1137
|
+
}
|
|
1138
|
+
// Check for search_context_size (Perplexity via OpenRouter)
|
|
1139
|
+
else if (usage.search_context_size) {
|
|
1140
|
+
webSearchCount = 1;
|
|
1141
|
+
}
|
|
1142
|
+
// Check for annotations with url_citation type
|
|
1143
|
+
else if (usage.annotations && Array.isArray(usage.annotations)) {
|
|
1144
|
+
const hasUrlCitation = usage.annotations.some(ann => {
|
|
1145
|
+
return ann && typeof ann === 'object' && 'type' in ann && ann.type === 'url_citation';
|
|
1146
|
+
});
|
|
1147
|
+
if (hasUrlCitation) {
|
|
1148
|
+
webSearchCount = 1;
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
// Check Gemini format (grounding metadata - binary 0 or 1)
|
|
1152
|
+
else if (usage.grounding_metadata?.grounding_support !== undefined || usage.grounding_metadata?.web_search_queries !== undefined) {
|
|
1153
|
+
webSearchCount = 1;
|
|
1154
|
+
}
|
|
1155
|
+
if (webSearchCount !== undefined) {
|
|
1156
|
+
additionalTokenData.webSearchCount = webSearchCount;
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1175
1159
|
// In LangChain, input_tokens is the sum of input and cache read tokens.
|
|
1176
1160
|
// Our cost calculation expects them to be separate, for Anthropic.
|
|
1177
1161
|
if (parsedUsage.input && additionalTokenData.cacheReadInputTokens) {
|