/** * agentRuntime —— 防腐层组合根(core 内核)。 * * 把向下端口 {@link IAgentLoop} 与三个向上端口({@link ISessionManager} / * {@link IMcpServerProvider} / {@link ISkillLoader})装配成一个 {@link AgentRuntime}, * 收口「请求 → 解析上行资源 → 组 IAgentLoopRequest → agent loop」,产出 env 无关的 * {@link AgentStreamEvent} 流。**SSE 协议桥接归下行适配器 impl/agui,内核不产 SSE** * ——从而消除原 core↔conversation 循环。 * * 内聚:{@link AgentRuntime} class 持有已装配的端口,`new AgentRuntime(ports?)` 是 * 唯一构造口(替代散落的 `createAgentRuntime` 工厂函数);{@link IAgentRuntime} 是它 * 的接口形态,便于注入/单测按接口断言。 * * 默认装配(`new AgentRuntime()` 不传参;缺省端口各自 `new` 一份,不导出模块级单例, * 避免在打包器(webpack/vite)环境下的共享状态副作用——调用方按需 `new` 或注入): * - IAgentLoop = {@link PiAgentLoop}(impl/agent/pi,pi-agent-core) * - ISessionManager = {@link InMemorySessionManager}(进程内内存态) * - ISkillLoader = {@link NoopSkillLoader}(空) * - IMcpServerProvider = {@link NoopMcpServerProvider}(无 server,零网络) * * 编排要点: * - **messages 不拍扁**:把请求 messages 转成 env 无关 {@link IAgentTurn}[],按需回灌 * `sessionManager.load` 的历史 → 组成完整 `messages` 交给下行端口。拍扁成字符串是 * pi 适配器内部的事(opencode / google-adk 等直接吃 messages)。 * - 上行端口按需调用(sessionId/skillResources/mcpResources 缺省不触发)。 * - assistant 文本边消费边累积(pass-through 记录 delta),流终结后 sessionManager.append * 助手轮(默认 no-op)。 * - MCP server 流结束后统一 close();单 server listTools 失败不阻断 run。 * * 分层:本文件是 core 中唯一 import impl 的文件——装配 impl/agent/pi 的 piAgentLoop * 作默认下行适配器(「随包发 pi 默认」有意为之);端口契约层(ports/types)零 impl 依赖; * agentProtocol 类型来自协议契约,发布构建会将契约声明 vendored 到 runtime 包内。 */ import type { IAgentAuth, AgentStreamEvent } from './types.js'; import { type IAgentProtocol } from '../vendor/linkque-agent-protocol/index.js'; import type { A2UICardRuntimeContext } from '../vendor/agent-a2ui-card-contract/index.js'; import type { IAgentLoop, IAgentRunRequest, IMcpServerProvider, ISessionManager, ISkillLoader, ThinkingLevel } from './ports.js'; /** 组合根可选端口装配;任一缺省走默认适配器。 */ export interface IAgentRuntimePorts { /** 向下端口:agent loop。默认 {@link piAgentLoop}。 */ agentLoop?: IAgentLoop; /** 向上端口:会话历史。默认 {@link inMemorySessionManager}(进程内内存态)。 */ sessionManager?: ISessionManager; /** 向上端口:技能加载。默认 {@link noopSkillLoader}(空)。 */ skillLoader?: ISkillLoader; /** 向上端口:MCP server 供给。默认 {@link noopMcpServerProvider}(空,零网络)。 */ mcpServerProvider?: IMcpServerProvider; } /** * runAgentLoop 的逐 run 可选项(只含「每次调用可能变」的运行态参数)。 * * 编译期可固定的(systemPrompt/model/skill/mcp resources/callParams.thinkingLevel 默认) * 已在 {@link AgentRuntime.create} 时从 protocol 提取固化进实例,不再逐 run 传。 */ export interface IAgentRuntimeRunOptions { /** 会话标识;提供时才触发 sessionManager 的 load/append(通常 = threadId)。 */ sessionId?: string; /** 工作目录(运行态);覆盖 create 默认。 */ cwd?: string; /** 主模型 id(运行态);覆盖 create 默认(protocol.agent.model.defaultModel)。 */ model?: string; /** 思考强度(运行态);覆盖 protocol.callParams.thinkingLevel。 */ thinkingLevel?: ThinkingLevel; /** 取消信号(运行态);透传给下行 agent loop,触发后中止本次 run。 */ signal?: AbortSignal; /** Gateway/host 注入的可信运行上下文;不会从模型或客户端字段名推断。 */ runtimeContext?: A2UICardRuntimeContext; /** * 诊断日志下沉点(运行态);透传至下行 agent loop,逐事件写一行。 * 缺省不写。agentdev worker 注入写 agent.log 的实现以排查事件流问题。 */ debugLog?: (line: string) => void; } /** * agent runtime 的接口形态:产出 AgentStreamEvent 流(SSE 桥接由 impl/agui 承担)。 * 命名按内部协议类型约定加 `I` 前缀;默认实现是 {@link AgentRuntime} class。 */ export interface IAgentRuntime { runAgentLoop(request: IAgentRunRequest, auth: IAgentAuth, options?: IAgentRuntimeRunOptions): AsyncIterable; } /** * AgentRuntime —— 防腐层组合根的默认实现(class 形态,内聚构造口与编排逻辑)。 * * 直接 `new AgentRuntime(ports?)` 构造;缺省端口走默认适配器。实例方法 * {@link AgentRuntime.runAgentLoop} 产出事件流。 */ export declare class AgentRuntime implements IAgentRuntime { /** 已装配的下行端口。 */ private readonly agentLoop; /** 已装配的会话历史端口。 */ private readonly sessionManager; /** 已装配的技能加载端口。 */ private readonly skillLoader; /** 已装配的 MCP server 供给端口。 */ private readonly mcpServerProvider; /** systemPrompt 默认(来自 protocol.agent.systemPrompt.content)。 */ private defaultSystemPrompt; /** model 默认(来自 protocol.agent.model.defaultModel)。 */ private defaultModel?; /** thinkingLevel 默认(来自 protocol.agent.model.callParams.thinkingLevel)。 */ private defaultThinkingLevel?; /** 技能资源(来自 protocol.agent.resources[SKILL])。 */ private skillResources; /** MCP server 规格(MCP + MCP_TOOL 资源按 serverName 聚合后)。 */ private mcpServerSpecs; /** CARD 资源(来自 protocol.agent.resources[CARD])。 */ private cardResources; /** * 裸构造(无 protocol):端口各自 new 一份(不共享单例,避免跨 runtime 状态污染 / * 打包器副作用)。protocol-derived 默认均为空。**生产路径用 {@link AgentRuntime.create}**; * 本 constructor 供单测/noop 调试或需要完全自定义默认的场景。 */ constructor(ports?: IAgentRuntimePorts); /** * 工厂:从 {@link IAgentProtocol} 提取 systemPrompt/model/callParams.thinkingLevel/ * skill+mcp resources 固化进实例,并装配端口(缺省默认)。 * * 这是 **protocol 与 runtime 关联的收口点**:调用方不再手工拆 protocol 喂 runAgentLoop。 * 凭据(`IAgentAuth`:provider+apiKey)不经此——protocol 不含凭据,仍由 * {@link AgentRuntime.runAgentLoop} 逐 run 从 env 解析传入。 * * @param protocol agent 协议声明(编译期事实) * @param ports 可选端口覆盖(任一缺省走默认适配器) */ static create(protocol: IAgentProtocol, ports?: IAgentRuntimePorts): AgentRuntime; /** 产出 AgentStreamEvent 流;内核编排「解析上行资源 → 组请求 → agent loop」。 */ runAgentLoop(request: IAgentRunRequest, auth: IAgentAuth, options?: IAgentRuntimeRunOptions): AsyncGenerator; }