feat(agent): support aborting agent execution in local and temporal modes

This commit is contained in:
yiling
2026-07-14 10:25:06 +08:00
parent e772581542
commit 8a942d4f4e
15 changed files with 428 additions and 39 deletions
+1
View File
@@ -2,6 +2,7 @@ import { WorkflowTask } from '@shumai/db'
export interface Executor {
submit(task: WorkflowTask): Promise<string>
cancel?(taskId: string): Promise<void>
start(): void
close(): void
}
@@ -403,4 +403,21 @@ describe('LocalExecutor Integration Tests', () => {
expect(executor2.getAgentConcurrencyLimit()).toBe(5)
})
})
describe('Cancellation', () => {
it('registers and triggers local cancel handlers', async () => {
const { registerLocalCancelHandler, unregisterLocalCancelHandler } =
await import('./workflow-utils')
const cancelSpy = vi.fn()
const taskId = 'task-cancel-test'
registerLocalCancelHandler(taskId, cancelSpy)
await executor.cancel(taskId)
expect(cancelSpy).toHaveBeenCalled()
unregisterLocalCancelHandler(taskId)
})
})
})
+5 -1
View File
@@ -3,7 +3,7 @@ import { WorkflowTask, WorkflowTaskStatus, WorkflowTaskType } from '@shumai/db'
import { Executor } from './executor'
import * as taskActivities from './activities/task'
import { logger } from '@shumai/core/src/logger'
import { getConcurrencyLimit } from './workflow-utils'
import { getConcurrencyLimit, triggerLocalCancel } from './workflow-utils'
type WorkflowFn = (task: WorkflowTask) => Promise<void>
@@ -165,6 +165,10 @@ export class LocalExecutor implements Executor {
return task.id
}
async cancel(taskId: string): Promise<void> {
triggerLocalCancel(taskId)
}
start(): void {
if (this.interval) return
this.interval = setInterval(() => this.tick(), 5000)
@@ -50,6 +50,13 @@ export class TemporalExecutor implements Executor {
return handle.workflowId
}
async cancel(taskId: string): Promise<void> {
const client = await this.getClient()
const workflowId = `chat-${taskId}`
const handle = client.workflow.getHandle(workflowId)
await handle.cancel()
}
start(): void {
// Client doesn't need explicit start
}
@@ -203,3 +203,27 @@ export function getConcurrencyLimit(envVar: string | undefined, defaultValue: nu
}
return parsed
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const globalObjWithCancel = globalThis as any
if (!globalObjWithCancel.__localCancelHandlers) {
globalObjWithCancel.__localCancelHandlers = new Map<string, () => void>()
}
const localCancelHandlers: Map<string, () => void> = globalObjWithCancel.__localCancelHandlers
export function registerLocalCancelHandler(taskId: string, cancelFn: () => void) {
localCancelHandlers.set(taskId, cancelFn)
}
export function unregisterLocalCancelHandler(taskId: string) {
localCancelHandlers.delete(taskId)
}
export function triggerLocalCancel(taskId: string): boolean {
const handler = localCancelHandlers.get(taskId)
if (handler) {
handler()
return true
}
return false
}
+6
View File
@@ -23,6 +23,12 @@ export class WorkflowService {
return this.executor.submit(task)
}
async cancel(taskId: string): Promise<void> {
if (this.executor.cancel) {
await this.executor.cancel(taskId)
}
}
async executeWait(task: WorkflowTask, timeoutMs: number = 30000): Promise<WorkflowTask> {
const start = Date.now()
while (Date.now() - start < timeoutMs) {