# Handoff to Sommatic App

> 终止节点：将已完成工作流的结果移交给某个 Sommatic AI App。它会创建一个携带打开描述符的持久化 HITL 任务；若有活动的 Command Center 会话订阅，则立即打开该应用。它是 sommatic.app.execute（应用作为关卡）的对称形态；此节点是应用作为终点：工作流就此结束，不会恢复。

Source: https://docs.sommatic.ai/zh/nodes/sommatic.app.handoff/

- 标识: `sommatic.app.handoff`
- 类别: 人工介入
- 版本: 1.0.0
- 状态: 可用
- 执行方式: sync

终止节点：将已完成工作流的结果移交给某个 Sommatic AI App。它会创建一个携带打开描述符的持久化 HITL 任务；若有活动的 Command Center 会话订阅，则立即打开该应用。它是 sommatic.app.execute（应用作为关卡）的对称形态；此节点是应用作为终点：工作流就此结束，不会恢复。

## 配置

- `target` (object, 必填)
  - `app_slug` (string, 必填): 要移交的 Sommatic AI App 的 slug（例如 my-review-app）。
  - `route` (string, 可选): 在应用内打开的路由（例如 /new-sentence）。默认为 /。 默认值: `/`.
  - `launch_mode` (string, 可选): 存在活动会话时应用的打开方式。 默认值: `command-center`.
- `input_mapping` (object, 可选): 预填入应用的载荷，由服务端根据 {{...}} 表达式解析。
- `organization_id` (string, 必填): 拥有所创建任务的组织。
- `assignee` (string, 必填): 任务分配给的身份（邮箱）。
- `task_title` (string, 必填): 为此次移交创建的持久化 HITL 任务的标题。
- `task_type` (object, 可选): 任务类型描述符。默认为 review。
- `task_priority` (object, 可选): 任务优先级描述符。默认为 medium。

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

## 输出端口

- `out` (默认): 移交已送达（任务已创建；若有活动会话则已打开应用）。
- `error`: Error

## 输入契约

- `target` (object, 可选)
- `input_mapping` (object, 可选)
- `organization_id` (string, 可选)
- `assignee` (string, 可选)
- `task_title` (string, 可选)

## 输出契约

- `app_handoff_descriptor` (object, 可选)
- `task_id` (string, 可选)
- `task_title` (string, 可选)

## 在表达式中

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

- `$("Handoff to Sommatic App").item.json.app_handoff_descriptor` (object) = `{…}`
- `$("Handoff to Sommatic App").item.json.task_id` (string) = `"task-xxxxxxxxxxxxxxx"`
- `$("Handoff to Sommatic App").item.json.task_title` (string) = `"Approve order ord_123"`
- `$("Handoff to Sommatic App").item.json.port` (string) = `"out"` (编辑器不提示)
- `$("Handoff to Sommatic App").item.json.app_handoff_descriptor.app_slug` (string) = `"sommatic-approval-desk"` (编辑器不提示)
- `$("Handoff to Sommatic App").item.json.app_handoff_descriptor.route_path` (string) = `"/"` (编辑器不提示)
- `$("Handoff to Sommatic App").item.json.app_handoff_descriptor.input_payload` (object) = `{…}` (编辑器不提示)
- `$("Handoff to Sommatic App").item.json.app_handoff_descriptor.launch_mode` (string) = `"command-center"` (编辑器不提示)


## 工作原理

终止交接：为用户创建携带应用打开描述符（应用 slug、路由、输入、启动模式）的任务，并返回该描述符，以便运行结果可打开应用。

1. 解析配置并合并到输入 payload 之上（配置优先）；`input_mapping` 中的 `{{ }}` 在服务器端解析。
2. 构建描述符：`app_slug` 取 `target.app_slug`，`route_path` 取 `target.route`（默认 `/`），`launch_mode` 取 `target.launch_mode`（默认 `command-center`），`input_payload` 取 `input_mapping`（默认 `{}`）。
3. 检查 `app_slug`、`organization_id`、`assignee` 和 `task_title`。
4. 提交一个任务：用户负责人、状态 `queued`、`source` 为 `workflow`，并带 `metadata.app_handoff_descriptor`；后端会为该用户认领。`task_type` 默认 `review`，`task_priority` 默认 `medium`。
5. 通过 `out` 在顶层返回 `{ app_handoff_descriptor, task_id, task_title }`，运行结果可从中获取描述符；之后打开该任务会重新启动预填数据的应用。

