# Safety Filter

> 依据安全准则评估文本，拦截有害内容。

Source: https://docs.sommatic.ai/zh/nodes/ai.safety.filter/

- 标识: `ai.safety.filter`
- 类别: AI
- 版本: 1.0.0
- 状态: 实验性. 桩实现：返回固定的 `{ message, timestamp }` 对象，不评估输入。不会拦截任何内容。
- 执行方式: async

依据安全准则评估文本，拦截有害内容。

使用审核模型（例如 OpenAI Moderation API、Llama Guard）检测仇恨、骚扰、自残、色情或暴力等类别。

**适用场景：**
- 在发送给 LLM 之前审核用户输入。
- 在展示给用户之前校验 LLM 输出。
- 保障品牌安全。

## 配置

- `policies` (array<string>, 可选): 要拦截的类别列表（例如 ["hate", "violence"]）。留空 = 全部。

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

## 输出端口

- `safe` (默认): 内容通过检查。
- `blocked`: 内容违反安全策略。
- `error`: 审核服务故障。

## 输入契约

- `text` (string, 必填): 需要审核的内容。

## 输出契约

- `is_safe` (boolean, 可选)
- `flagged_categories` (array<string>, 可选)
- `scores` (object, 可选)

## 在表达式中

后续节点可通过 `$("Safety Filter").item.json.<路径>` 读取此节点输出中的内容，即工作流编辑器在 Previous Nodes 中提示的路径（输出契约）。

- `$("Safety Filter").item.json.is_safe` (boolean)
- `$("Safety Filter").item.json.flagged_categories` (array)
- `$("Safety Filter").item.json.scores` (object)


## 工作原理

实验性桩实现。它不评估输入，直接返回固定的 `{ message, timestamp }` 对象且不带端口，因此引擎将其路由到此节点未声明的 `out`：连接到 `safe` 或 `blocked` 的节点都不会执行。

1. 解析节点配置：配置键中的表达式会基于本次运行求值；传入条目按原样使用。
2. 返回不带端口的 `{ message: "Executed ai.safety.filter", timestamp }`。引擎路由到 `out`；由于没有 `out` 连线，该分支到此结束。

## 在工作流中

### 节点

```json
{
  "node_id": "node-safety-filter",
  "label": "Moderate content",
  "operator_slug": "ai.safety.filter",
  "operator_version": "1.0.0",
  "config": {
    "policies": [
      "hate",
      "violence"
    ]
  }
}
```

### Envelope

```json
{
  "ok": true,
  "port": "out",
  "data": {
    "message": "Executed ai.safety.filter",
    "timestamp": "2026-11-26T15:04:05.000Z"
  }
}
```

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

### `safe`

永远不会触发。

### `blocked`

永远不会触发：不会拦截任何内容。

### `error`

代码从不输出此端口。

## 代码中的行为

- `policies`: 已声明但被忽略。

## 说明与限制

- 尚未实现：内容审核、分类得分和拦截。如需可用的安全检查，请使用路由到 `pass`/`fail` 的 `ai.safety.scan`。


## 契约（JSON Schema）

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

```json
{
  "slug": "ai.safety.filter",
  "name": "Safety Filter",
  "category": "ai",
  "version": "1.0.0",
  "description": "Evaluates text against safety guidelines to block harmful content.\n\n  Uses a moderation model (e.g., OpenAI Moderation API, Llama Guard) to detect categories like Hate, Harassment, Self-Harm, Sexual, or Violence.\n\n  **Use Cases:**\n  - Moderating user input before sending to an LLM.\n  - Validating LLM output before showing to user.\n  - Ensuring brand safety.",
  "capabilities": [
    "moderation",
    "safety",
    "guardrails"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "policies": {
        "type": "array",
        "items": {
          "type": "string"
        },
        "title": "Blocked Categories",
        "description": "List of categories to block (e.g. [\"hate\", \"violence\"]). Empty = All."
      },
      "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": {
      "text": {
        "type": "string",
        "description": "Creating content to moderate."
      }
    },
    "required": [
      "text"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "is_safe": {
        "type": "boolean"
      },
      "flagged_categories": {
        "type": "array",
        "items": {
          "type": "string"
        }
      },
      "scores": {
        "type": "object"
      }
    }
  },
  "ports": [
    {
      "port_id": "safe",
      "label": "Safe",
      "is_default": true,
      "description": "Content passed checks."
    },
    {
      "port_id": "blocked",
      "label": "Blocked",
      "description": "Content violated safety policy."
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Moderation service failure."
    }
  ],
  "execution_mode": "async",
  "tags": [
    "safety",
    "moderation",
    "guardrail"
  ],
  "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": {
    "policies": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "title": "Blocked Categories",
      "description": "List of categories to block (e.g. [\"hate\", \"violence\"]). Empty = All."
    },
    "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": {
    "text": {
      "type": "string",
      "description": "Creating content to moderate."
    }
  },
  "required": [
    "text"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "is_safe": {
      "type": "boolean"
    },
    "flagged_categories": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "scores": {
      "type": "object"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "safe",
    "label": "Safe",
    "is_default": true,
    "description": "Content passed checks."
  },
  {
    "port_id": "blocked",
    "label": "Blocked",
    "description": "Content violated safety policy."
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Moderation service failure."
  }
]
```
