fix(gateway): bridge quick commands into GatewayConfig runtime
Follow-up on salvaged PR #975. Bridge quick_commands from config.yaml into load_gateway_config(), normalize non-dict quick command config at runtime, and add coverage for GatewayConfig round-trips plus config.yaml bridging. This makes the GatewayConfig quick-command fix complete for the real user-facing config path implicated by issue #973.
This commit is contained in:
parent
ce56b45514
commit
7e52e8eb54
3 changed files with 49 additions and 2 deletions
|
|
@ -308,6 +308,16 @@ def load_gateway_config() -> GatewayConfig:
|
||||||
if sr and isinstance(sr, dict):
|
if sr and isinstance(sr, dict):
|
||||||
config.default_reset_policy = SessionResetPolicy.from_dict(sr)
|
config.default_reset_policy = SessionResetPolicy.from_dict(sr)
|
||||||
|
|
||||||
|
# Bridge quick commands from config.yaml into gateway runtime config.
|
||||||
|
# config.yaml is the user-facing config source, so when present it
|
||||||
|
# should override gateway.json for this setting.
|
||||||
|
qc = yaml_cfg.get("quick_commands")
|
||||||
|
if qc is not None:
|
||||||
|
if isinstance(qc, dict):
|
||||||
|
config.quick_commands = qc
|
||||||
|
else:
|
||||||
|
logger.warning("Ignoring invalid quick_commands in config.yaml (expected mapping, got %s)", type(qc).__name__)
|
||||||
|
|
||||||
# Bridge discord settings from config.yaml to env vars
|
# Bridge discord settings from config.yaml to env vars
|
||||||
# (env vars take precedence — only set if not already defined)
|
# (env vars take precedence — only set if not already defined)
|
||||||
discord_cfg = yaml_cfg.get("discord", {})
|
discord_cfg = yaml_cfg.get("discord", {})
|
||||||
|
|
|
||||||
|
|
@ -1017,6 +1017,8 @@ class GatewayRunner:
|
||||||
quick_commands = self.config.get("quick_commands", {}) or {}
|
quick_commands = self.config.get("quick_commands", {}) or {}
|
||||||
else:
|
else:
|
||||||
quick_commands = getattr(self.config, "quick_commands", {}) or {}
|
quick_commands = getattr(self.config, "quick_commands", {}) or {}
|
||||||
|
if not isinstance(quick_commands, dict):
|
||||||
|
quick_commands = {}
|
||||||
if command in quick_commands:
|
if command in quick_commands:
|
||||||
qcmd = quick_commands[command]
|
qcmd = quick_commands[command]
|
||||||
if qcmd.get("type") == "exec":
|
if qcmd.get("type") == "exec":
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ from gateway.config import (
|
||||||
Platform,
|
Platform,
|
||||||
PlatformConfig,
|
PlatformConfig,
|
||||||
SessionResetPolicy,
|
SessionResetPolicy,
|
||||||
|
load_gateway_config,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -89,15 +90,49 @@ class TestGatewayConfigRoundtrip:
|
||||||
platforms={
|
platforms={
|
||||||
Platform.TELEGRAM: PlatformConfig(
|
Platform.TELEGRAM: PlatformConfig(
|
||||||
enabled=True,
|
enabled=True,
|
||||||
token="tok",
|
token="tok_123",
|
||||||
home_channel=HomeChannel(Platform.TELEGRAM, "123", "Home"),
|
home_channel=HomeChannel(Platform.TELEGRAM, "123", "Home"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
reset_triggers=["/new"],
|
reset_triggers=["/new"],
|
||||||
|
quick_commands={"limits": {"type": "exec", "command": "echo ok"}},
|
||||||
)
|
)
|
||||||
d = config.to_dict()
|
d = config.to_dict()
|
||||||
restored = GatewayConfig.from_dict(d)
|
restored = GatewayConfig.from_dict(d)
|
||||||
|
|
||||||
assert Platform.TELEGRAM in restored.platforms
|
assert Platform.TELEGRAM in restored.platforms
|
||||||
assert restored.platforms[Platform.TELEGRAM].token == "tok"
|
assert restored.platforms[Platform.TELEGRAM].token == "tok_123"
|
||||||
assert restored.reset_triggers == ["/new"]
|
assert restored.reset_triggers == ["/new"]
|
||||||
|
assert restored.quick_commands == {"limits": {"type": "exec", "command": "echo ok"}}
|
||||||
|
|
||||||
|
|
||||||
|
class TestLoadGatewayConfig:
|
||||||
|
def test_bridges_quick_commands_from_config_yaml(self, tmp_path, monkeypatch):
|
||||||
|
hermes_home = tmp_path / ".hermes"
|
||||||
|
hermes_home.mkdir()
|
||||||
|
config_path = hermes_home / "config.yaml"
|
||||||
|
config_path.write_text(
|
||||||
|
"quick_commands:\n"
|
||||||
|
" limits:\n"
|
||||||
|
" type: exec\n"
|
||||||
|
" command: echo ok\n",
|
||||||
|
encoding="utf-8",
|
||||||
|
)
|
||||||
|
|
||||||
|
monkeypatch.setenv("HERMES_HOME", str(hermes_home))
|
||||||
|
|
||||||
|
config = load_gateway_config()
|
||||||
|
|
||||||
|
assert config.quick_commands == {"limits": {"type": "exec", "command": "echo ok"}}
|
||||||
|
|
||||||
|
def test_invalid_quick_commands_in_config_yaml_are_ignored(self, tmp_path, monkeypatch):
|
||||||
|
hermes_home = tmp_path / ".hermes"
|
||||||
|
hermes_home.mkdir()
|
||||||
|
config_path = hermes_home / "config.yaml"
|
||||||
|
config_path.write_text("quick_commands: not-a-mapping\n", encoding="utf-8")
|
||||||
|
|
||||||
|
monkeypatch.setenv("HERMES_HOME", str(hermes_home))
|
||||||
|
|
||||||
|
config = load_gateway_config()
|
||||||
|
|
||||||
|
assert config.quick_commands == {}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue