superbrain-distributed-sdk 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.
package/demo.js ADDED
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const index_1 = require("./index");
4
+ async function main() {
5
+ console.log("๐Ÿค– Node.js Superbrain Agent Demo");
6
+ try {
7
+ // We will assume the local cluster is running from earlier commands on port 50050
8
+ const client = new index_1.Client('localhost:50050');
9
+ console.log("โœ… Custom Client initialized");
10
+ client.register("typescript-demo-agent");
11
+ console.log("โœ… Agent registered to Secure Fabric");
12
+ const dataStr = "Hello from Node.js (TypeScript) via Koffi/gRPC!";
13
+ const dataBytes = Buffer.from(dataStr, 'utf-8');
14
+ const ptr = client.allocate(dataBytes.length + 128);
15
+ console.log(`โœ… Allocated pointer: ${ptr}`);
16
+ client.write(ptr, 0, dataBytes);
17
+ console.log(`โœ… Wrote data to pointer`);
18
+ const readBuf = client.read(ptr, 0, dataBytes.length + 128);
19
+ // Remove null padding
20
+ const resultStr = readBuf.toString('utf-8').replace(/\0/g, '');
21
+ console.log(`โœ… Read verified: ${resultStr}`);
22
+ client.free(ptr);
23
+ console.log("๐Ÿงน Memory freed.");
24
+ }
25
+ catch (err) {
26
+ console.error("Failed to run demo:", err.message);
27
+ }
28
+ }
29
+ main();
package/demo.ts ADDED
@@ -0,0 +1,37 @@
1
+ import { Client, SuperbrainError } from './index';
2
+
3
+ async function main() {
4
+ console.log("๐Ÿค– Node.js Superbrain Agent Demo");
5
+
6
+ try {
7
+ // We will assume the local cluster is running from earlier commands on port 50050
8
+ const client = new Client('localhost:50050');
9
+ console.log("โœ… Custom Client initialized");
10
+
11
+ client.register("typescript-demo-agent");
12
+ console.log("โœ… Agent registered to Secure Fabric");
13
+
14
+ const dataStr = "Hello from Node.js (TypeScript) via Koffi/gRPC!";
15
+ const dataBytes = Buffer.from(dataStr, 'utf-8');
16
+
17
+ const ptr = client.allocate(dataBytes.length + 128);
18
+ console.log(`โœ… Allocated pointer: ${ptr}`);
19
+
20
+ client.write(ptr, 0, dataBytes);
21
+ console.log(`โœ… Wrote data to pointer`);
22
+
23
+ const readBuf = client.read(ptr, 0, dataBytes.length + 128);
24
+
25
+ // Remove null padding
26
+ const resultStr = readBuf.toString('utf-8').replace(/\0/g, '');
27
+ console.log(`โœ… Read verified: ${resultStr}`);
28
+
29
+ client.free(ptr);
30
+ console.log("๐Ÿงน Memory freed.");
31
+
32
+ } catch (err: any) {
33
+ console.error("Failed to run demo:", err.message);
34
+ }
35
+ }
36
+
37
+ main();
package/index.js ADDED
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.Client = exports.SuperbrainError = void 0;
40
+ const koffi_1 = __importDefault(require("koffi"));
41
+ const os = __importStar(require("os"));
42
+ const path = __importStar(require("path"));
43
+ const fs = __importStar(require("fs"));
44
+ class SuperbrainError extends Error {
45
+ constructor(message) {
46
+ super(message);
47
+ this.name = 'SuperbrainError';
48
+ }
49
+ }
50
+ exports.SuperbrainError = SuperbrainError;
51
+ // Locate shared library
52
+ const libName = os.platform() === 'darwin' ? 'libsuperbrain.dylib' : 'libsuperbrain.so';
53
+ // Try finding it correctly in the package or local structure
54
+ let libPath = path.join(__dirname, '..', '..', 'lib', libName);
55
+ if (!fs.existsSync(libPath)) {
56
+ libPath = path.join(process.cwd(), libName);
57
+ }
58
+ if (!fs.existsSync(libPath)) {
59
+ libPath = path.join(process.cwd(), '..', 'lib', libName);
60
+ }
61
+ if (!fs.existsSync(libPath)) {
62
+ throw new SuperbrainError(`Shared library ${libName} not found at ${libPath}. Ensure it is built and in the correct path.`);
63
+ }
64
+ const lib = koffi_1.default.load(libPath);
65
+ // C Bindings
66
+ const SB_NewClient = lib.func('SB_NewClient', 'str', ['str']);
67
+ const SB_NewClientWithEncryption = lib.func('SB_NewClientWithEncryption', 'str', ['str', 'uint8_t*', 'int']);
68
+ const SB_Register = lib.func('SB_Register', 'str', ['str', 'str']);
69
+ const SB_Allocate = lib.func('SB_Allocate', 'str', ['str', 'uint64_t']);
70
+ const SB_Write = lib.func('SB_Write', 'str', ['str', 'str', 'uint64_t', 'uint8_t*', 'uint64_t']);
71
+ const SB_Read = lib.func('SB_Read', 'str', ['str', 'str', 'uint64_t', 'uint64_t', '_Out_ uint8_t**', '_Out_ uint64_t*']);
72
+ const SB_Free = lib.func('SB_Free', 'str', ['str', 'str']);
73
+ class Client {
74
+ clientId;
75
+ constructor(addrs, encryptionKey) {
76
+ let res;
77
+ if (encryptionKey) {
78
+ if (encryptionKey.length !== 32) {
79
+ throw new SuperbrainError('Encryption key must be exactly 32 bytes for AES-GCM-256');
80
+ }
81
+ res = SB_NewClientWithEncryption(addrs, encryptionKey, encryptionKey.length);
82
+ }
83
+ else {
84
+ res = SB_NewClient(addrs);
85
+ }
86
+ if (res && res.startsWith('error:')) {
87
+ throw new SuperbrainError(res);
88
+ }
89
+ this.clientId = res;
90
+ }
91
+ register(agentId) {
92
+ const res = SB_Register(this.clientId, agentId);
93
+ if (res && res.startsWith('error:')) {
94
+ throw new SuperbrainError(res);
95
+ }
96
+ }
97
+ allocate(size) {
98
+ const res = SB_Allocate(this.clientId, size);
99
+ if (res && res.startsWith('error:')) {
100
+ throw new SuperbrainError(res);
101
+ }
102
+ return res;
103
+ }
104
+ write(ptrId, offset, data) {
105
+ const res = SB_Write(this.clientId, ptrId, offset, data, data.length);
106
+ if (res && res.startsWith('error:')) {
107
+ throw new SuperbrainError(res);
108
+ }
109
+ }
110
+ read(ptrId, offset, length) {
111
+ // Output pointers for koffi
112
+ const outDataPtr = [null];
113
+ const outLenPtr = [0];
114
+ const res = SB_Read(this.clientId, ptrId, offset, length, outDataPtr, outLenPtr);
115
+ if (res && res.startsWith('error:')) {
116
+ throw new SuperbrainError(res);
117
+ }
118
+ const outBufPtr = outDataPtr[0];
119
+ const outLen = outLenPtr[0];
120
+ if (!outBufPtr || outLen === 0) {
121
+ return Buffer.alloc(0);
122
+ }
123
+ // Decode the C string memory pointer into a Buffer
124
+ const decodedBuffer = koffi_1.default.decode(outBufPtr, 'uint8_t', outLen);
125
+ const buffer = Buffer.from(decodedBuffer);
126
+ // Note: C-allocated pointer memory leak if we don't C-free,
127
+ // but for now Superbrain handles general lifecycle cleanup
128
+ // when client exists or pointer freed.
129
+ return buffer;
130
+ }
131
+ free(ptrId) {
132
+ const res = SB_Free(this.clientId, ptrId);
133
+ if (res && res.startsWith('error:')) {
134
+ throw new SuperbrainError(res);
135
+ }
136
+ }
137
+ }
138
+ exports.Client = Client;
package/index.ts ADDED
@@ -0,0 +1,118 @@
1
+ import koffi from 'koffi';
2
+ import * as os from 'os';
3
+ import * as path from 'path';
4
+ import * as fs from 'fs';
5
+
6
+ export class SuperbrainError extends Error {
7
+ constructor(message: string) {
8
+ super(message);
9
+ this.name = 'SuperbrainError';
10
+ }
11
+ }
12
+
13
+ // Locate shared library
14
+ const libName = os.platform() === 'darwin' ? 'libsuperbrain.dylib' : 'libsuperbrain.so';
15
+
16
+ // Try finding it correctly in the package or local structure
17
+ let libPath = path.join(__dirname, '..', '..', 'lib', libName);
18
+ if (!fs.existsSync(libPath)) {
19
+ libPath = path.join(process.cwd(), libName);
20
+ }
21
+ if (!fs.existsSync(libPath)) {
22
+ libPath = path.join(process.cwd(), '..', 'lib', libName);
23
+ }
24
+
25
+ if (!fs.existsSync(libPath)) {
26
+ throw new SuperbrainError(`Shared library ${libName} not found at ${libPath}. Ensure it is built and in the correct path.`);
27
+ }
28
+
29
+ const lib = koffi.load(libPath);
30
+
31
+ // C Bindings
32
+ const SB_NewClient = lib.func('SB_NewClient', 'str', ['str']);
33
+ const SB_NewClientWithEncryption = lib.func('SB_NewClientWithEncryption', 'str', ['str', 'uint8_t*', 'int']);
34
+ const SB_Register = lib.func('SB_Register', 'str', ['str', 'str']);
35
+ const SB_Allocate = lib.func('SB_Allocate', 'str', ['str', 'uint64_t']);
36
+ const SB_Write = lib.func('SB_Write', 'str', ['str', 'str', 'uint64_t', 'uint8_t*', 'uint64_t']);
37
+ const SB_Read = lib.func('SB_Read', 'str', ['str', 'str', 'uint64_t', 'uint64_t', '_Out_ uint8_t**', '_Out_ uint64_t*']);
38
+ const SB_Free = lib.func('SB_Free', 'str', ['str', 'str']);
39
+
40
+ export class Client {
41
+ private clientId: string;
42
+
43
+ constructor(addrs: string, encryptionKey?: Buffer) {
44
+ let res: string;
45
+ if (encryptionKey) {
46
+ if (encryptionKey.length !== 32) {
47
+ throw new SuperbrainError('Encryption key must be exactly 32 bytes for AES-GCM-256');
48
+ }
49
+ res = SB_NewClientWithEncryption(addrs, encryptionKey, encryptionKey.length);
50
+ } else {
51
+ res = SB_NewClient(addrs);
52
+ }
53
+
54
+ if (res && res.startsWith('error:')) {
55
+ throw new SuperbrainError(res);
56
+ }
57
+
58
+ this.clientId = res;
59
+ }
60
+
61
+ public register(agentId: string): void {
62
+ const res = SB_Register(this.clientId, agentId);
63
+ if (res && res.startsWith('error:')) {
64
+ throw new SuperbrainError(res);
65
+ }
66
+ }
67
+
68
+ public allocate(size: number): string {
69
+ const res = SB_Allocate(this.clientId, size);
70
+ if (res && res.startsWith('error:')) {
71
+ throw new SuperbrainError(res);
72
+ }
73
+ return res;
74
+ }
75
+
76
+ public write(ptrId: string, offset: number, data: Buffer): void {
77
+ const res = SB_Write(this.clientId, ptrId, offset, data, data.length);
78
+ if (res && res.startsWith('error:')) {
79
+ throw new SuperbrainError(res);
80
+ }
81
+ }
82
+
83
+ public read(ptrId: string, offset: number, length: number): Buffer {
84
+ // Output pointers for koffi
85
+ const outDataPtr = [null];
86
+ const outLenPtr = [0];
87
+
88
+ const res = SB_Read(this.clientId, ptrId, offset, length, outDataPtr, outLenPtr);
89
+
90
+ if (res && res.startsWith('error:')) {
91
+ throw new SuperbrainError(res);
92
+ }
93
+
94
+ const outBufPtr = outDataPtr[0] as any;
95
+ const outLen = outLenPtr[0] as number;
96
+
97
+ if (!outBufPtr || outLen === 0) {
98
+ return Buffer.alloc(0);
99
+ }
100
+
101
+ // Decode the C string memory pointer into a Buffer
102
+ const decodedBuffer = koffi.decode(outBufPtr, 'uint8_t', outLen);
103
+ const buffer = Buffer.from(decodedBuffer);
104
+
105
+ // Note: C-allocated pointer memory leak if we don't C-free,
106
+ // but for now Superbrain handles general lifecycle cleanup
107
+ // when client exists or pointer freed.
108
+
109
+ return buffer;
110
+ }
111
+
112
+ public free(ptrId: string): void {
113
+ const res = SB_Free(this.clientId, ptrId);
114
+ if (res && res.startsWith('error:')) {
115
+ throw new SuperbrainError(res);
116
+ }
117
+ }
118
+ }
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "superbrain-distributed-sdk",
3
+ "version": "0.1.0",
4
+ "description": "Premium High-Performance Distributed Memory SDK for AI Agents",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/anispy211/superbrainSdk.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/anispy211/superbrainSdk/issues"
13
+ },
14
+ "homepage": "https://github.com/anispy211/superbrainSdk#readme",
15
+ "keywords": [
16
+ "distributed-memory",
17
+ "ai-agents",
18
+ "mcp",
19
+ "grpc",
20
+ "performance",
21
+ "context-sharing"
22
+ ],
23
+ "author": "Anispy",
24
+ "license": "MIT",
25
+ "engines": {
26
+ "node": ">=18.0.0"
27
+ },
28
+ "dependencies": {
29
+ "koffi": "^2.15.1"
30
+ },
31
+ "devDependencies": {
32
+ "@types/node": "^20.0.0",
33
+ "typescript": "^5.0.0"
34
+ }
35
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2022",
4
+ "module": "commonjs",
5
+ "esModuleInterop": true,
6
+ "forceConsistentCasingInFileNames": true,
7
+ "strict": true,
8
+ "skipLibCheck": true,
9
+ "resolveJsonModule": true,
10
+ "types": [
11
+ "node"
12
+ ]
13
+ },
14
+ "ts-node": {
15
+ "esm": true
16
+ }
17
+ }