File size: 3,491 Bytes
6a059d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**

 * Chat API Utilities

 * Handles session management and file uploads

 */

export async function createNewSession(

    firstMessage: string | undefined,

    API_BASE_URL: string,

    getAuthHeaders: () => Record<string, string>,

    setCurrentSessionId: (id: string) => void,

    setMessages: (messages: any[]) => void,

    loadChatHistory: () => void

): Promise<string | null> {
    // Generate title from first message if provided
    let title = "New Chat"
    if (firstMessage) {
        const content = firstMessage.trim()
        if (content.length <= 50) {
            title = content
        } else {
            title = content.substring(0, 50).split(' ').slice(0, -1).join(' ') + "..."
        }
    }

    try {
        const headers: Record<string, string> = {
            "Content-Type": "application/json",
            ...getAuthHeaders()
        }
        const response = await fetch(`${API_BASE_URL}/api/v1/chat/sessions`, {
            method: "POST",
            headers,
            body: JSON.stringify({ title })
        })
        if (response.ok) {
            const session = await response.json()
            setCurrentSessionId(session.id)
            setMessages([])
            loadChatHistory()
            return session.id
        }
    } catch (error) {
        console.error("Failed to create session:", error)
    }
    return null
}

export async function loadSession(

    sessionId: string,

    API_BASE_URL: string,

    getAuthHeaders: () => Record<string, string>,

    setMessages: (messages: any[]) => void,

    setCurrentSessionId: (id: string) => void,

    setShowHistory: (show: boolean) => void

): Promise<void> {
    try {
        const headers: Record<string, string> = {
            "Content-Type": "application/json",
            ...getAuthHeaders()
        }
        const response = await fetch(`${API_BASE_URL}/api/v1/chat/sessions/${sessionId}/messages`, {
            headers
        })
        if (response.ok) {
            const sessionMessages = await response.json()
            setMessages(sessionMessages)
            setCurrentSessionId(sessionId)
            setShowHistory(false)
        }
    } catch (error) {
        console.error("Failed to load session:", error)
    }
}

export async function uploadFiles(

    sessionId: string,

    files: File[],

    API_BASE_URL: string,

    getAuthHeaders: () => Record<string, string>,

    setUploadingFiles: (uploading: boolean) => void

): Promise<string[]> {
    const attachmentIds: string[] = []
    setUploadingFiles(true)

    try {
        for (const file of files) {
            const formData = new FormData()
            formData.append("file", file)

            const headers: Record<string, string> = {
                ...getAuthHeaders()
            }
            const response = await fetch(`${API_BASE_URL}/api/v1/chat/sessions/${sessionId}/attachments`, {
                method: "POST",
                headers,
                body: formData,
            })

            if (response.ok) {
                const data = await response.json()
                attachmentIds.push(data.attachment.id)
            } else {
                const error = await response.json()
                throw new Error(error.detail || "Failed to upload file")
            }
        }
    } finally {
        setUploadingFiles(false)
    }

    return attachmentIds
}