(client, threadId, startRequest, options = {})
| 557 | } |
| 558 | |
| 559 | async function captureTurn(client, threadId, startRequest, options = {}) { |
| 560 | const state = createTurnCaptureState(threadId, options); |
| 561 | const previousHandler = client.notificationHandler; |
| 562 | |
| 563 | client.setNotificationHandler((message) => { |
| 564 | if (!state.turnId) { |
| 565 | state.bufferedNotifications.push(message); |
| 566 | return; |
| 567 | } |
| 568 | |
| 569 | if (message.method === "thread/started" || message.method === "thread/name/updated") { |
| 570 | applyTurnNotification(state, message); |
| 571 | return; |
| 572 | } |
| 573 | |
| 574 | if (!belongsToTurn(state, message)) { |
| 575 | if (previousHandler) { |
| 576 | previousHandler(message); |
| 577 | } |
| 578 | return; |
| 579 | } |
| 580 | |
| 581 | applyTurnNotification(state, message); |
| 582 | }); |
| 583 | |
| 584 | try { |
| 585 | const response = await startRequest(); |
| 586 | options.onResponse?.(response, state); |
| 587 | state.turnId = response.turn?.id ?? null; |
| 588 | if (state.turnId) { |
| 589 | state.threadTurnIds.set(state.threadId, state.turnId); |
| 590 | } |
| 591 | for (const message of state.bufferedNotifications) { |
| 592 | if (belongsToTurn(state, message)) { |
| 593 | applyTurnNotification(state, message); |
| 594 | } else { |
| 595 | if (previousHandler) { |
| 596 | previousHandler(message); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | state.bufferedNotifications.length = 0; |
| 601 | |
| 602 | if (response.turn?.status && response.turn.status !== "inProgress") { |
| 603 | completeTurn(state, response.turn); |
| 604 | } |
| 605 | |
| 606 | return await state.completion; |
| 607 | } finally { |
| 608 | clearCompletionTimer(state); |
| 609 | client.setNotificationHandler(previousHandler ?? null); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | async function withAppServer(cwd, fn) { |
| 614 | let client = null; |
no test coverage detected