tool_call_batches.py 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """Normalize local bridge batches before persistence and per-tool execution."""
  2. from copy import deepcopy
  3. import json
  4. from agent.message_sanitization import coalesce_tool_call_id, deterministic_call_id, uniquify_tool_call_ids
  5. from agent.transports.types import ToolCall
  6. from tools.connectors import is_connector_name
  7. from tools.connectors.gateway.config import MAX_CALLS_PER_DISPATCH
  8. from tools.tool_search_catalog import TOOL_CALL_NAME
  9. from tools.tool_search_validation import normalize_tool_call_entries
  10. def expand_local_tool_batches(tool_calls: list, *, provider_data: dict | None = None) -> list:
  11. """Give every local entry the ordinary agent execution and persistence path.
  12. Registry-only batch dispatch loses the live agent's inline tools, guardrails,
  13. and per-server concurrency policy. Split the *new* assistant turn instead,
  14. before it is persisted; never rewrite an earlier conversation message.
  15. Connector-only batches keep their existing dispatcher and wire shape.
  16. """
  17. # The embedded app bridge validates the complete envelope before admitting
  18. # any connector call. Splitting a mixed batch here would execute its valid
  19. # prefix before the bridge can reject the local/unauthorized entry.
  20. from agent.runtime_context import current as embedded_context
  21. if embedded_context() is not None:
  22. return tool_calls
  23. expanded, replacements = [], {}
  24. for call in tool_calls:
  25. if call.function.name != TOOL_CALL_NAME:
  26. expanded.append(call)
  27. continue
  28. try:
  29. args = call.function.arguments
  30. entries, error = normalize_tool_call_entries(json.loads(args) if isinstance(args, str) else args)
  31. except (TypeError, ValueError, AttributeError):
  32. entries, error = [], True
  33. if (error or not 1 < len(entries) <= MAX_CALLS_PER_DISPATCH
  34. or all(is_connector_name(entry["name"]) for entry in entries)):
  35. # Leave malformed/oversized requests intact for the dispatcher's error.
  36. expanded.append(call)
  37. continue
  38. parent_id = coalesce_tool_call_id(call)
  39. children = []
  40. for index, entry in enumerate(entries):
  41. arguments = json.dumps({"calls": [entry]}, ensure_ascii=False)
  42. if index == 0:
  43. child = deepcopy(call)
  44. child.function.arguments = arguments
  45. else:
  46. # A new entry must not inherit the parent's Responses item id or
  47. # provider signature. IDs are deterministic for stable replay.
  48. child = ToolCall(
  49. id=deterministic_call_id(TOOL_CALL_NAME, parent_id + arguments, index),
  50. name=TOOL_CALL_NAME, arguments=arguments,
  51. )
  52. expanded.append(child)
  53. children.append(child)
  54. replacements[parent_id] = children
  55. uniquify_tool_call_ids(expanded)
  56. # Native providers replay ordered blocks instead of rebuilding them from
  57. # tool_calls. Replace the matching tool block in place, preserving signed
  58. # thinking/text blocks and their order exactly.
  59. if provider_data and replacements:
  60. for key, replace in (("anthropic_content_blocks", _anthropic_children),
  61. ("bedrock_content_blocks", _bedrock_children)):
  62. if blocks := provider_data.get(key):
  63. provider_data[key] = [child for block in blocks for child in replace(block, replacements)]
  64. return expanded
  65. def _anthropic_children(block, replacements):
  66. children = replacements.get(block.get("id")) if block.get("type") == "tool_use" else None
  67. if children is None:
  68. return [block]
  69. return [{**block, "id": coalesce_tool_call_id(child),
  70. "input": json.loads(child.function.arguments)} for child in children]
  71. def _bedrock_children(block, replacements):
  72. tool = block.get("toolUse")
  73. children = replacements.get(tool.get("toolUseId")) if isinstance(tool, dict) else None
  74. if children is None:
  75. return [block]
  76. return [{**block, "toolUse": {**tool, "toolUseId": coalesce_tool_call_id(child),
  77. "input": json.loads(child.function.arguments)}} for child in children]