(
filePath: string,
relativePath: string,
config: FilesApiConfig,
opts?: { signal?: AbortSignal },
)
| 376 | * @returns Upload result with success/failure status |
| 377 | */ |
| 378 | export async function uploadFile( |
| 379 | filePath: string, |
| 380 | relativePath: string, |
| 381 | config: FilesApiConfig, |
| 382 | opts?: { signal?: AbortSignal }, |
| 383 | ): Promise<UploadResult> { |
| 384 | const baseUrl = config.baseUrl || getDefaultApiBaseUrl() |
| 385 | const url = `${baseUrl}/v1/files` |
| 386 | |
| 387 | const headers = { |
| 388 | Authorization: `Bearer ${config.oauthToken}`, |
| 389 | 'anthropic-version': ANTHROPIC_VERSION, |
| 390 | 'anthropic-beta': FILES_API_BETA_HEADER, |
| 391 | } |
| 392 | |
| 393 | logDebug(`Uploading file ${filePath} as ${relativePath}`) |
| 394 | |
| 395 | // Read file content first (outside retry loop since it's not a network operation) |
| 396 | let content: Buffer |
| 397 | try { |
| 398 | content = await fs.readFile(filePath) |
| 399 | } catch (error) { |
| 400 | logEvent('tengu_file_upload_failed', { |
| 401 | error_type: |
| 402 | 'file_read' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, |
| 403 | }) |
| 404 | return { |
| 405 | path: relativePath, |
| 406 | error: errorMessage(error), |
| 407 | success: false, |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | const fileSize = content.length |
| 412 | |
| 413 | if (fileSize > MAX_FILE_SIZE_BYTES) { |
| 414 | logEvent('tengu_file_upload_failed', { |
| 415 | error_type: |
| 416 | 'file_too_large' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, |
| 417 | }) |
| 418 | return { |
| 419 | path: relativePath, |
| 420 | error: `File exceeds maximum size of ${MAX_FILE_SIZE_BYTES} bytes (actual: ${fileSize})`, |
| 421 | success: false, |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | // Use crypto.randomUUID for boundary to avoid collisions when uploads start same millisecond |
| 426 | const boundary = `----FormBoundary${randomUUID()}` |
| 427 | const filename = path.basename(relativePath) |
| 428 | |
| 429 | // Build the multipart body |
| 430 | const bodyParts: Buffer[] = [] |
| 431 | |
| 432 | // File part |
| 433 | bodyParts.push( |
| 434 | Buffer.from( |
| 435 | `--${boundary}\r\n` + |
no test coverage detected