@posthog/ai 3.3.1 → 4.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 (39) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/lib/anthropic/index.cjs.js +293 -0
  3. package/lib/anthropic/index.cjs.js.map +1 -0
  4. package/lib/anthropic/index.d.ts +45 -0
  5. package/lib/anthropic/index.esm.js +282 -0
  6. package/lib/anthropic/index.esm.js.map +1 -0
  7. package/lib/index.cjs.js +36 -8
  8. package/lib/index.cjs.js.map +1 -1
  9. package/lib/index.d.ts +2 -2
  10. package/lib/index.esm.js +36 -8
  11. package/lib/index.esm.js.map +1 -1
  12. package/lib/langchain/index.cjs.js +1003 -0
  13. package/lib/langchain/index.cjs.js.map +1 -0
  14. package/lib/langchain/index.d.ts +68 -0
  15. package/lib/langchain/index.esm.js +979 -0
  16. package/lib/langchain/index.esm.js.map +1 -0
  17. package/lib/openai/index.cjs.js +286 -0
  18. package/lib/openai/index.cjs.js.map +1 -0
  19. package/lib/openai/index.d.ts +49 -0
  20. package/lib/openai/index.esm.js +274 -0
  21. package/lib/openai/index.esm.js.map +1 -0
  22. package/lib/posthog-ai/src/anthropic/index.d.ts +1 -0
  23. package/lib/posthog-ai/src/langchain/index.d.ts +1 -0
  24. package/lib/posthog-ai/src/openai/index.d.ts +3 -2
  25. package/lib/posthog-ai/src/vercel/index.d.ts +1 -0
  26. package/lib/vercel/index.cjs.js +408 -0
  27. package/lib/vercel/index.cjs.js.map +1 -0
  28. package/lib/vercel/index.d.ts +21 -0
  29. package/lib/vercel/index.esm.js +404 -0
  30. package/lib/vercel/index.esm.js.map +1 -0
  31. package/package.json +28 -1
  32. package/src/anthropic/index.ts +2 -0
  33. package/src/langchain/callbacks.ts +39 -7
  34. package/src/langchain/index.ts +1 -0
  35. package/src/openai/index.ts +4 -2
  36. package/src/utils.ts +1 -1
  37. package/src/vercel/index.ts +1 -0
  38. package/src/vercel/middleware.ts +4 -4
  39. package/tsconfig.json +1 -0
