@forbocai/node 0.4.10 → 0.5.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/dist/index.d.mts CHANGED
@@ -96,12 +96,27 @@ declare const generateEmbedding: (text: string) => Promise<number[]>;
96
96
  */
97
97
  declare const getVectorTable: (dbPath: string, tableName?: string) => Promise<any>;
98
98
  /**
99
- * Creates a new table in LanceDB.
99
+ * Creates a new LanceDB table with the memory schema.
100
100
  * @param dbPath - Path to the LanceDB directory.
101
101
  * @param tableName - Name of the table.
102
- * @param data - Initial data to populate the table.
103
- * @returns The created table object.
102
+ * @param initialData - Initial data rows.
103
+ * @returns The created table.
104
104
  */
105
- declare const createTable: (dbPath: string, tableName: string, data: any[]) => Promise<any>;
105
+ declare const createTable: (dbPath: string, tableName?: string, initialData?: any[]) => Promise<any>;
106
+ /**
107
+ * Creates a memory item with an embedding.
108
+ * @param text - The text to embed and store.
109
+ * @param type - Memory type.
110
+ * @param importance - Importance score (0-1).
111
+ * @returns A memory item object with embedding.
112
+ */
113
+ declare const createMemoryItemWithVector: (text: string, type?: string, importance?: number) => Promise<{
114
+ id: string;
115
+ text: string;
116
+ timestamp: number;
117
+ type: string;
118
+ importance: number;
119
+ vector: number[];
120
+ }>;
106
121
 
107
- export { type IMemory, type MemoryModuleConfig, type VectorStatus, createCortex, createMemory, createMemoryItem, createTable, generateEmbedding, getVectorTable, initVectorEngine };
122
+ export { type IMemory, type MemoryModuleConfig, type VectorStatus, createCortex, createMemory, createMemoryItem, createMemoryItemWithVector, createTable, generateEmbedding, getVectorTable, initVectorEngine };
package/dist/index.d.ts CHANGED
@@ -96,12 +96,27 @@ declare const generateEmbedding: (text: string) => Promise<number[]>;
96
96
  */
97
97
  declare const getVectorTable: (dbPath: string, tableName?: string) => Promise<any>;
98
98
  /**
99
- * Creates a new table in LanceDB.
99
+ * Creates a new LanceDB table with the memory schema.
100
100
  * @param dbPath - Path to the LanceDB directory.
101
101
  * @param tableName - Name of the table.
102
- * @param data - Initial data to populate the table.
103
- * @returns The created table object.
102
+ * @param initialData - Initial data rows.
103
+ * @returns The created table.
104
104
  */
105
- declare const createTable: (dbPath: string, tableName: string, data: any[]) => Promise<any>;
105
+ declare const createTable: (dbPath: string, tableName?: string, initialData?: any[]) => Promise<any>;
106
+ /**
107
+ * Creates a memory item with an embedding.
108
+ * @param text - The text to embed and store.
109
+ * @param type - Memory type.
110
+ * @param importance - Importance score (0-1).
111
+ * @returns A memory item object with embedding.
112
+ */
113
+ declare const createMemoryItemWithVector: (text: string, type?: string, importance?: number) => Promise<{
114
+ id: string;
115
+ text: string;
116
+ timestamp: number;
117
+ type: string;
118
+ importance: number;
119
+ vector: number[];
120
+ }>;
106
121
 
