# 触发器

> 通过 Webhook 或其他工作流启动工作流，并管理 Ingress Binding。

Source: https://docs.sommatic.ai/zh/api/reference/triggers/

## 通过链式调用触发工作流

`POST /workflow-orchestration/trigger/chain/{mode}`

从另一个工作流的链式节点分派一次工作流执行。按 slug 或定义 ID 解析目标工作流。模式决定执行测试版本（草稿）还是生产版本（已发布版本）。

认证: Bearer 令牌

### 参数

- `mode` (path, string, 必填): 执行模式。“test”执行草稿，“prod”执行已发布版本。 可选值: `test`, `prod`.

### 请求体

链式调用载荷，包含目标工作流和输入数据。

- `organization_id` (string, 必填): 目标工作流所属的组织范围。
- `target_flow_slug` (string): 要调用的目标工作流的标识（slug）。
- `target_flow_definition_id` (string): 明确指定的流程定义 ID，优先于 slug。
- `execution_mode` (string): `sync` 会等待执行完成；`async` 立即返回。 可选值: `sync`, `async`.
- `data` (object): 传给目标工作流的输入数据。
- `chain_context` (object): 来自调用方工作流的追踪元数据。
  - `caller_flow_instance_id` (string)
  - `caller_flow_slug` (string)
  - `caller_node_id` (string)
  - `correlation_id` (string)

### 响应

- `200`: 工作流执行成功（同步模式）。
- `202`: 工作流已分派（异步模式）。
- `404`: 未找到目标工作流。
- `500`: 服务器内部错误。

### 请求示例

```bash
curl -X POST "https://api.sommatic.ai/workflow-orchestration/trigger/chain/<mode>" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
       "organization_id": "<organization_id>"
     }'
```

## dispatchChainStream (/workflow-orchestration/trigger/chain/:mode/stream)

`POST /workflow-orchestration/trigger/chain/{mode}/stream`

认证: Bearer 令牌

### 参数

- `mode` (path, string, 必填)

### 响应


### 请求示例

```bash
curl -X POST "https://api.sommatic.ai/workflow-orchestration/trigger/chain/<mode>/stream" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -N
```

## 通过 Webhook 触发工作流

`POST /workflow-orchestration/trigger/webhooks/{mode}/{bindingIdentifier}`

外部入口：任何能够发起 HTTP POST 的系统都可以通过它执行工作流。请求会按 id 或自定义 slug 解析为一个 `IngressBinding`，并将其目标 `FlowDefinition` 分派给编排器。

原始 HTTP 请求会作为工作流的初始 `$input` 传入，结构为 `{ body, headers, query, method }`。下游节点可通过 `{{ $json.body.<field> }}` 读取 POST 的载荷，也可以引用上游节点：`{{ $('Webhook Receiver').item.json.body.<field> }}`。

默认情况下调用为同步，返回完整的编排器结果。对于长时间运行的工作流，可设置 `execution_mode=async`——响应会立即返回 `instance_id`，并可通过 `/workflow-orchestration/execution/stream` 的 SSE 通道跟踪进度。

该端点按设计不做身份验证。各绑定的鉴权（共享密钥、签名校验、IP 白名单）由 `IngressBinding` 的配置负责，而不是由此路由负责。

认证: 由 Ingress Binding 负责认证

### 参数

- `mode` (path, string, 必填): `test` 运行草稿；`prod` 运行最新的已发布版本。 可选值: `test`, `prod`.
- `bindingIdentifier` (path, string, 必填): `IngressBinding` 的 id（推荐）或其自定义 slug。
- `execution_mode` (query, string): 对于长时间运行的流程，可用 `async` 覆盖默认的 `sync` 行为。 可选值: `sync`, `async`.

### 请求体

任意 JSON 载荷。完整 JSON 在工作流中为 `$json.body`。若请求体中存在以下两个便捷键，会被读取：`event`（路由提示）和 `execution_mode`（行为与同名查询参数相同）。


### 响应

- `202`: 已接受。在 `sync` 模式下，响应包含编排器的最终结果；在 `async` 模式下，立即返回 `status: running` 和用于通过 SSE 流跟踪的 `instance_id`。
- `400`: 路径或查询参数缺失或无效。
- `404`: 未找到 `IngressBinding`，或其目标 `FlowDefinition` 无法解析为现有工作流。在 `prod` 模式下，若没有已发布的默认版本，也会返回此状态。
- `413`: 请求体超过该路由的大小上限（5 MB）。
- `500`: 编排器不可达，或分派过程中出现未处理的错误。

### 请求示例

```bash
curl -X POST "https://api.sommatic.ai/workflow-orchestration/trigger/webhooks/<mode>/<bindingIdentifier>" \
  -H "Content-Type: application/json" \
  -d '{}'
```

## 创建入口绑定。

`POST /workflow-orchestration/triggers/ingress-binding/`

返回根据所提供数据创建的入口绑定。

