File size: 2,859 Bytes
bc7e9cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { get } from "svelte/store";
import { websocketService } from "../services/websocket";
import { chatStore } from "../stores/chat-store";
import { authStore } from "../services/auth";
import { type ChatMessage } from "../models/chat-data";

export class ChatController {
  sendMessage(content: string): void {
    const trimmedContent = content.trim();
    if (!trimmedContent) return;

    const state = get(chatStore);
    if (!state.connected || state.processing) return;

    const userMessage: ChatMessage = {
      id: `user_${Date.now()}`,
      role: "user",
      content: trimmedContent,
      timestamp: Date.now(),
    };

    chatStore.addMessage(userMessage);
    chatStore.setError(null);

    websocketService.send({
      type: "chat",
      payload: { content: trimmedContent },
      timestamp: Date.now(),
    });

    chatStore.setProcessing(true);
  }

  stopConversation(): void {
    const state = get(chatStore);
    if (!state.connected) return;

    websocketService.send({
      type: "abort",
      payload: {},
      timestamp: Date.now(),
    });

    chatStore.setProcessing(false);
  }

  clearConversation(): void {
    const state = get(chatStore);
    if (!state.connected) return;

    websocketService.send({
      type: "clear_conversation",
      payload: {},
      timestamp: Date.now(),
    });

    chatStore.clearMessages();
  }

  handleConnectionChange(connected: boolean): void {
    chatStore.setConnected(connected);

    if (connected) {
      this.authenticate();
      this.syncEditor();
    }
  }

  handleProcessingChange(processing: boolean): void {
    chatStore.setProcessing(processing);
  }

  handleError(error: string | null): void {
    chatStore.setError(error);
    chatStore.setProcessing(false);
  }

  private authenticate(): void {
    const token = authStore.getToken();
    if (token) {
      websocketService.send({
        type: "auth",
        payload: { token },
        timestamp: Date.now(),
      });
    } else {
      const authState = get(authStore);
      if (!authState.isAuthenticated && !authState.loading) {
        chatStore.setError(
          "Authentication required. Please sign in with Hugging Face.",
        );
        chatStore.setConnected(false);
        websocketService.disconnect();
      }
    }
  }

  private syncEditor(): void {
    import("../services/content-manager").then(({ contentManager }) => {
      const currentContent = contentManager.getCurrentContent();
      if (currentContent) {
        setTimeout(() => {
          if (websocketService.isConnected()) {
            websocketService.send({
              type: "editor_sync",
              payload: { content: currentContent },
              timestamp: Date.now(),
            });
          }
        }, 500);
      }
    });
  }
}

export const chatController = new ChatController();