peppa/apps/web/app/(app)/team/actions.ts
Асель 5b41e7d779 PEP-75: a team of agents — Kazakh names on the board, a character in the prompt
Also: «Открыть терминал» on live session cards.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-14 18:49:34 +05:00

43 lines
1.5 KiB
TypeScript

"use server";
import { createAgent, isSuperadmin, PeppaError, updateAgent } from "@peppa/core";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { asWebActor, requireUser } from "@/lib/auth";
import { db } from "@/lib/db";
/** PEP-75: the team is platform-wide, so only a superadmin changes it. */
async function run(fn: () => Promise<string | void>): Promise<never> {
let target = "/team";
try {
const user = asWebActor(await requireUser());
if (!(await isSuperadmin(db, user))) throw new PeppaError("Команду меняет только суперадмин");
target = (await fn()) || target;
} catch (e) {
target = `/team?error=${encodeURIComponent((e instanceof PeppaError ? e.message : `Ошибка: ${(e as Error).message}`).slice(0, 300))}`;
}
revalidatePath("/", "layout");
redirect(target);
}
export async function addAgentAction(f: FormData) {
await run(async () => {
const a = await createAgent(db, { name: String(f.get("name") ?? "") });
return `/team?saved=${a.id}#agent-${a.id}`;
});
}
export async function saveAgentAction(f: FormData) {
await run(async () => {
const id = Number(f.get("id"));
await updateAgent(db, id, { name: String(f.get("name") ?? ""), character: String(f.get("character") ?? "") });
return `/team?saved=${id}#agent-${id}`;
});
}
export async function toggleAgentAction(f: FormData) {
await run(async () => {
await updateAgent(db, Number(f.get("id")), { disabled: f.get("disabled") === "1" });
});
}