revert 1af0358e65
Deploy Function / deploy (push) Failing after 43s
Deploy Function / deploy (push) Failing after 43s
revert Merge pull request 'REFACTOR OpenAI service to manage env and convo creation' (#12) from issue/9-hacer-una-funcin-que-inserte-el-id-de-conversacin into main Reviewed-on: AlexRG/genesis-2#12
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
// Setup type definitions for built-in Supabase Runtime APIs
|
||||
import "@supabase/functions-js/edge-runtime.d.ts";
|
||||
import { corsHeaders } from "../_shared/cors.ts";
|
||||
import { OpenAIService } from "../_shared/openai-service.ts";
|
||||
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
|
||||
|
||||
type WebhookPayload = {
|
||||
@@ -15,6 +13,9 @@ type WebhookPayload = {
|
||||
const SUPABASE_URL = Deno.env.get("SUPABASE_URL") ?? "";
|
||||
const SUPABASE_SERVICE_ROLE_KEY =
|
||||
Deno.env.get("SUPABASE_SERVICE_ROLE_KEY") ?? "";
|
||||
const OPENAI_API_KEY = Deno.env.get("OPENAI_API_KEY") ?? "";
|
||||
const OPENAI_BASE_URL =
|
||||
Deno.env.get("OPENAI_BASE_URL") ?? "https://api.openai.com/v1";
|
||||
|
||||
const ALLOWED_SCHEMA = "public";
|
||||
const ALLOWED_TABLES = new Set(["planes_estudio", "asignaturas"]);
|
||||
@@ -26,16 +27,27 @@ const supabase = createClient(SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY, {
|
||||
function jsonResponse(status: number, body: Record<string, unknown>) {
|
||||
return new Response(JSON.stringify(body), {
|
||||
status,
|
||||
headers: { "Content-Type": "application/json", ...corsHeaders },
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
async function createConversationId(
|
||||
openaiService: OpenAIService,
|
||||
metadata: Record<string, string>,
|
||||
) {
|
||||
const conversation = await openaiService.createConversation(metadata);
|
||||
const conversationId = conversation?.id as string | undefined;
|
||||
async function createConversationId(metadata: Record<string, string>) {
|
||||
const response = await fetch(`${OPENAI_BASE_URL}/conversations`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${OPENAI_API_KEY}`,
|
||||
},
|
||||
body: JSON.stringify({ metadata }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(`OpenAI error: ${response.status} ${errorText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const conversationId = data?.id as string | undefined;
|
||||
|
||||
if (!conversationId) {
|
||||
throw new Error("OpenAI response missing conversation id");
|
||||
@@ -53,9 +65,8 @@ Deno.serve(async (req) => {
|
||||
return jsonResponse(500, { error: "Supabase env vars missing" });
|
||||
}
|
||||
|
||||
const openaiService = OpenAIService.fromEnv();
|
||||
if (!(openaiService instanceof OpenAIService)) {
|
||||
return jsonResponse(500, { error: openaiService.message });
|
||||
if (!OPENAI_API_KEY) {
|
||||
return jsonResponse(500, { error: "OPENAI_API_KEY missing" });
|
||||
}
|
||||
|
||||
let payload: WebhookPayload;
|
||||
@@ -97,7 +108,7 @@ Deno.serve(async (req) => {
|
||||
|
||||
let conversationId: string;
|
||||
try {
|
||||
conversationId = await createConversationId(openaiService, {
|
||||
conversationId = await createConversationId({
|
||||
table,
|
||||
record_id: String(recordId),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user