Skip to content

Commit

Permalink
Merge pull request #1018 from Pythagora-io/cm
Browse files Browse the repository at this point in the history
Cm
  • Loading branch information
LeonOstrez authored Jun 19, 2024
2 parents 52be03b + 43d3191 commit 87d2942
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 159 deletions.
34 changes: 13 additions & 21 deletions core/agents/code_monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,6 @@ async def run(self) -> AgentResponse:
else:
return await self.implement_changes()

def _get_task_convo(self) -> AgentConvo:
# FIXME: Current prompts reuse task breakdown / iteration messages so we have to resort to this
task = self.current_state.current_task
current_task_index = self.current_state.tasks.index(task)

convo = AgentConvo(self).template(
"breakdown",
task=task,
iteration=None,
current_task_index=current_task_index,
)
# TODO: We currently show last iteration to the code monkey; we might need to show the task
# breakdown and all the iterations instead? To think about when refactoring prompts
if self.current_state.iterations:
convo.assistant(self.current_state.iterations[-1]["description"])
else:
convo.assistant(self.current_state.current_task["instructions"])
return convo

async def implement_changes(self) -> AgentResponse:
file_name = self.step["save_file"]["path"]

Expand All @@ -73,12 +54,23 @@ async def implement_changes(self) -> AgentResponse:
feedback = None

