colonyos 1.0.5

Rust SDK for ColonyOS - build distributed applications with executors that can run anywhere
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# Building a WASM Executor

This tutorial explains how to build a ColonyOS executor that runs in WebAssembly (WASM), enabling executors to run directly in web browsers.

![WASM Executor](imgs/wasm.png)

## Overview

A WASM executor allows you to run ColonyOS workers in the browser. This is useful for:

- Distributing computation to end-user devices
- Building browser-based development tools
- Creating interactive demos
- Edge computing scenarios where executors run on client devices

## Architecture

```mermaid
graph TB
    subgraph Browser
        W[WASM Module]
        JS[JavaScript]
        UI[Web UI]
    end

    subgraph ColonyOS
        S[Server]
        Q[(Process Queue)]
    end

    subgraph CLI
        C[colonies CLI]
    end

    JS --> W
    UI --> JS
    W <-->|HTTP/WebSocket| S
    S --> Q
    C -->|submit| S
    Q -->|assign| W
    W -->|result| S
    S -->|response| C
```

## WASM Executor Flow

```mermaid
sequenceDiagram
    participant Browser
    participant WASM
    participant Server
    participant CLI

    Browser->>WASM: Initialize module
    WASM->>Server: register()
    WASM->>Server: approve()

    CLI->>Server: submit(func, targettype=wasm)
    Server-->>CLI: Process ID

    loop Long-polling
        WASM->>Server: assign()
        Server-->>WASM: Process
        WASM->>WASM: Execute in browser
        WASM->>Server: set_output()
        WASM->>Server: close()
    end

    Server-->>CLI: Result
```

## Prerequisites

- Rust toolchain with `wasm32-unknown-unknown` target
- `wasm-pack` for building and packaging WASM modules
- A ColonyOS server accessible from the browser

### Installing Prerequisites

```bash
# Add WASM target
rustup target add wasm32-unknown-unknown

# Install wasm-pack
cargo install wasm-pack
```

## Project Structure

The WASM executor example is located in `examples/wasm/`:

```
examples/wasm/
├── Cargo.toml      # Dependencies for WASM build
├── src/
│   └── lib.rs      # Executor implementation
└── index.html      # Browser UI for running the executor
```

## Understanding the Code

### Dependencies (Cargo.toml)

```toml
[package]
name = "wasm-executor"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
# ColonyOS SDK with WASM feature
colonyos = { version = "1.0", default-features = false, features = ["wasm"] }

# WASM bindings
wasm-bindgen = "0.2.88"
wasm-bindgen-futures = "0.4.38"
js-sys = "0.3"
web-sys = { version = "0.3", features = ["console", "Window", ...] }

# Async runtime for WASM
gloo-timers = { version = "0.3", features = ["futures"] }
```

Key points:
- `crate-type = ["cdylib"]` enables WASM compilation
- `colonyos` uses `default-features = false` and `features = ["wasm"]`
- `wasm-bindgen` provides JavaScript interop
- `gloo-timers` provides async timer support in WASM

### Executor Implementation (src/lib.rs)

The executor auto-generates its private key and uses the colony private key for registration:

```rust
use colonyos::core::{Executor, Process};
use colonyos::crypto;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct WasmExecutorConfig {
    server_url: String,
    colony_name: String,
    executor_name: String,
    executor_type: String,
    colony_prvkey: String,  // Colony owner key for registration
}

#[wasm_bindgen]
pub struct WasmExecutor {
    config: WasmExecutorConfig,
    private_key: String,    // Auto-generated executor key
    executor_id: String,
    running: bool,
}

#[wasm_bindgen]
impl WasmExecutor {
    #[wasm_bindgen(constructor)]
    pub fn new(config: WasmExecutorConfig) -> Self {
        // Auto-generate a new private key for this executor
        let private_key = crypto::gen_prvkey();
        let executor_id = crypto::gen_id(&private_key);

        WasmExecutor {
            config,
            private_key,
            executor_id,
            running: false,
        }
    }

    #[wasm_bindgen]
    pub async fn register(&self) -> Result<(), JsValue> {
        let executor = Executor::new(
            &self.config.executor_name,
            &self.executor_id,
            &self.config.executor_type,
            &self.config.colony_name,
        );

        // Register using colony owner key
        colonyos::add_executor(&executor, &self.config.colony_prvkey)
            .await
            .map_err(|e| JsValue::from_str(&e.to_string()))?;

        // Auto-approve using colony owner key
        colonyos::approve_executor(
            &self.config.colony_name,
            &self.config.executor_name,
            &self.config.colony_prvkey,
        ).await.map_err(|e| JsValue::from_str(&e.to_string()))?;

        Ok(())
    }

    #[wasm_bindgen]
    pub async fn start(&mut self) -> Result<(), JsValue> {
        self.running = true;

        while self.running {
            // Long-poll for processes (10 second timeout)
            match colonyos::assign(&self.config.colony_name, 10, &self.private_key).await {
                Ok(process) => {
                    self.execute_process(&process).await?;
                }
                Err(e) => {
                    if e.conn_err() {
                        // Wait before retrying on connection error
                        gloo_timers::future::TimeoutFuture::new(1000).await;
                    }
                    // Timeout means no process available - continue polling
                }
            }
        }

        Ok(())
    }
}
```

### Handling Functions

The executor can handle different function types:

