feat: /usage 用量面板中文化(上游 0.10.2)
基于 @tmustier/pi-usage-extension 0.10.2(github.com/tmustier/pi-extensions 的 usage-extension/),仅改文案与 3 处中文宽度/排版补丁。 - zh-map.mjs:100 条词表(103 处替换),覆盖视图/周期/表头/快捷键/加载/洞察全部文案 - apply-zh.mjs:幂等应用器,上游改文案时报 MISS 而非静默漏译 - 本地补丁:x 轴日期改 ASCII M/D、分组横线用 visibleWidth、中文括号前不补空格 - 数据层:辅助用量伪渠道改 工具/摘要,CACHE_VERSION 6→7(名字会 intern 进缓存) - 保持英文:CSV 表头、JSON 键名、导出文件名,避免下游表格/脚本失配 - scripts/:渲染与数据/导出冒烟脚本
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env node
|
||||
// 数据冒烟:用 jiti 直接加载扩展的 data.ts / export.ts / graph.ts,
|
||||
// 跑一次采集(会写 ~/.pi/agent/usage-extension-cache.json),打印渠道名、总量、
|
||||
// 洞察文案与导出片段。
|
||||
//
|
||||
// 用法:
|
||||
// node scripts/usage-data-smoke.mjs [--dir <扩展目录>]
|
||||
//
|
||||
// 用途:确认文案替换没改坏数据层(模板串、正则、缓存版本),
|
||||
// 以及导出内容(CSV 表头语言、JSON 键)符合预期。
|
||||
|
||||
import { existsSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { dirname, join, resolve } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
|
||||
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
const argv = process.argv.slice(2);
|
||||
const i = argv.indexOf("--dir");
|
||||
const extDir = (i >= 0 && argv[i + 1] ? argv[i + 1] : repoRoot).replace(/^~(?=\/|$)/, homedir());
|
||||
|
||||
// jiti 随 pi 一起装在 agent 目录里;给几个候选位置
|
||||
const JITI_CANDIDATES = [
|
||||
process.env.JITI,
|
||||
join(homedir(), ".pi/agent/npm/node_modules/jiti/lib/jiti.mjs"),
|
||||
join(homedir(), ".pi/agent/npm/node_modules/jiti/lib/jiti.cjs"),
|
||||
].filter(Boolean);
|
||||
const jitiPath = JITI_CANDIDATES.find((p) => existsSync(p));
|
||||
if (!jitiPath) {
|
||||
console.error("找不到 jiti,请用 JITI=<...>/jiti/lib/jiti.mjs 指定");
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const { createJiti } = await import(pathToFileURL(jitiPath).href);
|
||||
const jiti = createJiti(import.meta.url);
|
||||
const data = await jiti.import(`${extDir}/data.ts`);
|
||||
const exp = await jiti.import(`${extDir}/export.ts`);
|
||||
const graph = await jiti.import(`${extDir}/graph.ts`);
|
||||
|
||||
const t0 = Date.now();
|
||||
const d = await data.collectUsageData({});
|
||||
console.log(`采集耗时 ${((Date.now() - t0) / 1000).toFixed(1)}s`);
|
||||
const stats = d.allTime;
|
||||
console.log("渠道/伪渠道行:", [...stats.providers.keys()].join(", "));
|
||||
console.log("总量:", JSON.stringify(stats.totals));
|
||||
for (const ins of stats.insights.insights) console.log(`- [${ins.kind}] ${ins.stat} ${ins.headline}\n ${ins.advice}`);
|
||||
|
||||
console.log("\n--- table CSV 前 3 行 ---");
|
||||
console.log(exp.buildTableCsv(stats.providers, stats.totals).split("\n").slice(0, 3).join("\n"));
|
||||
console.log("\n--- graph CSV 前 2 行 ---");
|
||||
const gm = graph.buildGraphModel(d.hourly, {
|
||||
period: "allTime",
|
||||
metric: "cost",
|
||||
groupBy: "provider",
|
||||
cumulative: true,
|
||||
hidden: new Set(),
|
||||
bounds: d.bounds,
|
||||
});
|
||||
console.log(exp.buildGraphCsv(gm).split("\n").slice(0, 2).join("\n"));
|
||||
console.log("\n--- insights JSON 前 8 行 ---");
|
||||
console.log(exp.buildInsightsJson("allTime", stats.totals, stats.insights.insights).split("\n").slice(0, 8).join("\n"));
|
||||
@@ -0,0 +1,116 @@
|
||||
#!/usr/bin/env node
|
||||
// 渲染冒烟:用 pi 自己的扩展加载器加载本扩展,绕过 TUI 直接调用 render(),
|
||||
// 把图表/表格/洞察三个视图按指定宽度打印成纯文本(去 ANSI),并报告超宽行数。
|
||||
//
|
||||
// 用法:
|
||||
// node scripts/usage-render-smoke.mjs # 默认 110 列,三视图
|
||||
// node scripts/usage-render-smoke.mjs --width 80 --views table,insights
|
||||
// PI_DIST=/path/to/@earendil-works/pi-coding-agent node scripts/usage-render-smoke.mjs
|
||||
//
|
||||
// 只读会话数据,不写任何文件(采集缓存由扩展自己维护)。
|
||||
|
||||
import { existsSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { dirname, join, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
|
||||
const argv = process.argv.slice(2);
|
||||
const arg = (name, fallback) => {
|
||||
const i = argv.indexOf(`--${name}`);
|
||||
return i >= 0 && argv[i + 1] ? argv[i + 1] : fallback;
|
||||
};
|
||||
|
||||
// pi 安装位置:优先 --pi-dist / PI_DIST,其次常见全局路径
|
||||
const PI_DIST_CANDIDATES = [
|
||||
arg("pi-dist", process.env.PI_DIST),
|
||||
join(homedir(), ".local/lib/node_modules/@agegr/pi-web/node_modules/@earendil-works/pi-coding-agent"),
|
||||
join(homedir(), ".local/lib/node_modules/@earendil-works/pi-coding-agent"),
|
||||
"/usr/lib/node_modules/@earendil-works/pi-coding-agent",
|
||||
"/usr/local/lib/node_modules/@earendil-works/pi-coding-agent",
|
||||
].filter(Boolean);
|
||||
const PI_DIST = PI_DIST_CANDIDATES.find((p) => existsSync(join(p, "dist/core/extensions/loader.js")));
|
||||
if (!PI_DIST) {
|
||||
console.error("找不到 pi 安装目录,请用 PI_DIST=<...>/pi-coding-agent 指定");
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const extDir = arg("dir", repoRoot).replace(/^~(?=\/|$)/, homedir());
|
||||
const width = Number(arg("width", "110"));
|
||||
const wantViews = arg("views", "graph,table,insights").split(",");
|
||||
|
||||
const { loadExtensions } = await import(`${PI_DIST}/dist/core/extensions/loader.js`);
|
||||
const res = await loadExtensions([extDir], process.cwd());
|
||||
if (!res.extensions?.length) {
|
||||
console.error("扩展加载失败:", JSON.stringify(res.errors ?? [], null, 2).slice(0, 2000));
|
||||
process.exit(1);
|
||||
}
|
||||
const cmd = res.extensions[0].commands.get("usage");
|
||||
if (!cmd) {
|
||||
console.error("未注册 usage 命令");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const strip = (s) => s.replace(/\x1b\[[0-9;]*m/g, "");
|
||||
// 主题桩:fg(color, text) / bold(text) 等一律返回原文本,便于纯文本比对。
|
||||
const theme = new Proxy({}, { get: () => (a, b) => (typeof a === "string" && typeof b === "string" ? b : a) });
|
||||
|
||||
let component = null;
|
||||
const tui = { requestRender() {}, terminal: { columns: width, rows: 60 } };
|
||||
const ctx = {
|
||||
hasUI: true,
|
||||
ui: {
|
||||
custom: async (factory) => {
|
||||
let value;
|
||||
const done = (v) => (value = v);
|
||||
const c = await factory({ requestRender() {}, terminal: tui.terminal }, theme, {}, done);
|
||||
if (c?.render && c?.handleInput) component = c;
|
||||
await new Promise((resolveWait) => {
|
||||
const timer = setInterval(() => {
|
||||
if (value !== undefined) {
|
||||
clearInterval(timer);
|
||||
resolveWait();
|
||||
}
|
||||
}, 50);
|
||||
setTimeout(() => {
|
||||
clearInterval(timer);
|
||||
resolveWait();
|
||||
}, 300_000);
|
||||
});
|
||||
return value;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
await cmd.handler("", ctx);
|
||||
if (!component) {
|
||||
console.error("未捕获渲染组件");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// pi 的 ui.custom 返回包装对象,只暴露 render/handleInput,读不到 viewMode,
|
||||
// 所以用本地游标跟踪当前视图(切换顺序 = 扩展内的 VIEW_CYCLE:graph → table → insights)。
|
||||
const VIEW_CYCLE = ["graph", "table", "insights"];
|
||||
let exitCode = 0;
|
||||
let current = 0; // 当前视图游标(初始 graph)
|
||||
for (const view of wantViews) {
|
||||
const target = VIEW_CYCLE.indexOf(view);
|
||||
if (target < 0) {
|
||||
console.error(`未知视图: ${view}(可选 ${VIEW_CYCLE.join("/")})`);
|
||||
current = 0;
|
||||
continue;
|
||||
}
|
||||
const presses = (target - current + VIEW_CYCLE.length) % VIEW_CYCLE.length;
|
||||
for (let i = 0; i < presses; i++) component.handleInput("v");
|
||||
const lines = component.render(width).map(strip);
|
||||
const over = lines.filter((l) => l.length > width);
|
||||
console.log(`\n===== view=${view} width=${width} lines=${lines.length} 超宽=${over.length} =====`);
|
||||
console.log(lines.join("\n"));
|
||||
current = target;
|
||||
if (over.length > 0) {
|
||||
exitCode = 1;
|
||||
for (const l of over) console.log("OVER:", l);
|
||||
}
|
||||
}
|
||||
process.exit(exitCode);
|
||||
Reference in New Issue
Block a user