hen 0.22.0

Run protocol-aware API request collections from the command line or through MCP.
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
use std::{
    collections::HashMap,
    fmt,
    io::IsTerminal,
    sync::{OnceLock, RwLock},
};

use pest::Parser;
use pest_derive::Parser;

#[derive(Parser)]
#[grammar = "src/parser/context.pest"]
struct VarPlaceholderParser;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PromptMode {
    Interactive,
    NonInteractive,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PromptInputError {
    prompt: String,
    default: Option<String>,
}

impl PromptInputError {
    fn missing(prompt: String, default: Option<String>) -> Self {
        Self { prompt, default }
    }

    pub fn prompt(&self) -> &str {
        &self.prompt
    }

    pub fn default(&self) -> Option<&str> {
        self.default.as_deref()
    }
}

impl fmt::Display for PromptInputError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.default {
            Some(default) => write!(
                f,
                "Missing value for prompt '{}' (default: {})",
                self.prompt, default
            ),
            None => write!(f, "Missing value for prompt '{}'", self.prompt),
        }
    }
}

impl std::error::Error for PromptInputError {}

pub fn inject_from_prompt(input: &str) -> String {
    try_inject_from_prompt(input).expect("prompt resolution failed")
}

pub fn try_inject_from_prompt(input: &str) -> Result<String, PromptInputError> {
    let mut output = String::new();

    let mut pairs = VarPlaceholderParser::parse(Rule::text, input).unwrap();

    let pair = pairs.next().unwrap();

    for inner_pair in pair.into_inner() {
        match inner_pair.as_rule() {
            Rule::word => {
                output.push_str(inner_pair.as_str());
            }
            Rule::input => {
                let (key, default) = parse_input_pair(inner_pair.as_str());

                if let Some(value) = prompt_inputs().read().unwrap().get(&key).cloned() {
                    output.push_str(value.as_str());
                    continue;
                }

                if !can_prompt_for_inputs() {
                    return Err(PromptInputError::missing(key, default));
                }

                let prompt = match &default {
                    Some(def) => format!("Provide a value for \"{}\" (default: {})", key, def),
                    None => format!("Provide a value for \"{}\"", key),
                };

                let mut dialog = dialoguer::Input::new().with_prompt(prompt);
                if let Some(def) = &default {
                    dialog = dialog.default(def.to_string());
                }

                let input: String = dialog.interact().unwrap();

                // store the resolved value so repeated prompts reuse it
                prompt_inputs()
                    .write()
                    .unwrap()
                    .insert(key.clone(), input.clone());

                output.push_str(input.as_str());
            }
            Rule::var => {
                // retain the variable placeholder
                // unresolved variables may be encountered in the context of a preamble.
                output.push_str(format!("{{{{{}}}}}", inner_pair.as_str()).as_str());
            }
            _ => {
                unreachable!("unexpected rule: {:?}", inner_pair.as_rule());
            }
        }
    }
    Ok(output)
}

pub(crate) fn try_inject_from_prompt_inputs_or_defaults(
    input: &str,
) -> Result<String, PromptInputError> {
    let mut output = String::new();

    let mut pairs = VarPlaceholderParser::parse(Rule::text, input).unwrap();
    let pair = pairs.next().unwrap();

    for inner_pair in pair.into_inner() {
        match inner_pair.as_rule() {
            Rule::word => {
                output.push_str(inner_pair.as_str());
            }
            Rule::input => {
                let (key, default) = parse_input_pair(inner_pair.as_str());

                if let Some(value) = prompt_inputs().read().unwrap().get(&key).cloned() {
                    output.push_str(value.as_str());
                    continue;
                }

                if let Some(default) = default {
                    output.push_str(default.as_str());
                    continue;
                }

                return Err(PromptInputError::missing(key, None));
            }
            Rule::var => {
                output.push_str(format!("{{{{{}}}}}", inner_pair.as_str()).as_str());
            }
            _ => {
                unreachable!("unexpected rule: {:?}", inner_pair.as_rule());
            }
        }
    }

    Ok(output)
}

