From 55026af22ec36faa6dfa2169847bb24d2febf2b8 Mon Sep 17 00:00:00 2001 From: yiling Date: Tue, 7 Jul 2026 12:30:41 +0800 Subject: [PATCH] refactor(chatbot): import AgentMessage type statically in DTO layer --- bun.lock | 27 ++++++++--------- packages/core/src/chat/chat.test.ts | 45 +++++++++++++++++++---------- packages/core/src/chat/chat.ts | 14 ++++----- packages/dtos/package.json | 1 + packages/dtos/src/chat.ts | 13 +++------ 5 files changed, 56 insertions(+), 44 deletions(-) diff --git a/bun.lock b/bun.lock index 364519f2..5db1316a 100644 --- a/bun.lock +++ b/bun.lock @@ -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:*", diff --git a/packages/core/src/chat/chat.test.ts b/packages/core/src/chat/chat.test.ts index eaca5071..27f7b7b7 100644 --- a/packages/core/src/chat/chat.test.ts +++ b/packages/core/src/chat/chat.test.ts @@ -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).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).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).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).fromId).toBe('entry-old') }) }) diff --git a/packages/core/src/chat/chat.ts b/packages/core/src/chat/chat.ts index 79a5b895..1857bada 100644 --- a/packages/core/src/chat/chat.ts +++ b/packages/core/src/chat/chat.ts @@ -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 { diff --git a/packages/dtos/package.json b/packages/dtos/package.json index 66190881..6b9e94a9 100644 --- a/packages/dtos/package.json +++ b/packages/dtos/package.json @@ -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" } } diff --git a/packages/dtos/src/chat.ts b/packages/dtos/src/chat.ts index a40b06e3..9ad1b08a 100644 --- a/packages/dtos/src/chat.ts +++ b/packages/dtos/src/chat.ts @@ -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 -export type ChatMessage = { - id: string - role: string - content: string | unknown[] - timestamp?: number - [key: string]: unknown -} +export type ChatMessage = AgentMessage & { id: string }