semantic-state-estimator 1.1.1 → 2.0.0-beta.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Francisco Rueda
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -23,6 +23,17 @@ interface SemanticStateEngineConfig {
23
23
  * @param driftScore Drift magnitude: 1 − cosine_similarity ∈ [0, 2].
24
24
  */
25
25
  onDriftDetected?: (vector: number[], driftScore: number) => void;
26
+ /**
27
+ * Optional callback invoked after every successful `update`, once the new
28
+ * EMA state has been fused and listeners have been notified.
29
+ *
30
+ * Use this for observability — logging, metrics, or debugging — without
31
+ * coupling to the React/Zustand subscription model.
32
+ *
33
+ * @param snapshot The updated point-in-time snapshot.
34
+ * @param text The raw text that triggered the update.
35
+ */
36
+ onStateChange?: (snapshot: Snapshot, text: string) => void;
26
37
  /**
27
38
  * The embedding provider used to obtain embedding vectors asynchronously.
28
39
  * Any object implementing `getEmbedding(text: string): Promise<Float32Array | number[]>`
@@ -54,25 +65,26 @@ interface Snapshot {
54
65
  * SemanticStateEngine tracks the implicit semantic intent of an event stream
55
66
  * using Exponential Moving Average (EMA) vector fusion.
56
67
  *
68
+ * The computationally intensive vector operations (cosine similarity, EMA
69
+ * fusion, health calculation) are delegated to a Rust/WebAssembly core
70
+ * (`WasmStateEngine`) for better performance and type safety, while the
71
+ * public API surface remains identical.
72
+ *
57
73
  * It fires an optional drift callback when incoming embeddings diverge
58
74
  * significantly from the current state, and exposes a healthScore that
59
75
  * degrades with both age and volatility.
60
76
  */
61
77
  declare class SemanticStateEngine {
62
- private readonly alpha;
63
- private readonly driftThreshold;
64
78
  private readonly onDriftDetected?;
79
+ private readonly onStateChange?;
65
80
  private readonly provider;
66
81
  readonly modelName: string;
67
- private stateVector;
68
- private lastUpdatedAt;
69
- private lastDrift;
70
- private updateCount;
82
+ private readonly wasmEngine;
71
83
  private readonly listeners;
72
84
  constructor(config: SemanticStateEngineConfig);
73
85
  /**
74
- * Obtains an embedding for `text` from the WorkerManager and fuses it into
75
- * the rolling semantic state using EMA.
86
+ * Obtains an embedding for `text` from the provider and fuses it into
87
+ * the rolling semantic state using EMA (computed in Rust/WASM).
76
88
  *
77
89
  * On the first call the embedding establishes the baseline.
78
90
  * On subsequent calls, if the cosine similarity between the current state
@@ -92,17 +104,6 @@ declare class SemanticStateEngine {
92
104
  * Returns a point-in-time snapshot of the current semantic state.
93
105
  */
94
106
  getSnapshot(): Snapshot;
95
- /**
96
- * Computes the current healthScore.
97
- *
98
- * Starts at 1.0 and subtracts:
99
- * - An age penalty proportional to milliseconds elapsed since the last update.
100
- * - A drift penalty proportional to the most recent drift magnitude.
101
- *
102
- * The result is clamped to [0, 1].
103
- */
104
- private calculateHealth;
105
- private buildSummary;
106
107
  }
107
108
 
108
109
  export { type EmbeddingProvider as E, SemanticStateEngine as S, type SemanticStateEngineConfig as a, type Snapshot as b };
@@ -23,6 +23,17 @@ interface SemanticStateEngineConfig {
23
23
  * @param driftScore Drift magnitude: 1 − cosine_similarity ∈ [0, 2].
24
24
  */
25
25
  onDriftDetected?: (vector: number[], driftScore: number) => void;
26
+ /**
27
+ * Optional callback invoked after every successful `update`, once the new
28
+ * EMA state has been fused and listeners have been notified.
29
+ *
30
+ * Use this for observability — logging, metrics, or debugging — without
31
+ * coupling to the React/Zustand subscription model.
32
+ *
33
+ * @param snapshot The updated point-in-time snapshot.
34
+ * @param text The raw text that triggered the update.
35
+ */
36
+ onStateChange?: (snapshot: Snapshot, text: string) => void;
26
37
  /**
27
38
  * The embedding provider used to obtain embedding vectors asynchronously.
28
39
  * Any object implementing `getEmbedding(text: string): Promise<Float32Array | number[]>`
@@ -54,25 +65,26 @@ interface Snapshot {
54
65
  * SemanticStateEngine tracks the implicit semantic intent of an event stream
55
66
  * using Exponential Moving Average (EMA) vector fusion.
56
67
  *
68
+ * The computationally intensive vector operations (cosine similarity, EMA
69
+ * fusion, health calculation) are delegated to a Rust/WebAssembly core
70
+ * (`WasmStateEngine`) for better performance and type safety, while the
71
+ * public API surface remains identical.
72
+ *
57
73
  * It fires an optional drift callback when incoming embeddings diverge
58
74
  * significantly from the current state, and exposes a healthScore that
59
75
  * degrades with both age and volatility.
60
76
  */
61
77
  declare class SemanticStateEngine {
62
- private readonly alpha;
63
- private readonly driftThreshold;
64
78
  private readonly onDriftDetected?;
79
+ private readonly onStateChange?;
65
80
  private readonly provider;
66
81
  readonly modelName: string;
67
- private stateVector;
68
- private lastUpdatedAt;
69
- private lastDrift;
70
- private updateCount;
82
+ private readonly wasmEngine;
71
83
  private readonly listeners;
72
84
  constructor(config: SemanticStateEngineConfig);
73
85
  /**
74
- * Obtains an embedding for `text` from the WorkerManager and fuses it into
75
- * the rolling semantic state using EMA.
86
+ * Obtains an embedding for `text` from the provider and fuses it into
87
+ * the rolling semantic state using EMA (computed in Rust/WASM).
76
88
  *
77
89
  * On the first call the embedding establishes the baseline.
78
90
  * On subsequent calls, if the cosine similarity between the current state
@@ -92,17 +104,6 @@ declare class SemanticStateEngine {
92
104
  * Returns a point-in-time snapshot of the current semantic state.
93
105
  */
94
106
  getSnapshot(): Snapshot;
95
- /**
96
- * Computes the current healthScore.
97
- *
98
- * Starts at 1.0 and subtracts:
99
- * - An age penalty proportional to milliseconds elapsed since the last update.
100
- * - A drift penalty proportional to the most recent drift magnitude.
101
- *
102
- * The result is clamped to [0, 1].
103
- */
104
- private calculateHealth;
105
- private buildSummary;
106
107
  }
107
108
 
108
109
  export { type EmbeddingProvider as E, SemanticStateEngine as S, type SemanticStateEngineConfig as a, type Snapshot as b };