a70f0c52a9
CI / test (pull_request) Failing after 8s
- Added CORS handling for the create-chat-conversation function. - Implemented health check endpoint for the function. - Created endpoints for managing conversations and messages with OpenAI. - Added error handling and response formatting for better API usability. - Introduced utility functions for environment variable management and Supabase client creation. - Enhanced schema handling for structured responses from OpenAI. - Implemented conversation archiving and retrieval logic.
58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import { HttpError } from "./errors.ts";
|
|
|
|
export function pickSchemaFields(
|
|
definicion: any,
|
|
campos?: string[],
|
|
) {
|
|
if (!definicion || definicion.type !== "object" || !definicion.properties) {
|
|
return definicion;
|
|
}
|
|
|
|
const extra = {
|
|
properties: {
|
|
"ai-message": {
|
|
type: "string",
|
|
description:
|
|
"Mensaje breve para el usuario final confirmando qué se mejoró y qué se hizo.",
|
|
examples: [
|
|
"Listo: mejoré la redacción del perfil de ingreso y propuse un tema de investigación alineado al plan.",
|
|
],
|
|
},
|
|
},
|
|
};
|
|
|
|
let out = structuredClone(definicion);
|
|
|
|
// Si piden campos, filtramos propiedades/required a esos campos
|
|
if (Array.isArray(campos) && campos.length > 0) {
|
|
const entries = Object.entries(out.properties).filter(([k]) =>
|
|
campos.includes(k)
|
|
);
|
|
out.properties = Object.fromEntries(entries);
|
|
if (Array.isArray(out.required)) {
|
|
out.required = out.required.filter((k: string) => campos.includes(k));
|
|
}
|
|
}
|
|
|
|
// Siempre agregamos ai-message
|
|
out.properties = { ...out.properties, ...extra.properties };
|
|
out.required = Array.isArray(out.required)
|
|
? [...new Set([...out.required, ...Object.keys(extra.properties)])]
|
|
: Object.keys(extra.properties);
|
|
|
|
return out;
|
|
}
|
|
|
|
export function safePlanForPrompt(plan: any) {
|
|
const copy = structuredClone(plan);
|
|
if (copy?.estructuras_plan) delete copy.estructuras_plan;
|
|
return copy;
|
|
}
|
|
|
|
export function assertUuid(v: string, name: string) {
|
|
// validación ligera
|
|
if (!v || typeof v !== "string" || v.length < 10) {
|
|
throw new HttpError(400, "bad_input", `Invalid ${name}`);
|
|
}
|
|
}
|