Skip to content

Architecture Design

This document describes xopcbot's overall architecture and module relationships.

System Architecture

┌─────────────────────────────────────────────────────────────┐
│                      xopcbot                               │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  ┌─────────────────────────────────────────────────────┐   │
│  │                    CLI Layer                         │   │
│  │  ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐   │   │
│  │  │ setup   │ │ onboard │ │ agent   │ │ gateway │   │   │
│  │  └─────────┘ └─────────┘ └─────────┘ └─────────┘   │   │
│  └─────────────────────────────────────────────────────┘   │
│                            │                                │
│  ┌─────────────────────────────────────────────────────┐   │
│  │                     Core                              │   │
│  │  ┌─────────────────────────────────────────────┐   │   │
│  │  │              AgentService                    │   │   │
│  │  │  ┌─────────┐ ┌─────────┐ ┌─────────┐      │   │   │
│  │  │  │ Prompt  │ │ Memory  │ │ Skills  │      │   │   │
│  │  │  │ Builder │ │ Search  │ │         │      │   │   │
│  │  │  └─────────┘ └─────────┘ └─────────┘      │   │   │
│  │  └─────────────────────────────────────────────┘   │   │
│  └─────────────────────────────────────────────────────┘   │
│                            │                                │
│  ┌─────────────────────────────────────────────────────┐   │
│  │                    Providers                          │   │
│  │            @mariozechner/pi-ai (20+ providers)       │   │
│  └─────────────────────────────────────────────────────┘   │
│                            │                                │
└────────────────────────────┼────────────────────────────────┘

        ┌────────────────────┼────────────────────┐
        │                    │                    │
        ▼                    ▼                    ▼
  ┌──────────┐        ┌──────────┐        ┌──────────┐
  │Telegram │        │  Cron    │        │ Gateway  │
  │ Channel │        │ Scheduler │        │   API    │
  └──────────┘        └──────────┘        └──────────┘

Core Modules

Agent Service (src/agent/service.ts)

AgentService is the core orchestrator responsible for:

  1. Message Processing - Receive user messages, call LLM, handle tool calls
  2. Prompt Building - Build system prompt from SOUL.md/USER.md/AGENTS.md/TOOLS.md
  3. Memory Management - Session message storage and context compression
  4. Tool Execution - Unified execution of built-in tools + extension tools
  5. Extension Integration - Load extension tools and hooks

Prompt Builder (src/agent/prompt/)

Modular prompt building system:

src/agent/prompt/
├── index.ts         # PromptBuilder - main builder
│                    # buildIdentitySection, buildMemorySection, etc.
├── modes.ts         # Prompt modes (full/minimal/none)
├── memory/
│   └── index.ts     # memory_search, memory_get tools
│                    # Semantic search of MEMORY.md and memory/*.md
└── skills.ts        # Skills loading system

Prompt Sections:

SectionDescription
Identity"You are a personal assistant running in xopcbot"
Versionxopcbot version info
Tool Call StyleTool calling style (verbose/brief/minimal)
SafetySafety principles
Memorymemory_search/memory_get usage guide
WorkspaceWorking directory
SkillsSkills system
MessagingMessage sending
HeartbeatsHeartbeat monitoring
RuntimeRuntime info

Built-in Tools (src/agent/tools/)

ToolFileDescription
read_fileread.tsRead file content
write_filewrite.tsCreate/overwrite file
edit_fileedit.tsPrecise file editing
list_dirlist-dir.tsList directory contents
shellshell.tsExecute shell commands
grepgrep.tsText search
findfind.tsFile search
web_searchweb.tsWeb search
web_fetchweb.tsWeb scraping
send_messagecommunication.tsSend messages
memory_searchmemory-tool.tsSearch memory files
memory_getmemory-tool.tsRead memory snippets

Session Memory (src/agent/memory/)

src/agent/memory/
├── store.ts       # MemoryStore - session message storage
└── compaction.ts  # SessionCompactor - context compression
                  # Supports extractive/abstractive/structured modes

Extension System (src/extensions/)

src/extensions/
├── types.ts       # Extension type definitions
├── api.ts         # Extension API
├── loader.ts      # Extension loader
├── hooks.ts       # Hook system
└── index.ts      # Exports

Hook Lifecycle:

before_agent_start → agent_end → message_received → 
before_tool_call → after_tool_call → message_sending → session_end

Data Flow

Conversation Flow

User (Telegram/Gateway)


┌─────────────────────┐
│   Channel Handler   │
└──────────┬──────────┘


┌─────────────────────┐
│   AgentService      │
│  ┌───────────────┐  │
│  │ Load Bootstrap │  │ ← SOUL.md, USER.md, TOOLS.md, AGENTS.md
│  └───────┬───────┘  │
│          ▼          │
│  ┌───────────────┐  │
│  │ Build Prompt  │  │ ← memory_search/memory_get
│  └───────┬───────┘  │
│          ▼          │
│  ┌───────────────┐  │
│  │ LLM (pi-ai)   │  │
│  └───────┬───────┘  │
│          ▼          │
│  ┌───────────────┐  │
│  │ Execute Tools │  │ ← Tools (filesystem, shell, web, memory...)
│  │ + Extensions  │  │
│  └───────┬───────┘  │
└──────────┬──────────┘


┌─────────────────────┐
│   Response          │
└──────────┬──────────┘


User Reply / Channel Response

CLI Command Registration Pattern

xopcbot uses self-registration pattern:

typescript
// src/cli/commands/mycommand.ts
import { register } from '../registry.js';

function createMyCommand(ctx: CLIContext): Command {
  return new Command('mycommand')
    .description('My command')
    .action(async () => { ... });
}

register({
  id: 'mycommand',
  name: 'mycommand',
  description: 'My command',
  factory: createMyCommand,
  metadata: { category: 'utility' },
});

Extension Points

Adding New Tools

  1. Create new file in src/agent/tools/
  2. Implement AgentTool interface
  3. Export and register in src/agent/tools/index.ts
  4. Add to tools array in AgentService

Adding Hooks

typescript
api.registerHook('before_tool_call', async (event, ctx) => {
  // Intercept tool calls
  return { modified: true };
});

Adding Extensions

  1. Create xopcbot.extension.json manifest
  2. Implement register(api) function
  3. Publish or load locally

Tech Stack

LayerTechnology
RuntimeNode.js 22+
LanguageTypeScript 5.x
LLM SDK@mariozechner/pi-ai
CLICommander.js
Telegramnode-telegram-bot-api
ValidationZod + TypeBox
LoggingPino
Cronnode-cron
HTTP ServerHono

Released under the MIT License.