@minhspark/codex-mcp-bridge 1.12.1 → 1.12.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/turn.mjs CHANGED
@@ -54,10 +54,16 @@ export async function runTurn(client, { threadId, input, timeoutMs = 240000, tur
54
54
  });
55
55
 
56
56
  const process = (msg) => {
57
+ if (settled) return;
57
58
  const params = msg.params ?? {};
58
59
  if (turnId && params.turnId && params.turnId !== turnId) return;
59
60
 
60
61
  switch (msg.method) {
62
+ case "thread/closed": {
63
+ settled = true;
64
+ resolveDone({ status: "disconnected", error: { message: "The thread closed before its turn completed" } });
65
+ return;
66
+ }
61
67
  case "item/completed": {
62
68
  const summary = summarizeItem(params.item);
63
69
  if (!summary) return;
@@ -90,7 +96,7 @@ export async function runTurn(client, { threadId, input, timeoutMs = 240000, tur
90
96
  };
91
97
 
92
98
  const unsubscribe = client.subscribe(threadId, (msg) => {
93
- if (!turnId) {
99
+ if (!turnId && msg.method !== "thread/closed") {
94
100
  buffered.push(msg);
95
101
  return;
96
102
  }
@@ -113,15 +119,27 @@ export async function runTurn(client, { threadId, input, timeoutMs = 240000, tur
113
119
  });
114
120
 
115
121
  try {
116
- const started = await client.request(
117
- "turn/start",
118
- { threadId, input, ...turnOverrides },
119
- { timeoutMs: Math.min(timeoutMs, 60000) },
120
- );
121
- turnId = started?.turn?.id ?? null;
122
- for (const msg of buffered.splice(0)) process(msg);
123
-
124
- const outcome = await done;
122
+ const start = await Promise.race([
123
+ client.request(
124
+ "turn/start",
125
+ { ...turnOverrides, threadId, input },
126
+ { timeoutMs: Math.min(timeoutMs, 60000) },
127
+ ).then((started) => ({ started }), (error) => ({ error })),
128
+ done.then((outcome) => ({ outcome })),
129
+ ]);
130
+ if (start.error) throw start.error;
131
+ let outcome = start.outcome;
132
+ if (!outcome) {
133
+ turnId = start.started?.turn?.id ?? null;
134
+ if (typeof turnId !== "string" || !turnId.trim()) {
135
+ throw new Error("Codex app-server did not return a turn id for turn/start");
136
+ }
137
+ for (const msg of buffered.splice(0)) process(msg);
138
+ if (TERMINAL_STATUSES.has(start.started?.turn?.status)) {
139
+ process({ method: "turn/completed", params: { turn: start.started.turn } });
140
+ }
141
+ outcome = await done;
142
+ }
125
143
  return {
126
144
  threadId,
127
145
  turnId,