本页内容
Sommatic 文档
打开控制台

Write State

将一个值写入持久化状态存储。

以 Markdown 查看
在以下应用中打开
运维 实验性 ops.state.write 版本 1.0.0 执行方式: async

设计用途是把值保存在 key 下:存入当前运行(instance)或工作流键值状态(global、case)。在当前运行时中,它总是以 logger.debug is not a function 从 error 输出,因此不会写入任何内容。

该算子让工作流可以在不同执行步骤之间记住信息,甚至在不同工作流之间共享数据(使用 Global 作用域)。

作用域:

  • Instance: 仅对本次工作流运行可见。
  • Case: 对同一 Case/Thread 中的所有工作流可见。
  • Global: 全系统可见(请谨慎使用)。

适用场景:

  • 保存用户的进度。
  • 缓存 API 令牌。
  • 通过“Case”作用域将数据从父工作流传给子工作流。

工作原理

  1. 将输入项与解析后的配置合并(配置优先)。
  2. 使用 scope(默认 instance);key 为空时以 Missing 'key' 从 error 输出。
  3. 调用 logger.debug() 时抛错;worker 捕获后从 error 输出。下面的步骤目前不会执行。
  4. global / case:从 success 返回 { value },并通过 meta.kv_state 请求后端保存到 <key> 或 case:<key>。
  5. instance:返回 { _state_update, value, port },但引擎不会应用:运行变量不会改变。

在工作流中

节点在工作流文件(draft_nodes[])中的写法,以及一次成功运行的 envelope。

{
  "node_id": "node-save-last-sync",
  "label": "Save last sync time",
  "operator_slug": "ops.state.write",
  "operator_version": "1.0.0",
  "config": {
    "scope": "global",
    "key": "last_sync_at",
    "value": "{{ $now }}"
  }
}

配置

scope string 必填

所存变量的可见范围。

默认值: instance

可选值: instancecaseglobal

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

代码中的行为

除 schema 描述之外,实现代码对这些键的实际处理。

key
不在配置 schema 中(仅出现在输入契约中),但会从节点配置读取;节点上未设置时从输入项读取:key、value、ttl_ms。
ttl_ms
只会写入日志,从不应用。

输出端口

success 默认

在当前运行时中不会到达:logger.debug() 调用会先失败。此处展示代码构建的数据结构。

数据
{
  "value": "2026-11-26T15:04:05.000Z"
}
error

目前每次运行都会如此。输出为整个返回对象 { port, error }。

数据
{
  "port": "error",
  "error": "logger.debug is not a function"
}

在表达式中

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

表达式EditorWorker
$("Write State").item.json.valueany= "2026-11-26T15:04:05.000Z"已存储值的确认。

错误

消息 端口 原因与处理
logger.debug is not a function error Worker 调用了运行时 logger 不提供的 logger.debug()。每次运行都会发生,任何配置都无法避免。
Missing 'key' error 节点和输入项中都没有设置 key。

输入契约

key string 必填

变量名/键

value any 可选

要存储的数据(任意可 JSON 序列化的值)

ttl_ms number 可选

存活时间(毫秒)

输出契约

value any 可选

已存储值的确认。

契约(JSON Schema)

与编排器声明完全一致的定义。All 是完整的 *.definition.js;可与上方各节对照,找出文档与 schema 之间的差异。发布前可用它校验节点。

{
  "slug": "ops.state.write",
  "name": "Write State",
  "category": "ops",
  "version": "1.0.0",
  "description": "Persists a value to the persistent state store.\n\n  This operator allows workflows to remember information across different execution steps, or even share data between different workflows (using Global scope).\n\n  **Scopes:**\n  - **Instance:** Visible only to this workflow run.\n  - **Case:** Visible to all workflows in the same Case/Thread.\n  - **Global:** Visible system-wide (use with caution).\n\n  **Use Cases:**\n  - Saving a user's progress.\n  - Caching an API token.\n  - Passing data from a parent workflow to a child workflow via 'Case' scope.",
  "capabilities": [
    "state",
    "persistence",
    "store"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "scope"
    ],
    "properties": {
      "scope": {
        "type": "string",
        "enum": [
          "instance",
          "case",
          "global"
        ],
        "default": "instance",
        "title": "State Scope",
        "description": "Visibility level of the stored variable."
      },
      "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": {
      "key": {
        "type": "string",
        "description": "Variable Name/Key"
      },
      "value": {
        "type": "any",
        "description": "Data to store (Any JSON serializable)"
      },
      "ttl_ms": {
        "type": "number",
        "description": "Time-to-Live in milliseconds"
      }
    },
    "required": [
      "key"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "value": {
        "type": "any",
        "description": "The stored value confirmation."
      }
    }
  },
  "ports": [
    {
      "port_id": "success",
      "label": "Success",
      "is_default": true,
      "description": "State saved successfully."
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Database/Storage failure."
    }
  ],
  "execution_mode": "async",
  "tags": [
    "state",
    "database",
    "persistence"
  ],
  "is_stateful": true,
  "publisher": {
    "identity": "org_link_loom",
    "profile": {
      "name": "Link Loom",
      "verified": true,
      "url": "https://me.veripass.com/link-loom",
      "logo_url": null
    }
  }
}

执行

执行方式
async
有状态
是
重试(retryOnFail)
否
能力
statepersistencestore
标签
statedatabasepersistence

说明与限制

  • Worker 调用了 logger.debug(),而运行时 logger 没有该方法(只提供 info、warning、error、log 和 success)。每次运行该调用都会抛错。
  • 键值状态保存在工作流定义的元数据(kv_state)中,由该工作流的所有运行共享。只有当运行使用草稿图时,后端才会在启动时把它注入为 $input.$kv_state;已发布版本的运行不会收到。写入(每个步骤的 meta.kv_state)在运行结束后才合并进元数据,因此同一次运行中的后续节点看不到。没有过期机制。
  • 若要在同一次运行的后续节点中保留某个值,请直接使用节点输出:下游节点可通过 {{ $json.<字段> }} 或节点名称读取。
  • 从 error 端口输出不会使运行失败:引擎沿 error 上的连线继续;若没有连线,该分支直接结束。
  • execution 设置(retryOnFail、maxTries、waitBetweenTries、continueOnFail、timeout_ms)可以填写,但本 worker 和引擎都不会应用。

已对照编排器代码核实 · 31e874b · 2026-11-26

安装智能体工具

为你的编程智能体接入 Sommatic AI 官方文档:技能包、文档 MCP 服务器与 Markdown 页面,无需再抓取网页。

仅安装技能包,不会自动更新;需要时重新运行即可。

npx skills add https://docs.sommatic.ai
智能体如何读取本文档