Skip to content

Commit 9fe8413

Browse files
committed
Implement message logging to create recorded exchanges.
Signed-off-by: Katharine Berry <[email protected]>
1 parent a1af52f commit 9fe8413

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

app/src/pkjs/lib/message_queue.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var MAX_BYTES_IN_FLIGHT = 600;
1818

1919
function MessageQueue() {
2020
this.queue = [];
21+
this.log = null;
2122
this.messagesInFlight = 0;
2223
this.bytesInFlight = 0;
2324
}
@@ -44,7 +45,22 @@ function countBytes(message) {
4445
return bytes;
4546
}
4647

48+
MessageQueue.prototype.startLogging = function() {
49+
this.log = [];
50+
};
51+
52+
MessageQueue.prototype.stopLogging = function() {
53+
this.log = null;
54+
}
55+
56+
MessageQueue.prototype.getLog = function() {
57+
return this.log || [];
58+
}
59+
4760
MessageQueue.prototype.enqueue = function(message) {
61+
if (this.log) {
62+
this.log.push(message);
63+
}
4864
this.queue.push(message);
4965
if (this.messagesInFlight < 10 && this.bytesInFlight < MAX_BYTES_IN_FLIGHT) {
5066
console.log('sending immediately, messages in flight: ' + this.messagesInFlight + ', bytes in flight: ' + this.bytesInFlight);

app/src/pkjs/session.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
var LOGGING_ENABLED = false;
18+
1719
var location = require('./location');
1820
var config = require('./config');
1921
var actions = require('./actions');
@@ -35,6 +37,9 @@ function getSettings() {
3537
}
3638

3739
Session.prototype.run = function() {
40+
if (LOGGING_ENABLED) {
41+
messageQueue.startLogging();
42+
}
3843
console.log("Opening websocket connection...");
3944
var url = API_URL + '?prompt=' + encodeURIComponent(this.prompt) + '&token=' + exports.userToken;
4045
if (location.isReady() && config.isLocationEnabled()) {
@@ -151,6 +156,10 @@ Session.prototype.handleMessage = function(event) {
151156
this.enqueue({
152157
CHAT_DONE: true
153158
});
159+
if (LOGGING_ENABLED) {
160+
console.log(JSON.stringify(messageQueue.getLog()));
161+
messageQueue.stopLogging();
162+
}
154163
} else if (message[0] == 'a') {
155164
actions.handleAction(this, this.ws, message.substring(1));
156165
} else if (message[0] == 't') {

0 commit comments

Comments
 (0)