SE - AI chatbot docs

User manual, architecture, and installation reference for SE - AI chatbot

View the Project on GitHub M0WA/SE

Architecture

SE is a self-hosted AI chatbot with search-engine features, written in Go as a hexagonal architecture: a chat model that can cite live web results and this instance’s own crawled index side by side, backed by a hybrid (BM25 + semantic) search engine also exposed as a plain public search UI. internal/domain holds pure business logic (no I/O); internal/ports defines the interfaces to the outside world; internal/application orchestrates use cases (search, crawling, background jobs) purely in terms of those ports; internal/adapters/* implement the ports against real infrastructure (SQL, HTTP fetchers, embedding/chat APIs, headless browsers). Three independent binaries share this core – cmd/search (public), cmd/admin (internal), cmd/crawl (internal-only) – coordinating mainly through the shared SQL database, with one exception: cmd/admin talks to cmd/crawl over an internal HTTP API to run/poll crawl jobs. Five more binaries (cmd/mcp-web, cmd/mcp-datetime, cmd/mcp-sandbox, cmd/mcp-files, cmd/mcp-vision) are first-party MCP tool servers spawned on demand as stdio subprocesses, not systemd services. A ninth binary, cmd/gpu-control, is a root-privileged HTTP control service installed only on the separate GPU host – outside this core’s own SQL-database coordination entirely, reached instead over the private LAN by internal/adapters/httpgpumode (called by both cmd/search and cmd/admin) – see Binaries below.

Architecture diagram

Source: architecture.mmd (Mermaid) – edit that file, then regenerate the picture:

echo '{"args": ["--no-sandbox"]}' > /tmp/puppeteer-config.json
npx --yes @mermaid-js/mermaid-cli@latest \
  -i docs/architecture/architecture.mmd -o docs/architecture/architecture.svg -b white \
  -p /tmp/puppeteer-config.json

(The -p/--no-sandbox step works around Puppeteer’s headless Chromium refusing to start under most CI/container/dev-VM setups without it – mmdc fails with No usable sandbox! otherwise.)

Layers

Domain (internal/domain)

Pure logic — every file imports only the Go standard library, with no SQL, HTTP, or framework dependency.

Component Responsibility
Document & search-result core (document.go) Document, SearchResult, IndexedDocument, DocumentVersion, DomainSummary, DocumentsOverview and related admin-overview shapes.
Document aliasing & content dedup (document_alias.go, content_fingerprint.go, content_dedup_status.go) Canonical-document alias tracking; ContentHash/SimHash64 exact and near-duplicate fingerprinting; persisted dedup-run status.
BM25 scoring (bm25.go) PostingStats, TermStat, TermScore; BM25Score/BM25ScoreDocument/BM25TermScores implement the BM25 relevance formula and its per-term breakdown.
Hybrid ranking, query parsing & snippets (hybrid.go, query.go, snippet.go, overrides.go) HybridResult blending BM25+semantic+PageRank; ParsedQuery/ParseQuery for term/phrase/site: parsing; Snippet highlighting; admin-configured block/boost RankingOverrides.
Vector math & embedding endpoints (vector.go, embedding_endpoint.go, embedding_provider.go, embedding_status.go) CosineSimilarity/VectorNorm/CombineWeighted; EmbeddingHTTPEndpoint config model; persisted recompute status.
Chat vision settings (chat_vision.go) ChatVisionSettings – independent similarity-search (references an existing EmbeddingHTTPEndpoint by ID) and captioning (its own full endpoint config) toggles for an image attached to a chat turn, deliberately decoupled from both EmbeddingHTTPEndpoint/EmbeddingSearchWeights and ChatEndpoint.
Fuzzy matching & vocabulary (fuzzy.go, vocabulary_cache.go, tokenizer.go) Levenshtein-based NearestTerm fallback for zero-hit query terms; VocabularyCache; shared Tokenize used by indexing, query parsing, and overrides.
Crawl jobs (crawljob.go) CrawlJob/CrawlJobSummary/CrawlPageEvent models, plus a full in-memory, mutex-protected CrawlJobStore implementation used as the lightweight/test stand-in for the DB-backed one.
Scheduled crawls (scheduled_crawl.go) ScheduledCrawl model for admin-created recurring or one-off crawl definitions.
PageRank (pagerank.go) Iterative PageRank computation over the crawled link graph; persisted PageRankStatus.
Operational & tuning settings (settings.go, tuning.go, link_scope.go, renderer.go, url_normalize.go) OperationalSettings, TuningSettings, link-scope/renderer enums, CanonicalizeURL.
Chat (chat.go) ChatMessage, ChatEndpoint config, ToolCallResult – web search/fetch is only via tools the model invokes on admin-configured MCPServer connections (mcp_server.go), never a direct search by this layer.
MCP servers (mcp_server.go) MCPServer (admin-configured MCP connection – stdio or Streamable HTTP), MCPTool (a tool discovered from an active server’s tools/list, rediscovered every turn).
Agents (agent.go) Agent – an admin-defined specialization (static system prompt + optional MCPServer scope), addressed by ChatEndpoint.DefaultAgentID or a per-question override.
Corpus stats cache (corpus_stats.go) Concurrency-safe cached snapshot of corpus-wide totals BM25 scoring needs per request.
Overview/admin metrics shapes (overview_metrics.go) Pure data shapes feeding the admin Overview page’s charts.

Ports (internal/ports)

34 interfaces defining pure contracts between the core and adapters; the package imports database/sql only for the sql.DBStats value type, performing no I/O itself.

Port Responsibility Implemented by
Fetcher / AuthFetcher / Renderer Plain HTTP fetch, authenticated fetch with per-request options, headless-browser rendering. httpfetcher, browserfetcher
RobotsChecker robots.txt compliance check before fetching. robots
EmbeddingProvider Text-to-vector conversion. hashembed, httpembed
ImageEmbedder Optional capability an EmbeddingProvider may additionally implement – image-to-vector conversion (a chat-completions-style request against a vision-language model’s embeddings endpoint), checked via type assertion. httpembed (not hashembed)
SQLRepository Broad relational-DB port: document save/versioning/embeddings, postings/corpus-stats/vocabulary lookups, alias resolution, ANN semantic search. sqlrepo
PageRankRepository, ContentDedupRepository, EmbeddingRepository, SemanticMatcher Narrow slices of SQLRepository, scoped to exactly what each background job (or, for SemanticMatcher, the vision-similarity endpoint) needs. sqlrepo
SessionStore Shared-DB login session tokens, each carrying a role (admin vs. regular user) and, for a regular user, which User it belongs to. sqlrepo
UserStore CRUD for every DB-backed account – there is no separate hardcoded admin account; User.IsAdmin is what additionally grants /admin/* access on top of the same self-service search/chat access every account gets. An admin manages create/delete/IsAdmin; each account self-serves its own password and personal chat prompt (User.CustomPrompt, injected into every turn) via search-server’s /account page. sqlrepo
HealthChecker Cheap DB liveness check backing /healthz. sqlrepo
AdminRepository Read-mostly admin diagnostics port (stats, listings, time series, pool stats); also carries SaveDocumentOptionalVocabulary (the Document-upload feature’s own indexing entry point – see DocumentJobStore below). sqlrepo
SearchService Primary driving port for public search. internal/application (hybrid search use case)
CrawlerService Executes an actual crawl with live per-page progress. internal/application (crawl loop)
CrawlJobService Network contract admin-server uses to poll/control crawl-server’s jobs. HTTP client adapter (crawlclient)
CrawlJobStore crawl-server’s own job/page-history persistence. domain.CrawlJobStore (in-memory/test), sqlrepo (production)
DocumentJobStore admin-server’s own persistence for Document-upload jobs (see admin_document_jobs.go) – always local/synchronous, unlike CrawlJobStore there’s no separate crawl-server network hop. Method names are fully qualified (CreateDocumentJob, not Create) since the same *sqlrepo.Repository also implements CrawlJobStore’s bare Create/Get/List/Delete for a different table. sqlrepo
DebugSearchService Raw, unblended BM25/semantic/PageRank score breakdown for admin diagnostics. internal/application
SettingsStore Generic key/value settings persistence shared by every process. sqlrepo
ScheduledCrawlStore CRUD + scheduling operations on ScheduledCrawl, shared by admin CRUD and the crawl-server ticker. sqlrepo
EmbeddingEndpointStore, ChatEndpointStore, ChatVisionStore CRUD/get-set for admin-configured embedding, chat, and chat-vision endpoint config. sqlrepo
GPUModeStore Single-row get/set for GPU mode (Vision) settings – the admin-configured control endpoint (cmd/gpu-control) that lets the public chat page’s Chat/Vision/Search toggle switch the shared self-hosted GPU between its normal chat role and image/video generation (ComfyUI+LTX). Enabled defaults false (the capability doesn’t exist on this deployment until an admin turns it on). sqlrepo
MCPServerStore CRUD for admin-configured MCP server connections. sqlrepo
UserMCPServerStore CRUD for per-user, self-service MCP server connections (/account/mcp-servers) – same MCPServer shape, but each row is owned by and scoped to one userID; http transport only (no stdio, which grants local command execution – admin rows only). sqlrepo
AgentStore CRUD for admin-defined agents – a named specialization (static system prompt + optional MCP server scope), resolved by ChatService.Chat each turn. sqlrepo
FileStore CRUD for files a regular-user session uploaded (/account/files) or cmd/mcp-files’s write_file created for them – scoped by owner userID. Always attached to one PersistedChat; Repository.DeleteChat explicitly cascade-deletes its files (SQLite, unlike Postgres, doesn’t enforce the FK’s ON DELETE CASCADE). ChatService never reads it directly – cmd/mcp-files reaches it only via restapi’s /account/api/files endpoints. sqlrepo
ChatStore CRUD for a session’s own pinned (persistent) chats (/account/api/chats) – title, chosen agent, full turn history, resynced each turn and reloaded on page load. Only a pinned chat may have files attached. sqlrepo
ChatCompleter Calls an OpenAI-compatible chat-completions endpoint. httpchat
GPUModeController Calls cmd/gpu-control’s own small HTTP API (GET/POST /gpu/api/mode, POST /gpu/api/generate, GET /gpu/api/generate/{id}, GET /gpu/api/view) over the private LAN, given domain.GPUModeSettings on every call (same “config per call” convention as ChatCompleter, since it’s DB-backed and can change between calls). Generate/GenerateResult submit and poll a fixed, embedded ComfyUI text-to-video workflow; ViewAsset streams the finished video’s bytes back through, so a browser never talks to cmd/gpu-control/ComfyUI directly. httpgpumode
MCPToolProvider / MCPSession Opens one MCP session per chat turn across every active server (tool discovery + calls). mcpclient

Application (internal/application)

Orchestration/use-case layer; verified to import only internal/domain and internal/ports (no adapter imports) in non-test code.

Component Responsibility Key dependencies
hybridSearchService / hybridSearchAdapter Core hybrid search: query parsing, concurrent BM25/embedding fetch, candidate pooling, constraint/override filtering, score blending, pagination, snippet hydration; adapted to the SearchService port. SQLRepository, EmbeddingProvider
crawlLoop Shared crawl control-flow engine (queueing, link-scope/allow-block filtering, sitemap discovery, robots checks, fetch, thin-content filtering, canonical aliasing), reused by every CrawlerService via an injected save callback. AuthFetcher, RobotsChecker
sqlCrawlerService Implements CrawlerService: drives crawlLoop, embeds and persists each fetched page. AuthFetcher, RobotsChecker, SQLRepository, EmbeddingProvider
embedTitleWeighted Shared helper computing a title+body weighted embedding, used by the crawler and the recompute job. EmbeddingProvider
RunPageRankJob / *WithStatus Recomputes PageRank from the link graph and writes results back. PageRankRepository, SettingsStore
RunContentDedupJob / *WithStatus Groups exact/near-duplicate documents (SimHash + LSH) and merges losers into a canonical doc. ContentDedupRepository, SettingsStore
RunEmbeddingRecomputeJob / *WithStatus Re-embeds every document’s stored text against currently-enabled providers without recrawling. EmbeddingRepository, EmbeddingProvider, SettingsStore
TriggerDueCrawls (scheduler) Finds and triggers due scheduled crawls, records completion/next-run state. ScheduledCrawlStore
RecoverInterruptedCrawls Resumes or fails crawl jobs left queued/running when crawl-server last stopped. CrawlJobStore
RenderAwareFetcher Routes fetches through a headless-browser Renderer when requested. AuthFetcher, Renderer
ChatService Orchestrates one chat turn: loads config, resolves and injects the active agent (endpoint default or per-question override), merges the caller’s own MCP servers into the global catalog (always, never narrowed by the agent’s scope), opens an MCP session (passing cmd/mcp-vision’s configuration as env vars, read live from ChatVisionStore every turn), trims history, delegates completion, and runs any tool calls the model makes. ChatEndpointStore, ChatCompleter, MCPServerStore, MCPToolProvider, AgentStore, UserMCPServerStore, ChatVisionStore
GPUModeService Backs GET/POST /vision/api/mode and restapi.Handler.handleChat’s own availability check (consults a cheap in-memory cache, refreshed on a background poll – never a live network call per chat turn). Enforces the security gate beyond the admin master switch (GPUModeSettings.Enabled) and session auth (both restapi’s own job): a global minimum dwell between accepted switches and a per-account rate limit, on top of whatever GPUModeController itself reports (e.g. a 409 conflict when a switch to a different target is already in flight). GPUModeStore, GPUModeController

Adapters (internal/adapters)

13 adapter packages, plus restapi (14 total) as the shared HTTP handler layer for cmd/search and cmd/admin.

Adapter Responsibility
sqlrepo SQL persistence layer shared by all three binaries (SQLite locally/CI, Postgres in the dev deployment); implements SQLRepository, PageRankRepository, ContentDedupRepository, EmbeddingRepository, SemanticMatcher, SessionStore, AdminRepository, CrawlJobStore, DocumentJobStore, SettingsStore, ScheduledCrawlStore, EmbeddingEndpointStore, ChatEndpointStore, ChatVisionStore, GPUModeStore, UserStore, FileStore, ChatStore, and more.
restapi HTTP handler layer for both the public search UI/API and the admin UI/API — routing, JSON REST endpoints, embedded static assets, auth/session and crawl-internal-token checks.
httpfetcher Default plain-HTTP page fetcher with timeout/UA/cookie/basic-auth support, routed through netguard.
browserfetcher Renders JS-heavy pages via a headless Chromium or Firefox browser over Playwright.
htmlparser Pure HTML title/text/link/canonical-URL extraction.
robots Fetches, caches, and evaluates robots.txt rules.
netguard Shared SSRF guard (custom DialContext), with two policies: a strict one (AllowedIP) blocking every private/reserved range, used by every outbound crawler fetch; and a permissive one (AllowedConfiguredEndpointIP) for admin-configured endpoints (httpembed/httpchat’s BaseURL/TokenizeURL, httpgpumode’s ControlBaseURL, and restapi’s Document-upload S3 import’s endpoint) that only blocks link-local/multicast/unspecified addresses, since a self-hosted embeddings/chat backend (or an S3-compatible endpoint) legitimately lives on a private network or loopback.
hashembed Dependency-free fallback embedding provider via feature hashing.
httpembed Calls an OpenAI-compatible embeddings HTTP endpoint (e.g. IONOS AI Model Hub) with chunking and rate-limit-aware retry; outbound calls routed through netguard’s configured-endpoint policy. Also implements ImageEmbedder – a chat-completions-style image_url content request against the same endpoint, for a vision-language embedding model.
httpchat Calls an OpenAI-compatible chat-completions endpoint; outbound calls routed through netguard’s configured-endpoint policy.
httpgpumode Calls cmd/gpu-control’s HTTP API (ControlBaseURL/ControlAPIKey from domain.GPUModeSettings, sent as X-Internal-Token); outbound calls routed through netguard’s configured-endpoint policy, same as httpchat/httpembed.
crawlclient HTTP client cmd/admin uses to delegate crawl-job operations to cmd/crawl.
settingscrypto AES-256-GCM encryption of the admin-configured embedding/chat/MCP-server API keys at rest.
mcpclient MCP client (github.com/modelcontextprotocol/go-sdk) – connects to every active admin-configured MCPServer for a chat turn (spawning a stdio child process like cmd/mcp-web, or dialing an http server’s Streamable HTTP endpoint), discovers its tools, and routes calls to the right connection.
dockersandbox Runs untrusted Python/Go snippets in a locked-down Docker container via the docker CLI – memory/CPU/process/wall-clock limits, dropped capabilities, read-only rootfs, no network unless configured. RunOptions.Files additionally mounts named input files into the same read-only bind mount as the code itself, independent of network access. RunOptions.SystemPackages (apt/apk) runs in a separate short-lived container first (Docker’s ordinary default capabilities, writable root – but never running the model’s own code, only the fixed install command), commits it to a throwaway image, then runs the actual snippet from that image under the sandbox’s completely normal lockdown – installing packages and running untrusted code never share a container. Used only by cmd/mcp-sandbox; not wired through any port, since that MCP binary is the entire integration surface.

Binaries

At runtime, the three binaries coordinate almost entirely through the shared SQL database: each opens its own DB connection (a *sql.DB can’t cross OS processes), and internal/bootstrap’s SyncSettings polls the settings table roughly every 10 seconds so an admin edit propagates to the others. The one real inter-binary call is cmd/admin → cmd/crawl over HTTP via crawlclient.

Deployment

The dev/test deployment (se.mo-sys.de) runs all three Go binaries as independent, hardened systemd services from one Debian package: searchengine-search.service, searchengine-admin.service, searchengine-crawl.service (each NoNewPrivileges=true, ProtectSystem=strict, ProtectHome=true, Restart=on-failure), sharing one system user (searchengine) and one EnvironmentFile (/etc/searchengine/searchengine.env) holding DB_DRIVER/DB_DSN, per-service listen addresses (loopback-only by default), CRAWL_SERVER_URL/CRAWL_INTERNAL_TOKEN, and SETTINGS_ENCRYPTION_KEY – no admin credentials live here; the first admin account is a users row seeded via packaging/create-admin.sh (see docs/manual/installation.md step 7). postinst creates the user and starts all three services, but deliberately does not install or reload nginx config – the tracked packaging/nginx/searchengine.conf can drift from the live /etc/nginx/... unless manually re-synced.

nginx is the public entrypoint on 80/443, splitting traffic by path: /login, /logout, /admin (a plain string-prefix match, not path-segment-aware) route to admin-server (127.0.0.1:8081); everything else falls through to search-server (127.0.0.1:8080). crawl-server (127.0.0.1:8082) gets no location block and must never be exposed publicly. A separate, non-public block on 127.0.0.1:8090 exposes nginx’s stub_status for scraping.

Observability is host-level, independent of the searchengine package: a Prometheus agent (--enable-feature=agent, no local TSDB) on 127.0.0.1:9090 scrapes node-exporter (9100), nginx-exporter (9113, via stub_status), postgres-exporter (9187, reusing DB_DSN), and optionally SearXNG’s OpenMetrics endpoint, then remote_write-forwards to an external IONOS Monitoring Service pipeline. SearXNG – the self-hosted metasearch instance cmd/mcp-web’s web_search tool queries – runs as a separate Docker Compose deployment on 127.0.0.1:8888, outside the searchengine .deb. Everything binds 127.0.0.1 only, since there is no host firewall.

The admin-configured embedding/chat endpoints (httpembed/httpchat, generic OpenAI-compatible HTTP clients) point at a dedicated inference host, gpu.mo-sys.de (one NVIDIA H200 NVL GPU), not a third-party API. Two vLLM processes share that GPU: Qwen/Qwen3-VL-Embedding-8B in embed mode on :8000 (backing httpembed, --gpu-memory-utilization 0.2 – explicit, since the default tries to reserve a fraction of total VRAM regardless of what the chat process already holds), and RedHatAI/Qwen2.5-72B-Instruct-FP8-dynamic in generate mode on :8001 (backing httpchat, --max-model-len 32768, no YaRN). Each is its own systemd unit, bound to the private network interface only, gated by a bearer API key. The host also runs node-exporter and NVIDIA’s DCGM exporter, feeding the same IONOS pipeline under external_labels.site: gpu-h200. It also runs cmd/gpu-control (see Binaries above) and, when Vision mode is enabled, ComfyUI+LTX-2.5 (comfyui.service) – since generation and chat can’t both fit in VRAM, gpu-control stops/starts vllm-chat.service and comfyui.service against each other, while vllm-embed.service stays running throughout.

Keeping this document current

Update this document and its diagram whenever a domain type, port, use case, adapter, or binary is added, changed, or removed – see CLAUDE.md’s architecture-documentation rule.