feat: Add typed stuff
This commit is contained in:
@@ -9,7 +9,7 @@
|
|||||||
import {
|
import {
|
||||||
createClaudeProvider,
|
createClaudeProvider,
|
||||||
createResponseType,
|
createResponseType,
|
||||||
validateResponseType,
|
parseAndValidateResponseType,
|
||||||
AIProviderError,
|
AIProviderError,
|
||||||
AIErrorType
|
AIErrorType
|
||||||
} from '../src/index.js';
|
} from '../src/index.js';
|
||||||
@@ -137,7 +137,7 @@ async function demonstrateUserProfileGeneration() {
|
|||||||
await claude.initialize();
|
await claude.initialize();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await claude.complete({
|
const response = await claude.complete<UserProfile>({
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
role: 'user',
|
role: 'user',
|
||||||
@@ -149,23 +149,16 @@ async function demonstrateUserProfileGeneration() {
|
|||||||
temperature: 0.7
|
temperature: 0.7
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('Raw response:', response.content);
|
console.log('Raw response:', response.rawContent);
|
||||||
|
|
||||||
// Validate and parse the response
|
const userProfile = response.content;
|
||||||
const validation = validateResponseType(response.content, userProfileType);
|
console.log('\nParsed User Profile:');
|
||||||
|
console.log(`Name: ${userProfile.name}`);
|
||||||
if (validation.isValid) {
|
console.log(`Age: ${userProfile.age}`);
|
||||||
const userProfile = validation.data as UserProfile;
|
console.log(`Email: ${userProfile.email}`);
|
||||||
console.log('\nParsed User Profile:');
|
console.log(`Theme Preference: ${userProfile.preferences.theme}`);
|
||||||
console.log(`Name: ${userProfile.name}`);
|
console.log(`Notifications: ${userProfile.preferences.notifications}`);
|
||||||
console.log(`Age: ${userProfile.age}`);
|
console.log(`Skills: ${userProfile.skills.join(', ')}`);
|
||||||
console.log(`Email: ${userProfile.email}`);
|
|
||||||
console.log(`Theme Preference: ${userProfile.preferences.theme}`);
|
|
||||||
console.log(`Notifications: ${userProfile.preferences.notifications}`);
|
|
||||||
console.log(`Skills: ${userProfile.skills.join(', ')}`);
|
|
||||||
} else {
|
|
||||||
console.error('Validation failed:', validation.error);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof AIProviderError) {
|
if (error instanceof AIProviderError) {
|
||||||
@@ -183,7 +176,7 @@ async function demonstrateProductAnalysis() {
|
|||||||
await claude.initialize();
|
await claude.initialize();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await claude.complete({
|
const response = await claude.complete<ProductAnalysis>({
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
role: 'user',
|
role: 'user',
|
||||||
@@ -195,24 +188,18 @@ async function demonstrateProductAnalysis() {
|
|||||||
temperature: 0.5
|
temperature: 0.5
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('Raw response:', response.content);
|
console.log('Raw response:', response.rawContent);
|
||||||
|
|
||||||
const validation = validateResponseType(response.content, productAnalysisType);
|
const analysis = response.content;
|
||||||
|
console.log('\nProduct Analysis:');
|
||||||
if (validation.isValid) {
|
console.log(`Product: ${analysis.productName}`);
|
||||||
const analysis = validation.data as ProductAnalysis;
|
console.log(`Category: ${analysis.category}`);
|
||||||
console.log('\nProduct Analysis:');
|
console.log(`Price Range: ${analysis.priceRange}`);
|
||||||
console.log(`Product: ${analysis.productName}`);
|
console.log(`Overall Rating: ${analysis.overallRating}/10`);
|
||||||
console.log(`Category: ${analysis.category}`);
|
console.log(`Recommendation: ${analysis.recommendation}`);
|
||||||
console.log(`Price Range: ${analysis.priceRange}`);
|
console.log(`Pros: ${analysis.pros.join(', ')}`);
|
||||||
console.log(`Overall Rating: ${analysis.overallRating}/10`);
|
console.log(`Cons: ${analysis.cons.join(', ')}`);
|
||||||
console.log(`Recommendation: ${analysis.recommendation}`);
|
console.log(`Reasoning: ${analysis.reasoning}`);
|
||||||
console.log(`Pros: ${analysis.pros.join(', ')}`);
|
|
||||||
console.log(`Cons: ${analysis.cons.join(', ')}`);
|
|
||||||
console.log(`Reasoning: ${analysis.reasoning}`);
|
|
||||||
} else {
|
|
||||||
console.error('Validation failed:', validation.error);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof AIProviderError) {
|
if (error instanceof AIProviderError) {
|
||||||
@@ -248,11 +235,14 @@ function processPayment(amount, cardNumber) {
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await claude.complete({
|
const response = await claude.complete<CodeReview>({
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
role: 'user',
|
role: 'user',
|
||||||
content: `Please review this JavaScript code and provide a comprehensive analysis:\n\n\`\`\`javascript\n${sampleCode}\n\`\`\``
|
content: `Please review this JavaScript code and provide a comprehensive analysis:\n\n\
|
||||||
|
`${sampleCode}
|
||||||
|
\
|
||||||
|
``` `
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
responseType: codeReviewType,
|
responseType: codeReviewType,
|
||||||
@@ -260,27 +250,21 @@ function processPayment(amount, cardNumber) {
|
|||||||
temperature: 0.3
|
temperature: 0.3
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('Raw response:', response.content);
|
console.log('Raw response:', response.rawContent);
|
||||||
|
|
||||||
const validation = validateResponseType(response.content, codeReviewType);
|
const review = response.content;
|
||||||
|
console.log('\nCode Review:');
|
||||||
if (validation.isValid) {
|
console.log(`Overall Score: ${review.overallScore}/10`);
|
||||||
const review = validation.data as CodeReview;
|
console.log(`Summary: ${review.summary}`);
|
||||||
console.log('\nCode Review:');
|
console.log(`\nStrengths:`);
|
||||||
console.log(`Overall Score: ${review.overallScore}/10`);
|
review.strengths.forEach(strength => console.log(` • ${strength}`));
|
||||||
console.log(`Summary: ${review.summary}`);
|
console.log(`\nImprovements:`);
|
||||||
console.log(`\nStrengths:`);
|
review.improvements.forEach(improvement => console.log(` • ${improvement}`));
|
||||||
review.strengths.forEach(strength => console.log(` • ${strength}`));
|
console.log(`\nIssues:`);
|
||||||
console.log(`\nImprovements:`);
|
review.issues.forEach(issue => {
|
||||||
review.improvements.forEach(improvement => console.log(` • ${improvement}`));
|
const lineInfo = issue.line ? ` (line ${issue.line})` : '';
|
||||||
console.log(`\nIssues:`);
|
console.log(` • [${issue.severity.toUpperCase()}] ${issue.type}: ${issue.message}${lineInfo}`);
|
||||||
review.issues.forEach(issue => {
|
});
|
||||||
const lineInfo = issue.line ? ` (line ${issue.line})` : '';
|
|
||||||
console.log(` • [${issue.severity.toUpperCase()}] ${issue.type}: ${issue.message}${lineInfo}`);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
console.error('Validation failed:', validation.error);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof AIProviderError) {
|
if (error instanceof AIProviderError) {
|
||||||
@@ -319,11 +303,12 @@ async function demonstrateStreamingWithResponseType() {
|
|||||||
console.log('\n\nStream completed. Usage:', chunk.usage);
|
console.log('\n\nStream completed. Usage:', chunk.usage);
|
||||||
|
|
||||||
// Validate the complete streamed response
|
// Validate the complete streamed response
|
||||||
const validation = validateResponseType(fullResponse, productAnalysisType);
|
try {
|
||||||
if (validation.isValid) {
|
const analysis = parseAndValidateResponseType(fullResponse, productAnalysisType);
|
||||||
console.log('\nStreamed response validation: SUCCESS');
|
console.log('\nStreamed response validation: SUCCESS');
|
||||||
} else {
|
console.log(`Product: ${analysis.productName}`);
|
||||||
console.log('\nStreamed response validation: FAILED -', validation.error);
|
} catch (e) {
|
||||||
|
console.log('\nStreamed response validation: FAILED -', (e as Error).message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ export { AIProviderError, AIErrorType } from './types/index.js';
|
|||||||
export {
|
export {
|
||||||
createResponseType,
|
createResponseType,
|
||||||
generateResponseTypePrompt,
|
generateResponseTypePrompt,
|
||||||
validateResponseType
|
parseAndValidateResponseType
|
||||||
} from './types/index.js';
|
} from './types/index.js';
|
||||||
|
|
||||||
// Base provider
|
// Base provider
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ import type {
|
|||||||
ProviderInfo,
|
ProviderInfo,
|
||||||
ResponseType
|
ResponseType
|
||||||
} from '../types/index.js';
|
} 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
|
// ABSTRACT BASE PROVIDER CLASS
|
||||||
@@ -148,7 +148,9 @@ export abstract class BaseAIProvider {
|
|||||||
* console.log(response.content);
|
* 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
|
// Ensure provider is ready for use
|
||||||
this.ensureInitialized();
|
this.ensureInitialized();
|
||||||
|
|
||||||
@@ -160,7 +162,23 @@ export abstract class BaseAIProvider {
|
|||||||
const processedParams = this.processResponseType(params);
|
const processedParams = this.processResponseType(params);
|
||||||
|
|
||||||
// Delegate to provider-specific implementation
|
// 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) {
|
} catch (error) {
|
||||||
// Normalize error to our standard format
|
// Normalize error to our standard format
|
||||||
throw this.normalizeError(error as Error);
|
throw this.normalizeError(error as Error);
|
||||||
@@ -249,7 +267,7 @@ export abstract class BaseAIProvider {
|
|||||||
* @returns Promise resolving to completion response
|
* @returns Promise resolving to completion response
|
||||||
* @throws {Error} If completion fails (will be normalized to AIProviderError)
|
* @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.
|
* Provider-specific streaming implementation.
|
||||||
|
|||||||
@@ -220,7 +220,7 @@ export class ClaudeProvider extends BaseAIProvider {
|
|||||||
* @returns Promise resolving to formatted completion response
|
* @returns Promise resolving to formatted completion response
|
||||||
* @throws {Error} If API request fails
|
* @throws {Error} If API request fails
|
||||||
*/
|
*/
|
||||||
protected async doComplete<T = any>(params: CompletionParams<T>): Promise<CompletionResponse> {
|
protected async doComplete(params: CompletionParams): Promise<CompletionResponse<string>> {
|
||||||
if (!this.client) {
|
if (!this.client) {
|
||||||
throw new AIProviderError('Claude client not initialized', AIErrorType.INVALID_REQUEST);
|
throw new AIProviderError('Claude client not initialized', AIErrorType.INVALID_REQUEST);
|
||||||
}
|
}
|
||||||
@@ -533,7 +533,7 @@ export class ClaudeProvider extends BaseAIProvider {
|
|||||||
* @returns Formatted completion response
|
* @returns Formatted completion response
|
||||||
* @throws {AIProviderError} If response format is unexpected
|
* @throws {AIProviderError} If response format is unexpected
|
||||||
*/
|
*/
|
||||||
private formatCompletionResponse(response: any): CompletionResponse {
|
private formatCompletionResponse(response: any): CompletionResponse<string> {
|
||||||
// Extract text content from response blocks
|
// Extract text content from response blocks
|
||||||
const content = response.content
|
const content = response.content
|
||||||
?.filter((block: any) => block.type === 'text')
|
?.filter((block: any) => block.type === 'text')
|
||||||
|
|||||||
@@ -268,7 +268,7 @@ export class GeminiProvider extends BaseAIProvider {
|
|||||||
* @returns Promise resolving to formatted completion response
|
* @returns Promise resolving to formatted completion response
|
||||||
* @throws {Error} If API request fails
|
* @throws {Error} If API request fails
|
||||||
*/
|
*/
|
||||||
protected async doComplete<T = any>(params: CompletionParams<T>): Promise<CompletionResponse> {
|
protected async doComplete(params: CompletionParams): Promise<CompletionResponse<string>> {
|
||||||
if (!this.client || !this.model) {
|
if (!this.client || !this.model) {
|
||||||
throw new AIProviderError('Gemini client not initialized', AIErrorType.INVALID_REQUEST);
|
throw new AIProviderError('Gemini client not initialized', AIErrorType.INVALID_REQUEST);
|
||||||
}
|
}
|
||||||
@@ -617,7 +617,7 @@ export class GeminiProvider extends BaseAIProvider {
|
|||||||
* @returns Formatted completion response
|
* @returns Formatted completion response
|
||||||
* @throws {AIProviderError} If response format is unexpected
|
* @throws {AIProviderError} If response format is unexpected
|
||||||
*/
|
*/
|
||||||
private formatCompletionResponse(response: any, model: string): CompletionResponse {
|
private formatCompletionResponse(response: any, model: string): CompletionResponse<string> {
|
||||||
// Handle multiple text parts in the response
|
// Handle multiple text parts in the response
|
||||||
const candidate = response.candidates?.[0];
|
const candidate = response.candidates?.[0];
|
||||||
if (!candidate) {
|
if (!candidate) {
|
||||||
|
|||||||
@@ -244,7 +244,7 @@ export class OpenAIProvider extends BaseAIProvider {
|
|||||||
* @returns Promise resolving to formatted completion response
|
* @returns Promise resolving to formatted completion response
|
||||||
* @throws {Error} If API request fails
|
* @throws {Error} If API request fails
|
||||||
*/
|
*/
|
||||||
protected async doComplete<T = any>(params: CompletionParams<T>): Promise<CompletionResponse> {
|
protected async doComplete(params: CompletionParams): Promise<CompletionResponse<string>> {
|
||||||
if (!this.client) {
|
if (!this.client) {
|
||||||
throw new AIProviderError('OpenAI client not initialized', AIErrorType.INVALID_REQUEST);
|
throw new AIProviderError('OpenAI client not initialized', AIErrorType.INVALID_REQUEST);
|
||||||
}
|
}
|
||||||
@@ -537,7 +537,7 @@ export class OpenAIProvider extends BaseAIProvider {
|
|||||||
* @returns Formatted completion response
|
* @returns Formatted completion response
|
||||||
* @throws {AIProviderError} If response format is unexpected
|
* @throws {AIProviderError} If response format is unexpected
|
||||||
*/
|
*/
|
||||||
private formatCompletionResponse(response: OpenAI.Chat.Completions.ChatCompletion): CompletionResponse {
|
private formatCompletionResponse(response: OpenAI.Chat.Completions.ChatCompletion): CompletionResponse<string> {
|
||||||
const choice = response.choices[0];
|
const choice = response.choices[0];
|
||||||
if (!choice || !choice.message.content) {
|
if (!choice || !choice.message.content) {
|
||||||
throw new AIProviderError(
|
throw new AIProviderError(
|
||||||
|
|||||||
@@ -524,7 +524,7 @@ export class OpenWebUIProvider extends BaseAIProvider {
|
|||||||
* @param response - Raw OpenWebUI response
|
* @param response - Raw OpenWebUI response
|
||||||
* @returns Formatted completion response
|
* @returns Formatted completion response
|
||||||
*/
|
*/
|
||||||
private formatChatResponse(response: OpenWebUIChatResponse): CompletionResponse {
|
private formatChatResponse(response: OpenWebUIChatResponse): CompletionResponse<string> {
|
||||||
const choice = response.choices[0];
|
const choice = response.choices[0];
|
||||||
if (!choice || !choice.message.content) {
|
if (!choice || !choice.message.content) {
|
||||||
throw new AIProviderError(
|
throw new AIProviderError(
|
||||||
@@ -556,7 +556,7 @@ export class OpenWebUIProvider extends BaseAIProvider {
|
|||||||
* @param response - Raw Ollama response
|
* @param response - Raw Ollama response
|
||||||
* @returns Formatted completion response
|
* @returns Formatted completion response
|
||||||
*/
|
*/
|
||||||
private formatOllamaResponse(response: OllamaGenerateResponse): CompletionResponse {
|
private formatOllamaResponse(response: OllamaGenerateResponse): CompletionResponse<string> {
|
||||||
return {
|
return {
|
||||||
content: response.response,
|
content: response.response,
|
||||||
model: response.model,
|
model: response.model,
|
||||||
@@ -962,7 +962,7 @@ export class OpenWebUIProvider extends BaseAIProvider {
|
|||||||
* @returns Promise resolving to formatted completion response
|
* @returns Promise resolving to formatted completion response
|
||||||
* @throws {Error} If API request fails
|
* @throws {Error} If API request fails
|
||||||
*/
|
*/
|
||||||
protected async doComplete<T = any>(params: CompletionParams<T>): Promise<CompletionResponse> {
|
protected async doComplete(params: CompletionParams): Promise<CompletionResponse<string>> {
|
||||||
if (this.useOllamaProxy) {
|
if (this.useOllamaProxy) {
|
||||||
return this.completeWithOllama(params);
|
return this.completeWithOllama(params);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -89,9 +89,11 @@ export interface TokenUsage {
|
|||||||
/**
|
/**
|
||||||
* Response from an AI completion request
|
* Response from an AI completion request
|
||||||
*/
|
*/
|
||||||
export interface CompletionResponse {
|
export interface CompletionResponse<T = string> {
|
||||||
/** Generated content */
|
/** Generated content, either as a string or a typed object */
|
||||||
content: string;
|
content: T;
|
||||||
|
/** Raw response from the provider, if content is a typed object */
|
||||||
|
rawContent?: string;
|
||||||
/** Model used for generation */
|
/** Model used for generation */
|
||||||
model: string;
|
model: string;
|
||||||
/** Token usage statistics */
|
/** Token usage statistics */
|
||||||
@@ -220,55 +222,71 @@ export function createResponseType<T = any>(
|
|||||||
export function generateResponseTypePrompt(responseType: ResponseType): string {
|
export function generateResponseTypePrompt(responseType: ResponseType): string {
|
||||||
const { typeDefinition, description, example, strictJson } = responseType;
|
const { typeDefinition, description, example, strictJson } = responseType;
|
||||||
|
|
||||||
let prompt = `You must respond with a JSON object that matches the following TypeScript type definition:\n\n`;
|
let prompt = `You are an AI assistant that must respond with a JSON object. The JSON object must strictly adhere to the following TypeScript type definition:
|
||||||
prompt += `Type Definition:\n\`\`\`typescript\n${typeDefinition}\n\`\`\`\n\n`;
|
|
||||||
prompt += `Description: ${description}\n\n`;
|
`;
|
||||||
|
prompt += `Type Definition:
|
||||||
|
typescript
|
||||||
|
${typeDefinition}
|
||||||
|
|
||||||
|
|
||||||
|
`;
|
||||||
|
prompt += `Description: ${description}
|
||||||
|
|
||||||
|
`;
|
||||||
|
|
||||||
if (example) {
|
if (example) {
|
||||||
prompt += `Example:\n\`\`\`json\n${JSON.stringify(example, null, 2)}\n\`\`\`\n\n`;
|
prompt += `Example of the expected JSON output:
|
||||||
|
json
|
||||||
|
${JSON.stringify(example, null, 2)}
|
||||||
|
|
||||||
|
|
||||||
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strictJson) {
|
if (strictJson) {
|
||||||
prompt += `IMPORTANT: Your response must be valid JSON only. Do not include any text before or after the JSON object. Do not use markdown formatting.`;
|
prompt += `IMPORTANT: Your entire response must be a single, valid JSON object that conforms to the type definition above. Do not include any additional text, explanations, or markdown formatting before or after the JSON object.`;
|
||||||
} else {
|
} else {
|
||||||
prompt += `Your response should follow the structure defined above.`;
|
prompt += `Your response should contain a JSON object that follows the structure defined above.`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return prompt;
|
return prompt;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates that a response matches the expected type structure
|
* Parses and validates that a response matches the expected type structure
|
||||||
*
|
*
|
||||||
* @param response - The response content to validate
|
* @param response - The response content to validate
|
||||||
* @param responseType - The expected response type
|
* @param responseType - The expected response type
|
||||||
* @returns Object with validation result and parsed data
|
* @returns The parsed and validated data object
|
||||||
|
* @throws {AIProviderError} If parsing or validation fails
|
||||||
*/
|
*/
|
||||||
export function validateResponseType<T = any>(
|
export function parseAndValidateResponseType<T = any>(
|
||||||
response: string,
|
response: string,
|
||||||
responseType: ResponseType<T>
|
responseType: ResponseType<T>
|
||||||
): { isValid: boolean; data?: T; error?: string } {
|
): T {
|
||||||
try {
|
try {
|
||||||
// Parse JSON response
|
// Attempt to parse the JSON response
|
||||||
const parsed = JSON.parse(response);
|
const parsed = JSON.parse(response);
|
||||||
|
|
||||||
// Basic validation - in a real implementation, you might want to use
|
// Basic validation: ensure it's a non-null object.
|
||||||
// a schema validation library like zod or ajv for more thorough validation
|
// For more robust validation, a library like Zod or Ajv could be used
|
||||||
|
// based on the typeDefinition, but that's beyond the current scope.
|
||||||
if (typeof parsed !== 'object' || parsed === null) {
|
if (typeof parsed !== 'object' || parsed === null) {
|
||||||
return {
|
throw new Error('Response must be a JSON object');
|
||||||
isValid: false,
|
|
||||||
error: 'Response must be a JSON object'
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
// Here you could add more sophisticated validation if needed, e.g.,
|
||||||
isValid: true,
|
// checking keys against the responseType.typeDefinition
|
||||||
data: parsed as T
|
|
||||||
};
|
return parsed as T;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {
|
// Wrap parsing/validation errors in a standardized AIProviderError
|
||||||
isValid: false,
|
throw new AIProviderError(
|
||||||
error: `Invalid JSON: ${(error as Error).message}`
|
`Failed to parse or validate structured response: ${(error as Error).message}`,
|
||||||
};
|
AIErrorType.INVALID_REQUEST,
|
||||||
|
undefined,
|
||||||
|
error as Error
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,8 +2,8 @@
|
|||||||
* Tests for Claude Provider
|
* Tests for Claude Provider
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { describe, it, expect, beforeEach } from 'bun:test';
|
import { describe, it, expect, beforeEach, jest } from 'bun:test';
|
||||||
import { ClaudeProvider, AIProviderError, AIErrorType } from '../src/index.js';
|
import { ClaudeProvider, createClaudeProvider, createResponseType, parseAndValidateResponseType, AIProviderError, AIErrorType } from '../src/index.js';
|
||||||
|
|
||||||
describe('ClaudeProvider', () => {
|
describe('ClaudeProvider', () => {
|
||||||
let provider: ClaudeProvider;
|
let provider: ClaudeProvider;
|
||||||
@@ -135,4 +135,32 @@ describe('ClaudeProvider', () => {
|
|||||||
expect(result.messages).toHaveLength(2);
|
expect(result.messages).toHaveLength(2);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('structured responses', () => {
|
||||||
|
it('should handle structured responses correctly', async () => {
|
||||||
|
const provider = new ClaudeProvider({ apiKey: 'test-key' });
|
||||||
|
const mockResponse = {
|
||||||
|
content: JSON.stringify({ name: 'John Doe', age: 30 }),
|
||||||
|
model: 'claude-3-5-sonnet-20241022',
|
||||||
|
usage: { promptTokens: 10, completionTokens: 20, totalTokens: 30 },
|
||||||
|
id: 'test-id'
|
||||||
|
};
|
||||||
|
(provider as any).doComplete = jest.fn().mockResolvedValue(mockResponse);
|
||||||
|
(provider as any).initialized = true;
|
||||||
|
|
||||||
|
const responseType = createResponseType<{ name: string; age: number }>(
|
||||||
|
`{ name: string; age: number }`,
|
||||||
|
'A user profile'
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = await provider.complete<{ name: string; age: number }>({
|
||||||
|
messages: [{ role: 'user', content: 'test' }],
|
||||||
|
responseType
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.content).toEqual({ name: 'John Doe', age: 30 });
|
||||||
|
expect(response.rawContent).toBe(JSON.stringify({ name: 'John Doe', age: 30 }));
|
||||||
|
expect((provider as any).doComplete).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -2,8 +2,8 @@
|
|||||||
* Tests for Gemini Provider
|
* Tests for Gemini Provider
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { describe, it, expect, beforeEach } from 'bun:test';
|
import { describe, it, expect, beforeEach, jest } from 'bun:test';
|
||||||
import { GeminiProvider, AIProviderError, AIErrorType } from '../src/index.js';
|
import { GeminiProvider, createResponseType, AIProviderError, AIErrorType } from '../src/index.js';
|
||||||
|
|
||||||
describe('GeminiProvider', () => {
|
describe('GeminiProvider', () => {
|
||||||
let provider: GeminiProvider;
|
let provider: GeminiProvider;
|
||||||
@@ -353,4 +353,32 @@ describe('GeminiProvider', () => {
|
|||||||
}).toThrow('No candidates found in Gemini response');
|
}).toThrow('No candidates found in Gemini response');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('structured responses', () => {
|
||||||
|
it('should handle structured responses correctly', async () => {
|
||||||
|
const provider = new GeminiProvider({ apiKey: 'test-key' });
|
||||||
|
const mockResponse = {
|
||||||
|
content: JSON.stringify({ name: 'John Doe', age: 30 }),
|
||||||
|
model: 'gemini-1.5-pro',
|
||||||
|
usage: { promptTokens: 10, completionTokens: 20, totalTokens: 30 },
|
||||||
|
id: 'test-id'
|
||||||
|
};
|
||||||
|
(provider as any).doComplete = jest.fn().mockResolvedValue(mockResponse);
|
||||||
|
(provider as any).initialized = true;
|
||||||
|
|
||||||
|
const responseType = createResponseType<{ name: string; age: number }>
|
||||||
|
(`{ name: string; age: number }`,
|
||||||
|
'A user profile'
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = await provider.complete<{ name: string; age: number }>({
|
||||||
|
messages: [{ role: 'user', content: 'test' }],
|
||||||
|
responseType
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.content).toEqual({ name: 'John Doe', age: 30 });
|
||||||
|
expect(response.rawContent).toBe(JSON.stringify({ name: 'John Doe', age: 30 }));
|
||||||
|
expect((provider as any).doComplete).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -2,8 +2,8 @@
|
|||||||
* Tests for OpenAI Provider
|
* Tests for OpenAI Provider
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { describe, it, expect, beforeEach } from 'bun:test';
|
import { describe, it, expect, beforeEach, jest } from 'bun:test';
|
||||||
import { OpenAIProvider, AIProviderError, AIErrorType } from '../src/index.js';
|
import { OpenAIProvider, createResponseType, AIProviderError, AIErrorType } from '../src/index.js';
|
||||||
|
|
||||||
describe('OpenAIProvider', () => {
|
describe('OpenAIProvider', () => {
|
||||||
let provider: OpenAIProvider;
|
let provider: OpenAIProvider;
|
||||||
@@ -258,4 +258,32 @@ describe('OpenAIProvider', () => {
|
|||||||
}).toThrow('No content found in OpenAI response');
|
}).toThrow('No content found in OpenAI response');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('structured responses', () => {
|
||||||
|
it('should handle structured responses correctly', async () => {
|
||||||
|
const provider = new OpenAIProvider({ apiKey: 'test-key' });
|
||||||
|
const mockResponse = {
|
||||||
|
content: JSON.stringify({ name: 'John Doe', age: 30 }),
|
||||||
|
model: 'gpt-4',
|
||||||
|
usage: { promptTokens: 10, completionTokens: 20, totalTokens: 30 },
|
||||||
|
id: 'test-id'
|
||||||
|
};
|
||||||
|
(provider as any).doComplete = jest.fn().mockResolvedValue(mockResponse);
|
||||||
|
(provider as any).initialized = true;
|
||||||
|
|
||||||
|
const responseType = createResponseType<{ name: string; age: number }>
|
||||||
|
(`{ name: string; age: number }`,
|
||||||
|
'A user profile'
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = await provider.complete<{ name: string; age: number }>({
|
||||||
|
messages: [{ role: 'user', content: 'test' }],
|
||||||
|
responseType
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.content).toEqual({ name: 'John Doe', age: 30 });
|
||||||
|
expect(response.rawContent).toBe(JSON.stringify({ name: 'John Doe', age: 30 }));
|
||||||
|
expect((provider as any).doComplete).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -2,9 +2,9 @@
|
|||||||
* Tests for OpenWebUI provider implementation
|
* Tests for OpenWebUI provider implementation
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { describe, it, expect, beforeEach } from 'bun:test';
|
import { describe, it, expect, beforeEach, jest } from 'bun:test';
|
||||||
import { OpenWebUIProvider, type OpenWebUIConfig } from '../src/providers/openwebui.js';
|
import { OpenWebUIProvider, type OpenWebUIConfig } from '../src/providers/openwebui.js';
|
||||||
import { AIProviderError, AIErrorType, type CompletionParams } from '../src/types/index.js';
|
import { AIProviderError, AIErrorType, type CompletionParams, createResponseType } from '../src/types/index.js';
|
||||||
|
|
||||||
describe('OpenWebUIProvider', () => {
|
describe('OpenWebUIProvider', () => {
|
||||||
let provider: OpenWebUIProvider;
|
let provider: OpenWebUIProvider;
|
||||||
@@ -416,4 +416,32 @@ describe('OpenWebUIProvider', () => {
|
|||||||
await expect(provider.complete(params)).rejects.toThrow('Provider must be initialized before use');
|
await expect(provider.complete(params)).rejects.toThrow('Provider must be initialized before use');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('structured responses', () => {
|
||||||
|
it('should handle structured responses correctly', async () => {
|
||||||
|
const provider = new OpenWebUIProvider({ apiKey: 'test-key' });
|
||||||
|
const mockResponse = {
|
||||||
|
content: JSON.stringify({ name: 'John Doe', age: 30 }),
|
||||||
|
model: 'llama3.1',
|
||||||
|
usage: { promptTokens: 10, completionTokens: 20, totalTokens: 30 },
|
||||||
|
id: 'test-id'
|
||||||
|
};
|
||||||
|
(provider as any).doComplete = jest.fn().mockResolvedValue(mockResponse);
|
||||||
|
(provider as any).initialized = true;
|
||||||
|
|
||||||
|
const responseType = createResponseType<{ name: string; age: number }>
|
||||||
|
(`{ name: string; age: number }`,
|
||||||
|
'A user profile'
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = await provider.complete<{ name: string; age: number }>({
|
||||||
|
messages: [{ role: 'user', content: 'test' }],
|
||||||
|
responseType
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.content).toEqual({ name: 'John Doe', age: 30 });
|
||||||
|
expect(response.rawContent).toBe(JSON.stringify({ name: 'John Doe', age: 30 }));
|
||||||
|
expect((provider as any).doComplete).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user