142 lines
4.7 KiB
Markdown
142 lines
4.7 KiB
Markdown
# Agent 执行引擎结构与策略分发
|
||
|
||
> 源码:
|
||
`internal/infrastructure/adk/adapter.go`
|
||
|
||
|
||
`adk` 包内部由 `Factory`、`Agent`、`Runner` 三层组成:Factory 负责创建对象,Agent 负责按 kind 执行,Runner 负责生命周期管理。
|
||
|
||
## 三层结构图
|
||
|
||
```mermaid
|
||
flowchart TD
|
||
subgraph Factory["adk.Factory(创建层)"]
|
||
F["sessionCounter / plugins / router"]
|
||
F -->|NewLLMAgent| LA["Agent{kind:llm}"]
|
||
F -->|NewSequentialAgent| SA["Agent{kind:sequential}"]
|
||
F -->|NewParallelAgent| PA["Agent{kind:parallel}"]
|
||
F -->|NewLoopAgent| LO["Agent{kind:loop}"]
|
||
F -->|NewRunner| R["Runner"]
|
||
end
|
||
|
||
subgraph Agent["adk.Agent(执行层)"]
|
||
LA -->|runWithVars| LLM["runLLM"]
|
||
SA -->|runWithVars| SEQ["runSequential"]
|
||
PA -->|runWithVars| FAN["runFanOut"]
|
||
LO -->|runWithVars| FAN
|
||
end
|
||
|
||
subgraph Runner["adk.Runner(生命周期层)"]
|
||
R -->|Run| RUN["agent.run"]
|
||
R -->|Stream| STREAM["agent.stream"]
|
||
R -->|notifyPlugins| PLUGIN["RunnerPlugin"]
|
||
end
|
||
```
|
||
|
||
|
||
| 层 | 核心类型 | 职责 |
|
||
|---|---|---|
|
||
| 创建层 | `Factory` | 创建 Agent / Runner,注入 ToolRouter |
|
||
| 执行层 | `Agent` | 根据 `kind` 选择 `runLLM` / `runSequential` / `runFanOut` |
|
||
| 生命周期层 | `Runner` | 插件通知、会话 ID 生成、普通/流式执行入口 |
|
||
|
||
设计意图:创建与执行分离,执行与生命周期分离,依赖通过外部注入。
|
||
|
||
---
|
||
|
||
## 执行策略分发图
|
||
|
||
`runWithVars` 是 Agent 执行的分发中心。
|
||
|
||
```mermaid
|
||
flowchart TD
|
||
Run["Agent.run"] --> RW["Agent.runWithVars"]
|
||
RW --> Switch{"kind?"}
|
||
Switch -->|"llm"| RunLLM["runLLM"]
|
||
Switch -->|"sequential"| RunSeq["runSequential"]
|
||
Switch -->|"loop / parallel"| RunFan["runFanOut"]
|
||
```
|
||
|
||
### 代码对应
|
||
|
||
```go
|
||
func (a *Agent) runWithVars(ctx context.Context, content model.ChatContent, vars map[string]string) (string, error) {
|
||
switch a.kind {
|
||
case "llm":
|
||
return a.runLLM(ctx, content, vars)
|
||
case "sequential":
|
||
return a.runSequential(ctx, content, vars)
|
||
case "loop", "parallel":
|
||
return a.runFanOut(ctx, content, vars)
|
||
default:
|
||
return "", fmt.Errorf("agent %q has unknown kind %q", a.name, a.kind)
|
||
}
|
||
}
|
||
```
|
||
|
||
| kind | 执行方法 | 说明 |
|
||
|---|---|---|
|
||
| `llm` | `runLLM` | 普通大模型对话 + Tool-Calling Loop |
|
||
| `sequential` | `runSequential` | 按顺序执行子 Agent,并通过 outputKey 传值 |
|
||
| `loop` | `runFanOut` | 当前实现按 fan-out 聚合子 Agent 输出 |
|
||
| `parallel` | `runFanOut` | 当前实现按 fan-out 聚合子 Agent 输出 |
|
||
|
||
> 注意:当前 `loop` 与 `parallel` 都走 `runFanOut`,并不是真正的并发执行。
|
||
|
||
|
||
---
|
||
|
||
## 为什么 AgentFactory 和 RunnerFactory 是同一个 Factory
|
||
|
||
`bootstrap.Build()` 中只创建了一个 `*adk.Factory`,同时把它作为 `ports.AgentFactory` 和 `ports.RunnerFactory` 传入 Armory:
|
||
|
||
```go
|
||
agentFactory := adk.NewFactory()
|
||
agentFactory.UseToolRouter(toolRouter)
|
||
runnerFactory := agentFactory
|
||
```
|
||
|
||
调用方通过窄接口依赖 Factory,但底层共享同一个会话计数器、工具路由器和插件注册表。
|
||
|
||
```mermaid
|
||
flowchart LR
|
||
B["bootstrap.Build"] -->|"agentFactory"| F["*adk.Factory"]
|
||
B -->|"runnerFactory"| F
|
||
F -->|"实现"| AF["ports.AgentFactory"]
|
||
F -->|"实现"| RF["ports.RunnerFactory"]
|
||
AN["Armory.AgentNode / WorkflowNode"] --> AF
|
||
RN["Armory.RunnerNode"] --> RF
|
||
```
|
||
|
||
好处:
|
||
|
||
1. `sessionCounter` 全局唯一,多个 Runner 不会生成重复会话 ID。
|
||
2. `router` 只注入一次,所有 Agent 共享同一个工具路由器。
|
||
3. Runner 可以对同一个 Factory 产出的 `*Agent` 做类型断言和编排。
|
||
4. 外部按需依赖 `ports.AgentFactory` 或 `ports.RunnerFactory`,不依赖具体类型。
|
||
|
||
---
|
||
|
||
## Sequential 变量作用域链
|
||
|
||
Sequential 工作流通过 `output-key` 把上一步的完整输出写入 `scope`,后续 Agent 的 instruction 在 `applyVars` 时替换 `{key}`。
|
||
|
||
```mermaid
|
||
flowchart LR
|
||
R["Agent.run<br/>vars = {}"] --> RW["runWithVars"]
|
||
RW --> S["runSequential<br/>scope := cloneVars(vars)"]
|
||
S --> S1["sub1.runWithVars(scope)"]
|
||
S1 -->|"outputKey: analysis_result"| Scope["scope"]
|
||
Scope -->|"applyVars(instruction, scope)"| S2["sub2.runWithVars(scope)"]
|
||
S2 -->|"outputKey: draft_diagram"| Scope
|
||
Scope -->|"applyVars(instruction, scope)"| S3["sub3.runWithVars(scope)"]
|
||
S3 -->|"最终输出"| Out["返回 last"]
|
||
```
|
||
|
||
要点:
|
||
|
||
- `run` 是无变量入口,内部传入 `map[string]string{}` 作为初始作用域。
|
||
- `runWithVars` 才是带变量作用域的递归入口。
|
||
- Sequential 每次使用 `cloneVars` 复制作用域,避免影响外部调用方。
|
||
- `loop` / `parallel` 走 `runFanOut`,所有子 Agent 共用同一份变量,不会逐级累加 `output-key`。
|