@stll/folio-agents 0.6.1 → 0.8.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/dist/execute.js +50 -5
- package/dist/operation-schema.d.ts +615 -25
- package/dist/operation-schema.js +154 -8
- package/dist/parse.d.ts +14 -1
- package/dist/parse.js +149 -10
- package/dist/tools.js +52 -21
- package/dist/types.d.ts +14 -4
- package/package.json +2 -2
package/dist/execute.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { explainTextTooLong, parseAddCommentInput, parseSuggestChangesInput } from "./parse.js";
|
|
2
2
|
import { FOLIO_AGENT_TOOL_NAMES } from "./types.js";
|
|
3
|
-
import { FOLIO_DOCUMENT_OPERATION_CONTRACT_VERSION, createFolioAITextRangeHandle, getFolioDocumentOutline, readFolioDocumentSection } from "@stll/folio-core/server";
|
|
3
|
+
import { FOLIO_DOCUMENT_OPERATION_CONTRACT_VERSION, createFolioAITextRangeHandle, getFolioDocumentOutline, hashFolioAIBlockText, normalizeFolioAIBlockText, readFolioDocumentSection } from "@stll/folio-core/server";
|
|
4
4
|
//#region src/execute.ts
|
|
5
5
|
const isPlainObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
6
6
|
const isNonEmptyString = (value) => typeof value === "string" && value.length > 0;
|
|
@@ -13,6 +13,15 @@ const fail = (error) => ({
|
|
|
13
13
|
error
|
|
14
14
|
});
|
|
15
15
|
const VALID_TOOL_NAMES = Object.values(FOLIO_AGENT_TOOL_NAMES);
|
|
16
|
+
/**
|
|
17
|
+
* The same normalized-text hash the core snapshot/apply machinery uses for
|
|
18
|
+
* `precondition.blockTextHash` (see `hashFolioAIBlockText` /
|
|
19
|
+
* `normalizeFolioAIBlockText`). Computed straight from a block's `text`
|
|
20
|
+
* rather than looked up from `snapshot.anchors` so every read path (main
|
|
21
|
+
* document, a section, a find_text match) can attach it without threading
|
|
22
|
+
* the anchors map around — it is defined to produce the exact same value.
|
|
23
|
+
*/
|
|
24
|
+
const blockTextHashOf = (text) => hashFolioAIBlockText(normalizeFolioAIBlockText(text));
|
|
16
25
|
/** `find_text` requires a short `query` and caps how much of a large match set it returns in one call. */
|
|
17
26
|
const MAX_QUERY_LENGTH = 1e3;
|
|
18
27
|
const MAX_FIND_MATCHES = 200;
|
|
@@ -60,7 +69,8 @@ const readDocument = (bridge) => {
|
|
|
60
69
|
return ok(blocks.map((block) => ({
|
|
61
70
|
blockId: block.id,
|
|
62
71
|
kind: block.kind,
|
|
63
|
-
text: block.text
|
|
72
|
+
text: block.text,
|
|
73
|
+
blockTextHash: blockTextHashOf(block.text)
|
|
64
74
|
})));
|
|
65
75
|
};
|
|
66
76
|
const parseSectionHandle = (value) => {
|
|
@@ -122,7 +132,8 @@ const readSection = (args, bridge) => {
|
|
|
122
132
|
blocks: selected.map(({ id, kind, text }) => ({
|
|
123
133
|
blockId: id,
|
|
124
134
|
kind,
|
|
125
|
-
text
|
|
135
|
+
text,
|
|
136
|
+
blockTextHash: blockTextHashOf(text)
|
|
126
137
|
})),
|
|
127
138
|
totalBlocks: resolved.section.blocks.length,
|
|
128
139
|
truncated: hasMore,
|
|
@@ -162,6 +173,16 @@ const readStory = (args, bridge) => {
|
|
|
162
173
|
const CONTEXT_RADIUS = 40;
|
|
163
174
|
const WORD_CHARACTER_AT_END = /[\p{L}\p{M}\p{N}_]$/u;
|
|
164
175
|
const WORD_CHARACTER_AT_START = /^[\p{L}\p{M}\p{N}_]/u;
|
|
176
|
+
/**
|
|
177
|
+
* Window (UTF-16 code units) sliced on each side of a match for the
|
|
178
|
+
* whole-word boundary check. The regexes above only ever test the single
|
|
179
|
+
* grapheme adjacent to the match, but a surrogate pair or a base character
|
|
180
|
+
* with stacked combining marks can span a few code units — this window is
|
|
181
|
+
* generous enough to cover that while staying a constant, not
|
|
182
|
+
* `block.text.length`. Without a bound, `.slice(0, at)` / `.slice(at + len)`
|
|
183
|
+
* on every match makes a whole-word search O(n^2) in the block's length.
|
|
184
|
+
*/
|
|
185
|
+
const WORD_BOUNDARY_WINDOW = 8;
|
|
165
186
|
const findTextMatches = (blocks, query, matchCase, wholeWord, getTargetPage, pageFilter) => {
|
|
166
187
|
const matches = [];
|
|
167
188
|
let totalMatches = 0;
|
|
@@ -169,10 +190,11 @@ const findTextMatches = (blocks, query, matchCase, wholeWord, getTargetPage, pag
|
|
|
169
190
|
const expression = new RegExp(escapedQuery, matchCase ? "gu" : "giu");
|
|
170
191
|
for (const block of blocks) {
|
|
171
192
|
let occurrence = 0;
|
|
193
|
+
let blockTextHash;
|
|
172
194
|
for (const match of block.text.matchAll(expression)) {
|
|
173
195
|
const at = match.index;
|
|
174
196
|
const matchedText = match[0];
|
|
175
|
-
if (wholeWord && (WORD_CHARACTER_AT_END.test(block.text.slice(0, at)) || WORD_CHARACTER_AT_START.test(block.text.slice(at + matchedText.length)))) continue;
|
|
197
|
+
if (wholeWord && (WORD_CHARACTER_AT_END.test(block.text.slice(Math.max(0, at - WORD_BOUNDARY_WINDOW), at)) || WORD_CHARACTER_AT_START.test(block.text.slice(at + matchedText.length, at + matchedText.length + WORD_BOUNDARY_WINDOW)))) continue;
|
|
176
198
|
const range = createFolioAITextRangeHandle({
|
|
177
199
|
blockId: block.id,
|
|
178
200
|
text: block.text,
|
|
@@ -186,10 +208,12 @@ const findTextMatches = (blocks, query, matchCase, wholeWord, getTargetPage, pag
|
|
|
186
208
|
if (matches.length < MAX_FIND_MATCHES) {
|
|
187
209
|
const contextStart = Math.max(0, at - CONTEXT_RADIUS);
|
|
188
210
|
const contextEnd = Math.min(block.text.length, at + matchedText.length + CONTEXT_RADIUS);
|
|
211
|
+
blockTextHash ??= blockTextHashOf(block.text);
|
|
189
212
|
matches.push({
|
|
190
213
|
type: "main",
|
|
191
214
|
story: { type: "main" },
|
|
192
215
|
blockId: block.id,
|
|
216
|
+
blockTextHash,
|
|
193
217
|
range,
|
|
194
218
|
occurrenceInBlock: occurrence,
|
|
195
219
|
context: block.text.slice(contextStart, contextEnd),
|
|
@@ -212,7 +236,7 @@ const findStoryTextMatches = (text, story, query, matchCase, wholeWord) => {
|
|
|
212
236
|
for (const match of text.matchAll(expression)) {
|
|
213
237
|
const at = match.index;
|
|
214
238
|
const matchedText = match[0];
|
|
215
|
-
if (wholeWord && (WORD_CHARACTER_AT_END.test(text.slice(0, at)) || WORD_CHARACTER_AT_START.test(text.slice(at + matchedText.length)))) continue;
|
|
239
|
+
if (wholeWord && (WORD_CHARACTER_AT_END.test(text.slice(Math.max(0, at - WORD_BOUNDARY_WINDOW), at)) || WORD_CHARACTER_AT_START.test(text.slice(at + matchedText.length, at + matchedText.length + WORD_BOUNDARY_WINDOW)))) continue;
|
|
216
240
|
if (matches.length < MAX_FIND_MATCHES) matches.push({
|
|
217
241
|
type: "story",
|
|
218
242
|
story,
|
|
@@ -318,10 +342,31 @@ const summarizeApplyResult = (result) => ({
|
|
|
318
342
|
issues: result.issues ?? [],
|
|
319
343
|
receipts: result.receipts ?? []
|
|
320
344
|
});
|
|
345
|
+
/**
|
|
346
|
+
* Attach a `precondition.blockTextHash` guard to every operation before
|
|
347
|
+
* handing the batch to the bridge.
|
|
348
|
+
*
|
|
349
|
+
* When the caller (the model) already echoed a `precondition` on the
|
|
350
|
+
* operation — sourced from a `blockTextHash` returned by an earlier
|
|
351
|
+
* `read_document` / `read_section` / `find_text` call — that is honored
|
|
352
|
+
* as-is: it is the only signal that can catch a document edit made
|
|
353
|
+
* BETWEEN that read and this apply call.
|
|
354
|
+
*
|
|
355
|
+
* When no precondition was supplied, fall back to stamping one from this
|
|
356
|
+
* call's own fresh `bridge.snapshot()`, matching the previous behavior.
|
|
357
|
+
* This fallback cannot detect cross-call staleness — the snapshot it reads
|
|
358
|
+
* from is taken right before applying, so it always matches the current
|
|
359
|
+
* live document — but it still guards a later operation in the SAME batch
|
|
360
|
+
* against an earlier operation in that batch touching the same block.
|
|
361
|
+
*/
|
|
321
362
|
const applyOperations = (bridge, operations) => {
|
|
322
363
|
const snapshot = bridge.snapshot();
|
|
323
364
|
const guardedOperations = [];
|
|
324
365
|
for (const operation of operations) {
|
|
366
|
+
if (operation.precondition !== void 0) {
|
|
367
|
+
guardedOperations.push(operation);
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
325
370
|
const blockId = operation.type === "replaceRange" || operation.type === "commentOnRange" || operation.type === "formatRange" ? operation.range.blockId : operation.blockId;
|
|
326
371
|
const blockTextHash = snapshot.anchors[blockId]?.textHash;
|
|
327
372
|
guardedOperations.push({
|