Spaces:
Sleeping
Sleeping
File size: 9,531 Bytes
f871fed |
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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
'use client'
import { useState, useCallback, useEffect } from 'react'
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { chatApi } from '@/lib/api/chat'
import { QUERY_KEYS } from '@/lib/api/query-client'
import {
NotebookChatMessage,
CreateNotebookChatSessionRequest,
UpdateNotebookChatSessionRequest,
SourceListResponse,
NoteResponse
} from '@/lib/types/api'
import { ContextSelections } from '@/app/(dashboard)/notebooks/[id]/page'
interface UseNotebookChatParams {
notebookId: string
sources: SourceListResponse[]
notes: NoteResponse[]
contextSelections: ContextSelections
}
export function useNotebookChat({ notebookId, sources, notes, contextSelections }: UseNotebookChatParams) {
const queryClient = useQueryClient()
const [currentSessionId, setCurrentSessionId] = useState<string | null>(null)
const [messages, setMessages] = useState<NotebookChatMessage[]>([])
const [isSending, setIsSending] = useState(false)
const [tokenCount, setTokenCount] = useState<number>(0)
const [charCount, setCharCount] = useState<number>(0)
// Pending model override for when user changes model before a session exists
const [pendingModelOverride, setPendingModelOverride] = useState<string | null>(null)
// Fetch sessions for this notebook
const {
data: sessions = [],
isLoading: loadingSessions,
refetch: refetchSessions
} = useQuery({
queryKey: QUERY_KEYS.notebookChatSessions(notebookId),
queryFn: () => chatApi.listSessions(notebookId),
enabled: !!notebookId
})
// Fetch current session with messages
const {
data: currentSession,
refetch: refetchCurrentSession
} = useQuery({
queryKey: QUERY_KEYS.notebookChatSession(currentSessionId!),
queryFn: () => chatApi.getSession(currentSessionId!),
enabled: !!notebookId && !!currentSessionId
})
// Update messages when current session changes
useEffect(() => {
if (currentSession?.messages) {
setMessages(currentSession.messages)
}
}, [currentSession])
// Auto-select most recent session when sessions are loaded
useEffect(() => {
if (sessions.length > 0 && !currentSessionId) {
// Sessions are sorted by created date desc from API
const mostRecentSession = sessions[0]
setCurrentSessionId(mostRecentSession.id)
}
}, [sessions, currentSessionId])
// Create session mutation
const createSessionMutation = useMutation({
mutationFn: (data: CreateNotebookChatSessionRequest) =>
chatApi.createSession(data),
onSuccess: (newSession) => {
queryClient.invalidateQueries({
queryKey: QUERY_KEYS.notebookChatSessions(notebookId)
})
setCurrentSessionId(newSession.id)
toast.success('Chat session created')
},
onError: () => {
toast.error('Failed to create chat session')
}
})
// Update session mutation
const updateSessionMutation = useMutation({
mutationFn: ({ sessionId, data }: {
sessionId: string
data: UpdateNotebookChatSessionRequest
}) => chatApi.updateSession(sessionId, data),
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: QUERY_KEYS.notebookChatSessions(notebookId)
})
queryClient.invalidateQueries({
queryKey: QUERY_KEYS.notebookChatSession(currentSessionId!)
})
toast.success('Session updated')
},
onError: () => {
toast.error('Failed to update session')
}
})
// Delete session mutation
const deleteSessionMutation = useMutation({
mutationFn: (sessionId: string) =>
chatApi.deleteSession(sessionId),
onSuccess: (_, deletedId) => {
queryClient.invalidateQueries({
queryKey: QUERY_KEYS.notebookChatSessions(notebookId)
})
if (currentSessionId === deletedId) {
setCurrentSessionId(null)
setMessages([])
}
toast.success('Session deleted')
},
onError: () => {
toast.error('Failed to delete session')
}
})
// Build context from sources and notes based on user selections
const buildContext = useCallback(async () => {
// Build context_config mapping IDs to selection modes
const context_config: { sources: Record<string, string>, notes: Record<string, string> } = {
sources: {},
notes: {}
}
// Map source selections
sources.forEach(source => {
const mode = contextSelections.sources[source.id]
if (mode === 'insights') {
context_config.sources[source.id] = 'insights'
} else if (mode === 'full') {
context_config.sources[source.id] = 'full content'
} else {
context_config.sources[source.id] = 'not in'
}
})
// Map note selections
notes.forEach(note => {
const mode = contextSelections.notes[note.id]
if (mode === 'full') {
context_config.notes[note.id] = 'full content'
} else {
context_config.notes[note.id] = 'not in'
}
})
// Call API to build context with actual content
const response = await chatApi.buildContext({
notebook_id: notebookId,
context_config
})
// Store token and char counts
setTokenCount(response.token_count)
setCharCount(response.char_count)
return response.context
}, [notebookId, sources, notes, contextSelections])
// Send message (synchronous, no streaming)
const sendMessage = useCallback(async (message: string, modelOverride?: string) => {
let sessionId = currentSessionId
// Auto-create session if none exists
if (!sessionId) {
try {
const defaultTitle = message.length > 30
? `${message.substring(0, 30)}...`
: message
const newSession = await chatApi.createSession({
notebook_id: notebookId,
title: defaultTitle,
// Include pending model override when creating session
model_override: pendingModelOverride ?? undefined
})
sessionId = newSession.id
setCurrentSessionId(sessionId)
// Clear pending model override now that it's applied to the session
setPendingModelOverride(null)
queryClient.invalidateQueries({
queryKey: QUERY_KEYS.notebookChatSessions(notebookId)
})
} catch {
toast.error('Failed to create chat session')
return
}
}
// Add user message optimistically
const userMessage: NotebookChatMessage = {
id: `temp-${Date.now()}`,
type: 'human',
content: message,
timestamp: new Date().toISOString()
}
setMessages(prev => [...prev, userMessage])
setIsSending(true)
try {
// Build context and send message
const context = await buildContext()
const response = await chatApi.sendMessage({
session_id: sessionId,
message,
context,
model_override: modelOverride ?? (currentSession?.model_override ?? undefined)
})
// Update messages with API response
setMessages(response.messages)
// Refetch current session to get updated data
await refetchCurrentSession()
} catch (error) {
console.error('Error sending message:', error)
toast.error('Failed to send message')
// Remove optimistic message on error
setMessages(prev => prev.filter(msg => !msg.id.startsWith('temp-')))
} finally {
setIsSending(false)
}
}, [
notebookId,
currentSessionId,
currentSession,
pendingModelOverride,
buildContext,
refetchCurrentSession,
queryClient
])
// Switch session
const switchSession = useCallback((sessionId: string) => {
setCurrentSessionId(sessionId)
}, [])
// Create session
const createSession = useCallback((title?: string) => {
return createSessionMutation.mutate({
notebook_id: notebookId,
title
})
}, [createSessionMutation, notebookId])
// Update session
const updateSession = useCallback((sessionId: string, data: UpdateNotebookChatSessionRequest) => {
return updateSessionMutation.mutate({
sessionId,
data
})
}, [updateSessionMutation])
// Delete session
const deleteSession = useCallback((sessionId: string) => {
return deleteSessionMutation.mutate(sessionId)
}, [deleteSessionMutation])
// Set model override - handles both existing sessions and pending state
const setModelOverride = useCallback((model: string | null) => {
if (currentSessionId) {
// Session exists - update it directly
updateSessionMutation.mutate({
sessionId: currentSessionId,
data: { model_override: model }
})
} else {
// No session yet - store as pending
setPendingModelOverride(model)
}
}, [currentSessionId, updateSessionMutation])
// Update token/char counts when context selections change
useEffect(() => {
const updateContextCounts = async () => {
try {
await buildContext()
} catch (error) {
console.error('Error updating context counts:', error)
}
}
updateContextCounts()
}, [buildContext])
return {
// State
sessions,
currentSession: currentSession || sessions.find(s => s.id === currentSessionId),
currentSessionId,
messages,
isSending,
loadingSessions,
tokenCount,
charCount,
pendingModelOverride,
// Actions
createSession,
updateSession,
deleteSession,
switchSession,
sendMessage,
setModelOverride,
refetchSessions
}
}
|