feat: add initial implementation of Simple AI Provider package

This commit is contained in:
2025-05-28 11:54:24 +02:00
commit 42902445fb
15 changed files with 1616 additions and 0 deletions

147
src/types/index.ts Normal file
View File

@@ -0,0 +1,147 @@
/**
* Core types and interfaces for the Simple AI Provider package
* Defines the contract that all AI providers must implement
*/
/**
* Configuration options for AI providers
*/
export interface AIProviderConfig {
/** API key for the AI service */
apiKey: string;
/** Optional base URL for custom endpoints */
baseUrl?: string;
/** Request timeout in milliseconds (default: 30000) */
timeout?: number;
/** Maximum number of retry attempts (default: 3) */
maxRetries?: number;
/** Additional provider-specific options */
[key: string]: any;
}
/**
* Message role types supported by AI providers
*/
export type MessageRole = 'system' | 'user' | 'assistant';
/**
* Individual message in a conversation
*/
export interface AIMessage {
/** Role of the message sender */
role: MessageRole;
/** Content of the message */
content: string;
/** Optional metadata for the message */
metadata?: Record<string, any>;
}
/**
* Parameters for AI completion requests
*/
export interface CompletionParams {
/** Array of messages forming the conversation */
messages: AIMessage[];
/** Model to use for completion */
model?: string;
/** Maximum tokens to generate (default: 1000) */
maxTokens?: number;
/** Temperature for randomness (0.0 to 1.0, default: 0.7) */
temperature?: number;
/** Top-p for nucleus sampling (default: 1.0) */
topP?: number;
/** Stop sequences to end generation */
stopSequences?: string[];
/** Whether to stream the response */
stream?: boolean;
/** Additional provider-specific parameters */
[key: string]: any;
}
/**
* Usage statistics for a completion
*/
export interface TokenUsage {
/** Number of tokens in the prompt */
promptTokens: number;
/** Number of tokens in the completion */
completionTokens: number;
/** Total number of tokens used */
totalTokens: number;
}
/**
* Response from an AI completion request
*/
export interface CompletionResponse {
/** Generated content */
content: string;
/** Model used for generation */
model: string;
/** Token usage statistics */
usage: TokenUsage;
/** Unique identifier for the request */
id: string;
/** Provider-specific metadata */
metadata?: Record<string, any>;
}
/**
* Streaming chunk from an AI completion
*/
export interface CompletionChunk {
/** Content delta for this chunk */
content: string;
/** Whether this is the final chunk */
isComplete: boolean;
/** Unique identifier for the request */
id: string;
/** Token usage (only available on final chunk) */
usage?: TokenUsage;
}
/**
* Error types that can occur during AI operations
*/
export enum AIErrorType {
AUTHENTICATION = 'authentication',
RATE_LIMIT = 'rate_limit',
INVALID_REQUEST = 'invalid_request',
MODEL_NOT_FOUND = 'model_not_found',
NETWORK = 'network',
TIMEOUT = 'timeout',
UNKNOWN = 'unknown'
}
/**
* Custom error class for AI provider errors
*/
export class AIProviderError extends Error {
constructor(
message: string,
public type: AIErrorType,
public statusCode?: number,
public originalError?: Error
) {
super(message);
this.name = 'AIProviderError';
}
}
/**
* Provider information and capabilities
*/
export interface ProviderInfo {
/** Name of the provider */
name: string;
/** Version of the provider implementation */
version: string;
/** List of available models */
models: string[];
/** Maximum context length supported */
maxContextLength: number;
/** Whether streaming is supported */
supportsStreaming: boolean;
/** Additional capabilities */
capabilities?: Record<string, any>;
}