# Outlook - Message Category

> Apply Outlook categories (labels) to messages via the Microsoft Graph API.

Source: https://docs.sommatic.ai/en/nodes/connectors.microsoft.outlook.message.category/

- Slug: `connectors.microsoft.outlook.message.category`
- Family: Connectors
- Version: 1.0.0
- Status: Available
- Execution: async

Apply Outlook categories (labels) to messages via the Microsoft Graph API.

Categories live in the user master list and can be applied to messages with a merge-safe PATCH that preserves categories already set by humans or other automations.

**Operations:**
- **apply:** Apply one or more categories to an existing message. Merges with existing categories (does not overwrite).
- **ensure_master_category:** Create one or more categories in the user master list. Idempotent: returns success if the category already exists.
- **apply_with_ensure:** Combination of the two. Recommended for workflows that should self-bootstrap their categories.

## Configuration

- `connection_id` (string, required): Credential for Microsoft Graph API access. Requires `Mail.ReadWrite` (for PATCH /messages/{id}) and `MailboxSettings.ReadWrite` (for POST /outlook/masterCategories). Accepts credentials of type `oauth2` (access + refresh token), `service-account` (tenant id + client id + client secret), or `object` (advanced Graph config).
- `mailbox_mode` (string, optional): Token owner: uses the mailbox of the account that generated the credential. Other mailbox: specify an email address (requires Application permissions in Azure AD). Default: `token_owner`. Allowed values: `token_owner`, `other`.
- `mailbox_email` (any, optional, accepts expressions): Single email or array/comma-separated list of mailboxes to operate on. When more than one is provided, the worker fans out and applies the categories in every mailbox that contains the same message (matched by internetMessageId). Shown when: mailbox_mode = `other`.
- `internet_message_id` (string, optional, accepts expressions): RFC-5322 Internet Message ID of the email (e.g., <BL0PR16MB...@outlook.com>). Required for multi-mailbox mode: the worker uses it to find the local message id in each mailbox via $filter=internetMessageId eq. When omitted and a single mailbox is provided, the worker falls back to the legacy single-mailbox flow using `message_id` directly. Shown when: operation = `apply` | `apply_with_ensure`.
- `operation` (string, required): apply: apply categories to an existing message (merge-safe). ensure_master_category: create categories in the user master list (idempotent). apply_with_ensure: ensure + apply in one call (recommended). Default: `apply_with_ensure`. Allowed values: `apply`, `ensure_master_category`, `apply_with_ensure`.
- `message_id` (string, optional, accepts expressions): Graph message ID of the email to categorize. Shown when: operation = `apply` | `apply_with_ensure`.
- `categories` (array<string | object>, optional, accepts expressions): Categories to apply or ensure. Accepts: an array of display-name strings, a comma-separated string, OR an array of { name, color } objects to set a distinct color per category (e.g. [{name:"Recibido por IA - Sommatic",color:"preset8"},{name:"Asignado DAUITA",color:"preset4"}]). When an item has no color, the node-level category_color is used.
- `category_color` (string, optional): Outlook category color preset. Required for ensure_master_category and apply_with_ensure (ignored by apply). Common values: preset0 (Red), preset8 (Purple), preset23 (Dark Purple - closest to Sommatic AI corporate #2A213B). Use "none" for no color. Allowed values: `preset0`, `preset1`, `preset2`, `preset3`, `preset4`, `preset5`, `preset6`, `preset7`, `preset8`, `preset9`, `preset10`, `preset11`, `preset12`, `preset13`, `preset14`, `preset15`, `preset16`, `preset17`, `preset18`, `preset19`, `preset20`, `preset21`, `preset22`, `preset23`, `preset24`, `none`. Shown when: operation = `ensure_master_category` | `apply_with_ensure`.
- `match_scope` (string, optional): conversation (default): an internet_message_id is probed and expanded to the WHOLE thread (labels original + replies/forwards). message: resolve ONLY the exact internet_message_id matches (the reply/forward copy) — never expand to the conversation. Use "message" to label/mark-read a specific IA-generated copy without touching the citizen original. Default: `conversation`. Allowed values: `conversation`, `message`. Shown when: operation = `apply` | `apply_with_ensure`.
- `mark_read` (boolean, optional): After labeling, PATCH { isRead:true } on every resolved message. Scope it to IA-generated copies only (via match_scope:"message" or by targeting the forward_recipient mailbox); never point a mark_read node at the citizen original, which must stay unread. Default: `false`. Shown when: operation = `apply` | `apply_with_ensure`.
- `resolve_max_attempts` (number, optional): Bounded retry on the message-resolution query. Absorbs Graph indexing latency of a just-sent reply/forward copy (a "success that resolved 0 messages" does not re-fire the node retry). Use e.g. 6 for IA-copy nodes; leave 1 for legacy calls. Default: `1`. Shown when: operation = `apply` | `apply_with_ensure`.
- `resolve_wait_ms` (number, optional): Delay between resolution attempts in milliseconds (e.g. 1500). Only relevant when resolve_max_attempts > 1. Default: `0`. Shown when: operation = `apply` | `apply_with_ensure`.
- `require_apply` (boolean, optional): When true, emit port:"error" if nothing was labeled after all retries (instead of a silent success). Lets the caller drive a fallback branch (e.g. a notice + human task). Default: `false`. Shown when: operation = `apply` | `apply_with_ensure`.

Its configuration shows the shared execution settings, but this node does not apply retries (`retryOnFail`, `maxTries`) yet. `continueOnFail` is not applied by any node.

## Output ports

- `out` (default): Category operation completed successfully.
- `error`: Operation failed (auth error, invalid message ID, API error).

## Input contract

- `connection_id` (string, required)
- `operation` (string, required)
- `message_id` (string, optional)
- `internet_message_id` (string, optional)
- `conversation_id` (string, optional)
- `categories` (array<string>, optional)
- `category_color` (string, optional)
- `match_scope` (string, optional): Allowed values: `conversation`, `message`.
- `mark_read` (boolean, optional)
- `resolve_max_attempts` (number, optional)
- `resolve_wait_ms` (number, optional)
- `require_apply` (boolean, optional)

## Output contract

- `result` (object, optional)
  - `operation` (string, optional)
  - `message_id` (string, optional)
  - `applied` (array<string>, optional): Categories newly added to the message.
  - `preserved` (array<string>, optional): Categories that were already on the message and were preserved.
  - `final_categories` (array<string>, optional): Final categories on the message after the operation.
  - `ensured` (array<string>, optional): Categories created in the master list.
  - `already_existed` (array<string>, optional): Categories that already existed in the master list.
  - `master_category_ensured` (boolean, optional)
  - `conversation_id_resolved` (string, optional): Thread id used to resolve messages (multi-mailbox mode).
  - `messages_per_mailbox` (object, optional): Count of messages labeled per mailbox.
  - `messages_read_by_mailbox` (object, optional): Count of messages marked read (isRead:true) per mailbox, when mark_read is set.

## In expressions

What a later node can read from this node's output with `$("Outlook - Message Category").item.json.<path>` (write your node's label instead of `Outlook - Message Category`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("Outlook - Message Category").item.json.result` (object) = `{…}`
- `$("Outlook - Message Category").item.json.result.operation` (string) = `"apply_with_ensure"`
- `$("Outlook - Message Category").item.json.result.message_id` (string) = `"AAkALgAAAAAAHYQDEapmEc2byACqAC-EWg0A"`
- `$("Outlook - Message Category").item.json.result.applied` (array) = `[…] (1)`: Categories newly added to the message.
- `$("Outlook - Message Category").item.json.result.preserved` (array) = `[…] (1)`: Categories that were already on the message and were preserved.
- `$("Outlook - Message Category").item.json.result.final_categories` (array) = `[…] (2)`: Final categories on the message after the operation.
- `$("Outlook - Message Category").item.json.result.ensured` (array) = `[…] (0)`: Categories created in the master list.
- `$("Outlook - Message Category").item.json.result.already_existed` (array) = `[…] (1)`: Categories that already existed in the master list.
- `$("Outlook - Message Category").item.json.result.master_category_ensured` (boolean) = `true`
- `$("Outlook - Message Category").item.json.result.conversation_id_resolved` (string): Thread id used to resolve messages (multi-mailbox mode). **The worker does not return it: reads as undefined.**
- `$("Outlook - Message Category").item.json.result.messages_per_mailbox` (object): Count of messages labeled per mailbox. **The worker does not return it: reads as undefined.**
- `$("Outlook - Message Category").item.json.result.messages_read_by_mailbox` (object): Count of messages marked read (isRead:true) per mailbox, when mark_read is set. **The worker does not return it: reads as undefined.**
- `$("Outlook - Message Category").item.json.result.permission_denied` (array) = `[…] (0)` (Not suggested by the editor)


## How it works

Applies Outlook categories (labels) to a message without removing the existing ones, and can create them in the mailbox's master category list with a color. It works on one mailbox by message id, or fans out across several mailboxes by Internet Message ID or conversation.

1. Resolves the node config, requires `connection_id` and gets a Graph token (app-only with `client_id`, `client_secret`, `tenant_id`, or the stored `token` / `access_token`). Every request asks for immutable ids (`Prefer: IdType="ImmutableId"`).
2. Normalizes `categories` from a comma-separated string, an array of names or an array of `{ name, color }`; items without a color use `category_color`.
3. Single-mailbox mode applies when there is one mailbox (or `mailbox_mode` is `token_owner`) and neither `internet_message_id` nor `conversation_id` is set: it works on `message_id` directly.
4. Otherwise it fans out over `mailbox_email` (array or comma-separated list). With `internet_message_id` and `match_scope: conversation` it first probes the mailboxes for the conversation id; then, in parallel per mailbox, it resolves the local messages (the conversation's messages, up to 50, or with `match_scope: message` only the exact Internet Message ID copies, up to 10), retrying `resolve_max_attempts` times with `resolve_wait_ms` between attempts.
5. `ensure_master_category` creates each category in the master list; an existing one is accepted and its color is corrected when it differs. `apply` reads the message's categories, merges the new ones without duplicates and PATCHes them, retrying 412 (change key), 429 (honouring `Retry-After`) and 404 up to `patch_max_attempts`. `apply_with_ensure` runs both.
6. With `mark_read`, marks the resolved messages as read (best effort). Chooses `error` when every mailbox failed, or when `require_apply` is set and nothing was labeled; otherwise `out`.

## In a workflow

### Node

```json
{
  "node_id": "node-label-processed",
  "label": "Label as processed",
  "operator_slug": "connectors.microsoft.outlook.message.category",
  "operator_version": "1.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "mailbox_mode": "other",
    "mailbox_email": "support@example.com",
    "operation": "apply_with_ensure",
    "message_id": "{{ $json.id }}",
    "categories": [
      {
        "name": "Processed",
        "color": "preset4"
      }
    ],
    "category_color": "preset8"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "result": {
      "operation": "apply_with_ensure",
      "message_id": "AAkALgAAAAAAHYQDEapmEc2byACqAC-EWg0A",
      "applied": [
        "Processed"
      ],
      "preserved": [
        "Customer"
      ],
      "final_categories": [
        "Customer",
        "Processed"
      ],
      "master_category_ensured": true,
      "ensured": [],
      "already_existed": [
        "Processed"
      ],
      "permission_denied": []
    }
  },
  "port": "out"
}
```

## Output ports (behaviour in the code)

### `out`

Single mode: the categories were applied or already present. Multi mode: at least one mailbox did not fail; the output is then `{ result: { operation, mode: "multi_mailbox", conversation_id_resolved, mailboxes_total, applied_in, messages_per_mailbox, messages_read_by_mailbox, not_found_in, permission_denied_in, errored_in, conflict_in, any_applied, any_conflict } }`.

```json
{
  "result": {
    "operation": "apply_with_ensure",
    "message_id": "AAkALgAAAAAAHYQDEapmEc2byACqAC-EWg0A",
    "applied": [
      "Processed"
    ],
    "preserved": [
      "Customer"
    ],
    "final_categories": [
      "Customer",
      "Processed"
    ],
    "master_category_ensured": true,
    "ensured": [],
    "already_existed": [
      "Processed"
    ],
    "permission_denied": []
  }
}
```

### `error`

A validation failed (the output is the envelope `{ ok, errors, port }`), a Graph call failed in single mode (`{ error, response }`), or in multi mode every mailbox failed or `require_apply` found nothing labeled (the multi-mailbox `result`).

```json
{
  "ok": false,
  "errors": [
    "At least one category is required for apply operation"
  ],
  "port": "error"
}
```

## Behaviour in the code

- `patch_max_attempts`: Undeclared. Attempts for each category PATCH (default 4, minimum 1). `patch_wait_ms` (undeclared, default 500) is multiplied by the attempt number between tries, unless Graph sends `Retry-After`.
- `conversation_id`: Undeclared in the config schema (only in the input contract). When set, it is used directly as the thread to label and the probe by Internet Message ID is skipped; it also switches to multi mode.
- `mailbox_email`: Read only when `mailbox_mode` is `other`. Accepts one address, an array or a comma-separated list; more than one address switches to multi mode.
- `category_color`: Outlook preset (`preset0`…`preset24`) or `none`. Ignored by `apply`. A per-item `color` in `categories` takes precedence.
- `mark_read`: In single mode it marks `message_id` as read; in multi mode every resolved message. With `match_scope: conversation` that includes the whole thread, so use `match_scope: message` to mark only one copy.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `Microsoft 365 credential is required` | `error` | `connection_id` is empty. |
| `Failed to resolve Microsoft 365 access token` | `error` | The credential could not be resolved or the token request failed. |
| `Message ID is required for apply operation` | `error` | Single mode without `message_id`. |
| `At least one category is required for apply operation` | `error` | `categories` is empty. For `ensure_master_category` the message is `At least one category displayName is required`. |
| `Failed to ensure master category '<name>': <Graph message>` | `error` | Creating the category failed for a reason other than "already exists" or a permission error. |
| `Unknown operation: <operation>` | `error` | `operation` is not apply, ensure_master_category or apply_with_ensure (single mode). |
| `No messages could be labeled in this mailbox: <reason>` | `error` | Multi mode: reported per mailbox inside `errored_in` (or `conflict_in` for change-key conflicts that survived the retries). |

## Notes and limits

- Applying is merge-safe and idempotent: existing categories are preserved and a category already present returns `reason: "already_present"` with `skipped`.
- Without `MailboxSettings.ReadWrite`, `apply_with_ensure` still labels the message and returns `master_category_ensured: false` with a `warning`; the category may show without color.
- In multi mode, a 401/403 in a mailbox is counted in `permission_denied_in`, not as an error. Conversation ids differ per mailbox, so when a thread lookup returns nothing the worker re-derives the local thread from `internet_message_id`.
- Multi mode writes one `[label-trace]` summary line to the logs per run, plus one line per mailbox where the label did not land.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "connectors.microsoft.outlook.message.category",
  "name": "Outlook - Message Category",
  "category": "integrations",
  "version": "1.0.0",
  "description": "Apply Outlook categories (labels) to messages via the Microsoft Graph API.\n\n  Categories live in the user master list and can be applied to messages with a merge-safe PATCH that preserves categories already set by humans or other automations.\n\n  **Operations:**\n  - **apply:** Apply one or more categories to an existing message. Merges with existing categories (does not overwrite).\n  - **ensure_master_category:** Create one or more categories in the user master list. Idempotent: returns success if the category already exists.\n  - **apply_with_ensure:** Combination of the two. Recommended for workflows that should self-bootstrap their categories.",
  "capabilities": [
    "email",
    "microsoft",
    "outlook",
    "office365",
    "graph-api",
    "category",
    "label"
  ],
  "group": [
    {
      "key": "microsoft",
      "name": "Microsoft",
      "icon_url": "/icons/integrations/microsoft/microsoft.svg"
    },
    {
      "key": "microsoft365",
      "name": "Microsoft 365",
      "icon_url": "/icons/integrations/microsoft/microsoft365.svg"
    }
  ],
  "icon_url": "/icons/integrations/microsoft/outlook.svg",
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "Microsoft 365 Credential",
        "description": "Credential for Microsoft Graph API access. Requires `Mail.ReadWrite` (for PATCH /messages/{id}) and `MailboxSettings.ReadWrite` (for POST /outlook/masterCategories). Accepts credentials of type `oauth2` (access + refresh token), `service-account` (tenant id + client id + client secret), or `object` (advanced Graph config).",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "oauth2",
            "service-account",
            "object"
          ],
          "types": [
            "outlook",
            "microsoft365",
            "oauth2"
          ]
        }
      },
      "mailbox_mode": {
        "type": "string",
        "title": "Mailbox",
        "enum": [
          "token_owner",
          "other"
        ],
        "default": "token_owner",
        "description": "Token owner: uses the mailbox of the account that generated the credential. Other mailbox: specify an email address (requires Application permissions in Azure AD)."
      },
      "mailbox_email": {
        "title": "Mailbox Email(s)",
        "description": "Single email or array/comma-separated list of mailboxes to operate on. When more than one is provided, the worker fans out and applies the categories in every mailbox that contains the same message (matched by internetMessageId).",
        "format": "expression",
        "displayOptions": {
          "show": {
            "mailbox_mode": [
              "other"
            ]
          }
        }
      },
      "internet_message_id": {
        "type": "string",
        "title": "Internet Message ID",
        "description": "RFC-5322 Internet Message ID of the email (e.g., <BL0PR16MB...@outlook.com>). Required for multi-mailbox mode: the worker uses it to find the local message id in each mailbox via $filter=internetMessageId eq. When omitted and a single mailbox is provided, the worker falls back to the legacy single-mailbox flow using `message_id` directly.",
        "format": "expression",
        "displayOptions": {
          "show": {
            "operation": [
              "apply",
              "apply_with_ensure"
            ]
          }
        }
      },
      "operation": {
        "type": "string",
        "title": "Operation",
        "enum": [
          "apply",
          "ensure_master_category",
          "apply_with_ensure"
        ],
        "default": "apply_with_ensure",
        "description": "apply: apply categories to an existing message (merge-safe). ensure_master_category: create categories in the user master list (idempotent). apply_with_ensure: ensure + apply in one call (recommended)."
      },
      "message_id": {
        "type": "string",
        "title": "Message ID",
        "description": "Graph message ID of the email to categorize.",
        "format": "expression",
        "displayOptions": {
          "show": {
            "operation": [
              "apply",
              "apply_with_ensure"
            ]
          }
        }
      },
      "categories": {
        "type": "array",
        "title": "Categories",
        "items": {
          "type": [
            "string",
            "object"
          ]
        },
        "description": "Categories to apply or ensure. Accepts: an array of display-name strings, a comma-separated string, OR an array of { name, color } objects to set a distinct color per category (e.g. [{name:\"Recibido por IA - Sommatic\",color:\"preset8\"},{name:\"Asignado DAUITA\",color:\"preset4\"}]). When an item has no color, the node-level category_color is used.",
        "format": "expression"
      },
      "category_color": {
        "type": "string",
        "title": "Category Color",
        "enum": [
          "preset0",
          "preset1",
          "preset2",
          "preset3",
          "preset4",
          "preset5",
          "preset6",
          "preset7",
          "preset8",
          "preset9",
          "preset10",
          "preset11",
          "preset12",
          "preset13",
          "preset14",
          "preset15",
          "preset16",
          "preset17",
          "preset18",
          "preset19",
          "preset20",
          "preset21",
          "preset22",
          "preset23",
          "preset24",
          "none"
        ],
        "description": "Outlook category color preset. Required for ensure_master_category and apply_with_ensure (ignored by apply). Common values: preset0 (Red), preset8 (Purple), preset23 (Dark Purple - closest to Sommatic AI corporate #2A213B). Use \"none\" for no color.",
        "displayOptions": {
          "show": {
            "operation": [
              "ensure_master_category",
              "apply_with_ensure"
            ]
          }
        }
      },
      "match_scope": {
        "type": "string",
        "title": "Match Scope",
        "enum": [
          "conversation",
          "message"
        ],
        "default": "conversation",
        "description": "conversation (default): an internet_message_id is probed and expanded to the WHOLE thread (labels original + replies/forwards). message: resolve ONLY the exact internet_message_id matches (the reply/forward copy) — never expand to the conversation. Use \"message\" to label/mark-read a specific IA-generated copy without touching the citizen original.",
        "displayOptions": {
          "show": {
            "operation": [
              "apply",
              "apply_with_ensure"
            ]
          }
        }
      },
      "mark_read": {
        "type": "boolean",
        "title": "Mark as Read",
        "default": false,
        "description": "After labeling, PATCH { isRead:true } on every resolved message. Scope it to IA-generated copies only (via match_scope:\"message\" or by targeting the forward_recipient mailbox); never point a mark_read node at the citizen original, which must stay unread.",
        "displayOptions": {
          "show": {
            "operation": [
              "apply",
              "apply_with_ensure"
            ]
          }
        }
      },
      "resolve_max_attempts": {
        "type": "number",
        "title": "Resolve Max Attempts",
        "default": 1,
        "description": "Bounded retry on the message-resolution query. Absorbs Graph indexing latency of a just-sent reply/forward copy (a \"success that resolved 0 messages\" does not re-fire the node retry). Use e.g. 6 for IA-copy nodes; leave 1 for legacy calls.",
        "displayOptions": {
          "show": {
            "operation": [
              "apply",
              "apply_with_ensure"
            ]
          }
        }
      },
      "resolve_wait_ms": {
        "type": "number",
        "title": "Resolve Wait (ms)",
        "default": 0,
        "description": "Delay between resolution attempts in milliseconds (e.g. 1500). Only relevant when resolve_max_attempts > 1.",
        "displayOptions": {
          "show": {
            "operation": [
              "apply",
              "apply_with_ensure"
            ]
          }
        }
      },
      "require_apply": {
        "type": "boolean",
        "title": "Require Apply",
        "default": false,
        "description": "When true, emit port:\"error\" if nothing was labeled after all retries (instead of a silent success). Lets the caller drive a fallback branch (e.g. a notice + human task).",
        "displayOptions": {
          "show": {
            "operation": [
              "apply",
              "apply_with_ensure"
            ]
          }
        }
      },
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    },
    "required": [
      "connection_id",
      "operation"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "operation": {
        "type": "string"
      },
      "message_id": {
        "type": "string"
      },
      "internet_message_id": {
        "type": "string"
      },
      "conversation_id": {
        "type": "string"
      },
      "categories": {
        "type": "array",
        "items": {
          "type": "string"
        }
      },
      "category_color": {
        "type": "string"
      },
      "match_scope": {
        "type": "string",
        "enum": [
          "conversation",
          "message"
        ]
      },
      "mark_read": {
        "type": "boolean"
      },
      "resolve_max_attempts": {
        "type": "number"
      },
      "resolve_wait_ms": {
        "type": "number"
      },
      "require_apply": {
        "type": "boolean"
      }
    },
    "required": [
      "connection_id",
      "operation"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "result": {
        "type": "object",
        "properties": {
          "operation": {
            "type": "string"
          },
          "message_id": {
            "type": "string"
          },
          "applied": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "Categories newly added to the message."
          },
          "preserved": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "Categories that were already on the message and were preserved."
          },
          "final_categories": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "Final categories on the message after the operation."
          },
          "ensured": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "Categories created in the master list."
          },
          "already_existed": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "Categories that already existed in the master list."
          },
          "master_category_ensured": {
            "type": "boolean"
          },
          "conversation_id_resolved": {
            "type": "string",
            "description": "Thread id used to resolve messages (multi-mailbox mode)."
          },
          "messages_per_mailbox": {
            "type": "object",
            "description": "Count of messages labeled per mailbox."
          },
          "messages_read_by_mailbox": {
            "type": "object",
            "description": "Count of messages marked read (isRead:true) per mailbox, when mark_read is set."
          }
        }
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Success",
      "description": "Category operation completed successfully.",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Operation failed (auth error, invalid message ID, API error).",
      "is_default": false,
      "supports_condition": true
    }
  ],
  "execution_mode": "async",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard-io",
    "cloud-io"
  ],
  "tags": [
    "email",
    "microsoft",
    "outlook",
    "office365",
    "category",
    "label",
    "connector"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://me.veripass.com/sommatic",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string",
      "title": "Microsoft 365 Credential",
      "description": "Credential for Microsoft Graph API access. Requires `Mail.ReadWrite` (for PATCH /messages/{id}) and `MailboxSettings.ReadWrite` (for POST /outlook/masterCategories). Accepts credentials of type `oauth2` (access + refresh token), `service-account` (tenant id + client id + client secret), or `object` (advanced Graph config).",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "oauth2",
          "service-account",
          "object"
        ],
        "types": [
          "outlook",
          "microsoft365",
          "oauth2"
        ]
      }
    },
    "mailbox_mode": {
      "type": "string",
      "title": "Mailbox",
      "enum": [
        "token_owner",
        "other"
      ],
      "default": "token_owner",
      "description": "Token owner: uses the mailbox of the account that generated the credential. Other mailbox: specify an email address (requires Application permissions in Azure AD)."
    },
    "mailbox_email": {
      "title": "Mailbox Email(s)",
      "description": "Single email or array/comma-separated list of mailboxes to operate on. When more than one is provided, the worker fans out and applies the categories in every mailbox that contains the same message (matched by internetMessageId).",
      "format": "expression",
      "displayOptions": {
        "show": {
          "mailbox_mode": [
            "other"
          ]
        }
      }
    },
    "internet_message_id": {
      "type": "string",
      "title": "Internet Message ID",
      "description": "RFC-5322 Internet Message ID of the email (e.g., <BL0PR16MB...@outlook.com>). Required for multi-mailbox mode: the worker uses it to find the local message id in each mailbox via $filter=internetMessageId eq. When omitted and a single mailbox is provided, the worker falls back to the legacy single-mailbox flow using `message_id` directly.",
      "format": "expression",
      "displayOptions": {
        "show": {
          "operation": [
            "apply",
            "apply_with_ensure"
          ]
        }
      }
    },
    "operation": {
      "type": "string",
      "title": "Operation",
      "enum": [
        "apply",
        "ensure_master_category",
        "apply_with_ensure"
      ],
      "default": "apply_with_ensure",
      "description": "apply: apply categories to an existing message (merge-safe). ensure_master_category: create categories in the user master list (idempotent). apply_with_ensure: ensure + apply in one call (recommended)."
    },
    "message_id": {
      "type": "string",
      "title": "Message ID",
      "description": "Graph message ID of the email to categorize.",
      "format": "expression",
      "displayOptions": {
        "show": {
          "operation": [
            "apply",
            "apply_with_ensure"
          ]
        }
      }
    },
    "categories": {
      "type": "array",
      "title": "Categories",
      "items": {
        "type": [
          "string",
          "object"
        ]
      },
      "description": "Categories to apply or ensure. Accepts: an array of display-name strings, a comma-separated string, OR an array of { name, color } objects to set a distinct color per category (e.g. [{name:\"Recibido por IA - Sommatic\",color:\"preset8\"},{name:\"Asignado DAUITA\",color:\"preset4\"}]). When an item has no color, the node-level category_color is used.",
      "format": "expression"
    },
    "category_color": {
      "type": "string",
      "title": "Category Color",
      "enum": [
        "preset0",
        "preset1",
        "preset2",
        "preset3",
        "preset4",
        "preset5",
        "preset6",
        "preset7",
        "preset8",
        "preset9",
        "preset10",
        "preset11",
        "preset12",
        "preset13",
        "preset14",
        "preset15",
        "preset16",
        "preset17",
        "preset18",
        "preset19",
        "preset20",
        "preset21",
        "preset22",
        "preset23",
        "preset24",
        "none"
      ],
      "description": "Outlook category color preset. Required for ensure_master_category and apply_with_ensure (ignored by apply). Common values: preset0 (Red), preset8 (Purple), preset23 (Dark Purple - closest to Sommatic AI corporate #2A213B). Use \"none\" for no color.",
      "displayOptions": {
        "show": {
          "operation": [
            "ensure_master_category",
            "apply_with_ensure"
          ]
        }
      }
    },
    "match_scope": {
      "type": "string",
      "title": "Match Scope",
      "enum": [
        "conversation",
        "message"
      ],
      "default": "conversation",
      "description": "conversation (default): an internet_message_id is probed and expanded to the WHOLE thread (labels original + replies/forwards). message: resolve ONLY the exact internet_message_id matches (the reply/forward copy) — never expand to the conversation. Use \"message\" to label/mark-read a specific IA-generated copy without touching the citizen original.",
      "displayOptions": {
        "show": {
          "operation": [
            "apply",
            "apply_with_ensure"
          ]
        }
      }
    },
    "mark_read": {
      "type": "boolean",
      "title": "Mark as Read",
      "default": false,
      "description": "After labeling, PATCH { isRead:true } on every resolved message. Scope it to IA-generated copies only (via match_scope:\"message\" or by targeting the forward_recipient mailbox); never point a mark_read node at the citizen original, which must stay unread.",
      "displayOptions": {
        "show": {
          "operation": [
            "apply",
            "apply_with_ensure"
          ]
        }
      }
    },
    "resolve_max_attempts": {
      "type": "number",
      "title": "Resolve Max Attempts",
      "default": 1,
      "description": "Bounded retry on the message-resolution query. Absorbs Graph indexing latency of a just-sent reply/forward copy (a \"success that resolved 0 messages\" does not re-fire the node retry). Use e.g. 6 for IA-copy nodes; leave 1 for legacy calls.",
      "displayOptions": {
        "show": {
          "operation": [
            "apply",
            "apply_with_ensure"
          ]
        }
      }
    },
    "resolve_wait_ms": {
      "type": "number",
      "title": "Resolve Wait (ms)",
      "default": 0,
      "description": "Delay between resolution attempts in milliseconds (e.g. 1500). Only relevant when resolve_max_attempts > 1.",
      "displayOptions": {
        "show": {
          "operation": [
            "apply",
            "apply_with_ensure"
          ]
        }
      }
    },
    "require_apply": {
      "type": "boolean",
      "title": "Require Apply",
      "default": false,
      "description": "When true, emit port:\"error\" if nothing was labeled after all retries (instead of a silent success). Lets the caller drive a fallback branch (e.g. a notice + human task).",
      "displayOptions": {
        "show": {
          "operation": [
            "apply",
            "apply_with_ensure"
          ]
        }
      }
    },
    "execution": {
      "type": "object",
      "title": "Execution Settings",
      "properties": {
        "alwaysOutputData": {
          "type": "boolean",
          "title": "Always Output Data",
          "description": "Always output data, even on error (if configured).",
          "default": false
        },
        "retryOnFail": {
          "type": "boolean",
          "title": "Retry On Fail",
          "description": "Retry the execution if it fails.",
          "default": false
        },
        "maxTries": {
          "type": "integer",
          "title": "Max Retries",
          "description": "Number of retries.",
          "default": 3,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "waitBetweenTries": {
          "type": "integer",
          "title": "Wait Between Retries (ms)",
          "default": 1000,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "continueOnFail": {
          "type": "boolean",
          "title": "Continue On Fail",
          "description": "Do not crash workflow on error.",
          "default": false
        },
        "timeout_ms": {
          "type": "integer",
          "title": "Timeout (ms)",
          "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
          "default": 300000
        }
      }
    }
  },
  "required": [
    "connection_id",
    "operation"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "operation": {
      "type": "string"
    },
    "message_id": {
      "type": "string"
    },
    "internet_message_id": {
      "type": "string"
    },
    "conversation_id": {
      "type": "string"
    },
    "categories": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "category_color": {
      "type": "string"
    },
    "match_scope": {
      "type": "string",
      "enum": [
        "conversation",
        "message"
      ]
    },
    "mark_read": {
      "type": "boolean"
    },
    "resolve_max_attempts": {
      "type": "number"
    },
    "resolve_wait_ms": {
      "type": "number"
    },
    "require_apply": {
      "type": "boolean"
    }
  },
  "required": [
    "connection_id",
    "operation"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "result": {
      "type": "object",
      "properties": {
        "operation": {
          "type": "string"
        },
        "message_id": {
          "type": "string"
        },
        "applied": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "description": "Categories newly added to the message."
        },
        "preserved": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "description": "Categories that were already on the message and were preserved."
        },
        "final_categories": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "description": "Final categories on the message after the operation."
        },
        "ensured": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "description": "Categories created in the master list."
        },
        "already_existed": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "description": "Categories that already existed in the master list."
        },
        "master_category_ensured": {
          "type": "boolean"
        },
        "conversation_id_resolved": {
          "type": "string",
          "description": "Thread id used to resolve messages (multi-mailbox mode)."
        },
        "messages_per_mailbox": {
          "type": "object",
          "description": "Count of messages labeled per mailbox."
        },
        "messages_read_by_mailbox": {
          "type": "object",
          "description": "Count of messages marked read (isRead:true) per mailbox, when mark_read is set."
        }
      }
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Success",
    "description": "Category operation completed successfully.",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Operation failed (auth error, invalid message ID, API error).",
    "is_default": false,
    "supports_condition": true
  }
]
```
