@posthog/ai 3.0.0 → 3.1.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.
package/CHANGELOG.md CHANGED
@@ -1,4 +1,10 @@
1
- # Next
1
+ # 3.1.1 - 2025-02-07
2
+
3
+ - fix: bump ai to 4.1.0
4
+
5
+ # 3.1.0 - 2025-02-07
6
+
7
+ - feat: add posthogCostOverride, posthogModelOverride, and posthogProviderOverride to sendEventToPosthog for vercel
2
8
 
3
9
  # 2.4.0 - 2025-02-03
4
10
 
package/lib/index.cjs.js CHANGED
@@ -111,12 +111,22 @@ const sendEventToPosthog = ({
111
111
  $ai_error: error
112
112
  };
113
113
  }
114
+ let costOverrideData = {};
115
+ if (params.posthogCostOverride) {
116
+ const inputCostUSD = (params.posthogCostOverride.inputCost ?? 0) * (usage.inputTokens ?? 0);
117
+ const outputCostUSD = (params.posthogCostOverride.outputCost ?? 0) * (usage.outputTokens ?? 0);
118
+ costOverrideData = {
119
+ $ai_input_cost_usd: inputCostUSD,
120
+ $ai_output_cost_usd: outputCostUSD,
121
+ $ai_total_cost_usd: inputCostUSD + outputCostUSD
122
+ };
123
+ }
114
124
  client.capture({
115
125
  distinctId: distinctId ?? traceId,
116
126
  event: '$ai_generation',
117
127
  properties: {
118
- $ai_provider: provider,
119
- $ai_model: model,
128
+ $ai_provider: params.posthogProviderOverride ?? provider,
129
+ $ai_model: params.posthogModelOverride ?? model,
120
130
  $ai_model_parameters: getModelParams(params),
121
131
  $ai_input: withPrivacyMode(client, params.posthogPrivacyMode ?? false, input),
122
132
  $ai_output_choices: withPrivacyMode(client, params.posthogPrivacyMode ?? false, output),
@@ -130,7 +140,8 @@ const sendEventToPosthog = ({
130
140
  ...(distinctId ? {} : {
131
141
  $process_person_profile: false
132
142
  }),
133
- ...errorData
143
+ ...errorData,
144
+ ...costOverrideData
134
145
  },
135
146
  groups: params.posthogGroups
136
147
  });
@@ -512,6 +523,11 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
512
523
  const modelId = options.posthogModelOverride ?? (result.response?.modelId ? result.response.modelId : model.modelId);
513
524
  const provider = options.posthogProviderOverride ?? extractProvider(model);
514
525
  const baseURL = ''; // cannot currently get baseURL from vercel
526
+ let content = result.text;
527
+ if (!content) {
528
+ // support generate Object
529
+ content = result.toolCalls?.[0].args || JSON.stringify(result);
530
+ }
515
531
  sendEventToPosthog({
516
532
  client: phClient,
517
533
  distinctId: options.posthogDistinctId,
@@ -520,7 +536,7 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
520
536
  provider: provider,
521
537
  input: options.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
522
538
  output: [{
523
- content: result.text,
539
+ content,
524
540
  role: 'assistant'
525
541
  }],
526
542
  latency,
@@ -1283,9 +1299,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
1283
1299
  this.debug = options.debug || false;
1284
1300
  }
1285
1301
  // ===== CALLBACK METHODS =====
1286
- handleChainStart(chain, inputs, runId, parentRunId, tags, metadata,
1287
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
1288
- runType, runName) {
1302
+ handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, runType, runName) {
1289
1303
  this._logDebugEvent('on_chain_start', runId, parentRunId, {
1290
1304
  inputs,
1291
1305
  tags
@@ -1483,7 +1497,9 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
1483
1497
  const traceId = this._getTraceId(runId);
1484
1498
  this._popParentOfRun(runId);
1485
1499
  const run = this._popRunMetadata(runId);
1486
- if (!run) return;
1500
+ if (!run) {
1501
+ return;
1502
+ }
1487
1503
  if ('modelParams' in run) {
1488
1504
  console.warn(`Run ${runId} is a generation, but attempted to be captured as a trace/span.`);
1489
1505
  return;
@@ -1592,10 +1608,14 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
1592
1608
  _getLangchainRunName(serialized, ...args) {
1593
1609
  if (args && args.length > 0) {
1594
1610
  for (const arg of args) {
1595
- if (arg && typeof arg === 'object' && 'name' in arg) return arg.name;
1611
+ if (arg && typeof arg === 'object' && 'name' in arg) {
1612
+ return arg.name;
1613
+ }
1596
1614
  }
1597
1615
  }
1598
- if (serialized && serialized.name) return serialized.name;
1616
+ if (serialized && serialized.name) {
1617
+ return serialized.name;
1618
+ }
1599
1619
  if (serialized && serialized.id) {
1600
1620
  return Array.isArray(serialized.id) ? serialized.id[serialized.id.length - 1] : serialized.id;
1601
1621
  }