/** * @license * Copyright 2025 OSA * SPDX-License-Identifier: Apache-2.0 */ import type { GenerateContentParameters, Part, ToolListUnion } from '@google/genai'; import { GenerateContentResponse } from '@google/genai'; import type OpenAI from 'openai'; /** * Tool call accumulator for streaming responses */ export interface ToolCallAccumulator { id?: string; name?: string; arguments: string; } /** * Converter class for transforming data between OSA and OpenAI formats */ export declare class OpenAIContentConverter { private model; private streamingToolCallParser; /** Counter for generating unique tool call indices when not provided */ private nextToolCallIndex; /** Counter for generating unique tool call IDs */ private toolCallIdCounter; constructor(model: string); /** * Generate a consistent index from a string ID using simple hash * This ensures the same ID always maps to the same index */ private hashStringToIndex; /** * Generate a unique tool call ID using multiple sources of randomness */ private generateToolCallId; /** * Reset streaming tool calls parser for new stream processing * This should be called at the beginning of each stream to prevent * data pollution from previous incomplete streams */ resetStreamingToolCalls(): void; /** * Finalize any incomplete tool calls when stream ends unexpectedly * This should be called when the stream terminates without a finish_reason * to recover any partial tool call data and report warnings * * @returns Array of parts containing any recovered tool calls with warnings */ finalizeIncompleteToolCalls(): Part[]; /** * Check if there are any pending tool calls that haven't been emitted * Useful for detecting incomplete streams */ hasPendingToolCalls(): boolean; /** * Convert OSA tool parameters to OpenAI JSON Schema format */ convertOSAToolParametersToOpenAI(parameters: Record): Record | undefined; /** * Convert OSA tools to OpenAI format for API compatibility. * Handles both OSA tools (using 'parameters' field) and MCP tools (using 'parametersJsonSchema' field). */ convertOSAToolsToOpenAI(OSATools: ToolListUnion): Promise; /** * Convert OSA request to OpenAI message format */ convertOSARequestToOpenAI(request: GenerateContentParameters): OpenAI.Chat.ChatCompletionMessageParam[]; /** * Extract and add system instruction message from request config */ private addSystemInstructionMessage; /** * Process contents and convert to OpenAI messages */ private processContents; /** * Process a single content item and convert to OpenAI message(s) */ private processContent; /** * Parse OSA parts into categorized components */ private parseParts; private extractFunctionResponseContent; /** * Determine media type from MIME type */ private getMediaType; /** * Create multimodal OpenAI message from parsed parts */ private createMultimodalMessage; /** * Convert MIME type to OpenAI audio format */ private getAudioFormat; /** * Type guard to check if content is a valid Content object */ private isContentObject; /** * Extract text content from various OSA content union types */ private extractTextFromContentUnion; /** * Convert OpenAI response to OSA format */ convertOpenAIResponseToOSA(openaiResponse: OpenAI.Chat.ChatCompletion): GenerateContentResponse; /** * Convert OpenAI stream chunk to OSA format */ convertOpenAIChunkToOSA(chunk: OpenAI.Chat.ChatCompletionChunk): GenerateContentResponse; /** * Convert OSA response format to OpenAI chat completion format for logging */ convertOSAResponseToOpenAI(response: GenerateContentResponse): OpenAI.Chat.ChatCompletion; /** * Map OpenAI finish reasons to OSA finish reasons */ private mapOpenAIFinishReasonToOSA; /** * Map OSA finish reasons to OpenAI finish reasons */ private mapOSAFinishReasonToOpenAI; /** * Clean up orphaned tool calls from message history to prevent OpenAI API errors */ private cleanOrphanedToolCalls; /** * Merge consecutive assistant messages to combine split text and tool calls */ private mergeConsecutiveAssistantMessages; }