feat: Add typed stuff
This commit is contained in:
@@ -22,7 +22,7 @@ import type {
|
||||
ProviderInfo,
|
||||
ResponseType
|
||||
} from '../types/index.js';
|
||||
import { AIProviderError, AIErrorType, generateResponseTypePrompt } from '../types/index.js';
|
||||
import { AIProviderError, AIErrorType, generateResponseTypePrompt, parseAndValidateResponseType } from '../types/index.js';
|
||||
|
||||
// ============================================================================
|
||||
// ABSTRACT BASE PROVIDER CLASS
|
||||
@@ -148,7 +148,9 @@ export abstract class BaseAIProvider {
|
||||
* console.log(response.content);
|
||||
* ```
|
||||
*/
|
||||
public async complete<T = any>(params: CompletionParams<T>): Promise<CompletionResponse> {
|
||||
public async complete<T>(params: CompletionParams<T>): Promise<CompletionResponse<T>>;
|
||||
public async complete(params: CompletionParams): Promise<CompletionResponse<string>>;
|
||||
public async complete<T = any>(params: CompletionParams<T>): Promise<CompletionResponse<T | string>> {
|
||||
// Ensure provider is ready for use
|
||||
this.ensureInitialized();
|
||||
|
||||
@@ -160,7 +162,23 @@ export abstract class BaseAIProvider {
|
||||
const processedParams = this.processResponseType(params);
|
||||
|
||||
// Delegate to provider-specific implementation
|
||||
return await this.doComplete(processedParams);
|
||||
const response = await this.doComplete(processedParams);
|
||||
|
||||
// If a responseType is defined, parse and validate the response
|
||||
if (params.responseType) {
|
||||
const parsedData = parseAndValidateResponseType<T>(response.content, params.responseType);
|
||||
return {
|
||||
...response,
|
||||
content: parsedData,
|
||||
rawContent: response.content,
|
||||
};
|
||||
}
|
||||
|
||||
// Otherwise, return the raw string content
|
||||
return {
|
||||
...response,
|
||||
content: response.content,
|
||||
};
|
||||
} catch (error) {
|
||||
// Normalize error to our standard format
|
||||
throw this.normalizeError(error as Error);
|
||||
@@ -249,7 +267,7 @@ export abstract class BaseAIProvider {
|
||||
* @returns Promise resolving to completion response
|
||||
* @throws {Error} If completion fails (will be normalized to AIProviderError)
|
||||
*/
|
||||
protected abstract doComplete<T = any>(params: CompletionParams<T>): Promise<CompletionResponse>;
|
||||
protected abstract doComplete(params: CompletionParams): Promise<CompletionResponse<string>>;
|
||||
|
||||
/**
|
||||
* Provider-specific streaming implementation.
|
||||
|
||||
Reference in New Issue
Block a user