()
| 668 | ): Promise<HandleResult> { |
| 669 | const cs = await this.getOrCreateSession(adapter, channelId); |
| 670 | const run = async (): Promise<HandleResult> => { |
| 671 | cs.running = true; |
| 672 | |
| 673 | let turnHadVisibleOutput = false; |
| 674 | let sawAgentStart = false; |
| 675 | let sawAgentEnd = false; |
| 676 | const runId = randomUUID(); |
| 677 | let unsubscribe = () => undefined; |
| 678 | const waitForQueuedAgentEvents = async (): Promise<void> => { |
| 679 | const maybeQueue = (cs.session as { _agentEventQueue?: unknown }) |
| 680 | ._agentEventQueue; |
| 681 | if ( |
| 682 | !maybeQueue || |
| 683 | typeof (maybeQueue as Promise<unknown>).then !== "function" |
| 684 | ) { |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | try { |
| 689 | await maybeQueue; |
| 690 | } catch (error) { |
| 691 | log("[PackAgent] Waiting for queued agent events failed:", error); |
| 692 | } |
| 693 | }; |
| 694 | |
| 695 | try { |
| 696 | if (this.pendingAbortChannels.delete(channelId)) { |
| 697 | return { stopReason: "aborted" }; |
| 698 | } |
| 699 | |
| 700 | const forwardAgentEvent = (event: AgentEvent): void => { |
| 701 | if (event.type === "agent_start") { |
| 702 | sawAgentStart = true; |
| 703 | } else if (event.type === "agent_end") { |
| 704 | if (sawAgentEnd) { |
| 705 | return; |
| 706 | } |
| 707 | sawAgentEnd = true; |
| 708 | } |
| 709 | |
| 710 | onEvent(event); |
| 711 | }; |
| 712 | |
| 713 | // Wire up file output callback for this run |
| 714 | cs.fileOutputCallbackRef.current = (event) => { |
| 715 | forwardAgentEvent(event); |
| 716 | }; |
| 717 | cs.delegatedToolRunContextRef.current = { |
| 718 | runId, |
| 719 | channelId, |
| 720 | adapter, |
| 721 | }; |
| 722 | |
| 723 | // Subscribe to agent events and forward to adapter |
| 724 | unsubscribe = cs.session.subscribe((event: any) => { |
| 725 | switch (event.type) { |
| 726 | case "agent_start": |
| 727 | log("\n=== [AGENT SESSION START] ==="); |
no test coverage detected