mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 09:37:20 +00:00
feat(chatbot): require agentId in ChatRequest and validate agent presence
This commit is contained in:
@@ -139,6 +139,7 @@ describe('Chat API', () => {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
agentId: 'agent1',
|
||||
textPrompt: 'hi',
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -48,13 +48,35 @@ describe('ChatService', () => {
|
||||
data: { rootFolderId: rootFolder.id },
|
||||
})
|
||||
|
||||
return { user, team, teamMember, project, rootFolder }
|
||||
const agentUser = await prisma.user.create({
|
||||
data: {
|
||||
id: 'test-agent-id',
|
||||
name: 'Test Agent',
|
||||
email: 'test-agent@shumai.ai',
|
||||
type: 'agent',
|
||||
},
|
||||
})
|
||||
|
||||
const agent = await prisma.agent.create({
|
||||
data: {
|
||||
id: 'test-agent-id',
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
config: {
|
||||
provider: 'openai',
|
||||
model: 'gpt-4',
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
return { user, team, teamMember, project, rootFolder, agent, agentUser }
|
||||
}
|
||||
|
||||
it('should start a new chat session and trigger workflow', async () => {
|
||||
const { user, project, rootFolder } = await setupBasicData()
|
||||
const { user, project, rootFolder, agent } = await setupBasicData()
|
||||
|
||||
const { sessionId, taskId } = await chatService.startOrContinueChat(user, {
|
||||
agentId: agent.id,
|
||||
textPrompt: 'hello world',
|
||||
projectId: project.id,
|
||||
})
|
||||
@@ -98,10 +120,11 @@ describe('ChatService', () => {
|
||||
})
|
||||
|
||||
it('should continue an existing chat session', async () => {
|
||||
const { user, project } = await setupBasicData()
|
||||
const { user, project, agent } = await setupBasicData()
|
||||
|
||||
// Start
|
||||
const { sessionId } = await chatService.startOrContinueChat(user, {
|
||||
agentId: agent.id,
|
||||
textPrompt: 'first message',
|
||||
projectId: project.id,
|
||||
})
|
||||
@@ -110,6 +133,7 @@ describe('ChatService', () => {
|
||||
const { sessionId: nextSessionId, taskId: nextTaskId } = await chatService.startOrContinueChat(
|
||||
user,
|
||||
{
|
||||
agentId: agent.id,
|
||||
textPrompt: 'second message',
|
||||
sessionId,
|
||||
},
|
||||
@@ -140,7 +164,7 @@ describe('ChatService', () => {
|
||||
})
|
||||
|
||||
it('should inject context of referenced assets and attached files', async () => {
|
||||
const { user, project, rootFolder } = await setupBasicData()
|
||||
const { user, project, rootFolder, agent } = await setupBasicData()
|
||||
|
||||
const childFolder = await prisma.asset.create({
|
||||
data: {
|
||||
@@ -171,6 +195,7 @@ describe('ChatService', () => {
|
||||
})
|
||||
|
||||
const { taskId } = await chatService.startOrContinueChat(user, {
|
||||
agentId: agent.id,
|
||||
textPrompt: 'process file',
|
||||
projectId: project.id,
|
||||
assetIds: [childFolder.id],
|
||||
@@ -193,14 +218,16 @@ describe('ChatService', () => {
|
||||
})
|
||||
|
||||
it('should list sessions of a user', async () => {
|
||||
const { user, project } = await setupBasicData()
|
||||
const { user, project, agent } = await setupBasicData()
|
||||
|
||||
await chatService.startOrContinueChat(user, {
|
||||
agentId: agent.id,
|
||||
textPrompt: 'chat 1',
|
||||
projectId: project.id,
|
||||
})
|
||||
|
||||
await chatService.startOrContinueChat(user, {
|
||||
agentId: agent.id,
|
||||
textPrompt: 'chat 2',
|
||||
projectId: project.id,
|
||||
})
|
||||
@@ -210,9 +237,10 @@ describe('ChatService', () => {
|
||||
})
|
||||
|
||||
it('should list messages mapped correctly', async () => {
|
||||
const { user, project } = await setupBasicData()
|
||||
const { user, project, agent } = await setupBasicData()
|
||||
|
||||
const { sessionId } = await chatService.startOrContinueChat(user, {
|
||||
agentId: agent.id,
|
||||
textPrompt: 'hello',
|
||||
projectId: project.id,
|
||||
})
|
||||
@@ -224,9 +252,10 @@ describe('ChatService', () => {
|
||||
})
|
||||
|
||||
it('should delete session and cascade', async () => {
|
||||
const { user, project } = await setupBasicData()
|
||||
const { user, project, agent } = await setupBasicData()
|
||||
|
||||
const { sessionId } = await chatService.startOrContinueChat(user, {
|
||||
agentId: agent.id,
|
||||
textPrompt: 'to delete',
|
||||
projectId: project.id,
|
||||
})
|
||||
|
||||
@@ -84,6 +84,7 @@ export class ChatService {
|
||||
req: ChatRequest,
|
||||
): Promise<{ sessionId: string; taskId: string }> {
|
||||
const {
|
||||
agentId,
|
||||
textPrompt,
|
||||
attachedFiles = [],
|
||||
assetIds = [],
|
||||
@@ -218,31 +219,9 @@ export class ChatService {
|
||||
const projectId = asset.project.id
|
||||
|
||||
// Ensure AI agent configuration exists
|
||||
const agentId = 'default'
|
||||
const userExists = await tx.user.findUnique({ where: { id: agentId } })
|
||||
if (!userExists) {
|
||||
await tx.user.create({
|
||||
data: {
|
||||
id: agentId,
|
||||
name: 'Ai Agent',
|
||||
email: `${agentId}@shumai.ai`,
|
||||
type: 'agent',
|
||||
},
|
||||
})
|
||||
}
|
||||
const agentExists = await tx.agent.findUnique({ where: { id: agentId } })
|
||||
if (!agentExists) {
|
||||
await tx.agent.create({
|
||||
data: {
|
||||
id: agentId,
|
||||
teamId,
|
||||
type: 'chat',
|
||||
config: {
|
||||
provider: 'openai',
|
||||
model: 'gpt-4',
|
||||
},
|
||||
},
|
||||
})
|
||||
const agent = await tx.agent.findUnique({ where: { id: agentId } })
|
||||
if (!agent) {
|
||||
throw new Error(`Agent with ID "${agentId}" not found`)
|
||||
}
|
||||
|
||||
let activeSessionId = passedSessionId
|
||||
|
||||
@@ -2,6 +2,7 @@ import { z } from 'zod'
|
||||
|
||||
export const chatRequestSchema = z
|
||||
.object({
|
||||
agentId: z.string(),
|
||||
textPrompt: z.string().optional(),
|
||||
attachedFiles: z.array(z.string()).optional(), // Uploaded file asset IDs
|
||||
assetIds: z.array(z.string()).optional(), // Workspace file/folder IDs referenced
|
||||
|
||||
Reference in New Issue
Block a user