Skip to main content
@moss-js/moss / SessionIndex

SessionIndex

A local-first, in-process index for real-time indexing and querying. All operations (addDocs, deleteDocs, query) run in process memory with no cloud round-trip per operation. Call pushIndex() to persist the session to the cloud. Construct a session with MossClient.session(); the constructor is not used directly.

Example

Accessors

Methods

addDocs()

addDocs(docs, options?): Promise<{ added: number; updated: number }>
Adds or updates documents in the local session index. For built-in models, embeddings are generated locally. For modelId: "custom", each document must carry an embedding.

Parameters

Returns

Promise<{ added: number; updated: number }>

deleteDocs()

deleteDocs(docIds): Promise<number>
Deletes documents from the local session by id. Returns the number removed.

Parameters

Returns

Promise<number>

getDocs()

getDocs(options?): Promise<DocumentInfo[]>
Fetches documents currently in the local session.

Parameters

Returns

Promise<DocumentInfo[]>

query()

query(query, options?): Promise<SearchResult>
Hybrid (keyword + semantic) search over the local session index, entirely in-memory. For modelId: "custom", an explicit options.embedding is required.

Parameters

Returns

Promise<SearchResult>

loadIndex()

loadIndex(indexName, options?): Promise<number>
Loads an existing cloud index into the session by name. A session starts empty, so this is how it resumes the cloud index it is named after. With options.autoRefresh = true, the SDK polls the cloud index and pulls newer versions in on subsequent reads (paused while the session has un-pushed local edits). Returns the number of documents loaded, 0 when no cloud index has that name.

Parameters

Returns

Promise<number>

pushIndex()

pushIndex(): Promise<PushIndexResult>
Uploads the session to the cloud, creating or replacing the index with the same name. For a built-in model the service re-embeds the text with that model, so the index loads and answers text queries like one built with createIndex. A custom-model session keeps its own embeddings.

Returns

Promise<PushIndexResult>

saveToDisk()

saveToDisk(cachePath): Promise<void>
Persists the session to <cachePath>/<name>/ for offline reuse without a cloud round-trip.

Parameters


loadFromDisk()

loadFromDisk(cachePath): Promise<number>
Restores a session previously written by saveToDisk. Returns the number of documents loaded.

Parameters

Returns

Promise<number>

close()

close(): Promise<void>
Sends the final usage report, then releases the session’s native resources: its auto-refresh poller, its in-memory index and its reference to the shared embedding model. Resolves once the report arrives or 2 seconds pass. Safe to call more than once. Afterwards name and modelId still read; docCount and every method throw. MossClient.close() also closes every session the client opened. SessionIndex also implements Symbol.asyncDispose, so await using closes it when the enclosing block ends (Node.js 24 or later).

Returns

Promise<void>

Example

See Releasing resources.