Files
ai-agent-scaffold-go/docs/mermaid/Agent执行引擎位置与启动装配.md
huanghaosheng 407e19124f docs: 收录执行引擎图与项目完善报告
此前的 .gitignore 忽略了 docs/,这批文档一直没有入库。收录四篇执行引擎流程图与 71 条缺陷的完善报告,作为后续升级的现状基线。
2026-11-02 10:44:12 +08:00

60 lines
2.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Agent 执行引擎位置与启动装配
> 源码:
`internal/app/bootstrap/bootstrap.go`
`internal/infrastructure/adk/adapter.go`
Agent 执行引擎是“HTTP 请求进来后,真正驱动大模型和工具执行”的核心模块。它向上承接 ChatService,向下连接 OpenAI 客户端与 MCP 工具系统;项目启动时通过 Armory 装配链把它创建并注册到内存 Registry。
## 整体调用链图
```mermaid
flowchart LR
HTTP["trigger/http/agent_handler.go"] --> Chat["domain/agent/service/chat/service.go"]
Chat --> Runner["model.Runner"]
Runner --> ADK["adk.Runner"]
ADK --> Agent["adk.Agent"]
Agent --> ChatModel["ports.ChatModel"]
ChatModel --> Eino["ai/EinoChatModel"]
Eino --> OpenAI["ai/OpenAIClient"]
Agent --> ToolRouter["ports.ToolRouter"]
ToolRouter --> MCP["ai/MCPToolRouter/MCPSSEClient"]
```
- `HTTP`:Gin HTTP Handler,接收 `/chat` 与 `/chat_stream` 请求。
- `Chat`:ChatService,负责会话管理和消息路由。
- `Runner`:领域层定义的 Runner 接口。
- `ADK`:`adk.Runner`,负责生命周期管理。
- `Agent`:`adk.Agent`,负责 LLM 调用、工具调用和工作流编排。
- `ChatModel`:实际由 `EinoChatModel` 实现,内部使用 `OpenAIClient`。
- `ToolRouter`:实际由 `MCPToolRouter` 实现,内部使用 `MCPSSEClient`。
因此,这个文件是整条链路中的“调度核心”,而不是单纯的 HTTP 客户端或模型客户端。
---
## 启动装配时序图
```mermaid
sequenceDiagram
participant B as bootstrap.Build
participant F as adk.Factory
participant Armory as Armory 装配链
participant R as RunnerRegistry
B->>F: NewFactory()
B->>F: UseToolRouter(MCPToolRouter)
B->>Armory: AcceptArmoryAgents()
Armory->>F: NewLLMAgent(config, chatModel)
Armory->>F: NewSequentialAgent(config, subAgents)
Armory->>F: NewRunner(appName, entryAgent, plugins)
Armory->>R: Register(RegisteredAgent{Runner: runner})
```
- `bootstrap.Build` 是启动装配入口。
- `adk.Factory` 负责创建 Agent 与 Runner。
- Armory 装配链负责把 YAML 配置解析并调用 Factory 创建对象。
- `RunnerRegistry` 是 `ports.InMemoryAgentRegistry`,保存注册后的 Agent 与 Runner。
- 整个装配发生在启动阶段,运行时只从 Registry 中取 Runner 执行。