认证: Bearer 令牌

### 请求体

要创建的 IngressBinding 对象。

- `organization_id` (string, 必填): 该入口绑定所属组织的 ID。
- `slug` (string, 必填): 唯一标识该入口绑定的稳定语义键（例如 ingress.http.email_inbound、ingress.queue.support_tickets）。
- `ingress_kind` (string, 必填): 入口类型："http"、"webhook"、"queue"、"event-grid"、"cron"、"pubsub"、"s3-event" 等。
- `description` (string): 可选，用于说明入口行为和用途的可读描述。
- `route` (string): 该绑定的路径、主题、订阅或表达式。对于创建时未指定路由的 webhook 绑定，服务会设置为 "/trigger/webhooks/{test|prod}/{slug or id}"；公开端点为 POST /workflow-orchestration/trigger/webhooks/{mode}/{bindingIdentifier}。示例：- "/trigger/webhooks/prod/ingress.http.inbound_email" - "queue:support_tickets" - "topic:user.events" - "cron:0 /5 * * * *"
- `target_trigger_id` (string): 接收该入口绑定事件的 TriggerModel 的标识符。
- `target_flow_version_id` (string): 可选，绕过 TriggerModel 时直接引用的 FlowVersionModel（高级模式）。
- `target_flow_definition_id` (string): 可选，绑定到草稿流程的 webhook 触发器直接引用的 FlowDefinitionModel。
- `authentication` (object): 该入口端点的认证与授权要求。
  - `mode` (string): "none"、"api-key"、"oauth2"、"veripass-session"、"service-token"、"custom"。
  - `allowed_api_keys` (array<string>)
  - `oauth_scopes` (array<string>)
  - `veripass_policy` (string): 可选，访问该入口端点所需的 Veripass 策略（角色、作用域、上下文规则）。
- `payload_mapping` (object): 传入载荷的解码与校验规则，定义外部数据如何转换为内部 EnvelopeModel 载荷。
  - `content_type` (string): "json"、"form-data"、"text"、"binary"、"base64" 或自定义解析器标识符。
  - `json_path` (string): 可选，类似 JSONPath 的选择器，用于提取载荷中的子结构。
  - `raw_body` (boolean): 为 true 时，在信封中保留原始 HTTP 请求体。
  - `schema` (object): 可选，类似 JSON Schema 的结构，用于校验传入载荷。
- `correlation_strategy` (string): 为信封推导或生成 correlation_id 的策略：- "from-header" - "from-payload" - "auto-generate" - "custom-rule"
- `correlation_field` (string): 当 correlation_strategy 引用请求头或载荷时使用的字段名（例如 "x-correlation-id"）。
- `instance_creation` (object): 定义收到事件时是否以及如何创建新的 FlowInstanceModel 执行的规则。
  - `mode` (string): "always-new"、"resume-existing"、"merge"、"conditional"、"none"。
  - `resume_lookup_key` (string): 在 "resume-existing" 模式下用于查找现有流程实例的字段（请求头或载荷）。
- `rate_limit` (object): 可选的入口级限流，防止过度激活。
  - `rpm` (integer)
  - `rps` (integer)
- `is_system` (boolean): 为 true 表示该绑定由平台内部管理。
- `is_enabled` (boolean): 为 true 表示该入口绑定当前处于启用状态。
- `target_node_id` (string): 该绑定所关联的流程图中的节点 ID，用于实现每个节点仅一对绑定的约束。
- `mode` (string): 该绑定的执行模式："test"（通过 flow_definition_id 执行草稿）或 "production"（通过 flow_version_id 执行已发布版本）。 可选值: `test`, `production`.
- `is_deprecated` (boolean): 为 true 表示新的集成不应再使用该绑定。

### 响应

- `200`: 成功。
- `500`: 执行此操作时出错。

### 请求示例

```bash
curl -X POST "https://api.sommatic.ai/workflow-orchestration/triggers/ingress-binding/" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
       "organization_id": "<organization_id>",
       "slug": "<slug>",
       "ingress_kind": "<ingress_kind>"
     }'
```

## 更新现有入口绑定。

`PATCH /workflow-orchestration/triggers/ingress-binding/`

使用所提供的数据更新现有入口绑定。

认证: Bearer 令牌

### 请求体

要创建的 IngressBinding 对象。

