Skip to content

Commit 7538e12

Browse files
author
Eric Wheeler
committed
fix: preserve content integrity in VS Code LM provider
The provider was modifying system information and model responses by stripping escape sequences and performing other transformations. The issue was discovered when attempting to use escape sequences like '\x1b' in text, which were being removed by the cleanTerminalOutput method. This caused problems in scenarios where escape sequences needed to be preserved, such as when working with terminal control codes. It is not the responsibility of the provider to modify input/output to and from the model. Roo expects all communication through different providers to be clean so that it can manage transformation centrally. Signed-off-by: Eric Wheeler <[email protected]>
1 parent ae43cd5 commit 7538e12

File tree

1 file changed

+3
-45
lines changed

1 file changed

+3
-45
lines changed

src/api/providers/vscode-lm.ts

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -282,54 +282,13 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
282282
return this.client
283283
}
284284

285-
private cleanTerminalOutput(text: string): string {
286-
if (!text) {
287-
return ""
288-
}
289-
290-
return (
291-
text
292-
// Normalize line breaks
293-
.replace(/\r\n/g, "\n")
294-
.replace(/\r/g, "\n")
295-
296-
// Remove ANSI escape sequences
297-
.replace(/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g, "") // Complete set of ANSI sequences
298-
.replace(/\x9B[0-?]*[ -/]*[@-~]/g, "") // CSI sequences
299-
300-
// Remove terminal title setting sequences and other OSC sequences
301-
.replace(/\x1B\][0-9;]*(?:\x07|\x1B\\)/g, "")
302-
303-
// Remove control characters
304-
.replace(/[\x00-\x09\x0B-\x0C\x0E-\x1F\x7F]/g, "")
305-
306-
// Remove VS Code escape sequences
307-
.replace(/\x1B[PD].*?\x1B\\/g, "") // DCS sequences
308-
.replace(/\x1B_.*?\x1B\\/g, "") // APC sequences
309-
.replace(/\x1B\^.*?\x1B\\/g, "") // PM sequences
310-
.replace(/\x1B\[[\d;]*[HfABCDEFGJKST]/g, "") // Cursor movement and clear screen
311-
312-
// Remove Windows paths and service information
313-
.replace(/^(?:PS )?[A-Z]:\\[^\n]*$/gm, "")
314-
.replace(/^;?Cwd=.*$/gm, "")
315-
316-
// Clean escaped sequences
317-
.replace(/\\x[0-9a-fA-F]{2}/g, "")
318-
.replace(/\\u[0-9a-fA-F]{4}/g, "")
319-
320-
// Final cleanup
321-
.replace(/\n{3,}/g, "\n\n") // Remove multiple empty lines
322-
.trim()
323-
)
324-
}
325-
326285
private cleanMessageContent(content: any): any {
327286
if (!content) {
328287
return content
329288
}
330289

331290
if (typeof content === "string") {
332-
return this.cleanTerminalOutput(content)
291+
return content
333292
}
334293

335294
if (Array.isArray(content)) {
@@ -352,16 +311,15 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
352311
this.ensureCleanState()
353312
const client: vscode.LanguageModelChat = await this.getClient()
354313

355-
// Clean system prompt and messages
356-
const cleanedSystemPrompt = this.cleanTerminalOutput(systemPrompt)
314+
// Process messages
357315
const cleanedMessages = messages.map((msg) => ({
358316
...msg,
359317
content: this.cleanMessageContent(msg.content),
360318
}))
361319

362320
// Convert Anthropic messages to VS Code LM messages
363321
const vsCodeLmMessages: vscode.LanguageModelChatMessage[] = [
364-
vscode.LanguageModelChatMessage.Assistant(cleanedSystemPrompt),
322+
vscode.LanguageModelChatMessage.Assistant(systemPrompt),
365323
...convertToVsCodeLmMessages(cleanedMessages),
366324
]
367325

0 commit comments

Comments
 (0)