dispatch.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """Route connector calls through the normal dispatch policy pipeline."""
  2. import json
  3. from dataclasses import asdict
  4. from tools.registry import tool_error
  5. from tools.connectors.gateway.config import MAX_CALLS_PER_DISPATCH
  6. from tools.connectors.gateway.merge import assemble_results, fill_remote_failure, partition_calls
  7. def dispatch_connector_call(name, arguments, tool_call_id, turn_id=None, task_id=None):
  8. from tools.connectors.data_use import record_data_use
  9. from tools.connectors.gateway.bridge import run_remote
  10. partition = partition_calls([{"name": name, "arguments": arguments}])
  11. entries = run_remote(partition.remote, tool_call_id, availability=None, client_factory=None, turn_id=turn_id,
  12. task_id=task_id)
  13. entry = entries[0]
  14. record_data_use(entry.get("data_use") or ())
  15. return json.dumps({key: value for key, value in entry.items() if key in {"response", "error", "account"}},
  16. ensure_ascii=False)
  17. def dispatch_connector_batch(calls, ids, *, user_task, enabled_tools,
  18. middleware_trace, enabled_toolsets, disabled_toolsets):
  19. from dataclasses import replace
  20. from model_tools import handle_function_call, suppress_post_tool_call_hook
  21. from tools.interrupt import is_interrupted
  22. if len(calls) > MAX_CALLS_PER_DISPATCH:
  23. return tool_error(f"too many calls: {len(calls)} > max {MAX_CALLS_PER_DISPATCH}. "
  24. "Retry with fewer calls per batch.")
  25. partition = partition_calls(calls)
  26. if partition.local:
  27. from tools.tool_search_validation import local_batch_error
  28. return tool_error(local_batch_error(calls))
  29. entries = list(partition.errors)
  30. for offset, plan in enumerate(partition.remote):
  31. if is_interrupted():
  32. # Check before every entry so /stop prevents unstarted remote side effects.
  33. entries.extend(fill_remote_failure(
  34. partition.remote[offset:], "Stopped by the user before this call was made.",
  35. code="INTERRUPTED"))
  36. break
  37. # Each entry is its own call: its own policy, middleware, hooks and call id (``<id>#<position>``),
  38. # so observers never fold an entry into the wrapper or into a sibling entry.
  39. entry_ids = replace(ids, tool_call_id=f"{ids.tool_call_id}#{plan.position}") if ids.tool_call_id else ids
  40. with suppress_post_tool_call_hook(False):
  41. payload = handle_function_call(
  42. plan.name, plan.arguments, **asdict(entry_ids), user_task=user_task,
  43. enabled_tools=enabled_tools, tool_request_middleware_trace=list(middleware_trace),
  44. skip_pre_tool_call_hook=False, skip_tool_request_middleware=False,
  45. skip_tool_execution_middleware=False,
  46. enabled_toolsets=enabled_toolsets, disabled_toolsets=disabled_toolsets,
  47. )
  48. try:
  49. value = json.loads(payload) if isinstance(payload, str) else payload
  50. except ValueError:
  51. value = payload
  52. entry = {"index": plan.position, "name": plan.name}
  53. if isinstance(value, dict) and "error" in value:
  54. error = value["error"]
  55. entry["error"] = error if isinstance(error, dict) else {"code": "TOOL_ERROR", "message": str(error)}
  56. else:
  57. entry["response"] = value.get("response", value) if isinstance(value, dict) else value
  58. if isinstance(value, dict) and value.get("account"):
  59. entry["account"] = value["account"]
  60. entries.append(entry)
  61. return json.dumps(assemble_results(len(calls), entries), ensure_ascii=False)