Skip to main content
@moss-dev/moss-web / MossClient

MossClient

MossClient is the entry point for the Moss Browser/WASM SDK. It manages indexes and documents and runs semantic search locally in the browser. Once an index is loaded with loadIndex, queries run entirely in-browser with no server round-trips. This is the in-browser client. For server-side (Node.js) code, use the Node MossClient instead. See Browser vs Node for guidance. This client does not have sessions.

Example

Creating a client

Constructor (lazy)

new MossClient(projectId, projectKey, options?): MossClient
Creates a client with lazy initialization. The WebAssembly module and embedding model load on the first API call.

Parameters

Returns

MossClient

Constructor (custom authenticator)

new MossClient(projectId, authenticator, options?): MossClient
Creates a client with lazy initialization that authenticates through an IAuthenticator instead of a projectKey, so the key is never shipped in browser code. See Custom authenticator.

Parameters

Returns

MossClient

create() (eager)

MossClient.create(projectId, projectKey, options?): Promise<MossClient>
Creates a client with eager initialization. The WebAssembly module and embedding model load immediately, before the returned promise resolves.

Parameters

Returns

Promise<MossClient> Promise that resolves to a ready MossClient.

Example

create() (custom authenticator)

MossClient.create(projectId, authenticator, options?): Promise<MossClient>
Creates a client with eager initialization that authenticates through an IAuthenticator instead of a projectKey, so the key is never shipped in browser code. See Custom authenticator.

Parameters

Returns

Promise<MossClient> Promise that resolves to a ready MossClient.

Custom authenticator

Both the constructor and create() also accept an IAuthenticator in place of projectKey, so production frontends never ship the key. The authenticator fetches a short-lived token from your own backend. The client caches it until shortly before expiry and refreshes it after a 401.
getAuthToken() is only available when the client was created with an authenticator. See Custom Authenticator for the MyBackendAuthenticator implementation and the backend token endpoint.

Options

Index management

createIndex()

createIndex(name, docs, options?): Promise
Creates a new index with the provided documents.

Parameters

Example


addDocs()

addDocs(name, docs, options?): Promise
Adds or updates documents in an index.

Parameters

Example


deleteDocs()

deleteDocs(name, docIds, options?): Promise
Deletes documents from an index by their IDs.

Parameters

Example


getIndex()

getIndex(name): Promise
Gets metadata about a specific index.

Parameters

Example


listIndexes()

listIndexes(): Promise
Lists all available indexes.

Example


deleteIndex()

deleteIndex(name): Promise
Deletes an index and all its data.

Parameters

Example


getDocs()

getDocs(name, options?): Promise
Retrieves documents from an index.

Parameters

Example


getJobStatus()

getJobStatus(jobId): Promise
Gets the current status of an async operation.

Parameters

Example

Queries run against an index that has been loaded into the browser. Always call loadIndex before query.

loadIndex()

loadIndex(name, options?): Promise
Loads an index into the browser for fast local querying. Call this before query.

Parameters

Example


loadIndexes()

loadIndexes(names, options?): Promise
Loads several indexes, one at a time. Best-effort: a name that fails is reported in failed, and the indexes that loaded stay loaded. options apply to every index.

Parameters

Returns

A LoadIndexesResult:

Example


hasIndex()

hasIndex(name): Promise
Checks whether an index is loaded locally in the browser.

Parameters

Example


getIndexInfo()

getIndexInfo(name): Promise
Gets info about a locally loaded index.

Parameters

Example


query()

query(name, queryText, options?): Promise
Performs a semantic similarity search against a loaded index. The index must be loaded with loadIndex first; the search then runs entirely in the browser.

Parameters

Example


refreshIndex()

refreshIndex(name): Promise
Refreshes a loaded index from the server, picking up the latest changes.

Parameters

Example


unloadIndex()

unloadIndex(name): Promise
Unloads an index from the browser, freeing its resources. Querying it again requires calling loadIndex first.

Parameters

Example

Cleanup

dispose()

dispose(): void
Releases the client’s resources: stops auto-refresh, frees the loaded indexes and the ONNX Runtime sessions of models no longer in use, and sends the pending usage counts. The WebAssembly module stays loaded. Call this when the client is no longer needed. The loaded indexes are gone afterwards, so load them again before the next query. See Releasing resources.

Example