@statedelta-actions/actions 0.2.0 → 0.5.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/README.md CHANGED
@@ -16,6 +16,8 @@ Ações são **declarativas**. Uma action é um ID mais uma lista ordenada de di
16
16
 
17
17
  **Sem sugar no runtime.** Diretivas devem ser passadas em **forma canônica** — toda diretiva tem campo `type`. Sugar forms (shorthands de autoria) são concern do compilador JSON DSL, que normaliza antes de entregar pro engine. O engine não interpreta nem converte sugar.
18
18
 
19
+ **Modo interactive opcional.** Actions podem **pausar entre diretivas** e aguardar input externo via generators (sync ou async). Implementado de forma ortogonal ao modo async — a granularidade é per-action transitiva via mini-graph interno. Ver [Modo Interactive](#modo-interactive) abaixo.
20
+
19
21
  ## Instalação
20
22
 
21
23
  ```bash
@@ -128,6 +130,47 @@ const handlers = {
128
130
  | `validate` | Register | Validação estrutural (rejeita diretivas malformadas) |
129
131
  | `execute` | Runtime | Processa a diretiva e retorna resultado |
130
132
  | `analyze` | — | Consumido pelo `ActionAnalyzer` externo, não pelo engine |
133
+ | `async` (flag) | Construct | Marca handler como assíncrono (opt-in explícito) |
134
+
135
+ ### Handlers Async
136
+
137
+ Handlers podem retornar `Promise<ApplyResult>`. O engine detecta async de duas formas:
138
+
139
+ ```typescript
140
+ const handlers = {
141
+ // 1. Auto-detect via `async function` — pega quando execute é declarado async.
142
+ fetchUser: {
143
+ async execute(directive, frame) {
144
+ const user = await api.get(`/users/${directive.id}`);
145
+ return { ok: true, data: user };
146
+ },
147
+ },
148
+
149
+ // 2. Flag explícita — para wrappers que retornam Promise sem ser `async function`.
150
+ delegateAsync: {
151
+ async: true,
152
+ execute: (d, frame, engine) => engine.invokeAsync("inner", undefined, frame),
153
+ },
154
+ };
155
+ ```
156
+
157
+ **Engine híbrido.** O engine pode misturar handlers sync e async no mesmo registry. Se qualquer handler for async (ou qualquer hook for async), o engine inteiro vira async — `invoke()` lança e você passa a usar `invokeAsync()`. Mas o **JIT per-action decide sync/async por action**: actions que só usam handlers sync compilam wrapper sync, sem `await`. FPS/games com 100% handlers sync pagam zero overhead de async; ETL/business com handlers async awaita só onde precisa.
158
+
159
+ ```typescript
160
+ const engine = createActionEngine({
161
+ handlers: {
162
+ update: { execute: (d, f) => { /* sync */ return { ok: true }; } },
163
+ fetchDB: { async execute(d, f) { return { ok: true, data: await db.query(...) }; } },
164
+ },
165
+ });
166
+
167
+ engine.isAsync; // true (fetchDB é async)
168
+ engine.invoke("anything"); // throws — use invokeAsync
169
+
170
+ // JIT compila:
171
+ // action que usa só `update` → wrapper sync, zero await
172
+ // action que usa `fetchDB` → wrapper async, await no handler
173
+ ```
131
174
 
132
175
  ## Ações
133
176
 
@@ -156,11 +199,13 @@ Três categorias. **Toda diretiva tem campo `type`** (forma canônica). O engine
156
199
  { type: "let", name: "total", value: "$", resolve: (ctx, scope) => ({ value: scope.subtotal * 1.1 }) }
157
200
  ```
158
201
 
159
- **Control** — saída antecipada:
202
+ **Control** — saída antecipada e ramificação:
160
203
 
161
204
  ```typescript
162
205
  { type: "return", value: "done" } // success: true, data: "done"
163
206
  { type: "throw", message: "saldo insuficiente" } // success: false
207
+ { type: "if", cond: (ctx, scope) => scope.hp > 0,
208
+ then: [...], else: [...] } // ramificação inline
164
209
  ```
165
210
 
166
211
  **Handler** — dispatch pro handler registrado:
@@ -172,7 +217,21 @@ Três categorias. **Toda diretiva tem campo `type`** (forma canônica). O engine
172
217
 
173
218
  O interpreter e JIT operam sobre formato uniforme — um único dispatch via `type` field, sem branching de categorias.
174
219
 
175
- Os nomes `const`, `let`, `return`, `throw` são **tipos reservados** — o engine rejeita handlers com esses nomes.
220
+ Os nomes `const`, `let`, `return`, `throw`, `if`, `pause` são **tipos reservados** — o engine rejeita handlers com esses nomes.
221
+
222
+ ### `if` (then/else)
223
+
224
+ `cond` aceita função `(ctx, scope) => boolean` ou boolean literal. Branches executam **no mesmo scope da action** — `const`/`let` em branch ficam visíveis depois. `return`/`throw`/`halt` em branch saem da action inteira (semântica esperada). Aninhamento livre.
225
+
226
+ ```typescript
227
+ { type: "if",
228
+ cond: (_ctx, scope) => scope.hp > 0,
229
+ then: [{ type: "state", target: "status", value: "alive" }],
230
+ else: [{ type: "throw", message: "dead" }],
231
+ }
232
+ ```
233
+
234
+ `cond` lança → erro coletado em `errors[]`, nenhum branch executa, prossegue. JIT emite `if/else` JS nativo unrolled (zero call overhead).
176
235
 
177
236
  Diretivas suportam:
178
237
  - **`as`** — captura `result.data` no scope: `scope["prev"] = result.data`
@@ -240,7 +299,7 @@ engine.context(ctx, () => {
240
299
  engine.invoke("combat");
241
300
  });
242
301
 
243
- // Async (obrigatório se algum hook for async)
302
+ // Async (obrigatório se algum hook ou handler for async)
244
303
  const result = await engine.invokeAsync("heal", undefined, ctx);
245
304
  ```
246
305
 
@@ -353,6 +412,8 @@ createActionEngine({ handlers, mode: "auto" }); // Interpreta primeiro, pr
353
412
  | `jit` | Mais lento (compila) | `new Function` compilado | Produção, ações estáveis |
354
413
  | `auto` | Rápido | Promove per-action após threshold | Uso geral (padrão) |
355
414
 
415
+ **Decisão sync/async é per-action.** No JIT per-action, cada action é compilada sync ou async independentemente, baseado nos handlers que ela usa. Em um engine híbrido, actions 100% sync ficam com wrapper sync e zero `await`; actions com pelo menos um handler async ficam com wrapper `async` e `await` em todos os handlers da action. Isso garante que uso fully-sync (FPS, game loops) não pague o custo de async functions só porque outro handler do mesmo engine é async.
416
+
356
417
  Threshold de auto-promote (padrão: 8):
357
418
 
358
419
  ```typescript
@@ -368,7 +429,7 @@ engine.compile();
368
429
  Informações de compilação:
369
430
 
370
431
  ```typescript
371
- engine.isAsync; // true se hooks async detectados
432
+ engine.isAsync; // true se algum hook OU handler é async
372
433
  engine.compilationMode; // "interpret" | "jit" (modo atual, não o requestado)
373
434
  ```
374
435
 
@@ -394,7 +455,7 @@ createActionEngine({
394
455
  });
395
456
  ```
396
457
 
397
- Hooks podem ser sync ou async. Hooks async tornam o engine async (`engine.isAsync === true`), exigindo `invokeAsync()`.
458
+ Hooks podem ser sync ou async. Hooks async tornam o engine async (`engine.isAsync === true`), exigindo `invokeAsync()`. Handlers async (via flag `async: true` ou `async function`) também tornam o engine async — ver [Handlers Async](#handlers-async).
398
459
 
399
460
  **Custo zero quando ausente.** A compilação JIT não emite código de hook para hooks não registrados.
400
461
 
@@ -569,6 +630,22 @@ import type {
569
630
  ActionInterceptResult,
570
631
  } from "@statedelta-actions/actions";
571
632
 
633
+ // Interactive
634
+ import type {
635
+ InteractiveConfig,
636
+ PauseEvent,
637
+ InteractiveSession,
638
+ AsyncInteractiveSession,
639
+ InteractiveApplyResult,
640
+ Responder,
641
+ } from "@statedelta-actions/actions";
642
+
643
+ import {
644
+ drainSync,
645
+ drainAsync,
646
+ replayResponder,
647
+ } from "@statedelta-actions/actions";
648
+
572
649
  // Register Pipeline
573
650
  import { RESERVED_TYPES } from "@statedelta-actions/actions";
574
651
 
@@ -588,6 +665,184 @@ import { buildActionExecutor } from "@statedelta-actions/actions";
588
665
  import type { GeneratedActionExecutor } from "@statedelta-actions/actions";
589
666
  ```
590
667
 
668
+ ## Modo Interactive
669
+
670
+ Pausa execução de uma action e aguarda input externo do customer. Implementado via **generators** (sync ou async), ortogonal ao modo async.
671
+
672
+ **Use cases:**
673
+ - UX interativa em produção (wizards, prompts, confirmações)
674
+ - Debug entre diretivas (futuro — modo separado)
675
+
676
+ **Mecânica:**
677
+ - **Handler interactive** declarado com `interactive: true` (espelha `async: true`) — handler é generator function
678
+ - **Diretiva reservada `type: "pause"`** — engine emite yield direto pra confirmação/breakpoint declarativo
679
+ - **Mini-graph interno** propaga `_interactiveActions` transitivamente (ADR-026)
680
+ - **API**: `engine.invokeInteractive(id, params, ctx)` retorna `Iterator | AsyncIterator`
681
+
682
+ ### Habilitação
683
+
684
+ ```typescript
685
+ const engine = createActionEngine({
686
+ handlers,
687
+ interactive: {}, // habilita modo interactive
688
+ });
689
+ ```
690
+
691
+ Sem `interactive` configurado:
692
+ - Handler com `interactive: true` → erro no constructor (fail-fast)
693
+ - Diretiva `type: "pause"` → erro no register
694
+
695
+ ### Handler interactive
696
+
697
+ Handlers são **primitivas Lego** — genéricas, sem semântica de domínio (ADR-028). Vocabulário canonical pequeno (`state`, `emit`, `action`, `log`, `halt`) + handler interactive primitivo proposto: **`input`** (yield + schema + retry).
698
+
699
+ > ⚠️ **Não use nomes domain-specific** (`askUser`, `confirmDelete`, `validateEmail`). Cria explosão combinatória, polui vocabulário canonical e viola o princípio Lego. Customer compõe domínio via **payload** das diretivas, não criando handlers.
700
+
701
+ ```typescript
702
+ // Pattern recomendado: primitiva `input` genérica com schema + retry interno
703
+ const handlers = {
704
+ input: {
705
+ interactive: true,
706
+ *execute(directive) {
707
+ let lastError: string | null = null;
708
+ let attempt = 1;
709
+ const max = directive.maxAttempts ?? Infinity;
710
+ while (attempt <= max) {
711
+ const answer = yield {
712
+ kind: "input",
713
+ payload: directive.payload,
714
+ schema: directive.schema,
715
+ attempt,
716
+ lastError,
717
+ };
718
+ if (!directive.schema) return { ok: true, data: answer };
719
+ const r = directive.schema.parse(answer);
720
+ if (r.ok) return { ok: true, data: r.data };
721
+ lastError = r.error;
722
+ attempt++;
723
+ }
724
+ return { ok: false, error: `validation failed after ${max} attempts` };
725
+ },
726
+ },
727
+ };
728
+ ```
729
+
730
+ Customer compõe actions com **payload domain-specific** sobre a primitiva:
731
+
732
+ ```typescript
733
+ { id: "register", directives: [
734
+ { type: "input", payload: { kind: "text", label: "Nome" }, as: "name", schema: nameSchema },
735
+ { type: "input", payload: { kind: "number", label: "Idade" }, as: "age", schema: ageSchema },
736
+ { type: "input", payload: { kind: "confirm", message: "Confirma?" }, as: "ok" },
737
+ { type: "state", target: "users", op: "push", value: { name: ..., age: ... } },
738
+ ]}
739
+ ```
740
+
741
+ Consumer interpreta `payload.kind` — renderiza UI/CLI/voice. Framework não opina.
742
+
743
+ **Schema agnóstico** — interface mínima `{ parse(raw): { ok: true, data } | { ok: false, error } }`. Customer adapta zod/valibot/json-schema/custom em 1 linha.
744
+
745
+ > **`input` (intra-tick) ≠ `request` (cross-tick).** `input` pausa **dentro** de uma invocação (yield/next imediato). `request` (RealmSystem futuro, fora deste package) anota dependência declarativa, lock cross-tick. São conceitos distintos com nomes propositalmente diferentes.
746
+
747
+ Auto-detect via `isGeneratorFunction(execute)` cobre `function*` e `async function*`. Use a flag `interactive: true` pra wrappers que retornam iterator sem ser generator function.
748
+
749
+ ### Diretiva `type: "pause"` (engine-level)
750
+
751
+ ```typescript
752
+ { type: "pause", message: "Confirmação destrutiva?", as: "ack" }
753
+ ```
754
+
755
+ Engine yield direto:
756
+ ```typescript
757
+ session.next(); // → { source: "pause", payload: { message: "..." }, ... }
758
+ session.next("ok"); // → continua, captura "ok" em scope.ack
759
+ session.next(false); // → aborta (abortedBy: "pause")
760
+ session.next("cancel"); // → aborta
761
+ session.next("abort"); // → aborta
762
+ ```
763
+
764
+ ### Drenagem do iterator
765
+
766
+ ```typescript
767
+ // Customer dirigindo manualmente
768
+ const session = engine.invokeInteractive("wizard", undefined, ctx);
769
+ const r1 = session.next(); // PauseEvent ou payload custom
770
+ const r2 = session.next("Anderson"); // entrega resposta
771
+ // quando r.done === true, r.value é o DirectiveResult final
772
+ ```
773
+
774
+ Helpers opcionais pra "play mode":
775
+
776
+ ```typescript
777
+ import { drainSync, drainAsync, replayResponder } from "@statedelta-actions/actions";
778
+
779
+ // Drena com responder programático
780
+ const result = drainSync(session, (event) => {
781
+ // event pode ser PauseEvent (type:"pause") ou payload custom (handler)
782
+ if ("prompt" in event) return responses[event.prompt];
783
+ if ((event as PauseEvent).source === "pause") return "ok";
784
+ return undefined;
785
+ });
786
+
787
+ // Replay determinístico (testes)
788
+ const result = drainSync(session, replayResponder(["Anderson", 42, "yes"]));
789
+ ```
790
+
791
+ ### Sub-actions interativas (yield* propaga pausas)
792
+
793
+ Quando action root invoca child interactive, pausas do child fluem pro consumer no nível raiz via `yield*`. Handler `action` (consumer-defined) decide:
794
+
795
+ ```typescript
796
+ const handlers = {
797
+ action: {
798
+ execute: (d, f, e) => {
799
+ if (e.isActionInteractive(d.id)) {
800
+ // Target é interactive → propaga via iterator
801
+ return { ok: true, iterator: e.invokeInteractive(d.id, d.params, f) };
802
+ }
803
+ // Target sync → invoke regular
804
+ const r = e.invoke(d.id, d.params, f);
805
+ return { ok: r.success, data: r.data };
806
+ },
807
+ },
808
+ };
809
+ ```
810
+
811
+ Engine consulta mini-graph e marca actions transitivamente interativas. JIT detecta `_result.iterator` em runtime e emite `yield*` automaticamente.
812
+
813
+ ### Matriz async × interactive
814
+
815
+ | Engine async? | Action interactive? | Compilação |
816
+ |---------------|---------------------|------------|
817
+ | não | não | `function ()` |
818
+ | não | sim | `function* ()` |
819
+ | sim | não | `async function ()` |
820
+ | sim | sim | `async function* ()` |
821
+
822
+ Granularidade per-action via mini-graph (ADR-021/025/026). FPS 100% sync com 1 action interactive isolada não paga overhead de generator nas outras.
823
+
824
+ ### `invoke()` per-action transitivo
825
+
826
+ `invoke()` lança per-action (não global):
827
+
828
+ ```typescript
829
+ engine.invoke("syncAction"); // OK — sub-árvore inteira sync
830
+ engine.invoke("asyncAction"); // throw "use invokeAsync"
831
+ engine.invoke("interactiveAction"); // throw "use invokeInteractive"
832
+ ```
833
+
834
+ Engine híbrido (handler async + actions sync isoladas) permite `invoke()` regular nas sync. ADR-026.
835
+
836
+ ### Action hooks fora do generator
837
+
838
+ `beforeAction` / `afterAction` (ADR-019) executam **antes** e **depois** do generator. Skip via `beforeAction` retorna `memoResult` imediatamente. `afterAction` substitui o resultado final.
839
+
840
+ ### Catch atômico
841
+
842
+ Diretivas dentro de `catch` executam **atomicamente** (ADR-023) via `engine.runDirectives` — sem yield. Caminho de erro não pausa. Diretivas `type: "pause"` ou handler interactive dentro de catch geram warning em register-time.
843
+
844
+ ---
845
+
591
846
  ## Analyzer
592
847
 
593
848
  Funcionalidades de análise estática foram extraídas para o pacote `@statedelta-actions/analyzer`:
package/dist/index.cjs CHANGED
@@ -1,9 +1,9 @@
1
- 'use strict';var core=require('@statedelta-actions/core');var H=class{_listeners=new Map;on(e,i){let s=this._listeners.get(e);s||(s=new Set,this._listeners.set(e,s)),s.add(i);let n=false;return ()=>{n||(n=true,s.delete(i),s.size===0&&this._listeners.delete(e));}}once(e,i){let s=this.on(e,n=>{s(),i(n);});return s}emit(e,i){let s=this._listeners.get(e);if(!s||s.size===0)return;let n=[...s];for(let r=0;r<n.length;r++)try{n[r](i);}catch(c){console.error(`[SimpleEmitter] Listener error on "${e}":`,c);}}listenerCount(e){return this._listeners.get(e)?.size??0}hasListeners(e){let i=this._listeners.get(e);return i!==void 0&&i.size>0}off(e){this._listeners.delete(e);}removeAllListeners(){this._listeners.clear();}};var I=new Set(["const","let","return","throw"]);function W(t,e,i,s){let n=[];for(let r=0;r<t.directives.length;r++){let c=t.directives[r],f=c[s];if(!f){n.push(`directive[${r}]: missing type field "${s}"`);continue}if(I.has(f))continue;if(!e[f]){n.push(`directive[${r}]: no handler for type "${f}"`);continue}let _=i.get(f);if(_?.validate)try{let C=_.validate(c);C&&!C.valid&&n.push(`directive[${r}]: ${C.error??"validation failed"}`);}catch(C){n.push(`directive[${r}]: validate threw: ${C}`);}let h=c.catch;if(h&&Array.isArray(h))for(let C=0;C<h.length;C++){let v=h[C][s];v&&I.has(v)||v&&!e[v]&&n.push(`directive[${r}].catch[${C}]: no handler for type "${v}"`);}}return n}function S(t,e){let i=t.length,s=new Array(i);for(let n=0;n<i;n++){let r=t[n],c=r.catch;Array.isArray(c)&&c.length>0?s[n]={...r,catch:S(c)}:s[n]=r;}return s}function P(t,e,i,s,n,r,c){return {success:false,aborted:true,abortedBy:t,appliedCount:e,skippedCount:i,errors:s,processedCount:n,totalCount:r,counters:c}}function te(t){let e=t.filledNames.has("beforeDirective"),i=t.filledNames.has("afterDirective"),s=t.filledNames.has("onDirectivesComplete");return function(r,c,f,_,h,C,T){let v=Math.min(r.length,T),l=[],m=0,x=0,{counters:u,scope:D}=c,R=c.ctx;for(let a=0;a<v;a++){let p=r[a],E=p[h];if(E==="const"||E==="let"){let o=p.name;if(typeof p.resolve=="function")try{D[o]=p.resolve(R,D).value;}catch(d){l.push({directiveIndex:a,error:`Binding resolve: ${d}`}),u.errors++;}else D[o]=p.value;continue}if(E==="return"){let o=a+1;if(typeof p.resolve=="function")try{let d=p.resolve(R,D),b="value"in d?d.value:"return"in d?d.return:p.value;return {success:!0,aborted:!1,appliedCount:m,skippedCount:x,errors:l,processedCount:o,totalCount:v,counters:u,data:b}}catch(d){l.push({directiveIndex:a,error:`Control resolve: ${d}`}),u.errors++;}else return {success:true,aborted:false,appliedCount:m,skippedCount:x,errors:l,processedCount:o,totalCount:v,counters:u,data:p.value};continue}if(E==="throw"){let o=a+1;if(typeof p.resolve=="function")try{let d=p.resolve(R,D),b="message"in d?d.message:"throw"in d?d.throw:p.message;return {success:!1,aborted:!0,abortedBy:"throw",appliedCount:m,skippedCount:x,errors:l,processedCount:o,totalCount:v,counters:u,data:b}}catch(d){l.push({directiveIndex:a,error:`Control resolve: ${d}`}),u.errors++;}else return {success:false,aborted:true,abortedBy:"throw",appliedCount:m,skippedCount:x,errors:l,processedCount:o,totalCount:v,counters:u,data:p.message};continue}let y=p,A=c;if(e)try{let o=core.processIntercept(_.beforeDirective(y,c),"beforeDirective");if(o.action===core.Intercept.SKIP){x++,u.directivesSkipped++;continue}if(o.action===core.Intercept.ABORT)return P(o.abortReason,m,x,l,a,v,u);o.ctx!==void 0&&(A=c.withCtx(o.ctx)),o.directive!==void 0&&(y=o.directive);}catch(o){l.push({directiveIndex:a,error:`Hook beforeDirective: ${o}`}),u.errors++;}if(typeof y.resolve=="function")try{let o=y.resolve(A.ctx,A.scope);y={...y,...o};}catch(o){l.push({directiveIndex:a,error:`Directive resolve: ${o}`}),u.errors++;continue}let w=y[h],F=w?f[w]:void 0;if(!F){l.push({directiveIndex:a,error:`No handler for directive type: ${w??"undefined"}`}),u.errors++;continue}let g;try{g=F(y,A,C);}catch(o){g={ok:false,error:String(o)};}let $=a+1;if(g.ok){if(m++,u.directivesApplied++,typeof y.as=="string"&&(D[y.as]=g.data),g.halt)return {success:true,aborted:true,abortedBy:"halt",appliedCount:m,skippedCount:x,errors:l,processedCount:$,totalCount:v,counters:u,data:g.data}}else {if(g.halt)return {success:false,aborted:true,abortedBy:"halt",appliedCount:m,skippedCount:x,errors:l,processedCount:$,totalCount:v,counters:u,data:g.data};let o=y.catch;if(Array.isArray(o)&&o.length>0){D.$exception=g.error??"handler failed";let d=C.runDirectives(o,A);m+=d.appliedCount;for(let b=0;b<d.errors.length;b++)l.push(d.errors[b]);if(d.aborted)return {success:d.success,aborted:true,abortedBy:d.abortedBy,appliedCount:m,skippedCount:x,errors:l,processedCount:$,totalCount:v,counters:u,data:d.data}}else l.push({directiveIndex:a,error:g.error??"handler failed"}),u.errors++;}if(i)try{if(_.afterDirective(y,g,A)==="abort")return P("afterDirective",m,x,l,$,v,u)}catch(o){l.push({directiveIndex:a,error:`Hook afterDirective: ${o}`}),u.errors++;}}let k={success:true,aborted:false,appliedCount:m,skippedCount:x,errors:l,processedCount:v,totalCount:v,counters:u};if(s)try{_.onDirectivesComplete(k);}catch{}return k}}function re(t){let e=t.filledNames.has("beforeDirective"),i=t.filledNames.has("afterDirective"),s=t.filledNames.has("onDirectivesComplete");return async function(r,c,f,_,h,C,T){let v=Math.min(r.length,T),l=[],m=0,x=0,{counters:u,scope:D}=c,R=c.ctx;for(let a=0;a<v;a++){let p=r[a],E=p[h];if(E==="const"||E==="let"){let o=p.name;if(typeof p.resolve=="function")try{D[o]=p.resolve(R,D).value;}catch(d){l.push({directiveIndex:a,error:`Binding resolve: ${d}`}),u.errors++;}else D[o]=p.value;continue}if(E==="return"){let o=a+1;if(typeof p.resolve=="function")try{let d=p.resolve(R,D),b="value"in d?d.value:"return"in d?d.return:p.value;return {success:!0,aborted:!1,appliedCount:m,skippedCount:x,errors:l,processedCount:o,totalCount:v,counters:u,data:b}}catch(d){l.push({directiveIndex:a,error:`Control resolve: ${d}`}),u.errors++;}else return {success:true,aborted:false,appliedCount:m,skippedCount:x,errors:l,processedCount:o,totalCount:v,counters:u,data:p.value};continue}if(E==="throw"){let o=a+1;if(typeof p.resolve=="function")try{let d=p.resolve(R,D),b="message"in d?d.message:"throw"in d?d.throw:p.message;return {success:!1,aborted:!0,abortedBy:"throw",appliedCount:m,skippedCount:x,errors:l,processedCount:o,totalCount:v,counters:u,data:b}}catch(d){l.push({directiveIndex:a,error:`Control resolve: ${d}`}),u.errors++;}else return {success:false,aborted:true,abortedBy:"throw",appliedCount:m,skippedCount:x,errors:l,processedCount:o,totalCount:v,counters:u,data:p.message};continue}let y=p,A=c;if(e)try{let o=core.processIntercept(await _.beforeDirective(y,c),"beforeDirective");if(o.action===core.Intercept.SKIP){x++,u.directivesSkipped++;continue}if(o.action===core.Intercept.ABORT)return P(o.abortReason,m,x,l,a,v,u);o.ctx!==void 0&&(A=c.withCtx(o.ctx)),o.directive!==void 0&&(y=o.directive);}catch(o){l.push({directiveIndex:a,error:`Hook beforeDirective: ${o}`}),u.errors++;}if(typeof y.resolve=="function")try{let o=y.resolve(A.ctx,A.scope);y={...y,...o};}catch(o){l.push({directiveIndex:a,error:`Directive resolve: ${o}`}),u.errors++;continue}let w=y[h],F=w?f[w]:void 0;if(!F){l.push({directiveIndex:a,error:`No handler for directive type: ${w??"undefined"}`}),u.errors++;continue}let g;try{g=F(y,A,C);}catch(o){g={ok:false,error:String(o)};}let $=a+1;if(g.ok){if(m++,u.directivesApplied++,typeof y.as=="string"&&(D[y.as]=g.data),g.halt)return {success:true,aborted:true,abortedBy:"halt",appliedCount:m,skippedCount:x,errors:l,processedCount:$,totalCount:v,counters:u,data:g.data}}else {if(g.halt)return {success:false,aborted:true,abortedBy:"halt",appliedCount:m,skippedCount:x,errors:l,processedCount:$,totalCount:v,counters:u,data:g.data};let o=y.catch;if(Array.isArray(o)&&o.length>0){D.$exception=g.error??"handler failed";let d=await C.runDirectivesAsync(o,A);m+=d.appliedCount;for(let b=0;b<d.errors.length;b++)l.push(d.errors[b]);if(d.aborted)return {success:d.success,aborted:true,abortedBy:d.abortedBy,appliedCount:m,skippedCount:x,errors:l,processedCount:$,totalCount:v,counters:u,data:d.data}}else l.push({directiveIndex:a,error:g.error??"handler failed"}),u.errors++;}if(i)try{if(await _.afterDirective(y,g,A)==="abort")return P("afterDirective",m,x,l,$,v,u)}catch(o){l.push({directiveIndex:a,error:`Hook afterDirective: ${o}`}),u.errors++;}}let k={success:true,aborted:false,appliedCount:m,skippedCount:x,errors:l,processedCount:v,totalCount:v,counters:u};if(s)try{await _.onDirectivesComplete(k);}catch{}return k}}function B(t,e){return e?re(t):te(t)}function j(t){let{filledNames:e,hasAnyAsync:i}=t,s=h=>e.has(h),n=h=>t.asyncNames.has(h)?"await ":"",r=[];for(let h of e)r.push(`const ${h} = directiveHooks.${h};`);r.push("const len = Math.min(directives.length, maxDirectives);"),r.push("const errors = []; let appliedCount = 0; let skippedCount = 0;"),r.push("const counters = frame.counters;"),r.push("for (let i = 0; i < len; i++) {"),r.push(" let directive = directives[i];"),s("beforeDirective")?(r.push(" let _df = frame;"),r.push(" try {"),r.push(` const _bd = ${n("beforeDirective")}beforeDirective(directive, frame);`),r.push(' if (_bd === "skip") { skippedCount++; counters.directivesSkipped++; continue; }'),r.push(' if (_bd === "abort") return { success: false, aborted: true, abortedBy: "beforeDirective", appliedCount, skippedCount, errors, processedCount: i, totalCount: len, counters };'),r.push(' if (typeof _bd === "object" && _bd !== null) {'),r.push(' if ("abort" in _bd) return { success: false, aborted: true, abortedBy: _bd.abort, appliedCount, skippedCount, errors, processedCount: i, totalCount: len, counters };'),r.push(' if ("ctx" in _bd) _df = frame.withCtx(_bd.ctx);'),r.push(' if ("directive" in _bd) directive = _bd.directive;'),r.push(" }"),r.push(' } catch (_e) { errors.push({ directiveIndex: i, error: "Hook beforeDirective: " + _e }); counters.errors++; }')):r.push(" const _df = frame;"),r.push(' if (typeof directive.resolve === "function") {'),r.push(" try { const _r = directive.resolve(_df.ctx); directive = Object.assign({}, directive, _r); }"),r.push(' catch (_e) { errors.push({ directiveIndex: i, error: "Directive resolve: " + _e }); counters.errors++; continue; }'),r.push(" }"),r.push(" const _type = directive[typeField];"),r.push(" const _handler = _type ? handlers[_type] : undefined;"),r.push(' if (!_handler) { errors.push({ directiveIndex: i, error: "No handler for directive type: " + (_type || "undefined") }); counters.errors++; continue; }'),r.push(" let _result;"),r.push(" try { _result = _handler(directive, _df, engine); } catch (_e) { errors.push({ directiveIndex: i, error: String(_e) }); counters.errors++; continue; }"),r.push(" if (_result.ok) { appliedCount++; counters.directivesApplied++; }"),r.push(' else { errors.push({ directiveIndex: i, error: _result.error || "handler failed" }); counters.errors++; }'),s("afterDirective")&&(r.push(" try {"),r.push(` const _ad = ${n("afterDirective")}afterDirective(directive, _result, _df);`),r.push(' if (_ad === "abort") return { success: false, aborted: true, abortedBy: "afterDirective", appliedCount, skippedCount, errors, processedCount: i + 1, totalCount: len, counters };'),r.push(' } catch (_e) { errors.push({ directiveIndex: i, error: "Hook afterDirective: " + _e }); counters.errors++; }')),r.push("}"),r.push("const _finalResult = { success: true, aborted: false, appliedCount, skippedCount, errors, processedCount: len, totalCount: len, counters };"),s("onDirectivesComplete")&&r.push(`try { ${n("onDirectivesComplete")}onDirectivesComplete(_finalResult); } catch (_e) {}`),r.push("return _finalResult;");let c=r.join(`
2
- `),f=i?"async ":"";return {fn:new Function("directives","frame","handlers","directiveHooks","typeField","engine","maxDirectives",`"use strict";
3
- return (${f}() => {
4
- ${c}
5
- })();`),isAsync:i}}function N(t,e,i){return `return{success:false,aborted:true,abortedBy:${t},appliedCount,skippedCount,errors,processedCount:${e},totalCount:${i},counters};`}function ie(t,e){return `return{success:true,aborted:true,abortedBy:"halt",appliedCount,skippedCount,errors,processedCount:${t},totalCount:${e},counters,data:_result.data};`}function se(t,e){return `return{success:false,aborted:true,abortedBy:"halt",appliedCount,skippedCount,errors,processedCount:${t},totalCount:${e},counters,data:_result.data};`}function ne(t,e,i){let s=JSON.stringify(e.name);typeof e.resolve=="function"?(t.push("try{"),t.push(`scope[${s}]=$.d[${i}].resolve(ctx,scope).value;`),t.push(`}catch(_e){errors.push({directiveIndex:${i},error:"Binding resolve: "+_e});counters.errors++;}`)):t.push(`scope[${s}]=$.d[${i}].value;`);}function oe(t,e,i,s){let n=typeof e.resolve=="function",r=i+1;n?(t.push("try{"),t.push(`const _rv=$.d[${i}].resolve(ctx,scope);`),t.push(`return{success:true,aborted:false,appliedCount,skippedCount,errors,processedCount:${r},totalCount:${s},counters,data:"value" in _rv?_rv.value:"return" in _rv?_rv.return:$.d[${i}].value};`),t.push(`}catch(_e){errors.push({directiveIndex:${i},error:"Control resolve: "+_e});counters.errors++;}`)):t.push(`return{success:true,aborted:false,appliedCount,skippedCount,errors,processedCount:${r},totalCount:${s},counters,data:$.d[${i}].value};`);}function ce(t,e,i,s){let n=typeof e.resolve=="function",r=i+1;n?(t.push("try{"),t.push(`const _rv=$.d[${i}].resolve(ctx,scope);`),t.push(`return{success:false,aborted:true,abortedBy:"throw",appliedCount,skippedCount,errors,processedCount:${r},totalCount:${s},counters,data:"message" in _rv?_rv.message:"throw" in _rv?_rv.throw:$.d[${i}].message};`),t.push(`}catch(_e){errors.push({directiveIndex:${i},error:"Control resolve: "+_e});counters.errors++;}`)):t.push(`return{success:false,aborted:true,abortedBy:"throw",appliedCount,skippedCount,errors,processedCount:${r},totalCount:${s},counters,data:$.d[${i}].message};`);}function ae(t,e,i,s,n,r,c,f,_,h){let C=e[n],T=r.get(C),v=typeof e.resolve=="function",l=typeof e.as=="string",m=Array.isArray(e.catch)&&e.catch.length>0,x=l?JSON.stringify(e.as):"",u=i+1;t.push(`_b${i}:{`);let D=c||v,R=c;D&&t.push(`let _dir=$.d[${i}];`),R&&t.push("let _df=frame;"),c&&(t.push("try{"),t.push(`const _bd=${_}_hookBefore($.d[${i}],frame);`),t.push(`if(_bd==="skip"){skippedCount++;counters.directivesSkipped++;break _b${i};}`),t.push(`if(_bd==="abort")${N('"beforeDirective"',i,s)}`),t.push('if(typeof _bd==="object"&&_bd!==null){'),t.push(`if("abort" in _bd)${N("_bd.abort",i,s)}`),t.push('if("ctx" in _bd)_df=frame.withCtx(_bd.ctx);'),t.push('if("directive" in _bd)_dir=_bd.directive;'),t.push("}"),t.push(`}catch(_e){errors.push({directiveIndex:${i},error:"Hook beforeDirective: "+_e});counters.errors++;}`)),c?(t.push('if(typeof _dir.resolve==="function"){'),t.push("try{const _r=_dir.resolve(_df.ctx,scope);_dir=Object.assign({},_dir,_r);}"),t.push(`catch(_e){errors.push({directiveIndex:${i},error:"Directive resolve: "+_e});counters.errors++;break _b${i};}}`)):v&&(t.push(`try{const _r=$.d[${i}].resolve(ctx,scope);_dir=Object.assign({},$.d[${i}],_r);}`),t.push(`catch(_e){errors.push({directiveIndex:${i},error:"Directive resolve: "+_e});counters.errors++;break _b${i};}`));let k=D?"_dir":`$.d[${i}]`,a=R?"_df":"frame";t.push("let _result;"),t.push(`try{_result=${T}(${k},${a},$.engine);}`),t.push("catch(_e){_result={ok:false,error:String(_e)};}"),t.push("if(_result.ok){"),t.push("appliedCount++;counters.directivesApplied++;"),l&&t.push(`scope[${x}]=_result.data;`),t.push(`if(_result.halt)${ie(u,s)}`),t.push("}else{"),t.push(`if(_result.halt)${se(u,s)}`),m?(t.push('scope.$exception=_result.error||"handler failed";'),t.push(`const _cr=$.runner($.d[${i}].catch,${a});`),t.push("appliedCount+=_cr.appliedCount;"),t.push("for(let _j=0;_j<_cr.errors.length;_j++)errors.push(_cr.errors[_j]);"),t.push(`if(_cr.aborted)return{success:_cr.success,aborted:true,abortedBy:_cr.abortedBy,appliedCount,skippedCount,errors,processedCount:${u},totalCount:${s},counters,data:_cr.data};`)):t.push(`errors.push({directiveIndex:${i},error:_result.error||"handler failed"});counters.errors++;`),t.push("}"),f&&(t.push("try{"),t.push(`const _ad=${h}_hookAfter(${k},_result,${a});`),t.push(`if(_ad==="abort")${N('"afterDirective"',u,s)}`),t.push(`}catch(_e){errors.push({directiveIndex:${i},error:"Hook afterDirective: "+_e});counters.errors++;}`)),t.push("}");}function O(t,e,i){let s=[],n=t.length,{filledNames:r,hasAnyAsync:c,asyncNames:f}=e,_=r.has("beforeDirective"),h=r.has("afterDirective"),C=r.has("onDirectivesComplete"),T=f.has("beforeDirective")?"await ":"",v=f.has("afterDirective")?"await ":"",l=f.has("onDirectivesComplete")?"await ":"",m=new Set;for(let a of t){let p=a[i];p&&!I.has(p)&&m.add(p);}let x=new Map,u=0;for(let a of m)x.set(a,`_h${u++}`);s.push("const counters=frame.counters;"),s.push("const ctx=frame.ctx;"),s.push("const errors=[];"),s.push("let appliedCount=0;"),s.push("let skippedCount=0;");for(let[a,p]of x)s.push(`const ${p}=$.h[${JSON.stringify(a)}];`);_&&s.push("const _hookBefore=$.hooks.beforeDirective;"),h&&s.push("const _hookAfter=$.hooks.afterDirective;");for(let a=0;a<t.length;a++){let p=t[a];switch(p[i]){case "const":case "let":ne(s,p,a);break;case "return":oe(s,p,a,n);break;case "throw":ce(s,p,a,n);break;default:ae(s,p,a,n,i,x,_,h,T,v);break}}s.push(`const _finalResult={success:true,aborted:false,appliedCount,skippedCount,errors,processedCount:${n},totalCount:${n},counters};`),C&&s.push(`try{${l}$.hooks.onDirectivesComplete(_finalResult);}catch(_e){}`),s.push("return _finalResult;");let D=s.join(`
6
- `),R=c?"async ":"";return {fn:new Function("frame","scope","$",`"use strict";
7
- return(${R}()=>{
8
- ${D}
9
- })();`),isAsync:c}}function K(t,e,i){return {success:false,aborted:false,appliedCount:0,skippedCount:0,processedCount:0,totalCount:0,errors:[{directiveIndex:-1,error:e}],counters:t,...i}}function U(t,e){return K(e,`Action not found: "${t}"`)}function G(t,e,i){return K(i,`Max depth ${e} exceeded invoking "${t}"`,{aborted:true,abortedBy:"maxDepth"})}function Y(t,e){return K(e,`Action "${t}" is private (sub-action). Can only be invoked from within its parent scope.`)}function z(t,e){return {success:true,aborted:false,appliedCount:0,skippedCount:0,processedCount:0,totalCount:0,errors:[],data:t,counters:e}}function ue(t){return typeof t=="object"&&t!==null&&"execute"in t}function Q(t){let e=Object.create(null),i=new Map;for(let s of Object.keys(t)){let n=t[s];ue(n)?(e[s]=n.execute,i.set(s,n)):(e[s]=n,i.set(s,{execute:n}));}return {handlers:e,definitions:i}}function X(t,e){if(t==="*")return true;if(!t.includes("*"))return t===e;let i=t.replace(/[.+^${}()|[\]\\]/g,"\\$&");return new RegExp("^"+i.replace(/\*/g,".*")+"$").test(e)}function Z(t){return typeof t=="string"?{pattern:t}:t}function L(t,e,i){let s=new Map;if(!e&&!i){for(let n of t)s.set(n,{status:"available"});return s}if(e){let n=e.map(Z);for(let r of t){let c=n.some(f=>X(f.pattern,r));s.set(r,{status:c?"available":"denied"});}}else {let n=i.map(Z);for(let r of t){let c=n.find(f=>X(f.pattern,r));c?s.set(r,{status:"denied",reason:c.reason,source:c.source}):s.set(r,{status:"available"});}}return s}var pe=8,he="type",V={maxDepth:10,maxRules:1e4,maxDirectives:1e5},q=class{_directiveExecutor;_mode;_ctx;_requestedMode;_threshold;_limits;_typeField;_handlers;_definitions;_directiveHooks;_directiveAnalysis;_isAsync;_directivePermissions;_beforeAction;_afterAction;_registry=new Map;_emitter=new H;_batchDepth=0;_batchErrors=[];_batchWarnings=[];_batchActions=[];_batchRegistered=[];_registeredIds=new Set;_directiveRunner;_engineRef;constructor(e){this._requestedMode=e.mode??"auto",this._threshold=e.autoJitThreshold??pe,this._typeField=e.typeField??he,this._limits={maxDepth:e.limits?.maxDepth??V.maxDepth,maxRules:e.limits?.maxRules??V.maxRules,maxDirectives:e.limits?.maxDirectives??V.maxDirectives};let{handlers:i,definitions:s}=Q(e.handlers);for(let n of Object.keys(e.handlers))if(I.has(n))throw new Error(`Handler type "${n}" is reserved for engine-internal directives`);if(e.allowedDirectives&&e.blockedDirectives)throw new Error("allowedDirectives and blockedDirectives are mutually exclusive. Provide one or neither.");this._handlers=i,this._definitions=s,this._directivePermissions=L(Object.keys(i),e.allowedDirectives,e.blockedDirectives),this._directiveHooks=e.directiveHooks??{},this._beforeAction=e.actionHooks?.beforeAction??null,this._afterAction=e.actionHooks?.afterAction??null,this._directiveAnalysis=core.analyzeSlots(this._directiveHooks,core.DIRECTIVE_SLOT_NAMES),this._isAsync=this._directiveAnalysis.hasAnyAsync,this._engineRef={runDirectives:(n,r)=>this._executeDirectives(n,r),runDirectivesAsync:(n,r)=>this._executeDirectivesAsync(n,r),invoke:(n,r,c)=>this._invokeInternal(n,r,c),invokeAsync:(n,r,c)=>this._invokeInternalAsync(n,r,c),evaluateRules:()=>{throw new Error("evaluateRules() not available in ActionEngine context. Use createRuleEngine().")},evaluateRulesAsync:()=>{throw new Error("evaluateRulesAsync() not available in ActionEngine context. Use createRuleEngine().")}},this._directiveRunner=(n,r)=>this._directiveExecutor(n,r,this._handlers,this._directiveHooks,this._typeField,this._engineRef,this._limits.maxDirectives),this._requestedMode==="jit"?(this._directiveExecutor=this._buildJitDirectiveExecutor(),this._mode="jit"):(this._directiveExecutor=B(this._directiveAnalysis,this._isAsync),this._mode="interpret");}register(e){let i=[],s=[],n=[],r=[];for(let f of e){if(!f.id){s.push({actionId:"",error:"Action must have an id"});continue}let _=W(f,this._handlers,this._definitions,this._typeField);if(_.length>0){for(let h of _)s.push({actionId:f.id,error:h});continue}r.push(f);}for(let f of r){let _=S(f.directives,this._typeField),h={definition:f,directives:_,compiled:null,invokeCount:0};this._requestedMode==="jit"&&(h.compiled=this._compileAction(_)),this._registry.set(f.id,h),this._registeredIds.add(f.id),i.push(f.id);}if(this._batchDepth>0){this._batchErrors.push(...s),this._batchWarnings.push(...n);for(let f of r)this._batchActions.push(f);return this._batchRegistered.push(...i),{registered:i,errors:s,warnings:n}}let c={registered:i,errors:s,warnings:n};return i.length>0&&this._emitter.emit("register",{actions:r,result:c,registered:i}),c}unregister(e){let i=this._registry.delete(e);i&&this._registeredIds.delete(e);let s=e+"/",n=[];for(let r of this._registry.keys())r.startsWith(s)&&(this._registry.delete(r),this._registeredIds.delete(r),n.push(r));return i&&this._emitter.emit("unregister",{id:e,cascaded:n}),i}invoke(e,i,s){if(this._isAsync)throw new Error("Cannot call invoke() with async hooks. Use invokeAsync() instead.");let n=s!==void 0?s:this._requireCtx(),r=core.createRootFrame(n,this._limits);return this._invokeInternal(e,i,r)}async invokeAsync(e,i,s){let n=s!==void 0?s:this._requireCtx(),r=core.createRootFrame(n,this._limits);return this._invokeInternalAsync(e,i,r)}setContext(e){this._ctx=e;}context(e,i){let s=this._ctx;this._ctx=e;try{return i()}finally{this._ctx=s;}}beginBatch(){this._batchDepth++,this._batchDepth===1&&(this._batchErrors=[],this._batchWarnings=[],this._batchActions=[],this._batchRegistered=[]);}endBatch(){if(this._batchDepth<=0)throw new Error("endBatch() called without matching beginBatch()");if(this._batchDepth--,this._batchDepth>0)return {registered:[],errors:[],warnings:[]};let e={registered:this._batchRegistered,errors:this._batchErrors,warnings:this._batchWarnings};return this._batchRegistered.length>0&&this._emitter.emit("register",{actions:this._batchActions,result:e,registered:this._batchRegistered}),e}on(e,i){return this._emitter.on(e,i)}get handlerDefinitions(){return this._definitions}get registeredIds(){return this._registeredIds}getActionDefinition(e){return this._registry.get(e)?.definition}get typeField(){return this._typeField}get directivePermissions(){return this._directivePermissions}get isAsync(){return this._isAsync}get compilationMode(){return this._mode}get directiveHookSlots(){return this._directiveAnalysis.filledNames}get asyncSlots(){return this._directiveAnalysis.asyncNames}compile(){if(this._requestedMode!=="interpret"){this._mode!=="jit"&&this._promote();for(let e of this._registry.values())e.compiled||(e.compiled=this._compileAction(e.directives));}}_invokeInternal(e,i,s){let n=this._prepareInvoke(e,i,s);if("success"in n)return n;let{stored:r,childFrame:c,childScope:f}=n;if(this._beforeAction!==null){let h=this._beforeAction(e,i,c);if(h?.skip)return z(h.data,c.counters)}let _;if(r.compiled?_=r.compiled.fn(c,f,r.compiled.$):(_=this._directiveRunner(r.directives,c),this._maybeAutoPromote(r)),this._afterAction!==null){let h=this._afterAction(e,i,_,c);h!==void 0&&(_=h);}return _}async _invokeInternalAsync(e,i,s){let n=this._prepareInvoke(e,i,s);if("success"in n)return n;let{stored:r,childFrame:c,childScope:f}=n;if(this._beforeAction!==null){let h=this._beforeAction(e,i,c);if(h?.skip)return z(h.data,c.counters)}let _;if(r.compiled?_=await r.compiled.fn(c,f,r.compiled.$):(_=await this._directiveRunner(r.directives,c),this._maybeAutoPromote(r)),this._afterAction!==null){let h=this._afterAction(e,i,_,c);h!==void 0&&(_=h);}return _}_prepareInvoke(e,i,s){let n=this._registry.get(e);if(!n)return U(e,s.counters);if(e.includes("/")&&!this._isAccessible(e,s))return Y(e,s.counters);if(s.depth>=s.limits.maxDepth)return G(e,s.limits.maxDepth,s.counters);let r=Object.create(s.scope);i&&Object.assign(r,i);let c=s.child("action:"+e,0,e).withScope(r);return {stored:n,childFrame:c,childScope:r}}_maybeAutoPromote(e){this._requestedMode==="auto"&&++e.invokeCount>=this._threshold&&(e.compiled=this._compileAction(e.directives),this._mode!=="jit"&&this._promote());}_executeDirectives(e,i){return this._directiveExecutor(e,i,this._handlers,this._directiveHooks,this._typeField,this._engineRef,this._limits.maxDirectives)}async _executeDirectivesAsync(e,i){return this._directiveExecutor(e,i,this._handlers,this._directiveHooks,this._typeField,this._engineRef,this._limits.maxDirectives)}_compileAction(e){let{fn:i}=O(e,this._directiveAnalysis,this._typeField),s={d:e,h:this._handlers,hooks:this._directiveHooks,engine:this._engineRef,runner:this._directiveRunner};return {fn:i,$:s}}_isAccessible(e,i){let n="action:"+e.slice(0,e.lastIndexOf("/"));return i.path.includes(n)}_requireCtx(){if(this._ctx===void 0)throw new Error("No context set. Pass ctx to invoke(id, params, ctx), call setContext(ctx), or use context(ctx, fn).");return this._ctx}_promote(){this._directiveExecutor=this._buildJitDirectiveExecutor(),this._mode="jit";}_buildJitDirectiveExecutor(){let{fn:e}=j(this._directiveAnalysis);return e}};function fe(t){return new q(t)}exports.RESERVED_TYPES=I;exports.SimpleEmitter=H;exports.buildActionExecutor=O;exports.buildDirectiveExecutor=j;exports.createActionEngine=fe;exports.createDirectiveInterpreter=B;exports.normalizeDirectives=S;
1
+ 'use strict';var core=require('@statedelta-actions/core');function Ae(s,e){let r;for(;;){let i=s.next(r);if(i.done)return i.value;r=e?e(i.value):void 0;}}async function Re(s,e){let r;for(;;){let i=await s.next(r);if(i.done)return i.value;r=e?await e(i.value):void 0;}}function Ie(s){let e=0;return ()=>{if(!(e>=s.length))return s[e++]}}var j=class{_listeners=new Map;on(e,r){let i=this._listeners.get(e);i||(i=new Set,this._listeners.set(e,i)),i.add(r);let o=false;return ()=>{o||(o=true,i.delete(r),i.size===0&&this._listeners.delete(e));}}once(e,r){let i=this.on(e,o=>{i(),r(o);});return i}emit(e,r){let i=this._listeners.get(e);if(!i||i.size===0)return;let o=[...i];for(let t=0;t<o.length;t++)try{o[t](r);}catch(n){console.error(`[SimpleEmitter] Listener error on "${e}":`,n);}}listenerCount(e){return this._listeners.get(e)?.size??0}hasListeners(e){let r=this._listeners.get(e);return r!==void 0&&r.size>0}off(e){this._listeners.delete(e);}removeAllListeners(){this._listeners.clear();}};var L=new Set(["const","let","return","throw","pause","if"]);function ue(s,e,r,i){let o=[];return X(s.directives,e,r,i,"directive",o,s.id),o}function X(s,e,r,i,o,t,n){for(let a=0;a<s.length;a++){let d=s[a],f=d[i],x=`${o}[${a}]`;if(!f){t.push(`${x}: missing type field "${i}"`);continue}if(f==="if"){let l=d.cond;typeof l!="function"&&typeof l!="boolean"&&t.push(`${x}: "if" requires cond as function or boolean`);let _=d.then;Array.isArray(_)?X(_,e,r,i,`${x}.then`,t,n):t.push(`${x}: "if" requires then as array`);let m=d.else;m!==void 0&&(Array.isArray(m)?X(m,e,r,i,`${x}.else`,t,n):t.push(`${x}.else: must be array`));continue}if(L.has(f))continue;if(!e[f])throw new Error(`Action "${n}" ${x}: no handler registered for type "${f}". Register the handler in createActionEngine({ handlers }) before registering this action.`);let C=r.get(f);if(C?.validate)try{let l=C.validate(d);l&&!l.valid&&t.push(`${x}: ${l.error??"validation failed"}`);}catch(l){t.push(`${x}: validate threw: ${l}`);}let y=d.catch;if(y&&Array.isArray(y))for(let l=0;l<y.length;l++){let m=y[l][i];if(!(m&&L.has(m))&&m&&!e[m])throw new Error(`Action "${n}" ${x}.catch[${l}]: no handler registered for type "${m}". Register the handler in createActionEngine({ handlers }) before registering this action.`)}}}function N(s,e){let r=s.length,i=new Array(r);for(let o=0;o<r;o++){let t=s[o],n=t[e],a=t,d=false,f=t.catch;if(Array.isArray(f)&&f.length>0&&(a={...a,catch:N(f,e)},d=true),n==="if"){let x=t.then;Array.isArray(x)&&x.length>0&&(a={...a,then:N(x,e)},d=true);let C=t.else;Array.isArray(C)&&C.length>0&&(a={...a,else:N(C,e)},d=true);}i[o]=d?a:t;}return i}function z(s,e,r,i,o,t,n){return {success:false,aborted:true,abortedBy:s,appliedCount:e,skippedCount:r,errors:i,processedCount:o,totalCount:t,counters:n}}function ke(s){let e=s.filledNames.has("beforeDirective"),r=s.filledNames.has("afterDirective"),i=s.filledNames.has("onDirectivesComplete");return function(t,n,a,d,f,x,C){let y=Math.min(t.length,C),l=[],_=0,m=0,{counters:p,scope:A}=n,$=n.ctx,w=[{dirs:t,i:0,len:y,topLevelIfIndex:-1}],k=0;for(;w.length>0;){let R=w[w.length-1];if(R.i>=R.len){w.pop();continue}let T=R.i++,S=R.topLevelIfIndex!==-1,b=S?R.topLevelIfIndex:T;S||(k=T+1);let h=R.dirs[T],E=h[f];if(E==="const"||E==="let"){let c=h.name;if(typeof h.resolve=="function")try{A[c]=h.resolve($,A).value;}catch(u){l.push({directiveIndex:b,error:`Binding resolve: ${u}`}),p.errors++;}else A[c]=h.value;continue}if(E==="return"){let c=k;if(typeof h.resolve=="function")try{let u=h.resolve($,A),v="value"in u?u.value:"return"in u?u.return:h.value;return {success:!0,aborted:!1,appliedCount:_,skippedCount:m,errors:l,processedCount:c,totalCount:y,counters:p,data:v}}catch(u){l.push({directiveIndex:b,error:`Control resolve: ${u}`}),p.errors++;}else return {success:true,aborted:false,appliedCount:_,skippedCount:m,errors:l,processedCount:c,totalCount:y,counters:p,data:h.value};continue}if(E==="throw"){let c=k;if(typeof h.resolve=="function")try{let u=h.resolve($,A),v="message"in u?u.message:"throw"in u?u.throw:h.message;return {success:!1,aborted:!0,abortedBy:"throw",appliedCount:_,skippedCount:m,errors:l,processedCount:c,totalCount:y,counters:p,data:v}}catch(u){l.push({directiveIndex:b,error:`Control resolve: ${u}`}),p.errors++;}else return {success:false,aborted:true,abortedBy:"throw",appliedCount:_,skippedCount:m,errors:l,processedCount:c,totalCount:y,counters:p,data:h.message};continue}if(E==="if"){let c;try{let v=h.cond;c=typeof v=="function"?!!v($,A):!!v;}catch(v){l.push({directiveIndex:b,error:`if cond: ${v}`}),p.errors++;continue}let u=c?h.then:h.else;if(Array.isArray(u)&&u.length>0){let v=Math.min(u.length,C),G=S?R.topLevelIfIndex:T;w.push({dirs:u,i:0,len:v,topLevelIfIndex:G});}continue}let g=h,I=n;if(e)try{let c=core.processIntercept(d.beforeDirective(g,n),"beforeDirective");if(c.action===core.Intercept.SKIP){m++,p.directivesSkipped++;continue}if(c.action===core.Intercept.ABORT)return z(c.abortReason,_,m,l,k===0?T:k-1,y,p);c.ctx!==void 0&&(I=n.withCtx(c.ctx)),c.directive!==void 0&&(g=c.directive);}catch(c){l.push({directiveIndex:b,error:`Hook beforeDirective: ${c}`}),p.errors++;}if(typeof g.resolve=="function")try{let c=g.resolve(I.ctx,I.scope);g={...g,...c};}catch(c){l.push({directiveIndex:b,error:`Directive resolve: ${c}`}),p.errors++;continue}let F=g[f],M=F?a[F]:void 0;if(!M){l.push({directiveIndex:b,error:`No handler for directive type: ${F??"undefined"}`}),p.errors++;continue}let D;try{D=M(g,I,x);}catch(c){D={ok:false,error:String(c)};}let H=k;if(D.ok){if(_++,p.directivesApplied++,typeof g.as=="string"&&(A[g.as]=D.data),D.halt)return {success:true,aborted:true,abortedBy:"halt",appliedCount:_,skippedCount:m,errors:l,processedCount:H,totalCount:y,counters:p,data:D.data}}else {if(D.halt)return {success:false,aborted:true,abortedBy:"halt",appliedCount:_,skippedCount:m,errors:l,processedCount:H,totalCount:y,counters:p,data:D.data};let c=g.catch;if(Array.isArray(c)&&c.length>0){A.$exception=D.error??"handler failed";let u=x.runDirectives(c,I);_+=u.appliedCount;for(let v=0;v<u.errors.length;v++)l.push(u.errors[v]);if(u.aborted)return {success:u.success,aborted:true,abortedBy:u.abortedBy,appliedCount:_,skippedCount:m,errors:l,processedCount:H,totalCount:y,counters:p,data:u.data}}else l.push({directiveIndex:b,error:D.error??"handler failed"}),p.errors++;}if(r)try{if(d.afterDirective(g,D,I)==="abort")return z("afterDirective",_,m,l,H,y,p)}catch(c){l.push({directiveIndex:b,error:`Hook afterDirective: ${c}`}),p.errors++;}}let P={success:true,aborted:false,appliedCount:_,skippedCount:m,errors:l,processedCount:y,totalCount:y,counters:p};if(i)try{d.onDirectivesComplete(P);}catch{}return P}}function Ee(s){let e=s.filledNames.has("beforeDirective"),r=s.filledNames.has("afterDirective"),i=s.filledNames.has("onDirectivesComplete");return async function(t,n,a,d,f,x,C){let y=Math.min(t.length,C),l=[],_=0,m=0,{counters:p,scope:A}=n,$=n.ctx,w=[{dirs:t,i:0,len:y,topLevelIfIndex:-1}],k=0;for(;w.length>0;){let R=w[w.length-1];if(R.i>=R.len){w.pop();continue}let T=R.i++,S=R.topLevelIfIndex!==-1,b=S?R.topLevelIfIndex:T;S||(k=T+1);let h=R.dirs[T],E=h[f];if(E==="const"||E==="let"){let c=h.name;if(typeof h.resolve=="function")try{A[c]=h.resolve($,A).value;}catch(u){l.push({directiveIndex:b,error:`Binding resolve: ${u}`}),p.errors++;}else A[c]=h.value;continue}if(E==="return"){let c=k;if(typeof h.resolve=="function")try{let u=h.resolve($,A),v="value"in u?u.value:"return"in u?u.return:h.value;return {success:!0,aborted:!1,appliedCount:_,skippedCount:m,errors:l,processedCount:c,totalCount:y,counters:p,data:v}}catch(u){l.push({directiveIndex:b,error:`Control resolve: ${u}`}),p.errors++;}else return {success:true,aborted:false,appliedCount:_,skippedCount:m,errors:l,processedCount:c,totalCount:y,counters:p,data:h.value};continue}if(E==="throw"){let c=k;if(typeof h.resolve=="function")try{let u=h.resolve($,A),v="message"in u?u.message:"throw"in u?u.throw:h.message;return {success:!1,aborted:!0,abortedBy:"throw",appliedCount:_,skippedCount:m,errors:l,processedCount:c,totalCount:y,counters:p,data:v}}catch(u){l.push({directiveIndex:b,error:`Control resolve: ${u}`}),p.errors++;}else return {success:false,aborted:true,abortedBy:"throw",appliedCount:_,skippedCount:m,errors:l,processedCount:c,totalCount:y,counters:p,data:h.message};continue}if(E==="if"){let c;try{let v=h.cond;c=typeof v=="function"?!!v($,A):!!v;}catch(v){l.push({directiveIndex:b,error:`if cond: ${v}`}),p.errors++;continue}let u=c?h.then:h.else;if(Array.isArray(u)&&u.length>0){let v=Math.min(u.length,C),G=S?R.topLevelIfIndex:T;w.push({dirs:u,i:0,len:v,topLevelIfIndex:G});}continue}let g=h,I=n;if(e)try{let c=core.processIntercept(await d.beforeDirective(g,n),"beforeDirective");if(c.action===core.Intercept.SKIP){m++,p.directivesSkipped++;continue}if(c.action===core.Intercept.ABORT)return z(c.abortReason,_,m,l,k===0?T:k-1,y,p);c.ctx!==void 0&&(I=n.withCtx(c.ctx)),c.directive!==void 0&&(g=c.directive);}catch(c){l.push({directiveIndex:b,error:`Hook beforeDirective: ${c}`}),p.errors++;}if(typeof g.resolve=="function")try{let c=g.resolve(I.ctx,I.scope);g={...g,...c};}catch(c){l.push({directiveIndex:b,error:`Directive resolve: ${c}`}),p.errors++;continue}let F=g[f],M=F?a[F]:void 0;if(!M){l.push({directiveIndex:b,error:`No handler for directive type: ${F??"undefined"}`}),p.errors++;continue}let D;try{D=await M(g,I,x);}catch(c){D={ok:false,error:String(c)};}let H=k;if(D.ok){if(_++,p.directivesApplied++,typeof g.as=="string"&&(A[g.as]=D.data),D.halt)return {success:true,aborted:true,abortedBy:"halt",appliedCount:_,skippedCount:m,errors:l,processedCount:H,totalCount:y,counters:p,data:D.data}}else {if(D.halt)return {success:false,aborted:true,abortedBy:"halt",appliedCount:_,skippedCount:m,errors:l,processedCount:H,totalCount:y,counters:p,data:D.data};let c=g.catch;if(Array.isArray(c)&&c.length>0){A.$exception=D.error??"handler failed";let u=await x.runDirectivesAsync(c,I);_+=u.appliedCount;for(let v=0;v<u.errors.length;v++)l.push(u.errors[v]);if(u.aborted)return {success:u.success,aborted:true,abortedBy:u.abortedBy,appliedCount:_,skippedCount:m,errors:l,processedCount:H,totalCount:y,counters:p,data:u.data}}else l.push({directiveIndex:b,error:D.error??"handler failed"}),p.errors++;}if(r)try{if(await d.afterDirective(g,D,I)==="abort")return z("afterDirective",_,m,l,H,y,p)}catch(c){l.push({directiveIndex:b,error:`Hook afterDirective: ${c}`}),p.errors++;}}let P={success:true,aborted:false,appliedCount:_,skippedCount:m,errors:l,processedCount:y,totalCount:y,counters:p};if(i)try{await d.onDirectivesComplete(P);}catch{}return P}}function U(s,e){return e?Ee(s):ke(s)}function B(s,e,r,i,o,t,n,a){return {success:false,aborted:true,abortedBy:s,appliedCount:e,skippedCount:r,errors:i,processedCount:o,totalCount:t,counters:n,data:a}}function fe(s){if(s===null||typeof s!="object")return null;if(typeof s.next=="function")return s;let e=s.iterator;return e!=null&&typeof e=="object"&&typeof e.next=="function"?e:null}function we(s){let e=s.filledNames.has("beforeDirective"),r=s.filledNames.has("afterDirective"),i=s.filledNames.has("onDirectivesComplete");return function*(t,n,a,d,f,x,C){let y=Math.min(t.length,C),l=[],_=0,m=0,{counters:p,scope:A}=n,$=n.ctx,w=[{dirs:t,i:0,len:y,topLevelIfIndex:-1}],k=0;for(;w.length>0;){let R=w[w.length-1];if(R.i>=R.len){w.pop();continue}let T=R.i++,S=R.topLevelIfIndex!==-1,b=S?R.topLevelIfIndex:T;S||(k=T+1);let h=R.dirs[T],E=h[f];if(E==="const"||E==="let"){let c=h.name;if(typeof h.resolve=="function")try{A[c]=h.resolve($,A).value;}catch(u){l.push({directiveIndex:b,error:`Binding resolve: ${u}`}),p.errors++;}else A[c]=h.value;continue}if(E==="return"){let c=k,u=h.value;if(typeof h.resolve=="function")try{let v=h.resolve($,A);u="value"in v?v.value:"return"in v?v.return:u;}catch(v){l.push({directiveIndex:b,error:`Control resolve: ${v}`}),p.errors++;continue}return {success:true,aborted:false,appliedCount:_,skippedCount:m,errors:l,processedCount:c,totalCount:y,counters:p,data:u}}if(E==="throw"){let c=k,u=h.message;if(typeof h.resolve=="function")try{let v=h.resolve($,A);u="message"in v?v.message:"throw"in v?v.throw:u;}catch(v){l.push({directiveIndex:b,error:`Control resolve: ${v}`}),p.errors++;continue}return B("throw",_,m,l,c,y,p,u)}if(E==="pause"){let c=k,u=yield {source:"pause",directive:h,frame:n,index:b,payload:{message:h.message}};if(u===false||u==="abort"||u==="cancel")return B("pause",_,m,l,c,y,p,u);_++,p.directivesApplied++,typeof h.as=="string"&&u!==void 0&&(A[h.as]=u);continue}if(E==="if"){let c;try{let v=h.cond;c=typeof v=="function"?!!v($,A):!!v;}catch(v){l.push({directiveIndex:b,error:`if cond: ${v}`}),p.errors++;continue}let u=c?h.then:h.else;if(Array.isArray(u)&&u.length>0){let v=Math.min(u.length,C),G=S?R.topLevelIfIndex:T;w.push({dirs:u,i:0,len:v,topLevelIfIndex:G});}continue}let g=h,I=n;if(e)try{let c=core.processIntercept(d.beforeDirective(g,n),"beforeDirective");if(c.action===core.Intercept.SKIP){m++,p.directivesSkipped++;continue}if(c.action===core.Intercept.ABORT)return B(c.abortReason,_,m,l,k===0?T:k-1,y,p);c.ctx!==void 0&&(I=n.withCtx(c.ctx)),c.directive!==void 0&&(g=c.directive);}catch(c){l.push({directiveIndex:b,error:`Hook beforeDirective: ${c}`}),p.errors++;}if(typeof g.resolve=="function")try{let c=g.resolve(I.ctx,I.scope);g={...g,...c};}catch(c){l.push({directiveIndex:b,error:`Directive resolve: ${c}`}),p.errors++;continue}let F=g[f],M=F?a[F]:void 0;if(!M){l.push({directiveIndex:b,error:`No handler for directive type: ${F??"undefined"}`}),p.errors++;continue}let D;try{let c=M(g,I,x),u=fe(c);u!==null?D=yield*u:D=c;}catch(c){D={ok:false,error:String(c)};}let H=k;if(D.ok){if(_++,p.directivesApplied++,typeof g.as=="string"&&(A[g.as]=D.data),D.halt)return {success:true,aborted:true,abortedBy:"halt",appliedCount:_,skippedCount:m,errors:l,processedCount:H,totalCount:y,counters:p,data:D.data}}else {if(D.halt)return B("halt",_,m,l,H,y,p,D.data);let c=g.catch;if(Array.isArray(c)&&c.length>0){A.$exception=D.error??"handler failed";let u=x.runDirectives(c,I);_+=u.appliedCount;for(let v=0;v<u.errors.length;v++)l.push(u.errors[v]);if(u.aborted)return {success:u.success,aborted:true,abortedBy:u.abortedBy,appliedCount:_,skippedCount:m,errors:l,processedCount:H,totalCount:y,counters:p,data:u.data}}else l.push({directiveIndex:b,error:D.error??"handler failed"}),p.errors++;}if(r)try{if(d.afterDirective(g,D,I)==="abort")return B("afterDirective",_,m,l,H,y,p)}catch(c){l.push({directiveIndex:b,error:`Hook afterDirective: ${c}`}),p.errors++;}}let P={success:true,aborted:false,appliedCount:_,skippedCount:m,errors:l,processedCount:y,totalCount:y,counters:p};if(i)try{d.onDirectivesComplete(P);}catch{}return P}}function Te(s){let e=s.filledNames.has("beforeDirective"),r=s.filledNames.has("afterDirective"),i=s.filledNames.has("onDirectivesComplete");return async function*(t,n,a,d,f,x,C){let y=Math.min(t.length,C),l=[],_=0,m=0,{counters:p,scope:A}=n,$=n.ctx,w=[{dirs:t,i:0,len:y,topLevelIfIndex:-1}],k=0;for(;w.length>0;){let R=w[w.length-1];if(R.i>=R.len){w.pop();continue}let T=R.i++,S=R.topLevelIfIndex!==-1,b=S?R.topLevelIfIndex:T;S||(k=T+1);let h=R.dirs[T],E=h[f];if(E==="const"||E==="let"){let c=h.name;if(typeof h.resolve=="function")try{A[c]=h.resolve($,A).value;}catch(u){l.push({directiveIndex:b,error:`Binding resolve: ${u}`}),p.errors++;}else A[c]=h.value;continue}if(E==="return"){let c=k,u=h.value;if(typeof h.resolve=="function")try{let v=h.resolve($,A);u="value"in v?v.value:"return"in v?v.return:u;}catch(v){l.push({directiveIndex:b,error:`Control resolve: ${v}`}),p.errors++;continue}return {success:true,aborted:false,appliedCount:_,skippedCount:m,errors:l,processedCount:c,totalCount:y,counters:p,data:u}}if(E==="throw"){let c=k,u=h.message;if(typeof h.resolve=="function")try{let v=h.resolve($,A);u="message"in v?v.message:"throw"in v?v.throw:u;}catch(v){l.push({directiveIndex:b,error:`Control resolve: ${v}`}),p.errors++;continue}return B("throw",_,m,l,c,y,p,u)}if(E==="pause"){let c=k,u=yield {source:"pause",directive:h,frame:n,index:b,payload:{message:h.message}};if(u===false||u==="abort"||u==="cancel")return B("pause",_,m,l,c,y,p,u);_++,p.directivesApplied++,typeof h.as=="string"&&u!==void 0&&(A[h.as]=u);continue}if(E==="if"){let c;try{let v=h.cond;c=typeof v=="function"?!!v($,A):!!v;}catch(v){l.push({directiveIndex:b,error:`if cond: ${v}`}),p.errors++;continue}let u=c?h.then:h.else;if(Array.isArray(u)&&u.length>0){let v=Math.min(u.length,C),G=S?R.topLevelIfIndex:T;w.push({dirs:u,i:0,len:v,topLevelIfIndex:G});}continue}let g=h,I=n;if(e)try{let c=core.processIntercept(await d.beforeDirective(g,n),"beforeDirective");if(c.action===core.Intercept.SKIP){m++,p.directivesSkipped++;continue}if(c.action===core.Intercept.ABORT)return B(c.abortReason,_,m,l,k===0?T:k-1,y,p);c.ctx!==void 0&&(I=n.withCtx(c.ctx)),c.directive!==void 0&&(g=c.directive);}catch(c){l.push({directiveIndex:b,error:`Hook beforeDirective: ${c}`}),p.errors++;}if(typeof g.resolve=="function")try{let c=g.resolve(I.ctx,I.scope);g={...g,...c};}catch(c){l.push({directiveIndex:b,error:`Directive resolve: ${c}`}),p.errors++;continue}let F=g[f],M=F?a[F]:void 0;if(!M){l.push({directiveIndex:b,error:`No handler for directive type: ${F??"undefined"}`}),p.errors++;continue}let D;try{let c=await M(g,I,x),u=fe(c);u!==null?D=yield*u:D=c;}catch(c){D={ok:false,error:String(c)};}let H=k;if(D.ok){if(_++,p.directivesApplied++,typeof g.as=="string"&&(A[g.as]=D.data),D.halt)return {success:true,aborted:true,abortedBy:"halt",appliedCount:_,skippedCount:m,errors:l,processedCount:H,totalCount:y,counters:p,data:D.data}}else {if(D.halt)return B("halt",_,m,l,H,y,p,D.data);let c=g.catch;if(Array.isArray(c)&&c.length>0){A.$exception=D.error??"handler failed";let u=await x.runDirectivesAsync(c,I);_+=u.appliedCount;for(let v=0;v<u.errors.length;v++)l.push(u.errors[v]);if(u.aborted)return {success:u.success,aborted:true,abortedBy:u.abortedBy,appliedCount:_,skippedCount:m,errors:l,processedCount:H,totalCount:y,counters:p,data:u.data}}else l.push({directiveIndex:b,error:D.error??"handler failed"}),p.errors++;}if(r)try{if(await d.afterDirective(g,D,I)==="abort")return B("afterDirective",_,m,l,H,y,p)}catch(c){l.push({directiveIndex:b,error:`Hook afterDirective: ${c}`}),p.errors++;}}let P={success:true,aborted:false,appliedCount:_,skippedCount:m,errors:l,processedCount:y,totalCount:y,counters:p};if(i)try{await d.onDirectivesComplete(P);}catch{}return P}}function Z(s,e){return e?Te(s):we(s)}function ee(s,e=s.hasAnyAsync){let{filledNames:r}=s,i=x=>r.has(x),o=x=>s.asyncNames.has(x)?"await ":"",t=e?"await ":"",n=[];for(let x of r)n.push(`const ${x} = directiveHooks.${x};`);n.push("const len = Math.min(directives.length, maxDirectives);"),n.push("const errors = []; let appliedCount = 0; let skippedCount = 0;"),n.push("const counters = frame.counters;"),n.push("for (let i = 0; i < len; i++) {"),n.push(" let directive = directives[i];"),i("beforeDirective")?(n.push(" let _df = frame;"),n.push(" try {"),n.push(` const _bd = ${o("beforeDirective")}beforeDirective(directive, frame);`),n.push(' if (_bd === "skip") { skippedCount++; counters.directivesSkipped++; continue; }'),n.push(' if (_bd === "abort") return { success: false, aborted: true, abortedBy: "beforeDirective", appliedCount, skippedCount, errors, processedCount: i, totalCount: len, counters };'),n.push(' if (typeof _bd === "object" && _bd !== null) {'),n.push(' if ("abort" in _bd) return { success: false, aborted: true, abortedBy: _bd.abort, appliedCount, skippedCount, errors, processedCount: i, totalCount: len, counters };'),n.push(' if ("ctx" in _bd) _df = frame.withCtx(_bd.ctx);'),n.push(' if ("directive" in _bd) directive = _bd.directive;'),n.push(" }"),n.push(' } catch (_e) { errors.push({ directiveIndex: i, error: "Hook beforeDirective: " + _e }); counters.errors++; }')):n.push(" const _df = frame;"),n.push(' if (typeof directive.resolve === "function") {'),n.push(" try { const _r = directive.resolve(_df.ctx); directive = Object.assign({}, directive, _r); }"),n.push(' catch (_e) { errors.push({ directiveIndex: i, error: "Directive resolve: " + _e }); counters.errors++; continue; }'),n.push(" }"),n.push(" const _type = directive[typeField];"),n.push(" const _handler = _type ? handlers[_type] : undefined;"),n.push(' if (!_handler) { errors.push({ directiveIndex: i, error: "No handler for directive type: " + (_type || "undefined") }); counters.errors++; continue; }'),n.push(" let _result;"),n.push(` try { _result = ${t}_handler(directive, _df, engine); } catch (_e) { errors.push({ directiveIndex: i, error: String(_e) }); counters.errors++; continue; }`),n.push(" if (_result.ok) { appliedCount++; counters.directivesApplied++; }"),n.push(' else { errors.push({ directiveIndex: i, error: _result.error || "handler failed" }); counters.errors++; }'),i("afterDirective")&&(n.push(" try {"),n.push(` const _ad = ${o("afterDirective")}afterDirective(directive, _result, _df);`),n.push(' if (_ad === "abort") return { success: false, aborted: true, abortedBy: "afterDirective", appliedCount, skippedCount, errors, processedCount: i + 1, totalCount: len, counters };'),n.push(' } catch (_e) { errors.push({ directiveIndex: i, error: "Hook afterDirective: " + _e }); counters.errors++; }')),n.push("}"),n.push("const _finalResult = { success: true, aborted: false, appliedCount, skippedCount, errors, processedCount: len, totalCount: len, counters };"),i("onDirectivesComplete")&&n.push(`try { ${o("onDirectivesComplete")}onDirectivesComplete(_finalResult); } catch (_e) {}`),n.push("return _finalResult;");let a=n.join(`
2
+ `),d=e?"async ":"";return {fn:new Function("directives","frame","handlers","directiveHooks","typeField","engine","maxDirectives",`"use strict";
3
+ return (${d}() => {
4
+ ${a}
5
+ })();`),isAsync:e}}function te(s,e,r){return `return{success:false,aborted:true,abortedBy:${s},appliedCount,skippedCount,errors,processedCount:${e},totalCount:${r},counters};`}function $e(s,e){return `return{success:true,aborted:true,abortedBy:"halt",appliedCount,skippedCount,errors,processedCount:${s},totalCount:${e},counters,data:_result.data};`}function Se(s,e){return `return{success:false,aborted:true,abortedBy:"halt",appliedCount,skippedCount,errors,processedCount:${s},totalCount:${e},counters,data:_result.data};`}function He(s,e,r,i){let o=JSON.stringify(e.name);typeof e.resolve=="function"?(s.push("try{"),s.push(`scope[${o}]=${r}.resolve(ctx,scope).value;`),s.push(`}catch(_e){errors.push({directiveIndex:${i},error:"Binding resolve: "+_e});counters.errors++;}`)):s.push(`scope[${o}]=${r}.value;`);}function Fe(s,e,r,i,o,t){typeof e.resolve=="function"?(s.push("try{"),s.push(`const _rv=${r}.resolve(ctx,scope);`),s.push(`return{success:true,aborted:false,appliedCount,skippedCount,errors,processedCount:${o},totalCount:${t},counters,data:"value" in _rv?_rv.value:"return" in _rv?_rv.return:${r}.value};`),s.push(`}catch(_e){errors.push({directiveIndex:${i},error:"Control resolve: "+_e});counters.errors++;}`)):s.push(`return{success:true,aborted:false,appliedCount,skippedCount,errors,processedCount:${o},totalCount:${t},counters,data:${r}.value};`);}function Pe(s,e,r,i,o,t){typeof e.resolve=="function"?(s.push("try{"),s.push(`const _rv=${r}.resolve(ctx,scope);`),s.push(`return{success:false,aborted:true,abortedBy:"throw",appliedCount,skippedCount,errors,processedCount:${o},totalCount:${t},counters,data:"message" in _rv?_rv.message:"throw" in _rv?_rv.throw:${r}.message};`),s.push(`}catch(_e){errors.push({directiveIndex:${i},error:"Control resolve: "+_e});counters.errors++;}`)):s.push(`return{success:false,aborted:true,abortedBy:"throw",appliedCount,skippedCount,errors,processedCount:${o},totalCount:${t},counters,data:${r}.message};`);}function Me(s,e,r,i,o,t){let n=typeof e.as=="string",a=n?JSON.stringify(e.as):"";s.push(`{const _ack=yield{source:"pause",directive:${r},frame,index:${i},payload:{message:${r}.message}};`),s.push(`if(_ack===false||_ack==="abort"||_ack==="cancel")return{success:false,aborted:true,abortedBy:"pause",appliedCount,skippedCount,errors,processedCount:${o},totalCount:${t},counters,data:_ack};`),s.push("appliedCount++;counters.directivesApplied++;"),n&&s.push(`if(_ack!==undefined)scope[${a}]=_ack;`),s.push("}");}function Be(s,e,r,i,o){let{L:t,total:n,typeField:a,handlerVars:d,hasBefore:f,hasAfter:x,awBefore:C,awAfter:y,awHandler:l,actionIsAsync:_,actionIsInteractive:m,interactiveHandlerSet:p}=o,A=s[a],$=d.get(A),w=typeof s.resolve=="function",k=typeof s.as=="string",P=Array.isArray(s.catch)&&s.catch.length>0,R=k?JSON.stringify(s.as):"",T=m&&p.has(A),S=o.blockCounter.n++;t.push(`_b${S}:{`);let b=f||w,h=f;b&&t.push(`let _dir=${e};`),h&&t.push("let _df=frame;"),f&&(t.push("try{"),t.push(`const _bd=${C}_hookBefore(${e},frame);`),t.push(`if(_bd==="skip"){skippedCount++;counters.directivesSkipped++;break _b${S};}`),t.push(`if(_bd==="abort")${te('"beforeDirective"',i-1,n)}`),t.push('if(typeof _bd==="object"&&_bd!==null){'),t.push(`if("abort" in _bd)${te("_bd.abort",i-1,n)}`),t.push('if("ctx" in _bd)_df=frame.withCtx(_bd.ctx);'),t.push('if("directive" in _bd)_dir=_bd.directive;'),t.push("}"),t.push(`}catch(_e){errors.push({directiveIndex:${r},error:"Hook beforeDirective: "+_e});counters.errors++;}`)),f?(t.push('if(typeof _dir.resolve==="function"){'),t.push("try{const _r=_dir.resolve(_df.ctx,scope);_dir=Object.assign({},_dir,_r);}"),t.push(`catch(_e){errors.push({directiveIndex:${r},error:"Directive resolve: "+_e});counters.errors++;break _b${S};}}`)):w&&(t.push(`try{const _r=${e}.resolve(ctx,scope);_dir=Object.assign({},${e},_r);}`),t.push(`catch(_e){errors.push({directiveIndex:${r},error:"Directive resolve: "+_e});counters.errors++;break _b${S};}`));let E=b?"_dir":e,g=h?"_df":"frame";if(t.push("let _result;"),T?(t.push(`try{_result=yield* ${$}(${E},${g},$.engine);}`),t.push("catch(_e){_result={ok:false,error:String(_e)};}")):m?(t.push(`try{_result=${l}${$}(${E},${g},$.engine);if(_result&&_result.iterator)_result=yield* _result.iterator;}`),t.push("catch(_e){_result={ok:false,error:String(_e)};}")):(t.push(`try{_result=${l}${$}(${E},${g},$.engine);}`),t.push("catch(_e){_result={ok:false,error:String(_e)};}")),t.push("if(_result.ok){"),t.push("appliedCount++;counters.directivesApplied++;"),k&&t.push(`scope[${R}]=_result.data;`),t.push(`if(_result.halt)${$e(i,n)}`),t.push("}else{"),t.push(`if(_result.halt)${Se(i,n)}`),P){t.push('scope.$exception=_result.error||"handler failed";');let I=_?"$.runner":"$.runnerSync";t.push(`const _cr=${l}${I}(${e}.catch,${g});`),t.push("appliedCount+=_cr.appliedCount;"),t.push("for(let _j=0;_j<_cr.errors.length;_j++)errors.push(_cr.errors[_j]);"),t.push(`if(_cr.aborted)return{success:_cr.success,aborted:true,abortedBy:_cr.abortedBy,appliedCount,skippedCount,errors,processedCount:${i},totalCount:${n},counters,data:_cr.data};`);}else t.push(`errors.push({directiveIndex:${r},error:_result.error||"handler failed"});counters.errors++;`);t.push("}"),x&&(t.push("try{"),t.push(`const _ad=${y}_hookAfter(${E},_result,${g});`),t.push(`if(_ad==="abort")${te('"afterDirective"',i,n)}`),t.push(`}catch(_e){errors.push({directiveIndex:${r},error:"Hook afterDirective: "+_e});counters.errors++;}`)),t.push("}");}function Ge(s,e,r,i,o,t){let{L:n}=t,a=s.cond,d=typeof a=="boolean";n.push("{"),d?n.push(`if(${a?"true":"false"}){`):(n.push("let _cond=false,_condOk=true;"),n.push(`try{const _c=${e}.cond;_cond=typeof _c==="function"?!!_c(ctx,scope):!!_c;}`),n.push(`catch(_e){errors.push({directiveIndex:${r},error:"if cond: "+_e});counters.errors++;_condOk=false;}`),n.push("if(_condOk){"),n.push("if(_cond){"));let f=s.then;Array.isArray(f)&&f.length>0&&re(f,`${e}.then`,i,o,t),n.push("}");let x=s.else;Array.isArray(x)&&x.length>0&&(n.push("else{"),re(x,`${e}.else`,i,o,t),n.push("}")),d||n.push("}"),n.push("}");}function re(s,e,r,i,o){let t=r!==-1;for(let n=0;n<s.length;n++){let a=s[n],d=`${e}[${n}]`,f=t?r:n,x=t?i:n+1;switch(a[o.typeField]){case "const":case "let":He(o.L,a,d,f);break;case "return":Fe(o.L,a,d,f,x,o.total);break;case "throw":Pe(o.L,a,d,f,x,o.total);break;case "pause":Me(o.L,a,d,f,x,o.total);break;case "if":{let y=t?r:n,l=t?i:n+1;Ge(a,d,f,y,l,o);break}default:Be(a,d,f,x,o);break}}}function ne(s,e,r){for(let i of s){let o=i[e];if(o&&!L.has(o)&&r.add(o),o==="if"){let t=i.then;Array.isArray(t)&&t.length>0&&ne(t,e,r);let n=i.else;Array.isArray(n)&&n.length>0&&ne(n,e,r);}}}var pe=new Set;function ie(s,e,r,i=pe,o,t=false,n=pe){let a=[],d=s.length,{filledNames:f,hasAnyAsync:x,asyncNames:C}=e,y=f.has("beforeDirective"),l=f.has("afterDirective"),_=f.has("onDirectivesComplete"),m=o!==void 0?o:V(s,i,r),p=x||m,A=p?"await ":"",$=C.has("beforeDirective")?"await ":"",w=C.has("afterDirective")?"await ":"",k=C.has("onDirectivesComplete")?"await ":"",P=new Set;ne(s,r,P);let R=new Map,T=0;for(let I of P)R.set(I,`_h${T++}`);a.push("const counters=frame.counters;"),a.push("const ctx=frame.ctx;"),a.push("const errors=[];"),a.push("let appliedCount=0;"),a.push("let skippedCount=0;");for(let[I,F]of R)a.push(`const ${F}=$.h[${JSON.stringify(I)}];`);y&&a.push("const _hookBefore=$.hooks.beforeDirective;"),l&&a.push("const _hookAfter=$.hooks.afterDirective;"),re(s,"$.d",-1,0,{L:a,total:d,typeField:r,handlerVars:R,hasBefore:y,hasAfter:l,awBefore:$,awAfter:w,awHandler:A,actionIsAsync:p,actionIsInteractive:t,interactiveHandlerSet:n,blockCounter:{n:0}}),a.push(`const _finalResult={success:true,aborted:false,appliedCount,skippedCount,errors,processedCount:${d},totalCount:${d},counters};`),_&&a.push(`try{${k}$.hooks.onDirectivesComplete(_finalResult);}catch(_e){}`),a.push("return _finalResult;");let h=`{
6
+ ${a.join(`
7
+ `)}
8
+ }`,E;return t?E=`(${p?"async function*()":"function*()"}${h})()`:E=`(${p?"async ()=>":"()=>"}${h})()`,{fn:new Function("frame","scope","$",`"use strict";
9
+ return ${E};`),isAsync:p,isInteractive:t}}function V(s,e,r){for(let i of s){let o=i[r];if(o&&e.has(o))return true;if(o==="if"){let n=i.then;if(Array.isArray(n)&&n.length>0&&V(n,e,r))return true;let a=i.else;if(Array.isArray(a)&&a.length>0&&V(a,e,r))return true}let t=i.catch;if(Array.isArray(t)&&t.length>0&&V(t,e,r))return true}return false}function se(s,e,r){return {success:false,aborted:false,appliedCount:0,skippedCount:0,processedCount:0,totalCount:0,errors:[{directiveIndex:-1,error:e}],counters:s,...r}}function ve(s,e){return se(e,`Action not found: "${s}"`,{aborted:true,abortedBy:"action-not-found"})}function he(s,e,r){return se(r,`Max depth ${e} exceeded invoking "${s}"`,{aborted:true,abortedBy:"maxDepth"})}function ye(s,e){return se(e,`Action "${s}" is private (sub-action). Can only be invoked from within its parent scope.`)}function W(s,e){return {success:true,aborted:false,appliedCount:0,skippedCount:0,processedCount:0,totalCount:0,errors:[],data:s,counters:e}}function Le(s){return typeof s=="object"&&s!==null&&"execute"in s}function xe(s){let e=Object.create(null),r=new Map;for(let i of Object.keys(s)){let o=s[i];Le(o)?(e[i]=o.execute,r.set(i,o)):(e[i]=o,r.set(i,{execute:o}));}return {handlers:e,definitions:r}}function me(s,e){if(s==="*")return true;if(!s.includes("*"))return s===e;let r=s.replace(/[.+^${}()|[\]\\]/g,"\\$&");return new RegExp("^"+r.replace(/\*/g,".*")+"$").test(e)}function _e(s){return typeof s=="string"?{pattern:s}:s}function ge(s,e,r){let i=new Map;if(!e&&!r){for(let o of s)i.set(o,{status:"available"});return i}if(e){let o=e.map(_e);for(let t of s){let n=o.some(a=>me(a.pattern,t));i.set(t,{status:n?"available":"denied"});}}else {let o=r.map(_e);for(let t of s){let n=o.find(a=>me(a.pattern,t));n?i.set(t,{status:"denied",reason:n.reason,source:n.source}):i.set(t,{status:"available"});}}return i}function Ce(s){let{typeField:e,asyncHandlerSet:r,interactiveHandlerSet:i}=s,o=new Map,t=new Set,n=new Set,a=new Set,d=new Set;return {upsert(f,x,C){o.set(f,Ne(x,e)),Y(x,r,e)?t.add(f):t.delete(f),C||Q(x,i,e)?n.add(f):n.delete(f);},remove(f){o.delete(f),t.delete(f),n.delete(f);},recompute(){a=De(o,t),d=De(o,n);},isAsync(f){return a.has(f)},isInteractive(f){return d.has(f)},has(f){return o.has(f)}}}function Ne(s,e){let r=new Set;return J(s,e,r),r}function J(s,e,r){for(let i of s){i[e]==="action"&&typeof i.id=="string"&&r.add(i.id);let o=i.catch;if(Array.isArray(o)&&o.length>0&&J(o,e,r),i[e]==="if"){let t=i.then;Array.isArray(t)&&t.length>0&&J(t,e,r);let n=i.else;Array.isArray(n)&&n.length>0&&J(n,e,r);}}}function Y(s,e,r){for(let i of s){let o=i[r];if(o&&e.has(o))return true;let t=i.catch;if(Array.isArray(t)&&t.length>0&&Y(t,e,r))return true;if(o==="if"){let n=i.then;if(Array.isArray(n)&&n.length>0&&Y(n,e,r))return true;let a=i.else;if(Array.isArray(a)&&a.length>0&&Y(a,e,r))return true}}return false}function Q(s,e,r){for(let i of s){let o=i[r];if(o==="pause"||o&&e.has(o))return true;let t=i.catch;if(Array.isArray(t)&&t.length>0&&Q(t,e,r))return true;if(o==="if"){let n=i.then;if(Array.isArray(n)&&n.length>0&&Q(n,e,r))return true;let a=i.else;if(Array.isArray(a)&&a.length>0&&Q(a,e,r))return true}}return false}function O(s,e){for(let r of s){let i=r[e];if(i==="pause")return true;let o=r.catch;if(Array.isArray(o)&&o.length>0&&O(o,e))return true;if(i==="if"){let t=r.then;if(Array.isArray(t)&&t.length>0&&O(t,e))return true;let n=r.else;if(Array.isArray(n)&&n.length>0&&O(n,e))return true}}return false}function De(s,e){let r=new Set(e),i=true;for(;i;){i=false;for(let[o,t]of s)if(!r.has(o)){for(let n of t)if(r.has(n)){r.add(o),i=true;break}}}return r}var Ue=8,qe="type",ce={maxDepth:10,maxRules:1e4,maxDirectives:1e5},ae=class{_directiveExecutor;_mode;_ctx;_requestedMode;_threshold;_limits;_typeField;_handlers;_definitions;_directiveHooks;_directiveAnalysis;_asyncHandlerSet;_interactiveHandlerSet;_isAsync;_isInteractive;_directivePermissions;_miniGraph;_interactiveExecutor;_beforeAction;_afterAction;_registry=new Map;_emitter=new j;_batchDepth=0;_batchErrors=[];_batchWarnings=[];_batchActions=[];_batchRegistered=[];_registeredIds=new Set;_directiveRunner;_directiveRunnerSync;_engineRef;constructor(e){this._requestedMode=e.mode??"auto",this._threshold=e.autoJitThreshold??Ue,this._typeField=e.typeField??qe,this._limits={maxDepth:e.limits?.maxDepth??ce.maxDepth,maxRules:e.limits?.maxRules??ce.maxRules,maxDirectives:e.limits?.maxDirectives??ce.maxDirectives};let{handlers:r,definitions:i}=xe(e.handlers);for(let a of Object.keys(e.handlers))if(L.has(a))throw new Error(`Handler type "${a}" is reserved for engine-internal directives`);if(e.allowedDirectives&&e.blockedDirectives)throw new Error("allowedDirectives and blockedDirectives are mutually exclusive. Provide one or neither.");this._handlers=r,this._definitions=i,this._directivePermissions=ge(Object.keys(r),e.allowedDirectives,e.blockedDirectives),this._directiveHooks=e.directiveHooks??{},this._beforeAction=e.actionHooks?.beforeAction??null,this._afterAction=e.actionHooks?.afterAction??null,this._directiveAnalysis=core.analyzeSlots(this._directiveHooks,core.DIRECTIVE_SLOT_NAMES);let o=new Set,t=new Set;for(let[a,d]of this._definitions)(d.async===true||core.isAsyncFunction(d.execute))&&o.add(a),(d.interactive===true||core.isGeneratorFunction(d.execute))&&t.add(a);if(this._asyncHandlerSet=o,this._interactiveHandlerSet=t,this._isInteractive=e.interactive!==void 0,!this._isInteractive&&t.size>0){let a=Array.from(t).join(", ");throw new Error(`Handler(s) [${a}] are interactive but engine has no \`interactive\` config. Add \`interactive: {}\` to enable interactive mode.`)}this._isAsync=this._directiveAnalysis.hasAnyAsync||o.size>0,this._miniGraph=Ce({typeField:this._typeField,asyncHandlerSet:o,interactiveHandlerSet:t}),this._engineRef={runDirectives:(a,d)=>this._executeDirectives(a,d),runDirectivesAsync:(a,d)=>this._executeDirectivesAsync(a,d),invoke:(a,d,f)=>this._invokeInternal(a,d,f),invokeAsync:(a,d,f)=>this._invokeInternalAsync(a,d,f),isActionAsync:a=>this._miniGraph.has(a)?this._miniGraph.isAsync(a):void 0,isActionInteractive:a=>this._miniGraph.has(a)?this._miniGraph.isInteractive(a):void 0,invokeInteractive:(a,d,f)=>this._invokeInteractiveInternal(a,d,f),evaluateRules:()=>{throw new Error("evaluateRules() not available in ActionEngine context. Use createRuleEngine().")},evaluateRulesAsync:()=>{throw new Error("evaluateRulesAsync() not available in ActionEngine context. Use createRuleEngine().")}},this._directiveRunner=(a,d)=>this._directiveExecutor(a,d,this._handlers,this._directiveHooks,this._typeField,this._engineRef,this._limits.maxDirectives);let n=U(this._directiveAnalysis,false);this._directiveRunnerSync=(a,d)=>n(a,d,this._handlers,this._directiveHooks,this._typeField,this._engineRef,this._limits.maxDirectives),this._requestedMode==="jit"?(this._directiveExecutor=this._buildJitDirectiveExecutor(),this._mode="jit"):this._isAsync?(this._directiveExecutor=U(this._directiveAnalysis,true),this._mode="interpret"):(this._directiveExecutor=n,this._mode="interpret"),this._interactiveExecutor=this._isInteractive?this._isAsync?Z(this._directiveAnalysis,true):Z(this._directiveAnalysis,false):null;}register(e){let r=[],i=[],o=[],t=[];for(let a of e){if(!a.id){i.push({actionId:"",error:"Action must have an id"});continue}if(!this._isInteractive&&O(a.directives,this._typeField)){i.push({actionId:a.id,error:'Action uses directive type:"pause" but engine has no `interactive` config. Add `interactive: {}` to enable.'});continue}let d=ue(a,this._handlers,this._definitions,this._typeField);if(d.length>0){for(let f of d)i.push({actionId:a.id,error:f});continue}t.push(a);}for(let a of t){let d=N(a.directives,this._typeField),f={definition:a,directives:d,compiled:null,invokeCount:0};this._registry.set(a.id,f),this._registeredIds.add(a.id),this._miniGraph.upsert(a.id,d,a.interactive===true),r.push(a.id);}if(this._batchDepth>0){this._batchErrors.push(...i),this._batchWarnings.push(...o);for(let a of t)this._batchActions.push(a);return this._batchRegistered.push(...r),{registered:r,errors:i,warnings:o}}t.length>0&&(this._miniGraph.recompute(),this._invalidateAndMaybeRecompile());let n={registered:r,errors:i,warnings:o};return r.length>0&&this._emitter.emit("register",{actions:t,result:n,registered:r}),n}unregister(e){let r=this._registry.delete(e);r&&(this._registeredIds.delete(e),this._miniGraph.remove(e));let i=e+"/",o=[];for(let t of this._registry.keys())t.startsWith(i)&&(this._registry.delete(t),this._registeredIds.delete(t),this._miniGraph.remove(t),o.push(t));return r&&(this._miniGraph.recompute(),this._invalidateAndMaybeRecompile(),this._emitter.emit("unregister",{id:e,cascaded:o})),r}invoke(e,r,i){if(this._miniGraph.isInteractive(e))throw new Error(`Cannot call invoke("${e}") \u2014 action is interactive (transitively). Use invokeInteractive() instead.`);if(this._miniGraph.isAsync(e))throw new Error(`Cannot call invoke("${e}") \u2014 action is async (transitively). Use invokeAsync() instead.`);if(this._directiveAnalysis.hasAnyAsync)throw new Error(`Cannot call invoke("${e}") \u2014 engine has async directive hooks. Use invokeAsync() instead.`);let o=i!==void 0?i:this._requireCtx(),t=core.createRootFrame(o,this._limits);return this._invokeInternal(e,r,t)}async invokeAsync(e,r,i){if(this._miniGraph.isInteractive(e))throw new Error(`Cannot call invokeAsync("${e}") \u2014 action is interactive (transitively). Use invokeInteractive() instead.`);let o=i!==void 0?i:this._requireCtx(),t=core.createRootFrame(o,this._limits);return this._invokeInternalAsync(e,r,t)}setContext(e){this._ctx=e;}context(e,r){let i=this._ctx;this._ctx=e;try{return r()}finally{this._ctx=i;}}beginBatch(){this._batchDepth++,this._batchDepth===1&&(this._batchErrors=[],this._batchWarnings=[],this._batchActions=[],this._batchRegistered=[]);}endBatch(){if(this._batchDepth<=0)throw new Error("endBatch() called without matching beginBatch()");if(this._batchDepth--,this._batchDepth>0)return {registered:[],errors:[],warnings:[]};let e={registered:this._batchRegistered,errors:this._batchErrors,warnings:this._batchWarnings};return this._batchRegistered.length>0&&(this._miniGraph.recompute(),this._invalidateAndMaybeRecompile()),this._batchRegistered.length>0&&this._emitter.emit("register",{actions:this._batchActions,result:e,registered:this._batchRegistered}),e}on(e,r){return this._emitter.on(e,r)}get handlerDefinitions(){return this._definitions}get registeredIds(){return this._registeredIds}getActionDefinition(e){return this._registry.get(e)?.definition}get typeField(){return this._typeField}get directivePermissions(){return this._directivePermissions}get isAsync(){return this._isAsync}get isInteractive(){return this._isInteractive}isActionAsync(e){if(this._miniGraph.has(e))return this._miniGraph.isAsync(e)}isActionInteractive(e){if(this._miniGraph.has(e))return this._miniGraph.isInteractive(e)}invokeInteractive(e,r,i){if(!this._isInteractive||this._interactiveExecutor===null)throw new Error("Cannot call invokeInteractive() \u2014 engine has no `interactive` config. Add `interactive: {}` to enable.");let o=i!==void 0?i:this._requireCtx(),t=core.createRootFrame(o,this._limits);return this._invokeInteractiveInternal(e,r,t)}_invokeInteractiveInternal(e,r,i){let o=this._prepareInvoke(e,r,i);if("success"in o)return be(o,this._isAsync);let{stored:t,childFrame:n,childScope:a}=o;if(this._beforeAction!==null){let f=this._beforeAction(e,r,n);if(f?.skip)return be(W(f.data,n.counters),this._isAsync)}let d;if(t.compiled&&t.compiled.isInteractive)d=t.compiled.fn(n,n.scope,t.compiled.$);else {let f=this._interactiveExecutor;d=f(t.directives,n,this._handlers,this._directiveHooks,this._typeField,this._engineRef,this._limits.maxDirectives);}return Ve(d,e,r,n,this._afterAction,this._isAsync)}get compilationMode(){return this._mode}get directiveHookSlots(){return this._directiveAnalysis.filledNames}get asyncSlots(){return this._directiveAnalysis.asyncNames}compile(){if(this._requestedMode!=="interpret"){this._mode!=="jit"&&this._promote();for(let[e,r]of this._registry)r.compiled||(r.compiled=this._compileAction(e,r.directives));}}_invokeInternal(e,r,i){let o=this._prepareInvoke(e,r,i);if("success"in o)return o;let{stored:t,childFrame:n,childScope:a}=o;if(this._beforeAction!==null){let f=this._beforeAction(e,r,n);if(f?.skip)return W(f.data,n.counters)}let d;if(t.compiled?d=t.compiled.fn(n,a,t.compiled.$):this._isAsync&&!this._miniGraph.isAsync(e)?(t.compiled=this._compileAction(e,t.directives),d=t.compiled.fn(n,a,t.compiled.$)):(d=this._directiveRunner(t.directives,n),this._maybeAutoPromote(t)),this._afterAction!==null){let f=this._afterAction(e,r,d,n);f!==void 0&&(d=f);}return d}async _invokeInternalAsync(e,r,i){let o=this._prepareInvoke(e,r,i);if("success"in o)return o;let{stored:t,childFrame:n,childScope:a}=o;if(this._beforeAction!==null){let f=this._beforeAction(e,r,n);if(f?.skip)return W(f.data,n.counters)}let d;if(t.compiled?d=await t.compiled.fn(n,a,t.compiled.$):(d=await this._directiveRunner(t.directives,n),this._maybeAutoPromote(t)),this._afterAction!==null){let f=this._afterAction(e,r,d,n);f!==void 0&&(d=f);}return d}_prepareInvoke(e,r,i){let o=this._registry.get(e);if(!o)return ve(e,i.counters);if(e.includes("/")&&!this._isAccessible(e,i))return ye(e,i.counters);if(i.depth>=i.limits.maxDepth)return he(e,i.limits.maxDepth,i.counters);let t=Object.create(i.scope);r&&Object.assign(t,r);let n=i.child("action:"+e,0,e).withScope(t);return {stored:o,childFrame:n,childScope:t}}_maybeAutoPromote(e){this._requestedMode==="auto"&&++e.invokeCount>=this._threshold&&(e.compiled=this._compileAction(e.definition.id,e.directives),this._mode!=="jit"&&this._promote());}_executeDirectives(e,r){return this._directiveExecutor(e,r,this._handlers,this._directiveHooks,this._typeField,this._engineRef,this._limits.maxDirectives)}async _executeDirectivesAsync(e,r){return this._directiveExecutor(e,r,this._handlers,this._directiveHooks,this._typeField,this._engineRef,this._limits.maxDirectives)}_compileAction(e,r){let i=this._miniGraph.isAsync(e),o=this._miniGraph.isInteractive(e),{fn:t,isAsync:n,isInteractive:a}=ie(r,this._directiveAnalysis,this._typeField,this._asyncHandlerSet,i,o,this._interactiveHandlerSet),d={d:r,h:this._handlers,hooks:this._directiveHooks,engine:this._engineRef,runner:this._directiveRunner,runnerSync:this._directiveRunnerSync};return {fn:t,$:d,isAsync:n,isInteractive:a}}_isAccessible(e,r){let o="action:"+e.slice(0,e.lastIndexOf("/"));return r.path.includes(o)}_requireCtx(){if(this._ctx===void 0)throw new Error("No context set. Pass ctx to invoke(id, params, ctx), call setContext(ctx), or use context(ctx, fn).");return this._ctx}_promote(){this._directiveExecutor=this._buildJitDirectiveExecutor(),this._mode="jit";}_buildJitDirectiveExecutor(){let{fn:e}=ee(this._directiveAnalysis,this._isAsync);return e}_invalidateAndMaybeRecompile(){for(let e of this._registry.values())e.compiled=null;if(this._requestedMode==="jit")for(let[e,r]of this._registry)r.compiled=this._compileAction(e,r.directives);}};function be(s,e){return e?{next:()=>Promise.resolve({value:s,done:true}),return:()=>Promise.resolve({value:s,done:true}),throw:o=>Promise.reject(o),[Symbol.asyncIterator](){return this}}:{next:()=>({value:s,done:true}),return:()=>({value:s,done:true}),throw:i=>{throw i},[Symbol.iterator](){return this}}}function Ve(s,e,r,i,o,t){if(t){let d=s;return {async next(x){let C=await d.next(x);if(C.done){let y=C.value;if(o!==null){let l=o(e,r,y,i);l!==void 0&&(y=l);}return {value:y,done:true}}return {value:C.value,done:false}},async return(){let x=await d.return(void 0),C=x.value;if(x.done&&o!==null){let y=o(e,r,C,i);y!==void 0&&(C=y);}return {value:C,done:true}},async throw(x){let C=await d.throw(x);if(C.done){let y=C.value;if(o!==null){let l=o(e,r,y,i);l!==void 0&&(y=l);}return {value:y,done:true}}return {value:C.value,done:false}},[Symbol.asyncIterator](){return this}}}let n=s;return {next(d){let f=n.next(d);if(f.done){let x=f.value;if(o!==null){let C=o(e,r,x,i);C!==void 0&&(x=C);}return {value:x,done:true}}return {value:f.value,done:false}},return(){let d=n.return(void 0),f=d.value;if(d.done&&o!==null){let x=o(e,r,f,i);x!==void 0&&(f=x);}return {value:f,done:true}},throw(d){let f=n.throw(d);if(f.done){let x=f.value;if(o!==null){let C=o(e,r,x,i);C!==void 0&&(x=C);}return {value:x,done:true}}return {value:f.value,done:false}},[Symbol.iterator](){return this}}}function We(s){return new ae(s)}exports.RESERVED_TYPES=L;exports.SimpleEmitter=j;exports.buildActionExecutor=ie;exports.buildDirectiveExecutor=ee;exports.createActionEngine=We;exports.createDirectiveInterpreter=U;exports.drainAsync=Re;exports.drainSync=Ae;exports.normalizeDirectives=N;exports.replayResponder=Ie;