Skip to content

Export dialogs as structured cells and typed parts - #17

Open
jackhogan wants to merge 1 commit into
mainfrom
export-cells
Open

jackhogan wants to merge 1 commit into
mainfrom
export-cells

Conversation

@jackhogan

Copy link
Copy Markdown

This is the aihist part of AnswerDotAI/mdhtml#98, replacing #9 and #11 (and replacing AnswerDotAI/aidialog#71 and AnswerDotAI/aidialog#72, since aidialog no longer needs to change).

Previously dlg2md unpacked cells, replies, outputs, and attachments only to flatten them straight into one Markdown string, so things like octavo or solveit sharing that needed cell boundaries or metadata had to re-parse that Markdown or rebuild something similar. This PR exposes the structured cell list directly and turns dlg2md into a thin join over it.

The idea is to keep aihist focused on extracting what's in the notebook and leave filtering and presentation to callers.

Implementation details and design decisions (AI-written)

Structured cell projection (src/export.rs)

export_nb(nb, opts) and export_cell(nb, cell, language, weave) are now public and re-exported from src/lib.rs under the full feature:

  • ExportOpts accepts language (None), exportfilter (false), weave (false), frontmatter (true), and att_data (true).
  • ExportDoc returns frontmatter, resolved language (opts.language, then metadata.kernelspec.language, then metadata.language_info.name, defaulting to "python"), and cells.
  • ExportCell holds id, kind, src, out, attachments, and full cell meta. Empty code cells are skipped (export_cell returns None). attachment: references remain intact in src and out while cell attachments populate attachments (with base64 whitespace stripped, and data omitted when att_data is false).
  • ExportPart serializes with a lowercase type tag across md, code, text, html (with optional text), image (with optional text), tool, and usage.

When frontmatter is enabled, export_nb extracts leading YAML frontmatter from cell 0 if it is a raw or note cell starting with ---, terminated by --- or ..., and containing at least one YAML key-value line. Frontmatter is normalized to end with \n and extracted before exportfilter runs. Any Markdown after the closing delimiter in a note cell stays as cell 0.

Typed output and reply parts (src/output.rs, src/reply.rs)

output_parts(outputs, html1st) exposes OutputPart (Text, Md, Html, Image) under #[cfg(feature = "full")]. merge_streams merges only consecutive stdout, stderr, or markdown stream entries so streams interleaved with display_data or execute_result preserve notebook order. Html and Image retain ANSI-stripped text/plain in text. render_md now formats over output_parts.

reply_parts(s) exposes ReplyPart (Md, Tool, Usage), keeping invalid JSON fences inside Md. Module layering remains bottom-up: output.rs and reply.rs do not import export.rs.

Flat Markdown joins and attachment inlining

dlg2md_nb and msg2md_in join the output of export_nb and export_cell through parts_to_md and cell_to_md. inline_atts replaces attachment: references longest-name-first for both raw and percent-encoded names, avoiding prefix collisions such as a.png matching inside a.png2.

Python bindings (py/src/lib.rs, python/aihist/hist.py)

py/src/lib.rs exposes export_nb(nb, opts="{}"), and aihist.hist exports export_dlg(d, *, exportfilter=False, weave=False, language=None, frontmatter=True, att_data=True).

_nb(d) now accepts raw notebook dicts directly alongside Dialog objects and message iterables, so export_dlg and dlg2md work without wrapping dicts in aidialog.

Compatibility and dependencies

Adds no new Rust or Python dependencies and leaves aidialog unchanged. Building with --no-default-features keeps serde derives out of the minimal build used by jupygate.

Python dlg2md keeps language='python' by default; passing language=None opts into kernelspec inference. A leading raw YAML frontmatter cell now renders as bare frontmatter in dlg2md_nb rather than a fenced raw block and survives exportfilter=True.

Companion PRs: mdhtml PR, draft octavo PR.

Documentation and validation

Updated README.md and DEV.md, and added tests in tests/aihist.rs and tests/test_hist.py covering typed parts, att_data, meta, HTML and image text fallbacks, language inference, exportfilter, frontmatter=False, interleaved stream ordering, longest-first attachment inlining, and raw dict input.

  • cargo check --no-default-features and cargo test --no-default-features: 5 passed.
  • cargo clippy --workspace --all-targets: 0 new warnings.
  • cargo develop and cargo test: 5 passed.
  • pytest -q: 12 passed.

@jackhogan jackhogan self-assigned this Nov 26, 2026
@jackhogan jackhogan added the enhancement New feature or request label Nov 26, 2026
@jackhogan
jackhogan requested a review from jph00 November 26, 2026 23:47

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant