Skip to content

Treat AI responses without code blocks as JavaScript#26

Merged
friuns2 merged 1 commit intofriuns2:masterfrom
friuns:feature/ai-response-fallback
Jan 2, 2026
Merged

Treat AI responses without code blocks as JavaScript#26
friuns2 merged 1 commit intofriuns2:masterfrom
friuns:feature/ai-response-fallback

Conversation

@friuns
Copy link
Copy Markdown

@friuns friuns commented Jan 2, 2026

User description

Modified parseFilesFromMessage() in src/utils.js to treat entire AI response as JavaScript code when no code blocks are detected. This prevents 'empty code' errors when AI provides direct code without markdown formatting.

Changes:

  • Added fallback logic to create script.js file with full response content
  • Prevents "empty code" errors for direct JavaScript responses

PR Type

Enhancement


Description

  • Adds fallback logic to treat entire AI response as JavaScript code

  • Prevents "empty code" errors when AI provides unformatted code

  • Creates script.js file with full response content when no code blocks detected


Diagram Walkthrough

flowchart LR
  A["AI Response"] --> B{"Code blocks detected?"}
  B -->|Yes| C["Parse code blocks"]
  B -->|No| D["Treat entire response as JavaScript"]
  C --> E["Return files array"]
  D --> F["Create script.js with full content"]
  F --> E
Loading

File Walkthrough

Relevant files
Enhancement
utils.js
Add fallback for unformatted code responses                           

src/utils.js

  • Added fallback logic in parseFilesFromMessage() function to handle
    responses without code blocks
  • When no code blocks are found and message is not empty, creates a
    script.js file with the entire message content
  • Sets langauge property to "javascript" for the fallback file
  • Clears messageWithoutCodeBlocks when using entire response as code to
    avoid duplication
+11/-0   

- Modified parseFilesFromMessage() in src/utils.js to treat entire response as JavaScript code when no code blocks are detected
- This prevents 'empty code' errors when AI provides direct code without markdown formatting
- Added fallback logic to create script.js file with full response content
@qodo-code-review
Copy link
Copy Markdown

PR Compliance Guide 🔍

Below is a summary of compliance checks for this PR:

Security Compliance
Untrusted code execution

Description: Fallback logic stores the entire AI message as script.js JavaScript code when no code
blocks are found, which could enable execution of untrusted/generated content (e.g., XSS
or arbitrary script execution) if downstream logic automatically runs or injects returned
files into a browser/preview without sandboxing or user confirmation.
utils.js [116-125]

Referred Code
// If no code blocks were found, treat the entire message as a code block
if (files.length === 0 && message.trim()) {
    files.push({
        name: "script.js",
        content: message,
        langauge: "javascript",
        hidden: false
    });
    messageWithoutCodeBlocks = ""; // Clear the message since we're using it as code
}
Ticket Compliance
🎫 No ticket provided
  • Create ticket/issue
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
🟢
Generic: Comprehensive Audit Trails

Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Error Handling

Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Logging Practices

Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

🔴
Generic: Meaningful Naming and Self-Documenting Code

Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting

Status:
Misspelled Identifier: The newly added fallback file object uses the misspelled property langauge, reducing
clarity and likely diverging from the intended language field.

Referred Code
files.push({
    name: "script.js",
    content: message,
    langauge: "javascript",
    hidden: false
});

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Robust Error Handling and Edge Case Management

Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation

Status:
Missing Null Guard: The new condition calls message.trim() without guarding against message being
null/undefined or non-string, which can throw at runtime instead of degrading gracefully.

Referred Code
// If no code blocks were found, treat the entire message as a code block
if (files.length === 0 && message.trim()) {
    files.push({
        name: "script.js",
        content: message,
        langauge: "javascript",
        hidden: false
    });
    messageWithoutCodeBlocks = ""; // Clear the message since we're using it as code
}

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Security-First Input Validation and Data Handling

Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities

Status:
Unvalidated Code Content: The fallback treats the entire unvalidated message as JavaScript (script.js), which may
enable unsafe handling/execution downstream depending on how these files are used.

Referred Code
// If no code blocks were found, treat the entire message as a code block
if (files.length === 0 && message.trim()) {
    files.push({
        name: "script.js",
        content: message,
        langauge: "javascript",
        hidden: false
    });

Learn more about managing compliance generic rules or creating your own custom rules

Compliance status legend 🟢 - Fully Compliant
🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label

@friuns2 friuns2 merged commit 545aa71 into friuns2:master Jan 2, 2026
@qodo-code-review
Copy link
Copy Markdown

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Fix property name typo

Correct the langauge property name typo to language.

src/utils.js [120-121]

 content: message,
-langauge: "javascript",
+language: "javascript",
  • Apply / Chat
Suggestion importance[1-10]: 8

__

Why: This suggestion corrects a typo in the langauge property name, which is a clear bug that would prevent consumers of the generated file object from accessing the language property correctly.

Medium
Add heuristic to prevent misinterpreting text

Add a heuristic check to only treat messages that resemble code as a fallback
script, preventing misinterpretation of plain text responses.

src/utils.js [116-125]

 // If no code blocks were found, treat the entire message as a code block
 if (files.length === 0 && message.trim()) {
-    files.push({
-        name: "script.js",
-        content: message,
-        langauge: "javascript",
-        hidden: false
-    });
-    messageWithoutCodeBlocks = ""; // Clear the message since we're using it as code
+    // Heuristic to check if the message content is likely to be code.
+    const isLikelyCode = /const|let|var|function|=>|import|export|console\.log|\{|\}/.test(message);
+    if (isLikelyCode) {
+        files.push({
+            name: "script.js",
+            content: message,
+            langauge: "javascript",
+            hidden: false
+        });
+        messageWithoutCodeBlocks = ""; // Clear the message since we're using it as code
+    }
 }
  • Apply / Chat
Suggestion importance[1-10]: 7

__

Why: The suggestion correctly identifies a flaw in the new logic where non-code text could be misinterpreted as a script, and proposes a reasonable heuristic to improve the robustness of the feature.

Medium
General
Trim fallback content

Trim leading and trailing whitespace from the message content before assigning
it to the fallback file.

src/utils.js [120]

-content: message,
+content: message.trim(),
  • Apply / Chat
Suggestion importance[1-10]: 4

__

Why: This is a good code quality suggestion that improves the fallback logic by ensuring the generated file content is clean of extraneous whitespace, although the impact is minor.

Low
  • More

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants