> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agentfront.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Enclave Integration

> Use VectoriaDB with Enclave for semantic search in sandboxed environments

Use VectoriaDB with [Enclave](https://enclave.dev) for semantic search in sandboxed environments.

## Setup

```ts title="src/enclave/document-search.ts" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
import { VectoriaDB, DocumentMetadata } from 'vectoriadb';
import { Enclave } from 'enclave';

interface DocumentMeta extends DocumentMetadata {
  title: string;
  category: string;
  createdAt: string;
}

// Create search index
const documentIndex = new VectoriaDB<DocumentMeta>({
  defaultSimilarityThreshold: 0.35,
  useHNSW: true,
});

// Initialize in Enclave context
export async function initializeSearch(enclave: Enclave) {
  await documentIndex.initialize();

  // Index documents from Enclave storage
  const documents = await enclave.listDocuments();

  for (const doc of documents) {
    await documentIndex.add(doc.id, doc.content, {
      id: doc.id,
      title: doc.title,
      category: doc.category,
      createdAt: doc.createdAt,
    });
  }
}

// Search documents
export async function searchDocuments(query: string, category?: string) {
  return documentIndex.search(query, {
    topK: 10,
    filter: category ? (m) => m.category === category : undefined,
  });
}
```

## Sync with Enclave Events

```ts title="src/enclave/sync.ts" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
// Keep index in sync with Enclave changes
enclave.on('document:created', async (doc) => {
  await documentIndex.add(doc.id, doc.content, {
    id: doc.id,
    title: doc.title,
    category: doc.category,
    createdAt: doc.createdAt,
  });
});

enclave.on('document:updated', async (doc) => {
  await documentIndex.update(doc.id, {
    text: doc.content,
    metadata: {
      id: doc.id,
      title: doc.title,
      category: doc.category,
      createdAt: doc.createdAt,
    },
  });
});

enclave.on('document:deleted', async (docId) => {
  documentIndex.remove(docId);
});
```

## Category-based Search

```ts title="src/enclave/category-search.ts" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
// Search within specific categories
export async function searchByCategory(
  query: string,
  categories: string[]
) {
  return documentIndex.search(query, {
    topK: 20,
    filter: (m) => categories.includes(m.category),
  });
}

// Usage
const codeResults = await searchByCategory('authentication', ['code', 'snippets']);
const docResults = await searchByCategory('getting started', ['documentation']);
```

## Related

<CardGroup cols={3}>
  <Card title="FrontMCP" icon="wand-magic-sparkles" href="/vectoriadb/integrations/frontmcp">
    FrontMCP integration
  </Card>

  <Card title="Express" icon="node-js" href="/vectoriadb/integrations/express">
    Express integration
  </Card>

  <Card title="Next.js" icon="react" href="/vectoriadb/integrations/nextjs">
    Next.js integration
  </Card>
</CardGroup>
