@czap/worker 0.1.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 (53) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +19 -0
  3. package/dist/compositor-script.d.ts +19 -0
  4. package/dist/compositor-script.d.ts.map +1 -0
  5. package/dist/compositor-script.js +374 -0
  6. package/dist/compositor-script.js.map +1 -0
  7. package/dist/compositor-startup.d.ts +200 -0
  8. package/dist/compositor-startup.d.ts.map +1 -0
  9. package/dist/compositor-startup.js +490 -0
  10. package/dist/compositor-startup.js.map +1 -0
  11. package/dist/compositor-types.d.ts +135 -0
  12. package/dist/compositor-types.d.ts.map +1 -0
  13. package/dist/compositor-types.js +7 -0
  14. package/dist/compositor-types.js.map +1 -0
  15. package/dist/compositor-worker.d.ts +65 -0
  16. package/dist/compositor-worker.d.ts.map +1 -0
  17. package/dist/compositor-worker.js +454 -0
  18. package/dist/compositor-worker.js.map +1 -0
  19. package/dist/evaluate-inline.d.ts +22 -0
  20. package/dist/evaluate-inline.d.ts.map +1 -0
  21. package/dist/evaluate-inline.js +42 -0
  22. package/dist/evaluate-inline.js.map +1 -0
  23. package/dist/host.d.ts +97 -0
  24. package/dist/host.d.ts.map +1 -0
  25. package/dist/host.js +115 -0
  26. package/dist/host.js.map +1 -0
  27. package/dist/index.d.ts +40 -0
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +40 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/messages.d.ts +254 -0
  32. package/dist/messages.d.ts.map +1 -0
  33. package/dist/messages.js +55 -0
  34. package/dist/messages.js.map +1 -0
  35. package/dist/render-worker.d.ts +77 -0
  36. package/dist/render-worker.d.ts.map +1 -0
  37. package/dist/render-worker.js +396 -0
  38. package/dist/render-worker.js.map +1 -0
  39. package/dist/spsc-ring.d.ts +171 -0
  40. package/dist/spsc-ring.d.ts.map +1 -0
  41. package/dist/spsc-ring.js +240 -0
  42. package/dist/spsc-ring.js.map +1 -0
  43. package/package.json +51 -0
  44. package/src/compositor-script.ts +374 -0
  45. package/src/compositor-startup.ts +666 -0
  46. package/src/compositor-types.ts +175 -0
  47. package/src/compositor-worker.ts +613 -0
  48. package/src/evaluate-inline.ts +42 -0
  49. package/src/host.ts +189 -0
  50. package/src/index.ts +49 -0
  51. package/src/messages.ts +343 -0
  52. package/src/render-worker.ts +454 -0
  53. package/src/spsc-ring.ts +309 -0
