Connects to the iii-engine via WebSocket (default: ws://localhost:49134)
Registers one or more functions with unique IDs
Stays running to handle function invocations
Optionally registers triggers to bind functions to events
Workers are stateless. They don’t store data - they connect, register functions, and handle invocations. All state is managed by the iii-engine modules (state, queue, pubsub, etc.).
Clone the iii instance for each async handler to satisfy Rust’s ownership rules:
let iii_clone = iii.clone();iii.register_function_with_description( "my::function", "Description", move |input| { let iii = iii_clone.clone(); async move { /* use iii here */ } },);
2
Parse input with serde
Use serde_json to deserialize input:
let req: ChatRequest = serde_json::from_value(input) .map_err(|e| IIIError::Handler(e.to_string()))?;
Python workers use the iii-sdk package and are ideal for ML workloads.
# From workers/embedding/main.py:1-91import asyncioimport osfrom iii_sdk import III# 1. Create III instance with worker nameiii = III( "ws://localhost:49134", worker_name="embedding",)# 2. Register function using decorator@iii.function( id="embedding::generate", description="Generate text embeddings")async def generate_embedding(input): text = input.get("text", "") batch = input.get("batch") model = get_model() if batch: embeddings = model.encode(batch, normalize_embeddings=True) return { "embeddings": [e.tolist() for e in embeddings], "dim": embeddings.shape[1], } embedding = model.encode([text], normalize_embeddings=True)[0] return {"embedding": embedding.tolist(), "dim": len(embedding)}@iii.function( id="embedding::similarity", description="Compute cosine similarity")async def compute_similarity(input): a = input.get("a", []) b = input.get("b", []) if len(a) != len(b) or not a: return {"similarity": 0.0} dot = sum(x * y for x, y in zip(a, b)) norm_a = sum(x * x for x in a) ** 0.5 norm_b = sum(x * x for x in b) ** 0.5 denom = norm_a * norm_b return {"similarity": dot / denom if denom > 0 else 0.0}# 3. Keep worker runningasync def main(): print("embedding worker started") try: await asyncio.Event().wait() except KeyboardInterrupt: await iii.shutdown()if __name__ == "__main__": asyncio.run(main())