fix: enhance the response in UI - #713
Conversation
…s and improve error handling
…e JSON stringification
…hen no answer is returned
There was a problem hiding this comment.
🟡 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 normalizeanswerinto a display-friendly string and attempt extraction of a"response"field from JSON. - Updated
Completionto return the same API response but withanswerrewritten viagetDisplayAnswer.
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 returnsanswerTextinstead ofcontent, 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.
…er JSON parsing and fallback for string replacements
There was a problem hiding this comment.
🟡 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
There was a problem hiding this comment.
🟡 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 omitsfollowingsor 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
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 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
answeras 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 getDisplayAnsweris a display-formatting helper; rethrowing non-SyntaxError exceptions from the JSON-parse attempt can bubble up and fail the wholeCompletioncall. Since this block is best-effort parsing, swallow parse errors and fall back to returningcontent.
App/frontend-app/src/api/chatService.ts:41
- When extracting
responsefrom parsed JSON, returning it directly can bypass the same cleanup logic (trim/code-fence removal / nested JSON-string handling). Format the extracted string viagetDisplayAnswerfor consistent display output.
return response;
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Lite
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 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
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 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 omitsfollowings), this will fail to extract the response even though aresponsestring is present in the payload.
- Files reviewed: 1/1 changed files
- Comments generated: 0 new
- Review effort level: Lite
Purpose
This pull request introduces a utility function to standardize and improve the formatting of chat API answers before returning them from the
Completionfunction. 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:
getDisplayAnswerfunction to handle various answer types, clean up code block formatting, and extract the main response from JSON or stringified objects.Completionfunction to usegetDisplayAnswer, ensuring that theanswerfield in the returned object is consistently formatted for display.Does this introduce a breaking change?
Golden Path Validation
Deployment Validation
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
answerfield returned from the chat API.Improvements to API response handling:
getDisplayAnswerfunction to robustly extract and format the answer text from potentially complex or malformed API responses, handling JSON and string cases, and gracefully managing errors.Completionfunction to usegetDisplayAnswerso that the returnedanswerfield is always a clean, user-friendly string.