@rong/agentscript 0.1.2 → 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.
@@ -0,0 +1,480 @@
1
+ # `generate`
2
+
3
+ This document defines the semantics of `generate` in AgentScript: generation sites, prompt layers, agent identity, selected context, output contracts, generation configuration, retries, validation, debug output, and trace output.
4
+
5
+ For context selection and labels, see [`use ... as ...`](./use-as.md). For the broader design map, see [Context Engineering](./context-engineering.md).
6
+
7
+ ## Purpose
8
+
9
+ `generate` is the only LLM call site in AgentScript.
10
+
11
+ ```agentscript
12
+ generate({ input: "Answer using the selected context." }) -> {
13
+ ok boolean
14
+ answer string
15
+ }
16
+ ```
17
+
18
+ Ordinary code can compute values, call tools, call agents, and organize state. Only `generate` asks the current model to produce new output.
19
+
20
+ ## Recommended syntax
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
+ The output shape after `->` is optional:
38
+
39
+ ```agentscript
40
+ generate({ input: "Draft a response." })
41
+ ```
42
+
43
+ When no shape is declared, the runtime should not inject a schema or require structured JSON output.
44
+
45
+ ## Configuration fields
46
+
47
+ | Field | Required | Type | Meaning |
48
+ |---|---:|---|---|
49
+ | `input` | yes | `string` | Per-generation task instruction. |
50
+ | `max_output` | no | `number` / budget literal | Requested output generation budget. |
51
+ | `attempts` | no | `number` | Retry count for JSON parse failures or shape validation failures. |
52
+ | `temperature` | no | `number` | Sampling temperature passed to providers that support it. |
53
+ | `think` | no | `boolean` / `string` | Request model reasoning / thinking mode. |
54
+ | `strict` | no | `boolean` | Controls whether output shape validation is strict. |
55
+ | `debug` | no | `boolean` | Enables prompt / trace debug output for this generation. |
56
+
57
+ Recommended defaults:
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
+ The fields fall into three groups:
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 construction
86
+
87
+ A `generate` prompt has four conceptual layers.
88
+
89
+ ### Agent identity
90
+
91
+ Agent identity comes from the current agent configuration:
92
+
93
+ ```agentscript
94
+ role "Senior Researcher"
95
+ description "Answer questions with search and structured reasoning."
96
+ ```
97
+
98
+ It is rendered into the provider system prompt as identity:
99
+
100
+ ```text
101
+ You are Senior Researcher.
102
+ Answer questions with search and structured reasoning.
103
+ ```
104
+
105
+ Agent `role` is an AgentScript identity concept. It is not the same as a provider message role such as `system`, `user`, or `assistant`.
106
+
107
+ ### Selected context
108
+
109
+ Selected context comes from visible `use` declarations:
110
+
111
+ ```agentscript
112
+ use input.question as user question
113
+ use scratch.summary < 2k as observations
114
+ ```
115
+
116
+ A rendered prompt section may look like:
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 labels organize prompt sections. They do not create provider messages.
132
+
133
+ ### Instruction
134
+
135
+ The instruction comes from the `input` field of `generate(...)`:
136
+
137
+ ```agentscript
138
+ generate({ input: "Answer using only the selected context." }) -> {
139
+ answer string
140
+ }
141
+ ```
142
+
143
+ The instruction is the local task for this one LLM call. It is distinct from long-lived context.
144
+
145
+ ### Output contract
146
+
147
+ The output contract comes from the optional shape after `->`:
148
+
149
+ ```agentscript
150
+ generate({ input: "Answer" }) -> {
151
+ ok boolean
152
+ answer string
153
+ citations list[string]
154
+ }
155
+ ```
156
+
157
+ The runtime asks the provider for structured output when possible and validates the returned value against the shape.
158
+
159
+ ## `max_output`
160
+
161
+ `max_output` is the requested provider-side generation budget.
162
+
163
+ ```agentscript
164
+ generate({
165
+ input: "Answer briefly"
166
+ max_output: 300
167
+ }) -> {
168
+ answer string
169
+ }
170
+ ```
171
+
172
+ Semantics:
173
+
174
+ ```text
175
+ max_output = provider-side generation budget requested by AgentScript
176
+ ```
177
+
178
+ It is separate from `use` input context budgets:
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
+ Difference:
192
+
193
+ ```text
194
+ use ... < 4k = input context budget
195
+ max_output: 800 = output generation budget
196
+ ```
197
+
198
+ ## `attempts`
199
+
200
+ `attempts` controls how many times the runtime may try to obtain a valid structured result.
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
+ Retryable failures:
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
+ Non-retryable failures:
224
+
225
+ ```text
226
+ provider auth error
227
+ network error
228
+ model not found
229
+ quota exceeded
230
+ timeout, unless runtime policy decides it is retryable
231
+ ```
232
+
233
+ Default:
234
+
235
+ ```text
236
+ attempts: 1
237
+ ```
238
+
239
+ ## `temperature`
240
+
241
+ `temperature` is the sampling 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
+ Semantics:
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` is a model reasoning / thinking mode request.
263
+
264
+ Recommended values:
265
+
266
+ ```agentscript
267
+ think: false
268
+ think: true
269
+ think: "auto"
270
+ think: "low"
271
+ think: "medium"
272
+ think: "high"
273
+ ```
274
+
275
+ Semantics:
276
+
277
+ ```text
278
+ false do not request thinking/reasoning mode
279
+ true request provider default thinking/reasoning mode
280
+ "auto" let provider/model decide
281
+ "low" request low-intensity reasoning
282
+ "medium" request medium-intensity reasoning
283
+ "high" request high-intensity reasoning
284
+ ```
285
+
286
+ Example:
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` is a provider/model capability hint. Not every model guarantees support.
301
+
302
+ If unsupported, the runtime may handle it according to policy:
303
+
304
+ ```text
305
+ ignore
306
+ warn
307
+ fail
308
+ ```
309
+
310
+ Recommended default policy:
311
+
312
+ ```text
313
+ warn in debug mode, otherwise ignore
314
+ ```
315
+
316
+ ## `strict`
317
+
318
+ `strict` controls output 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
+ Default:
332
+
333
+ ```text
334
+ strict: false
335
+ ```
336
+
337
+ ### `strict: false`
338
+
339
+ Allows limited coercion:
340
+
341
+ ```text
342
+ "true" -> true
343
+ "false" -> false
344
+ "42" -> 42
345
+ "3.14" -> 3.14
346
+ ```
347
+
348
+ Still required:
349
+
350
+ ```text
351
+ output is parseable
352
+ required fields exist
353
+ unsafe conversions fail
354
+ ```
355
+
356
+ ### `strict: true`
357
+
358
+ Strict validation:
359
+
360
+ ```text
361
+ coercion is forbidden
362
+ required fields must exist
363
+ field types must match exactly
364
+ extra fields are rejected
365
+ shape mismatch triggers retry if attempts > 1
366
+ ```
367
+
368
+ In one sentence:
369
+
370
+ ```text
371
+ strict is AgentScript runtime control over the output contract.
372
+ ```
373
+
374
+ ## `debug`
375
+
376
+ `debug` controls debug output for this `generate` call.
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
+ Recommended debug output:
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` only affects debug output. It does not change prompt semantics.
403
+
404
+ ## Trace requirements
405
+
406
+ A `generate` trace should explain the actual prompt inputs, configuration, validation, and result:
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 answers:
442
+
443
+ - Which agent identity generated this output?
444
+ - What instruction was used?
445
+ - What selected context was visible?
446
+ - Which context items were clipped?
447
+ - What provider hints and runtime behavior were requested?
448
+ - What shape was requested?
449
+ - How many attempts were needed?
450
+ - What validation mode was used?
451
+ - What value was returned?
452
+
453
+ ## Final expression return
454
+
455
+ A `generate` expression can be the final top-level expression in a function body. In that case, the function returns the generated value implicitly.
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
+ This is equivalent to explicitly returning the generate result when no earlier explicit `return` is executed.
468
+
469
+ ## Design checklist
470
+
471
+ Before changing `generate`, verify:
472
+
473
+ - Does it remain the only LLM call site?
474
+ - Does it use only visible `use` context sources?
475
+ - Does it keep agent identity, selected context, instruction, and output contract distinct?
476
+ - Does `role` remain agent identity rather than provider role control?
477
+ - Does `max_output` remain an output generation budget, not a context item budget?
478
+ - Does `strict` remain runtime validation behavior rather than provider configuration?
479
+ - Does `think` remain a provider/model capability hint rather than guaranteed reasoning access?
480
+ - Does trace explain the actual prompt, configuration, validation, and result?
@@ -158,6 +158,8 @@ Shapes are not a full static type system.
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
  ### Rules
@@ -167,14 +169,30 @@ use past_lessons < 2k
167
169
  - Memory query results do not automatically enter prompts.
168
170
  - Trace events do not automatically enter prompts.
169
171
  - `use value < n` applies a context budget.
172
+ - `use value as label` attaches a literal context label to the selected source.
173
+ - `use value < n as label` applies the budget first, then attaches the label.
170
174
  - `llm`, `tool`, `agent`, `memory` bindings cannot be used.
171
175
  - Function bindings cannot be used.
172
176
  - `use` declarations are inherited by child scopes.
173
177
 
178
+ ### Context labels
179
+
180
+ The label after `as` is literal label text. It is not an expression, is not evaluated, and does not read variables from scope.
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
+ `as evidence` labels the context section as `evidence` even if a variable named `evidence` exists. Labels organize prompt sections and trace output; they do not change provider message roles such as `system`, `user`, or `assistant`.
189
+
174
190
  ### Deferred evaluation
175
191
 
176
192
  `use expr < budget` declares a context source, not a snapshot. The expression is re-evaluated when `generate` builds the prompt. This means updates to a variable made after `use` but before `generate` are visible at generation time.
177
193
 
194
+ For the full design semantics, see [`use ... as ...`](./use-as.md).
195
+
178
196
  ## Generate
179
197
 
180
198
  `generate` calls the current model and requires an `input` instruction. A return shape is optional.
@@ -182,7 +200,7 @@ use past_lessons < 2k
182
200
  ```agentscript
