跳转到内容

示例

最简单的 Rotifer 基因,适合作为起点。

genes/hello/phenotype.json

{
"domain": "general.greeting",
"inputSchema": {
"type": "object",
"properties": {
"name": { "type": "string", "description": "要问候的名称" }
},
"required": ["name"]
},
"outputSchema": {
"type": "object",
"properties": {
"greeting": { "type": "string" }
},
"required": ["greeting"]
},
"version": "0.1.0",
"fidelity": "Wrapped",
"transparency": "Open"
}

genes/hello/index.ts

export async function express(input: { name: string }) {
return { greeting: `你好,${input.name}!欢迎来到 Rotifer。` };
}

运行:

Terminal window
rotifer test hello
rotifer arena submit hello

调用外部 API 的基因,演示 Wrapped 保真度模式。

genes/weather/index.ts

export async function express(input: { city: string; units?: string }) {
const units = input.units || "celsius";
const res = await fetch(
`https://wttr.in/${encodeURIComponent(input.city)}?format=j1`
);
const data = await res.json();
const current = data.current_condition[0];
const tempC = parseFloat(current.temp_C);
const temperature = units === "fahrenheit" ? tempC * 9/5 + 32 : tempC;
return {
temperature,
condition: current.weatherDesc[0].value,
humidity: parseFloat(current.humidity),
};
}

顺序组合两个基因:搜索网页并摘要结果。

import { Seq, Transform } from "@rotifer/algebra";
const searchAndSummarize = Seq(
"genesis-web-search",
Transform((r) => ({
text: r.results.map(r => r.snippet).join(" "),
maxLength: 200,
})),
"summarizer"
);

MCP 工具迁移 — Scan → Wrap → Compile

Section titled “MCP 工具迁移 — Scan → Wrap → Compile”

三步将现有 MCP 工具迁移为 Rotifer 基因:

Terminal window
$ rotifer scan tools/ # 第 1 步:扫描
$ rotifer wrap calculator --domain math.calculate # 第 2 步:封装
$ rotifer compile calculator # 第 3 步:编译为 WASM

纯逻辑基因,完全在 WASM 沙箱中运行。

export async function express(input: { code: string; indent?: number }) {
const indent = input.indent ?? 2;
try {
const parsed = JSON.parse(input.code);
return { formatted: JSON.stringify(parsed, null, indent), valid: true };
} catch {
return { formatted: input.code, valid: false };
}
}

由于没有外部依赖,编译后具有 Native 保真度——Arena 中的最高信任级别。