// Domain cards shared by pages: agent questions, runners, project list. import Link from "next/link"; import { BellRing, CircleCheck, CircleHelp, FileCheck, Hourglass, Play, RotateCcw, Send, ShieldQuestion, Square, SquareTerminal } from "lucide-react"; import { pausedProjectIds, messageDetails, questionAuthor, questionDetails, questionIsFreeText, questionState, questionText, questionType, splitForDisplay, trashDaysLeft, type Project, type Question, type SessionView, type StepView } from "@peppa/core"; import { db } from "@/lib/db"; import { MessagesSquare } from "lucide-react"; import { DeleteProjectControl, mayDeleteProject, RestoreProjectButton } from "@/components/ProjectDelete"; import type { AnalystLink } from "@peppa/core"; import { SubscriptionName } from "@/components/SubscriptionName"; import { PausedBadge } from "@/components/ProjectPause"; import { answerQuestion, fixStoryAction, messageSession, resumeTaskSession, stopTaskSession } from "@/app/(app)/actions"; import { Textarea } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { AgentBadge } from "@/components/AgentBadge"; import { Input } from "@/components/ui/input"; import { ago, Disclosure, Empty, Key, linkify, StatusBadge } from "@/components/common"; import { AgentSteps } from "@/components/AgentSteps"; import { Dictate } from "@/components/Dictate"; import { SessionUsageLine } from "@/components/Accounting"; import { cn } from "@/lib/utils"; import { cleanText } from "@/lib/chat-text"; import { getT } from "@/lib/i18n"; import { subscriptionNamesVisible, uiMode } from "@/lib/auth"; import { asIs, m } from "@peppa/shared/i18n"; const KIND = { approval: { label: m("Одобрение плана"), icon: FileCheck, border: "border-l-primary" }, question: { label: m("Вопрос"), icon: CircleHelp, border: "border-l-info" }, permission: { label: m("Разрешение"), icon: ShieldQuestion, border: "border-l-warn" }, attention: { label: m("Сообщение от фабрики"), icon: BellRing, border: "border-l-ok" }, } as const; /** Hidden inputs of a server-action form. */ export function Hidden({ fields }: { fields: Record }) { return ( <> {Object.entries(fields).map(([k, v]) => (v == null ? null : ))} ); } export async function QuestionCard({ q, taskKey, taskStatus, back, compact = false, plain = false, clean = false, signed = false }: { q: Question; taskKey?: string | null; taskStatus?: string | null; back: string; compact?: boolean; /** PEP-120: for a product manager — no question number, a terminal command only under «Подробнее». */ plain?: boolean; /** PEP-207: chats for a non-superadmin — no terminal output or paths in the main text. */ clean?: boolean; /** PEP-156: the author is already shown above the card (a chat line). */ signed?: boolean }) { const t = await getT(); const review = q.kind === "attention" && taskStatus === "review"; // PEP-207: a «готово» message stays one after the story moved on — its outcome is shown, not a form. const reviewCard = q.kind === "attention" && (q.msgType === "done" || (review && q.msgType == null)); const answered = !!q.answeredAt; const notice = questionType(q, taskStatus) === null && q.taskId == null && q.sessionId == null; // PEP-149: a factory message with answers to choose from is a question for the person. const k = KIND[q.kind === "attention" && !review && q.options?.length ? "question" : q.kind]; const Icon = review ? CircleCheck : k.icon; // PEP-151: the same state came again — one message with the time it was last refreshed. const state = await questionState(db, q.id).catch(() => null); const updatedAt = state?.updatedAt ? new Date(state.updatedAt) : null; // PEP-152: the main text — at most five lines; the rest and the technical details open under «Подробнее». const details = (await messageDetails(db, [q.id]).catch(() => null))?.get(q.id) ?? null; // A permission request keeps its command whole: that is what the person allows. const raw = t.text(questionText(q)); const shown = q.kind === "permission" ? { main: raw, more: details } : splitForDisplay(clean ? cleanText(raw, false, (process.env.GITEA_URL ?? "").replace(/\/+$/, "")) : raw, details ? t.text(details) : null); // PEP-156: an agent's own words carry its nickname; the rest is said by the factory. const author = await questionAuthor(db, q).catch(() => null); const fromAgent = author?.kind === "agent"; // PEP-154: what the filter removed (command output, paths) — for «Инженер» and a superadmin only. const original = (await uiMode()) === "engineer" || (await subscriptionNamesVisible()) ? await questionDetails(db, q.id).catch(() => null) : null; return (
{review ? t("Готово к ревью") : q.kind === "attention" && fromAgent ? t("Сообщение агента") : t(k.label)} {taskKey && {taskKey}} {!signed && author?.kind === "agent" && author.name && } {!plain && <>#{q.id} · }{updatedAt ? t("обновлено {ago}", { ago: ago(updatedAt, t) }) : ago(q.createdAt, t)}
{plain && q.kind === "permission" ? ( <>

{t("Агенту нужно ваше разрешение, чтобы продолжить работу.")}

{t("Подробнее")}

{t.text(questionText(q))}

{!!state?.repeats &&

{t("Повторов: {n} · первое сообщение {ago}", { n: state.repeats, ago: ago(q.createdAt, t) })}

}
) : (

{linkify(shown.main)}

)} {(shown.more || !!state?.repeats) && !(plain && q.kind === "permission") && (
{t("Подробнее")} {shown.more &&

{shown.more}

} {!!state?.repeats &&

{t("Повторов: {n} · первое сообщение {ago}", { n: state.repeats, ago: ago(q.createdAt, t) })}

}
)} {original && (
{t("Подробнее: исходный текст")}
{original}
)} {/* PEP-149: a notice about the product as a whole (released, backlog is empty) asks nothing — no answer form. */} {answered && reviewCard ? ( // PEP-207: the «готово» message after a press shows what came of it; no buttons are left.

{q.answeredVia === "system" ? : } {q.answeredVia === "system" && taskStatus === "done" ? t("Принято, {time}", { time: hhmm(q.answeredAt!) }) : q.answeredVia === "system" ? t("Уже не актуально") : /^✅/.test(q.answer ?? "") ? t("Вы приняли, {time}", { time: hhmm(q.answeredAt!) }) : t("Вы попросили поправить, {time}: {text}", { time: hhmm(q.answeredAt!), text: q.answer ?? "" })}

) : answered ? (

{q.answeredVia === "system" ? : } {q.answeredVia === "system" ? t.text(q.answer) : t("Ответ ({via}): {answer}", { via: q.answeredVia, answer: q.answer })}

) : review && taskKey ? ( // PEP-207: everything about the result right in the message — the same actions as on the task page.
{t("Поправить")}