AkurAI Build
Menu

popagent

public

Latest change 6e497f1faa80b1a0b14fa997f3a9fad2e3233530 - Require evidence before remediation resolution by AkurAI Build

#!/usr/bin/env bun

const baseUrl = (process.env.POPAGENT_URL ?? "http://127.0.0.1:5180").replace(/\/$/, "");
const [command = "help", ...args] = Bun.argv.slice(2);

const usage = `Usage: popagent <command> [arguments]

One-shot commands:
  popagent help
  popagent api <METHOD> <path> [json]
  popagent agents
  popagent agent-update <agent> <json>
  popagent skills <agent>
  popagent skill-create <agent> <json>
  popagent skill-update <agent> <skill> <json>
  popagent skill-delete <agent> <skill>
  popagent workspaces
  popagent workspace-create <json>
  popagent workspace-update <workspace> <json>
  popagent workspace-delete <workspace>
  popagent docs <workspace>
  popagent doc-read <workspace> <path>
  popagent doc-search <query> [workspace]
  popagent repo-brief <workspace> <query>
  popagent doc-save <workspace> <json>
  popagent doc-move <workspace> <json>
  popagent doc-delete <workspace> <json>
  popagent folder-create <workspace> <json>
  popagent folder-delete <workspace> <json>
  popagent docs-reindex <workspace>
  popagent tasks [workspace]
  popagent task <task-id>
  popagent task-create <workspace> <prompt> [model]
  popagent task-cancel <task-id>
  popagent task-delete <task-id>
  popagent task-resolve <task-id> <json>
  popagent signals [limit]
  popagent automation-status
  popagent automation-reconcile
  popagent automation-run <workspace>
  popagent signal-ignore <signal-id>
  popagent signal-retry <signal-id>
  popagent schedules [workspace]
  popagent schedule-create <json>
  popagent schedule-update <schedule> <json>
  popagent schedule-delete <schedule>
  popagent settings <section>
  popagent settings-update <section> <json>

JSON arguments are passed unchanged to the API.

Environment:
  POPAGENT_URL      API base URL (default: http://127.0.0.1:5180)
  POPAGENT_API_KEY  Sent only as x-popagent-key; never printed
`;

function fail(message: string): never {
  console.error(`popagent: ${message}`);
  process.exit(2);
}

async function api(method: string, path: string, body?: string): Promise<void> {
  if (!path.startsWith("/api/")) fail("path must begin with /api/");
  const headers: Record<string, string> = {};
  if (process.env.POPAGENT_API_KEY) headers["x-popagent-key"] = process.env.POPAGENT_API_KEY;
  if (body !== undefined) headers["content-type"] = "application/json";
  const response = await fetch(`${baseUrl}${path}`, { method, headers, body });
  const text = await response.text();
  if (text) console.log(text);
  if (!response.ok) process.exit(1);
}

const endpoint = (base: string, id: string, suffix = "") => `${base}/${encodeURIComponent(id)}${suffix}`;

async function collectionJson(method: string, path: string, values: string[], name: string) {
  const [body] = values;
  if (!body) fail(`${name} requires json`);
  await api(method, path, body);
}

async function resource(method: string, base: string, values: string[], name: string, suffix = "") {
  const [id] = values;
  if (!id) fail(`${name} requires an id`);
  await api(method, endpoint(base, id, suffix));
}

async function resourceGet(base: string, values: string[], name: string, suffix = "", method = "GET") {
  await resource(method, base, values, name, suffix);
}

async function resourceJson(method: string, base: string, values: string[], name: string, suffix = "") {
  const [id, body] = values;
  if (!id || !body) fail(`${name} requires an id and json`);
  await api(method, endpoint(base, id, suffix), body);
}

async function nestedJson(method: string, base: string, values: string[], name: string, suffix: string) {
  const [id, body] = values;
  if (!id || !body) fail(`${name} requires an id and json`);
  await api(method, endpoint(base, id, suffix), body);
}

async function nestedResource(method: string, base: string, values: string[], name: string, suffix: string) {
  const [parent, id] = values;
  if (!parent || !id) fail(`${name} requires two ids`);
  await api(method, `${endpoint(base, parent, suffix)}/${encodeURIComponent(id)}`);
}

async function nestedResourceJson(method: string, base: string, values: string[], name: string, suffix: string) {
  const [parent, id, body] = values;
  if (!parent || !id || !body) fail(`${name} requires two ids and json`);
  await api(method, `${endpoint(base, parent, suffix)}/${encodeURIComponent(id)}`, body);
}

async function optionalWorkspaceList(path: string, values: string[]) {
  const [workspace] = values;
  await api("GET", `${path}${workspace ? `?workspaceId=${encodeURIComponent(workspace)}` : ""}`);
}

