@absolutejs/absolute 0.19.0-beta.604 → 0.19.0-beta.605

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 (45) hide show
  1. package/dist/ai/client/index.js +186 -6
  2. package/dist/ai/client/index.js.map +4 -4
  3. package/dist/ai/client/ui.js +190 -6
  4. package/dist/ai/client/ui.js.map +4 -4
  5. package/dist/ai/index.js +289 -36
  6. package/dist/ai/index.js.map +7 -7
  7. package/dist/ai/rag/quality.js +17 -6
  8. package/dist/ai/rag/quality.js.map +3 -3
  9. package/dist/ai/rag/ui.js +190 -6
  10. package/dist/ai/rag/ui.js.map +4 -4
  11. package/dist/ai-client/angular/ai/index.js +185 -5
  12. package/dist/ai-client/react/ai/index.js +200 -6
  13. package/dist/ai-client/vue/ai/index.js +289 -97
  14. package/dist/angular/ai/index.js +186 -6
  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 +201 -7
  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 +7 -1
  29. package/dist/src/ai/rag/ui.d.ts +1 -1
  30. package/dist/src/react/ai/useRAG.d.ts +5 -0
  31. package/dist/src/react/ai/useRAGChunkPreview.d.ts +4 -0
  32. package/dist/src/react/ai/useRAGSources.d.ts +1 -0
  33. package/dist/src/svelte/ai/createRAG.d.ts +5 -0
  34. package/dist/src/svelte/ai/createRAGChunkPreview.d.ts +4 -0
  35. package/dist/src/svelte/ai/createRAGSources.d.ts +1 -0
  36. package/dist/src/vue/ai/useRAG.d.ts +65 -0
  37. package/dist/src/vue/ai/useRAGChunkPreview.d.ts +34 -0
  38. package/dist/src/vue/ai/useRAGSearch.d.ts +30 -0
  39. package/dist/src/vue/ai/useRAGSources.d.ts +1 -0
  40. package/dist/svelte/ai/index.js +247 -53
  41. package/dist/svelte/ai/index.js.map +6 -6
  42. package/dist/types/ai.d.ts +60 -0
  43. package/dist/vue/ai/index.js +253 -59
  44. package/dist/vue/ai/index.js.map +6 -6
  45. package/package.json +1 -1
package/dist/ai/index.js CHANGED
@@ -243,6 +243,11 @@ var buildContextLabel = (metadata) => {
243
243
  if (speaker) {
244
244
  return `Speaker ${speaker}`;
245
245
  }
246
+ const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.map((value) => getContextString(value)).filter((value) => typeof value === "string") : [];
247
+ const sectionTitle = getContextString(metadata.sectionTitle) ?? sectionPath.at(-1);
248
+ if (sectionTitle) {
249
+ return `Section ${sectionTitle}`;
250
+ }
246
251
  return;
247
252
  };
