@rong/agentscript 0.1.3 → 0.1.5

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,476 @@
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。自由形式的 `generate` 是允许的,但不推荐用于 agent workflow;优先声明明确的输出 shape,便于 retry、validation、trace 和下游 agent 调用保持可审计。
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 max 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 max 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 ... max 4k = 输入上下文预算
195
+ max_output: 800 = 输出生成预算
196
+ ```
197
+
198
+ ## `attempts`
199
+
200
+ `attempts` 控制 runtime 获取有效结构化结果的尝试次数。`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
+ 如果选中的 provider/model 支持,则作为 sampling temperature 透传。
257
+ 如果不支持,adapter 可以按照 capability policy 选择 ignore、warn 或 fail。
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,不是所有模型都保证支持。如果不支持,adapter 可以按照 capability policy 选择 ignore、warn 或 fail。
301
+
302
+ ## Provider hint 的 capability policy
303
+
304
+ `temperature` 和 `think` 都是 provider/model capability hint。不支持的 provider hint 默认在 debug mode 下 warn,否则 ignore。
305
+
306
+ Adapter 可以按照 runtime capability policy 对不支持的 hint 选择 ignore、warn 或 fail,但文档中的默认语义统一为:
307
+
308
+ ```text
309
+ 不支持的 provider hint 默认在 debug mode 下 warn,否则 ignore
310
+ ```
311
+
312
+ ## `strict`
313
+
314
+ `strict` 控制输出 shape validation。
315
+
316
+ ```agentscript
317
+ generate({
318
+ input: "Classify the issue",
319
+ max_output: 300,
320
+ strict: true
321
+ }) -> {
322
+ category string
323
+ confidence number
324
+ }
325
+ ```
326
+
327
+ 默认:
328
+
329
+ ```text
330
+ strict: false
331
+ ```
332
+
333
+ ### `strict: false`
334
+
335
+ 允许有限 coercion:
336
+
337
+ ```text
338
+ "true" -> true
339
+ "false" -> false
340
+ "42" -> 42
341
+ "3.14" -> 3.14
342
+ ```
343
+
344
+ 但仍然要求:
345
+
346
+ ```text
347
+ 输出可解析
348
+ 必需字段存在
349
+ 无法安全转换的类型失败
350
+ ```
351
+
352
+ ### `strict: true`
353
+
354
+ 严格验证:
355
+
356
+ ```text
357
+ 禁止 coercion
358
+ 必需字段必须存在
359
+ 字段类型必须精确匹配
360
+ extra fields rejected
361
+ shape mismatch triggers retry if attempts > 1
362
+ ```
363
+
364
+ 一句话:
365
+
366
+ ```text
367
+ strict 是 AgentScript runtime 对输出契约的控制。
368
+ ```
369
+
370
+ ## `debug`
371
+
372
+ `debug` 控制本次 `generate` 的调试输出。
373
+
374
+ ```agentscript
375
+ generate({
376
+ input: "Answer the question",
377
+ max_output: 800,
378
+ debug: true
379
+ }) -> {
380
+ answer string
381
+ }
382
+ ```
383
+
384
+ 建议输出:
385
+
386
+ ```text
387
+ resolved agent identity
388
+ generate input
389
+ selected context entries
390
+ context labels
391
+ budgets
392
+ rendered prompt/messages
393
+ output shape
394
+ raw model output
395
+ validation result
396
+ ```
397
+
398
+ `debug` 只影响调试输出,不改变 prompt 语义。
399
+
400
+ ## Trace 要求
401
+
402
+ `generate` trace 应解释实际 prompt 输入、配置、校验和结果:
403
+
404
+ ```json
405
+ {
406
+ "kind": "generate",
407
+ "data": {
408
+ "instruction": "Answer from observations",
409
+ "config": {
410
+ "max_output": 800,
411
+ "attempts": 1,
412
+ "temperature": 0.2,
413
+ "think": "medium",
414
+ "strict": false,
415
+ "debug": false
416
+ },
417
+ "context": {
418
+ "context": [
419
+ {
420
+ "index": 0,
421
+ "source": "scratch.summary",
422
+ "label": "observations",
423
+ "value": [{ "fact": "A" }],
424
+ "text": "[...]",
425
+ "budget": { "amount": 2, "unit": "k" },
426
+ "clipped": false
427
+ }
428
+ ]
429
+ },
430
+ "attempts": 1,
431
+ "validation": { "ok": true, "strict": false },
432
+ "result": { "answer": "..." }
433
+ }
434
+ }
435
+ ```
436
+
437
+ Trace 要能回答:
438
+
439
+ - 哪个 agent identity 生成了该输出?
440
+ - 使用了什么 instruction?
441
+ - 哪些 selected context 可见?
442
+ - 哪些 context item 被裁剪?
443
+ - 请求了哪些 provider hint 和 runtime behavior?
444
+ - 请求了什么 shape?
445
+ - 尝试了几次?
446
+ - 使用了什么 validation mode?
447
+ - 返回了什么值?
448
+
449
+ ## Final expression return
450
+
451
+ `generate` 表达式可以作为函数体最后一个顶层表达式。在这种情况下,函数会隐式返回生成值。
452
+
453
+ ```agentscript
454
+ func answer(question) {
455
+ use question as user question
456
+
457
+ generate({ input: "Answer" }) -> {
458
+ answer string
459
+ }
460
+ }
461
+ ```
462
+
463
+ 当没有更早执行显式 `return` 时,这等价于显式返回 generate 结果。
464
+
465
+ ## 设计检查清单
466
+
467
+ 修改 `generate` 前,应检查:
468
+
469
+ - 它是否仍是唯一 LLM 调用点?
470
+ - 它是否只使用可见的 `use` context source?
471
+ - 它是否仍区分 agent identity、selected context、instruction 和 output contract?
472
+ - `role` 是否仍是 agent identity,而不是 provider role 控制?
473
+ - `max_output` 是否仍是输出生成预算,而不是 context item budget?
474
+ - `strict` 是否仍是 runtime validation behavior,而不是 provider 配置?
475
+ - `think` 是否仍是 provider/model capability hint,而不是保证可用的 reasoning access?
476
+ - trace 是否解释了实际 prompt、配置、校验和结果?