Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

4.8 KB
Raw Blame
import { Resource } from '@opentelemetry/resources';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { metrics } from '@opentelemetry/api';
import type { Guard } from './guard/base';

export type InstrumentationType = 'openai' | 'anthropic' | 'cohere' | 'groq' | 'mistral' | 'google-ai' | 'vertexai' | 'together' | 'typesafe' | 'ollama' | 'vllm' | 'vercel-ai' | 'langchain' | 'langgraph' | 'pinecone' | 'bedrock' | 'llamaindex' | 'huggingface' | 'replicate' | 'chroma' | 'qdrant' | 'milvus' | 'astra' | 'azure-ai-inference' | 'openai-agents' | 'strands' | 'google-adk' | 'claude-agent-sdk' | 'cursor-sdk' | 'ai21' | 'gradient' | 'browser-use' | 'mcp' | 'mem0' | 'elevenlabs' | 'assemblyai' | 'transformers' | 'pg' | 'firecrawl' | 'letta';

export type OpenlitInstrumentations = Partial<Record<InstrumentationType, any>>;

export type PricingObject = Record<string, Record<string, unknown>>;

/**
 * Internal config interface used by OpenlitConfig.
 */
export interface OpenlitConfigInterface {
  environment?: string;
  applicationName?: string;
  pricingInfo?: PricingObject;
  tracer: NodeTracerProvider;
  otlpEndpoint?: string;
  otlpHeaders?: Record<string, unknown>;
  disableBatch?: boolean;
  captureMessageContent?: boolean;
  pricingJson?: string | PricingObject;
  disableMetrics?: boolean;
  disableEvents?: boolean;
  maxContentLength?: number | null;
  customSpanAttributes?: Record<string, string> | null;
  openlitApiKey?: string;
  openlitUrl?: string;
}

/**
 * Public init() options.
 *
 * Names match the Python SDK's init() parameters in camelCase form:
 *   Python: application_name     → JS: applicationName
 *   Python: otlp_endpoint        → JS: otlpEndpoint
 *   Python: otlp_headers         → JS: otlpHeaders
 *   Python: disable_batch        → JS: disableBatch
 *   Python: capture_message_content → JS: captureMessageContent
 *   Python: disabled_instrumentors  → JS: disabledInstrumentors
 *   Python: disable_metrics      → JS: disableMetrics
 *   Python: disable_events       → JS: disableEvents
 *   Python: pricing_json         → JS: pricingJson
 *   Python: max_content_length   → JS: maxContentLength
 *   Python: custom_span_attributes → JS: customSpanAttributes
 *   Python: collect_gpu_stats     → JS: collectGpuStats
 */
export type OpenlitOptions = {
  environment?: string;
  applicationName?: string;
  tracer?: NodeTracerProvider;
  otlpEndpoint?: string;
  otlpHeaders?: Record<string, unknown>;
  disableBatch?: boolean;
  captureMessageContent?: boolean;
  disabledInstrumentors?: string[];
  instrumentations?: OpenlitInstrumentations;
  disableMetrics?: boolean;
  disableEvents?: boolean;
  pricingJson?: string | PricingObject;
  maxContentLength?: number | null;
  customSpanAttributes?: Record<string, string> | null;
  openlitApiKey?: string;
  openlitUrl?: string;
  guards?: Guard[];
  guardFailOpen?: boolean;
  collectGpuStats?: boolean;
};

/**
 * Resolved options used internally after merging args, env vars, and defaults.
 */
export interface ResolvedOptions {
  environment: string;
  applicationName: string;
  tracer?: NodeTracerProvider;
  otlpEndpoint?: string;
  otlpHeaders?: Record<string, unknown>;
  disableBatch: boolean;
  captureMessageContent: boolean;
  disabledInstrumentors?: string[];
  instrumentations?: OpenlitInstrumentations;
  disableMetrics: boolean;
  disableEvents: boolean;
  pricingJson?: string | PricingObject;
  maxContentLength?: number | null;
  customSpanAttributes?: Record<string, string> | null;
  openlitApiKey?: string;
  openlitUrl?: string;
  guards?: Guard[];
  guardFailOpen: boolean;
  collectGpuStats: boolean;
}

export type SetupTracerOptions = ResolvedOptions & {
  resource: Resource;
};

export type MeterType = ReturnType<typeof metrics.getMeter>;

export type SetupMetricsOptions = SetupTracerOptions & {
  meter?: MeterType;
  exportIntervalMillis?: number;
};

export type SetupEventsOptions = SetupTracerOptions;

export interface BaseOpenlitOptions {
  url?: string;
  apiKey?: string;
}

export interface PromptHubOptions extends BaseOpenlitOptions {
  name?: string;
  version?: string;
  shouldCompile?: boolean;
  variables?: Record<string, any>;
  promptId?: string;
  metaProperties?: Record<string, any>;
}

export interface VaultOptions extends BaseOpenlitOptions {
  key?: string;
  tags?: string[];
  shouldSetEnv?: boolean;
}

export type RuleEntityType = 'context' | 'prompt' | 'evaluation';

export interface RuleEngineOptions extends BaseOpenlitOptions {
  entityType: RuleEntityType;
  fields: Record<string, string | number | boolean>;
  includeEntityData?: boolean;
  entityInputs?: Record<string, any>;
}

export interface RuleEngineResult {
  matchingRuleIds: string[];
  entities: Array<{ rule_id: string; entity_type: string; entity_id: string }>;
  entity_data?: Record<string, any>;
}