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

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.
Files changed (38) hide show
  1. package/dist/ai/client/index.js +142 -18
  2. package/dist/ai/client/index.js.map +4 -4
  3. package/dist/ai/client/ui.js +143 -18
  4. package/dist/ai/client/ui.js.map +4 -4
  5. package/dist/ai/index.js +371 -28
  6. package/dist/ai/index.js.map +7 -7
  7. package/dist/ai/rag/quality.js +92 -16
  8. package/dist/ai/rag/quality.js.map +3 -3
  9. package/dist/ai/rag/ui.js +143 -18
  10. package/dist/ai/rag/ui.js.map +4 -4
  11. package/dist/ai-client/angular/ai/index.js +141 -17
  12. package/dist/ai-client/react/ai/index.js +141 -17
  13. package/dist/ai-client/vue/ai/index.js +141 -17
  14. package/dist/angular/ai/index.js +142 -18
  15. package/dist/angular/ai/index.js.map +4 -4
  16. package/dist/angular/index.js +2 -2
  17. package/dist/angular/index.js.map +1 -1
  18. package/dist/angular/server.js +2 -2
  19. package/dist/angular/server.js.map +1 -1
  20. package/dist/build.js +2 -2
  21. package/dist/build.js.map +1 -1
  22. package/dist/index.js +2 -2
  23. package/dist/index.js.map +1 -1
  24. package/dist/react/ai/index.js +142 -18
  25. package/dist/react/ai/index.js.map +6 -6
  26. package/dist/src/ai/client/ui.d.ts +1 -1
  27. package/dist/src/ai/rag/index.d.ts +1 -1
  28. package/dist/src/ai/rag/presentation.d.ts +4 -1
  29. package/dist/src/ai/rag/ui.d.ts +1 -1
  30. package/dist/src/vue/ai/useRAG.d.ts +14 -4
  31. package/dist/src/vue/ai/useRAGChunkPreview.d.ts +12 -2
  32. package/dist/src/vue/ai/useRAGSearch.d.ts +2 -2
  33. package/dist/svelte/ai/index.js +142 -18
  34. package/dist/svelte/ai/index.js.map +6 -6
  35. package/dist/types/ai.d.ts +15 -2
  36. package/dist/vue/ai/index.js +142 -18
  37. package/dist/vue/ai/index.js.map +5 -5
  38. package/package.json +1 -1
@@ -1872,6 +1872,60 @@ 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 = [
1917
+ previousChunkId,
1918
+ activeSource.chunkId,
1919
+ nextChunkId
1920
+ ].filter((chunkId, index, values) => Boolean(chunkId) && values.indexOf(chunkId) === index);
1921
+ return {
1922
+ chunkExcerpt: buildExcerpt(activeSource.text, 160),
1923
+ sectionExcerpt: buildExcerpt(sectionSources.map((source) => source.text).join(`
1924
+
1925
+ `), 320),
1926
+ windowExcerpt: buildExcerpt(collectText(orderedWindowIds), 240)
1927
+ };
1928
+ };
1875
1929
  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
