@absolutejs/absolute 0.19.0-beta.607 → 0.19.0-beta.608

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.
@@ -869,6 +869,56 @@ var buildExcerpt = (text, maxLength = 160) => {
869
869
  }
870
870
  return `${normalized.slice(0, Math.max(0, maxLength - 1)).trimEnd()}…`;
871
871
  };
872
+ var selectPreferredExcerpt = (excerpts, sectionChunkCount) => {
873
+ if (!excerpts) {
874
+ return "";
875
+ }
876
+ const chunkExcerpt = excerpts.chunkExcerpt?.trim() ?? "";
877
+ const windowExcerpt = excerpts.windowExcerpt?.trim() ?? "";
878
+ const sectionExcerpt = excerpts.sectionExcerpt?.trim() ?? "";
879
+ if (sectionChunkCount && sectionChunkCount > 1 && chunkExcerpt.length > 0 && chunkExcerpt.length < 72) {
880
+ if (sectionChunkCount <= 3 && sectionExcerpt) {
881
+ return sectionExcerpt;
882
+ }
883
+ if (windowExcerpt) {
884
+ return windowExcerpt;
885
+ }
886
+ }
887
+ return chunkExcerpt || windowExcerpt || sectionExcerpt;
888
+ };
889
+ var buildGroundingChunkExcerpts = (sources, activeChunkId) => {
890
+ if (sources.length === 0) {
891
+ return;
892
+ }
893
+ const activeSource = (activeChunkId ? sources.find((source) => source.chunkId === activeChunkId) : undefined) ?? sources[0];
894
+ if (!activeSource) {
895
+ return;
896
+ }
897
+ const chunkMap = new Map(sources.map((source) => [source.chunkId, source]));
898
+ const activeMetadata = activeSource.metadata ?? {};
899
+ const previousChunkId = getContextString(activeMetadata.previousChunkId);
900
+ const nextChunkId = getContextString(activeMetadata.nextChunkId);
901
+ const sectionChunkId = getContextString(activeMetadata.sectionChunkId);
902
+ const sectionSources = sectionChunkId ? sources.filter((source) => getContextString(source.metadata?.sectionChunkId) === sectionChunkId).sort((left, right) => {
903
+ const leftIndex = getContextNumber(left.metadata?.sectionChunkIndex) ?? Number.MAX_SAFE_INTEGER;
904
+ const rightIndex = getContextNumber(right.metadata?.sectionChunkIndex) ?? Number.MAX_SAFE_INTEGER;
905
+ if (leftIndex !== rightIndex) {
906
+ return leftIndex - rightIndex;
907
+ }
908
+ return left.chunkId.localeCompare(right.chunkId);
909
+ }) : [activeSource];
910
+ const collectText = (chunkIds) => chunkIds.map((chunkId) => chunkMap.get(chunkId)?.text).filter((text) => typeof text === "string").join(`
911
+
912
+ `);
913
+ const orderedWindowIds = [previousChunkId, activeSource.chunkId, nextChunkId].filter((chunkId, index, values) => Boolean(chunkId) && values.indexOf(chunkId) === index);
914
+ return {
915
+ chunkExcerpt: buildExcerpt(activeSource.text, 160),
916
+ sectionExcerpt: buildExcerpt(sectionSources.map((source) => source.text).join(`
917
+
918
+ `), 320),
919
+ windowExcerpt: buildExcerpt(collectText(orderedWindowIds), 240)
920
+ };
921
+ };
872
922
  var buildGroundingReferenceEvidenceLabel = (reference) => [reference.label, reference.locatorLabel, reference.contextLabel].filter((value) => Boolean(value && value.length > 0)).filter((value, index, values) => values.findIndex((entry) => entry === value) === index).join(" · ");
