mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 17:37:15 +00:00
feat(agent): support aborting agent execution in local and temporal modes
This commit is contained in:
@@ -4,6 +4,12 @@ import chatRoute from './chat'
|
||||
import { chatService } from '@shumai/core/src/chat/chat'
|
||||
import { authzService } from '@shumai/core/src/authz/authz'
|
||||
|
||||
vi.mock('@shumai/workflow-core', () => ({
|
||||
workflowService: {
|
||||
cancel: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@shumai/core/src/chat/chat', async (importOriginal) => {
|
||||
const original = await importOriginal<typeof import('@shumai/core/src/chat/chat')>()
|
||||
return {
|
||||
@@ -24,11 +30,13 @@ vi.mock('@shumai/db', () => ({
|
||||
},
|
||||
workflowTask: {
|
||||
findUnique: vi.fn(),
|
||||
findMany: vi.fn(),
|
||||
update: vi.fn(),
|
||||
},
|
||||
},
|
||||
}))
|
||||
|
||||
import { prisma } from '@shumai/db'
|
||||
import { prisma, type WorkflowTask } from '@shumai/db'
|
||||
|
||||
describe('Chat API', () => {
|
||||
const app = new Hono<{ Variables: { user: { id: string; name: string } } }>()
|
||||
@@ -196,4 +204,54 @@ describe('Chat API', () => {
|
||||
expect(text).toContain('{"type":"ping"}')
|
||||
})
|
||||
})
|
||||
|
||||
describe('POST /teams/:teamId/chat/sessions/:sessionId/abort', () => {
|
||||
it('aborts an active chat session and updates task status', async () => {
|
||||
const mockActiveTasks = [
|
||||
{
|
||||
id: 'task1',
|
||||
status: 'processing',
|
||||
type: 'chat',
|
||||
payload: {
|
||||
agent: {
|
||||
sessionId: 'session1',
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
vi.mocked(prisma.workflowTask.findMany).mockResolvedValue(mockActiveTasks as unknown as WorkflowTask[])
|
||||
vi.mocked(prisma.workflowTask.update).mockResolvedValue({} as unknown as WorkflowTask)
|
||||
|
||||
const { workflowService } = await import('@shumai/workflow-core')
|
||||
|
||||
const res = await app.request('/teams/team1/chat/sessions/session1/abort', {
|
||||
method: 'POST',
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
expect(data).toEqual({ success: true })
|
||||
|
||||
expect(workflowService.cancel).toHaveBeenCalledWith('task1')
|
||||
expect(prisma.workflowTask.update).toHaveBeenCalledWith({
|
||||
where: { id: 'task1' },
|
||||
data: {
|
||||
status: 'failed',
|
||||
output: { error: 'aborted' },
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it('returns 404 if no active task is found for the session', async () => {
|
||||
vi.mocked(prisma.workflowTask.findMany).mockResolvedValue([])
|
||||
|
||||
const res = await app.request('/teams/team1/chat/sessions/session1/abort', {
|
||||
method: 'POST',
|
||||
})
|
||||
|
||||
expect(res.status).toBe(404)
|
||||
const data = await res.json()
|
||||
expect(data.error).toBe('No active execution found for this session')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -6,6 +6,7 @@ import { chatService, mapEntryToMessage } from '@shumai/core/src/chat/chat'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { chatRequestSchema, paginationParamsSchema } from '@shumai/dtos'
|
||||
import type { Prisma } from '@shumai/db'
|
||||
import { workflowService } from '@shumai/workflow-core'
|
||||
|
||||
type User = Prisma.UserGetPayload<Record<string, never>>
|
||||
|
||||
@@ -173,5 +174,44 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
}
|
||||
})
|
||||
})
|
||||
.post('/teams/:teamId/chat/sessions/:sessionId/abort', async (c) => {
|
||||
const user = c.get('user')
|
||||
const teamId = c.req.param('teamId')
|
||||
const sessionId = c.req.param('sessionId')
|
||||
|
||||
await authzService.hasPermission({
|
||||
user,
|
||||
permission: Permission.Read,
|
||||
type: ResourceType.Team,
|
||||
id: teamId,
|
||||
})
|
||||
|
||||
const activeTasks = await prisma.workflowTask.findMany({
|
||||
where: {
|
||||
status: { in: ['pending', 'processing'] },
|
||||
type: 'chat',
|
||||
},
|
||||
})
|
||||
|
||||
const taskToAbort = activeTasks.find(
|
||||
(t) => (t.payload as PrismaJson.WorkflowTaskPayload)?.agent?.sessionId === sessionId,
|
||||
)
|
||||
|
||||
if (!taskToAbort) {
|
||||
return c.json({ error: 'No active execution found for this session' }, 404)
|
||||
}
|
||||
|
||||
await workflowService.cancel(taskToAbort.id)
|
||||
|
||||
await prisma.workflowTask.update({
|
||||
where: { id: taskToAbort.id },
|
||||
data: {
|
||||
status: 'failed',
|
||||
output: { error: 'aborted' },
|
||||
},
|
||||
})
|
||||
|
||||
return c.json({ success: true }, 200)
|
||||
})
|
||||
|
||||
export default route
|
||||
|
||||
Reference in New Issue
Block a user