Repository: TransformerOptimus/SuperAGI
File: superagi/controllers/agent_template.py (Python)

Code before:
        raise HTTPException(status_code = 404, detail = "Agent Execution Id undefined")
    if agent_id == 'undefined':
        raise HTTPException(status_code = 404, detail = "Agent Id undefined")

    agent = db.session.query(Agent).filter(Agent.id == agent_id).first()
    if agent is None:
        raise HTTPException(status_code=404, detail="Agent not found")

    main_keys = AgentTemplate.main_keys()

    if agent_execution_id == "-1":
        agent_configurations = db.session.query(AgentConfiguration).filter(AgentConfiguration.agent_id == agent_id).all()
        if not agent_configurations:
            raise HTTPException(status_code=404, detail="Agent configurations not found")
        
        agent_template = AgentTemplate(name=agent.name, description=agent.description,
                                   agent_workflow_id=agent.agent_workflow_id,
                                   organisation_id=organisation.id)
        db.session.add(agent_template)
        db.session.commit()

        for agent_configuration in agent_configurations:
            config_value = agent_configuration.value
            if agent_configuration.key not in main_keys:
                continue
            if agent_configuration.key == "tools":
                config_value = str(Tool.convert_tool_ids_to_names(db, eval(agent_configuration.value)))
            agent_template_config = AgentTemplateConfig(agent_template_id=agent_template.id, key=agent_configuration.key,
                                                        value=config_value)
            db.session.add(agent_template_config)    
    else:
        agent_execution_configurations = db.session.query(AgentExecutionConfiguration).filter(AgentExecutionConfiguration.agent_execution_id == agent_execution_id).all()
        if not agent_execution_configurations:
            raise HTTPException(status_code=404, detail="Agent execution configurations not found")
        
        agent_template = AgentTemplate(name=agent.name, description=agent.description,
                                   agent_workflow_id=agent.agent_workflow_id,
                                   organisation_id=organisation.id)
        db.session.add(agent_template)
        db.session.commit()
        
        for agent_execution_configuration in agent_execution_configurations:
            config_value = agent_execution_configuration.value
            if agent_execution_configuration.key not in main_keys:
                continue
            if agent_execution_configuration.key == "tools":
                config_value = str(Tool.convert_tool_ids_to_names(db, eval(agent_execution_configuration.value)))
            agent_template_config = AgentTemplateConfig(agent_template_id=agent_template.id, key=agent_execution_configuration.key,
                                                        value=config_value)
            db.session.add(agent_template_config)
        

Review comment (suggestion):
```    for agent_configuration in agent_configurations:
            config_value = agent_configuration.value
            if agent_configuration.key not in main_keys:
                continue
            if agent_configuration.key == "tools":
                config_value = str(Tool.convert_tool_ids_to_names(db, eval(agent_configuration.value)))
            agent_template_config = AgentTemplateConfig(agent_template_id=agent_template.id, key=agent_configuration.key,
                                                        value=config_value)
            db.session.add(agent_template_config)    
    else:
        agent_execution_configurations = db.session.query(AgentExecutionConfiguration).filter(AgentExecutionConfiguration.agent_execution_id == agent_execution_id).all()
        if not agent_execution_configurations:
            raise HTTPException(status_code=404, detail="Agent execution configurations not found")
        
        agent_template = AgentTemplate(name=agent.name, description=agent.description,
                                   agent_workflow_id=agent.agent_workflow_id,
                                   organisation_id=organisation.id)
        db.session.add(agent_template)
        db.session.commit()
        
        for agent_execution_configuration in agent_execution_configurations:
            config_value = agent_execution_configuration.value
            if agent_execution_configuration.key not in main_keys:
                continue
            if agent_execution_configuration.key == "tools":
                config_value = str(Tool.convert_tool_ids_to_names(db, eval(agent_execution_configuration.value)))
            agent_template_config = AgentTemplateConfig(agent_template_id=agent_template.id, key=agent_execution_configuration.key,
                                                        value=config_value)
            db.session.add(agent_template_config)```
            
            Dont repeat the code.
            Try to write it in a better way.

Code after:
        raise HTTPException(status_code = 404, detail = "Agent Execution Id undefined")
    if agent_id == 'undefined':
        raise HTTPException(status_code = 404, detail = "Agent Id undefined")

    agent = db.session.query(Agent).filter(Agent.id == agent_id).first()
    if agent is None:
        raise HTTPException(status_code=404, detail="Agent not found")

    configs = None

    if agent_execution_id == "-1":
        configs = db.session.query(AgentConfiguration).filter(AgentConfiguration.agent_id == agent_id).all()
        if not configs:
            raise HTTPException(status_code=404, detail="Agent configurations not found")    
    else:
        configs = db.session.query(AgentExecutionConfiguration).filter(AgentExecutionConfiguration.agent_execution_id == agent_execution_id).all()
        if not configs:
            raise HTTPException(status_code=404, detail="Agent execution configurations not found")

    if configs is None:
        raise HTTPException(status_code=404, detail="Configurations not found")
    
    agent_template = AgentTemplate(name=agent.name, description=agent.description,
                                   agent_workflow_id=agent.agent_workflow_id,
                                   organisation_id=organisation.id)
    db.session.add(agent_template)
    db.session.commit()

    for config in configs:
            config_value = config.value
            if config.key not in AgentTemplate.main_keys():
                continue
            if config.key == "tools":
                config_value = str(Tool.convert_tool_ids_to_names(db, eval(config.value)))
            agent_template_config = AgentTemplateConfig(agent_template_id=agent_template.id, key=config.key,
                                                        value=config_value)
            db.session.add(agent_template_config)       
        
    db.session.commit()
    db.session.flush()
    return agent_template.to_dict()

@router.get("/list")
def list_agent_templates(template_source="local", search_str="", page=0, organisation=Depends(get_user_organisation)):
    """
        List agent templates.

        Args:
            template_source (str, optional): The source of the templates ("local" or "marketplace"). Defaults to "local".
            search_str (str, optional): The search string to filter templates. Defaults to "".
            page (int, optional): The page number for paginated results. Defaults to 0.