refactor(chatbot): import AgentMessage type statically in DTO layer

This commit is contained in:
yiling
2026-07-07 12:30:41 +08:00
parent b8eddcd854
commit 55026af22e
5 changed files with 56 additions and 44 deletions
+14 -13
View File
@@ -37,7 +37,7 @@
},
"apps/agent": {
"name": "@shumai/agent-app",
"version": "0.1.5",
"version": "0.1.7",
"bin": {
"shumai-agent": "./src/index.ts",
},
@@ -49,7 +49,7 @@
},
"apps/cli": {
"name": "@shumai/cli",
"version": "0.1.5",
"version": "0.1.7",
"bin": {
"shumai-cli": "./src/index.ts",
},
@@ -64,7 +64,7 @@
},
"apps/transcode": {
"name": "@shumai/transcode-app",
"version": "0.1.5",
"version": "0.1.7",
"bin": {
"shumai-transcode": "./src/index.ts",
},
@@ -76,7 +76,7 @@
},
"apps/web": {
"name": "@shumai/web-app",
"version": "0.1.5",
"version": "0.1.7",
"dependencies": {
"@shumai/agent": "workspace:*",
"@shumai/api": "workspace:*",
@@ -91,7 +91,7 @@
},
"packages/agent": {
"name": "@shumai/agent",
"version": "0.1.5",
"version": "0.1.7",
"dependencies": {
"@anthropic-ai/sandbox-runtime": "0.0.52",
"@earendil-works/pi-agent-core": "0.78.0",
@@ -115,7 +115,7 @@
},
"packages/api": {
"name": "@shumai/api",
"version": "0.1.5",
"version": "0.1.7",
"dependencies": {
"@hono/zod-validator": "0.7.6",
"@shumai/core": "workspace:*",
@@ -129,7 +129,7 @@
},
"packages/core": {
"name": "@shumai/core",
"version": "0.1.5",
"version": "0.1.7",
"dependencies": {
"@shumai/db": "workspace:*",
"@shumai/dtos": "workspace:*",
@@ -156,7 +156,7 @@
},
"packages/db": {
"name": "@shumai/db",
"version": "0.1.5",
"version": "0.1.7",
"dependencies": {
"@earendil-works/pi-agent-core": "0.78.0",
"@prisma/adapter-pg": "7.8.0",
@@ -172,18 +172,19 @@
},
"packages/dtos": {
"name": "@shumai/dtos",
"version": "0.1.5",
"version": "0.1.7",
"dependencies": {
"@earendil-works/pi-agent-core": "0.78.0",
"zod": "4.4.3",
},
},
"packages/tableprinter": {
"name": "@shumai/tableprinter",
"version": "0.1.5",
"version": "0.1.7",
},
"packages/transcode": {
"name": "@shumai/transcode",
"version": "0.1.5",
"version": "0.1.7",
"dependencies": {
"@shumai/core": "workspace:*",
"@shumai/db": "workspace:*",
@@ -197,7 +198,7 @@
},
"packages/webui": {
"name": "@shumai/webui",
"version": "0.1.5",
"version": "0.1.7",
"dependencies": {
"@base-ui/react": "1.5.0",
"@dnd-kit/abstract": "0.4.0",
@@ -294,7 +295,7 @@
},
"packages/workflow-core": {
"name": "@shumai/workflow-core",
"version": "0.1.5",
"version": "0.1.7",
"dependencies": {
"@shumai/core": "workspace:*",
"@shumai/db": "workspace:*",
+30 -15
View File
@@ -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')
})
})
+7 -7
View File
@@ -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 {
+1
View File
@@ -6,6 +6,7 @@
"main": "./src/index.ts",
"types": "./src/index.ts",
"dependencies": {
"@earendil-works/pi-agent-core": "0.78.0",
"zod": "4.4.3"
}
}
+4 -9
View File
@@ -1,4 +1,5 @@
import { z } from 'zod'
import type { AgentMessage } from '@earendil-works/pi-agent-core'
export const chatRequestSchema = z
.object({
@@ -41,13 +42,7 @@ export const chatMessageSchema = z.intersection(
z.object({
id: z.string(),
}),
z.record(z.string(), z.unknown()),
)
z.unknown(),
) as unknown as z.ZodType<AgentMessage & { id: string }>
export type ChatMessage = {
id: string
role: string
content: string | unknown[]
timestamp?: number
[key: string]: unknown
}
export type ChatMessage = AgentMessage & { id: string }