80 lines
4.2 KiB
TypeScript
80 lines
4.2 KiB
TypeScript
// Telegram message texts and buttons for PM questions. HTML parse mode.
|
|
import type { Question, SessionView } from "@peppa/core";
|
|
import type { InlineButton } from "./telegram.ts";
|
|
|
|
export const esc = (s: string) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
const clip = (s: string, n: number) => (s.length > n ? `${s.slice(0, n)}…` : s);
|
|
|
|
const KIND: Record<Question["kind"], string> = {
|
|
approval: "📝 Нужно одобрение proposal",
|
|
question: "❓ Вопрос",
|
|
permission: "🔐 Нужно разрешение",
|
|
attention: "👀 Сессия ждёт тебя",
|
|
};
|
|
|
|
export interface Rendered {
|
|
text: string;
|
|
buttons: InlineButton[][];
|
|
}
|
|
|
|
// callback_data (≤64 bytes): "q:<questionId>:<choice>", "s:<sessionId>:<action>" or "i:<incidentId>:<decision>" (core/incidents.ts)
|
|
export function renderQuestion(
|
|
q: Question,
|
|
ctx: { taskKey: string | null; taskTitle: string | null; taskStatus?: string | null; session: SessionView | null; webUrl: string },
|
|
): Rendered {
|
|
const head = [`<b>${esc(ctx.taskKey ?? (q.sessionId ? `Сессия ${q.sessionId}` : "Релизы проекта"))}</b>`, ctx.taskTitle ? `<i>${esc(clip(ctx.taskTitle, 80))}</i>` : null]
|
|
.filter(Boolean)
|
|
.join(" · ");
|
|
const body = esc(clip(q.text, 3300));
|
|
const lines = [head, KIND[q.kind], "", body];
|
|
|
|
const buttons: InlineButton[][] = [];
|
|
if (q.kind === "approval") {
|
|
buttons.push([{ text: "✅ Одобрить", callback_data: `q:${q.id}:approve` }]);
|
|
lines.push("", "<i>Чтобы вернуть на доработку — ответь на это сообщение комментарием.</i>");
|
|
} else if (q.kind === "permission") {
|
|
buttons.push([
|
|
{ text: "✅ Разрешить", callback_data: `q:${q.id}:allow` },
|
|
{ text: "♾ Всегда", callback_data: `q:${q.id}:always` },
|
|
{ text: "⛔ Запретить", callback_data: `q:${q.id}:deny` },
|
|
]);
|
|
} else if (q.kind === "question") {
|
|
(q.options ?? []).slice(0, 6).forEach((o, i) => buttons.push([{ text: clip(o, 60), callback_data: `q:${q.id}:o${i}` }]));
|
|
lines.push("", "<i>Выбери вариант или ответь на это сообщение своим текстом.</i>");
|
|
} else if (ctx.taskStatus === "review") {
|
|
lines[1] = "✅ Готово к ревью";
|
|
buttons.push([{ text: "✅ Принять", callback_data: `q:${q.id}:accept` }]);
|
|
lines.push("", "<i>Есть замечания — ответь на это сообщение, агент доработает.</i>");
|
|
} else {
|
|
const s = ctx.session;
|
|
if (s && ["failed", "rate_limited", "stalled"].includes(s.status)) {
|
|
buttons.push([
|
|
{ text: "🔁 Перезапустить", callback_data: `s:${s.id}:resume` },
|
|
{ text: "⏹ Остановить", callback_data: `s:${s.id}:stop` },
|
|
]);
|
|
} else {
|
|
buttons.push([{ text: "▶️ Продолжай", callback_data: `q:${q.id}:cont` }]);
|
|
lines.push("", "<i>Или ответь на это сообщение — текст уйдёт в сессию.</i>");
|
|
}
|
|
}
|
|
if (ctx.session) lines.push(`<code>tmux attach -t ${esc(ctx.session.tmuxName)}</code>`);
|
|
return { text: lines.join("\n"), buttons };
|
|
}
|
|
|
|
/** Text after the question is closed: original + who answered what, no buttons. */
|
|
export function renderAnswered(original: string, q: Question): string {
|
|
const mark = q.answeredVia === "system" ? "⌛" : "✅";
|
|
const via = { telegram: "в Telegram", web: "в вебе", terminal: "в терминале", system: "" }[q.answeredVia ?? "telegram"];
|
|
return `${original}\n\n${mark} ${q.answeredVia === "system" ? esc(q.answer ?? "") : `Ответ ${via}: <b>${esc(clip(q.answer ?? "", 500))}</b>`}`;
|
|
}
|
|
|
|
/** The answer text a button stands for. */
|
|
export function choiceToAnswer(q: Question, choice: string): string | null {
|
|
if (q.kind === "approval" && choice === "approve") return "ок";
|
|
if (q.kind === "permission" && ["allow", "always", "deny"].includes(choice)) return choice;
|
|
if (q.kind === "attention" && choice === "cont") return "Продолжай.";
|
|
const m = /^o(\d+)$/.exec(choice);
|
|
if (q.kind === "question" && m) return q.options?.[Number(m[1])] ?? null;
|
|
return null;
|
|
}
|