@posthog/ai 4.0.0 → 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/CHANGELOG.md +5 -1
- package/lib/anthropic/index.cjs.js +22 -3
- package/lib/anthropic/index.cjs.js.map +1 -1
- package/lib/anthropic/index.esm.js +22 -3
- package/lib/anthropic/index.esm.js.map +1 -1
- package/lib/index.cjs.js +22 -3
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.esm.js +22 -3
- package/lib/index.esm.js.map +1 -1
- package/lib/langchain/index.cjs.js +1 -0
- package/lib/langchain/index.cjs.js.map +1 -1
- package/lib/langchain/index.esm.js +1 -0
- package/lib/langchain/index.esm.js.map +1 -1
- package/lib/openai/index.cjs.js +22 -3
- package/lib/openai/index.cjs.js.map +1 -1
- package/lib/openai/index.esm.js +22 -3
- package/lib/openai/index.esm.js.map +1 -1
- package/lib/vercel/index.cjs.js +22 -3
- package/lib/vercel/index.cjs.js.map +1 -1
- package/lib/vercel/index.esm.js +22 -3
- package/lib/vercel/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/utils.ts +24 -3
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:
|
|
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,
|
|
116
|
-
$ai_output_choices: withPrivacyMode(client, params.posthogPrivacyMode ?? false,
|
|
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,
|