@particle-academy/last-word 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Particle Academy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # @particle-academy/last-word
2
+
3
+ [![Fancy UI suite](art/fancy-ui.svg)](https://particle.academy)
4
+
5
+ Zero-dependency, **isomorphic** (browser + Node) `.docx` writer + reader for
6
+ agentic word-processing documents — a JSON document model with **markdown
7
+ bridges**. The Node/TypeScript mirror of the PHP
8
+ [`particle-academy/last-word`](https://github.com/Particle-Academy/last-word).
9
+ Sister to [`holy-sheet`](https://github.com/Particle-Academy/holy-sheet-js)
10
+ (xlsx) and [`dark-slide`](https://github.com/Particle-Academy/dark-slide-js)
11
+ (pptx).
12
+
13
+ The point is the **Editor round-trip**: a WYSIWYG editor (react-fancy
14
+ `Editor`) speaks markdown; Word speaks `.docx`. LastWord bridges the two
15
+ through one JSON model — `fromMarkdown → toBytes` to export a real Word file,
16
+ `read → toMarkdown` to import one — with no converter sandwich
17
+ (mammoth/turndown/docx) in between.
18
+
19
+ ```ts
20
+ import { Agent } from "@particle-academy/last-word";
21
+
22
+ // Markdown in…
23
+ const doc = Agent.fromMarkdown(`# Q3 Report
24
+
25
+ Revenue was **up 12%** — see the [dashboard](https://example.com).
26
+
27
+ - Wins
28
+ - Enterprise renewals
29
+ - Risks
30
+ `);
31
+
32
+ // …Word file out.
33
+ const bytes: Uint8Array = Agent.toBytes(doc); // universal
34
+ await Agent.write(doc, "report.docx"); // Node only
35
+
36
+ // And back: .docx → model → markdown for the editor.
37
+ const imported = Agent.read(bytes);
38
+ const markdown = Agent.toMarkdown(imported);
39
+ ```
40
+
41
+ ## The document model
42
+
43
+ A `Doc` is `{ title?, blocks }`. Blocks are JSON-friendly discriminated
44
+ unions — exactly what an agent emits:
45
+
46
+ | Block | Shape |
47
+ | --- | --- |
48
+ | heading | `{ type: "heading", level: 1-6, runs }` |
49
+ | paragraph | `{ type: "paragraph", runs, align? }` |
50
+ | list | `{ type: "list", ordered?, items: [{ runs, children? }] }` (nesting ≥ 3 deep) |
51
+ | table | `{ type: "table", rows: [{ header?, cells: [{ blocks }] }] }` |
52
+ | code | `{ type: "code", language?, text }` |
53
+ | quote | `{ type: "quote", blocks }` |
54
+ | image | `{ type: "image", src: "data:image/png;base64,…", widthPx?, heightPx?, alt? }` |
55
+ | pageBreak | `{ type: "pageBreak" }` |
56
+ | hr | `{ type: "hr" }` |
57
+
58
+ A `Run` is an inline span: `{ text, bold?, italic?, underline?, strike?,
59
+ code?, link?, color?, highlight? }` (colors are `#RRGGBB`).
60
+
61
+ ## API
62
+
63
+ `Agent` (static) mirrors the PHP surface:
64
+
65
+ - `validate(doc)` → structured errors `{path, message}[]` (empty = valid)
66
+ - `validateAndRepair(doc)` → `{ok, schema, errors}` (coerces strings to runs,
67
+ clamps heading levels, drops unknown block types with the error retained)
68
+ - `toBytes(doc)` → `Uint8Array` (universal, deterministic output)
69
+ - `write(doc, path)` → `{path, bytes, blocks}` (Node only)
70
+ - `read(bytes)` / `fromBytes(bytes)` → `Doc` (universal; tolerates
71
+ Word-authored files — outlineLvl headings, named highlights, unknown
72
+ constructs degrade to paragraphs, never throw)
73
+ - `toMarkdown(doc)` / `fromMarkdown(md)` → the Editor bridge (GFM: headings,
74
+ `**`/`*`/`~~`, inline code, links, nested lists, tables, fenced code,
75
+ blockquotes, images, `---`)
76
+ - `describe(doc)` → plain-text summary (title, block counts, word count)
77
+ - `jsonSchema()` → JSON Schema for LLM tool-use
78
+ - `version()` → package version
79
+
80
+ Markdown is lossy only where GFM has no syntax: underline / color / highlight
81
+ decorations, paragraph alignment, image pixel sizes, and page breaks are
82
+ dropped on `toMarkdown`; everything else round-trips.
83
+
84
+ Images are embedded from data URLs (PNG/JPEG); when `widthPx`/`heightPx` are
85
+ omitted the intrinsic size is sniffed from the bytes (PNG IHDR / JPEG SOF)
86
+ and capped at 6.5in width keeping aspect.
87
+
88
+ ---
89
+
90
+ ## ⭐ Star Fancy UI
91
+
92
+ If this package is useful to you, a quick ⭐ on the repo really helps us build a better kit. Thank you!