# kachofugetsu09/akashic-agent architecture

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

- Interactive diagram: https://gitdiagram.com/kachofugetsu09/akashic-agent
- Repository: https://github.com/kachofugetsu09/akashic-agent

## Overview

#### 定位与入口
Akashic 是 Python 3.12 的可扩展个人 AI Agent：处理多渠道对话，并基于外部信息、记忆和空闲任务主动推送。进程入口是 `main.py`；启动/初始化、依赖装配和 API/渠道宿主集中在 `bootstrap/app.py`、`bootstrap/wiring.py`、`bootstrap/channel_host.py`。运行数据边界是用户工作区 `~/.akashic/workspace/`。

#### 输入渠道与服务边界
渠道实现统一落在 `infra/channels/base.py`，由宿主接入 CLI/TUI、Web Chat、Telegram、QQ：`infra/channels/web_chat_channel.py`、`infra/channels/telegram_channel.py`、`infra/channels/qq_channel.py`。本地 Chat 与 Dashboard 的 HTTP API 分别由 `bootstrap/chat_api.py`、`bootstrap/dashboard_api.py` 暴露；前端是独立 Vite/React 应用，入口为 `frontend/chat/src/main.tsx` 和 `frontend/dashboard/src/main.tsx`。

#### 被动对话执行管线
消息进入后由回合编排器驱动 `agent/turns/orchestrator.py`，核心 agent runner 位于 `agent/core/runner.py`。生命周期门面和阶段定义在 `agent/lifecycle/facade.py`、`agent/lifecycle/phase.py`，按 BeforeTurn、BeforeReasoning、PromptRender、推理、AfterReasoning、AfterTurn 等边界允许扩展介入。提示词组装与 token 预算位于 `agent/prompting/assembler.py`、`agent/prompting/budget.py`；LLM 提供者抽象在 `agent/provider.py` 和 `infra/providers/llm_provider.py`，对接配置的 OpenAI-compatible DeepSeek/Qwen 等服务。

#### 工具、MCP 与后台委派
模型工具循环由 `agent/looping/core.py` 和 `agent/looping/handlers.py` 控制，工具注册和执行边界在 `agent/tools/registry.py`、`agent/tool_runtime.py`。内建能力包括 shell、文件、Web、日程、记忆和子 agent；子任务生命周期由 `agent/background/subagent_manager.py` 管理。MCP 服务器的连接、注册和工具转换位于 `agent/mcp/client.py`、`agent/mcp/registry.py`、`agent/mcp/tool.py`，使外部数据源与工具不直接耦合到 agent 核心。

#### 记忆与会话状态
会话级状态由 `session/manager.py`、`session/store.py` 管理。长期记忆有引擎契约层 `core/memory/engine.py`，默认实现以插件形式装载；语义检索、查询改写、画像/事实提取和响应后 consolidation 位于 `memory2/retriever.py`、`memory2/post_response_worker.py`、`memory2/store.py`。该边界将 Markdown 长期记忆、向量库及 embedding/fast LLM 服务隔离，并在回合前检索注入、回合后异步归档。

#### 插件与热重载扩展面
插件是主要扩展机制，负责生命周期模块、事件订阅、工具拦截/注册、MCP 服务、技能与 Dashboard 面板。装载、代际快照和热重载由 `agent/plugins/manager.py`、`agent/plugins/snapshot.py`、`agent/plugins/watcher.py` 管理：候选版本验证后原子切换，新请求使用新代际，运行中请求保持旧代际。内置插件包如记忆与主动能力位于 `plugins/default_memory/plugin.py`、`plugins/default_proactive/plugin.py`；可安装包定义在 `plugin_packages/`。

