(cwd, fn)
| 611 | } |
| 612 | |
| 613 | async function withAppServer(cwd, fn) { |
| 614 | let client = null; |
| 615 | try { |
| 616 | client = await CodexAppServerClient.connect(cwd); |
| 617 | const result = await fn(client); |
| 618 | await client.close(); |
| 619 | return result; |
| 620 | } catch (error) { |
| 621 | const brokerRequested = client?.transport === "broker" || Boolean(process.env[BROKER_ENDPOINT_ENV]); |
| 622 | const shouldRetryDirect = |
| 623 | (client?.transport === "broker" && error?.rpcCode === BROKER_BUSY_RPC_CODE) || |
| 624 | (brokerRequested && (error?.code === "ENOENT" || error?.code === "ECONNREFUSED")); |
| 625 | |
| 626 | if (client) { |
| 627 | await client.close().catch(() => {}); |
| 628 | client = null; |
| 629 | } |
| 630 | |
| 631 | if (!shouldRetryDirect) { |
| 632 | throw error; |
| 633 | } |
| 634 | |
| 635 | const directClient = await CodexAppServerClient.connect(cwd, { disableBroker: true }); |
| 636 | try { |
| 637 | return await fn(directClient); |
| 638 | } finally { |
| 639 | await directClient.close(); |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | async function withDirectAppServer(cwd, fn) { |
| 645 | const client = await CodexAppServerClient.connect(cwd, { disableBroker: true }); |
no test coverage detected