Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ship-rs-build
The root package is a pure Rust crate, published to crates.io. Rust users who depend on it never build PyO3. `default = ["full"]` enables all modules; `default-features = false` exposes `output` (core `render`/`strip_ansi`/`keep_ends`/MIME helpers) and `paths` (`realpath`) with only `serde_json`. Its modules are:

- `cells`: one notebook cell as a dialog message, and as a model reads it
- `export`: Markdown documents (`msg2md`, `dlg2md`), plain-text message parts (`msg2text`), and Python scripts (`msgs2py`)
- `export`: structured typed cell projection (`export_nb`, `export_cell`), Markdown documents (`msg2md`, `dlg2md`), plain-text message parts (`msg2text`), and Python scripts (`msgs2py`)
- `input`: a prompt's model input, with the examples, CRAFTs, `$` values and warnings that a host supplies
- `output`: code outputs and tool results as a model reads them
- `media`: linked-media loading, MIME detection, resizing images, and media tags
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ A saved reply is Markdown, with each tool call and its result in a fenced `json

`dlg2chat` turns a dialog into chat messages, with each reply's tool calls as real parts, and `chat2dlg` turns chat messages back into a dialog. `reply2dlg` turns one reply into a dialog of notes and code that calls the tools, which `dlg2reply` turns back into the reply. `hist_xml`, `to_media` and `to_parts` give one message as a model reads it.

`dlg2md` writes a dialog as one Markdown document: notes as they are, code in language fences with its outputs in `::: output` divs, and each prompt and its reply in `::: prompt` and `::: reply` divs. `msg2md` writes one message. Both accept `language='python'` and inline stored attachments referenced by notes, prompts and replies; ordinary file links remain links.
`export_dlg` projects a dialog or notebook dict into `{frontmatter, language, cells}` structured typed parts (preserving cell IDs, metadata, types, typed source/output parts including Markdown, raw code, plain-text/HTML/image outputs with plain-text fallbacks, separated prompt-reply tool and usage blocks, and stored attachment maps) with `exportfilter`, `weave`, `frontmatter`, `att_data`, and kernelspec `language` inference. `dlg2md` joins those parts into one Markdown document (with stored attachments inlined, tools folded, code outputs in `::: output` divs, and prompts/replies in `::: prompt`/`::: reply` divs), and `msg2md` writes one message; ordinary file links remain links.

`msg2text(m, part='input')` gives the source, or `part='output'` gives text output with prompt tools removed. `msgs2py(msgs)` writes nbdev-marked code; `full=True` includes all code, commenting notes, prompts, replies and outputs. These conversions read saved messages without fetching linked files or changing the dialog.

Expand Down
6 changes: 6 additions & 0 deletions py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ fn msg2md(nb: &str, language: &str, weave: bool) -> PyResult<Option<String>> {
Ok(export::msg2md_in(Some(&nb), cell, language, weave))
}

/// Project a notebook JSON into `{frontmatter, language, cells}` structured typed parts
#[pyfunction]
#[pyo3(signature = (nb, opts="{}"))]
fn export_nb(nb: &str, opts: &str) -> PyResult<String> { Ok(ser(export::export_nb(&de(nb)?, &de(opts)?))) }

/// A notebook JSON as one Markdown document
#[pyfunction]
#[pyo3(signature = (nb, language="python", exportfilter=false, weave=false))]
Expand Down Expand Up @@ -292,6 +297,7 @@ fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(usage_md, m)?)?;
m.add_function(wrap_pyfunction!(fmt_tools, m)?)?;
m.add_function(wrap_pyfunction!(msg2md, m)?)?;
m.add_function(wrap_pyfunction!(export_nb, m)?)?;
m.add_function(wrap_pyfunction!(dlg2md, m)?)?;
m.add_function(wrap_pyfunction!(msg2text, m)?)?;
m.add_function(wrap_pyfunction!(msgs2py, m)?)?;
Expand Down
17 changes: 14 additions & 3 deletions python/aihist/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
__all__ = ['UNSUPPORTED_MSG', 'im_max', 'IMG_TOKS', 'jwrap', 'media_item', 'resize_img', 'prep_img', 'pdf_pages', 'count_tokens',
'output_parts', 'merge_media', 'tool_result', 'get_refs', 'get_exprs', 'is_nameerr', 'vars_tag', 'vars_hist', 'task_tags', 'warning_tag',
'cell_turn', 'cell_media', 'cell_reply', 'is_shown', 'references', 'hist_xml', 'to_media', 'to_parts', 'tool_call', 'build', 'dlg2hist', 'reply2chat', 'dlg2chat', 'chat2dlg', 'reply2dlg',
'dlg2reply', 'msg2md', 'dlg2md', 'msg2text', 'msgs2py']
'dlg2reply', 'export_dlg', 'msg2md', 'dlg2md', 'msg2text', 'msgs2py']

UNSUPPORTED_MSG,im_max,IMG_TOKS = _core.UNSUPPORTED,_core.IM_MAX,_core.IMG_TOKENS

Expand Down Expand Up @@ -286,7 +286,18 @@ def dlg2reply(sub):
msgs += [Msg('assistant', [ToolUse(id=tr.id, name=tr.name, arguments=tr.arguments)]), Msg('tool', [tr])]
return write_reply(msgs)

def _nb(d): return get_ipynb(d) if isinstance(d, Dialog) else msgs2ipynb(list(d))
def _nb(d): return d if isinstance(d, dict) else get_ipynb(d) if isinstance(d, Dialog) else msgs2ipynb(list(d))

def export_dlg(d,
*, exportfilter:bool=False, # Keep only the messages `export_filter` selects?
weave:bool=False, # Code messages contribute only `render_text` over their outputs, as document prose?
language:str=None, # Kernel language for code parts (inferred from notebook metadata when None, else 'python')
frontmatter:bool=True, # Extract a leading raw/note YAML frontmatter cell?
att_data:bool=True, # Include base64 payloads in `cell['attachments']` (False keeps only MIME types)?
):
"Project a dialog or notebook dict into `{frontmatter, language, cells}` structured typed parts"
opts = json.dumps(dict(language=language, exportfilter=exportfilter, weave=weave, frontmatter=frontmatter, att_data=att_data))
return json.loads(_core.export_nb(json.dumps(_nb(d)), opts))

def msg2md(m,
weave:bool=False, # Code messages contribute only `render_text` over their outputs, as document prose?
Expand All @@ -301,7 +312,7 @@ def dlg2md(d,
*, language:str='python', # Kernel language for code fences
):
"The dialog as one Markdown document"
return _core.dlg2md(json.dumps(_nb(d)), language, exportfilter, weave)
return _core.dlg2md(json.dumps(_nb(d)), language or '', exportfilter, weave)

def msg2text(m, part='input'):
"Input source or plain-text output for copying one message part"
Expand Down
Loading
Loading