Initial implementation of xtrm-agent multi-agent system

Multi-agent AI automation system with shared message bus, specialized
roles (coder/researcher/reviewer), and deny-by-default security.

- Config system with Pydantic validation and YAML loading
- Async message bus with inter-agent delegation
- LLM providers: Anthropic (Claude) and LiteLLM (DeepSeek/Kimi/MiniMax)
- Tool system: registry, builtins (file/bash/web), approval engine, MCP client
- Agent engine with tool-calling loop and orchestrator for multi-agent management
- CLI channel (REPL) and Discord channel
- Docker + Dockge deployment config
- Typer CLI: chat, serve, status, agents commands

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Kaloyan Danchev
2026-02-18 10:21:42 +02:00
commit 378d599125
34 changed files with 4124 additions and 0 deletions
View File
+22
View File
@@ -0,0 +1,22 @@
"""Base channel interface."""
from __future__ import annotations
from abc import ABC, abstractmethod
from xtrm_agent.bus import MessageBus
class BaseChannel(ABC):
"""Abstract base for all input/output channels."""
def __init__(self, bus: MessageBus) -> None:
self.bus = bus
@abstractmethod
async def start(self) -> None:
"""Start listening for messages."""
@abstractmethod
async def stop(self) -> None:
"""Clean up and stop."""
+110
View File
@@ -0,0 +1,110 @@
"""Interactive CLI channel — REPL with prompt_toolkit + rich."""
from __future__ import annotations
import asyncio
from loguru import logger
from prompt_toolkit import PromptSession
from prompt_toolkit.patch_stdout import patch_stdout
from rich.console import Console
from rich.markdown import Markdown
from xtrm_agent.bus import InboundMessage, MessageBus, OutboundMessage
from xtrm_agent.channels.base import BaseChannel
class CLIChannel(BaseChannel):
"""Interactive REPL channel."""
def __init__(
self,
bus: MessageBus,
default_agent: str = "coder",
) -> None:
super().__init__(bus)
self.default_agent = default_agent
self.console = Console()
self._running = False
self._outbound_queue = bus.subscribe_outbound("cli")
async def start(self) -> None:
"""Run the interactive REPL."""
self._running = True
session: PromptSession[str] = PromptSession()
self.console.print("[bold]xtrm-agent[/bold] — type a message or @agent_name to target an agent")
self.console.print("Type [bold]/quit[/bold] to exit\n")
# Start output listener
output_task = asyncio.create_task(self._output_loop())
try:
while self._running:
try:
with patch_stdout():
user_input = await session.prompt_async("you> ")
except (EOFError, KeyboardInterrupt):
break
text = user_input.strip()
if not text:
continue
if text.lower() in ("/quit", "/exit"):
break
msg = InboundMessage(
channel="cli",
sender_id="user",
chat_id="cli",
content=text,
)
await self.bus.publish_inbound(msg)
# Wait for the response
try:
out_msg = await asyncio.wait_for(self._outbound_queue.get(), timeout=300)
self._render_response(out_msg)
except asyncio.TimeoutError:
self.console.print("[red]Timed out waiting for response[/red]")
finally:
self._running = False
output_task.cancel()
async def _output_loop(self) -> None:
"""Background task to handle unsolicited outbound messages."""
# This handles messages that arrive outside the normal request/response flow
# (e.g., delegation results, notifications)
pass
def _render_response(self, msg: OutboundMessage) -> None:
"""Render agent response with rich markdown."""
self.console.print()
self.console.print(Markdown(msg.content))
self.console.print()
async def stop(self) -> None:
self._running = False
async def run_single_message(
bus: MessageBus,
message: str,
agent: str | None = None,
outbound_queue: asyncio.Queue[OutboundMessage] | None = None,
) -> str:
"""Send a single message and wait for the response."""
if outbound_queue is None:
outbound_queue = bus.subscribe_outbound("cli")
msg = InboundMessage(
channel="cli",
sender_id="user",
chat_id="cli",
content=message,
target_agent=agent,
)
await bus.publish_inbound(msg)
out = await asyncio.wait_for(outbound_queue.get(), timeout=300)
return out.content
+98
View File
@@ -0,0 +1,98 @@
"""Discord channel — bot integration via discord.py."""
from __future__ import annotations
import asyncio
import os
import discord
from loguru import logger
from xtrm_agent.bus import InboundMessage, MessageBus, OutboundMessage
from xtrm_agent.channels.base import BaseChannel
class DiscordChannel(BaseChannel):
"""Discord bot channel."""
def __init__(
self,
bus: MessageBus,
token_env: str = "DISCORD_BOT_TOKEN",
default_agent: str = "coder",
allowed_users: list[str] | None = None,
) -> None:
super().__init__(bus)
self.token_env = token_env
self.default_agent = default_agent
self.allowed_users = set(allowed_users or [])
self._outbound_queue = bus.subscribe_outbound("discord")
intents = discord.Intents.default()
intents.message_content = True
self.client = discord.Client(intents=intents)
self._setup_events()
def _setup_events(self) -> None:
@self.client.event
async def on_ready() -> None:
logger.info(f"Discord bot connected as {self.client.user}")
@self.client.event
async def on_message(message: discord.Message) -> None:
if message.author == self.client.user:
return
if message.author.bot:
return
# Check allowlist
if self.allowed_users and str(message.author.id) not in self.allowed_users:
return
# Only respond to mentions or DMs
is_dm = isinstance(message.channel, discord.DMChannel)
is_mentioned = self.client.user in message.mentions if self.client.user else False
if not is_dm and not is_mentioned:
return
content = message.content
# Strip bot mention from content
if self.client.user:
content = content.replace(f"<@{self.client.user.id}>", "").strip()
msg = InboundMessage(
channel="discord",
sender_id=str(message.author.id),
chat_id=str(message.channel.id),
content=content,
metadata={"guild_id": str(message.guild.id) if message.guild else ""},
)
await self.bus.publish_inbound(msg)
# Wait for response and send it
try:
async with message.channel.typing():
out = await asyncio.wait_for(self._outbound_queue.get(), timeout=300)
await self._send_chunked(message.channel, out.content)
except asyncio.TimeoutError:
await message.channel.send("Sorry, I timed out processing your request.")
async def _send_chunked(
self, channel: discord.abc.Messageable, content: str
) -> None:
"""Send a message, splitting into 2000-char chunks if needed."""
while content:
chunk = content[:2000]
content = content[2000:]
await channel.send(chunk)
async def start(self) -> None:
token = os.environ.get(self.token_env)
if not token:
logger.error(f"Discord token not found in env var '{self.token_env}'")
return
logger.info("Starting Discord bot...")
await self.client.start(token)
async def stop(self) -> None:
await self.client.close()