@telorun/templating 0.20.1 → 0.21.1

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.
Files changed (40) hide show
  1. package/dist/cel/analyze.d.ts +26 -0
  2. package/dist/cel/analyze.d.ts.map +1 -1
  3. package/dist/cel/analyze.js +114 -8
  4. package/dist/cel/catalog.d.ts.map +1 -1
  5. package/dist/cel/catalog.js +10 -0
  6. package/dist/cel/compile.d.ts +9 -3
  7. package/dist/cel/compile.d.ts.map +1 -1
  8. package/dist/cel/compile.js +52 -11
  9. package/dist/cel/diagnose.d.ts +10 -1
  10. package/dist/cel/diagnose.d.ts.map +1 -1
  11. package/dist/cel/diagnose.js +42 -6
  12. package/dist/cel/environment.d.ts.map +1 -1
  13. package/dist/cel/environment.js +2 -0
  14. package/dist/cel/module-call.d.ts +108 -0
  15. package/dist/cel/module-call.d.ts.map +1 -0
  16. package/dist/cel/module-call.js +245 -0
  17. package/dist/cel/value-identity.d.ts +18 -0
  18. package/dist/cel/value-identity.d.ts.map +1 -0
  19. package/dist/cel/value-identity.js +25 -0
  20. package/dist/engine.d.ts +66 -0
  21. package/dist/engine.d.ts.map +1 -1
  22. package/dist/engines/cel.d.ts.map +1 -1
  23. package/dist/engines/cel.js +87 -6
  24. package/dist/engines/sql.d.ts.map +1 -1
  25. package/dist/engines/sql.js +5 -1
  26. package/dist/index.d.ts +3 -2
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +7 -1
  29. package/package.json +3 -3
  30. package/src/cel/analyze.ts +131 -9
  31. package/src/cel/catalog.ts +10 -0
  32. package/src/cel/compile.ts +59 -12
  33. package/src/cel/diagnose.ts +56 -6
  34. package/src/cel/environment.ts +3 -0
  35. package/src/cel/module-call.ts +369 -0
  36. package/src/cel/value-identity.ts +34 -0
  37. package/src/engine.ts +72 -0
  38. package/src/engines/cel.ts +104 -6
  39. package/src/engines/sql.ts +5 -1
  40. package/src/index.ts +20 -1
