incognitolm commited on
Commit
d2850b8
·
1 Parent(s): 357bada

Update chatStream.js

Browse files
Files changed (1) hide show
  1. server/chatStream.js +9 -5
server/chatStream.js CHANGED
@@ -3,6 +3,7 @@ import { Worker } from "worker_threads";
3
  import { fileURLToPath } from "url";
4
  import path from "path";
5
  import { LIGHTNING_BASE } from "./config.js";
 
6
 
7
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
8
  const WORKER_PATH = path.join(__dirname, "searchWorker.js");
@@ -54,8 +55,11 @@ function makeClient(accessToken, clientId) {
54
 
55
  async function websocketChatStream(body, headers, onToken) {
56
 
57
- const ws = new WebSocket(`${LIGHTNING_BASE.replace("http","ws")}/ws/chat`);
58
-
 
 
 
59
  await new Promise(resolve => {
60
  ws.onopen = resolve;
61
  });
@@ -80,9 +84,9 @@ async function websocketChatStream(body, headers, onToken) {
80
 
81
  return new Promise((resolve, reject) => {
82
 
83
- ws.onmessage = (event) => {
84
 
85
- const line = event.data;
86
 
87
  if (!line.startsWith("data:")) return;
88
 
@@ -99,7 +103,7 @@ async function websocketChatStream(body, headers, onToken) {
99
  ws.close();
100
  resolve({ assistantText });
101
  }
102
- };
103
 
104
  ws.onerror = reject;
105
  });
 
3
  import { fileURLToPath } from "url";
4
  import path from "path";
5
  import { LIGHTNING_BASE } from "./config.js";
6
+ import WebSocket from "ws";
7
 
8
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
9
  const WORKER_PATH = path.join(__dirname, "searchWorker.js");
 
55
 
56
  async function websocketChatStream(body, headers, onToken) {
57
 
58
+ const wsURL =
59
+ (LIGHTNING_BASE.startsWith("https")
60
+ ? LIGHTNING_BASE.replace("https", "wss")
61
+ : LIGHTNING_BASE.replace("http", "ws")
62
+ ) + "/ws/chat";
63
  await new Promise(resolve => {
64
  ws.onopen = resolve;
65
  });
 
84
 
85
  return new Promise((resolve, reject) => {
86
 
87
+ ws.on("message", (data) => {
88
 
89
+ const line = data.toString();
90
 
91
  if (!line.startsWith("data:")) return;
92
 
 
103
  ws.close();
104
  resolve({ assistantText });
105
  }
106
+ });
107
 
108
  ws.onerror = reject;
109
  });