-
Notifications
You must be signed in to change notification settings - Fork 447
Expand file tree
/
Copy pathcodebuff-api.ts
More file actions
560 lines (485 loc) · 16 KB
/
codebuff-api.ts
File metadata and controls
560 lines (485 loc) · 16 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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
import { WEBSITE_URL } from '@codebuff/sdk'
import type {
PublishAgentsResponse,
} from '@codebuff/common/types/api/agents/publish'
/**
* API response types for consistent error handling.
*
* When `ok` is true, `data` may be undefined for responses with no body (e.g., 204 No Content).
* Callers should check for `response.data` when they expect data from the endpoint.
*/
export type ApiResponse<T> =
| { ok: true; status: number; data?: T }
| { ok: false; status: number; error?: string; errorData?: Record<string, unknown> }
// ============================================================================
// Type-safe endpoint request/response types
// ============================================================================
/** User fields that can be fetched from /api/v1/me */
export type UserField = 'id' | 'email' | 'discord_id' | 'referral_code'
export type UserDetails<T extends UserField = UserField> = {
[K in T]: K extends 'discord_id' | 'referral_code' ? string | null : string
}
export interface UsageRequest {
fingerprintId?: string
}
export interface UsageResponse {
type: 'usage-response'
usage: number
remainingBalance: number | null
balanceBreakdown?: Record<string, number>
next_quota_reset: string | null
}
export interface LoginCodeRequest {
fingerprintId: string
}
export interface LoginCodeResponse {
loginUrl: string
fingerprintHash: string
expiresAt: string
}
export interface LoginStatusRequest {
fingerprintId: string
fingerprintHash: string
expiresAt: string
}
export interface LoginStatusResponse {
user?: Record<string, unknown>
}
export interface ReferralRequest {
referralCode: string
}
export interface ReferralResponse {
credits_redeemed?: number
error?: string
}
export interface LogoutRequest {
userId?: string
fingerprintId?: string
fingerprintHash?: string
}
/**
* Retry configuration
*/
export interface RetryConfig {
/** Maximum number of retry attempts (default: 3) */
maxRetries?: number
/** Initial delay in ms before first retry (default: 1000) */
initialDelayMs?: number
/** Maximum delay in ms between retries (default: 10000) */
maxDelayMs?: number
/** HTTP status codes to retry on (default: [408, 429, 500, 502, 503, 504]) */
retryableStatusCodes?: number[]
}
const DEFAULT_RETRY_CONFIG: Required<RetryConfig> = {
maxRetries: 3,
initialDelayMs: 1000,
maxDelayMs: 10000,
retryableStatusCodes: [408, 429, 500, 502, 503, 504],
}
/**
* Configuration for creating a Codebuff API client
*/
export interface CodebuffApiClientConfig {
/** Base URL for API requests (defaults to WEBSITE_URL from SDK) */
baseUrl?: string
/** Auth token for Bearer authentication */
authToken?: string
/** Custom fetch implementation (for testing) */
fetch?: typeof fetch
/** Default timeout in ms for all requests (default: 30000) */
defaultTimeoutMs?: number
/** Default retry configuration */
retry?: RetryConfig
}
/**
* Options for individual requests
*/
export interface RequestOptions {
/** Query parameters to append to URL */
query?: Record<string, string>
/** Include Authorization header (default: true when authToken is set) */
includeAuth?: boolean
/** Include session token as Cookie header (for legacy endpoints) */
includeCookie?: boolean
/** Request timeout in ms (overrides default) */
timeoutMs?: number
/** Retry configuration (overrides default) */
retry?: RetryConfig | false
/** Custom headers */
headers?: Record<string, string>
}
export interface CodebuffApiClient {
readonly baseUrl: string
readonly authToken?: string
/** Make a raw HTTP request */
request<T>(
method: string,
path: string,
body?: unknown,
options?: RequestOptions,
): Promise<ApiResponse<T>>
/** Make a GET request */
get<T>(path: string, options?: RequestOptions): Promise<ApiResponse<T>>
/** Make a POST request */
post<T>(
path: string,
body?: Record<string, unknown>,
options?: RequestOptions,
): Promise<ApiResponse<T>>
/** Make a PUT request */
put<T>(
path: string,
body?: Record<string, unknown>,
options?: RequestOptions,
): Promise<ApiResponse<T>>
/** Make a PATCH request */
patch<T>(
path: string,
body?: Record<string, unknown>,
options?: RequestOptions,
): Promise<ApiResponse<T>>
/** Make a DELETE request */
delete<T>(path: string, options?: RequestOptions): Promise<ApiResponse<T>>
// ============================================================================
// Type-safe endpoint methods
// ============================================================================
/** Fetch user details from /api/v1/me */
me<T extends UserField>(
fields: readonly T[],
): Promise<ApiResponse<UserDetails<T>>>
/** Fetch usage data from /api/v1/usage */
usage(req?: UsageRequest): Promise<ApiResponse<UsageResponse>>
/** Request a login code from /api/auth/cli/code */
loginCode(req: LoginCodeRequest): Promise<ApiResponse<LoginCodeResponse>>
/** Check login status from /api/auth/cli/status */
loginStatus(
req: LoginStatusRequest,
): Promise<ApiResponse<LoginStatusResponse>>
/** Redeem a referral code via /api/referrals */
referral(req: ReferralRequest): Promise<ApiResponse<ReferralResponse>>
/** Publish agents via /api/agents/publish */
publish(
data: Record<string, unknown>[],
allLocalAgentIds?: string[],
): Promise<ApiResponse<PublishAgentsResponse>>
/** Logout via /api/auth/cli/logout */
logout(req?: LogoutRequest): Promise<ApiResponse<void>>
}
/**
* Sleep for a given duration
*/
const sleep = (ms: number): Promise<void> =>
new Promise((resolve) => setTimeout(resolve, ms))
/**
* Calculate delay with exponential backoff and jitter
*/
const calculateBackoffDelay = (
attempt: number,
initialDelayMs: number,
maxDelayMs: number,
): number => {
const exponentialDelay = initialDelayMs * Math.pow(2, attempt)
const jitter = Math.random() * 0.3 * exponentialDelay // 0-30% jitter
return Math.min(exponentialDelay + jitter, maxDelayMs)
}
/**
* Check if an error is retryable (network errors).
*
* Note: AbortError is NOT retryable because it indicates intentional cancellation
* (e.g., user cancelled the request or our timeout was exceeded).
*/
const isRetryableError = (error: unknown): boolean => {
if (error instanceof Error) {
const name = error.name.toLowerCase()
const message = error.message.toLowerCase()
// Don't retry abort errors - they indicate intentional cancellation
if (name === 'aborterror') {
return false
}
return (
name === 'timeouterror' ||
message.includes('network') ||
message.includes('fetch') ||
message.includes('econnreset') ||
message.includes('econnrefused')
)
}
return false
}
/**
* Create a Codebuff API client for making authenticated requests to the Codebuff API
*/
export function createCodebuffApiClient(
config: CodebuffApiClientConfig = {},
): CodebuffApiClient {
const {
baseUrl = WEBSITE_URL,
authToken,
fetch: fetchFn = fetch,
defaultTimeoutMs = 30000,
retry: defaultRetryConfig = {},
} = config
const mergedDefaultRetry: Required<RetryConfig> = {
...DEFAULT_RETRY_CONFIG,
...defaultRetryConfig,
}
async function request<T>(
method: string,
path: string,
body?: unknown,
options: RequestOptions = {},
): Promise<ApiResponse<T>> {
const {
query,
includeAuth = true,
includeCookie = false,
timeoutMs = defaultTimeoutMs,
retry: retryConfig = mergedDefaultRetry,
headers: customHeaders = {},
} = options
// Build URL with query parameters
let url = `${baseUrl}${path}`
if (query && Object.keys(query).length > 0) {
const params = new URLSearchParams(query)
url += `?${params.toString()}`
}
// Build headers
const headers: Record<string, string> = { ...customHeaders }
if (authToken && includeAuth) {
headers['Authorization'] = `Bearer ${authToken}`
}
if (authToken && includeCookie) {
headers['Cookie'] = `next-auth.session-token=${authToken};`
}
if (body !== undefined) {
headers['Content-Type'] = 'application/json'
}
// Build fetch options
const fetchOptions: RequestInit = {
method,
headers,
}
if (body !== undefined) {
fetchOptions.body = JSON.stringify(body)
}
// Determine retry config
const shouldRetry = retryConfig !== false
const retryOpts = shouldRetry
? { ...mergedDefaultRetry, ...retryConfig }
: null
let lastError: unknown
const maxAttempts = shouldRetry ? (retryOpts?.maxRetries ?? 0) + 1 : 1
for (let attempt = 0; attempt < maxAttempts; attempt++) {
// Create abort controller for timeout
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), timeoutMs)
try {
const response = await fetchFn(url, {
...fetchOptions,
signal: controller.signal,
})
clearTimeout(timeoutId)
if (response.ok) {
try {
const data = (await response.json()) as T
return { ok: true, status: response.status, data }
} catch {
// Response was OK but no JSON body (e.g., 204 No Content)
return { ok: true, status: response.status }
}
}
// Check if we should retry on this status code
if (
shouldRetry &&
retryOpts &&
retryOpts.retryableStatusCodes.includes(response.status) &&
attempt < maxAttempts - 1
) {
const delay = calculateBackoffDelay(
attempt,
retryOpts.initialDelayMs,
retryOpts.maxDelayMs,
)
await sleep(delay)
continue
}
// Parse error response
let errorMessage: string | undefined
let errorData: unknown
try {
const errorBody = await response.json()
errorData = errorBody
errorMessage =
errorBody?.error || errorBody?.message || response.statusText
} catch {
try {
errorMessage = await response.text()
} catch {
errorMessage = response.statusText
}
}
return { ok: false, status: response.status, error: errorMessage, errorData: errorData as Record<string, unknown> | undefined }
} catch (error) {
clearTimeout(timeoutId)
lastError = error
// Check if we should retry on this error
if (
shouldRetry &&
retryOpts &&
isRetryableError(error) &&
attempt < maxAttempts - 1
) {
const delay = calculateBackoffDelay(
attempt,
retryOpts.initialDelayMs,
retryOpts.maxDelayMs,
)
await sleep(delay)
continue
}
// Don't retry, throw the error
throw error
}
}
// Should not reach here, but just in case
throw lastError ?? new Error('Request failed after all retries')
}
return {
baseUrl,
authToken,
request,
get<T>(path: string, options?: RequestOptions): Promise<ApiResponse<T>> {
return request<T>('GET', path, undefined, options)
},
post<T>(
path: string,
body?: Record<string, unknown>,
options?: RequestOptions,
): Promise<ApiResponse<T>> {
return request<T>('POST', path, body, options)
},
put<T>(
path: string,
body?: Record<string, unknown>,
options?: RequestOptions,
): Promise<ApiResponse<T>> {
return request<T>('PUT', path, body, options)
},
patch<T>(
path: string,
body?: Record<string, unknown>,
options?: RequestOptions,
): Promise<ApiResponse<T>> {
return request<T>('PATCH', path, body, options)
},
delete<T>(path: string, options?: RequestOptions): Promise<ApiResponse<T>> {
return request<T>('DELETE', path, undefined, options)
},
// ============================================================================
// Type-safe endpoint methods
// ============================================================================
me<T extends UserField>(
fields: readonly T[],
): Promise<ApiResponse<UserDetails<T>>> {
return request<UserDetails<T>>('GET', '/api/v1/me', undefined, {
query: { fields: fields.join(',') },
})
},
usage(req: UsageRequest = {}): Promise<ApiResponse<UsageResponse>> {
// Auth is sent via Authorization header (includeAuth defaults to true)
return request<UsageResponse>('POST', '/api/v1/usage', {
fingerprintId: req.fingerprintId ?? 'cli-usage',
})
},
loginCode(req: LoginCodeRequest): Promise<ApiResponse<LoginCodeResponse>> {
return request<LoginCodeResponse>(
'POST',
'/api/auth/cli/code',
{ fingerprintId: req.fingerprintId },
{ includeAuth: false },
)
},
loginStatus(
req: LoginStatusRequest,
): Promise<ApiResponse<LoginStatusResponse>> {
return request<LoginStatusResponse>('GET', '/api/auth/cli/status', undefined, {
query: {
fingerprintId: req.fingerprintId,
fingerprintHash: req.fingerprintHash,
expiresAt: req.expiresAt,
},
includeAuth: false,
})
},
referral(req: ReferralRequest): Promise<ApiResponse<ReferralResponse>> {
// Auth is sent via Authorization header (includeAuth defaults to true)
// Also include cookie for legacy web session support
return request<ReferralResponse>(
'POST',
'/api/referrals',
{ referralCode: req.referralCode },
{ includeCookie: true },
)
},
publish(
data: Record<string, unknown>[],
allLocalAgentIds?: string[],
): Promise<ApiResponse<PublishAgentsResponse>> {
// Auth is sent via Authorization header (includeAuth defaults to true)
return request<PublishAgentsResponse>('POST', '/api/agents/publish', {
data,
allLocalAgentIds,
})
},
logout(req: LogoutRequest = {}): Promise<ApiResponse<void>> {
// Auth is sent via Authorization header (includeAuth defaults to true)
return request<void>('POST', '/api/auth/cli/logout', {
userId: req.userId,
fingerprintId: req.fingerprintId,
fingerprintHash: req.fingerprintHash,
})
},
}
}
// ============================================================================
// Shared singleton client
// ============================================================================
let sharedClient: CodebuffApiClient | null = null
let sharedAuthToken: string | undefined
// Track the token that was used to create the current client instance
let clientCreatedWithToken: string | undefined
/**
* Get or create the shared API client singleton.
* The client is lazily created and reused across the application.
*
* Note: Always call setApiClientAuthToken() before getApiClient() when you need
* to ensure a specific auth token is used. The client is recreated whenever
* the auth token changes.
*/
export function getApiClient(): CodebuffApiClient {
// Recreate client if it doesn't exist or if the token has changed since creation
if (!sharedClient || clientCreatedWithToken !== sharedAuthToken) {
sharedClient = createCodebuffApiClient({ authToken: sharedAuthToken })
clientCreatedWithToken = sharedAuthToken
}
return sharedClient
}
/**
* Set the auth token for the shared API client.
* This will cause the next call to getApiClient() to create a new client
* with the updated token.
*/
export function setApiClientAuthToken(authToken: string | undefined): void {
sharedAuthToken = authToken
// Note: We don't eagerly invalidate the client here. Instead, getApiClient()
// checks if the token has changed and recreates the client if needed.
// This avoids race conditions where the client is nullified but not yet recreated.
}
/**
* Reset the shared client (mainly for testing)
*/
export function resetApiClient(): void {
sharedClient = null
sharedAuthToken = undefined
clientCreatedWithToken = undefined
}