iterations = self.current_state.iterations
user_feedback = None
user_feedback_qa = None
llm = self.get_llm()
convo = self._get_task_convo().template(
if iterations:
instructions = iterations[-1]["description"]
user_feedback = iterations[-1]["user_feedback"]
user_feedback_qa = iterations[-1]["user_feedback_qa"]
else:
instructions = self.current_state.current_task["instructions"]

convo = AgentConvo(self).template(
"implement_changes",
file_name=file_name,
file_content=file_content,
instructions=iterations[-1]["description"] if iterations else task["instructions"],
instructions=instructions,
user_feedback=user_feedback,
user_feedback_qa=user_feedback_qa,
)
if feedback:
convo.assistant(f"```\n{self.prev_response.data['new_content']}\n```\n").template(
Expand Down
6 changes: 3 additions & 3 deletions core/agents/tech_lead.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DevelopmentPlan(BaseModel):


class UpdatedDevelopmentPlan(BaseModel):
updated_current_task: Task = Field(
updated_current_epic: Task = Field(
description="Updated detailed description of what was implemented while working on the current development task."
)
plan: list[Task] = Field(description="List of unfinished development tasks.")
Expand Down Expand Up @@ -198,8 +198,8 @@ async def update_epic(self) -> AgentResponse:
parser=JSONParser(UpdatedDevelopmentPlan),
temperature=0,
)
log.debug(f"Reworded last task as: {response.updated_current_task.description}")
finished_tasks[-1]["description"] = response.updated_current_task.description
log.debug(f"Reworded last task as: {response.updated_current_epic.description}")
finished_tasks[-1]["description"] = response.updated_current_epic.description

self.next_state.tasks = finished_tasks + [
{
Expand Down
71 changes: 27 additions & 44 deletions core/prompts/code-monkey/implement_changes.prompt
Original file line number Diff line number Diff line change
@@ -1,56 +1,39 @@
You are working on a project and your job is to implement new code changes based on given instructions.
Now you have to implement ALL changes that are related to `{{ file_name }}` described in development instructions listed below.
Make sure you don't make any mistakes, especially ones that could affect rest of project. Your changes will be reviewed by very detailed reviewer. Because of that, it is extremely important that you are STRICTLY following ALL the following rules while implementing changes:

{% include "partials/coding_rules.prompt" %}

You are currently working on this task:
```
{{ state.current_task.description }}
```

{% include "partials/user_feedback.prompt" %}

Here are development instructions and now you have to focus only on changes in `{{ file_name }}`:
---start_of_development_instructions---

{{ instructions }}

---end_of_development_instructions---

{% if rework_feedback is defined %}
You previously made changes to file `{{ file_name }}`, according to the instructions described in the previous message.
You previously made changes to file `{{ file_name }}` but not all changes were accepted, and the reviewer provided feedback on the changes that you must rework:
{{ rework_feedback}}
Please update the file accordingly and output the full new version of the file.

The reviewer accepted some of your changes, and the file now looks like this:
```
{{ file_content }}
```
{% elif file_content %}
I need to modify file `{{ file_name }}` that currently looks like this:
Here is how `{{ file_name }}` looks like currently:
```
{{ file_content }}
```
{% else %}
I need to create a new file `{{ file_name }}`:
You need to create a new file `{{ file_name }}`.
{% endif %}

**IMPORTANT**
{% if rework_feedback is defined %}
But not all changes were accepted, and the reviewer provided feedback on the changes that you must rework:
{{ rework_feedback}}
Please update the file accordingly and output the full new version of the file.
{% else %}
I want you to implement changes described in previous message, that starts with `{{ " ".join(instructions.split()[:5]) }}` and ends with `{{ " ".join(instructions.split()[-5:]) }}`.
{% endif %}
Make sure you don't make any mistakes, especially ones that could affect rest of project. Your changes will {% if rework_feedback is defined %}again {% endif %}be reviewed by very detailed reviewer. Because of that, it is extremely important that you are STRICTLY following ALL the following rules while implementing changes:

**IMPORTANT** Output format
You must output the COMPLETE NEW VERSION of this file in following format:
-----------------------format----------------------------
```
the full contents of the updated file, without skipping over any content
```
------------------------end_of_format---------------------------

**IMPORTANT** Comprehensive Codebase Insight
It's crucial to grasp the full scope of the codebase related to your tasks to avert mistakes. Check the initial conversation message for a list of files. Pay a lot of attention to files that are directly included in the file you are currently modifying or that are importing your file.
Consider these examples to guide your approach and thought process:
-----------------------start_of_examples----------------------------
- UI components or templates: Instead of placing scripts directly on specific pages, integrating them in the <head> section or as reusable partials enhances application-wide consistency and reusability.
- Database operations: Be careful not to execute an action, like password hashing, both in a routing function and a model's pre('save') hook, which could lead to redundancy and errors.
- Adding backend logic: Prior to creating new functions, verify if an equivalent function exists in the codebase that you could import and use, preventing unnecessary code duplication and keeping the project efficient.
-----------------------end_of_examples----------------------------

**IMPORTANT** Coding principles
Write high-quality code, first organize it logically with clear, meaningful names for variables, functions, and classes. Aim for simplicity and adhere to the DRY (Don't Repeat Yourself) principle to avoid code duplication. Ensure your codebase is structured and modular for easy navigation and updates.

**IMPORTANT** If the instructions have comments like `// ..add code here...` or `# placeholder for code`, instead of copying the comment, interpret the instructions and output the relevant code.

**IMPORTANT** Your reply MUST NOT omit any code in the new implementation or substitute anything with comments like `// .. rest of the code goes here ..` or `# insert existing code here`, because I will overwrite the existing file with the content you provide. Output ONLY the content for this file, without additional explanation, suggestions or notes. Your output MUST start with ``` and MUST end with ``` and include only the complete file contents.

**IMPORTANT** When working with configuration files (e.g. config.json, .env,...), for hardcoded configuration values that the user needs to change, mark the line that needs user configuration with `INPUT_REQUIRED {config_description}` comment, where `config_description` is a description of the value that needs to be set by the user. Use appropriate syntax for comments in the file you're saving (for example `// INPUT_REQUIRED {config_description}` in JavaScript). NEVER ask the user to write code or provide implementation, even if the instructions suggest it! If the file type doesn't support comments (eg JSON), don't add any.

**IMPORTANT**: Logging
Whenever you write code, make sure to log code execution so that when a developer looks at the CLI output, they can understand what is happening on the server. If the description above mentions the exact code that needs to be added but doesn't contain enough logs, you need to add the logs handlers inside that code yourself.

**IMPORTANT**: Error handling
Whenever you write code, make sure to add error handling for all edge cases you can think of because this app will be used in production so there shouldn't be any crashes. Whenever you log the error, you **MUST** log the entire error message and trace and not only the error message. If the description above mentions the exact code that needs to be added but doesn't contain enough error handlers, you need to add the error handlers inside that code yourself.
{% include "partials/files_list.prompt" %}
39 changes: 39 additions & 0 deletions core/prompts/partials/coding_rules.prompt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# RULES FOR IMPLEMENTING CODE CHANGES
---start_of_coding_rules---

## Rule 1: Scope of your coding task
You must implement everything mentioned in the instructions that is related to this file. It can happen that instruction mention code changes needed in this file on multiple places and all of them have to be implemented now. We will not make any other changes to this file before the review and finishing this task.

## Rule 2: Output format
You must output the COMPLETE NEW VERSION of this file in following format:
---format---
```
the full contents of the updated file, without skipping over any content
```
---end_of_format---

## Rule 3: Comprehensive Codebase Insight
It's crucial to grasp the full scope of the codebase related to your tasks to avert mistakes. Check the initial conversation message for a list of files. Pay a lot of attention to files that are directly included in the file you are currently modifying or that are importing your file.
Consider these examples to guide your approach and thought process:
---start_of_examples---
- UI components or templates: Instead of placing scripts directly on specific pages, integrating them in the <head> section or as reusable partials enhances application-wide consistency and reusability.
- Database operations: Be careful not to execute an action, like password hashing, both in a routing function and a model's pre('save') hook, which could lead to redundancy and errors.
- Adding backend logic: Prior to creating new functions, verify if an equivalent function exists in the codebase that you could import and use, preventing unnecessary code duplication and keeping the project efficient.
---end_of_examples---

## Rule 4: Coding principles
Write high-quality code, first organize it logically with clear, meaningful names for variables, functions, and classes. Aim for simplicity and adhere to the DRY (Don't Repeat Yourself) principle to avoid code duplication. Ensure your codebase is structured and modular for easy navigation and updates.

If the instructions have comments like `// ..add code here...` or `# placeholder for code`, instead of copying the comment, interpret the instructions and output the relevant code.

Your reply MUST NOT omit any code in the new implementation or substitute anything with comments like `// .. rest of the code goes here ..` or `# insert existing code here`, because I will overwrite the existing file with the content you provide. Output ONLY the content for this file, without additional explanation, suggestions or notes. Your output MUST start with ``` and MUST end with ``` and include only the complete file contents.

When working with configuration files (e.g. config.json, .env,...), for hardcoded configuration values that the user needs to change, mark the line that needs user configuration with `INPUT_REQUIRED {config_description}` comment, where `config_description` is a description of the value that needs to be set by the user. Use appropriate syntax for comments in the file you're saving (for example `// INPUT_REQUIRED {config_description}` in JavaScript). NEVER ask the user to write code or provide implementation, even if the instructions suggest it! If the file type doesn't support comments (eg JSON), don't add any.

## Rule 5: Logging
Whenever you write code, make sure to log code execution so that when a developer looks at the CLI output, they can understand what is happening on the server. If the description above mentions the exact code that needs to be added but doesn't contain enough logs, you need to add the logs handlers inside that code yourself.

## Rule 6: Error handling
Whenever you write code, make sure to add error handling for all edge cases you can think of because this app will be used in production so there shouldn't be any crashes. Whenever you log the error, you **MUST** log the entire error message and trace and not only the error message. If the description above mentions the exact code that needs to be added but doesn't contain enough error handlers, you need to add the error handlers inside that code yourself.

---end_of_coding_rules---
Loading

0 comments on commit 87d2942

Please sign in to comment.