Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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",
"commander": "^12.0.0",
"livekit-server-sdk": "^2.9.2",
"pino": "^8.19.0",
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
48 changes: 48 additions & 0 deletions agents/src/stream/injectable_stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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();

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

async inject(value: T) {
const unlock = await this.injectMutex.lock();
try {
const writer = this.identityStream.writable.getWriter();
await writer.write(value);
await writer.close();
} 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();
try {
// this will not cancel the source stream but instead keep the readable open until the source finishes
await this.identityStream.writable.close();
} finally {
unlock();
}
}

async cancel(reason?: any) {
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;
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"examples:minimal": "pnpm exec tsx examples/src/multimodal_agent.ts"
},
"devDependencies": {
"@types/node": "^22.13.10",
"@changesets/cli": "^2.27.1",
"@livekit/changesets-changelog-github": "^0.0.4",
"@rushstack/heft": "^0.66.0",
Expand All @@ -44,5 +45,5 @@
"typescript": "^5.4.5",
"vitest": "^1.6.0"
},
"packageManager": "pnpm@9.7.0"
"packageManager": "pnpm@10.11.0"
}
Loading