@folijs/agent 0.0.0-stage → 0.1.1
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 +87 -0
- package/LICENSE +21 -0
- package/README.md +156 -2
- package/dist/handles.d.ts +2 -0
- package/dist/handles.d.ts.map +1 -0
- package/dist/handles.js +72 -0
- package/dist/handles.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/operations.d.ts +20 -0
- package/dist/operations.d.ts.map +1 -0
- package/dist/operations.js +383 -0
- package/dist/operations.js.map +1 -0
- package/dist/read.d.ts +50 -0
- package/dist/read.d.ts.map +1 -0
- package/dist/read.js +233 -0
- package/dist/read.js.map +1 -0
- package/dist/result.d.ts +11 -0
- package/dist/result.d.ts.map +1 -0
- package/dist/result.js +7 -0
- package/dist/result.js.map +1 -0
- package/dist/session.d.ts +116 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +68 -0
- package/dist/session.js.map +1 -0
- package/dist/templates.d.ts +43 -0
- package/dist/templates.d.ts.map +1 -0
- package/dist/templates.js +182 -0
- package/dist/templates.js.map +1 -0
- package/package.json +38 -3
- package/src/handles.ts +73 -0
- package/src/index.ts +11 -0
- package/src/operations.ts +496 -0
- package/src/read.ts +284 -0
- package/src/result.ts +26 -0
- package/src/session.ts +203 -0
- package/src/templates.ts +252 -0
package/src/read.ts
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { controlsIn, type EditorState, fieldsIn, type Node } from "@folijs/core";
|
|
2
|
+
import { wordContext } from "@folijs/core/internal";
|
|
3
|
+
import type { Handles } from "./handles.js";
|
|
4
|
+
|
|
5
|
+
/** A block as an agent reads it: the document as it would be with every change accepted. */
|
|
6
|
+
export interface Block {
|
|
7
|
+
/** Its name in the session's operations. It stays the block's across edits. */
|
|
8
|
+
handle: string;
|
|
9
|
+
/** `other`: content Foli does not model, or a custom block. It cannot be edited. */
|
|
10
|
+
kind: "paragraph" | "table" | "other";
|
|
11
|
+
/** A paragraph's style, by the name Word shows; the default style when it names none. */
|
|
12
|
+
style?: string;
|
|
13
|
+
/** A numbered or bulleted paragraph's marker, as Word shows it ("1.", "(a)", "•"). */
|
|
14
|
+
number?: string;
|
|
15
|
+
/**
|
|
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
|
|
18
|
+
* date]" (a content control showing its placeholder, by its title), "[object]". Text deleted
|
|
19
|
+
* as a tracked change, hidden text and field codes are left out. A table: "". Other: what it
|
|
20
|
+
* is.
|
|
21
|
+
*/
|
|
22
|
+
text: string;
|
|
23
|
+
/** A table's size: its rows, and the cells of its longest row. */
|
|
24
|
+
rows?: number;
|
|
25
|
+
columns?: number;
|
|
26
|
+
/** Where a block in a table is: its table's handle, row and cell in the row, from 1. */
|
|
27
|
+
cell?: { table: string; row: number; column: number };
|
|
28
|
+
/** How many tables it is inside: 0 in the body. */
|
|
29
|
+
depth: number;
|
|
30
|
+
/** It holds tracked changes not yet accepted or rejected (anyone's). */
|
|
31
|
+
changed: boolean;
|
|
32
|
+
/** How many comments are anchored in it. */
|
|
33
|
+
comments: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type Attrs = Record<string, unknown>;
|
|
37
|
+
|
|
38
|
+
const revisionOf = (node: Node) =>
|
|
39
|
+
node.attrs.revision as { insertion?: unknown; deletion?: unknown } | null | undefined;
|
|
40
|
+
const marked = (node: Node, name: string) =>
|
|
41
|
+
node.marks.some((m) => m.type.name === name && m.attrs.value !== false);
|
|
42
|
+
const TRACKED = ["insertion", "deletion", "formatChange"];
|
|
43
|
+
/**
|
|
44
|
+
* Inline nodes that draw nothing: text runs on across them, for find too. Core's own list
|
|
45
|
+
* (`isUnseen`, state/fields.ts). Anything else that is not text stops a match there, so it
|
|
46
|
+
* shows in the text as something no document text is.
|
|
47
|
+
*/
|
|
48
|
+
const UNSEEN = new Set([
|
|
49
|
+
"fieldStart",
|
|
50
|
+
"fieldCode",
|
|
51
|
+
"fieldSeparator",
|
|
52
|
+
"fieldEnd",
|
|
53
|
+
"bookmarkStart",
|
|
54
|
+
"bookmarkEnd",
|
|
55
|
+
"commentStart",
|
|
56
|
+
"commentEnd",
|
|
57
|
+
"commentReference",
|
|
58
|
+
"controlStart",
|
|
59
|
+
"controlEnd",
|
|
60
|
+
]);
|
|
61
|
+
const STANDS_AS: Record<string, string> = {
|
|
62
|
+
picture: "[picture]",
|
|
63
|
+
noteReference: "[note]",
|
|
64
|
+
pageBreak: "[page break]",
|
|
65
|
+
columnBreak: "[column break]",
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** A stretch of the body that does not show; `label`: what shows in its place. */
|
|
69
|
+
interface Hidden {
|
|
70
|
+
from: number;
|
|
71
|
+
to: number;
|
|
72
|
+
label?: string;
|
|
73
|
+
/** Its label is in a block's text already: a control over several paragraphs shows it once. */
|
|
74
|
+
shown?: boolean;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** What of the body does not show: field codes and controls' placeholders, in order. */
|
|
78
|
+
function hiddenRanges(state: EditorState): Hidden[] {
|
|
79
|
+
const doc = state.doc;
|
|
80
|
+
const ranges: Hidden[] = fieldsIn(doc).map((f) => ({ from: f.from, to: f.codeEnd }));
|
|
81
|
+
const controls = wordContext(state)?.envelope.controls;
|
|
82
|
+
if (controls)
|
|
83
|
+
for (const range of controlsIn(doc)) {
|
|
84
|
+
const control = Object.hasOwn(controls, range.id) ? controls[range.id] : undefined;
|
|
85
|
+
if (control?.placeholder?.showing)
|
|
86
|
+
ranges.push({
|
|
87
|
+
from: range.contentFrom,
|
|
88
|
+
to: range.contentTo,
|
|
89
|
+
label: `[empty: ${control.alias ?? control.tag ?? control.kind}]`,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return ranges.sort((a, b) => a.from - b.from);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** A paragraph's text as `Block.text` has it, and what else a read says of it. */
|
|
96
|
+
function paragraphText(node: Node, pos: number, hiddenAt: (at: number) => Hidden | null) {
|
|
97
|
+
let text = "";
|
|
98
|
+
let changed = !!revisionOf(node) || !!node.attrs.formatChange;
|
|
99
|
+
let comments = 0;
|
|
100
|
+
node.forEach((child, offset) => {
|
|
101
|
+
const name = child.type.name;
|
|
102
|
+
if (name === "commentReference") comments++;
|
|
103
|
+
if (TRACKED.some((mark) => marked(child, mark))) changed = true;
|
|
104
|
+
const hidden = hiddenAt(pos + 1 + offset);
|
|
105
|
+
if (hidden) {
|
|
106
|
+
// What stands for a placeholder, once; not for one deleted with its control.
|
|
107
|
+
if (hidden.label && !hidden.shown && !marked(child, "deletion")) {
|
|
108
|
+
text += hidden.label;
|
|
109
|
+
hidden.shown = true;
|
|
110
|
+
}
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (marked(child, "hidden") || marked(child, "deletion")) return;
|
|
114
|
+
if (child.isText) text += child.text;
|
|
115
|
+
else if (name === "tab") text += "\t";
|
|
116
|
+
else if (name === "hardBreak") text += "\n";
|
|
117
|
+
else if (child.type.spec.text) text += child.type.spec.text(child.attrs);
|
|
118
|
+
else if (!UNSEEN.has(name)) text += STANDS_AS[name] ?? "[object]";
|
|
119
|
+
});
|
|
120
|
+
return { text, changed, comments };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* @internal
|
|
125
|
+
* The body's blocks in document order, a table before its cells' blocks. A paragraph, a row or a
|
|
126
|
+
* table deleted as a tracked change is left out when nothing of it would stay.
|
|
127
|
+
*
|
|
128
|
+
* shortcut: a paragraph whose mark is deleted but that keeps text is read as its own block,
|
|
129
|
+
* where Word's final view joins it with the next (which, as there, has no number of its own);
|
|
130
|
+
* upgrade if agents trip on it.
|
|
131
|
+
*/
|
|
132
|
+
export function readBlocks(state: EditorState, handles: Handles): Block[] {
|
|
133
|
+
const context = wordContext(state);
|
|
134
|
+
const styleNames = new Map<string, string>();
|
|
135
|
+
for (const style of context?.envelope.styles ?? [])
|
|
136
|
+
if (style.type === "paragraph" && !styleNames.has(style.id))
|
|
137
|
+
styleNames.set(style.id, style.name ?? style.id);
|
|
138
|
+
const hidden = hiddenRanges(state);
|
|
139
|
+
let h = 0;
|
|
140
|
+
// Asked in document order only: the ranges are walked once.
|
|
141
|
+
const hiddenAt = (at: number) => {
|
|
142
|
+
while (h < hidden.length && (hidden[h] as Hidden).to <= at) h++;
|
|
143
|
+
const range = hidden[h];
|
|
144
|
+
return range && range.from <= at ? range : null;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const blocks: Block[] = [];
|
|
148
|
+
/** Each read paragraph with its attributes, and whether the one before joins into it. */
|
|
149
|
+
const paragraphs: { block: Block; attrs: Attrs; joined: boolean }[] = [];
|
|
150
|
+
const add = (block: Omit<Block, "depth">, cell: Block["cell"], depth: number): Block => {
|
|
151
|
+
const made: Block = { ...block, depth };
|
|
152
|
+
if (cell) made.cell = cell;
|
|
153
|
+
blocks.push(made);
|
|
154
|
+
return made;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const walk = (parent: Node, start: number, cell: Block["cell"], depth: number) => {
|
|
158
|
+
let joins = false;
|
|
159
|
+
parent.forEach((node, offset, index) => {
|
|
160
|
+
const pos = start + offset;
|
|
161
|
+
const name = node.type.name;
|
|
162
|
+
const joined = joins;
|
|
163
|
+
joins = false;
|
|
164
|
+
if (name === "paragraph") {
|
|
165
|
+
const read = paragraphText(node, pos, hiddenAt);
|
|
166
|
+
// A deleted mark joins its paragraph with the next one, when that is a paragraph.
|
|
167
|
+
const next = index + 1 < parent.childCount ? parent.child(index + 1) : null;
|
|
168
|
+
const deleted = !!revisionOf(node)?.deletion && next?.type.name === "paragraph";
|
|
169
|
+
if (deleted && !read.text) {
|
|
170
|
+
joins = joined;
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
joins = deleted;
|
|
174
|
+
const block = add(
|
|
175
|
+
{ handle: handles.at(pos, node), kind: "paragraph", ...read },
|
|
176
|
+
cell,
|
|
177
|
+
depth,
|
|
178
|
+
);
|
|
179
|
+
paragraphs.push({ block, attrs: node.attrs as Attrs, joined });
|
|
180
|
+
} else if (name === "table") {
|
|
181
|
+
// Every row deleted as a tracked change: the table is not read.
|
|
182
|
+
let stays = false;
|
|
183
|
+
node.forEach((row) => {
|
|
184
|
+
stays ||= !revisionOf(row)?.deletion;
|
|
185
|
+
});
|
|
186
|
+
if (!stays) return;
|
|
187
|
+
const handle = handles.at(pos, node);
|
|
188
|
+
const table = add(
|
|
189
|
+
{ handle, kind: "table", text: "", changed: false, comments: 0 },
|
|
190
|
+
cell,
|
|
191
|
+
depth,
|
|
192
|
+
);
|
|
193
|
+
let rows = 0;
|
|
194
|
+
let columns = 0;
|
|
195
|
+
node.forEach((row, rowOffset) => {
|
|
196
|
+
if (revisionOf(row)) table.changed = true;
|
|
197
|
+
if (revisionOf(row)?.deletion) return;
|
|
198
|
+
rows++;
|
|
199
|
+
columns = Math.max(columns, row.childCount);
|
|
200
|
+
const rowStart = pos + 1 + rowOffset + 1;
|
|
201
|
+
row.forEach((cellNode, cellOffset, column) => {
|
|
202
|
+
const place = { table: handle, row: rows, column: column + 1 };
|
|
203
|
+
walk(cellNode, rowStart + cellOffset + 1, place, depth + 1);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
table.rows = rows;
|
|
207
|
+
table.columns = columns;
|
|
208
|
+
} else {
|
|
209
|
+
const text = node.type.spec.text?.(node.attrs) ?? String(node.attrs.label ?? name);
|
|
210
|
+
add(
|
|
211
|
+
{ handle: handles.at(pos, node), kind: "other", text, changed: false, comments: 0 },
|
|
212
|
+
cell,
|
|
213
|
+
depth,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
};
|
|
218
|
+
walk(state.doc, 0, undefined, 0);
|
|
219
|
+
|
|
220
|
+
if (context) {
|
|
221
|
+
// As the view numbers the final text: what joins the paragraph before has no number.
|
|
222
|
+
const markers = context.resolver.markers(
|
|
223
|
+
paragraphs.map((p) => (p.joined ? { numbering: { id: 0 } } : p.attrs)),
|
|
224
|
+
);
|
|
225
|
+
paragraphs.forEach(({ block, attrs }, index) => {
|
|
226
|
+
const id = context.resolver.paragraph(attrs).paragraph.style;
|
|
227
|
+
if (id !== null) block.style = styleNames.get(id) ?? id;
|
|
228
|
+
const marker = markers[index];
|
|
229
|
+
// A bullet is a character of a symbol font, often in the private use area: one sign for all.
|
|
230
|
+
const number =
|
|
231
|
+
marker && (context.resolver.numberFormat(attrs) === "bullet" ? "•" : marker.text);
|
|
232
|
+
if (number) block.number = number;
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
return blocks;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* @internal
|
|
240
|
+
* Text from a document on one line: every character that ends a line shows as ↵. For all a
|
|
241
|
+
* document names that is put in a line for a model (a style, a list number, a control), as for
|
|
242
|
+
* its text: none of it may start a line of its own, which would read as another block.
|
|
243
|
+
*/
|
|
244
|
+
export const oneLine = (text: string) => text.replace(/\r\n|[\n\r\u0085\u2028\u2029]/g, "↵");
|
|
245
|
+
|
|
246
|
+
const count = (n: number | undefined, what: string) => `${n} ${what}${n === 1 ? "" : "s"}`;
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Blocks as the text a model reads, a line each:
|
|
250
|
+
*
|
|
251
|
+
* [b1] Title: Mutual Non-Disclosure Agreement
|
|
252
|
+
* [b2] Heading 1, 1.: Purpose
|
|
253
|
+
* [b3] Normal, changed, 2 comments: The parties wish to…
|
|
254
|
+
* [b4] table, 3 rows, 2 columns
|
|
255
|
+
* [b5] r1 c1, Normal: Item
|
|
256
|
+
* [b9] not editable: w:sdt
|
|
257
|
+
*
|
|
258
|
+
* After the handle: where it is in a table, its style, its list number, `changed` when it holds
|
|
259
|
+
* tracked changes, its comments; after the colon, its text. A line break inside a paragraph
|
|
260
|
+
* shows as ↵, as does any other character that ends a line, so a block is always one line.
|
|
261
|
+
*/
|
|
262
|
+
export function formatBlocks(blocks: readonly Block[]): string {
|
|
263
|
+
const line = (block: Block): string => {
|
|
264
|
+
const start = `[${block.handle}] ${" ".repeat(block.depth)}`;
|
|
265
|
+
const where = block.cell ? [`r${block.cell.row} c${block.cell.column}`] : [];
|
|
266
|
+
const flags = [
|
|
267
|
+
...(block.changed ? ["changed"] : []),
|
|
268
|
+
...(block.comments ? [count(block.comments, "comment")] : []),
|
|
269
|
+
];
|
|
270
|
+
if (block.kind === "table") {
|
|
271
|
+
const size = [count(block.rows, "row"), count(block.columns, "column")];
|
|
272
|
+
return start + [...where, "table", ...size, ...flags].join(", ");
|
|
273
|
+
}
|
|
274
|
+
const text = oneLine(block.text);
|
|
275
|
+
if (block.kind === "other") return `${start}${[...where, "not editable"].join(", ")}: ${text}`;
|
|
276
|
+
const head = [
|
|
277
|
+
...where,
|
|
278
|
+
oneLine(block.style ?? "paragraph"),
|
|
279
|
+
...(block.number ? [oneLine(block.number)] : []),
|
|
280
|
+
];
|
|
281
|
+
return `${start}${[...head, ...flags].join(", ")}: ${text}`;
|
|
282
|
+
};
|
|
283
|
+
return blocks.map(line).join("\n");
|
|
284
|
+
}
|
package/src/result.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Why an operation was not done, in words a model can act on. */
|
|
2
|
+
export interface Refusal {
|
|
3
|
+
ok: false;
|
|
4
|
+
code:
|
|
5
|
+
| "unknown-handle"
|
|
6
|
+
| "gone"
|
|
7
|
+
| "not-a-paragraph"
|
|
8
|
+
| "no-match"
|
|
9
|
+
| "several-matches"
|
|
10
|
+
| "unknown-style"
|
|
11
|
+
| "unknown-control"
|
|
12
|
+
| "read-only"
|
|
13
|
+
| "invalid"
|
|
14
|
+
| "refused";
|
|
15
|
+
message: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type Ok<T = object> = { ok: true } & T;
|
|
19
|
+
export type Result<T = object> = Ok<T> | Refusal;
|
|
20
|
+
|
|
21
|
+
/** @internal */
|
|
22
|
+
export const refuse = (code: Refusal["code"], message: string): Refusal => ({
|
|
23
|
+
ok: false,
|
|
24
|
+
code,
|
|
25
|
+
message,
|
|
26
|
+
});
|
package/src/session.ts
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createEditor,
|
|
3
|
+
type Editor,
|
|
4
|
+
type EditorDocument,
|
|
5
|
+
type Extension,
|
|
6
|
+
type FieldsReport,
|
|
7
|
+
type FoliDocument,
|
|
8
|
+
} from "@folijs/core";
|
|
9
|
+
import { Handles } from "./handles.js";
|
|
10
|
+
import {
|
|
11
|
+
type Changed,
|
|
12
|
+
type CommentOptions,
|
|
13
|
+
type Context,
|
|
14
|
+
changed,
|
|
15
|
+
comment,
|
|
16
|
+
deleteBlock,
|
|
17
|
+
insert,
|
|
18
|
+
type NewParagraph,
|
|
19
|
+
type ReplaceOptions,
|
|
20
|
+
replaceText,
|
|
21
|
+
resolve,
|
|
22
|
+
setStyle,
|
|
23
|
+
styles,
|
|
24
|
+
} from "./operations.js";
|
|
25
|
+
import { type Block, formatBlocks, readBlocks } from "./read.js";
|
|
26
|
+
import { type Result, refuse } from "./result.js";
|
|
27
|
+
import {
|
|
28
|
+
type Control,
|
|
29
|
+
controls,
|
|
30
|
+
fillControl,
|
|
31
|
+
type Unfilled,
|
|
32
|
+
type UpdateFieldsOptions,
|
|
33
|
+
updateFields,
|
|
34
|
+
} from "./templates.js";
|
|
35
|
+
|
|
36
|
+
export interface SessionOptions {
|
|
37
|
+
/** Whose tracked changes and comments the session's edits are. */
|
|
38
|
+
author: string;
|
|
39
|
+
/** The app's custom node and mark types, as `createEditor` takes them. */
|
|
40
|
+
extensions?: readonly Extension[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface ReadOptions {
|
|
44
|
+
/** Start at this block (a handle from an earlier read). Default: the first block. */
|
|
45
|
+
from?: string;
|
|
46
|
+
/** At most this many blocks. Default: all of them. */
|
|
47
|
+
limit?: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface ReadResult {
|
|
51
|
+
/** The blocks as lines, each starting with its handle in brackets (`formatBlocks`). */
|
|
52
|
+
text: string;
|
|
53
|
+
blocks: Block[];
|
|
54
|
+
/** The handle to read `from` next when `limit` cut the read short; null at the end. */
|
|
55
|
+
next: string | null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** One agent's work on one document: its edits are tracked changes by `author`. */
|
|
59
|
+
export interface Session {
|
|
60
|
+
/**
|
|
61
|
+
* The headless editor under the session, for what the session has no operation for. Its
|
|
62
|
+
* commands take positions; edits made through it are tracked like the session's own, unless
|
|
63
|
+
* tracking is turned off on it. `setDocument` on it forgets every handle.
|
|
64
|
+
*/
|
|
65
|
+
readonly editor: Editor;
|
|
66
|
+
readonly author: string;
|
|
67
|
+
/** The document as it is now, to save or to export (`exportDocx` of `@folijs/docx`). */
|
|
68
|
+
document(): FoliDocument;
|
|
69
|
+
/**
|
|
70
|
+
* The body as it would be with every tracked change accepted, a block per line. Headers,
|
|
71
|
+
* footers and notes are not read. Refused when `from` names no block.
|
|
72
|
+
*/
|
|
73
|
+
read(options?: ReadOptions): Result<ReadResult>;
|
|
74
|
+
/** One block by its handle. */
|
|
75
|
+
block(handle: string): Result<{ block: Block; text: string }>;
|
|
76
|
+
/**
|
|
77
|
+
* Replaces `find` in a paragraph by `replacement` ("" deletes it). `find` is text as `read`
|
|
78
|
+
* shows it, capitals and spaces included, and must be in the block once, unless `all`. The
|
|
79
|
+
* replacement takes the formatting of the text it replaces. Returns the block as it reads
|
|
80
|
+
* now, how many places were replaced and how many could not be (a locked control, a field).
|
|
81
|
+
*/
|
|
82
|
+
replaceText(
|
|
83
|
+
handle: string,
|
|
84
|
+
find: string,
|
|
85
|
+
replacement: string,
|
|
86
|
+
options?: ReplaceOptions,
|
|
87
|
+
): Result<Changed & { replaced: number; refused: number }>;
|
|
88
|
+
/**
|
|
89
|
+
* Inserts paragraphs after a block (a paragraph, a table, anything read): one, or a list.
|
|
90
|
+
* Next to a paragraph they are formatted as Enter there gives in Word: as the paragraph,
|
|
91
|
+
* numbering included, or in the style its style names as the next (after a heading: body
|
|
92
|
+
* text); a paragraph that names a `style` has that style and nothing of the neighbour's.
|
|
93
|
+
* Returns the new blocks with their handles.
|
|
94
|
+
*/
|
|
95
|
+
insertAfter(handle: string, paragraphs: NewParagraph | readonly NewParagraph[]): Result<Changed>;
|
|
96
|
+
/** As `insertAfter`, before the block: the block keeps its handle. */
|
|
97
|
+
insertBefore(handle: string, paragraphs: NewParagraph | readonly NewParagraph[]): Result<Changed>;
|
|
98
|
+
/**
|
|
99
|
+
* Deletes a paragraph, or a table with all its cells. A paragraph that must stay (the last of
|
|
100
|
+
* the document or of a cell, one before a table, one that ends a section) is emptied instead,
|
|
101
|
+
* and `text` says why.
|
|
102
|
+
*/
|
|
103
|
+
deleteBlock(handle: string): Result<{ text: string }>;
|
|
104
|
+
/** The paragraph styles there are, by the names `read` shows and `setStyle` takes. */
|
|
105
|
+
styles(): string[];
|
|
106
|
+
/**
|
|
107
|
+
* Puts a paragraph in a style, named by its name or id (capitals do not count). Its direct
|
|
108
|
+
* formatting stays. Recorded as a tracked formatting change.
|
|
109
|
+
*/
|
|
110
|
+
setStyle(handle: string, style: string): Result<Changed>;
|
|
111
|
+
/**
|
|
112
|
+
* Comments a paragraph in the session's author's name: the whole block, or `on` text in it
|
|
113
|
+
* (there once). A line break in `text` starts a new paragraph of the comment. A comment is
|
|
114
|
+
* not a tracked change: rejecting the session's changes leaves it. Returns the comment's id.
|
|
115
|
+
*/
|
|
116
|
+
comment(
|
|
117
|
+
handle: string,
|
|
118
|
+
text: string,
|
|
119
|
+
options?: CommentOptions,
|
|
120
|
+
): Result<Changed & { comment: number }>;
|
|
121
|
+
/** The body's content controls: what a template leaves to be filled, with their values. */
|
|
122
|
+
controls(): Control[];
|
|
123
|
+
/**
|
|
124
|
+
* Fills a content control: the one with this id, else every control with this tag, else with
|
|
125
|
+
* this title. Text for a text control (line breaks only where the control takes them), an
|
|
126
|
+
* item for a list, `yyyy-mm-dd` or any text for a date, true or false for a check box; null
|
|
127
|
+
* or "" empties it. A tracked change. Returns the blocks changed, the controls as they are
|
|
128
|
+
* now, and `unfilled`: those of the name that did not take the value, each with why.
|
|
129
|
+
*/
|
|
130
|
+
fillControl(
|
|
131
|
+
name: string,
|
|
132
|
+
value: string | boolean | null,
|
|
133
|
+
): Result<Changed & { controls: Control[]; unfilled: Unfilled[] }>;
|
|
134
|
+
/**
|
|
135
|
+
* Updates the document's fields (dates, cross-references, tables of contents; page numbers
|
|
136
|
+
* need a layout and are left), and fills its merge fields from `data`. A tracked change.
|
|
137
|
+
*/
|
|
138
|
+
updateFields(options?: UpdateFieldsOptions): Result<FieldsReport & { text: string }>;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Opens a document for an agent. The document is validated as `createEditor` does (an older
|
|
143
|
+
* format is migrated); tracking is on from the start, whatever the document's own setting.
|
|
144
|
+
* Throws on a document that does not validate, and on an empty `author`.
|
|
145
|
+
*/
|
|
146
|
+
export function openSession(document: EditorDocument, options: SessionOptions): Session {
|
|
147
|
+
const { author, extensions } = options;
|
|
148
|
+
if (!author.trim()) throw new TypeError("openSession: `author` must name who edits");
|
|
149
|
+
const editor = createEditor({
|
|
150
|
+
document,
|
|
151
|
+
author,
|
|
152
|
+
trackChanges: true,
|
|
153
|
+
// Deleted text is neither read nor found: the agent works on the text as it would be.
|
|
154
|
+
reviewView: "final",
|
|
155
|
+
...(extensions ? { extensions } : {}),
|
|
156
|
+
});
|
|
157
|
+
const handles = new Handles(editor);
|
|
158
|
+
|
|
159
|
+
const context: Context = { editor, handles, author };
|
|
160
|
+
|
|
161
|
+
const read = (options: ReadOptions = {}): Result<ReadResult> => {
|
|
162
|
+
// A model's tool call may send null for an object left out.
|
|
163
|
+
const { from, limit } = options ?? {};
|
|
164
|
+
if (limit !== undefined && !(Number.isInteger(limit) && limit > 0))
|
|
165
|
+
return refuse("invalid", "limit must be a whole number above 0.");
|
|
166
|
+
const all = readBlocks(editor.state, handles);
|
|
167
|
+
let start = 0;
|
|
168
|
+
if (from !== undefined) {
|
|
169
|
+
const target = resolve(context, from);
|
|
170
|
+
if ("ok" in target) return target;
|
|
171
|
+
start = all.findIndex((block) => block.handle === from);
|
|
172
|
+
}
|
|
173
|
+
const blocks = all.slice(start, limit === undefined ? undefined : start + limit);
|
|
174
|
+
const next = all[start + blocks.length]?.handle ?? null;
|
|
175
|
+
return { ok: true, text: formatBlocks(blocks), blocks, next };
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
const block = (handle: string): Result<{ block: Block; text: string }> => {
|
|
179
|
+
const target = resolve(context, handle);
|
|
180
|
+
if ("ok" in target) return target;
|
|
181
|
+
const { blocks, text } = changed(context, [handle]);
|
|
182
|
+
return { ok: true, block: blocks[0] as Block, text };
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
return {
|
|
186
|
+
editor,
|
|
187
|
+
author,
|
|
188
|
+
document: () => editor.getDocument(),
|
|
189
|
+
read,
|
|
190
|
+
block,
|
|
191
|
+
replaceText: (handle, find, replacement, options) =>
|
|
192
|
+
replaceText(context, handle, find, replacement, options),
|
|
193
|
+
insertAfter: (handle, paragraphs) => insert(context, handle, paragraphs, "after"),
|
|
194
|
+
insertBefore: (handle, paragraphs) => insert(context, handle, paragraphs, "before"),
|
|
195
|
+
deleteBlock: (handle) => deleteBlock(context, handle),
|
|
196
|
+
styles: () => styles(context),
|
|
197
|
+
setStyle: (handle, style) => setStyle(context, handle, style),
|
|
198
|
+
comment: (handle, text, options) => comment(context, handle, text, options),
|
|
199
|
+
controls: () => controls(context),
|
|
200
|
+
fillControl: (name, value) => fillControl(context, name, value),
|
|
201
|
+
updateFields: (options) => updateFields(context, options),
|
|
202
|
+
};
|
|
203
|
+
}
|