Spaces:
Running
Running
File size: 1,775 Bytes
9434357 bc7e9cd 9434357 bc7e9cd 9434357 bc7e9cd 9434357 7ad6991 bc7e9cd 7ad6991 bc7e9cd 7ad6991 9434357 bc7e9cd 9434357 bc7e9cd 9434357 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
import { websocketService, type WebSocketMessage } from "./websocket";
import { messageHandler } from "./message-handler";
import { chatController } from "../controllers/chat-controller";
import { authStore } from "./auth";
export class AgentService {
private isInitialized = false;
private unsubscribeHandlers: Array<() => void> = [];
connect(): void {
if (this.isInitialized) return;
this.unsubscribeHandlers.push(
websocketService.onConnection((connected) => {
chatController.handleConnectionChange(connected);
}),
websocketService.onMessage(this.handleMessage.bind(this)),
);
websocketService.connect();
this.isInitialized = true;
}
disconnect(): void {
websocketService.disconnect();
this.unsubscribeHandlers.forEach((unsubscribe) => unsubscribe());
this.unsubscribeHandlers = [];
this.isInitialized = false;
}
sendMessage(content: string): void {
chatController.sendMessage(content);
}
stopConversation(): void {
chatController.stopConversation();
}
clearConversation(): void {
chatController.clearConversation();
}
sendRawMessage(message: unknown): void {
if (websocketService.isConnected()) {
websocketService.send(message as WebSocketMessage);
}
}
reauthenticate(): void {
if (websocketService.isConnected()) {
const token = authStore.getToken();
if (token) {
websocketService.send({
type: "auth",
payload: { token },
timestamp: Date.now(),
} as WebSocketMessage);
}
} else {
this.connect();
}
}
private handleMessage(message: WebSocketMessage): void {
messageHandler.handleMessage(message);
}
}
export const agentService = new AgentService();
|