@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
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { controlIsReadOnly, controlProperties, controlsIn, controlValues, fieldsIn, parseFieldInstruction, setControlValue, updateFields as updateFieldsOf, } from "@folijs/core";
|
|
2
|
+
import { changed } from "./operations.js";
|
|
3
|
+
import { oneLine, readBlocks } from "./read.js";
|
|
4
|
+
import { refuse } from "./result.js";
|
|
5
|
+
const FILLABLE = new Set(["text", "richText", "dropDown", "comboBox", "date", "checkbox"]);
|
|
6
|
+
/**
|
|
7
|
+
* The body's controls with what a model needs of each, in document order. Not those in a block
|
|
8
|
+
* that is not read (deleted as a tracked change): they are gone to the agent, as their block is.
|
|
9
|
+
*/
|
|
10
|
+
function found(context) {
|
|
11
|
+
const { editor, handles } = context;
|
|
12
|
+
const doc = editor.state.doc;
|
|
13
|
+
// A read first: handles are issued in the document's order, whatever is asked first.
|
|
14
|
+
const read = new Set(readBlocks(editor.state, handles).map((block) => block.handle));
|
|
15
|
+
const ranges = new Map();
|
|
16
|
+
// The first range of an id, as `controlValues` reports the first.
|
|
17
|
+
for (const range of controlsIn(doc))
|
|
18
|
+
if (!ranges.has(range.id))
|
|
19
|
+
ranges.set(range.id, range);
|
|
20
|
+
const out = [];
|
|
21
|
+
for (const value of controlValues(editor.state)) {
|
|
22
|
+
const range = ranges.get(value.id);
|
|
23
|
+
const properties = controlProperties(editor.state, value.id);
|
|
24
|
+
if (!range || !properties)
|
|
25
|
+
continue;
|
|
26
|
+
// The innermost block around the control's content: a paragraph, for all but a row's.
|
|
27
|
+
const $from = doc.resolve(range.contentFrom);
|
|
28
|
+
let depth = $from.depth;
|
|
29
|
+
while (depth > 1 && !$from.node(depth).isBlock)
|
|
30
|
+
depth--;
|
|
31
|
+
const pos = depth > 0 ? $from.before(depth) : range.from;
|
|
32
|
+
const node = doc.nodeAt(pos);
|
|
33
|
+
if (!node)
|
|
34
|
+
continue;
|
|
35
|
+
const block = handles.at(pos, node);
|
|
36
|
+
if (!read.has(block))
|
|
37
|
+
continue;
|
|
38
|
+
// A list's value reads as its item is shown: what `items` lists and `fillControl` takes.
|
|
39
|
+
const item = properties.items?.find((i) => i.value === value.value);
|
|
40
|
+
const control = {
|
|
41
|
+
id: value.id,
|
|
42
|
+
name: oneLine(value.tag || value.title || value.id),
|
|
43
|
+
kind: value.kind,
|
|
44
|
+
value: shownValue(item?.display ?? value.value),
|
|
45
|
+
empty: value.showingPlaceholder,
|
|
46
|
+
readOnly: controlIsReadOnly(properties),
|
|
47
|
+
block,
|
|
48
|
+
};
|
|
49
|
+
if (value.tag)
|
|
50
|
+
control.tag = oneLine(value.tag);
|
|
51
|
+
if (value.title)
|
|
52
|
+
control.title = oneLine(value.title);
|
|
53
|
+
if (properties.items)
|
|
54
|
+
control.items = properties.items.map((i) => oneLine(i.display ?? i.value));
|
|
55
|
+
out.push(control);
|
|
56
|
+
}
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
/** @internal */
|
|
60
|
+
export function controls(context) {
|
|
61
|
+
return found(context);
|
|
62
|
+
}
|
|
63
|
+
/** A control's value as it is given: its text on one line, as all a document names. */
|
|
64
|
+
const shownValue = (value) => typeof value === "string" ? oneLine(value) : value;
|
|
65
|
+
const same = (a, b) => a?.trim().toLowerCase() === b;
|
|
66
|
+
const isDay = (text) => {
|
|
67
|
+
const day = new Date(`${text}T00:00:00Z`);
|
|
68
|
+
return !Number.isNaN(day.getTime()) && day.toISOString().slice(0, 10) === text;
|
|
69
|
+
};
|
|
70
|
+
/** Why `control` does not take `value`, known before trying; null when it may. */
|
|
71
|
+
function unfit(control, value) {
|
|
72
|
+
const label = JSON.stringify(control.name);
|
|
73
|
+
if (control.readOnly)
|
|
74
|
+
return refuse("read-only", `The control ${label} is locked: it cannot be filled.`);
|
|
75
|
+
if (!FILLABLE.has(control.kind))
|
|
76
|
+
return refuse("read-only", `The control ${label} is a ${control.kind} control: it holds no value to fill.`);
|
|
77
|
+
if (control.kind === "checkbox")
|
|
78
|
+
return typeof value === "boolean"
|
|
79
|
+
? null
|
|
80
|
+
: refuse("invalid", `The control ${label} is a check box: its value is true or false.`);
|
|
81
|
+
if (typeof value === "boolean")
|
|
82
|
+
return refuse("invalid", `The control ${label} takes text, not true or false.`);
|
|
83
|
+
if (control.kind === "dropDown" && value && !control.items?.includes(value))
|
|
84
|
+
return refuse("invalid", `The control ${label} is a drop-down list: its value is one of ${(control.items ?? []).map((i) => JSON.stringify(i)).join(", ")}.`);
|
|
85
|
+
if (control.kind === "date" && value && /^\d{4}-\d{2}-\d{2}$/.test(value) && !isDay(value))
|
|
86
|
+
return refuse("invalid", `${JSON.stringify(value)} is not a day of the calendar.`);
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
/** @internal */
|
|
90
|
+
export function fillControl(context, name, value) {
|
|
91
|
+
const { editor } = context;
|
|
92
|
+
if (typeof name !== "string")
|
|
93
|
+
return refuse("invalid", "A content control is named by its id, tag or title, a string.");
|
|
94
|
+
if (value !== null && typeof value !== "string" && typeof value !== "boolean")
|
|
95
|
+
return refuse("invalid", "A control's value is text, true or false, or null to empty it.");
|
|
96
|
+
const all = found(context);
|
|
97
|
+
const wanted = oneLine(name).trim().toLowerCase();
|
|
98
|
+
// Its id names one control; a tag every control that has it; then a title.
|
|
99
|
+
const targets = [
|
|
100
|
+
all.filter((c) => c.id === name),
|
|
101
|
+
all.filter((c) => same(c.tag, wanted)),
|
|
102
|
+
all.filter((c) => same(c.title, wanted)),
|
|
103
|
+
].find((group) => group.length);
|
|
104
|
+
if (!targets) {
|
|
105
|
+
const names = all.map((c) => c.name);
|
|
106
|
+
return refuse("unknown-control", names.length
|
|
107
|
+
? `There is no content control ${JSON.stringify(name)}. The controls are: ${names.join(", ")}.`
|
|
108
|
+
: "The document has no content controls.");
|
|
109
|
+
}
|
|
110
|
+
const filled = [];
|
|
111
|
+
const unfilled = [];
|
|
112
|
+
for (const control of targets) {
|
|
113
|
+
// "true" and "false" are a check box's values too; each control reads the value its way.
|
|
114
|
+
const input = control.kind === "checkbox" && (value === "true" || value === "false")
|
|
115
|
+
? value === "true"
|
|
116
|
+
: value;
|
|
117
|
+
const why = unfit(control, input) ??
|
|
118
|
+
// The value it has: nothing to record.
|
|
119
|
+
(control.value === input || (control.empty && (input === null || input === ""))
|
|
120
|
+
? null
|
|
121
|
+
: editor.run(setControlValue(control.id, input))
|
|
122
|
+
? null
|
|
123
|
+
: refuse("refused", `The control ${JSON.stringify(control.name)} could not be filled.`));
|
|
124
|
+
if (why)
|
|
125
|
+
unfilled.push({ id: control.id, name: control.name, code: why.code, message: why.message });
|
|
126
|
+
else
|
|
127
|
+
filled.push(control.block);
|
|
128
|
+
}
|
|
129
|
+
const first = unfilled[0];
|
|
130
|
+
if (!filled.length && first)
|
|
131
|
+
return refuse(first.code, first.message);
|
|
132
|
+
const ids = new Set(targets.map((t) => t.id));
|
|
133
|
+
const result = changed(context, filled);
|
|
134
|
+
if (unfilled.length)
|
|
135
|
+
result.text += `\nNot filled: ${unfilled.map((u) => u.message).join(" ")}`;
|
|
136
|
+
return {
|
|
137
|
+
ok: true,
|
|
138
|
+
...result,
|
|
139
|
+
controls: found(context).filter((c) => ids.has(c.id)),
|
|
140
|
+
unfilled,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
const count = (n, one, many = `${one}s`) => `${n} ${n === 1 ? one : many}`;
|
|
144
|
+
/** @internal */
|
|
145
|
+
export function updateFields(context, options = {}) {
|
|
146
|
+
const given = options?.data;
|
|
147
|
+
if (given !== undefined && (given === null || typeof given !== "object" || Array.isArray(given)))
|
|
148
|
+
return refuse("invalid", "data must be an object: merge field names with their values.");
|
|
149
|
+
for (const [key, value] of Object.entries(given ?? {}))
|
|
150
|
+
if (value !== null && !(value instanceof Date) && typeof value === "object")
|
|
151
|
+
return refuse("invalid", `data.${key} must be text, a number, true or false, or a date.`);
|
|
152
|
+
const report = updateFieldsOf(context.editor, given ? { data: given } : {});
|
|
153
|
+
// What was left, said for a model: by reason, the body's only (the session reads no other).
|
|
154
|
+
const body = report.skipped.filter((s) => s.story === null);
|
|
155
|
+
const pages = body.filter((s) => s.reason === "no-layout").length;
|
|
156
|
+
const noData = new Set(body.filter((s) => s.reason === "no-data").map((s) => s.detail ?? ""));
|
|
157
|
+
// Without data core leaves merge fields alone and does not report them: name them.
|
|
158
|
+
if (!given)
|
|
159
|
+
for (const field of fieldsIn(context.editor.state.doc)) {
|
|
160
|
+
const { name, args } = parseFieldInstruction(field.instruction);
|
|
161
|
+
if (name.toUpperCase() === "MERGEFIELD" && args[0])
|
|
162
|
+
noData.add(args[0]);
|
|
163
|
+
}
|
|
164
|
+
noData.delete("");
|
|
165
|
+
const others = body
|
|
166
|
+
.filter((s) => s.reason !== "no-layout" && s.reason !== "no-data")
|
|
167
|
+
.map((s) => `${s.instruction.trim()} (${s.reason}${s.detail ? `: ${s.detail}` : ""})`);
|
|
168
|
+
const done = report.updated + report.rebuilt;
|
|
169
|
+
const text = [
|
|
170
|
+
`${count(done, "field")} updated, ${report.unchanged} unchanged.`,
|
|
171
|
+
...(noData.size ? [`Merge fields without data: ${[...noData].join(", ")}.`] : []),
|
|
172
|
+
...(pages
|
|
173
|
+
? [`${count(pages, "page number")} left: they need a page layout the session does not have.`]
|
|
174
|
+
: []),
|
|
175
|
+
...(others.length ? [`Left as they were: ${others.join("; ")}.`] : []),
|
|
176
|
+
...(done ? ["Read again to see the text."] : []),
|
|
177
|
+
].join(" ");
|
|
178
|
+
// One sentence run, on one line: a field's instruction and a merge field's name are the
|
|
179
|
+
// document's, and must not start a line any more than its text may.
|
|
180
|
+
return { ok: true, ...report, text: oneLine(text) };
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=templates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"templates.js","sourceRoot":"","sources":["../src/templates.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,iBAAiB,EACjB,iBAAiB,EACjB,UAAU,EACV,aAAa,EAEb,QAAQ,EACR,qBAAqB,EACrB,eAAe,EACf,YAAY,IAAI,cAAc,GAC/B,MAAM,cAAc,CAAC;AACtB,OAAO,EAA8B,OAAO,EAAE,MAAM,iBAAiB,CAAC;AACtE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EAA6B,MAAM,EAAE,MAAM,aAAa,CAAC;AAqChE,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,EAAE,UAA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HANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@folijs/agent",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Agent editing for the Foli document engine: an LLM reads a document as text with a handle per block and edits it with a few coarse operations, every edit a tracked change. No DOM, no Node built-ins.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/josep-audenis/foli.git",
|
|
9
|
+
"directory": "packages/agent"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/josep-audenis/foli#readme",
|
|
12
|
+
"bugs": "https://github.com/josep-audenis/foli/issues",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"CHANGELOG.md",
|
|
24
|
+
"src",
|
|
25
|
+
"!src/**/*.test.*",
|
|
26
|
+
"!src/testing"
|
|
27
|
+
],
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@folijs/core": "1.1.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/node": "^20.19.43",
|
|
33
|
+
"fast-check": "^4.10.2",
|
|
34
|
+
"@folijs/core": "1.1.0",
|
|
35
|
+
"@folijs/docx": "1.1.0"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.build.json",
|
|
39
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
40
|
+
}
|
|
6
41
|
}
|
package/src/handles.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { Editor, Node } from "@folijs/core";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
* Names for blocks that outlive edits: `b1`, `b2`… in the order they were first read.
|
|
6
|
+
*
|
|
7
|
+
* A handle is the position after its block, moved through every transaction's mapping: the
|
|
8
|
+
* block's closing, which for a paragraph is Word's paragraph mark, what a paragraph is in Word.
|
|
9
|
+
* Enter in a paragraph gives the text before the caret a new mark, so the handle stays with what
|
|
10
|
+
* comes after it; a paragraph joined with the next one loses its mark, and its handle; accepting
|
|
11
|
+
* a deleted paragraph or rejecting an inserted one leaves the paragraphs around under their
|
|
12
|
+
* handles. `w14:paraId` cannot do this: paragraphs Foli creates have none (roadmap, Phase 13).
|
|
13
|
+
*
|
|
14
|
+
* A block that is removed and comes back by undo comes back without its handle.
|
|
15
|
+
*/
|
|
16
|
+
export class Handles {
|
|
17
|
+
readonly #editor: Editor;
|
|
18
|
+
/** Each handle's position: after its block. */
|
|
19
|
+
readonly #ends = new Map<string, number>();
|
|
20
|
+
#byEnd: Map<number, string> | null = null;
|
|
21
|
+
#issued = 0;
|
|
22
|
+
|
|
23
|
+
constructor(editor: Editor) {
|
|
24
|
+
this.#editor = editor;
|
|
25
|
+
let last = editor.state.doc;
|
|
26
|
+
editor.subscribe((state, transactions) => {
|
|
27
|
+
// `setDocument` tells without a transaction: no position of the old document holds.
|
|
28
|
+
if (!transactions.length && state.doc !== last) this.#ends.clear();
|
|
29
|
+
last = state.doc;
|
|
30
|
+
for (const tr of transactions) {
|
|
31
|
+
if (!tr.steps.length) continue;
|
|
32
|
+
for (const [handle, end] of this.#ends) {
|
|
33
|
+
// The two sides of the block's closing. A block removed, or joined with the next,
|
|
34
|
+
// loses one of them or sees them part, and the handle goes.
|
|
35
|
+
const before = tr.mapping.mapResult(end - 1, 1);
|
|
36
|
+
const after = tr.mapping.mapResult(end, -1);
|
|
37
|
+
if (before.deleted || after.deleted || after.pos !== before.pos + 1)
|
|
38
|
+
this.#ends.delete(handle);
|
|
39
|
+
else this.#ends.set(handle, after.pos);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
this.#byEnd = null;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The handle of the block `node` at `pos` (before it), a new one if it has none yet. */
|
|
47
|
+
at(pos: number, node: Node): string {
|
|
48
|
+
const end = pos + node.nodeSize;
|
|
49
|
+
this.#byEnd ??= new Map([...this.#ends].map(([handle, at]) => [at, handle]));
|
|
50
|
+
let handle = this.#byEnd.get(end);
|
|
51
|
+
if (!handle) {
|
|
52
|
+
handle = `b${++this.#issued}`;
|
|
53
|
+
this.#ends.set(handle, end);
|
|
54
|
+
this.#byEnd.set(end, handle);
|
|
55
|
+
}
|
|
56
|
+
return handle;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Whether `handle` was ever issued. */
|
|
60
|
+
issued(handle: string): boolean {
|
|
61
|
+
const n = /^b([1-9]\d*)$/.exec(handle);
|
|
62
|
+
return !!n && Number(n[1]) <= this.#issued;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Where the block `handle` names is now (before it), with the block; null when it is gone. */
|
|
66
|
+
resolve(handle: string): { pos: number; node: Node } | null {
|
|
67
|
+
const end = this.#ends.get(handle);
|
|
68
|
+
const doc = this.#editor.state.doc;
|
|
69
|
+
if (end === undefined || end > doc.content.size) return null;
|
|
70
|
+
const node = doc.resolve(end).nodeBefore;
|
|
71
|
+
return node?.isBlock ? { pos: end - node.nodeSize, node } : null;
|
|
72
|
+
}
|
|
73
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type { Changed, CommentOptions, NewParagraph, ReplaceOptions } from "./operations.js";
|
|
2
|
+
export { type Block, formatBlocks } from "./read.js";
|
|
3
|
+
export type { Ok, Refusal, Result } from "./result.js";
|
|
4
|
+
export {
|
|
5
|
+
openSession,
|
|
6
|
+
type ReadOptions,
|
|
7
|
+
type ReadResult,
|
|
8
|
+
type Session,
|
|
9
|
+
type SessionOptions,
|
|
10
|
+
} from "./session.js";
|
|
11
|
+
export type { Control, Unfilled, UpdateFieldsOptions } from "./templates.js";
|