@nwire/app 0.9.2 → 0.10.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.
Files changed (46) hide show
  1. package/README.md +81 -95
  2. package/dist/app.d.ts +7 -24
  3. package/dist/app.js +6 -24
  4. package/dist/compose-app.d.ts +35 -0
  5. package/dist/compose-app.js +138 -0
  6. package/dist/create-app.d.ts +38 -66
  7. package/dist/create-app.js +42 -216
  8. package/dist/define-plugin.d.ts +10 -149
  9. package/dist/define-plugin.js +11 -62
  10. package/dist/runtime/framework-hooks.d.ts +110 -0
  11. package/dist/runtime/framework-hooks.js +39 -0
  12. package/dist/runtime/index.d.ts +6 -0
  13. package/dist/runtime/index.js +6 -0
  14. package/dist/runtime/runtime.d.ts +349 -0
  15. package/dist/runtime/runtime.js +642 -0
  16. package/package.json +8 -5
  17. package/dist/__tests__/create-app.test.d.ts +0 -6
  18. package/dist/__tests__/create-app.test.d.ts.map +0 -1
  19. package/dist/__tests__/create-app.test.js +0 -126
  20. package/dist/__tests__/create-app.test.js.map +0 -1
  21. package/dist/__tests__/define-plugin.test.d.ts +0 -16
  22. package/dist/__tests__/define-plugin.test.d.ts.map +0 -1
  23. package/dist/__tests__/define-plugin.test.js +0 -269
  24. package/dist/__tests__/define-plugin.test.js.map +0 -1
  25. package/dist/__tests__/framework-events.test.d.ts +0 -18
  26. package/dist/__tests__/framework-events.test.d.ts.map +0 -1
  27. package/dist/__tests__/framework-events.test.js +0 -156
  28. package/dist/__tests__/framework-events.test.js.map +0 -1
  29. package/dist/app.d.ts.map +0 -1
  30. package/dist/app.js.map +0 -1
  31. package/dist/create-app.d.ts.map +0 -1
  32. package/dist/create-app.js.map +0 -1
  33. package/dist/define-plugin.d.ts.map +0 -1
  34. package/dist/define-plugin.js.map +0 -1
  35. package/dist/framework-event-bus.d.ts +0 -129
  36. package/dist/framework-event-bus.d.ts.map +0 -1
  37. package/dist/framework-event-bus.js +0 -188
  38. package/dist/framework-event-bus.js.map +0 -1
  39. package/dist/framework-events.d.ts +0 -233
  40. package/dist/framework-events.d.ts.map +0 -1
  41. package/dist/framework-events.js +0 -136
  42. package/dist/framework-events.js.map +0 -1
  43. package/dist/runtime.d.ts +0 -185
  44. package/dist/runtime.d.ts.map +0 -1
  45. package/dist/runtime.js +0 -197
  46. package/dist/runtime.js.map +0 -1
