llman 0.0.55

A tool for managing LLM application rules(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
use crate::sdd::shared::constants::SPEC_FILE;
use crate::sdd::spec::backend::{BACKEND, SpecBackend};
use crate::sdd::spec::ir::{DeltaSpecDoc, FeatureRefEntry, MainSpecDoc};
use anyhow::{Result, anyhow};
use regex::Regex;
use serde::Serialize;
use std::path::Path;

#[derive(Debug, Clone, Serialize)]
pub struct Scenario {
    #[serde(rename = "rawText")]
    pub raw_text: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct Requirement {
    pub text: String,
    pub scenarios: Vec<Scenario>,
}

#[derive(Debug, Clone, Serialize)]
pub struct FeatureRef {
    pub path: String,
    pub scope: String,
    pub required: bool,
}

impl From<&FeatureRefEntry> for FeatureRef {
    fn from(entry: &FeatureRefEntry) -> Self {
        Self {
            path: entry.path.clone(),
            scope: entry.scope.clone(),
            required: entry.required,
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct Spec {
    pub name: String,
    pub overview: String,
    pub requirements: Vec<Requirement>,
    /// Feature references for BDD "point-only" specs (executable behavior lives in
    /// `.feature` files). Present only when the spec carries `feature_refs`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub feature_refs: Option<Vec<FeatureRef>>,
    pub metadata: SpecMetadata,
}

#[derive(Debug, Clone, Serialize)]
pub struct SpecMetadata {
    pub format: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct Change {
    pub name: String,
    pub why: String,
    #[serde(rename = "whatChanges")]
    pub what_changes: String,
    pub deltas: Vec<Delta>,
    pub metadata: ChangeMetadata,
}

#[derive(Debug, Clone, Serialize)]
pub struct ChangeMetadata {
    pub format: String,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum DeltaOperation {
    Added,
    Modified,
    Removed,
    Renamed,
}

#[derive(Debug, Clone, Serialize)]
pub struct RenamePair {
    pub from: String,
    pub to: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct Delta {
    pub spec: String,
    pub operation: DeltaOperation,
    pub description: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub requirement: Option<Requirement>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub requirements: Option<Vec<Requirement>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rename: Option<RenamePair>,
}

pub fn parse_spec(content: &str, name: &str) -> Result<Spec> {
    let context = format!("spec `{}`", name);
    let doc = BACKEND.parse_main_spec(content, &context)?;
    Ok(convert_main_doc_to_spec(&doc, name))
}

pub fn parse_change(content: &str, name: &str, change_dir: &Path) -> Result<Change> {
    let why =
        extract_section(content, "Why").ok_or_else(|| anyhow!("Change must have a Why section"))?;
    let what_changes = extract_section(content, "What Changes")
        .ok_or_else(|| anyhow!("Change must have a What Changes section"))?;

    let deltas = parse_change_deltas(&what_changes, change_dir)?;

    Ok(Change {
        name: name.to_string(),
        why: why.trim().to_string(),
        what_changes: what_changes.trim().to_string(),
        deltas,
        metadata: ChangeMetadata {
            format: "openspec-change".to_string(),
        },
    })
}

fn parse_change_deltas(what_changes: &str, change_dir: &Path) -> Result<Vec<Delta>> {
    let spec_deltas = parse_delta_specs(change_dir)?;
    if !spec_deltas.is_empty() {
        return Ok(spec_deltas);
    }
    Ok(parse_simple_deltas(what_changes))
}

pub fn parse_delta_specs(change_dir: &Path) -> Result<Vec<Delta>> {
    let mut deltas = Vec::new();
    let specs_dir = change_dir.join("specs");
    if !specs_dir.exists() {
        return Ok(deltas);
    }

    for entry in std::fs::read_dir(specs_dir)? {
        let entry = entry?;
        if !entry.file_type()?.is_dir() {
            continue;
        }
        let spec_name = entry.file_name().to_string_lossy().to_string();
        let spec_file = entry.path().join(SPEC_FILE);
        if !spec_file.exists() {
            continue;
        }
        let content = std::fs::read_to_string(&spec_file)?;
        let context = format!("delta spec `{}`", spec_name);
        let doc = BACKEND.parse_delta_spec(&content, &context)?;
        deltas.extend(convert_delta_doc_to_deltas(&spec_name, &doc)?);
    }

    Ok(deltas)
}

fn convert_main_doc_to_spec(doc: &MainSpecDoc, fallback_name: &str) -> Spec {
    let mut scenarios_by_req: std::collections::HashMap<&str, Vec<Scenario>> =
        std::collections::HashMap::new();
    for scenario in &doc.scenarios {
        scenarios_by_req
            .entry(scenario.req_id.as_str())
            .or_default()
            .push(Scenario {
                raw_text: render_scenario_text(
                    scenario.given.trim(),
                    scenario.when_.trim(),
                    scenario.then_.trim(),
                ),
            });
    }

    let requirements = doc
        .requirements
        .iter()
        .map(|req| Requirement {
            text: req.statement.trim().to_string(),
            scenarios: scenarios_by_req
                .remove(req.req_id.as_str())
                .unwrap_or_default(),
        })
        .collect::<Vec<_>>();

    Spec {
        name: if doc.name.trim().is_empty() {
            fallback_name.to_string()
        } else {
            doc.name.trim().to_string()
        },
        overview: doc.purpose.trim().to_string(),
        requirements,
        feature_refs: doc
            .feature_refs
            .as_ref()
            .map(|refs| refs.iter().map(FeatureRef::from).collect()),
        metadata: SpecMetadata {
            format: "llman-sdd-toon".to_string(),
        },
    }
}

fn convert_delta_doc_to_deltas(spec_name: &str, doc: &DeltaSpecDoc) -> Result<Vec<Delta>> {
    let mut scenarios_by_req: std::collections::HashMap<&str, Vec<Scenario>> =
        std::collections::HashMap::new();
    for scenario in &doc.op_scenarios {
        scenarios_by_req
            .entry(scenario.req_id.as_str())
            .or_default()
            .push(Scenario {
                raw_text: render_scenario_text(
                    scenario.given.trim(),
                    scenario.when_.trim(),
                    scenario.then_.trim(),
                ),
            });
    }

    let mut deltas = Vec::new();
    for op in &doc.ops {
        let op_kind = op.op.trim().to_ascii_lowercase();
        match op_kind.as_str() {
            "add_requirement" => {
                let title = op
                    .title
                    .as_deref()
                    .map(|v| v.trim())
                    .filter(|v| !v.is_empty())
                    .ok_or_else(|| anyhow!("delta spec `{}`: add_requirement op for req_id `{}` is missing `title`", spec_name, op.req_id))?;
                let statement = op
                    .statement
                    .as_deref()
                    .map(|v| v.trim())
                    .filter(|v| !v.is_empty())
                    .ok_or_else(|| anyhow!("delta spec `{}`: add_requirement op for req_id `{}` is missing `statement`", spec_name, op.req_id))?;
                let requirement = Requirement {
                    text: statement.to_string(),
                    scenarios: scenarios_by_req
                        .remove(op.req_id.as_str())
                        .unwrap_or_default(),
                };
                deltas.push(Delta {
                    spec: spec_name.to_string(),
                    operation: DeltaOperation::Added,
                    description: format!("Add requirement: {}", title),
                    requirement: Some(requirement.clone()),
                    requirements: Some(vec![requirement]),
                    rename: None,
                });
            }
            "modify_requirement" => {
                let title = op
                    .title
                    .as_deref()
                    .map(|v| v.trim())
                    .filter(|v| !v.is_empty())
                    .ok_or_else(|| anyhow!("delta spec `{}`: modify_requirement op for req_id `{}` is missing `title`", spec_name, op.req_id))?;
                let statement = op
                    .statement
                    .as_deref()
                    .map(|v| v.trim())
                    .filter(|v| !v.is_empty())
                    .ok_or_else(|| anyhow!("delta spec `{}`: modify_requirement op for req_id `{}` is missing `statement`", spec_name, op.req_id))?;
                let requirement = Requirement {
                    text: statement.to_string(),
                    scenarios: scenarios_by_req
                        .remove(op.req_id.as_str())
                        .unwrap_or_default(),
                };
                deltas.push(Delta {
                    spec: spec_name.to_string(),
                    operation: DeltaOperation::Modified,
                    description: format!("Modify requirement: {}", title),
                    requirement: Some(requirement.clone()),
                    requirements: Some(vec![requirement]),
                    rename: None,
                });
            }
            "remove_requirement" => {
                let text = op
                    .name
                    .as_deref()
                    .map(|v| v.trim())
                    .filter(|v| !v.is_empty())
                    .unwrap_or_else(|| op.req_id.trim())
                    .to_string();
                let requirement = Requirement {
                    text: text.clone(),
                    scenarios: Vec::new(),
                };
                deltas.push(Delta {
                    spec: spec_name.to_string(),
                    operation: DeltaOperation::Removed,
                    description: format!("Remove requirement: {}", text),
                    requirement: Some(requirement.clone()),
                    requirements: Some(vec![requirement]),
                    rename: None,
                });
            }
            "rename_requirement" => {
                let from = op
                    .from
                    .as_deref()
                    .map(|v| v.trim())
                    .filter(|v| !v.is_empty())
                    .ok_or_else(|| anyhow!("delta spec `{}`: rename_requirement op for req_id `{}` is missing `from`", spec_name, op.req_id))?;
                let to = op
                    .to
                    .as_deref()
                    .map(|v| v.trim())
                    .filter(|v| !v.is_empty())
                    .ok_or_else(|| anyhow!("delta spec `{}`: rename_requirement op for req_id `{}` is missing `to`", spec_name, op.req_id))?;
                deltas.push(Delta {
                    spec: spec_name.to_string(),
                    operation: DeltaOperation::Renamed,
                    description: format!("Rename requirement from \"{}\" to \"{}\"", from, to),
                    requirement: None,
                    requirements: None,
                    rename: Some(RenamePair {
                        from: from.to_string(),
                        to: to.to_string(),
                    }),
                });
            }
            other => {
                return Err(anyhow!(
                    "delta spec `{}`: unsupported op `{}` (expected add_requirement/modify_requirement/remove_requirement/rename_requirement)",
                    spec_name,
                    other
                ));
            }
        }
    }

    Ok(deltas)
}

fn render_scenario_text(given: &str, when_: &str, then_: &str) -> String {
    if given.trim().is_empty() {
        format!("WHEN: {when_}\nTHEN: {then_}")
    } else {
        format!("GIVEN: {given}\nWHEN: {when_}\nTHEN: {then_}")
    }
}

fn parse_simple_deltas(what_changes: &str) -> Vec<Delta> {
    let mut deltas = Vec::new();
    let re = Regex::new(r"^\s*-\s*\*\*([^*:]+)\*\*:\s*(.+)$").expect("regex");
    for line in what_changes.lines() {
        let caps = match re.captures(line) {
            Some(caps) => caps,
            None => continue,
        };
        let spec_name = caps.get(1).map(|m| m.as_str().trim()).unwrap_or("");
        let description = caps.get(2).map(|m| m.as_str().trim()).unwrap_or("");
        if spec_name.is_empty() || description.is_empty() {
            continue;
        }

        let lower = description.to_lowercase();
        let operation = if lower.contains("rename") {
            DeltaOperation::Renamed
        } else if lower.contains("add") || lower.contains("create") || lower.contains("new") {
            DeltaOperation::Added
        } else if lower.contains("remove") || lower.contains("delete") {
            DeltaOperation::Removed
        } else {
            DeltaOperation::Modified
        };

        deltas.push(Delta {
            spec: spec_name.to_string(),
            operation,
            description: description.to_string(),
            requirement: None,
            requirements: None,
            rename: None,
        });
    }
    deltas
}

fn extract_section(content: &str, title: &str) -> Option<String> {
    let normalized = content.replace("\r\n", "\n").replace("\r", "\n");
    let lines: Vec<&str> = normalized.lines().collect();
    let header_re = Regex::new(r"^##\s+(.+)$").expect("regex");

    let mut start_index = None;
    for (idx, line) in lines.iter().enumerate() {
        if let Some(caps) = header_re.captures(line) {
            let header = caps.get(1)?.as_str().trim();
            if header.eq_ignore_ascii_case(title) {
                start_index = Some(idx + 1);
                break;
            }
        }
    }

    let start = start_index?;
    let mut collected = Vec::new();
    for line in lines.iter().skip(start) {
        if header_re.is_match(line) {
            break;
        }
        collected.push(*line);
    }
    Some(collected.join("\n").trim().to_string())
}