Skip to content

Commit 5c04e0b

Browse files
committed
bug fixes on details
1 parent e399b9b commit 5c04e0b

5 files changed

Lines changed: 21 additions & 20 deletions

File tree

agents/src/llm/fallback_adapter.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,6 @@ class FallbackLLMStream extends LLMStream {
209209
extraKwargs: this.extraKwargs,
210210
});
211211

212-
// Listen for error events - child LLMs emit errors via their LLM instance, not the stream
213-
let streamError: Error | undefined;
214-
const errorHandler = (ev: { error: Error }) => {
215-
streamError = ev.error;
216-
};
217-
llm.on('error', errorHandler);
218-
219212
try {
220213
let shouldSetCurrent = !checkRecovery;
221214
for await (const chunk of stream) {
@@ -225,11 +218,6 @@ class FallbackLLMStream extends LLMStream {
225218
}
226219
yield chunk;
227220
}
228-
229-
// If an error was emitted but not thrown through iteration, throw it now
230-
if (streamError) {
231-
throw streamError;
232-
}
233221
} catch (error) {
234222
if (error instanceof APIError) {
235223
if (checkRecovery) {
@@ -257,8 +245,6 @@ class FallbackLLMStream extends LLMStream {
257245
this._log.error({ llm: llm.label(), error }, 'unexpected error, switching to next LLM');
258246
}
259247
throw error;
260-
} finally {
261-
llm.off('error', errorHandler);
262248
}
263249
}
264250

agents/src/telemetry/otel_http_exporter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export interface SimpleOTLPHttpLogExporterConfig {
5757
export class SimpleOTLPHttpLogExporter {
5858
private readonly config: SimpleOTLPHttpLogExporterConfig;
5959
private jwt: string | null = null;
60+
private jwtExpiresAt = 0;
6061

6162
private static readonly FORCE_DOUBLE_KEYS = new Set([
6263
'transcriptConfidence',
@@ -102,7 +103,7 @@ export class SimpleOTLPHttpLogExporter {
102103
}
103104

104105
private async ensureJwt(): Promise<void> {
105-
if (this.jwt) return;
106+
if (this.jwt && Date.now() < this.jwtExpiresAt) return;
106107

107108
const apiKey = process.env.LIVEKIT_API_KEY;
108109
const apiSecret = process.env.LIVEKIT_API_SECRET;
@@ -114,6 +115,7 @@ export class SimpleOTLPHttpLogExporter {
114115
const token = new AccessToken(apiKey, apiSecret, { ttl: '6h' });
115116
token.addObservabilityGrant({ write: true });
116117
this.jwt = await token.toJwt();
118+
this.jwtExpiresAt = Date.now() + 5 * 60 * 60 * 1000;
117119
}
118120

119121
private buildPayload(records: SimpleLogRecord[]): object {

agents/src/telemetry/pino_otel_transport.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export class PinoCloudExporter {
9696
private readonly batchSize: number;
9797
private readonly flushIntervalMs: number;
9898
private jwt: string | null = null;
99+
private jwtExpiresAt = 0;
99100
private pendingLogs: any[] = [];
100101
private flushTimer: NodeJS.Timeout | null = null;
101102

@@ -172,6 +173,9 @@ export class PinoCloudExporter {
172173
await this.sendLogs(logs);
173174
} catch (error) {
174175
this.pendingLogs = [...logs, ...this.pendingLogs];
176+
if (this.pendingLogs.length > 10000) {
177+
this.pendingLogs = this.pendingLogs.slice(-10000);
178+
}
175179
console.error('[PinoCloudExporter] Failed to flush logs:', error);
176180
}
177181
}
@@ -223,7 +227,7 @@ export class PinoCloudExporter {
223227
}
224228

225229
private async ensureJwt(): Promise<void> {
226-
if (this.jwt) return;
230+
if (this.jwt && Date.now() < this.jwtExpiresAt) return;
227231

228232
const apiKey = process.env.LIVEKIT_API_KEY;
229233
const apiSecret = process.env.LIVEKIT_API_SECRET;
@@ -235,6 +239,7 @@ export class PinoCloudExporter {
235239
const token = new AccessToken(apiKey, apiSecret, { ttl: '6h' });
236240
token.addObservabilityGrant({ write: true });
237241
this.jwt = await token.toJwt();
242+
this.jwtExpiresAt = Date.now() + 5 * 60 * 60 * 1000;
238243
}
239244

240245
async shutdown(): Promise<void> {

agents/src/utils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ export const mergeFrames = (buffer: AudioBuffer): AudioFrame => {
6262

6363
const sampleRate = buffer[0]!.sampleRate;
6464
const channels = buffer[0]!.channels;
65+
let totalDataLength = 0;
6566
let samplesPerChannel = 0;
66-
let data = new Int16Array();
6767

6868
for (const frame of buffer) {
6969
if (frame.sampleRate !== sampleRate) {
@@ -74,10 +74,17 @@ export const mergeFrames = (buffer: AudioBuffer): AudioFrame => {
7474
throw new TypeError('channel count mismatch');
7575
}
7676

77-
data = new Int16Array([...data, ...frame.data]);
77+
totalDataLength += frame.data.length;
7878
samplesPerChannel += frame.samplesPerChannel;
7979
}
8080

81+
const data = new Int16Array(totalDataLength);
82+
let offset = 0;
83+
for (const frame of buffer) {
84+
data.set(frame.data, offset);
85+
offset += frame.data.length;
86+
}
87+
8188
return new AudioFrame(data, sampleRate, channels, samplesPerChannel);
8289
}
8390

@@ -101,7 +108,7 @@ export class Queue<T> {
101108
await once(this.#events, 'put');
102109
}
103110
let item = this.items.shift();
104-
if (typeof item === 'undefined') {
111+
if (item === undefined) {
105112
item = await _get();
106113
}
107114
return item;

agents/src/voice/speech_handle.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class SpeechHandle {
4141
_tasks: Task<void>[] = [];
4242

4343
/** @internal */
44-
_numSteps = 1;
44+
_numSteps: number;
4545

4646
/** @internal - OpenTelemetry context for the agent turn span */
4747
_agentTurnContext?: Context;
@@ -62,6 +62,7 @@ export class SpeechHandle {
6262
public _stepIndex: number,
6363
readonly parent?: SpeechHandle,
6464
) {
65+
this._numSteps = _stepIndex;
6566
this.doneFut.await.finally(() => {
6667
for (const callback of this.doneCallbacks) {
6768
callback(this);

0 commit comments

Comments
 (0)