Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions external_plugins/cloudflare-clef/.zcode-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "cloudflare-clef",
"version": "0.3.0",
"description": "Decision evaluation on Cloudflare Workers AI CLEF / CLEF Flash, exposed as the MCP tool clef_evaluate. Credentials are DPAPI-encrypted at rest. Images attach by absolute local file path or embedded data URL. Protocol layer ported verbatim from the MIT-licensed OpenClaw plugin openclaw-cloudflare-clef 0.1.3.",
"description_i18n": {
"en": "Decision evaluation on Cloudflare Workers AI CLEF / CLEF Flash, exposed as the MCP tool clef_evaluate. Credentials are DPAPI-encrypted at rest. Images attach by absolute local file path or embedded data URL. Protocol layer ported verbatim from the MIT-licensed OpenClaw plugin openclaw-cloudflare-clef 0.1.3.",
"zh-CN": "基于 Cloudflare Workers AI CLEF / CLEF Flash 的结构化决策评测 MCP 工具 clef_evaluate,凭据经 DPAPI 加密落盘。图片支持绝对路径本地文件或内嵌 data URL 两种挂载方式。协议层逐字节移植自 MIT 许可的 OpenClaw 插件 openclaw-cloudflare-clef 0.1.3。"
},
"author": { "name": "Ported from OpenClaw openclaw-cloudflare-clef 0.1.3" },
"mcpServers": {
"cloudflare-clef": {
"command": "node",
"args": ["${CLAUDE_PLUGIN_ROOT}/server/launch.mjs"]
}
}
}
78 changes: 78 additions & 0 deletions external_plugins/cloudflare-clef/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Cloudflare CLEF Decisions (ZCode plugin)

[中文文档](./README_CN.md)

Ported from the OpenClaw decision-provider plugin `openclaw-cloudflare-clef` (0.1.3): structured decision evaluation (boolean / choice / score) on Cloudflare Workers AI CLEF-family models, returning per-question answers with calibrated probabilities instead of free-text guesses.

- The protocol layer (`server/protocol.mjs`) is **byte-identical** to the original; all 15 protocol unit tests pass.
- The OpenClaw host integration is rewritten as a ZCode **MCP stdio server** (zero npm dependencies) exposing the tool `clef_evaluate`.
- Credentials are **DPAPI-encrypted at rest** — no plaintext on disk — filling the gap left by ZCode not shipping a secret store like OpenClaw's.

## Prerequisites (Cloudflare side)

1. **Account ID**: Cloudflare dashboard → Workers & Pages overview, right side; 32 hex characters.
2. **API Token**: dashboard avatar → My Profile → API Tokens → Create Token, permission **Workers AI → Run**, scoped to your account.

## Credentials (DPAPI-encrypted store, recommended)

Run once in PowerShell from the plugin directory (interactive, token input hidden):

```powershell
powershell -ExecutionPolicy Bypass -File server\store-secret.ps1
```

Both values are stored DPAPI-encrypted at `%USERPROFILE%\.zcode\secrets\cloudflare-clef.json`, **bound to the current Windows user** — the file is useless if copied to another machine or account, and no plaintext ever touches the disk. Re-run to rotate; delete the file to revoke.

Then **fully quit and restart ZCode**. Startup chain: `launch.mjs` decrypts the two values via PowerShell and injects them into the process environment (MCP traffic never passes through PowerShell), then loads the server.

Alternative: set user-level environment variables `CLOUDFLARE_ACCOUNT_ID` / `CLOUDFLARE_API_TOKEN` (plaintext in the registry; not recommended). Real environment variables take precedence over the encrypted store.

Node.js ≥ 22 must be on PATH.

**Honest security boundary**: DPAPI protects against file exfiltration (backups, cloud sync, copied disks), not against other processes running as the same Windows user (Windows has no stronger non-interactive local option). Credentials and submitted evidence are never logged; logs additionally redact token/secret/api-key-style keys by pattern.

## Cost

- Workers Free is recommended: 10,000 Neurons per UTC day, hard platform limit when exhausted; the free allowance is shared with other Workers AI usage on the account.
- Workers Paid also includes the free allowance but bills beyond it; this plugin does not change your plan and does not guarantee zero spend on Paid accounts.
- Official pricing as of 2026-10-20: CLEF Flash 3,455 Neurons / M input tokens; CLEF 21,818 / M. Input-only billing.

