fix(channels): zcode 适配器真正继承 anthropic 基座(此前 9 个函数只有 2 个)

## 缺陷(潜伏,从未被触发)
`channels-zcode` 适配器的手法是把 `anthropic.lua` 源码读进来执行、只覆盖
`transform_request`(身份块注入),其余协议转换全部复用基座。

但那句读取写在**适配器内部**。实测:
- 适配器运行在**另一个沙箱 VM** —— 那里 `__llmsproxy_plugin_dir` 与
  `LLMSPROXY_PLUGIN_DIR` **都不存在**、**也没有文件系统访问**;
- ⇒ `loadAnthropic()` 必然返回 nil,**且静默降级**:适配器照常装载,
  但只有它自己定义的 **2** 个函数,缺
  `transform_response` / `transform_stream_chunk` / `serialize_*` /
  `deserialize_request` 共 **7** 个。

**后果**:内核 `Transform()` 对缺失函数**响亮报错**(不是静默透传)⇒
非流式在响应阶段 502,流式**逐帧**报错 ⇒ 该渠道**完全不可用**。

**为什么一直没暴露**:zcode 从来没有凭据(0 账号)⇒ 源未注册
⇒ 该路径从未被调用过。拿到账号的那一刻才会炸,且报错指向内核而非配置。

## 修法
基座源码改由**插件主体**读好并**内联**进适配器源码(插件 VM 有 io,
实测可读)。与 v2.0.35 buddy 适配器同一手法。

两处必须注意的坑:
1. **不能拼接后 gsub 替换**:gsub 的替换串里 `%` 有特殊含义(`%1` 是捕获
   引用),而适配器源码含 `%s`/`%d` 等格式化串 ⇒ 会破坏语法。
   改用 `find(ph, 1, true)` + `sub` 的纯字符串替换。
2. **占位符 token 不能在注释里出现字面量**(第一版就栽在这:gsub 先命中了
   解释它的那行注释,替换掉注释、代码里仍留着未定义全局)。
3. 顺带补上**插件主体缺失的 `plugin.adaptersDir()`** —— 原先这个函数只
   存在于 zcode 适配器源码**内部**,插件主体从来没有过。

## 判据
- `internal/core`(**真实路径**,会咬变异):
  - `TestZcodeAdapterInjectsOfficialIdentityBlocks`
  - `TestZcodeAdapterKeepsAnthropicProtocolConversion`(工具调用 / 图像块存活)
  - `TestZcodeAdapterFailsLoudlyWithoutAnthropicBase` —— 语义已随设计更新:
    基座不可得现在应在**安装期**失败(新增 `adapters_dir` 测试缝隙把
    "基座缺失"从运行期逐帧炸变成安装期一条明确错误)
- `internal/lua/adapter_zcode_base_test.go`:验证源码模板 + 合并逻辑,
  **并如实标注边界** —— 它在测试里自己做替换,所以**不能**证明安装处内联了
  非空基座(实测:把 `asrc` 换成空串它不报红;那份由上面的 core 判据覆盖)。

**变异验证**:把安装处的 `asrc` 换成空基座 ⇒ core 三条判据全部报红;
用重构前的适配器源码 ⇒ `TestZcodeAdapterInheritsAnthropicBase` 报红(缺 7 个)。
This commit is contained in:
JianFeeeee
2026-10-17 00:01:00 +08:00
parent 58073a6cbe
commit a189c4f61c
3 changed files with 191 additions and 53 deletions

View File

