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