@posthog/ai 3.3.2 → 4.0.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
@@ -1,4 +1,4 @@
1
- import OpenAIOrignal, { AzureOpenAI } from 'openai';
1
+ import OpenAIOrignal, { ClientOptions as ClientOptions$1, AzureOpenAI } from 'openai';
2
2
  import { PostHog } from 'posthog-node';
3
3
  import { RequestOptions, APIPromise } from 'openai/core';
4
4
  import { Stream } from 'openai/streaming';
@@ -35,7 +35,7 @@ type ChatCompletionCreateParamsBase$1 = OpenAIOrignal.Chat.Completions.ChatCompl
35
35
  type ChatCompletionCreateParamsNonStreaming$1 = OpenAIOrignal.Chat.Completions.ChatCompletionCreateParamsNonStreaming;
36
36
  type ChatCompletionCreateParamsStreaming$1 = OpenAIOrignal.Chat.Completions.ChatCompletionCreateParamsStreaming;
37
37
 
38
- interface MonitoringOpenAIConfig$1 {
38
+ interface MonitoringOpenAIConfig$1 extends ClientOptions$1 {
39
39
  apiKey: string;
40
40
  posthog: PostHog;
41
41
  baseURL?: string;
package/lib/index.esm.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import OpenAIOrignal, { AzureOpenAI } from 'openai';
2
2
  import * as uuid from 'uuid';
3
3
  import { v4 } from 'uuid';
4
+ import { Buffer } from 'buffer';
4
5
  import { experimental_wrapLanguageModel } from 'ai';
5
6
  import AnthropicOriginal from '@anthropic-ai/sdk';
6
7
 
@@ -59,6 +60,20 @@ const mergeSystemPrompt = (params, provider) => {
59
60
  const withPrivacyMode = (client, privacyMode, input) => {
60
61
  return client.privacy_mode || privacyMode ? null : input;
61
62
  };
63
+ function sanitizeValues(obj) {
64
+ if (obj === undefined || obj === null) {
65
+ return obj;
66
+ }
67
+ const jsonSafe = JSON.parse(JSON.stringify(obj));
68
+ if (typeof jsonSafe === 'string') {
69
+ return Buffer.from(jsonSafe, 'utf8').toString('utf8');
70
+ } else if (Array.isArray(jsonSafe)) {
71
+ return jsonSafe.map(sanitizeValues);
72
+ } else if (jsonSafe && typeof jsonSafe === 'object') {
73
+ return Object.fromEntries(Object.entries(jsonSafe).map(([k, v]) => [k, sanitizeValues(v)]));
74
+ }
75
+ return jsonSafe;
76
+ }
62
77
  const sendEventToPosthog = ({
63
78
  client,
64
79
  distinctId,
@@ -77,11 +92,15 @@ const sendEventToPosthog = ({
77
92
  tools
78
93
  }) => {
79
94
  if (client.capture) {
95
+ // sanitize input and output for UTF-8 validity
96
+ const safeInput = sanitizeValues(input);
97
+ const safeOutput = sanitizeValues(output);
98
+ const safeError = sanitizeValues(error);
80
99
  let errorData = {};
81
100
  if (isError) {
82
101
  errorData = {
83
102
  $ai_is_error: true,
84
- $ai_error: error
103
+ $ai_error: safeError
85
104
  };
86
105
  }
87
106
  let costOverrideData = {};
@@ -112,8 +131,8 @@ const sendEventToPosthog = ({
112
131
  $ai_provider: params.posthogProviderOverride ?? provider,
113
132
  $ai_model: params.posthogModelOverride ?? model,
114
133
  $ai_model_parameters: getModelParams(params),
115
- $ai_input: withPrivacyMode(client, params.posthogPrivacyMode ?? false, input),
116
- $ai_output_choices: withPrivacyMode(client, params.posthogPrivacyMode ?? false, output),
134
+ $ai_input: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeInput),
135
+ $ai_output_choices: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeOutput),
117
136
  $ai_http_status: httpStatus,
118
137
  $ai_input_tokens: usage.inputTokens ?? 0,
119
138
  $ai_output_tokens: usage.outputTokens ?? 0,