@@ -0,0 +1,108 @@
1
+ /**
2
+ * **A call whose receiver is one of the declaring module's names.**
3
+ *
4
+ * `Billing.format(x)`, `Self.y(1)`, `<ModuleName>.y(1)` — CEL's own
5
+ * qualified-function rule, the one `cel.bind` and cel-go's `math.greatest`
6
+ * already follow. Resolution happens on the PARSED TREE, right after
7
+ * `env.parse` and before any check: the author's text, spans and the expression
8
+ * a trace shows stay exactly as written, which rewriting the source would move.
9
+ *
10
+ * A module's names are known from its own file before any import resolves — its
11
+ * `imports:` keys, `Self`, its `metadata.name` and `Telo` — so the set is an
12
+ * INPUT here rather than something this module derives. A call on a bare
13
+ * identifier outside the set stays an ordinary method call, which is what keeps
14
+ * the catalog's `format` and `Billing.format` from ever colliding, with one
15
+ * catalog environment compiling both.
16
+ *
17
+ * Two engine internals carry it, neither exposed by cel-js's typings — hence
18
+ * the exact version pin and `tests/module-call.test.ts`, which fails the moment
19
+ * an upgrade moves either:
20
+ *
21
+ * - `ASTNode.setMeta('macro', …)` redirects a node's check and evaluation. A
22
+ * comprehension macro (`map`, `filter`, …) sets `alternate` instead, and
23
+ * `alternate` WINS over `macro`, so a call the parser expanded as a macro
24
+ * needs it cleared or the rewrite is silently ignored. The parser expands
25
+ * those names BEFORE this rewrite and refuses any call whose arguments do not
26
+ * fit the macro, so no function may take a macro's name
27
+ * (`FUNCTION_NAME_RESERVED`); clearing keeps a call that did parse reported
28
+ * as the module call it names.
29
+ * - `async` must be set explicitly: cel-js derives it for an `rcall` from
30
+ * `receiverWithArgs`, which only the rcall's own checker fills in — and ours
31
+ * replaces it.
32
+ *
33
+ * The dispatch table is read from the activation under a key no CEL source can
34
+ * spell, so nothing an author can write reaches the mechanism past the export
35
+ * gate or the dependency edge. Binding the table is the kernel's, per scope;
36
+ * until something binds one, evaluating a module call throws naming the
37
+ * function.
38
+ */
39
+ import type { ASTNode } from "@marcbachmann/cel-js";
40
+ /**
41
+ * Activation key the per-scope dispatch table is read from.
42
+ *
43
+ * `@` and `:` are outside CEL's identifier grammar and there is no other way to
44
+ * reach the activation, so no expression can name this — a spellable dispatcher
45
+ * would let an author call past the export gate and past the dependency edge
46
+ * that orders the callee before its caller.
47
+ */
48
+ export declare const MODULE_CALL_DISPATCH_KEY = "@telo:module-functions";
49
+ /** Per-scope binding table: qualified name → the callable's `call`. */
50
+ export type ModuleCallDispatch = ReadonlyMap<string, (args: readonly unknown[]) => unknown>;
51
+ /**
52
+ * The CEL type a qualified call yields, as a type name (`"string"`,
53
+ * `"list<int>"`), or undefined for "not known here".
54
+ *
55
+ * A hook rather than a table because the answer belongs to the analysis, not to
56
+ * the expression: a signature is declared on a resource in the module the call
57
+ * resolves through. With no resolver — every compile, and every analysis until
58
+ * signatures are typed — a module call is `dyn`, which types its arguments and
59
+ * claims nothing about its result.
60
+ */
61
+ export type ModuleCallTypeResolver = (qualified: string) => string | undefined;
62
+ /** What a resolved module call says about itself: the qualified name as
63
+ * written, the argument nodes, and — once the tree has been type-checked — the
64
+ * type the checker gave each argument. The single reader — no other surface
65
+ * pattern-matches the macro's shape. */
66
+ export interface ModuleCall {
67
+ readonly qualified: string;
68
+ readonly args: readonly ASTNode[];
69
+ readonly argumentTypes?: readonly string[];
70
+ }
71
+ export declare function moduleCallOf(node: ASTNode): ModuleCall | undefined;
72
+ /** Message for a call nothing has bound. Not a fallback value: a call that
73
+ * cannot reach its function has no result, and inventing one would make a
74
+ * missing export read as a null. */
75
+ export declare function unboundModuleCallMessage(qualified: string): string;
76
+ /**
77
+ * Rewrite every module call in `ast` in place and report them in source order.
78
+ *
79
+ * Returns the qualified names as written (`["Billing.format", "Self.y"]`), which
80
+ * is what a compiled value carries so no consumer has to re-parse to learn what
81
+ * an expression calls. Idempotent: a node already resolved is rewritten once and
82
+ * reported again, so two passes over one tree agree about what it calls.
83
+ */
84
+ export declare function resolveModuleCalls(ast: ASTNode | undefined, moduleNames?: ReadonlySet<string>, typeOf?: ModuleCallTypeResolver): string[];
85
+ /** Every resolved module call in `ast` with its node, outermost first. */
86
+ export declare function moduleCallNodes(ast: ASTNode | undefined): Array<{
87
+ node: ASTNode;
88
+ call: ModuleCall;
89
+ }>;
90
+ /**
91
+ * Bare identifiers used as a call receiver that resolved to NO module name.
92
+ *
93
+ * `Foo.bar()` with no `Foo` import is an unknown identifier like any other, but
94
+ * the repair is not the usual one — it needs an `imports:` alias, not a
95
+ * different spelling — so the caller that reports the root says so.
96
+ */
97
+ export declare function unresolvedCallReceivers(ast: ASTNode | undefined, moduleNames?: ReadonlySet<string>): Set<string>;
98
+ /**
99
+ * Names bound INSIDE the expression — a comprehension variable, a `cel.bind`
100
+ * name — that collide with one of the module's names.
101
+ *
102
+ * Such a binding is unreachable through a call: `Billing.f(x)` inside it
103
+ * resolves to the module, never to the bound value. Reported as a reserved name
104
+ * rather than silently shadowed, which is the rule every other name in CEL
105
+ * scope already follows.
106
+ */
107
+ export declare function moduleNameBindings(ast: ASTNode | undefined, moduleNames?: ReadonlySet<string>): string[];
108
+ //# sourceMappingURL=module-call.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module-call.d.ts","sourceRoot":"","sources":["../../src/cel/module-call.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAEpD;;;;;;;GAOG;AACH,eAAO,MAAM,wBAAwB,2BAA2B,CAAC;AAEjE,uEAAuE;AACvE,MAAM,MAAM,kBAAkB,GAAG,WAAW,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,SAAS,OAAO,EAAE,KAAK,OAAO,CAAC,CAAC;AAE5F;;;;;;;;;GASG;AACH,MAAM,MAAM,sBAAsB,GAAG,CAAC,SAAS,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC;AAwC/E;;;yCAGyC;AACzC,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,IAAI,EAAE,SAAS,OAAO,EAAE,CAAC;IAClC,QAAQ,CAAC,aAAa,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CAC5C;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,OAAO,GAAG,UAAU,GAAG,SAAS,CASlE;AAED;;qCAEqC;AACrC,wBAAgB,wBAAwB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CASlE;AAID;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,GAAG,EAAE,OAAO,GAAG,SAAS,EACxB,WAAW,GAAE,WAAW,CAAC,MAAM,CAAY,EAC3C,MAAM,CAAC,EAAE,sBAAsB,GAC9B,MAAM,EAAE,CAIV;AAiGD,0EAA0E;AAC1E,wBAAgB,eAAe,CAAC,GAAG,EAAE,OAAO,GAAG,SAAS,GAAG,KAAK,CAAC;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,UAAU,CAAA;CAAE,CAAC,CAcpG;AASD;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CACrC,GAAG,EAAE,OAAO,GAAG,SAAS,EACxB,WAAW,GAAE,WAAW,CAAC,MAAM,CAAY,GAC1C,GAAG,CAAC,MAAM,CAAC,CAkBb;AAED;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAChC,GAAG,EAAE,OAAO,GAAG,SAAS,EACxB,WAAW,GAAE,WAAW,CAAC,MAAM,CAAY,GAC1C,MAAM,EAAE,CAUV"}
@@ -0,0 +1,245 @@
1
+ /**
2
+ * Activation key the per-scope dispatch table is read from.
3
+ *
4
+ * `@` and `:` are outside CEL's identifier grammar and there is no other way to
5
+ * reach the activation, so no expression can name this — a spellable dispatcher
6
+ * would let an author call past the export gate and past the dependency edge
7
+ * that orders the callee before its caller.
8
+ */
9
+ export const MODULE_CALL_DISPATCH_KEY = "@telo:module-functions";
10
+ /** cel-js's `ASTNode` is a union over its operators, so it has no statically
11
+ * known members to extend — the internals are read through this cast, in one
12
+ * place. */
13
+ const metaOf = (node) => node;
14
+ export function moduleCallOf(node) {
15
+ const macro = metaOf(node).meta?.macro;
16
+ return macro?.teloModuleCall === true
17
+ ? {
18
+ qualified: macro.qualified,
19
+ args: macro.args,
20
+ ...(macro.argumentTypes ? { argumentTypes: macro.argumentTypes } : {}),
21
+ }
22
+ : undefined;
23
+ }
24
+ /** Message for a call nothing has bound. Not a fallback value: a call that
25
+ * cannot reach its function has no result, and inventing one would make a
26
+ * missing export read as a null. */
27
+ export function unboundModuleCallMessage(qualified) {
28
+ const dot = qualified.indexOf(".");
29
+ const receiver = qualified.slice(0, dot);
30
+ const fn = qualified.slice(dot + 1);
31
+ return (`unbound function '${qualified}' — nothing is bound under that name in this scope. ` +
32
+ `'${receiver}' must be an imports: alias (or Self, or this module's own name) whose ` +
33
+ `module declares a callable named '${fn}' and exports it.`);
34
+ }
35
+ const NO_NAMES = new Set();
36
+ /**
37
+ * Rewrite every module call in `ast` in place and report them in source order.
38
+ *
39
+ * Returns the qualified names as written (`["Billing.format", "Self.y"]`), which
40
+ * is what a compiled value carries so no consumer has to re-parse to learn what
41
+ * an expression calls. Idempotent: a node already resolved is rewritten once and
42
+ * reported again, so two passes over one tree agree about what it calls.
43
+ */
44
+ export function resolveModuleCalls(ast, moduleNames = NO_NAMES, typeOf) {
45
+ const calls = [];
46
+ if (ast && moduleNames.size > 0)
47
+ visit(ast, moduleNames, typeOf, calls);
48
+ return calls;
49
+ }
50
+ function visit(node, moduleNames, typeOf, calls) {
51
+ // Already resolved: rewritten once, reported every time, so the return value
52
+ // is what the TREE calls rather than what this pass happened to change.
53
+ const resolved = moduleCallOf(node);
54
+ if (resolved) {
55
+ calls.push(resolved.qualified);
56
+ for (const arg of resolved.args)
57
+ visit(arg, moduleNames, typeOf, calls);
58
+ return;
59
+ }
60
+ const receiver = moduleCallReceiver(node, moduleNames);
61
+ if (receiver) {
62
+ const [method, , args] = node.args;
63
+ const qualified = `${receiver}.${method}`;
64
+ calls.push(qualified);
65
+ rewrite(node, qualified, args, typeOf);
66
+ // The receiver names a MODULE, not a value, so it is not descended into:
67
+ // that is what keeps it out of every access chain, out of `refs`, and out
68
+ // of the undeclared-root check.
69
+ for (const arg of args)
70
+ visit(arg, moduleNames, typeOf, calls);
71
+ return;
72
+ }
73
+ descend(node, (child) => visit(child, moduleNames, typeOf, calls));
74
+ }
75
+ /** The module name a node's receiver is, or undefined when the node is not a
76
+ * module call. `a.b.c(x)` is not one — its receiver is a member access, not a
77
+ * bare identifier. */
78
+ function moduleCallReceiver(node, moduleNames) {
79
+ if (node.op !== "rcall" || !Array.isArray(node.args))
80
+ return undefined;
81
+ const [method, receiver, args] = node.args;
82
+ if (typeof method !== "string" || !Array.isArray(args))
83
+ return undefined;
84
+ if (!isNode(receiver) || receiver.op !== "id")
85
+ return undefined;
86
+ const name = receiver.args;
87
+ return moduleNames.has(name) ? name : undefined;
88
+ }
89
+ function rewrite(node, qualified, args, typeOf) {
90
+ const macro = {
91
+ teloModuleCall: true,
92
+ qualified,
93
+ args,
94
+ typeCheck(chk, m, ctx) {
95
+ // The arguments are checked in the CURRENT context, so a mistake inside
96
+ // one is reported where it is written — and each argument's type is kept,
97
+ // so whoever knows the callee's signature can compare against it.
98
+ m.argumentTypes = m.args.map((arg) => typeNameOf(chk.check(arg, ctx)));
99
+ // cel-js derives an rcall's asyncness from `receiverWithArgs`, which only
100
+ // the rcall checker this macro replaces fills in — so it is stated: the
101
+ // dispatch is synchronous, so the node is async exactly when one of its
102
+ // arguments is. Asked only now, once each argument is checked: an
103
+ // argument's answer is cached on first reading, and read before its own
104
+ // check it is the conservative `true` for every operator and call.
105
+ metaOf(node).setMeta("async", m.args.some((arg) => arg.maybeAsync === true));
106
+ const declared = typeOf?.(m.qualified);
107
+ return declared === undefined ? chk.dynType : chk.getType(declared);
108
+ },
109
+ evaluate(ev, m, ctx) {
110
+ const table = ctx.getValue(MODULE_CALL_DISPATCH_KEY);
111
+ const fn = table?.get(m.qualified);
112
+ if (!fn)
113
+ throw new Error(unboundModuleCallMessage(m.qualified));
114
+ const values = m.args.map((arg) => ev.eval(arg, ctx));
115
+ // A callable's `call` is synchronous; an ARGUMENT need not be, and every
116
+ // other dispatch in cel-js settles its operands first. Handing a pending
117
+ // Promise to the function instead would pass it a value of the wrong type
118
+ // with nothing reported.
119
+ return values.some((value) => value instanceof Promise)
120
+ ? Promise.all(values).then((settled) => fn(settled))
121
+ : fn(values);
122
+ },
123
+ };
124
+ metaOf(node)
125
+ // A comprehension macro expands into an `alternate`, which cel-js consults
126
+ // BEFORE `macro`. Left in place, `Billing.map(i, i)` would evaluate as a
127
+ // comprehension over an identifier rather than as the module call it names.
128
+ .setMeta("alternate", undefined)
129
+ .setMeta("macro", macro);
130
+ }
131
+ /** Every resolved module call in `ast` with its node, outermost first. */
132
+ export function moduleCallNodes(ast) {
133
+ const out = [];
134
+ if (ast)
135
+ walk(ast);
136
+ return out;
137
+ function walk(node) {
138
+ const call = moduleCallOf(node);
139
+ if (call) {
140
+ out.push({ node, call });
141
+ for (const arg of call.args)
142
+ walk(arg);
143
+ return;
144
+ }
145
+ descend(node, walk);
146
+ }
147
+ }
148
+ /** The name of a checker type object (`int`, `list<string>`), or `dyn` for one
149
+ * that carries none. */
150
+ function typeNameOf(type) {
151
+ const name = type?.name;
152
+ return typeof name === "string" ? name : "dyn";
153
+ }
154
+ /**
155
+ * Bare identifiers used as a call receiver that resolved to NO module name.
156
+ *
157
+ * `Foo.bar()` with no `Foo` import is an unknown identifier like any other, but
158
+ * the repair is not the usual one — it needs an `imports:` alias, not a
159
+ * different spelling — so the caller that reports the root says so.
160
+ */
161
+ export function unresolvedCallReceivers(ast, moduleNames = NO_NAMES) {
162
+ const out = new Set();
163
+ if (ast)
164
+ walk(ast);
165
+ return out;
166
+ function walk(node) {
167
+ if (moduleCallOf(node)) {
168
+ for (const arg of node.args[2])
169
+ walk(arg);
170
+ return;
171
+ }
172
+ if (node.op === "rcall" && Array.isArray(node.args)) {
173
+ const receiver = node.args[1];
174
+ if (isNode(receiver) && receiver.op === "id" && !moduleNames.has(receiver.args)) {
175
+ out.add(receiver.args);
176
+ }
177
+ }
178
+ descend(node, walk);
179
+ }
180
+ }
181
+ /**
182
+ * Names bound INSIDE the expression — a comprehension variable, a `cel.bind`
183
+ * name — that collide with one of the module's names.
184
+ *
185
+ * Such a binding is unreachable through a call: `Billing.f(x)` inside it
186
+ * resolves to the module, never to the bound value. Reported as a reserved name
187
+ * rather than silently shadowed, which is the rule every other name in CEL
188
+ * scope already follows.
189
+ */
190
+ export function moduleNameBindings(ast, moduleNames = NO_NAMES) {
191
+ const out = [];
192
+ if (ast && moduleNames.size > 0)
193
+ walk(ast);
194
+ return [...new Set(out)];
195
+ function walk(node) {
196
+ const bound = boundNameOf(node);
197
+ if (bound !== undefined && moduleNames.has(bound))
198
+ out.push(bound);
199
+ descend(node, walk);
200
+ }
201
+ }
202
+ /** The name a binding form introduces: `cel.bind(<name>, …)`'s first argument,
203
+ * or a comprehension macro's iteration variable. */
204
+ function boundNameOf(node) {
205
+ if (node.op !== "rcall" || !Array.isArray(node.args))
206
+ return undefined;
207
+ if (moduleCallOf(node))
208
+ return undefined;
209
+ const [method, receiver, args] = node.args;
210
+ if (!Array.isArray(args) || args.length === 0)
211
+ return undefined;
212
+ const first = args[0];
213
+ if (!isNode(first) || first.op !== "id")
214
+ return undefined;
215
+ if (method === "bind") {
216
+ return isNode(receiver) && receiver.op === "id" && receiver.args === "cel"
217
+ ? first.args
218
+ : undefined;
219
+ }
220
+ return COMPREHENSION_METHODS.has(method) ? first.args : undefined;
221
+ }
222
+ /** cel-js's comprehension macros, which bind their first argument as the
223
+ * iteration variable. Mirrors the set `analyze.ts` walks with. */
224
+ const COMPREHENSION_METHODS = new Set(["filter", "map", "exists", "all", "exists_one"]);
225
+ /** Generic descent over a node's operands — an array, a single node, or an
226
+ * array of arrays (a map literal's entries). */
227
+ function descend(node, fn) {
228
+ const args = node.args;
229
+ if (Array.isArray(args)) {
230
+ for (const arg of args) {
231
+ if (isNode(arg))
232
+ fn(arg);
233
+ else if (Array.isArray(arg))
234
+ for (const item of arg)
235
+ if (isNode(item))
236
+ fn(item);
237
+ }
238
+ return;
239
+ }
240
+ if (isNode(args))
241
+ fn(args);
242
+ }
243
+ function isNode(v) {
244
+ return v !== null && typeof v === "object" && "op" in v;
245
+ }
@@ -0,0 +1,18 @@
1
+ /** The classes the CEL value domain is typed by. */
2
+ export interface CelValueClasses {
3
+ readonly Duration: abstract new (...args: never[]) => unknown;
4
+ readonly UnsignedInt: abstract new (...args: never[]) => unknown;
5
+ }
6
+ /**
7
+ * Refuse to run with a CEL engine whose values are not the SDK's.
8
+ *
9
+ * cel-js dispatches by constructor, so a second installed copy of it gives the
10
+ * engine a `Duration` / `UnsignedInt` no controller can construct: every value a
11
+ * controller builds from `@telorun/sdk` would fail `+`, `type()` and every
12
+ * overload, at the first expression that meets one. Checked once, where the
13
+ * engine is loaded, so the cause is named before anything evaluates.
14
+ */
15
+ export declare function assertCelValueIdentity(classes: CelValueClasses): void;
16
+ /** The SDK's classes — the identity every Telo package and controller shares. */
17
+ export declare const SDK_CEL_VALUE_CLASSES: CelValueClasses;
18
+ //# sourceMappingURL=value-identity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"value-identity.d.ts","sourceRoot":"","sources":["../../src/cel/value-identity.ts"],"names":[],"mappings":"AAGA,oDAAoD;AACpD,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,QAAQ,EAAE,QAAQ,MAAM,GAAG,IAAI,EAAE,KAAK,EAAE,KAAK,OAAO,CAAC;IAC9D,QAAQ,CAAC,WAAW,EAAE,QAAQ,MAAM,GAAG,IAAI,EAAE,KAAK,EAAE,KAAK,OAAO,CAAC;CAClE;AAED;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,eAAe,GAAG,IAAI,CAYrE;AAED,iFAAiF;AACjF,eAAO,MAAM,qBAAqB,EAAE,eAA2C,CAAC"}
@@ -0,0 +1,25 @@
1
+ import { Environment } from "@marcbachmann/cel-js";
2
+ import { Duration, UnsignedInt } from "@telorun/sdk";
3
+ /**
4
+ * Refuse to run with a CEL engine whose values are not the SDK's.
5
+ *
6
+ * cel-js dispatches by constructor, so a second installed copy of it gives the
7
+ * engine a `Duration` / `UnsignedInt` no controller can construct: every value a
8
+ * controller builds from `@telorun/sdk` would fail `+`, `type()` and every
9
+ * overload, at the first expression that meets one. Checked once, where the
10
+ * engine is loaded, so the cause is named before anything evaluates.
11
+ */
12
+ export function assertCelValueIdentity(classes) {
13
+ const probe = new Environment();
14
+ const duration = probe.evaluate("duration('1s')");
15
+ const uint = probe.evaluate("1u");
16
+ if (duration instanceof classes.Duration && uint instanceof classes.UnsignedInt)
17
+ return;
18
+ throw new Error("@telorun/templating's CEL engine is a different copy of @marcbachmann/cel-js than the one " +
19
+ "@telorun/sdk exports Duration and UnsignedInt from, so a duration or uint a controller " +
20
+ "builds would be rejected by every CEL expression. Two copies of @marcbachmann/cel-js are " +
21
+ "installed: every Telo package pins the same exact version, so deduplicate the install " +
22
+ "(`pnpm dedupe` / `npm dedupe`) until one remains.");
23
+ }
24
+ /** The SDK's classes — the identity every Telo package and controller shares. */
25
+ export const SDK_CEL_VALUE_CLASSES = { Duration, UnsignedInt };
package/dist/engine.d.ts CHANGED
@@ -1,10 +1,24 @@
1
1
  import type { Environment } from "@marcbachmann/cel-js";
