@stll/folio-agents 0.4.1 → 0.5.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/README.md +36 -21
- package/dist/bridge.d.ts +4 -2
- package/dist/bridges/editor-ref.d.ts +13 -8
- package/dist/bridges/editor-ref.js +7 -1
- package/dist/execute.js +209 -33
- package/dist/index.d.ts +2 -2
- package/dist/tools.js +159 -20
- package/dist/types.d.ts +46 -6
- package/dist/types.js +4 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,19 +18,24 @@ bun add @stll/folio-agents
|
|
|
18
18
|
|
|
19
19
|
## Tools
|
|
20
20
|
|
|
21
|
-
| Tool
|
|
22
|
-
|
|
|
23
|
-
| `read_document`
|
|
24
|
-
| `
|
|
25
|
-
| `
|
|
26
|
-
| `
|
|
27
|
-
| `
|
|
28
|
-
| `
|
|
29
|
-
| `
|
|
30
|
-
| `
|
|
31
|
-
| `
|
|
32
|
-
| `
|
|
33
|
-
| `
|
|
21
|
+
| Tool | What it does |
|
|
22
|
+
| ---------------------- | ---------------------------------------------------------------------- |
|
|
23
|
+
| `read_document` | Read the document body as `{ blockId, kind, text }` blocks |
|
|
24
|
+
| `get_document_outline` | Read heading hierarchy and stable section handles |
|
|
25
|
+
| `read_section` | Read a bounded logical section, with block cursor pagination |
|
|
26
|
+
| `list_stories` | List main, header, footer, footnote, and endnote story handles |
|
|
27
|
+
| `read_story` | Read one story by its typed handle |
|
|
28
|
+
| `find_text` | Search by document, section, real page, or story scope |
|
|
29
|
+
| `read_comments` | Read comment threads (author, text, resolved, anchored block, replies) |
|
|
30
|
+
| `read_changes` | Read pending tracked changes (insertions/deletions) awaiting review |
|
|
31
|
+
| `add_comment` | Attach a comment to a block, optionally quoting specific text |
|
|
32
|
+
| `suggest_changes` | Propose block or stable-range edits as tracked changes |
|
|
33
|
+
| `reply_comment` | Reply to a comment thread |
|
|
34
|
+
| `resolve_comment` | Resolve or reopen a comment thread |
|
|
35
|
+
| `read_page` | Read a page's plain text (live editor only) |
|
|
36
|
+
| `read_selection` | Read the current text selection (live editor only) |
|
|
37
|
+
| `scroll_to_block` | Scroll the live editor to a block (live editor only) |
|
|
38
|
+
| `show_in_document` | Reveal a stable block or exact text range (live editor only) |
|
|
34
39
|
|
|
35
40
|
Block ids and comment ids always come from a prior tool call
|
|
36
41
|
(`read_document`, `find_text`, `read_comments`) within the same conversation —
|
|
@@ -39,11 +44,20 @@ operation is skipped (e.g. the block changed since it was last read), so the
|
|
|
39
44
|
model can re-read and retry. Successful mutation results include `receipts`
|
|
40
45
|
that identify affected blocks, ranges, insertions, and created comments.
|
|
41
46
|
|
|
47
|
+
For document questions, start with `get_document_outline`, then call
|
|
48
|
+
`read_section` or scoped `find_text`. This keeps unrelated contract text out
|
|
49
|
+
of model context. Section handles use Folio block identities, heading depth,
|
|
50
|
+
and a text hash, so structurally changed, renamed, or deleted headings fail
|
|
51
|
+
stale instead of resolving to the wrong content. Page scopes and page numbers
|
|
52
|
+
use Folio's live layout; they are never approximated from character counts and
|
|
53
|
+
therefore require a live, paginated editor.
|
|
54
|
+
|
|
42
55
|
### Untrusted documents
|
|
43
56
|
|
|
44
|
-
`read_document`, `
|
|
45
|
-
document content verbatim. If a `.docx`
|
|
46
|
-
text can carry prompt-injection payloads
|
|
57
|
+
`read_document`, `read_section`, `read_story`, `read_page`, `read_comments`,
|
|
58
|
+
`read_changes`, and `find_text` return document content verbatim. If a `.docx`
|
|
59
|
+
comes from an untrusted party, its text can carry prompt-injection payloads
|
|
60
|
+
straight into the model's context —
|
|
47
61
|
treat any document-derived tool result as untrusted model input, the same way
|
|
48
62
|
you would treat a fetched web page. Mutations stay safe by design regardless:
|
|
49
63
|
`suggest_changes` and `add_comment` land as tracked changes or comments
|
|
@@ -90,11 +104,12 @@ const result = executeFolioToolCall("suggest_changes", { operations: [...] }, br
|
|
|
90
104
|
```
|
|
91
105
|
|
|
92
106
|
On a `DocxEditorRef` that implements the read surface (`getTrackedChanges`,
|
|
93
|
-
`getCommentAnchors`, `getSelectionText`, `getPageText`
|
|
94
|
-
has full parity with the headless one:
|
|
95
|
-
changes, comment entries carry a resolved
|
|
96
|
-
/ `read_selection` work against the live
|
|
97
|
-
predates those methods, the bridge degrades
|
|
107
|
+
`getCommentAnchors`, `getSelectionText`, `getPageText`, `getTargetPage`, and
|
|
108
|
+
`showInDocument`), the editor-ref bridge has full parity with the headless one:
|
|
109
|
+
`read_changes` returns real tracked changes, comment entries carry a resolved
|
|
110
|
+
`blockId` / `quote`, and `read_page` / `read_selection` work against the live
|
|
111
|
+
view. Against an older ref that predates those methods, the bridge degrades
|
|
112
|
+
per-member: `read_changes`
|
|
98
113
|
returns `[]`, comment entries fall back to `blockId: null` / `quote: ""`, and
|
|
99
114
|
`read_page` / `read_selection` report an unsupported-capability error — see
|
|
100
115
|
`src/bridges/editor-ref.ts` for the exact fallback per method.
|
package/dist/bridge.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FolioAgentChange, FolioAgentComment } from "./types.js";
|
|
2
|
-
import { FolioAIEditSnapshot, FolioDocumentOperationBatch, FolioDocumentOperationResult, FolioDocumentOperationUndoHandle, FolioDocumentOperationUndoResult, FolioDocumentStory, FolioDocumentStoryHandle } from "@stll/folio-core/server";
|
|
2
|
+
import { FolioAIEditSnapshot, FolioDocumentNavigationTarget, FolioDocumentOperationBatch, FolioDocumentOperationResult, FolioDocumentOperationUndoHandle, FolioDocumentOperationUndoResult, FolioDocumentStory, FolioDocumentStoryHandle } from "@stll/folio-core/server";
|
|
3
3
|
|
|
4
4
|
//#region src/bridge.d.ts
|
|
5
5
|
/**
|
|
@@ -32,7 +32,9 @@ type FolioAgentBridge = {
|
|
|
32
32
|
scrollToBlock?(blockId: string): boolean; /** The user's current text selection in the live editor, as plain text. */
|
|
33
33
|
getSelectionText?(): string; /** Total page count in the live, paginated editor. */
|
|
34
34
|
getPageCount?(): number; /** Plain text of the given 1-based page in the live editor. */
|
|
35
|
-
getPageText?(page: number): string;
|
|
35
|
+
getPageText?(page: number): string; /** Resolve a main-story block or exact text range to its real rendered page. */
|
|
36
|
+
getTargetPage?(target: FolioDocumentNavigationTarget): number | null; /** Select and reveal a stable block or text range in the live editor. */
|
|
37
|
+
showInDocument?(target: FolioDocumentNavigationTarget): boolean;
|
|
36
38
|
};
|
|
37
39
|
//#endregion
|
|
38
40
|
export { FolioAgentBridge };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FolioAgentBridge } from "../bridge.js";
|
|
2
|
-
import { FolioAIEditApplyMode, FolioAIEditApplyResult, FolioAIEditOperation, FolioAIEditSnapshot, FolioDocumentOperationBatch, FolioDocumentOperationResult, FolioDocumentOperationUndoHandle, FolioDocumentOperationUndoResult } from "@stll/folio-core/server";
|
|
2
|
+
import { FolioAIEditApplyMode, FolioAIEditApplyResult, FolioAIEditOperation, FolioAIEditSnapshot, FolioDocumentNavigationTarget, FolioDocumentOperationBatch, FolioDocumentOperationResult, FolioDocumentOperationUndoHandle, FolioDocumentOperationUndoResult } from "@stll/folio-core/server";
|
|
3
3
|
import { FolioCommentAnchor, FolioReviewChange as FolioReviewChange$1 } from "@stll/folio-core/ai-edits";
|
|
4
4
|
import { Comment } from "@stll/folio-core/types/content";
|
|
5
5
|
|
|
@@ -16,11 +16,12 @@ type FolioAgentEditorApplyDocumentOperationsOptions = {
|
|
|
16
16
|
* ONLY the members actually called below.
|
|
17
17
|
*
|
|
18
18
|
* The read-surface members (`getTrackedChanges`, `getCommentAnchors`,
|
|
19
|
-
* `getSelectionText`, `getPageText`) are
|
|
20
|
-
* current `DocxEditorRef` always implements
|
|
21
|
-
* older `@stll/folio-react` (before these methods
|
|
22
|
-
* structurally satisfies this type, and `createEditorRefBridge`
|
|
23
|
-
* back to the pre-existing degraded behavior for each one it does
|
|
19
|
+
* `getSelectionText`, `getPageText`, `getTargetPage`, `showInDocument`) are
|
|
20
|
+
* OPTIONAL here even though the current `DocxEditorRef` always implements
|
|
21
|
+
* them: a ref built against an older `@stll/folio-react` (before these methods
|
|
22
|
+
* existed) still structurally satisfies this type, and `createEditorRefBridge`
|
|
23
|
+
* below falls back to the pre-existing degraded behavior for each one it does
|
|
24
|
+
* not find.
|
|
24
25
|
*/
|
|
25
26
|
type FolioAgentEditorRefLike = {
|
|
26
27
|
/** `DocxEditorRef.createAIEditSnapshot`. `null` before the editor view mounts. */createAIEditSnapshot(): FolioAIEditSnapshot | null; /** `DocxEditorRef.applyAIEditOperations`. */
|
|
@@ -58,7 +59,9 @@ type FolioAgentEditorRefLike = {
|
|
|
58
59
|
* on a ref that lacks it. See {@link createEditorRefBridge} for how a
|
|
59
60
|
* `null` return (page in range, layout not yet computed) is handled.
|
|
60
61
|
*/
|
|
61
|
-
getPageText?(page: number): string | null;
|
|
62
|
+
getPageText?(page: number): string | null; /** `DocxEditorRef.getTargetPage`, when available on newer refs. */
|
|
63
|
+
getTargetPage?(target: FolioDocumentNavigationTarget, snapshot?: FolioAIEditSnapshot): number | null; /** `DocxEditorRef.showInDocument`, when available on newer refs. */
|
|
64
|
+
showInDocument?(target: FolioDocumentNavigationTarget, snapshot?: FolioAIEditSnapshot): boolean;
|
|
62
65
|
};
|
|
63
66
|
/** Options for {@link createEditorRefBridge}. */
|
|
64
67
|
type CreateEditorRefBridgeOptions = {
|
|
@@ -78,7 +81,7 @@ type CreateEditorRefBridgeOptions = {
|
|
|
78
81
|
* to `DocxEditor`.
|
|
79
82
|
*
|
|
80
83
|
* KNOWN LIMITATIONS (only apply to a `ref` that predates the read-surface
|
|
81
|
-
* additions below; the current `DocxEditorRef` implements all
|
|
84
|
+
* additions below; the current `DocxEditorRef` implements all six):
|
|
82
85
|
* - `getChanges()` returns `[]` when `ref.getTrackedChanges` is absent, since
|
|
83
86
|
* there is then no ref-level way to enumerate tracked changes from
|
|
84
87
|
* ProseMirror mark attributes.
|
|
@@ -91,6 +94,8 @@ type CreateEditorRefBridgeOptions = {
|
|
|
91
94
|
* entirely rather than implementing it as a no-op, which is what tells
|
|
92
95
|
* `executeFolioToolCall` to report the tool as unsupported instead of
|
|
93
96
|
* throwing.
|
|
97
|
+
* - Page-scoped search and `show_in_document` report an unsupported-capability
|
|
98
|
+
* error when `ref.getTargetPage` / `ref.showInDocument` are absent.
|
|
94
99
|
*/
|
|
95
100
|
declare const createEditorRefBridge: (options: CreateEditorRefBridgeOptions) => FolioAgentBridge;
|
|
96
101
|
//#endregion
|
|
@@ -35,7 +35,7 @@ const paragraphPlainText = (paragraph) => {
|
|
|
35
35
|
* to `DocxEditor`.
|
|
36
36
|
*
|
|
37
37
|
* KNOWN LIMITATIONS (only apply to a `ref` that predates the read-surface
|
|
38
|
-
* additions below; the current `DocxEditorRef` implements all
|
|
38
|
+
* additions below; the current `DocxEditorRef` implements all six):
|
|
39
39
|
* - `getChanges()` returns `[]` when `ref.getTrackedChanges` is absent, since
|
|
40
40
|
* there is then no ref-level way to enumerate tracked changes from
|
|
41
41
|
* ProseMirror mark attributes.
|
|
@@ -48,6 +48,8 @@ const paragraphPlainText = (paragraph) => {
|
|
|
48
48
|
* entirely rather than implementing it as a no-op, which is what tells
|
|
49
49
|
* `executeFolioToolCall` to report the tool as unsupported instead of
|
|
50
50
|
* throwing.
|
|
51
|
+
* - Page-scoped search and `show_in_document` report an unsupported-capability
|
|
52
|
+
* error when `ref.getTargetPage` / `ref.showInDocument` are absent.
|
|
51
53
|
*/
|
|
52
54
|
const createEditorRefBridge = (options) => {
|
|
53
55
|
const { ref, author, getComments, setComments } = options;
|
|
@@ -192,6 +194,10 @@ const createEditorRefBridge = (options) => {
|
|
|
192
194
|
if (getSelectionText) bridge.getSelectionText = () => getSelectionText();
|
|
193
195
|
const getPageText = ref.getPageText;
|
|
194
196
|
if (getPageText) bridge.getPageText = (page) => getPageText(page) ?? "";
|
|
197
|
+
const getTargetPage = ref.getTargetPage;
|
|
198
|
+
if (getTargetPage) bridge.getTargetPage = (target) => getTargetPage(target);
|
|
199
|
+
const showInDocument = ref.showInDocument;
|
|
200
|
+
if (showInDocument) bridge.showInDocument = (target) => showInDocument(target);
|
|
195
201
|
return bridge;
|
|
196
202
|
};
|
|
197
203
|
//#endregion
|
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 } from "@stll/folio-core/server";
|
|
3
|
+
import { FOLIO_DOCUMENT_OPERATION_CONTRACT_VERSION, createFolioAITextRangeHandle, getFolioDocumentOutline, 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;
|
|
@@ -38,6 +38,8 @@ const executeFolioToolCall = (name, args, bridge) => {
|
|
|
38
38
|
};
|
|
39
39
|
const dispatch = (name, args, bridge) => {
|
|
40
40
|
if (name === FOLIO_AGENT_TOOL_NAMES.readDocument) return readDocument(bridge);
|
|
41
|
+
if (name === FOLIO_AGENT_TOOL_NAMES.getDocumentOutline) return getDocumentOutline(args, bridge);
|
|
42
|
+
if (name === FOLIO_AGENT_TOOL_NAMES.readSection) return readSection(args, bridge);
|
|
41
43
|
if (name === FOLIO_AGENT_TOOL_NAMES.listStories) return bridge.listStories ? ok(bridge.listStories()) : fail("This editor surface does not support story discovery.");
|
|
42
44
|
if (name === FOLIO_AGENT_TOOL_NAMES.readStory) return readStory(args, bridge);
|
|
43
45
|
if (name === FOLIO_AGENT_TOOL_NAMES.findText) return findText(args, bridge);
|
|
@@ -50,6 +52,7 @@ const dispatch = (name, args, bridge) => {
|
|
|
50
52
|
if (name === FOLIO_AGENT_TOOL_NAMES.readPage) return readPage(args, bridge);
|
|
51
53
|
if (name === FOLIO_AGENT_TOOL_NAMES.readSelection) return readSelection(bridge);
|
|
52
54
|
if (name === FOLIO_AGENT_TOOL_NAMES.scrollToBlock) return scrollToBlock(args, bridge);
|
|
55
|
+
if (name === FOLIO_AGENT_TOOL_NAMES.showInDocument) return showInDocument(args, bridge);
|
|
53
56
|
return fail(`Unknown tool "${name}".`);
|
|
54
57
|
};
|
|
55
58
|
const readDocument = (bridge) => {
|
|
@@ -60,63 +63,140 @@ const readDocument = (bridge) => {
|
|
|
60
63
|
text: block.text
|
|
61
64
|
})));
|
|
62
65
|
};
|
|
63
|
-
const
|
|
64
|
-
if (!
|
|
65
|
-
|
|
66
|
-
const
|
|
66
|
+
const parseSectionHandle = (value) => {
|
|
67
|
+
if (!isPlainObject(value)) return null;
|
|
68
|
+
const headingBlockId = value["headingBlockId"];
|
|
69
|
+
const headingTextHash = value["headingTextHash"];
|
|
70
|
+
const headingLevel = value["headingLevel"];
|
|
71
|
+
if (value["type"] !== "headingSection" || value["story"] !== "main" || !isNonEmptyString(headingBlockId) || !isNonEmptyString(headingTextHash) || typeof headingLevel !== "number" || !Number.isInteger(headingLevel) || headingLevel < 1 || headingLevel > 9) return null;
|
|
72
|
+
return {
|
|
73
|
+
type: "headingSection",
|
|
74
|
+
story: "main",
|
|
75
|
+
headingBlockId,
|
|
76
|
+
headingTextHash,
|
|
77
|
+
headingLevel
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
const getDocumentOutline = (args, bridge) => {
|
|
81
|
+
const maxDepth = isPlainObject(args) ? args["maxDepth"] : void 0;
|
|
82
|
+
if (maxDepth !== void 0 && (typeof maxDepth !== "number" || !Number.isInteger(maxDepth) || maxDepth < 1 || maxDepth > 9)) return fail("get_document_outline's `maxDepth` must be an integer from 1 to 9.");
|
|
83
|
+
const outline = getFolioDocumentOutline(bridge.snapshot());
|
|
84
|
+
const depth = maxDepth ?? 3;
|
|
85
|
+
const sections = outline.sections.filter(({ level }) => level <= depth).map((entry) => {
|
|
86
|
+
const page = bridge.getTargetPage?.({
|
|
87
|
+
type: "block",
|
|
88
|
+
story: "main",
|
|
89
|
+
blockId: entry.headingBlockId
|
|
90
|
+
}) ?? void 0;
|
|
91
|
+
return page === void 0 ? entry : Object.assign({}, entry, { page });
|
|
92
|
+
});
|
|
93
|
+
return ok({
|
|
94
|
+
sections,
|
|
95
|
+
totalSections: outline.sections.length,
|
|
96
|
+
truncated: sections.length < outline.sections.length
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
const readSection = (args, bridge) => {
|
|
100
|
+
if (!isPlainObject(args)) return fail("read_section expects a section `handle` from get_document_outline.");
|
|
101
|
+
const handle = parseSectionHandle(args["handle"]);
|
|
102
|
+
if (handle === null) return fail("read_section requires a valid `handle` from get_document_outline.");
|
|
103
|
+
const maxBlocks = args["maxBlocks"] ?? 100;
|
|
104
|
+
if (typeof maxBlocks !== "number" || !Number.isInteger(maxBlocks) || maxBlocks < 1 || maxBlocks > 200) return fail("read_section's `maxBlocks` must be an integer from 1 to 200.");
|
|
105
|
+
const afterBlockId = args["afterBlockId"];
|
|
106
|
+
if (afterBlockId !== void 0 && !isNonEmptyString(afterBlockId)) return fail("read_section's `afterBlockId` must be a non-empty string when provided.");
|
|
107
|
+
const resolved = readFolioDocumentSection(bridge.snapshot(), handle);
|
|
108
|
+
if (resolved.status === "missing") return fail("The section no longer exists; run get_document_outline again.");
|
|
109
|
+
if (resolved.status === "stale") return fail("The section heading changed; run get_document_outline again for a fresh handle.");
|
|
110
|
+
let startIndex = 0;
|
|
111
|
+
if (afterBlockId !== void 0) {
|
|
112
|
+
const cursorIndex = resolved.section.blocks.findIndex(({ id }) => id === afterBlockId);
|
|
113
|
+
if (cursorIndex === -1) return fail("read_section's `afterBlockId` is not part of this section.");
|
|
114
|
+
startIndex = cursorIndex + 1;
|
|
115
|
+
}
|
|
116
|
+
const selected = resolved.section.blocks.slice(startIndex, startIndex + maxBlocks);
|
|
117
|
+
const hasMore = startIndex + selected.length < resolved.section.blocks.length;
|
|
118
|
+
const lastBlockId = selected.at(-1)?.id;
|
|
119
|
+
return ok({
|
|
120
|
+
handle,
|
|
121
|
+
heading: resolved.section.heading,
|
|
122
|
+
blocks: selected.map(({ id, kind, text }) => ({
|
|
123
|
+
blockId: id,
|
|
124
|
+
kind,
|
|
125
|
+
text
|
|
126
|
+
})),
|
|
127
|
+
totalBlocks: resolved.section.blocks.length,
|
|
128
|
+
truncated: hasMore,
|
|
129
|
+
...hasMore && lastBlockId !== void 0 && { nextAfterBlockId: lastBlockId }
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
const parseStoryHandle = (value) => {
|
|
133
|
+
if (!isPlainObject(value)) return null;
|
|
134
|
+
const handle = value;
|
|
67
135
|
const type = handle["type"];
|
|
68
|
-
|
|
69
|
-
if (type === "
|
|
70
|
-
else if (type === "header" || type === "footer") {
|
|
136
|
+
if (type === "main") return { type };
|
|
137
|
+
if (type === "header" || type === "footer") {
|
|
71
138
|
const relationshipId = handle["relationshipId"];
|
|
72
|
-
if (!isNonEmptyString(relationshipId)) return
|
|
73
|
-
|
|
139
|
+
if (!isNonEmptyString(relationshipId)) return null;
|
|
140
|
+
return {
|
|
74
141
|
type,
|
|
75
142
|
relationshipId
|
|
76
143
|
};
|
|
77
|
-
}
|
|
144
|
+
}
|
|
145
|
+
if (type === "footnote" || type === "endnote") {
|
|
78
146
|
const noteId = handle["noteId"];
|
|
79
|
-
if (typeof noteId !== "number" || !Number.isInteger(noteId)) return
|
|
80
|
-
|
|
147
|
+
if (typeof noteId !== "number" || !Number.isInteger(noteId)) return null;
|
|
148
|
+
return {
|
|
81
149
|
type,
|
|
82
150
|
noteId
|
|
83
151
|
};
|
|
84
|
-
}
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
};
|
|
155
|
+
const readStory = (args, bridge) => {
|
|
156
|
+
if (!bridge.readStory) return fail("This editor surface does not support story reads.");
|
|
157
|
+
const parsed = isPlainObject(args) ? parseStoryHandle(args["handle"]) : null;
|
|
158
|
+
if (parsed === null) return fail("read_story requires a valid typed `handle` from list_stories.");
|
|
85
159
|
const story = bridge.readStory(parsed);
|
|
86
160
|
return story ? ok(story) : fail("The requested story was not found; run list_stories again.");
|
|
87
161
|
};
|
|
88
162
|
const CONTEXT_RADIUS = 40;
|
|
89
|
-
const
|
|
163
|
+
const WORD_CHARACTER_AT_END = /[\p{L}\p{M}\p{N}_]$/u;
|
|
164
|
+
const WORD_CHARACTER_AT_START = /^[\p{L}\p{M}\p{N}_]/u;
|
|
165
|
+
const findTextMatches = (blocks, query, matchCase, wholeWord, getTargetPage, pageFilter) => {
|
|
90
166
|
const matches = [];
|
|
91
167
|
let totalMatches = 0;
|
|
92
|
-
const
|
|
168
|
+
const escapedQuery = query.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
169
|
+
const expression = new RegExp(escapedQuery, matchCase ? "gu" : "giu");
|
|
93
170
|
for (const block of blocks) {
|
|
94
|
-
const haystack = matchCase ? block.text : block.text.toLowerCase();
|
|
95
171
|
let occurrence = 0;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
if (at
|
|
172
|
+
for (const match of block.text.matchAll(expression)) {
|
|
173
|
+
const at = match.index;
|
|
174
|
+
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;
|
|
176
|
+
const range = createFolioAITextRangeHandle({
|
|
177
|
+
blockId: block.id,
|
|
178
|
+
text: block.text,
|
|
179
|
+
startOffset: at,
|
|
180
|
+
endOffset: at + matchedText.length
|
|
181
|
+
});
|
|
182
|
+
if (range === null) continue;
|
|
183
|
+
const page = getTargetPage?.(range) ?? void 0;
|
|
184
|
+
if (pageFilter !== void 0 && page !== pageFilter) continue;
|
|
100
185
|
totalMatches += 1;
|
|
101
186
|
if (matches.length < MAX_FIND_MATCHES) {
|
|
102
187
|
const contextStart = Math.max(0, at - CONTEXT_RADIUS);
|
|
103
|
-
const contextEnd = Math.min(block.text.length, at +
|
|
104
|
-
const range = createFolioAITextRangeHandle({
|
|
105
|
-
blockId: block.id,
|
|
106
|
-
text: block.text,
|
|
107
|
-
startOffset: at,
|
|
108
|
-
endOffset: at + query.length
|
|
109
|
-
});
|
|
110
|
-
if (range === null) break;
|
|
188
|
+
const contextEnd = Math.min(block.text.length, at + matchedText.length + CONTEXT_RADIUS);
|
|
111
189
|
matches.push({
|
|
190
|
+
type: "main",
|
|
191
|
+
story: { type: "main" },
|
|
112
192
|
blockId: block.id,
|
|
113
193
|
range,
|
|
114
194
|
occurrenceInBlock: occurrence,
|
|
115
|
-
context: block.text.slice(contextStart, contextEnd)
|
|
195
|
+
context: block.text.slice(contextStart, contextEnd),
|
|
196
|
+
...page !== void 0 && page !== null && { page }
|
|
116
197
|
});
|
|
117
198
|
}
|
|
118
199
|
occurrence += 1;
|
|
119
|
-
fromIndex = at + needle.length;
|
|
120
200
|
}
|
|
121
201
|
}
|
|
122
202
|
return {
|
|
@@ -124,15 +204,76 @@ const findTextMatches = (blocks, query, matchCase) => {
|
|
|
124
204
|
totalMatches
|
|
125
205
|
};
|
|
126
206
|
};
|
|
207
|
+
const findStoryTextMatches = (text, story, query, matchCase, wholeWord) => {
|
|
208
|
+
const matches = [];
|
|
209
|
+
let totalMatches = 0;
|
|
210
|
+
const escapedQuery = query.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
211
|
+
const expression = new RegExp(escapedQuery, matchCase ? "gu" : "giu");
|
|
212
|
+
for (const match of text.matchAll(expression)) {
|
|
213
|
+
const at = match.index;
|
|
214
|
+
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;
|
|
216
|
+
if (matches.length < MAX_FIND_MATCHES) matches.push({
|
|
217
|
+
type: "story",
|
|
218
|
+
story,
|
|
219
|
+
startOffset: at,
|
|
220
|
+
endOffset: at + matchedText.length,
|
|
221
|
+
occurrenceInStory: totalMatches,
|
|
222
|
+
context: text.slice(Math.max(0, at - CONTEXT_RADIUS), Math.min(text.length, at + matchedText.length + CONTEXT_RADIUS))
|
|
223
|
+
});
|
|
224
|
+
totalMatches += 1;
|
|
225
|
+
}
|
|
226
|
+
return {
|
|
227
|
+
matches,
|
|
228
|
+
totalMatches
|
|
229
|
+
};
|
|
230
|
+
};
|
|
127
231
|
const findText = (args, bridge) => {
|
|
128
232
|
if (!isPlainObject(args)) return fail("find_text expects an object with a non-empty `query` string.");
|
|
129
233
|
const query = args["query"];
|
|
130
234
|
const matchCase = args["matchCase"];
|
|
235
|
+
const wholeWord = args["wholeWord"];
|
|
131
236
|
if (!isNonEmptyString(query)) return fail("find_text requires a non-empty string `query`.");
|
|
132
237
|
if (query.length > MAX_QUERY_LENGTH) return fail(`find_text's \`query\` is ${query.length.toLocaleString()} characters, over the ${MAX_QUERY_LENGTH.toLocaleString()}-character limit; shorten it.`);
|
|
133
238
|
if (matchCase !== void 0 && typeof matchCase !== "boolean") return fail("find_text's `matchCase` must be a boolean when provided.");
|
|
134
|
-
|
|
135
|
-
const
|
|
239
|
+
if (wholeWord !== void 0 && typeof wholeWord !== "boolean") return fail("find_text's `wholeWord` must be a boolean when provided.");
|
|
240
|
+
const scope = args["scope"];
|
|
241
|
+
let blocks = bridge.snapshot().blocks;
|
|
242
|
+
let getTargetPage;
|
|
243
|
+
let pageFilter;
|
|
244
|
+
if (scope !== void 0) {
|
|
245
|
+
if (!isPlainObject(scope) || !isNonEmptyString(scope["type"])) return fail("find_text's `scope` must be a document, section, page, or story scope.");
|
|
246
|
+
if (scope["type"] === "section") {
|
|
247
|
+
const handle = parseSectionHandle(scope["handle"]);
|
|
248
|
+
if (handle === null) return fail("find_text's section scope requires a handle from get_document_outline.");
|
|
249
|
+
const section = readFolioDocumentSection(bridge.snapshot(), handle);
|
|
250
|
+
if (section.status !== "found") return fail("The scoped section is stale or missing; run get_document_outline again.");
|
|
251
|
+
blocks = section.section.blocks;
|
|
252
|
+
} else if (scope["type"] === "page") {
|
|
253
|
+
const page = scope["page"];
|
|
254
|
+
if (typeof page !== "number" || !Number.isInteger(page) || page < 1) return fail("find_text's page scope requires an integer `page` >= 1.");
|
|
255
|
+
if (!bridge.getTargetPage) return fail("This editor surface cannot search by page without live pagination.");
|
|
256
|
+
const pageCount = bridge.getPageCount?.();
|
|
257
|
+
if (pageCount !== void 0 && page > pageCount) return fail(`find_text's page (${page}) exceeds the document's page count (${pageCount}).`);
|
|
258
|
+
getTargetPage = bridge.getTargetPage;
|
|
259
|
+
pageFilter = page;
|
|
260
|
+
} else if (scope["type"] === "story") {
|
|
261
|
+
const handle = parseStoryHandle(scope["handle"]);
|
|
262
|
+
if (handle === null) return fail("find_text's story scope requires a handle from list_stories.");
|
|
263
|
+
if (handle.type !== "main") {
|
|
264
|
+
if (!bridge.readStory) return fail("This editor surface does not support story search.");
|
|
265
|
+
const story = bridge.readStory(handle);
|
|
266
|
+
if (story === null) return fail("The scoped story was not found; run list_stories again.");
|
|
267
|
+
const found = findStoryTextMatches(story.text, handle, query, matchCase === true, wholeWord === true);
|
|
268
|
+
return ok({
|
|
269
|
+
matches: found.matches,
|
|
270
|
+
truncated: found.totalMatches > found.matches.length,
|
|
271
|
+
totalMatches: found.totalMatches
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
} else if (scope["type"] !== "document") return fail("find_text's `scope.type` must be document, section, page, or story.");
|
|
275
|
+
}
|
|
276
|
+
const { matches, totalMatches } = findTextMatches(blocks, query, matchCase === true, wholeWord === true, getTargetPage, pageFilter);
|
|
136
277
|
const result = {
|
|
137
278
|
matches,
|
|
138
279
|
truncated: totalMatches > matches.length,
|
|
@@ -231,6 +372,7 @@ const readPage = (args, bridge) => {
|
|
|
231
372
|
if (pageCount !== void 0 && page > pageCount) return fail(`read_page's \`page\` (${page}) exceeds the document's page count (${pageCount}).`);
|
|
232
373
|
return ok({
|
|
233
374
|
page,
|
|
375
|
+
totalPages: pageCount,
|
|
234
376
|
text: bridge.getPageText(page)
|
|
235
377
|
});
|
|
236
378
|
};
|
|
@@ -244,5 +386,39 @@ const scrollToBlock = (args, bridge) => {
|
|
|
244
386
|
if (!isNonEmptyString(blockId)) return fail("scroll_to_block requires a non-empty string `blockId`.");
|
|
245
387
|
return ok({ scrolled: bridge.scrollToBlock(blockId) });
|
|
246
388
|
};
|
|
389
|
+
const parseTextRange = (value) => {
|
|
390
|
+
if (!isPlainObject(value)) return null;
|
|
391
|
+
const blockId = value["blockId"];
|
|
392
|
+
const startOffset = value["startOffset"];
|
|
393
|
+
const endOffset = value["endOffset"];
|
|
394
|
+
const selectedTextHash = value["selectedTextHash"];
|
|
395
|
+
if (value["type"] !== "textRange" || value["story"] !== "main" || !isNonEmptyString(blockId) || typeof startOffset !== "number" || !Number.isInteger(startOffset) || startOffset < 0 || typeof endOffset !== "number" || !Number.isInteger(endOffset) || endOffset <= startOffset || !isNonEmptyString(selectedTextHash)) return null;
|
|
396
|
+
return {
|
|
397
|
+
type: "textRange",
|
|
398
|
+
story: "main",
|
|
399
|
+
blockId,
|
|
400
|
+
startOffset,
|
|
401
|
+
endOffset,
|
|
402
|
+
selectedTextHash
|
|
403
|
+
};
|
|
404
|
+
};
|
|
405
|
+
const showInDocument = (args, bridge) => {
|
|
406
|
+
if (!bridge.showInDocument) return fail("This editor surface does not support show_in_document (no live editor view).");
|
|
407
|
+
if (!isPlainObject(args)) return fail("show_in_document expects exactly one of `blockId` or `range`.");
|
|
408
|
+
const blockId = args["blockId"];
|
|
409
|
+
const rawRange = args["range"];
|
|
410
|
+
if (blockId === void 0 === (rawRange === void 0)) return fail("show_in_document requires exactly one of `blockId` or `range`.");
|
|
411
|
+
if (blockId !== void 0) {
|
|
412
|
+
if (!isNonEmptyString(blockId)) return fail("show_in_document's `blockId` must be a non-empty string.");
|
|
413
|
+
return ok({ shown: bridge.showInDocument({
|
|
414
|
+
type: "block",
|
|
415
|
+
story: "main",
|
|
416
|
+
blockId
|
|
417
|
+
}) });
|
|
418
|
+
}
|
|
419
|
+
const range = parseTextRange(rawRange);
|
|
420
|
+
if (range === null) return fail("show_in_document's `range` must be copied from find_text.");
|
|
421
|
+
return ok({ shown: bridge.showInDocument(range) });
|
|
422
|
+
};
|
|
247
423
|
//#endregion
|
|
248
424
|
export { executeFolioToolCall };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FOLIO_AGENT_TOOL_NAMES, FolioAgentApplyOperationsSummary, FolioAgentBlock, FolioAgentChange, FolioAgentComment, FolioAgentCommentFilter, FolioAgentCommentReply, FolioAgentTextMatch, FolioAgentToolDefinition, FolioAgentToolName, FolioToolCallResult } from "./types.js";
|
|
1
|
+
import { FOLIO_AGENT_TOOL_NAMES, FolioAgentApplyOperationsSummary, FolioAgentBlock, FolioAgentChange, FolioAgentComment, FolioAgentCommentFilter, FolioAgentCommentReply, FolioAgentDocumentOutline, FolioAgentOutlineEntry, FolioAgentScopedFindTextResult, FolioAgentSectionRead, FolioAgentStoryFindTextResult, FolioAgentStoryTextMatch, FolioAgentTextMatch, FolioAgentToolDefinition, FolioAgentToolName, FolioToolCallResult } from "./types.js";
|
|
2
2
|
import { FolioAgentBridge } from "./bridge.js";
|
|
3
3
|
import { CreateEditorRefBridgeOptions, FolioAgentEditorApplyDocumentOperationsOptions, FolioAgentEditorRefLike, createEditorRefBridge } from "./bridges/editor-ref.js";
|
|
4
4
|
import { CreateReviewerBridgeOptions, createReviewerBridge } from "./bridges/reviewer.js";
|
|
@@ -8,4 +8,4 @@ import { FOLIO_DOCUMENT_OPERATION_BATCH_JSON_SCHEMA, FOLIO_DOCUMENT_OPERATION_JS
|
|
|
8
8
|
import { ParseAddCommentResult, ParseSuggestChangesResult, parseAddCommentInput, parseSuggestChangesInput } from "./parse.js";
|
|
9
9
|
import { AnthropicToolDefinition, OpenAIToolDefinition, toAnthropicTools, toOpenAITools } from "./providers.js";
|
|
10
10
|
import { FOLIO_AGENT_TOOLS, getFolioToolDefinitions } from "./tools.js";
|
|
11
|
-
export { type AnthropicToolDefinition, type CreateEditorRefBridgeOptions, type CreateReviewerBridgeOptions, FOLIO_AGENT_TOOLS, FOLIO_AGENT_TOOL_NAMES, FOLIO_DOCUMENT_OPERATION_BATCH_JSON_SCHEMA, FOLIO_DOCUMENT_OPERATION_JSON_SCHEMA, type FolioAgentApplyOperationsSummary, type FolioAgentBlock, type FolioAgentBlockDiff, type FolioAgentBridge, type FolioAgentChange, type FolioAgentComment, type FolioAgentCommentFilter, type FolioAgentCommentReply, type FolioAgentEditorApplyDocumentOperationsOptions, type FolioAgentEditorRefLike, type FolioAgentTextMatch, type FolioAgentToolDefinition, type FolioAgentToolName, type FolioAgentVersionDiff, type FolioAgentVersionDiffSegment, type FolioToolCallResult, type OpenAIToolDefinition, type ParseAddCommentResult, type ParseSuggestChangesResult, compareDocxVersions, createEditorRefBridge, createReviewerBridge, executeFolioToolCall, folioDocumentOperationBatchSchema, formatVersionDiffForLLM, getFolioToolDefinitions, parseAddCommentInput, parseSuggestChangesInput, toAnthropicTools, toOpenAITools };
|
|
11
|
+
export { type AnthropicToolDefinition, type CreateEditorRefBridgeOptions, type CreateReviewerBridgeOptions, FOLIO_AGENT_TOOLS, FOLIO_AGENT_TOOL_NAMES, FOLIO_DOCUMENT_OPERATION_BATCH_JSON_SCHEMA, FOLIO_DOCUMENT_OPERATION_JSON_SCHEMA, type FolioAgentApplyOperationsSummary, type FolioAgentBlock, type FolioAgentBlockDiff, type FolioAgentBridge, type FolioAgentChange, type FolioAgentComment, type FolioAgentCommentFilter, type FolioAgentCommentReply, type FolioAgentDocumentOutline, type FolioAgentEditorApplyDocumentOperationsOptions, type FolioAgentEditorRefLike, type FolioAgentOutlineEntry, type FolioAgentScopedFindTextResult, type FolioAgentSectionRead, type FolioAgentStoryFindTextResult, type FolioAgentStoryTextMatch, type FolioAgentTextMatch, type FolioAgentToolDefinition, type FolioAgentToolName, type FolioAgentVersionDiff, type FolioAgentVersionDiffSegment, type FolioToolCallResult, type OpenAIToolDefinition, type ParseAddCommentResult, type ParseSuggestChangesResult, compareDocxVersions, createEditorRefBridge, createReviewerBridge, executeFolioToolCall, folioDocumentOperationBatchSchema, formatVersionDiffForLLM, getFolioToolDefinitions, parseAddCommentInput, parseSuggestChangesInput, toAnthropicTools, toOpenAITools };
|
package/dist/tools.js
CHANGED
|
@@ -2,6 +2,84 @@ import { FOLIO_AGENT_TOOL_NAMES } from "./types.js";
|
|
|
2
2
|
import { FOLIO_TEXT_RANGE_JSON_SCHEMA } from "./operation-schema.js";
|
|
3
3
|
import { FOLIO_DOCUMENT_OPERATION_TYPES } from "@stll/folio-core/server";
|
|
4
4
|
//#region src/tools.ts
|
|
5
|
+
const storyHandleSchema = {
|
|
6
|
+
type: "object",
|
|
7
|
+
properties: {
|
|
8
|
+
type: {
|
|
9
|
+
type: "string",
|
|
10
|
+
enum: [
|
|
11
|
+
"main",
|
|
12
|
+
"header",
|
|
13
|
+
"footer",
|
|
14
|
+
"footnote",
|
|
15
|
+
"endnote"
|
|
16
|
+
]
|
|
17
|
+
},
|
|
18
|
+
relationshipId: { type: "string" },
|
|
19
|
+
noteId: { type: "integer" }
|
|
20
|
+
},
|
|
21
|
+
required: ["type"],
|
|
22
|
+
additionalProperties: false
|
|
23
|
+
};
|
|
24
|
+
const sectionHandleSchema = {
|
|
25
|
+
type: "object",
|
|
26
|
+
properties: {
|
|
27
|
+
type: {
|
|
28
|
+
type: "string",
|
|
29
|
+
enum: ["headingSection"]
|
|
30
|
+
},
|
|
31
|
+
story: {
|
|
32
|
+
type: "string",
|
|
33
|
+
enum: ["main"]
|
|
34
|
+
},
|
|
35
|
+
headingBlockId: { type: "string" },
|
|
36
|
+
headingTextHash: { type: "string" },
|
|
37
|
+
headingLevel: {
|
|
38
|
+
type: "integer",
|
|
39
|
+
minimum: 1,
|
|
40
|
+
maximum: 9
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
required: [
|
|
44
|
+
"type",
|
|
45
|
+
"story",
|
|
46
|
+
"headingBlockId",
|
|
47
|
+
"headingTextHash",
|
|
48
|
+
"headingLevel"
|
|
49
|
+
],
|
|
50
|
+
additionalProperties: false
|
|
51
|
+
};
|
|
52
|
+
const scopedHandleSchema = {
|
|
53
|
+
type: "object",
|
|
54
|
+
properties: {
|
|
55
|
+
type: {
|
|
56
|
+
type: "string",
|
|
57
|
+
enum: [
|
|
58
|
+
"main",
|
|
59
|
+
"header",
|
|
60
|
+
"footer",
|
|
61
|
+
"footnote",
|
|
62
|
+
"endnote",
|
|
63
|
+
"headingSection"
|
|
64
|
+
]
|
|
65
|
+
},
|
|
66
|
+
story: {
|
|
67
|
+
type: "string",
|
|
68
|
+
enum: ["main"]
|
|
69
|
+
},
|
|
70
|
+
relationshipId: { type: "string" },
|
|
71
|
+
noteId: { type: "integer" },
|
|
72
|
+
headingBlockId: { type: "string" },
|
|
73
|
+
headingTextHash: { type: "string" },
|
|
74
|
+
headingLevel: {
|
|
75
|
+
type: "integer",
|
|
76
|
+
minimum: 1,
|
|
77
|
+
maximum: 9
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
required: ["type"],
|
|
81
|
+
additionalProperties: false
|
|
82
|
+
};
|
|
5
83
|
/**
|
|
6
84
|
* `suggest_changes` deliberately narrows the full document-operation contract
|
|
7
85
|
* (see `FOLIO_DOCUMENT_OPERATION_JSON_SCHEMA` in `operation-schema.ts`):
|
|
@@ -89,6 +167,43 @@ const FOLIO_AGENT_TOOLS = [
|
|
|
89
167
|
additionalProperties: false
|
|
90
168
|
}
|
|
91
169
|
},
|
|
170
|
+
{
|
|
171
|
+
name: FOLIO_AGENT_TOOL_NAMES.getDocumentOutline,
|
|
172
|
+
description: "Read a lightweight heading outline before opening document content. Returns stable section handles, heading hierarchy, and real rendered page numbers when a live paginated editor is available.",
|
|
173
|
+
inputSchema: {
|
|
174
|
+
type: "object",
|
|
175
|
+
properties: { maxDepth: {
|
|
176
|
+
type: "integer",
|
|
177
|
+
minimum: 1,
|
|
178
|
+
maximum: 9,
|
|
179
|
+
description: "Deepest heading level to return. Defaults to 3."
|
|
180
|
+
} },
|
|
181
|
+
required: [],
|
|
182
|
+
additionalProperties: false
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
name: FOLIO_AGENT_TOOL_NAMES.readSection,
|
|
187
|
+
description: "Read one logical heading section using a handle from get_document_outline. Content is block-bounded and paginated with an afterBlockId cursor, avoiding a full-document read.",
|
|
188
|
+
inputSchema: {
|
|
189
|
+
type: "object",
|
|
190
|
+
properties: {
|
|
191
|
+
handle: sectionHandleSchema,
|
|
192
|
+
maxBlocks: {
|
|
193
|
+
type: "integer",
|
|
194
|
+
minimum: 1,
|
|
195
|
+
maximum: 200,
|
|
196
|
+
description: "Maximum blocks to return. Defaults to 100."
|
|
197
|
+
},
|
|
198
|
+
afterBlockId: {
|
|
199
|
+
type: "string",
|
|
200
|
+
description: "Continue after this block id from the preceding read_section response."
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
required: ["handle"],
|
|
204
|
+
additionalProperties: false
|
|
205
|
+
}
|
|
206
|
+
},
|
|
92
207
|
{
|
|
93
208
|
name: FOLIO_AGENT_TOOL_NAMES.listStories,
|
|
94
209
|
description: "List readable document stories and their typed handles.",
|
|
@@ -104,32 +219,14 @@ const FOLIO_AGENT_TOOLS = [
|
|
|
104
219
|
description: "Read one document story using a handle returned by `list_stories`.",
|
|
105
220
|
inputSchema: {
|
|
106
221
|
type: "object",
|
|
107
|
-
properties: { handle:
|
|
108
|
-
type: "object",
|
|
109
|
-
properties: {
|
|
110
|
-
type: {
|
|
111
|
-
type: "string",
|
|
112
|
-
enum: [
|
|
113
|
-
"main",
|
|
114
|
-
"header",
|
|
115
|
-
"footer",
|
|
116
|
-
"footnote",
|
|
117
|
-
"endnote"
|
|
118
|
-
]
|
|
119
|
-
},
|
|
120
|
-
relationshipId: { type: "string" },
|
|
121
|
-
noteId: { type: "integer" }
|
|
122
|
-
},
|
|
123
|
-
required: ["type"],
|
|
124
|
-
additionalProperties: false
|
|
125
|
-
} },
|
|
222
|
+
properties: { handle: storyHandleSchema },
|
|
126
223
|
required: ["handle"],
|
|
127
224
|
additionalProperties: false
|
|
128
225
|
}
|
|
129
226
|
},
|
|
130
227
|
{
|
|
131
228
|
name: FOLIO_AGENT_TOOL_NAMES.findText,
|
|
132
|
-
description: "Search
|
|
229
|
+
description: "Search a document, section, rendered page, or story and return `{ matches, truncated, totalMatches }`. Main-story matches include a stable block and exact range; other stories return story-relative offsets. Every match includes surrounding context. `matches` is capped at 200 entries; `truncated` is true and `totalMatches` reports the real count when there were more — narrow the query or scope instead of assuming you saw every hit.",
|
|
133
230
|
inputSchema: {
|
|
134
231
|
type: "object",
|
|
135
232
|
properties: {
|
|
@@ -140,6 +237,32 @@ const FOLIO_AGENT_TOOLS = [
|
|
|
140
237
|
matchCase: {
|
|
141
238
|
type: "boolean",
|
|
142
239
|
description: "Case-sensitive match. Defaults to false (case-insensitive)."
|
|
240
|
+
},
|
|
241
|
+
wholeWord: {
|
|
242
|
+
type: "boolean",
|
|
243
|
+
description: "Match only Unicode word boundaries. Defaults to false."
|
|
244
|
+
},
|
|
245
|
+
scope: {
|
|
246
|
+
type: "object",
|
|
247
|
+
properties: {
|
|
248
|
+
type: {
|
|
249
|
+
type: "string",
|
|
250
|
+
enum: [
|
|
251
|
+
"document",
|
|
252
|
+
"section",
|
|
253
|
+
"page",
|
|
254
|
+
"story"
|
|
255
|
+
]
|
|
256
|
+
},
|
|
257
|
+
handle: scopedHandleSchema,
|
|
258
|
+
page: {
|
|
259
|
+
type: "integer",
|
|
260
|
+
minimum: 1
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
required: ["type"],
|
|
264
|
+
additionalProperties: false,
|
|
265
|
+
description: "Limit search to the main document, a section, a real rendered page, or a story."
|
|
143
266
|
}
|
|
144
267
|
},
|
|
145
268
|
required: ["query"],
|
|
@@ -272,6 +395,22 @@ const FOLIO_AGENT_TOOLS = [
|
|
|
272
395
|
additionalProperties: false
|
|
273
396
|
}
|
|
274
397
|
},
|
|
398
|
+
{
|
|
399
|
+
name: FOLIO_AGENT_TOOL_NAMES.showInDocument,
|
|
400
|
+
description: "Reveal and select a block or exact text range in the live editor. Pass either blockId or the range returned by find_text; stale or missing targets return false.",
|
|
401
|
+
inputSchema: {
|
|
402
|
+
type: "object",
|
|
403
|
+
properties: {
|
|
404
|
+
blockId: {
|
|
405
|
+
type: "string",
|
|
406
|
+
description: "A main-story block id."
|
|
407
|
+
},
|
|
408
|
+
range: FOLIO_TEXT_RANGE_JSON_SCHEMA
|
|
409
|
+
},
|
|
410
|
+
required: [],
|
|
411
|
+
additionalProperties: false
|
|
412
|
+
}
|
|
413
|
+
},
|
|
275
414
|
{
|
|
276
415
|
name: FOLIO_AGENT_TOOL_NAMES.scrollToBlock,
|
|
277
416
|
description: "Scroll the live editor to the given block and select it, so the user can see what you are discussing. Only available on a live editor surface.",
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FOLIO_DOCUMENT_OPERATION_CONTRACT_VERSION, FolioAITextRangeHandle, FolioDocumentOperationIssue, FolioDocumentOperationReceipt } from "@stll/folio-core/server";
|
|
1
|
+
import { FOLIO_DOCUMENT_OPERATION_CONTRACT_VERSION, FolioAITextRangeHandle, FolioDocumentOperationIssue, FolioDocumentOperationReceipt, FolioDocumentOutlineEntry, FolioDocumentSectionHandle, FolioDocumentStoryHandle } from "@stll/folio-core/server";
|
|
2
2
|
|
|
3
3
|
//#region src/types.d.ts
|
|
4
4
|
/**
|
|
@@ -10,6 +10,8 @@ import { FOLIO_DOCUMENT_OPERATION_CONTRACT_VERSION, FolioAITextRangeHandle, Foli
|
|
|
10
10
|
/** Every tool name this package exposes, as a stable string union (no enums). */
|
|
11
11
|
declare const FOLIO_AGENT_TOOL_NAMES: {
|
|
12
12
|
readonly readDocument: "read_document";
|
|
13
|
+
readonly getDocumentOutline: "get_document_outline";
|
|
14
|
+
readonly readSection: "read_section";
|
|
13
15
|
readonly listStories: "list_stories";
|
|
14
16
|
readonly readStory: "read_story";
|
|
15
17
|
readonly findText: "find_text";
|
|
@@ -22,6 +24,7 @@ declare const FOLIO_AGENT_TOOL_NAMES: {
|
|
|
22
24
|
readonly readPage: "read_page";
|
|
23
25
|
readonly readSelection: "read_selection";
|
|
24
26
|
readonly scrollToBlock: "scroll_to_block";
|
|
27
|
+
readonly showInDocument: "show_in_document";
|
|
25
28
|
};
|
|
26
29
|
type FolioAgentToolName = (typeof FOLIO_AGENT_TOOL_NAMES)[keyof typeof FOLIO_AGENT_TOOL_NAMES];
|
|
27
30
|
/**
|
|
@@ -56,13 +59,44 @@ type FolioAgentBlock = {
|
|
|
56
59
|
kind: string;
|
|
57
60
|
text: string;
|
|
58
61
|
};
|
|
59
|
-
/** One `find_text` match
|
|
62
|
+
/** One main-story `find_text` match. Existing consumers can keep using its block and range directly. */
|
|
60
63
|
type FolioAgentTextMatch = {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
+
/** Present on results from the current executor; optional for source compatibility with older hosts. */type?: "main";
|
|
65
|
+
story?: {
|
|
66
|
+
type: "main";
|
|
67
|
+
};
|
|
68
|
+
blockId: string; /** Stable handle that can be passed directly to `show_in_document` or a range operation. */
|
|
69
|
+
range: FolioAITextRangeHandle; /** 0-based index of this occurrence within its block. */
|
|
70
|
+
occurrenceInBlock: number; /** Real rendered page when a live paginated surface supplies it. */
|
|
71
|
+
page?: number;
|
|
64
72
|
context: string;
|
|
65
73
|
};
|
|
74
|
+
type FolioAgentStoryTextMatch = {
|
|
75
|
+
type: "story";
|
|
76
|
+
story: Exclude<FolioDocumentStoryHandle, {
|
|
77
|
+
type: "main";
|
|
78
|
+
}>;
|
|
79
|
+
startOffset: number;
|
|
80
|
+
endOffset: number; /** 0-based index of this occurrence within the story. */
|
|
81
|
+
occurrenceInStory: number;
|
|
82
|
+
context: string;
|
|
83
|
+
};
|
|
84
|
+
type FolioAgentOutlineEntry = FolioDocumentOutlineEntry & {
|
|
85
|
+
page?: number;
|
|
86
|
+
};
|
|
87
|
+
type FolioAgentDocumentOutline = {
|
|
88
|
+
sections: FolioAgentOutlineEntry[];
|
|
89
|
+
totalSections: number;
|
|
90
|
+
truncated: boolean;
|
|
91
|
+
};
|
|
92
|
+
type FolioAgentSectionRead = {
|
|
93
|
+
handle: FolioDocumentSectionHandle;
|
|
94
|
+
heading: FolioDocumentOutlineEntry;
|
|
95
|
+
blocks: FolioAgentBlock[];
|
|
96
|
+
totalBlocks: number;
|
|
97
|
+
truncated: boolean;
|
|
98
|
+
nextAfterBlockId?: string;
|
|
99
|
+
};
|
|
66
100
|
/**
|
|
67
101
|
* Result of {@link FOLIO_AGENT_TOOL_NAMES.findText}. `matches` is capped at a
|
|
68
102
|
* fixed limit; when the query has more hits than that, `truncated` is `true`
|
|
@@ -74,6 +108,12 @@ type FolioAgentFindTextResult = {
|
|
|
74
108
|
truncated: boolean;
|
|
75
109
|
totalMatches: number;
|
|
76
110
|
};
|
|
111
|
+
type FolioAgentStoryFindTextResult = {
|
|
112
|
+
matches: FolioAgentStoryTextMatch[];
|
|
113
|
+
truncated: boolean;
|
|
114
|
+
totalMatches: number;
|
|
115
|
+
};
|
|
116
|
+
type FolioAgentScopedFindTextResult = FolioAgentFindTextResult | FolioAgentStoryFindTextResult;
|
|
77
117
|
/** One reply within a comment thread. */
|
|
78
118
|
type FolioAgentCommentReply = {
|
|
79
119
|
id: string;
|
|
@@ -120,4 +160,4 @@ type FolioAgentApplyOperationsSummary = {
|
|
|
120
160
|
receipts: FolioDocumentOperationReceipt[];
|
|
121
161
|
};
|
|
122
162
|
//#endregion
|
|
123
|
-
export { FOLIO_AGENT_TOOL_NAMES, FolioAgentApplyOperationsSummary, FolioAgentBlock, FolioAgentChange, FolioAgentComment, FolioAgentCommentFilter, FolioAgentCommentReply, FolioAgentFindTextResult, FolioAgentTextMatch, FolioAgentToolDefinition, FolioAgentToolName, FolioToolCallResult };
|
|
163
|
+
export { FOLIO_AGENT_TOOL_NAMES, FolioAgentApplyOperationsSummary, FolioAgentBlock, FolioAgentChange, FolioAgentComment, FolioAgentCommentFilter, FolioAgentCommentReply, FolioAgentDocumentOutline, FolioAgentFindTextResult, FolioAgentOutlineEntry, FolioAgentScopedFindTextResult, FolioAgentSectionRead, FolioAgentStoryFindTextResult, FolioAgentStoryTextMatch, FolioAgentTextMatch, FolioAgentToolDefinition, FolioAgentToolName, FolioToolCallResult };
|
package/dist/types.js
CHANGED
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
/** Every tool name this package exposes, as a stable string union (no enums). */
|
|
9
9
|
const FOLIO_AGENT_TOOL_NAMES = {
|
|
10
10
|
readDocument: "read_document",
|
|
11
|
+
getDocumentOutline: "get_document_outline",
|
|
12
|
+
readSection: "read_section",
|
|
11
13
|
listStories: "list_stories",
|
|
12
14
|
readStory: "read_story",
|
|
13
15
|
findText: "find_text",
|
|
@@ -19,7 +21,8 @@ const FOLIO_AGENT_TOOL_NAMES = {
|
|
|
19
21
|
resolveComment: "resolve_comment",
|
|
20
22
|
readPage: "read_page",
|
|
21
23
|
readSelection: "read_selection",
|
|
22
|
-
scrollToBlock: "scroll_to_block"
|
|
24
|
+
scrollToBlock: "scroll_to_block",
|
|
25
|
+
showInDocument: "show_in_document"
|
|
23
26
|
};
|
|
24
27
|
//#endregion
|
|
25
28
|
export { FOLIO_AGENT_TOOL_NAMES };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stll/folio-agents",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Framework-neutral LLM tool layer over folio's ai-edits engine: function-calling tools so an agent can read and mutate .docx documents through @stll/folio-core.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agent",
|