@posthog/ai 7.9.1 → 7.9.3

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/index.mjs CHANGED
@@ -5,7 +5,7 @@ import { uuidv7 } from '@posthog/core';
5
5
  import AnthropicOriginal from '@anthropic-ai/sdk';
6
6
  import { GoogleGenAI } from '@google/genai';
7
7
 
8
- var version = "7.9.1";
8
+ var version = "7.9.3";
9
9
 
10
10
  // Type guards for safer type checking
11
11
  const isString = value => {
@@ -224,6 +224,13 @@ const sanitizeLangChain = data => {
224
224
  return processMessages(data, sanitizeLangChainImage);
225
225
  };
226
226
 
227
+ const TOKEN_PROPERTY_KEYS = new Set(['$ai_input_tokens', '$ai_output_tokens', '$ai_cache_read_input_tokens', '$ai_cache_creation_input_tokens', '$ai_total_tokens', '$ai_reasoning_tokens']);
228
+ function getTokensSource(posthogProperties) {
229
+ if (posthogProperties && Object.keys(posthogProperties).some(key => TOKEN_PROPERTY_KEYS.has(key))) {
230
+ return 'passthrough';
231
+ }
232
+ return 'sdk';
233
+ }
227
234
  // limit large outputs by truncating to 200kb (approx 200k bytes)
228
235
  const MAX_OUTPUT_SIZE = 200000;
229
236
  const STRING_FORMAT = 'utf8';
@@ -803,6 +810,7 @@ const sendEventToPosthog = async ({
803
810
  $ai_trace_id: traceId,
804
811
  $ai_base_url: baseURL,
805
812
  ...params.posthogProperties,
813
+ $ai_tokens_source: getTokensSource(params.posthogProperties),
806
814
  ...(distinctId ? {} : {
807
815
  $process_person_profile: false
808
816
  }),
@@ -4083,15 +4091,6 @@ function calculateGoogleWebSearchCount(response) {
4083
4091
  return hasGrounding ? 1 : 0;
4084
4092
  }
4085
4093
 
4086
- //#region rolldown:runtime
4087
- var __defProp = Object.defineProperty;
4088
- var __export = (target, all) => {
4089
- for (var name in all) __defProp(target, name, {
4090
- get: all[name],
4091
- enumerable: true
4092
- });
4093
- };
4094
-
4095
4094
  function getDefaultExportFromCjs (x) {
4096
4095
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
4097
4096
  }
@@ -4335,12 +4334,6 @@ function escapeIfNeeded(value, pathSet = /* @__PURE__ */ new WeakSet()) {
4335
4334
  return value;
4336
4335
  }
4337
4336
 
4338
- //#region src/load/serializable.ts
4339
- var serializable_exports = {};
4340
- __export(serializable_exports, {
4341
- Serializable: () => Serializable,
4342
- get_lc_unique_name: () => get_lc_unique_name
4343
- });
4344
4337
  function shallowCopy(obj) {
4345
4338
  return Array.isArray(obj) ? [...obj] : { ...obj };
4346
4339
  }
@@ -4368,8 +4361,7 @@ function replaceSecrets(root, secretsMap) {
4368
4361
  */
4369
4362
  function get_lc_unique_name(serializableClass) {
4370
4363
  const parentClass = Object.getPrototypeOf(serializableClass);
4371
- const lcNameIsSubclassed = typeof serializableClass.lc_name === "function" && (typeof parentClass.lc_name !== "function" || serializableClass.lc_name() !== parentClass.lc_name());
4372
- if (lcNameIsSubclassed) return serializableClass.lc_name();
4364
+ if (typeof serializableClass.lc_name === "function" && (typeof parentClass.lc_name !== "function" || serializableClass.lc_name() !== parentClass.lc_name())) return serializableClass.lc_name();
4373
4365
  else return serializableClass.name;
4374
4366
  }
4375
4367
  var Serializable = class Serializable {
@@ -4395,34 +4387,26 @@ var Serializable = class Serializable {
4395
4387
  * Keys are paths to the secret in constructor args, e.g. "foo.bar.baz".
4396
4388
  * Values are the secret ids, which will be used when deserializing.
4397
4389
  */
4398
- get lc_secrets() {
4399
- return void 0;
4400
- }
4390
+ get lc_secrets() {}
4401
4391
  /**
4402
4392
  * A map of additional attributes to merge with constructor args.
4403
4393
  * Keys are the attribute names, e.g. "foo".
4404
4394
  * Values are the attribute values, which will be serialized.
4405
4395
  * These attributes need to be accepted by the constructor as arguments.
4406
4396
  */
4407
- get lc_attributes() {
4408
- return void 0;
4409
- }
4397
+ get lc_attributes() {}
4410
4398
  /**
4411
4399
  * A map of aliases for constructor args.
4412
4400
  * Keys are the attribute names, e.g. "foo".
4413
4401
  * Values are the alias that will replace the key in serialization.
4414
4402
  * This is used to eg. make argument names match Python.
4415
4403
  */
4416
- get lc_aliases() {
4417
- return void 0;
4418
- }
4404
+ get lc_aliases() {}
4419
4405
  /**
4420
4406
  * A manual list of keys that should be serialized.
4421
4407
  * If not overridden, all fields passed into the constructor will be serialized.
4422
4408
  */
4423
- get lc_serializable_keys() {
4424
- return void 0;
4425
- }
4409
+ get lc_serializable_keys() {}
4426
4410
  constructor(kwargs, ..._args) {
4427
4411
  if (this.lc_serializable_keys !== void 0) this.lc_kwargs = Object.fromEntries(Object.entries(kwargs || {}).filter(([key]) => this.lc_serializable_keys?.includes(key)));
4428
4412
  else this.lc_kwargs = kwargs ?? {};
@@ -4460,8 +4444,7 @@ var Serializable = class Serializable {
4460
4444
  const pathSet = /* @__PURE__ */ new WeakSet();
4461
4445
  pathSet.add(this);
4462
4446
  for (const [key, value] of Object.entries(kwargs)) escapedKwargs[key] = escapeIfNeeded(value, pathSet);
4463
- const kwargsWithSecrets = Object.keys(secrets).length ? replaceSecrets(escapedKwargs, secrets) : escapedKwargs;
4464
- const processedKwargs = mapKeys(kwargsWithSecrets, keyToJson, aliases);
4447
+ const processedKwargs = mapKeys(Object.keys(secrets).length ? replaceSecrets(escapedKwargs, secrets) : escapedKwargs, keyToJson, aliases);
4465
4448
  return {
4466
4449
  lc: 1,
4467
4450
  type: "constructor",
@@ -4478,70 +4461,23 @@ var Serializable = class Serializable {
4478
4461
  }
4479
4462
  };
4480
4463
 
4481
- //#region src/utils/env.ts
4482
- var env_exports = {};
4483
- __export(env_exports, {
4484
- getEnv: () => getEnv,
4485
- getEnvironmentVariable: () => getEnvironmentVariable,
4486
- getRuntimeEnvironment: () => getRuntimeEnvironment,
4487
- isBrowser: () => isBrowser,
4488
- isDeno: () => isDeno,
4489
- isJsDom: () => isJsDom,
4490
- isNode: () => isNode,
4491
- isWebWorker: () => isWebWorker
4492
- });
4493
- const isBrowser = () => typeof window !== "undefined" && typeof window.document !== "undefined";
4494
- const isWebWorker = () => typeof globalThis === "object" && globalThis.constructor && globalThis.constructor.name === "DedicatedWorkerGlobalScope";
4495
- const isJsDom = () => typeof window !== "undefined" && window.name === "nodejs" || typeof navigator !== "undefined" && navigator.userAgent.includes("jsdom");
4496
4464
  const isDeno = () => typeof Deno !== "undefined";
4497
- const isNode = () => typeof process !== "undefined" && typeof process.versions !== "undefined" && typeof process.versions.node !== "undefined" && !isDeno();
4498
- const getEnv = () => {
4499
- let env;
4500
- if (isBrowser()) env = "browser";
4501
- else if (isNode()) env = "node";
4502
- else if (isWebWorker()) env = "webworker";
4503
- else if (isJsDom()) env = "jsdom";
4504
- else if (isDeno()) env = "deno";
4505
- else env = "other";
4506
- return env;
4507
- };
4508
- let runtimeEnvironment;
4509
- function getRuntimeEnvironment() {
4510
- if (runtimeEnvironment === void 0) {
4511
- const env = getEnv();
4512
- runtimeEnvironment = {
4513
- library: "langchain-js",
4514
- runtime: env
4515
- };
4516
- }
4517
- return runtimeEnvironment;
4518
- }
4519
4465
  function getEnvironmentVariable(name) {
4520
4466
  try {
4521
4467
  if (typeof process !== "undefined") return process.env?.[name];
4522
4468
  else if (isDeno()) return Deno?.env.get(name);
4523
- else return void 0;
4469
+ else return;
4524
4470
  } catch {
4525
- return void 0;
4471
+ return;
4526
4472
  }
4527
4473
  }
4528
4474
 
4529
- //#region src/callbacks/base.ts
4530
- var base_exports = {};
4531
- __export(base_exports, {
4532
- BaseCallbackHandler: () => BaseCallbackHandler,
4533
- callbackHandlerPrefersStreaming: () => callbackHandlerPrefersStreaming,
4534
- isBaseCallbackHandler: () => isBaseCallbackHandler
4535
- });
4536
4475
  /**
4537
4476
  * Abstract class that provides a set of optional methods that can be
4538
4477
  * overridden in derived classes to handle various events during the
4539
4478
  * execution of a LangChain application.
4540
4479
  */
4541
4480
  var BaseCallbackHandlerMethodsClass = class {};
4542
- function callbackHandlerPrefersStreaming(x) {
4543
- return "lc_prefer_streaming" in x && x.lc_prefer_streaming;
4544
- }
4545
4481
  /**
4546
4482
  * Abstract base class for creating callback handlers in the LangChain
4547
4483
  * framework. It provides a set of optional methods that can be overridden
@@ -4557,18 +4493,10 @@ var BaseCallbackHandler = class extends BaseCallbackHandlerMethodsClass {
4557
4493
  this.name
4558
4494
  ];
4559
4495
  }
4560
- get lc_secrets() {
4561
- return void 0;
4562
- }
4563
- get lc_attributes() {
4564
- return void 0;
4565
- }
4566
- get lc_aliases() {
4567
- return void 0;
4568
- }
4569
- get lc_serializable_keys() {
4570
- return void 0;
4571
- }
4496
+ get lc_secrets() {}
4497
+ get lc_attributes() {}
4498
+ get lc_aliases() {}
4499
+ get lc_serializable_keys() {}
4572
4500
  /**
4573
4501
  * The name of the serializable. Override to provide an alias or
4574
4502
  * to preserve the serialized module name in minified environments.
@@ -4625,10 +4553,6 @@ var BaseCallbackHandler = class extends BaseCallbackHandlerMethodsClass {
4625
4553
  return new Handler();
4626
4554
  }
4627
4555
  };
4628
- const isBaseCallbackHandler = (x) => {
4629
- const callbackHandler = x;
4630
- return callbackHandler !== void 0 && typeof callbackHandler.copy === "function" && typeof callbackHandler.name === "string" && typeof callbackHandler.awaitHandlers === "boolean";
4631
- };
4632
4556
 
4633
4557
  class LangChainCallbackHandler extends BaseCallbackHandler {
4634
4558
  constructor(options) {