乐于分享
好东西不私藏

OpenClaw.NET 技术解读(第四篇):OpenClaw.Core 核心库

OpenClaw.NET 技术解读(第四篇):OpenClaw.Core 核心库

摘要:Gateway 负责组装进程,Agent 负责对话循环;OpenClaw.Core 则是跨进程的 共享契约层:配置模型、会话与持久化抽象、消息管道、安全与校验、插件与技能元数据、可观测性原语。本篇按目录与职责梳理,并说明它为何被标为 AOT 友好。


一、定位与包身份

OpenClaw.Core 在 csproj 中的描述是:abstractions, models, validation, security, and memory primitives(抽象、模型、校验、安全、内存原语)。它被 Gateway、Agent、Channels、CLI 等引用,是 “领域模型 + 可插拔接口 + 横切能力” 的集中地,而不是某个 HTTP 宿主。

    <IsAotCompatible>true</IsAotCompatible>    <Description>Core abstractions, models, validation, security, and memory primitives for OpenClaw.NET.</Description>

为何单独成库:把 GatewayConfigSessionInboundMessageIMemoryStoreITool 等稳定边界沉淀在这里,宿主与运行时只做组合;便于测试、裁剪(trim)与 NativeAOT 发布。


二、目录地图(按文件夹)

仓库内约 百余个 C# 文件,可按顶层文件夹快速导航:

目录
主要职责
Models/ GatewayConfig

 及子配置块(内存、渠道、插件、技能、安全等)、Session / SessionBranch、契约与集成 API 的 DTO、Operator / Admin 相关模型
Abstractions/ IMemoryStore

IToolIChannelAdapter、沙箱与钩子、自动化与画像存储等 端口接口
Sessions/ SessionManager

:活跃会话、超时、锁、与 IMemoryStore 协同持久化
Memory/ FileMemoryStore

SqliteMemoryStore 等默认存储实现(不含 MemPalace 插件)
Pipeline/ MessagePipeline

(入站/出站 Channel)、ToolApprovalServiceCronScheduler、命令处理器等
Middleware/ IMessageMiddleware

、限流、Token 预算等与消息路径相关的中间件契约与实现
Plugins/ PluginDiscovery

PluginModelsPluginConfigValidatorPluginCapabilityPolicy
Skills/ SkillLoader

SkillPromptBuilder、技能模型与巡检
Security/ SecretResolver

、白名单、URL 安全、脱敏、配对、绑定地址分类等
Validation/ ConfigValidator

ProviderSmokeProbe、医生与 setup 验证、Ollama 端点规范化等
Observability/ Telemetry

RuntimeMetricsTurnContext、用量与审计辅助类型
Setup/
本地预设、配置路径、setup 产物与升级快照等引导逻辑
Http/ HttpClientFactory

 等共享 HTTP 构造
Features/ / Contacts/ / Canvas/
特性存储、联系人、Canvas/A2UI 校验等

三、配置:GatewayConfig 是一棵大树

几乎所有运行时行为都能追溯到 GatewayConfig(见 Models/GatewayConfig.cs):绑定地址与端口、RuntimeMode / Orchestrator)、LlmMemory(Provider、Sqlite、Mempalace 子块)、PluginsSkillsSecurityChannelsToolingSandboxDelegation……

网关启动时的 ConfigValidator 对这棵树做一致性校验;Core 还提供 GatewayConfigFileConfigPathResolver 等,支持外部 JSON 与环境变量合并。理解 Core 的第一步往往是:你要改的行为落在哪个配置节点下。


四、会话与内存:端口与实现分离

IMemoryStore 定义会话与持久化笔记、分支等的最小契约:

