caxton 0.1.4

A secure WebAssembly runtime for multi-agent systems
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
# Building Your First Agent

Learn how to create WebAssembly agents for Caxton in your preferred programming language.

## Agent Basics

Every Caxton agent must:
1. Export a `handle_message` function
2. Accept FIPA-formatted messages
3. Return valid FIPA responses (or null)
4. Compile to WebAssembly

## Agent Lifecycle

Agents follow a managed lifecycle:
- **Deployment**: WASM modules are validated and loaded
- **Execution**: Agents process messages in isolated sandboxes
- **Management**: Hot reload enables zero-downtime updates
- **Monitoring**: Resource usage and health are tracked

For production deployment and management, see [Agent Lifecycle Management](../operations/agent-lifecycle-management.md).

## JavaScript Agent

### Simple Echo Agent

Create `echo-agent.js`:

```javascript
// Echo agent that responds to any message
export function handle_message(message_bytes) {
    // Parse the incoming message
    const message = JSON.parse(new TextDecoder().decode(message_bytes));

    console.log(`[${message.receiver}] Received from ${message.sender}: ${message.content.text}`);

    // Create response
    const response = {
        performative: 'inform',
        sender: message.receiver,  // Our ID
        receiver: message.sender,   // Reply to sender
        conversation_id: message.conversation_id,
        in_reply_to: message.id,
        content: {
            text: `Echo: ${message.content.text}`,
            timestamp: Date.now()
        }
    };

    // Return encoded response
    return new TextEncoder().encode(JSON.stringify(response));
}

// Optional: Handle agent initialization
export function init() {
    console.log("Echo agent initialized");
    return 0;  // Success
}

// Optional: Handle agent shutdown
export function shutdown() {
    console.log("Echo agent shutting down");
    return 0;  // Success
}
```

### Compile to WebAssembly

