@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/templates.ts
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ControlValue,
|
|
3
|
+
controlIsReadOnly,
|
|
4
|
+
controlProperties,
|
|
5
|
+
controlsIn,
|
|
6
|
+
controlValues,
|
|
7
|
+
type FieldsReport,
|
|
8
|
+
fieldsIn,
|
|
9
|
+
parseFieldInstruction,
|
|
10
|
+
setControlValue,
|
|
11
|
+
updateFields as updateFieldsOf,
|
|
12
|
+
} from "@folijs/core";
|
|
13
|
+
import { type Changed, type Context, changed } from "./operations.js";
|
|
14
|
+
import { oneLine, readBlocks } from "./read.js";
|
|
15
|
+
import { type Refusal, type Result, refuse } from "./result.js";
|
|
16
|
+
|
|
17
|
+
/** A content control of the body: a place a template leaves to be filled. */
|
|
18
|
+
export interface Control {
|
|
19
|
+
/** Its own name in the document: what `fillControl` takes to fill this control and no other. */
|
|
20
|
+
id: string;
|
|
21
|
+
/**
|
|
22
|
+
* Its tag, or its title when it has no tag, or its id. `fillControl` takes it too; controls
|
|
23
|
+
* that share a tag are all filled by it.
|
|
24
|
+
*/
|
|
25
|
+
name: string;
|
|
26
|
+
tag?: string;
|
|
27
|
+
title?: string;
|
|
28
|
+
kind: ControlValue["kind"];
|
|
29
|
+
/**
|
|
30
|
+
* Text: the text. A list: the chosen item. A date: `yyyy-mm-dd`, or the text typed. A check
|
|
31
|
+
* box: whether it is checked. Null while it is empty, and for kinds that hold no value.
|
|
32
|
+
*/
|
|
33
|
+
value: string | boolean | null;
|
|
34
|
+
/** It shows its placeholder: nothing was filled in yet. */
|
|
35
|
+
empty: boolean;
|
|
36
|
+
/** A drop-down list's or combo box's items, as shown. */
|
|
37
|
+
items?: string[];
|
|
38
|
+
/** Locked, or filled from bound data: it cannot be filled. */
|
|
39
|
+
readOnly: boolean;
|
|
40
|
+
/** The handle of the block it is in (the first, when it holds several). */
|
|
41
|
+
block: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A control that was not filled, with why, when others of the same name were. */
|
|
45
|
+
export interface Unfilled {
|
|
46
|
+
id: string;
|
|
47
|
+
name: string;
|
|
48
|
+
code: Refusal["code"];
|
|
49
|
+
message: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const FILLABLE = new Set(["text", "richText", "dropDown", "comboBox", "date", "checkbox"]);
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The body's controls with what a model needs of each, in document order. Not those in a block
|
|
56
|
+
* that is not read (deleted as a tracked change): they are gone to the agent, as their block is.
|
|
57
|
+
*/
|
|
58
|
+
function found(context: Context): Control[] {
|
|
59
|
+
const { editor, handles } = context;
|
|
60
|
+
const doc = editor.state.doc;
|
|
61
|
+
// A read first: handles are issued in the document's order, whatever is asked first.
|
|
62
|
+
const read = new Set(readBlocks(editor.state, handles).map((block) => block.handle));
|
|
63
|
+
const ranges = new Map<string, ReturnType<typeof controlsIn>[number]>();
|
|
64
|
+
// The first range of an id, as `controlValues` reports the first.
|
|
65
|
+
for (const range of controlsIn(doc)) if (!ranges.has(range.id)) ranges.set(range.id, range);
|
|
66
|
+
const out: Control[] = [];
|
|
67
|
+
for (const value of controlValues(editor.state)) {
|
|
68
|
+
const range = ranges.get(value.id);
|
|
69
|
+
const properties = controlProperties(editor.state, value.id);
|
|
70
|
+
if (!range || !properties) continue;
|
|
71
|
+
// The innermost block around the control's content: a paragraph, for all but a row's.
|
|
72
|
+
const $from = doc.resolve(range.contentFrom);
|
|
73
|
+
let depth = $from.depth;
|
|
74
|
+
while (depth > 1 && !$from.node(depth).isBlock) depth--;
|
|
75
|
+
const pos = depth > 0 ? $from.before(depth) : range.from;
|
|
76
|
+
const node = doc.nodeAt(pos);
|
|
77
|
+
if (!node) continue;
|
|
78
|
+
const block = handles.at(pos, node);
|
|
79
|
+
if (!read.has(block)) continue;
|
|
80
|
+
// A list's value reads as its item is shown: what `items` lists and `fillControl` takes.
|
|
81
|
+
const item = properties.items?.find((i) => i.value === value.value);
|
|
82
|
+
const control: Control = {
|
|
83
|
+
id: value.id,
|
|
84
|
+
name: oneLine(value.tag || value.title || value.id),
|
|
85
|
+
kind: value.kind,
|
|
86
|
+
value: shownValue(item?.display ?? value.value),
|
|
87
|
+
empty: value.showingPlaceholder,
|
|
88
|
+
readOnly: controlIsReadOnly(properties),
|
|
89
|
+
block,
|
|
90
|
+
};
|
|
91
|
+
if (value.tag) control.tag = oneLine(value.tag);
|
|
92
|
+
if (value.title) control.title = oneLine(value.title);
|
|
93
|
+
if (properties.items)
|
|
94
|
+
control.items = properties.items.map((i) => oneLine(i.display ?? i.value));
|
|
95
|
+
out.push(control);
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** @internal */
|
|
101
|
+
export function controls(context: Context): Control[] {
|
|
102
|
+
return found(context);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** A control's value as it is given: its text on one line, as all a document names. */
|
|
106
|
+
const shownValue = (value: string | boolean | null) =>
|
|
107
|
+
typeof value === "string" ? oneLine(value) : value;
|
|
108
|
+
|
|
109
|
+
const same = (a: string | undefined, b: string) => a?.trim().toLowerCase() === b;
|
|
110
|
+
const isDay = (text: string) => {
|
|
111
|
+
const day = new Date(`${text}T00:00:00Z`);
|
|
112
|
+
return !Number.isNaN(day.getTime()) && day.toISOString().slice(0, 10) === text;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/** Why `control` does not take `value`, known before trying; null when it may. */
|
|
116
|
+
function unfit(control: Control, value: string | boolean | null): Refusal | null {
|
|
117
|
+
const label = JSON.stringify(control.name);
|
|
118
|
+
if (control.readOnly)
|
|
119
|
+
return refuse("read-only", `The control ${label} is locked: it cannot be filled.`);
|
|
120
|
+
if (!FILLABLE.has(control.kind))
|
|
121
|
+
return refuse(
|
|
122
|
+
"read-only",
|
|
123
|
+
`The control ${label} is a ${control.kind} control: it holds no value to fill.`,
|
|
124
|
+
);
|
|
125
|
+
if (control.kind === "checkbox")
|
|
126
|
+
return typeof value === "boolean"
|
|
127
|
+
? null
|
|
128
|
+
: refuse("invalid", `The control ${label} is a check box: its value is true or false.`);
|
|
129
|
+
if (typeof value === "boolean")
|
|
130
|
+
return refuse("invalid", `The control ${label} takes text, not true or false.`);
|
|
131
|
+
if (control.kind === "dropDown" && value && !control.items?.includes(value))
|
|
132
|
+
return refuse(
|
|
133
|
+
"invalid",
|
|
134
|
+
`The control ${label} is a drop-down list: its value is one of ${(control.items ?? []).map((i) => JSON.stringify(i)).join(", ")}.`,
|
|
135
|
+
);
|
|
136
|
+
if (control.kind === "date" && value && /^\d{4}-\d{2}-\d{2}$/.test(value) && !isDay(value))
|
|
137
|
+
return refuse("invalid", `${JSON.stringify(value)} is not a day of the calendar.`);
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** @internal */
|
|
142
|
+
export function fillControl(
|
|
143
|
+
context: Context,
|
|
144
|
+
name: string,
|
|
145
|
+
value: string | boolean | null,
|
|
146
|
+
): Result<Changed & { controls: Control[]; unfilled: Unfilled[] }> {
|
|
147
|
+
const { editor } = context;
|
|
148
|
+
if (typeof name !== "string")
|
|
149
|
+
return refuse("invalid", "A content control is named by its id, tag or title, a string.");
|
|
150
|
+
if (value !== null && typeof value !== "string" && typeof value !== "boolean")
|
|
151
|
+
return refuse("invalid", "A control's value is text, true or false, or null to empty it.");
|
|
152
|
+
const all = found(context);
|
|
153
|
+
const wanted = oneLine(name).trim().toLowerCase();
|
|
154
|
+
// Its id names one control; a tag every control that has it; then a title.
|
|
155
|
+
const targets = [
|
|
156
|
+
all.filter((c) => c.id === name),
|
|
157
|
+
all.filter((c) => same(c.tag, wanted)),
|
|
158
|
+
all.filter((c) => same(c.title, wanted)),
|
|
159
|
+
].find((group) => group.length);
|
|
160
|
+
if (!targets) {
|
|
161
|
+
const names = all.map((c) => c.name);
|
|
162
|
+
return refuse(
|
|
163
|
+
"unknown-control",
|
|
164
|
+
names.length
|
|
165
|
+
? `There is no content control ${JSON.stringify(name)}. The controls are: ${names.join(", ")}.`
|
|
166
|
+
: "The document has no content controls.",
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
const filled: string[] = [];
|
|
170
|
+
const unfilled: Unfilled[] = [];
|
|
171
|
+
for (const control of targets) {
|
|
172
|
+
// "true" and "false" are a check box's values too; each control reads the value its way.
|
|
173
|
+
const input =
|
|
174
|
+
control.kind === "checkbox" && (value === "true" || value === "false")
|
|
175
|
+
? value === "true"
|
|
176
|
+
: value;
|
|
177
|
+
const why =
|
|
178
|
+
unfit(control, input) ??
|
|
179
|
+
// The value it has: nothing to record.
|
|
180
|
+
(control.value === input || (control.empty && (input === null || input === ""))
|
|
181
|
+
? null
|
|
182
|
+
: editor.run(setControlValue(control.id, input))
|
|
183
|
+
? null
|
|
184
|
+
: refuse("refused", `The control ${JSON.stringify(control.name)} could not be filled.`));
|
|
185
|
+
if (why)
|
|
186
|
+
unfilled.push({ id: control.id, name: control.name, code: why.code, message: why.message });
|
|
187
|
+
else filled.push(control.block);
|
|
188
|
+
}
|
|
189
|
+
const first = unfilled[0];
|
|
190
|
+
if (!filled.length && first) return refuse(first.code, first.message);
|
|
191
|
+
const ids = new Set(targets.map((t) => t.id));
|
|
192
|
+
const result = changed(context, filled);
|
|
193
|
+
if (unfilled.length) result.text += `\nNot filled: ${unfilled.map((u) => u.message).join(" ")}`;
|
|
194
|
+
return {
|
|
195
|
+
ok: true,
|
|
196
|
+
...result,
|
|
197
|
+
controls: found(context).filter((c) => ids.has(c.id)),
|
|
198
|
+
unfilled,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export interface UpdateFieldsOptions {
|
|
203
|
+
/**
|
|
204
|
+
* Values for the document's merge fields (`«Name»` in a read), by field name, capitals not
|
|
205
|
+
* counting: text, numbers, true or false, dates.
|
|
206
|
+
*/
|
|
207
|
+
data?: Readonly<Record<string, string | number | boolean | Date | null>>;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const count = (n: number, one: string, many = `${one}s`) => `${n} ${n === 1 ? one : many}`;
|
|
211
|
+
|
|
212
|
+
/** @internal */
|
|
213
|
+
export function updateFields(
|
|
214
|
+
context: Context,
|
|
215
|
+
options: UpdateFieldsOptions = {},
|
|
216
|
+
): Result<FieldsReport & { text: string }> {
|
|
217
|
+
const given = options?.data;
|
|
218
|
+
if (given !== undefined && (given === null || typeof given !== "object" || Array.isArray(given)))
|
|
219
|
+
return refuse("invalid", "data must be an object: merge field names with their values.");
|
|
220
|
+
for (const [key, value] of Object.entries(given ?? {}))
|
|
221
|
+
if (value !== null && !(value instanceof Date) && typeof value === "object")
|
|
222
|
+
return refuse("invalid", `data.${key} must be text, a number, true or false, or a date.`);
|
|
223
|
+
const report = updateFieldsOf(context.editor, given ? { data: given } : {});
|
|
224
|
+
|
|
225
|
+
// What was left, said for a model: by reason, the body's only (the session reads no other).
|
|
226
|
+
const body = report.skipped.filter((s) => s.story === null);
|
|
227
|
+
const pages = body.filter((s) => s.reason === "no-layout").length;
|
|
228
|
+
const noData = new Set(body.filter((s) => s.reason === "no-data").map((s) => s.detail ?? ""));
|
|
229
|
+
// Without data core leaves merge fields alone and does not report them: name them.
|
|
230
|
+
if (!given)
|
|
231
|
+
for (const field of fieldsIn(context.editor.state.doc)) {
|
|
232
|
+
const { name, args } = parseFieldInstruction(field.instruction);
|
|
233
|
+
if (name.toUpperCase() === "MERGEFIELD" && args[0]) noData.add(args[0]);
|
|
234
|
+
}
|
|
235
|
+
noData.delete("");
|
|
236
|
+
const others = body
|
|
237
|
+
.filter((s) => s.reason !== "no-layout" && s.reason !== "no-data")
|
|
238
|
+
.map((s) => `${s.instruction.trim()} (${s.reason}${s.detail ? `: ${s.detail}` : ""})`);
|
|
239
|
+
const done = report.updated + report.rebuilt;
|
|
240
|
+
const text = [
|
|
241
|
+
`${count(done, "field")} updated, ${report.unchanged} unchanged.`,
|
|
242
|
+
...(noData.size ? [`Merge fields without data: ${[...noData].join(", ")}.`] : []),
|
|
243
|
+
...(pages
|
|
244
|
+
? [`${count(pages, "page number")} left: they need a page layout the session does not have.`]
|
|
245
|
+
: []),
|
|
246
|
+
...(others.length ? [`Left as they were: ${others.join("; ")}.`] : []),
|
|
247
|
+
...(done ? ["Read again to see the text."] : []),
|
|
248
|
+
].join(" ");
|
|
249
|
+
// One sentence run, on one line: a field's instruction and a merge field's name are the
|
|
250
|
+
// document's, and must not start a line any more than its text may.
|
|
251
|
+
return { ok: true, ...report, text: oneLine(text) };
|
|
252
|
+
}
|