axios+telemetry cleanup

This commit is contained in:
2026-04-02 15:19:11 +03:00
parent a3cbca1e11
commit 7e1eac8002
100 changed files with 3048 additions and 4491 deletions

View File

@@ -1,4 +1,4 @@
import axios from 'axios'
import { nativeRequest } from '../utils/http.js'
import { debugBody, extractErrorDetail } from './debugUtils.js'
import {
@@ -148,38 +148,26 @@ export function createBridgeApiClient(deps: BridgeApiDeps): BridgeApiClient {
const response = await withOAuthRetry(
(token: string) =>
axios.post<{
nativeRequest<{
environment_id: string
environment_secret: string
}>(
`${deps.baseUrl}/v1/environments/bridge`,
{
machine_name: config.machineName,
directory: config.dir,
branch: config.branch,
git_repo_url: config.gitRepoUrl,
// Advertise session capacity so claude.ai/code can show
// "2/4 sessions" badges and only block the picker when
// actually at capacity. Backends that don't yet accept
// this field will silently ignore it.
max_sessions: config.maxSessions,
// worker_type lets claude.ai filter environments by origin
// (e.g. assistant picker only shows assistant-mode workers).
// Desktop cowork app sends "cowork"; we send a distinct value.
metadata: { worker_type: config.workerType },
// Idempotent re-registration: if we have a backend-issued
// environment_id from a prior session (--session-id resume),
// send it back so the backend reattaches instead of creating
// a new env. The backend may still hand back a fresh ID if
// the old one expired — callers must compare the response.
...(config.reuseEnvironmentId && {
environment_id: config.reuseEnvironmentId,
}),
},
{
method: 'POST',
body: {
machine_name: config.machineName,
directory: config.dir,
branch: config.branch,
git_repo_url: config.gitRepoUrl,
max_sessions: config.maxSessions,
metadata: { worker_type: config.workerType },
...(config.reuseEnvironmentId && {
environment_id: config.reuseEnvironmentId,
}),
},
headers: getHeaders(token),
timeout: 15_000,
validateStatus: status => status < 500,
},
),
'Registration',
@@ -209,17 +197,16 @@ export function createBridgeApiClient(deps: BridgeApiDeps): BridgeApiClient {
const prevEmptyPolls = consecutiveEmptyPolls
consecutiveEmptyPolls = 0
const response = await axios.get<WorkResponse | null>(
`${deps.baseUrl}/v1/environments/${environmentId}/work/poll`,
const pollUrl = reclaimOlderThanMs !== undefined
? `${deps.baseUrl}/v1/environments/${environmentId}/work/poll?reclaim_older_than_ms=${reclaimOlderThanMs}`
: `${deps.baseUrl}/v1/environments/${environmentId}/work/poll`
const response = await nativeRequest<WorkResponse | null>(
pollUrl,
{
method: 'GET',
headers: getHeaders(environmentSecret),
params:
reclaimOlderThanMs !== undefined
? { reclaim_older_than_ms: reclaimOlderThanMs }
: undefined,
timeout: 10_000,
signal,
validateStatus: status => status < 500,
timeout: 10_000,
},
)
@@ -256,13 +243,13 @@ export function createBridgeApiClient(deps: BridgeApiDeps): BridgeApiClient {
debug(`[bridge:api] POST .../work/${workId}/ack`)
const response = await axios.post(
const response = await nativeRequest(
`${deps.baseUrl}/v1/environments/${environmentId}/work/${workId}/ack`,
{},
{
method: 'POST',
body: {},
headers: getHeaders(sessionToken),
timeout: 10_000,
validateStatus: s => s < 500,
},
)
@@ -282,13 +269,13 @@ export function createBridgeApiClient(deps: BridgeApiDeps): BridgeApiClient {
const response = await withOAuthRetry(
(token: string) =>
axios.post(
nativeRequest(
`${deps.baseUrl}/v1/environments/${environmentId}/work/${workId}/stop`,
{ force },
{
method: 'POST',
body: { force },
headers: getHeaders(token),
timeout: 10_000,
validateStatus: s => s < 500,
},
),
'StopWork',
@@ -305,12 +292,12 @@ export function createBridgeApiClient(deps: BridgeApiDeps): BridgeApiClient {
const response = await withOAuthRetry(
(token: string) =>
axios.delete(
nativeRequest(
`${deps.baseUrl}/v1/environments/bridge/${environmentId}`,
{
method: 'DELETE',
headers: getHeaders(token),
timeout: 10_000,
validateStatus: s => s < 500,
},
),
'Deregister',
@@ -329,13 +316,13 @@ export function createBridgeApiClient(deps: BridgeApiDeps): BridgeApiClient {
const response = await withOAuthRetry(
(token: string) =>
axios.post(
nativeRequest(
`${deps.baseUrl}/v1/sessions/${sessionId}/archive`,
{},
{
method: 'POST',
body: {},
headers: getHeaders(token),
timeout: 10_000,
validateStatus: s => s < 500,
},
),
'ArchiveSession',
@@ -368,13 +355,13 @@ export function createBridgeApiClient(deps: BridgeApiDeps): BridgeApiClient {
const response = await withOAuthRetry(
(token: string) =>
axios.post(
nativeRequest(
`${deps.baseUrl}/v1/environments/${environmentId}/bridge/reconnect`,
{ session_id: sessionId },
{
method: 'POST',
body: { session_id: sessionId },
headers: getHeaders(token),
timeout: 10_000,
validateStatus: s => s < 500,
},
),
'ReconnectSession',
@@ -394,18 +381,18 @@ export function createBridgeApiClient(deps: BridgeApiDeps): BridgeApiClient {
debug(`[bridge:api] POST .../work/${workId}/heartbeat`)
const response = await axios.post<{
const response = await nativeRequest<{
lease_extended: boolean
state: string
last_heartbeat: string
ttl_seconds: number
}>(
`${deps.baseUrl}/v1/environments/${environmentId}/work/${workId}/heartbeat`,
{},
{
method: 'POST',
body: {},
headers: getHeaders(sessionToken),
timeout: 10_000,
validateStatus: s => s < 500,
},
)
@@ -427,13 +414,13 @@ export function createBridgeApiClient(deps: BridgeApiDeps): BridgeApiClient {
`[bridge:api] POST /v1/sessions/${sessionId}/events type=${event.type}`,
)
const response = await axios.post(
const response = await nativeRequest(
`${deps.baseUrl}/v1/sessions/${sessionId}/events`,
{ events: [event] },
{
method: 'POST',
body: { events: [event] },
headers: getHeaders(sessionToken),
timeout: 10_000,
validateStatus: s => s < 500,
},
)