( anusClient: AnusClient, oldString: string, potentiallyProblematicNewString: string, abortSignal: AbortSignal, )
| 530 | }; |
| 531 | |
| 532 | export async function correctNewStringEscaping( |
| 533 | anusClient: AnusClient, |
| 534 | oldString: string, |
| 535 | potentiallyProblematicNewString: string, |
| 536 | abortSignal: AbortSignal, |
| 537 | ): Promise<string> { |
| 538 | const prompt = ` |
| 539 | Context: A text replacement operation is planned. The text to be replaced (old_string) has been correctly identified in the file. However, the replacement text (new_string) might have been improperly escaped by a previous LLM generation (e.g. too many backslashes for newlines like \\n instead of \n, or unnecessarily quotes like \\"Hello\\" instead of "Hello"). |
| 540 | |
| 541 | old_string (this is the exact text that will be replaced): |
| 542 | \`\`\` |
| 543 | ${oldString} |
| 544 | \`\`\` |
| 545 | |
| 546 | potentially_problematic_new_string (this is the text that should replace old_string, but MIGHT have bad escaping, or might be entirely correct): |
| 547 | \`\`\` |
| 548 | ${potentiallyProblematicNewString} |
| 549 | \`\`\` |
| 550 | |
| 551 | Task: Analyze the potentially_problematic_new_string. If it's syntactically invalid due to incorrect escaping (e.g., "\n", "\t", "\\", "\\'", "\\""), correct the invalid syntax. The goal is to ensure the new_string, when inserted into the code, will be a valid and correctly interpreted. |
| 552 | |
| 553 | For example, if old_string is "foo" and potentially_problematic_new_string is "bar\\nbaz", the corrected_new_string_escaping should be "bar\nbaz". |
| 554 | If potentially_problematic_new_string is console.log(\\"Hello World\\"), it should be console.log("Hello World"). |
| 555 | |
| 556 | Return ONLY the corrected string in the specified JSON format with the key 'corrected_new_string_escaping'. If no escaping correction is needed, return the original potentially_problematic_new_string. |
| 557 | `.trim(); |
| 558 | |
| 559 | const contents: Content[] = [{ role: 'user', parts: [{ text: prompt }] }]; |
| 560 | |
| 561 | try { |
| 562 | const result = await anusClient.generateJson( |
| 563 | contents, |
| 564 | CORRECT_NEW_STRING_ESCAPING_SCHEMA, |
| 565 | abortSignal, |
| 566 | EditModel, |
| 567 | EditConfig, |
| 568 | ); |
| 569 | |
| 570 | if ( |
| 571 | result && |
| 572 | typeof result['corrected_new_string_escaping'] === 'string' && |
| 573 | result['corrected_new_string_escaping'].length > 0 |
| 574 | ) { |
| 575 | return result['corrected_new_string_escaping']; |
| 576 | } else { |
| 577 | return potentiallyProblematicNewString; |
| 578 | } |
| 579 | } catch (error) { |
| 580 | if (abortSignal.aborted) { |
| 581 | throw error; |
| 582 | } |
| 583 | |
| 584 | console.error( |
| 585 | 'Error during LLM call for new_string escaping correction:', |
| 586 | error, |
| 587 | ); |
| 588 | return potentiallyProblematicNewString; |
| 589 | } |
no test coverage detected