From 453c43a375e3fb4e6f872a9008ea6e7e9f6e594e Mon Sep 17 00:00:00 2001 From: Guillermo Arrieta Medina Date: Mon, 19 Jan 2026 16:33:08 -0600 Subject: [PATCH] Antes de cambio de ai-structured --- supabase/functions/ai-generate-plan/index.ts | 33 +++++++++++++++----- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/supabase/functions/ai-generate-plan/index.ts b/supabase/functions/ai-generate-plan/index.ts index 4e59599..11372ac 100644 --- a/supabase/functions/ai-generate-plan/index.ts +++ b/supabase/functions/ai-generate-plan/index.ts @@ -21,7 +21,9 @@ Deno.serve(async (req) => { const method = req.method; if (method !== "POST") { console.error( - `[${Date.now()}][${functionName}]: Invalid method: ${method}`, + `[${ + new Date().toISOString() + }][${functionName}]: Invalid method: ${method}`, ); return new Response( JSON.stringify({ error: "Method not allowed" }), @@ -36,7 +38,9 @@ Deno.serve(async (req) => { req.headers.get("authorization"); if (!authHeaderRaw) { console.error( - `[${Date.now()}][${functionName}]: Missing Authorization header`, + `[${ + new Date().toISOString() + }][${functionName}]: Missing Authorization header`, ); return new Response( JSON.stringify({ error: "Authorization header missing" }), @@ -50,7 +54,9 @@ Deno.serve(async (req) => { const contentType = (req.headers.get("content-type") || "").toLowerCase(); if (!contentType.startsWith("multipart/form-data")) { console.error( - `[${Date.now()}][${functionName}]: Unsupported content type: ${contentType}`, + `[${ + new Date().toISOString() + }][${functionName}]: Unsupported content type: ${contentType}`, ); return new Response( JSON.stringify({ error: "Unsupported content type" }), @@ -75,7 +81,15 @@ Deno.serve(async (req) => { }, }); - // TODO: iniciar sesion con el usuario e implementar RLS + // TODO: quitar hardcode de usuario + const { data: user, error: userError } = await supabaseAnon.auth + .signInWithPassword({ + email: "guillermo.arrieta@lasalle.mx", + password: "admin", + }); + if (userError) { + throw new Error("Error authenticating user: " + userError.message); + } const SERVICE_ROLE_KEY = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY"); if (!SERVICE_ROLE_KEY) { @@ -91,7 +105,7 @@ Deno.serve(async (req) => { const validation = parseAndValidate(formData); if (!validation.success) { console.error( - `[${Date.now()}][${functionName}]: Validation errors:`, + `[${new Date().toISOString()}][${functionName}]: Validation errors:`, validation.errors, ); return new Response( @@ -128,7 +142,7 @@ Deno.serve(async (req) => { { role: "user", content: userPrompt }, ], }, - estructuraPlan, + structured: estructuraPlan.definicion, references: { openaiFileIds: payload.iaConfig.archivosReferencia, vectorStoreIds: payload.iaConfig.repositoriosIds, @@ -145,6 +159,9 @@ Deno.serve(async (req) => { const { data: aiJson, error } = await supabaseService.functions.invoke( "ai-structured", { + headers: { + Authorization: `Bearer ${user.session?.access_token}`, + }, method: "POST", body: aiStructuredFormData, }, @@ -244,7 +261,9 @@ Deno.serve(async (req) => { // Log full error server-side for diagnostics if (error instanceof Error) { console.error( - `[${Date.now()}][${functionName}]: Request handler error:`, + `[${ + new Date().toISOString() + }][${functionName}]: Request handler error:`, error.message, ); }