ask-please 0.4.8

Agent Skills Kit — download version-specific library docs for AI coding agents (Rust port of @pleaseai/ask).
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
445
446
447
448
449
450
451
452
453
454
//! Project-local docs materialization + the `ask list` view. Rust port of
//! `packages/cli/src/storage.ts`.
//!
//! `save_docs` writes (or links/refs) the fetched docs under
//! `.ask/docs/<name>@<version>/`; `list_docs` joins `ask.json` (intent) with
//! `.ask/resolved.json` (last materialization) for `ask list`.

use std::path::{Path, PathBuf};

pub use crate::ask_json::StoreMode;
use crate::io::{empty_resolved, get_ask_dir, read_ask_json, read_resolved_json};
use crate::resolved::{EntryFormat, Materialization};
use crate::sources::DocFile;
use crate::spec::library_name_from_spec;

/// Options for [`save_docs`].
#[derive(Debug, Clone, Default)]
pub struct SaveDocsOptions {
    pub store_mode: StoreMode,
    pub store_path: Option<PathBuf>,
    /// Relative docs subpath inside `store_path` (github `docsPath`). Link/ref
    /// modes join this so the symlink/ref target is the docs dir, not the repo
    /// root.
    pub store_subpath: Option<String>,
}

pub fn get_docs_dir(project_dir: &Path) -> PathBuf {
    get_ask_dir(project_dir).join("docs")
}

pub fn get_library_docs_dir(project_dir: &Path, name: &str, version: &str) -> PathBuf {
    get_docs_dir(project_dir).join(format!("{name}@{version}"))
}

fn effective_store_path(store_path: &Path, store_subpath: Option<&str>) -> PathBuf {
    match store_subpath.filter(|s| !s.is_empty()) {
        Some(sub) => store_path.join(sub),
        None => store_path.to_path_buf(),
    }
}

/// Materialize docs into the project. Returns the path AGENTS.md should point at.
pub fn save_docs(
    project_dir: &Path,
    name: &str,
    version: &str,
    files: &[DocFile],
    options: &SaveDocsOptions,
) -> anyhow::Result<PathBuf> {
    let docs_dir = get_library_docs_dir(project_dir, name, version);

    // ref mode: no project-local materialization.
    if options.store_mode == StoreMode::Ref {
        return Ok(match &options.store_path {
            Some(sp) => effective_store_path(sp, options.store_subpath.as_deref()),
            None => docs_dir,
        });
    }

    // link mode: symlink project-local → store (docs subdir).
    if options.store_mode == StoreMode::Link {
        if let Some(sp) = &options.store_path {
            let link_target = effective_store_path(sp, options.store_subpath.as_deref());
            // Use symlink_metadata (examines the link itself), not exists (which
            // follows it): a DANGLING symlink — left behind when `ask cache gc`
            // deletes the store target — reports exists()==false, so the cleanup
            // would be skipped and the following symlink_dir would fail EEXIST with
            // a confusing "Failed to create symlink". remove_dir_all removes the
            // link itself (verified for both live and dangling links), so the stale
            // link is cleanly replaced.
            if docs_dir.symlink_metadata().is_ok() {
                std::fs::remove_dir_all(&docs_dir)?;
            }
            if let Some(parent) = docs_dir.parent() {
                std::fs::create_dir_all(parent)?;
            }
            match symlink_dir(&link_target, &docs_dir) {
                Ok(()) => return Ok(docs_dir),
                Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => {
                    // Clean up partial state, then fall through to copy.
                    let _ = std::fs::remove_dir_all(&docs_dir);
                    eprintln!(
                        "  Symlink creation failed ({}), falling back to copy mode",
                        e.kind()
                    );
                }
                Err(e) => {
                    let _ = std::fs::remove_dir_all(&docs_dir);
                    anyhow::bail!("Failed to create symlink at {}: {e}", docs_dir.display());
                }
            }
        }
    }

    // copy mode (default).
    if docs_dir.exists() {
        std::fs::remove_dir_all(&docs_dir)?;
    }
    std::fs::create_dir_all(&docs_dir)?;
    for file in files {
        let file_path = docs_dir.join(&file.path);
        if let Some(parent) = file_path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        std::fs::write(&file_path, &file.content)?;
    }
    let index = files
        .iter()
        .map(|f| format!("- [{}](./{})", f.path, f.path))
        .collect::<Vec<_>>()
        .join("\n");
    std::fs::write(
        docs_dir.join("INDEX.md"),
        format!("# {name}@{version} Documentation\n\n{index}\n"),
    )?;

    Ok(docs_dir)
}

#[cfg(unix)]
fn symlink_dir(target: &Path, link: &Path) -> std::io::Result<()> {
    std::os::unix::fs::symlink(target, link)
}

#[cfg(not(unix))]
fn symlink_dir(target: &Path, link: &Path) -> std::io::Result<()> {
    std::os::windows::fs::symlink_dir(target, link)
}

