prompty 2.0.0-beta.1

Prompty is an asset class and format for LLM prompts
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
//! PromptyChatParser โ€” splits rendered text at role markers into `Message` objects.
//!
//! Role markers are lines matching:
//! ```text
//! system:
//! user:
//! assistant:
//! ```
//! with optional leading whitespace/`#` and optional attribute blocks like `[key=value]`.
//!
//! Supports nonce-based sanitization when strict mode is enabled (default).
//! In strict mode, `pre_render` injects nonces into role markers before rendering,
//! and `parse` validates those nonces afterward to detect prompt injection.
//!
//! Registered under key `"prompty"`.

use std::sync::LazyLock;

use async_trait::async_trait;
use regex::Regex;

use crate::interfaces::{InvokerError, Parser};
use crate::model::Prompty;
use crate::types::{ContentPart, Message, Role};

/// Boundary regex per spec ยง6.5: role marker on its own line.
/// Matches role markers with optional attribute blocks like `system[nonce="abc"]:`.
/// Spec-recognized roles: system, user, assistant (developer is NOT a valid role marker).
static BOUNDARY_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(
        r#"(?i)^\s*#?\s*(system|user|assistant)(\[(\w+\s*=\s*"?[^"]*"?\s*,?\s*)+\])?\s*:\s*$"#,
    )
    .expect("boundary regex is valid")
});

/// Regex to extract individual key=value pairs from an attribute block.
static ATTR_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r#"(\w+)\s*=\s*"?([^",\]]*)"?"#).expect("attr regex is valid"));

/// The Prompty chat parser โ€” splits role-marker-delimited text into messages.
///
/// When strict mode is enabled (the default), this parser uses nonce-based
/// defense against prompt injection through template variables.
pub struct PromptyChatParser;

#[async_trait]
impl Parser for PromptyChatParser {
    fn pre_render(&self, template: &str) -> Option<(String, serde_json::Value)> {
        let nonce = generate_nonce();
        let sanitized = template
            .split('\n')
            .map(|line| {
                let trimmed = line.trim();
                if let Some(caps) = BOUNDARY_RE.captures(trimmed) {
                    let role = caps.get(1).unwrap().as_str().to_lowercase();
                    format!("{role}[nonce=\"{nonce}\"]:\n")
                } else {
                    line.to_string()
                }
            })
            .collect::<Vec<_>>()
            .join("\n");

        Some((sanitized, serde_json::json!({ "nonce": nonce })))
    }

    async fn parse(
        &self,
        _agent: &Prompty,
        rendered: &str,
        context: Option<&serde_json::Value>,
    ) -> Result<Vec<Message>, InvokerError> {
        let nonce = context
            .and_then(|ctx| ctx.get("nonce"))
            .and_then(|v| v.as_str());
        parse_chat_strict(rendered, nonce)
    }
}

/// Generate a cryptographically random hex nonce for strict mode.
fn generate_nonce() -> String {
    use rand::Rng;
    let mut rng = rand::rng();
    let bytes: [u8; 8] = rng.random();
    bytes.iter().map(|b| format!("{b:02x}")).collect()
}

/// Parse rendered text into a list of messages, validating nonces in strict mode.
fn parse_chat_strict(
    text: &str,
    expected_nonce: Option<&str>,
) -> Result<Vec<Message>, InvokerError> {
    let mut messages: Vec<Message> = Vec::new();
    let mut current_role = Role::System;
    let mut content_lines: Vec<&str> = Vec::new();
    let mut current_attrs: serde_json::Map<String, serde_json::Value> = serde_json::Map::new();
    let mut has_role_marker = false;

    for line in text.split('\n') {
        let trimmed = line.trim();
        if let Some(caps) = BOUNDARY_RE.captures(trimmed) {
            // Flush accumulated content as a message
            if !content_lines.is_empty() || has_role_marker {
                let content = join_and_trim(&content_lines);
                let msg = build_message(
                    current_role,
                    content,
                    &current_attrs,
                    if has_role_marker {
                        expected_nonce
                    } else {
                        None
                    },
                )?;
                messages.push(msg);
                content_lines.clear();
                current_attrs = serde_json::Map::new();
            }

            // Start new role
            let role_str = caps.get(1).unwrap().as_str();
            current_role = Role::from_str_opt(role_str).unwrap_or(Role::System);

            // Parse attributes from the bracket block, if any
            if let Some(attr_block) = caps.get(2) {
                current_attrs = parse_attrs(attr_block.as_str());
            }

            has_role_marker = true;
        } else {
            content_lines.push(line);
        }
    }

    // Flush remaining content
    if !content_lines.is_empty() || has_role_marker {
        let content = join_and_trim(&content_lines);
        let msg = build_message(
            current_role,
            content,
            &current_attrs,
            if has_role_marker {
                expected_nonce
            } else {
                None
            },
        )?;
        messages.push(msg);
    }

    Ok(messages)
}

