bonescript-compiler 0.2.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/LICENSE +21 -0
- package/dist/algorithm_catalog.d.ts +32 -0
- package/dist/algorithm_catalog.js +323 -0
- package/dist/algorithm_catalog.js.map +1 -0
- package/dist/ast.d.ts +244 -0
- package/dist/ast.js +8 -0
- package/dist/ast.js.map +1 -0
- package/dist/cli.d.ts +4 -0
- package/dist/cli.js +605 -0
- package/dist/cli.js.map +1 -0
- package/dist/emit_batch.d.ts +7 -0
- package/dist/emit_batch.js +133 -0
- package/dist/emit_batch.js.map +1 -0
- package/dist/emit_capability.d.ts +7 -0
- package/dist/emit_capability.js +376 -0
- package/dist/emit_capability.js.map +1 -0
- package/dist/emit_composition.d.ts +22 -0
- package/dist/emit_composition.js +184 -0
- package/dist/emit_composition.js.map +1 -0
- package/dist/emit_deploy.d.ts +9 -0
- package/dist/emit_deploy.js +191 -0
- package/dist/emit_deploy.js.map +1 -0
- package/dist/emit_events.d.ts +14 -0
- package/dist/emit_events.js +305 -0
- package/dist/emit_events.js.map +1 -0
- package/dist/emit_extras.d.ts +12 -0
- package/dist/emit_extras.js +234 -0
- package/dist/emit_extras.js.map +1 -0
- package/dist/emit_full.d.ts +13 -0
- package/dist/emit_full.js +273 -0
- package/dist/emit_full.js.map +1 -0
- package/dist/emit_maintenance.d.ts +16 -0
- package/dist/emit_maintenance.js +442 -0
- package/dist/emit_maintenance.js.map +1 -0
- package/dist/emit_runtime.d.ts +13 -0
- package/dist/emit_runtime.js +691 -0
- package/dist/emit_runtime.js.map +1 -0
- package/dist/emit_sourcemap.d.ts +29 -0
- package/dist/emit_sourcemap.js +123 -0
- package/dist/emit_sourcemap.js.map +1 -0
- package/dist/emit_tests.d.ts +15 -0
- package/dist/emit_tests.js +185 -0
- package/dist/emit_tests.js.map +1 -0
- package/dist/emit_websocket.d.ts +6 -0
- package/dist/emit_websocket.js +223 -0
- package/dist/emit_websocket.js.map +1 -0
- package/dist/emitter.d.ts +25 -0
- package/dist/emitter.js +511 -0
- package/dist/emitter.js.map +1 -0
- package/dist/extension_manager.d.ts +38 -0
- package/dist/extension_manager.js +170 -0
- package/dist/extension_manager.js.map +1 -0
- package/dist/formatter.d.ts +34 -0
- package/dist/formatter.js +317 -0
- package/dist/formatter.js.map +1 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +113 -0
- package/dist/index.js.map +1 -0
- package/dist/ir.d.ts +168 -0
- package/dist/ir.js +10 -0
- package/dist/ir.js.map +1 -0
- package/dist/lexer.d.ts +195 -0
- package/dist/lexer.js +619 -0
- package/dist/lexer.js.map +1 -0
- package/dist/lowering.d.ts +25 -0
- package/dist/lowering.js +500 -0
- package/dist/lowering.js.map +1 -0
- package/dist/module_loader.d.ts +25 -0
- package/dist/module_loader.js +126 -0
- package/dist/module_loader.js.map +1 -0
- package/dist/optimizer.d.ts +26 -0
- package/dist/optimizer.js +158 -0
- package/dist/optimizer.js.map +1 -0
- package/dist/parse_decls.d.ts +13 -0
- package/dist/parse_decls.js +442 -0
- package/dist/parse_decls.js.map +1 -0
- package/dist/parse_decls2.d.ts +13 -0
- package/dist/parse_decls2.js +295 -0
- package/dist/parse_decls2.js.map +1 -0
- package/dist/parse_expr.d.ts +7 -0
- package/dist/parse_expr.js +197 -0
- package/dist/parse_expr.js.map +1 -0
- package/dist/parse_types.d.ts +6 -0
- package/dist/parse_types.js +51 -0
- package/dist/parse_types.js.map +1 -0
- package/dist/parser.d.ts +10 -0
- package/dist/parser.js +62 -0
- package/dist/parser.js.map +1 -0
- package/dist/parser_base.d.ts +19 -0
- package/dist/parser_base.js +50 -0
- package/dist/parser_base.js.map +1 -0
- package/dist/parser_recovery.d.ts +26 -0
- package/dist/parser_recovery.js +140 -0
- package/dist/parser_recovery.js.map +1 -0
- package/dist/scaffold.d.ts +13 -0
- package/dist/scaffold.js +376 -0
- package/dist/scaffold.js.map +1 -0
- package/dist/solver.d.ts +26 -0
- package/dist/solver.js +281 -0
- package/dist/solver.js.map +1 -0
- package/dist/typechecker.d.ts +52 -0
- package/dist/typechecker.js +534 -0
- package/dist/typechecker.js.map +1 -0
- package/dist/types.d.ts +38 -0
- package/dist/types.js +85 -0
- package/dist/types.js.map +1 -0
- package/dist/verifier.d.ts +46 -0
- package/dist/verifier.js +307 -0
- package/dist/verifier.js.map +1 -0
- package/package.json +52 -0
- package/src/algorithm_catalog.ts +345 -0
- package/src/ast.ts +334 -0
- package/src/cli.ts +624 -0
- package/src/emit_batch.ts +140 -0
- package/src/emit_capability.ts +436 -0
- package/src/emit_composition.ts +196 -0
- package/src/emit_deploy.ts +190 -0
- package/src/emit_events.ts +307 -0
- package/src/emit_extras.ts +240 -0
- package/src/emit_full.ts +309 -0
- package/src/emit_maintenance.ts +459 -0
- package/src/emit_runtime.ts +731 -0
- package/src/emit_sourcemap.ts +140 -0
- package/src/emit_tests.ts +205 -0
- package/src/emit_websocket.ts +229 -0
- package/src/emitter.ts +566 -0
- package/src/extension_manager.ts +187 -0
- package/src/formatter.ts +297 -0
- package/src/index.ts +88 -0
- package/src/ir.ts +215 -0
- package/src/lexer.ts +630 -0
- package/src/lowering.ts +556 -0
- package/src/module_loader.ts +114 -0
- package/src/optimizer.ts +196 -0
- package/src/parse_decls.ts +409 -0
- package/src/parse_decls2.ts +244 -0
- package/src/parse_expr.ts +197 -0
- package/src/parse_types.ts +54 -0
- package/src/parser.ts +64 -0
- package/src/parser_base.ts +57 -0
- package/src/parser_recovery.ts +153 -0
- package/src/scaffold.ts +375 -0
- package/src/solver.ts +330 -0
- package/src/typechecker.ts +591 -0
- package/src/types.ts +122 -0
- package/src/verifier.ts +348 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BoneScript Batch Executor Emitter
|
|
3
|
+
* Generates a batch processing module for sync: batch capabilities.
|
|
4
|
+
* Batch capabilities are queued and processed in configurable intervals.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as IR from "./ir";
|
|
8
|
+
|
|
9
|
+
export function emitBatchExecutor(system: IR.IRSystem): string {
|
|
10
|
+
// Collect all batch capabilities
|
|
11
|
+
const batchMethods: { modName: string; method: IR.IRMethod }[] = [];
|
|
12
|
+
for (const mod of system.modules) {
|
|
13
|
+
for (const iface of mod.interfaces) {
|
|
14
|
+
for (const method of iface.methods) {
|
|
15
|
+
if (method.sync === "batch") {
|
|
16
|
+
batchMethods.push({ modName: mod.name, method });
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (batchMethods.length === 0) return "";
|
|
23
|
+
|
|
24
|
+
const lines: string[] = [];
|
|
25
|
+
lines.push(`// Generated by BoneScript compiler. DO NOT EDIT.`);
|
|
26
|
+
lines.push(`// Batch executor for sync: batch capabilities.`);
|
|
27
|
+
lines.push(`// Set BATCH_INTERVAL_MS to control flush frequency (default: 5000ms).`);
|
|
28
|
+
lines.push(``);
|
|
29
|
+
lines.push(`import { query } from "./db";`);
|
|
30
|
+
lines.push(`import { logger } from "./logger";`);
|
|
31
|
+
lines.push(`import { counter, histogram } from "./metrics";`);
|
|
32
|
+
lines.push(``);
|
|
33
|
+
|
|
34
|
+
// Batch queue type
|
|
35
|
+
lines.push(`interface BatchItem {`);
|
|
36
|
+
lines.push(` capability: string;`);
|
|
37
|
+
lines.push(` payload: Record<string, unknown>;`);
|
|
38
|
+
lines.push(` enqueuedAt: Date;`);
|
|
39
|
+
lines.push(` resolve: (result: any) => void;`);
|
|
40
|
+
lines.push(` reject: (err: Error) => void;`);
|
|
41
|
+
lines.push(`}`);
|
|
42
|
+
lines.push(``);
|
|
43
|
+
|
|
44
|
+
// Per-capability queues
|
|
45
|
+
lines.push(`const queues: Map<string, BatchItem[]> = new Map([`);
|
|
46
|
+
for (const { modName, method } of batchMethods) {
|
|
47
|
+
lines.push(` ["${modName}.${method.name}", []],`);
|
|
48
|
+
}
|
|
49
|
+
lines.push(`]);`);
|
|
50
|
+
lines.push(``);
|
|
51
|
+
|
|
52
|
+
// Enqueue function
|
|
53
|
+
lines.push(`export function enqueueBatch(capability: string, payload: Record<string, unknown>): Promise<any> {`);
|
|
54
|
+
lines.push(` return new Promise((resolve, reject) => {`);
|
|
55
|
+
lines.push(` const queue = queues.get(capability);`);
|
|
56
|
+
lines.push(` if (!queue) {`);
|
|
57
|
+
lines.push(` reject(new Error(\`Unknown batch capability: \${capability}\`));`);
|
|
58
|
+
lines.push(` return;`);
|
|
59
|
+
lines.push(` }`);
|
|
60
|
+
lines.push(` queue.push({ capability, payload, enqueuedAt: new Date(), resolve, reject });`);
|
|
61
|
+
lines.push(` counter("batch.enqueued", { capability });`);
|
|
62
|
+
lines.push(` });`);
|
|
63
|
+
lines.push(`}`);
|
|
64
|
+
lines.push(``);
|
|
65
|
+
|
|
66
|
+
// Flush function — processes all queued items
|
|
67
|
+
lines.push(`async function flushBatch(capability: string, items: BatchItem[]): Promise<void> {`);
|
|
68
|
+
lines.push(` if (items.length === 0) return;`);
|
|
69
|
+
lines.push(` const start = Date.now();`);
|
|
70
|
+
lines.push(` logger.info("batch_flush_started", { event: capability, metadata: { count: items.length } });`);
|
|
71
|
+
lines.push(``);
|
|
72
|
+
lines.push(` // Process items in a single DB transaction`);
|
|
73
|
+
lines.push(` const { pool } = require("./db");`);
|
|
74
|
+
lines.push(` const client = await pool.connect();`);
|
|
75
|
+
lines.push(` try {`);
|
|
76
|
+
lines.push(` await client.query("BEGIN");`);
|
|
77
|
+
lines.push(` for (const item of items) {`);
|
|
78
|
+
lines.push(` try {`);
|
|
79
|
+
lines.push(` // Execute the batch item`);
|
|
80
|
+
lines.push(` // Each capability's batch handler is registered below`);
|
|
81
|
+
lines.push(` const handler = batchHandlers.get(capability);`);
|
|
82
|
+
lines.push(` if (handler) {`);
|
|
83
|
+
lines.push(` const result = await handler(item.payload, client);`);
|
|
84
|
+
lines.push(` item.resolve(result);`);
|
|
85
|
+
lines.push(` } else {`);
|
|
86
|
+
lines.push(` item.reject(new Error(\`No batch handler for \${capability}\`));`);
|
|
87
|
+
lines.push(` }`);
|
|
88
|
+
lines.push(` } catch (e: any) {`);
|
|
89
|
+
lines.push(` item.reject(e);`);
|
|
90
|
+
lines.push(` }`);
|
|
91
|
+
lines.push(` }`);
|
|
92
|
+
lines.push(` await client.query("COMMIT");`);
|
|
93
|
+
lines.push(` histogram("batch.duration_ms", Date.now() - start, { capability });`);
|
|
94
|
+
lines.push(` counter("batch.processed", { capability, count: String(items.length) });`);
|
|
95
|
+
lines.push(` logger.info("batch_flush_completed", { event: capability, metadata: { count: items.length, duration_ms: Date.now() - start } });`);
|
|
96
|
+
lines.push(` } catch (e: any) {`);
|
|
97
|
+
lines.push(` await client.query("ROLLBACK");`);
|
|
98
|
+
lines.push(` for (const item of items) item.reject(e);`);
|
|
99
|
+
lines.push(` logger.error("batch_flush_failed", { event: capability, metadata: { error: e.message } });`);
|
|
100
|
+
lines.push(` } finally {`);
|
|
101
|
+
lines.push(` client.release();`);
|
|
102
|
+
lines.push(` }`);
|
|
103
|
+
lines.push(`}`);
|
|
104
|
+
lines.push(``);
|
|
105
|
+
|
|
106
|
+
// Batch handler registry
|
|
107
|
+
lines.push(`type BatchHandler = (payload: Record<string, unknown>, client: any) => Promise<any>;`);
|
|
108
|
+
lines.push(`const batchHandlers: Map<string, BatchHandler> = new Map();`);
|
|
109
|
+
lines.push(``);
|
|
110
|
+
lines.push(`export function registerBatchHandler(capability: string, handler: BatchHandler): void {`);
|
|
111
|
+
lines.push(` batchHandlers.set(capability, handler);`);
|
|
112
|
+
lines.push(`}`);
|
|
113
|
+
lines.push(``);
|
|
114
|
+
|
|
115
|
+
// Batch worker — flushes all queues on interval
|
|
116
|
+
lines.push(`export function startBatchWorker(intervalMs: number = parseInt(process.env.BATCH_INTERVAL_MS || "5000")): NodeJS.Timeout {`);
|
|
117
|
+
lines.push(` logger.info("batch_worker_started", { event: "startup", metadata: { interval_ms: intervalMs } });`);
|
|
118
|
+
lines.push(` return setInterval(async () => {`);
|
|
119
|
+
lines.push(` for (const [capability, queue] of queues) {`);
|
|
120
|
+
lines.push(` if (queue.length === 0) continue;`);
|
|
121
|
+
lines.push(` const batch = queue.splice(0, queue.length); // drain queue atomically`);
|
|
122
|
+
lines.push(` await flushBatch(capability, batch).catch(e => {`);
|
|
123
|
+
lines.push(` logger.error("batch_worker_error", { event: capability, metadata: { error: e.message } });`);
|
|
124
|
+
lines.push(` });`);
|
|
125
|
+
lines.push(` }`);
|
|
126
|
+
lines.push(` }, intervalMs);`);
|
|
127
|
+
lines.push(`}`);
|
|
128
|
+
lines.push(``);
|
|
129
|
+
|
|
130
|
+
// Capability-specific stub handlers
|
|
131
|
+
for (const { modName, method } of batchMethods) {
|
|
132
|
+
const key = `${modName}.${method.name}`;
|
|
133
|
+
lines.push(`// Batch handler for ${key}`);
|
|
134
|
+
lines.push(`// Register your implementation:`);
|
|
135
|
+
lines.push(`// registerBatchHandler("${key}", async (payload, client) => { ... });`);
|
|
136
|
+
lines.push(``);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return lines.join("\n");
|
|
140
|
+
}
|
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BoneScript Capability Body Emitter
|
|
3
|
+
*
|
|
4
|
+
* Translates IR effects and preconditions into real TypeScript + SQL.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as IR from "./ir";
|
|
8
|
+
|
|
9
|
+
// ─── Expression Parser ────────────────────────────────────────────────────────
|
|
10
|
+
// Parses the serialized expression strings from the IR back into a structured form.
|
|
11
|
+
|
|
12
|
+
type ExprKind =
|
|
13
|
+
| { kind: "literal"; value: string; raw: string }
|
|
14
|
+
| { kind: "field"; path: string[] }
|
|
15
|
+
| { kind: "binop"; op: string; left: Expr; right: Expr }
|
|
16
|
+
| { kind: "call"; name: string; args: Expr[] };
|
|
17
|
+
|
|
18
|
+
type Expr = ExprKind;
|
|
19
|
+
|
|
20
|
+
function parseExprStr(s: string): Expr {
|
|
21
|
+
s = s.trim();
|
|
22
|
+
|
|
23
|
+
// Strip outer parens
|
|
24
|
+
if (s.startsWith("(") && s.endsWith(")")) {
|
|
25
|
+
return parseExprStr(s.slice(1, -1));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// String literal
|
|
29
|
+
if (s.startsWith('"') && s.endsWith('"')) {
|
|
30
|
+
return { kind: "literal", value: s.slice(1, -1), raw: s };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Number literal
|
|
34
|
+
if (/^-?\d+(\.\d+)?$/.test(s)) {
|
|
35
|
+
return { kind: "literal", value: s, raw: s };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Boolean
|
|
39
|
+
if (s === "true" || s === "false") {
|
|
40
|
+
return { kind: "literal", value: s, raw: s };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Binary operators (check in precedence order, right-to-left to handle left-assoc)
|
|
44
|
+
const binOps = [" or ", " and ", " == ", " != ", " >= ", " <= ", " > ", " < ", " in ", " contains ", " + ", " - ", " * ", " / "];
|
|
45
|
+
for (const op of binOps) {
|
|
46
|
+
const idx = findBinOp(s, op);
|
|
47
|
+
if (idx !== -1) {
|
|
48
|
+
const left = parseExprStr(s.slice(0, idx));
|
|
49
|
+
const right = parseExprStr(s.slice(idx + op.length));
|
|
50
|
+
return { kind: "binop", op: op.trim(), left, right };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Function call: name(args)
|
|
55
|
+
const callMatch = s.match(/^(\w+)\((.*)?\)$/);
|
|
56
|
+
if (callMatch) {
|
|
57
|
+
const args = callMatch[2] ? splitArgs(callMatch[2]).map(parseExprStr) : [];
|
|
58
|
+
return { kind: "call", name: callMatch[1], args };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Field reference: a.b.c
|
|
62
|
+
if (/^[\w.]+$/.test(s)) {
|
|
63
|
+
return { kind: "field", path: s.split(".") };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Fallback: treat as opaque literal
|
|
67
|
+
return { kind: "literal", value: s, raw: s };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function findBinOp(s: string, op: string): number {
|
|
71
|
+
let depth = 0;
|
|
72
|
+
for (let i = 0; i <= s.length - op.length; i++) {
|
|
73
|
+
const ch = s[i];
|
|
74
|
+
if (ch === "(" || ch === "[") depth++;
|
|
75
|
+
else if (ch === ")" || ch === "]") depth--;
|
|
76
|
+
else if (depth === 0 && s.slice(i, i + op.length) === op) {
|
|
77
|
+
return i;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return -1;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function splitArgs(s: string): string[] {
|
|
84
|
+
const args: string[] = [];
|
|
85
|
+
let depth = 0;
|
|
86
|
+
let current = "";
|
|
87
|
+
for (const ch of s) {
|
|
88
|
+
if (ch === "(" || ch === "[") depth++;
|
|
89
|
+
else if (ch === ")" || ch === "]") depth--;
|
|
90
|
+
else if (ch === "," && depth === 0) {
|
|
91
|
+
args.push(current.trim());
|
|
92
|
+
current = "";
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
current += ch;
|
|
96
|
+
}
|
|
97
|
+
if (current.trim()) args.push(current.trim());
|
|
98
|
+
return args;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ─── Entity Resolution ────────────────────────────────────────────────────────
|
|
102
|
+
// Determines which entities need to be fetched from the DB for a capability.
|
|
103
|
+
|
|
104
|
+
interface EntityFetch {
|
|
105
|
+
paramName: string; // capability parameter name (e.g., "item")
|
|
106
|
+
entityType: string; // entity type name (e.g., "Item")
|
|
107
|
+
tableName: string; // SQL table name (e.g., "items")
|
|
108
|
+
idField: string; // request body field for the ID (e.g., "item_id")
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function toSnakeCase(s: string): string {
|
|
112
|
+
return s.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function getEntityFetches(method: IR.IRMethod, mod: IR.IRModule, system: IR.IRSystem): EntityFetch[] {
|
|
116
|
+
const fetches: EntityFetch[] = [];
|
|
117
|
+
const seen = new Set<string>();
|
|
118
|
+
|
|
119
|
+
// Build a map of all entity names → table names across the whole system
|
|
120
|
+
const allModels = new Map<string, string>(); // entityName → tableName
|
|
121
|
+
for (const m of system.modules) {
|
|
122
|
+
for (const model of m.models) {
|
|
123
|
+
allModels.set(model.name, toSnakeCase(model.name) + "s");
|
|
124
|
+
allModels.set(model.name.toLowerCase(), toSnakeCase(model.name) + "s");
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
for (const param of method.input) {
|
|
129
|
+
const tableName = allModels.get(param.type) || allModels.get(param.type.toLowerCase());
|
|
130
|
+
if (tableName && !seen.has(param.name)) {
|
|
131
|
+
seen.add(param.name);
|
|
132
|
+
fetches.push({
|
|
133
|
+
paramName: param.name,
|
|
134
|
+
entityType: param.type,
|
|
135
|
+
tableName,
|
|
136
|
+
idField: param.name + "_id",
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return fetches;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ─── Precondition Compiler ────────────────────────────────────────────────────
|
|
145
|
+
|
|
146
|
+
interface CompiledPrecondition {
|
|
147
|
+
code: string; // TypeScript guard clause
|
|
148
|
+
description: string;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function compilePrecondition(expr: Expr, indent: string): string {
|
|
152
|
+
const condition = exprToTs(expr, true);
|
|
153
|
+
const description = exprToDescription(expr).replace(/"/g, '\\"');
|
|
154
|
+
return [
|
|
155
|
+
`${indent}if (${condition}) {`,
|
|
156
|
+
`${indent} return res.status(422).json({ error: { code: "PRECONDITION_FAILED", message: ${JSON.stringify(description)} } });`,
|
|
157
|
+
`${indent}}`,
|
|
158
|
+
].join("\n");
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function exprToTs(expr: Expr, negate: boolean = false): string {
|
|
162
|
+
const inner = exprToTsInner(expr);
|
|
163
|
+
return negate ? `!(${inner})` : inner;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function exprToTsInner(expr: Expr): string {
|
|
167
|
+
switch (expr.kind) {
|
|
168
|
+
case "literal":
|
|
169
|
+
if (expr.value === "true") return "true";
|
|
170
|
+
if (expr.value === "false") return "false";
|
|
171
|
+
if (/^"/.test(expr.raw)) return expr.raw;
|
|
172
|
+
return expr.value;
|
|
173
|
+
|
|
174
|
+
case "field":
|
|
175
|
+
// Convert field path to JS property access
|
|
176
|
+
return expr.path.join("?.");
|
|
177
|
+
|
|
178
|
+
case "binop": {
|
|
179
|
+
const l = exprToTsInner(expr.left);
|
|
180
|
+
const r = exprToTsInner(expr.right);
|
|
181
|
+
switch (expr.op) {
|
|
182
|
+
case "==": return `${l} === ${r}`;
|
|
183
|
+
case "!=": return `${l} !== ${r}`;
|
|
184
|
+
case "and": return `(${l} && ${r})`;
|
|
185
|
+
case "or": return `(${l} || ${r})`;
|
|
186
|
+
case "in": return `[${r}].flat().includes(${l})`;
|
|
187
|
+
case "contains": return `${l}?.includes(${r})`;
|
|
188
|
+
case ">": case "<": case ">=": case "<=":
|
|
189
|
+
case "+": case "-": case "*": case "/":
|
|
190
|
+
return `${l} ${expr.op} ${r}`;
|
|
191
|
+
default: return `${l} ${expr.op} ${r}`;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
case "call":
|
|
196
|
+
if (expr.name === "now") return "new Date()";
|
|
197
|
+
return `${expr.name}(${expr.args.map(exprToTsInner).join(", ")})`;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function exprToDescription(expr: Expr): string {
|
|
202
|
+
switch (expr.kind) {
|
|
203
|
+
case "literal": return expr.raw;
|
|
204
|
+
case "field": return expr.path.join(".");
|
|
205
|
+
case "binop": {
|
|
206
|
+
const l = exprToDescription(expr.left);
|
|
207
|
+
const r = exprToDescription(expr.right);
|
|
208
|
+
return `${l} ${expr.op} ${r}`;
|
|
209
|
+
}
|
|
210
|
+
case "call": return `${expr.name}(${expr.args.map(exprToDescription).join(", ")})`;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// ─── Effect Compiler ──────────────────────────────────────────────────────────
|
|
215
|
+
|
|
216
|
+
interface CompiledEffect {
|
|
217
|
+
sql: string;
|
|
218
|
+
params: string[];
|
|
219
|
+
description: string;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function compileEffect(effect: IR.IREffect, mod: IR.IRModule, system: IR.IRSystem, paramIdx: { n: number }): CompiledEffect | null {
|
|
223
|
+
const targetParts = effect.target.split(".");
|
|
224
|
+
if (targetParts.length < 2) return null;
|
|
225
|
+
|
|
226
|
+
const entityParam = targetParts[0]; // e.g., "item" or "trade"
|
|
227
|
+
const fieldName = targetParts[1]; // e.g., "quantity" or "offered_items"
|
|
228
|
+
const nestedPath = targetParts.slice(2); // e.g., ["owner_id"] for nested JSONB
|
|
229
|
+
|
|
230
|
+
// Find the model for this entity param — search across all modules
|
|
231
|
+
const model = (() => {
|
|
232
|
+
for (const m of system.modules) {
|
|
233
|
+
const found = m.models.find(mdl =>
|
|
234
|
+
toSnakeCase(mdl.name) === entityParam ||
|
|
235
|
+
mdl.name.toLowerCase() === entityParam.toLowerCase()
|
|
236
|
+
);
|
|
237
|
+
if (found) return found;
|
|
238
|
+
}
|
|
239
|
+
return mod.models.find(m =>
|
|
240
|
+
toSnakeCase(m.name) === entityParam ||
|
|
241
|
+
m.name.toLowerCase() === entityParam.toLowerCase()
|
|
242
|
+
);
|
|
243
|
+
})();
|
|
244
|
+
if (!model) return null;
|
|
245
|
+
|
|
246
|
+
const tableName = toSnakeCase(model.name) + "s";
|
|
247
|
+
const valueExpr = parseExprStr(effect.value);
|
|
248
|
+
const valueTs = exprToTsInner(valueExpr);
|
|
249
|
+
const idParam = `req.body.${entityParam}_id || req.params.id`;
|
|
250
|
+
|
|
251
|
+
// Detect if the param is a list type (bulk operation)
|
|
252
|
+
const isBulk = effect.target.includes("[]") ||
|
|
253
|
+
(entityParam.endsWith("s") && !model.name.toLowerCase().endsWith("s"));
|
|
254
|
+
const bulkIdParam = `req.body.${entityParam}_ids || req.body.${entityParam}?.map((x: any) => x.id)`;
|
|
255
|
+
const whereClause = isBulk
|
|
256
|
+
? `WHERE id = ANY($2::uuid[])`
|
|
257
|
+
: `WHERE id = ${`$${2}`}`;
|
|
258
|
+
|
|
259
|
+
// Handle nested JSONB path: trade.offered_items.owner_id
|
|
260
|
+
if (nestedPath.length > 0) {
|
|
261
|
+
const jsonbField = fieldName;
|
|
262
|
+
const jsonbPath = nestedPath.join(".");
|
|
263
|
+
const p1 = `$${paramIdx.n++}`;
|
|
264
|
+
const p2 = `$${paramIdx.n++}`;
|
|
265
|
+
// Use jsonb_set to update nested path
|
|
266
|
+
const jsonbPathLiteral = `'{${nestedPath.join(",")}}'`;
|
|
267
|
+
return {
|
|
268
|
+
sql: `UPDATE ${tableName} SET ${jsonbField} = jsonb_set(COALESCE(${jsonbField}, '{}'), ${jsonbPathLiteral}, to_jsonb(${p1}::text), true), updated_at = NOW() WHERE id = ${p2} RETURNING *`,
|
|
269
|
+
params: [valueTs, idParam],
|
|
270
|
+
description: `${effect.target} = ${effect.value}`,
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
switch (effect.op) {
|
|
275
|
+
case "assign": {
|
|
276
|
+
const p1 = `$${paramIdx.n++}`;
|
|
277
|
+
const p2 = `$${paramIdx.n++}`;
|
|
278
|
+
return {
|
|
279
|
+
sql: `UPDATE ${tableName} SET ${fieldName} = ${p1}, updated_at = NOW() WHERE id = ${p2} RETURNING *`,
|
|
280
|
+
params: [valueTs, idParam],
|
|
281
|
+
description: `${effect.target} = ${effect.value}`,
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
case "add": {
|
|
285
|
+
const p1 = `$${paramIdx.n++}`;
|
|
286
|
+
const p2 = `$${paramIdx.n++}`;
|
|
287
|
+
const fieldType = model.fields.find(f => f.name === fieldName)?.type || "";
|
|
288
|
+
const isNumeric = ["uint", "int", "float"].includes(fieldType);
|
|
289
|
+
if (isNumeric) {
|
|
290
|
+
return {
|
|
291
|
+
sql: `UPDATE ${tableName} SET ${fieldName} = ${fieldName} + ${p1}, updated_at = NOW() WHERE id = ${p2} RETURNING *`,
|
|
292
|
+
params: [valueTs, idParam],
|
|
293
|
+
description: `${effect.target} += ${effect.value}`,
|
|
294
|
+
};
|
|
295
|
+
} else {
|
|
296
|
+
return {
|
|
297
|
+
sql: `UPDATE ${tableName} SET ${fieldName} = ${fieldName} || jsonb_build_array(${p1}::jsonb), updated_at = NOW() WHERE id = ${p2} RETURNING *`,
|
|
298
|
+
params: [valueTs, idParam],
|
|
299
|
+
description: `${effect.target} += ${effect.value}`,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
case "remove": {
|
|
304
|
+
const p1 = `$${paramIdx.n++}`;
|
|
305
|
+
const p2 = `$${paramIdx.n++}`;
|
|
306
|
+
const fieldType = model.fields.find(f => f.name === fieldName)?.type || "";
|
|
307
|
+
const isNumeric = ["uint", "int", "float"].includes(fieldType);
|
|
308
|
+
if (isNumeric) {
|
|
309
|
+
return {
|
|
310
|
+
sql: `UPDATE ${tableName} SET ${fieldName} = ${fieldName} - ${p1}, updated_at = NOW() WHERE id = ${p2} RETURNING *`,
|
|
311
|
+
params: [valueTs, idParam],
|
|
312
|
+
description: `${effect.target} -= ${effect.value}`,
|
|
313
|
+
};
|
|
314
|
+
} else {
|
|
315
|
+
return {
|
|
316
|
+
sql: `UPDATE ${tableName} SET ${fieldName} = (SELECT jsonb_agg(elem) FROM jsonb_array_elements(${fieldName}) elem WHERE elem != ${p1}::jsonb), updated_at = NOW() WHERE id = ${p2} RETURNING *`,
|
|
317
|
+
params: [valueTs, idParam],
|
|
318
|
+
description: `${effect.target} -= ${effect.value}`,
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// ─── Main Capability Body Emitter ─────────────────────────────────────────────
|
|
326
|
+
|
|
327
|
+
export function emitCapabilityBody(
|
|
328
|
+
method: IR.IRMethod,
|
|
329
|
+
mod: IR.IRModule,
|
|
330
|
+
system: IR.IRSystem,
|
|
331
|
+
indent: string = " "
|
|
332
|
+
): string {
|
|
333
|
+
const lines: string[] = [];
|
|
334
|
+
const fetches = getEntityFetches(method, mod, system);
|
|
335
|
+
|
|
336
|
+
// 0. Destructure primitive params from req.body
|
|
337
|
+
const primitiveParams = method.input.filter(p => {
|
|
338
|
+
const isPrimitive = ["string", "uint", "int", "float", "bool", "timestamp", "uuid", "bytes", "json"].includes(p.type);
|
|
339
|
+
const isListOrSet = p.type.startsWith("list<") || p.type.startsWith("set<");
|
|
340
|
+
const isEntityFetch = fetches.some(f => f.paramName === p.name);
|
|
341
|
+
return (isPrimitive || isListOrSet) && !isEntityFetch;
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
if (primitiveParams.length > 0) {
|
|
345
|
+
const destructured = primitiveParams.map(p => p.name).join(", ");
|
|
346
|
+
lines.push(`${indent}const { ${destructured} } = req.body;`);
|
|
347
|
+
lines.push(``);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// 1. Fetch entities referenced in preconditions/effects
|
|
351
|
+
if (fetches.length > 0) {
|
|
352
|
+
lines.push(`${indent}// Fetch entities`);
|
|
353
|
+
for (const fetch of fetches) {
|
|
354
|
+
const idExpr = `req.body.${fetch.idField} || req.params.id`;
|
|
355
|
+
lines.push(`${indent}const ${fetch.paramName} = await queryOne(\`SELECT * FROM ${fetch.tableName} WHERE id = $1\`, [${idExpr}]);`);
|
|
356
|
+
lines.push(`${indent}if (!${fetch.paramName}) {`);
|
|
357
|
+
lines.push(`${indent} return res.status(404).json({ error: { code: "NOT_FOUND", message: "${fetch.paramName} not found" } });`);
|
|
358
|
+
lines.push(`${indent}}`);
|
|
359
|
+
}
|
|
360
|
+
lines.push(``);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// 2. Precondition checks
|
|
364
|
+
if (method.preconditions.length > 0) {
|
|
365
|
+
lines.push(`${indent}// Preconditions`);
|
|
366
|
+
for (const pre of method.preconditions) {
|
|
367
|
+
try {
|
|
368
|
+
const expr = parseExprStr(pre.expression);
|
|
369
|
+
lines.push(compilePrecondition(expr, indent));
|
|
370
|
+
} catch {
|
|
371
|
+
// Fallback: emit as comment if parsing fails
|
|
372
|
+
lines.push(`${indent}// CHECK: ${pre.description}`);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
lines.push(``);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// 3. Effects (applied in declaration order, each in its own query)
|
|
379
|
+
if (method.effects.length > 0) {
|
|
380
|
+
lines.push(`${indent}// Effects (applied in declaration order)`);
|
|
381
|
+
const effectResults: string[] = [];
|
|
382
|
+
|
|
383
|
+
for (const effect of method.effects) {
|
|
384
|
+
// Each effect gets its own parameter numbering starting at 1
|
|
385
|
+
const paramIdx = { n: 1 };
|
|
386
|
+
const compiled = compileEffect(effect, mod, system, paramIdx);
|
|
387
|
+
if (compiled) {
|
|
388
|
+
const resultVar = `__effect_${effectResults.length}`;
|
|
389
|
+
effectResults.push(resultVar);
|
|
390
|
+
lines.push(`${indent}const ${resultVar} = await query(\`${compiled.sql}\`, [${compiled.params.join(", ")}]);`);
|
|
391
|
+
lines.push(`${indent}if (!${resultVar} || ${resultVar}.length === 0) {`);
|
|
392
|
+
lines.push(`${indent} throw new Error("Effect failed: ${compiled.description.replace(/"/g, '\\"')}");`);
|
|
393
|
+
lines.push(`${indent}}`);
|
|
394
|
+
} else {
|
|
395
|
+
// Fallback for complex effects we can't compile
|
|
396
|
+
lines.push(`${indent}// EFFECT: ${effect.target} ${effect.op === "assign" ? "=" : effect.op === "add" ? "+=" : "-="} ${effect.value}`);
|
|
397
|
+
lines.push(`${indent}// TODO: Implement this effect manually`);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
lines.push(``);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// 4. Event emissions
|
|
404
|
+
if (method.emissions.length > 0) {
|
|
405
|
+
lines.push(`${indent}// Emit events`);
|
|
406
|
+
for (const ev of method.emissions) {
|
|
407
|
+
const payload = buildEventPayload(method, fetches);
|
|
408
|
+
if (method.sync === "transactional") {
|
|
409
|
+
lines.push(`${indent}await eventBus.publish("${ev}", ${payload}, "${mod.name}", auth.trace_id, __client);`);
|
|
410
|
+
} else {
|
|
411
|
+
lines.push(`${indent}await eventBus.publish("${ev}", ${payload}, "${mod.name}", auth.trace_id);`);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
lines.push(``);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// 5. Return result
|
|
418
|
+
const resultEntity = fetches[0];
|
|
419
|
+
if (resultEntity) {
|
|
420
|
+
lines.push(`${indent}res.json({ ok: true, action: "${method.name}", entity: ${resultEntity.paramName} });`);
|
|
421
|
+
} else {
|
|
422
|
+
lines.push(`${indent}res.json({ ok: true, action: "${method.name}" });`);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return lines.join("\n");
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function buildEventPayload(method: IR.IRMethod, fetches: EntityFetch[]): string {
|
|
429
|
+
const fields: string[] = [];
|
|
430
|
+
for (const fetch of fetches) {
|
|
431
|
+
fields.push(`${fetch.paramName}_id: ${fetch.paramName}?.id`);
|
|
432
|
+
}
|
|
433
|
+
fields.push(`timestamp: new Date().toISOString()`);
|
|
434
|
+
fields.push(`actor_id: auth.actor_id`);
|
|
435
|
+
return `{ ${fields.join(", ")} }`;
|
|
436
|
+
}
|