/// Remove project-local docs for `name` (all versions when `version` is `None`).
pub fn remove_docs(project_dir: &Path, name: &str, version: Option<&str>) -> anyhow::Result<()> {
    if let Some(version) = version {
        let docs_dir = get_library_docs_dir(project_dir, name, version);
        if docs_dir.exists() {
            std::fs::remove_dir_all(&docs_dir)?;
        }
        return Ok(());
    }
    let base = get_docs_dir(project_dir);
    if !base.exists() {
        return Ok(());
    }
    let prefix = format!("{name}@");
    for entry in std::fs::read_dir(&base)? {
        let entry = entry?;
        if entry.file_name().to_string_lossy().starts_with(&prefix) {
            std::fs::remove_dir_all(entry.path())?;
        }
    }
    Ok(())
}

/// Source classification for a list entry.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ListSource {
    PmDriven,
    Github,
    Unresolved,
}

/// One `ask list` row, joined from `ask.json` + `.ask/resolved.json`. Entries
/// declared but never installed surface as `version: "unresolved"`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListDocsEntry {
    pub name: String,
    pub version: String,
    pub format: EntryFormat,
    pub source: ListSource,
    pub spec: String,
    pub location: String,
    pub file_count: u64,
}

pub fn list_docs(project_dir: &Path) -> Vec<ListDocsEntry> {
    let Ok(Some(ask_json)) = read_ask_json(project_dir) else {
        return Vec::new();
    };
    // `ask list` is a display-only, infallible lister (it already returns empty
    // on an unreadable ask.json). It performs no rewrites, so masking a resolved
    // read error here can't trigger the incorrect-rewrite bug the read_resolved_json
    // change guards against — keep the infallible signature with an empty fallback.
    let resolved = read_resolved_json(project_dir).unwrap_or_else(|_| empty_resolved());

    let mut out = Vec::new();
    for entry in &ask_json.libraries {
        let spec = entry.spec().to_string();
        let name = library_name_from_spec(&spec);
        let source_kind = if spec.starts_with("github:") {
            ListSource::Github
        } else {
            ListSource::PmDriven
        };

        let Some(cached) = resolved.entries.get(&name) else {
            out.push(ListDocsEntry {
                name,
                version: "unresolved".into(),
                format: EntryFormat::Docs,
                source: ListSource::Unresolved,
                spec,
                location: "(not installed — run `ask install`)".into(),
                file_count: 0,
            });
            continue;
        };

        // intent-skills format: docs live in node_modules; the skill listing is
        // sourced from the AGENTS.md intent block (agents-intent, deferred).
        if cached.format == Some(EntryFormat::IntentSkills) {
            out.push(ListDocsEntry {
                name,
                version: cached.resolved_version.clone(),
                format: EntryFormat::IntentSkills,
                source: source_kind,
                spec: spec.clone(),
                location: format!("node_modules/{}", pkg_from_spec(&spec)),
                file_count: 0,
            });
            continue;
        }

        if cached.materialization == Some(Materialization::InPlace) {
            if let Some(in_place) = &cached.in_place_path {
                out.push(ListDocsEntry {
                    name,
                    version: cached.resolved_version.clone(),
                    format: EntryFormat::Docs,
                    source: source_kind,
                    spec,
                    location: in_place.clone(),
                    file_count: cached.file_count,
                });
                continue;
            }
        }

        let docs_dir = get_library_docs_dir(project_dir, &name, &cached.resolved_version);
        let file_count = if docs_dir.exists() {
            count_files(&docs_dir)
        } else {
            cached.file_count
        };
        let location = pathdiff_relative(project_dir, &docs_dir)
            .unwrap_or_else(|| docs_dir.to_string_lossy().into_owned());
        out.push(ListDocsEntry {
            name,
            version: cached.resolved_version.clone(),
            format: EntryFormat::Docs,
            source: source_kind,
            spec,
            location,
            file_count,
        });
    }
    out.sort_by(|a, b| a.name.cmp(&b.name));
    out
}

fn pkg_from_spec(spec: &str) -> &str {
    match spec.find(':') {
        Some(i) => &spec[i + 1..],
        None => spec,
    }
}

/// Path of `target` relative to `base` (forward-slash), or `None` when `target`
/// is not under `base`.
fn pathdiff_relative(base: &Path, target: &Path) -> Option<String> {
    let rel = target.strip_prefix(base).ok()?;
    if rel.as_os_str().is_empty() {
        return None;
    }
    Some(
        rel.components()
            .map(|c| c.as_os_str().to_string_lossy())
            .collect::<Vec<_>>()
            .join("/"),
    )
}

