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:
@@ -50,7 +50,11 @@ export class OpenAIService {
|
|||||||
private readonly openai: OpenAI;
|
private readonly openai: OpenAI;
|
||||||
private readonly bucket: string;
|
private readonly bucket: string;
|
||||||
|
|
||||||
private constructor(client: SupabaseClient, openai: OpenAI, bucket: string) {
|
private constructor(
|
||||||
|
client: SupabaseClient,
|
||||||
|
openai: OpenAI,
|
||||||
|
bucket: string,
|
||||||
|
) {
|
||||||
this.supabase = client;
|
this.supabase = client;
|
||||||
this.openai = openai;
|
this.openai = openai;
|
||||||
this.bucket = bucket;
|
this.bucket = bucket;
|
||||||
@@ -76,21 +80,41 @@ export class OpenAIService {
|
|||||||
return new OpenAIService(client, openai, bucket);
|
return new OpenAIService(client, openai, bucket);
|
||||||
}
|
}
|
||||||
|
|
||||||
async createConversation(metadata?: Record<string, string>) {
|
|
||||||
const conversation = await this.openai.conversations.create({
|
|
||||||
metadata,
|
|
||||||
});
|
|
||||||
return conversation;
|
|
||||||
}
|
|
||||||
|
|
||||||
async createStructuredResponse<TOutput = unknown>(
|
async createStructuredResponse<TOutput = unknown>(
|
||||||
options: StructuredResponseOptions,
|
options: StructuredResponseOptions,
|
||||||
files?: File[],
|
files?: File[],
|
||||||
): Promise<StructuredResponseResult<TOutput>> {
|
): Promise<StructuredResponseResult<TOutput>> {
|
||||||
try {
|
try {
|
||||||
const uploadedToStorage = await this.uploadFilesToStorage(files ?? []);
|
const uploadedToStorage = await this.uploadFilesToStorage(
|
||||||
|
files ?? [],
|
||||||
|
);
|
||||||
const openaiFileIds = await this.uploadFilesToOpenAI(files ?? []);
|
const openaiFileIds = await this.uploadFilesToOpenAI(files ?? []);
|
||||||
|
|
||||||
|
const newOptions = { ...options };
|
||||||
|
|
||||||
|
// Attach file references to the request as an extra user message
|
||||||
|
if (openaiFileIds.length > 0) {
|
||||||
|
const fileParts:
|
||||||
|
OpenAITypes.OpenAI.Responses.ResponseInputFile[] =
|
||||||
|
openaiFileIds.map((id) => ({
|
||||||
|
type: "input_file",
|
||||||
|
file_id: id,
|
||||||
|
}));
|
||||||
|
|
||||||
|
const arr = Array.isArray(options.input) ? options.input : [];
|
||||||
|
arr.push({
|
||||||
|
role: "user",
|
||||||
|
content: [
|
||||||
|
...fileParts,
|
||||||
|
{
|
||||||
|
type: "input_text",
|
||||||
|
text: "Usa estos archivos como referencia",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
newOptions.input = arr;
|
||||||
|
}
|
||||||
|
|
||||||
// Narrow to non-streaming response
|
// Narrow to non-streaming response
|
||||||
const openaiRaw = (await this.openai.responses.create(
|
const openaiRaw = (await this.openai.responses.create(
|
||||||
newOptions as OpenAITypes.OpenAI.Responses.ResponseCreateParamsNonStreaming,
|
newOptions as OpenAITypes.OpenAI.Responses.ResponseCreateParamsNonStreaming,
|
||||||
@@ -98,8 +122,7 @@ export class OpenAIService {
|
|||||||
|
|
||||||
const { model, id: responseId } = openaiRaw;
|
const { model, id: responseId } = openaiRaw;
|
||||||
const usage = openaiRaw?.usage ?? null;
|
const usage = openaiRaw?.usage ?? null;
|
||||||
const conversationId =
|
const conversationId = (
|
||||||
(
|
|
||||||
openaiRaw as OpenAITypes.OpenAI.Responses.Response & {
|
openaiRaw as OpenAITypes.OpenAI.Responses.Response & {
|
||||||
conversation_id?: string | null;
|
conversation_id?: string | null;
|
||||||
}
|
}
|
||||||
@@ -111,13 +134,14 @@ export class OpenAIService {
|
|||||||
|
|
||||||
// Prefer `output_text` if present (SDK convenience)
|
// Prefer `output_text` if present (SDK convenience)
|
||||||
const maybeOutputText = openaiRaw.output_text;
|
const maybeOutputText = openaiRaw.output_text;
|
||||||
if (typeof maybeOutputText === "string" && maybeOutputText.length > 0) {
|
if (
|
||||||
|
typeof maybeOutputText === "string" &&
|
||||||
|
maybeOutputText.length > 0
|
||||||
|
) {
|
||||||
outputText = maybeOutputText;
|
outputText = maybeOutputText;
|
||||||
try {
|
try {
|
||||||
output = JSON.parse(maybeOutputText) as TOutput;
|
output = JSON.parse(maybeOutputText) as TOutput;
|
||||||
} catch {
|
} catch { /* non-JSON text, keep as text only */ }
|
||||||
/* non-JSON text, keep as text only */
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Fallback: attempt to serialize `openaiRaw.output` into text
|
// Fallback: attempt to serialize `openaiRaw.output` into text
|
||||||
const maybeOutput = openaiRaw.output as unknown;
|
const maybeOutput = openaiRaw.output as unknown;
|
||||||
@@ -125,9 +149,7 @@ export class OpenAIService {
|
|||||||
try {
|
try {
|
||||||
outputText = JSON.stringify(maybeOutput);
|
outputText = JSON.stringify(maybeOutput);
|
||||||
output = maybeOutput as TOutput;
|
output = maybeOutput as TOutput;
|
||||||
} catch {
|
} catch { /* ignore */ }
|
||||||
/* ignore */
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,7 +208,9 @@ export class OpenAIService {
|
|||||||
ids.push(created.id);
|
ids.push(created.id);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`OpenAI file upload failed: ${(e as Error)?.message ?? String(e)}`,
|
`OpenAI file upload failed: ${
|
||||||
|
(e as Error)?.message ?? String(e)
|
||||||
|
}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
// Setup type definitions for built-in Supabase Runtime APIs
|
// Setup type definitions for built-in Supabase Runtime APIs
|
||||||
import "@supabase/functions-js/edge-runtime.d.ts";
|
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";
|
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
|
||||||
|
|
||||||
type WebhookPayload = {
|
type WebhookPayload = {
|
||||||
@@ -15,6 +13,9 @@ type WebhookPayload = {
|
|||||||
const SUPABASE_URL = Deno.env.get("SUPABASE_URL") ?? "";
|
const SUPABASE_URL = Deno.env.get("SUPABASE_URL") ?? "";
|
||||||
const SUPABASE_SERVICE_ROLE_KEY =
|
const SUPABASE_SERVICE_ROLE_KEY =
|
||||||
Deno.env.get("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_SCHEMA = "public";
|
||||||
const ALLOWED_TABLES = new Set(["planes_estudio", "asignaturas"]);
|
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>) {
|
function jsonResponse(status: number, body: Record<string, unknown>) {
|
||||||
return new Response(JSON.stringify(body), {
|
return new Response(JSON.stringify(body), {
|
||||||
status,
|
status,
|
||||||
headers: { "Content-Type": "application/json", ...corsHeaders },
|
headers: { "Content-Type": "application/json" },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createConversationId(
|
async function createConversationId(metadata: Record<string, string>) {
|
||||||
openaiService: OpenAIService,
|
const response = await fetch(`${OPENAI_BASE_URL}/conversations`, {
|
||||||
metadata: Record<string, string>,
|
method: "POST",
|
||||||
) {
|
headers: {
|
||||||
const conversation = await openaiService.createConversation(metadata);
|
"Content-Type": "application/json",
|
||||||
const conversationId = conversation?.id as string | undefined;
|
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) {
|
if (!conversationId) {
|
||||||
throw new Error("OpenAI response missing conversation id");
|
throw new Error("OpenAI response missing conversation id");
|
||||||
@@ -53,9 +65,8 @@ Deno.serve(async (req) => {
|
|||||||
return jsonResponse(500, { error: "Supabase env vars missing" });
|
return jsonResponse(500, { error: "Supabase env vars missing" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const openaiService = OpenAIService.fromEnv();
|
if (!OPENAI_API_KEY) {
|
||||||
if (!(openaiService instanceof OpenAIService)) {
|
return jsonResponse(500, { error: "OPENAI_API_KEY missing" });
|
||||||
return jsonResponse(500, { error: openaiService.message });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let payload: WebhookPayload;
|
let payload: WebhookPayload;
|
||||||
@@ -97,7 +108,7 @@ Deno.serve(async (req) => {
|
|||||||
|
|
||||||
let conversationId: string;
|
let conversationId: string;
|
||||||
try {
|
try {
|
||||||
conversationId = await createConversationId(openaiService, {
|
conversationId = await createConversationId({
|
||||||
table,
|
table,
|
||||||
record_id: String(recordId),
|
record_id: String(recordId),
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user