@stll/folio-agents 0.2.0 → 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/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/operation-schema.d.ts +1576 -0
- package/dist/operation-schema.js +525 -0
- package/dist/tools.d.ts +10 -1
- package/dist/tools.js +73 -78
- package/package.json +1 -1
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
import { FOLIO_DOCUMENT_OPERATION_CONTRACT_VERSION, FOLIO_DOCUMENT_OPERATION_MODES, InvalidFolioDocumentOperationBatchError, UnsupportedFolioDocumentOperationVersionError, parseFolioDocumentOperationBatch } from "@stll/folio-core/server";
|
|
2
|
+
//#region src/operation-schema.ts
|
|
3
|
+
/**
|
|
4
|
+
* Reusable projections of folio's versioned document-operation contract for
|
|
5
|
+
* tool builders: a JSON Schema for one operation, a JSON Schema for the
|
|
6
|
+
* versioned batch envelope, and a Standard Schema V1 wrapper around
|
|
7
|
+
* `parseFolioDocumentOperationBatch` so downstream tool definitions (e.g.
|
|
8
|
+
* TanStack AI tools) can consume the contract directly instead of
|
|
9
|
+
* hand-mirroring it in valibot/zod.
|
|
10
|
+
*
|
|
11
|
+
* The strict throwing parser in `@stll/folio-core` stays the single source of
|
|
12
|
+
* truth for semantics; everything here is a projection of it. Constraints
|
|
13
|
+
* JSON Schema cannot express (`endOffset > startOffset`, unique operation
|
|
14
|
+
* ids within a batch, per-type mode support) are noted in `description`s and
|
|
15
|
+
* enforced by the parser.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Pattern for the normalized text hashes the contract uses to detect stale
|
|
19
|
+
* targets (`selectedTextHash`, `precondition.blockTextHash`). Mirrors the
|
|
20
|
+
* parser's check in `@stll/folio-core`'s `document-operations.ts`.
|
|
21
|
+
*/
|
|
22
|
+
const NORMALIZED_TEXT_HASH_PATTERN = "^h[0-9a-z]+$";
|
|
23
|
+
/**
|
|
24
|
+
* JSON Schema for the contract's `textRange` handle (a serializable range
|
|
25
|
+
* over the visible text of one main-story block, as returned by `find_text`
|
|
26
|
+
* style tools). Shared between {@link FOLIO_DOCUMENT_OPERATION_JSON_SCHEMA}
|
|
27
|
+
* and the `suggest_changes` tool schema in `tools.ts` so the two cannot
|
|
28
|
+
* drift. `endOffset` must be greater than `startOffset`; JSON Schema cannot
|
|
29
|
+
* express that relation, the parser enforces it.
|
|
30
|
+
*/
|
|
31
|
+
const FOLIO_TEXT_RANGE_JSON_SCHEMA = {
|
|
32
|
+
type: "object",
|
|
33
|
+
description: "A range over the visible text of one main-story block. Offsets are zero-based UTF-16 boundaries; `endOffset` must be greater than `startOffset`. Copy range objects verbatim from the tool that produced them — `selectedTextHash` makes a shifted or edited selection fail as stale instead of hitting the wrong text.",
|
|
34
|
+
properties: {
|
|
35
|
+
type: {
|
|
36
|
+
type: "string",
|
|
37
|
+
enum: ["textRange"]
|
|
38
|
+
},
|
|
39
|
+
story: {
|
|
40
|
+
type: "string",
|
|
41
|
+
enum: ["main"]
|
|
42
|
+
},
|
|
43
|
+
blockId: {
|
|
44
|
+
type: "string",
|
|
45
|
+
minLength: 1
|
|
46
|
+
},
|
|
47
|
+
startOffset: {
|
|
48
|
+
type: "integer",
|
|
49
|
+
minimum: 0
|
|
50
|
+
},
|
|
51
|
+
endOffset: {
|
|
52
|
+
type: "integer",
|
|
53
|
+
minimum: 1,
|
|
54
|
+
description: "Exclusive end offset; must be greater than `startOffset`."
|
|
55
|
+
},
|
|
56
|
+
selectedTextHash: {
|
|
57
|
+
type: "string",
|
|
58
|
+
pattern: NORMALIZED_TEXT_HASH_PATTERN,
|
|
59
|
+
description: "Normalized hash of the selected text, used to detect stale ranges."
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
required: [
|
|
63
|
+
"type",
|
|
64
|
+
"story",
|
|
65
|
+
"blockId",
|
|
66
|
+
"startOffset",
|
|
67
|
+
"endOffset",
|
|
68
|
+
"selectedTextHash"
|
|
69
|
+
],
|
|
70
|
+
additionalProperties: false
|
|
71
|
+
};
|
|
72
|
+
const preconditionJsonSchema = {
|
|
73
|
+
type: "object",
|
|
74
|
+
description: "Optional guard: the operation is skipped (reason `preconditionFailed`) unless the target block's normalized text hash still matches.",
|
|
75
|
+
properties: { blockTextHash: {
|
|
76
|
+
type: "string",
|
|
77
|
+
pattern: NORMALIZED_TEXT_HASH_PATTERN,
|
|
78
|
+
description: "Normalized hash of the target block's text."
|
|
79
|
+
} },
|
|
80
|
+
required: ["blockTextHash"],
|
|
81
|
+
additionalProperties: false
|
|
82
|
+
};
|
|
83
|
+
const commentJsonSchema = {
|
|
84
|
+
type: "object",
|
|
85
|
+
description: "A comment attached to the text affected by this operation.",
|
|
86
|
+
properties: { text: {
|
|
87
|
+
type: "string",
|
|
88
|
+
description: "The comment body."
|
|
89
|
+
} },
|
|
90
|
+
required: ["text"],
|
|
91
|
+
additionalProperties: false
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Properties every operation variant accepts: the required `id` plus the
|
|
95
|
+
* optional review metadata (`severity`, `area`) and `precondition` guard.
|
|
96
|
+
*/
|
|
97
|
+
const operationMetaProperties = {
|
|
98
|
+
id: {
|
|
99
|
+
type: "string",
|
|
100
|
+
description: "Caller-supplied operation id, echoed back in `applied` / `skipped` results. Must be unique within a batch (enforced by the parser)."
|
|
101
|
+
},
|
|
102
|
+
severity: {
|
|
103
|
+
type: "string",
|
|
104
|
+
enum: [
|
|
105
|
+
"low",
|
|
106
|
+
"medium",
|
|
107
|
+
"high"
|
|
108
|
+
],
|
|
109
|
+
description: "Optional review severity for structured-review workflows."
|
|
110
|
+
},
|
|
111
|
+
area: {
|
|
112
|
+
type: "string",
|
|
113
|
+
description: "Optional review area label (e.g. \"Penalty\") for structured-review workflows."
|
|
114
|
+
},
|
|
115
|
+
precondition: preconditionJsonSchema
|
|
116
|
+
};
|
|
117
|
+
const blockIdProperty = {
|
|
118
|
+
type: "string",
|
|
119
|
+
description: "Id of the target block, from a prior document read."
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* JSON Schema (draft-07 compatible) for ONE document operation: the full
|
|
123
|
+
* ten-variant union accepted by `parseFolioDocumentOperationBatch` in
|
|
124
|
+
* `@stll/folio-core`, one `oneOf` variant per entry in
|
|
125
|
+
* `FOLIO_DOCUMENT_OPERATION_TYPES`. Intended for LLM tool definitions and
|
|
126
|
+
* other consumers that need the contract's wire shape without re-declaring
|
|
127
|
+
* it; note that the `suggest_changes` tool in `tools.ts` deliberately
|
|
128
|
+
* narrows this union (see the comment there).
|
|
129
|
+
*/
|
|
130
|
+
const FOLIO_DOCUMENT_OPERATION_JSON_SCHEMA = {
|
|
131
|
+
description: "One document operation, discriminated by `type`. Mirrors the operation union accepted by `parseFolioDocumentOperationBatch` in @stll/folio-core.",
|
|
132
|
+
oneOf: [
|
|
133
|
+
{
|
|
134
|
+
type: "object",
|
|
135
|
+
description: "Replace an exact text match inside one block.",
|
|
136
|
+
properties: {
|
|
137
|
+
...operationMetaProperties,
|
|
138
|
+
type: {
|
|
139
|
+
type: "string",
|
|
140
|
+
enum: ["replaceInBlock"]
|
|
141
|
+
},
|
|
142
|
+
blockId: blockIdProperty,
|
|
143
|
+
find: {
|
|
144
|
+
type: "string",
|
|
145
|
+
description: "The exact text to find within the block."
|
|
146
|
+
},
|
|
147
|
+
replace: {
|
|
148
|
+
type: "string",
|
|
149
|
+
description: "The replacement text."
|
|
150
|
+
},
|
|
151
|
+
comment: commentJsonSchema
|
|
152
|
+
},
|
|
153
|
+
required: [
|
|
154
|
+
"id",
|
|
155
|
+
"type",
|
|
156
|
+
"blockId",
|
|
157
|
+
"find",
|
|
158
|
+
"replace"
|
|
159
|
+
],
|
|
160
|
+
additionalProperties: false
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
type: "object",
|
|
164
|
+
description: "Replace the text covered by a range handle.",
|
|
165
|
+
properties: {
|
|
166
|
+
...operationMetaProperties,
|
|
167
|
+
type: {
|
|
168
|
+
type: "string",
|
|
169
|
+
enum: ["replaceRange"]
|
|
170
|
+
},
|
|
171
|
+
range: FOLIO_TEXT_RANGE_JSON_SCHEMA,
|
|
172
|
+
replace: {
|
|
173
|
+
type: "string",
|
|
174
|
+
description: "The replacement text."
|
|
175
|
+
},
|
|
176
|
+
comment: commentJsonSchema
|
|
177
|
+
},
|
|
178
|
+
required: [
|
|
179
|
+
"id",
|
|
180
|
+
"type",
|
|
181
|
+
"range",
|
|
182
|
+
"replace"
|
|
183
|
+
],
|
|
184
|
+
additionalProperties: false
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
type: "object",
|
|
188
|
+
description: "Attach a comment to the text covered by a range handle.",
|
|
189
|
+
properties: {
|
|
190
|
+
...operationMetaProperties,
|
|
191
|
+
type: {
|
|
192
|
+
type: "string",
|
|
193
|
+
enum: ["commentOnRange"]
|
|
194
|
+
},
|
|
195
|
+
range: FOLIO_TEXT_RANGE_JSON_SCHEMA,
|
|
196
|
+
comment: commentJsonSchema
|
|
197
|
+
},
|
|
198
|
+
required: [
|
|
199
|
+
"id",
|
|
200
|
+
"type",
|
|
201
|
+
"range",
|
|
202
|
+
"comment"
|
|
203
|
+
],
|
|
204
|
+
additionalProperties: false
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
type: "object",
|
|
208
|
+
description: "Apply inline formatting to the text covered by a range handle. Direct mode only (formatting is not representable as a tracked change).",
|
|
209
|
+
properties: {
|
|
210
|
+
...operationMetaProperties,
|
|
211
|
+
type: {
|
|
212
|
+
type: "string",
|
|
213
|
+
enum: ["formatRange"]
|
|
214
|
+
},
|
|
215
|
+
range: FOLIO_TEXT_RANGE_JSON_SCHEMA,
|
|
216
|
+
formatting: {
|
|
217
|
+
type: "object",
|
|
218
|
+
description: "Inline formatting to apply; at least one property is required.",
|
|
219
|
+
properties: {
|
|
220
|
+
bold: { type: "boolean" },
|
|
221
|
+
italic: { type: "boolean" },
|
|
222
|
+
underline: { type: "boolean" }
|
|
223
|
+
},
|
|
224
|
+
minProperties: 1,
|
|
225
|
+
additionalProperties: false
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
required: [
|
|
229
|
+
"id",
|
|
230
|
+
"type",
|
|
231
|
+
"range",
|
|
232
|
+
"formatting"
|
|
233
|
+
],
|
|
234
|
+
additionalProperties: false
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
type: "object",
|
|
238
|
+
description: "Insert a new paragraph after the anchor block.",
|
|
239
|
+
properties: {
|
|
240
|
+
...operationMetaProperties,
|
|
241
|
+
type: {
|
|
242
|
+
type: "string",
|
|
243
|
+
enum: ["insertAfterBlock"]
|
|
244
|
+
},
|
|
245
|
+
blockId: blockIdProperty,
|
|
246
|
+
text: {
|
|
247
|
+
type: "string",
|
|
248
|
+
description: "The paragraph text to insert."
|
|
249
|
+
},
|
|
250
|
+
inheritFormatting: {
|
|
251
|
+
type: "boolean",
|
|
252
|
+
description: "Inherit the anchor block's formatting for the inserted paragraph."
|
|
253
|
+
},
|
|
254
|
+
pageBreakBefore: {
|
|
255
|
+
type: "boolean",
|
|
256
|
+
description: "Start the inserted paragraph on a new page (`pageBreakBefore`)."
|
|
257
|
+
},
|
|
258
|
+
styleId: {
|
|
259
|
+
type: "string",
|
|
260
|
+
description: "Paragraph style id for the inserted block (e.g. \"ClauseHeading1\")."
|
|
261
|
+
},
|
|
262
|
+
comment: commentJsonSchema
|
|
263
|
+
},
|
|
264
|
+
required: [
|
|
265
|
+
"id",
|
|
266
|
+
"type",
|
|
267
|
+
"blockId",
|
|
268
|
+
"text"
|
|
269
|
+
],
|
|
270
|
+
additionalProperties: false
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
type: "object",
|
|
274
|
+
description: "Insert a new paragraph before the anchor block.",
|
|
275
|
+
properties: {
|
|
276
|
+
...operationMetaProperties,
|
|
277
|
+
type: {
|
|
278
|
+
type: "string",
|
|
279
|
+
enum: ["insertBeforeBlock"]
|
|
280
|
+
},
|
|
281
|
+
blockId: blockIdProperty,
|
|
282
|
+
text: {
|
|
283
|
+
type: "string",
|
|
284
|
+
description: "The paragraph text to insert."
|
|
285
|
+
},
|
|
286
|
+
inheritFormatting: {
|
|
287
|
+
type: "boolean",
|
|
288
|
+
description: "Inherit the anchor block's formatting for the inserted paragraph."
|
|
289
|
+
},
|
|
290
|
+
pageBreakBefore: {
|
|
291
|
+
type: "boolean",
|
|
292
|
+
description: "Start the inserted paragraph on a new page (`pageBreakBefore`)."
|
|
293
|
+
},
|
|
294
|
+
styleId: {
|
|
295
|
+
type: "string",
|
|
296
|
+
description: "Paragraph style id for the inserted block (e.g. \"ClauseHeading1\")."
|
|
297
|
+
},
|
|
298
|
+
comment: commentJsonSchema
|
|
299
|
+
},
|
|
300
|
+
required: [
|
|
301
|
+
"id",
|
|
302
|
+
"type",
|
|
303
|
+
"blockId",
|
|
304
|
+
"text"
|
|
305
|
+
],
|
|
306
|
+
additionalProperties: false
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
type: "object",
|
|
310
|
+
description: "Replace one block's entire text.",
|
|
311
|
+
properties: {
|
|
312
|
+
...operationMetaProperties,
|
|
313
|
+
type: {
|
|
314
|
+
type: "string",
|
|
315
|
+
enum: ["replaceBlock"]
|
|
316
|
+
},
|
|
317
|
+
blockId: blockIdProperty,
|
|
318
|
+
text: {
|
|
319
|
+
type: "string",
|
|
320
|
+
description: "The new block text."
|
|
321
|
+
},
|
|
322
|
+
preserveFormatting: {
|
|
323
|
+
type: "boolean",
|
|
324
|
+
description: "Keep the block's existing formatting for the replacement text."
|
|
325
|
+
},
|
|
326
|
+
styleId: {
|
|
327
|
+
type: "string",
|
|
328
|
+
description: "Paragraph style id to set on the replaced block."
|
|
329
|
+
},
|
|
330
|
+
comment: commentJsonSchema
|
|
331
|
+
},
|
|
332
|
+
required: [
|
|
333
|
+
"id",
|
|
334
|
+
"type",
|
|
335
|
+
"blockId",
|
|
336
|
+
"text"
|
|
337
|
+
],
|
|
338
|
+
additionalProperties: false
|
|
339
|
+
},
|
|
340
|
+
{
|
|
341
|
+
type: "object",
|
|
342
|
+
description: "Delete one block.",
|
|
343
|
+
properties: {
|
|
344
|
+
...operationMetaProperties,
|
|
345
|
+
type: {
|
|
346
|
+
type: "string",
|
|
347
|
+
enum: ["deleteBlock"]
|
|
348
|
+
},
|
|
349
|
+
blockId: blockIdProperty,
|
|
350
|
+
comment: commentJsonSchema
|
|
351
|
+
},
|
|
352
|
+
required: [
|
|
353
|
+
"id",
|
|
354
|
+
"type",
|
|
355
|
+
"blockId"
|
|
356
|
+
],
|
|
357
|
+
additionalProperties: false
|
|
358
|
+
},
|
|
359
|
+
{
|
|
360
|
+
type: "object",
|
|
361
|
+
description: "Attach a comment to one block, optionally quoting text within it.",
|
|
362
|
+
properties: {
|
|
363
|
+
...operationMetaProperties,
|
|
364
|
+
type: {
|
|
365
|
+
type: "string",
|
|
366
|
+
enum: ["commentOnBlock"]
|
|
367
|
+
},
|
|
368
|
+
blockId: blockIdProperty,
|
|
369
|
+
quote: {
|
|
370
|
+
type: "string",
|
|
371
|
+
description: "Exact text within the block the comment is about."
|
|
372
|
+
},
|
|
373
|
+
comment: commentJsonSchema
|
|
374
|
+
},
|
|
375
|
+
required: [
|
|
376
|
+
"id",
|
|
377
|
+
"type",
|
|
378
|
+
"blockId",
|
|
379
|
+
"comment"
|
|
380
|
+
],
|
|
381
|
+
additionalProperties: false
|
|
382
|
+
},
|
|
383
|
+
{
|
|
384
|
+
type: "object",
|
|
385
|
+
description: "Insert a signature table next to the anchor block. Direct mode only (table insertion is not representable as a tracked change).",
|
|
386
|
+
properties: {
|
|
387
|
+
...operationMetaProperties,
|
|
388
|
+
type: {
|
|
389
|
+
type: "string",
|
|
390
|
+
enum: ["insertSignatureTable"]
|
|
391
|
+
},
|
|
392
|
+
blockId: blockIdProperty,
|
|
393
|
+
position: {
|
|
394
|
+
type: "string",
|
|
395
|
+
enum: ["after", "before"],
|
|
396
|
+
description: "Insert after the anchor block (default) or before it. Defaults to \"after\"."
|
|
397
|
+
},
|
|
398
|
+
parties: {
|
|
399
|
+
type: "array",
|
|
400
|
+
description: "The signing parties, one table cell per party.",
|
|
401
|
+
items: {
|
|
402
|
+
type: "object",
|
|
403
|
+
properties: {
|
|
404
|
+
name: {
|
|
405
|
+
type: "string",
|
|
406
|
+
description: "Party name (rendered bold)."
|
|
407
|
+
},
|
|
408
|
+
signatory: {
|
|
409
|
+
type: "string",
|
|
410
|
+
description: "Name of the person signing."
|
|
411
|
+
},
|
|
412
|
+
title: {
|
|
413
|
+
type: "string",
|
|
414
|
+
description: "Signatory title (rendered in italics)."
|
|
415
|
+
}
|
|
416
|
+
},
|
|
417
|
+
required: ["name"],
|
|
418
|
+
additionalProperties: false
|
|
419
|
+
}
|
|
420
|
+
},
|
|
421
|
+
comment: commentJsonSchema
|
|
422
|
+
},
|
|
423
|
+
required: [
|
|
424
|
+
"id",
|
|
425
|
+
"type",
|
|
426
|
+
"blockId",
|
|
427
|
+
"parties"
|
|
428
|
+
],
|
|
429
|
+
additionalProperties: false
|
|
430
|
+
}
|
|
431
|
+
]
|
|
432
|
+
};
|
|
433
|
+
/**
|
|
434
|
+
* JSON Schema (draft-07 compatible) for the versioned batch envelope accepted
|
|
435
|
+
* by `parseFolioDocumentOperationBatch`: `version` (always
|
|
436
|
+
* `FOLIO_DOCUMENT_OPERATION_CONTRACT_VERSION`), `operations`, and the
|
|
437
|
+
* optional `mode` / `atomic` / `dryRun` flags — the exact wire shape of
|
|
438
|
+
* `FolioDocumentOperationBatch`. Hand this to an LLM tool definition (or any
|
|
439
|
+
* JSON Schema consumer) instead of re-declaring the contract.
|
|
440
|
+
*/
|
|
441
|
+
const FOLIO_DOCUMENT_OPERATION_BATCH_JSON_SCHEMA = {
|
|
442
|
+
type: "object",
|
|
443
|
+
description: "A versioned batch of document operations. Operation ids must be unique within the batch (enforced by the parser, not expressible in JSON Schema).",
|
|
444
|
+
properties: {
|
|
445
|
+
version: {
|
|
446
|
+
type: "integer",
|
|
447
|
+
enum: [FOLIO_DOCUMENT_OPERATION_CONTRACT_VERSION],
|
|
448
|
+
description: "Document-operation contract version."
|
|
449
|
+
},
|
|
450
|
+
operations: {
|
|
451
|
+
type: "array",
|
|
452
|
+
description: "The operations to apply, in order.",
|
|
453
|
+
items: FOLIO_DOCUMENT_OPERATION_JSON_SCHEMA
|
|
454
|
+
},
|
|
455
|
+
mode: {
|
|
456
|
+
type: "string",
|
|
457
|
+
enum: FOLIO_DOCUMENT_OPERATION_MODES,
|
|
458
|
+
description: "How edits land: \"tracked-changes\" (default) proposes revisions for human review, \"direct\" applies immediately. `formatRange` and `insertSignatureTable` support \"direct\" only."
|
|
459
|
+
},
|
|
460
|
+
atomic: {
|
|
461
|
+
type: "boolean",
|
|
462
|
+
description: "Reject the whole batch when any operation would be skipped."
|
|
463
|
+
},
|
|
464
|
+
dryRun: {
|
|
465
|
+
type: "boolean",
|
|
466
|
+
description: "Preview the batch without mutating the document."
|
|
467
|
+
}
|
|
468
|
+
},
|
|
469
|
+
required: ["version", "operations"],
|
|
470
|
+
additionalProperties: false
|
|
471
|
+
};
|
|
472
|
+
/**
|
|
473
|
+
* Convert the parser's JSONPath-style location (`$.operations[0].blockId`)
|
|
474
|
+
* into a Standard Schema issue path (`["operations", 0, "blockId"]`).
|
|
475
|
+
* Returns `undefined` for the root path so root-level issues carry no path.
|
|
476
|
+
*/
|
|
477
|
+
const toStandardSchemaPath = (path) => {
|
|
478
|
+
const segments = [];
|
|
479
|
+
for (const match of path.matchAll(/\.(?<key>[^.[\]]+)|\[(?<index>\d+)\]/gu)) {
|
|
480
|
+
const { key, index } = match.groups ?? {};
|
|
481
|
+
if (key !== void 0) segments.push(key);
|
|
482
|
+
else if (index !== void 0) segments.push(Number(index));
|
|
483
|
+
}
|
|
484
|
+
return segments.length > 0 ? segments : void 0;
|
|
485
|
+
};
|
|
486
|
+
const toStandardSchemaIssue = (error) => {
|
|
487
|
+
if (error instanceof InvalidFolioDocumentOperationBatchError) {
|
|
488
|
+
const path = toStandardSchemaPath(error.path);
|
|
489
|
+
return {
|
|
490
|
+
message: error.message,
|
|
491
|
+
...path !== void 0 && { path }
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
if (error instanceof UnsupportedFolioDocumentOperationVersionError) return {
|
|
495
|
+
message: error.message,
|
|
496
|
+
path: ["version"]
|
|
497
|
+
};
|
|
498
|
+
return { message: error instanceof Error ? error.message : String(error) };
|
|
499
|
+
};
|
|
500
|
+
/**
|
|
501
|
+
* Standard Schema V1 (https://standardschema.dev) validator for a document
|
|
502
|
+
* operation batch, for spec-aware consumers (TanStack AI tool `inputSchema`,
|
|
503
|
+
* tRPC input, etc.). Delegates to `parseFolioDocumentOperationBatch` from
|
|
504
|
+
* `@stll/folio-core`, so the strict parser stays the single source of truth:
|
|
505
|
+
* `validate` never throws, returning `{ value }` with the parser's exact
|
|
506
|
+
* output on success and `{ issues }` (message plus a key path when the parser
|
|
507
|
+
* reported one) on failure. The matching LLM-facing JSON schema is attached
|
|
508
|
+
* as {@link FolioDocumentOperationBatchSchema.jsonSchema}.
|
|
509
|
+
*/
|
|
510
|
+
const folioDocumentOperationBatchSchema = {
|
|
511
|
+
"~standard": {
|
|
512
|
+
version: 1,
|
|
513
|
+
vendor: "folio",
|
|
514
|
+
validate: (value) => {
|
|
515
|
+
try {
|
|
516
|
+
return { value: parseFolioDocumentOperationBatch(value) };
|
|
517
|
+
} catch (error) {
|
|
518
|
+
return { issues: [toStandardSchemaIssue(error)] };
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
},
|
|
522
|
+
jsonSchema: FOLIO_DOCUMENT_OPERATION_BATCH_JSON_SCHEMA
|
|
523
|
+
};
|
|
524
|
+
//#endregion
|
|
525
|
+
export { FOLIO_DOCUMENT_OPERATION_BATCH_JSON_SCHEMA, FOLIO_DOCUMENT_OPERATION_JSON_SCHEMA, FOLIO_TEXT_RANGE_JSON_SCHEMA, folioDocumentOperationBatchSchema };
|
package/dist/tools.d.ts
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import { FolioAgentToolDefinition } from "./types.js";
|
|
2
|
+
import { FolioDocumentOperationType } from "@stll/folio-core/server";
|
|
2
3
|
|
|
3
4
|
//#region src/tools.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* The contract operation types `suggest_changes` exposes to the model,
|
|
7
|
+
* derived from the shared contract constant minus the exclusions above.
|
|
8
|
+
* Exported (module-level only, not from the package index) so
|
|
9
|
+
* `parse.test.ts` can assert this enum stays in lockstep with what
|
|
10
|
+
* `parseSuggestChangesInput` actually accepts.
|
|
11
|
+
*/
|
|
12
|
+
declare const SUGGEST_CHANGES_OPERATION_TYPES: readonly FolioDocumentOperationType[];
|
|
4
13
|
/**
|
|
5
14
|
* The tools this package exposes, described for an LLM. Every tool that reads
|
|
6
15
|
* or mutates the document expects `blockId` values that came from
|
|
@@ -13,4 +22,4 @@ declare const FOLIO_AGENT_TOOLS: FolioAgentToolDefinition[];
|
|
|
13
22
|
/** The tool definitions this package exposes. Same array as {@link FOLIO_AGENT_TOOLS}, as a function for symmetry with the other providers. */
|
|
14
23
|
declare const getFolioToolDefinitions: () => FolioAgentToolDefinition[];
|
|
15
24
|
//#endregion
|
|
16
|
-
export { FOLIO_AGENT_TOOLS, getFolioToolDefinitions };
|
|
25
|
+
export { FOLIO_AGENT_TOOLS, SUGGEST_CHANGES_OPERATION_TYPES, getFolioToolDefinitions };
|
package/dist/tools.js
CHANGED
|
@@ -1,4 +1,75 @@
|
|
|
1
1
|
import { FOLIO_AGENT_TOOL_NAMES } from "./types.js";
|
|
2
|
+
import { FOLIO_TEXT_RANGE_JSON_SCHEMA } from "./operation-schema.js";
|
|
3
|
+
import { FOLIO_DOCUMENT_OPERATION_TYPES } from "@stll/folio-core/server";
|
|
4
|
+
//#region src/tools.ts
|
|
5
|
+
/**
|
|
6
|
+
* `suggest_changes` deliberately narrows the full document-operation contract
|
|
7
|
+
* (see `FOLIO_DOCUMENT_OPERATION_JSON_SCHEMA` in `operation-schema.ts`):
|
|
8
|
+
* - excluded types: `formatRange` and `insertSignatureTable` (direct-only,
|
|
9
|
+
* not representable as tracked changes for human review) and
|
|
10
|
+
* `commentOnBlock` (covered by the dedicated `add_comment` tool);
|
|
11
|
+
* - `id` is optional here (auto-generated `op-1`, `op-2`, … by `parse.ts`)
|
|
12
|
+
* where the contract requires it;
|
|
13
|
+
* - `comment` is a plain string here; `parse.ts` wraps it into the contract's
|
|
14
|
+
* `{ text }` object;
|
|
15
|
+
* - the review metadata (`severity`, `area`), `precondition` guard, and the
|
|
16
|
+
* insert/replace formatting knobs (`inheritFormatting`, `pageBreakBefore`,
|
|
17
|
+
* `preserveFormatting`, `styleId`, `position`, `parties`, `quote`,
|
|
18
|
+
* `formatting`) are not exposed to the model.
|
|
19
|
+
*/
|
|
20
|
+
const SUGGEST_CHANGES_EXCLUDED_OPERATION_TYPES = /* @__PURE__ */ new Set([
|
|
21
|
+
"formatRange",
|
|
22
|
+
"commentOnBlock",
|
|
23
|
+
"insertSignatureTable"
|
|
24
|
+
]);
|
|
25
|
+
/**
|
|
26
|
+
* The contract operation types `suggest_changes` exposes to the model,
|
|
27
|
+
* derived from the shared contract constant minus the exclusions above.
|
|
28
|
+
* Exported (module-level only, not from the package index) so
|
|
29
|
+
* `parse.test.ts` can assert this enum stays in lockstep with what
|
|
30
|
+
* `parseSuggestChangesInput` actually accepts.
|
|
31
|
+
*/
|
|
32
|
+
const SUGGEST_CHANGES_OPERATION_TYPES = FOLIO_DOCUMENT_OPERATION_TYPES.filter((type) => !SUGGEST_CHANGES_EXCLUDED_OPERATION_TYPES.has(type));
|
|
33
|
+
const suggestChangesOperationSchema = {
|
|
34
|
+
type: "object",
|
|
35
|
+
properties: {
|
|
36
|
+
id: {
|
|
37
|
+
type: "string",
|
|
38
|
+
description: "Optional caller-supplied operation id, echoed back in `applied`/`skipped`. Auto-generated (op-1, op-2, …) when omitted."
|
|
39
|
+
},
|
|
40
|
+
type: {
|
|
41
|
+
type: "string",
|
|
42
|
+
enum: SUGGEST_CHANGES_OPERATION_TYPES,
|
|
43
|
+
description: "The kind of edit to make."
|
|
44
|
+
},
|
|
45
|
+
blockId: {
|
|
46
|
+
type: "string",
|
|
47
|
+
description: "The block to edit, from `read_document` or `find_text`."
|
|
48
|
+
},
|
|
49
|
+
range: {
|
|
50
|
+
...FOLIO_TEXT_RANGE_JSON_SCHEMA,
|
|
51
|
+
description: "Required for `replaceRange` and `commentOnRange`: copy the range object returned by `find_text`."
|
|
52
|
+
},
|
|
53
|
+
find: {
|
|
54
|
+
type: "string",
|
|
55
|
+
description: "Required for `replaceInBlock`: the exact text to find within the block, up to 100,000 characters."
|
|
56
|
+
},
|
|
57
|
+
replace: {
|
|
58
|
+
type: "string",
|
|
59
|
+
description: "Required for `replaceInBlock` and `replaceRange`: replacement text, up to 100,000 characters."
|
|
60
|
+
},
|
|
61
|
+
text: {
|
|
62
|
+
type: "string",
|
|
63
|
+
description: "Required for `insertAfterBlock` / `insertBeforeBlock` / `replaceBlock`: the text to insert or replace the block with, up to 100,000 characters."
|
|
64
|
+
},
|
|
65
|
+
comment: {
|
|
66
|
+
type: "string",
|
|
67
|
+
description: "Optional comment explaining this edit, attached to the affected text, up to 100,000 characters."
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
required: ["type"],
|
|
71
|
+
additionalProperties: false
|
|
72
|
+
};
|
|
2
73
|
/**
|
|
3
74
|
* The tools this package exposes, described for an LLM. Every tool that reads
|
|
4
75
|
* or mutates the document expects `blockId` values that came from
|
|
@@ -134,83 +205,7 @@ const FOLIO_AGENT_TOOLS = [
|
|
|
134
205
|
properties: { operations: {
|
|
135
206
|
type: "array",
|
|
136
207
|
description: "The edits to propose, applied in order. At most 50 per call.",
|
|
137
|
-
items:
|
|
138
|
-
type: "object",
|
|
139
|
-
properties: {
|
|
140
|
-
id: {
|
|
141
|
-
type: "string",
|
|
142
|
-
description: "Optional caller-supplied operation id, echoed back in `applied`/`skipped`. Auto-generated (op-1, op-2, …) when omitted."
|
|
143
|
-
},
|
|
144
|
-
type: {
|
|
145
|
-
type: "string",
|
|
146
|
-
enum: [
|
|
147
|
-
"replaceInBlock",
|
|
148
|
-
"replaceRange",
|
|
149
|
-
"commentOnRange",
|
|
150
|
-
"insertAfterBlock",
|
|
151
|
-
"insertBeforeBlock",
|
|
152
|
-
"replaceBlock",
|
|
153
|
-
"deleteBlock"
|
|
154
|
-
],
|
|
155
|
-
description: "The kind of edit to make."
|
|
156
|
-
},
|
|
157
|
-
blockId: {
|
|
158
|
-
type: "string",
|
|
159
|
-
description: "The block to edit, from `read_document` or `find_text`."
|
|
160
|
-
},
|
|
161
|
-
range: {
|
|
162
|
-
type: "object",
|
|
163
|
-
description: "Required for `replaceRange`: copy the range object returned by `find_text`.",
|
|
164
|
-
properties: {
|
|
165
|
-
type: {
|
|
166
|
-
type: "string",
|
|
167
|
-
enum: ["textRange"]
|
|
168
|
-
},
|
|
169
|
-
story: {
|
|
170
|
-
type: "string",
|
|
171
|
-
enum: ["main"]
|
|
172
|
-
},
|
|
173
|
-
blockId: { type: "string" },
|
|
174
|
-
startOffset: {
|
|
175
|
-
type: "integer",
|
|
176
|
-
minimum: 0
|
|
177
|
-
},
|
|
178
|
-
endOffset: {
|
|
179
|
-
type: "integer",
|
|
180
|
-
minimum: 1
|
|
181
|
-
},
|
|
182
|
-
selectedTextHash: { type: "string" }
|
|
183
|
-
},
|
|
184
|
-
required: [
|
|
185
|
-
"type",
|
|
186
|
-
"story",
|
|
187
|
-
"blockId",
|
|
188
|
-
"startOffset",
|
|
189
|
-
"endOffset",
|
|
190
|
-
"selectedTextHash"
|
|
191
|
-
],
|
|
192
|
-
additionalProperties: false
|
|
193
|
-
},
|
|
194
|
-
find: {
|
|
195
|
-
type: "string",
|
|
196
|
-
description: "Required for `replaceInBlock`: the exact text to find within the block, up to 100,000 characters."
|
|
197
|
-
},
|
|
198
|
-
replace: {
|
|
199
|
-
type: "string",
|
|
200
|
-
description: "Required for `replaceInBlock` and `replaceRange`: replacement text, up to 100,000 characters."
|
|
201
|
-
},
|
|
202
|
-
text: {
|
|
203
|
-
type: "string",
|
|
204
|
-
description: "Required for `insertAfterBlock` / `insertBeforeBlock` / `replaceBlock`: the text to insert or replace the block with, up to 100,000 characters."
|
|
205
|
-
},
|
|
206
|
-
comment: {
|
|
207
|
-
type: "string",
|
|
208
|
-
description: "Optional comment explaining this edit, attached to the affected text, up to 100,000 characters."
|
|
209
|
-
}
|
|
210
|
-
},
|
|
211
|
-
required: ["type"],
|
|
212
|
-
additionalProperties: false
|
|
213
|
-
}
|
|
208
|
+
items: suggestChangesOperationSchema
|
|
214
209
|
} },
|
|
215
210
|
required: ["operations"],
|
|
216
211
|
additionalProperties: false
|
|
@@ -294,4 +289,4 @@ const FOLIO_AGENT_TOOLS = [
|
|
|
294
289
|
/** The tool definitions this package exposes. Same array as {@link FOLIO_AGENT_TOOLS}, as a function for symmetry with the other providers. */
|
|
295
290
|
const getFolioToolDefinitions = () => FOLIO_AGENT_TOOLS;
|
|
296
291
|
//#endregion
|
|
297
|
-
export { FOLIO_AGENT_TOOLS, getFolioToolDefinitions };
|
|
292
|
+
export { FOLIO_AGENT_TOOLS, SUGGEST_CHANGES_OPERATION_TYPES, getFolioToolDefinitions };
|