File size: 9,084 Bytes
6a50e97
d8e039b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a50e97
 
d8e039b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a50e97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8e039b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a50e97
 
d8e039b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a50e97
d8e039b
 
 
 
 
 
 
 
 
 
 
 
 
 
6a50e97
 
d8e039b
 
 
 
 
 
 
6a50e97
 
 
d8e039b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a50e97
 
 
d8e039b
 
 
 
 
 
 
 
 
 
 
 
6a50e97
 
 
d8e039b
 
 
6a50e97
 
 
 
 
 
 
 
 
d8e039b
 
 
 
 
 
 
6a50e97
d8e039b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a50e97
 
 
d8e039b
 
 
 
 
 
 
 
 
 
 
 
 
6a50e97
 
 
d8e039b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import React, { useState, useEffect, useCallback } from 'react'
import { Message, ChatSession, MessageStatus } from '@/types/chat'
import { chatStorage } from '@/lib/chat-storage'

interface UseChatOptions {
  api_endpoint?: string
  defaultModel?: string
  defaultSystemPrompt?: string
}

interface ApiResponse {
  thinking_content: string
  content: string
  model_used: string
  supports_thinking: boolean
}

export function useChat(options: UseChatOptions = {}) {
  const {
    api_endpoint = `${window.location.origin}/generate`,
    defaultModel = 'Qwen/Qwen3-30B-A3B',
    defaultSystemPrompt = ''
  } = options

  // Chat state
  const [sessions, setSessions] = useState<ChatSession[]>([])
  const [currentSessionId, setCurrentSessionId] = useState<string | null>(null)
  const [input, setInput] = useState('')
  const [status, setStatus] = useState<MessageStatus>({
    isLoading: false,
    error: null
  })

  // Model settings
  const [selectedModel, setSelectedModel] = useState(defaultModel)
  const [systemPrompt, setSystemPrompt] = useState(defaultSystemPrompt)
  const [temperature, setTemperature] = useState(0.7)
  const [maxTokens, setMaxTokens] = useState(1024)

  // Current session - add dependency on sessions to force re-render
  const currentSession = React.useMemo(() => {
    const session = sessions.find((s: any) => s.id === currentSessionId) || null
    console.log('useChat - currentSession updated:', { 
      sessionId: currentSessionId, 
      found: !!session, 
      messageCount: session?.messages?.length || 0 
    })
    return session
  }, [sessions, currentSessionId])
  
  const messages = React.useMemo(() => {
    const msgs = currentSession?.messages || []
    console.log('useChat - messages computed:', msgs.length, msgs.map(m => ({ id: m.id, role: m.role })))
    return msgs
  }, [currentSession?.messages])

  // Load sessions on mount
  useEffect(() => {
    const loadedSessions = chatStorage.getAllSessions()
    setSessions(loadedSessions)
    
    const currentSession = chatStorage.getCurrentSession()
    if (currentSession) {
      setCurrentSessionId(currentSession.id)
    } else if (loadedSessions.length > 0) {
      setCurrentSessionId(loadedSessions[0].id)
      chatStorage.setCurrentSession(loadedSessions[0].id)
    }
  }, [])



  // Create new session
  const createNewSession = useCallback(() => {
    const newSession = chatStorage.createSession(
      undefined, // Auto-generate title
      selectedModel,
      systemPrompt
    )
    
    // Update React state with all sessions from localStorage
    const updatedSessions = chatStorage.getAllSessions()
    setSessions([...updatedSessions]) // Force update with new array reference
    setCurrentSessionId(newSession.id)
    chatStorage.setCurrentSession(newSession.id)
    
    return newSession.id
  }, [selectedModel, systemPrompt])

  // Switch to session
  const selectSession = useCallback((sessionId: string) => {
    setCurrentSessionId(sessionId)
    chatStorage.setCurrentSession(sessionId)
  }, [])

  // Delete session
  const deleteSession = useCallback((sessionId: string) => {
    chatStorage.deleteSession(sessionId)
    const updatedSessions = chatStorage.getAllSessions()
    setSessions([...updatedSessions]) // Force update with new array reference
    
    if (currentSessionId === sessionId) {
      if (updatedSessions.length > 0) {
        setCurrentSessionId(updatedSessions[0].id)
        chatStorage.setCurrentSession(updatedSessions[0].id)
      } else {
        setCurrentSessionId(null)
      }
    }
  }, [currentSessionId])

  // Rename session
  const renameSession = useCallback((sessionId: string, newTitle: string) => {
    chatStorage.updateSession(sessionId, { title: newTitle })
    const updatedSessions = chatStorage.getAllSessions()
    setSessions([...updatedSessions]) // Force update with new array reference
  }, [])

  // Add message to current session
  const addMessage = useCallback((message: Omit<Message, 'id' | 'timestamp'>) => {
    if (!currentSessionId) return

    chatStorage.addMessageToSession(currentSessionId, message)
    // Force update with new array reference
    const updatedSessions = chatStorage.getAllSessions()
    setSessions([...updatedSessions])
  }, [currentSessionId])

  // Send message
  const sendMessage = useCallback(async () => {
    if (!input.trim() || status.isLoading) return

    let sessionId = currentSessionId

    // Create new session if none exists
    if (!sessionId) {
      sessionId = createNewSession()
    }

    const userMessage = input.trim()
    setInput('')
    setStatus({ isLoading: true, error: null })

    // Add user message directly to the specific session
    if (sessionId) {
      chatStorage.addMessageToSession(sessionId, {
        role: 'user',
        content: userMessage
      })
      // Force update sessions state with fresh data
      const updatedSessions = chatStorage.getAllSessions()
      setSessions([...updatedSessions]) // Create new array reference to force re-render
    }

    // Add system message if system prompt is set
    // Check actual session messages from storage, not React state
    const actualSession = chatStorage.getSession(sessionId!)
    const hasMessages = actualSession?.messages && actualSession.messages.length > 1 // >1 because user message was just added
    
    if (systemPrompt && !hasMessages && sessionId) {
      chatStorage.addMessageToSession(sessionId, {
        role: 'system',
        content: systemPrompt
      })
      // Force update sessions state with fresh data
      const updatedSessions = chatStorage.getAllSessions()
      setSessions([...updatedSessions]) // Create new array reference to force re-render
    }

    try {
      // Get conversation history (excluding system messages for the API)
      const actualSession = chatStorage.getSession(sessionId!)
      const conversationHistory = actualSession?.messages
        ?.filter((msg: any) => msg.role !== 'system')
        ?.map((msg: any) => ({
          role: msg.role,
          content: msg.content
        })) || []

      const response = await fetch(api_endpoint, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          prompt: userMessage,
          messages: conversationHistory,
          system_prompt: systemPrompt || null,
          model_name: selectedModel,
          temperature,
          max_new_tokens: maxTokens
        }),
      })

      if (!response.ok) {
        const errorData = await response.json()
        throw new Error(errorData.detail || `HTTP error! status: ${response.status}`)
      }

      const data: ApiResponse = await response.json()

      // Add assistant message directly to the specific session
      if (sessionId) {
        chatStorage.addMessageToSession(sessionId, {
          role: 'assistant',
          content: data.content,
          thinking_content: data.thinking_content,
          model_used: data.model_used,
          supports_thinking: data.supports_thinking
        })
        // Force update sessions state with fresh data
        const updatedSessions = chatStorage.getAllSessions()
        setSessions([...updatedSessions]) // Create new array reference to force re-render
      }

      setStatus({ isLoading: false, error: null })
    } catch (error) {
      const errorMessage = error instanceof Error ? error.message : 'An error occurred'
      setStatus({ isLoading: false, error: errorMessage })
      
      // Add error message directly to the specific session
      if (sessionId) {
        chatStorage.addMessageToSession(sessionId, {
          role: 'assistant',
          content: `Sorry, I encountered an error: ${errorMessage}`
        })
        // Force update sessions state with fresh data
        const updatedSessions = chatStorage.getAllSessions()
        setSessions([...updatedSessions]) // Create new array reference to force re-render
      }
    }
  }, [
    input,
    status.isLoading,
    currentSessionId,
    createNewSession,
    addMessage,
    systemPrompt,
    messages.length,
    api_endpoint,
    selectedModel,
    temperature,
    maxTokens
  ])

  // Stop generation (placeholder for future implementation)
  const stopGeneration = useCallback(() => {
    setStatus({ isLoading: false, error: null })
  }, [])

  // Clear all sessions
  const clearAllSessions = useCallback(() => {
    chatStorage.clear()
    setSessions([])
    setCurrentSessionId(null)
  }, [])

  return {
    // Session management
    sessions,
    currentSession,
    currentSessionId,
    createNewSession,
    selectSession,
    deleteSession,
    renameSession,
    clearAllSessions,

    // Messages
    messages,
    input,
    setInput,

    // Chat actions
    sendMessage,
    stopGeneration,

    // Status
    isLoading: status.isLoading,
    error: status.error,

    // Model settings
    selectedModel,
    setSelectedModel,
    systemPrompt,
    setSystemPrompt,
    temperature,
    setTemperature,
    maxTokens,
    setMaxTokens
  }
}