pub fn inject_from_variable(input: &str, context: &HashMap<String, String>) -> String {
    let mut output = String::new();

    let mut pairs = VarPlaceholderParser::parse(Rule::text, input).unwrap();

    let pair = pairs.next().unwrap();

    for inner_pair in pair.into_inner() {
        match inner_pair.as_rule() {
            Rule::word => {
                output.push_str(inner_pair.as_str());
            }
            Rule::var => {
                let key = inner_pair.as_str().to_string();
                if let Some(value) = context.get(&key) {
                    output.push_str(value);
                } else {
                    output.push_str(format!("{{{{{}}}}}", key).as_str());
                }
            }
            Rule::input => {
                // retain the input placeholder
                output.push_str(format!("[[{}]]", inner_pair.as_str()).as_str());
            }
            _ => {
                unreachable!("unexpected rule: {:?}", inner_pair.as_rule());
            }
        }
    }
    output
}

pub fn resolve_with_context(input: &str, context: &HashMap<String, String>) -> String {
    let mut current = inject_from_variable(input, context);
    loop {
        let next = inject_from_variable(current.as_str(), context);
        if next == current {
            return current;
        }
        current = next;
    }
}

pub fn extract_placeholders(input: &str) -> Vec<String> {
    let mut names = Vec::new();

    let mut pairs = VarPlaceholderParser::parse(Rule::text, input).unwrap();
    let pair = pairs.next().unwrap();

    for inner_pair in pair.into_inner() {
        if inner_pair.as_rule() == Rule::var {
            names.push(inner_pair.as_str().to_string());
        }
    }

    names
}

pub fn set_prompt_inputs(inputs: HashMap<String, String>) {
    let mut map = prompt_inputs().write().unwrap();
    map.clear();
    map.extend(inputs);
}

pub fn set_prompt_mode(mode: PromptMode) {
    *prompt_mode_lock().write().unwrap() = mode;
}

pub fn prompt_mode() -> PromptMode {
    *prompt_mode_lock().read().unwrap()
}

pub fn can_prompt_for_inputs() -> bool {
    prompt_mode() == PromptMode::Interactive && prompt_terminals_available()
}

pub fn extract_prompt_placeholders(input: &str) -> Vec<(String, Option<String>)> {
    let mut prompts = Vec::new();
    let mut remainder = input;

    while let Some(start) = remainder.find("[[") {
        let after_start = &remainder[start + 2..];
        let Some(end) = after_start.find("]]") else {
            break;
        };

        let raw = after_start[..end].trim();
        if !raw.is_empty() {
            prompts.push(parse_input_pair(raw));
        }

        remainder = &after_start[end + 2..];
    }

    prompts
}

pub fn has_prompt_input(key: &str) -> bool {
    prompt_inputs().read().unwrap().contains_key(key)
}

fn prompt_inputs() -> &'static RwLock<HashMap<String, String>> {
    static PROMPT_INPUTS: OnceLock<RwLock<HashMap<String, String>>> = OnceLock::new();
    PROMPT_INPUTS.get_or_init(|| RwLock::new(HashMap::new()))
}

fn prompt_mode_lock() -> &'static RwLock<PromptMode> {
    static PROMPT_MODE: OnceLock<RwLock<PromptMode>> = OnceLock::new();
    PROMPT_MODE.get_or_init(|| RwLock::new(PromptMode::Interactive))
}

fn prompt_terminals_available() -> bool {
    #[cfg(test)]
    if let Some(available) = *prompt_terminal_override().read().unwrap() {
        return available;
    }

    std::io::stdin().is_terminal() && std::io::stderr().is_terminal()
}

#[cfg(test)]
fn prompt_terminal_override() -> &'static RwLock<Option<bool>> {
    static PROMPT_TERMINAL_OVERRIDE: OnceLock<RwLock<Option<bool>>> = OnceLock::new();
    PROMPT_TERMINAL_OVERRIDE.get_or_init(|| RwLock::new(None))
}

