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