@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/dist/read.js
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { controlsIn, fieldsIn } from "@folijs/core";
|
|
2
|
+
import { wordContext } from "@folijs/core/internal";
|
|
3
|
+
const revisionOf = (node) => node.attrs.revision;
|
|
4
|
+
const marked = (node, name) => node.marks.some((m) => m.type.name === name && m.attrs.value !== false);
|
|
5
|
+
const TRACKED = ["insertion", "deletion", "formatChange"];
|
|
6
|
+
/**
|
|
7
|
+
* Inline nodes that draw nothing: text runs on across them, for find too. Core's own list
|
|
8
|
+
* (`isUnseen`, state/fields.ts). Anything else that is not text stops a match there, so it
|
|
9
|
+
* shows in the text as something no document text is.
|
|
10
|
+
*/
|
|
11
|
+
const UNSEEN = new Set([
|
|
12
|
+
"fieldStart",
|
|
13
|
+
"fieldCode",
|
|
14
|
+
"fieldSeparator",
|
|
15
|
+
"fieldEnd",
|
|
16
|
+
"bookmarkStart",
|
|
17
|
+
"bookmarkEnd",
|
|
18
|
+
"commentStart",
|
|
19
|
+
"commentEnd",
|
|
20
|
+
"commentReference",
|
|
21
|
+
"controlStart",
|
|
22
|
+
"controlEnd",
|
|
23
|
+
]);
|
|
24
|
+
const STANDS_AS = {
|
|
25
|
+
picture: "[picture]",
|
|
26
|
+
noteReference: "[note]",
|
|
27
|
+
pageBreak: "[page break]",
|
|
28
|
+
columnBreak: "[column break]",
|
|
29
|
+
};
|
|
30
|
+
/** What of the body does not show: field codes and controls' placeholders, in order. */
|
|
31
|
+
function hiddenRanges(state) {
|
|
32
|
+
const doc = state.doc;
|
|
33
|
+
const ranges = fieldsIn(doc).map((f) => ({ from: f.from, to: f.codeEnd }));
|
|
34
|
+
const controls = wordContext(state)?.envelope.controls;
|
|
35
|
+
if (controls)
|
|
36
|
+
for (const range of controlsIn(doc)) {
|
|
37
|
+
const control = Object.hasOwn(controls, range.id) ? controls[range.id] : undefined;
|
|
38
|
+
if (control?.placeholder?.showing)
|
|
39
|
+
ranges.push({
|
|
40
|
+
from: range.contentFrom,
|
|
41
|
+
to: range.contentTo,
|
|
42
|
+
label: `[empty: ${control.alias ?? control.tag ?? control.kind}]`,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return ranges.sort((a, b) => a.from - b.from);
|
|
46
|
+
}
|
|
47
|
+
/** A paragraph's text as `Block.text` has it, and what else a read says of it. */
|
|
48
|
+
function paragraphText(node, pos, hiddenAt) {
|
|
49
|
+
let text = "";
|
|
50
|
+
let changed = !!revisionOf(node) || !!node.attrs.formatChange;
|
|
51
|
+
let comments = 0;
|
|
52
|
+
node.forEach((child, offset) => {
|
|
53
|
+
const name = child.type.name;
|
|
54
|
+
if (name === "commentReference")
|
|
55
|
+
comments++;
|
|
56
|
+
if (TRACKED.some((mark) => marked(child, mark)))
|
|
57
|
+
changed = true;
|
|
58
|
+
const hidden = hiddenAt(pos + 1 + offset);
|
|
59
|
+
if (hidden) {
|
|
60
|
+
// What stands for a placeholder, once; not for one deleted with its control.
|
|
61
|
+
if (hidden.label && !hidden.shown && !marked(child, "deletion")) {
|
|
62
|
+
text += hidden.label;
|
|
63
|
+
hidden.shown = true;
|
|
64
|
+
}
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (marked(child, "hidden") || marked(child, "deletion"))
|
|
68
|
+
return;
|
|
69
|
+
if (child.isText)
|
|
70
|
+
text += child.text;
|
|
71
|
+
else if (name === "tab")
|
|
72
|
+
text += "\t";
|
|
73
|
+
else if (name === "hardBreak")
|
|
74
|
+
text += "\n";
|
|
75
|
+
else if (child.type.spec.text)
|
|
76
|
+
text += child.type.spec.text(child.attrs);
|
|
77
|
+
else if (!UNSEEN.has(name))
|
|
78
|
+
text += STANDS_AS[name] ?? "[object]";
|
|
79
|
+
});
|
|
80
|
+
return { text, changed, comments };
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* @internal
|
|
84
|
+
* The body's blocks in document order, a table before its cells' blocks. A paragraph, a row or a
|
|
85
|
+
* table deleted as a tracked change is left out when nothing of it would stay.
|
|
86
|
+
*
|
|
87
|
+
* shortcut: a paragraph whose mark is deleted but that keeps text is read as its own block,
|
|
88
|
+
* where Word's final view joins it with the next (which, as there, has no number of its own);
|
|
89
|
+
* upgrade if agents trip on it.
|
|
90
|
+
*/
|
|
91
|
+
export function readBlocks(state, handles) {
|
|
92
|
+
const context = wordContext(state);
|
|
93
|
+
const styleNames = new Map();
|
|
94
|
+
for (const style of context?.envelope.styles ?? [])
|
|
95
|
+
if (style.type === "paragraph" && !styleNames.has(style.id))
|
|
96
|
+
styleNames.set(style.id, style.name ?? style.id);
|
|
97
|
+
const hidden = hiddenRanges(state);
|
|
98
|
+
let h = 0;
|
|
99
|
+
// Asked in document order only: the ranges are walked once.
|
|
100
|
+
const hiddenAt = (at) => {
|
|
101
|
+
while (h < hidden.length && hidden[h].to <= at)
|
|
102
|
+
h++;
|
|
103
|
+
const range = hidden[h];
|
|
104
|
+
return range && range.from <= at ? range : null;
|
|
105
|
+
};
|
|
106
|
+
const blocks = [];
|
|
107
|
+
/** Each read paragraph with its attributes, and whether the one before joins into it. */
|
|
108
|
+
const paragraphs = [];
|
|
109
|
+
const add = (block, cell, depth) => {
|
|
110
|
+
const made = { ...block, depth };
|
|
111
|
+
if (cell)
|
|
112
|
+
made.cell = cell;
|
|
113
|
+
blocks.push(made);
|
|
114
|
+
return made;
|
|
115
|
+
};
|
|
116
|
+
const walk = (parent, start, cell, depth) => {
|
|
117
|
+
let joins = false;
|
|
118
|
+
parent.forEach((node, offset, index) => {
|
|
119
|
+
const pos = start + offset;
|
|
120
|
+
const name = node.type.name;
|
|
121
|
+
const joined = joins;
|
|
122
|
+
joins = false;
|
|
123
|
+
if (name === "paragraph") {
|
|
124
|
+
const read = paragraphText(node, pos, hiddenAt);
|
|
125
|
+
// A deleted mark joins its paragraph with the next one, when that is a paragraph.
|
|
126
|
+
const next = index + 1 < parent.childCount ? parent.child(index + 1) : null;
|
|
127
|
+
const deleted = !!revisionOf(node)?.deletion && next?.type.name === "paragraph";
|
|
128
|
+
if (deleted && !read.text) {
|
|
129
|
+
joins = joined;
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
joins = deleted;
|
|
133
|
+
const block = add({ handle: handles.at(pos, node), kind: "paragraph", ...read }, cell, depth);
|
|
134
|
+
paragraphs.push({ block, attrs: node.attrs, joined });
|
|
135
|
+
}
|
|
136
|
+
else if (name === "table") {
|
|
137
|
+
// Every row deleted as a tracked change: the table is not read.
|
|
138
|
+
let stays = false;
|
|
139
|
+
node.forEach((row) => {
|
|
140
|
+
stays ||= !revisionOf(row)?.deletion;
|
|
141
|
+
});
|
|
142
|
+
if (!stays)
|
|
143
|
+
return;
|
|
144
|
+
const handle = handles.at(pos, node);
|
|
145
|
+
const table = add({ handle, kind: "table", text: "", changed: false, comments: 0 }, cell, depth);
|
|
146
|
+
let rows = 0;
|
|
147
|
+
let columns = 0;
|
|
148
|
+
node.forEach((row, rowOffset) => {
|
|
149
|
+
if (revisionOf(row))
|
|
150
|
+
table.changed = true;
|
|
151
|
+
if (revisionOf(row)?.deletion)
|
|
152
|
+
return;
|
|
153
|
+
rows++;
|
|
154
|
+
columns = Math.max(columns, row.childCount);
|
|
155
|
+
const rowStart = pos + 1 + rowOffset + 1;
|
|
156
|
+
row.forEach((cellNode, cellOffset, column) => {
|
|
157
|
+
const place = { table: handle, row: rows, column: column + 1 };
|
|
158
|
+
walk(cellNode, rowStart + cellOffset + 1, place, depth + 1);
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
table.rows = rows;
|
|
162
|
+
table.columns = columns;
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
const text = node.type.spec.text?.(node.attrs) ?? String(node.attrs.label ?? name);
|
|
166
|
+
add({ handle: handles.at(pos, node), kind: "other", text, changed: false, comments: 0 }, cell, depth);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
};
|
|
170
|
+
walk(state.doc, 0, undefined, 0);
|
|
171
|
+
if (context) {
|
|
172
|
+
// As the view numbers the final text: what joins the paragraph before has no number.
|
|
173
|
+
const markers = context.resolver.markers(paragraphs.map((p) => (p.joined ? { numbering: { id: 0 } } : p.attrs)));
|
|
174
|
+
paragraphs.forEach(({ block, attrs }, index) => {
|
|
175
|
+
const id = context.resolver.paragraph(attrs).paragraph.style;
|
|
176
|
+
if (id !== null)
|
|
177
|
+
block.style = styleNames.get(id) ?? id;
|
|
178
|
+
const marker = markers[index];
|
|
179
|
+
// A bullet is a character of a symbol font, often in the private use area: one sign for all.
|
|
180
|
+
const number = marker && (context.resolver.numberFormat(attrs) === "bullet" ? "•" : marker.text);
|
|
181
|
+
if (number)
|
|
182
|
+
block.number = number;
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
return blocks;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* @internal
|
|
189
|
+
* Text from a document on one line: every character that ends a line shows as ↵. For all a
|
|
190
|
+
* document names that is put in a line for a model (a style, a list number, a control), as for
|
|
191
|
+
* its text: none of it may start a line of its own, which would read as another block.
|
|
192
|
+
*/
|
|
193
|
+
export const oneLine = (text) => text.replace(/\r\n|[\n\r\u0085\u2028\u2029]/g, "↵");
|
|
194
|
+
const count = (n, what) => `${n} ${what}${n === 1 ? "" : "s"}`;
|
|
195
|
+
/**
|
|
196
|
+
* Blocks as the text a model reads, a line each:
|
|
197
|
+
*
|
|
198
|
+
* [b1] Title: Mutual Non-Disclosure Agreement
|
|
199
|
+
* [b2] Heading 1, 1.: Purpose
|
|
200
|
+
* [b3] Normal, changed, 2 comments: The parties wish to…
|
|
201
|
+
* [b4] table, 3 rows, 2 columns
|
|
202
|
+
* [b5] r1 c1, Normal: Item
|
|
203
|
+
* [b9] not editable: w:sdt
|
|
204
|
+
*
|
|
205
|
+
* After the handle: where it is in a table, its style, its list number, `changed` when it holds
|
|
206
|
+
* tracked changes, its comments; after the colon, its text. A line break inside a paragraph
|
|
207
|
+
* shows as ↵, as does any other character that ends a line, so a block is always one line.
|
|
208
|
+
*/
|
|
209
|
+
export function formatBlocks(blocks) {
|
|
210
|
+
const line = (block) => {
|
|
211
|
+
const start = `[${block.handle}] ${" ".repeat(block.depth)}`;
|
|
212
|
+
const where = block.cell ? [`r${block.cell.row} c${block.cell.column}`] : [];
|
|
213
|
+
const flags = [
|
|
214
|
+
...(block.changed ? ["changed"] : []),
|
|
215
|
+
...(block.comments ? [count(block.comments, "comment")] : []),
|
|
216
|
+
];
|
|
217
|
+
if (block.kind === "table") {
|
|
218
|
+
const size = [count(block.rows, "row"), count(block.columns, "column")];
|
|
219
|
+
return start + [...where, "table", ...size, ...flags].join(", ");
|
|
220
|
+
}
|
|
221
|
+
const text = oneLine(block.text);
|
|
222
|
+
if (block.kind === "other")
|
|
223
|
+
return `${start}${[...where, "not editable"].join(", ")}: ${text}`;
|
|
224
|
+
const head = [
|
|
225
|
+
...where,
|
|
226
|
+
oneLine(block.style ?? "paragraph"),
|
|
227
|
+
...(block.number ? [oneLine(block.number)] : []),
|
|
228
|
+
];
|
|
229
|
+
return `${start}${[...head, ...flags].join(", ")}: ${text}`;
|
|
230
|
+
};
|
|
231
|
+
return blocks.map(line).join("\n");
|
|
232
|
+
}
|
|
233
|
+
//# sourceMappingURL=read.js.map
|
package/dist/read.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read.js","sourceRoot":"","sources":["../src/read.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAoB,QAAQ,EAAa,MAAM,cAAc,CAAC;AACjF,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAoCpD,MAAM,UAAU,GAAG,CAAC,IAAU,EAAE,EAAE,CAChC,IAAI,CAAC,KAAK,CAAC,QAA0E,CAAC;AACxF,MAAM,MAAM,GAAG,CAAC,IAAU,EAAE,IAAY,EAAE,EAAE,CAC1C,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,IAAI,IAAI,CAAC,CAAC,KAAK,CAAC,KAAK,KAAK,KAAK,CAAC,CAAC;AAC1E,MAAM,OAAO,GAAG,CAAC,WAAW,EAAE,UAAU,EAAE,cAAc,CAAC,CAAC;AAC1D;;;;GAIG;AACH,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC;IACrB,YAAY;IACZ,WAAW;IACX,gBAAgB;IAChB,UAAU;IACV,eAAe;IACf,aAAa;IACb,cAAc;IACd,YAAY;IACZ,kBAAkB;IAClB,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KAAK,OAAO;YAAE,OAAO,GAAG,KAAK,GAAG,CAAC,GAAG,KAAK,EAAE,cAAc,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;QAC/F,MAAM,IAAI,GAAG;YACX,GAAG,KAAK;YACR,OAAO,CAAC,KAAK,CAAC,KAAK,IAAI,WAAW,CAAC;YACnC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;SACjD,CAAC;QACF,OAAO,GAAG,KAAK,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;IAC9D,CAAC,CAAC;IACF,OAAO,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACrC,CAAC"}
|
package/dist/result.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Why an operation was not done, in words a model can act on. */
|
|
2
|
+
export interface Refusal {
|
|
3
|
+
ok: false;
|
|
4
|
+
code: "unknown-handle" | "gone" | "not-a-paragraph" | "no-match" | "several-matches" | "unknown-style" | "unknown-control" | "read-only" | "invalid" | "refused";
|
|
5
|
+
message: string;
|
|
6
|
+
}
|
|
7
|
+
export type Ok<T = object> = {
|
|
8
|
+
ok: true;
|
|
9
|
+
} & T;
|
|
10
|
+
export type Result<T = object> = Ok<T> | Refusal;
|
|
11
|
+
//# sourceMappingURL=result.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,KAAK,CAAC;IACV,IAAI,EACA,gBAAgB,GAChB,MAAM,GACN,iBAAiB,GACjB,UAAU,GACV,iBAAiB,GACjB,eAAe,GACf,iBAAiB,GACjB,WAAW,GACX,SAAS,GACT,SAAS,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,MAAM,EAAE,CAAC,CAAC,GAAG,MAAM,IAAI;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE,GAAG,CAAC,CAAC;AAC9C,MAAM,MAAM,MAAM,CAAC,CAAC,GAAG,MAAM,IAAI,EAAE,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC"}
|
package/dist/result.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"result.js","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"AAoBA,gBAAgB;AAChB,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC,IAAqB,EAAE,OAAe,EAAW,EAAE,CAAC,CAAC;IAC1E,EAAE,EAAE,KAAK;IACT,IAAI;IACJ,OAAO;CACR,CAAC,CAAC"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { type Editor, type EditorDocument, type Extension, type FieldsReport, type FoliDocument } from "@folijs/core";
|
|
2
|
+
import { type Changed, type CommentOptions, type NewParagraph, type ReplaceOptions } from "./operations.js";
|
|
3
|
+
import { type Block } from "./read.js";
|
|
4
|
+
import { type Result } from "./result.js";
|
|
5
|
+
import { type Control, type Unfilled, type UpdateFieldsOptions } from "./templates.js";
|
|
6
|
+
export interface SessionOptions {
|
|
7
|
+
/** Whose tracked changes and comments the session's edits are. */
|
|
8
|
+
author: string;
|
|
9
|
+
/** The app's custom node and mark types, as `createEditor` takes them. */
|
|
10
|
+
extensions?: readonly Extension[];
|
|
11
|
+
}
|
|
12
|
+
export interface ReadOptions {
|
|
13
|
+
/** Start at this block (a handle from an earlier read). Default: the first block. */
|
|
14
|
+
from?: string;
|
|
15
|
+
/** At most this many blocks. Default: all of them. */
|
|
16
|
+
limit?: number;
|
|
17
|
+
}
|
|
18
|
+
export interface ReadResult {
|
|
19
|
+
/** The blocks as lines, each starting with its handle in brackets (`formatBlocks`). */
|
|
20
|
+
text: string;
|
|
21
|
+
blocks: Block[];
|
|
22
|
+
/** The handle to read `from` next when `limit` cut the read short; null at the end. */
|
|
23
|
+
next: string | null;
|
|
24
|
+
}
|
|
25
|
+
/** One agent's work on one document: its edits are tracked changes by `author`. */
|
|
26
|
+
export interface Session {
|
|
27
|
+
/**
|
|
28
|
+
* The headless editor under the session, for what the session has no operation for. Its
|
|
29
|
+
* commands take positions; edits made through it are tracked like the session's own, unless
|
|
30
|
+
* tracking is turned off on it. `setDocument` on it forgets every handle.
|
|
31
|
+
*/
|
|
32
|
+
readonly editor: Editor;
|
|
33
|
+
readonly author: string;
|
|
34
|
+
/** The document as it is now, to save or to export (`exportDocx` of `@folijs/docx`). */
|
|
35
|
+
document(): FoliDocument;
|
|
36
|
+
/**
|
|
37
|
+
* The body as it would be with every tracked change accepted, a block per line. Headers,
|
|
38
|
+
* footers and notes are not read. Refused when `from` names no block.
|
|
39
|
+
*/
|
|
40
|
+
read(options?: ReadOptions): Result<ReadResult>;
|
|
41
|
+
/** One block by its handle. */
|
|
42
|
+
block(handle: string): Result<{
|
|
43
|
+
block: Block;
|
|
44
|
+
text: string;
|
|
45
|
+
}>;
|
|
46
|
+
/**
|
|
47
|
+
* Replaces `find` in a paragraph by `replacement` ("" deletes it). `find` is text as `read`
|
|
48
|
+
* shows it, capitals and spaces included, and must be in the block once, unless `all`. The
|
|
49
|
+
* replacement takes the formatting of the text it replaces. Returns the block as it reads
|
|
50
|
+
* now, how many places were replaced and how many could not be (a locked control, a field).
|
|
51
|
+
*/
|
|
52
|
+
replaceText(handle: string, find: string, replacement: string, options?: ReplaceOptions): Result<Changed & {
|
|
53
|
+
replaced: number;
|
|
54
|
+
refused: number;
|
|
55
|
+
}>;
|
|
56
|
+
/**
|
|
57
|
+
* Inserts paragraphs after a block (a paragraph, a table, anything read): one, or a list.
|
|
58
|
+
* Next to a paragraph they are formatted as Enter there gives in Word: as the paragraph,
|
|
59
|
+
* numbering included, or in the style its style names as the next (after a heading: body
|
|
60
|
+
* text); a paragraph that names a `style` has that style and nothing of the neighbour's.
|
|
61
|
+
* Returns the new blocks with their handles.
|
|
62
|
+
*/
|
|
63
|
+
insertAfter(handle: string, paragraphs: NewParagraph | readonly NewParagraph[]): Result<Changed>;
|
|
64
|
+
/** As `insertAfter`, before the block: the block keeps its handle. */
|
|
65
|
+
insertBefore(handle: string, paragraphs: NewParagraph | readonly NewParagraph[]): Result<Changed>;
|
|
66
|
+
/**
|
|
67
|
+
* Deletes a paragraph, or a table with all its cells. A paragraph that must stay (the last of
|
|
68
|
+
* the document or of a cell, one before a table, one that ends a section) is emptied instead,
|
|
69
|
+
* and `text` says why.
|
|
70
|
+
*/
|
|
71
|
+
deleteBlock(handle: string): Result<{
|
|
72
|
+
text: string;
|
|
73
|
+
}>;
|
|
74
|
+
/** The paragraph styles there are, by the names `read` shows and `setStyle` takes. */
|
|
75
|
+
styles(): string[];
|
|
76
|
+
/**
|
|
77
|
+
* Puts a paragraph in a style, named by its name or id (capitals do not count). Its direct
|
|
78
|
+
* formatting stays. Recorded as a tracked formatting change.
|
|
79
|
+
*/
|
|
80
|
+
setStyle(handle: string, style: string): Result<Changed>;
|
|
81
|
+
/**
|
|
82
|
+
* Comments a paragraph in the session's author's name: the whole block, or `on` text in it
|
|
83
|
+
* (there once). A line break in `text` starts a new paragraph of the comment. A comment is
|
|
84
|
+
* not a tracked change: rejecting the session's changes leaves it. Returns the comment's id.
|
|
85
|
+
*/
|
|
86
|
+
comment(handle: string, text: string, options?: CommentOptions): Result<Changed & {
|
|
87
|
+
comment: number;
|
|
88
|
+
}>;
|
|
89
|
+
/** The body's content controls: what a template leaves to be filled, with their values. */
|
|
90
|
+
controls(): Control[];
|
|
91
|
+
/**
|
|
92
|
+
* Fills a content control: the one with this id, else every control with this tag, else with
|
|
93
|
+
* this title. Text for a text control (line breaks only where the control takes them), an
|
|
94
|
+
* item for a list, `yyyy-mm-dd` or any text for a date, true or false for a check box; null
|
|
95
|
+
* or "" empties it. A tracked change. Returns the blocks changed, the controls as they are
|
|
96
|
+
* now, and `unfilled`: those of the name that did not take the value, each with why.
|
|
97
|
+
*/
|
|
98
|
+
fillControl(name: string, value: string | boolean | null): Result<Changed & {
|
|
99
|
+
controls: Control[];
|
|
100
|
+
unfilled: Unfilled[];
|
|
101
|
+
}>;
|
|
102
|
+
/**
|
|
103
|
+
* Updates the document's fields (dates, cross-references, tables of contents; page numbers
|
|
104
|
+
* need a layout and are left), and fills its merge fields from `data`. A tracked change.
|
|
105
|
+
*/
|
|
106
|
+
updateFields(options?: UpdateFieldsOptions): Result<FieldsReport & {
|
|
107
|
+
text: string;
|
|
108
|
+
}>;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Opens a document for an agent. The document is validated as `createEditor` does (an older
|
|
112
|
+
* format is migrated); tracking is on from the start, whatever the document's own setting.
|
|
113
|
+
* Throws on a document that does not validate, and on an empty `author`.
|
|
114
|
+
*/
|
|
115
|
+
export declare function openSession(document: EditorDocument, options: SessionOptions): Session;
|
|
116
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,MAAM,EACX,KAAK,cAAc,EACnB,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,KAAK,YAAY,EAClB,MAAM,cAAc,CAAC;AAEtB,OAAO,EACL,KAAK,OAAO,EACZ,KAAK,cAAc,EAMnB,KAAK,YAAY,EACjB,KAAK,cAAc,EAKpB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,KAAK,KAAK,EAA4B,MAAM,WAAW,CAAC;AACjE,OAAO,EAAE,KAAK,MAAM,EAAU,MAAM,aAAa,CAAC;AAClD,OAAO,EACL,KAAK,OAAO,EAGZ,KAAK,QAAQ,EACb,KAAK,mBAAmB,EAEz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import { Handles } from "./handles.js";
|
|
3
|
+
import { changed, comment, deleteBlock, insert, replaceText, resolve, setStyle, styles, } from "./operations.js";
|
|
4
|
+
import { formatBlocks, readBlocks } from "./read.js";
|
|
5
|
+
import { refuse } from "./result.js";
|
|
6
|
+
import { controls, fillControl, updateFields, } from "./templates.js";
|
|
7
|
+
/**
|
|
8
|
+
* Opens a document for an agent. The document is validated as `createEditor` does (an older
|
|
9
|
+
* format is migrated); tracking is on from the start, whatever the document's own setting.
|
|
10
|
+
* Throws on a document that does not validate, and on an empty `author`.
|
|
11
|
+
*/
|
|
12
|
+
export function openSession(document, options) {
|
|
13
|
+
const { author, extensions } = options;
|
|
14
|
+
if (!author.trim())
|
|
15
|
+
throw new TypeError("openSession: `author` must name who edits");
|
|
16
|
+
const editor = createEditor({
|
|
17
|
+
document,
|
|
18
|
+
author,
|
|
19
|
+
trackChanges: true,
|
|
20
|
+
// Deleted text is neither read nor found: the agent works on the text as it would be.
|
|
21
|
+
reviewView: "final",
|
|
22
|
+
...(extensions ? { extensions } : {}),
|
|
23
|
+
});
|
|
24
|
+
const handles = new Handles(editor);
|
|
25
|
+
const context = { editor, handles, author };
|
|
26
|
+
const read = (options = {}) => {
|
|
27
|
+
// A model's tool call may send null for an object left out.
|
|
28
|
+
const { from, limit } = options ?? {};
|
|
29
|
+
if (limit !== undefined && !(Number.isInteger(limit) && limit > 0))
|
|
30
|
+
return refuse("invalid", "limit must be a whole number above 0.");
|
|
31
|
+
const all = readBlocks(editor.state, handles);
|
|
32
|
+
let start = 0;
|
|
33
|
+
if (from !== undefined) {
|
|
34
|
+
const target = resolve(context, from);
|
|
35
|
+
if ("ok" in target)
|
|
36
|
+
return target;
|
|
37
|
+
start = all.findIndex((block) => block.handle === from);
|
|
38
|
+
}
|
|
39
|
+
const blocks = all.slice(start, limit === undefined ? undefined : start + limit);
|
|
40
|
+
const next = all[start + blocks.length]?.handle ?? null;
|
|
41
|
+
return { ok: true, text: formatBlocks(blocks), blocks, next };
|
|
42
|
+
};
|
|
43
|
+
const block = (handle) => {
|
|
44
|
+
const target = resolve(context, handle);
|
|
45
|
+
if ("ok" in target)
|
|
46
|
+
return target;
|
|
47
|
+
const { blocks, text } = changed(context, [handle]);
|
|
48
|
+
return { ok: true, block: blocks[0], text };
|
|
49
|
+
};
|
|
50
|
+
return {
|
|
51
|
+
editor,
|
|
52
|
+
author,
|
|
53
|
+
document: () => editor.getDocument(),
|
|
54
|
+
read,
|
|
55
|
+
block,
|
|
56
|
+
replaceText: (handle, find, replacement, options) => replaceText(context, handle, find, replacement, options),
|
|
57
|
+
insertAfter: (handle, paragraphs) => insert(context, handle, paragraphs, "after"),
|
|
58
|
+
insertBefore: (handle, paragraphs) => insert(context, handle, paragraphs, "before"),
|
|
59
|
+
deleteBlock: (handle) => deleteBlock(context, handle),
|
|
60
|
+
styles: () => styles(context),
|
|
61
|
+
setStyle: (handle, style) => setStyle(context, handle, style),
|
|
62
|
+
comment: (handle, text, options) => comment(context, handle, text, options),
|
|
63
|
+
controls: () => controls(context),
|
|
64
|
+
fillControl: (name, value) => fillControl(context, name, value),
|
|
65
|
+
updateFields: (options) => updateFields(context, options),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,GAMb,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAIL,OAAO,EACP,OAAO,EACP,WAAW,EACX,MAAM,EAGN,WAAW,EACX,OAAO,EACP,QAAQ,EACR,MAAM,GACP,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAc,YAAY,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACjE,OAAO,EAAe,MAAM,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAEL,QAAQ,EACR,WAA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AAM,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,CAAC;QACjF,YAAY,EAAE,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,CAAC;QACnF,WAAW,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,WAAW,CAAC,OAAO,EAAE,MAAM,CAAC;QACrD,MAAM,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC;QAC7B,QAAQ,EAAE,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC;QAC7D,OAAO,EAAE,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC;QAC3E,QAAQ,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC;QACjC,WAAW,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,WAAW,CAAC,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC;QAC/D,YAAY,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,YAAY,CAAC,OAAO,EAAE,OAAO,CAAC;KAC1D,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type ControlValue } from "@folijs/core";
|
|
2
|
+
import { type Refusal } from "./result.js";
|
|
3
|
+
/** A content control of the body: a place a template leaves to be filled. */
|
|
4
|
+
export interface Control {
|
|
5
|
+
/** Its own name in the document: what `fillControl` takes to fill this control and no other. */
|
|
6
|
+
id: string;
|
|
7
|
+
/**
|
|
8
|
+
* Its tag, or its title when it has no tag, or its id. `fillControl` takes it too; controls
|
|
9
|
+
* that share a tag are all filled by it.
|
|
10
|
+
*/
|
|
11
|
+
name: string;
|
|
12
|
+
tag?: string;
|
|
13
|
+
title?: string;
|
|
14
|
+
kind: ControlValue["kind"];
|
|
15
|
+
/**
|
|
16
|
+
* Text: the text. A list: the chosen item. A date: `yyyy-mm-dd`, or the text typed. A check
|
|
17
|
+
* box: whether it is checked. Null while it is empty, and for kinds that hold no value.
|
|
18
|
+
*/
|
|
19
|
+
value: string | boolean | null;
|
|
20
|
+
/** It shows its placeholder: nothing was filled in yet. */
|
|
21
|
+
empty: boolean;
|
|
22
|
+
/** A drop-down list's or combo box's items, as shown. */
|
|
23
|
+
items?: string[];
|
|
24
|
+
/** Locked, or filled from bound data: it cannot be filled. */
|
|
25
|
+
readOnly: boolean;
|
|
26
|
+
/** The handle of the block it is in (the first, when it holds several). */
|
|
27
|
+
block: string;
|
|
28
|
+
}
|
|
29
|
+
/** A control that was not filled, with why, when others of the same name were. */
|
|
30
|
+
export interface Unfilled {
|
|
31
|
+
id: string;
|
|
32
|
+
name: string;
|
|
33
|
+
code: Refusal["code"];
|
|
34
|
+
message: string;
|
|
35
|
+
}
|
|
36
|
+
export interface UpdateFieldsOptions {
|
|
37
|
+
/**
|
|
38
|
+
* Values for the document's merge fields (`«Name»` in a read), by field name, capitals not
|
|
39
|
+
* counting: text, numbers, true or false, dates.
|
|
40
|
+
*/
|
|
41
|
+
data?: Readonly<Record<string, string | number | boolean | Date | null>>;
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=templates.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"templates.d.ts","sourceRoot":"","sources":["../src/templates.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,YAAY,EAUlB,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,KAAK,OAAO,EAAuB,MAAM,aAAa,CAAC;AAEhE,6EAA6E;AAC7E,MAAM,WAAW,OAAO;IACtB,gGAAgG;IAChG,EAAE,EAAE,MAAM,CAAC;IACX;;;OAGG;IACH,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,YAAY,CAAC,MAAM,CAAC,CAAC;IAC3B;;;OAGG;IACH,KAAK,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;IAC/B,2DAA2D;IAC3D,KAAK,EAAE,OAAO,CAAC;IACf,yDAAyD;IACzD,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,8DAA8D;IAC9D,QAAQ,EAAE,OAAO,CAAC;IAClB,2EAA2E;IAC3E,KAAK,EAAE,MAAM,CAAC;CACf;AAED,kFAAkF;AAClF,MAAM,WAAW,QAAQ;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;CACjB;AAwJD,MAAM,WAAW,mBAAmB;IAClC;;;OAGG;IACH,IAAI,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC;CAC1E"}
|