@absolutejs/absolute 0.19.0-beta.606 → 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.
@@ -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,
@@ -1879,11 +1929,19 @@ var buildGroundingReferenceEvidenceSummary = (reference) => [
1879
1929
  reference.contextLabel,
1880
1930
  reference.provenanceLabel
1881
1931
  ].filter((value) => Boolean(value && value.length > 0)).filter((value, index, values) => values.findIndex((entry) => entry === value) === index).join(" · ");
1932
+ var buildGroundingSectionKey = (reference) => reference.contextLabel ?? reference.locatorLabel ?? reference.label ?? reference.source ?? reference.chunkId;
1933
+ var buildGroundingSectionSummaryLine = (reference) => [
1934
+ reference.source ?? reference.title ?? reference.chunkId,
1935
+ reference.locatorLabel,
1936
+ reference.contextLabel,
1937
+ reference.provenanceLabel
1938
+ ].filter((value) => Boolean(value && value.length > 0)).filter((value, index, values) => values.findIndex((entry) => entry === value) === index).join(" · ");
1882
1939
  var buildGroundedAnswerCitationDetail = (reference) => ({
1883
1940
  contextLabel: reference.contextLabel,
1884
1941
  evidenceLabel: buildGroundingReferenceEvidenceLabel(reference),
1885
1942
  evidenceSummary: buildGroundingReferenceEvidenceSummary(reference),
1886
- excerpt: reference.excerpt,
1943
+ excerpt: selectPreferredExcerpt(reference.excerpts, getContextNumber(reference.metadata?.sectionChunkCount)) || reference.excerpt,
1944
+ excerpts: reference.excerpts,
1887
1945
  label: reference.label,
1888
1946
  locatorLabel: reference.locatorLabel,
1889
1947
  number: reference.number,
@@ -1969,26 +2027,97 @@ var buildRAGGroundedAnswer = (content, sources) => {
1969
2027
  hasCitations,
1970
2028
  parts,
1971
2029
  references,
2030
+ sectionSummaries: buildRAGGroundedAnswerSectionSummaries(references),
1972
2031
  ungroundedReferenceNumbers: [...ungroundedReferenceNumbers].sort((left, right) => left - right)
1973
2032
  };
1974
2033
  };
2034
+ var buildRAGGroundedAnswerSectionSummaries = (references) => {
2035
+ const groups = new Map;
2036
+ for (const reference of references) {
2037
+ const key = buildGroundingSectionKey(reference);
2038
+ const existing = groups.get(key);
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;
2045
+ groups.set(key, {
2046
+ chunkIds: [reference.chunkId],
2047
+ contextLabel: reference.contextLabel,
2048
+ count: 1,
2049
+ excerpt: selectPreferredExcerpt(excerpts, getContextNumber(reference.metadata?.sectionChunkCount)) || excerpts?.sectionExcerpt || reference.excerpt,
2050
+ excerpts,
2051
+ key,
2052
+ label: key,
2053
+ locatorLabel: reference.locatorLabel,
2054
+ provenanceLabel: reference.provenanceLabel,
2055
+ referenceNumbers: [reference.number],
2056
+ references: [reference],
2057
+ summary: buildGroundingSectionSummaryLine(reference) || reference.label || reference.chunkId
2058
+ });
2059
+ continue;
2060
+ }
2061
+ existing.count += 1;
2062
+ if (!existing.chunkIds.includes(reference.chunkId)) {
2063
+ existing.chunkIds.push(reference.chunkId);
2064
+ }
2065
+ if (!existing.referenceNumbers.includes(reference.number)) {
2066
+ existing.referenceNumbers.push(reference.number);
2067
+ }
2068
+ existing.references.push(reference);
2069
+ if (!existing.contextLabel && reference.contextLabel) {
2070
+ existing.contextLabel = reference.contextLabel;
2071
+ }
2072
+ if (!existing.locatorLabel && reference.locatorLabel) {
2073
+ existing.locatorLabel = reference.locatorLabel;
2074
+ }
2075
+ if (!existing.provenanceLabel && reference.provenanceLabel) {
2076
+ existing.provenanceLabel = reference.provenanceLabel;
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
+ }
2086
+ }
2087
+ return [...groups.values()].map((group) => ({
2088
+ ...group,
2089
+ referenceNumbers: [...group.referenceNumbers].sort((left, right) => left - right),
2090
+ references: group.references.slice().sort((left, right) => left.number - right.number)
2091
+ })).sort((left, right) => {
2092
+ const leftFirst = left.referenceNumbers[0] ?? Number.POSITIVE_INFINITY;
2093
+ const rightFirst = right.referenceNumbers[0] ?? Number.POSITIVE_INFINITY;
2094
+ if (leftFirst !== rightFirst) {
2095
+ return leftFirst - rightFirst;
2096
+ }
2097
+ return left.label.localeCompare(right.label);
2098
+ });
2099
+ };
1975
2100
  var buildRAGGroundingReferences = (sources) => {
1976
2101
  const citations = buildRAGCitations(sources);
1977
2102
  const citationReferenceMap = buildRAGCitationReferenceMap(citations);
1978
- return citations.map((citation) => ({
1979
- chunkId: citation.chunkId,
1980
- contextLabel: citation.contextLabel ?? buildContextLabel(citation.metadata),
1981
- excerpt: buildExcerpt(citation.text),
1982
- label: citation.label,
1983
- locatorLabel: citation.locatorLabel ?? buildLocatorLabel(citation.metadata, citation.source, citation.title),
1984
- metadata: citation.metadata,
1985
- number: citationReferenceMap[citation.chunkId] ?? 0,
1986
- provenanceLabel: citation.provenanceLabel ?? buildProvenanceLabel(citation.metadata),
1987
- score: citation.score,
1988
- source: citation.source,
1989
- text: citation.text,
1990
- title: citation.title
1991
- }));
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
+ });
1992
2121
  };
1993
2122
 
1994
2123
  // src/ai/rag/presentation.ts
@@ -2172,7 +2301,7 @@ var buildRAGChunkStructure = (metadata) => {
2172
2301
  return;
2173
2302
  }
2174
2303
  const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.filter((value) => typeof value === "string" && value.trim().length > 0) : undefined;
2175
- 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;
2176
2305
  const section = {
2177
2306
  depth: getContextNumber2(metadata.sectionDepth),
2178
2307
  kind: sectionKind,
@@ -2201,6 +2330,52 @@ var buildExcerpt2 = (text, maxLength = 160) => {
2201
2330
  }
2202
2331
  return `${normalized.slice(0, Math.max(0, maxLength - 1)).trimEnd()}…`;
2203
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
+ };
2204
2379
  var buildRAGChunkGraph = (chunks) => {
2205
2380
  const nodes = [];
2206
2381
  const edges = [];
@@ -2412,6 +2587,7 @@ var buildRAGSourceSummaries = (sources) => {
2412
2587
  return sourceGroups.map((group) => {
2413
2588
  const groupCitations = citations.filter((citation) => group.chunks.some((chunk) => chunk.chunkId === citation.chunkId));
2414
2589
  const leadChunk = group.chunks.slice().sort((left, right) => right.score - left.score)[0];
2590
+ const excerpts = leadChunk ? buildRAGChunkExcerpts(group.chunks, leadChunk.chunkId) : undefined;
2415
2591
  return {
2416
2592
  bestScore: group.bestScore,
2417
2593
  citationNumbers: groupCitations.map((citation) => citationReferenceMap[citation.chunkId] ?? 0),
@@ -2419,7 +2595,8 @@ var buildRAGSourceSummaries = (sources) => {
2419
2595
  chunkIds: group.chunks.map((chunk) => chunk.chunkId),
2420
2596
  contextLabel: leadChunk?.labels?.contextLabel ?? buildContextLabel2(leadChunk?.metadata),
2421
2597
  count: group.count,
2422
- excerpt: buildExcerpt2(leadChunk?.text ?? ""),
2598
+ excerpt: buildRAGPreferredExcerpt(excerpts, leadChunk?.structure ?? buildRAGChunkStructure(leadChunk?.metadata)) || buildExcerpt2(leadChunk?.text ?? ""),
2599
+ excerpts,
2423
2600
  key: group.key,
2424
2601
  label: group.label,
2425
2602
  locatorLabel: leadChunk?.labels?.locatorLabel ?? buildLocatorLabel2(leadChunk?.metadata, leadChunk?.source, leadChunk?.title),
@@ -1014,6 +1014,56 @@ var buildExcerpt = (text, maxLength = 160) => {
1014
1014
  }
1015
1015
  return `${normalized.slice(0, Math.max(0, maxLength - 1)).trimEnd()}\u2026`;
1016
1016
  };
1017
+ var selectPreferredExcerpt = (excerpts, sectionChunkCount) => {
1018
+ if (!excerpts) {
1019
+ return "";
1020
+ }
1021
+ const chunkExcerpt = excerpts.chunkExcerpt?.trim() ?? "";
1022
+ const windowExcerpt = excerpts.windowExcerpt?.trim() ?? "";
1023
+ const sectionExcerpt = excerpts.sectionExcerpt?.trim() ?? "";
1024
+ if (sectionChunkCount && sectionChunkCount > 1 && chunkExcerpt.length > 0 && chunkExcerpt.length < 72) {
1025
+ if (sectionChunkCount <= 3 && sectionExcerpt) {
1026
+ return sectionExcerpt;
1027
+ }
1028
+ if (windowExcerpt) {
1029
+ return windowExcerpt;
1030
+ }
1031
+ }
1032
+ return chunkExcerpt || windowExcerpt || sectionExcerpt;
1033
+ };
1034
+ var buildGroundingChunkExcerpts = (sources, activeChunkId) => {
1035
+ if (sources.length === 0) {
1036
+ return;
1037
+ }
1038
+ const activeSource = (activeChunkId ? sources.find((source) => source.chunkId === activeChunkId) : undefined) ?? sources[0];
1039
+ if (!activeSource) {
1040
+ return;
1041
+ }
1042
+ const chunkMap = new Map(sources.map((source) => [source.chunkId, source]));
1043
+ const activeMetadata = activeSource.metadata ?? {};
1044
+ const previousChunkId = getContextString(activeMetadata.previousChunkId);
1045
+ const nextChunkId = getContextString(activeMetadata.nextChunkId);
1046
+ const sectionChunkId = getContextString(activeMetadata.sectionChunkId);
1047
+ const sectionSources = sectionChunkId ? sources.filter((source) => getContextString(source.metadata?.sectionChunkId) === sectionChunkId).sort((left, right) => {
1048
+ const leftIndex = getContextNumber(left.metadata?.sectionChunkIndex) ?? Number.MAX_SAFE_INTEGER;
1049
+ const rightIndex = getContextNumber(right.metadata?.sectionChunkIndex) ?? Number.MAX_SAFE_INTEGER;
1050
+ if (leftIndex !== rightIndex) {
1051
+ return leftIndex - rightIndex;
1052
+ }
1053
+ return left.chunkId.localeCompare(right.chunkId);
1054
+ }) : [activeSource];
1055
+ const collectText = (chunkIds) => chunkIds.map((chunkId) => chunkMap.get(chunkId)?.text).filter((text) => typeof text === "string").join(`
1056
+
1057
+ `);
1058
+ const orderedWindowIds = [previousChunkId, activeSource.chunkId, nextChunkId].filter((chunkId, index, values) => Boolean(chunkId) && values.indexOf(chunkId) === index);
1059
+ return {
1060
+ chunkExcerpt: buildExcerpt(activeSource.text, 160),
1061
+ sectionExcerpt: buildExcerpt(sectionSources.map((source) => source.text).join(`
1062
+
1063
+ `), 320),
1064
+ windowExcerpt: buildExcerpt(collectText(orderedWindowIds), 240)
1065
+ };
1066
+ };
1017
1067
  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(" \xB7 ");
1018
1068
  var buildGroundingReferenceEvidenceSummary = (reference) => [
1019
1069
  reference.source ?? reference.title ?? reference.chunkId,
@@ -1021,11 +1071,19 @@ var buildGroundingReferenceEvidenceSummary = (reference) => [
1021
1071
  reference.contextLabel,
1022
1072
  reference.provenanceLabel
1023
1073
  ].filter((value) => Boolean(value && value.length > 0)).filter((value, index, values) => values.findIndex((entry) => entry === value) === index).join(" \xB7 ");
1074
+ var buildGroundingSectionKey = (reference) => reference.contextLabel ?? reference.locatorLabel ?? reference.label ?? reference.source ?? reference.chunkId;
1075
+ var buildGroundingSectionSummaryLine = (reference) => [
1076
+ reference.source ?? reference.title ?? reference.chunkId,
1077
+ reference.locatorLabel,
1078
+ reference.contextLabel,
1079
+ reference.provenanceLabel
1080
+ ].filter((value) => Boolean(value && value.length > 0)).filter((value, index, values) => values.findIndex((entry) => entry === value) === index).join(" \xB7 ");
1024
1081
  var buildGroundedAnswerCitationDetail = (reference) => ({
1025
1082
  contextLabel: reference.contextLabel,
1026
1083
  evidenceLabel: buildGroundingReferenceEvidenceLabel(reference),
1027
1084
  evidenceSummary: buildGroundingReferenceEvidenceSummary(reference),
1028
- excerpt: reference.excerpt,
1085
+ excerpt: selectPreferredExcerpt(reference.excerpts, getContextNumber(reference.metadata?.sectionChunkCount)) || reference.excerpt,
1086
+ excerpts: reference.excerpts,
1029
1087
  label: reference.label,
1030
1088
  locatorLabel: reference.locatorLabel,
1031
1089
  number: reference.number,
@@ -1111,26 +1169,97 @@ var buildRAGGroundedAnswer = (content, sources) => {
1111
1169
  hasCitations,
1112
1170
  parts,
1113
1171
  references,
1172
+ sectionSummaries: buildRAGGroundedAnswerSectionSummaries(references),
1114
1173
  ungroundedReferenceNumbers: [...ungroundedReferenceNumbers].sort((left, right) => left - right)
1115
1174
  };
1116
1175
  };
1176
+ var buildRAGGroundedAnswerSectionSummaries = (references) => {
1177
+ const groups = new Map;
1178
+ for (const reference of references) {
1179
+ const key = buildGroundingSectionKey(reference);
1180
+ const existing = groups.get(key);
1181
+ if (!existing) {
1182
+ const excerpts = reference.excerpts ? {
1183
+ chunkExcerpt: reference.excerpts.chunkExcerpt,
1184
+ sectionExcerpt: reference.excerpts.sectionExcerpt,
1185
+ windowExcerpt: reference.excerpts.windowExcerpt
1186
+ } : undefined;
1187
+ groups.set(key, {
1188
+ chunkIds: [reference.chunkId],
1189
+ contextLabel: reference.contextLabel,
1190
+ count: 1,
1191
+ excerpt: selectPreferredExcerpt(excerpts, getContextNumber(reference.metadata?.sectionChunkCount)) || excerpts?.sectionExcerpt || reference.excerpt,
1192
+ excerpts,
1193
+ key,
1194
+ label: key,
1195
+ locatorLabel: reference.locatorLabel,
1196
+ provenanceLabel: reference.provenanceLabel,
1197
+ referenceNumbers: [reference.number],
1198
+ references: [reference],
1199
+ summary: buildGroundingSectionSummaryLine(reference) || reference.label || reference.chunkId
1200
+ });
1201
+ continue;
1202
+ }
1203
+ existing.count += 1;
1204
+ if (!existing.chunkIds.includes(reference.chunkId)) {
1205
+ existing.chunkIds.push(reference.chunkId);
1206
+ }
1207
+ if (!existing.referenceNumbers.includes(reference.number)) {
1208
+ existing.referenceNumbers.push(reference.number);
1209
+ }
1210
+ existing.references.push(reference);
1211
+ if (!existing.contextLabel && reference.contextLabel) {
1212
+ existing.contextLabel = reference.contextLabel;
1213
+ }
1214
+ if (!existing.locatorLabel && reference.locatorLabel) {
1215
+ existing.locatorLabel = reference.locatorLabel;
1216
+ }
1217
+ if (!existing.provenanceLabel && reference.provenanceLabel) {
1218
+ existing.provenanceLabel = reference.provenanceLabel;
1219
+ }
1220
+ if (!existing.excerpts && reference.excerpts) {
1221
+ existing.excerpts = {
1222
+ chunkExcerpt: reference.excerpts.chunkExcerpt,
1223
+ sectionExcerpt: reference.excerpts.sectionExcerpt,
1224
+ windowExcerpt: reference.excerpts.windowExcerpt
1225
+ };
1226
+ existing.excerpt = reference.excerpts.sectionExcerpt;
1227
+ }
1228
+ }
1229
+ return [...groups.values()].map((group) => ({
1230
+ ...group,
1231
+ referenceNumbers: [...group.referenceNumbers].sort((left, right) => left - right),
1232
+ references: group.references.slice().sort((left, right) => left.number - right.number)
1233
+ })).sort((left, right) => {
1234
+ const leftFirst = left.referenceNumbers[0] ?? Number.POSITIVE_INFINITY;
1235
+ const rightFirst = right.referenceNumbers[0] ?? Number.POSITIVE_INFINITY;
1236
+ if (leftFirst !== rightFirst) {
1237
+ return leftFirst - rightFirst;
1238
+ }
1239
+ return left.label.localeCompare(right.label);
1240
+ });
1241
+ };
1117
1242
  var buildRAGGroundingReferences = (sources) => {
1118
1243
  const citations = buildRAGCitations(sources);
1119
1244
  const citationReferenceMap = buildRAGCitationReferenceMap(citations);
1120
- return citations.map((citation) => ({
1121
- chunkId: citation.chunkId,
1122
- contextLabel: citation.contextLabel ?? buildContextLabel(citation.metadata),
1123
- excerpt: buildExcerpt(citation.text),
1124
- label: citation.label,
1125
- locatorLabel: citation.locatorLabel ?? buildLocatorLabel(citation.metadata, citation.source, citation.title),
1126
- metadata: citation.metadata,
1127
- number: citationReferenceMap[citation.chunkId] ?? 0,
1128
- provenanceLabel: citation.provenanceLabel ?? buildProvenanceLabel(citation.metadata),
1129
- score: citation.score,
1130
- source: citation.source,
1131
- text: citation.text,
1132
- title: citation.title
1133
- }));
1245
+ return citations.map((citation) => {
1246
+ const excerpts = buildGroundingChunkExcerpts(sources, citation.chunkId);
1247
+ return {
1248
+ chunkId: citation.chunkId,
1249
+ contextLabel: citation.contextLabel ?? buildContextLabel(citation.metadata),
1250
+ excerpt: selectPreferredExcerpt(excerpts, getContextNumber(citation.metadata?.sectionChunkCount)) || excerpts?.chunkExcerpt || buildExcerpt(citation.text),
1251
+ excerpts,
1252
+ label: citation.label,
1253
+ locatorLabel: citation.locatorLabel ?? buildLocatorLabel(citation.metadata, citation.source, citation.title),
1254
+ metadata: citation.metadata,
1255
+ number: citationReferenceMap[citation.chunkId] ?? 0,
1256
+ provenanceLabel: citation.provenanceLabel ?? buildProvenanceLabel(citation.metadata),
1257
+ score: citation.score,
1258
+ source: citation.source,
1259
+ text: citation.text,
1260
+ title: citation.title
1261
+ };
1262
+ });
1134
1263
  };
1135
1264
 
1136
1265
  // src/ai/rag/presentation.ts
@@ -1719,7 +1848,7 @@ var buildRAGChunkStructure = (metadata) => {
1719
1848
  return;
1720
1849
  }
1721
1850
  const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.filter((value) => typeof value === "string" && value.trim().length > 0) : undefined;
1722
- const sectionKind = metadata.sectionKind === "markdown_heading" || metadata.sectionKind === "html_heading" ? metadata.sectionKind : undefined;
1851
+ 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;
1723
1852
  const section = {
1724
1853
  depth: getContextNumber2(metadata.sectionDepth),
1725
1854
  kind: sectionKind,
@@ -1748,6 +1877,52 @@ var buildExcerpt2 = (text, maxLength = 160) => {
1748
1877
  }
1749
1878
  return `${normalized.slice(0, Math.max(0, maxLength - 1)).trimEnd()}\u2026`;
1750
1879
  };
1880
+ var buildRAGChunkExcerpts = (chunks, activeChunkId) => {
1881
+ if (chunks.length === 0) {
1882
+ return;
1883
+ }
1884
+ const graph = buildRAGChunkGraph(chunks.map((chunk) => ({
1885
+ chunkId: chunk.chunkId,
1886
+ metadata: chunk.metadata,
1887
+ structure: chunk.structure
1888
+ })));
1889
+ const navigation = buildRAGChunkGraphNavigation(graph, activeChunkId);
1890
+ const activeChunk = chunks.find((chunk) => chunk.chunkId === navigation.activeChunkId) ?? chunks[0];
1891
+ if (!activeChunk) {
1892
+ return;
1893
+ }
1894
+ const chunkMap = new Map(chunks.map((chunk) => [chunk.chunkId, chunk]));
1895
+ const orderedWindowIds = [
1896
+ navigation.previousNode?.chunkId,
1897
+ activeChunk.chunkId,
1898
+ navigation.nextNode?.chunkId
1899
+ ].filter((chunkId, index, ids) => Boolean(chunkId) && ids.indexOf(chunkId) === index);
1900
+ const orderedSectionIds = navigation.sectionNodes.length > 0 ? navigation.sectionNodes.map((node) => node.chunkId) : [activeChunk.chunkId];
1901
+ const collectText = (chunkIds) => chunkIds.map((chunkId) => chunkMap.get(chunkId)?.text).filter((text) => typeof text === "string").join(`
1902
+
1903
+ `);
1904
+ return {
1905
+ chunkExcerpt: buildExcerpt2(activeChunk.text, 160),
1906
+ sectionExcerpt: buildExcerpt2(collectText(orderedSectionIds), 320),
1907
+ windowExcerpt: buildExcerpt2(collectText(orderedWindowIds), 240)
1908
+ };
1909
+ };
1910
+ var buildRAGPreferredExcerpt = (excerpts, structure) => {
1911
+ if (!excerpts) {
1912
+ return "";
1913
+ }
1914
+ const chunkLength = excerpts.chunkExcerpt.trim().length;
1915
+ const sectionChunkCount = structure?.sequence?.sectionChunkCount ?? 1;
1916
+ if (sectionChunkCount > 1 && chunkLength > 0 && chunkLength < 72) {
1917
+ if (sectionChunkCount <= 3 && excerpts.sectionExcerpt.trim().length > 0) {
1918
+ return excerpts.sectionExcerpt;
1919
+ }
1920
+ if (excerpts.windowExcerpt.trim().length > 0) {
1921
+ return excerpts.windowExcerpt;
1922
+ }
1923
+ }
1924
+ return excerpts.chunkExcerpt;
1925
+ };
1751
1926
  var buildRAGChunkGraph = (chunks) => {
1752
1927
  const nodes = [];
1753
1928
  const edges = [];
@@ -1959,6 +2134,7 @@ var buildRAGSourceSummaries = (sources) => {
1959
2134
  return sourceGroups.map((group) => {
1960
2135
  const groupCitations = citations.filter((citation) => group.chunks.some((chunk) => chunk.chunkId === citation.chunkId));
1961
2136
  const leadChunk = group.chunks.slice().sort((left, right) => right.score - left.score)[0];
2137
+ const excerpts = leadChunk ? buildRAGChunkExcerpts(group.chunks, leadChunk.chunkId) : undefined;
1962
2138
  return {
1963
2139
  bestScore: group.bestScore,
1964
2140
  citationNumbers: groupCitations.map((citation) => citationReferenceMap[citation.chunkId] ?? 0),
@@ -1966,7 +2142,8 @@ var buildRAGSourceSummaries = (sources) => {
1966
2142
  chunkIds: group.chunks.map((chunk) => chunk.chunkId),
1967
2143
  contextLabel: leadChunk?.labels?.contextLabel ?? buildContextLabel2(leadChunk?.metadata),
1968
2144
  count: group.count,
1969
- excerpt: buildExcerpt2(leadChunk?.text ?? ""),
2145
+ excerpt: buildRAGPreferredExcerpt(excerpts, leadChunk?.structure ?? buildRAGChunkStructure(leadChunk?.metadata)) || buildExcerpt2(leadChunk?.text ?? ""),
2146
+ excerpts,
1970
2147
  key: group.key,
1971
2148
  label: group.label,
1972
2149
  locatorLabel: leadChunk?.labels?.locatorLabel ?? buildLocatorLabel2(leadChunk?.metadata, leadChunk?.source, leadChunk?.title),
@@ -4134,5 +4311,5 @@ export {
4134
4311
  AIStreamService
4135
4312
  };
4136
4313
 
4137
- //# debugId=6BA4A5140094B73664756E2164756E21
4314
+ //# debugId=CBE86239F7337F8064756E2164756E21
4138
4315
  //# sourceMappingURL=index.js.map