2
2
  import type { CompiledValue } from "@telorun/sdk";
3
+ import type { ModuleCallFlags } from "./cel/diagnose.js";
4
+ import type { ModuleCallTypeResolver } from "./cel/module-call.js";
3
5
  /** Compile-time environment passed to `engine.compile`. Engines that need to
4
6
  * parse against a CEL environment (the `cel` engine) read it from `celEnv`;
5
7
  * engines that resolve fully at compile time (`literal`) ignore it. */
6
8
  export interface CompileEnv {
7
9
  readonly celEnv: Environment;
10
+ /**
11
+ * The names a call's receiver may be for the call to resolve as a MODULE
12
+ * call — the declaring module's `imports:` keys, `Self`, its `metadata.name`
13
+ * and `Telo`.
14
+ *
15
+ * An INPUT, not something an engine derives: they are known from the module's
16
+ * own file before any import resolves, and a partial compiles with the names
17
+ * of the module that includes it. Absent means "no module names here", which
18
+ * leaves every call an ordinary catalog call — the reading every caller had
19
+ * before module functions existed.
20
+ */
21
+ readonly moduleNames?: ReadonlySet<string>;
8
22
  }
9
23
  /** Analyze-time environment passed to `engine.analyze`. The walker resolves
10
24
  * the path-specific effective context (kernel globals merged in, x-telo-context
@@ -31,6 +45,33 @@ export interface AnalyzeEnv {
31
45
  * did before it existed.
32
46
  */
33
47
  readonly rootsDeclared?: boolean;
48
+ /** The declaring module's names — see {@link CompileEnv.moduleNames}. An
49
+ * analysis that does not supply them reads a module call as an ordinary
50
+ * method call, so the compile and analyze halves must be given the same set
51
+ * or they disagree about what the expression says. */
52
+ readonly moduleNames?: ReadonlySet<string>;
53
+ /** The type a module call yields, when the caller can resolve one. Absent
54
+ * leaves every module call `dyn` — its arguments are still checked where
55
+ * they are written, and nothing is claimed about its result. */
56
+ readonly moduleCallType?: ModuleCallTypeResolver;
57
+ /** The JSON Schema a module call's RESULT carries, when the caller can resolve
58
+ * one — what member access on the result (`Billing.total(xs).amont`) is
59
+ * checked against. Absent leaves such access unchecked. */
60
+ readonly moduleCallResult?: (qualified: string) => Record<string, unknown> | undefined;
61
+ /** The derived flags of the module function a qualified call reaches, carried
62
+ * onto its {@link CallSite} — boolean wherever the call resolves, so a
63
+ * consumer reading `deterministic === false` sees every impure one. */
64
+ readonly moduleCallFlags?: ModuleCallFlags;
65
+ /**
66
+ * Whether a bare identifier could denote a MODULE at all.
67
+ *
68
+ * The host's naming rule, which this package does not own: an unknown call
69
+ * receiver is repaired by an `imports:` alias only where the name could be
70
+ * one, and telling the author of `dbb.query(1)` to add an import is advice
71
+ * for a different mistake. Absent leaves the unknown-identifier message
72
+ * unqualified, which is what it says with no host rule to consult.
73
+ */
74
+ readonly couldNameModule?: (name: string) => boolean;
34
75
  }