## 在工作流中

### 节点

```json
{
  "node_id": "node-handoff-approval",
  "label": "Hand off to approval desk",
  "operator_slug": "sommatic.app.handoff",
  "operator_version": "1.0.0",
  "config": {
    "target": {
      "app_slug": "sommatic-approval-desk",
      "route": "/",
      "launch_mode": "command-center"
    },
    "input_mapping": {
      "order_id": "{{ $json.order_id }}",
      "amount": "{{ $json.amount }}"
    },
    "organization_id": "<organization_id>",
    "assignee": "approver@example.com",
    "task_title": "Approve order {{ $json.order_id }}"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "port": "out",
  "app_handoff_descriptor": {
    "app_slug": "sommatic-approval-desk",
    "route_path": "/",
    "input_payload": {
      "order_id": "ord_123",
      "amount": 1250
    },
    "launch_mode": "command-center"
  },
  "task_id": "task-xxxxxxxxxxxxxxx",
  "task_title": "Approve order ord_123"
}
```

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

### `out`

任务已创建。运行应在此结束；不会挂起。

```json
{
  "port": "out",
  "app_handoff_descriptor": {
    "app_slug": "sommatic-approval-desk",
    "route_path": "/",
    "input_payload": {
      "order_id": "ord_123",
      "amount": 1250
    },
    "launch_mode": "command-center"
  },
  "task_id": "task-xxxxxxxxxxxxxxx",
  "task_title": "Approve order ord_123"
}
```

### `error`

缺少必填字段或后端调用失败。

```json
{
  "port": "error",
  "error": "Missing 'assignee'"
}
```

## 代码中的行为

- `task_type`: 默认对象为 `{ id: 1, name: "review", title: "Review" }`，而后端目录中 `review` 的 id 是 12。若收件箱按 id 过滤，请显式设置 `task_type`。
- `target`: 也会读取未声明的顶层备选键：`app_slug`、`route`、`launch_mode`、`input_payload`、`title`、`type` 和 `priority`。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `Missing 'target.app_slug'` | `error` | 没有 `target.app_slug`（顶层也没有 `app_slug`）。 |
| `Missing 'organization_id'` | `error` | 配置和 payload 中都没有。 |
| `Missing 'assignee'` | `error` | 交接始终需要用户身份（邮箱）。 |
| `Missing 'task_title'` | `error` | `task_title`（或 `title`）为空。 |
| `Backend did not return a task id` | `error` | 后端响应中没有 `result.id`。 |
| `Request failed with status code <status>` | `error` | 后端拒绝了该任务。 |
| `services.apiManager.uri is not resolved in dependencies.config — check Link Loom Cloud environment config.` | `error` | 编排器未配置后端地址。这是服务配置问题，与节点无关。 |

## 说明与限制

- 不会挂起或等待：连接到 `out` 的节点会立即执行。请将其作为运行的最后一个节点。
- 任务的 `context.workflow_instance_id` 和 `context.trace_id` 以空值发送：worker 从运行时未设置的字段读取它们，因此任务不会关联到本次运行。
- 非幂等：每次执行都会创建新任务。HTTP 调用不会重试。
- worker 将字段直接放在信封顶层、不使用 `data`，因此引擎把整个信封（包括 `port` 键）作为下一个节点的 payload。
- worker 不设置 `ok`（示例中显示为 `true`）；引擎只按 `port` 路由。


## 契约（JSON Schema）

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

