72 lines
3.2 KiB
TypeScript
72 lines
3.2 KiB
TypeScript
// PEP-174: «Поставить на паузу» / «Продолжить» for a project. The pause itself is a setting
|
||
// (core/project-pause.ts) every place that starts agents, calls the AI roles or notifies people
|
||
// checks; here the live agents are stopped and, on «Продолжить», resumed.
|
||
import {
|
||
activateRunnerTask,
|
||
clearProjectPause,
|
||
getProjectPause,
|
||
getSession,
|
||
listSessions,
|
||
logEvent,
|
||
setProjectPause,
|
||
type ProjectPause,
|
||
} from "@peppa/core";
|
||
import type { Db } from "@peppa/db";
|
||
import { resumeSession, stopSession } from "./sessions.ts";
|
||
|
||
const WORKING = ["proposing", "in_progress"];
|
||
|
||
const stamp = (ms: number) =>
|
||
new Date(ms).toLocaleString("ru-RU", { day: "2-digit", month: "2-digit", hour: "2-digit", minute: "2-digit", timeZone: process.env.PEPPA_TZ || undefined });
|
||
|
||
/**
|
||
* Puts the project on pause: from this moment nothing starts and nobody is notified; the live
|
||
* agents are stopped (branches and working copies stay). Returns the agents stopped; already on
|
||
* pause — nothing changes.
|
||
*/
|
||
export async function pauseProject(db: Db, projectId: number, by: number | null, now = new Date()): Promise<{ stopped: number; already: boolean }> {
|
||
if (await getProjectPause(db, projectId)) return { stopped: 0, already: true };
|
||
const live = await listSessions(db, { active: true, projectId });
|
||
// First the mark: whatever comes in while the agents stop is refused already.
|
||
const pause: ProjectPause = { since: now.getTime(), by, sessions: live.map((s) => s.id) };
|
||
await setProjectPause(db, projectId, pause);
|
||
let stopped = 0;
|
||
for (const s of live) {
|
||
try {
|
||
await stopSession(db, s.id);
|
||
stopped++;
|
||
} catch {
|
||
// Gone already — nothing to stop.
|
||
}
|
||
}
|
||
await logEvent(db, { projectId, type: "project_paused", payload: { sessions: pause.sessions } });
|
||
return { stopped, already: false };
|
||
}
|
||
|
||
/**
|
||
* Ends the pause: the agents stopped by it continue their stories (same conversation), the
|
||
* dispatcher takes stories again on its next tick. What happened during the pause is not sent.
|
||
*/
|
||
export async function resumeProject(db: Db, projectId: number, now = new Date()): Promise<{ resumed: number; was: boolean }> {
|
||
const pause = await clearProjectPause(db, projectId, now);
|
||
if (!pause) return { resumed: 0, was: false };
|
||
await logEvent(db, { projectId, type: "project_resumed", payload: { from: pause.since, to: now.getTime(), fromText: stamp(pause.since), toText: stamp(now.getTime()) } });
|
||
let resumed = 0;
|
||
for (const id of pause.sessions) {
|
||
try {
|
||
const s = await getSession(db, id);
|
||
if (s.taskId) {
|
||
// The story moved on during the pause, or another agent took it: leave it.
|
||
if (!s.taskStatus || !WORKING.includes(s.taskStatus)) continue;
|
||
if ((await listSessions(db, { active: true, taskId: s.taskId })).length) continue;
|
||
await activateRunnerTask(db, s.id, s.taskId);
|
||
}
|
||
await resumeSession(db, s.id, { prompt: "Проект был на паузе — продолжай с того места, где остановился." });
|
||
resumed++;
|
||
} catch {
|
||
// Not resumable (working copy gone, no subscription): the dispatcher or the PM picks it up.
|
||
}
|
||
}
|
||
return { resumed, was: true };
|
||
}
|