@nwire/app 0.8.17 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,197 @@
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
@@ -0,0 +1 @@
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,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with plugin lifecycle, framework events, and DI hooks. The 'app' in the sealed architecture: composes modules + plugins, boots in order, exposes a Container, fires framework events at every lifecycle transition. Phase 69 extraction in progress; currently ships the framework events + bus.",
3
+ "version": "0.9.1",
4
+ "description": "Nwire — managed Container with plugin lifecycle, framework events, and DI hooks. Composes modules + plugins, boots in order, exposes a Container, fires framework events at every lifecycle transition.",
5
5
  "keywords": [
6
6
  "app",
7
7
  "container",
@@ -29,10 +29,10 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@nwire/container": "0.8.17",
33
- "@nwire/hooks": "0.8.17",
34
- "@nwire/logger": "0.8.17",
35
- "@nwire/messages": "0.8.17"
32
+ "@nwire/logger": "0.9.1",
33
+ "@nwire/hooks": "0.9.1",
34
+ "@nwire/container": "0.9.1",
35
+ "@nwire/messages": "0.9.1"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/node": "^22.19.9",