eli 0.3.2

Ease Lives Instantly — hook-first AI agent framework with multi-channel support
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
//! Hook-first Eli framework runtime.

use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;

use serde_json::Value;
use tokio::sync::RwLock;

use crate::envelope::{ValueExt, unpack_batch_vec};
use crate::hooks::{ChannelHook, EliHookSpec, HookRuntime, TapeStoreKind};
use crate::types::{
    Envelope, MessageHandler, OutboundChannelRouter, PromptValue, State, TurnResult,
};

// ---------------------------------------------------------------------------
// PluginStatus
// ---------------------------------------------------------------------------

/// Records whether a plugin loaded successfully.
#[derive(Debug, Clone)]
pub struct PluginStatus {
    pub is_success: bool,
    pub detail: Option<String>,
}

// ---------------------------------------------------------------------------
// EliFramework
// ---------------------------------------------------------------------------

/// Minimal framework core. Everything grows from hook plugins.
pub struct EliFramework {
    /// Working directory / project root.
    pub workspace: RwLock<PathBuf>,
    /// The hook runtime that dispatches to registered plugins.
    hook_runtime: RwLock<HookRuntime>,
    /// Status of each loaded plugin.
    plugin_status: RwLock<HashMap<String, PluginStatus>>,
    /// Optional router for outbound message dispatch.
    outbound_router: RwLock<Option<Arc<dyn OutboundChannelRouter>>>,
}

