UNPKG

vibe-tools

Version:
301 lines (300 loc) 11.4 kB
import type { Config, Provider } from '../types'; import type { VideoAnalysisOptions } from '../types'; import OpenAI from 'openai'; interface GoogleGenerativeLanguageRequestBody { contents: { parts: { text: string; }[]; }[]; generationConfig: { maxOutputTokens: number; }; system_instruction?: { parts: { text: string; }[]; }; tools?: { google_search: Record<string, never>; }[]; } export interface ModelOptions { model: string; maxTokens: number; systemPrompt?: string; tokenCount?: number; webSearch?: boolean; timeout?: number; debug: boolean | undefined; reasoningEffort?: 'low' | 'medium' | 'high'; } export interface ProviderConfig { model?: string; maxTokens?: number; apiKey?: string; referer?: string; appName?: string; debugLogMaxLength?: number; } export interface BaseModelProvider { executePrompt(prompt: string, options?: ModelOptions): Promise<string>; supportsWebSearch(modelName: string): Promise<{ supported: boolean; model?: string; error?: string; }>; tokenUsage?: { promptTokens: number; completionTokens: number; totalTokens: number; }; executeVideoPrompt?(prompt: string, options: VideoAnalysisOptions): Promise<string>; getDefaultMaxTokens?(): number; } export declare abstract class BaseProvider implements BaseModelProvider { abstract provider: Provider; protected config: Config; protected availableModels?: Promise<Set<string>>; tokenUsage?: { promptTokens: number; completionTokens: number; totalTokens: number; }; constructor(); /** * Resolves a model name to an available model from the provider. * This method implements a multi-step resolution process: * 1. Try exact match with provider prefix * 2. Try exact match within any provider namespace * 3. Try prefix matching with various provider prefixes * 4. Try handling special suffixes like -latest or -exp * 5. Try finding similar models based on string similarity * * If no match is found, it throws a ModelNotFoundError with helpful suggestions. * * @param options The model options containing the requested model name * @returns The resolved model name that can be used with the provider's API * @throws ModelNotFoundError if no matching model is found */ protected getModel(options: ModelOptions | undefined): Promise<string>; /** * Try to find an exact match for the model in the available models. * @param model The requested model name * @param availableModels Set of available models * @returns The matched model name or undefined if no match found */ private tryExactMatch; /** * Try to find a match for the model within any provider namespace. * @param model The requested model name * @param modelWithoutPrefix The model name without provider prefix * @param availableModels Set of available models * @returns The matched model name or undefined if no match found */ private tryProviderNamespaceMatch; /** * Try to find a match using various prefix matching strategies. * @param model The requested model name * @param modelWithoutPrefix The model name without provider prefix * @param availableModels Set of available models * @returns The matched model name or undefined if no match found */ private tryPrefixMatch; /** * Try to handle models with -latest suffix by finding the latest version. * @param model The requested model name * @param availableModels Set of available models * @returns The matched model name or undefined if no match found */ private trySuffixHandling; /** * Try to handle models with -exp or -exp-* suffix by finding a non-experimental version. * @param model The requested model name * @param availableModels Set of available models * @returns The matched model name or undefined if no match found */ private tryExperimentalSuffixHandling; /** * Find similar models based on string similarity. * @param model The requested model name * @param modelWithoutPrefix The model name without provider prefix * @param availableModels Set of available models * @returns Array of similar model names */ private findSimilarModels; protected getSystemPrompt(options?: ModelOptions): string | undefined; protected logRequestStart(options: ModelOptions, model: string, maxTokens: number, systemPrompt: string | undefined, endpoint: string, headers?: Record<string, string>): void; protected handleLargeTokenCount(tokenCount: number): { model?: string; error?: string; }; protected debugLog(options: ModelOptions | undefined, message: string, ...args: any[]): void; protected truncateForLogging(obj: any, maxLength?: number): string; /** * Determines if the given model supports the reasoning effort parameter. * Also checks the OVERRIDE_SAFETY_CHECKS environment variable to allow bypassing model restrictions. */ protected doesModelSupportReasoningEffort(model: string): boolean; protected setTokenUsage(promptTokens: number, completionTokens: number): void; protected abstract webSearchParameters(requestParams: Record<string, any>): Record<string, any>; abstract supportsWebSearch(modelName: string): Promise<{ supported: boolean; model?: string; error?: string; }>; abstract executePrompt(prompt: string, options: ModelOptions): Promise<string>; executeVideoPrompt?(prompt: string, options: VideoAnalysisOptions): Promise<string>; getDefaultMaxTokens?(): number; } export declare function retryWithBackoff<T>(operation: () => Promise<T>, maxAttempts?: number, baseDelay?: number, // 1 second shouldRetry?: (error: any) => boolean): Promise<T>; declare abstract class OpenAIBase extends BaseProvider { protected defaultClient: OpenAI; protected webSearchClient: OpenAI; constructor(apiKey: string, baseURL?: string, options?: { defaultHeaders?: Record<string, string>; }, webSearchOptions?: { baseURL?: string; defaultHeaders?: Record<string, string>; }); protected webSearchParameters(requestParams: Record<string, any>): Record<string, any>; protected getClient(options: ModelOptions): OpenAI; supportsWebSearch(modelName: string): Promise<{ supported: boolean; model?: string; error?: string; }>; executePrompt(prompt: string, options: ModelOptions): Promise<string>; } export declare class GoogleVertexAIProvider extends BaseProvider { provider: "gemini"; private readonly getAuthHeaders; constructor(); private initializeModels; supportsWebSearch(modelName: string): Promise<{ supported: boolean; model?: string; error?: string; }>; protected webSearchParameters<T extends Record<string, any>>(requestParams: T): T; executePrompt(prompt: string, options: ModelOptions): Promise<string>; protected handleLargeTokenCount(tokenCount: number): { model?: string; error?: string; }; private _getAuthHeaders; } export declare class GoogleGenerativeLanguageProvider extends BaseProvider { provider: "gemini"; constructor(); getDefaultMaxTokens(): number; private initializeModels; private getAPIKey; supportsWebSearch(modelName: string): Promise<{ supported: boolean; model?: string; error?: string; }>; protected webSearchParameters(requestParams: GoogleGenerativeLanguageRequestBody): GoogleGenerativeLanguageRequestBody; executePrompt(prompt: string, options: ModelOptions): Promise<string>; executeVideoPrompt(prompt: string, options: VideoAnalysisOptions): Promise<string>; protected handleLargeTokenCount(tokenCount: number): { model?: string; error?: string; }; } export declare class OpenAIProvider extends OpenAIBase { provider: "openai"; constructor(); supportsWebSearch(modelName: string): Promise<{ supported: boolean; model?: string; error?: string; }>; executePrompt(prompt: string, options: ModelOptions): Promise<string>; } export declare class OpenRouterProvider extends OpenAIBase { provider: "openrouter"; private readonly headers; constructor(); protected handleLargeTokenCount(tokenCount: number): { model?: string; error?: string; }; private initializeModels; executePrompt(prompt: string, options: ModelOptions): Promise<string>; supportsWebSearch(modelName: string): Promise<{ supported: boolean; model?: string; error?: string; }>; } export declare class PerplexityProvider extends BaseProvider { provider: "perplexity"; protected webSearchParameters(requestParams: Record<string, any>): Record<string, any>; supportsWebSearch(modelName: string): Promise<{ supported: boolean; model?: string; error?: string; }>; executePrompt(prompt: string, options: ModelOptions): Promise<string>; } export declare class ModelBoxProvider extends OpenAIBase { provider: "modelbox"; private static readonly defaultHeaders; private static readonly webSearchHeaders; constructor(); protected handleLargeTokenCount(tokenCount: number): { model?: string; error?: string; }; private initializeModels; supportsWebSearch(modelName: string): Promise<{ supported: boolean; model?: string; error?: string; }>; executePrompt(prompt: string, options: ModelOptions): Promise<string>; } export declare class AnthropicProvider extends BaseProvider { provider: "anthropic"; private client; protected webSearchParameters(requestParams: Record<string, any>): Record<string, any>; constructor(); protected handleLargeTokenCount(tokenCount: number): { model?: string; error?: string; }; supportsWebSearch(modelName: string): Promise<{ supported: boolean; model?: string; error?: string; }>; executePrompt(prompt: string, options: ModelOptions): Promise<string>; } export declare class XAIProvider extends OpenAIBase { provider: "xai"; constructor(); protected webSearchParameters(requestParams: Record<string, any>): Record<string, any>; protected doesModelSupportReasoningEffort(model: string): boolean; supportsWebSearch(modelName: string): Promise<{ supported: boolean; model?: string; error?: string; }>; } export declare class GroqProvider extends OpenAIBase { provider: "groq"; constructor(); private initializeModels; supportsWebSearch(modelName: string): Promise<{ supported: boolean; model?: string; error?: string; }>; executePrompt(prompt: string, options: ModelOptions): Promise<string>; getDefaultMaxTokens(): number; } export declare function createProvider(provider: 'gemini' | 'openai' | 'openrouter' | 'perplexity' | 'modelbox' | 'anthropic' | 'xai' | 'groq'): BaseModelProvider; export {};