( potentiallyProblematicString: string, client: AnusClient, abortSignal: AbortSignal, )
| 602 | }; |
| 603 | |
| 604 | export async function correctStringEscaping( |
| 605 | potentiallyProblematicString: string, |
| 606 | client: AnusClient, |
| 607 | abortSignal: AbortSignal, |
| 608 | ): Promise<string> { |
| 609 | const prompt = ` |
| 610 | Context: An LLM has just generated potentially_problematic_string and the text might have been improperly escaped (e.g. too many backslashes for newlines like \\n instead of \n, or unnecessarily quotes like \\"Hello\\" instead of "Hello"). |
| 611 | |
| 612 | potentially_problematic_string (this text MIGHT have bad escaping, or might be entirely correct): |
| 613 | \`\`\` |
| 614 | ${potentiallyProblematicString} |
| 615 | \`\`\` |
| 616 | |
| 617 | Task: Analyze the potentially_problematic_string. If it's syntactically invalid due to incorrect escaping (e.g., "\n", "\t", "\\", "\\'", "\\""), correct the invalid syntax. The goal is to ensure the text will be a valid and correctly interpreted. |
| 618 | |
| 619 | For example, if potentially_problematic_string is "bar\\nbaz", the corrected_new_string_escaping should be "bar\nbaz". |
| 620 | If potentially_problematic_string is console.log(\\"Hello World\\"), it should be console.log("Hello World"). |
| 621 | |
| 622 | Return ONLY the corrected string in the specified JSON format with the key 'corrected_string_escaping'. If no escaping correction is needed, return the original potentially_problematic_string. |
| 623 | `.trim(); |
| 624 | |
| 625 | const contents: Content[] = [{ role: 'user', parts: [{ text: prompt }] }]; |
| 626 | |
| 627 | try { |
| 628 | const result = await client.generateJson( |
| 629 | contents, |
| 630 | CORRECT_STRING_ESCAPING_SCHEMA, |
| 631 | abortSignal, |
| 632 | EditModel, |
| 633 | EditConfig, |
| 634 | ); |
| 635 | |
| 636 | if ( |
| 637 | result && |
| 638 | typeof result['corrected_string_escaping'] === 'string' && |
| 639 | result['corrected_string_escaping'].length > 0 |
| 640 | ) { |
| 641 | return result['corrected_string_escaping']; |
| 642 | } else { |
| 643 | return potentiallyProblematicString; |
| 644 | } |
| 645 | } catch (error) { |
| 646 | if (abortSignal.aborted) { |
| 647 | throw error; |
| 648 | } |
| 649 | |
| 650 | console.error( |
| 651 | 'Error during LLM call for string escaping correction:', |
| 652 | error, |
| 653 | ); |
| 654 | return potentiallyProblematicString; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | function trimPairIfPossible( |
| 659 | target: string, |
no test coverage detected