# lorekeeper-ku/lorekeeper architecture

> Architecture diagram of the GitHub repository lorekeeper-ku/lorekeeper, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lorekeeper-ku/lorekeeper
- Repository: https://github.com/lorekeeper-ku/lorekeeper

## Overview

LoreKeeper is a conversational story-writing and canon-management application. A user message enters the Streamlit UI, is handled by a function-calling chat loop, and may read approved lore, create or propose canon changes, write a verified scene, or request artwork. Approved facts persist in SQLite; conversation and pending drafts remain in session context. The principal scene workflow retrieves canon, generates text, checks claims, and repairs contradictions. The README documents these capabilities; arrows below are limited to relationships shown in excerpts, indexed references, or README statements.

Main entry points: [`generation_engine/chat_tools.py`](https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/chat_tools.py) (Validated tools), [`generation_engine/ui_backend.py`](https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/ui_backend.py) (Message backend), [`generation_engine/orchestrator.py`](https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/orchestrator.py) (Scene workflow), [`continuity_checker/canon_verifier.py`](https://github.com/lorekeeper-ku/lorekeeper/blob/main/continuity_checker/canon_verifier.py) (Continuity checker)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_experience["Chat experience"]
  node_streamlit["Streamlit UI<br/>[app.py]"]
  node_ui["Chat components<br/>[components.py]"]
  node_backend["Message backend<br/>[ui_backend.py]"]
  node_chatloop["Tool chat loop"]
  node_tools["Validated tools<br/>[chat_tools.py]"]
  node_session["Conversation context<br/>[conversation.py]"]
end

subgraph group_canon["Canon management"]
  node_canonstore[("Canon SQLite store<br/>[canon_store.py]")]
  node_retriever["Canon retrieval<br/>[canon_retriever.py]"]
  node_knowledge["Knowledge graph<br/>[knowledge_graph.py]"]
  node_memory["Canon memory<br/>[memory.py]"]
  node_canonedit["Canon proposals<br/>[canon_editor.py]"]
end

subgraph group_story["Story and continuity"]
  node_character["Character creation"]
  node_storygen["Scene generation"]
  node_workflow["Scene workflow<br/>[orchestrator.py]"]
  node_teamflow["Text visual workflow<br/>[team_pipeline.py]"]
  node_checker["Continuity checker<br/>[canon_verifier.py]"]
  node_classifier["Contradiction classifier"]
  node_addition["Addition judge<br/>[addition_judge.py]"]
  node_policy["Predicate policy"]
  node_relevance["Fact relevance<br/>[relevance.py]"]
  node_nli["NLI comparator<br/>[nli_comparator.py]"]
end

subgraph group_visual["Visuals"]
  node_visuals["Image generation checks<br/>[visuals.py]"]
  node_visionmodel["Vision result models<br/>[model.py]"]
end

subgraph group_shared["Shared services"]
  node_aiclient["AI provider client<br/>[ai_client.py]"]
  node_schemas["Shared contracts<br/>[schemas.py]"]
end

node_user(("User"))

node_user -->|"sends message"| node_streamlit
node_streamlit -->|"renders chat"| node_ui
node_streamlit -->|"handles request"| node_backend
node_ui -->|"submits request"| node_backend
node_backend -->|"runs conversation"| node_chatloop
node_chatloop -->|"executes calls"| node_tools
node_chatloop -->|"requests tool choice"| node_aiclient
node_tools -->|"reads and writes"| node_canonstore
node_tools -->|"retrieves canon"| node_memory
node_tools -->|"proposes changes"| node_canonedit
node_tools -->|"creates character"| node_character
node_tools -->|"writes scene"| node_storygen
node_tools -->|"requests visuals"| node_teamflow
node_tools -->|"generates image"| node_visuals
node_backend -->|"runs scene pipeline"| node_workflow
node_backend -->|"runs team workflow"| node_teamflow
node_backend -->|"uses conversation state"| node_session
node_memory -->|"retrieves facts"| node_retriever
node_memory -->|"accesses facts"| node_canonstore
node_retriever -->|"loads world facts"| node_canonstore
node_retriever -->|"builds graph"| node_knowledge
node_workflow -->|"verifies scene"| node_checker
node_workflow -->|"generates and repairs"| node_storygen
node_checker -->|"selects facts"| node_relevance
node_checker -->|"classifies claims"| node_classifier
node_checker -->|"compares claims"| node_nli
node_classifier -->|"checks predicates"| node_policy
node_classifier -->|"judges additions"| node_addition
node_visuals -->|"requests image"| node_aiclient
node_visuals -->|"returns check result"| node_visionmodel
node_canonstore -->|"validates facts"| node_schemas
node_visuals -->|"uses asset contracts"| node_schemas
node_workflow -->|"uses report contracts"| node_schemas

click node_streamlit "https://github.com/lorekeeper-ku/lorekeeper/blob/main/app.py"
click node_ui "https://github.com/lorekeeper-ku/lorekeeper/blob/main/lorekeeper_ui/components.py"
click node_backend "https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/ui_backend.py"
click node_chatloop "https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/chat_orchestrator.py"
click node_tools "https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/chat_tools.py"
click node_session "https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/conversation.py"
click node_canonstore "https://github.com/lorekeeper-ku/lorekeeper/blob/main/canon_memory/canon_store.py"
click node_retriever "https://github.com/lorekeeper-ku/lorekeeper/blob/main/canon_memory/canon_retriever.py"
click node_knowledge "https://github.com/lorekeeper-ku/lorekeeper/blob/main/canon_memory/knowledge_graph.py"
click node_memory "https://github.com/lorekeeper-ku/lorekeeper/blob/main/canon_memory/memory.py"
click node_canonedit "https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/canon_editor.py"
click node_character "https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/character_creator.py"
click node_storygen "https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/ai_story_generator.py"
click node_workflow "https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/orchestrator.py"
click node_teamflow "https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/team_pipeline.py"
click node_checker "https://github.com/lorekeeper-ku/lorekeeper/blob/main/continuity_checker/canon_verifier.py"
click node_classifier "https://github.com/lorekeeper-ku/lorekeeper/blob/main/continuity_checker/contradiction_classifier.py"
click node_addition "https://github.com/lorekeeper-ku/lorekeeper/blob/main/continuity_checker/addition_judge.py"
click node_policy "https://github.com/lorekeeper-ku/lorekeeper/blob/main/continuity_checker/predicate_policy.py"
click node_relevance "https://github.com/lorekeeper-ku/lorekeeper/blob/main/continuity_checker/relevance.py"
click node_nli "https://github.com/lorekeeper-ku/lorekeeper/blob/main/continuity_checker/nli_comparator.py"
click node_visuals "https://github.com/lorekeeper-ku/lorekeeper/blob/main/multimodal/visuals.py"
click node_visionmodel "https://github.com/lorekeeper-ku/lorekeeper/blob/main/multimodal/model.py"
click node_aiclient "https://github.com/lorekeeper-ku/lorekeeper/blob/main/shared/ai_client.py"
click node_schemas "https://github.com/lorekeeper-ku/lorekeeper/blob/main/shared/schemas.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_streamlit,node_ui,node_backend,node_chatloop,node_tools,node_session,node_user toneBlue
class node_canonstore,node_retriever,node_knowledge,node_memory,node_canonedit toneAmber
class node_character,node_storygen,node_workflow,node_teamflow,node_checker,node_classifier,node_addition,node_policy,node_relevance,node_nli toneMint
class node_visuals,node_visionmodel toneRose
class node_aiclient,node_schemas toneIndigo
```

## Components

### Chat experience

- [Streamlit UI](https://github.com/lorekeeper-ku/lorekeeper/blob/main/app.py): `app.py`
- [Chat components](https://github.com/lorekeeper-ku/lorekeeper/blob/main/lorekeeper_ui/components.py): `lorekeeper_ui/components.py`
- [Message backend](https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/ui_backend.py): `generation_engine/ui_backend.py`
- [Tool chat loop](https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/chat_orchestrator.py): `generation_engine/chat_orchestrator.py`
- [Validated tools](https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/chat_tools.py): `generation_engine/chat_tools.py`
- [Conversation context](https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/conversation.py): `generation_engine/conversation.py`

### Canon management

- [Canon SQLite store](https://github.com/lorekeeper-ku/lorekeeper/blob/main/canon_memory/canon_store.py): `canon_memory/canon_store.py`
- [Canon retrieval](https://github.com/lorekeeper-ku/lorekeeper/blob/main/canon_memory/canon_retriever.py): `canon_memory/canon_retriever.py`
- [Knowledge graph](https://github.com/lorekeeper-ku/lorekeeper/blob/main/canon_memory/knowledge_graph.py): `canon_memory/knowledge_graph.py`
- [Canon memory](https://github.com/lorekeeper-ku/lorekeeper/blob/main/canon_memory/memory.py): `canon_memory/memory.py`
- [Canon proposals](https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/canon_editor.py): `generation_engine/canon_editor.py`

### Story and continuity

- [Character creation](https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/character_creator.py): `generation_engine/character_creator.py`
- [Scene generation](https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/ai_story_generator.py): `generation_engine/ai_story_generator.py`
- [Scene workflow](https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/orchestrator.py): `generation_engine/orchestrator.py`
- [Text visual workflow](https://github.com/lorekeeper-ku/lorekeeper/blob/main/generation_engine/team_pipeline.py): `generation_engine/team_pipeline.py`
- [Continuity checker](https://github.com/lorekeeper-ku/lorekeeper/blob/main/continuity_checker/canon_verifier.py): `continuity_checker/canon_verifier.py`
- [Contradiction classifier](https://github.com/lorekeeper-ku/lorekeeper/blob/main/continuity_checker/contradiction_classifier.py): `continuity_checker/contradiction_classifier.py`
- [Addition judge](https://github.com/lorekeeper-ku/lorekeeper/blob/main/continuity_checker/addition_judge.py): `continuity_checker/addition_judge.py`
- [Predicate policy](https://github.com/lorekeeper-ku/lorekeeper/blob/main/continuity_checker/predicate_policy.py): `continuity_checker/predicate_policy.py`
- [Fact relevance](https://github.com/lorekeeper-ku/lorekeeper/blob/main/continuity_checker/relevance.py): `continuity_checker/relevance.py`
- [NLI comparator](https://github.com/lorekeeper-ku/lorekeeper/blob/main/continuity_checker/nli_comparator.py): `continuity_checker/nli_comparator.py`

### Visuals

- [Image generation checks](https://github.com/lorekeeper-ku/lorekeeper/blob/main/multimodal/visuals.py): `multimodal/visuals.py`
- [Vision result models](https://github.com/lorekeeper-ku/lorekeeper/blob/main/multimodal/model.py): `multimodal/model.py`

### Shared services

- [AI provider client](https://github.com/lorekeeper-ku/lorekeeper/blob/main/shared/ai_client.py): `shared/ai_client.py`
- [Shared contracts](https://github.com/lorekeeper-ku/lorekeeper/blob/main/shared/schemas.py): `shared/schemas.py`

### Other components

- **User**

## Connections

- User → Streamlit UI: sends message (evidence: `README.md`)
- Streamlit UI → Chat components: renders chat (evidence: `app.py`)
- Streamlit UI → Message backend: handles request (evidence: `app.py`)
- Chat components → Message backend: submits request (evidence: `lorekeeper_ui/components.py`)
- Message backend → Tool chat loop: runs conversation (evidence: `generation_engine/ui_backend.py`)
- Tool chat loop → Validated tools: executes calls (evidence: `generation_engine/chat_orchestrator.py`)
- Tool chat loop → AI provider client: requests tool choice (evidence: `generation_engine/chat_orchestrator.py`)
- Validated tools → Canon SQLite store: reads and writes (evidence: `generation_engine/chat_tools.py`)
- Validated tools → Canon memory: retrieves canon (evidence: `generation_engine/chat_tools.py`)
- Validated tools → Canon proposals: proposes changes (evidence: `generation_engine/chat_tools.py`)
- Validated tools → Character creation: creates character (evidence: `generation_engine/chat_tools.py`)
- Validated tools → Scene generation: writes scene (evidence: `generation_engine/chat_tools.py`)
- Validated tools → Text visual workflow: requests visuals (evidence: `generation_engine/chat_tools.py`)
- Validated tools → Image generation checks: generates image (evidence: `generation_engine/chat_tools.py`)
- Message backend → Scene workflow: runs scene pipeline (evidence: `generation_engine/ui_backend.py`)
- Message backend → Text visual workflow: runs team workflow (evidence: `generation_engine/ui_backend.py`)
- Message backend → Conversation context: uses conversation state (evidence: `generation_engine/ui_backend.py`)
- Canon memory → Canon retrieval: retrieves facts (evidence: `canon_memory/memory.py`)
- Canon memory → Canon SQLite store: accesses facts (evidence: `canon_memory/memory.py`)
- Canon retrieval → Canon SQLite store: loads world facts (evidence: `canon_memory/canon_retriever.py`)
- Canon retrieval → Knowledge graph: builds graph (evidence: `canon_memory/canon_retriever.py`)
- Scene workflow → Continuity checker: verifies scene (evidence: `generation_engine/orchestrator.py`)
- Scene workflow → Scene generation: generates and repairs (evidence: `generation_engine/orchestrator.py`)
- Continuity checker → Fact relevance: selects facts (evidence: `continuity_checker/canon_verifier.py`)
- Continuity checker → Contradiction classifier: classifies claims (evidence: `continuity_checker/canon_verifier.py`)
- Continuity checker → NLI comparator: compares claims (evidence: `continuity_checker/nli_comparator.py`)
- Contradiction classifier → Predicate policy: checks predicates (evidence: `continuity_checker/contradiction_classifier.py`)
- Contradiction classifier → Addition judge: judges additions (evidence: `continuity_checker/contradiction_classifier.py`)
- Image generation checks → AI provider client: requests image (evidence: `multimodal/visuals.py`)
- Image generation checks → Vision result models: returns check result (evidence: `multimodal/visuals.py`)
- Canon SQLite store → Shared contracts: validates facts (evidence: `canon_memory/canon_store.py`)
- Image generation checks → Shared contracts: uses asset contracts (evidence: `multimodal/visuals.py`)
- Scene workflow → Shared contracts: uses report contracts (evidence: `generation_engine/orchestrator.py`)
