sihyeonn commented on code in PR #12857:
URL: https://github.com/apache/apisix/pull/12857#discussion_r2957545892


##########
apisix/plugins/ai-drivers/openai-base.lua:
##########
@@ -239,7 +239,36 @@ function _M.request(self, ctx, conf, request_table, 
extra_opts)
         end
     end
 
-    local path = (parsed_url and parsed_url.path or self.path)
+    local path_mode = extra_opts.path_mode or "fixed"
+    local endpoint_path = parsed_url and parsed_url.path
+    local req_path = ctx.var.uri
+    local path
+
+    if path_mode == "preserve" then
+        path = req_path
+        if ctx.var.is_args and ctx.var.args and #ctx.var.args > 0 then
+            local req_args_tab = core.string.decode_args(ctx.var.args)
+            if type(req_args_tab) == "table" then
+                core.table.merge(query_params, req_args_tab)
+            end
+        end
+    elseif path_mode == "append" then
+        local prefix = endpoint_path or ""
+        if prefix == "" or prefix == "/" then
+            path = req_path
+        else
+            path = prefix .. req_path
+            path = path:gsub("//+", "/")
+        end
+        if ctx.var.is_args and ctx.var.args and #ctx.var.args > 0 then
+            local req_args_tab = core.string.decode_args(ctx.var.args)
+            if type(req_args_tab) == "table" then
+                core.table.merge(query_params, req_args_tab)
+            end

Review Comment:
   Done — extracted the path logic into a build_path() helper.



##########
apisix/plugins/ai-drivers/openai-base.lua:
##########
@@ -239,7 +239,36 @@ function _M.request(self, ctx, conf, request_table, 
extra_opts)
         end
     end
 
-    local path = (parsed_url and parsed_url.path or self.path)
+    local path_mode = extra_opts.path_mode or "fixed"
+    local endpoint_path = parsed_url and parsed_url.path
+    local req_path = ctx.var.uri
+    local path
+
+    if path_mode == "preserve" then
+        path = req_path
+        if ctx.var.is_args and ctx.var.args and #ctx.var.args > 0 then
+            local req_args_tab = core.string.decode_args(ctx.var.args)
+            if type(req_args_tab) == "table" then
+                core.table.merge(query_params, req_args_tab)
+            end
+        end
+    elseif path_mode == "append" then
+        local prefix = endpoint_path or ""
+        if prefix == "" or prefix == "/" then
+            path = req_path
+        else
+            path = prefix .. req_path
+            path = path:gsub("//+", "/")
+        end
+        if ctx.var.is_args and ctx.var.args and #ctx.var.args > 0 then
+            local req_args_tab = core.string.decode_args(ctx.var.args)
+            if type(req_args_tab) == "table" then
+                core.table.merge(query_params, req_args_tab)
+            end

Review Comment:
   Already using ctx.var.is_args in the merge_request_query_params function, 
kept as is.



##########
apisix/plugins/ai-drivers/openai-base.lua:
##########
@@ -239,7 +239,36 @@ function _M.request(self, ctx, conf, request_table, 
extra_opts)
         end
     end
 
-    local path = (parsed_url and parsed_url.path or self.path)
+    local path_mode = extra_opts.path_mode or "fixed"
+    local endpoint_path = parsed_url and parsed_url.path
+    local req_path = ctx.var.uri
+    local path
+
+    if path_mode == "preserve" then
+        path = req_path
+        if ctx.var.is_args and ctx.var.args and #ctx.var.args > 0 then
+            local req_args_tab = core.string.decode_args(ctx.var.args)
+            if type(req_args_tab) == "table" then
+                core.table.merge(query_params, req_args_tab)
+            end
+        end
+    elseif path_mode == "append" then
+        local prefix = endpoint_path or ""
+        if prefix == "" or prefix == "/" then
+            path = req_path
+        else
+            path = prefix .. req_path
+            path = path:gsub("//+", "/")
+        end
+        if ctx.var.is_args and ctx.var.args and #ctx.var.args > 0 then
+            local req_args_tab = core.string.decode_args(ctx.var.args)
+            if type(req_args_tab) == "table" then
+                core.table.merge(query_params, req_args_tab)
+            end
+        end
+    else
+        path = (endpoint_path and endpoint_path ~= "" and endpoint_path) or 
self.path

Review Comment:
   Handled — build_path falls back to the driver default path when 
endpoint_path is nil or empty string.



##########
apisix/plugins/ai-drivers/openai-base.lua:
##########
@@ -45,8 +45,26 @@ local HTTP_INTERNAL_SERVER_ERROR = 
ngx.HTTP_INTERNAL_SERVER_ERROR
 local HTTP_GATEWAY_TIMEOUT = ngx.HTTP_GATEWAY_TIMEOUT
 
 
-function _M.new(opt)
-    return setmetatable(opt, mt)
+local function merge_request_query_params(ctx, query_params)
+    if ctx.var.is_args == "?" and ctx.var.args and #ctx.var.args > 0 then
+        local req_args_tab = core.string.decode_args(ctx.var.args)
+        if type(req_args_tab) == "table" then
+            core.table.merge(query_params, req_args_tab)
+        end
+    end
+end
+
+
+function _M.new(opts)
+    local self = {

Review Comment:
   You're right, that was unintentional. Reverted _M.new back to the original 
setmetatable(opt, mt) so all fields are passed through transparently. Sorry for 
the delay on this.



##########
apisix/plugins/ai-proxy/schema.lua:
##########
@@ -89,6 +89,15 @@ local ai_instance_schema = {
                         type = "string",
                         description = "To be specified to override the 
endpoint of the AI Instance",
                     },
+                    path_mode = {

Review Comment:
   path_mode_schema is already defined once and reused across both 
ai_instance_schema and ai_proxy_schema.



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