llm-wiki-engine 0.4.1

Git-backed wiki engine with MCP/ACP server — bring your own LLM
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
use std::path::Path;

use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};

use crate::config;
use crate::engine::{EngineState, WikiEngine};
use crate::git;
use crate::markdown;
use crate::search;
use crate::space_builder;

/// A registered page type with its schema location and description.
#[derive(Debug, Serialize, Deserialize)]
pub struct SchemaTypeEntry {
    /// Type identifier (e.g. `"concept"`).
    pub name: String,
    /// Human-readable description of the type.
    pub description: String,
    /// Relative path to the JSON Schema file.
    pub schema_path: String,
}

/// List all registered types in a wiki's type registry.
pub fn schema_list(engine: &EngineState, wiki_name: &str) -> Result<Vec<SchemaTypeEntry>> {
    let space = engine.space(wiki_name)?;
    Ok(space
        .type_registry
        .list_types()
        .into_iter()
        .map(|(name, desc)| SchemaTypeEntry {
            name: name.to_string(),
            description: desc.to_string(),
            schema_path: space
                .type_registry
                .schema_path(name)
                .unwrap_or_default()
                .to_string(),
        })
        .collect())
}

/// Return the raw JSON Schema content for a named type.
pub fn schema_show(engine: &EngineState, wiki_name: &str, type_name: &str) -> Result<String> {
    let space = engine.space(wiki_name)?;
    let schema_path = space
        .type_registry
        .schema_path(type_name)
        .ok_or_else(|| anyhow::anyhow!("type '{type_name}' is not registered"))?;
    let full_path = space.repo_root.join(schema_path);

    if full_path.exists() {
        return std::fs::read_to_string(&full_path)
            .with_context(|| format!("failed to read schema: {}", full_path.display()));
    }

    let filename = full_path.file_name().and_then(|n| n.to_str()).unwrap_or("");
    crate::default_schemas::default_schemas()
        .get(filename)
        .map(|s| s.to_string())
        .ok_or_else(|| {
            anyhow::anyhow!(
                "schema file not found on disk and no embedded default for '{type_name}': {}",
                full_path.display()
            )
        })
}

/// Return a frontmatter template for a type, derived from its JSON Schema.
pub fn schema_show_template(
    engine: &EngineState,
    wiki_name: &str,
    type_name: &str,
) -> Result<String> {
    let content = schema_show(engine, wiki_name, type_name)?;
    let schema: serde_json::Value = serde_json::from_str(&content)?;
    Ok(generate_template(&schema, type_name))
}

/// Copy a schema file into the wiki and register the type in `wiki.toml`.
pub fn schema_add(
    engine: &EngineState,
    wiki_name: &str,
    type_name: &str,
    src_path: &Path,
) -> Result<String> {
    let space = engine.space(wiki_name)?;

    // Validate the schema file
    let content = std::fs::read_to_string(src_path)
        .with_context(|| format!("failed to read: {}", src_path.display()))?;
    let schema_value: serde_json::Value =
        serde_json::from_str(&content).context("file is not valid JSON")?;
    jsonschema::Validator::new(&schema_value)
        .map_err(|e| anyhow::anyhow!("file is not a valid JSON Schema: {e}"))?;

    // Copy to schemas/
    let filename = src_path
        .file_name()
        .ok_or_else(|| anyhow::anyhow!("invalid path"))?;
    let dest = space.repo_root.join("schemas").join(filename);
    std::fs::copy(src_path, &dest)?;

    // Check if x-wiki-types declares the type
    let has_type = schema_value
        .get("x-wiki-types")
        .and_then(|v| v.as_object())
        .map(|obj| obj.contains_key(type_name))
        .unwrap_or(false);

    let mut msg = format!("copied to {}", dest.display());

    if !has_type {
        // Add wiki.toml override
        let mut wiki_cfg = config::load_wiki(&space.repo_root)?;
        wiki_cfg.types.insert(
            type_name.to_string(),
            config::TypeEntry {
                schema: format!("schemas/{}", filename.to_string_lossy()),
                description: format!("Custom type: {type_name}"),
            },
        );
        config::save_wiki(&wiki_cfg, &space.repo_root)?;
        msg.push_str(&format!(", added [types.{type_name}] to wiki.toml"));
    }

    // Validate index resolution
    if let Err(e) = space_builder::build_space(&space.repo_root, "en_stem") {
        msg.push_str(&format!("\nWARNING: index resolution failed: {e}"));
    }

    Ok(msg)
}

