From 09a08628967c09817074eaae84381020d50b6b4b Mon Sep 17 00:00:00 2001 From: jank Date: Thu, 4 Sep 2025 15:28:43 +0200 Subject: [PATCH] chore: fix some stuff idk --- README.md | 8 ----- examples/structured-response-types.ts | 32 ----------------- src/types/index.ts | 51 +++++++++------------------ 3 files changed, 16 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index 07c9122..db04b87 100644 --- a/README.md +++ b/README.md @@ -103,14 +103,6 @@ interface ProductAnalysis { // 2. Create a ResponseType object const productAnalysisType = createResponseType( - `{ - productName: string; - priceRange: 'budget' | 'mid-range' | 'premium'; - pros: string[]; - cons: string[]; - overallRating: number; - recommendation: 'buy' | 'consider' | 'avoid'; - }`, 'A comprehensive product analysis with pros, cons, rating, and recommendation' ); diff --git a/examples/structured-response-types.ts b/examples/structured-response-types.ts index 2040eda..857f8c4 100644 --- a/examples/structured-response-types.ts +++ b/examples/structured-response-types.ts @@ -31,16 +31,6 @@ interface UserProfile { } const userProfileType = createResponseType( - `{ - name: string; - age: number; - email: string; - preferences: { - theme: 'light' | 'dark'; - notifications: boolean; - }; - skills: string[]; - }`, 'A user profile with personal information, preferences, and skills', { name: 'John Doe', @@ -64,16 +54,6 @@ interface ProductAnalysis { } const productAnalysisType = createResponseType( - `{ - productName: string; - category: string; - priceRange: 'budget' | 'mid-range' | 'premium'; - pros: string[]; - cons: string[]; - overallRating: number; - recommendation: 'buy' | 'consider' | 'avoid'; - reasoning: string; - }`, 'A comprehensive product analysis with pros, cons, rating, and recommendation', { productName: 'Example Product', @@ -102,18 +82,6 @@ interface CodeReview { } const codeReviewType = createResponseType( - `{ - overallScore: number; - issues: Array<{ - type: 'error' | 'warning' | 'suggestion'; - line?: number; - message: string; - severity: 'low' | 'medium' | 'high'; - }>; - strengths: string[]; - improvements: string[]; - summary: string; - }`, 'A comprehensive code review with scoring, issues, and recommendations', { overallScore: 8, diff --git a/src/types/index.ts b/src/types/index.ts index c4f1124..c3d650f 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -41,7 +41,7 @@ export interface AIMessage { */ export interface ResponseType { /** The TypeScript type definition as a string */ - typeDefinition: string; + typeDefinition?: string; /** Human-readable description of the expected response format */ description: string; /** Example of the expected response structure */ @@ -164,14 +164,13 @@ export interface ProviderInfo { capabilities?: Record; } -// ============================================================================ +// ============================================================================ // RESPONSE TYPE UTILITIES -// ============================================================================ +// ============================================================================ /** * Creates a response type definition for structured AI outputs * - * @param typeDefinition - TypeScript type definition as a string * @param description - Human-readable description of the expected format * @param example - Optional example of the expected response structure * @param strictJson - Whether to enforce strict JSON formatting (default: true) @@ -180,15 +179,6 @@ export interface ProviderInfo { * @example * ```typescript * const userType = createResponseType( - * `{ - * name: string; - * age: number; - * email: string; - * preferences: { - * theme: 'light' | 'dark'; - * notifications: boolean; - * }; - * }`, * 'A user profile with personal information and preferences', * { * name: 'John Doe', @@ -200,13 +190,11 @@ export interface ProviderInfo { * ``` */ export function createResponseType( - typeDefinition: string, description: string, example?: T, strictJson: boolean = true ): ResponseType { return { - typeDefinition: typeDefinition.trim(), description, example, strictJson @@ -222,32 +210,25 @@ export function createResponseType( export function generateResponseTypePrompt(responseType: ResponseType): string { const { typeDefinition, description, example, strictJson } = responseType; - 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: + let prompt = 'You are an AI assistant that must respond with a JSON object.'; -`; - prompt += `Type Definition: -typescript -${typeDefinition} - - -`; - prompt += `Description: ${description} - -`; + if (typeDefinition) { + prompt += ' The JSON object must strictly adhere to the following TypeScript type definition:\n\n'; + prompt += 'Type Definition:\n```typescript\n' + typeDefinition + '\n```\n\n'; + } else { + prompt += '\n\n'; + } + + prompt += 'Description: ' + description + '\n\n'; if (example) { - prompt += `Example of the expected JSON output: -json -${JSON.stringify(example, null, 2)} - - -`; + prompt += 'Example of the expected JSON output:\n```json\n' + JSON.stringify(example, null, 2) + '\n```\n\n'; } if (strictJson) { - 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.`; + prompt += 'IMPORTANT: Your entire response must be a single, valid JSON object. Do not include any additional text, explanations, or markdown formatting before or after the JSON object.'; } else { - prompt += `Your response should contain a JSON object that follows the structure defined above.`; + prompt += 'Your response should contain a JSON object that follows the structure defined above.'; } return prompt; @@ -289,4 +270,4 @@ export function parseAndValidateResponseType( error as Error ); } -} \ No newline at end of file +}