第 8 讲:Agent 接口与注册表

DeepSeek Harness 源码解析 · 第 8 讲

DeepSeek Harness 源码解析系列 第 8 讲:Agent 接口与注册表 基于 DeepSeek Harness 源码 · 2026-08-23 一、本节概述 前几讲我们了解了 Harness 的整体架构、事件系统、Turn flow 和依赖注入。本节聚焦 Agent 接口 与 AgentRegistry 注册表 —— 这是整个 Agent 运行时的心脏。每个插件(UI、Hooks、编排器)都通过这个接口编程,而注册表负责管理所有活跃 Agent 的生命周期。 源码位置: packages/core/agent/ ,核心文件 index.ts (706 行)定义了 AgentRegistry 服务类, runtime-types.ts (292 行)定义了 Agent 公开接口和全部 agent/* 事件词汇表。 二、Agent 接口:零循环依赖的抽象 Agent 接口是整个框架最关键的设计之一。它定义了 Agent 的"外观",但不包含任何具体的循环实现 —— 这意味着 循环是可替换的 。所有插件只依赖这个接口,不依赖具体的 dsh-agent-loop 包。 📄 packages/core/agent/src/runtime-types.ts(第 64-144 行) /** Public live-agent handle. */ export interface Agent { /** The single identity shared with {@link session}. */ readonly id: SessionId // ↑ Agent 的唯一标识,与 Session 共用同一个 ID,保证 Agent 和 Session 一一对应 /** The provider route and model this agent's requests use. */ readonly options: AgentOptions // ↑ 包含 provider(供应商路由)、model(模型 ID)、maxTokens(最大输出 token 数) /** The live session this agent drives; its log is the durable source of truth. */ readonly session: Session // ↑ Agent 驱动的 Session 对象,Session 的事件日志是唯一的持久化事实来源 /** The agent-owned projection of durable pending work. */ readonly inbox: Inbox // ↑ 邮箱系统:投影持久化的待处理消息,分为 next-turn 和 next-step 两个队列 /** The current lifecycle state, mirrored on every `agent/status` transition. */ readonly status: AgentStatus // ↑ 生命周期状态:'idle'(空闲)或 'running'(运行中),每次状态切换都会触发事件 /** Agent-scoped context; its contributions are agent-local, unwind on disposal. */ readonly ctx: Context // ↑ Agent 作用域上下文:在此注册的工具/提示/监听器只属于这个 Agent,销毁时自动清理 /** Clear queued and steering work — unless `keepInbox` — and abort the active turn. */ cancel(cause: AgentCancelCause, options?: CancelOptions): void // ↑ 取消当前操作:默认清空邮箱,keepInbox=true 时保留待处理消息只中止当前 turn /** Resolve after the current whole-agent activity reaches quiescence. */ whenIdl…

阅读全文:DeepSeek Harness 源码解析 第 8 讲