@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,478 @@
1
+ # AgentScript 语言参考
2
+
3
+ 本文档描述 AgentScript v1.0.0 的当前语言规范。
4
+
5
+ ## 设计原则
6
+
7
+ - Agent 是执行和 prompt 身份的基本单元。
8
+ - 上下文显式化:普通变量、工具结果、memory 记录和 trace 事件不会自动进入 prompt,除非通过 `use` 显式选择。
9
+ - 作用域控制变量生命周期、上下文继承和 prompt 暴露边界。
10
+ - import 的 llm、tool、memory、file 和 agent 是具有明确边界的运行时能力。
11
+ - planner、executor、verifier、reflect、improve 和 evolve 等模式名保持为普通标识符。
12
+ - Trace 是调试和审计产物,不是 prompt context。
13
+
14
+ ## 程序结构
15
+
16
+ 程序由 import 声明和 agent 声明组成。执行从选定的 agent 和函数开始,或者从程序的 `main agent` 和 `main func` 开始。
17
+
18
+ ```agentscript
19
+ import llm Qwen from "ollama://localhost:11434/qwen3.6"
20
+
21
+ main agent Assistant {
22
+ model Qwen
23
+ role "Assistant"
24
+ description "Answer with structured JSON."
25
+
26
+ main func(input {
27
+ question string
28
+ }) {
29
+ use input.question
30
+ return generate({ input: "Answer the question" }) {
31
+ return {
32
+ ok boolean
33
+ answer string
34
+ }
35
+ }
36
+ }
37
+ }
38
+ ```
39
+
40
+ ## Import
41
+
42
+ AgentScript 支持五种资源类型:
43
+
44
+ ```agentscript
45
+ import llm Qwen from "ollama://localhost:11434/qwen3.6"
46
+ import tool Find from "sh://find"
47
+ import memory Lessons from "file://./.agentscript/lessons.jsonl"
48
+ import file Requirements from "./requirements.md"
49
+ import agent Worker from "./worker.as"
50
+ ```
51
+
52
+ 资源绑定是显式能力声明。`llm`、`tool`、`memory`、`agent` 绑定不能通过 `use` 直接加入 prompt context。File import 是只读上下文资源,仍需显式 `use`。
53
+
54
+ ### LLM URI
55
+
56
+ ```agentscript
57
+ import llm Fast from "openai://gpt-4.1-mini"
58
+ import llm Strong from "anthropic://claude-sonnet-4-0"
59
+ import llm Local from "ollama://localhost:11434/qwen3.6"
60
+ ```
61
+
62
+ 环境变量:
63
+ - `OPENAI_API_KEY` / `OPENAI_BASE_URL`
64
+ - `ANTHROPIC_API_KEY` / `ANTHROPIC_BASE_URL`
65
+ - `OLLAMA_BASE_URL`
66
+
67
+ ### Tool URI
68
+
69
+ 工具使用 URI scheme 进行分发:
70
+
71
+ | Scheme | Provider | 示例 |
72
+ |--------|----------|------|
73
+ | `sh://` | 基于 shell 的工具 | `sh://find`, `sh://grep` |
74
+ | `file://` | 文件操作 | `file://workspace` |
75
+ | `env://` | 环境变量 | `env://process` |
76
+ | `http://` / `https://` | HTTP 请求 | `https://api.example.com` |
77
+ | `mcp://` | 外部 MCP 工具 | `mcp://tools/search` |
78
+
79
+ ### Memory URI
80
+
81
+ ```agentscript
82
+ import memory Lessons from "file://./.agentscript/lessons.jsonl"
83
+ import memory Runs from "sqlite://./.agentscript/memory.db#runs"
84
+ ```
85
+
86
+ File memory 使用 JSONL 格式。SQLite memory 使用固定 schema,支持命名空间隔离。
87
+
88
+ ### File URI
89
+
90
+ 相对路径基于导入的 `.as` 文件目录解析(REPL 中基于当前工作目录)。路径访问限制在 workspace 根目录内。
91
+
92
+ ## Agent 和函数
93
+
94
+ Agent 包含配置和函数。函数创建运行时作用域。跨 Agent 调用创建独立的函数作用域和嵌套的 trace 事件。
95
+
96
+ ```agentscript
97
+ result = Worker(input)
98
+ result = Worker.run(input)
99
+ ```
100
+
101
+ `AgentName(input)` 调用目标 agent 的 `main func`。`AgentName.funcName(input)` 调用指定的命名函数。
102
+
103
+ ### 入口规则
104
+
105
+ - 一个程序最多一个 `main agent`。
106
+ - 一个 agent 最多一个 `main func`。
107
+ - 多 Agent 程序必须声明 `main agent`。
108
+ - `main agent { ... }` 可以省略 agent 名称。
109
+ - `main func(input) { ... }` 可以省略函数名称。
110
+
111
+ ## 配置
112
+
113
+ `model`、`role`、`description` 是作用域配置值,供 `generate` 使用。
114
+
115
+ ```agentscript
116
+ agent A {
117
+ model Qwen
118
+ role "Researcher"
119
+ description "Collect facts and answer carefully."
120
+ }
121
+ ```
122
+
123
+ 子作用域可以覆盖这些值:
124
+
125
+ ```agentscript
126
+ func careful(input) {
127
+ model Strong
128
+ role "Specialist"
129
+ ...
130
+ }
131
+ ```
132
+
133
+ ## 值和 Shape
134
+
135
+ 运行时值以 JSON 为核心:
136
+
137
+ - `string`、`number`、`boolean`、`none`
138
+ - `list`、`object`
139
+
140
+ Shape 用于输入校验和 `generate` 输出校验:
141
+
142
+ ```agentscript
143
+ return generate({ input: "Extract facts" }) {
144
+ return {
145
+ ok boolean
146
+ title string
147
+ items list[json]
148
+ meta json
149
+ }
150
+ }
151
+ ```
152
+
153
+ 支持的 shape 类型:`string`、`number`、`boolean`、`json`、`list`、`list[T]`(T 为任意支持的类型)。
154
+
155
+ Shape 不是完整的静态类型系统。
156
+
157
+ ## 显式上下文(`use`)
158
+
159
+ `use` 选择在当前作用域及其子作用域中,后续 `generate` 可以包含哪些变量的值。
160
+
161
+ ```agentscript
162
+ use input.question
163
+ use Requirements < 4k
164
+ use past_lessons < 2k
165
+ ```
166
+
167
+ ### 规则
168
+
169
+ - 未被 `use` 的变量不会进入 prompt。
170
+ - 工具输出不会自动进入 prompt。
171
+ - Memory 查询结果不会自动进入 prompt。
172
+ - Trace 事件不会自动进入 prompt。
173
+ - `use value < n` 应用上下文预算。
174
+ - `llm`、`tool`、`agent`、`memory` 绑定不能被 `use`。
175
+ - 函数绑定不能被 `use`。
176
+ - `use` 声明被子作用域继承。
177
+
178
+ ### 延迟求值
179
+
180
+ `use expr < budget` 声明的是 context source,而不是当前值的快照。当 `generate` 构建 prompt 时,表达式会被重新求值。这意味着在 `use` 之后、`generate` 之前对变量的修改在生成时刻是可见的。
181
+
182
+ ## Generate
183
+
184
+ `generate` 调用当前模型,需要 `input` 指令和返回 shape。
185
+
186
+ ```agentscript
187
+ answer = generate({
188
+ input: "Answer using the selected context."
189
+ limit: 800
190
+ attempts: 3
191
+ debug: true
192
+ }) {
193
+ return {
194
+ ok boolean
195
+ answer string
196
+ reason string
197
+ }
198
+ }
199
+ ```
200
+
201
+ ### 语义
202
+
203
+ - `input`:每次生成的指令。必填。
204
+ - `limit`:生成预算(数字或 `2k` 格式)。可选。
205
+ - `attempts`:JSON 解析失败或 shape 不匹配时的重试次数。可选,默认 1。
206
+ - `debug`:将完整 prompt 打印到 stderr。可选,默认 false。
207
+ - `return { ... }` 块声明期望的输出 shape。
208
+ - Provider 错误(认证、网络、超时、模型不存在)直接失败,不做重试。
209
+ - Shape 校验包含类型强制转换(如 `"true"` -> `true`,`"42"` -> `42`)。
210
+
211
+ ## 控制流
212
+
213
+ ### If / else
214
+
215
+ ```agentscript
216
+ if answer.ok and not input.dry_run {
217
+ return answer
218
+ } else {
219
+ return fallback(answer)
220
+ }
221
+ ```
222
+
223
+ 支持的运算符:`==`、`!=`、`and`、`or`、`not`。`<` 不是通用比较运算符——它只能用于预算和循环上限。
224
+
225
+ ### Loop until
226
+
227
+ ```agentscript
228
+ done = false
229
+
230
+ loop until done < 6 {
231
+ observation = observe(input)
232
+ done = observation.ok
233
+ }
234
+ ```
235
+
236
+ 每轮开始时检查条件。条件为真或达到迭代上限时退出。
237
+
238
+ ### Repeat
239
+
240
+ ```agentscript
241
+ repeat * 3 {
242
+ result = attempt(input)
243
+ if result.ok {
244
+ return result
245
+ }
246
+ }
247
+ ```
248
+
249
+ 每次迭代创建子作用域。外层已有的变量被更新时跨迭代保留。迭代内新建的变量在每次结束后丢弃。
250
+
251
+ ### For in
252
+
253
+ ```agentscript
254
+ for step in plan.steps < 12 {
255
+ result = Executor(step)
256
+ results.add(result)
257
+ }
258
+ ```
259
+
260
+ 列表在循环开始时只求值一次。每次迭代创建子作用域。循环变量作用域限定在循环体内。
261
+
262
+ ## 列表和 JSON 辅助
263
+
264
+ ```agentscript
265
+ first = items[0]
266
+ count = items.length
267
+ items.add(new_item)
268
+ summary = items.summary
269
+ ```
270
+
271
+ - `list[index]` 只读。index 必须是非负整数。
272
+ - `list.add(value)` 修改原列表。恰好接受一个参数。
273
+ - `.length` 返回列表长度。
274
+ - `.summary` 返回列表的 JSON-safe 运行时视图。它不是 LLM 生成的摘要,也不是语义压缩;如果需要控制 prompt 大小,应通过 `use scratch.summary < 2k` 这类显式 context budget 裁剪。
275
+
276
+ ## 工具
277
+
278
+ 工具通过 URI scheme 导入,使用结构化 JSON 参数调用。
279
+
280
+ ```agentscript
281
+ import tool Find from "sh://find"
282
+ import tool Grep from "sh://grep"
283
+ import tool File from "file://workspace"
284
+ import tool Env from "env://process"
285
+ import tool Http from "https://api.example.com"
286
+ ```
287
+
288
+ ### Find
289
+
290
+ ```agentscript
291
+ files = Find.run({
292
+ path: "."
293
+ name: "*.ts"
294
+ type: "file"
295
+ max: 50
296
+ })
297
+ ```
298
+
299
+ ### Grep
300
+
301
+ ```agentscript
302
+ matches = Grep.run({
303
+ path: "src"
304
+ pattern: "TODO"
305
+ include: "*.ts"
306
+ max: 100
307
+ })
308
+ ```
309
+
310
+ ### Sed
311
+
312
+ ```agentscript
313
+ lines = Sed.run({
314
+ path: "src/main.as"
315
+ start: 1
316
+ max: 20
317
+ })
318
+ ```
319
+
320
+ ### File
321
+
322
+ ```agentscript
323
+ content = File.read({ path: "README.md" })
324
+ entries = File.list({ path: "src" })
325
+ result = File.write({ path: "output.md", content: "# Result" })
326
+ result = File.patch({ path: "file.as", search: "old", replace: "new" })
327
+ result = File.undo(effects)
328
+ ```
329
+
330
+ 写和 patch 操作返回可撤销的 effect 记录。`File.undo` 接受 effect 记录列表并逆转。
331
+
332
+ ### Env
333
+
334
+ ```agentscript
335
+ home = Env.get({ name: "HOME" })
336
+ ```
337
+
338
+ ### Http
339
+
340
+ ```agentscript
341
+ response = Http.get({ url: "/api/data", headers: { Authorization: "Bearer ..." }, timeout: 10000 })
342
+ response = Http.post({ url: "/api/submit", body: { key: "value" }, timeout: 10000 })
343
+ ```
344
+
345
+ HTTP 请求限制在 import URI 的 origin 内。
346
+
347
+ ### 安全
348
+
349
+ - 禁止通用 shell 入口(`sh://sh`、`sh://bash`、`sh://zsh`、`sh://fish`)。
350
+ - 文件路径限制在 workspace 根目录内。
351
+ - 逃逸 workspace 的符号链接不会被跟随。
352
+ - 写操作返回 effect 记录用于审计和撤销。
353
+
354
+ ## Memory
355
+
356
+ Memory 提供跨运行的持久化、显式、可审计状态。
357
+
358
+ ```agentscript
359
+ import memory Lessons from "file://./.agentscript/lessons.jsonl"
360
+ import memory Runs from "sqlite://./.agentscript/memory.db#runs"
361
+ ```
362
+
363
+ ## 文件导入
364
+
365
+ 文件导入将本地文件作为显式上下文资源加载。
366
+
367
+ ```agentscript
368
+ import file Requirements from "./requirements.md"
369
+ import file Config from "./config.json"
370
+
371
+ func answer(input) {
372
+ use Requirements < 4k
373
+ use Config
374
+ return generate({ input: "Answer from the referenced file." }) {
375
+ return {
376
+ ok boolean
377
+ answer string
378
+ }
379
+ }
380
+ }
381
+ ```
382
+
383
+ 文本文件作为字符串加载。JSON 文件解析为 JSON 值。文件内容必须显式 `use` 才能进入 prompt context。
384
+
385
+ ### API
386
+
387
+ ```agentscript
388
+ Lessons.add({
389
+ kind: "lesson"
390
+ text: reflection.insight
391
+ goal: input.goal
392
+ })
393
+
394
+ past = Lessons.query({
395
+ kind: "lesson"
396
+ text: input.goal
397
+ limit: 5
398
+ })
399
+
400
+ use past < 2k
401
+ ```
402
+
403
+ ### 规则
404
+
405
+ - Memory 绑定不能直接用 `use` 作为 prompt context。
406
+ - 查询结果是普通数据,必须显式 `use`。
407
+ - `add` 接受一个对象参数。运行时自动补充 `id`、`created_at`、`updated_at`。
408
+ - `query` 支持 `text`(大小写不敏感子串匹配)、`kind`(精确匹配)、`where`(精确字段匹配)和 `limit`。
409
+ - File memory 使用 JSONL 格式,文件和父目录自动创建。
410
+ - SQLite memory 使用固定 schema,不暴露任意 SQL。
411
+
412
+ ## Agent 组合
413
+
414
+ 多 Agent 组合通过 import、函数调用和显式参数传递实现。
415
+
416
+ ```agentscript
417
+ import agent Planner from "./planner.as"
418
+ import agent Executor from "./executor.as"
419
+
420
+ main agent Controller {
421
+ main func(input) {
422
+ plan = Planner(input)
423
+ results = []
424
+ for step in plan.steps < 10 {
425
+ result = Executor({ goal: input.goal, step: step })
426
+ results.add(result)
427
+ }
428
+ return results.summary
429
+ }
430
+ }
431
+ ```
432
+
433
+ 每个 Agent 调用创建独立作用域。上下文边界永远不会隐式跨越。Trace 事件嵌套记录。
434
+
435
+ ## 保留词
436
+
437
+ `import`、`from`、`main`、`agent`、`func`、`use`、`loop`、`until`、`repeat`、`for`、`in`、`return`、`if`、`else`、`and`、`or`、`not`、`generate`、`true`、`false`、`none`、`string`、`number`、`boolean`、`json`、`list`。
438
+
439
+ 以下不是保留词:`input`、`act`、`reason`、`observe`、`reflect`、`answer`、`scratch`、`done`、`task`、`output`、`context`、`repair`。
440
+
441
+ ## 执行模型
442
+
443
+ ```text
444
+ 源码 -> tokenizer -> parser -> AST -> semantic analyzer -> interpreter -> trace + result
445
+ ```
446
+
447
+ 解释器是树遍历求值器。无 IR、字节码或编译步骤。
448
+
449
+ ## 模块
450
+
451
+ | 模块 | 路径 | 职责 |
452
+ |------|------|------|
453
+ | Tokenizer | `src/parser/tokenizer.ts` | 词法分析 |
454
+ | Parser | `src/parser/parser.ts` | 递归下降解析 |
455
+ | Semantic analyzer | `src/semantic/analyzer.ts` | 静态语义检查 |
456
+ | Interpreter | `src/runtime/interpreter.ts` | 入口、Agent/函数调用 |
457
+ | Evaluator | `src/runtime/evaluator.ts` | 表达式求值、工具调度、use 解析 |
458
+ | Generator | `src/runtime/generate.ts` | Generate 执行、修复、context 构建 |
459
+ | Scope | `src/runtime/scope.ts` | 变量作用域、配置、use 声明 |
460
+ | Context builder | `src/runtime/context.ts` | Prompt 构建、裁剪 |
461
+ | LLM provider | `src/providers/llm/` | OpenAI、Anthropic、Ollama |
462
+ | Memory provider | `src/providers/memory/` | File JSONL、SQLite |
463
+ | Tool provider | `src/providers/tools/` | 宿主工具实现 |
464
+ | CLI | `src/bin/agentscript.ts` | 命令行接口 |
465
+ | REPL | `src/bin/repl.ts` | 交互式 REPL |
466
+
467
+ ## 非目标
468
+
469
+ AgentScript v1.0.0 不包含:
470
+
471
+ - 通用工作流引擎。
472
+ - 通用并行执行语法。
473
+ - 通用事务或自动回滚。
474
+ - 任意 SQL 执行。
475
+ - 自动长期记忆。
476
+ - 自动捕获局部变量到 prompt。
477
+ - 自动修改 `.as` 源码。
478
+ - 完整静态类型系统。