Only show warning if truncation comment is in new content

This commit is contained in:
Saoud Rizwan
2024-10-17 06:02:16 -04:00
parent 0d09897aa6
commit 717d83b5cb
2 changed files with 15 additions and 11 deletions

View File

@@ -1075,8 +1075,7 @@ export class Cline {
await this.diffViewProvider.update(newContent, true) await this.diffViewProvider.update(newContent, true)
await delay(300) // wait for diff view to update await delay(300) // wait for diff view to update
this.diffViewProvider.scrollToFirstDiff() this.diffViewProvider.scrollToFirstDiff()
showOmissionWarning(this.diffViewProvider.originalContent || "", newContent)
showOmissionWarning(newContent)
const completeMessage = JSON.stringify({ const completeMessage = JSON.stringify({
...sharedMessageProps, ...sharedMessageProps,

View File

@@ -2,12 +2,14 @@ import * as vscode from "vscode"
/** /**
* Detects potential AI-generated code omissions in the given file content. * Detects potential AI-generated code omissions in the given file content.
* @param fileContent The content of the file to check. * @param originalFileContent The original content of the file.
* @param newFileContent The new content of the file to check.
* @returns True if a potential omission is detected, false otherwise. * @returns True if a potential omission is detected, false otherwise.
*/ */
function detectCodeOmission(fileContent: string): boolean { function detectCodeOmission(originalFileContent: string, newFileContent: string): boolean {
const lines = fileContent.split("\n") const originalLines = originalFileContent.split("\n")
const omissionKeywords = ["remain", "remains", "unchanged", "rest", "previous", "..."] const newLines = newFileContent.split("\n")
const omissionKeywords = ["remain", "remains", "unchanged", "rest", "previous", "existing", "..."]
const commentPatterns = [ const commentPatterns = [
/^\s*\/\//, // Single-line comment for most languages /^\s*\/\//, // Single-line comment for most languages
@@ -16,11 +18,13 @@ function detectCodeOmission(fileContent: string): boolean {
/^\s*<!--/, // HTML comment opening /^\s*<!--/, // HTML comment opening
] ]
for (const line of lines) { for (const line of newLines) {
if (commentPatterns.some((pattern) => pattern.test(line))) { if (commentPatterns.some((pattern) => pattern.test(line))) {
const words = line.toLowerCase().split(/\s+/) const words = line.toLowerCase().split(/\s+/)
if (omissionKeywords.some((keyword) => words.includes(keyword))) { if (omissionKeywords.some((keyword) => words.includes(keyword))) {
return true if (!originalLines.includes(line)) {
return true
}
} }
} }
} }
@@ -30,10 +34,11 @@ function detectCodeOmission(fileContent: string): boolean {
/** /**
* Shows a warning in VSCode if a potential code omission is detected. * Shows a warning in VSCode if a potential code omission is detected.
* @param fileContent The content of the file to check. * @param originalFileContent The original content of the file.
* @param newFileContent The new content of the file to check.
*/ */
export function showOmissionWarning(fileContent: string): void { export function showOmissionWarning(originalFileContent: string, newFileContent: string): void {
if (detectCodeOmission(fileContent)) { if (detectCodeOmission(originalFileContent, newFileContent)) {
vscode.window vscode.window
.showWarningMessage( .showWarningMessage(
"Potential code truncation detected. This happens when the AI reaches its max output limit.", "Potential code truncation detected. This happens when the AI reaches its max output limit.",