Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/server/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { formatUpstreamError, getUpstreamConnectionStatus, proxyDispatcher, reco
import { detectStaleInstall } from "../update.js";
import { PACKAGE_NAME, VERSION, BUILD_COMMIT } from "../version.js";
import { buildOverview, buildSessionDetail, buildSessionList, buildSessionPage, handleConfigGet, handleConfigPut, hiddenEmptyCount, renderUI } from "../web/index.js";
import { buildSessionReportMd, buildSessionReportSvg } from "../web/report.js";
import { queryLogLines } from "../web/logs-query.js";
import { buildDiagnostics } from "../web/diagnostics.js";
import { storageStatusWire } from "../storage/runtime-status.js";
Expand Down Expand Up @@ -156,6 +157,7 @@ export async function handleAdminRoute(req: http.IncomingMessage, res: http.Serv
}
if (req.method === "GET" && req.url === "/__bili/stats") return sendStats(res);
if (req.method === "GET" && req.url?.startsWith("/__bili/cache-report")) return sendCacheReport(res, req.url);
if (req.method === "GET" && req.url?.startsWith("/__bili/session-report")) return sendSessionReport(res, req.url);
if (req.method === "GET" && req.url === "/__bili/status") return sendStatus(res, opts);
if (req.method === "GET" && req.url === "/__bili/resign") return sendResignStatus(res);
if (req.method === "GET" && req.url === "/__bili/overview") return sendOverview(res, opts);
Expand Down Expand Up @@ -1000,3 +1002,28 @@ async function sendWebSessionDetail(res: http.ServerResponse, url: string): Prom
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify(detail, null, 2));
}

