fix: sanitize cronjob names from agent (#10)

This commit is contained in:
Felipe Domingues 2026-05-30 12:30:21 -03:00 committed by GitHub
parent e08e40f62b
commit 792aa697b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -49,6 +49,7 @@ const HERMES_API_SERVER_KEY = Deno.env.get("HERMES_API_SERVER_KEY") ?? "";
const CONTRACT_VERSION = "2026-05-28"; const CONTRACT_VERSION = "2026-05-28";
const MODEL = "google/gemini-2.5-flash"; const MODEL = "google/gemini-2.5-flash";
const MAX_JOB_NAME_LENGTH = 60;
const VALID_MCP_SLUGS = new Set([ const VALID_MCP_SLUGS = new Set([
"google_workspace", "google_workspace",
@ -129,6 +130,28 @@ function tryParseJson(text: string): ParsedJob | null {
return null; return null;
} }
function cleanTitleCandidate(value: string): string {
return value
.replace(/^mika[\s,:;-]+/i, "")
.replace(/\s+/g, " ")
.trim();
}
function truncateJobName(value: string): string {
const title = cleanTitleCandidate(value);
if (title.length <= MAX_JOB_NAME_LENGTH) return title;
return `${title.slice(0, MAX_JOB_NAME_LENGTH - 3).trimEnd()}...`;
}
function buildJobName(bodyName: string | undefined, actionPrompt: string, input: string): string {
const candidates = [bodyName ?? "", actionPrompt, input];
for (const candidate of candidates) {
const name = truncateJobName(candidate);
if (name.length > 0) return name;
}
return "Automacao Mika";
}
async function parseWithAI(input: string, tz: string): Promise<ParsedJob | null> { async function parseWithAI(input: string, tz: string): Promise<ParsedJob | null> {
if (!LOVABLE_API_KEY) return null; if (!LOVABLE_API_KEY) return null;
const res = await fetch("https://ai.gateway.lovable.dev/v1/chat/completions", { const res = await fetch("https://ai.gateway.lovable.dev/v1/chat/completions", {
@ -284,8 +307,8 @@ Deno.serve(async (req) => {
/* fallback */ /* fallback */
} }
// 7) Nome: usa o fornecido ou deriva do input // 7) Nome: usa o fornecido ou deriva da ação, respeitando o CHECK (1..60 chars).
const name = (body.name ?? "").trim() || input.slice(0, 80); const name = buildJobName(body.name, actionPrompt, input);
const description = body.description ?? null; const description = body.description ?? null;
// 8) Insert (RLS bypass via service role; trigger enforce_job_limit ainda valida) // 8) Insert (RLS bypass via service role; trigger enforce_job_limit ainda valida)