35
76
  /** A mechanically applicable repair for a diagnostic. `replacement` is the
36
77
  * **whole analyzed source**, corrected — never a fragment — so a consumer can
@@ -63,11 +104,26 @@ export interface EngineDiagnostic {
63
104
  * call), and policy that depends on manifest context does not belong in a
64
105
  * templating engine. */
65
106
  export interface CallSite {
107
+ /** The name called. For a module call this is the QUALIFIED name as written
108
+ * (`Billing.format`), which is what keeps a module function and a catalog
109
+ * function of the same bare name apart in every consumer that keys on it. */
66
110
  readonly name: string;
67
111
  /** How it was written — `f(x)` vs `x.f()`. */
68
112
  readonly form: "global" | "receiver";
113
+ /** Set when the receiver is one of the declaring module's names, so the call
114
+ * resolved to a module function rather than to the catalog. Its determinism
115
+ * and host-backedness are not this engine's to know — they follow from the
116
+ * callable the name resolves to — so they are carried only as the host's
117
+ * {@link AnalyzeEnv.moduleCallFlags} reports them. */
118
+ readonly moduleCall?: true;
69
119
  /** Argument count as written; excludes the receiver. */
70
120
  readonly arity: number;
121
+ /** For a module call, each argument as the analysis saw it: the type the
122
+ * checker gave it (absent when the expression did not type-check) and, when
123
+ * the argument is a plain member chain (`variables.price`), that chain — so a
124
+ * caller holding the callee's signature can compare a declared shape rather
125
+ * than a CEL type alone. */
126
+ readonly arguments?: readonly CallArgument[];
71
127
  /** Offsets of the whole call within the analyzed source. */
72
128
  readonly start: number;
73
129
  readonly end: number;
@@ -75,6 +131,16 @@ export interface CallSite {
75
131
  * name resolves to nothing, or to a function carrying no determinism
76
132
  * metadata — absent is not "deterministic". */
77
133
  readonly deterministic?: boolean;
134
+ /** Whether the resolved function is supplied by the host rather than written
135
+ * in CEL — a catalog function the kernel implements natively, or a module
136
+ * function reaching native code — so nothing without that host can run it.
137
+ * `undefined` under the same rule as {@link CallSite.deterministic}. */
138
+ readonly hostBacked?: boolean;
139
+ }
140
+ /** One argument of a module call. */
141
+ export interface CallArgument {
142
+ readonly type?: string;
143
+ readonly chain?: readonly string[];
78
144
  }
79
145
  /** What one `analyze` call establishes about one source. Everything derivable
80
146
  * from the expression alone is derived here, once; everything that needs
@@ -1 +1 @@
1
- {"version":3,"file":"engine.d.ts","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C,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,GAAG,aAAa,GAAG,OAAO,CAAC;IAElE;;iEAE6D;IAC7D,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,GAAG,aAAa,CAAC;IAExD;;;;;;;;;;;;uBAYmB;IACnB,UAAU,CAAC,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,eAAe,EAAE,CAAC;IAExD;;;;;;;;;;;;;;;;;2CAiBuC;IACvC,YAAY,CAAC,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC1C"}
@@ -1 +1 @@
1
- {"version":3,"file":"cel.d.ts","sourceRoot":"","sources":["../../src/engines/cel.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAoB,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAElG;;;;;;;;;;2BAU2B;AAC3B,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,GAAG,aAAa,CA2GnF;AAYD;yCACyC;AACzC,eAAO,MAAM,SAAS,EAAE,gBAWvB,CAAC"}
1
+ {"version":3,"file":"cel.d.ts","sourceRoot":"","sources":["../../src/engines/cel.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EACV,UAAU,EACV,aAAa,EAGb,gBAAgB,EACjB,MAAM,cAAc,CAAC;AAEtB;;;;;;;;;;2BAU2B;AAC3B,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,GAAG,aAAa,CAkKnF;AAsCD;yCACyC;AACzC,eAAO,MAAM,SAAS,EAAE,gBAWvB,CAAC"}