( baseUrl: string, accessToken: string, title: string, timeoutMs: number, tags?: string[], )
| 24 | } |
| 25 | |
| 26 | export async function createCodeSession( |
| 27 | baseUrl: string, |
| 28 | accessToken: string, |
| 29 | title: string, |
| 30 | timeoutMs: number, |
| 31 | tags?: string[], |
| 32 | ): Promise<string | null> { |
| 33 | const url = `${baseUrl}/v1/code/sessions` |
| 34 | let response |
| 35 | try { |
| 36 | response = await axios.post( |
| 37 | url, |
| 38 | // bridge: {} is the positive signal for the oneof runner — omitting it |
| 39 | // (or sending environment_id: "") now 400s. BridgeRunner is an empty |
| 40 | // message today; it's a placeholder for future bridge-specific options. |
| 41 | { title, bridge: {}, ...(tags?.length ? { tags } : {}) }, |
| 42 | { |
| 43 | headers: oauthHeaders(accessToken), |
| 44 | timeout: timeoutMs, |
| 45 | validateStatus: s => s < 500, |
| 46 | }, |
| 47 | ) |
| 48 | } catch (err: unknown) { |
| 49 | logForDebugging( |
| 50 | `[code-session] Session create request failed: ${errorMessage(err)}`, |
| 51 | ) |
| 52 | return null |
| 53 | } |
| 54 | |
| 55 | if (response.status !== 200 && response.status !== 201) { |
| 56 | const detail = extractErrorDetail(response.data) |
| 57 | logForDebugging( |
| 58 | `[code-session] Session create failed ${response.status}${detail ? `: ${detail}` : ''}`, |
| 59 | ) |
| 60 | return null |
| 61 | } |
| 62 | |
| 63 | const data: unknown = response.data |
| 64 | if ( |
| 65 | !data || |
| 66 | typeof data !== 'object' || |
| 67 | !('session' in data) || |
| 68 | !data.session || |
| 69 | typeof data.session !== 'object' || |
| 70 | !('id' in data.session) || |
| 71 | typeof data.session.id !== 'string' || |
| 72 | !data.session.id.startsWith('cse_') |
| 73 | ) { |
| 74 | logForDebugging( |
| 75 | `[code-session] No session.id (cse_*) in response: ${jsonStringify(data).slice(0, 200)}`, |
| 76 | ) |
| 77 | return null |
| 78 | } |
| 79 | return data.session.id |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Credentials from POST /bridge. JWT is opaque — do not decode. |
no test coverage detected