#### 主动推送与空闲执行
事件总线和队列在 `bus/event_bus.py`、`bus/queue.py`，连接生命周期、主动事件及内部事件。调度器 `agent/scheduler.py` 触发主动周期；主动内核在 `agent/core/proactive_kernel.py`，通过 MCP 信息源评估 alert/content/context，并经渠道发送。较新的主动运行时模型位于 `proactive_v2/loop.py`、`proactive_v2/energy.py`、`proactive_v2/sensor.py`；无合适推送时，可通过技能和插件执行 Drift 空闲任务。

Main entry points: [`agent/turns/orchestrator.py`](https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/turns/orchestrator.py) (回合编排器), [`agent/core/proactive_kernel.py`](https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/core/proactive_kernel.py) (主动内核), [`agent/plugins/manager.py`](https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/plugins/manager.py) (插件热重载), [`agent/core/runner.py`](https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/core/runner.py) (Agent Runner)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_host["启动与接入"]
  node_entry["进程入口<br/>Python entrypoint<br/>[main.py]"]
  node_bootstrap["应用装配<br/>bootstrap<br/>[app.py]"]
  node_channels["渠道宿主<br/>channel host<br/>[channel_host.py]"]
  node_http_apis["Chat 与 Dashboard API<br/>HTTP APIs<br/>[chat_api.py]"]
  node_frontends["React 前端<br/>Vite apps<br/>[main.tsx]"]
end

subgraph group_agent["对话执行"]
  node_orchestrator["回合编排器<br/>turn orchestrator<br/>[orchestrator.py]"]
  node_lifecycle["生命周期扩展点<br/>phase facade<br/>[facade.py]"]
  node_runner["Agent Runner<br/>agent core<br/>[runner.py]"]
  node_prompting["提示词与预算<br/>prompt assembly<br/>[assembler.py]"]
  node_llm{{"LLM 提供者<br/>provider boundary<br/>[llm_provider.py]"}}
end

subgraph group_extensions["扩展与工具"]
  node_tool_loop["模型工具循环<br/>tool loop<br/>[core.py]"]
  node_tool_runtime["工具运行时<br/>tool boundary<br/>[tool_runtime.py]"]
  node_mcp{{"MCP 集成<br/>MCP client registry<br/>[client.py]"}}
  node_plugins["插件热重载<br/>plugin manager<br/>[manager.py]"]
end

subgraph group_state["状态与记忆"]
  node_sessions[("会话状态<br/>session manager<br/>[manager.py]")]
  node_memory[("长期记忆<br/>memory engine<br/>[retriever.py]")]
  node_workspace[("用户工作区<br/>runtime data boundary")]
end

subgraph group_proactive["主动运行时"]
  node_event_bus["事件总线<br/>event queue<br/>[event_bus.py]"]
  node_scheduler["主动调度器<br/>scheduler<br/>[scheduler.py]"]
  node_proactive_kernel["主动内核<br/>proactive agent"]
  node_proactive_v2["主动运行时 v2<br/>idle loop<br/>[loop.py]"]
end

node_entry -->|"启动"| node_bootstrap
node_bootstrap -->|"装配"| node_channels
node_bootstrap -->|"暴露"| node_http_apis
node_frontends -->|"HTTP"| node_http_apis
node_channels -->|"入站消息"| node_orchestrator
node_http_apis -->|"聊天请求"| node_orchestrator
node_orchestrator -->|"驱动阶段"| node_lifecycle
node_orchestrator -->|"执行回合"| node_runner
node_runner -->|"组装上下文"| node_prompting
node_prompting -->|"推理请求"| node_llm
node_runner -->|"工具调用"| node_tool_loop
node_tool_loop -->|"执行工具"| node_tool_runtime
node_tool_runtime -->|"外部工具"| node_mcp
node_orchestrator -->|"读取与更新"| node_sessions
node_memory -->|"检索注入"| node_prompting
node_runner -.->|"响应后归档"| node_memory
node_sessions -->|"持久化"| node_workspace
node_memory -->|"持久化"| node_workspace
node_plugins -.->|"扩展阶段"| node_lifecycle
node_plugins -.->|"注册与拦截"| node_tool_runtime
node_plugins -.->|"注册服务"| node_mcp
node_scheduler -->|"触发周期"| node_proactive_kernel
node_event_bus -->|"主动事件"| node_proactive_kernel
node_proactive_v2 -->|"评估与空闲任务"| node_proactive_kernel
node_proactive_kernel -->|"查询信息源"| node_mcp
node_proactive_kernel -->|"主动推送"| node_channels
node_plugins -.->|"提供技能"| node_proactive_v2

