From 411e0f21541722b5e88b0d12cb50aa07a61364bb Mon Sep 17 00:00:00 2001 From: Alejandro Rosales Date: Tue, 13 Jan 2026 11:10:41 -0600 Subject: [PATCH] Add deno.lock file and refactor AI function to streamline OpenAI integration --- deno.lock | 25 +++++++++++++++ supabase/functions/ai-structured/index.ts | 35 +++++++++++---------- testers/ai-structured.ts | 38 +---------------------- 3 files changed, 44 insertions(+), 54 deletions(-) create mode 100644 deno.lock diff --git a/deno.lock b/deno.lock new file mode 100644 index 0000000..4a149c6 --- /dev/null +++ b/deno.lock @@ -0,0 +1,25 @@ +{ + "version": "5", + "specifiers": { + "jsr:@openai/openai@*": "6.9.0", + "jsr:@supabase/functions-js@*": "2.90.1" + }, + "jsr": { + "@openai/openai@6.9.0": { + "integrity": "d1828101f5a782a75c25805c4e06c1316d9c19f422d84755fb908046184c3ef6" + }, + "@supabase/functions-js@2.90.1": { + "integrity": "9a077ecf42aa84594ef3aac621d643e15ffaa9506a4c051e35c93f127ffdb035" + } + }, + "workspace": { + "packageJson": { + "dependencies": [ + "npm:@supabase/supabase-js@^2.90.1", + "npm:@types/bun@latest", + "npm:supabase-js@^1.0.4", + "npm:supabase@^2.72.6" + ] + } + } +} diff --git a/supabase/functions/ai-structured/index.ts b/supabase/functions/ai-structured/index.ts index eee89e2..bcdfe8d 100644 --- a/supabase/functions/ai-structured/index.ts +++ b/supabase/functions/ai-structured/index.ts @@ -1,22 +1,23 @@ -// Setup type definitions for built-in Supabase Runtime APIs -import "jsr:@supabase/functions-js/edge-runtime.d.ts"; -import { load } from "jsr:@std/dotenv"; +import OpenAI from 'jsr:@openai/openai'; -interface reqPayload { - name: string; -} -console.info('server started'); +Deno.serve(async (req) => { + const apiKey = Deno.env.get('OPENAI_API_KEY') + const openai = new OpenAI({ + apiKey: apiKey, + }) -Deno.serve(async (req: Request) => { + // Documentation here: https://github.com/openai/openai-node + const response = await openai.responses.create({ + model: 'gpt-5-nano', + instructions: 'You are a coding assistant that talks like a pirate', + input: 'Are semicolons optional in JavaScript?', + }); + const reply = response.output_text - const { name }: reqPayload = await req.json(); - const data = { - message: Deno.env.toObject(), - }; + - return new Response( - JSON.stringify(data), - { headers: { 'Content-Type': 'application/json', 'Connection': 'keep-alive' }} - ); -}); \ No newline at end of file + return new Response(reply, { + headers: { 'Content-Type': 'text/plain' }, + }) +}) \ No newline at end of file diff --git a/testers/ai-structured.ts b/testers/ai-structured.ts index 494cafe..9f630c4 100644 --- a/testers/ai-structured.ts +++ b/testers/ai-structured.ts @@ -16,43 +16,7 @@ try { const response = await supabase.functions.invoke("ai-structured", { body: { - response: { - model: "gpt-5", - input: [ - { role: "system", content: "Responde SIEMPRE en JSON válido." }, - { role: "user", content: "Dame 3 ideas de proyecto de IA para educación." }, - ], - /* conversation: "conv_...", */ // opcional - }, - structured: { - type: "json_schema", - name: "ideas", - strict: true, - schema: { - type: "object", - properties: { - ideas: { - type: "array", - items: { - type: "object", - properties: { - titulo: { type: "string" }, - descripcion: { type: "string" } - }, - required: ["titulo", "descripcion"], - additionalProperties: false - } - } - }, - required: ["ideas"], - additionalProperties: false - } - }, - references: { - /* vectorStoreIds: ["vs_..."], */ // opcional (file_search) ✅ - /* openaiFileIds: ["file_..."], */ // opcional (input_file) ✅ - }, - usarMCP: false, // opcional ✅ + query: "Escribe un poema sobre la primavera en español", }, }); console.log({ d: response.data, e: response.error });