-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1008 lines (816 loc) · 66.5 KB
/
Copy pathindex.html
File metadata and controls
1008 lines (816 loc) · 66.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="zh-CN" data-theme="light">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RemindAI Agent 工作流 — 论文与架构图表</title>
<style>
:root {
--bg: #ffffff; --bg2: #f8f9fa; --bg-sidebar: #1a1d23;
--text: #1a1d23; --text2: #5f6368; --text-s: #c4c7cc; --text-sa: #fff;
--border: #e0e0e0; --accent: #6366f1; --accent-l: #eef2ff;
--card-bg: #fff; --card-shadow: 0 1px 3px rgba(0,0,0,.08);
--code-bg: #f1f3f4; --bq-border: #6366f1; --bq-bg: #f8f9ff;
--table-border: #e8eaed; --table-stripe: #f8f9fa;
--hr: #e0e0e0; --header-grad: linear-gradient(135deg,#6366f1,#8b5cf6);
}
[data-theme="dark"] {
--bg: #1a1d23; --bg2: #22262d; --bg-sidebar: #111318;
--text: #e8eaed; --text2: #9aa0a6; --text-s: #9aa0a6; --text-sa: #fff;
--border: #3c4043; --accent: #818cf8; --accent-l: #1e1e3a;
--card-bg: #22262d; --card-shadow: 0 1px 3px rgba(0,0,0,.3);
--code-bg: #2d3139; --bq-border: #818cf8; --bq-bg: #1e1e3a;
--table-border: #3c4043; --table-stripe: #252830;
--hr: #3c4043; --header-grad: linear-gradient(135deg,#4f46e5,#7c3aed);
}
*{margin:0;padding:0;box-sizing:border-box}
html,body{height:100%;font-family:'Segoe UI',system-ui,-apple-system,sans-serif;background:var(--bg);color:var(--text)}
body{display:flex;overflow:hidden}
.sidebar{width:280px;min-width:280px;height:100vh;background:var(--bg-sidebar);color:var(--text-s);display:flex;flex-direction:column;overflow-y:auto;border-right:1px solid rgba(255,255,255,.06)}
.sidebar-header{padding:20px 20px 16px;background:var(--header-grad);color:#fff}
.sidebar-header h2{font-size:16px;font-weight:700}
.sidebar-header p{font-size:11px;opacity:.8;margin-top:4px}
.sidebar-section{padding:8px 0}
.sidebar-section-title{padding:8px 20px;font-size:10px;font-weight:700;text-transform:uppercase;letter-spacing:1.2px;color:rgba(255,255,255,.35)}
.sidebar-item{display:flex;align-items:center;gap:10px;padding:9px 20px;font-size:13px;cursor:pointer;transition:all .15s;border-left:3px solid transparent;color:var(--text-s);text-decoration:none}
.sidebar-item:hover{background:rgba(255,255,255,.06);color:var(--text-sa)}
.sidebar-item.active{background:rgba(99,102,241,.18);border-left-color:var(--accent);color:var(--text-sa);font-weight:600}
.sidebar-item .icon{font-size:15px;width:20px;text-align:center;flex-shrink:0}
.sidebar-item .badge{font-size:10px;padding:2px 7px;border-radius:10px;background:rgba(255,255,255,.1);margin-left:auto}
.main{flex:1;display:flex;flex-direction:column;height:100vh;overflow:hidden}
.toolbar{display:flex;align-items:center;justify-content:space-between;padding:10px 24px;background:var(--bg2);border-bottom:1px solid var(--border);flex-shrink:0}
.toolbar-title{font-size:14px;font-weight:600;color:var(--text)}
.toolbar-actions{display:flex;gap:8px;align-items:center}
.btn{padding:6px 14px;border-radius:6px;border:1px solid var(--border);background:var(--bg);color:var(--text);cursor:pointer;font-size:12px;font-weight:500;transition:all .15s}
.btn:hover{background:var(--bg2);border-color:var(--accent)}
.theme-toggle{font-size:17px;cursor:pointer;padding:4px 8px;border-radius:6px;background:none;border:1px solid var(--border)}
.content{flex:1;overflow-y:auto}
.article{max-width:860px;margin:0 auto;padding:40px 48px 80px}
.article h1{font-size:28px;font-weight:800;margin-bottom:4px;line-height:1.3}
.article h2{font-size:20px;font-weight:700;margin:40px 0 14px;padding-bottom:8px;border-bottom:1px solid var(--border)}
.article h3{font-size:16px;font-weight:700;margin:30px 0 10px}
.article h4{font-size:14px;font-weight:700;margin:22px 0 8px;color:var(--text2)}
.article p{font-size:14px;line-height:1.9;margin-bottom:14px}
.article strong{color:var(--accent)}
.article code{background:var(--code-bg);padding:2px 6px;border-radius:4px;font-family:'JetBrains Mono','Cascadia Code',monospace;font-size:12.5px}
.article pre{background:var(--bg2);border:1px solid var(--border);border-radius:8px;padding:16px 20px;overflow-x:auto;margin-bottom:16px}
.article pre code{background:none;padding:0;font-size:12px;line-height:1.6}
.article blockquote{border-left:4px solid var(--bq-border);background:var(--bq-bg);padding:12px 18px;margin-bottom:14px;border-radius:0 6px 6px 0}
.article blockquote p{margin-bottom:4px;font-size:13px;color:var(--text2)}
.article ul,.article ol{margin-bottom:14px;padding-left:24px}
.article li{font-size:14px;line-height:1.9;margin-bottom:4px}
.article hr{border:none;border-top:1px solid var(--hr);margin:28px 0}
.article table{width:100%;border-collapse:collapse;margin-bottom:16px;font-size:13px}
.article th{background:var(--bg2);text-align:left;padding:8px 12px;font-weight:600;border-bottom:2px solid var(--table-border)}
.article td{padding:8px 12px;border-bottom:1px solid var(--table-border)}
.article tr:nth-child(even) td{background:var(--table-stripe)}
.article a{color:var(--accent);text-decoration:none}
.article a:hover{text-decoration:underline}
.article img{max-width:100%;border-radius:6px}
.fig-callout{background:var(--card-bg);border:1px solid var(--border);border-radius:10px;padding:16px 20px;margin:20px 0;box-shadow:var(--card-shadow);display:flex;align-items:center;gap:14px;cursor:pointer;transition:all .15s}
.fig-callout:hover{border-color:var(--accent);box-shadow:0 2px 8px rgba(99,102,241,.15)}
.fig-callout .fig-icon{width:44px;height:44px;border-radius:10px;background:var(--accent-l);display:flex;align-items:center;justify-content:center;font-size:20px;flex-shrink:0}
.fig-callout .fig-info{flex:1}
.fig-callout .fig-label{font-size:12px;font-weight:700;color:var(--accent);text-transform:uppercase}
.fig-callout .fig-title{font-size:14px;font-weight:600;margin-top:2px}
.fig-callout .fig-desc{font-size:12px;color:var(--text2);margin-top:2px}
.fig-callout .fig-arrow{font-size:18px;color:var(--text2)}
.iframe-container{width:100%;height:calc(100vh - 53px)}
.iframe-container iframe{width:100%;height:100%;border:none}
.view-hidden{position:absolute;left:0;top:0;width:1px;height:1px;overflow:hidden;opacity:0;pointer-events:none}
.view-hidden iframe{width:1px;height:1px}
@media print{.view-hidden{position:static;width:100%;height:auto;overflow:visible;opacity:1;pointer-events:auto}
.view-hidden iframe{width:100%;height:650px}}
.meta-block{background:var(--bg2);border:1px solid var(--border);border-radius:8px;padding:16px 20px;margin-bottom:28px;font-size:13px;color:var(--text2);line-height:1.8}
.meta-block strong{color:var(--text)}
.toc{background:var(--bg2);border:1px solid var(--border);border-radius:8px;padding:16px 20px;margin:0 0 28px}
.toc h3{font-size:14px;margin:0 0 10px;padding:0;border:none}
.toc ol{padding-left:20px;margin:0}
.toc li{font-size:13px;line-height:2;margin:0}
.toc a{color:var(--accent)}
@media(max-width:768px){
.sidebar{width:60px;min-width:60px}
.sidebar-header{padding:12px 8px}
.sidebar-header h2{font-size:11px}
.sidebar-header p{display:none}
.sidebar-section-title{display:none}
.sidebar-item{padding:10px 8px;justify-content:center}
.sidebar-item span:not(.icon){display:none}
.sidebar-item .badge{display:none}
.article{padding:24px 16px 60px}
}
@media print {
.sidebar, .toolbar, .theme-toggle { display: none !important; }
body { display: block !important; overflow: visible !important; background: #fff !important; color: #000 !important; }
.main { display: block !important; height: auto !important; overflow: visible !important; }
.content { overflow: visible !important; height: auto !important; }
#view-paper { display: block !important; }
.article { padding: 10px 0 !important; max-width: 100% !important; }
.article h2 { page-break-after: avoid; }
.article h3 { page-break-after: avoid; }
.fig-callout { break-inside: avoid; border: 1px solid #ccc !important; background: #f9f9f9 !important; }
.meta-block, .toc { break-inside: avoid; }
blockquote { break-inside: avoid; }
pre { break-inside: avoid; white-space: pre-wrap !important; }
table { break-inside: avoid; }
.print-diagram { page-break-before: always; break-inside: avoid; }
.print-diagram iframe { width: 100%; height: 650px; border: 1px solid #ccc; }
.print-diagram .print-label { font-size: 16px; font-weight: 700; margin-bottom: 8px; padding: 4px 0; border-bottom: 2px solid #333; }
/* Hide the on-screen only elements */
.no-print { display: none !important; }
}
</style>
</head>
<body>
<aside class="sidebar">
<div class="sidebar-header"><h2>RemindAI</h2><p>Agent 工作流论文 & 架构图表</p></div>
<div class="sidebar-section"><div class="sidebar-section-title">论文</div>
<a class="sidebar-item active" data-view="paper" onclick="switchView('paper')"><span class="icon">📄</span>完整论文</a>
</div>
<div class="sidebar-section"><div class="sidebar-section-title">架构图表</div>
<a class="sidebar-item" data-view="fig1" onclick="switchView('fig1')"><span class="icon">🏗️</span>核心架构<span class="badge">图1</span></a>
<a class="sidebar-item" data-view="fig2" onclick="switchView('fig2')"><span class="icon">🔄</span>对话循环<span class="badge">图2</span></a>
<a class="sidebar-item" data-view="fig3" onclick="switchView('fig3')"><span class="icon">🔁</span>生命周期<span class="badge">图3</span></a>
<a class="sidebar-item" data-view="fig4" onclick="switchView('fig4')"><span class="icon">🧩</span>Skill & MCP<span class="badge">图4</span></a>
<a class="sidebar-item" data-view="fig5" onclick="switchView('fig5')"><span class="icon">🧠</span>Memory & Schedule<span class="badge">图5</span></a>
</div>
</aside>
<main class="main">
<div class="toolbar">
<span class="toolbar-title" id="toolbar-title">📄 RemindAI Agent 工作流 — 完整论文</span>
<div class="toolbar-actions">
<button class="btn" onclick="printAll()">🖨️ 打印全部</button>
<button class="theme-toggle" onclick="toggleTheme()" id="theme-btn">☀️</button>
</div>
</div>
<div class="content" id="content-area">
<!-- ==================== PAPER ==================== -->
<div id="view-paper" class="article">
<h1>RemindAI Agent 工作流</h1>
<h2 style="border:none;margin-top:4px;padding:0;font-size:16px;font-weight:500;color:var(--text2)">工具外壳、技能系统与持久记忆的工程实践</h2>
<div class="meta-block">
<strong>作者:</strong>RemindAI 团队 | <strong>版本:</strong>v1.0.5 | <strong>日期:</strong>2026年7月<br>
<strong>摘要:</strong>本文系统阐述 RemindAI Agent 工作流的设计理念、核心架构与工程实现。RemindAI 提出 <strong>"Agent = LLM(决策推理)+ ToolShell(工具执行)+ Permissions(权限控制)"</strong> 的核心公式,通过五层分层架构(表现层、智能核心层、工具外壳层、工具生态层、基础设施层)和四大横向支撑系统(Memory、Schedule、Worktree、Security),构建了一个可控、可扩展、可记忆的 AI Agent 框架。本文详细分析了 Agent Loop 对话循环的工作机制(含 DSML 兼容层、Tool Call 三层防御、Cache-Aligned 上下文压缩)、ToolShell 工具外壳的设计模式(含智能并行执行与资源冲突检测)、多 Agent 协作系统(Commander/Worker/Reviewer 角色体系)、Skill 技能系统的相关性路由与注入架构、MCP 协议的工具扩展能力、Memory 系统的双写容灾与软失效过滤、知识库的中文友好切块策略,以及宠物经济系统、在线协作服务与对外 API 服务器等完整功能矩阵。
</div>
<div class="meta-block" style="margin-top:-12px">
<strong>关键词:</strong>AI Agent;ToolShell;Agent Loop;Cache-Aligned 压缩;DSML 兼容层;多 Agent 协作;Skill 相关性路由;MCP;持久记忆;知识库;资源冲突检测;宠物经济
</div>
<!-- TOC -->
<div class="toc">
<h3>📑 目录</h3>
<ol>
<li><a href="#s1">引言</a></li>
<li><a href="#s2">背景与动机</a></li>
<li><a href="#s3">核心架构设计</a></li>
<li><a href="#s4">Agent 对话循环</a></li>
<li><a href="#s5">ToolShell 工具外壳</a></li>
<li><a href="#s6">Skill 技能系统</a></li>
<li><a href="#s7">MCP 模型上下文协议集成</a></li>
<li><a href="#s8">Memory 记忆系统</a></li>
<li><a href="#s9">Schedule 计划管理</a></li>
<li><a href="#s10">Worktree 隔离实验</a></li>
<li><a href="#s11">权限与安全模型</a></li>
<li><a href="#s12">System Probe 环境探测</a></li>
<li><a href="#s13">多 Agent 协作系统</a></li>
<li><a href="#s14">知识库与在线服务</a></li>
<li><a href="#s15">宠物经济与 API 服务器</a></li>
<li><a href="#s16">系统集成与工作流全景</a></li>
<li><a href="#s17">实践经验与工程教训</a></li>
<li><a href="#s18">相关工作与对比分析</a></li>
<li><a href="#s19">未来展望</a></li>
<li><a href="#s20">结论</a></li>
</ol></div>
<hr>
<!-- ===== 1. 引言 ===== -->
<h2 id="s1">1. 引言</h2>
<p>大语言模型(Large Language Model, LLM)的快速发展正在深刻改变软件开发的范式。从早期的代码补全工具到如今能够自主完成复杂编程任务的 AI Agent,开发者与代码之间的关系正在被重新定义。然而,一个令人困惑的现象普遍存在:为什么同样的 API 和模型,有些人能让 Agent 高效地完成从编码到部署的全流程,而另一些人却只能得到无法运行的代码片段?为什么 Claude Code、Codex 等工具在某些场景下表现出色,而在其他场景下却举步维艰?</p>
<p>答案在于:<strong>Agent 不仅仅是 LLM</strong>。一个真正高效的 AI Agent 需要在 LLM 的推理能力之上,构建一套完整的工具执行框架、知识注入系统和权限控制模型。这正是 RemindAI 的设计出发点。</p>
<p>RemindAI 是一个面向软件开发的 AI Agent 框架,其核心公式为:</p>
<blockquote><p><strong>Agent = LLM(决策与推理)+ ToolShell(工具执行与环境交互)+ Permissions(安全可控的权限模型)</strong></p></blockquote>
<p>这个公式看似简单,但其工程实现涉及诸多关键设计决策。本文将从架构设计、核心子系统、工程实践三个维度,系统阐述 RemindAI Agent 工作流的设计理念与实现细节。</p>
<h3>1.1 本文贡献</h3>
<p>本文的主要贡献包括:</p>
<ol>
<li><strong>提出五层分层架构模型</strong>:将 Agent 系统从表现层到基础设施层进行清晰的分层设计,各层职责明确、接口规范。</li>
<li><strong>系统阐述 Agent Loop 对话循环</strong>:详细分析从用户输入到最终响应的完整工作流,包括 System Prompt 注入、Context 组装、LLM 调用、Tool Call 解析、结果反馈与循环控制。</li>
<li><strong>设计 ToolShell 工具外壳模式</strong>:提出一种统一的工具执行抽象层,将文件操作、Shell 执行、Python/JS 运行时、并行调用等能力以一致的方式暴露给 LLM。</li>
<li><strong>构建 Skill 技能注入架构</strong>:设计了项目级技能和全局技能的分层管理体系,通过 SKILL.md 和 tools.json 实现知识的模块化注入。</li>
<li><strong>实现多层记忆系统</strong>:基于 SQLite 的持久记忆存储,结合可选的向量检索,实现跨会话的知识积累与上下文召回。</li>
<li><strong>提出 Schedule 任务管理机制</strong>:将大型需求自动拆解为 P0/P1/P2 优先级的子任务,实现自动化的工作计划追踪。</li>
</ol>
<hr>
<!-- ===== 2. 背景与动机 ===== -->
<h2 id="s2">2. 背景与动机</h2>
<h3>2.1 "老式 LLM"与 AI Agent</h3>
<p>在 AI Agent 概念兴起之前,开发者使用 LLM 的方式相对简单:将问题描述输入对话框,获取代码或文字回复,然后手动将其集成到项目中。这种模式可称为"老式 LLM"交互模式。它的局限非常明显:</p>
<ul>
<li><strong>缺乏工具能力</strong>:LLM 只能生成文本,无法直接操作文件系统、执行命令或调用外部 API。</li>
<li><strong>缺乏上下文持久化</strong>:每次对话相互独立,模型无法"记住"之前的项目约定、技术选型和踩过的坑。</li>
<li><strong>缺乏环境感知</strong>:模型不知道本地安装了哪些工具、版本是什么、环境变量如何配置。</li>
<li><strong>缺乏安全控制</strong>:一旦给了 LLM 执行能力,如何在可控的范围内运行成为关键问题。</li>
</ul>
<p>RemindAI 的诞生源于对这些痛点的切身体会。正如作者所言:"我只是一个会使用工具的猴子,把我的方式通过炼化成范式的文档带给 LLM,使其与我的开发环境融合变成了 Agent。"这种将开发者工作流范式化、工具化的思路,构成了 RemindAI 的核心设计哲学。</p>
<h3>2.2 核心洞察</h3>
<p>在构建 RemindAI 的过程中,我们形成了以下核心洞察:</p>
<p><strong>洞察一:LLM 只是决策引擎,不是执行引擎。</strong>你搭配的 API + Key 只是为你提供了决策思维能力,但实际执行仍然调用本地资源(CPU、内存、GPU 等)。Agent 的"行动力"来自于它所连接的工具链。</p>
<p><strong>洞察二:工具链的完整性决定 Agent 的能力上限。</strong>只有安装了对应的开发工具并正确配置环境变量,Agent 才能"理解"开发环境并做出正确的决策。</p>
<p><strong>洞察三:安全不是可选项,而是架构基础。</strong>Normal/Auto 双模式、安全边界检查、文件操作确认机制——这些不是事后添加的"功能",而是从第一天就嵌入架构的基础设施。</p>
<p><strong>洞察四:记忆是 Agent 进化之路。</strong>没有记忆的 Agent 每次对话都是从零开始。持久化的事实记录、决策记录、错误教训,让 Agent 具备了跨会话学习和进化的能力。</p>
<hr>
<!-- ===== 3. 核心架构设计 ===== -->
<h2 id="s3">3. 核心架构设计</h2>
<div class="fig-callout" data-view="fig1">
<div class="fig-icon">🏗️</div>
<div class="fig-info">
<div class="fig-label">点击查看</div>
<div class="fig-title">图 1: RemindAI 核心架构</div>
<div class="fig-desc">五层分层架构(表现层 → 智能核心层 → 工具外壳层 → 工具生态层 → 基础设施层)+ 横向支撑系统</div>
</div>
<div class="fig-arrow">→</div>
</div>
<p>RemindAI 采用<strong>五层分层架构 + 横向支撑系统</strong>的设计模式。五层分别是:</p>
<h3>3.1 表现层(Presentation Layer)</h3>
<p>表现层是用户与 RemindAI 交互的界面,基于 Flutter 框架构建,支持 Desktop 和 Mobile 平台。核心功能包括对话窗口、技能管理面板、设置面板、Markdown 渲染和代码高亮。表现层负责接收用户输入、渲染 Agent 输出,并将用户确认传递给下层。</p>
<h3>3.2 智能核心层(Intelligence Core)</h3>
<p>智能核心层是 Agent 的"大脑",负责决策和推理:</p>
<ul>
<li><strong>Prompt Engine(提示词引擎)</strong>:负责 System Prompt 的组装,包括身份锚定、当前日期注入、Skill 上下文的相关性路由拼接、Memory 召回结果的注入、System Probe 结果的嵌入。这是决定 Agent "知道什么"的关键环节。</li>
<li><strong>Context Manager(上下文管理器)</strong>:<strong>Cache-Aligned</strong> 压缩策略实现 95%+ Prompt Cache 命中率;Token 预算驱动保留(25% 可用输入空间);Output Reservation(12.8K tokens 预留输出);超大工具结果压缩(>5000字符截断);频繁压缩检测与自适应调整;记忆沉淀(压缩前提取值得记住的信息存入向量库)。</li>
<li><strong>LLM Client</strong>:统一的 LLM API 调用层,支持 OpenAI / Anthropic / Gemini 三协议原生实现,含 DSML 兼容层处理 DeepSeek 非标行为、不完整 JSON 自修复、流截断恢复。</li>
<li><strong>Hook 系统</strong>:8 个生命周期钩子(onSessionStart/End、onBeforeUserMessage、onBefore/AfterLlmCall、onBefore/AfterToolResult、onAgentDone),已实现 MemoryRecallHook、MemoryStoreHook、KbRecallHook、ScheduleHook、SystemProbeHook。</li>
</ul>
<h3>3.3 工具外壳层(ToolShell Layer)</h3>
<p>ToolShell 是 RemindAI 最核心的创新之一——一个统一的工具执行抽象层:</p>
<ul>
<li><strong>File Ops</strong>:文件读写、搜索、删除操作</li>
<li><strong>Shell Exec</strong>:跨平台 Shell 命令执行(Bash / PowerShell / CMD)</li>
<li><strong>Python Runner</strong>:Python 代码执行,自动捕获 Matplotlib 图表</li>
<li><strong>JS Runner</strong>:JavaScript/TypeScript 执行,Bun/Node 双运行时</li>
<li><strong>Parallel Runner</strong>:最多 8 个子调用的并行执行引擎,支持并行读取、写入不同文件、删除不同文件、执行不同命令,通过智能资源冲突检测保障安全</li>
</ul>
<h3>3.4 工具生态层(Tool Ecosystem)</h3>
<p>工具生态层提供了可扩展的能力接入机制:</p>
<ul>
<li><strong>Skill Registry</strong>:项目级技能与全局技能的注册、发现、注入</li>
<li><strong>MCP Client</strong>:模型上下文协议客户端,支持 stdio/SSE/HTTP 三种传输</li>
<li><strong>Tool Registry</strong>:插件式工具注册框架</li>
<li><strong>System Probe</strong>:本地开发环境工具链自动探测</li>
</ul>
<h3>3.5 基础设施层(Infrastructure)</h3>
<p>基础设施层是 Agent 实际依赖的系统资源:</p>
<ul>
<li><strong>Database</strong>:Drift/SQLite 持久化存储</li>
<li><strong>Memory Store</strong>:基于 Qdrant 向量库 + SQLite 的记忆系统</li>
<li><strong>App Logger</strong>:Zone 拦截的日志系统</li>
<li><strong>Isolate Pool</strong>:7 类后台 CPU 密集型任务并行处理</li>
</ul>
<h3>3.6 横向支撑系统</h3>
<p>四大横向支撑系统贯穿所有层级:</p>
<ul>
<li><strong>Schedule</strong>:P0/P1/P2 任务管理与自动追踪</li>
<li><strong>Worktree</strong>:基于 Git 的隔离实验环境</li>
<li><strong>Memory</strong>:长/短期记忆策略(MIND/REMIND)</li>
<li><strong>Security</strong>:安全边界检查与权限控制</li>
</ul>
<p>这种"分层 + 横向"的架构设计使得每一层都可以独立演进而互不影响。例如,当需要升级 LLM API 时,只需修改 Layer 2 的相关组件;当需要添加新的工具类型时,只需在 Layer 3 中注册新的 Tool 实现。</p>
<hr>
<!-- ===== 4. Agent 对话循环 ===== -->
<h2 id="s4">4. Agent 对话循环</h2>
<div class="fig-callout" data-view="fig2">
<div class="fig-icon">🔄</div>
<div class="fig-info">
<div class="fig-label">点击查看</div>
<div class="fig-title">图 2: Agent 对话循环流程</div>
<div class="fig-desc">Prompt → Context → LLM → Tool Call → Loop:从用户输入到最终响应的完整工作流</div>
</div>
<div class="fig-arrow">→</div>
</div>
<p>Agent Loop(对话循环)是 RemindAI 最核心的工作流引擎。</p>
<h3>4.1 循环的七个阶段</h3>
<p><strong>阶段一:用户输入。</strong>用户通过表现层的对话窗口输入自然语言描述的需求。这可以是"帮我写一个 REST API"这样的开发任务,也可以是"解释一下这段代码"这样的理解任务,甚至可以是"把文件夹里的所有图片压缩一下"这样的文件操作任务。</p>
<p><strong>阶段二:System Prompt 组装。</strong>Prompt Engine 将以下内容组装为完整的 System Prompt:</p>
<ul>
<li><strong>Skill 上下文</strong>:所有激活的 Skill 的 SKILL.md 内容被拼接进来</li>
<li><strong>Memory 召回</strong>:如果 REMIND=true,从 memory.db 中检索相关的历史记忆</li>
<li><strong>System Probe 结果</strong>:本地开发环境的工具链信息("你可以使用 Python 3.12、Node 20.x、Git 2.45...")</li>
<li><strong>工具列表</strong>:当前可用的所有 ToolShell 工具及其参数规范(OpenAI Function Calling 格式)</li>
<li><strong>工作目录信息</strong>:当前项目路径、文件结构摘要</li>
<li><strong>Schedule 状态</strong>:当前待办任务的摘要</li>
</ul>
<p><strong>阶段三:Context Manager 控制。</strong>System Prompt 的内容非常丰富,但 LLM 的上下文窗口是有限且昂贵的资源。Context Manager 负责 Token 预算控制,确保不超出模型的上下文窗口限制,必要时进行滑动窗口裁剪和历史压缩摘要。</p>
<p><strong>阶段四:LLM 调用与决策。</strong>组装完成的完整上下文发送给 LLM API。LLM 返回两种可能的结果:纯文本回复(直接进入阶段七),或 Tool Call 指令(进入阶段五)。Tool Call 指令包含工具名称、参数 JSON,以及 LLM 的推理过程。</p>
<p><strong>阶段五:Tool Call 执行。</strong>Agent Loop 解析 LLM 返回的 Tool Call 指令,提取工具名称和参数,通过 Tool Router 路由到对应的工具实现。Tool Router 根据工具名称选择对应的 ToolShell 能力(File Ops、Shell Exec、Python Runner 等)。</p>
<p><strong>阶段六:结果捕获与存储。</strong>工具执行完成后:</p>
<ol>
<li>执行结果(stdout、文件内容、图表图片等)被捕获</li>
<li>结构化的结果对象(包含状态码、stdout、stderr、图片路径)返回给 Agent Loop</li>
<li>结果自动存储到 Memory System(类型为 outcome 或 error),为后续对话提供上下文</li>
</ol>
<p><strong>阶段七:结果反馈与循环控制。</strong>工具执行结果注入 Context,Agent Loop 将更新的上下文再次发送给 LLM。循环在以下条件终止:</p>
<ul>
<li>LLM 返回纯文本(没有 Tool Call)</li>
<li>达到最大循环次数(防止无限循环)</li>
<li>用户主动中断</li>
</ul>
<p>整个循环形成一个"回边",在架构图中以紫色虚线箭头标识。</p>
<h3>4.2 Agent Run 生命周期</h3>
<div class="fig-callout" data-view="fig3">
<div class="fig-icon">🔁</div>
<div class="fig-info">
<div class="fig-label">点击查看</div>
<div class="fig-title">图 3: Agent Run 生命周期</div>
<div class="fig-desc">五个主阶段 + 三个中断等待状态 + 三个终态出口 + 修正重试回边</div>
</div>
<div class="fig-arrow">→</div>
</div>
<p>Agent Run 的生命周期是一个完整的状态机,涵盖从任务入队到最终退出的全部状态。在主生命周期轨道上顺序经过五个阶段:</p>
<ol>
<li><strong>任务入队(Queued)</strong>:接收用户请求,分配唯一 Run ID</li>
<li><strong>分析规划(Planning)</strong>:注入 System Prompt,召回 Memory,拆解 Schedule</li>
<li><strong>工具执行(Executing)</strong>:ToolShell 调度工具调用,支持并行/串行执行</li>
<li><strong>结果审查(Reviewing)</strong>:校验输出质量,检查完整性</li>
<li><strong>任务完成(Completed)</strong>:返回最终响应,写入 outcome 记忆</li>
</ol>
<p>在 Normal 模式下,执行阶段可能触发<strong>审批中断(Needs Approval)</strong>——此时 Run 进入等待状态,直到用户确认或拒绝。拒绝后 Run 进入终态<strong>取消(Cancelled)</strong>。当缺少必要参数时,审查阶段可能触发<strong>阻塞中断(Blocked)</strong>——等待用户补充后恢复执行,超时则进入终态<strong>超时退出(Expired)</strong>。执行阶段的不可恢复异常直接进入终态<strong>致命错误(Error)</strong>。审查阶段的结果若不满足质量门,可沿修正重试回边(dashed return path)回到执行阶段继续迭代。</p>
<h3>4.3 循环效率优化</h3>
<p><strong>原则一:最小化 LLM 调用。</strong>能在一次 Tool Call 中完成的操作不分多次调用。例如,读取多个文件使用并行调用而非逐个读取。</p>
<p><strong>原则二:上下文压缩。</strong>当 Token 使用接近窗口上限时,Context Manager 自动触发历史压缩。RemindAI 采用 <strong>Cache-Aligned</strong> 策略:复用原始消息前缀作为摘要提示词,实现 95%+ 的 Prompt Cache 命中率。压缩采用 Token 预算驱动(25% 可用输入空间),带输出预留(12.8K tokens)防止 LLM 输出溢出,并在压缩间隔过短(<10 轮)时自动检测并调整策略。</p>
<p><strong>原则三:结果标准化。</strong>工具执行的结果以结构化格式返回(包含状态码、stdout、stderr、图片路径等),便于 Agent Loop 解析和下一轮决策。</p>
<h3>4.4 DSML 兼容层</h3>
<p>DeepSeek 等模型有时不通过标准 <code>tool_calls</code> 协议返回工具调用,而是在文本内容中嵌入 DSML 标记。RemindAI 的 <code>_DsmlParser</code> 自动检测并解析这些非标输出,转换为标准 <code>ToolCall</code> 对象。支持三种变体前缀(全角/半角),确保无论模型以何种方式输出工具调用,Agent Loop 都能正确处理。</p>
<h3>4.5 Tool Call 配对完整性(三层防御)</h3>
<p>LLM API 要求每个 <code>tool_calls</code> 都必须有对应的 <code>tool</code> 结果消息。压缩、截断、中断等异常路径可能破坏这种配对关系,导致 HTTP 400 错误。RemindAI 构建了三层防御:</p>
<ol>
<li><strong>ContextCompactor 压缩清理</strong>:压缩边界切割时自动清理孤立的 tool_calls 和无主的 tool 结果</li>
<li><strong>保真区向前/向后扩展</strong>:保留区包含 tool 结果时,自动扩展到对应的 assistant tool_calls;反之亦然</li>
<li><strong>AgentLoop 发送前验证</strong>:最后一道防线,调用 <code>_validateToolCallPairing</code> 检测所有 assistant 的 tool_calls 是否都有对应结果,发现孤立项时自动修复而非报错</li>
</ol>
<hr>
<!-- ===== 5. ToolShell ===== -->
<h2 id="s5">5. ToolShell 工具外壳</h2>
<p>ToolShell 是 RemindAI 的工具执行抽象层。它的设计目标是:将所有类型的工具执行(文件操作、Shell 命令、Python 代码、JS 代码、MCP 调用)统一为一套一致的接口,让 LLM 无需关心底层执行细节。</p>
<h3>5.1 Python Runner</h3>
<p>执行 Python 代码并返回结果。这是 RemindAI 最具特色的工具之一,因为它能自动捕获 Matplotlib 图表:当代码调用 <code>plt.show()</code> 或 <code>plt.savefig()</code> 时,图表被自动渲染为 PNG 文件并返回路径。Agent 可以在回复中使用 <code></code> 的 Markdown 语法直接展示图表。</p>
<p>在本文的撰写过程中,所有架构图、流程图均为 RemindAI 使用 Python Runner 自动生成。这体现了"Agent 使用自己创建的工具来创建另一个工具"的元能力。</p>
<h3>5.2 JS Runner</h3>
<p>执行 JavaScript/TypeScript 代码。自动检测可用运行时(优先 Bun,其次 Node),支持 ESM 语法。自动检测 ECharts 等图表库并注入无头渲染,图表自动输出为 PNG。</p>
<h3>5.3 File Ops</h3>
<p>统一的文件操作接口:读取、写入(create/overwrite/append)、删除、搜索(glob + 内容正则)。所有操作遵守安全边界:禁止操作 <code>.git/</code>、<code>.env</code>、<code>node_modules/</code>、<code>*.pem</code> 等敏感路径。</p>
<h3>5.4 Shell Exec</h3>
<p>跨平台 Shell 命令执行。Windows 上自动选择 PowerShell(无则降级 CMD),Linux/macOS 上选择 Bash(无则降级 sh)。支持超时控制和 cwd 指定。长时间命令(build、install)有超时保护。</p>
<h3>5.5 Parallel Runner</h3>
<p>并行子调用引擎:在一次决策中发起多个互不依赖的调用。单批最多 8 个子调用。支持并行读取、并行写入不同文件、并行删除不同文件、并行执行不同命令。框架通过<strong>智能资源冲突检测</strong>保障安全:检查写路径冲突、写+删交叉冲突、重复命令冲突,仅拒绝真正存在竞态的操作组合。嵌套 parallel 调用被禁止。</p>
<hr>
<!-- ===== 6. Skill ===== -->
<h2 id="s6">6. Skill 技能系统</h2>
<div class="fig-callout" data-view="fig4">
<div class="fig-icon">🧩</div>
<div class="fig-info">
<div class="fig-label">点击查看</div>
<div class="fig-title">图 4: Skill & MCP 生态系统</div>
<div class="fig-desc">项目级/全局技能的分层管理 + /skill-temp / /skill-cti 工作流 + MCP 集成</div>
</div>
<div class="fig-arrow">→</div>
</div>
<p>Skill 系统是 RemindAI 的知识注入机制。它的核心思想是:<strong>将开发者的领域知识和操作规范"固化"为可复用的技能模块,在每次对话中自动注入 System Prompt</strong>。</p>
<h3>6.1 Skill 分层设计</h3>
<table>
<tr><th>类型</th><th>位置</th><th>激活方式</th><th>生命周期</th></tr>
<tr><td><strong>项目级技能</strong></td><td><code>.toolshell/skills/<name>/</code></td><td>恒定激活</td><td>跟随工作目录</td></tr>
<tr><td><strong>全局技能</strong></td><td><code>Skills/</code></td><td>用户开关</td><td>跨项目复用</td></tr>
</table>
<p>每个 Skill 目录包含:</p>
<ul>
<li><strong>SKILL.md</strong>(必需):技能说明与使用指南,会被注入到 System Prompt</li>
<li><strong>tools.json</strong>(可选):自定义工具定义,按 OpenAI Function Calling 格式声明</li>
</ul>
<h3>6.2 Skill 工作流命令</h3>
<p><strong>/skill-temp —— 创建项目级技能:</strong>在 <code>.toolshell/skills/</code> 下创建 SKILL.md(和可选的 tools.json),产物恒定激活且仅在该工作目录生效。这是 <strong>默认的技能创建路径</strong>——除非用户明确要求"装到全局",否则所有新技能都建为项目级。</p>
<p><strong>/skill-cti —— 创建·测试·安装全局技能:</strong>三步闭环流程:</p>
<ol>
<li><strong>Create</strong>:在 <code>.toolshell/_staging/</code> 中搭建技能骨架</li>
<li><strong>Test</strong>:自测验证 SKILL.md 完整性和 tools.json 合法性,跑最小用例</li>
<li><strong>Install</strong>:调用 <code>toolshell_install_skill</code> 提升为全局技能,清理 staging</li>
</ol>
<h3>6.3 SkillRouter 相关性匹配引擎</h3>
<p>RemindAI 不会将所有技能无差别注入 System Prompt,而是通过 <code>SkillRouter</code> 进行智能相关性匹配,三层评分策略:</p>
<ul>
<li><strong>Pinned Skills</strong>:会话中被实际使用过的技能,后续轮次始终注入(满分 1.0)</li>
<li><strong>关键词匹配</strong>:双向匹配——技能名出现在输入中加 0.5 分,反向关键词命中每个加 0.08,正向每个加 0.06。中文 bigram 分词 + 停用词过滤</li>
<li><strong>语义匹配</strong>(可选):实时计算用户输入与技能描述的 embedding 余弦相似度</li>
</ul>
<p>阈值 0.2,单轮最多注入 10 个技能。项目级技能始终全量注入不受路由限制。</p>
<h3>6.4 Skill 的价值</h3>
<ul>
<li><strong>知识补全</strong>:为 LLM 提供它可能不知道的最新信息(如新框架的 API 用法)</li>
<li><strong>规范固化</strong>:将项目编码规范、命名约定、架构决策固化为可复用的文档</li>
<li><strong>工作流自动化</strong>:将重复的操作流程(如文档转换、图表生成)封装为 Skill</li>
</ul>
<p>RemindAI 本身携带了多个内置 Skill,例如:</p>
<ul>
<li><strong>Archify-Skill</strong>:架构图生成技能,将 JSON 架构描述渲染为自包含的 HTML 图表</li>
<li><strong>Schedule-Skill</strong>:工作计划管理技能,提供 P0/P1/P2 任务拆解与追踪</li>
<li><strong>System-Skill</strong>:开发环境探测技能,自动识别本地工具链</li>
</ul>
<hr>
<!-- ===== 7. MCP ===== -->
<h2 id="s7">7. MCP 模型上下文协议集成</h2>
<p>MCP(Model Context Protocol)是 Anthropic 提出的标准化协议,用于 LLM 与外部工具和数据源之间的通信。RemindAI 完整实现了 MCP 客户端,支持三种传输方式:</p>
<table>
<tr><th>传输方式</th><th>适用场景</th><th>特点</th></tr>
<tr><td><strong>stdio</strong></td><td>本地子进程</td><td>通过 stdin/stdout 通信,零网络开销</td></tr>
<tr><td><strong>SSE</strong></td><td>远程服务</td><td>Server-Sent Events,支持流式推送</td></tr>
<tr><td><strong>Streamable HTTP</strong></td><td>Web 服务</td><td>标准 HTTP 协议,兼容性最广</td></tr>
</table>
<p>在 RemindAI 的架构中,MCP Servers 位于工具生态层(Layer 4),与 Skill 系统和 System Probe 并列。LLM 通过 ToolShell 的工具调用接口访问 MCP 服务,MCP 的响应以标准的工具结果格式返回。</p>
<p>MCP 的引入极大地扩展了 RemindAI 的能力边界。通过 MCP,Agent 可以访问:</p>
<ul>
<li>第三方 API 服务(如天气查询、数据库查询)</li>
<li>专业工具(如图像处理、PDF 解析)</li>
<li>企业内部系统(如工单系统、知识库)</li>
<li>社区贡献的 MCP Servers</li>
</ul>
<hr>
<!-- ===== 8. Memory ===== -->
<h2 id="s8">8. Memory 记忆系统</h2>
<div class="fig-callout" data-view="fig5">
<div class="fig-icon">🧠</div>
<div class="fig-info">
<div class="fig-title">图 5: Memory & Schedule 数据流全景</div>
<div class="fig-desc">Memory 存储/召回流程、MIND/REMIND 策略、Schedule 状态机、Worktree 隔离</div>
</div>
<div class="fig-arrow">→</div>
</div>
<p>Memory 系统是 RemindAI 实现"跨会话智能"的关键。没有记忆的 Agent 面临的核心问题:</p>
<ul>
<li>每次新对话都从零开始,无法利用之前的经验</li>
<li>Agent 无法从过去的错误中学习</li>
<li>重要的架构决策在对话结束后就"消失"了</li>
<li>跨会话的连续工作流被打断</li>
</ul>
<h3>8.1 存储模型</h3>
<p>Memory 系统的存储基于 SQLite 数据库(<code>.toolshell/memory.db</code>),每条记忆包含类型、内容、重要性评分三个核心字段:</p>
<table>
<tr><th>类型</th><th>含义</th><th>示例</th></tr>
<tr><td><strong>fact</strong></td><td>用户陈述的事实、项目约定、技术选型</td><td>"项目使用 Riverpod 进行状态管理"</td></tr>
<tr><td><strong>decision</strong></td><td>做出的决策及原因</td><td>"选择 Drift 而非 Hive,因为需要关系查询"</td></tr>
<tr><td><strong>context</strong></td><td>项目背景、架构信息</td><td>"项目采用 Clean Architecture,分层为 data/domain/presentation"</td></tr>
<tr><td><strong>error</strong></td><td>踩过的坑、失败的方案</td><td>"flutter_math_fork 的 FormatException 是已知无害异常"</td></tr>
<tr><td><strong>outcome</strong></td><td>任务完成结果</td><td>"成功完成架构图生成,产出 5 张 HTML 图表"</td></tr>
</table>
<h3>8.2 重要性评分</h3>
<p>记忆系统使用 0.0~1.0 的重要性评分作为核心过滤器。判断标准是:<strong>这条信息 6 个月后还有价值吗?</strong></p>
<ul>
<li><strong>0.8~1.0</strong>:核心架构决策、用户强调的偏好、不可逆的技术选型</li>
<li><strong>0.5~0.7</strong>:一般性上下文、中等重要的事实</li>
<li><strong>0.1~0.4</strong>:临时信息、琐碎细节</li>
</ul>
<h3>8.3 软失效过滤(Supersede)</h3>
<p>写入新记忆前,以 0.85 相似度阈值检索语义高度重合的旧记忆,标记为 <code>superseded=true</code>(不物理删除),召回时自动过滤被标记的旧记忆。典型场景:用户偏好变更时防止旧结论污染检索结果。这是 v1.0.5 引入的关键优化,解决了"过时记忆与新记忆同权重混合"的问题。</p>
<h3>8.4 召回策略(REMIND)</h3>
<p>当 <code>memory.json</code> 中 <code>REMIND=true</code> 时,每次新任务开始前 Memory 系统自动触发召回。召回策略有两种:</p>
<ul>
<li><strong>SQLite 关键词匹配</strong>:基于 SQLite 的 LIKE 查询进行关键词检索——始终可用</li>
<li><strong>Qdrant 向量检索</strong>:如果配置了嵌入模型 API,则对记忆生成向量嵌入,使用语义相似度排序——可选启用</li>
</ul>
<h3>8.5 生命周期策略(MIND)</h3>
<p><code>memory.json</code> 中的 <code>MIND</code> 字段控制记忆的生命周期策略:</p>
<ul>
<li><strong>MIND=true(长期记忆)</strong>:跨会话持久存储,会话结束时记忆保留</li>
<li><strong>MIND=false(短期记忆)</strong>:会话结束时生成本次会话摘要存为一条 fact,清除其余记忆</li>
</ul>
<hr>
<!-- ===== 9. Schedule ===== -->
<h2 id="s9">9. Schedule 计划管理</h2>
<p>Schedule 系统是 RemindAI 的任务管理机制。它的核心思想是:<strong>将用户的自然语言需求自动拆解为结构化的任务列表,按优先级排序,并在执行过程中自动追踪进度。</strong></p>
<h3>9.1 三级优先级体系</h3>
<table>
<tr><th>级别</th><th>含义</th><th>处理原则</th></tr>
<tr><td><strong>P0 🔴</strong></td><td>阻塞性 / 紧急</td><td>立即处理,不可被打断</td></tr>
<tr><td><strong>P1 🟡</strong></td><td>重要但不紧急</td><td>当前 P0 完成后按序执行</td></tr>
<tr><td><strong>P2 🟢</strong></td><td>一般 / 可延后</td><td>有空再做,不主动提起</td></tr>
</table>
<h3>9.2 核心操作</h3>
<p>Schedule 系统提供六个核心操作:</p>
<ul>
<li><strong>schedule_add_task</strong>:添加新任务,指定优先级、标签、备注</li>
<li><strong>schedule_complete</strong>:标记任务完成,移动到已完成区域</li>
<li><strong>schedule_update</strong>:更新任务描述、优先级或位置</li>
<li><strong>schedule_delete</strong>:删除不再需要的待办任务</li>
<li><strong>schedule_review</strong>:生成当前计划的进度报告</li>
<li><strong>schedule_archive</strong>:归档超过指定天数的已完成条目</li>
</ul>
<h3>9.3 与 Agent Loop 的集成</h3>
<p>Schedule 系统与 Agent Loop 紧密集成:</p>
<ul>
<li>每次新对话开始时,Schedule 自动加载当前待办摘要并注入 System Prompt</li>
<li>Agent 每完成一个子任务,自动调用 schedule_complete 标记完成</li>
<li>执行过程中发现新问题,Agent 自动调用 schedule_add_task 插入新任务</li>
<li>用户问进度或 P0 完成时,自动生成进度报告</li>
</ul>
<hr>
<!-- ===== 10. Worktree ===== -->
<h2 id="s10">10. Worktree 隔离实验</h2>
<p>Worktree 隔离实验是 RemindAI 应对"探索性修改风险"的方案。当 Agent 需要做大范围重构、尝试不确定的实现方案、或升级有兼容风险的依赖时,可以在隔离的 Git 工作树中进行实验——验证通过则合并,不满意则直接丢弃,主工作目录完全不受影响。</p>
<h3>10.1 适用场景</h3>
<ul>
<li><strong>大范围重构</strong>:修改数十个文件的架构调整</li>
<li><strong>尝试性方案</strong>:不确定某个技术方案是否可行</li>
<li><strong>依赖升级</strong>:升级可能有兼容风险的依赖</li>
<li><strong>探索性开发</strong>:快速原型验证</li>
</ul>
<h3>10.2 标准工作流</h3>
<ol>
<li><strong>开始隔离</strong>:调用 <code>toolshell_worktree_start</code>,创建独立分支 + 工作树</li>
<li><strong>正常开发</strong>:在隔离环境中自由修改、测试</li>
<li><strong>存档节点</strong>:在关键里程碑调用 <code>toolshell_worktree_checkpoint</code> 创建存档点</li>
<li><strong>审查改动</strong>:调用 <code>toolshell_worktree_diff</code> 查看变更摘要或完整差异</li>
<li><strong>结束隔离</strong>:
<ul>
<li>满意 → <code>finish(action="merge")</code>:自动提交,合并回主分支,清理工作树</li>
<li>不满意 → <code>finish(action="discard")</code>:直接丢弃,改动全部消失</li>
</ul>
</ol>
<h3>10.3 Checkpoint 存档/回退</h3>
<p>Checkpoint 是 Worktree 隔离的一个创新特性。在同一个实验分支内,Agent 可以创建多个存档点(如 <code>before-schema-change</code>、<code>after-migration</code>),如果后续步骤走偏了,可以回退到上一个存档点重新尝试,而不必放弃整个实验。</p>
<hr>
<!-- ===== 11. 权限与安全 ===== -->
<h2 id="s11">11. 权限与安全模型</h2>
<p>安全模型是 RemindAI 架构的重要基石。它提供两种操作模式:</p>
<table>
<tr><th>模式</th><th>行为</th><th>适用场景</th></tr>
<tr><td><strong>Normal</strong></td><td>每次写/删/执行前向用户确认</td><td>日常开发,安全优先</td></tr>
<tr><td><strong>Auto</strong></td><td>完全自主,直接执行所有操作</td><td>信任的操作序列</td></tr>
</table>
<p>两种模式均遵守<strong>安全边界</strong>,绝对不可操作的路径包括:</p>
<ul>
<li><code>.git/</code> 目录</li>
<li><code>.env</code> / <code>.env.*</code> 文件</li>
<li><code>node_modules/</code></li>
<li><code>*.pem</code> / <code>*.key</code> 私钥文件</li>
<li>项目根目录之外的任何路径</li>
</ul>
<p>用户可以在 Normal 模式下回复 <code>always</code> 切换为 Auto 模式。Approval Gate 是 Normal/Auto 模式的切换点——在 Normal 模式下,Tool Call 必须经过审批才能执行。</p>
<hr>
<!-- ===== 12. System Probe ===== -->
<h2 id="s12">12. System Probe 环境探测</h2>
<p>System Probe 是 RemindAI 的"感官系统"——它探测本地开发环境中安装了哪些工具、版本是什么、环境变量如何配置,并将这些信息注入 System Prompt。这让 LLM 能够精准地知道"我能用什么"。</p>
<p>探测类别包括:运行时(Python、Node、Bun、Dart)、包管理器(npm、pnpm、yarn、pip、cargo)、版本控制(git)、构建工具、容器工具、搜索工具、编辑器、数据库、网络工具等。</p>
<p>这种详细的工具链信息让 LLM 能够做出精准的"用什么"决策。例如,当需要安装依赖时,LLM 知道应该用 <code>pnpm</code> 而非 <code>npm</code>(因为探测结果显示 pnpm 版本更高);当需要图表渲染时,LLM 知道可以使用 Python + Matplotlib(因为确认了 Python 3.12 可用)。</p>
<hr>
<!-- ===== 13. 多 Agent 协作 ===== -->
<h2 id="s13">13. 多 Agent 协作系统</h2>
<p>RemindAI v1.0.5 实现了完整的多 Agent 协作框架,支持六种角色体系和多种通信机制。</p>
<h3>13.1 角色体系</h3>
<table>
<tr><th>角色</th><th>职责</th><th>数量限制</th></tr>
<tr><td><strong>Commander</strong></td><td>总指挥,拆解需求、分配任务、协调各 Agent</td><td>唯一</td></tr>
<tr><td><strong>Worker</strong></td><td>通用工作者</td><td>多个</td></tr>
<tr><td><strong>Reviewer</strong></td><td>审查员,校验其他 Agent 产出</td><td>多个</td></tr>
<tr><td><strong>Researcher</strong></td><td>研究员,信息收集和分析</td><td>多个</td></tr>
<tr><td><strong>Coder</strong></td><td>编码员,专注代码实现</td><td>多个</td></tr>
<tr><td><strong>Custom</strong></td><td>自定义角色</td><td>多个</td></tr>
</table>
<h3>13.2 权限隔离</h3>
<p>每个 Agent 独立配置权限集(fileRead/fileWrite/fileDelete/exec/network),工具加载时按权限过滤。Commander 默认拥有全部权限,Worker 按任务需求精确授权。</p>
<h3>13.3 通信机制</h3>
<ul>
<li><strong>广播(Broadcast)</strong>:指挥部向所有 Agent 发送消息,注入 <code>[指挥部广播]</code> 前缀</li>
<li><strong>点对点(Direct)</strong>:Agent 间直接通信,注入 <code>[发送者 对你说]</code> 前缀</li>
<li><strong>自动路由</strong>:Agent 产出后自动将摘要注入其他 Agent 上下文;短内容直接转发,长内容(>300字)调用 LLM 压缩后转发</li>
<li><strong>文件分发</strong>:支持向单个 Agent 发送文件或全局分发</li>
</ul>
<h3>13.4 Sub-Readers 并行理解编排</h3>
<p>三阶段流程:<strong>规划</strong>(主模型拆解为 1-6 个不重叠子任务)→ <strong>并行执行</strong>(独立只读 Agent + ReadOnlyExecutor,通过 Future.wait 并行)→ <strong>合并</strong>(主模型去重汇总)。ReadOnlyExecutor 仅允许 read/search 两个工具,保证多 Agent 指向同一目录时无冲突。</p>
<hr>
<!-- ===== 14. 知识库与在线服务 ===== -->
<h2 id="s14">14. 知识库与在线服务</h2>
<h3>14.1 知识库系统</h3>
<p>知识库系统实现了"文档 → 文本 → 切块 → 嵌入 → 写入 Qdrant"的完整索引流水线。</p>
<ul>
<li><strong>中文友好切块策略</strong>:默认 chunkSize=900、overlap=120。先按段落聚合,超长段落按句末标点切分,极端情况硬切。相邻块保留重叠维持上下文连贯。</li>
<li><strong>多知识库独立检索</strong>:每个知识库使用独立 Qdrant collection + 独立嵌入模型配置,通过 KbRecallHook 在会话中按需接入。</li>
<li><strong>文本提取</strong>:纯文本类直接读取,办公文档通过 pandoc/pdftotext 转换。</li>
</ul>
<h3>14.2 在线协作服务</h3>
<p>基于 WebSocket 的在线多人协作服务,支持:</p>
<ul>
<li>内置完整 HTTP 服务器(静态资源 + WebSocket + 文件下载)</li>
<li>白名单机制(IP 精确/CIDR 网段/通配符)+ 连接数限制 + 拉闸开关</li>
<li>每个白名单条目独立分配模型卡、MCP 服务器、技能、搜索引擎</li>
<li>Artifact 提取(从 LLM 回复中提取代码块)+ ZIP 批量下载</li>
<li>管理员(localhost)完全控制,普通用户按白名单限制</li>
</ul>
<hr>
<!-- ===== 15. 宠物经济与 API 服务器 ===== -->
<h2 id="s15">15. 宠物经济与 API 服务器</h2>
<h3>15.1 宠物经济系统</h3>
<p>六层架构的完整养成系统:</p>
<ul>
<li><strong>数据层</strong>:精灵图元数据(帧尺寸、动画序列、FPS)</li>
<li><strong>逻辑层</strong>:11 种状态 × 8 种事件的状态机,加权随机转换</li>
<li><strong>引擎层</strong>:单 Timer 驱动帧推进 + 状态超时触发</li>
<li><strong>注册层</strong>:内置 3 种猫咪精灵 + 用户自定义注册</li>
<li><strong>观察层</strong>:12 种全局事件类型 + 3 分钟 idle 检测 + 50 条事件历史</li>
<li><strong>经济层</strong>:Token→宠物币兑换 + 20 种食物商店 + 投喂/饱腹/心情 + 13 个成就</li>
</ul>
<p>所有 LLM 调用入口(主聊天/记忆提取/API 服务/在线服务)均计入 token 统计→兑换宠物币→驱动养成循环。</p>
<h3>15.2 对外 API 服务器</h3>
<p>内置 HTTP 服务器,提供三种独立端点:</p>
<table>
<tr><th>端点</th><th>路径</th><th>功能</th></tr>
<tr><td><strong>OpenAI 聚合</strong></td><td><code>/v1/chat/completions</code></td><td>跑完整 AgentLoop(技能/MCP/记忆/搜索),流式/非流式</td></tr>
<tr><td><strong>Claude 聚合</strong></td><td><code>/v1/agent/messages</code></td><td>同样跑聚合 Agent,以 Anthropic 协议输出 SSE</td></tr>
<tr><td><strong>Claude 代理</strong></td><td><code>/v1/messages</code></td><td>纯协议转换透传,让任意模型伪装成 Claude</td></tr>
</table>
<p>安全设计:默认仅 127.0.0.1 + Bearer token 鉴权 + IP 白名单(CIDR)+ 三端点独立开关 + 记忆三档位(none/isolated/shared)。</p>
<hr>
<!-- ===== 16. 系统集成 ===== -->
<h2 id="s16">16. 系统集成与工作流全景</h2>
<p>将以上所有子系统串联起来,RemindAI 的完整工作流如下:</p>
<ol>
<li>用户输入需求</li>
<li>Prompt Engine 组装 System Prompt(Skill + Memory + System Probe + 工具列表 + Schedule)</li>
<li>Context Manager 控制 Token 预算</li>
<li>LLM 返回决策(纯文本回复 OR Tool Call 指令)</li>
<li>如果是纯文本,直接返回给用户</li>
<li>如果是 Tool Call:Permission Gate 检查 → 安全边界检查 → ToolShell 执行 → 结果存储到 Memory → 结果注入 Context → 回到步骤 4</li>
</ol>
<p>在整个过程中:</p>
<ul>
<li>大型需求通过 Schedule 自动拆解为 P0/P1/P2 任务</li>
<li>探索性修改通过 Worktree 在隔离环境中进行</li>
<li>关键信息通过 Memory 系统持久化到 SQLite(可选 Qdrant 向量索引)</li>
<li>做出的架构决策自动记录为 memory_store (decision)</li>
<li>踩过的坑自动记录为 memory_store (error)</li>
</ul>
<hr>
<!-- ===== 17. 实践经验 ===== -->
<h2 id="s17">17. 实践经验与工程教训</h2>
<h3>14.1 实践案例:音乐播放器开发</h3>
<p>作者手动编写了一个 8000 多行的音乐播放器,花了半个月时间。而用 RemindAI 开发同样的功能,只用了 <strong>10 分钟、2000 多行代码、8 毛钱的 DeepSeek V4 Pro API 费用</strong>。这背后是 Flutter CLI + Dart 编译器 + Android SDK 完整工具链的支持,以及项目规范和架构指导通过 Skill 系统的注入。</p>
<p>这个案例并非说明"AI 取代了开发者",而是展示了:<strong>当 Agent 拥有完整的工具链和正确的上下文时,它能将开发效率提升数个数量级。</strong></p>
<h3>14.2 工程教训</h3>
<p><strong>教训一:Token 预算管理是关键。</strong>在 System Prompt 中放太多 Skill 和 Memory 会导致 Token 超支,反而降低 Agent 性能。需要精心的"上下文预算"管理:只注入最相关的 Skill,只召回最重要的 Memory,必要时使用摘要而非原文。</p>
<p><strong>教训二:Normal 模式不可省略。</strong>即使是资深开发者,在让 Agent 执行批量删除、大规模重构等操作时,Normal 模式的确认机制可以避免灾难性后果。</p>
<p><strong>教训三:错误记忆比成功记忆更有价值。</strong>很多开发者倾向于只记录成功的 outcome,而忽略了失败的 error 教训。事实上,一次踩坑的经验往往比十次成功的经验更有价值——它帮助 Agent 避免重复同样的错误。</p>
<h3>14.3 常见反模式</h3>
<ul>
<li><strong>过度 Skill 化</strong>:为每个小功能都创建 Skill,导致 System Prompt 过长,反而降低性能。Skill 应该覆盖"重复出现的模式",而非"一次性任务"。</li>
<li><strong>忽略错误记忆</strong>:只记录成功的结果(outcome),不记录失败的教训(error)。错误记忆往往比成功记忆更有价值。</li>
<li><strong>P0 泛滥</strong>:将所有任务都标记为 P0,导致优先级体系失效。P0 应该是真正的阻塞性问题。</li>
</ul>
<hr>
<!-- ===== 18. 相关工作 ===== -->
<h2 id="s18">18. 相关工作与对比分析</h2>
<table>
<tr><th>维度</th><th>AutoGPT / BabyAGI</th><th>Claude Code / Codex CLI</th><th>RemindAI</th></tr>
<tr><td><strong>任务管理</strong></td><td>自主链式分解</td><td>—</td><td>Schedule P0/P1/P2</td></tr>
<tr><td><strong>工具执行</strong></td><td>插件式</td><td>内置 CLI</td><td>ToolShell 统一抽象 + 智能并行</td></tr>
<tr><td><strong>知识注入</strong></td><td>—</td><td>有限自定义</td><td>Skill 分层 + MCP</td></tr>
<tr><td><strong>记忆系统</strong></td><td>向量数据库</td><td>会话级</td><td>SQLite + Qdrant + MIND/REMIND</td></tr>
<tr><td><strong>安全模型</strong></td><td>基础权限</td><td>确认机制</td><td>Normal/Auto + 安全边界</td></tr>
<tr><td><strong>隔离实验</strong></td><td>—</td><td>—</td><td>Git Worktree + Checkpoint</td></tr>
<tr><td><strong>环境感知</strong></td><td>—</td><td>内置规则</td><td>System Probe 自动探测</td></tr>
<tr><td><strong>上下文管理</strong></td><td>简单裁剪</td><td>SDK 自动</td><td>Cache-Aligned + Token 预算 + 输出预留</td></tr>
<tr><td><strong>多 Agent</strong></td><td>—</td><td>—</td><td>指挥/工作/审查 + 并行文档理解</td></tr>
</table>
<p>AutoGPT 和 BabyAGI 是早期的自主 Agent 框架,强调链式思考和自主任务分解。RemindAI 借鉴了它们的任务分解思想(体现在 Schedule 系统中),但在工具执行抽象、安全模型、记忆策略等方面做出了显著改进。</p>
<p>Claude Code 和 Codex CLI 是当前最流行的 AI 编程助手。它们在代码生成方面表现出色,但在工具链管理、知识注入、跨会话记忆等方面仍有局限。RemindAI 的 ToolShell + Skill + Memory 三位一体设计,提供了一种更系统化的解决方案。</p>
<hr>
<!-- ===== 19. 未来展望 ===== -->
<h2 id="s19">19. 未来展望</h2>
<h3>16.1 可视化 Skill 编辑器</h3>
<p>当前的 Skill 和 Schedule 主要通过文本配置。一个可视化的拖拽式编辑器可以让非技术用户也能创建和定制 Agent 工作流。</p>
<h3>16.2 更强的上下文管理</h3>
<p>随着 LLM 上下文窗口的不断扩大(从 4K 到 128K 再到 1M+),Context Manager 需要更精细的"注意力预算"管理:不是简单地裁剪,而是根据内容的重要性动态分配 Token。当前已实现 Cache-Aligned 压缩和 Token 预算驱动策略,未来将探索基于语义重要性的自适应裁剪。</p>
<h3>16.3 联邦式 Memory</h3>
<p>当前 Memory 是本地化的(SQLite + Qdrant)。未来可以将 Memory 扩展为联邦式架构:个人私有记忆保留在本地,团队共享记忆同步到中心节点,社区公共记忆通过 MCP 协议接入。</p>
<h3>16.4 跨平台扩展</h3>
<p>当前正式支持 Windows 平台。未来将完善 Linux 和 macOS 的原生支持,包括系统级集成(通知、快捷键、文件关联)和平台特有功能。</p>
<hr>
<!-- ===== 20. 结论 ===== -->
<h2 id="s20">20. 结论</h2>
<p>本文系统阐述了 RemindAI Agent 工作流的设计理念、核心架构与工程实现。我们提出了 <strong>"Agent = LLM + ToolShell + Permissions"</strong> 的核心公式,并通过五层分层架构和四大横向支撑系统将其工程化。</p>
<p>核心贡献包括:</p>
<ol>
<li><strong>五层分层架构</strong>:表现层 → 智能核心层 → 工具外壳层 → 工具生态层 → 基础设施层,清晰的职责分离使得每一层可以独立演进。</li>
<li><strong>Agent Loop 对话循环</strong>:七阶段循环 + DSML 兼容层 + Tool Call 三层防御,形成了完整的"思考-行动-观察"闭环。</li>
<li><strong>Cache-Aligned 上下文压缩</strong>:复用原始消息前缀实现 95%+ Prompt Cache 命中率;Token 预算驱动保留(25%)+ Output Reservation(12.8K)+ 记忆沉淀 + 频繁压缩检测。</li>
<li><strong>智能并行执行</strong>:资源冲突检测允许操作不同目标的写/删/执行并行,仅拒绝真正存在竞态的组合。</li>
<li><strong>多 Agent 协作</strong>:六种角色体系 + 广播/点对点通信 + 自动路由 + Sub-Readers 并行理解编排。</li>
<li><strong>Memory 记忆系统</strong>:SQLite + Qdrant 双写容灾 + 软失效过滤 + MIND/REMIND 灵活策略,让 Agent 具备跨会话学习和进化能力。</li>
<li><strong>Skill 相关性路由</strong>:三层评分(Pin + 关键词 + 语义)的按需注入,解决了"System Prompt 过长导致性能下降"的问题。</li>
<li><strong>Hook 系统</strong>:8 个生命周期钩子支持不修改核心循环即可扩展记忆/知识库/计划/探测等能力。</li>
<li><strong>完整功能矩阵</strong>:知识库中文切块索引 + 在线协作服务 + 三端点 API 服务器 + 宠物经济养成闭环。</li>
</ol>
<p>在架构设计上,五层分层架构提供了清晰的职责分离,而横向支撑系统(Schedule、Worktree、Memory、Security、Logging)则为各层提供了非功能性的保障。这种"分层 + 横向"的架构模式,使得 RemindAI 既保持了系统各部分的独立性,又确保了整体的一致性。</p>
<p>通过实际案例验证,RemindAI 能够在保持安全可控的前提下,显著提升 AI 辅助软件开发的效率与质量。我们相信,随着 LLM 能力的持续提升和工具生态的日益丰富,以 RemindAI 为代表的 Agent 框架将在软件开发领域发挥越来越重要的作用。</p>
<hr>
<blockquote>
<p><strong>📊 图表索引</strong></p>
<p>
<a href="javascript:void(0)" onclick="switchView('fig1')"><strong>图 1</strong>: RemindAI 核心架构 — 五层分层 + 横向支撑</a><br>
<a href="javascript:void(0)" onclick="switchView('fig2')"><strong>图 2</strong>: Agent 对话循环流程 — Prompt → Context → LLM → Tool Call</a><br>
<a href="javascript:void(0)" onclick="switchView('fig3')"><strong>图 3</strong>: Agent Run 生命周期 — 状态机与终态出口</a><br>
<a href="javascript:void(0)" onclick="switchView('fig4')"><strong>图 4</strong>: Skill & MCP 生态系统</a><br>
<a href="javascript:void(0)" onclick="switchView('fig5')"><strong>图 5</strong>: Memory & Schedule 数据流全景</a>
</p>
<p>所有图表均为自包含 HTML 文件,支持明暗主题切换和 PNG/JPEG/WebP/SVG 导出。点击左侧导航栏或文中卡片即可查看。</p>
</blockquote>
<p style="text-align:center;color:var(--text2);font-size:12px;margin-top:40px"><em>本文由 RemindAI Agent 辅助撰写。所有架构图均为 RemindAI Agent 使用 Archify 技能自动生成。</em></p>
</div>
<!-- END PAPER -->
<!-- FIGURE IFRAMES (hidden offscreen but loaded) -->
<div id="view-fig1" class="iframe-container view-hidden"><iframe src="remindai-core-architecture.html" title="核心架构" loading="lazy"></iframe></div>
<div id="view-fig2" class="iframe-container view-hidden"><iframe src="remindai-agent-loop.html" title="对话循环" loading="lazy"></iframe></div>
<div id="view-fig3" class="iframe-container view-hidden"><iframe src="remindai-agent-lifecycle.html" title="生命周期" loading="lazy"></iframe></div>
<div id="view-fig4" class="iframe-container view-hidden"><iframe src="remindai-skill-ecosystem.html" title="Skill & MCP" loading="lazy"></iframe></div>
<div id="view-fig5" class="iframe-container view-hidden"><iframe src="remindai-memory-schedule.html" title="Memory & Schedule" loading="lazy"></iframe></div>
</div></main>
<script>
const titles={paper:'📄 RemindAI Agent 工作流 — 完整论文',fig1:'🏗️ 图1: 核心架构 — 五层分层+横向支撑',fig2:'🔄 图2: Agent对话循环',fig3:'🔁 图3: Agent Run生命周期',fig4:'🧩 图4: Skill & MCP生态系统',fig5:'🧠 图5: Memory & Schedule数据流'};
function switchView(v){
document.querySelectorAll('.sidebar-item').forEach(e=>e.classList.remove('active'));
document.querySelector('[data-view="'+v+'"]').classList.add('active');
document.querySelectorAll('[id^="view-"]').forEach(e=>{e.classList.add('view-hidden');e.classList.remove('view-active')});
var t=document.getElementById('view-'+v);if(t){t.classList.remove('view-hidden');t.classList.add('view-active')}
document.getElementById('toolbar-title').textContent=titles[v]||'';
localStorage.setItem('remindai-view',v);
}
document.addEventListener('click',function(e){var c=e.target.closest('.fig-callout');if(c&&c.dataset.view)switchView(c.dataset.view)});
function toggleTheme(){
var h=document.documentElement,c=h.getAttribute('data-theme'),n=c==='dark'?'light':'dark';
h.setAttribute('data-theme',n);document.getElementById('theme-btn').textContent=n==='dark'?'🌙':'☀️';
localStorage.setItem('remindai-theme',n);
}
function printAll(){
// Build a complete print document with paper + all diagrams
var w=window.open('','_blank','width=900,height=700');
var paper=document.getElementById('view-paper').cloneNode(true);
// Remove the TOC links' href for print cleanliness
paper.querySelectorAll('.toc a').forEach(a=>{a.removeAttribute('href');a.style.color='#000';a.style.textDecoration='none'});
// Remove fig-callout onclick behavior hints
paper.querySelectorAll('.fig-callout').forEach(c=>{c.style.cursor='default';c.querySelector('.fig-arrow').textContent=''});
var html='<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><title>RemindAI Agent 工作流 — 全文打印</title>';
html+='<style>body{font-family:system-ui,sans-serif;max-width:800px;margin:0 auto;padding:20px;color:#000;background:#fff;line-height:1.8}';
html+='h1{font-size:24px}h2{font-size:18px;border-bottom:1px solid #ccc;padding-bottom:4px;margin-top:28px}h3{font-size:15px}';
html+='code{background:#f0f0f0;padding:1px 5px;border-radius:3px;font-size:12px}pre{background:#f5f5f5;padding:12px;border-radius:6px;overflow-x:auto;font-size:12px}';
html+='blockquote{border-left:3px solid #6366f1;background:#f8f9ff;padding:8px 14px;margin:12px 0}';
html+='table{border-collapse:collapse;width:100%;margin:12px 0}th,td{border:1px solid #ddd;padding:6px 10px;text-align:left;font-size:13px}th{background:#f0f0f0}';
html+='.meta-block{background:#f9f9f9;border:1px solid #ddd;padding:10px 14px;margin:12px 0;font-size:13px}';
html+='.fig-callout{border:1px solid #ccc;padding:10px 14px;margin:12px 0;background:#fafafa}';
html+='.print-diagram{page-break-before:always;margin-top:20px}';
html+='.print-diagram .print-label{font-size:16px;font-weight:700;margin-bottom:6px;padding-bottom:4px;border-bottom:2px solid #333}';
html+='img{max-width:100%}';
html+='@media print{body{padding:0}.print-diagram{page-break-before:always}}</style></head><body>';
html+='<h1>RemindAI Agent 工作流</h1><p style="color:#666">工具外壳、技能系统与持久记忆的工程实践</p>';
html+=paper.innerHTML;
// Add diagram sections
var diagrams=[
{label:'图 1: RemindAI 核心架构 — 五层分层 + 横向支撑系统',src:'remindai-core-architecture.html'},
{label:'图 2: Agent 对话循环 — Prompt → Context → LLM → Tool Call',src:'remindai-agent-loop.html'},
{label:'图 3: Agent Run 生命周期 — 状态机与终态出口',src:'remindai-agent-lifecycle.html'},
{label:'图 4: Skill & MCP 生态系统',src:'remindai-skill-ecosystem.html'},
{label:'图 5: Memory & Schedule 数据流全景',src:'remindai-memory-schedule.html'}
];
diagrams.forEach(function(d){
html+='<div class="print-diagram"><div class="print-label">'+d.label+'</div>';
html+='<iframe src="'+d.src+'" style="width:100%;height:620px;border:1px solid #ddd"></iframe></div>';
});
html+='</body></html>';
w.document.write(html);
w.document.close();
// Wait for iframes to load then print
var iframes=w.document.querySelectorAll('iframe');
if(iframes.length===0){setTimeout(function(){w.print()},400);}
else{
var loaded=0;
function checkDone(el){
if(el&&el.dataset && el.dataset.done)return;
if(el&&el.dataset)el.dataset.done='1';
loaded++;
if(loaded>=iframes.length){setTimeout(function(){w.print()},600);}
}
iframes.forEach(function(f){
// If already loaded (cached), mark done immediately
if(f.contentDocument&&f.contentDocument.readyState==='complete'){checkDone(f)}
else{f.onload=function(){checkDone(f)}}
// Fallback: force print after 4s
setTimeout(function(){if(!f.dataset.done)checkDone(f)},4000);
});
}
}
(function(){
var t=localStorage.getItem('remindai-theme');
if(t){document.documentElement.setAttribute('data-theme',t);document.getElementById('theme-btn').textContent=t==='dark'?'🌙':'☀️'}
else if(window.matchMedia('(prefers-color-scheme:dark)').matches){document.documentElement.setAttribute('data-theme','dark');document.getElementById('theme-btn').textContent='🌙'}
// Show paper by default (remove view-hidden)