@posthog/ai 8.14.4 → 8.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +14 -0
  2. package/dist/adk/index.cjs +22 -7
  3. package/dist/adk/index.cjs.map +1 -1
  4. package/dist/adk/index.mjs +22 -7
  5. package/dist/adk/index.mjs.map +1 -1
  6. package/dist/anthropic/index.cjs +12 -5
  7. package/dist/anthropic/index.cjs.map +1 -1
  8. package/dist/anthropic/index.mjs +12 -5
  9. package/dist/anthropic/index.mjs.map +1 -1
  10. package/dist/claude-agent-sdk/index.cjs +12 -5
  11. package/dist/claude-agent-sdk/index.cjs.map +1 -1
  12. package/dist/claude-agent-sdk/index.mjs +12 -5
  13. package/dist/claude-agent-sdk/index.mjs.map +1 -1
  14. package/dist/gemini/index.cjs +403 -19
  15. package/dist/gemini/index.cjs.map +1 -1
  16. package/dist/gemini/index.d.ts +28 -0
  17. package/dist/gemini/index.mjs +403 -19
  18. package/dist/gemini/index.mjs.map +1 -1
  19. package/dist/index.cjs +12 -5
  20. package/dist/index.cjs.map +1 -1
  21. package/dist/index.mjs +12 -5
  22. package/dist/index.mjs.map +1 -1
  23. package/dist/langchain/index.cjs +12 -5
  24. package/dist/langchain/index.cjs.map +1 -1
  25. package/dist/langchain/index.mjs +12 -5
  26. package/dist/langchain/index.mjs.map +1 -1
  27. package/dist/langchain/middleware/index.cjs +12 -5
  28. package/dist/langchain/middleware/index.cjs.map +1 -1
  29. package/dist/langchain/middleware/index.mjs +12 -5
  30. package/dist/langchain/middleware/index.mjs.map +1 -1
  31. package/dist/openai/index.cjs +12 -5
  32. package/dist/openai/index.cjs.map +1 -1
  33. package/dist/openai/index.mjs +12 -5
  34. package/dist/openai/index.mjs.map +1 -1
  35. package/dist/openai-agents/index.cjs +12 -5
  36. package/dist/openai-agents/index.cjs.map +1 -1
  37. package/dist/openai-agents/index.mjs +12 -5
  38. package/dist/openai-agents/index.mjs.map +1 -1
  39. package/dist/otel/index.cjs +11 -4
  40. package/dist/otel/index.cjs.map +1 -1
  41. package/dist/otel/index.mjs +11 -4
  42. package/dist/otel/index.mjs.map +1 -1
  43. package/dist/vercel/index.cjs +12 -5
  44. package/dist/vercel/index.cjs.map +1 -1
  45. package/dist/vercel/index.mjs +12 -5
  46. package/dist/vercel/index.mjs.map +1 -1
  47. package/package.json +4 -3
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["DEFAULT_OTEL_HOST","normalizeToken","normalizeHost","OTLPTraceExporter","OTLPTraceExporter","BatchSpanProcessor"],"sources":["../../src/sanitization/base64_recognizer.ts","../../src/sanitization/media_type_context.ts","../../src/sanitization/binary_content_redactor.ts","../../src/otel/spans.ts","../../src/otel/redact.ts","../../src/gatewayWarning.ts","../../src/otel/exporter.ts","../../src/otel/requestContext.ts","../../src/otel/processor.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 private readonly explicitMediaType?: string\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.explicitMediaType) return this.explicitMediaType\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 hasExplicitBinaryMediaType(): boolean {\n if (!this.explicitMediaType && (!this.parent || !this.key || !STRONG_CONTEXT_KEYS.has(this.key))) return false\n const mediaType = this.inferFromSiblingMime()\n return mediaType !== undefined && !mediaType.toLowerCase().startsWith('text/')\n }\n\n signalsBinary(): boolean {\n if (this.explicitMediaType) return true\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, mediaType?: string): T\n redact(value: unknown, mediaType?: string): unknown {\n this.visited = new WeakSet()\n return this.walk(value, mediaType ? new MediaTypeContext(undefined, undefined, mediaType) : MediaTypeContext.EMPTY)\n }\n\n /** Redact one leaf while a caller owns bounded, type-preserving traversal. @internal */\n redactLeaf(value: string | Uint8Array, context: MediaTypeContext): string {\n return typeof value === 'string' ? this.redactString(value, context) : this.placeholderFor(context.inferMediaType())\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.redactLeaf(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.redactLeaf(value, ctx)\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 Object.defineProperty(out, k, {\n value: this.walk(obj[k], new MediaTypeContext(obj, k)),\n enumerable: true,\n writable: true,\n configurable: true,\n })\n }\n return out\n }\n\n private redactString(value: string, ctx: MediaTypeContext): string {\n const hasExplicitBinaryMediaType = ctx.hasExplicitBinaryMediaType()\n const recognitionValue = hasExplicitBinaryMediaType ? value.replace(/[\\r\\n]/g, '') : value\n const minLength = hasExplicitBinaryMediaType\n ? Math.min(recognitionValue.length, STRONG_CONTEXT_MIN_LENGTH)\n : ctx.signalsBinary()\n ? STRONG_CONTEXT_MIN_LENGTH\n : WEAK_CONTEXT_MIN_LENGTH\n const recognition = this.recognizer.recognize(recognitionValue, 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","import type { Attributes } from '@opentelemetry/api'\nimport type { ReadableSpan, TimedEvent } from '@opentelemetry/sdk-trace-base'\n\nconst AI_SPAN_PREFIXES = ['gen_ai.', 'llm.', 'ai.', 'traceloop.'] as const\n\n/**\n * Returns `true` when the span is AI-related — its name or any attribute\n * key starts with `gen_ai.`, `llm.`, `ai.`, or `traceloop.`.\n */\nexport function isAISpan(span: ReadableSpan): boolean {\n if (AI_SPAN_PREFIXES.some((prefix) => span.name.startsWith(prefix))) {\n return true\n }\n const attributes = span.attributes\n if (attributes) {\n return Object.keys(attributes).some((key) => AI_SPAN_PREFIXES.some((prefix) => key.startsWith(prefix)))\n }\n return false\n}\n\n/** Copies rather than mutates: the span is shared across every registered processor/exporter. */\nexport function withAttributes(span: ReadableSpan, attributes: Attributes, events?: TimedEvent[]): ReadableSpan {\n return Object.create(span, {\n attributes: { value: attributes, enumerable: true, configurable: true },\n ...(events ? { events: { value: events, enumerable: true, configurable: true } } : {}),\n })\n}\n","import type { Attributes, AttributeValue } from '@opentelemetry/api'\nimport type { ReadableSpan, TimedEvent } from '@opentelemetry/sdk-trace-base'\n\nimport { BinaryContentRedactor } from '../sanitization/binary_content_redactor'\nimport { withAttributes } from './spans'\n\n// Deliberately always redacts here — the OTLP export path has no per-client passthrough gate.\nconst redactor = new BinaryContentRedactor()\n\nexport function redactSpan(span: ReadableSpan): ReadableSpan {\n const attributes = span.attributes ? redactAttributes(span.attributes) : span.attributes\n const events = span.events ? redactEvents(span.events) : span.events\n if (attributes === span.attributes && events === span.events) {\n return span\n }\n return withAttributes(span, attributes, events)\n}\n\nfunction redactAttributes(attributes: Attributes): Attributes {\n let changed = false\n const out: Attributes = {}\n for (const key of Object.keys(attributes)) {\n const value = attributes[key]\n const redacted = value === undefined ? value : redactAttributeValue(value)\n if (redacted !== value) {\n changed = true\n }\n out[key] = redacted\n }\n return changed ? out : attributes\n}\n\nfunction redactEvents(events: TimedEvent[]): TimedEvent[] {\n let changed = false\n const out = events.map((event) => {\n if (!event.attributes) {\n return event\n }\n const attributes = redactAttributes(event.attributes)\n if (attributes === event.attributes) {\n return event\n }\n changed = true\n return { ...event, attributes }\n })\n return changed ? out : events\n}\n\nfunction redactAttributeValue(value: AttributeValue): AttributeValue {\n if (typeof value === 'string') {\n return redactString(value)\n }\n if (isStringArray(value)) {\n let changed = false\n const out = value.map((item) => {\n if (typeof item !== 'string') {\n return item\n }\n const redacted = redactString(item)\n if (redacted !== item) {\n changed = true\n }\n return redacted\n })\n return changed ? out : value\n }\n return value\n}\n\nfunction isStringArray(value: AttributeValue): value is Array<string | null | undefined> {\n return Array.isArray(value) && value.every((item) => typeof item !== 'number' && typeof item !== 'boolean')\n}\n\nfunction redactString(value: string): string {\n const trimmed = value.trimStart()\n if (trimmed.startsWith('{') || trimmed.startsWith('[')) {\n const redacted = redactJson(value)\n if (redacted !== undefined) {\n return redacted\n }\n }\n return redactor.redact(value)\n}\n\nfunction redactJson(value: string): string | undefined {\n let parsed: unknown\n try {\n parsed = JSON.parse(value)\n } catch {\n return undefined\n }\n if (parsed === null || typeof parsed !== 'object') {\n return undefined\n }\n const redactedStr = JSON.stringify(redactor.redact(parsed))\n // Preserve the original string (and its formatting) when nothing was redacted.\n return redactedStr === JSON.stringify(parsed) ? value : redactedStr\n}\n","// Warn when a wrapper's base_url points at the PostHog AI Gateway: the gateway\n// emits its own $ai_generation, so each call would be captured (and, for billable\n// products, billed) twice. We only warn — the wrapper's event carries data the\n// gateway never sees (groups, custom properties, trace hierarchy).\n\n// Keep in sync with the gateway's deployed hosts (see services/llm-gateway in the\n// main repo). gateway.us.posthog.com is live today; the rest are listed ahead of\n// any traffic moving to them.\nexport const POSTHOG_AI_GATEWAY_HOSTS: readonly string[] = [\n 'gateway.posthog.com',\n 'gateway.us.posthog.com',\n 'gateway.eu.posthog.com',\n 'ai-gateway.us.posthog.com',\n 'ai-gateway.eu.posthog.com',\n]\n\n// Swap for the dedicated AI Gateway page once it ships.\nconst GATEWAY_DOCS_URL = 'https://posthog.com/docs/ai-observability'\n\nconst extractHost = (baseURL: string): string | undefined => {\n try {\n // Tolerate bare hosts that omit a scheme, e.g. \"gateway.us.posthog.com/v1\".\n const hasScheme = /^[a-z][a-z0-9+.-]*:\\/\\//i.test(baseURL)\n return new URL(hasScheme ? baseURL : `https://${baseURL}`).hostname.toLowerCase()\n } catch {\n return undefined\n }\n}\n\nexport const isPostHogAiGatewayUrl = (baseURL: string | undefined | null): boolean => {\n if (!baseURL) {\n return false\n }\n const host = extractHost(baseURL)\n return host !== undefined && POSTHOG_AI_GATEWAY_HOSTS.includes(host)\n}\n\n// Warns on every gateway call by design: the misconfiguration is impossible to\n// miss that way, and a doubled bill is worse than noisy logs.\nexport const warnIfPostHogAiGateway = (baseURL: string | undefined | null): void => {\n if (!isPostHogAiGatewayUrl(baseURL)) {\n return\n }\n console.warn(\n '[PostHog] The PostHog AI wrapper is pointed at the PostHog AI Gateway. ' +\n 'Both capture $ai_generation, so every call is double-counted and double-billed. ' +\n `Use one or the other — see ${GATEWAY_DOCS_URL}.`\n )\n}\n\n// OTel spans don't pass through captureAiGeneration, so detect the gateway from\n// the span's host/URL attributes instead. These follow the GenAI / HTTP semantic\n// conventions: `server.address` is a bare host, `url.full` a full URL, both of\n// which isPostHogAiGatewayUrl accepts.\nconst OTEL_GATEWAY_URL_ATTRIBUTES = ['server.address', 'url.full'] as const\n\nexport const warnIfPostHogAiGatewayOtelAttributes = (attributes: Record<string, unknown> | undefined): void => {\n if (!attributes) {\n return\n }\n for (const key of OTEL_GATEWAY_URL_ATTRIBUTES) {\n const value = attributes[key]\n if (typeof value === 'string' && isPostHogAiGatewayUrl(value)) {\n warnIfPostHogAiGateway(value)\n return\n }\n }\n}\n","import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'\nimport type { ReadableSpan } from '@opentelemetry/sdk-trace-base'\n\nimport { redactSpan } from './redact'\nimport { isAISpan } from './spans'\nimport { warnIfPostHogAiGatewayOtelAttributes } from '../gatewayWarning'\n\nconst DEFAULT_OTEL_HOST = 'https://us.i.posthog.com'\n// OpenTelemetry's ExportResultCode.SUCCESS is 0. Keep this local so loading the\n// published subpath does not require the exporter package's transitive dependencies.\nconst EXPORT_SUCCESS = 0\n\nfunction normalizeToken(value?: unknown): string {\n return typeof value === 'string' ? value.trim() : ''\n}\n\nfunction normalizeHost(value?: unknown): string {\n const normalizedValue = typeof value === 'string' ? value.trim() : ''\n return normalizedValue || DEFAULT_OTEL_HOST\n}\n\n/**\n * Options for the PostHogTraceExporter. `projectToken` is required; a blank token disables the\n * exporter as a defensive no-op. You can also optionally override the `host` URL. `host` defaults to `https://us.i.posthog.com`.\n *\n * @example\n * ```ts\n * import { PostHogTraceExporter } from '@posthog/ai/otel'\n *\n * new PostHogTraceExporter({ projectToken: 'phc_...' })\n * ```\n *\n * @example\n * ```ts\n * import { PostHogTraceExporter } from '@posthog/ai/otel'\n *\n * new PostHogTraceExporter({ projectToken: 'phc_...', host: 'https://eu.i.posthog.com' })\n * ```\n */\nexport type PostHogTraceExporterOptions = { projectToken: string; host?: string }\n\n/**\n * An OpenTelemetry `TraceExporter` that sends AI traces to PostHog's OTLP\n * ingestion endpoint. PostHog converts `gen_ai.*` spans into\n * `$ai_generation` events server-side.\n *\n * Only AI-related spans (those whose name or attribute keys start with\n * `gen_ai.`, `llm.`, `ai.`, or `traceloop.`) are exported; all other\n * spans are silently dropped.\n *\n * Use this when the API you're integrating with only accepts a\n * `TraceExporter` (e.g. Vercel's `registerOTel`) or when you need to\n * plug PostHog into an existing processor chain. Otherwise prefer\n * {@link PostHogSpanProcessor}, which is self-contained.\n *\n * `projectToken` is required; a blank token disables the exporter as a defensive no-op.\n * You can also optionally override the `host` URL.\n *\n * Exporters only see finished spans, so they can't add per-request PostHog context;\n * use {@link PostHogSpanProcessor} with `client` for that.\n *\n * @example\n * ```ts\n * import { PostHogTraceExporter } from '@posthog/ai/otel'\n * import { registerOTel } from '@vercel/otel'\n *\n * registerOTel({\n * serviceName: 'my-app',\n * traceExporter: new PostHogTraceExporter({ projectToken: 'phc_...' }),\n * })\n * ```\n */\nexport class PostHogTraceExporter extends OTLPTraceExporter {\n private readonly disabled: boolean\n\n constructor(options: PostHogTraceExporterOptions) {\n const token = normalizeToken(options.projectToken)\n const disabled = !token\n const host = token ? new URL(normalizeHost(options.host)).origin : DEFAULT_OTEL_HOST\n super({\n url: `${host}/i/v0/ai/otel`,\n headers: token\n ? {\n Authorization: `Bearer ${token}`,\n }\n : {},\n })\n\n this.disabled = disabled\n if (this.disabled) {\n console.warn('[PostHogTraceExporter] projectToken is missing or blank; the exporter will be disabled.')\n }\n }\n\n override export(spans: ReadableSpan[], resultCallback: (result: { code: number; error?: Error }) => void): void {\n if (this.disabled) {\n // Intentionally report success: missing or blank tokens disable exporting as a compatibility no-op.\n // Reporting failure would make OpenTelemetry treat every span as an export error.\n resultCallback({ code: EXPORT_SUCCESS })\n return\n }\n\n const aiSpans = spans.filter(isAISpan)\n if (aiSpans.length === 0) {\n resultCallback({ code: EXPORT_SUCCESS })\n return\n }\n for (const span of aiSpans) {\n warnIfPostHogAiGatewayOtelAttributes(span.attributes)\n }\n super.export(aiSpans.map(redactSpan), resultCallback)\n }\n}\n","import type { AttributeValue, Attributes } from '@opentelemetry/api'\nimport type { ReadableSpan } from '@opentelemetry/sdk-trace-base'\n\nimport { withAttributes } from './spans'\n\nexport type CapturedContext = {\n distinctId?: string\n sessionId?: string\n properties?: Record<string, unknown>\n}\n\n// Structural so declarations type-check against posthog-node versions that have no getContext.\nexport type PostHogContextSource = {\n getContext?(): CapturedContext | undefined\n}\n\nconst DISTINCT_ID_ATTRIBUTE = 'posthog.distinct_id'\nconst SESSION_ID_ATTRIBUTE = '$session_id'\n\n// Ingestion prefers posthog.distinct_id, so adding it would override the user a span already names.\nconst IDENTITY_ATTRIBUTES = [\n DISTINCT_ID_ATTRIBUTE,\n 'user.id',\n 'ai.telemetry.metadata.posthog_distinct_id',\n 'ai.settings.context.posthog_distinct_id',\n]\n\ntype Primitive = string | number | boolean\n\nfunction isPrimitive(value: unknown): value is Primitive {\n return (\n typeof value === 'string' || typeof value === 'boolean' || (typeof value === 'number' && Number.isFinite(value))\n )\n}\n\nfunction isHomogeneousPrimitiveArray(value: unknown): value is string[] | number[] | boolean[] {\n return Array.isArray(value) && value.every((item) => isPrimitive(item) && typeof item === typeof value[0])\n}\n\nfunction rejectNonFinite(_key: string, value: unknown): unknown {\n if (typeof value === 'number' && !Number.isFinite(value)) {\n throw new TypeError('non-finite number')\n }\n return value\n}\n\nfunction toAttributeValue(value: unknown): AttributeValue | undefined {\n if (value === undefined || value === null || value === '') {\n return undefined\n }\n if (isPrimitive(value) || isHomogeneousPrimitiveArray(value)) {\n return value\n }\n try {\n const json = JSON.stringify(value, rejectNonFinite)\n if (json === undefined || json === 'null') {\n return undefined\n }\n return json.startsWith('\"') ? String(JSON.parse(json)) || undefined : json\n } catch {\n return undefined\n }\n}\n\nfunction hasValues(context: CapturedContext): boolean {\n return Boolean(context.distinctId || context.sessionId || Object.keys(context.properties ?? {}).length > 0)\n}\n\nexport function captureContext(client: PostHogContextSource): CapturedContext | undefined {\n try {\n // posthog-node versions before getContext existed are still within the peer range.\n if (typeof client.getContext !== 'function') {\n return undefined\n }\n const context = client.getContext()\n return context && hasValues(context) ? context : undefined\n } catch {\n // Capturing is best effort and must never break the instrumented call.\n return undefined\n }\n}\n\nfunction toAttributes(context: CapturedContext, spanAttributes: Attributes): Attributes {\n const hasIdentity = IDENTITY_ATTRIBUTES.some((key) => spanAttributes[key] !== undefined)\n const values: Record<string, unknown> = {\n ...(context.distinctId && !hasIdentity ? { [DISTINCT_ID_ATTRIBUTE]: context.distinctId } : {}),\n ...(context.sessionId ? { [SESSION_ID_ATTRIBUTE]: context.sessionId } : {}),\n ...context.properties,\n }\n const attributes: Attributes = {}\n for (const [key, value] of Object.entries(values)) {\n if (hasIdentity && key === DISTINCT_ID_ATTRIBUTE) {\n continue\n }\n const attribute = toAttributeValue(value)\n if (attribute !== undefined) {\n attributes[key] = attribute\n }\n }\n return attributes\n}\n\nexport function withRequestContext(span: ReadableSpan, context: CapturedContext): ReadableSpan {\n try {\n const attributes = toAttributes(context, span.attributes)\n if (Object.keys(attributes).length === 0) {\n return span\n }\n return withAttributes(span, { ...attributes, ...span.attributes })\n } catch {\n // Conversion runs inside span.end() and must never break the instrumented call.\n return span\n }\n}\n","import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'\nimport type { Context } from '@opentelemetry/api'\nimport { BatchSpanProcessor, type SpanProcessor, type ReadableSpan, type Span } from '@opentelemetry/sdk-trace-base'\n\nimport { redactSpan } from './redact'\nimport { captureContext, withRequestContext, type CapturedContext, type PostHogContextSource } from './requestContext'\nimport { isAISpan } from './spans'\nimport { warnIfPostHogAiGatewayOtelAttributes } from '../gatewayWarning'\n\nconst DEFAULT_OTEL_HOST = 'https://us.i.posthog.com'\n\nfunction normalizeToken(value?: unknown): string {\n return typeof value === 'string' ? value.trim() : ''\n}\n\nfunction normalizeHost(value?: unknown): string {\n const normalizedValue = typeof value === 'string' ? value.trim() : ''\n return normalizedValue || DEFAULT_OTEL_HOST\n}\n\nexport interface PostHogSpanProcessorOptions {\n /**\n * Your PostHog project token (the `phc_...` key). Required; a blank token disables the\n * processor as a defensive no-op.\n */\n projectToken: string\n\n /**\n * PostHog host URL. Defaults to `https://us.i.posthog.com`.\n */\n host?: string\n\n /**\n * Your posthog-node client. When set, each AI span this processor exports gets the distinct ID,\n * session ID (as `$session_id`) and properties from the context entered with `client.withContext()` or\n * `client.enterContext()`. Other processors on the provider don't see them.\n * Attributes already on the span win over properties, which win over the context's IDs.\n * The context is captured when a span starts, but its properties are read when the span ends,\n * so mutating a context object while a span is open shows up in the export.\n * Must be the same posthog-node instance you call `withContext` or `enterContext` on.\n */\n client?: PostHogContextSource\n\n /**\n * @internal Injected processor for testing — bypasses exporter creation.\n */\n _spanProcessor?: SpanProcessor\n}\n\nclass NoopSpanProcessor implements SpanProcessor {\n onStart(_span: Span, _parentContext: Context): void {\n return\n }\n onEnd(_span: ReadableSpan): void {\n return\n }\n shutdown(): Promise<void> {\n return Promise.resolve()\n }\n forceFlush(): Promise<void> {\n return Promise.resolve()\n }\n}\n\n/**\n * An OpenTelemetry `SpanProcessor` that sends AI traces to PostHog.\n *\n * `projectToken` is required; a blank token disables the processor as a defensive no-op.\n *\n * Internally batches spans and exports them to PostHog's OTLP ingestion\n * endpoint. Only AI-related spans (those whose name or attribute keys\n * start with `gen_ai.`, `llm.`, `ai.`, or `traceloop.`) are exported;\n * all other spans are silently dropped.\n *\n * Request-scoped and serverless runtimes should keep a reference to this\n * processor and await {@link PostHogSpanProcessor.forceFlush} before the request\n * lifecycle ends. This waits for queued exports without sending one request per span.\n *\n * This is the recommended integration point when your setup accepts a\n * `SpanProcessor`. If you need a `TraceExporter` instead (e.g. for\n * Vercel's `registerOTel`), use {@link PostHogTraceExporter}.\n *\n * @example\n * ```ts\n * import { PostHogSpanProcessor } from '@posthog/ai/otel'\n * import { NodeSDK } from '@opentelemetry/sdk-node'\n *\n * const processor = new PostHogSpanProcessor({ projectToken: 'phc_...' })\n * const sdk = new NodeSDK({ spanProcessors: [processor] })\n * sdk.start()\n *\n * // In request-scoped runtimes, wait for queued exports before returning.\n * await processor.forceFlush()\n * ```\n *\n * @example\n * ```ts\n * // Per request: pass your posthog-node client, then wrap the work in a context.\n * const processor = new PostHogSpanProcessor({ projectToken: 'phc_...', client: posthog })\n * await posthog.withContext({ distinctId: user.id, properties: { plan: 'pro' } }, () => handleRequest())\n * ```\n */\nexport class PostHogSpanProcessor implements SpanProcessor {\n private readonly inner: SpanProcessor\n private readonly client?: PostHogContextSource\n private readonly capturedContexts = new WeakMap<ReadableSpan, CapturedContext>()\n\n constructor(options: PostHogSpanProcessorOptions) {\n const token = normalizeToken(options.projectToken)\n if (!token) {\n console.warn('[PostHogSpanProcessor] projectToken is missing or blank; the processor will be disabled.')\n this.inner = new NoopSpanProcessor()\n return\n }\n this.client = options.client\n\n if (options._spanProcessor) {\n this.inner = options._spanProcessor\n } else {\n const host = new URL(normalizeHost(options.host)).origin\n const exporter = new OTLPTraceExporter({\n url: `${host}/i/v0/ai/otel`,\n headers: {\n Authorization: `Bearer ${token}`,\n },\n })\n this.inner = new BatchSpanProcessor(exporter)\n }\n }\n\n onStart(span: Span, parentContext: Context): void {\n const captured = this.client && captureContext(this.client)\n if (captured) {\n this.capturedContexts.set(span, captured)\n }\n // Forwarded unconditionally — filtering happens in onEnd. We can't filter\n // here because the span hasn't finished yet and may not have AI attributes\n // set. BatchSpanProcessor.onStart is a no-op so this is safe.\n this.inner.onStart(span, parentContext)\n }\n\n onEnd(span: ReadableSpan): void {\n const captured = this.capturedContexts.get(span)\n this.capturedContexts.delete(span)\n if (isAISpan(span)) {\n warnIfPostHogAiGatewayOtelAttributes(span.attributes)\n this.inner.onEnd(redactSpan(captured ? withRequestContext(span, captured) : span))\n }\n }\n\n shutdown(): Promise<void> {\n return this.inner.shutdown()\n }\n\n forceFlush(): Promise<void> {\n return this.inner.forceFlush()\n }\n}\n"],"mappings":";;;;AAAA,MAAM,qBAAqB;AAC3B,MAAM,qBAAqB;AAI3B,IAAa,mBAAb,MAA8B;CAC5B,UAAU,OAAe,WAAsC;EAC7D,MAAM,UAAU,mBAAmB,KAAK,KAAK;EAC7C,IAAI,SAAS,OAAO;GAAE,MAAM;GAAY,WAAW,QAAQ;EAAG;EAE9D,IAAI,MAAM,SAAS,WAAW,OAAO,EAAE,MAAM,OAAO;EAEpD,MAAM,mBAAmB,MAAM,MAAM,GAAG,SAAS;EACjD,IAAI,mBAAmB,KAAK,gBAAgB,GAC1C,OAAO,EAAE,MAAM,MAAM;OAErB,OAAO,EAAE,MAAM,OAAO;CAE1B;AACF;;;ACnBA,MAAM,iBAAiB;CAAC;CAAa;CAAc;CAAY;AAAW;AAE1E,MAAM,sCAAsB,IAAI,IAAI;CAClC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC;AAED,MAAM,uCAAuB,IAAI,IAAI;CACnC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC;AAED,MAAM,oCAAoB,IAAI,IAAI;CAAC;CAAQ;CAAc;CAAY;CAAS;AAAW,CAAC;AAE1F,MAAM,sCAAsB,IAAI,IAAI;CAAC;CAAO;CAAO;CAAO;CAAQ;CAAO;CAAO;AAAM,CAAC;AAEvF,IAAa,mBAAb,MAAa,iBAAiB;;EACJ,KAAA,QAAA,IAAI,iBAAiB,KAAA,GAAW,KAAA,CAAS;;CAEjE,YACE,QACA,KACA,mBACA;EAHiB,KAAA,SAAA;EACA,KAAA,MAAA;EACA,KAAA,oBAAA;CAChB;CAEH,iBAAqC;EACnC,OACE,KAAK,qBAAqB,KAAK,KAAK,uBA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AA,IAAI,QAAuC;EAG7E,MAAM,QAAQ,eAAe,QAAQ,YAAY;EACjD,IAAI,CAAC,OAAO;GACV,QAAQ,KAAK,0FAA0F;GACvG,KAAK,QAAQ,IAAI,kBAAkB;GACnC;EACF;EACA,KAAK,SAAS,QAAQ;EAEtB,IAAI,QAAQ,gBACV,KAAK,QAAQ,QAAQ;OAChB;GACL,MAAM,OAAO,IAAI,IAAI,cAAc,QAAQ,IAAI,CAAC,CAAC,CAAC;GAClD,MAAM,WAAW,IAAII,wCAAAA,kBAAkB;IACrC,KAAK,GAAG,KAAK;IACb,SAAS,EACP,eAAe,UAAU,QAC3B;GACF,CAAC;GACD,KAAK,QAAQ,IAAIC,8BAAAA,mBAAmB,QAAQ;EAC9C;CACF;CAEA,QAAQ,MAAY,eAA8B;EAChD,MAAM,WAAW,KAAK,UAAU,eAAe,KAAK,MAAM;EAC1D,IAAI,UACF,KAAK,iBAAiB,IAAI,MAAM,QAAQ;EAK1C,KAAK,MAAM,QAAQ,MAAM,aAAa;CACxC;CAEA,MAAM,MAA0B;EAC9B,MAAM,WAAW,KAAK,iBAAiB,IAAI,IAAI;EAC/C,KAAK,iBAAiB,OAAO,IAAI;EACjC,IAAI,SAAS,IAAI,GAAG;GAClB,qCAAqC,KAAK,UAAU;GACpD,KAAK,MAAM,MAAM,WAAW,WAAW,mBAAmB,MAAM,QAAQ,IAAI,IAAI,CAAC;EACnF;CACF;CAEA,WAA0B;EACxB,OAAO,KAAK,MAAM,SAAS;CAC7B;CAEA,aAA4B;EAC1B,OAAO,KAAK,MAAM,WAAW;CAC/B;AACF"}
1
+ {"version":3,"file":"index.cjs","names":["DEFAULT_OTEL_HOST","normalizeToken","normalizeHost","OTLPTraceExporter","OTLPTraceExporter","BatchSpanProcessor"],"sources":["../../src/sanitization/base64_recognizer.ts","../../src/sanitization/media_type_context.ts","../../src/sanitization/binary_content_redactor.ts","../../src/otel/spans.ts","../../src/otel/redact.ts","../../src/gatewayWarning.ts","../../src/otel/exporter.ts","../../src/otel/requestContext.ts","../../src/otel/processor.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'])\nconst TYPED_BINARY_DATA_TYPES = new Set(['image', 'audio', 'video', 'document'])\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 private readonly explicitMediaType?: string\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.explicitMediaType) return this.explicitMediaType\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 hasDefiniteBinaryData(): boolean {\n if (\n this.parent &&\n this.key === 'data' &&\n typeof this.parent.type === 'string' &&\n TYPED_BINARY_DATA_TYPES.has(this.parent.type)\n )\n return true\n if (!this.explicitMediaType && (!this.parent || !this.key || !STRONG_CONTEXT_KEYS.has(this.key))) return false\n const mediaType = this.inferFromSiblingMime()\n return mediaType !== undefined && !mediaType.toLowerCase().startsWith('text/')\n }\n\n signalsBinary(): boolean {\n if (this.explicitMediaType) return true\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, mediaType?: string): T\n redact(value: unknown, mediaType?: string): unknown {\n this.visited = new WeakSet()\n return this.walk(value, mediaType ? new MediaTypeContext(undefined, undefined, mediaType) : MediaTypeContext.EMPTY)\n }\n\n /** Redact one leaf while a caller owns bounded, type-preserving traversal. @internal */\n redactLeaf(value: string | Uint8Array, context: MediaTypeContext): string {\n return typeof value === 'string' ? this.redactString(value, context) : this.placeholderFor(context.inferMediaType())\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.redactLeaf(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.redactLeaf(value, ctx)\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 Object.defineProperty(out, k, {\n value: this.walk(obj[k], new MediaTypeContext(obj, k)),\n enumerable: true,\n writable: true,\n configurable: true,\n })\n }\n return out\n }\n\n private redactString(value: string, ctx: MediaTypeContext): string {\n const hasDefiniteBinaryData = ctx.hasDefiniteBinaryData()\n const recognitionValue = hasDefiniteBinaryData ? value.replace(/[\\r\\n]/g, '') : value\n const minLength = hasDefiniteBinaryData\n ? Math.min(recognitionValue.length, STRONG_CONTEXT_MIN_LENGTH)\n : ctx.signalsBinary()\n ? STRONG_CONTEXT_MIN_LENGTH\n : WEAK_CONTEXT_MIN_LENGTH\n const recognition = this.recognizer.recognize(recognitionValue, 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","import type { Attributes } from '@opentelemetry/api'\nimport type { ReadableSpan, TimedEvent } from '@opentelemetry/sdk-trace-base'\n\nconst AI_SPAN_PREFIXES = ['gen_ai.', 'llm.', 'ai.', 'traceloop.'] as const\n\n/**\n * Returns `true` when the span is AI-related — its name or any attribute\n * key starts with `gen_ai.`, `llm.`, `ai.`, or `traceloop.`.\n */\nexport function isAISpan(span: ReadableSpan): boolean {\n if (AI_SPAN_PREFIXES.some((prefix) => span.name.startsWith(prefix))) {\n return true\n }\n const attributes = span.attributes\n if (attributes) {\n return Object.keys(attributes).some((key) => AI_SPAN_PREFIXES.some((prefix) => key.startsWith(prefix)))\n }\n return false\n}\n\n/** Copies rather than mutates: the span is shared across every registered processor/exporter. */\nexport function withAttributes(span: ReadableSpan, attributes: Attributes, events?: TimedEvent[]): ReadableSpan {\n return Object.create(span, {\n attributes: { value: attributes, enumerable: true, configurable: true },\n ...(events ? { events: { value: events, enumerable: true, configurable: true } } : {}),\n })\n}\n","import type { Attributes, AttributeValue } from '@opentelemetry/api'\nimport type { ReadableSpan, TimedEvent } from '@opentelemetry/sdk-trace-base'\n\nimport { BinaryContentRedactor } from '../sanitization/binary_content_redactor'\nimport { withAttributes } from './spans'\n\n// Deliberately always redacts here — the OTLP export path has no per-client passthrough gate.\nconst redactor = new BinaryContentRedactor()\n\nexport function redactSpan(span: ReadableSpan): ReadableSpan {\n const attributes = span.attributes ? redactAttributes(span.attributes) : span.attributes\n const events = span.events ? redactEvents(span.events) : span.events\n if (attributes === span.attributes && events === span.events) {\n return span\n }\n return withAttributes(span, attributes, events)\n}\n\nfunction redactAttributes(attributes: Attributes): Attributes {\n let changed = false\n const out: Attributes = {}\n for (const key of Object.keys(attributes)) {\n const value = attributes[key]\n const redacted = value === undefined ? value : redactAttributeValue(value)\n if (redacted !== value) {\n changed = true\n }\n out[key] = redacted\n }\n return changed ? out : attributes\n}\n\nfunction redactEvents(events: TimedEvent[]): TimedEvent[] {\n let changed = false\n const out = events.map((event) => {\n if (!event.attributes) {\n return event\n }\n const attributes = redactAttributes(event.attributes)\n if (attributes === event.attributes) {\n return event\n }\n changed = true\n return { ...event, attributes }\n })\n return changed ? out : events\n}\n\nfunction redactAttributeValue(value: AttributeValue): AttributeValue {\n if (typeof value === 'string') {\n return redactString(value)\n }\n if (isStringArray(value)) {\n let changed = false\n const out = value.map((item) => {\n if (typeof item !== 'string') {\n return item\n }\n const redacted = redactString(item)\n if (redacted !== item) {\n changed = true\n }\n return redacted\n })\n return changed ? out : value\n }\n return value\n}\n\nfunction isStringArray(value: AttributeValue): value is Array<string | null | undefined> {\n return Array.isArray(value) && value.every((item) => typeof item !== 'number' && typeof item !== 'boolean')\n}\n\nfunction redactString(value: string): string {\n const trimmed = value.trimStart()\n if (trimmed.startsWith('{') || trimmed.startsWith('[')) {\n const redacted = redactJson(value)\n if (redacted !== undefined) {\n return redacted\n }\n }\n return redactor.redact(value)\n}\n\nfunction redactJson(value: string): string | undefined {\n let parsed: unknown\n try {\n parsed = JSON.parse(value)\n } catch {\n return undefined\n }\n if (parsed === null || typeof parsed !== 'object') {\n return undefined\n }\n const redactedStr = JSON.stringify(redactor.redact(parsed))\n // Preserve the original string (and its formatting) when nothing was redacted.\n return redactedStr === JSON.stringify(parsed) ? value : redactedStr\n}\n","// Warn when a wrapper's base_url points at the PostHog AI Gateway: the gateway\n// emits its own $ai_generation, so each call would be captured (and, for billable\n// products, billed) twice. We only warn — the wrapper's event carries data the\n// gateway never sees (groups, custom properties, trace hierarchy).\n\n// Keep in sync with the gateway's deployed hosts (see services/llm-gateway in the\n// main repo). gateway.us.posthog.com is live today; the rest are listed ahead of\n// any traffic moving to them.\nexport const POSTHOG_AI_GATEWAY_HOSTS: readonly string[] = [\n 'gateway.posthog.com',\n 'gateway.us.posthog.com',\n 'gateway.eu.posthog.com',\n 'ai-gateway.us.posthog.com',\n 'ai-gateway.eu.posthog.com',\n]\n\n// Swap for the dedicated AI Gateway page once it ships.\nconst GATEWAY_DOCS_URL = 'https://posthog.com/docs/ai-observability'\n\nconst extractHost = (baseURL: string): string | undefined => {\n try {\n // Tolerate bare hosts that omit a scheme, e.g. \"gateway.us.posthog.com/v1\".\n const hasScheme = /^[a-z][a-z0-9+.-]*:\\/\\//i.test(baseURL)\n return new URL(hasScheme ? baseURL : `https://${baseURL}`).hostname.toLowerCase()\n } catch {\n return undefined\n }\n}\n\nexport const isPostHogAiGatewayUrl = (baseURL: string | undefined | null): boolean => {\n if (!baseURL) {\n return false\n }\n const host = extractHost(baseURL)\n return host !== undefined && POSTHOG_AI_GATEWAY_HOSTS.includes(host)\n}\n\n// Warns on every gateway call by design: the misconfiguration is impossible to\n// miss that way, and a doubled bill is worse than noisy logs.\nexport const warnIfPostHogAiGateway = (baseURL: string | undefined | null): void => {\n if (!isPostHogAiGatewayUrl(baseURL)) {\n return\n }\n console.warn(\n '[PostHog] The PostHog AI wrapper is pointed at the PostHog AI Gateway. ' +\n 'Both capture $ai_generation, so every call is double-counted and double-billed. ' +\n `Use one or the other — see ${GATEWAY_DOCS_URL}.`\n )\n}\n\n// OTel spans don't pass through captureAiGeneration, so detect the gateway from\n// the span's host/URL attributes instead. These follow the GenAI / HTTP semantic\n// conventions: `server.address` is a bare host, `url.full` a full URL, both of\n// which isPostHogAiGatewayUrl accepts.\nconst OTEL_GATEWAY_URL_ATTRIBUTES = ['server.address', 'url.full'] as const\n\nexport const warnIfPostHogAiGatewayOtelAttributes = (attributes: Record<string, unknown> | undefined): void => {\n if (!attributes) {\n return\n }\n for (const key of OTEL_GATEWAY_URL_ATTRIBUTES) {\n const value = attributes[key]\n if (typeof value === 'string' && isPostHogAiGatewayUrl(value)) {\n warnIfPostHogAiGateway(value)\n return\n }\n }\n}\n","import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'\nimport type { ReadableSpan } from '@opentelemetry/sdk-trace-base'\n\nimport { redactSpan } from './redact'\nimport { isAISpan } from './spans'\nimport { warnIfPostHogAiGatewayOtelAttributes } from '../gatewayWarning'\n\nconst DEFAULT_OTEL_HOST = 'https://us.i.posthog.com'\n// OpenTelemetry's ExportResultCode.SUCCESS is 0. Keep this local so loading the\n// published subpath does not require the exporter package's transitive dependencies.\nconst EXPORT_SUCCESS = 0\n\nfunction normalizeToken(value?: unknown): string {\n return typeof value === 'string' ? value.trim() : ''\n}\n\nfunction normalizeHost(value?: unknown): string {\n const normalizedValue = typeof value === 'string' ? value.trim() : ''\n return normalizedValue || DEFAULT_OTEL_HOST\n}\n\n/**\n * Options for the PostHogTraceExporter. `projectToken` is required; a blank token disables the\n * exporter as a defensive no-op. You can also optionally override the `host` URL. `host` defaults to `https://us.i.posthog.com`.\n *\n * @example\n * ```ts\n * import { PostHogTraceExporter } from '@posthog/ai/otel'\n *\n * new PostHogTraceExporter({ projectToken: 'phc_...' })\n * ```\n *\n * @example\n * ```ts\n * import { PostHogTraceExporter } from '@posthog/ai/otel'\n *\n * new PostHogTraceExporter({ projectToken: 'phc_...', host: 'https://eu.i.posthog.com' })\n * ```\n */\nexport type PostHogTraceExporterOptions = { projectToken: string; host?: string }\n\n/**\n * An OpenTelemetry `TraceExporter` that sends AI traces to PostHog's OTLP\n * ingestion endpoint. PostHog converts `gen_ai.*` spans into\n * `$ai_generation` events server-side.\n *\n * Only AI-related spans (those whose name or attribute keys start with\n * `gen_ai.`, `llm.`, `ai.`, or `traceloop.`) are exported; all other\n * spans are silently dropped.\n *\n * Use this when the API you're integrating with only accepts a\n * `TraceExporter` (e.g. Vercel's `registerOTel`) or when you need to\n * plug PostHog into an existing processor chain. Otherwise prefer\n * {@link PostHogSpanProcessor}, which is self-contained.\n *\n * `projectToken` is required; a blank token disables the exporter as a defensive no-op.\n * You can also optionally override the `host` URL.\n *\n * Exporters only see finished spans, so they can't add per-request PostHog context;\n * use {@link PostHogSpanProcessor} with `client` for that.\n *\n * @example\n * ```ts\n * import { PostHogTraceExporter } from '@posthog/ai/otel'\n * import { registerOTel } from '@vercel/otel'\n *\n * registerOTel({\n * serviceName: 'my-app',\n * traceExporter: new PostHogTraceExporter({ projectToken: 'phc_...' }),\n * })\n * ```\n */\nexport class PostHogTraceExporter extends OTLPTraceExporter {\n private readonly disabled: boolean\n\n constructor(options: PostHogTraceExporterOptions) {\n const token = normalizeToken(options.projectToken)\n const disabled = !token\n const host = token ? new URL(normalizeHost(options.host)).origin : DEFAULT_OTEL_HOST\n super({\n url: `${host}/i/v0/ai/otel`,\n headers: token\n ? {\n Authorization: `Bearer ${token}`,\n }\n : {},\n })\n\n this.disabled = disabled\n if (this.disabled) {\n console.warn('[PostHogTraceExporter] projectToken is missing or blank; the exporter will be disabled.')\n }\n }\n\n override export(spans: ReadableSpan[], resultCallback: (result: { code: number; error?: Error }) => void): void {\n if (this.disabled) {\n // Intentionally report success: missing or blank tokens disable exporting as a compatibility no-op.\n // Reporting failure would make OpenTelemetry treat every span as an export error.\n resultCallback({ code: EXPORT_SUCCESS })\n return\n }\n\n const aiSpans = spans.filter(isAISpan)\n if (aiSpans.length === 0) {\n resultCallback({ code: EXPORT_SUCCESS })\n return\n }\n for (const span of aiSpans) {\n warnIfPostHogAiGatewayOtelAttributes(span.attributes)\n }\n super.export(aiSpans.map(redactSpan), resultCallback)\n }\n}\n","import type { AttributeValue, Attributes } from '@opentelemetry/api'\nimport type { ReadableSpan } from '@opentelemetry/sdk-trace-base'\n\nimport { withAttributes } from './spans'\n\nexport type CapturedContext = {\n distinctId?: string\n sessionId?: string\n properties?: Record<string, unknown>\n}\n\n// Structural so declarations type-check against posthog-node versions that have no getContext.\nexport type PostHogContextSource = {\n getContext?(): CapturedContext | undefined\n}\n\nconst DISTINCT_ID_ATTRIBUTE = 'posthog.distinct_id'\nconst SESSION_ID_ATTRIBUTE = '$session_id'\n\n// Ingestion prefers posthog.distinct_id, so adding it would override the user a span already names.\nconst IDENTITY_ATTRIBUTES = [\n DISTINCT_ID_ATTRIBUTE,\n 'user.id',\n 'ai.telemetry.metadata.posthog_distinct_id',\n 'ai.settings.context.posthog_distinct_id',\n]\n\ntype Primitive = string | number | boolean\n\nfunction isPrimitive(value: unknown): value is Primitive {\n return (\n typeof value === 'string' || typeof value === 'boolean' || (typeof value === 'number' && Number.isFinite(value))\n )\n}\n\nfunction isHomogeneousPrimitiveArray(value: unknown): value is string[] | number[] | boolean[] {\n return Array.isArray(value) && value.every((item) => isPrimitive(item) && typeof item === typeof value[0])\n}\n\nfunction rejectNonFinite(_key: string, value: unknown): unknown {\n if (typeof value === 'number' && !Number.isFinite(value)) {\n throw new TypeError('non-finite number')\n }\n return value\n}\n\nfunction toAttributeValue(value: unknown): AttributeValue | undefined {\n if (value === undefined || value === null || value === '') {\n return undefined\n }\n if (isPrimitive(value) || isHomogeneousPrimitiveArray(value)) {\n return value\n }\n try {\n const json = JSON.stringify(value, rejectNonFinite)\n if (json === undefined || json === 'null') {\n return undefined\n }\n return json.startsWith('\"') ? String(JSON.parse(json)) || undefined : json\n } catch {\n return undefined\n }\n}\n\nfunction hasValues(context: CapturedContext): boolean {\n return Boolean(context.distinctId || context.sessionId || Object.keys(context.properties ?? {}).length > 0)\n}\n\nexport function captureContext(client: PostHogContextSource): CapturedContext | undefined {\n try {\n // posthog-node versions before getContext existed are still within the peer range.\n if (typeof client.getContext !== 'function') {\n return undefined\n }\n const context = client.getContext()\n return context && hasValues(context) ? context : undefined\n } catch {\n // Capturing is best effort and must never break the instrumented call.\n return undefined\n }\n}\n\nfunction toAttributes(context: CapturedContext, spanAttributes: Attributes): Attributes {\n const hasIdentity = IDENTITY_ATTRIBUTES.some((key) => spanAttributes[key] !== undefined)\n const values: Record<string, unknown> = {\n ...(context.distinctId && !hasIdentity ? { [DISTINCT_ID_ATTRIBUTE]: context.distinctId } : {}),\n ...(context.sessionId ? { [SESSION_ID_ATTRIBUTE]: context.sessionId } : {}),\n ...context.properties,\n }\n const attributes: Attributes = {}\n for (const [key, value] of Object.entries(values)) {\n if (hasIdentity && key === DISTINCT_ID_ATTRIBUTE) {\n continue\n }\n const attribute = toAttributeValue(value)\n if (attribute !== undefined) {\n attributes[key] = attribute\n }\n }\n return attributes\n}\n\nexport function withRequestContext(span: ReadableSpan, context: CapturedContext): ReadableSpan {\n try {\n const attributes = toAttributes(context, span.attributes)\n if (Object.keys(attributes).length === 0) {\n return span\n }\n return withAttributes(span, { ...attributes, ...span.attributes })\n } catch {\n // Conversion runs inside span.end() and must never break the instrumented call.\n return span\n }\n}\n","import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'\nimport type { Context } from '@opentelemetry/api'\nimport { BatchSpanProcessor, type SpanProcessor, type ReadableSpan, type Span } from '@opentelemetry/sdk-trace-base'\n\nimport { redactSpan } from './redact'\nimport { captureContext, withRequestContext, type CapturedContext, type PostHogContextSource } from './requestContext'\nimport { isAISpan } from './spans'\nimport { warnIfPostHogAiGatewayOtelAttributes } from '../gatewayWarning'\n\nconst DEFAULT_OTEL_HOST = 'https://us.i.posthog.com'\n\nfunction normalizeToken(value?: unknown): string {\n return typeof value === 'string' ? value.trim() : ''\n}\n\nfunction normalizeHost(value?: unknown): string {\n const normalizedValue = typeof value === 'string' ? value.trim() : ''\n return normalizedValue || DEFAULT_OTEL_HOST\n}\n\nexport interface PostHogSpanProcessorOptions {\n /**\n * Your PostHog project token (the `phc_...` key). Required; a blank token disables the\n * processor as a defensive no-op.\n */\n projectToken: string\n\n /**\n * PostHog host URL. Defaults to `https://us.i.posthog.com`.\n */\n host?: string\n\n /**\n * Your posthog-node client. When set, each AI span this processor exports gets the distinct ID,\n * session ID (as `$session_id`) and properties from the context entered with `client.withContext()` or\n * `client.enterContext()`. Other processors on the provider don't see them.\n * Attributes already on the span win over properties, which win over the context's IDs.\n * The context is captured when a span starts, but its properties are read when the span ends,\n * so mutating a context object while a span is open shows up in the export.\n * Must be the same posthog-node instance you call `withContext` or `enterContext` on.\n */\n client?: PostHogContextSource\n\n /**\n * @internal Injected processor for testing — bypasses exporter creation.\n */\n _spanProcessor?: SpanProcessor\n}\n\nclass NoopSpanProcessor implements SpanProcessor {\n onStart(_span: Span, _parentContext: Context): void {\n return\n }\n onEnd(_span: ReadableSpan): void {\n return\n }\n shutdown(): Promise<void> {\n return Promise.resolve()\n }\n forceFlush(): Promise<void> {\n return Promise.resolve()\n }\n}\n\n/**\n * An OpenTelemetry `SpanProcessor` that sends AI traces to PostHog.\n *\n * `projectToken` is required; a blank token disables the processor as a defensive no-op.\n *\n * Internally batches spans and exports them to PostHog's OTLP ingestion\n * endpoint. Only AI-related spans (those whose name or attribute keys\n * start with `gen_ai.`, `llm.`, `ai.`, or `traceloop.`) are exported;\n * all other spans are silently dropped.\n *\n * Request-scoped and serverless runtimes should keep a reference to this\n * processor and await {@link PostHogSpanProcessor.forceFlush} before the request\n * lifecycle ends. This waits for queued exports without sending one request per span.\n *\n * This is the recommended integration point when your setup accepts a\n * `SpanProcessor`. If you need a `TraceExporter` instead (e.g. for\n * Vercel's `registerOTel`), use {@link PostHogTraceExporter}.\n *\n * @example\n * ```ts\n * import { PostHogSpanProcessor } from '@posthog/ai/otel'\n * import { NodeSDK } from '@opentelemetry/sdk-node'\n *\n * const processor = new PostHogSpanProcessor({ projectToken: 'phc_...' })\n * const sdk = new NodeSDK({ spanProcessors: [processor] })\n * sdk.start()\n *\n * // In request-scoped runtimes, wait for queued exports before returning.\n * await processor.forceFlush()\n * ```\n *\n * @example\n * ```ts\n * // Per request: pass your posthog-node client, then wrap the work in a context.\n * const processor = new PostHogSpanProcessor({ projectToken: 'phc_...', client: posthog })\n * await posthog.withContext({ distinctId: user.id, properties: { plan: 'pro' } }, () => handleRequest())\n * ```\n */\nexport class PostHogSpanProcessor implements SpanProcessor {\n private readonly inner: SpanProcessor\n private readonly client?: PostHogContextSource\n private readonly capturedContexts = new WeakMap<ReadableSpan, CapturedContext>()\n\n constructor(options: PostHogSpanProcessorOptions) {\n const token = normalizeToken(options.projectToken)\n if (!token) {\n console.warn('[PostHogSpanProcessor] projectToken is missing or blank; the processor will be disabled.')\n this.inner = new NoopSpanProcessor()\n return\n }\n this.client = options.client\n\n if (options._spanProcessor) {\n this.inner = options._spanProcessor\n } else {\n const host = new URL(normalizeHost(options.host)).origin\n const exporter = new OTLPTraceExporter({\n url: `${host}/i/v0/ai/otel`,\n headers: {\n Authorization: `Bearer ${token}`,\n },\n })\n this.inner = new BatchSpanProcessor(exporter)\n }\n }\n\n onStart(span: Span, parentContext: Context): void {\n const captured = this.client && captureContext(this.client)\n if (captured) {\n this.capturedContexts.set(span, captured)\n }\n // Forwarded unconditionally — filtering happens in onEnd. We can't filter\n // here because the span hasn't finished yet and may not have AI attributes\n // set. BatchSpanProcessor.onStart is a no-op so this is safe.\n this.inner.onStart(span, parentContext)\n }\n\n onEnd(span: ReadableSpan): void {\n const captured = this.capturedContexts.get(span)\n this.capturedContexts.delete(span)\n if (isAISpan(span)) {\n warnIfPostHogAiGatewayOtelAttributes(span.attributes)\n this.inner.onEnd(redactSpan(captured ? withRequestContext(span, captured) : span))\n }\n }\n\n shutdown(): Promise<void> {\n return this.inner.shutdown()\n }\n\n forceFlush(): Promise<void> {\n return this.inner.forceFlush()\n }\n}\n"],"mappings":";;;;AAAA,MAAM,qBAAqB;AAC3B,MAAM,qBAAqB;AAI3B,IAAa,mBAAb,MAA8B;CAC5B,UAAU,OAAe,WAAsC;EAC7D,MAAM,UAAU,mBAAmB,KAAK,KAAK;EAC7C,IAAI,SAAS,OAAO;GAAE,MAAM;GAAY,WAAW,QAAQ;EAAG;EAE9D,IAAI,MAAM,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I,QAAQ,gBACV,KAAK,QAAQ,QAAQ;OAChB;GACL,MAAM,OAAO,IAAI,IAAI,cAAc,QAAQ,IAAI,CAAC,CAAC,CAAC;GAClD,MAAM,WAAW,IAAII,wCAAAA,kBAAkB;IACrC,KAAK,GAAG,KAAK;IACb,SAAS,EACP,eAAe,UAAU,QAC3B;GACF,CAAC;GACD,KAAK,QAAQ,IAAIC,8BAAAA,mBAAmB,QAAQ;EAC9C;CACF;CAEA,QAAQ,MAAY,eAA8B;EAChD,MAAM,WAAW,KAAK,UAAU,eAAe,KAAK,MAAM;EAC1D,IAAI,UACF,KAAK,iBAAiB,IAAI,MAAM,QAAQ;EAK1C,KAAK,MAAM,QAAQ,MAAM,aAAa;CACxC;CAEA,MAAM,MAA0B;EAC9B,MAAM,WAAW,KAAK,iBAAiB,IAAI,IAAI;EAC/C,KAAK,iBAAiB,OAAO,IAAI;EACjC,IAAI,SAAS,IAAI,GAAG;GAClB,qCAAqC,KAAK,UAAU;GACpD,KAAK,MAAM,MAAM,WAAW,WAAW,mBAAmB,MAAM,QAAQ,IAAI,IAAI,CAAC;EACnF;CACF;CAEA,WAA0B;EACxB,OAAO,KAAK,MAAM,SAAS;CAC7B;CAEA,aAA4B;EAC1B,OAAO,KAAK,MAAM,WAAW;CAC/B;AACF"}
@@ -61,6 +61,12 @@ const FILE_FAMILY_TYPES = /* @__PURE__ */ new Set([
61
61
  "media",
62
62
  "file-data"
63
63
  ]);
