Move copilot to websockets

This commit is contained in:
Pranav
2025-05-11 00:37:59 -07:00
parent bcce9cf74b
commit 34e929c705
16 changed files with 422 additions and 151 deletions
@@ -0,0 +1,49 @@
import { createConsumer } from '@rails/actioncable';
class CopilotActionCableConnector {
constructor(
{ accountId, userId, onDisconnect, onCopilotResponse },
websocketHost = ''
) {
const websocketURL = websocketHost ? `${websocketHost}/cable` : undefined;
this.consumer = createConsumer(websocketURL);
this.subscription = this.consumer.subscriptions.create(
{
channel: 'CopilotChannel',
account_id: accountId,
user_id: userId,
},
{
received: this.onReceived,
disconnected: onDisconnect,
}
);
this.events = {
'copilot.response': onCopilotResponse,
};
}
onReceived = ({ event, data } = {}) => {
if (this.events[event] && typeof this.events[event] === 'function') {
this.events[event](data);
}
};
disconnect() {
this.subscription.unsubscribe();
this.consumer.disconnect();
}
sendMessage({ message, assistantId, conversationId, previousHistory }) {
this.subscription.perform('message', {
message,
assistant_id: assistantId,
conversation_id: conversationId,
previous_history: previousHistory,
});
}
}
export default CopilotActionCableConnector;