@rong/agentscript 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/INSTALL.md +92 -0
  3. package/LICENSE +21 -0
  4. package/README.md +246 -0
  5. package/dist/ast/constants.js +1 -0
  6. package/dist/ast/format.js +41 -0
  7. package/dist/ast/types.js +1 -0
  8. package/dist/bin/agentscript.js +234 -0
  9. package/dist/bin/input.js +19 -0
  10. package/dist/bin/repl.js +290 -0
  11. package/dist/index.js +26 -0
  12. package/dist/parser/errors.js +8 -0
  13. package/dist/parser/parser.js +661 -0
  14. package/dist/parser/tokenizer.js +246 -0
  15. package/dist/providers/llm/anthropic.js +36 -0
  16. package/dist/providers/llm/index.js +3 -0
  17. package/dist/providers/llm/ollama.js +19 -0
  18. package/dist/providers/llm/openai.js +31 -0
  19. package/dist/providers/llm/protocol.js +45 -0
  20. package/dist/providers/llm/shared.js +147 -0
  21. package/dist/providers/llm/types.js +1 -0
  22. package/dist/providers/llm/uri.js +24 -0
  23. package/dist/providers/memory/file.js +44 -0
  24. package/dist/providers/memory/host.js +66 -0
  25. package/dist/providers/memory/index.js +1 -0
  26. package/dist/providers/memory/shared.js +56 -0
  27. package/dist/providers/memory/sqlite.js +98 -0
  28. package/dist/providers/mock/index.js +32 -0
  29. package/dist/providers/tools/env.js +11 -0
  30. package/dist/providers/tools/file.js +99 -0
  31. package/dist/providers/tools/host.js +34 -0
  32. package/dist/providers/tools/http.js +40 -0
  33. package/dist/providers/tools/index.js +2 -0
  34. package/dist/providers/tools/scheme.js +16 -0
  35. package/dist/providers/tools/shared.js +92 -0
  36. package/dist/providers/tools/shell.js +80 -0
  37. package/dist/runtime/context.js +160 -0
  38. package/dist/runtime/errors.js +14 -0
  39. package/dist/runtime/evaluator.js +276 -0
  40. package/dist/runtime/generate.js +175 -0
  41. package/dist/runtime/guards.js +39 -0
  42. package/dist/runtime/input.js +38 -0
  43. package/dist/runtime/interpreter.js +314 -0
  44. package/dist/runtime/json.js +59 -0
  45. package/dist/runtime/loader.js +146 -0
  46. package/dist/runtime/scope.js +47 -0
  47. package/dist/runtime/shape.js +132 -0
  48. package/dist/runtime/trace.js +54 -0
  49. package/dist/runtime/truth.js +13 -0
  50. package/dist/runtime/types.js +1 -0
  51. package/dist/runtime/uri.js +10 -0
  52. package/dist/semantic/analyzer.js +519 -0
  53. package/dist/semantic/diagnostics.js +16 -0
  54. package/dist/utils/assert.js +3 -0
  55. package/docs/cn/context-engineering.md +389 -0
  56. package/docs/cn/language.md +478 -0
  57. package/docs/design-history/v0-design.md +365 -0
  58. package/docs/design-history/v0-implement.md +274 -0
  59. package/docs/design-history/v1-design.md +323 -0
  60. package/docs/design-history/v1-implement.md +267 -0
  61. package/docs/design-history/v2-design.md +387 -0
  62. package/docs/design-history/v2-implement.md +399 -0
  63. package/docs/en/context-engineering.md +332 -0
  64. package/docs/en/language.md +478 -0
  65. package/examples/changelog.as +29 -0
  66. package/examples/extract.as +29 -0
  67. package/examples/review.as +38 -0
  68. package/examples/summarize.as +28 -0
  69. package/examples/translate.as +33 -0
  70. package/package.json +59 -0
  71. package/tutorials/cli.as +22 -0
  72. package/tutorials/helloworld.as +14 -0
  73. package/tutorials/memory.as +19 -0
  74. package/tutorials/plan-execute.as +155 -0
  75. package/tutorials/react.as +98 -0
  76. package/tutorials/repl.as +31 -0
  77. package/tutorials/self-improve.as +60 -0
