token用量输入/输出分开统计展示;加载更多按钮改为占满宽度与全局按钮风格一致
This commit is contained in:
+13
-11
@@ -92,25 +92,27 @@ function recordModelUsage(sessionId, modelId, usage) {
|
|||||||
).run(sessionId, modelId, pt, ct, tt, now());
|
).run(sessionId, modelId, pt, ct, tt, now());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 每模型 token 用量统计:今日(本地时区自然日起)+ 累计。
|
// 每模型 token 用量统计:今日(本地时区自然日起)+ 累计,输入/输出分开。
|
||||||
// 返回 { [model_id]: { today_tokens, total_tokens } },配置内的模型保证有零值占位。
|
// 返回 { [model_id]: { today_prompt, today_completion, total_prompt, total_completion } },
|
||||||
|
// 配置内的模型保证有零值占位。
|
||||||
function modelUsageStats() {
|
function modelUsageStats() {
|
||||||
const d = new Date();
|
const d = new Date();
|
||||||
const dayStart = new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
|
const dayStart = new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
|
||||||
const stats = {};
|
const stats = {};
|
||||||
const slot = function (id) {
|
const slot = function (id) {
|
||||||
if (!stats[id]) stats[id] = { today_tokens: 0, total_tokens: 0 };
|
if (!stats[id]) stats[id] = { today_prompt: 0, today_completion: 0, total_prompt: 0, total_completion: 0 };
|
||||||
return stats[id];
|
return stats[id];
|
||||||
};
|
};
|
||||||
for (const m of cfg.models) slot(m.id);
|
for (const m of cfg.models) slot(m.id);
|
||||||
const today = db.prepare(
|
const sumSql = 'SELECT model_id, SUM(prompt_tokens) AS p, SUM(completion_tokens) AS c FROM model_usage';
|
||||||
'SELECT model_id, SUM(total_tokens) AS n FROM model_usage WHERE created_at >= ? GROUP BY model_id'
|
for (const r of db.prepare(sumSql + ' WHERE created_at >= ? GROUP BY model_id').all(dayStart)) {
|
||||||
).all(dayStart);
|
slot(r.model_id).today_prompt = r.p || 0;
|
||||||
for (const r of today) slot(r.model_id).today_tokens = r.n || 0;
|
slot(r.model_id).today_completion = r.c || 0;
|
||||||
const total = db.prepare(
|
}
|
||||||
'SELECT model_id, SUM(total_tokens) AS n FROM model_usage GROUP BY model_id'
|
for (const r of db.prepare(sumSql + ' GROUP BY model_id').all()) {
|
||||||
).all();
|
slot(r.model_id).total_prompt = r.p || 0;
|
||||||
for (const r of total) slot(r.model_id).total_tokens = r.n || 0;
|
slot(r.model_id).total_completion = r.c || 0;
|
||||||
|
}
|
||||||
return stats;
|
return stats;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -78,12 +78,11 @@ body {
|
|||||||
.search-cancel { background: none; border: none; color: #2a5299; font-size: 14px; padding: 6px 4px; cursor: pointer; flex: none; }
|
.search-cancel { background: none; border: none; color: #2a5299; font-size: 14px; padding: 6px 4px; cursor: pointer; flex: none; }
|
||||||
.search-tip { color: #8891a5; font-size: 12px; padding: 2px 4px 8px; }
|
.search-tip { color: #8891a5; font-size: 12px; padding: 2px 4px 8px; }
|
||||||
.load-more {
|
.load-more {
|
||||||
display: block; margin: 4px auto 14px;
|
width: 100%; background: #f2f3f7; color: #445;
|
||||||
padding: 8px 26px; background: #fff; color: #2a5299;
|
border: 1px solid #dde; border-radius: 10px;
|
||||||
border: 1px solid #d5deee; border-radius: 18px;
|
padding: 10px; font-size: 14px; cursor: pointer; margin-bottom: 10px;
|
||||||
font-size: 13px; cursor: pointer; box-shadow: 0 1px 3px rgba(30,40,90,0.06);
|
|
||||||
}
|
}
|
||||||
.load-more:active { background: #eef3fc; }
|
.load-more:active { background: #e8eaf0; }
|
||||||
.load-more:disabled { opacity: 0.5; cursor: default; }
|
.load-more:disabled { opacity: 0.5; cursor: default; }
|
||||||
|
|
||||||
/* ---- 任务状态栏 ---- */
|
/* ---- 任务状态栏 ---- */
|
||||||
|
|||||||
@@ -284,10 +284,10 @@
|
|||||||
<div class="toast hidden" id="toast"></div>
|
<div class="toast hidden" id="toast"></div>
|
||||||
|
|
||||||
<!-- 发版时更新 v 参数,避免移动端浏览器缓存旧 JS -->
|
<!-- 发版时更新 v 参数,避免移动端浏览器缓存旧 JS -->
|
||||||
<script src="/js/core.js?v=20260901n"></script>
|
<script src="/js/core.js?v=20260901o"></script>
|
||||||
<script src="/js/tasks.js?v=20260901n"></script>
|
<script src="/js/tasks.js?v=20260901o"></script>
|
||||||
<script src="/js/skills.js?v=20260901n"></script>
|
<script src="/js/skills.js?v=20260901o"></script>
|
||||||
<script src="/js/models.js?v=20260901n"></script>
|
<script src="/js/models.js?v=20260901o"></script>
|
||||||
<script src="/js/settings.js?v=20260901n"></script>
|
<script src="/js/settings.js?v=20260901o"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -40,8 +40,10 @@ async function loadModelCards() {
|
|||||||
'<div class="model-card-meta">' + esc(m.id) +
|
'<div class="model-card-meta">' + esc(m.id) +
|
||||||
(run && run.is_busy && run.current_session_id ? ' · 会话 ' + esc(run.current_session_id) : '') + '</div>' +
|
(run && run.is_busy && run.current_session_id ? ' · 会话 ' + esc(run.current_session_id) : '') + '</div>' +
|
||||||
'<div class="cfg-kv"><label>余额</label><b>--</b></div>' +
|
'<div class="cfg-kv"><label>余额</label><b>--</b></div>' +
|
||||||
'<div class="cfg-kv"><label>今日 token</label><b>' + (run ? fmtTok(run.today_tokens) : '--') + '</b></div>' +
|
'<div class="cfg-kv"><label>今日输入 token</label><b>' + (run ? fmtTok(run.today_prompt) : '--') + '</b></div>' +
|
||||||
'<div class="cfg-kv"><label>累计 token</label><b>' + (run ? fmtTok(run.total_tokens) : '--') + '</b></div>' +
|
'<div class="cfg-kv"><label>今日输出 token</label><b>' + (run ? fmtTok(run.today_completion) : '--') + '</b></div>' +
|
||||||
|
'<div class="cfg-kv"><label>累计输入 token</label><b>' + (run ? fmtTok(run.total_prompt) : '--') + '</b></div>' +
|
||||||
|
'<div class="cfg-kv"><label>累计输出 token</label><b>' + (run ? fmtTok(run.total_completion) : '--') + '</b></div>' +
|
||||||
'</div>';
|
'</div>';
|
||||||
}).join('') +
|
}).join('') +
|
||||||
(modelsPendingRestart
|
(modelsPendingRestart
|
||||||
|
|||||||
@@ -38,14 +38,16 @@ app.use('/api/skills', require('./routes/skills'));
|
|||||||
app.use('/api/config', require('./routes/config'));
|
app.use('/api/config', require('./routes/config'));
|
||||||
app.use('/api/system', require('./routes/system'));
|
app.use('/api/system', require('./routes/system'));
|
||||||
|
|
||||||
// 模型池状态(前端可展示空闲/占用)+ 每模型 token 用量(今日/累计)
|
// 模型池状态(前端可展示空闲/占用)+ 每模型 token 用量(今日/累计,输入/输出分开)
|
||||||
app.get('/api/models', (req, res) => {
|
app.get('/api/models', (req, res) => {
|
||||||
const rows = db.prepare('SELECT id, display_name, is_busy, current_session_id FROM model_pool ORDER BY id').all();
|
const rows = db.prepare('SELECT id, display_name, is_busy, current_session_id FROM model_pool ORDER BY id').all();
|
||||||
const stats = engine.modelUsageStats();
|
const stats = engine.modelUsageStats();
|
||||||
for (const r of rows) {
|
for (const r of rows) {
|
||||||
const s = stats[r.id] || { today_tokens: 0, total_tokens: 0 };
|
const s = stats[r.id] || {};
|
||||||
r.today_tokens = s.today_tokens;
|
r.today_prompt = s.today_prompt || 0;
|
||||||
r.total_tokens = s.total_tokens;
|
r.today_completion = s.today_completion || 0;
|
||||||
|
r.total_prompt = s.total_prompt || 0;
|
||||||
|
r.total_completion = s.total_completion || 0;
|
||||||
}
|
}
|
||||||
res.json(rows);
|
res.json(rows);
|
||||||
});
|
});
|
||||||
|
|||||||
+6
-4
@@ -245,11 +245,13 @@ async function main() {
|
|||||||
ok(qc.some(x => x.id === fs9.id), 'q= 按消息内容命中');
|
ok(qc.some(x => x.id === fs9.id), 'q= 按消息内容命中');
|
||||||
const qnone = await api('GET', '/api/sessions?q=绝无此内容xyzzy-9x7');
|
const qnone = await api('GET', '/api/sessions?q=绝无此内容xyzzy-9x7');
|
||||||
ok(Array.isArray(qnone) && qnone.length === 0, 'q= 无命中返回空');
|
ok(Array.isArray(qnone) && qnone.length === 0, 'q= 无命中返回空');
|
||||||
// token 用量:model_usage 由 mock 上游的 usage 帧驱动,今日/累计应已统计
|
// token 用量:model_usage 由 mock 上游的 usage 帧驱动,输入/输出分开统计
|
||||||
const usagePool = await api('GET', '/api/models');
|
const usagePool = await api('GET', '/api/models');
|
||||||
const sumToday = usagePool.reduce(function (n, m) { return n + (m.today_tokens || 0); }, 0);
|
const sumP = usagePool.reduce(function (n, m) { return n + (m.today_prompt || 0); }, 0);
|
||||||
const sumAll = usagePool.reduce(function (n, m) { return n + (m.total_tokens || 0); }, 0);
|
const sumC = usagePool.reduce(function (n, m) { return n + (m.today_completion || 0); }, 0);
|
||||||
ok(sumToday > 0 && sumAll >= sumToday, '/api/models 携带今日/累计 token 用量', '今日=' + sumToday + ' 累计=' + sumAll);
|
const sumTP = usagePool.reduce(function (n, m) { return n + (m.total_prompt || 0); }, 0);
|
||||||
|
ok(sumP > 0 && sumC > 0 && sumTP >= sumP,
|
||||||
|
'/api/models 携带今日/累计输入输出 token 用量', '今日输入=' + sumP + ' 今日输出=' + sumC);
|
||||||
|
|
||||||
// ---- 10. 技能管理(阶段 3)----
|
// ---- 10. 技能管理(阶段 3)----
|
||||||
console.log('\n[10] 技能管理');
|
console.log('\n[10] 技能管理');
|
||||||
|
|||||||
Reference in New Issue
Block a user