# captainflasmr/ollama-buddy architecture

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

- Interactive diagram: https://gitdiagram.com/captainflasmr/ollama-buddy
- Repository: https://github.com/captainflasmr/ollama-buddy

## Overview

#### Purpose and Entry Points
Ollama Buddy is an Emacs 29.1+ Lisp package for local Ollama chat and model management, extended with hosted and OpenAI-compatible LLM providers. The user-facing entry points are role and main Transient menus, which open/manage Org-native chat sessions. Package loading begins at `ollama-buddy.el`; chat lifecycle and shared state reside in `ollama-buddy-core.el`; menu composition is in `ollama-buddy-transient.el`.

#### Chat Runtime and Request Pipeline
Chat buffers collect prompts, conversation history, attachments, parameters, and response metadata, then stream results into Org-formatted output, including foldable reasoning blocks. Core dispatch supports Emacs network processes and a curl fallback; cancellation and error handling belong on this boundary. Relevant paths: `ollama-buddy-core.el`, `ollama-buddy-curl.el`, `ollama-buddy-remote.el`.

#### Provider Abstraction
`ollama-buddy-provider.el` is the central adapter layer: provider registrations define authentication, endpoint/model discovery, API type, and model prefixes. It translates the internal conversation/tool format into Ollama, OpenAI-compatible, Claude, or Gemini request formats. Provider-specific compatibility modules remain available for OpenAI, Claude, Gemini, Grok, Copilot, Codestral, DeepSeek, OpenRouter, and OpenCode. Paths: `ollama-buddy-provider.el`, `ollama-buddy-openai-compat.el`, `ollama-buddy-claude.el`, `ollama-buddy-gemini.el`.

#### Agentic Execution, Planning, and Project Context
The tools subsystem exposes registered Emacs operations—including file, buffer, shell, search, and evaluation capabilities—to model tool calls, with safe and unguarded execution modes. Plan mode converts model-produced work into Org TODO headings and executes steps with the same tool loop. Project integration supplies project-root context, summaries, and external-agent launch support. Paths: `ollama-buddy-tools.el`, `ollama-buddy-plan.el`, `ollama-buddy-project.el`, `ollama-buddy-opencode.el`.

#### Retrieval and Search
The RAG module indexes local documents/source, creates embeddings through Ollama `/api/embed` or an OpenAI-compatible embeddings service, and retrieves chunks for inline `@rag` expansion or tool calls. Web search is a distinct API integration used both inline and as a tool. Paths: `ollama-buddy-rag.el`, `ollama-buddy-web-search.el`, `ollama-buddy-tools.el`.

#### Editing and Coding Workflows
Prompt commands can target either chat or in-buffer rewriting. The rewrite subsystem streams candidate text into the source buffer, supports acceptance/rejection and diff presentation, and strips model formatting. A separate completion mode performs FIM-style inline code completion through Ollama generation. Paths: `ollama-buddy-rewrite.el`, `ollama-buddy-completion.el`, `ollama-buddy-core.el`.

#### Personas, Presets, and Annotation
Role presets define command/model/system-prompt configurations; user prompts and skills are Org-based content loaded into sessions or expanded inline. Directory annotation batches source files through a structured response workflow and applies project-relative annotations. Bundled editable content lives outside executable Lisp. Paths: `ollama-buddy-user-prompts.el`, `ollama-buddy-presets/ollama-buddy--preset__developer.el`, `ollama-buddy-annotate.el`, `ollama-buddy-user-prompts/skills__Annotate-Project__system.org`.

#### External Dependencies and Infrastructure
The primary local service is Ollama (`/api/chat`, model APIs, embeddings, cloud tags, and web search); remote services include OpenAI, Anthropic, Google Gemini, xAI, Mistral, DeepSeek, OpenRouter, GitHub Copilot, and arbitrary local OpenAI servers such as LM Studio/vLLM. Credentials are expected through Emacs `auth-source`. External coding agents and terminals are launched from Emacs; Pi integration is implemented in `pi-extension/ollama-buddy-pi.ts`. Reference docs are generated/maintained under `docs/ollama-buddy.org`.

Main entry points: [`ollama-buddy-core.el`](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-core.el) (Chat runtime), [`ollama-buddy-provider.el`](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-provider.el) (Provider adapter), [`ollama-buddy-tools.el`](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-tools.el) (Tool execution loop), [`ollama-buddy-curl.el`](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-curl.el) (Request transport)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_emacs["Emacs package"]
  node_entry["Package entry<br/>Emacs Lisp entry<br/>[ollama-buddy.el]"]
  node_menus["Role &amp; main menus<br/>Transient UI"]
  node_chat["Chat runtime<br/>Org chat sessions"]
  node_transport["Request transport<br/>network / curl"]
  node_remote["Remote requests<br/>remote transport"]
  node_rewrite["Rewrite workflow<br/>in-buffer editing"]
  node_completion["Inline completion<br/>FIM completion mode"]
  node_presets["Roles &amp; prompts<br/>Org-backed presets"]
  node_developer_preset["Developer preset<br/>role preset"]
end

subgraph group_providers["Provider layer"]
  node_provider["Provider adapter<br/>provider registry"]
  node_openai_compat["OpenAI-compatible API<br/>provider compatibility"]
  node_claude["Claude provider<br/>provider compatibility"]
  node_gemini["Gemini provider<br/>provider compatibility"]
end

subgraph group_automation["Automation &amp; context"]
  node_tools["Tool execution loop<br/>Emacs operations"]
  node_plan["Plan mode<br/>Org TODO executor"]
  node_project["Project context<br/>project integration"]
  node_rag[("RAG index &amp; retrieval<br/>document retrieval")]
  node_web_search["Web search<br/>search integration"]
  node_annotate["Project annotation<br/>structured batch workflow"]
end

subgraph group_external["External services"]
  node_ollama{{"Ollama<br/>local model service"}}
  node_hosted{{"Hosted LLM APIs<br/>remote providers"}}
  node_auth["auth-source<br/>credential store"]
  node_pi["Pi extension<br/>external agent integration<br/>[ollama-buddy-pi.ts]"]
end

node_entry -->|"loads"| node_menus
node_entry -->|"loads"| node_chat
node_menus -->|"opens sessions"| node_chat
node_chat -->|"dispatches requests"| node_transport
node_chat -->|"selects provider"| node_provider
node_chat -->|"targets edits"| node_rewrite
node_chat -->|"supports completion"| node_completion
node_chat -->|"loads role context"| node_presets
node_presets -->|"includes"| node_developer_preset
node_transport -->|"local API"| node_ollama
node_transport -.->|"fallback / remote"| node_remote
node_remote -->|"API requests"| node_hosted
node_provider -->|"adapts"| node_openai_compat
node_provider -->|"adapts"| node_claude
node_provider -->|"adapts"| node_gemini
node_provider -->|"reads credentials"| node_auth
node_chat -->|"model tool calls"| node_tools
node_plan -->|"executes steps"| node_tools
node_tools -->|"retrieval tool"| node_rag
node_tools -->|"search tool"| node_web_search
node_rag -->|"embeddings request"| node_provider
node_project -->|"launches agent"| node_pi
node_annotate -->|"structured processing"| node_tools

