@grimoirelabs/core 0.17.5 → 0.19.0
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/dist/compiler/expression-parser.d.ts.map +1 -1
- package/dist/compiler/expression-parser.js +5 -1
- package/dist/compiler/expression-parser.js.map +1 -1
- package/dist/compiler/grimoire/formatter.d.ts +13 -0
- package/dist/compiler/grimoire/formatter.d.ts.map +1 -0
- package/dist/compiler/grimoire/formatter.js +831 -0
- package/dist/compiler/grimoire/formatter.js.map +1 -0
- package/dist/compiler/grimoire/index.d.ts +1 -0
- package/dist/compiler/grimoire/index.d.ts.map +1 -1
- package/dist/compiler/grimoire/index.js +1 -0
- package/dist/compiler/grimoire/index.js.map +1 -1
- package/dist/compiler/index.d.ts +1 -1
- package/dist/compiler/index.d.ts.map +1 -1
- package/dist/compiler/index.js +1 -1
- package/dist/compiler/index.js.map +1 -1
- package/dist/compiler/type-checker.d.ts.map +1 -1
- package/dist/compiler/type-checker.js +16 -1
- package/dist/compiler/type-checker.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/runtime/context.d.ts.map +1 -1
- package/dist/runtime/context.js +8 -0
- package/dist/runtime/context.js.map +1 -1
- package/dist/runtime/expression-evaluator.d.ts +2 -1
- package/dist/runtime/expression-evaluator.d.ts.map +1 -1
- package/dist/runtime/expression-evaluator.js +25 -7
- package/dist/runtime/expression-evaluator.js.map +1 -1
- package/dist/types/expressions.d.ts +1 -1
- package/dist/types/expressions.d.ts.map +1 -1
- package/dist/types/expressions.js.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/query-provider.d.ts +15 -4
- package/dist/types/query-provider.d.ts.map +1 -1
- package/dist/types/query-provider.js +1 -2
- package/dist/types/query-provider.js.map +1 -1
- package/dist/venues/types.d.ts +3 -0
- package/dist/venues/types.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,831 @@
|
|
|
1
|
+
import { GrimoireError } from "./errors.js";
|
|
2
|
+
import { parse } from "./parser.js";
|
|
3
|
+
export function formatGrimoire(source) {
|
|
4
|
+
try {
|
|
5
|
+
const ast = parse(source);
|
|
6
|
+
return {
|
|
7
|
+
success: true,
|
|
8
|
+
formatted: new GrimoireFormatter().format(ast),
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
catch (error) {
|
|
12
|
+
const err = error;
|
|
13
|
+
const grimoireError = error instanceof GrimoireError ? error : undefined;
|
|
14
|
+
return {
|
|
15
|
+
success: false,
|
|
16
|
+
error: {
|
|
17
|
+
code: grimoireError?.code ?? "ERR_FORMAT_RUNTIME",
|
|
18
|
+
message: err.message,
|
|
19
|
+
line: grimoireError?.location?.line,
|
|
20
|
+
column: grimoireError?.location?.column,
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const INDENT = " ";
|
|
26
|
+
class GrimoireFormatter {
|
|
27
|
+
lines = [];
|
|
28
|
+
format(ast) {
|
|
29
|
+
this.line(0, `spell ${ast.name} {`);
|
|
30
|
+
const topLevelWriters = [];
|
|
31
|
+
if (ast.imports.length > 0) {
|
|
32
|
+
topLevelWriters.push(() => {
|
|
33
|
+
for (const item of ast.imports) {
|
|
34
|
+
this.writeImport(item);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
if (ast.sections.length > 0) {
|
|
39
|
+
topLevelWriters.push(() => {
|
|
40
|
+
for (const section of ast.sections) {
|
|
41
|
+
this.writeSection(section);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
if (ast.blocks.length > 0) {
|
|
46
|
+
topLevelWriters.push(() => {
|
|
47
|
+
for (const block of ast.blocks) {
|
|
48
|
+
this.writeBlock(block);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
if (ast.triggers.length > 0) {
|
|
53
|
+
topLevelWriters.push(() => {
|
|
54
|
+
for (const trigger of ast.triggers) {
|
|
55
|
+
this.writeTrigger(trigger);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
topLevelWriters.forEach((write, index) => {
|
|
60
|
+
if (index > 0) {
|
|
61
|
+
this.blankLine();
|
|
62
|
+
}
|
|
63
|
+
write();
|
|
64
|
+
});
|
|
65
|
+
this.line(0, "}");
|
|
66
|
+
return `${this.lines.join("\n")}\n`;
|
|
67
|
+
}
|
|
68
|
+
writeImport(node) {
|
|
69
|
+
const alias = node.alias ? ` as ${node.alias}` : "";
|
|
70
|
+
this.line(1, `import ${this.quote(node.path)}${alias}`);
|
|
71
|
+
}
|
|
72
|
+
writeSection(section) {
|
|
73
|
+
switch (section.kind) {
|
|
74
|
+
case "version":
|
|
75
|
+
this.writeVersionSection(section);
|
|
76
|
+
return;
|
|
77
|
+
case "description":
|
|
78
|
+
this.line(1, `description: ${this.quote(section.value)}`);
|
|
79
|
+
return;
|
|
80
|
+
case "assets":
|
|
81
|
+
this.writeAssetsSection(section);
|
|
82
|
+
return;
|
|
83
|
+
case "params":
|
|
84
|
+
this.writeParamsSection(section);
|
|
85
|
+
return;
|
|
86
|
+
case "limits":
|
|
87
|
+
this.writeLimitsSection(section);
|
|
88
|
+
return;
|
|
89
|
+
case "venues":
|
|
90
|
+
this.writeVenuesSection(section);
|
|
91
|
+
return;
|
|
92
|
+
case "state":
|
|
93
|
+
this.writeStateSection(section);
|
|
94
|
+
return;
|
|
95
|
+
case "skills":
|
|
96
|
+
this.writeSkillsSection(section);
|
|
97
|
+
return;
|
|
98
|
+
case "advisors":
|
|
99
|
+
this.writeAdvisorsSection(section);
|
|
100
|
+
return;
|
|
101
|
+
case "guards":
|
|
102
|
+
this.writeGuardsSection(section);
|
|
103
|
+
return;
|
|
104
|
+
default:
|
|
105
|
+
this.assertNever(section);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
writeVersionSection(section) {
|
|
109
|
+
this.line(1, `version: ${this.quote(section.value)}`);
|
|
110
|
+
}
|
|
111
|
+
writeAssetsSection(section) {
|
|
112
|
+
if (section.items.length === 0) {
|
|
113
|
+
this.line(1, "assets: []");
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const simpleAssets = section.items.every((item) => item.chain === undefined && item.address === undefined && item.decimals === undefined);
|
|
117
|
+
if (simpleAssets) {
|
|
118
|
+
const symbols = section.items.map((item) => this.symbol(item.symbol)).join(", ");
|
|
119
|
+
this.line(1, `assets: [${symbols}]`);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
this.line(1, "assets: {");
|
|
123
|
+
for (const item of section.items) {
|
|
124
|
+
this.writeAssetItem(item, 2);
|
|
125
|
+
}
|
|
126
|
+
this.line(1, "}");
|
|
127
|
+
}
|
|
128
|
+
writeAssetItem(item, indent) {
|
|
129
|
+
const hasDetail = item.chain !== undefined || item.address !== undefined || item.decimals !== undefined;
|
|
130
|
+
if (!hasDetail) {
|
|
131
|
+
this.line(indent, `${this.symbol(item.symbol)}: ${this.symbol(item.symbol)}`);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
this.line(indent, `${this.symbol(item.symbol)}: {`);
|
|
135
|
+
if (item.chain !== undefined) {
|
|
136
|
+
this.line(indent + 1, `chain: ${item.chain}`);
|
|
137
|
+
}
|
|
138
|
+
if (item.address !== undefined) {
|
|
139
|
+
this.line(indent + 1, `address: ${item.address}`);
|
|
140
|
+
}
|
|
141
|
+
if (item.decimals !== undefined) {
|
|
142
|
+
this.line(indent + 1, `decimals: ${item.decimals}`);
|
|
143
|
+
}
|
|
144
|
+
this.line(indent, "}");
|
|
145
|
+
}
|
|
146
|
+
writeParamsSection(section) {
|
|
147
|
+
this.line(1, "params: {");
|
|
148
|
+
for (const item of section.items) {
|
|
149
|
+
this.writeParamItem(item, 2);
|
|
150
|
+
}
|
|
151
|
+
this.line(1, "}");
|
|
152
|
+
}
|
|
153
|
+
writeParamItem(item, indent) {
|
|
154
|
+
const isSimple = item.type === undefined &&
|
|
155
|
+
item.asset === undefined &&
|
|
156
|
+
item.min === undefined &&
|
|
157
|
+
item.max === undefined &&
|
|
158
|
+
item.value !== undefined;
|
|
159
|
+
if (isSimple) {
|
|
160
|
+
const simpleValue = item.value;
|
|
161
|
+
if (simpleValue !== undefined) {
|
|
162
|
+
this.line(indent, `${item.name}: ${this.expr(simpleValue)}`);
|
|
163
|
+
}
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
this.line(indent, `${item.name}: {`);
|
|
167
|
+
if (item.type !== undefined) {
|
|
168
|
+
this.line(indent + 1, `type: ${this.symbol(item.type)}`);
|
|
169
|
+
}
|
|
170
|
+
if (item.asset !== undefined) {
|
|
171
|
+
this.line(indent + 1, `asset: ${this.symbol(item.asset)}`);
|
|
172
|
+
}
|
|
173
|
+
if (item.value !== undefined) {
|
|
174
|
+
this.line(indent + 1, `default: ${this.expr(item.value)}`);
|
|
175
|
+
}
|
|
176
|
+
if (item.min !== undefined) {
|
|
177
|
+
this.line(indent + 1, `min: ${item.min}`);
|
|
178
|
+
}
|
|
179
|
+
if (item.max !== undefined) {
|
|
180
|
+
this.line(indent + 1, `max: ${item.max}`);
|
|
181
|
+
}
|
|
182
|
+
this.line(indent, "}");
|
|
183
|
+
}
|
|
184
|
+
writeLimitsSection(section) {
|
|
185
|
+
this.line(1, "limits: {");
|
|
186
|
+
for (const item of section.items) {
|
|
187
|
+
this.writeLimitItem(item, 2);
|
|
188
|
+
}
|
|
189
|
+
this.line(1, "}");
|
|
190
|
+
}
|
|
191
|
+
writeLimitItem(item, indent) {
|
|
192
|
+
this.line(indent, `${item.name}: ${this.expr(item.value)}`);
|
|
193
|
+
}
|
|
194
|
+
writeVenuesSection(section) {
|
|
195
|
+
this.line(1, "venues: {");
|
|
196
|
+
for (const group of section.groups) {
|
|
197
|
+
this.writeVenueGroup(group, 2);
|
|
198
|
+
}
|
|
199
|
+
this.line(1, "}");
|
|
200
|
+
}
|
|
201
|
+
writeVenueGroup(group, indent) {
|
|
202
|
+
if (group.venues.length === 1) {
|
|
203
|
+
const only = group.venues[0];
|
|
204
|
+
if (only) {
|
|
205
|
+
this.line(indent, `${group.name}: @${only.name}`);
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
const refs = group.venues.map((venue) => `@${venue.name}`).join(", ");
|
|
210
|
+
this.line(indent, `${group.name}: [${refs}]`);
|
|
211
|
+
}
|
|
212
|
+
writeStateSection(section) {
|
|
213
|
+
this.line(1, "state: {");
|
|
214
|
+
this.writeStateScope("persistent", section.persistent, 2);
|
|
215
|
+
this.writeStateScope("ephemeral", section.ephemeral, 2);
|
|
216
|
+
this.line(1, "}");
|
|
217
|
+
}
|
|
218
|
+
writeStateScope(name, items, indent) {
|
|
219
|
+
this.line(indent, `${name}: {`);
|
|
220
|
+
for (const item of items) {
|
|
221
|
+
this.line(indent + 1, `${item.name}: ${this.expr(item.initialValue)}`);
|
|
222
|
+
}
|
|
223
|
+
this.line(indent, "}");
|
|
224
|
+
}
|
|
225
|
+
writeSkillsSection(section) {
|
|
226
|
+
this.line(1, "skills: {");
|
|
227
|
+
for (const item of section.items) {
|
|
228
|
+
this.writeSkillItem(item, 2);
|
|
229
|
+
}
|
|
230
|
+
this.line(1, "}");
|
|
231
|
+
}
|
|
232
|
+
writeSkillItem(item, indent) {
|
|
233
|
+
this.line(indent, `${item.name}: {`);
|
|
234
|
+
this.line(indent + 1, `type: ${item.type}`);
|
|
235
|
+
const adapters = item.adapters.map((adapter) => this.symbol(adapter)).join(", ");
|
|
236
|
+
this.line(indent + 1, `adapters: [${adapters}]`);
|
|
237
|
+
if (item.defaultConstraints?.maxSlippage !== undefined) {
|
|
238
|
+
this.line(indent + 1, "default_constraints: {");
|
|
239
|
+
this.line(indent + 2, `max_slippage: ${this.expr(item.defaultConstraints.maxSlippage)}`);
|
|
240
|
+
this.line(indent + 1, "}");
|
|
241
|
+
}
|
|
242
|
+
this.line(indent, "}");
|
|
243
|
+
}
|
|
244
|
+
writeAdvisorsSection(section) {
|
|
245
|
+
this.line(1, "advisors: {");
|
|
246
|
+
for (const item of section.items) {
|
|
247
|
+
this.writeAdvisorItem(item, 2);
|
|
248
|
+
}
|
|
249
|
+
this.line(1, "}");
|
|
250
|
+
}
|
|
251
|
+
writeAdvisorItem(item, indent) {
|
|
252
|
+
this.line(indent, `${item.name}: {`);
|
|
253
|
+
this.line(indent + 1, `model: ${this.quote(item.model)}`);
|
|
254
|
+
if (item.systemPrompt !== undefined) {
|
|
255
|
+
this.line(indent + 1, `system_prompt: ${this.quote(item.systemPrompt)}`);
|
|
256
|
+
}
|
|
257
|
+
if (item.skills !== undefined) {
|
|
258
|
+
this.line(indent + 1, `skills: ${this.stringList(item.skills)}`);
|
|
259
|
+
}
|
|
260
|
+
if (item.allowedTools !== undefined) {
|
|
261
|
+
this.line(indent + 1, `allowed_tools: ${this.stringList(item.allowedTools)}`);
|
|
262
|
+
}
|
|
263
|
+
if (item.mcp !== undefined) {
|
|
264
|
+
this.line(indent + 1, `mcp: ${this.stringList(item.mcp)}`);
|
|
265
|
+
}
|
|
266
|
+
if (item.timeout !== undefined) {
|
|
267
|
+
this.line(indent + 1, `timeout: ${item.timeout}`);
|
|
268
|
+
}
|
|
269
|
+
if (item.fallback !== undefined) {
|
|
270
|
+
this.line(indent + 1, `fallback: ${item.fallback ? "true" : "false"}`);
|
|
271
|
+
}
|
|
272
|
+
if (item.maxPerRun !== undefined || item.maxPerHour !== undefined) {
|
|
273
|
+
this.line(indent + 1, "rate_limit: {");
|
|
274
|
+
if (item.maxPerRun !== undefined) {
|
|
275
|
+
this.line(indent + 2, `max_per_run: ${item.maxPerRun}`);
|
|
276
|
+
}
|
|
277
|
+
if (item.maxPerHour !== undefined) {
|
|
278
|
+
this.line(indent + 2, `max_per_hour: ${item.maxPerHour}`);
|
|
279
|
+
}
|
|
280
|
+
this.line(indent + 1, "}");
|
|
281
|
+
}
|
|
282
|
+
this.line(indent, "}");
|
|
283
|
+
}
|
|
284
|
+
writeGuardsSection(section) {
|
|
285
|
+
this.line(1, "guards: {");
|
|
286
|
+
for (const item of section.items) {
|
|
287
|
+
this.writeGuardItem(item, 2);
|
|
288
|
+
}
|
|
289
|
+
this.line(1, "}");
|
|
290
|
+
}
|
|
291
|
+
writeGuardItem(item, indent) {
|
|
292
|
+
const metadata = [];
|
|
293
|
+
if (item.severity !== "halt") {
|
|
294
|
+
metadata.push({
|
|
295
|
+
key: "severity",
|
|
296
|
+
value: {
|
|
297
|
+
kind: "literal",
|
|
298
|
+
literalType: "string",
|
|
299
|
+
value: item.severity,
|
|
300
|
+
},
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
if (item.message !== undefined) {
|
|
304
|
+
metadata.push({
|
|
305
|
+
key: "message",
|
|
306
|
+
value: {
|
|
307
|
+
kind: "literal",
|
|
308
|
+
literalType: "string",
|
|
309
|
+
value: item.message,
|
|
310
|
+
},
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
if (item.fallback !== undefined) {
|
|
314
|
+
metadata.push({
|
|
315
|
+
key: "fallback",
|
|
316
|
+
value: {
|
|
317
|
+
kind: "literal",
|
|
318
|
+
literalType: "boolean",
|
|
319
|
+
value: item.fallback,
|
|
320
|
+
},
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
const suffix = metadata.length > 0
|
|
324
|
+
? ` ${this.constraintClause({ kind: "constraint_clause", constraints: metadata })}`
|
|
325
|
+
: "";
|
|
326
|
+
this.line(indent, `${item.id}: ${this.expr(item.check)}${suffix}`);
|
|
327
|
+
}
|
|
328
|
+
writeBlock(block) {
|
|
329
|
+
const params = block.params.length > 0 ? `(${block.params.join(", ")})` : "";
|
|
330
|
+
this.line(1, `block ${block.name}${params} {`);
|
|
331
|
+
this.writeStatementBlock(block.body, 2);
|
|
332
|
+
this.line(1, "}");
|
|
333
|
+
}
|
|
334
|
+
writeTrigger(trigger) {
|
|
335
|
+
this.line(1, `on ${this.triggerType(trigger.trigger)}: {`);
|
|
336
|
+
this.writeStatementBlock(trigger.body, 2);
|
|
337
|
+
this.line(1, "}");
|
|
338
|
+
}
|
|
339
|
+
triggerType(trigger) {
|
|
340
|
+
switch (trigger.kind) {
|
|
341
|
+
case "manual":
|
|
342
|
+
case "hourly":
|
|
343
|
+
case "daily":
|
|
344
|
+
return trigger.kind;
|
|
345
|
+
case "schedule":
|
|
346
|
+
return this.quote(trigger.cron);
|
|
347
|
+
case "condition": {
|
|
348
|
+
const every = trigger.pollInterval !== undefined ? ` every ${trigger.pollInterval}` : "";
|
|
349
|
+
return `condition ${this.expr(trigger.expression)}${every}`;
|
|
350
|
+
}
|
|
351
|
+
case "event": {
|
|
352
|
+
const event = this.symbol(trigger.event);
|
|
353
|
+
const filter = trigger.filter ? ` where ${this.expr(trigger.filter)}` : "";
|
|
354
|
+
return `event ${event}${filter}`;
|
|
355
|
+
}
|
|
356
|
+
default:
|
|
357
|
+
return this.assertNever(trigger);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
writeStatementBlock(statements, indent) {
|
|
361
|
+
for (const statement of statements) {
|
|
362
|
+
this.writeStatement(statement, indent);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
writeStatement(statement, indent) {
|
|
366
|
+
switch (statement.kind) {
|
|
367
|
+
case "assignment":
|
|
368
|
+
this.writeAssignment(statement, indent);
|
|
369
|
+
return;
|
|
370
|
+
case "advise":
|
|
371
|
+
this.writeAdvise(statement, indent);
|
|
372
|
+
return;
|
|
373
|
+
case "if":
|
|
374
|
+
this.writeIf(statement, indent);
|
|
375
|
+
return;
|
|
376
|
+
case "for":
|
|
377
|
+
this.writeFor(statement, indent);
|
|
378
|
+
return;
|
|
379
|
+
case "repeat":
|
|
380
|
+
this.writeRepeat(statement, indent);
|
|
381
|
+
return;
|
|
382
|
+
case "until":
|
|
383
|
+
this.writeUntil(statement, indent);
|
|
384
|
+
return;
|
|
385
|
+
case "try":
|
|
386
|
+
this.writeTry(statement, indent);
|
|
387
|
+
return;
|
|
388
|
+
case "parallel":
|
|
389
|
+
this.writeParallel(statement, indent);
|
|
390
|
+
return;
|
|
391
|
+
case "pipeline":
|
|
392
|
+
this.writePipeline(statement, indent);
|
|
393
|
+
return;
|
|
394
|
+
case "do":
|
|
395
|
+
this.writeDo(statement, indent);
|
|
396
|
+
return;
|
|
397
|
+
case "atomic":
|
|
398
|
+
this.writeAtomic(statement, indent);
|
|
399
|
+
return;
|
|
400
|
+
case "method_call":
|
|
401
|
+
this.writeMethodCall(statement, indent);
|
|
402
|
+
return;
|
|
403
|
+
case "emit":
|
|
404
|
+
this.writeEmit(statement, indent);
|
|
405
|
+
return;
|
|
406
|
+
case "halt":
|
|
407
|
+
this.writeHalt(statement, indent);
|
|
408
|
+
return;
|
|
409
|
+
case "wait":
|
|
410
|
+
this.writeWait(statement, indent);
|
|
411
|
+
return;
|
|
412
|
+
case "pass":
|
|
413
|
+
this.line(indent, "pass");
|
|
414
|
+
return;
|
|
415
|
+
case "advisory":
|
|
416
|
+
throw new Error("Inline advisory statements are no longer supported by the parser");
|
|
417
|
+
default:
|
|
418
|
+
this.assertNever(statement);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
writeAssignment(statement, indent) {
|
|
422
|
+
let line = `${statement.target} = ${this.expr(statement.value)}`;
|
|
423
|
+
if (statement.skill !== undefined) {
|
|
424
|
+
line += ` using ${this.symbol(statement.skill)}`;
|
|
425
|
+
}
|
|
426
|
+
if (statement.constraints !== undefined) {
|
|
427
|
+
line += ` ${this.constraintClause(statement.constraints)}`;
|
|
428
|
+
}
|
|
429
|
+
this.line(indent, line);
|
|
430
|
+
}
|
|
431
|
+
writeAdvise(statement, indent) {
|
|
432
|
+
this.line(indent, `${statement.target} = advise ${statement.advisor}: ${this.quote(statement.prompt)} {`);
|
|
433
|
+
if (statement.context !== undefined) {
|
|
434
|
+
this.line(indent + 1, "context: {");
|
|
435
|
+
for (const [key, value] of Object.entries(statement.context)) {
|
|
436
|
+
this.line(indent + 2, `${key}: ${this.expr(value)}`);
|
|
437
|
+
}
|
|
438
|
+
this.line(indent + 1, "}");
|
|
439
|
+
}
|
|
440
|
+
if (statement.within !== undefined) {
|
|
441
|
+
this.line(indent + 1, `within: ${this.symbol(statement.within)}`);
|
|
442
|
+
}
|
|
443
|
+
this.line(indent + 1, "output: {");
|
|
444
|
+
this.writeOutputSchema(statement.outputSchema, indent + 2);
|
|
445
|
+
this.line(indent + 1, "}");
|
|
446
|
+
if (statement.onViolationExplicit || statement.onViolation !== "reject") {
|
|
447
|
+
this.line(indent + 1, `on_violation: ${statement.onViolation ?? "reject"}`);
|
|
448
|
+
}
|
|
449
|
+
if (statement.clampConstraints !== undefined) {
|
|
450
|
+
this.line(indent + 1, `clamp_constraints: ${this.stringList(statement.clampConstraints)}`);
|
|
451
|
+
}
|
|
452
|
+
this.line(indent + 1, `timeout: ${statement.timeout}`);
|
|
453
|
+
this.line(indent + 1, `fallback: ${this.expr(statement.fallback)}`);
|
|
454
|
+
this.line(indent, "}");
|
|
455
|
+
}
|
|
456
|
+
writeOutputSchema(schema, indent) {
|
|
457
|
+
this.line(indent, `type: ${schema.type}`);
|
|
458
|
+
if (schema.values !== undefined) {
|
|
459
|
+
this.line(indent, `values: ${this.stringList(schema.values)}`);
|
|
460
|
+
}
|
|
461
|
+
if (schema.min !== undefined) {
|
|
462
|
+
this.line(indent, `min: ${schema.min}`);
|
|
463
|
+
}
|
|
464
|
+
if (schema.max !== undefined) {
|
|
465
|
+
this.line(indent, `max: ${schema.max}`);
|
|
466
|
+
}
|
|
467
|
+
if (schema.minLength !== undefined) {
|
|
468
|
+
this.line(indent, `min_length: ${schema.minLength}`);
|
|
469
|
+
}
|
|
470
|
+
if (schema.maxLength !== undefined) {
|
|
471
|
+
this.line(indent, `max_length: ${schema.maxLength}`);
|
|
472
|
+
}
|
|
473
|
+
if (schema.pattern !== undefined) {
|
|
474
|
+
this.line(indent, `pattern: ${this.quote(schema.pattern)}`);
|
|
475
|
+
}
|
|
476
|
+
if (schema.fields !== undefined) {
|
|
477
|
+
this.line(indent, "fields: {");
|
|
478
|
+
for (const [key, value] of Object.entries(schema.fields)) {
|
|
479
|
+
if (this.schemaCanInline(value)) {
|
|
480
|
+
this.line(indent + 1, `${key}: ${value.type}`);
|
|
481
|
+
}
|
|
482
|
+
else {
|
|
483
|
+
this.line(indent + 1, `${key}: {`);
|
|
484
|
+
this.writeOutputSchema(value, indent + 2);
|
|
485
|
+
this.line(indent + 1, "}");
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
this.line(indent, "}");
|
|
489
|
+
}
|
|
490
|
+
if (schema.items !== undefined) {
|
|
491
|
+
if (this.schemaCanInline(schema.items)) {
|
|
492
|
+
this.line(indent, `items: ${schema.items.type}`);
|
|
493
|
+
}
|
|
494
|
+
else {
|
|
495
|
+
this.line(indent, "items: {");
|
|
496
|
+
this.writeOutputSchema(schema.items, indent + 1);
|
|
497
|
+
this.line(indent, "}");
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
schemaCanInline(schema) {
|
|
502
|
+
return (schema.values === undefined &&
|
|
503
|
+
schema.min === undefined &&
|
|
504
|
+
schema.max === undefined &&
|
|
505
|
+
schema.minLength === undefined &&
|
|
506
|
+
schema.maxLength === undefined &&
|
|
507
|
+
schema.pattern === undefined &&
|
|
508
|
+
schema.fields === undefined &&
|
|
509
|
+
schema.items === undefined);
|
|
510
|
+
}
|
|
511
|
+
writeIf(statement, indent) {
|
|
512
|
+
this.line(indent, `if ${this.expr(statement.condition)} {`);
|
|
513
|
+
this.writeStatementBlock(statement.thenBody, indent + 1);
|
|
514
|
+
this.line(indent, "}");
|
|
515
|
+
for (const branch of statement.elifs) {
|
|
516
|
+
this.line(indent, `elif ${this.expr(branch.condition)} {`);
|
|
517
|
+
this.writeStatementBlock(branch.body, indent + 1);
|
|
518
|
+
this.line(indent, "}");
|
|
519
|
+
}
|
|
520
|
+
if (statement.elseBody.length > 0) {
|
|
521
|
+
this.line(indent, "else {");
|
|
522
|
+
this.writeStatementBlock(statement.elseBody, indent + 1);
|
|
523
|
+
this.line(indent, "}");
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
writeFor(statement, indent) {
|
|
527
|
+
const maxIterations = statement.maxIterations !== undefined ? ` max ${statement.maxIterations}` : "";
|
|
528
|
+
this.line(indent, `for ${statement.variable} in ${this.expr(statement.iterable)}${maxIterations} {`);
|
|
529
|
+
this.writeStatementBlock(statement.body, indent + 1);
|
|
530
|
+
this.line(indent, "}");
|
|
531
|
+
}
|
|
532
|
+
writeRepeat(statement, indent) {
|
|
533
|
+
this.line(indent, `repeat ${this.expr(statement.count)} {`);
|
|
534
|
+
this.writeStatementBlock(statement.body, indent + 1);
|
|
535
|
+
this.line(indent, "}");
|
|
536
|
+
}
|
|
537
|
+
writeUntil(statement, indent) {
|
|
538
|
+
const maxIterations = statement.maxIterations !== undefined ? ` max ${statement.maxIterations}` : "";
|
|
539
|
+
this.line(indent, `loop until ${this.expr(statement.condition)}${maxIterations} {`);
|
|
540
|
+
this.writeStatementBlock(statement.body, indent + 1);
|
|
541
|
+
this.line(indent, "}");
|
|
542
|
+
}
|
|
543
|
+
writeTry(statement, indent) {
|
|
544
|
+
this.line(indent, "try {");
|
|
545
|
+
this.writeStatementBlock(statement.tryBody, indent + 1);
|
|
546
|
+
this.line(indent, "}");
|
|
547
|
+
for (const catchNode of statement.catches) {
|
|
548
|
+
this.writeCatch(catchNode, indent);
|
|
549
|
+
}
|
|
550
|
+
if (statement.finallyBody !== undefined) {
|
|
551
|
+
this.line(indent, "finally {");
|
|
552
|
+
this.writeStatementBlock(statement.finallyBody, indent + 1);
|
|
553
|
+
this.line(indent, "}");
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
writeCatch(node, indent) {
|
|
557
|
+
this.line(indent, `catch ${node.error} {`);
|
|
558
|
+
if (node.action !== undefined) {
|
|
559
|
+
this.line(indent + 1, `action: ${node.action}`);
|
|
560
|
+
}
|
|
561
|
+
if (node.retry !== undefined) {
|
|
562
|
+
this.line(indent + 1, "retry: {");
|
|
563
|
+
this.line(indent + 2, `max_attempts: ${node.retry.maxAttempts}`);
|
|
564
|
+
this.line(indent + 2, `backoff: ${node.retry.backoff}`);
|
|
565
|
+
if (node.retry.backoffBase !== undefined) {
|
|
566
|
+
this.line(indent + 2, `backoff_base: ${node.retry.backoffBase}`);
|
|
567
|
+
}
|
|
568
|
+
if (node.retry.maxBackoff !== undefined) {
|
|
569
|
+
this.line(indent + 2, `max_backoff: ${node.retry.maxBackoff}`);
|
|
570
|
+
}
|
|
571
|
+
this.line(indent + 1, "}");
|
|
572
|
+
}
|
|
573
|
+
this.writeStatementBlock(node.body, indent + 1);
|
|
574
|
+
this.line(indent, "}");
|
|
575
|
+
}
|
|
576
|
+
writeParallel(statement, indent) {
|
|
577
|
+
const headerParts = [];
|
|
578
|
+
if (statement.join !== undefined) {
|
|
579
|
+
headerParts.push(`join=${statement.join.type}`);
|
|
580
|
+
if (statement.join.metric !== undefined) {
|
|
581
|
+
headerParts.push(`metric=${this.expr(statement.join.metric)}`);
|
|
582
|
+
}
|
|
583
|
+
if (statement.join.order !== undefined) {
|
|
584
|
+
headerParts.push(`order=${statement.join.order}`);
|
|
585
|
+
}
|
|
586
|
+
if (statement.join.count !== undefined) {
|
|
587
|
+
headerParts.push(`count=${statement.join.count}`);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
if (statement.onFail !== undefined) {
|
|
591
|
+
headerParts.push(`on_fail=${statement.onFail}`);
|
|
592
|
+
}
|
|
593
|
+
const headerSuffix = headerParts.length > 0 ? ` ${headerParts.join(" ")}` : "";
|
|
594
|
+
this.line(indent, `parallel${headerSuffix} {`);
|
|
595
|
+
for (const branch of statement.branches) {
|
|
596
|
+
this.line(indent + 1, `${branch.name}: {`);
|
|
597
|
+
this.writeStatementBlock(branch.body, indent + 2);
|
|
598
|
+
this.line(indent + 1, "}");
|
|
599
|
+
}
|
|
600
|
+
this.line(indent, "}");
|
|
601
|
+
}
|
|
602
|
+
writePipeline(statement, indent) {
|
|
603
|
+
const prefix = `${statement.outputBinding ? `${statement.outputBinding} = ` : ""}${this.expr(statement.source)}`;
|
|
604
|
+
if (statement.stages.length === 0) {
|
|
605
|
+
this.line(indent, prefix);
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
const firstStage = statement.stages[0];
|
|
609
|
+
if (!firstStage) {
|
|
610
|
+
this.line(indent, prefix);
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
this.line(indent, `${prefix} | ${this.pipelineStageHeader(firstStage)}: {`);
|
|
614
|
+
this.writeStatement(firstStage.step, indent + 1);
|
|
615
|
+
for (let i = 0; i < statement.stages.length - 1; i++) {
|
|
616
|
+
const next = statement.stages[i + 1];
|
|
617
|
+
if (!next)
|
|
618
|
+
continue;
|
|
619
|
+
this.line(indent, `} | ${this.pipelineStageHeader(next)}: {`);
|
|
620
|
+
this.writeStatement(next.step, indent + 1);
|
|
621
|
+
}
|
|
622
|
+
this.line(indent, "}");
|
|
623
|
+
}
|
|
624
|
+
pipelineStageHeader(stage) {
|
|
625
|
+
if (stage.op === "reduce" && stage.initial !== undefined) {
|
|
626
|
+
return `reduce(${this.expr(stage.initial)})`;
|
|
627
|
+
}
|
|
628
|
+
if ((stage.op === "take" || stage.op === "skip") && stage.count !== undefined) {
|
|
629
|
+
return `${stage.op} ${stage.count}`;
|
|
630
|
+
}
|
|
631
|
+
if (stage.op === "sort") {
|
|
632
|
+
const parts = ["sort"];
|
|
633
|
+
if (stage.by !== undefined) {
|
|
634
|
+
parts.push(`by ${this.expr(stage.by)}`);
|
|
635
|
+
}
|
|
636
|
+
if (stage.order !== undefined) {
|
|
637
|
+
parts.push(`order ${stage.order}`);
|
|
638
|
+
}
|
|
639
|
+
return parts.join(" ");
|
|
640
|
+
}
|
|
641
|
+
return stage.op;
|
|
642
|
+
}
|
|
643
|
+
writeDo(statement, indent) {
|
|
644
|
+
const args = statement.args.length > 0
|
|
645
|
+
? `(${statement.args.map((arg) => this.expr(arg)).join(", ")})`
|
|
646
|
+
: "";
|
|
647
|
+
this.line(indent, `do ${statement.name}${args}`);
|
|
648
|
+
}
|
|
649
|
+
writeAtomic(statement, indent) {
|
|
650
|
+
const mode = statement.onFailure ? ` ${statement.onFailure}` : "";
|
|
651
|
+
this.line(indent, `atomic${mode} {`);
|
|
652
|
+
this.writeStatementBlock(statement.body, indent + 1);
|
|
653
|
+
this.line(indent, "}");
|
|
654
|
+
}
|
|
655
|
+
writeMethodCall(statement, indent) {
|
|
656
|
+
let line = `${this.expr(statement.object)}.${statement.method}(${statement.args
|
|
657
|
+
.map((arg) => this.expr(arg))
|
|
658
|
+
.join(", ")})`;
|
|
659
|
+
if (statement.outputBinding !== undefined) {
|
|
660
|
+
line = `${statement.outputBinding} = ${line}`;
|
|
661
|
+
}
|
|
662
|
+
if (statement.skill !== undefined) {
|
|
663
|
+
line += ` using ${this.symbol(statement.skill)}`;
|
|
664
|
+
}
|
|
665
|
+
if (statement.constraints !== undefined) {
|
|
666
|
+
line += ` ${this.constraintClause(statement.constraints)}`;
|
|
667
|
+
}
|
|
668
|
+
this.line(indent, line);
|
|
669
|
+
}
|
|
670
|
+
writeEmit(statement, indent) {
|
|
671
|
+
if (statement.data.length === 0) {
|
|
672
|
+
this.line(indent, `emit ${statement.event}`);
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
const payload = statement.data
|
|
676
|
+
.map((entry) => `${entry.key}=${this.expr(entry.value)}`)
|
|
677
|
+
.join(", ");
|
|
678
|
+
this.line(indent, `emit ${statement.event}(${payload})`);
|
|
679
|
+
}
|
|
680
|
+
writeHalt(statement, indent) {
|
|
681
|
+
this.line(indent, `halt ${this.quote(statement.reason)}`);
|
|
682
|
+
}
|
|
683
|
+
writeWait(statement, indent) {
|
|
684
|
+
this.line(indent, `wait ${statement.duration}`);
|
|
685
|
+
}
|
|
686
|
+
constraintClause(clause) {
|
|
687
|
+
if (clause.constraints.length === 1) {
|
|
688
|
+
const single = clause.constraints[0];
|
|
689
|
+
if (single) {
|
|
690
|
+
return `with ${single.key}=${this.expr(single.value)}`;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
const all = clause.constraints.map((item) => `${item.key}=${this.expr(item.value)}`).join(", ");
|
|
694
|
+
return `with (${all})`;
|
|
695
|
+
}
|
|
696
|
+
expr(node) {
|
|
697
|
+
return this.exprWithPrecedence(node, 0);
|
|
698
|
+
}
|
|
699
|
+
exprWithPrecedence(node, parentPrecedence) {
|
|
700
|
+
switch (node.kind) {
|
|
701
|
+
case "literal":
|
|
702
|
+
return this.literal(node.value, node.literalType);
|
|
703
|
+
case "unit_literal":
|
|
704
|
+
return `${this.number(node.value)} ${node.unit}`;
|
|
705
|
+
case "identifier":
|
|
706
|
+
return node.name;
|
|
707
|
+
case "venue_ref_expr":
|
|
708
|
+
return `@${node.name}`;
|
|
709
|
+
case "advisory_expr":
|
|
710
|
+
return `**${node.prompt}**${node.advisor ? ` via ${node.advisor}` : ""}`;
|
|
711
|
+
case "percentage":
|
|
712
|
+
return `${this.number(node.value * 100)}%`;
|
|
713
|
+
case "binary": {
|
|
714
|
+
const precedence = this.binaryPrecedence(node.op);
|
|
715
|
+
const left = this.exprWithPrecedence(node.left, precedence);
|
|
716
|
+
const right = this.exprWithPrecedence(node.right, precedence + 1);
|
|
717
|
+
return this.wrapIfNeeded(`${left} ${node.op} ${right}`, precedence, parentPrecedence);
|
|
718
|
+
}
|
|
719
|
+
case "unary": {
|
|
720
|
+
const precedence = 8;
|
|
721
|
+
const arg = this.exprWithPrecedence(node.arg, precedence);
|
|
722
|
+
const text = node.op === "not" ? `not ${arg}` : `-${arg}`;
|
|
723
|
+
return this.wrapIfNeeded(text, precedence, parentPrecedence);
|
|
724
|
+
}
|
|
725
|
+
case "ternary": {
|
|
726
|
+
const precedence = 1;
|
|
727
|
+
const condition = this.exprWithPrecedence(node.condition, precedence);
|
|
728
|
+
const thenExpr = this.exprWithPrecedence(node.thenExpr, precedence);
|
|
729
|
+
const elseExpr = this.exprWithPrecedence(node.elseExpr, precedence);
|
|
730
|
+
return this.wrapIfNeeded(`${condition} ? ${thenExpr} : ${elseExpr}`, precedence, parentPrecedence);
|
|
731
|
+
}
|
|
732
|
+
case "call": {
|
|
733
|
+
const callee = this.exprWithPrecedence(node.callee, 9);
|
|
734
|
+
const args = node.args.map((arg) => this.expr(arg));
|
|
735
|
+
const kwargs = node.kwargs?.map((arg) => `${arg.key}=${this.expr(arg.value)}`) ?? [];
|
|
736
|
+
return `${callee}(${[...args, ...kwargs].join(", ")})`;
|
|
737
|
+
}
|
|
738
|
+
case "property_access": {
|
|
739
|
+
const object = this.exprWithPrecedence(node.object, 9);
|
|
740
|
+
return `${object}.${node.property}`;
|
|
741
|
+
}
|
|
742
|
+
case "array_access": {
|
|
743
|
+
const array = this.exprWithPrecedence(node.array, 9);
|
|
744
|
+
return `${array}[${this.expr(node.index)}]`;
|
|
745
|
+
}
|
|
746
|
+
case "array_literal":
|
|
747
|
+
return `[${node.elements.map((element) => this.expr(element)).join(", ")}]`;
|
|
748
|
+
case "object_literal": {
|
|
749
|
+
if (node.entries.length === 0) {
|
|
750
|
+
return "{}";
|
|
751
|
+
}
|
|
752
|
+
const entries = node.entries
|
|
753
|
+
.map((entry) => `${entry.key}: ${this.expr(entry.value)}`)
|
|
754
|
+
.join(", ");
|
|
755
|
+
return `{ ${entries} }`;
|
|
756
|
+
}
|
|
757
|
+
default:
|
|
758
|
+
return this.assertNever(node);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
binaryPrecedence(op) {
|
|
762
|
+
switch (op) {
|
|
763
|
+
case "or":
|
|
764
|
+
return 2;
|
|
765
|
+
case "and":
|
|
766
|
+
return 3;
|
|
767
|
+
case "==":
|
|
768
|
+
case "!=":
|
|
769
|
+
return 4;
|
|
770
|
+
case "<":
|
|
771
|
+
case ">":
|
|
772
|
+
case "<=":
|
|
773
|
+
case ">=":
|
|
774
|
+
return 5;
|
|
775
|
+
case "+":
|
|
776
|
+
case "-":
|
|
777
|
+
return 6;
|
|
778
|
+
case "*":
|
|
779
|
+
case "/":
|
|
780
|
+
case "%":
|
|
781
|
+
return 7;
|
|
782
|
+
default:
|
|
783
|
+
return 1;
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
wrapIfNeeded(text, precedence, parentPrecedence) {
|
|
787
|
+
if (precedence < parentPrecedence) {
|
|
788
|
+
return `(${text})`;
|
|
789
|
+
}
|
|
790
|
+
return text;
|
|
791
|
+
}
|
|
792
|
+
stringList(values) {
|
|
793
|
+
return `[${values.map((value) => this.symbol(value)).join(", ")}]`;
|
|
794
|
+
}
|
|
795
|
+
symbol(value) {
|
|
796
|
+
return this.isSymbol(value) ? value : this.quote(value);
|
|
797
|
+
}
|
|
798
|
+
literal(value, literalType) {
|
|
799
|
+
if (literalType === "string") {
|
|
800
|
+
return this.quote(String(value));
|
|
801
|
+
}
|
|
802
|
+
if (literalType === "boolean") {
|
|
803
|
+
return value ? "true" : "false";
|
|
804
|
+
}
|
|
805
|
+
if (literalType === "address") {
|
|
806
|
+
return String(value);
|
|
807
|
+
}
|
|
808
|
+
return this.number(typeof value === "number" ? value : Number.parseFloat(String(value)));
|
|
809
|
+
}
|
|
810
|
+
quote(value) {
|
|
811
|
+
return JSON.stringify(value);
|
|
812
|
+
}
|
|
813
|
+
number(value) {
|
|
814
|
+
return String(value);
|
|
815
|
+
}
|
|
816
|
+
line(indent, text) {
|
|
817
|
+
this.lines.push(`${INDENT.repeat(indent)}${text}`);
|
|
818
|
+
}
|
|
819
|
+
blankLine() {
|
|
820
|
+
if (this.lines[this.lines.length - 1] !== "") {
|
|
821
|
+
this.lines.push("");
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
isSymbol(value) {
|
|
825
|
+
return /^[A-Za-z_][A-Za-z0-9_]*$/.test(value);
|
|
826
|
+
}
|
|
827
|
+
assertNever(value) {
|
|
828
|
+
throw new Error(`Unhandled formatter node: ${JSON.stringify(value)}`);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
//# sourceMappingURL=formatter.js.map
|