rendering messages WIP

pull/1160/head
mijauexe 2 years ago
parent 609a96c3ff
commit d412e43696

@ -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)

@ -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,

@ -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,

@ -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)

@ -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:

@ -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."

@ -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":

@ -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 []

@ -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:

@ -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")

@ -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):

@ -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):

@ -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):

Loading…
Cancel
Save