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

Bugfix/issue 27 invalid item access #29

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ export class RealtimeClient extends RealtimeEventHandler {

// Setup for application control flow
const handler = (event, ...args) => {
if (!this.realtime.isConnected()) {
return { item: null, delta: null };
}
const { item, delta } = this.conversation.processEvent(event, ...args);
return { item, delta };
};
Expand Down Expand Up @@ -329,6 +332,9 @@ export class RealtimeClient extends RealtimeEventHandler {
// Handlers to update application state
this.realtime.on('server.conversation.item.created', (event) => {
const { item } = handlerWithDispatch(event);
if (!item) {
return;
}
this.dispatch('conversation.item.appended', { item });
if (item.status === 'completed') {
this.dispatch('conversation.item.completed', { item });
Expand All @@ -352,6 +358,9 @@ export class RealtimeClient extends RealtimeEventHandler {
);
this.realtime.on('server.response.output_item.done', async (event) => {
const { item } = handlerWithDispatch(event);
if (!item) {
return;
}
if (item.status === 'completed') {
this.dispatch('conversation.item.completed', { item });
}
Expand Down
33 changes: 33 additions & 0 deletions test/tests/client_disconnect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as chai from 'chai';
const expect = chai.expect;

import { RealtimeClient } from '../../index.js';

export async function run({ debug = false } = {}) {
describe('RealtimeClient (Node.js) - Disconnect', () => {

it('Should not throw an error if the client disconnects while receiving messages', async function() {
this.timeout(10000);
const client = new RealtimeClient({
apiKey: process.env.OPENAI_API_KEY,
debug,
});

client.realtime.on('server.response.audio.delta', () => {
client.disconnect();
});

await client.connect();
await client.waitForSessionCreated();

client.sendUserMessageContent([{ type: 'input_text', text: 'Hello' }]);

await client.waitForNextCompletedItem();


expect(client).to.exist;

});
});

}