64
+ const TYPED_BINARY_DATA_TYPES = /* @__PURE__ */ new Set([
65
+ "image",
66
+ "audio",
67
+ "video",
68
+ "document"
69
+ ]);
64
70
  const KNOWN_AUDIO_FORMATS = /* @__PURE__ */ new Set([
65
71
  "wav",
66
72
  "mp3",
@@ -112,7 +118,8 @@ var MediaTypeContext = class MediaTypeContext {
112
118
  if (key.includes("image")) return "image";
113
119
  if (key.includes("file") || key.includes("document")) return "application/octet-stream";
114
120
  }
115
- hasExplicitBinaryMediaType() {
121
+ hasDefiniteBinaryData() {
122
+ if (this.parent && this.key === "data" && typeof this.parent.type === "string" && TYPED_BINARY_DATA_TYPES.has(this.parent.type)) return true;
116
123
  if (!this.explicitMediaType && (!this.parent || !this.key || !STRONG_CONTEXT_KEYS.has(this.key))) return false;
117
124
  const mediaType = this.inferFromSiblingMime();
118
125
  return mediaType !== void 0 && !mediaType.toLowerCase().startsWith("text/");
@@ -166,9 +173,9 @@ var BinaryContentRedactor = class {
166
173
  return out;
167
174
  }
168
175
  redactString(value, ctx) {
169
- const hasExplicitBinaryMediaType = ctx.hasExplicitBinaryMediaType();
170
- const recognitionValue = hasExplicitBinaryMediaType ? value.replace(/[\r\n]/g, "") : value;
171
- const minLength = hasExplicitBinaryMediaType ? Math.min(recognitionValue.length, STRONG_CONTEXT_MIN_LENGTH) : ctx.signalsBinary() ? STRONG_CONTEXT_MIN_LENGTH : WEAK_CONTEXT_MIN_LENGTH;
176
+ const hasDefiniteBinaryData = ctx.hasDefiniteBinaryData();
177
+ const recognitionValue = hasDefiniteBinaryData ? value.replace(/[\r\n]/g, "") : value;
178
+ const minLength = hasDefiniteBinaryData ? Math.min(recognitionValue.length, STRONG_CONTEXT_MIN_LENGTH) : ctx.signalsBinary() ? STRONG_CONTEXT_MIN_LENGTH : WEAK_CONTEXT_MIN_LENGTH;
172
179
  const recognition = this.recognizer.recognize(recognitionValue, minLength);
173
180
  switch (recognition.kind) {
174
181
  case "data-url": return this.placeholderFor(recognition.mediaType);
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":["DEFAULT_OTEL_HOST","normalizeToken","normalizeHost"],"sources":["../../src/sanitization/base64_recognizer.ts","../../src/sanitization/media_type_context.ts","../../src/sanitization/binary_content_redactor.ts","../../src/otel/spans.ts","../../src/otel/redact.ts","../../src/gatewayWarning.ts","../../src/otel/exporter.ts","../../src/otel/requestContext.ts","../../src/otel/processor.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 private readonly explicitMediaType?: string\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.explicitMediaType) return this.explicitMediaType\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 hasExplicitBinaryMediaType(): boolean {\n if (!this.explicitMediaType && (!this.parent || !this.key || !STRONG_CONTEXT_KEYS.has(this.key))) return false\n const mediaType = this.inferFromSiblingMime()\n return mediaType !== undefined && !mediaType.toLowerCase().startsWith('text/')\n }\n\n signalsBinary(): boolean {\n if (this.explicitMediaType) return true\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, mediaType?: string): T\n redact(value: unknown, mediaType?: string): unknown {\n this.visited = new WeakSet()\n return this.walk(value, mediaType ? new MediaTypeContext(undefined, undefined, mediaType) : MediaTypeContext.EMPTY)\n }\n\n /** Redact one leaf while a caller owns bounded, type-preserving traversal. @internal */\n redactLeaf(value: string | Uint8Array, context: MediaTypeContext): string {\n return typeof value === 'string' ? this.redactString(value, context) : this.placeholderFor(context.inferMediaType())\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.redactLeaf(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.redactLeaf(value, ctx)\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 Object.defineProperty(out, k, {\n value: this.walk(obj[k], new MediaTypeContext(obj, k)),\n enumerable: true,\n writable: true,\n configurable: true,\n })\n }\n return out\n }\n\n private redactString(value: string, ctx: MediaTypeContext): string {\n const hasExplicitBinaryMediaType = ctx.hasExplicitBinaryMediaType()\n const recognitionValue = hasExplicitBinaryMediaType ? value.replace(/[\\r\\n]/g, '') : value\n const minLength = hasExplicitBinaryMediaType\n ? Math.min(recognitionValue.length, STRONG_CONTEXT_MIN_LENGTH)\n : ctx.signalsBinary()\n ? STRONG_CONTEXT_MIN_LENGTH\n : WEAK_CONTEXT_MIN_LENGTH\n const recognition = this.recognizer.recognize(recognitionValue, 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","import type { Attributes } from '@opentelemetry/api'\nimport type { ReadableSpan, TimedEvent } from '@opentelemetry/sdk-trace-base'\n\nconst AI_SPAN_PREFIXES = ['gen_ai.', 'llm.', 'ai.', 'traceloop.'] as const\n\n/**\n * Returns `true` when the span is AI-related — its name or any attribute\n * key starts with `gen_ai.`, `llm.`, `ai.`, or `traceloop.`.\n */\nexport function isAISpan(span: ReadableSpan): boolean {\n if (AI_SPAN_PREFIXES.some((prefix) => span.name.startsWith(prefix))) {\n return true\n }\n const attributes = span.attributes\n if (attributes) {\n return Object.keys(attributes).some((key) => AI_SPAN_PREFIXES.some((prefix) => key.startsWith(prefix)))\n }\n return false\n}\n\n/** Copies rather than mutates: the span is shared across every registered processor/exporter. */\nexport function withAttributes(span: ReadableSpan, attributes: Attributes, events?: TimedEvent[]): ReadableSpan {\n return Object.create(span, {\n attributes: { value: attributes, enumerable: true, configurable: true },\n ...(events ? { events: { value: events, enumerable: true, configurable: true } } : {}),\n })\n}\n","import type { Attributes, AttributeValue } from '@opentelemetry/api'\nimport type { ReadableSpan, TimedEvent } from '@opentelemetry/sdk-trace-base'\n\nimport { BinaryContentRedactor } from '../sanitization/binary_content_redactor'\nimport { withAttributes } from './spans'\n\n// Deliberately always redacts here — the OTLP export path has no per-client passthrough gate.\nconst redactor = new BinaryContentRedactor()\n\nexport function redactSpan(span: ReadableSpan): ReadableSpan {\n const attributes = span.attributes ? redactAttributes(span.attributes) : span.attributes\n const events = span.events ? redactEvents(span.events) : span.events\n if (attributes === span.attributes && events === span.events) {\n return span\n }\n return withAttributes(span, attributes, events)\n}\n\nfunction redactAttributes(attributes: Attributes): Attributes {\n let changed = false\n const out: Attributes = {}\n for (const key of Object.keys(attributes)) {\n const value = attributes[key]\n const redacted = value === undefined ? value : redactAttributeValue(value)\n if (redacted !== value) {\n changed = true\n }\n out[key] = redacted\n }\n return changed ? out : attributes\n}\n\nfunction redactEvents(events: TimedEvent[]): TimedEvent[] {\n let changed = false\n const out = events.map((event) => {\n if (!event.attributes) {\n return event\n }\n const attributes = redactAttributes(event.attributes)\n if (attributes === event.attributes) {\n return event\n }\n changed = true\n return { ...event, attributes }\n })\n return changed ? out : events\n}\n\nfunction redactAttributeValue(value: AttributeValue): AttributeValue {\n if (typeof value === 'string') {\n return redactString(value)\n }\n if (isStringArray(value)) {\n let changed = false\n const out = value.map((item) => {\n if (typeof item !== 'string') {\n return item\n }\n const redacted = redactString(item)\n if (redacted !== item) {\n changed = true\n }\n return redacted\n })\n return changed ? out : value\n }\n return value\n}\n\nfunction isStringArray(value: AttributeValue): value is Array<string | null | undefined> {\n return Array.isArray(value) && value.every((item) => typeof item !== 'number' && typeof item !== 'boolean')\n}\n\nfunction redactString(value: string): string {\n const trimmed = value.trimStart()\n if (trimmed.startsWith('{') || trimmed.startsWith('[')) {\n const redacted = redactJson(value)\n if (redacted !== undefined) {\n return redacted\n }\n }\n return redactor.redact(value)\n}\n\nfunction redactJson(value: string): string | undefined {\n let parsed: unknown\n try {\n parsed = JSON.parse(value)\n } catch {\n return undefined\n }\n if (parsed === null || typeof parsed !== 'object') {\n return undefined\n }\n const redactedStr = JSON.stringify(redactor.redact(parsed))\n // Preserve the original string (and its formatting) when nothing was redacted.\n return redactedStr === JSON.stringify(parsed) ? value : redactedStr\n}\n","// Warn when a wrapper's base_url points at the PostHog AI Gateway: the gateway\n// emits its own $ai_generation, so each call would be captured (and, for billable\n// products, billed) twice. We only warn — the wrapper's event carries data the\n// gateway never sees (groups, custom properties, trace hierarchy).\n\n// Keep in sync with the gateway's deployed hosts (see services/llm-gateway in the\n// main repo). gateway.us.posthog.com is live today; the rest are listed ahead of\n// any traffic moving to them.\nexport const POSTHOG_AI_GATEWAY_HOSTS: readonly string[] = [\n 'gateway.posthog.com',\n 'gateway.us.posthog.com',\n 'gateway.eu.posthog.com',\n 'ai-gateway.us.posthog.com',\n 'ai-gateway.eu.posthog.com',\n]\n\n// Swap for the dedicated AI Gateway page once it ships.\nconst GATEWAY_DOCS_URL = 'https://posthog.com/docs/ai-observability'\n\nconst extractHost = (baseURL: string): string | undefined => {\n try {\n // Tolerate bare hosts that omit a scheme, e.g. \"gateway.us.posthog.com/v1\".\n const hasScheme = /^[a-z][a-z0-9+.-]*:\\/\\//i.test(baseURL)\n return new URL(hasScheme ? baseURL : `https://${baseURL}`).hostname.toLowerCase()\n } catch {\n return undefined\n }\n}\n\nexport const isPostHogAiGatewayUrl = (baseURL: string | undefined | null): boolean => {\n if (!baseURL) {\n return false\n }\n const host = extractHost(baseURL)\n return host !== undefined && POSTHOG_AI_GATEWAY_HOSTS.includes(host)\n}\n\n// Warns on every gateway call by design: the misconfiguration is impossible to\n// miss that way, and a doubled bill is worse than noisy logs.\nexport const warnIfPostHogAiGateway = (baseURL: string | undefined | null): void => {\n if (!isPostHogAiGatewayUrl(baseURL)) {\n return\n }\n console.warn(\n '[PostHog] The PostHog AI wrapper is pointed at the PostHog AI Gateway. ' +\n 'Both capture $ai_generation, so every call is double-counted and double-billed. ' +\n `Use one or the other — see ${GATEWAY_DOCS_URL}.`\n )\n}\n\n// OTel spans don't pass through captureAiGeneration, so detect the gateway from\n// the span's host/URL attributes instead. These follow the GenAI / HTTP semantic\n// conventions: `server.address` is a bare host, `url.full` a full URL, both of\n// which isPostHogAiGatewayUrl accepts.\nconst OTEL_GATEWAY_URL_ATTRIBUTES = ['server.address', 'url.full'] as const\n\nexport const warnIfPostHogAiGatewayOtelAttributes = (attributes: Record<string, unknown> | undefined): void => {\n if (!attributes) {\n return\n }\n for (const key of OTEL_GATEWAY_URL_ATTRIBUTES) {\n const value = attributes[key]\n if (typeof value === 'string' && isPostHogAiGatewayUrl(value)) {\n warnIfPostHogAiGateway(value)\n return\n }\n }\n}\n","import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'\nimport type { ReadableSpan } from '@opentelemetry/sdk-trace-base'\n\nimport { redactSpan } from './redact'\nimport { isAISpan } from './spans'\nimport { warnIfPostHogAiGatewayOtelAttributes } from '../gatewayWarning'\n\nconst DEFAULT_OTEL_HOST = 'https://us.i.posthog.com'\n// OpenTelemetry's ExportResultCode.SUCCESS is 0. Keep this local so loading the\n// published subpath does not require the exporter package's transitive dependencies.\nconst EXPORT_SUCCESS = 0\n\nfunction normalizeToken(value?: unknown): string {\n return typeof value === 'string' ? value.trim() : ''\n}\n\nfunction normalizeHost(value?: unknown): string {\n const normalizedValue = typeof value === 'string' ? value.trim() : ''\n return normalizedValue || DEFAULT_OTEL_HOST\n}\n\n/**\n * Options for the PostHogTraceExporter. `projectToken` is required; a blank token disables the\n * exporter as a defensive no-op. You can also optionally override the `host` URL. `host` defaults to `https://us.i.posthog.com`.\n *\n * @example\n * ```ts\n * import { PostHogTraceExporter } from '@posthog/ai/otel'\n *\n * new PostHogTraceExporter({ projectToken: 'phc_...' })\n * ```\n *\n * @example\n * ```ts\n * import { PostHogTraceExporter } from '@posthog/ai/otel'\n *\n * new PostHogTraceExporter({ projectToken: 'phc_...', host: 'https://eu.i.posthog.com' })\n * ```\n */\nexport type PostHogTraceExporterOptions = { projectToken: string; host?: string }\n\n/**\n * An OpenTelemetry `TraceExporter` that sends AI traces to PostHog's OTLP\n * ingestion endpoint. PostHog converts `gen_ai.*` spans into\n * `$ai_generation` events server-side.\n *\n * Only AI-related spans (those whose name or attribute keys start with\n * `gen_ai.`, `llm.`, `ai.`, or `traceloop.`) are exported; all other\n * spans are silently dropped.\n *\n * Use this when the API you're integrating with only accepts a\n * `TraceExporter` (e.g. Vercel's `registerOTel`) or when you need to\n * plug PostHog into an existing processor chain. Otherwise prefer\n * {@link PostHogSpanProcessor}, which is self-contained.\n *\n * `projectToken` is required; a blank token disables the exporter as a defensive no-op.\n * You can also optionally override the `host` URL.\n *\n * Exporters only see finished spans, so they can't add per-request PostHog context;\n * use {@link PostHogSpanProcessor} with `client` for that.\n *\n * @example\n * ```ts\n * import { PostHogTraceExporter } from '@posthog/ai/otel'\n * import { registerOTel } from '@vercel/otel'\n *\n * registerOTel({\n * serviceName: 'my-app',\n * traceExporter: new PostHogTraceExporter({ projectToken: 'phc_...' }),\n * })\n * ```\n */\nexport class PostHogTraceExporter extends OTLPTraceExporter {\n private readonly disabled: boolean\n\n constructor(options: PostHogTraceExporterOptions) {\n const token = normalizeToken(options.projectToken)\n const disabled = !token\n const host = token ? new URL(normalizeHost(options.host)).origin : DEFAULT_OTEL_HOST\n super({\n url: `${host}/i/v0/ai/otel`,\n headers: token\n ? {\n Authorization: `Bearer ${token}`,\n }\n : {},\n })\n\n this.disabled = disabled\n if (this.disabled) {\n console.warn('[PostHogTraceExporter] projectToken is missing or blank; the exporter will be disabled.')\n }\n }\n\n override export(spans: ReadableSpan[], resultCallback: (result: { code: number; error?: Error }) => void): void {\n if (this.disabled) {\n // Intentionally report success: missing or blank tokens disable exporting as a compatibility no-op.\n // Reporting failure would make OpenTelemetry treat every span as an export error.\n resultCallback({ code: EXPORT_SUCCESS })\n return\n }\n\n const aiSpans = spans.filter(isAISpan)\n if (aiSpans.length === 0) {\n resultCallback({ code: EXPORT_SUCCESS })\n return\n }\n for (const span of aiSpans) {\n warnIfPostHogAiGatewayOtelAttributes(span.attributes)\n }\n super.export(aiSpans.map(redactSpan), resultCallback)\n }\n}\n","import type { AttributeValue, Attributes } from '@opentelemetry/api'\nimport type { ReadableSpan } from '@opentelemetry/sdk-trace-base'\n\nimport { withAttributes } from './spans'\n\nexport type CapturedContext = {\n distinctId?: string\n sessionId?: string\n properties?: Record<string, unknown>\n}\n\n// Structural so declarations type-check against posthog-node versions that have no getContext.\nexport type PostHogContextSource = {\n getContext?(): CapturedContext | undefined\n}\n\nconst DISTINCT_ID_ATTRIBUTE = 'posthog.distinct_id'\nconst SESSION_ID_ATTRIBUTE = '$session_id'\n\n// Ingestion prefers posthog.distinct_id, so adding it would override the user a span already names.\nconst IDENTITY_ATTRIBUTES = [\n DISTINCT_ID_ATTRIBUTE,\n 'user.id',\n 'ai.telemetry.metadata.posthog_distinct_id',\n 'ai.settings.context.posthog_distinct_id',\n]\n\ntype Primitive = string | number | boolean\n\nfunction isPrimitive(value: unknown): value is Primitive {\n return (\n typeof value === 'string' || typeof value === 'boolean' || (typeof value === 'number' && Number.isFinite(value))\n )\n}\n\nfunction isHomogeneousPrimitiveArray(value: unknown): value is string[] | number[] | boolean[] {\n return Array.isArray(value) && value.every((item) => isPrimitive(item) && typeof item === typeof value[0])\n}\n\nfunction rejectNonFinite(_key: string, value: unknown): unknown {\n if (typeof value === 'number' && !Number.isFinite(value)) {\n throw new TypeError('non-finite number')\n }\n return value\n}\n\nfunction toAttributeValue(value: unknown): AttributeValue | undefined {\n if (value === undefined || value === null || value === '') {\n return undefined\n }\n if (isPrimitive(value) || isHomogeneousPrimitiveArray(value)) {\n return value\n }\n try {\n const json = JSON.stringify(value, rejectNonFinite)\n if (json === undefined || json === 'null') {\n return undefined\n }\n return json.startsWith('\"') ? String(JSON.parse(json)) || undefined : json\n } catch {\n return undefined\n }\n}\n\nfunction hasValues(context: CapturedContext): boolean {\n return Boolean(context.distinctId || context.sessionId || Object.keys(context.properties ?? {}).length > 0)\n}\n\nexport function captureContext(client: PostHogContextSource): CapturedContext | undefined {\n try {\n // posthog-node versions before getContext existed are still within the peer range.\n if (typeof client.getContext !== 'function') {\n return undefined\n }\n const context = client.getContext()\n return context && hasValues(context) ? context : undefined\n } catch {\n // Capturing is best effort and must never break the instrumented call.\n return undefined\n }\n}\n\nfunction toAttributes(context: CapturedContext, spanAttributes: Attributes): Attributes {\n const hasIdentity = IDENTITY_ATTRIBUTES.some((key) => spanAttributes[key] !== undefined)\n const values: Record<string, unknown> = {\n ...(context.distinctId && !hasIdentity ? { [DISTINCT_ID_ATTRIBUTE]: context.distinctId } : {}),\n ...(context.sessionId ? { [SESSION_ID_ATTRIBUTE]: context.sessionId } : {}),\n ...context.properties,\n }\n const attributes: Attributes = {}\n for (const [key, value] of Object.entries(values)) {\n if (hasIdentity && key === DISTINCT_ID_ATTRIBUTE) {\n continue\n }\n const attribute = toAttributeValue(value)\n if (attribute !== undefined) {\n attributes[key] = attribute\n }\n }\n return attributes\n}\n\nexport function withRequestContext(span: ReadableSpan, context: CapturedContext): ReadableSpan {\n try {\n const attributes = toAttributes(context, span.attributes)\n if (Object.keys(attributes).length === 0) {\n return span\n }\n return withAttributes(span, { ...attributes, ...span.attributes })\n } catch {\n // Conversion runs inside span.end() and must never break the instrumented call.\n return span\n }\n}\n","import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'\nimport type { Context } from '@opentelemetry/api'\nimport { BatchSpanProcessor, type SpanProcessor, type ReadableSpan, type Span } from '@opentelemetry/sdk-trace-base'\n\nimport { redactSpan } from './redact'\nimport { captureContext, withRequestContext, type CapturedContext, type PostHogContextSource } from './requestContext'\nimport { isAISpan } from './spans'\nimport { warnIfPostHogAiGatewayOtelAttributes } from '../gatewayWarning'\n\nconst DEFAULT_OTEL_HOST = 'https://us.i.posthog.com'\n\nfunction normalizeToken(value?: unknown): string {\n return typeof value === 'string' ? value.trim() : ''\n}\n\nfunction normalizeHost(value?: unknown): string {\n const normalizedValue = typeof value === 'string' ? value.trim() : ''\n return normalizedValue || DEFAULT_OTEL_HOST\n}\n\nexport interface PostHogSpanProcessorOptions {\n /**\n * Your PostHog project token (the `phc_...` key). Required; a blank token disables the\n * processor as a defensive no-op.\n */\n projectToken: string\n\n /**\n * PostHog host URL. Defaults to `https://us.i.posthog.com`.\n */\n host?: string\n\n /**\n * Your posthog-node client. When set, each AI span this processor exports gets the distinct ID,\n * session ID (as `$session_id`) and properties from the context entered with `client.withContext()` or\n * `client.enterContext()`. Other processors on the provider don't see them.\n * Attributes already on the span win over properties, which win over the context's IDs.\n * The context is captured when a span starts, but its properties are read when the span ends,\n * so mutating a context object while a span is open shows up in the export.\n * Must be the same posthog-node instance you call `withContext` or `enterContext` on.\n */\n client?: PostHogContextSource\n\n /**\n * @internal Injected processor for testing — bypasses exporter creation.\n */\n _spanProcessor?: SpanProcessor\n}\n\nclass NoopSpanProcessor implements SpanProcessor {\n onStart(_span: Span, _parentContext: Context): void {\n return\n }\n onEnd(_span: ReadableSpan): void {\n return\n }\n shutdown(): Promise<void> {\n return Promise.resolve()\n }\n forceFlush(): Promise<void> {\n return Promise.resolve()\n }\n}\n\n/**\n * An OpenTelemetry `SpanProcessor` that sends AI traces to PostHog.\n *\n * `projectToken` is required; a blank token disables the processor as a defensive no-op.\n *\n * Internally batches spans and exports them to PostHog's OTLP ingestion\n * endpoint. Only AI-related spans (those whose name or attribute keys\n * start with `gen_ai.`, `llm.`, `ai.`, or `traceloop.`) are exported;\n * all other spans are silently dropped.\n *\n * Request-scoped and serverless runtimes should keep a reference to this\n * processor and await {@link PostHogSpanProcessor.forceFlush} before the request\n * lifecycle ends. This waits for queued exports without sending one request per span.\n *\n * This is the recommended integration point when your setup accepts a\n * `SpanProcessor`. If you need a `TraceExporter` instead (e.g. for\n * Vercel's `registerOTel`), use {@link PostHogTraceExporter}.\n *\n * @example\n * ```ts\n * import { PostHogSpanProcessor } from '@posthog/ai/otel'\n * import { NodeSDK } from '@opentelemetry/sdk-node'\n *\n * const processor = new PostHogSpanProcessor({ projectToken: 'phc_...' })\n * const sdk = new NodeSDK({ spanProcessors: [processor] })\n * sdk.start()\n *\n * // In request-scoped runtimes, wait for queued exports before returning.\n * await processor.forceFlush()\n * ```\n *\n * @example\n * ```ts\n * // Per request: pass your posthog-node client, then wrap the work in a context.\n * const processor = new PostHogSpanProcessor({ projectToken: 'phc_...', client: posthog })\n * await posthog.withContext({ distinctId: user.id, properties: { plan: 'pro' } }, () => handleRequest())\n * ```\n */\nexport class PostHogSpanProcessor implements SpanProcessor {\n private readonly inner: SpanProcessor\n private readonly client?: PostHogContextSource\n private readonly capturedContexts = new WeakMap<ReadableSpan, CapturedContext>()\n\n constructor(options: PostHogSpanProcessorOptions) {\n const token = normalizeToken(options.projectToken)\n if (!token) {\n console.warn('[PostHogSpanProcessor] projectToken is missing or blank; the processor will be disabled.')\n this.inner = new NoopSpanProcessor()\n return\n }\n this.client = options.client\n\n if (options._spanProcessor) {\n this.inner = options._spanProcessor\n } else {\n const host = new URL(normalizeHost(options.host)).origin\n const exporter = new OTLPTraceExporter({\n url: `${host}/i/v0/ai/otel`,\n headers: {\n Authorization: `Bearer ${token}`,\n },\n })\n this.inner = new BatchSpanProcessor(exporter)\n }\n }\n\n onStart(span: Span, parentContext: Context): void {\n const captured = this.client && captureContext(this.client)\n if (captured) {\n this.capturedContexts.set(span, captured)\n }\n // Forwarded unconditionally — filtering happens in onEnd. We can't filter\n // here because the span hasn't finished yet and may not have AI attributes\n // set. BatchSpanProcessor.onStart is a no-op so this is safe.\n this.inner.onStart(span, parentContext)\n }\n\n onEnd(span: ReadableSpan): void {\n const captured = this.capturedContexts.get(span)\n this.capturedContexts.delete(span)\n if (isAISpan(span)) {\n warnIfPostHogAiGatewayOtelAttributes(span.attributes)\n this.inner.onEnd(redactSpan(captured ? withRequestContext(span, captured) : span))\n }\n }\n\n shutdown(): Promise<void> {\n return this.inner.shutdown()\n }\n\n forceFlush(): Promise<void> {\n return this.inner.forceFlush()\n }\n}\n"],"mappings":";;;AAAA,MAAM,qBAAqB;AAC3B,MAAM,qBAAqB;AAI3B,IAAa,mBAAb,MAA8B;CAC5B,UAAU,OAAe,WAAsC;EAC7D,MAAM,UAAU,mBAAmB,KAAK,KAAK;EAC7C,IAAI,SAAS,OAAO;GAAE,MAAM;GAAY,WAAW,QAAQ;EAAG;EAE9D,IAAI,MAAM,SAAS,WAAW,OAAO,EAAE,MAAM,OAAO;EAEpD,MAAM,mBAAmB,MAAM,MAAM,GAAG,SAAS;EACjD,IAAI,mBAAmB,KAAK,gBAAgB,GAC1C,OAAO,EAAE,MAAM,MAAM;OAErB,OAAO,EAAE,MAAM,OAAO;CAE1B;AACF;;;ACnBA,MAAM,iBAAiB;CAAC;CAAa;CAAc;CAAY;AAAW;AAE1E,MAAM,sCAA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,MAAM,WAAW,KAAK,iBAAiB,IAAI,IAAI;EAC/C,KAAK,iBAAiB,OAAO,IAAI;EACjC,IAAI,SAAS,IAAI,GAAG;GAClB,qCAAqC,KAAK,UAAU;GACpD,KAAK,MAAM,MAAM,WAAW,WAAW,mBAAmB,MAAM,QAAQ,IAAI,IAAI,CAAC;EACnF;CACF;CAEA,WAA0B;EACxB,OAAO,KAAK,MAAM,SAAS;CAC7B;CAEA,aAA4B;EAC1B,OAAO,KAAK,MAAM,WAAW;CAC/B;AACF"}
1
+ {"version":3,"file":"index.mjs","names":["DEFAULT_OTEL_HOST","normalizeToken","normalizeHost"],"sources":["../../src/sanitization/base64_recognizer.ts","../../src/sanitization/media_type_context.ts","../../src/sanitization/binary_content_redactor.ts","../../src/otel/spans.ts","../../src/otel/redact.ts","../../src/gatewayWarning.ts","../../src/otel/exporter.ts","../../src/otel/requestContext.ts","../../src/otel/processor.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'])\nconst TYPED_BINARY_DATA_TYPES = new Set(['image', 'audio', 'video', 'document'])\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 private readonly explicitMediaType?: string\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.explicitMediaType) return this.explicitMediaType\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 hasDefiniteBinaryData(): boolean {\n if (\n this.parent &&\n this.key === 'data' &&\n typeof this.parent.type === 'string' &&\n TYPED_BINARY_DATA_TYPES.has(this.parent.type)\n )\n return true\n if (!this.explicitMediaType && (!this.parent || !this.key || !STRONG_CONTEXT_KEYS.has(this.key))) return false\n const mediaType = this.inferFromSiblingMime()\n return mediaType !== undefined && !mediaType.toLowerCase().startsWith('text/')\n }\n\n signalsBinary(): boolean {\n if (this.explicitMediaType) return true\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, mediaType?: string): T\n redact(value: unknown, mediaType?: string): unknown {\n this.visited = new WeakSet()\n return this.walk(value, mediaType ? new MediaTypeContext(undefined, undefined, mediaType) : MediaTypeContext.EMPTY)\n }\n\n /** Redact one leaf while a caller owns bounded, type-preserving traversal. @internal */\n redactLeaf(value: string | Uint8Array, context: MediaTypeContext): string {\n return typeof value === 'string' ? this.redactString(value, context) : this.placeholderFor(context.inferMediaType())\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.redactLeaf(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.redactLeaf(value, ctx)\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 Object.defineProperty(out, k, {\n value: this.walk(obj[k], new MediaTypeContext(obj, k)),\n enumerable: true,\n writable: true,\n configurable: true,\n })\n }\n return out\n }\n\n private redactString(value: string, ctx: MediaTypeContext): string {\n const hasDefiniteBinaryData = ctx.hasDefiniteBinaryData()\n const recognitionValue = hasDefiniteBinaryData ? value.replace(/[\\r\\n]/g, '') : value\n const minLength = hasDefiniteBinaryData\n ? Math.min(recognitionValue.length, STRONG_CONTEXT_MIN_LENGTH)\n : ctx.signalsBinary()\n ? STRONG_CONTEXT_MIN_LENGTH\n : WEAK_CONTEXT_MIN_LENGTH\n const recognition = this.recognizer.recognize(recognitionValue, 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","import type { Attributes } from '@opentelemetry/api'\nimport type { ReadableSpan, TimedEvent } from '@opentelemetry/sdk-trace-base'\n\nconst AI_SPAN_PREFIXES = ['gen_ai.', 'llm.', 'ai.', 'traceloop.'] as const\n\n/**\n * Returns `true` when the span is AI-related — its name or any attribute\n * key starts with `gen_ai.`, `llm.`, `ai.`, or `traceloop.`.\n */\nexport function isAISpan(span: ReadableSpan): boolean {\n if (AI_SPAN_PREFIXES.some((prefix) => span.name.startsWith(prefix))) {\n return true\n }\n const attributes = span.attributes\n if (attributes) {\n return Object.keys(attributes).some((key) => AI_SPAN_PREFIXES.some((prefix) => key.startsWith(prefix)))\n }\n return false\n}\n\n/** Copies rather than mutates: the span is shared across every registered processor/exporter. */\nexport function withAttributes(span: ReadableSpan, attributes: Attributes, events?: TimedEvent[]): ReadableSpan {\n return Object.create(span, {\n attributes: { value: attributes, enumerable: true, configurable: true },\n ...(events ? { events: { value: events, enumerable: true, configurable: true } } : {}),\n })\n}\n","import type { Attributes, AttributeValue } from '@opentelemetry/api'\nimport type { ReadableSpan, TimedEvent } from '@opentelemetry/sdk-trace-base'\n\nimport { BinaryContentRedactor } from '../sanitization/binary_content_redactor'\nimport { withAttributes } from './spans'\n\n// Deliberately always redacts here — the OTLP export path has no per-client passthrough gate.\nconst redactor = new BinaryContentRedactor()\n\nexport function redactSpan(span: ReadableSpan): ReadableSpan {\n const attributes = span.attributes ? redactAttributes(span.attributes) : span.attributes\n const events = span.events ? redactEvents(span.events) : span.events\n if (attributes === span.attributes && events === span.events) {\n return span\n }\n return withAttributes(span, attributes, events)\n}\n\nfunction redactAttributes(attributes: Attributes): Attributes {\n let changed = false\n const out: Attributes = {}\n for (const key of Object.keys(attributes)) {\n const value = attributes[key]\n const redacted = value === undefined ? value : redactAttributeValue(value)\n if (redacted !== value) {\n changed = true\n }\n out[key] = redacted\n }\n return changed ? out : attributes\n}\n\nfunction redactEvents(events: TimedEvent[]): TimedEvent[] {\n let changed = false\n const out = events.map((event) => {\n if (!event.attributes) {\n return event\n }\n const attributes = redactAttributes(event.attributes)\n if (attributes === event.attributes) {\n return event\n }\n changed = true\n return { ...event, attributes }\n })\n return changed ? out : events\n}\n\nfunction redactAttributeValue(value: AttributeValue): AttributeValue {\n if (typeof value === 'string') {\n return redactString(value)\n }\n if (isStringArray(value)) {\n let changed = false\n const out = value.map((item) => {\n if (typeof item !== 'string') {\n return item\n }\n const redacted = redactString(item)\n if (redacted !== item) {\n changed = true\n }\n return redacted\n })\n return changed ? out : value\n }\n return value\n}\n\nfunction isStringArray(value: AttributeValue): value is Array<string | null | undefined> {\n return Array.isArray(value) && value.every((item) => typeof item !== 'number' && typeof item !== 'boolean')\n}\n\nfunction redactString(value: string): string {\n const trimmed = value.trimStart()\n if (trimmed.startsWith('{') || trimmed.startsWith('[')) {\n const redacted = redactJson(value)\n if (redacted !== undefined) {\n return redacted\n }\n }\n return redactor.redact(value)\n}\n\nfunction redactJson(value: string): string | undefined {\n let parsed: unknown\n try {\n parsed = JSON.parse(value)\n } catch {\n return undefined\n }\n if (parsed === null || typeof parsed !== 'object') {\n return undefined\n }\n const redactedStr = JSON.stringify(redactor.redact(parsed))\n // Preserve the original string (and its formatting) when nothing was redacted.\n return redactedStr === JSON.stringify(parsed) ? value : redactedStr\n}\n","// Warn when a wrapper's base_url points at the PostHog AI Gateway: the gateway\n// emits its own $ai_generation, so each call would be captured (and, for billable\n// products, billed) twice. We only warn — the wrapper's event carries data the\n// gateway never sees (groups, custom properties, trace hierarchy).\n\n// Keep in sync with the gateway's deployed hosts (see services/llm-gateway in the\n// main repo). gateway.us.posthog.com is live today; the rest are listed ahead of\n// any traffic moving to them.\nexport const POSTHOG_AI_GATEWAY_HOSTS: readonly string[] = [\n 'gateway.posthog.com',\n 'gateway.us.posthog.com',\n 'gateway.eu.posthog.com',\n 'ai-gateway.us.posthog.com',\n 'ai-gateway.eu.posthog.com',\n]\n\n// Swap for the dedicated AI Gateway page once it ships.\nconst GATEWAY_DOCS_URL = 'https://posthog.com/docs/ai-observability'\n\nconst extractHost = (baseURL: string): string | undefined => {\n try {\n // Tolerate bare hosts that omit a scheme, e.g. \"gateway.us.posthog.com/v1\".\n const hasScheme = /^[a-z][a-z0-9+.-]*:\\/\\//i.test(baseURL)\n return new URL(hasScheme ? baseURL : `https://${baseURL}`).hostname.toLowerCase()\n } catch {\n return undefined\n }\n}\n\nexport const isPostHogAiGatewayUrl = (baseURL: string | undefined | null): boolean => {\n if (!baseURL) {\n return false\n }\n const host = extractHost(baseURL)\n return host !== undefined && POSTHOG_AI_GATEWAY_HOSTS.includes(host)\n}\n\n// Warns on every gateway call by design: the misconfiguration is impossible to\n// miss that way, and a doubled bill is worse than noisy logs.\nexport const warnIfPostHogAiGateway = (baseURL: string | undefined | null): void => {\n if (!isPostHogAiGatewayUrl(baseURL)) {\n return\n }\n console.warn(\n '[PostHog] The PostHog AI wrapper is pointed at the PostHog AI Gateway. ' +\n 'Both capture $ai_generation, so every call is double-counted and double-billed. ' +\n `Use one or the other — see ${GATEWAY_DOCS_URL}.`\n )\n}\n\n// OTel spans don't pass through captureAiGeneration, so detect the gateway from\n// the span's host/URL attributes instead. These follow the GenAI / HTTP semantic\n// conventions: `server.address` is a bare host, `url.full` a full URL, both of\n// which isPostHogAiGatewayUrl accepts.\nconst OTEL_GATEWAY_URL_ATTRIBUTES = ['server.address', 'url.full'] as const\n\nexport const warnIfPostHogAiGatewayOtelAttributes = (attributes: Record<string, unknown> | undefined): void => {\n if (!attributes) {\n return\n }\n for (const key of OTEL_GATEWAY_URL_ATTRIBUTES) {\n const value = attributes[key]\n if (typeof value === 'string' && isPostHogAiGatewayUrl(value)) {\n warnIfPostHogAiGateway(value)\n return\n }\n }\n}\n","import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'\nimport type { ReadableSpan } from '@opentelemetry/sdk-trace-base'\n\nimport { redactSpan } from './redact'\nimport { isAISpan } from './spans'\nimport { warnIfPostHogAiGatewayOtelAttributes } from '../gatewayWarning'\n\nconst DEFAULT_OTEL_HOST = 'https://us.i.posthog.com'\n// OpenTelemetry's ExportResultCode.SUCCESS is 0. Keep this local so loading the\n// published subpath does not require the exporter package's transitive dependencies.\nconst EXPORT_SUCCESS = 0\n\nfunction normalizeToken(value?: unknown): string {\n return typeof value === 'string' ? value.trim() : ''\n}\n\nfunction normalizeHost(value?: unknown): string {\n const normalizedValue = typeof value === 'string' ? value.trim() : ''\n return normalizedValue || DEFAULT_OTEL_HOST\n}\n\n/**\n * Options for the PostHogTraceExporter. `projectToken` is required; a blank token disables the\n * exporter as a defensive no-op. You can also optionally override the `host` URL. `host` defaults to `https://us.i.posthog.com`.\n *\n * @example\n * ```ts\n * import { PostHogTraceExporter } from '@posthog/ai/otel'\n *\n * new PostHogTraceExporter({ projectToken: 'phc_...' })\n * ```\n *\n * @example\n * ```ts\n * import { PostHogTraceExporter } from '@posthog/ai/otel'\n *\n * new PostHogTraceExporter({ projectToken: 'phc_...', host: 'https://eu.i.posthog.com' })\n * ```\n */\nexport type PostHogTraceExporterOptions = { projectToken: string; host?: string }\n\n/**\n * An OpenTelemetry `TraceExporter` that sends AI traces to PostHog's OTLP\n * ingestion endpoint. PostHog converts `gen_ai.*` spans into\n * `$ai_generation` events server-side.\n *\n * Only AI-related spans (those whose name or attribute keys start with\n * `gen_ai.`, `llm.`, `ai.`, or `traceloop.`) are exported; all other\n * spans are silently dropped.\n *\n * Use this when the API you're integrating with only accepts a\n * `TraceExporter` (e.g. Vercel's `registerOTel`) or when you need to\n * plug PostHog into an existing processor chain. Otherwise prefer\n * {@link PostHogSpanProcessor}, which is self-contained.\n *\n * `projectToken` is required; a blank token disables the exporter as a defensive no-op.\n * You can also optionally override the `host` URL.\n *\n * Exporters only see finished spans, so they can't add per-request PostHog context;\n * use {@link PostHogSpanProcessor} with `client` for that.\n *\n * @example\n * ```ts\n * import { PostHogTraceExporter } from '@posthog/ai/otel'\n * import { registerOTel } from '@vercel/otel'\n *\n * registerOTel({\n * serviceName: 'my-app',\n * traceExporter: new PostHogTraceExporter({ projectToken: 'phc_...' }),\n * })\n * ```\n */\nexport class PostHogTraceExporter extends OTLPTraceExporter {\n private readonly disabled: boolean\n\n constructor(options: PostHogTraceExporterOptions) {\n const token = normalizeToken(options.projectToken)\n const disabled = !token\n const host = token ? new URL(normalizeHost(options.host)).origin : DEFAULT_OTEL_HOST\n super({\n url: `${host}/i/v0/ai/otel`,\n headers: token\n ? {\n Authorization: `Bearer ${token}`,\n }\n : {},\n })\n\n this.disabled = disabled\n if (this.disabled) {\n console.warn('[PostHogTraceExporter] projectToken is missing or blank; the exporter will be disabled.')\n }\n }\n\n override export(spans: ReadableSpan[], resultCallback: (result: { code: number; error?: Error }) => void): void {\n if (this.disabled) {\n // Intentionally report success: missing or blank tokens disable exporting as a compatibility no-op.\n // Reporting failure would make OpenTelemetry treat every span as an export error.\n resultCallback({ code: EXPORT_SUCCESS })\n return\n }\n\n const aiSpans = spans.filter(isAISpan)\n if (aiSpans.length === 0) {\n resultCallback({ code: EXPORT_SUCCESS })\n return\n }\n for (const span of aiSpans) {\n warnIfPostHogAiGatewayOtelAttributes(span.attributes)\n }\n super.export(aiSpans.map(redactSpan), resultCallback)\n }\n}\n","import type { AttributeValue, Attributes } from '@opentelemetry/api'\nimport type { ReadableSpan } from '@opentelemetry/sdk-trace-base'\n\nimport { withAttributes } from './spans'\n\nexport type CapturedContext = {\n distinctId?: string\n sessionId?: string\n properties?: Record<string, unknown>\n}\n\n// Structural so declarations type-check against posthog-node versions that have no getContext.\nexport type PostHogContextSource = {\n getContext?(): CapturedContext | undefined\n}\n\nconst DISTINCT_ID_ATTRIBUTE = 'posthog.distinct_id'\nconst SESSION_ID_ATTRIBUTE = '$session_id'\n\n// Ingestion prefers posthog.distinct_id, so adding it would override the user a span already names.\nconst IDENTITY_ATTRIBUTES = [\n DISTINCT_ID_ATTRIBUTE,\n 'user.id',\n 'ai.telemetry.metadata.posthog_distinct_id',\n 'ai.settings.context.posthog_distinct_id',\n]\n\ntype Primitive = string | number | boolean\n\nfunction isPrimitive(value: unknown): value is Primitive {\n return (\n typeof value === 'string' || typeof value === 'boolean' || (typeof value === 'number' && Number.isFinite(value))\n )\n}\n\nfunction isHomogeneousPrimitiveArray(value: unknown): value is string[] | number[] | boolean[] {\n return Array.isArray(value) && value.every((item) => isPrimitive(item) && typeof item === typeof value[0])\n}\n\nfunction rejectNonFinite(_key: string, value: unknown): unknown {\n if (typeof value === 'number' && !Number.isFinite(value)) {\n throw new TypeError('non-finite number')\n }\n return value\n}\n\nfunction toAttributeValue(value: unknown): AttributeValue | undefined {\n if (value === undefined || value === null || value === '') {\n return undefined\n }\n if (isPrimitive(value) || isHomogeneousPrimitiveArray(value)) {\n return value\n }\n try {\n const json = JSON.stringify(value, rejectNonFinite)\n if (json === undefined || json === 'null') {\n return undefined\n }\n return json.startsWith('\"') ? String(JSON.parse(json)) || undefined : json\n } catch {\n return undefined\n }\n}\n\nfunction hasValues(context: CapturedContext): boolean {\n return Boolean(context.distinctId || context.sessionId || Object.keys(context.properties ?? {}).length > 0)\n}\n\nexport function captureContext(client: PostHogContextSource): CapturedContext | undefined {\n try {\n // posthog-node versions before getContext existed are still within the peer range.\n if (typeof client.getContext !== 'function') {\n return undefined\n }\n const context = client.getContext()\n return context && hasValues(context) ? context : undefined\n } catch {\n // Capturing is best effort and must never break the instrumented call.\n return undefined\n }\n}\n\nfunction toAttributes(context: CapturedContext, spanAttributes: Attributes): Attributes {\n const hasIdentity = IDENTITY_ATTRIBUTES.some((key) => spanAttributes[key] !== undefined)\n const values: Record<string, unknown> = {\n ...(context.distinctId && !hasIdentity ? { [DISTINCT_ID_ATTRIBUTE]: context.distinctId } : {}),\n ...(context.sessionId ? { [SESSION_ID_ATTRIBUTE]: context.sessionId } : {}),\n ...context.properties,\n }\n const attributes: Attributes = {}\n for (const [key, value] of Object.entries(values)) {\n if (hasIdentity && key === DISTINCT_ID_ATTRIBUTE) {\n continue\n }\n const attribute = toAttributeValue(value)\n if (attribute !== undefined) {\n attributes[key] = attribute\n }\n }\n return attributes\n}\n\nexport function withRequestContext(span: ReadableSpan, context: CapturedContext): ReadableSpan {\n try {\n const attributes = toAttributes(context, span.attributes)\n if (Object.keys(attributes).length === 0) {\n return span\n }\n return withAttributes(span, { ...attributes, ...span.attributes })\n } catch {\n // Conversion runs inside span.end() and must never break the instrumented call.\n return span\n }\n}\n","import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'\nimport type { Context } from '@opentelemetry/api'\nimport { BatchSpanProcessor, type SpanProcessor, type ReadableSpan, type Span } from '@opentelemetry/sdk-trace-base'\n\nimport { redactSpan } from './redact'\nimport { captureContext, withRequestContext, type CapturedContext, type PostHogContextSource } from './requestContext'\nimport { isAISpan } from './spans'\nimport { warnIfPostHogAiGatewayOtelAttributes } from '../gatewayWarning'\n\nconst DEFAULT_OTEL_HOST = 'https://us.i.posthog.com'\n\nfunction normalizeToken(value?: unknown): string {\n return typeof value === 'string' ? value.trim() : ''\n}\n\nfunction normalizeHost(value?: unknown): string {\n const normalizedValue = typeof value === 'string' ? value.trim() : ''\n return normalizedValue || DEFAULT_OTEL_HOST\n}\n\nexport interface PostHogSpanProcessorOptions {\n /**\n * Your PostHog project token (the `phc_...` key). Required; a blank token disables the\n * processor as a defensive no-op.\n */\n projectToken: string\n\n /**\n * PostHog host URL. Defaults to `https://us.i.posthog.com`.\n */\n host?: string\n\n /**\n * Your posthog-node client. When set, each AI span this processor exports gets the distinct ID,\n * session ID (as `$session_id`) and properties from the context entered with `client.withContext()` or\n * `client.enterContext()`. Other processors on the provider don't see them.\n * Attributes already on the span win over properties, which win over the context's IDs.\n * The context is captured when a span starts, but its properties are read when the span ends,\n * so mutating a context object while a span is open shows up in the export.\n * Must be the same posthog-node instance you call `withContext` or `enterContext` on.\n */\n client?: PostHogContextSource\n\n /**\n * @internal Injected processor for testing — bypasses exporter creation.\n */\n _spanProcessor?: SpanProcessor\n}\n\nclass NoopSpanProcessor implements SpanProcessor {\n onStart(_span: Span, _parentContext: Context): void {\n return\n }\n onEnd(_span: ReadableSpan): void {\n return\n }\n shutdown(): Promise<void> {\n return Promise.resolve()\n }\n forceFlush(): Promise<void> {\n return Promise.resolve()\n }\n}\n\n/**\n * An OpenTelemetry `SpanProcessor` that sends AI traces to PostHog.\n *\n * `projectToken` is required; a blank token disables the processor as a defensive no-op.\n *\n * Internally batches spans and exports them to PostHog's OTLP ingestion\n * endpoint. Only AI-related spans (those whose name or attribute keys\n * start with `gen_ai.`, `llm.`, `ai.`, or `traceloop.`) are exported;\n * all other spans are silently dropped.\n *\n * Request-scoped and serverless runtimes should keep a reference to this\n * processor and await {@link PostHogSpanProcessor.forceFlush} before the request\n * lifecycle ends. This waits for queued exports without sending one request per span.\n *\n * This is the recommended integration point when your setup accepts a\n * `SpanProcessor`. If you need a `TraceExporter` instead (e.g. for\n * Vercel's `registerOTel`), use {@link PostHogTraceExporter}.\n *\n * @example\n * ```ts\n * import { PostHogSpanProcessor } from '@posthog/ai/otel'\n * import { NodeSDK } from '@opentelemetry/sdk-node'\n *\n * const processor = new PostHogSpanProcessor({ projectToken: 'phc_...' })\n * const sdk = new NodeSDK({ spanProcessors: [processor] })\n * sdk.start()\n *\n * // In request-scoped runtimes, wait for queued exports before returning.\n * await processor.forceFlush()\n * ```\n *\n * @example\n * ```ts\n * // Per request: pass your posthog-node client, then wrap the work in a context.\n * const processor = new PostHogSpanProcessor({ projectToken: 'phc_...', client: posthog })\n * await posthog.withContext({ distinctId: user.id, properties: { plan: 'pro' } }, () => handleRequest())\n * ```\n */\nexport class PostHogSpanProcessor implements SpanProcessor {\n private readonly inner: SpanProcessor\n private readonly client?: PostHogContextSource\n private readonly capturedContexts = new WeakMap<ReadableSpan, CapturedContext>()\n\n constructor(options: PostHogSpanProcessorOptions) {\n const token = normalizeToken(options.projectToken)\n if (!token) {\n console.warn('[PostHogSpanProcessor] projectToken is missing or blank; the processor will be disabled.')\n this.inner = new NoopSpanProcessor()\n return\n }\n this.client = options.client\n\n if (options._spanProcessor) {\n this.inner = options._spanProcessor\n } else {\n const host = new URL(normalizeHost(options.host)).origin\n const exporter = new OTLPTraceExporter({\n url: `${host}/i/v0/ai/otel`,\n headers: {\n Authorization: `Bearer ${token}`,\n },\n })\n this.inner = new BatchSpanProcessor(exporter)\n }\n }\n\n onStart(span: Span, parentContext: Context): void {\n const captured = this.client && captureContext(this.client)\n if (captured) {\n this.capturedContexts.set(span, captured)\n }\n // Forwarded unconditionally — filtering happens in onEnd. We can't filter\n // here because the span hasn't finished yet and may not have AI attributes\n // set. BatchSpanProcessor.onStart is a no-op so this is safe.\n this.inner.onStart(span, parentContext)\n }\n\n onEnd(span: ReadableSpan): void {\n const captured = this.capturedContexts.get(span)\n this.capturedContexts.delete(span)\n if (isAISpan(span)) {\n warnIfPostHogAiGatewayOtelAttributes(span.attributes)\n this.inner.onEnd(redactSpan(captured ? withRequestContext(span, captured) : span))\n }\n }\n\n shutdown(): Promise<void> {\n return this.inner.shutdown()\n }\n\n forceFlush(): Promise<void> {\n return this.inner.forceFlush()\n }\n}\n"],"mappings":";;;AAAA,MAAM,qBAAqB;AAC3B,MAAM,qBAAqB;AAI3B,IAAa,mBAAb,MAA8B;CAC5B,UAAU,OAAe,WAAsC;EAC7D,MAAM,UAAU,mBAAmB,KAAK,KAAK;EAC7C,IAAI,SAAS,OAAO;GAAE,MAAM;GAAY,WAAW,QAAQ;EAAG;EAE9D,IAAI,MAAM,SAAS,WAAW,OAAO,EAAE,MAAM,OAAO;EAEpD,MAAM,mBAAmB,MAAM,MAAM,GAAG,SAAS;EACjD,IAAI,mBAAmB,KAAK,gBAAgB,GAC1C,OAAO,EAAE,MAAM,MAAM;OAErB,OAAO,EAAE,MAAM,OAAO;CAE1B;AACF;;;ACnBA,MAAM,iBAAiB;CAAC;CAAa;CAAc;CAAY;AAAW;AAE1E,MAAM,sCAAsB,IAAI,IAAI;CAClC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC;AAED,MAAM,uCAAuB,IAAI,IAAI;CACnC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC;AAED,MAAM,oCAAoB,IAAI,IAAI;CAAC;CAAQ;CAAc;CAAY;CAAS;AAAW,CAAC;AAC1F,MAAM,0CAA0B,IAAI,IAAI;CAAC;CAAS;CAAS;CAAS;AAAU,CAAC;AAE/E,MAAM,sCAAsB,IAAI,IAAI;CAAC;CAAO;CAAO;CAAO;CAAQ;CAAO;CAAO;AAAM,CAAC;AAEvF,IAAa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AAS;CAC7B;CAEA,aAA4B;EAC1B,OAAO,KAAK,MAAM,WAAW;CAC/B;AACF"}
@@ -92,6 +92,12 @@ const FILE_FAMILY_TYPES = /* @__PURE__ */ new Set([
92
92
  "media",
93
93
  "file-data"
94
94
  ]);
95
+ const TYPED_BINARY_DATA_TYPES = /* @__PURE__ */ new Set([
96
+ "image",
97
+ "audio",
98
+ "video",
99
+ "document"
100
+ ]);
95
101
  const KNOWN_AUDIO_FORMATS = /* @__PURE__ */ new Set([
96
102
  "wav",
97
103
  "mp3",
@@ -143,7 +149,8 @@ var MediaTypeContext = class MediaTypeContext {
143
149
  if (key.includes("image")) return "image";
144
150
  if (key.includes("file") || key.includes("document")) return "application/octet-stream";
145
151
  }
146
- hasExplicitBinaryMediaType() {
152
+ hasDefiniteBinaryData() {
153
+ if (this.parent && this.key === "data" && typeof this.parent.type === "string" && TYPED_BINARY_DATA_TYPES.has(this.parent.type)) return true;
147
154
  if (!this.explicitMediaType && (!this.parent || !this.key || !STRONG_CONTEXT_KEYS.has(this.key))) return false;
148
155
  const mediaType = this.inferFromSiblingMime();
149
156
  return mediaType !== void 0 && !mediaType.toLowerCase().startsWith("text/");
@@ -197,9 +204,9 @@ var BinaryContentRedactor = class {
197
204
  return out;
198
205
  }
199
206
  redactString(value, ctx) {
200
- const hasExplicitBinaryMediaType = ctx.hasExplicitBinaryMediaType();
201
- const recognitionValue = hasExplicitBinaryMediaType ? value.replace(/[\r\n]/g, "") : value;
202
- const minLength = hasExplicitBinaryMediaType ? Math.min(recognitionValue.length, STRONG_CONTEXT_MIN_LENGTH) : ctx.signalsBinary() ? STRONG_CONTEXT_MIN_LENGTH : WEAK_CONTEXT_MIN_LENGTH;
207
+ const hasDefiniteBinaryData = ctx.hasDefiniteBinaryData();
208
+ const recognitionValue = hasDefiniteBinaryData ? value.replace(/[\r\n]/g, "") : value;
209
+ const minLength = hasDefiniteBinaryData ? Math.min(recognitionValue.length, STRONG_CONTEXT_MIN_LENGTH) : ctx.signalsBinary() ? STRONG_CONTEXT_MIN_LENGTH : WEAK_CONTEXT_MIN_LENGTH;
203
210
  const recognition = this.recognizer.recognize(recognitionValue, minLength);
204
211
  switch (recognition.kind) {
205
212
  case "data-url": return this.placeholderFor(recognition.mediaType);
@@ -398,7 +405,7 @@ function sanitizeValues(obj) {
398
405
  }
399
406
  //#endregion
400
407
  //#region package.json
401
- var version = "8.14.4";
408
+ var version = "8.15.0";
402
409
  //#endregion
403
410
  //#region src/serializeError.ts
404
411
  const DEFAULT_MAX_DEPTH = 3;