File size: 2,892 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
import { writable, derived } from "svelte/store";
import { type ChatMessage, type MessageSegment } from "../models/chat-data";

interface SimpleChatState {
  messages: ChatMessage[];
  connected: boolean;
  processing: boolean;
  error: string | null;
  typingIndicator: boolean;
}

function createChatStore() {
  const initialState: SimpleChatState = {
    messages: [],
    connected: false,
    processing: false,
    error: null,
    typingIndicator: false,
  };

  const { subscribe, update } = writable<SimpleChatState>(initialState);

  return {
    subscribe,

    setConnected(connected: boolean) {
      update((state) => ({ ...state, connected }));
    },

    setProcessing(processing: boolean) {
      update((state) => ({ ...state, processing }));
    },

    setError(error: string | null) {
      update((state) => ({ ...state, error }));
    },

    addMessage(message: ChatMessage) {
      update((state) => ({
        ...state,
        messages: [...state.messages, message],
      }));
    },

    appendToLastMessage(content: string) {
      update((state) => {
        if (state.messages.length === 0) return state;
        const messages = [...state.messages];
        const lastMessage = messages[messages.length - 1];
        messages[messages.length - 1] = {
          ...lastMessage,
          content: lastMessage.content + content,
        };
        return { ...state, messages };
      });
    },

    setLastMessageContent(content: string) {
      update((state) => {
        if (state.messages.length === 0) return state;
        const messages = [...state.messages];
        const lastMessage = messages[messages.length - 1];
        messages[messages.length - 1] = {
          ...lastMessage,
          content,
        };
        return { ...state, messages };
      });
    },

    setLastMessageSegments(segments: MessageSegment[]) {
      update((state) => {
        if (state.messages.length === 0) return state;
        const messages = [...state.messages];
        const lastMessage = messages[messages.length - 1];
        messages[messages.length - 1] = {
          ...lastMessage,
          segments,
        };
        return { ...state, messages };
      });
    },

    clearMessages() {
      update((state) => ({
        ...state,
        messages: [],
        error: null,
      }));
    },

    setTypingIndicator(typingIndicator: boolean) {
      update((state) => ({ ...state, typingIndicator }));
    },
  };
}

export const chatStore = createChatStore();

export const isProcessing = derived(chatStore, ($chat) => $chat.processing);
export const isConnected = derived(chatStore, ($chat) => $chat.connected);
export const messages = derived(chatStore, ($chat) => $chat.messages);
export const error = derived(chatStore, ($chat) => $chat.error);
export const typingIndicator = derived(
  chatStore,
  ($chat) => $chat.typingIndicator,
);