This is an automated email from the ASF dual-hosted git repository.
nic443 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new 38ea6f81d fix(ai-prompt-decorator): prevent message accumulation
across requests (#12582)
38ea6f81d is described below
commit 38ea6f81d53c0cf601edb4463b8d3001c9b0df9e
Author: Zhihuang Lin <[email protected]>
AuthorDate: Thu Sep 4 17:14:00 2025 +0800
fix(ai-prompt-decorator): prevent message accumulation across requests
(#12582)
---
apisix/plugins/ai-prompt-decorator.lua | 9 +++-
t/plugin/ai-prompt-decorator.t | 90 +++++++++++++++++++++++++++++++++-
2 files changed, 97 insertions(+), 2 deletions(-)
diff --git a/apisix/plugins/ai-prompt-decorator.lua
b/apisix/plugins/ai-prompt-decorator.lua
index 10b36e82c..ddddf32fe 100644
--- a/apisix/plugins/ai-prompt-decorator.lua
+++ b/apisix/plugins/ai-prompt-decorator.lua
@@ -81,7 +81,14 @@ end
local function decorate(conf, body_tab)
- local new_messages = conf.prepend or EMPTY
+ local new_messages = {}
+
+ if conf.prepend then
+ for i = 1, #conf.prepend do
+ new_messages[i] = conf.prepend[i]
+ end
+ end
+
for _, message in pairs(body_tab.messages) do
core.table.insert_tail(new_messages, message)
end
diff --git a/t/plugin/ai-prompt-decorator.t b/t/plugin/ai-prompt-decorator.t
index 15f40eeed..ae88c8cd4 100644
--- a/t/plugin/ai-prompt-decorator.t
+++ b/t/plugin/ai-prompt-decorator.t
@@ -260,7 +260,95 @@ passed
-=== TEST 7: sanity: configure neither append nor prepend should fail
+=== TEST 7: verify no message accumulation across multiple requests
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+
+ -- Configure route with prepend
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "uri": "/echo",
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": {
+ "127.0.0.1:1980": 1
+ }
+ },
+ "plugins": {
+ "ai-prompt-decorator": {
+ "prepend":[
+ {
+ "role": "system",
+ "content": "system prompt"
+ }
+ ]
+ }
+ }
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ ngx.say("failed to configure route")
+ return
+ end
+
+ -- First request
+ local code1, body1, actual_resp1 = t('/echo',
+ ngx.HTTP_POST,
+ [[{
+ "messages": [
+ { "role": "user", "content": "first message" }
+ ]
+ }]],
+ [[{
+ "messages": [
+ { "role": "system", "content": "system prompt" },
+ { "role": "user", "content": "first message" }
+ ]
+ }]]
+ )
+
+ if code1 >= 300 then
+ ngx.status = code1
+ ngx.say("first request failed")
+ return
+ end
+
+ -- Second request should have the same structure, not accumulating
history
+ local code2, body2, actual_resp2 = t('/echo',
+ ngx.HTTP_POST,
+ [[{
+ "messages": [
+ { "role": "user", "content": "second message" }
+ ]
+ }]],
+ [[{
+ "messages": [
+ { "role": "system", "content": "system prompt" },
+ { "role": "user", "content": "second message" }
+ ]
+ }]]
+ )
+
+ if code2 >= 300 then
+ ngx.status = code2
+ ngx.say("second request failed")
+ return
+ end
+
+ ngx.say("passed")
+ }
+ }
+--- response_body
+passed
+
+
+
+=== TEST 8: sanity: configure neither append nor prepend should fail
--- config
location /t {
content_by_lua_block {