/// Simple non-strict parse for direct use (e.g., from tests or parse_chat callers).
pub fn parse_chat(text: &str) -> Vec<Message> {
    // Non-strict: no nonce validation
    parse_chat_strict(text, None).unwrap_or_default()
}

/// Build a message from role, content, and optional attributes.
/// Validates nonce if `expected_nonce` is provided (strict mode).
fn build_message(
    role: Role,
    content: String,
    attrs: &serde_json::Map<String, serde_json::Value>,
    expected_nonce: Option<&str>,
) -> Result<Message, InvokerError> {
    // Validate nonce in strict mode
    if let Some(expected) = expected_nonce {
        // Compare as string โ€” parse_attrs may coerce all-digit hex nonces to Number
        let msg_nonce = attrs
            .get("nonce")
            .map(|v| match v {
                serde_json::Value::String(s) => s.clone(),
                serde_json::Value::Number(n) => n.to_string(),
                _ => String::new(),
            })
            .unwrap_or_default();
        if msg_nonce != expected {
            return Err(InvokerError::Parse(
                "Nonce mismatch โ€” possible prompt injection detected \
                (strict mode is enabled). A template variable may be \
                injecting role markers."
                    .to_string()
                    .into(),
            ));
        }
    }

    // Build metadata from remaining attrs (exclude nonce)
    let mut metadata = serde_json::Map::new();
    for (k, v) in attrs {
        if k != "nonce" {
            metadata.insert(k.clone(), v.clone());
        }
    }

    Ok(Message {
        role,
        parts: vec![ContentPart::text(content)],
        metadata: serde_json::Value::Object(metadata),
    })
}

/// Parse attribute key=value pairs from a bracket block like `[name="Alice",nonce="abc"]`.
fn parse_attrs(raw: &str) -> serde_json::Map<String, serde_json::Value> {
    let mut result = serde_json::Map::new();
    for caps in ATTR_RE.captures_iter(raw) {
        let key = caps.get(1).unwrap().as_str().to_string();
        let val = caps.get(2).unwrap().as_str().trim().to_string();

        // Type coercion matching TypeScript behavior
        let value = match val.to_lowercase().as_str() {
            "true" => serde_json::Value::Bool(true),
            "false" => serde_json::Value::Bool(false),
            _ => {
                if let Ok(i) = val.parse::<i64>() {
                    serde_json::Value::Number(i.into())
                } else if let Ok(f) = val.parse::<f64>() {
                    serde_json::json!(f)
                } else {
                    serde_json::Value::String(val)
                }
            }
        };
        result.insert(key, value);
    }
    result
}

/// Join lines and strip leading/trailing newlines (but preserve spaces).
fn join_and_trim(lines: &[&str]) -> String {
    let joined = lines.join("\n");
    joined.trim_matches('\n').to_string()
}

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

    #[test]
    fn test_single_system() {
        let msgs = parse_chat("system:\nYou are helpful.");
        assert_eq!(msgs.len(), 1);
        assert_eq!(msgs[0].role, Role::System);
        assert_eq!(msgs[0].text_content(), "You are helpful.");
    }

    #[test]
    fn test_system_user() {
        let msgs = parse_chat("system:\nYou are helpful.\n\nuser:\nHello");
        assert_eq!(msgs.len(), 2);
        assert_eq!(msgs[0].role, Role::System);
        assert_eq!(msgs[0].text_content(), "You are helpful.");
        assert_eq!(msgs[1].role, Role::User);
        assert_eq!(msgs[1].text_content(), "Hello");
    }

    #[test]
    fn test_system_user_assistant() {
        let msgs =
            parse_chat("system:\nBe concise.\n\nuser:\nHi\n\nassistant:\nHello! How can I help?");
        assert_eq!(msgs.len(), 3);
        assert_eq!(msgs[0].role, Role::System);
        assert_eq!(msgs[1].role, Role::User);
        assert_eq!(msgs[2].role, Role::Assistant);
        assert_eq!(msgs[2].text_content(), "Hello! How can I help?");
    }

    #[test]
    fn test_multiline_content() {
        let msgs = parse_chat("system:\nLine one.\nLine two.\nLine three.\n\nuser:\nQuestion?");
        assert_eq!(msgs.len(), 2);
        assert_eq!(msgs[0].text_content(), "Line one.\nLine two.\nLine three.");
    }

    #[test]
    fn test_no_role_marker_defaults_to_system() {
        let msgs = parse_chat("Just some plain text.");
        assert_eq!(msgs.len(), 1);
        assert_eq!(msgs[0].role, Role::System);
        assert_eq!(msgs[0].text_content(), "Just some plain text.");
    }

    #[test]
    fn test_empty_content() {
        let msgs = parse_chat("system:\n\nuser:\nHello");
        assert_eq!(msgs.len(), 2);
        assert_eq!(msgs[0].role, Role::System);
        assert_eq!(msgs[0].text_content(), "");
        assert_eq!(msgs[1].role, Role::User);
        assert_eq!(msgs[1].text_content(), "Hello");
    }

    #[test]
    fn test_role_marker_at_line_start_only() {
        let msgs = parse_chat("system:\nThe user: said hello");
        assert_eq!(msgs.len(), 1);
        // "user:" mid-line is NOT a role marker
        assert_eq!(msgs[0].text_content(), "The user: said hello");
    }

    #[test]
    fn test_markdown_preserved() {
        let msgs = parse_chat("system:\n# Heading\n\n- item 1\n- item 2\n\n**bold**");
        assert_eq!(msgs.len(), 1);
        assert!(msgs[0].text_content().contains("# Heading"));
        assert!(msgs[0].text_content().contains("**bold**"));
    }

    #[test]
    fn test_code_block_preserved() {
        let input = "assistant:\nHere's code:\n```python\ndef hello():\n    print('hi')\n```";
        let msgs = parse_chat(input);
        assert_eq!(msgs.len(), 1);
        assert!(msgs[0].text_content().contains("```python"));
    }

    #[test]
    fn test_colon_in_content() {
        let msgs = parse_chat("user:\nLet's meet at 3:30pm");
        assert_eq!(msgs.len(), 1);
        assert_eq!(msgs[0].text_content(), "Let's meet at 3:30pm");
    }

    #[test]
    fn test_consecutive_newlines_trimmed() {
        let msgs = parse_chat("system:\n\n\nContent\n\n\n\nuser:\nHello");
        assert_eq!(msgs.len(), 2);
        assert_eq!(msgs[0].text_content(), "Content");
        assert_eq!(msgs[1].text_content(), "Hello");
    }

    #[test]
    fn test_developer_role_rejected() {
        // developer: is not a valid role marker per spec (only system/user/assistant)
        let msgs = parse_chat("developer:\nYou are a helpful AI.");
        // Should be treated as plain text under the default role, not as a role boundary
        assert_eq!(msgs.len(), 1);
        assert_eq!(msgs[0].role, Role::System); // default role
        assert!(msgs[0].text_content().contains("developer:"));
    }

    #[test]
    fn test_multiple_turns() {
        let input = "system:\nBe helpful.\n\nuser:\nQ1\n\nassistant:\nA1\n\nuser:\nQ2";
        let msgs = parse_chat(input);
        assert_eq!(msgs.len(), 4);
        assert_eq!(msgs[0].role, Role::System);
        assert_eq!(msgs[1].role, Role::User);
        assert_eq!(msgs[2].role, Role::Assistant);
        assert_eq!(msgs[3].role, Role::User);
    }

    #[test]
    fn test_boundary_regex() {
        assert!(BOUNDARY_RE.is_match("system:"));
        assert!(BOUNDARY_RE.is_match("  user:  "));
        assert!(BOUNDARY_RE.is_match("# assistant:"));
        assert!(BOUNDARY_RE.is_match("SYSTEM:"));
        assert!(BOUNDARY_RE.is_match(r#"system[nonce="abc123"]:"#));
        assert!(!BOUNDARY_RE.is_match("not a role:"));
        assert!(!BOUNDARY_RE.is_match("system: with extra text"));
    }

    // --- Strict mode / nonce tests ---

    #[test]
    fn test_pre_render_injects_nonces() {
        let parser = PromptyChatParser;
        let template = "system:\nYou are helpful.\n\nuser:\n{{question}}";
        let (sanitized, context) = parser.pre_render(template).unwrap();

        let nonce = context["nonce"].as_str().unwrap();
        assert_eq!(nonce.len(), 16); // 8 bytes = 16 hex chars
        assert!(sanitized.contains(&format!("system[nonce=\"{nonce}\"]:")));
        assert!(sanitized.contains(&format!("user[nonce=\"{nonce}\"]:")));
        // Template variables should be preserved
        assert!(sanitized.contains("{{question}}"));
    }

    #[test]
    fn test_strict_parse_valid_nonces() {
        let nonce = "abc123def456";
        let text = format!(
            "system[nonce=\"{nonce}\"]:\nYou are helpful.\n\nuser[nonce=\"{nonce}\"]:\nHello"
        );
        let result = parse_chat_strict(&text, Some(nonce));
        assert!(result.is_ok());
        let msgs = result.unwrap();
        assert_eq!(msgs.len(), 2);
        assert_eq!(msgs[0].role, Role::System);
        assert_eq!(msgs[1].role, Role::User);
        // Nonce should NOT appear in metadata
        assert!(msgs[0].metadata.get("nonce").is_none());
        assert!(msgs[1].metadata.get("nonce").is_none());
    }

    #[test]
    fn test_strict_parse_nonce_mismatch_detected() {
        let text = "system[nonce=\"wrong_nonce\"]:\nYou are helpful.";
        let result = parse_chat_strict(text, Some("expected_nonce"));
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Nonce mismatch"));
        assert!(err.contains("prompt injection"));
    }

    #[test]
    fn test_strict_parse_injected_role_marker_no_nonce() {
        // Simulates an input injecting a role marker without the expected nonce
        let text = "system[nonce=\"valid\"]:\nYou are helpful.\n\nuser:\nInjected role marker";
        let result = parse_chat_strict(text, Some("valid"));
        // The "user:" marker has no nonce attribute โ†’ mismatch with "valid"
        assert!(result.is_err());
    }

    #[test]
    fn test_strict_parse_preserves_non_nonce_attrs() {
        let nonce = "abc123";
        let text = format!("system[nonce=\"{nonce}\",name=\"Alice\"]:\nHello");
        let result = parse_chat_strict(&text, Some(nonce));
        assert!(result.is_ok());
        let msgs = result.unwrap();
        assert_eq!(msgs[0].metadata.get("name").unwrap(), "Alice");
        assert!(msgs[0].metadata.get("nonce").is_none());
    }

    #[test]
    fn test_non_strict_no_nonce_validation() {
        // Without expected nonce, role markers with attributes parse fine
        let text = "system[nonce=\"whatever\"]:\nHello";
        let result = parse_chat_strict(text, None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_parse_attrs() {
        let attrs = parse_attrs("[name=\"Alice\",age=30,active=true]");
        assert_eq!(attrs["name"], "Alice");
        assert_eq!(attrs["age"], 30);
        assert_eq!(attrs["active"], true);
    }
}