mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 23:24:21 +00:00
feat(chatbot): remove manual AgentSessionEntry creation from ChatService
This commit is contained in:
@@ -98,14 +98,11 @@ describe('ChatService', () => {
|
|||||||
})
|
})
|
||||||
expect(comment).toBeNull()
|
expect(comment).toBeNull()
|
||||||
|
|
||||||
// Verify session entry is created
|
// Verify no session entry is created yet (it is created automatically when the workflow runs)
|
||||||
const entry = await prisma.agentSessionEntry.findFirst({
|
const entry = await prisma.agentSessionEntry.findFirst({
|
||||||
where: { sessionId },
|
where: { sessionId },
|
||||||
})
|
})
|
||||||
expect(entry).toBeDefined()
|
expect(entry).toBeNull()
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
||||||
const payload = entry?.entry as any
|
|
||||||
expect(payload.message.content[0].text).toBe('hello world')
|
|
||||||
|
|
||||||
// Verify WorkflowTask is created with correct payload
|
// Verify WorkflowTask is created with correct payload
|
||||||
const task = await prisma.workflowTask.findUnique({
|
const task = await prisma.workflowTask.findUnique({
|
||||||
@@ -142,16 +139,11 @@ describe('ChatService', () => {
|
|||||||
expect(nextSessionId).toBe(sessionId)
|
expect(nextSessionId).toBe(sessionId)
|
||||||
expect(nextTaskId).toBeDefined()
|
expect(nextTaskId).toBeDefined()
|
||||||
|
|
||||||
// Verify entries order
|
// Verify no entries are created yet (they are created automatically when the workflow runs)
|
||||||
const entries = await prisma.agentSessionEntry.findMany({
|
const entries = await prisma.agentSessionEntry.findMany({
|
||||||
where: { sessionId },
|
where: { sessionId },
|
||||||
orderBy: { id: 'asc' },
|
|
||||||
})
|
})
|
||||||
expect(entries).toHaveLength(2)
|
expect(entries).toHaveLength(0)
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
||||||
expect((entries[0].entry as any).message.content[0].text).toBe('first message')
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
||||||
expect((entries[1].entry as any).message.content[0].text).toBe('second message')
|
|
||||||
|
|
||||||
// Verify WorkflowTask is created with correct payload
|
// Verify WorkflowTask is created with correct payload
|
||||||
const task = await prisma.workflowTask.findUnique({
|
const task = await prisma.workflowTask.findUnique({
|
||||||
@@ -242,6 +234,25 @@ describe('ChatService', () => {
|
|||||||
projectId: project.id,
|
projectId: project.id,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Manually insert a test entry representing user message
|
||||||
|
await prisma.agentSessionEntry.create({
|
||||||
|
data: {
|
||||||
|
id: 'test-entry-id',
|
||||||
|
sessionId,
|
||||||
|
entry: {
|
||||||
|
type: 'message',
|
||||||
|
id: 'test-entry-id',
|
||||||
|
parentId: null,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
message: {
|
||||||
|
role: 'user',
|
||||||
|
content: [{ type: 'text', text: 'hello' }],
|
||||||
|
timestamp: Date.now(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
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')
|
expect(messages[0].role).toBe('user')
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import type { Prisma } from '@shumai/db'
|
|||||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||||
import { paginateQuery, type PaginatedData } from '@shumai/core/src/pagination'
|
import { paginateQuery, type PaginatedData } from '@shumai/core/src/pagination'
|
||||||
import type { ChatRequest, ChatSessionInfo, ChatMessage } from '@shumai/dtos'
|
import type { ChatRequest, ChatSessionInfo, ChatMessage } from '@shumai/dtos'
|
||||||
import { ulid } from 'ulid'
|
|
||||||
|
|
||||||
type User = Prisma.UserGetPayload<Record<string, never>>
|
type User = Prisma.UserGetPayload<Record<string, never>>
|
||||||
|
|
||||||
@@ -194,13 +193,13 @@ export class ChatService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let activeSessionId = passedSessionId
|
let activeSessionId = passedSessionId
|
||||||
let existingSession = null
|
|
||||||
|
|
||||||
if (activeSessionId) {
|
if (activeSessionId) {
|
||||||
existingSession = await tx.agentSession.findUnique({
|
const sessionExists = await tx.agentSession.findUnique({
|
||||||
where: { id: activeSessionId },
|
where: { id: activeSessionId },
|
||||||
|
select: { id: true },
|
||||||
})
|
})
|
||||||
if (!existingSession) throw new Error('Session not found')
|
if (!sessionExists) throw new Error('Session not found')
|
||||||
} else {
|
} else {
|
||||||
const newSession = await tx.agentSession.create({
|
const newSession = await tx.agentSession.create({
|
||||||
data: {
|
data: {
|
||||||
@@ -212,38 +211,6 @@ export class ChatService {
|
|||||||
})
|
})
|
||||||
activeSessionId = newSession.id
|
activeSessionId = newSession.id
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append entry for user's message
|
|
||||||
const parentId = passedSessionId ? existingSession?.leafId : null
|
|
||||||
const entryId = ulid()
|
|
||||||
const entryJson = {
|
|
||||||
type: 'message' as const,
|
|
||||||
id: entryId,
|
|
||||||
parentId: parentId || null,
|
|
||||||
timestamp: new Date().toISOString(),
|
|
||||||
message: {
|
|
||||||
role: 'user' as const,
|
|
||||||
content: [{ type: 'text' as const, text: finalPrompt }],
|
|
||||||
timestamp: Date.now(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
await tx.agentSessionEntry.create({
|
|
||||||
data: {
|
|
||||||
id: entryId,
|
|
||||||
sessionId: activeSessionId!,
|
|
||||||
entry: entryJson,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
// Update session's state
|
|
||||||
await tx.agentSession.update({
|
|
||||||
where: { id: activeSessionId },
|
|
||||||
data: {
|
|
||||||
leafId: entryId,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
// Trigger chat workflow
|
// Trigger chat workflow
|
||||||
const task = await tx.workflowTask.create({
|
const task = await tx.workflowTask.create({
|
||||||
data: {
|
data: {
|
||||||
|
|||||||
Reference in New Issue
Block a user