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
*/
export type MessageRole = 'system' | 'user' | 'assistant';
export type MessageRole = "system" | "user" | "assistant";
/**
* Individual message in a conversation
@@ -122,13 +122,13 @@ export interface CompletionChunk {
* Error types that can occur during AI operations
*/
export enum AIErrorType {
AUTHENTICATION = 'authentication',
RATE_LIMIT = 'rate_limit',
INVALID_REQUEST = 'invalid_request',
MODEL_NOT_FOUND = 'model_not_found',
NETWORK = 'network',
TIMEOUT = 'timeout',
UNKNOWN = 'unknown'
AUTHENTICATION = "authentication",
RATE_LIMIT = "rate_limit",
INVALID_REQUEST = "invalid_request",
MODEL_NOT_FOUND = "model_not_found",
NETWORK = "network",
TIMEOUT = "timeout",
UNKNOWN = "unknown",
}
/**
@@ -139,10 +139,10 @@ export class AIProviderError extends Error {
message: string,
public type: AIErrorType,
public statusCode?: number,
public originalError?: Error
public originalError?: Error,
) {
super(message);
this.name = 'AIProviderError';
this.name = "AIProviderError";
}
}
@@ -192,12 +192,12 @@ export interface ProviderInfo {
export function createResponseType<T = any>(
description: string,
example?: T,
strictJson: boolean = true
strictJson: boolean = true,
): ResponseType<T> {
return {
description,
example,
strictJson
strictJson,
};
}
@@ -210,25 +210,32 @@ 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.';
let prompt = "You are an AI assistant that must respond with a JSON object.";
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';
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 += "\n\n";
}
prompt += 'Description: ' + description + '\n\n';
prompt += "Description: " + description + "\n\n";
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) {
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 {
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;
@@ -244,17 +251,19 @@ export function generateResponseTypePrompt(responseType: ResponseType): string {
*/
export function parseAndValidateResponseType<T = any>(
response: string,
responseType: ResponseType<T>
responseType: ResponseType<T>,
): T {
try {
// 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.
// 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) {
throw new Error('Response must be a JSON object');
if (typeof parsed !== "object" || parsed === null) {
throw new Error("Response must be a JSON object");
}
// 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}`,
AIErrorType.INVALID_REQUEST,
undefined,
error as Error
error as Error,
);
}
}