@rong/agentscript 0.1.0 → 0.1.2

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.
@@ -0,0 +1,197 @@
1
+ # AgentScript 隐式返回规范草案
2
+
3
+ 本文档描述 AgentScript 函数的隐式返回规则草案。
4
+
5
+ ## 1. 基本规则
6
+
7
+ 函数体最后一个顶层表达式可以作为函数返回值。
8
+
9
+ ```agentscript
10
+ func answer(input) {
11
+ use input.question
12
+
13
+ generate({ input: "Answer the question" }) -> {
14
+ ok boolean
15
+ answer string
16
+ }
17
+ }
18
+ ```
19
+
20
+ 等价于:
21
+
22
+ ```agentscript
23
+ func answer(input) {
24
+ use input.question
25
+
26
+ return generate({ input: "Answer the question" }) -> {
27
+ ok boolean
28
+ answer string
29
+ }
30
+ }
31
+ ```
32
+
33
+ 这个规则称为:
34
+
35
+ ```text
36
+ final expression return
37
+ ```
38
+
39
+ ## 2. 可隐式返回的表达式
40
+
41
+ 允许最后一行隐式返回以下表达式形式:
42
+
43
+ ```text
44
+ generate(...) -> shape
45
+ 普通函数调用
46
+ agent 调用
47
+ 变量引用
48
+ 字段访问
49
+ 索引访问
50
+ object literal
51
+ list literal
52
+ ```
53
+
54
+ 例如:
55
+
56
+ ```agentscript
57
+ func run(input) {
58
+ Worker(input)
59
+ }
60
+ ```
61
+
62
+ ```agentscript
63
+ func get_result(result) {
64
+ result.value
65
+ }
66
+ ```
67
+
68
+ ```agentscript
69
+ func observe(action) {
70
+ {
71
+ facts: [action.summary],
72
+ source: action.source
73
+ }
74
+ }
75
+ ```
76
+
77
+ ## 3. 不可隐式返回的语句
78
+
79
+ 下面这些不是表达式,不参与隐式返回:
80
+
81
+ ```text
82
+ use 声明
83
+ 赋值语句
84
+ import
85
+ loop
86
+ repeat
87
+ for
88
+ if/else,早期版本可先不表达式化
89
+ ```
90
+
91
+ 例如:
92
+
93
+ ```agentscript
94
+ func bad(input) {
95
+ use input.question
96
+ }
97
+ ```
98
+
99
+ 这里没有返回表达式,函数返回 `none`,或由 semantic analyzer 给出 warning。
100
+
101
+ 赋值也不返回:
102
+
103
+ ```agentscript
104
+ func f() {
105
+ x = answer()
106
+ }
107
+ ```
108
+
109
+ 如果要返回,需要写:
110
+
111
+ ```agentscript
112
+ func f() {
113
+ x = answer()
114
+ x
115
+ }
116
+ ```
117
+
118
+ ## 4. 显式 `return` 优先
119
+
120
+ 显式 `return` 仍然合法,并且在复杂控制流中推荐使用。
121
+
122
+ ```agentscript
123
+ func answer(input) {
124
+ if input.dry_run {
125
+ return {
126
+ ok: false,
127
+ answer: "dry run"
128
+ }
129
+ }
130
+
131
+ generate({ input: "Answer" }) -> {
132
+ ok boolean
133
+ answer string
134
+ }
135
+ }
136
+ ```
137
+
138
+ ## 5. 无返回值
139
+
140
+ 如果函数没有显式 `return`,最后一行也不是可返回表达式,则返回:
141
+
142
+ ```agentscript
143
+ none
144
+ ```
145
+
146
+ 可以显式写:
147
+
148
+ ```agentscript
149
+ return none
150
+ ```
151
+
152
+ 用于表达“此函数只产生副作用”。
153
+
154
+ ## 6. 推荐风格
155
+
156
+ 对于典型 LLM 调用,推荐省略 `return`:
157
+
158
+ ```agentscript
159
+ func summarize(content) {
160
+ use content < 8k
161
+
162
+ generate({
163
+ input: "Summarize the content"
164
+ limit: 1000
165
+ }) -> {
166
+ title string
167
+ summary string
168
+ key_points list[string]
169
+ }
170
+ }
171
+ ```
172
+
173
+ 对于分支、提前退出、错误处理,推荐使用显式 `return`:
174
+
175
+ ```agentscript
176
+ func answer(input) {
177
+ if not input.question {
178
+ return {
179
+ ok: false,
180
+ answer: ""
181
+ }
182
+ }
183
+
184
+ use input.question
185
+
186
+ generate({ input: "Answer the question" }) -> {
187
+ ok boolean
188
+ answer string
189
+ }
190
+ }
191
+ ```
192
+
193
+ ## 一句话定义
194
+
195
+ ```text
196
+ A function returns the value of its final top-level expression when no explicit return is reached.
197
+ ```
@@ -1,6 +1,6 @@
1
1
  # AgentScript 语言参考
