2424 PublicAgentResponse ,
2525 ResponseModel ,
2626)
27- from openagent .tools import BaseTool , ToolConfig , ToolParameters
27+ from openagent .tools import BaseTool , ToolConfig
2828
2929auth_handler = Auth ()
3030
@@ -175,7 +175,7 @@ def list_agents(
175175)
176176def get_agent (
177177 agent_id : int ,
178- wallet_address : str = Depends (auth_handler .auth_wrapper ),
178+ wallet_address : str | None = Depends (auth_handler .optional_auth_wrapper ),
179179 db : Session = Depends (get_db ),
180180) -> ResponseModel [AgentResponse ] | APIExceptionResponse :
181181 try :
@@ -186,14 +186,16 @@ def get_agent(
186186 error = f"Agent with ID { agent_id } not found" ,
187187 )
188188
189- if agent .wallet_address .lower () != wallet_address .lower ():
190- return APIExceptionResponse (
191- status_code = status .HTTP_403_FORBIDDEN ,
192- error = "Not authorized to query this agent" ,
193- )
189+ # return full agent info if authenticated and wallet addresses match
190+ if wallet_address and agent .wallet_address .lower () == wallet_address .lower ():
191+ response_data = AgentResponse .model_validate (agent )
192+ else :
193+ # return public info for unauthenticated users or non-owners
194+ response_data = PublicAgentResponse .model_validate (agent )
195+
194196 return ResponseModel (
195197 code = status .HTTP_200_OK ,
196- data = AgentResponse .model_validate (agent ),
198+ data = AgentResponse .model_validate (response_data ),
197199 message = "Agent retrieved successfully" ,
198200 )
199201 except Exception as error :
@@ -355,6 +357,59 @@ def run_agent(
355357 )
356358
357359
360+ @router .post (
361+ "/{agent_id}/stop" ,
362+ response_model = ResponseModel [AgentResponse ],
363+ summary = "Stop an agent" ,
364+ description = "Stop an agent by setting its status to inactive" ,
365+ responses = {
366+ 200 : {"description" : "Successfully stopped agent" },
367+ 403 : {"description" : "Not authorized to stop this agent" },
368+ 404 : {"description" : "Agent not found" },
369+ 500 : {"description" : "Internal server error" },
370+ },
371+ )
372+ def stop_agent (
373+ agent_id : int ,
374+ wallet_address : str = Depends (auth_handler .auth_wrapper ),
375+ db : Session = Depends (get_db ),
376+ ) -> ResponseModel [AgentResponse ] | APIExceptionResponse :
377+ try :
378+ # get agent
379+ agent = db .query (Agent ).filter (Agent .id == agent_id ).first ()
380+ if not agent :
381+ return APIExceptionResponse (
382+ status_code = status .HTTP_404_NOT_FOUND ,
383+ error = f"Agent with ID { agent_id } not found" ,
384+ )
385+
386+ # check if the user is authorized to stop this agent
387+ if agent .wallet_address .lower () != wallet_address .lower ():
388+ return APIExceptionResponse (
389+ status_code = status .HTTP_403_FORBIDDEN ,
390+ error = "Not authorized to stop this agent" ,
391+ )
392+
393+ # update the agent status to inactive
394+ agent .status = AgentStatus .INACTIVE
395+ db .commit ()
396+ db .refresh (agent )
397+
398+ # TODO: stop any running agent processes
399+
400+ return ResponseModel (
401+ code = status .HTTP_200_OK ,
402+ data = AgentResponse .model_validate (agent ),
403+ message = "Agent stopped successfully" ,
404+ )
405+ except Exception as error :
406+ db .rollback ()
407+ return APIExceptionResponse (
408+ status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
409+ error = error ,
410+ )
411+
412+
358413@router .post (
359414 "/{agent_id}/execute/{tool_name}" ,
360415 response_model = ResponseModel [dict [str , Any ]],
@@ -473,9 +528,11 @@ def build_model(model: Model) -> AI_Model:
473528 raise ValueError (f"Unsupported model: { model } " )
474529
475530
476- def initialize_tool_executor (agent : Agent , tool : Tool , model : Model , tool_config : ToolConfig ) -> BaseTool :
531+ def initialize_tool_executor (
532+ agent : Agent , tool : Tool , model : Model , tool_config : ToolConfig
533+ ) -> BaseTool :
477534 model_instance = build_model (model )
478-
535+
479536 return get_tool_executor (agent , tool , model_instance , tool_config )
480537
481538
0 commit comments