|
| 1 | +"""Guardrail slash command handlers.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from facio.bus.events import OutboundMessage |
| 6 | +from facio.command.router import CommandContext |
| 7 | + |
| 8 | + |
| 9 | +async def cmd_guard(ctx: CommandContext) -> OutboundMessage: |
| 10 | + """Manage guardrails: enable/disable and banned topics.""" |
| 11 | + loop = ctx.loop |
| 12 | + guardrail = getattr(loop, "_invariant_guardrail", None) |
| 13 | + scanner = getattr(loop, "_llm_guard", None) |
| 14 | + |
| 15 | + args = ctx.args.strip() |
| 16 | + |
| 17 | + # /guard on — enable guardrails at runtime |
| 18 | + if args == "on": |
| 19 | + if guardrail is None or scanner is None: |
| 20 | + # First-time enable — instantiate from config |
| 21 | + from facio.config.loader import get_config_path, load_config, save_config |
| 22 | + from facio.security.guardrails import InvariantGuardrail |
| 23 | + from facio.security.llm_guard import LLMGuardScanner |
| 24 | + |
| 25 | + config = load_config(get_config_path()) |
| 26 | + config.tools.guardrails.enabled = True |
| 27 | + save_config(config, get_config_path()) |
| 28 | + guardrail = InvariantGuardrail(config.tools.guardrails) |
| 29 | + scanner = LLMGuardScanner(config.tools.guardrails, getattr(loop, "workspace", None)) |
| 30 | + loop._invariant_guardrail = guardrail |
| 31 | + loop._llm_guard = scanner |
| 32 | + else: |
| 33 | + guardrail._enabled = True |
| 34 | + scanner._enabled = True |
| 35 | + from facio.config.loader import get_config_path, load_config, save_config |
| 36 | + config = load_config(get_config_path()) |
| 37 | + config.tools.guardrails.enabled = True |
| 38 | + save_config(config, get_config_path()) |
| 39 | + return OutboundMessage( |
| 40 | + channel=ctx.msg.channel, |
| 41 | + chat_id=ctx.msg.chat_id, |
| 42 | + content="✅ Guardrails **enabled**.", |
| 43 | + metadata={"render_as": "text"}, |
| 44 | + ) |
| 45 | + |
| 46 | + # /guard off — disable guardrails at runtime |
| 47 | + if args == "off": |
| 48 | + if guardrail is not None: |
| 49 | + guardrail._enabled = False |
| 50 | + if scanner is not None: |
| 51 | + scanner._enabled = False |
| 52 | + from facio.config.loader import get_config_path, load_config, save_config |
| 53 | + config = load_config(get_config_path()) |
| 54 | + config.tools.guardrails.enabled = False |
| 55 | + save_config(config, get_config_path()) |
| 56 | + return OutboundMessage( |
| 57 | + channel=ctx.msg.channel, |
| 58 | + chat_id=ctx.msg.chat_id, |
| 59 | + content="⛔ Guardrails **disabled**.", |
| 60 | + metadata={"render_as": "text"}, |
| 61 | + ) |
| 62 | + |
| 63 | + # /guard status — show current state |
| 64 | + if args == "status": |
| 65 | + enabled = (guardrail is not None and guardrail.available) or (scanner is not None and scanner.available) |
| 66 | + return OutboundMessage( |
| 67 | + channel=ctx.msg.channel, |
| 68 | + chat_id=ctx.msg.chat_id, |
| 69 | + content=f"Guardrails: **{'enabled' if enabled else 'disabled'}**", |
| 70 | + metadata={"render_as": "text"}, |
| 71 | + ) |
| 72 | + |
| 73 | + # Everything below requires guardrails to be active |
| 74 | + if scanner is None or not scanner.available: |
| 75 | + return OutboundMessage( |
| 76 | + channel=ctx.msg.channel, |
| 77 | + chat_id=ctx.msg.chat_id, |
| 78 | + content="Guardrails are disabled. Use `/guard on` to enable.", |
| 79 | + metadata={"render_as": "text"}, |
| 80 | + ) |
| 81 | + |
| 82 | + if not args or args == "list": |
| 83 | + topics = scanner.ban_topics |
| 84 | + if topics: |
| 85 | + lines = "\n".join(f"- {t}" for t in topics) |
| 86 | + content = f"**Banned topics ({len(topics)}):**\n{lines}" |
| 87 | + else: |
| 88 | + content = "No banned topics configured." |
| 89 | + return OutboundMessage( |
| 90 | + channel=ctx.msg.channel, |
| 91 | + chat_id=ctx.msg.chat_id, |
| 92 | + content=content, |
| 93 | + metadata={"render_as": "text"}, |
| 94 | + ) |
| 95 | + |
| 96 | + parts = args.split(None, 1) |
| 97 | + action = parts[0].lower() |
| 98 | + topic = parts[1].strip() if len(parts) > 1 else "" |
| 99 | + |
| 100 | + if action == "add": |
| 101 | + if not topic: |
| 102 | + return OutboundMessage( |
| 103 | + channel=ctx.msg.channel, |
| 104 | + chat_id=ctx.msg.chat_id, |
| 105 | + content="Usage: `/guard add <topic>`", |
| 106 | + metadata={"render_as": "text"}, |
| 107 | + ) |
| 108 | + scanner.add_topic(topic) |
| 109 | + return OutboundMessage( |
| 110 | + channel=ctx.msg.channel, |
| 111 | + chat_id=ctx.msg.chat_id, |
| 112 | + content=f"Added banned topic: **{topic}**", |
| 113 | + metadata={"render_as": "text"}, |
| 114 | + ) |
| 115 | + |
| 116 | + if action == "remove": |
| 117 | + if not topic: |
| 118 | + return OutboundMessage( |
| 119 | + channel=ctx.msg.channel, |
| 120 | + chat_id=ctx.msg.chat_id, |
| 121 | + content="Usage: `/guard remove <topic>`", |
| 122 | + metadata={"render_as": "text"}, |
| 123 | + ) |
| 124 | + if topic.lower() in [t.lower() for t in scanner.ban_topics]: |
| 125 | + scanner.remove_topic(topic) |
| 126 | + return OutboundMessage( |
| 127 | + channel=ctx.msg.channel, |
| 128 | + chat_id=ctx.msg.chat_id, |
| 129 | + content=f"Removed banned topic: **{topic}**", |
| 130 | + metadata={"render_as": "text"}, |
| 131 | + ) |
| 132 | + return OutboundMessage( |
| 133 | + channel=ctx.msg.channel, |
| 134 | + chat_id=ctx.msg.chat_id, |
| 135 | + content=f"Topic not found: **{topic}**", |
| 136 | + metadata={"render_as": "text"}, |
| 137 | + ) |
| 138 | + |
| 139 | + return OutboundMessage( |
| 140 | + channel=ctx.msg.channel, |
| 141 | + chat_id=ctx.msg.chat_id, |
| 142 | + content="Usage: `/guard [on|off|status|list|add|remove] [topic]`", |
| 143 | + metadata={"render_as": "text"}, |
| 144 | + ) |
0 commit comments