@stll/folio-agents 0.8.9 → 0.9.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 +4 -4
- package/dist/bridge.d.ts +15 -2
- package/dist/bridge.js +9 -0
- package/dist/bridges/editor-ref.d.ts +1 -0
- package/dist/bridges/editor-ref.js +39 -36
- package/dist/bridges/reviewer.js +16 -15
- package/dist/codecs.d.ts +92 -0
- package/dist/codecs.js +150 -0
- package/dist/execute.d.ts +5 -3
- package/dist/execute.js +50 -86
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/operation-schema.d.ts +21 -2083
- package/dist/parse.d.ts +15 -2
- package/dist/parse.js +24 -20
- package/dist/tool-contract.d.ts +111 -0
- package/dist/tool-contract.js +4 -0
- package/dist/tools.d.ts +69 -2
- package/dist/tools.js +44 -124
- package/dist/types.d.ts +18 -3
- package/dist/types.js +0 -6
- package/package.json +2 -2
- package/skills/folio-agents/SKILL.md +1 -1
package/README.md
CHANGED
|
@@ -70,7 +70,7 @@ cannot silently apply one.
|
|
|
70
70
|
import { FolioDocxReviewer } from "@stll/folio-core/server";
|
|
71
71
|
import {
|
|
72
72
|
createReviewerBridge,
|
|
73
|
-
|
|
73
|
+
executeFolioToolCallUntyped,
|
|
74
74
|
getFolioToolDefinitions,
|
|
75
75
|
toAnthropicTools,
|
|
76
76
|
} from "@stll/folio-agents";
|
|
@@ -80,7 +80,7 @@ const bridge = createReviewerBridge(reviewer);
|
|
|
80
80
|
const tools = toAnthropicTools(getFolioToolDefinitions());
|
|
81
81
|
|
|
82
82
|
// Inside your tool-use loop, for each tool_use block the model emits:
|
|
83
|
-
const result =
|
|
83
|
+
const result = executeFolioToolCallUntyped(toolName, toolInput, bridge);
|
|
84
84
|
// result: { ok: true, result } | { ok: false, error } — feed either back to the model.
|
|
85
85
|
|
|
86
86
|
const reviewedBuffer = await reviewer.toBuffer();
|
|
@@ -230,7 +230,7 @@ SDK's own `jsonSchema()` / `tool()` helpers:
|
|
|
230
230
|
|
|
231
231
|
```ts
|
|
232
232
|
import { jsonSchema, tool } from "ai";
|
|
233
|
-
import {
|
|
233
|
+
import { executeFolioToolCallUntyped, getFolioToolDefinitions } from "@stll/folio-agents";
|
|
234
234
|
|
|
235
235
|
const tools = Object.fromEntries(
|
|
236
236
|
getFolioToolDefinitions().map((def) => [
|
|
@@ -238,7 +238,7 @@ const tools = Object.fromEntries(
|
|
|
238
238
|
tool({
|
|
239
239
|
description: def.description,
|
|
240
240
|
inputSchema: jsonSchema(def.inputSchema),
|
|
241
|
-
execute: async (input) =>
|
|
241
|
+
execute: async (input) => executeFolioToolCallUntyped(def.name, input, bridge),
|
|
242
242
|
}),
|
|
243
243
|
]),
|
|
244
244
|
);
|
package/dist/bridge.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { FolioAgentCommentId } from "./codecs.js";
|
|
1
2
|
import { FolioAgentChange, FolioAgentComment } from "./types.js";
|
|
2
|
-
import { FolioAIEditSnapshot, FolioDocumentNavigationTarget, FolioDocumentOperationBatch, FolioDocumentOperationResult, FolioDocumentOperationUndoHandle, FolioDocumentOperationUndoResult, FolioDocumentStory, FolioDocumentStoryHandle } from "@stll/folio-core/server";
|
|
3
|
+
import { FolioAIEditSnapshot, FolioDocumentNavigationTarget, FolioDocumentOperationBatch, FolioDocumentOperationMode, FolioDocumentOperationResult, FolioDocumentOperationUndoHandle, FolioDocumentOperationUndoResult, FolioDocumentStory, FolioDocumentStoryHandle } from "@stll/folio-core/server";
|
|
3
4
|
//#region src/bridge.d.ts
|
|
4
5
|
/**
|
|
5
6
|
* The structural contract every folio surface (headless reviewer, live
|
|
@@ -17,6 +18,12 @@ import { FolioAIEditSnapshot, FolioDocumentNavigationTarget, FolioDocumentOperat
|
|
|
17
18
|
type FolioAgentBridge = {
|
|
18
19
|
/** Snapshot the current document into AI-facing blocks + anchors. */
|
|
19
20
|
snapshot(): FolioAIEditSnapshot;
|
|
21
|
+
/**
|
|
22
|
+
* Default document-operation mode this surface applies when a batch omits
|
|
23
|
+
* `mode`. The executor uses it when it prepares already-parsed batches so
|
|
24
|
+
* the bridge can usually forward them without rebuilding the batch object.
|
|
25
|
+
*/
|
|
26
|
+
documentOperationMode?: FolioDocumentOperationMode;
|
|
20
27
|
/**
|
|
21
28
|
* Apply a versioned operation batch against the current document. The
|
|
22
29
|
* bridge decides mode (tracked-changes by default) and author internally.
|
|
@@ -49,5 +56,11 @@ type FolioAgentBridge = {
|
|
|
49
56
|
/** Select and reveal a stable block or text range in the live editor. */
|
|
50
57
|
showInDocument?(target: FolioDocumentNavigationTarget): boolean;
|
|
51
58
|
};
|
|
59
|
+
type DecodedCommentHandlers = {
|
|
60
|
+
replyToComment(commentId: FolioAgentCommentId, text: string): boolean;
|
|
61
|
+
resolveComment(commentId: FolioAgentCommentId, resolved: boolean): boolean;
|
|
62
|
+
};
|
|
63
|
+
declare const registerDecodedCommentHandlers: (bridge: FolioAgentBridge, handlers: DecodedCommentHandlers) => FolioAgentBridge;
|
|
64
|
+
declare const getDecodedCommentHandlers: (bridge: FolioAgentBridge) => DecodedCommentHandlers | undefined;
|
|
52
65
|
//#endregion
|
|
53
|
-
export { FolioAgentBridge };
|
|
66
|
+
export { FolioAgentBridge, getDecodedCommentHandlers, registerDecodedCommentHandlers };
|
package/dist/bridge.js
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/bridge.ts
|
|
2
|
+
const decodedCommentHandlers = /* @__PURE__ */ new WeakMap();
|
|
3
|
+
const registerDecodedCommentHandlers = (bridge, handlers) => {
|
|
4
|
+
decodedCommentHandlers.set(bridge, handlers);
|
|
5
|
+
return bridge;
|
|
6
|
+
};
|
|
7
|
+
const getDecodedCommentHandlers = (bridge) => decodedCommentHandlers.get(bridge);
|
|
8
|
+
//#endregion
|
|
9
|
+
export { getDecodedCommentHandlers, registerDecodedCommentHandlers };
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { registerDecodedCommentHandlers } from "../bridge.js";
|
|
2
|
+
import { decodeCommentId } from "../codecs.js";
|
|
1
3
|
import { toAgentChange } from "./shared.js";
|
|
2
4
|
import { FOLIO_DOCUMENT_OPERATION_CONTRACT_VERSION, assertSupportedFolioDocumentOperationVersion, getFolioDocumentOperationIssues, getFolioDocumentOperationReceipts } from "@stll/folio-core/server";
|
|
3
5
|
import { createReply } from "@stll/folio-core/docx/replyToComment";
|
|
@@ -7,14 +9,6 @@ const toAgentCommentReply = (reply) => ({
|
|
|
7
9
|
author: reply.author,
|
|
8
10
|
text: replyPlainText(reply)
|
|
9
11
|
});
|
|
10
|
-
/**
|
|
11
|
-
* Parse a tool-supplied comment id into the numeric `Comment.id` this bridge
|
|
12
|
-
* matches against, rejecting anything but a bare non-negative integer.
|
|
13
|
-
* `Number.parseInt` alone would silently accept trailing junk (`"12abc"` ->
|
|
14
|
-
* `12`), which could reply to or resolve the wrong comment on malformed tool
|
|
15
|
-
* input.
|
|
16
|
-
*/
|
|
17
|
-
const parseCommentId = (commentId) => /^\d+$/.test(commentId) ? Number.parseInt(commentId, 10) : null;
|
|
18
12
|
/** A host-state `Comment`'s plain text, its paragraphs joined by newlines (mirrors `FolioDocxReviewer`'s reading). */
|
|
19
13
|
const replyPlainText = (comment) => comment.content.map(paragraphPlainText).join("\n");
|
|
20
14
|
const paragraphPlainText = (paragraph) => {
|
|
@@ -55,6 +49,28 @@ const createEditorRefBridge = (options) => {
|
|
|
55
49
|
const { ref, author, getComments, setComments } = options;
|
|
56
50
|
const undoDocumentOperations = ref.undoDocumentOperations?.bind(ref);
|
|
57
51
|
const mode = options.mode ?? "tracked-changes";
|
|
52
|
+
const replyToDecodedComment = (commentId, text) => {
|
|
53
|
+
const comments = getComments();
|
|
54
|
+
const reply = createReply(comments, commentId, {
|
|
55
|
+
author,
|
|
56
|
+
text
|
|
57
|
+
});
|
|
58
|
+
if (!reply) return false;
|
|
59
|
+
setComments([...comments, reply]);
|
|
60
|
+
return true;
|
|
61
|
+
};
|
|
62
|
+
const resolveDecodedComment = (commentId, resolved) => {
|
|
63
|
+
const comments = getComments();
|
|
64
|
+
let found = false;
|
|
65
|
+
const next = comments.map((comment) => {
|
|
66
|
+
if (comment.id !== commentId) return comment;
|
|
67
|
+
found = true;
|
|
68
|
+
return Object.assign({}, comment, { done: resolved });
|
|
69
|
+
});
|
|
70
|
+
if (!found) return false;
|
|
71
|
+
setComments(next);
|
|
72
|
+
return true;
|
|
73
|
+
};
|
|
58
74
|
const requireSnapshot = () => {
|
|
59
75
|
const snapshot = ref.createAIEditSnapshot();
|
|
60
76
|
if (!snapshot) throw new Error("The editor view is not mounted; no snapshot is available yet.");
|
|
@@ -62,17 +78,19 @@ const createEditorRefBridge = (options) => {
|
|
|
62
78
|
};
|
|
63
79
|
const bridge = {
|
|
64
80
|
snapshot: requireSnapshot,
|
|
81
|
+
documentOperationMode: mode,
|
|
65
82
|
applyDocumentOperations: (batch) => {
|
|
66
83
|
assertSupportedFolioDocumentOperationVersion(batch.version);
|
|
67
84
|
const snapshot = requireSnapshot();
|
|
68
|
-
const versionedBatch = {
|
|
85
|
+
const versionedBatch = batch.mode === void 0 ? {
|
|
69
86
|
...batch,
|
|
70
87
|
mode
|
|
71
|
-
};
|
|
88
|
+
} : batch;
|
|
72
89
|
if (ref.applyDocumentOperations) {
|
|
73
90
|
const result = ref.applyDocumentOperations({
|
|
74
91
|
snapshot,
|
|
75
92
|
batch: versionedBatch,
|
|
93
|
+
mode,
|
|
76
94
|
author
|
|
77
95
|
});
|
|
78
96
|
return {
|
|
@@ -114,9 +132,9 @@ const createEditorRefBridge = (options) => {
|
|
|
114
132
|
}
|
|
115
133
|
const result = ref.applyAIEditOperations({
|
|
116
134
|
snapshot,
|
|
117
|
-
operations: versionedBatch.operations,
|
|
118
|
-
|
|
119
|
-
|
|
135
|
+
operations: [...versionedBatch.operations],
|
|
136
|
+
author,
|
|
137
|
+
...versionedBatch.mode !== void 0 && { mode: versionedBatch.mode }
|
|
120
138
|
});
|
|
121
139
|
return {
|
|
122
140
|
version: FOLIO_DOCUMENT_OPERATION_CONTRACT_VERSION,
|
|
@@ -162,30 +180,12 @@ const createEditorRefBridge = (options) => {
|
|
|
162
180
|
return changes ? changes.map(toAgentChange) : [];
|
|
163
181
|
},
|
|
164
182
|
replyToComment: (commentId, text) => {
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
const comments = getComments();
|
|
168
|
-
const reply = createReply(comments, parentId, {
|
|
169
|
-
author,
|
|
170
|
-
text
|
|
171
|
-
});
|
|
172
|
-
if (!reply) return false;
|
|
173
|
-
setComments([...comments, reply]);
|
|
174
|
-
return true;
|
|
183
|
+
const decoded = decodeCommentId(commentId);
|
|
184
|
+
return decoded !== null && replyToDecodedComment(decoded.numeric, text);
|
|
175
185
|
},
|
|
176
186
|
resolveComment: (commentId, resolved) => {
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
const comments = getComments();
|
|
180
|
-
let found = false;
|
|
181
|
-
const next = comments.map((comment) => {
|
|
182
|
-
if (comment.id !== targetId) return comment;
|
|
183
|
-
found = true;
|
|
184
|
-
return Object.assign({}, comment, { done: resolved });
|
|
185
|
-
});
|
|
186
|
-
if (!found) return false;
|
|
187
|
-
setComments(next);
|
|
188
|
-
return true;
|
|
187
|
+
const decoded = decodeCommentId(commentId);
|
|
188
|
+
return decoded !== null && resolveDecodedComment(decoded.numeric, resolved);
|
|
189
189
|
},
|
|
190
190
|
scrollToBlock: (blockId) => ref.scrollToBlock(blockId),
|
|
191
191
|
getPageCount: () => ref.getTotalPages()
|
|
@@ -198,7 +198,10 @@ const createEditorRefBridge = (options) => {
|
|
|
198
198
|
if (getTargetPage) bridge.getTargetPage = (target) => getTargetPage(target);
|
|
199
199
|
const showInDocument = ref.showInDocument;
|
|
200
200
|
if (showInDocument) bridge.showInDocument = (target) => showInDocument(target);
|
|
201
|
-
return bridge
|
|
201
|
+
return registerDecodedCommentHandlers(bridge, {
|
|
202
|
+
replyToComment: ({ numeric }, text) => replyToDecodedComment(numeric, text),
|
|
203
|
+
resolveComment: ({ numeric }, resolved) => resolveDecodedComment(numeric, resolved)
|
|
204
|
+
});
|
|
202
205
|
};
|
|
203
206
|
//#endregion
|
|
204
207
|
export { createEditorRefBridge };
|
package/dist/bridges/reviewer.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { registerDecodedCommentHandlers } from "../bridge.js";
|
|
2
|
+
import { decodeCommentId } from "../codecs.js";
|
|
1
3
|
import { toAgentChange } from "./shared.js";
|
|
2
4
|
//#region src/bridges/reviewer.ts
|
|
3
5
|
const toAgentCommentReply = (reply) => ({
|
|
@@ -15,13 +17,6 @@ const toAgentComment = (comment) => ({
|
|
|
15
17
|
replies: comment.replies.map(toAgentCommentReply)
|
|
16
18
|
});
|
|
17
19
|
/**
|
|
18
|
-
* Parse a tool-supplied comment id into the numeric id `FolioDocxReviewer`
|
|
19
|
-
* expects, rejecting anything but a bare non-negative integer. `Number.
|
|
20
|
-
* parseInt` alone would silently accept trailing junk (`"12abc"` -> `12`),
|
|
21
|
-
* which could reply to the wrong comment thread on malformed tool input.
|
|
22
|
-
*/
|
|
23
|
-
const parseCommentId = (commentId) => /^\d+$/.test(commentId) ? Number.parseInt(commentId, 10) : null;
|
|
24
|
-
/**
|
|
25
20
|
* Build a {@link FolioAgentBridge} over a headless {@link FolioDocxReviewer}
|
|
26
21
|
* (`@stll/folio-core/server`). No optional capability members are
|
|
27
22
|
* implemented: a headless document has no live page/selection/scroll
|
|
@@ -31,24 +26,30 @@ const parseCommentId = (commentId) => /^\d+$/.test(commentId) ? Number.parseInt(
|
|
|
31
26
|
*/
|
|
32
27
|
const createReviewerBridge = (reviewer, options = {}) => {
|
|
33
28
|
const mode = options.mode ?? "tracked-changes";
|
|
34
|
-
return {
|
|
29
|
+
return registerDecodedCommentHandlers({
|
|
35
30
|
snapshot: () => reviewer.snapshot(),
|
|
36
|
-
|
|
31
|
+
documentOperationMode: mode,
|
|
32
|
+
applyDocumentOperations: (batch) => reviewer.applyDocumentOperations(batch.mode === void 0 ? {
|
|
37
33
|
...batch,
|
|
38
34
|
mode
|
|
39
|
-
}),
|
|
35
|
+
} : batch),
|
|
40
36
|
undoDocumentOperations: (undoHandle) => reviewer.undoDocumentOperations(undoHandle),
|
|
41
37
|
getComments: () => reviewer.getComments().map(toAgentComment),
|
|
42
38
|
getChanges: () => reviewer.getChanges().map(toAgentChange),
|
|
43
39
|
listStories: () => reviewer.listStories(),
|
|
44
40
|
readStory: (handle) => reviewer.readStory(handle),
|
|
45
41
|
replyToComment: (commentId, text) => {
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
return reviewer.replyTo(parentId, { text }) !== null;
|
|
42
|
+
const decoded = decodeCommentId(commentId);
|
|
43
|
+
return decoded !== null && reviewer.replyTo(decoded.numeric, { text }) !== null;
|
|
49
44
|
},
|
|
50
|
-
resolveComment: (commentId, resolved) =>
|
|
51
|
-
|
|
45
|
+
resolveComment: (commentId, resolved) => {
|
|
46
|
+
const decoded = decodeCommentId(commentId);
|
|
47
|
+
return decoded !== null && reviewer.resolveComment(decoded.raw, { resolved });
|
|
48
|
+
}
|
|
49
|
+
}, {
|
|
50
|
+
replyToComment: ({ numeric }, text) => reviewer.replyTo(numeric, { text }) !== null,
|
|
51
|
+
resolveComment: ({ raw }, resolved) => reviewer.resolveComment(raw, { resolved })
|
|
52
|
+
});
|
|
52
53
|
};
|
|
53
54
|
//#endregion
|
|
54
55
|
export { createReviewerBridge };
|
package/dist/codecs.d.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { FOLIO_TEXT_RANGE_JSON_SCHEMA } from "./operation-schema.js";
|
|
2
|
+
import { FolioAITextRangeHandle, FolioDocumentSectionHandle, FolioDocumentStoryHandle } from "@stll/folio-core/server";
|
|
3
|
+
//#region src/codecs.d.ts
|
|
4
|
+
declare const FOLIO_STORY_HANDLE_JSON_SCHEMA: {
|
|
5
|
+
readonly type: "object";
|
|
6
|
+
readonly properties: {
|
|
7
|
+
readonly type: {
|
|
8
|
+
readonly type: "string";
|
|
9
|
+
readonly enum: readonly ["main", "header", "footer", "footnote", "endnote"];
|
|
10
|
+
};
|
|
11
|
+
readonly relationshipId: {
|
|
12
|
+
readonly type: "string";
|
|
13
|
+
};
|
|
14
|
+
readonly noteId: {
|
|
15
|
+
readonly type: "integer";
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
readonly required: readonly ["type"];
|
|
19
|
+
readonly additionalProperties: false;
|
|
20
|
+
};
|
|
21
|
+
declare const FOLIO_SECTION_HANDLE_JSON_SCHEMA: {
|
|
22
|
+
readonly type: "object";
|
|
23
|
+
readonly properties: {
|
|
24
|
+
readonly type: {
|
|
25
|
+
readonly type: "string";
|
|
26
|
+
readonly enum: readonly ["headingSection"];
|
|
27
|
+
};
|
|
28
|
+
readonly story: {
|
|
29
|
+
readonly type: "string";
|
|
30
|
+
readonly enum: readonly ["main"];
|
|
31
|
+
};
|
|
32
|
+
readonly headingBlockId: {
|
|
33
|
+
readonly type: "string";
|
|
34
|
+
};
|
|
35
|
+
readonly headingTextHash: {
|
|
36
|
+
readonly type: "string";
|
|
37
|
+
};
|
|
38
|
+
readonly headingLevel: {
|
|
39
|
+
readonly type: "integer";
|
|
40
|
+
readonly minimum: 1;
|
|
41
|
+
readonly maximum: 9;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
readonly required: readonly ["type", "story", "headingBlockId", "headingTextHash", "headingLevel"];
|
|
45
|
+
readonly additionalProperties: false;
|
|
46
|
+
};
|
|
47
|
+
declare const FOLIO_SCOPED_HANDLE_JSON_SCHEMA: {
|
|
48
|
+
readonly type: "object";
|
|
49
|
+
readonly properties: {
|
|
50
|
+
readonly type: {
|
|
51
|
+
readonly type: "string";
|
|
52
|
+
readonly enum: readonly ["main", "header", "footer", "footnote", "endnote", "headingSection"];
|
|
53
|
+
};
|
|
54
|
+
readonly story: {
|
|
55
|
+
readonly type: "string";
|
|
56
|
+
readonly enum: readonly ["main"];
|
|
57
|
+
};
|
|
58
|
+
readonly relationshipId: {
|
|
59
|
+
readonly type: "string";
|
|
60
|
+
};
|
|
61
|
+
readonly noteId: {
|
|
62
|
+
readonly type: "integer";
|
|
63
|
+
};
|
|
64
|
+
readonly headingBlockId: {
|
|
65
|
+
readonly type: "string";
|
|
66
|
+
};
|
|
67
|
+
readonly headingTextHash: {
|
|
68
|
+
readonly type: "string";
|
|
69
|
+
};
|
|
70
|
+
readonly headingLevel: {
|
|
71
|
+
readonly type: "integer";
|
|
72
|
+
readonly minimum: 1;
|
|
73
|
+
readonly maximum: 9;
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
readonly required: readonly ["type"];
|
|
77
|
+
readonly additionalProperties: false;
|
|
78
|
+
};
|
|
79
|
+
declare const FOLIO_COMMENT_ID_JSON_SCHEMA: {
|
|
80
|
+
readonly type: "string";
|
|
81
|
+
readonly description: "The comment id string from `read_comments`.";
|
|
82
|
+
};
|
|
83
|
+
type FolioAgentCommentId = {
|
|
84
|
+
raw: string;
|
|
85
|
+
numeric: number;
|
|
86
|
+
};
|
|
87
|
+
declare const decodeSectionHandle: (value: unknown) => FolioDocumentSectionHandle | null;
|
|
88
|
+
declare const decodeStoryHandle: (value: unknown) => FolioDocumentStoryHandle | null;
|
|
89
|
+
declare const decodeMainStoryTextRangeHandle: (value: unknown) => FolioAITextRangeHandle | null;
|
|
90
|
+
declare const decodeCommentId: (value: unknown) => FolioAgentCommentId | null;
|
|
91
|
+
//#endregion
|
|
92
|
+
export { FOLIO_COMMENT_ID_JSON_SCHEMA, FOLIO_SCOPED_HANDLE_JSON_SCHEMA, FOLIO_SECTION_HANDLE_JSON_SCHEMA, FOLIO_STORY_HANDLE_JSON_SCHEMA, FOLIO_TEXT_RANGE_JSON_SCHEMA, FolioAgentCommentId, decodeCommentId, decodeMainStoryTextRangeHandle, decodeSectionHandle, decodeStoryHandle };
|
package/dist/codecs.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { FOLIO_TEXT_RANGE_JSON_SCHEMA } from "./operation-schema.js";
|
|
2
|
+
//#region src/codecs.ts
|
|
3
|
+
const NORMALIZED_TEXT_HASH_PATTERN = /^h[0-9a-z]+$/;
|
|
4
|
+
const isPlainObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5
|
+
const isNonEmptyString = (value) => typeof value === "string" && value.length > 0;
|
|
6
|
+
const FOLIO_STORY_HANDLE_JSON_SCHEMA = {
|
|
7
|
+
type: "object",
|
|
8
|
+
properties: {
|
|
9
|
+
type: {
|
|
10
|
+
type: "string",
|
|
11
|
+
enum: [
|
|
12
|
+
"main",
|
|
13
|
+
"header",
|
|
14
|
+
"footer",
|
|
15
|
+
"footnote",
|
|
16
|
+
"endnote"
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
relationshipId: { type: "string" },
|
|
20
|
+
noteId: { type: "integer" }
|
|
21
|
+
},
|
|
22
|
+
required: ["type"],
|
|
23
|
+
additionalProperties: false
|
|
24
|
+
};
|
|
25
|
+
const FOLIO_SECTION_HANDLE_JSON_SCHEMA = {
|
|
26
|
+
type: "object",
|
|
27
|
+
properties: {
|
|
28
|
+
type: {
|
|
29
|
+
type: "string",
|
|
30
|
+
enum: ["headingSection"]
|
|
31
|
+
},
|
|
32
|
+
story: {
|
|
33
|
+
type: "string",
|
|
34
|
+
enum: ["main"]
|
|
35
|
+
},
|
|
36
|
+
headingBlockId: { type: "string" },
|
|
37
|
+
headingTextHash: { type: "string" },
|
|
38
|
+
headingLevel: {
|
|
39
|
+
type: "integer",
|
|
40
|
+
minimum: 1,
|
|
41
|
+
maximum: 9
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
required: [
|
|
45
|
+
"type",
|
|
46
|
+
"story",
|
|
47
|
+
"headingBlockId",
|
|
48
|
+
"headingTextHash",
|
|
49
|
+
"headingLevel"
|
|
50
|
+
],
|
|
51
|
+
additionalProperties: false
|
|
52
|
+
};
|
|
53
|
+
const FOLIO_SCOPED_HANDLE_JSON_SCHEMA = {
|
|
54
|
+
type: "object",
|
|
55
|
+
properties: {
|
|
56
|
+
type: {
|
|
57
|
+
type: "string",
|
|
58
|
+
enum: [
|
|
59
|
+
"main",
|
|
60
|
+
"header",
|
|
61
|
+
"footer",
|
|
62
|
+
"footnote",
|
|
63
|
+
"endnote",
|
|
64
|
+
"headingSection"
|
|
65
|
+
]
|
|
66
|
+
},
|
|
67
|
+
story: {
|
|
68
|
+
type: "string",
|
|
69
|
+
enum: ["main"]
|
|
70
|
+
},
|
|
71
|
+
relationshipId: { type: "string" },
|
|
72
|
+
noteId: { type: "integer" },
|
|
73
|
+
headingBlockId: { type: "string" },
|
|
74
|
+
headingTextHash: { type: "string" },
|
|
75
|
+
headingLevel: {
|
|
76
|
+
type: "integer",
|
|
77
|
+
minimum: 1,
|
|
78
|
+
maximum: 9
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
required: ["type"],
|
|
82
|
+
additionalProperties: false
|
|
83
|
+
};
|
|
84
|
+
const FOLIO_COMMENT_ID_JSON_SCHEMA = {
|
|
85
|
+
type: "string",
|
|
86
|
+
description: "The comment id string from `read_comments`."
|
|
87
|
+
};
|
|
88
|
+
const decodeSectionHandle = (value) => {
|
|
89
|
+
if (!isPlainObject(value)) return null;
|
|
90
|
+
const headingBlockId = value["headingBlockId"];
|
|
91
|
+
const headingTextHash = value["headingTextHash"];
|
|
92
|
+
const headingLevel = value["headingLevel"];
|
|
93
|
+
if (value["type"] !== "headingSection" || value["story"] !== "main" || !isNonEmptyString(headingBlockId) || !isNonEmptyString(headingTextHash) || !NORMALIZED_TEXT_HASH_PATTERN.test(headingTextHash) || typeof headingLevel !== "number" || !Number.isInteger(headingLevel) || headingLevel < 1 || headingLevel > 9) return null;
|
|
94
|
+
return {
|
|
95
|
+
type: "headingSection",
|
|
96
|
+
story: "main",
|
|
97
|
+
headingBlockId,
|
|
98
|
+
headingTextHash,
|
|
99
|
+
headingLevel
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
const decodeStoryHandle = (value) => {
|
|
103
|
+
if (!isPlainObject(value)) return null;
|
|
104
|
+
const type = value["type"];
|
|
105
|
+
if (type === "main") return { type };
|
|
106
|
+
if (type === "header" || type === "footer") {
|
|
107
|
+
const relationshipId = value["relationshipId"];
|
|
108
|
+
if (!isNonEmptyString(relationshipId)) return null;
|
|
109
|
+
return {
|
|
110
|
+
type,
|
|
111
|
+
relationshipId
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
if (type === "footnote" || type === "endnote") {
|
|
115
|
+
const noteId = value["noteId"];
|
|
116
|
+
if (typeof noteId !== "number" || !Number.isInteger(noteId)) return null;
|
|
117
|
+
return {
|
|
118
|
+
type,
|
|
119
|
+
noteId
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
};
|
|
124
|
+
const decodeMainStoryTextRangeHandle = (value) => {
|
|
125
|
+
if (!isPlainObject(value)) return null;
|
|
126
|
+
const blockId = value["blockId"];
|
|
127
|
+
const startOffset = value["startOffset"];
|
|
128
|
+
const endOffset = value["endOffset"];
|
|
129
|
+
const selectedTextHash = value["selectedTextHash"];
|
|
130
|
+
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) || !NORMALIZED_TEXT_HASH_PATTERN.test(selectedTextHash)) return null;
|
|
131
|
+
return {
|
|
132
|
+
type: "textRange",
|
|
133
|
+
story: "main",
|
|
134
|
+
blockId,
|
|
135
|
+
startOffset,
|
|
136
|
+
endOffset,
|
|
137
|
+
selectedTextHash
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
const decodeCommentId = (value) => {
|
|
141
|
+
if (typeof value !== "string" || !/^\d+$/u.test(value)) return null;
|
|
142
|
+
const numeric = Number(value);
|
|
143
|
+
if (!Number.isSafeInteger(numeric)) return null;
|
|
144
|
+
return {
|
|
145
|
+
raw: value,
|
|
146
|
+
numeric
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
//#endregion
|
|
150
|
+
export { FOLIO_COMMENT_ID_JSON_SCHEMA, FOLIO_SCOPED_HANDLE_JSON_SCHEMA, FOLIO_SECTION_HANDLE_JSON_SCHEMA, FOLIO_STORY_HANDLE_JSON_SCHEMA, FOLIO_TEXT_RANGE_JSON_SCHEMA, decodeCommentId, decodeMainStoryTextRangeHandle, decodeSectionHandle, decodeStoryHandle };
|
package/dist/execute.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { FolioToolCallResult } from "./types.js";
|
|
1
|
+
import { FolioAgentToolName, FolioToolCallResult } from "./types.js";
|
|
2
2
|
import { FolioAgentBridge } from "./bridge.js";
|
|
3
|
+
import { FolioAgentToolInputByName, FolioToolCallResultFor } from "./tool-contract.js";
|
|
3
4
|
//#region src/execute.d.ts
|
|
4
5
|
/**
|
|
5
6
|
* Execute one tool call against a {@link FolioAgentBridge}. Every EXPECTED
|
|
@@ -13,6 +14,7 @@ import { FolioAgentBridge } from "./bridge.js";
|
|
|
13
14
|
* Every bridge member used here ({@link FolioDocxReviewer} and the live-editor
|
|
14
15
|
* ref) is synchronous, so this stays synchronous too.
|
|
15
16
|
*/
|
|
16
|
-
declare const
|
|
17
|
+
declare const executeFolioToolCallUntyped: (name: string, args: unknown, bridge: FolioAgentBridge) => FolioToolCallResult;
|
|
18
|
+
declare function executeFolioToolCall<TName extends FolioAgentToolName>(name: TName, args: FolioAgentToolInputByName[TName], bridge: FolioAgentBridge): FolioToolCallResultFor<TName>;
|
|
17
19
|
//#endregion
|
|
18
|
-
export { executeFolioToolCall };
|
|
20
|
+
export { executeFolioToolCall, executeFolioToolCallUntyped };
|