Skip to content

Commit 4d16ffa

Browse files
committed
fix(tui): guard assistant tokens + drop unused isActive (anomalyco#5374)
1 parent f3d15d3 commit 4d16ffa

3 files changed

Lines changed: 2 additions & 17 deletions

File tree

‎packages/tui/src/feature-plugins/home/footer.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function TokenRate(props: { api: TuiPluginApi }) {
4141
props.api.event.on("message.updated", (evt) => {
4242
const info = evt.properties.info
4343
if (info.role !== "assistant") return
44-
const tokens = info.tokens.output + info.tokens.reasoning
44+
const tokens = (info.tokens?.output ?? 0) + (info.tokens?.reasoning ?? 0)
4545
lastActivityAt = Date.now()
4646
if (info.id !== currentId) {
4747
currentId = info.id
@@ -67,6 +67,7 @@ function TokenRate(props: { api: TuiPluginApi }) {
6767
tick()
6868
const now = Date.now()
6969
if (now - lastActivityAt > ACTIVITY_TIMEOUT) return
70+
if (currentTokens <= 0) return
7071
return {
7172
rate: meter.rate(now),
7273
total: currentTokens,

‎packages/tui/src/util/token-rate.ts‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ export class TokenRateMeter {
3434
return (last.tokens - first.tokens) / elapsed
3535
}
3636

37-
// True when a sample landed within `timeoutMs` of `at`.
38-
isActive(at = Date.now(), timeoutMs = 2000): boolean {
39-
const last = this.samples[this.samples.length - 1]
40-
return !!last && at - last.at <= timeoutMs
41-
}
42-
4337
private prune(at: number) {
4438
const windowStart = at - this.windowMs
4539
let drop = 0

‎packages/tui/test/util/token-rate.test.ts‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,4 @@ describe("util.token-rate", () => {
5454
meter.add(10, 2000)
5555
expect(meter.rate(2000)).toBe(0)
5656
})
57-
58-
test("isActive reflects recent sampling within the timeout", () => {
59-
const meter = new TokenRateMeter()
60-
expect(meter.isActive(1000, 2000)).toBe(false)
61-
meter.add(10, 1000)
62-
expect(meter.isActive(1000, 2000)).toBe(true)
63-
expect(meter.isActive(2999, 2000)).toBe(true)
64-
expect(meter.isActive(3000, 2000)).toBe(true)
65-
expect(meter.isActive(3001, 2000)).toBe(false)
66-
})
6757
})

0 commit comments

Comments
 (0)