// 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 (
{t("Сессия #{id} уже завершена — открыта другая.", { id: gone })}