MCPcopy Create free account
hub / github.com/anus-dev/ANUS / correctOldStringMismatch

Function correctOldStringMismatch

packages/core/src/utils/editCorrector.ts:375–434  ·  view source on GitHub ↗
(
  anusClient: AnusClient,
  fileContent: string,
  problematicSnippet: string,
  abortSignal: AbortSignal,
)

Source from the content-addressed store, hash-verified

373};
374
375export async function correctOldStringMismatch(
376 anusClient: AnusClient,
377 fileContent: string,
378 problematicSnippet: string,
379 abortSignal: AbortSignal,
380): Promise<string> {
381 const prompt = `
382Context: A process needs to find an exact literal, unique match for a specific text snippet within a file's content. The provided snippet failed to match exactly. This is most likely because it has been overly escaped.
383
384Task: Analyze the provided file content and the problematic target snippet. Identify the segment in the file content that the snippet was *most likely* intended to match. Output the *exact*, literal text of that segment from the file content. Focus *only* on removing extra escape characters and correcting formatting, whitespace, or minor differences to achieve a PERFECT literal match. The output must be the exact literal text as it appears in the file.
385
386Problematic target snippet:
387\`\`\`
388${problematicSnippet}
389\`\`\`
390
391File Content:
392\`\`\`
393${fileContent}
394\`\`\`
395
396For example, if the problematic target snippet was "\\\\\\nconst greeting = \`Hello \\\\\`\${name}\\\\\`\`;" and the file content had content that looked like "\nconst greeting = \`Hello ${'\\`'}\${name}${'\\`'}\`;", then corrected_target_snippet should likely be "\nconst greeting = \`Hello ${'\\`'}\${name}${'\\`'}\`;" to fix the incorrect escaping to match the original file content.
397If the differences are only in whitespace or formatting, apply similar whitespace/formatting changes to the corrected_target_snippet.
398
399Return ONLY the corrected target snippet in the specified JSON format with the key 'corrected_target_snippet'. If no clear, unique match can be found, return an empty string for 'corrected_target_snippet'.
400`.trim();
401
402 const contents: Content[] = [{ role: 'user', parts: [{ text: prompt }] }];
403
404 try {
405 const result = await anusClient.generateJson(
406 contents,
407 OLD_STRING_CORRECTION_SCHEMA,
408 abortSignal,
409 EditModel,
410 EditConfig,
411 );
412
413 if (
414 result &&
415 typeof result['corrected_target_snippet'] === 'string' &&
416 result['corrected_target_snippet'].length > 0
417 ) {
418 return result['corrected_target_snippet'];
419 } else {
420 return problematicSnippet;
421 }
422 } catch (error) {
423 if (abortSignal.aborted) {
424 throw error;
425 }
426
427 console.error(
428 'Error during LLM call for old string snippet correction:',
429 error,
430 );
431
432 return problematicSnippet;

Callers 1

ensureCorrectEditFunction · 0.85

Calls 1

generateJsonMethod · 0.80

Tested by

no test coverage detected