@rong/agentscript 0.1.1 → 0.1.3

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,215 @@
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
+ 这条规则适用于所有表达式形式,包括调用表达式。被调用者解析为本地函数还是 agent 调用,都不影响 final expression return。例如,直接用 agent 名称调用另一个 agent 时,会调用该 agent 的 `main func`,其结果会被隐式返回:
63
+
64
+ ```agentscript
65
+ agent Planner {
66
+ main func(input) {
67
+ generate({ input: "Create a plan" }) -> {
68
+ steps list[string]
69
+ }
70
+ }
71
+ }
72
+
73
+ agent Controller {
74
+ func run(input) {
75
+ Planner(input)
76
+ }
77
+ }
78
+ ```
79
+
80
+ ```agentscript
81
+ func get_result(result) {
82
+ result.value
83
+ }
84
+ ```
85
+
86
+ ```agentscript
87
+ func observe(action) {
88
+ {
89
+ facts: [action.summary],
90
+ source: action.source
91
+ }
92
+ }
93
+ ```
94
+
95
+ ## 3. 不可隐式返回的语句
96
+
97
+ 下面这些不是表达式,不参与隐式返回:
98
+
99
+ ```text
100
+ use 声明
101
+ 赋值语句
102
+ import
103
+ loop
104
+ repeat
105
+ for
106
+ if/else,早期版本可先不表达式化
107
+ ```
108
+
109
+ 例如:
110
+
111
+ ```agentscript
112
+ func bad(input) {
113
+ use input.question
114
+ }
115
+ ```
116
+
117
+ 这里没有返回表达式,函数返回 `none`,或由 semantic analyzer 给出 warning。
118
+
119
+ 赋值也不返回:
120
+
121
+ ```agentscript
122
+ func f() {
123
+ x = answer()
124
+ }
125
+ ```
126
+
127
+ 如果要返回,需要写:
128
+
129
+ ```agentscript
130
+ func f() {
131
+ x = answer()
132
+ x
133
+ }
134
+ ```
135
+
136
+ ## 4. 显式 `return` 优先
137
+
138
+ 显式 `return` 仍然合法,并且在复杂控制流中推荐使用。
139
+
140
+ ```agentscript
141
+ func answer(input) {
142
+ if input.dry_run {
143
+ return {
144
+ ok: false,
145
+ answer: "dry run"
146
+ }
147
+ }
148
+
149
+ generate({ input: "Answer" }) -> {
150
+ ok boolean
151
+ answer string
152
+ }
153
+ }
154
+ ```
155
+
156
+ ## 5. 无返回值
157
+
158
+ 如果函数没有显式 `return`,最后一行也不是可返回表达式,则返回:
159
+
160
+ ```agentscript
161
+ none
162
+ ```
163
+
164
+ 可以显式写:
165
+
166
+ ```agentscript
167
+ return none
168
+ ```
169
+
170
+ 用于表达“此函数只产生副作用”。
171
+
172
+ ## 6. 推荐风格
173
+
174
+ 对于典型 LLM 调用,推荐省略 `return`:
175
+
176
+ ```agentscript
177
+ func summarize(content) {
178
+ use content < 8k
179
+
180
+ generate({
181
+ input: "Summarize the content"
182
+ limit: 1000
183
+ }) -> {
184
+ title string
185
+ summary string
186
+ key_points list[string]
187
+ }
188
+ }
189
+ ```
190
+
191
+ 对于分支、提前退出、错误处理,推荐使用显式 `return`:
192
+
193
+ ```agentscript
194
+ func answer(input) {
195
+ if not input.question {
196
+ return {
197
+ ok: false,
198
+ answer: ""
199
+ }
200
+ }
201
+
202
+ use input.question
203
+
204
+ generate({ input: "Answer the question" }) -> {
205
+ ok boolean
206
+ answer string
207
+ }
208
+ }
209
+ ```
210
+
211
+ ## 一句话定义
212
+
213
+ ```text
214
+ A function returns the value of its final top-level expression when no explicit return is reached.
215
+ ```
@@ -27,7 +27,7 @@ main agent Assistant {
27
27
  question string
28
28
  }) {
29
29
  use input.question
30
- return generate({ input: "Answer the question" }) -> {
30
+ generate({ input: "Answer the question" }) -> {
31
31
  ok boolean
32
32
  answer string
33
33
  }
@@ -138,7 +138,7 @@ func careful(input) {
138
138
  Shape 用于输入校验和 `generate` 输出校验:
139
139
 
140
140
  ```agentscript
141
- return generate({ input: "Extract facts" }) -> {
141
+ generate({ input: "Extract facts" }) -> {
142
142
  ok boolean
143
143
  title string
144
144
  items list[json]
@@ -158,6 +158,8 @@ Shape 不是完整的静态类型系统。
158
158
  use input.question
159
159
  use Requirements < 4k
160
160
  use past_lessons < 2k
161
+ use input.question as user
162
+ use docs.summary < 4k as evidence
161
163
  ```
162
164
 
163
165
  ### 规则
@@ -167,17 +169,31 @@ use past_lessons < 2k
167
169
  - Memory 查询结果不会自动进入 prompt。
168
170
  - Trace 事件不会自动进入 prompt。
169
171
  - `use value < n` 应用上下文预算。
172
+ - `use value as label` 为选中的 context source 附加字面标签。
173
+ - `use value < n as label` 先应用预算,再附加标签。
170
174
  - `llm`、`tool`、`agent`、`memory` 绑定不能被 `use`。
171
175
  - 函数绑定不能被 `use`。
172
176
  - `use` 声明被子作用域继承。
173
177
 
178
+ ### Context label
179
+
180
+ `as` 后面的 label 是字面标签文本,不是表达式,不会求值,也不会读取作用域中的变量。
181
+
182
+ ```agentscript
183
+ use docs as evidence
184
+ use docs.summary < 4k as retrieved evidence
185
+ use input.question as user
186
+ ```
187
+
188
+ 即使当前作用域中存在名为 `evidence` 的变量,`as evidence` 也只是把 context section 标记为 `evidence`。Label 用于组织 prompt section 和 trace 输出;它不会改变 `system`、`user`、`assistant` 等 provider message role。
189
+
174
190
  ### 延迟求值
175
191
 
176
192
  `use expr < budget` 声明的是 context source,而不是当前值的快照。当 `generate` 构建 prompt 时,表达式会被重新求值。这意味着在 `use` 之后、`generate` 之前对变量的修改在生成时刻是可见的。
177
193
 
178
194
  ## Generate
179
195
 
180
- `generate` 调用当前模型,需要 `input` 指令和返回 shape
196
+ `generate` 调用当前模型,需要 `input` 指令。返回 shape 是可选的。
181
197
 
182
198
  ```agentscript
183
199
  answer = generate({
@@ -198,7 +214,8 @@ answer = generate({
198
214
  - `limit`:生成预算(数字或 `2k` 格式)。可选。
199
215
  - `attempts`:JSON 解析失败或 shape 不匹配时的重试次数。可选,默认 1。
200
216
  - `debug`:将完整 prompt 打印到 stderr。可选,默认 false。
201
- - `-> { ... }` 块声明期望的输出 shape。
217
+ - 可选的 `-> { ... }` 块声明期望的输出 shape。
218
+ - 不写 `-> { ... }` 时,`generate` 输出无约束:AgentScript 不会在 prompt 中加入返回 schema,不会要求 provider 使用结构化输出,也不会对返回值做类型强制转换或 shape 校验。
202
219
  - Provider 错误(认证、网络、超时、模型不存在)直接失败,不做重试。
203
220
  - Shape 校验包含类型强制转换(如 `"true"` -> `true`,`"42"` -> `42`)。
204
221
 
@@ -365,7 +382,7 @@ import file Config from "./config.json"
365
382
  func answer(input) {
366
383
  use Requirements < 4k
367
384
  use Config
368
- return generate({ input: "Answer from the referenced file." }) -> {
385
+ generate({ input: "Answer from the referenced file." }) -> {
369
386
  ok boolean
370
387
  answer string
371
388
  }
@@ -417,7 +434,7 @@ main agent Controller {
417
434
  result = Executor({ goal: input.goal, step: step })
418
435
  results.add(result)
419
436
  }
420
- return results.summary
437
+ results.summary
421
438
  }
422
439
  }
423
440
  ```