- PEP-5: statusLine hook (cost, context, 5h/7d limit windows), transcript tokens, sessions.usage, usage strip over every page, runner usage, bot /status, MCP get_usage - PEP-43: project/story budgets in settings, watchdog pause (Escape, question to the PM: extend x2 / remove limit / stop), start/resume guard, budget on the story card - PEP-47: project metrics tab (cycle time, first pass, review returns, questions, runner hours, PM wait, limits, spend by week/agent/epic), MCP get_metrics - PEP-48: event authors (actor columns, AsyncLocalStorage context), question_answered, /journal for superadmins, project journal tab, authors in the story feed - Migration 0007_usage_audit; OpenSpec changes archived Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
123 lines
4.5 KiB
TypeScript
123 lines
4.5 KiB
TypeScript
"use server";
|
|
// «Подписки» (PEP-67): every action is superadmin-only (others get "not found").
|
|
|
|
import { createPool, deletePool, getProject, PeppaError, requireSuperadmin, setProjectPool, updatePool, updateSubscription, type SubscriptionProvider } from "@peppa/core";
|
|
import { addSubscription, cancelLogin, checkSubscription, finishClaudeLogin, removeSubscription, startClaudeLogin, startCodexLogin } from "@peppa/runner";
|
|
import { revalidatePath } from "next/cache";
|
|
import { redirect } from "next/navigation";
|
|
import { asWebActor, requireUser } from "@/lib/auth";
|
|
import { db } from "@/lib/db";
|
|
|
|
const str = (f: FormData, k: string) => String(f.get(k) ?? "").trim();
|
|
const ids = (f: FormData, k: string) => f.getAll(k).map(Number).filter((n) => Number.isInteger(n) && n > 0);
|
|
const BASE = "/subscriptions";
|
|
|
|
/** Runs a superadmin action; errors come back as ?error=… (redirect stays outside try). */
|
|
async function attempt(f: FormData, fn: () => Promise<string | void>): Promise<never> {
|
|
const user = asWebActor(await requireUser());
|
|
let target = str(f, "back") || BASE;
|
|
try {
|
|
await requireSuperadmin(db, user);
|
|
target = (await fn()) || target;
|
|
} catch (e) {
|
|
const msg = e instanceof PeppaError ? e.message : `Ошибка: ${(e as Error).message}`;
|
|
const [path, query] = target.split("?");
|
|
const params = new URLSearchParams(query);
|
|
params.set("error", msg.slice(0, 300));
|
|
target = `${path}?${params}`;
|
|
}
|
|
revalidatePath("/", "layout");
|
|
redirect(target);
|
|
}
|
|
|
|
export async function addSubscriptionAction(f: FormData) {
|
|
const user = asWebActor(await requireUser());
|
|
await attempt(f, async () => {
|
|
const provider = str(f, "provider") as SubscriptionProvider;
|
|
if (!["claude", "codex", "opencode"].includes(provider)) throw new PeppaError("Неизвестный провайдер");
|
|
const s = await addSubscription(db, {
|
|
provider,
|
|
name: str(f, "name"),
|
|
method: str(f, "method"),
|
|
secret: str(f, "secret") || undefined,
|
|
keyProvider: str(f, "keyProvider") || undefined,
|
|
createdBy: user.id,
|
|
});
|
|
return s.status === "pending" ? `${BASE}?login=${s.id}#s-${s.id}` : `${BASE}?added=${s.id}#s-${s.id}`;
|
|
});
|
|
}
|
|
|
|
export async function reconnectSubscription(f: FormData) {
|
|
await attempt(f, async () => {
|
|
const id = Number(str(f, "id"));
|
|
if (str(f, "provider") === "codex") await startCodexLogin(db, id);
|
|
else await startClaudeLogin(db, id);
|
|
return `${BASE}?login=${id}#s-${id}`;
|
|
});
|
|
}
|
|
|
|
export async function submitLoginCode(f: FormData) {
|
|
await attempt(f, async () => {
|
|
const id = Number(str(f, "id"));
|
|
await finishClaudeLogin(db, id, str(f, "code"));
|
|
return `${BASE}?added=${id}#s-${id}`;
|
|
});
|
|
}
|
|
|
|
export async function cancelLoginAction(f: FormData) {
|
|
await attempt(f, async () => {
|
|
await cancelLogin(db, Number(str(f, "id")));
|
|
});
|
|
}
|
|
|
|
export async function checkSubscriptionAction(f: FormData) {
|
|
await attempt(f, async () => {
|
|
const id = Number(str(f, "id"));
|
|
await checkSubscription(db, id);
|
|
return `${BASE}#s-${id}`;
|
|
});
|
|
}
|
|
|
|
export async function renameSubscription(f: FormData) {
|
|
await attempt(f, async () => {
|
|
if (!str(f, "name")) throw new PeppaError("Нужно название подписки");
|
|
await updateSubscription(db, Number(str(f, "id")), { name: str(f, "name") });
|
|
});
|
|
}
|
|
|
|
export async function deleteSubscriptionAction(f: FormData) {
|
|
await attempt(f, async () => {
|
|
await removeSubscription(db, Number(str(f, "id")));
|
|
});
|
|
}
|
|
|
|
export async function createPoolAction(f: FormData) {
|
|
const user = asWebActor(await requireUser());
|
|
await attempt(f, async () => {
|
|
await createPool(db, { name: str(f, "name"), subscriptionIds: ids(f, "subscription"), createdBy: user.id });
|
|
return `${BASE}#pools`;
|
|
});
|
|
}
|
|
|
|
export async function updatePoolAction(f: FormData) {
|
|
await attempt(f, async () => {
|
|
await updatePool(db, Number(str(f, "id")), { name: str(f, "name") || undefined, subscriptionIds: ids(f, "subscription") });
|
|
return `${BASE}#pools`;
|
|
});
|
|
}
|
|
|
|
export async function deletePoolAction(f: FormData) {
|
|
await attempt(f, async () => {
|
|
await deletePool(db, Number(str(f, "id")));
|
|
return `${BASE}#pools`;
|
|
});
|
|
}
|
|
|
|
/** «⋯ Настройки проекта» → пул по умолчанию (only a superadmin changes it). */
|
|
export async function setProjectPoolAction(f: FormData) {
|
|
await attempt(f, async () => {
|
|
const p = await getProject(db, str(f, "projectKey"));
|
|
await setProjectPool(db, p.id, str(f, "poolId") ? Number(str(f, "poolId")) : null);
|
|
});
|
|
}
|