plexus-core 0.5.3

Core infrastructure for Plexus RPC: Activation trait, DynamicHub, schemas
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
# Plugin Architecture Guide

## Philosophy

**Plugins are RPC adapters over standalone systems.**

Every plugin follows a two-layer architecture:

1. **Core System Layer**: A standalone struct/module that implements the actual functionality
2. **RPC Adapter Layer**: A thin wrapper that exposes the core system over JSON-RPC

This separation ensures:
- Core functionality can be used programmatically without RPC overhead
- Business logic is testable independent of RPC concerns
- Multiple RPC interfaces could expose the same core system differently

## File Structure

### Simple Plugin (Single File)

For simple plugins where the core system fits in one file:

```
src/plugins/health/
├── mod.rs          # Public exports
├── types.rs        # Domain types (HealthStatus, etc.)
└── plugin.rs       # Core system + RPC adapter
```

**`types.rs`**: Domain-specific types
```rust
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthStatus {
    pub status: String,
    pub uptime_seconds: u64,
    pub timestamp: i64,
}

// Implement PluginStreamItem to convert to HubStreamItem
impl PluginStreamItem for HealthStatus {
    fn into_hub_item(self, path: PluginPath) -> HubStreamItem {
        HubStreamItem::Data {
            path,
            content_type: std::any::type_name::<Self>().to_string(),
            data: serde_json::to_value(self).unwrap(),
        }
    }
}
```

**`plugin.rs`**: Core system implementation
```rust
/// Core system - can be used programmatically
pub struct HealthPlugin {
    start_time: Instant,
}

impl HealthPlugin {
    pub fn new() -> Self {
        Self { start_time: Instant::now() }
    }

    /// Business logic method - returns tightly-typed stream
    async fn check_stream(&self)
        -> Pin<Box<dyn Stream<Item = HealthStatus> + Send + 'static>>
    {
        let uptime = self.start_time.elapsed().as_secs();
        Box::pin(stream! {
            yield HealthStatus {
                status: "healthy".to_string(),
                uptime_seconds: uptime,
                timestamp: chrono::Utc::now().timestamp(),
            };
        })
    }
}

/// RPC adapter trait - defines the JSON-RPC interface
#[rpc(server, namespace = "health")]
pub trait HealthRpc {
    #[subscription(
        name = "check",
        unsubscribe = "unsubscribe_check",
        item = serde_json::Value
    )]
    async fn check(&self) -> SubscriptionResult;
}

/// RPC adapter implementation - bridges core system to RPC
#[async_trait]
impl HealthRpcServer for HealthPlugin {
    async fn check(&self, pending: PendingSubscriptionSink) -> SubscriptionResult {
        let stream = self.check_stream().await;
        let path = PluginPath::root("health");
        stream.into_subscription(pending, path).await
    }
}
```

**`mod.rs`**: Public exports
```rust
mod plugin;
mod types;

pub use plugin::{HealthPlugin, HealthRpcServer};
pub use types::HealthStatus;
```

### Complex Plugin (Multi-Module)

For complex plugins where the core system needs multiple files:

```
src/plugins/bash/
├── mod.rs              # Public exports
├── types.rs            # Domain types (BashOutput, BashError, etc.)
├── plugin.rs           # RPC adapter only
└── executor/           # Core system submodule
    ├── mod.rs          # Executor public exports
    ├── process.rs      # Process management
    ├── stream.rs       # Output streaming
    └── session.rs      # Session state
```

**Pattern**: Core system lives in its own submodule, RPC adapter stays thin.

**`executor/mod.rs`**: Core system exports
```rust
mod process;
mod session;
mod stream;

pub use process::BashProcess;
pub use session::BashSession;
pub use stream::OutputStream;

/// Core system - standalone bash executor
pub struct BashExecutor {
    sessions: HashMap<String, BashSession>,
}

impl BashExecutor {
    pub fn new() -> Self { /* ... */ }

    /// Business logic - programmatic API
    pub async fn execute(&mut self, cmd: &str) -> OutputStream { /* ... */ }
    pub async fn create_session(&mut self) -> String { /* ... */ }
    pub async fn kill_session(&mut self, id: &str) -> Result<()> { /* ... */ }
}
```

**`plugin.rs`**: RPC adapter wraps core system
```rust
use super::executor::BashExecutor;
use super::types::*;

/// RPC adapter trait
#[rpc(server, namespace = "bash")]
pub trait BashRpc {
    #[subscription(name = "execute", ...)]
    async fn execute(&self, command: String) -> SubscriptionResult;
}

/// RPC adapter struct - wraps core system
pub struct BashPlugin {
    executor: Arc<Mutex<BashExecutor>>,
}

impl BashPlugin {
    pub fn new() -> Self {
        Self {
            executor: Arc::new(Mutex::new(BashExecutor::new())),
        }
    }

    /// Thin wrapper - delegates to core system
    async fn execute_stream(&self, command: String)
        -> Pin<Box<dyn Stream<Item = BashOutput> + Send + 'static>>
    {
        let executor = self.executor.clone();
        Box::pin(stream! {
            let mut exec = executor.lock().await;
            let mut output_stream = exec.execute(&command).await;

            while let Some(output) = output_stream.next().await {
                yield output;
            }
        })
    }
}

/// RPC adapter implementation
#[async_trait]
impl BashRpcServer for BashPlugin {
    async fn execute(&self, pending: PendingSubscriptionSink, command: String)
        -> SubscriptionResult
    {
        let stream = self.execute_stream(command).await;
        let path = PluginPath::root("bash");
        stream.into_subscription(pending, path).await
    }
}
```

## Step-by-Step Plugin Creation

### 1. Design Your Domain Types

