/** * observability-api.ts * Provides reusable utilities for wrapping API calls with Helicone logging for AI agents */ import { BasePaymentsAPI } from '../base-payments.js'; import { PaymentOptions, StartAgentRequest } from '../../common/types.js'; import { EnvironmentName } from '../../environments.js'; import { AsyncLoggerProviders, CustomProperties, HeliconePayloadConfig, HeliconeResponseConfig, ChatOpenAIConfiguration, OpenAIConfiguration } from './types.js'; /** * Wraps an async operation with Helicone logging * * @param agentName - Name of the agent for logging purposes * @param payloadConfig - Configuration for the Helicone payload * @param operation - The async operation to execute (returns internal result with extra data) * @param resultExtractor - Function to extract the user-facing result from internal result * @param usageCalculator - Function to calculate usage metrics from the internal result * @param responseIdPrefix - Prefix for the response ID * @param heliconeApiKey - The Helicone API key for logging * @param heliconeManualLoggingUrl - The Helicone manual logging endpoint URL * @param accountAddress - The account address for logging purposes * @param environmentName - The environment name for logging purposes * @param startAgentRequest - The agent request for logging purposes * @param customProperties - Custom properties to add as Helicone headers (should include agentid and sessionid) * @returns Promise that resolves to the extracted user result */ export declare function withHeliconeLogging(agentName: string, payloadConfig: HeliconePayloadConfig, operation: () => Promise, resultExtractor: (internalResult: TInternal) => TExtracted, usageCalculator: (internalResult: TInternal) => HeliconeResponseConfig['usage'], responseIdPrefix: string, heliconeApiKey: string, heliconeManualLoggingUrl: string, accountAddress: string, environmentName: EnvironmentName, startAgentRequest: StartAgentRequest, customProperties: CustomProperties): Promise; /** * Helper function to calculate usage for image operations based on pixels */ export declare function calculateImageUsage(pixels: number): HeliconeResponseConfig['usage']; /** * Helper function to calculate usage for video operations (typically 1 token) */ export declare function calculateVideoUsage(): HeliconeResponseConfig['usage']; /** * Helper function to calculate usage for song operations based on tokens/quota */ export declare function calculateSongUsage(tokens: number): HeliconeResponseConfig['usage']; /** * Helper function to calculate usage for dummy song operations */ export declare function calculateDummySongUsage(): HeliconeResponseConfig['usage']; /** * Creates a ChatOpenAI configuration with logging enabled * * Usage: const llm = new ChatOpenAI(withLangchain("gpt-4o-mini", apiKey, heliconeApiKey, heliconeBaseLoggingUrl, accountAddress, agentRequest, customProperties)); * * @param model - The OpenAI model to use (e.g., "gpt-4o-mini", "gpt-4") * @param apiKey - The OpenAI API key * @param heliconeApiKey - The Helicone API key for logging * @param heliconeBaseLoggingUrl - The Helicone base logging endpoint URL * @param accountAddress - The account address for logging purposes * @param environmentName - The environment name for logging purposes * @param agentRequest - The agent request for logging purposes * @param customProperties - Custom properties to add as Helicone headers * @returns Configuration object for ChatOpenAI constructor with logging enabled */ export declare function withLangchain(model: string, apiKey: string, heliconeApiKey: string, heliconeBaseLoggingUrl: string, accountAddress: string, environmentName: EnvironmentName, agentRequest: StartAgentRequest, customProperties: CustomProperties): ChatOpenAIConfiguration; /** * Creates an OpenAI client configuration with logging enabled * * Usage: const openai = new OpenAI(withOpenAI(apiKey, heliconeApiKey, heliconeBaseLoggingUrl, accountAddress, agentRequest, customProperties)); * * @param apiKey - The OpenAI API key * @param heliconeApiKey - The Helicone API key for logging * @param heliconeBaseLoggingUrl - The Helicone base logging endpoint URL * @param accountAddress - The account address for logging purposes * @param environmentName - The environment name for logging purposes * @param agentRequest - The agent request for logging purposes * @param customProperties - Custom properties to add as Helicone headers * @returns Configuration object for OpenAI constructor with logging enabled */ export declare function withOpenAI(apiKey: string, heliconeApiKey: string, heliconeBaseLoggingUrl: string, accountAddress: string, environmentName: EnvironmentName, agentRequest: StartAgentRequest, customProperties: CustomProperties): OpenAIConfiguration; /** * The ObservabilityAPI class provides methods to wrap API calls with Helicone logging */ export declare class ObservabilityAPI extends BasePaymentsAPI { protected readonly heliconeBaseLoggingUrl: string; protected readonly heliconeManualLoggingUrl: string; protected readonly heliconeAsyncLoggingUrl: string; constructor(options: PaymentOptions); /** * This method is used to create a singleton instance of the ObservabilityAPI class. * * @param options - The options to initialize the payments class. * @returns The instance of the ObservabilityAPI class. */ static getInstance(options: PaymentOptions): ObservabilityAPI; /** * Wraps an async operation with Helicone logging * * @param agentName - Name of the agent for logging purposes * @param payloadConfig - Configuration for the Helicone payload * @param operation - The async operation to execute (returns internal result with extra data) * @param resultExtractor - Function to extract the user-facing result from internal result * @param usageCalculator - Function to calculate usage metrics from the internal result * @param responseIdPrefix - Prefix for the response ID * @param startAgentRequest - The agent request for logging purposes * @param customProperties - Custom properties to add as Helicone headers (should include agentid and sessionid) * @returns Promise that resolves to the extracted user result */ withHeliconeLogging(agentName: string, payloadConfig: HeliconePayloadConfig, operation: () => Promise, resultExtractor: (internalResult: TInternal) => TExtracted, usageCalculator: (internalResult: TInternal) => HeliconeResponseConfig['usage'], responseIdPrefix: string, startAgentRequest: StartAgentRequest, customProperties: CustomProperties): Promise; /** * Creates a ChatOpenAI configuration with logging enabled * * Usage: const llm = new ChatOpenAI(observability.withLangchain("gpt-4o-mini", apiKey, agentRequest, customProperties)); * * @param model - The OpenAI model to use (e.g., "gpt-4o-mini", "gpt-4") * @param apiKey - The OpenAI API key * @param agentRequest - The agent request for logging purposes * @param customProperties - Custom properties to add as Helicone headers (should include agentid and sessionid) * @returns Configuration object for ChatOpenAI constructor with logging enabled */ withLangchain(model: string, apiKey: string, startAgentRequest: StartAgentRequest, customProperties: CustomProperties): ChatOpenAIConfiguration; /** * Creates an OpenAI client configuration with logging enabled * * Usage: const openai = new OpenAI(observability.withOpenAI(apiKey, heliconeApiKey, agentRequest, customProperties)); * * @param apiKey - The OpenAI API key * @param agentRequest - The agent request for logging purposes * @param customProperties - Custom properties to add as Helicone headers (should include agentid and sessionid) * @returns Configuration object for OpenAI constructor with logging enabled */ withOpenAI(apiKey: string, agentRequest: StartAgentRequest, customProperties: CustomProperties): OpenAIConfiguration; /** * Creates an async logger with Nevermined logging enabled and automatic property injection * * This method wraps the OpenTelemetry SpanProcessor to automatically add all Helicone * properties to every span. This mimics Python's Traceloop.set_association_properties() - * no wrapping of individual LLM calls needed! * * @example * ```typescript * import OpenAI from 'openai'; * * const logger = observability.withAsyncLogger({ openAI: OpenAI }, agentRequest); * logger.init(); * * // Make LLM calls normally - properties are automatically added to all spans! * const openai = new OpenAI({ apiKey }); * const result = await openai.chat.completions.create({ ... }); * ``` * * @param providers - AI SDK modules to instrument (OpenAI, Anthropic, etc.) * @param agentRequest - The agent request for logging purposes * @param customProperties - Custom properties to add as Helicone headers * @returns The async logger instance with init() method */ withAsyncLogger(providers: AsyncLoggerProviders, agentRequest: StartAgentRequest, customProperties?: CustomProperties): { init: () => void; }; /** * Helper function to calculate usage for image operations based on pixels */ calculateImageUsage(pixels: number): HeliconeResponseConfig['usage']; /** * Helper function to calculate usage for video operations (typically 1 token) */ calculateVideoUsage(): HeliconeResponseConfig['usage']; /** * Helper function to calculate usage for song operations based on tokens/quota */ calculateSongUsage(tokens: number): HeliconeResponseConfig['usage']; /** * Helper function to calculate usage for dummy song operations */ calculateDummySongUsage(): HeliconeResponseConfig['usage']; /** * Creates a standardized Helicone payload for API logging */ createHeliconePayload(config: HeliconePayloadConfig): { model: string; temperature: number; top_p: number; frequency_penalty: number; presence_penalty: number; n: number; stream: boolean; messages: { role: string; content: string; }[]; }; /** * Creates a standardized Helicone response for API logging */ createHeliconeResponse(config: HeliconeResponseConfig): { id: string; object: string; created: number; model: string; choices: { index: number; message: { role: string; content: string; refusal: null; annotations: never[]; }; logprobs: null; finish_reason: string; }[]; usage: { prompt_tokens: number; completion_tokens: number; total_tokens: number; prompt_tokens_details: { cached_tokens: number; audio_tokens: number; }; completion_tokens_details: { reasoning_tokens: number; audio_tokens: number; accepted_prediction_tokens: number; rejected_prediction_tokens: number; }; }; service_tier: string; system_fingerprint: string; }; } //# sourceMappingURL=observability-api.d.ts.map