# Vectry Event Subscription

> 根据系统内部事件（Event Bus）触发工作流执行。

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

- 标识: `event.trigger`
- 类别: 触发器
- 版本: 1.0.0
- 状态: 实验性. 目前没有任何组件订阅事件总线，因此该触发器不会自行启动运行；`filters` 与 `since` 不会被读取。
- 执行方式: async

根据系统内部事件（Event Bus）触发工作流执行。

该触发器订阅 Vectry Causal Event Bus。它监听特定的业务或系统事件（例如“Order Created”“User Signup”），并在出现匹配事件时启动工作流。

**功能特性：**
- **过滤：** 只订阅符合特定条件（domain、entity、tag）的事件。
- **回放：** 处理历史事件（“since”）或只处理新事件。
- **模式匹配：** 在复杂事件序列上触发（如已配置）。

**适用场景：**
- 出现“UserCreated”事件时发送欢迎邮件。
- 审计高额交易。

## 配置

- `filters` (array<object>, 必填): 事件匹配条件。
- `since` (string, 可选): 时间戳（ISO）或“now”。 默认值: `now`.

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

## 输出端口

- `out` (默认): Event Received
- `error`: Error

## 输出契约

- `event_id` (string, 可选)
- `source` (string, 可选)
- `type` (string, 可选)
- `payload` (object, 可选)

## 在表达式中

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

- `$("Vectry Event Subscription").item.json.event_id` (string) = `"evt_01JDX4Q8M2"`
- `$("Vectry Event Subscription").item.json.source` (string) = `"vectry"`
- `$("Vectry Event Subscription").item.json.type` (string) = `"order.created"`
- `$("Vectry Event Subscription").item.json.payload` (object) = `{…}`
- `$("Vectry Event Subscription").item.json.payload.order_id` (string) = `"ord_123"` (编辑器不提示)
- `$("Vectry Event Subscription").item.json.payload.total` (number) = `129.99` (编辑器不提示)
- `$("Vectry Event Subscription").item.json.payload.currency` (string) = `"USD"` (编辑器不提示)


## 工作原理

事件驱动工作流的入口节点。它将运行输入整理为 `{ event_id, source, type, payload }`；当前版本不会订阅事件总线，过滤条件也不会生效。

1. 触发来源：当前版本中没有后端组件为本节点订阅事件总线。只有在以事件结构的输入显式启动工作流（手动运行或执行请求）时才会运行。
2. 按原样读取输入项；不读取也不解析节点配置。
3. 输出 `event_id`（默认 `unknown`）、`source`（默认 `vectry`）、`type`（默认 `unknown`）和 `payload`：若输入中有 `payload` 字段则取该字段，否则取整个输入。

## 在工作流中

### 节点

```json
{
  "node_id": "node-order-created",
  "label": "Order created",
  "operator_slug": "event.trigger",
  "operator_version": "1.0.0",
  "config": {
    "filters": [
      {
        "system_domain": "sales",
        "system_entity": "order",
        "operation": "created"
      }
    ],
    "since": "now"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "event_id": "evt_01JDX4Q8M2",
    "source": "vectry",
    "type": "order.created",
    "payload": {
      "order_id": "ord_123",
      "total": 129.99,
      "currency": "USD"
    }
  },
  "port": "out"
}
```

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

### `out`

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

```json
{
  "event_id": "evt_01JDX4Q8M2",
  "source": "vectry",
  "type": "order.created",
  "payload": {
    "order_id": "ord_123",
    "total": 129.99,
    "currency": "USD"
  }
}
```

### `error`

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

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

## 代码中的行为

- `filters`: 在 schema 中已声明且为必填，但不会被读取：不会进行任何事件过滤。`since` 同样不会被读取。

## 说明与限制

- 可将本节点视为带类型的入口：以 `{ event_id, source, type, payload }` 启动工作流，并在下游通过 `$json.payload` 读取事件数据。
- 使用示例数据时，引擎会跳过本节点，并经 `out` 原样输出示例输入。


## 契约（JSON Schema）

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

```json
{
  "slug": "event.trigger",
  "name": "Vectry Event Subscription",
  "category": "triggers",
  "version": "1.0.0",
  "description": "Trigger workflow execution based on internal system events (Event Bus).\n\n  This trigger subscribes to the Vectry Causal Event Bus. It listens for specific business or system events (e.g., \"Order Created\", \"User Signup\") and initiates the workflow when a matching event occurs.\n\n  **Features:**\n  - **Filtering:** Subscribe only to events matching specific criteria (domain, entity, tag).\n  - **Replay:** Process historical events (\"since\") or only new ones.\n  - **Pattern Matching:** Trigger on complex event sequences (if configured).\n\n  **Use Cases:**\n  - Sending a Welcome Email when a 'UserCreated' event occurs.\n  - Auditing high-value transactions.",
  "capabilities": [
    "event-subscription",
    "internal-bus",
    "pubsub",
    "vectry"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "filters"
    ],
    "properties": {
      "filters": {
        "type": "array",
        "title": "Event Filters",
        "description": "Match criteria for events.",
        "items": {
          "type": "object",
          "properties": {
            "system_domain": {
              "type": "string",
              "title": "Domain"
            },
            "system_entity": {
              "type": "string",
              "title": "Entity"
            },
            "operation": {
              "type": "string",
              "title": "Operation"
            },
            "tags": {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          }
        }
      },
      "since": {
        "type": "string",
        "title": "Start From",
        "default": "now",
        "description": "Timestamp (ISO) or \"now\"."
      },
      "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",
    "description": "The event payload.",
    "properties": {
      "event_id": {
        "type": "string"
      },
      "source": {
        "type": "string"
      },
      "type": {
        "type": "string"
      },
      "payload": {
        "type": "object"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Event Received",
      "is_default": true
    },
    {
      "port_id": "error",
      "label": "Error"
    }
  ],
  "execution_mode": "async",
  "tags": [
    "vectry",
    "events",
    "bus",
    "reactive"
  ],
  "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",
  "required": [
    "filters"
  ],
  "properties": {
    "filters": {
      "type": "array",
      "title": "Event Filters",
      "description": "Match criteria for events.",
      "items": {
        "type": "object",
        "properties": {
          "system_domain": {
            "type": "string",
            "title": "Domain"
          },
          "system_entity": {
            "type": "string",
            "title": "Entity"
          },
          "operation": {
            "type": "string",
            "title": "Operation"
          },
          "tags": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        }
      }
    },
    "since": {
      "type": "string",
      "title": "Start From",
      "default": "now",
      "description": "Timestamp (ISO) or \"now\"."
    },
    "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",
  "description": "The event payload.",
  "properties": {
    "event_id": {
      "type": "string"
    },
    "source": {
      "type": "string"
    },
    "type": {
      "type": "string"
    },
    "payload": {
      "type": "object"
    }
  }
}
```

### ports

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