mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-01 05:14:25 +00:00
feat(chat): support optimistic context display entry locally during message sending
This commit is contained in:
@@ -183,4 +183,72 @@ describe('useChatbotStore', () => {
|
|||||||
expect(finalState.messages[0].id).toBe('user-msg-ulid-old')
|
expect(finalState.messages[0].id).toBe('user-msg-ulid-old')
|
||||||
expect(finalState.messages[1].id).toBe('user-msg-ulid-new')
|
expect(finalState.messages[1].id).toBe('user-msg-ulid-new')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should create optimistic context display message when sending with assets', async () => {
|
||||||
|
// 1. Setup chatAssets in store
|
||||||
|
useChatbotStore.setState({
|
||||||
|
chatAssets: [{ id: 'asset-1', name: 'File 1.txt', type: 'file' }],
|
||||||
|
})
|
||||||
|
|
||||||
|
// 2. Mock POST response with context_display_info entry streaming back
|
||||||
|
const mockEvents = [
|
||||||
|
'data: {"type":"session","sessionId":"sess-123"}\n\n',
|
||||||
|
'data: {"type":"entry","entry":{"id":"context-display-ulid","role":"custom","customType":"context_display_info","details":{"assets":[{"id":"asset-1","name":"File 1.txt","type":"file"}]},"timestamp":120}}\n\n',
|
||||||
|
'data: {"type":"entry","entry":{"id":"user-msg-ulid","role":"user","content":[{"type":"text","text":"hello"}],"timestamp":123}}\n\n',
|
||||||
|
'data: {"type":"done","status":"completed"}\n\n',
|
||||||
|
]
|
||||||
|
|
||||||
|
let eventIdx = 0
|
||||||
|
const mockReader = {
|
||||||
|
read: vi.fn(async () => {
|
||||||
|
if (eventIdx < mockEvents.length) {
|
||||||
|
const value = new TextEncoder().encode(mockEvents[eventIdx++])
|
||||||
|
return { done: false, value }
|
||||||
|
}
|
||||||
|
return { done: true, value: undefined }
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
const mockResponse = {
|
||||||
|
ok: true,
|
||||||
|
headers: {
|
||||||
|
get: vi.fn(() => 'sess-123'),
|
||||||
|
},
|
||||||
|
body: {
|
||||||
|
getReader: () => mockReader,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
vi.mocked(client.api.chat.$post).mockResolvedValue(mockResponse as any)
|
||||||
|
|
||||||
|
const sendPromise = useChatbotStore.getState().sendMessage('hello', 'proj-123')
|
||||||
|
|
||||||
|
// 3. Verify that both the optimistic context message and user message are present immediately
|
||||||
|
const stateWithOptimistic = useChatbotStore.getState()
|
||||||
|
expect(stateWithOptimistic.messages).toHaveLength(2)
|
||||||
|
|
||||||
|
// First message should be the optimistic context_display_info
|
||||||
|
expect(stateWithOptimistic.messages[0].id).toContain('temp-context-')
|
||||||
|
expect(stateWithOptimistic.messages[0].role).toBe('custom')
|
||||||
|
expect((stateWithOptimistic.messages[0] as { customType?: string }).customType).toBe(
|
||||||
|
'context_display_info',
|
||||||
|
)
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
expect((stateWithOptimistic.messages[0] as any).details).toEqual({
|
||||||
|
assets: [{ id: 'asset-1', name: 'File 1.txt', type: 'file' }],
|
||||||
|
})
|
||||||
|
|
||||||
|
// Second message should be the optimistic user message
|
||||||
|
expect(stateWithOptimistic.messages[1].id).toContain('temp-')
|
||||||
|
expect(stateWithOptimistic.messages[1].role).toBe('user')
|
||||||
|
|
||||||
|
await sendPromise
|
||||||
|
|
||||||
|
// 4. Verify they are replaced correctly by the real backend entries
|
||||||
|
const finalState = useChatbotStore.getState()
|
||||||
|
expect(finalState.messages).toHaveLength(2)
|
||||||
|
expect(finalState.messages[0].id).toBe('context-display-ulid')
|
||||||
|
expect(finalState.messages[1].id).toBe('user-msg-ulid')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -136,8 +136,28 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
|
|||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
} as unknown as ChatMessage
|
} as unknown as ChatMessage
|
||||||
|
|
||||||
|
const newMessages = [...state.messages]
|
||||||
|
if (state.chatAssets.length > 0) {
|
||||||
|
const tempContextId = `temp-context-${Date.now()}`
|
||||||
|
const optimisticContextMsg = {
|
||||||
|
id: tempContextId,
|
||||||
|
role: 'custom' as const,
|
||||||
|
customType: 'context_display_info',
|
||||||
|
details: {
|
||||||
|
assets: state.chatAssets.map((a) => ({
|
||||||
|
id: a.id,
|
||||||
|
name: a.name,
|
||||||
|
type: a.type,
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
timestamp: Date.now() - 1,
|
||||||
|
} as unknown as ChatMessage
|
||||||
|
newMessages.push(optimisticContextMsg)
|
||||||
|
}
|
||||||
|
newMessages.push(optimisticMsg)
|
||||||
|
|
||||||
set({
|
set({
|
||||||
messages: [...state.messages, optimisticMsg],
|
messages: newMessages,
|
||||||
isStreaming: true,
|
isStreaming: true,
|
||||||
chatAssets: [],
|
chatAssets: [],
|
||||||
})
|
})
|
||||||
@@ -197,10 +217,20 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
|
|||||||
const entry = data.entry as ChatMessage
|
const entry = data.entry as ChatMessage
|
||||||
set((s) => {
|
set((s) => {
|
||||||
const isExisting = s.messages.some((m) => m.id === entry.id)
|
const isExisting = s.messages.some((m) => m.id === entry.id)
|
||||||
// Replace temp/optimistic message when the real user message streams back
|
// Replace temp/optimistic message when the real counterpart streams back
|
||||||
const filtered = s.messages.filter(
|
const filtered = s.messages.filter((m) => {
|
||||||
(m) => !(entry.role === 'user' && !isExisting && m.id.startsWith('temp-')),
|
if (entry.role === 'user' && !isExisting) {
|
||||||
)
|
return !(m.id.startsWith('temp-') && !m.id.startsWith('temp-context-'))
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
entry.role === 'custom' &&
|
||||||
|
entry.customType === 'context_display_info' &&
|
||||||
|
!isExisting
|
||||||
|
) {
|
||||||
|
return !m.id.startsWith('temp-context-')
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
// Prevent duplicates by ID
|
// Prevent duplicates by ID
|
||||||
const existingIdx = filtered.findIndex((m) => m.id === entry.id)
|
const existingIdx = filtered.findIndex((m) => m.id === entry.id)
|
||||||
|
|||||||
Reference in New Issue
Block a user