feat: add file upload support

This commit is contained in:
weishu
2026-01-17 19:38:12 +08:00
parent 940d7b8233
commit 53e4de6684
28 changed files with 1018 additions and 49 deletions
+14 -5
View File
@@ -1,6 +1,6 @@
import { useMutation } from '@tanstack/react-query'
import type { ApiClient } from '@/api/client'
import type { DecryptedMessage } from '@/types/api'
import type { AttachmentMetadata, DecryptedMessage } from '@/types/api'
import { makeClientSideId } from '@/lib/messages'
import {
appendOptimisticMessage,
@@ -14,6 +14,7 @@ type SendMessageInput = {
text: string
localId: string
createdAt: number
attachments?: AttachmentMetadata[]
}
function findMessageByLocalId(
@@ -31,7 +32,7 @@ function findMessageByLocalId(
}
export function useSendMessage(api: ApiClient | null, sessionId: string | null): {
sendMessage: (text: string) => void
sendMessage: (text: string, attachments?: AttachmentMetadata[]) => void
retryMessage: (localId: string) => void
isSending: boolean
} {
@@ -42,14 +43,21 @@ export function useSendMessage(api: ApiClient | null, sessionId: string | null):
if (!api) {
throw new Error('API unavailable')
}
await api.sendMessage(input.sessionId, input.text, input.localId)
await api.sendMessage(input.sessionId, input.text, input.localId, input.attachments)
},
onMutate: async (input) => {
const optimisticMessage: DecryptedMessage = {
id: input.localId,
seq: null,
localId: input.localId,
content: { role: 'user', content: input.text },
content: {
role: 'user',
content: {
type: 'text',
text: input.text,
attachments: input.attachments
}
},
createdAt: input.createdAt,
status: 'sending',
originalText: input.text,
@@ -67,7 +75,7 @@ export function useSendMessage(api: ApiClient | null, sessionId: string | null):
},
})
const sendMessage = (text: string) => {
const sendMessage = (text: string, attachments?: AttachmentMetadata[]) => {
if (!api || !sessionId) return
if (mutation.isPending) return
const localId = makeClientSideId('local')
@@ -76,6 +84,7 @@ export function useSendMessage(api: ApiClient | null, sessionId: string | null):
text,
localId,
createdAt: Date.now(),
attachments,
})
}