diff options
| author | tanaka takashi <isazaka@gmail.com> | 2026-07-09 20:07:18 +0900 |
|---|---|---|
| committer | tanaka takashi <isazaka@gmail.com> | 2026-07-09 20:07:18 +0900 |
| commit | 2bbb9752568bb1701051878ffb844bc78c81f1f4 (patch) | |
| tree | 79dfe3769c8105afe7312093a1d35808e6badac1 /src/ollama_web/static | |
| parent | 77eef27cad9b0e5b0551d5a8ea9485779ee7dc18 (diff) | |
トークン消費量のリングゲージの表示タイミング調整
Diffstat (limited to 'src/ollama_web/static')
| -rw-r--r-- | src/ollama_web/static/app.js | 42 |
1 files changed, 37 insertions, 5 deletions
diff --git a/src/ollama_web/static/app.js b/src/ollama_web/static/app.js index a91d903..378e39f 100644 --- a/src/ollama_web/static/app.js +++ b/src/ollama_web/static/app.js @@ -354,7 +354,6 @@ async function selectSession(id) { stickToBottom = true; renderSession(); renderSessionList((await (await fetch("/api/sessions")).json()).sessions || []); - updateTokenGaugeFromSession(); } catch (err) { console.error("failed to select session", err); } @@ -398,7 +397,10 @@ function _renderAssistantContent(body, content) { function renderSession() { chatEl.innerHTML = ""; - if (!currentSession) return; + if (!currentSession) { + updateTokenGauge(0, 0); + return; + } const msgs = currentSession.messages || []; for (const m of msgs) { if (m.role === "user") { @@ -438,6 +440,7 @@ function renderSession() { } } } + updateTokenGaugeFromSession(); } function renderAttachments() { @@ -906,13 +909,42 @@ function updateTokenGauge(promptEvalCount, numCtx) { } function updateTokenGaugeFromSession() { - if (!currentSession) return; + if (!currentSession) { + updateTokenGauge(0, 0); + return; + } const tc = currentSession.token_count; - if (!tc || !tc.num_ctx) { + const messages = currentSession.messages || []; + const assistantMessages = messages.filter((m) => m.role === "assistant"); + + if (tc && tc.num_ctx) { + updateTokenGauge(tc.prompt_eval_count || 0, tc.num_ctx || 0); + return; + } + + // Fall back to the most recent assistant message token_count when the + // session aggregate is missing (e.g. older sessions). + let fallback = null; + for (let i = assistantMessages.length - 1; i >= 0; i--) { + const mtc = assistantMessages[i].token_count; + if (mtc && mtc.num_ctx) { + fallback = mtc; + break; + } + } + if (fallback) { + updateTokenGauge(fallback.prompt_eval_count || 0, fallback.num_ctx || 0); + return; + } + + // Hide gauge only when the session has no chat history at all. + if (messages.length === 0) { updateTokenGauge(0, 0); return; } - updateTokenGauge(tc.prompt_eval_count || 0, tc.num_ctx || 0); + + // Keep the current gauge value for sessions that have messages but no + // token data yet, so the gauge does not flicker during session switching. } loadSessions(); |
