# Workflow Chain Receiver

> 接收来自其他工作流 Chain 节点的执行请求。

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

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

接收来自其他工作流 Chain 节点的执行请求。

该触发器是被其他工作流通过 `workflow.chain` 节点调用时的入口。它支持对等的工作流链式调用，而无需建立父子层级。

**功能特性：**
- **调用方过滤：** 可选地限制哪些工作流可以链式调用本工作流。
- **载荷透传：** 调用方工作流的载荷在下游节点中以 `$json` 提供。
- **可追溯性：** 保留链式调用上下文（调用方实例、节点、关联 ID），用于审计。

**适用场景：**
- 模块化组合工作流（例如由多个调用方共用的校验工作流）。
- 事件驱动的链式调用：工作流 A 完成一个阶段后委派给工作流 B。
- 可复用的子流程，且仍可独立测试和部署。

## 配置

- `accepted_caller_ids` (array<string>, 可选): 选择允许调用本工作流的工作流。留空则接受任何工作流的调用。 默认值: `[]`.

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

## 输出端口

- `out` (默认): 已接收并校验链式调用载荷。
- `error`: 调用方未获授权或校验失败。

## 输出契约

- `data` (object, 可选): 链式调用的输入数据。
- `chain_context` (object, 可选): 来自调用方的可追溯性元数据。
  - `caller_flow_definition_id` (string, 可选)
  - `caller_flow_instance_id` (string, 可选)
  - `caller_flow_slug` (string, 可选)
  - `caller_node_id` (string, 可选)
  - `correlation_id` (string, 可选)

## 在表达式中

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

- `$("Workflow Chain Receiver").item.json.data` (object): 链式调用的输入数据。 **worker 不返回：读取结果为 undefined.**
- `$("Workflow Chain Receiver").item.json.chain_context` (object) = `{…}`: 来自调用方的可追溯性元数据。
- `$("Workflow Chain Receiver").item.json.chain_context.caller_flow_definition_id` (string) = `"<flow_definition_id>"`
- `$("Workflow Chain Receiver").item.json.chain_context.caller_flow_instance_id` (string) = `"<flow_instance_id>"`
- `$("Workflow Chain Receiver").item.json.chain_context.caller_flow_slug` (string) = `"order-intake"`
- `$("Workflow Chain Receiver").item.json.chain_context.caller_node_id` (string) = `"node-call-billing"`
- `$("Workflow Chain Receiver").item.json.chain_context.correlation_id` (string) = `"chain-8f2k1m9x0q4w7e3r5t6y"`
- `$("Workflow Chain Receiver").item.json.order_id` (string) = `"ord_123"` (编辑器不提示)
- `$("Workflow Chain Receiver").item.json.customer_email` (string) = `"ana@example.com"` (编辑器不提示)
- `$("Workflow Chain Receiver").item.json.chain_context.root_flow_instance_result_id` (string) = `"<flow_instance_result_id>"` (编辑器不提示)


## 工作原理

由 `workflow.chain` 调用的子工作流的入口节点。可选择限制允许调用它的工作流，然后输出调用方发送的数据及 `chain_context`。

1. 触发来源：其他工作流中的 `workflow.chain` 节点。调用方为草稿运行（`test`）时，后端运行本工作流的草稿；调用方运行已发布版本（`prod`）时，运行默认的已发布版本；本工作流没有已发布版本时 `prod` 调用失败。运行输入为 `{ data, chain_context }`。
2. 解析节点配置。若 `accepted_caller_ids` 非空且 `chain_context.caller_flow_definition_id` 不在其中，则返回 `error`。
3. 否则将 `data` 展开到根级，添加 `chain_context`，并在 `meta` 中返回调用方的定义 ID、slug、实例 ID 以及链路的关联 ID。

## 在工作流中

### 节点

```json
{
  "node_id": "node-from-parent",
  "label": "Called by order intake",
  "operator_slug": "workflow.trigger",
  "operator_version": "1.0.0",
  "config": {
    "accepted_caller_ids": [
      "<flow_definition_id>"
    ]
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "order_id": "ord_123",
    "customer_email": "ana@example.com",
    "chain_context": {
      "caller_flow_definition_id": "<flow_definition_id>",
      "caller_flow_instance_id": "<flow_instance_id>",
      "caller_flow_slug": "order-intake",
      "caller_node_id": "node-call-billing",
      "correlation_id": "chain-8f2k1m9x0q4w7e3r5t6y",
      "root_flow_instance_result_id": "<flow_instance_result_id>"
    }
  },
  "port": "out",
  "meta": {
    "caller_flow_definition_id": "<flow_definition_id>",
    "caller_flow_slug": "order-intake",
    "caller_flow_instance_id": "<flow_instance_id>",
    "correlation_id": "chain-8f2k1m9x0q4w7e3r5t6y"
  }
}
```

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

### `out`

调用方被接受。