```rust
async fn execute_process(&self, process: &Process) -> Result<(), String> {
    match process.spec.funcname.as_str() {
        "echo" => {
            let output = process.spec.args.first().cloned().unwrap_or_default();
            self.complete_process(&process.processid, vec![output]).await
        }
        "add" => {
            let a: f64 = process.spec.args[0].parse().unwrap();
            let b: f64 = process.spec.args[1].parse().unwrap();
            self.complete_process(&process.processid, vec![(a + b).to_string()]).await
        }
        "browser_info" => {
            // Access browser APIs via web-sys
            let window = web_sys::window().unwrap();
            let navigator = window.navigator();
            let user_agent = navigator.user_agent().unwrap_or_default();
            self.complete_process(&process.processid, vec![user_agent]).await
        }
        _ => {
            colonyos::fail(&process.processid, &self.private_key).await;
            Ok(())
        }
    }
}
```

### JavaScript Integration (index.html)

```javascript
import init, { WasmExecutor, WasmExecutorConfig } from './pkg/wasm_executor.js';

async function main() {
    // Initialize WASM module
    await init();

    // Create configuration (executor key is auto-generated)
    const config = new WasmExecutorConfig(
        "http://localhost:50080",  // Server URL
        "dev",                      // Colony name
        "wasm-executor",            // Executor name
        "wasm",                     // Executor type
        "colony_private_key_here"   // Colony owner key for registration
    );

    // Create and start executor
    const executor = new WasmExecutor(config);
    await executor.register();  // Registers and auto-approves
    await executor.start();     // Starts long-polling for processes
}

main().catch(console.error);
```

## Building

```mermaid
graph LR
    A[src/lib.rs] -->|wasm-pack build| B[pkg/]
    B --> C[wasm_executor.js]
    B --> D[wasm_executor_bg.wasm]
    C --> E[index.html]
    D --> E
    E -->|serve| F[Browser]
```

```bash
cd examples/wasm

# Build for web target
wasm-pack build --target web

# The output is in pkg/
ls pkg/
# wasm_executor.js
# wasm_executor_bg.wasm
# ...
```

## Running

1. Start a ColonyOS server (e.g., using docker-compose):

```bash
cd /path/to/colonies
docker-compose up -d
source docker-compose.env
```

2. Serve the example with a local web server:

```bash
cd examples/wasm

# Using Python
python3 -m http.server 8080

# Or using Node.js
npx serve .
```

3. Open `http://localhost:8080` in your browser (or use your machine's IP for remote access)

4. Configure the settings:
   - **Server URL**: Your ColonyOS server address (e.g., `http://10.0.0.200:50080`)
   - **Colony Name**: The colony to join (e.g., `dev`)
   - **Executor Name**: A unique name for this executor
   - **Executor Type**: The type for process matching (e.g., `wasm`)
   - **Colony Private Key**: The colony owner's private key for registration

5. Click "Start Executor"

## Submitting Processes

From the CLI, submit processes targeting the WASM executor:

```bash
# Source environment variables
source docker-compose.env

# Echo function
colonies function exec --func echo --args "Hello from browser!" --targettype wasm --wait --out

# Add two numbers
colonies function exec --func add --args 5 --args 3 --targettype wasm --wait --out

# Multiply two numbers
colonies function exec --func multiply --args 7 --args 6 --targettype wasm --wait --out

# Get a greeting
colonies function exec --func greet --args "World" --targettype wasm --wait --out

# Get browser information
colonies function exec --func browser_info --targettype wasm --wait --out
```

## Supported Functions

The example WASM executor supports these functions:

| Function | Arguments | Description |
|----------|-----------|-------------|
| `echo` | `message` | Returns the message back |
| `add` | `a, b` | Adds two numbers |
| `multiply` | `a, b` | Multiplies two numbers |
| `greet` | `name` | Returns a greeting message |
| `browser_info` | none | Returns browser user agent and language |

## Security Considerations

- **Colony Private Key**: The colony owner key is used for self-registration. In production, consider using a registration service instead.
- **Executor Keys**: Each executor auto-generates its own private key on startup.
- **Process Validation**: Validate process inputs before processing to prevent injection attacks.

## Extending the Executor

To add new functions:

1. Add a new match arm in `execute_process`:

```rust
"my_function" => {
    // Your implementation
    let result = do_something(&process.spec.args);
    self.complete_process(&process.processid, vec![result]).await
}
```

2. Use browser APIs via `web-sys` for capabilities like:
   - Canvas drawing
   - WebGL computations
   - Geolocation
   - WebRTC communication

## Troubleshooting

### Connection Errors

If you see "Connection error" in the browser console:
- Verify the server URL is correct and accessible
- Check that the server is running
- For remote servers, ensure the port is open

### "No process available"

This is normal - it means the long-poll timed out without a process. The executor will automatically retry.

### Registration Errors

If registration fails with "Access denied":
- Verify the colony private key is correct
- Check that the colony exists on the server

### Memory Issues

WebAssembly has memory limits. For large computations:
- Process data in chunks
- Use streaming where possible
- Monitor memory usage in browser dev tools

## Next Steps

- See [getting-started.md]getting-started.md for general SDK usage
- See [API.md]API.md for the complete API reference
- Explore the source code in [examples/wasm/src/lib.rs]../examples/wasm/src/lib.rs