122 lines
6.6 KiB
TypeScript
122 lines
6.6 KiB
TypeScript
// glossary-ignore-file: раздел «Сессии» — терминалы агентов, только в виде «Инженер» (PEP-257)
|
|
// «Сессии» (PEP-257): one place for the terminals of all live agent sessions of the user's
|
|
// projects. Left — the list, right — the terminal of the chosen one, open right away: the first
|
|
// of the list without `?id`, the given one with it (a story links here with its session).
|
|
import Link from "next/link";
|
|
import { SquareTerminal } from "lucide-react";
|
|
import { FINISHED_SESSION_STATUSES, listUserSessions, PROVIDER_NAMES, type SessionView } from "@peppa/core";
|
|
import { AgentBadge } from "@/components/AgentBadge";
|
|
import { AttachCommand } from "@/components/cards";
|
|
import { ago, Empty, Key, PageHeader, StatusBadge } from "@/components/common";
|
|
import { LiveRefresh } from "@/components/LiveRefresh";
|
|
import { RunnerTerminal } from "@/components/RunnerTerminal";
|
|
import { Workspace } from "@/components/Workspace";
|
|
import { requireUser } from "@/lib/auth";
|
|
import { db } from "@/lib/db";
|
|
import { inbox } from "@/lib/queries";
|
|
import { SESSION_STATUS, TONE_BG, TONE_TEXT } from "@/lib/status";
|
|
import { cn } from "@/lib/utils";
|
|
import { getT } from "@/lib/i18n";
|
|
|
|
export default async function Sessions({ searchParams }: { searchParams: Promise<{ id?: string }> }) {
|
|
const user = await requireUser();
|
|
const t = await getT();
|
|
const { id } = await searchParams;
|
|
const [all, open] = await Promise.all([listUserSessions(db, user.id), inbox(user.id)]);
|
|
const asking = new Set(open.map((q) => q.sessionId).filter(Boolean));
|
|
const needsPm = (s: SessionView) => s.status === "waiting_input" || asking.has(s.id);
|
|
// Live ones only: who needs a person first, then the newest.
|
|
const live = all.filter((s) => !FINISHED_SESSION_STATUSES.includes(s.status)).sort((a, b) => Number(needsPm(b)) - Number(needsPm(a)) || b.id - a.id);
|
|
const wanted = id ? Number(id) : null;
|
|
const selected = (wanted && live.find((s) => s.id === wanted)) || live[0];
|
|
// The link pointed at a session that has ended (or is not the user's): say so, show the first.
|
|
const gone = wanted && selected?.id !== wanted ? wanted : null;
|
|
|
|
return (
|
|
<Workspace aside={null} wide>
|
|
<LiveRefresh scope="runners" />
|
|
<PageHeader title={t("Сессии")}>{t("Живые сессии агентов ваших проектов: выберите слева — справа сразу её терминал.")}</PageHeader>
|
|
{gone && (
|
|
<p className="mb-3 rounded-md border border-warn/40 bg-warn/10 px-3 py-2 text-sm" data-session-gone={gone}>
|
|
{t("Сессия #{id} уже завершена — открыта другая.", { id: gone })}
|
|
</p>
|
|
)}
|
|
{!selected ? (
|
|
<Empty>
|
|
<span className="flex flex-col items-center gap-2">
|
|
<SquareTerminal className="size-6" />
|
|
<span>{t("Живых сессий нет.")}</span>
|
|
<span>{t("Агент запускается, когда стори переносят в «В работе» на доске проекта, или кнопкой «Новый терминал» в «Работе агентов».")}</span>
|
|
</span>
|
|
</Empty>
|
|
) : (
|
|
<div className="grid gap-4 lg:grid-cols-[minmax(240px,300px)_minmax(0,1fr)]" data-sessions>
|
|
<nav aria-label={t("Список сессий")} className="min-w-0">
|
|
<ul className="m-0 flex list-none flex-col gap-1 rounded-lg border bg-card/50 p-1.5 lg:max-h-[calc(100dvh-10rem)] lg:overflow-y-auto">
|
|
{live.map((s) => (
|
|
<SessionRow key={s.id} s={s} selected={s.id === selected.id} needsPm={needsPm(s)} />
|
|
))}
|
|
</ul>
|
|
</nav>
|
|
<SessionPane key={selected.id} s={selected} />
|
|
</div>
|
|
)}
|
|
</Workspace>
|
|
);
|
|
}
|
|
|
|
async function SessionRow({ s, selected, needsPm }: { s: SessionView; selected: boolean; needsPm: boolean }) {
|
|
const t = await getT();
|
|
const st = SESSION_STATUS[s.status] ?? { label: s.status, tone: "muted" as const };
|
|
return (
|
|
<li>
|
|
<Link
|
|
href={`/sessions?id=${s.id}`}
|
|
scroll={false}
|
|
data-hotkey-item
|
|
data-session-row={s.id}
|
|
aria-current={selected ? "true" : undefined}
|
|
className={cn(
|
|
"grid grid-cols-[auto_minmax(0,1fr)] items-center gap-x-2.5 gap-y-0.5 rounded-md border border-transparent px-2.5 py-2 text-sm text-foreground transition-colors hover:bg-accent hover:no-underline",
|
|
needsPm && "bg-primary/[0.07] shadow-[inset_2px_0_0_var(--primary)]",
|
|
selected && "border-border bg-accent",
|
|
)}
|
|
>
|
|
<span className={cn("row-span-3 size-2 self-start mt-1.5 rounded-full", TONE_BG[st.tone], (s.status === "running" || s.status === "starting") && "animate-pulse")} />
|
|
<span className="flex min-w-0 items-baseline gap-2">
|
|
<span className="shrink-0 font-mono text-xs text-primary">{s.taskKey ?? s.projectKey}</span>
|
|
<span className="truncate">{s.taskTitle ?? s.name ?? t("без стори")}</span>
|
|
</span>
|
|
<span className="flex min-w-0 flex-wrap items-center gap-x-1.5 font-mono text-[11px]">
|
|
<span className={TONE_TEXT[st.tone]}>{t(st.label)}</span>
|
|
<span className="text-muted-foreground">· {PROVIDER_NAMES[s.provider]}</span>
|
|
</span>
|
|
<span className="truncate font-mono text-[11px] text-muted-foreground">
|
|
{s.agentName ? `${s.agentName} · ` : ""}#{s.id} · {ago(s.lastActivityAt, t)}
|
|
</span>
|
|
</Link>
|
|
</li>
|
|
);
|
|
}
|
|
|
|
async function SessionPane({ s }: { s: SessionView }) {
|
|
const t = await getT();
|
|
return (
|
|
<section aria-label={t("Терминал")} className="flex min-w-0 flex-col gap-2.5 lg:h-[calc(100dvh-10rem)]" data-session-pane={s.id}>
|
|
<header className="flex flex-wrap items-center gap-x-2 gap-y-1 text-sm">
|
|
<StatusBadge status={s.status} kind="session" />
|
|
{s.taskKey ? <Key href={`/t/${s.taskKey}`}>{s.taskKey}</Key> : <Key href={`/p/${s.projectKey}`}>{s.projectKey}</Key>}
|
|
<span className="min-w-0 flex-[1_1_12em] truncate font-medium">{s.taskTitle ?? s.name ?? t("без стори")}</span>
|
|
{s.agentName && <AgentBadge name={s.agentName} />}
|
|
<span className="font-mono text-xs text-muted-foreground">
|
|
{PROVIDER_NAMES[s.provider]} · <Link href={`/runners?id=${s.id}`} className="text-muted-foreground hover:text-foreground">#{s.id}</Link>
|
|
</span>
|
|
</header>
|
|
<AttachCommand name={s.tmuxName} />
|
|
<div className="h-[70dvh] min-h-0 lg:h-auto lg:flex-1">
|
|
<RunnerTerminal sessionId={s.id} live fill />
|
|
</div>
|
|
</section>
|
|
);
|
|
}
|