```json
{
  "order_id": "ord_123",
  "customer_email": "ana@example.com",
  "chain_context": {
    "caller_flow_definition_id": "<flow_definition_id>",
    "caller_flow_instance_id": "<flow_instance_id>",
    "caller_flow_slug": "order-intake",
    "caller_node_id": "node-call-billing",
    "correlation_id": "chain-8f2k1m9x0q4w7e3r5t6y",
    "root_flow_instance_result_id": "<flow_instance_result_id>"
  }
}
```

### `error`

调用方不在 `accepted_caller_ids` 中。输出为信封 `{ ok, errors, port, meta: { rejected_caller_id, rejected_caller_slug } }`。

```json
{
  "ok": false,
  "errors": [
    "Caller workflow 'order-intake' is not in the accepted callers list"
  ],
  "port": "error",
  "meta": {
    "rejected_caller_id": "<flow_definition_id>",
    "rejected_caller_slug": "order-intake"
  }
}
```

## 代码中的行为

- `accepted_caller_ids`: 与 `chain_context.caller_flow_definition_id`（工作流定义 ID，而非 slug）进行匹配。为空时接受所有调用方。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `Caller workflow '<caller_flow_slug or caller_flow_definition_id>' is not in the accepted callers list` | `error` | 将调用方工作流的定义 ID 加入 `accepted_caller_ids`，或保持列表为空以接受任意调用方。列表非空时，未携带 `chain_context` 启动的运行（例如手动运行）同样会被拒绝。 |

## 说明与限制

- 调用方数据的键会展开到根级，下游可通过 `$json.<field>` 读取；若 `data` 中含有 `chain_context` 键，会被真实的 `chain_context` 覆盖。
- `chain_context.root_flow_instance_result_id` 在整条调用链的每一跳中保持不变，因此任何子工作流都能报告顶层运行的 ID。
- 使用示例数据时，引擎会跳过本节点，经 `out` 原样输出示例输入（不会展开 `data`）。


## 契约（JSON Schema）

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

```json
{
  "slug": "workflow.trigger",
  "name": "Workflow Chain Receiver",
  "category": "triggers",
  "version": "1.0.0",
  "description": "Receives execution requests from another workflow's Chain node.\n\n  This trigger acts as the entry point for a workflow that is invoked by another workflow via the `workflow.chain` node. It enables peer-to-peer workflow chaining without creating a parent-child hierarchy.\n\n  **Features:**\n  - **Caller Filtering:** Optionally restrict which workflows can chain into this one.\n  - **Payload Passthrough:** The calling workflow's payload is available as `$json` in downstream nodes.\n  - **Traceability:** Chain context (caller instance, node, correlation ID) is preserved for audit.\n\n  **Use Cases:**\n  - Modular workflow composition (e.g., shared validation workflow invoked by multiple callers).\n  - Event-driven chaining where Workflow A completes a phase and delegates to Workflow B.\n  - Reusable sub-processes that remain independently testable and deployable.",
  "capabilities": [
    "workflow",
    "chain",
    "composition",
    "trigger"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "accepted_caller_ids": {
        "type": "array",
        "items": {
          "type": "string"
        },
        "title": "Accepted Callers",
        "description": "Select which workflows are allowed to invoke this one. Leave empty to accept calls from any workflow.",
        "default": [],
        "x-ui-widget": "flow-definition-multi-picker"
      },
      "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 payload sent by the calling workflow.",
    "properties": {
      "data": {
        "type": "object",
        "description": "The chain input data."
      },
      "chain_context": {
        "type": "object",
        "description": "Traceability metadata from the caller.",
        "properties": {
          "caller_flow_definition_id": {
            "type": "string"
          },
          "caller_flow_instance_id": {
            "type": "string"
          },
          "caller_flow_slug": {
            "type": "string"
          },
          "caller_node_id": {
            "type": "string"
          },
          "correlation_id": {
            "type": "string"
          }
        }
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Received",
      "is_default": true,
      "description": "Chain payload received and validated."
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Caller not authorized or validation failed."
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "workflow",
    "chain",
    "trigger",
    "composition"
  ],
  "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": {
    "accepted_caller_ids": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "title": "Accepted Callers",
      "description": "Select which workflows are allowed to invoke this one. Leave empty to accept calls from any workflow.",
      "default": [],
      "x-ui-widget": "flow-definition-multi-picker"
    },
    "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 payload sent by the calling workflow.",
  "properties": {
    "data": {
      "type": "object",
      "description": "The chain input data."
    },
    "chain_context": {
      "type": "object",
      "description": "Traceability metadata from the caller.",
      "properties": {
        "caller_flow_definition_id": {
          "type": "string"
        },
        "caller_flow_instance_id": {
          "type": "string"
        },
        "caller_flow_slug": {
          "type": "string"
        },
        "caller_node_id": {
          "type": "string"
        },
        "correlation_id": {
          "type": "string"
        }
      }
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Received",
    "is_default": true,
    "description": "Chain payload received and validated."
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Caller not authorized or validation failed."
  }
]
```
