| 1087 | |
| 1088 | let result: WrapperSessionReadyResponse; |
| 1089 | try { |
| 1090 | result = await pending.promise; |
| 1091 | } finally { |
| 1092 | if (inFlight === pending) inFlight = undefined; |
| 1093 | } |
| 1094 | if (result.status === 'error') { |
| 1095 | const status = result.error.code === 'INVALID_REQUEST' ? 400 : 503; |
| 1096 | return jsonResponse( |
| 1097 | { |
| 1098 | error: result.error.code, |
| 1099 | message: result.error.message, |
| 1100 | ...(result.error.subtype ? { subtype: result.error.subtype } : {}), |
| 1101 | ...(result.error.detail ? { detail: result.error.detail } : {}), |
| 1102 | ...(result.error.retryable !== undefined ? { retryable: result.error.retryable } : {}), |
| 1103 | ...(result.error.wrapperRunId ? { wrapperRunId: result.error.wrapperRunId } : {}), |
| 1104 | }, |
| 1105 | status |
| 1106 | ); |
| 1107 | } |
| 1108 | |
| 1109 | return jsonResponse(result); |
| 1110 | }; |
| 1111 | } |
| 1112 | |
| 1113 | export function createRuntimeEnvironmentHandler(deps: ServerDependencies) { |
| 1114 | return async (req: Request): Promise<Response> => { |
| 1115 | if (!deps.updateRuntimeEnvironment) { |
| 1116 | return errorResponse('NOT_READY', 'Runtime environment updater is not configured', 503); |
| 1117 | } |
| 1118 | |
| 1119 | let body: unknown; |
| 1120 | try { |
| 1121 | body = await req.json(); |
| 1122 | } catch { |
| 1123 | return errorResponse('INVALID_REQUEST', 'Invalid JSON body', 400); |
| 1124 | } |
| 1125 | if ( |
| 1126 | typeof body !== 'object' || |
| 1127 | body === null || |
| 1128 | !('env' in body) || |
| 1129 | typeof body.env !== 'object' || |
| 1130 | body.env === null || |
| 1131 | Array.isArray(body.env) |
| 1132 | ) { |
| 1133 | return errorResponse('INVALID_REQUEST', 'Invalid runtime environment request', 400); |
| 1134 | } |
| 1135 | |
| 1136 | const env: Record<string, string> = {}; |
| 1137 | for (const [name, value] of Object.entries(body.env)) { |
| 1138 | if (typeof value !== 'string') { |
| 1139 | return errorResponse('INVALID_REQUEST', 'Invalid runtime environment request', 400); |
| 1140 | } |
| 1141 | env[name] = value; |
| 1142 | } |
| 1143 | |
| 1144 | try { |
| 1145 | await deps.updateRuntimeEnvironment(env); |
| 1146 | } catch { |