Files
ipass-plugin-cache/www/assets/me.js
T
ssw 43718bd0c4 iPass cache gateway 源码
容量管理: zones.rs 直读 nginx file_cache 共享内存记账(双轴: 磁盘 + keys_zone 索引内存), ?scan=1 扫盘上限 20 万文件; POST /api/admin/cache/purge 整区清理(只删文件不删目录, 根治 [crit] mkdir failed); POST /api/admin/cache/invalidate 按制品精准失效(预览->确认两步式, glob/子串, 删除上限 5000/扫描上限 20 万); GET /api/admin/cache 新增 capacity 块; 前端容量面板 + 按制品失效工具条; 测试 test-cache-capacity.sh 48/48(容量 27 + 精准失效 21, 含 EXPECT_MIN 护栏与前端静态契约); docs 容量管理章节
2026-09-21 09:25:11 +08:00

270 lines
9.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* 账号态「我的用量」面板(1.0.30)
输入 API Key → 调 /api/v1/me → 渲染概览 / 24h 小时分布 / 按 slot 拆分 /
我能调的上游 + apix + 我的域名。
Key 只在 sessionStorage,关页面即清。
*/
(function () {
"use strict";
var KEY_STORE = "ipass.me.key";
var $ = IP.$;
function esc(s) { return IP.esc(s); }
function setKey(k) {
if (k) sessionStorage.setItem(KEY_STORE, k);
else sessionStorage.removeItem(KEY_STORE);
}
function getKey() {
return sessionStorage.getItem(KEY_STORE) || "";
}
/** 走 X-API-Key 头,调 API。不能用 IP.api(那个走 cookie)。
* 401 直接 throw 让 UI 显出重新输入 Key。 */
function meApi(method, path, body) {
var key = getKey();
if (!key) return Promise.reject(new Error("未设置 API Key"));
var opt = { method: method, headers: { "X-API-Key": key } };
if (body) {
opt.headers["Content-Type"] = "application/json";
opt.body = JSON.stringify(body);
}
return fetch(path, opt).then(function (res) {
return res.json().catch(function () { return { code: res.status, msg: "响应解析失败" }; })
.then(function (data) {
if (!res.ok) throw new Error(data.msg || ("请求失败 HTTP " + res.status));
return data;
});
});
}
function showKeyCard() {
$("keyCard").hidden = false;
$("dashCard").hidden = true;
$("apiKey").focus();
}
function showDash() {
$("keyCard").hidden = true;
$("dashCard").hidden = false;
}
function renderTopbar(me) {
$("topbar").innerHTML =
'<div class="topbar">' +
'<div class="brand"><b>尚数网 山海关</b><span>个人用量面板</span></div>' +
'<div class="user-box">' +
'<span id="whoami">' + esc(me.username) + " · " +
(me.role === "admin" ? "管理员" : "账号") + "</span>" +
'<button class="btn-sm" id="topbarAdminBtn" ' + (me.role === "admin" ? "" : "hidden") + '>管理控制台</button>' +
"</div>" +
"</div>";
if (me.role === "admin") {
$("topbarAdminBtn").addEventListener("click", function () {
// 跳 admin login(cookie 鉴权不走 me Key)
location.href = "login.html";
});
}
}
function renderHourly(hour24, labels) {
var el = $("dHourChart");
// server 返回 hour_24h_labels("23h前"…"现在"),老 server 没这个字段时降级到索引
var lbls = (labels && labels.length === hour24.length) ? labels
: hour24.map(function (_, h) { return h; });
IP.chart.line(el, {
labels: lbls,
series: [{ name: "调用数", values: hour24.map(function (v) { return Number(v) || 0; }) }],
height: 90,
emptyText: "过去 24 小时没有调用记录"
});
}
function renderSlots(bySlot) {
var body = $("dSlotBody");
if (!bySlot || !bySlot.length) {
body.innerHTML = '<tr><td colspan="6" class="empty">过去 7 天没有调用记录</td></tr>';
return;
}
body.innerHTML = bySlot.map(function (s) {
var lastStatus = s.last_status || 0;
var badgeClass = lastStatus >= 200 && lastStatus < 300 ? "b-ok"
: lastStatus >= 400 && lastStatus < 500 ? "b-warn"
: lastStatus >= 500 ? "b-bad" : "b-gray";
return '<tr>' +
'<td class="mono">' + esc(s.slot) + '</td>' +
'<td>' + (s.calls_24h || 0) + '</td>' +
'<td>' + (s.calls_7d || 0) + '</td>' +
'<td>' + IP.chart.fmtNum(s.tin_7d || 0) + '</td>' +
'<td>' + IP.chart.fmtNum(s.tout_7d || 0) + '</td>' +
'<td><span class="badge ' + badgeClass + '">' + lastStatus + '</span></td>' +
'</tr>';
}).join("");
}
/**
* 同一个 upstream 通常**两个 base_url 都存在**:
* - `base_url`:OpenAI 协议出口(如 `https://lm.mayishangshu.cn:88/deepseek`)
* - `anthropic_base_url`:Anthropic 协议出口(如 `https://lm.mayishangshu.cn:88/deepseek/anthropic`)
*
* 网关出口是**纯 slot**(不带厂商路径、不带 /v1)——厂商版本/协议路径段
* 原样存在上游配置里,转发时拼到 SDK 余下路径前。客户端 SDK 自己拼
* `/chat/completions` 或 `/v1/messages`,网关不猜路径。
*
* **1.0.34b**:只展示**纯 URL**(不加任何尾巴),点击按钮直接复制。
* 不展示 curl —— WorkBuddy / Claude Code 客户端拿到 URL 后**自行拼路径**,
* 中间再加 `/chat/completions` 这种尾巴会 404。
*/
function renderUpstreams(upstreams) {
var el = $("dUpstream");
if (!upstreams || !upstreams.length) {
el.innerHTML = '当前账号无可用上游(可能白名单为空,或全部 slot 被停用)';
return;
}
function renderBtn(proto, url) {
return '<button class="btn-sm copy-url-btn" data-url="' + esc(url) + '" ' +
'title="点击复制(不带尾巴,原样可粘到 WorkBuddy / Claude Code)">' +
(proto === "openai" ? "OpenAI" : "Anthropic") + '</button>';
}
function renderUrlRow(proto, url) {
return '<div class="upstream-url-row">' +
renderBtn(proto, url) +
'<code class="upstream-url mono">' + esc(url) + '</code>' +
'</div>';
}
el.innerHTML = upstreams.map(function (u) {
var protocols = [];
if (u.base_url) protocols.push(["openai", u.base_url]);
if (u.anthropic_base_url) protocols.push(["anthropic", u.anthropic_base_url]);
return '<div class="upstream-card">' +
'<div class="upstream-head">' +
'<span class="mono"><b>' + esc(u.slot || "") + '</b></span>' +
'<span class="muted" style="margin-left:8px;font-size:12px">' +
esc((u.models || []).join("、") || "—") + '</span>' +
(u.note ? '<span class="muted" style="margin-left:8px;font-size:12px">— ' + esc(u.note) + '</span>' : '') +
'</div>' +
protocols.map(function (p) { return renderUrlRow(p[0], p[1]); }).join("") +
'</div>';
}).join("");
// 复制按钮:原样复制 URL,不加尾巴
Array.prototype.forEach.call(el.querySelectorAll(".copy-url-btn"), function (btn) {
btn.addEventListener("click", function (e) {
e.preventDefault();
IP.copyText(btn.getAttribute("data-url"), "URL");
});
});
}
function renderApix(apix) {
var el = $("dApix");
if (!apix || !apix.length) {
el.innerHTML = '当前账号无可用 API 代理';
return;
}
el.innerHTML = '<table><thead><tr>' +
'<th>slug</th><th>用途</th><th>调用方式</th><th>说明</th>' +
'</tr></thead><tbody>' +
apix.map(function (a) {
var mode = a.mode || "vendor";
var call = mode === "passthrough"
? '<code>POST /apixp/' + esc(a.slug) + '/&lt;tail&gt;</code>'
: '<code>GET /apix/' + esc(a.slug) + '?参数=值</code>';
return '<tr>' +
'<td class="mono">' + esc(a.slug || "") + '</td>' +
'<td>' + esc(a.display_name || a.url || "—") + '</td>' +
'<td style="font-size:12.5px">' + call + '</td>' +
'<td class="muted" style="font-size:12px">' + esc((a.desc || a.usage_hint || "").slice(0, 80)) + '</td>' +
'</tr>';
}).join("") + '</tbody></table>';
}
function renderDomains(domains) {
var el = $("dDomains");
if (!domains || !domains.length) {
el.innerHTML = '当前账号没有域名(需要去"全部域名"页申请)';
return;
}
el.innerHTML = '<table><thead><tr>' +
'<th>子域名</th><th>后端</th><th>状态</th><th>创建时间</th>' +
'</tr></thead><tbody>' +
domains.map(function (d) {
return '<tr>' +
'<td class="mono">' + esc(d.full || d.subdomain || "—") + '</td>' +
'<td class="mono" style="font-size:12px">' + esc(d.backend || "—") + '</td>' +
'<td><span class="badge ' + (d.enabled ? "b-ok" : "b-gray") + '">' +
(d.enabled ? "启用" : "停用") + '</span></td>' +
'<td class="muted">' + esc(d.created_at_text || "") + '</td>' +
'</tr>';
}).join("") + '</tbody></table>';
}
function renderDash(me) {
renderTopbar(me);
$("dUser").textContent = me.username || "-";
var r = me.recent || {};
$("dCalls24").textContent = r.calls_24h || 0;
$("dCalls7").textContent = r.calls_7d || 0;
$("dTin24").textContent = IP.chart.fmtNum(r.tin_24h || 0);
$("dTout24").textContent = IP.chart.fmtNum(r.tout_24h || 0);
$("dLast").textContent = r.last_at
? esc(r.last_at + " · " + (r.last_slot || "-"))
: "—";
renderHourly(r.hour_24h || [], r.hour_24h_labels || []);
renderSlots(r.by_slot || []);
renderUpstreams(me.upstreams || []);
renderApix(me.apix || []);
renderDomains(me.domains || []);
}
function loadDash() {
IP.toast("加载中…");
meApi("GET", "/api/v1/me").then(function (resp) {
var me = resp.data || {};
if (!me.username) {
IP.toast("接口返回异常:缺 username", true);
showKeyCard();
return;
}
renderDash(me);
showDash();
IP.toast("已加载");
}).catch(function (err) {
if (err.message && err.message.indexOf("HTTP 401") >= 0) {
setKey("");
IP.toast("Key 无效或已过期,请重新输入", true);
showKeyCard();
$("apiKey").value = "";
$("apiKey").focus();
} else {
IP.toast("加载失败:" + (err.message || err), true);
}
});
}
function bind() {
$("keyForm").addEventListener("submit", function (e) {
e.preventDefault();
var k = $("apiKey").value.trim();
if (!k) { IP.toast("请输入 API Key", true); return; }
setKey(k);
loadDash();
});
$("refreshBtn").addEventListener("click", loadDash);
}
function boot() {
bind();
if (getKey()) loadDash();
else showKeyCard();
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", boot);
} else {
boot();
}
})();