agentic-flow 2.0.1-alpha.14 → 2.0.1-alpha.16

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.
@@ -1,17 +1,19 @@
1
1
  /**
2
2
  * EmbeddingService - Unified embedding interface for agentic-flow
3
3
  *
4
- * Supports two backends:
5
- * - Simple: Hash-based, ~0.01ms, not semantic
6
- * - ONNX: Real neural embeddings via ruvector-onnx-embeddings-wasm, ~10-50ms, semantic
4
+ * Uses ruvector@0.1.61+ for ONNX embeddings with:
5
+ * - SIMD128 acceleration (6x faster)
6
+ * - Parallel worker threads (7 workers)
7
+ * - all-MiniLM-L6-v2 model (384 dimensions)
7
8
  *
8
9
  * Configure via:
9
- * - AGENTIC_FLOW_EMBEDDINGS=simple|onnx (default: simple)
10
+ * - AGENTIC_FLOW_EMBEDDINGS=simple|onnx|auto (default: auto)
10
11
  * - AGENTIC_FLOW_EMBEDDING_MODEL=all-MiniLM-L6-v2 (default)
11
12
  */
12
- export type EmbeddingBackend = 'simple' | 'onnx' | 'onnx-native';
13
+ export type EmbeddingBackend = 'simple' | 'onnx' | 'auto';
13
14
  export interface EmbeddingStats {
14
15
  backend: EmbeddingBackend;
16
+ effectiveBackend: EmbeddingBackend;
15
17
  dimension: number;
16
18
  totalEmbeddings: number;
17
19
  totalLatencyMs: number;
@@ -20,27 +22,50 @@ export interface EmbeddingStats {
20
22
  modelLoaded: boolean;
21
23
  modelName?: string;
22
24
  simdAvailable?: boolean;
25
+ parallelWorkers?: number;
26
+ }
27
+ export interface SimilarityResult {
28
+ similarity: number;
29
+ timeMs: number;
30
+ }
31
+ export interface SearchResult {
32
+ text: string;
33
+ index: number;
34
+ similarity: number;
35
+ }
36
+ export interface DuplicateGroup {
37
+ indices: number[];
38
+ texts: string[];
39
+ similarity: number;
23
40
  }
24
41
  export declare class EmbeddingService {
25
42
  private static instance;
26
43
  private backend;
44
+ private effectiveBackend;
27
45
  private dimension;
28
46
  private modelName;
29
- private embedder;
30
- private wasmModule;
31
- private loadingPromise;
32
47
  private modelLoaded;
48
+ private loadingPromise;
33
49
  private totalEmbeddings;
34
50
  private totalLatencyMs;
35
51
  private cacheHits;
36
52
  private cache;
37
53
  private cacheEnabled;
54
+ private corpus;
38
55
  private constructor();
39
56
  static getInstance(): EmbeddingService;
40
57
  /**
41
- * Get current backend
58
+ * Resolve the effective backend based on ONNX detection
59
+ */
60
+ private resolveBackend;
61
+ /**
62
+ * Get configured backend (may be 'auto')
42
63
  */
43
64
  getBackend(): EmbeddingBackend;
65
+ /**
66
+ * Get effective backend after detection
67
+ */
68
+ getEffectiveBackend(): EmbeddingBackend;
44
69
  /**
45
70
  * Get embedding dimension
46
71
  */
@@ -49,23 +74,56 @@ export declare class EmbeddingService {
49
74
  * Check if ONNX model is loaded
50
75
  */
51
76
  isModelLoaded(): boolean;
52
- /**
53
- * Initialize ONNX embedder (lazy load on first use)
54
- */
55
- initOnnx(): Promise<void>;
56
- private _loadOnnxModel;
57
77
  /**
58
78
  * Generate embedding for text
79
+ * Auto-detects ONNX and uses it if available (default behavior)
59
80
  */
60
81
  embed(text: string): Promise<Float32Array>;
61
82
  /**
62
- * Generate embeddings for multiple texts
83
+ * Generate embeddings for multiple texts (batch processing with parallel workers)
84
+ * Batch processing provides significant speedup with parallel ONNX workers
63
85
  */
64
86
  embedBatch(texts: string[]): Promise<Float32Array[]>;
65
87
  /**
66
88
  * Compute similarity between two texts
67
89
  */
68
90
  similarity(text1: string, text2: string): Promise<number>;
91
+ /**
92
+ * Compute NxN similarity matrix for a list of texts
93
+ * Uses parallel workers for ONNX backend
94
+ */
95
+ similarityMatrix(texts: string[]): Promise<number[][]>;
96
+ /**
97
+ * Build a corpus for semantic search
98
+ */
99
+ buildCorpus(texts: string[]): Promise<void>;
100
+ /**
101
+ * Semantic search against the corpus
102
+ * Returns top-k most similar texts
103
+ */
104
+ semanticSearch(query: string, topK?: number): Promise<SearchResult[]>;
105
+ /**
106
+ * Find near-duplicate texts in a list
107
+ * Groups texts with similarity above threshold
108
+ */
109
+ findDuplicates(texts: string[], threshold?: number): Promise<DuplicateGroup[]>;
110
+ /**
111
+ * K-means clustering of texts
112
+ * Returns cluster assignments and centroids
113
+ */
114
+ clusterTexts(texts: string[], k?: number, maxIterations?: number): Promise<{
115
+ clusters: number[];
116
+ centroids: Float32Array[];
117
+ }>;
118
+ /**
119
+ * Stream embeddings for large batches (memory efficient)
120
+ * Yields embeddings one at a time
121
+ */
122
+ streamEmbed(texts: string[], batchSize?: number): AsyncGenerator<{
123
+ index: number;
124
+ text: string;
125
+ embedding: Float32Array;
126
+ }>;
69
127
  /**
70
128
  * Simple hash-based embedding (fast, not semantic)
71
129
  */
@@ -82,14 +140,29 @@ export declare class EmbeddingService {
82
140
  * Clear cache
83
141
  */
84
142
  clearCache(): void;
143
+ /**
144
+ * Clear corpus
145
+ */
146
+ clearCorpus(): void;
147
+ /**
148
+ * Shutdown (cleanup workers)
149
+ */
150
+ shutdown(): Promise<void>;
85
151
  /**
86
152
  * Reset instance (for testing)
87
153
  */
88
- static reset(): void;
154
+ static reset(): Promise<void>;
89
155
  }
90
156
  export declare function getEmbeddingService(): EmbeddingService;
91
157
  export declare function embed(text: string): Promise<Float32Array>;
92
158
  export declare function embedBatch(texts: string[]): Promise<Float32Array[]>;
93
159
  export declare function textSimilarity(text1: string, text2: string): Promise<number>;
94
160
  export declare function simpleEmbed(text: string, dim?: number): Float32Array;
161
+ export declare function similarityMatrix(texts: string[]): Promise<number[][]>;
162
+ export declare function semanticSearch(query: string, topK?: number): Promise<SearchResult[]>;
163
+ export declare function findDuplicates(texts: string[], threshold?: number): Promise<DuplicateGroup[]>;
164
+ export declare function clusterTexts(texts: string[], k?: number): Promise<{
165
+ clusters: number[];
166
+ centroids: Float32Array[];
167
+ }>;
95
168
  //# sourceMappingURL=EmbeddingService.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"EmbeddingService.d.ts","sourceRoot":"","sources":["../../src/intelligence/EmbeddingService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