Skip to content

fix: enhance the response in UI - #713

Open
Akhileswara-Microsoft wants to merge 11 commits into
devfrom
BugFix_Akhil
Open

fix: enhance the response in UI#713
Akhileswara-Microsoft wants to merge 11 commits into
devfrom
BugFix_Akhil

Conversation

@Akhileswara-Microsoft

Copy link
Copy Markdown

Purpose

This pull request introduces a utility function to standardize and improve the formatting of chat API answers before returning them from the Completion function. The main focus is on extracting and cleaning up the response text, especially when the answer is returned as a JSON object or contains extraneous formatting.

Answer formatting and extraction improvements:

  • Added a new getDisplayAnswer function to handle various answer types, clean up code block formatting, and extract the main response from JSON or stringified objects.
  • Updated the Completion function to use getDisplayAnswer, ensuring that the answer field in the returned object is consistently formatted for display.

Does this introduce a breaking change?

  • Yes
  • No

Golden Path Validation

  • I have tested the primary workflows (the "golden path") to ensure they function correctly without errors.

Deployment Validation

  • I have validated the deployment process successfully and all services are running as expected with this change.

Other Information

This pull request refines how chat API responses are handled and displayed by introducing a new function to extract and clean up the answer text. The main focus is on improving the reliability and readability of the answer field returned from the chat API.

Improvements to API response handling:

  • Added a new getDisplayAnswer function to robustly extract and format the answer text from potentially complex or malformed API responses, handling JSON and string cases, and gracefully managing errors.
  • Updated the Completion function to use getDisplayAnswer so that the returned answer field is always a clean, user-friendly string.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new formatting helper currently loses its cleanup in the fallback return path and the regex-based JSON fallback can truncate responses containing escaped quotes.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR updates the frontend chat API client to standardize how the answer field is prepared for display, especially when the backend returns JSON-shaped content or wrapped formatting.

Changes:

  • Added getDisplayAnswer(answer: unknown) to normalize answer into a display-friendly string and attempt extraction of a "response" field from JSON.
  • Updated Completion to return the same API response but with answer rewritten via getDisplayAnswer.
File summaries
File Description
App/frontend-app/src/api/chatService.ts Introduces getDisplayAnswer and applies it in Completion to normalize/extract the display answer text.
Review details

Suppressed comments (1)

App/frontend-app/src/api/chatService.ts:55

  • The function strips code-fence/whitespace into content, but the final fallback returns answerText instead of content, so the cleanup is lost when JSON parsing/extraction doesn't succeed.
    return answerText;
}
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread App/frontend-app/src/api/chatService.ts Outdated
…er JSON parsing and fallback for string replacements
Copilot AI review requested due to automatic review settings September 7, 2026 13:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new getDisplayAnswer implementation contains a TypeScript typing issue and a fallback return that bypasses the intended cleanup logic.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 2
  • Review effort level: Lite

Comment thread App/frontend-app/src/api/chatService.ts
Comment thread App/frontend-app/src/api/chatService.ts Outdated
Copilot AI review requested due to automatic review settings September 7, 2026 13:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new answer-extraction logic has confirmed edge cases where it will fail to extract/clean common response shapes (JSON primitives, non-```json fences, and reordered/partial JSON fields).

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details

Suppressed comments (1)

App/frontend-app/src/api/chatService.ts:45

  • The fallback regex requires a very specific JSON shape/order (it only matches when "followings" appears immediately after "response"). If the API omits followings or reorders fields, extraction will fail and the UI will show the full raw JSON instead of the response text.
        const responseMatch = content.match(/"response"\s*:\s*"((?:\\.|[^"\\])*)"\s*,\s*"followings"\s*:/i);
  • Files reviewed: 1/1 changed files
  • Comments generated: 2
  • Review effort level: Lite

Comment thread App/frontend-app/src/api/chatService.ts Outdated
Comment thread App/frontend-app/src/api/chatService.ts
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings September 7, 2026 13:59
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new formatter introduces non-localized user-facing fallback text and has a few consistency/resilience gaps (cleanup not applied to extracted/parsed strings; unnecessary error rethrow) that should be addressed before approval.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details

Suppressed comments (3)

Previously missed (2) — in code that hasn't changed since the last review.

App/frontend-app/src/api/chatService.ts:16

  • Avoid hardcoding a user-facing English fallback string in this API utility; it bypasses the app’s i18n patterns and changes downstream error detection (callers treat any non-empty answer as success). Prefer returning an empty string for null/undefined and let the UI layer provide localized messaging.
    App/frontend-app/src/api/chatService.ts:47
  • getDisplayAnswer is a display-formatting helper; rethrowing non-SyntaxError exceptions from the JSON-parse attempt can bubble up and fail the whole Completion call. Since this block is best-effort parsing, swallow parse errors and fall back to returning content.

App/frontend-app/src/api/chatService.ts:41

  • When extracting response from parsed JSON, returning it directly can bypass the same cleanup logic (trim/code-fence removal / nested JSON-string handling). Format the extracted string via getDisplayAnswer for consistent display output.
                return response;
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment thread App/frontend-app/src/api/chatService.ts Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings September 7, 2026 14:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new JSON extraction logic is case-sensitive for "response" and the malformed-JSON fallback regex is overly order-dependent, which can cause valid response payloads to display as raw JSON instead of the intended text.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details

Suppressed comments (1)

App/frontend-app/src/api/chatService.ts:48

  • The SyntaxError fallback regex only matches when "followings" appears immediately after "response". If the LLM returns fields in a different order (or omits followings), we won’t extract the response even though the string clearly contains it. Consider matching the response field without depending on a specific subsequent key.
        const responseMatch = content.match(/"response"\s*:\s*"((?:\\.|[^"\\])*)"\s*,\s*"followings"\s*:/i);
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment thread App/frontend-app/src/api/chatService.ts
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings September 7, 2026 14:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The new fallback regex for extracting "response" is too strict about JSON field ordering and can fail to extract valid answers, causing incorrect UI output.

Review details

Suppressed comments (1)

Previously missed (1) — in code that hasn't changed since the last review.

App/frontend-app/src/api/chatService.ts:52

  • The fallback regex extraction is overly strict because it requires the "followings" field to appear immediately after "response". If the model outputs fields in a different order (or omits followings), this will fail to extract the response even though a response string is present in the payload.
  • Files reviewed: 1/1 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

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 this pull request may close these issues.

2 participants