@soulcraft/brainy 0.9.5

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 (54) hide show
  1. package/LICENSE +21 -0
  2. package/README.demo.md +59 -0
  3. package/README.md +1257 -0
  4. package/brainy.png +0 -0
  5. package/cli-wrapper.js +56 -0
  6. package/dist/augmentationFactory.d.ts +87 -0
  7. package/dist/augmentationPipeline.d.ts +205 -0
  8. package/dist/augmentationRegistry.d.ts +48 -0
  9. package/dist/augmentationRegistryLoader.d.ts +147 -0
  10. package/dist/augmentations/conduitAugmentations.d.ts +173 -0
  11. package/dist/augmentations/memoryAugmentations.d.ts +71 -0
  12. package/dist/augmentations/serverSearchAugmentations.d.ts +168 -0
  13. package/dist/brainy.js +116929 -0
  14. package/dist/brainy.min.js +16107 -0
  15. package/dist/brainyData.d.ts +507 -0
  16. package/dist/cli.d.ts +7 -0
  17. package/dist/coreTypes.d.ts +131 -0
  18. package/dist/examples/basicUsage.d.ts +5 -0
  19. package/dist/hnsw/hnswIndex.d.ts +96 -0
  20. package/dist/hnsw/hnswIndexOptimized.d.ts +167 -0
  21. package/dist/index.d.ts +49 -0
  22. package/dist/mcp/brainyMCPAdapter.d.ts +69 -0
  23. package/dist/mcp/brainyMCPService.d.ts +99 -0
  24. package/dist/mcp/index.d.ts +14 -0
  25. package/dist/mcp/mcpAugmentationToolset.d.ts +68 -0
  26. package/dist/pipeline.d.ts +281 -0
  27. package/dist/sequentialPipeline.d.ts +114 -0
  28. package/dist/storage/fileSystemStorage.d.ts +123 -0
  29. package/dist/storage/opfsStorage.d.ts +244 -0
  30. package/dist/storage/s3CompatibleStorage.d.ts +158 -0
  31. package/dist/types/augmentations.d.ts +324 -0
  32. package/dist/types/augmentations.d.ts.map +1 -0
  33. package/dist/types/brainyDataInterface.d.ts +51 -0
  34. package/dist/types/brainyDataInterface.d.ts.map +1 -0
  35. package/dist/types/fileSystemTypes.d.ts +6 -0
  36. package/dist/types/fileSystemTypes.d.ts.map +1 -0
  37. package/dist/types/graphTypes.d.ts +134 -0
  38. package/dist/types/graphTypes.d.ts.map +1 -0
  39. package/dist/types/mcpTypes.d.ts +140 -0
  40. package/dist/types/mcpTypes.d.ts.map +1 -0
  41. package/dist/types/pipelineTypes.d.ts +27 -0
  42. package/dist/types/pipelineTypes.d.ts.map +1 -0
  43. package/dist/types/tensorflowTypes.d.ts +7 -0
  44. package/dist/types/tensorflowTypes.d.ts.map +1 -0
  45. package/dist/unified.d.ts +12 -0
  46. package/dist/unified.js +117122 -0
  47. package/dist/unified.min.js +16107 -0
  48. package/dist/utils/distance.d.ts +32 -0
  49. package/dist/utils/embedding.d.ts +55 -0
  50. package/dist/utils/environment.d.ts +28 -0
  51. package/dist/utils/index.d.ts +3 -0
  52. package/dist/utils/version.d.ts +6 -0
  53. package/dist/utils/workerUtils.d.ts +28 -0
  54. package/package.json +156 -0
