From 476aab3b625004cc12b8d579edfc04d8f835b7b2 Mon Sep 17 00:00:00 2001 From: "Mx. Corey Frang" Date: Thu, 27 Jun 2024 13:28:44 -0400 Subject: [PATCH 1/4] Add some thrown errors if the at driver has closed --- src/agent/at-driver.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/agent/at-driver.js b/src/agent/at-driver.js index 82ca2ca..aba4254 100644 --- a/src/agent/at-driver.js +++ b/src/agent/at-driver.js @@ -43,7 +43,14 @@ export class ATDriver { } ) ); - this.closed = new Promise(resolve => socket.once('close', () => resolve())); + this.hasClosed = false; + this.closed = new Promise(resolve => + socket.once('close', () => { + this.hasClosed = true; + this.log(AgentMessage.AT_DRIVER_COMMS, { direction: 'closed' }); + resolve(); + }) + ); this._nextId = 0; } @@ -62,6 +69,7 @@ export class ATDriver { for await (const rawMessage of iterateEmitter(this.socket, 'message', 'close', 'error')) { const message = rawMessage.toString(); this.log(AgentMessage.AT_DRIVER_COMMS, { direction: 'inbound', message }); + if (this.hasClosed) throw new Error('AT-Driver connection unexpectedly closed'); yield JSON.parse(message); } } @@ -70,8 +78,10 @@ export class ATDriver { const id = this._nextId++; const rawMessage = JSON.stringify({ id, ...command }); this.log(AgentMessage.AT_DRIVER_COMMS, { direction: 'outbound', message: rawMessage }); + if (this.hasClosed) throw new Error('AT-Driver connection unexpectedly closed'); this.socket.send(rawMessage); for await (const message of this._messages()) { + if (this.hasClosed) throw new Error('AT-Driver connection unexpectedly closed'); if (message.id === id) { if (message.error) { throw new Error(message.error); @@ -101,6 +111,7 @@ export class ATDriver { */ async *speeches() { for await (const message of this._messages()) { + if (this.hasClosed) throw new Error('AT-Driver connection unexpectedly closed'); if (message.method === 'interaction.capturedOutput') { yield message.params.data; } From 7abe47794ea776690bc4add1cd9e756150d748d2 Mon Sep 17 00:00:00 2001 From: "Mx. Corey Frang" Date: Wed, 3 Jul 2024 14:39:33 -0400 Subject: [PATCH 2/4] throw trying to collect _messages after close has happened --- src/agent/at-driver.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agent/at-driver.js b/src/agent/at-driver.js index aba4254..ea001a9 100644 --- a/src/agent/at-driver.js +++ b/src/agent/at-driver.js @@ -66,10 +66,10 @@ export class ATDriver { } async *_messages() { + if (this.hasClosed) throw new Error('AT-Driver connection unexpectedly closed'); for await (const rawMessage of iterateEmitter(this.socket, 'message', 'close', 'error')) { const message = rawMessage.toString(); this.log(AgentMessage.AT_DRIVER_COMMS, { direction: 'inbound', message }); - if (this.hasClosed) throw new Error('AT-Driver connection unexpectedly closed'); yield JSON.parse(message); } } From 21d1b369b053e0d9258503e7b8c0db025b1f7d5a Mon Sep 17 00:00:00 2001 From: "Mx. Corey Frang" Date: Wed, 3 Jul 2024 14:44:00 -0400 Subject: [PATCH 3/4] move more hasClosed checks to before the _messages --- src/agent/at-driver.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/agent/at-driver.js b/src/agent/at-driver.js index ea001a9..ea15605 100644 --- a/src/agent/at-driver.js +++ b/src/agent/at-driver.js @@ -81,7 +81,6 @@ export class ATDriver { if (this.hasClosed) throw new Error('AT-Driver connection unexpectedly closed'); this.socket.send(rawMessage); for await (const message of this._messages()) { - if (this.hasClosed) throw new Error('AT-Driver connection unexpectedly closed'); if (message.id === id) { if (message.error) { throw new Error(message.error); @@ -110,8 +109,8 @@ export class ATDriver { * @returns {AsyncGenerator} */ async *speeches() { + if (this.hasClosed) throw new Error('AT-Driver connection unexpectedly closed'); for await (const message of this._messages()) { - if (this.hasClosed) throw new Error('AT-Driver connection unexpectedly closed'); if (message.method === 'interaction.capturedOutput') { yield message.params.data; } From 8e405b20e5cbd2a597af2ee31873db239ccbff3b Mon Sep 17 00:00:00 2001 From: "Mx. Corey Frang" Date: Thu, 11 Jul 2024 10:38:38 -0400 Subject: [PATCH 4/4] Review feedback - thanks mike! --- src/agent/at-driver.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/agent/at-driver.js b/src/agent/at-driver.js index ea15605..51cc518 100644 --- a/src/agent/at-driver.js +++ b/src/agent/at-driver.js @@ -78,8 +78,12 @@ export class ATDriver { const id = this._nextId++; const rawMessage = JSON.stringify({ id, ...command }); this.log(AgentMessage.AT_DRIVER_COMMS, { direction: 'outbound', message: rawMessage }); - if (this.hasClosed) throw new Error('AT-Driver connection unexpectedly closed'); - this.socket.send(rawMessage); + await new Promise((resolve, reject) => { + this.socket.send(rawMessage, error => { + if (error) reject(error); + else resolve(); + }); + }); for await (const message of this._messages()) { if (message.id === id) { if (message.error) { @@ -109,7 +113,6 @@ export class ATDriver { * @returns {AsyncGenerator} */ async *speeches() { - if (this.hasClosed) throw new Error('AT-Driver connection unexpectedly closed'); for await (const message of this._messages()) { if (message.method === 'interaction.capturedOutput') { yield message.params.data;