@posthog/ai 4.1.0 → 4.2.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/lib/index.d.ts CHANGED
@@ -23,6 +23,7 @@ interface MonitoringParams {
23
23
  posthogModelOverride?: string;
24
24
  posthogProviderOverride?: string;
25
25
  posthogCostOverride?: CostOverride;
26
+ fullDebug?: boolean;
26
27
  }
27
28
  interface CostOverride {
28
29
  inputCost: number;
@@ -94,6 +95,7 @@ interface ClientOptions {
94
95
  posthogModelOverride?: string;
95
96
  posthogProviderOverride?: string;
96
97
  posthogCostOverride?: CostOverride;
98
+ fullDebug?: boolean;
97
99
  }
98
100
  declare const wrapVercelLanguageModel: (model: LanguageModelV1, phClient: PostHog, options: ClientOptions) => LanguageModelV1;
99
101
 
package/lib/index.esm.js CHANGED
@@ -105,7 +105,8 @@ const sendEventToPosthog = ({
105
105
  usage = {},
106
106
  isError = false,
107
107
  error,
108
- tools
108
+ tools,
109
+ fullDebug = false
109
110
  }) => {
110
111
  if (client.capture) {
111
112
  // sanitize input and output for UTF-8 validity
@@ -140,32 +141,37 @@ const sendEventToPosthog = ({
140
141
  $ai_cache_creation_input_tokens: usage.cacheCreationInputTokens
141
142
  } : {})
142
143
  };
144
+ const properties = {
145
+ $ai_provider: params.posthogProviderOverride ?? provider,
146
+ $ai_model: params.posthogModelOverride ?? model,
147
+ $ai_model_parameters: getModelParams(params),
148
+ $ai_input: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeInput),
149
+ $ai_output_choices: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeOutput),
150
+ $ai_http_status: httpStatus,
151
+ $ai_input_tokens: usage.inputTokens ?? 0,
152
+ $ai_output_tokens: usage.outputTokens ?? 0,
153
+ ...additionalTokenValues,
154
+ $ai_latency: latency,
155
+ $ai_trace_id: traceId,
156
+ $ai_base_url: baseURL,
157
+ ...params.posthogProperties,
158
+ ...(distinctId ? {} : {
159
+ $process_person_profile: false
160
+ }),
161
+ ...(tools ? {
162
+ $ai_tools: tools
163
+ } : {}),
164
+ ...errorData,
165
+ ...costOverrideData
166
+ };
167
+ if (fullDebug) {
168
+ // @ts-ignore
169
+ console.log('Sending event to PostHog', properties);
170
+ }
143
171
  client.capture({
144
172
  distinctId: distinctId ?? traceId,
145
173
  event: '$ai_generation',
146
- properties: {
147
- $ai_provider: params.posthogProviderOverride ?? provider,
148
- $ai_model: params.posthogModelOverride ?? model,
149
- $ai_model_parameters: getModelParams(params),
150
- $ai_input: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeInput),
151
- $ai_output_choices: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeOutput),
152
- $ai_http_status: httpStatus,
153
- $ai_input_tokens: usage.inputTokens ?? 0,
154
- $ai_output_tokens: usage.outputTokens ?? 0,
155
- ...additionalTokenValues,
156
- $ai_latency: latency,
157
- $ai_trace_id: traceId,
158
- $ai_base_url: baseURL,
159
- ...params.posthogProperties,
160
- ...(distinctId ? {} : {
161
- $process_person_profile: false
162
- }),
163
- ...(tools ? {
164
- $ai_tools: tools
165
- } : {}),
166
- ...errorData,
167
- ...costOverrideData
168
- },
174
+ properties,
169
175
  groups: params.posthogGroups
170
176
  });
171
177
  }
@@ -516,7 +522,8 @@ const mapVercelPrompt = prompt => {
516
522
  } else {
517
523
  promptsArray = prompt;
518
524
  }
519
- return promptsArray.map(p => {
525
+ // Map and truncate individual content
526
+ const inputs = promptsArray.map(p => {
520
527
  let content = {};
521
528
  if (Array.isArray(p.content)) {
522
529
  content = p.content.map(c => {
@@ -577,6 +584,27 @@ const mapVercelPrompt = prompt => {
577
584
  content
578
585
  };
579
586
  });
587
+ try {
588
+ // Trim the inputs array until its JSON size fits within MAX_OUTPUT_SIZE
589
+ let serialized = JSON.stringify(inputs);
590
+ while (Buffer.byteLength(serialized, 'utf8') > MAX_OUTPUT_SIZE && inputs.length > 0) {
591
+ // Remove oldest message
592
+ inputs.shift();
593
+ // add blank message to beginning of array
594
+ inputs.unshift({
595
+ role: 'assistant',
596
+ content: '[removed message due to size limit]'
597
+ });
598
+ serialized = JSON.stringify(inputs);
599
+ }
600
+ } catch (error) {
601
+ console.error('Error stringifying inputs');
602
+ return [{
603
+ role: 'posthog',
604
+ content: 'An error occurred while processing your request. Please try again.'
605
+ }];
606
+ }
607
+ return inputs;
580
608
  };
581
609
  const mapVercelOutput = result => {
582
610
  // normalize string results to object
@@ -690,7 +718,8 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
690
718
  inputTokens: result.usage.promptTokens,
691
719
  outputTokens: result.usage.completionTokens,
692
720
  ...additionalTokenValues
693
- }
721
+ },
722
+ fullDebug: options.fullDebug
694
723
  });
695
724
  return result;
696
725
  } catch (error) {
@@ -712,7 +741,8 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
712
741
  outputTokens: 0
713
742
  },
714
743
  isError: true,
715
- error: truncate(JSON.stringify(error))
744
+ error: truncate(JSON.stringify(error)),
745
+ fullDebug: options.fullDebug
716
746
  });
717
747
  throw error;
718
748
  }
@@ -778,7 +808,8 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
778
808
  baseURL,
779
809
  params: mergedParams,
780
810
  httpStatus: 200,
781
- usage
811
+ usage,
812
+ fullDebug: options.fullDebug
782
813
  });
783
814
  }
784
815
  });
@@ -804,7 +835,8 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
804
835
  outputTokens: 0
805
836
  },
806
837
  isError: true,
807
- error: truncate(JSON.stringify(error))
838
+ error: truncate(JSON.stringify(error)),
839
+ fullDebug: options.fullDebug
808
840
  });
809
841
  throw error;
810
842
  }