Skip to content

Commit d6c5a0e

Browse files
committed
✨ feat(docs): add v2.1.2 config selection, stdlib, harness, ADT, pattern matching, error handling docs
- Add part2_config_stdlib.md: use config, stdlib/news.nx, strict/warn modes - Add part2_harness.md: Harness hexad (E/T/C/S/L/V) documentation - Add part2_actor.md: Actor Model (spawn/pass/await) documentation - Add part2_pattern_matching.md: 7 pattern types documentation - Add part2_adt.md: Struct/Enum/Trait/Impl documentation - Add part2_error_handling.md: ? operator, otherwise, defer documentation - Update mkdocs.yml navigation with new pages - Update quickstart.md version to v2.1.2
1 parent cc24a94 commit d6c5a0e

8 files changed

Lines changed: 2124 additions & 1 deletion

docs/part2_actor.md

Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
---
2+
comments: true
3+
---
4+
5+
# Actor Model:多 Agent 并发编排
6+
7+
Nexa v2.0 引入了基于 **Actor Model** 的多 Agent 并发编排系统,让多个 Agent 能够独立运行、通过消息传递协作,避免共享状态带来的竞态条件。
8+
9+
---
10+
11+
## 📋 Actor Model 核心概念
12+
13+
| 概念 | 说明 |
14+
|------|------|
15+
| **Actor** | 独立的执行单元,拥有自己的状态和邮箱 |
16+
| **Mailbox** | 消息队列,Actor 通过邮箱接收消息 |
17+
| **Message** | Actor 之间传递的数据 |
18+
| **Handle** | Actor 的引用,用于发送消息和等待结果 |
19+
20+
---
21+
22+
## 🚀 `spawn` — 创建子 Agent Actor
23+
24+
`spawn` 创建一个新的 Actor,在独立线程中运行。
25+
26+
### 语法
27+
28+
```nexa
29+
handle = spawn AgentName("初始消息");
30+
handle = spawn AgentName(args);
31+
```
32+
33+
### 示例
34+
35+
```nexa
36+
agent Analyzer {
37+
prompt: "分析数据并返回结果"
38+
}
39+
40+
flow main {
41+
// 创建三个并行 Actor
42+
a1 = spawn Analyzer("分析数据集 A");
43+
a2 = spawn Analyzer("分析数据集 B");
44+
a3 = spawn Analyzer("分析数据集 C");
45+
46+
// 等待所有结果
47+
r1 = await a1;
48+
r2 = await a2;
49+
r3 = await a3;
50+
51+
print("All analysis completed");
52+
}
53+
```
54+
55+
### 运行时行为
56+
57+
1. **ActorSystem** 创建 ActorHandle
58+
2. 启动独立线程运行 Agent
59+
3. 返回 Handle 供后续操作
60+
61+
---
62+
63+
## 📨 `pass` — 异步消息发送
64+
65+
`pass` 向 Actor 发送消息,不阻塞当前执行。
66+
67+
### 语法
68+
69+
```nexa
70+
pass handle, message;
71+
pass handle, "消息内容";
72+
```
73+
74+
### 示例
75+
76+
```nexa
77+
agent Worker {
78+
prompt: "处理任务"
79+
}
80+
81+
flow main {
82+
worker = spawn Worker("ready");
83+
84+
// 异步发送多个任务
85+
pass worker, "任务 1";
86+
pass worker, "任务 2";
87+
pass worker, "任务 3";
88+
89+
// 等待最终结果
90+
result = await worker;
91+
}
92+
```
93+
94+
---
95+
96+
## `await` — 同步等待结果
97+
98+
`await` 阻塞等待 Actor 完成并获取结果。
99+
100+
### 语法
101+
102+
```nexa
103+
result = await handle;
104+
result = await handle, timeout; // 带超时(秒)
105+
```
106+
107+
### 示例
108+
109+
```nexa
110+
flow main {
111+
worker = spawn LongRunningTask("start");
112+
113+
// 等待最多 30 秒
114+
result = await worker, 30;
115+
116+
if result == timeout {
117+
print("Task timed out");
118+
} else {
119+
print("Result: " + result);
120+
}
121+
}
122+
```
123+
124+
---
125+
126+
## 📥 `receive` — Actor 内部消息接收
127+
128+
在 Actor 内部从邮箱接收消息。
129+
130+
### 语法
131+
132+
```nexa
133+
msg = receive();
134+
msg = receive(timeout); // 带超时
135+
```
136+
137+
### 示例
138+
139+
```nexa
140+
agent MessageProcessor {
141+
prompt: "处理接收到的消息"
142+
}
143+
144+
flow main {
145+
processor = spawn MessageProcessor("start");
146+
147+
// 发送消息
148+
pass processor, "Hello";
149+
pass processor, "World";
150+
151+
// Actor 内部会依次 receive 并处理
152+
result = await processor;
153+
}
154+
```
155+
156+
---
157+
158+
## 🎯 DAG 管道与 Actor 结合
159+
160+
Actor 可以与 DAG 操作符 `|>>` 结合,实现复杂的并发编排。
161+
162+
### 示例:Fan-out / Fan-in
163+
164+
```nexa
165+
agent Researcher {
166+
prompt: "研究主题"
167+
}
168+
169+
agent Synthesizer {
170+
prompt: "综合多个研究结果"
171+
}
172+
173+
flow main {
174+
topic = "量子计算应用";
175+
176+
// Fan-out: 并行启动多个研究 Actor
177+
researchers = topic |>> [
178+
spawn Researcher("角度 A"),
179+
spawn Researcher("角度 B"),
180+
spawn Researcher("角度 C")
181+
];
182+
183+
// Fan-in: 等待所有结果并综合
184+
final = researchers &>> Synthesizer;
185+
186+
print(final);
187+
}
188+
```
189+
190+
---
191+
192+
## 🔄 Actor 生命周期
193+
194+
```
195+
spawn
196+
197+
198+
┌─────────────┐
199+
│ Running │ ◄── pass (接收消息)
200+
└─────────────┘
201+
202+
├── 完成 ──► Completed
203+
204+
├── 错误 ──► Failed
205+
206+
└── 超时 ──► Timeout
207+
```
208+
209+
### 状态说明
210+
211+
| 状态 | 说明 |
212+
|------|------|
213+
| `running` | Actor 正在运行 |
214+
| `completed` | Actor 正常完成 |
215+
| `failed` | Actor 发生错误 |
216+
| `timeout` | Actor 执行超时 |
217+
218+
---
219+
220+
## 📊 最佳实践
221+
222+
### 1. 合理设置超时
223+
224+
```nexa
225+
// 推荐:为长时间任务设置超时
226+
result = await worker, 300; // 5 分钟超时
227+
228+
// 避免:无限等待
229+
result = await worker; // ❌ 可能永久阻塞
230+
```
231+
232+
### 2. 错误处理
233+
234+
```nexa
235+
flow main {
236+
worker = spawn RiskyTask("start");
237+
238+
try_agent {
239+
result = await worker;
240+
} catch_correction(e: ActorError) {
241+
reflect "Actor 失败:#{e},启动备用 Actor";
242+
backup = spawn BackupTask("start");
243+
result = await backup;
244+
}
245+
}
246+
```
247+
248+
### 3. 资源管理
249+
250+
```nexa
251+
// 推荐:限制并发 Actor 数量
252+
max_concurrent = 10;
253+
actors = [];
254+
255+
for each task in task_list {
256+
if actors.length >= max_concurrent {
257+
// 等待一个完成
258+
completed = await actors[0];
259+
actors = actors[1:];
260+
}
261+
actors.append(spawn Worker(task));
262+
}
263+
```
264+
265+
---
266+
267+
## 🆚 Actor vs DAG 操作符
268+
269+
| 特性 | Actor (`spawn/await`) | DAG (`|>>/&>>`) |
270+
|------|----------------------|-----------------|
271+
| 执行模型 | 独立线程 | 协程/异步 |
272+
| 状态隔离 | 完全隔离 | 共享上下文 |
273+
| 消息传递 | 显式 `pass` | 隐式管道 |
274+
| 适用场景 | 长时间任务、需要隔离 | 快速流水线、数据流 |
275+
276+
### 选择建议
277+
278+
- **使用 Actor**:任务需要长时间运行、需要状态隔离、需要动态消息传递
279+
- **使用 DAG**:任务快速完成、数据流清晰、需要简洁语法
280+
281+
---
282+
283+
## 📚 相关文档
284+
285+
- [Harness 六元组](part2_harness.md) — E/T/C/S/L/V 运行时原语
286+
- [高级特性](part2_advanced.md) — 管道、DAG、意图路由
287+
- [基础语法](part1_basic.md) — Agent、Flow、Protocol 基础

0 commit comments

Comments
 (0)