fix(运行时): 将插件路径匹配移到宿主

新增宿主侧精确路径匹配和插件级 match.paths 配置。\n按请求路径固定插件会话链,移除 Lua 插件内的路径判断与 bypass 状态,并迁移现有插件配置和测试。
This commit is contained in:
starrrLight committed 2026-10-15 15:49:05 +08:00
1 parent f905aa22cc
commit d1f5f69454
12 files changed
+126 -200

No files matched your search

+28
View File
@@ -109,14 +109,42 @@ class PluginLLMAccess(Model):
allow: list[str] = Field(default_factory=list)
class PluginMatch(Model):
paths: list[str] = Field(min_length=1)
@field_validator("paths")
@classmethod
def paths_valid(cls, value: list[str]) -> list[str]:
if len(value) != len(set(value)):
raise ValueError("plugin match.paths contains a duplicate path")
for path in value:
if not path.startswith("/"):
raise ValueError("plugin match.paths entries must be absolute paths")
if any(ord(char) < 0x20 or ord(char) == 0x7F for char in path):
raise ValueError("plugin match.paths entries must not contain control characters")
if any(char in path for char in "*?#"):
raise ValueError("plugin match.paths entries must be exact paths without wildcards")
return value
def matches(self, path: str) -> bool:
return path in self.paths
class PluginConfig(Model):
id: str = Field(pattern=r"^[a-zA-Z0-9_-]{1,64}$")
file: Path
enabled: bool = True
match: PluginMatch | None = None
config: dict = Field(default_factory=dict)
network: NetworkPolicy = Field(default_factory=NetworkPolicy)
llm: PluginLLMAccess = Field(default_factory=PluginLLMAccess)
@model_validator(mode="after")
def reject_legacy_path_match(self):
if "paths" in self.config:
raise ValueError(f"plugin {self.id}: config.paths is unsupported; use match.paths")
return self
class Limits(Model):
max_sessions: int = Field(default=4, gt=0)
+3 -2
View File
@@ -240,10 +240,11 @@ def create_app(settings: Settings | str = "config.yaml") -> FastAPI:
handed_off = False
try:
manager = app.state.manager
session = await manager.open(request_id)
request_path = request.scope["path"]
session = await manager.open(request_id, path=request_path)
original = {
"method": "POST",
"path": "/chat/stream",
"path": request_path,
"headers": safe_headers(dict(request.headers)),
"body": text,
}
+17 -9
View File
@@ -18,8 +18,9 @@ ADAPTER_ID = "__protocol_adapter__"
class Session:
def __init__(self, manager, request_id=None):
def __init__(self, manager, request_id=None, plugins=None):
self.manager = manager
self.plugins = list(manager.plugins if plugins is None else plugins)
self.context = {"request_id": request_id or str(uuid4())}
self.started = time.monotonic()
self.closed = False
@@ -140,9 +141,10 @@ class Session:
)
async def initialize(self, adapter_only=False):
for plugin_id, source in self.manager.sources.items():
if adapter_only and plugin_id != ADAPTER_ID:
continue
plugin_ids = [] if adapter_only else [plugin.id for plugin in self.plugins]
plugin_ids.append(ADAPTER_ID)
for plugin_id in plugin_ids:
source = self.manager.sources[plugin_id]
await self.rpc(
{
"op": "load",
@@ -170,7 +172,7 @@ class Session:
async def apply(self, hook, value, start=0):
values = [value]
for plugin in self.manager.plugins[start:]:
for plugin in self.plugins[start:]:
output = []
for item in values:
result = await self.invoke(plugin.id, hook, item)
@@ -226,7 +228,7 @@ class Session:
async def end(self):
output = []
for index, plugin in enumerate(self.manager.plugins):
for index, plugin in enumerate(self.plugins):
result = await self.invoke(plugin.id, "on_end")
for event in self.decision(result, None, "on_end"):
output.extend(await self.apply("on_event", event, start=index + 1))
@@ -293,11 +295,12 @@ class PluginManager:
await self.llm.start()
self.llm_started = True
async def open(self, request_id=None, adapter_only=False):
async def open(self, request_id=None, path=None, adapter_only=False, all_plugins=False):
if self.stopping or len(self.sessions) >= self.settings.limits.max_sessions:
raise Overloaded()
await self.start()
session = Session(self, request_id)
plugins = self.plugins if all_plugins or path is None else self.plugins_for_path(path)
session = Session(self, request_id, plugins)
self.sessions.add(session)
try:
await session.initialize(adapter_only)
@@ -307,9 +310,14 @@ class PluginManager:
raise
async def validate(self):
session = await self.open()
session = await self.open(all_plugins=True)
await session.aclose()
def plugins_for_path(self, path):
return [
plugin for plugin in self.plugins if plugin.match is None or plugin.match.matches(path)
]
async def error_event(self, request_id, last_event, error_type):
# Failed/timed-out business runtimes never render their own errors.
session = await self.open(request_id, adapter_only=True)