@@ -0,0 +1,175 @@
1
+ /**
2
+ * Type definitions for the CompositorWorker module.
3
+ *
4
+ * @module
5
+ */
6
+
7
+ import type { RuntimeCoordinator, CompositeState } from '@czap/core';
8
+ import type {
9
+ WorkerUpdate,
10
+ BootstrapQuantizerRegistration,
11
+ StartupComputePacket,
12
+ ResolvedStateEntry,
13
+ } from './messages.js';
14
+
15
+ // ---------------------------------------------------------------------------
16
+ // Types
17
+ // ---------------------------------------------------------------------------
18
+
19
+ /**
20
+ * A `CompositeState` snapshot emitted by the compositor worker, optionally
21
+ * annotated with per-quantizer generation counters. The generation map
22
+ * enables receivers to drop stale out-of-order messages.
23
+ */
24
+ export type CompositorWorkerState = CompositeState & {
25
+ readonly resolvedStateGenerations?: Record<string, number>;
26
+ };
27
+
28
+ /**
29
+ * Acknowledgement payload emitted by the worker after it applies a
30
+ * resolved-state update from the main thread.
31
+ */
32
+ export interface ResolvedStateAckPayload {
33
+ /** Generation counter the worker acknowledges. */
34
+ readonly generation: number;
35
+ /** The state transitions the worker actually observed. */
36
+ readonly states: readonly {
37
+ readonly name: string;
38
+ readonly state: string;
39
+ }[];
40
+ /** Whether non-discrete outputs (blend, CSS, etc.) changed in this round. */
41
+ readonly additionalOutputsChanged: boolean;
42
+ }
43
+
44
+ /**
45
+ * Host-facing surface of a compositor worker. Returned by
46
+ * {@link CompositorWorker} as the public control/observation API. Owns
47
+ * the underlying `Worker` -- call {@link CompositorWorkerShape.dispose}
48
+ * to terminate and release resources.
49
+ */
50
+ export interface CompositorWorkerShape {
51
+ /** The underlying Worker instance. */
52
+ readonly worker: Worker;
53
+ /** Shared runtime coordination surface reflecting host-side worker state. */
54
+ readonly runtime: RuntimeCoordinator.Shape;
55
+
56
+ /** Register a quantizer in the worker. */
57
+ addQuantizer(
58
+ name: string,
59
+ boundary: {
60
+ readonly id: string;
61
+ readonly states: readonly string[];
62
+ readonly thresholds: readonly number[];
63
+ },
64
+ ): void;
65
+
66
+ /** Remove a quantizer from the worker. */
67
+ removeQuantizer(name: string): void;
68
+
69
+ /** Evaluate a quantizer with a numeric value (threshold-based). */
70
+ evaluate(name: string, value: number): void;
71
+
72
+ /** Override blend weights for a quantizer. */
73
+ setBlendWeights(name: string, weights: Record<string, number>): void;
74
+
75
+ /** Seed resolved quantizer state into the worker without raw threshold evaluation. */
76
+ bootstrapResolvedState(states: readonly ResolvedStateEntry[]): void;
77
+
78
+ /** Mirror resolved quantizer state updates into the worker without raw threshold evaluation. */
79
+ applyResolvedState(states: readonly ResolvedStateEntry[]): void;
80
+
81
+ /** Request the worker to compute and return a CompositeState. */
82
+ requestCompute(): void;
83
+
84
+ /** Subscribe to state updates from the worker. Returns an unsubscribe function. */
85
+ onState(callback: (state: CompositorWorkerState) => void): () => void;
86
+
87
+ /** Subscribe to resolved-state acknowledgement updates. Returns an unsubscribe function. */
88
+ onResolvedStateAck(callback: (ack: ResolvedStateAckPayload) => void): () => void;
89
+
90
+ /** Subscribe to metrics updates. Returns an unsubscribe function. */
91
+ onMetrics(callback: (fps: number, budgetUsed: number) => void): () => void;
92
+
93
+ /** Terminate the worker and clean up resources. */
94
+ dispose(): void;
95
+ }
96
+
97
+ /**
98
+ * Named stages of the compositor-worker startup handshake. Used for
99
+ * structured telemetry so hosts can instrument how long each phase took.
100
+ */
101
+ export type CompositorWorkerStartupStage = 'claim-or-create' | 'coordinator-reset-or-create' | 'listener-bind';
102
+
103
+ /**
104
+ * Pluggable telemetry sink invoked during compositor-worker startup.
105
+ * `recordStage` is called once per stage with an elapsed-time sample;
106
+ * `onResolvedStateSettled` (optional) fires once the worker confirms a
107
+ * resolved-state hydration.
108
+ */
109
+ export interface CompositorWorkerStartupTelemetry {
110
+ /** Record how long a startup stage took. `durationNs` is in nanoseconds. */
111
+ recordStage(stage: CompositorWorkerStartupStage, durationNs: number): void;
112
+ /** Fired when the worker acknowledges the resolved-state bootstrap. */
113
+ onResolvedStateSettled?(states: readonly ResolvedStateEntry[]): void;
114
+ }
115
+
116
+ /**
117
+ * Finer-grained diagnostic stages emitted during startup and compute
118
+ * dispatch. Useful for pinpointing message-queue / callback latency in
119
+ * profiling builds.
120
+ */
121
+ export type CompositorWorkerStartupDiagnosticStage =
122
+ | 'coordinator-reset-or-create:runtime-reset-reuse'
123
+ | 'request-compute:packet-finalize'
124
+ | 'request-compute:dispatch-send'
125
+ | 'request-compute:post-send-bookkeeping'
126
+ | 'state-delivery:message-receipt'
127
+ | 'state-delivery:callback-queue-turn'
128
+ | 'state-delivery:host-callback-delivery';
129
+
130
+ /**
131
+ * A pre-warmed compositor worker held in a standby pool, ready to be
132
+ * claimed by a new host without paying the Worker+Blob-URL boot cost.
133
+ *
134
+ * The lease exposes both the raw `Worker` and its associated coordinator,
135
+ * plus the constructors used to mint more workers if the pool is empty.
136
+ */
137
+ export interface StandbyCompositorLease {
138
+ /** The warm Worker instance. */
139
+ readonly worker: Worker;
140
+ /** Shared runtime coordinator already attached to the worker. */
141
+ readonly runtime: RuntimeCoordinator.Shape;
142
+ /** Pooled pool capacity advertised by the lease. */
143
+ readonly capacity: number;
144
+ /** Worker constructor used to mint peers. */
145
+ readonly workerConstructor: typeof Worker;
146
+ /** Blob URL factory used to mint worker sources. */
147
+ readonly createObjectUrl: typeof URL.createObjectURL;
148
+ /** Previously-bootstrapped registrations the lease already knows about. */
149
+ readonly bootstrapSnapshot: readonly BootstrapQuantizerRegistration[];
150
+ }
151
+
152
+ /**
153
+ * Mutable scratch state used while coalescing startup messages into a
154
+ * single {@link StartupComputePacket}. Internal to the startup pipeline;
155
+ * exported for tests and host-side diagnostic inspectors.
156
+ */
157
+ export interface StartupPacketState {
158
+ /** The bootstrap mode the packet will advertise. */
159
+ bootstrapMode: StartupComputePacket['bootstrapMode'];
160
+ /** Deduplicated registrations keyed by quantizer name. */
161
+ registrations: Map<string, BootstrapQuantizerRegistration>;
162
+ /** Cached ordered registration list (invalidated when the map mutates). */
163
+ registrationList: readonly BootstrapQuantizerRegistration[] | null;
164
+ /** Cached runtime seed list derived from registrations. */
165
+ runtimeSeedList:
166
+ | readonly {
167
+ readonly name: string;
168
+ readonly states: readonly string[];
169
+ }[]
170
+ | null;
171
+ /** Pending updates to replay after bootstrap. */
172
+ updates: WorkerUpdate[];
173
+ /** Whether `runtimeSeedList` needs to be recomputed. */
174
+ runtimeSeedDirty: boolean;
175
+ }