@ -77,6 +77,28 @@ plugin.K = {
ZCODE_ORIGIN = "https://zcode.z.ai",
}
-- ★★ plugin.adaptersDir():适配器目录 = 插件目录的**兄弟**目录
-- (<data>/plugins 与 <data>/adapters)。由插件目录推导,不猜绝对路径
-- (测试与多实例部署都靠它隔离)。
--
-- ⚠️ 为什么必须放在**插件主体**:适配器运行在**另一个沙箱 VM**,
-- 那里 `__llmsproxy_plugin_dir` 与 `LLMSPROXY_PLUGIN_DIR` **都不存在**、
-- **也没有文件系统访问** ⇒ 在适配器里读任何文件都拿不到东西且**静默降级**
-- (实测:zcode 适配器因此只拿到自己定义的 2 个函数,缺 7 个,
-- 每次调用内核报 "missing transform_response" ⇒ 渠道完全不可用)。
-- 所以凡是要"读兄弟文件"的,一律在插件主体做好再内联进去。
function plugin.adaptersDir()
local pd = __llmsproxy_plugin_dir
if type(pd) ~= "string" or pd == "" then
pd = (os.getenv and os.getenv("LLMSPROXY_PLUGIN_DIR")) or ""
end
if type(pd) ~= "string" or pd == "" then return nil end
local base = pd:gsub("/+$", "")
local parent = base:match("^(.*)/[^/]+$")
if not parent then return nil end
return parent .. "/adapters"
end
-- 申请的内核权限。设置页会把"申请中但未授权"标红。
-- 声明本插件要用的内核动作(运维据此授予;declared 与 granted 成对展示)。
--
@ -10201,38 +10223,23 @@ local EXPECTED_MIN_CHARS = 2800
-- ============================================================================
-- 复用 anthropic 适配器
-- ============================================================================
-- 适配器目录 = 插件目录的**兄弟**目录(<data>/plugins 与 <data>/adapters)。
-- 由插件目录推导,不猜绝对路径(测试与多实例部署都靠它隔离)。
local function adaptersDir()
local pd = __llmsproxy_plugin_dir
if type(pd) ~= "string" or pd == "" then
pd = (os.getenv and os.getenv("LLMSPROXY_PLUGIN_DIR")) or ""
end
if type(pd) ~= "string" or pd == "" then return nil end
local base = pd:gsub("/+$", "")
local parent = base:match("^(.*)/[^/]+$")
if not parent then return nil end
return parent .. "/adapters"
end
-- loadAnthropic():在本地环境里执行内核的 anthropic 适配器源码,拿回它的表。
local function loadAnthropic()
local dir = adaptersDir()
if not dir then return nil, "无法推导适配器目录" end
local f = io.open(dir .. "/anthropic.lua", "r")
if not f then return nil, "读不到 anthropic.lua(" .. dir .. ")" end
local src = f:read("*a")
f:close()
local chunk, lerr = load(src, "anthropic.lua")
if not chunk then return nil, "anthropic.lua 无法编译: " .. tostring(lerr) end
-- ★★ 基座由**插件主体内联**进来(见下方 §基座占位符),
-- **不能**在适配器里自己读文件。实测(2026-10-16):
-- 适配器运行在**另一个沙箱 VM**,那里 `__llmsproxy_plugin_dir` 与
-- `LLMSPROXY_PLUGIN_DIR` **都不存在**,**也没有文件系统访问**
-- ⇒ 原先那段"就地读 anthropic.lua"必然失败且**静默降级**:
-- 适配器照常装载,但只有它自己定义的 2 个函数,
-- 缺 transform_response / transform_stream_chunk 等 **7 个**。
-- ⇒ 后果:每次调用内核对缺失函数**响亮报错**(不是静默透传)
-- ⇒ 流式请求逐帧报错 ⇒ 该渠道**完全不可用**。
-- 该缺陷一直潜伏:zcode 从来没有凭据 ⇒ 源未注册 ⇒ 从未被调用过。
local BASE = (function()
local chunk, lerr = load(__INLINE_ANTHROPIC_SRC__, "anthropic-base.lua")
if not chunk then return nil, lerr end
local ok, base = pcall(chunk)
if not ok or type(base) ~= "table" then
return nil, "anthropic.lua 执行失败: " .. tostring(base)
end
return base, nil
end
local BASE, BASE_ERR = loadAnthropic()
if not ok or type(base) ~= "table" then return nil, tostring(base) end
return base
end)()
if BASE then
for k, v in pairs(BASE) do
if adapter[k] == nil then adapter[k] = v end
@ -10617,8 +10624,18 @@ end
-- system 缺官方身份块结构直接 405 + 3012(实测),与 HTTP 头/版本/频率全无关。
-- 通用 anthropic 适配器不注入身份块 ⇒ 即使拿到 zcode 账号也**必然**被拒。
-- 幂等(update 失败退化为 install);失败只记日志,不阻断插件加载。
local function installZcodeAdapter()
-- dirOverride 仅用于测试/诊断:强制指定适配器目录,让"基座不可得"
-- 成为**安装期可观测**的失败(否则它只会在运行期逐帧炸,且原因指向内核)。
local function installZcodeAdapter(dirOverride)
if not __llmsproxy_adapters then return false, "无适配器桥(旧内核?)" end
-- ★ 读到 anthropic 基座源码并在**插件主体**内联进适配器源码。
-- ⚠ 不能在适配器里自己读文件:适配器沙箱没有 fs、也没有 plugin_dir,
-- 那样会**静默降级**成"只有自己定义的 2 个函数"(见适配器顶部注释)。
local adir = dirOverride or plugin.adaptersDir()
if not adir then return false, "无法推导适配器目录" end
local af = io.open(adir .. "/anthropic.lua", "r")
if not af then return false, "读不到 anthropic.lua(" .. adir .. ")" end
local asrc = af:read("*a"); af:close()
-- ★ 把 3012 判据/文案/诊断模块**前置**到适配器源码里。
-- 这样适配器与插件主体用的是**同一份源码文本**(ZCODE_DIAG_SRC)——
-- 否则"请勿连续重试"这句话会只在一条路径上有(参考为此专门抽成单一份)。
@ -10627,6 +10644,13 @@ local function installZcodeAdapter()
-- 且装载会静默成功)。故把它包进一个函数,让 return 只结束该函数。
local full = "local Z = (function()\n" .. ZCODE_DIAG_SRC ..
"\nend)()\n" .. ZCODE_ADAPTER_SRC
-- ⚠ 用**纯字符串替换**而不是 gsub:gsub 的替换串里 `%` 有特殊含义
-- (`%1` 是捕获引用),而适配器源码含 `%s`/`%d` 等格式化串 ⇒ 会破坏语法。
-- ⚠ 占位符 token 刻意在注释里**不写字面量**,否则 find 会先命中注释。
local ph = "__INLINE_ANTHROPIC_SRC__"
local qi = full:find(ph, 1, true)
if not qi then return false, "适配器源码缺少基座占位符" end
full = full:sub(1, qi - 1) .. string.format("%q", asrc) .. full:sub(qi + #ph)
local f, lerr = load(full)
if not f then return false, "源码语法错误: " .. tostring(lerr) end
local _, uerr = __llmsproxy_adapters.update("channels-zcode", full)
@ -11744,13 +11768,21 @@ end
-- plugin.__adapter_install_probe:只给测试/诊断用 —— 报告本插件自装适配器的
-- 装载结果(含语法错误详情)。"装了但没生效"这类问题没有它只能靠猜。
function plugin.__adapter_install_probe()
function plugin.__adapter_install_probe(args)
args = args or {}
-- 带 adapters_dir 时**重新安装** zcode:用来验证「基座不可得 ⇒ 安装期报错」。
-- 之所以要这个缝隙:基座现在由**插件主体内联**(适配器沙箱无 fs),
-- 安装失败若不在此暴露,就只会在运行期逐帧炸,且错误指向内核而非配置。
if args.adapters_dir then
local ok, err = installZcodeAdapter(args.adapters_dir)
return { json = { zcode_reinstall = { ok = ok and true or false, err = err } } }
end
return { json = {
codearts = plugin._codearts_adapter,
trae = plugin._trae_adapter,
gemini = plugin._gemini_adapter,
zcode = plugin._zcode_adapter,
} }
codearts = plugin._codearts_adapter,
trae = plugin._trae_adapter,
gemini = plugin._gemini_adapter,
zcode = plugin._zcode_adapter,
} }
end
-- plugin.__sync_source_probe:只给测试/诊断用 —— 手动触发一次源同步并
@ -11850,4 +11882,6 @@ do
if not tok2 then logEvent("TRAE 适配器装载失败: " .. tostring(terr2)) end
end
return plugin