@posthog/ai 4.1.0 → 4.2.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/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,45 @@ 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', JSON.stringify(properties));
170
+ try {
171
+ // @ts-ignore
172
+ console.log('Size of properties (kb)', Math.round(Buffer.byteLength(JSON.stringify(properties), STRING_FORMAT) / 1024 * 10000) / 10000);
173
+ // @ts-ignore
174
+ console.log('Size of properties (mb)', Math.round(Buffer.byteLength(JSON.stringify(properties), STRING_FORMAT) / 1024 / 1024 * 10000) / 10000);
175
+ } catch (error) {
176
+ console.error('Error printing size of properties', error);
177
+ }
178
+ }
143
179
  client.capture({
144
180
  distinctId: distinctId ?? traceId,
145
181
  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
- },
182
+ properties,
169
183
  groups: params.posthogGroups
170
184
  });
171
185
  }
@@ -516,7 +530,8 @@ const mapVercelPrompt = prompt => {
516
530
  } else {
517
531
  promptsArray = prompt;
518
532
  }
519
- return promptsArray.map(p => {
533
+ // Map and truncate individual content
534
+ const inputs = promptsArray.map(p => {
520
535
  let content = {};
521
536
  if (Array.isArray(p.content)) {
522
537
  content = p.content.map(c => {
@@ -577,6 +592,30 @@ const mapVercelPrompt = prompt => {
577
592
  content
578
593
  };
579
594
  });
595
+ try {
596
+ // Trim the inputs array until its JSON size fits within MAX_OUTPUT_SIZE
597
+ let serialized = JSON.stringify(inputs);
598
+ let removedCount = 0;
599
+ while (Buffer.byteLength(serialized, 'utf8') > MAX_OUTPUT_SIZE && inputs.length > 0) {
600
+ inputs.shift();
601
+ removedCount++;
602
+ serialized = JSON.stringify(inputs);
603
+ }
604
+ if (removedCount > 0) {
605
+ // Add one placeholder to indicate how many were removed
606
+ inputs.unshift({
607
+ role: 'assistant',
608
+ content: `[${removedCount} message${removedCount === 1 ? '' : 's'} removed due to size limit]`
609
+ });
610
+ }
611
+ } catch (error) {
612
+ console.error('Error stringifying inputs', error);
613
+ return [{
614
+ role: 'posthog',
615
+ content: 'An error occurred while processing your request. Please try again.'
616
+ }];
617
+ }
618
+ return inputs;
580
619
  };
581
620
  const mapVercelOutput = result => {
582
621
  // normalize string results to object
@@ -690,7 +729,8 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
690
729
  inputTokens: result.usage.promptTokens,
691
730
  outputTokens: result.usage.completionTokens,
692
731
  ...additionalTokenValues
693
- }
732
+ },
733
+ fullDebug: options.fullDebug
694
734
  });
695
735
  return result;
696
736
  } catch (error) {
@@ -712,7 +752,8 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
712
752
  outputTokens: 0
713
753
  },
714
754
  isError: true,
715
- error: truncate(JSON.stringify(error))
755
+ error: truncate(JSON.stringify(error)),
756
+ fullDebug: options.fullDebug
716
757
  });
717
758
  throw error;
718
759
  }
@@ -778,7 +819,8 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
778
819
  baseURL,
779
820
  params: mergedParams,
780
821
  httpStatus: 200,
781
- usage
822
+ usage,
823
+ fullDebug: options.fullDebug
782
824
  });
783
825
  }
784
826
  });
@@ -804,7 +846,8 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
804
846
  outputTokens: 0
805
847
  },
806
848
  isError: true,
807
- error: truncate(JSON.stringify(error))
849
+ error: truncate(JSON.stringify(error)),
850
+ fullDebug: options.fullDebug
808
851
  });
809
852
  throw error;
810
853
  }