Skip to content

Commit 18a67fc

Browse files
authored
Shubhra/set ttft and ttfb to -1 when queue is empty (#475)
1 parent 2ec46bb commit 18a67fc

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

.changeset/cold-tomatoes-eat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@livekit/agents': patch
3+
---
4+
5+
set ttft/ttfb to -1 when output is empty

agents/src/llm/llm.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ export abstract class LLMStream implements AsyncIterableIterator<ChatChunk> {
7878

7979
protected async monitorMetrics() {
8080
const startTime = process.hrtime.bigint();
81-
let ttft: bigint | undefined;
81+
let ttft: bigint = BigInt(-1);
8282
let requestId = '';
8383
let usage: CompletionUsage | undefined;
8484

8585
for await (const ev of this.queue) {
8686
this.output.put(ev);
8787
requestId = ev.requestId;
88-
if (!ttft) {
88+
if (ttft === BigInt(-1)) {
8989
ttft = process.hrtime.bigint() - startTime;
9090
}
9191
if (ev.usage) {
@@ -98,7 +98,7 @@ export abstract class LLMStream implements AsyncIterableIterator<ChatChunk> {
9898
const metrics: LLMMetrics = {
9999
timestamp: Date.now(),
100100
requestId,
101-
ttft: Math.trunc(Number(ttft! / BigInt(1000000))),
101+
ttft: ttft === BigInt(-1) ? -1 : Math.trunc(Number(ttft / BigInt(1000000))),
102102
duration: Math.trunc(Number(duration / BigInt(1000000))),
103103
cancelled: false, // XXX(nbsp)
104104
label: this.label,

agents/src/tts/tts.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export abstract class SynthesizeStream
136136
const metrics: TTSMetrics = {
137137
timestamp: Date.now(),
138138
requestId,
139-
ttfb: Math.trunc(Number(ttfb! / BigInt(1000000))),
139+
ttfb: Math.trunc(Number((ttfb || BigInt(0)) / BigInt(1000000))),
140140
duration: Math.trunc(Number(duration / BigInt(1000000))),
141141
charactersCount: text.length,
142142
audioDuration,
@@ -257,13 +257,13 @@ export abstract class ChunkedStream implements AsyncIterableIterator<Synthesized
257257
protected async monitorMetrics() {
258258
const startTime = process.hrtime.bigint();
259259
let audioDuration = 0;
260-
let ttfb: bigint | undefined;
260+
let ttfb: bigint = BigInt(-1);
261261
let requestId = '';
262262

263263
for await (const audio of this.queue) {
264264
this.output.put(audio);
265265
requestId = audio.requestId;
266-
if (!ttfb) {
266+
if (ttfb === BigInt(-1)) {
267267
ttfb = process.hrtime.bigint() - startTime;
268268
}
269269
audioDuration += audio.frame.samplesPerChannel / audio.frame.sampleRate;
@@ -274,7 +274,7 @@ export abstract class ChunkedStream implements AsyncIterableIterator<Synthesized
274274
const metrics: TTSMetrics = {
275275
timestamp: Date.now(),
276276
requestId,
277-
ttfb: Math.trunc(Number(ttfb! / BigInt(1000000))),
277+
ttfb: ttfb === BigInt(-1) ? -1 : Math.trunc(Number(ttfb / BigInt(1000000))),
278278
duration: Math.trunc(Number(duration / BigInt(1000000))),
279279
charactersCount: this.#text.length,
280280
audioDuration,

0 commit comments

Comments
 (0)