aione-web 0.0.0-stage → 1.0.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/AIOne.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ export interface AIOneOptions {
2
+ workerUrl?: string;
3
+ name?: string;
4
+ }
5
+
6
+ export default class AIOne {
7
+ static create(options?: AIOneOptions): Promise<AIOne>;
8
+
9
+ loadModel(file: File): Promise<void>;
10
+ generate(prompt: string, maxTokens?: number): Promise<string>;
11
+ unloadModel(): Promise<void>;
12
+ close(): void;
13
+ }
14
+
15
+ export interface Message {
16
+ role: "user" | "assistant";
17
+ content: string;
18
+ }
package/AIOne.js ADDED
@@ -0,0 +1,104 @@
1
+ export default class AIOne {
2
+ static async create(options = {}) {
3
+ const workerUrl = options.workerUrl ?? "/aio1-worker.js";
4
+
5
+ if (typeof SharedWorker === "undefined") {
6
+ throw new Error("SharedWorker is not supported by this browser");
7
+ }
8
+
9
+ const worker = new SharedWorker(workerUrl, {
10
+ name: options.name ?? "AIOne",
11
+ type: "classic"
12
+ });
13
+
14
+ worker.onerror = event => {
15
+ console.error("AIOne SharedWorker error:", event);
16
+ };
17
+
18
+ const client = new AIOne(worker.port);
19
+ await client.ready();
20
+
21
+ return client;
22
+ }
23
+
24
+ constructor(port) {
25
+ this.port = port;
26
+ this.nextId = 0;
27
+ this.pending = new Map();
28
+
29
+ this.readyPromise = new Promise((resolve, reject) => {
30
+ this.resolveReady = resolve;
31
+ this.rejectReady = reject;
32
+ });
33
+
34
+ this.port.onmessage = ({ data }) => {
35
+ if (data.type === "ready") {
36
+ this.resolveReady();
37
+ return;
38
+ }
39
+
40
+ const pending = this.pending.get(data.id);
41
+ if (!pending) return;
42
+
43
+ this.pending.delete(data.id);
44
+
45
+ if (data.error) {
46
+ pending.reject(new Error(data.error));
47
+ } else {
48
+ pending.resolve(data.result);
49
+ }
50
+ };
51
+
52
+ this.port.onmessageerror = () => {
53
+ this.rejectReady(new Error("AIOne worker message error"));
54
+
55
+ for (const pending of this.pending.values()) {
56
+ pending.reject(new Error("AIOne worker message error"));
57
+ }
58
+
59
+ this.pending.clear();
60
+ };
61
+
62
+ this.port.start();
63
+ }
64
+
65
+ ready() {
66
+ return this.readyPromise;
67
+ }
68
+
69
+ call(method, args = {}) {
70
+ const id = ++this.nextId;
71
+
72
+ return new Promise((resolve, reject) => {
73
+ this.pending.set(id, { resolve, reject });
74
+
75
+ this.port.postMessage({
76
+ id,
77
+ method,
78
+ args
79
+ });
80
+ });
81
+ }
82
+
83
+ loadModel(file) {
84
+ return this.call("loadModel", { file });
85
+ }
86
+
87
+ generate(prompt, maxTokens = 512) {
88
+ return this.call("generate", { prompt, maxTokens });
89
+ }
90
+
91
+ unloadModel() {
92
+ return this.call("unloadModel");
93
+ }
94
+
95
+ close() {
96
+ this.port.close();
97
+
98
+ for (const pending of this.pending.values()) {
99
+ pending.reject(new Error("AIOne connection closed"));
100
+ }
101
+
102
+ this.pending.clear();
103
+ }
104
+ }