> ## Documentation Index
> Fetch the complete documentation index at: https://docs.expectedparrot.com/llms.txt
> Use this file to discover all available pages before exploring further.

# FileTree

> Capture a directory in a portable .ep package and restore a saved revision.

Use a `FileTree` to keep a document with its illustrations, a skill with its
scripts, or any set of files that refer to each other by relative path.

For the bundled agent skill, run `ep skills show edsl-file-trees --human`.

```bash theme={null}
ep file-trees create drafts/post -o post.ep -m "First draft"
# Edit the source directory, then save a second revision.
ep file-trees create drafts/post -o post.ep -m "Replace the figure"
ep log post.ep
ep file-trees materialize post.ep --ref HEAD~1 -o preview/first-draft
ep file-trees materialize post.ep -o preview/current
```

```python theme={null}
from pathlib import Path
from edsl import FileTree

post = FileTree.from_directory("drafts/post")
post.save("post.ep")
current = FileTree.load("post.ep")
print(list(current))
print(current["report.md"])

# Assemble a snapshot from individual files as well.
snippet = FileTree({"README.md": "# Example\n", "src/main.py": Path("main.py")})
snippet.materialize("preview/snippet")
```

## Use a FileTree in a model question

Put the snapshot in a scenario and reference its field in the question text.
This example saves a review job you can inspect locally.

```python theme={null}
from edsl import FileTree, Model, QuestionFreeText, Scenario

post = FileTree.load("post.ep")
review = QuestionFreeText(
    question_name="review",
    question_text="Do the figures support the claims in this article? {{ files }}",
)
job = review.by(Scenario({"files": post})).by(Model("gpt-4o"))
job.save("review.ep")
```

```bash theme={null}
ep jobs preview-prompt review.ep review
```

Both `{{ files }}` and `{{ scenario.files }}` expand the tree at the reference.
Text appears verbatim under its relative path. Opaque files become numbered
`FileStore` attachments, and the prompt maps each number to a path. Entries
appear in path order, including files the article does not link to. Only the
first rendered reference to that field in each question expands it. A question
that omits the reference or skips it in a conditional receives none of the tree.

The Jobs package captures the selected snapshot. To review an older revision,
use `FileTree.git.load("post.ep", ref="HEAD~1")` when building the scenario.
Preview lists attachment paths, MIME types, sizes, and SHA-256 hashes. For a
post with two figures, add `--expect-files 2` to check the count. Choose a
model that accepts the file types and total input size. EDSL reports known
adapter incompatibilities with the offending relative path.

### Read a code from one image

