From 7344c2330fdba1203f447f84b06cf08bb4ebb736 Mon Sep 17 00:00:00 2001 From: Aaron Ruan Date: Tue, 26 May 2026 21:57:32 +0800 Subject: [PATCH] Update --- src/letscode/main.py | 186 ++++++++++++++++++++++++++++++++++++++- src/letscode/solution.py | 5 ++ tests/test_main.py | 25 ++++++ tests/test_solution.py | 10 +++ 4 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 tests/test_main.py diff --git a/src/letscode/main.py b/src/letscode/main.py index 697a12c..c09e955 100644 --- a/src/letscode/main.py +++ b/src/letscode/main.py @@ -1,7 +1,9 @@ import asyncio import os import subprocess +from dataclasses import dataclass from pathlib import Path +from typing import Literal from dotenv import load_dotenv from textual.app import App, ComposeResult @@ -14,6 +16,7 @@ from textual.widgets import ( Footer, Header, Markdown, + Static, TabbedContent, TabPane, TextArea, @@ -35,11 +38,36 @@ from .solution import ( editor_command, ensure_solution_file, read_solution_file, + reset_solution_file, solution_path, write_solution_file, ) from .storage import GeneratedHint, ProblemStore +ChatRole = Literal["user", "agent"] + + +@dataclass(frozen=True) +class ChatEntry: + role: ChatRole + content: str + + +class ChatBubble(Horizontal): + """A single chat message row.""" + + def __init__(self, entry: ChatEntry) -> None: + super().__init__(classes=f"chat-row {entry.role}") + self.entry = entry + + def compose(self) -> ComposeResult: + content = self.entry.content.strip() or "No content." + yield Static( + content, + classes=f"chat-bubble {self.entry.role}", + markup=False, + ) + class PromptInput(TextArea): """Multiline prompt input that submits on Enter.""" @@ -92,6 +120,8 @@ class LetsCodeApp(App): HINTS_SELECTOR = "#hints" JUDGE_SELECTOR = "#judge-output" SOLUTION_PREVIEW_SELECTOR = "#solution-preview" + CHAT_CONTAINER_SELECTOR = "#chat" + CHAT_TAB_ID = "chat-tab" TESTFIRE_BUTTON_ID = "testfire-button" FULL_TEST_BUTTON_ID = "full-test-button" @@ -100,6 +130,7 @@ class LetsCodeApp(App): LOAD_PROBLEM_COMMAND = "/load_problem" HINT_COMMAND = "/hint" FIX_COMMAND = "/fix" + RESET_COMMAND = "/reset" CSS = """\ Screen { @@ -117,11 +148,37 @@ class LetsCodeApp(App): padding: 1; } - #problem, #hints, #judge-output, #solution-preview { + #problem, #hints, #judge-output, #solution-preview, #chat { height: auto; background: transparent; } + .chat-row { + height: auto; + width: 100%; + margin-bottom: 1; + } + + .chat-bubble { + height: auto; + padding: 1; + } + + .chat-bubble.user { + background: $primary 20%; + border-left: $primary thick; + } + + .chat-bubble.agent { + background: $surface; + border-left: $foreground thick; + } + + .chat-empty { + height: auto; + color: $text-muted; + } + #judge-buttons { height: auto; margin-bottom: 1; @@ -165,9 +222,21 @@ class LetsCodeApp(App): Markdown("# Loading problem..."), id="hints", ) + with TabPane("Chat", id=self.CHAT_TAB_ID): + yield ScrollableContainer( + Static( + "No messages yet. Type a question or request below.", + classes="chat-empty", + markup=False, + ), + id="chat", + ) yield PromptInput( language="markdown", - placeholder="Type your commands. Enter submits, Alt+Enter adds a new line.", + placeholder=( + "Ask the agent or type a command. Enter submits, Alt+Enter adds " + "a new line." + ), id="input", compact=True, highlight_cursor_line=False, @@ -181,6 +250,7 @@ class LetsCodeApp(App): self.current_fetched_hints_markdown: list[str] = [] self.current_generated_hints_markdown: list[str] = [] self.generated_hints_count = 0 + self.chat_entries: list[ChatEntry] = [] self.agent: LetsCodeAgent | None = None self.current_judge: GeneratedJudge | None = None self.action_load_problem() @@ -200,6 +270,9 @@ class LetsCodeApp(App): if command == self.FIX_COMMAND: return self._handle_fix_command(args) + if command == self.RESET_COMMAND: + return self._handle_reset_command(args) + return False def _handle_load_problem_command(self, args: list[str]) -> bool: @@ -231,10 +304,20 @@ class LetsCodeApp(App): self.run_worker(self.fix_solution(suite), exclusive=True) return True + def _handle_reset_command(self, args: list[str]) -> bool: + if len(args) > 1 or (args and not args[0].isdigit()): + self.notify("Usage: /reset [question_id]", severity="warning") + return True + + question_id = int(args[0]) if args else None + self.run_worker(self.reset_solution(question_id), exclusive=True) + return True + async def load_problem(self, question_id: int) -> None: self.current_problem_id = question_id self.agent = None self.current_judge = None + self.chat_entries = [] problem_markdown = await load_problem_markdown(question_id) self.current_problem_markdown = problem_markdown.problem @@ -255,6 +338,7 @@ class LetsCodeApp(App): problem_markdown.fetched_hints, problem_markdown.generated_hints, ) + await self._render_chat_log() await self.prepare_judge() async def _render_hints( @@ -502,6 +586,55 @@ class LetsCodeApp(App): else: self.notify("Fix written, but tests still fail.", severity="warning") + async def reset_solution(self, question_id: int | None = None) -> None: + if question_id is None and not self.current_problem_markdown: + self.notify("Problem is still loading.", severity="warning") + return + + target_question_id = ( + self.current_problem_id if question_id is None else question_id + ) + is_current_problem = target_question_id == self.current_problem_id and bool( + self.current_problem_markdown + ) + + judge_output = self.query_one(self.JUDGE_SELECTOR, Markdown) + await judge_output.update( + f"# Judge\n\nResetting solution for problem {target_question_id}..." + ) + + try: + if is_current_problem: + judge = await self._ensure_judge() + else: + problem_markdown = await load_problem_markdown(target_question_id) + model_slug = os.getenv("LETSCODE_AGENT_MODEL", self.DEFAULT_AGENT_MODEL) + judge = await asyncio.to_thread( + generate_judge, + problem_markdown.problem, + model_slug, + ) + + path = solution_path(judge) + reset_solution_file(path, judge.initial_code_snippet) + except Exception as error: + await judge_output.update(f"# Judge\n\nUnable to reset solution: `{error}`") + self.notify(f"Unable to reset solution: {error}", severity="error") + return + + if is_current_problem: + await self._update_solution_preview(judge.initial_code_snippet, path) + if self.agent is not None: + self.agent.set_solution_context( + solution_code=judge.initial_code_snippet, + judge_output="", + ) + + await judge_output.update( + f"# Judge\n\nReset solution for problem {target_question_id} at `{path}`." + ) + self.notify(f"Reset solution for problem {target_question_id}.") + async def open_solution_editor(self) -> None: if not self.current_problem_markdown: self.notify("Problem is still loading.", severity="warning") @@ -604,6 +737,53 @@ class LetsCodeApp(App): f"{label}\n\n```python\n{preview}\n```" ) + async def chat_with_agent(self, message: str) -> None: + if not self.current_problem_markdown: + self.notify("Problem is still loading.", severity="warning") + return + + self.chat_entries.append(ChatEntry("user", message)) + pending_index = len(self.chat_entries) + self.chat_entries.append(ChatEntry("agent", "Thinking...")) + self.query_one(TabbedContent).active = self.CHAT_TAB_ID + await self._render_chat_log() + + try: + response = await asyncio.to_thread(self.get_agent().chat, message) + except Exception as error: + self.chat_entries[pending_index] = ChatEntry( + "agent", + f"Unable to get a response: {error}", + ) + await self._render_chat_log() + self.notify(f"Unable to chat with agent: {error}", severity="error") + return + + self.chat_entries[pending_index] = ChatEntry("agent", response) + await self._render_chat_log() + + async def _render_chat_log(self) -> None: + chat_container = self.query_one( + self.CHAT_CONTAINER_SELECTOR, + ScrollableContainer, + ) + await chat_container.remove_children() + + if not self.chat_entries: + await chat_container.mount( + Static( + "No messages yet. Type a question or request below.", + classes="chat-empty", + markup=False, + ) + ) + else: + await chat_container.mount( + *(ChatBubble(entry) for entry in self.chat_entries) + ) + + chat_container.scroll_end(animate=False) + async def on_prompt_input_submitted(self, event: PromptInput.Submitted) -> None: """Handle user input.""" input_widget = self.query_one(self.INPUT_SELECTOR, PromptInput) @@ -618,6 +798,8 @@ class LetsCodeApp(App): if message.startswith("/") and self.handle_slash_command(message): return + self.run_worker(self.chat_with_agent(message), exclusive=True) + def main() -> None: load_dotenv() diff --git a/src/letscode/solution.py b/src/letscode/solution.py index a8758d7..796d646 100644 --- a/src/letscode/solution.py +++ b/src/letscode/solution.py @@ -18,6 +18,11 @@ def ensure_solution_file(path: Path, initial_code: str) -> Path: return path +def reset_solution_file(path: Path, initial_code: str) -> Path: + write_solution_file(path, initial_code) + return path + + def write_solution_file(path: Path, solution_code: str) -> None: path.parent.mkdir(parents=True, exist_ok=True) path.write_text(solution_code, encoding="utf-8") diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 0000000..fc8f13a --- /dev/null +++ b/tests/test_main.py @@ -0,0 +1,25 @@ +import unittest + +from letscode.main import ChatBubble, ChatEntry + + +class ChatBubbleTest(unittest.TestCase): + def test_chat_bubble_uses_role_classes(self) -> None: + bubble = ChatBubble(ChatEntry("user", "How should I start?")) + child = list(bubble.compose())[0] + + self.assertTrue(bubble.has_class("chat-row")) + self.assertTrue(bubble.has_class("user")) + self.assertTrue(child.has_class("chat-bubble")) + self.assertTrue(child.has_class("user")) + + def test_chat_bubble_preserves_agent_role(self) -> None: + bubble = ChatBubble(ChatEntry("agent", "Look for a hash map invariant.")) + child = list(bubble.compose())[0] + + self.assertTrue(bubble.has_class("agent")) + self.assertTrue(child.has_class("agent")) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_solution.py b/tests/test_solution.py index 72762e5..06bdfce 100644 --- a/tests/test_solution.py +++ b/tests/test_solution.py @@ -6,6 +6,7 @@ from letscode.solution import ( editor_command, ensure_solution_file, read_solution_file, + reset_solution_file, write_solution_file, ) @@ -28,6 +29,15 @@ class SolutionFileTest(unittest.TestCase): self.assertEqual(read_solution_file(path), "class Solution:\n pass\n") + def test_reset_solution_file_overwrites_existing_solution(self) -> None: + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "nested" / "solution.py" + + write_solution_file(path, "custom\n") + reset_solution_file(path, "initial\n") + + self.assertEqual(read_solution_file(path), "initial\n") + def test_editor_command_splits_configured_editor(self) -> None: path = Path("/tmp/solution.py") -- knotmirror 0.0.1