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
3b5137decd
commit
858b8f5189
2 changed files with 42 additions and 22 deletions
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
import { createClient } from "https://esm.sh/@supabase/supabase-js@2.45.4";
|
import { createClient } from "https://esm.sh/@supabase/supabase-js@2.45.4";
|
||||||
import { corsHeaders } from "../_shared/cors.ts";
|
import { corsHeaders } from "../_shared/cors.ts";
|
||||||
import { setRailwayReplicas } from "../_shared/railway.ts";
|
import { setRailwayReplicas, getServiceEnvironmentId } from "../_shared/railway.ts";
|
||||||
|
|
||||||
const SUPABASE_URL = Deno.env.get("SUPABASE_URL")!;
|
const SUPABASE_URL = Deno.env.get("SUPABASE_URL")!;
|
||||||
const SUPABASE_SERVICE_ROLE_KEY = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!;
|
const SUPABASE_SERVICE_ROLE_KEY = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!;
|
||||||
|
|
@ -45,7 +45,7 @@ Deno.serve(async (req) => {
|
||||||
|
|
||||||
if (!agent) return jsonResponse(404, { error: "agent_instance not found" });
|
if (!agent) return jsonResponse(404, { error: "agent_instance not found" });
|
||||||
|
|
||||||
if (!agent.railway_service_id || !agent.vps_pool_id) {
|
if (!agent.railway_service_id) {
|
||||||
return jsonResponse(409, {
|
return jsonResponse(409, {
|
||||||
error: "agent has no container — needs full provisioning instead",
|
error: "agent has no container — needs full provisioning instead",
|
||||||
});
|
});
|
||||||
|
|
@ -55,21 +55,31 @@ Deno.serve(async (req) => {
|
||||||
return jsonResponse(200, { ok: true, already_active: true });
|
return jsonResponse(200, { ok: true, already_active: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data: pool } = await supabase
|
// Resolve environmentId: prioriza vps_pool, fallback para query no Railway
|
||||||
.from("vps_pool")
|
let environmentId: string | null = null;
|
||||||
.select("railway_environment_id")
|
if (agent.vps_pool_id) {
|
||||||
.eq("id", agent.vps_pool_id)
|
const { data: pool } = await supabase
|
||||||
.maybeSingle();
|
.from("vps_pool")
|
||||||
|
.select("railway_environment_id")
|
||||||
if (!pool?.railway_environment_id) {
|
.eq("id", agent.vps_pool_id)
|
||||||
return jsonResponse(500, { error: "pool environment not configured" });
|
.maybeSingle();
|
||||||
|
environmentId = pool?.railway_environment_id ?? null;
|
||||||
|
}
|
||||||
|
if (!environmentId) {
|
||||||
|
environmentId = await getServiceEnvironmentId({
|
||||||
|
token: RAILWAY_API_TOKEN,
|
||||||
|
serviceId: agent.railway_service_id,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (!environmentId) {
|
||||||
|
return jsonResponse(500, { error: "could not resolve railway environmentId" });
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await setRailwayReplicas({
|
await setRailwayReplicas({
|
||||||
token: RAILWAY_API_TOKEN,
|
token: RAILWAY_API_TOKEN,
|
||||||
serviceId: agent.railway_service_id,
|
serviceId: agent.railway_service_id,
|
||||||
environmentId: pool.railway_environment_id,
|
environmentId,
|
||||||
replicas: 1,
|
replicas: 1,
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
import { createClient } from "https://esm.sh/@supabase/supabase-js@2.45.4";
|
import { createClient } from "https://esm.sh/@supabase/supabase-js@2.45.4";
|
||||||
import { corsHeaders } from "../_shared/cors.ts";
|
import { corsHeaders } from "../_shared/cors.ts";
|
||||||
import { setRailwayReplicas } from "../_shared/railway.ts";
|
import { setRailwayReplicas, getServiceEnvironmentId } from "../_shared/railway.ts";
|
||||||
|
|
||||||
const SUPABASE_URL = Deno.env.get("SUPABASE_URL")!;
|
const SUPABASE_URL = Deno.env.get("SUPABASE_URL")!;
|
||||||
const SUPABASE_SERVICE_ROLE_KEY = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!;
|
const SUPABASE_SERVICE_ROLE_KEY = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!;
|
||||||
|
|
@ -47,7 +47,7 @@ Deno.serve(async (req) => {
|
||||||
if (agent.status === "suspended") {
|
if (agent.status === "suspended") {
|
||||||
return jsonResponse(200, { ok: true, already_suspended: true });
|
return jsonResponse(200, { ok: true, already_suspended: true });
|
||||||
}
|
}
|
||||||
if (!agent.railway_service_id || !agent.vps_pool_id) {
|
if (!agent.railway_service_id) {
|
||||||
// Sem container provisionado ainda — só marca o status
|
// Sem container provisionado ainda — só marca o status
|
||||||
await supabase
|
await supabase
|
||||||
.from("agent_instances")
|
.from("agent_instances")
|
||||||
|
|
@ -56,21 +56,31 @@ Deno.serve(async (req) => {
|
||||||
return jsonResponse(200, { ok: true, no_container: true });
|
return jsonResponse(200, { ok: true, no_container: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data: pool } = await supabase
|
// Resolve o environmentId: prioriza vps_pool, fallback para query no Railway
|
||||||
.from("vps_pool")
|
let environmentId: string | null = null;
|
||||||
.select("railway_environment_id")
|
if (agent.vps_pool_id) {
|
||||||
.eq("id", agent.vps_pool_id)
|
const { data: pool } = await supabase
|
||||||
.maybeSingle();
|
.from("vps_pool")
|
||||||
|
.select("railway_environment_id")
|
||||||
if (!pool?.railway_environment_id) {
|
.eq("id", agent.vps_pool_id)
|
||||||
return jsonResponse(500, { error: "pool environment not configured" });
|
.maybeSingle();
|
||||||
|
environmentId = pool?.railway_environment_id ?? null;
|
||||||
|
}
|
||||||
|
if (!environmentId) {
|
||||||
|
environmentId = await getServiceEnvironmentId({
|
||||||
|
token: RAILWAY_API_TOKEN,
|
||||||
|
serviceId: agent.railway_service_id,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (!environmentId) {
|
||||||
|
return jsonResponse(500, { error: "could not resolve railway environmentId" });
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await setRailwayReplicas({
|
await setRailwayReplicas({
|
||||||
token: RAILWAY_API_TOKEN,
|
token: RAILWAY_API_TOKEN,
|
||||||
serviceId: agent.railway_service_id,
|
serviceId: agent.railway_service_id,
|
||||||
environmentId: pool.railway_environment_id,
|
environmentId,
|
||||||
replicas: 0,
|
replicas: 0,
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue