MCPcopy Create free account
hub / github.com/CreminiAI/skillpack / getOrCreateWs

Function getOrCreateWs

web/js/chat.js:251–316  ·  view source on GitHub ↗
(options = {})

Source from the content-addressed store, hash-verified

249}
250
251async function getOrCreateWs(options = {}) {
252 const { background = false } = options;
253 if (ws && ws.readyState === WebSocket.OPEN) {
254 return ws;
255 }
256
257 if (wsConnectPromise) {
258 return wsConnectPromise;
259 }
260
261 clearReconnectTimer();
262
263 wsConnectPromise = new Promise((resolve, reject) => {
264 const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
265 const provider = state.config && state.config.provider ? state.config.provider : "openai";
266 const params = new URLSearchParams({
267 provider,
268 channelId: currentChannelId || DEFAULT_WEB_CHANNEL_ID,
269 });
270 const wsUrl = `${protocol}//${window.location.host}${state.API_BASE}/api/chat?${params.toString()}`;
271
272 const socket = new WebSocket(wsUrl);
273 ws = socket;
274
275 socket.onopen = () => {
276 shouldMaintainWs = true;
277 wsConnectPromise = null;
278 resolve(socket);
279 };
280 socket.onerror = (err) => {
281 console.error(err);
282 wsConnectPromise = null;
283 reject(new Error("WebSocket connection failed"));
284 };
285
286 socket.onmessage = (event) => {
287 try {
288 const parsed = JSON.parse(event.data);
289 if (parsed.error) {
290 if (background && !currentAssistantMsg) {
291 console.warn("Background WebSocket message error:", parsed.error);
292 } else {
293 handleError(parsed.error);
294 }
295 } else if (parsed.done) {
296 handleDone();
297 } else if (parsed.type) {
298 handleAgentEvent(parsed);
299 }
300 } catch (e) {
301 console.error("Failed to parse message", e);
302 }
303 };
304
305 socket.onclose = () => {
306 if (ws === socket) {
307 ws = null;
308 }

Callers 3

sendMessageFunction · 0.85
sendBotCommandFunction · 0.85

Calls 7

clearReconnectTimerFunction · 0.85
handleErrorFunction · 0.85
handleDoneFunction · 0.85
handleAgentEventFunction · 0.85
enableInputFunction · 0.85
scheduleReconnectFunction · 0.85
parseMethod · 0.80

Tested by

no test coverage detected