2
2
 
3
- 本文档描述 AgentScript v1.0.0 的当前语言规范。
3
+ 本文档描述 AgentScript v0.1.x 的当前语言规范。
4
4
 
5
5
  ## 设计原则
6
6
 
@@ -27,11 +27,9 @@ main agent Assistant {
27
27
  question string
28
28
  }) {
29
29
  use input.question
30
- return generate({ input: "Answer the question" }) {
31
- return {
32
- ok boolean
33
- answer string
34
- }
30
+ generate({ input: "Answer the question" }) -> {
31
+ ok boolean
32
+ answer string
35
33
  }
36
34
  }
37
35
  }
@@ -140,13 +138,11 @@ func careful(input) {
140
138
  Shape 用于输入校验和 `generate` 输出校验:
141
139
 
142
140
  ```agentscript
143
- return generate({ input: "Extract facts" }) {
144
- return {
145
- ok boolean
146
- title string
147
- items list[json]
148
- meta json
149
- }
141
+ generate({ input: "Extract facts" }) -> {
142
+ ok boolean
143
+ title string
144
+ items list[json]
145
+ meta json
150
146
  }
151
147
  ```
152
148
 
@@ -181,7 +177,7 @@ use past_lessons < 2k
181
177
 
182
178
  ## Generate
183
179
 
184
- `generate` 调用当前模型,需要 `input` 指令和返回 shape
180
+ `generate` 调用当前模型,需要 `input` 指令。返回 shape 是可选的。
185
181
 
186
182
  ```agentscript
187
183
  answer = generate({
@@ -189,12 +185,10 @@ answer = generate({
189
185
  limit: 800
190
186
  attempts: 3
191
187
  debug: true
192
- }) {
193
- return {
194
- ok boolean
195
- answer string
196
- reason string
197
- }
188
+ }) -> {
189
+ ok boolean
190
+ answer string
191
+ reason string
198
192
  }
199
193
  ```
200
194
 
@@ -204,7 +198,8 @@ answer = generate({
204
198
  - `limit`:生成预算(数字或 `2k` 格式)。可选。
205
199
  - `attempts`:JSON 解析失败或 shape 不匹配时的重试次数。可选,默认 1。
206
200
  - `debug`:将完整 prompt 打印到 stderr。可选,默认 false。
207
- - `return { ... }` 块声明期望的输出 shape。
201
+ - 可选的 `-> { ... }` 块声明期望的输出 shape。
202
+ - 不写 `-> { ... }` 时,`generate` 输出无约束:AgentScript 不会在 prompt 中加入返回 schema,不会要求 provider 使用结构化输出,也不会对返回值做类型强制转换或 shape 校验。
208
203
  - Provider 错误(认证、网络、超时、模型不存在)直接失败,不做重试。
209
204
  - Shape 校验包含类型强制转换(如 `"true"` -> `true`,`"42"` -> `42`)。
210
205
 
@@ -371,11 +366,9 @@ import file Config from "./config.json"
371
366
  func answer(input) {
372
367
  use Requirements < 4k
373
368
  use Config
374
- return generate({ input: "Answer from the referenced file." }) {
375
- return {
376
- ok boolean
377
- answer string
378
- }
369
+ generate({ input: "Answer from the referenced file." }) -> {
370
+ ok boolean
371
+ answer string
379
372
  }
380
373
  }
381
374
  ```
@@ -425,7 +418,7 @@ main agent Controller {
425
418
  result = Executor({ goal: input.goal, step: step })
426
419
  results.add(result)
427
420
  }
428
- return results.summary
421
+ results.summary
429
422
  }
430
423
  }
431
424
  ```
@@ -466,7 +459,7 @@ main agent Controller {
466
459
 
467
460
  ## 非目标
468
461
 
469
- AgentScript v1.0.0 不包含:
462
+ AgentScript v0.1.x 不包含:
470
463
 
471
464
  - 通用工作流引擎。
472
465
  - 通用并行执行语法。
@@ -20,7 +20,7 @@ V0 支持:
20
20
  - Agent 内函数、变量赋值、对象/列表字面量、成员访问、函数调用。
21
21
  - `model`、`role`、`description` 作用域配置。
22
22
  - `use` 上下文声明和 `< n` 上下文预算。
23
- - `generate({ input, limit, attempts, debug }) { return { ... } }` LLM 调用。
23
+ - `generate({ input, limit, attempts, debug }) -> { ... }` LLM 调用。
24
24
  - `if` / `else`,`==`、`!=`、`and`、`or`、`not`。
25
25
  - `loop until condition < n` 有上限循环。
26
26
  - `repeat * n` 有上限重复执行。
@@ -50,17 +50,15 @@ main agent ResearchAgent {
50
50
  question string
51
51
  }) {
52
52
  use input.question
53
- return answer(input.question)
53
+ answer(input.question)
54
54
  }
55
55
 
56
56
  func answer(question) {
57
57
  use question
58
58
 
59
- return generate({ input: "Answer the question" }) {
60
- return {
61
- ok boolean
62
- text string
63
- }
59
+ generate({ input: "Answer the question" }) -> {
60
+ ok boolean
61
+ text string
64
62
  }
65
63
  }
66
64
  }
@@ -105,10 +103,8 @@ agent A {
105
103
  model Strong
106
104
  description "Use a stronger model for this function."
107
105
 
108
- return generate({ input: "Answer carefully" }) {
109
- return {
110
- text string
111
- }
106
+ generate({ input: "Answer carefully" }) -> {
107
+ text string
112
108
  }
113
109
  }
114
110
  }
@@ -134,13 +130,11 @@ V0 运行时数据以 JSON 为核心:
134
130
  `generate` 返回 shape 使用轻量标注:
135
131
 
136
132
  ```agentscript
137
- return generate({ input: "Extract facts" }) {
138
- return {
139
- facts list[string]
140
- source string
141
- meta json
142
- ok boolean
143
- }
133
+ generate({ input: "Extract facts" }) -> {
134
+ facts list[string]
135
+ source string
136
+ meta json
137
+ ok boolean
144
138
  }
145
139
  ```
146
140
 
@@ -155,11 +149,9 @@ func compose(question, scratch) {
155
149
  use question
156
150
  use scratch.summary < 2k
157
151
 
158
- return generate({ input: "Answer using only the context" }) {
159
- return {
160
- ok boolean
161
- text string
162
- }
152
+ generate({ input: "Answer using only the context" }) -> {
153
+ ok boolean
154
+ text string
163
155
  }
164
156
  }
165
157
  ```
@@ -175,19 +167,17 @@ func compose(question, scratch) {
175
167
 
176
168
  ## Generate
177
169
 
178
- `generate({ input, limit, attempts, debug }) { return { ... } }` 表示一次 LLM call。`input` 是本次 call 的最后用户指令,可以是字符串、对象或其它 JSON 值。`limit`、`attempts`、`debug` 都是可选参数。
170
+ `generate({ input, limit, attempts, debug }) -> { ... }` 表示一次 LLM call。`input` 是本次 call 的最后用户指令,可以是字符串、对象或其它 JSON 值。`limit`、`attempts`、`debug` 都是可选参数。
179
171
 
180
172
  ```agentscript
181
173
  answer = generate({
182
174
  input: "Answer using collected facts"
183
175
  limit: 800
184
176
  attempts: 3
185
- }) {
186
- return {
187
- ok boolean
188
- text string
189
- error string
190
- }
177
+ }) -> {
178
+ ok boolean
179
+ text string
180
+ error string
191
181
  }
192
182
  ```
193
183
 
@@ -198,7 +188,7 @@ answer = generate({
198
188
  - `limit` 是本次 LLM call 的预算,支持 `800` 或 `2k` 这类 budget 字面量。
199
189
  - `attempts` 表示最多生成次数;它不是额外重试次数。
200
190
  - `debug` 是 boolean,缺省值为 `false`;为 `true` 时 runtime 将完整 prompt 打印到 stderr。
201
- - 块内 `return { ... }` 描述 LLM 输出 JSON shape,不会从外层函数返回。
191
+ - `-> { ... }` 描述 LLM 输出 JSON shape,不会从外层函数返回。
202
192
  - LLM 输出会先按 shape 做有限容错转换。
203
193
  - 当输出不是 JSON 或转换后仍不符合 shape,且 `attempts > 1` 时,下一次调用会把上一次输出和错误信息附加到 `input` 后,请模型 repair。
204
194
  - provider 网络、认证、超时、模型不存在等基础设施错误不会被 repair 重试。
@@ -1,6 +1,6 @@
1
1
  # AgentScript V0 Implement
2
2
 
3
- 本文档是 V0 阶段的历史实现快照。语言设计见 `v0-design.md`。当前 v1.0.0 的实现入口和模块概览见 `../en/language.md` 与 `../cn/language.md`。
3
+ 本文档是 V0 阶段的历史实现快照。语言设计见 `v0-design.md`。当前 v0.1.x 的实现入口和模块概览见 `../en/language.md` 与 `../cn/language.md`。
4
4
 
5
5
  ## 执行管线
6
6
 
@@ -92,7 +92,7 @@ V0 AST 节点:
92
92
  - `main agent { ... }` 映射为匿名入口 Agent,内部名 `__main_agent`。
93
93
  - `main func(input) { ... }` 映射为匿名入口函数,内部名 `__main`。
94
94
  - `AgentName(input)` 是普通 `CallExpr`,语义和运行时把它解析为目标 Agent 的 `main func` 调用。
95
- - `generate({ input: "...", limit: 500 }) { return { ... } }` 把生成预算保存在 `GenerateExpr` options 中。
95
+ - `generate({ input: "...", limit: 500 }) -> { ... }` 把生成预算保存在 `GenerateExpr` options 中。
96
96
  - `use value < 2k` 的预算在 `UseStmt` 上。
97
97
  - `loop until done < 6` 的 `< 6` 是循环上限,不是比较表达式。
98
98
 
@@ -198,7 +198,7 @@ V0 的预算裁剪按字符数实现:`2k` 约等于 2000 字符。这是当前
198
198
 
199
199
  ```ts
200
200
  interface ToolProvider {
201
- call(request: ToolCallRequest): Promise<RuntimeValue>;
201
+ call(request: ToolCallRequest): Promise<RuntimeValue>;
202
202
  }
203
203
  ```
204
204
 
@@ -113,10 +113,8 @@ for item in list < n {
113
113
  V1 不需要专门的 `plan` 类型。推荐结构:
114
114
 
115
115
  ```agentscript
116
- return generate({ input: "Create a short executable plan" }) {
117
- return {
118
- steps list[json]
119
- }
116
+ generate({ input: "Create a short executable plan" }) -> {
117
+ steps list[json]
120
118
  }
121
119
  ```
122
120
 
@@ -280,11 +278,9 @@ func answer(input) {
280
278
  use Requirements < 4k
281
279
  use input.question
282
280
 
283
- return generate({ input: "Answer from the referenced file" }) {
284
- return {
285
- ok boolean
286
- text string
287
- }
281
+ generate({ input: "Answer from the referenced file" }) -> {
282
+ ok boolean
283
+ text string
288
284
  }
289
285
  }
290
286
  ```
@@ -210,15 +210,13 @@ agent Reflector {
210
210
  main func(input) {
211
211
  use input
212
212
 
213
- return generate({
213
+ generate({
214
214
  input: "Extract one reusable lesson from this run.",
215
215
  attempts: 3
216
- }) {
217
- return {
218
- insight string
219
- mistake string
220
- next_rule string
221
- }
216
+ }) -> {
217
+ insight string
218
+ mistake string
219
+ next_rule string
222
220
  }
223
221
  }
224
222
  }
@@ -270,12 +268,10 @@ main agent Learner {
270
268
  result = generate({
271
269
  input: "Answer the goal using relevant past lessons.",
272
270
  attempts: 3
273
- }) {
274
- return {
275
- ok boolean
276
- answer string
277
- reason string
278
- }
271
+ }) -> {
272
+ ok boolean
273
+ answer string
274
+ reason string
279
275
  }
280
276
 
281
277
  reflection = reflect({
@@ -290,19 +286,17 @@ main agent Learner {
290
286
  ok: result.ok
291
287
  })
292
288
 
293
- return result
289
+ result
294
290
  }
295
291
 
296
292
  func reflect(run) {
297
293
  use run
298
294
 
299
- return generate({
295
+ generate({
300
296
  input: "Extract one reusable lesson from this run.",
301
297
  attempts: 3
302
- }) {
303
- return {
304
- insight string
305
- }
298
+ }) -> {
299
+ insight string
306
300
  }
307
301
  }
308
302
  }
@@ -8,13 +8,13 @@ AgentScript has variables, functions, loops, and agent calls, but its primary pu
8
8
 
9
9
  AgentScript's control flow serves prompt context construction.
10
10
 
11
- Ordinary statements organize data, call tools, call agents, and update intermediate state. LLM calls happen only through `generate(...) { return ... }`, and the context visible to `generate` must be declared explicitly with `use`.
11
+ Ordinary statements organize data, call tools, call agents, and update intermediate state. LLM calls happen only through `generate(...) -> { ... }`, and the context visible to `generate` must be declared explicitly with `use`.
12
12
 
13
13
  The core objects are:
14
14
 
15
15
  - **Data**: ordinary variables, JSON values, lists, file imports, tool observations, agent return values.
16
16
  - **Context Source**: a prompt context origin declared by `use expr < budget`.
17
- - **Generation Site**: an LLM call declared by `generate({ input, limit, attempts, debug }) { return shape }`.
17
+ - **Generation Site**: an LLM call declared by `generate({ input, limit, attempts, debug }) -> shape`.
18
18
  - **Boundary**: context visibility boundaries formed by agent, function, and block scopes.
19
19
 
20
20
  ## Semantics of `use`
@@ -30,15 +30,13 @@ func answer(question, scratch) {
30
30
  use question
31
31
  use scratch.summary < 2k
32
32
 
33
- return generate({
33
+ generate({
34
34
  input: "Answer the question using collected facts"
35
35
  limit: 800
36
- }) {
37
- return {
38
- ok boolean
39
- text string
40
- error string
41
- }
36
+ }) -> {
37
+ ok boolean
38
+ text string
39
+ error string
42
40
  }
43
41
  }
44
42
  ```
@@ -57,10 +55,8 @@ main func(input) {
57
55
  scratch.add({ fact: "A" })
58
56
  scratch.add({ fact: "B" })
59
57
 
60
- return generate({ input: "Answer from scratch" }) {
61
- return {
62
- text string
63
- }
58
+ generate({ input: "Answer from scratch" }) -> {
59
+ text string
64
60
  }
65
61
  }
66
62
  ```
@@ -103,15 +99,13 @@ Each function call creates an independent context boundary.
103
99
  ```agentscript
104
100
  func caller(input) {
105
101
  use input.goal
106
- return helper(input)
102
+ helper(input)
107
103
  }
108
104
 
109
105
  func helper(input) {
110
106
  use input.detail
111
- return generate({ input: "Work on detail" }) {
112
- return {
113
- ok boolean
114
- }
107
+ generate({ input: "Work on detail" }) -> {
108
+ ok boolean
115
109
  }
116
110
  }
117
111
  ```
@@ -145,10 +139,8 @@ Blocks (`if`, `repeat`, `loop`, `for`) create child scopes. `use` declarations i
145
139
  if condition {
146
140
  temp = compute(input)
147
141
  use temp
148
- result = generate({ input: "Use temp" }) {
149
- return {
150
- ok boolean
151
- }
142
+ result = generate({ input: "Use temp" }) -> {
143
+ ok boolean
152
144
  }
153
145
  }
154
146
  ```
@@ -205,11 +197,9 @@ Source labels help models understand context and help humans audit prompts.
205
197
  Comes from the `input` field of `generate(...)` options.
206
198
 
207
199
  ```agentscript
208
- generate({ input: "Answer the question using only collected facts" }) {
209
- return {
210
- ok boolean
211
- text string
212
- }
200
+ generate({ input: "Answer the question using only collected facts" }) -> {
201
+ ok boolean
202
+ text string
213
203
  }
214
204
  ```
215
205
 
@@ -217,10 +207,10 @@ The instruction is the per-call task. `limit`, `attempts`, and `debug` are local
217
207
 
218
208
  ### Output contract
219
209
 
220
- Comes from `return { ... }` shape.
210
+ Comes from the optional `-> { ... }` shape on a `generate` expression.
221
211
 
222
212
  ```agentscript
223
- return {
213
+ generate({ input: "Answer" }) -> {
224
214
  ok boolean
225
215
  text string
226
216
  error string