/// Summary of a `schema remove` operation.
#[derive(Debug, Serialize, Deserialize)]
pub struct SchemaRemoveReport {
    /// Number of indexed pages with this type that were removed from the index.
    pub pages_removed: usize,
    /// Number of page files actually deleted from disk.
    pub pages_deleted_from_disk: usize,
    /// True if the `[types.<name>]` entry was removed from `wiki.toml`.
    pub wiki_toml_updated: bool,
    /// True if the schema JSON file was deleted.
    pub schema_file_deleted: bool,
    /// True if this was a dry run (no changes made).
    pub dry_run: bool,
}

/// Remove a type schema and optionally delete its pages.
pub fn schema_remove(
    manager: &WikiEngine,
    wiki_name: &str,
    type_name: &str,
    delete: bool,
    delete_pages: bool,
    dry_run: bool,
) -> Result<SchemaRemoveReport> {
    if type_name == "default" {
        bail!("cannot remove the 'default' type");
    }

    let engine = manager
        .state
        .read()
        .map_err(|_| anyhow::anyhow!("lock poisoned"))?;
    let space = engine.space(wiki_name)?;

    // Count pages of this type in the index
    let searcher = space.index_manager.searcher()?;
    let list_result = search::list(
        &search::ListOptions {
            r#type: Some(type_name.to_string()),
            ..Default::default()
        },
        &searcher,
        wiki_name,
        &space.index_schema,
    )?;
    let pages_to_remove = list_result.total;

    if dry_run {
        return Ok(SchemaRemoveReport {
            pages_removed: pages_to_remove,
            pages_deleted_from_disk: if delete_pages { pages_to_remove } else { 0 },
            wiki_toml_updated: space
                .type_registry
                .list_types()
                .iter()
                .any(|(n, _)| *n == type_name),
            schema_file_deleted: delete,
            dry_run: true,
        });
    }

    // Remove pages from index
    if pages_to_remove > 0 {
        space
            .index_manager
            .delete_by_type(&space.index_schema, type_name)?;
    }

    // Delete page files from disk if requested
    let mut pages_deleted_from_disk = 0;
    if delete_pages && pages_to_remove > 0 {
        for page in &list_result.pages {
            if markdown::delete_page(&page.slug, &space.wiki_root)? {
                pages_deleted_from_disk += 1;
            }
        }
    }

    // Remove wiki.toml override if present
    let mut wiki_toml_updated = false;
    let mut wiki_cfg = config::load_wiki(&space.repo_root)?;
    if wiki_cfg.types.remove(type_name).is_some() {
        config::save_wiki(&wiki_cfg, &space.repo_root)?;
        wiki_toml_updated = true;
    }

    // Delete schema file if requested
    let mut schema_file_deleted = false;
    if delete && let Some(schema_path) = space.type_registry.schema_path(type_name) {
        let full_path = space.repo_root.join(schema_path);
        if full_path.exists() {
            // Check if other types use this schema
            let content = std::fs::read_to_string(&full_path).unwrap_or_default();
            if let Ok(schema) = serde_json::from_str::<serde_json::Value>(&content) {
                let wiki_types = schema
                    .get("x-wiki-types")
                    .and_then(|v| v.as_object())
                    .map(|obj| obj.len())
                    .unwrap_or(0);
                if wiki_types <= 1 {
                    std::fs::remove_file(&full_path)?;
                    schema_file_deleted = true;
                }
                // If multiple types use this schema, don't delete
            }
        }
    }

    // Auto-commit if configured and changes were made
    let resolved = space.resolved_config(&engine.config);
    let repo_root = space.repo_root.clone();
    if resolved.ingest.auto_commit
        && (pages_deleted_from_disk > 0 || wiki_toml_updated || schema_file_deleted)
    {
        let msg = format!(
            "schema remove: {type_name}{} pages, wiki.toml={wiki_toml_updated}, schema={schema_file_deleted}",
            pages_deleted_from_disk
        );
        let _ = git::commit(&repo_root, &msg);
    }

    Ok(SchemaRemoveReport {
        pages_removed: pages_to_remove,
        pages_deleted_from_disk,
        wiki_toml_updated,
        schema_file_deleted,
        dry_run: false,
    })
}