Create `types.rs` with:
- Input/output types for your system
- Implement `PluginStreamItem` for any type you'll stream

```rust
use crate::plugin_system::types::PluginStreamItem;
use crate::hub::{path::PluginPath, types::HubStreamItem};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MyOutput {
    pub data: String,
}

impl PluginStreamItem for MyOutput {
    fn into_hub_item(self, path: PluginPath) -> HubStreamItem {
        HubStreamItem::Data {
            path,
            content_type: std::any::type_name::<Self>().to_string(),
            data: serde_json::to_value(self).unwrap(),
        }
    }
}
```

### 2. Implement Core System

**Simple case**: Add to `plugin.rs`
**Complex case**: Create `executor/` or `core/` submodule

Core system should:
- Have no RPC dependencies
- Return native Rust types
- Be independently testable
- Use standard async Rust patterns

```rust
pub struct MySystem {
    // Internal state
}

impl MySystem {
    pub fn new() -> Self { /* ... */ }

    /// Business logic - pure Rust
    pub async fn do_work(&self, input: &str)
        -> Pin<Box<dyn Stream<Item = MyOutput> + Send + 'static>>
    {
        Box::pin(stream! {
            // Your logic here
            yield MyOutput { data: input.to_string() };
        })
    }
}
```

### 3. Create RPC Adapter

In `plugin.rs`, define the RPC interface:

```rust
use jsonrpsee::proc_macros::rpc;
use crate::plugin_system::conversion::SubscriptionResult;

#[rpc(server, namespace = "myplugin")]
pub trait MyRpc {
    #[subscription(
        name = "do_work",
        unsubscribe = "unsubscribe_do_work",
        item = serde_json::Value
    )]
    async fn do_work(&self, input: String) -> SubscriptionResult;
}
```

### 4. Implement RPC Adapter

Bridge your core system to the RPC trait:

```rust
use async_trait::async_trait;
use jsonrpsee::PendingSubscriptionSink;
use crate::{
    hub::path::PluginPath,
    plugin_system::conversion::IntoSubscription,
};

pub struct MyPlugin {
    system: MySystem,
}

impl MyPlugin {
    pub fn new() -> Self {
        Self { system: MySystem::new() }
    }
}

#[async_trait]
impl MyRpcServer for MyPlugin {
    async fn do_work(&self, pending: PendingSubscriptionSink, input: String)
        -> SubscriptionResult
    {
        let stream = self.system.do_work(&input).await;
        let path = PluginPath::root("myplugin");
        stream.into_subscription(pending, path).await
    }
}
```

### 5. Export from Module

In `mod.rs`:

```rust
mod plugin;
mod types;

// For complex plugins:
// mod executor;

pub use plugin::{MyPlugin, MyRpcServer};
pub use types::MyOutput;
```

### 6. Register in Main

In `src/main.rs`:

```rust
use cognition_pipeline::plugins::myplugin::MyPlugin;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let mut module = RpcModule::new(());

    let my_plugin = MyPlugin::new();
    module.merge(my_plugin.into_rpc())?;

    // Start server...
}
```

## Key Patterns

### Always Return Streams

Even for single-value responses, use streams for consistency:

```rust
async fn single_value(&self) -> Pin<Box<dyn Stream<Item = MyType> + Send + 'static>> {
    Box::pin(stream! {
        yield MyType { /* ... */ };
    })
}
```

### Path Tracking for Nested Calls

If your plugin calls other plugins:

```rust
async fn nested_call(&self, pending: PendingSubscriptionSink)
    -> SubscriptionResult
{
    let stream = self.inner_stream().await;
    let path = PluginPath::root("outer").extend("inner");
    stream.into_subscription(pending, path).await
}
```

### Use HubStreamItem Variants

Your streams can yield different event types:

```rust
impl PluginStreamItem for MyProgress {
    fn into_hub_item(self, path: PluginPath) -> HubStreamItem {
        HubStreamItem::Progress {
            path,
            message: self.message,
            percentage: Some(self.percent),
        }
    }
}

impl PluginStreamItem for MyError {
    fn into_hub_item(self, path: PluginPath) -> HubStreamItem {
        HubStreamItem::Error {
            path,
            error: self.message,
            recoverable: self.can_retry,
        }
    }
}
```

## Testing Strategy

### Test Core System Independently

```rust
#[tokio::test]
async fn test_core_system() {
    let system = MySystem::new();
    let mut stream = system.do_work("test").await;

    let output = stream.next().await.unwrap();
    assert_eq!(output.data, "test");
}
```

### Test RPC Integration

```rust
#[tokio::test]
async fn test_rpc_adapter() {
    let plugin = MyPlugin::new();
    let module = plugin.into_rpc();
    // Use jsonrpsee test client
}
```

## Common Mistakes to Avoid

1. **Don't put RPC types in core system** - Keep jsonrpsee types only in the adapter layer
2. **Don't forget PluginStreamItem** - All stream items must implement this trait
3. **Don't block the executor** - Use async/await, never blocking calls
4. **Don't forget path tracking** - Always create a PluginPath for your streams
5. **Don't skip error handling** - Use HubStreamItem::Error for recoverable errors

## Decision Tree: Simple vs Complex

**Use simple structure (plugin.rs only) when:**
- Core system is < 200 lines
- Single responsibility (health check, ping, etc.)
- Minimal state management

**Use complex structure (executor/ submodule) when:**
- Core system is > 200 lines
- Multiple related components (process, session, stream)
- Needs internal module organization
- Could be extracted as a separate crate

## Next Steps

After creating your plugin:
1. Test core system independently
2. Test RPC integration
3. Add to `src/plugins/mod.rs`
4. Register in `src/main.rs`
5. Create test client in `bin/test-<plugin>/`
6. Document wire format and example usage