mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-03 06:24:30 +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',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
|
agentId: 'agent1',
|
||||||
textPrompt: 'hi',
|
textPrompt: 'hi',
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -48,13 +48,35 @@ describe('ChatService', () => {
|
|||||||
data: { rootFolderId: rootFolder.id },
|
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 () => {
|
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, {
|
const { sessionId, taskId } = await chatService.startOrContinueChat(user, {
|
||||||
|
agentId: agent.id,
|
||||||
textPrompt: 'hello world',
|
textPrompt: 'hello world',
|
||||||
projectId: project.id,
|
projectId: project.id,
|
||||||
})
|
})
|
||||||
@@ -98,10 +120,11 @@ describe('ChatService', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should continue an existing chat session', async () => {
|
it('should continue an existing chat session', async () => {
|
||||||
const { user, project } = await setupBasicData()
|
const { user, project, agent } = await setupBasicData()
|
||||||
|
|
||||||
// Start
|
// Start
|
||||||
const { sessionId } = await chatService.startOrContinueChat(user, {
|
const { sessionId } = await chatService.startOrContinueChat(user, {
|
||||||
|
agentId: agent.id,
|
||||||
textPrompt: 'first message',
|
textPrompt: 'first message',
|
||||||
projectId: project.id,
|
projectId: project.id,
|
||||||
})
|
})
|
||||||
@@ -110,6 +133,7 @@ describe('ChatService', () => {
|
|||||||
const { sessionId: nextSessionId, taskId: nextTaskId } = await chatService.startOrContinueChat(
|
const { sessionId: nextSessionId, taskId: nextTaskId } = await chatService.startOrContinueChat(
|
||||||
user,
|
user,
|
||||||
{
|
{
|
||||||
|
agentId: agent.id,
|
||||||
textPrompt: 'second message',
|
textPrompt: 'second message',
|
||||||
sessionId,
|
sessionId,
|
||||||
},
|
},
|
||||||
@@ -140,7 +164,7 @@ describe('ChatService', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should inject context of referenced assets and attached files', async () => {
|
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({
|
const childFolder = await prisma.asset.create({
|
||||||
data: {
|
data: {
|
||||||
@@ -171,6 +195,7 @@ describe('ChatService', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const { taskId } = await chatService.startOrContinueChat(user, {
|
const { taskId } = await chatService.startOrContinueChat(user, {
|
||||||
|
agentId: agent.id,
|
||||||
textPrompt: 'process file',
|
textPrompt: 'process file',
|
||||||
projectId: project.id,
|
projectId: project.id,
|
||||||
assetIds: [childFolder.id],
|
assetIds: [childFolder.id],
|
||||||
@@ -193,14 +218,16 @@ describe('ChatService', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should list sessions of a user', async () => {
|
it('should list sessions of a user', async () => {
|
||||||
const { user, project } = await setupBasicData()
|
const { user, project, agent } = await setupBasicData()
|
||||||
|
|
||||||
await chatService.startOrContinueChat(user, {
|
await chatService.startOrContinueChat(user, {
|
||||||
|
agentId: agent.id,
|
||||||
textPrompt: 'chat 1',
|
textPrompt: 'chat 1',
|
||||||
projectId: project.id,
|
projectId: project.id,
|
||||||
})
|
})
|
||||||
|
|
||||||
await chatService.startOrContinueChat(user, {
|
await chatService.startOrContinueChat(user, {
|
||||||
|
agentId: agent.id,
|
||||||
textPrompt: 'chat 2',
|
textPrompt: 'chat 2',
|
||||||
projectId: project.id,
|
projectId: project.id,
|
||||||
})
|
})
|
||||||
@@ -210,9 +237,10 @@ describe('ChatService', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should list messages mapped correctly', async () => {
|
it('should list messages mapped correctly', async () => {
|
||||||
const { user, project } = await setupBasicData()
|
const { user, project, agent } = await setupBasicData()
|
||||||
|
|
||||||
const { sessionId } = await chatService.startOrContinueChat(user, {
|
const { sessionId } = await chatService.startOrContinueChat(user, {
|
||||||
|
agentId: agent.id,
|
||||||
textPrompt: 'hello',
|
textPrompt: 'hello',
|
||||||
projectId: project.id,
|
projectId: project.id,
|
||||||
})
|
})
|
||||||
@@ -224,9 +252,10 @@ describe('ChatService', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should delete session and cascade', async () => {
|
it('should delete session and cascade', async () => {
|
||||||
const { user, project } = await setupBasicData()
|
const { user, project, agent } = await setupBasicData()
|
||||||
|
|
||||||
const { sessionId } = await chatService.startOrContinueChat(user, {
|
const { sessionId } = await chatService.startOrContinueChat(user, {
|
||||||
|
agentId: agent.id,
|
||||||
textPrompt: 'to delete',
|
textPrompt: 'to delete',
|
||||||
projectId: project.id,
|
projectId: project.id,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -84,6 +84,7 @@ export class ChatService {
|
|||||||
req: ChatRequest,
|
req: ChatRequest,
|
||||||
): Promise<{ sessionId: string; taskId: string }> {
|
): Promise<{ sessionId: string; taskId: string }> {
|
||||||
const {
|
const {
|
||||||
|
agentId,
|
||||||
textPrompt,
|
textPrompt,
|
||||||
attachedFiles = [],
|
attachedFiles = [],
|
||||||
assetIds = [],
|
assetIds = [],
|
||||||
@@ -218,31 +219,9 @@ export class ChatService {
|
|||||||
const projectId = asset.project.id
|
const projectId = asset.project.id
|
||||||
|
|
||||||
// Ensure AI agent configuration exists
|
// Ensure AI agent configuration exists
|
||||||
const agentId = 'default'
|
const agent = await tx.agent.findUnique({ where: { id: agentId } })
|
||||||
const userExists = await tx.user.findUnique({ where: { id: agentId } })
|
if (!agent) {
|
||||||
if (!userExists) {
|
throw new Error(`Agent with ID "${agentId}" not found`)
|
||||||
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',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let activeSessionId = passedSessionId
|
let activeSessionId = passedSessionId
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { z } from 'zod'
|
|||||||
|
|
||||||
export const chatRequestSchema = z
|
export const chatRequestSchema = z
|
||||||
.object({
|
.object({
|
||||||
|
agentId: z.string(),
|
||||||
textPrompt: z.string().optional(),
|
textPrompt: z.string().optional(),
|
||||||
attachedFiles: z.array(z.string()).optional(), // Uploaded file asset IDs
|
attachedFiles: z.array(z.string()).optional(), // Uploaded file asset IDs
|
||||||
assetIds: z.array(z.string()).optional(), // Workspace file/folder IDs referenced
|
assetIds: z.array(z.string()).optional(), // Workspace file/folder IDs referenced
|
||||||
|
|||||||
Reference in New Issue
Block a user