( anusClient: AnusClient, originalOldString: string, correctedOldString: string, originalNewString: string, abortSignal: AbortSignal, )
| 450 | * Adjusts the new_string to align with a corrected old_string, maintaining the original intent. |
| 451 | */ |
| 452 | export async function correctNewString( |
| 453 | anusClient: AnusClient, |
| 454 | originalOldString: string, |
| 455 | correctedOldString: string, |
| 456 | originalNewString: string, |
| 457 | abortSignal: AbortSignal, |
| 458 | ): Promise<string> { |
| 459 | if (originalOldString === correctedOldString) { |
| 460 | return originalNewString; |
| 461 | } |
| 462 | |
| 463 | const prompt = ` |
| 464 | Context: A text replacement operation was planned. The original text to be replaced (original_old_string) was slightly different from the actual text in the file (corrected_old_string). The original_old_string has now been corrected to match the file content. |
| 465 | We now need to adjust the replacement text (original_new_string) so that it makes sense as a replacement for the corrected_old_string, while preserving the original intent of the change. |
| 466 | |
| 467 | original_old_string (what was initially intended to be found): |
| 468 | \`\`\` |
| 469 | ${originalOldString} |
| 470 | \`\`\` |
| 471 | |
| 472 | corrected_old_string (what was actually found in the file and will be replaced): |
| 473 | \`\`\` |
| 474 | ${correctedOldString} |
| 475 | \`\`\` |
| 476 | |
| 477 | original_new_string (what was intended to replace original_old_string): |
| 478 | \`\`\` |
| 479 | ${originalNewString} |
| 480 | \`\`\` |
| 481 | |
| 482 | Task: Based on the differences between original_old_string and corrected_old_string, and the content of original_new_string, generate a corrected_new_string. This corrected_new_string should be what original_new_string would have been if it was designed to replace corrected_old_string directly, while maintaining the spirit of the original transformation. |
| 483 | |
| 484 | For example, if original_old_string was "\\\\\\nconst greeting = \`Hello \\\\\`\${name}\\\\\`\`;" and corrected_old_string is "\nconst greeting = \`Hello ${'\\`'}\${name}${'\\`'}\`;", and original_new_string was "\\\\\\nconst greeting = \`Hello \\\\\`\${name} \${lastName}\\\\\`\`;", then corrected_new_string should likely be "\nconst greeting = \`Hello ${'\\`'}\${name} \${lastName}${'\\`'}\`;" to fix the incorrect escaping. |
| 485 | If the differences are only in whitespace or formatting, apply similar whitespace/formatting changes to the corrected_new_string. |
| 486 | |
| 487 | Return ONLY the corrected string in the specified JSON format with the key 'corrected_new_string'. If no adjustment is deemed necessary or possible, return the original_new_string. |
| 488 | `.trim(); |
| 489 | |
| 490 | const contents: Content[] = [{ role: 'user', parts: [{ text: prompt }] }]; |
| 491 | |
| 492 | try { |
| 493 | const result = await anusClient.generateJson( |
| 494 | contents, |
| 495 | NEW_STRING_CORRECTION_SCHEMA, |
| 496 | abortSignal, |
| 497 | EditModel, |
| 498 | EditConfig, |
| 499 | ); |
| 500 | |
| 501 | if ( |
| 502 | result && |
| 503 | typeof result['corrected_new_string'] === 'string' && |
| 504 | result['corrected_new_string'].length > 0 |
| 505 | ) { |
| 506 | return result['corrected_new_string']; |
| 507 | } else { |
| 508 | return originalNewString; |
| 509 | } |
no test coverage detected