npm package: @mavenagi/knowledge-base-integration
Description: This library facilitates integration between MavenAGI and external knowledge bases. It allows you to create the hooks necessary to sync data from various sources into a MavenAGI knowledge base from a Maven AGI App Studio app.
Latest version: 1.1.2

# @mavenagi/knowledge-base-integration

This library facilitates integration between MavenAGI and external knowledge bases. It allows you to create the hooks necessary to sync data from various sources into a MavenAGI knowledge base from a Maven AGI App Studio app.

## Installation

```bash
npm install @mavenagi/knowledge-base-integration
```

## Usage

To use this library, create an instance of `MavenAGIKnowledgeBaseIntegration` with custom functions for your specific integration:

```typescript
import MavenAGIKnowledgeBaseIntegration, { KnowledgeDocumentContentType } from '@mavenagi/knowledge-base-integration';

const integration = new MavenAGIKnowledgeBaseIntegration({
  preInstallTest: async ({ settings, organizationId, agentId }) => {
    // Implement your pre-install test logic here
  },
  generatePaginatedResults: async function* ({ settings, organizationId, agentId }) {
    // Implement your data fetching logic in an async generator here
    // yield results as an array of { id, title, content } objects
  },
  translateContent: (content: string) => {
    // Optional: Implement your content translation logic here
    // If not provided, content will be passed through unchanged
  },
  contentType: KnowledgeDocumentContentType.Html, // Optional, defaults to HTML
  maxConcurrentDocumentCreate: 1 // Optional, defaults to 1... controls how many documents are created concurrently
});
```

### Returned Methods

#### preInstall(config: IntegrationConfig): Promise<void>

This method tests the connection to your data provider before installation.

#### postInstall(config: IntegrationConfig): Promise<void>

This method creates or updates the knowledge base and syncs all data after installation.

#### knowledgeBaseRefreshed(config: IntegrationConfig): Promise<void>

This method syncs all data when the knowledge base is refreshed.

## Example

```typescript
// src/index.ts
import MavenAGIKnowledgeBaseIntegration, { type IntegrationConfig, KnowledgeDocumentContentType } from '@mavenagi/knowledge-base-integration';

const { preInstall, postInstall, knowledgeBaseRefreshed } = 
  new MavenAGIKnowledgeBaseIntegration({
    preInstallTest: async ({ settings }: IntegrationConfig) => {
      const response = await fetch(
        `${settings.apiURL}/api/v2/test_endpoint`,
        {
          headers: {
            AUTH_HEADER: settings.authHeader
          }
        }
      );
      if (!response.ok) throw new Error('Failed to connect to API');
    },
    generatePaginatedResults: async function* ({ settings }: IntegrationConfig) {
      let nextPageUrl =
        `${settings.confluenceUrl}/api/v2/documents`;
      while (nextPageUrl) {
        const response = await fetch(nextPageUrl, {
          headers: {
            AUTH_HEADER: settings.authHeader
          }
        });
        const data = await response.json();
        yield data.results.map(({ title, id, body }) => ({ title, id, content: body.html }));
        nextPageUrl = data._links.next;
      }
    },
    translateContent: (content: string) => {
      // Optional: Implement content translation if needed
      return content;
    },
    contentType: KnowledgeDocumentContentType.Html
  })

export default {
  preInstall,
  postInstall,
  knowledgeBaseRefreshed,
  executeAction() {}
}
```

## Development

To build the project:

```bash
npm run build
```

To run tests:

```bash
npm run test
```