refactor: share execution prep for tool-call phase
Keep the current tool-call bridge contract stable while extracting shared execution setup and tightening Anthropic forwarding regressions. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -15,6 +15,49 @@ def _json_string(value: Any) -> str:
|
||||
return "{}"
|
||||
|
||||
|
||||
def _openai_tool_name(tool: Any) -> str | None:
|
||||
if not isinstance(tool, dict):
|
||||
return None
|
||||
if tool.get("type") == "function":
|
||||
fn = tool.get("function")
|
||||
if isinstance(fn, dict):
|
||||
name = fn.get("name")
|
||||
if isinstance(name, str) and name.strip():
|
||||
return name.strip()
|
||||
name = tool.get("name")
|
||||
if isinstance(name, str) and name.strip():
|
||||
return name.strip()
|
||||
return None
|
||||
|
||||
|
||||
def _anthropic_tool_name(tool: Any) -> str | None:
|
||||
if not isinstance(tool, dict):
|
||||
return None
|
||||
name = tool.get("name")
|
||||
if isinstance(name, str) and name.strip():
|
||||
return name.strip()
|
||||
fn = tool.get("function")
|
||||
if isinstance(fn, dict):
|
||||
nested_name = fn.get("name")
|
||||
if isinstance(nested_name, str) and nested_name.strip():
|
||||
return nested_name.strip()
|
||||
return None
|
||||
|
||||
|
||||
def _tool_event_allowed(
|
||||
tool_name: str,
|
||||
tool_config: dict[str, Any] | None,
|
||||
*,
|
||||
forced_tool_name: str | None = None,
|
||||
) -> bool:
|
||||
if not (tool_config and isinstance(tool_config.get("tools"), list) and tool_config.get("tools")):
|
||||
return True
|
||||
for tool in tool_config.get("tools") or []:
|
||||
if tool_name == _anthropic_tool_name(tool) or tool_name == _openai_tool_name(tool):
|
||||
return True
|
||||
return bool(forced_tool_name and tool_name == forced_tool_name)
|
||||
|
||||
|
||||
def _openai_forced_tool_name(tool_choice: Any) -> str | None:
|
||||
if not isinstance(tool_choice, dict):
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user