diff --git a/devika.py b/devika.py index 9224f54e..961b792a 100644 --- a/devika.py +++ b/devika.py @@ -26,7 +26,11 @@ app = Flask(__name__) -CORS(app, resources={r"/*": {"origins": ["https://localhost:3000"]}}) # Change the origin to your frontend URL +CORS(app, resources={r"/*": {"origins": # Change the origin to your frontend URL + [ + "https://localhost:3000", + "http://localhost:3000", + ]}}) app.register_blueprint(project_bp) socketio.init_app(app) diff --git a/src/agents/coder/coder.py b/src/agents/coder/coder.py index 3e021d61..cdaf2213 100644 --- a/src/agents/coder/coder.py +++ b/src/agents/coder/coder.py @@ -34,7 +34,7 @@ def render( def validate_response(self, response: str) -> Union[List[Dict[str, str]], bool]: response = response.strip() - self.logger.debug(f"Response from the model: {response}") + # self.logger.debug(f"Response from the model: {response}") if "~~~" not in response: return False @@ -49,10 +49,15 @@ def validate_response(self, response: str) -> Union[List[Dict[str, str]], bool]: code_block = False for line in response.split("\n"): - if line.startswith("File: "): + if line.startswith("File:"): if current_file and current_code: result.append({"file": current_file, "code": "\n".join(current_code)}) - current_file = line.split(":")[1].strip() + if "`" in line: + current_file = line.split("`")[1].strip() + elif line.startswith("File:") and line.endswith(":") and "`" not in line: + current_file = line.split(":")[1].strip() + else: + return False current_code = [] code_block = False elif line.startswith("```"): @@ -71,10 +76,11 @@ def save_code_to_project(self, response: List[Dict[str, str]], project_name: str for file in response: file_path = os.path.join(self.project_dir, project_name, file['file']) - file_path_dir = os.path.dirname(file_path) + file_norm_path = os.path.normpath(file_path) + file_path_dir = os.path.dirname(file_norm_path) os.makedirs(file_path_dir, exist_ok=True) - with open(file_path, "w", encoding="utf-8") as f: + with open(file_norm_path, "w+", encoding="utf-8") as f: f.write(file["code"]) return file_path_dir diff --git a/src/agents/feature/feature.py b/src/agents/feature/feature.py index 99bcf30b..fc441832 100644 --- a/src/agents/feature/feature.py +++ b/src/agents/feature/feature.py @@ -69,10 +69,11 @@ def save_code_to_project(self, response: List[Dict[str, str]], project_name: str for file in response: file_path = os.path.join(self.project_dir, project_name, file['file']) - file_path_dir = os.path.dirname(file_path) + file_norm_path = os.path.normpath(file_path) + file_path_dir = os.path.dirname(file_norm_path) os.makedirs(file_path_dir, exist_ok=True) - with open(file_path, "w", encoding="utf-8") as f: + with open(file_norm_path, "w+", encoding="utf-8") as f: f.write(file["code"]) return file_path_dir diff --git a/src/project.py b/src/project.py index dd2c68be..75ffeda4 100644 --- a/src/project.py +++ b/src/project.py @@ -36,13 +36,30 @@ def create_project(self, project: str): project_state = Projects(project=project, message_stack_json=json.dumps([])) session.add(project_state) session.commit() + # Create project directory + project_dir = os.path.join(self.project_path, project) + os.makedirs(project_dir, exist_ok=True) def delete_project(self, project: str): with Session(self.engine) as session: - project_state = session.query(Projects).filter(Projects.project == project).first() + project_state = session.query(Projects).filter_by(project=project).first() if project_state: session.delete(project_state) session.commit() + # Delete project directory + project_dir = os.path.join(self.project_path, project) + if os.path.exists(project_dir): + # Empty the directory + for root, dirs, files in os.walk(project_dir, topdown=False): + for file in files: + file_path = os.path.join(root, file) + os.remove(file_path) + for dir1 in dirs: + dir_path = os.path.join(root, dir1) + os.rmdir(dir_path) + + # Remove the empty directory + os.rmdir(project_dir) def add_message_to_project(self, project: str, message: dict): with Session(self.engine) as session: diff --git a/src/state.py b/src/state.py index 1e5c3491..458f7ab1 100644 --- a/src/state.py +++ b/src/state.py @@ -100,6 +100,9 @@ def update_latest_state(self, project: str, state: dict): emit_agent("agent-state", state_stack) def get_latest_state(self, project: str): + if not project: + # If no project is selected, return None immediately + return None with Session(self.engine) as session: agent_state = session.query(AgentStateModel).filter(AgentStateModel.project == project).first() if agent_state: @@ -174,4 +177,4 @@ def get_latest_token_usage(self, project: str): if agent_state: return json.loads(agent_state.state_stack_json)[-1]["token_usage"] return 0 - \ No newline at end of file +