> 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/dart-flutter.md). Agents can also search and read this documentation through its [MCP server](https://dnotifier.gitbook.io/product-docs/~gitbook/mcp).

# Dart / Flutter

Requires `notifier.aiEnabled == true`. Prefer `transport: 'http'`.

## `sendAI()`

```dart
Future<dynamic> sendAI({
  required String senderId,
  required dynamic message,
  bool saveHistory = true,
  String? sessionId,
  String? provider,
  String? model,
})
```

| Parameter     | Type      | Required | Default         | Description                                                                                               |
| ------------- | --------- | -------- | --------------- | --------------------------------------------------------------------------------------------------------- |
| `senderId`    | `String`  | ✅        | —               | User id                                                                                                   |
| `message`     | `dynamic` | ✅        | —               | Prompt or chat payload                                                                                    |
| `saveHistory` | `bool`    | —        | `true`          | Persist to AI history                                                                                     |
| `sessionId`   | `String?` | —        | new UUID        | Continue existing session                                                                                 |
| `provider`    | `String?` | —        | project default | AI provider (e.g. `'open_ai'`). 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`

```dart
final response = await notifier.sendAI(
  senderId: 'user-123',
  provider: 'open_ai',
  model: 'gpt-4o',
  message: {'text': 'Summarize our refund policy.'},
);

// Chat-style
await notifier.sendAI(
  senderId: 'user-123',
  message: {
    'useKnowledgeBase': true,
    'messages': [
      {'role': 'system', 'content': 'You are a support agent.'},
      {'role': 'user', 'content': 'How do I reset my password?'},
    ],
  },
);
```

## `fetchAIHistory()`

```dart
Future<dynamic> fetchAIHistory({required String senderId})
```

**Server code:** `301`

## `deleteAIHistoryMessage()`

```dart
Future<dynamic> deleteAIHistoryMessage({
  required String senderId,
  required String messageId,
})
```

**Server code:** `302`

## `search()`

```dart
Future<dynamic> search({
  required String senderId,
  required String query,
  int? limit,
  double? minSimilarity,
  String? filterbySource,
  Map<String, dynamic>? vectorStore,
  Map<String, dynamic>? vectorStoreConfig,
  Map<String, dynamic>? embedding,
  Map<String, dynamic>? embeddingConfig,
  Map<String, dynamic>? serverConfig,
})
```

| Parameter                                  | Type                    | Required | Validation                                                                                                                                      |
| ------------------------------------------ | ----------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| `senderId`                                 | `String`                | ✅        | non-empty                                                                                                                                       |
| `query`                                    | `String`                | ✅        | non-empty after trim                                                                                                                            |
| `limit`                                    | `int?`                  | —        | must be `> 0` if set                                                                                                                            |
| `minSimilarity`                            | `double?`               | —        | must be 0–1 if set                                                                                                                              |
| `filterbySource`                           | `String?`               | —        | —                                                                                                                                               |
| `vectorStore`, `embedding`, `serverConfig` | `Map<String, dynamic>?` | —        | See [Vector stores](https://dnotifier.gitbook.io/product-docs/ai/vector-stores.md). `vectorStoreConfig` and `embeddingConfig` are the same maps |

**Server code:** `303`

```dart
final hits = await notifier.search(
  senderId: 'user-123',
  query: 'refund policy',
  limit: 5,
  minSimilarity: 0.7,
  filterbySource: 'help-center',
);
```

## `addDocument()`

```dart
Future<dynamic> addDocument({
  required String senderId,
  String? recordId,
  String? record_id,
  String? content,
  String? text,
  String? type,
  Map<String, dynamic>? metadata,
})
```

Also accepts `vectorStore`, `embedding`, and `serverConfig`, including on update, delete, list, and get. See [Vector stores and embeddings](https://dnotifier.gitbook.io/product-docs/ai/vector-stores.md).

**Server code:** `306`

## `updateDocument()`

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

## `deleteDocument()`

```dart
Future<dynamic> deleteDocument({
  required String senderId,
  String? recordId,
  String? record_id,
})
```

**Server code:** `308`

## `listDocuments()`

```dart
Future<dynamic> listDocuments({
  required String senderId,
  int? limit,
  int? offset,
  String? type,
})
```

**Server code:** `309`

## `getDocument()`

```dart
Future<dynamic> getDocument({
  required String senderId,
  String? recordId,
  String? record_id,
})
```

**Server code:** `310`

## `sendWithOpenAI()` — Deprecated

> **Deprecated:** Use `sendAI(senderId:, message:)` instead.

```dart
Future<dynamic> sendWithOpenAI({
  required String senderId,
  required dynamic message,
  String? openaiApiKey,
  String? model,
  String? baseUrl,
})
```

Delegates to `sendAI()`. The `openaiApiKey`, `model`, and `baseUrl` parameters are accepted for backward compatibility but not used.

Throws `StateError('AI is not enabled for this subscription')` when AI is disabled.

## Document CRUD example

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

final doc = await notifier.getDocument(
  senderId: 'user-123',
  recordId: 'doc-001',
);

await notifier.deleteDocument(
  senderId: 'user-123',
  recordId: 'doc-001',
);
```
