react-brai 1.0.2 → 1.0.4
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.
Potentially problematic release.
This version of react-brai might be problematic. Click here for more details.
- package/package.json +28 -28
- package/src/useLocalAI.ts +93 -93
- package/tsconfig.json +19 -19
package/package.json
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "react-brai",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"main": "./dist/index.js",
|
|
5
|
-
"module": "./dist/index.mjs",
|
|
6
|
-
"types": "./dist/index.d.ts",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"build:worker": "tsup src/worker.ts --format esm --no-splitting --clean --dts",
|
|
9
|
-
"embed": "node scripts/embed-worker.js",
|
|
10
|
-
"build:lib": "tsup src/index.ts --format cjs,esm --dts --external react",
|
|
11
|
-
"build": "npm run build:worker && npm run embed && npm run build:lib"
|
|
12
|
-
},
|
|
13
|
-
"keywords": [],
|
|
14
|
-
"author": "",
|
|
15
|
-
"license": "ISC",
|
|
16
|
-
"description": "",
|
|
17
|
-
"dependencies": {
|
|
18
|
-
"@mlc-ai/web-llm": "^0.2.80"
|
|
19
|
-
},
|
|
20
|
-
"devDependencies": {
|
|
21
|
-
"@types/node": "^25.0.3",
|
|
22
|
-
"@types/react": "^19.2.7",
|
|
23
|
-
"@webgpu/types": "^0.1.68",
|
|
24
|
-
"react": "^19.2.3",
|
|
25
|
-
"tsup": "^8.5.1",
|
|
26
|
-
"typescript": "^5.9.3"
|
|
27
|
-
}
|
|
28
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "react-brai",
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"module": "./dist/index.mjs",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build:worker": "tsup src/worker.ts --format esm --no-splitting --clean --dts",
|
|
9
|
+
"embed": "node scripts/embed-worker.js",
|
|
10
|
+
"build:lib": "tsup src/index.ts --format cjs,esm --dts --external react",
|
|
11
|
+
"build": "npm run build:worker && npm run embed && npm run build:lib"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [],
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"description": "",
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@mlc-ai/web-llm": "^0.2.80"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/node": "^25.0.3",
|
|
22
|
+
"@types/react": "^19.2.7",
|
|
23
|
+
"@webgpu/types": "^0.1.68",
|
|
24
|
+
"react": "^19.2.3",
|
|
25
|
+
"tsup": "^8.5.1",
|
|
26
|
+
"typescript": "^5.9.3"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/useLocalAI.ts
CHANGED
|
@@ -1,94 +1,94 @@
|
|
|
1
|
-
// src/useLocalAI.ts
|
|
2
|
-
import { useState, useCallback, useEffect, useRef } from "react";
|
|
3
|
-
import { Message, InitProgressReport } from "./types";
|
|
4
|
-
// This file doesn't exist yet, but your "npm run build" script will generate it!
|
|
5
|
-
import { WORKER_CODE } from "./worker-embedded";
|
|
6
|
-
|
|
7
|
-
export function useLocalAI() {
|
|
8
|
-
const workerRef = useRef<Worker | null>(null);
|
|
9
|
-
|
|
10
|
-
const [isSupported, setIsSupported] = useState<boolean | null>(null);
|
|
11
|
-
|
|
12
|
-
const [isReady, setIsReady] = useState(false);
|
|
13
|
-
const [isLoading, setIsLoading] = useState(false);
|
|
14
|
-
const [response, setResponse] = useState("");
|
|
15
|
-
const [progress, setProgress] = useState<InitProgressReport | null>(null);
|
|
16
|
-
const [error, setError] = useState<string | null>(null);
|
|
17
|
-
|
|
18
|
-
// 1. AUTOMATIC INITIALIZATION (No more initWorker)
|
|
19
|
-
useEffect(() => {
|
|
20
|
-
if (!navigator.gpu) {
|
|
21
|
-
console.error("WebGPU is not supported on this device.");
|
|
22
|
-
setIsSupported(false);
|
|
23
|
-
setError("WebGPU is not supported on this device.");
|
|
24
|
-
return; // Stop here, don't load the worker
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
setIsSupported(true);
|
|
28
|
-
|
|
29
|
-
if (!WORKER_CODE) return;
|
|
30
|
-
|
|
31
|
-
// A. Create the worker from the embedded string
|
|
32
|
-
const blob = new Blob([WORKER_CODE], { type: 'application/javascript' });
|
|
33
|
-
const workerUrl = URL.createObjectURL(blob);
|
|
34
|
-
const worker = new Worker(workerUrl, { type: 'module' });
|
|
35
|
-
|
|
36
|
-
workerRef.current = worker;
|
|
37
|
-
|
|
38
|
-
// B. Set up the listeners immediately
|
|
39
|
-
worker.onmessage = (event) => {
|
|
40
|
-
const { type, payload } = event.data;
|
|
41
|
-
|
|
42
|
-
switch (type) {
|
|
43
|
-
case "PROGRESS":
|
|
44
|
-
setProgress(payload);
|
|
45
|
-
break;
|
|
46
|
-
case "READY":
|
|
47
|
-
setIsReady(true);
|
|
48
|
-
break;
|
|
49
|
-
case "TOKEN":
|
|
50
|
-
setResponse((prev) => prev + payload);
|
|
51
|
-
break;
|
|
52
|
-
case "DONE":
|
|
53
|
-
setIsLoading(false);
|
|
54
|
-
break;
|
|
55
|
-
case "ERROR":
|
|
56
|
-
setError(String(payload));
|
|
57
|
-
setIsLoading(false);
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
// C. Cleanup when the component unmounts
|
|
63
|
-
return () => {
|
|
64
|
-
worker.terminate();
|
|
65
|
-
URL.revokeObjectURL(workerUrl);
|
|
66
|
-
};
|
|
67
|
-
}, []);
|
|
68
|
-
|
|
69
|
-
const loadModel = useCallback((modelId: string) => {
|
|
70
|
-
console.log("~~~ Loading Model:", modelId);
|
|
71
|
-
if (!workerRef.current) return;
|
|
72
|
-
setError(null);
|
|
73
|
-
workerRef.current.postMessage({ type: "LOAD", modelId });
|
|
74
|
-
}, []);
|
|
75
|
-
|
|
76
|
-
const chat = useCallback((messages: Message[]) => {
|
|
77
|
-
if (!workerRef.current) return;
|
|
78
|
-
setIsLoading(true);
|
|
79
|
-
setResponse("");
|
|
80
|
-
workerRef.current.postMessage({ type: "GENERATE", messages });
|
|
81
|
-
}, []);
|
|
82
|
-
|
|
83
|
-
return {
|
|
84
|
-
// initWorker is gone!
|
|
85
|
-
isSupported,
|
|
86
|
-
loadModel,
|
|
87
|
-
chat,
|
|
88
|
-
isReady,
|
|
89
|
-
isLoading,
|
|
90
|
-
response,
|
|
91
|
-
progress,
|
|
92
|
-
error
|
|
93
|
-
};
|
|
1
|
+
// src/useLocalAI.ts
|
|
2
|
+
import { useState, useCallback, useEffect, useRef } from "react";
|
|
3
|
+
import { Message, InitProgressReport } from "./types";
|
|
4
|
+
// This file doesn't exist yet, but your "npm run build" script will generate it!
|
|
5
|
+
import { WORKER_CODE } from "./worker-embedded";
|
|
6
|
+
|
|
7
|
+
export function useLocalAI() {
|
|
8
|
+
const workerRef = useRef<Worker | null>(null);
|
|
9
|
+
|
|
10
|
+
const [isSupported, setIsSupported] = useState<boolean | null>(null);
|
|
11
|
+
|
|
12
|
+
const [isReady, setIsReady] = useState(false);
|
|
13
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
14
|
+
const [response, setResponse] = useState("");
|
|
15
|
+
const [progress, setProgress] = useState<InitProgressReport | null>(null);
|
|
16
|
+
const [error, setError] = useState<string | null>(null);
|
|
17
|
+
|
|
18
|
+
// 1. AUTOMATIC INITIALIZATION (No more initWorker)
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (!navigator.gpu) {
|
|
21
|
+
console.error("WebGPU is not supported on this device.");
|
|
22
|
+
setIsSupported(false);
|
|
23
|
+
setError("WebGPU is not supported on this device.");
|
|
24
|
+
return; // Stop here, don't load the worker
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
setIsSupported(true);
|
|
28
|
+
|
|
29
|
+
if (!WORKER_CODE) return;
|
|
30
|
+
|
|
31
|
+
// A. Create the worker from the embedded string
|
|
32
|
+
const blob = new Blob([WORKER_CODE], { type: 'application/javascript' });
|
|
33
|
+
const workerUrl = URL.createObjectURL(blob);
|
|
34
|
+
const worker = new Worker(workerUrl, { type: 'module' });
|
|
35
|
+
|
|
36
|
+
workerRef.current = worker;
|
|
37
|
+
|
|
38
|
+
// B. Set up the listeners immediately
|
|
39
|
+
worker.onmessage = (event) => {
|
|
40
|
+
const { type, payload } = event.data;
|
|
41
|
+
|
|
42
|
+
switch (type) {
|
|
43
|
+
case "PROGRESS":
|
|
44
|
+
setProgress(payload);
|
|
45
|
+
break;
|
|
46
|
+
case "READY":
|
|
47
|
+
setIsReady(true);
|
|
48
|
+
break;
|
|
49
|
+
case "TOKEN":
|
|
50
|
+
setResponse((prev) => prev + payload);
|
|
51
|
+
break;
|
|
52
|
+
case "DONE":
|
|
53
|
+
setIsLoading(false);
|
|
54
|
+
break;
|
|
55
|
+
case "ERROR":
|
|
56
|
+
setError(String(payload));
|
|
57
|
+
setIsLoading(false);
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// C. Cleanup when the component unmounts
|
|
63
|
+
return () => {
|
|
64
|
+
worker.terminate();
|
|
65
|
+
URL.revokeObjectURL(workerUrl);
|
|
66
|
+
};
|
|
67
|
+
}, []);
|
|
68
|
+
|
|
69
|
+
const loadModel = useCallback((modelId: string) => {
|
|
70
|
+
console.log("~~~ Loading Model:", modelId);
|
|
71
|
+
if (!workerRef.current) return;
|
|
72
|
+
setError(null);
|
|
73
|
+
workerRef.current.postMessage({ type: "LOAD", modelId });
|
|
74
|
+
}, []);
|
|
75
|
+
|
|
76
|
+
const chat = useCallback((messages: Message[]) => {
|
|
77
|
+
if (!workerRef.current) return;
|
|
78
|
+
setIsLoading(true);
|
|
79
|
+
setResponse("");
|
|
80
|
+
workerRef.current.postMessage({ type: "GENERATE", messages });
|
|
81
|
+
}, []);
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
// initWorker is gone!
|
|
85
|
+
isSupported,
|
|
86
|
+
loadModel,
|
|
87
|
+
chat,
|
|
88
|
+
isReady,
|
|
89
|
+
isLoading,
|
|
90
|
+
response,
|
|
91
|
+
progress,
|
|
92
|
+
error
|
|
93
|
+
};
|
|
94
94
|
}
|
package/tsconfig.json
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2017",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"lib": ["DOM", "DOM.Iterable", "ESNext"],
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"sourceMap": true,
|
|
8
|
-
"strict": true,
|
|
9
|
-
"moduleResolution": "node",
|
|
10
|
-
"allowSyntheticDefaultImports": true,
|
|
11
|
-
"esModuleInterop": true,
|
|
12
|
-
"skipLibCheck": true,
|
|
13
|
-
"forceConsistentCasingInFileNames": true,
|
|
14
|
-
"incremental": false, // <--- THIS IS THE FIX
|
|
15
|
-
"jsx": "react",
|
|
16
|
-
"types":["@webgpu/types"]
|
|
17
|
-
},
|
|
18
|
-
"include": ["src"],
|
|
19
|
-
"exclude": ["node_modules", "dist"]
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2017",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"lib": ["DOM", "DOM.Iterable", "ESNext"],
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"sourceMap": true,
|
|
8
|
+
"strict": true,
|
|
9
|
+
"moduleResolution": "node",
|
|
10
|
+
"allowSyntheticDefaultImports": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"forceConsistentCasingInFileNames": true,
|
|
14
|
+
"incremental": false, // <--- THIS IS THE FIX
|
|
15
|
+
"jsx": "react",
|
|
16
|
+
"types":["@webgpu/types"]
|
|
17
|
+
},
|
|
18
|
+
"include": ["src"],
|
|
19
|
+
"exclude": ["node_modules", "dist"]
|
|
20
20
|
}
|