> 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/workflow-exports/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

## Imports

```dart
import 'package:dnotifier/dnotifier.dart';
```

Exported types: `Agent`, `defineAgent`, `AgentRun`, `Workflow`, `WorkflowEntry`, `WorkflowContext`, `WorkflowRunner`, `WorkflowRunResult`, `WorkflowError`, `WorkflowNotifier`.

## `defineAgent()`

```dart
Agent defineAgent({
  required String name,
  AgentRun? run,
  String? description,
  String? instructions,
  String? provider,
  String? model,
  List<RegisteredTool>? tools,
  Map<String, List<String>>? permissions,
  bool observability = false,
})
```

`AgentRun` is `Future<dynamic> Function(WorkflowContext ctx)`. Classic agents pass `run:`; tool agents pass `tools:` (and may omit `run`).

```dart
final intentAgent = DNotifier.defineAgent(
  name: 'intent-agent',
  run: (ctx) async {
    await ctx.sendAI(
      message: {'text': 'Classify: search or general?'},
      saveHistory: false,
      label: 'Intent classification',
    );
    return {'intent': 'search'};
  },
);
```

Also on the client: `notifier.tools.register`, `notifier.mcp.connect`, `notifier.runAgent`.\
→ [Tools & MCP — Dart](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/tools-and-mcp/dart-flutter.md)

## `Workflow` class

```dart
Workflow({
  required String name,
  String? description,
  bool observability = false,
  required WorkflowEntry entry,
})
```

| Parameter       | Type            | Required | Default | Description                                 |
| --------------- | --------------- | -------- | ------- | ------------------------------------------- |
| `name`          | `String`        | ✅        | —       | Workflow identifier                         |
| `description`   | `String?`       | —        | —       | ≤ 100 characters                            |
| `observability` | `bool`          | —        | `false` | Dashboard telemetry                         |
| `entry`         | `WorkflowEntry` | ✅        | —       | `Future<dynamic> Function(WorkflowContext)` |

### Methods

| Method                                             | Returns    | Description                                                             |
| -------------------------------------------------- | ---------- | ----------------------------------------------------------------------- |
| `registerAgent(String name, Object agentOrRemote)` | `Workflow` | Register local `Agent`, `RemoteAgentRef`, or `{receiverId}` map         |
| `registerAgents(Map<String, Object> agents)`       | `Workflow` | Register multiple (fluent)                                              |
| `getAgent(String name)`                            | `Agent?`   | Lookup **local** agent                                                  |
| `listen(WorkflowNotifier notifier)`                | `Workflow` | Host local agents for remote `runAgent`, MCP, and A2A calls (WebSocket) |
| `stopListening()`                                  | `Workflow` | Stop answering remote calls                                             |

```dart
final workflow = Workflow(
  name: 'intent-router',
  description: 'Route to search or general Q&A',
  observability: true,
  entry: (ctx) async {
    final result = await ctx.runAgent('intent-agent');
    // ...
    return {'branch': 'search'};
  },
).registerAgents({
  'intent-agent': intentAgent,
});
```

## `WorkflowContext`

| Member / method                                                     | Description                                        |
| ------------------------------------------------------------------- | -------------------------------------------------- |
| `notifier`                                                          | `WorkflowNotifier` (implemented by `DNotifier`)    |
| `senderId`                                                          | `String`                                           |
| `workflow`                                                          | `Workflow`                                         |
| `input`                                                             | `dynamic`                                          |
| `state`                                                             | `Map<String, dynamic>`                             |
| `agentName`                                                         | `String?`                                          |
| `executionId`                                                       | `String?`                                          |
| `runAgent(name, {input})`                                           | Invoke a registered agent                          |
| `runAgents(spec)`                                                   | Run agents in parallel → `{ results }`             |
| `waitForMessage({timeoutMs})`                                       | Wait for next inbound realtime message (WebSocket) |
| `sendAI({message, saveHistory, sessionId, provider, model, label})` | AI with observability                              |
| `fetchAIHistory({label})`                                           | AI history                                         |
| `search({query, limit, minSimilarity, filterbySource, label})`      | Semantic search                                    |
| `addDocument(...)`                                                  | Add document                                       |
| `updateDocument(...)`                                               | Update document                                    |
| `deleteDocument(...)`                                               | Delete document                                    |
| `listDocuments(...)`                                                | List documents                                     |
| `getDocument(...)`                                                  | Get document                                       |
| `recordStep(Map step)`                                              | Custom observability step                          |

## `runWorkflow()`

On `DNotifier`:

```dart
Future<WorkflowRunResult> runWorkflow({
  required Workflow workflow,
  required dynamic input,
  String? senderId,
})
```

## `WorkflowRunResult`

| Field         | Type                   | Description                |
| ------------- | ---------------------- | -------------------------- |
| `result`      | `dynamic`              | `entry` return value       |
| `state`       | `Map<String, dynamic>` | Final shared state         |
| `executionId` | `String?`              | When observability enabled |

## `WorkflowRunner` (advanced)

```dart
final runner = WorkflowRunner(
  notifier: notifier,
  senderId: 'user-123',
  workflow: workflow,
);
final run = await runner.run(input: 'How does messaging work?');
```

## `WorkflowError`

`Exception` subclass for validation failures (empty names, missing agents, description too long).
