(taskId: string, getAppState: () => AppState, setAppState: SetAppState)
| 619 | * @returns true if backgrounded successfully, false otherwise |
| 620 | */ |
| 621 | export function backgroundAgentTask(taskId: string, getAppState: () => AppState, setAppState: SetAppState): boolean { |
| 622 | const state = getAppState(); |
| 623 | const task = state.tasks[taskId]; |
| 624 | if (!isLocalAgentTask(task) || task.isBackgrounded) { |
| 625 | return false; |
| 626 | } |
| 627 | |
| 628 | // Update state to mark as backgrounded |
| 629 | setAppState(prev => { |
| 630 | const prevTask = prev.tasks[taskId]; |
| 631 | if (!isLocalAgentTask(prevTask)) { |
| 632 | return prev; |
| 633 | } |
| 634 | return { |
| 635 | ...prev, |
| 636 | tasks: { |
| 637 | ...prev.tasks, |
| 638 | [taskId]: { |
| 639 | ...prevTask, |
| 640 | isBackgrounded: true |
| 641 | } |
| 642 | } |
| 643 | }; |
| 644 | }); |
| 645 | |
| 646 | // Resolve the background signal to interrupt the agent loop |
| 647 | const resolver = backgroundSignalResolvers.get(taskId); |
| 648 | if (resolver) { |
| 649 | resolver(); |
| 650 | backgroundSignalResolvers.delete(taskId); |
| 651 | } |
| 652 | return true; |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Unregister a foreground agent task when the agent completes without being backgrounded. |
no test coverage detected