diff --git a/plagin_tokens/src/tracker.ts b/plagin_tokens/src/tracker.ts index b691b07..a4529e7 100644 --- a/plagin_tokens/src/tracker.ts +++ b/plagin_tokens/src/tracker.ts @@ -30,6 +30,7 @@ export class Tracker { private estTokens = 0 private realActive = false private completedAt: number | null = null + private historyRecorded = false private finalRate = 0 private state: SpeedState = "idle" private lastActivity = 0 @@ -76,6 +77,7 @@ export class Tracker { this.finalRate = 0 this.state = "streaming" this.busy = true + this.historyRecorded = false this.lastActivity = this.now() if (!this.sessionSampleHistory.has(this.sessionID!)) { this.sessionSampleHistory.set(this.sessionID!, []) @@ -88,13 +90,13 @@ export class Tracker { const streaming = !info.time?.completed if (this.sessionID !== info.sessionID) { - if (!streaming) return + if (!streaming && !info.time?.completed) return this.startMessage(info, info.time?.created ?? now) if (!this.sessionSampleHistory.has(this.sessionID!)) { this.sessionSampleHistory.set(this.sessionID!, []) } } else if (this.messageID !== info.id) { - if (this.messageID === null || streaming) { + if (this.messageID === null || streaming || !!info.time?.completed) { this.startMessage(info, info.time?.created ?? now) if (!this.sessionSampleHistory.has(this.sessionID!)) { this.sessionSampleHistory.set(this.sessionID!, []) @@ -145,6 +147,7 @@ export class Tracker { this.realActive = false this.completedAt = null this.finalRate = 0 + this.historyRecorded = false this.sessionStartTimestamps.set(sessionID, this.startedAt) } if (!this.sessionSampleHistory.has(this.sessionID!)) { @@ -173,6 +176,7 @@ export class Tracker { now - this.lastActivity > this.idleTimeoutMs && (!this.busy || now - this.lastActivity > this.stuckTimeoutMs) ) { + if (!this.historyRecorded && this.sessionID) this.finalize() this.state = "idle" } else if ( this.state === "finished" && @@ -223,7 +227,7 @@ export class Tracker { } private finalize(completedAt?: number): void { - if (!this.sessionID) return + if (!this.sessionID || this.historyRecorded) return const t = completedAt ?? this.now() const output = Math.max(this.lastOutput, this.estTokens) this.pushSample(output, t) @@ -250,6 +254,7 @@ export class Tracker { endTime: t, } this.history.push(sessionData) + this.historyRecorded = true if (this.sessionID && this.sessionStartTimestamps.has(this.sessionID)) { this.sessionEndTimestamps.set(this.sessionID, t) }