sub2api icon indicating copy to clipboard operation
sub2api copied to clipboard

Trae 国际版如何接入 Sub2API ?有谁试过吗?

Open zuelu opened this issue 7 months ago • 4 comments

Trae 国际版如何接入 Sub2API ?有谁试过吗? 用了很多姿势都没能成功

zuelu avatar Apr 21 '26 01:04 zuelu

用baseurl后缀用v1/chat/completions就可以了

vast-huhh avatar Apr 21 '26 08:04 vast-huhh

直接改配置文件, 使用自定义Token

zeeklog avatar Apr 21 '26 15:04 zeeklog

国内版不能用吗?

l-spaces avatar Apr 29 '26 09:04 l-spaces

可以接入了 ,需要反代注入`package.cpath = "/www/server/btwaf/inc/?.so;/usr/local/lib/lua/5.1/?.so;/usr/lib/lua/5.1/?.so;" .. package.cpath

local cjson = require "cjson.safe"

local allowed_efforts = { low = true, medium = true, high = true, xhigh = true, }

local function env_or_default(name, default) local value = os.getenv(name) if value == nil or value == "" then return default end return value end

local function to_number_or_default(value, default) local number = tonumber(value or "") if number == nil or number <= 0 then return default end return number end

local function trim(value) return (value:gsub("^%s+", ""):gsub("%s+$", "")) end

local default_effort = string.lower(env_or_default("SUB2API_DEFAULT_REASONING_EFFORT", "high")) if not allowed_efforts[default_effort] then default_effort = "high" end

local max_rewrite_bytes = to_number_or_default(os.getenv("SUB2API_REASONING_REWRITE_MAX_BYTES"), 16 * 1024 * 1024)

local raw_models = env_or_default("SUB2API_DEFAULT_REASONING_MODELS", "gpt5.5,gpt-5.5") local model_allow = {} for item in string.gmatch(raw_models, "([^,]+)") do local model = string.lower(trim(item)) if model ~= "" then model_allow[model] = true end end

local function read_request_body() ngx.req.read_body() local body_data = ngx.req.get_body_data() if body_data then return body_data end

local body_file = ngx.req.get_body_file() if not body_file then return nil end

local file, err = io.open(body_file, "rb") if not file then ngx.log(ngx.WARN, "sub2api reasoning inject: failed to open body file: ", err or "unknown") return nil end body_data = file:read(max_rewrite_bytes + 1) file:close() return body_data end

local function top_level_insert_object_field(json_text, field_json) local pos = 1 while true do local ch = string.sub(json_text, pos, pos) if ch == "" then return nil end if ch == "{" then return string.sub(json_text, 1, pos) .. field_json .. "," .. string.sub(json_text, pos + 1) end if ch ~= " " and ch ~= "\t" and ch ~= "\r" and ch ~= "\n" then return nil end pos = pos + 1 end end

local function has_reasoning_effort(body) if body.reasoning_effort ~= nil and body.reasoning_effort ~= "" then return true end if type(body.reasoning) == "table" and body.reasoning.effort ~= nil and body.reasoning.effort ~= "" then return true end return false end

if ngx.req.get_method() ~= "POST" then return end

if ngx.var.uri ~= "/v1/chat/completions" then return end

local content_type = ngx.var.http_content_type or "" if not string.find(string.lower(content_type), "application/json", 1, true) then return end

local body_data = read_request_body() if not body_data or #body_data == 0 or #body_data > max_rewrite_bytes then return end

local body = cjson.decode(body_data) if type(body) ~= "table" then return end

local model = body.model if type(model) ~= "string" or not model_allow[string.lower(model)] then return end

if has_reasoning_effort(body) then return end

local field_json = [["reasoning_effort":"]] .. default_effort .. [["]] local rewritten_body = top_level_insert_object_field(body_data, field_json) if not rewritten_body then return end

ngx.req.set_body_data(rewritten_body) ngx.req.set_header("Content-Length", #rewritten_body) ngx.req.set_header("Content-Type", "application/json") ngx.req.set_header("X-Sub2API-Reasoning-Injected", default_effort) `

xingzhe11011 avatar May 20 '26 02:05 xingzhe11011