@soulcraft/brainy 0.33.0 → 0.35.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.
@@ -2,6 +2,7 @@
2
2
  * Embedding functions for converting data to vectors
3
3
  */
4
4
  import { EmbeddingFunction, EmbeddingModel, Vector } from '../coreTypes.js';
5
+ import { ModelLoadOptions } from './robustModelLoader.js';
5
6
  /**
6
7
  * TensorFlow Universal Sentence Encoder embedding model
7
8
  * This model provides high-quality text embeddings using TensorFlow.js
@@ -10,6 +11,10 @@ import { EmbeddingFunction, EmbeddingModel, Vector } from '../coreTypes.js';
10
11
  * This implementation attempts to use GPU processing when available for better performance,
11
12
  * falling back to CPU processing for compatibility across all environments.
12
13
  */
14
+ export interface UniversalSentenceEncoderOptions extends ModelLoadOptions {
15
+ /** Whether to enable verbose logging */
16
+ verbose?: boolean;
17
+ }
13
18
  export declare class UniversalSentenceEncoder implements EmbeddingModel {
14
19
  private model;
15
20
  private initialized;
@@ -17,13 +22,12 @@ export declare class UniversalSentenceEncoder implements EmbeddingModel {
17
22
  private use;
18
23
  private backend;
19
24
  private verbose;
25
+ private robustLoader;
20
26
  /**
21
27
  * Create a new UniversalSentenceEncoder instance
22
- * @param options Configuration options
28
+ * @param options Configuration options including reliability settings
23
29
  */
24
- constructor(options?: {
25
- verbose?: boolean;
26
- });
30
+ constructor(options?: UniversalSentenceEncoderOptions);
27
31
  /**
28
32
  * Add polyfills and patches for TensorFlow.js compatibility
29
33
  * This addresses issues with TensorFlow.js across all server environments
@@ -44,13 +48,10 @@ export declare class UniversalSentenceEncoder implements EmbeddingModel {
44
48
  */
45
49
  private logger;
46
50
  /**
47
- * Load the Universal Sentence Encoder model with retry logic
48
- * This helps handle network failures and JSON parsing errors from TensorFlow Hub
49
- * @param loadFunction The function to load the model
50
- * @param maxRetries Maximum number of retry attempts
51
- * @param baseDelay Base delay in milliseconds for exponential backoff
51
+ * Load the Universal Sentence Encoder model with robust retry and fallback mechanisms
52
+ * @param loadFunction The function to load the model from TensorFlow Hub
52
53
  */
53
- private loadModelWithRetry;
54
+ private loadModelFromLocal;
54
55
  /**
55
56
  * Initialize the embedding model
56
57
  */
@@ -59,13 +60,6 @@ export declare class UniversalSentenceEncoder implements EmbeddingModel {
59
60
  * Embed text into a vector using Universal Sentence Encoder
60
61
  * @param data Text to embed
61
62
  */
62
- /**
63
- * Generate a deterministic vector from a string
64
- * This is used as a fallback when the Universal Sentence Encoder is not available
65
- * @param text Input text
66
- * @returns A 512-dimensional vector derived from the text
67
- */
68
- private generateFallbackVector;
69
63
  embed(data: string | string[]): Promise<Vector>;
70
64
  /**
71
65
  * Embed multiple texts into vectors using Universal Sentence Encoder
@@ -1 +1 @@
1
- {"version":3,"file":"embedding.d.ts","sourceRoot":"","sources":["../../src/utils/embedding.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAI3E;;;;;;;GAOG;AACH,qBAAa,wBAAyB,YAAW,cAAc;IAC7D,OAAO,CAAC,KAAK,CAAY;IACzB,OAAO,CAAC,WAAW,CAAQ;IAC3B,OAAO,CAAC,EAAE,CAAY;IACtB,OAAO,CAAC,GAAG,CAAY;IACvB,OAAO,CAAC,OAAO,CAAgB;IAC/B,OAAO,CAAC,OAAO,CAAgB;IAE/B;;;OAGG;gBACS,OAAO,GAAE;QAAE,OAAO,CAAC,EAAE,OAAO,CAAA;KAAO;IAI/C;;;;;;;;OAQG;IACH,OAAO,CAAC,+BAA+B;IA4CvC;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAczB;;;OAGG;IACH,OAAO,CAAC,MAAM;IAWd;;;;;;OAMG;YACW,kBAAkB;IAkIhC;;OAEG;IACU,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAgKlC;;;OAGG;IACH;;;;;OAKG;IACH,OAAO,CAAC,sBAAsB;IA0CjB,KAAK,CAAC,IAAI,E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CACzC,OAAO,GAAE;IAAE,OAAO,CAAC,EAAE,OAAO,CAAA;CAAO,GAClC,iBAAiB,CAEnB;AAED;;;;;GAKG;AACH,eAAO,MAAM,wBAAwB,EAAE,iBACR,CAAA;AAe/B,wBAAgB,4BAA4B,CAC1C,OAAO,GAAE;IAAE,OAAO,CAAC,EAAE,OAAO,CAAA;CAAO,GAClC,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC,CAyC5C;AAED;;;;;;;GAOG;AACH,wBAAgB,gCAAgC,CAC9C,OAAO,GAAE;IAAE,OAAO,CAAC,EAAE,OAAO,CAAA;CAAO,GAClC,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC,CAE5C;AAED;;;;;GAKG;AACH,eAAO,MAAM,6BAA6B,cAV3B,MAAM,EAAE,KAAK,OAAO,CAAC,MAAM,EAAE,CAUmC,CAAA;AAE/E;;;;GAIG;AACH,wBAAgB,+BAA+B,CAC7C,KAAK,EAAE,cAAc,GACpB,iBAAiB,CAUnB"}
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Robust Model Loader - Enhanced model loading with retry mechanisms and fallbacks
3
+ *
4
+ * This module provides a more reliable way to load TensorFlow models with:
5
+ * - Exponential backoff retry mechanisms
6
+ * - Timeout handling
7
+ * - Multiple fallback strategies
8
+ * - Better error handling and logging
9
+ * - Optional local model bundling support
10
+ */
11
+ import { EmbeddingModel } from '../coreTypes.js';
12
+ export interface ModelLoadOptions {
13
+ /** Maximum number of retry attempts */
14
+ maxRetries?: number;
15
+ /** Initial retry delay in milliseconds */
16
+ initialRetryDelay?: number;
17
+ /** Maximum retry delay in milliseconds */
18
+ maxRetryDelay?: number;
19
+ /** Request timeout in milliseconds */
20
+ timeout?: number;
21
+ /** Whether to use exponential backoff */
22
+ useExponentialBackoff?: boolean;
23
+ /** Fallback model URLs to try if primary fails */
24
+ fallbackUrls?: string[];
25
+ /** Whether to enable verbose logging */
26
+ verbose?: boolean;
27
+ /** Whether to prefer local bundled model if available */
28
+ preferLocalModel?: boolean;
29
+ }
30
+ export interface RetryConfig {
31
+ attempt: number;
32
+ maxRetries: number;
33
+ delay: number;
34
+ error: Error;
35
+ }
36
+ export declare class RobustModelLoader {
37
+ private options;
38
+ private loadAttempts;
39
+ constructor(options?: ModelLoadOptions);
40
+ /**
41
+ * Load a model with robust retry and fallback mechanisms
42
+ */
43
+ loadModel(primaryLoadFunction: () => Promise<EmbeddingModel>, modelIdentifier?: string): Promise<EmbeddingModel>;
44
+ /**
45
+ * Load a model with retry logic and exponential backoff
46
+ */
47
+ private loadWithRetries;
48
+ /**
49
+ * Try to load a locally bundled model
50
+ */
51
+ private tryLoadLocalBundledModel;
52
+ /**
53
+ * Load model from a specific URL
54
+ */
55
+ private loadFromUrl;
56
+ /**
57
+ * Create a model wrapper that matches the Universal Sentence Encoder interface
58
+ */
59
+ private createModelWrapper;
60
+ /**
61
+ * Apply timeout to a promise
62
+ */
63
+ private withTimeout;
64
+ /**
65
+ * Calculate retry delay with exponential backoff
66
+ */
67
+ private calculateRetryDelay;
68
+ /**
69
+ * Sleep for specified milliseconds
70
+ */
71
+ private sleep;
72
+ /**
73
+ * Log message if verbose mode is enabled
74
+ */
75
+ private log;
76
+ /**
77
+ * Get loading statistics
78
+ */
79
+ getLoadingStats(): {
80
+ [key: string]: number;
81
+ };
82
+ /**
83
+ * Reset loading statistics
84
+ */
85
+ resetStats(): void;
86
+ }
87
+ /**
88
+ * Create a robust model loader with sensible defaults
89
+ */
90
+ export declare function createRobustModelLoader(options?: ModelLoadOptions): RobustModelLoader;
91
+ /**
92
+ * Utility function to create fallback URLs for Universal Sentence Encoder
93
+ */
94
+ export declare function getUniversalSentenceEncoderFallbacks(): string[];
@@ -0,0 +1 @@
1
+ {"version":3,"file":"robustModelLoader.d.ts","sourceRoot":"","sources":["../../src/utils/robustModelLoader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OA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KAAE;IAQ5C;;OAEG;IACH,UAAU,IAAI,IAAI;CAGnB;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,CAAC,EAAE,gBAAgB,GAAG,iBAAiB,CAErF;AAED;;GAEG;AACH,wBAAgB,oCAAoC,IAAI,MAAM,EAAE,CAM/D"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soulcraft/brainy",
3
- "version": "0.33.0",
3
+ "version": "0.35.0",
4
4
  "description": "A vector graph database using HNSW indexing with Origin Private File System storage",
5
5
  "main": "dist/unified.js",
6
6
  "module": "dist/unified.js",
@@ -54,26 +54,7 @@
54
54
  "build:browser": "BUILD_TYPE=browser rollup -c rollup.config.js",
55
55
  "start": "node dist/unified.js",
56
56
  "demo": "npm run build && npm run build:browser && npx http-server -o /demo/index.html",
57
- "release": "standard-version",
58
- "release:patch": "standard-version --release-as patch",
59
- "release:minor": "standard-version --release-as minor",
60
- "release:major": "standard-version --release-as major",
61
- "release:dry-run": "standard-version --dry-run",
62
- "github-release": "node scripts/create-github-release.js",
63
- "changelog:check": "echo 'Changelog is now automatically generated from commit messages'",
64
- "lint": "eslint --ext .ts,.js src/",
65
- "lint:fix": "eslint --ext .ts,.js src/ --fix",
66
- "format": "prettier --write \"src/**/*.{ts,js}\"",
67
- "check:format": "prettier --check \"src/**/*.{ts,js}\"",
68
- "check:style": "node scripts/check-code-style.js",
69
57
  "prepare": "npm run build",
70
- "deploy": "npm run build && npm publish",
71
- "workflow": "node scripts/release-workflow.js",
72
- "workflow:patch": "node scripts/release-workflow.js patch",
73
- "workflow:minor": "node scripts/release-workflow.js minor",
74
- "workflow:major": "node scripts/release-workflow.js major",
75
- "workflow:dry-run": "npm run build && npm test && npm run release:dry-run",
76
- "dry-run": "npm pack --dry-run",
77
58
  "test": "vitest run",
78
59
  "test:watch": "vitest",
79
60
  "test:ui": "vitest --ui",
@@ -90,7 +71,27 @@
90
71
  "test:environments": "vitest run tests/multi-environment.test.ts",
91
72
  "test:specialized": "vitest run tests/specialized-scenarios.test.ts",
92
73
  "test:performance": "vitest run tests/performance.test.ts",
93
- "test:comprehensive": "npm run test:error-handling && npm run test:edge-cases && npm run test:storage && npm run test:environments && npm run test:specialized"
74
+ "test:comprehensive": "npm run test:error-handling && npm run test:edge-cases && npm run test:storage && npm run test:environments && npm run test:specialized",
75
+ "_generate-pdf": "node dev/scripts/generate-architecture-pdf.js",
76
+ "_release": "standard-version",
77
+ "_release:patch": "standard-version --release-as patch",
78
+ "_release:minor": "standard-version --release-as minor",
79
+ "_release:major": "standard-version --release-as major",
80
+ "_release:dry-run": "standard-version --dry-run",
81
+ "_github-release": "node scripts/create-github-release.js",
82
+ "_changelog:check": "echo 'Changelog is now automatically generated from commit messages'",
83
+ "_lint": "eslint --ext .ts,.js src/",
84
+ "_lint:fix": "eslint --ext .ts,.js src/ --fix",
85
+ "_format": "prettier --write \"src/**/*.{ts,js}\"",
86
+ "_check:format": "prettier --check \"src/**/*.{ts,js}\"",
87
+ "_check:style": "node scripts/check-code-style.js",
88
+ "_deploy": "npm run build && npm publish",
89
+ "_workflow": "node scripts/release-workflow.js",
90
+ "_workflow:patch": "node scripts/release-workflow.js patch",
91
+ "_workflow:minor": "node scripts/release-workflow.js minor",
92
+ "_workflow:major": "node scripts/release-workflow.js major",
93
+ "_workflow:dry-run": "npm run build && npm test && npm run _release:dry-run",
94
+ "_dry-run": "npm pack --dry-run"
94
95
  },
95
96
  "keywords": [
96
97
  "vector-database",
@@ -151,7 +152,7 @@
151
152
  "happy-dom": "^18.0.1",
152
153
  "jsdom": "^26.1.0",
153
154
  "node-fetch": "^3.3.2",
154
- "puppeteer": "^22.5.0",
155
+ "puppeteer": "^22.15.0",
155
156
  "rollup": "^4.13.0",
156
157
  "rollup-plugin-terser": "^7.0.2",
157
158
  "standard-version": "^9.5.0",