#[cfg(test)]
pub(crate) fn set_prompt_terminal_override(available: Option<bool>) {
    *prompt_terminal_override().write().unwrap() = available;
}

#[doc(hidden)]
#[allow(dead_code)]
pub fn prompt_state_test_guard() -> std::sync::MutexGuard<'static, ()> {
    static TEST_GUARD: OnceLock<std::sync::Mutex<()>> = OnceLock::new();
    TEST_GUARD
        .get_or_init(|| std::sync::Mutex::new(()))
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
}

fn parse_input_pair(raw: &str) -> (String, Option<String>) {
    let mut parts = raw.splitn(2, '=');
    let key = parts.next().unwrap().trim().to_string();
    let default = parts.next().map(|value| value.trim().to_string());
    (key, default)
}

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

    fn test_guard() -> std::sync::MutexGuard<'static, ()> {
        prompt_state_test_guard()
    }

    #[test]
    fn should_replace_variables() {
        let _guard = test_guard();
        let input = "this is a test with a {{variable}}";
        let mut context = HashMap::new();
        context.insert("variable".to_string(), "value".to_string());

        let output = inject_from_variable(input, &context);

        assert_eq!(output, "this is a test with a value");
    }

    #[test]
    fn should_use_provided_prompt_inputs() {
        let _guard = test_guard();
        let mut inputs = HashMap::new();
        inputs.insert("foo".to_string(), "bar".to_string());
        set_prompt_mode(PromptMode::Interactive);
        set_prompt_inputs(inputs);

        let output = inject_from_prompt("value [[ foo ]]");

        assert_eq!(output, "value bar");

        set_prompt_inputs(HashMap::new());
        set_prompt_mode(PromptMode::Interactive);
    }

    #[test]
    fn should_extract_prompt_placeholders_from_multiline_source() {
        let prompts = extract_prompt_placeholders(
            "GET https://example.com/[[ token ]]\n? region = [[ region = us-east-1 ]]",
        );

        assert_eq!(
            prompts,
            vec![
                ("token".to_string(), None),
                ("region".to_string(), Some("us-east-1".to_string()))
            ]
        );
    }

    #[test]
    fn should_extract_prompt_placeholders_with_url_defaults() {
        let prompts = extract_prompt_placeholders(
            "GET [[ ws_origin = wss://example.com ]]/chat",
        );

        assert_eq!(
            prompts,
            vec![(
                "ws_origin".to_string(),
                Some("wss://example.com".to_string())
            )]
        );
    }

    #[test]
    fn should_fail_on_missing_prompt_when_noninteractive() {
        let _guard = test_guard();
        set_prompt_mode(PromptMode::NonInteractive);
        set_prompt_inputs(HashMap::new());

        let err = try_inject_from_prompt("value [[ foo = bar ]]").unwrap_err();

        assert_eq!(err.prompt(), "foo");
        assert_eq!(err.default(), Some("bar"));

        set_prompt_mode(PromptMode::Interactive);
    }

    #[test]
    fn should_fail_on_missing_prompt_with_url_default_when_noninteractive() {
        let _guard = test_guard();
        set_prompt_mode(PromptMode::NonInteractive);
        set_prompt_inputs(HashMap::new());

        let err =
            try_inject_from_prompt("value [[ ws_origin = wss://example.com ]]").unwrap_err();

        assert_eq!(err.prompt(), "ws_origin");
        assert_eq!(err.default(), Some("wss://example.com"));

        set_prompt_mode(PromptMode::Interactive);
    }

    #[test]
    fn should_use_defaults_without_prompting_in_inputs_or_defaults_mode() {
        let _guard = test_guard();
        set_prompt_mode(PromptMode::Interactive);
        set_prompt_inputs(HashMap::new());

        let output = try_inject_from_prompt_inputs_or_defaults(
            "value [[ ws_origin = wss://example.com ]]",
        )
        .unwrap();

        assert_eq!(output, "value wss://example.com");

        set_prompt_inputs(HashMap::new());
        set_prompt_mode(PromptMode::Interactive);
    }
}