mirror of
https://github.com/domfelipe/mika-agent-assist.git
synced 2026-08-07 10:56:44 +00:00
Corrigiu provenção token do bot
X-Lovable-Edit-ID: edt-a169bb9e-e525-4b91-b180-2d62dcb9da78 Co-authored-by: domfelipe <53182096+domfelipe@users.noreply.github.com>
This commit is contained in:
commit
4a7467a102
2 changed files with 38 additions and 5 deletions
|
|
@ -197,9 +197,18 @@ Deno.serve(async (req) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!telegramBotToken) {
|
if (!telegramBotToken) {
|
||||||
console.error(`[provision-agent] telegram_bot_token ausente — usuário ainda não conectou bot`);
|
// Onboarding ainda não concluído. NÃO marca agent como 'error' — o wizard de Telegram
|
||||||
await failJob(supabase, agent, job.id, "telegram_bot_token ausente no Vault — usuário precisa concluir onboarding antes");
|
// dispara este endpoint de novo após o usuário salvar o token (validate-telegram-bot).
|
||||||
return jsonResponse(412, { error: "telegram token missing" });
|
console.log(`[provision-agent] aguardando token do Telegram (job=${job.id})`);
|
||||||
|
await supabase
|
||||||
|
.from("provisioning_jobs")
|
||||||
|
.update({
|
||||||
|
status: "pending",
|
||||||
|
error_message: "Aguardando o usuário conectar o bot do Telegram",
|
||||||
|
completed_at: null,
|
||||||
|
})
|
||||||
|
.eq("id", job.id);
|
||||||
|
return jsonResponse(202, { waiting_for_token: true, agent_instance_id: agent.id });
|
||||||
}
|
}
|
||||||
console.log(`[provision-agent] token Telegram OK (len=${telegramBotToken.length})`);
|
console.log(`[provision-agent] token Telegram OK (len=${telegramBotToken.length})`);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -98,12 +98,13 @@ Deno.serve(async (req) => {
|
||||||
409,
|
409,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (agent.status === "suspended" || agent.status === "error") {
|
if (agent.status === "suspended") {
|
||||||
return jsonResponse(
|
return jsonResponse(
|
||||||
{ error: "Agente suspenso. Regularize sua assinatura antes de conectar o Telegram." },
|
{ error: "Agente suspenso. Regularize sua assinatura antes de conectar o Telegram." },
|
||||||
403,
|
403,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
// status='error' por falta de token anterior é recuperável — apenas seguimos.
|
||||||
|
|
||||||
// 4) Verifica unicidade do bot username (em outro agent_instance)
|
// 4) Verifica unicidade do bot username (em outro agent_instance)
|
||||||
const { data: conflict } = await admin
|
const { data: conflict } = await admin
|
||||||
|
|
@ -186,7 +187,9 @@ Deno.serve(async (req) => {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7) Atualiza agent_instances
|
// 7) Atualiza agent_instances — reseta para 'provisioning' se estava em erro,
|
||||||
|
// garante que o provision-agent re-execute do zero.
|
||||||
|
const nextStatus = agent.status === "error" ? "provisioning" : agent.status;
|
||||||
const { error: updErr } = await admin
|
const { error: updErr } = await admin
|
||||||
.from("agent_instances")
|
.from("agent_instances")
|
||||||
.update({
|
.update({
|
||||||
|
|
@ -196,6 +199,7 @@ Deno.serve(async (req) => {
|
||||||
telegram_token_invalid: false,
|
telegram_token_invalid: false,
|
||||||
telegram_webhook_configured: false,
|
telegram_webhook_configured: false,
|
||||||
telegram_first_message_received_at: null,
|
telegram_first_message_received_at: null,
|
||||||
|
status: nextStatus,
|
||||||
updated_at: new Date().toISOString(),
|
updated_at: new Date().toISOString(),
|
||||||
})
|
})
|
||||||
.eq("id", agent.id);
|
.eq("id", agent.id);
|
||||||
|
|
@ -205,6 +209,26 @@ Deno.serve(async (req) => {
|
||||||
return jsonResponse({ error: "Falha ao salvar dados do bot." }, 500);
|
return jsonResponse({ error: "Falha ao salvar dados do bot." }, 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 8) Auto-provisionamento: dispara provision-agent assincronamente para que
|
||||||
|
// o container seja criado/atualizado no Railway. Fire-and-forget — o wizard
|
||||||
|
// não bloqueia esperando o deploy completar.
|
||||||
|
if (nextStatus === "provisioning" || agent.status === "active") {
|
||||||
|
try {
|
||||||
|
const provisionUrl = `${supabaseUrl}/functions/v1/provision-agent`;
|
||||||
|
console.log(`auto-provision: disparando para agent ${agent.id}`);
|
||||||
|
fetch(provisionUrl, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${serviceKey}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ agent_instance_id: agent.id }),
|
||||||
|
}).catch((err) => console.error("auto-provision fetch error:", err));
|
||||||
|
} catch (err) {
|
||||||
|
console.error("auto-provision trigger error:", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return jsonResponse({
|
return jsonResponse({
|
||||||
valid: true,
|
valid: true,
|
||||||
bot_username: botUsername,
|
bot_username: botUsername,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue