exomonad-core 0.1.0

ExoMonad core: effect system, WASM hosting, MCP server, built-in handlers, shared types
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
//! Popup service for WASM host functions.
//!
//! Shows interactive popup choice lists via Zellij CLI pipes and returns user selection.
//!
//! Uses the Zellij CLI pipe mechanism:
//! 1. `zellij pipe --plugin file:plugin.wasm --name exomonad:popup -- "request_id|title|item1,item2,item3"`
//! 2. Plugin receives via `pipe()` with `PipeSource::Cli(pipe_id)` and payload
//! 3. Plugin blocks CLI, shows popup UI
//! 4. On submit, plugin calls `cli_pipe_output(&pipe_id, "request_id:selected_item")`
//! 5. CLI receives response on stdout

use crate::ui_protocol::transport;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::io::Read;
use std::process::{Command, Stdio};
use std::sync::mpsc;
use std::time::Duration;

/// Timeout for waiting for popup response (5 minutes).
const POPUP_TIMEOUT: Duration = Duration::from_secs(300);

/// Default path to the Zellij plugin
const DEFAULT_PLUGIN_PATH: &str = "~/.config/zellij/plugins/exomonad-plugin.wasm";

/// Get the plugin path, checking it exists.
/// Override with EXOMONAD_PLUGIN_PATH env var.
///
/// NOTE: Returns expanded path (file:/Users/...) because Zellij CLI does NOT expand tilde.
/// This creates a separate plugin instance from KDL-loaded plugins (which use file:~/.config/...),
/// but that's intentional for ephemeral popup use case.
fn get_plugin_path() -> Result<String> {
    let path =
        std::env::var("EXOMONAD_PLUGIN_PATH").unwrap_or_else(|_| DEFAULT_PLUGIN_PATH.to_string());

    // Expand ~ to home directory (required for CLI commands - Zellij CLI doesn't expand ~)
    let expanded = if let Some(rest) = path.strip_prefix("~/") {
        if let Some(home) = dirs::home_dir() {
            home.join(rest).to_string_lossy().to_string()
        } else {
            path.clone()
        }
    } else {
        path.clone()
    };

    if !std::path::Path::new(&expanded).exists() {
        anyhow::bail!(
            "Zellij plugin not found at '{}'. Run 'just install-all' to install it.",
            expanded
        );
    }

    Ok(format!("file:{}", expanded))
}

// ============================================================================
// Input/Output types
// ============================================================================

/// Input for show_popup (matches Haskell PopupRequest).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PopupInput {
    /// Title displayed at the top of the popup.
    pub title: String,
    /// List of UI components.
    pub components: Vec<serde_json::Value>,
}

/// Output from show_popup (matches Haskell PopupResponse).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PopupOutput {
    /// Button clicked: "submit" or "cancel"
    pub button: String,
    /// Component values as JSON object
    pub values: serde_json::Value,
}

// ============================================================================
// Popup Service
// ============================================================================

/// Popup service for showing interactive forms.
pub struct PopupService {
    /// Zellij session name (from config, not detected).
    zellij_session: String,
}

impl PopupService {
    /// Create a new popup service with the specified Zellij session.
    pub fn new(zellij_session: String) -> Self {
        Self { zellij_session }
    }

