mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 22:14:50 +00:00
feat(chat): support markups, attachments, and timestamps in agent 1-to-1 chat (#368)
* feat(chat): support markups, attachments, and timestamps in agent 1-to-1 chat * feat(chat): improve message card layout with inline badges and integrated assets * fix(chat): enable image attachment thumbnails in 1-to-1 chat cards * fix(chat): address code review feedback on input state, mentions, i18n, and session deletion * fix(chat): replace ring with border color on selected message card
This commit is contained in:
@@ -181,9 +181,7 @@ describe('useChatbotStore', () => {
|
||||
const stateWithOptimistic = useChatbotStore.getState()
|
||||
expect(stateWithOptimistic.messages).toHaveLength(1)
|
||||
expect(stateWithOptimistic.messages[0].id).toContain('temp-')
|
||||
expect((stateWithOptimistic.messages[0] as { content: unknown }).content).toEqual([
|
||||
{ type: 'text', text: 'hello' },
|
||||
])
|
||||
expect((stateWithOptimistic.messages[0] as { content: unknown }).content).toBe('hello')
|
||||
expect(stateWithOptimistic.isStreaming).toBe(true)
|
||||
|
||||
await sendPromise
|
||||
@@ -301,28 +299,24 @@ describe('useChatbotStore', () => {
|
||||
|
||||
const sendPromise = useChatbotStore.getState().sendMessage('team-123', 'hello', 'proj-123')
|
||||
|
||||
// 3. Verify that both the optimistic context message and user message are present immediately
|
||||
// 3. Verify that the optimistic message includes referencedAssets
|
||||
const stateWithOptimistic = useChatbotStore.getState()
|
||||
expect(stateWithOptimistic.messages).toHaveLength(2)
|
||||
expect(stateWithOptimistic.messages).toHaveLength(1)
|
||||
|
||||
// First message should be the optimistic context_display_info
|
||||
expect(stateWithOptimistic.messages[0].id).toContain('temp-context-')
|
||||
// Message should be the optimistic shumai_message with referencedAssets in details
|
||||
expect(stateWithOptimistic.messages[0].id).toContain('temp-')
|
||||
expect(stateWithOptimistic.messages[0].role).toBe('custom')
|
||||
expect((stateWithOptimistic.messages[0] as { customType?: string }).customType).toBe(
|
||||
'context_display_info',
|
||||
'shumai_message',
|
||||
)
|
||||
// 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')
|
||||
expect((stateWithOptimistic.messages[0] as any).details.referencedAssets).toEqual([
|
||||
{ id: 'asset-1', name: 'File 1.txt', type: 'file' },
|
||||
])
|
||||
|
||||
await sendPromise
|
||||
|
||||
// 4. Verify they are replaced correctly by the real backend entries
|
||||
// 4. Verify message list after streaming completes
|
||||
const finalState = useChatbotStore.getState()
|
||||
expect(finalState.messages).toHaveLength(2)
|
||||
expect(finalState.messages[0].id).toBe('context-display-ulid')
|
||||
@@ -418,4 +412,93 @@ describe('useChatbotStore', () => {
|
||||
expect((finalState.messages[0] as { customType?: string }).customType).toBe('shumai_message')
|
||||
expect(finalState.messages[1].id).toBe('assistant-msg-ulid')
|
||||
})
|
||||
|
||||
it('should include attachments, annotations, and second in optimistic message and API call', async () => {
|
||||
const mockEvents = [
|
||||
'data: {"type":"session","sessionId":"sess-456"}\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-456'),
|
||||
},
|
||||
body: {
|
||||
getReader: () => mockReader,
|
||||
},
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
vi.mocked(client.api.teams[':teamId'].chat.$post).mockResolvedValue(mockResponse as any)
|
||||
|
||||
const sendPromise = useChatbotStore.getState().sendMessage(
|
||||
'team-123',
|
||||
'Look at this frame',
|
||||
'proj-123',
|
||||
'file-asset-1',
|
||||
undefined,
|
||||
['att-1'],
|
||||
[
|
||||
{
|
||||
type: 'box',
|
||||
color: '#ff0000',
|
||||
points: [
|
||||
[10, 20],
|
||||
[30, 40],
|
||||
],
|
||||
},
|
||||
],
|
||||
12.5,
|
||||
[{ id: 'att-1', name: 'screenshot.png', type: 'image', mediaType: 'image/png' }],
|
||||
)
|
||||
|
||||
// Check optimistic message has annotations, second position, and attachedFiles
|
||||
const optimisticState = useChatbotStore.getState()
|
||||
expect(optimisticState.messages).toHaveLength(1)
|
||||
const optMsg = optimisticState.messages[0] as unknown as Record<string, unknown>
|
||||
expect(optMsg.customType).toBe('shumai_message')
|
||||
const details = optMsg.details as Record<string, unknown>
|
||||
expect(details.annotation).toBe(true)
|
||||
expect(details.annotations).toHaveLength(1)
|
||||
expect(details.position).toEqual({ type: 'time', seconds: 12.5 })
|
||||
expect(details.attachedFiles).toEqual([
|
||||
{ id: 'att-1', name: 'screenshot.png', type: 'image', mediaType: 'image/png' },
|
||||
])
|
||||
|
||||
await sendPromise
|
||||
|
||||
expect(client.api.teams[':teamId'].chat.$post).toHaveBeenCalledWith({
|
||||
param: { teamId: 'team-123' },
|
||||
json: {
|
||||
agentId: 'agent-1',
|
||||
textPrompt: 'Look at this frame',
|
||||
attachedFiles: ['att-1'],
|
||||
assetIds: undefined,
|
||||
sessionId: undefined,
|
||||
contextAssetId: 'file-asset-1',
|
||||
projectId: 'proj-123',
|
||||
second: 12.5,
|
||||
annotations: [
|
||||
{
|
||||
type: 'box',
|
||||
color: '#ff0000',
|
||||
points: [
|
||||
[10, 20],
|
||||
[30, 40],
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { create } from 'zustand'
|
||||
import { client } from '@/ui/api/client'
|
||||
import type { ChatMessage, ChatSessionInfo } from '@shumai/dtos'
|
||||
import type { Annotation } from '@/ui/types'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
export const AGENT_PREFERENCE_STORAGE_KEY = 'shumai_selected_agent_id'
|
||||
@@ -18,6 +19,15 @@ export interface ChatAsset {
|
||||
type: 'file' | 'folder'
|
||||
}
|
||||
|
||||
export interface AttachedFileMeta {
|
||||
id: string
|
||||
name: string
|
||||
type: string
|
||||
url?: string
|
||||
mediaType?: string
|
||||
mimeType?: string
|
||||
}
|
||||
|
||||
interface ChatbotState {
|
||||
isChatbotOpen: boolean
|
||||
setIsChatbotOpen: (open: boolean) => void
|
||||
@@ -54,6 +64,10 @@ interface ChatbotState {
|
||||
projectId: string,
|
||||
contextAssetId?: string,
|
||||
onAssetMutation?: () => void,
|
||||
attachmentIds?: string[],
|
||||
annotations?: Annotation[],
|
||||
second?: number | null,
|
||||
attachedFilesMeta?: AttachedFileMeta[],
|
||||
) => Promise<void>
|
||||
abortActiveSession: (teamId: string) => Promise<void>
|
||||
}
|
||||
@@ -177,37 +191,43 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
|
||||
})
|
||||
},
|
||||
|
||||
sendMessage: async (teamId, text, projectId, contextAssetId, onAssetMutation) => {
|
||||
sendMessage: async (
|
||||
teamId,
|
||||
text,
|
||||
projectId,
|
||||
contextAssetId,
|
||||
onAssetMutation,
|
||||
attachmentIds,
|
||||
annotations,
|
||||
second,
|
||||
attachedFilesMeta,
|
||||
) => {
|
||||
const state = useChatbotStore.getState()
|
||||
if (state.isStreaming) return
|
||||
|
||||
const tempId = `temp-${Date.now()}`
|
||||
const optimisticMsg = {
|
||||
id: tempId,
|
||||
role: 'user' as const,
|
||||
content: [{ type: 'text', text }],
|
||||
role: 'custom' as const,
|
||||
customType: 'shumai_message',
|
||||
content: text,
|
||||
display: true,
|
||||
details: {
|
||||
currentAsset: contextAssetId ? { id: contextAssetId, name: '', type: 'file' } : undefined,
|
||||
position:
|
||||
second !== undefined && second !== null ? { type: 'time', seconds: second } : undefined,
|
||||
annotation: !!(annotations && annotations.length > 0),
|
||||
annotations: annotations || undefined,
|
||||
attachedFiles: attachedFilesMeta || undefined,
|
||||
referencedAssets:
|
||||
state.chatAssets.length > 0
|
||||
? state.chatAssets.map((a) => ({ id: a.id, name: a.name, type: a.type }))
|
||||
: undefined,
|
||||
},
|
||||
timestamp: Date.now(),
|
||||
} 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)
|
||||
const newMessages = [...state.messages, optimisticMsg]
|
||||
|
||||
set({
|
||||
messages: newMessages,
|
||||
@@ -229,11 +249,17 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
|
||||
param: { teamId },
|
||||
json: {
|
||||
agentId,
|
||||
textPrompt: text,
|
||||
assetIds,
|
||||
textPrompt: text || undefined,
|
||||
attachedFiles: attachmentIds && attachmentIds.length > 0 ? attachmentIds : undefined,
|
||||
assetIds: assetIds && assetIds.length > 0 ? assetIds : undefined,
|
||||
sessionId: state.currentSessionId || undefined,
|
||||
contextAssetId,
|
||||
projectId,
|
||||
second: second ?? undefined,
|
||||
annotations:
|
||||
annotations && annotations.length > 0
|
||||
? (annotations as unknown as Record<string, unknown>[])
|
||||
: undefined,
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user