@rong/agentscript 0.1.3 → 0.1.4
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.
- package/CHANGELOG.md +14 -0
- package/README.md +3 -3
- package/dist/parser/parser.js +20 -5
- package/dist/providers/llm/anthropic.js +6 -2
- package/dist/providers/llm/ollama.js +9 -2
- package/dist/providers/llm/openai.js +15 -1
- package/dist/providers/llm/shared.js +4 -4
- package/dist/runtime/context.js +2 -2
- package/dist/runtime/generate.js +22 -6
- package/dist/runtime/shape.js +6 -4
- package/dist/semantic/analyzer.js +20 -3
- package/docs/cn/context-engineering.md +49 -322
- package/docs/cn/final-expression-return.md +1 -1
- package/docs/cn/generate.md +480 -0
- package/docs/cn/language.md +9 -2
- package/docs/cn/use-as.md +225 -0
- package/docs/en/context-engineering.md +50 -268
- package/docs/en/final-expression-return.md +1 -1
- package/docs/en/generate.md +480 -0
- package/docs/en/language.md +9 -2
- package/docs/en/use-as.md +225 -0
- package/examples/changelog.as +1 -1
- package/examples/extract.as +1 -1
- package/examples/review.as +1 -1
- package/examples/summarize.as +1 -1
- package/examples/translate.as +1 -1
- package/package.json +1 -1
- package/tutorials/cli.as +1 -1
- package/tutorials/plan-execute.as +4 -4
- package/tutorials/react.as +4 -4
- package/docs/cn/role-label.md +0 -492
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
# `generate`
|
|
2
|
+
|
|
3
|
+
本文档定义 AgentScript 中 `generate` 的语义:generation site、prompt 层次、agent identity、selected context、输出契约、生成配置、重试、校验、debug output 和 trace output。
|
|
4
|
+
|
|
5
|
+
Context 选择和 label 见 [`use ... as ...`](./use-as.md)。整体设计总纲见 [Context Engineering](./context-engineering.md)。
|
|
6
|
+
|
|
7
|
+
## 目的
|
|
8
|
+
|
|
9
|
+
`generate` 是 AgentScript 中唯一的 LLM 调用点。
|
|
10
|
+
|
|
11
|
+
```agentscript
|
|
12
|
+
generate({ input: "Answer using the selected context." }) -> {
|
|
13
|
+
ok boolean
|
|
14
|
+
answer string
|
|
15
|
+
}
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
普通代码可以计算值、调用工具、调用 Agent 和组织状态。只有 `generate` 会请求当前模型生成新输出。
|
|
19
|
+
|
|
20
|
+
## 推荐语法
|
|
21
|
+
|
|
22
|
+
```agentscript
|
|
23
|
+
generate({
|
|
24
|
+
input: "Classify the issue"
|
|
25
|
+
max_output: 300
|
|
26
|
+
attempts: 2
|
|
27
|
+
temperature: 0.2
|
|
28
|
+
think: "medium"
|
|
29
|
+
strict: true
|
|
30
|
+
debug: false
|
|
31
|
+
}) -> {
|
|
32
|
+
category string
|
|
33
|
+
confidence number
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`->` 后面的输出 shape 是可选的:
|
|
38
|
+
|
|
39
|
+
```agentscript
|
|
40
|
+
generate({ input: "Draft a response." })
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
没有声明 shape 时,runtime 不应注入 schema,也不应要求 provider 返回结构化 JSON。
|
|
44
|
+
|
|
45
|
+
## 配置字段
|
|
46
|
+
|
|
47
|
+
| 字段 | 必需 | 类型 | 语义 |
|
|
48
|
+
|---|---:|---|---|
|
|
49
|
+
| `input` | 是 | `string` | 本次 `generate` 的任务指令。 |
|
|
50
|
+
| `max_output` | 否 | `number` / budget literal | 请求的输出生成预算。 |
|
|
51
|
+
| `attempts` | 否 | `number` | JSON parse 失败或 shape validation 失败时的重试次数。 |
|
|
52
|
+
| `temperature` | 否 | `number` | 采样温度,传递给支持该参数的 provider。 |
|
|
53
|
+
| `think` | 否 | `boolean` / `string` | 请求模型启用 reasoning / thinking 模式。 |
|
|
54
|
+
| `strict` | 否 | `boolean` | 控制 shape validation 是否严格。 |
|
|
55
|
+
| `debug` | 否 | `boolean` | 是否输出 prompt / trace 调试信息。 |
|
|
56
|
+
|
|
57
|
+
推荐默认值:
|
|
58
|
+
|
|
59
|
+
```text
|
|
60
|
+
max_output: provider/runtime default
|
|
61
|
+
attempts: 1
|
|
62
|
+
temperature: provider/model default
|
|
63
|
+
think: false
|
|
64
|
+
strict: false
|
|
65
|
+
debug: false
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
这些字段分为三类:
|
|
69
|
+
|
|
70
|
+
```text
|
|
71
|
+
Generation instruction:
|
|
72
|
+
input
|
|
73
|
+
|
|
74
|
+
Generation provider hints:
|
|
75
|
+
max_output
|
|
76
|
+
temperature
|
|
77
|
+
think
|
|
78
|
+
|
|
79
|
+
AgentScript runtime behavior:
|
|
80
|
+
attempts
|
|
81
|
+
strict
|
|
82
|
+
debug
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Prompt 构造
|
|
86
|
+
|
|
87
|
+
一次 `generate` 的 prompt 有四个概念层。
|
|
88
|
+
|
|
89
|
+
### Agent identity
|
|
90
|
+
|
|
91
|
+
Agent identity 来自当前 Agent 配置:
|
|
92
|
+
|
|
93
|
+
```agentscript
|
|
94
|
+
role "Senior Researcher"
|
|
95
|
+
description "Answer questions with search and structured reasoning."
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
它会作为 identity 渲染进 provider system prompt:
|
|
99
|
+
|
|
100
|
+
```text
|
|
101
|
+
You are Senior Researcher.
|
|
102
|
+
Answer questions with search and structured reasoning.
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Agent `role` 是 AgentScript 的身份概念,不等同于 `system`、`user`、`assistant` 等 provider message role。
|
|
106
|
+
|
|
107
|
+
### Selected context
|
|
108
|
+
|
|
109
|
+
Selected context 来自可见的 `use` 声明:
|
|
110
|
+
|
|
111
|
+
```agentscript
|
|
112
|
+
use input.question as user question
|
|
113
|
+
use scratch.summary < 2k as observations
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
渲染出的 prompt section 可以是:
|
|
117
|
+
|
|
118
|
+
```text
|
|
119
|
+
Context:
|
|
120
|
+
[user question]
|
|
121
|
+
source: input.question
|
|
122
|
+
What is AgentScript?
|
|
123
|
+
|
|
124
|
+
[observations]
|
|
125
|
+
source: scratch.summary
|
|
126
|
+
[
|
|
127
|
+
{ "fact": "..." }
|
|
128
|
+
]
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Context label 用于组织 prompt section,不会创建 provider message。
|
|
132
|
+
|
|
133
|
+
### Instruction
|
|
134
|
+
|
|
135
|
+
Instruction 来自 `generate(...)` 的 `input` 字段:
|
|
136
|
+
|
|
137
|
+
```agentscript
|
|
138
|
+
generate({ input: "Answer using only the selected context." }) -> {
|
|
139
|
+
answer string
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Instruction 是本次 LLM 调用的局部任务,区别于长期 context。
|
|
144
|
+
|
|
145
|
+
### Output contract
|
|
146
|
+
|
|
147
|
+
Output contract 来自 `->` 后可选的 shape:
|
|
148
|
+
|
|
149
|
+
```agentscript
|
|
150
|
+
generate({ input: "Answer" }) -> {
|
|
151
|
+
ok boolean
|
|
152
|
+
answer string
|
|
153
|
+
citations list[string]
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Runtime 会在可能时请求 provider 返回结构化输出,并校验返回值满足该 shape。
|
|
158
|
+
|
|
159
|
+
## `max_output`
|
|
160
|
+
|
|
161
|
+
`max_output` 表示本次模型生成的输出预算。
|
|
162
|
+
|
|
163
|
+
```agentscript
|
|
164
|
+
generate({
|
|
165
|
+
input: "Answer briefly"
|
|
166
|
+
max_output: 300
|
|
167
|
+
}) -> {
|
|
168
|
+
answer string
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
语义:
|
|
173
|
+
|
|
174
|
+
```text
|
|
175
|
+
max_output = provider-side generation budget requested by AgentScript
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
它和 `use` 的输入上下文预算分开:
|
|
179
|
+
|
|
180
|
+
```agentscript
|
|
181
|
+
use docs.summary < 4k
|
|
182
|
+
|
|
183
|
+
generate({
|
|
184
|
+
input: "Answer from the selected docs"
|
|
185
|
+
max_output: 800
|
|
186
|
+
}) -> {
|
|
187
|
+
answer string
|
|
188
|
+
}
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
区别:
|
|
192
|
+
|
|
193
|
+
```text
|
|
194
|
+
use ... < 4k = 输入上下文预算
|
|
195
|
+
max_output: 800 = 输出生成预算
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## `attempts`
|
|
199
|
+
|
|
200
|
+
`attempts` 控制结构化输出失败时的重试次数。
|
|
201
|
+
|
|
202
|
+
```agentscript
|
|
203
|
+
generate({
|
|
204
|
+
input: "Extract metadata"
|
|
205
|
+
max_output: 500
|
|
206
|
+
attempts: 3
|
|
207
|
+
}) -> {
|
|
208
|
+
title string
|
|
209
|
+
tags list[string]
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
触发重试的情况:
|
|
214
|
+
|
|
215
|
+
```text
|
|
216
|
+
JSON parse failed
|
|
217
|
+
shape validation failed
|
|
218
|
+
required field missing
|
|
219
|
+
type mismatch
|
|
220
|
+
strict mode violation
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
不应重试的情况:
|
|
224
|
+
|
|
225
|
+
```text
|
|
226
|
+
provider auth error
|
|
227
|
+
network error
|
|
228
|
+
model not found
|
|
229
|
+
quota exceeded
|
|
230
|
+
timeout,是否重试可由 runtime policy 决定
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
默认值:
|
|
234
|
+
|
|
235
|
+
```text
|
|
236
|
+
attempts: 1
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## `temperature`
|
|
240
|
+
|
|
241
|
+
`temperature` 是采样温度。
|
|
242
|
+
|
|
243
|
+
```agentscript
|
|
244
|
+
generate({
|
|
245
|
+
input: "Brainstorm alternatives"
|
|
246
|
+
max_output: 1000
|
|
247
|
+
temperature: 0.7
|
|
248
|
+
}) -> {
|
|
249
|
+
ideas list[string]
|
|
250
|
+
}
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
语义:
|
|
254
|
+
|
|
255
|
+
```text
|
|
256
|
+
If supported by the selected provider/model, pass through as sampling temperature.
|
|
257
|
+
If unsupported, adapter may ignore, warn, or fail according to capability policy.
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## `think`
|
|
261
|
+
|
|
262
|
+
`think` 是模型 reasoning / thinking 模式请求。
|
|
263
|
+
|
|
264
|
+
推荐支持:
|
|
265
|
+
|
|
266
|
+
```agentscript
|
|
267
|
+
think: false
|
|
268
|
+
think: true
|
|
269
|
+
think: "auto"
|
|
270
|
+
think: "low"
|
|
271
|
+
think: "medium"
|
|
272
|
+
think: "high"
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
语义:
|
|
276
|
+
|
|
277
|
+
```text
|
|
278
|
+
false 不请求 thinking/reasoning 模式
|
|
279
|
+
true 请求 provider 默认 thinking/reasoning 模式
|
|
280
|
+
"auto" 由 provider/model 自行决定
|
|
281
|
+
"low" 请求低强度 reasoning
|
|
282
|
+
"medium" 请求中等强度 reasoning
|
|
283
|
+
"high" 请求高强度 reasoning
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
示例:
|
|
287
|
+
|
|
288
|
+
```agentscript
|
|
289
|
+
generate({
|
|
290
|
+
input: "Analyze the tradeoffs"
|
|
291
|
+
max_output: 1200
|
|
292
|
+
think: "high"
|
|
293
|
+
}) -> {
|
|
294
|
+
decision string
|
|
295
|
+
tradeoffs list[string]
|
|
296
|
+
risks list[string]
|
|
297
|
+
}
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
`think` 是 provider/model capability hint,不是所有模型都保证支持。
|
|
301
|
+
|
|
302
|
+
如果 provider 不支持,runtime 可按策略处理:
|
|
303
|
+
|
|
304
|
+
```text
|
|
305
|
+
ignore
|
|
306
|
+
warn
|
|
307
|
+
fail
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
默认建议:
|
|
311
|
+
|
|
312
|
+
```text
|
|
313
|
+
warn in debug mode, otherwise ignore
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
## `strict`
|
|
317
|
+
|
|
318
|
+
`strict` 控制输出 shape validation。
|
|
319
|
+
|
|
320
|
+
```agentscript
|
|
321
|
+
generate({
|
|
322
|
+
input: "Classify the issue"
|
|
323
|
+
max_output: 300
|
|
324
|
+
strict: true
|
|
325
|
+
}) -> {
|
|
326
|
+
category string
|
|
327
|
+
confidence number
|
|
328
|
+
}
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
默认:
|
|
332
|
+
|
|
333
|
+
```text
|
|
334
|
+
strict: false
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
### `strict: false`
|
|
338
|
+
|
|
339
|
+
允许有限 coercion:
|
|
340
|
+
|
|
341
|
+
```text
|
|
342
|
+
"true" -> true
|
|
343
|
+
"false" -> false
|
|
344
|
+
"42" -> 42
|
|
345
|
+
"3.14" -> 3.14
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
但仍然要求:
|
|
349
|
+
|
|
350
|
+
```text
|
|
351
|
+
输出可解析
|
|
352
|
+
必需字段存在
|
|
353
|
+
无法安全转换的类型失败
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
### `strict: true`
|
|
357
|
+
|
|
358
|
+
严格验证:
|
|
359
|
+
|
|
360
|
+
```text
|
|
361
|
+
禁止 coercion
|
|
362
|
+
必需字段必须存在
|
|
363
|
+
字段类型必须精确匹配
|
|
364
|
+
extra fields rejected
|
|
365
|
+
shape mismatch triggers retry if attempts > 1
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
一句话:
|
|
369
|
+
|
|
370
|
+
```text
|
|
371
|
+
strict 是 AgentScript runtime 对输出契约的控制。
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
## `debug`
|
|
375
|
+
|
|
376
|
+
`debug` 控制本次 `generate` 的调试输出。
|
|
377
|
+
|
|
378
|
+
```agentscript
|
|
379
|
+
generate({
|
|
380
|
+
input: "Answer the question"
|
|
381
|
+
max_output: 800
|
|
382
|
+
debug: true
|
|
383
|
+
}) -> {
|
|
384
|
+
answer string
|
|
385
|
+
}
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
建议输出:
|
|
389
|
+
|
|
390
|
+
```text
|
|
391
|
+
resolved agent identity
|
|
392
|
+
generate input
|
|
393
|
+
selected context entries
|
|
394
|
+
context labels
|
|
395
|
+
budgets
|
|
396
|
+
rendered prompt/messages
|
|
397
|
+
output shape
|
|
398
|
+
raw model output
|
|
399
|
+
validation result
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
`debug` 只影响调试输出,不改变 prompt 语义。
|
|
403
|
+
|
|
404
|
+
## Trace 要求
|
|
405
|
+
|
|
406
|
+
`generate` trace 应解释实际 prompt 输入、配置、校验和结果:
|
|
407
|
+
|
|
408
|
+
```json
|
|
409
|
+
{
|
|
410
|
+
"kind": "generate",
|
|
411
|
+
"data": {
|
|
412
|
+
"instruction": "Answer from observations",
|
|
413
|
+
"config": {
|
|
414
|
+
"max_output": 800,
|
|
415
|
+
"attempts": 1,
|
|
416
|
+
"temperature": 0.2,
|
|
417
|
+
"think": "medium",
|
|
418
|
+
"strict": false,
|
|
419
|
+
"debug": false
|
|
420
|
+
},
|
|
421
|
+
"context": {
|
|
422
|
+
"context": [
|
|
423
|
+
{
|
|
424
|
+
"index": 0,
|
|
425
|
+
"source": "scratch.summary",
|
|
426
|
+
"label": "observations",
|
|
427
|
+
"value": [{ "fact": "A" }],
|
|
428
|
+
"text": "[...]",
|
|
429
|
+
"budget": { "amount": 2, "unit": "k" },
|
|
430
|
+
"clipped": false
|
|
431
|
+
}
|
|
432
|
+
]
|
|
433
|
+
},
|
|
434
|
+
"attempts": 1,
|
|
435
|
+
"validation": { "ok": true, "strict": false },
|
|
436
|
+
"result": { "answer": "..." }
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
Trace 要能回答:
|
|
442
|
+
|
|
443
|
+
- 哪个 agent identity 生成了该输出?
|
|
444
|
+
- 使用了什么 instruction?
|
|
445
|
+
- 哪些 selected context 可见?
|
|
446
|
+
- 哪些 context item 被裁剪?
|
|
447
|
+
- 请求了哪些 provider hint 和 runtime behavior?
|
|
448
|
+
- 请求了什么 shape?
|
|
449
|
+
- 尝试了几次?
|
|
450
|
+
- 使用了什么 validation mode?
|
|
451
|
+
- 返回了什么值?
|
|
452
|
+
|
|
453
|
+
## Final expression return
|
|
454
|
+
|
|
455
|
+
`generate` 表达式可以作为函数体最后一个顶层表达式。在这种情况下,函数会隐式返回生成值。
|
|
456
|
+
|
|
457
|
+
```agentscript
|
|
458
|
+
func answer(question) {
|
|
459
|
+
use question as user question
|
|
460
|
+
|
|
461
|
+
generate({ input: "Answer" }) -> {
|
|
462
|
+
answer string
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
当没有更早执行显式 `return` 时,这等价于显式返回 generate 结果。
|
|
468
|
+
|
|
469
|
+
## 设计检查清单
|
|
470
|
+
|
|
471
|
+
修改 `generate` 前,应检查:
|
|
472
|
+
|
|
473
|
+
- 它是否仍是唯一 LLM 调用点?
|
|
474
|
+
- 它是否只使用可见的 `use` context source?
|
|
475
|
+
- 它是否仍区分 agent identity、selected context、instruction 和 output contract?
|
|
476
|
+
- `role` 是否仍是 agent identity,而不是 provider role 控制?
|
|
477
|
+
- `max_output` 是否仍是输出生成预算,而不是 context item budget?
|
|
478
|
+
- `strict` 是否仍是 runtime validation behavior,而不是 provider 配置?
|
|
479
|
+
- `think` 是否仍是 provider/model capability hint,而不是保证可用的 reasoning access?
|
|
480
|
+
- trace 是否解释了实际 prompt、配置、校验和结果?
|
package/docs/cn/language.md
CHANGED
|
@@ -191,6 +191,8 @@ use input.question as user
|
|
|
191
191
|
|
|
192
192
|
`use expr < budget` 声明的是 context source,而不是当前值的快照。当 `generate` 构建 prompt 时,表达式会被重新求值。这意味着在 `use` 之后、`generate` 之前对变量的修改在生成时刻是可见的。
|
|
193
193
|
|
|
194
|
+
完整设计语义见 [`use ... as ...`](./use-as.md)。
|
|
195
|
+
|
|
194
196
|
## Generate
|
|
195
197
|
|
|
196
198
|
`generate` 调用当前模型,需要 `input` 指令。返回 shape 是可选的。
|
|
@@ -198,7 +200,7 @@ use input.question as user
|
|
|
198
200
|
```agentscript
|
|
199
201
|
answer = generate({
|
|
200
202
|
input: "Answer using the selected context."
|
|
201
|
-
|
|
203
|
+
max_output: 800
|
|
202
204
|
attempts: 3
|
|
203
205
|
debug: true
|
|
204
206
|
}) -> {
|
|
@@ -211,14 +213,19 @@ answer = generate({
|
|
|
211
213
|
### 语义
|
|
212
214
|
|
|
213
215
|
- `input`:每次生成的指令。必填。
|
|
214
|
-
- `
|
|
216
|
+
- `max_output`:输出生成预算(数字或 `2k` 格式)。可选。
|
|
215
217
|
- `attempts`:JSON 解析失败或 shape 不匹配时的重试次数。可选,默认 1。
|
|
218
|
+
- `temperature`:provider sampling hint。可选。
|
|
219
|
+
- `think`:provider/model reasoning hint。可选。
|
|
220
|
+
- `strict`:控制 shape validation 是否严格。可选,默认 false。
|
|
216
221
|
- `debug`:将完整 prompt 打印到 stderr。可选,默认 false。
|
|
217
222
|
- 可选的 `-> { ... }` 块声明期望的输出 shape。
|
|
218
223
|
- 不写 `-> { ... }` 时,`generate` 输出无约束:AgentScript 不会在 prompt 中加入返回 schema,不会要求 provider 使用结构化输出,也不会对返回值做类型强制转换或 shape 校验。
|
|
219
224
|
- Provider 错误(认证、网络、超时、模型不存在)直接失败,不做重试。
|
|
220
225
|
- Shape 校验包含类型强制转换(如 `"true"` -> `true`,`"42"` -> `42`)。
|
|
221
226
|
|
|
227
|
+
Prompt 构造、identity、retry 和 trace 语义见 [`generate`](./generate.md)。
|
|
228
|
+
|
|
222
229
|
## 控制流
|
|
223
230
|
|
|
224
231
|
### If / else
|