Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agent: Update WebRTC data channel messages #505

Merged
merged 10 commits into from
Nov 20, 2023
18 changes: 15 additions & 3 deletions packages/voice/src/app/agent/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,8 @@ export class WebRtcChatManager implements ChatManager {
console.log('[chat] got mic stream');
this.inAnalyzer = new StreamAnalyzer(this.audioContext, this.localAudioTrack!.mediaStream!);
this.pinger = setInterval(() => {
var obj = { type: 'ping', timestamp: performance.now() };
this.room?.localParticipant.publishData(this.textEncoder.encode(JSON.stringify(obj)), DataPacket_Kind.RELIABLE);
const obj = { type: 'ping', timestamp: performance.now() };
this.sendData(obj);
}, 5000);
this.maybePublishLocalAudio();
this.changeState(ChatManagerState.LISTENING);
Expand All @@ -608,7 +608,9 @@ export class WebRtcChatManager implements ChatManager {
this.changeState(ChatManagerState.IDLE);
}
interrupt() {
throw new Error('Method not implemented.');
console.log('[chat] interrupting');
const obj = { type: 'interrupt' };
this.sendData(obj);
}
private changeState(state: ChatManagerState) {
if (state != this._state) {
Expand All @@ -624,6 +626,9 @@ export class WebRtcChatManager implements ChatManager {
this.room.localParticipant.publishTrack(this.localAudioTrack, opts);
}
}
private sendData(obj: any) {
this.room?.localParticipant.publishData(this.textEncoder.encode(JSON.stringify(obj)), DataPacket_Kind.RELIABLE);
}
private handleSocketOpen() {
console.log('[chat] socket opened');
const obj = {
Expand Down Expand Up @@ -678,6 +683,13 @@ export class WebRtcChatManager implements ChatManager {
if (data.type === 'pong') {
const elapsed_ms = performance.now() - data.timestamp;
console.debug(`[chat] worker RTT: ${elapsed_ms.toFixed(0)} ms`);
} else if (data.type === 'state') {
const newState = data.state;
this.changeState(newState);
} else if (data.type === 'transcript') {
console.log(`[chat] transcript: ${data.text}`);
} else if (data.type === 'output') {
console.log(`[chat] output: ${data.text}`);
}
}
}
Expand Down