click node_entry "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy.el"
click node_menus "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-transient.el"
click node_chat "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-core.el"
click node_transport "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-curl.el"
click node_remote "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-remote.el"
click node_rewrite "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-rewrite.el"
click node_completion "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-completion.el"
click node_presets "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-user-prompts.el"
click node_developer_preset "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-presets/ollama-buddy--preset__developer.el"
click node_provider "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-provider.el"
click node_openai_compat "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-openai-compat.el"
click node_claude "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-claude.el"
click node_gemini "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-gemini.el"
click node_tools "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-tools.el"
click node_plan "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-plan.el"
click node_project "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-project.el"
click node_rag "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-rag.el"
click node_web_search "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-web-search.el"
click node_annotate "https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-annotate.el"
click node_pi "https://github.com/captainflasmr/ollama-buddy/blob/main/pi-extension/ollama-buddy-pi.ts"

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_entry,node_menus,node_chat,node_transport,node_remote,node_rewrite,node_completion,node_presets,node_developer_preset toneBlue
class node_provider,node_openai_compat,node_claude,node_gemini toneAmber
class node_tools,node_plan,node_project,node_rag,node_web_search,node_annotate toneMint
class node_ollama,node_hosted,node_auth,node_pi toneRose
```

## Components

### Emacs package

- [Package entry](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy.el): Emacs Lisp entry · Loads the package and commands. · `ollama-buddy.el`
- [Role & main menus](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-transient.el): Transient UI · Opens and manages chat workflows. · `ollama-buddy-transient.el`
- [Chat runtime](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-core.el): Org chat sessions · Owns prompts, history, attachments, streaming output, and lifecycle state. · `ollama-buddy-core.el`
- [Request transport](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-curl.el): network / curl · Dispatches requests and handles cancellation and errors. · `ollama-buddy-curl.el`
- [Remote requests](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-remote.el): remote transport · `ollama-buddy-remote.el`
- [Rewrite workflow](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-rewrite.el): in-buffer editing · Streams candidate edits with diff and accept/reject controls. · `ollama-buddy-rewrite.el`
- [Inline completion](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-completion.el): FIM completion mode · `ollama-buddy-completion.el`
- [Roles & prompts](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-user-prompts.el): Org-backed presets · Loads role configurations, user prompts, and skills. · `ollama-buddy-user-prompts.el`
- [Developer preset](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-presets/ollama-buddy--preset__developer.el): role preset · `ollama-buddy-presets/ollama-buddy--preset__developer.el`

### Provider layer

- [Provider adapter](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-provider.el): provider registry · Converts internal conversations and tools to provider-specific APIs. · `ollama-buddy-provider.el`
- [OpenAI-compatible API](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-openai-compat.el): provider compatibility · `ollama-buddy-openai-compat.el`
- [Claude provider](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-claude.el): provider compatibility · `ollama-buddy-claude.el`
- [Gemini provider](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-gemini.el): provider compatibility · `ollama-buddy-gemini.el`

### Automation & context

- [Tool execution loop](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-tools.el): Emacs operations · Runs registered file, buffer, shell, search, and evaluation tools. · `ollama-buddy-tools.el`
- [Plan mode](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-plan.el): Org TODO executor · Turns model work plans into executable tool-loop steps. · `ollama-buddy-plan.el`
- [Project context](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-project.el): project integration · Supplies project-root context, summaries, and agent launching. · `ollama-buddy-project.el`
- [RAG index & retrieval](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-rag.el): document retrieval · Indexes local content, creates embeddings, and injects retrieved chunks. · `ollama-buddy-rag.el`
- [Web search](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-web-search.el): search integration · Supports inline expansion and tool calls. · `ollama-buddy-web-search.el`
- [Project annotation](https://github.com/captainflasmr/ollama-buddy/blob/main/ollama-buddy-annotate.el): structured batch workflow · Applies project-relative annotations to source files. · `ollama-buddy-annotate.el`

### External services

- **Ollama**: local model service · Provides chat, generation, model, embedding, and search APIs.
- **Hosted LLM APIs**: remote providers · OpenAI-compatible, Anthropic, Gemini, and other hosted models.
- **auth-source**: credential store
- [Pi extension](https://github.com/captainflasmr/ollama-buddy/blob/main/pi-extension/ollama-buddy-pi.ts): external agent integration · `pi-extension/ollama-buddy-pi.ts`

## Connections

- Package entry → Role & main menus: loads
- Package entry → Chat runtime: loads
- Role & main menus → Chat runtime: opens sessions
- Chat runtime → Request transport: dispatches requests
- Chat runtime → Provider adapter: selects provider
- Chat runtime → Rewrite workflow: targets edits
- Chat runtime → Inline completion: supports completion
- Chat runtime → Roles & prompts: loads role context
- Roles & prompts → Developer preset: includes
- Request transport → Ollama: local API
- Request transport → Remote requests: fallback / remote
- Remote requests → Hosted LLM APIs: API requests
- Provider adapter → OpenAI-compatible API: adapts
- Provider adapter → Claude provider: adapts
- Provider adapter → Gemini provider: adapts
- Provider adapter → auth-source: reads credentials
- Chat runtime → Tool execution loop: model tool calls
- Plan mode → Tool execution loop: executes steps
- Tool execution loop → RAG index & retrieval: retrieval tool
- Tool execution loop → Web search: search tool
- RAG index & retrieval → Provider adapter: embeddings request
- Project context → Pi extension: launches agent
- Project annotation → Tool execution loop: structured processing
