diff options
| author | Adam Malczewski <[email protected]> | 2026-10-19 17:02:40 +0900 |
|---|---|---|
| committer | Adam Malczewski <[email protected]> | 2026-10-19 17:02:40 +0900 |
| commit | 01f9fb2a4ad6110def531ead16b370c818e8ee81 (patch) | |
| tree | 398996139295c24350714bb02a88b0dc07545123 /src/features/chat/ui/CacheTimerBadge.svelte | |
| parent | c4397f633415a46852f0e3b49c78ab7b2d87d4c1 (diff) | |
| download | dispatch-web-01f9fb2a4ad6110def531ead16b370c818e8ee81.tar.gz dispatch-web-01f9fb2a4ad6110def531ead16b370c818e8ee81.zip | |
feat(chat): cache timer for Claude Code subscription models
Per-step and per-turn timers for the gaps between model requests (the
Claude prompt cache expires after 5 min idle). A step gap runs live under
the executing tools from step-complete until its last tool result (or the
next step's output), then is stamped on that step's metrics line beside
its cache badge. A turn gap runs live on the turn's metrics line from
done/error until the next message is sent, then is stamped there. Counts
m:ss up to 10 min, then '10+ min'; colour tracks the 5-min TTL.
Gated on claude/<model>. Pure reducer in core/metrics/cache-timer.ts;
the turn gap is seeded from an idle conversation's lastActivityAt so it
survives a reload. Stamps are in-memory only.
Diffstat (limited to 'src/features/chat/ui/CacheTimerBadge.svelte')
| -rw-r--r-- | src/features/chat/ui/CacheTimerBadge.svelte | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/features/chat/ui/CacheTimerBadge.svelte b/src/features/chat/ui/CacheTimerBadge.svelte new file mode 100644 index 0000000..e5abed5 --- /dev/null +++ b/src/features/chat/ui/CacheTimerBadge.svelte @@ -0,0 +1,58 @@ +<script lang="ts"> + /** + * One cache-timer badge. LIVE when given `since` (counts up from that epoch + * ms, ticking once a second, and stops ticking at the 10-minute cap); STAMPED + * when given `elapsedMs` (a finished gap — static). Formatting lives in + * `core/metrics` (`viewCacheTimer`). Owning the tick here means only this + * badge re-renders each second, not the transcript. + */ + import { untrack } from "svelte"; + import { CACHE_TIMER_MAX_MS, viewCacheTimer } from "../../../core/metrics"; + + let { + since, + elapsedMs, + now = Date.now, + }: { + /** Live: epoch ms the gap started. */ + since?: number; + /** Stamped: the gap's final duration (wins over `since`). */ + elapsedMs?: number; + /** Wall clock (injected for tests). */ + now?: () => number; + } = $props(); + + // Initial read only (the effect below keeps it current while live). + let current = $state(untrack(() => now())); + + $effect(() => { + if (elapsedMs !== undefined || since === undefined) return; + const start = since; + current = now(); + if (current - start >= CACHE_TIMER_MAX_MS) return; + const id = setInterval(() => { + current = now(); + if (current - start >= CACHE_TIMER_MAX_MS) clearInterval(id); + }, 1000); + return () => clearInterval(id); + }); + + const badgeClass = { + success: "badge-success", + warning: "badge-warning", + error: "badge-error", + } as const; + + const live = $derived(elapsedMs === undefined); + const view = $derived(viewCacheTimer(elapsedMs ?? current - (since ?? current))); +</script> + +<span + class="badge badge-sm gap-1 font-mono tabular-nums {badgeClass[view.level]}" + class:badge-outline={!live} + title={live + ? "Cache timer (running): time since the last model request. The Claude prompt cache expires after 5 minutes idle." + : "Cache timer (stopped): how long the cache sat idle before the next request. The Claude prompt cache expires after 5 minutes idle."} + data-testid={live ? "cache-timer-live" : "cache-timer-stamp"} + ><span aria-hidden="true">⏱</span>{view.label}</span +> |
