feat(chat): filter out context messages from API list and SSE stream

This commit is contained in:
yiling
2026-07-11 15:47:48 +08:00
parent 35db3fc49e
commit cc9b32716d
3 changed files with 82 additions and 2 deletions
+2
View File
@@ -67,6 +67,7 @@ const route = new Hono<{ Variables: { user: User } }>()
if (newEntries.length > 0) {
for (const record of newEntries) {
const message = mapEntryToMessage(record)
if (!message) continue
await stream.writeSSE({
data: JSON.stringify({
type: 'entry',
@@ -94,6 +95,7 @@ const route = new Hono<{ Variables: { user: User } }>()
})
for (const record of finalEntries) {
const message = mapEntryToMessage(record)
if (!message) continue
await stream.writeSSE({
data: JSON.stringify({
type: 'entry',
+75 -1
View File
@@ -1,6 +1,6 @@
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { chatService, buildSessionMessages, type PathEntry } from './chat'
import { chatService, buildSessionMessages, mapEntryToMessage, type PathEntry } from './chat'
import { describe, expect, it } from 'vitest'
describe('ChatService', () => {
@@ -255,6 +255,80 @@ describe('ChatService', () => {
expect(msg.content).toEqual([{ type: 'text', text: 'hello' }])
})
it('should hide customType context messages', async () => {
const { user, project } = await setupBasicData()
const { sessionId } = await chatService.startOrContinueChat(user, {
textPrompt: 'hello',
projectId: project.id,
})
// 1. Manually insert a test entry representing user message
await prisma.agentSessionEntry.create({
data: {
id: 'test-entry-1-user',
sessionId,
entry: {
type: 'message',
id: 'test-entry-1-user',
parentId: null,
timestamp: new Date().toISOString(),
message: {
role: 'user',
content: [{ type: 'text', text: 'hello' }],
timestamp: Date.now(),
},
} as unknown as PrismaJson.PiSessionEntry,
},
})
// 2. Manually insert a test entry representing a custom context message
await prisma.agentSessionEntry.create({
data: {
id: 'test-entry-2-context',
sessionId,
entry: {
type: 'custom_message',
id: 'test-entry-2-context',
parentId: 'test-entry-1-user',
timestamp: new Date().toISOString(),
customType: 'context',
content: 'some context',
display: 'chat',
} as unknown as PrismaJson.PiSessionEntry,
},
})
// 3. Manually insert a test entry representing a custom thinking level change
await prisma.agentSessionEntry.create({
data: {
id: 'test-entry-3-thinking',
sessionId,
entry: {
type: 'thinking_level_change',
id: 'test-entry-3-thinking',
parentId: 'test-entry-2-context',
timestamp: new Date().toISOString(),
thinkingLevel: 'deep',
} as unknown as PrismaJson.PiSessionEntry,
},
})
// Verify listMessages returns only the user and thinking level change message, hiding the context message
const messages = await chatService.listMessages(user.id, sessionId)
expect(messages).toHaveLength(2)
expect(messages[0].id).toBe('test-entry-1-user')
expect(messages[1].id).toBe('test-entry-3-thinking')
// Verify mapEntryToMessage returns null for context message
const contextRecord = await prisma.agentSessionEntry.findUnique({
where: { id: 'test-entry-2-context' },
})
expect(contextRecord).not.toBeNull()
const mapped = mapEntryToMessage(contextRecord!)
expect(mapped).toBeNull()
})
it('should delete session and cascade', async () => {
const { user, project } = await setupBasicData()
+5 -1
View File
@@ -28,6 +28,7 @@ export function buildSessionMessages(pathEntries: PathEntry[]): ChatMessage[] {
timestamp: entry.message.timestamp || timestampMs,
} as unknown as ChatMessage)
} else if (entry.type === 'custom_message') {
if (entry.customType === 'context') return
messages.push({
id: entry.id,
role: 'custom',
@@ -89,8 +90,11 @@ export function buildSessionMessages(pathEntries: PathEntry[]): ChatMessage[] {
export function mapEntryToMessage(
entryRecord: Prisma.AgentSessionEntryGetPayload<Record<string, never>>,
): ChatMessage {
): ChatMessage | null {
const entryObj = entryRecord.entry as unknown as SessionTreeEntry
if (entryObj.type === 'custom_message' && (entryObj as { customType?: string }).customType === 'context') {
return null
}
const pathEntries = [{ ...entryObj, id: entryRecord.id }]
const messages = buildSessionMessages(pathEntries)
if (messages.length > 0) {