chore: fix some stuff idk
This commit is contained in:
@@ -41,7 +41,7 @@ export interface AIMessage {
|
||||
*/
|
||||
export interface ResponseType<T = any> {
|
||||
/** 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<string, any>;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// ============================================================================
|
||||
// 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<T = any>(
|
||||
typeDefinition: string,
|
||||
description: string,
|
||||
example?: T,
|
||||
strictJson: boolean = true
|
||||
): ResponseType<T> {
|
||||
return {
|
||||
typeDefinition: typeDefinition.trim(),
|
||||
description,
|
||||
example,
|
||||
strictJson
|
||||
@@ -222,32 +210,25 @@ export function createResponseType<T = any>(
|
||||
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<T = any>(
|
||||
error as Error
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user