impl EliFramework {
    /// Create a new framework instance rooted at the current working directory.
    pub fn new() -> Self {
        Self {
            workspace: RwLock::new(std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."))),
            hook_runtime: RwLock::new(HookRuntime::new(Vec::new())),
            plugin_status: RwLock::new(HashMap::new()),
            outbound_router: RwLock::new(None),
        }
    }

    /// Create a framework with a given workspace path.
    pub fn with_workspace(workspace: PathBuf) -> Self {
        Self {
            workspace: RwLock::new(workspace),
            hook_runtime: RwLock::new(HookRuntime::new(Vec::new())),
            plugin_status: RwLock::new(HashMap::new()),
            outbound_router: RwLock::new(None),
        }
    }

    // -- Plugin registration ------------------------------------------------

    /// Register a plugin with the framework.
    pub async fn register_plugin(&self, name: &str, plugin: Arc<dyn EliHookSpec>) {
        let mut rt = self.hook_runtime.write().await;
        rt.register(plugin);
        let mut status = self.plugin_status.write().await;
        status.insert(
            name.to_string(),
            PluginStatus {
                is_success: true,
                detail: None,
            },
        );
    }

    /// Record a plugin load failure.
    pub async fn record_plugin_failure(&self, name: &str, detail: String) {
        let mut status = self.plugin_status.write().await;
        status.insert(
            name.to_string(),
            PluginStatus {
                is_success: false,
                detail: Some(detail),
            },
        );
    }

    /// Load hooks from a list of plugins. This is the Rust equivalent of
    /// the Python `load_hooks` which discovers plugins via entry points.
    /// In Rust, callers explicitly provide the plugin list.
    pub async fn load_hooks(&self, plugins: Vec<(String, Arc<dyn EliHookSpec>)>) {
        for (name, plugin) in plugins {
            self.register_plugin(&name, plugin).await;
        }
    }

    // -- Main orchestration loop --------------------------------------------

    /// Run one inbound message through all hooks and return the turn result.
    pub async fn process_inbound(&self, mut inbound: Envelope) -> anyhow::Result<TurnResult> {
        // Strip internal-only context keys from inbound to prevent injection.
        if let Some(ctx) = inbound.get_mut("context").and_then(|v| v.as_object_mut()) {
            ctx.remove("_eli_cleanup_only");
        }

        let rt = self.hook_runtime.read().await;
        let workspace = self.workspace.read().await.clone();

        // 0. Classify inbound — Greet messages short-circuit the pipeline
        if let Some(crate::smart_router::RouteDecision::Greet(reply)) =
            rt.call_classify_inbound(&inbound)
        {
            let session_id = Self::default_session_id(&inbound);
            let channel = inbound
                .get("channel")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_owned();
            let chat_id = inbound
                .get("chat_id")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_owned();
            let output_channel = inbound
                .get("output_channel")
                .and_then(|v| v.as_str())
                .unwrap_or(&channel)
                .to_owned();
            let outbound = serde_json::json!({
                "content": reply,
                "session_id": session_id,
                "channel": channel,
                "chat_id": chat_id,
                "output_channel": output_channel,
            });
            return Ok(TurnResult {
                session_id,
                prompt: PromptValue::Text(String::new()),
                model_output: reply.clone(),
                outbounds: vec![outbound],
            });
        }

        // 1. Resolve session
        let session_id = match rt.call_resolve_session(&inbound).await {
            Ok(Some(id)) => id,
            Ok(None) => Self::default_session_id(&inbound),
            Err(e) => {
                tracing::warn!(error = %e, "resolve_session failed, using default session id");
                Self::default_session_id(&inbound)
            }
        };

        // Inject session_id into the inbound envelope if it's an object
        let mut inbound = inbound;
        if let Some(obj) = inbound.as_object_mut() {
            obj.entry("session_id")
                .or_insert_with(|| Value::String(session_id.clone()));
        }

        // 2. Build state
        let mut state: State = HashMap::new();
        state.insert(
            "_runtime_workspace".to_string(),
            Value::String(workspace.to_string_lossy().to_string()),
        );

        let hook_states = match rt.call_load_state(&inbound, &session_id).await {
            Ok(states) => states,
            Err(e) => {
                tracing::warn!(error = %e, session_id = %session_id, "load_state failed, using empty state");
                Vec::new()
            }
        };
        // Iterate last-registered-first; `or_insert` keeps the first value seen
        // per key, so later-registered plugins take priority.
        for hs in hook_states.into_iter().rev().flatten() {
            for (k, v) in hs {
                state.entry(k).or_insert(v);
            }
        }

        // 3. Build prompt
        let prompt = match rt
            .call_build_user_prompt(&inbound, &session_id, &state)
            .await
        {
            Some(p) => p,
            None => PromptValue::Text(inbound.content_text()),
        };

        // 4. Run model
        let model_output: String;
        let run_result = rt.call_run_model(&prompt, &session_id, &state).await;
        match run_result {
            Ok(Some(output)) => {
                model_output = output;
            }
            Ok(None) => {
                let fallback_err = anyhow::anyhow!("no model skill returned output");
                rt.notify_error("run_model:fallback", &fallback_err, Some(&inbound))
                    .await;
                model_output = "[Error: no model plugin available]".to_owned();
            }
            Err(e) => {
                let err_msg = format!("{e}");
                let anyhow_err = anyhow::anyhow!("run_model hook failed: {}", err_msg);
                rt.notify_error("run_model", &anyhow_err, Some(&inbound))
                    .await;
                model_output = format!("[Error: {err_msg}]");
            }
        }

        // 5. Save state (always runs, even if model failed)
        rt.call_save_state(&session_id, &state, &inbound, &model_output)
            .await;

        // 6. Collect and dispatch outbounds
        let outbounds = self
            .collect_outbounds(&rt, &inbound, &session_id, &state, &model_output)
            .await;

        for outbound in &outbounds {
            rt.call_dispatch_outbound(outbound).await;
        }

        Ok(TurnResult {
            session_id,
            prompt,
            model_output,
            outbounds,
        })
    }

    // -- Diagnostics --------------------------------------------------------

    /// Return hook implementation summary for diagnostics.
    pub async fn hook_report(&self) -> HashMap<String, Vec<String>> {
        let rt = self.hook_runtime.read().await;
        rt.hook_report()
    }

    /// Return the plugin status map.
    pub async fn plugin_status(&self) -> HashMap<String, PluginStatus> {
        self.plugin_status.read().await.clone()
    }

    // -- Outbound router ----------------------------------------------------

    /// Bind an outbound channel router.
    pub async fn bind_outbound_router(&self, router: Arc<dyn OutboundChannelRouter>) {
        let mut r = self.outbound_router.write().await;
        *r = Some(router);
    }

    /// Dispatch a message through the bound outbound router.
    pub async fn dispatch_via_router(&self, message: &Envelope) -> bool {
        let router = self.outbound_router.read().await;
        match router.as_ref() {
            Some(r) => r.dispatch(message.clone()).await,
            None => false,
        }
    }

    /// Signal a session to quit through the bound outbound router.
    pub async fn quit_via_router(&self, session_id: &str) {
        let router = self.outbound_router.read().await;
        if let Some(r) = router.as_ref() {
            r.quit(session_id).await;
        }
    }

    // -- Channel and tape store accessors -----------------------------------

    /// Collect channels from all plugins, deduplicating by name.
    pub async fn get_channels(
        &self,
        message_handler: MessageHandler,
    ) -> HashMap<String, Box<dyn ChannelHook>> {
        let rt = self.hook_runtime.read().await;
        let all_channels = rt.call_provide_channels(message_handler);
        let mut map: HashMap<String, Box<dyn ChannelHook>> = HashMap::new();
        for ch in all_channels {
            let name = ch.name().to_string();
            map.entry(name).or_insert(ch);
        }
        map
    }

    /// Get the first provided tape store.
    pub async fn get_tape_store(&self) -> Option<TapeStoreKind> {
        let rt = self.hook_runtime.read().await;
        rt.call_provide_tape_store()
    }

    /// Build the system prompt via the hook chain.
    pub async fn get_system_prompt(&self, prompt: &PromptValue, state: &State) -> String {
        let rt = self.hook_runtime.read().await;
        rt.call_build_system_prompt(&prompt.as_text(), state)
            .unwrap_or_default()
    }

    // -- Internal helpers ---------------------------------------------------

    /// Compute a default session ID from the envelope fields.
    fn default_session_id(message: &Envelope) -> String {
        // Try session_id field first
        if let Some(obj) = message.as_object()
            && let Some(Value::String(sid)) = obj.get("session_id")
        {
            return sid.clone();
        }
        let channel = message.field_str("channel", "default");
        let chat_id = message.field_str("chat_id", "default");
        format!("{channel}:{chat_id}")
    }

    /// Collect outbound envelopes from render hooks, falling back to a default
    /// envelope containing the model output.
    async fn collect_outbounds(
        &self,
        rt: &HookRuntime,
        message: &Envelope,
        session_id: &str,
        state: &State,
        model_output: &str,
    ) -> Vec<Envelope> {
        let batches = rt
            .call_render_outbound(message, session_id, state, model_output)
            .await;
        if !batches.is_empty() {
            return unpack_batch_vec(batches);
        }

        // Fallback: build a default outbound envelope
        let mut fallback = serde_json::Map::new();
        fallback.insert(
            "content".to_string(),
            Value::String(model_output.to_string()),
        );
        fallback.insert(
            "session_id".to_string(),
            Value::String(session_id.to_string()),
        );

        if let Some(channel) = message.field("channel", None) {
            fallback.insert("channel".to_string(), channel);
        }
        if let Some(chat_id) = message.field("chat_id", None) {
            fallback.insert("chat_id".to_string(), chat_id);
        }

        vec![Value::Object(fallback)]
    }
}

impl Default for EliFramework {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::hooks::{EliHookSpec, HookError};
    use async_trait::async_trait;
    use serde_json::json;
    use std::sync::Arc;

