import type { OpenAIClient, OpenAIClientOptions, OpenAIModelConfigOverrides, OpenAIModelNames, OpenAISpeechOptions } from './providers/OpenAI/OpenAI.typedefs'; import type { GoogleGenerativeAIClient, GoogleGenerativeAIClientOptions, GoogleGenerativeAIModelNames, GoogleGenerativeAISpeechOptions, GoogleGenerativeAIModelConfigOverrides } from './providers/GoogleGenerativeAI/GoogleGenerativeAI.typedefs'; import type { LLMAPIClient, LLMAPIClientOptions, LLMAPIModelNames, LLMAPIModelConfigOverrides, LLMAPISpeechOptions } from './providers/LLMAPI/LLMAPI.typedefs'; import type { LLMCompletionService, LLMAssistanceService, LLMSpeechToTextService, LLMTextToSpeechService } from './services'; import type { LLMLoggerInterface } from './utilities/logger'; import type { LLMReporterInterface } from './utilities/reporter'; import type { LLMSchemaInterface, InferSchema } from './utilities/schema'; import type { LLMTracedPrompt } from './utilities/llmTracing/llmTracing.typedefs'; /** * Enum of supported LLM providers. */ export declare enum LLMProviders { OpenAI = "OpenAI", GoogleGenerativeAI = "GoogleGenerativeAI", LLMAPI = "LLMAPI" } /** * Maps each LLM provider to its corresponding instance type. */ export type LLMInstances = { [LLMProviders.OpenAI]: OpenAIClient; [LLMProviders.GoogleGenerativeAI]: GoogleGenerativeAIClient; [LLMProviders.LLMAPI]: LLMAPIClient; }; /** * Configuration options for initializing each LLM provider's client. */ export type LLMInstanceOptions = { [LLMProviders.OpenAI]: OpenAIClientOptions; [LLMProviders.GoogleGenerativeAI]: GoogleGenerativeAIClientOptions; [LLMProviders.LLMAPI]: LLMAPIClientOptions; }; /** * Supported service purposes/types that the LLM gateway provides. */ export declare enum LLMPurposes { Completion = "completion", Assistance = "assistance", SpeechToText = "speech_to_text", TextToSpeech = "text_to_speech" } /** * Maps each LLM purpose to its corresponding service implementation for a specific provider. * @template Provider - The LLM provider type * @template Reporter - The reporter interface type */ export type LLMServiceByPurpose | undefined> = { [LLMPurposes.Completion]: LLMCompletionService; [LLMPurposes.Assistance]: LLMAssistanceService; [LLMPurposes.SpeechToText]: LLMSpeechToTextService; [LLMPurposes.TextToSpeech]: LLMTextToSpeechService; }; /** * Function type for building an LLM service instance for a specific provider and purpose. * @template Provider - The LLM provider type * @template Purpose - The service purpose type * @template Reporter - Reporter interface type (optional) * @param logger - Optional LLMLogger instance for service logging * @param reporter - Optional reporter instance for metrics collection * @param options - Provider-specific configuration options * @returns An instance of the requested LLM service */ export type LLMServiceBuilder | undefined> = (logger: LLMLoggerInterface | undefined, reporter: Reporter, options?: LLMInstanceOptions[Provider]) => LLMServiceByPurpose[Purpose]; /** * Defines token limits for LLM models. */ export interface LLMLimits { maxInputTokens: number; maxOutputTokens: number; } /** * Describes which capabilities an LLM model supports. */ export interface LLMModelCapabilities { vision: boolean; tools: boolean; reasoning: boolean; webSearch: boolean; jsonOutput?: boolean; structuredOutputs?: boolean; tts?: boolean; stt?: boolean; streamingSTT?: boolean; /** * Whether the model's tool/function-result message can natively carry * images. When absent or false, providers surface tool-returned images * through a separate user message instead. */ multimodalToolResults?: boolean; } /** * Configuration parameters that control LLM generation behavior. */ export type LLMConfig = Partial<{ temperature: number; top_p: number | null; }> & (Provider extends LLMProviders.OpenAI ? OpenAIModelConfigOverrides : Provider extends LLMProviders.GoogleGenerativeAI ? GoogleGenerativeAIModelConfigOverrides : Provider extends LLMProviders.LLMAPI ? LLMAPIModelConfigOverrides : {}); /** * Maps a provider to all available models it can access. * @template Provider - The LLM provider type */ export type LLMProviderAvailableModels = { [ModelName in LLMModelName[Provider]]: LLMModel; }; /** * Maps service purposes to the available models for a specific provider. * @template Purpose - The service purpose type * @template Provider - The LLM provider type */ export type LLMProviderModelsByPurpose = Record>>; /** * Maps each provider to its supported model names. */ export type LLMModelName = { [LLMProviders.OpenAI]: OpenAIModelNames; [LLMProviders.GoogleGenerativeAI]: GoogleGenerativeAIModelNames; [LLMProviders.LLMAPI]: LLMAPIModelNames; }; /** * Represents an LLM model with its configuration and limitations. * @template Provider - The LLM provider type * @template ModelName - The specific model name for the provider */ export interface LLMModel { name: ModelName; limits: LLMLimits; config: LLMConfig; pricing: LLMModelPricing; capabilities?: LLMModelCapabilities; } /** * Pricing information for LLM models */ export interface LLMModelPricing { /** * Pricing calculation for text input */ getPriceForTextInput: (tokens: number) => number; /** * Pricing calculation for text output */ getPriceForTextOutput: (tokens: number) => number; /** * Pricing calculation for audio input */ getPriceForAudioInput: (audio_tokens: number) => number; /** * Pricing calculation for audio output */ getPriceForAudioOutput: (audio_tokens: number) => number; /** * Pricing calculation for cached text input (optional) * If not provided, cached tokens will be charged at regular text input rate * OpenAI: 50% discount on cached tokens * Google: 75-90% discount depending on model */ getPriceForCachedTextInput?: (tokens: number) => number; /** * Pricing calculation for cached audio input (optional) * If not provided, cached tokens will be charged at regular audio input rate */ getPriceForCachedAudioInput?: (audio_tokens: number) => number; /** * Currency code for the pricing */ currency: 'USD'; } /** * Roles that can be assigned to messages in an LLM conversation. */ export declare enum LLMRoles { User = "user", Assistant = "assistant" } /** * Possible content types for messages in an LLM conversation. */ export declare enum LLMMessageContentType { TEXT = "text", IMAGE_URL = "image_url", AUDIO_FILE = "audio_file" } /** * Represents a text part in a message in an LLM conversation. */ export interface LLMMessageTextContent { type: LLMMessageContentType.TEXT; text: string; } /** * Represents an image part (by url) in a message in an LLM conversation. */ export interface LLMMessageImageUrlContent { type: LLMMessageContentType.IMAGE_URL; image_url: { url: string; detail?: 'auto' | 'low' | 'high'; }; } /** * Union type for all possible message content types in an LLM completion. */ export type LLMCompletionsMessageContent = LLMMessageTextContent | LLMMessageImageUrlContent; /** * Union type for all possible message content types in an LLM assistance. */ export type LLMAssistanceMessageContent = LLMMessageTextContent | LLMMessageImageUrlContent; /** * Represents an audio file buffer */ export type LLMAudioContent = { type: LLMMessageContentType.AUDIO_FILE; buffer: Buffer; }; /** * Represents a message in an LLM completion request. */ export interface LLMCompletionMessage { role: LLMRoles; content: LLMCompletionsMessageContent[]; } /** * Represents a message in an LLM assistance request. */ export interface LLMAssistanceMessage { role: LLMRoles; content: LLMAssistanceMessageContent[]; type?: LLMHistoryEntryTypes.Message; } /** * What a restored history entry is. An entry that declares no `type` is an * ordinary conversation message, which is why `LLMAssistanceMessage` carries * the member optionally: an `LLMAssistanceMessage[]` written against the * message-only contract is already a valid `LLMHistoryEntry[]`. */ export declare enum LLMHistoryEntryTypes { Message = "message", ToolCall = "tool_call", ToolResult = "tool_result", Context = "context" } /** * One tool call a past round made. `id` is what an `LLMHistoryToolResultEntry` * correlates against, and `arguments` is the decoded argument object — * providers that transport arguments as a JSON string are serialized to by the * mapping, so no caller encodes them. */ export interface LLMHistoryToolCall { id: string; name: string; arguments: Record; /** * The provider's own signature over the call, when the caller kept one. * Gemini issues a `thought_signature` it expects echoed on replay; a call * restored without one replays with the documented validator-bypass marker. */ signature?: string; } /** * A past assistant round that called tools, with the narration the round * emitted alongside them. Every call is answered by an * `LLMHistoryToolResultEntry` of the same history; an unanswered one is * answered by the package instead of by the caller. */ export interface LLMHistoryToolCallEntry { type: LLMHistoryEntryTypes.ToolCall; calls: LLMHistoryToolCall[]; text?: string; } /** The result a past tool call returned, correlated by `toolCallId`. */ export interface LLMHistoryToolResultEntry { type: LLMHistoryEntryTypes.ToolResult; toolCallId: string; result: LLMToolResult; } /** * Context that belongs to the conversation without being a turn of it — a * carried plan, a summary of what came before. It maps to each provider's own * mid-conversation context representation, so a caller never disguises it as a * user message. */ export interface LLMHistoryContextEntry { type: LLMHistoryEntryTypes.Context; text: string; } /** * One entry of a restored conversation. The union is additive: the * message-only history every existing caller passes is still exactly a * `LLMHistoryEntry[]`. */ export type LLMHistoryEntry = LLMAssistanceMessage | LLMHistoryToolCallEntry | LLMHistoryToolResultEntry | LLMHistoryContextEntry; /** * Represents an error that occurred during an LLM request. */ export type LLMRequestError = Error; /** * Token usage reported by an LLM provider for a single call. */ export interface LLMModelUsage { inputTextTokens?: number; outputTextTokens?: number; inputAudioTokens?: number; outputAudioTokens?: number; inputCachedTextTokens?: number; inputCachedAudioTokens?: number; outputReasoningTokens?: number; } /** * Absolute USD cost for a single LLM call, derived from usage and model pricing. */ export interface LLMCostsResult { input: number; output: number; total: number; currency: string; } /** * Generic result type for LLM requests that can either succeed or fail. * @template ResponseType - The type of the successful result */ export type LLMRequestResult = ResponseType | { error: LLMRequestError; }; /** * Result type for LLM completion requests. */ export type LLMCompletionResult = LLMRequestResult<{ text: string; usage?: LLMModelUsage; cost?: LLMCostsResult; }>; /** * Base options for LLM methods that can be extended by specific method options. * Contains common properties like abort signal for cancellation. * @template Reporter - Reporter interface type */ export type LLMMethodBaseOptions | undefined> = { abortSignal?: AbortSignal; traceContext?: LLMCallTraceContext; } & (Reporter extends LLMReporterInterface ? R extends undefined ? { reporterContext?: never; } : { reporterContext: R; } : { reporterContext?: never; }); /** * Per-call trace context threaded into a single gateway call. When a prompt is * present it names the observation instead of `/` and links * the observation to its managed prompt version. */ export type LLMCallTraceContext = { prompt?: LLMTracedPrompt; /** * Names the observation. Wins over every other source, and is the only way to * name a call that has no managed prompt to borrow a name from - speech-to-text * and text-to-speech, which would otherwise read as `/` and * lose the domain operation they belong to. */ name?: string; /** * @deprecated Use `prompt`. Kept on 7.8 for compatibility; still names the * observation when `prompt` is absent. */ promptName?: string; /** * The user this call was made for, as a typed trace attribute. Travels beside * the metadata bag rather than inside it, because the tracer promotes it to * the trace's own user field. */ userId?: string; /** * Groups every trace of one conversation, review, or agent run under a single * Langfuse session. A typed trace attribute, never a metadata key. */ sessionId?: string; /** Langfuse trace tags. A typed trace attribute, never a metadata key. */ tags?: string[]; metadata?: Record; }; export declare enum LLMReasoningEffort { None = "none", Minimal = "minimal", Low = "low", Medium = "medium", High = "high", Xhigh = "xhigh" } /** * Per-call generation knobs resolved from a prompt's `config.params`. They * override the static model-map defaults for this call only: a prompt that * carries none keeps today's behavior exactly. Providers apply each knob in * their own request dialect and ignore the ones they cannot express. */ export interface LLMGenerationTuning { reasoningEffort?: LLMReasoningEffort; } /** * Options for sending a message to an LLM service. * @template Provider - The LLM provider type * @template Reporter - Reporter interface type */ export type LLMSendMessageOptions | undefined> = LLMMethodBaseOptions & { message: LLMCompletionMessage; model: LLMModel; history?: LLMCompletionMessage[]; instructions?: string; generationParams?: LLMGenerationTuning; }; /** * Result type for structured output from LLM providers. * @template T - The type of the structured data */ export type LLMStructuredResult = LLMRequestResult<{ text: string; data?: T; parseError?: string; toolIterationsUsed?: number; toolIterationsExhausted?: true; stopReason?: LLMToolLoopStopReasons; terminalTool?: LLMTerminalToolOutcome; usage?: LLMModelUsage; cost?: LLMCostsResult; }>; /** * Limits for files that can be uploaded to LLM services. */ export type LLMFileLimits = { maxFileSize: number; supportedMimeTypes: LLMUploadFileMimeTypes[]; }; /** * Supported MIME types for files that can be uploaded to LLM services. */ export declare enum LLMUploadFileMimeTypes { IMAGE_PNG = "image/png", IMAGE_JPEG = "image/jpeg", IMAGE_JPG = "image/jpg", IMAGE_GIF = "image/gif", IMAGE_WEBP = "image/webp", IMAGE_SVG_XML = "image/svg+xml", IMAGE_BMP = "image/bmp", PLAIN_TEXT = "text/plain", MARKDOWN = "text/markdown", AUDIO_MP3 = "audio/mp3", AUDIO_MPEG = "audio/mpeg", AUDIO_WAV = "audio/wav", AUDIO_WEBM = "audio/webm", AUDIO_OGG = "audio/ogg" } /** * Represents the MIME types for images that can be uploaded to LLM services. */ export type LLMImageMimeType = LLMUploadFileMimeTypes.IMAGE_PNG | LLMUploadFileMimeTypes.IMAGE_JPEG | LLMUploadFileMimeTypes.IMAGE_JPG | LLMUploadFileMimeTypes.IMAGE_GIF | LLMUploadFileMimeTypes.IMAGE_WEBP | LLMUploadFileMimeTypes.IMAGE_SVG_XML | LLMUploadFileMimeTypes.IMAGE_BMP; /** * Represents the MIME types for audio files that can be uploaded to LLM services. */ export type LLMAudioMimeType = LLMUploadFileMimeTypes.AUDIO_MP3 | LLMUploadFileMimeTypes.AUDIO_MPEG | LLMUploadFileMimeTypes.AUDIO_WAV | LLMUploadFileMimeTypes.AUDIO_WEBM | LLMUploadFileMimeTypes.AUDIO_OGG; /** * Represents a file to be uploaded to an LLM service. * @template Reporter - Reporter interface type */ export type LLMUploadFileOptions | undefined> = LLMMethodBaseOptions & { mimeType: LLMUploadFileMimeTypes; name: string; path: string; }; /** * Represents a file that has been successfully uploaded to an LLM service. */ export interface LLMUploadedFile extends Omit, 'reporterContext' | 'abortSignal'> { fileId: string; } /** * Result type for file upload operations. */ export type LLMUploadFileResult = LLMRequestResult; /** * Options for creating a file storage for a specific LLM provider. * @template Provider - The LLM provider type * @template Reporter - Reporter interface type */ export type LLMCreateFileStorageOptions | undefined> = LLMMethodBaseOptions & { uploadedFiles: LLMUploadedFile[]; model: LLMModel; instructions?: string; }; /** * Represents a created file storage in an LLM service. */ export interface LLMCreatedFileStorage { storageId: string; } /** * Result type for file storage creation operations. */ export type LLMCreateFileStorageResult = LLMRequestResult; /** * Options for creating a chat session with a specific LLM provider. * @template Provider - The LLM provider type * @template Reporter - Reporter interface type */ export type LLMCreateChatOptions | undefined> = LLMMethodBaseOptions & { model: LLMModel; instructions?: string; /** * The conversation to restore before this chat's first message. Plain * messages, past tool calls and their results, and mid-conversation context * all ride here and map to each provider's native representation. */ history?: LLMHistoryEntry[]; storageId?: string; files?: LLMUploadedFile[]; }; /** * Represents a created chat session in an LLM service. */ export interface LLMCreatedChat { chatId: string; } /** * Result type for chat creation operations. */ export type LLMCreateChatResult = LLMRequestResult; /** * Options for chat assistance with a specific LLM provider. * @template Provider - The LLM provider type * @template Reporter - Reporter interface type */ export type LLMAssistanceOptions | undefined> = LLMMethodBaseOptions & { model: LLMModel; message: LLMAssistanceMessage; chatId: string; storageId?: string; tools?: LLMToolDefinition[]; maxToolIterations?: number; generationParams?: LLMGenerationTuning; /** Groups this legacy tool loop under one agent trace observation. */ agentName?: string; /** * Invoked with the model's visible assistant text from a tool-calling round * that will continue the loop (narration emitted alongside tool calls), * before that round's tools execute. Never carries the final round's answer * or reasoning/thinking content; the text may be empty when the round * produced only tool calls, so the consumer skips empty/whitespace-only text. * Observability-only: the callback must not throw — the tool loop does not * guard the call. */ onAssistantNarration?: (text: string) => void; onModelStepStarted?: () => void; onToolCallRejected?: (call: LLMRejectedToolCall) => void; }; export interface LLMRejectedToolCall { name: string; input: Record; error: string; } /** * Result type for assistance requests. */ export type LLMAssistanceResult = LLMRequestResult<{ text: string; toolIterationsUsed?: number; toolIterationsExhausted?: true; stopReason?: LLMToolLoopStopReasons; terminalTool?: LLMTerminalToolOutcome; }>; /** * Options for a one-shot prompt in a newly created chat. * Combines chat creation options with message sending options. * @template Provider - The LLM provider type * @template Reporter - Reporter interface type */ export type LLMAssistInNewChatOptions | undefined> = LLMCreateChatOptions & { prompt: string; tools?: LLMToolDefinition[]; maxToolIterations?: number; }; /** * An image returned by a tool's `execute` for the model to see. */ export interface LLMToolResultImage { /** Image location. An https URL, or a data URL for inline bytes. */ url: string; /** Vision detail hint passed to providers that accept it. */ detail?: 'auto' | 'low' | 'high'; /** * MIME type of the image. Defaults to `image/png`. Used by providers that * require raw bytes (Gemini) when fetching and re-encoding the image. */ mimeType?: string; /** Optional caption rendered next to the image for the model. */ label?: string; } /** * Structured result a tool's `execute` may return when it needs to surface * images alongside text. The `content` string always becomes the * tool/function-result message; `images` are surfaced to the model the way * each provider's API allows. */ export interface LLMStructuredToolResult { content: string; images?: LLMToolResultImage[]; } /** * What a tool's `execute` may return. A plain `string` keeps the legacy * text-only behaviour; an `LLMStructuredToolResult` additionally carries * images. */ export type LLMToolResult = string | LLMStructuredToolResult; /** * The verdict of a tool's `canExecute` precondition. A refusal carries the * `reason` the model is told, which is why it is required: a denial the model * cannot explain to itself is a denial it will retry blindly. */ export type LLMToolGateResult = { allowed: true; } | { allowed: false; reason: string; }; export declare enum LLMToolTracingObservation { Tool = "tool", Retriever = "retriever", None = "none" } export interface LLMToolTracingOptions> { observation?: LLMToolTracingObservation; mapInput?: (args: Input) => unknown; } export interface LLMSerializedToolDefinition { name: string; description: string; parameters: unknown; } export interface LLMToolDefinition { name: string; description: string; parameters: Schema; execute: (args: InferSchema) => Promise | LLMToolResult; /** * Decides whether this call may run, on the arguments the model supplied. * Checked before `execute`, so a refused call never has its side effect. A * refusal is ordinary control flow, not an error: the model is told the * `reason` as the call's result and the run continues with its iteration * budget untouched. Use it for permission, quota, and state preconditions * that depend on the arguments rather than on the tool being present at all. */ canExecute?: (args: InferSchema) => Promise | LLMToolGateResult; tracing?: LLMToolTracingOptions>; /** * Ends the turn once this tool runs. The round executes in full (every call * of that round, including parallel ones), but its results are not fed back * and no further model round starts — the loop stops with * `LLMToolLoopStopReasons.TerminalTool` and this tool's output as the turn * outcome. Use for tools that hand control back to the application, such as * an `ask_user` tool that needs a human reply before the agent can continue. */ terminal?: boolean; } /** * Why a provider's model⇄tool loop stopped. `Completed` is the ordinary end * (a model round returned no tool calls), `MaxIterations` means the loop hit * `maxToolIterations` with tool calls still pending, and `TerminalTool` means * a tool declaring `terminal` ran and ended the turn. */ export declare enum LLMToolLoopStopReasons { Completed = "completed", MaxIterations = "max_iterations", TerminalTool = "terminal_tool" } /** * The terminal tool call that ended a turn: the tool's name and the output it * returned, which the application handles instead of a model answer. When more * than one terminal call lands in the same round, the first in the round's * order is reported (all of them still execute). */ export interface LLMTerminalToolOutcome { name: string; output: LLMToolResult; } /** * Function type for counting tokens in messages for a specific provider's model. * @template Provider - The LLM provider type * @template ContentType - The type of the message content * @param messages - Array of message strings to count tokens for * @param model - The LLM model to use for token counting * @returns Promise resolving to the number of tokens */ export type LLMCountTokensFunction = (messagesContents: ContentType, model: LLMModel) => Promise; /** * Options for combining messages with a limit on the number of tokens. * @template Provider - The LLM provider type * @template TokenCountFunction - Function type for counting tokens */ export interface CombineMessagesOptions> { history?: LLMCompletionMessage[]; message: LLMCompletionMessage; model: LLMModel; instance: LLMInstances[Provider]; logger: LLMLoggerInterface | undefined; countTokensFn: TokenCountFunction; } /** * Options for transcribing audio to text using an LLM service. * @template Provider - The LLM provider type * @template Reporter - Reporter interface type */ export type LLMTranscribeOptions | undefined> = LLMMethodBaseOptions & { pathToAudio: string; mimeType?: LLMAudioMimeType; model: LLMModel; instructions?: string; language?: string; }; /** * Result type for LLM transcription requests. */ export type LLMTranscribeResult = LLMRequestResult<{ text: string; usage?: LLMModelUsage; cost?: LLMCostsResult; }>; /** * Provider-specific options for creating speech from text. */ export type LLMSpeechOptions = { [LLMProviders.OpenAI]: OpenAISpeechOptions; [LLMProviders.GoogleGenerativeAI]: GoogleGenerativeAISpeechOptions; [LLMProviders.LLMAPI]: LLMAPISpeechOptions; }; /** * Options for creating speech from text using an LLM service. * @template Provider - The LLM provider type * @template Reporter - Reporter interface type */ export type LLMCreateSpeechOptions | undefined> = LLMMethodBaseOptions & { text: string; model: LLMModel; instructions?: string; speechOptions?: LLMSpeechOptions[Provider]; }; /** * Result type for LLM speech creation requests. * Contains the generated audio buffer and its MIME type. */ export type LLMCreateSpeechResult = LLMRequestResult<{ audio: Buffer; mimeType: string | null; }>; /** * Options for creating LLM services. * @template Provider - The LLM provider type * @template Reporter - Reporter interface type */ export interface LLMServiceOptions | undefined> { /** The LLM provider (OpenAI, GoogleGenerativeAI) */ provider: Provider; /** Provider-specific configuration options */ options?: LLMInstanceOptions[Provider]; /** Logger instance (optional, no logging if not provided) */ logger?: LLMLoggerInterface; /** Reporter instance (optional, no reporting if not provided) */ reporter?: Reporter; }