| 20 | export const dynamic = 'force-dynamic' |
| 21 | |
| 22 | async function settleWithConcurrency<T, R>( |
| 23 | items: T[], |
| 24 | concurrency: number, |
| 25 | task: (item: T) => Promise<R> |
| 26 | ): Promise<Array<PromiseSettledResult<R>>> { |
| 27 | const results: Array<PromiseSettledResult<R> | undefined> = new Array(items.length) |
| 28 | let nextIndex = 0 |
| 29 | |
| 30 | const workers = Array.from({ length: Math.min(concurrency, items.length) }, async () => { |
| 31 | while (nextIndex < items.length) { |
| 32 | const index = nextIndex |
| 33 | nextIndex += 1 |
| 34 | try { |
| 35 | results[index] = { status: 'fulfilled', value: await task(items[index]) } |
| 36 | } catch (reason) { |
| 37 | results[index] = { status: 'rejected', reason } |
| 38 | } |
| 39 | } |
| 40 | }) |
| 41 | |
| 42 | await Promise.all(workers) |
| 43 | |
| 44 | return results.map( |
| 45 | (result) => |
| 46 | result ?? { |
| 47 | status: 'rejected', |
| 48 | reason: new Error('MCP refresh discovery task did not run'), |
| 49 | } |
| 50 | ) |
| 51 | } |
| 52 | |
| 53 | export const GET = withRouteHandler( |
| 54 | withMcpAuth('read')(async (request: NextRequest, { userId, workspaceId, requestId }) => { |