    /// Show a popup and wait for user response.
    ///
    /// Uses Zellij CLI pipe mechanism with simple payload format:
    /// 1. `zellij pipe --plugin file:plugin.wasm --name exomonad:popup -- "request_id|title|item1,item2,item3"`
    /// 2. Plugin receives via `pipe()` with `PipeSource::Cli(pipe_id)` and payload
    /// 3. Plugin blocks CLI, shows popup UI, user interacts
    /// 4. On submit, plugin calls `cli_pipe_output(&pipe_id, "request_id:selected_item")`
    /// 5. This CLI command receives response on stdout and exits
    #[tracing::instrument(skip(self))]
    pub fn show_popup(&self, input: &PopupInput) -> Result<PopupOutput> {
        tracing::info!(title = %input.title, components = input.components.len(), "Showing popup");

        // Generate unique request ID
        let request_id = uuid::Uuid::new_v4().to_string();

        // Extract choice items from components
        let items = extract_choice_items(&input.components);

        if items.is_empty() {
            anyhow::bail!("Popup must have at least one choice item");
        }

        // Verify plugin exists
        let plugin_path = get_plugin_path()?;
        let session = &self.zellij_session;

        // Build payload: "request_id|title|item1,item2,item3"
        // Sanitize delimiters to avoid parsing issues
        let safe_title = sanitize_payload_field(&input.title);
        let safe_items: Vec<String> = items.iter().map(|s| sanitize_payload_field(s)).collect();
        let payload = format!("{}|{}|{}", request_id, safe_title, safe_items.join(","));

        tracing::debug!(
            request_id = %request_id,
            plugin = %plugin_path,
            session = %session,
            items = ?items,
            "Sending popup request via zellij pipe --plugin"
        );

        // Use zellij pipe --plugin to send request and receive response on stdout.
        // The plugin will call cli_pipe_output() to send the response back.
        // We must specify --session explicitly because the MCP server process may not
        // have ZELLIJ_SESSION_NAME in its environment.
        let mut child = Command::new("zellij")
            .arg("--session")
            .arg(session)
            .arg("pipe")
            .arg("--plugin")
            .arg(&plugin_path)
            .arg("--name")
            .arg(transport::POPUP_PIPE)
            .arg("--")
            .arg(&payload)
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .context("Failed to spawn zellij pipe command")?;

        let (tx, rx) = mpsc::channel();
        let mut stdout = child.stdout.take().expect("stdout was piped");
        let mut stderr = child.stderr.take().expect("stderr was piped");

        // Spawn thread to read stdout (blocks until plugin sends response)
        let stderr_handle = std::thread::spawn(move || {
            let mut buffer = Vec::new();
            let _ = stderr.read_to_end(&mut buffer);
            buffer
        });

        std::thread::spawn(move || {
            let mut buffer = Vec::new();
            let result = stdout.read_to_end(&mut buffer);
            let _ = tx.send((result, buffer));
        });

        tracing::debug!(request_id = %request_id, "Waiting for popup response (timeout: {:?})", POPUP_TIMEOUT);

        // Wait for response with timeout
        let response_str = match rx.recv_timeout(POPUP_TIMEOUT) {
            Ok((Ok(_), buffer)) => {
                let _ = child.wait();
                // Capture stderr for debugging
                if let Ok(stderr_buf) = stderr_handle.join() {
                    if !stderr_buf.is_empty() {
                        let stderr_str = String::from_utf8_lossy(&stderr_buf);
                        tracing::warn!(request_id = %request_id, stderr = %stderr_str, "zellij pipe stderr");
                    }
                }
                String::from_utf8(buffer).context("Invalid UTF-8 in popup response")?
            }
            Ok((Err(e), _)) => {
                let _ = child.kill();
                let _ = child.wait();
                // Capture stderr for debugging
                if let Ok(stderr_buf) = stderr_handle.join() {
                    if !stderr_buf.is_empty() {
                        let stderr_str = String::from_utf8_lossy(&stderr_buf);
                        tracing::error!(request_id = %request_id, stderr = %stderr_str, "zellij pipe failed");
                    }
                }
                return Err(anyhow::Error::from(e).context("Failed to read popup response"));
            }
            Err(mpsc::RecvTimeoutError::Timeout) => {
                tracing::warn!(request_id = %request_id, "Popup timed out after {:?}", POPUP_TIMEOUT);
                let _ = child.kill();
                let _ = child.wait();
                // Join stderr thread to avoid orphaned thread
                let _ = stderr_handle.join();
                anyhow::bail!("Popup timed out after {} seconds", POPUP_TIMEOUT.as_secs());
            }
            Err(mpsc::RecvTimeoutError::Disconnected) => {
                let _ = child.kill();
                let _ = child.wait();
                // Join stderr thread to avoid orphaned thread
                let _ = stderr_handle.join();
                anyhow::bail!("Popup response channel disconnected unexpectedly");
            }
        };

        tracing::info!(
            request_id = %request_id,
            response = %response_str,
            response_len = response_str.len(),
            response_bytes = ?response_str.as_bytes(),
            "Received popup response"
        );

        // Parse response: "{request_id}:{selected_item}" or "{request_id}:CANCELLED"
        let response_str = response_str.trim();

        if response_str.is_empty() {
            anyhow::bail!(
                "Empty response from zellij pipe - plugin may have failed to load or respond"
            );
        }

        let (resp_request_id, selection) = response_str.split_once(':').context(format!(
            "Invalid popup response format: expected 'request_id:selection', got: {:?}",
            response_str
        ))?;

        // Verify request_id matches
        if resp_request_id != request_id {
            tracing::warn!(
                expected = %request_id,
                received = %resp_request_id,
                "Request ID mismatch in popup response"
            );
        }

        let (button, values) = if selection == "CANCELLED" {
            ("cancel".to_string(), serde_json::json!({}))
        } else {
            (
                "submit".to_string(),
                serde_json::json!({"selected": selection}),
            )
        };

        tracing::info!(
            request_id = %request_id,
            button = %button,
            "Popup completed"
        );

        Ok(PopupOutput { button, values })
    }
}

/// Sanitize a string for use in the payload protocol.
///
/// Replaces delimiter characters (`|` and `,`) with safe alternatives
/// to prevent parsing issues in the plugin.
fn sanitize_payload_field(s: &str) -> String {
    s.replace('|', "-").replace(',', ";")
}

/// Extract choice items from popup components.
///
/// Looks for Choice components and extracts their options.
/// Falls back to text content from Text components if no Choice found.
fn extract_choice_items(components: &[serde_json::Value]) -> Vec<String> {
    for component in components {
        // Look for Choice component with options
        if component.get("type").and_then(|t| t.as_str()) == Some("choice") {
            if let Some(options) = component.get("options").and_then(|o| o.as_array()) {
                return options
                    .iter()
                    .filter_map(|v| v.as_str().map(String::from))
                    .collect();
            }
        }
    }

    // Fallback: collect text content from Text components as items
    components
        .iter()
        .filter_map(|c| {
            if c.get("type").and_then(|t| t.as_str()) == Some("text") {
                c.get("content").and_then(|v| v.as_str().map(String::from))
            } else {
                None
            }
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_popup_input_serialization() {
        let input = PopupInput {
            title: "Test".to_string(),
            components: vec![serde_json::json!({
                "type": "text",
                "id": "msg",
                "content": "Hello"
            })],
        };

        let json = serde_json::to_string(&input).unwrap();
        let parsed: PopupInput = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.title, "Test");
        assert_eq!(parsed.components.len(), 1);
    }

    #[test]
    fn test_popup_output_serialization() {
        let output = PopupOutput {
            button: "submit".to_string(),
            values: serde_json::json!({"choice": "option1"}),
        };

        let json = serde_json::to_string(&output).unwrap();
        let parsed: PopupOutput = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.button, "submit");
    }

    #[test]
    fn test_extract_choice_items_from_choice_component() {
        let components = vec![serde_json::json!({
            "type": "choice",
            "id": "action",
            "label": "Select action",
            "options": ["Option A", "Option B", "Option C"]
        })];

        let items = extract_choice_items(&components);
        assert_eq!(items, vec!["Option A", "Option B", "Option C"]);
    }

    #[test]
    fn test_extract_choice_items_fallback_to_text() {
        let components = vec![
            serde_json::json!({
                "type": "text",
                "id": "t1",
                "content": "First Item"
            }),
            serde_json::json!({
                "type": "text",
                "id": "t2",
                "content": "Second Item"
            }),
        ];

        let items = extract_choice_items(&components);
        assert_eq!(items, vec!["First Item", "Second Item"]);
    }

    #[test]
    fn test_extract_choice_items_empty() {
        let components: Vec<serde_json::Value> = vec![];
        let items = extract_choice_items(&components);
        assert!(items.is_empty());
    }

    #[test]
    fn test_extract_choice_items_prefers_choice_over_text() {
        let components = vec![
            serde_json::json!({
                "type": "text",
                "id": "header",
                "content": "Please select:"
            }),
            serde_json::json!({
                "type": "choice",
                "id": "action",
                "label": "Select action",
                "options": ["A", "B"]
            }),
        ];

        let items = extract_choice_items(&components);
        // Should extract from choice component, not text
        assert_eq!(items, vec!["A", "B"]);
    }

    #[test]
    fn test_sanitize_payload_field() {
        // Pipes replaced with dashes
        assert_eq!(sanitize_payload_field("Select | Insert"), "Select - Insert");
        // Commas replaced with semicolons
        assert_eq!(sanitize_payload_field("A, B, C"), "A; B; C");
        // Both
        assert_eq!(sanitize_payload_field("X|Y,Z"), "X-Y;Z");
        // Clean string unchanged
        assert_eq!(sanitize_payload_field("Normal Text"), "Normal Text");
    }
}