cursor-tools
Version:
CLI tools for AI agents
208 lines (207 loc) • 7.84 kB
TypeScript
import type { Config } from '../types';
import OpenAI from 'openai';
export interface ModelOptions {
model: string;
maxTokens: number;
systemPrompt: string;
tokenCount?: number;
webSearch?: boolean;
timeout?: number;
debug: boolean | undefined;
}
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;
}>;
}
export declare abstract class BaseProvider implements BaseModelProvider {
protected config: Config;
protected availableModels?: Promise<Set<string>>;
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;
abstract supportsWebSearch(modelName: string): Promise<{
supported: boolean;
model?: string;
error?: string;
}>;
abstract executePrompt(prompt: string, options: ModelOptions): Promise<string>;
}
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 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 {
private readonly getAuthHeaders;
constructor();
private initializeModels;
supportsWebSearch(modelName: string): Promise<{
supported: boolean;
model?: string;
error?: string;
}>;
executePrompt(prompt: string, options: ModelOptions): Promise<string>;
protected handleLargeTokenCount(tokenCount: number): {
model?: string;
error?: string;
};
private _getAuthHeaders;
}
export declare class GoogleGenerativeLanguageProvider extends BaseProvider {
constructor();
private initializeModels;
private getAPIKey;
supportsWebSearch(modelName: string): Promise<{
supported: boolean;
model?: string;
error?: string;
}>;
executePrompt(prompt: string, options: ModelOptions): Promise<string>;
protected handleLargeTokenCount(tokenCount: number): {
model?: string;
error?: string;
};
}
export declare class OpenAIProvider extends OpenAIBase {
constructor();
supportsWebSearch(modelName: string): Promise<{
supported: boolean;
model?: string;
error?: string;
}>;
executePrompt(prompt: string, options: ModelOptions): Promise<string>;
}
export declare class OpenRouterProvider extends OpenAIBase {
private readonly headers;
constructor();
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 {
supportsWebSearch(modelName: string): Promise<{
supported: boolean;
model?: string;
error?: string;
}>;
executePrompt(prompt: string, options: ModelOptions): Promise<string>;
}
export declare class ModelBoxProvider extends OpenAIBase {
private static readonly defaultHeaders;
private static readonly webSearchHeaders;
constructor();
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 {
private client;
constructor();
supportsWebSearch(modelName: string): Promise<{
supported: boolean;
model?: string;
error?: string;
}>;
executePrompt(prompt: string, options: ModelOptions): Promise<string>;
}
export declare function createProvider(provider: 'gemini' | 'openai' | 'openrouter' | 'perplexity' | 'modelbox' | 'anthropic'): BaseModelProvider;
export {};