Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Aug 17, 2025

Fixes #7156

Summary

This PR adds an XML auto-repair feature to fix broken XML tool calls from certain LLM providers (like Targon) that don't properly format XML responses, specifically missing opening brackets in their XML tags.

Changes

  • ✨ Added XML repair utility function (src/api/utils/xml-repair.ts) to detect and fix broken XML patterns
  • 🔧 Added configuration option openAiXmlAutoRepair in OpenAI Compatible provider settings
  • 🔄 Integrated XML repair into streaming response handling for OpenAI and BaseOpenAiCompatibleProvider
  • ✅ Added comprehensive test suite for XML repair functionality
  • 🎨 Added UI checkbox in OpenAI Compatible settings to enable/disable XML auto-repair

How it works

The XML repair function detects patterns like:

read_file>
args>
<file>
<path>main.gopath>
</file>
args>
read_file>

And repairs them to:

<read_file>
<args>
<file>
<path>main.go</path>
</file>
</args>
</read_file>

Testing

  • All new tests pass (10/10)
  • Existing tests continue to pass
  • Type checking passes
  • Linting passes

Usage

Users can enable this feature by:

  1. Going to Settings → API Provider → OpenAI Compatible
  2. Checking the "XML Auto-repair" checkbox
  3. The feature will automatically fix broken XML in streaming responses

This is particularly useful for providers like Targon that have issues with XML formatting in their responses.


Important

Adds XML auto-repair feature for OpenAI-compatible providers, fixing broken XML tags and integrating a configuration option and UI checkbox.

  • Behavior:
    • Adds XML auto-repair feature for OpenAI-compatible providers, fixing missing opening brackets in XML tags.
    • Integrated into createMessage() in base-openai-compatible-provider.ts and openai.ts.
    • New configuration option openAiXmlAutoRepair in provider-settings.ts.
  • Utilities:
    • Adds repairBrokenXml() function in xml-repair.ts to fix broken XML patterns.
    • Adds hasBrokenXmlPattern() to detect broken XML.
  • UI:
    • Adds checkbox in OpenAICompatible.tsx to enable/disable XML auto-repair.
  • Testing:
    • Adds tests for XML repair in xml-repair.spec.ts.

This description was created by Ellipsis for 3bf47ad. You can customize this summary. It will automatically update as commits are pushed.

… providers

- Add XML repair utility function to fix missing opening brackets in XML tags
- Add configuration option openAiXmlAutoRepair in OpenAI Compatible settings
- Integrate XML repair into OpenAI and BaseOpenAiCompatibleProvider streaming
- Add comprehensive tests for XML repair functionality
- Add UI checkbox for enabling XML auto-repair in settings

Fixes #7156
@roomote roomote bot requested review from cte, jr and mrubens as code owners August 17, 2025 06:03
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request labels Aug 17, 2025
@roomote roomote bot mentioned this pull request Aug 17, 2025
Copy link
Contributor Author

@roomote roomote bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewing my own code is like debugging in production - technically possible but morally questionable.

<div>
<Checkbox
checked={apiConfiguration?.openAiXmlAutoRepair ?? false}
onChange={handleInputChange("openAiXmlAutoRepair", noTransform)}>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing i18n translations! The keys settings:providers.xmlAutoRepair and settings:providers.xmlAutoRepairDescription are not defined in any locale files. This will display raw translation keys in the UI.

You'll need to add these translations to all locale files in webview-ui/src/i18n/locales/*/settings.json


if (delta?.content) {
// Apply XML repair if enabled for OpenAI-compatible providers
const content = this.options.openAiXmlAutoRepair ? repairBrokenXml(delta.content) : delta.content
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance consideration: This calls repairBrokenXml() on every streaming chunk. For large responses with many chunks, this could impact performance. Consider:

  1. Batching chunks before processing
  2. Only checking for broken patterns first with hasBrokenXmlPattern() before attempting repair
  3. Implementing a streaming-aware repair that maintains state between chunks

/**
* Checks if the XML has a valid structure with proper opening and closing tags
*/
function hasValidXmlStructure(xml: string): boolean {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function hasValidXmlStructure() is defined but never used anywhere in the codebase. Should we remove it or integrate it into the repair logic to skip processing when XML is already valid?

// Track open tags to determine if we need opening or closing tags
const openTags: string[] = []

for (let line of lines) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edge case concern: What happens with multiple broken patterns on the same line like read_file>args>? The current logic processes line by line but might not handle all combinations correctly. Consider adding test cases for these scenarios.

/**
* Configuration for XML auto-repair behavior
*/
export interface XmlAutoRepairConfig {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The XmlAutoRepairConfig interface includes fields for future enhancements (useSmallModel, repairModelId) that aren't implemented yet. Consider either:

  1. Removing these until needed
  2. Adding a TODO comment explaining the future plan
  3. Documenting that these are reserved for future use

import { describe, it, expect } from "vitest"
import { repairBrokenXml, hasBrokenXmlPattern } from "../xml-repair"

describe("xml-repair", () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good unit test coverage! However, consider adding integration tests that verify the repair works correctly in the actual streaming context where it's used. This would help catch issues with chunk boundaries and partial XML fragments.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Aug 17, 2025
@daniel-lxs daniel-lxs closed this Aug 18, 2025
@github-project-automation github-project-automation bot moved this from Triage to Done in Roo Code Roadmap Aug 18, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Aug 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Broken XML auto fixing

4 participants