MCPcopy Create free account
hub / github.com/AsyncFuncAI/deepwiki-open / createChatWebSocket

Function createChatWebSocket

src/utils/websocketClient.ts:43–75  ·  view source on GitHub ↗
(
  request: ChatCompletionRequest,
  onMessage: (message: string) => void,
  onError: (error: Event) => void,
  onClose: () => void
)

Source from the content-addressed store, hash-verified

41 * @returns The WebSocket connection
42 */
43export const createChatWebSocket = (
44 request: ChatCompletionRequest,
45 onMessage: (message: string) => void,
46 onError: (error: Event) => void,
47 onClose: () => void
48): WebSocket => {
49 // Create WebSocket connection
50 const ws = new WebSocket(getWebSocketUrl());
51
52 // Set up event handlers
53 ws.onopen = () => {
54 console.log('WebSocket connection established');
55 // Send the request as JSON
56 ws.send(JSON.stringify(request));
57 };
58
59 ws.onmessage = (event) => {
60 // Call the message handler with the received text
61 onMessage(event.data);
62 };
63
64 ws.onerror = (error) => {
65 console.error('WebSocket error:', error);
66 onError(error);
67 };
68
69 ws.onclose = () => {
70 console.log('WebSocket connection closed');
71 onClose();
72 };
73
74 return ws;
75};
76
77/**
78 * Closes a WebSocket connection

Callers 2

continueResearchFunction · 0.90
handleConfirmAskFunction · 0.90

Calls 1

getWebSocketUrlFunction · 0.85

Tested by

no test coverage detected