mirror of
https://github.com/domfelipe/mika-agent-assist.git
synced 2026-08-07 11:16:46 +00:00
Changes
Co-authored-by: domfelipe <53182096+domfelipe@users.noreply.github.com>
This commit is contained in:
parent
76cb26a927
commit
467f3edfb9
1 changed files with 38 additions and 3 deletions
|
|
@ -75,6 +75,8 @@ Deno.serve(async (req) => {
|
||||||
auth: { persistSession: false, autoRefreshToken: false },
|
auth: { persistSession: false, autoRefreshToken: false },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log(`[provision-agent] início para agent_instance_id=${body.agent_instance_id}`);
|
||||||
|
|
||||||
// 1) Carregar agent_instance
|
// 1) Carregar agent_instance
|
||||||
const { data: agent, error: agentErr } = await supabase
|
const { data: agent, error: agentErr } = await supabase
|
||||||
.from("agent_instances")
|
.from("agent_instances")
|
||||||
|
|
@ -85,14 +87,17 @@ Deno.serve(async (req) => {
|
||||||
.maybeSingle();
|
.maybeSingle();
|
||||||
|
|
||||||
if (agentErr || !agent) {
|
if (agentErr || !agent) {
|
||||||
|
console.error(`[provision-agent] agent_instance não encontrado: ${agentErr?.message}`);
|
||||||
return jsonResponse(404, { error: "agent_instance not found", detail: agentErr?.message });
|
return jsonResponse(404, { error: "agent_instance not found", detail: agentErr?.message });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (agent.status !== "provisioning") {
|
if (agent.status !== "provisioning") {
|
||||||
|
console.log(`[provision-agent] status atual=${agent.status}, abortando`);
|
||||||
return jsonResponse(409, { error: "agent_instance is not in provisioning status", status: agent.status });
|
return jsonResponse(409, { error: "agent_instance is not in provisioning status", status: agent.status });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (agent.railway_service_id) {
|
if (agent.railway_service_id) {
|
||||||
|
console.log(`[provision-agent] já tem railway_service_id=${agent.railway_service_id}, abortando`);
|
||||||
return jsonResponse(409, { error: "agent_instance already has a railway_service_id", railway_service_id: agent.railway_service_id });
|
return jsonResponse(409, { error: "agent_instance already has a railway_service_id", railway_service_id: agent.railway_service_id });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -106,6 +111,7 @@ Deno.serve(async (req) => {
|
||||||
const fullName = (profile?.full_name?.trim() || "Usuário").toString();
|
const fullName = (profile?.full_name?.trim() || "Usuário").toString();
|
||||||
const firstName = fullName.split(" ")[0] || "Usuário";
|
const firstName = fullName.split(" ")[0] || "Usuário";
|
||||||
const agentName = body.agent_name?.trim() || `Mika de ${firstName}`;
|
const agentName = body.agent_name?.trim() || `Mika de ${firstName}`;
|
||||||
|
console.log(`[provision-agent] profile carregado: ${fullName} → agent_name=${agentName}`);
|
||||||
|
|
||||||
// 1c) Carregar subscription ativa (para definir modelo Pro vs Basic)
|
// 1c) Carregar subscription ativa (para definir modelo Pro vs Basic)
|
||||||
const { data: subscription } = await supabase
|
const { data: subscription } = await supabase
|
||||||
|
|
@ -120,6 +126,7 @@ Deno.serve(async (req) => {
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
const planSlug = ((subscription as any)?.plans?.slug as string | undefined) ?? "basic";
|
const planSlug = ((subscription as any)?.plans?.slug as string | undefined) ?? "basic";
|
||||||
const isPro = ["professional", "enterprise"].includes(planSlug);
|
const isPro = ["professional", "enterprise"].includes(planSlug);
|
||||||
|
console.log(`[provision-agent] plano=${planSlug} isPro=${isPro}`);
|
||||||
|
|
||||||
// 2) Buscar pool disponível (com IDs Railway preenchidos e capacidade)
|
// 2) Buscar pool disponível (com IDs Railway preenchidos e capacidade)
|
||||||
const { data: pool, error: poolErr } = await supabase
|
const { data: pool, error: poolErr } = await supabase
|
||||||
|
|
@ -133,9 +140,17 @@ Deno.serve(async (req) => {
|
||||||
.maybeSingle();
|
.maybeSingle();
|
||||||
|
|
||||||
if (poolErr || !pool || !pool.railway_project_id || !pool.railway_environment_id) {
|
if (poolErr || !pool || !pool.railway_project_id || !pool.railway_environment_id) {
|
||||||
|
console.error(`[provision-agent] sem vps_pool disponível: ${poolErr?.message}`);
|
||||||
await failJob(supabase, agent, null, "Nenhum vps_pool com Railway IDs configurados disponível");
|
await failJob(supabase, agent, null, "Nenhum vps_pool com Railway IDs configurados disponível");
|
||||||
|
await notifyAdmin(
|
||||||
|
`❌ <b>Falha no auto-provisionamento</b>\n\n` +
|
||||||
|
`👤 <b>Cliente:</b> ${fullName}\n` +
|
||||||
|
`❗ <b>Erro:</b> Nenhum vps_pool disponível\n\n` +
|
||||||
|
`➡️ <a href="https://mika.domco.ai/admin">Resolver manualmente</a>`,
|
||||||
|
);
|
||||||
return jsonResponse(503, { error: "no railway pool available" });
|
return jsonResponse(503, { error: "no railway pool available" });
|
||||||
}
|
}
|
||||||
|
console.log(`[provision-agent] pool selecionado: ${pool.id} (railway_project=${pool.railway_project_id})`);
|
||||||
|
|
||||||
// 3) Criar provisioning_job em status running
|
// 3) Criar provisioning_job em status running
|
||||||
const { data: job, error: jobErr } = await supabase
|
const { data: job, error: jobErr } = await supabase
|
||||||
|
|
@ -158,12 +173,15 @@ Deno.serve(async (req) => {
|
||||||
.single();
|
.single();
|
||||||
|
|
||||||
if (jobErr || !job) {
|
if (jobErr || !job) {
|
||||||
|
console.error(`[provision-agent] falha ao criar job: ${jobErr?.message}`);
|
||||||
return jsonResponse(500, { error: "failed to create provisioning_job", detail: jobErr?.message });
|
return jsonResponse(500, { error: "failed to create provisioning_job", detail: jobErr?.message });
|
||||||
}
|
}
|
||||||
|
console.log(`[provision-agent] provisioning_job criado: ${job.id}`);
|
||||||
|
|
||||||
// 4) Decrypt do telegram_bot_token (se existir)
|
// 4) Decrypt do telegram_bot_token (se existir)
|
||||||
let telegramBotToken = "";
|
let telegramBotToken = "";
|
||||||
if (agent.telegram_bot_token_vault_id) {
|
if (agent.telegram_bot_token_vault_id) {
|
||||||
|
console.log(`[provision-agent] decifrando token do Vault: ${agent.telegram_bot_token_vault_id}`);
|
||||||
const { data: secret } = await supabase.rpc("vault_decrypt_secret", {
|
const { data: secret } = await supabase.rpc("vault_decrypt_secret", {
|
||||||
secret_id: agent.telegram_bot_token_vault_id,
|
secret_id: agent.telegram_bot_token_vault_id,
|
||||||
});
|
});
|
||||||
|
|
@ -171,15 +189,18 @@ Deno.serve(async (req) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!telegramBotToken) {
|
if (!telegramBotToken) {
|
||||||
|
console.error(`[provision-agent] telegram_bot_token ausente — usuário ainda não conectou bot`);
|
||||||
await failJob(supabase, agent, job.id, "telegram_bot_token ausente no Vault — usuário precisa concluir onboarding antes");
|
await failJob(supabase, agent, job.id, "telegram_bot_token ausente no Vault — usuário precisa concluir onboarding antes");
|
||||||
return jsonResponse(412, { error: "telegram token missing" });
|
return jsonResponse(412, { error: "telegram token missing" });
|
||||||
}
|
}
|
||||||
|
console.log(`[provision-agent] token Telegram OK (len=${telegramBotToken.length})`);
|
||||||
|
|
||||||
// 5) Apagar webhook Telegram (Hermes vai usar polling)
|
// 5) Apagar webhook Telegram (Hermes vai usar polling)
|
||||||
try {
|
try {
|
||||||
await deleteTelegramWebhook(telegramBotToken);
|
await deleteTelegramWebhook(telegramBotToken);
|
||||||
|
console.log(`[provision-agent] deleteTelegramWebhook OK`);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn("deleteTelegramWebhook failed (continuing):", String(e));
|
console.warn("[provision-agent] deleteTelegramWebhook failed (continuing):", String(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6) Montar variáveis de ambiente do container
|
// 6) Montar variáveis de ambiente do container
|
||||||
|
|
@ -212,6 +233,7 @@ Deno.serve(async (req) => {
|
||||||
|
|
||||||
// 7) Criar serviço no Railway
|
// 7) Criar serviço no Railway
|
||||||
const serviceName = `mika-${agent.uuid_tenant.replace(/-/g, "").slice(0, 8)}`;
|
const serviceName = `mika-${agent.uuid_tenant.replace(/-/g, "").slice(0, 8)}`;
|
||||||
|
console.log(`[provision-agent] criando serviço Railway: ${serviceName}`);
|
||||||
let railwayServiceId: string;
|
let railwayServiceId: string;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
@ -220,6 +242,7 @@ Deno.serve(async (req) => {
|
||||||
projectId: pool.railway_project_id,
|
projectId: pool.railway_project_id,
|
||||||
name: serviceName,
|
name: serviceName,
|
||||||
});
|
});
|
||||||
|
console.log(`[provision-agent] serviço criado: ${railwayServiceId}`);
|
||||||
|
|
||||||
await configureRailwayService({
|
await configureRailwayService({
|
||||||
token: RAILWAY_API_TOKEN,
|
token: RAILWAY_API_TOKEN,
|
||||||
|
|
@ -229,16 +252,26 @@ Deno.serve(async (req) => {
|
||||||
startCommand: HERMES_START_COMMAND,
|
startCommand: HERMES_START_COMMAND,
|
||||||
variables: envVars,
|
variables: envVars,
|
||||||
});
|
});
|
||||||
|
console.log(`[provision-agent] serviço configurado com ${Object.keys(envVars).length} env vars`);
|
||||||
|
|
||||||
await deployRailwayService({
|
await deployRailwayService({
|
||||||
token: RAILWAY_API_TOKEN,
|
token: RAILWAY_API_TOKEN,
|
||||||
serviceId: railwayServiceId,
|
serviceId: railwayServiceId,
|
||||||
environmentId: pool.railway_environment_id,
|
environmentId: pool.railway_environment_id,
|
||||||
});
|
});
|
||||||
|
console.log(`[provision-agent] deploy disparado em Railway`);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const msg = e instanceof Error ? e.message : String(e);
|
const msg = e instanceof Error ? e.message : String(e);
|
||||||
console.error("Railway provisioning failed:", msg);
|
console.error("[provision-agent] Railway provisioning failed:", msg);
|
||||||
await scheduleRetry(supabase, agent, job.id, msg);
|
const reachedMax = await scheduleRetry(supabase, agent, job.id, msg);
|
||||||
|
if (reachedMax) {
|
||||||
|
await notifyAdmin(
|
||||||
|
`❌ <b>Falha no auto-provisionamento</b>\n\n` +
|
||||||
|
`👤 <b>Cliente:</b> ${fullName}\n` +
|
||||||
|
`❗ <b>Erro:</b> ${msg}\n\n` +
|
||||||
|
`➡️ <a href="https://mika.domco.ai/admin">Provisionar manualmente</a>`,
|
||||||
|
);
|
||||||
|
}
|
||||||
return jsonResponse(500, { error: "railway provisioning failed", detail: msg });
|
return jsonResponse(500, { error: "railway provisioning failed", detail: msg });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -262,6 +295,8 @@ Deno.serve(async (req) => {
|
||||||
.update({ railway_service_id: railwayServiceId, status: "running" })
|
.update({ railway_service_id: railwayServiceId, status: "running" })
|
||||||
.eq("id", job.id);
|
.eq("id", job.id);
|
||||||
|
|
||||||
|
console.log(`[provision-agent] sucesso: agent=${agent.id} railway=${railwayServiceId} (aguardando deploy)`);
|
||||||
|
|
||||||
// status do agent permanece 'provisioning' — railway-webhook atualiza para 'active' quando deploy subir
|
// status do agent permanece 'provisioning' — railway-webhook atualiza para 'active' quando deploy subir
|
||||||
return jsonResponse(200, {
|
return jsonResponse(200, {
|
||||||
success: true,
|
success: true,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue