# Manual / Ad-Hoc Trigger

> 允许用户或管理员手动启动工作流。

Source: https://docs.sommatic.ai/zh/nodes/manual.trigger/

- 标识: `manual.trigger`
- 类别: 触发器
- 版本: 1.0.0
- 状态: 可用
- 执行方式: sync

允许用户或管理员手动启动工作流。

该触发器为工作流定义“启动按钮”。它常用于测试、调试，或需要人工显式发起的工作流（例如“运行月末报表”）。

**功能特性：**
- **表单输入：** 可以为用户点击“Run”时必须提供的输入载荷定义 schema。
- **测试：** 非常适合用多组不同的测试数据集验证逻辑。

**适用场景：**
- 启动一次性的数据库迁移脚本。
- 用模拟客户数据测试新的工作流。

## 配置

- `payload_template` (object, 可选): 自动预填手动输入表单，方便使用。

配置中会显示通用执行设置，但该节点目前不执行重试（`retryOnFail`、`maxTries`）。所有节点均未实现 `continueOnFail`。

## 输出端口

- `out` (默认): Started
- `error`: Error

## 输出契约

- `triggered_by` (string, 可选): 点击 Run 的用户。
- `timestamp` (string, 可选)
- `manual_payload` (object, 可选): 用户输入的数据。

## 在表达式中

后续节点可通过 `$("Manual / Ad-Hoc Trigger").item.json.<路径>` 读取此节点输出中的内容（请把 `Manual / Ad-Hoc Trigger` 换成你的节点标签）。**Editor** 标记工作流编辑器在 Previous Nodes 中提示的路径（输出契约）；**Worker** 标记 worker 实际通过 `out` 返回的路径，依据“端口”一节所示的输出。

- `$("Manual / Ad-Hoc Trigger").item.json.triggered_by` (string) = `"manual"`: 点击 Run 的用户。
- `$("Manual / Ad-Hoc Trigger").item.json.timestamp` (string) = `"2026-11-26T09:15:42.008-05:00"`
- `$("Manual / Ad-Hoc Trigger").item.json.manual_payload` (object) = `{…}`: 用户输入的数据。
- `$("Manual / Ad-Hoc Trigger").item.json.manual_payload.period` (string) = `"2026-10"` (编辑器不提示)
- `$("Manual / Ad-Hoc Trigger").item.json.manual_payload.region` (string) = `"north"` (编辑器不提示)
- `$("Manual / Ad-Hoc Trigger").item.json.period` (string) = `"2026-10"` (编辑器不提示)
- `$("Manual / Ad-Hoc Trigger").item.json.region` (string) = `"north"` (编辑器不提示)


## 工作原理

用于手动或通过执行请求启动的工作流的入口节点。它将 `payload_template` 与输入合并，结果同时放在 `manual_payload` 和根级输出中，并附带 `triggered_by` 和 `timestamp`。

1. 触发来源：在编辑器中运行，或携带输入的执行请求。运行草稿为测试运行；运行已发布版本为生产运行。
2. 解析节点配置并合并 `{ ...payload_template, ...input }`：输入中的键优先于模板。
3. 经 `out` 输出 `{ triggered_by, timestamp, manual_payload, ...merged }`。

## 在工作流中

### 节点

```json
{
  "node_id": "node-start",
  "label": "Run report",
  "operator_slug": "manual.trigger",
  "operator_version": "1.0.0",
  "config": {
    "payload_template": {
      "region": "north"
    }
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "triggered_by": "manual",
    "timestamp": "2026-11-26T09:15:42.008-05:00",
    "manual_payload": {
      "region": "north",
      "period": "2026-10"
    },
    "region": "north",
    "period": "2026-10"
  },
  "port": "out"
}
```

## 输出端口 (代码中的行为)

### `out`

总是从此端口输出，除非发生意外异常。

```json
{
  "triggered_by": "manual",
  "timestamp": "2026-11-26T09:15:42.008-05:00",
  "manual_payload": {
    "period": "2026-10",
    "region": "north"
  },
  "period": "2026-10",
  "region": "north"
}
```

### `error`

仅在发生意外异常时输出，内容为 `{ error, response }`。正常使用中不会出现。

```json
{
  "error": "<exception message>",
  "response": null
}
```

## 代码中的行为

- `payload_template`: 作为默认值与输入合并（输入优先）。若某键与 `triggered_by`、`timestamp` 或 `manual_payload` 同名，合并后的值会在根级覆盖它们。

## 说明与限制

- `triggered_by` 始终为 `manual`：启动运行的用户不会传入本节点。
- `timestamp` 为编排器本地时间，采用 ISO 格式。
- 当工作流保存了触发器状态或键值状态时，后端会将 `$trigger_state` / `$kv_state` 加入输入，因此它们也会出现在输出中。
- 使用示例数据时，引擎会跳过本节点，经 `out` 原样输出示例输入，不包含 `triggered_by`、`timestamp` 和 `manual_payload`。


## 契约（JSON Schema）

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

```json
{
  "slug": "manual.trigger",
  "name": "Manual / Ad-Hoc Trigger",
  "category": "triggers",
  "version": "1.0.0",
  "description": "Allows a workflow to be started manually by a user or administrator.\n\n  This trigger defines the \"Start Button\" for a workflow. It is commonly used for testing, debugging, or workflows that require explicit human initiation (e.g., \"Run End of Month Report\").\n\n  **Features:**\n  - **Form Input:** Can define a schema for the input payload the user must provide when clicking \"Run\".\n  - **Testing:** Ideal for validating logic with disparate test datasets.\n\n  **Use Cases:**\n  - Launching a one-off database migration script.\n  - Testing a new workflow with mock customer data.",
  "capabilities": [
    "testing",
    "ad-hoc",
    "manual",
    "ui"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "payload_template": {
        "type": "object",
        "title": "Default/Sample Payload",
        "description": "Auto-fills the manual input form for convenience."
      },
      "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
          }
        }
      }
    }
  },
  "input_contract_schema": null,
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "triggered_by": {
        "type": "string",
        "description": "User who clicked Run."
      },
      "timestamp": {
        "type": "string",
        "format": "date-time"
      },
      "manual_payload": {
        "type": "object",
        "description": "Data entered by user."
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Started",
      "is_default": true
    },
    {
      "port_id": "error",
      "label": "Error"
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "test",
    "manual",
    "debug",
    "start"
  ],
  "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": {
    "payload_template": {
      "type": "object",
      "title": "Default/Sample Payload",
      "description": "Auto-fills the manual input form for convenience."
    },
    "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
        }
      }
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "triggered_by": {
      "type": "string",
      "description": "User who clicked Run."
    },
    "timestamp": {
      "type": "string",
      "format": "date-time"
    },
    "manual_payload": {
      "type": "object",
      "description": "Data entered by user."
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Started",
    "is_default": true
  },
  {
    "port_id": "error",
    "label": "Error"
  }
]
```