async function sendSessionReport(res: http.ServerResponse, url: string): Promise<void> {
const q = new URL(url, "http://localhost").searchParams;
const id = q.get("session");
if (!id) {
res.writeHead(400, { "content-type": "application/json" });
res.end(JSON.stringify({ error: "missing session" }));
return;
}
const detail = await buildSessionDetail(id);
if (!detail) {
res.writeHead(404, { "content-type": "application/json" });
res.end(JSON.stringify({ error: `unknown session: ${id}` }));
return;
}
const opts = { mask: q.get("mask") !== "off", lang: q.get("lang") === "en" ? "en" : "zh-CN" };
if (q.get("format") === "svg") {
const r = buildSessionReportSvg(detail, opts);
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify({ svg: r.svg, w: r.w, h: r.h }));
return;
}
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify({ md: buildSessionReportMd(detail, opts) }));
}
55 changes: 55 additions & 0 deletions src/web/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,7 @@ export const WEB_CLIENT = `(function () {
const parts = [];
parts.push('<a class="btn sm" href="#/sessions">' + t("common.back") + "</a>");
parts.push('<div class="page-head"><div><h1 title="' + escapeHtml(d.title || d.label || d.id) + '">' + escapeHtml(d.title || d.label || d.id.slice(0, 16)) + '</h1><div class="sub mono">' + escapeHtml(d.id) + "</div></div><div>" + detailBadges(d) + "</div></div>");
parts.push('<div style="display:flex;align-items:center;gap:8px;margin-top:12px;flex-wrap:wrap"><span class="dim small">' + t("det.report_note") + '</span><button id="sessrpt-md" class="btn sm">' + t("det.report_md") + '</button><button id="sessrpt-png" class="btn sm">' + t("det.report_png") + '</button></div>');
parts.push('<div class="card"><div class="card-h"><span>' + t("det.identity") + '</span></div><div class="card-b"><dl class="kv">');
// Full title wraps in place, is hoverable (title attr) and carries a copy button.
parts.push('<div class="k">' + t("common.title") + '</div><div class="v" title="' + escapeHtml(d.title || "") + '">' + (d.title ? escapeHtml(d.title) + ' <button id="title-copy" class="btn sm">' + t("common.copy") + "</button>" : '<span class="faint">' + t("common.none") + "</span>") + "</div>");
Expand Down Expand Up @@ -1514,6 +1515,7 @@ export const WEB_CLIENT = `(function () {
bindHandoffActions(d);
bindBlocksActions(d);
bindCacheReportActions(d);
bindSessionReportActions(d);
const cc = $("session-conflicts-clear");
if (cc) {
cc.addEventListener("click", async () => {
Expand Down Expand Up @@ -1622,6 +1624,59 @@ export const WEB_CLIENT = `(function () {
d.blockDetails.forEach((b) => { L.push(""); L.push(buildBlockMd(b)); });
return L.join("\\n");
}
// #3057 report export: MD/SVG render server-side (/__bili/session-report); the browser downloads MD and rasterizes the closed-vector SVG to PNG.
function downloadUrl(url, name) {
var a = document.createElement("a");
a.href = url; a.download = name;
document.body.appendChild(a); a.click(); a.remove();
setTimeout(function () { URL.revokeObjectURL(url); }, 4000);
}
function svgToPngBlob(svgStr, w, h) {
return new Promise(function (resolve, reject) {
var scale = Math.max(1, Math.min(2, 1600 / w));
var outW = Math.round(w * scale), outH = Math.round(h * scale);
if (outH > 14000) { scale = 14000 / h; outW = Math.round(w * scale); outH = Math.round(h * scale); }
var img = new Image();
var url = URL.createObjectURL(new Blob([svgStr], { type: "image/svg+xml;charset=utf-8" }));
img.onload = function () {
try {
var canvas = document.createElement("canvas");
canvas.width = outW; canvas.height = outH;
var ctx = canvas.getContext("2d");
ctx.fillStyle = "#ffffff"; ctx.fillRect(0, 0, outW, outH);
ctx.drawImage(img, 0, 0, outW, outH);
URL.revokeObjectURL(url);
canvas.toBlob(function (b) { b ? resolve(b) : reject(new Error("PNG encode failed")); }, "image/png");
} catch (e) { URL.revokeObjectURL(url); reject(e); }
};
img.onerror = function () { URL.revokeObjectURL(url); reject(new Error("SVG rasterize failed")); };
img.src = url;
});
}
function bindSessionReportActions(d) {
var mdBtn = $("sessrpt-md"), pngBtn = $("sessrpt-png");
if (!mdBtn && !pngBtn) return;
var safeId = String(d.id).replace(/[^A-Za-z0-9._-]/g, "_");
var base = "billion-context-report-" + safeId;
var q = "session=" + encodeURIComponent(d.id) + "&mask=on&lang=" + (locale === "en" ? "en" : "zh-CN");
if (mdBtn) mdBtn.addEventListener("click", async function () {
busy(mdBtn, true);
try {
var r = await json("/__bili/session-report?format=md&" + q);
if (!r || typeof r.md !== "string") throw new Error("no report");
downloadUrl(URL.createObjectURL(new Blob([r.md], { type: "text/markdown;charset=utf-8" })), base + ".md");
} catch (e) { toast(t("toast.failed", { msg: e.message }), "err"); } finally { busy(mdBtn, false); }
});
if (pngBtn) pngBtn.addEventListener("click", async function () {
busy(pngBtn, true);
try {
var r = await json("/__bili/session-report?format=svg&" + q);
if (!r || !r.svg) throw new Error("no report");
var blob = await svgToPngBlob(r.svg, r.w || 920, r.h || 800);
downloadUrl(URL.createObjectURL(blob), base + ".png");
} catch (e) { toast(t("toast.failed", { msg: e.message }), "err"); } finally { busy(pngBtn, false); }
});
}
function bindBlocksActions(d) {
if (!d.blockDetails || !d.blockDetails.length) return;
const cp = $("blocks-copy");
Expand Down
16 changes: 16 additions & 0 deletions src/web/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,14 @@ export const MESSAGES: Record<Locale, Record<string, string>> = {
"det.block_inactive": "已归档",
"det.blocks_download": "下载块 (.md)",
"det.report_dl": "下载缓存报告 (.md)",
"det.report_md": "下载完整报告 · Markdown",
"det.report_png": "下载完整报告 · 图片(PNG)",
"det.report_note": "标题等隐私默认掩码",
"det.report_masked": "[已掩码]",
"det.rpt_input": "输入",
"det.rpt_cached": "已缓存",
"det.rpt_missed": "未命中",
"det.rpt_hit": "命中率",
"det.blocks_copy_md": "复制 Markdown",
"det.handoff": "会话交接文档",
"det.handoff_hint": "与 bili export 相同的内容 —— 复制粘贴到新会话即可继续",
Expand Down Expand Up @@ -783,6 +791,14 @@ export const MESSAGES: Record<Locale, Record<string, string>> = {
"det.block_inactive": "inactive",
"det.blocks_download": "download blocks (.md)",
"det.report_dl": "download cache report (.md)",
"det.report_md": "full report · Markdown",
"det.report_png": "full report · image (PNG)",
"det.report_note": "title & other PII masked by default",
"det.report_masked": "[masked]",
"det.rpt_input": "input",
"det.rpt_cached": "cached",
"det.rpt_missed": "missed",
"det.rpt_hit": "hit %",
"det.blocks_copy_md": "copy Markdown",
"det.handoff": "Handoff document",
"det.handoff_hint": "Same content as bili export — paste into a new session to continue",
Expand Down
Loading
Loading