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
//! # Brainwires WASM
//!
//! WASM bindings for the Brainwires Agent Framework.
//!
//! This crate provides a JavaScript-friendly API for the WASM-compatible subset of the
//! Brainwires framework, enabling browser-based AI agent applications. All public functions
//! are exposed via `wasm-bindgen` and can be called directly from JavaScript/TypeScript.
//!
//! ## Features
//!
//! - **Core types** (messages, tools, tasks) — always available
//! - **MDAP types and configuration** — always available
//! - **Code interpreters** — enabled with the `interpreters` feature
//! - **Tool orchestrator** — enabled with the `orchestrator` feature; provides a Rhai-based
//! script engine that can invoke JavaScript tool callbacks from WASM
//!
//! ## JS Usage
//!
//! ```js
//! import init, { version, validate_message, serialize_history } from 'brainwires-wasm';
//!
//! await init();
//! console.log(version()); // e.g. "0.4.1"
//! ```
use *;
/// Re-export of the [`brainwires_core`] crate for Rust consumers who depend on this
/// WASM crate and need access to core types (`Message`, `Tool`, `Task`, etc.).
pub use brainwires_core;
/// Re-export of the MDAP module from [`brainwires_agents`] for Rust consumers
/// who need MDAP (Multi-Dimensional Adaptive Planning) types and configuration.
pub use mdap;
/// Re-export of the [`brainwires_code_interpreters`] crate (requires the `interpreters` feature).
///
/// Provides sandboxed code execution capabilities for languages like JavaScript and Python
/// within the WASM environment.
pub use brainwires_code_interpreters;
/// WASM orchestrator module providing JavaScript-compatible bindings for the tool orchestrator.
///
/// This module is only available when the `orchestrator` feature is enabled. It exposes
/// [`WasmOrchestrator`](wasm_orchestrator::WasmOrchestrator) and
/// [`ExecutionLimits`](wasm_orchestrator::ExecutionLimits) for running Rhai scripts
/// that can call registered JavaScript tool functions.
/// Convenience re-exports of the orchestrator types at crate root level.
///
/// - [`WasmExecutionLimits`] — Alias for [`wasm_orchestrator::ExecutionLimits`]
/// - [`WasmOrchestrator`] — The main orchestrator entry point
pub use ;
// ── WASM Bindings ────────────────────────────────────────────────────────
/// Returns the crate version string (e.g. `"0.4.1"`).
///
/// This is the version of the `brainwires-wasm` package as declared in `Cargo.toml`.
///
/// # JS Example
///
/// ```js
/// const v = version();
/// console.log(`Brainwires WASM v${v}`);
/// ```
/// Validates and normalizes a JSON-encoded message.
///
/// Parses the input JSON string into a [`brainwires_core::Message`] struct and
/// re-serializes it back to JSON. This ensures the message conforms to the expected
/// schema and strips any unknown fields.
///
/// # Parameters
///
/// - `json` — A JSON string representing a single message object.
///
/// # Returns
///
/// The normalized JSON string on success, or an error string describing the
/// validation failure.
///
/// # JS Example
///
/// ```js
/// try {
/// const normalized = validate_message('{"role":"user","content":"Hello"}');
/// console.log(normalized);
/// } catch (e) {
/// console.error("Invalid message:", e);
/// }
/// ```
/// Validates and normalizes a JSON-encoded tool definition.
///
/// Parses the input JSON string into a [`brainwires_core::Tool`] struct and
/// re-serializes it back to JSON. Use this to verify that a tool definition
/// is well-formed before registering it.
///
/// # Parameters
///
/// - `json` — A JSON string representing a tool definition object.
///
/// # Returns
///
/// The normalized JSON string on success, or an error string describing the
/// validation failure.
///
/// # JS Example
///
/// ```js
/// const toolJson = JSON.stringify({
/// name: "calculator",
/// description: "Performs arithmetic",
/// input_schema: { type: "object", properties: { expr: { type: "string" } } }
/// });
/// const normalized = validate_tool(toolJson);
/// ```
/// Serializes a conversation history to the stateless protocol format.
///
/// Takes a JSON array of [`brainwires_core::Message`] objects and converts them
/// into the stateless history format used by AI provider APIs. This is useful
/// when you need to send a full conversation context in a single API request.
///
/// # Parameters
///
/// - `messages_json` — A JSON string containing an array of message objects.
///
/// # Returns
///
/// A JSON string in the stateless history format on success, or an error string
/// if the input is malformed.
///
/// # JS Example
///
/// ```js
/// const messages = JSON.stringify([
/// { role: "user", content: "What is 2+2?" },
/// { role: "assistant", content: "4" }
/// ]);
/// const history = serialize_history(messages);
/// // Use `history` in an API request body
/// ```