```json
{
  "slug": "sommatic.app.handoff",
  "name": "Handoff to Sommatic App",
  "category": "hitl",
  "version": "1.0.0",
  "description": "Terminal node: hands a finished workflow's result to a Sommatic AI App. Creates a durable HITL Task carrying the open descriptor and, if a live Command Center session is subscribed, opens the app instantly. Symmetric counterpart of sommatic.app.execute (app-as-gate); this is app-as-terminal — the workflow ends and does not resume.",
  "capabilities": [
    "ui",
    "human",
    "terminal"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "target",
      "organization_id",
      "assignee",
      "task_title"
    ],
    "properties": {
      "target": {
        "type": "object",
        "title": "Target App",
        "required": [
          "app_slug"
        ],
        "properties": {
          "app_slug": {
            "type": "string",
            "title": "App Slug",
            "description": "Slug of the Sommatic AI App to hand off to (e.g. my-review-app)."
          },
          "route": {
            "type": "string",
            "title": "Route Path",
            "description": "Route opened inside the app (e.g. /new-sentence). Defaults to /.",
            "default": "/"
          },
          "launch_mode": {
            "type": "string",
            "title": "Launch Mode",
            "description": "How the app is opened when a live session exists.",
            "default": "command-center"
          }
        }
      },
      "input_mapping": {
        "type": "object",
        "title": "Input Payload",
        "description": "Payload pre-filled into the app, resolved server-side from {{...}} expressions."
      },
      "organization_id": {
        "type": "string",
        "title": "Organization",
        "description": "Organization that owns the created Task."
      },
      "assignee": {
        "type": "string",
        "title": "Assignee",
        "ui_widget": "user_picker",
        "description": "Identity (email) the Task is assigned to."
      },
      "task_title": {
        "type": "string",
        "title": "Task Title",
        "description": "Title of the durable HITL Task created for the handoff."
      },
      "task_type": {
        "type": "object",
        "title": "Task Type",
        "description": "Task type descriptor. Defaults to review."
      },
      "task_priority": {
        "type": "object",
        "title": "Task Priority",
        "description": "Task priority descriptor. Defaults to medium."
      },
      "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": {
    "type": "object",
    "properties": {
      "target": {
        "type": "object"
      },
      "input_mapping": {
        "type": "object"
      },
      "organization_id": {
        "type": "string"
      },
      "assignee": {
        "type": "string"
      },
      "task_title": {
        "type": "string"
      }
    }
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "app_handoff_descriptor": {
        "type": "object"
      },
      "task_id": {
        "type": "string"
      },
      "task_title": {
        "type": "string"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Out",
      "is_default": true,
      "description": "Handoff delivered (Task created; app opened if live session)."
    },
    {
      "port_id": "error",
      "label": "Error"
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "ui",
    "terminal",
    "handoff"
  ],
  "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": [
    "target",
    "organization_id",
    "assignee",
    "task_title"
  ],
  "properties": {
    "target": {
      "type": "object",
      "title": "Target App",
      "required": [
        "app_slug"
      ],
      "properties": {
        "app_slug": {
          "type": "string",
          "title": "App Slug",
          "description": "Slug of the Sommatic AI App to hand off to (e.g. my-review-app)."
        },
        "route": {
          "type": "string",
          "title": "Route Path",
          "description": "Route opened inside the app (e.g. /new-sentence). Defaults to /.",
          "default": "/"
        },
        "launch_mode": {
          "type": "string",
          "title": "Launch Mode",
          "description": "How the app is opened when a live session exists.",
          "default": "command-center"
        }
      }
    },
    "input_mapping": {
      "type": "object",
      "title": "Input Payload",
      "description": "Payload pre-filled into the app, resolved server-side from {{...}} expressions."
    },
    "organization_id": {
      "type": "string",
      "title": "Organization",
      "description": "Organization that owns the created Task."
    },
    "assignee": {
      "type": "string",
      "title": "Assignee",
      "ui_widget": "user_picker",
      "description": "Identity (email) the Task is assigned to."
    },
    "task_title": {
      "type": "string",
      "title": "Task Title",
      "description": "Title of the durable HITL Task created for the handoff."
    },
    "task_type": {
      "type": "object",
      "title": "Task Type",
      "description": "Task type descriptor. Defaults to review."
    },
    "task_priority": {
      "type": "object",
      "title": "Task Priority",
      "description": "Task priority descriptor. Defaults to medium."
    },
    "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

```json
{
  "type": "object",
  "properties": {
    "target": {
      "type": "object"
    },
    "input_mapping": {
      "type": "object"
    },
    "organization_id": {
      "type": "string"
    },
    "assignee": {
      "type": "string"
    },
    "task_title": {
      "type": "string"
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "app_handoff_descriptor": {
      "type": "object"
    },
    "task_id": {
      "type": "string"
    },
    "task_title": {
      "type": "string"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Out",
    "is_default": true,
    "description": "Handoff delivered (Task created; app opened if live session)."
  },
  {
    "port_id": "error",
    "label": "Error"
  }
]
```
