- web: Gitea OAuth2 login, signed session cookie, everything behind login; inbox with answers, project board (goals → tasks), task page with OpenSpec artifacts, questions and session controls; new project form; auto-refresh, light/dark, mobile - users + projects.owner_id; tokens AES-256-GCM encrypted; owner-only access - provisionProject (runner): new or existing Gitea repo, peppa-bot collaborator, template + openspec init, push, rollback; used by web and peppa project create (replaces scripts/bootstrap-project.sh) - peppa web login-link, peppa project owner; Gitea OAuth app script - bins/template resolved via PEPPA_ROOT (works inside the Next build) - testkit: mock-gitea + 10 tests (42 e2e total, all green) - OpenSpec: pep-1-web-platform archived into specs web-auth, project-provisioning, web-workspace Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
125 lines
4.4 KiB
TypeScript
125 lines
4.4 KiB
TypeScript
"use server";
|
|
|
|
import {
|
|
createGoal,
|
|
createTask,
|
|
getQuestion,
|
|
getSession,
|
|
giteaForUser,
|
|
PeppaError,
|
|
projectForUser,
|
|
resolveTask,
|
|
} from "@peppa/core";
|
|
import { answerAndDeliver, provisionProject, resumeSession, sendToSession, startSession, stopSession, type Provider } from "@peppa/runner";
|
|
import { revalidatePath } from "next/cache";
|
|
import { redirect } from "next/navigation";
|
|
import { requireUser } from "@/lib/auth";
|
|
import { db } from "@/lib/db";
|
|
|
|
const str = (f: FormData, k: string) => String(f.get(k) ?? "").trim();
|
|
const back = (f: FormData) => str(f, "back") || "/";
|
|
|
|
/** Runs an action; errors come back to the page as ?error=… (redirect must stay outside try). */
|
|
async function attempt(f: FormData, fn: () => Promise<string | void>): Promise<never> {
|
|
let target = back(f);
|
|
try {
|
|
target = (await fn()) || target;
|
|
} catch (e) {
|
|
const msg = e instanceof PeppaError ? e.message : `Ошибка: ${(e as Error).message}`;
|
|
const sep = target.includes("?") ? "&" : "?";
|
|
target = `${target.split("?")[0]}${sep}error=${encodeURIComponent(msg.slice(0, 300))}`;
|
|
}
|
|
revalidatePath("/", "layout");
|
|
redirect(target);
|
|
}
|
|
|
|
async function ownedSession(userId: number, sessionId: number) {
|
|
const s = await getSession(db, sessionId);
|
|
await projectForUser(db, userId, s.projectId);
|
|
return s;
|
|
}
|
|
|
|
export async function answerQuestion(f: FormData) {
|
|
const user = await requireUser();
|
|
await attempt(f, async () => {
|
|
const q = await getQuestion(db, Number(str(f, "questionId")));
|
|
await projectForUser(db, user.id, q.projectId);
|
|
const answer = str(f, "choice") || str(f, "answer");
|
|
if (!answer) throw new PeppaError("Пустой ответ");
|
|
if (q.answeredAt) throw new PeppaError(`Уже отвечено${q.answeredVia === "system" ? "" : ` (${q.answeredVia})`}: ${q.answer}`);
|
|
await answerAndDeliver(db, q.id, answer, "web");
|
|
});
|
|
}
|
|
|
|
export async function addGoal(f: FormData) {
|
|
const user = await requireUser();
|
|
await attempt(f, async () => {
|
|
const p = await projectForUser(db, user.id, Number(str(f, "projectId")));
|
|
if (!str(f, "title")) throw new PeppaError("Нужно название цели");
|
|
await createGoal(db, p.id, str(f, "title"), str(f, "description") || undefined);
|
|
});
|
|
}
|
|
|
|
export async function addTask(f: FormData) {
|
|
const user = await requireUser();
|
|
await attempt(f, async () => {
|
|
const p = await projectForUser(db, user.id, Number(str(f, "projectId")));
|
|
if (!str(f, "title")) throw new PeppaError("Нужно название задачи");
|
|
const goalId = str(f, "goalId") ? Number(str(f, "goalId")) : undefined;
|
|
const ref = await createTask(db, { projectId: p.id, goalId, title: str(f, "title"), description: str(f, "description") || undefined });
|
|
return `/t/${ref.key}`;
|
|
});
|
|
}
|
|
|
|
export async function startTaskSession(f: FormData) {
|
|
const user = await requireUser();
|
|
await attempt(f, async () => {
|
|
const ref = await resolveTask(db, str(f, "taskKey"));
|
|
await projectForUser(db, user.id, ref.project.id);
|
|
await startSession(db, ref.key, { provider: (str(f, "provider") || "claude") as Provider, prompt: str(f, "prompt") || undefined });
|
|
});
|
|
}
|
|
|
|
export async function stopTaskSession(f: FormData) {
|
|
const user = await requireUser();
|
|
await attempt(f, async () => {
|
|
const s = await ownedSession(user.id, Number(str(f, "sessionId")));
|
|
await stopSession(db, s.id);
|
|
});
|
|
}
|
|
|
|
export async function resumeTaskSession(f: FormData) {
|
|
const user = await requireUser();
|
|
await attempt(f, async () => {
|
|
const s = await ownedSession(user.id, Number(str(f, "sessionId")));
|
|
await resumeSession(db, s.id);
|
|
});
|
|
}
|
|
|
|
export async function messageSession(f: FormData) {
|
|
const user = await requireUser();
|
|
await attempt(f, async () => {
|
|
const s = await ownedSession(user.id, Number(str(f, "sessionId")));
|
|
if (!str(f, "text")) throw new PeppaError("Пустое сообщение");
|
|
await sendToSession(db, s.id, str(f, "text"));
|
|
});
|
|
}
|
|
|
|
export async function createProjectAction(f: FormData) {
|
|
const user = await requireUser();
|
|
await attempt(f, async () => {
|
|
const owner = str(f, "owner") || user.login;
|
|
const p = await provisionProject(db, {
|
|
gitea: await giteaForUser(db, user),
|
|
owner,
|
|
ownerIsUser: owner === user.login,
|
|
slug: str(f, "slug").toLowerCase(),
|
|
key: str(f, "key"),
|
|
name: str(f, "name"),
|
|
existing: str(f, "mode") === "existing",
|
|
ownerId: user.id,
|
|
});
|
|
return `/p/${p.key}`;
|
|
});
|
|
}
|