@@ -1,136 +0,0 @@
1
- /**
2
- * Framework events — lifecycle hooks expressed as values.
3
- *
4
- * The principle: every primitive is a value; lifecycle is events. There is
5
- * no separate "hooks" system — plugins (and apps) react to framework events
6
- * with the same `on(Event, handler)` shape they'd use for a domain event.
7
- *
8
- * Tense convention encodes dispatch semantics:
9
- *
10
- * `*-ing` → series-bail (sequential, return false to prevent, throw to fail)
11
- * `*-ed` → parallel (Promise.allSettled, fire-and-forget)
12
- *
13
- * A third mode `series` exists for the rare case where you need ordering
14
- * but no veto power — currently unused on built-in events but available to
15
- * apps that define their own framework-like events.
16
- *
17
- * ## What's in this file
18
- *
19
- * The generic infrastructure (`defineFrameworkEvent`, `FrameworkEventDefinition`)
20
- * and the app-lifecycle events (`AppRegistering` / `AppBooting` / `AppBooted` /
21
- * `AppReady` / `AppShuttingDown` / `AppShutdown`). These have no domain
22
- * coupling — they take only the app name + timing metadata.
23
- *
24
- * Action-specific events (`ActionDispatching` / `ActionCompleted` /
25
- * `ActionFailed`) and event-recording events (`EventRecording` /
26
- * `EventRecorded`) live in `@nwire/forge` because their payload types
27
- * reference forge-specific types (`ActionDefinition`, `HandlerContext`).
28
- *
29
- * Both packages re-export from each other through forge's index so that
30
- * `import { AppBooted, ActionCompleted } from "@nwire/forge"` keeps
31
- * working — most consumers don't notice the split.
32
- */
33
- /**
34
- * Declare a framework event. The mode picks dispatch semantics:
35
- *
36
- * defineFrameworkEvent<{...}>("nwire.action.dispatching", "series-bail");
37
- * defineFrameworkEvent<{...}>("nwire.action.completed", "parallel");
38
- *
39
- * App authors can declare their OWN framework events (e.g.
40
- * `defineFrameworkEvent<{ tenant: string }>("app.tenant-switched", "parallel")`)
41
- * and fire them through the same bus. Every subscriber pattern works
42
- * uniformly across built-in and user-defined events.
43
- */
44
- export function defineFrameworkEvent(name, mode) {
45
- return { $kind: "framework-event", name, mode };
46
- }
47
- // ─── App lifecycle ──────────────────────────────────────────────
48
- /**
49
- * Fired during `app.start()` right after plugin `register` has run, before
50
- * provider boot. Plugins/apps can return `false` here to short-circuit boot.
51
- */
52
- export const AppRegistering = defineFrameworkEvent("nwire.app.registering", "series-bail");
53
- /**
54
- * Fired during `app.start()` after provider boot and before plugin `boot`
55
- * callbacks. Useful for diagnostics that need the container populated but
56
- * want to run before plugins start subscribing to real traffic.
57
- */
58
- export const AppBooting = defineFrameworkEvent("nwire.app.booting", "series-bail");
59
- /**
60
- * Fired after `app.start()` completes — all providers booted, all plugins
61
- * booted, all initializers boot-phase complete. Safe to do post-boot work.
62
- */
63
- export const AppBooted = defineFrameworkEvent("nwire.app.booted", "parallel");
64
- /**
65
- * Fired after the wire is fully ready — endpoints listening, health checks
66
- * passing. This is the signal external observers (Studio, log aggregators)
67
- * use to start treating the app as live.
68
- */
69
- export const AppReady = defineFrameworkEvent("nwire.app.ready", "parallel");
70
- /**
71
- * Fired at the start of `app.stop()` — interceptable so plugins can refuse
72
- * shutdown (e.g., "still draining work, give me 5 more seconds"). Throwing
73
- * fails the shutdown; the runtime then falls back to hard-timeout SIGKILL.
74
- */
75
- export const AppShuttingDown = defineFrameworkEvent("nwire.app.shutting-down", "series-bail");
76
- /** Fired after `app.stop()` completes — everything torn down. */
77
- export const AppShutdown = defineFrameworkEvent("nwire.app.shutdown", "parallel");
78
- export const PluginRegistered = defineFrameworkEvent("nwire.plugin.registered", "parallel");
79
- /**
80
- * Fired before a plugin's `boot()` callback (object-form) or its `boot(...)`
81
- * fns registered via the closure-form setup. Interceptable — returning
82
- * `false` aborts boot of the whole app (the runtime treats a vetoed plugin
83
- * boot as a fatal startup failure, surfaced with the plugin name).
84
- */
85
- export const PluginBooting = defineFrameworkEvent("nwire.plugin.booting", "series-bail");
86
- /** Fired after a plugin's boot completes successfully. Observable. */
87
- export const PluginBooted = defineFrameworkEvent("nwire.plugin.booted", "parallel");
88
- /**
89
- * Fired before a plugin's `shutdown()` callback runs. Interceptable. Used
90
- * by observers that want to flush state before a plugin tears down.
91
- */
92
- export const PluginShuttingDown = defineFrameworkEvent("nwire.plugin.shutting-down", "series-bail");
93
- /** Fired after a plugin's shutdown completes. Observable. */
94
- export const PluginShutdown = defineFrameworkEvent("nwire.plugin.shutdown", "parallel");
95
- // ─── Wire (transport interface) lifecycle ──────────────────────────
96
- /**
97
- * Fired before an interface is attached to a host (typically an endpoint).
98
- * The payload carries the transport id + the interface's `manifest()` so
99
- * subscribers can inspect the wired surface before it goes live.
100
- * Interceptable — return `false` to refuse the mount (useful for safety
101
- * gates like "this endpoint only accepts http interfaces").
102
- */
103
- export const WireMounting = defineFrameworkEvent("nwire.wire.mounting", "series-bail");
104
- /**
105
- * Fired after an interface attaches successfully. Observable. Subscribers
106
- * use this to register routes with introspection tools, expose the
107
- * transport in Studio, etc.
108
- */
109
- export const WireMounted = defineFrameworkEvent("nwire.wire.mounted", "parallel");
110
- /**
111
- * Fired after an interface is unmounted (during shutdown or live re-mount).
112
- * Observable.
113
- */
114
- export const WireUnmounted = defineFrameworkEvent("nwire.wire.unmounted", "parallel");
115
- // ─── Catalog ───────────────────────────────────────────────────────
116
- /**
117
- * Every built-in lifecycle event. Forge augments this list with its own
118
- * action / event-recording events; the union catalog lives there.
119
- */
120
- export const builtInLifecycleEvents = [
121
- AppRegistering,
122
- AppBooting,
123
- AppBooted,
124
- AppReady,
125
- AppShuttingDown,
126
- AppShutdown,
127
- PluginRegistered,
128
- PluginBooting,
129
- PluginBooted,
130
- PluginShuttingDown,
131
- PluginShutdown,
132
- WireMounting,
133
- WireMounted,
134
- WireUnmounted,
135
- ];
136
- //# sourceMappingURL=framework-events.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"framework-events.js","source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AAa,CAAC,CAAC;AAEhD,6DAA6D;AAC7D,MAAM,CAAC,MAAM,cAAc,GAAG,oBAAoB,CAK/C,uBAAuB,EAAE,UAAU,CAAC,CAAC;AAExC,sEAAsE;AAEtE;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,oBAAoB,CAI7C,qBAAqB,EAAE,aAAa,CAAC,CAAC;AAEzC;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,oBAAoB,CAI5C,oBAAoB,EAAE,UAAU,CAAC,CAAC;AAErC;;;GAGG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,oBAAoB,CAG9C,sBAAsB,EAAE,UAAU,CAAC,CAAC;AAEvC,sEAAsE;AAEtE;;;GAGG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG;IACpC,cAAc;IACd,UAAU;IACV,SAAS;IACT,QAAQ;IACR,eAAe;IACf,WAAW;IACX,gBAAgB;IAChB,aAAa;IACb,YAAY;IACZ,kBAAkB;IAClB,cAAc;IACd,YAAY;IACZ,WAAW;IACX,aAAa;CACL,CAAC"}
package/dist/runtime.d.ts DELETED
@@ -1,185 +0,0 @@
1
- /**
2
- * Runtime — generic dispatch + observation substrate.
3
- *
4
- * The "app" half of the runtime: a managed Container, a hooks-backed
5
- * dispatch chain, a framework-event bus, and a canonical telemetry stream.
6
- * Domain engines (`@nwire/forge.Runtime`) extend this class and layer their
7
- * own registries (actors, workflows, projections, queries, event store)
8
- * on top.
9
- *
10
- * What this class owns:
11
- * - A Container (default `createContainer()`).
12
- * - The `runtime.dispatch` hook from `@nwire/hooks` — middleware substrate
13
- * for any sub-class's per-action pipeline. Subclasses register the
14
- * pinned innermost step that calls their handler; this base only owns
15
- * the hook itself, the user-middleware adapter, and the tap into the
16
- * telemetry stream.
17
- * - The `FrameworkEventBus` — lifecycle events (`AppBooting`,
18
- * `PluginRegistered`, …). Subclasses add per-action / per-actor events
19
- * by registering more event definitions.
20
- * - `onTelemetry` / `offTelemetry` / `emit` — the canonical observation
21
- * stream every consumer (dev logger, Studio Live SSE, OTel exporter)
22
- * listens on. Subclasses widen the `Telemetry` union with their own
23
- * domain kinds (e.g. `action.dispatched`, `actor.transitioned`) and
24
- * call `this.emit(record)` to push them through the same channel.
25
- */
26
- import { type Container } from "@nwire/container";
27
- import { type Hook } from "@nwire/hooks";
28
- import { type Logger } from "@nwire/logger";
29
- import { FrameworkEventBus, type FrameworkEventHandler } from "./framework-event-bus.js";
30
- import type { FrameworkEventDefinition } from "./framework-events.js";
31
- /**
32
- * Serialized form of any thrown value — `Error` instances keep `name` /
33
- * `message` / `stack` plus any own enumerable properties; non-Error throws
34
- * round-trip as `{ name: "NonError", message: String(err) }`. Used by every
35
- * telemetry record that carries an error payload.
36
- */
37
- export type SerializedError = {
38
- readonly name: string;
39
- readonly message: string;
40
- readonly stack?: string;
41
- readonly [k: string]: unknown;
42
- };
43
- export declare function serializeError(err: unknown): SerializedError;
44
- /**
45
- * `hook.step` — one record per chain/listener phase on any adopted hook.
46
- * `runId` / `parentRunId` link nested hook invocations into a causal tree
47
- * — that's what Studio's Trace page uses.
48
- */
49
- export interface HookStepTelemetry {
50
- readonly kind: "hook.step";
51
- readonly hookName: string;
52
- readonly hookId: string;
53
- readonly runId: string;
54
- readonly parentRunId?: string;
55
- readonly stepId: number;
56
- readonly stepKind: "chain" | "listener";
57
- readonly stepName?: string;
58
- readonly phase: "start" | "end" | "error";
59
- readonly durationMs?: number;
60
- readonly error?: SerializedError;
61
- readonly appName: string;
62
- readonly ts: string;
63
- }
64
- /**
65
- * `lifecycle` — one record per framework-event firing on the bus. The
66
- * runtime bridges every fire into the telemetry stream so dev logger,
67
- * Studio Live, and OTel exporter all see lifecycle activity through the
68
- * same channel as domain events. `phase: "prevented"` flags series-bail
69
- * handlers that vetoed.
70
- */
71
- export interface LifecycleTelemetry {
72
- readonly kind: "lifecycle";
73
- readonly event: string;
74
- readonly namespace: string;
75
- readonly payload: unknown;
76
- readonly phase: "fired" | "prevented";
77
- readonly appName: string;
78
- readonly ts: string;
79
- }
80
- /**
81
- * Base `Telemetry` union — generic kinds only. Subclasses (forge) WIDEN
82
- * this with their own domain kinds (`action.dispatched`, `event.published`,
83
- * `actor.transitioned`, …) and re-export the wider union as their own
84
- * `Telemetry` alias. Consumers that listen on the base accept everything;
85
- * consumers that care about discriminants narrow with `switch (rec.kind)`.
86
- */
87
- export type Telemetry = HookStepTelemetry | LifecycleTelemetry;
88
- export type TelemetryListener<T = Telemetry> = (record: T) => void;
89
- /**
90
- * Per-dispatch context passed through the `runtime.dispatch` hook. Subclass
91
- * dispatchers populate `coreFn` with their retry-loop + handler-invocation
92
- * closure; the pinned innermost chain step calls it and writes `result`.
93
- * User middlewares attached via `runtime.use()` see this ctx via adapters
94
- * that re-expose the legacy `(next, action, input, ctx)` shape.
95
- *
96
- * Types here are deliberately structural / unknown — the base runtime
97
- * doesn't know what an "action" or "context" looks like; forge tightens
98
- * the generics in its own re-export.
99
- */
100
- export interface DispatchHookCtx {
101
- readonly action: unknown;
102
- readonly input: unknown;
103
- readonly ctx: unknown;
104
- readonly coreFn: () => Promise<unknown>;
105
- result?: unknown;
106
- }
107
- /**
108
- * Onion-style extension point around action dispatch. Outermost first;
109
- * registration order is execution order (matches Koa/express semantics).
110
- * Middlewares run OUTSIDE any retry loop — one pass per dispatch.
111
- */
112
- export type DispatchMiddleware = (next: () => Promise<unknown>, action: unknown, input: unknown, ctx: unknown) => Promise<unknown>;
113
- export interface RuntimeOptions {
114
- readonly container?: Container;
115
- readonly logger?: Logger;
116
- /**
117
- * App / service name. Stamped on every telemetry record so a single
118
- * multi-tenant observer can demux records by origin.
119
- */
120
- readonly appName?: string;
121
- /**
122
- * Framework events to pre-create hooks for. The bus also lazily creates
123
- * a hook the first time `bus.on(Event, …)` or `bus.fire(Event, …)` runs
124
- * — but eager creation surfaces every known event in `listHooks()` +
125
- * scan + Studio before any subscription has happened.
126
- */
127
- readonly events?: readonly FrameworkEventDefinition<unknown>[];
128
- }
129
- export declare class Runtime {
130
- protected readonly container: Container;
131
- protected readonly logger: Logger;
132
- readonly appName: string;
133
- /**
134
- * Dispatch substrate. Subclasses register their pinned `handler` step at
135
- * priority `-Infinity` and call `dispatchHook.run(ctx)` from their own
136
- * `dispatch` method. The tap forwards every chain step to the canonical
137
- * telemetry stream.
138
- */
139
- protected readonly dispatchHook: Hook<DispatchHookCtx>;
140
- private userMiddlewareCount;
141
- readonly frameworkEvents: FrameworkEventBus;
142
- private readonly telemetryListeners;
143
- constructor(options?: RuntimeOptions);
144
- /**
145
- * Subscribe to a framework event. Sugar over `runtime.frameworkEvents.on`
146
- * so plugins can write `runtime.onFramework(ActionDispatching, ...)`.
147
- * Returns an unsubscribe function.
148
- */
149
- onFramework<TPayload>(event: FrameworkEventDefinition<TPayload>, handler: FrameworkEventHandler<TPayload>, priority?: number): () => void;
150
- /**
151
- * Register a dispatch middleware. Outermost first — the order you call
152
- * `use()` is the order layers wrap (first `use` is the outermost layer).
153
- * Middlewares run once per dispatch, outside the retry loop.
154
- *
155
- * Each user middleware gets a distinct negative priority so the chain
156
- * order matches registration order; the pinned subclass-owned "handler"
157
- * step at `-Infinity` stays strictly innermost.
158
- */
159
- use(middleware: DispatchMiddleware): void;
160
- /**
161
- * Wire a hook's per-step tap into the canonical telemetry stream. After
162
- * this call, every `.use()` / `.on()` step on the hook emits a
163
- * `kind: "hook.step"` record through `runtime.onTelemetry`, the same way
164
- * the built-in `runtime.dispatch` hook does.
165
- *
166
- * The framework calls this for every per-plugin / per-module lifecycle
167
- * hook; plugin authors can call it on their own hooks if they want their
168
- * extension points surfaced in Studio / dev-logger / OTel for free.
169
- */
170
- adoptHook<TCtx>(hook: Hook<TCtx>): void;
171
- /**
172
- * Subscribe to the canonical telemetry stream. Returns an unsubscribe.
173
- * Throwing in a listener is caught and logged; never breaks dispatch.
174
- */
175
- onTelemetry<T = Telemetry>(listener: TelemetryListener<T>): () => void;
176
- offTelemetry<T = Telemetry>(listener: TelemetryListener<T>): void;
177
- /**
178
- * Push a record onto the telemetry stream. Accepts `unknown` so
179
- * subclass-widened records (CQRS kinds in forge) don't need cast
180
- * gymnastics — listeners narrow with `switch (rec.kind)`.
181
- */
182
- protected emit(record: unknown): void;
183
- getContainer(): Container;
184
- }
185
- //# sourceMappingURL=runtime.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAmB,KAAK,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACnE,OAAO,EAAQ,KAAK,IAAI,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAc,KAAK,MAAM,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,iBAAiB,EAAE,KAAK,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AACtF,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AAEnE;;;;;GAKG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;CAC/B,CAAC;AAEF,wBAAgB,cAAc,CAAC,GAAG,EAAE,OAAO,GAAG,eAAe,CAc5D;AAED;;;;GAIG;AACH,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,MAAM,EAA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,IAAI;IAoBvC;;;OAGG;IACH,WAAW,CAAC,CAAC,GAAG,SAAS,EAAE,QAAQ,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI;IAKtE,YAAY,CAAC,CAAC,GAAG,SAAS,EAAE,QAAQ,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,IAAI;IAKjE;;;;OAIG;IACH,SAAS,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,GAAG,IAAI;IAarC,YAAY,IAAI,SAAS;CAG1B"}
package/dist/runtime.js DELETED
@@ -1,197 +0,0 @@
1
- /**
2
- * Runtime — generic dispatch + observation substrate.
3
- *
4
- * The "app" half of the runtime: a managed Container, a hooks-backed
5
- * dispatch chain, a framework-event bus, and a canonical telemetry stream.
6
- * Domain engines (`@nwire/forge.Runtime`) extend this class and layer their
7
- * own registries (actors, workflows, projections, queries, event store)
8
- * on top.
9
- *
10
- * What this class owns:
11
- * - A Container (default `createContainer()`).
12
- * - The `runtime.dispatch` hook from `@nwire/hooks` — middleware substrate
13
- * for any sub-class's per-action pipeline. Subclasses register the
14
- * pinned innermost step that calls their handler; this base only owns
15
- * the hook itself, the user-middleware adapter, and the tap into the
16
- * telemetry stream.
17
- * - The `FrameworkEventBus` — lifecycle events (`AppBooting`,
18
- * `PluginRegistered`, …). Subclasses add per-action / per-actor events
19
- * by registering more event definitions.
20
- * - `onTelemetry` / `offTelemetry` / `emit` — the canonical observation
21
- * stream every consumer (dev logger, Studio Live SSE, OTel exporter)
22
- * listens on. Subclasses widen the `Telemetry` union with their own
23
- * domain kinds (e.g. `action.dispatched`, `actor.transitioned`) and
24
- * call `this.emit(record)` to push them through the same channel.
25
- */
26
- import { createContainer } from "@nwire/container";
27
- import { hook } from "@nwire/hooks";
28
- import { NoopLogger } from "@nwire/logger";
29
- import { FrameworkEventBus } from "./framework-event-bus.js";
30
- export function serializeError(err) {
31
- if (err instanceof Error) {
32
- const out = {
33
- name: err.name,
34
- message: err.message,
35
- stack: err.stack,
36
- };
37
- for (const k of Object.keys(err)) {
38
- if (k === "name" || k === "message" || k === "stack")
39
- continue;
40
- out[k] = err[k];
41
- }
42
- return out;
43
- }
44
- return { name: "NonError", message: String(err) };
45
- }
46
- export class Runtime {
47
- container;
48
- logger;
49
- appName;
50
- /**
51
- * Dispatch substrate. Subclasses register their pinned `handler` step at
52
- * priority `-Infinity` and call `dispatchHook.run(ctx)` from their own
53
- * `dispatch` method. The tap forwards every chain step to the canonical
54
- * telemetry stream.
55
- */
56
- dispatchHook;
57
- userMiddlewareCount = 0;
58
- frameworkEvents;
59
- telemetryListeners = [];
60
- constructor(options = {}) {
61
- this.container = options.container ?? createContainer();
62
- this.logger = options.logger ?? new NoopLogger();
63
- this.appName = options.appName ?? "app";
64
- this.frameworkEvents = new FrameworkEventBus(this.logger, {
65
- adoptHook: (h) => this.adoptHook(h),
66
- events: options.events ?? [],
67
- });
68
- // Bridge framework-event firings into the telemetry stream so every
69
- // existing telemetry consumer (dev logger, Studio Live SSE, OTel
70
- // exporter) sees lifecycle activity through the same channel as
71
- // domain events. `namespace` = the second dotted segment so dev
72
- // logger / Studio can group by phase (app / plugin / wire / …).
73
- this.frameworkEvents.onFire((rec) => {
74
- const parts = rec.eventName.split(".");
75
- const namespace = parts.length >= 2 ? parts[1] : "framework";
76
- this.emit({
77
- kind: "lifecycle",
78
- event: rec.eventName,
79
- namespace,
80
- payload: rec.payload,
81
- phase: rec.phase,
82
- appName: this.appName,
83
- ts: rec.ts,
84
- });
85
- });
86
- this.dispatchHook = hook("runtime.dispatch");
87
- this.dispatchHook.tap((obs) => {
88
- this.emit({
89
- kind: "hook.step",
90
- hookName: obs.hookName,
91
- hookId: obs.hookId,
92
- runId: obs.runId,
93
- parentRunId: obs.parentRunId,
94
- stepId: obs.stepId,
95
- stepKind: obs.stepKind,
96
- stepName: obs.stepName,
97
- phase: obs.phase,
98
- durationMs: obs.durationMs,
99
- error: obs.error ? serializeError(obs.error) : undefined,
100
- appName: this.appName,
101
- ts: new Date().toISOString(),
102
- });
103
- });
104
- }
105
- /**
106
- * Subscribe to a framework event. Sugar over `runtime.frameworkEvents.on`
107
- * so plugins can write `runtime.onFramework(ActionDispatching, ...)`.
108
- * Returns an unsubscribe function.
109
- */
110
- onFramework(event, handler, priority = 0) {
111
- return this.frameworkEvents.on(event, handler, priority);
112
- }
113
- /**
114
- * Register a dispatch middleware. Outermost first — the order you call
115
- * `use()` is the order layers wrap (first `use` is the outermost layer).
116
- * Middlewares run once per dispatch, outside the retry loop.
117
- *
118
- * Each user middleware gets a distinct negative priority so the chain
119
- * order matches registration order; the pinned subclass-owned "handler"
120
- * step at `-Infinity` stays strictly innermost.
121
- */
122
- use(middleware) {
123
- const priority = -this.userMiddlewareCount;
124
- this.userMiddlewareCount += 1;
125
- this.dispatchHook.use(async (hctx, next) => {
126
- hctx.result = await middleware(async () => {
127
- await next();
128
- return hctx.result;
129
- }, hctx.action, hctx.input, hctx.ctx);
130
- }, { name: middleware.name || `middleware#${this.userMiddlewareCount}`, priority });
131
- }
132
- /**
133
- * Wire a hook's per-step tap into the canonical telemetry stream. After
134
- * this call, every `.use()` / `.on()` step on the hook emits a
135
- * `kind: "hook.step"` record through `runtime.onTelemetry`, the same way
136
- * the built-in `runtime.dispatch` hook does.
137
- *
138
- * The framework calls this for every per-plugin / per-module lifecycle
139
- * hook; plugin authors can call it on their own hooks if they want their
140
- * extension points surfaced in Studio / dev-logger / OTel for free.
141
- */
142
- adoptHook(hook) {
143
- hook.tap((obs) => {
144
- this.emit({
145
- kind: "hook.step",
146
- hookName: obs.hookName,
147
- hookId: obs.hookId,
148
- runId: obs.runId,
149
- parentRunId: obs.parentRunId,
150
- stepId: obs.stepId,
151
- stepKind: obs.stepKind,
152
- stepName: obs.stepName,
153
- phase: obs.phase,
154
- durationMs: obs.durationMs,
155
- error: obs.error ? serializeError(obs.error) : undefined,
156
- appName: this.appName,
157
- ts: new Date().toISOString(),
158
- });
159
- });
160
- }
161
- /**
162
- * Subscribe to the canonical telemetry stream. Returns an unsubscribe.
163
- * Throwing in a listener is caught and logged; never breaks dispatch.
164
- */
165
- onTelemetry(listener) {
166
- this.telemetryListeners.push(listener);
167
- return () => this.offTelemetry(listener);
168
- }
169
- offTelemetry(listener) {
170
- const i = this.telemetryListeners.indexOf(listener);
171
- if (i >= 0)
172
- this.telemetryListeners.splice(i, 1);
173
- }
174
- /**
175
- * Push a record onto the telemetry stream. Accepts `unknown` so
176
- * subclass-widened records (CQRS kinds in forge) don't need cast
177
- * gymnastics — listeners narrow with `switch (rec.kind)`.
178
- */
179
- emit(record) {
180
- if (this.telemetryListeners.length === 0)
181
- return;
182
- for (const listener of this.telemetryListeners) {
183
- try {
184
- listener(record);
185
- }
186
- catch (err) {
187
- // Best-effort — don't let a bad listener break dispatch.
188
- // eslint-disable-next-line no-console
189
- console.error("Runtime.onTelemetry listener threw:", err);
190
- }
191
- }
192
- }
193
- getContainer() {
194
- return this.container;
195
- }
196
- }
197
- //# sourceMappingURL=runtime.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,eAAe,EAAkB,MAAM,kBAAkB,CAAC;AACnE,OAAO,EAAE,IAAI,EAAa,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAe,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,iBAAiB,EAA8B,MAAM,uBAAuB,CAAC;AAgBtF,MAAM,UAAU,cAAc,CAAC,GAAY;IACzC,IAAI,GAAG,YAAY,KAAK,EAAE,CAAC;QACzB,MAAM,GAAG,GAAoB;YAC3B,IAAI,EAAE,GAAG,CAAC,IAAI;YACd,OAAO,EAAE,GAAG,CAAC,OAAO;YACpB,KAAK,EAAE,GAAG,CAAC,KAAK;SACjB,CAAC;QACF,KAAK,MAAM,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,C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