/** * OpenAI Direct Integration Module - Pure Client (LLM Provider Refactoring Phase 2) * * Features: * - Direct OpenAI API integration bypassing AI SDK schema corruption bug * - Support for OpenAI, Azure OpenAI, and OpenAI-compatible providers * - Streaming and non-streaming responses returning LLMResponse * - Function/tool calling detection (NO execution) * - Proper error handling and retry logic * - Browser-safe configuration injection * - Pure client: only calls APIs and returns structured data * * Provider Support: * - OpenAI: Direct integration with OpenAI API * - Azure OpenAI: Direct integration with Azure OpenAI endpoints * - OpenAI-Compatible: Direct integration with custom OpenAI-compatible APIs * - XAI: Direct integration using OpenAI package with custom base URL * - Ollama: Direct integration using OpenAI package with Ollama's OpenAI-compatible endpoint * * Implementation Details: * - Uses official OpenAI package for reliable API access * - Converts AI SDK message format to OpenAI format * - Returns LLMResponse with type='text' or type='tool_calls' * - Streaming support with chunk-by-chunk processing via onChunk callback * - Error handling with descriptive messages * - Configuration injection from llm-config module * - NO event emission, NO storage, NO tool execution * * Recent Changes: * - 2026-03-13: Switched world reasoning overrides to the `reasoning` request object with `default` omission and explicit `none|low|medium|high` effort values. * - 2026-03-12: Reclassified streaming abort logs as info-level cancellations to suppress expected stop/edit noise. * - 2026-02-28: Added canonical `llm.openai` category emission while preserving legacy `openai` logs during migration. * - 2026-02-27: Normalized overlong tool-call IDs to OpenAI's 40-char limit and preserved assistant/tool ID linkage in outbound message conversion. * - 2026-02-16: Fixed streaming tool-call chunk merge to preserve delayed tool `id`/`name` fields across deltas. * - 2026-02-13: Added abort-signal support for streaming and non-streaming calls to enable chat stop cancellation. * - 2026-02-07: Tool definitions now attached for all providers including Ollama * - 2025-11-09: Phase 2 - Removed ALL tool execution logic (~200 lines) * - Provider is now a pure client - only API calls and data transformation * - Returns LLMResponse interface with type discriminator * - Filters invalid tool calls (empty/missing names) and logs warnings * - Includes usage tracking (prompt_tokens, completion_tokens) for non-streaming * - 2025-11-08: Removed ALL event emission from provider (publishToolEvent, publishSSE) * - Streaming uses onChunk callback instead of publishSSE * - Initial implementation with full OpenAI package integration */ import OpenAI from 'openai'; import { World, Agent, ChatMessage, LLMResponse } from './types.js'; import { OpenAIConfig, AzureConfig, OpenAICompatibleConfig, XAIConfig, OllamaConfig } from './llm-config.js'; /** * OpenAI client factory for standard OpenAI API */ export declare function createOpenAIClient(config: OpenAIConfig): OpenAI; /** * OpenAI client factory for Azure OpenAI */ export declare function createAzureOpenAIClient(config: AzureConfig): OpenAI; /** * OpenAI client factory for OpenAI-compatible APIs */ export declare function createOpenAICompatibleClient(config: OpenAICompatibleConfig): OpenAI; /** * OpenAI client factory for XAI */ export declare function createXAIClient(config: XAIConfig): OpenAI; /** * OpenAI client factory for Ollama (OpenAI-compatible endpoint) */ export declare function createOllamaClient(config: OllamaConfig): OpenAI; /** * Streaming OpenAI response handler - Pure client (no tool execution) * Returns LLMResponse with type='text' or type='tool_calls' */ export declare function streamOpenAIResponse(client: OpenAI, model: string, messages: ChatMessage[], agent: Agent, mcpTools: Record, world: World, onChunk: (chunk: { content?: string; reasoningContent?: string; }) => void, messageId: string, abortSignal?: AbortSignal): Promise; /** * Non-streaming OpenAI response handler - Pure client (no tool execution) * Returns LLMResponse with type='text' or type='tool_calls' */ export declare function generateOpenAIResponse(client: OpenAI, model: string, messages: ChatMessage[], agent: Agent, mcpTools: Record, world: World, abortSignal?: AbortSignal): Promise; /** * Factory function to create appropriate OpenAI client based on provider type */ export declare function createClientForProvider(providerType: string, config: any): OpenAI;