Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes in save template #1120

Merged
merged 7 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gui/pages/Content/Agents/AgentWorkspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export default function AgentWorkspace({env, agentId, agentName, selectedView, a
// }

function saveAgentTemplate() {
saveAgentAsTemplate(selectedRun?.id)
saveAgentAsTemplate(agentId, selectedRun?.id ? selectedRun?.id : -1)
.then((response) => {
toast.success("Agent saved as template successfully", {autoClose: 1800});
})
Expand Down
4 changes: 2 additions & 2 deletions gui/pages/api/DashboardService.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ export const fetchAgentTemplateListLocal = () => {
return api.get('/agent_templates/list?template_source=local');
};

export const saveAgentAsTemplate = (executionId) => {
return api.post(`/agent_templates/save_agent_as_template/agent_execution_id/${executionId}`);
export const saveAgentAsTemplate = (agentId, executionId) => {
return api.post(`/agent_templates/save_agent_as_template/agent_id/${agentId}/agent_execution_id/${executionId}`);
};

export const fetchAgentTemplateConfig = (templateId) => {
Expand Down
69 changes: 45 additions & 24 deletions superagi/controllers/agent_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ def edit_agent_template(agent_template_id: int,
db.session.flush()


@router.post("/save_agent_as_template/agent_execution_id/{agent_execution_id}")
@router.post("/save_agent_as_template/agent_id/{agent_id}/agent_execution_id/{agent_execution_id}")
def save_agent_as_template(agent_execution_id: str,
agent_id: str,
organisation=Depends(get_user_organisation)):
"""
Save an agent as a template.
Expand All @@ -209,44 +210,64 @@ def save_agent_as_template(agent_execution_id: str,
Raises:
HTTPException (status_code=404): If the agent or agent execution configurations are not found.
"""

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

agent_executions = AgentExecution.get_agent_execution_from_id(db.session, agent_execution_id)
if agent_executions is None:
raise HTTPException(status_code = 404, detail = "Agent Execution not found")
agent_id = agent_executions.agent_id
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")

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 configurations not found")
main_keys = AgentTemplate.main_keys()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this variable instead directly use in call method, these variables make the method long and complex

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


agent_template = AgentTemplate(name=agent.name, description=agent.description,
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()
main_keys = AgentTemplate.main_keys()

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)
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":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

            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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also improve the unit tests as there are too many changes, and code coverage might decrease.

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)


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)):
"""
Expand Down