MCPcopy
hub / github.com/openai/codex-plugin-cc / captureTurn

Function captureTurn

plugins/codex/scripts/lib/codex.mjs:559–611  ·  view source on GitHub ↗
(client, threadId, startRequest, options = {})

Source from the content-addressed store, hash-verified

557}
558
559async 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
613async function withAppServer(cwd, fn) {
614 let client = null;

Callers 2

runAppServerReviewFunction · 0.85
runAppServerTurnFunction · 0.85

Calls 6

createTurnCaptureStateFunction · 0.85
applyTurnNotificationFunction · 0.85
belongsToTurnFunction · 0.85
completeTurnFunction · 0.85
clearCompletionTimerFunction · 0.85

Tested by

no test coverage detected