Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AI code block “Apply” fails due to <think> tags at start of response (diff desync) (deepseek-r1-671b) #283

Open
jonhannis opened this issue Feb 14, 2025 · 1 comment · May be fixed by #287

Comments

@jonhannis
Copy link

Description:

When applying AI-generated code suggestions in Void, the code is not inserted/merged if the assistant’s response begins with a <think>...</think> block. The reasoning section (the r1 model’s hidden chain-of-thought) occupies multiple lines at the start of the answer, which throws off the diff algorithm that Void uses to merge changes. Essentially, these extra lines cause a desync: the editor can’t correctly match the AI’s suggested code with the corresponding lines in the document, so no synced diff edits can be applied.

Steps to Reproduce:

Use an AI model (such as DeepSeek-R1) that includes a reasoning section wrapped in `<think>...</think>` tags.

Ask the model to generate or modify code. The assistant’s response will start with a block like:

<think>
... (internal reasoning content) ...
</think>
Actual code starts here...

In the chat interface, click the “Apply” button on the code block suggestion.

Observed: desync in the entire document; the code from the suggestion can not be applied. 

Cause:

The <think>...</think> tags and their content are mistakenly included as part of the applied code. When the diff algorithm compares the AI’s suggested code with the document, these extra lines cause a misalignment because they don’t exist in the file. As a result, the computed diff is empty, and the BulkEditService applies no changes.

Affected Components:

Chat Response Parsing: The AI’s raw output is not pre-processed to filter out the `<think>` section. This means that both the chat UI and the underlying code used for generating diffs see the extra lines.
Code Apply Logic: The function responsible for computing and applying code edits (likely in chatCodeblockActions.ts or similar) processes the AI’s response with the `<think> block still intact, resulting in an desynced diff.

Proposed Solution:

Introduce a preprocessing step to strip out the first <think>...</think> section from the AI response before it is used in either the chat display or the diff computation. Specifically:

As soon as the AI’s response is received, check if it begins with `<think>`.
If it does, remove everything from the opening `<think>` to the corresponding closing `</think>` (inclusive).
Proceed to process the remaining text as the actual answer.

This will ensure that:

The chat view displays only the final answer (with a possible placeholder indicating processing time, if desired).
The diff algorithm compares only the actual code, allowing it to compute the correct changes and apply them to the document.

Notes:

The fix should remove the initial ... block only if it appears at the very beginning of the response. To handle cases where the block might include nested tags (for example, if the block contains self-referential or meta-thinking content), the implementation must use a robust parsing strategy (such as a stack-based approach or an XML/HTML parser) to correctly match each opening tag with its corresponding closing tag. This ensures that the entire intended block is removed—even when nested tags are present—while leaving any subsequent tags (that might be legitimately part of the actual answer) intact. If the tags are malformed or improperly nested, the algorithm should fail gracefully, either by removing content only up to the first detectable closing tag or by issuing a warning, so that the remainder of the response can be processed correctly.
Future improvements may include an option to view the internal reasoning in the chat window as a collapsible section without being included with the diff process.
This change aligns with the existing feature request to handle <think> tags more gracefully and should resolve the “Apply” button failure when using AI models like DeepSeek-R1.

devin-ai-integration bot added a commit to jhacksman/void that referenced this issue Feb 15, 2025
- Add utility to strip <think> tags from responses
- Strip tags before computing diffs in Apply operation
- Handle nested tags properly
- Keep original response intact for UI display

Fixes voideditor#283

Co-Authored-By: Jack Hacksman <[email protected]>
@jhacksman jhacksman linked a pull request Feb 15, 2025 that will close this issue
@andrewpareles
Copy link
Contributor

Thanks for the report, fix is on #283. Will be in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants