Add create-chat-conversation function and related configurations

This commit is contained in:
2026-02-03 09:26:30 -06:00
parent e1d865e6c3
commit e024057e47
6 changed files with 163 additions and 29 deletions
+11
View File
@@ -15,3 +15,14 @@ entrypoint = "./functions/ai-generate-plan/index.ts"
# enabled = false
# verify_jwt = false
# import_map = "./functions/ai-structured/deno.json"
[functions.create-chat-conversation]
enabled = true
verify_jwt = true
import_map = "./functions/create-chat-conversation/deno.json"
# Uncomment to specify a custom file path to the entrypoint.
# Supported file extensions are: .ts, .js, .mjs, .jsx, .tsx
entrypoint = "./functions/create-chat-conversation/index.ts"
# Specifies static files to be bundled with the function. Supports glob patterns.
# For example, if you want to serve static HTML pages in your function:
# static_files = [ "./functions/create-chat-conversation/*.html" ]
@@ -0,0 +1,3 @@
# Configuration for private npm package dependencies
# For more information on using private registries with Edge Functions, see:
# https://supabase.com/docs/guides/functions/import-maps#importing-from-private-registries
@@ -0,0 +1,5 @@
{
"imports": {
"@supabase/functions-js": "jsr:@supabase/functions-js@^2"
}
}
@@ -0,0 +1,134 @@
// Setup type definitions for built-in Supabase Runtime APIs
import "@supabase/functions-js/edge-runtime.d.ts";
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
type WebhookPayload = {
type: "INSERT" | "UPDATE" | "DELETE";
table: string;
schema: string;
record: Record<string, unknown> | null;
old_record: Record<string, unknown> | null;
};
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"]);
const supabase = createClient(SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY, {
auth: { persistSession: false },
});
function jsonResponse(status: number, body: Record<string, unknown>) {
return new Response(JSON.stringify(body), {
status,
headers: { "Content-Type": "application/json" },
});
}
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");
}
return conversationId;
}
Deno.serve(async (req) => {
if (req.method !== "POST") {
return jsonResponse(405, { error: "Method not allowed" });
}
if (!SUPABASE_URL || !SUPABASE_SERVICE_ROLE_KEY) {
return jsonResponse(500, { error: "Supabase env vars missing" });
}
if (!OPENAI_API_KEY) {
return jsonResponse(500, { error: "OPENAI_API_KEY missing" });
}
let payload: WebhookPayload;
try {
payload = await req.json();
} catch {
return jsonResponse(400, { error: "Invalid JSON body" });
}
const { type, table, schema, record } = payload;
if (schema !== ALLOWED_SCHEMA || !ALLOWED_TABLES.has(table)) {
return jsonResponse(400, { error: "Table not allowed" });
}
if (type !== "INSERT") {
return jsonResponse(200, { status: "ignored", reason: "event_type" });
}
if (!record || typeof record !== "object") {
return jsonResponse(400, { error: "Missing record" });
}
const recordId = record["id"] as string | number | undefined;
if (!recordId) {
return jsonResponse(400, { error: "Record id missing" });
}
const existingConversationId = record["conversation_id"] as
| string
| null
| undefined;
if (existingConversationId) {
return jsonResponse(200, {
status: "skipped",
conversation_id: existingConversationId,
});
}
let conversationId: string;
try {
conversationId = await createConversationId({
table,
record_id: String(recordId),
});
} catch (error) {
return jsonResponse(502, { error: (error as Error).message });
}
const { error } = await supabase
.from(table)
.update({ conversation_id: conversationId })
.eq("id", recordId);
if (error) {
return jsonResponse(500, { error: error.message });
}
return jsonResponse(200, {
status: "updated",
table,
record_id: recordId,
conversation_id: conversationId,
});
});