支持导入定价文档并在统计页估算费用。

把用量与单价对齐,方便对照各模型实际开销。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-14 01:14:20 +08:00
co-authored by Cursor
parent 752eaccefa
commit 6bd180bb7b
9 changed files with 1999 additions and 39 deletions
+176
View File
@@ -0,0 +1,176 @@
import type { TokenUsageRecord } from "./token-store.js";
import {
findModelRate,
resolveRateCatalog,
type ResolvedRate,
} from "./pricing.js";
import type { PeakWindow, RateBand } from "./pricing-parse.js";
export type Money = {
amount: number;
currency: string;
};
export type RecordCost = {
cost: Money | null;
unmatched: boolean;
sourceName?: string;
};
export type ModelCostRow = {
model: string;
calls: number;
promptTokens: number;
completionTokens: number;
totalTokens: number;
cachedTokens: number;
cacheMissTokens: number;
cost: Money | null;
unmatched: boolean;
sourceName?: string;
sourceUrl?: string;
};
export type CostReport = {
total: Money | null;
byCurrency: Record<string, number>;
unpricedCalls: number;
pricedCalls: number;
byModel: ModelCostRow[];
};
export function isPeakAt(
at: Date,
windows: PeakWindow[] | undefined,
weekdays: number[] | undefined,
): boolean {
if (!windows?.length) return false;
if (weekdays?.length && !weekdays.includes(at.getUTCDay())) return false;
const minutes = at.getUTCHours() * 60 + at.getUTCMinutes();
return windows.some((w) => minutes >= w.startMinutes && minutes < w.endMinutes);
}
export function splitPromptTokens(record: TokenUsageRecord): {
cached: number;
miss: number;
output: number;
} {
const cached = Math.max(0, record.cachedTokens ?? 0);
let miss = record.cacheMissTokens;
if (miss == null) {
miss = Math.max(0, record.promptTokens - cached);
}
const covered = cached + miss;
if (covered < record.promptTokens) {
miss += record.promptTokens - covered;
}
return { cached, miss, output: Math.max(0, record.completionTokens) };
}
function bandPrice(band: RateBand | undefined, peak: boolean): number {
if (!band) return 0;
if (peak && band.peak != null) return band.peak;
return band.offPeak;
}
export function costForRecord(
record: TokenUsageRecord,
rate: ResolvedRate,
): Money {
const at = new Date(record.at);
const peak = isPeakAt(at, rate.peakWindows, rate.peakWeekdays);
const parts = splitPromptTokens(record);
const hitPrice = rate.inputCacheHit
? bandPrice(rate.inputCacheHit, peak)
: bandPrice(rate.inputCacheMiss, peak);
const missPrice = bandPrice(rate.inputCacheMiss, peak);
const outPrice = bandPrice(rate.output, peak);
const unit = rate.unitTokens || 1_000_000;
const amount =
(parts.cached * hitPrice + parts.miss * missPrice + parts.output * outPrice) /
unit;
return { amount: Math.round(amount * 1e10) / 1e10, currency: rate.currency };
}
export function attachRecordCost(
record: TokenUsageRecord,
catalog = resolveRateCatalog(),
): RecordCost {
const rate = findModelRate(record.model || "", catalog);
if (!rate) return { cost: null, unmatched: true };
return {
cost: costForRecord(record, rate),
unmatched: false,
sourceName: rate.sourceName,
};
}
export function buildCostReport(
records: TokenUsageRecord[],
catalog = resolveRateCatalog(),
): CostReport {
const byModel = new Map<string, ModelCostRow>();
const byCurrency: Record<string, number> = {};
let unpricedCalls = 0;
let pricedCalls = 0;
for (const record of records) {
const model = record.model?.trim() || "(未填型号)";
const rate = findModelRate(record.model || "", catalog);
const priced = rate ? costForRecord(record, rate) : null;
if (priced) {
pricedCalls += 1;
byCurrency[priced.currency] = (byCurrency[priced.currency] ?? 0) + priced.amount;
} else {
unpricedCalls += 1;
}
const prev = byModel.get(model) ?? {
model,
calls: 0,
promptTokens: 0,
completionTokens: 0,
totalTokens: 0,
cachedTokens: 0,
cacheMissTokens: 0,
cost: priced ? { amount: 0, currency: priced.currency } : null,
unmatched: !rate,
sourceName: rate?.sourceName,
sourceUrl: rate?.sourceUrl,
};
prev.calls += 1;
prev.promptTokens += record.promptTokens;
prev.completionTokens += record.completionTokens;
prev.totalTokens += record.totalTokens;
prev.cachedTokens += record.cachedTokens ?? 0;
prev.cacheMissTokens += record.cacheMissTokens ?? 0;
if (priced) {
if (!prev.cost) prev.cost = { amount: 0, currency: priced.currency };
if (prev.cost.currency === priced.currency) {
prev.cost.amount += priced.amount;
}
prev.unmatched = false;
}
byModel.set(model, prev);
}
const currencies = Object.keys(byCurrency);
const total =
currencies.length === 1
? { amount: byCurrency[currencies[0]], currency: currencies[0] }
: null;
const rows = [...byModel.values()].sort((a, b) => {
const ac = a.cost?.amount ?? -1;
const bc = b.cost?.amount ?? -1;
return bc - ac;
});
return {
total,
byCurrency,
unpricedCalls,
pricedCalls,
byModel: rows,
};
}
+615
View File
@@ -0,0 +1,615 @@
/** 从官方价目文档(HTML / Markdown / JSON)抽出按型号的单价。 */
export type PeakWindow = {
/** 距 UTC 当天 00:00 的分钟数,含起点不含终点 */
startMinutes: number;
endMinutes: number;
};
export type RateBand = {
offPeak: number;
peak?: number;
};
export type ModelRate = {
model: string;
aliases: string[];
currency: string;
unitTokens: number;
inputCacheHit?: RateBand;
inputCacheMiss: RateBand;
output: RateBand;
peakWindows?: PeakWindow[];
/** JS getUTCDay:0 日 … 6 六。省略则每天都可能高峰 */
peakWeekdays?: number[];
};
export type ParsePricingResult = {
title?: string;
rates: ModelRate[];
warnings: string[];
};
const UNIT_1M = 1_000_000;
const MODEL_STOP = new Set(
[
"model",
"models",
"pricing",
"features",
"peak",
"off-peak",
"offpeak",
"input",
"output",
"tokens",
"token",
"cache",
"hit",
"miss",
"thinking",
"maximum",
"vision",
"usd",
"cny",
"base",
"url",
"openai",
"anthropic",
"format",
"version",
"concurrency",
"limit",
].map((s) => s.toLowerCase()),
);
export function parsePricingDocument(
text: string,
contentType = "text/html",
): ParsePricingResult {
const trimmed = text.trim();
const type = contentType.toLowerCase();
if (type.includes("json") || (trimmed.startsWith("{") && trimmed.includes("pricing"))) {
const jsonResult = parsePricingJson(trimmed);
if (jsonResult.rates.length) return jsonResult;
}
const html = /<table[\s>]/i.test(trimmed) ? parsePricingHtml(trimmed) : { rates: [], warnings: [] as string[], title: pageTitle(trimmed) };
if (html.rates.length) {
return decorate(html, trimmed);
}
const md = parsePricingMarkdown(stripTags(trimmed));
if (md.rates.length) return decorate(md, trimmed);
return {
title: pageTitle(trimmed),
rates: [],
warnings: ["未能从文档解析出型号价格"],
};
}
function decorate(result: ParsePricingResult, source: string): ParsePricingResult {
const peak = parsePeakSchedule(source);
const aliases = parseLegacyAliases(source, result.rates.map((r) => r.model));
const rates = result.rates.map((r) => ({
...r,
aliases: unique([...(r.aliases ?? []), ...(aliases.get(r.model.toLowerCase()) ?? [])]),
...(peak
? { peakWindows: peak.windows, peakWeekdays: peak.weekdays }
: {}),
}));
return {
title: result.title ?? pageTitle(source),
rates,
warnings: result.warnings,
};
}
function parsePricingHtml(html: string): ParsePricingResult {
const tables = extractHtmlTables(html).map(expandTableGrid);
return extractFromGrids(tables, html);
}
function parsePricingMarkdown(text: string): ParsePricingResult {
const tables = extractMarkdownTables(text);
return extractFromGrids(tables, text);
}
function extractFromGrids(grids: string[][][], source: string): ParsePricingResult {
const warnings: string[] = [];
const rates: ModelRate[] = [];
const currencyHint = detectCurrency(source);
const unit = detectUnitTokens(source);
for (const grid of grids) {
const fromMatrix = extractDeepSeekStyle(grid, currencyHint, unit);
if (fromMatrix.length) {
rates.push(...fromMatrix);
continue;
}
const generic = extractGenericHeaderTable(grid, currencyHint, unit);
rates.push(...generic);
}
const uniqueRates = mergeRatesByModel(rates);
if (!uniqueRates.length) {
warnings.push("表格里没有识别到单价");
}
return { title: pageTitle(source), rates: uniqueRates, warnings };
}
function extractDeepSeekStyle(
grid: string[][],
currencyHint: string,
unit: number,
): ModelRate[] {
let models: string[] = [];
for (const row of grid) {
const idx = row.findIndex((c) => /^model$/i.test(cleanCell(c)));
if (idx < 0) continue;
const found = row
.slice(idx + 1)
.map(cleanCell)
.filter((c) => looksLikeModelId(c));
if (found.length) {
models = unique(found);
break;
}
}
if (!models.length) return [];
const hit: Partial<Record<string, number[]>> = {};
const miss: Partial<Record<string, number[]>> = {};
const output: Partial<Record<string, number[]>> = {};
let currency = currencyHint;
for (const row of grid) {
const cells = row.map(cleanCell);
const joined = cells.join(" ").toLowerCase();
const band = bandOf(joined);
if (!band) continue;
const money = parseMoneyCells(row);
if (money.length < models.length) continue;
const slice = money.slice(-models.length);
currency = slice[0]?.currency ?? currency;
const amounts = slice.map((m) => m.amount);
if (/cache\s*hit/.test(joined) || /缓存命中/.test(joined)) {
hit[band] = amounts;
} else if (/cache\s*miss/.test(joined) || /缓存未命中/.test(joined)) {
miss[band] = amounts;
} else if (/output/.test(joined) || /输出/.test(joined)) {
output[band] = amounts;
}
}
if (!miss.offPeak && !output.offPeak) return [];
return models.map((model, i) => {
const missOff = miss.offPeak?.[i] ?? miss.peak?.[i];
const outOff = output.offPeak?.[i] ?? output.peak?.[i];
const missPeak = miss.peak?.[i];
const outPeak = output.peak?.[i];
const hitOff = hit.offPeak?.[i];
const hitPeak = hit.peak?.[i];
const inputMiss = missOff ?? missPeak;
const out = outOff ?? outPeak;
if (inputMiss == null || out == null) {
return null;
}
const rate: ModelRate = {
model,
aliases: [],
currency,
unitTokens: unit,
inputCacheMiss: {
offPeak: inputMiss,
...(missPeak != null && missPeak !== inputMiss ? { peak: missPeak } : {}),
},
output: {
offPeak: out,
...(outPeak != null && outPeak !== out ? { peak: outPeak } : {}),
},
};
if (hitOff != null || hitPeak != null) {
const off = hitOff ?? hitPeak!;
rate.inputCacheHit = {
offPeak: off,
...(hitPeak != null && hitPeak !== off ? { peak: hitPeak } : {}),
};
}
return rate;
}).filter((r): r is ModelRate => r != null);
}
function extractGenericHeaderTable(
grid: string[][],
currencyHint: string,
unit: number,
): ModelRate[] {
if (grid.length < 2) return [];
const header = grid[0].map(cleanCell);
const kinds = header.map(headerKind);
const modelCol = kinds.indexOf("model");
const inputCol = kinds.indexOf("input");
const cachedCol = kinds.indexOf("cached");
const outputCol = kinds.indexOf("output");
if (modelCol < 0 || inputCol < 0 || outputCol < 0) return [];
const rates: ModelRate[] = [];
for (const row of grid.slice(1)) {
const model = cleanCell(row[modelCol] ?? "");
if (!looksLikeModelId(model)) continue;
const input = parsePriceCell(row[inputCol] ?? "", currencyHint);
const output = parsePriceCell(row[outputCol] ?? "", currencyHint);
if (!input || !output) continue;
const cached =
cachedCol >= 0 ? parsePriceCell(row[cachedCol] ?? "", currencyHint) : null;
const currency = input.currency ?? output.currency ?? currencyHint;
const rate: ModelRate = {
model,
aliases: [],
currency,
unitTokens: unit,
inputCacheMiss: { offPeak: input.amount },
output: { offPeak: output.amount },
};
if (cached) rate.inputCacheHit = { offPeak: cached.amount };
rates.push(rate);
}
return rates;
}
function headerKind(cell: string): "model" | "input" | "cached" | "output" | "other" {
const t = cell.toLowerCase().replace(/\s+/g, " ").trim();
if (/模型名称|^(model|型号|模型)$/i.test(t) || t === "id") return "model";
if (/缓存存储|cache(?:d)?\s*input\s*storage/.test(t)) return "other";
if (/cache\s*hit|cached(?:\s+input)?$|缓存命中/.test(t)) return "cached";
if (/output|completion|输出/.test(t)) return "output";
if (/input|prompt|cache\s*miss|输入/.test(t)) return "input";
return "other";
}
function bandOf(joined: string): "offPeak" | "peak" | null {
if (/off[- ]?peak|闲时|非高峰|低峰/.test(joined)) return "offPeak";
if (/(^|[^a-z])peak([^a-z]|$)|高峰/.test(joined)) return "peak";
return null;
}
export function parsePeakSchedule(text: string): {
windows: PeakWindow[];
weekdays: number[];
} | null {
const plain = stripTags(text).replace(/\s+/g, " ");
const windows: PeakWindow[] = [];
const utcRange =
/peak hours are\s+(\d{1,2}:\d{2})\s*[-–—]\s*(\d{1,2}:\d{2})\s+and\s+(\d{1,2}:\d{2})\s*[-–—]\s*(\d{1,2}:\d{2})\s*utc/i.exec(
plain,
) ??
/高峰.*?(\d{1,2}:\d{2})\s*[-–—]\s*(\d{1,2}:\d{2}).*?(\d{1,2}:\d{2})\s*[-–—]\s*(\d{1,2}:\d{2}).*utc/i.exec(
plain,
);
if (utcRange) {
windows.push(
{ startMinutes: parseHhMm(utcRange[1]), endMinutes: parseHhMm(utcRange[2]) },
{ startMinutes: parseHhMm(utcRange[3]), endMinutes: parseHhMm(utcRange[4]) },
);
}
if (!windows.length) return null;
const weekdays = /monday through friday|周一至周五|星期一至星期五/i.test(plain)
? [1, 2, 3, 4, 5]
: [0, 1, 2, 3, 4, 5, 6];
return { windows, weekdays };
}
export function parseLegacyAliases(
text: string,
models: string[],
): Map<string, string[]> {
const map = new Map<string, string[]>();
const codes = [
...text.matchAll(/<code\b[^>]*>([^<]+)<\/code>/gi),
...text.matchAll(/`([a-zA-Z][a-zA-Z0-9._:+/-]{1,80})`/g),
].map((m) => cleanCell(m[1]));
const plain = stripTags(text).replace(/\s+/g, " ");
const flash = models.find((m) => /flash/i.test(m));
if (
flash &&
/legacy names[\s\S]{0,800}billed at the flash price/i.test(plain)
) {
const legacy = codes.filter(
(c) => /flash/i.test(c) && c.toLowerCase() !== flash.toLowerCase(),
);
if (legacy.length) map.set(flash.toLowerCase(), unique(legacy));
}
return map;
}
function parsePricingJson(text: string): ParsePricingResult {
try {
const data = JSON.parse(text) as unknown;
const rates: ModelRate[] = [];
const rows = jsonModelRows(data);
for (const row of rows) {
const model = String(row.id ?? row.model ?? "").trim();
if (!model) continue;
const pricing = (row.pricing ?? row) as Record<string, unknown>;
const prompt = jsonPrice(pricing.prompt ?? pricing.input ?? pricing.inputCacheMiss);
const completion = jsonPrice(pricing.completion ?? pricing.output);
if (prompt == null || completion == null) continue;
const cached = jsonPrice(pricing.input_cache_read ?? pricing.cached ?? pricing.inputCacheHit);
// OpenRouter 等接口用「每个 token」;官方价目表通常是每 1M。
const perToken = prompt > 0 && prompt < 1e-4;
const rate: ModelRate = {
model: model.includes("/") ? model.split("/").pop()! : model,
aliases: model.includes("/") ? [model] : [],
currency: "USD",
unitTokens: UNIT_1M,
inputCacheMiss: { offPeak: perToken ? prompt * UNIT_1M : prompt },
output: { offPeak: perToken ? completion * UNIT_1M : completion },
};
if (cached != null) {
rate.inputCacheHit = { offPeak: perToken ? cached * UNIT_1M : cached };
}
rates.push(rate);
}
return { rates: mergeRatesByModel(rates), warnings: rates.length ? [] : ["JSON 中没有单价"] };
} catch {
return { rates: [], warnings: ["JSON 解析失败"] };
}
}
function jsonModelRows(data: unknown): Array<Record<string, unknown>> {
if (Array.isArray(data)) return data.filter(isRecord);
if (!isRecord(data)) return [];
if (Array.isArray(data.models)) return data.models.filter(isRecord);
if (Array.isArray(data.data)) return data.data.filter(isRecord);
if (Array.isArray(data.rates)) return data.rates.filter(isRecord);
return [];
}
function jsonPrice(value: unknown): number | null {
if (typeof value === "number" && Number.isFinite(value)) return value;
if (typeof value === "string") {
const n = Number(value);
return Number.isFinite(n) ? n : null;
}
return null;
}
function isRecord(v: unknown): v is Record<string, unknown> {
return typeof v === "object" && v != null && !Array.isArray(v);
}
type RawCell = { text: string; colspan: number; rowspan: number };
function extractHtmlTables(html: string): RawCell[][][] {
const tables: RawCell[][][] = [];
const re = /<table\b[^>]*>([\s\S]*?)<\/table>/gi;
let m: RegExpExecArray | null;
while ((m = re.exec(html))) {
tables.push(parseHtmlTableRows(m[1]));
}
return tables;
}
function parseHtmlTableRows(inner: string): RawCell[][] {
const rows: RawCell[][] = [];
const trRe = /<tr\b[^>]*>([\s\S]*?)<\/tr>/gi;
let tr: RegExpExecArray | null;
while ((tr = trRe.exec(inner))) {
const cells: RawCell[] = [];
const tdRe = /<t[dh]\b([^>]*)>([\s\S]*?)<\/t[dh]>/gi;
let td: RegExpExecArray | null;
while ((td = tdRe.exec(tr[1]))) {
const attrs = td[1];
cells.push({
text: cellText(td[2]),
colspan: attrInt(attrs, "colspan") || 1,
rowspan: attrInt(attrs, "rowspan") || 1,
});
}
if (cells.length) rows.push(cells);
}
return rows;
}
function expandTableGrid(rows: RawCell[][]): string[][] {
const occupier: Array<{ left: number; text: string } | undefined> = [];
const grid: string[][] = [];
for (const row of rows) {
const out: string[] = [];
let col = 0;
let i = 0;
while (i < row.length || occupier.some((o, idx) => idx >= col && o && o.left > 0)) {
const occ = occupier[col];
if (occ && occ.left > 0) {
out[col] = occ.text;
occ.left -= 1;
col += 1;
continue;
}
if (i >= row.length) break;
const cell = row[i++];
for (let k = 0; k < cell.colspan; k++) {
while (occupier[col]?.left) {
out[col] = occupier[col]!.text;
occupier[col]!.left -= 1;
col += 1;
}
out[col] = cell.text;
if (cell.rowspan > 1) {
occupier[col] = { left: cell.rowspan - 1, text: cell.text };
}
col += 1;
}
}
grid.push(out);
}
return grid;
}
function extractMarkdownTables(text: string): string[][][] {
const lines = text.split(/\r?\n/);
const tables: string[][][] = [];
let current: string[][] = [];
const flush = () => {
if (current.length >= 2) tables.push(current);
current = [];
};
for (const line of lines) {
if (!/^\s*\|/.test(line)) {
flush();
continue;
}
if (/^\s*\|?\s*:?-{3,}/.test(line)) continue;
const cells = line
.replace(/^\s*\|/, "")
.replace(/\|\s*$/, "")
.split("|")
.map((c) => cleanCell(c));
current.push(cells);
}
flush();
return tables;
}
function attrInt(attrs: string, name: string): number {
const m = new RegExp(`${name}\\s*=\\s*["']?(\\d+)`, "i").exec(attrs);
return m ? Number(m[1]) : 0;
}
function cellText(raw: string): string {
return cleanCell(
raw
.replace(/<sup\b[^>]*>[\s\S]*?<\/sup>/gi, "")
.replace(/<br\s*\/?>/gi, " ")
.replace(/<\/(p|div|tr|h\d)>/gi, " "),
);
}
export function cleanCell(raw: string): string {
return decodeEntities(stripTags(raw))
.replace(/\u00a0/g, " ")
.replace(/\s+/g, " ")
.trim();
}
function stripTags(html: string): string {
return html.replace(/<script[\s\S]*?<\/script>/gi, " ").replace(/<[^>]+>/g, " ");
}
function decodeEntities(s: string): string {
return s
.replace(/&amp;/g, "&")
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&quot;/g, '"')
.replace(/&#39;/g, "'")
.replace(/&nbsp;/g, " ");
}
function pageTitle(text: string): string | undefined {
const m = /<title[^>]*>([\s\S]*?)<\/title>/i.exec(text);
if (!m) return undefined;
return decodeEntities(m[1]).replace(/\s+/g, " ").trim() || undefined;
}
function looksLikeModelId(s: string): boolean {
const t = s.trim();
if (t.length < 2 || t.length > 80) return false;
if (MODEL_STOP.has(t.toLowerCase())) return false;
if (!/^[a-zA-Z][a-zA-Z0-9._:+/-]*$/.test(t)) return false;
if (/^(https?:|www\.)/i.test(t)) return false;
return /[-/._]/.test(t) || /^(gpt|o[1-9]|claude|gemini|deepseek|qwen|glm|mistral|llama)/i.test(t);
}
function parseMoneyCells(row: string[]): Array<{ amount: number; currency: string }> {
const out: Array<{ amount: number; currency: string }> = [];
for (const cell of row) {
const m = parseMoneyCell(cell);
if (m) out.push(m);
}
return out;
}
function parsePriceCell(
cell: string,
currencyHint: string,
): { amount: number; currency: string } | null {
const t = cleanCell(cell);
if (!t) return null;
if (/^(?:[-–—\\/]|不支持|暂不支持)$/i.test(t)) return null;
if (/^(free|免费)$/i.test(t)) return { amount: 0, currency: currencyHint };
const money = parseMoneyCell(t);
if (money) return money;
return parseBareNumber(t, currencyHint);
}
function parseMoneyCell(cell: string): { amount: number; currency: string } | null {
const t = cleanCell(cell);
const m =
/^[\$]\s*([0-9]+(?:\.[0-9]+)?)\b/.exec(t) ||
/^[¥¥]\s*([0-9]+(?:\.[0-9]+)?)\b/.exec(t) ||
/^([0-9]+(?:\.[0-9]+)?)\s*元/.exec(t);
if (!m) return null;
const amount = Number(m[1]);
if (!Number.isFinite(amount)) return null;
const currency = t.includes("¥") || t.includes("¥") || t.includes("元") ? "CNY" : "USD";
return { amount, currency };
}
function parseBareNumber(
cell: string,
currencyHint = "USD",
): { amount: number; currency: string } | null {
const t = cleanCell(cell);
if (!/^[0-9]+(?:\.[0-9]+)?$/.test(t)) return null;
const amount = Number(t);
return Number.isFinite(amount) ? { amount, currency: currencyHint } : null;
}
function detectCurrency(text: string): string {
if (/元\s*[//]\s*百万|元\/百万|人民币/.test(text)) return "CNY";
if (/\$\s*[0-9]|prices are in usd/i.test(text)) return "USD";
if (/[¥¥]|CNY/.test(text)) return "CNY";
return "USD";
}
function detectUnitTokens(text: string): number {
if (/1\s*[Mm]\s*tokens|per\s*1M|每\s*100\s*万|百万\s*token/i.test(text)) return UNIT_1M;
return UNIT_1M;
}
function parseHhMm(s: string): number {
const [h, m] = s.split(":").map(Number);
return h * 60 + (m || 0);
}
function unique(xs: string[]): string[] {
const seen = new Set<string>();
const out: string[] = [];
for (const x of xs) {
const k = x.toLowerCase();
if (seen.has(k)) continue;
seen.add(k);
out.push(x);
}
return out;
}
function mergeRatesByModel(rates: ModelRate[]): ModelRate[] {
const map = new Map<string, ModelRate>();
for (const r of rates) {
const key = r.model.toLowerCase();
const prev = map.get(key);
if (!prev) {
map.set(key, { ...r, aliases: unique(r.aliases) });
continue;
}
map.set(key, {
...prev,
...r,
aliases: unique([...prev.aliases, ...r.aliases]),
});
}
return [...map.values()];
}
+275
View File
@@ -0,0 +1,275 @@
import { readFileSync, writeFileSync } from "node:fs";
import path from "node:path";
import { randomUUID } from "node:crypto";
import { ensureUserDataDirs, getUserDataDir } from "../config/user-data-dir.js";
import {
parsePricingDocument,
type ModelRate,
} from "./pricing-parse.js";
export type PricingSource = {
id: string;
name: string;
url: string;
createdAt: string;
fetchedAt?: string;
fetchError?: string;
title?: string;
rates: ModelRate[];
};
type PricingFile = {
version: 1;
sources: PricingSource[];
};
const FILE_NAME = "pricing-sources.json";
const FETCH_TIMEOUT_MS = 20_000;
const MAX_BYTES = 2_000_000;
export const SUGGESTED_PRICING_DOCS: Array<{ name: string; url: string }> = [
{
name: "DeepSeek Models & Pricing",
url: "https://api-docs.deepseek.com/quick_start/pricing",
},
{
name: "DeepSeek 模型与价格",
url: "https://api-docs.deepseek.com/zh-cn/quick_start/pricing",
},
{
name: "智谱 GLM API 定价",
url: "https://docs.bigmodel.cn/cn/guide/start/pricing",
},
{
name: "Z.AI GLM Pricing",
url: "https://docs.z.ai/guides/overview/pricing",
},
];
function filePath(): string {
return path.join(getUserDataDir(), FILE_NAME);
}
function emptyFile(): PricingFile {
return { version: 1, sources: [] };
}
function readFile(): PricingFile {
ensureUserDataDirs();
try {
const raw = readFileSync(filePath(), "utf8");
const parsed = JSON.parse(raw) as PricingFile;
if (parsed.version !== 1 || !Array.isArray(parsed.sources)) return emptyFile();
return parsed;
} catch {
return emptyFile();
}
}
function writeFile(data: PricingFile): void {
ensureUserDataDirs();
writeFileSync(filePath(), JSON.stringify(data, null, 2), "utf8");
}
export function listPricingSources(): PricingSource[] {
return readFile().sources;
}
export function getPricingSource(id: string): PricingSource | null {
return listPricingSources().find((s) => s.id === id) ?? null;
}
export function normalizePricingUrl(raw: string): string {
const trimmed = raw.trim();
let url: URL;
try {
url = new URL(trimmed);
} catch {
throw new Error("不是合法 URL");
}
if (url.protocol !== "http:" && url.protocol !== "https:") {
throw new Error("只支持 http/https 官方文档");
}
url.hash = "";
if (url.pathname.length > 1) {
url.pathname = url.pathname.replace(/\/+$/, "");
}
return url.toString();
}
export async function addPricingSource(input: {
url: string;
name?: string;
}): Promise<PricingSource> {
const url = normalizePricingUrl(input.url);
const data = readFile();
const existing = data.sources.find((s) => s.url === url);
if (existing) {
return refreshPricingSource(existing.id);
}
const source: PricingSource = {
id: randomUUID(),
name: input.name?.trim() || hostLabel(url),
url,
createdAt: new Date().toISOString(),
rates: [],
};
data.sources.push(source);
writeFile(data);
return refreshPricingSource(source.id);
}
export function deletePricingSource(id: string): boolean {
const data = readFile();
const next = data.sources.filter((s) => s.id !== id);
if (next.length === data.sources.length) return false;
writeFile({ version: 1, sources: next });
return true;
}
export async function refreshPricingSource(id: string): Promise<PricingSource> {
const data = readFile();
const source = data.sources.find((s) => s.id === id);
if (!source) throw new Error("价目来源不存在");
try {
const parsed = await fetchAndParsePricing(source.url);
source.fetchedAt = new Date().toISOString();
source.title = parsed.title;
source.rates = parsed.rates;
if (parsed.title && looksGenericName(source.name)) {
source.name = shortenTitle(parsed.title);
}
source.fetchError = parsed.rates.length
? undefined
: parsed.warnings[0] || "未能从文档解析出型号价格";
} catch (err) {
source.fetchedAt = new Date().toISOString();
source.fetchError = err instanceof Error ? err.message : String(err);
}
writeFile(data);
return source;
}
export type FetchedDoc = { contentType: string; text: string };
export function markdownDocFallbacks(url: string): string[] {
try {
const parsed = new URL(url);
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return [];
if (parsed.pathname.endsWith(".md")) return [];
const docPath = parsed.pathname.replace(/\/+$/, "") || "/";
parsed.hash = "";
parsed.search = "";
parsed.pathname = `${docPath}.md`;
return [parsed.toString()];
} catch {
return [];
}
}
export async function fetchAndParsePricing(url: string): Promise<{
title?: string;
rates: ModelRate[];
warnings: string[];
}> {
const candidates = [url, ...markdownDocFallbacks(url)];
let lastError = "未能从文档解析出型号价格";
let lastTitle: string | undefined;
for (const candidate of candidates) {
try {
const doc = await fetchPricingDocument(candidate);
const parsed = parsePricingDocument(doc.text, doc.contentType);
lastTitle = parsed.title ?? lastTitle;
if (parsed.rates.length) return parsed;
lastError = parsed.warnings[0] || lastError;
} catch (err) {
lastError = err instanceof Error ? err.message : String(err);
}
}
return { title: lastTitle, rates: [], warnings: [lastError] };
}
export async function fetchPricingDocument(url: string): Promise<FetchedDoc> {
const res = await fetch(url, {
redirect: "follow",
headers: {
Accept: "text/html, text/markdown, application/json, text/plain;q=0.9",
"User-Agent": "WritingAgent/0.1 (local token cost)",
},
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
});
if (!res.ok) {
throw new Error(`拉取失败 HTTP ${res.status}`);
}
const buf = Buffer.from(await res.arrayBuffer());
if (buf.length > MAX_BYTES) {
throw new Error("文档过大");
}
const contentType = res.headers.get("content-type") ?? "text/html";
return { contentType, text: buf.toString("utf8") };
}
export type ResolvedRate = ModelRate & {
sourceId: string;
sourceName: string;
sourceUrl: string;
};
export function resolveRateCatalog(sources = listPricingSources()): {
byKey: Map<string, ResolvedRate>;
rates: ResolvedRate[];
} {
const byKey = new Map<string, ResolvedRate>();
for (const source of sources) {
for (const rate of source.rates) {
const resolved: ResolvedRate = {
...rate,
sourceId: source.id,
sourceName: source.name,
sourceUrl: source.url,
};
byKey.set(rate.model.toLowerCase(), resolved);
for (const alias of rate.aliases) {
byKey.set(alias.toLowerCase(), resolved);
}
}
}
const seen = new Set<string>();
const rates: ResolvedRate[] = [];
for (const rate of byKey.values()) {
const id = `${rate.sourceId}:${rate.model.toLowerCase()}`;
if (seen.has(id)) continue;
seen.add(id);
rates.push(rate);
}
return { byKey, rates };
}
export function findModelRate(
model: string,
catalog = resolveRateCatalog(),
): ResolvedRate | undefined {
const key = model.trim().toLowerCase();
if (!key) return undefined;
const exact = catalog.byKey.get(key);
if (exact) return exact;
const tail = key.split("/").pop();
if (tail && tail !== key) return catalog.byKey.get(tail);
return undefined;
}
function hostLabel(url: string): string {
try {
return new URL(url).hostname.replace(/^www\./, "");
} catch {
return url;
}
}
function looksGenericName(name: string): boolean {
return /^(https?:|www\.|[a-z0-9.-]+\.[a-z]{2,}$)/i.test(name);
}
function shortenTitle(title: string): string {
return title.replace(/\s*[·|]\s*.*$/, "").trim() || title;
}
+5 -2
View File
@@ -129,7 +129,7 @@ export function readRecords(query: TokenStatsQuery = {}): TokenUsageRecord[] {
const lines = readFileSync(file, "utf8").split(/\r?\n/).filter(Boolean);
for (let j = lines.length - 1; j >= 0 && records.length < limit; j--) {
try {
const record = JSON.parse(lines[j]) as TokenUsageRecord;
const record = JSON.parse(lines[j].trim()) as TokenUsageRecord;
if (query.bookId && record.bookId !== query.bookId) continue;
if (query.orchestratorId && record.orchestratorId !== query.orchestratorId) {
continue;
@@ -171,7 +171,10 @@ function accumulateCaller(
}
export function summarizeTokenUsage(query: TokenStatsQuery = {}): TokenStatsSummary {
const records = readRecords({ ...query, limit: query.limit ?? 2000 });
return summarizeRecords(readRecords({ ...query, limit: query.limit ?? 2000 }));
}
export function summarizeRecords(records: TokenUsageRecord[]): TokenStatsSummary {
const summary: TokenStatsSummary = {
totalCalls: records.length,
promptTokens: 0,