Improved reading and writing large Office files without copying them into memory (#1243)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-28 23:03:30 +01:00
1 parent 918572c158
commit dbe609a9a7
37 files changed
+1161 -471

No files matched your search

@@ -8,18 +8,22 @@ to `app/AGENTS.md`, which apply here as well.
## How a presentation comes about
1. `PresentationTemplate.Read` reads a .pptx or .potx with the Open XML SDK (`DocumentFormat.OpenXml`):
its layouts, grouped into families of color variants, and the slides it carries. Without a template,
`PresentationTemplate.CreateDefault` stands in, so that one prompt and one check serve both cases.
its layouts, grouped into families of color variants, and the slides it carries. `ReadFile` reads a file
through a `PackageSkeleton` from `app/MindWork AI Studio/Tools/OpenXml/`, see "No copy of a file in memory".
Without a template, `PresentationTemplate.CreateDefault` stands in, so that one prompt and one check serve
both cases.
2. `ToCatalogPrompt` and `ToTemplateSlidesPrompt` describe the layouts and the chosen template slides to
the model in words: where each slot sits, what a label belongs to, and how much text fits.
3. `SlideDeckSpec.TryParse` reads the JSON answer of the model leniently.
4. `SlideDeckSpecValidator` checks the deck against the template. Its issues are written for the model,
which gets one repair round.
5. `SlideDeckSpec.ToMarkdownOutline` shows the result as an outline, and `PresentationWriter.WriteAsync`
writes the file: a copy of the template, with its slides replaced. `PresentationFile.CreateAsync` makes
that copy in the temporary folder and streams it into the target once it is complete, both with
`TemplateCopy` from `app/MindWork AI Studio/Tools/OpenXml/`, which every Office document written from
a template shares.
5. `SlideDeckSpec.ToMarkdownOutline` shows the result as an outline, and `PresentationWriter.Write`
writes the file: the template, with its slides replaced. `PresentationFile.CreateAsync` lets the writer
edit a `PackageSkeleton` of the template, which holds only its XML, loads the pictures one by one into
files of their own (`StagedPictures`), and has the skeleton write the presentation into a `StagedDocument`,
which `TargetFile` streams into the target once it is complete. `PackageSkeleton`, `StagedDocument`, and
`TargetFile` live in `app/MindWork AI Studio/Tools/OpenXml/` and serve every Office document AI Studio
reads or writes.
## Rules which are easy to break
@@ -65,19 +69,25 @@ to `app/AGENTS.md`, which apply here as well.
remains. Only empty picture placeholders stay when the user asks for it
(`PresentationWriteOptions.KeepEmptyPicturePlaceholders`), for pictures of their own.
- **No copy of a file in memory.** Templates of organizations carry pictures and videos and easily reach
a hundred megabytes, and AI Studio also runs on small devices. Read templates from their file, and
let the writer edit a copy on disk; a `MemoryStream` with the whole file would hold it once more. The
Open XML SDK saves the copy in the update mode of ZipArchive, which loads every entry behind the first
changed one into memory, compressed, e.g., the pictures and videos behind the first slide. The Slide
Assistant accepts that because it adds and removes parts; a writer which only changes the XML of existing
parts writes with `PackageRewrite` instead, as the document segments do.
hundreds of megabytes, and AI Studio also runs on small devices. Never open a template with the Open XML
SDK directly: the SDK copies a file it opens for reading only into the temporary folder, as a whole, and
that folder lives in memory on many Linux systems; it saves a file in the update mode of ZipArchive, which
loads every entry behind the first changed one into memory, e.g., the pictures and videos behind the first
slide. Read and write through a `PackageSkeleton` instead, which holds only the XML of the template, and
never hold the whole file in a `MemoryStream`. A new picture leaves its image part empty in the skeleton,
and the skeleton takes it from its file when it writes the presentation (`StagedPictures.Place`); only one
picture is in memory at a time.
- **Write only into the file the user chose.** Inside the Flatpak, the save dialog grants access to
exactly that file, so nothing may be created, renamed, or moved next to it. Temporary files go into
the temporary folder, and the finished presentation is written over the chosen file, never moved there.
`TemplateCopy` keeps both rules, and `TemplateFile.OpenRead` opens a template which PowerPoint still
shows; use them rather than a `FileStream` of your own.
- **Removing a shape removes what only it used:** its image, chart, or diagram parts, and the animations
of its slide, see `PresentationWriter.RemoveShape`. Otherwise PowerPoint offers to repair the file.
exactly that file, so nothing may be created, renamed, or moved next to it. Staged files go below the
data directory (`StagedDocument`), and the finished presentation is written over the chosen file, never
moved there (`TargetFile`). `TemplateFile.OpenRead` opens a template which PowerPoint still shows; use
these rather than a `FileStream` of your own.
- **Removing a shape removes what only it used:** its image, chart, or diagram parts, its references to
videos, sounds, and links, and the animations of its slide, see `PresentationWriter.RemoveShape`.
Otherwise PowerPoint offers to repair the file, or a video nobody sees stays in it.
- **Removing a template slide removes the links to it,** see `PresentationWriter.UnlinkRemovedSlides`. As long as
a kept slide, e.g., a table of contents, links to a removed slide, the Open XML SDK keeps that slide in the file,
outside the list of slides, with all its pictures and videos.
## Tests