(agent_id:int,agent_execution: AgentExecutionIn,api_key: str = Security(validate_api_key),organisation:Organisation = Depends(get_organisation_from_api_key))
| 119 | |
| 120 | @router.post("/{agent_id}/run",status_code=200) |
| 121 | def create_run(agent_id:int,agent_execution: AgentExecutionIn,api_key: str = Security(validate_api_key),organisation:Organisation = Depends(get_organisation_from_api_key)): |
| 122 | agent=Agent.get_agent_from_id(db.session,agent_id) |
| 123 | if not agent: |
| 124 | raise HTTPException(status_code=404, detail="Agent not found") |
| 125 | project=Project.find_by_id(db.session, agent.project_id) |
| 126 | if project.organisation_id!=organisation.id: |
| 127 | raise HTTPException(status_code=404, detail="Agent not found") |
| 128 | db_schedule=AgentSchedule.find_by_agent_id(db.session, agent_id) |
| 129 | if db_schedule is not None: |
| 130 | raise HTTPException(status_code=409, detail="Agent is already scheduled,cannot run") |
| 131 | start_step = AgentWorkflow.fetch_trigger_step_id(db.session, agent.agent_workflow_id) |
| 132 | db_agent_execution=AgentExecution.get_execution_by_agent_id_and_status(db.session, agent_id, "CREATED") |
| 133 | |
| 134 | if db_agent_execution is None: |
| 135 | db_agent_execution = AgentExecution(status="RUNNING", last_execution_time=datetime.now(), |
| 136 | agent_id=agent_id, name=agent_execution.name, num_of_calls=0, |
| 137 | num_of_tokens=0, |
| 138 | current_agent_step_id=start_step.id) |
| 139 | db.session.add(db_agent_execution) |
| 140 | else: |
| 141 | db_agent_execution.status = "RUNNING" |
| 142 | |
| 143 | db.session.commit() |
| 144 | db.session.flush() |
| 145 | |
| 146 | agent_execution_configs = {} |
| 147 | if agent_execution.goal is not None: |
| 148 | agent_execution_configs = { |
| 149 | "goal": agent_execution.goal, |
| 150 | } |
| 151 | |
| 152 | if agent_execution.instruction is not None: |
| 153 | agent_execution_configs["instructions"] = agent_execution.instruction, |
| 154 | |
| 155 | if agent_execution_configs != {}: |
| 156 | AgentExecutionConfiguration.add_or_update_agent_execution_config(session=db.session, execution=db_agent_execution, |
| 157 | agent_execution_configs=agent_execution_configs) |
| 158 | EventHandler(session=db.session).create_event('run_created', |
| 159 | {'agent_execution_id': db_agent_execution.id, |
| 160 | 'agent_execution_name':db_agent_execution.name |
| 161 | }, |
| 162 | agent_id, |
| 163 | organisation.id if organisation else 0) |
| 164 | |
| 165 | agent_execution_knowledge = AgentConfiguration.get_agent_config_by_key_and_agent_id(session= db.session, key= 'knowledge', agent_id= agent_id) |
| 166 | if agent_execution_knowledge and agent_execution_knowledge.value != 'None': |
| 167 | knowledge_name = Knowledges.get_knowledge_from_id(db.session, int(agent_execution_knowledge.value)).name |
| 168 | if knowledge_name is not None: |
| 169 | EventHandler(session=db.session).create_event('knowledge_picked', |
| 170 | {'knowledge_name': knowledge_name, |
| 171 | 'agent_execution_id': db_agent_execution.id}, |
| 172 | agent_id, |
| 173 | organisation.id if organisation else 0 |
| 174 | ) |
| 175 | |
| 176 | if db_agent_execution.status == "RUNNING": |
| 177 | execute_agent.delay(db_agent_execution.id, datetime.now()) |
| 178 | return { |
nothing calls this directly
no test coverage detected