Copilot commented on code in PR #13674:
URL: https://github.com/apache/apisix/pull/13674#discussion_r3542787270


##########
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:
   openai_tool_name returns early for already-valid names without checking 
whether that OpenAI name is already taken by a previously-sanitized tool. This 
can create duplicate OpenAI tool names (e.g., an invalid name sanitizes to 
"foo" and a different tool is originally named "foo"), and it can also cause 
response restoration to map the shared OpenAI name back to the wrong original 
tool.



-- 
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