mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 05:54:44 +00:00
refactor(chatbot): import AgentMessage type statically in DTO layer
This commit is contained in:
@@ -37,7 +37,7 @@
|
|||||||
},
|
},
|
||||||
"apps/agent": {
|
"apps/agent": {
|
||||||
"name": "@shumai/agent-app",
|
"name": "@shumai/agent-app",
|
||||||
"version": "0.1.5",
|
"version": "0.1.7",
|
||||||
"bin": {
|
"bin": {
|
||||||
"shumai-agent": "./src/index.ts",
|
"shumai-agent": "./src/index.ts",
|
||||||
},
|
},
|
||||||
@@ -49,7 +49,7 @@
|
|||||||
},
|
},
|
||||||
"apps/cli": {
|
"apps/cli": {
|
||||||
"name": "@shumai/cli",
|
"name": "@shumai/cli",
|
||||||
"version": "0.1.5",
|
"version": "0.1.7",
|
||||||
"bin": {
|
"bin": {
|
||||||
"shumai-cli": "./src/index.ts",
|
"shumai-cli": "./src/index.ts",
|
||||||
},
|
},
|
||||||
@@ -64,7 +64,7 @@
|
|||||||
},
|
},
|
||||||
"apps/transcode": {
|
"apps/transcode": {
|
||||||
"name": "@shumai/transcode-app",
|
"name": "@shumai/transcode-app",
|
||||||
"version": "0.1.5",
|
"version": "0.1.7",
|
||||||
"bin": {
|
"bin": {
|
||||||
"shumai-transcode": "./src/index.ts",
|
"shumai-transcode": "./src/index.ts",
|
||||||
},
|
},
|
||||||
@@ -76,7 +76,7 @@
|
|||||||
},
|
},
|
||||||
"apps/web": {
|
"apps/web": {
|
||||||
"name": "@shumai/web-app",
|
"name": "@shumai/web-app",
|
||||||
"version": "0.1.5",
|
"version": "0.1.7",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shumai/agent": "workspace:*",
|
"@shumai/agent": "workspace:*",
|
||||||
"@shumai/api": "workspace:*",
|
"@shumai/api": "workspace:*",
|
||||||
@@ -91,7 +91,7 @@
|
|||||||
},
|
},
|
||||||
"packages/agent": {
|
"packages/agent": {
|
||||||
"name": "@shumai/agent",
|
"name": "@shumai/agent",
|
||||||
"version": "0.1.5",
|
"version": "0.1.7",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@anthropic-ai/sandbox-runtime": "0.0.52",
|
"@anthropic-ai/sandbox-runtime": "0.0.52",
|
||||||
"@earendil-works/pi-agent-core": "0.78.0",
|
"@earendil-works/pi-agent-core": "0.78.0",
|
||||||
@@ -115,7 +115,7 @@
|
|||||||
},
|
},
|
||||||
"packages/api": {
|
"packages/api": {
|
||||||
"name": "@shumai/api",
|
"name": "@shumai/api",
|
||||||
"version": "0.1.5",
|
"version": "0.1.7",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@hono/zod-validator": "0.7.6",
|
"@hono/zod-validator": "0.7.6",
|
||||||
"@shumai/core": "workspace:*",
|
"@shumai/core": "workspace:*",
|
||||||
@@ -129,7 +129,7 @@
|
|||||||
},
|
},
|
||||||
"packages/core": {
|
"packages/core": {
|
||||||
"name": "@shumai/core",
|
"name": "@shumai/core",
|
||||||
"version": "0.1.5",
|
"version": "0.1.7",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shumai/db": "workspace:*",
|
"@shumai/db": "workspace:*",
|
||||||
"@shumai/dtos": "workspace:*",
|
"@shumai/dtos": "workspace:*",
|
||||||
@@ -156,7 +156,7 @@
|
|||||||
},
|
},
|
||||||
"packages/db": {
|
"packages/db": {
|
||||||
"name": "@shumai/db",
|
"name": "@shumai/db",
|
||||||
"version": "0.1.5",
|
"version": "0.1.7",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@earendil-works/pi-agent-core": "0.78.0",
|
"@earendil-works/pi-agent-core": "0.78.0",
|
||||||
"@prisma/adapter-pg": "7.8.0",
|
"@prisma/adapter-pg": "7.8.0",
|
||||||
@@ -172,18 +172,19 @@
|
|||||||
},
|
},
|
||||||
"packages/dtos": {
|
"packages/dtos": {
|
||||||
"name": "@shumai/dtos",
|
"name": "@shumai/dtos",
|
||||||
"version": "0.1.5",
|
"version": "0.1.7",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@earendil-works/pi-agent-core": "0.78.0",
|
||||||
"zod": "4.4.3",
|
"zod": "4.4.3",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"packages/tableprinter": {
|
"packages/tableprinter": {
|
||||||
"name": "@shumai/tableprinter",
|
"name": "@shumai/tableprinter",
|
||||||
"version": "0.1.5",
|
"version": "0.1.7",
|
||||||
},
|
},
|
||||||
"packages/transcode": {
|
"packages/transcode": {
|
||||||
"name": "@shumai/transcode",
|
"name": "@shumai/transcode",
|
||||||
"version": "0.1.5",
|
"version": "0.1.7",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shumai/core": "workspace:*",
|
"@shumai/core": "workspace:*",
|
||||||
"@shumai/db": "workspace:*",
|
"@shumai/db": "workspace:*",
|
||||||
@@ -197,7 +198,7 @@
|
|||||||
},
|
},
|
||||||
"packages/webui": {
|
"packages/webui": {
|
||||||
"name": "@shumai/webui",
|
"name": "@shumai/webui",
|
||||||
"version": "0.1.5",
|
"version": "0.1.7",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@base-ui/react": "1.5.0",
|
"@base-ui/react": "1.5.0",
|
||||||
"@dnd-kit/abstract": "0.4.0",
|
"@dnd-kit/abstract": "0.4.0",
|
||||||
@@ -294,7 +295,7 @@
|
|||||||
},
|
},
|
||||||
"packages/workflow-core": {
|
"packages/workflow-core": {
|
||||||
"name": "@shumai/workflow-core",
|
"name": "@shumai/workflow-core",
|
||||||
"version": "0.1.5",
|
"version": "0.1.7",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shumai/core": "workspace:*",
|
"@shumai/core": "workspace:*",
|
||||||
"@shumai/db": "workspace:*",
|
"@shumai/db": "workspace:*",
|
||||||
|
|||||||
@@ -255,8 +255,11 @@ describe('ChatService', () => {
|
|||||||
|
|
||||||
const messages = await chatService.listMessages(user.id, sessionId)
|
const messages = await chatService.listMessages(user.id, sessionId)
|
||||||
expect(messages).toHaveLength(1)
|
expect(messages).toHaveLength(1)
|
||||||
expect(messages[0].role).toBe('user')
|
// Casting to any to access properties of union in test assertions
|
||||||
expect(messages[0].content).toEqual([{ type: 'text', text: 'hello' }])
|
// 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 () => {
|
it('should delete session and cascade', async () => {
|
||||||
@@ -329,24 +332,36 @@ describe('ChatService', () => {
|
|||||||
expect(messages).toHaveLength(4)
|
expect(messages).toHaveLength(4)
|
||||||
|
|
||||||
// 1. Compaction summary message
|
// 1. Compaction summary message
|
||||||
expect(messages[0].role).toBe('custom')
|
// Casting to any to access properties of union in test assertions
|
||||||
expect(messages[0].customType).toBe('compaction-summary')
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
expect(messages[0].content).toBe('Compacted conversation history')
|
const msg0 = messages[0] as any
|
||||||
expect((messages[0].details as Record<string, unknown>).tokensBefore).toBe(200)
|
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)
|
// 2. Kept entry (entry-2)
|
||||||
expect(messages[1].id).toBe('entry-2')
|
// Casting to any to access properties of union in test assertions
|
||||||
expect(messages[1].role).toBe('assistant')
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
expect(messages[1].content).toBe('kept message')
|
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)
|
// 3. Thinking level change (entry-3)
|
||||||
expect(messages[2].role).toBe('thinking_level_change')
|
// Casting to any to access properties of union in test assertions
|
||||||
expect(messages[2].content).toContain('Thinking level changed to high')
|
// 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)
|
// 4. Branch summary (entry-4)
|
||||||
expect(messages[3].role).toBe('custom')
|
// Casting to any to access properties of union in test assertions
|
||||||
expect(messages[3].customType).toBe('branch-summary')
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
expect(messages[3].content).toBe('Branch summary details')
|
const msg3 = messages[3] as any
|
||||||
expect((messages[3].details as Record<string, unknown>).fromId).toBe('entry-old')
|
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,
|
role: entry.message.role,
|
||||||
content: entry.message.content as string | unknown[],
|
content: entry.message.content as string | unknown[],
|
||||||
timestamp: entry.message.timestamp || timestampMs,
|
timestamp: entry.message.timestamp || timestampMs,
|
||||||
})
|
} as unknown as ChatMessage)
|
||||||
} else if (entry.type === 'custom_message') {
|
} else if (entry.type === 'custom_message') {
|
||||||
messages.push({
|
messages.push({
|
||||||
id: entry.id || 'unknown-id',
|
id: entry.id || 'unknown-id',
|
||||||
@@ -56,7 +56,7 @@ export function buildSessionMessages(pathEntries: SimpleMessageEntry[]): ChatMes
|
|||||||
display: entry.display,
|
display: entry.display,
|
||||||
details: entry.details,
|
details: entry.details,
|
||||||
timestamp: timestampMs,
|
timestamp: timestampMs,
|
||||||
})
|
} as unknown as ChatMessage)
|
||||||
} else if (entry.type === 'branch_summary' && entry.summary) {
|
} else if (entry.type === 'branch_summary' && entry.summary) {
|
||||||
messages.push({
|
messages.push({
|
||||||
id: entry.id || 'unknown-id',
|
id: entry.id || 'unknown-id',
|
||||||
@@ -65,14 +65,14 @@ export function buildSessionMessages(pathEntries: SimpleMessageEntry[]): ChatMes
|
|||||||
content: entry.summary,
|
content: entry.summary,
|
||||||
details: { fromId: entry.fromId },
|
details: { fromId: entry.fromId },
|
||||||
timestamp: timestampMs,
|
timestamp: timestampMs,
|
||||||
})
|
} as unknown as ChatMessage)
|
||||||
} else if (entry.type === 'thinking_level_change' && entry.thinkingLevel) {
|
} else if (entry.type === 'thinking_level_change' && entry.thinkingLevel) {
|
||||||
messages.push({
|
messages.push({
|
||||||
id: entry.id || 'unknown-id',
|
id: entry.id || 'unknown-id',
|
||||||
role: 'thinking_level_change',
|
role: 'thinking_level_change',
|
||||||
content: `Thinking level changed to ${entry.thinkingLevel}`,
|
content: `Thinking level changed to ${entry.thinkingLevel}`,
|
||||||
timestamp: timestampMs,
|
timestamp: timestampMs,
|
||||||
})
|
} as unknown as ChatMessage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +85,7 @@ export function buildSessionMessages(pathEntries: SimpleMessageEntry[]): ChatMes
|
|||||||
content: compaction.summary || '',
|
content: compaction.summary || '',
|
||||||
details: { tokensBefore: compaction.tokensBefore },
|
details: { tokensBefore: compaction.tokensBefore },
|
||||||
timestamp: compactionTimestamp,
|
timestamp: compactionTimestamp,
|
||||||
})
|
} as unknown as ChatMessage)
|
||||||
const compactionIdx = pathEntries.findIndex(
|
const compactionIdx = pathEntries.findIndex(
|
||||||
(e) => e.type === 'compaction' && e.id === compaction?.id,
|
(e) => e.type === 'compaction' && e.id === compaction?.id,
|
||||||
)
|
)
|
||||||
@@ -118,9 +118,9 @@ export function mapEntryToMessage(
|
|||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
id: entryRecord.id,
|
id: entryRecord.id,
|
||||||
role: 'unknown',
|
role: 'user',
|
||||||
content: '',
|
content: '',
|
||||||
}
|
} as unknown as ChatMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ChatService {
|
export class ChatService {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
"main": "./src/index.ts",
|
"main": "./src/index.ts",
|
||||||
"types": "./src/index.ts",
|
"types": "./src/index.ts",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@earendil-works/pi-agent-core": "0.78.0",
|
||||||
"zod": "4.4.3"
|
"zod": "4.4.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
import type { AgentMessage } from '@earendil-works/pi-agent-core'
|
||||||
|
|
||||||
export const chatRequestSchema = z
|
export const chatRequestSchema = z
|
||||||
.object({
|
.object({
|
||||||
@@ -41,13 +42,7 @@ export const chatMessageSchema = z.intersection(
|
|||||||
z.object({
|
z.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
}),
|
}),
|
||||||
z.record(z.string(), z.unknown()),
|
z.unknown(),
|
||||||
)
|
) as unknown as z.ZodType<AgentMessage & { id: string }>
|
||||||
|
|
||||||
export type ChatMessage = {
|
export type ChatMessage = AgentMessage & { id: string }
|
||||||
id: string
|
|
||||||
role: string
|
|
||||||
content: string | unknown[]
|
|
||||||
timestamp?: number
|
|
||||||
[key: string]: unknown
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user