docs: 收录执行引擎图与项目完善报告
此前的 .gitignore 忽略了 docs/,这批文档一直没有入库。收录四篇执行引擎流程图与 71 条缺陷的完善报告,作为后续升级的现状基线。
This commit is contained in:
143
docs/mermaid/Agent执行引擎运行时流程.md
Normal file
143
docs/mermaid/Agent执行引擎运行时流程.md
Normal file
@@ -0,0 +1,143 @@
|
||||
# Agent 执行引擎运行时流程
|
||||
|
||||
> 源码:
|
||||
`internal/trigger/http/agent_handler.go`
|
||||
`internal/domain/agent/service/chat/service.go`
|
||||
`internal/infrastructure/adk/adapter.go`
|
||||
|
||||
运行时流程包含两部分:一是单个 LLM Agent 内部的 Tool-Calling Loop,二是一次聊天请求从 HTTP 到 Runner、Agent、模型、工具的整体时序。
|
||||
|
||||
## Tool-Calling Loop 流程图
|
||||
|
||||
`runLLM` 实现了大模型“思考 → 调用工具 → 看到结果 → 再思考”的循环,最多迭代 4 轮。
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
Start["初始 messages: system + user"] --> Gen["ChatModel.Generate"]
|
||||
Gen --> Check{"有 ToolCalls?"}
|
||||
Check -->|"无"| Return["返回 reply.Content"]
|
||||
Check -->|"有"| AppendAssistant["追加 assistant tool_calls"]
|
||||
AppendAssistant --> Exec["executeToolCalls"]
|
||||
Exec --> AppendTool["追加 tool 结果"]
|
||||
AppendTool --> Loop{"iter < 4?"}
|
||||
Loop -->|"是"| Gen
|
||||
Loop -->|"否"| Error["超过最大迭代次数"]
|
||||
```
|
||||
|
||||
### 代码对应
|
||||
|
||||
```go
|
||||
func (a *Agent) runLLM(ctx context.Context, content model.ChatContent, vars map[string]string) (string, error) {
|
||||
messages := initialMessages(applyVars(a.instruction, vars), firstText(content))
|
||||
for iter := 0; iter < maxToolCallIterations; iter++ {
|
||||
reply, err := a.chatModel.Generate(ctx, messages)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(reply.ToolCalls) == 0 {
|
||||
return reply.Content, nil
|
||||
}
|
||||
messages = append(messages, ports.ChatMessage{Role: ports.ChatRoleAssistant, Content: reply.Content, ToolCalls: reply.ToolCalls})
|
||||
toolMessages, err := a.executeToolCalls(ctx, reply.ToolCalls)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
messages = append(messages, toolMessages...)
|
||||
}
|
||||
return "", fmt.Errorf("agent %q exceeded tool-call iteration limit %d", a.name, maxToolCallIterations)
|
||||
}
|
||||
```
|
||||
|
||||
### 关键步骤
|
||||
|
||||
1. 构建初始消息:`system` + `user`。
|
||||
2. 调用 `ChatModel.Generate`。
|
||||
3. 如果没有工具调用,直接返回文本结果。
|
||||
4. 如果有工具调用,把 assistant 消息加入历史。
|
||||
5. 执行工具调用并追加 `tool` 角色消息。
|
||||
6. 回到第 2 步,最多 4 轮。
|
||||
|
||||
---
|
||||
|
||||
## 请求执行时序图
|
||||
|
||||
一次普通聊天请求从 HTTP 进入,经过 ChatService 找到 Runner,再由 Agent 调用大模型和工具,最终返回结果。
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant H as HTTP Handler
|
||||
participant C as ChatService
|
||||
participant R as adk.Runner
|
||||
participant A as adk.Agent
|
||||
participant CM as EinoChatModel/OpenAIClient
|
||||
participant TR as MCPToolRouter/MCPSSEClient
|
||||
|
||||
H->>C: HandleMessage()
|
||||
C->>R: Runner.Run()
|
||||
R->>R: notifyPlugins()
|
||||
R->>A: agent.run()
|
||||
A->>A: runWithVars()
|
||||
A->>CM: ChatModel.Generate()
|
||||
CM-->>A: ChatReply{ToolCalls}
|
||||
A->>TR: ToolRouter.CallTool()
|
||||
TR-->>A: tool result
|
||||
A->>CM: Generate(messages + tool result)
|
||||
CM-->>A: final content
|
||||
A-->>R: output
|
||||
R-->>C: []string{output}
|
||||
C-->>H: JSON response
|
||||
```
|
||||
|
||||
| 参与者 | 实际类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `HTTP Handler` | `internal/trigger/http/agent_handler.go` | 接收 `/chat` 请求 |
|
||||
| `ChatService` | `internal/domain/agent/service/chat/service.go` | 会话路由 |
|
||||
| `adk.Runner` | `internal/infrastructure/adk/adapter.go` | 生命周期管理、插件通知 |
|
||||
| `adk.Agent` | `internal/infrastructure/adk/adapter.go` | 执行核心 |
|
||||
| `EinoChatModel/OpenAIClient` | `internal/infrastructure/ai/*` | 大模型调用 |
|
||||
| `MCPToolRouter/MCPSSEClient` | `internal/infrastructure/ai/*` | 工具调用 |
|
||||
|
||||
> 图中展示的是包含一轮工具调用的场景。实际运行时可能根据 Tool-Calling Loop 进行多轮工具调用,直到模型返回最终文本。
|
||||
|
||||
|
||||
---
|
||||
|
||||
## Runner 视角的完整链路(含 Registry)
|
||||
|
||||
前面的时序图侧重“Agent 内部如何调模型和工具”。下面这张图把 `AgentRegistry` 的查找过程显式画出来,更侧重 Runner 在会话链路中的位置。
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant Client
|
||||
participant Handler as HTTP Handler
|
||||
participant Chat as ChatService
|
||||
participant Registry as AgentRegistry
|
||||
participant Runner as adk.Runner
|
||||
participant Agent as adk.Agent
|
||||
participant Model as ChatModel
|
||||
participant Tool as ToolRouter
|
||||
|
||||
Client->>Handler: POST /api/v1/chat
|
||||
Handler->>Chat: HandleMessage(agentID, userID, sessionID, message)
|
||||
Chat->>Registry: Get(agentID)
|
||||
Registry-->>Chat: RegisteredAgent{Runner}
|
||||
Chat->>Runner: Run(userID, sessionID, content)
|
||||
Runner->>Runner: notifyPlugins()
|
||||
Runner->>Agent: run(content)
|
||||
Agent->>Model: Generate(messages)
|
||||
Model-->>Agent: ToolCalls
|
||||
Agent->>Tool: CallTool(...)
|
||||
Tool-->>Agent: result
|
||||
Agent->>Model: Generate(messages + tool result)
|
||||
Model-->>Agent: final content
|
||||
Agent-->>Runner: output
|
||||
Runner-->>Chat: []string{output}
|
||||
Chat-->>Handler: outputs
|
||||
Handler-->>Client: JSON response
|
||||
```
|
||||
|
||||
关键点:
|
||||
|
||||
- `ChatService` 不直接依赖 `adk.Runner`,只依赖 `model.Runner` 接口。
|
||||
- `Runner` 不关心模型和工具的协议细节,只负责会话与生命周期。
|
||||
- 入口 Agent 通常是一个 workflow Agent,内部再编排多个子 Agent。
|
||||
Reference in New Issue
Block a user