@folijs/agent 0.3.3 → 0.4.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/CHANGELOG.md +19 -0
- package/README.md +124 -21
- package/dist/changes.d.ts +23 -0
- package/dist/changes.d.ts.map +1 -0
- package/dist/changes.js +136 -0
- package/dist/changes.js.map +1 -0
- package/dist/formatting.d.ts +24 -0
- package/dist/formatting.d.ts.map +1 -0
- package/dist/formatting.js +106 -0
- package/dist/formatting.js.map +1 -0
- package/dist/handles.js +32 -12
- package/dist/handles.js.map +1 -1
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/inserts.d.ts +42 -0
- package/dist/inserts.d.ts.map +1 -0
- package/dist/inserts.js +242 -0
- package/dist/inserts.js.map +1 -0
- package/dist/operations.d.ts.map +1 -1
- package/dist/operations.js +52 -27
- package/dist/operations.js.map +1 -1
- package/dist/paragraphs.d.ts +16 -0
- package/dist/paragraphs.d.ts.map +1 -0
- package/dist/paragraphs.js +146 -0
- package/dist/paragraphs.js.map +1 -0
- package/dist/read.d.ts +15 -1
- package/dist/read.d.ts.map +1 -1
- package/dist/read.js +138 -6
- package/dist/read.js.map +1 -1
- package/dist/schema.d.ts +25 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +51 -0
- package/dist/schema.js.map +1 -0
- package/dist/session.d.ts +79 -2
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +141 -16
- package/dist/session.js.map +1 -1
- package/dist/stories.d.ts +2 -0
- package/dist/stories.d.ts.map +1 -0
- package/dist/stories.js +80 -0
- package/dist/stories.js.map +1 -0
- package/dist/tables.d.ts +21 -0
- package/dist/tables.d.ts.map +1 -0
- package/dist/tables.js +485 -0
- package/dist/tables.js.map +1 -0
- package/dist/tools.d.ts +89 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +717 -0
- package/dist/tools.js.map +1 -0
- package/package.json +4 -4
- package/src/changes.ts +193 -0
- package/src/formatting.ts +149 -0
- package/src/handles.ts +40 -13
- package/src/index.ts +23 -0
- package/src/inserts.ts +339 -0
- package/src/operations.ts +71 -34
- package/src/paragraphs.ts +188 -0
- package/src/read.ts +141 -9
- package/src/schema.ts +74 -0
- package/src/session.ts +247 -18
- package/src/stories.ts +92 -0
- package/src/tables.ts +664 -0
- package/src/tools.ts +925 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Paragraph formatting and lists (23.5): alignment, line spacing, space before and after,
|
|
3
|
+
* indentation and list numbering of paragraphs, each through core's command for it, so every
|
|
4
|
+
* change is a tracked formatting change as the editor makes it.
|
|
5
|
+
*/
|
|
6
|
+
import {
|
|
7
|
+
type Alignment,
|
|
8
|
+
changeIndent,
|
|
9
|
+
listKindAt,
|
|
10
|
+
type Node,
|
|
11
|
+
setAlign,
|
|
12
|
+
setLineSpacing,
|
|
13
|
+
setParagraphSpacing,
|
|
14
|
+
toggleList,
|
|
15
|
+
wrapInList,
|
|
16
|
+
} from "@folijs/core";
|
|
17
|
+
import {
|
|
18
|
+
type Changed,
|
|
19
|
+
type Context,
|
|
20
|
+
changed,
|
|
21
|
+
lockedControls,
|
|
22
|
+
paragraphAt,
|
|
23
|
+
quote,
|
|
24
|
+
} from "./operations.js";
|
|
25
|
+
import { type Refusal, type Result, refuse } from "./result.js";
|
|
26
|
+
|
|
27
|
+
/** What to change of the paragraphs; what is left out stays as it is. */
|
|
28
|
+
export interface ParagraphFormat {
|
|
29
|
+
/** left, center, right or justify. */
|
|
30
|
+
align?: "left" | "center" | "right" | "justify";
|
|
31
|
+
/** A multiple of single spacing (1, 1.15, 1.5, 2); null: the style's. */
|
|
32
|
+
lineSpacing?: number | null;
|
|
33
|
+
/** Space before, in points; null: the style's. */
|
|
34
|
+
spaceBefore?: number | null;
|
|
35
|
+
/** Space after, in points; null: the style's. */
|
|
36
|
+
spaceAfter?: number | null;
|
|
37
|
+
/** Half an inch in or out; in a list, a level deeper or higher. */
|
|
38
|
+
indent?: "more" | "less";
|
|
39
|
+
/** Number the paragraphs as a list (one list for paragraphs next to each other); null: none. */
|
|
40
|
+
list?: "bullet" | "numbered" | null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const ALIGN: Record<string, Alignment> = {
|
|
44
|
+
left: "start",
|
|
45
|
+
center: "center",
|
|
46
|
+
right: "end",
|
|
47
|
+
justify: "justify",
|
|
48
|
+
};
|
|
49
|
+
const KEYS = new Set(["align", "lineSpacing", "spaceBefore", "spaceAfter", "indent", "list"]);
|
|
50
|
+
const points = (v: unknown) => v === null || (typeof v === "number" && v >= 0 && v <= 1584);
|
|
51
|
+
|
|
52
|
+
/** Why `format` cannot be applied as given, or null. */
|
|
53
|
+
function wrong(format: unknown): string | null {
|
|
54
|
+
if (!format || typeof format !== "object" || Array.isArray(format))
|
|
55
|
+
return 'format is an object of what to change: { "align": "center", "spaceAfter": 6 }.';
|
|
56
|
+
const f = format as Record<string, unknown>;
|
|
57
|
+
const keys = Object.keys(f);
|
|
58
|
+
if (!keys.length) return "format names nothing to change.";
|
|
59
|
+
const unknown = keys.find((k) => !KEYS.has(k));
|
|
60
|
+
if (unknown) return `format has no ${quote(unknown)}: it takes ${[...KEYS].join(", ")}.`;
|
|
61
|
+
if (f.align !== undefined && !Object.hasOwn(ALIGN, String(f.align)))
|
|
62
|
+
return "align is left, center, right or justify.";
|
|
63
|
+
if (
|
|
64
|
+
f.lineSpacing !== undefined &&
|
|
65
|
+
f.lineSpacing !== null &&
|
|
66
|
+
!(typeof f.lineSpacing === "number" && f.lineSpacing >= 0.25 && f.lineSpacing <= 132)
|
|
67
|
+
)
|
|
68
|
+
return "lineSpacing is a multiple of single spacing (1, 1.15, 1.5, 2; at least 0.25), or null.";
|
|
69
|
+
if (f.spaceBefore !== undefined && !points(f.spaceBefore))
|
|
70
|
+
return "spaceBefore is in points, 0 to 1584, or null.";
|
|
71
|
+
if (f.spaceAfter !== undefined && !points(f.spaceAfter))
|
|
72
|
+
return "spaceAfter is in points, 0 to 1584, or null.";
|
|
73
|
+
if (f.indent !== undefined && f.indent !== "more" && f.indent !== "less")
|
|
74
|
+
return 'indent is "more" or "less".';
|
|
75
|
+
if (f.list !== undefined && f.list !== null && f.list !== "bullet" && f.list !== "numbered")
|
|
76
|
+
return 'list is "bullet", "numbered", or null to take the numbering off.';
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Paragraphs next to each other, as runs: one list for each run. */
|
|
81
|
+
function runsOf(targets: { pos: number; node: Node }[]): { from: number; to: number }[][] {
|
|
82
|
+
const runs: { from: number; to: number }[][] = [];
|
|
83
|
+
let last: { pos: number; node: Node } | null = null;
|
|
84
|
+
for (const t of [...targets].sort((a, b) => a.pos - b.pos)) {
|
|
85
|
+
const range = { from: t.pos + 1, to: t.pos + t.node.nodeSize - 1 };
|
|
86
|
+
if (last && last.pos + last.node.nodeSize === t.pos) runs[runs.length - 1]?.push(range);
|
|
87
|
+
else runs.push([range]);
|
|
88
|
+
last = t;
|
|
89
|
+
}
|
|
90
|
+
return runs;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** @internal */
|
|
94
|
+
export function formatParagraphs(
|
|
95
|
+
context: Context,
|
|
96
|
+
handles: string | readonly string[],
|
|
97
|
+
format: ParagraphFormat,
|
|
98
|
+
): Result<Changed> {
|
|
99
|
+
const { editor } = context;
|
|
100
|
+
const why = wrong(format);
|
|
101
|
+
if (why) return refuse("invalid", why);
|
|
102
|
+
const list = typeof handles === "string" ? [handles] : handles;
|
|
103
|
+
if (!Array.isArray(list) || !list.length || list.some((h) => typeof h !== "string"))
|
|
104
|
+
return refuse("invalid", 'handles is a paragraph\'s handle, or a list of them: ["b4", "b5"].');
|
|
105
|
+
const targets: { pos: number; node: Node }[] = [];
|
|
106
|
+
for (const handle of new Set(list)) {
|
|
107
|
+
const target: Refusal | { pos: number; node: Node } = paragraphAt(
|
|
108
|
+
context,
|
|
109
|
+
handle,
|
|
110
|
+
"paragraphs are formatted",
|
|
111
|
+
);
|
|
112
|
+
if ("ok" in target) return target;
|
|
113
|
+
// A locked control refuses an edit of any paragraph in it, and with it the whole run's.
|
|
114
|
+
const locked = lockedControls(context, target.pos, target.node)[0];
|
|
115
|
+
if (locked)
|
|
116
|
+
return refuse(
|
|
117
|
+
"read-only",
|
|
118
|
+
`Block ${handle} is in the locked content control ${quote(locked)}: its formatting cannot be changed. Nothing was changed.`,
|
|
119
|
+
);
|
|
120
|
+
targets.push(target);
|
|
121
|
+
}
|
|
122
|
+
const before = editor.state.doc;
|
|
123
|
+
// No step here moves positions: each run as it comes.
|
|
124
|
+
const was = new Map(targets.map((t) => [t.pos, t.node]));
|
|
125
|
+
for (const run of runsOf(targets)) {
|
|
126
|
+
const first = run[0] as { from: number };
|
|
127
|
+
const last = run[run.length - 1] as { to: number };
|
|
128
|
+
const select = () => editor.select(first.from, last.to);
|
|
129
|
+
if (format.align !== undefined) {
|
|
130
|
+
select();
|
|
131
|
+
editor.run(setAlign(ALIGN[format.align] as Alignment));
|
|
132
|
+
}
|
|
133
|
+
if (format.lineSpacing !== undefined) {
|
|
134
|
+
select();
|
|
135
|
+
editor.run(setLineSpacing(format.lineSpacing));
|
|
136
|
+
}
|
|
137
|
+
if (format.spaceBefore !== undefined || format.spaceAfter !== undefined) {
|
|
138
|
+
select();
|
|
139
|
+
editor.run(
|
|
140
|
+
setParagraphSpacing({
|
|
141
|
+
...(format.spaceBefore === undefined ? {} : { before: format.spaceBefore }),
|
|
142
|
+
...(format.spaceAfter === undefined ? {} : { after: format.spaceAfter }),
|
|
143
|
+
}),
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
if (format.list !== undefined) {
|
|
147
|
+
select();
|
|
148
|
+
const now = listKindAt(editor.state);
|
|
149
|
+
if (format.list === null) {
|
|
150
|
+
// Paragraph by paragraph: a run in two lists, or partly numbered, reads as no list.
|
|
151
|
+
for (const range of run) {
|
|
152
|
+
editor.select(range.from, range.to);
|
|
153
|
+
const kind = listKindAt(editor.state);
|
|
154
|
+
if (kind) editor.run(toggleList(kind));
|
|
155
|
+
}
|
|
156
|
+
} else {
|
|
157
|
+
const kind = format.list === "bullet" ? "bulletList" : "orderedList";
|
|
158
|
+
if (now !== kind) editor.run(wrapInList(kind));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (format.indent !== undefined) {
|
|
162
|
+
select();
|
|
163
|
+
editor.run(changeIndent(format.indent === "more" ? 1 : -1));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const handlesNow = [...new Set(list)];
|
|
167
|
+
if (editor.state.doc === before) {
|
|
168
|
+
const read = changed(context, handlesNow);
|
|
169
|
+
return {
|
|
170
|
+
ok: true,
|
|
171
|
+
blocks: read.blocks,
|
|
172
|
+
text: `Nothing changed: the paragraphs already are so.\n${read.text}`,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
// Paragraphs a command left as they were (an indent past what the list defines, less of none).
|
|
176
|
+
const same = [...new Set(list)].filter((h) => {
|
|
177
|
+
const found = context.handles.resolve(h);
|
|
178
|
+
return found && was.get(found.pos) === found.node;
|
|
179
|
+
});
|
|
180
|
+
const read = changed(context, handlesNow);
|
|
181
|
+
return {
|
|
182
|
+
ok: true,
|
|
183
|
+
blocks: read.blocks,
|
|
184
|
+
text: same.length
|
|
185
|
+
? `${read.text}\n(unchanged, already so or no further to go: ${same.join(", ")})`
|
|
186
|
+
: read.text,
|
|
187
|
+
};
|
|
188
|
+
}
|
package/src/read.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { controlsIn, type EditorState, fieldsIn, type Node } from "@folijs/core";
|
|
1
|
+
import { controlsIn, type EditorState, fieldsIn, type Mark, type Node } from "@folijs/core";
|
|
2
2
|
import { wordContext } from "@folijs/core/internal";
|
|
3
3
|
import type { Handles } from "./handles.js";
|
|
4
4
|
|
|
@@ -14,7 +14,7 @@ export interface Block {
|
|
|
14
14
|
number?: string;
|
|
15
15
|
/**
|
|
16
16
|
* A paragraph's text: tabs as tabs, line breaks as "\n". What is not text stands in brackets:
|
|
17
|
-
* "[picture]", "[note]" (a note's reference), "[page break]", "[column break]", "[empty: Offer
|
|
17
|
+
* "[picture]" (with its alt text: "[picture: …]"), "[note]" (a note's reference), "[page break]", "[column break]", "[empty: Offer
|
|
18
18
|
* date]" (a content control showing its placeholder, by its title), "[object]". Text deleted
|
|
19
19
|
* as a tracked change, hidden text and field codes are left out. A table: "". Other: what it
|
|
20
20
|
* is.
|
|
@@ -33,8 +33,19 @@ export interface Block {
|
|
|
33
33
|
depth: number;
|
|
34
34
|
/** It holds tracked changes not yet accepted or rejected (anyone's). */
|
|
35
35
|
changed: boolean;
|
|
36
|
+
/** A table's style, by name, and how many rows from its top repeat on each page (23.9). */
|
|
37
|
+
tableStyle?: string;
|
|
38
|
+
headerRows?: number;
|
|
36
39
|
/** How many comments are anchored in it. */
|
|
37
40
|
comments: number;
|
|
41
|
+
/**
|
|
42
|
+
* The paragraph's own formatting, when `read` was asked for it (`formatting`): its own
|
|
43
|
+
* properties (`paragraph centered, space after 6 pt`), then runs of text that have some, each
|
|
44
|
+
* with what it has (`bold, 14 pt: "Total"`). The style's is not shown.
|
|
45
|
+
*/
|
|
46
|
+
formatting?: string;
|
|
47
|
+
/** A header's or footer's block (`read` with `headers`): which story it is in. */
|
|
48
|
+
story?: { id: string; kind: "header" | "footer" };
|
|
38
49
|
}
|
|
39
50
|
|
|
40
51
|
type Attrs = Record<string, unknown>;
|
|
@@ -119,7 +130,12 @@ function paragraphText(node: Node, pos: number, hiddenAt: (at: number) => Hidden
|
|
|
119
130
|
else if (name === "tab") text += "\t";
|
|
120
131
|
else if (name === "hardBreak") text += "\n";
|
|
121
132
|
else if (child.type.spec.text) text += child.type.spec.text(child.attrs);
|
|
122
|
-
else if (
|
|
133
|
+
else if (name === "picture" && typeof child.attrs.alt === "string" && child.attrs.alt.trim()) {
|
|
134
|
+
// Its alt text: what tells a model which picture it is (23.9's trial asked for it).
|
|
135
|
+
const alt = child.attrs.alt.replace(/[\s[\]]+/g, " ").trim();
|
|
136
|
+
const cut = [...alt];
|
|
137
|
+
text += `[picture: ${cut.length > 60 ? `${cut.slice(0, 57).join("")}…` : alt}]`;
|
|
138
|
+
} else if (!UNSEEN.has(name)) text += STANDS_AS[name] ?? "[object]";
|
|
123
139
|
});
|
|
124
140
|
return { text, changed, comments };
|
|
125
141
|
}
|
|
@@ -190,20 +206,22 @@ export function readBlocks(state: EditorState, handles: Handles): Block[] {
|
|
|
190
206
|
if (!stays) return;
|
|
191
207
|
const handle = handles.at(pos, node);
|
|
192
208
|
const table = add(
|
|
193
|
-
{ handle, kind: "table", text: "", changed:
|
|
209
|
+
{ handle, kind: "table", text: "", changed: !!node.attrs.formatChange, comments: 0 },
|
|
194
210
|
cell,
|
|
195
211
|
depth,
|
|
196
212
|
);
|
|
197
213
|
let rows = 0;
|
|
198
214
|
let columns = 0;
|
|
199
215
|
node.forEach((row, rowOffset) => {
|
|
200
|
-
if (revisionOf(row)) table.changed = true;
|
|
216
|
+
if (revisionOf(row) || row.attrs.formatChange) table.changed = true;
|
|
201
217
|
if (revisionOf(row)?.deletion) return;
|
|
202
218
|
rows++;
|
|
203
219
|
const rowStart = pos + 1 + rowOffset + 1;
|
|
204
|
-
// The grid column each cell starts at: the
|
|
205
|
-
|
|
220
|
+
// The grid column each cell starts at: the columns the row leaves empty before its
|
|
221
|
+
// first cell, the cells before it, and what they span.
|
|
222
|
+
let grid = 1 + ((row.attrs.gridBefore as number | null) ?? 0);
|
|
206
223
|
row.forEach((cellNode, cellOffset) => {
|
|
224
|
+
if (cellNode.attrs.formatChange) table.changed = true;
|
|
207
225
|
const span = (cellNode.attrs.gridSpan as number | null) ?? 1;
|
|
208
226
|
const merge = cellNode.attrs.vMerge as string | null;
|
|
209
227
|
const place = {
|
|
@@ -222,6 +240,20 @@ export function readBlocks(state: EditorState, handles: Handles): Block[] {
|
|
|
222
240
|
});
|
|
223
241
|
table.rows = rows;
|
|
224
242
|
table.columns = columns;
|
|
243
|
+
const styleId = node.attrs.style as string | null;
|
|
244
|
+
const style = styleId
|
|
245
|
+
? (context?.envelope.styles.find((s) => s.id === styleId)?.name ?? styleId)
|
|
246
|
+
: null;
|
|
247
|
+
if (style) table.tableStyle = style;
|
|
248
|
+
// From the top, among the rows read (a row deleted as a tracked change is not).
|
|
249
|
+
let header = 0;
|
|
250
|
+
let counting = true;
|
|
251
|
+
node.forEach((row) => {
|
|
252
|
+
if (!counting || revisionOf(row)?.deletion) return;
|
|
253
|
+
if (row.attrs.header === true) header++;
|
|
254
|
+
else counting = false;
|
|
255
|
+
});
|
|
256
|
+
if (header) table.headerRows = header;
|
|
225
257
|
} else {
|
|
226
258
|
const text = node.type.spec.text?.(node.attrs) ?? String(node.attrs.label ?? name);
|
|
227
259
|
add(
|
|
@@ -292,7 +324,11 @@ export function formatBlocks(blocks: readonly Block[]): string {
|
|
|
292
324
|
];
|
|
293
325
|
if (block.kind === "table") {
|
|
294
326
|
const size = [count(block.rows, "row"), count(block.columns, "column")];
|
|
295
|
-
|
|
327
|
+
const look = [
|
|
328
|
+
...(block.tableStyle ? [oneLine(block.tableStyle)] : []),
|
|
329
|
+
...(block.headerRows ? [count(block.headerRows, "header row")] : []),
|
|
330
|
+
];
|
|
331
|
+
return start + [...where, "table", ...size, ...look, ...flags].join(", ");
|
|
296
332
|
}
|
|
297
333
|
const text = oneLine(block.text);
|
|
298
334
|
if (block.kind === "other") return `${start}${[...where, "not editable"].join(", ")}: ${text}`;
|
|
@@ -301,7 +337,103 @@ export function formatBlocks(blocks: readonly Block[]): string {
|
|
|
301
337
|
oneLine(block.style ?? "paragraph"),
|
|
302
338
|
...(block.number ? [oneLine(block.number)] : []),
|
|
303
339
|
];
|
|
304
|
-
|
|
340
|
+
const own = block.formatting
|
|
341
|
+
? `
|
|
342
|
+
${" ".repeat(start.length)} formatting: ${block.formatting}`
|
|
343
|
+
: "";
|
|
344
|
+
return `${start}${[...head, ...flags].join(", ")}: ${text}${own}`;
|
|
305
345
|
};
|
|
306
346
|
return blocks.map(line).join("\n");
|
|
307
347
|
}
|
|
348
|
+
|
|
349
|
+
/** What a run's own marks make it, in words; empty for none. */
|
|
350
|
+
function described(marks: readonly Mark[]): string[] {
|
|
351
|
+
const out: string[] = [];
|
|
352
|
+
for (const mark of marks) {
|
|
353
|
+
const a = mark.attrs;
|
|
354
|
+
switch (mark.type.name) {
|
|
355
|
+
case "bold":
|
|
356
|
+
case "italic":
|
|
357
|
+
case "strike":
|
|
358
|
+
out.push(a.value === false ? `not ${mark.type.name}` : mark.type.name);
|
|
359
|
+
break;
|
|
360
|
+
case "underline":
|
|
361
|
+
out.push(a.style === "none" ? "not underlined" : "underlined");
|
|
362
|
+
break;
|
|
363
|
+
case "font": {
|
|
364
|
+
const name = a.ascii ?? a.hAnsi;
|
|
365
|
+
if (name) out.push(`font ${JSON.stringify(name)}`);
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
case "fontSize":
|
|
369
|
+
if (typeof a.value === "number") out.push(`${a.value / 2} pt`);
|
|
370
|
+
break;
|
|
371
|
+
case "color":
|
|
372
|
+
if (a.value && a.value !== "auto") out.push(`color ${a.value as string}`);
|
|
373
|
+
break;
|
|
374
|
+
case "highlight":
|
|
375
|
+
if (a.value && a.value !== "none") out.push(`highlight ${a.value as string}`);
|
|
376
|
+
break;
|
|
377
|
+
case "link":
|
|
378
|
+
out.push(`link ${JSON.stringify(a.href)}`);
|
|
379
|
+
break;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return out;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* @internal
|
|
387
|
+
* A paragraph's runs that have formatting of their own, as `Block.formatting` gives them; text
|
|
388
|
+
* deleted as a tracked change is left out, as `read` leaves it out.
|
|
389
|
+
*/
|
|
390
|
+
export function runFormatting(paragraph: Node): string {
|
|
391
|
+
const runs: { what: string; text: string }[] = [];
|
|
392
|
+
paragraph.forEach((child) => {
|
|
393
|
+
if (!child.isText || child.marks.some((m) => m.type.name === "deletion")) return;
|
|
394
|
+
const what = described(child.marks).join(", ");
|
|
395
|
+
const last = runs[runs.length - 1];
|
|
396
|
+
if (last && last.what === what) last.text += child.text;
|
|
397
|
+
else runs.push({ what, text: child.text as string });
|
|
398
|
+
});
|
|
399
|
+
return runs
|
|
400
|
+
.filter((r) => r.what)
|
|
401
|
+
.map(
|
|
402
|
+
(r) =>
|
|
403
|
+
`${r.what}: ${JSON.stringify(r.text.length > 80 ? `${r.text.slice(0, 77)}…` : r.text)}`,
|
|
404
|
+
)
|
|
405
|
+
.join("; ");
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* @internal
|
|
410
|
+
* A paragraph's own properties in words (alignment, spacing, indentation), as
|
|
411
|
+
* `Block.formatting` starts with them; empty when it has none of its own.
|
|
412
|
+
*/
|
|
413
|
+
export function paragraphFormatting(paragraph: Node): string {
|
|
414
|
+
const a = paragraph.attrs as Record<string, Record<string, unknown> | string | null>;
|
|
415
|
+
const out: string[] = [];
|
|
416
|
+
const align: Record<string, string> = {
|
|
417
|
+
center: "centered",
|
|
418
|
+
right: "right aligned",
|
|
419
|
+
end: "right aligned",
|
|
420
|
+
both: "justified",
|
|
421
|
+
distribute: "justified",
|
|
422
|
+
left: "left aligned",
|
|
423
|
+
start: "left aligned",
|
|
424
|
+
};
|
|
425
|
+
if (typeof a.justification === "string" && align[a.justification])
|
|
426
|
+
out.push(align[a.justification] as string);
|
|
427
|
+
const spacing = (a.spacing ?? {}) as Record<string, unknown>;
|
|
428
|
+
if (typeof spacing.line === "number" && (spacing.lineRule ?? "auto") === "auto")
|
|
429
|
+
out.push(`line spacing ${Math.round((spacing.line / 240) * 100) / 100}`);
|
|
430
|
+
if (typeof spacing.before === "number") out.push(`space before ${spacing.before / 20} pt`);
|
|
431
|
+
if (typeof spacing.after === "number") out.push(`space after ${spacing.after / 20} pt`);
|
|
432
|
+
const indent = (a.indent ?? {}) as Record<string, unknown>;
|
|
433
|
+
if (typeof indent.left === "number" && indent.left) out.push(`indent ${indent.left / 20} pt`);
|
|
434
|
+
if (typeof indent.firstLine === "number" && indent.firstLine)
|
|
435
|
+
out.push(`first line ${indent.firstLine / 20} pt`);
|
|
436
|
+
if (typeof indent.hanging === "number" && indent.hanging)
|
|
437
|
+
out.push(`hanging ${indent.hanging / 20} pt`);
|
|
438
|
+
return out.length ? `paragraph ${out.join(", ")}` : "";
|
|
439
|
+
}
|
package/src/schema.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The part of JSON Schema the tools' arguments use (ADR-0013), and its check. Small on purpose:
|
|
3
|
+
* the arguments are flat objects of strings, numbers, booleans and short lists.
|
|
4
|
+
*/
|
|
5
|
+
export interface JsonSchema {
|
|
6
|
+
type: "object" | "string" | "integer" | "number" | "boolean" | "array" | "null";
|
|
7
|
+
description?: string;
|
|
8
|
+
/** An object's properties; nothing else is allowed in it. */
|
|
9
|
+
properties?: Record<string, JsonSchemaOrAny>;
|
|
10
|
+
required?: readonly string[];
|
|
11
|
+
additionalProperties?: boolean;
|
|
12
|
+
/** An array's items. */
|
|
13
|
+
items?: JsonSchemaOrAny;
|
|
14
|
+
enum?: readonly (string | number | boolean | null)[];
|
|
15
|
+
minimum?: number;
|
|
16
|
+
maximum?: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** A schema, or one of several (`anyOf`): a value that fits any of them fits. */
|
|
20
|
+
export type JsonSchemaOrAny = JsonSchema | { anyOf: readonly JsonSchema[]; description?: string };
|
|
21
|
+
|
|
22
|
+
const kind = (value: unknown) =>
|
|
23
|
+
value === null ? "null" : Array.isArray(value) ? "array" : typeof value;
|
|
24
|
+
|
|
25
|
+
/** Why `value` does not fit `schema`, in words for a model, or null when it fits. */
|
|
26
|
+
export function invalid(
|
|
27
|
+
value: unknown,
|
|
28
|
+
schema: JsonSchemaOrAny,
|
|
29
|
+
path = "arguments",
|
|
30
|
+
): string | null {
|
|
31
|
+
if ("anyOf" in schema) {
|
|
32
|
+
const reasons = schema.anyOf.map((option) => invalid(value, option, path));
|
|
33
|
+
return reasons.includes(null)
|
|
34
|
+
? null
|
|
35
|
+
: `${path} must be ${schema.anyOf.map((o) => o.type).join(" or ")}`;
|
|
36
|
+
}
|
|
37
|
+
const is = kind(value);
|
|
38
|
+
const fits =
|
|
39
|
+
schema.type === "integer"
|
|
40
|
+
? Number.isInteger(value)
|
|
41
|
+
: schema.type === "number"
|
|
42
|
+
? is === "number" && Number.isFinite(value)
|
|
43
|
+
: is === schema.type;
|
|
44
|
+
if (!fits)
|
|
45
|
+
return `${path} must be ${schema.type === "array" ? "a list" : schema.type}, not ${is}`;
|
|
46
|
+
if (schema.enum && !schema.enum.includes(value as string))
|
|
47
|
+
return `${path} must be one of ${schema.enum.map((v) => JSON.stringify(v)).join(", ")}`;
|
|
48
|
+
if (schema.minimum !== undefined && (value as number) < schema.minimum)
|
|
49
|
+
return `${path} must be at least ${schema.minimum}`;
|
|
50
|
+
if (schema.maximum !== undefined && (value as number) > schema.maximum)
|
|
51
|
+
return `${path} must be at most ${schema.maximum}`;
|
|
52
|
+
if (schema.type === "array" && schema.items)
|
|
53
|
+
for (const [index, item] of (value as unknown[]).entries()) {
|
|
54
|
+
const wrong = invalid(item, schema.items, `${path}[${index}]`);
|
|
55
|
+
if (wrong) return wrong;
|
|
56
|
+
}
|
|
57
|
+
if (schema.type === "object") {
|
|
58
|
+
const object = value as Record<string, unknown>;
|
|
59
|
+
const properties = schema.properties ?? {};
|
|
60
|
+
for (const key of schema.required ?? [])
|
|
61
|
+
if (!Object.hasOwn(object, key)) return `${path}.${key} is missing`;
|
|
62
|
+
for (const key of Object.keys(object)) {
|
|
63
|
+
const property = Object.hasOwn(properties, key) ? properties[key] : undefined;
|
|
64
|
+
if (!property) {
|
|
65
|
+
if (schema.additionalProperties) continue;
|
|
66
|
+
const known = Object.keys(properties);
|
|
67
|
+
return `${path}.${key} is not an argument${known.length ? ` (there are: ${known.join(", ")})` : ""}`;
|
|
68
|
+
}
|
|
69
|
+
const wrong = invalid(object[key], property, `${path}.${key}`);
|
|
70
|
+
if (wrong) return wrong;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|