@posthog/ai 7.21.0 → 8.0.1

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.
@@ -1,4 +1,5 @@
1
- import * as uuid from 'uuid';
1
+ import 'uuid';
2
+ import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
2
3
 
3
4
  const DATA_URL_PREFIX_RE = /^data:([^;,\s]+)(?:;[^;,\s]+)*;base64,/i;
4
5
  const BASE64_ALPHABET_RE = /^[A-Za-z0-9+/_=-]+$/;
@@ -214,470 +215,7 @@ function sanitizeValues(obj) {
214
215
  return jsonSafe;
215
216
  }
216
217
 
217
- function getDefaultExportFromCjs (x) {
218
- return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
219
- }
220
-
221
- var decamelize;
222
- var hasRequiredDecamelize;
223
-
224
- function requireDecamelize () {
225
- if (hasRequiredDecamelize) return decamelize;
226
- hasRequiredDecamelize = 1;
227
- decamelize = function (str, sep) {
228
- if (typeof str !== 'string') {
229
- throw new TypeError('Expected a string');
230
- }
231
-
232
- sep = typeof sep === 'undefined' ? '_' : sep;
233
-
234
- return str
235
- .replace(/([a-z\d])([A-Z])/g, '$1' + sep + '$2')
236
- .replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + sep + '$2')
237
- .toLowerCase();
238
- };
239
- return decamelize;
240
- }
241
-
242
- var decamelizeExports = requireDecamelize();
243
- var snakeCase = /*@__PURE__*/getDefaultExportFromCjs(decamelizeExports);
244
-
245
- var camelcase = {exports: {}};
246
-
247
- var hasRequiredCamelcase;
248
-
249
- function requireCamelcase () {
250
- if (hasRequiredCamelcase) return camelcase.exports;
251
- hasRequiredCamelcase = 1;
252
-
253
- const UPPERCASE = /[\p{Lu}]/u;
254
- const LOWERCASE = /[\p{Ll}]/u;
255
- const LEADING_CAPITAL = /^[\p{Lu}](?![\p{Lu}])/gu;
256
- const IDENTIFIER = /([\p{Alpha}\p{N}_]|$)/u;
257
- const SEPARATORS = /[_.\- ]+/;
258
-
259
- const LEADING_SEPARATORS = new RegExp('^' + SEPARATORS.source);
260
- const SEPARATORS_AND_IDENTIFIER = new RegExp(SEPARATORS.source + IDENTIFIER.source, 'gu');
261
- const NUMBERS_AND_IDENTIFIER = new RegExp('\\d+' + IDENTIFIER.source, 'gu');
262
-
263
- const preserveCamelCase = (string, toLowerCase, toUpperCase) => {
264
- let isLastCharLower = false;
265
- let isLastCharUpper = false;
266
- let isLastLastCharUpper = false;
267
-
268
- for (let i = 0; i < string.length; i++) {
269
- const character = string[i];
270
-
271
- if (isLastCharLower && UPPERCASE.test(character)) {
272
- string = string.slice(0, i) + '-' + string.slice(i);
273
- isLastCharLower = false;
274
- isLastLastCharUpper = isLastCharUpper;
275
- isLastCharUpper = true;
276
- i++;
277
- } else if (isLastCharUpper && isLastLastCharUpper && LOWERCASE.test(character)) {
278
- string = string.slice(0, i - 1) + '-' + string.slice(i - 1);
279
- isLastLastCharUpper = isLastCharUpper;
280
- isLastCharUpper = false;
281
- isLastCharLower = true;
282
- } else {
283
- isLastCharLower = toLowerCase(character) === character && toUpperCase(character) !== character;
284
- isLastLastCharUpper = isLastCharUpper;
285
- isLastCharUpper = toUpperCase(character) === character && toLowerCase(character) !== character;
286
- }
287
- }
288
-
289
- return string;
290
- };
291
-
292
- const preserveConsecutiveUppercase = (input, toLowerCase) => {
293
- LEADING_CAPITAL.lastIndex = 0;
294
-
295
- return input.replace(LEADING_CAPITAL, m1 => toLowerCase(m1));
296
- };
297
-
298
- const postProcess = (input, toUpperCase) => {
299
- SEPARATORS_AND_IDENTIFIER.lastIndex = 0;
300
- NUMBERS_AND_IDENTIFIER.lastIndex = 0;
301
-
302
- return input.replace(SEPARATORS_AND_IDENTIFIER, (_, identifier) => toUpperCase(identifier))
303
- .replace(NUMBERS_AND_IDENTIFIER, m => toUpperCase(m));
304
- };
305
-
306
- const camelCase = (input, options) => {
307
- if (!(typeof input === 'string' || Array.isArray(input))) {
308
- throw new TypeError('Expected the input to be `string | string[]`');
309
- }
310
-
311
- options = {
312
- pascalCase: false,
313
- preserveConsecutiveUppercase: false,
314
- ...options
315
- };
316
-
317
- if (Array.isArray(input)) {
318
- input = input.map(x => x.trim())
319
- .filter(x => x.length)
320
- .join('-');
321
- } else {
322
- input = input.trim();
323
- }
324
-
325
- if (input.length === 0) {
326
- return '';
327
- }
328
-
329
- const toLowerCase = options.locale === false ?
330
- string => string.toLowerCase() :
331
- string => string.toLocaleLowerCase(options.locale);
332
- const toUpperCase = options.locale === false ?
333
- string => string.toUpperCase() :
334
- string => string.toLocaleUpperCase(options.locale);
335
-
336
- if (input.length === 1) {
337
- return options.pascalCase ? toUpperCase(input) : toLowerCase(input);
338
- }
339
-
340
- const hasUpperCase = input !== toLowerCase(input);
341
-
342
- if (hasUpperCase) {
343
- input = preserveCamelCase(input, toLowerCase, toUpperCase);
344
- }
345
-
346
- input = input.replace(LEADING_SEPARATORS, '');
347
-
348
- if (options.preserveConsecutiveUppercase) {
349
- input = preserveConsecutiveUppercase(input, toLowerCase);
350
- } else {
351
- input = toLowerCase(input);
352
- }
353
-
354
- if (options.pascalCase) {
355
- input = toUpperCase(input.charAt(0)) + input.slice(1);
356
- }
357
-
358
- return postProcess(input, toUpperCase);
359
- };
360
-
361
- camelcase.exports = camelCase;
362
- // TODO: Remove this for the next major release
363
- camelcase.exports.default = camelCase;
364
- return camelcase.exports;
365
- }
366
-
367
- requireCamelcase();
368
-
369
- //#region src/load/map_keys.ts
370
- function keyToJson(key, map) {
371
- return map?.[key] || snakeCase(key);
372
- }
373
- function mapKeys(fields, mapper, map) {
374
- const mapped = {};
375
- for (const key in fields) if (Object.hasOwn(fields, key)) mapped[mapper(key, map)] = fields[key];
376
- return mapped;
377
- }
378
-
379
- //#region src/load/validation.ts
380
- /**
381
- * Sentinel key used to mark escaped user objects during serialization.
382
- *
383
- * When a plain object contains 'lc' key (which could be confused with LC objects),
384
- * we wrap it as `{"__lc_escaped__": {...original...}}`.
385
- */
386
- const LC_ESCAPED_KEY = "__lc_escaped__";
387
- /**
388
- * Check if an object needs escaping to prevent confusion with LC objects.
389
- *
390
- * An object needs escaping if:
391
- * 1. It has an `'lc'` key (could be confused with LC serialization format)
392
- * 2. It has only the escape key (would be mistaken for an escaped object)
393
- */
394
- function needsEscaping(obj) {
395
- return "lc" in obj || Object.keys(obj).length === 1 && LC_ESCAPED_KEY in obj;
396
- }
397
- /**
398
- * Wrap an object in the escape marker.
399
- *
400
- * @example
401
- * ```typescript
402
- * {"key": "value"} // becomes {"__lc_escaped__": {"key": "value"}}
403
- * ```
404
- */
405
- function escapeObject(obj) {
406
- return { [LC_ESCAPED_KEY]: obj };
407
- }
408
- /**
409
- * Check if an object looks like a Serializable instance (duck typing).
410
- */
411
- function isSerializableLike(obj) {
412
- return obj !== null && typeof obj === "object" && "lc_serializable" in obj && typeof obj.toJSON === "function";
413
- }
414
- /**
415
- * Create a "not_implemented" serialization result for objects that cannot be serialized.
416
- */
417
- function createNotImplemented(obj) {
418
- let id;
419
- if (obj !== null && typeof obj === "object") if ("lc_id" in obj && Array.isArray(obj.lc_id)) id = obj.lc_id;
420
- else id = [obj.constructor?.name ?? "Object"];
421
- else id = [typeof obj];
422
- return {
423
- lc: 1,
424
- type: "not_implemented",
425
- id
426
- };
427
- }
428
- /**
429
- * Escape a value if it needs escaping (contains `lc` key).
430
- *
431
- * This is a simpler version of `serializeValue` that doesn't handle Serializable
432
- * objects - it's meant to be called on kwargs values that have already been
433
- * processed by `toJSON()`.
434
- *
435
- * @param value - The value to potentially escape.
436
- * @param pathSet - WeakSet to track ancestor objects in the current path to detect circular references.
437
- * Objects are removed after processing to allow shared references (same object in
438
- * multiple places) while still detecting true circular references (ancestor in descendant).
439
- * @returns The value with any `lc`-containing objects wrapped in escape markers.
440
- */
441
- function escapeIfNeeded(value, pathSet = /* @__PURE__ */ new WeakSet()) {
442
- if (value !== null && typeof value === "object" && !Array.isArray(value)) {
443
- if (pathSet.has(value)) return createNotImplemented(value);
444
- if (isSerializableLike(value)) return value;
445
- pathSet.add(value);
446
- const record = value;
447
- if (needsEscaping(record)) {
448
- pathSet.delete(value);
449
- return escapeObject(record);
450
- }
451
- const result = {};
452
- for (const [key, val] of Object.entries(record)) result[key] = escapeIfNeeded(val, pathSet);
453
- pathSet.delete(value);
454
- return result;
455
- }
456
- if (Array.isArray(value)) return value.map((item) => escapeIfNeeded(item, pathSet));
457
- return value;
458
- }
459
-
460
- function shallowCopy(obj) {
461
- return Array.isArray(obj) ? [...obj] : { ...obj };
462
- }
463
- function replaceSecrets(root, secretsMap) {
464
- const result = shallowCopy(root);
465
- for (const [path, secretId] of Object.entries(secretsMap)) {
466
- const [last, ...partsReverse] = path.split(".").reverse();
467
- let current = result;
468
- for (const part of partsReverse.reverse()) {
469
- if (current[part] === void 0) break;
470
- current[part] = shallowCopy(current[part]);
471
- current = current[part];
472
- }
473
- if (current[last] !== void 0) current[last] = {
474
- lc: 1,
475
- type: "secret",
476
- id: [secretId]
477
- };
478
- }
479
- return result;
480
- }
481
- /**
482
- * Get a unique name for the module, rather than parent class implementations.
483
- * Should not be subclassed, subclass lc_name above instead.
484
- */
485
- function get_lc_unique_name(serializableClass) {
486
- const parentClass = Object.getPrototypeOf(serializableClass);
487
- if (typeof serializableClass.lc_name === "function" && (typeof parentClass.lc_name !== "function" || serializableClass.lc_name() !== parentClass.lc_name())) return serializableClass.lc_name();
488
- else return serializableClass.name;
489
- }
490
- var Serializable = class Serializable {
491
- lc_serializable = false;
492
- lc_kwargs;
493
- /**
494
- * The name of the serializable. Override to provide an alias or
495
- * to preserve the serialized module name in minified environments.
496
- *
497
- * Implemented as a static method to support loading logic.
498
- */
499
- static lc_name() {
500
- return this.name;
501
- }
502
- /**
503
- * The final serialized identifier for the module.
504
- */
505
- get lc_id() {
506
- return [...this.lc_namespace, get_lc_unique_name(this.constructor)];
507
- }
508
- /**
509
- * A map of secrets, which will be omitted from serialization.
510
- * Keys are paths to the secret in constructor args, e.g. "foo.bar.baz".
511
- * Values are the secret ids, which will be used when deserializing.
512
- */
513
- get lc_secrets() {}
514
- /**
515
- * A map of additional attributes to merge with constructor args.
516
- * Keys are the attribute names, e.g. "foo".
517
- * Values are the attribute values, which will be serialized.
518
- * These attributes need to be accepted by the constructor as arguments.
519
- */
520
- get lc_attributes() {}
521
- /**
522
- * A map of aliases for constructor args.
523
- * Keys are the attribute names, e.g. "foo".
524
- * Values are the alias that will replace the key in serialization.
525
- * This is used to eg. make argument names match Python.
526
- */
527
- get lc_aliases() {}
528
- /**
529
- * A manual list of keys that should be serialized.
530
- * If not overridden, all fields passed into the constructor will be serialized.
531
- */
532
- get lc_serializable_keys() {}
533
- constructor(kwargs, ..._args) {
534
- if (this.lc_serializable_keys !== void 0) this.lc_kwargs = Object.fromEntries(Object.entries(kwargs || {}).filter(([key]) => this.lc_serializable_keys?.includes(key)));
535
- else this.lc_kwargs = kwargs ?? {};
536
- }
537
- toJSON() {
538
- if (!this.lc_serializable) return this.toJSONNotImplemented();
539
- if (this.lc_kwargs instanceof Serializable || typeof this.lc_kwargs !== "object" || Array.isArray(this.lc_kwargs)) return this.toJSONNotImplemented();
540
- const aliases = {};
541
- const secrets = {};
542
- const kwargs = Object.keys(this.lc_kwargs).reduce((acc, key) => {
543
- acc[key] = key in this ? this[key] : this.lc_kwargs[key];
544
- return acc;
545
- }, {});
546
- for (let current = Object.getPrototypeOf(this); current; current = Object.getPrototypeOf(current)) {
547
- Object.assign(aliases, Reflect.get(current, "lc_aliases", this));
548
- Object.assign(secrets, Reflect.get(current, "lc_secrets", this));
549
- Object.assign(kwargs, Reflect.get(current, "lc_attributes", this));
550
- }
551
- Object.keys(secrets).forEach((keyPath) => {
552
- let read = this;
553
- let write = kwargs;
554
- const [last, ...partsReverse] = keyPath.split(".").reverse();
555
- for (const key of partsReverse.reverse()) {
556
- if (!(key in read) || read[key] === void 0) return;
557
- if (!(key in write) || write[key] === void 0) {
558
- if (typeof read[key] === "object" && read[key] != null) write[key] = {};
559
- else if (Array.isArray(read[key])) write[key] = [];
560
- }
561
- read = read[key];
562
- write = write[key];
563
- }
564
- if (last in read && read[last] !== void 0) write[last] = write[last] || read[last];
565
- });
566
- const escapedKwargs = {};
567
- const pathSet = /* @__PURE__ */ new WeakSet();
568
- pathSet.add(this);
569
- for (const [key, value] of Object.entries(kwargs)) escapedKwargs[key] = escapeIfNeeded(value, pathSet);
570
- const processedKwargs = mapKeys(Object.keys(secrets).length ? replaceSecrets(escapedKwargs, secrets) : escapedKwargs, keyToJson, aliases);
571
- return {
572
- lc: 1,
573
- type: "constructor",
574
- id: this.lc_id,
575
- kwargs: processedKwargs
576
- };
577
- }
578
- toJSONNotImplemented() {
579
- return {
580
- lc: 1,
581
- type: "not_implemented",
582
- id: this.lc_id
583
- };
584
- }
585
- };
586
-
587
- const isDeno = () => typeof Deno !== "undefined";
588
- function getEnvironmentVariable(name) {
589
- try {
590
- if (typeof process !== "undefined") return process.env?.[name];
591
- else if (isDeno()) return Deno?.env.get(name);
592
- else return;
593
- } catch {
594
- return;
595
- }
596
- }
597
-
598
- /**
599
- * Abstract class that provides a set of optional methods that can be
600
- * overridden in derived classes to handle various events during the
601
- * execution of a LangChain application.
602
- */
603
- var BaseCallbackHandlerMethodsClass = class {};
604
- /**
605
- * Abstract base class for creating callback handlers in the LangChain
606
- * framework. It provides a set of optional methods that can be overridden
607
- * in derived classes to handle various events during the execution of a
608
- * LangChain application.
609
- */
610
- var BaseCallbackHandler = class extends BaseCallbackHandlerMethodsClass {
611
- lc_serializable = false;
612
- get lc_namespace() {
613
- return [
614
- "langchain_core",
615
- "callbacks",
616
- this.name
617
- ];
618
- }
619
- get lc_secrets() {}
620
- get lc_attributes() {}
621
- get lc_aliases() {}
622
- get lc_serializable_keys() {}
623
- /**
624
- * The name of the serializable. Override to provide an alias or
625
- * to preserve the serialized module name in minified environments.
626
- *
627
- * Implemented as a static method to support loading logic.
628
- */
629
- static lc_name() {
630
- return this.name;
631
- }
632
- /**
633
- * The final serialized identifier for the module.
634
- */
635
- get lc_id() {
636
- return [...this.lc_namespace, get_lc_unique_name(this.constructor)];
637
- }
638
- lc_kwargs;
639
- ignoreLLM = false;
640
- ignoreChain = false;
641
- ignoreAgent = false;
642
- ignoreRetriever = false;
643
- ignoreCustomEvent = false;
644
- raiseError = false;
645
- awaitHandlers = getEnvironmentVariable("LANGCHAIN_CALLBACKS_BACKGROUND") === "false";
646
- constructor(input) {
647
- super();
648
- this.lc_kwargs = input || {};
649
- if (input) {
650
- this.ignoreLLM = input.ignoreLLM ?? this.ignoreLLM;
651
- this.ignoreChain = input.ignoreChain ?? this.ignoreChain;
652
- this.ignoreAgent = input.ignoreAgent ?? this.ignoreAgent;
653
- this.ignoreRetriever = input.ignoreRetriever ?? this.ignoreRetriever;
654
- this.ignoreCustomEvent = input.ignoreCustomEvent ?? this.ignoreCustomEvent;
655
- this.raiseError = input.raiseError ?? this.raiseError;
656
- this.awaitHandlers = this.raiseError || (input._awaitHandler ?? this.awaitHandlers);
657
- }
658
- }
659
- copy() {
660
- return new this.constructor(this);
661
- }
662
- toJSON() {
663
- return Serializable.prototype.toJSON.call(this);
664
- }
665
- toJSONNotImplemented() {
666
- return Serializable.prototype.toJSONNotImplemented.call(this);
667
- }
668
- static fromMethods(methods) {
669
- class Handler extends BaseCallbackHandler {
670
- name = uuid.v7();
671
- constructor() {
672
- super();
673
- Object.assign(this, methods);
674
- }
675
- }
676
- return new Handler();
677
- }
678
- };
679
-
680
- var version = "7.21.0";
218
+ var version = "8.0.1";
681
219
 
682
220
  const DEFAULT_MAX_DEPTH = 3;
683
221
  const MAX_STACK_LINES = 20;