openai-webhook-responces recibe peticiones y hace log de las responses

This commit is contained in:
2026-02-25 15:24:57 -06:00
parent 8aeb1d0a64
commit 9263587f0b
11 changed files with 183 additions and 46 deletions
+47 -17
View File
@@ -14,7 +14,23 @@
" export: true,\n",
"});\n",
"\n",
"const openai = new OpenAI();"
"const openai = new OpenAI();\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3430f0fd",
"metadata": {},
"outputs": [],
"source": [
"{\n",
" const response = await openai.responses.create({\n",
" model: \"gpt-5-nano\",\n",
" input: \"Cuál es el sentido de la vida?\",\n",
" background: true,\n",
" });\n",
"}\n"
]
},
{
@@ -288,8 +304,8 @@
],
"source": [
"const { data: input } = await supabase.from(\"planes_estudio\")\n",
".select(\"*, estructuras_plan (definicion)\").limit(1).single();\n",
"input"
" .select(\"*, estructuras_plan (definicion)\").limit(1).single();\n",
"input;\n"
]
},
{
@@ -320,16 +336,22 @@
" },\n",
"\n",
" items: [\n",
" { type: \"message\", role: \"system\", content: \"En caso de que te pidan algo que no tiene nada que ver con planes de estudio o asignatura responde con un refusal.\" },\n",
" {\n",
" type: \"message\",\n",
" role: \"system\",\n",
" content:\n",
" \"En caso de que te pidan algo que no tiene nada que ver con planes de estudio o asignatura responde con un refusal.\",\n",
" },\n",
" ],\n",
"});\n",
"\n",
"const {data: conversationPlane} = await supabase.from(\"conversaciones_plan\").insert({\n",
" openai_conversation_id: openaiConversation.id,\n",
" plan_estudio_id: input.id,\n",
"}).select(\"id\").single();\n",
"const { data: conversationPlane } = await supabase.from(\"conversaciones_plan\")\n",
" .insert({\n",
" openai_conversation_id: openaiConversation.id,\n",
" plan_estudio_id: input.id,\n",
" }).select(\"id\").single();\n",
"\n",
"conversationPlane"
"conversationPlane;\n"
]
},
{
@@ -373,14 +395,16 @@
],
"source": [
"// Archivar una conversación\n",
"const items = await openai.conversations.items.list(conversation.openai_conversation_id);\n",
"const items = await openai.conversations.items.list(\n",
" conversation.openai_conversation_id,\n",
");\n",
"await supabase.from(\"conversaciones_plan\").update({\n",
" estado: \"ARCHIVANDO\",\n",
" conversacion_json: items,\n",
" openai_conversation_id: null,\n",
"}).eq(\"id\", conversationPlane.id);\n",
"\n",
"await openai.conversations.delete(conversation.openai_conversation_id);"
"await openai.conversations.delete(conversation.openai_conversation_id);\n"
]
},
{
@@ -450,10 +474,13 @@
],
"source": [
"// Listar mensajes dado una conversacion_plan.id\n",
"const { data: conversation } = await supabase.from(\"conversaciones_plan\").select(\"openai_conversation_id\").eq(\"id\", conversationPlane.id).single();\n",
"const items = await openai.conversations.items.list(conversation.openai_conversation_id);\n",
"const { data: conversation } = await supabase.from(\"conversaciones_plan\")\n",
" .select(\"openai_conversation_id\").eq(\"id\", conversationPlane.id).single();\n",
"const items = await openai.conversations.items.list(\n",
" conversation.openai_conversation_id,\n",
");\n",
"// items.data.filter((item) => item.type === \"message\" && [\"assistant\", \"user\"].includes(item.role))\n",
"JSON.stringify(items, null, 2)"
"JSON.stringify(items, null, 2);\n"
]
},
{
@@ -714,7 +741,7 @@
"const { data: respuesta, error } = await supabase.functions.invoke(\n",
" `create-chat-conversation/conversations/${data.conversation_plan.id}/messages`,\n",
" {\n",
" method: \"GET\"\n",
" method: \"GET\",\n",
" },\n",
");\n",
"\n",
@@ -767,7 +794,7 @@
}
],
"source": [
"conversationToUpdate.openai_conversation_id"
"conversationToUpdate.openai_conversation_id;\n"
]
},
{
@@ -792,7 +819,10 @@
}
],
"source": [
"await openai.beta.threads.messages.retrieve('msg_057bd8aacc77d01f006994e942d98481938f35bf58dacf126d', {thread_id:conversationToUpdate.openai_conversation_id})"
"await openai.beta.threads.messages.retrieve(\n",
" \"msg_057bd8aacc77d01f006994e942d98481938f35bf58dacf126d\",\n",
" { thread_id: conversationToUpdate.openai_conversation_id },\n",
");\n"
]
},
{