/// Validate schema files for one type or all types; returns a list of issue strings.
pub fn schema_validate(
    engine: &EngineState,
    wiki_name: &str,
    type_name: Option<&str>,
) -> Result<Vec<String>> {
    let space = engine.space(wiki_name)?;
    let mut issues = Vec::new();

    if let Some(name) = type_name {
        // Validate single type
        if !space.type_registry.is_known(name) {
            bail!("type '{name}' is not registered");
        }
        let schema_path = space
            .type_registry
            .schema_path(name)
            .ok_or_else(|| anyhow::anyhow!("no schema path for type '{name}'"))?;
        let full_path = space.repo_root.join(schema_path);
        validate_schema_file(&full_path, &mut issues);
    } else {
        // Validate all schemas
        let schemas_dir = space.repo_root.join("schemas");
        if schemas_dir.is_dir() {
            let mut entries: Vec<_> = std::fs::read_dir(&schemas_dir)?
                .filter_map(|e| e.ok())
                .filter(|e| e.path().extension().and_then(|ext| ext.to_str()) == Some("json"))
                .collect();
            entries.sort_by_key(|e| e.file_name());
            for entry in entries {
                validate_schema_file(&entry.path(), &mut issues);
            }
        }
    }

    // Index resolution check
    match space_builder::build_space(&space.repo_root, "en_stem") {
        Ok(_) => {}
        Err(e) => issues.push(format!("index resolution failed: {e}")),
    }

    Ok(issues)
}

fn validate_schema_file(path: &Path, issues: &mut Vec<String>) {
    let filename = path.file_name().unwrap_or_default().to_string_lossy();

    let content = match std::fs::read_to_string(path) {
        Ok(c) => c,
        Err(e) => {
            issues.push(format!("{filename}: cannot read: {e}"));
            return;
        }
    };

    let schema: serde_json::Value = match serde_json::from_str(&content) {
        Ok(v) => v,
        Err(e) => {
            issues.push(format!("{filename}: invalid JSON: {e}"));
            return;
        }
    };

    if let Err(e) = jsonschema::Validator::new(&schema) {
        issues.push(format!("{filename}: invalid JSON Schema: {e}"));
        return;
    }

    if schema.get("x-wiki-types").is_none() {
        issues.push(format!(
            "{filename}: missing x-wiki-types (types won't be discovered)"
        ));
    }
}

fn generate_template(schema: &serde_json::Value, type_name: &str) -> String {
    let required: Vec<&str> = schema
        .get("required")
        .and_then(|v| v.as_array())
        .map(|arr| arr.iter().filter_map(|v| v.as_str()).collect())
        .unwrap_or_default();

    let properties = schema
        .get("properties")
        .and_then(|v| v.as_object())
        .cloned()
        .unwrap_or_default();

    let mut lines = vec!["---".to_string()];

    // Required fields first
    for field in &required {
        if let Some(prop) = properties.get(*field) {
            lines.push(format_template_field(field, prop, type_name));
        }
    }

    // Common optional fields
    for field in &["summary", "status", "last_updated", "tags"] {
        if !required.contains(field)
            && let Some(prop) = properties.get(*field)
        {
            lines.push(format_template_field(field, prop, type_name));
        }
    }

    lines.push("---".to_string());
    lines.join("\n")
}

fn format_template_field(name: &str, prop: &serde_json::Value, type_name: &str) -> String {
    let prop_type = prop
        .get("type")
        .and_then(|v| v.as_str())
        .unwrap_or("string");

    match prop_type {
        "array" => {
            if name == "read_when" || name == "tags" {
                format!("{name}:\n  - \"\"")
            } else {
                format!("{name}: []")
            }
        }
        "string" => {
            if name == "type" {
                format!("type: {type_name}")
            } else if name == "status" {
                "status: active".to_string()
            } else if name == "last_updated" {
                format!(
                    "last_updated: \"{}\"",
                    chrono::Utc::now().format("%Y-%m-%d")
                )
            } else {
                format!("{name}: \"\"")
            }
        }
        "boolean" => format!("{name}: false"),
        _ => format!("{name}: \"\""),
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn default_schemas_map_has_concept() {
        let schemas = crate::default_schemas::default_schemas();
        assert!(
            schemas.contains_key("concept.json"),
            "embedded concept.json missing"
        );
        let content = schemas["concept.json"];
        assert!(
            content.contains("\"concept\""),
            "concept.json lacks type identifier"
        );
    }
}