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


##########
apisix/plugins/ai-prompt-guard.lua:
##########
@@ -97,6 +97,24 @@ local function get_content_to_check(conf, messages)
 end
 
 
+-- Flatten a single message's content into `content`. OpenAI Chat allows 
content
+-- to be either a plain string or an array of typed parts (e.g.
+-- {type = "text", text = "..."}), so collect the text parts instead of 
inserting
+-- the table as-is, which would later break table.concat.
+local function append_message_text(content, msg)
+    if type(msg.content) == "string" then
+        core.table.insert(content, msg.content)
+    elseif type(msg.content) == "table" then
+        for _, part in ipairs(msg.content) do
+            if type(part) == "table" and part.type == "text"
+                    and type(part.text) == "string" then
+                core.table.insert(content, part.text)
+            end
+        end
+    end
+end

Review Comment:
   `append_message_text` assumes `msg` is a table; if a client sends a 
malformed `messages` array (e.g. a string element), this will raise `attempt to 
index ...` and return 500. Adding a defensive `type(msg) ~= "table"` guard 
keeps the plugin from crashing on bad input and mirrors the more defensive 
checks used elsewhere in protocol adapters.



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