click node_entry "https://github.com/kachofugetsu09/akashic-agent/blob/main/main.py"
click node_bootstrap "https://github.com/kachofugetsu09/akashic-agent/blob/main/bootstrap/app.py"
click node_channels "https://github.com/kachofugetsu09/akashic-agent/blob/main/bootstrap/channel_host.py"
click node_http_apis "https://github.com/kachofugetsu09/akashic-agent/blob/main/bootstrap/chat_api.py"
click node_frontends "https://github.com/kachofugetsu09/akashic-agent/blob/main/frontend/chat/src/main.tsx"
click node_orchestrator "https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/turns/orchestrator.py"
click node_lifecycle "https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/lifecycle/facade.py"
click node_runner "https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/core/runner.py"
click node_prompting "https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/prompting/assembler.py"
click node_llm "https://github.com/kachofugetsu09/akashic-agent/blob/main/infra/providers/llm_provider.py"
click node_tool_loop "https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/looping/core.py"
click node_tool_runtime "https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/tool_runtime.py"
click node_mcp "https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/mcp/client.py"
click node_plugins "https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/plugins/manager.py"
click node_sessions "https://github.com/kachofugetsu09/akashic-agent/blob/main/session/manager.py"
click node_memory "https://github.com/kachofugetsu09/akashic-agent/blob/main/memory2/retriever.py"
click node_event_bus "https://github.com/kachofugetsu09/akashic-agent/blob/main/bus/event_bus.py"
click node_scheduler "https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/scheduler.py"
click node_proactive_kernel "https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/core/proactive_kernel.py"
click node_proactive_v2 "https://github.com/kachofugetsu09/akashic-agent/blob/main/proactive_v2/loop.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_entry,node_bootstrap,node_channels,node_http_apis,node_frontends toneBlue
class node_orchestrator,node_lifecycle,node_runner,node_prompting,node_llm toneAmber
class node_tool_loop,node_tool_runtime,node_mcp,node_plugins toneMint
class node_sessions,node_memory,node_workspace toneRose
class node_event_bus,node_scheduler,node_proactive_kernel,node_proactive_v2 toneIndigo
```

## Components

### 启动与接入

- [进程入口](https://github.com/kachofugetsu09/akashic-agent/blob/main/main.py): Python entrypoint · `main.py`
- [应用装配](https://github.com/kachofugetsu09/akashic-agent/blob/main/bootstrap/app.py): bootstrap · 初始化依赖、API 与渠道宿主。 · `bootstrap/app.py`
- [渠道宿主](https://github.com/kachofugetsu09/akashic-agent/blob/main/bootstrap/channel_host.py): channel host · 统一接入 CLI/TUI、Web Chat、Telegram 和 QQ。 · `bootstrap/channel_host.py`
- [Chat 与 Dashboard API](https://github.com/kachofugetsu09/akashic-agent/blob/main/bootstrap/chat_api.py): HTTP APIs · `bootstrap/chat_api.py`
- [React 前端](https://github.com/kachofugetsu09/akashic-agent/blob/main/frontend/chat/src/main.tsx): Vite apps · Chat 和 Dashboard 是独立前端应用。 · `frontend/chat/src/main.tsx`

### 对话执行

- [回合编排器](https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/turns/orchestrator.py): turn orchestrator · `agent/turns/orchestrator.py`
- [生命周期扩展点](https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/lifecycle/facade.py): phase facade · 在回合、推理和提示词阶段调用扩展。 · `agent/lifecycle/facade.py`
- [Agent Runner](https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/core/runner.py): agent core · `agent/core/runner.py`
- [提示词与预算](https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/prompting/assembler.py): prompt assembly · `agent/prompting/assembler.py`
- [LLM 提供者](https://github.com/kachofugetsu09/akashic-agent/blob/main/infra/providers/llm_provider.py): provider boundary · 对接 OpenAI-compatible 的 DeepSeek、Qwen 等服务。 · `infra/providers/llm_provider.py`

### 扩展与工具

- [模型工具循环](https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/looping/core.py): tool loop · `agent/looping/core.py`
- [工具运行时](https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/tool_runtime.py): tool boundary · 注册并执行内建工具与子 agent。 · `agent/tool_runtime.py`
- [MCP 集成](https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/mcp/client.py): MCP client registry · 将外部服务转换为 Agent 工具。 · `agent/mcp/client.py`
- [插件热重载](https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/plugins/manager.py): plugin manager · 以代际快照原子切换插件版本。 · `agent/plugins/manager.py`

### 状态与记忆

- [会话状态](https://github.com/kachofugetsu09/akashic-agent/blob/main/session/manager.py): session manager · `session/manager.py`
- [长期记忆](https://github.com/kachofugetsu09/akashic-agent/blob/main/memory2/retriever.py): memory engine · 检索注入并在响应后异步归档。 · `memory2/retriever.py`
- **用户工作区**: runtime data boundary · 持久化运行数据边界。

### 主动运行时

- [事件总线](https://github.com/kachofugetsu09/akashic-agent/blob/main/bus/event_bus.py): event queue · 承载生命周期、主动及内部事件。 · `bus/event_bus.py`
- [主动调度器](https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/scheduler.py): scheduler · `agent/scheduler.py`
- [主动内核](https://github.com/kachofugetsu09/akashic-agent/blob/main/agent/core/proactive_kernel.py): proactive agent · 评估 MCP 信息后选择渠道推送。 · `agent/core/proactive_kernel.py`
- [主动运行时 v2](https://github.com/kachofugetsu09/akashic-agent/blob/main/proactive_v2/loop.py): idle loop · 基于传感器与能量模型执行主动或 Drift 空闲任务。 · `proactive_v2/loop.py`

## Connections

- 进程入口 → 应用装配: 启动
- 应用装配 → 渠道宿主: 装配
- 应用装配 → Chat 与 Dashboard API: 暴露
- React 前端 → Chat 与 Dashboard API: HTTP
- 渠道宿主 → 回合编排器: 入站消息
- Chat 与 Dashboard API → 回合编排器: 聊天请求
- 回合编排器 → 生命周期扩展点: 驱动阶段
- 回合编排器 → Agent Runner: 执行回合
- Agent Runner → 提示词与预算: 组装上下文
- 提示词与预算 → LLM 提供者: 推理请求
- Agent Runner → 模型工具循环: 工具调用
- 模型工具循环 → 工具运行时: 执行工具
- 工具运行时 → MCP 集成: 外部工具
- 回合编排器 → 会话状态: 读取与更新
- 长期记忆 → 提示词与预算: 检索注入
- Agent Runner → 长期记忆: 响应后归档
- 会话状态 → 用户工作区: 持久化
- 长期记忆 → 用户工作区: 持久化
- 插件热重载 → 生命周期扩展点: 扩展阶段
- 插件热重载 → 工具运行时: 注册与拦截
- 插件热重载 → MCP 集成: 注册服务
- 主动调度器 → 主动内核: 触发周期
- 事件总线 → 主动内核: 主动事件
- 主动运行时 v2 → 主动内核: 评估与空闲任务
- 主动内核 → MCP 集成: 查询信息源
- 主动内核 → 渠道宿主: 主动推送
- 插件热重载 → 主动运行时 v2: 提供技能
