@claritylabs/cl-sdk 3.0.6 → 3.0.7

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.mjs CHANGED
@@ -9078,13 +9078,157 @@ function simplifyOrganizerTitle(value, fallback, kind) {
9078
9078
  return title;
9079
9079
  }
9080
9080
  function endorsementReference(value) {
9081
- return value?.match(/\bendorsement\s+(?:no\.?|number|#)?\s*([A-Z0-9][A-Z0-9.-]*)\b/i)?.[1]?.toUpperCase();
9081
+ const text = cleanText(value, "");
9082
+ const explicit = text.match(/\bendorsement\s+(?:no\.?|number|#)?\s*([A-Z0-9][A-Z0-9.-]*)\b/i)?.[1]?.toUpperCase();
9083
+ if (explicit) return explicit;
9084
+ return text.match(/\b(?:[A-Z]{2,}-)?END\s+0*([0-9]{1,4})\b/i)?.[1]?.toUpperCase();
9082
9085
  }
9083
9086
  function endorsementTitle(value) {
9084
9087
  const text = cleanText(value, "");
9085
- const number = text.match(/\bendorsement\s+(?:no\.?|number|#)\s*([A-Z0-9][A-Z0-9.-]*)\b/i)?.[1]?.toUpperCase();
9088
+ const explicit = text.match(/\bendorsement\s+(?:no\.?|number|#)\s*([A-Z0-9][A-Z0-9.-]*)\b/i)?.[1]?.toUpperCase();
9089
+ const number = explicit ?? text.match(/\b(?:[A-Z]{2,}-)?END\s+0*([0-9]{1,4})\b/i)?.[1]?.toUpperCase();
9086
9090
  return number ? `Endorsement No. ${number}` : void 0;
9087
9091
  }
9092
+ function sourceNodeText(node) {
9093
+ return cleanText([node.title, node.description, node.textExcerpt].filter(Boolean).join(" "), "");
9094
+ }
9095
+ function semanticGroupNodeId(documentId, kind, title, childNodeIds) {
9096
+ return [
9097
+ documentId.replace(/[^a-zA-Z0-9_.:-]/g, "_"),
9098
+ "source_node",
9099
+ kind,
9100
+ title.replace(/[^a-zA-Z0-9_.:-]/g, "_").toLowerCase().slice(0, 48),
9101
+ childNodeIds.join("_").replace(/[^a-zA-Z0-9_.:-]/g, "_").slice(0, 80)
9102
+ ].join(":");
9103
+ }
9104
+ function looksLikeDeclarationsStart(node) {
9105
+ return /\bdeclarations?\s+(page|schedule)\b/i.test(sourceNodeText(node));
9106
+ }
9107
+ function looksLikeDeclarationsContinuation(node) {
9108
+ const text = sourceNodeText(node);
9109
+ return looksLikeDeclarationsStart(node) || /\b(item\s+\d+\.|coverage part|each claim limit|aggregate limit|retroactive date|self-insured retention|premium|payment plan|producer|broker|forms? and endorsements?|extended reporting period|discovery period)\b/i.test(text);
9110
+ }
9111
+ function looksLikePolicyFormStart(node) {
9112
+ const text = sourceNodeText(node);
9113
+ return /\bpolicy form\b/i.test(node.title) || /\btechnology errors?\s*&?\s*omissions\b/i.test(text) && /\bplease read this entire policy carefully\b/i.test(text) || /\bform\s+[A-Z]{2,}-[A-Z0-9-]+\s+\d{2}\s+\d{2}\b/i.test(text);
9114
+ }
9115
+ function looksLikePolicyFormContinuation(node) {
9116
+ const text = sourceNodeText(node);
9117
+ if (looksLikePolicyFormStart(node)) return true;
9118
+ return /\b(insuring agreement|definitions?|exclusions?|conditions?|claim means|insured means|wrongful act means|limits of liability|notice of claim|cancellation by|action against the company)\b/i.test(text);
9119
+ }
9120
+ function groupAdjacentChildren(params) {
9121
+ if (params.childIds.length < 2) return params.sourceTree;
9122
+ const children = params.childIds.map((id2) => params.children.find((child) => child.id === id2)).filter((child) => Boolean(child));
9123
+ if (children.length < 2) return params.sourceTree;
9124
+ const parentId = children[0].parentId;
9125
+ if (!children.every((child) => child.parentId === parentId)) return params.sourceTree;
9126
+ const documentId = children[0].documentId;
9127
+ const id = semanticGroupNodeId(documentId, params.kind, params.title, children.map((child) => child.id));
9128
+ if (params.sourceTree.some((node) => node.id === id)) return params.sourceTree;
9129
+ const pageStarts = children.map((child) => child.pageStart).filter((page) => typeof page === "number");
9130
+ const pageEnds = children.map((child) => child.pageEnd ?? child.pageStart).filter((page) => typeof page === "number");
9131
+ const sourceSpanIds = [...new Set(children.flatMap((child) => child.sourceSpanIds))];
9132
+ const order = Math.min(...children.map((child) => child.order));
9133
+ const groupNode = {
9134
+ id,
9135
+ documentId,
9136
+ parentId,
9137
+ kind: params.kind,
9138
+ title: params.title,
9139
+ description: params.description,
9140
+ textExcerpt: children.map((child) => child.textExcerpt ?? child.description).filter(Boolean).join("\n\n").slice(0, 1600),
9141
+ sourceSpanIds,
9142
+ pageStart: pageStarts.length ? Math.min(...pageStarts) : void 0,
9143
+ pageEnd: pageEnds.length ? Math.max(...pageEnds) : void 0,
9144
+ bbox: children.flatMap((child) => child.bbox ?? []).slice(0, 12),
9145
+ order,
9146
+ path: "",
9147
+ metadata: { sourceTreeVersion: "v3", organizer: params.organizer }
9148
+ };
9149
+ const wanted = new Set(children.map((child) => child.id));
9150
+ return [
9151
+ ...params.sourceTree.map(
9152
+ (node) => wanted.has(node.id) ? { ...node, parentId: id, order: node.order + 1e-3 } : node
9153
+ ),
9154
+ groupNode
9155
+ ];
9156
+ }
9157
+ function applySemanticPageGrouping(sourceTree) {
9158
+ const relabeled = sourceTree.map((node) => {
9159
+ if (node.kind === "document" || node.kind === "page_group") return node;
9160
+ const text = sourceNodeText(node);
9161
+ const endorsement = endorsementTitle(text);
9162
+ if (endorsement && node.kind === "page") {
9163
+ return {
9164
+ ...node,
9165
+ kind: "endorsement",
9166
+ title: endorsement,
9167
+ description: cleanText([endorsement, "endorsement", node.pageStart ? `page ${node.pageStart}` : void 0, node.textExcerpt].filter(Boolean).join(" | "), endorsement),
9168
+ metadata: { ...node.metadata, organizerRepair: "semantic_page_grouping" }
9169
+ };
9170
+ }
9171
+ if (node.kind === "page" && looksLikeDeclarationsStart(node)) {
9172
+ return {
9173
+ ...node,
9174
+ title: "Declarations",
9175
+ description: cleanText([node.description, "Declarations"].join(" "), "Declarations"),
9176
+ metadata: { ...node.metadata, organizerRepair: "semantic_page_grouping" }
9177
+ };
9178
+ }
9179
+ if (node.kind === "page" && looksLikePolicyFormStart(node)) {
9180
+ return {
9181
+ ...node,
9182
+ title: "Policy Form",
9183
+ description: cleanText([node.description, "Policy Form"].join(" "), "Policy Form"),
9184
+ metadata: { ...node.metadata, organizerRepair: "semantic_page_grouping" }
9185
+ };
9186
+ }
9187
+ return node;
9188
+ });
9189
+ const rootId = sourceTreeRootId(relabeled);
9190
+ const children = (nodesByParent(relabeled).get(rootId) ?? []).filter((node) => node.kind !== "document").sort((left, right) => left.order - right.order);
9191
+ let nextTree = relabeled;
9192
+ const declarationsStartIndex = children.findIndex(looksLikeDeclarationsStart);
9193
+ if (declarationsStartIndex >= 0) {
9194
+ const declarationIds = [];
9195
+ for (let index = declarationsStartIndex; index < children.length; index += 1) {
9196
+ const child = children[index];
9197
+ if (index > declarationsStartIndex && (looksLikePolicyFormStart(child) || endorsementTitle(sourceNodeText(child)))) break;
9198
+ if (!looksLikeDeclarationsContinuation(child)) break;
9199
+ declarationIds.push(child.id);
9200
+ }
9201
+ nextTree = groupAdjacentChildren({
9202
+ sourceTree: nextTree,
9203
+ children,
9204
+ childIds: declarationIds,
9205
+ kind: "page_group",
9206
+ title: "Declarations",
9207
+ description: "Declarations pages and schedules grouped by source order",
9208
+ organizer: "semantic_declarations_grouping"
9209
+ });
9210
+ }
9211
+ const policyStartIndex = children.findIndex(looksLikePolicyFormStart);
9212
+ if (policyStartIndex >= 0) {
9213
+ const policyIds = [];
9214
+ for (let index = policyStartIndex; index < children.length; index += 1) {
9215
+ const child = children[index];
9216
+ if (index > policyStartIndex && endorsementTitle(sourceNodeText(child))) break;
9217
+ if (!looksLikePolicyFormContinuation(child)) break;
9218
+ policyIds.push(child.id);
9219
+ }
9220
+ nextTree = groupAdjacentChildren({
9221
+ sourceTree: nextTree,
9222
+ children,
9223
+ childIds: policyIds,
9224
+ kind: "form",
9225
+ title: "Policy Form",
9226
+ description: "Policy form pages grouped by source order",
9227
+ organizer: "semantic_policy_form_grouping"
9228
+ });
9229
+ }
9230
+ return applyEndorsementGrouping(normalizeDocumentSourceTreePaths(nextTree));
9231
+ }
9088
9232
  function rejectsOrganizerGroup(group, children) {
9089
9233
  if (/^endorsements?\b/i.test(group.title) && group.kind !== "page_group") return true;
9090
9234
  if (group.kind !== "endorsement") return false;
@@ -9571,6 +9715,7 @@ async function runSourceTreeExtraction(params) {
9571
9715
  } catch (error) {
9572
9716
  warnings.push(`Source-tree organizer failed; deterministic tree used (${error instanceof Error ? error.message : String(error)})`);
9573
9717
  }
9718
+ sourceTree = applySemanticPageGrouping(sourceTree);
9574
9719
  const deterministicProfile = buildDeterministicOperationalProfile({
9575
9720
  sourceTree,
9576
9721
  sourceSpans
@@ -9611,7 +9756,6 @@ async function runSourceTreeExtraction(params) {
9611
9756
  } catch (error) {
9612
9757
  warnings.push(`Operational profile model pass failed; deterministic profile used (${error instanceof Error ? error.message : String(error)})`);
9613
9758
  }
9614
- sourceTree = applyEndorsementGrouping(sourceTree);
9615
9759
  const document = materializeDocument({
9616
9760
  id: params.id,
9617
9761
  sourceTree,