UNPKG

@posthog/ai

Version:
1 lines 130 kB
{"version":3,"file":"index.cjs","sources":["../../src/sanitization/base64_recognizer.ts","../../src/sanitization/media_type_context.ts","../../src/sanitization/binary_content_redactor.ts","../../src/sanitization.ts","../../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@1.1.29_@opentelemetry+api@1.9.0_@opentelemetry+sdk-trace-base@2.2.0_@op_b5576b05b1f0c657108074bbb9246c56/node_modules/@langchain/core/dist/load/map_keys.js","../../../../node_modules/.pnpm/@langchain+core@1.1.29_@opentelemetry+api@1.9.0_@opentelemetry+sdk-trace-base@2.2.0_@op_b5576b05b1f0c657108074bbb9246c56/node_modules/@langchain/core/dist/load/validation.js","../../../../node_modules/.pnpm/@langchain+core@1.1.29_@opentelemetry+api@1.9.0_@opentelemetry+sdk-trace-base@2.2.0_@op_b5576b05b1f0c657108074bbb9246c56/node_modules/@langchain/core/dist/load/serializable.js","../../../../node_modules/.pnpm/@langchain+core@1.1.29_@opentelemetry+api@1.9.0_@opentelemetry+sdk-trace-base@2.2.0_@op_b5576b05b1f0c657108074bbb9246c56/node_modules/@langchain/core/dist/utils/env.js","../../../../node_modules/.pnpm/@langchain+core@1.1.29_@opentelemetry+api@1.9.0_@opentelemetry+sdk-trace-base@2.2.0_@op_b5576b05b1f0c657108074bbb9246c56/node_modules/@langchain/core/dist/callbacks/base.js","../../src/serializeError.ts","../../src/langchain/callbacks.ts"],"sourcesContent":["const DATA_URL_PREFIX_RE = /^data:([^;,\\s]+)(?:;[^;,\\s]+)*;base64,/i\nconst BASE64_ALPHABET_RE = /^[A-Za-z0-9+/_=-]+$/\n\nexport type Base64Recognition = { kind: 'data-url'; mediaType: string } | { kind: 'raw' } | { kind: 'none' }\n\nexport class Base64Recognizer {\n recognize(value: string, minLength: number): Base64Recognition {\n const dataUrl = DATA_URL_PREFIX_RE.exec(value)\n if (dataUrl) return { kind: 'data-url', mediaType: dataUrl[1] }\n\n if (value.length < minLength) return { kind: 'none' }\n\n const confidencePrefix = value.slice(0, minLength)\n if (BASE64_ALPHABET_RE.test(confidencePrefix)) {\n return { kind: 'raw' }\n } else {\n return { kind: 'none' }\n }\n }\n}\n","const MIME_HINT_KEYS = ['mediaType', 'media_type', 'mimeType', 'mime_type'] as const\n\nconst STRONG_CONTEXT_KEYS = new Set([\n 'data',\n 'file_data',\n 'fileData',\n 'image_url',\n 'imageUrl',\n 'video_url',\n 'videoUrl',\n 'audio',\n 'audio_data',\n 'audioData',\n 'inline_data',\n 'inlineData',\n 'source',\n 'result',\n])\n\nconst STRONG_CONTEXT_TYPES = new Set([\n 'image',\n 'image_url',\n 'input_image',\n 'audio',\n 'input_audio',\n 'video',\n 'video_url',\n 'file',\n 'input_file',\n 'document',\n 'media',\n 'file-data',\n])\n\nconst FILE_FAMILY_TYPES = new Set(['file', 'input_file', 'document', 'media', 'file-data'])\n\nconst KNOWN_AUDIO_FORMATS = new Set(['wav', 'mp3', 'ogg', 'flac', 'm4a', 'aac', 'webm'])\n\nexport class MediaTypeContext {\n static readonly EMPTY = new MediaTypeContext(undefined, undefined)\n\n constructor(\n private readonly parent: Record<string, unknown> | undefined,\n private readonly key: string | undefined\n ) {}\n\n inferMediaType(): string | undefined {\n return (\n this.inferFromSiblingMime() ?? this.inferFromSiblingFormat() ?? this.inferFromParentType() ?? this.inferFromKey()\n )\n }\n\n inferFromSiblingMime(): string | undefined {\n if (!this.parent) return undefined\n for (const hint of MIME_HINT_KEYS) {\n const v = this.parent[hint]\n if (typeof v === 'string') return v\n }\n return undefined\n }\n\n inferFromSiblingFormat(): string | undefined {\n if (!this.parent) return undefined\n const fmt = this.parent.format\n if (typeof fmt === 'string' && KNOWN_AUDIO_FORMATS.has(fmt.toLowerCase())) {\n return `audio/${fmt.toLowerCase()}`\n }\n return undefined\n }\n\n inferFromParentType(): string | undefined {\n if (!this.parent) return undefined\n const t = this.parent.type\n if (typeof t !== 'string') return undefined\n if (t === 'image' || t === 'image_url' || t === 'input_image') return 'image'\n if (t === 'audio' || t === 'input_audio') return 'audio'\n if (t === 'video' || t === 'video_url') return 'video'\n if (FILE_FAMILY_TYPES.has(t)) return 'application/octet-stream'\n return undefined\n }\n\n inferFromKey(): string | undefined {\n if (!this.key) return undefined\n const key = this.key.toLowerCase()\n if (key.includes('audio')) return 'audio'\n if (key.includes('video')) return 'video'\n if (key.includes('image')) return 'image'\n if (key.includes('file') || key.includes('document')) return 'application/octet-stream'\n return undefined\n }\n\n signalsBinary(): boolean {\n if (this.parent) {\n for (const hint of MIME_HINT_KEYS) {\n if (typeof this.parent[hint] === 'string') return true\n }\n const fmt = this.parent.format\n if (typeof fmt === 'string' && KNOWN_AUDIO_FORMATS.has(fmt.toLowerCase())) return true\n const t = this.parent.type\n if (typeof t === 'string' && STRONG_CONTEXT_TYPES.has(t)) return true\n }\n if (this.key && STRONG_CONTEXT_KEYS.has(this.key)) return true\n return false\n }\n}\n","import { Base64Recognizer } from './base64_recognizer'\nimport { MediaTypeContext } from './media_type_context'\n\nconst STRONG_CONTEXT_MIN_LENGTH = 64\nconst WEAK_CONTEXT_MIN_LENGTH = 1024\n\nexport class BinaryContentRedactor {\n private visited: WeakSet<object> = new WeakSet()\n\n constructor(private readonly recognizer: Base64Recognizer = new Base64Recognizer()) {}\n\n redact<T>(value: T): T\n redact(value: unknown): unknown {\n if (this.isMultimodalEnabled()) return value\n this.visited = new WeakSet()\n return this.walk(value, MediaTypeContext.EMPTY)\n }\n\n private walk(value: unknown, ctx: MediaTypeContext): unknown {\n if (value === null || value === undefined) return value\n if (typeof value === 'string') return this.redactString(value, ctx)\n if (typeof value !== 'object') return value\n\n // Buffer extends Uint8Array, so this branch catches both.\n if (typeof Uint8Array !== 'undefined' && value instanceof Uint8Array) {\n return this.placeholderFor(ctx.inferMediaType())\n }\n\n if (this.visited.has(value)) return null\n this.visited.add(value)\n\n if (Array.isArray(value)) {\n return value.map((item) => this.walk(item, ctx))\n }\n\n const obj = value as Record<string, unknown>\n const out: Record<string, unknown> = {}\n for (const k of Object.keys(obj)) {\n out[k] = this.walk(obj[k], new MediaTypeContext(obj, k))\n }\n return out\n }\n\n private redactString(value: string, ctx: MediaTypeContext): string {\n const minLength = ctx.signalsBinary() ? STRONG_CONTEXT_MIN_LENGTH : WEAK_CONTEXT_MIN_LENGTH\n const recognition = this.recognizer.recognize(value, minLength)\n switch (recognition.kind) {\n case 'data-url':\n return this.placeholderFor(recognition.mediaType)\n case 'raw':\n return this.placeholderFor(ctx.inferMediaType())\n case 'none':\n return value\n }\n }\n\n private placeholderFor(mediaType?: string): string {\n if (!mediaType) return '[base64 redacted]'\n if (mediaType === 'application/octet-stream') return '[base64 file redacted]'\n return `[base64 ${mediaType} redacted]`\n }\n\n private isMultimodalEnabled(): boolean {\n const val = process.env._INTERNAL_LLMA_MULTIMODAL || ''\n return val.toLowerCase() === 'true' || val === '1' || val.toLowerCase() === 'yes'\n }\n}\n","import { BinaryContentRedactor } from './sanitization/binary_content_redactor'\n\nconst redactor = new BinaryContentRedactor()\n\nexport function redactBase64DataUrl(str: string): string\nexport function redactBase64DataUrl(str: unknown): unknown\nexport function redactBase64DataUrl(str: unknown): unknown {\n return redactor.redact(str)\n}\n\nexport const sanitizeOpenAI = (data: unknown): unknown => redactor.redact(data)\nexport const sanitizeOpenAIResponse = (data: unknown): unknown => redactor.redact(data)\nexport const sanitizeAnthropic = (data: unknown): unknown => redactor.redact(data)\nexport const sanitizeGemini = (data: unknown): unknown => redactor.redact(data)\nexport const sanitizeLangChain = (data: unknown): unknown => redactor.redact(data)\n","import { PostHog } from 'posthog-node'\nimport OpenAIOrignal from 'openai'\nimport AnthropicOriginal from '@anthropic-ai/sdk'\nimport type { ChatCompletionTool } from 'openai/resources/chat/completions'\nimport type { ResponseCreateParamsWithTools } from 'openai/lib/ResponsesParser'\nimport type { Tool as GeminiTool } from '@google/genai'\nimport type {\n FormattedMessage,\n FormattedContent,\n FormattedAudioContent,\n FormattedImageContent,\n FormattedDocumentContent,\n} from './types'\nimport { v4 as uuidv4 } from 'uuid'\nimport { isString } from './typeGuards'\nimport { redactBase64DataUrl } from './sanitization'\n\ntype ChatCompletionCreateParamsBase = OpenAIOrignal.Chat.Completions.ChatCompletionCreateParams\ntype MessageCreateParams = AnthropicOriginal.Messages.MessageCreateParams\ntype ResponseCreateParams = OpenAIOrignal.Responses.ResponseCreateParams\ntype EmbeddingCreateParams = OpenAIOrignal.EmbeddingCreateParams\ntype TranscriptionCreateParams = OpenAIOrignal.Audio.Transcriptions.TranscriptionCreateParams\ntype AnthropicTool = AnthropicOriginal.Tool\n\nconst TOKEN_PROPERTY_KEYS = new Set([\n '$ai_input_tokens',\n '$ai_output_tokens',\n '$ai_cache_read_input_tokens',\n '$ai_cache_creation_input_tokens',\n '$ai_total_tokens',\n '$ai_reasoning_tokens',\n])\n\nexport function getTokensSource(posthogProperties?: Record<string, unknown>): string {\n if (posthogProperties && Object.keys(posthogProperties).some((key) => TOKEN_PROPERTY_KEYS.has(key))) {\n return 'passthrough'\n }\n return 'sdk'\n}\n\n// limit large outputs by truncating to 200kb (approx 200k bytes)\nexport const MAX_OUTPUT_SIZE = 200000\nconst STRING_FORMAT = 'utf8'\n\n// Reused across calls to avoid per-invocation allocation; truncate() runs\n// hundreds of times for prompts with many parts.\nconst sharedTextEncoder = new TextEncoder()\nconst sharedTextDecoder = new TextDecoder(STRING_FORMAT, { fatal: false })\n\nexport const utf8ByteLength = (str: string): number => sharedTextEncoder.encode(str).byteLength\n\n/**\n * Safely converts content to a string, preserving structure for objects/arrays.\n * - If content is already a string, returns it as-is\n * - If content is an object or array, stringifies it with JSON.stringify to preserve structure\n * - Otherwise, converts to string with String()\n *\n * This prevents the \"[object Object]\" bug when objects are naively converted to strings.\n *\n * @param content - The content to convert to a string\n * @returns A string representation that preserves structure for complex types\n */\nexport function toContentString(content: unknown): string {\n if (typeof content === 'string') {\n return content\n }\n if (content !== undefined && content !== null && typeof content === 'object') {\n try {\n return JSON.stringify(content)\n } catch {\n // Fallback for circular refs, BigInt, or objects with throwing toJSON\n return String(content)\n }\n }\n return String(content)\n}\n\nexport interface MonitoringEventPropertiesWithDefaults {\n distinctId?: string\n traceId: string\n properties?: Record<string, any>\n privacyMode: boolean\n groups?: Record<string, any>\n modelOverride?: string\n providerOverride?: string\n costOverride?: CostOverride\n captureImmediate?: boolean\n}\n\nexport type MonitoringEventProperties = Partial<MonitoringEventPropertiesWithDefaults>\n\nexport type MonitoringParams = {\n [K in keyof MonitoringEventProperties as `posthog${Capitalize<string & K>}`]: MonitoringEventProperties[K]\n}\n\nexport interface CostOverride {\n inputCost: number\n outputCost: number\n}\n\nexport const getModelParams = (\n params:\n | ((\n | ChatCompletionCreateParamsBase\n | MessageCreateParams\n | ResponseCreateParams\n | ResponseCreateParamsWithTools\n | EmbeddingCreateParams\n | TranscriptionCreateParams\n ) &\n MonitoringParams)\n | 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 'language',\n 'response_format',\n 'timestamp_granularities',\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\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 // Handle audio output (gpt-4o-audio-preview)\n if (choice.message.audio) {\n content.push({\n type: 'audio',\n ...choice.message.audio,\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 buildInlineDataBlock = (\n mimeType: string,\n data: string\n): FormattedAudioContent | FormattedImageContent | FormattedDocumentContent => {\n if (mimeType.startsWith('audio/')) {\n return { type: 'audio', mime_type: mimeType, data }\n }\n if (mimeType.startsWith('image/')) {\n return { type: 'image', inline_data: { mime_type: mimeType, data } }\n }\n return { type: 'document', inline_data: { mime_type: mimeType, data } }\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 } else if (part.inlineData) {\n // Handle inline data (images, audio, documents)\n const mimeType = part.inlineData.mimeType || part.inlineData.mime_type || 'application/octet-stream'\n let data = part.inlineData.data\n\n // Handle binary data (Uint8Array/Buffer -> base64)\n if (data instanceof Uint8Array) {\n if (typeof Buffer !== 'undefined') {\n data = Buffer.from(data).toString('base64')\n } else {\n let binary = ''\n for (let i = 0; i < data.length; i++) {\n binary += String.fromCharCode(data[i])\n }\n data = btoa(binary)\n }\n }\n\n // Sanitize base64 data for images and other large inline data\n data = redactBase64DataUrl(data)\n\n content.push(buildInlineDataBlock(mimeType, data))\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\nfunction toSafeString(input: unknown): string {\n if (input === undefined || input === null) {\n return ''\n }\n if (typeof input === 'string') {\n return input\n }\n try {\n return JSON.stringify(input)\n } catch {\n console.warn('Failed to stringify input', input)\n return ''\n }\n}\n\nexport const truncate = (input: unknown): string => {\n const str = toSafeString(input)\n if (str === '') {\n return ''\n }\n\n // Check if we need to truncate and ensure STRING_FORMAT is respected\n const buffer = sharedTextEncoder.encode(str)\n if (buffer.length <= MAX_OUTPUT_SIZE) {\n // Ensure STRING_FORMAT is respected\n return sharedTextDecoder.decode(buffer)\n }\n\n // Truncate the buffer and ensure a valid string is returned.\n // fatal: false means we get U+FFFD at the end if truncation broke the encoding.\n const truncatedBuffer = buffer.slice(0, MAX_OUTPUT_SIZE)\n let truncatedStr = sharedTextDecoder.decode(truncatedBuffer)\n if (truncatedStr.endsWith('\\uFFFD')) {\n truncatedStr = truncatedStr.slice(0, -1)\n }\n return `${truncatedStr}... [truncated]`\n}\n\n/**\n * Calculate web search count from raw API response.\n *\n * Uses a two-tier detection strategy:\n * Priority 1 (Exact Count): Count actual web search calls when available\n * Priority 2 (Binary Detection): Return 1 if web search indicators are present, 0 otherwise\n *\n * @param result - Raw API response from any provider (OpenAI, Perplexity, OpenRouter, Gemini, etc.)\n * @returns Number of web searches performed (exact count or binary 1/0)\n */\nexport function calculateWebSearchCount(result: unknown): number {\n if (!result || typeof result !== 'object') {\n return 0\n }\n\n // Priority 1: Exact Count\n // Check for OpenAI Responses API web_search_call items\n if ('output' in result && Array.isArray(result.output)) {\n let count = 0\n\n for (const item of result.output) {\n if (typeof item === 'object' && item !== null && 'type' in item && item.type === 'web_search_call') {\n count++\n }\n }\n\n if (count > 0) {\n return count\n }\n }\n\n // Priority 2: Binary Detection (1 or 0)\n\n // Check for citations at root level (Perplexity)\n if ('citations' in result && Array.isArray(result.citations) && result.citations.length > 0) {\n return 1\n }\n\n // Check for search_results at root level (Perplexity via OpenRouter)\n if ('search_results' in result && Array.isArray(result.search_results) && result.search_results.length > 0) {\n return 1\n }\n\n // Check for usage.search_context_size (Perplexity via OpenRouter)\n if ('usage' in result && typeof result.usage === 'object' && result.usage !== null) {\n if ('search_context_size' in result.usage && result.usage.search_context_size) {\n return 1\n }\n }\n\n // Check for annotations with url_citation in choices[].message or choices[].delta (OpenAI/Perplexity)\n if ('choices' in result && Array.isArray(result.choices)) {\n for (const choice of result.choices) {\n if (typeof choice === 'object' && choice !== null) {\n // Check both message (non-streaming) and delta (streaming) for annotations\n const content = ('message' in choice ? choice.message : null) || ('delta' in choice ? choice.delta : null)\n\n if (typeof content === 'object' && content !== null && 'annotations' in content) {\n const annotations = content.annotations\n\n if (Array.isArray(annotations)) {\n const hasUrlCitation = annotations.some((ann: unknown) => {\n return typeof ann === 'object' && ann !== null && 'type' in ann && ann.type === 'url_citation'\n })\n\n if (hasUrlCitation) {\n return 1\n }\n }\n }\n }\n }\n }\n\n // Check for annotations in output[].content[] (OpenAI Responses API)\n if ('output' in result && Array.isArray(result.output)) {\n for (const item of result.output) {\n if (typeof item === 'object' && item !== null && 'content' in item) {\n const content = item.content\n\n if (Array.isArray(content)) {\n for (const contentItem of content) {\n if (typeof contentItem === 'object' && contentItem !== null && 'annotations' in contentItem) {\n const annotations = contentItem.annotations\n\n if (Array.isArray(annotations)) {\n const hasUrlCitation = annotations.some((ann: unknown) => {\n return typeof ann === 'object' && ann !== null && 'type' in ann && ann.type === 'url_citation'\n })\n\n if (hasUrlCitation) {\n return 1\n }\n }\n }\n }\n }\n }\n }\n }\n\n // Check for grounding_metadata (Gemini)\n if ('candidates' in result && Array.isArray(result.candidates)) {\n for (const candidate of result.candidates) {\n if (\n typeof candidate === 'object' &&\n candidate !== null &&\n 'grounding_metadata' in candidate &&\n candidate.grounding_metadata\n ) {\n return 1\n }\n }\n }\n\n return 0\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 if (params.tools) {\n return params.tools\n }\n\n return null\n }\n\n return null\n}\n\nexport enum AIEvent {\n Generation = '$ai_generation',\n Embedding = '$ai_embedding',\n}\n\nexport function 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 // Sanitize lone surrogates by round-tripping through UTF-8\n return new TextDecoder().decode(new TextEncoder().encode(jsonSafe))\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\nconst POSTHOG_PARAMS_MAP: Record<keyof MonitoringParams, string> = {\n posthogDistinctId: 'distinctId',\n posthogTraceId: 'traceId',\n posthogProperties: 'properties',\n posthogPrivacyMode: 'privacyMode',\n posthogGroups: 'groups',\n posthogModelOverride: 'modelOverride',\n posthogProviderOverride: 'providerOverride',\n posthogCostOverride: 'costOverride',\n posthogCaptureImmediate: 'captureImmediate',\n}\n\nexport function extractPosthogParams<T>(body: T & MonitoringParams): {\n providerParams: T\n posthogParams: MonitoringEventPropertiesWithDefaults\n} {\n const providerParams: Record<string, unknown> = {}\n const posthogParams: Record<string, unknown> = {}\n\n for (const [key, value] of Object.entries(body)) {\n if (POSTHOG_PARAMS_MAP[key as keyof MonitoringParams]) {\n posthogParams[POSTHOG_PARAMS_MAP[key as keyof MonitoringParams]] = value\n } else if (key.startsWith('posthog')) {\n console.warn(`Unknown Posthog parameter ${key}`)\n } else {\n providerParams[key] = value\n }\n }\n\n return {\n providerParams: providerParams as T,\n posthogParams: addDefaults(posthogParams),\n }\n}\n\nfunction addDefaults(params: MonitoringEventProperties): MonitoringEventPropertiesWithDefaults {\n return {\n ...params,\n privacyMode: params.privacyMode ?? false,\n traceId: params.traceId ?? uuidv4(),\n }\n}\n\nexport function formatOpenAIResponsesInput(input: unknown, instructions?: string | null): FormattedMessage[] {\n const messages: FormattedMessage[] = []\n\n if (instructions) {\n messages.push({\n role: 'system',\n content: instructions,\n })\n }\n\n if (Array.isArray(input)) {\n for (const item of input) {\n if (typeof item === 'string') {\n messages.push({ role: 'user', content: item })\n } else if (item && typeof item === 'object') {\n const obj = item as Record<string, unknown>\n const role = isString(obj.role) ? obj.role : 'user'\n\n // Handle content properly - preserve structure for objects/arrays\n const content = obj.content ?? obj.text ?? item\n messages.push({ role, content: toContentString(content) })\n } else {\n messages.push({ role: 'user', content: toContentString(item) })\n }\n }\n } else if (typeof input === 'string') {\n messages.push({ role: 'user', content: input })\n } else if (input) {\n messages.push({ role: 'user', content: toContentString(input) })\n }\n\n return messages\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\";\n\n//#region src/load/map_keys.ts\nfunction keyToJson(key, map) {\n\treturn map?.[key] || snakeCase(key);\n}\nfunction keyFromJson(key, map) {\n\treturn map?.[key] || camelCase(key);\n}\nfunction mapKeys(fields, mapper, map) {\n\tconst mapped = {};\n\tfor (const key in fields) if (Object.hasOwn(fields, key)) mapped[mapper(key, map)] = fields[key];\n\treturn mapped;\n}\n\n//#endregion\nexport { keyFromJson, keyToJson, mapKeys };\n//# sourceMappingURL=map_keys.js.map","//#region src/load/validation.ts\n/**\n* Sentinel key used to mark escaped user objects during serialization.\n*\n* When a plain object contains 'lc' key (which could be confused with LC objects),\n* we wrap it as `{\"__lc_escaped__\": {...original...}}`.\n*/\nconst LC_ESCAPED_KEY = \"__lc_escaped__\";\n/**\n* Check if an object needs escaping to prevent confusion with LC objects.\n*\n* An object needs escaping if:\n* 1. It has an `'lc'` key (could be confused with LC serialization format)\n* 2. It has only the escape key (would be mistaken for an escaped object)\n*/\nfunction needsEscaping(obj) {\n\treturn \"lc\" in obj || Object.keys(obj).length === 1 && LC_ESCAPED_KEY in obj;\n}\n/**\n* Wrap an object in the escape marker.\n*\n* @example\n* ```typescript\n* {\"key\": \"value\"} // becomes {\"__lc_escaped__\": {\"key\": \"value\"}}\n* ```\n*/\nfunction escapeObject(obj) {\n\treturn { [LC_ESCAPED_KEY]: obj };\n}\n/**\n* Check if an object is an escaped user object.\n*\n* @example\n* ```typescript\n* {\"__lc_escaped__\": {...}} // is an escaped object\n* ```\n*/\nfunction isEscapedObject(obj) {\n\treturn Object.keys(obj).length === 1 && LC_ESCAPED_KEY in obj;\n}\n/**\n* Check if an object looks like a Serializable instance (duck typing).\n*/\nfunction isSerializableLike(obj) {\n\treturn obj !== null && typeof obj === \"object\" && \"lc_serializable\" in obj && typeof obj.toJSON === \"function\";\n}\n/**\n* Create a \"not_implemented\" serialization result for objects that cannot be serialized.\n*/\nfunction createNotImplemented(obj) {\n\tlet id;\n\tif (obj !== null && typeof obj === \"object\") if (\"lc_id\" in obj && Array.isArray(obj.lc_id)) id = obj.lc_id;\n\telse id = [obj.constructor?.name ?? \"Object\"];\n\telse id = [typeof obj];\n\treturn {\n\t\tlc: 1,\n\t\ttype: \"not_implemented\",\n\t\tid\n\t};\n}\n/**\n* Escape a value if it needs escaping (contains `lc` key).\n*\n* This is a simpler version of `serializeValue` that doesn't handle Serializable\n* objects - it's meant to be called on kwargs values that have already been\n* processed by `toJSON()`.\n*\n* @param value - The value to potentially escape.\n* @param pathSet - WeakSet to track ancestor objects in the current path to detect circular references.\n* Objects are removed after processing to allow shared references (same object in\n* multiple places) while still detecting true circular references (ancestor in descendant).\n* @returns The value with any `lc`-containing objects wrapped in escape markers.\n*/\nfunction escapeIfNeeded(value, pathSet = /* @__PURE__ */ new WeakSet()) {\n\tif (value !== null && typeof value === \"object\" && !Array.isArray(value)) {\n\t\tif (pathSet.has(value)) return createNotImplemented(value);\n\t\tif (isSerializableLike(value)) return value;\n\t\tpathSet.add(value);\n\t\tconst record = value;\n\t\tif (needsEscaping(record)) {\n\t\t\tpathSet.delete(value);\n\t\t\treturn escapeObject(record);\n\t\t}\n\t\tconst result = {};\n\t\tfor (const [key, val] of Object.entries(record)) result[key] = escapeIfNeeded(val, pathSet);\n\t\tpathSet.delete(value);\n\t\treturn result;\n\t}\n\tif (Array.isArray(value)) return value.map((item) => escapeIfNeeded(item, pathSet));\n\treturn value;\n}\n/**\n* Unescape a value, processing escape markers in object values and arrays.\n*\n* When an escaped object is encountered (`{\"__lc_escaped__\": ...}`), it's\n* unwrapped and the contents are returned AS-IS (no further processing).\n* The contents represent user data that should not be modified.\n*\n* For regular objects and arrays, we recurse to find any nested escape markers.\n*\n* @param obj - The value to unescape.\n* @returns The unescaped value.\n*/\nfunction unescapeValue(obj) {\n\tif (obj !== null && typeof obj === \"object\" && !Array.isArray(obj)) {\n\t\tconst record = obj;\n\t\tif (isEscapedObject(record)) return record[LC_ESCAPED_KEY];\n\t\tconst result = {};\n\t\tfor (const [key, value] of Object.entries(record)) result[key] = unescapeValue(value);\n\t\treturn result;\n\t}\n\tif (Array.isArray(obj)) return obj.map((item) => unescapeValue(item));\n\treturn obj;\n}\n\n//#endregion\nexport { escapeIfNeeded, isEscapedObject, unescapeValue };\n//# sourceMappingURL=validation.js.map","import { __exportAll } from \"../_virtual/_rolldown/runtime.js\";\nimport { keyToJson, mapKeys } from \"./map_keys.js\";\nimport { escapeIfNeeded } from \"./validation.js\";\n\n//#region src/load/serializable.ts\nvar serializable_exports = /* @__PURE__ */ __exportAll({\n\tSerializable: () => Serializable,\n\tget_lc_unique_name: () => get_lc_unique_name\n});\nfunction shallowCopy(obj) {\n\treturn Array.isArray(obj) ? [...obj] : { ...obj };\n}\nfunction replaceSecrets(root, secretsMap) {\n\tconst result = shallowCopy(root);\n\tfor (const [path, secretId] of Object.entries(secretsMap)) {\n\t\tconst [last, ...partsReverse] = path.split(\".\").reverse();\n\t\tlet current = result;\n\t\tfor (const part of partsReverse.reverse()) {\n\t\t\tif (current[part] === void 0) break;\n\t\t\tcurrent[part] = shallowCopy(current[part]);\n\t\t\tcurrent = current[part];\n\t\t}\n\t\tif (current[last] !== void 0) current[last] = {\n\t\t\tlc: 1,\n\t\t\ttype: \"secret\",\n\t\t\tid: [secretId]\n\t\t};\n\t}\n\treturn 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*/\nfunction get_lc_unique_name(serializableClass) {\n\tconst parentClass = Object.getPrototypeOf(serializableClass);\n\tif (typeof serializableClass.lc_name === \"function\" && (typeof parentClass.lc_name !== \"function\" || serializableClass.lc_name() !== parentClass.lc_name())) return serializableClass.lc_name();\n\telse return serializableClass.name;\n}\nvar Serializable = class Serializable {\n\tlc_serializable = false;\n\tlc_kwargs;\n\t/**\n\t* The name of the serializable. Override to provide an alias or\n\t* to preserve the serialized module name in minified environments.\n\t*\n\t* Implemented as a static method to support loading logic.\n\t*/\n\tstatic lc_name() {\n\t\treturn this.name;\n\t}\n\t/**\n\t* The final serialized identifier for the module.\n\t*/\n\tget lc_id() {\n\t\treturn [...this.lc_namespace, get_lc_unique_name(this.constructor)];\n\t}\n\t/**\n\t* A map of secrets, which will be omitted from serialization.\n\t* Keys are paths to the secret in constructor args, e.g. \"foo.bar.baz\".\n\t* Values are the secret ids, which will be used when deserializing.\n\t*/\n\tget lc_secrets() {}\n\t/**\n\t* A map of additional attributes to merge with constructor args.\n\t* Keys are the attribute names, e.g. \"foo\".\n\t* Values are the attribute values, which will be serialized.\n\t* These attributes need to be accepted by the constructor as arguments.\n\t*/\n\tget lc_attributes() {}\n\t/**\n\t* A map of aliases for constructor args.\n\t* Keys are the attribute names, e.g. \"foo\".\n\t* Values are the alias that will replace the key in serialization.\n\t* This is used to eg. make argument names match Python.\n\t*/\n\tget lc_aliases() {}\n\t/**\n\t* A manual list of keys that should be serialized.\n\t* If not overridden, all fields passed into the constructor will be serialized.\n\t*/\n\tget lc_serializable_keys() {}\n\tconstructor(kwargs, ..._args) {\n\t\tif (this.lc_serializable_keys !== void 0) this.lc_kwargs = Object.fromEntries(Object.entries(kwargs || {}).filter(([key]) => this.lc_serializable_keys?.includes(key)));\n\t\telse this.lc_kwargs = kwargs ?? {};\n\t}\n\ttoJSON() {\n\t\tif (!this.lc_serializable) return this.toJSONNotImplemented();\n\t\tif (this.lc_kwargs instanceof Serializable || typeof this.lc_kwargs !== \"object\" || Array.isArray(this.lc_kwargs)) return this.toJSONNotImplemented();\n\t\tconst aliases = {};\n\t\tconst secrets = {};\n\t\tconst kwargs = Object.keys(this.lc_kwargs).reduce((acc, key) => {\n\t\t\tacc[key] = key in this ? this[key] : this.lc_kwargs[key];\n\t\t\treturn acc;\n\t\t}, {});\n\t\tfor (let current = Object.getPrototypeOf(this); current; current = Object.getPrototypeOf(current)) {\n\t\t\tObject.assign(aliases, Reflect.get(current, \"lc_aliases\", this));\n\t\t\tObject.assign(secrets, Reflect.get(current, \"lc_secrets\", this));\n\t\t\tObject.assign(kwargs, Reflect.get(current, \"lc_attributes\", this));\n\t\t}\n\t\tObject.keys(secrets).forEach((keyPath) => {\n\t\t\tlet read = this;\n\t\t\tlet write = kwargs;\n\t\t\tconst [last, ...partsReverse] = keyPath.split(\".\").reverse();\n\t\t\tfor (const key of partsReverse.reverse()) {\n\t\t\t\tif (!(key in read) || read[key] === void 0) return;\n\t\t\t\tif (!(key in write) || write[key] === void 0) {\n\t\t\t\t\tif (typeof read[key] === \"object\" && read[key] != null) write[key] = {};\n\t\t\t\t\telse if (Array.isArray(read[key])) write[key] = [];\n\t\t\t\t}\n\t\t\t\tread = read[key];\n\t\t\t\twrite = write[key];\n\t\t\t}\n\t\t\tif (last in read && read[last] !== void 0) write[last] = write[last] || read[last];\n\t\t});\n\t\tconst escapedKwargs = {};\n\t\tconst pathSet = /* @__PURE__ */ new WeakSet();\n\t\tpathSet.add(this);\n\t\tfor (const [key, value] of Object.entries(kwargs)) escapedKwargs[key] = escapeIfNeeded(value, pathSet);\n\t\tconst processedKwargs = mapKeys(Object.keys(secrets).length ? replaceSecrets(escapedKwargs, secrets) : escapedKwargs, keyToJson, aliases);\n\t\treturn {\n\t\t\tlc: 1,\n\t\t\ttype: \"constructor\",\n\t\t\tid: this.lc_id,\n\t\t\tkwargs: processedKwargs\n\t\t};\n\t}\n\ttoJSONNotImplemented() {\n\t\treturn {\n\t\t\tlc: 1,\n\t\t\ttype: \"not_implemented\",\n\t\t\tid: this.lc_id\n\t\t};\n\t}\n};\n\n//#endregion\nexport { Serializable, get_lc_unique_name, serializable_exports };\n//# sourceMappingURL=serializable.js.map","import { __exportAll } from \"../_virtual/_rolldown/runtime.js\";\n\n//#region src/utils/env.ts\nvar env_exports = /* @__PURE__ */ __exportAll({\n\tgetEnv: () => getEnv,\n\tgetEnvironmentVariable: () => getEnvironmentVariable,\n\tgetRuntimeEnvironment: () => getRuntimeEnvironment,\n\tisBrowser: () => isBrowser,\n\tisDeno: () => isDeno,\n\tisJsDom: () => isJsDom,\n\tisNode: () => isNode,\n\tisWebWorker: () => isWebWorker\n});\nconst isBrowser = () => typeof window !== \"undefined\" && typeof window.document !== \"undefined\";\nconst isWebWorker = () => typeof globalThis === \"object\" && globalThis.constructor && globalThis.constructor.name === \"DedicatedWorkerGlobalScope\";\nconst isJsDom = () => typeof window !== \"undefined\" && window.name === \"nodejs\" || typeof navigator !== \"undefined\" && navigator.userAgent.includes(\"jsdom\");\nconst isDeno = () => typeof Deno !== \"undefined\";\nconst isNode = () => typeof process !== \"undefined\" && typeof process.versions !== \"undefined\" && typeof process.versions.node !== \"undefined\" && !isDeno();\nconst getEnv = () => {\n\tlet env;\n\tif (isBrowser()) env = \"browser\";\n\telse if (isNode()) env = \"node\";\n\telse if (isWebWorker()) env = \"webworker\";\n\telse if (isJsDom()) env = \"jsdom\";\n\telse if (isDeno()) env = \"deno\";\n\telse env = \"other\";\n\treturn env;\n};\nlet runtimeEnvironment;\nfunction getRuntimeEnvironment() {\n\tif (runtimeEnvironment === void 0) runtimeEnvironment = {\n\t\tlibrary: \"langchain-js\",\n\t\truntime: getEnv()\n\t};\n\treturn runtimeEnvironment;\n}\nfunction getEnvironmentVariable(name) {\n\ttry {\n\t\tif (typeof process !== \"undefined\") return process.env?.[name];\n\t\telse if (isDeno()) return Deno?.env.get(name);\n\t\telse return;\n\t} catch {\n\t\treturn;\n\t}\n}\n\n//#endregion\nexport { env_exports, getEnv, getEnvironmentVariable, getRuntimeEnvironment, isBrowser, isDeno, isJsDom, isNode, isWebWorker };\n//# sourceMappingURL=env.js.map","import { __exportAll } from \"../_virtual/_rolldown/runtime.js\";\nimport { Serializable, get_lc_unique_name } from \"../load/serializable.js\";\nimport { getEnvironmentVariable } from \"../utils/env.js\";\nimport * as uuid from \"uuid\";\n\n//#region src/callbacks/base.ts\nvar base_exports = /* @__PURE__ */ __exportAll({\n\tBaseCallbackHandler: () => BaseCallbackHandler,\n\tcallbackHandlerPrefersStreaming: () => callbackHandlerPrefersStreaming,\n\tisBaseCallbackHandler: () => isBaseCallbackHandler\n});\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*/\nvar BaseCallbackHandlerMethodsClass = class {};\nfunction callbackHandlerPrefersStreaming(x) {\n\treturn \"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*/\nvar BaseCallbackHandler = class extends BaseCallbackHandlerMethodsClass {\n\tlc_serializable = false;\n\tget lc_namespace() {\n\t\treturn [\n\t\t\t\"langchain_core\",\n\t\t\t\"callbacks\",\n\t\t\tthis.name\n\t\t];\n\t}\n\tget lc_secrets() {}\n\tget lc_attributes() {}\n\tget lc_aliases() {}\n\tget lc_serializable_keys() {}\n\t/**\n\t* The name of the serializable. Override to provide an alias or\n\t* to preserve the serialized module name in minified environments.\n\t*\n\t* Implemented as a static method to support loading logic.\n\t*/\n\tstatic lc_name() {\n\t\treturn this.name;\n\t}\n\t/**\n\t* The final serialized identifier for the module.\n\t*/\n\tget lc_id() {\n\t\treturn [...this.lc_namespace, get_lc_unique_name(this.constructor)];\n\t}\n\tlc_kwargs;\n\tignoreLLM = false;\n\tignoreChain = false;\n\tignoreAgent = false;\n\tignoreRetriever = false;\n\tignoreCustomEvent = false;\n\traiseError = false;\n\tawaitHandlers = getEnvironmentVariable(\"LANGCHAIN_CALLBACKS_BACKGROUND\") === \"false\";\n\tconstructor(input) {\n\t\tsuper();\n\t\tthis.lc_kwargs = input || {};\n\t\tif (input) {\n\t\t\tthis.ignoreLLM = input.ignoreLLM ?? this.ignoreLLM;\n\t\t\tthis.ignoreChain = input.ignoreChain ?? this.ignoreChain;\n\t\t\tthis.ignoreAgent = input.ignoreAgent ?? this.ignoreAgent;\n\t\t\tthis.ignoreRetriever = input.ignoreRetriever ?? this.ignoreRetriever;\n\t\t\tthis.ignoreCustomEvent = input.ignoreCustomEvent ?? this.ignoreCustomEvent;\n\t\t\tthis.raiseError = input.raiseError ?? this.raiseError;\n\t\t\tthis.awaitHandlers = this.raiseError || (input._awaitHandler ?? this.awaitHandlers);\n\t\t}\n\t}\n\tcopy() {\n\t\treturn new this.constructor(this);\n\t}\n\ttoJSON() {\n\t\treturn Serializable.prototype.toJSON.call(this);\n\t}\n\ttoJSONNotImplemented() {\n\t\treturn Serializable.prototype.toJSONNotImplemented.call(this);\n\t}\n\tstatic fromMethods(methods) {\n\t\tclass Handler extends BaseCallbackHandler {\n\t\t\tname = uuid.v7();\n\t\t\tconstructor() {\n\t\t\t\tsuper();\n\t\t\t\tObject.assign(this, methods);\n\t\t\t}\n\t\t}\n\t\treturn new Handler();\n\t}\n};\nconst isBaseCallbackHandler = (x) => {\n\tconst callbackHandler = x;\n\treturn callbackHandler !== void 0 && typeof callbackHandler.copy === \"function\" && typeof callbackHandler.name === \"string\" && typeof callbackHandler.awaitHandlers === \"boolean\";\n};\n\n//#endregion\nexport { BaseCallbackHandler, base_exports, callbackHandlerPrefersStreaming, isBaseCallbackHandler };\n//# sourceMappingURL=base.js.map","import { sanitizeValues } from './utils'\n\nconst DEFAULT_MAX_DEPTH = 3\nconst MAX_STACK_LINES = 20\n\nexport function serializeError(value: unknown, depth = DEFAULT_MAX_DEPTH): unknown {\n if (depth < 0 || value === null || typeof value !== 'object') {\n return value\n }\n if (value instanceof Error) {\n const out: Record<string, unknown> = {\n name: value.name,\n message: value.message,\n stack: truncateStack(value.stack),\n }\n for (const key of Object.keys(value)) {\n out[key] = serializeError((value as unknown as Record<string, unknown>)[key], depth - 1)\n }\n if (value.cause !== undefined) {\n out.cause = serializeError(value.cause, depth - 1)\n }\n return out\n }\n if (Array.isArray(value)) {\n return value.map((item) => serializeError(item, depth - 1))\n }\n return value\n}\n\nexport function stringifyError(error: unknown): string {\n tr