> For the complete documentation index, see [llms.txt](https://dnotifier.gitbook.io/product-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dnotifier.gitbook.io/product-docs/reference/ai-methods/javascript-typescript.md). Agents can also search and read this documentation through its [MCP server](https://dnotifier.gitbook.io/product-docs/~gitbook/mcp).

# JavaScript / TypeScript

Requires `notifier.aiEnabled === true` for AI features. Prefer `transport: "http"`.

## `sendAI(options)`

Send a prompt to the DNotifier AI pipeline.

| Parameter     | Type                 | Required | Default         | Description                                                                                               |
| ------------- | -------------------- | -------- | --------------- | --------------------------------------------------------------------------------------------------------- |
| `senderId`    | `string`             | ✅        | —               | User id                                                                                                   |
| `message`     | `string` \| `object` | ✅        | —               | Prompt text or structured chat payload                                                                    |
| `saveHistory` | `boolean`            | —        | `true`          | Persist to AI history                                                                                     |
| `sessionId`   | `string`             | —        | new UUID        | Continue an existing session                                                                              |
| `provider`    | `string`             | —        | project default | AI provider (e.g. `"open_ai"`, `"anthropic"`). Merged into request `data`.                                |
| `model`       | `string`             | —        | project default | Model id (e.g. `"gpt-4o"`). Merged into request `data`. Used for session logs and workflow observability. |

**Server metadata:** `type: "ai"`, `code: 300`

When set, `provider` and `model` land in the AI `data` payload next to your message fields. Leave them off to use the project default (`current_model`).

Keys and endpoints stay in the portal. Catalog: [multi-model providers](https://dnotifier.gitbook.io/product-docs/ai/multi-models.md).

| Provider      | `provider`      |
| ------------- | --------------- |
| OpenAI        | `open_ai`       |
| Anthropic     | `anthropic`     |
| Google Gemini | `google_gemini` |
| Hugging Face  | `hugging_face`  |
| Perplexity    | `perplexity`    |
| Azure AI      | `azure_ai`      |
| AWS Bedrock   | `aws_bedrock`   |
| Ollama        | `ollama`        |

**Message shapes:**

```js
// Simple text
{ text: "Summarize our refund policy." }

// Chat-style with optional RAG
{
  useKnowledgeBase: true,
  messages: [
    { role: "system", content: "You are a support agent." },
    { role: "user", content: "How do I reset my password?" },
  ],
}
```

**Returns:**

| Transport | Return                                                  |
| --------- | ------------------------------------------------------- |
| `http`    | Full AI response object                                 |
| `ws`      | Session id string; response on `onMessage` (code `300`) |

```js
const response = await notifier.sendAI({
  senderId: "user-123",
  provider: "open_ai",
  model: "gpt-4o",
  message: { text: "Hello, AI!" },
});

// Continue session
const sessionId = response?.metadata?.packet?.id ?? response;
await notifier.sendAI({
  senderId: "user-123",
  sessionId,
  message: { text: "Follow-up question." },
});
```

## `fetchAIHistory(options)`

| Parameter  | Type     | Required |
| ---------- | -------- | -------- |
| `senderId` | `string` | ✅        |

**Server code:** `301`

```js
const history = await notifier.fetchAIHistory({ senderId: "user-123" });
```

## `deleteAIHistoryMessage(options)`

| Parameter   | Type     | Required |
| ----------- | -------- | -------- |
| `senderId`  | `string` | ✅        |
| `messageId` | `string` | ✅        |

**Server code:** `302`

```js
await notifier.deleteAIHistoryMessage({
  senderId: "user-123",
  messageId: "msg-id",
});
```

## `search(options)`

Semantic search over the knowledge base.

| Parameter                           | Type     | Required | Default        | Description                                                                                             |
| ----------------------------------- | -------- | -------- | -------------- | ------------------------------------------------------------------------------------------------------- |
| `senderId`                          | `string` | ✅        | —              | User id                                                                                                 |
| `query`                             | `string` | ✅        | —              | Search text                                                                                             |
| `limit`                             | `number` | —        | server default | Max results                                                                                             |
| `minSimilarity`                     | `number` | —        | —              | Similarity threshold (0–1)                                                                              |
| `filterbySource`                    | `string` | —        | —              | Metadata source filter                                                                                  |
| `vectorStore` / `vectorStoreConfig` | `object` | —        | —              | Store for this call. See [Vector stores](https://dnotifier.gitbook.io/product-docs/ai/vector-stores.md) |
| `embedding` / `embeddingConfig`     | `object` | —        | —              | Embedding model for this call                                                                           |
| `serverConfig`                      | `object` | —        | —              | Call-level `{ vectorStore, embedding }`. Overrides `notifier.serverConfig`                              |

**Server code:** `303`

```js
const hits = await notifier.search({
  senderId: "user-123",
  query: "refund policy",
  limit: 5,
  minSimilarity: 0.7,
  filterbySource: "help-center",
});
```

## Knowledge base — `addDocument(options)`

| Parameter                                  | Type     | Required | Description                                                                     |
| ------------------------------------------ | -------- | -------- | ------------------------------------------------------------------------------- |
| `senderId`                                 | `string` | ✅        | User id                                                                         |
| `recordId` or `record_id`                  | `string` | ✅        | Document id                                                                     |
| `content` or `text`                        | `string` | ✅        | Document body                                                                   |
| `type`                                     | `string` | —        | Document type (e.g. `"text"`)                                                   |
| `metadata`                                 | `object` | —        | Arbitrary metadata                                                              |
| `vectorStore`, `embedding`, `serverConfig` | `object` | —        | Same runtime config as `search`. Also accepted by update, delete, list, and get |

**Server code:** `306`

```js
await notifier.addDocument({
  senderId: "user-123",
  recordId: "doc-001",
  content: "Refunds are processed within 5 business days.",
  type: "text",
  metadata: { source: "help-center" },
});
```

## Knowledge base — `updateDocument(options)`

Same parameters as `addDocument`. **Server code:** `307`

## Knowledge base — `deleteDocument(options)`

| Parameter                 | Type     | Required |
| ------------------------- | -------- | -------- |
| `senderId`                | `string` | ✅        |
| `recordId` or `record_id` | `string` | ✅        |

**Server code:** `308`

## Knowledge base — `listDocuments(options)`

| Parameter  | Type     | Required |
| ---------- | -------- | -------- |
| `senderId` | `string` | ✅        |
| `limit`    | `number` | —        |
| `offset`   | `number` | —        |
| `type`     | `string` | —        |

**Server code:** `309`

## Knowledge base — `getDocument(options)`

| Parameter                 | Type     | Required |
| ------------------------- | -------- | -------- |
| `senderId`                | `string` | ✅        |
| `recordId` or `record_id` | `string` | ✅        |

**Server code:** `310`

## `sendWithOpenAI(options)` — Deprecated

> **Deprecated:** Use `sendAI({ senderId, message })` instead. This method will be removed in a future release.

| Parameter      | Type                 | Required | Description                     |
| -------------- | -------------------- | -------- | ------------------------------- |
| `senderId`     | `string`             | ✅        | User id                         |
| `message`      | `string` \| `object` | ✅        | Prompt                          |
| `openaiApiKey` | `string`             | —        | Ignored — delegates to `sendAI` |
| `model`        | `string`             | —        | Ignored                         |
| `baseUrl`      | `string`             | —        | Ignored                         |

The current implementation calls `sendAI()` internally. The `openaiApiKey`, `model`, and `baseUrl` parameters are not used.

```js
// Don't use — migrate to sendAI
await notifier.sendWithOpenAI({
  senderId: "user-123",
  message: { text: "Hello" },
});
```

## Errors

| Error                                        | Cause                             |
| -------------------------------------------- | --------------------------------- |
| `"Not connected"`                            | Not authenticated                 |
| `"senderId required"`                        | Missing sender                    |
| `"query required"`                           | `search` without query            |
| `"recordId is required"`                     | Document methods without id       |
| `"content or text is required"`              | Empty document body               |
| `"AI is not enabled for this subscription."` | `sendWithOpenAI` when AI disabled |