873
923
  var buildGroundingReferenceEvidenceSummary = (reference) => [
874
924
  reference.source ?? reference.title ?? reference.chunkId,
@@ -887,7 +937,8 @@ var buildGroundedAnswerCitationDetail = (reference) => ({
887
937
  contextLabel: reference.contextLabel,
888
938
  evidenceLabel: buildGroundingReferenceEvidenceLabel(reference),
889
939
  evidenceSummary: buildGroundingReferenceEvidenceSummary(reference),
890
- excerpt: reference.excerpt,
940
+ excerpt: selectPreferredExcerpt(reference.excerpts, getContextNumber(reference.metadata?.sectionChunkCount)) || reference.excerpt,
941
+ excerpts: reference.excerpts,
891
942
  label: reference.label,
892
943
  locatorLabel: reference.locatorLabel,
893
944
  number: reference.number,
@@ -983,10 +1034,17 @@ var buildRAGGroundedAnswerSectionSummaries = (references) => {
983
1034
  const key = buildGroundingSectionKey(reference);
984
1035
  const existing = groups.get(key);
985
1036
  if (!existing) {
1037
+ const excerpts = reference.excerpts ? {
1038
+ chunkExcerpt: reference.excerpts.chunkExcerpt,
1039
+ sectionExcerpt: reference.excerpts.sectionExcerpt,
1040
+ windowExcerpt: reference.excerpts.windowExcerpt
1041
+ } : undefined;
986
1042
  groups.set(key, {
987
1043
  chunkIds: [reference.chunkId],
988
1044
  contextLabel: reference.contextLabel,
989
1045
  count: 1,
1046
+ excerpt: selectPreferredExcerpt(excerpts, getContextNumber(reference.metadata?.sectionChunkCount)) || excerpts?.sectionExcerpt || reference.excerpt,
1047
+ excerpts,
990
1048
  key,
991
1049
  label: key,
992
1050
  locatorLabel: reference.locatorLabel,
@@ -1014,6 +1072,14 @@ var buildRAGGroundedAnswerSectionSummaries = (references) => {
1014
1072
  if (!existing.provenanceLabel && reference.provenanceLabel) {
1015
1073
  existing.provenanceLabel = reference.provenanceLabel;
1016
1074
  }
1075
+ if (!existing.excerpts && reference.excerpts) {
1076
+ existing.excerpts = {
1077
+ chunkExcerpt: reference.excerpts.chunkExcerpt,
1078
+ sectionExcerpt: reference.excerpts.sectionExcerpt,
1079
+ windowExcerpt: reference.excerpts.windowExcerpt
1080
+ };
1081
+ existing.excerpt = reference.excerpts.sectionExcerpt;
1082
+ }
1017
1083
  }
1018
1084
  return [...groups.values()].map((group) => ({
1019
1085
  ...group,
@@ -1031,20 +1097,24 @@ var buildRAGGroundedAnswerSectionSummaries = (references) => {
1031
1097
  var buildRAGGroundingReferences = (sources) => {
1032
1098
  const citations = buildRAGCitations(sources);
1033
1099
  const citationReferenceMap = buildRAGCitationReferenceMap(citations);
1034
- return citations.map((citation) => ({
1035
- chunkId: citation.chunkId,
1036
- contextLabel: citation.contextLabel ?? buildContextLabel(citation.metadata),
1037
- excerpt: buildExcerpt(citation.text),
1038
- label: citation.label,
1039
- locatorLabel: citation.locatorLabel ?? buildLocatorLabel(citation.metadata, citation.source, citation.title),
1040
- metadata: citation.metadata,
1041
- number: citationReferenceMap[citation.chunkId] ?? 0,
1042
- provenanceLabel: citation.provenanceLabel ?? buildProvenanceLabel(citation.metadata),
1043
- score: citation.score,
1044
- source: citation.source,
1045
- text: citation.text,
1046
- title: citation.title
1047
- }));
1100
+ return citations.map((citation) => {
1101
+ const excerpts = buildGroundingChunkExcerpts(sources, citation.chunkId);
1102
+ return {
1103
+ chunkId: citation.chunkId,
1104
+ contextLabel: citation.contextLabel ?? buildContextLabel(citation.metadata),
1105
+ excerpt: selectPreferredExcerpt(excerpts, getContextNumber(citation.metadata?.sectionChunkCount)) || excerpts?.chunkExcerpt || buildExcerpt(citation.text),
1106
+ excerpts,
1107
+ label: citation.label,
1108
+ locatorLabel: citation.locatorLabel ?? buildLocatorLabel(citation.metadata, citation.source, citation.title),
1109
+ metadata: citation.metadata,
1110
+ number: citationReferenceMap[citation.chunkId] ?? 0,
1111
+ provenanceLabel: citation.provenanceLabel ?? buildProvenanceLabel(citation.metadata),
1112
+ score: citation.score,
1113
+ source: citation.source,
1114
+ text: citation.text,
1115
+ title: citation.title
1116
+ };
1117
+ });
1048
1118
  };
1049
1119
 
1050
1120
  // src/ai/rag/presentation.ts
@@ -1228,7 +1298,7 @@ var buildRAGChunkStructure = (metadata) => {
1228
1298
  return;
1229
1299
  }
1230
1300
  const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.filter((value) => typeof value === "string" && value.trim().length > 0) : undefined;
1231
- const sectionKind = metadata.sectionKind === "markdown_heading" || metadata.sectionKind === "html_heading" ? metadata.sectionKind : undefined;
1301
+ const sectionKind = metadata.sectionKind === "markdown_heading" || metadata.sectionKind === "html_heading" || metadata.sectionKind === "office_heading" || metadata.sectionKind === "spreadsheet_rows" || metadata.sectionKind === "presentation_slide" ? metadata.sectionKind : undefined;
1232
1302
  const section = {
1233
1303
  depth: getContextNumber2(metadata.sectionDepth),
1234
1304
  kind: sectionKind,
@@ -1257,6 +1327,52 @@ var buildExcerpt2 = (text, maxLength = 160) => {
1257
1327
  }
1258
1328
  return `${normalized.slice(0, Math.max(0, maxLength - 1)).trimEnd()}…`;
1259
1329
  };
1330
+ var buildRAGChunkExcerpts = (chunks, activeChunkId) => {
1331
+ if (chunks.length === 0) {
1332
+ return;
1333
+ }
1334
+ const graph = buildRAGChunkGraph(chunks.map((chunk) => ({
1335
+ chunkId: chunk.chunkId,
1336
+ metadata: chunk.metadata,
1337
+ structure: chunk.structure
1338
+ })));
1339
+ const navigation = buildRAGChunkGraphNavigation(graph, activeChunkId);
1340
+ const activeChunk = chunks.find((chunk) => chunk.chunkId === navigation.activeChunkId) ?? chunks[0];
1341
+ if (!activeChunk) {
1342
+ return;
1343
+ }
1344
+ const chunkMap = new Map(chunks.map((chunk) => [chunk.chunkId, chunk]));
1345
+ const orderedWindowIds = [
1346
+ navigation.previousNode?.chunkId,
1347
+ activeChunk.chunkId,
1348
+ navigation.nextNode?.chunkId
1349
+ ].filter((chunkId, index, ids) => Boolean(chunkId) && ids.indexOf(chunkId) === index);
1350
+ const orderedSectionIds = navigation.sectionNodes.length > 0 ? navigation.sectionNodes.map((node) => node.chunkId) : [activeChunk.chunkId];
1351
+ const collectText = (chunkIds) => chunkIds.map((chunkId) => chunkMap.get(chunkId)?.text).filter((text) => typeof text === "string").join(`
1352
+
1353
+ `);
1354
+ return {
1355
+ chunkExcerpt: buildExcerpt2(activeChunk.text, 160),
1356
+ sectionExcerpt: buildExcerpt2(collectText(orderedSectionIds), 320),
1357
+ windowExcerpt: buildExcerpt2(collectText(orderedWindowIds), 240)
1358
+ };
1359
+ };
1360
+ var buildRAGPreferredExcerpt = (excerpts, structure) => {
1361
+ if (!excerpts) {
1362
+ return "";
1363
+ }
1364
+ const chunkLength = excerpts.chunkExcerpt.trim().length;
1365
+ const sectionChunkCount = structure?.sequence?.sectionChunkCount ?? 1;
1366
+ if (sectionChunkCount > 1 && chunkLength > 0 && chunkLength < 72) {
1367
+ if (sectionChunkCount <= 3 && excerpts.sectionExcerpt.trim().length > 0) {
1368
+ return excerpts.sectionExcerpt;
1369
+ }
1370
+ if (excerpts.windowExcerpt.trim().length > 0) {
1371
+ return excerpts.windowExcerpt;
1372
+ }
1373
+ }
1374
+ return excerpts.chunkExcerpt;
1375
+ };
1260
1376
  var buildRAGChunkGraph = (chunks) => {
1261
1377
  const nodes = [];
1262
1378
  const edges = [];
@@ -1468,6 +1584,7 @@ var buildRAGSourceSummaries = (sources) => {
1468
1584
  return sourceGroups.map((group) => {
1469
1585
  const groupCitations = citations.filter((citation) => group.chunks.some((chunk) => chunk.chunkId === citation.chunkId));
1470
1586
  const leadChunk = group.chunks.slice().sort((left, right) => right.score - left.score)[0];
1587
+ const excerpts = leadChunk ? buildRAGChunkExcerpts(group.chunks, leadChunk.chunkId) : undefined;
1471
1588
  return {
1472
1589
  bestScore: group.bestScore,
1473
1590
  citationNumbers: groupCitations.map((citation) => citationReferenceMap[citation.chunkId] ?? 0),
@@ -1475,7 +1592,8 @@ var buildRAGSourceSummaries = (sources) => {
1475
1592
  chunkIds: group.chunks.map((chunk) => chunk.chunkId),
1476
1593
  contextLabel: leadChunk?.labels?.contextLabel ?? buildContextLabel2(leadChunk?.metadata),
1477
1594
  count: group.count,
1478
- excerpt: buildExcerpt2(leadChunk?.text ?? ""),
1595
+ excerpt: buildRAGPreferredExcerpt(excerpts, leadChunk?.structure ?? buildRAGChunkStructure(leadChunk?.metadata)) || buildExcerpt2(leadChunk?.text ?? ""),
1596
+ excerpts,
1479
1597
  key: group.key,
1480
1598
  label: group.label,
1481
1599
  locatorLabel: leadChunk?.labels?.locatorLabel ?? buildLocatorLabel2(leadChunk?.metadata, leadChunk?.source, leadChunk?.title),
@@ -829,6 +829,56 @@ var buildExcerpt = (text, maxLength = 160) => {
829
829
  }
830
830
  return `${normalized.slice(0, Math.max(0, maxLength - 1)).trimEnd()}…`;
831
831
  };
832
+ var selectPreferredExcerpt = (excerpts, sectionChunkCount) => {
833
+ if (!excerpts) {
834
+ return "";
835
+ }
836
+ const chunkExcerpt = excerpts.chunkExcerpt?.trim() ?? "";
837
+ const windowExcerpt = excerpts.windowExcerpt?.trim() ?? "";
838
+ const sectionExcerpt = excerpts.sectionExcerpt?.trim() ?? "";
839
+ if (sectionChunkCount && sectionChunkCount > 1 && chunkExcerpt.length > 0 && chunkExcerpt.length < 72) {
840
+ if (sectionChunkCount <= 3 && sectionExcerpt) {
841
+ return sectionExcerpt;
842
+ }
843
+ if (windowExcerpt) {
844
+ return windowExcerpt;
845
+ }
846
+ }
847
+ return chunkExcerpt || windowExcerpt || sectionExcerpt;
848
+ };
849
+ var buildGroundingChunkExcerpts = (sources, activeChunkId) => {
850
+ if (sources.length === 0) {
851
+ return;
852
+ }
853
+ const activeSource = (activeChunkId ? sources.find((source) => source.chunkId === activeChunkId) : undefined) ?? sources[0];
854
+ if (!activeSource) {
855
+ return;
856
+ }
857
+ const chunkMap = new Map(sources.map((source) => [source.chunkId, source]));
858
+ const activeMetadata = activeSource.metadata ?? {};
859
+ const previousChunkId = getContextString(activeMetadata.previousChunkId);
860
+ const nextChunkId = getContextString(activeMetadata.nextChunkId);
861
+ const sectionChunkId = getContextString(activeMetadata.sectionChunkId);
862
+ const sectionSources = sectionChunkId ? sources.filter((source) => getContextString(source.metadata?.sectionChunkId) === sectionChunkId).sort((left, right) => {
863
+ const leftIndex = getContextNumber(left.metadata?.sectionChunkIndex) ?? Number.MAX_SAFE_INTEGER;
864
+ const rightIndex = getContextNumber(right.metadata?.sectionChunkIndex) ?? Number.MAX_SAFE_INTEGER;
865
+ if (leftIndex !== rightIndex) {
866
+ return leftIndex - rightIndex;
867
+ }
868
+ return left.chunkId.localeCompare(right.chunkId);
869
+ }) : [activeSource];
870
+ const collectText = (chunkIds) => chunkIds.map((chunkId) => chunkMap.get(chunkId)?.text).filter((text) => typeof text === "string").join(`
871
+
872
+ `);
873
+ const orderedWindowIds = [previousChunkId, activeSource.chunkId, nextChunkId].filter((chunkId, index, values) => Boolean(chunkId) && values.indexOf(chunkId) === index);
874
+ return {
875
+ chunkExcerpt: buildExcerpt(activeSource.text, 160),
876
+ sectionExcerpt: buildExcerpt(sectionSources.map((source) => source.text).join(`
877
+
878
+ `), 320),
879
+ windowExcerpt: buildExcerpt(collectText(orderedWindowIds), 240)
880
+ };
881
+ };
832
882
  var buildGroundingReferenceEvidenceLabel = (reference) => [reference.label, reference.locatorLabel, reference.contextLabel].filter((value) => Boolean(value && value.length > 0)).filter((value, index, values) => values.findIndex((entry) => entry === value) === index).join(" · ");
833
883
  var buildGroundingReferenceEvidenceSummary = (reference) => [
834
884
  reference.source ?? reference.title ?? reference.chunkId,
@@ -847,7 +897,8 @@ var buildGroundedAnswerCitationDetail = (reference) => ({
847
897
  contextLabel: reference.contextLabel,
848
898
  evidenceLabel: buildGroundingReferenceEvidenceLabel(reference),
849
899
  evidenceSummary: buildGroundingReferenceEvidenceSummary(reference),
850
- excerpt: reference.excerpt,
900
+ excerpt: selectPreferredExcerpt(reference.excerpts, getContextNumber(reference.metadata?.sectionChunkCount)) || reference.excerpt,
901
+ excerpts: reference.excerpts,
851
902
  label: reference.label,
852
903
  locatorLabel: reference.locatorLabel,
853
904
  number: reference.number,
@@ -943,10 +994,17 @@ var buildRAGGroundedAnswerSectionSummaries = (references) => {
943
994
  const key = buildGroundingSectionKey(reference);
944
995
  const existing = groups.get(key);
945
996
  if (!existing) {
997
+ const excerpts = reference.excerpts ? {
998
+ chunkExcerpt: reference.excerpts.chunkExcerpt,
999
+ sectionExcerpt: reference.excerpts.sectionExcerpt,
1000
+ windowExcerpt: reference.excerpts.windowExcerpt
1001
+ } : undefined;
946
1002
  groups.set(key, {
947
1003
  chunkIds: [reference.chunkId],
948
1004
  contextLabel: reference.contextLabel,
949
1005
  count: 1,
1006
+ excerpt: selectPreferredExcerpt(excerpts, getContextNumber(reference.metadata?.sectionChunkCount)) || excerpts?.sectionExcerpt || reference.excerpt,
1007
+ excerpts,
950
1008
  key,
951
1009
  label: key,
952
1010
  locatorLabel: reference.locatorLabel,
@@ -974,6 +1032,14 @@ var buildRAGGroundedAnswerSectionSummaries = (references) => {
974
1032
  if (!existing.provenanceLabel && reference.provenanceLabel) {
975
1033
  existing.provenanceLabel = reference.provenanceLabel;
976
1034
  }
1035
+ if (!existing.excerpts && reference.excerpts) {
1036
+ existing.excerpts = {
1037
+ chunkExcerpt: reference.excerpts.chunkExcerpt,
1038
+ sectionExcerpt: reference.excerpts.sectionExcerpt,
1039
+ windowExcerpt: reference.excerpts.windowExcerpt
1040
+ };
1041
+ existing.excerpt = reference.excerpts.sectionExcerpt;
1042
+ }
977
1043
  }
978
1044
  return [...groups.values()].map((group) => ({
979
1045
  ...group,
@@ -991,20 +1057,24 @@ var buildRAGGroundedAnswerSectionSummaries = (references) => {
991
1057
  var buildRAGGroundingReferences = (sources) => {
992
1058
  const citations = buildRAGCitations(sources);
993
1059
  const citationReferenceMap = buildRAGCitationReferenceMap(citations);
994
- return citations.map((citation) => ({
995
- chunkId: citation.chunkId,
996
- contextLabel: citation.contextLabel ?? buildContextLabel(citation.metadata),
997
- excerpt: buildExcerpt(citation.text),
998
- label: citation.label,
999
- locatorLabel: citation.locatorLabel ?? buildLocatorLabel(citation.metadata, citation.source, citation.title),
1000
- metadata: citation.metadata,
1001
- number: citationReferenceMap[citation.chunkId] ?? 0,
1002
- provenanceLabel: citation.provenanceLabel ?? buildProvenanceLabel(citation.metadata),
1003
- score: citation.score,
1004
- source: citation.source,
1005
- text: citation.text,
1006
- title: citation.title
1007
- }));
1060
+ return citations.map((citation) => {
1061
+ const excerpts = buildGroundingChunkExcerpts(sources, citation.chunkId);
1062
+ return {
1063
+ chunkId: citation.chunkId,
1064
+ contextLabel: citation.contextLabel ?? buildContextLabel(citation.metadata),
1065
+ excerpt: selectPreferredExcerpt(excerpts, getContextNumber(citation.metadata?.sectionChunkCount)) || excerpts?.chunkExcerpt || buildExcerpt(citation.text),
1066
+ excerpts,
1067
+ label: citation.label,
1068
+ locatorLabel: citation.locatorLabel ?? buildLocatorLabel(citation.metadata, citation.source, citation.title),
1069
+ metadata: citation.metadata,
1070
+ number: citationReferenceMap[citation.chunkId] ?? 0,
1071
+ provenanceLabel: citation.provenanceLabel ?? buildProvenanceLabel(citation.metadata),
1072
+ score: citation.score,
1073
+ source: citation.source,
1074
+ text: citation.text,
1075
+ title: citation.title
1076
+ };
1077
+ });
1008
1078
  };
1009
1079
 
1010
1080
  // src/ai/rag/presentation.ts
@@ -1188,7 +1258,7 @@ var buildRAGChunkStructure = (metadata) => {
1188
1258
  return;
1189
1259
  }
1190
1260
  const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.filter((value) => typeof value === "string" && value.trim().length > 0) : undefined;
1191
- const sectionKind = metadata.sectionKind === "markdown_heading" || metadata.sectionKind === "html_heading" ? metadata.sectionKind : undefined;
1261
+ const sectionKind = metadata.sectionKind === "markdown_heading" || metadata.sectionKind === "html_heading" || metadata.sectionKind === "office_heading" || metadata.sectionKind === "spreadsheet_rows" || metadata.sectionKind === "presentation_slide" ? metadata.sectionKind : undefined;
1192
1262
  const section = {
1193
1263
  depth: getContextNumber2(metadata.sectionDepth),
1194
1264
  kind: sectionKind,
@@ -1217,6 +1287,52 @@ var buildExcerpt2 = (text, maxLength = 160) => {
1217
1287
  }
1218
1288
  return `${normalized.slice(0, Math.max(0, maxLength - 1)).trimEnd()}…`;
1219
1289
  };
1290
+ var buildRAGChunkExcerpts = (chunks, activeChunkId) => {
1291
+ if (chunks.length === 0) {
1292
+ return;
1293
+ }
1294
+ const graph = buildRAGChunkGraph(chunks.map((chunk) => ({
1295
+ chunkId: chunk.chunkId,
1296
+ metadata: chunk.metadata,
1297
+ structure: chunk.structure
1298
+ })));
1299
+ const navigation = buildRAGChunkGraphNavigation(graph, activeChunkId);
1300
+ const activeChunk = chunks.find((chunk) => chunk.chunkId === navigation.activeChunkId) ?? chunks[0];
1301
+ if (!activeChunk) {
1302
+ return;
1303
+ }
1304
+ const chunkMap = new Map(chunks.map((chunk) => [chunk.chunkId, chunk]));
1305
+ const orderedWindowIds = [
1306
+ navigation.previousNode?.chunkId,
1307
+ activeChunk.chunkId,
1308
+ navigation.nextNode?.chunkId
1309
+ ].filter((chunkId, index, ids) => Boolean(chunkId) && ids.indexOf(chunkId) === index);
1310
+ const orderedSectionIds = navigation.sectionNodes.length > 0 ? navigation.sectionNodes.map((node) => node.chunkId) : [activeChunk.chunkId];
1311
+ const collectText = (chunkIds) => chunkIds.map((chunkId) => chunkMap.get(chunkId)?.text).filter((text) => typeof text === "string").join(`
1312
+
1313
+ `);
1314
+ return {
1315
+ chunkExcerpt: buildExcerpt2(activeChunk.text, 160),
1316
+ sectionExcerpt: buildExcerpt2(collectText(orderedSectionIds), 320),
1317
+ windowExcerpt: buildExcerpt2(collectText(orderedWindowIds), 240)
1318
+ };
1319
+ };
1320
+ var buildRAGPreferredExcerpt = (excerpts, structure) => {
1321
+ if (!excerpts) {
1322
+ return "";
1323
+ }
1324
+ const chunkLength = excerpts.chunkExcerpt.trim().length;
1325
+ const sectionChunkCount = structure?.sequence?.sectionChunkCount ?? 1;
1326
+ if (sectionChunkCount > 1 && chunkLength > 0 && chunkLength < 72) {
1327
+ if (sectionChunkCount <= 3 && excerpts.sectionExcerpt.trim().length > 0) {
1328
+ return excerpts.sectionExcerpt;
1329
+ }
1330
+ if (excerpts.windowExcerpt.trim().length > 0) {
1331
+ return excerpts.windowExcerpt;
1332
+ }
1333
+ }
1334
+ return excerpts.chunkExcerpt;
1335
+ };
1220
1336
  var buildRAGChunkGraph = (chunks) => {
1221
1337
  const nodes = [];
1222
1338
  const edges = [];
@@ -1428,6 +1544,7 @@ var buildRAGSourceSummaries = (sources) => {
1428
1544
  return sourceGroups.map((group) => {
1429
1545
  const groupCitations = citations.filter((citation) => group.chunks.some((chunk) => chunk.chunkId === citation.chunkId));
1430
1546
  const leadChunk = group.chunks.slice().sort((left, right) => right.score - left.score)[0];
1547
+ const excerpts = leadChunk ? buildRAGChunkExcerpts(group.chunks, leadChunk.chunkId) : undefined;
1431
1548
  return {
1432
1549
  bestScore: group.bestScore,
1433
1550
  citationNumbers: groupCitations.map((citation) => citationReferenceMap[citation.chunkId] ?? 0),
@@ -1435,7 +1552,8 @@ var buildRAGSourceSummaries = (sources) => {
1435
1552
  chunkIds: group.chunks.map((chunk) => chunk.chunkId),
1436
1553
  contextLabel: leadChunk?.labels?.contextLabel ?? buildContextLabel2(leadChunk?.metadata),
1437
1554
  count: group.count,
1438
- excerpt: buildExcerpt2(leadChunk?.text ?? ""),
1555
+ excerpt: buildRAGPreferredExcerpt(excerpts, leadChunk?.structure ?? buildRAGChunkStructure(leadChunk?.metadata)) || buildExcerpt2(leadChunk?.text ?? ""),
1556
+ excerpts,
1439
1557
  key: group.key,
1440
1558
  label: group.label,
1441
1559
  locatorLabel: leadChunk?.labels?.locatorLabel ?? buildLocatorLabel2(leadChunk?.metadata, leadChunk?.source, leadChunk?.title),
@@ -1872,6 +1872,56 @@ var buildExcerpt = (text, maxLength = 160) => {
1872
1872
  }
1873
1873
  return `${normalized.slice(0, Math.max(0, maxLength - 1)).trimEnd()}…`;
1874
1874
  };
1875
+ var selectPreferredExcerpt = (excerpts, sectionChunkCount) => {
1876
+ if (!excerpts) {
1877
+ return "";
1878
+ }
1879
+ const chunkExcerpt = excerpts.chunkExcerpt?.trim() ?? "";
1880
+ const windowExcerpt = excerpts.windowExcerpt?.trim() ?? "";
1881
+ const sectionExcerpt = excerpts.sectionExcerpt?.trim() ?? "";
1882
+ if (sectionChunkCount && sectionChunkCount > 1 && chunkExcerpt.length > 0 && chunkExcerpt.length < 72) {
1883
+ if (sectionChunkCount <= 3 && sectionExcerpt) {
1884
+ return sectionExcerpt;
1885
+ }
1886
+ if (windowExcerpt) {
1887
+ return windowExcerpt;
1888
+ }
1889
+ }
1890
+ return chunkExcerpt || windowExcerpt || sectionExcerpt;
1891
+ };
1892
+ var buildGroundingChunkExcerpts = (sources, activeChunkId) => {
1893
+ if (sources.length === 0) {
1894
+ return;
1895
+ }
1896
+ const activeSource = (activeChunkId ? sources.find((source) => source.chunkId === activeChunkId) : undefined) ?? sources[0];
1897
+ if (!activeSource) {
1898
+ return;
1899
+ }
1900
+ const chunkMap = new Map(sources.map((source) => [source.chunkId, source]));
1901
+ const activeMetadata = activeSource.metadata ?? {};
1902
+ const previousChunkId = getContextString(activeMetadata.previousChunkId);
1903
+ const nextChunkId = getContextString(activeMetadata.nextChunkId);
1904
+ const sectionChunkId = getContextString(activeMetadata.sectionChunkId);
1905
+ const sectionSources = sectionChunkId ? sources.filter((source) => getContextString(source.metadata?.sectionChunkId) === sectionChunkId).sort((left, right) => {
1906
+ const leftIndex = getContextNumber(left.metadata?.sectionChunkIndex) ?? Number.MAX_SAFE_INTEGER;
1907
+ const rightIndex = getContextNumber(right.metadata?.sectionChunkIndex) ?? Number.MAX_SAFE_INTEGER;
1908
+ if (leftIndex !== rightIndex) {
1909
+ return leftIndex - rightIndex;
1910
+ }
1911
+ return left.chunkId.localeCompare(right.chunkId);
1912
+ }) : [activeSource];
1913
+ const collectText = (chunkIds) => chunkIds.map((chunkId) => chunkMap.get(chunkId)?.text).filter((text) => typeof text === "string").join(`
1914
+
1915
+ `);
1916
+ const orderedWindowIds = [previousChunkId, activeSource.chunkId, nextChunkId].filter((chunkId, index, values) => Boolean(chunkId) && values.indexOf(chunkId) === index);
1917
+ return {
1918
+ chunkExcerpt: buildExcerpt(activeSource.text, 160),
1919
+ sectionExcerpt: buildExcerpt(sectionSources.map((source) => source.text).join(`
1920
+
1921
+ `), 320),
1922
+ windowExcerpt: buildExcerpt(collectText(orderedWindowIds), 240)
1923
+ };
1924
+ };
1875
1925
  var buildGroundingReferenceEvidenceLabel = (reference) => [reference.label, reference.locatorLabel, reference.contextLabel].filter((value) => Boolean(value && value.length > 0)).filter((value, index, values) => values.findIndex((entry) => entry === value) === index).join(" · ");
1876
1926
  var buildGroundingReferenceEvidenceSummary = (reference) => [
1877
1927
  reference.source ?? reference.title ?? reference.chunkId,
@@ -1890,7 +1940,8 @@ var buildGroundedAnswerCitationDetail = (reference) => ({
1890
1940
  contextLabel: reference.contextLabel,
1891
1941
  evidenceLabel: buildGroundingReferenceEvidenceLabel(reference),
1892
1942
  evidenceSummary: buildGroundingReferenceEvidenceSummary(reference),
1893
- excerpt: reference.excerpt,
1943
+ excerpt: selectPreferredExcerpt(reference.excerpts, getContextNumber(reference.metadata?.sectionChunkCount)) || reference.excerpt,
1944
+ excerpts: reference.excerpts,
1894
1945
  label: reference.label,
1895
1946
  locatorLabel: reference.locatorLabel,
1896
1947
  number: reference.number,
@@ -1986,10 +2037,17 @@ var buildRAGGroundedAnswerSectionSummaries = (references) => {
1986
2037
  const key = buildGroundingSectionKey(reference);
1987
2038
  const existing = groups.get(key);
1988
2039
  if (!existing) {
2040
+ const excerpts = reference.excerpts ? {
2041
+ chunkExcerpt: reference.excerpts.chunkExcerpt,
2042
+ sectionExcerpt: reference.excerpts.sectionExcerpt,
2043
+ windowExcerpt: reference.excerpts.windowExcerpt
2044
+ } : undefined;
1989
2045
  groups.set(key, {
1990
2046
  chunkIds: [reference.chunkId],
1991
2047
  contextLabel: reference.contextLabel,
1992
2048
  count: 1,
2049
+ excerpt: selectPreferredExcerpt(excerpts, getContextNumber(reference.metadata?.sectionChunkCount)) || excerpts?.sectionExcerpt || reference.excerpt,
2050
+ excerpts,
1993
2051
  key,
1994
2052
  label: key,
1995
2053
  locatorLabel: reference.locatorLabel,
@@ -2017,6 +2075,14 @@ var buildRAGGroundedAnswerSectionSummaries = (references) => {
2017
2075
  if (!existing.provenanceLabel && reference.provenanceLabel) {
2018
2076
  existing.provenanceLabel = reference.provenanceLabel;
2019
2077
  }
2078
+ if (!existing.excerpts && reference.excerpts) {
2079
+ existing.excerpts = {
2080
+ chunkExcerpt: reference.excerpts.chunkExcerpt,
2081
+ sectionExcerpt: reference.excerpts.sectionExcerpt,
2082
+ windowExcerpt: reference.excerpts.windowExcerpt
2083
+ };
2084
+ existing.excerpt = reference.excerpts.sectionExcerpt;
2085
+ }
2020
2086
  }
2021
2087
  return [...groups.values()].map((group) => ({
2022
2088
  ...group,
@@ -2034,20 +2100,24 @@ var buildRAGGroundedAnswerSectionSummaries = (references) => {
2034
2100
  var buildRAGGroundingReferences = (sources) => {
2035
2101
  const citations = buildRAGCitations(sources);
2036
2102
  const citationReferenceMap = buildRAGCitationReferenceMap(citations);
2037
- return citations.map((citation) => ({
2038
- chunkId: citation.chunkId,
2039
- contextLabel: citation.contextLabel ?? buildContextLabel(citation.metadata),
2040
- excerpt: buildExcerpt(citation.text),
2041
- label: citation.label,
2042
- locatorLabel: citation.locatorLabel ?? buildLocatorLabel(citation.metadata, citation.source, citation.title),
2043
- metadata: citation.metadata,
2044
- number: citationReferenceMap[citation.chunkId] ?? 0,
2045
- provenanceLabel: citation.provenanceLabel ?? buildProvenanceLabel(citation.metadata),
2046
- score: citation.score,
2047
- source: citation.source,
2048
- text: citation.text,
2049
- title: citation.title
2050
- }));
2103
+ return citations.map((citation) => {
2104
+ const excerpts = buildGroundingChunkExcerpts(sources, citation.chunkId);
2105
+ return {
2106
+ chunkId: citation.chunkId,
2107
+ contextLabel: citation.contextLabel ?? buildContextLabel(citation.metadata),
2108
+ excerpt: selectPreferredExcerpt(excerpts, getContextNumber(citation.metadata?.sectionChunkCount)) || excerpts?.chunkExcerpt || buildExcerpt(citation.text),
2109
+ excerpts,
2110
+ label: citation.label,
2111
+ locatorLabel: citation.locatorLabel ?? buildLocatorLabel(citation.metadata, citation.source, citation.title),
2112
+ metadata: citation.metadata,
2113
+ number: citationReferenceMap[citation.chunkId] ?? 0,
2114
+ provenanceLabel: citation.provenanceLabel ?? buildProvenanceLabel(citation.metadata),
2115
+ score: citation.score,
2116
+ source: citation.source,
2117
+ text: citation.text,
2118
+ title: citation.title
2119
+ };
2120
+ });
2051
2121
  };
2052
2122
 
2053
2123
  // src/ai/rag/presentation.ts
@@ -2231,7 +2301,7 @@ var buildRAGChunkStructure = (metadata) => {
2231
2301
  return;
2232
2302
  }
2233
2303
  const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.filter((value) => typeof value === "string" && value.trim().length > 0) : undefined;
2234
- const sectionKind = metadata.sectionKind === "markdown_heading" || metadata.sectionKind === "html_heading" ? metadata.sectionKind : undefined;
2304
+ const sectionKind = metadata.sectionKind === "markdown_heading" || metadata.sectionKind === "html_heading" || metadata.sectionKind === "office_heading" || metadata.sectionKind === "spreadsheet_rows" || metadata.sectionKind === "presentation_slide" ? metadata.sectionKind : undefined;
2235
2305
  const section = {
2236
2306
  depth: getContextNumber2(metadata.sectionDepth),
2237
2307
  kind: sectionKind,
@@ -2260,6 +2330,52 @@ var buildExcerpt2 = (text, maxLength = 160) => {
2260
2330
  }
2261
2331
  return `${normalized.slice(0, Math.max(0, maxLength - 1)).trimEnd()}…`;
2262
2332
  };
2333
+ var buildRAGChunkExcerpts = (chunks, activeChunkId) => {
2334
+ if (chunks.length === 0) {
2335
+ return;
2336
+ }
2337
+ const graph = buildRAGChunkGraph(chunks.map((chunk) => ({
2338
+ chunkId: chunk.chunkId,
2339
+ metadata: chunk.metadata,
2340
+ structure: chunk.structure
2341
+ })));
2342
+ const navigation = buildRAGChunkGraphNavigation(graph, activeChunkId);
2343
+ const activeChunk = chunks.find((chunk) => chunk.chunkId === navigation.activeChunkId) ?? chunks[0];
2344
+ if (!activeChunk) {
2345
+ return;
2346
+ }
2347
+ const chunkMap = new Map(chunks.map((chunk) => [chunk.chunkId, chunk]));
2348
+ const orderedWindowIds = [
2349
+ navigation.previousNode?.chunkId,
2350
+ activeChunk.chunkId,
2351
+ navigation.nextNode?.chunkId
2352
+ ].filter((chunkId, index, ids) => Boolean(chunkId) && ids.indexOf(chunkId) === index);
2353
+ const orderedSectionIds = navigation.sectionNodes.length > 0 ? navigation.sectionNodes.map((node) => node.chunkId) : [activeChunk.chunkId];
2354
+ const collectText = (chunkIds) => chunkIds.map((chunkId) => chunkMap.get(chunkId)?.text).filter((text) => typeof text === "string").join(`
2355
+
2356
+ `);
2357
+ return {
2358
+ chunkExcerpt: buildExcerpt2(activeChunk.text, 160),
2359
+ sectionExcerpt: buildExcerpt2(collectText(orderedSectionIds), 320),
2360
+ windowExcerpt: buildExcerpt2(collectText(orderedWindowIds), 240)
2361
+ };
2362
+ };
2363
+ var buildRAGPreferredExcerpt = (excerpts, structure) => {
2364
+ if (!excerpts) {
2365
+ return "";
2366
+ }
2367
+ const chunkLength = excerpts.chunkExcerpt.trim().length;
2368
+ const sectionChunkCount = structure?.sequence?.sectionChunkCount ?? 1;
2369
+ if (sectionChunkCount > 1 && chunkLength > 0 && chunkLength < 72) {
2370
+ if (sectionChunkCount <= 3 && excerpts.sectionExcerpt.trim().length > 0) {
2371
+ return excerpts.sectionExcerpt;
2372
+ }
2373
+ if (excerpts.windowExcerpt.trim().length > 0) {
2374
+ return excerpts.windowExcerpt;
2375
+ }
2376
+ }
2377
+ return excerpts.chunkExcerpt;
2378
+ };
2263
2379
  var buildRAGChunkGraph = (chunks) => {
2264
2380
  const nodes = [];
2265
2381
  const edges = [];
@@ -2471,6 +2587,7 @@ var buildRAGSourceSummaries = (sources) => {
2471
2587
  return sourceGroups.map((group) => {
2472
2588
  const groupCitations = citations.filter((citation) => group.chunks.some((chunk) => chunk.chunkId === citation.chunkId));
2473
2589
  const leadChunk = group.chunks.slice().sort((left, right) => right.score - left.score)[0];
2590
+ const excerpts = leadChunk ? buildRAGChunkExcerpts(group.chunks, leadChunk.chunkId) : undefined;
2474
2591
  return {
2475
2592
  bestScore: group.bestScore,
2476
2593
  citationNumbers: groupCitations.map((citation) => citationReferenceMap[citation.chunkId] ?? 0),
@@ -2478,7 +2595,8 @@ var buildRAGSourceSummaries = (sources) => {
2478
2595
  chunkIds: group.chunks.map((chunk) => chunk.chunkId),
2479
2596
  contextLabel: leadChunk?.labels?.contextLabel ?? buildContextLabel2(leadChunk?.metadata),
2480
2597
  count: group.count,
2481
- excerpt: buildExcerpt2(leadChunk?.text ?? ""),
2598
+ excerpt: buildRAGPreferredExcerpt(excerpts, leadChunk?.structure ?? buildRAGChunkStructure(leadChunk?.metadata)) || buildExcerpt2(leadChunk?.text ?? ""),
2599
+ excerpts,
2482
2600
  key: group.key,
2483
2601
  label: group.label,
2484
2602
  locatorLabel: leadChunk?.labels?.locatorLabel ?? buildLocatorLabel2(leadChunk?.metadata, leadChunk?.source, leadChunk?.title),