## Usage

After installation, ask the agent to call `clef_evaluate`, for example:

```json
{
"model": "clef-flash",
"state": "Cargo not yet telex-released, ETA tomorrow, needs origin-port follow-up.",
"questions": {
"urgent": { "type": "boolean", "instructions": "Is this urgent?" },
"route": { "type": "choice", "criteria": { "customs": "Customs clearance", "ocean": "Ocean freight" } },
"severity": { "type": "score", "criteria": ["None", "Moderate", "Critical"] }
}
}
```

Returns `{model, answers, usage}`; answers are keyed by the original question ids, choice/score include full probability distributions, boolean returns `probabilityTrue`. Omitting `model` uses `clef-flash`.

Images in state attach three ways: embedded data URLs, `{content_type,base64}` objects, or **absolute local file paths** (`.png/.jpg/.jpeg/.webp`; the string must be exactly the file path). Path-form images are read from disk by the server and embedded as `[image #N]` — chat models cannot reliably emit large base64 blobs, so prefer paths for real screenshots. A missing path or a file over 4 MiB returns a clear unsupported-input error.

## Translation and boundaries (same as the original)

- boolean → noul → probabilityTrue; choice keeps all probabilities; score index objects become arrays.
- Question and option ids are remapped on the wire (Chinese and slashes allowed) and restored in results.
- Request URL is fixed to `api.cloudflare.com` (SSRF excluded by construction), redirects disabled; 55 s timeout; MCP request cancellation supported.
- No retries, no background polling, no fallback to paid models; 429 surfaces a rate-limit error (with retry-after hint).
- Text/JSON evidence plus up to 4 images (PNG/JPEG/WebP, 4 MiB each, 8 MiB total, 16 MP limit); absolute-path image files referenced in state are read from local disk at evaluation time (png/jpg/jpeg/webp only, must exist, ≤ 4 MiB).
- Conservative input budget: UTF-8 bytes + 1,024 + 128 per alternative ≤ 24,000 (adapter-side approximation, not a vendor tokenizer).
- Response bodies are read with a 1 MiB cap.

## Verification

`node --test server/protocol.test.mjs` covers protocol translation and rejection of malformed responses (15/15 passing). MCP handshake, tools/list, missing-credential guidance, and the DPAPI store→decrypt→call chain (including a real request and 401 mapping) were smoke-tested. **Live end-to-end verification with valid credentials is pending** — after installing and storing credentials, the first real call is that verification.

## Sources

- https://developers.cloudflare.com/workers-ai/models/clef-flash/
- https://developers.cloudflare.com/workers-ai/models/clef/
- https://developers.cloudflare.com/workers-ai/platform/pricing/
76 changes: 76 additions & 0 deletions external_plugins/cloudflare-clef/README_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Cloudflare CLEF 决策评测(ZCode 插件)

移植自 OpenClaw decision-provider 插件 `openclaw-cloudflare-clef`(0.1.3):用 Cloudflare Workers AI 的 CLEF 系列模型做结构化决策评测(boolean / choice / score),返回带校准概率的答案,而不是让聊天模型自由发挥。

- 协议层(`server/protocol.mjs`)与原插件**逐字节一致**,15 个协议单测全部通过。
- OpenClaw 宿主集成层改写为 ZCode 的 **MCP stdio server**(零 npm 依赖),以工具 `clef_evaluate` 暴露。
- 凭据用 **Windows DPAPI 加密落盘**,静态存储无明文——弥补 ZCode 没有 OpenClaw secret store 的空缺。

## 安装前置(Cloudflare 侧)

1. **Account ID**:Cloudflare 仪表盘 → Workers & Pages 概览页右侧,32 位十六进制。
2. **API Token**:仪表盘右上角头像 → My Profile → API Tokens → Create Token,权限选 **Workers AI → Run**(作用域限定到你的账户)。

## 凭据配置(DPAPI 加密存储,推荐)

在 PowerShell 里运行插件目录下的 `server\store-secret.ps1`(交互式输入,Token 不回显):

```powershell
powershell -ExecutionPolicy Bypass -File "<插件安装目录>\server\store-secret.ps1"
```

