@posthog/ai
Version:
PostHog Node.js AI integrations
405 lines (391 loc) • 12.5 kB
JavaScript
import AnthropicOriginal from '@anthropic-ai/sdk';
import { v4 } from 'uuid';
import { Buffer } from 'buffer';
const STRING_FORMAT = 'utf8';
const getModelParams = params => {
if (!params) {
return {};
}
const modelParams = {};
const paramKeys = ['temperature', 'max_tokens', 'max_completion_tokens', 'top_p', 'frequency_penalty', 'presence_penalty', 'n', 'stop', 'stream', 'streaming'];
for (const key of paramKeys) {
if (key in params && params[key] !== undefined) {
modelParams[key] = params[key];
}
}
return modelParams;
};
const formatResponseAnthropic = response => {
const output = [];
const content = [];
for (const choice of response.content ?? []) {
if (choice?.type === 'text' && choice?.text) {
content.push({
type: 'text',
text: choice.text
});
} else if (choice?.type === 'tool_use' && choice?.name && choice?.id) {
content.push({
type: 'function',
id: choice.id,
function: {
name: choice.name,
arguments: choice.input || {}
}
});
}
}
if (content.length > 0) {
output.push({
role: 'assistant',
content
});
}
return output;
};
const mergeSystemPrompt = (params, provider) => {
{
const messages = params.messages || [];
if (!params.system) {
return messages;
}
const systemMessage = params.system;
return [{
role: 'system',
content: systemMessage
}, ...messages];
}
};
const withPrivacyMode = (client, privacyMode, input) => {
return client.privacy_mode || privacyMode ? null : input;
};
/**
* Extract available tool calls from the request parameters.
* These are the tools provided to the LLM, not the tool calls in the response.
*/
const extractAvailableToolCalls = (provider, params) => {
{
if (params.tools) {
return params.tools;
}
return null;
}
};
function sanitizeValues(obj) {
if (obj === undefined || obj === null) {
return obj;
}
const jsonSafe = JSON.parse(JSON.stringify(obj));
if (typeof jsonSafe === 'string') {
return Buffer.from(jsonSafe, STRING_FORMAT).toString(STRING_FORMAT);
} else if (Array.isArray(jsonSafe)) {
return jsonSafe.map(sanitizeValues);
} else if (jsonSafe && typeof jsonSafe === 'object') {
return Object.fromEntries(Object.entries(jsonSafe).map(([k, v]) => [k, sanitizeValues(v)]));
}
return jsonSafe;
}
const sendEventToPosthog = async ({
client,
distinctId,
traceId,
model,
provider,
input,
output,
latency,
baseURL,
params,
httpStatus = 200,
usage = {},
isError = false,
error,
tools,
captureImmediate = false
}) => {
if (!client.capture) {
return Promise.resolve();
}
// sanitize input and output for UTF-8 validity
const safeInput = sanitizeValues(input);
const safeOutput = sanitizeValues(output);
const safeError = sanitizeValues(error);
let errorData = {};
if (isError) {
errorData = {
$ai_is_error: true,
$ai_error: safeError
};
}
let costOverrideData = {};
if (params.posthogCostOverride) {
const inputCostUSD = (params.posthogCostOverride.inputCost ?? 0) * (usage.inputTokens ?? 0);
const outputCostUSD = (params.posthogCostOverride.outputCost ?? 0) * (usage.outputTokens ?? 0);
costOverrideData = {
$ai_input_cost_usd: inputCostUSD,
$ai_output_cost_usd: outputCostUSD,
$ai_total_cost_usd: inputCostUSD + outputCostUSD
};
}
const additionalTokenValues = {
...(usage.reasoningTokens ? {
$ai_reasoning_tokens: usage.reasoningTokens
} : {}),
...(usage.cacheReadInputTokens ? {
$ai_cache_read_input_tokens: usage.cacheReadInputTokens
} : {}),
...(usage.cacheCreationInputTokens ? {
$ai_cache_creation_input_tokens: usage.cacheCreationInputTokens
} : {})
};
const properties = {
$ai_provider: params.posthogProviderOverride ?? provider,
$ai_model: params.posthogModelOverride ?? model,
$ai_model_parameters: getModelParams(params),
$ai_input: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeInput),
$ai_output_choices: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeOutput),
$ai_http_status: httpStatus,
$ai_input_tokens: usage.inputTokens ?? 0,
$ai_output_tokens: usage.outputTokens ?? 0,
...additionalTokenValues,
$ai_latency: latency,
$ai_trace_id: traceId,
$ai_base_url: baseURL,
...params.posthogProperties,
...(distinctId ? {} : {
$process_person_profile: false
}),
...(tools ? {
$ai_tools: tools
} : {}),
...errorData,
...costOverrideData
};
const event = {
distinctId: distinctId ?? traceId,
event: '$ai_generation',
properties,
groups: params.posthogGroups
};
if (captureImmediate) {
// await capture promise to send single event in serverless environments
await client.captureImmediate(event);
} else {
client.capture(event);
}
};
// Type guards for safer type checking
const isObject = value => {
return value !== null && typeof value === 'object' && !Array.isArray(value);
};
const REDACTED_IMAGE_PLACEHOLDER = '[base64 image redacted]';
// ============================================
// Common Message Processing
// ============================================
const processMessages = (messages, transformContent) => {
if (!messages) return messages;
const processContent = content => {
if (typeof content === 'string') return content;
if (!content) return content;
if (Array.isArray(content)) {
return content.map(transformContent);
}
// Handle single object content
return transformContent(content);
};
const processMessage = msg => {
if (!isObject(msg) || !('content' in msg)) return msg;
return {
...msg,
content: processContent(msg.content)
};
};
// Handle both arrays and single messages
if (Array.isArray(messages)) {
return messages.map(processMessage);
}
return processMessage(messages);
};
const sanitizeAnthropicImage = item => {
if (!isObject(item)) return item;
// Handle Anthropic's image format
if (item.type === 'image' && 'source' in item && isObject(item.source) && item.source.type === 'base64' && 'data' in item.source) {
return {
...item,
source: {
...item.source,
data: REDACTED_IMAGE_PLACEHOLDER
}
};
}
return item;
};
const sanitizeAnthropic = data => {
return processMessages(data, sanitizeAnthropicImage);
};
class PostHogAnthropic extends AnthropicOriginal {
constructor(config) {
const {
posthog,
...anthropicConfig
} = config;
super(anthropicConfig);
this.phClient = posthog;
this.messages = new WrappedMessages(this, this.phClient);
}
}
class WrappedMessages extends AnthropicOriginal.Messages {
constructor(parentClient, phClient) {
super(parentClient);
this.phClient = phClient;
}
create(body, options) {
const {
posthogDistinctId,
posthogTraceId,
posthogProperties,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
posthogPrivacyMode = false,
posthogGroups,
posthogCaptureImmediate,
...anthropicParams
} = body;
const traceId = posthogTraceId ?? v4();
const startTime = Date.now();
const parentPromise = super.create(anthropicParams, options);
if (anthropicParams.stream) {
return parentPromise.then(value => {
let accumulatedContent = '';
const usage = {
inputTokens: 0,
outputTokens: 0,
cacheCreationInputTokens: 0,
cacheReadInputTokens: 0
};
if ('tee' in value) {
const [stream1, stream2] = value.tee();
(async () => {
try {
for await (const chunk of stream1) {
if ('delta' in chunk) {
if ('text' in chunk.delta) {
const delta = chunk?.delta?.text ?? '';
accumulatedContent += delta;
}
}
if (chunk.type == 'message_start') {
usage.inputTokens = chunk.message.usage.input_tokens ?? 0;
usage.cacheCreationInputTokens = chunk.message.usage.cache_creation_input_tokens ?? 0;
usage.cacheReadInputTokens = chunk.message.usage.cache_read_input_tokens ?? 0;
}
if ('usage' in chunk) {
usage.outputTokens = chunk.usage.output_tokens ?? 0;
}
}
const latency = (Date.now() - startTime) / 1000;
const availableTools = extractAvailableToolCalls('anthropic', anthropicParams);
await sendEventToPosthog({
client: this.phClient,
distinctId: posthogDistinctId,
traceId,
model: anthropicParams.model,
provider: 'anthropic',
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams, 'anthropic')),
output: [{
content: accumulatedContent,
role: 'assistant'
}],
latency,
baseURL: this.baseURL ?? '',
params: body,
httpStatus: 200,
usage,
tools: availableTools,
captureImmediate: posthogCaptureImmediate
});
} catch (error) {
// error handling
await sendEventToPosthog({
client: this.phClient,
distinctId: posthogDistinctId,
traceId,
model: anthropicParams.model,
provider: 'anthropic',
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
output: [],
latency: 0,
baseURL: this.baseURL ?? '',
params: body,
httpStatus: error?.status ? error.status : 500,
usage: {
inputTokens: 0,
outputTokens: 0
},
isError: true,
error: JSON.stringify(error),
captureImmediate: posthogCaptureImmediate
});
}
})();
// Return the other stream to the user
return stream2;
}
return value;
});
} else {
const wrappedPromise = parentPromise.then(async result => {
if ('content' in result) {
const latency = (Date.now() - startTime) / 1000;
const availableTools = extractAvailableToolCalls('anthropic', anthropicParams);
await sendEventToPosthog({
client: this.phClient,
distinctId: posthogDistinctId,
traceId,
model: anthropicParams.model,
provider: 'anthropic',
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
output: formatResponseAnthropic(result),
latency,
baseURL: this.baseURL ?? '',
params: body,
httpStatus: 200,
usage: {
inputTokens: result.usage.input_tokens ?? 0,
outputTokens: result.usage.output_tokens ?? 0,
cacheCreationInputTokens: result.usage.cache_creation_input_tokens ?? 0,
cacheReadInputTokens: result.usage.cache_read_input_tokens ?? 0
},
tools: availableTools,
captureImmediate: posthogCaptureImmediate
});
}
return result;
}, async error => {
await sendEventToPosthog({
client: this.phClient,
distinctId: posthogDistinctId,
traceId,
model: anthropicParams.model,
provider: 'anthropic',
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
output: [],
latency: 0,
baseURL: this.baseURL ?? '',
params: body,
httpStatus: error?.status ? error.status : 500,
usage: {
inputTokens: 0,
outputTokens: 0
},
isError: true,
error: JSON.stringify(error),
captureImmediate: posthogCaptureImmediate
});
throw error;
});
return wrappedPromise;
}
}
}
export { PostHogAnthropic as Anthropic, PostHogAnthropic, WrappedMessages, PostHogAnthropic as default };
//# sourceMappingURL=index.mjs.map