nic-6443 commented on code in PR #13674:
URL: https://github.com/apache/apisix/pull/13674#discussion_r3542829955


##########
apisix/plugins/ai-protocols/converters/anthropic-messages-to-openai-chat.lua:
##########
@@ -62,6 +63,42 @@ local function sanitize_tool_name(name)
 end
 
 
+-- Map an Anthropic tool name to the name used on the OpenAI side, recording
+-- the mapping so the response converter can restore the original name.
+-- `maps.forward` is original → openai, `maps.reverse` is openai → original.
+-- Both the `tools` array and the `tool_use` blocks in the conversation history
+-- go through here, so a tool always carries the same name in both places.
+local function openai_tool_name(name, maps)
+    local mapped = maps.forward[name]
+    if mapped then
+        return mapped
+    end
+
+    if string_len(name) <= TOOL_NAME_MAX_LEN
+            and not ngx_re_find(name, "[^a-zA-Z0-9_-]", "jo") then
+        return name
+    end

Review Comment:
   Good catch, and it's worse than a hypothetical — `master` has the same bug 
today, because the old collision check only looked at names that had already 
been sanitized, never at names that were valid to begin with:
   
   ```
   tools = [{name = "fo o"}, {name = "fo_o"}]
   -> both declared to the upstream as "fo_o", and the response converter maps 
"fo_o" back to "fo o"
   ```
   
   Fixed in a0c05ed: already-valid tool names are reserved up front, and the 
suffix loop now runs against every name handed to the upstream rather than only 
the sanitized ones. The tool that literally owns `fo_o` keeps it; `fo o` 
becomes `fo_o_2`. Added TEST 62 covering both the uniqueness and the round-trip 
restoration.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to