凭据以 DPAPI 密文存于 `%USERPROFILE%\.zcode\secrets\cloudflare-clef.json`,**绑定当前 Windows 用户**——文件被拷到别的机器或别的账户无法解密,磁盘上任何时刻都没有明文。轮换凭据就重跑一次脚本;删除该文件即吊销。

之后**完全退出并重启 ZCode**。插件启动链路:`launch.mjs` 启动时经 PowerShell 解密两条凭据注入本进程环境变量(MCP 通信完全不经过 PowerShell),再加载 server。

替代方案:也可以设用户级环境变量 `CLOUDFLARE_ACCOUNT_ID` / `CLOUDFLARE_API_TOKEN`(明文落注册表,不推荐);两者同时存在时优先用加密文件。

需要 Node.js ≥ 22 在 PATH 中(本机已确认 v26.12.1 可用)。

**安全边界(如实说明)**:DPAPI 防的是文件泄露、备份、网盘同步;当前 Windows 用户下的进程仍可解密(Windows 本地无更强的免交互方案)。凭据与提交的证据永不写日志,日志另有按 `token/secret/api-key` 键名的自动脱敏。

## 费用

- 建议 Workers Free:10,000 Neurons/UTC 日免费,用尽后平台硬限制;免费量与账户内其他 Workers AI 用量共享。
- Workers Paid 同样有免费量,但超额收费;本插件不改变账户套餐,不保证 Paid 账户零支出。
- 2026-10-20 官方价:CLEF Flash 3,455 Neurons / M input tokens;CLEF 21,818 Neurons / M input tokens。仅按输入计价。

## 用法

安装后在会话中让模型调用 `clef_evaluate`,例如:

```json
{
"model": "clef-flash",
"state": "货物尚未电放,ETA 明天,需要联系起运港。",
"questions": {
"是否紧急/urgent": { "type": "boolean", "instructions": "Is this urgent?" },
"route": { "type": "choice", "criteria": { "关务/customs": "Customs clearance", "海运/ocean": "Ocean freight" } },
"severity": { "type": "score", "criteria": ["None", "Moderate", "Critical"] }
}
}
```

返回 `{model, answers, usage}`;答案以原始问题 id 为键,choice/score 附带完整概率分布,boolean 返回 `probabilityTrue`。`model` 省略时用 `clef-flash`。

state 中的图片有三种挂载方式:内嵌 data URL、`{content_type,base64}` 对象,或**绝对路径本地文件**(`.png/.jpg/.jpeg/.webp`,字符串须精确等于文件路径)。路径方式由 server 读盘嵌入并以 `[image #N]` 占位——聊天模型无法可靠生成大段 base64,实际截图请优先用路径;路径不存在或超过 4 MiB 会返回明确的 unsupported-input 错误。

## 翻译与边界(与原插件一致)

- boolean → noul → probabilityTrue;choice 保留全部概率;score 的索引对象转数组。
- 重新映射问题与选项 ID,允许中文、斜杠等标识,恢复结果原 ID。
- 请求 URL 固定为 `api.cloudflare.com`(构造上排除 SSRF),禁止重定向;55 秒超时;支持 MCP 请求取消。
- 无重试、无后台轮询、无收费模型自动回退;429 返回限流错误(含 retry-after 提示)。
- 支持文本/JSON 证据,最多 4 张图片(PNG/JPEG/WebP,单张 4 MiB、合计 8 MiB、16 MP 上限);state 中以绝对路径引用的本地图片文件会在评测时被读取(仅 png/jpg/jpeg/webp,文件须存在且 ≤ 4 MiB)。
- 保守输入预算:UTF-8 字节 + 1,024 + 每候选 128 ≤ 24,000(接入侧近似保护,非厂商 tokenizer 精确计算)。
- 响应体读取上限 1 MiB。

## 验证

`node --test server/protocol.test.mjs` 验证协议转换与拒绝异常响应(15/15 通过)。已另做 MCP 握手、tools/list、无凭据指引、DPAPI 存取全链路(含真实请求与 401 映射)冒烟测试。**未做真实凭据端到端验证**——安装并存储凭据后,首次调用即是端到端验证。

## 来源

- https://developers.cloudflare.com/workers-ai/models/clef-flash/
- https://developers.cloudflare.com/workers-ai/models/clef/
- https://developers.cloudflare.com/workers-ai/platform/pricing/
Loading