From 1d5c01a70754912d76652c662cb82cd0dc2a37ad Mon Sep 17 00:00:00 2001 From: LeonOstrez Date: Wed, 9 Aug 2023 10:38:39 +0200 Subject: [PATCH] fixes of storing users and apps --- euclid/database/database.py | 41 +++++++++++++++++----- euclid/database/models/app.py | 4 +-- euclid/helpers/agents/ProductOwner.py | 5 +-- euclid/main.py | 2 +- euclid/utils/arguments.py | 50 +++++++++++++++++++++++++++ euclid/utils/utils.py | 37 -------------------- 6 files changed, 88 insertions(+), 51 deletions(-) create mode 100644 euclid/utils/arguments.py diff --git a/euclid/database/database.py b/euclid/database/database.py index 66af7d1..e407833 100644 --- a/euclid/database/database.py +++ b/euclid/database/database.py @@ -27,10 +27,11 @@ def save_user(user_id, email, password): return user except DoesNotExist: try: - return User.create(id=user_id, email=email, password=password) - except IntegrityError as e: existing_user = User.get(User.email == email) return existing_user + except DoesNotExist: + return User.create(id=user_id, email=email, password=password) + def get_user(user_id=None, email=None): @@ -53,12 +54,27 @@ def get_user(user_id=None, email=None): def save_app(args): try: app = App.get(App.id == args['app_id']) + for key, value in args.items(): + if key != 'app_id' and value is not None: + setattr(app, key, value) + app.save() except DoesNotExist: - try: - user = get_user(user_id=args['user_id']) - except ValueError: - user = save_user(args['user_id'], args['email'], args['password']) - app = App.create(id=args['app_id'], user=user, app_type=args['app_type'], name=args['name']) + if args.get('user_id') is not None: + try: + user = get_user(user_id=args['user_id']) + except ValueError: + user = save_user(args['user_id'], args['email'], args['password']) + args['user_id'] = user.id + args['email'] = user.email + else: + user = None + + app = App.create( + id=args['app_id'], + user=user, + app_type=args.get('app_type'), + name=args.get('name') + ) return app @@ -140,7 +156,7 @@ def save_development_step(app_id, prompt_path, prompt_data, llm_req_num, message app = get_app(app_id) hash_id = hash_data({ 'prompt_path': prompt_path, - 'prompt_data': {k: v for k, v in prompt_data.items() if k not in {"directory_tree"}}, + 'prompt_data': {k: v for k, v in (prompt_data.items() if prompt_data is not None else {}) if k not in {"directory_tree"}}, 'llm_req_num': llm_req_num }) try: @@ -234,15 +250,22 @@ def get_user_input_from_hash_id(project, query): def get_development_step_from_hash_id(app_id, prompt_path, prompt_data, llm_req_num): + if prompt_data is None: + prompt_data_dict = {} + else: + prompt_data_dict = {k: v for k, v in prompt_data.items() if k not in {"directory_tree"}} + hash_id = hash_data({ 'prompt_path': prompt_path, - 'prompt_data': {k: v for k, v in prompt_data.items() if k not in {"directory_tree"}}, + 'prompt_data': prompt_data_dict, 'llm_req_num': llm_req_num }) + try: dev_step = DevelopmentSteps.get((DevelopmentSteps.hash_id == hash_id) & (DevelopmentSteps.app == app_id)) except DoesNotExist: return None + return dev_step diff --git a/euclid/database/models/app.py b/euclid/database/models/app.py index ed6ffa0..2940397 100644 --- a/euclid/database/models/app.py +++ b/euclid/database/models/app.py @@ -6,6 +6,6 @@ from database.models.user import User class App(BaseModel): user = ForeignKeyField(User, backref='apps') - app_type = CharField() - name = CharField() + app_type = CharField(null=True) + name = CharField(null=True) status = CharField(default='started') \ No newline at end of file diff --git a/euclid/helpers/agents/ProductOwner.py b/euclid/helpers/agents/ProductOwner.py index 8842217..936b26d 100644 --- a/euclid/helpers/agents/ProductOwner.py +++ b/euclid/helpers/agents/ProductOwner.py @@ -16,6 +16,7 @@ class ProductOwner(Agent): super().__init__('product_owner', project) def get_project_description(self): + save_app(self.project.args) self.project.current_step = 'project_description' convo_project_description = AgentConvo(self) @@ -28,13 +29,13 @@ class ProductOwner(Agent): # PROJECT DESCRIPTION self.project.args['app_type'] = ask_for_app_type() - self.project.args['name'] = clean_filename(ask_user('What is the project name?')) + self.project.args['name'] = clean_filename(ask_user(self.project, 'What is the project name?')) setup_workspace(self.project.root_path, self.project.args['name']) save_app(self.project.args) - main_prompt = ask_for_main_app_definition(self, project) + main_prompt = ask_for_main_app_definition(self.project) high_level_messages = get_additional_info_from_openai( self.project, diff --git a/euclid/main.py b/euclid/main.py index 5b1f65e..b1e7b48 100644 --- a/euclid/main.py +++ b/euclid/main.py @@ -4,7 +4,7 @@ from __future__ import print_function, unicode_literals from dotenv import load_dotenv from helpers.Project import Project -from utils.utils import get_arguments +from utils.arguments import get_arguments from logger.logger import logger def init(): diff --git a/euclid/utils/arguments.py b/euclid/utils/arguments.py new file mode 100644 index 0000000..1987757 --- /dev/null +++ b/euclid/utils/arguments.py @@ -0,0 +1,50 @@ +import sys +import uuid + +from database.database import get_app + + +def get_arguments(): + # The first element in sys.argv is the name of the script itself. + # Any additional elements are the arguments passed from the command line. + args = sys.argv[1:] + + # Create an empty dictionary to store the key-value pairs. + arguments = {} + + # Loop through the arguments and parse them as key-value pairs. + for arg in args: + if '=' in arg: + key, value = arg.split('=', 1) + arguments[key] = value + else: + arguments[arg] = True + + if 'app_id' in arguments: + try: + app = get_app(arguments['app_id']) + arguments['user_id'] = str(app.user.id) + arguments['app_type'] = app.app_type + arguments['name'] = app.name + # Add any other fields from the App model you wish to include + except ValueError as e: + print('fkldsjlfjsdlkfjkldsjflkdsjfljdsklfjdslk') + print(e) + # Handle the error as needed, possibly exiting the script + else: + arguments['app_id'] = str(uuid.uuid4()) + + if 'user_id' not in arguments: + arguments['user_id'] = str(uuid.uuid4()) + + if 'email' not in arguments: + arguments['email'] = str(uuid.uuid4()) + + if 'password' not in arguments: + arguments['password'] = 'password' + + if 'step' not in arguments: + arguments['step'] = None + + print(f"If you wish to continue with this project in future run 'python main.py app_id={arguments['app_id']}'") + return arguments diff --git a/euclid/utils/utils.py b/euclid/utils/utils.py index 5f4afcb..392950a 100644 --- a/euclid/utils/utils.py +++ b/euclid/utils/utils.py @@ -1,10 +1,8 @@ # utils/utils.py -import sys import os import platform import distro -import uuid import json import hashlib import re @@ -16,41 +14,6 @@ from const.common import ROLES, STEPS from logger.logger import logger -def get_arguments(): - # The first element in sys.argv is the name of the script itself. - # Any additional elements are the arguments passed from the command line. - args = sys.argv[1:] - - # Create an empty dictionary to store the key-value pairs. - arguments = {} - - # Loop through the arguments and parse them as key-value pairs. - for arg in args: - if '=' in arg: - key, value = arg.split('=', 1) - arguments[key] = value - else: - arguments[arg] = True - - if 'user_id' not in arguments: - arguments['user_id'] = str(uuid.uuid4()) - - if 'email' not in arguments: - arguments['email'] = 'email' - - if 'password' not in arguments: - arguments['password'] = 'password' - - if 'app_id' not in arguments: - arguments['app_id'] = str(uuid.uuid4()) - - if 'step' not in arguments: - arguments['step'] = None - - print(f"If you wish to continue with this project in future run 'python main.py app_id={arguments['app_id']}'") - return arguments - - def capitalize_first_word_with_underscores(s): # Split the string into words based on underscores. words = s.split('_')