forked from ultraworkers/claw-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
466 lines (407 loc) · 13 KB
/
Copy pathapi.ts
File metadata and controls
466 lines (407 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import axios, { type AxiosRequestConfig, type AxiosResponse } from 'axios'
import { randomUUID } from 'crypto'
import { getOauthConfig } from 'src/constants/oauth.js'
import { getOrganizationUUID } from 'src/services/oauth/client.js'
import z from 'zod/v4'
import { getClaudeAIOAuthTokens } from '../auth.js'
import { logForDebugging } from '../debug.js'
import { parseGitHubRepository } from '../detectRepository.js'
import { errorMessage, toError } from '../errors.js'
import { lazySchema } from '../lazySchema.js'
import { logError } from '../log.js'
import { sleep } from '../sleep.js'
import { jsonStringify } from '../slowOperations.js'
// Retry configuration for teleport API requests
const TELEPORT_RETRY_DELAYS = [2000, 4000, 8000, 16000] // 4 retries with exponential backoff
const MAX_TELEPORT_RETRIES = TELEPORT_RETRY_DELAYS.length
export const CCR_BYOC_BETA = 'ccr-byoc-2025-07-29'
/**
* Checks if an axios error is a transient network error that should be retried
*/
export function isTransientNetworkError(error: unknown): boolean {
if (!axios.isAxiosError(error)) {
return false
}
// Retry on network errors (no response received)
if (!error.response) {
return true
}
// Retry on server errors (5xx)
if (error.response.status >= 500) {
return true
}
// Don't retry on client errors (4xx) - they're not transient
return false
}
/**
* Makes an axios GET request with automatic retry for transient network errors
* Uses exponential backoff: 2s, 4s, 8s, 16s (4 retries = 5 total attempts)
*/
export async function axiosGetWithRetry<T>(
url: string,
config?: AxiosRequestConfig,
): Promise<AxiosResponse<T>> {
let lastError: unknown
for (let attempt = 0; attempt <= MAX_TELEPORT_RETRIES; attempt++) {
try {
return await axios.get<T>(url, config)
} catch (error) {
lastError = error
// Don't retry if this isn't a transient error
if (!isTransientNetworkError(error)) {
throw error
}
// Don't retry if we've exhausted all retries
if (attempt >= MAX_TELEPORT_RETRIES) {
logForDebugging(
`Teleport request failed after ${attempt + 1} attempts: ${errorMessage(error)}`,
)
throw error
}
const delay = TELEPORT_RETRY_DELAYS[attempt] ?? 2000
logForDebugging(
`Teleport request failed (attempt ${attempt + 1}/${MAX_TELEPORT_RETRIES + 1}), retrying in ${delay}ms: ${errorMessage(error)}`,
)
await sleep(delay)
}
}
throw lastError
}
// Types matching the actual Sessions API response from api/schemas/sessions/sessions.py
export type SessionStatus = 'requires_action' | 'running' | 'idle' | 'archived'
export type GitSource = {
type: 'git_repository'
url: string
revision?: string | null
allow_unrestricted_git_push?: boolean
}
export type KnowledgeBaseSource = {
type: 'knowledge_base'
knowledge_base_id: string
}
export type SessionContextSource = GitSource | KnowledgeBaseSource
// Outcome types from api/schemas/sandbox.py
export type OutcomeGitInfo = {
type: 'github'
repo: string
branches: string[]
}
export type GitRepositoryOutcome = {
type: 'git_repository'
git_info: OutcomeGitInfo
}
export type Outcome = GitRepositoryOutcome
export type SessionContext = {
sources: SessionContextSource[]
cwd: string
outcomes: Outcome[] | null
custom_system_prompt: string | null
append_system_prompt: string | null
model: string | null
// Seed filesystem with a git bundle on Files API
seed_bundle_file_id?: string
github_pr?: { owner: string; repo: string; number: number }
reuse_outcome_branches?: boolean
}
export type SessionResource = {
type: 'session'
id: string
title: string | null
session_status: SessionStatus
environment_id: string
created_at: string
updated_at: string
session_context: SessionContext
}
export type ListSessionsResponse = {
data: SessionResource[]
has_more: boolean
first_id: string | null
last_id: string | null
}
export const CodeSessionSchema = lazySchema(() =>
z.object({
id: z.string(),
title: z.string(),
description: z.string(),
status: z.enum([
'idle',
'working',
'waiting',
'completed',
'archived',
'cancelled',
'rejected',
]),
repo: z
.object({
name: z.string(),
owner: z.object({
login: z.string(),
}),
default_branch: z.string().optional(),
})
.nullable(),
turns: z.array(z.string()),
created_at: z.string(),
updated_at: z.string(),
}),
)
// Export the inferred type from the Zod schema
export type CodeSession = z.infer<ReturnType<typeof CodeSessionSchema>>
/**
* Validates and prepares for API requests
* @returns Object containing access token and organization UUID
*/
export async function prepareApiRequest(): Promise<{
accessToken: string
orgUUID: string
}> {
const accessToken = getClaudeAIOAuthTokens()?.accessToken
if (accessToken === undefined) {
throw new Error(
'Claude Code web sessions require authentication with a Claude.ai account. API key authentication is not sufficient. Please run /login to authenticate, or check your authentication status with /status.',
)
}
const orgUUID = await getOrganizationUUID()
if (!orgUUID) {
throw new Error('Unable to get organization UUID')
}
return { accessToken, orgUUID }
}
/**
* Fetches code sessions from the new Sessions API (/v1/sessions)
* @returns Array of code sessions
*/
export async function fetchCodeSessionsFromSessionsAPI(): Promise<
CodeSession[]
> {
const { accessToken, orgUUID } = await prepareApiRequest()
const url = `${getOauthConfig().BASE_API_URL}/v1/sessions`
try {
const headers = {
...getOAuthHeaders(accessToken),
'anthropic-beta': 'ccr-byoc-2025-07-29',
'x-organization-uuid': orgUUID,
}
const response = await axiosGetWithRetry<ListSessionsResponse>(url, {
headers,
})
if (response.status !== 200) {
throw new Error(`Failed to fetch code sessions: ${response.statusText}`)
}
// Transform SessionResource[] to CodeSession[] format
const sessions: CodeSession[] = response.data.data.map(session => {
// Extract repository info from git sources
const gitSource = session.session_context.sources.find(
(source): source is GitSource => source.type === 'git_repository',
)
let repo: CodeSession['repo'] = null
if (gitSource?.url) {
// Parse GitHub URL using the existing utility function
const repoPath = parseGitHubRepository(gitSource.url)
if (repoPath) {
const [owner, name] = repoPath.split('/')
if (owner && name) {
repo = {
name,
owner: {
login: owner,
},
default_branch: gitSource.revision || undefined,
}
}
}
}
return {
id: session.id,
title: session.title || 'Untitled',
description: '', // SessionResource doesn't have description field
status: session.session_status as CodeSession['status'], // Map session_status to status
repo,
turns: [], // SessionResource doesn't have turns field
created_at: session.created_at,
updated_at: session.updated_at,
}
})
return sessions
} catch (error) {
const err = toError(error)
logError(err)
throw error
}
}
/**
* Creates OAuth headers for API requests
* @param accessToken The OAuth access token
* @returns Headers object with Authorization, Content-Type, and anthropic-version
*/
export function getOAuthHeaders(accessToken: string): Record<string, string> {
return {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
'anthropic-version': '2023-06-01',
}
}
/**
* Fetches a single session by ID from the Sessions API
* @param sessionId The session ID to fetch
* @returns The session resource
*/
export async function fetchSession(
sessionId: string,
): Promise<SessionResource> {
const { accessToken, orgUUID } = await prepareApiRequest()
const url = `${getOauthConfig().BASE_API_URL}/v1/sessions/${sessionId}`
const headers = {
...getOAuthHeaders(accessToken),
'anthropic-beta': 'ccr-byoc-2025-07-29',
'x-organization-uuid': orgUUID,
}
const response = await axios.get<SessionResource>(url, {
headers,
timeout: 15000,
validateStatus: status => status < 500,
})
if (response.status !== 200) {
// Extract error message from response if available
const errorData = response.data as { error?: { message?: string } }
const apiMessage = errorData?.error?.message
if (response.status === 404) {
throw new Error(`Session not found: ${sessionId}`)
}
if (response.status === 401) {
throw new Error('Session expired. Please run /login to sign in again.')
}
throw new Error(
apiMessage ||
`Failed to fetch session: ${response.status} ${response.statusText}`,
)
}
return response.data
}
/**
* Extracts the first branch name from a session's git repository outcomes
* @param session The session resource to extract from
* @returns The first branch name, or undefined if none found
*/
export function getBranchFromSession(
session: SessionResource,
): string | undefined {
const gitOutcome = session.session_context.outcomes?.find(
(outcome): outcome is GitRepositoryOutcome =>
outcome.type === 'git_repository',
)
return gitOutcome?.git_info?.branches[0]
}
/**
* Content for a remote session message.
* Accepts a plain string or an array of content blocks (text, image, etc.)
* following the Anthropic API messages spec.
*/
export type RemoteMessageContent =
| string
| Array<{ type: string; [key: string]: unknown }>
/**
* Sends a user message event to an existing remote session via the Sessions API
* @param sessionId The session ID to send the event to
* @param messageContent The user message content (string or content blocks)
* @param opts.uuid Optional UUID for the event — callers that added a local
* UserMessage first should pass its UUID so echo filtering can dedup
* @returns Promise<boolean> True if successful, false otherwise
*/
export async function sendEventToRemoteSession(
sessionId: string,
messageContent: RemoteMessageContent,
opts?: { uuid?: string },
): Promise<boolean> {
try {
const { accessToken, orgUUID } = await prepareApiRequest()
const url = `${getOauthConfig().BASE_API_URL}/v1/sessions/${sessionId}/events`
const headers = {
...getOAuthHeaders(accessToken),
'anthropic-beta': 'ccr-byoc-2025-07-29',
'x-organization-uuid': orgUUID,
}
const userEvent = {
uuid: opts?.uuid ?? randomUUID(),
session_id: sessionId,
type: 'user',
parent_tool_use_id: null,
message: {
role: 'user',
content: messageContent,
},
}
const requestBody = {
events: [userEvent],
}
logForDebugging(
`[sendEventToRemoteSession] Sending event to session ${sessionId}`,
)
// The endpoint may block until the CCR worker is ready. Observed ~2.6s
// in normal cases; allow a generous margin for cold-start containers.
const response = await axios.post(url, requestBody, {
headers,
validateStatus: status => status < 500,
timeout: 30000,
})
if (response.status === 200 || response.status === 201) {
logForDebugging(
`[sendEventToRemoteSession] Successfully sent event to session ${sessionId}`,
)
return true
}
logForDebugging(
`[sendEventToRemoteSession] Failed with status ${response.status}: ${jsonStringify(response.data)}`,
)
return false
} catch (error) {
logForDebugging(`[sendEventToRemoteSession] Error: ${errorMessage(error)}`)
return false
}
}
/**
* Updates the title of an existing remote session via the Sessions API
* @param sessionId The session ID to update
* @param title The new title for the session
* @returns Promise<boolean> True if successful, false otherwise
*/
export async function updateSessionTitle(
sessionId: string,
title: string,
): Promise<boolean> {
try {
const { accessToken, orgUUID } = await prepareApiRequest()
const url = `${getOauthConfig().BASE_API_URL}/v1/sessions/${sessionId}`
const headers = {
...getOAuthHeaders(accessToken),
'anthropic-beta': 'ccr-byoc-2025-07-29',
'x-organization-uuid': orgUUID,
}
logForDebugging(
`[updateSessionTitle] Updating title for session ${sessionId}: "${title}"`,
)
const response = await axios.patch(
url,
{ title },
{
headers,
validateStatus: status => status < 500,
},
)
if (response.status === 200) {
logForDebugging(
`[updateSessionTitle] Successfully updated title for session ${sessionId}`,
)
return true
}
logForDebugging(
`[updateSessionTitle] Failed with status ${response.status}: ${jsonStringify(response.data)}`,
)
return false
} catch (error) {
logForDebugging(`[updateSessionTitle] Error: ${errorMessage(error)}`)
return false
}
}