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 6 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
61 changes: 40 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,62 @@ 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")

#Fetch agent id from agent execution id and check whether the agent_id received is correct or not.
if agent_execution_id!="-1":
agent_execution_config = AgentExecution.get_agent_execution_from_id(db.session, agent_execution_id)
if agent_execution_config is None:
raise HTTPException(status_code = 404, detail = "Agent Execution not found")
agent_id_from_execution_id = agent_execution_config.agent_id
if agent_id != agent_id_from_execution_id:
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we need this check?

Copy link
Collaborator

Choose a reason for hiding this comment

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

if we have execution id we can take the details from there independent of the agent id sent and in case execution id is not there we can look for agent id

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

its required to check whether the execution_id belongs to the correct agent or not
will remove this

raise HTTPException(status_code = 404, detail = "Wrong agent id")

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


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 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