@stll/folio-agents 0.1.1 → 0.3.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 +16 -15
- package/dist/bridge.d.ts +7 -6
- package/dist/bridges/editor-ref.d.ts +9 -3
- package/dist/bridges/editor-ref.js +63 -10
- package/dist/bridges/reviewer.js +6 -1
- package/dist/compare.d.ts +10 -48
- package/dist/compare.js +15 -264
- package/dist/execute.js +65 -5
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/operation-schema.d.ts +1576 -0
- package/dist/operation-schema.js +525 -0
- package/dist/parse.js +49 -3
- package/dist/tools.d.ts +10 -1
- package/dist/tools.js +111 -43
- package/dist/types.d.ts +9 -1
- package/dist/types.js +2 -0
- package/package.json +3 -3
- package/skills/folio-agents/SKILL.md +10 -6
package/dist/execute.js
CHANGED
|
@@ -1,5 +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
4
|
//#region src/execute.ts
|
|
4
5
|
const isPlainObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5
6
|
const isNonEmptyString = (value) => typeof value === "string" && value.length > 0;
|
|
@@ -37,6 +38,8 @@ const executeFolioToolCall = (name, args, bridge) => {
|
|
|
37
38
|
};
|
|
38
39
|
const dispatch = (name, args, bridge) => {
|
|
39
40
|
if (name === FOLIO_AGENT_TOOL_NAMES.readDocument) return readDocument(bridge);
|
|
41
|
+
if (name === FOLIO_AGENT_TOOL_NAMES.listStories) return bridge.listStories ? ok(bridge.listStories()) : fail("This editor surface does not support story discovery.");
|
|
42
|
+
if (name === FOLIO_AGENT_TOOL_NAMES.readStory) return readStory(args, bridge);
|
|
40
43
|
if (name === FOLIO_AGENT_TOOL_NAMES.findText) return findText(args, bridge);
|
|
41
44
|
if (name === FOLIO_AGENT_TOOL_NAMES.readComments) return readComments(args, bridge);
|
|
42
45
|
if (name === FOLIO_AGENT_TOOL_NAMES.readChanges) return ok(bridge.getChanges());
|
|
@@ -57,6 +60,31 @@ const readDocument = (bridge) => {
|
|
|
57
60
|
text: block.text
|
|
58
61
|
})));
|
|
59
62
|
};
|
|
63
|
+
const readStory = (args, bridge) => {
|
|
64
|
+
if (!bridge.readStory) return fail("This editor surface does not support story reads.");
|
|
65
|
+
if (!isPlainObject(args) || !isPlainObject(args["handle"])) return fail("read_story requires a typed `handle` from list_stories.");
|
|
66
|
+
const handle = args["handle"];
|
|
67
|
+
const type = handle["type"];
|
|
68
|
+
let parsed;
|
|
69
|
+
if (type === "main") parsed = { type };
|
|
70
|
+
else if (type === "header" || type === "footer") {
|
|
71
|
+
const relationshipId = handle["relationshipId"];
|
|
72
|
+
if (!isNonEmptyString(relationshipId)) return fail("read_story requires the handle's `relationshipId`.");
|
|
73
|
+
parsed = {
|
|
74
|
+
type,
|
|
75
|
+
relationshipId
|
|
76
|
+
};
|
|
77
|
+
} else if (type === "footnote" || type === "endnote") {
|
|
78
|
+
const noteId = handle["noteId"];
|
|
79
|
+
if (typeof noteId !== "number" || !Number.isInteger(noteId)) return fail("read_story requires the handle's integer `noteId`.");
|
|
80
|
+
parsed = {
|
|
81
|
+
type,
|
|
82
|
+
noteId
|
|
83
|
+
};
|
|
84
|
+
} else return fail("read_story received an unsupported story handle type.");
|
|
85
|
+
const story = bridge.readStory(parsed);
|
|
86
|
+
return story ? ok(story) : fail("The requested story was not found; run list_stories again.");
|
|
87
|
+
};
|
|
60
88
|
const CONTEXT_RADIUS = 40;
|
|
61
89
|
const findTextMatches = (blocks, query, matchCase) => {
|
|
62
90
|
const matches = [];
|
|
@@ -73,8 +101,16 @@ const findTextMatches = (blocks, query, matchCase) => {
|
|
|
73
101
|
if (matches.length < MAX_FIND_MATCHES) {
|
|
74
102
|
const contextStart = Math.max(0, at - CONTEXT_RADIUS);
|
|
75
103
|
const contextEnd = Math.min(block.text.length, at + query.length + CONTEXT_RADIUS);
|
|
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;
|
|
76
111
|
matches.push({
|
|
77
112
|
blockId: block.id,
|
|
113
|
+
range,
|
|
78
114
|
occurrenceInBlock: occurrence,
|
|
79
115
|
context: block.text.slice(contextStart, contextEnd)
|
|
80
116
|
});
|
|
@@ -97,11 +133,12 @@ const findText = (args, bridge) => {
|
|
|
97
133
|
if (matchCase !== void 0 && typeof matchCase !== "boolean") return fail("find_text's `matchCase` must be a boolean when provided.");
|
|
98
134
|
const { blocks } = bridge.snapshot();
|
|
99
135
|
const { matches, totalMatches } = findTextMatches(blocks, query, matchCase === true);
|
|
100
|
-
|
|
136
|
+
const result = {
|
|
101
137
|
matches,
|
|
102
138
|
truncated: totalMatches > matches.length,
|
|
103
139
|
totalMatches
|
|
104
|
-
}
|
|
140
|
+
};
|
|
141
|
+
return ok(result);
|
|
105
142
|
};
|
|
106
143
|
const isCommentFilter = (value) => value === "all" || value === "open" || value === "resolved";
|
|
107
144
|
const readComments = (args, bridge) => {
|
|
@@ -122,26 +159,49 @@ const explainSkipReason = (reason) => {
|
|
|
122
159
|
if (reason === "ambiguousFind") return "`find` matches more than once in this block; narrow it so it matches exactly once, or use a replaceBlock operation instead.";
|
|
123
160
|
if (reason === "missingFind") return "`find` was not found in this block; re-read the block's current text and retry.";
|
|
124
161
|
if (reason === "unsupportedBlock") return "this block kind does not support this operation.";
|
|
162
|
+
if (reason === "unsupportedMode") return "this operation does not support the requested mutation mode; inspect document operation capabilities and retry with a supported mode.";
|
|
163
|
+
if (reason === "atomicBatchRejected") return "another operation in this atomic batch could not be applied; no operations were committed.";
|
|
164
|
+
if (reason === "preconditionFailed") return "the target block changed after the operation was prepared; re-read the document and retry with a fresh operation.";
|
|
165
|
+
if (reason === "staleRange") return "the selected range changed or shifted; run find_text again and retry with the fresh range.";
|
|
125
166
|
if (reason === "emptyOperation") return "this operation has no effect; nothing to apply.";
|
|
126
167
|
if (reason === "noopOperation") return "this operation would not change the document (the text already matches what you asked for).";
|
|
127
168
|
return reason;
|
|
128
169
|
};
|
|
129
170
|
const summarizeApplyResult = (result) => ({
|
|
171
|
+
version: result.version,
|
|
130
172
|
applied: result.applied.map((entry) => ({ id: entry.id })),
|
|
131
173
|
skipped: result.skipped.map((entry) => ({
|
|
132
174
|
id: entry.id,
|
|
133
175
|
reason: explainSkipReason(entry.reason)
|
|
134
|
-
}))
|
|
176
|
+
})),
|
|
177
|
+
issues: result.issues ?? [],
|
|
178
|
+
receipts: result.receipts ?? []
|
|
135
179
|
});
|
|
180
|
+
const applyOperations = (bridge, operations) => {
|
|
181
|
+
const snapshot = bridge.snapshot();
|
|
182
|
+
const guardedOperations = [];
|
|
183
|
+
for (const operation of operations) {
|
|
184
|
+
const blockId = operation.type === "replaceRange" || operation.type === "commentOnRange" || operation.type === "formatRange" ? operation.range.blockId : operation.blockId;
|
|
185
|
+
const blockTextHash = snapshot.anchors[blockId]?.textHash;
|
|
186
|
+
guardedOperations.push({
|
|
187
|
+
...operation,
|
|
188
|
+
...blockTextHash !== void 0 && { precondition: { blockTextHash } }
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
return summarizeApplyResult(bridge.applyDocumentOperations({
|
|
192
|
+
version: FOLIO_DOCUMENT_OPERATION_CONTRACT_VERSION,
|
|
193
|
+
operations: guardedOperations
|
|
194
|
+
}));
|
|
195
|
+
};
|
|
136
196
|
const addComment = (args, bridge) => {
|
|
137
197
|
const parsed = parseAddCommentInput(args);
|
|
138
198
|
if (!parsed.ok) return fail(parsed.error);
|
|
139
|
-
return ok(
|
|
199
|
+
return ok(applyOperations(bridge, [parsed.operation]));
|
|
140
200
|
};
|
|
141
201
|
const suggestChanges = (args, bridge) => {
|
|
142
202
|
const parsed = parseSuggestChangesInput(args);
|
|
143
203
|
if (!parsed.ok) return fail(parsed.error);
|
|
144
|
-
return ok(
|
|
204
|
+
return ok(applyOperations(bridge, parsed.operations));
|
|
145
205
|
};
|
|
146
206
|
const replyComment = (args, bridge) => {
|
|
147
207
|
if (!isPlainObject(args)) return fail("reply_comment expects an object with `commentId` and `text` strings.");
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { FOLIO_AGENT_TOOL_NAMES, FolioAgentApplyOperationsSummary, FolioAgentBlock, FolioAgentChange, FolioAgentComment, FolioAgentCommentFilter, FolioAgentCommentReply, FolioAgentTextMatch, FolioAgentToolDefinition, FolioAgentToolName, FolioToolCallResult } from "./types.js";
|
|
2
2
|
import { FolioAgentBridge } from "./bridge.js";
|
|
3
|
-
import { CreateEditorRefBridgeOptions, FolioAgentEditorRefLike, createEditorRefBridge } from "./bridges/editor-ref.js";
|
|
3
|
+
import { CreateEditorRefBridgeOptions, FolioAgentEditorApplyDocumentOperationsOptions, FolioAgentEditorRefLike, createEditorRefBridge } from "./bridges/editor-ref.js";
|
|
4
4
|
import { CreateReviewerBridgeOptions, createReviewerBridge } from "./bridges/reviewer.js";
|
|
5
5
|
import { FolioAgentBlockDiff, FolioAgentVersionDiff, FolioAgentVersionDiffSegment, compareDocxVersions, formatVersionDiffForLLM } from "./compare.js";
|
|
6
6
|
import { executeFolioToolCall } from "./execute.js";
|
|
7
|
+
import { FOLIO_DOCUMENT_OPERATION_BATCH_JSON_SCHEMA, FOLIO_DOCUMENT_OPERATION_JSON_SCHEMA, folioDocumentOperationBatchSchema } from "./operation-schema.js";
|
|
7
8
|
import { ParseAddCommentResult, ParseSuggestChangesResult, parseAddCommentInput, parseSuggestChangesInput } from "./parse.js";
|
|
8
9
|
import { AnthropicToolDefinition, OpenAIToolDefinition, toAnthropicTools, toOpenAITools } from "./providers.js";
|
|
9
10
|
import { FOLIO_AGENT_TOOLS, getFolioToolDefinitions } from "./tools.js";
|
|
10
|
-
export { type AnthropicToolDefinition, type CreateEditorRefBridgeOptions, type CreateReviewerBridgeOptions, FOLIO_AGENT_TOOLS, FOLIO_AGENT_TOOL_NAMES, type FolioAgentApplyOperationsSummary, type FolioAgentBlock, type FolioAgentBlockDiff, type FolioAgentBridge, type FolioAgentChange, type FolioAgentComment, type FolioAgentCommentFilter, type FolioAgentCommentReply, type FolioAgentEditorRefLike, type FolioAgentTextMatch, type FolioAgentToolDefinition, type FolioAgentToolName, type FolioAgentVersionDiff, type FolioAgentVersionDiffSegment, type FolioToolCallResult, type OpenAIToolDefinition, type ParseAddCommentResult, type ParseSuggestChangesResult, compareDocxVersions, createEditorRefBridge, createReviewerBridge, executeFolioToolCall, 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 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 };
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,7 @@ import { FOLIO_AGENT_TOOL_NAMES } from "./types.js";
|
|
|
4
4
|
import { executeFolioToolCall } from "./execute.js";
|
|
5
5
|
import { createEditorRefBridge } from "./bridges/editor-ref.js";
|
|
6
6
|
import { createReviewerBridge } from "./bridges/reviewer.js";
|
|
7
|
+
import { FOLIO_DOCUMENT_OPERATION_BATCH_JSON_SCHEMA, FOLIO_DOCUMENT_OPERATION_JSON_SCHEMA, folioDocumentOperationBatchSchema } from "./operation-schema.js";
|
|
7
8
|
import { toAnthropicTools, toOpenAITools } from "./providers.js";
|
|
8
9
|
import { FOLIO_AGENT_TOOLS, getFolioToolDefinitions } from "./tools.js";
|
|
9
|
-
export { FOLIO_AGENT_TOOLS, FOLIO_AGENT_TOOL_NAMES, compareDocxVersions, createEditorRefBridge, createReviewerBridge, executeFolioToolCall, formatVersionDiffForLLM, getFolioToolDefinitions, parseAddCommentInput, parseSuggestChangesInput, toAnthropicTools, toOpenAITools };
|
|
10
|
+
export { FOLIO_AGENT_TOOLS, FOLIO_AGENT_TOOL_NAMES, FOLIO_DOCUMENT_OPERATION_BATCH_JSON_SCHEMA, FOLIO_DOCUMENT_OPERATION_JSON_SCHEMA, compareDocxVersions, createEditorRefBridge, createReviewerBridge, executeFolioToolCall, folioDocumentOperationBatchSchema, formatVersionDiffForLLM, getFolioToolDefinitions, parseAddCommentInput, parseSuggestChangesInput, toAnthropicTools, toOpenAITools };
|