@claritylabs/cl-sdk 3.1.8 → 3.1.10

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
@@ -10736,6 +10736,19 @@ function isSourceTreeHeaderRow(row) {
10736
10736
  function tableCellText(cell) {
10737
10737
  return cleanText(cell.textExcerpt ?? cell.description ?? cell.title, "");
10738
10738
  }
10739
+ function tableCellValueText(cell) {
10740
+ return cleanText(cell.textExcerpt ?? cell.description ?? "", "");
10741
+ }
10742
+ function tableRowTextFromCells(cells) {
10743
+ const text = cells.map((cell) => {
10744
+ const label = cleanText(cell.title, "");
10745
+ const value = tableCellValueText(cell);
10746
+ if (!value) return label;
10747
+ if (!label || value.toLowerCase() === label.toLowerCase()) return value;
10748
+ return `${label}: ${value}`;
10749
+ }).filter(Boolean).join(" | ");
10750
+ return text ? cleanText(text, text) : void 0;
10751
+ }
10739
10752
  function tableRowTextForPrompt(row, cells) {
10740
10753
  return cleanText(
10741
10754
  cells.length ? cells.map(tableCellText).filter(Boolean).join(" | ") : row.textExcerpt ?? row.description ?? row.title,
@@ -10750,6 +10763,17 @@ function isGenericColumnTitle(value) {
10750
10763
  const title = cleanText(value, "");
10751
10764
  return !title || /^(?:column\s+\d+|table cell|value)$/i.test(title);
10752
10765
  }
10766
+ function metadataColumnName(metadata) {
10767
+ const value = metadata?.columnName;
10768
+ return typeof value === "string" ? cleanText(value, "") || void 0 : void 0;
10769
+ }
10770
+ function metadataTableColumnName(metadata) {
10771
+ const table = metadata?.table;
10772
+ if (!table || typeof table !== "object" || Array.isArray(table)) return void 0;
10773
+ if (!("columnName" in table)) return void 0;
10774
+ const value = table.columnName;
10775
+ return typeof value === "string" ? cleanText(value, "") || void 0 : void 0;
10776
+ }
10753
10777
  function bboxSummary(node) {
10754
10778
  const box = node.bbox?.[0];
10755
10779
  if (!box) return void 0;
@@ -10893,12 +10917,40 @@ function findCellForContinuation(params) {
10893
10917
  }
10894
10918
  return params.cells[1] ?? params.cells[params.cells.length - 1];
10895
10919
  }
10920
+ function columnLabelStartIndex(rows) {
10921
+ const headerIndex = rows.findIndex(
10922
+ ({ row, cells }) => isSourceTreeHeaderRow(row) && cells.length > 1
10923
+ );
10924
+ return headerIndex >= 0 ? headerIndex : 0;
10925
+ }
10926
+ function metadataWithColumnLabel(metadata, label) {
10927
+ const next = {
10928
+ ...metadata ?? {},
10929
+ columnName: label,
10930
+ visualTableRepairColumnLabel: label
10931
+ };
10932
+ if (metadata?.table && typeof metadata.table === "object" && !Array.isArray(metadata.table)) {
10933
+ next.table = {
10934
+ ...metadata.table,
10935
+ columnName: label
10936
+ };
10937
+ }
10938
+ return next;
10939
+ }
10940
+ function tableCellDescription(cell, label) {
10941
+ const text = tableCellText(cell);
10942
+ return cleanText(
10943
+ text && text.toLowerCase() !== label.toLowerCase() ? `${label} | ${text}` : label,
10944
+ cell.description
10945
+ );
10946
+ }
10896
10947
  function applyVisualTableRepair(sourceTree, repair) {
10897
10948
  if (repair.tables.length === 0) return sourceTree;
10898
10949
  const byId = new Map(sourceTree.map((node) => [node.id, node]));
10899
10950
  const byParent = nodesByParent(sourceTree);
10900
10951
  const updates = /* @__PURE__ */ new Map();
10901
10952
  const removeIds = /* @__PURE__ */ new Set();
10953
+ const rowsToRebuildText = /* @__PURE__ */ new Set();
10902
10954
  const currentNode = (id) => updates.get(id) ?? byId.get(id);
10903
10955
  for (const tableRepair of repair.tables) {
10904
10956
  const table = byId.get(tableRepair.tableNodeId);
@@ -10912,20 +10964,40 @@ function applyVisualTableRepair(sourceTree, repair) {
10912
10964
  if (normalized) columnLabels.set(label.columnIndex, normalized);
10913
10965
  }
10914
10966
  if (columnLabels.size > 0) {
10915
- for (const { row, cells } of rows) {
10967
+ const firstLabelRowIndex = columnLabelStartIndex(rows);
10968
+ const normalizedLabels = new Set([...columnLabels.values()].map((label) => label.toLowerCase()));
10969
+ for (const [rowIndex, { row, cells }] of rows.entries()) {
10916
10970
  if (removeIds.has(row.id)) continue;
10971
+ if (rowIndex < firstLabelRowIndex) {
10972
+ for (const cell of cells) {
10973
+ const metadataLabel = metadataColumnName(cell.metadata);
10974
+ if (metadataLabel && isGenericColumnTitle(metadataLabel) && normalizedLabels.has(cleanText(cell.title, "").toLowerCase())) {
10975
+ updates.set(cell.id, {
10976
+ ...currentNode(cell.id) ?? cell,
10977
+ title: metadataLabel,
10978
+ description: tableCellDescription(cell, metadataLabel),
10979
+ metadata: metadataWithColumnLabel(cell.metadata, metadataLabel)
10980
+ });
10981
+ rowsToRebuildText.add(row.id);
10982
+ }
10983
+ }
10984
+ continue;
10985
+ }
10917
10986
  for (const [fallbackIndex, cell] of cells.entries()) {
10918
10987
  const columnIndex = tableCellColumnIndex(cell, fallbackIndex);
10919
10988
  const label = columnLabels.get(columnIndex);
10920
- if (!label || cell.title === label) continue;
10989
+ if (!label) continue;
10990
+ const current = currentNode(cell.id) ?? cell;
10991
+ if (current.title === label && metadataColumnName(current.metadata) === label && (metadataTableColumnName(current.metadata) ?? label) === label) {
10992
+ continue;
10993
+ }
10921
10994
  updates.set(cell.id, {
10922
- ...currentNode(cell.id) ?? cell,
10995
+ ...current,
10923
10996
  title: label,
10924
- metadata: {
10925
- ...cell.metadata ?? {},
10926
- visualTableRepairColumnLabel: label
10927
- }
10997
+ description: tableCellDescription(current, label),
10998
+ metadata: metadataWithColumnLabel(current.metadata, label)
10928
10999
  });
11000
+ rowsToRebuildText.add(row.id);
10929
11001
  }
10930
11002
  }
10931
11003
  }
@@ -10966,6 +11038,7 @@ function applyVisualTableRepair(sourceTree, repair) {
10966
11038
  visualTableRepair: "merged_continuation"
10967
11039
  }
10968
11040
  });
11041
+ rowsToRebuildText.add(targetRow.id);
10969
11042
  }
10970
11043
  }
10971
11044
  const nextRowText = appendDistinctText(targetRow.textExcerpt ?? targetRow.description, sourceText);
@@ -10984,6 +11057,21 @@ function applyVisualTableRepair(sourceTree, repair) {
10984
11057
  for (const sourceCell of sourceCells) removeIds.add(sourceCell.id);
10985
11058
  }
10986
11059
  }
11060
+ for (const rowId of rowsToRebuildText) {
11061
+ if (removeIds.has(rowId)) continue;
11062
+ const row = currentNode(rowId);
11063
+ if (!row || row.kind !== "table_row") continue;
11064
+ const cells = (byParent.get(row.id) ?? []).filter((node) => node.kind === "table_cell" && !removeIds.has(node.id)).map((node) => currentNode(node.id) ?? node).sort(
11065
+ (left, right) => tableCellColumnIndex(left, 0) - tableCellColumnIndex(right, 0) || left.order - right.order || left.id.localeCompare(right.id)
11066
+ );
11067
+ const textExcerpt = tableRowTextFromCells(cells);
11068
+ if (!textExcerpt) continue;
11069
+ updates.set(row.id, {
11070
+ ...row,
11071
+ textExcerpt,
11072
+ description: cleanText([row.title, textExcerpt].filter(Boolean).join(" | "), row.description)
11073
+ });
11074
+ }
10987
11075
  if (updates.size === 0 && removeIds.size === 0) return sourceTree;
10988
11076
  const repaired = sourceTree.filter((node) => !removeIds.has(node.id)).map((node) => updates.get(node.id) ?? node);
10989
11077
  return normalizeDocumentSourceTreePaths(normalizeContainerEvidenceFromChildren(repaired));