brainwires-wasm 0.6.0

WASM bindings for the Brainwires Agent Framework
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
# brainwires-wasm

[![Crates.io](https://img.shields.io/crates/v/brainwires-wasm.svg)](https://crates.io/crates/brainwires-wasm)
[![Documentation](https://img.shields.io/docsrs/brainwires-wasm)](https://docs.rs/brainwires-wasm)
[![License](https://img.shields.io/crates/l/brainwires-wasm.svg)](LICENSE)

WebAssembly bindings for the Brainwires Agent Framework.

## Overview

`brainwires-wasm` provides a JavaScript-friendly API for running the Brainwires Agent Framework in browser and Node.js environments via WebAssembly. The crate exposes core type validation, conversation history serialization, and a sandboxed tool orchestrator that lets AI models execute Rhai scripts calling registered JavaScript tool callbacks — all with configurable resource limits to prevent runaway execution.

**Design principles:**

- **Browser-native** — compiles to `cdylib` WASM module consumable by `wasm-pack`, `wasm-bindgen`, or any bundler; no OS-specific dependencies
- **Zero-copy validation** — parse and re-serialize Messages and Tools through the canonical Rust types to guarantee schema conformance from JavaScript
- **Sandboxed orchestration** — Rhai script engine runs with operation limits, call-depth caps, string/array size bounds, and real-time timeouts to prevent abuse
- **Incremental opt-in** — default build includes only validation and serialization; heavier features (`interpreters`, `orchestrator`) are behind Cargo feature flags
- **Transparent interop** — all public functions accept and return JSON strings or `JsValue`, making TypeScript integration straightforward

```text
  ┌───────────────────────────────────────────────────────────────────────┐
  │                          brainwires-wasm                             │
  │                                                                      │
  │  JavaScript / TypeScript                                             │
  │      │                                                               │
  │      ▼                                                               │
  │  ┌─── Core Bindings ──────────────────────────────────────────────┐  │
  │  │  version()            → framework version string               │  │
  │  │  validate_message()   → parse + re-serialize Message JSON      │  │
  │  │  validate_tool()      → parse + re-serialize Tool JSON         │  │
  │  │  serialize_history()  → messages → stateless protocol format   │  │
  │  └────────────────────────────────────────────────────────────────┘  │
  │                                                                      │
  │  ┌─── Re-exports (Rust consumers) ────────────────────────────────┐  │
  │  │  brainwires_core   — Message, Tool, Content, Role, …          │  │
  │  │  brainwires_mdap   — MdapConfig, MdapPreset, MdapMetrics, …   │  │
  │  │  brainwires_code_interpreters  (interpreters feature)          │  │
  │  └────────────────────────────────────────────────────────────────┘  │
  │                                                                      │
  │  ┌─── WasmOrchestrator (orchestrator feature) ────────────────────┐  │
  │  │  register_tool(name, js_callback)                              │  │
  │  │  registered_tools() → [String]                                 │  │
  │  │  execute(script, limits) → OrchestratorResult                  │  │
  │  │                                                                │  │
  │  │  ExecutionLimits                                               │  │
  │  │    ::new()      — default (100k ops, 50 calls)                 │  │
  │  │    ::quick()    — constrained (10k ops, 10 calls)              │  │
  │  │    ::extended() — generous (for complex orchestration)         │  │
  │  │                                                                │  │
  │  │  Rhai Engine (sandboxed)                                       │  │
  │  │    max_operations • max_tool_calls • timeout_ms                │  │
  │  │    max_string_size • max_array_size • max_map_size             │  │
  │  │    max_expr_depth(64) • max_call_depth(64)                     │  │
  │  └────────────────────────────────────────────────────────────────┘  │
  └───────────────────────────────────────────────────────────────────────┘
```

## Quick Start

Add to your `Cargo.toml`:

```toml
[dependencies]
brainwires-wasm = "0.6"
```

Build with `wasm-pack`:

```bash
wasm-pack build --target web
```

Use from JavaScript / TypeScript:

```js
import init, {
  version,
  validate_message,
  validate_tool,
  serialize_history,
} from "./pkg/brainwires_wasm.js";

await init();

// Check framework version
console.log(version()); // → "0.2.0"

// Validate a message
const msg = JSON.stringify({
  role: "user",
  content: [{ type: "text", text: "Hello" }],
});
const normalized = validate_message(msg);
console.log(normalized); // → canonical JSON

// Validate a tool definition
const tool = JSON.stringify({
  name: "read_file",
  description: "Read a file from disk",
  input_schema: { type: "object", properties: { path: { type: "string" } } },
});
const normalizedTool = validate_tool(tool);

// Serialize conversation history for API requests
const history = JSON.stringify([
  { role: "user", content: [{ type: "text", text: "Hello" }] },
  { role: "assistant", content: [{ type: "text", text: "Hi there!" }] },
]);
const stateless = serialize_history(history);
console.log(stateless); // → stateless protocol format
```

## Features

| Feature | Default | Description |
|---------|---------|-------------|
| *(none)* | Yes | Core bindings: `version`, `validate_message`, `validate_tool`, `serialize_history` |
| `interpreters` | No | Enables `brainwires-code-interpreters` re-export for WASM code execution |
| `orchestrator` | No | Enables `WasmOrchestrator` and `ExecutionLimits` with Rhai script engine, `js-sys`, `web-sys`, and real-time timeout support |

```toml
# Default (validation + serialization only)
brainwires-wasm = "0.6"

# With code interpreters
brainwires-wasm = { version = "0.6", features = ["interpreters"] }

# With tool orchestration
brainwires-wasm = { version = "0.6", features = ["orchestrator"] }

# Everything enabled
brainwires-wasm = { version = "0.6", features = ["interpreters", "orchestrator"] }
```

## Architecture

### Core Bindings

Free functions exposed to JavaScript via `#[wasm_bindgen]`.

| Function | Signature | Description |
|----------|-----------|-------------|
| `version` | `() → String` | Returns the crate version (`CARGO_PKG_VERSION`) |
| `validate_message` | `(json: &str) → Result<String, String>` | Parse JSON into `brainwires_core::Message`, re-serialize to canonical form |
| `validate_tool` | `(json: &str) → Result<String, String>` | Parse JSON into `brainwires_core::Tool`, re-serialize to canonical form |
| `serialize_history` | `(messages_json: &str) → Result<String, String>` | Convert `Vec<Message>` JSON to stateless protocol format via `serialize_messages_to_stateless_history` |

All functions accept plain JSON strings and return JSON strings or descriptive error messages, making them easy to call from any JavaScript runtime.

### ExecutionLimits (requires `orchestrator` feature)

WASM-compatible wrapper around `brainwires_tool_system::orchestrator::ExecutionLimits` with JavaScript getter/setter bindings.

| Constructor | Description |
|-------------|-------------|
| `new()` | Default limits — balanced for typical scripts |
| `quick()` | Constrained limits for simple, fast scripts |
| `extended()` | Generous limits for complex orchestration |

**Default preset values:**

| Property | `new()` | `quick()` | `extended()` |
|----------|---------|-----------|--------------|
| `max_operations` | 100,000 | 10,000 | *(extended)* |
| `max_tool_calls` | 50 | 10 | *(extended)* |
| `timeout_ms` | *(default)* | *(quick)* | *(extended)* |
| `max_string_size` | *(default)* | *(quick)* | *(extended)* |
| `max_array_size` | *(default)* | *(quick)* | *(extended)* |

All properties have JavaScript-compatible getters and setters:

| Property | Type | Description |
|----------|------|-------------|
| `max_operations` | `u64` | Maximum Rhai operations before termination |
| `max_tool_calls` | `usize` | Maximum tool invocations per execution |
| `timeout_ms` | `u64` | Real-time wall-clock timeout in milliseconds |
| `max_string_size` | `usize` | Maximum allowed string length in the script |
| `max_array_size` | `usize` | Maximum allowed array length in the script |

### WasmOrchestrator (requires `orchestrator` feature)

JavaScript-compatible tool orchestrator that executes Rhai scripts with registered tool callbacks.

| Method | Description |
|--------|-------------|
| `new()` | Create orchestrator; sets up `console_error_panic_hook` for better browser error messages |
| `register_tool(name, callback)` | Register a JavaScript function as a tool executor; callback receives JSON string, returns string |
| `registered_tools()` | List names of all registered tools → `Vec<String>` |
| `execute(script, limits)` | Execute a Rhai script with resource limits → `Result<JsValue, JsValue>` containing `OrchestratorResult` |

**Engine safety configuration:**

| Constant | Value | Description |
|----------|-------|-------------|
| `MAX_EXPR_DEPTH` | 64 | Maximum expression nesting depth (prevents stack overflow) |
| `MAX_CALL_DEPTH` | 64 | Maximum function call nesting depth (prevents deep recursion) |

**`OrchestratorResult` (returned by `execute`):**

| Field | Type | Description |
|-------|------|-------------|
| `success` | `bool` | Whether script completed without errors |
| `output` | `String` | Script return value (or error message) |
| `tool_calls` | `Vec<ToolCall>` | Record of every tool invocation during execution |
| `execution_time_ms` | `u64` | Total wall-clock execution time |

**`ToolCall` (per-invocation record):**

| Field | Type | Description |
|-------|------|-------------|
| `tool_name` | `String` | Name of the tool called |
| `input` | `Value` | JSON input passed to the tool |
| `output` | `String` | String result returned by the tool |
| `success` | `bool` | Whether the call succeeded |
| `duration_ms` | `u64` | Wall-clock duration of this call |

**Error handling during execution:**

| Error | Reported As |
|-------|-------------|
| Script compilation failure | `OrchestratorResult::error` with `"Compilation error: ..."` |
| Operation limit exceeded | `"Script exceeded maximum operations (N)"` |
| Timeout exceeded | `"Script execution timed out after Nms"` |
| Tool call limit exceeded | Tool returns `"ERROR: Maximum tool calls (N) exceeded"` |
| JavaScript callback throws | Tool returns `"Tool error: ..."` |
| Other Rhai runtime errors | `"Execution error: ..."` |

### Re-exports

For Rust consumers, the crate re-exports WASM-safe framework crates:

| Re-export | Always | Feature |
|-----------|--------|---------|
| `brainwires_core` | Yes ||
| `brainwires_mdap` | Yes ||
| `brainwires_code_interpreters` | No | `interpreters` |
| `WasmOrchestrator` | No | `orchestrator` |
| `WasmExecutionLimits` | No | `orchestrator` |

## Usage Examples

### Validate messages from JavaScript

```js
import init, { validate_message } from "./pkg/brainwires_wasm.js";
await init();

// Valid message — returns normalized JSON
const valid = validate_message(
  '{"role":"user","content":[{"type":"text","text":"Hello"}]}'
);
console.log(JSON.parse(valid));

// Invalid message — returns descriptive error
try {
  validate_message('{"role":"invalid"}');
} catch (e) {
  console.error(e); // → "Invalid message JSON: ..."
}
```

### Validate tool definitions

```js
import init, { validate_tool } from "./pkg/brainwires_wasm.js";
await init();

const tool = validate_tool(
  JSON.stringify({
    name: "search",
    description: "Search the codebase",
    input_schema: {
      type: "object",
      properties: {
        query: { type: "string", description: "Search query" },
      },
      required: ["query"],
    },
  })
);
console.log(JSON.parse(tool));
```

### Serialize conversation history for API calls

```js
import init, { serialize_history } from "./pkg/brainwires_wasm.js";
await init();

const messages = [
  { role: "user", content: [{ type: "text", text: "What is Rust?" }] },
  {
    role: "assistant",
    content: [{ type: "text", text: "Rust is a systems programming language." }],
  },
  { role: "user", content: [{ type: "text", text: "Show me an example." }] },
];

const stateless = serialize_history(JSON.stringify(messages));
// → Stateless protocol format ready for API requests
```

### Execute Rhai scripts with tool orchestration

```js
import init, {
  WasmOrchestrator,
  ExecutionLimits,
} from "./pkg/brainwires_wasm.js";
await init();

const orchestrator = new WasmOrchestrator();

// Register tools as JavaScript callbacks
orchestrator.register_tool("read_file", (jsonInput) => {
  const { path } = JSON.parse(jsonInput);
  return `Contents of ${path}: hello world`;
});

orchestrator.register_tool("write_file", (jsonInput) => {
  const { path, content } = JSON.parse(jsonInput);
  return `Wrote ${content.length} bytes to ${path}`;
});

console.log(orchestrator.registered_tools());
// → ["read_file", "write_file"]

// Execute a Rhai script that calls the registered tools
const limits = new ExecutionLimits(); // default: 100k ops, 50 calls
const result = orchestrator.execute(
  `
    let data = read_file(#{ path: "config.json" });
    let summary = "Processed: " + data;
    write_file(#{ path: "output.txt", content: summary });
    summary
  `,
  limits
);

console.log(result.success); // → true
console.log(result.output); // → "Processed: Contents of config.json: hello world"
console.log(result.tool_calls.length); // → 2
console.log(result.execution_time_ms); // → 5
```

### Use quick limits for simple scripts

```js
import init, {
  WasmOrchestrator,
  ExecutionLimits,
} from "./pkg/brainwires_wasm.js";
await init();

const orchestrator = new WasmOrchestrator();
orchestrator.register_tool("greet", (jsonInput) => {
  const { name } = JSON.parse(jsonInput);
  return `Hello, ${name}!`;
});

// Quick limits: 10k operations, 10 tool calls
const limits = ExecutionLimits.quick();
const result = orchestrator.execute(
  `greet(#{ name: "World" })`,
  limits
);
console.log(result.output); // → "Hello, World!"
```

### Custom execution limits

```js
import init, { ExecutionLimits } from "./pkg/brainwires_wasm.js";
await init();

const limits = new ExecutionLimits();

// Customize individual properties
limits.max_operations = 50_000;
limits.max_tool_calls = 20;
limits.timeout_ms = 5000;
limits.max_string_size = 1024 * 1024; // 1 MB
limits.max_array_size = 10_000;

console.log(limits.max_operations); // → 50000
console.log(limits.max_tool_calls); // → 20
```

### Use from Rust (re-exports)

```rust
use brainwires_wasm::brainwires_core::{Message, Tool, Role};
use brainwires_wasm::brainwires_mdap::MdapConfig;

// Access all core types through the WASM crate
let msg = Message {
    role: Role::User,
    content: vec![],
    ..Default::default()
};

// WASM bindings also available
let version = brainwires_wasm::version();
```

## Integration

Use via the `brainwires` facade crate with the `wasm` feature, or depend on `brainwires-wasm` directly:

```toml
# Via facade
[dependencies]
brainwires = { version = "0.6", features = ["wasm"] }

# Direct
[dependencies]
brainwires-wasm = "0.6"
```

The crate re-exports all components at the top level:

```rust
use brainwires_wasm::{
    // WASM bindings (always available)
    version, validate_message, validate_tool, serialize_history,

    // Core framework types (Rust consumers)
    brainwires_core,
    brainwires_mdap,
};

// With `interpreters` feature
#[cfg(feature = "interpreters")]
use brainwires_wasm::brainwires_code_interpreters;

// With `orchestrator` feature
#[cfg(feature = "orchestrator")]
use brainwires_wasm::{WasmExecutionLimits, WasmOrchestrator};
use brainwires_wasm::wasm_orchestrator::{ExecutionLimits, WasmOrchestrator};
```

### Building for different targets

```bash
# Browser (ES module)
wasm-pack build --target web

# Bundler (webpack, vite, etc.)
wasm-pack build --target bundler

# Node.js
wasm-pack build --target nodejs

# No bundler (standalone)
wasm-pack build --target no-modules
```

## License

Licensed under the MIT License. See [LICENSE](../../LICENSE) for details.