# Generate UUID

> 生成密码学强度的唯一标识符（UUID/NanoID）。

Source: https://docs.sommatic.ai/zh/nodes/data.uuid/

- 标识: `data.uuid`
- 类别: 数据
- 版本: 1.0.0
- 状态: 可用
- 执行方式: sync

生成密码学强度的唯一标识符（UUID/NanoID）。

该算子生成的唯一字符串可用作数据库键、交易 ID 或幂等令牌。

**格式：**
- **v4：** 随机 UUID（标准）。
- **v5：** 基于名称的 UUID（由 namespace + name 确定性生成）。
- **NanoID：** 紧凑、URL 友好的唯一字符串。

**适用场景：**
- 为新创建的资源分配 ID。
- 生成用于跟踪请求链的关联 ID。

## 配置

- `version` (string, 可选): 要生成的 ID 类型。 默认值: `v4`. 可选值: `v4`, `v5`, `nanoid`.
- `namespace_id` (string, 可选): 用于确定性 v5 生成的命名空间 UUID。
- `name` (string, 可选): 用于确定性 v5 生成的输入名称。

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

## 输出端口

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

## 输入契约

- `name` (string, 可选): 用于 v5 生成的输入名称（可选）。

## 输出契约

- `uuid` (string, 可选): 生成的唯一标识符。

## 在表达式中

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

- `$("Generate UUID").item.json.uuid` (string) = `"3b1f8e2a-6c4d-4f7e-9a2b-8d5c1e0f7a34"`: 生成的唯一标识符。
- `$("Generate UUID").item.json.result` (string) = `"3b1f8e2a-6c4d-4f7e-9a2b-8d5c1e0f7a34"` (编辑器不提示)


## 工作原理

生成随机 v4 UUID、确定性 v5 UUID 或 20 位随机十六进制 id，以 `{ uuid, result }` 返回。

1. 解析配置；`version` 默认为 `v4`。
2. `v4`：随机 UUID。
3. `v5`：需要 `namespace_id`（UUID）和 `name`；相同组合总是得到相同的 UUID。
4. `nanoid`：10 个随机字节的十六进制形式（20 个字符）；并非真正的 nanoid。
5. 通过 `out` 返回 `{ uuid, result }`（同一个值两次）；输入 payload 不会保留。

## 在工作流中

### 节点

```json
{
  "node_id": "node-order-key",
  "label": "Order key",
  "operator_slug": "data.uuid",
  "operator_version": "1.0.0",
  "config": {
    "version": "v5",
    "namespace_id": "6ba7b811-9dad-11d1-80b4-00c04fd430c8",
    "name": "{{ $json.order_id }}"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "uuid": "3b1f8e2a-6c4d-5f7e-9a2b-8d5c1e0f7a34",
    "result": "3b1f8e2a-6c4d-5f7e-9a2b-8d5c1e0f7a34"
  },
  "port": "out"
}
```

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

### `out`

id 已生成。

```json
{
  "uuid": "3b1f8e2a-6c4d-4f7e-9a2b-8d5c1e0f7a34",
  "result": "3b1f8e2a-6c4d-4f7e-9a2b-8d5c1e0f7a34"
}
```

### `error`

v5 所需输入缺失或无效，或 `version` 未知。

```json
{
  "ok": false,
  "errors": [
    "UUID Error: v5 requires namespace_id and name"
  ],
  "port": "error"
}
```

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `UUID Error: v5 requires namespace_id and name` | `error` | `version` 为 `v5`，但两个键之一为空。 |
| `UUID Error: Invalid UUID` | `error` | `namespace_id` 不是有效的 UUID。 |
| `UUID Error: Unknown UUID version: <version>` | `error` | `version` 不是 `v4`、`v5` 或 `nanoid`。 |

## 说明与限制

- payload 会被 `{ uuid, result }` 替换：可用 `$("Node")` 引用之前的数据，或在其他节点中直接用 `{{ $uuid }}` 生成 id。
- 当返回的信封没有 `data`（或 `data` 为假值）时，引擎会把整个信封作为节点输出：经 `error` 端口时，下一个节点收到的 payload 是 `{ ok: false, errors: [...], port: "error" }`。


## 契约（JSON Schema）

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

```json
{
  "slug": "data.uuid",
  "name": "Generate UUID",
  "category": "data",
  "version": "1.0.0",
  "description": "Generates a cryptographically strong unique identifier (UUID/NanoID).\n\n  This operator produces unique strings suitable for database keys, transaction IDs, or idempotency tokens.\n\n  **Formats:**\n  - **v4:** Random UUID (Standard).\n  - **v5:** Name-based UUID (Deterministic based on namespace + name).\n  - **NanoID:** Compact, URL-friendly unique string.\n\n  **Use Cases:**\n  - Assigning an ID to a newly created resource.\n  - Generating a correlation ID for tracking request chains.",
  "capabilities": [
    "utils",
    "id-generation",
    "security"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "version": {
        "type": "string",
        "enum": [
          "v4",
          "v5",
          "nanoid"
        ],
        "default": "v4",
        "title": "Format",
        "description": "Type of ID to generate."
      },
      "namespace_id": {
        "type": "string",
        "title": "Namespace UUID (v5 only)",
        "description": "Namespace UUID for deterministic v5 generation."
      },
      "name": {
        "type": "string",
        "title": "Name (v5 only)",
        "description": "Input name used for deterministic v5 generation."
      },
      "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": {
      "name": {
        "type": "string",
        "description": "Input name for v5 generation (optional)."
      }
    }
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "uuid": {
        "type": "string",
        "description": "The generated unique identifier."
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Generated",
      "is_default": true
    },
    {
      "port_id": "error",
      "label": "Error"
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "id",
    "uuid",
    "key",
    "generator"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_link_loom",
    "profile": {
      "name": "Link Loom",
      "verified": true,
      "url": "https://me.veripass.com/link-loom",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "version": {
      "type": "string",
      "enum": [
        "v4",
        "v5",
        "nanoid"
      ],
      "default": "v4",
      "title": "Format",
      "description": "Type of ID to generate."
    },
    "namespace_id": {
      "type": "string",
      "title": "Namespace UUID (v5 only)",
      "description": "Namespace UUID for deterministic v5 generation."
    },
    "name": {
      "type": "string",
      "title": "Name (v5 only)",
      "description": "Input name used for deterministic v5 generation."
    },
    "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": {
    "name": {
      "type": "string",
      "description": "Input name for v5 generation (optional)."
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "uuid": {
      "type": "string",
      "description": "The generated unique identifier."
    }
  }
}
```

### ports

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