mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 12:24:39 +00:00
feat(agent): refactor message context into unified shumai_message (#365)
* feat(agent): refactor message context into unified shumai_message * fix(agent): persist custom_message entry type in DatabaseSessionStorage and handle message mapping defensively * fix(agent): pass clean prompt to harness and replace optimistic messages on shumai_message stream * fix(agent): align asset context DTO contract, sync comment attachments, and fix session stats
This commit is contained in:
@@ -613,6 +613,53 @@ describe('ChatService', () => {
|
||||
expect(mapEntryToMessage(leafRecord)).toBeNull()
|
||||
})
|
||||
|
||||
it('should map shumai_message custom_message entries with clean text and structured details', () => {
|
||||
const entryRecord = {
|
||||
id: 'entry-shumai-msg',
|
||||
type: 'custom_message',
|
||||
parentId: null,
|
||||
createdAt: new Date('2026-07-07T00:05:00.000Z'),
|
||||
data: {
|
||||
customType: 'shumai_message',
|
||||
content: 'Please adjust color grading',
|
||||
display: true,
|
||||
details: {
|
||||
user: { id: 'u1', name: 'Alice', role: 'editor' },
|
||||
currentAsset: { id: 'a1', name: 'video.mp4', type: 'file' },
|
||||
position: { type: 'time', seconds: 12.5 },
|
||||
},
|
||||
},
|
||||
} as unknown as Parameters<typeof mapEntryToMessage>[0]
|
||||
|
||||
const msg = mapEntryToMessage(entryRecord)
|
||||
expect(msg).toBeDefined()
|
||||
expect(msg?.id).toBe('entry-shumai-msg')
|
||||
expect(msg?.role).toBe('custom')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((msg as any)?.customType).toBe('shumai_message')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((msg as any)?.content).toBe('Please adjust color grading')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((msg as any)?.details?.user?.name).toBe('Alice')
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((msg as any)?.details?.position?.seconds).toBe(12.5)
|
||||
})
|
||||
|
||||
it('should skip legacy context custom_message in mapEntryToMessage', () => {
|
||||
const entryRecord = {
|
||||
id: 'entry-legacy-context',
|
||||
type: 'custom_message',
|
||||
parentId: null,
|
||||
createdAt: new Date('2026-07-07T00:05:00.000Z'),
|
||||
data: {
|
||||
customType: 'context',
|
||||
content: 'System context prompt...',
|
||||
},
|
||||
} as unknown as Parameters<typeof mapEntryToMessage>[0]
|
||||
|
||||
expect(mapEntryToMessage(entryRecord)).toBeNull()
|
||||
})
|
||||
|
||||
describe('getNewSessionMessages', () => {
|
||||
it('returns new messages and lastEntryId', async () => {
|
||||
const { user, team, project } = await setupBasicData()
|
||||
@@ -900,5 +947,46 @@ describe('ChatService', () => {
|
||||
}),
|
||||
).rejects.toThrow(/Insufficient role/)
|
||||
})
|
||||
|
||||
it('safely maps entries without message object or with customType shumai_message', () => {
|
||||
// 1. Entry with type: 'message' but customType: 'shumai_message'
|
||||
const legacyEntry = {
|
||||
id: 'legacy-1',
|
||||
sessionId: 'sess-1',
|
||||
assetId: null,
|
||||
parentId: null,
|
||||
type: 'message',
|
||||
data: {
|
||||
customType: 'shumai_message',
|
||||
content: 'Hello legacy',
|
||||
display: true,
|
||||
details: { user: { id: 'u1', name: 'User 1' } },
|
||||
},
|
||||
createdAt: new Date(),
|
||||
}
|
||||
|
||||
const msg1 = mapEntryToMessage(legacyEntry)
|
||||
expect(msg1).toBeDefined()
|
||||
expect(msg1?.role).toBe('custom')
|
||||
if (msg1 && msg1.role === 'custom') {
|
||||
expect(msg1.customType).toBe('shumai_message')
|
||||
expect(msg1.content).toBe('Hello legacy')
|
||||
expect(msg1.details).toEqual({ user: { id: 'u1', name: 'User 1' } })
|
||||
}
|
||||
|
||||
// 2. Entry with type: 'message' but empty data / no message
|
||||
const emptyEntry = {
|
||||
id: 'empty-1',
|
||||
sessionId: 'sess-1',
|
||||
assetId: null,
|
||||
parentId: null,
|
||||
type: 'message',
|
||||
data: {},
|
||||
createdAt: new Date(),
|
||||
}
|
||||
|
||||
const msg2 = mapEntryToMessage(emptyEntry)
|
||||
expect(msg2).toBeNull()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -30,11 +30,31 @@ export function buildSessionMessages(pathEntries: PathEntry[]): ChatMessage[] {
|
||||
const appendMessage = (entry: PathEntry) => {
|
||||
const timestampMs = new Date(entry.timestamp).getTime()
|
||||
if (entry.type === 'message') {
|
||||
messages.push({
|
||||
...entry.message,
|
||||
id: entry.id,
|
||||
timestamp: entry.message.timestamp || timestampMs,
|
||||
} as unknown as ChatMessage)
|
||||
if ((entry as unknown as { customType?: string }).customType === 'shumai_message') {
|
||||
const customEntry = entry as unknown as {
|
||||
customType: string
|
||||
content: string
|
||||
display?: boolean
|
||||
details?: Record<string, unknown>
|
||||
}
|
||||
messages.push({
|
||||
id: entry.id,
|
||||
role: 'custom',
|
||||
customType: customEntry.customType,
|
||||
content: customEntry.content,
|
||||
display: customEntry.display,
|
||||
details: customEntry.details,
|
||||
timestamp: timestampMs,
|
||||
} as unknown as ChatMessage)
|
||||
return
|
||||
}
|
||||
if (entry.message) {
|
||||
messages.push({
|
||||
...entry.message,
|
||||
id: entry.id,
|
||||
timestamp: entry.message.timestamp || timestampMs,
|
||||
} as unknown as ChatMessage)
|
||||
}
|
||||
} else if (entry.type === 'custom_message') {
|
||||
if (entry.customType === 'context') return
|
||||
messages.push({
|
||||
@@ -117,9 +137,13 @@ export function mapEntryToMessage(
|
||||
entryRecord: Prisma.AgentSessionEntryGetPayload<Record<string, never>>,
|
||||
): ChatMessage | null {
|
||||
const payload = (entryRecord.data as Record<string, unknown>) || {}
|
||||
let type = (entryRecord.type || 'message') as SessionTreeEntry['type']
|
||||
if (type === 'message' && payload.customType === 'shumai_message') {
|
||||
type = 'custom_message'
|
||||
}
|
||||
const entryObj = {
|
||||
id: entryRecord.id,
|
||||
type: (entryRecord.type || 'message') as SessionTreeEntry['type'],
|
||||
type,
|
||||
parentId: entryRecord.parentId,
|
||||
timestamp: entryRecord.createdAt.toISOString(),
|
||||
...payload,
|
||||
|
||||
Reference in New Issue
Block a user