@@ -0,0 +1,173 @@
1
+ import { AugmentationType, IConduitAugmentation, IWebSocketSupport, AugmentationResponse, WebSocketConnection } from '../types/augmentations.js';
2
+ /**
3
+ * Base class for conduit augmentations that provide data synchronization between Brainy instances
4
+ */
5
+ declare abstract class BaseConduitAugmentation implements IConduitAugmentation {
6
+ readonly name: string;
7
+ readonly description: string;
8
+ enabled: boolean;
9
+ protected isInitialized: boolean;
10
+ protected connections: Map<string, any>;
11
+ constructor(name: string);
12
+ initialize(): Promise<void>;
13
+ shutDown(): Promise<void>;
14
+ getStatus(): Promise<'active' | 'inactive' | 'error'>;
15
+ abstract establishConnection(targetSystemId: string, config: Record<string, unknown>): Promise<AugmentationResponse<WebSocketConnection>>;
16
+ abstract readData(query: Record<string, unknown>, options?: Record<string, unknown>): Promise<AugmentationResponse<unknown>>;
17
+ abstract writeData(data: Record<string, unknown>, options?: Record<string, unknown>): Promise<AugmentationResponse<unknown>>;
18
+ abstract monitorStream(streamId: string, callback: (data: unknown) => void): Promise<void>;
19
+ protected ensureInitialized(): Promise<void>;
20
+ }
21
+ /**
22
+ * WebSocket conduit augmentation for syncing Brainy instances using WebSockets
23
+ *
24
+ * This conduit is for syncing between browsers and servers, or between servers.
25
+ * WebSockets cannot be used for direct browser-to-browser communication without a server in the middle.
26
+ */
27
+ export declare class WebSocketConduitAugmentation extends BaseConduitAugmentation implements IWebSocketSupport {
28
+ readonly description = "Conduit augmentation that syncs Brainy instances using WebSockets";
29
+ private webSocketConnections;
30
+ private messageCallbacks;
31
+ constructor(name?: string);
32
+ getType(): AugmentationType;
33
+ /**
34
+ * Establishes a connection to another Brainy instance
35
+ * @param targetSystemId The URL or identifier of the target system
36
+ * @param config Configuration options for the connection
37
+ */
38
+ establishConnection(targetSystemId: string, config: Record<string, unknown>): Promise<AugmentationResponse<WebSocketConnection>>;
39
+ /**
40
+ * Reads data from a connected Brainy instance
41
+ * @param query Query parameters for reading data
42
+ * @param options Additional options
43
+ */
44
+ readData(query: Record<string, unknown>, options?: Record<string, unknown>): Promise<AugmentationResponse<unknown>>;
45
+ /**
46
+ * Writes data to a connected Brainy instance
47
+ * @param data The data to write
48
+ * @param options Additional options
49
+ */
50
+ writeData(data: Record<string, unknown>, options?: Record<string, unknown>): Promise<AugmentationResponse<unknown>>;
51
+ /**
52
+ * Monitors a data stream from a connected Brainy instance
53
+ * @param streamId The ID of the stream to monitor (usually a connection ID)
54
+ * @param callback Function to call when new data is received
55
+ */
56
+ monitorStream(streamId: string, callback: (data: unknown) => void): Promise<void>;
57
+ /**
58
+ * Establishes a WebSocket connection
59
+ * @param url The WebSocket server URL to connect to
60
+ * @param protocols Optional subprotocols
61
+ */
62
+ connectWebSocket(url: string, protocols?: string | string[]): Promise<WebSocketConnection>;
63
+ /**
64
+ * Sends data through an established WebSocket connection
65
+ * @param connectionId The identifier of the established connection
66
+ * @param data The data to send (will be serialized if not a string)
67
+ */
68
+ sendWebSocketMessage(connectionId: string, data: unknown): Promise<void>;
69
+ /**
70
+ * Registers a callback for incoming WebSocket messages
71
+ * @param connectionId The identifier of the established connection
72
+ * @param callback The function to call when a message is received
73
+ */
74
+ onWebSocketMessage(connectionId: string, callback: (data: unknown) => void): Promise<void>;
75
+ /**
76
+ * Removes a callback for incoming WebSocket messages
77
+ * @param connectionId The identifier of the established connection
78
+ * @param callback The function to remove from the callbacks
79
+ */
80
+ offWebSocketMessage(connectionId: string, callback: (data: unknown) => void): Promise<void>;
81
+ /**
82
+ * Closes an established WebSocket connection
83
+ * @param connectionId The identifier of the established connection
84
+ * @param code Optional close code
85
+ * @param reason Optional close reason
86
+ */
87
+ closeWebSocket(connectionId: string, code?: number, reason?: string): Promise<void>;
88
+ }
89
+ /**
90
+ * WebRTC conduit augmentation for syncing Brainy instances using WebRTC
91
+ *
92
+ * This conduit is for direct peer-to-peer syncing between browsers.
93
+ * It is the recommended approach for browser-to-browser communication.
94
+ */
95
+ export declare class WebRTCConduitAugmentation extends BaseConduitAugmentation implements IWebSocketSupport {
96
+ readonly description = "Conduit augmentation that syncs Brainy instances using WebRTC";
97
+ private peerConnections;
98
+ private dataChannels;
99
+ private webSocketConnections;
100
+ private messageCallbacks;
101
+ private signalServer;
102
+ constructor(name?: string);
103
+ getType(): AugmentationType;
104
+ initialize(): Promise<void>;
105
+ /**
106
+ * Establishes a connection to another Brainy instance using WebRTC
107
+ * @param targetSystemId The peer ID or signal server URL
108
+ * @param config Configuration options for the connection
109
+ */
110
+ establishConnection(targetSystemId: string, config: Record<string, unknown>): Promise<AugmentationResponse<WebSocketConnection>>;
111
+ /**
112
+ * Handles an incoming WebRTC offer
113
+ * @param peerId The ID of the peer sending the offer
114
+ * @param offer The SDP offer
115
+ * @param config Configuration options
116
+ */
117
+ private handleOffer;
118
+ /**
119
+ * Reads data from a connected Brainy instance
120
+ * @param query Query parameters for reading data
121
+ * @param options Additional options
122
+ */
123
+ readData(query: Record<string, unknown>, options?: Record<string, unknown>): Promise<AugmentationResponse<unknown>>;
124
+ /**
125
+ * Writes data to a connected Brainy instance
126
+ * @param data The data to write
127
+ * @param options Additional options
128
+ */
129
+ writeData(data: Record<string, unknown>, options?: Record<string, unknown>): Promise<AugmentationResponse<unknown>>;
130
+ /**
131
+ * Monitors a data stream from a connected Brainy instance
132
+ * @param streamId The ID of the stream to monitor (usually a connection ID)
133
+ * @param callback Function to call when new data is received
134
+ */
135
+ monitorStream(streamId: string, callback: (data: unknown) => void): Promise<void>;
136
+ /**
137
+ * Establishes a WebSocket connection (used for signaling in WebRTC)
138
+ * @param url The WebSocket server URL to connect to
139
+ * @param protocols Optional subprotocols
140
+ */
141
+ connectWebSocket(url: string, protocols?: string | string[]): Promise<WebSocketConnection>;
142
+ /**
143
+ * Sends data through an established WebSocket or WebRTC connection
144
+ * @param connectionId The identifier of the established connection
145
+ * @param data The data to send (will be serialized if not a string)
146
+ */
147
+ sendWebSocketMessage(connectionId: string, data: unknown): Promise<void>;
148
+ /**
149
+ * Registers a callback for incoming WebSocket or WebRTC messages
150
+ * @param connectionId The identifier of the established connection
151
+ * @param callback The function to call when a message is received
152
+ */
153
+ onWebSocketMessage(connectionId: string, callback: (data: unknown) => void): Promise<void>;
154
+ /**
155
+ * Removes a callback for incoming WebSocket or WebRTC messages
156
+ * @param connectionId The identifier of the established connection
157
+ * @param callback The function to remove from the callbacks
158
+ */
159
+ offWebSocketMessage(connectionId: string, callback: (data: unknown) => void): Promise<void>;
160
+ /**
161
+ * Closes an established WebSocket or WebRTC connection
162
+ * @param connectionId The identifier of the established connection
163
+ * @param code Optional close code
164
+ * @param reason Optional close reason
165
+ */
166
+ closeWebSocket(connectionId: string, code?: number, reason?: string): Promise<void>;
167
+ }
168
+ /**
169
+ * Factory function to create the appropriate conduit augmentation based on the type
170
+ */
171
+ export declare function createConduitAugmentation(type: 'websocket' | 'webrtc', name?: string, options?: Record<string, unknown>): Promise<IConduitAugmentation & IWebSocketSupport>;
172
+ export {};
173
+ //# sourceMappingURL=conduitAugmentations.d.ts.map
@@ -0,0 +1,71 @@
1
+ import { AugmentationType, IMemoryAugmentation, AugmentationResponse } from '../types/augmentations.js';
2
+ import { StorageAdapter } from '../coreTypes.js';
3
+ /**
4
+ * Base class for memory augmentations that wrap a StorageAdapter
5
+ */
6
+ declare abstract class BaseMemoryAugmentation implements IMemoryAugmentation {
7
+ readonly name: string;
8
+ readonly description: string;
9
+ enabled: boolean;
10
+ protected storage: StorageAdapter;
11
+ protected isInitialized: boolean;
12
+ constructor(name: string, storage: StorageAdapter);
13
+ initialize(): Promise<void>;
14
+ shutDown(): Promise<void>;
15
+ getStatus(): Promise<'active' | 'inactive' | 'error'>;
16
+ storeData(key: string, data: unknown, options?: Record<string, unknown>): Promise<AugmentationResponse<boolean>>;
17
+ retrieveData(key: string, options?: Record<string, unknown>): Promise<AugmentationResponse<unknown>>;
18
+ updateData(key: string, data: unknown, options?: Record<string, unknown>): Promise<AugmentationResponse<boolean>>;
19
+ deleteData(key: string, options?: Record<string, unknown>): Promise<AugmentationResponse<boolean>>;
20
+ listDataKeys(pattern?: string, options?: Record<string, unknown>): Promise<AugmentationResponse<string[]>>;
21
+ /**
22
+ * Searches for data in the storage using vector similarity.
23
+ * Implements the findNearest functionality by calculating distances client-side.
24
+ * @param query The query vector or data to search for
25
+ * @param k Number of results to return (default: 10)
26
+ * @param options Optional search options
27
+ */
28
+ search(query: unknown, k?: number, options?: Record<string, unknown>): Promise<AugmentationResponse<Array<{
29
+ id: string;
30
+ score: number;
31
+ data: unknown;
32
+ }>>>;
33
+ protected ensureInitialized(): Promise<void>;
34
+ }
35
+ /**
36
+ * Memory augmentation that uses in-memory storage
37
+ */
38
+ export declare class MemoryStorageAugmentation extends BaseMemoryAugmentation {
39
+ readonly description = "Memory augmentation that stores data in memory";
40
+ enabled: boolean;
41
+ constructor(name: string);
42
+ getType(): AugmentationType;
43
+ }
44
+ /**
45
+ * Memory augmentation that uses file system storage
46
+ */
47
+ export declare class FileSystemStorageAugmentation extends BaseMemoryAugmentation {
48
+ readonly description = "Memory augmentation that stores data in the file system";
49
+ enabled: boolean;
50
+ constructor(name: string, rootDirectory?: string);
51
+ getType(): AugmentationType;
52
+ }
53
+ /**
54
+ * Memory augmentation that uses OPFS (Origin Private File System) storage
55
+ */
56
+ export declare class OPFSStorageAugmentation extends BaseMemoryAugmentation {
57
+ readonly description = "Memory augmentation that stores data in the Origin Private File System";
58
+ enabled: boolean;
59
+ constructor(name: string);
60
+ getType(): AugmentationType;
61
+ }
62
+ /**
63
+ * Factory function to create the appropriate memory augmentation based on the environment
64
+ */
65
+ export declare function createMemoryAugmentation(name: string, options?: {
66
+ storageType?: 'memory' | 'filesystem' | 'opfs';
67
+ rootDirectory?: string;
68
+ requestPersistentStorage?: boolean;
69
+ }): Promise<IMemoryAugmentation>;
70
+ export {};
71
+ //# sourceMappingURL=memoryAugmentations.d.ts.map
@@ -0,0 +1,168 @@
1
+ /**
2
+ * Server Search Augmentations
3
+ *
4
+ * This file implements conduit and activation augmentations for browser-server search functionality.
5
+ * It allows Brainy to search a server-hosted instance and store results locally.
6
+ */
7
+ import { AugmentationType, IActivationAugmentation, AugmentationResponse, WebSocketConnection } from '../types/augmentations.js';
8
+ import { WebSocketConduitAugmentation } from './conduitAugmentations.js';
9
+ import { BrainyDataInterface } from '../types/brainyDataInterface.js';
10
+ /**
11
+ * ServerSearchConduitAugmentation
12
+ *
13
+ * A specialized conduit augmentation that provides functionality for searching
14
+ * a server-hosted Brainy instance and storing results locally.
15
+ */
16
+ export declare class ServerSearchConduitAugmentation extends WebSocketConduitAugmentation {
17
+ private localDb;
18
+ constructor(name?: string);
19
+ /**
20
+ * Initialize the augmentation
21
+ */
22
+ initialize(): Promise<void>;
23
+ /**
24
+ * Set the local Brainy instance
25
+ * @param db The Brainy instance to use for local storage
26
+ */
27
+ setLocalDb(db: BrainyDataInterface): void;
28
+ /**
29
+ * Get the local Brainy instance
30
+ * @returns The local Brainy instance
31
+ */
32
+ getLocalDb(): BrainyDataInterface | null;
33
+ /**
34
+ * Search the server-hosted Brainy instance and store results locally
35
+ * @param connectionId The ID of the established connection
36
+ * @param query The search query
37
+ * @param limit Maximum number of results to return
38
+ * @returns Search results
39
+ */
40
+ searchServer(connectionId: string, query: string, limit?: number): Promise<AugmentationResponse<unknown>>;
41
+ /**
42
+ * Search the local Brainy instance
43
+ * @param query The search query
44
+ * @param limit Maximum number of results to return
45
+ * @returns Search results
46
+ */
47
+ searchLocal(query: string, limit?: number): Promise<AugmentationResponse<unknown>>;
48
+ /**
49
+ * Search both server and local instances, combine results, and store server results locally
50
+ * @param connectionId The ID of the established connection
51
+ * @param query The search query
52
+ * @param limit Maximum number of results to return
53
+ * @returns Combined search results
54
+ */
55
+ searchCombined(connectionId: string, query: string, limit?: number): Promise<AugmentationResponse<unknown>>;
56
+ /**
57
+ * Add data to both local and server instances
58
+ * @param connectionId The ID of the established connection
59
+ * @param data Text or vector to add
60
+ * @param metadata Metadata for the data
61
+ * @returns ID of the added data
62
+ */
63
+ addToBoth(connectionId: string, data: string | any[], metadata?: any): Promise<AugmentationResponse<string>>;
64
+ }
65
+ /**
66
+ * ServerSearchActivationAugmentation
67
+ *
68
+ * An activation augmentation that provides actions for server search functionality.
69
+ */
70
+ export declare class ServerSearchActivationAugmentation implements IActivationAugmentation {
71
+ readonly name: string;
72
+ readonly description: string;
73
+ enabled: boolean;
74
+ private isInitialized;
75
+ private conduitAugmentation;
76
+ private connections;
77
+ constructor(name?: string);
78
+ getType(): AugmentationType;
79
+ /**
80
+ * Initialize the augmentation
81
+ */
82
+ initialize(): Promise<void>;
83
+ /**
84
+ * Shut down the augmentation
85
+ */
86
+ shutDown(): Promise<void>;
87
+ /**
88
+ * Get the status of the augmentation
89
+ */
90
+ getStatus(): Promise<'active' | 'inactive' | 'error'>;
91
+ /**
92
+ * Set the conduit augmentation to use for server search
93
+ * @param conduit The ServerSearchConduitAugmentation to use
94
+ */
95
+ setConduitAugmentation(conduit: ServerSearchConduitAugmentation): void;
96
+ /**
97
+ * Store a connection for later use
98
+ * @param connectionId The ID to use for the connection
99
+ * @param connection The WebSocket connection
100
+ */
101
+ storeConnection(connectionId: string, connection: WebSocketConnection): void;
102
+ /**
103
+ * Get a stored connection
104
+ * @param connectionId The ID of the connection to retrieve
105
+ * @returns The WebSocket connection
106
+ */
107
+ getConnection(connectionId: string): WebSocketConnection | undefined;
108
+ /**
109
+ * Trigger an action based on a processed command or internal state
110
+ * @param actionName The name of the action to trigger
111
+ * @param parameters Optional parameters for the action
112
+ */
113
+ triggerAction(actionName: string, parameters?: Record<string, unknown>): AugmentationResponse<unknown>;
114
+ /**
115
+ * Handle the connectToServer action
116
+ * @param parameters Action parameters
117
+ */
118
+ private handleConnectToServer;
119
+ /**
120
+ * Handle the searchServer action
121
+ * @param parameters Action parameters
122
+ */
123
+ private handleSearchServer;
124
+ /**
125
+ * Handle the searchLocal action
126
+ * @param parameters Action parameters
127
+ */
128
+ private handleSearchLocal;
129
+ /**
130
+ * Handle the searchCombined action
131
+ * @param parameters Action parameters
132
+ */
133
+ private handleSearchCombined;
134
+ /**
135
+ * Handle the addToBoth action
136
+ * @param parameters Action parameters
137
+ */
138
+ private handleAddToBoth;
139
+ /**
140
+ * Generates an expressive output or response from Brainy
141
+ * @param knowledgeId The identifier of the knowledge to express
142
+ * @param format The desired output format (e.g., 'text', 'json')
143
+ */
144
+ generateOutput(knowledgeId: string, format: string): AugmentationResponse<string | Record<string, unknown>>;
145
+ /**
146
+ * Interacts with an external system or API
147
+ * @param systemId The identifier of the external system
148
+ * @param payload The data to send to the external system
149
+ */
150
+ interactExternal(systemId: string, payload: Record<string, unknown>): AugmentationResponse<unknown>;
151
+ }
152
+ /**
153
+ * Factory function to create server search augmentations
154
+ * @param serverUrl The URL of the server to connect to
155
+ * @param options Additional options
156
+ * @returns An object containing the created augmentations
157
+ */
158
+ export declare function createServerSearchAugmentations(serverUrl: string, options?: {
159
+ conduitName?: string;
160
+ activationName?: string;
161
+ protocols?: string | string[];
162
+ localDb?: BrainyDataInterface;
163
+ }): Promise<{
164
+ conduit: ServerSearchConduitAugmentation;
165
+ activation: ServerSearchActivationAugmentation;
166
+ connection: WebSocketConnection;
167
+ }>;
168
+ //# sourceMappingURL=serverSearchAugmentations.d.ts.map