From 0fb15fd178517340a0181d9edfe182dae2e0b2c1 Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Tue, 24 Nov 2026 18:14:29 +0800 Subject: [PATCH] docs: add Mastra integration guide (#5189) ## Summary Supersedes #2330 and brings the Mastra integration guide onto the current `docs/web` Mintlify documentation structure with verified LanceDB Embedded examples against the published `@mastra/lance` release. Credit to @vishesh-baghel for authoring the original LanceDB Mastra guide in #2330 and building the merged upstream Mastra storage and vector adapters in mastra-ai/mastra#3324. ### Changes - Add `docs/web/integrations/ai/mastra.mdx` covering LanceDB Embedded usage with `@mastra/lance`: - Node.js 22.13.0+ requirement and `npm install @mastra/lance @mastra/core`. - Runnable local `LanceVectorStore` quickstart (`create`, `createIndex`, `upsert`, `query`, `close`) with verified nearest-neighbor output (`lancedb 1`). - Explanation of the two-step native vector index behavior in `@mastra/lance` (first `createIndex()` initializes an empty table; calling `createIndex()` again after upserting at least 256 rows builds the native index on the `vector` column). - Pointer to `LanceStorage` noting the published three-argument `LanceStorage.create(id, name, uri)` signature and linking upstream Mastra references, without unverified cloud claims. - Register `integrations/ai/mastra` under **AI Platforms & Frameworks** in `docs/web/docs.json`. - Add the TypeScript example snippets to `docs/web-tests/ts/integrations.ts` and regenerate the corresponding exports (`TsFrameworksMastraQuickstart`, `TsFrameworksMastraNativeIndex`) in `docs/web/snippets/integrations.mdx`. ## Verification - Verified against published `@mastra/lance@1.6.1` and `@mastra/core@1.85.0` (resolving `@lancedb/lancedb@0.31.0` and `apache-arrow@18.1.0`): - Two-vector local quickstart returns `lancedb 1`. - Native `IVF_FLAT` index build after upserting 256 rows reports `vector_idx` with 256 indexed rows, 0 unindexed rows, and nearest-neighbor score `1`. - Confirmed `LanceStorage.create(id, name, uri)` succeeds with three arguments while the older two-argument call fails. --- docs/web-tests/ts/integrations.ts | 34 ++++++++++++++ docs/web/docs.json | 1 + docs/web/integrations/ai/mastra.mdx | 69 +++++++++++++++++++++++++++++ docs/web/snippets/integrations.mdx | 4 ++ 4 files changed, 108 insertions(+) create mode 100644 docs/web/integrations/ai/mastra.mdx diff --git a/docs/web-tests/ts/integrations.ts b/docs/web-tests/ts/integrations.ts index 3ae7967e9..75987cf7d 100644 --- a/docs/web-tests/ts/integrations.ts +++ b/docs/web-tests/ts/integrations.ts @@ -132,3 +132,37 @@ Question: ${input}`, ); // --8<-- [end:frameworks_genkit_custom_retriever] +// --8<-- [start:frameworks_mastra_quickstart] +import { LanceVectorStore } from "@mastra/lance"; + +const store = await LanceVectorStore.create("./lancedb-data"); +const indexName = "documents"; + +await store.createIndex({ indexName, dimension: 3 }); + +await store.upsert({ + indexName, + ids: ["mastra", "lancedb"], + vectors: [ + [0, 1, 0], + [1, 0, 0], + ], +}); + +const results = await store.query({ + indexName, + queryVector: [1, 0, 0], + topK: 1, +}); + +console.log(results[0].id, results[0].score); +store.close(); +// --8<-- [end:frameworks_mastra_quickstart] + +// --8<-- [start:frameworks_mastra_native_index] +await store.createIndex({ + indexName, + dimension: 3, + indexConfig: { type: "ivfflat", numPartitions: 1 }, +}); +// --8<-- [end:frameworks_mastra_native_index] diff --git a/docs/web/docs.json b/docs/web/docs.json index b561346cd..f2cd5ffa6 100644 --- a/docs/web/docs.json +++ b/docs/web/docs.json @@ -676,6 +676,7 @@ "integrations/ai/llamaIndex", "integrations/ai/genkit", "integrations/ai/kiln", + "integrations/ai/mastra", "integrations/ai/prompttools", "integrations/ai/synthetic-data-kit", "integrations/ai/vscode" diff --git a/docs/web/integrations/ai/mastra.mdx b/docs/web/integrations/ai/mastra.mdx new file mode 100644 index 000000000..b5703724a --- /dev/null +++ b/docs/web/integrations/ai/mastra.mdx @@ -0,0 +1,69 @@ +--- +title: "Mastra" +sidebarTitle: "Mastra" +description: "Use LanceDB Embedded as a local vector store in Mastra with @mastra/lance." +availability: + embedded: available + enterprise: unavailable +--- + +import { + TsFrameworksMastraNativeIndex, + TsFrameworksMastraQuickstart, +} from '/snippets/integrations.mdx'; + +[Mastra](https://mastra.ai) is an open-source TypeScript framework for building AI agents, workflows, and retrieval-augmented generation (RAG) pipelines. Its official `@mastra/lance` package connects Mastra to LanceDB Embedded on local disk with no separate database server. + +## Installation {#installation} + +`@mastra/lance` requires Node.js 22.13.0 or later and depends on `@mastra/core`. Install both packages: + + +```bash Bash icon="terminal" +npm install @mastra/lance @mastra/core +``` + + +Installing `@mastra/lance` also installs `@lancedb/lancedb` and the matching native binary for your platform. + +## Vector search quickstart {#vector-search-quickstart} + +Save the following script as `example.mjs`. It opens a local LanceDB directory with `LanceVectorStore.create()`, creates a `documents` table, upserts two 3-dimensional vectors, and runs a nearest-neighbor query: + + + {TsFrameworksMastraQuickstart} + + +Run the script: + + +```bash Bash icon="terminal" +node example.mjs +``` + + +The nearest-neighbor query returns `lancedb` with a cosine similarity score of `1`: + +```text Output +lancedb 1 +``` + +## Native vector index {#native-vector-index} + +The first `createIndex()` call initializes an empty LanceDB table without building a native vector index. When `tableName` is omitted, `indexName` sets the table name and `vector` is the vector column. Queries work on small tables immediately through flat scan. + +To build a native [LanceDB vector index](/indexing/vector-index) when extending the quickstart, call `createIndex()` again after upserting at least 256 rows and before `store.close()`: + + + {TsFrameworksMastraNativeIndex} + + +Supported `indexConfig.type` values include `"ivfflat"`, `"ivfpq"`, and the default HNSW PQ index. + +## Application storage and references {#application-storage-and-references} + +`@mastra/lance` also exports `LanceStorage` for Mastra agent memory and workflow state. The published `LanceStorage.create(id, name, uri)` factory takes three required arguments (`id`, `name`, and `uri`). See the [released `LanceStorage.create` implementation](https://github.com/mastra-ai/mastra/blob/d60ba71be015e2acbdc303208a14563902fb1f39/stores/lance/src/storage/index.ts#L78-L119) for the current signature if you encounter older two-argument examples. + +- [Mastra LanceDB vector store reference](https://mastra.ai/integrations/vectors/lance) +- [Mastra LanceDB storage guide](https://mastra.ai/integrations/databases/lancedb) +- [Released `LanceVectorStore.createIndex` implementation](https://github.com/mastra-ai/mastra/blob/d60ba71be015e2acbdc303208a14563902fb1f39/stores/lance/src/vector/index.ts#L685-L815) diff --git a/docs/web/snippets/integrations.mdx b/docs/web/snippets/integrations.mdx index 00f8bb02e..b8981dcb0 100644 --- a/docs/web/snippets/integrations.mdx +++ b/docs/web/snippets/integrations.mdx @@ -176,3 +176,7 @@ export const TsFrameworksGenkitCustomRetriever = "export const menuRetriever = l export const TsFrameworksGenkitUsage = "import { lancedbIndexerRef, lancedb, lancedbRetrieverRef, WriteMode } from \"genkitx-lancedb\";\nimport { textEmbedding004, vertexAI } from \"@genkit-ai/vertexai\";\nimport { gemini } from \"@genkit-ai/vertexai\";\nimport { z, genkit } from \"genkit\";\nimport { Document } from \"genkit/retriever\";\nimport { chunk } from \"llm-chunk\";\nimport { readFile } from \"fs/promises\";\nimport path from \"path\";\nimport pdf from \"pdf-parse/lib/pdf-parse\";\n\nconst ai = genkit({\n plugins: [\n // vertexAI provides the textEmbedding004 embedder\n vertexAI(),\n\n // the local vector store requires an embedder to translate from text to vector\n lancedb([\n {\n dbUri: \".db\", // optional lancedb uri, default to .db\n tableName: \"table\", // optional table name, default to table\n embedder: textEmbedding004,\n },\n ]),\n ],\n});\n"; +export const TsFrameworksMastraNativeIndex = "await store.createIndex({\n indexName,\n dimension: 3,\n indexConfig: { type: \"ivfflat\", numPartitions: 1 },\n});\n"; + +export const TsFrameworksMastraQuickstart = "import { LanceVectorStore } from \"@mastra/lance\";\n\nconst store = await LanceVectorStore.create(\"./lancedb-data\");\nconst indexName = \"documents\";\n\nawait store.createIndex({ indexName, dimension: 3 });\n\nawait store.upsert({\n indexName,\n ids: [\"mastra\", \"lancedb\"],\n vectors: [\n [0, 1, 0],\n [1, 0, 0],\n ],\n});\n\nconst results = await store.query({\n indexName,\n queryVector: [1, 0, 0],\n topK: 1,\n});\n\nconsole.log(results[0].id, results[0].score);\nstore.close();\n"; +