mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 14:59:31 +00:00
refactor(chatbot): import AgentMessage type statically in DTO layer
This commit is contained in:
@@ -255,8 +255,11 @@ describe('ChatService', () => {
|
||||
|
||||
const messages = await chatService.listMessages(user.id, sessionId)
|
||||
expect(messages).toHaveLength(1)
|
||||
expect(messages[0].role).toBe('user')
|
||||
expect(messages[0].content).toEqual([{ type: 'text', text: 'hello' }])
|
||||
// Casting to any to access properties of union in test assertions
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const msg = messages[0] as any
|
||||
expect(msg.role).toBe('user')
|
||||
expect(msg.content).toEqual([{ type: 'text', text: 'hello' }])
|
||||
})
|
||||
|
||||
it('should delete session and cascade', async () => {
|
||||
@@ -329,24 +332,36 @@ describe('ChatService', () => {
|
||||
expect(messages).toHaveLength(4)
|
||||
|
||||
// 1. Compaction summary message
|
||||
expect(messages[0].role).toBe('custom')
|
||||
expect(messages[0].customType).toBe('compaction-summary')
|
||||
expect(messages[0].content).toBe('Compacted conversation history')
|
||||
expect((messages[0].details as Record<string, unknown>).tokensBefore).toBe(200)
|
||||
// Casting to any to access properties of union in test assertions
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const msg0 = messages[0] as any
|
||||
expect(msg0.role).toBe('custom')
|
||||
expect(msg0.customType).toBe('compaction-summary')
|
||||
expect(msg0.content).toBe('Compacted conversation history')
|
||||
expect((msg0.details as Record<string, unknown>).tokensBefore).toBe(200)
|
||||
|
||||
// 2. Kept entry (entry-2)
|
||||
expect(messages[1].id).toBe('entry-2')
|
||||
expect(messages[1].role).toBe('assistant')
|
||||
expect(messages[1].content).toBe('kept message')
|
||||
// Casting to any to access properties of union in test assertions
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const msg1 = messages[1] as any
|
||||
expect(msg1.id).toBe('entry-2')
|
||||
expect(msg1.role).toBe('assistant')
|
||||
expect(msg1.content).toBe('kept message')
|
||||
|
||||
// 3. Thinking level change (entry-3)
|
||||
expect(messages[2].role).toBe('thinking_level_change')
|
||||
expect(messages[2].content).toContain('Thinking level changed to high')
|
||||
// Casting to any to access properties of union in test assertions
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const msg2 = messages[2] as any
|
||||
expect(msg2.role).toBe('thinking_level_change')
|
||||
expect(msg2.content).toContain('Thinking level changed to high')
|
||||
|
||||
// 4. Branch summary (entry-4)
|
||||
expect(messages[3].role).toBe('custom')
|
||||
expect(messages[3].customType).toBe('branch-summary')
|
||||
expect(messages[3].content).toBe('Branch summary details')
|
||||
expect((messages[3].details as Record<string, unknown>).fromId).toBe('entry-old')
|
||||
// Casting to any to access properties of union in test assertions
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const msg3 = messages[3] as any
|
||||
expect(msg3.role).toBe('custom')
|
||||
expect(msg3.customType).toBe('branch-summary')
|
||||
expect(msg3.content).toBe('Branch summary details')
|
||||
expect((msg3.details as Record<string, unknown>).fromId).toBe('entry-old')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -46,7 +46,7 @@ export function buildSessionMessages(pathEntries: SimpleMessageEntry[]): ChatMes
|
||||
role: entry.message.role,
|
||||
content: entry.message.content as string | unknown[],
|
||||
timestamp: entry.message.timestamp || timestampMs,
|
||||
})
|
||||
} as unknown as ChatMessage)
|
||||
} else if (entry.type === 'custom_message') {
|
||||
messages.push({
|
||||
id: entry.id || 'unknown-id',
|
||||
@@ -56,7 +56,7 @@ export function buildSessionMessages(pathEntries: SimpleMessageEntry[]): ChatMes
|
||||
display: entry.display,
|
||||
details: entry.details,
|
||||
timestamp: timestampMs,
|
||||
})
|
||||
} as unknown as ChatMessage)
|
||||
} else if (entry.type === 'branch_summary' && entry.summary) {
|
||||
messages.push({
|
||||
id: entry.id || 'unknown-id',
|
||||
@@ -65,14 +65,14 @@ export function buildSessionMessages(pathEntries: SimpleMessageEntry[]): ChatMes
|
||||
content: entry.summary,
|
||||
details: { fromId: entry.fromId },
|
||||
timestamp: timestampMs,
|
||||
})
|
||||
} as unknown as ChatMessage)
|
||||
} else if (entry.type === 'thinking_level_change' && entry.thinkingLevel) {
|
||||
messages.push({
|
||||
id: entry.id || 'unknown-id',
|
||||
role: 'thinking_level_change',
|
||||
content: `Thinking level changed to ${entry.thinkingLevel}`,
|
||||
timestamp: timestampMs,
|
||||
})
|
||||
} as unknown as ChatMessage)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ export function buildSessionMessages(pathEntries: SimpleMessageEntry[]): ChatMes
|
||||
content: compaction.summary || '',
|
||||
details: { tokensBefore: compaction.tokensBefore },
|
||||
timestamp: compactionTimestamp,
|
||||
})
|
||||
} as unknown as ChatMessage)
|
||||
const compactionIdx = pathEntries.findIndex(
|
||||
(e) => e.type === 'compaction' && e.id === compaction?.id,
|
||||
)
|
||||
@@ -118,9 +118,9 @@ export function mapEntryToMessage(
|
||||
}
|
||||
return {
|
||||
id: entryRecord.id,
|
||||
role: 'unknown',
|
||||
role: 'user',
|
||||
content: '',
|
||||
}
|
||||
} as unknown as ChatMessage
|
||||
}
|
||||
|
||||
export class ChatService {
|
||||
|
||||
Reference in New Issue
Block a user