@@ -7,9 +7,11 @@ import {
77} from '@/lib/core/security/input-validation.server'
88import {
99 DEFAULT_MAX_ERROR_BODY_BYTES ,
10+ isPayloadSizeLimitError ,
1011 readResponseTextWithLimit ,
1112} from '@/lib/core/utils/stream-limits'
1213import { PulseOperationError } from '@/lib/internal/pulse/errors'
14+ import { MAX_TOOL_RESPONSE_BODY_BYTES } from '@/lib/internal/tool-operations/response-limits'
1315
1416const logger = createLogger ( 'PulseClient' )
1517const PULSE_API_HOST = 'api.runpulse.com'
@@ -34,23 +36,32 @@ async function downloadLargeResult(
3436 error : 'Pulse returned an invalid result URL' ,
3537 } )
3638 }
37- const isPulseHost = new URL ( url ) . hostname === PULSE_API_HOST
38- const response = await secureFetchWithPinnedIP ( url , validation . resolvedIP , {
39- profile : 'contentFetch' ,
40- method : 'GET' ,
41- headers : isPulseHost ? { 'x-api-key' : apiKey } : { } ,
42- maxResponseBytes : DEFAULT_MAX_RESPONSE_BYTES ,
43- signal,
44- } )
45- signal ?. throwIfAborted ( )
46- if ( ! response . ok ) {
47- logger . error ( 'Pulse large result download failed' , { status : response . status } )
48- throw new PulseOperationError ( response . status , {
39+ const target = new URL ( url )
40+ const isPulseHost = target . protocol === 'https:' && target . hostname === PULSE_API_HOST
41+ try {
42+ const response = await secureFetchWithPinnedIP ( url , validation . resolvedIP , {
43+ profile : 'contentFetch' ,
44+ method : 'GET' ,
45+ headers : isPulseHost ? { 'x-api-key' : apiKey } : { } ,
46+ maxResponseBytes : MAX_TOOL_RESPONSE_BODY_BYTES ,
47+ signal,
48+ } )
49+ signal ?. throwIfAborted ( )
50+ if ( ! response . ok ) {
51+ logger . error ( 'Pulse large result download failed' , { status : response . status } )
52+ throw new PulseOperationError ( response . status , {
53+ success : false ,
54+ error : `Failed to download the Pulse result: ${ response . statusText } ` ,
55+ } )
56+ }
57+ return await response . json ( )
58+ } catch ( error ) {
59+ if ( ! isPayloadSizeLimitError ( error ) ) throw error
60+ throw new PulseOperationError ( 413 , {
4961 success : false ,
50- error : `Failed to download the Pulse result: ${ response . statusText } ` ,
62+ error : `The Pulse result is too large to return (over ${ MAX_TOOL_RESPONSE_BODY_BYTES / ( 1024 * 1024 ) } MiB). Request fewer pages. ` ,
5163 } )
5264 }
53- return response . json ( )
5465}
5566
5667export async function submitPulseParse (
0 commit comments