248
253
  var formatMediaTimestamp = (value) => {
@@ -292,6 +297,10 @@ var buildLocatorLabel = (metadata, source, title) => {
292
297
  if (mediaStart) {
293
298
  return `Timestamp ${mediaStart}`;
294
299
  }
300
+ const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.map((value) => getContextString(value)).filter((value) => typeof value === "string") : [];
301
+ if (sectionPath.length > 0) {
302
+ return `Section ${sectionPath.join(" > ")}`;
303
+ }
295
304
  return;
296
305
  };
297
306
  var formatTimestampLabel = (value) => {
@@ -341,8 +350,10 @@ var buildExcerpt = (text, maxLength = 160) => {
341
350
  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 ");
342
351
  var buildGroundingReferenceEvidenceSummary = (reference) => [
343
352
  reference.source ?? reference.title ?? reference.chunkId,
353
+ reference.locatorLabel,
354
+ reference.contextLabel,
344
355
  reference.provenanceLabel
345
- ].filter((value) => Boolean(value && value.length > 0)).join(" \xB7 ");
356
+ ].filter((value) => Boolean(value && value.length > 0)).filter((value, index, values) => values.findIndex((entry) => entry === value) === index).join(" \xB7 ");
346
357
  var buildGroundedAnswerCitationDetail = (reference) => ({
347
358
  contextLabel: reference.contextLabel,
348
359
  evidenceLabel: buildGroundingReferenceEvidenceLabel(reference),
@@ -366,12 +377,12 @@ var buildRAGCitations = (sources) => {
366
377
  continue;
367
378
  unique.set(key, {
368
379
  chunkId: source.chunkId,
369
- contextLabel: buildContextLabel(source.metadata),
380
+ contextLabel: source.labels?.contextLabel ?? buildContextLabel(source.metadata),
370
381
  key,
371
382
  label: buildSourceLabel(source),
372
- locatorLabel: buildLocatorLabel(source.metadata, source.source, source.title),
383
+ locatorLabel: source.labels?.locatorLabel ?? buildLocatorLabel(source.metadata, source.source, source.title),
373
384
  metadata: source.metadata,
374
- provenanceLabel: buildProvenanceLabel(source.metadata),
385
+ provenanceLabel: source.labels?.provenanceLabel ?? buildProvenanceLabel(source.metadata),
375
386
  score: source.score,
376
387
  source: source.source,
377
388
  text: source.text,
@@ -441,7 +452,7 @@ var buildRAGGroundingReferences = (sources) => {
441
452
  const citationReferenceMap = buildRAGCitationReferenceMap(citations);
442
453
  return citations.map((citation) => ({
443
454
  chunkId: citation.chunkId,
444
- contextLabel: buildContextLabel(citation.metadata),
455
+ contextLabel: citation.contextLabel ?? buildContextLabel(citation.metadata),
445
456
  excerpt: buildExcerpt(citation.text),
446
457
  label: citation.label,
447
458
  locatorLabel: citation.locatorLabel ?? buildLocatorLabel(citation.metadata, citation.source, citation.title),
@@ -4014,6 +4025,11 @@ var buildContextLabel2 = (metadata) => {
4014
4025
  if (speaker) {
4015
4026
  return `Speaker ${speaker}`;
4016
4027
  }
4028
+ const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.map((value) => getContextString2(value)).filter((value) => typeof value === "string") : [];
4029
+ const sectionTitle = getContextString2(metadata.sectionTitle) ?? sectionPath.at(-1);
4030
+ if (sectionTitle) {
4031
+ return `Section ${sectionTitle}`;
4032
+ }
4017
4033
  return;
4018
4034
  };
4019
4035
  var buildLocatorLabel2 = (metadata, source, title) => {
@@ -4053,6 +4069,10 @@ var buildLocatorLabel2 = (metadata, source, title) => {
4053
4069
  if (mediaStart) {
4054
4070
  return `Timestamp ${mediaStart}`;
4055
4071
  }
4072
+ const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.map((value) => getContextString2(value)).filter((value) => typeof value === "string") : [];
4073
+ if (sectionPath.length > 0) {
4074
+ return `Section ${sectionPath.join(" > ")}`;
4075
+ }
4056
4076
  return;
4057
4077
  };
4058
4078
  var buildProvenanceLabel2 = (metadata) => {
@@ -4098,6 +4118,33 @@ var buildRAGSourceLabels = ({
4098
4118
  provenanceLabel
4099
4119
  };
4100
4120
  };
4121
+ var buildRAGChunkStructure = (metadata) => {
4122
+ if (!metadata) {
4123
+ return;
4124
+ }
4125
+ const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.filter((value) => typeof value === "string" && value.trim().length > 0) : undefined;
4126
+ const sectionKind = metadata.sectionKind === "markdown_heading" || metadata.sectionKind === "html_heading" ? metadata.sectionKind : undefined;
4127
+ const section = {
4128
+ depth: getContextNumber2(metadata.sectionDepth),
4129
+ kind: sectionKind,
4130
+ path: sectionPath && sectionPath.length > 0 ? sectionPath : undefined,
4131
+ title: getContextString2(metadata.sectionTitle)
4132
+ };
4133
+ const sequence = {
4134
+ nextChunkId: getContextString2(metadata.nextChunkId),
4135
+ previousChunkId: getContextString2(metadata.previousChunkId),
4136
+ sectionChunkCount: getContextNumber2(metadata.sectionChunkCount),
4137
+ sectionChunkId: getContextString2(metadata.sectionChunkId),
4138
+ sectionChunkIndex: getContextNumber2(metadata.sectionChunkIndex)
4139
+ };
4140
+ if (!section.title && (!section.path || section.path.length === 0) && typeof section.depth !== "number" && !section.kind && !sequence.nextChunkId && !sequence.previousChunkId && typeof sequence.sectionChunkCount !== "number" && !sequence.sectionChunkId && typeof sequence.sectionChunkIndex !== "number") {
4141
+ return;
4142
+ }
4143
+ return {
4144
+ section: section.title || section.path && section.path.length > 0 || typeof section.depth === "number" || section.kind ? section : undefined,
4145
+ sequence: sequence.nextChunkId || sequence.previousChunkId || typeof sequence.sectionChunkCount === "number" || sequence.sectionChunkId || typeof sequence.sectionChunkIndex === "number" ? sequence : undefined
4146
+ };
4147
+ };
4101
4148
  var buildExcerpt2 = (text, maxLength = 160) => {
4102
4149
  const normalized = text.replaceAll(/\s+/g, " ").trim();
4103
4150
  if (normalized.length <= maxLength) {
@@ -4105,6 +4152,136 @@ var buildExcerpt2 = (text, maxLength = 160) => {
4105
4152
  }
4106
4153
  return `${normalized.slice(0, Math.max(0, maxLength - 1)).trimEnd()}\u2026`;
4107
4154
  };
4155
+ var buildRAGChunkGraph = (chunks) => {
4156
+ const nodes = [];
4157
+ const edges = [];
4158
+ const edgeKeys = new Set;
4159
+ const sections = new Map;
4160
+ for (const chunk of chunks) {
4161
+ const labels = chunk.labels ?? buildRAGSourceLabels({
4162
+ metadata: chunk.metadata,
4163
+ source: chunk.source,
4164
+ title: chunk.title
4165
+ });
4166
+ const structure = chunk.structure ?? buildRAGChunkStructure(chunk.metadata);
4167
+ nodes.push({
4168
+ chunkId: chunk.chunkId,
4169
+ contextLabel: labels?.contextLabel,
4170
+ label: chunk.source ?? chunk.title ?? chunk.chunkId,
4171
+ locatorLabel: labels?.locatorLabel,
4172
+ provenanceLabel: labels?.provenanceLabel,
4173
+ score: chunk.score,
4174
+ source: chunk.source,
4175
+ structure,
4176
+ title: chunk.title
4177
+ });
4178
+ const previousChunkId = structure?.sequence?.previousChunkId;
4179
+ if (previousChunkId) {
4180
+ const key = `previous:${previousChunkId}:${chunk.chunkId}`;
4181
+ if (!edgeKeys.has(key)) {
4182
+ edgeKeys.add(key);
4183
+ edges.push({
4184
+ fromChunkId: previousChunkId,
4185
+ relation: "previous",
4186
+ toChunkId: chunk.chunkId
4187
+ });
4188
+ }
4189
+ }
4190
+ const nextChunkId = structure?.sequence?.nextChunkId;
4191
+ if (nextChunkId) {
4192
+ const key = `next:${chunk.chunkId}:${nextChunkId}`;
4193
+ if (!edgeKeys.has(key)) {
4194
+ edgeKeys.add(key);
4195
+ edges.push({
4196
+ fromChunkId: chunk.chunkId,
4197
+ relation: "next",
4198
+ toChunkId: nextChunkId
4199
+ });
4200
+ }
4201
+ }
4202
+ const sectionId = structure?.sequence?.sectionChunkId;
4203
+ if (sectionId) {
4204
+ const existing = sections.get(sectionId);
4205
+ if (!existing) {
4206
+ sections.set(sectionId, {
4207
+ chunkCount: structure.sequence?.sectionChunkCount ?? 1,
4208
+ chunkIds: [chunk.chunkId],
4209
+ depth: structure.section?.depth,
4210
+ id: sectionId,
4211
+ kind: structure.section?.kind,
4212
+ path: structure.section?.path,
4213
+ title: structure.section?.title
4214
+ });
4215
+ continue;
4216
+ }
4217
+ if (!existing.chunkIds.includes(chunk.chunkId)) {
4218
+ existing.chunkIds.push(chunk.chunkId);
4219
+ }
4220
+ existing.chunkCount = Math.max(existing.chunkCount, structure.sequence?.sectionChunkCount ?? existing.chunkCount);
4221
+ }
4222
+ }
4223
+ for (const section of sections.values()) {
4224
+ section.chunkIds.sort((left, right) => {
4225
+ const leftNode = nodes.find((node) => node.chunkId === left);
4226
+ const rightNode = nodes.find((node) => node.chunkId === right);
4227
+ const leftIndex = leftNode?.structure?.sequence?.sectionChunkIndex ?? Number.MAX_SAFE_INTEGER;
4228
+ const rightIndex = rightNode?.structure?.sequence?.sectionChunkIndex ?? Number.MAX_SAFE_INTEGER;
4229
+ if (leftIndex !== rightIndex) {
4230
+ return leftIndex - rightIndex;
4231
+ }
4232
+ return left.localeCompare(right);
4233
+ });
4234
+ }
4235
+ nodes.sort((left, right) => {
4236
+ const leftSection = left.structure?.sequence?.sectionChunkIndex ?? Number.MAX_SAFE_INTEGER;
4237
+ const rightSection = right.structure?.sequence?.sectionChunkIndex ?? Number.MAX_SAFE_INTEGER;
4238
+ if (leftSection !== rightSection) {
4239
+ return leftSection - rightSection;
4240
+ }
4241
+ const leftScore = left.score ?? Number.NEGATIVE_INFINITY;
4242
+ const rightScore = right.score ?? Number.NEGATIVE_INFINITY;
4243
+ if (leftScore !== rightScore) {
4244
+ return rightScore - leftScore;
4245
+ }
4246
+ return left.label.localeCompare(right.label);
4247
+ });
4248
+ return {
4249
+ edges,
4250
+ nodes,
4251
+ sections: [...sections.values()].sort((left, right) => (left.title ?? left.id).localeCompare(right.title ?? right.id))
4252
+ };
4253
+ };
4254
+ var buildRAGChunkPreviewGraph = (preview) => buildRAGChunkGraph(preview.chunks.map((chunk) => ({
4255
+ chunkId: chunk.chunkId,
4256
+ labels: chunk.labels,
4257
+ metadata: chunk.metadata,
4258
+ source: chunk.source ?? preview.document.source,
4259
+ structure: chunk.structure,
4260
+ title: chunk.title ?? preview.document.title
4261
+ })));
4262
+ var buildRAGChunkPreviewNavigation = (preview, activeChunkId) => buildRAGChunkGraphNavigation(buildRAGChunkPreviewGraph(preview), activeChunkId);
4263
+ var buildRAGChunkGraphNavigation = (graph, activeChunkId) => {
4264
+ if (graph.nodes.length === 0) {
4265
+ return {
4266
+ activeChunkId,
4267
+ sectionNodes: []
4268
+ };
4269
+ }
4270
+ const activeNode = (activeChunkId ? graph.nodes.find((node) => node.chunkId === activeChunkId) : undefined) ?? graph.nodes[0];
4271
+ const resolvedActiveChunkId = activeNode?.chunkId;
4272
+ const previousNode = activeNode?.structure?.sequence?.previousChunkId ? graph.nodes.find((node) => node.chunkId === activeNode.structure?.sequence?.previousChunkId) : undefined;
4273
+ const nextNode = activeNode?.structure?.sequence?.nextChunkId ? graph.nodes.find((node) => node.chunkId === activeNode.structure?.sequence?.nextChunkId) : undefined;
4274
+ const section = activeNode?.structure?.sequence?.sectionChunkId ? graph.sections.find((entry) => entry.id === activeNode.structure?.sequence?.sectionChunkId) : undefined;
4275
+ const sectionNodes = section ? section.chunkIds.map((chunkId) => graph.nodes.find((node) => node.chunkId === chunkId)).filter((node) => Boolean(node)) : activeNode ? [activeNode] : [];
4276
+ return {
4277
+ activeChunkId: resolvedActiveChunkId,
4278
+ activeNode,
4279
+ nextNode,
4280
+ previousNode,
4281
+ section,
4282
+ sectionNodes
4283
+ };
4284
+ };
4108
4285
  var buildRAGRetrievedState = (messages) => {
4109
4286
  const message = getLatestRetrievedMessage(messages);
4110
4287
  if (!message) {
@@ -4146,6 +4323,7 @@ var buildRAGSourceSummaries = (sources) => {
4146
4323
  label: group.label,
4147
4324
  locatorLabel: leadChunk?.labels?.locatorLabel ?? buildLocatorLabel2(leadChunk?.metadata, leadChunk?.source, leadChunk?.title),
4148
4325
  provenanceLabel: leadChunk?.labels?.provenanceLabel ?? buildProvenanceLabel2(leadChunk?.metadata),
4326
+ structure: leadChunk?.structure ?? buildRAGChunkStructure(leadChunk?.metadata),
4149
4327
  source: group.source,
4150
4328
  title: group.title
4151
4329
  };
@@ -4274,6 +4452,7 @@ var buildSourceGroup = (source, key) => ({
4274
4452
  source: source.source,
4275
4453
  title: source.title
4276
4454
  }),
4455
+ structure: source.structure ?? buildRAGChunkStructure(source.metadata),
4277
4456
  source: source.source,
4278
4457
  title: source.title
4279
4458
  });
@@ -4292,6 +4471,7 @@ var updateSourceGroup = (groups, source) => {
4292
4471
  source: source.source,
4293
4472
  title: source.title
4294
4473
  });
4474
+ existing.structure = source.structure ?? buildRAGChunkStructure(source.metadata);
4295
4475
  existing.source = source.source;
4296
4476
  existing.title = source.title;
4297
4477
  } else {
@@ -7925,22 +8105,43 @@ var markdownStructureUnits = (value) => {
7925
8105
  `);
7926
8106
  const sections = [];
7927
8107
  let current = [];
8108
+ let currentPath = [];
8109
+ const headingStack = [];
7928
8110
  const flushCurrentSection = () => {
7929
8111
  if (current.length === 0) {
7930
8112
  return;
7931
8113
  }
7932
- sections.push(current.join(`
7933
- `));
8114
+ sections.push({
8115
+ lines: current,
8116
+ sectionPath: [...currentPath]
8117
+ });
7934
8118
  current = [];
7935
8119
  };
7936
8120
  for (const line of lines) {
7937
- const startsNewSection = /^\s*#{1,6}\s+/.test(line) && current.length > 0;
7938
- if (startsNewSection)
7939
- flushCurrentSection();
8121
+ const headingMatch = line.match(/^\s*(#{1,6})\s+(.+)$/);
8122
+ if (headingMatch) {
8123
+ if (current.length > 0) {
8124
+ flushCurrentSection();
8125
+ }
8126
+ const depth = headingMatch[1]?.length ?? 1;
8127
+ const headingText = normalizeWhitespace(headingMatch[2] ?? "");
8128
+ if (headingText) {
8129
+ headingStack[depth - 1] = headingText;
8130
+ headingStack.length = depth;
8131
+ currentPath = [...headingStack];
8132
+ }
8133
+ }
7940
8134
  current.push(line);
7941
8135
  }
7942
8136
  flushCurrentSection();
7943
- return sections.map((section) => stripMarkdown(section)).map((section) => normalizeWhitespace(section)).filter(Boolean);
8137
+ return sections.map(({ lines: sectionLines, sectionPath }) => ({
8138
+ sectionDepth: sectionPath.length > 0 ? sectionPath.length : undefined,
8139
+ sectionKind: sectionPath.length > 0 ? "markdown_heading" : undefined,
8140
+ sectionPath: sectionPath.length > 0 ? sectionPath : undefined,
8141
+ sectionTitle: sectionPath.at(-1),
8142
+ text: normalizeWhitespace(stripMarkdown(sectionLines.join(`
8143
+ `)))
8144
+ })).filter((section) => Boolean(section.text));
7944
8145
  };
7945
8146
  var joinHtmlHeadingSection = (headings, content) => {
7946
8147
  const normalizedHeadings = headings.map((heading) => normalizeWhitespace(heading));
@@ -7963,7 +8164,13 @@ var htmlStructureUnits = (value) => {
7963
8164
  }
7964
8165
  const section = joinHtmlHeadingSection(activeHeadings, content);
7965
8166
  if (section) {
7966
- sections.push(section);
8167
+ sections.push({
8168
+ sectionDepth: activeHeadings.length > 0 ? activeHeadings.length : undefined,
8169
+ sectionKind: activeHeadings.length > 0 ? "html_heading" : undefined,
8170
+ sectionPath: activeHeadings.length > 0 ? [...activeHeadings] : undefined,
8171
+ sectionTitle: activeHeadings.at(-1),
8172
+ text: section
8173
+ });
7967
8174
  }
7968
8175
  };
7969
8176
  for (const match of focused.matchAll(headingPattern)) {
@@ -7984,7 +8191,7 @@ var htmlStructureUnits = (value) => {
7984
8191
  if (sections.length > 0) {
7985
8192
  return sections;
7986
8193
  }
7987
- return [normalizeWhitespace(stripHtmlTags(focused))].filter(Boolean);
8194
+ return [{ text: normalizeWhitespace(stripHtmlTags(focused)) }].filter((section) => Boolean(section.text));
7988
8195
  };
7989
8196
  var inferFormat = (document) => {
7990
8197
  if (document.format) {
@@ -9149,7 +9356,7 @@ var fixedUnits = (text, maxChunkLength) => {
9149
9356
  return units;
9150
9357
  };
9151
9358
  var sourceAwareUnits = (document, format, normalizedText) => {
9152
- const resolveStructuredUnits = (sections) => sections.length > 0 ? sections : paragraphUnits(normalizedText);
9359
+ const resolveStructuredUnits = (sections) => sections.length > 0 ? sections : paragraphUnits(normalizedText).map((text) => ({ text }));
9153
9360
  switch (format) {
9154
9361
  case "markdown": {
9155
9362
  const sections = markdownStructureUnits(document.text);
@@ -9161,7 +9368,7 @@ var sourceAwareUnits = (document, format, normalizedText) => {
9161
9368
  }
9162
9369
  case "text":
9163
9370
  default:
9164
- return paragraphUnits(normalizedText);
9371
+ return paragraphUnits(normalizedText).map((text) => ({ text }));
9165
9372
  }
9166
9373
  };
9167
9374
  var overlapTail = (value, overlap) => {
@@ -9225,10 +9432,13 @@ var chunkFromUnits = (units, maxChunkLength, chunkOverlap, minChunkLength) => {
9225
9432
  return merged;
9226
9433
  };
9227
9434
  var chunkSourceAwareUnit = (unit, options) => {
9228
- if (unit.length <= options.maxChunkLength) {
9435
+ if (unit.text.length <= options.maxChunkLength) {
9229
9436
  return [unit];
9230
9437
  }
9231
- return chunkFromUnits(paragraphUnits(unit), options.maxChunkLength, options.chunkOverlap, options.minChunkLength);
9438
+ return chunkFromUnits(paragraphUnits(unit.text), options.maxChunkLength, options.chunkOverlap, options.minChunkLength).map((text) => ({
9439
+ ...unit,
9440
+ text
9441
+ }));
9232
9442
  };
9233
9443
  var resolveChunkingUnits = (text, options) => {
9234
9444
  if (options.strategy === "fixed") {
@@ -9251,15 +9461,15 @@ var resolveChunkingOptions = (document, defaults) => {
9251
9461
  strategy
9252
9462
  };
9253
9463
  };
9254
- var createChunkTexts = (document, format, text, options) => {
9464
+ var createChunkEntries = (document, format, text, options) => {
9255
9465
  if (text.length <= options.maxChunkLength && options.strategy !== "source_aware") {
9256
- return [text];
9466
+ return [{ text }];
9257
9467
  }
9258
9468
  if (options.strategy === "source_aware") {
9259
9469
  return sourceAwareUnits(document, format, text).flatMap((unit) => chunkSourceAwareUnit(unit, options));
9260
9470
  }
9261
9471
  const units = resolveChunkingUnits(text, options);
9262
- return chunkFromUnits(units, options.maxChunkLength, options.chunkOverlap, options.minChunkLength);
9472
+ return chunkFromUnits(units, options.maxChunkLength, options.chunkOverlap, options.minChunkLength).map((entry) => ({ text: entry }));
9263
9473
  };
9264
9474
  var prepareRAGDocument = (document, defaultChunking) => {
9265
9475
  const format = inferFormat(document);
@@ -9281,18 +9491,46 @@ var prepareRAGDocument = (document, defaultChunking) => {
9281
9491
  source,
9282
9492
  title
9283
9493
  };
9284
- const chunkTexts = createChunkTexts(document, format, normalizedText, chunking);
9285
- const chunks = chunkTexts.map((text, index) => ({
9286
- chunkId: `${documentId}:${String(index + 1).padStart(RAG_CHUNK_ID_PAD_LENGTH, "0")}`,
9287
- metadata: {
9288
- ...metadata,
9289
- chunkCount: chunkTexts.length,
9290
- chunkIndex: index
9291
- },
9292
- source,
9293
- text,
9294
- title
9295
- }));
9494
+ const chunkEntries = createChunkEntries(document, format, normalizedText, chunking);
9495
+ const chunks = chunkEntries.map((entry, index) => {
9496
+ const sectionPath = Array.isArray(entry.sectionPath) ? entry.sectionPath.filter((value) => typeof value === "string" && value.length > 0) : undefined;
9497
+ const sectionTitle = typeof entry.sectionTitle === "string" && entry.sectionTitle.length > 0 ? entry.sectionTitle : sectionPath?.at(-1);
9498
+ const chunkTitle = sectionTitle && sectionTitle !== title ? `${title} \xB7 ${sectionTitle}` : title;
9499
+ const sectionChunkId = sectionPath && sectionPath.length > 0 ? `${documentId}:section:${slugify(sectionPath.join(" "))}` : undefined;
9500
+ const sectionSiblingIndexes = sectionChunkId === undefined ? [index] : chunkEntries.reduce((indexes, candidate, candidateIndex) => {
9501
+ const candidatePath = Array.isArray(candidate.sectionPath) ? candidate.sectionPath.filter((value) => typeof value === "string" && value.length > 0) : undefined;
9502
+ const candidateSectionId = candidatePath && candidatePath.length > 0 ? `${documentId}:section:${slugify(candidatePath.join(" "))}` : undefined;
9503
+ if (candidateSectionId === sectionChunkId) {
9504
+ indexes.push(candidateIndex);
9505
+ }
9506
+ return indexes;
9507
+ }, []);
9508
+ const sectionChunkIndex = sectionSiblingIndexes.indexOf(index);
9509
+ const previousChunkId = index > 0 ? `${documentId}:${String(index).padStart(RAG_CHUNK_ID_PAD_LENGTH, "0")}` : undefined;
9510
+ const nextChunkId = index + 1 < chunkEntries.length ? `${documentId}:${String(index + 2).padStart(RAG_CHUNK_ID_PAD_LENGTH, "0")}` : undefined;
9511
+ return {
9512
+ chunkId: `${documentId}:${String(index + 1).padStart(RAG_CHUNK_ID_PAD_LENGTH, "0")}`,
9513
+ metadata: {
9514
+ ...metadata,
9515
+ chunkCount: chunkEntries.length,
9516
+ chunkIndex: index,
9517
+ ...sectionTitle ? { sectionTitle } : {},
9518
+ ...sectionPath && sectionPath.length > 0 ? { sectionPath } : {},
9519
+ ...typeof entry.sectionDepth === "number" ? { sectionDepth: entry.sectionDepth } : {},
9520
+ ...entry.sectionKind ? { sectionKind: entry.sectionKind } : {},
9521
+ ...sectionChunkId ? { sectionChunkId } : {},
9522
+ ...sectionChunkId && sectionChunkIndex >= 0 ? {
9523
+ sectionChunkCount: sectionSiblingIndexes.length,
9524
+ sectionChunkIndex
9525
+ } : {},
9526
+ ...previousChunkId ? { previousChunkId } : {},
9527
+ ...nextChunkId ? { nextChunkId } : {}
9528
+ },
9529
+ source,
9530
+ text: entry.text,
9531
+ title: chunkTitle
9532
+ };
9533
+ });
9296
9534
  return {
9297
9535
  chunks,
9298
9536
  documentId,
@@ -9949,6 +10187,19 @@ var renderSourceLabels = (input) => {
9949
10187
  ].filter((row) => row.length > 0);
9950
10188
  return rows.length > 0 ? `<ul class="rag-source-labels">${rows.join("")}</ul>` : "";
9951
10189
  };
10190
+ var renderChunkStructure = (structure) => {
10191
+ if (!structure) {
10192
+ return "";
10193
+ }
10194
+ const rows = [
10195
+ structure.section?.title ? `<li><strong>Section</strong> ${escapeHtml2(structure.section.title)}</li>` : "",
10196
+ structure.section?.path && structure.section.path.length > 1 ? `<li><strong>Section path</strong> ${escapeHtml2(structure.section.path.join(" > "))}</li>` : "",
10197
+ typeof structure.sequence?.sectionChunkIndex === "number" && typeof structure.sequence?.sectionChunkCount === "number" ? `<li><strong>Section chunk</strong> ${structure.sequence.sectionChunkIndex + 1} of ${structure.sequence.sectionChunkCount}</li>` : "",
10198
+ structure.sequence?.previousChunkId ? `<li><strong>Previous</strong> ${escapeHtml2(structure.sequence.previousChunkId)}</li>` : "",
10199
+ structure.sequence?.nextChunkId ? `<li><strong>Next</strong> ${escapeHtml2(structure.sequence.nextChunkId)}</li>` : ""
10200
+ ].filter((row) => row.length > 0);
10201
+ return rows.length > 0 ? `<ul class="rag-chunk-structure">${rows.join("")}</ul>` : "";
10202
+ };
9952
10203
  var renderEmptyState = (kind) => {
9953
10204
  switch (kind) {
9954
10205
  case "documents":
@@ -9988,7 +10239,7 @@ var defaultStatus = ({
9988
10239
  }
9989
10240
  return `<dl class="rag-status">` + `<div><dt>Backend</dt><dd>${escapeHtml2(status.backend)}</dd></div>` + `<div><dt>Vector mode</dt><dd>${escapeHtml2(status.vectorMode)}</dd></div>` + `<div><dt>Embedding dimensions</dt><dd>${status.dimensions ?? "n/a"}</dd></div>` + `<div><dt>Vector acceleration</dt><dd>${status.native?.active ? "active" : "inactive"}</dd></div>` + `<div><dt>Documents</dt><dd>${documents?.total ?? "n/a"}</dd></div>` + `<div><dt>Total chunks</dt><dd>${documents?.chunkCount ?? "n/a"}</dd></div>` + `<div><dt>Seed docs</dt><dd>${documents?.byKind.seed ?? 0}</dd></div>` + `<div><dt>Custom docs</dt><dd>${documents?.byKind.custom ?? 0}</dd></div>` + `</dl>${renderCapabilityList(capabilities)}`;
9990
10241
  };
9991
- var defaultSearchResultItem = (source, index) => '<article class="rag-search-result">' + `<h3>${escapeHtml2(source.title ?? source.chunkId ?? `Result ${index + 1}`)}</h3>` + `<p class="rag-search-source">${escapeHtml2(source.source ?? "unknown source")}</p>` + renderSourceLabels(source.labels) + `<p class="rag-search-score">score ${source.score.toFixed(RAG_SEARCH_SCORE_DECIMAL_PLACES)}</p>` + `<p class="rag-search-text">${escapeHtml2(source.text)}</p>` + "</article>";
10242
+ var defaultSearchResultItem = (source, index) => '<article class="rag-search-result">' + `<h3>${escapeHtml2(source.title ?? source.chunkId ?? `Result ${index + 1}`)}</h3>` + `<p class="rag-search-source">${escapeHtml2(source.source ?? "unknown source")}</p>` + renderSourceLabels(source.labels) + renderChunkStructure(source.structure) + `<p class="rag-search-score">score ${source.score.toFixed(RAG_SEARCH_SCORE_DECIMAL_PLACES)}</p>` + `<p class="rag-search-text">${escapeHtml2(source.text)}</p>` + "</article>";
9992
10243
  var defaultSearchResults = ({
9993
10244
  query,
9994
10245
  results,
@@ -10018,7 +10269,7 @@ var defaultChunkPreview = (input) => {
10018
10269
  return acc;
10019
10270
  }, []);
10020
10271
  const groupHtml = groups.map((group) => {
10021
- const chunkHtml = group.chunks.map((chunk) => '<article class="rag-chunk">' + `<h5>${escapeHtml2(chunk.chunkId)}</h5>` + `<p class="rag-chunk-meta">chunk ${typeof chunk.metadata?.chunkIndex === "number" ? chunk.metadata.chunkIndex : 0} of ${typeof chunk.metadata?.chunkCount === "number" ? chunk.metadata.chunkCount : input.chunks.length}</p>` + renderSourceLabels(chunk.labels) + `<pre>${escapeHtml2(chunk.text)}</pre>` + "</article>").join("");
10272
+ const chunkHtml = group.chunks.map((chunk) => '<article class="rag-chunk">' + `<h5>${escapeHtml2(chunk.chunkId)}</h5>` + `<p class="rag-chunk-meta">chunk ${typeof chunk.metadata?.chunkIndex === "number" ? chunk.metadata.chunkIndex : 0} of ${typeof chunk.metadata?.chunkCount === "number" ? chunk.metadata.chunkCount : input.chunks.length}</p>` + renderSourceLabels(chunk.labels) + renderChunkStructure(chunk.structure) + `<pre>${escapeHtml2(chunk.text)}</pre>` + "</article>").join("");
10022
10273
  return `<section class="rag-chunk-group"><h4>${escapeHtml2(group.title)}</h4>${chunkHtml}</section>`;
10023
10274
  }).join("");
10024
10275
  return `<section class="rag-chunk-preview">` + `<h3>${escapeHtml2(input.document.title)}</h3>` + `<p class="rag-chunk-preview-source">${escapeHtml2(input.document.source)}</p>` + renderSourceLabels(input.document.labels) + `<article class="rag-chunk-normalized">` + `<h4>Normalized text</h4>` + `<pre>${escapeHtml2(input.normalizedText)}</pre>` + `</article>${groupHtml}</section>`;
@@ -10452,6 +10703,7 @@ var buildSources2 = (results) => results.map((result) => ({
10452
10703
  metadata: result.metadata,
10453
10704
  score: normalizeScore(result.score),
10454
10705
  source: result.source,
10706
+ structure: buildRAGChunkStructure(result.metadata),
10455
10707
  text: result.chunkText,
10456
10708
  title: result.title
10457
10709
  }));
@@ -15600,7 +15852,8 @@ var ragChat = (config) => {
15600
15852
  metadata: chunk.metadata,
15601
15853
  source: chunk.source ?? preview.document.source,
15602
15854
  title: chunk.title ?? preview.document.title
15603
- })
15855
+ }),
15856
+ structure: buildRAGChunkStructure(chunk.metadata)
15604
15857
  }))
15605
15858
  };
15606
15859
  };
@@ -21197,5 +21450,5 @@ export {
21197
21450
  aiChat
21198
21451
  };
21199
21452
 
21200
- //# debugId=5C4A7D98C1C2BE6B64756E2164756E21
21453
+ //# debugId=DE5EC1314BD5A9F664756E2164756E21
21201
21454
  //# sourceMappingURL=index.js.map