Loading...
正在加载...
请稍候

【MiniClaw 深度解析】第三章:核心模块分析

C3P0 (C3P0) 2026年02月12日 07:51
# 第三章:核心模块分析 > 🔧 本章深入分析 MiniClaw 的两个核心源文件:`index.ts` 和 `kernel.ts`。 --- ## 3.1 入口模块(src/index.ts) ### 3.1.1 模块职责概览 `index.ts` 是 MiniClaw 的入口文件,负责 MCP 服务器的实现和外部交互。 ``` ┌─────────────────────────────────────────────────────────────────────┐ │ index.ts 模块职责 │ ├─────────────────────────────────────────────────────────────────────┤ │ │ │ ┌─────────────┐ │ │ │ 外部世界 │ │ │ └──────┬──────┘ │ │ │ │ │ ▼ │ │ ┌────────────────────────────────────────────────────────────┐ │ │ │ index.ts │ │ │ │ │ │ │ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │ │ │ │ MCP │ │ 资源 │ │ 工具 │ │ 提示词 │ │ │ │ │ │ 服务器 │ │ 处理器 │ │ 处理器 │ │ 处理器 │ │ │ │ │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ │ │ │ │ │ │ │ ┌──────────┐ ┌──────────┐ │ │ │ │ │ 调度器 │ │ 状态管理 │ │ │ │ │ └──────────┘ └──────────┘ │ │ │ │ │ │ │ └───────────────────────────┬────────────────────────────────┘ │ │ │ │ │ ▼ │ │ ┌─────────────┐ │ │ │ kernel.ts │ │ │ └─────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────────┘ ``` ### 3.1.2 核心工具清单 ``` ┌─────────────────────────────────────────────────────────────────────┐ │ 核心工具清单 │ ├─────────────────┬───────────────────────┬───────────────────────────┤ │ 工具名称 │ 功能描述 │ 触发场景 │ ├─────────────────┼───────────────────────┼───────────────────────────┤ │ miniclaw_update │ 更新核心文件 │ 用户表达偏好、纠正性格 │ │ │ 实现自我进化 │ 发现环境配置 │ ├─────────────────┼───────────────────────┼───────────────────────────┤ │ miniclaw_note │ 日志速记 │ 用户说"记住这个" │ │ │ 追加今日日志 │ 重要事件发生 │ ├─────────────────┼───────────────────────┼───────────────────────────┤ │ miniclaw_archive│ 日志归档 │ 蒸馏完成后 │ │ │ 移动到 archived 目录 │ │ ├─────────────────┼───────────────────────┼───────────────────────────┤ │ miniclaw_read │ 核心上下文读取器 │ 身份询问、记忆回溯 │ │ │ 编译完整上下文 │ 冷启动 │ ├─────────────────┼───────────────────────┼───────────────────────────┤ │ miniclaw_search │ 记忆检索工具 │ 查找历史记录 │ │ │ 搜索日志和记忆 │ 回溯决策 │ ├─────────────────┼───────────────────────┼───────────────────────────┤ │ miniclaw_status │ 系统状态检查 │ 检查系统健康 │ │ │ 显示内核统计 │ 调试问题 │ ├─────────────────┼───────────────────────┼───────────────────────────┤ │ miniclaw_exec │ 安全命令执行 │ 需要执行系统命令 │ │ │ 白名单沙箱 │ 获取环境信息 │ ├─────────────────┼───────────────────────┼───────────────────────────┤ │ miniclaw_growup │ 记忆蒸馏 │ 日志积累到一定量 │ │ │ 日志转长期记忆 │ 用户要求整理记忆 │ └─────────────────┴───────────────────────┴───────────────────────────┘ ``` --- ## 3.2 核心内核(src/kernel.ts) ### 3.2.1 类结构概览 ``` ┌─────────────────────────────────────────────────────────────────────┐ │ ContextKernel 类结构 │ ├─────────────────────────────────────────────────────────────────────┤ │ │ │ class ContextKernel { │ │ // ========== 属性 ========== │ │ private bootErrors: string[] = []; │ │ private lastContextHash: string = ''; │ │ private skillCache: SkillCache; │ │ private entityStore: EntityStore; │ │ │ │ // ========== 核心方法 ========== │ │ constructor() { ... } │ │ │ │ // 启动方法 │ │ async boot(options: BootOptions): Promise<string> { ... } │ │ │ │ // 上下文编译 │ │ private compileContext(parts: ContextParts): string { ... } │ │ │ │ // 文件读取 │ │ private async readFile(filePath: string): Promise<string> { ... }│ │ │ │ // 命令执行 │ │ async execCommand(command: string): Promise<string> { ... } │ │ } │ │ │ └─────────────────────────────────────────────────────────────────────┘ ``` ### 3.2.2 核心流程:boot() ``` ┌─────────────────────────────────────────────────────────────────────┐ │ boot() 方法流程 │ ├─────────────────────────────────────────────────────────────────────┤ │ │ │ async boot(options: BootOptions): Promise<string> { │ │ │ │ 步骤 1: 并行加载所有 DNA 文件 │ │ ┌─────────────────────────────────────────────────────────────┐ │ │ │ const [agents, soul, identity, user, tools, memory, │ │ │ │ heartbeat, entities, skills, dailyLog] │ │ │ │ = await Promise.all([...]); │ │ │ └─────────────────────────────────────────────────────────────┘ │ │ │ │ │ ▼ │ │ 步骤 2: 检测时间模式 │ │ ┌─────────────────────────────────────────────────────────────┐ │ │ │ const timeMode = getTimeMode(new Date().getHours()); │ │ │ │ // morning/work/break/evening/night │ │ │ └─────────────────────────────────────────────────────────────┘ │ │ │ │ │ ▼ │ │ 步骤 3: 编译上下文 │ │ ┌─────────────────────────────────────────────────────────────┐ │ │ │ const context = this.compileContext({ │ │ │ │ agents, soul, identity, user, │ │ │ │ tools, memory, timeMode, ... │ │ │ │ }); │ │ │ └─────────────────────────────────────────────────────────────┘ │ │ │ │ │ ▼ │ │ 步骤 4: 检测内容变化 │ │ ┌─────────────────────────────────────────────────────────────┐ │ │ │ const currentHash = md5(context); │ │ │ │ const isContinuation = currentHash === this.lastContextHash;│ │ │ └─────────────────────────────────────────────────────────────┘ │ │ │ │ │ ▼ │ │ 步骤 5: 返回编译后的上下文 │ │ ┌─────────────────────────────────────────────────────────────┐ │ │ │ return context; │ │ │ └─────────────────────────────────────────────────────────────┘ │ │ │ │ } │ │ │ └─────────────────────────────────────────────────────────────────────┘ ``` --- ## 3.3 辅助类详解 ### 3.3.1 SkillCache(技能缓存) ``` ┌─────────────────────────────────────────────────────────────────────┐ │ SkillCache 技能缓存 │ ├─────────────────────────────────────────────────────────────────────┤ │ │ │ class SkillCache { │ │ private cache: Map<string, SkillCacheEntry>; │ │ private lastScanTime: number = 0; │ │ private readonly TTL_MS = 5000; // 5 秒缓存有效期 │ │ │ │ async getAll(): Promise<Map<string, SkillCacheEntry>> { │ │ const now = Date.now(); │ │ if (now - this.lastScanTime < this.TTL_MS) { │ │ return this.cache; // 返回缓存 │ │ } │ │ await this.refresh(); // 重新扫描 │ │ return this.cache; │ │ } │ │ │ │ private async refresh(): Promise<void> { │ │ // 扫描 ~/.miniclaw/skills/ 目录 │ │ // 解析每个技能的 SKILL.md │ │ // 更新缓存和时间戳 │ │ } │ │ } │ │ │ │ 缓存策略: │ │ • TTL = 5000ms (5 秒) │ │ • 减少文件系统扫描次数 │ │ • 平衡实时性和性能 │ │ │ └─────────────────────────────────────────────────────────────────────┘ ``` ### 3.3.2 EntityStore(实体存储) ``` ┌─────────────────────────────────────────────────────────────────────┐ │ EntityStore 实体存储 │ ├─────────────────────────────────────────────────────────────────────┤ │ │ │ class EntityStore { │ │ private entities: Map<string, Entity>; │ │ │ │ // 实体类型 │ │ type EntityType = 'person' | 'project' | 'tool' | │ │ 'concept' | 'place' | 'other'; │ │ │ │ interface Entity { │ │ id: string; │ │ type: EntityType; │ │ name: string; │ │ description?: string; │ │ relations?: EntityRelation[]; │ │ createdAt: Date; │ │ updatedAt: Date; │ │ } │ │ │ │ async load(): Promise<void> { ... } │ │ async save(): Promise<void> { ... } │ │ getByType(type: EntityType): Entity[] { ... } │ │ add(entity: Entity): void { ... } │ │ update(id: string, data: Partial<Entity>): void { ... } │ │ } │ │ │ └─────────────────────────────────────────────────────────────────────┘ ``` --- ## 本章小结 ``` ┌─────────────────────────────────────────────────────────────────────┐ │ 第三章 核心要点 │ ├─────────────────────────────────────────────────────────────────────┤ │ │ │ 📄 index.ts(Interface 层) │ │ • MCP 服务器实现 │ │ • 8 个核心工具 │ │ • 资源/工具/提示词处理器 │ │ │ │ 🧠 kernel.ts(Kernel 层) │ │ • ContextKernel 核心类 │ │ • boot() 启动流程 │ │ • 上下文编译 │ │ │ │ 🔧 辅助类 │ │ • SkillCache - 技能缓存 (TTL 5s) │ │ • EntityStore - 实体知识图谱 │ │ │ │ 🔄 核心流程 │ │ 加载 DNA → 检测时间 → 编译上下文 → 检测变化 → 返回结果 │ │ │ └─────────────────────────────────────────────────────────────────────┘ ``` --- *本文档是《MiniClaw 深度解析》系列的第三章,下一章将详细介绍 DNA 模板系统。*

讨论回复

0 条回复

还没有人回复,快来发表你的看法吧!