public interface IMemoryStore{    ValueTask<Session?> GetSessionAsync(string sessionId, CancellationToken ct);    ValueTask SaveSessionAsync(Session session, CancellationToken ct);    ValueTask<string?> LoadNoteAsync(string key, CancellationToken ct);    ValueTask SaveNoteAsync(string key, string content, CancellationToken ct);    ValueTask DeleteNoteAsync(string key, CancellationToken ct);    ValueTask<IReadOnlyList<string>> ListNotesWithPrefixAsync(string prefix, CancellationToken ct);    // ── Conversation Branching ─────────────────────────────────────────    ValueTask SaveBranchAsync(SessionBranch branch, CancellationToken ct);    ValueTask<SessionBranch?> LoadBranchAsync(string branchId, CancellationToken ct);    ValueTask<IReadOnlyList<SessionBranch>> ListBranchesAsync(string sessionId, CancellationToken ct);    ValueTask DeleteBranchAsync(string branchId, CancellationToken ct);}

扩展接口(如 IMemoryNoteSearchISessionAdminStore)由具体存储实现;Gateway 在注册 DI 时根据 Memory.Provider 选择 文件 / Sqlite / 动态插件(如 MemPalace)

SessionManager 在内存中维护 活跃会话字典每会话信号量、准入与超时扫描,并把负载落到 IMemoryStore

/// <summary>/// Manages active sessions with automatic expiry. Thread-safe, allocation-light./// </summary>public sealed class SessionManager : IAsyncDisposable, IDisposable{    private readonly ConcurrentDictionary<string, Session> _active = new();

会话键约定 channelId:senderId 或显式 sessionId(文档与源码注释中有说明),渠道与 Web UI 共用同一套会话语义。


五、消息管道:MessagePipeline

MessagePipeline 使用 System.Threading.Channels 实现有界入站/出站队列,用于在高负载下 背压、避免无界堆积:

/// <summary>/// High-throughput, zero-allocation message pipeline using System.Threading.Channels./// Bounded channel applies backpressure to prevent OOM under load./// </summary>public sealed class MessagePipeline : IAsyncDisposable

Gateway 侧的 Worker 从 Reader 取 InboundMessage,再交给 MiddlewarePipeline 与 Agent;出站同理。Core 定义消息信封(如 Models/Messages.cs),保证 Host 与 Channels 解耦。


六、插件与安全策略

  • Plugins/
    :发现、manifest、配置校验、AOT 下能力封锁策略(与 PluginCapabilityPolicyGatewayRuntimeMode 配合),详见系列第一篇、第二篇。
  • Security/
    :密钥解析、URL 拉取安全、工具路径策略、配对与 Ed25519 等——与 SECURITY.md 中的运维建议一一对应。
  • Validation/
    :启动前配置校验、模型 doctor、provider smoke,减少 “服务起来了但运行时才炸” 的概率。

七、技能与兼容性

Skills/ 负责加载 SKILL.md、拼装注入提示词;Compatibility/ 嵌入 public-smoke.json 等兼容矩阵数据,供 CLI/Admin 展示。它们把「生态兼容」从 Gateway 里剥离成 可版本化的数据 + 纯逻辑


八、与前几篇的衔接

篇目
与 Core 的关系
第一篇
总览里 Core =「共享模型与基础设施」
第二篇
MemPalace 作为 IMemoryStore 的一种实现,通过插件与 Gateway DI 接入;契约仍在 Core
第三篇
Gateway AddOpenClawCoreServices 等扩展方法注册的就是 Core 中的管理器与默认实现
本篇
把 Core 内部再拆成 模型 / 抽象 / 管道 / 校验 / 安全 几块阅读

延伸阅读(仓库内)

路径
建议
src/OpenClaw.Core/Models/GatewayConfig.cs
配置全貌入口
src/OpenClaw.Core/Validation/ConfigValidator.cs
校验规则清单
src/OpenClaw.Core/Sessions/SessionManager.cs
会话生命周期
docs/GETTING_STARTED.md
与 Gateway/Agent 并读的仓库地图

版权声明:基于开源仓库 OpenClaw.NET 整理,仅供技术交流。