@moss-dev/moss-web 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/LICENSE.txt +168 -0
- package/README.md +123 -0
- package/dist/client.d.ts +38 -0
- package/dist/client.js +133 -0
- package/dist/embedder.d.ts +17 -0
- package/dist/embedder.js +85 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/types.d.ts +121 -0
- package/dist/types.js +1 -0
- package/package.json +49 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
Copyright (c) 2025 InferEdge Inc.
|
|
2
|
+
Required Notice: Copyright InferEdge Inc. (https://inferedge.dev)
|
|
3
|
+
Licensor Line of Business: InferEdge MOSS Runtime (https://inferedge.dev)
|
|
4
|
+
|
|
5
|
+
# PolyForm Shield License 1.0.0
|
|
6
|
+
|
|
7
|
+
<https://polyformproject.org/licenses/shield/1.0.0>
|
|
8
|
+
|
|
9
|
+
## Acceptance
|
|
10
|
+
|
|
11
|
+
In order to get any license under these terms, you must agree
|
|
12
|
+
to them as both strict obligations and conditions to all
|
|
13
|
+
your licenses.
|
|
14
|
+
|
|
15
|
+
## Copyright License
|
|
16
|
+
|
|
17
|
+
The licensor grants you a copyright license for the
|
|
18
|
+
software to do everything you might do with the software
|
|
19
|
+
that would otherwise infringe the licensor's copyright
|
|
20
|
+
in it for any permitted purpose. However, you may
|
|
21
|
+
only distribute the software according to [Distribution
|
|
22
|
+
License](#distribution-license) and make changes or new works
|
|
23
|
+
based on the software according to [Changes and New Works
|
|
24
|
+
License](#changes-and-new-works-license).
|
|
25
|
+
|
|
26
|
+
## Distribution License
|
|
27
|
+
|
|
28
|
+
The licensor grants you an additional copyright license
|
|
29
|
+
to distribute copies of the software. Your license
|
|
30
|
+
to distribute covers distributing the software with
|
|
31
|
+
changes and new works permitted by [Changes and New Works
|
|
32
|
+
License](#changes-and-new-works-license).
|
|
33
|
+
|
|
34
|
+
## Notices
|
|
35
|
+
|
|
36
|
+
You must ensure that anyone who gets a copy of any part of
|
|
37
|
+
the software from you also gets a copy of these terms or the
|
|
38
|
+
URL for them above, as well as copies of any plain-text lines
|
|
39
|
+
beginning with `Required Notice:` that the licensor provided
|
|
40
|
+
with the software. For example:
|
|
41
|
+
|
|
42
|
+
> Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
|
|
43
|
+
|
|
44
|
+
## Changes and New Works License
|
|
45
|
+
|
|
46
|
+
The licensor grants you an additional copyright license to
|
|
47
|
+
make changes and new works based on the software for any
|
|
48
|
+
permitted purpose.
|
|
49
|
+
|
|
50
|
+
## Patent License
|
|
51
|
+
|
|
52
|
+
The licensor grants you a patent license for the software that
|
|
53
|
+
covers patent claims the licensor can license, or becomes able
|
|
54
|
+
to license, that you would infringe by using the software.
|
|
55
|
+
|
|
56
|
+
## Noncompete
|
|
57
|
+
|
|
58
|
+
Any purpose is a permitted purpose, except for providing any
|
|
59
|
+
product that competes with the software or any product the
|
|
60
|
+
licensor or any of its affiliates provides using the software.
|
|
61
|
+
|
|
62
|
+
## Competition
|
|
63
|
+
|
|
64
|
+
Goods and services compete even when they provide functionality
|
|
65
|
+
through different kinds of interfaces or for different technical
|
|
66
|
+
platforms. Applications can compete with services, libraries
|
|
67
|
+
with plugins, frameworks with development tools, and so on,
|
|
68
|
+
even if they're written in different programming languages
|
|
69
|
+
or for different computer architectures. Goods and services
|
|
70
|
+
compete even when provided free of charge. If you market a
|
|
71
|
+
product as a practical substitute for the software or another
|
|
72
|
+
product, it definitely competes.
|
|
73
|
+
|
|
74
|
+
## New Products
|
|
75
|
+
|
|
76
|
+
If you are using the software to provide a product that does
|
|
77
|
+
not compete, but the licensor or any of its affiliates brings
|
|
78
|
+
your product into competition by providing a new version of
|
|
79
|
+
the software or another product using the software, you may
|
|
80
|
+
continue using versions of the software available under these
|
|
81
|
+
terms beforehand to provide your competing product, but not
|
|
82
|
+
any later versions.
|
|
83
|
+
|
|
84
|
+
## Discontinued Products
|
|
85
|
+
|
|
86
|
+
You may begin using the software to compete with a product
|
|
87
|
+
or service that the licensor or any of its affiliates has
|
|
88
|
+
stopped providing, unless the licensor includes a plain-text
|
|
89
|
+
line beginning with `Licensor Line of Business:` with the
|
|
90
|
+
software that mentions that line of business. For example:
|
|
91
|
+
|
|
92
|
+
> Licensor Line of Business: YoyodyneCMS Content Management
|
|
93
|
+
System (http://example.com/cms)
|
|
94
|
+
|
|
95
|
+
## Sales of Business
|
|
96
|
+
|
|
97
|
+
If the licensor or any of its affiliates sells a line of
|
|
98
|
+
business developing the software or using the software
|
|
99
|
+
to provide a product, the buyer can also enforce
|
|
100
|
+
[Noncompete](#noncompete) for that product.
|
|
101
|
+
|
|
102
|
+
## Fair Use
|
|
103
|
+
|
|
104
|
+
You may have "fair use" rights for the software under the
|
|
105
|
+
law. These terms do not limit them.
|
|
106
|
+
|
|
107
|
+
## No Other Rights
|
|
108
|
+
|
|
109
|
+
These terms do not allow you to sublicense or transfer any of
|
|
110
|
+
your licenses to anyone else, or prevent the licensor from
|
|
111
|
+
granting licenses to anyone else. These terms do not imply
|
|
112
|
+
any other licenses.
|
|
113
|
+
|
|
114
|
+
## Patent Defense
|
|
115
|
+
|
|
116
|
+
If you make any written claim that the software infringes or
|
|
117
|
+
contributes to infringement of any patent, your patent license
|
|
118
|
+
for the software granted under these terms ends immediately. If
|
|
119
|
+
your company makes such a claim, your patent license ends
|
|
120
|
+
immediately for work on behalf of your company.
|
|
121
|
+
|
|
122
|
+
## Violations
|
|
123
|
+
|
|
124
|
+
The first time you are notified in writing that you have
|
|
125
|
+
violated any of these terms, or done anything with the software
|
|
126
|
+
not covered by your licenses, your licenses can nonetheless
|
|
127
|
+
continue if you come into full compliance with these terms,
|
|
128
|
+
and take practical steps to correct past violations, within
|
|
129
|
+
32 days of receiving notice. Otherwise, all your licenses
|
|
130
|
+
end immediately.
|
|
131
|
+
|
|
132
|
+
## No Liability
|
|
133
|
+
|
|
134
|
+
***As far as the law allows, the software comes as is, without
|
|
135
|
+
any warranty or condition, and the licensor will not be liable
|
|
136
|
+
to you for any damages arising out of these terms or the use
|
|
137
|
+
or nature of the software, under any kind of legal claim.***
|
|
138
|
+
|
|
139
|
+
## Definitions
|
|
140
|
+
|
|
141
|
+
The **licensor** is the individual or entity offering these
|
|
142
|
+
terms, and the **software** is the software the licensor makes
|
|
143
|
+
available under these terms.
|
|
144
|
+
|
|
145
|
+
A **product** can be a good or service, or a combination
|
|
146
|
+
of them.
|
|
147
|
+
|
|
148
|
+
**You** refers to the individual or entity agreeing to these
|
|
149
|
+
terms.
|
|
150
|
+
|
|
151
|
+
**Your company** is any legal entity, sole proprietorship,
|
|
152
|
+
or other kind of organization that you work for, plus all
|
|
153
|
+
its affiliates.
|
|
154
|
+
|
|
155
|
+
**Affiliates** means the other organizations than an
|
|
156
|
+
organization has control over, is under the control of, or is
|
|
157
|
+
under common control with.
|
|
158
|
+
|
|
159
|
+
**Control** means ownership of substantially all the assets of
|
|
160
|
+
an entity, or the power to direct its management and policies
|
|
161
|
+
by vote, contract, or otherwise. Control can be direct or
|
|
162
|
+
indirect.
|
|
163
|
+
|
|
164
|
+
**Your licenses** are all the licenses granted to you for the
|
|
165
|
+
software under these terms.
|
|
166
|
+
|
|
167
|
+
**Use** means anything you do with the software requiring one
|
|
168
|
+
of your licenses.
|
package/README.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# Moss client library for the browser
|
|
2
|
+
|
|
3
|
+
`@moss-dev/moss-web` enables **private, in-browser semantic search** — no server round-trips for queries.
|
|
4
|
+
|
|
5
|
+
Built on WebAssembly for near-native performance in any modern browser.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **In-Browser Vector Search** — Sub-millisecond retrieval with zero network latency
|
|
10
|
+
- **Semantic & Hybrid Search** — Beyond keyword matching
|
|
11
|
+
- **Multi-Index Support** — Manage multiple isolated search spaces
|
|
12
|
+
- **Full CRUD** — Create, update, and delete indexes and documents from the browser
|
|
13
|
+
- **Privacy-First** — Queries run entirely in the browser, no data leaves the device
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @moss-dev/moss-web
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { MossClient } from "@moss-dev/moss-web";
|
|
25
|
+
|
|
26
|
+
// Initialize client (WASM/model download happens automatically on first use)
|
|
27
|
+
const client = new MossClient("your-project-id", "your-project-key");
|
|
28
|
+
|
|
29
|
+
// Create an index with documents
|
|
30
|
+
await client.createIndex("knowledge-base", [
|
|
31
|
+
{ id: "1", text: "Machine learning fundamentals" },
|
|
32
|
+
{ id: "2", text: "Deep learning neural networks" },
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
// Load the index into browser memory for fast local queries
|
|
36
|
+
await client.loadIndex("knowledge-base");
|
|
37
|
+
|
|
38
|
+
// Query — runs entirely in-browser
|
|
39
|
+
const results = await client.query("knowledge-base", "AI and neural networks");
|
|
40
|
+
results.docs.forEach((doc) => {
|
|
41
|
+
console.log(`${doc.id}: ${doc.text} (score: ${doc.score})`);
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## API
|
|
46
|
+
|
|
47
|
+
### Creating a Client
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
// Simple: lazy initialization (WASM/model loads on first API call)
|
|
51
|
+
const client = new MossClient(projectId, projectKey, options?);
|
|
52
|
+
|
|
53
|
+
// Alternative: eager initialization (WASM/model loads immediately)
|
|
54
|
+
const client = await MossClient.create(projectId, projectKey, options?);
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
**Options:**
|
|
58
|
+
|
|
59
|
+
- `model` — `"moss-minilm"` (default, fast, for most use-cases) or `"moss-mediumlm"`
|
|
60
|
+
- `baseUrl` — Custom API base URL (for self-hosted Moss instances)
|
|
61
|
+
|
|
62
|
+
### Index Management
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
// Create index with documents
|
|
66
|
+
await client.createIndex(name, docs, options?);
|
|
67
|
+
|
|
68
|
+
// Create index from files
|
|
69
|
+
await client.createIndexFromFiles(name, files, options?);
|
|
70
|
+
|
|
71
|
+
// Add or update documents
|
|
72
|
+
await client.addDocs(name, docs, options?);
|
|
73
|
+
|
|
74
|
+
// Delete documents by ID
|
|
75
|
+
await client.deleteDocs(name, docIds, options?);
|
|
76
|
+
|
|
77
|
+
// Get index metadata
|
|
78
|
+
await client.getIndex(name);
|
|
79
|
+
|
|
80
|
+
// List all indexes
|
|
81
|
+
await client.listIndexes();
|
|
82
|
+
|
|
83
|
+
// Delete an index
|
|
84
|
+
await client.deleteIndex(name);
|
|
85
|
+
|
|
86
|
+
// Get documents from an index
|
|
87
|
+
await client.getDocs(name, options?);
|
|
88
|
+
|
|
89
|
+
// Check job status for async operations
|
|
90
|
+
await client.getJobStatus(jobId);
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Local Search
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
// Load index into browser memory for querying
|
|
97
|
+
await client.loadIndex(name, options?);
|
|
98
|
+
|
|
99
|
+
// Check if index is loaded locally
|
|
100
|
+
await client.hasIndex(name);
|
|
101
|
+
|
|
102
|
+
// Get local index info
|
|
103
|
+
await client.getIndexInfo(name);
|
|
104
|
+
|
|
105
|
+
// Query loaded index (runs in-browser)
|
|
106
|
+
await client.query(name, queryText, options?);
|
|
107
|
+
|
|
108
|
+
// Refresh index from server
|
|
109
|
+
await client.refreshIndex(name);
|
|
110
|
+
|
|
111
|
+
// Unload index from browser memory
|
|
112
|
+
await client.unloadIndex(name);
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Cleanup
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
client.dispose();
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
See [LICENSE](./LICENSE.txt).
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { MossClientOptions, IndexInfo, SearchResult, QueryOptions, RefreshResult, DocumentInfo, MutationResult, CreateIndexOptions, MutationOptions, ParseFileInput, JobStatusResponse, GetDocumentsOptions, LoadIndexOptions } from "./types";
|
|
2
|
+
export declare class MossClient {
|
|
3
|
+
private readonly projectId;
|
|
4
|
+
private readonly projectKey;
|
|
5
|
+
private readonly options;
|
|
6
|
+
private initialized;
|
|
7
|
+
private indexManager;
|
|
8
|
+
private manageClient;
|
|
9
|
+
private embedder;
|
|
10
|
+
private initPromise;
|
|
11
|
+
constructor(projectId: string, projectKey: string, options?: MossClientOptions);
|
|
12
|
+
/**
|
|
13
|
+
* Create and initialize a new MossClient instance.
|
|
14
|
+
* This is the recommended way to create a client as it ensures
|
|
15
|
+
* the WASM modules are loaded before returning.
|
|
16
|
+
*/
|
|
17
|
+
static create(projectId: string, projectKey: string, options?: MossClientOptions): Promise<MossClient>;
|
|
18
|
+
private ensureInitialized;
|
|
19
|
+
private doInit;
|
|
20
|
+
private getIndexManager;
|
|
21
|
+
private getManageClient;
|
|
22
|
+
createIndex(indexName: string, docs: DocumentInfo[], options?: CreateIndexOptions): Promise<MutationResult>;
|
|
23
|
+
createIndexFromFiles(indexName: string, files: ParseFileInput[], options?: CreateIndexOptions): Promise<MutationResult>;
|
|
24
|
+
addDocs(indexName: string, docs: DocumentInfo[], options?: MutationOptions): Promise<MutationResult>;
|
|
25
|
+
deleteDocs(indexName: string, docIds: string[], options?: MutationOptions): Promise<MutationResult>;
|
|
26
|
+
getIndex(indexName: string): Promise<IndexInfo>;
|
|
27
|
+
listIndexes(): Promise<IndexInfo[]>;
|
|
28
|
+
deleteIndex(indexName: string): Promise<boolean>;
|
|
29
|
+
getDocs(indexName: string, options?: GetDocumentsOptions): Promise<DocumentInfo[]>;
|
|
30
|
+
getJobStatus(jobId: string): Promise<JobStatusResponse>;
|
|
31
|
+
loadIndex(indexName: string, _options?: LoadIndexOptions): Promise<string>;
|
|
32
|
+
unloadIndex(indexName: string): Promise<void>;
|
|
33
|
+
hasIndex(indexName: string): Promise<boolean>;
|
|
34
|
+
getIndexInfo(indexName: string): Promise<IndexInfo>;
|
|
35
|
+
refreshIndex(indexName: string): Promise<RefreshResult>;
|
|
36
|
+
query(indexName: string, query: string, options?: QueryOptions): Promise<SearchResult>;
|
|
37
|
+
dispose(): void;
|
|
38
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import wasmInit, { IndexManager, ManageClient, Tokenizer as WasmTokenizer, poolAndNormalize, } from "@moss-dev/moss-wasm";
|
|
2
|
+
import { Embedder } from "./embedder";
|
|
3
|
+
const DEFAULT_MODEL = "moss-minilm";
|
|
4
|
+
export class MossClient {
|
|
5
|
+
constructor(projectId, projectKey, options) {
|
|
6
|
+
this.initialized = false;
|
|
7
|
+
this.indexManager = null;
|
|
8
|
+
this.manageClient = null;
|
|
9
|
+
this.embedder = null;
|
|
10
|
+
this.initPromise = null;
|
|
11
|
+
this.projectId = projectId;
|
|
12
|
+
this.projectKey = projectKey;
|
|
13
|
+
this.options = options ?? {};
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Create and initialize a new MossClient instance.
|
|
17
|
+
* This is the recommended way to create a client as it ensures
|
|
18
|
+
* the WASM modules are loaded before returning.
|
|
19
|
+
*/
|
|
20
|
+
static async create(projectId, projectKey, options) {
|
|
21
|
+
const client = new MossClient(projectId, projectKey, options);
|
|
22
|
+
await client.ensureInitialized();
|
|
23
|
+
return client;
|
|
24
|
+
}
|
|
25
|
+
async ensureInitialized() {
|
|
26
|
+
if (this.initialized)
|
|
27
|
+
return;
|
|
28
|
+
if (this.initPromise)
|
|
29
|
+
return this.initPromise;
|
|
30
|
+
this.initPromise = this.doInit();
|
|
31
|
+
return this.initPromise;
|
|
32
|
+
}
|
|
33
|
+
async doInit() {
|
|
34
|
+
await wasmInit(this.options.wasmUrl);
|
|
35
|
+
if (this.options.baseUrl) {
|
|
36
|
+
this.indexManager = IndexManager.withBaseUrl(this.projectId, this.projectKey, this.options.baseUrl);
|
|
37
|
+
this.manageClient = ManageClient.withBaseUrl(this.projectId, this.projectKey, this.options.baseUrl);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
this.indexManager = new IndexManager(this.projectId, this.projectKey);
|
|
41
|
+
this.manageClient = new ManageClient(this.projectId, this.projectKey);
|
|
42
|
+
}
|
|
43
|
+
const model = this.options.model ?? DEFAULT_MODEL;
|
|
44
|
+
this.embedder = new Embedder(model, WasmTokenizer, poolAndNormalize);
|
|
45
|
+
await this.embedder.init(this.options.onnxWasmPath);
|
|
46
|
+
this.initialized = true;
|
|
47
|
+
}
|
|
48
|
+
async getIndexManager() {
|
|
49
|
+
await this.ensureInitialized();
|
|
50
|
+
return this.indexManager;
|
|
51
|
+
}
|
|
52
|
+
async getManageClient() {
|
|
53
|
+
await this.ensureInitialized();
|
|
54
|
+
return this.manageClient;
|
|
55
|
+
}
|
|
56
|
+
// ── Index management (CRUD) ────────────────────────────────
|
|
57
|
+
async createIndex(indexName, docs, options) {
|
|
58
|
+
const manage = await this.getManageClient();
|
|
59
|
+
const hasEmbeddings = docs.some((d) => Array.isArray(d.embedding) && d.embedding.length > 0);
|
|
60
|
+
const modelId = options?.modelId ?? (hasEmbeddings ? "custom" : DEFAULT_MODEL);
|
|
61
|
+
return manage.createIndex(indexName, docs, modelId);
|
|
62
|
+
}
|
|
63
|
+
async createIndexFromFiles(indexName, files, options) {
|
|
64
|
+
const manage = await this.getManageClient();
|
|
65
|
+
const modelId = options?.modelId ?? DEFAULT_MODEL;
|
|
66
|
+
return manage.createIndexFromFiles(indexName, files, modelId);
|
|
67
|
+
}
|
|
68
|
+
async addDocs(indexName, docs, options) {
|
|
69
|
+
const manage = await this.getManageClient();
|
|
70
|
+
return manage.addDocs(indexName, docs, options?.upsert);
|
|
71
|
+
}
|
|
72
|
+
async deleteDocs(indexName, docIds, options) {
|
|
73
|
+
const manage = await this.getManageClient();
|
|
74
|
+
return manage.deleteDocs(indexName, docIds);
|
|
75
|
+
}
|
|
76
|
+
async getIndex(indexName) {
|
|
77
|
+
const manage = await this.getManageClient();
|
|
78
|
+
return manage.getIndex(indexName);
|
|
79
|
+
}
|
|
80
|
+
async listIndexes() {
|
|
81
|
+
const manage = await this.getManageClient();
|
|
82
|
+
return manage.listIndexes();
|
|
83
|
+
}
|
|
84
|
+
async deleteIndex(indexName) {
|
|
85
|
+
const manage = await this.getManageClient();
|
|
86
|
+
return manage.deleteIndex(indexName);
|
|
87
|
+
}
|
|
88
|
+
async getDocs(indexName, options) {
|
|
89
|
+
const manage = await this.getManageClient();
|
|
90
|
+
return manage.getDocs(indexName, options?.docIds);
|
|
91
|
+
}
|
|
92
|
+
async getJobStatus(jobId) {
|
|
93
|
+
const manage = await this.getManageClient();
|
|
94
|
+
return manage.getJobStatus(jobId);
|
|
95
|
+
}
|
|
96
|
+
// ── Local index operations ─────────────────────────────────
|
|
97
|
+
async loadIndex(indexName, _options) {
|
|
98
|
+
const im = await this.getIndexManager();
|
|
99
|
+
await im.loadIndex(indexName);
|
|
100
|
+
return indexName;
|
|
101
|
+
}
|
|
102
|
+
async unloadIndex(indexName) {
|
|
103
|
+
const im = await this.getIndexManager();
|
|
104
|
+
return im.unloadIndex(indexName);
|
|
105
|
+
}
|
|
106
|
+
async hasIndex(indexName) {
|
|
107
|
+
const im = await this.getIndexManager();
|
|
108
|
+
return im.hasIndex(indexName);
|
|
109
|
+
}
|
|
110
|
+
async getIndexInfo(indexName) {
|
|
111
|
+
const im = await this.getIndexManager();
|
|
112
|
+
return im.getIndexInfo(indexName);
|
|
113
|
+
}
|
|
114
|
+
async refreshIndex(indexName) {
|
|
115
|
+
const im = await this.getIndexManager();
|
|
116
|
+
return im.refreshIndex(indexName);
|
|
117
|
+
}
|
|
118
|
+
async query(indexName, query, options) {
|
|
119
|
+
await this.ensureInitialized();
|
|
120
|
+
const embedding = await this.embedder.embed(query);
|
|
121
|
+
return this.indexManager.query(indexName, query, embedding, options?.topK, options?.alpha, options?.filter);
|
|
122
|
+
}
|
|
123
|
+
dispose() {
|
|
124
|
+
this.embedder?.dispose();
|
|
125
|
+
this.indexManager?.free();
|
|
126
|
+
this.manageClient?.free();
|
|
127
|
+
this.initialized = false;
|
|
128
|
+
this.indexManager = null;
|
|
129
|
+
this.manageClient = null;
|
|
130
|
+
this.embedder = null;
|
|
131
|
+
this.initPromise = null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Tokenizer, poolAndNormalize as PoolAndNormalizeFn } from "@moss-dev/moss-wasm";
|
|
2
|
+
import type { MossModel } from "./types";
|
|
3
|
+
export declare class Embedder {
|
|
4
|
+
private session;
|
|
5
|
+
private tokenizer;
|
|
6
|
+
private readonly modelId;
|
|
7
|
+
private readonly hiddenDim;
|
|
8
|
+
private initPromise;
|
|
9
|
+
private readonly wasmTokenizerCtor;
|
|
10
|
+
private readonly poolAndNormalize;
|
|
11
|
+
constructor(modelId: MossModel, wasmTokenizerCtor: typeof Tokenizer, poolAndNormalize: typeof PoolAndNormalizeFn);
|
|
12
|
+
init(onnxWasmPath?: string): Promise<void>;
|
|
13
|
+
private _init;
|
|
14
|
+
embed(text: string): Promise<Float32Array>;
|
|
15
|
+
private poolResult;
|
|
16
|
+
dispose(): void;
|
|
17
|
+
}
|
package/dist/embedder.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import * as ort from "onnxruntime-web";
|
|
2
|
+
const CDN_BASE = "https://models.moss.link/";
|
|
3
|
+
const MODEL_CONFIGS = {
|
|
4
|
+
"moss-minilm": { hiddenDim: 384 },
|
|
5
|
+
"moss-mediumlm": { hiddenDim: 1024 },
|
|
6
|
+
};
|
|
7
|
+
export class Embedder {
|
|
8
|
+
constructor(modelId, wasmTokenizerCtor, poolAndNormalize) {
|
|
9
|
+
this.session = null;
|
|
10
|
+
this.tokenizer = null;
|
|
11
|
+
this.initPromise = null;
|
|
12
|
+
this.modelId = modelId;
|
|
13
|
+
this.hiddenDim = MODEL_CONFIGS[modelId].hiddenDim;
|
|
14
|
+
this.wasmTokenizerCtor = wasmTokenizerCtor;
|
|
15
|
+
this.poolAndNormalize = poolAndNormalize;
|
|
16
|
+
}
|
|
17
|
+
async init(onnxWasmPath) {
|
|
18
|
+
if (this.initPromise)
|
|
19
|
+
return this.initPromise;
|
|
20
|
+
this.initPromise = this._init(onnxWasmPath);
|
|
21
|
+
return this.initPromise;
|
|
22
|
+
}
|
|
23
|
+
async _init(onnxWasmPath) {
|
|
24
|
+
if (onnxWasmPath) {
|
|
25
|
+
ort.env.wasm.wasmPaths = onnxWasmPath;
|
|
26
|
+
}
|
|
27
|
+
const modelUrl = `${CDN_BASE}${this.modelId}/`;
|
|
28
|
+
const [tokenizerBytes, onnxModel] = await Promise.all([
|
|
29
|
+
fetch(`${modelUrl}tokenizer.json`).then((r) => {
|
|
30
|
+
if (!r.ok)
|
|
31
|
+
throw new Error(`Failed to fetch tokenizer.json: ${r.status}`);
|
|
32
|
+
return r.arrayBuffer();
|
|
33
|
+
}),
|
|
34
|
+
fetch(`${modelUrl}onnx/model.onnx`).then((r) => {
|
|
35
|
+
if (!r.ok)
|
|
36
|
+
throw new Error(`Failed to fetch model.onnx: ${r.status}`);
|
|
37
|
+
return r.arrayBuffer();
|
|
38
|
+
}),
|
|
39
|
+
]);
|
|
40
|
+
this.tokenizer = new this.wasmTokenizerCtor(new Uint8Array(tokenizerBytes));
|
|
41
|
+
this.session = await ort.InferenceSession.create(onnxModel, {
|
|
42
|
+
executionProviders: ["wasm"],
|
|
43
|
+
graphOptimizationLevel: "all",
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
async embed(text) {
|
|
47
|
+
if (!this.tokenizer || !this.session) {
|
|
48
|
+
throw new Error("Embedder not initialized — call init() first");
|
|
49
|
+
}
|
|
50
|
+
const tokens = this.tokenizer.tokenize(text);
|
|
51
|
+
const seqLen = tokens.inputIds.length;
|
|
52
|
+
const inputIdsTensor = new ort.Tensor("int64", BigInt64Array.from(tokens.inputIds.map(BigInt)), [1, seqLen]);
|
|
53
|
+
const attentionMaskTensor = new ort.Tensor("int64", BigInt64Array.from(tokens.attentionMask.map(BigInt)), [1, seqLen]);
|
|
54
|
+
const tokenTypeIdsTensor = new ort.Tensor("int64", BigInt64Array.from(tokens.tokenTypeIds.map(BigInt)), [1, seqLen]);
|
|
55
|
+
const feeds = {
|
|
56
|
+
input_ids: inputIdsTensor,
|
|
57
|
+
attention_mask: attentionMaskTensor,
|
|
58
|
+
token_type_ids: tokenTypeIdsTensor,
|
|
59
|
+
};
|
|
60
|
+
const output = await this.session.run(feeds);
|
|
61
|
+
const lastHiddenState = output["last_hidden_state"];
|
|
62
|
+
if (!lastHiddenState) {
|
|
63
|
+
const firstKey = Object.keys(output)[0];
|
|
64
|
+
if (!firstKey)
|
|
65
|
+
throw new Error("ONNX model returned no outputs");
|
|
66
|
+
const fallback = output[firstKey];
|
|
67
|
+
return this.poolResult(fallback.data, tokens.attentionMask);
|
|
68
|
+
}
|
|
69
|
+
return this.poolResult(lastHiddenState.data, tokens.attentionMask);
|
|
70
|
+
}
|
|
71
|
+
poolResult(tokenEmbeddings, attentionMask) {
|
|
72
|
+
return this.poolAndNormalize(tokenEmbeddings, new Int32Array(attentionMask), this.hiddenDim);
|
|
73
|
+
}
|
|
74
|
+
dispose() {
|
|
75
|
+
if (this.tokenizer) {
|
|
76
|
+
this.tokenizer.free();
|
|
77
|
+
this.tokenizer = null;
|
|
78
|
+
}
|
|
79
|
+
if (this.session) {
|
|
80
|
+
this.session.release();
|
|
81
|
+
this.session = null;
|
|
82
|
+
}
|
|
83
|
+
this.initPromise = null;
|
|
84
|
+
}
|
|
85
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { MossClient } from "./client";
|
|
2
|
+
export type { MossClientOptions, MossModel, IndexInfo, SearchResult, QueryResultDocumentInfo, QueryOptions, MetadataFilter, FilterCondition, RefreshResult, ModelRef, DocumentInfo, MutationResult, CreateIndexOptions, MutationOptions, ParseFileInput, JobStatus, JobPhase, JobProgress, JobStatusResponse, GetDocumentsOptions, LoadIndexOptions, ISODate, } from "./types";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { MossClient } from "./client";
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
export type MossModel = "moss-minilm" | "moss-mediumlm";
|
|
2
|
+
export interface MossClientOptions {
|
|
3
|
+
baseUrl?: string;
|
|
4
|
+
model?: MossModel;
|
|
5
|
+
wasmUrl?: string;
|
|
6
|
+
onnxWasmPath?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface ModelRef {
|
|
9
|
+
id: string;
|
|
10
|
+
version?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface IndexInfo {
|
|
13
|
+
id: string;
|
|
14
|
+
name: string;
|
|
15
|
+
version?: string;
|
|
16
|
+
status: string;
|
|
17
|
+
docCount: number;
|
|
18
|
+
createdAt?: string;
|
|
19
|
+
updatedAt?: string;
|
|
20
|
+
model: ModelRef;
|
|
21
|
+
}
|
|
22
|
+
export interface QueryResultDocumentInfo {
|
|
23
|
+
id: string;
|
|
24
|
+
text: string;
|
|
25
|
+
metadata?: Record<string, string>;
|
|
26
|
+
score: number;
|
|
27
|
+
}
|
|
28
|
+
export interface SearchResult {
|
|
29
|
+
docs: QueryResultDocumentInfo[];
|
|
30
|
+
query: string;
|
|
31
|
+
indexName?: string;
|
|
32
|
+
timeTakenMs?: number;
|
|
33
|
+
}
|
|
34
|
+
export interface QueryOptions {
|
|
35
|
+
topK?: number;
|
|
36
|
+
alpha?: number;
|
|
37
|
+
filter?: MetadataFilter;
|
|
38
|
+
}
|
|
39
|
+
export type MetadataFilter = {
|
|
40
|
+
field: string;
|
|
41
|
+
condition: FilterCondition;
|
|
42
|
+
} | {
|
|
43
|
+
$and: MetadataFilter[];
|
|
44
|
+
} | {
|
|
45
|
+
$or: MetadataFilter[];
|
|
46
|
+
};
|
|
47
|
+
export type FilterCondition = {
|
|
48
|
+
$eq: string | number;
|
|
49
|
+
} | {
|
|
50
|
+
$ne: string | number;
|
|
51
|
+
} | {
|
|
52
|
+
$gt: string | number;
|
|
53
|
+
} | {
|
|
54
|
+
$gte: string | number;
|
|
55
|
+
} | {
|
|
56
|
+
$lt: string | number;
|
|
57
|
+
} | {
|
|
58
|
+
$lte: string | number;
|
|
59
|
+
} | {
|
|
60
|
+
$in: (string | number)[];
|
|
61
|
+
} | {
|
|
62
|
+
$nin: (string | number)[];
|
|
63
|
+
} | {
|
|
64
|
+
$near: string | number;
|
|
65
|
+
};
|
|
66
|
+
export interface RefreshResult {
|
|
67
|
+
indexName: string;
|
|
68
|
+
previousUpdatedAt: string;
|
|
69
|
+
newUpdatedAt: string;
|
|
70
|
+
wasUpdated: boolean;
|
|
71
|
+
}
|
|
72
|
+
export interface DocumentInfo {
|
|
73
|
+
id: string;
|
|
74
|
+
text: string;
|
|
75
|
+
metadata?: Record<string, string>;
|
|
76
|
+
embedding?: number[];
|
|
77
|
+
}
|
|
78
|
+
export interface MutationResult {
|
|
79
|
+
jobId: string;
|
|
80
|
+
indexName: string;
|
|
81
|
+
docCount: number;
|
|
82
|
+
}
|
|
83
|
+
export interface CreateIndexOptions {
|
|
84
|
+
modelId?: MossModel | "custom";
|
|
85
|
+
onProgress?: (progress: JobProgress) => void;
|
|
86
|
+
}
|
|
87
|
+
export interface MutationOptions {
|
|
88
|
+
upsert?: boolean;
|
|
89
|
+
onProgress?: (progress: JobProgress) => void;
|
|
90
|
+
}
|
|
91
|
+
export interface ParseFileInput {
|
|
92
|
+
name: string;
|
|
93
|
+
data: Uint8Array;
|
|
94
|
+
contentType: string;
|
|
95
|
+
}
|
|
96
|
+
export type JobStatus = "pending_upload" | "uploading" | "building" | "completed" | "failed";
|
|
97
|
+
export type JobPhase = "downloading" | "deserializing" | "generating_embeddings" | "building_index" | "uploading" | "cleanup" | "waiting_for_parser" | "parsing" | "parsing_complete" | "queued";
|
|
98
|
+
export interface JobProgress {
|
|
99
|
+
jobId: string;
|
|
100
|
+
status: JobStatus;
|
|
101
|
+
progress: number;
|
|
102
|
+
currentPhase: JobPhase | null;
|
|
103
|
+
}
|
|
104
|
+
export interface JobStatusResponse {
|
|
105
|
+
jobId: string;
|
|
106
|
+
status: JobStatus;
|
|
107
|
+
progress: number;
|
|
108
|
+
currentPhase?: JobPhase;
|
|
109
|
+
error?: string;
|
|
110
|
+
createdAt: string;
|
|
111
|
+
updatedAt: string;
|
|
112
|
+
completedAt?: string;
|
|
113
|
+
}
|
|
114
|
+
export interface GetDocumentsOptions {
|
|
115
|
+
docIds?: string[];
|
|
116
|
+
}
|
|
117
|
+
export interface LoadIndexOptions {
|
|
118
|
+
autoRefresh?: boolean;
|
|
119
|
+
pollingIntervalInSeconds?: number;
|
|
120
|
+
}
|
|
121
|
+
export type ISODate = string;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@moss-dev/moss-web",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Moss browser SDK — in-browser semantic search powered by WASM",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"module": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE.txt"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"clean": "rm -rf dist",
|
|
23
|
+
"prepack": "npm run clean && npm run build",
|
|
24
|
+
"samples": "npx vite"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@moss-dev/moss-wasm": "0.10.0",
|
|
28
|
+
"onnxruntime-web": "^1.21.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"typescript": "^5.7.0",
|
|
32
|
+
"vite": "^8.0.8",
|
|
33
|
+
"@moss-dev/moss-wasm": "0.10.0"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"moss",
|
|
37
|
+
"semantic-search",
|
|
38
|
+
"wasm",
|
|
39
|
+
"browser",
|
|
40
|
+
"embeddings",
|
|
41
|
+
"vector"
|
|
42
|
+
],
|
|
43
|
+
"author": "InferEdge Inc.",
|
|
44
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "https://github.com/usemoss/moss"
|
|
48
|
+
}
|
|
49
|
+
}
|