2020from dataclasses import is_dataclass , replace
2121from typing import Any , Dict , List , Optional , Tuple
2222
23- from conductor .ai .agents ._internal .token_utils import agent_api_auth_headers
23+ from conductor .ai .agents ._internal .agent_http import _agent_api_client , agent_post
2424from conductor .ai .agents .frameworks .serializer import WorkerInfo
2525
2626logger = logging .getLogger ("conductor.ai.agents.frameworks.claude_agent_sdk" )
@@ -888,17 +888,24 @@ def _push_event_nonblocking(
888888
889889 def _do_push ():
890890 try :
891- import requests
892-
893- url = f"{ server_url } /agent/events/{ execution_id } "
894- headers = agent_api_auth_headers (server_url , auth_key = auth_key , auth_secret = auth_secret )
895- requests .post (url , json = event , headers = headers , timeout = 5 )
891+ agent_post (server_url , auth_key , auth_secret , f"/agent/events/{ execution_id } " , event )
896892 except Exception as exc :
897893 logger .debug ("Event push failed (execution_id=%s): %s" , execution_id , exc )
898894
899895 _EVENT_PUSH_POOL .submit (_do_push )
900896
901897
898+ def _task_client (server_url : str , auth_key : str , auth_secret : str ):
899+ """Return a ``TaskResourceApi`` backed by the shared cached ``ApiClient``.
900+
901+ Reuses the single per-(server_url, auth_key) ApiClient from ``agent_http`` so
902+ the ``/tasks`` progress update and the ``/agent/*`` posts share one token.
903+ """
904+ from conductor .client .http .api .task_resource_api import TaskResourceApi
905+
906+ return TaskResourceApi (_agent_api_client (server_url , auth_key , auth_secret ))
907+
908+
902909def _update_task_progress_nonblocking (
903910 task_id : str ,
904911 execution_id : str ,
@@ -924,20 +931,15 @@ def _update_task_progress_nonblocking(
924931
925932 def _do_update ():
926933 try :
927- import requests
934+ from conductor . client . http . models . task_result import TaskResult
928935
929- url = f"{ server_url } /tasks"
930- headers : Dict [str , str ] = {"Content-Type" : "application/json" }
931- headers .update (
932- agent_api_auth_headers (server_url , auth_key = auth_key , auth_secret = auth_secret )
936+ result = TaskResult (
937+ task_id = task_id ,
938+ workflow_instance_id = execution_id ,
939+ status = "IN_PROGRESS" ,
940+ output_data = progress_data ,
933941 )
934- body = {
935- "taskId" : task_id ,
936- "workflowInstanceId" : execution_id ,
937- "status" : "IN_PROGRESS" ,
938- "outputData" : progress_data ,
939- }
940- requests .post (url , json = body , headers = headers , timeout = 5 )
942+ _task_client (server_url , auth_key , auth_secret ).update_task (result )
941943 except Exception as exc :
942944 logger .debug (
943945 "Task progress update failed (task_id=%s, execution_id=%s): %s" ,
@@ -968,26 +970,15 @@ def _create_tracking_workflow(
968970 Returns the execution ID of the new workflow, or None on failure.
969971 Uses POST /api/agent/execution (Agentspan custom endpoint).
970972 """
971- try :
972- import requests
973-
974- url = f"{ server_url } /agent/execution"
975- headers : Dict [str , str ] = {"Content-Type" : "application/json" }
976- headers .update (
977- agent_api_auth_headers (server_url , auth_key = auth_key , auth_secret = auth_secret )
978- )
979- body : Dict [str , Any ] = {"workflowName" : workflow_name , "input" : input_data }
980- if parent_workflow_id :
981- body ["parentWorkflowId" ] = parent_workflow_id
982- if parent_workflow_task_id :
983- body ["parentWorkflowTaskId" ] = parent_workflow_task_id
984- resp = requests .post (url , json = body , headers = headers , timeout = 5 )
985- if resp .status_code < 400 :
986- return resp .json ().get ("executionId" )
987- return None
988- except Exception as exc :
989- logger .debug ("Create tracking workflow failed: %s" , exc )
990- return None
973+ body : Dict [str , Any ] = {"workflowName" : workflow_name , "input" : input_data }
974+ if parent_workflow_id :
975+ body ["parentWorkflowId" ] = parent_workflow_id
976+ if parent_workflow_task_id :
977+ body ["parentWorkflowTaskId" ] = parent_workflow_task_id
978+ resp = agent_post (
979+ server_url , auth_key , auth_secret , "/agent/execution" , body , read_response = True
980+ )
981+ return resp .get ("executionId" ) if isinstance (resp , dict ) else None
991982
992983
993984def _inject_tool_task (
@@ -1009,32 +1000,23 @@ def _inject_tool_task(
10091000 For SUB_WORKFLOW tasks, pass sub_workflow_param with keys:
10101001 name, version, executionId (the tracking sub-workflow).
10111002 """
1012- try :
1013- import requests
1014-
1015- url = f"{ server_url } /agent/{ execution_id } /tasks"
1016- headers : Dict [str , str ] = {"Content-Type" : "application/json" }
1017- headers .update (
1018- agent_api_auth_headers (server_url , auth_key = auth_key , auth_secret = auth_secret )
1019- )
1020- body : Dict [str , Any ] = {
1021- "taskDefName" : tool_name ,
1022- "referenceTaskName" : ref_name ,
1023- "type" : task_type ,
1024- "inputData" : input_data ,
1025- }
1026- if sub_workflow_param :
1027- body ["subWorkflowParam" ] = sub_workflow_param
1028- resp = requests .post (url , json = body , headers = headers , timeout = 5 )
1029- return resp .status_code < 400
1030- except Exception as exc :
1031- logger .debug (
1032- "Inject tool task failed (execution_id=%s, ref=%s): %s" ,
1033- execution_id ,
1034- ref_name ,
1035- exc ,
1036- )
1037- return False
1003+ body : Dict [str , Any ] = {
1004+ "taskDefName" : tool_name ,
1005+ "referenceTaskName" : ref_name ,
1006+ "type" : task_type ,
1007+ "inputData" : input_data ,
1008+ }
1009+ if sub_workflow_param :
1010+ body ["subWorkflowParam" ] = sub_workflow_param
1011+ resp = agent_post (
1012+ server_url ,
1013+ auth_key ,
1014+ auth_secret ,
1015+ f"/agent/{ execution_id } /tasks" ,
1016+ body ,
1017+ read_response = True ,
1018+ )
1019+ return resp is not None
10381020
10391021
10401022def _complete_tool_task_nonblocking (
@@ -1053,14 +1035,13 @@ def _complete_tool_task_nonblocking(
10531035
10541036 def _do_complete ():
10551037 try :
1056- import requests
1057-
1058- url = f" { server_url } /agent/tasks/ { execution_id } / { ref_name } / { status } "
1059- headers : Dict [ str , str ] = { "Content-Type" : "application/json" }
1060- headers . update (
1061- agent_api_auth_headers ( server_url , auth_key = auth_key , auth_secret = auth_secret )
1038+ agent_post (
1039+ server_url ,
1040+ auth_key ,
1041+ auth_secret ,
1042+ f"/agent/tasks/ { execution_id } / { ref_name } / { status } " ,
1043+ output_data ,
10621044 )
1063- requests .post (url , json = output_data , headers = headers , timeout = 5 )
10641045 except Exception as exc :
10651046 logger .debug (
10661047 "Complete tool task failed (execution_id=%s, ref=%s): %s" ,
@@ -1086,14 +1067,13 @@ def _complete_workflow_nonblocking(
10861067
10871068 def _do_complete ():
10881069 try :
1089- import requests
1090-
1091- url = f" { server_url } /agent/execution/ { workflow_execution_id } /complete"
1092- headers : Dict [ str , str ] = { "Content-Type" : "application/json" }
1093- headers . update (
1094- agent_api_auth_headers ( server_url , auth_key = auth_key , auth_secret = auth_secret )
1070+ agent_post (
1071+ server_url ,
1072+ auth_key ,
1073+ auth_secret ,
1074+ f"/agent/execution/ { workflow_execution_id } /complete" ,
1075+ output_data or {},
10951076 )
1096- requests .post (url , json = output_data or {}, headers = headers , timeout = 5 )
10971077 except Exception as exc :
10981078 logger .debug (
10991079 "Complete workflow failed (execution_id=%s): %s" ,
0 commit comments