feat(LLM): 增加可扩展模型插件能力

新增顶层 LLM 注册配置、插件授权列表和 guard 类型适配器。\n模型由独立 Python 进程加载,通过受限 IPC 为 Lua 提供 host.llm.list、host.llm.call 和可调用接口。\n增加 Qwen3Guard 结果解析、流式审核示例、可选依赖、Docker 构建文件和协议测试。
This commit is contained in:
starrrLight committed 2026-10-11 17:39:03 +08:00
1 parent 8c1d243b51
commit b331927773
11 files changed
+693 -4

No files matched your search

+13 -2
View File
@@ -17,7 +17,7 @@ def json_bytes(value) -> bytes:
class LuaPlugin:
def __init__(self, source: str, limits: dict, callback):
def __init__(self, source: str, limits: dict, callback, llm_models=None):
self.limits = limits
self.callback = callback
self.lua = LuaRuntime(
@@ -62,6 +62,15 @@ class LuaPlugin:
self.callback("http", self.to_python(request)),
)
host["http"] = http
llm = self.lua.table()
llm["list"] = lambda: self.to_lua(llm_models or [])
llm["call"] = lambda model, request: self.to_lua(
self.callback("llm", {"model": model, "request": self.to_python(request)})
)
metatable = self.lua.table()
metatable["__call"] = lambda _, model, request: llm["call"](model, request)
self.lua.eval("setmetatable")(llm, metatable)
host["llm"] = llm
log = self.lua.table()
for level in ("info", "warning", "error"):
# Deliberately discard caller-provided text; logs contain metadata only.
@@ -177,7 +186,9 @@ def worker_main(connection, limits: dict):
current = command.get("plugin")
try:
if command["op"] == "load":
plugins[current] = LuaPlugin(command["source"], limits, callback)
plugins[current] = LuaPlugin(
command["source"], limits, callback, command.get("llm", [])
)
result = None
elif command["op"] == "invoke":
result = plugins[current].invoke(