The `examples/filetree-vision` directory in the EDSL repository contains a
two-file snapshot. In `source/note.md`, the batch is `KITE-724` and the
instruction is to read the purple card. In `source/images/cards.png`, three
colored cards each show a different five-digit number. The saved question asks
for the batch and the selected code, joined by a slash. [Download the example files](https://github.com/expectedparrot/edsl/tree/917cbeafe800de427e4b0ee9755d5f524b4e32c5/examples/filetree-vision).

![Three colored cards, each displaying a five-digit code.](https://raw.githubusercontent.com/expectedparrot/edsl/917cbeafe800de427e4b0ee9755d5f524b4e32c5/examples/filetree-vision/source/images/cards.png)

From the repository root, inspect the exact input and the recorded live answer
offline.

```bash theme={null}
cd examples/filetree-vision
ep inspect cards.ep
ep jobs preview-prompt job.ep receipt --expect-files 1 --expect-text KITE-724 --forbid-text 74923
ep results select --file results.ep --column answer.receipt
```

Preview shows one attachment named `images/cards.png`. The check verifies
that the target number is absent from the prompt text. In the recorded run,
OpenAI's `gpt-4o-mini-2024-07-18` returned **`KITE-724/74923`**, matching
the number on the purple card.

To repeat the provider call with OpenAI credentials, use a separate output
file and bypass the cache.

```bash theme={null}
ep run job.ep --local --fresh --output rerun.ep
ep results select --file rerun.ep --column answer.receipt
```

## Directory snapshots

Directory capture preserves relative paths, empty directories, executable flags,
and file contents, including line endings. It skips Git's own `.git` metadata
and rejects symlinks and special files. Choose a new or empty destination
directory when materializing.

Git tracks readable UTF-8 text and an index giving every file's SHA-256 digest
and size. The portable `.ep` keeps one copy of each asset outside Git's object
database, including images replaced in later revisions. Copying the package
preserves offline rendering and restoration. Version 1 packages remain
readable. Their next save uses the new format and preserves the original commits.

With FileTree support enabled on your Coop server, publish or retrieve the package
using the standard commands:

```bash theme={null}
ep push post.ep --alias illustrated-post --visibility private
ep clone alice/illustrated-post --path local.ep
ep pull local.ep
```

Pushing publishes the package's current revision and uploads the files every
earlier revision uses, skipping files already published for that tree. If Coop
has a newer version than your package, for example one you pushed from another
copy, `ep push` stops. Run `ep pull` first, then push again.

As with other EDSL objects, history stays with the owner. When you clone a tree
you own on a server with Git hosting, `ep clone` copies its full history and
files. Anyone else who can open the tree receives its current revision as a new
package with a single commit, and `ep pull` adds a commit whenever the published
revision changes. Either way the clone contains the file bytes and works
offline. A plain Git clone of a version 2 tree carries its text and pointers;
use `ep clone` or the portable package for the images as well.

On the Coop object page, a file tree sits beside a viewer. A root-level
`README.md` opens first, otherwise `SKILL.md`, otherwise the first root-level
`.md` or `.markdown` file. Use relative image references such as `![Cover](images/cover.webp)`.
A relative link such as `[Rubric](references/rubric.md#scoring)` opens the
linked Markdown document in the reader. Images and links in that document
resolve from its own folder. Links to other files download the published
bytes. The browser's Back button returns to the previous document.
Select any file in the tree to view it. Markdown renders as a document, PDF
and Word (`.docx`) files show their pages, CSV and TSV files show as a table,
and images and other text files display directly. Every file has a
**Download** button. The tree shows folders, including empty ones, and file
sizes.
Choose **Use in Research Agent**
to open a draft request to retrieve and inspect that FileTree by UUID.

Coop displays Expected Parrot survey respondent links as buttons. Use ordinary
Markdown link text to set the button label. For example:

```markdown theme={null}
[Give us feedback](https://www.expectedparrot.com/respond/human-surveys/3ae8df49-3fe3-498c-9944-bb0e293c05d9)
```

This displays **Give us feedback**. Use the respondent URL for your survey,
including any invitation parameters. The optional label `ep_button` displays
**Take the survey**.

To keep a UTF-8 file opaque, pass its relative path in
`blob_paths=["runs/job.json"]` to `from_directory`, or use
`--blob runs/job.json` on each CLI capture. When constructing a tree directly,
strings supply literal text, bytes supply an opaque blob, and a `Path` or EDSL
`FileStore` supplies a snapshot of its contents. JSON serialization carries
one snapshot, while a saved `.ep` carries the revision history.

## Inspect an agent skill

Capture the skill directory so its `SKILL.md`, scripts, and references share
one revision. Inspect the saved package to see the skill name and description
in `data.skill`.

```bash theme={null}
ep file-trees create my-skill -o my-skill.ep
ep inspect my-skill.ep
```

In Python, `FileTree.load("my-skill.ep").skill_metadata()` returns the full YAML
front matter, including extra fields such as `tags`. The root `SKILL.md` must
declare a non-empty string for both `name` and `description`. The method raises
`ValueError` with a correction if it cannot read those fields. For a package
with a malformed root skill file, `ep inspect` still lists its paths and
includes an `INVALID_SKILL_METADATA` warning.

## Compile Markdown to PDF

Install Pandoc and XeLaTeX on your PATH. Call `to_pdf()` to compile a Markdown
document from the loaded revision. It returns a PDF `FileStore`, and passing
an output path also writes the PDF there.

```python theme={null}
from edsl import FileTree

tree = FileTree.load("post.ep")
pdf = tree.to_pdf("post.pdf")  # One .md or .markdown file anywhere in the tree

# With several Markdown files, select one by its full tree-relative path.
previous = FileTree.git.load("post.ep", ref="HEAD~1")
pdf = previous.to_pdf("earlier.pdf", source="reports/post.md")
```

Reference figures relative to the selected Markdown file. For example,
`reports/post.md` can embed `../images/chart.png`. A missing figure raises an
error identifying the source and image. The print style supports title, author,
and date metadata, tables, captions, and syntax-highlighted code. Use trusted
documents with the local TeX toolchain. For WebP figures, install Pillow.
The PDF compiler renders a temporary PNG from the captured WebP. Pass an installed
body font with `font="Times New Roman"` when a document needs glyphs such as ○.

## Export Markdown or HTML with embedded images

Give `export_document()` a Markdown or HTML output path. It reads one Markdown
source from the loaded revision and writes its referenced images into the output
as base64 data URIs. The same source-selection rules as `to_pdf()` apply.

```python theme={null}
tree = FileTree.load("post.ep")
tree.export_document("article.md", source="reports/post.md")
tree.export_document("article.html", source="reports/post.md")
```

Both exports require Pandoc. Markdown images (including reference-style images),
HTML image tags, and responsive image candidates resolve relative to the source.
Keep external image files in the tree and reference their relative paths.
Ordinary hyperlinks remain links. Pandoc can normalize Markdown formatting.
Some Markdown hosts block data URIs for images; open the HTML export in a
browser for a portable view.

## Export a saved revision from the CLI

Use `-o` to choose a format by filename, or `--output-dir` to write all three
formats from the same revision. Omit `--source` when the tree has one Markdown
file. With several Markdown files, give the full path inside the tree.

```bash theme={null}
ep file-trees export post.ep --source reports/post.md -o article.pdf
ep file-trees export post.ep --source reports/post.md -o article.html
ep file-trees export post.ep --source reports/post.md --ref HEAD~1 --output-dir exports/
```

The last command writes `exports/post.md`, `exports/post.html`, and
`exports/post.pdf`. Each file uses the selected source's basename. PDF exports
also require XeLaTeX and accept an installed body font via `--font`.

The JSON receipt gives the selected `source`, its Git `commit`, and the path to
each file in `outputs`. Use `referenced_images` to see which local files the
source uses, and `embedded_images` to see which of those files each format
includes. For example, Markdown and HTML preserve responsive image candidates
in raw HTML, while PDF uses Markdown image nodes. An image used twice appears
once in each list. Text-only documents have empty lists. A missing local image
fails the export before any output file is replaced.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.