From 3a64c930eae53f750124b82104f37137eb82d521 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Thu, 23 Apr 2026 18:10:17 +0000 Subject: [PATCH 1/3] Changes Co-authored-by: domfelipe <53182096+domfelipe@users.noreply.github.com> --- supabase/functions/provision-agent/index.ts | 97 +++++++++++++++------ 1 file changed, 72 insertions(+), 25 deletions(-) diff --git a/supabase/functions/provision-agent/index.ts b/supabase/functions/provision-agent/index.ts index 94b19c1..0c1e603 100644 --- a/supabase/functions/provision-agent/index.ts +++ b/supabase/functions/provision-agent/index.ts @@ -19,8 +19,7 @@ interface RequestBody { const SUPABASE_URL = Deno.env.get("SUPABASE_URL")!; const SUPABASE_SERVICE_ROLE_KEY = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!; const RAILWAY_API_TOKEN = Deno.env.get("RAILWAY_API_TOKEN"); -const OPENCODE_ZEN_API_KEY = Deno.env.get("OPENCODE_ZEN_API_KEY") ?? ""; -const OPENCODE_GO_API_KEY = Deno.env.get("OPENCODE_GO_API_KEY") ?? ""; +const OPENROUTER_API_KEY = Deno.env.get("OPENROUTER_API_KEY") ?? ""; Deno.serve(async (req) => { if (req.method === "OPTIONS") return new Response(null, { headers: corsHeaders }); @@ -29,6 +28,10 @@ Deno.serve(async (req) => { return jsonResponse(500, { error: "RAILWAY_API_TOKEN not configured" }); } + if (!OPENROUTER_API_KEY) { + return jsonResponse(500, { error: "OPENROUTER_API_KEY not configured" }); + } + let body: RequestBody; try { body = await req.json(); @@ -44,11 +47,11 @@ Deno.serve(async (req) => { auth: { persistSession: false, autoRefreshToken: false }, }); - // 1) Carregar agent_instance + profile + // 1) Carregar agent_instance const { data: agent, error: agentErr } = await supabase .from("agent_instances") .select( - "id, user_id, uuid_tenant, status, telegram_bot_token_vault_id, telegram_bot_username, railway_service_id", + "id, user_id, uuid_tenant, status, telegram_bot_token_vault_id, telegram_bot_username, telegram_user_chat_id, railway_service_id", ) .eq("id", body.agent_instance_id) .maybeSingle(); @@ -65,6 +68,31 @@ Deno.serve(async (req) => { return jsonResponse(409, { error: "agent_instance already has a railway_service_id", railway_service_id: agent.railway_service_id }); } + // 1b) Carregar profile (full_name → nome do agente) + const { data: profile } = await supabase + .from("profiles") + .select("full_name") + .eq("id", agent.user_id) + .maybeSingle(); + + const fullName = (profile?.full_name?.trim() || "Usuário").toString(); + const firstName = fullName.split(" ")[0] || "Usuário"; + const agentName = `Mika de ${firstName}`; + + // 1c) Carregar subscription ativa (para definir modelo Pro vs Basic) + const { data: subscription } = await supabase + .from("subscriptions") + .select("plan_id, status, plans(slug)") + .eq("user_id", agent.user_id) + .in("status", ["active", "trialing"]) + .order("created_at", { ascending: false }) + .limit(1) + .maybeSingle(); + + // deno-lint-ignore no-explicit-any + const planSlug = ((subscription as any)?.plans?.slug as string | undefined) ?? "basic"; + const isPro = ["professional", "enterprise"].includes(planSlug); + // 2) Buscar pool disponível (com IDs Railway preenchidos e capacidade) const { data: pool, error: poolErr } = await supabase .from("vps_pool") @@ -91,7 +119,12 @@ Deno.serve(async (req) => { status: "running", attempt: 1, started_at: new Date().toISOString(), - payload: { uuid_tenant: agent.uuid_tenant, telegram_bot_username: agent.telegram_bot_username }, + payload: { + uuid_tenant: agent.uuid_tenant, + telegram_bot_username: agent.telegram_bot_username, + plan_slug: planSlug, + agent_name: agentName, + }, }) .select("id") .single(); @@ -101,27 +134,48 @@ Deno.serve(async (req) => { } // 4) Decrypt do telegram_bot_token (se existir) - let telegramToken = ""; + let telegramBotToken = ""; if (agent.telegram_bot_token_vault_id) { const { data: secret } = await supabase.rpc("vault_decrypt_secret", { secret_id: agent.telegram_bot_token_vault_id, }); - telegramToken = secret?.[0]?.decrypted_secret ?? ""; + telegramBotToken = secret?.[0]?.decrypted_secret ?? ""; } - if (!telegramToken) { + if (!telegramBotToken) { 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" }); } // 5) Apagar webhook Telegram (Hermes vai usar polling) try { - await deleteTelegramWebhook(telegramToken); + await deleteTelegramWebhook(telegramBotToken); } catch (e) { console.warn("deleteTelegramWebhook failed (continuing):", String(e)); } - // 6) Criar serviço no Railway + // 6) Montar variáveis de ambiente do container + const hasChatId = !!agent.telegram_user_chat_id; + const soulContent = `Você se chama ${agentName}. Você é um assistente pessoal de IA criado pela DOMCO para ${fullName}. Você é proativo, direto e fala sempre em português brasileiro. Você ajuda ${firstName} a ser mais produtivo — gerenciando emails, agenda, tarefas e automatizando o que puder. Seja conciso nas respostas via Telegram. Nunca se identifique como Hermes ou como produto da Nous Research — você é Mika.`; + + const envVars: Record = { + TELEGRAM_BOT_TOKEN: telegramBotToken, + TELEGRAM_ALLOWED_USERS: hasChatId ? String(agent.telegram_user_chat_id) : "", + TELEGRAM_HOME_CHANNEL: hasChatId ? String(agent.telegram_user_chat_id) : "", + GATEWAY_ALLOW_ALL_USERS: hasChatId ? "false" : "true", + HERMES_SOUL_MD: soulContent, + HERMES_TTS_PROVIDER: "disabled", + HERMES_STT_PROVIDER: "local", + OPENROUTER_API_KEY, + HERMES_MODEL: isPro + ? "openrouter/google/gemma-4-31b-it" + : "openrouter/google/gemma-4-27b-a4b-it", + HERMES_FALLBACK_MODEL: "openrouter/google/gemma-4-31b-it", + API_SERVER_ENABLED: "false", + HERMES_HOME: "/opt/data", + }; + + // 7) Criar serviço no Railway const serviceName = `mika-${agent.uuid_tenant.replace(/-/g, "").slice(0, 8)}`; let railwayServiceId: string; @@ -137,16 +191,8 @@ Deno.serve(async (req) => { serviceId: railwayServiceId, environmentId: pool.railway_environment_id, image: "nousresearch/hermes-agent:latest", - variables: { - TELEGRAM_BOT_TOKEN: telegramToken, - TELEGRAM_ALLOWED_USERS: "", - API_SERVER_ENABLED: "false", - HERMES_HOME: "/root/.hermes", - MAIN_MODEL_PROVIDER: "opencode-zen", - OPENCODE_ZEN_API_KEY, - OPENCODE_GO_API_KEY, - HERMES_GATEWAY_CMD: "true", - }, + startCommand: "/opt/hermes/docker/entrypoint.sh gateway run", + variables: envVars, }); await deployRailwayService({ @@ -161,7 +207,7 @@ Deno.serve(async (req) => { return jsonResponse(500, { error: "railway provisioning failed", detail: msg }); } - // 7) Persistir railway_service_id no agent_instance e no job + // 8) Persistir railway_service_id no agent_instance e no job (status='running') await supabase .from("agent_instances") .update({ railway_service_id: railwayServiceId, vps_pool_id: pool.id }) @@ -169,15 +215,17 @@ Deno.serve(async (req) => { await supabase .from("provisioning_jobs") - .update({ railway_service_id: railwayServiceId }) + .update({ railway_service_id: railwayServiceId, status: "running" }) .eq("id", job.id); - // status permanece 'provisioning' — o railway-webhook atualiza para 'active' quando o deploy subir + // status do agent permanece 'provisioning' — railway-webhook atualiza para 'active' quando deploy subir return jsonResponse(200, { success: true, agent_instance_id: agent.id, railway_service_id: railwayServiceId, job_id: job.id, + plan_slug: planSlug, + agent_name: agentName, }); }); @@ -209,7 +257,6 @@ async function scheduleRetry( jobId: string, message: string, ) { - // Lê a tentativa atual const { data: job } = await supabase .from("provisioning_jobs") .select("attempt, max_attempts") @@ -224,7 +271,7 @@ async function scheduleRetry( return; } - const nextDelayMs = Math.pow(attempt, 2) * 60_000; // attempt^2 minutos + const nextDelayMs = Math.pow(attempt, 2) * 60_000; const nextRetryAt = new Date(Date.now() + nextDelayMs).toISOString(); await supabase From b11e6da3a367a7e368591a9dc3fe67c1808f8d1a Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Thu, 23 Apr 2026 18:10:40 +0000 Subject: [PATCH 2/3] Changes Co-authored-by: domfelipe <53182096+domfelipe@users.noreply.github.com> --- supabase/functions/_shared/railway.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/supabase/functions/_shared/railway.ts b/supabase/functions/_shared/railway.ts index 379c4c7..8e7a5aa 100644 --- a/supabase/functions/_shared/railway.ts +++ b/supabase/functions/_shared/railway.ts @@ -65,20 +65,25 @@ export async function configureRailwayService(opts: { environmentId: string; image: string; variables: Record; + startCommand?: string; }): Promise { // O Railway expõe variáveis via variableUpsert (uma por vez) e fonte/imagem via serviceInstanceUpdate. - // Setamos a imagem primeiro. + // Setamos a imagem (e startCommand opcional) primeiro. const updateSource = ` mutation ServiceInstanceUpdate($serviceId: String!, $environmentId: String!, $input: ServiceInstanceUpdateInput!) { serviceInstanceUpdate(serviceId: $serviceId, environmentId: $environmentId, input: $input) } `; + const sourceInput: Record = { source: { image: opts.image } }; + if (opts.startCommand) { + sourceInput.startCommand = opts.startCommand; + } const sourceRes = await railwayQuery( updateSource, { serviceId: opts.serviceId, environmentId: opts.environmentId, - input: { source: { image: opts.image } }, + input: sourceInput, }, opts.token, ); From 7a5e24944522dd318391b45eefca220834a866d8 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Thu, 23 Apr 2026 18:10:53 +0000 Subject: [PATCH 3/3] Changes Co-authored-by: domfelipe <53182096+domfelipe@users.noreply.github.com> --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 9004f75..a1148f1 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ Já configuradas via `.env` (gerado automaticamente pelo Lovable Cloud): - `PADDLE_API_KEY` / `PADDLE_WEBHOOK_SECRET` - `SUPABASE_SERVICE_ROLE_KEY` +- `RAILWAY_API_TOKEN` — token da Railway Public API, usado por `provision-agent`/`suspend-agent`/`resume-agent` para criar e gerenciar containers Hermes. +- `OPENROUTER_API_KEY` — **obrigatório**. Injetado em cada container Hermes provisionado para que o agente possa chamar os modelos `openrouter/google/gemma-4-*-it`. Sem isso o `provision-agent` retorna 500. - (opcional, Fase 5) credenciais SSH para Hermes ---