183
201
  answer = generate({
184
202
  input: "Answer using the selected context."
185
- limit: 800
203
+ max_output: 800
186
204
  attempts: 3
187
205
  debug: true
188
206
  }) -> {
@@ -195,14 +213,19 @@ answer = generate({
195
213
  ### Semantics
196
214
 
197
215
  - `input` is the per-generation instruction. Required.
198
- - `limit` is the generation budget (number or `2k` style). Optional.
216
+ - `max_output` is the output generation budget (number or `2k` style). Optional.
199
217
  - `attempts` controls retry for JSON parse errors or shape mismatch. Optional, defaults to 1.
218
+ - `temperature` is a provider sampling hint. Optional.
219
+ - `think` is a provider/model reasoning hint. Optional.
220
+ - `strict` controls shape validation strictness. Optional, defaults to false.
200
221
  - `debug` prints the full prompt to stderr. Optional, defaults to false.
201
222
  - The optional `-> { ... }` block declares the expected output shape.
202
223
  - Without `-> { ... }`, the generate output is unconstrained: AgentScript does not add a return schema to the prompt, does not request provider structured output, and does not coerce or validate the returned value.
203
224
  - Provider errors (auth, network, timeout, missing model) fail directly without retry.
204
225
  - Shape validation includes coercion (e.g. `"true"` -> `true`, `"42"` -> `42`).
205
226
 
227
+ For prompt construction, identity, retry, and trace semantics, see [`generate`](./generate.md).
228
+
206
229
  ## Control flow
207
230
 
208
231
  ### If / else