-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_runner.py
More file actions
48 lines (35 loc) · 1.36 KB
/
plugin_runner.py
File metadata and controls
48 lines (35 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Plugin runner — loads registered plugins and runs the first match.
Usage in chat_handler.py:
plugin_name, plugin_context = await plugin_runner.run(user_text, cfg)
# plugin_context is a str to inject as a system message, or "" if no match.
To add a plugin: instantiate it in _REGISTRY below.
"""
import logging
from plugins.base import BasePlugin
from plugins.web_search import WebSearchPlugin
log = logging.getLogger("ov_server")
# Ordered list — first match wins.
_REGISTRY: list[BasePlugin] = [
WebSearchPlugin(),
]
async def run(text: str, cfg: dict) -> tuple[str, str]:
"""Run all plugins against *text*. Return (plugin_name, context) for the first match.
Returns ("", "") when no plugin matches or the plugin is disabled in cfg.
"""
if not text:
return "", ""
for plugin in _REGISTRY:
plugin_cfg = cfg.get("plugins", {}).get(plugin.name, {})
if not plugin_cfg.get("enabled", True):
continue
query = plugin.matches(text)
if query is None:
continue
log.info(f"[plugin:{plugin.name}] matched — query='{query[:80]}'")
try:
context = await plugin.run(query, cfg)
except Exception as exc:
log.warning(f"[plugin:{plugin.name}] run() raised: {exc}")
context = ""
return plugin.name, context
return "", ""