@superdoc-dev/sdk 1.19.2 → 1.20.1

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.
Files changed (103) hide show
  1. package/dist/action-primitives/doc-index.cjs +215 -0
  2. package/dist/action-primitives/doc-index.d.ts +83 -0
  3. package/dist/action-primitives/doc-index.d.ts.map +1 -0
  4. package/dist/action-primitives/doc-index.js +211 -0
  5. package/dist/action-primitives/engine.cjs +204 -0
  6. package/dist/action-primitives/engine.d.ts +71 -0
  7. package/dist/action-primitives/engine.d.ts.map +1 -0
  8. package/dist/action-primitives/engine.js +196 -0
  9. package/dist/action-primitives/receipt.cjs +39 -0
  10. package/dist/action-primitives/receipt.d.ts +49 -0
  11. package/dist/action-primitives/receipt.d.ts.map +1 -0
  12. package/dist/action-primitives/receipt.js +32 -0
  13. package/dist/action-primitives/resolve.cjs +252 -0
  14. package/dist/action-primitives/resolve.d.ts +56 -0
  15. package/dist/action-primitives/resolve.d.ts.map +1 -0
  16. package/dist/action-primitives/resolve.js +246 -0
  17. package/dist/action-primitives/session-cache.cjs +43 -0
  18. package/dist/action-primitives/session-cache.d.ts +19 -0
  19. package/dist/action-primitives/session-cache.d.ts.map +1 -0
  20. package/dist/action-primitives/session-cache.js +37 -0
  21. package/dist/action-primitives/tools/list-transform.cjs +661 -0
  22. package/dist/action-primitives/tools/list-transform.d.ts +98 -0
  23. package/dist/action-primitives/tools/list-transform.d.ts.map +1 -0
  24. package/dist/action-primitives/tools/list-transform.js +656 -0
  25. package/dist/action-primitives/tools/structure-insert.cjs +1343 -0
  26. package/dist/action-primitives/tools/structure-insert.d.ts +183 -0
  27. package/dist/action-primitives/tools/structure-insert.d.ts.map +1 -0
  28. package/dist/action-primitives/tools/structure-insert.js +1338 -0
  29. package/dist/action-primitives/tools/text-transform.cjs +669 -0
  30. package/dist/action-primitives/tools/text-transform.d.ts +64 -0
  31. package/dist/action-primitives/tools/text-transform.d.ts.map +1 -0
  32. package/dist/action-primitives/tools/text-transform.js +664 -0
  33. package/dist/action-primitives/types.d.ts +36 -0
  34. package/dist/action-primitives/types.d.ts.map +1 -0
  35. package/dist/action-primitives/types.js +15 -0
  36. package/dist/agent/actions.cjs +5381 -0
  37. package/dist/agent/actions.d.ts +404 -0
  38. package/dist/agent/actions.d.ts.map +1 -0
  39. package/dist/agent/actions.js +5373 -0
  40. package/dist/agent/catalog.cjs +483 -0
  41. package/dist/agent/catalog.d.ts +103 -0
  42. package/dist/agent/catalog.d.ts.map +1 -0
  43. package/dist/agent/catalog.js +471 -0
  44. package/dist/agent/doc-snapshot.cjs +663 -0
  45. package/dist/agent/doc-snapshot.d.ts +247 -0
  46. package/dist/agent/doc-snapshot.d.ts.map +1 -0
  47. package/dist/agent/doc-snapshot.js +657 -0
  48. package/dist/agent/index.d.ts +16 -0
  49. package/dist/agent/index.d.ts.map +1 -0
  50. package/dist/agent/index.js +15 -0
  51. package/dist/agent/ir.cjs +170 -0
  52. package/dist/agent/ir.d.ts +216 -0
  53. package/dist/agent/ir.d.ts.map +1 -0
  54. package/dist/agent/ir.js +181 -0
  55. package/dist/agent/operation-catalog.cjs +418 -0
  56. package/dist/agent/operation-catalog.d.ts +36 -0
  57. package/dist/agent/operation-catalog.d.ts.map +1 -0
  58. package/dist/agent/operation-catalog.js +446 -0
  59. package/dist/agent/runtime.cjs +501 -0
  60. package/dist/agent/runtime.d.ts +120 -0
  61. package/dist/agent/runtime.d.ts.map +1 -0
  62. package/dist/agent/runtime.js +493 -0
  63. package/dist/embedded-prompts.generated.cjs +13 -0
  64. package/dist/embedded-prompts.generated.d.ts +4 -0
  65. package/dist/embedded-prompts.generated.d.ts.map +1 -0
  66. package/dist/embedded-prompts.generated.js +9 -0
  67. package/dist/generated/client.cjs +18 -0
  68. package/dist/generated/client.d.ts +134 -0
  69. package/dist/generated/client.d.ts.map +1 -1
  70. package/dist/generated/client.js +18 -0
  71. package/dist/generated/contract.cjs +622 -3
  72. package/dist/generated/contract.d.ts.map +1 -1
  73. package/dist/generated/contract.js +622 -3
  74. package/dist/index.cjs +3 -7
  75. package/dist/index.d.ts +8 -6
  76. package/dist/index.d.ts.map +1 -1
  77. package/dist/index.js +2 -8
  78. package/dist/presets/core.cjs +454 -0
  79. package/dist/presets/core.d.ts +20 -0
  80. package/dist/presets/core.d.ts.map +1 -0
  81. package/dist/presets/core.js +447 -0
  82. package/dist/presets.cjs +55 -8
  83. package/dist/presets.d.ts +39 -8
  84. package/dist/presets.d.ts.map +1 -1
  85. package/dist/presets.js +53 -8
  86. package/dist/prompts/mcp-prompt.md +23 -0
  87. package/dist/prompts/system-prompt.md +108 -0
  88. package/dist/runtime/transport-common.cjs +8 -0
  89. package/dist/runtime/transport-common.d.ts.map +1 -1
  90. package/dist/runtime/transport-common.js +8 -0
  91. package/dist/tools.cjs +46 -11
  92. package/dist/tools.d.ts +55 -8
  93. package/dist/tools.d.ts.map +1 -1
  94. package/dist/tools.js +45 -13
  95. package/package.json +9 -8
  96. package/tools/__pycache__/__init__.cpython-312.pyc +0 -0
  97. package/tools/__pycache__/intent_dispatch_generated.cpython-312.pyc +0 -0
  98. package/tools/catalog.json +14 -0
  99. package/tools/tools-policy.json +1 -1
  100. package/tools/tools.anthropic.json +14 -0
  101. package/tools/tools.generic.json +14 -0
  102. package/tools/tools.openai.json +14 -0
  103. package/tools/tools.vercel.json +14 -0
