@claritylabs/cl-sdk 3.1.14 → 3.1.16
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.js +152 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +152 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -9142,6 +9142,10 @@ var OperationalProfileCleanupSchema = z41.object({
|
|
|
9142
9142
|
})).default([]),
|
|
9143
9143
|
warnings: z41.array(z41.string()).default([])
|
|
9144
9144
|
});
|
|
9145
|
+
var CLEANUP_CANDIDATE_ID_LIMIT = 12;
|
|
9146
|
+
var CLEANUP_SOURCE_NODE_LIMIT = 90;
|
|
9147
|
+
var CLEANUP_SIBLING_WINDOW = 4;
|
|
9148
|
+
var CLEANUP_KEYWORD = /\b(coverage|limit|liability|deductible|retention|retroactive|premium|aggregate|sublimit|sub-limit|claim|occurrence|loss|proceeding|endorsement|declarations?)\b|\$[0-9]/i;
|
|
9145
9149
|
function compactNode(node, maxText = 700) {
|
|
9146
9150
|
return {
|
|
9147
9151
|
id: node.id,
|
|
@@ -9154,6 +9158,9 @@ function compactNode(node, maxText = 700) {
|
|
|
9154
9158
|
text: (node.textExcerpt ?? node.description).slice(0, maxText)
|
|
9155
9159
|
};
|
|
9156
9160
|
}
|
|
9161
|
+
function compactIds(ids) {
|
|
9162
|
+
return uniqueStrings([...ids ?? []]).slice(0, CLEANUP_CANDIDATE_ID_LIMIT);
|
|
9163
|
+
}
|
|
9157
9164
|
function compactCoverageForCleanup(coverage, coverageIndex) {
|
|
9158
9165
|
return {
|
|
9159
9166
|
coverageIndex,
|
|
@@ -9163,8 +9170,8 @@ function compactCoverageForCleanup(coverage, coverageIndex) {
|
|
|
9163
9170
|
premium: coverage.premium,
|
|
9164
9171
|
retroactiveDate: coverage.retroactiveDate,
|
|
9165
9172
|
coverageOrigin: coverage.coverageOrigin,
|
|
9166
|
-
sourceNodeIds: coverage.sourceNodeIds,
|
|
9167
|
-
sourceSpanIds: coverage.sourceSpanIds,
|
|
9173
|
+
sourceNodeIds: compactIds(coverage.sourceNodeIds),
|
|
9174
|
+
sourceSpanIds: compactIds(coverage.sourceSpanIds),
|
|
9168
9175
|
terms: coverage.limits.map((term, termIndex) => ({
|
|
9169
9176
|
termIndex,
|
|
9170
9177
|
kind: term.kind,
|
|
@@ -9172,13 +9179,112 @@ function compactCoverageForCleanup(coverage, coverageIndex) {
|
|
|
9172
9179
|
value: term.value,
|
|
9173
9180
|
amount: term.amount,
|
|
9174
9181
|
appliesTo: term.appliesTo,
|
|
9175
|
-
sourceNodeIds: term.sourceNodeIds,
|
|
9176
|
-
sourceSpanIds: term.sourceSpanIds
|
|
9182
|
+
sourceNodeIds: compactIds(term.sourceNodeIds),
|
|
9183
|
+
sourceSpanIds: compactIds(term.sourceSpanIds)
|
|
9177
9184
|
}))
|
|
9178
9185
|
};
|
|
9179
9186
|
}
|
|
9187
|
+
function nodeTextForSelection(node) {
|
|
9188
|
+
return [
|
|
9189
|
+
node.kind,
|
|
9190
|
+
node.title,
|
|
9191
|
+
node.description,
|
|
9192
|
+
node.textExcerpt
|
|
9193
|
+
].filter(Boolean).join(" ");
|
|
9194
|
+
}
|
|
9195
|
+
function coverageTextForSelection(coverage) {
|
|
9196
|
+
return [
|
|
9197
|
+
coverage.name,
|
|
9198
|
+
coverage.coverageCode,
|
|
9199
|
+
coverage.limit,
|
|
9200
|
+
coverage.deductible,
|
|
9201
|
+
coverage.premium,
|
|
9202
|
+
coverage.retroactiveDate,
|
|
9203
|
+
coverage.sectionRef,
|
|
9204
|
+
coverage.endorsementNumber,
|
|
9205
|
+
...coverage.limits.flatMap((term) => [
|
|
9206
|
+
term.kind,
|
|
9207
|
+
term.label,
|
|
9208
|
+
term.value,
|
|
9209
|
+
term.appliesTo
|
|
9210
|
+
])
|
|
9211
|
+
].filter(Boolean).join(" ");
|
|
9212
|
+
}
|
|
9213
|
+
function nodeTextMatchesCoverage(node, coverageTerms) {
|
|
9214
|
+
const text = nodeTextForSelection(node).toLowerCase();
|
|
9215
|
+
return coverageTerms.some((term) => term.length >= 5 && text.includes(term));
|
|
9216
|
+
}
|
|
9217
|
+
function selectCoverageCleanupNodes(sourceTree, profile) {
|
|
9218
|
+
const nodeById = new Map(sourceTree.map((node) => [node.id, node]));
|
|
9219
|
+
const childrenByParent = /* @__PURE__ */ new Map();
|
|
9220
|
+
for (const node of sourceTree) {
|
|
9221
|
+
if (!node.parentId) continue;
|
|
9222
|
+
const children = childrenByParent.get(node.parentId) ?? [];
|
|
9223
|
+
children.push(node);
|
|
9224
|
+
childrenByParent.set(node.parentId, children);
|
|
9225
|
+
}
|
|
9226
|
+
for (const children of childrenByParent.values()) {
|
|
9227
|
+
children.sort((left, right) => left.order - right.order);
|
|
9228
|
+
}
|
|
9229
|
+
const selected = /* @__PURE__ */ new Map();
|
|
9230
|
+
const addNode = (node, score) => {
|
|
9231
|
+
if (!node || node.kind === "document") return;
|
|
9232
|
+
const current = selected.get(node.id);
|
|
9233
|
+
if (!current || score > current.score) selected.set(node.id, { node, score });
|
|
9234
|
+
};
|
|
9235
|
+
const sourceNodeIds = uniqueStrings(profile.coverages.flatMap((coverage) => [
|
|
9236
|
+
...coverage.sourceNodeIds,
|
|
9237
|
+
...coverage.limits.flatMap((term) => term.sourceNodeIds)
|
|
9238
|
+
]));
|
|
9239
|
+
const coveragePages2 = /* @__PURE__ */ new Set();
|
|
9240
|
+
const coverageTerms = uniqueStrings(profile.coverages.flatMap(
|
|
9241
|
+
(coverage) => coverageTextForSelection(coverage).toLowerCase().split(/[^a-z0-9$,.]+/i).filter((part) => part.length >= 5)
|
|
9242
|
+
));
|
|
9243
|
+
for (const id of sourceNodeIds) {
|
|
9244
|
+
const node = nodeById.get(id);
|
|
9245
|
+
if (!node) continue;
|
|
9246
|
+
addNode(node, 1e3);
|
|
9247
|
+
if (node.pageStart) coveragePages2.add(node.pageStart);
|
|
9248
|
+
if (node.pageEnd) coveragePages2.add(node.pageEnd);
|
|
9249
|
+
let parentId = node.parentId;
|
|
9250
|
+
let parentScore = 940;
|
|
9251
|
+
while (parentId) {
|
|
9252
|
+
const parent = nodeById.get(parentId);
|
|
9253
|
+
if (!parent) break;
|
|
9254
|
+
addNode(parent, parentScore);
|
|
9255
|
+
parentId = parent.parentId;
|
|
9256
|
+
parentScore -= 30;
|
|
9257
|
+
}
|
|
9258
|
+
const siblings = node.parentId ? childrenByParent.get(node.parentId) ?? [] : [];
|
|
9259
|
+
for (const sibling of siblings) {
|
|
9260
|
+
if (Math.abs(sibling.order - node.order) <= CLEANUP_SIBLING_WINDOW) {
|
|
9261
|
+
addNode(sibling, 850 - Math.abs(sibling.order - node.order));
|
|
9262
|
+
}
|
|
9263
|
+
}
|
|
9264
|
+
}
|
|
9265
|
+
for (const selectedNode of [...selected.values()].map((entry) => entry.node)) {
|
|
9266
|
+
const children = childrenByParent.get(selectedNode.id) ?? [];
|
|
9267
|
+
for (const child of children.slice(0, 24)) addNode(child, 760);
|
|
9268
|
+
}
|
|
9269
|
+
for (const node of sourceTree) {
|
|
9270
|
+
if (node.kind === "document") continue;
|
|
9271
|
+
if (!node.pageStart || !coveragePages2.has(node.pageStart)) continue;
|
|
9272
|
+
const text = nodeTextForSelection(node);
|
|
9273
|
+
if (CLEANUP_KEYWORD.test(text) || nodeTextMatchesCoverage(node, coverageTerms)) {
|
|
9274
|
+
addNode(node, 600);
|
|
9275
|
+
}
|
|
9276
|
+
}
|
|
9277
|
+
return [...selected.values()].sort(
|
|
9278
|
+
(left, right) => right.score - left.score || (left.node.pageStart ?? Number.MAX_SAFE_INTEGER) - (right.node.pageStart ?? Number.MAX_SAFE_INTEGER) || left.node.order - right.node.order
|
|
9279
|
+
).slice(0, CLEANUP_SOURCE_NODE_LIMIT).sort(
|
|
9280
|
+
(left, right) => (left.node.pageStart ?? Number.MAX_SAFE_INTEGER) - (right.node.pageStart ?? Number.MAX_SAFE_INTEGER) || left.node.order - right.node.order
|
|
9281
|
+
).map((entry) => entry.node);
|
|
9282
|
+
}
|
|
9180
9283
|
function buildOperationalProfileCleanupPrompt(sourceTree, profile) {
|
|
9181
|
-
const nodes = sourceTree
|
|
9284
|
+
const nodes = selectCoverageCleanupNodes(sourceTree, profile).map((node) => compactNode(
|
|
9285
|
+
node,
|
|
9286
|
+
node.kind === "page" || node.kind === "page_group" ? 260 : node.kind === "table_row" || node.kind === "table_cell" ? 520 : 360
|
|
9287
|
+
));
|
|
9182
9288
|
const candidate = {
|
|
9183
9289
|
documentType: profile.documentType,
|
|
9184
9290
|
policyTypes: profile.policyTypes,
|
|
@@ -10749,6 +10855,46 @@ function tableRowTextFromCells(cells) {
|
|
|
10749
10855
|
}).filter(Boolean).join(" | ");
|
|
10750
10856
|
return text ? cleanText(text, text) : void 0;
|
|
10751
10857
|
}
|
|
10858
|
+
function normalizeTableBoundaryDelimiters(value) {
|
|
10859
|
+
const text = cleanText(value, "");
|
|
10860
|
+
if (!text) return void 0;
|
|
10861
|
+
const normalized = text.replace(/\s+\|\s+\|/g, " | ").replace(/\s+\/\s+\|/g, " |").replace(/\s+[|/]\s*$/g, "").replace(/\s{2,}/g, " ").trim();
|
|
10862
|
+
return normalized || void 0;
|
|
10863
|
+
}
|
|
10864
|
+
function normalizeSourceTreeTableDisplayText(sourceTree) {
|
|
10865
|
+
const byParent = nodesByParent(sourceTree);
|
|
10866
|
+
const updates = /* @__PURE__ */ new Map();
|
|
10867
|
+
const currentNode = (node) => updates.get(node.id) ?? node;
|
|
10868
|
+
for (const node of sourceTree) {
|
|
10869
|
+
if (node.kind !== "table_cell") continue;
|
|
10870
|
+
const textExcerpt = normalizeTableBoundaryDelimiters(node.textExcerpt);
|
|
10871
|
+
const textChanged = textExcerpt !== void 0 && textExcerpt !== node.textExcerpt;
|
|
10872
|
+
const description = textChanged && textExcerpt ? normalizeTableBoundaryDelimiters([node.title, textExcerpt].filter(Boolean).join(" | ")) : normalizeTableBoundaryDelimiters(node.description);
|
|
10873
|
+
if (textExcerpt === node.textExcerpt && description === node.description) continue;
|
|
10874
|
+
updates.set(node.id, {
|
|
10875
|
+
...node,
|
|
10876
|
+
...textExcerpt !== void 0 ? { textExcerpt } : {},
|
|
10877
|
+
...description !== void 0 ? { description } : {}
|
|
10878
|
+
});
|
|
10879
|
+
}
|
|
10880
|
+
for (const node of sourceTree) {
|
|
10881
|
+
if (node.kind !== "table_row") continue;
|
|
10882
|
+
const cells = (byParent.get(node.id) ?? []).filter((child) => child.kind === "table_cell").map(currentNode).sort(
|
|
10883
|
+
(left, right) => tableCellColumnIndex(left, 0) - tableCellColumnIndex(right, 0) || left.order - right.order || left.id.localeCompare(right.id)
|
|
10884
|
+
);
|
|
10885
|
+
const textExcerpt = cells.length ? tableRowTextFromCells(cells) : normalizeTableBoundaryDelimiters(node.textExcerpt);
|
|
10886
|
+
const textChanged = textExcerpt !== void 0 && textExcerpt !== node.textExcerpt;
|
|
10887
|
+
const description = textChanged && textExcerpt ? normalizeTableBoundaryDelimiters([node.title, textExcerpt].filter(Boolean).join(" | ")) : normalizeTableBoundaryDelimiters(node.description);
|
|
10888
|
+
if (textExcerpt === node.textExcerpt && description === node.description) continue;
|
|
10889
|
+
updates.set(node.id, {
|
|
10890
|
+
...node,
|
|
10891
|
+
...textExcerpt !== void 0 ? { textExcerpt } : {},
|
|
10892
|
+
...description !== void 0 ? { description } : {}
|
|
10893
|
+
});
|
|
10894
|
+
}
|
|
10895
|
+
if (updates.size === 0) return sourceTree;
|
|
10896
|
+
return sourceTree.map((node) => updates.get(node.id) ?? node);
|
|
10897
|
+
}
|
|
10752
10898
|
function tableRowTextForPrompt(row, cells) {
|
|
10753
10899
|
return cleanText(
|
|
10754
10900
|
cells.length ? cells.map(tableCellText).filter(Boolean).join(" | ") : row.textExcerpt ?? row.description ?? row.title,
|
|
@@ -11547,7 +11693,7 @@ async function runSourceTreeExtraction(params) {
|
|
|
11547
11693
|
trackUsage: localTrack,
|
|
11548
11694
|
log: params.log
|
|
11549
11695
|
});
|
|
11550
|
-
sourceTree = visualTableRepair.sourceTree;
|
|
11696
|
+
sourceTree = normalizeSourceTreeTableDisplayText(visualTableRepair.sourceTree);
|
|
11551
11697
|
warnings.push(...visualTableRepair.warnings);
|
|
11552
11698
|
const emptyProfile = emptyOperationalProfile();
|
|
11553
11699
|
let operationalProfile = emptyProfile;
|