/** * Shared type definitions for Tavus Logger * These types are used across both Node.js and browser logger implementations */ /** * Base type for HTTP request body data * Use unknown for truly dynamic data that needs type guards */ export type HttpBody = string | Record | unknown[] | null | undefined; /** * HTTP request data for logging */ export interface LoggerRequest { method: string; url: string; headers: Record; body?: HttpBody; bodySize?: number; bodyType?: string; queryParams?: Record; fullUrl?: string; } /** * HTTP response data for logging */ export interface LoggerResponse { status: number; statusText?: string; headers: Record; body?: HttpBody; bodySize?: number; bodyType?: string; duration: number; ok?: boolean; redirected?: boolean; type?: string; } /** * Proxy request data (Node.js only) */ export interface ProxyRequest { requestId: string; timestamp: string; endpoint: string; method: string; url: string; originalUrl?: string; path?: string; query?: Record; params?: Record; headers: Record; body?: HttpBody; rawBody?: unknown; ip?: string; ips?: string[]; hostname?: string; protocol?: string; secure?: boolean; userAgent?: string; referer?: string; origin?: string; contentType?: string; contentLength?: string; accept?: string; acceptEncoding?: string; acceptLanguage?: string; connection?: string; cacheControl?: string; pragma?: string; metadata?: ProxyRequestMetadata; } /** * Metadata for proxy requests */ export interface ProxyRequestMetadata { isLocalhost: boolean; isCurl: boolean; isPostman: boolean; isInsomnia: boolean; hasOrigin: boolean; hasReferer: boolean; requestSize: number; timestamp: number; } /** * Proxy response data (Node.js only) */ export interface ProxyResponse { requestId: string; timestamp: string; statusCode: number; statusMessage: string; headers: Record; body?: HttpBody; error?: ErrorInfo; duration: number; metadata?: ProxyResponseMetadata; } /** * Metadata for proxy responses */ export interface ProxyResponseMetadata { isError: boolean; isSuccess: boolean; responseSize: number; timestamp: number; } /** * Error information structure */ export interface ErrorInfo { message: string; stack?: string; name: string; code?: string; } /** * Options for logging HTTP requests */ export interface LogRequestOptions { method: string; url: string; headers: Record; body?: HttpBody; } /** * Options for logging HTTP responses */ export interface LogResponseOptions { requestId: string; status: number; headers: Record; body?: HttpBody; response?: Response; // Browser Response object } /** * Log entry metadata */ export interface LogEntryMetadata { endpoint: string; requestType: 'conversation' | 'video' | 'auth' | 'streaming' | 'other'; hasBinaryData: boolean; binaryDataSize?: number; isVideoData?: boolean; shouldLog?: boolean; contentType?: string; contentLength?: number; userAgent?: string; referrer?: string; timestamp?: number; requestId?: string; } /** * Raw request/response data for detailed logging */ export interface RawLogData { requestHeaders?: Record; responseHeaders?: Record; requestBody?: HttpBody | SampledVideoPayload | TruncatedPayload; responseBody?: HttpBody; url?: string; method?: string; } /** * Complete log entry structure */ export interface TavusLogEntry { sessionId: string; timestamp: string; request: LoggerRequest; response: LoggerResponse; metadata?: LogEntryMetadata; error?: ErrorInfo; raw?: RawLogData; } /** * Logger configuration options */ export interface TavusLoggerConfig { enabled: boolean; logDirectory?: string; // Node.js only includeHeaders: boolean; includeBody: boolean; maxBodySize: number; sessionTimeout: number; // Enhanced logging options logProxyRequests?: boolean; // Node.js only logClientInfo?: boolean; // Node.js only logPerformance?: boolean; logErrors: boolean; logVideoData?: boolean; // Browser only logAllStatusCodes?: boolean; // Browser only sampleLargePayloads?: boolean; // Browser only maxVideoPayloadSize?: number; // Browser only logRawData?: boolean; // Browser only logQueryParams?: boolean; // Browser only logUserAgent?: boolean; // Browser only logReferrer?: boolean; // Browser only logTimestamps?: boolean; // Browser only logRequestIds?: boolean; // Browser only logContentTypes?: boolean; // Browser only logContentLengths?: boolean; // Browser only logAllHeaders?: boolean; // Browser only logStackTraces?: boolean; // Browser only logNetworkInfo?: boolean; // Browser only } /** * Session summary data */ export interface SessionSummary { sessionId: string; requestCount: number; errorCount: number; totalDuration: number; endpoints: string[]; } /** * Pending request information stored internally */ export interface PendingRequest { method: string; url: string; headers: Record; body?: HttpBody | SampledVideoPayload | TruncatedPayload; startTime: number; sessionId: string; // Enhanced metadata (browser only) queryParams?: Record; fullUrl?: string; bodySize?: number; bodyType?: string; contentType?: string; contentLength?: string; userAgent?: string; referrer?: string; timestamp?: number; requestId?: string; networkInfo?: Record; } /** * Network information for browser logging */ export interface NetworkInfo extends Record { effectiveType?: string; downlink?: number; rtt?: number; saveData?: boolean; navigationStart?: number; fetchStart?: number; domainLookupStart?: number; domainLookupEnd?: number; connectStart?: number; connectEnd?: number; requestStart?: number; responseStart?: number; responseEnd?: number; } /** * Sampled video payload structure */ export interface SampledVideoPayload extends Record { sampled: true; totalSize: number; sampleSize: number; samples: { start: string; middle: string; end: string; }; type: 'video'; } /** * Truncated payload structure */ export interface TruncatedPayload extends Record { truncated: true; size: number; preview: string; type: 'video' | 'regular'; } /** * Type guard to check if an error is an Error instance */ export function isError(error: unknown): error is Error { return error instanceof Error; } /** * Type guard to check if an error has a code property */ export function isErrorWithCode(error: unknown): error is Error & { code: string } { return isError(error) && 'code' in error && typeof (error as Error & { code?: unknown }).code === 'string'; } /** * Type guard to check if a value is a valid HTTP body */ export function isHttpBody(value: unknown): value is HttpBody { return ( value === null || value === undefined || typeof value === 'string' || Array.isArray(value) || (typeof value === 'object' && value !== null) ); } /** * Type guard to check if a value is a record */ export function isRecord(value: unknown): value is Record { return typeof value === 'object' && value !== null && !Array.isArray(value); }