1930
  var buildGroundingReferenceEvidenceSummary = (reference) => [
1877
1931
  reference.source ?? reference.title ?? reference.chunkId,
@@ -1890,7 +1944,8 @@ var buildGroundedAnswerCitationDetail = (reference) => ({
1890
1944
  contextLabel: reference.contextLabel,
1891
1945
  evidenceLabel: buildGroundingReferenceEvidenceLabel(reference),
1892
1946
  evidenceSummary: buildGroundingReferenceEvidenceSummary(reference),
1893
- excerpt: reference.excerpt,
1947
+ excerpt: selectPreferredExcerpt(reference.excerpts, getContextNumber(reference.metadata?.sectionChunkCount)) || reference.excerpt,
1948
+ excerpts: reference.excerpts,
1894
1949
  label: reference.label,
1895
1950
  locatorLabel: reference.locatorLabel,
1896
1951
  number: reference.number,
@@ -1910,6 +1965,8 @@ var buildRAGCitations = (sources) => {
1910
1965
  unique.set(key, {
1911
1966
  chunkId: source.chunkId,
1912
1967
  contextLabel: source.labels?.contextLabel ?? buildContextLabel(source.metadata),
1968
+ excerpt: selectPreferredExcerpt(buildGroundingChunkExcerpts(sources, source.chunkId), getContextNumber(source.metadata?.sectionChunkCount)) || buildExcerpt(source.text),
1969
+ excerpts: buildGroundingChunkExcerpts(sources, source.chunkId),
1913
1970
  key,
1914
1971
  label: buildSourceLabel(source),
1915
1972
  locatorLabel: source.labels?.locatorLabel ?? buildLocatorLabel(source.metadata, source.source, source.title),
@@ -1986,10 +2043,17 @@ var buildRAGGroundedAnswerSectionSummaries = (references) => {
1986
2043
  const key = buildGroundingSectionKey(reference);
1987
2044
  const existing = groups.get(key);
1988
2045
  if (!existing) {
2046
+ const excerpts = reference.excerpts ? {
2047
+ chunkExcerpt: reference.excerpts.chunkExcerpt,
2048
+ sectionExcerpt: reference.excerpts.sectionExcerpt,
2049
+ windowExcerpt: reference.excerpts.windowExcerpt
2050
+ } : undefined;
1989
2051
  groups.set(key, {
1990
2052
  chunkIds: [reference.chunkId],
1991
2053
  contextLabel: reference.contextLabel,
1992
2054
  count: 1,
2055
+ excerpt: selectPreferredExcerpt(excerpts, getContextNumber(reference.metadata?.sectionChunkCount)) || excerpts?.sectionExcerpt || reference.excerpt,
2056
+ excerpts,
1993
2057
  key,
1994
2058
  label: key,
1995
2059
  locatorLabel: reference.locatorLabel,
@@ -2017,6 +2081,14 @@ var buildRAGGroundedAnswerSectionSummaries = (references) => {
2017
2081
  if (!existing.provenanceLabel && reference.provenanceLabel) {
2018
2082
  existing.provenanceLabel = reference.provenanceLabel;
2019
2083
  }
2084
+ if (!existing.excerpts && reference.excerpts) {
2085
+ existing.excerpts = {
2086
+ chunkExcerpt: reference.excerpts.chunkExcerpt,
2087
+ sectionExcerpt: reference.excerpts.sectionExcerpt,
2088
+ windowExcerpt: reference.excerpts.windowExcerpt
2089
+ };
2090
+ existing.excerpt = reference.excerpts.sectionExcerpt;
2091
+ }
2020
2092
  }
2021
2093
  return [...groups.values()].map((group) => ({
2022
2094
  ...group,
@@ -2034,20 +2106,24 @@ var buildRAGGroundedAnswerSectionSummaries = (references) => {
2034
2106
  var buildRAGGroundingReferences = (sources) => {
2035
2107
  const citations = buildRAGCitations(sources);
2036
2108
  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
- }));
2109
+ return citations.map((citation) => {
2110
+ const excerpts = buildGroundingChunkExcerpts(sources, citation.chunkId);
2111
+ return {
2112
+ chunkId: citation.chunkId,
2113
+ contextLabel: citation.contextLabel ?? buildContextLabel(citation.metadata),
2114
+ excerpt: selectPreferredExcerpt(excerpts, getContextNumber(citation.metadata?.sectionChunkCount)) || excerpts?.chunkExcerpt || buildExcerpt(citation.text),
2115
+ excerpts,
2116
+ label: citation.label,
2117
+ locatorLabel: citation.locatorLabel ?? buildLocatorLabel(citation.metadata, citation.source, citation.title),
2118
+ metadata: citation.metadata,
2119
+ number: citationReferenceMap[citation.chunkId] ?? 0,
2120
+ provenanceLabel: citation.provenanceLabel ?? buildProvenanceLabel(citation.metadata),
2121
+ score: citation.score,
2122
+ source: citation.source,
2123
+ text: citation.text,
2124
+ title: citation.title
2125
+ };
2126
+ });
2051
2127
  };
2052
2128
 
2053
2129
  // src/ai/rag/presentation.ts
@@ -2231,7 +2307,7 @@ var buildRAGChunkStructure = (metadata) => {
2231
2307
  return;
2232
2308
  }
2233
2309
  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;
2310
+ 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
2311
  const section = {
2236
2312
  depth: getContextNumber2(metadata.sectionDepth),
2237
2313
  kind: sectionKind,
@@ -2260,6 +2336,52 @@ var buildExcerpt2 = (text, maxLength = 160) => {
2260
2336
  }
2261
2337
  return `${normalized.slice(0, Math.max(0, maxLength - 1)).trimEnd()}…`;
2262
2338
  };
2339
+ var buildRAGChunkExcerpts = (chunks, activeChunkId) => {
2340
+ if (chunks.length === 0) {
2341
+ return;
2342
+ }
2343
+ const graph = buildRAGChunkGraph(chunks.map((chunk) => ({
2344
+ chunkId: chunk.chunkId,
2345
+ metadata: chunk.metadata,
2346
+ structure: chunk.structure
2347
+ })));
2348
+ const navigation = buildRAGChunkGraphNavigation(graph, activeChunkId);
2349
+ const activeChunk = chunks.find((chunk) => chunk.chunkId === navigation.activeChunkId) ?? chunks[0];
2350
+ if (!activeChunk) {
2351
+ return;
2352
+ }
2353
+ const chunkMap = new Map(chunks.map((chunk) => [chunk.chunkId, chunk]));
2354
+ const orderedWindowIds = [
2355
+ navigation.previousNode?.chunkId,
2356
+ activeChunk.chunkId,
2357
+ navigation.nextNode?.chunkId
2358
+ ].filter((chunkId, index, ids) => Boolean(chunkId) && ids.indexOf(chunkId) === index);
2359
+ const orderedSectionIds = navigation.sectionNodes.length > 0 ? navigation.sectionNodes.map((node) => node.chunkId) : [activeChunk.chunkId];
2360
+ const collectText = (chunkIds) => chunkIds.map((chunkId) => chunkMap.get(chunkId)?.text).filter((text) => typeof text === "string").join(`
2361
+
2362
+ `);
2363
+ return {
2364
+ chunkExcerpt: buildExcerpt2(activeChunk.text, 160),
2365
+ sectionExcerpt: buildExcerpt2(collectText(orderedSectionIds), 320),
2366
+ windowExcerpt: buildExcerpt2(collectText(orderedWindowIds), 240)
2367
+ };
2368
+ };
2369
+ var buildRAGPreferredExcerpt = (excerpts, structure) => {
2370
+ if (!excerpts) {
2371
+ return "";
2372
+ }
2373
+ const chunkLength = excerpts.chunkExcerpt.trim().length;
2374
+ const sectionChunkCount = structure?.sequence?.sectionChunkCount ?? 1;
2375
+ if (sectionChunkCount > 1 && chunkLength > 0 && chunkLength < 72) {
2376
+ if (sectionChunkCount <= 3 && excerpts.sectionExcerpt.trim().length > 0) {
2377
+ return excerpts.sectionExcerpt;
2378
+ }
2379
+ if (excerpts.windowExcerpt.trim().length > 0) {
2380
+ return excerpts.windowExcerpt;
2381
+ }
2382
+ }
2383
+ return excerpts.chunkExcerpt;
2384
+ };
2263
2385
  var buildRAGChunkGraph = (chunks) => {
2264
2386
  const nodes = [];
2265
2387
  const edges = [];
@@ -2471,6 +2593,7 @@ var buildRAGSourceSummaries = (sources) => {
2471
2593
  return sourceGroups.map((group) => {
2472
2594
  const groupCitations = citations.filter((citation) => group.chunks.some((chunk) => chunk.chunkId === citation.chunkId));
2473
2595
  const leadChunk = group.chunks.slice().sort((left, right) => right.score - left.score)[0];
2596
+ const excerpts = leadChunk ? buildRAGChunkExcerpts(group.chunks, leadChunk.chunkId) : undefined;
2474
2597
  return {
2475
2598
  bestScore: group.bestScore,
2476
2599
  citationNumbers: groupCitations.map((citation) => citationReferenceMap[citation.chunkId] ?? 0),
@@ -2478,7 +2601,8 @@ var buildRAGSourceSummaries = (sources) => {
2478
2601
  chunkIds: group.chunks.map((chunk) => chunk.chunkId),
2479
2602
  contextLabel: leadChunk?.labels?.contextLabel ?? buildContextLabel2(leadChunk?.metadata),
2480
2603
  count: group.count,
2481
- excerpt: buildExcerpt2(leadChunk?.text ?? ""),
2604
+ excerpt: buildRAGPreferredExcerpt(excerpts, leadChunk?.structure ?? buildRAGChunkStructure(leadChunk?.metadata)) || buildExcerpt2(leadChunk?.text ?? ""),
2605
+ excerpts,
2482
2606
  key: group.key,
2483
2607
  label: group.label,
2484
2608
  locatorLabel: leadChunk?.labels?.locatorLabel ?? buildLocatorLabel2(leadChunk?.metadata, leadChunk?.source, leadChunk?.title),
@@ -1014,6 +1014,60 @@ 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 = [
1059
+ previousChunkId,
1060
+ activeSource.chunkId,
1061
+ nextChunkId
1062
+ ].filter((chunkId, index, values) => Boolean(chunkId) && values.indexOf(chunkId) === index);
1063
+ return {
1064
+ chunkExcerpt: buildExcerpt(activeSource.text, 160),
1065
+ sectionExcerpt: buildExcerpt(sectionSources.map((source) => source.text).join(`
1066
+
1067
+ `), 320),
1068
+ windowExcerpt: buildExcerpt(collectText(orderedWindowIds), 240)
1069
+ };
1070
+ };
1017
1071
  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
1072
  var buildGroundingReferenceEvidenceSummary = (reference) => [
1019
1073
  reference.source ?? reference.title ?? reference.chunkId,
@@ -1032,7 +1086,8 @@ var buildGroundedAnswerCitationDetail = (reference) => ({
1032
1086
  contextLabel: reference.contextLabel,
1033
1087
  evidenceLabel: buildGroundingReferenceEvidenceLabel(reference),
1034
1088
  evidenceSummary: buildGroundingReferenceEvidenceSummary(reference),
1035
- excerpt: reference.excerpt,
1089
+ excerpt: selectPreferredExcerpt(reference.excerpts, getContextNumber(reference.metadata?.sectionChunkCount)) || reference.excerpt,
1090
+ excerpts: reference.excerpts,
1036
1091
  label: reference.label,
1037
1092
  locatorLabel: reference.locatorLabel,
1038
1093
  number: reference.number,
@@ -1052,6 +1107,8 @@ var buildRAGCitations = (sources) => {
1052
1107
  unique.set(key, {
1053
1108
  chunkId: source.chunkId,
1054
1109
  contextLabel: source.labels?.contextLabel ?? buildContextLabel(source.metadata),
1110
+ excerpt: selectPreferredExcerpt(buildGroundingChunkExcerpts(sources, source.chunkId), getContextNumber(source.metadata?.sectionChunkCount)) || buildExcerpt(source.text),
1111
+ excerpts: buildGroundingChunkExcerpts(sources, source.chunkId),
1055
1112
  key,
1056
1113
  label: buildSourceLabel(source),
1057
1114
  locatorLabel: source.labels?.locatorLabel ?? buildLocatorLabel(source.metadata, source.source, source.title),
@@ -1128,10 +1185,17 @@ var buildRAGGroundedAnswerSectionSummaries = (references) => {
1128
1185
  const key = buildGroundingSectionKey(reference);
1129
1186
  const existing = groups.get(key);
1130
1187
  if (!existing) {
1188
+ const excerpts = reference.excerpts ? {
1189
+ chunkExcerpt: reference.excerpts.chunkExcerpt,
1190
+ sectionExcerpt: reference.excerpts.sectionExcerpt,
1191
+ windowExcerpt: reference.excerpts.windowExcerpt
1192
+ } : undefined;
1131
1193
  groups.set(key, {
1132
1194
  chunkIds: [reference.chunkId],
1133
1195
  contextLabel: reference.contextLabel,
1134
1196
  count: 1,
1197
+ excerpt: selectPreferredExcerpt(excerpts, getContextNumber(reference.metadata?.sectionChunkCount)) || excerpts?.sectionExcerpt || reference.excerpt,
1198
+ excerpts,
1135
1199
  key,
1136
1200
  label: key,
1137
1201
  locatorLabel: reference.locatorLabel,
@@ -1159,6 +1223,14 @@ var buildRAGGroundedAnswerSectionSummaries = (references) => {
1159
1223
  if (!existing.provenanceLabel && reference.provenanceLabel) {
1160
1224
  existing.provenanceLabel = reference.provenanceLabel;
1161
1225
  }
1226
+ if (!existing.excerpts && reference.excerpts) {
1227
+ existing.excerpts = {
1228
+ chunkExcerpt: reference.excerpts.chunkExcerpt,
1229
+ sectionExcerpt: reference.excerpts.sectionExcerpt,
1230
+ windowExcerpt: reference.excerpts.windowExcerpt
1231
+ };
1232
+ existing.excerpt = reference.excerpts.sectionExcerpt;
1233
+ }
1162
1234
  }
1163
1235
  return [...groups.values()].map((group) => ({
1164
1236
  ...group,
@@ -1176,20 +1248,24 @@ var buildRAGGroundedAnswerSectionSummaries = (references) => {
1176
1248
  var buildRAGGroundingReferences = (sources) => {
1177
1249
  const citations = buildRAGCitations(sources);
1178
1250
  const citationReferenceMap = buildRAGCitationReferenceMap(citations);
1179
- return citations.map((citation) => ({
1180
- chunkId: citation.chunkId,
1181
- contextLabel: citation.contextLabel ?? buildContextLabel(citation.metadata),
1182
- excerpt: buildExcerpt(citation.text),
1183
- label: citation.label,
1184
- locatorLabel: citation.locatorLabel ?? buildLocatorLabel(citation.metadata, citation.source, citation.title),
1185
- metadata: citation.metadata,
1186
- number: citationReferenceMap[citation.chunkId] ?? 0,
1187
- provenanceLabel: citation.provenanceLabel ?? buildProvenanceLabel(citation.metadata),
1188
- score: citation.score,
1189
- source: citation.source,
1190
- text: citation.text,
1191
- title: citation.title
1192
- }));
1251
+ return citations.map((citation) => {
1252
+ const excerpts = buildGroundingChunkExcerpts(sources, citation.chunkId);
1253
+ return {
1254
+ chunkId: citation.chunkId,
1255
+ contextLabel: citation.contextLabel ?? buildContextLabel(citation.metadata),
1256
+ excerpt: selectPreferredExcerpt(excerpts, getContextNumber(citation.metadata?.sectionChunkCount)) || excerpts?.chunkExcerpt || buildExcerpt(citation.text),
1257
+ excerpts,
1258
+ label: citation.label,
1259
+ locatorLabel: citation.locatorLabel ?? buildLocatorLabel(citation.metadata, citation.source, citation.title),
1260
+ metadata: citation.metadata,
1261
+ number: citationReferenceMap[citation.chunkId] ?? 0,
1262
+ provenanceLabel: citation.provenanceLabel ?? buildProvenanceLabel(citation.metadata),
1263
+ score: citation.score,
1264
+ source: citation.source,
1265
+ text: citation.text,
1266
+ title: citation.title
1267
+ };
1268
+ });
1193
1269
  };
1194
1270
 
1195
1271
  // src/ai/rag/presentation.ts
@@ -1778,7 +1854,7 @@ var buildRAGChunkStructure = (metadata) => {
1778
1854
  return;
1779
1855
  }
1780
1856
  const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.filter((value) => typeof value === "string" && value.trim().length > 0) : undefined;
1781
- const sectionKind = metadata.sectionKind === "markdown_heading" || metadata.sectionKind === "html_heading" ? metadata.sectionKind : undefined;
1857
+ 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;
1782
1858
  const section = {
1783
1859
  depth: getContextNumber2(metadata.sectionDepth),
1784
1860
  kind: sectionKind,
@@ -1807,6 +1883,52 @@ var buildExcerpt2 = (text, maxLength = 160) => {
1807
1883
  }
1808
1884
  return `${normalized.slice(0, Math.max(0, maxLength - 1)).trimEnd()}\u2026`;
1809
1885
  };
1886
+ var buildRAGChunkExcerpts = (chunks, activeChunkId) => {
1887
+ if (chunks.length === 0) {
1888
+ return;
1889
+ }
1890
+ const graph = buildRAGChunkGraph(chunks.map((chunk) => ({
1891
+ chunkId: chunk.chunkId,
1892
+ metadata: chunk.metadata,
1893
+ structure: chunk.structure
1894
+ })));
1895
+ const navigation = buildRAGChunkGraphNavigation(graph, activeChunkId);
1896
+ const activeChunk = chunks.find((chunk) => chunk.chunkId === navigation.activeChunkId) ?? chunks[0];
1897
+ if (!activeChunk) {
1898
+ return;
1899
+ }
1900
+ const chunkMap = new Map(chunks.map((chunk) => [chunk.chunkId, chunk]));
1901
+ const orderedWindowIds = [
1902
+ navigation.previousNode?.chunkId,
1903
+ activeChunk.chunkId,
1904
+ navigation.nextNode?.chunkId
1905
+ ].filter((chunkId, index, ids) => Boolean(chunkId) && ids.indexOf(chunkId) === index);
1906
+ const orderedSectionIds = navigation.sectionNodes.length > 0 ? navigation.sectionNodes.map((node) => node.chunkId) : [activeChunk.chunkId];
1907
+ const collectText = (chunkIds) => chunkIds.map((chunkId) => chunkMap.get(chunkId)?.text).filter((text) => typeof text === "string").join(`
1908
+
1909
+ `);
1910
+ return {
1911
+ chunkExcerpt: buildExcerpt2(activeChunk.text, 160),
1912
+ sectionExcerpt: buildExcerpt2(collectText(orderedSectionIds), 320),
1913
+ windowExcerpt: buildExcerpt2(collectText(orderedWindowIds), 240)
1914
+ };
1915
+ };
1916
+ var buildRAGPreferredExcerpt = (excerpts, structure) => {
1917
+ if (!excerpts) {
1918
+ return "";
1919
+ }
1920
+ const chunkLength = excerpts.chunkExcerpt.trim().length;
1921
+ const sectionChunkCount = structure?.sequence?.sectionChunkCount ?? 1;
1922
+ if (sectionChunkCount > 1 && chunkLength > 0 && chunkLength < 72) {
1923
+ if (sectionChunkCount <= 3 && excerpts.sectionExcerpt.trim().length > 0) {
1924
+ return excerpts.sectionExcerpt;
1925
+ }
1926
+ if (excerpts.windowExcerpt.trim().length > 0) {
1927
+ return excerpts.windowExcerpt;
1928
+ }
1929
+ }
1930
+ return excerpts.chunkExcerpt;
1931
+ };
1810
1932
  var buildRAGChunkGraph = (chunks) => {
1811
1933
  const nodes = [];
1812
1934
  const edges = [];
@@ -2018,6 +2140,7 @@ var buildRAGSourceSummaries = (sources) => {
2018
2140
  return sourceGroups.map((group) => {
2019
2141
  const groupCitations = citations.filter((citation) => group.chunks.some((chunk) => chunk.chunkId === citation.chunkId));
2020
2142
  const leadChunk = group.chunks.slice().sort((left, right) => right.score - left.score)[0];
2143
+ const excerpts = leadChunk ? buildRAGChunkExcerpts(group.chunks, leadChunk.chunkId) : undefined;
2021
2144
  return {
2022
2145
  bestScore: group.bestScore,
2023
2146
  citationNumbers: groupCitations.map((citation) => citationReferenceMap[citation.chunkId] ?? 0),
@@ -2025,7 +2148,8 @@ var buildRAGSourceSummaries = (sources) => {
2025
2148
  chunkIds: group.chunks.map((chunk) => chunk.chunkId),
2026
2149
  contextLabel: leadChunk?.labels?.contextLabel ?? buildContextLabel2(leadChunk?.metadata),
2027
2150
  count: group.count,
2028
- excerpt: buildExcerpt2(leadChunk?.text ?? ""),
2151
+ excerpt: buildRAGPreferredExcerpt(excerpts, leadChunk?.structure ?? buildRAGChunkStructure(leadChunk?.metadata)) || buildExcerpt2(leadChunk?.text ?? ""),
2152
+ excerpts,
2029
2153
  key: group.key,
2030
2154
  label: group.label,
2031
2155
  locatorLabel: leadChunk?.labels?.locatorLabel ?? buildLocatorLabel2(leadChunk?.metadata, leadChunk?.source, leadChunk?.title),
@@ -4193,5 +4317,5 @@ export {
4193
4317
  AIStreamService
4194
4318
  };
4195
4319
 
4196
- //# debugId=2C3A632BB772C68C64756E2164756E21
4320
+ //# debugId=66CB3D3B24BE2E6464756E2164756E21
4197
4321
  //# sourceMappingURL=index.js.map