@@ -1229,48 +1229,48 @@ This is ONE FILE ONLY. Generate the complete diff now:`;
12291229if ( ! filesToModify || allDiffs . length === 0 ) {
12301230 console . log ( `[AGENT] Using single-pass generation method...` ) ;
12311231 const apiStartTime = Date . now ( ) ;
1232- try {
1233- output = await retryWithBackoff ( async ( ) => {
1234- return await callAI ( prompt ) ;
1235- } , 3 , 1000 ) ;
1236- const apiDuration = Date . now ( ) - apiStartTime ;
1237- console . log ( `[AGENT] API call completed in ${ apiDuration } ms` ) ;
1238- } catch ( error ) {
1239- const errorDetails = formatErrorDetails ( error , { } ) ;
1240- await handleError ( error , `${ provider . toUpperCase ( ) } API Error` , { errorDetails } ) ;
1241- // handleError calls process.exit(1), so we never reach here
1232+ try {
1233+ output = await retryWithBackoff ( async ( ) => {
1234+ return await callAI ( prompt ) ;
1235+ } , 3 , 1000 ) ;
1236+ const apiDuration = Date . now ( ) - apiStartTime ;
1237+ console . log ( `[AGENT] API call completed in ${ apiDuration } ms` ) ;
1238+ } catch ( error ) {
1239+ const errorDetails = formatErrorDetails ( error , { } ) ;
1240+ await handleError ( error , `${ provider . toUpperCase ( ) } API Error` , { errorDetails } ) ;
1241+ // handleError calls process.exit(1), so we never reach here
12421242 }
12431243}
12441244
12451245// Extract diff from potential markdown code blocks (only if using fallback method)
12461246let diff = output . trim ( ) ;
12471247if ( ! filesToModify || allDiffs . length === 0 ) {
12481248 // Only do markdown extraction for fallback single-pass method
1249- if ( output . includes ( "```" ) ) {
1250- // Try to extract content between code fences
1251- // Handle both single and multiple code blocks
1252- const matches = output . matchAll ( / ` ` ` (?: d i f f ) ? \n ( [ \s \S ] * ?) ` ` ` / g) ;
1253- const extractedDiffs = [ ] ;
1254- for ( const match of matches ) {
1255- extractedDiffs . push ( match [ 1 ] . trim ( ) ) ;
1256- }
1257- // Use the longest extracted diff (likely the actual diff)
1258- if ( extractedDiffs . length > 0 ) {
1259- diff = extractedDiffs . reduce ( ( a , b ) => a . length > b . length ? a : b ) ;
1260- }
1261-
1262- // If no code blocks found but output contains diff markers, use the whole output
1263- if ( ! diff . includes ( "--- a/" ) && output . includes ( "--- a/" ) ) {
1264- // Extract everything after the first "--- a/" line
1265- const diffStart = output . indexOf ( "--- a/" ) ;
1266- diff = output . substring ( diffStart ) . trim ( ) ;
1267- // Remove any trailing markdown or explanations
1268- const diffEnd = diff . indexOf ( "\n\n```" ) !== - 1 ? diff . indexOf ( "\n\n```" ) :
1269- diff . indexOf ( "\n\n##" ) !== - 1 ? diff . indexOf ( "\n\n##" ) :
1270- diff . indexOf ( "\n\n**" ) !== - 1 ? diff . indexOf ( "\n\n**" ) :
1271- diff . length ;
1272- diff = diff . substring ( 0 , diffEnd ) . trim ( ) ;
1273- }
1249+ if ( output . includes ( "```" ) ) {
1250+ // Try to extract content between code fences
1251+ // Handle both single and multiple code blocks
1252+ const matches = output . matchAll ( / ` ` ` (?: d i f f ) ? \n ( [ \s \S ] * ?) ` ` ` / g) ;
1253+ const extractedDiffs = [ ] ;
1254+ for ( const match of matches ) {
1255+ extractedDiffs . push ( match [ 1 ] . trim ( ) ) ;
1256+ }
1257+ // Use the longest extracted diff (likely the actual diff)
1258+ if ( extractedDiffs . length > 0 ) {
1259+ diff = extractedDiffs . reduce ( ( a , b ) => a . length > b . length ? a : b ) ;
1260+ }
1261+
1262+ // If no code blocks found but output contains diff markers, use the whole output
1263+ if ( ! diff . includes ( "--- a/" ) && output . includes ( "--- a/" ) ) {
1264+ // Extract everything after the first "--- a/" line
1265+ const diffStart = output . indexOf ( "--- a/" ) ;
1266+ diff = output . substring ( diffStart ) . trim ( ) ;
1267+ // Remove any trailing markdown or explanations
1268+ const diffEnd = diff . indexOf ( "\n\n```" ) !== - 1 ? diff . indexOf ( "\n\n```" ) :
1269+ diff . indexOf ( "\n\n##" ) !== - 1 ? diff . indexOf ( "\n\n##" ) :
1270+ diff . indexOf ( "\n\n**" ) !== - 1 ? diff . indexOf ( "\n\n**" ) :
1271+ diff . length ;
1272+ diff = diff . substring ( 0 , diffEnd ) . trim ( ) ;
1273+ }
12741274 }
12751275} else {
12761276 // For one-file-at-a-time, diff is already clean (output from combined diffs)
@@ -1372,9 +1372,9 @@ if (validationResult.errors.length > 0 || diff.match(/\+\+\+ b\/[^\n]*$/m)) {
13721372 }
13731373
13741374 const errorMsg = `${ truncationReason } . The AI model may have generated an incomplete diff.\n\nErrors:\n${ validationResult . errors . join ( '\n' ) } \n\nAPI Response Info:\n- Stop reason: ${ stopReason } \n- Output tokens used: ${ outputTokens } \n- Was truncated: ${ wasTruncated } \n\nDiff preview (last 500 chars):\n\`\`\`\n${ diff . substring ( Math . max ( 0 , diffLength - 500 ) ) } \n\`\`\`\n\nPossible causes:\n- Model hit output token limit (check max_tokens setting)\n- Input context too large, leaving insufficient room for output\n- Model stopped generating for other reasons\n\nSuggestions:\n- Reduce input context size (fewer files) - already reduced to 400KB\n- Break task into smaller parts\n- Verify max_tokens is sufficient (currently 180K)` ;
1375- const errorDetails = formatErrorDetails ( new Error ( errorMsg ) , { diff : diff . substring ( Math . max ( 0 , diffLength - 1000 ) ) , files : validationResult . stats . filesChanged } ) ;
1375+ const errorDetails = formatErrorDetails ( new Error ( errorMsg ) , { diff : diff . substring ( Math . max ( 0 , diffLength - 1000 ) ) , files : validationResult . stats . filesChanged } ) ;
13761376 await handleError ( new Error ( errorMsg ) , "Incomplete Diff (Truncated)" , { diff : diff . substring ( Math . max ( 0 , diffLength - 1000 ) ) , errorDetails } ) ;
1377- // handleError calls process.exit(1), so we never reach here
1377+ // handleError calls process.exit(1), so we never reach here
13781378 }
13791379 }
13801380}
0 commit comments