/** Представление очереди разбора: компактные строки, markdown-таблица для текста и HTML-приложение (MCP Apps) с кнопками. */ type Json = Record; /** Прежний URI (оставлен рабочим: клиенты могут держать его в кэше). */ export const REVIEW_UI_URI_LEGACY = "ui://accounting_automation/review-table"; export const REVIEW_UI_MIME = "text/html;profile=mcp-app"; export interface ReviewRow { id: string; kind: string; role: string; date: string | null; amount: string | null; who: string | null; what: string | null; confidence: number | null; problem: string | null; snoozed: boolean; file_url: string | null; } const s = (v: unknown): string | null => (v === null || v === undefined || v === "" ? null : String(v)); const day = (v: unknown): string | null => { const x = s(v); return x ? x.slice(0, 10) : null; }; /** Одна строка таблицы из задачи списка (task + review). */ export function toRow(t: Json): ReviewRow { const review = (t.review ?? {}) as Json; const kind = String(t.kind); let date: string | null = null; let amount: string | null = null; let who: string | null = null; let what: string | null = null; let confidence: number | null = typeof review.confidence === "number" ? review.confidence : null; if (kind === "categorization") { const sg = (review.suggestion ?? {}) as Json; date = day(sg.date); amount = s(sg.amount); who = s(sg.vendor); what = [s(sg.account), sg.project_customer_id ? `проект ${String(sg.project_customer_id)}` : null].filter(Boolean).join(", ") || s(review.feed_txn_id); } else if (kind === "invoice") { date = day(review.invoice_date); amount = s(review.amount); who = s(review.vendor_name); what = [s(review.invoice_number) ? `№ ${String(review.invoice_number)}` : null, review.project_customer_id ? `проект ${String(review.project_customer_id)}` : null].filter(Boolean).join(", ") || null; confidence = null; } else if (kind === "certificate") { date = day(review.expires_on); who = s(review.vendor_name); what = "сертификат страхования"; confidence = null; } return { id: String(t.id), kind, role: String(t.assignee_role), date, amount, who, what, confidence, problem: s(t.problem_highlight), snoozed: t.snoozed_until != null, file_url: s(t.file_url), }; } const cell = (v: string | number | null, max = 60): string => { const x = v === null ? "" : String(v).replace(/\s+/g, " ").replace(/\|/g, "/"); return x.length > max ? `${x.slice(0, max - 1)}…` : x; }; /** Текстовая таблица для хостов без поддержки интерактивных приложений и для самой модели. */ export function rowsToMarkdown(rows: ReviewRow[], total: number): string { if (rows.length === 0) return "Очередь разбора пуста."; const head = "| id | вид | дата | сумма | кто | что | % | проблема | роль |\n|---|---|---|---|---|---|---|---|---|"; const body = rows.map( (r) => `| ${r.id.slice(0, 8)} | ${r.kind} | ${cell(r.date)} | ${cell(r.amount)} | ${cell(r.who, 30)} | ${cell(r.what, 40)} | ${cell(r.confidence)} | ${cell(r.problem, 80)} | ${r.role} |`, ); return `${head}\n${body.join("\n")}\n\nПоказано ${rows.length} из ${total}. Действия по нескольким задачам - одним вызовом acc_review_batch (id - первые 8 символов из таблицы).`; } /** HTML-приложение (MCP Apps): таблица очереди с кнопками Done / Delegate / Snooze. Данные приходят в ui/notifications/tool-result. */ export const REVIEW_UI_HTML = String.raw` Очередь разбора бухгалтерии
Загрузка…
`; import { createHash } from "node:crypto"; /** URI с хэшем содержимого: изменённое приложение не подменяется устаревшей копией из кэша клиента. */ export const REVIEW_UI_URI = `ui://accounting_automation/review-table-${createHash("sha256").update(REVIEW_UI_HTML).digest("hex").slice(0, 8)}`;