@@ -0,0 +1,215 @@
1
+ 'use strict';
2
+
3
+ const DEFAULT_PAGE_LIMIT = 250;
4
+ function pushLookup(map, key, value) {
5
+ const existing = map.get(key);
6
+ if (existing == null) {
7
+ map.set(key, [value]);
8
+ return;
9
+ }
10
+ existing.push(value);
11
+ }
12
+ function pushUniqueLookup(map, key, value) {
13
+ const existing = map.get(key);
14
+ if (existing == null) {
15
+ map.set(key, [value]);
16
+ return;
17
+ }
18
+ if (!existing.includes(value)) {
19
+ existing.push(value);
20
+ }
21
+ }
22
+ async function listAllBlocks(documentHandle, invokeOptions, pageLimit) {
23
+ const blocks = [];
24
+ let offset = 0;
25
+ while (true) {
26
+ const page = await documentHandle.blocks.list({ offset, limit: pageLimit, includeText: false }, invokeOptions);
27
+ blocks.push(...page.blocks);
28
+ offset += page.blocks.length;
29
+ if (page.blocks.length === 0 || offset >= page.total) {
30
+ break;
31
+ }
32
+ }
33
+ return blocks;
34
+ }
35
+ async function listAllListItems(documentHandle, invokeOptions, pageLimit) {
36
+ const items = [];
37
+ let offset = 0;
38
+ while (true) {
39
+ const page = await documentHandle.lists.list({ offset, limit: pageLimit }, invokeOptions);
40
+ items.push(...page.items);
41
+ offset += page.items.length;
42
+ if (page.items.length === 0 || offset >= page.total) {
43
+ break;
44
+ }
45
+ }
46
+ return items;
47
+ }
48
+ function coerceErrorMessage(error) {
49
+ if (error instanceof Error) {
50
+ return error.message;
51
+ }
52
+ return String(error);
53
+ }
54
+ async function getTableShape(documentHandle, nodeId, invokeOptions) {
55
+ const table = await documentHandle.tables.get({ nodeId }, invokeOptions);
56
+ return { rows: table.rows, columns: table.columns };
57
+ }
58
+ async function buildWorkflowDocIndex(input) {
59
+ const pageLimit = input.pageLimit ?? DEFAULT_PAGE_LIMIT;
60
+ const invokeOptions = input.invokeOptions;
61
+ const info = input.info ?? (await input.documentHandle.info({}, invokeOptions));
62
+ const [rawBlocks, rawListItems] = await Promise.all([
63
+ listAllBlocks(input.documentHandle, invokeOptions, pageLimit),
64
+ listAllListItems(input.documentHandle, invokeOptions, pageLimit),
65
+ ]);
66
+ const diagnostics = [];
67
+ let paragraphOrdinal = 0;
68
+ let bodyParagraphOrdinal = 0;
69
+ let headingOrdinal = 0;
70
+ const blocks = rawBlocks.map((block) => {
71
+ const record = block;
72
+ const textPreview = block.textPreview ?? undefined;
73
+ const nonEmptyText = typeof textPreview === 'string' && textPreview.trim().length > 0;
74
+ const indexed = {
75
+ kind: 'block',
76
+ ordinal: block.ordinal,
77
+ nodeId: block.nodeId,
78
+ nodeType: block.nodeType,
79
+ ref: block.ref ?? undefined,
80
+ textPreview,
81
+ styleId: typeof record.styleId === 'string' ? record.styleId : undefined,
82
+ styleName: typeof record.styleName === 'string' ? record.styleName : undefined,
83
+ headingLevel: block.headingLevel ?? undefined,
84
+ };
85
+ if (block.nodeType === 'paragraph') {
86
+ paragraphOrdinal += 1;
87
+ indexed.paragraphOrdinal = paragraphOrdinal;
88
+ if (nonEmptyText) {
89
+ bodyParagraphOrdinal += 1;
90
+ indexed.bodyParagraphOrdinal = bodyParagraphOrdinal;
91
+ }
92
+ }
93
+ if (block.nodeType === 'heading') {
94
+ headingOrdinal += 1;
95
+ indexed.headingOrdinal = headingOrdinal;
96
+ }
97
+ return indexed;
98
+ });
99
+ const tables = [];
100
+ const tableBlocks = blocks.filter((block) => block.nodeType === 'table');
101
+ for (let index = 0; index < tableBlocks.length; index++) {
102
+ const tableBlock = tableBlocks[index];
103
+ if (tableBlock == null)
104
+ continue;
105
+ const tableOrdinal = index + 1;
106
+ tableBlock.tableOrdinal = tableOrdinal;
107
+ let rows;
108
+ let columns;
109
+ try {
110
+ const shape = await getTableShape(input.documentHandle, tableBlock.nodeId, invokeOptions);
111
+ rows = shape.rows;
112
+ columns = shape.columns;
113
+ }
114
+ catch (error) {
115
+ diagnostics.push({
116
+ code: 'TABLE_SHAPE_UNAVAILABLE',
117
+ message: `Failed to resolve table shape for node ${tableBlock.nodeId}.`,
118
+ details: {
119
+ nodeId: tableBlock.nodeId,
120
+ tableOrdinal,
121
+ error: coerceErrorMessage(error),
122
+ },
123
+ });
124
+ }
125
+ tables.push({
126
+ kind: 'table',
127
+ nodeId: tableBlock.nodeId,
128
+ tableOrdinal,
129
+ blockOrdinal: tableBlock.ordinal,
130
+ ref: tableBlock.ref,
131
+ rows,
132
+ columns,
133
+ });
134
+ }
135
+ const lists = rawListItems.map((item, index) => ({
136
+ kind: 'listItem',
137
+ nodeId: item.address.nodeId,
138
+ listId: item.listId,
139
+ ref: item.handle.ref,
140
+ apiOrdinal: item.ordinal,
141
+ indexOrdinal: index + 1,
142
+ level: item.level,
143
+ marker: item.marker,
144
+ listKind: item.kind,
145
+ text: item.text,
146
+ }));
147
+ const byRef = new Map();
148
+ const byNodeId = new Map();
149
+ const byBlockOrdinal = new Map();
150
+ const byParagraphOrdinal = new Map();
151
+ const byBodyParagraphOrdinal = new Map();
152
+ const byHeadingOrdinal = new Map();
153
+ const byListOrdinal = new Map();
154
+ const byTableOrdinal = new Map();
155
+ for (const block of blocks) {
156
+ if (block.ref != null && block.ref.length > 0) {
157
+ pushLookup(byRef, block.ref, block);
158
+ }
159
+ pushLookup(byNodeId, block.nodeId, block);
160
+ byBlockOrdinal.set(block.ordinal, block);
161
+ if (typeof block.paragraphOrdinal === 'number') {
162
+ byParagraphOrdinal.set(block.paragraphOrdinal, block);
163
+ }
164
+ if (typeof block.bodyParagraphOrdinal === 'number') {
165
+ byBodyParagraphOrdinal.set(block.bodyParagraphOrdinal, block);
166
+ }
167
+ if (typeof block.headingOrdinal === 'number') {
168
+ byHeadingOrdinal.set(block.headingOrdinal, block);
169
+ }
170
+ }
171
+ for (const listItem of lists) {
172
+ if (listItem.ref != null && listItem.ref.length > 0) {
173
+ pushLookup(byRef, listItem.ref, listItem);
174
+ }
175
+ if (!byNodeId.has(listItem.nodeId)) {
176
+ pushLookup(byNodeId, listItem.nodeId, listItem);
177
+ }
178
+ if (typeof listItem.apiOrdinal === 'number') {
179
+ pushUniqueLookup(byListOrdinal, listItem.apiOrdinal, listItem);
180
+ }
181
+ pushUniqueLookup(byListOrdinal, listItem.indexOrdinal, listItem);
182
+ }
183
+ for (const table of tables) {
184
+ if (!byNodeId.has(table.nodeId)) {
185
+ pushLookup(byNodeId, table.nodeId, table);
186
+ }
187
+ if (table.ref != null && table.ref.length > 0 && !byRef.has(table.ref)) {
188
+ pushLookup(byRef, table.ref, table);
189
+ }
190
+ byTableOrdinal.set(table.tableOrdinal, table);
191
+ }
192
+ return {
193
+ documentKey: input.documentKey,
194
+ revision: info.revision,
195
+ builtAtMs: Date.now(),
196
+ counts: info.counts,
197
+ outline: info.outline,
198
+ blocks,
199
+ lists,
200
+ tables,
201
+ lookup: {
202
+ byRef,
203
+ byNodeId,
204
+ byBlockOrdinal,
205
+ byParagraphOrdinal,
206
+ byBodyParagraphOrdinal,
207
+ byHeadingOrdinal,
208
+ byListOrdinal,
209
+ byTableOrdinal,
210
+ },
211
+ diagnostics,
212
+ };
213
+ }
214
+
215
+ exports.buildWorkflowDocIndex = buildWorkflowDocIndex;
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Read-optimized document snapshot for action-primitive tools.
3
+ *
4
+ * The workflow tools repeatedly ask the same questions: "what is the first
5
+ * heading?", "which table is table 2?", "what block owns this ref?", and so on.
6
+ * This index converts the raw doc-api pagination surface into stable ordinal
7
+ * lookups once per document revision so each workflow can stay deterministic
8
+ * and avoid re-fetching broad document state.
9
+ */
10
+ import type { BoundDocApi, DocBlocksListResult, DocInfoResult } from '../generated/client.js';
11
+ import type { InvokeOptions } from '../runtime/process.js';
12
+ export type WorkflowIndexDiagnostic = {
13
+ code: string;
14
+ message: string;
15
+ details?: Record<string, unknown>;
16
+ };
17
+ export type WorkflowIndexedBlock = {
18
+ kind: 'block';
19
+ ordinal: number;
20
+ paragraphOrdinal?: number;
21
+ bodyParagraphOrdinal?: number;
22
+ headingOrdinal?: number;
23
+ nodeId: string;
24
+ nodeType: DocBlocksListResult['blocks'][number]['nodeType'];
25
+ ref?: string;
26
+ textPreview?: string | null;
27
+ styleId?: string;
28
+ styleName?: string;
29
+ headingLevel?: number;
30
+ tableOrdinal?: number;
31
+ };
32
+ export type WorkflowIndexedListItem = {
33
+ kind: 'listItem';
34
+ nodeId: string;
35
+ listId: string;
36
+ ref?: string;
37
+ apiOrdinal?: number;
38
+ indexOrdinal: number;
39
+ level?: number;
40
+ marker?: string;
41
+ listKind?: 'ordered' | 'bullet';
42
+ text?: string;
43
+ };
44
+ export type WorkflowIndexedTable = {
45
+ kind: 'table';
46
+ nodeId: string;
47
+ tableOrdinal: number;
48
+ blockOrdinal: number;
49
+ ref?: string;
50
+ rows?: number;
51
+ columns?: number;
52
+ };
53
+ export type WorkflowIndexedEntity = WorkflowIndexedBlock | WorkflowIndexedListItem | WorkflowIndexedTable;
54
+ export type WorkflowDocIndex = {
55
+ documentKey: string;
56
+ revision: string;
57
+ builtAtMs: number;
58
+ counts: DocInfoResult['counts'];
59
+ outline: DocInfoResult['outline'];
60
+ blocks: WorkflowIndexedBlock[];
61
+ lists: WorkflowIndexedListItem[];
62
+ tables: WorkflowIndexedTable[];
63
+ lookup: {
64
+ byRef: Map<string, WorkflowIndexedEntity[]>;
65
+ byNodeId: Map<string, WorkflowIndexedEntity[]>;
66
+ byBlockOrdinal: Map<number, WorkflowIndexedBlock>;
67
+ byParagraphOrdinal: Map<number, WorkflowIndexedBlock>;
68
+ byBodyParagraphOrdinal: Map<number, WorkflowIndexedBlock>;
69
+ byHeadingOrdinal: Map<number, WorkflowIndexedBlock>;
70
+ byListOrdinal: Map<number, WorkflowIndexedListItem[]>;
71
+ byTableOrdinal: Map<number, WorkflowIndexedTable>;
72
+ };
73
+ diagnostics: WorkflowIndexDiagnostic[];
74
+ };
75
+ export type BuildWorkflowDocIndexInput = {
76
+ documentHandle: BoundDocApi;
77
+ documentKey: string;
78
+ invokeOptions?: InvokeOptions;
79
+ pageLimit?: number;
80
+ info?: DocInfoResult;
81
+ };
82
+ export declare function buildWorkflowDocIndex(input: BuildWorkflowDocIndexInput): Promise<WorkflowDocIndex>;
83
+ //# sourceMappingURL=doc-index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"doc-index.d.ts","sourceRoot":"","sources":["../../src/action-primitives/doc-index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,KAAK,EACV,WAAW,EACX,mBAAmB,EACnB,aAAa,EAGd,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAI3D,MAAM,MAAM,uBAAuB,GAAG;IACpC,IAAI,EAAE,MA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pB,aAAa,CAAC,EAAE,aAAa,CAAC;IAC9B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,EAAE,aAAa,CAAC;CACtB,CAAC;AA0EF,wBAAsB,qBAAqB,CAAC,KAAK,EAAE,0BAA0B,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAwKxG"}
@@ -0,0 +1,211 @@
1
+ const DEFAULT_PAGE_LIMIT = 250;
2
+ function pushLookup(map, key, value) {
3
+ const existing = map.get(key);
4
+ if (existing == null) {
5
+ map.set(key, [value]);
6
+ return;
7
+ }
8
+ existing.push(value);
9
+ }
10
+ function pushUniqueLookup(map, key, value) {
11
+ const existing = map.get(key);
12
+ if (existing == null) {
13
+ map.set(key, [value]);
14
+ return;
15
+ }
16
+ if (!existing.includes(value)) {
17
+ existing.push(value);
18
+ }
19
+ }
20
+ async function listAllBlocks(documentHandle, invokeOptions, pageLimit) {
21
+ const blocks = [];
22
+ let offset = 0;
23
+ while (true) {
24
+ const page = await documentHandle.blocks.list({ offset, limit: pageLimit, includeText: false }, invokeOptions);
25
+ blocks.push(...page.blocks);
26
+ offset += page.blocks.length;
27
+ if (page.blocks.length === 0 || offset >= page.total) {
28
+ break;
29
+ }
30
+ }
31
+ return blocks;
32
+ }
33
+ async function listAllListItems(documentHandle, invokeOptions, pageLimit) {
34
+ const items = [];
35
+ let offset = 0;
36
+ while (true) {
37
+ const page = await documentHandle.lists.list({ offset, limit: pageLimit }, invokeOptions);
38
+ items.push(...page.items);
39
+ offset += page.items.length;
40
+ if (page.items.length === 0 || offset >= page.total) {
41
+ break;
42
+ }
43
+ }
44
+ return items;
45
+ }
46
+ function coerceErrorMessage(error) {
47
+ if (error instanceof Error) {
48
+ return error.message;
49
+ }
50
+ return String(error);
51
+ }
52
+ async function getTableShape(documentHandle, nodeId, invokeOptions) {
53
+ const table = await documentHandle.tables.get({ nodeId }, invokeOptions);
54
+ return { rows: table.rows, columns: table.columns };
55
+ }
56
+ export async function buildWorkflowDocIndex(input) {
57
+ const pageLimit = input.pageLimit ?? DEFAULT_PAGE_LIMIT;
58
+ const invokeOptions = input.invokeOptions;
59
+ const info = input.info ?? (await input.documentHandle.info({}, invokeOptions));
60
+ const [rawBlocks, rawListItems] = await Promise.all([
61
+ listAllBlocks(input.documentHandle, invokeOptions, pageLimit),
62
+ listAllListItems(input.documentHandle, invokeOptions, pageLimit),
63
+ ]);
64
+ const diagnostics = [];
65
+ let paragraphOrdinal = 0;
66
+ let bodyParagraphOrdinal = 0;
67
+ let headingOrdinal = 0;
68
+ const blocks = rawBlocks.map((block) => {
69
+ const record = block;
70
+ const textPreview = block.textPreview ?? undefined;
71
+ const nonEmptyText = typeof textPreview === 'string' && textPreview.trim().length > 0;
72
+ const indexed = {
73
+ kind: 'block',
74
+ ordinal: block.ordinal,
75
+ nodeId: block.nodeId,
76
+ nodeType: block.nodeType,
77
+ ref: block.ref ?? undefined,
78
+ textPreview,
79
+ styleId: typeof record.styleId === 'string' ? record.styleId : undefined,
80
+ styleName: typeof record.styleName === 'string' ? record.styleName : undefined,
81
+ headingLevel: block.headingLevel ?? undefined,
82
+ };
83
+ if (block.nodeType === 'paragraph') {
84
+ paragraphOrdinal += 1;
85
+ indexed.paragraphOrdinal = paragraphOrdinal;
86
+ if (nonEmptyText) {
87
+ bodyParagraphOrdinal += 1;
88
+ indexed.bodyParagraphOrdinal = bodyParagraphOrdinal;
89
+ }
90
+ }
91
+ if (block.nodeType === 'heading') {
92
+ headingOrdinal += 1;
93
+ indexed.headingOrdinal = headingOrdinal;
94
+ }
95
+ return indexed;
96
+ });
97
+ const tables = [];
98
+ const tableBlocks = blocks.filter((block) => block.nodeType === 'table');
99
+ for (let index = 0; index < tableBlocks.length; index++) {
100
+ const tableBlock = tableBlocks[index];
101
+ if (tableBlock == null)
102
+ continue;
103
+ const tableOrdinal = index + 1;
104
+ tableBlock.tableOrdinal = tableOrdinal;
105
+ let rows;
106
+ let columns;
107
+ try {
108
+ const shape = await getTableShape(input.documentHandle, tableBlock.nodeId, invokeOptions);
109
+ rows = shape.rows;
110
+ columns = shape.columns;
111
+ }
112
+ catch (error) {
113
+ diagnostics.push({
114
+ code: 'TABLE_SHAPE_UNAVAILABLE',
115
+ message: `Failed to resolve table shape for node ${tableBlock.nodeId}.`,
116
+ details: {
117
+ nodeId: tableBlock.nodeId,
118
+ tableOrdinal,
119
+ error: coerceErrorMessage(error),
120
+ },
121
+ });
122
+ }
123
+ tables.push({
124
+ kind: 'table',
125
+ nodeId: tableBlock.nodeId,
126
+ tableOrdinal,
127
+ blockOrdinal: tableBlock.ordinal,
128
+ ref: tableBlock.ref,
129
+ rows,
130
+ columns,
131
+ });
132
+ }
133
+ const lists = rawListItems.map((item, index) => ({
134
+ kind: 'listItem',
135
+ nodeId: item.address.nodeId,
136
+ listId: item.listId,
137
+ ref: item.handle.ref,
138
+ apiOrdinal: item.ordinal,
139
+ indexOrdinal: index + 1,
140
+ level: item.level,
141
+ marker: item.marker,
142
+ listKind: item.kind,
143
+ text: item.text,
144
+ }));
145
+ const byRef = new Map();
146
+ const byNodeId = new Map();
147
+ const byBlockOrdinal = new Map();
148
+ const byParagraphOrdinal = new Map();
149
+ const byBodyParagraphOrdinal = new Map();
150
+ const byHeadingOrdinal = new Map();
151
+ const byListOrdinal = new Map();
152
+ const byTableOrdinal = new Map();
153
+ for (const block of blocks) {
154
+ if (block.ref != null && block.ref.length > 0) {
155
+ pushLookup(byRef, block.ref, block);
156
+ }
157
+ pushLookup(byNodeId, block.nodeId, block);
158
+ byBlockOrdinal.set(block.ordinal, block);
159
+ if (typeof block.paragraphOrdinal === 'number') {
160
+ byParagraphOrdinal.set(block.paragraphOrdinal, block);
161
+ }
162
+ if (typeof block.bodyParagraphOrdinal === 'number') {
163
+ byBodyParagraphOrdinal.set(block.bodyParagraphOrdinal, block);
164
+ }
165
+ if (typeof block.headingOrdinal === 'number') {
166
+ byHeadingOrdinal.set(block.headingOrdinal, block);
167
+ }
168
+ }
169
+ for (const listItem of lists) {
170
+ if (listItem.ref != null && listItem.ref.length > 0) {
171
+ pushLookup(byRef, listItem.ref, listItem);
172
+ }
173
+ if (!byNodeId.has(listItem.nodeId)) {
174
+ pushLookup(byNodeId, listItem.nodeId, listItem);
175
+ }
176
+ if (typeof listItem.apiOrdinal === 'number') {
177
+ pushUniqueLookup(byListOrdinal, listItem.apiOrdinal, listItem);
178
+ }
179
+ pushUniqueLookup(byListOrdinal, listItem.indexOrdinal, listItem);
180
+ }
181
+ for (const table of tables) {
182
+ if (!byNodeId.has(table.nodeId)) {
183
+ pushLookup(byNodeId, table.nodeId, table);
184
+ }
185
+ if (table.ref != null && table.ref.length > 0 && !byRef.has(table.ref)) {
186
+ pushLookup(byRef, table.ref, table);
187
+ }
188
+ byTableOrdinal.set(table.tableOrdinal, table);
189
+ }
190
+ return {
191
+ documentKey: input.documentKey,
192
+ revision: info.revision,
193
+ builtAtMs: Date.now(),
194
+ counts: info.counts,
195
+ outline: info.outline,
196
+ blocks,
197
+ lists,
198
+ tables,
199
+ lookup: {
200
+ byRef,
201
+ byNodeId,
202
+ byBlockOrdinal,
203
+ byParagraphOrdinal,
204
+ byBodyParagraphOrdinal,
205
+ byHeadingOrdinal,
206
+ byListOrdinal,
207
+ byTableOrdinal,
208
+ },
209
+ diagnostics,
210
+ };
211
+ }