From d412e43696f6cc01fdc8a161fef9d164a862fa0b Mon Sep 17 00:00:00 2001 From: mijauexe Date: Wed, 26 Mar 2025 16:31:01 +0100 Subject: [PATCH] rendering messages WIP --- core/agents/developer.py | 5 +- core/agents/mixins.py | 3 +- core/agents/tech_lead.py | 10 ++- core/cli/helpers.py | 117 ++++++++++++++++++++++++++++---- core/cli/main.py | 89 +++++++++++++++++++++++- core/config/actions.py | 6 +- core/db/models/project_state.py | 1 + core/db/models/user_input.py | 4 +- core/state/state_manager.py | 4 +- core/ui/base.py | 10 ++- core/ui/console.py | 4 +- core/ui/ipc_client.py | 8 ++- core/ui/virtual.py | 4 +- 13 files changed, 234 insertions(+), 31 deletions(-) diff --git a/core/agents/developer.py b/core/agents/developer.py index 979593d915..ccce91d3d6 100644 --- a/core/agents/developer.py +++ b/core/agents/developer.py @@ -13,7 +13,7 @@ from core.config import PARSE_TASK_AGENT_NAME, TASK_BREAKDOWN_AGENT_NAME from core.config.actions import ( DEV_TASK_BREAKDOWN, DEV_TASK_REVIEW_FEEDBACK, - DEV_TASK_STARTING, + DEV_TASK_START, DEV_TROUBLESHOOT, DEV_WAIT_TEST, ) @@ -257,7 +257,7 @@ class Developer(ChatWithBreakdownMixin, RelevantFilesMixin, BaseAgent): # There might be state leftovers from previous tasks that we need to clean here self.next_state.modified_files = {} self.set_next_steps(response, source) - self.next_state.action = f"Task #{current_task_index + 1} start" + self.next_state.action = DEV_TASK_START.format({current_task_index + 1}) await telemetry.trace_code_event( "task-start", { @@ -325,7 +325,6 @@ class Developer(ChatWithBreakdownMixin, RelevantFilesMixin, BaseAgent): "task_index": task_index, } ) - self.next_state.action = DEV_TASK_STARTING.format(task_index) await self.send_message(f"Starting task #{task_index} with the description:\n\n" + description) if self.current_state.run_command: await self.ui.send_run_command(self.current_state.run_command) diff --git a/core/agents/mixins.py b/core/agents/mixins.py index 48ec9448ed..8a782b493e 100644 --- a/core/agents/mixins.py +++ b/core/agents/mixins.py @@ -8,6 +8,7 @@ from core.agents.convo import AgentConvo from core.agents.response import AgentResponse from core.cli.helpers import get_line_changes from core.config import GET_RELEVANT_FILES_AGENT_NAME, TASK_BREAKDOWN_AGENT_NAME, TROUBLESHOOTER_BUG_REPORT +from core.config.actions import MIX_BREAKDOWN_CHAT_PROMPT from core.config.constants import CONVO_ITERATIONS_LIMIT from core.llm.parser import JSONParser from core.log import get_logger @@ -56,7 +57,7 @@ class ChatWithBreakdownMixin: ) chat = await self.ask_question( - "Are you happy with the breakdown? Now is a good time to ask questions or suggest changes.", + MIX_BREAKDOWN_CHAT_PROMPT, buttons={"yes": "Yes, looks good!"}, default="yes", verbose=False, diff --git a/core/agents/tech_lead.py b/core/agents/tech_lead.py index 24cc46061d..956430cbe8 100644 --- a/core/agents/tech_lead.py +++ b/core/agents/tech_lead.py @@ -9,7 +9,13 @@ from core.agents.convo import AgentConvo from core.agents.mixins import RelevantFilesMixin from core.agents.response import AgentResponse from core.config import TECH_LEAD_EPIC_BREAKDOWN, TECH_LEAD_PLANNING -from core.config.actions import TL_CREATE_INITIAL_EPIC, TL_CREATE_PLAN, TL_INITIAL_PROJECT_NAME, TL_START_FEATURE +from core.config.actions import ( + TL_CREATE_INITIAL_EPIC, + TL_CREATE_PLAN, + TL_EDIT_DEV_PLAN, + TL_INITIAL_PROJECT_NAME, + TL_START_FEATURE, +) from core.db.models import Complexity from core.db.models.project_state import TaskStatus from core.llm.parser import JSONParser @@ -297,7 +303,7 @@ class TechLead(RelevantFilesMixin, BaseAgent): await self.ui.send_project_stage({"stage": ProjectStage.OPEN_PLAN}) response = await self.ask_question( - "Open and edit your development plan in the Progress tab", + TL_EDIT_DEV_PLAN, buttons={"done_editing": "I'm done editing, the plan looks good"}, default="done_editing", buttons_only=True, diff --git a/core/cli/helpers.py b/core/cli/helpers.py index ccf95e14f5..15c9776936 100644 --- a/core/cli/helpers.py +++ b/core/cli/helpers.py @@ -16,8 +16,8 @@ from core.config.actions import ( BH_WAIT_BUG_REP_INSTRUCTIONS, CM_UPDATE_FILES, DEV_TASK_BREAKDOWN, - DEV_TASK_STARTING, DEV_TROUBLESHOOT, + MIX_BREAKDOWN_CHAT_PROMPT, TC_TASK_DONE, ) from core.config.env_importer import import_from_dotenv @@ -271,6 +271,49 @@ async def list_projects_json(db: SessionManager): print(json.dumps(data, indent=2)) +def find_first_todo_task(tasks): + """ + Find the first task with status 'todo' from a list of tasks. + + :param tasks: List of task objects + :return: First task with status 'todo', or None if not found + """ + for task in tasks: + if task.get("status") == "todo": + return task + return None + + +def trim_logs(logs: str) -> str: + """ + Trim logs by removing everything after specific marker phrases. + + This function cuts off the string at the first occurrence of + "Here are the backend logs" or "Here are the frontend logs". + + :param logs: Log text to trim + :return: Trimmed log text with the marker phrase removed + """ + if not logs: + return "" + + # Define marker phrases + markers = ["Here are the backend logs", "Here are the frontend logs"] + + # Find the first occurrence of any marker + index = float("inf") + for marker in markers: + pos = logs.find(marker) + if pos != -1 and pos < index: + index = pos + + # If a marker was found, trim the string + if index != float("inf"): + return logs[:index] + + return logs + + async def load_convo( sm: StateManager, project_id: Optional[UUID] = None, @@ -293,18 +336,19 @@ async def load_convo( prev_state = project_states[i - 1] if i > 0 else None convo_el = {} - ui = await sm.find_user_input(state, branch_id) + convo_el["id"] = str(state.id) + user_inputs = await sm.find_user_input(state, branch_id) - if ui is not None and ui.question is not None: - convo_el["question"] = ui.question - if ui.answer_text is not None: - convo_el["answer"] = str(ui.answer_text) - else: - convo_el["answer"] = str(ui.answer_button) + if user_inputs: + convo_el["user_inputs"] = [] + for ui in user_inputs: + if ui.question: + answer = trim_logs(ui.answer_text) if ui.answer_text is not None else ui.answer_button + convo_el["user_inputs"].append({"question": ui.question, "answer": answer}) if state.action is not None: - if DEV_TASK_STARTING[:-2] in state.action: - task_counter = int(state.action.split("#")[-1]) + if "Task" in state.action and "start" in state.action: + task_counter = int(state.action.split("#")[1].split()[0]) elif state.action == DEV_TROUBLESHOOT.format(task_counter): if state.iterations is not None: @@ -324,25 +368,32 @@ async def load_convo( convo_el["instructions"] = task["instructions"] elif state.action == TC_TASK_DONE.format(task_counter): - if len(state.tasks) > 0: + if state.tasks: + # find the next task description and print it in question + next_task = find_first_todo_task(state.tasks) + # next_task_alt = find_first_todo_task(prev_state.tasks) + if next_task: + convo_el["task_description"] = next_task["description"] task = state.tasks[task_counter - 1] if "test_instructions" in task and task["test_instructions"] is not None: convo_el["test_instructions"] = task["test_instructions"] elif state.action == CM_UPDATE_FILES: - files = {} + files = [] for steps in state.steps: + file = {} if "save_file" in steps and "path" in steps["save_file"]: path = steps["save_file"]["path"] - files["path"] = path + file["path"] = path current_file = await sm.get_file_for_project(state.id, path) prev_file = await sm.get_file_for_project(prev_state.id, path) if current_file and prev_file: - files["diff"] = get_line_changes( + file["diff"] = get_line_changes( old_content=prev_file.content.content, new_content=current_file.content.content ) + files.append(file) convo_el["files"] = files @@ -383,7 +434,43 @@ async def load_convo( if "initial_explanation" in si and si["initial_explanation"] is not None: convo_el["initial_explanation"] = si["initial_explanation"] - if len(convo_el.keys()) > 0: + if convo_el.get("user_inputs", None) is not None or convo_el.get("files", None) is not None: + for j, ui in enumerate(user_inputs): + if ui.question and ui.question == MIX_BREAKDOWN_CHAT_PROMPT: + if len(state.tasks) == 1: + if state.tasks[0]["instructions"] is not None: + # convo_el["user_inputs"].insert(j, {'question': state.tasks[task_counter - 1]["instructions"]}) + convo_el["breakdown"] = state.tasks[task_counter - 1]["instructions"] + break + elif ( + state.tasks[task_counter - 1] is not None + and state.tasks[task_counter - 1]["instructions"] is not None + ): + # convo_el["user_inputs"].insert(j, {'question': state.tasks[task_counter - 1]["instructions"]}) + convo_el["breakdown"] = state.tasks[task_counter - 1]["instructions"] + break + elif state.iterations: + if ( + state.iterations[-1].get("bug_hunting_cycles", None) is not None + and state.iterations[-1]["bug_hunting_cycles"] + and state.iterations[-1]["bug_hunting_cycles"][-1].get("human_readable_instructions", None) + is not None + ): + # convo_el["user_inputs"].insert(j, {'question': state.iterations[-1]["bug_hunting_cycles"][-1]["human_readable_instructions"]}) + convo_el["breakdown"] = state.iterations[-1]["bug_hunting_cycles"][-1][ + "human_readable_instructions" + ] + break + else: + # try to get the next state's instructions from the task + next_state = project_states[i + 1] + if next_state.tasks is not None: + next_task = find_first_todo_task(next_state.tasks) + if next_task: + # convo_el["user_inputs"].insert(j, {'question': next_task["description"]}) + convo_el["task_description"] = next_task["description"] + break + convo_el["action"] = state.action convo.append(convo_el) diff --git a/core/cli/main.py b/core/cli/main.py index a3296a7811..cf1c26fa26 100644 --- a/core/cli/main.py +++ b/core/cli/main.py @@ -5,6 +5,8 @@ import sys from argparse import Namespace from asyncio import run +from core.config.actions import TL_EDIT_DEV_PLAN + try: import sentry_sdk from sentry_sdk.integrations.asyncio import AsyncioIntegration @@ -30,12 +32,23 @@ from core.llm.base import APIError from core.log import get_logger from core.state.state_manager import StateManager from core.telemetry import telemetry -from core.ui.base import ProjectStage, UIBase, UIClosedError, UserInput, pythagora_source +from core.ui.base import ( + ProjectStage, + UIBase, + UIClosedError, + UserInput, + bug_hunter_source, + developer_source, + history_source1, + history_source2, + pythagora_source, +) log = get_logger(__name__) telemetry_sent = False +source_alt = True def init_sentry(): @@ -194,6 +207,14 @@ async def start_new_project(sm: StateManager, ui: UIBase) -> bool: return project_state is not None +def alternate_source(): + """Toggle between pythagora_source and history_source1.""" + global source_alt + source = history_source1 if source_alt else history_source2 + source_alt = not source_alt + return source + + async def run_pythagora_session(sm: StateManager, ui: UIBase, args: Namespace): """ Run a Pythagora session. @@ -206,6 +227,72 @@ async def run_pythagora_session(sm: StateManager, ui: UIBase, args: Namespace): if args.project and args.step: convo = await load_convo(sm, args.project, args.step) + # jsonnnnn = json.dumps(convo, indent=4) + + for msg in convo: + if "breakdown" in msg: + await ui.send_message(msg["breakdown"], source=developer_source, project_state_id=msg["id"]) + + if "instructions" in msg: + await ui.send_message(msg["instructions"], source=bug_hunter_source, project_state_id=msg["id"]) + + if "task_description" in msg: + await ui.send_message(msg["task_description"], source=developer_source, project_state_id=msg["id"]) + + if "user_inputs" in msg and msg["user_inputs"]: + for input_item in msg["user_inputs"]: + # Process question if present + if "question" in input_item: + await ui.send_message( + input_item["question"], source=alternate_source(), project_state_id=msg["id"] + ) + + # Process answer if present + if "answer" in input_item: + if input_item["question"] == TL_EDIT_DEV_PLAN: + await ui.send_message( + "Starting task with the description:\n\n" + input_item["answer"], + project_state_id=msg["id"], + ) + else: + await ui.send_message( + input_item["answer"], source=alternate_source(), project_state_id=msg["id"] + ) + + if "test_instructions" in msg: + await ui.send_test_instructions( + msg["test_instructions"], project_state_id=msg["id"], source=alternate_source() + ) + + if "files" in msg: + source = alternate_source() + for f in msg["files"]: + await ui.send_file_status(f["path"], "done", source=source) + + # Process any other fields in the message + for key, value in msg.items(): + if ( + key + not in [ + "user_inputs", + "action", + "id", + "test_instructions", + "files", + "task_description", + "breakdown", + ] + and value + ): + if isinstance(value, str): + await ui.send_message(f"{key}: {value}", source=alternate_source(), project_state_id=msg["id"]) + elif isinstance(value, dict): + for k, v in value.items(): + if v and isinstance(v, str): + await ui.send_message( + f"{k}: {v}", source=alternate_source(), project_state_id=msg["id"] + ) + log.debug(f"Convo exists: {len(convo) > 0}") if args.project or args.branch or args.step: diff --git a/core/config/actions.py b/core/config/actions.py index 103b2e13e7..e01687a2e3 100644 --- a/core/config/actions.py +++ b/core/config/actions.py @@ -7,7 +7,7 @@ CM_UPDATE_FILES = "Updating files" DEV_WAIT_TEST = "Awaiting user test" -DEV_TASK_STARTING = "Starting task #{}" +DEV_TASK_START = "Task #{} start" DEV_TASK_BREAKDOWN = "Task #{} breakdown" DEV_TROUBLESHOOT = "Troubleshooting #{}" DEV_TASK_REVIEW_FEEDBACK = "Task review feedback" @@ -39,3 +39,7 @@ TS_TASK_REVIEWED = "Task #{} reviewed" TS_ALT_SOLUTION = "Alternative solution (attempt #{})" PS_EPIC_COMPLETE = "Epic {} completed" + +# other constants +TL_EDIT_DEV_PLAN = "Open and edit your development plan in the Progress tab" +MIX_BREAKDOWN_CHAT_PROMPT = "Are you happy with the breakdown? Now is a good time to ask questions or suggest changes." diff --git a/core/db/models/project_state.py b/core/db/models/project_state.py index 49ce930ea2..1eef30e3da 100644 --- a/core/db/models/project_state.py +++ b/core/db/models/project_state.py @@ -226,6 +226,7 @@ class ProjectState(Base): branch = branch.scalar_one_or_none() project_states_result = await session.execute(select(ProjectState).where(ProjectState.branch_id == branch.id)) + # project_states_result = await session.execute(select(ProjectState).where(and_(ProjectState.branch_id == branch.id), ProjectState.action.isnot(None))) return project_states_result.scalars().all() async def create_next_state(self) -> "ProjectState": diff --git a/core/db/models/user_input.py b/core/db/models/user_input.py index a5493c61e9..c262836bb8 100644 --- a/core/db/models/user_input.py +++ b/core/db/models/user_input.py @@ -60,7 +60,7 @@ class UserInput(Base): return obj @staticmethod - async def find_user_input(session: AsyncSession, project_state, branch_id) -> Optional["UserInput"]: + async def find_user_inputs(session: AsyncSession, project_state, branch_id) -> Optional[list["UserInput"]]: from core.db.models import UserInput user_input = await session.execute( @@ -78,4 +78,4 @@ class UserInput(Base): ) user_input = user_input.scalars().all() - return user_input[0] if len(user_input) > 0 else None + return user_input if len(user_input) > 0 else [] diff --git a/core/state/state_manager.py b/core/state/state_manager.py index 3259c5f9e0..0570dafefe 100644 --- a/core/state/state_manager.py +++ b/core/state/state_manager.py @@ -90,9 +90,9 @@ class StateManager: async with self.session_manager as session: return await Project.get_branches_for_project_id(session, project_id) - async def find_user_input(self, project_state, branch_id) -> Optional["UserInput"]: + async def find_user_input(self, project_state, branch_id) -> Optional[list["UserInput"]]: async with self.session_manager as session: - return await UserInput.find_user_input(session, project_state, branch_id) + return await UserInput.find_user_inputs(session, project_state, branch_id) async def get_file_for_project(self, state_id: UUID, path: str): async with self.session_manager as session: diff --git a/core/ui/base.py b/core/ui/base.py index de78fcf584..983925a1d9 100644 --- a/core/ui/base.py +++ b/core/ui/base.py @@ -368,7 +368,9 @@ class UIBase: """ raise NotImplementedError() - async def send_test_instructions(self, test_instructions: str, project_state_id: Optional[str] = None): + async def send_test_instructions( + self, test_instructions: str, project_state_id: Optional[str] = None, source: Optional[UISource] = None + ): """ Send test instructions. @@ -474,6 +476,12 @@ class UIBase: pythagora_source = UISource("Pythagora", "pythagora") + +developer_source = AgentSource("Developer", "developer") +bug_hunter_source = AgentSource("Bug Hunter", "bug-hunter") + +history_source1 = UISource("Developer", "developer") +history_source2 = UISource("Bug Hunter", "bug-hunter") success_source = UISource("Congratulations", "success") diff --git a/core/ui/console.py b/core/ui/console.py index 28fe5248a8..3e369a6281 100644 --- a/core/ui/console.py +++ b/core/ui/console.py @@ -169,7 +169,9 @@ class PlainConsoleUI(UIBase): async def send_project_stats(self, stats: dict): pass - async def send_test_instructions(self, test_instructions: str, project_state_id: Optional[str] = None): + async def send_test_instructions( + self, test_instructions: str, project_state_id: Optional[str] = None, source: Optional[UISource] = None + ): pass async def knowledge_base_update(self, knowledge_base: dict): diff --git a/core/ui/ipc_client.py b/core/ui/ipc_client.py index d839e279b9..8cc6445fc8 100644 --- a/core/ui/ipc_client.py +++ b/core/ui/ipc_client.py @@ -497,7 +497,12 @@ class IPCClientUI(UIBase): content=stats, ) - async def send_test_instructions(self, test_instructions: str, project_state_id: Optional[str] = None): + async def send_test_instructions( + self, + test_instructions: str, + project_state_id: Optional[str] = None, + source: Optional[UISource] = None, + ): try: log.debug("Sending test instructions") parsed_instructions = json.loads(test_instructions) @@ -511,6 +516,7 @@ class IPCClientUI(UIBase): "test_instructions": parsed_instructions, }, project_state_id=project_state_id, + category=source.type_name if source else None, ) async def knowledge_base_update(self, knowledge_base: dict): diff --git a/core/ui/virtual.py b/core/ui/virtual.py index 58467b34a9..503a5aa5d6 100644 --- a/core/ui/virtual.py +++ b/core/ui/virtual.py @@ -168,7 +168,9 @@ class VirtualUI(UIBase): async def send_project_stats(self, stats: dict): pass - async def send_test_instructions(self, test_instructions: str, project_state_id: Optional[str] = None): + async def send_test_instructions( + self, test_instructions: str, project_state_id: Optional[str] = None, source: Optional[UISource] = None + ): pass async def knowledge_base_update(self, knowledge_base: dict):