Tạo custom nodes cho IAI Flow. Mỗi node là một đơn vị xử lý độc lập với input/output contract rõ ràng.
Mỗi node executor nhận input và config, trả về output:
type NodeExecutor = (
input: unknown,
config: Record<string, unknown>,
env: ExecutorEnv
) => Promise<unknown>;
interface ExecutorEnv {
DB: D1Database; // Cloudflare D1
KV?: KVNamespace; // Cloudflare KV
ANTHROPIC_API_KEY?: string; // for AI nodes
log: LogFn; // write execution log
}
interface NodeCatalogEntry {
id: string; // e.g. "http_request"
type: string; // same as id
name: string; // display name
category: string; // ai | integration | data | logic | trigger | utility
description: string;
official: boolean;
config_schema?: Record<string, ConfigField>;
}
interface ConfigField {
type: "string" | "number" | "boolean" | "json" | "code";
label: string;
required?: boolean;
default?: unknown;
placeholder?: string;
}
// workers/api/src/nodes/my-http-node.ts
import type { NodeExecutor } from "../types";
export const myHttpNode: NodeExecutor = async (input, config, env) => {
const url = String(config.url || "");
const method = String(config.method || "GET");
const response = await fetch(url, {
method,
headers: { "Content-Type": "application/json" },
body: method !== "GET" ? JSON.stringify(input) : undefined,
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${url}`);
}
return response.json();
};
Trong workers/api/src/executor.ts, thêm node vào NODE_EXECUTORS map:
import { myHttpNode } from "./nodes/my-http-node";
const NODE_EXECUTORS: Record<string, NodeExecutor> = {
// ...existing nodes...
"my_http": myHttpNode,
};
Và thêm vào NODES_CATALOG trong index.ts:
{
id: "my_http",
type: "my_http",
name: "My HTTP Node",
category: "integration",
description: "Custom HTTP node with special logic.",
official: false,
}
| Category | Dùng cho |
|---|---|
ai | AI/LLM nodes: claude, agent, prompt |
integration | External APIs, webhooks, HTTP |
data | D1, KV, R2, transforms |
logic | Condition, branch, loop, merge |
trigger | Manual, schedule, webhook, http_trigger |
utility | Log, delay, debug |
// Test locally with wrangler dev
wrangler dev workers/api/src/index.ts --local
// Then POST a test run
curl -X POST http://localhost:8787/api/workflows/test/run \
-H "Content-Type: application/json" \
-d '{"input": {"url": "https://example.com"}}'
process.env, không có Node.js built-ins. Chỉ dùng Web APIs và Cloudflare platform APIs.