diff --git a/backend/services/coach.go b/backend/services/coach.go index 7756118d..1ab75722 100644 --- a/backend/services/coach.go +++ b/backend/services/coach.go @@ -87,10 +87,18 @@ Evaluation Criteria: Original Weak Statement: "%s" User's Improved Argument: "%s" +Feedback Structure: +Provide constructive feedback divided into two clearly identified sections: +Strengths: +- [Specific strengths regarding clarity, logic, or persuasiveness] + +Areas for Improvement: +- [Specific actionable suggestions to strengthen the argument] + Required Output Format: { "score": X, - "feedback": "text describing strengths and areas for improvement" + "feedback": "Strengths:\n- ...\n\nAreas for Improvement:\n- ..." } Provide ONLY the JSON output without additional text or markdown formatting.`, diff --git a/frontend/src/Pages/StrengthenArgument.tsx b/frontend/src/Pages/StrengthenArgument.tsx index e12b7e7e..59c2d6f8 100644 --- a/frontend/src/Pages/StrengthenArgument.tsx +++ b/frontend/src/Pages/StrengthenArgument.tsx @@ -47,6 +47,167 @@ interface Notification { variant: "default" | "destructive"; } +interface BodyContent { + type: "bullet" | "numbered" | "mixed-lines" | "paragraph" | "empty"; + items?: string[]; + raw?: string; +} + +interface ParsedSection { + title: string; + content: BodyContent; +} + +function findHeading( + text: string, + pattern: RegExp +): { start: number; end: number; title: string } | null { + const match = pattern.exec(text); + if (!match) return null; + const fullMatch = match[0]; + const matchIndex = match.index; + const leadingDelim = fullMatch.match(/^(?:\n|[.!?]\s+)/); + const delimLen = leadingDelim ? leadingDelim[0].length : 0; + return { + start: matchIndex + delimLen, + end: matchIndex + fullMatch.length, + title: match[1] || match[2] || match[0], + }; +} + +function parseBodyContent(bodyText: string): BodyContent { + const lines = bodyText.split("\n").map((l) => l.trim()).filter(Boolean); + if (lines.length === 0) return { type: "empty" }; + + const isBulletList = lines.every((l) => /^[-*•]\s+/.test(l)); + const isNumberedList = lines.every((l) => /^\d+[\.\)]\s+/.test(l)); + + if (isBulletList) { + return { + type: "bullet", + items: lines.map((l) => l.replace(/^[-*•]\s+/, "")), + }; + } + if (isNumberedList) { + return { + type: "numbered", + items: lines.map((l) => l.replace(/^\d+[\.\)]\s+/, "")), + }; + } + const hasBulletsOrNumbers = lines.some((l) => /^[-*•]\s+|^\d+[\.\)]\s+/.test(l)); + if (hasBulletsOrNumbers) { + return { + type: "mixed-lines", + items: lines, + }; + } + return { + type: "paragraph", + raw: bodyText.trim(), + }; +} + +function renderFormattedFeedback(text: string) { + if (!text || !text.trim()) return null; + + const sPattern = + /(?:^|\n|[.!?]\s+)(?:\*\*(strengths?)(?:\*\*\s*:|\s*:\*\*)|(strengths?)\s*:)\s*/i; + const iPattern = + /(?:^|\n|[.!?]\s+)(?:\*\*(areas?\s+for\s+improvement|areas?\s+to\s+improve|improvements?)(?:\*\*\s*:|\s*:\*\*)|(areas?\s+for\s+improvement|areas?\s+to\s+improve|improvements?)\s*:)\s*/i; + + const sMatch = findHeading(text, sPattern); + const iMatch = findHeading(text, iPattern); + + if (!sMatch && !iMatch) { + return ( +

+ {text.trim()} +

+ ); + } + + const headings: { start: number; end: number; title: string }[] = []; + if (sMatch) headings.push({ ...sMatch, title: "Strengths" }); + if (iMatch) headings.push({ ...iMatch, title: "Areas for Improvement" }); + headings.sort((a, b) => a.start - b.start); + + const introText = text.slice(0, headings[0].start).trim(); + const rawSections: ParsedSection[] = []; + + for (let idx = 0; idx < headings.length; idx++) { + const current = headings[idx]; + const nextStart = idx + 1 < headings.length ? headings[idx + 1].start : text.length; + const bodyText = text.slice(current.end, nextStart).trim(); + rawSections.push({ + title: current.title, + content: parseBodyContent(bodyText), + }); + } + + const sections = rawSections.filter((s) => s.content.type !== "empty"); + + if (sections.length === 0) { + return ( +

+ {text.trim()} +

+ ); + } + + return ( +
+ {introText && ( +

+ {introText} +

+ )} + {sections.map((section) => ( +
+

+ {section.title} +

+ {section.content.type === "bullet" && ( + + )} + {section.content.type === "numbered" && ( +
    + {section.content.items?.map((item, idx) => ( +
  1. + {item} +
  2. + ))} +
+ )} + {section.content.type === "paragraph" && ( +

+ {section.content.raw} +

+ )} + {section.content.type === "mixed-lines" && ( +
+ {section.content.items?.map((item, idx) => ( +

+ {item} +

+ ))} +
+ )} +
+ ))} +
+ ); +} + const StrengthenArgument: React.FC = () => { const [topic, setTopic] = useState(""); const [stance, setStance] = useState("for"); @@ -305,7 +466,7 @@ const StrengthenArgument: React.FC = () => { - Weak Opening Statement for {weakStatement.stance} the topic "{weakStatement.topic}" + Weak Opening Statement ({weakStatement.stance.toLowerCase() === "against" ? "Against" : "For"}) on the topic "{weakStatement.topic}" @@ -338,20 +499,27 @@ const StrengthenArgument: React.FC = () => { )} - {/* Feedback Dialog (Unchanged) */} + {/* Feedback Dialog */} - + Feedback - - {score !== null && ( -

- {animatedScore.toString().padStart(2, "0")} -

- )} - {feedback &&

{feedback}

} + + Review your argument score and feedback from the debate coach.
+
+ {score !== null && ( +

+ {animatedScore.toString().padStart(2, "0")} +

+ )} +
+ {feedback && ( +
+ {renderFormattedFeedback(feedback)} +
+ )}