chore: Remove json markdown stuff

This commit is contained in:
2025-09-04 15:50:32 +02:00
parent 027a2ba2ed
commit 901666b513

View File

@@ -22,7 +22,7 @@ export interface AIProviderConfig {
/** /**
* Message role types supported by AI providers * Message role types supported by AI providers
*/ */
export type MessageRole = 'system' | 'user' | 'assistant'; export type MessageRole = "system" | "user" | "assistant";
/** /**
* Individual message in a conversation * Individual message in a conversation
@@ -122,13 +122,13 @@ export interface CompletionChunk {
* Error types that can occur during AI operations * Error types that can occur during AI operations
*/ */
export enum AIErrorType { export enum AIErrorType {
AUTHENTICATION = 'authentication', AUTHENTICATION = "authentication",
RATE_LIMIT = 'rate_limit', RATE_LIMIT = "rate_limit",
INVALID_REQUEST = 'invalid_request', INVALID_REQUEST = "invalid_request",
MODEL_NOT_FOUND = 'model_not_found', MODEL_NOT_FOUND = "model_not_found",
NETWORK = 'network', NETWORK = "network",
TIMEOUT = 'timeout', TIMEOUT = "timeout",
UNKNOWN = 'unknown' UNKNOWN = "unknown",
} }
/** /**
@@ -139,10 +139,10 @@ export class AIProviderError extends Error {
message: string, message: string,
public type: AIErrorType, public type: AIErrorType,
public statusCode?: number, public statusCode?: number,
public originalError?: Error public originalError?: Error,
) { ) {
super(message); super(message);
this.name = 'AIProviderError'; this.name = "AIProviderError";
} }
} }
@@ -192,12 +192,12 @@ export interface ProviderInfo {
export function createResponseType<T = any>( export function createResponseType<T = any>(
description: string, description: string,
example?: T, example?: T,
strictJson: boolean = true strictJson: boolean = true,
): ResponseType<T> { ): ResponseType<T> {
return { return {
description, description,
example, example,
strictJson strictJson,
}; };
} }
@@ -210,25 +210,32 @@ 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 are an AI assistant that must respond with a JSON object.'; let prompt = "You are an AI assistant that must respond with a JSON object.";
if (typeDefinition) { if (typeDefinition) {
prompt += ' The JSON object must strictly adhere to the following TypeScript type definition:\n\n'; prompt +=
prompt += 'Type Definition:\n```typescript\n' + typeDefinition + '\n```\n\n'; " 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 { } else {
prompt += '\n\n'; prompt += "\n\n";
} }
prompt += 'Description: ' + description + '\n\n'; prompt += "Description: " + description + "\n\n";
if (example) { if (example) {
prompt += 'Example of the expected JSON output:\n```json\n' + JSON.stringify(example, null, 2) + '\n```\n\n'; prompt +=
"Example of the expected JSON output:\n```json\n" +
JSON.stringify(example, null, 2) +
"\n```\n\n";
} }
if (strictJson) { if (strictJson) {
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.'; 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 { } 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; return prompt;
@@ -244,17 +251,19 @@ export function generateResponseTypePrompt(responseType: ResponseType): string {
*/ */
export function parseAndValidateResponseType<T = any>( export function parseAndValidateResponseType<T = any>(
response: string, response: string,
responseType: ResponseType<T> responseType: ResponseType<T>,
): T { ): T {
try { try {
// Attempt to parse the JSON response // Attempt to parse the JSON response
const parsed = JSON.parse(response); const parsed = JSON.parse(
response.replaceAll("```json", "").replaceAll("```", ""),
);
// Basic validation: ensure it's a non-null object. // Basic validation: ensure it's a non-null object.
// For more robust validation, a library like Zod or Ajv could be used // For more robust validation, a library like Zod or Ajv could be used
// based on the typeDefinition, but that's beyond the current scope. // based on the typeDefinition, but that's beyond the current scope.
if (typeof parsed !== 'object' || parsed === null) { if (typeof parsed !== "object" || parsed === null) {
throw new Error('Response must be a JSON object'); throw new Error("Response must be a JSON object");
} }
// Here you could add more sophisticated validation if needed, e.g., // Here you could add more sophisticated validation if needed, e.g.,
@@ -267,7 +276,7 @@ export function parseAndValidateResponseType<T = any>(
`Failed to parse or validate structured response: ${(error as Error).message}`, `Failed to parse or validate structured response: ${(error as Error).message}`,
AIErrorType.INVALID_REQUEST, AIErrorType.INVALID_REQUEST,
undefined, undefined,
error as Error error as Error,
); );
} }
} }