Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions agents/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@livekit/mutex": "^1.1.1",
"@livekit/protocol": "^1.29.1",
"@livekit/typed-emitter": "^3.0.0",
"@std/streams": "jsr:^1.0.9",
"@std/async": "npm:@jsr/std__async@^1.0.13",
"commander": "^12.0.0",
"heap-js": "^2.6.0",
Expand Down
1 change: 0 additions & 1 deletion agents/src/stream/deferred_stream.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: 2025 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { type ReadableStream } from 'node:stream/web';
import { IdentityTransform } from './identity_transform.js';

export class DeferredReadableStream<T> {
Expand Down
1 change: 0 additions & 1 deletion agents/src/stream/identity_transform.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: 2025 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { TransformStream } from 'node:stream/web';

export class IdentityTransform<T> extends TransformStream<T, T> {
constructor() {
Expand Down
62 changes: 62 additions & 0 deletions agents/src/stream/injectable_stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Mutex } from '@livekit/mutex';
import { mergeReadableStreams } from '@std/streams';
import { IdentityTransform } from './identity_transform.js';

export class InjectableStream<T> {
private source: ReadableStream<T>;
private identityStream: IdentityTransform<T>;
private mergedStream: ReadableStream<T>;
private injectMutex = new Mutex();
private writer: WritableStreamDefaultWriter<T>;
private closed = false;

constructor(source: ReadableStream<T>) {
this.source = source;
this.identityStream = new IdentityTransform<T>();
this.mergedStream = mergeReadableStreams<T>(this.source, this.identityStream.readable);

Check failure on line 16 in agents/src/stream/injectable_stream.ts

View workflow job for this annotation

GitHub Actions / Test

agents/tests/stream/injectable_stream.test.ts > InjectableStream > After Cancel > should propagate cancel reason

TypeError: Promise.withResolvers is not a function ❯ node_modules/.pnpm/@jsr[email protected]/node_modules/@jsr/std__streams/merge_readable_streams.ts:42:53 ❯ Module.mergeReadableStreams node_modules/.pnpm/@jsr[email protected]/node_modules/@jsr/std__streams/merge_readable_streams.ts:42:35 ❯ new InjectableStream agents/src/stream/injectable_stream.ts:16:25 ❯ agents/tests/stream/injectable_stream.test.ts:202:26

Check failure on line 16 in agents/src/stream/injectable_stream.ts

View workflow job for this annotation

GitHub Actions / Test

agents/tests/stream/injectable_stream.test.ts > InjectableStream > After Cancel > should prevent injection after cancel

TypeError: Promise.withResolvers is not a function ❯ node_modules/.pnpm/@jsr[email protected]/node_modules/@jsr/std__streams/merge_readable_streams.ts:42:53 ❯ Module.mergeReadableStreams node_modules/.pnpm/@jsr[email protected]/node_modules/@jsr/std__streams/merge_readable_streams.ts:42:35 ❯ new InjectableStream agents/src/stream/injectable_stream.ts:16:25 ❯ agents/tests/stream/injectable_stream.test.ts:192:26

Check failure on line 16 in agents/src/stream/injectable_stream.ts

View workflow job for this annotation

GitHub Actions / Test

agents/tests/stream/injectable_stream.test.ts > InjectableStream > After Cancel > should cancel both streams when cancel is called

TypeError: Promise.withResolvers is not a function ❯ node_modules/.pnpm/@jsr[email protected]/node_modules/@jsr/std__streams/merge_readable_streams.ts:42:53 ❯ Module.mergeReadableStreams node_modules/.pnpm/@jsr[email protected]/node_modules/@jsr/std__streams/merge_readable_streams.ts:42:35 ❯ new InjectableStream agents/src/stream/injectable_stream.ts:16:25 ❯ agents/tests/stream/injectable_stream.test.ts:176:26

Check failure on line 16 in agents/src/stream/injectable_stream.ts

View workflow job for this annotation

GitHub Actions / Test

agents/tests/stream/injectable_stream.test.ts > InjectableStream > After Close > should prevent injection after close

TypeError: Promise.withResolvers is not a function ❯ node_modules/.pnpm/@jsr[email protected]/node_modules/@jsr/std__streams/merge_readable_streams.ts:42:53 ❯ Module.mergeReadableStreams node_modules/.pnpm/@jsr[email protected]/node_modules/@jsr/std__streams/merge_readable_streams.ts:42:35 ❯ new InjectableStream agents/src/stream/injectable_stream.ts:16:25 ❯ agents/tests/stream/injectable_stream.test.ts:160:26

Check failure on line 16 in agents/src/stream/injectable_stream.ts

View workflow job for this annotation

GitHub Actions / Test

agents/tests/stream/injectable_stream.test.ts > InjectableStream > After Close > should continue reading from source after close

TypeError: Promise.withResolvers is not a function ❯ node_modules/.pnpm/@jsr[email protected]/node_modules/@jsr/std__streams/merge_readable_streams.ts:42:53 ❯ Module.mergeReadableStreams node_modules/.pnpm/@jsr[email protected]/node_modules/@jsr/std__streams/merge_readable_streams.ts:42:35 ❯ new InjectableStream agents/src/stream/injectable_stream.ts:16:25 ❯ agents/tests/stream/injectable_stream.test.ts:127:26

Check failure on line 16 in agents/src/stream/injectable_stream.ts

View workflow job for this annotation

GitHub Actions / Test

agents/tests/stream/injectable_stream.test.ts > InjectableStream > Read/Write with Inject > should handle multiple injections (if supported)

TypeError: Promise.withResolvers is not a function ❯ node_modules/.pnpm/@jsr[email protected]/node_modules/@jsr/std__streams/merge_readable_streams.ts:42:53 ❯ Module.mergeReadableStreams node_modules/.pnpm/@jsr[email protected]/node_modules/@jsr/std__streams/merge_readable_streams.ts:42:35 ❯ new InjectableStream agents/src/stream/injectable_stream.ts:16:25 ❯ agents/tests/stream/injectable_stream.test.ts:107:26

Check failure on line 16 in agents/src/stream/injectable_stream.ts

View workflow job for this annotation

GitHub Actions / Test

agents/tests/stream/injectable_stream.test.ts > InjectableStream > Read/Write with Inject > should merge injected values with source stream

TypeError: Promise.withResolvers is not a function ❯ node_modules/.pnpm/@jsr[email protected]/node_modules/@jsr/std__streams/merge_readable_streams.ts:42:53 ❯ Module.mergeReadableStreams node_modules/.pnpm/@jsr[email protected]/node_modules/@jsr/std__streams/merge_readable_streams.ts:42:35 ❯ new InjectableStream agents/src/stream/injectable_stream.ts:16:25 ❯ agents/tests/stream/injectable_stream.test.ts:76:26

Check failure on line 16 in agents/src/stream/injectable_stream.ts

View workflow job for this annotation

GitHub Actions / Test

agents/tests/stream/injectable_stream.test.ts > InjectableStream > Happy Path > should handle empty source stream

TypeError: Promise.withResolvers is not a function ❯ node_modules/.pnpm/@jsr[email protected]/node_modules/@jsr/std__streams/merge_readable_streams.ts:42:53 ❯ Module.mergeReadableStreams node_modules/.pnpm/@jsr[email protected]/node_modules/@jsr/std__streams/merge_readable_streams.ts:42:35 ❯ new InjectableStream agents/src/stream/injectable_stream.ts:16:25 ❯ agents/tests/stream/injectable_stream.test.ts:65:26

Check failure on line 16 in agents/src/stream/injectable_stream.ts

View workflow job for this annotation

GitHub Actions / Test

agents/tests/stream/injectable_stream.test.ts > InjectableStream > Happy Path > should pass through source stream data without injection

TypeError: Promise.withResolvers is not a function ❯ node_modules/.pnpm/@jsr[email protected]/node_modules/@jsr/std__streams/merge_readable_streams.ts:42:53 ❯ Module.mergeReadableStreams node_modules/.pnpm/@jsr[email protected]/node_modules/@jsr/std__streams/merge_readable_streams.ts:42:35 ❯ new InjectableStream agents/src/stream/injectable_stream.ts:16:25 ❯ agents/tests/stream/injectable_stream.test.ts:56:26
this.writer = this.identityStream.writable.getWriter();
}

async inject(value: T) {
const unlock = await this.injectMutex.lock();

if (this.closed) {
throw new Error('Cannot inject into a closed stream');
}

try {
await this.writer.write(value);
} finally {
unlock();
}
}

async close() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@toubatbrian might be worth taking over this PR and working on the releasing the lock on the readable source once we close the stream.

Some tests would be good to.

const unlock = await this.injectMutex.lock();

if (this.closed) {
return;
}

try {
// this will not cancel the source stream but instead keep the readable open until the source finishes
this.writer.releaseLock();
await this.identityStream.writable.close();
this.closed = true;
} finally {
unlock();
}
}

async cancel(reason?: any) {
await this.close();
await Promise.all([
this.mergedStream.cancel(reason),
this.identityStream.writable.abort(reason),
]);
}

get readable() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we also consider supporting "eject" (read single value from stream) operation? Basically making stream also have a queue-like API with put as inject and get as eject

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure if there's a concrete use case?

I'd be in favor of implementing additional features (generally) only once we know we have a defined use case for it.

return this.mergedStream;
}
}
1 change: 0 additions & 1 deletion agents/src/stt/stt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import type { AudioFrame } from '@livekit/rtc-node';
import type { TypedEventEmitter as TypedEmitter } from '@livekit/typed-emitter';
import { EventEmitter } from 'node:events';
import type { ReadableStream } from 'node:stream/web';
import { log } from '../log.js';
import type { STTMetrics } from '../metrics/base.js';
import { DeferredReadableStream } from '../stream/deferred_stream.js';
Expand Down
1 change: 0 additions & 1 deletion agents/src/tts/tts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import type { AudioFrame } from '@livekit/rtc-node';
import type { TypedEventEmitter as TypedEmitter } from '@livekit/typed-emitter';
import { EventEmitter } from 'node:events';
import type { ReadableStream } from 'node:stream/web';
import { log } from '../log.js';
import type { TTSMetrics } from '../metrics/base.js';
import { DeferredReadableStream } from '../stream/deferred_stream.js';
Expand Down
6 changes: 1 addition & 5 deletions agents/src/vad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import type { AudioFrame } from '@livekit/rtc-node';
import type { TypedEventEmitter as TypedEmitter } from '@livekit/typed-emitter';
import { EventEmitter } from 'node:events';
import type {
ReadableStream,
ReadableStreamDefaultReader,
WritableStreamDefaultWriter,
} from 'node:stream/web';

