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

import { MossClient } from "@moss-dev/moss-web";

const client = new MossClient("your-project-id", "your-project-key");

// Create an index with documents
await client.createIndex("docs", [
  { id: "1", text: "Machine learning fundamentals" },
  { id: "2", text: "Deep learning neural networks" },
]);

// Load the index into the browser, then query it
await client.loadIndex("docs");
const results = await client.query("docs", "AI and neural networks");

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

ParameterTypeDescription
projectIdstringYour project identifier.
projectKeystringYour project authentication key.
options?MossClientOptionsOptional configuration. See Options.

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

ParameterTypeDescription
projectIdstringYour project identifier.
projectKeystringYour project authentication key.
options?MossClientOptionsOptional configuration. See Options.

Returns

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

Example

// Lazy: WASM/model loads on first API call
const client = new MossClient("your-project-id", "your-project-key");

// Eager: WASM/model loads immediately
const client = await MossClient.create("your-project-id", "your-project-key");

Options

OptionTypeDescription
model"moss-minilm" | "moss-mediumlm"Embedding model. Defaults to "moss-minilm" (fast, for most use-cases). Use "moss-mediumlm" for higher quality.
baseUrlstringCustom API base URL, for self-hosted Moss instances.
const client = new MossClient("your-project-id", "your-project-key", {
  model: "moss-mediumlm",
  baseUrl: "https://moss.internal.example.com",
});

Index management

createIndex()

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

Parameters

ParameterTypeDescription
namestringName of the index to create.
docsDocumentInfo[]Documents to index.
options?CreateIndexOptionsOptional configuration.

Example

await client.createIndex("knowledge-base", [
  { id: "doc1", text: "Introduction to AI" },
  { id: "doc2", text: "Machine learning basics" },
]);

createIndexFromFiles()

createIndexFromFiles(name, files, options?): Promise
Creates a new index from files.

Parameters

ParameterTypeDescription
namestringName of the index to create.
filesFile[]Files to index.
options?CreateIndexOptionsOptional configuration.

Example

const input = document.querySelector("input[type=file]");
await client.createIndexFromFiles("knowledge-base", Array.from(input.files));

addDocs()

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

Parameters

ParameterTypeDescription
namestringName of the target index.
docsDocumentInfo[]Documents to add or update.
options?MutationOptionsOptional configuration.

Example

await client.addDocs("knowledge-base", [
  { id: "new-doc", text: "New content to index" },
]);

deleteDocs()

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

Parameters

ParameterTypeDescription
namestringName of the target index.
docIdsstring[]Document IDs to delete.
options?MutationOptionsOptional configuration.

Example

await client.deleteDocs("knowledge-base", ["doc1", "doc2"]);

getIndex()

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

Parameters

ParameterTypeDescription
namestringName of the index to retrieve.

Example

const info = await client.getIndex("knowledge-base");

listIndexes()

listIndexes(): Promise
Lists all available indexes.

Example

const indexes = await client.listIndexes();

deleteIndex()

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

Parameters

ParameterTypeDescription
namestringName of the index to delete.

Example

await client.deleteIndex("old-index");

getDocs()

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

Parameters

ParameterTypeDescription
namestringName of the target index.
options?GetDocumentsOptionsOptional configuration for retrieval.

Example

// Get all documents
const allDocs = await client.getDocs("knowledge-base");

// Get specific documents
const specificDocs = await client.getDocs("knowledge-base", {
  docIds: ["doc1", "doc2"],
});

getJobStatus()

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

Parameters

ParameterTypeDescription
jobIdstringThe job ID returned by an async operation.

Example

const status = await client.getJobStatus(jobId);
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

ParameterTypeDescription
namestringName of the index to load.
options?LoadIndexOptionsOptional configuration.

Example

await client.loadIndex("knowledge-base");

// Now queries run locally in the browser
const results = await client.query("knowledge-base", "search text");

hasIndex()

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

Parameters

ParameterTypeDescription
namestringName of the index to check.

Example

if (await client.hasIndex("knowledge-base")) {
  const results = await client.query("knowledge-base", "search text");
}

getIndexInfo()

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

Parameters

ParameterTypeDescription
namestringName of the loaded index.

Example

const info = await client.getIndexInfo("knowledge-base");

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

ParameterTypeDescription
namestringName of the target index to search.
queryTextstringThe search query text.
options?QueryOptionsOptional query configuration, such as topK.

Example

await client.loadIndex("knowledge-base");

const results = await client.query("knowledge-base", "machine learning");
results.docs.forEach((doc) => {
  console.log(`${doc.id}: ${doc.text} (score: ${doc.score})`);
});

refreshIndex()

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

Parameters

ParameterTypeDescription
namestringName of the loaded index to refresh.

Example

await client.refreshIndex("knowledge-base");

unloadIndex()

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

Parameters

ParameterTypeDescription
namestringName of the index to unload.

Example

await client.unloadIndex("knowledge-base");

Cleanup

dispose()

dispose(): void
Releases all resources held by the client, including loaded indexes and the WebAssembly runtime. Call this when the client is no longer needed.

Example

client.dispose();