# Generators

> 新生成的随机标识符。

Source: https://docs.sommatic.ai/zh/expressions/reference/generators/

每个示例都在已部署的表达式引擎上、基于页面末尾的[示例上下文](#example-context)运行过。

## `$uuid`

变量 · `string` · 来源: `uuid · v4`

```javascript
$uuid
```

随机的第 4 版 UUID，36 个字符，采用 `8-4-4-4-12` 的十六进制格式。它是属性而非函数：写 `$uuid`，不要写 `$uuid()`。每次读取都产生新值，即使在同一表达式中读取两次也是如此。

### 示例

```javascript
{{ $uuid }}
```

结果 (string) · 随机：每次运行都会生成新值:

```json
"d5d1221a-b9bc-4f05-ac90-de73b5841ca9"
```

```javascript
{{ 'msg-' + $uuid }}
```

结果 (string) · 随机：每次运行都会生成新值:

```json
"msg-c2e2e36c-61ac-46c7-8d1c-952afe5e313e"
```

### 错误

- `$uuid is not a function`: 以 `$uuid()` 的形式调用了它。请去掉括号。

### 说明

- 多个节点需要同一个 id 时，只生成一次（例如在 `data.transform` 中），再用 `$('…')` 从该节点读取。

在 JSONata 中: `$$uuid`

## `$randomId`

函数 · `string` · 来源: `Link Loom SDK · utilities.generator.id`

```javascript
$randomId({ length, prefix })
```

返回 `prefix` 加上 `length` 个随机十六进制字符（`0-9`、`a-f`）。适用于便于人工识读的引用编号；标准标识符请使用 `$uuid`。

### 参数

- `options` (object, 必填): 务必传入一个对象，即使是空对象：不传则 `$randomId()` 会失败。
- `options.length` (number, 可选, 默认 `15`): 随机字符的个数（最多 512）。
- `options.prefix` (string, 可选, 默认 `'id-'`): 放在随机字符前面的文本。

### 返回

`string`: `prefix` + `length` 个十六进制字符。

### 示例

```javascript
{{ $randomId({ length: 8, prefix: 'ord-' }) }}
```

结果 (string) · 随机：每次运行都会生成新值:

```json
"ord-70fb73b5"
```

```javascript
{{ $randomId({}) }}
```

结果 (string) · 随机：每次运行都会生成新值:

```json
"id-fe83456fd23b165"
```

不传选项对象会失败。

```javascript
{{ $randomId() }}
```

抛出:

```text
Cannot destructure property 'length' of 'undefined' as it is undefined.
```

### 错误

- `Cannot destructure property 'length' of 'undefined' as it is undefined.`: 调用时没有传选项对象。至少传入 `{}`。

在 JSONata 中: `$$randomId({ "length": 8, "prefix": "ord-" })`

## 示例上下文

示例基于以下数据运行：当前节点的输入（`$json`）、已运行的节点（`$(…)`）、本次运行的 ID 以及当前节点。时钟固定为 `2026-11-26T14:30:00.000Z`，服务器时区为 UTC，因此各页面中 `$now` 与 `$today` 的值都相同。

```json
{
  "$json": {
    "body": {
      "order_id": "ord_1042",
      "email": "ana.perez@example.com",
      "customer": {
        "name": "Ana Pérez",
        "segment": "gold"
      },
      "priority": "",
      "items": [
        {
          "sku": "A-100",
          "qty": 2,
          "price": 10.5
        },
        {
          "sku": "B-200",
          "qty": 0,
          "price": 4
        },
        {
          "sku": "C-300",
          "qty": 1,
          "price": 25
        }
      ],
      "tags": [
        "new",
        "web",
        "new"
      ],
      "created_at": "2026-11-25T09:15:00Z"
    },
    "headers": {
      "content-type": "application/json"
    },
    "query": {},
    "method": "POST"
  },
  "nodes": {
    "Webhook Receiver": "= $json",
    "Normalize input": {
      "correlation_id": "c-7f3a",
      "channel": "web"
    },
    "Classify": {
      "label": "billing",
      "confidence": 0.92
    }
  },
  "run": {
    "$flow_instance_result_id": "fir-xxxx",
    "$flow_instance_id": "fi-xxxx",
    "$flow_definition_id": "fd-xxxx",
    "$flow_version_id": "fv-xxxx",
    "$flow_slug": "order-intake"
  },
  "node": {
    "node_id": "node-notify",
    "label": "Notify customer"
  }
}
```

串联工作流的示例基于第二个上下文运行：一个由 `workflow.chain` 启动的工作流，其 `workflow.trigger` 节点的标签为 `Chain Entry Point`。

```json
{
  "$json": {
    "order_id": "ord_1042",
    "customer_email": "ana.perez@example.com",
    "amount": 46,
    "chain_context": {
      "caller_flow_definition_id": "fd-caller-xxxx",
      "caller_flow_instance_id": "fi-caller-xxxx",
      "caller_flow_slug": "order-intake",
      "caller_node_id": "node-chain-billing",
      "correlation_id": "chain-7Hq2Lw9sKd3pXa1mZr4t",
      "root_flow_instance_result_id": "fir-root-xxxx"
    }
  },
  "run": {
    "$flow_instance_result_id": "fir-hop-xxxx",
    "$flow_instance_id": "fi-hop-xxxx",
    "$flow_definition_id": "fd-billing-xxxx",
    "$flow_version_id": "fv-billing-xxxx",
    "$flow_slug": "billing-review",
    "$input": {
      "data": {
        "order_id": "ord_1042",
        "customer_email": "ana.perez@example.com",
        "amount": 46
      },
      "chain_context": {
        "caller_flow_definition_id": "fd-caller-xxxx",
        "caller_flow_instance_id": "fi-caller-xxxx",
        "caller_flow_slug": "order-intake",
        "caller_node_id": "node-chain-billing",
        "correlation_id": "chain-7Hq2Lw9sKd3pXa1mZr4t",
        "root_flow_instance_result_id": "fir-root-xxxx"
      }
    }
  },
  "node": {
    "node_id": "node-create-task",
    "label": "Create review task"
  }
}
```

示例运行所基于的编排器提交 31e874b.