@@ -0,0 +1,387 @@
1
+ # AgentScript V2 Design
2
+
3
+ V2 的目标是引入可持久化、可审计、显式使用的 Agent Memory,使 reflection、self-improvement 和更谨慎的 self-evolution 模式可以用普通 AgentScript 组合出来。
4
+
5
+ V2 不把 `reflect`、`improve`、`evolve` 做成关键词。它们应继续是普通函数名、Agent 名或业务模式。语言层面只提供必要的 memory 原语。
6
+
7
+ `use`、作用域和 prompt context 的基础语义见 [`Context Engineering`](../cn/context-engineering.md)。Memory 必须遵守同一原则:memory 不会自动进入 prompt,只有查询结果被赋值为普通数据并显式 `use` 后,才会进入后续 `generate`。
8
+
9
+ ## 目标
10
+
11
+ V2 支持的核心能力:
12
+
13
+ - `import memory` 声明外部记忆体。
14
+ - file JSONL memory backend。
15
+ - sqlite memory backend。
16
+ - 最小 memory 操作:`add` 和 `query`。
17
+ - memory 操作写入 trace,便于审计。
18
+ - reflection / self-improvement 通过普通 Agent、函数和 memory 组合表达。
19
+
20
+ V2 不追求:
21
+
22
+ - 向量数据库。
23
+ - 任意 SQL 执行。
24
+ - 自动长期记忆。
25
+ - 自动记录所有 trace 到 memory。
26
+ - 自动修改 `.as` 源码。
27
+ - 通用 eval/harness DSL。
28
+ - 通用事务、回滚或并行 workflow。
29
+
30
+ ## Memory 定位
31
+
32
+ Memory 是跨运行持久化的上下文资产。
33
+
34
+ 它不同于 tool:
35
+
36
+ - tool 表示外部动作能力。
37
+ - memory 表示可读写的持久化数据源。
38
+
39
+ 它也不同于 file import:
40
+
41
+ - file import 是静态或半静态输入。
42
+ - memory 会被 AgentScript 显式写入,并影响未来运行。
43
+
44
+ ```agentscript
45
+ import memory Lessons from "file://./.agentscript/lessons.jsonl"
46
+ import memory Runs from "sqlite://./.agentscript/memory.db#runs"
47
+ ```
48
+
49
+ Memory binding 是 runtime capability,不能直接进入 prompt:
50
+
51
+ ```agentscript
52
+ use Lessons // invalid
53
+ ```
54
+
55
+ 必须先查询,得到普通 JSON/list 数据:
56
+
57
+ ```agentscript
58
+ past = Lessons.query({
59
+ text: input.goal
60
+ limit: 5
61
+ })
62
+
63
+ use past < 2k
64
+ ```
65
+
66
+ ## 最小 API
67
+
68
+ V2 memory 先只定义两个操作。
69
+
70
+ ### add
71
+
72
+ ```agentscript
73
+ Lessons.add({
74
+ kind: "lesson"
75
+ text: reflection.insight
76
+ goal: input.goal
77
+ ok: result.ok
78
+ })
79
+ ```
80
+
81
+ 语义:
82
+
83
+ - 追加一条记录。
84
+ - 参数必须是 JSON object。
85
+ - runtime 自动补充 `id`、`created_at` 和 `updated_at`。
86
+ - 返回写入后的记录 envelope。
87
+ - 写入 trace,但不自动进入 prompt。
88
+
89
+ 推荐返回结构:
90
+
91
+ ```json
92
+ {
93
+ "id": "...",
94
+ "created_at": "...",
95
+ "updated_at": "...",
96
+ "record": {
97
+ "kind": "lesson",
98
+ "text": "...",
99
+ "goal": "..."
100
+ }
101
+ }
102
+ ```
103
+
104
+ ### query
105
+
106
+ ```agentscript
107
+ past = Lessons.query({
108
+ text: input.goal
109
+ kind: "lesson"
110
+ limit: 5
111
+ })
112
+ ```
113
+
114
+ 语义:
115
+
116
+ - 返回 list。
117
+ - `limit` 可选,缺省值由 runtime 设定。
118
+ - `text` 可选,表示文本相关查询。
119
+ - `kind` 可选,表示记录类型过滤。
120
+ - `where` 可选,表示顶层字段精确匹配。
121
+ - 结果默认按最近写入优先。
122
+
123
+ 推荐查询参数:
124
+
125
+ ```agentscript
126
+ {
127
+ text: string
128
+ kind: string
129
+ where: json
130
+ limit: number
131
+ }
132
+ ```
133
+
134
+ V2 的 `text` 查询不承诺语义向量检索。file backend 可以先做大小写不敏感字符串匹配;sqlite backend 可以先做 `LIKE`,之后再升级 FTS5。
135
+
136
+ ## File Memory
137
+
138
+ file memory 使用 JSONL:
139
+
140
+ ```agentscript
141
+ import memory Lessons from "file://./.agentscript/lessons.jsonl"
142
+ ```
143
+
144
+ 每一行是一条 envelope:
145
+
146
+ ```json
147
+ {"id":"...","created_at":"...","updated_at":"...","record":{"kind":"lesson","text":"..."}}
148
+ ```
149
+
150
+ 规则:
151
+
152
+ - 文件不存在时可自动创建。
153
+ - 父目录不存在时可自动创建。
154
+ - 每次 `add` 追加一行 JSON。
155
+ - `query` 读取文件并过滤。
156
+ - 损坏 JSONL 行应报错,不静默跳过。
157
+ - 相对路径基于入口 `.as` 文件目录解析;REPL 中基于当前工作目录。
158
+
159
+ file memory 的价值是透明、易调试、适合早期 self-improvement。
160
+
161
+ ## SQLite Memory
162
+
163
+ sqlite memory 使用固定 schema:
164
+
165
+ ```agentscript
166
+ import memory Lessons from "sqlite://./.agentscript/memory.db#lessons"
167
+ ```
168
+
169
+ URI 语义:
170
+
171
+ - path 指向 sqlite 数据库文件。
172
+ - fragment 指向 memory namespace 或 table 名。
173
+ - fragment 为空时使用默认 namespace `memory`。
174
+
175
+ 推荐 schema:
176
+
177
+ ```sql
178
+ CREATE TABLE IF NOT EXISTS memory_records (
179
+ namespace TEXT NOT NULL,
180
+ id TEXT NOT NULL,
181
+ created_at TEXT NOT NULL,
182
+ updated_at TEXT NOT NULL,
183
+ kind TEXT,
184
+ text TEXT,
185
+ record_json TEXT NOT NULL,
186
+ PRIMARY KEY (namespace, id)
187
+ );
188
+ ```
189
+
190
+ 实现规则:
191
+
192
+ - AgentScript 不暴露任意 SQL。
193
+ - `add(record)` 写入 `record_json`,并从 record 顶层提取 `kind` 和 `text`。
194
+ - `query({ kind, text, where, limit })` 在固定 schema 上查询。
195
+ - `where` 只做 record 顶层字段精确匹配。
196
+ - 第一版可以用 `LIKE` 做 text 查询;FTS5 可作为后续增强。
197
+
198
+ SQLite backend 的价值是稳定、可扩展、适合长期运行。
199
+
200
+ ## Reflection 模式
201
+
202
+ Reflection 不需要新关键词。
203
+
204
+ ```agentscript
205
+ agent Reflector {
206
+ model Qwen
207
+ role "Reflector"
208
+ description "Extract reusable lessons from execution results."
209
+
210
+ main func(input) {
211
+ use input
212
+
213
+ return generate({
214
+ input: "Extract one reusable lesson from this run.",
215
+ attempts: 3
216
+ }) {
217
+ return {
218
+ insight string
219
+ mistake string
220
+ next_rule string
221
+ }
222
+ }
223
+ }
224
+ }
225
+ ```
226
+
227
+ 调用方决定是否写入 memory:
228
+
229
+ ```agentscript
230
+ reflection = Reflector({
231
+ goal: input.goal
232
+ result: result
233
+ })
234
+
235
+ Lessons.add({
236
+ kind: "lesson"
237
+ text: reflection.insight
238
+ goal: input.goal
239
+ next_rule: reflection.next_rule
240
+ })
241
+ ```
242
+
243
+ ## Self-Improvement 模式
244
+
245
+ Self-improvement 的 V2 定义是:
246
+
247
+ > Agent 显式读取过去 lesson,把相关 lesson 放入当前 prompt,并在运行结束后显式写入新的 lesson。
248
+
249
+ ```agentscript
250
+ import llm Qwen from "ollama://localhost:11434/qwen3.6"
251
+ import memory Lessons from "file://./.agentscript/lessons.jsonl"
252
+
253
+ main agent Learner {
254
+ model Qwen
255
+ role "Learning Agent"
256
+ description "Use durable lessons to improve future answers."
257
+
258
+ main func(input {
259
+ goal string
260
+ }) {
261
+ past = Lessons.query({
262
+ text: input.goal
263
+ kind: "lesson"
264
+ limit: 5
265
+ })
266
+
267
+ use input.goal
268
+ use past < 2k
269
+
270
+ result = generate({
271
+ input: "Answer the goal using relevant past lessons.",
272
+ attempts: 3
273
+ }) {
274
+ return {
275
+ ok boolean
276
+ answer string
277
+ reason string
278
+ }
279
+ }
280
+
281
+ reflection = reflect({
282
+ goal: input.goal
283
+ result: result
284
+ })
285
+
286
+ Lessons.add({
287
+ kind: "lesson"
288
+ text: reflection.insight
289
+ goal: input.goal
290
+ ok: result.ok
291
+ })
292
+
293
+ return result
294
+ }
295
+
296
+ func reflect(run) {
297
+ use run
298
+
299
+ return generate({
300
+ input: "Extract one reusable lesson from this run.",
301
+ attempts: 3
302
+ }) {
303
+ return {
304
+ insight string
305
+ }
306
+ }
307
+ }
308
+ }
309
+ ```
310
+
311
+ 这里的改进来自 memory,而不是隐式上下文膨胀。
312
+
313
+ ## Self-Evolution 边界
314
+
315
+ V2 只支持保守的 self-evolution:
316
+
317
+ - 生成 rule、preference、lesson、profile 等可持久化数据。
318
+ - 下一次运行通过 memory 查询和 `use` 显式影响行为。
319
+ - 可以生成 patch proposal,但不自动修改源代码。
320
+
321
+ V2 不支持:
322
+
323
+ - Agent 自动改写自己的 `.as` 文件。
324
+ - 自动安装工具或依赖。
325
+ - 自动改变 import URI。
326
+ - 绕过 host 授权写入任意路径。
327
+
328
+ 原因:
329
+
330
+ - 代码自修改需要更强的权限模型、审计和回滚。
331
+ - memory 更新已经足够覆盖大多数 self-improvement 场景。
332
+ - AgentScript 的核心仍是 context engineering,而不是 autonomous code mutation。
333
+
334
+ ## Trace
335
+
336
+ Memory 操作必须写入 trace:
337
+
338
+ ```json
339
+ {
340
+ "kind": "memory",
341
+ "data": {
342
+ "memory": "Lessons",
343
+ "operation": "query",
344
+ "uri": "file://./.agentscript/lessons.jsonl",
345
+ "args": { "kind": "lesson", "limit": 5 },
346
+ "count": 3
347
+ }
348
+ }
349
+ ```
350
+
351
+ 写入 trace:
352
+
353
+ ```json
354
+ {
355
+ "kind": "memory",
356
+ "data": {
357
+ "memory": "Lessons",
358
+ "operation": "add",
359
+ "id": "...",
360
+ "record": { "kind": "lesson", "text": "..." }
361
+ }
362
+ }
363
+ ```
364
+
365
+ trace 是审计产物,不会自动进入 prompt。
366
+
367
+ ## 安全边界
368
+
369
+ V2 memory 需要 host runtime 控制:
370
+
371
+ - file/sqlite 路径必须限制在 workspace 或明确授权目录内。
372
+ - `file://` memory 只能写 JSONL。
373
+ - `sqlite://` memory 只能操作固定 schema。
374
+ - 不支持 `sqlite://...` 任意 SQL。
375
+ - memory binding 不能被 `use`。
376
+ - memory 查询结果是普通数据,可以被 `use`。
377
+
378
+ ## 与 V1 的关系
379
+
380
+ V1 的 Plan-and-Execute、Evaluator-Optimizer 和 Multi-agent composition 可以直接叠加 V2 memory:
381
+
382
+ - Planner 查询过去 plan 失败经验。
383
+ - Executor 查询工具调用注意事项。
384
+ - Verifier 写入失败样本。
385
+ - Controller 写入 run summary。
386
+
387
+ V2 不改变 V1 控制流,也不增加模式专用关键词。