@forbocai/browser 0.5.4 → 0.5.7
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 +12 -6
- package/dist/index.d.ts +12 -6
- package/dist/index.js +33 -15
- package/dist/index.mjs +29 -11
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
|
-
import { CortexConfig, ICortex,
|
|
1
|
+
import { CortexConfig, ICortex, MemoryItem } from '@forbocai/core';
|
|
2
2
|
|
|
3
3
|
declare const createBrowserCortex: (config?: CortexConfig) => ICortex;
|
|
4
4
|
declare const createCortex: (config?: CortexConfig) => ICortex;
|
|
5
5
|
|
|
6
6
|
interface IBrowserMemory {
|
|
7
|
-
store(text: string, type?:
|
|
7
|
+
store(text: string, type?: string, importance?: number): Promise<MemoryItem>;
|
|
8
8
|
recall(query: string, limit?: number): Promise<MemoryItem[]>;
|
|
9
9
|
clear(): Promise<void>;
|
|
10
10
|
}
|
|
11
|
-
declare const createBrowserMemory: (config?:
|
|
12
|
-
|
|
11
|
+
declare const createBrowserMemory: (config?: {
|
|
12
|
+
decay?: "none" | "temporal";
|
|
13
|
+
maxContextWindow?: number;
|
|
14
|
+
}) => {
|
|
15
|
+
store: (text: string, type?: string, importance?: number) => Promise<MemoryItem>;
|
|
13
16
|
recall: (query: string, limit?: number) => Promise<MemoryItem[]>;
|
|
14
17
|
clear: () => Promise<void>;
|
|
15
18
|
};
|
|
16
|
-
declare const createMemory: (config?:
|
|
17
|
-
|
|
19
|
+
declare const createMemory: (config?: {
|
|
20
|
+
decay?: "none" | "temporal";
|
|
21
|
+
maxContextWindow?: number;
|
|
22
|
+
}) => {
|
|
23
|
+
store: (text: string, type?: string, importance?: number) => Promise<MemoryItem>;
|
|
18
24
|
recall: (query: string, limit?: number) => Promise<MemoryItem[]>;
|
|
19
25
|
clear: () => Promise<void>;
|
|
20
26
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
|
-
import { CortexConfig, ICortex,
|
|
1
|
+
import { CortexConfig, ICortex, MemoryItem } from '@forbocai/core';
|
|
2
2
|
|
|
3
3
|
declare const createBrowserCortex: (config?: CortexConfig) => ICortex;
|
|
4
4
|
declare const createCortex: (config?: CortexConfig) => ICortex;
|
|
5
5
|
|
|
6
6
|
interface IBrowserMemory {
|
|
7
|
-
store(text: string, type?:
|
|
7
|
+
store(text: string, type?: string, importance?: number): Promise<MemoryItem>;
|
|
8
8
|
recall(query: string, limit?: number): Promise<MemoryItem[]>;
|
|
9
9
|
clear(): Promise<void>;
|
|
10
10
|
}
|
|
11
|
-
declare const createBrowserMemory: (config?:
|
|
12
|
-
|
|
11
|
+
declare const createBrowserMemory: (config?: {
|
|
12
|
+
decay?: "none" | "temporal";
|
|
13
|
+
maxContextWindow?: number;
|
|
14
|
+
}) => {
|
|
15
|
+
store: (text: string, type?: string, importance?: number) => Promise<MemoryItem>;
|
|
13
16
|
recall: (query: string, limit?: number) => Promise<MemoryItem[]>;
|
|
14
17
|
clear: () => Promise<void>;
|
|
15
18
|
};
|
|
16
|
-
declare const createMemory: (config?:
|
|
17
|
-
|
|
19
|
+
declare const createMemory: (config?: {
|
|
20
|
+
decay?: "none" | "temporal";
|
|
21
|
+
maxContextWindow?: number;
|
|
22
|
+
}) => {
|
|
23
|
+
store: (text: string, type?: string, importance?: number) => Promise<MemoryItem>;
|
|
18
24
|
recall: (query: string, limit?: number) => Promise<MemoryItem[]>;
|
|
19
25
|
clear: () => Promise<void>;
|
|
20
26
|
};
|
package/dist/index.js
CHANGED
|
@@ -40,7 +40,6 @@ __export(index_exports, {
|
|
|
40
40
|
module.exports = __toCommonJS(index_exports);
|
|
41
41
|
|
|
42
42
|
// src/cortex.ts
|
|
43
|
-
var import_web_llm = require("@mlc-ai/web-llm");
|
|
44
43
|
var import_meta = {};
|
|
45
44
|
var DEFAULT_MODEL = "smollm2-135m";
|
|
46
45
|
var MODEL_ALIASES = {
|
|
@@ -61,13 +60,18 @@ var createBrowserCortex = (config = {}) => {
|
|
|
61
60
|
};
|
|
62
61
|
const init = async (onProgress) => {
|
|
63
62
|
if (status.ready) return status;
|
|
63
|
+
if (typeof window === "undefined") {
|
|
64
|
+
throw new Error("BrowserCortex can only be initialized in a browser environment");
|
|
65
|
+
}
|
|
66
|
+
const { CreateWebWorkerMLCEngine } = await import("@mlc-ai/web-llm");
|
|
64
67
|
const initProgressCallback = (report) => {
|
|
65
|
-
|
|
66
|
-
if (onProgress) {
|
|
68
|
+
const match = report.text?.match(/(\d+)%/);
|
|
69
|
+
if (onProgress && match) {
|
|
70
|
+
onProgress(parseInt(match[1], 10));
|
|
67
71
|
}
|
|
68
72
|
};
|
|
69
73
|
const modelId = resolveModelId(friendlyModel);
|
|
70
|
-
engine = await
|
|
74
|
+
engine = await CreateWebWorkerMLCEngine(
|
|
71
75
|
new Worker(new URL("./worker.js", import_meta.url), { type: "module" }),
|
|
72
76
|
modelId,
|
|
73
77
|
{ initProgressCallback }
|
|
@@ -110,21 +114,29 @@ var createBrowserCortex = (config = {}) => {
|
|
|
110
114
|
};
|
|
111
115
|
var createCortex = (config = {}) => createBrowserCortex(config);
|
|
112
116
|
|
|
113
|
-
// src/memory.ts
|
|
114
|
-
var import_orama = require("@orama/orama");
|
|
115
|
-
|
|
116
117
|
// src/vector.ts
|
|
117
118
|
var embedder = null;
|
|
118
119
|
var initVectorEngine = async () => {
|
|
119
120
|
if (embedder) return;
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
121
|
+
if (typeof window === "undefined") {
|
|
122
|
+
throw new Error("BrowserVectorEngine can only be initialized in a browser environment");
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
const { pipeline } = await import("@huggingface/transformers");
|
|
126
|
+
embedder = await pipeline("feature-extraction", "Xenova/all-MiniLM-L6-v2");
|
|
127
|
+
} catch (e) {
|
|
128
|
+
console.error("Failed to init Browser Vector Engine:", e);
|
|
129
|
+
}
|
|
123
130
|
};
|
|
124
131
|
var generateEmbedding = async (text) => {
|
|
125
132
|
if (!embedder) await initVectorEngine();
|
|
126
|
-
|
|
127
|
-
|
|
133
|
+
try {
|
|
134
|
+
const result = await embedder(text, { pooling: "mean", normalize: true });
|
|
135
|
+
return Array.from(result.data);
|
|
136
|
+
} catch (e) {
|
|
137
|
+
console.error("Browser embedding failed:", e);
|
|
138
|
+
return new Array(384).fill(0);
|
|
139
|
+
}
|
|
128
140
|
};
|
|
129
141
|
|
|
130
142
|
// src/memory.ts
|
|
@@ -132,7 +144,11 @@ var createBrowserMemory = (config = {}) => {
|
|
|
132
144
|
let db = null;
|
|
133
145
|
const init = async () => {
|
|
134
146
|
if (db) return db;
|
|
135
|
-
|
|
147
|
+
if (typeof window === "undefined") {
|
|
148
|
+
throw new Error("BrowserMemory can only be initialized in a browser environment");
|
|
149
|
+
}
|
|
150
|
+
const { create } = await import("@orama/orama");
|
|
151
|
+
db = await create({
|
|
136
152
|
schema: {
|
|
137
153
|
id: "string",
|
|
138
154
|
text: "string",
|
|
@@ -155,7 +171,8 @@ var createBrowserMemory = (config = {}) => {
|
|
|
155
171
|
importance
|
|
156
172
|
};
|
|
157
173
|
const embedding = await generateEmbedding(text);
|
|
158
|
-
await (
|
|
174
|
+
const { insert } = await import("@orama/orama");
|
|
175
|
+
await insert(instance, {
|
|
159
176
|
...item,
|
|
160
177
|
embedding
|
|
161
178
|
});
|
|
@@ -164,7 +181,8 @@ var createBrowserMemory = (config = {}) => {
|
|
|
164
181
|
const recall = async (query, limit = 5) => {
|
|
165
182
|
const instance = await init();
|
|
166
183
|
const embedding = await generateEmbedding(query);
|
|
167
|
-
const
|
|
184
|
+
const { search } = await import("@orama/orama");
|
|
185
|
+
const results = await search(instance, {
|
|
168
186
|
mode: "vector",
|
|
169
187
|
vector: {
|
|
170
188
|
value: embedding,
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
// src/cortex.ts
|
|
2
|
-
import { CreateWebWorkerMLCEngine } from "@mlc-ai/web-llm";
|
|
3
2
|
var DEFAULT_MODEL = "smollm2-135m";
|
|
4
3
|
var MODEL_ALIASES = {
|
|
5
4
|
"smollm2-135m": "SmolLM2-135M-Instruct-q0f16-MLC",
|
|
@@ -19,9 +18,14 @@ var createBrowserCortex = (config = {}) => {
|
|
|
19
18
|
};
|
|
20
19
|
const init = async (onProgress) => {
|
|
21
20
|
if (status.ready) return status;
|
|
21
|
+
if (typeof window === "undefined") {
|
|
22
|
+
throw new Error("BrowserCortex can only be initialized in a browser environment");
|
|
23
|
+
}
|
|
24
|
+
const { CreateWebWorkerMLCEngine } = await import("@mlc-ai/web-llm");
|
|
22
25
|
const initProgressCallback = (report) => {
|
|
23
|
-
|
|
24
|
-
if (onProgress) {
|
|
26
|
+
const match = report.text?.match(/(\d+)%/);
|
|
27
|
+
if (onProgress && match) {
|
|
28
|
+
onProgress(parseInt(match[1], 10));
|
|
25
29
|
}
|
|
26
30
|
};
|
|
27
31
|
const modelId = resolveModelId(friendlyModel);
|
|
@@ -68,21 +72,29 @@ var createBrowserCortex = (config = {}) => {
|
|
|
68
72
|
};
|
|
69
73
|
var createCortex = (config = {}) => createBrowserCortex(config);
|
|
70
74
|
|
|
71
|
-
// src/memory.ts
|
|
72
|
-
import { create, insert, search } from "@orama/orama";
|
|
73
|
-
|
|
74
75
|
// src/vector.ts
|
|
75
76
|
var embedder = null;
|
|
76
77
|
var initVectorEngine = async () => {
|
|
77
78
|
if (embedder) return;
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
if (typeof window === "undefined") {
|
|
80
|
+
throw new Error("BrowserVectorEngine can only be initialized in a browser environment");
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
const { pipeline } = await import("@huggingface/transformers");
|
|
84
|
+
embedder = await pipeline("feature-extraction", "Xenova/all-MiniLM-L6-v2");
|
|
85
|
+
} catch (e) {
|
|
86
|
+
console.error("Failed to init Browser Vector Engine:", e);
|
|
87
|
+
}
|
|
81
88
|
};
|
|
82
89
|
var generateEmbedding = async (text) => {
|
|
83
90
|
if (!embedder) await initVectorEngine();
|
|
84
|
-
|
|
85
|
-
|
|
91
|
+
try {
|
|
92
|
+
const result = await embedder(text, { pooling: "mean", normalize: true });
|
|
93
|
+
return Array.from(result.data);
|
|
94
|
+
} catch (e) {
|
|
95
|
+
console.error("Browser embedding failed:", e);
|
|
96
|
+
return new Array(384).fill(0);
|
|
97
|
+
}
|
|
86
98
|
};
|
|
87
99
|
|
|
88
100
|
// src/memory.ts
|
|
@@ -90,6 +102,10 @@ var createBrowserMemory = (config = {}) => {
|
|
|
90
102
|
let db = null;
|
|
91
103
|
const init = async () => {
|
|
92
104
|
if (db) return db;
|
|
105
|
+
if (typeof window === "undefined") {
|
|
106
|
+
throw new Error("BrowserMemory can only be initialized in a browser environment");
|
|
107
|
+
}
|
|
108
|
+
const { create } = await import("@orama/orama");
|
|
93
109
|
db = await create({
|
|
94
110
|
schema: {
|
|
95
111
|
id: "string",
|
|
@@ -113,6 +129,7 @@ var createBrowserMemory = (config = {}) => {
|
|
|
113
129
|
importance
|
|
114
130
|
};
|
|
115
131
|
const embedding = await generateEmbedding(text);
|
|
132
|
+
const { insert } = await import("@orama/orama");
|
|
116
133
|
await insert(instance, {
|
|
117
134
|
...item,
|
|
118
135
|
embedding
|
|
@@ -122,6 +139,7 @@ var createBrowserMemory = (config = {}) => {
|
|
|
122
139
|
const recall = async (query, limit = 5) => {
|
|
123
140
|
const instance = await init();
|
|
124
141
|
const embedding = await generateEmbedding(query);
|
|
142
|
+
const { search } = await import("@orama/orama");
|
|
125
143
|
const results = await search(instance, {
|
|
126
144
|
mode: "vector",
|
|
127
145
|
vector: {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@forbocai/browser",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.7",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"description": "Web Browser implementation for ForbocAI SDK",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"test": "vitest"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@forbocai/core": "^0.5.
|
|
15
|
+
"@forbocai/core": "^0.5.7",
|
|
16
16
|
"@mlc-ai/web-llm": "^0.2.46",
|
|
17
17
|
"@orama/orama": "^2.0.26",
|
|
18
18
|
"@huggingface/transformers": "^3.0.0"
|