Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { after } from 'next/server'; |
| 2 | import type { |
| 3 | KnowledgeDegradationReason, |
| 4 | KnowledgeFilter, |
| 5 | KnowledgeRetrievalMode, |
| 6 | } from './search'; |
| 7 | import type { KnowledgeSourceType } from './types'; |
| 8 | |
| 9 | const DEFAULT_POSTHOG_HOST = 'https://us.i.posthog.com'; |
| 10 | const POSTHOG_EVENT_NAME = 'kb_search_executed'; |
| 11 | const POSTHOG_DISTINCT_ID = 'public-kb-search'; |
| 12 | |
| 13 | export interface KnowledgeSearchAnalyticsEvent { |
| 14 | query: string; |
| 15 | filter: KnowledgeFilter; |
| 16 | retrievalMode: KnowledgeRetrievalMode | 'unavailable'; |
| 17 | resultCount: number; |
| 18 | degradationCategory: KnowledgeDegradationReason | 'all-retrievers-failed' | null; |
| 19 | strongMatch: boolean | null; |
| 20 | statusCode: number; |
| 21 | durationMs: number; |
| 22 | keywordDurationMs: number; |
| 23 | semanticDurationMs: number | null; |
| 24 | resultSourceTypes: KnowledgeSourceType[]; |
| 25 | previewOverlayApplied: boolean; |
| 26 | } |
| 27 | |
| 28 | export interface PostHogCaptureConfig { |
| 29 | apiKey?: string; |
| 30 | host?: string; |
| 31 | timeoutMs?: number; |
| 32 | } |
| 33 | |
| 34 | interface PostHogCapture { |
| 35 | url: string; |
| 36 | body: Record<string, unknown>; |
| 37 | } |
| 38 | |
| 39 | const CREDENTIAL_FIELD = [ |
| 40 | '(?:x[_-])?api[_ -]?key', |
| 41 | 'refresh[_ -]?token', |
| 42 | 'access[_ -]?token', |
| 43 | 'client[_ -]?secret', |
| 44 | 'authorization', |
| 45 | 'password', |
| 46 | 'secret', |
| 47 | 'token', |
| 48 | ].join('|'); |
| 49 | const REDACTABLE_CREDENTIAL_VALUE = |
| 50 | `(?!\\[REDACTED\\])(?:"[^"]*"|'[^']*'|[^&\\s,}\\]]+)`; |
| 51 | const CREDENTIAL_ASSIGNMENT = new RegExp( |
| 52 | `(^|[?&#\\s,{])(["']?)(${CREDENTIAL_FIELD})\\2(\\s*[:=]\\s*)` + |
| 53 | REDACTABLE_CREDENTIAL_VALUE, |
| 54 | 'gi', |
| 55 | ); |
| 56 | const AUTHORIZATION_SCHEME_ASSIGNMENT = new RegExp( |
| 57 | `(^|[?&#\\s,{])(["']?)(authorization)\\2(\\s*[:=]\\s*)` + |
| 58 | `(?:Bearer|Basic)\\s+[^&\\s,}\\]]+`, |
| 59 | 'gi', |
| 60 | ); |
| 61 | const OAUTH_QUERY_ASSIGNMENT = new RegExp( |
| 62 | `(^|[?&#])(["']?)(code|state)\\2(\\s*=\\s*)${REDACTABLE_CREDENTIAL_VALUE}`, |
| 63 | 'gi', |
| 64 | ); |
| 65 | const STRUCTURED_OAUTH_ASSIGNMENT = new RegExp( |
| 66 | `(^|[,{]\\s*)(["']?)(code|state)\\2(\\s*:\\s*)${REDACTABLE_CREDENTIAL_VALUE}`, |
| 67 | 'gi', |
| 68 | ); |
| 69 | |
| 70 | function redactStructuredOAuthAssignments(query: string): string { |
| 71 | if (!query.startsWith('{') && !query.startsWith('[')) return query; |
| 72 | return query.replace( |
| 73 | STRUCTURED_OAUTH_ASSIGNMENT, |
| 74 | (_match, prefix, quote, field, separator) => |
| 75 | `${prefix}${quote}${field}${quote}${separator}[REDACTED]`, |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | export function redactKnowledgeSearchQuery(query: string): string { |
| 80 | const redacted = query |
| 81 | .trim() |
| 82 | .slice(0, 200) |
| 83 | .replace(AUTHORIZATION_SCHEME_ASSIGNMENT, (_match, prefix, quote, field, separator) => |
| 84 | `${prefix}${quote}${field}${quote}${separator}[REDACTED]`) |
| 85 | .replace(CREDENTIAL_ASSIGNMENT, (_match, prefix, quote, field, separator) => |
| 86 | `${prefix}${quote}${field}${quote}${separator}[REDACTED]`) |
| 87 | .replace(OAUTH_QUERY_ASSIGNMENT, (_match, prefix, quote, field, separator) => |
| 88 | `${prefix}${quote}${field}${quote}${separator}[REDACTED]`) |
| 89 | .replace(/\bBearer\s+[A-Za-z0-9._~+/=-]{12,}/gi, 'Bearer [REDACTED]') |
| 90 | .replace(/\b(?:ak|ck|phc)_[A-Za-z0-9_-]{12,}\b/g, '[REDACTED]') |
| 91 | .replace(/\bsk-[A-Za-z0-9_-]{12,}\b/g, '[REDACTED]'); |
| 92 | return redactStructuredOAuthAssignments(redacted); |
| 93 | } |
| 94 | |
| 95 | export function buildKnowledgeSearchCapture( |
| 96 | event: KnowledgeSearchAnalyticsEvent, |
| 97 | config: PostHogCaptureConfig, |
| 98 | ): PostHogCapture | null { |
| 99 | const apiKey = config.apiKey?.trim(); |
| 100 | if (!apiKey) return null; |
| 101 | |
| 102 | const host = (config.host?.trim() || DEFAULT_POSTHOG_HOST).replace(/\/+$/, ''); |
| 103 | return { |
| 104 | url: `${host}/i/v0/e/`, |
| 105 | body: { |
| 106 | api_key: apiKey, |
| 107 | event: POSTHOG_EVENT_NAME, |
| 108 | properties: { |
| 109 | distinct_id: POSTHOG_DISTINCT_ID, |
| 110 | '$process_person_profile': false, |
| 111 | query: redactKnowledgeSearchQuery(event.query), |
| 112 | filter: event.filter, |
| 113 | retrieval_mode: event.retrievalMode, |
| 114 | result_count: event.resultCount, |
| 115 | degradation_category: event.degradationCategory, |
| 116 | strong_match: event.strongMatch, |
| 117 | status_code: event.statusCode, |
| 118 | duration_ms: Math.round(event.durationMs), |
| 119 | keyword_duration_ms: Math.round(event.keywordDurationMs), |
| 120 | semantic_duration_ms: event.semanticDurationMs === null |
| 121 | ? null |
| 122 | : Math.round(event.semanticDurationMs), |
| 123 | result_source_types: event.resultSourceTypes, |
| 124 | preview_overlay_applied: event.previewOverlayApplied, |
| 125 | }, |
| 126 | }, |
| 127 | }; |
| 128 | } |
| 129 | |
| 130 | export async function sendKnowledgeSearchAnalytics( |
| 131 | event: KnowledgeSearchAnalyticsEvent, |
| 132 | config: PostHogCaptureConfig, |
| 133 | ): Promise<boolean> { |
| 134 | const capture = buildKnowledgeSearchCapture(event, config); |
| 135 | if (!capture) return false; |
| 136 | |
| 137 | try { |
| 138 | const response = await fetch(capture.url, { |
| 139 | method: 'POST', |
| 140 | headers: { 'Content-Type': 'application/json' }, |
| 141 | body: JSON.stringify(capture.body), |
| 142 | signal: AbortSignal.timeout(config.timeoutMs ?? 1_500), |
| 143 | }); |
| 144 | if (!response.ok) { |
| 145 | console.warn('[kb-search]', JSON.stringify({ |
| 146 | event: 'kb_search_analytics_delivery_failed', |
| 147 | statusCode: response.status, |
| 148 | })); |
| 149 | } |
| 150 | return response.ok; |
| 151 | } catch { |
| 152 | // Analytics must never affect search availability or latency. |
| 153 | console.warn('[kb-search]', JSON.stringify({ |
| 154 | event: 'kb_search_analytics_delivery_failed', |
| 155 | statusCode: null, |
| 156 | })); |
| 157 | return false; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | export function queueKnowledgeSearchAnalytics(event: KnowledgeSearchAnalyticsEvent): void { |
| 162 | after(() => sendKnowledgeSearchAnalytics(event, { |
| 163 | apiKey: process.env.POSTHOG_PROJECT_KEY ?? process.env.NEXT_PUBLIC_POSTHOG_KEY, |
| 164 | host: process.env.POSTHOG_HOST ?? process.env.NEXT_PUBLIC_POSTHOG_HOST, |
| 165 | })); |
| 166 | } |