    struct SessionPlugin;

    #[async_trait]
    impl EliHookSpec for SessionPlugin {
        fn plugin_name(&self) -> &str {
            "session-plugin"
        }

        async fn resolve_session(&self, message: &Envelope) -> Result<Option<String>, HookError> {
            Ok(message
                .as_object()
                .and_then(|o| o.get("session_id"))
                .and_then(|v| v.as_str())
                .map(|s| s.to_owned()))
        }
    }

    struct PromptPlugin {
        fragment: String,
    }

    #[async_trait]
    impl EliHookSpec for PromptPlugin {
        fn plugin_name(&self) -> &str {
            "prompt-plugin"
        }

        fn build_system_prompt(&self, _prompt_text: &str, _state: &State) -> Option<String> {
            Some(self.fragment.clone())
        }
    }

    struct ModelPlugin;

    #[async_trait]
    impl EliHookSpec for ModelPlugin {
        fn plugin_name(&self) -> &str {
            "model-plugin"
        }

        async fn run_model(
            &self,
            prompt: &PromptValue,
            _session_id: &str,
            _state: &State,
        ) -> Result<Option<String>, HookError> {
            Ok(Some(format!("echo: {}", prompt.as_text())))
        }
    }

    // -- Creation tests -------------------------------------------------------

    #[tokio::test]
    async fn test_framework_creation_default() {
        let fw = EliFramework::new();
        let ws = fw.workspace.read().await;
        assert!(ws.is_absolute() || *ws == std::path::PathBuf::from("."));
    }

    #[tokio::test]
    async fn test_framework_with_workspace() {
        let fw = EliFramework::with_workspace("/tmp/test".into());
        let ws = fw.workspace.read().await;
        assert_eq!(*ws, std::path::PathBuf::from("/tmp/test"));
    }

    // -- Plugin registration --------------------------------------------------

    #[tokio::test]
    async fn test_register_plugin_records_status() {
        let fw = EliFramework::new();
        fw.register_plugin("test", Arc::new(SessionPlugin)).await;
        let status = fw.plugin_status().await;
        assert!(status.contains_key("test"));
        assert!(status["test"].is_success);
    }

    #[tokio::test]
    async fn test_record_plugin_failure() {
        let fw = EliFramework::new();
        fw.record_plugin_failure("bad", "failed to load".into())
            .await;
        let status = fw.plugin_status().await;
        assert!(!status["bad"].is_success);
        assert_eq!(status["bad"].detail.as_deref(), Some("failed to load"));
    }

    #[tokio::test]
    async fn test_load_hooks_registers_multiple_plugins() {
        let fw = EliFramework::new();
        fw.load_hooks(vec![
            (
                "session".into(),
                Arc::new(SessionPlugin) as Arc<dyn EliHookSpec>,
            ),
            (
                "model".into(),
                Arc::new(ModelPlugin) as Arc<dyn EliHookSpec>,
            ),
        ])
        .await;
        let status = fw.plugin_status().await;
        assert_eq!(status.len(), 2);
        assert!(status["session"].is_success);
        assert!(status["model"].is_success);
    }

    // -- get_system_prompt ----------------------------------------------------

    #[tokio::test]
    async fn test_get_system_prompt_returns_first_result() {
        let fw = EliFramework::new();
        fw.register_plugin(
            "low",
            Arc::new(PromptPlugin {
                fragment: "low".into(),
            }),
        )
        .await;
        fw.register_plugin(
            "high",
            Arc::new(PromptPlugin {
                fragment: "high".into(),
            }),
        )
        .await;
        let prompt = PromptValue::Text("hello".into());
        let state = State::new();
        let result = fw.get_system_prompt(&prompt, &state).await;
        // Last-registered (high) wins
        assert_eq!(result, "high");
    }

    // -- process_inbound (full pipeline) --------------------------------------

    #[tokio::test]
    async fn test_process_inbound_full_pipeline() {
        let fw = EliFramework::new();
        fw.register_plugin("session", Arc::new(SessionPlugin) as Arc<dyn EliHookSpec>)
            .await;
        fw.register_plugin("model", Arc::new(ModelPlugin) as Arc<dyn EliHookSpec>)
            .await;

        let msg = json!({"content": "ping", "session_id": "test-session"});
        let result = fw.process_inbound(msg).await.unwrap();
        assert_eq!(result.session_id, "test-session");
        assert!(result.model_output.starts_with("echo: "));
        assert!(!result.outbounds.is_empty());
    }

    #[tokio::test]
    async fn test_process_inbound_default_session_id() {
        let fw = EliFramework::new();
        fw.register_plugin("model", Arc::new(ModelPlugin) as Arc<dyn EliHookSpec>)
            .await;

        let msg = json!({"content": "hello", "channel": "telegram", "chat_id": "42"});
        let result = fw.process_inbound(msg).await.unwrap();
        assert_eq!(result.session_id, "telegram:42");
    }

    // -- hook_report ----------------------------------------------------------

    #[tokio::test]
    async fn test_hook_report_reflects_registered_plugins() {
        let fw = EliFramework::new();
        fw.register_plugin("session", Arc::new(SessionPlugin) as Arc<dyn EliHookSpec>)
            .await;
        let report = fw.hook_report().await;
        assert!(report.contains_key("resolve_session"));
        assert!(report["resolve_session"].contains(&"session-plugin".to_owned()));
    }
}