import { log } from './log.js';
import type { VADMetrics } from './metrics/base.js';
import { DeferredReadableStream } from './stream/deferred_stream.js';
Expand Down
3 changes: 1 addition & 2 deletions agents/src/voice/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

/* eslint-disable @typescript-eslint/no-explicit-any */
import type { AudioFrame } from '@livekit/rtc-node';
import { ReadableStream } from 'node:stream/web';
import type { ChatChunk, ChatMessage, LLM } from '../llm/index.js';
import { ChatContext } from '../llm/index.js';
import { StreamAdapter as STTStreamAdapter } from '../stt/index.js';
import type { STT, SpeechEvent } from '../stt/index.js';
import { StreamAdapter as STTStreamAdapter } from '../stt/index.js';
import { SentenceTokenizer as BasicSentenceTokenizer } from '../tokenize/basic/index.js';
import type { TTS } from '../tts/index.js';
import { SynthesizeStream, StreamAdapter as TTSStreamAdapter } from '../tts/index.js';
Expand Down
3 changes: 1 addition & 2 deletions agents/src/voice/agent_activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SPDX-License-Identifier: Apache-2.0
import type { AudioFrame } from '@livekit/rtc-node';
import { Heap } from 'heap-js';
import type { ReadableStream } from 'node:stream/web';
import { type ChatContext, ChatMessage, ChatRole } from '../llm/chat_context.js';
import type { LLM } from '../llm/index.js';
import { log } from '../log.js';
Expand All @@ -12,8 +11,8 @@ import type { STT, SpeechEvent } from '../stt/stt.js';
import type { TTS } from '../tts/tts.js';
import { Future } from '../utils.js';
import type { VADEvent } from '../vad.js';
import { StopResponse } from './agent.js';
import type { Agent } from './agent.js';
import { StopResponse } from './agent.js';
import type { AgentSession } from './agent_session.js';
import {
AudioRecognition,
Expand Down
3 changes: 1 addition & 2 deletions agents/src/voice/agent_session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
//
// SPDX-License-Identifier: Apache-2.0
import type { AudioFrame, AudioSource, Room } from '@livekit/rtc-node';
import type { ReadableStream } from 'node:stream/web';
import { ChatContext } from '../llm/chat_context.js';
import type { ChatMessage } from '../llm/chat_context.js';
import { ChatContext } from '../llm/chat_context.js';
import type { LLM } from '../llm/index.js';
import { log } from '../log.js';
import type { AgentState } from '../pipeline/index.js';
Expand Down
1 change: 0 additions & 1 deletion agents/src/voice/audio_recognition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SPDX-License-Identifier: Apache-2.0
import type { AudioFrame } from '@livekit/rtc-node';
import { delay } from '@std/async';
import { ReadableStream } from 'node:stream/web';
import { type ChatContext, ChatRole } from '../llm/chat_context.js';
import { log } from '../log.js';
import { DeferredReadableStream } from '../stream/deferred_stream.js';
Expand Down
1 change: 0 additions & 1 deletion agents/src/voice/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// SPDX-License-Identifier: Apache-2.0
import type { AudioFrame, AudioSource } from '@livekit/rtc-node';
import { randomUUID } from 'node:crypto';
import type { ReadableStream } from 'stream/web';
import type { ChatContext } from '../llm/chat_context.js';
import { IdentityTransform } from '../stream/identity_transform.js';
import { Future } from '../utils.js';
Expand Down
1 change: 0 additions & 1 deletion agents/src/voice/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//
// SPDX-License-Identifier: Apache-2.0
import type { AudioFrame } from '@livekit/rtc-node';
import type { ReadableStream } from 'node:stream/web';
import type { ChatContext } from '../llm/chat_context.js';
import type { ChatChunk } from '../llm/llm.js';
import type { SpeechEvent } from '../stt/stt.js';
Expand Down
3 changes: 1 addition & 2 deletions agents/src/voice/room_io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
TrackPublishOptions,
TrackSource,
} from '@livekit/rtc-node';
import type { ReadableStream } from 'node:stream/web';
import { log } from '../log.js';
import { DeferredReadableStream } from '../stream/deferred_stream.js';
import type { AgentSession } from './agent_session.js';
Expand Down Expand Up @@ -57,7 +56,7 @@ export class RoomIO {
// TODO(AJS-41) remove hardcoded sample rate
sampleRate: 16000,
numChannels: 1,
}),
}) as ReadableStream<AudioFrame>,
);
}
};
Expand Down
Loading
Loading