fn count_files(dir: &Path) -> u64 {
    let mut count = 0;
    let Ok(entries) = std::fs::read_dir(dir) else {
        return 0;
    };
    for entry in entries.flatten() {
        match entry.file_type() {
            Ok(t) if t.is_dir() => count += count_files(&entry.path()),
            Ok(_) => count += 1,
            Err(_) => {}
        }
    }
    count
}

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

    fn df(path: &str, content: &str) -> DocFile {
        DocFile {
            path: path.into(),
            content: content.into(),
        }
    }

    #[test]
    fn save_copy_writes_files_and_index() {
        let dir = tempfile::tempdir().unwrap();
        let out = save_docs(
            dir.path(),
            "acme",
            "1.0.0",
            &[df("a.md", "A"), df("sub/b.md", "B")],
            &SaveDocsOptions::default(),
        )
        .unwrap();
        assert!(out.ends_with(".ask/docs/acme@1.0.0"));
        assert_eq!(std::fs::read_to_string(out.join("a.md")).unwrap(), "A");
        assert_eq!(std::fs::read_to_string(out.join("sub/b.md")).unwrap(), "B");
        let index = std::fs::read_to_string(out.join("INDEX.md")).unwrap();
        assert!(index.contains("# acme@1.0.0 Documentation"));
        assert!(index.contains("- [a.md](./a.md)"));
    }

    #[test]
    fn save_copy_replaces_existing() {
        let dir = tempfile::tempdir().unwrap();
        save_docs(
            dir.path(),
            "acme",
            "1.0.0",
            &[df("old.md", "x")],
            &SaveDocsOptions::default(),
        )
        .unwrap();
        let out = save_docs(
            dir.path(),
            "acme",
            "1.0.0",
            &[df("new.md", "y")],
            &SaveDocsOptions::default(),
        )
        .unwrap();
        assert!(!out.join("old.md").exists());
        assert!(out.join("new.md").exists());
    }

    #[test]
    fn ref_mode_returns_effective_store_path() {
        let dir = tempfile::tempdir().unwrap();
        let store = dir.path().join("store/github.com/o/r/v1");
        let out = save_docs(
            dir.path(),
            "acme",
            "1.0.0",
            &[],
            &SaveDocsOptions {
                store_mode: StoreMode::Ref,
                store_path: Some(store.clone()),
                store_subpath: Some("docs".into()),
            },
        )
        .unwrap();
        assert_eq!(out, store.join("docs"));
        // Nothing was materialized locally.
        assert!(!get_library_docs_dir(dir.path(), "acme", "1.0.0").exists());
    }

    #[cfg(unix)]
    #[test]
    fn link_mode_creates_symlink_to_subpath() {
        let dir = tempfile::tempdir().unwrap();
        let store = dir.path().join("store");
        std::fs::create_dir_all(store.join("docs")).unwrap();
        std::fs::write(store.join("docs/guide.md"), "G").unwrap();
        let out = save_docs(
            dir.path(),
            "acme",
            "1.0.0",
            &[],
            &SaveDocsOptions {
                store_mode: StoreMode::Link,
                store_path: Some(store.clone()),
                store_subpath: Some("docs".into()),
            },
        )
        .unwrap();
        assert!(out.symlink_metadata().unwrap().file_type().is_symlink());
        assert_eq!(std::fs::read_to_string(out.join("guide.md")).unwrap(), "G");
    }

    #[test]
    fn remove_docs_by_version_and_all() {
        let dir = tempfile::tempdir().unwrap();
        save_docs(
            dir.path(),
            "acme",
            "1.0.0",
            &[df("a.md", "A")],
            &SaveDocsOptions::default(),
        )
        .unwrap();
        save_docs(
            dir.path(),
            "acme",
            "2.0.0",
            &[df("a.md", "A")],
            &SaveDocsOptions::default(),
        )
        .unwrap();
        remove_docs(dir.path(), "acme", Some("1.0.0")).unwrap();
        assert!(!get_library_docs_dir(dir.path(), "acme", "1.0.0").exists());
        assert!(get_library_docs_dir(dir.path(), "acme", "2.0.0").exists());
        remove_docs(dir.path(), "acme", None).unwrap();
        assert!(!get_library_docs_dir(dir.path(), "acme", "2.0.0").exists());
    }

    #[test]
    fn list_docs_empty_without_ask_json() {
        let dir = tempfile::tempdir().unwrap();
        assert!(list_docs(dir.path()).is_empty());
    }

    #[test]
    fn list_docs_marks_unresolved_and_resolved() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(get_ask_dir(dir.path())).unwrap();
        std::fs::write(
            crate::io::get_ask_json_path(dir.path()),
            r#"{"libraries":["npm:acme","github:o/other"]}"#,
        )
        .unwrap();
        // Only acme is resolved.
        std::fs::write(
            crate::io::get_resolved_json_path(dir.path()),
            r#"{"schemaVersion":1,"generatedAt":"2026-01-01T00:00:00Z","entries":{
              "acme":{"spec":"npm:acme","resolvedVersion":"1.2.3",
                "contentHash":"sha256-0000000000000000000000000000000000000000000000000000000000000000",
                "fetchedAt":"2026-01-01T00:00:00Z","fileCount":3}}}"#,
        )
        .unwrap();

        let entries = list_docs(dir.path());
        assert_eq!(entries.len(), 2);
        // Sorted by name: "acme" before "other".
        assert_eq!(entries[0].name, "acme");
        assert_eq!(entries[0].version, "1.2.3");
        assert_eq!(entries[0].source, ListSource::PmDriven);
        assert_eq!(entries[1].name, "other");
        assert_eq!(entries[1].version, "unresolved");
        assert_eq!(entries[1].source, ListSource::Unresolved);
    }
}