@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.js CHANGED
@@ -11104,6 +11104,19 @@ function isSourceTreeHeaderRow(row) {
11104
11104
  function tableCellText(cell) {
11105
11105
  return cleanText(cell.textExcerpt ?? cell.description ?? cell.title, "");
11106
11106
  }
11107
+ function tableCellValueText(cell) {
11108
+ return cleanText(cell.textExcerpt ?? cell.description ?? "", "");
11109
+ }
11110
+ function tableRowTextFromCells(cells) {
11111
+ const text = cells.map((cell) => {
11112
+ const label = cleanText(cell.title, "");
11113
+ const value = tableCellValueText(cell);
11114
+ if (!value) return label;
11115
+ if (!label || value.toLowerCase() === label.toLowerCase()) return value;
11116
+ return `${label}: ${value}`;
11117
+ }).filter(Boolean).join(" | ");
11118
+ return text ? cleanText(text, text) : void 0;
11119
+ }
11107
11120
  function tableRowTextForPrompt(row, cells) {
11108
11121
  return cleanText(
11109
11122
  cells.length ? cells.map(tableCellText).filter(Boolean).join(" | ") : row.textExcerpt ?? row.description ?? row.title,
@@ -11118,6 +11131,17 @@ function isGenericColumnTitle(value) {
11118
11131
  const title = cleanText(value, "");
11119
11132
  return !title || /^(?:column\s+\d+|table cell|value)$/i.test(title);
11120
11133
  }
11134
+ function metadataColumnName(metadata) {
11135
+ const value = metadata?.columnName;
11136
+ return typeof value === "string" ? cleanText(value, "") || void 0 : void 0;
11137
+ }
11138
+ function metadataTableColumnName(metadata) {
11139
+ const table = metadata?.table;
11140
+ if (!table || typeof table !== "object" || Array.isArray(table)) return void 0;
11141
+ if (!("columnName" in table)) return void 0;
11142
+ const value = table.columnName;
11143
+ return typeof value === "string" ? cleanText(value, "") || void 0 : void 0;
11144
+ }
11121
11145
  function bboxSummary(node) {
11122
11146
  const box = node.bbox?.[0];
11123
11147
  if (!box) return void 0;
@@ -11261,12 +11285,40 @@ function findCellForContinuation(params) {
11261
11285
  }
11262
11286
  return params.cells[1] ?? params.cells[params.cells.length - 1];
11263
11287
  }
11288
+ function columnLabelStartIndex(rows) {
11289
+ const headerIndex = rows.findIndex(
11290
+ ({ row, cells }) => isSourceTreeHeaderRow(row) && cells.length > 1
11291
+ );
11292
+ return headerIndex >= 0 ? headerIndex : 0;
11293
+ }
11294
+ function metadataWithColumnLabel(metadata, label) {
11295
+ const next = {
11296
+ ...metadata ?? {},
11297
+ columnName: label,
11298
+ visualTableRepairColumnLabel: label
11299
+ };
11300
+ if (metadata?.table && typeof metadata.table === "object" && !Array.isArray(metadata.table)) {
11301
+ next.table = {
11302
+ ...metadata.table,
11303
+ columnName: label
11304
+ };
11305
+ }
11306
+ return next;
11307
+ }
11308
+ function tableCellDescription(cell, label) {
11309
+ const text = tableCellText(cell);
11310
+ return cleanText(
11311
+ text && text.toLowerCase() !== label.toLowerCase() ? `${label} | ${text}` : label,
11312
+ cell.description
11313
+ );
11314
+ }
11264
11315
  function applyVisualTableRepair(sourceTree, repair) {
11265
11316
  if (repair.tables.length === 0) return sourceTree;
11266
11317
  const byId = new Map(sourceTree.map((node) => [node.id, node]));
11267
11318
  const byParent = nodesByParent(sourceTree);
11268
11319
  const updates = /* @__PURE__ */ new Map();
11269
11320
  const removeIds = /* @__PURE__ */ new Set();
11321
+ const rowsToRebuildText = /* @__PURE__ */ new Set();
11270
11322
  const currentNode = (id) => updates.get(id) ?? byId.get(id);
11271
11323
  for (const tableRepair of repair.tables) {
11272
11324
  const table = byId.get(tableRepair.tableNodeId);
@@ -11280,20 +11332,40 @@ function applyVisualTableRepair(sourceTree, repair) {
11280
11332
  if (normalized) columnLabels.set(label.columnIndex, normalized);
11281
11333
  }
11282
11334
  if (columnLabels.size > 0) {
11283
- for (const { row, cells } of rows) {
11335
+ const firstLabelRowIndex = columnLabelStartIndex(rows);
11336
+ const normalizedLabels = new Set([...columnLabels.values()].map((label) => label.toLowerCase()));
11337
+ for (const [rowIndex, { row, cells }] of rows.entries()) {
11284
11338
  if (removeIds.has(row.id)) continue;
11339
+ if (rowIndex < firstLabelRowIndex) {
11340
+ for (const cell of cells) {
11341
+ const metadataLabel = metadataColumnName(cell.metadata);
11342
+ if (metadataLabel && isGenericColumnTitle(metadataLabel) && normalizedLabels.has(cleanText(cell.title, "").toLowerCase())) {
11343
+ updates.set(cell.id, {
11344
+ ...currentNode(cell.id) ?? cell,
11345
+ title: metadataLabel,
11346
+ description: tableCellDescription(cell, metadataLabel),
11347
+ metadata: metadataWithColumnLabel(cell.metadata, metadataLabel)
11348
+ });
11349
+ rowsToRebuildText.add(row.id);
11350
+ }
11351
+ }
11352
+ continue;
11353
+ }
11285
11354
  for (const [fallbackIndex, cell] of cells.entries()) {
11286
11355
  const columnIndex = tableCellColumnIndex(cell, fallbackIndex);
11287
11356
  const label = columnLabels.get(columnIndex);
11288
- if (!label || cell.title === label) continue;
11357
+ if (!label) continue;
11358
+ const current = currentNode(cell.id) ?? cell;
11359
+ if (current.title === label && metadataColumnName(current.metadata) === label && (metadataTableColumnName(current.metadata) ?? label) === label) {
11360
+ continue;
11361
+ }
11289
11362
  updates.set(cell.id, {
11290
- ...currentNode(cell.id) ?? cell,
11363
+ ...current,
11291
11364
  title: label,
11292
- metadata: {
11293
- ...cell.metadata ?? {},
11294
- visualTableRepairColumnLabel: label
11295
- }
11365
+ description: tableCellDescription(current, label),
11366
+ metadata: metadataWithColumnLabel(current.metadata, label)
11296
11367
  });
11368
+ rowsToRebuildText.add(row.id);
11297
11369
  }
11298
11370
  }
11299
11371
  }
@@ -11334,6 +11406,7 @@ function applyVisualTableRepair(sourceTree, repair) {
11334
11406
  visualTableRepair: "merged_continuation"
11335
11407
  }
11336
11408
  });
11409
+ rowsToRebuildText.add(targetRow.id);
11337
11410
  }
11338
11411
  }
11339
11412
  const nextRowText = appendDistinctText(targetRow.textExcerpt ?? targetRow.description, sourceText);
@@ -11352,6 +11425,21 @@ function applyVisualTableRepair(sourceTree, repair) {
11352
11425
  for (const sourceCell of sourceCells) removeIds.add(sourceCell.id);
11353
11426
  }
11354
11427
  }
11428
+ for (const rowId of rowsToRebuildText) {
11429
+ if (removeIds.has(rowId)) continue;
11430
+ const row = currentNode(rowId);
11431
+ if (!row || row.kind !== "table_row") continue;
11432
+ const cells = (byParent.get(row.id) ?? []).filter((node) => node.kind === "table_cell" && !removeIds.has(node.id)).map((node) => currentNode(node.id) ?? node).sort(
11433
+ (left, right) => tableCellColumnIndex(left, 0) - tableCellColumnIndex(right, 0) || left.order - right.order || left.id.localeCompare(right.id)
11434
+ );
11435
+ const textExcerpt = tableRowTextFromCells(cells);
11436
+ if (!textExcerpt) continue;
11437
+ updates.set(row.id, {
11438
+ ...row,
11439
+ textExcerpt,
11440
+ description: cleanText([row.title, textExcerpt].filter(Boolean).join(" | "), row.description)
11441
+ });
11442
+ }
11355
11443
  if (updates.size === 0 && removeIds.size === 0) return sourceTree;
11356
11444
  const repaired = sourceTree.filter((node) => !removeIds.has(node.id)).map((node) => updates.get(node.id) ?? node);
11357
11445
  return normalizeDocumentSourceTreePaths(normalizeContainerEvidenceFromChildren(repaired));