1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//! Defines the `aip_flow` module, used in the lua engine.
//!
//! ---
//!
//! ## Lua Documentation
//!
//! The `aip.flow` module allows controlling the flow of AIPACK agent execution.
//! The flow functions are designed to be returned so that the Agent Executor can act appropriately.
//!
//! ### Functions
//!
//! - `aip.flow.before_all_response(data: BeforeAllData) -> table`
//! - `aip.flow.data_response(data: DataData) -> table`
//! - `aip.flow.skip(reason?: string) -> table`
//! - `aip.flow.redo_run() -> table`
use crateResult;
use crateRuntime;
use ;
// region: --- Lua Functions
/// ## Lua Documentation
///
/// Customize the aipack execution flow at the 'Before All' stage.
///
/// This function is typically called within the `before_all` block of an agent script
/// to override the default behavior of passing all initial inputs to the agent.
///
/// ```lua
/// -- API Signature
/// aip.flow.before_all_response(data: BeforeAllData) -> table
/// ```
///
/// ### Arguments
///
/// - `data: table` - A table defining the new inputs and options for the agent execution cycle.
/// ```ts
/// type BeforeAllData = {
/// inputs?: any[], // Optional. A list of new inputs to use for the agent run cycle. Overrides initial inputs.
/// options?: AgentOptions, // Optional. Partial AgentOptions to override for this run.
/// before_all?: any, // Optional. The before_all data that can be access via before_all...
/// } & any // Can also include other arbitrary data fields if needed.
/// ```
///
///
/// ### Example
///
/// ```lua
/// local result = aip.flow.before_all_response({
/// inputs = {"processed_input_1", "processed_input_2"},
/// options = {
/// model = "gemini-2.5-flash",
/// input_concurrency = 3
/// },
/// before_all = {some_data = "hello world" } -- Arbitrary data is allowed
/// })
/// -- The agent executor will process this result table.
/// ```
///
/// ### Error
///
/// This function does not directly return any errors. Errors might occur during the creation of lua table.
///
/// ## Internal (not for Lua doc
///
/// Internaly this returns a Lua table with a specific structure recognized by AIPACK's executor.
/// This return value does not typically need to be captured or used by the script itself;
/// it serves as a directive for the AIPACK execution engine.
///
/// ```ts
/// type AipackFlowResponse = {
/// _aipack_: {
/// kind: "BeforeAllResponse",
/// data: BeforeAllData // The data table passed as argument
/// }
/// }
/// ```
/// ## Lua Documentation
///
/// Customize the aipack execution flow at the 'Data' stage for a single input.
///
/// This function is typically called within the `data` block of an agent script.
/// It allows overriding the input and/or options for the current input cycle,
/// or returning additional arbitrary data.
///
/// ```lua
/// -- API Signature
/// aip.flow.data_response(data: DataData) -> table
/// ```
///
/// ### Arguments
///
/// - `data: table` - A table defining the new input, options, and/or other data for the current cycle.
/// ```ts
/// type DataData = {
/// input?: any | nil, // Optional. The new input to use for this cycle. If nil, the original input is used.
/// data?: any | nil, // Data that will be available in the next stage. Same as returning a simple data.
/// options?: AgentOptions // Optional. Partial AgentOptions to override for this cycle.
/// } & any // Can also include other arbitrary data fields (e.g., computed values, flags)
/// ```
///
/// ### Example
///
/// ```lua
/// -- Use a transformed input and override the model for this cycle
/// return aip.flow.data_response({
/// data = data, -- The data that would have been returned
/// input = transformed_input,
/// options = { model = "gpt-5" },
/// })
/// -- The agent executor will process this result table.
/// ```
///
/// ### Error
///
/// This function does not directly return any errors. Errors might occur during the creation of lua table.
///
/// ## Internal (not for Lua doc)
///
/// Internaly this returns a Lua table with a specific structure recognized by AIPACK's executor.
/// This return value does not typically need to be captured or used by the script itself;
/// it serves as a directive for the AIPACK execution engine.
///
/// ```ts
/// type AipackFlowResponse = {
/// _aipack_: {
/// kind: "DataResponse",
/// data: DataData // The data table passed as argument
/// }
/// }
/// ```
/// ## Lua Documentation
///
/// Returns a response indicating a skip action for the current input cycle.
///
/// This function is typically called within the `data` block of an agent script
/// to instruct AIPACK to skip processing the current input value and move to the next one.
///
/// ```lua
/// -- API Signature
/// aip.flow.skip(reason?: string) -> table
/// ```
///
/// ### Arguments
///
/// - `reason: string (optional)`: An optional string providing the reason for skipping the input cycle.
/// This reason might be logged or displayed depending on the AIPACK execution context.
///
/// ### Example
///
/// ```lua
/// -- Skip processing if the input is nil or empty
/// if input == nil or input == "" then
/// return aip.flow.skip("Input is empty")
/// end
/// -- Continue processing the input if not skipped
/// -- ... rest of data block ...
/// ```
///
/// ### Error
///
/// This function does not directly return any errors. Errors might occur during the creation of lua table.
///
/// ## Internal (not for Lua doc)
///
/// Internaly this returns a Lua table with a specific structure recognized by AIPACK's executor.
/// This return value does not typically need to be captured or used by the script itself;
/// it serves as a directive for the AIPACK execution engine.
///
/// ```ts
/// type AipackFlowResponse = {
/// _aipack_: {
/// kind: "Skip",
/// data: {
/// reason: string | nil // The optional reason provided
/// }
/// }
/// }
/// ```
/// ## Lua Documentation
///
/// Returns a response instructing AIPACK to redo the entire agent execution.
///
/// When this function is called and its result is returned from any stage (Before All, Data, or Output),
/// the current agent execution will finish its current task(s) and then trigger a complete re-execution
/// of the agent using the same initial arguments but with the latest agent file content.
///
/// ```lua
/// -- API Signature
/// aip.flow.redo_run() -> table
/// ```
///
/// ### Example
///
/// ```lua
/// -- Trigger a redo if some condition is met in the output stage
/// if ai_response.content == "RETRY" then
/// return aip.flow.redo_run()
/// end
/// ```
///
/// ### Error
///
/// This function does not directly return any errors. Errors might occur during the creation of lua table.
///
/// ## Internal (not for Lua doc)
///
/// Internaly this returns a Lua table with a specific structure recognized by AIPACK's executor.
///
/// ```ts
/// type AipackFlowResponse = {
/// _aipack_: {
/// kind: "Redo"
/// }
/// }
/// ```
// endregion: --- Lua Functions
// region: --- Tests
// endregion: --- Tests