Skip to content

Commit

Permalink
Changes in save template (#1120)
Browse files Browse the repository at this point in the history
* fix

* changes in save template

* changes in save template

* bug fixes

* changes in save template

* changes in save template

---------

Co-authored-by: Rounak Bhatia <[email protected]>
Co-authored-by: namansleeps <[email protected]>
  • Loading branch information
3 people authored Aug 28, 2023
1 parent 54caf01 commit b754873
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
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
49 changes: 28 additions & 21 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,50 @@ 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")
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()
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)

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

0 comments on commit b754873

Please sign in to comment.