switch (command) {
  case "help":
  case "--help":
  case "-h":
    console.log(usage);
    break;
  case "api": {
    const [method, path, body] = args;
    if (!method || !path) fail("api requires METHOD and path");
    await api(method.toUpperCase(), path, body);
    break;
  }
  case "agents": await api("GET", "/api/agents"); break;
  case "agent-update": await resourceJson("PATCH", "/api/agents", args, "agent-update"); break;
  case "skills": await resourceGet("/api/agents", args, "skills", "/skills"); break;
  case "skill-create": await nestedJson("POST", "/api/agents", args, "skill-create", "/skills"); break;
  case "skill-update": await nestedResourceJson("PATCH", "/api/agents", args, "skill-update", "/skills"); break;
  case "skill-delete": await nestedResource("DELETE", "/api/agents", args, "skill-delete", "/skills"); break;
  case "workspaces": await api("GET", "/api/workspaces"); break;
  case "workspace-create": await collectionJson("POST", "/api/workspaces", args, "workspace-create"); break;
  case "workspace-update": await resourceJson("PATCH", "/api/workspaces", args, "workspace-update"); break;
  case "workspace-delete": await resource("DELETE", "/api/workspaces", args, "workspace-delete"); break;
  case "docs": await resourceGet("/api/workspaces", args, "docs", "/docs"); break;
  case "doc-read": {
    const [workspace, path] = args;
    if (!workspace || !path) fail("doc-read requires workspace and path");
    await api("GET", `/api/workspaces/${encodeURIComponent(workspace)}/docs/content?path=${encodeURIComponent(path)}`);
    break;
  }
  case "doc-search": {
    const [query, workspace] = args;
    if (!query) fail("doc-search requires a query");
    await api("GET", `/api/docs/search?q=${encodeURIComponent(query)}${workspace ? `&workspaceId=${encodeURIComponent(workspace)}` : ""}`);
    break;
  }
  case "repo-brief": {
    const [workspace, query] = args;
    if (!workspace || !query) fail("repo-brief requires workspace and query");
    await api("GET", `/api/workspaces/${encodeURIComponent(workspace)}/repo-brief?q=${encodeURIComponent(query)}`);
    break;
  }
  case "doc-save": await nestedJson("POST", "/api/workspaces", args, "doc-save", "/docs"); break;
  case "doc-move": await nestedJson("PATCH", "/api/workspaces", args, "doc-move", "/docs"); break;
  case "doc-delete": await nestedJson("DELETE", "/api/workspaces", args, "doc-delete", "/docs"); break;
  case "folder-create": await nestedJson("POST", "/api/workspaces", args, "folder-create", "/docs/folders"); break;
  case "folder-delete": await nestedJson("DELETE", "/api/workspaces", args, "folder-delete", "/docs/folders"); break;
  case "docs-reindex": await resourceGet("/api/workspaces", args, "docs-reindex", "/docs/reindex", "POST"); break;
  case "tasks": await optionalWorkspaceList("/api/tasks", args); break;
  case "task": await resourceGet("/api/tasks", args, "task"); break;
  case "task-create": {
    const [workspaceId, prompt, model] = args;
    if (!workspaceId || !prompt) fail("task-create requires workspace and prompt");
    await api("POST", "/api/tasks", JSON.stringify({ workspaceId, prompt, ...(model ? { model } : {}) }));
    break;
  }
  case "task-cancel": await resource("POST", "/api/tasks", args, "task-cancel", "/cancel"); break;
  case "task-delete": await resource("DELETE", "/api/tasks", args, "task-delete"); break;
  case "task-resolve": await resourceJson("POST", "/api/tasks", args, "task-resolve", "/resolve"); break;
  case "automation-status": await api("GET", "/api/autonomy/status"); break;
  case "automation-run": {
    const [workspaceId] = args;
    if (!workspaceId) fail("automation-run requires workspace");
    await api("POST", "/api/autonomy/run", JSON.stringify({ workspaceId }));
    break;
  }
  case "automation-reconcile": await api("POST", "/api/autonomy/reconcile"); break;
  case "signals": {
    const [limit] = args;
    await api("GET", `/api/autonomy/signals${limit ? `?limit=${encodeURIComponent(limit)}` : ""}`);
    break;
  }
  case "signal-ignore": await resource("POST", "/api/autonomy/signals", args, "signal-ignore", "/ignore"); break;
  case "signal-retry": await resource("POST", "/api/autonomy/signals", args, "signal-retry", "/retry"); break;
  case "schedules": await optionalWorkspaceList("/api/schedules", args); break;
  case "schedule-create": await collectionJson("POST", "/api/schedules", args, "schedule-create"); break;
  case "schedule-update": await resourceJson("PATCH", "/api/schedules", args, "schedule-update"); break;
  case "schedule-delete": await resource("DELETE", "/api/schedules", args, "schedule-delete"); break;
  case "settings": await resourceGet("/api/settings", args, "settings"); break;
  case "settings-update": await resourceJson("PATCH", "/api/settings", args, "settings-update"); break;
  default: fail(`unknown command: ${command}\n\n${usage}`);
}