2020 CURRENT_ENV ,
2121 FRONTEND_STATE_KEY ,
2222 LOCAL_EXECUTOR ,
23+ MATERIALS_ACCESS_KEY ,
24+ MATERIALS_PROJECT_ID ,
2325 OPENAPI_HOST ,
2426 Transfer2Agent ,
2527)
@@ -178,14 +180,6 @@ def _get_projectId(ctx: Union[InvocationContext, ToolContext]):
178180 )
179181
180182
181- @check_None_wrapper
182- def _get_machineType (ctx : Union [InvocationContext , ToolContext ]):
183- session_state = get_session_state (ctx )
184- return session_state [FRONTEND_STATE_KEY ]['biz' ].get ('machineType' ) or os .getenv (
185- 'MACHINE_TYPE' , 'c32_m64_cpu'
186- )
187-
188-
189183def _inject_ak (ctx : Union [InvocationContext , ToolContext ], executor , storage ):
190184 access_key = _get_ak (ctx )
191185 if executor is not None :
@@ -219,8 +213,7 @@ def _inject_projectId(ctx: Union[InvocationContext, ToolContext], executor, stor
219213
220214
221215def _inject_username (ctx : Union [InvocationContext , ToolContext ], executor ):
222- access_key = _get_ak (ctx )
223- username = ak_to_username (access_key = access_key )
216+ username = ak_to_username (access_key = MATERIALS_ACCESS_KEY )
224217 if username :
225218 if executor is not None :
226219 if executor ['type' ] == 'dispatcher' : # BohriumExecutor
@@ -238,8 +231,7 @@ def _inject_username(ctx: Union[InvocationContext, ToolContext], executor):
238231
239232
240233def _inject_ticket (ctx : Union [InvocationContext , ToolContext ], executor ):
241- access_key = _get_ak (ctx )
242- ticket = ak_to_ticket (access_key = access_key )
234+ ticket = ak_to_ticket (access_key = MATERIALS_ACCESS_KEY )
243235 if ticket :
244236 if executor is not None :
245237 if executor ['type' ] == 'dispatcher' : # BohriumExecutor
@@ -270,65 +262,6 @@ def _inject_current_env(executor):
270262 return executor
271263
272264
273- def _inject_machine_type (ctx : Union [InvocationContext , ToolContext ], executor ):
274- machine_type = _get_machineType (ctx )
275- session_state = get_session_state (ctx )
276- logger .info (
277- f"biz = { session_state [FRONTEND_STATE_KEY ]['biz' ]} ; "
278- f"machineType = { machine_type } "
279- )
280- if executor is not None :
281- if executor ['type' ] == 'dispatcher' : # BohriumExecutor
282- current_machine_type = executor ['machine' ]['remote_profile' ]['machine_type' ]
283- if not current_machine_type :
284- executor ['machine' ]['remote_profile' ]['machine_type' ] = str (
285- machine_type
286- )
287- logger .info (f"After inject_machine_type, executor = { executor } " )
288-
289- return executor
290-
291-
292- def inject_ak_projectId (func : BeforeToolCallback ) -> BeforeToolCallback :
293- @wraps (func )
294- async def wrapper (
295- tool : BaseTool , args : dict , tool_context : ToolContext
296- ) -> Optional [dict ]:
297- # 两步操作:
298- # 1. 调用被装饰的 before_tool_callback;
299- # 2. 如果调用的 before_tool_callback 有返回值,以这个为准
300- if (before_tool_result := await func (tool , args , tool_context )) is not None :
301- return before_tool_result
302-
303- # 如果 tool 为 Transfer2Agent,不做 ak 和 project_id 设置/校验
304- if tool .name == Transfer2Agent :
305- return None
306-
307- # 如果 tool 不是 CalculationMCPTool,不应该调用这个 callback
308- if not isinstance (tool , CalculationMCPTool ):
309- raise TypeError (
310- 'Not CalculationMCPTool type, current tool does not have <storage>'
311- )
312-
313- # 获取 access_key
314- access_key , tool .executor , tool .storage = _inject_ak (
315- tool_context , tool .executor , tool .storage
316- )
317-
318- # 获取 project_id
319- try :
320- project_id , tool .executor , tool .storage = _inject_projectId (
321- tool_context , tool .executor , tool .storage
322- )
323- except ValueError as e :
324- raise ValueError ('ProjectId is invalid' ) from e
325-
326- tool_context .state ['ak' ] = access_key
327- tool_context .state ['project_id' ] = project_id
328-
329- return wrapper
330-
331-
332265def inject_username_ticket (func : BeforeToolCallback ) -> BeforeToolCallback :
333266 @wraps (func )
334267 async def wrapper (
@@ -383,14 +316,14 @@ async def wrapper(
383316 job_create_url = f"{ OPENAPI_HOST } /openapi/v1/job/create"
384317 user_project_list_url = f"{ OPENAPI_HOST } /openapi/v1/open/user/project/list"
385318 payload = {
386- 'projectId' : int ( tool_context . state [ 'project_id' ]) ,
319+ 'projectId' : MATERIALS_PROJECT_ID ,
387320 'name' : 'check_job_create' ,
388321 }
389- params = {'accessKey' : tool_context . state [ 'ak' ] }
322+ params = {'accessKey' : MATERIALS_ACCESS_KEY }
390323
391324 logger .info (
392- f"[check_job_create] project_id = { tool_context . state [ 'project_id' ] } , "
393- f"ak = { tool_context . state [ 'ak' ] } "
325+ f"[check_job_create] project_id = { MATERIALS_PROJECT_ID } , "
326+ f"ak = { MATERIALS_ACCESS_KEY } "
394327 )
395328
396329 async with aiohttp .ClientSession () as session :
@@ -402,7 +335,7 @@ async def wrapper(
402335 project_name = [
403336 item ['project_name' ]
404337 for item in res ['data' ]['items' ]
405- if item ['project_id' ] == int ( tool_context . state [ 'project_id' ])
338+ if item ['project_id' ] == MATERIALS_PROJECT_ID
406339 ][0 ]
407340
408341 async with aiohttp .ClientSession () as session :
@@ -420,32 +353,6 @@ async def wrapper(
420353 return wrapper
421354
422355
423- def inject_machineType (func : BeforeToolCallback ) -> BeforeToolCallback :
424- @wraps (func )
425- async def wrapper (
426- tool : BaseTool , args : dict , tool_context : ToolContext
427- ) -> Optional [dict ]:
428- # 两步操作:
429- # 1. 调用被装饰的 before_tool_callback;
430- # 2. 如果调用的 before_tool_callback 有返回值,以这个为准
431- if (before_tool_result := await func (tool , args , tool_context )) is not None :
432- return before_tool_result
433-
434- # 如果 tool 为 Transfer2Agent,不做 ak 和 project_id 设置/校验
435- if tool .name == Transfer2Agent :
436- return None
437-
438- # 如果 tool 不是 CalculationMCPTool,不应该调用这个 callback
439- if not isinstance (tool , CalculationMCPTool ):
440- raise TypeError (
441- 'Not CalculationMCPTool type, current tool does not have <storage>'
442- )
443-
444- _inject_machine_type (tool_context , tool .executor )
445-
446- return wrapper
447-
448-
449356# 总应该在最后
450357def catch_before_tool_callback_error (func : BeforeToolCallback ) -> BeforeToolCallback :
451358 @wraps (func )
0 commit comments