@posthog/ai
Version:
PostHog Node.js AI integrations
1 lines • 103 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":["../../src/utils.ts","../../../../node_modules/.pnpm/decamelize@1.2.0/node_modules/decamelize/index.js","../../../../node_modules/.pnpm/camelcase@6.3.0/node_modules/camelcase/index.js","../../../../node_modules/.pnpm/@langchain+core@0.3.66_@opentelemetry+api@1.9.0_openai@5.10.1_ws@8.18.3_zod@3.25.76_/node_modules/@langchain/core/dist/load/map_keys.js","../../../../node_modules/.pnpm/@langchain+core@0.3.66_@opentelemetry+api@1.9.0_openai@5.10.1_ws@8.18.3_zod@3.25.76_/node_modules/@langchain/core/dist/load/serializable.js","../../../../node_modules/.pnpm/@langchain+core@0.3.66_@opentelemetry+api@1.9.0_openai@5.10.1_ws@8.18.3_zod@3.25.76_/node_modules/@langchain/core/dist/utils/env.js","../../../../node_modules/.pnpm/@langchain+core@0.3.66_@opentelemetry+api@1.9.0_openai@5.10.1_ws@8.18.3_zod@3.25.76_/node_modules/@langchain/core/dist/callbacks/base.js","../../src/typeGuards.ts","../../src/sanitization.ts","../../src/langchain/callbacks.ts"],"sourcesContent":["import { PostHog } from 'posthog-node'\nimport { Buffer } from 'buffer'\nimport OpenAIOrignal from 'openai'\nimport AnthropicOriginal from '@anthropic-ai/sdk'\nimport type { ChatCompletionTool } from 'openai/resources/chat/completions'\nimport type { Tool as GeminiTool } from '@google/genai'\nimport type { FormattedMessage, FormattedContent, TokenUsage } from './types'\n\ntype ChatCompletionCreateParamsBase = OpenAIOrignal.Chat.Completions.ChatCompletionCreateParams\ntype MessageCreateParams = AnthropicOriginal.Messages.MessageCreateParams\ntype ResponseCreateParams = OpenAIOrignal.Responses.ResponseCreateParams\ntype AnthropicTool = AnthropicOriginal.Tool\n\n// limit large outputs by truncating to 200kb (approx 200k bytes)\nexport const MAX_OUTPUT_SIZE = 200000\nconst STRING_FORMAT = 'utf8'\n\nexport interface MonitoringParams {\n posthogDistinctId?: string\n posthogTraceId?: string\n posthogProperties?: Record<string, any>\n posthogPrivacyMode?: boolean\n posthogGroups?: Record<string, any>\n posthogModelOverride?: string\n posthogProviderOverride?: string\n posthogCostOverride?: CostOverride\n posthogCaptureImmediate?: boolean\n}\n\nexport interface CostOverride {\n inputCost: number\n outputCost: number\n}\n\nexport const getModelParams = (\n params: ((ChatCompletionCreateParamsBase | MessageCreateParams | ResponseCreateParams) & MonitoringParams) | null\n): Record<string, any> => {\n if (!params) {\n return {}\n }\n const modelParams: Record<string, any> = {}\n const paramKeys = [\n 'temperature',\n 'max_tokens',\n 'max_completion_tokens',\n 'top_p',\n 'frequency_penalty',\n 'presence_penalty',\n 'n',\n 'stop',\n 'stream',\n 'streaming',\n ] as const\n\n for (const key of paramKeys) {\n if (key in params && (params as any)[key] !== undefined) {\n modelParams[key] = (params as any)[key]\n }\n }\n return modelParams\n}\n\n/**\n * Helper to format responses (non-streaming) for consumption, mirroring Python's openai vs. anthropic approach.\n */\nexport const formatResponse = (response: any, provider: string): FormattedMessage[] => {\n if (!response) {\n return []\n }\n if (provider === 'anthropic') {\n return formatResponseAnthropic(response)\n } else if (provider === 'openai') {\n return formatResponseOpenAI(response)\n } else if (provider === 'gemini') {\n return formatResponseGemini(response)\n }\n return []\n}\n\nexport const formatResponseAnthropic = (response: any): FormattedMessage[] => {\n const output: FormattedMessage[] = []\n const content: FormattedContent = []\n\n for (const choice of response.content ?? []) {\n if (choice?.type === 'text' && choice?.text) {\n content.push({ type: 'text', text: choice.text })\n } else if (choice?.type === 'tool_use' && choice?.name && choice?.id) {\n content.push({\n type: 'function',\n id: choice.id,\n function: {\n name: choice.name,\n arguments: choice.input || {},\n },\n })\n }\n }\n\n if (content.length > 0) {\n output.push({\n role: 'assistant',\n content,\n })\n }\n\n return output\n}\n\nexport const formatResponseOpenAI = (response: any): FormattedMessage[] => {\n const output: FormattedMessage[] = []\n\n if (response.choices) {\n for (const choice of response.choices) {\n const content: FormattedContent = []\n let role = 'assistant'\n\n if (choice.message) {\n if (choice.message.role) {\n role = choice.message.role\n }\n\n if (choice.message.content) {\n content.push({ type: 'text', text: choice.message.content })\n }\n\n if (choice.message.tool_calls) {\n for (const toolCall of choice.message.tool_calls) {\n content.push({\n type: 'function',\n id: toolCall.id,\n function: {\n name: toolCall.function.name,\n arguments: toolCall.function.arguments,\n },\n })\n }\n }\n }\n\n if (content.length > 0) {\n output.push({\n role,\n content,\n })\n }\n }\n }\n\n // Handle Responses API format\n if (response.output) {\n const content: FormattedContent = []\n let role = 'assistant'\n\n for (const item of response.output) {\n if (item.type === 'message') {\n role = item.role\n\n if (item.content && Array.isArray(item.content)) {\n for (const contentItem of item.content) {\n if (contentItem.type === 'output_text' && contentItem.text) {\n content.push({ type: 'text', text: contentItem.text })\n } else if (contentItem.text) {\n content.push({ type: 'text', text: contentItem.text })\n } else if (contentItem.type === 'input_image' && contentItem.image_url) {\n content.push({\n type: 'image',\n image: contentItem.image_url,\n })\n }\n }\n } else if (item.content) {\n content.push({ type: 'text', text: String(item.content) })\n }\n } else if (item.type === 'function_call') {\n content.push({\n type: 'function',\n id: item.call_id || item.id || '',\n function: {\n name: item.name,\n arguments: item.arguments || {},\n },\n })\n }\n }\n\n if (content.length > 0) {\n output.push({\n role,\n content,\n })\n }\n }\n\n return output\n}\n\nexport const formatResponseGemini = (response: any): FormattedMessage[] => {\n const output: FormattedMessage[] = []\n\n if (response.candidates && Array.isArray(response.candidates)) {\n for (const candidate of response.candidates) {\n if (candidate.content && candidate.content.parts) {\n const content: FormattedContent = []\n\n for (const part of candidate.content.parts) {\n if (part.text) {\n content.push({ type: 'text', text: part.text })\n } else if (part.functionCall) {\n content.push({\n type: 'function',\n function: {\n name: part.functionCall.name,\n arguments: part.functionCall.args,\n },\n })\n }\n }\n\n if (content.length > 0) {\n output.push({\n role: 'assistant',\n content,\n })\n }\n } else if (candidate.text) {\n output.push({\n role: 'assistant',\n content: [{ type: 'text', text: candidate.text }],\n })\n }\n }\n } else if (response.text) {\n output.push({\n role: 'assistant',\n content: [{ type: 'text', text: response.text }],\n })\n }\n\n return output\n}\n\nexport const mergeSystemPrompt = (params: MessageCreateParams & MonitoringParams, provider: string): any => {\n if (provider == 'anthropic') {\n const messages = params.messages || []\n if (!(params as any).system) {\n return messages\n }\n const systemMessage = (params as any).system\n return [{ role: 'system', content: systemMessage }, ...messages]\n }\n return params.messages\n}\n\nexport const withPrivacyMode = (client: PostHog, privacyMode: boolean, input: any): any => {\n return (client as any).privacy_mode || privacyMode ? null : input\n}\n\nexport const truncate = (str: string): string => {\n try {\n const buffer = Buffer.from(str, STRING_FORMAT)\n if (buffer.length <= MAX_OUTPUT_SIZE) {\n return str\n }\n const truncatedBuffer = buffer.slice(0, MAX_OUTPUT_SIZE)\n return `${truncatedBuffer.toString(STRING_FORMAT)}... [truncated]`\n } catch (error) {\n console.error('Error truncating, likely not a string')\n return str\n }\n}\n\n/**\n * Extract available tool calls from the request parameters.\n * These are the tools provided to the LLM, not the tool calls in the response.\n */\nexport const extractAvailableToolCalls = (\n provider: string,\n params: any\n): ChatCompletionTool[] | AnthropicTool[] | GeminiTool[] | null => {\n if (provider === 'anthropic') {\n if (params.tools) {\n return params.tools\n }\n\n return null\n } else if (provider === 'gemini') {\n if (params.config && params.config.tools) {\n return params.config.tools\n }\n\n return null\n } else if (provider === 'openai') {\n if (params.tools) {\n return params.tools\n }\n\n return null\n } else if (provider === 'vercel') {\n // Vercel AI SDK stores tools in params.mode.tools when mode type is 'regular'\n if (params.mode?.type === 'regular' && params.mode.tools) {\n return params.mode.tools\n }\n\n return null\n }\n\n return null\n}\n\nexport type SendEventToPosthogParams = {\n client: PostHog\n distinctId?: string\n traceId: string\n model: string\n provider: string\n input: any\n output: any\n latency: number\n baseURL: string\n httpStatus: number\n usage?: TokenUsage\n params: (ChatCompletionCreateParamsBase | MessageCreateParams | ResponseCreateParams) & MonitoringParams\n isError?: boolean\n error?: string\n tools?: ChatCompletionTool[] | AnthropicTool[] | GeminiTool[] | null\n captureImmediate?: boolean\n}\n\nfunction sanitizeValues(obj: any): any {\n if (obj === undefined || obj === null) {\n return obj\n }\n const jsonSafe = JSON.parse(JSON.stringify(obj))\n if (typeof jsonSafe === 'string') {\n return Buffer.from(jsonSafe, STRING_FORMAT).toString(STRING_FORMAT)\n } else if (Array.isArray(jsonSafe)) {\n return jsonSafe.map(sanitizeValues)\n } else if (jsonSafe && typeof jsonSafe === 'object') {\n return Object.fromEntries(Object.entries(jsonSafe).map(([k, v]) => [k, sanitizeValues(v)]))\n }\n return jsonSafe\n}\n\nexport const sendEventToPosthog = async ({\n client,\n distinctId,\n traceId,\n model,\n provider,\n input,\n output,\n latency,\n baseURL,\n params,\n httpStatus = 200,\n usage = {},\n isError = false,\n error,\n tools,\n captureImmediate = false,\n}: SendEventToPosthogParams): Promise<void> => {\n if (!client.capture) {\n return Promise.resolve()\n }\n // sanitize input and output for UTF-8 validity\n const safeInput = sanitizeValues(input)\n const safeOutput = sanitizeValues(output)\n const safeError = sanitizeValues(error)\n\n let errorData = {}\n if (isError) {\n errorData = {\n $ai_is_error: true,\n $ai_error: safeError,\n }\n }\n let costOverrideData = {}\n if (params.posthogCostOverride) {\n const inputCostUSD = (params.posthogCostOverride.inputCost ?? 0) * (usage.inputTokens ?? 0)\n const outputCostUSD = (params.posthogCostOverride.outputCost ?? 0) * (usage.outputTokens ?? 0)\n costOverrideData = {\n $ai_input_cost_usd: inputCostUSD,\n $ai_output_cost_usd: outputCostUSD,\n $ai_total_cost_usd: inputCostUSD + outputCostUSD,\n }\n }\n\n const additionalTokenValues = {\n ...(usage.reasoningTokens ? { $ai_reasoning_tokens: usage.reasoningTokens } : {}),\n ...(usage.cacheReadInputTokens ? { $ai_cache_read_input_tokens: usage.cacheReadInputTokens } : {}),\n ...(usage.cacheCreationInputTokens ? { $ai_cache_creation_input_tokens: usage.cacheCreationInputTokens } : {}),\n }\n\n const properties = {\n $ai_provider: params.posthogProviderOverride ?? provider,\n $ai_model: params.posthogModelOverride ?? model,\n $ai_model_parameters: getModelParams(params),\n $ai_input: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeInput),\n $ai_output_choices: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeOutput),\n $ai_http_status: httpStatus,\n $ai_input_tokens: usage.inputTokens ?? 0,\n $ai_output_tokens: usage.outputTokens ?? 0,\n ...additionalTokenValues,\n $ai_latency: latency,\n $ai_trace_id: traceId,\n $ai_base_url: baseURL,\n ...params.posthogProperties,\n ...(distinctId ? {} : { $process_person_profile: false }),\n ...(tools ? { $ai_tools: tools } : {}),\n ...errorData,\n ...costOverrideData,\n }\n\n const event = {\n distinctId: distinctId ?? traceId,\n event: '$ai_generation',\n properties,\n groups: params.posthogGroups,\n }\n\n if (captureImmediate) {\n // await capture promise to send single event in serverless environments\n await client.captureImmediate(event)\n } else {\n client.capture(event)\n }\n}\n","'use strict';\nmodule.exports = function (str, sep) {\n\tif (typeof str !== 'string') {\n\t\tthrow new TypeError('Expected a string');\n\t}\n\n\tsep = typeof sep === 'undefined' ? '_' : sep;\n\n\treturn str\n\t\t.replace(/([a-z\\d])([A-Z])/g, '$1' + sep + '$2')\n\t\t.replace(/([A-Z]+)([A-Z][a-z\\d]+)/g, '$1' + sep + '$2')\n\t\t.toLowerCase();\n};\n","'use strict';\n\nconst UPPERCASE = /[\\p{Lu}]/u;\nconst LOWERCASE = /[\\p{Ll}]/u;\nconst LEADING_CAPITAL = /^[\\p{Lu}](?![\\p{Lu}])/gu;\nconst IDENTIFIER = /([\\p{Alpha}\\p{N}_]|$)/u;\nconst SEPARATORS = /[_.\\- ]+/;\n\nconst LEADING_SEPARATORS = new RegExp('^' + SEPARATORS.source);\nconst SEPARATORS_AND_IDENTIFIER = new RegExp(SEPARATORS.source + IDENTIFIER.source, 'gu');\nconst NUMBERS_AND_IDENTIFIER = new RegExp('\\\\d+' + IDENTIFIER.source, 'gu');\n\nconst preserveCamelCase = (string, toLowerCase, toUpperCase) => {\n\tlet isLastCharLower = false;\n\tlet isLastCharUpper = false;\n\tlet isLastLastCharUpper = false;\n\n\tfor (let i = 0; i < string.length; i++) {\n\t\tconst character = string[i];\n\n\t\tif (isLastCharLower && UPPERCASE.test(character)) {\n\t\t\tstring = string.slice(0, i) + '-' + string.slice(i);\n\t\t\tisLastCharLower = false;\n\t\t\tisLastLastCharUpper = isLastCharUpper;\n\t\t\tisLastCharUpper = true;\n\t\t\ti++;\n\t\t} else if (isLastCharUpper && isLastLastCharUpper && LOWERCASE.test(character)) {\n\t\t\tstring = string.slice(0, i - 1) + '-' + string.slice(i - 1);\n\t\t\tisLastLastCharUpper = isLastCharUpper;\n\t\t\tisLastCharUpper = false;\n\t\t\tisLastCharLower = true;\n\t\t} else {\n\t\t\tisLastCharLower = toLowerCase(character) === character && toUpperCase(character) !== character;\n\t\t\tisLastLastCharUpper = isLastCharUpper;\n\t\t\tisLastCharUpper = toUpperCase(character) === character && toLowerCase(character) !== character;\n\t\t}\n\t}\n\n\treturn string;\n};\n\nconst preserveConsecutiveUppercase = (input, toLowerCase) => {\n\tLEADING_CAPITAL.lastIndex = 0;\n\n\treturn input.replace(LEADING_CAPITAL, m1 => toLowerCase(m1));\n};\n\nconst postProcess = (input, toUpperCase) => {\n\tSEPARATORS_AND_IDENTIFIER.lastIndex = 0;\n\tNUMBERS_AND_IDENTIFIER.lastIndex = 0;\n\n\treturn input.replace(SEPARATORS_AND_IDENTIFIER, (_, identifier) => toUpperCase(identifier))\n\t\t.replace(NUMBERS_AND_IDENTIFIER, m => toUpperCase(m));\n};\n\nconst camelCase = (input, options) => {\n\tif (!(typeof input === 'string' || Array.isArray(input))) {\n\t\tthrow new TypeError('Expected the input to be `string | string[]`');\n\t}\n\n\toptions = {\n\t\tpascalCase: false,\n\t\tpreserveConsecutiveUppercase: false,\n\t\t...options\n\t};\n\n\tif (Array.isArray(input)) {\n\t\tinput = input.map(x => x.trim())\n\t\t\t.filter(x => x.length)\n\t\t\t.join('-');\n\t} else {\n\t\tinput = input.trim();\n\t}\n\n\tif (input.length === 0) {\n\t\treturn '';\n\t}\n\n\tconst toLowerCase = options.locale === false ?\n\t\tstring => string.toLowerCase() :\n\t\tstring => string.toLocaleLowerCase(options.locale);\n\tconst toUpperCase = options.locale === false ?\n\t\tstring => string.toUpperCase() :\n\t\tstring => string.toLocaleUpperCase(options.locale);\n\n\tif (input.length === 1) {\n\t\treturn options.pascalCase ? toUpperCase(input) : toLowerCase(input);\n\t}\n\n\tconst hasUpperCase = input !== toLowerCase(input);\n\n\tif (hasUpperCase) {\n\t\tinput = preserveCamelCase(input, toLowerCase, toUpperCase);\n\t}\n\n\tinput = input.replace(LEADING_SEPARATORS, '');\n\n\tif (options.preserveConsecutiveUppercase) {\n\t\tinput = preserveConsecutiveUppercase(input, toLowerCase);\n\t} else {\n\t\tinput = toLowerCase(input);\n\t}\n\n\tif (options.pascalCase) {\n\t\tinput = toUpperCase(input.charAt(0)) + input.slice(1);\n\t}\n\n\treturn postProcess(input, toUpperCase);\n};\n\nmodule.exports = camelCase;\n// TODO: Remove this for the next major release\nmodule.exports.default = camelCase;\n","import snakeCase from \"decamelize\";\nimport camelCase from \"camelcase\";\nexport function keyToJson(key, map) {\n return map?.[key] || snakeCase(key);\n}\nexport function keyFromJson(key, map) {\n return map?.[key] || camelCase(key);\n}\nexport function mapKeys(fields, mapper, map) {\n const mapped = {};\n for (const key in fields) {\n if (Object.hasOwn(fields, key)) {\n mapped[mapper(key, map)] = fields[key];\n }\n }\n return mapped;\n}\n","import { keyToJson, mapKeys } from \"./map_keys.js\";\nfunction shallowCopy(obj) {\n return Array.isArray(obj) ? [...obj] : { ...obj };\n}\nfunction replaceSecrets(root, secretsMap) {\n const result = shallowCopy(root);\n for (const [path, secretId] of Object.entries(secretsMap)) {\n const [last, ...partsReverse] = path.split(\".\").reverse();\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let current = result;\n for (const part of partsReverse.reverse()) {\n if (current[part] === undefined) {\n break;\n }\n current[part] = shallowCopy(current[part]);\n current = current[part];\n }\n if (current[last] !== undefined) {\n current[last] = {\n lc: 1,\n type: \"secret\",\n id: [secretId],\n };\n }\n }\n return result;\n}\n/**\n * Get a unique name for the module, rather than parent class implementations.\n * Should not be subclassed, subclass lc_name above instead.\n */\nexport function get_lc_unique_name(\n// eslint-disable-next-line @typescript-eslint/no-use-before-define\nserializableClass) {\n // \"super\" here would refer to the parent class of Serializable,\n // when we want the parent class of the module actually calling this method.\n const parentClass = Object.getPrototypeOf(serializableClass);\n const lcNameIsSubclassed = typeof serializableClass.lc_name === \"function\" &&\n (typeof parentClass.lc_name !== \"function\" ||\n serializableClass.lc_name() !== parentClass.lc_name());\n if (lcNameIsSubclassed) {\n return serializableClass.lc_name();\n }\n else {\n return serializableClass.name;\n }\n}\nexport class Serializable {\n /**\n * The name of the serializable. Override to provide an alias or\n * to preserve the serialized module name in minified environments.\n *\n * Implemented as a static method to support loading logic.\n */\n static lc_name() {\n return this.name;\n }\n /**\n * The final serialized identifier for the module.\n */\n get lc_id() {\n return [\n ...this.lc_namespace,\n get_lc_unique_name(this.constructor),\n ];\n }\n /**\n * A map of secrets, which will be omitted from serialization.\n * Keys are paths to the secret in constructor args, e.g. \"foo.bar.baz\".\n * Values are the secret ids, which will be used when deserializing.\n */\n get lc_secrets() {\n return undefined;\n }\n /**\n * A map of additional attributes to merge with constructor args.\n * Keys are the attribute names, e.g. \"foo\".\n * Values are the attribute values, which will be serialized.\n * These attributes need to be accepted by the constructor as arguments.\n */\n get lc_attributes() {\n return undefined;\n }\n /**\n * A map of aliases for constructor args.\n * Keys are the attribute names, e.g. \"foo\".\n * Values are the alias that will replace the key in serialization.\n * This is used to eg. make argument names match Python.\n */\n get lc_aliases() {\n return undefined;\n }\n /**\n * A manual list of keys that should be serialized.\n * If not overridden, all fields passed into the constructor will be serialized.\n */\n get lc_serializable_keys() {\n return undefined;\n }\n constructor(kwargs, ..._args) {\n Object.defineProperty(this, \"lc_serializable\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: false\n });\n Object.defineProperty(this, \"lc_kwargs\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: void 0\n });\n if (this.lc_serializable_keys !== undefined) {\n this.lc_kwargs = Object.fromEntries(Object.entries(kwargs || {}).filter(([key]) => this.lc_serializable_keys?.includes(key)));\n }\n else {\n this.lc_kwargs = kwargs ?? {};\n }\n }\n toJSON() {\n if (!this.lc_serializable) {\n return this.toJSONNotImplemented();\n }\n if (\n // eslint-disable-next-line no-instanceof/no-instanceof\n this.lc_kwargs instanceof Serializable ||\n typeof this.lc_kwargs !== \"object\" ||\n Array.isArray(this.lc_kwargs)) {\n // We do not support serialization of classes with arg not a POJO\n // I'm aware the check above isn't as strict as it could be\n return this.toJSONNotImplemented();\n }\n const aliases = {};\n const secrets = {};\n const kwargs = Object.keys(this.lc_kwargs).reduce((acc, key) => {\n acc[key] = key in this ? this[key] : this.lc_kwargs[key];\n return acc;\n }, {});\n // get secrets, attributes and aliases from all superclasses\n for (\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n let current = Object.getPrototypeOf(this); current; current = Object.getPrototypeOf(current)) {\n Object.assign(aliases, Reflect.get(current, \"lc_aliases\", this));\n Object.assign(secrets, Reflect.get(current, \"lc_secrets\", this));\n Object.assign(kwargs, Reflect.get(current, \"lc_attributes\", this));\n }\n // include all secrets used, even if not in kwargs,\n // will be replaced with sentinel value in replaceSecrets\n Object.keys(secrets).forEach((keyPath) => {\n // eslint-disable-next-line @typescript-eslint/no-this-alias, @typescript-eslint/no-explicit-any\n let read = this;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let write = kwargs;\n const [last, ...partsReverse] = keyPath.split(\".\").reverse();\n for (const key of partsReverse.reverse()) {\n if (!(key in read) || read[key] === undefined)\n return;\n if (!(key in write) || write[key] === undefined) {\n if (typeof read[key] === \"object\" && read[key] != null) {\n write[key] = {};\n }\n else if (Array.isArray(read[key])) {\n write[key] = [];\n }\n }\n read = read[key];\n write = write[key];\n }\n if (last in read && read[last] !== undefined) {\n write[last] = write[last] || read[last];\n }\n });\n return {\n lc: 1,\n type: \"constructor\",\n id: this.lc_id,\n kwargs: mapKeys(Object.keys(secrets).length ? replaceSecrets(kwargs, secrets) : kwargs, keyToJson, aliases),\n };\n }\n toJSONNotImplemented() {\n return {\n lc: 1,\n type: \"not_implemented\",\n id: this.lc_id,\n };\n }\n}\n","export const isBrowser = () => typeof window !== \"undefined\" && typeof window.document !== \"undefined\";\nexport const isWebWorker = () => typeof globalThis === \"object\" &&\n globalThis.constructor &&\n globalThis.constructor.name === \"DedicatedWorkerGlobalScope\";\nexport const isJsDom = () => (typeof window !== \"undefined\" && window.name === \"nodejs\") ||\n (typeof navigator !== \"undefined\" && navigator.userAgent.includes(\"jsdom\"));\n// Supabase Edge Function provides a `Deno` global object\n// without `version` property\nexport const isDeno = () => typeof Deno !== \"undefined\";\n// Mark not-as-node if in Supabase Edge Function\nexport const isNode = () => typeof process !== \"undefined\" &&\n typeof process.versions !== \"undefined\" &&\n typeof process.versions.node !== \"undefined\" &&\n !isDeno();\nexport const getEnv = () => {\n let env;\n if (isBrowser()) {\n env = \"browser\";\n }\n else if (isNode()) {\n env = \"node\";\n }\n else if (isWebWorker()) {\n env = \"webworker\";\n }\n else if (isJsDom()) {\n env = \"jsdom\";\n }\n else if (isDeno()) {\n env = \"deno\";\n }\n else {\n env = \"other\";\n }\n return env;\n};\nlet runtimeEnvironment;\n/**\n * @deprecated Use getRuntimeEnvironmentSync instead\n */\nexport async function getRuntimeEnvironment() {\n return getRuntimeEnvironmentSync();\n}\nexport function getRuntimeEnvironmentSync() {\n if (runtimeEnvironment === undefined) {\n const env = getEnv();\n runtimeEnvironment = {\n library: \"langchain-js\",\n runtime: env,\n };\n }\n return runtimeEnvironment;\n}\nexport function getEnvironmentVariable(name) {\n // Certain Deno setups will throw an error if you try to access environment variables\n // https://github.com/langchain-ai/langchainjs/issues/1412\n try {\n if (typeof process !== \"undefined\") {\n // eslint-disable-next-line no-process-env\n return process.env?.[name];\n }\n else if (isDeno()) {\n return Deno?.env.get(name);\n }\n else {\n return undefined;\n }\n }\n catch (e) {\n return undefined;\n }\n}\n","import * as uuid from \"uuid\";\nimport { Serializable, get_lc_unique_name, } from \"../load/serializable.js\";\nimport { getEnvironmentVariable } from \"../utils/env.js\";\n/**\n * Abstract class that provides a set of optional methods that can be\n * overridden in derived classes to handle various events during the\n * execution of a LangChain application.\n */\nclass BaseCallbackHandlerMethodsClass {\n}\nexport function callbackHandlerPrefersStreaming(x) {\n return \"lc_prefer_streaming\" in x && x.lc_prefer_streaming;\n}\n/**\n * Abstract base class for creating callback handlers in the LangChain\n * framework. It provides a set of optional methods that can be overridden\n * in derived classes to handle various events during the execution of a\n * LangChain application.\n */\nexport class BaseCallbackHandler extends BaseCallbackHandlerMethodsClass {\n get lc_namespace() {\n return [\"langchain_core\", \"callbacks\", this.name];\n }\n get lc_secrets() {\n return undefined;\n }\n get lc_attributes() {\n return undefined;\n }\n get lc_aliases() {\n return undefined;\n }\n get lc_serializable_keys() {\n return undefined;\n }\n /**\n * The name of the serializable. Override to provide an alias or\n * to preserve the serialized module name in minified environments.\n *\n * Implemented as a static method to support loading logic.\n */\n static lc_name() {\n return this.name;\n }\n /**\n * The final serialized identifier for the module.\n */\n get lc_id() {\n return [\n ...this.lc_namespace,\n get_lc_unique_name(this.constructor),\n ];\n }\n constructor(input) {\n super();\n Object.defineProperty(this, \"lc_serializable\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: false\n });\n Object.defineProperty(this, \"lc_kwargs\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: void 0\n });\n Object.defineProperty(this, \"ignoreLLM\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: false\n });\n Object.defineProperty(this, \"ignoreChain\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: false\n });\n Object.defineProperty(this, \"ignoreAgent\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: false\n });\n Object.defineProperty(this, \"ignoreRetriever\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: false\n });\n Object.defineProperty(this, \"ignoreCustomEvent\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: false\n });\n Object.defineProperty(this, \"raiseError\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: false\n });\n Object.defineProperty(this, \"awaitHandlers\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: getEnvironmentVariable(\"LANGCHAIN_CALLBACKS_BACKGROUND\") === \"false\"\n });\n this.lc_kwargs = input || {};\n if (input) {\n this.ignoreLLM = input.ignoreLLM ?? this.ignoreLLM;\n this.ignoreChain = input.ignoreChain ?? this.ignoreChain;\n this.ignoreAgent = input.ignoreAgent ?? this.ignoreAgent;\n this.ignoreRetriever = input.ignoreRetriever ?? this.ignoreRetriever;\n this.ignoreCustomEvent =\n input.ignoreCustomEvent ?? this.ignoreCustomEvent;\n this.raiseError = input.raiseError ?? this.raiseError;\n this.awaitHandlers =\n this.raiseError || (input._awaitHandler ?? this.awaitHandlers);\n }\n }\n copy() {\n return new this.constructor(this);\n }\n toJSON() {\n return Serializable.prototype.toJSON.call(this);\n }\n toJSONNotImplemented() {\n return Serializable.prototype.toJSONNotImplemented.call(this);\n }\n static fromMethods(methods) {\n class Handler extends BaseCallbackHandler {\n constructor() {\n super();\n Object.defineProperty(this, \"name\", {\n enumerable: true,\n configurable: true,\n writable: true,\n value: uuid.v4()\n });\n Object.assign(this, methods);\n }\n }\n return new Handler();\n }\n}\nexport const isBaseCallbackHandler = (x) => {\n const callbackHandler = x;\n return (callbackHandler !== undefined &&\n typeof callbackHandler.copy === \"function\" &&\n typeof callbackHandler.name === \"string\" &&\n typeof callbackHandler.awaitHandlers === \"boolean\");\n};\n","// Type guards for safer type checking\n\nexport const isString = (value: unknown): value is string => {\n return typeof value === 'string'\n}\n\nexport const isObject = (value: unknown): value is Record<string, unknown> => {\n return value !== null && typeof value === 'object' && !Array.isArray(value)\n}\n","import { isString, isObject } from './typeGuards'\n\nconst REDACTED_IMAGE_PLACEHOLDER = '[base64 image redacted]'\n\n// ============================================\n// Base64 Detection Helpers\n// ============================================\n\nconst isBase64DataUrl = (str: string): boolean => {\n return /^data:([^;]+);base64,/.test(str)\n}\n\nconst isValidUrl = (str: string): boolean => {\n try {\n new URL(str)\n return true\n } catch {\n // Not an absolute URL, check if it's a relative URL or path\n return str.startsWith('/') || str.startsWith('./') || str.startsWith('../')\n }\n}\n\nconst isRawBase64 = (str: string): boolean => {\n // Skip if it's a valid URL or path\n if (isValidUrl(str)) {\n return false\n }\n\n // Check if it's a valid base64 string\n // Base64 images are typically at least a few hundred chars, but we'll be conservative\n return str.length > 20 && /^[A-Za-z0-9+/]+=*$/.test(str)\n}\n\nexport function redactBase64DataUrl(str: string): string\nexport function redactBase64DataUrl(str: unknown): unknown\nexport function redactBase64DataUrl(str: unknown): unknown {\n if (!isString(str)) return str\n\n // Check for data URL format\n if (isBase64DataUrl(str)) {\n return REDACTED_IMAGE_PLACEHOLDER\n }\n\n // Check for raw base64 (Vercel sends raw base64 for inline images)\n if (isRawBase64(str)) {\n return REDACTED_IMAGE_PLACEHOLDER\n }\n\n return str\n}\n\n// ============================================\n// Common Message Processing\n// ============================================\n\ntype ContentTransformer = (item: unknown) => unknown\n\nconst processMessages = (messages: unknown, transformContent: ContentTransformer): unknown => {\n if (!messages) return messages\n\n const processContent = (content: unknown): unknown => {\n if (typeof content === 'string') return content\n\n if (!content) return content\n\n if (Array.isArray(content)) {\n return content.map(transformContent)\n }\n\n // Handle single object content\n return transformContent(content)\n }\n\n const processMessage = (msg: unknown): unknown => {\n if (!isObject(msg) || !('content' in msg)) return msg\n return { ...msg, content: processContent(msg.content) }\n }\n\n // Handle both arrays and single messages\n if (Array.isArray(messages)) {\n return messages.map(processMessage)\n }\n\n return processMessage(messages)\n}\n\n// ============================================\n// Provider-Specific Image Sanitizers\n// ============================================\n\nconst sanitizeOpenAIImage = (item: unknown): unknown => {\n if (!isObject(item)) return item\n\n // Handle image_url format\n if (item.type === 'image_url' && 'image_url' in item && isObject(item.image_url) && 'url' in item.image_url) {\n return {\n ...item,\n image_url: {\n ...item.image_url,\n url: redactBase64DataUrl(item.image_url.url),\n },\n }\n }\n\n return item\n}\n\nconst sanitizeOpenAIResponseImage = (item: unknown): unknown => {\n if (!isObject(item)) return item\n\n // Handle input_image format\n if (item.type === 'input_image' && 'image_url' in item) {\n return {\n ...item,\n image_url: redactBase64DataUrl(item.image_url),\n }\n }\n\n return item\n}\n\nconst sanitizeAnthropicImage = (item: unknown): unknown => {\n if (!isObject(item)) return item\n\n // Handle Anthropic's image format\n if (\n item.type === 'image' &&\n 'source' in item &&\n isObject(item.source) &&\n item.source.type === 'base64' &&\n 'data' in item.source\n ) {\n return {\n ...item,\n source: {\n ...item.source,\n data: REDACTED_IMAGE_PLACEHOLDER,\n },\n }\n }\n\n return item\n}\n\nconst sanitizeGeminiPart = (part: unknown): unknown => {\n if (!isObject(part)) return part\n\n // Handle Gemini's inline data format\n if ('inlineData' in part && isObject(part.inlineData) && 'data' in part.inlineData) {\n return {\n ...part,\n inlineData: {\n ...part.inlineData,\n data: REDACTED_IMAGE_PLACEHOLDER,\n },\n }\n }\n\n return part\n}\n\nconst processGeminiItem = (item: unknown): unknown => {\n if (!isObject(item)) return item\n\n // If it has parts, process them\n if ('parts' in item && item.parts) {\n const parts = Array.isArray(item.parts) ? item.parts.map(sanitizeGeminiPart) : sanitizeGeminiPart(item.parts)\n\n return { ...item, parts }\n }\n\n return item\n}\n\nconst sanitizeLangChainImage = (item: unknown): unknown => {\n if (!isObject(item)) return item\n\n // OpenAI style\n if (item.type === 'image_url' && 'image_url' in item && isObject(item.image_url) && 'url' in item.image_url) {\n return {\n ...item,\n image_url: {\n ...item.image_url,\n url: redactBase64DataUrl(item.image_url.url),\n },\n }\n }\n\n // Direct image with data field\n if (item.type === 'image' && 'data' in item) {\n return { ...item, data: redactBase64DataUrl(item.data) }\n }\n\n // Anthropic style\n if (item.type === 'image' && 'source' in item && isObject(item.source) && 'data' in item.source) {\n return {\n ...item,\n source: {\n ...item.source,\n data: redactBase64DataUrl(item.source.data),\n },\n }\n }\n\n // Google style\n if (item.type === 'media' && 'data' in item) {\n return { ...item, data: redactBase64DataUrl(item.data) }\n }\n\n return item\n}\n\n// Export individual sanitizers for tree-shaking\nexport const sanitizeOpenAI = (data: unknown): unknown => {\n return processMessages(data, sanitizeOpenAIImage)\n}\n\nexport const sanitizeOpenAIResponse = (data: unknown): unknown => {\n return processMessages(data, sanitizeOpenAIResponseImage)\n}\n\nexport const sanitizeAnthropic = (data: unknown): unknown => {\n return processMessages(data, sanitizeAnthropicImage)\n}\n\nexport const sanitizeGemini = (data: unknown): unknown => {\n // Gemini has a different structure with 'parts' directly on items instead of 'content'\n // So we need custom processing instead of using processMessages\n if (!data) return data\n\n if (Array.isArray(data)) {\n return data.map(processGeminiItem)\n }\n\n return processGeminiItem(data)\n}\n\nexport const sanitizeLangChain = (data: unknown): unknown => {\n return processMessages(data, sanitizeLangChainImage)\n}\n","import { PostHog } from 'posthog-node'\nimport { withPrivacyMode, getModelParams } from '../utils'\nimport { BaseCallbackHandler } from '@langchain/core/callbacks/base'\nimport type { Serialized } from '@langchain/core/load/serializable'\nimport type { ChainValues } from '@langchain/core/utils/types'\nimport type { LLMResult } from '@langchain/core/outputs'\nimport type { AgentAction, AgentFinish } from '@langchain/core/agents'\nimport type { DocumentInterface } from '@langchain/core/documents'\nimport { ToolCall } from '@langchain/core/messages/tool'\nimport { BaseMessage } from '@langchain/core/messages'\nimport { sanitizeLangChain } from '../sanitization'\n\ninterface SpanMetadata {\n /** Name of the trace/span (e.g. chain name) */\n name: string\n /** Timestamp (in ms) when the run started */\n startTime: number\n /** Timestamp (in ms) when the run ended (if already finished) */\n endTime?: number\n /** The input state */\n input?: any\n}\n\ninterface GenerationMetadata extends SpanMetadata {\n /** Provider used (e.g. openai, anthropic) */\n provider?: string\n /** Model name used in the generation */\n model?: string\n /** The model parameters (temperature, max_tokens, etc.) */\n modelParams?: Record<string, any>\n /** The base URL—for example, the API base used */\n baseUrl?: string\n /** The tools used in the generation */\n tools?: Record<string, any>\n}\n\n/** A run may either be a Span or a Generation */\ntype RunMetadata = SpanMetadata | GenerationMetadata\n\n/** Storage for run metadata */\ntype RunMetadataStorage = { [runId: string]: RunMetadata }\n\nexport class LangChainCallbackHandler extends BaseCallbackHandler {\n public name = 'PosthogCallbackHandler'\n private client: PostHog\n private distinctId?: string | number\n private traceId?: string | number\n private properties: Record<string, any>\n private privacyMode: boolean\n private groups: Record<string, any>\n private debug: boolean\n\n private runs: RunMetadataStorage = {}\n private parentTree: { [runId: string]: string } = {}\n\n constructor(options: {\n client: PostHog\n distinctId?: string | number\n traceId?: string | number\n properties?: Record<string, any>\n privacyMode?: boolean\n groups?: Record<string, any>\n debug?: boolean\n }) {\n if (!options.client) {\n throw new Error('PostHog client is required')\n }\n super()\n this.client = options.client\n this.distinctId = options.distinctId\n this.traceId = options.traceId\n this.properties = options.properties || {}\n this.privacyMode = options.privacyMode || false\n this.groups = options.groups || {}\n this.debug = options.debug || false\n }\n\n // ===== CALLBACK METHODS =====\n\n public handleChainStart(\n chain: Serialized,\n inputs: ChainValues,\n runId: string,\n parentRunId?: string,\n tags?: string[],\n metadata?: Record<string, unknown>,\n runType?: string,\n runName?: string\n ): void {\n this._logDebugEvent('on_chain_start', runId, parentRunId, { inputs, tags })\n this._setParentOfRun(runId, parentRunId)\n this._setTraceOrSpanMetadata(chain, inputs, runId, parentRunId, metadata, tags, runName)\n }\n\n public handleChainEnd(\n outputs: ChainValues,\n runId: string,\n parentRunId?: string,\n tags?: string[],\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n kwargs?: { inputs?: Record<string, unknown> }\n ): void {\n this._logDebugEvent('on_chain_end', runId, parentRunId, { outputs, tags })\n this._popRunAndCaptureTraceOrSpan(runId, parentRunId, outputs)\n }\n\n public handleChainError(\n error: Error,\n runId: string,\n parentRunId?: string,\n tags?: string[],\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n kwargs?: { inputs?: Record<string, unknown> }\n ): void {\n this._logDebugEvent('on_chain_error', runId, parentRunId, { error, tags })\n this._popRunAndCaptureTraceOrSpan(runId, parentRunId, error)\n }\n\n public handleChatModelStart(\n serialized: Serialized,\n messages: BaseMessage[][],\n runId: string,\n parentRunId?: string,\n extraParams?: Record<string, unknown>,\n tags?: string[],\n metadata?: Record<string, unknown>,\n runName?: string\n ): void {\n this._logDebugEvent('on_chat_model_start', runId, parentRunId, { messages, tags })\n this._setParentOfRun(runId, parentRunId)\n // Flatten the two-dimensional messages and convert each message to a plain object\n const input = messages.flat().map((m) => this._convertMessageToDict(m))\n this._setLLMMetadata(serialized, runId, input, metadata, extraParams, runName)\n }\n\n public handleLLMStart(\n serialized: Serialized,\n prompts: string[],\n runId: string,\n parentRunId?: string,\n extraParams?: Record<string, unknown>,\n tags?: string[],\n metadata?: Record<string, unknown>,\n runName?: string\n ): void {\n this._logDebugEvent('on_llm_start', runId, parentRunId, { prompts, tags })\n this._setParentOfRun(runId, parentRunId)\n this._setLLMMetadata(serialized, runId, prompts, metadata, extraParams, runName)\n }\n\n public handleLLMEnd(\n output: LLMResult,\n runId: string,\n parentRunId?: string,\n tags?: string[],\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n extraParams?: Record<string, unknown>\n ): void {\n this._logDebugEvent('on_llm_end', runId, parentRunId, { output, tags })\n this._popRunAndCaptureGeneration(runId, parentRunId, output)\n }\n\n public handleLLMError(\n err: Error,\n runId: string,\n parentRunId?: string,\n tags?: string[],\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n extraParams?: Record<string, unknown>\n ): void {\n this._logDebugEvent('on_llm_error', runId, parentRunId, { err, tags })\n this._popRunAndCaptureGeneration(runId, parentRunId, err)\n }\n\n public handleToolStart(\n tool: Serialized,\n input: string,\n runId: string,\n parentRunId?: string,\n tags?: string[],\n metadata?: Record<string, unknown>,\n runName?: string\n ): void {\n this._logDebugEvent('on_tool_start', runId, parentRunId, { input, tags })\n this._setParentOfRun(runId, parentRunId)\n this._setTraceOrSpanMetadata(tool, input, runId, parentRunId, metadata, tags, runName)\n }\n\n public handleToolEnd(output: any, runId: string, parentRunId?: string, tags?: string[]): void {\n this._logDebugEvent('on_tool_end', runId, parentRunId, { output, tags })\n this._popRunAndCaptureTraceOrSpan(runId, parentRunId, output)\n }\n\n public handleToolError(err: Error, runId: string, parentRunId?: string, tags?: string[]): void {\n this._logDebugEvent('on_tool_error', runId, parentRunId, { err, tags })\n this._popRunAndCaptureTraceOrSpan(runId, parentRunId, err)\n }\n\n public handleRetrieverStart(\n retriever: Serialized,\n query: string,\n runId: string,\n parentRunId?: string,\n tags?: string[],\n metadata?: Record<string, unknown>,\n name?: string\n ): void {\n this._logDebugEvent('on_retriever_start', runId, parentRunId, { query, tags })\n this._setParentOfRun(runId, parentRunId)\n this._setTraceOrSpanMetadata(retriever, query, runId, parentRunId, metadata, tags, name)\n }\n\n public handleRetrieverEnd(\n documents: DocumentInterface[],\n runId: string,\n parentRunId?: string,\n tags?: string[]\n ): void {\n this._logDebugEvent('on_retriever_end', runId, parentRunId, { documents, tags })\n this._popRunAndCaptureTraceOrSpan(runId, parentRunId, documents)\n }\n\n public handleRetrieverError(err: Error, runId: string, parentRunId?: string, tags?: string[]): void {\n this._logDebugEvent('on_retriever_error', runId, parentRunId, { err, tags })\n this._popRunAndCaptureTraceOrSpan(runId, parentRunId, err)\n }\n\n public handleAgentAction(action: AgentAction, runId: string, parentRunId?: string, tags?: string[]): void {\n this._logDebugEvent('on_agent_action', runId, parentRunId, { action, tags })\n this._setParentOfRun(runId, parentRunId)\n this._setTraceOrSpanMetadata(null, action, runId, parentRunId)\n }\n\n public handleAgentEnd(action: AgentFinish, runId: string, parentRunId?: string, tags?: string[]): void {\n this._logDebugEvent('on_agent_finish', runId, parentRunId, { action, tags })\n this._popRunAndCaptureTraceOrSpan(runId, parentRunId, action)\n }\n\n // ===== PRIVATE HELPERS =====\n\n private _setParentOfRun(runId: string, parentRunId?: string): void {\n if (parentRunId) {\n this.parentTree[runId] = parentRunId\n }\n }\n\n private _popParentOfRun(runId: string): void {\n delete this.parentTree[runId]\n }\n\n private _findRootRun(runId: string): string {\n let id = runId\n while (this.parentTree[id]) {\n id = this.parentTree[id]\n }\n return id\n }\n\n private _setTraceOrSpanMetadata(\n serialized: any,\n input: any,\n runId: string,\n parentRunId?: string,\n ...args: any[]\n ): void {\n // Use default names if not provided: if this is a top-level run, we mark it as a trace, otherwise as a span.\n const defaultName = parentRunId ? 'span' : 'trace'\n const runName = this._getLangchainRunName(serialized, ...args) || defaultName\n this.runs[runId] = {\n name: runName,\n input,\n startTime: Date.now(),\n } as SpanMetadata\n }\n\n private _setLLMMetadata(\n serialized: Serialized | null,\n runId: string,\n messages: any,\n metadata?: any,\n extraParams?: any,\n runName?: string\n ): void {\n const runNameFound = this._getLangchainRunName(serialized, { extraParams, runName }) || 'generation'\n const generation: GenerationMetadata = {\n name: runNameFound,\n input: sanitizeLangChain(messages),\n startTime: Date.now(),\n }\n if (extraParams) {\n generation.modelParams = getModelParams(extraParams.invocation_params)\n\n if (extraParams.invocation_params && extraParams.invocation_params.tools) {\n generation.tools = extraParams.invocation_params.tools\n }\n }\n if (metadata) {\n if (metadata.ls_model_name) {\n generation.model = metadata.ls_model_name\n }\n if (metadata.ls_provider) {\n generation.provider = metadata.ls_provider\n }\n }\n if (serialized && 'kwargs' in serialized && serialized.kwargs.openai_api_base) {\n generation.baseUrl = serialized.kwargs.openai_api_base\n }\n this.runs[runId] = generation\n }\n\n private _popRunMetadata(runId: string): RunMetadata | undefined {\n c