execution-manager.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import type { Ability, AbilityExecution, ExecutorContext } from '../types/index.js'
  2. import { executeAbility } from './index.js'
  3. /**
  4. * Minimal ExecutionManager
  5. *
  6. * Simplified to track SINGLE execution at a time.
  7. * No session management, no cleanup timers, no multi-execution.
  8. *
  9. * This is the bare minimum to test the core concept.
  10. */
  11. export class ExecutionManager {
  12. private activeExecution: AbilityExecution | null = null
  13. async execute(
  14. ability: Ability,
  15. inputs: Record<string, unknown>,
  16. ctx: ExecutorContext
  17. ): Promise<AbilityExecution> {
  18. // Block concurrent executions
  19. if (this.activeExecution && this.activeExecution.status === 'running') {
  20. throw new Error(`Already executing ability: ${this.activeExecution.ability.name}`)
  21. }
  22. console.log(`[abilities] Starting execution: ${ability.name}`)
  23. const execution = await executeAbility(ability, inputs, ctx)
  24. this.activeExecution = execution
  25. // Clear active if completed/failed
  26. if (execution.status !== 'running') {
  27. this.activeExecution = null
  28. }
  29. return execution
  30. }
  31. getActive(): AbilityExecution | null {
  32. return this.activeExecution
  33. }
  34. cancel(): boolean {
  35. if (!this.activeExecution) return false
  36. if (this.activeExecution.status === 'running') {
  37. this.activeExecution.status = 'failed'
  38. this.activeExecution.error = 'Cancelled by user'
  39. this.activeExecution.completedAt = Date.now()
  40. this.activeExecution = null
  41. return true
  42. }
  43. return false
  44. }
  45. cleanup(): void {
  46. this.activeExecution = null
  47. }
  48. }