axios+telemetry cleanup
This commit is contained in:
@@ -7,7 +7,6 @@
|
||||
* API Reference: https://docs.anthropic.com/en/api/files-content
|
||||
*/
|
||||
|
||||
import axios from 'axios'
|
||||
import { randomUUID } from 'crypto'
|
||||
import * as fs from 'fs/promises'
|
||||
import * as path from 'path'
|
||||
@@ -15,6 +14,7 @@ import { count } from '../../utils/array.js'
|
||||
import { getCwd } from '../../utils/cwd.js'
|
||||
import { logForDebugging } from '../../utils/debug.js'
|
||||
import { errorMessage } from '../../utils/errors.js'
|
||||
import { isHttpError, nativeRequest } from '../../utils/http.js'
|
||||
import { logError } from '../../utils/log.js'
|
||||
import { sleep } from '../../utils/sleep.js'
|
||||
import {
|
||||
@@ -146,16 +146,17 @@ export async function downloadFile(
|
||||
|
||||
return retryWithBackoff(`Download file ${fileId}`, async () => {
|
||||
try {
|
||||
const response = await axios.get(url, {
|
||||
const response = await nativeRequest<ArrayBuffer>(url, {
|
||||
method: 'GET',
|
||||
headers,
|
||||
responseType: 'arraybuffer',
|
||||
timeout: 60000, // 60 second timeout for large files
|
||||
validateStatus: status => status < 500,
|
||||
})
|
||||
|
||||
if (response.status === 200) {
|
||||
logDebug(`Downloaded file ${fileId} (${response.data.length} bytes)`)
|
||||
return { done: true, value: Buffer.from(response.data) }
|
||||
const buffer = Buffer.from(response.data)
|
||||
logDebug(`Downloaded file ${fileId} (${buffer.length} bytes)`)
|
||||
return { done: true, value: buffer }
|
||||
}
|
||||
|
||||
// Non-retriable errors - throw immediately
|
||||
@@ -171,10 +172,10 @@ export async function downloadFile(
|
||||
|
||||
return { done: false, error: `status ${response.status}` }
|
||||
} catch (error) {
|
||||
if (!axios.isAxiosError(error)) {
|
||||
throw error
|
||||
if (isHttpError(error)) {
|
||||
return { done: false, error: `${error.status} ${error.message}` }
|
||||
}
|
||||
return { done: false, error: error.message }
|
||||
return { done: false, error: errorMessage(error) }
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -457,7 +458,9 @@ export async function uploadFile(
|
||||
try {
|
||||
return await retryWithBackoff(`Upload file ${relativePath}`, async () => {
|
||||
try {
|
||||
const response = await axios.post(url, body, {
|
||||
const response = await nativeRequest<any>(url, {
|
||||
method: 'POST',
|
||||
body,
|
||||
headers: {
|
||||
...headers,
|
||||
'Content-Type': `multipart/form-data; boundary=${boundary}`,
|
||||
@@ -465,7 +468,6 @@ export async function uploadFile(
|
||||
},
|
||||
timeout: 120000, // 2 minute timeout for uploads
|
||||
signal: opts?.signal,
|
||||
validateStatus: status => status < 500,
|
||||
})
|
||||
|
||||
if (response.status === 200 || response.status === 201) {
|
||||
@@ -521,11 +523,11 @@ export async function uploadFile(
|
||||
if (error instanceof UploadNonRetriableError) {
|
||||
throw error
|
||||
}
|
||||
if (axios.isCancel(error)) {
|
||||
throw new UploadNonRetriableError('Upload canceled')
|
||||
}
|
||||
// Network errors are retriable
|
||||
if (axios.isAxiosError(error)) {
|
||||
if (isHttpError(error)) {
|
||||
if (error.code === 'ECONNABORTED' || error.status === 408) {
|
||||
return { done: false, error: 'Upload timeout' }
|
||||
}
|
||||
return { done: false, error: error.message }
|
||||
}
|
||||
throw error
|
||||
@@ -643,11 +645,12 @@ export async function listFilesCreatedAfter(
|
||||
`List files after ${afterCreatedAt}`,
|
||||
async () => {
|
||||
try {
|
||||
const response = await axios.get(`${baseUrl}/v1/files`, {
|
||||
const queryParams = new URLSearchParams(params).toString()
|
||||
const fullUrl = `${baseUrl}/v1/files${queryParams ? `?${queryParams}` : ''}`
|
||||
const response = await nativeRequest<any>(fullUrl, {
|
||||
method: 'GET',
|
||||
headers,
|
||||
params,
|
||||
timeout: 60000,
|
||||
validateStatus: status => status < 500,
|
||||
})
|
||||
|
||||
if (response.status === 200) {
|
||||
@@ -671,14 +674,14 @@ export async function listFilesCreatedAfter(
|
||||
|
||||
return { done: false, error: `status ${response.status}` }
|
||||
} catch (error) {
|
||||
if (!axios.isAxiosError(error)) {
|
||||
throw error
|
||||
if (isHttpError(error)) {
|
||||
logEvent('tengu_file_list_failed', {
|
||||
error_type:
|
||||
'network' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
||||
})
|
||||
return { done: false, error: error.message }
|
||||
}
|
||||
logEvent('tengu_file_list_failed', {
|
||||
error_type:
|
||||
'network' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
||||
})
|
||||
return { done: false, error: error.message }
|
||||
throw error
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user