- `organization_id` (string, 必填): 该入口绑定所属组织的 ID。
- `slug` (string, 必填): 唯一标识该入口绑定的稳定语义键（例如 ingress.http.email_inbound、ingress.queue.support_tickets）。
- `ingress_kind` (string, 必填): 入口类型："http"、"webhook"、"queue"、"event-grid"、"cron"、"pubsub"、"s3-event" 等。
- `description` (string): 可选，用于说明入口行为和用途的可读描述。
- `route` (string): 该绑定的路径、主题、订阅或表达式。对于创建时未指定路由的 webhook 绑定，服务会设置为 "/trigger/webhooks/{test|prod}/{slug or id}"；公开端点为 POST /workflow-orchestration/trigger/webhooks/{mode}/{bindingIdentifier}。示例：- "/trigger/webhooks/prod/ingress.http.inbound_email" - "queue:support_tickets" - "topic:user.events" - "cron:0 /5 * * * *"
- `target_trigger_id` (string): 接收该入口绑定事件的 TriggerModel 的标识符。
- `target_flow_version_id` (string): 可选，绕过 TriggerModel 时直接引用的 FlowVersionModel（高级模式）。
- `target_flow_definition_id` (string): 可选，绑定到草稿流程的 webhook 触发器直接引用的 FlowDefinitionModel。
- `authentication` (object): 该入口端点的认证与授权要求。
  - `mode` (string): "none"、"api-key"、"oauth2"、"veripass-session"、"service-token"、"custom"。
  - `allowed_api_keys` (array<string>)
  - `oauth_scopes` (array<string>)
  - `veripass_policy` (string): 可选，访问该入口端点所需的 Veripass 策略（角色、作用域、上下文规则）。
- `payload_mapping` (object): 传入载荷的解码与校验规则，定义外部数据如何转换为内部 EnvelopeModel 载荷。
  - `content_type` (string): "json"、"form-data"、"text"、"binary"、"base64" 或自定义解析器标识符。
  - `json_path` (string): 可选，类似 JSONPath 的选择器，用于提取载荷中的子结构。
  - `raw_body` (boolean): 为 true 时，在信封中保留原始 HTTP 请求体。
  - `schema` (object): 可选，类似 JSON Schema 的结构，用于校验传入载荷。
- `correlation_strategy` (string): 为信封推导或生成 correlation_id 的策略：- "from-header" - "from-payload" - "auto-generate" - "custom-rule"
- `correlation_field` (string): 当 correlation_strategy 引用请求头或载荷时使用的字段名（例如 "x-correlation-id"）。
- `instance_creation` (object): 定义收到事件时是否以及如何创建新的 FlowInstanceModel 执行的规则。
  - `mode` (string): "always-new"、"resume-existing"、"merge"、"conditional"、"none"。
  - `resume_lookup_key` (string): 在 "resume-existing" 模式下用于查找现有流程实例的字段（请求头或载荷）。
- `rate_limit` (object): 可选的入口级限流，防止过度激活。
  - `rpm` (integer)
  - `rps` (integer)
- `is_system` (boolean): 为 true 表示该绑定由平台内部管理。
- `is_enabled` (boolean): 为 true 表示该入口绑定当前处于启用状态。
- `target_node_id` (string): 该绑定所关联的流程图中的节点 ID，用于实现每个节点仅一对绑定的约束。
- `mode` (string): 该绑定的执行模式："test"（通过 flow_definition_id 执行草稿）或 "production"（通过 flow_version_id 执行已发布版本）。 可选值: `test`, `production`.
- `is_deprecated` (boolean): 为 true 表示新的集成不应再使用该绑定。

### 响应

- `200`: 成功。
- `500`: 执行此操作时出错。

### 请求示例

```bash
curl -X PATCH "https://api.sommatic.ai/workflow-orchestration/triggers/ingress-binding/" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
       "organization_id": "<organization_id>",
       "slug": "<slug>",
       "ingress_kind": "<ingress_kind>"
     }'
```

## 按 id 删除入口绑定。

`DELETE /workflow-orchestration/triggers/ingress-binding/`

根据所提供的 ID 删除入口绑定。

认证: Bearer 令牌

### 请求体

要删除的入口绑定 ID

- `id` (string)

### 响应

- `200`: 成功。
- `500`: 执行此操作时出错。

### 请求示例

```bash
curl -X DELETE "https://api.sommatic.ai/workflow-orchestration/triggers/ingress-binding/" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
       "id": "<id>"
     }'
```

## 按查询选择器获取入口绑定。

`GET /workflow-orchestration/triggers/ingress-binding/{queryselector}`

返回与路由中指定的查询选择器和搜索条件匹配的入口绑定信息。

认证: Bearer 令牌

### 参数

- `queryselector` (path, enum, 必填): 此查询可用的筛选条件 可选值: `id`, `organization-id`, `all`.
- `search` (query, string): 用于搜索实体的关键字。
- `include_status` (query, string): 可选的状态参数，用于包含指定状态的入口绑定。
- `exclude_status` (query, string): 可选的状态参数，用于排除指定状态的入口绑定。
- `page` (query, integer): 页码，从 1 开始（默认 1）。
- `pageSize` (query, integer): 每页记录数（默认 25）。

### 响应

- `200`: 成功。
- `500`: 执行此操作时出错。

### 请求示例

```bash
curl -X GET "https://api.sommatic.ai/workflow-orchestration/triggers/ingress-binding/<queryselector>" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN"
```