Using [Javy](https://github.com/bytecodealliance/javy):

```bash
# Install Javy
npm install -g @bytecodealliance/javy

# Compile to WASM
javy compile echo-agent.js -o echo-agent.wasm

# Deploy to Caxton
caxton deploy echo-agent.wasm --name echo
```

### Advanced: Stateful Agent

```javascript
// Stateful counter agent
let counter = 0;
const state = new Map();

export function handle_message(message_bytes) {
    const message = JSON.parse(new TextDecoder().decode(message_bytes));

    switch(message.performative) {
        case 'request':
            return handleRequest(message);
        case 'query':
            return handleQuery(message);
        case 'subscribe':
            return handleSubscribe(message);
        default:
            return handleUnknown(message);
    }
}

function handleRequest(message) {
    const { action, params } = message.content;

    switch(action) {
        case 'increment':
            counter += params.amount || 1;
            break;
        case 'decrement':
            counter -= params.amount || 1;
            break;
        case 'reset':
            counter = 0;
            break;
    }

    return createResponse(message, {
        status: 'success',
        counter: counter
    });
}

function handleQuery(message) {
    return createResponse(message, {
        counter: counter,
        state: Object.fromEntries(state)
    });
}

function createResponse(originalMessage, content) {
    const response = {
        performative: 'inform',
        sender: originalMessage.receiver,
        receiver: originalMessage.sender,
        conversation_id: originalMessage.conversation_id,
        in_reply_to: originalMessage.id,
        content: content
    };

    return new TextEncoder().encode(JSON.stringify(response));
}
```

## Python Agent

### Setup

First, install the Python to WASM compiler:

```bash
pip install wasmtime-py pyodide-build
```

### Simple Agent

Create `weather_agent.py`:

```python
import json
import random
from datetime import datetime

class WeatherAgent:
    """Simulated weather information agent"""

    def __init__(self):
        self.cities = {
            'london': {'temp_base': 15, 'variance': 5},
            'new_york': {'temp_base': 20, 'variance': 8},
            'tokyo': {'temp_base': 18, 'variance': 6},
            'sydney': {'temp_base': 22, 'variance': 4}
        }

    def get_weather(self, city):
        """Generate simulated weather data"""
        if city.lower() not in self.cities:
            return None

        city_data = self.cities[city.lower()]
        temp = city_data['temp_base'] + random.uniform(-city_data['variance'], city_data['variance'])

        return {
            'city': city,
            'temperature': round(temp, 1),
            'unit': 'celsius',
            'conditions': random.choice(['sunny', 'cloudy', 'rainy', 'partly cloudy']),
            'humidity': random.randint(30, 80),
            'timestamp': datetime.now().isoformat()
        }

# Global agent instance
agent = WeatherAgent()

def handle_message(message_bytes):
    """Main message handler for Caxton"""
    try:
        # Decode and parse message
        message = json.loads(message_bytes.decode('utf-8'))

        # Handle different performatives
        if message['performative'] == 'query':
            return handle_query(message)
        elif message['performative'] == 'request':
            return handle_request(message)
        else:
            return create_not_understood(message)

    except Exception as e:
        return create_failure(message, str(e))

def handle_query(message):
    """Handle weather queries"""
    content = message.get('content', {})
    city = content.get('city')

    if not city:
        return create_failure(message, "No city specified")

    weather = agent.get_weather(city)

    if weather:
        response = {
            'performative': 'inform',
            'sender': message['receiver'],
            'receiver': message['sender'],
            'conversation_id': message.get('conversation_id'),
            'in_reply_to': message.get('id'),
            'content': {
                'weather': weather
            }
        }
    else:
        response = {
            'performative': 'failure',
            'sender': message['receiver'],
            'receiver': message['sender'],
            'conversation_id': message.get('conversation_id'),
            'in_reply_to': message.get('id'),
            'content': {
                'error': f"Unknown city: {city}"
            }
        }

    return json.dumps(response).encode('utf-8')

def create_not_understood(message):
    """Create not-understood response"""
    response = {
        'performative': 'not-understood',
        'sender': message['receiver'],
        'receiver': message['sender'],
        'conversation_id': message.get('conversation_id'),
        'in_reply_to': message.get('id'),
        'content': {
            'error': f"Unknown performative: {message['performative']}"
        }
    }
    return json.dumps(response).encode('utf-8')

def create_failure(message, error):
    """Create failure response"""
    response = {
        'performative': 'failure',
        'sender': message.get('receiver', 'unknown'),
        'receiver': message.get('sender', 'unknown'),
        'conversation_id': message.get('conversation_id'),
        'in_reply_to': message.get('id'),
        'content': {
            'error': error
        }
    }
    return json.dumps(response).encode('utf-8')
```

### Compile and Deploy

```bash
# Compile to WASM
pyodide build weather_agent.py -o weather_agent.wasm

# Deploy
caxton deploy weather_agent.wasm --name weather-service

# Test it
caxton message send \
  --to weather-service \
  --performative query \
  --content '{"city": "London"}'
```

## Go Agent

### Setup

```bash
# Install TinyGo (Go to WASM compiler)
wget https://github.com/tinygo-org/tinygo/releases/download/v0.30.0/tinygo_0.30.0_amd64.deb
sudo dpkg -i tinygo_0.30.0_amd64.deb
```

### Calculator Agent

Create `calculator_agent.go`:

```go
package main

import (
    "encoding/json"
    "fmt"
    "math"
)

// Message represents a FIPA message
type Message struct {
    Performative   string                 `json:"performative"`
    Sender         string                 `json:"sender"`
    Receiver       string                 `json:"receiver"`
    ConversationID string                 `json:"conversation_id"`
    ReplyWith      string                 `json:"reply_with,omitempty"`
    InReplyTo      string                 `json:"in_reply_to,omitempty"`
    Content        map[string]interface{} `json:"content"`
}

//export handle_message
func handle_message(messagePtr *byte, messageLen int) (*byte, int) {
    // Convert pointer to byte slice
    messageBytes := make([]byte, messageLen)
    for i := 0; i < messageLen; i++ {
        messageBytes[i] = *(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(messagePtr)) + uintptr(i)))
    }

    // Parse message
    var msg Message
    if err := json.Unmarshal(messageBytes, &msg); err != nil {
        return nil, 0
    }

    // Handle calculation requests
    if msg.Performative == "request" {
        response := processCalculation(msg)
        responseBytes, _ := json.Marshal(response)
        return &responseBytes[0], len(responseBytes)
    }

    return nil, 0
}

func processCalculation(msg Message) Message {
    operation, _ := msg.Content["operation"].(string)
    operands, _ := msg.Content["operands"].([]interface{})

    var result float64
    var err error

    switch operation {
    case "add":
        result = add(operands)
    case "multiply":
        result = multiply(operands)
    case "power":
        if len(operands) == 2 {
            base, _ := operands[0].(float64)
            exp, _ := operands[1].(float64)
            result = math.Pow(base, exp)
        }
    default:
        err = fmt.Errorf("unknown operation: %s", operation)
    }

    response := Message{
        Performative:   "inform",
        Sender:         msg.Receiver,
        Receiver:       msg.Sender,
        ConversationID: msg.ConversationID,
        InReplyTo:      msg.ReplyWith,
    }

    if err != nil {
        response.Performative = "failure"
        response.Content = map[string]interface{}{
            "error": err.Error(),
        }
    } else {
        response.Content = map[string]interface{}{
            "result": result,
            "operation": operation,
        }
    }

    return response
}

func add(operands []interface{}) float64 {
    sum := 0.0
    for _, op := range operands {
        if val, ok := op.(float64); ok {
            sum += val
        }
    }
    return sum
}

func multiply(operands []interface{}) float64 {
    product := 1.0
    for _, op := range operands {
        if val, ok := op.(float64); ok {
            product *= val
        }
    }
    return product
}

func main() {
    // Required for WASM
}
```

### Compile and Deploy

```bash
# Compile with TinyGo
tinygo build -o calculator.wasm -target wasi calculator_agent.go

# Deploy
caxton deploy calculator.wasm --name calculator

# Test
caxton message send \
  --to calculator \
  --performative request \
  --content '{"operation": "add", "operands": [5, 3, 2]}'
```

## Rust Agent

### Setup

```toml
# Cargo.toml
[package]
name = "rust-agent"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
wasm-bindgen = "0.2"

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

[profile.release]
opt-level = "z"
lto = true
```

### Smart Contract Agent

Create `src/lib.rs`:

```rust
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use wasm_bindgen::prelude::*;

#[derive(Serialize, Deserialize)]
struct Message {
    performative: String,
    sender: String,
    receiver: String,
    conversation_id: Option<String>,
    in_reply_to: Option<String>,
    content: serde_json::Value,
}

#[derive(Default)]
struct ContractState {
    balances: HashMap<String, u64>,
    total_supply: u64,
}

static mut STATE: Option<ContractState> = None;

#[no_mangle]
pub extern "C" fn init() -> i32 {
    unsafe {
        STATE = Some(ContractState {
            balances: HashMap::new(),
            total_supply: 1_000_000,
        });
    }
    0 // Success
}

#[no_mangle]
pub extern "C" fn handle_message(ptr: *const u8, len: usize) -> *const u8 {
    let bytes = unsafe { std::slice::from_raw_parts(ptr, len) };

    let message: Message = match serde_json::from_slice(bytes) {
        Ok(msg) => msg,
        Err(_) => return std::ptr::null(),
    };

    let response = match message.performative.as_str() {
        "request" => handle_request(message),
        "query" => handle_query(message),
        _ => create_not_understood(message),
    };

    match response {
        Some(resp) => {
            let json = serde_json::to_vec(&resp).unwrap();
            Box::into_raw(json.into_boxed_slice()) as *const u8
        }
        None => std::ptr::null(),
    }
}

fn handle_request(msg: Message) -> Option<Message> {
    let state = unsafe { STATE.as_mut()? };

    let action = msg.content.get("action")?.as_str()?;

    match action {
        "transfer" => {
            let from = msg.content.get("from")?.as_str()?;
            let to = msg.content.get("to")?.as_str()?;
            let amount = msg.content.get("amount")?.as_u64()?;

            // Perform transfer
            let from_balance = state.balances.entry(from.to_string()).or_insert(0);
            if *from_balance < amount {
                return create_failure(msg, "Insufficient balance");
            }

            *from_balance -= amount;
            *state.balances.entry(to.to_string()).or_insert(0) += amount;

            Some(Message {
                performative: "inform".to_string(),
                sender: msg.receiver,
                receiver: msg.sender,
                conversation_id: msg.conversation_id,
                in_reply_to: Some(msg.conversation_id.unwrap_or_default()),
                content: serde_json::json!({
                    "status": "success",
                    "from": from,
                    "to": to,
                    "amount": amount,
                }),
            })
        }
        "mint" => {
            let to = msg.content.get("to")?.as_str()?;
            let amount = msg.content.get("amount")?.as_u64()?;

            *state.balances.entry(to.to_string()).or_insert(0) += amount;
            state.total_supply += amount;

            Some(Message {
                performative: "inform".to_string(),
                sender: msg.receiver,
                receiver: msg.sender,
                conversation_id: msg.conversation_id,
                in_reply_to: Some(msg.conversation_id.unwrap_or_default()),
                content: serde_json::json!({
                    "status": "success",
                    "minted": amount,
                    "to": to,
                    "total_supply": state.total_supply,
                }),
            })
        }
        _ => create_not_understood(msg),
    }
}

fn handle_query(msg: Message) -> Option<Message> {
    let state = unsafe { STATE.as_ref()? };

    let query_type = msg.content.get("type")?.as_str()?;

    match query_type {
        "balance" => {
            let account = msg.content.get("account")?.as_str()?;
            let balance = state.balances.get(account).unwrap_or(&0);

            Some(Message {
                performative: "inform".to_string(),
                sender: msg.receiver,
                receiver: msg.sender,
                conversation_id: msg.conversation_id,
                in_reply_to: Some(msg.conversation_id.unwrap_or_default()),
                content: serde_json::json!({
                    "account": account,
                    "balance": balance,
                }),
            })
        }
        "total_supply" => {
            Some(Message {
                performative: "inform".to_string(),
                sender: msg.receiver,
                receiver: msg.sender,
                conversation_id: msg.conversation_id,
                in_reply_to: Some(msg.conversation_id.unwrap_or_default()),
                content: serde_json::json!({
                    "total_supply": state.total_supply,
                }),
            })
        }
        _ => create_not_understood(msg),
    }
}

fn create_not_understood(msg: Message) -> Option<Message> {
    Some(Message {
        performative: "not-understood".to_string(),
        sender: msg.receiver,
        receiver: msg.sender,
        conversation_id: msg.conversation_id,
        in_reply_to: Some(msg.conversation_id.unwrap_or_default()),
        content: serde_json::json!({
            "error": "Message not understood",
        }),
    })
}

fn create_failure(msg: Message, error: &str) -> Option<Message> {
    Some(Message {
        performative: "failure".to_string(),
        sender: msg.receiver,
        receiver: msg.sender,
        conversation_id: msg.conversation_id,
        in_reply_to: Some(msg.conversation_id.unwrap_or_default()),
        content: serde_json::json!({
            "error": error,
        }),
    })
}
```

### Compile and Deploy

```bash
# Build for WASM
cargo build --target wasm32-wasi --release

# Deploy
caxton deploy target/wasm32-wasi/release/rust_agent.wasm --name token-contract

# Test minting
caxton message send \
  --to token-contract \
  --performative request \
  --content '{"action": "mint", "to": "alice", "amount": 1000}'

# Query balance
caxton message send \
  --to token-contract \
  --performative query \
  --content '{"type": "balance", "account": "alice"}'
```

## Testing Your Agent

### Unit Testing

Test your agent logic before deployment:

```javascript
// test-agent.js
import { handle_message } from './echo-agent.js';

function test_echo() {
    const testMessage = {
        performative: 'inform',
        sender: 'test-sender',
        receiver: 'echo-agent',
        content: { text: 'Hello' }
    };

    const input = new TextEncoder().encode(JSON.stringify(testMessage));
    const output = handle_message(input);
    const response = JSON.parse(new TextDecoder().decode(output));

    console.assert(response.content.text === 'Echo: Hello');
    console.log('✓ Echo test passed');
}

test_echo();
```

### Integration Testing

Test with Caxton running:

```bash
# Deploy test agent
caxton deploy my-agent.wasm --name test-agent

# Send test messages
caxton test agent test-agent --suite basic

# Custom test script
cat > test.sh << 'EOF'
#!/bin/bash
# Send message and check response
RESPONSE=$(caxton message send \
  --to test-agent \
  --performative request \
  --content '{"test": true}' \
  --wait-reply \
  --timeout 5s)

echo "$RESPONSE" | jq '.content.status' | grep -q "success"
EOF

chmod +x test.sh
./test.sh
```

## Best Practices

1. **Handle All Performatives**: Always handle unknown performatives gracefully
2. **Validate Input**: Check message format and content before processing
3. **Use Structured Logging**: Log important events for debugging
4. **Implement Timeouts**: Don't block indefinitely on operations
5. **Minimize State**: Keep agent state minimal and consider persistence
6. **Error Handling**: Return appropriate FIPA error responses
7. **Resource Limits**: Be mindful of memory and CPU usage
8. **Idempotency**: Make operations idempotent where possible

## Next Steps

- [Message Protocol Guide]../developer-guide/message-protocols.md - Deep dive into FIPA protocols
- [API Reference]../developer-guide/api-reference.md - Complete API documentation
- [Testing Guide]../developer-guide/testing.md - Comprehensive testing strategies
- [Production Deployment]../operations/production-deployment.md - Deploy to production