107
- export { type IMemory, type MemoryModuleConfig, type VectorStatus, createCortex, createMemory, createMemoryItem, createTable, generateEmbedding, getVectorTable, initVectorEngine };
122
+ export { type IMemory, type MemoryModuleConfig, type VectorStatus, createCortex, createMemory, createMemoryItem, createMemoryItemWithVector, createTable, generateEmbedding, getVectorTable, initVectorEngine };
package/dist/index.js CHANGED
@@ -33,6 +33,7 @@ __export(index_exports, {
33
33
  createCortex: () => createCortex,
34
34
  createMemory: () => createMemory,
35
35
  createMemoryItem: () => createMemoryItem,
36
+ createMemoryItemWithVector: () => createMemoryItemWithVector,
36
37
  createTable: () => createTable,
37
38
  generateEmbedding: () => generateEmbedding,
38
39
  getVectorTable: () => getVectorTable,
@@ -41,9 +42,68 @@ __export(index_exports, {
41
42
  module.exports = __toCommonJS(index_exports);
42
43
 
43
44
  // src/cortex.ts
44
- var fs = __toESM(require("fs"));
45
- var path = __toESM(require("path"));
45
+ var fs2 = __toESM(require("fs"));
46
+ var path2 = __toESM(require("path"));
46
47
  var https = __toESM(require("https"));
48
+
49
+ // src/vector.ts
50
+ var path = __toESM(require("path"));
51
+ var fs = __toESM(require("fs"));
52
+ var embedder = null;
53
+ var lancedb;
54
+ var initVectorEngine = async () => {
55
+ if (embedder) return;
56
+ try {
57
+ console.log("> Initializing Vector Engine (Transformers)...");
58
+ const transformers = await import("@huggingface/transformers");
59
+ transformers.env.cacheDir = path.join(process.env.HOME || ".", ".forbocai", "cache");
60
+ embedder = await transformers.pipeline("feature-extraction", "Xenova/all-MiniLM-L6-v2");
61
+ console.log("> Initializing LanceDB...");
62
+ lancedb = await import("@lancedb/lancedb");
63
+ } catch (e) {
64
+ console.error("Failed to init Vector Engine:", e);
65
+ }
66
+ };
67
+ var generateEmbedding = async (text) => {
68
+ if (!embedder) await initVectorEngine();
69
+ try {
70
+ const output = await embedder(text, { pooling: "mean", normalize: true });
71
+ return Array.from(output.data);
72
+ } catch (e) {
73
+ console.error("Embedding failed:", e);
74
+ return new Array(384).fill(0);
75
+ }
76
+ };
77
+ var getVectorTable = async (dbPath, tableName = "memories") => {
78
+ if (!lancedb) await initVectorEngine();
79
+ if (!fs.existsSync(dbPath)) fs.mkdirSync(dbPath, { recursive: true });
80
+ const db = await lancedb.connect(dbPath);
81
+ const tables = await db.tableNames();
82
+ if (tables.includes(tableName)) {
83
+ return await db.openTable(tableName);
84
+ } else {
85
+ return null;
86
+ }
87
+ };
88
+ var createTable = async (dbPath, tableName = "memories", initialData = []) => {
89
+ if (!lancedb) await initVectorEngine();
90
+ if (!fs.existsSync(dbPath)) fs.mkdirSync(dbPath, { recursive: true });
91
+ const db = await lancedb.connect(dbPath);
92
+ return await db.createTable(tableName, initialData);
93
+ };
94
+ var createMemoryItemWithVector = async (text, type = "observation", importance = 0.5) => {
95
+ const embedding = await generateEmbedding(text);
96
+ return {
97
+ id: `mem_${Date.now()}_${Math.random().toString(36).substring(7)}`,
98
+ text,
99
+ timestamp: Date.now(),
100
+ type,
101
+ importance,
102
+ vector: embedding
103
+ };
104
+ };
105
+
106
+ // src/cortex.ts
47
107
  var isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
48
108
  if (!isNode) {
49
109
  throw new Error("ForbocAI SDK requires Node.js environment - browser support has been removed");
@@ -55,7 +115,7 @@ var MODEL_URLS = {
55
115
  var DEFAULT_MODEL = "smollm2-135m";
56
116
  var downloadFile = (url, destPath) => {
57
117
  return new Promise((resolve, reject) => {
58
- const file = fs.createWriteStream(destPath);
118
+ const file = fs2.createWriteStream(destPath);
59
119
  https.get(url, (response) => {
60
120
  if (response.statusCode === 302 || response.statusCode === 301) {
61
121
  downloadFile(response.headers.location, destPath).then(resolve).catch(reject);
@@ -71,7 +131,7 @@ var downloadFile = (url, destPath) => {
71
131
  resolve();
72
132
  });
73
133
  }).on("error", (err) => {
74
- fs.unlink(destPath, () => {
134
+ fs2.unlink(destPath, () => {
75
135
  });
76
136
  reject(err);
77
137
  });
@@ -88,24 +148,22 @@ var createNativeCortex = (config) => {
88
148
  let model;
89
149
  let context;
90
150
  let session;
91
- let featureExtraction = null;
92
- const MODELS_DIR = path.join(process.cwd(), "local_infrastructure", "models");
151
+ const MODELS_DIR = path2.join(process.cwd(), "local_infrastructure", "models");
93
152
  const init = async () => {
94
153
  if (status.ready) return status;
95
154
  try {
96
155
  console.log("> Initializing Native Cortex...");
97
156
  console.log("> Loading Embedding Model (all-MiniLM-L6-v2)...");
98
- const { pipeline: pipeline2 } = await import("@xenova/transformers");
99
- featureExtraction = await pipeline2("feature-extraction", "Xenova/all-MiniLM-L6-v2");
157
+ await initVectorEngine();
100
158
  const ensureDirectoryExists = (dirPath) => {
101
- if (!fs.existsSync(dirPath)) fs.mkdirSync(dirPath, { recursive: true });
159
+ if (!fs2.existsSync(dirPath)) fs2.mkdirSync(dirPath, { recursive: true });
102
160
  };
103
161
  ensureDirectoryExists(MODELS_DIR);
104
162
  const modelKey = config.model || DEFAULT_MODEL;
105
163
  const modelUrl = MODEL_URLS[modelKey] || MODEL_URLS[DEFAULT_MODEL];
106
- const modelFileName = path.basename(modelUrl);
107
- const modelPath = path.join(MODELS_DIR, modelFileName);
108
- if (!fs.existsSync(modelPath)) {
164
+ const modelFileName = path2.basename(modelUrl);
165
+ const modelPath = path2.join(MODELS_DIR, modelFileName);
166
+ if (!fs2.existsSync(modelPath)) {
109
167
  console.log(`> Downloading SLM: ${modelKey}...`);
110
168
  await downloadFile(modelUrl, modelPath);
111
169
  console.log(`> Download complete.`);
@@ -183,13 +241,7 @@ var createNativeCortex = (config) => {
183
241
  };
184
242
  const embed = async (text) => {
185
243
  if (!status.ready) await init();
186
- try {
187
- const output = await featureExtraction(text, { pooling: "mean", normalize: true });
188
- return Array.from(output.data);
189
- } catch (e) {
190
- console.error("Embedding failed:", e);
191
- return new Array(384).fill(0);
192
- }
244
+ return generateEmbedding(text);
193
245
  };
194
246
  return {
195
247
  init,
@@ -202,66 +254,6 @@ var createCortex = (config) => createNativeCortex(config);
202
254
 
203
255
  // src/memory.ts
204
256
  var path3 = __toESM(require("path"));
205
-
206
- // src/vector.ts
207
- var path2 = __toESM(require("path"));
208
- var fs2 = __toESM(require("fs"));
209
- var pipeline;
210
- var lancedb;
211
- var initVectorEngine = async () => {
212
- if (pipeline) return;
213
- try {
214
- console.log("> Initializing Vector Engine (Transformers)...");
215
- const transformers = await import("@xenova/transformers");
216
- transformers.env.cacheDir = path2.join(process.env.HOME || ".", ".forbocai", "cache");
217
- pipeline = await transformers.pipeline("feature-extraction", "Xenova/all-MiniLM-L6-v2");
218
- console.log("> Initializing LanceDB...");
219
- lancedb = await import("@lancedb/lancedb");
220
- } catch (e) {
221
- console.error("Failed to init Vector Engine:", e);
222
- }
223
- };
224
- var generateEmbedding = async (text) => {
225
- try {
226
- if (!pipeline) await initVectorEngine();
227
- const output = await pipeline(text, { pooling: "mean", normalize: true });
228
- return Array.from(output.data);
229
- } catch (e) {
230
- console.warn("Transformers embedding failed, falling back to Cortex:", e);
231
- const cortexConfig = {
232
- model: "smollm2-135m",
233
- temperature: 0.7,
234
- maxTokens: 128,
235
- gpu: true
236
- };
237
- const cortex = createCortex(cortexConfig);
238
- try {
239
- await cortex.init();
240
- return await cortex.embed(text);
241
- } catch (cortexError) {
242
- console.error("All embedding methods failed:", cortexError);
243
- return new Array(384).fill(0);
244
- }
245
- }
246
- };
247
- var getVectorTable = async (dbPath, tableName = "memories") => {
248
- if (!lancedb) await initVectorEngine();
249
- if (!fs2.existsSync(dbPath)) fs2.mkdirSync(dbPath, { recursive: true });
250
- const db = await lancedb.connect(dbPath);
251
- const tables = await db.tableNames();
252
- if (tables.includes(tableName)) {
253
- return await db.openTable(tableName);
254
- } else {
255
- return null;
256
- }
257
- };
258
- var createTable = async (dbPath, tableName, data) => {
259
- if (!lancedb) await initVectorEngine();
260
- const db = await lancedb.connect(dbPath);
261
- return await db.createTable(tableName, data);
262
- };
263
-
264
- // src/memory.ts
265
257
  var createMemoryItem = (text, type = "observation", importance = 0.5) => ({
266
258
  id: `mem_${Date.now()}_${Math.random().toString(36).substring(7)}`,
267
259
  text,
@@ -373,6 +365,7 @@ var createMemory = (config = {}) => createLanceDBMemory(config);
373
365
  createCortex,
374
366
  createMemory,
375
367
  createMemoryItem,
368
+ createMemoryItemWithVector,
376
369
  createTable,
377
370
  generateEmbedding,
378
371
  getVectorTable,
package/dist/index.mjs CHANGED
@@ -1,7 +1,66 @@
1
1
  // src/cortex.ts
2
- import * as fs from "fs";
3
- import * as path from "path";
2
+ import * as fs2 from "fs";
3
+ import * as path2 from "path";
4
4
  import * as https from "https";
5
+
6
+ // src/vector.ts
7
+ import * as path from "path";
8
+ import * as fs from "fs";
9
+ var embedder = null;
10
+ var lancedb;
11
+ var initVectorEngine = async () => {
12
+ if (embedder) return;
13
+ try {
14
+ console.log("> Initializing Vector Engine (Transformers)...");
15
+ const transformers = await import("@huggingface/transformers");
16
+ transformers.env.cacheDir = path.join(process.env.HOME || ".", ".forbocai", "cache");
17
+ embedder = await transformers.pipeline("feature-extraction", "Xenova/all-MiniLM-L6-v2");
18
+ console.log("> Initializing LanceDB...");
19
+ lancedb = await import("@lancedb/lancedb");
20
+ } catch (e) {
21
+ console.error("Failed to init Vector Engine:", e);
22
+ }
23
+ };
24
+ var generateEmbedding = async (text) => {
25
+ if (!embedder) await initVectorEngine();
26
+ try {
27
+ const output = await embedder(text, { pooling: "mean", normalize: true });
28
+ return Array.from(output.data);
29
+ } catch (e) {
30
+ console.error("Embedding failed:", e);
31
+ return new Array(384).fill(0);
32
+ }
33
+ };
34
+ var getVectorTable = async (dbPath, tableName = "memories") => {
35
+ if (!lancedb) await initVectorEngine();
36
+ if (!fs.existsSync(dbPath)) fs.mkdirSync(dbPath, { recursive: true });
37
+ const db = await lancedb.connect(dbPath);
38
+ const tables = await db.tableNames();
39
+ if (tables.includes(tableName)) {
40
+ return await db.openTable(tableName);
41
+ } else {
42
+ return null;
43
+ }
44
+ };
45
+ var createTable = async (dbPath, tableName = "memories", initialData = []) => {
46
+ if (!lancedb) await initVectorEngine();
47
+ if (!fs.existsSync(dbPath)) fs.mkdirSync(dbPath, { recursive: true });
48
+ const db = await lancedb.connect(dbPath);
49
+ return await db.createTable(tableName, initialData);
50
+ };
51
+ var createMemoryItemWithVector = async (text, type = "observation", importance = 0.5) => {
52
+ const embedding = await generateEmbedding(text);
53
+ return {
54
+ id: `mem_${Date.now()}_${Math.random().toString(36).substring(7)}`,
55
+ text,
56
+ timestamp: Date.now(),
57
+ type,
58
+ importance,
59
+ vector: embedding
60
+ };
61
+ };
62
+
63
+ // src/cortex.ts
5
64
  var isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
6
65
  if (!isNode) {
7
66
  throw new Error("ForbocAI SDK requires Node.js environment - browser support has been removed");
@@ -13,7 +72,7 @@ var MODEL_URLS = {
13
72
  var DEFAULT_MODEL = "smollm2-135m";
14
73
  var downloadFile = (url, destPath) => {
15
74
  return new Promise((resolve, reject) => {
16
- const file = fs.createWriteStream(destPath);
75
+ const file = fs2.createWriteStream(destPath);
17
76
  https.get(url, (response) => {
18
77
  if (response.statusCode === 302 || response.statusCode === 301) {
19
78
  downloadFile(response.headers.location, destPath).then(resolve).catch(reject);
@@ -29,7 +88,7 @@ var downloadFile = (url, destPath) => {
29
88
  resolve();
30
89
  });
31
90
  }).on("error", (err) => {
32
- fs.unlink(destPath, () => {
91
+ fs2.unlink(destPath, () => {
33
92
  });
34
93
  reject(err);
35
94
  });
@@ -46,24 +105,22 @@ var createNativeCortex = (config) => {
46
105
  let model;
47
106
  let context;
48
107
  let session;
49
- let featureExtraction = null;
50
- const MODELS_DIR = path.join(process.cwd(), "local_infrastructure", "models");
108
+ const MODELS_DIR = path2.join(process.cwd(), "local_infrastructure", "models");
51
109
  const init = async () => {
52
110
  if (status.ready) return status;
53
111
  try {
54
112
  console.log("> Initializing Native Cortex...");
55
113
  console.log("> Loading Embedding Model (all-MiniLM-L6-v2)...");
56
- const { pipeline: pipeline2 } = await import("@xenova/transformers");
57
- featureExtraction = await pipeline2("feature-extraction", "Xenova/all-MiniLM-L6-v2");
114
+ await initVectorEngine();
58
115
  const ensureDirectoryExists = (dirPath) => {
59
- if (!fs.existsSync(dirPath)) fs.mkdirSync(dirPath, { recursive: true });
116
+ if (!fs2.existsSync(dirPath)) fs2.mkdirSync(dirPath, { recursive: true });
60
117
  };
61
118
  ensureDirectoryExists(MODELS_DIR);
62
119
  const modelKey = config.model || DEFAULT_MODEL;
63
120
  const modelUrl = MODEL_URLS[modelKey] || MODEL_URLS[DEFAULT_MODEL];
64
- const modelFileName = path.basename(modelUrl);
65
- const modelPath = path.join(MODELS_DIR, modelFileName);
66
- if (!fs.existsSync(modelPath)) {
121
+ const modelFileName = path2.basename(modelUrl);
122
+ const modelPath = path2.join(MODELS_DIR, modelFileName);
123
+ if (!fs2.existsSync(modelPath)) {
67
124
  console.log(`> Downloading SLM: ${modelKey}...`);
68
125
  await downloadFile(modelUrl, modelPath);
69
126
  console.log(`> Download complete.`);
@@ -141,13 +198,7 @@ var createNativeCortex = (config) => {
141
198
  };
142
199
  const embed = async (text) => {
143
200
  if (!status.ready) await init();
144
- try {
145
- const output = await featureExtraction(text, { pooling: "mean", normalize: true });
146
- return Array.from(output.data);
147
- } catch (e) {
148
- console.error("Embedding failed:", e);
149
- return new Array(384).fill(0);
150
- }
201
+ return generateEmbedding(text);
151
202
  };
152
203
  return {
153
204
  init,
@@ -160,66 +211,6 @@ var createCortex = (config) => createNativeCortex(config);
160
211
 
161
212
  // src/memory.ts
162
213
  import * as path3 from "path";
163
-
164
- // src/vector.ts
165
- import * as path2 from "path";
166
- import * as fs2 from "fs";
167
- var pipeline;
168
- var lancedb;
169
- var initVectorEngine = async () => {
170
- if (pipeline) return;
171
- try {
172
- console.log("> Initializing Vector Engine (Transformers)...");
173
- const transformers = await import("@xenova/transformers");
174
- transformers.env.cacheDir = path2.join(process.env.HOME || ".", ".forbocai", "cache");
175
- pipeline = await transformers.pipeline("feature-extraction", "Xenova/all-MiniLM-L6-v2");
176
- console.log("> Initializing LanceDB...");
177
- lancedb = await import("@lancedb/lancedb");
178
- } catch (e) {
179
- console.error("Failed to init Vector Engine:", e);
180
- }
181
- };
182
- var generateEmbedding = async (text) => {
183
- try {
184
- if (!pipeline) await initVectorEngine();
185
- const output = await pipeline(text, { pooling: "mean", normalize: true });
186
- return Array.from(output.data);
187
- } catch (e) {
188
- console.warn("Transformers embedding failed, falling back to Cortex:", e);
189
- const cortexConfig = {
190
- model: "smollm2-135m",
191
- temperature: 0.7,
192
- maxTokens: 128,
193
- gpu: true
194
- };
195
- const cortex = createCortex(cortexConfig);
196
- try {
197
- await cortex.init();
198
- return await cortex.embed(text);
199
- } catch (cortexError) {
200
- console.error("All embedding methods failed:", cortexError);
201
- return new Array(384).fill(0);
202
- }
203
- }
204
- };
205
- var getVectorTable = async (dbPath, tableName = "memories") => {
206
- if (!lancedb) await initVectorEngine();
207
- if (!fs2.existsSync(dbPath)) fs2.mkdirSync(dbPath, { recursive: true });
208
- const db = await lancedb.connect(dbPath);
209
- const tables = await db.tableNames();
210
- if (tables.includes(tableName)) {
211
- return await db.openTable(tableName);
212
- } else {
213
- return null;
214
- }
215
- };
216
- var createTable = async (dbPath, tableName, data) => {
217
- if (!lancedb) await initVectorEngine();
218
- const db = await lancedb.connect(dbPath);
219
- return await db.createTable(tableName, data);
220
- };
221
-
222
- // src/memory.ts
223
214
  var createMemoryItem = (text, type = "observation", importance = 0.5) => ({
224
215
  id: `mem_${Date.now()}_${Math.random().toString(36).substring(7)}`,
225
216
  text,
@@ -330,6 +321,7 @@ export {
330
321
  createCortex,
331
322
  createMemory,
332
323
  createMemoryItem,
324
+ createMemoryItemWithVector,
333
325
  createTable,
334
326
  generateEmbedding,
335
327
  getVectorTable,
package/package.json CHANGED
@@ -1,23 +1,25 @@
1
1
  {
2
2
  "name": "@forbocai/node",
3
- "version": "0.4.10",
3
+ "version": "0.5.1",
4
4
  "license": "UNLICENSED",
5
5
  "description": "Node.js native implementation for ForbocAI SDK",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
8
8
  "types": "dist/index.d.ts",
9
+ "bin": {
10
+ "forbocai": "dist/cli.js"
11
+ },
9
12
  "scripts": {
10
- "build": "tsup src/index.ts --format cjs,esm --dts",
11
- "dev": "tsup src/index.ts --watch",
13
+ "build": "tsup src/index.ts src/cli.ts --format cjs,esm --dts",
14
+ "dev": "tsup src/index.ts src/cli.ts --watch",
12
15
  "test": "vitest"
13
16
  },
14
17
  "dependencies": {
15
- "@forbocai/core": "^0.4.10",
18
+ "@forbocai/core": "^0.5.1",
16
19
  "@lancedb/lancedb": "^0.23.0",
17
- "@xenova/transformers": "^2.17.2",
20
+ "@huggingface/transformers": "^3.0.0",
18
21
  "apache-arrow": "^18.1.0",
19
- "node-llama-cpp": "^3.15.1",
20
- "onnxruntime-node": "^1.23.2"
22
+ "node-llama-cpp": "^3.15.1"
21
23
  },
22
24
  "devDependencies": {
23
25
  "tsup": "^8.5.1",