mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
Free-tier requests now optionally leave through a Cloudflare WARP tunnel that runs inside the gateway process: WireGuard over UDP plus a gVisor netstack, so no TUN device, root, NET_ADMIN, or route changes are needed. Adapted from github.com/sudogeeker/ai-route (MIT). - warp.enabled/pool_size/stack in config, the WebUI, config.example.json and all READMEs; startup and hot reload fail closed when WARP cannot initialize. - Anonymous lane, free-model Zen-key fallback, free-model probes and the anonymous catalog refresh send no proxy traffic while enabled; Go keys and paid models keep their proxies. - Per-attempt leases feed 403/429 rotation and broken-tunnel failures back into the pool; /healthz and /api/monitor expose the pool state and readiness requires an active exit. - errWARPUnavailable maps to a 503 warp_unavailable response.
254 lines
8.9 KiB
JavaScript
254 lines
8.9 KiB
JavaScript
// Page: 概览 — readiness checklist, metrics, resources and token usage.
|
|
import * as store from "../store.js";
|
|
import { $, bindTabs, el, emptyRow, formatNumber, timeOnly } from "../ui.js";
|
|
import { t, onChange } from "../i18n.js";
|
|
|
|
function renderChecklist() {
|
|
const config = store.get("config");
|
|
const monitor = store.get("monitor");
|
|
if (!config || !monitor) return;
|
|
const resources = monitor.resources;
|
|
const models = resources.models || {};
|
|
const modelDescription = models.stale
|
|
? t(
|
|
`目录来自${models.cache_source || "磁盘"}快取,等待后台刷新。`,
|
|
"guide.models.cached",
|
|
models.cache_source || "磁盘",
|
|
)
|
|
: t("上游目录已完成首次刷新且有可暴露模型。", "guide.models.ready");
|
|
const checks = [
|
|
[
|
|
t("配置本地 Key", "guide.step.serverkey"),
|
|
t("客户端使用 Server Key 访问 Gateway。", "guide.step.serverkey.desc"),
|
|
(config.server_keys || []).length > 0,
|
|
],
|
|
[
|
|
t("选择上游通道", "guide.step.upstream"),
|
|
t("至少有 Zen / Go Key,或启用匿名通道。", "guide.step.upstream.desc"),
|
|
(config.zen_keys || []).length + (config.go_keys || []).length > 0 || config.anonymous,
|
|
],
|
|
[
|
|
t("检查 Proxy", "guide.step.proxy"),
|
|
t("至少一个代理节点健康,或 WARP 正在承载免费流量。", "guide.step.proxy.desc"),
|
|
(resources.warp?.active || 0) > 0 ||
|
|
(resources.proxies || []).some((proxy) => proxy.healthy),
|
|
],
|
|
[
|
|
t("等待模型目录", "guide.step.models"),
|
|
modelDescription,
|
|
models.exposed > 0 && !!models.updated_at,
|
|
],
|
|
];
|
|
const card = $("#overview-checklist");
|
|
const ready = checks.every(([, , done]) => done);
|
|
card.classList.toggle("hidden", ready);
|
|
if (ready) return;
|
|
const host = $("#guide-steps");
|
|
host.replaceChildren();
|
|
checks.forEach(([title, description, done]) => {
|
|
host.append(
|
|
el("article", { class: "guide-step" }, [
|
|
el("h3", { text: title }),
|
|
el("p", { text: description }),
|
|
el("div", {
|
|
class: `step-state ${done ? "ok" : "warn"}`,
|
|
text: done ? t("✓ 已就绪", "guide.done") : t("○ 待处理", "guide.todo"),
|
|
}),
|
|
]),
|
|
);
|
|
});
|
|
}
|
|
|
|
function renderResources(resources) {
|
|
const proxies = $("#proxy-table");
|
|
proxies.replaceChildren();
|
|
(resources.proxies || []).forEach((proxy) => {
|
|
proxies.append(
|
|
el("tr", {}, [
|
|
el("td", { text: proxy.address }),
|
|
el("td", {
|
|
class: `status ${proxy.healthy ? "ok" : "bad"}`,
|
|
text: proxy.healthy ? t("正常", "state.healthy") : t("不可用", "state.unavailable"),
|
|
}),
|
|
el("td", {
|
|
text: `Z ${proxy.zen_keys} · G ${proxy.go_keys} · A ${
|
|
proxy.anonymous ? t("开", "common.on") : t("关", "common.off")
|
|
}`,
|
|
}),
|
|
]),
|
|
);
|
|
});
|
|
const warp = resources.warp;
|
|
if (warp?.enabled) {
|
|
// WARP is not a proxy: show it beside them so the egress that carries
|
|
// free-model traffic is visible without pretending it is a bound node.
|
|
proxies.append(
|
|
el("tr", {}, [
|
|
el("td", { text: `WARP · ${warp.stack}` }),
|
|
el("td", {
|
|
class: `status ${warp.active > 0 ? "ok" : "bad"}`,
|
|
text: warp.active > 0 ? t("正常", "state.healthy") : t("不可用", "state.unavailable"),
|
|
}),
|
|
el("td", {
|
|
text: `${warp.active}/${warp.size} · ${(warp.egress || []).join(", ") || "-"}`,
|
|
}),
|
|
]),
|
|
);
|
|
}
|
|
if (!proxies.children.length) emptyRow(proxies, 3);
|
|
const keys = $("#key-table");
|
|
keys.replaceChildren();
|
|
if (resources.anonymous) {
|
|
keys.append(
|
|
el("tr", {}, [
|
|
el("td", { text: "anonymous" }),
|
|
el("td", { text: "zen" }),
|
|
el("td", { class: "status ok", text: t("已启用", "state.enabled") }),
|
|
]),
|
|
);
|
|
}
|
|
(resources.keys || []).forEach((key) => {
|
|
keys.append(
|
|
el("tr", {}, [
|
|
el("td", { text: key.id }),
|
|
el("td", { text: key.tier }),
|
|
el("td", {
|
|
class: `status ${key.cooldown_until ? "warn" : "ok"}`,
|
|
text: key.cooldown_until ? t("冷却中", "state.cooling") : t("可用", "state.available"),
|
|
}),
|
|
]),
|
|
);
|
|
});
|
|
if (!keys.children.length) emptyRow(keys, 3);
|
|
}
|
|
|
|
function renderBreakdowns(metrics) {
|
|
const host = $("#breakdowns");
|
|
host.replaceChildren();
|
|
[
|
|
[t("端点", "breakdown.endpoint"), metrics.endpoints],
|
|
[t("状态码", "breakdown.status"), metrics.statuses],
|
|
[t("模型", "breakdown.model"), metrics.models],
|
|
["Tier", metrics.tiers],
|
|
].forEach(([title, data]) => {
|
|
const body = el("tbody");
|
|
Object.entries(data || {})
|
|
.sort((a, b) => b[1] - a[1])
|
|
.slice(0, 10)
|
|
.forEach(([key, value]) => {
|
|
body.append(el("tr", {}, [el("td", { text: key }), el("td", { class: "num", text: value })]));
|
|
});
|
|
if (!body.children.length) emptyRow(body, 2, t("暂无请求", "breakdown.empty"));
|
|
host.append(
|
|
el("div", { class: "breakdown" }, [
|
|
el("h3", { text: title }),
|
|
el("div", { class: "table-wrap" }, [el("table", { class: "table" }, [body])]),
|
|
]),
|
|
);
|
|
});
|
|
}
|
|
|
|
function renderSummary(monitor) {
|
|
const metrics = monitor.metrics;
|
|
$("#m-total").textContent = formatNumber(metrics.last_hour.total);
|
|
$("#m-rate").textContent = `${((metrics.last_hour.success_rate || 0) * 100).toFixed(1)}%`;
|
|
$("#m-p95").textContent = `${metrics.last_hour.p95_ms || 0} ms`;
|
|
$("#m-active").textContent = `${metrics.active_requests || 0} / ${metrics.active_streams || 0}`;
|
|
renderResources(monitor.resources);
|
|
renderBreakdowns(metrics);
|
|
}
|
|
|
|
function renderTrend(metrics) {
|
|
const trend = $("#usage-trend");
|
|
const series = metrics?.series || [];
|
|
const maximum = Math.max(1, ...series.map((point) => point.total_tokens || 0));
|
|
trend.replaceChildren();
|
|
series.forEach((point) => {
|
|
const bar = el("div", { class: "trend-bar" });
|
|
bar.style.height = `${Math.max(2, Math.round(((point.total_tokens || 0) / maximum) * 100))}%`;
|
|
bar.title = `${timeOnly(point.minute)} · input ${formatNumber(point.input_tokens)} · output ${formatNumber(point.output_tokens)} · total ${formatNumber(point.total_tokens)}`;
|
|
trend.append(bar);
|
|
});
|
|
$("#usage-trend-meta").textContent = series.length
|
|
? t(
|
|
`峰值 ${formatNumber(maximum)} tokens / 分钟 · 近 ${series.length} 分钟`,
|
|
"usage.trend.peak",
|
|
formatNumber(maximum),
|
|
series.length,
|
|
)
|
|
: t("尚无趋势数据", "usage.trend.empty");
|
|
}
|
|
|
|
function renderUsageModels(usage) {
|
|
const host = $("#usage-models");
|
|
host.replaceChildren();
|
|
Object.entries(usage.models || {})
|
|
.sort((a, b) => (b[1].total_tokens || 0) - (a[1].total_tokens || 0))
|
|
.slice(0, 15)
|
|
.forEach(([model, value]) => {
|
|
host.append(
|
|
el("tr", {}, [
|
|
el("td", { class: "mono", text: model }),
|
|
el("td", { class: "num", text: formatNumber(value.input_tokens) }),
|
|
el("td", { class: "num", text: formatNumber(value.output_tokens) }),
|
|
el("td", { class: "num", text: formatNumber(value.total_tokens) }),
|
|
]),
|
|
);
|
|
});
|
|
if (!host.children.length) emptyRow(host, 4, t("尚无 Token 用量", "usage.empty.models"));
|
|
}
|
|
|
|
function renderTiers(usage) {
|
|
const host = $("#usage-tiers");
|
|
const entries = Object.entries(usage.tiers || {}).sort(
|
|
(a, b) => (b[1].total_tokens || 0) - (a[1].total_tokens || 0),
|
|
);
|
|
const total = Math.max(1, ...entries.map(([, value]) => value.total_tokens || 0));
|
|
host.replaceChildren();
|
|
entries.forEach(([tier, value]) => {
|
|
const fill = el("div", { class: "tier-fill" });
|
|
fill.style.width = `${Math.round(((value.total_tokens || 0) / total) * 100)}%`;
|
|
host.append(
|
|
el("div", { class: "tier-row" }, [
|
|
el("strong", { text: tier }),
|
|
el("div", { class: "tier-track" }, [fill]),
|
|
el("span", { class: "tier-value", text: formatNumber(value.total_tokens) }),
|
|
]),
|
|
);
|
|
});
|
|
if (!entries.length) host.append(el("div", { class: "empty", text: t("尚无 Tier 用量", "usage.empty.tiers") }));
|
|
}
|
|
|
|
function renderUsage(monitor) {
|
|
const usage = monitor.usage?.last_hour || {};
|
|
const tokens = usage.tokens || {};
|
|
$("#u-coverage").textContent = `${((usage.coverage || 0) * 100).toFixed(1)}%`;
|
|
$("#u-total").textContent = formatNumber(tokens.total_tokens);
|
|
$("#u-cached").textContent = formatNumber(tokens.cached_tokens);
|
|
$("#u-reasoning").textContent = formatNumber(tokens.reasoning_tokens);
|
|
renderTrend(monitor.metrics);
|
|
renderUsageModels(usage);
|
|
renderTiers(usage);
|
|
}
|
|
|
|
function render() {
|
|
renderChecklist();
|
|
const monitor = store.get("monitor");
|
|
if (!monitor) return;
|
|
renderSummary(monitor);
|
|
renderUsage(monitor);
|
|
}
|
|
|
|
export default {
|
|
id: "overview",
|
|
title: "概览",
|
|
sub: "运行指标、Token 用量与资源状态。",
|
|
init() {
|
|
bindTabs($("#page-overview"));
|
|
store.on("monitor", render);
|
|
store.on("config", renderChecklist);
|
|
onChange(render);
|
|
},
|
|
show: render,
|
|
};
|