Skip to content

Custom components

Weftra never runs the Python code stored in a flow by default. If you need a component the built-in catalog does not have, you have three options.

Option Runs as When to use it
Compiled Rust component part of your server binary you embed the server in your own Rust program
Component worker a separate process speaking JSON lines any language, no rebuild of the server
Python bridge (opt-in) the lfx package in a worker process you must run Python custom components stored in your flows

All three appear in GET /api/v1/all and in the palette, and they run like built-ins.

Implement the weftra_core::Component trait and register it in the ComponentRegistry you pass to the server. Each component computes one named output per call (Component::run_output(output, &ComponentContext)). The context gives you resolved inputs, messages, streaming, cancellation, variables and branch control.

let mut registry = ComponentRegistry::with_default_aliases();
registry.register(MyEcho); // impl Component
let flow = FlowDoc::from_json_str(&std::fs::read_to_string("flow.json")?)?;
let engine = FlowEngine::new(Arc::new(registry));
let resp = engine.run(&flow.data, RunRequest::with_input("hello")).await?;

To keep the built-in catalog as well, start from weftra_components::builtin_registry(opts) and register your components on top of it.

To serve your own registry over HTTP:

let registry = Arc::new(my_registry()); // Arc<ComponentRegistry>
weftra_server::serve(ServerConfig::default(), registry).await?;

The repository’s example crates/nexura/examples/flow_from_json.rs loads a flow JSON file and runs it on the engine with the built-in components and a scripted offline model.

A worker is any program the server starts without a shell. It reads one JSON request per line on stdin and writes one JSON response per line on stdout. It answers in order, one request at a time, and exits when stdin closes. stderr goes to the server log.

Register workers with --component-worker (repeatable) or WEFTRA_COMPONENT_WORKERS. The variable takes commands separated by ; or a JSON array. At startup the server calls each worker’s list and registers the components it returns. A worker component with the same name as a built-in replaces it, with a warning. A worker that fails hello or list stops startup.

weftra --component-worker "/opt/workers/my-worker --mode prod"
# or
WEFTRA_COMPONENT_WORKERS='[["/opt/workers/my worker", "--mode", "prod"]]' weftra

Requests are {"id": <u64>, "op": "<op>", ...}. Responses are {"id", "ok": true, ...result} or {"id", "ok": false, "error": "...", "traceback": "..."}.

Op Request Response
hello {protocol} {protocol: 1, worker, version?, ops?}
list {components: [{type, frontend_node, display_name?, category?, description?}]}
describe {code, frontend_node?, class_name?, user_id} {type, frontend_node}
update {code, class_name?, template, field, field_value, tool_mode, variables, user_id} {frontend_node}
validate {code} {imports: {errors}, function: {errors}}
build {type or code + class_name, inputs, output_names, vertex_id, session_id, flow_id, user_id} {outputs: {name: value}, status?}

frontend_node describes the node: template fields with _input_type / type / input_types, and outputs with name / types / method.

Values are tagged {type, value}. Supported types are null, text, number, bool, json, message, data, dataframe and list, plus secret for inputs. Handles such as LanguageModel, Tool, Embeddings, VectorStore, Retriever and Memory cannot cross the process boundary. They fail the build with an error naming the type.

  • Each worker command gets a pool of up to WEFTRA_COMPONENT_WORKER_POOL processes (default min(4, cores)).
  • A build that runs longer than WEFTRA_COMPONENT_WORKER_TIMEOUT seconds (default 300) is killed and the process replaced. So is one that breaks the protocol or exits.
  • Workers inherit the server’s environment minus its own secrets (WEFTRA_* and alias names containing SECRET, PASSWORD, API_KEY, DATABASE_URL or TOKEN).
  • Response lines are capped at 64 MiB.

Imported flows can carry Python custom components: nodes of a type Weftra does not have, with Python in their code field. With the Python bridge on, those nodes build through an embedded worker that runs the lfx package. Registered types always run in Rust.

python3 -m venv /opt/lfx && /opt/lfx/bin/pip install lfx
WEFTRA_PYTHON_COMPONENTS=true WEFTRA_PYTHON=/opt/lfx/bin/python weftra
  • Each lfx process uses about 260 MB and takes about 8 s to import.
  • /api/v1/custom_component, /custom_component/update and /validate/code use the worker for code that is not registered.
  • WEFTRA_PYTHON_WORKER replaces the embedded worker with your own command that speaks the same protocol.
  • Not supported: messages a component sends itself (send_message), model-provider and integration policies, and tracing.

For Docker, an optional image adds the Python venv. See Deployment.

Weftra is open source under Apache-2.0. Made by Liceron.