@@ -0,0 +1,1003 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var uuid = require('uuid');
6
+
7
+ function _interopNamespace(e) {
8
+ if (e && e.__esModule) return e;
9
+ var n = Object.create(null);
10
+ if (e) {
11
+ Object.keys(e).forEach(function (k) {
12
+ if (k !== 'default') {
13
+ var d = Object.getOwnPropertyDescriptor(e, k);
14
+ Object.defineProperty(n, k, d.get ? d : {
15
+ enumerable: true,
16
+ get: function () { return e[k]; }
17
+ });
18
+ }
19
+ });
20
+ }
21
+ n["default"] = e;
22
+ return Object.freeze(n);
23
+ }
24
+
25
+ var uuid__namespace = /*#__PURE__*/_interopNamespace(uuid);
26
+
27
+ const getModelParams = params => {
28
+ if (!params) {
29
+ return {};
30
+ }
31
+ const modelParams = {};
32
+ const paramKeys = ['temperature', 'max_tokens', 'max_completion_tokens', 'top_p', 'frequency_penalty', 'presence_penalty', 'n', 'stop', 'stream', 'streaming'];
33
+ for (const key of paramKeys) {
34
+ if (key in params && params[key] !== undefined) {
35
+ modelParams[key] = params[key];
36
+ }
37
+ }
38
+ return modelParams;
39
+ };
40
+ const withPrivacyMode = (client, privacyMode, input) => {
41
+ return client.privacy_mode || privacyMode ? null : input;
42
+ };
43
+
44
+ var decamelize = function (str, sep) {
45
+ if (typeof str !== 'string') {
46
+ throw new TypeError('Expected a string');
47
+ }
48
+
49
+ sep = typeof sep === 'undefined' ? '_' : sep;
50
+
51
+ return str
52
+ .replace(/([a-z\d])([A-Z])/g, '$1' + sep + '$2')
53
+ .replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + sep + '$2')
54
+ .toLowerCase();
55
+ };
56
+
57
+ var camelcase = {exports: {}};
58
+
59
+ const UPPERCASE = /[\p{Lu}]/u;
60
+ const LOWERCASE = /[\p{Ll}]/u;
61
+ const LEADING_CAPITAL = /^[\p{Lu}](?![\p{Lu}])/gu;
62
+ const IDENTIFIER = /([\p{Alpha}\p{N}_]|$)/u;
63
+ const SEPARATORS = /[_.\- ]+/;
64
+
65
+ const LEADING_SEPARATORS = new RegExp('^' + SEPARATORS.source);
66
+ const SEPARATORS_AND_IDENTIFIER = new RegExp(SEPARATORS.source + IDENTIFIER.source, 'gu');
67
+ const NUMBERS_AND_IDENTIFIER = new RegExp('\\d+' + IDENTIFIER.source, 'gu');
68
+
69
+ const preserveCamelCase = (string, toLowerCase, toUpperCase) => {
70
+ let isLastCharLower = false;
71
+ let isLastCharUpper = false;
72
+ let isLastLastCharUpper = false;
73
+
74
+ for (let i = 0; i < string.length; i++) {
75
+ const character = string[i];
76
+
77
+ if (isLastCharLower && UPPERCASE.test(character)) {
78
+ string = string.slice(0, i) + '-' + string.slice(i);
79
+ isLastCharLower = false;
80
+ isLastLastCharUpper = isLastCharUpper;
81
+ isLastCharUpper = true;
82
+ i++;
83
+ } else if (isLastCharUpper && isLastLastCharUpper && LOWERCASE.test(character)) {
84
+ string = string.slice(0, i - 1) + '-' + string.slice(i - 1);
85
+ isLastLastCharUpper = isLastCharUpper;
86
+ isLastCharUpper = false;
87
+ isLastCharLower = true;
88
+ } else {
89
+ isLastCharLower = toLowerCase(character) === character && toUpperCase(character) !== character;
90
+ isLastLastCharUpper = isLastCharUpper;
91
+ isLastCharUpper = toUpperCase(character) === character && toLowerCase(character) !== character;
92
+ }
93
+ }
94
+
95
+ return string;
96
+ };
97
+
98
+ const preserveConsecutiveUppercase = (input, toLowerCase) => {
99
+ LEADING_CAPITAL.lastIndex = 0;
100
+
101
+ return input.replace(LEADING_CAPITAL, m1 => toLowerCase(m1));
102
+ };
103
+
104
+ const postProcess = (input, toUpperCase) => {
105
+ SEPARATORS_AND_IDENTIFIER.lastIndex = 0;
106
+ NUMBERS_AND_IDENTIFIER.lastIndex = 0;
107
+
108
+ return input.replace(SEPARATORS_AND_IDENTIFIER, (_, identifier) => toUpperCase(identifier))
109
+ .replace(NUMBERS_AND_IDENTIFIER, m => toUpperCase(m));
110
+ };
111
+
112
+ const camelCase = (input, options) => {
113
+ if (!(typeof input === 'string' || Array.isArray(input))) {
114
+ throw new TypeError('Expected the input to be `string | string[]`');
115
+ }
116
+
117
+ options = {
118
+ pascalCase: false,
119
+ preserveConsecutiveUppercase: false,
120
+ ...options
121
+ };
122
+
123
+ if (Array.isArray(input)) {
124
+ input = input.map(x => x.trim())
125
+ .filter(x => x.length)
126
+ .join('-');
127
+ } else {
128
+ input = input.trim();
129
+ }
130
+
131
+ if (input.length === 0) {
132
+ return '';
133
+ }
134
+
135
+ const toLowerCase = options.locale === false ?
136
+ string => string.toLowerCase() :
137
+ string => string.toLocaleLowerCase(options.locale);
138
+ const toUpperCase = options.locale === false ?
139
+ string => string.toUpperCase() :
140
+ string => string.toLocaleUpperCase(options.locale);
141
+
142
+ if (input.length === 1) {
143
+ return options.pascalCase ? toUpperCase(input) : toLowerCase(input);
144
+ }
145
+
146
+ const hasUpperCase = input !== toLowerCase(input);
147
+
148
+ if (hasUpperCase) {
149
+ input = preserveCamelCase(input, toLowerCase, toUpperCase);
150
+ }
151
+
152
+ input = input.replace(LEADING_SEPARATORS, '');
153
+
154
+ if (options.preserveConsecutiveUppercase) {
155
+ input = preserveConsecutiveUppercase(input, toLowerCase);
156
+ } else {
157
+ input = toLowerCase(input);
158
+ }
159
+
160
+ if (options.pascalCase) {
161
+ input = toUpperCase(input.charAt(0)) + input.slice(1);
162
+ }
163
+
164
+ return postProcess(input, toUpperCase);
165
+ };
166
+
167
+ camelcase.exports = camelCase;
168
+ // TODO: Remove this for the next major release
169
+ camelcase.exports.default = camelCase;
170
+
171
+ function keyToJson(key, map) {
172
+ return map?.[key] || decamelize(key);
173
+ }
174
+ function mapKeys(fields, mapper, map) {
175
+ const mapped = {};
176
+ for (const key in fields) {
177
+ if (Object.hasOwn(fields, key)) {
178
+ mapped[mapper(key, map)] = fields[key];
179
+ }
180
+ }
181
+ return mapped;
182
+ }
183
+
184
+ function shallowCopy(obj) {
185
+ return Array.isArray(obj) ? [...obj] : { ...obj };
186
+ }
187
+ function replaceSecrets(root, secretsMap) {
188
+ const result = shallowCopy(root);
189
+ for (const [path, secretId] of Object.entries(secretsMap)) {
190
+ const [last, ...partsReverse] = path.split(".").reverse();
191
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
192
+ let current = result;
193
+ for (const part of partsReverse.reverse()) {
194
+ if (current[part] === undefined) {
195
+ break;
196
+ }
197
+ current[part] = shallowCopy(current[part]);
198
+ current = current[part];
199
+ }
200
+ if (current[last] !== undefined) {
201
+ current[last] = {
202
+ lc: 1,
203
+ type: "secret",
204
+ id: [secretId],
205
+ };
206
+ }
207
+ }
208
+ return result;
209
+ }
210
+ /**
211
+ * Get a unique name for the module, rather than parent class implementations.
212
+ * Should not be subclassed, subclass lc_name above instead.
213
+ */
214
+ function get_lc_unique_name(
215
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
216
+ serializableClass) {
217
+ // "super" here would refer to the parent class of Serializable,
218
+ // when we want the parent class of the module actually calling this method.
219
+ const parentClass = Object.getPrototypeOf(serializableClass);
220
+ const lcNameIsSubclassed = typeof serializableClass.lc_name === "function" &&
221
+ (typeof parentClass.lc_name !== "function" ||
222
+ serializableClass.lc_name() !== parentClass.lc_name());
223
+ if (lcNameIsSubclassed) {
224
+ return serializableClass.lc_name();
225
+ }
226
+ else {
227
+ return serializableClass.name;
228
+ }
229
+ }
230
+ class Serializable {
231
+ /**
232
+ * The name of the serializable. Override to provide an alias or
233
+ * to preserve the serialized module name in minified environments.
234
+ *
235
+ * Implemented as a static method to support loading logic.
236
+ */
237
+ static lc_name() {
238
+ return this.name;
239
+ }
240
+ /**
241
+ * The final serialized identifier for the module.
242
+ */
243
+ get lc_id() {
244
+ return [
245
+ ...this.lc_namespace,
246
+ get_lc_unique_name(this.constructor),
247
+ ];
248
+ }
249
+ /**
250
+ * A map of secrets, which will be omitted from serialization.
251
+ * Keys are paths to the secret in constructor args, e.g. "foo.bar.baz".
252
+ * Values are the secret ids, which will be used when deserializing.
253
+ */
254
+ get lc_secrets() {
255
+ return undefined;
256
+ }
257
+ /**
258
+ * A map of additional attributes to merge with constructor args.
259
+ * Keys are the attribute names, e.g. "foo".
260
+ * Values are the attribute values, which will be serialized.
261
+ * These attributes need to be accepted by the constructor as arguments.
262
+ */
263
+ get lc_attributes() {
264
+ return undefined;
265
+ }
266
+ /**
267
+ * A map of aliases for constructor args.
268
+ * Keys are the attribute names, e.g. "foo".
269
+ * Values are the alias that will replace the key in serialization.
270
+ * This is used to eg. make argument names match Python.
271
+ */
272
+ get lc_aliases() {
273
+ return undefined;
274
+ }
275
+ constructor(kwargs, ..._args) {
276
+ Object.defineProperty(this, "lc_serializable", {
277
+ enumerable: true,
278
+ configurable: true,
279
+ writable: true,
280
+ value: false
281
+ });
282
+ Object.defineProperty(this, "lc_kwargs", {
283
+ enumerable: true,
284
+ configurable: true,
285
+ writable: true,
286
+ value: void 0
287
+ });
288
+ this.lc_kwargs = kwargs || {};
289
+ }
290
+ toJSON() {
291
+ if (!this.lc_serializable) {
292
+ return this.toJSONNotImplemented();
293
+ }
294
+ if (
295
+ // eslint-disable-next-line no-instanceof/no-instanceof
296
+ this.lc_kwargs instanceof Serializable ||
297
+ typeof this.lc_kwargs !== "object" ||
298
+ Array.isArray(this.lc_kwargs)) {
299
+ // We do not support serialization of classes with arg not a POJO
300
+ // I'm aware the check above isn't as strict as it could be
301
+ return this.toJSONNotImplemented();
302
+ }
303
+ const aliases = {};
304
+ const secrets = {};
305
+ const kwargs = Object.keys(this.lc_kwargs).reduce((acc, key) => {
306
+ acc[key] = key in this ? this[key] : this.lc_kwargs[key];
307
+ return acc;
308
+ }, {});
309
+ // get secrets, attributes and aliases from all superclasses
310
+ for (
311
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
312
+ let current = Object.getPrototypeOf(this); current; current = Object.getPrototypeOf(current)) {
313
+ Object.assign(aliases, Reflect.get(current, "lc_aliases", this));
314
+ Object.assign(secrets, Reflect.get(current, "lc_secrets", this));
315
+ Object.assign(kwargs, Reflect.get(current, "lc_attributes", this));
316
+ }
317
+ // include all secrets used, even if not in kwargs,
318
+ // will be replaced with sentinel value in replaceSecrets
319
+ Object.keys(secrets).forEach((keyPath) => {
320
+ // eslint-disable-next-line @typescript-eslint/no-this-alias, @typescript-eslint/no-explicit-any
321
+ let read = this;
322
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
323
+ let write = kwargs;
324
+ const [last, ...partsReverse] = keyPath.split(".").reverse();
325
+ for (const key of partsReverse.reverse()) {
326
+ if (!(key in read) || read[key] === undefined)
327
+ return;
328
+ if (!(key in write) || write[key] === undefined) {
329
+ if (typeof read[key] === "object" && read[key] != null) {
330
+ write[key] = {};
331
+ }
332
+ else if (Array.isArray(read[key])) {
333
+ write[key] = [];
334
+ }
335
+ }
336
+ read = read[key];
337
+ write = write[key];
338
+ }
339
+ if (last in read && read[last] !== undefined) {
340
+ write[last] = write[last] || read[last];
341
+ }
342
+ });
343
+ return {
344
+ lc: 1,
345
+ type: "constructor",
346
+ id: this.lc_id,
347
+ kwargs: mapKeys(Object.keys(secrets).length ? replaceSecrets(kwargs, secrets) : kwargs, keyToJson, aliases),
348
+ };
349
+ }
350
+ toJSONNotImplemented() {
351
+ return {
352
+ lc: 1,
353
+ type: "not_implemented",
354
+ id: this.lc_id,
355
+ };
356
+ }
357
+ }
358
+
359
+ // Supabase Edge Function provides a `Deno` global object
360
+ // without `version` property
361
+ const isDeno = () => typeof Deno !== "undefined";
362
+ function getEnvironmentVariable(name) {
363
+ // Certain Deno setups will throw an error if you try to access environment variables
364
+ // https://github.com/langchain-ai/langchainjs/issues/1412
365
+ try {
366
+ if (typeof process !== "undefined") {
367
+ // eslint-disable-next-line no-process-env
368
+ return process.env?.[name];
369
+ }
370
+ else if (isDeno()) {
371
+ return Deno?.env.get(name);
372
+ }
373
+ else {
374
+ return undefined;
375
+ }
376
+ }
377
+ catch (e) {
378
+ return undefined;
379
+ }
380
+ }
381
+
382
+ /**
383
+ * Abstract class that provides a set of optional methods that can be
384
+ * overridden in derived classes to handle various events during the
385
+ * execution of a LangChain application.
386
+ */
387
+ class BaseCallbackHandlerMethodsClass {
388
+ }
389
+ /**
390
+ * Abstract base class for creating callback handlers in the LangChain
391
+ * framework. It provides a set of optional methods that can be overridden
392
+ * in derived classes to handle various events during the execution of a
393
+ * LangChain application.
394
+ */
395
+ class BaseCallbackHandler extends BaseCallbackHandlerMethodsClass {
396
+ get lc_namespace() {
397
+ return ["langchain_core", "callbacks", this.name];
398
+ }
399
+ get lc_secrets() {
400
+ return undefined;
401
+ }
402
+ get lc_attributes() {
403
+ return undefined;
404
+ }
405
+ get lc_aliases() {
406
+ return undefined;
407
+ }
408
+ /**
409
+ * The name of the serializable. Override to provide an alias or
410
+ * to preserve the serialized module name in minified environments.
411
+ *
412
+ * Implemented as a static method to support loading logic.
413
+ */
414
+ static lc_name() {
415
+ return this.name;
416
+ }
417
+ /**
418
+ * The final serialized identifier for the module.
419
+ */
420
+ get lc_id() {
421
+ return [
422
+ ...this.lc_namespace,
423
+ get_lc_unique_name(this.constructor),
424
+ ];
425
+ }
426
+ constructor(input) {
427
+ super();
428
+ Object.defineProperty(this, "lc_serializable", {
429
+ enumerable: true,
430
+ configurable: true,
431
+ writable: true,
432
+ value: false
433
+ });
434
+ Object.defineProperty(this, "lc_kwargs", {
435
+ enumerable: true,
436
+ configurable: true,
437
+ writable: true,
438
+ value: void 0
439
+ });
440
+ Object.defineProperty(this, "ignoreLLM", {
441
+ enumerable: true,
442
+ configurable: true,
443
+ writable: true,
444
+ value: false
445
+ });
446
+ Object.defineProperty(this, "ignoreChain", {
447
+ enumerable: true,
448
+ configurable: true,
449
+ writable: true,
450
+ value: false
451
+ });
452
+ Object.defineProperty(this, "ignoreAgent", {
453
+ enumerable: true,
454
+ configurable: true,
455
+ writable: true,
456
+ value: false
457
+ });
458
+ Object.defineProperty(this, "ignoreRetriever", {
459
+ enumerable: true,
460
+ configurable: true,
461
+ writable: true,
462
+ value: false
463
+ });
464
+ Object.defineProperty(this, "ignoreCustomEvent", {
465
+ enumerable: true,
466
+ configurable: true,
467
+ writable: true,
468
+ value: false
469
+ });
470
+ Object.defineProperty(this, "raiseError", {
471
+ enumerable: true,
472
+ configurable: true,
473
+ writable: true,
474
+ value: false
475
+ });
476
+ Object.defineProperty(this, "awaitHandlers", {
477
+ enumerable: true,
478
+ configurable: true,
479
+ writable: true,
480
+ value: getEnvironmentVariable("LANGCHAIN_CALLBACKS_BACKGROUND") === "false"
481
+ });
482
+ this.lc_kwargs = input || {};
483
+ if (input) {
484
+ this.ignoreLLM = input.ignoreLLM ?? this.ignoreLLM;
485
+ this.ignoreChain = input.ignoreChain ?? this.ignoreChain;
486
+ this.ignoreAgent = input.ignoreAgent ?? this.ignoreAgent;
487
+ this.ignoreRetriever = input.ignoreRetriever ?? this.ignoreRetriever;
488
+ this.ignoreCustomEvent =
489
+ input.ignoreCustomEvent ?? this.ignoreCustomEvent;
490
+ this.raiseError = input.raiseError ?? this.raiseError;
491
+ this.awaitHandlers =
492
+ this.raiseError || (input._awaitHandler ?? this.awaitHandlers);
493
+ }
494
+ }
495
+ copy() {
496
+ return new this.constructor(this);
497
+ }
498
+ toJSON() {
499
+ return Serializable.prototype.toJSON.call(this);
500
+ }
501
+ toJSONNotImplemented() {
502
+ return Serializable.prototype.toJSONNotImplemented.call(this);
503
+ }
504
+ static fromMethods(methods) {
505
+ class Handler extends BaseCallbackHandler {
506
+ constructor() {
507
+ super();
508
+ Object.defineProperty(this, "name", {
509
+ enumerable: true,
510
+ configurable: true,
511
+ writable: true,
512
+ value: uuid__namespace.v4()
513
+ });
514
+ Object.assign(this, methods);
515
+ }
516
+ }
517
+ return new Handler();
518
+ }
519
+ }
520
+
521
+ /** A run may either be a Span or a Generation */
522
+
523
+ /** Storage for run metadata */
524
+
525
+ class LangChainCallbackHandler extends BaseCallbackHandler {
526
+ name = 'PosthogCallbackHandler';
527
+ runs = {};
528
+ parentTree = {};
529
+ constructor(options) {
530
+ if (!options.client) {
531
+ throw new Error('PostHog client is required');
532
+ }
533
+ super();
534
+ this.client = options.client;
535
+ this.distinctId = options.distinctId;
536
+ this.traceId = options.traceId;
537
+ this.properties = options.properties || {};
538
+ this.privacyMode = options.privacyMode || false;
539
+ this.groups = options.groups || {};
540
+ this.debug = options.debug || false;
541
+ }
542
+
543
+ // ===== CALLBACK METHODS =====
544
+
545
+ handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, runType, runName) {
546
+ this._logDebugEvent('on_chain_start', runId, parentRunId, {
547
+ inputs,
548
+ tags
549
+ });
550
+ this._setParentOfRun(runId, parentRunId);
551
+ this._setTraceOrSpanMetadata(chain, inputs, runId, parentRunId, metadata, tags, runName);
552
+ }
553
+ handleChainEnd(outputs, runId, parentRunId, tags,
554
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
555
+ kwargs) {
556
+ this._logDebugEvent('on_chain_end', runId, parentRunId, {
557
+ outputs,
558
+ tags
559
+ });
560
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, outputs);
561
+ }
562
+ handleChainError(error, runId, parentRunId, tags,
563
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
564
+ kwargs) {
565
+ this._logDebugEvent('on_chain_error', runId, parentRunId, {
566
+ error,
567
+ tags
568
+ });
569
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, error);
570
+ }
571
+ handleChatModelStart(serialized, messages, runId, parentRunId, extraParams, tags, metadata, runName) {
572
+ this._logDebugEvent('on_chat_model_start', runId, parentRunId, {
573
+ messages,
574
+ tags
575
+ });
576
+ this._setParentOfRun(runId, parentRunId);
577
+ // Flatten the two-dimensional messages and convert each message to a plain object
578
+ const input = messages.flat().map(m => this._convertMessageToDict(m));
579
+ this._setLLMMetadata(serialized, runId, input, metadata, extraParams, runName);
580
+ }
581
+ handleLLMStart(serialized, prompts, runId, parentRunId, extraParams, tags, metadata, runName) {
582
+ this._logDebugEvent('on_llm_start', runId, parentRunId, {
583
+ prompts,
584
+ tags
585
+ });
586
+ this._setParentOfRun(runId, parentRunId);
587
+ this._setLLMMetadata(serialized, runId, prompts, metadata, extraParams, runName);
588
+ }
589
+ handleLLMEnd(output, runId, parentRunId, tags,
590
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
591
+ extraParams) {
592
+ this._logDebugEvent('on_llm_end', runId, parentRunId, {
593
+ output,
594
+ tags
595
+ });
596
+ this._popRunAndCaptureGeneration(runId, parentRunId, output);
597
+ }
598
+ handleLLMError(err, runId, parentRunId, tags,
599
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
600
+ extraParams) {
601
+ this._logDebugEvent('on_llm_error', runId, parentRunId, {
602
+ err,
603
+ tags
604
+ });
605
+ this._popRunAndCaptureGeneration(runId, parentRunId, err);
606
+ }
607
+ handleToolStart(tool, input, runId, parentRunId, tags, metadata, runName) {
608
+ this._logDebugEvent('on_tool_start', runId, parentRunId, {
609
+ input,
610
+ tags
611
+ });
612
+ this._setParentOfRun(runId, parentRunId);
613
+ this._setTraceOrSpanMetadata(tool, input, runId, parentRunId, metadata, tags, runName);
614
+ }
615
+ handleToolEnd(output, runId, parentRunId, tags) {
616
+ this._logDebugEvent('on_tool_end', runId, parentRunId, {
617
+ output,
618
+ tags
619
+ });
620
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, output);
621
+ }
622
+ handleToolError(err, runId, parentRunId, tags) {
623
+ this._logDebugEvent('on_tool_error', runId, parentRunId, {
624
+ err,
625
+ tags
626
+ });
627
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, err);
628
+ }
629
+ handleRetrieverStart(retriever, query, runId, parentRunId, tags, metadata, name) {
630
+ this._logDebugEvent('on_retriever_start', runId, parentRunId, {
631
+ query,
632
+ tags
633
+ });
634
+ this._setParentOfRun(runId, parentRunId);
635
+ this._setTraceOrSpanMetadata(retriever, query, runId, parentRunId, metadata, tags, name);
636
+ }
637
+ handleRetrieverEnd(documents, runId, parentRunId, tags) {
638
+ this._logDebugEvent('on_retriever_end', runId, parentRunId, {
639
+ documents,
640
+ tags
641
+ });
642
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, documents);
643
+ }
644
+ handleRetrieverError(err, runId, parentRunId, tags) {
645
+ this._logDebugEvent('on_retriever_error', runId, parentRunId, {
646
+ err,
647
+ tags
648
+ });
649
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, err);
650
+ }
651
+ handleAgentAction(action, runId, parentRunId, tags) {
652
+ this._logDebugEvent('on_agent_action', runId, parentRunId, {
653
+ action,
654
+ tags
655
+ });
656
+ this._setParentOfRun(runId, parentRunId);
657
+ this._setTraceOrSpanMetadata(null, action, runId, parentRunId);
658
+ }
659
+ handleAgentEnd(action, runId, parentRunId, tags) {
660
+ this._logDebugEvent('on_agent_finish', runId, parentRunId, {
661
+ action,
662
+ tags
663
+ });
664
+ this._popRunAndCaptureTraceOrSpan(runId, parentRunId, action);
665
+ }
666
+
667
+ // ===== PRIVATE HELPERS =====
668
+
669
+ _setParentOfRun(runId, parentRunId) {
670
+ if (parentRunId) {
671
+ this.parentTree[runId] = parentRunId;
672
+ }
673
+ }
674
+ _popParentOfRun(runId) {
675
+ delete this.parentTree[runId];
676
+ }
677
+ _findRootRun(runId) {
678
+ let id = runId;
679
+ while (this.parentTree[id]) {
680
+ id = this.parentTree[id];
681
+ }
682
+ return id;
683
+ }
684
+ _setTraceOrSpanMetadata(serialized, input, runId, parentRunId, ...args) {
685
+ // Use default names if not provided: if this is a top-level run, we mark it as a trace, otherwise as a span.
686
+ const defaultName = parentRunId ? 'span' : 'trace';
687
+ const runName = this._getLangchainRunName(serialized, ...args) || defaultName;
688
+ this.runs[runId] = {
689
+ name: runName,
690
+ input,
691
+ startTime: Date.now()
692
+ };
693
+ }
694
+ _setLLMMetadata(serialized, runId, messages, metadata, extraParams, runName) {
695
+ const runNameFound = this._getLangchainRunName(serialized, {
696
+ extraParams,
697
+ runName
698
+ }) || 'generation';
699
+ const generation = {
700
+ name: runNameFound,
701
+ input: messages,
702
+ startTime: Date.now()
703
+ };
704
+ if (extraParams) {
705
+ generation.modelParams = getModelParams(extraParams.invocation_params);
706
+ }
707
+ if (metadata) {
708
+ if (metadata.ls_model_name) {
709
+ generation.model = metadata.ls_model_name;
710
+ }
711
+ if (metadata.ls_provider) {
712
+ generation.provider = metadata.ls_provider;
713
+ }
714
+ }
715
+ if (serialized && 'kwargs' in serialized && serialized.kwargs.openai_api_base) {
716
+ generation.baseUrl = serialized.kwargs.openai_api_base;
717
+ }
718
+ this.runs[runId] = generation;
719
+ }
720
+ _popRunMetadata(runId) {
721
+ const endTime = Date.now();
722
+ const run = this.runs[runId];
723
+ if (!run) {
724
+ console.warn(`No run metadata found for run ${runId}`);
725
+ return undefined;
726
+ }
727
+ run.endTime = endTime;
728
+ delete this.runs[runId];
729
+ return run;
730
+ }
731
+ _getTraceId(runId) {
732
+ return this.traceId ? String(this.traceId) : this._findRootRun(runId);
733
+ }
734
+ _getParentRunId(traceId, runId, parentRunId) {
735
+ // Replace the parent-run if not found in our stored parent tree.
736
+ if (parentRunId && !this.parentTree[parentRunId]) {
737
+ return traceId;
738
+ }
739
+ return parentRunId;
740
+ }
741
+ _popRunAndCaptureTraceOrSpan(runId, parentRunId, outputs) {
742
+ const traceId = this._getTraceId(runId);
743
+ this._popParentOfRun(runId);
744
+ const run = this._popRunMetadata(runId);
745
+ if (!run) {
746
+ return;
747
+ }
748
+ if ('modelParams' in run) {
749
+ console.warn(`Run ${runId} is a generation, but attempted to be captured as a trace/span.`);
750
+ return;
751
+ }
752
+ const actualParentRunId = this._getParentRunId(traceId, runId, parentRunId);
753
+ this._captureTraceOrSpan(traceId, runId, run, outputs, actualParentRunId);
754
+ }
755
+ _captureTraceOrSpan(traceId, runId, run, outputs, parentRunId) {
756
+ const eventName = parentRunId ? '$ai_span' : '$ai_trace';
757
+ const latency = run.endTime ? (run.endTime - run.startTime) / 1000 : 0;
758
+ const eventProperties = {
759
+ $ai_trace_id: traceId,
760
+ $ai_input_state: withPrivacyMode(this.client, this.privacyMode, run.input),
761
+ $ai_latency: latency,
762
+ $ai_span_name: run.name,
763
+ $ai_span_id: runId
764
+ };
765
+ if (parentRunId) {
766
+ eventProperties['$ai_parent_id'] = parentRunId;
767
+ }
768
+ Object.assign(eventProperties, this.properties);
769
+ if (!this.distinctId) {
770
+ eventProperties['$process_person_profile'] = false;
771
+ }
772
+ if (outputs instanceof Error) {
773
+ eventProperties['$ai_error'] = outputs.toString();
774
+ eventProperties['$ai_is_error'] = true;
775
+ } else if (outputs !== undefined) {
776
+ eventProperties['$ai_output_state'] = withPrivacyMode(this.client, this.privacyMode, outputs);
777
+ }
778
+ this.client.capture({
779
+ distinctId: this.distinctId ? this.distinctId.toString() : runId,
780
+ event: eventName,
781
+ properties: eventProperties,
782
+ groups: this.groups
783
+ });
784
+ }
785
+ _popRunAndCaptureGeneration(runId, parentRunId, response) {
786
+ const traceId = this._getTraceId(runId);
787
+ this._popParentOfRun(runId);
788
+ const run = this._popRunMetadata(runId);
789
+ if (!run || typeof run !== 'object' || !('modelParams' in run)) {
790
+ console.warn(`Run ${runId} is not a generation, but attempted to be captured as such.`);
791
+ return;
792
+ }
793
+ const actualParentRunId = this._getParentRunId(traceId, runId, parentRunId);
794
+ this._captureGeneration(traceId, runId, run, response, actualParentRunId);
795
+ }
796
+ _captureGeneration(traceId, runId, run, output, parentRunId) {
797
+ const latency = run.endTime ? (run.endTime - run.startTime) / 1000 : 0;
798
+ const eventProperties = {
799
+ $ai_trace_id: traceId,
800
+ $ai_span_id: runId,
801
+ $ai_span_name: run.name,
802
+ $ai_parent_id: parentRunId,
803
+ $ai_provider: run.provider,
804
+ $ai_model: run.model,
805
+ $ai_model_parameters: run.modelParams,
806
+ $ai_input: withPrivacyMode(this.client, this.privacyMode, run.input),
807
+ $ai_http_status: 200,
808
+ $ai_latency: latency,
809
+ $ai_base_url: run.baseUrl
810
+ };
811
+ if (run.tools) {
812
+ eventProperties['$ai_tools'] = withPrivacyMode(this.client, this.privacyMode, run.tools);
813
+ }
814
+ if (output instanceof Error) {
815
+ eventProperties['$ai_http_status'] = output.status || 500;
816
+ eventProperties['$ai_error'] = output.toString();
817
+ eventProperties['$ai_is_error'] = true;
818
+ } else {
819
+ // Handle token usage
820
+ const [inputTokens, outputTokens, additionalTokenData] = this.parseUsage(output);
821
+ eventProperties['$ai_input_tokens'] = inputTokens;
822
+ eventProperties['$ai_output_tokens'] = outputTokens;
823
+
824
+ // Add additional token data to properties
825
+ if (additionalTokenData.cacheReadInputTokens) {
826
+ eventProperties['$ai_cache_read_tokens'] = additionalTokenData.cacheReadInputTokens;
827
+ }
828
+ if (additionalTokenData.reasoningTokens) {
829
+ eventProperties['$ai_reasoning_tokens'] = additionalTokenData.reasoningTokens;
830
+ }
831
+
832
+ // Handle generations/completions
833
+ let completions;
834
+ if (output.generations && Array.isArray(output.generations)) {
835
+ const lastGeneration = output.generations[output.generations.length - 1];
836
+ if (Array.isArray(lastGeneration)) {
837
+ completions = lastGeneration.map(gen => {
838
+ return {
839
+ role: 'assistant',
840
+ content: gen.text
841
+ };
842
+ });
843
+ }
844
+ }
845
+ if (completions) {
846
+ eventProperties['$ai_output_choices'] = withPrivacyMode(this.client, this.privacyMode, completions);
847
+ }
848
+ }
849
+ Object.assign(eventProperties, this.properties);
850
+ if (!this.distinctId) {
851
+ eventProperties['$process_person_profile'] = false;
852
+ }
853
+ this.client.capture({
854
+ distinctId: this.distinctId ? this.distinctId.toString() : traceId,
855
+ event: '$ai_generation',
856
+ properties: eventProperties,
857
+ groups: this.groups
858
+ });
859
+ }
860
+ _logDebugEvent(eventName, runId, parentRunId, extra) {
861
+ if (this.debug) {
862
+ console.log(`Event: ${eventName}, runId: ${runId}, parentRunId: ${parentRunId}, extra:`, extra);
863
+ }
864
+ }
865
+ _getLangchainRunName(serialized, ...args) {
866
+ if (args && args.length > 0) {
867
+ for (const arg of args) {
868
+ if (arg && typeof arg === 'object' && 'name' in arg) {
869
+ return arg.name;
870
+ } else if (arg && typeof arg === 'object' && 'runName' in arg) {
871
+ return arg.runName;
872
+ }
873
+ }
874
+ }
875
+ if (serialized && serialized.name) {
876
+ return serialized.name;
877
+ }
878
+ if (serialized && serialized.id) {
879
+ return Array.isArray(serialized.id) ? serialized.id[serialized.id.length - 1] : serialized.id;
880
+ }
881
+ return undefined;
882
+ }
883
+ _convertMessageToDict(message) {
884
+ let messageDict = {};
885
+
886
+ // Check the _getType() method or type property instead of instanceof
887
+ const messageType = message._getType?.() || message.type;
888
+ switch (messageType) {
889
+ case 'human':
890
+ messageDict = {
891
+ role: 'user',
892
+ content: message.content
893
+ };
894
+ break;
895
+ case 'ai':
896
+ messageDict = {
897
+ role: 'assistant',
898
+ content: message.content
899
+ };
900
+ break;
901
+ case 'system':
902
+ messageDict = {
903
+ role: 'system',
904
+ content: message.content
905
+ };
906
+ break;
907
+ case 'tool':
908
+ messageDict = {
909
+ role: 'tool',
910
+ content: message.content
911
+ };
912
+ break;
913
+ case 'function':
914
+ messageDict = {
915
+ role: 'function',
916
+ content: message.content
917
+ };
918
+ break;
919
+ default:
920
+ messageDict = {
921
+ role: messageType || 'unknown',
922
+ content: String(message.content)
923
+ };
924
+ }
925
+ if (message.additional_kwargs) {
926
+ messageDict = {
927
+ ...messageDict,
928
+ ...message.additional_kwargs
929
+ };
930
+ }
931
+ return messageDict;
932
+ }
933
+ _parseUsageModel(usage) {
934
+ const conversionList = [['promptTokens', 'input'], ['completionTokens', 'output'], ['input_tokens', 'input'], ['output_tokens', 'output'], ['prompt_token_count', 'input'], ['candidates_token_count', 'output'], ['inputTokenCount', 'input'], ['outputTokenCount', 'output'], ['input_token_count', 'input'], ['generated_token_count', 'output']];
935
+ const parsedUsage = conversionList.reduce((acc, [modelKey, typeKey]) => {
936
+ const value = usage[modelKey];
937
+ if (value != null) {
938
+ const finalCount = Array.isArray(value) ? value.reduce((sum, tokenCount) => sum + tokenCount, 0) : value;
939
+ acc[typeKey] = finalCount;
940
+ }
941
+ return acc;
942
+ }, {
943
+ input: 0,
944
+ output: 0
945
+ });
946
+
947
+ // Extract additional token details like cached tokens and reasoning tokens
948
+ const additionalTokenData = {};
949
+
950
+ // Check for cached tokens in various formats
951
+ if (usage.prompt_tokens_details?.cached_tokens != null) {
952
+ additionalTokenData.cacheReadInputTokens = usage.prompt_tokens_details.cached_tokens;
953
+ } else if (usage.input_token_details?.cache_read != null) {
954
+ additionalTokenData.cacheReadInputTokens = usage.input_token_details.cache_read;
955
+ } else if (usage.cachedPromptTokens != null) {
956
+ additionalTokenData.cacheReadInputTokens = usage.cachedPromptTokens;
957
+ }
958
+
959
+ // Check for reasoning tokens in various formats
960
+ if (usage.completion_tokens_details?.reasoning_tokens != null) {
961
+ additionalTokenData.reasoningTokens = usage.completion_tokens_details.reasoning_tokens;
962
+ } else if (usage.output_token_details?.reasoning != null) {
963
+ additionalTokenData.reasoningTokens = usage.output_token_details.reasoning;
964
+ } else if (usage.reasoningTokens != null) {
965
+ additionalTokenData.reasoningTokens = usage.reasoningTokens;
966
+ }
967
+ return [parsedUsage.input, parsedUsage.output, additionalTokenData];
968
+ }
969
+ parseUsage(response) {
970
+ let llmUsage = [0, 0, {}];
971
+ const llmUsageKeys = ['token_usage', 'usage', 'tokenUsage'];
972
+ if (response.llmOutput != null) {
973
+ const key = llmUsageKeys.find(k => response.llmOutput?.[k] != null);
974
+ if (key) {
975
+ llmUsage = this._parseUsageModel(response.llmOutput[key]);
976
+ }
977
+ }
978
+
979
+ // If top-level usage info was not found, try checking the generations.
980
+ if (llmUsage[0] === 0 && llmUsage[1] === 0 && response.generations) {
981
+ for (const generation of response.generations) {
982
+ for (const genChunk of generation) {
983
+ // Check other paths for usage information
984
+ if (genChunk.generationInfo?.usage_metadata) {
985
+ llmUsage = this._parseUsageModel(genChunk.generationInfo.usage_metadata);
986
+ return llmUsage;
987
+ }
988
+ const messageChunk = genChunk.generationInfo ?? {};
989
+ const responseMetadata = messageChunk.response_metadata ?? {};
990
+ const chunkUsage = responseMetadata['usage'] ?? responseMetadata['amazon-bedrock-invocationMetrics'] ?? messageChunk.usage_metadata;
991
+ if (chunkUsage) {
992
+ llmUsage = this._parseUsageModel(chunkUsage);
993
+ return llmUsage;
994
+ }
995
+ }
996
+ }
997
+ }
998
+ return llmUsage;
999
+ }
1000
+ }
1001
+
1002
+ exports.LangChainCallbackHandler = LangChainCallbackHandler;
1003
+ //# sourceMappingURL=index.cjs.js.map