-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathschemas.ts
More file actions
7242 lines (6899 loc) · 230 KB
/
schemas.ts
File metadata and controls
7242 lines (6899 loc) · 230 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { COMMAND_CATALOG } from './command-catalog.js';
import { CONTRACT_VERSION, JSON_SCHEMA_DIALECT, OPERATION_IDS, type OperationId } from './types.js';
import { NODE_TYPES, BLOCK_NODE_TYPES, DELETABLE_BLOCK_NODE_TYPES, INLINE_NODE_TYPES } from '../types/base.js';
import { SELECTION_EDGE_NODE_TYPES } from '../types/address.js';
import { INLINE_PROPERTY_REGISTRY, buildInlineRunPatchSchema } from '../format/inline-run-patch.js';
import { INLINE_DIRECTIVES } from '../types/style-policy.types.js';
import {
PARAGRAPH_ALIGNMENTS,
TAB_STOP_ALIGNMENTS,
TAB_STOP_LEADERS,
BORDER_SIDES,
CLEAR_BORDER_SIDES,
LINE_RULES,
} from '../paragraphs/paragraphs.js';
import { buildPatchSchema, buildStateSchema } from '../styles/index.js';
import { Z_ORDER_RELATIVE_HEIGHT_MAX, Z_ORDER_RELATIVE_HEIGHT_MIN } from '../images/z-order.js';
type JsonSchema = Record<string, unknown>;
/** JSON Schema descriptors for a single operation's input, output, and result variants. */
export interface OperationSchemaSet {
/** Schema describing the operation's accepted input payload. */
input: JsonSchema;
/** Schema describing the full output (success | failure union for mutations). */
output: JsonSchema;
/** Schema describing only the success branch of a mutation result. */
success?: JsonSchema;
/** Schema describing only the failure branch of a mutation result. */
failure?: JsonSchema;
}
/** Top-level contract envelope containing versioned operation schemas. */
export interface InternalContractSchemas {
/** JSON Schema dialect URI (e.g. `https://json-schema.org/draft/2020-12/schema`). */
$schema: string;
/** Semantic version of the document-api contract these schemas describe. */
contractVersion: string;
/** Shared schema definitions referenced by `$ref` in operation schemas. */
$defs?: Record<string, JsonSchema>;
/** Per-operation schema sets keyed by {@link OperationId}. */
operations: Record<OperationId, OperationSchemaSet>;
}
function objectSchema(properties: Record<string, JsonSchema>, required: readonly string[] = []): JsonSchema {
const schema: JsonSchema = {
type: 'object',
properties,
additionalProperties: false,
};
if (required.length > 0) {
schema.required = [...required];
}
return schema;
}
function arraySchema(items: JsonSchema): JsonSchema {
return {
type: 'array',
items,
};
}
/** Returns a `{ $ref: '#/$defs/<name>' }` pointer for use in operation schemas. */
function ref(name: string): JsonSchema {
return { $ref: `#/$defs/${name}` };
}
/**
* Builds a `oneOf` schema that merges each TargetLocator branch with additional
* payload properties. This avoids the `allOf` + `additionalProperties: false`
* conflict where each branch would reject keys defined in the other schema.
*/
function targetLocatorWithPayload(
payloadProperties: Record<string, JsonSchema>,
payloadRequired: readonly string[] = [],
): JsonSchema {
return {
oneOf: [
objectSchema(
{
target: {
...ref('SelectionTarget'),
description:
"Selection target: {kind:'selection', start:{kind:'text', blockId, offset}, end:{kind:'text', blockId, offset}}. Use 'ref' instead when you have a search result handle.",
},
...payloadProperties,
},
['target', ...payloadRequired],
),
objectSchema(
{
ref: {
type: 'string',
description:
"Handle ref string from a superdoc_search result. Pass the handle.ref value directly (e.g. 'text:eyJ...'). Preferred over 'target' for inline formatting.",
},
...payloadProperties,
},
['ref', ...payloadRequired],
),
],
};
}
/**
* Like {@link targetLocatorWithPayload}, but also allows an untargeted branch
* where neither `target` nor `ref` is present.
*/
function optionalTargetLocatorWithPayload(
payloadProperties: Record<string, JsonSchema>,
payloadRequired: readonly string[] = [],
): JsonSchema {
return {
oneOf: [
objectSchema(
{
target: {
...ref('SelectionTarget'),
description:
"Selection target: {kind:'selection', start:{kind:'text', blockId, offset}, end:{kind:'text', blockId, offset}}.",
},
...payloadProperties,
},
['target', ...payloadRequired],
),
objectSchema(
{
ref: {
type: 'string',
description:
'Handle ref from superdoc_search result (pass handle.ref value directly). Preferred over building a target object.',
},
...payloadProperties,
},
['ref', ...payloadRequired],
),
objectSchema({ ...payloadProperties }, [...payloadRequired]),
],
};
}
/** Shared output/success/failure shape for ImagesMutationResult operations. */
function imagesMutationSchemaSet(inputSchema: JsonSchema): OperationSchemaSet {
return {
input: inputSchema,
output: objectSchema({ success: { type: 'boolean' }, image: { type: 'object' }, failure: { type: 'object' } }),
success: objectSchema({ success: { const: true }, image: { type: 'object' } }, ['success', 'image']),
failure: objectSchema(
{
success: { const: false },
failure: objectSchema({ code: { type: 'string' }, message: { type: 'string' } }, ['code', 'message']),
},
['success', 'failure'],
),
};
}
const nodeTypeValues = NODE_TYPES;
const blockNodeTypeValues = BLOCK_NODE_TYPES;
const deletableBlockNodeTypeValues = DELETABLE_BLOCK_NODE_TYPES;
const inlineNodeTypeValues = INLINE_NODE_TYPES;
// ---------------------------------------------------------------------------
// Shared $defs — canonical schema definitions referenced via ref()
// ---------------------------------------------------------------------------
const knownTargetKindValues = [
'text',
'node',
'list',
'comment',
'trackedChange',
'table',
'tableCell',
'tableOfContents',
'section',
'sdt',
'field',
] as const;
/**
* Shared schema definitions referenced by `$ref` in operation schemas.
*
* Within entries, cross-references use `ref()` so that the entire $defs
* graph is self-consistent.
*/
const SHARED_DEFS: Record<string, JsonSchema> = {
// -- Primitives --
Range: objectSchema(
{
start: { type: 'integer' },
end: { type: 'integer' },
},
['start', 'end'],
),
Position: objectSchema(
{
blockId: { type: 'string' },
offset: { type: 'integer' },
},
['blockId', 'offset'],
),
InlineAnchor: objectSchema(
{
start: ref('Position'),
end: ref('Position'),
},
['start', 'end'],
),
TargetKind: {
anyOf: [{ enum: [...knownTargetKindValues] }, { type: 'string', pattern: '^ext:.+$' }],
},
// -- Address types --
TextAddress: objectSchema(
{
kind: { const: 'text' },
blockId: { type: 'string' },
range: ref('Range'),
},
['kind', 'blockId', 'range'],
),
TextSegment: objectSchema(
{
blockId: { type: 'string' },
range: ref('Range'),
},
['blockId', 'range'],
),
TextTarget: objectSchema(
{
kind: { const: 'text' },
segments: { type: 'array', items: ref('TextSegment'), minItems: 1 },
},
['kind', 'segments'],
),
// -- Selection-based targeting --
SelectionEdgeNodeAddress: objectSchema(
{
kind: { const: 'block' },
nodeType: { enum: [...SELECTION_EDGE_NODE_TYPES] },
nodeId: { type: 'string' },
},
['kind', 'nodeType', 'nodeId'],
),
SelectionPoint: {
description:
"A point in the document. Use {kind:'text', blockId, offset} for character positions or {kind:'nodeEdge', node:{kind:'block', nodeType, nodeId}, edge:'before'|'after'} for block boundaries.",
oneOf: [
objectSchema({ kind: { const: 'text' }, blockId: { type: 'string' }, offset: { type: 'integer', minimum: 0 } }, [
'kind',
'blockId',
'offset',
]),
objectSchema(
{
kind: { const: 'nodeEdge' },
node: ref('SelectionEdgeNodeAddress'),
edge: { enum: ['before', 'after'] },
},
['kind', 'node', 'edge'],
),
],
} satisfies JsonSchema,
SelectionTarget: objectSchema(
{
kind: { const: 'selection' },
start: ref('SelectionPoint'),
end: ref('SelectionPoint'),
},
['kind', 'start', 'end'],
),
TargetLocator: {
oneOf: [
objectSchema({ target: ref('SelectionTarget') }, ['target']),
objectSchema({ ref: { type: 'string' } }, ['ref']),
],
} satisfies JsonSchema,
DeleteBehavior: { enum: ['selection', 'exact'] } satisfies JsonSchema,
BlockNodeAddress: objectSchema(
{
kind: { const: 'block' },
nodeType: { enum: [...blockNodeTypeValues] },
nodeId: { type: 'string' },
},
['kind', 'nodeType', 'nodeId'],
),
DeletableBlockNodeAddress: objectSchema(
{
kind: { const: 'block' },
nodeType: { enum: [...deletableBlockNodeTypeValues] },
nodeId: { type: 'string' },
},
['kind', 'nodeType', 'nodeId'],
),
TableAddress: objectSchema(
{
kind: { const: 'block' },
nodeType: { const: 'table' },
nodeId: { type: 'string' },
},
['kind', 'nodeType', 'nodeId'],
),
TableRowAddress: objectSchema(
{
kind: { const: 'block' },
nodeType: { const: 'tableRow' },
nodeId: { type: 'string' },
},
['kind', 'nodeType', 'nodeId'],
),
TableCellAddress: objectSchema(
{
kind: { const: 'block' },
nodeType: { const: 'tableCell' },
nodeId: { type: 'string' },
},
['kind', 'nodeType', 'nodeId'],
),
TableOrRowAddress: objectSchema(
{
kind: { const: 'block' },
nodeType: { enum: ['table', 'tableRow'] },
nodeId: { type: 'string' },
},
['kind', 'nodeType', 'nodeId'],
),
TableOrCellAddress: objectSchema(
{
kind: { const: 'block' },
nodeType: { enum: ['table', 'tableCell'] },
nodeId: { type: 'string' },
},
['kind', 'nodeType', 'nodeId'],
),
ParagraphAddress: objectSchema(
{
kind: { const: 'block' },
nodeType: { const: 'paragraph' },
nodeId: { type: 'string' },
},
['kind', 'nodeType', 'nodeId'],
),
HeadingAddress: objectSchema(
{
kind: { const: 'block' },
nodeType: { const: 'heading' },
nodeId: { type: 'string' },
},
['kind', 'nodeType', 'nodeId'],
),
ListItemAddress: objectSchema(
{
kind: { const: 'block' },
nodeType: { const: 'listItem' },
nodeId: { type: 'string' },
},
['kind', 'nodeType', 'nodeId'],
),
SectionAddress: objectSchema(
{
kind: { const: 'section' },
sectionId: { type: 'string' },
},
['kind', 'sectionId'],
),
InlineNodeAddress: objectSchema(
{
kind: { const: 'inline' },
nodeType: { enum: [...inlineNodeTypeValues] },
anchor: ref('InlineAnchor'),
},
['kind', 'nodeType', 'anchor'],
),
NodeAddress: {
oneOf: [ref('BlockNodeAddress'), ref('InlineNodeAddress')],
},
CommentAddress: objectSchema(
{
kind: { const: 'entity' },
entityType: { const: 'comment' },
entityId: { type: 'string' },
story: ref('StoryLocator'),
},
['kind', 'entityType', 'entityId'],
),
TrackedChangeAddress: objectSchema(
{
kind: { const: 'entity' },
entityType: { const: 'trackedChange' },
entityId: { type: 'string' },
story: ref('StoryLocator'),
},
['kind', 'entityType', 'entityId'],
),
EntityAddress: {
oneOf: [ref('CommentAddress'), ref('TrackedChangeAddress')],
},
// -- Discovery components --
ResolvedHandle: objectSchema(
{
ref: { type: 'string' },
refStability: { enum: ['stable', 'ephemeral'] },
targetKind: ref('TargetKind'),
},
['ref', 'refStability', 'targetKind'],
),
PageInfo: objectSchema(
{
limit: { type: 'integer', minimum: 0 },
offset: { type: 'integer', minimum: 0 },
returned: { type: 'integer', minimum: 0 },
},
['limit', 'offset', 'returned'],
),
// -- Receipt scaffolds --
ReceiptSuccess: objectSchema(
{
success: { const: true },
inserted: arraySchema(ref('EntityAddress')),
updated: arraySchema(ref('EntityAddress')),
removed: arraySchema(ref('EntityAddress')),
},
['success'],
),
ReceiptFailure: objectSchema(
{
code: { type: 'string' },
message: { type: 'string' },
details: {},
},
['code', 'message'],
),
TextMutationRange: objectSchema(
{
from: { type: 'integer' },
to: { type: 'integer' },
},
['from', 'to'],
),
TextMutationResolution: objectSchema(
{
requestedTarget: ref('TextAddress'),
target: ref('TextAddress'),
range: ref('TextMutationRange'),
text: { type: 'string' },
selectionTarget: ref('SelectionTarget'),
},
['target', 'range', 'text'],
),
TextMutationSuccess: objectSchema(
{
success: { const: true },
resolution: ref('TextMutationResolution'),
inserted: arraySchema(ref('EntityAddress')),
updated: arraySchema(ref('EntityAddress')),
removed: arraySchema(ref('EntityAddress')),
},
['success', 'resolution'],
),
// -- Match fragments (query.match) --
MatchRun: objectSchema(
{
range: ref('Range'),
text: { type: 'string' },
styleId: { type: 'string' },
styles: objectSchema(
{
direct: objectSchema(
{
bold: { enum: [...INLINE_DIRECTIVES] },
italic: { enum: [...INLINE_DIRECTIVES] },
underline: { enum: [...INLINE_DIRECTIVES] },
strike: { enum: [...INLINE_DIRECTIVES] },
},
['bold', 'italic', 'underline', 'strike'],
),
effective: objectSchema(
{
bold: { type: 'boolean' },
italic: { type: 'boolean' },
underline: { type: 'boolean' },
strike: { type: 'boolean' },
},
['bold', 'italic', 'underline', 'strike'],
),
color: { type: 'string' },
highlight: { type: 'string' },
fontFamily: { type: 'string' },
fontSizePt: { type: 'number' },
},
['direct', 'effective'],
),
ref: { type: 'string' },
},
['range', 'text', 'styles', 'ref'],
),
MatchBlock: objectSchema(
{
blockId: { type: 'string' },
nodeType: { type: 'string' },
range: ref('Range'),
text: { type: 'string' },
paragraphStyle: objectSchema({
styleId: { type: 'string' },
isListItem: { type: 'boolean' },
listLevel: { type: 'integer', minimum: 0 },
}),
ref: { type: 'string' },
runs: arraySchema(ref('MatchRun')),
},
['blockId', 'nodeType', 'range', 'text', 'ref', 'runs'],
),
// -- Block-level address types (lists) --
BlockAddress: objectSchema(
{
kind: { const: 'block' },
nodeType: { const: 'paragraph' },
nodeId: { type: 'string' },
},
['kind', 'nodeType', 'nodeId'],
),
BlockRange: objectSchema(
{
from: ref('BlockAddress'),
to: ref('BlockAddress'),
},
['from', 'to'],
),
BlockAddressOrRange: {
oneOf: [ref('BlockAddress'), ref('BlockRange')],
},
// -- Story locator (discriminated union on storyType) --
StoryLocator: {
description:
"Story scope. Defaults to document body when omitted. Use {kind:'story', storyType:'body'} for body, or other storyType values for headers, footers, footnotes, endnotes.",
oneOf: [
objectSchema({ kind: { const: 'story' }, storyType: { const: 'body' } }, ['kind', 'storyType']),
objectSchema(
{
kind: { const: 'story' },
storyType: { const: 'headerFooterSlot' },
section: ref('SectionAddress'),
headerFooterKind: { enum: ['header', 'footer'] },
variant: { enum: ['default', 'first', 'even'] },
resolution: { enum: ['effective', 'explicit'] },
onWrite: { enum: ['materializeIfInherited', 'editResolvedPart', 'error'] },
},
['kind', 'storyType', 'section', 'headerFooterKind', 'variant'],
),
objectSchema(
{
kind: { const: 'story' },
storyType: { const: 'headerFooterPart' },
refId: { type: 'string' },
},
['kind', 'storyType', 'refId'],
),
objectSchema(
{
kind: { const: 'story' },
storyType: { const: 'footnote' },
noteId: { type: 'string' },
},
['kind', 'storyType', 'noteId'],
),
objectSchema(
{
kind: { const: 'story' },
storyType: { const: 'endnote' },
noteId: { type: 'string' },
},
['kind', 'storyType', 'noteId'],
),
],
} satisfies JsonSchema,
};
// ---------------------------------------------------------------------------
// Module-level aliases using $ref pointers
// ---------------------------------------------------------------------------
const rangeSchema = ref('Range');
const positionSchema = ref('Position');
const inlineAnchorSchema = ref('InlineAnchor');
const targetKindSchema = ref('TargetKind');
const textAddressSchema = ref('TextAddress');
const textTargetSchema = ref('TextTarget');
const blockNodeAddressSchema = ref('BlockNodeAddress');
const deletableBlockNodeAddressSchema = ref('DeletableBlockNodeAddress');
const tableAddressSchema = ref('TableAddress');
const tableRowAddressSchema = ref('TableRowAddress');
const tableCellAddressSchema = ref('TableCellAddress');
const tableOrCellAddressSchema = ref('TableOrCellAddress');
const paragraphAddressSchema = ref('ParagraphAddress');
const headingAddressSchema = ref('HeadingAddress');
const listItemAddressSchema = ref('ListItemAddress');
const paragraphTargetSchema: JsonSchema = {
oneOf: [paragraphAddressSchema, headingAddressSchema, listItemAddressSchema],
};
const sectionAddressSchema = ref('SectionAddress');
const inlineNodeAddressSchema = ref('InlineNodeAddress');
const nodeAddressSchema = ref('NodeAddress');
const commentAddressSchema = ref('CommentAddress');
const trackedChangeAddressSchema = ref('TrackedChangeAddress');
const entityAddressSchema = ref('EntityAddress');
const selectionTargetSchema = ref('SelectionTarget');
const targetLocatorSchema = ref('TargetLocator');
const deleteBehaviorSchema = ref('DeleteBehavior');
const resolvedHandleSchema = ref('ResolvedHandle');
const pageInfoSchema = ref('PageInfo');
const receiptSuccessSchema = ref('ReceiptSuccess');
const textMutationRangeSchema = ref('TextMutationRange');
const textMutationResolutionSchema = ref('TextMutationResolution');
const textMutationSuccessSchema = ref('TextMutationSuccess');
const matchRunSchema = ref('MatchRun');
const matchBlockSchema = ref('MatchBlock');
const storyLocatorSchema = ref('StoryLocator');
// Keep these aliases for internal readability
void positionSchema;
void inlineAnchorSchema;
void targetKindSchema;
void inlineNodeAddressSchema;
void textMutationRangeSchema;
void entityAddressSchema;
void matchRunSchema;
// ---------------------------------------------------------------------------
// Discovery envelope schemas (C0)
// ---------------------------------------------------------------------------
/**
* Builds a DiscoveryResult schema wrapping the given item schema.
* When `metaSchema` is provided, a required `meta` field is added to the envelope.
*/
function discoveryResultSchema(itemSchema: JsonSchema, metaSchema?: JsonSchema): JsonSchema {
const properties: Record<string, JsonSchema> = {
evaluatedRevision: { type: 'string' },
total: { type: 'integer', minimum: 0 },
items: arraySchema(itemSchema),
page: pageInfoSchema,
};
const required = ['evaluatedRevision', 'total', 'items', 'page'];
if (metaSchema) {
properties.meta = metaSchema;
required.push('meta');
}
return objectSchema(properties, required);
}
/**
* Wraps domain-specific properties into a DiscoveryItem schema
* (adds `id` and `handle` fields).
*/
function discoveryItemSchema(
domainProperties: Record<string, JsonSchema>,
domainRequired: readonly string[] = [],
): JsonSchema {
return objectSchema(
{
id: { type: 'string' },
handle: resolvedHandleSchema,
...domainProperties,
},
['id', 'handle', ...domainRequired],
);
}
function possibleFailureCodes(operationId: OperationId): string[] {
return [...COMMAND_CATALOG[operationId].possibleFailureCodes];
}
function preApplyThrowCodes(operationId: OperationId): string[] {
return [...COMMAND_CATALOG[operationId].throws.preApply];
}
function receiptFailureSchemaFor(operationId: OperationId): JsonSchema {
const codes = possibleFailureCodes(operationId);
if (codes.length === 0) {
throw new Error(`Operation "${operationId}" does not declare non-applied failure codes.`);
}
return objectSchema(
{
code: {
enum: codes,
},
message: { type: 'string' },
details: {},
},
['code', 'message'],
);
}
function preApplyFailureSchemaFor(operationId: OperationId): JsonSchema {
const codes = preApplyThrowCodes(operationId);
if (codes.length === 0) {
throw new Error(`Operation "${operationId}" does not declare pre-apply throw codes.`);
}
return objectSchema(
{
code: {
enum: codes,
},
message: { type: 'string' },
details: {},
},
['code', 'message'],
);
}
function receiptFailureResultSchemaFor(operationId: OperationId): JsonSchema {
return objectSchema(
{
success: { const: false },
failure: receiptFailureSchemaFor(operationId),
},
['success', 'failure'],
);
}
function preApplyFailureResultSchemaFor(operationId: OperationId): JsonSchema {
return objectSchema(
{
success: { const: false },
failure: preApplyFailureSchemaFor(operationId),
},
['success', 'failure'],
);
}
function receiptResultSchemaFor(operationId: OperationId): JsonSchema {
return {
oneOf: [receiptSuccessSchema, receiptFailureResultSchemaFor(operationId)],
};
}
function textMutationFailureSchemaFor(operationId: OperationId): JsonSchema {
return objectSchema(
{
success: { const: false },
failure: receiptFailureSchemaFor(operationId),
resolution: textMutationResolutionSchema,
},
['success', 'failure', 'resolution'],
);
}
function textMutationResultSchemaFor(operationId: OperationId): JsonSchema {
return {
oneOf: [textMutationSuccessSchema, textMutationFailureSchemaFor(operationId)],
};
}
const trackChangeRefSchema = trackedChangeAddressSchema;
const createParagraphSuccessSchema = objectSchema(
{
success: { const: true },
paragraph: paragraphAddressSchema,
insertionPoint: textAddressSchema,
trackedChangeRefs: arraySchema(trackChangeRefSchema),
ref: {
type: 'string',
description:
'Ref handle for the created block. Pass directly to superdoc_format or superdoc_edit ref param without searching.',
},
},
['success', 'paragraph', 'insertionPoint'],
);
function createParagraphFailureSchemaFor(operationId: OperationId): JsonSchema {
return objectSchema(
{
success: { const: false },
failure: receiptFailureSchemaFor(operationId),
},
['success', 'failure'],
);
}
function createParagraphResultSchemaFor(operationId: OperationId): JsonSchema {
return {
oneOf: [createParagraphSuccessSchema, createParagraphFailureSchemaFor(operationId)],
};
}
const createHeadingSuccessSchema = objectSchema(
{
success: { const: true },
heading: headingAddressSchema,
insertionPoint: textAddressSchema,
trackedChangeRefs: arraySchema(trackChangeRefSchema),
ref: {
type: 'string',
description:
'Ref handle for the created block. Pass directly to superdoc_format or superdoc_edit ref param without searching.',
},
},
['success', 'heading', 'insertionPoint'],
);
function createHeadingFailureSchemaFor(operationId: OperationId): JsonSchema {
return objectSchema(
{
success: { const: false },
failure: receiptFailureSchemaFor(operationId),
},
['success', 'failure'],
);
}
function createHeadingResultSchemaFor(operationId: OperationId): JsonSchema {
return {
oneOf: [createHeadingSuccessSchema, createHeadingFailureSchemaFor(operationId)],
};
}
const headingLevelSchema: JsonSchema = { type: 'integer', minimum: 1, maximum: 6 };
const listsInsertSuccessSchema = objectSchema(
{
success: { const: true },
item: listItemAddressSchema,
insertionPoint: textAddressSchema,
trackedChangeRefs: arraySchema(trackChangeRefSchema),
},
['success', 'item', 'insertionPoint'],
);
const listsMutateItemSuccessSchema = objectSchema(
{
success: { const: true },
item: listItemAddressSchema,
},
['success', 'item'],
);
const listsExitSuccessSchema = objectSchema(
{
success: { const: true },
paragraph: paragraphAddressSchema,
},
['success', 'paragraph'],
);
function listsFailureSchemaFor(operationId: OperationId): JsonSchema {
return objectSchema(
{
success: { const: false },
failure: receiptFailureSchemaFor(operationId),
},
['success', 'failure'],
);
}
function listsInsertResultSchemaFor(operationId: OperationId): JsonSchema {
return {
oneOf: [listsInsertSuccessSchema, listsFailureSchemaFor(operationId)],
};
}
function listsMutateItemResultSchemaFor(operationId: OperationId): JsonSchema {
return {
oneOf: [listsMutateItemSuccessSchema, listsFailureSchemaFor(operationId)],
};
}
function _listsExitResultSchemaFor(operationId: OperationId): JsonSchema {
return {
oneOf: [listsExitSuccessSchema, listsFailureSchemaFor(operationId)],
};
}
const nodeSummarySchema = objectSchema({
label: { type: 'string' },
text: { type: 'string' },
});
const nodeInfoSchema: JsonSchema = {
type: 'object',
required: ['nodeType', 'kind'],
properties: {
nodeType: { enum: [...nodeTypeValues] },
kind: { enum: ['block', 'inline'] },
summary: nodeSummarySchema,
text: { type: 'string' },
nodes: arraySchema({ type: 'object' }),
properties: { type: 'object' },
bodyText: { type: 'string' },
bodyNodes: arraySchema({ type: 'object' }),
},
additionalProperties: false,
};
const matchContextSchema = objectSchema(
{
address: nodeAddressSchema,
snippet: { type: 'string' },
highlightRange: rangeSchema,
textRanges: arraySchema(textAddressSchema),
target: selectionTargetSchema,
},
['address', 'snippet', 'highlightRange'],
);
const unknownNodeDiagnosticSchema = objectSchema(
{
message: { type: 'string' },
address: nodeAddressSchema,
hint: { type: 'string' },
},
['message'],
);
const textSelectorSchema = objectSchema(
{
type: { const: 'text', description: "Must be 'text' for text pattern search." },
pattern: { type: 'string', description: 'Text or regex pattern to match.' },
mode: { enum: ['contains', 'regex'], description: "Match mode: 'contains' (substring) or 'regex'." },
caseSensitive: { type: 'boolean', description: 'Case-sensitive matching. Default: false.' },
},
['type', 'pattern'],
);
const nodeSelectorSchema = objectSchema(
{
type: { const: 'node', description: "Must be 'node' for node type search." },
nodeType: {
enum: [...nodeTypeValues],
description: 'Block type to match (paragraph, heading, table, listItem, etc.).',
},
kind: { enum: ['block', 'inline'], description: "Filter: 'block' or 'inline'." },
},
['type'],
);
const selectorShorthandSchema = objectSchema(
{
nodeType: { enum: [...nodeTypeValues] },
},
['nodeType'],
);
const selectSchema: JsonSchema = {
anyOf: [textSelectorSchema, nodeSelectorSchema, selectorShorthandSchema],
};
// -- SDFindInput / SDFindResult schemas (SDM/1) --
const sdTextSelectorSchema = objectSchema(
{
type: { const: 'text' },
pattern: { type: 'string' },
mode: { enum: ['contains', 'regex'] },
caseSensitive: { type: 'boolean' },
},
['type', 'pattern'],
);
const sdNodeSelectorSchema = objectSchema(
{
type: { const: 'node' },
kind: { enum: ['block', 'inline'] },
nodeType: { type: 'string' },
},
['type'],
);
const sdSelectorSchema: JsonSchema = {
oneOf: [sdTextSelectorSchema, sdNodeSelectorSchema],
};
// sdAddressSchema removed — replaced by blockNodeAddressSchema, nodeAddressSchema, textAddressSchema
const sdReadOptionsSchema = objectSchema({
includeResolved: { type: 'boolean' },
includeProvenance: { type: 'boolean' },
includeContext: { type: 'boolean' },
});
const sdFindInputSchema = objectSchema(
{
in: storyLocatorSchema,
select: sdSelectorSchema,
within: blockNodeAddressSchema,
limit: { type: 'integer' },
offset: { type: 'integer' },
options: sdReadOptionsSchema,
},