@absolutejs/rag 0.0.1

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 (110) hide show
  1. package/README.md +6 -0
  2. package/dist/ai/client/index.js +12628 -0
  3. package/dist/ai/client/index.js.map +15 -0
  4. package/dist/ai/client/ui.js +11245 -0
  5. package/dist/ai/client/ui.js.map +12 -0
  6. package/dist/ai/rag/index.js +37146 -0
  7. package/dist/ai/rag/index.js.map +38 -0
  8. package/dist/ai/rag/quality.js +11358 -0
  9. package/dist/ai/rag/quality.js.map +12 -0
  10. package/dist/ai/rag/ui.js +11282 -0
  11. package/dist/ai/rag/ui.js.map +12 -0
  12. package/dist/angular/ai/index.js +4383 -0
  13. package/dist/angular/ai/index.js.map +17 -0
  14. package/dist/react/ai/index.js +4942 -0
  15. package/dist/react/ai/index.js.map +31 -0
  16. package/dist/src/ai/client/createRAGAnswerWorkflow.d.ts +32 -0
  17. package/dist/src/ai/client/createRAGStream.d.ts +2 -0
  18. package/dist/src/ai/client/createRAGWorkflow.d.ts +3 -0
  19. package/dist/src/ai/client/index.d.ts +8 -0
  20. package/dist/src/ai/client/ragClient.d.ts +349 -0
  21. package/dist/src/ai/client/ui.d.ts +5 -0
  22. package/dist/src/ai/rag/accessControl.d.ts +2 -0
  23. package/dist/src/ai/rag/adapters/filtering.d.ts +2 -0
  24. package/dist/src/ai/rag/adapters/inMemory.d.ts +8 -0
  25. package/dist/src/ai/rag/adapters/postgres.d.ts +20 -0
  26. package/dist/src/ai/rag/adapters/queryPlanning.d.ts +26 -0
  27. package/dist/src/ai/rag/adapters/sqlite.d.ts +24 -0
  28. package/dist/src/ai/rag/adapters/utils.d.ts +3 -0
  29. package/dist/src/ai/rag/chat.d.ts +1773 -0
  30. package/dist/src/ai/rag/collection.d.ts +15 -0
  31. package/dist/src/ai/rag/constants.d.ts +9 -0
  32. package/dist/src/ai/rag/emailProviders.d.ts +34 -0
  33. package/dist/src/ai/rag/embedding.d.ts +9 -0
  34. package/dist/src/ai/rag/embeddingProviders.d.ts +50 -0
  35. package/dist/src/ai/rag/extractorProviders.d.ts +58 -0
  36. package/dist/src/ai/rag/grounding.d.ts +6 -0
  37. package/dist/src/ai/rag/htmxConfig.d.ts +3 -0
  38. package/dist/src/ai/rag/htmxWorkflowRenderers.d.ts +3 -0
  39. package/dist/src/ai/rag/index.d.ts +30 -0
  40. package/dist/src/ai/rag/ingestion.d.ts +61 -0
  41. package/dist/src/ai/rag/jobState.d.ts +2 -0
  42. package/dist/src/ai/rag/lexical.d.ts +42 -0
  43. package/dist/src/ai/rag/presentation.d.ts +165 -0
  44. package/dist/src/ai/rag/quality.d.ts +607 -0
  45. package/dist/src/ai/rag/queryTransforms.d.ts +17 -0
  46. package/dist/src/ai/rag/reranking.d.ts +17 -0
  47. package/dist/src/ai/rag/resolveAbsoluteSQLiteVec.d.ts +3 -0
  48. package/dist/src/ai/rag/retrievalStrategies.d.ts +6 -0
  49. package/dist/src/ai/rag/sync.d.ts +37 -0
  50. package/dist/src/ai/rag/types.d.ts +11 -0
  51. package/dist/src/ai/rag/ui.d.ts +4 -0
  52. package/dist/src/ai/rag/workflowState.d.ts +1 -0
  53. package/dist/src/angular/ai/ai-rag-stream.service.d.ts +32 -0
  54. package/dist/src/angular/ai/ai-rag-workflow.service.d.ts +33 -0
  55. package/dist/src/angular/ai/index.d.ts +3 -0
  56. package/dist/src/angular/ai/rag-client.service.d.ts +243 -0
  57. package/dist/src/constants.d.ts +60 -0
  58. package/dist/src/react/ai/index.d.ts +18 -0
  59. package/dist/src/react/ai/useRAG.d.ts +341 -0
  60. package/dist/src/react/ai/useRAGChunkPreview.d.ts +20 -0
  61. package/dist/src/react/ai/useRAGCitations.d.ts +9 -0
  62. package/dist/src/react/ai/useRAGDocuments.d.ts +10 -0
  63. package/dist/src/react/ai/useRAGEvaluate.d.ts +28 -0
  64. package/dist/src/react/ai/useRAGGrounding.d.ts +10 -0
  65. package/dist/src/react/ai/useRAGIndexAdmin.d.ts +58 -0
  66. package/dist/src/react/ai/useRAGIngest.d.ts +16 -0
  67. package/dist/src/react/ai/useRAGOps.d.ts +20 -0
  68. package/dist/src/react/ai/useRAGSearch.d.ts +17 -0
  69. package/dist/src/react/ai/useRAGSources.d.ts +13 -0
  70. package/dist/src/react/ai/useRAGStatus.d.ts +10 -0
  71. package/dist/src/react/ai/useRAGStream.d.ts +55 -0
  72. package/dist/src/react/ai/useRAGStreamProgress.d.ts +9 -0
  73. package/dist/src/react/ai/useRAGWorkflow.d.ts +55 -0
  74. package/dist/src/svelte/ai/createRAG.d.ts +292 -0
  75. package/dist/src/svelte/ai/createRAGChunkPreview.d.ts +20 -0
  76. package/dist/src/svelte/ai/createRAGCitations.d.ts +10 -0
  77. package/dist/src/svelte/ai/createRAGDocuments.d.ts +10 -0
  78. package/dist/src/svelte/ai/createRAGEvaluate.d.ts +28 -0
  79. package/dist/src/svelte/ai/createRAGGrounding.d.ts +11 -0
  80. package/dist/src/svelte/ai/createRAGIndexAdmin.d.ts +58 -0
  81. package/dist/src/svelte/ai/createRAGIngest.d.ts +17 -0
  82. package/dist/src/svelte/ai/createRAGOps.d.ts +20 -0
  83. package/dist/src/svelte/ai/createRAGSearch.d.ts +15 -0
  84. package/dist/src/svelte/ai/createRAGSources.d.ts +14 -0
  85. package/dist/src/svelte/ai/createRAGStatus.d.ts +9 -0
  86. package/dist/src/svelte/ai/createRAGStream.d.ts +30 -0
  87. package/dist/src/svelte/ai/createRAGStreamProgress.d.ts +8 -0
  88. package/dist/src/svelte/ai/createRAGWorkflow.d.ts +30 -0
  89. package/dist/src/svelte/ai/index.d.ts +17 -0
  90. package/dist/src/vue/ai/index.d.ts +17 -0
  91. package/dist/src/vue/ai/useRAG.d.ts +2892 -0
  92. package/dist/src/vue/ai/useRAGChunkPreview.d.ts +152 -0
  93. package/dist/src/vue/ai/useRAGCitations.d.ts +10 -0
  94. package/dist/src/vue/ai/useRAGDocuments.d.ts +94 -0
  95. package/dist/src/vue/ai/useRAGEvaluate.d.ts +1002 -0
  96. package/dist/src/vue/ai/useRAGGrounding.d.ts +11 -0
  97. package/dist/src/vue/ai/useRAGIndexAdmin.d.ts +1104 -0
  98. package/dist/src/vue/ai/useRAGIngest.d.ts +27 -0
  99. package/dist/src/vue/ai/useRAGOps.d.ts +220 -0
  100. package/dist/src/vue/ai/useRAGSearch.d.ts +159 -0
  101. package/dist/src/vue/ai/useRAGSources.d.ts +14 -0
  102. package/dist/src/vue/ai/useRAGStatus.d.ts +9 -0
  103. package/dist/src/vue/ai/useRAGStream.d.ts +35 -0
  104. package/dist/src/vue/ai/useRAGStreamProgress.d.ts +8 -0
  105. package/dist/src/vue/ai/useRAGWorkflow.d.ts +35 -0
  106. package/dist/svelte/ai/index.js +4930 -0
  107. package/dist/svelte/ai/index.js.map +29 -0
  108. package/dist/vue/ai/index.js +4905 -0
  109. package/dist/vue/ai/index.js.map +29 -0
  110. package/package.json +81 -0
@@ -0,0 +1,4905 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __name = (target, name) => {
5
+ Object.defineProperty(target, "name", {
6
+ value: name,
7
+ enumerable: false,
8
+ configurable: true
9
+ });
10
+ return target;
11
+ };
12
+ var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
13
+ var __typeError = (msg) => {
14
+ throw TypeError(msg);
15
+ };
16
+ var __defNormalProp = (obj, key, value) => (key in obj) ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
17
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
18
+ var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
19
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
20
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
21
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
22
+ var __decoratorStart = (base) => [, , , __create(base?.[__knownSymbol("metadata")] ?? null)];
23
+ var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
24
+ var __expectFn = (fn) => fn !== undefined && typeof fn !== "function" ? __typeError("Function expected") : fn;
25
+ var __decoratorContext = (kind, name, done, metadata, fns) => ({
26
+ kind: __decoratorStrings[kind],
27
+ name,
28
+ metadata,
29
+ addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null))
30
+ });
31
+ var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
32
+ var __runInitializers = (array, flags, self, value) => {
33
+ for (var i = 0, fns = array[flags >> 1], n = fns && fns.length;i < n; i++)
34
+ flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value);
35
+ return value;
36
+ };
37
+ var __decorateElement = (array, flags, name, decorators, target, extra) => {
38
+ var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
39
+ var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
40
+ var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
41
+ var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : {
42
+ get [name]() {
43
+ return __privateGet(this, extra);
44
+ },
45
+ set [name](x) {
46
+ __privateSet(this, extra, x);
47
+ }
48
+ }, name));
49
+ k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
50
+ for (var i = decorators.length - 1;i >= 0; i--) {
51
+ ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
52
+ if (k) {
53
+ ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => (name in x) };
54
+ if (k ^ 3)
55
+ access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
56
+ if (k > 2)
57
+ access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
58
+ }
59
+ it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? undefined : { get: desc.get, set: desc.set } : target, ctx);
60
+ done._ = 1;
61
+ if (k ^ 4 || it === undefined)
62
+ __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
63
+ else if (typeof it !== "object" || it === null)
64
+ __typeError("Object expected");
65
+ else
66
+ __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
67
+ }
68
+ return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
69
+ };
70
+
71
+ // src/vue/ai/useRAGChunkPreview.ts
72
+ import { computed, ref } from "vue";
73
+
74
+ // src/ai/client/ragClient.ts
75
+ var UNFOUND_INDEX = -1;
76
+ var jsonHeaders = {
77
+ "Content-Type": "application/json"
78
+ };
79
+ var normalizeBasePath = (path) => path.endsWith("/") ? path.slice(0, UNFOUND_INDEX) : path;
80
+ var parseJson = async (response) => {
81
+ const payload = JSON.parse(await response.text());
82
+ return payload;
83
+ };
84
+ var isErrorPayload = (value) => {
85
+ if (!value || typeof value !== "object") {
86
+ return false;
87
+ }
88
+ return !("error" in value) || typeof value.error === "string";
89
+ };
90
+ var toErrorMessage = async (response) => {
91
+ try {
92
+ const payload = JSON.parse(await response.text());
93
+ if (isErrorPayload(payload) && typeof payload.error === "string" && payload.error) {
94
+ return payload.error;
95
+ }
96
+ } catch {}
97
+ return `Request failed with status ${response.status}`;
98
+ };
99
+ var createRAGClient = (options) => {
100
+ const basePath = normalizeBasePath(options.path);
101
+ const fetchImpl = options.fetch ?? fetch;
102
+ const search = async (input) => {
103
+ const response = await fetchImpl(`${basePath}/search`, {
104
+ body: JSON.stringify(input),
105
+ headers: jsonHeaders,
106
+ method: "POST"
107
+ });
108
+ if (!response.ok) {
109
+ throw new Error(await toErrorMessage(response));
110
+ }
111
+ const payload = await parseJson(response);
112
+ if (!payload.ok) {
113
+ throw new Error(payload.error ?? "RAG search failed");
114
+ }
115
+ if (input.includeTrace === true) {
116
+ return {
117
+ results: payload.results ?? [],
118
+ trace: payload.trace
119
+ };
120
+ }
121
+ return payload.results ?? [];
122
+ };
123
+ return {
124
+ async backends() {
125
+ const response = await fetchImpl(`${basePath}/backends`);
126
+ if (!response.ok) {
127
+ throw new Error(await toErrorMessage(response));
128
+ }
129
+ return parseJson(response);
130
+ },
131
+ async clearIndex() {
132
+ const response = await fetchImpl(`${basePath}/index`, {
133
+ method: "DELETE"
134
+ });
135
+ if (!response.ok) {
136
+ throw new Error(await toErrorMessage(response));
137
+ }
138
+ return parseJson(response);
139
+ },
140
+ async createDocument(input) {
141
+ const response = await fetchImpl(`${basePath}/documents`, {
142
+ body: JSON.stringify(input),
143
+ headers: jsonHeaders,
144
+ method: "POST"
145
+ });
146
+ if (!response.ok) {
147
+ return {
148
+ error: await toErrorMessage(response),
149
+ ok: false
150
+ };
151
+ }
152
+ return parseJson(response);
153
+ },
154
+ async deleteDocument(id) {
155
+ const response = await fetchImpl(`${basePath}/documents/${encodeURIComponent(id)}`, {
156
+ method: "DELETE"
157
+ });
158
+ if (!response.ok) {
159
+ return {
160
+ error: await toErrorMessage(response),
161
+ ok: false
162
+ };
163
+ }
164
+ return parseJson(response);
165
+ },
166
+ async documentChunks(id) {
167
+ const response = await fetchImpl(`${basePath}/documents/${encodeURIComponent(id)}/chunks`);
168
+ if (!response.ok) {
169
+ const error = await toErrorMessage(response);
170
+ const errorResponse = {
171
+ error,
172
+ ok: false
173
+ };
174
+ return errorResponse;
175
+ }
176
+ return parseJson(response);
177
+ },
178
+ async documents(kind) {
179
+ const query = kind ? `?kind=${encodeURIComponent(kind)}` : "";
180
+ const response = await fetchImpl(`${basePath}/documents${query}`);
181
+ if (!response.ok) {
182
+ throw new Error(await toErrorMessage(response));
183
+ }
184
+ return parseJson(response);
185
+ },
186
+ async evaluate(input) {
187
+ const response = await fetchImpl(`${basePath}/evaluate`, {
188
+ body: JSON.stringify(input),
189
+ headers: jsonHeaders,
190
+ method: "POST"
191
+ });
192
+ if (!response.ok) {
193
+ throw new Error(await toErrorMessage(response));
194
+ }
195
+ return parseJson(response);
196
+ },
197
+ async compareRetrievals(input) {
198
+ const response = await fetchImpl(`${basePath}/compare/retrieval`, {
199
+ body: JSON.stringify(input),
200
+ headers: jsonHeaders,
201
+ method: "POST"
202
+ });
203
+ if (!response.ok) {
204
+ throw new Error(await toErrorMessage(response));
205
+ }
206
+ const payload = await parseJson(response);
207
+ if (!payload.ok || !payload.comparison) {
208
+ throw new Error(payload.error ?? "RAG retrieval comparison failed");
209
+ }
210
+ return payload.comparison;
211
+ },
212
+ async retrievalComparisonHistory(input) {
213
+ const searchParams = new URLSearchParams;
214
+ if (typeof input?.limit === "number") {
215
+ searchParams.set("limit", String(input.limit));
216
+ }
217
+ if (input?.suiteId) {
218
+ searchParams.set("suiteId", input.suiteId);
219
+ }
220
+ if (input?.label) {
221
+ searchParams.set("label", input.label);
222
+ }
223
+ if (input?.winnerId) {
224
+ searchParams.set("winnerId", input.winnerId);
225
+ }
226
+ if (input?.groupKey) {
227
+ searchParams.set("groupKey", input.groupKey);
228
+ }
229
+ if (input?.tag) {
230
+ searchParams.set("tag", input.tag);
231
+ }
232
+ const suffix = searchParams.size > 0 ? `?${searchParams}` : "";
233
+ const response = await fetchImpl(`${basePath}/compare/retrieval/history${suffix}`);
234
+ if (!response.ok) {
235
+ throw new Error(await toErrorMessage(response));
236
+ }
237
+ const payload = await parseJson(response);
238
+ if (!payload.ok || !payload.runs) {
239
+ throw new Error(payload.error ?? "RAG retrieval comparison history failed");
240
+ }
241
+ return payload.runs;
242
+ },
243
+ async retrievalBaselines(input) {
244
+ const searchParams = new URLSearchParams;
245
+ if (input?.groupKey) {
246
+ searchParams.set("groupKey", input.groupKey);
247
+ }
248
+ if (input?.tag) {
249
+ searchParams.set("tag", input.tag);
250
+ }
251
+ if (typeof input?.limit === "number") {
252
+ searchParams.set("limit", String(input.limit));
253
+ }
254
+ if (input?.status) {
255
+ searchParams.set("status", input.status);
256
+ }
257
+ const suffix = searchParams.size > 0 ? `?${searchParams}` : "";
258
+ const response = await fetchImpl(`${basePath}/compare/retrieval/baselines${suffix}`);
259
+ if (!response.ok) {
260
+ throw new Error(await toErrorMessage(response));
261
+ }
262
+ const payload = await parseJson(response);
263
+ if (!payload.ok || !payload.baselines) {
264
+ throw new Error(payload.error ?? "RAG retrieval baseline list failed");
265
+ }
266
+ return payload.baselines;
267
+ },
268
+ async promoteRetrievalBaseline(input) {
269
+ const payload = await this.promoteRetrievalBaselineDetailed(input);
270
+ return payload.baseline;
271
+ },
272
+ async promoteRetrievalBaselineDetailed(input) {
273
+ const response = await fetchImpl(`${basePath}/compare/retrieval/baselines/promote`, {
274
+ body: JSON.stringify(input),
275
+ headers: jsonHeaders,
276
+ method: "POST"
277
+ });
278
+ if (!response.ok) {
279
+ throw new Error(await toErrorMessage(response));
280
+ }
281
+ const payload = await parseJson(response);
282
+ if (!payload.ok || !payload.baseline) {
283
+ throw new Error(payload.error ?? "RAG retrieval baseline promotion failed");
284
+ }
285
+ return payload;
286
+ },
287
+ async promoteRetrievalBaselineToLane(input) {
288
+ const payload = await this.promoteRetrievalBaselineToLaneDetailed(input);
289
+ return payload.baseline;
290
+ },
291
+ async promoteRetrievalBaselineToLaneDetailed(input) {
292
+ const response = await fetchImpl(`${basePath}/compare/retrieval/baselines/promote-lane`, {
293
+ body: JSON.stringify(input),
294
+ headers: jsonHeaders,
295
+ method: "POST"
296
+ });
297
+ if (!response.ok) {
298
+ throw new Error(await toErrorMessage(response));
299
+ }
300
+ const payload = await parseJson(response);
301
+ if (!payload.ok || !payload.baseline) {
302
+ throw new Error(payload.error ?? "RAG retrieval rollout-lane promotion failed");
303
+ }
304
+ return payload;
305
+ },
306
+ async promoteRetrievalBaselineFromRun(input) {
307
+ const payload = await this.promoteRetrievalBaselineFromRunDetailed(input);
308
+ return payload.baseline;
309
+ },
310
+ async promoteRetrievalBaselineFromRunDetailed(input) {
311
+ const response = await fetchImpl(`${basePath}/compare/retrieval/baselines/promote-run`, {
312
+ body: JSON.stringify(input),
313
+ headers: jsonHeaders,
314
+ method: "POST"
315
+ });
316
+ if (!response.ok) {
317
+ throw new Error(await toErrorMessage(response));
318
+ }
319
+ const payload = await parseJson(response);
320
+ if (!payload.ok || !payload.baseline) {
321
+ throw new Error(payload.error ?? "RAG retrieval baseline promotion from run failed");
322
+ }
323
+ return payload;
324
+ },
325
+ async revertRetrievalBaseline(input) {
326
+ const response = await fetchImpl(`${basePath}/compare/retrieval/baselines/revert`, {
327
+ body: JSON.stringify(input),
328
+ headers: jsonHeaders,
329
+ method: "POST"
330
+ });
331
+ if (!response.ok) {
332
+ throw new Error(await toErrorMessage(response));
333
+ }
334
+ const payload = await parseJson(response);
335
+ if (!payload.ok || !payload.baseline) {
336
+ throw new Error(payload.error ?? "RAG retrieval baseline revert failed");
337
+ }
338
+ return payload.baseline;
339
+ },
340
+ async retrievalBaselineDecisions(input) {
341
+ const searchParams = new URLSearchParams;
342
+ if (input?.groupKey) {
343
+ searchParams.set("groupKey", input.groupKey);
344
+ }
345
+ if (typeof input?.limit === "number") {
346
+ searchParams.set("limit", String(input.limit));
347
+ }
348
+ if (input?.kind) {
349
+ searchParams.set("kind", input.kind);
350
+ }
351
+ if (input?.freshnessStatus) {
352
+ searchParams.set("freshnessStatus", input.freshnessStatus);
353
+ }
354
+ if (input?.targetRolloutLabel) {
355
+ searchParams.set("targetRolloutLabel", input.targetRolloutLabel);
356
+ }
357
+ const suffix = searchParams.size > 0 ? `?${searchParams}` : "";
358
+ const response = await fetchImpl(`${basePath}/compare/retrieval/baselines/decisions${suffix}`);
359
+ if (!response.ok) {
360
+ throw new Error(await toErrorMessage(response));
361
+ }
362
+ const payload = await parseJson(response);
363
+ if (!payload.ok || !payload.decisions) {
364
+ throw new Error(payload.error ?? "RAG retrieval release decision list failed");
365
+ }
366
+ return payload.decisions;
367
+ },
368
+ async retrievalReleaseGroupHistory(input) {
369
+ const searchParams = new URLSearchParams;
370
+ searchParams.set("groupKey", input.groupKey);
371
+ if (typeof input.decisionLimit === "number") {
372
+ searchParams.set("decisionLimit", String(input.decisionLimit));
373
+ }
374
+ if (typeof input.baselineLimit === "number") {
375
+ searchParams.set("baselineLimit", String(input.baselineLimit));
376
+ }
377
+ if (typeof input.runLimit === "number") {
378
+ searchParams.set("runLimit", String(input.runLimit));
379
+ }
380
+ if (typeof input.benchmarkLimit === "number") {
381
+ searchParams.set("benchmarkLimit", String(input.benchmarkLimit));
382
+ }
383
+ if (input.targetRolloutLabel) {
384
+ searchParams.set("targetRolloutLabel", input.targetRolloutLabel);
385
+ }
386
+ const response = await fetchImpl(`${basePath}/compare/retrieval/release-history?${searchParams}`);
387
+ if (!response.ok) {
388
+ throw new Error(await toErrorMessage(response));
389
+ }
390
+ const payload = await parseJson(response);
391
+ if (!payload.ok) {
392
+ throw new Error(payload.error ?? "RAG retrieval release group history failed");
393
+ }
394
+ return payload;
395
+ },
396
+ async adaptiveNativePlannerBenchmark(input) {
397
+ const searchParams = new URLSearchParams;
398
+ if (typeof input?.limit === "number") {
399
+ searchParams.set("limit", String(input.limit));
400
+ }
401
+ if (typeof input?.runLimit === "number") {
402
+ searchParams.set("runLimit", String(input.runLimit));
403
+ }
404
+ if (input?.label) {
405
+ searchParams.set("label", input.label);
406
+ }
407
+ if (input?.description) {
408
+ searchParams.set("description", input.description);
409
+ }
410
+ if (input?.groupKey) {
411
+ searchParams.set("benchmarkGroupKey", input.groupKey);
412
+ }
413
+ if (input?.corpusGroupKey) {
414
+ searchParams.set("benchmarkCorpusGroupKey", input.corpusGroupKey);
415
+ }
416
+ const suffix = searchParams.size ? `?${searchParams}` : "";
417
+ const response = await fetchImpl(`${basePath}/compare/retrieval/benchmarks/adaptive-native-planner${suffix}`);
418
+ if (!response.ok) {
419
+ throw new Error(await toErrorMessage(response));
420
+ }
421
+ const payload = await parseJson(response);
422
+ if (!payload.ok) {
423
+ throw new Error(payload.error ?? "Adaptive native planner benchmark history failed");
424
+ }
425
+ return payload;
426
+ },
427
+ async runAdaptiveNativePlannerBenchmark(input) {
428
+ const response = await fetchImpl(`${basePath}/compare/retrieval/benchmarks/adaptive-native-planner/run`, {
429
+ body: JSON.stringify({
430
+ baselineRetrievalId: input?.baselineRetrievalId,
431
+ candidateRetrievalId: input?.candidateRetrievalId,
432
+ corpusGroupKey: input?.corpusGroupKey,
433
+ description: input?.description,
434
+ groupKey: input?.groupKey,
435
+ label: input?.label,
436
+ limit: input?.limit,
437
+ metadata: input?.metadata,
438
+ persistRun: input?.persistRun,
439
+ retrievals: input?.retrievals,
440
+ runLimit: input?.runLimit,
441
+ tags: input?.tags,
442
+ topK: input?.topK
443
+ }),
444
+ headers: jsonHeaders,
445
+ method: "POST"
446
+ });
447
+ if (!response.ok) {
448
+ throw new Error(await toErrorMessage(response));
449
+ }
450
+ const payload = await parseJson(response);
451
+ if (!payload.ok) {
452
+ throw new Error(payload.error ?? "Adaptive native planner benchmark run failed");
453
+ }
454
+ return payload;
455
+ },
456
+ async saveAdaptiveNativePlannerBenchmarkSnapshot(input) {
457
+ const response = await fetchImpl(`${basePath}/compare/retrieval/benchmarks/adaptive-native-planner/snapshots`, {
458
+ body: JSON.stringify({
459
+ createdAt: input?.createdAt,
460
+ description: input?.description,
461
+ label: input?.label,
462
+ limit: input?.limit,
463
+ metadata: input?.metadata,
464
+ snapshotMetadata: input?.snapshotMetadata,
465
+ version: input?.version
466
+ }),
467
+ headers: jsonHeaders,
468
+ method: "POST"
469
+ });
470
+ if (!response.ok) {
471
+ throw new Error(await toErrorMessage(response));
472
+ }
473
+ const payload = await parseJson(response);
474
+ if (!payload.ok) {
475
+ throw new Error(payload.error ?? "Adaptive native planner benchmark snapshot failed");
476
+ }
477
+ return payload;
478
+ },
479
+ async nativeBackendComparisonBenchmark(input) {
480
+ const searchParams = new URLSearchParams;
481
+ if (typeof input?.limit === "number") {
482
+ searchParams.set("limit", String(input.limit));
483
+ }
484
+ if (typeof input?.runLimit === "number") {
485
+ searchParams.set("runLimit", String(input.runLimit));
486
+ }
487
+ if (input?.label) {
488
+ searchParams.set("label", input.label);
489
+ }
490
+ if (input?.description) {
491
+ searchParams.set("description", input.description);
492
+ }
493
+ if (input?.groupKey) {
494
+ searchParams.set("benchmarkGroupKey", input.groupKey);
495
+ }
496
+ if (input?.corpusGroupKey) {
497
+ searchParams.set("benchmarkCorpusGroupKey", input.corpusGroupKey);
498
+ }
499
+ const suffix = searchParams.size ? `?${searchParams}` : "";
500
+ const response = await fetchImpl(`${basePath}/compare/retrieval/benchmarks/native-backend-comparison${suffix}`);
501
+ if (!response.ok) {
502
+ throw new Error(await toErrorMessage(response));
503
+ }
504
+ const payload = await parseJson(response);
505
+ if (!payload.ok) {
506
+ throw new Error(payload.error ?? "Native backend comparison benchmark history failed");
507
+ }
508
+ return payload;
509
+ },
510
+ async runNativeBackendComparisonBenchmark(input) {
511
+ const response = await fetchImpl(`${basePath}/compare/retrieval/benchmarks/native-backend-comparison/run`, {
512
+ body: JSON.stringify({
513
+ baselineRetrievalId: input?.baselineRetrievalId,
514
+ candidateRetrievalId: input?.candidateRetrievalId,
515
+ corpusGroupKey: input?.corpusGroupKey,
516
+ description: input?.description,
517
+ groupKey: input?.groupKey,
518
+ label: input?.label,
519
+ limit: input?.limit,
520
+ metadata: input?.metadata,
521
+ persistRun: input?.persistRun,
522
+ retrievals: input?.retrievals,
523
+ runLimit: input?.runLimit,
524
+ tags: input?.tags,
525
+ topK: input?.topK
526
+ }),
527
+ headers: jsonHeaders,
528
+ method: "POST"
529
+ });
530
+ if (!response.ok) {
531
+ throw new Error(await toErrorMessage(response));
532
+ }
533
+ const payload = await parseJson(response);
534
+ if (!payload.ok) {
535
+ throw new Error(payload.error ?? "Native backend comparison benchmark run failed");
536
+ }
537
+ return payload;
538
+ },
539
+ async saveNativeBackendComparisonBenchmarkSnapshot(input) {
540
+ const response = await fetchImpl(`${basePath}/compare/retrieval/benchmarks/native-backend-comparison/snapshots`, {
541
+ body: JSON.stringify({
542
+ createdAt: input?.createdAt,
543
+ description: input?.description,
544
+ label: input?.label,
545
+ limit: input?.limit,
546
+ metadata: input?.metadata,
547
+ snapshotMetadata: input?.snapshotMetadata,
548
+ version: input?.version
549
+ }),
550
+ headers: jsonHeaders,
551
+ method: "POST"
552
+ });
553
+ if (!response.ok) {
554
+ throw new Error(await toErrorMessage(response));
555
+ }
556
+ const payload = await parseJson(response);
557
+ if (!payload.ok) {
558
+ throw new Error(payload.error ?? "Native backend comparison benchmark snapshot failed");
559
+ }
560
+ return payload;
561
+ },
562
+ async retrievalLaneHandoffs(input) {
563
+ const searchParams = new URLSearchParams;
564
+ if (input?.groupKey) {
565
+ searchParams.set("groupKey", input.groupKey);
566
+ }
567
+ if (input?.sourceRolloutLabel) {
568
+ searchParams.set("sourceRolloutLabel", input.sourceRolloutLabel);
569
+ }
570
+ if (input?.targetRolloutLabel) {
571
+ searchParams.set("targetRolloutLabel", input.targetRolloutLabel);
572
+ }
573
+ if (typeof input?.limit === "number") {
574
+ searchParams.set("limit", String(input.limit));
575
+ }
576
+ const suffix = searchParams.size > 0 ? `?${searchParams}` : "";
577
+ const response = await fetchImpl(`${basePath}/compare/retrieval/handoffs${suffix}`);
578
+ if (!response.ok) {
579
+ throw new Error(await toErrorMessage(response));
580
+ }
581
+ const payload = await parseJson(response);
582
+ if (!payload.ok || !payload.handoffs) {
583
+ throw new Error(payload.error ?? "RAG retrieval lane handoff list failed");
584
+ }
585
+ return payload.handoffs;
586
+ },
587
+ async retrievalLaneHandoffDecisions(input) {
588
+ const searchParams = new URLSearchParams;
589
+ if (input?.groupKey) {
590
+ searchParams.set("groupKey", input.groupKey);
591
+ }
592
+ if (input?.sourceRolloutLabel) {
593
+ searchParams.set("sourceRolloutLabel", input.sourceRolloutLabel);
594
+ }
595
+ if (input?.targetRolloutLabel) {
596
+ searchParams.set("targetRolloutLabel", input.targetRolloutLabel);
597
+ }
598
+ if (input?.kind) {
599
+ searchParams.set("kind", input.kind);
600
+ }
601
+ if (typeof input?.limit === "number") {
602
+ searchParams.set("limit", String(input.limit));
603
+ }
604
+ const suffix = searchParams.size > 0 ? `?${searchParams}` : "";
605
+ const response = await fetchImpl(`${basePath}/compare/retrieval/handoffs/decisions${suffix}`);
606
+ if (!response.ok) {
607
+ throw new Error(await toErrorMessage(response));
608
+ }
609
+ const payload = await parseJson(response);
610
+ if (!payload.ok || !payload.decisions) {
611
+ throw new Error(payload.error ?? "RAG retrieval lane handoff decision list failed");
612
+ }
613
+ return payload.decisions;
614
+ },
615
+ async retrievalLaneHandoffIncidents(input) {
616
+ const searchParams = new URLSearchParams;
617
+ if (input?.groupKey)
618
+ searchParams.set("groupKey", input.groupKey);
619
+ if (typeof input?.limit === "number")
620
+ searchParams.set("limit", String(input.limit));
621
+ if (input?.status)
622
+ searchParams.set("status", input.status);
623
+ if (input?.severity)
624
+ searchParams.set("severity", input.severity);
625
+ if (input?.targetRolloutLabel) {
626
+ searchParams.set("targetRolloutLabel", input.targetRolloutLabel);
627
+ }
628
+ const suffix = searchParams.size > 0 ? `?${searchParams}` : "";
629
+ const response = await fetchImpl(`${basePath}/compare/retrieval/handoffs/incidents${suffix}`);
630
+ if (!response.ok) {
631
+ throw new Error(await toErrorMessage(response));
632
+ }
633
+ const payload = await parseJson(response);
634
+ if (!payload.ok || !payload.incidents) {
635
+ throw new Error(payload.error ?? "RAG retrieval lane handoff incident list failed");
636
+ }
637
+ return payload.incidents;
638
+ },
639
+ async retrievalLaneHandoffIncidentHistory(input) {
640
+ const suffix = input ? `?${new URLSearchParams(Object.entries(input).filter(([, value]) => value !== undefined).map(([key, value]) => [key, String(value)])).toString()}` : "";
641
+ const response = await fetchImpl(`${basePath}/compare/retrieval/handoffs/incidents/history${suffix}`);
642
+ if (!response.ok) {
643
+ throw new Error(await toErrorMessage(response));
644
+ }
645
+ const payload = await parseJson(response);
646
+ if (!payload.ok || !payload.records) {
647
+ throw new Error(payload.error ?? "RAG retrieval lane handoff incident history failed");
648
+ }
649
+ return payload.records;
650
+ },
651
+ async retrievalLaneHandoffAutoCompletePolicyHistory(input) {
652
+ const suffix = input ? `?${new URLSearchParams(Object.entries(input).filter(([, value]) => value !== undefined).map(([key, value]) => [key, String(value)])).toString()}` : "";
653
+ const response = await fetchImpl(`${basePath}/compare/retrieval/handoffs/policies/history${suffix}`);
654
+ if (!response.ok) {
655
+ throw new Error(await toErrorMessage(response));
656
+ }
657
+ const payload = await parseJson(response);
658
+ if (!payload.ok || !payload.records) {
659
+ throw new Error(payload.error ?? "RAG retrieval lane handoff auto-complete policy history failed");
660
+ }
661
+ return payload.records;
662
+ },
663
+ async retrievalReleaseLanePolicyHistory(input) {
664
+ const suffix = input ? `?${new URLSearchParams(Object.entries(input).filter(([, value]) => value !== undefined).map(([key, value]) => [key, String(value)])).toString()}` : "";
665
+ const response = await fetchImpl(`${basePath}/compare/retrieval/release-policies/history${suffix}`);
666
+ if (!response.ok) {
667
+ throw new Error(await toErrorMessage(response));
668
+ }
669
+ const payload = await parseJson(response);
670
+ if (!payload.ok || !payload.records) {
671
+ throw new Error(payload.error ?? "RAG retrieval release lane policy history failed");
672
+ }
673
+ return payload.records;
674
+ },
675
+ async retrievalBaselineGatePolicyHistory(input) {
676
+ const suffix = input ? `?${new URLSearchParams(Object.entries(input).filter(([, value]) => value !== undefined).map(([key, value]) => [key, String(value)])).toString()}` : "";
677
+ const response = await fetchImpl(`${basePath}/compare/retrieval/gate-policies/history${suffix}`);
678
+ if (!response.ok) {
679
+ throw new Error(await toErrorMessage(response));
680
+ }
681
+ const payload = await parseJson(response);
682
+ if (!payload.ok || !payload.records) {
683
+ throw new Error(payload.error ?? "RAG retrieval baseline gate policy history failed");
684
+ }
685
+ return payload.records;
686
+ },
687
+ async retrievalReleaseLaneEscalationPolicyHistory(input) {
688
+ const suffix = input ? `?${new URLSearchParams(Object.entries(input).filter(([, value]) => value !== undefined).map(([key, value]) => [key, String(value)])).toString()}` : "";
689
+ const response = await fetchImpl(`${basePath}/compare/retrieval/escalation-policies/history${suffix}`);
690
+ if (!response.ok) {
691
+ throw new Error(await toErrorMessage(response));
692
+ }
693
+ const payload = await parseJson(response);
694
+ if (!payload.ok || !payload.records) {
695
+ throw new Error(payload.error ?? "RAG retrieval release lane escalation policy history failed");
696
+ }
697
+ return payload.records;
698
+ },
699
+ async retrievalReleaseIncidentPolicyHistory(input) {
700
+ const suffix = input ? `?${new URLSearchParams(Object.entries(input).filter(([, value]) => value !== undefined).map(([key, value]) => [key, String(value)])).toString()}` : "";
701
+ const response = await fetchImpl(`${basePath}/compare/retrieval/incident-policies/history${suffix}`);
702
+ if (!response.ok) {
703
+ throw new Error(await toErrorMessage(response));
704
+ }
705
+ const payload = await parseJson(response);
706
+ if (!payload.ok || !payload.records) {
707
+ throw new Error(payload.error ?? "RAG retrieval release incident policy history failed");
708
+ }
709
+ return payload.records;
710
+ },
711
+ async retrievalReleaseIncidentStatus() {
712
+ const response = await fetchImpl(`${basePath}/status/release/incidents`);
713
+ if (!response.ok) {
714
+ throw new Error(await toErrorMessage(response));
715
+ }
716
+ return parseJson(response);
717
+ },
718
+ async retrievalIncidentRemediationStatus() {
719
+ const response = await fetchImpl(`${basePath}/status/release/remediations`);
720
+ if (!response.ok) {
721
+ throw new Error(await toErrorMessage(response));
722
+ }
723
+ return parseJson(response);
724
+ },
725
+ async retrievalLaneHandoffIncidentStatus() {
726
+ const response = await fetchImpl(`${basePath}/status/handoffs/incidents`);
727
+ if (!response.ok) {
728
+ throw new Error(await toErrorMessage(response));
729
+ }
730
+ return parseJson(response);
731
+ },
732
+ async acknowledgeRetrievalLaneHandoffIncident(input) {
733
+ const response = await fetchImpl(`${basePath}/compare/retrieval/handoffs/incidents/acknowledge`, {
734
+ body: JSON.stringify(input),
735
+ headers: jsonHeaders,
736
+ method: "POST"
737
+ });
738
+ if (!response.ok) {
739
+ throw new Error(await toErrorMessage(response));
740
+ }
741
+ const payload = await parseJson(response);
742
+ if (!payload.ok || !payload.incidents) {
743
+ throw new Error(payload.error ?? "RAG retrieval lane handoff incident acknowledgement failed");
744
+ }
745
+ return payload.incidents;
746
+ },
747
+ async unacknowledgeRetrievalLaneHandoffIncident(input) {
748
+ const response = await fetchImpl(`${basePath}/compare/retrieval/handoffs/incidents/unacknowledge`, {
749
+ body: JSON.stringify(input),
750
+ headers: jsonHeaders,
751
+ method: "POST"
752
+ });
753
+ if (!response.ok) {
754
+ throw new Error(await toErrorMessage(response));
755
+ }
756
+ const payload = await parseJson(response);
757
+ if (!payload.ok || !payload.incidents) {
758
+ throw new Error(payload.error ?? "RAG retrieval lane handoff incident unacknowledge failed");
759
+ }
760
+ return payload.incidents;
761
+ },
762
+ async resolveRetrievalLaneHandoffIncident(input) {
763
+ const response = await fetchImpl(`${basePath}/compare/retrieval/handoffs/incidents/resolve`, {
764
+ body: JSON.stringify(input),
765
+ headers: jsonHeaders,
766
+ method: "POST"
767
+ });
768
+ if (!response.ok) {
769
+ throw new Error(await toErrorMessage(response));
770
+ }
771
+ const payload = await parseJson(response);
772
+ if (!payload.ok || !payload.incidents) {
773
+ throw new Error(payload.error ?? "RAG retrieval lane handoff incident resolve failed");
774
+ }
775
+ return payload.incidents;
776
+ },
777
+ async decideRetrievalLaneHandoff(input) {
778
+ const payload = await this.decideRetrievalLaneHandoffDetailed(input);
779
+ if (!payload.decision) {
780
+ throw new Error(payload.error ?? "RAG retrieval lane handoff decision failed");
781
+ }
782
+ return payload.decision;
783
+ },
784
+ async decideRetrievalLaneHandoffDetailed(input) {
785
+ const response = await fetchImpl(`${basePath}/compare/retrieval/handoffs/decide`, {
786
+ body: JSON.stringify(input),
787
+ headers: jsonHeaders,
788
+ method: "POST"
789
+ });
790
+ if (!response.ok) {
791
+ throw new Error(await toErrorMessage(response));
792
+ }
793
+ return parseJson(response);
794
+ },
795
+ async retrievalReleaseIncidents(input) {
796
+ const searchParams = new URLSearchParams;
797
+ if (input?.groupKey) {
798
+ searchParams.set("groupKey", input.groupKey);
799
+ }
800
+ if (typeof input?.limit === "number") {
801
+ searchParams.set("limit", String(input.limit));
802
+ }
803
+ if (input?.status) {
804
+ searchParams.set("status", input.status);
805
+ }
806
+ if (input?.severity) {
807
+ searchParams.set("severity", input.severity);
808
+ }
809
+ if (input?.kind) {
810
+ searchParams.set("kind", input.kind);
811
+ }
812
+ if (typeof input?.acknowledged === "boolean") {
813
+ searchParams.set("acknowledged", String(input.acknowledged));
814
+ }
815
+ if (input?.targetRolloutLabel) {
816
+ searchParams.set("targetRolloutLabel", input.targetRolloutLabel);
817
+ }
818
+ const suffix = searchParams.size > 0 ? `?${searchParams}` : "";
819
+ const response = await fetchImpl(`${basePath}/compare/retrieval/incidents${suffix}`);
820
+ if (!response.ok) {
821
+ throw new Error(await toErrorMessage(response));
822
+ }
823
+ const payload = await parseJson(response);
824
+ if (!payload.ok || !payload.incidents) {
825
+ throw new Error(payload.error ?? "RAG retrieval release incident list failed");
826
+ }
827
+ return payload.incidents;
828
+ },
829
+ async retrievalIncidentRemediationDecisions(input) {
830
+ const suffix = input ? `?${new URLSearchParams(Object.entries(input).filter(([, value]) => value !== undefined).map(([key, value]) => [key, String(value)])).toString()}` : "";
831
+ const response = await fetchImpl(`${basePath}/compare/retrieval/incidents/remediations${suffix}`);
832
+ if (!response.ok) {
833
+ throw new Error(await toErrorMessage(response));
834
+ }
835
+ const payload = await parseJson(response);
836
+ if (!payload.ok || !payload.records) {
837
+ throw new Error(payload.error ?? "RAG retrieval incident remediation decision list failed");
838
+ }
839
+ return payload.records;
840
+ },
841
+ async retrievalIncidentRemediationExecutions(input) {
842
+ const suffix = input ? `?${new URLSearchParams(Object.entries(input).filter(([, value]) => value !== undefined).map(([key, value]) => [key, String(value)])).toString()}` : "";
843
+ const response = await fetchImpl(`${basePath}/compare/retrieval/incidents/remediations/executions${suffix}`);
844
+ if (!response.ok) {
845
+ throw new Error(await toErrorMessage(response));
846
+ }
847
+ const payload = await parseJson(response);
848
+ if (!payload.ok || !payload.records) {
849
+ throw new Error(payload.error ?? "RAG retrieval incident remediation execution history failed");
850
+ }
851
+ return payload.records;
852
+ },
853
+ async recordRetrievalIncidentRemediationDecision(input) {
854
+ const response = await fetchImpl(`${basePath}/compare/retrieval/incidents/remediations`, {
855
+ body: JSON.stringify(input),
856
+ headers: jsonHeaders,
857
+ method: "POST"
858
+ });
859
+ if (!response.ok) {
860
+ throw new Error(await toErrorMessage(response));
861
+ }
862
+ const payload = await parseJson(response);
863
+ if (!payload.ok || !payload.records) {
864
+ throw new Error(payload.error ?? "RAG retrieval incident remediation decision record failed");
865
+ }
866
+ return payload.records;
867
+ },
868
+ async executeRetrievalIncidentRemediation(input) {
869
+ const response = await fetchImpl(`${basePath}/compare/retrieval/incidents/remediations/execute`, {
870
+ body: JSON.stringify(input),
871
+ headers: jsonHeaders,
872
+ method: "POST"
873
+ });
874
+ if (!response.ok) {
875
+ throw new Error(await toErrorMessage(response));
876
+ }
877
+ const payload = await parseJson(response);
878
+ if (!payload.ok || !payload.execution) {
879
+ throw new Error(payload.error ?? "RAG retrieval incident remediation execution failed");
880
+ }
881
+ return payload;
882
+ },
883
+ async bulkExecuteRetrievalIncidentRemediations(input) {
884
+ const response = await fetchImpl(`${basePath}/compare/retrieval/incidents/remediations/execute/bulk`, {
885
+ body: JSON.stringify(input),
886
+ headers: jsonHeaders,
887
+ method: "POST"
888
+ });
889
+ if (!response.ok) {
890
+ throw new Error(await toErrorMessage(response));
891
+ }
892
+ const payload = await parseJson(response);
893
+ if (!payload.ok || !payload.results) {
894
+ throw new Error(payload.error ?? "Bulk RAG retrieval incident remediation execution failed");
895
+ }
896
+ return payload.results;
897
+ },
898
+ async acknowledgeRetrievalReleaseIncident(input) {
899
+ const response = await fetchImpl(`${basePath}/compare/retrieval/incidents/acknowledge`, {
900
+ body: JSON.stringify(input),
901
+ headers: jsonHeaders,
902
+ method: "POST"
903
+ });
904
+ if (!response.ok) {
905
+ throw new Error(await toErrorMessage(response));
906
+ }
907
+ const payload = await parseJson(response);
908
+ if (!payload.ok || !payload.incidents) {
909
+ throw new Error(payload.error ?? "RAG retrieval release incident acknowledgement failed");
910
+ }
911
+ return payload.incidents;
912
+ },
913
+ async unacknowledgeRetrievalReleaseIncident(input) {
914
+ const response = await fetchImpl(`${basePath}/compare/retrieval/incidents/unacknowledge`, {
915
+ body: JSON.stringify(input),
916
+ headers: jsonHeaders,
917
+ method: "POST"
918
+ });
919
+ if (!response.ok) {
920
+ throw new Error(await toErrorMessage(response));
921
+ }
922
+ const payload = await parseJson(response);
923
+ if (!payload.ok || !payload.incidents) {
924
+ throw new Error(payload.error ?? "RAG retrieval release incident unacknowledge failed");
925
+ }
926
+ return payload.incidents;
927
+ },
928
+ async resolveRetrievalReleaseIncident(input) {
929
+ const response = await fetchImpl(`${basePath}/compare/retrieval/incidents/resolve`, {
930
+ body: JSON.stringify(input),
931
+ headers: jsonHeaders,
932
+ method: "POST"
933
+ });
934
+ if (!response.ok) {
935
+ throw new Error(await toErrorMessage(response));
936
+ }
937
+ const payload = await parseJson(response);
938
+ if (!payload.ok || !payload.incidents) {
939
+ throw new Error(payload.error ?? "RAG retrieval release incident resolve failed");
940
+ }
941
+ return payload.incidents;
942
+ },
943
+ async retrievalPromotionCandidates(input) {
944
+ const searchParams = new URLSearchParams;
945
+ if (input?.groupKey) {
946
+ searchParams.set("groupKey", input.groupKey);
947
+ }
948
+ if (typeof input?.limit === "number") {
949
+ searchParams.set("limit", String(input.limit));
950
+ }
951
+ if (input?.tag) {
952
+ searchParams.set("tag", input.tag);
953
+ }
954
+ if (input?.targetRolloutLabel) {
955
+ searchParams.set("targetRolloutLabel", input.targetRolloutLabel);
956
+ }
957
+ if (typeof input?.approved === "boolean") {
958
+ searchParams.set("approved", String(input.approved));
959
+ }
960
+ if (typeof input?.ready === "boolean") {
961
+ searchParams.set("ready", String(input.ready));
962
+ }
963
+ if (typeof input?.blocked === "boolean") {
964
+ searchParams.set("blocked", String(input.blocked));
965
+ }
966
+ if (input?.reviewStatus) {
967
+ searchParams.set("reviewStatus", input.reviewStatus);
968
+ }
969
+ if (input?.freshnessStatus) {
970
+ searchParams.set("freshnessStatus", input.freshnessStatus);
971
+ }
972
+ if (input?.sortBy) {
973
+ searchParams.set("sortBy", input.sortBy);
974
+ }
975
+ if (input?.sortDirection) {
976
+ searchParams.set("sortDirection", input.sortDirection);
977
+ }
978
+ const suffix = searchParams.size > 0 ? `?${searchParams}` : "";
979
+ const response = await fetchImpl(`${basePath}/compare/retrieval/candidates${suffix}`);
980
+ if (!response.ok) {
981
+ throw new Error(await toErrorMessage(response));
982
+ }
983
+ const payload = await parseJson(response);
984
+ if (!payload.ok || !payload.candidates) {
985
+ throw new Error(payload.error ?? "RAG retrieval promotion candidate list failed");
986
+ }
987
+ return payload.candidates;
988
+ },
989
+ async approveRetrievalCandidate(input) {
990
+ const response = await fetchImpl(`${basePath}/compare/retrieval/baselines/approve`, {
991
+ body: JSON.stringify(input),
992
+ headers: jsonHeaders,
993
+ method: "POST"
994
+ });
995
+ if (!response.ok) {
996
+ throw new Error(await toErrorMessage(response));
997
+ }
998
+ const payload = await parseJson(response);
999
+ if (!payload.ok || !payload.decisions) {
1000
+ throw new Error(payload.error ?? "RAG retrieval approval failed");
1001
+ }
1002
+ return payload.decisions;
1003
+ },
1004
+ async rejectRetrievalCandidate(input) {
1005
+ const response = await fetchImpl(`${basePath}/compare/retrieval/baselines/reject`, {
1006
+ body: JSON.stringify(input),
1007
+ headers: jsonHeaders,
1008
+ method: "POST"
1009
+ });
1010
+ if (!response.ok) {
1011
+ throw new Error(await toErrorMessage(response));
1012
+ }
1013
+ const payload = await parseJson(response);
1014
+ if (!payload.ok || !payload.decisions) {
1015
+ throw new Error(payload.error ?? "RAG retrieval rejection failed");
1016
+ }
1017
+ return payload.decisions;
1018
+ },
1019
+ async ingest(chunks) {
1020
+ const response = await fetchImpl(`${basePath}/ingest`, {
1021
+ body: JSON.stringify({ chunks }),
1022
+ headers: jsonHeaders,
1023
+ method: "POST"
1024
+ });
1025
+ if (!response.ok) {
1026
+ return {
1027
+ error: await toErrorMessage(response),
1028
+ ok: false
1029
+ };
1030
+ }
1031
+ return parseJson(response);
1032
+ },
1033
+ async ingestDocuments(input) {
1034
+ const response = await fetchImpl(`${basePath}/ingest`, {
1035
+ body: JSON.stringify(input),
1036
+ headers: jsonHeaders,
1037
+ method: "POST"
1038
+ });
1039
+ if (!response.ok) {
1040
+ return {
1041
+ error: await toErrorMessage(response),
1042
+ ok: false
1043
+ };
1044
+ }
1045
+ return parseJson(response);
1046
+ },
1047
+ async ingestUploads(input) {
1048
+ const response = await fetchImpl(`${basePath}/ingest`, {
1049
+ body: JSON.stringify(input),
1050
+ headers: jsonHeaders,
1051
+ method: "POST"
1052
+ });
1053
+ if (!response.ok) {
1054
+ return {
1055
+ error: await toErrorMessage(response),
1056
+ ok: false
1057
+ };
1058
+ }
1059
+ return parseJson(response);
1060
+ },
1061
+ async ingestUrls(input) {
1062
+ const response = await fetchImpl(`${basePath}/ingest`, {
1063
+ body: JSON.stringify(input),
1064
+ headers: jsonHeaders,
1065
+ method: "POST"
1066
+ });
1067
+ if (!response.ok) {
1068
+ return {
1069
+ error: await toErrorMessage(response),
1070
+ ok: false
1071
+ };
1072
+ }
1073
+ return parseJson(response);
1074
+ },
1075
+ async analyzeBackend() {
1076
+ const response = await fetchImpl(`${basePath}/backend/analyze`, {
1077
+ method: "POST"
1078
+ });
1079
+ if (!response.ok) {
1080
+ return {
1081
+ error: await toErrorMessage(response),
1082
+ ok: false
1083
+ };
1084
+ }
1085
+ return parseJson(response);
1086
+ },
1087
+ async ops() {
1088
+ const response = await fetchImpl(`${basePath}/ops`);
1089
+ if (!response.ok) {
1090
+ throw new Error(await toErrorMessage(response));
1091
+ }
1092
+ return parseJson(response);
1093
+ },
1094
+ async searchTraceHistory(input) {
1095
+ const searchParams = new URLSearchParams;
1096
+ if (input?.query) {
1097
+ searchParams.set("query", input.query);
1098
+ }
1099
+ if (input?.groupKey) {
1100
+ searchParams.set("groupKey", input.groupKey);
1101
+ }
1102
+ if (input?.tag) {
1103
+ searchParams.set("tag", input.tag);
1104
+ }
1105
+ if (typeof input?.limit === "number") {
1106
+ searchParams.set("limit", String(input.limit));
1107
+ }
1108
+ const suffix = searchParams.size > 0 ? `?${searchParams}` : "";
1109
+ const response = await fetchImpl(`${basePath}/traces${suffix}`);
1110
+ if (!response.ok) {
1111
+ throw new Error(await toErrorMessage(response));
1112
+ }
1113
+ const payload = await parseJson(response);
1114
+ if (!payload.ok || !payload.history) {
1115
+ throw new Error(payload.error ?? "RAG search trace history failed");
1116
+ }
1117
+ return payload.history;
1118
+ },
1119
+ async searchTraceGroups(input) {
1120
+ const searchParams = new URLSearchParams;
1121
+ if (input?.tag) {
1122
+ searchParams.set("tag", input.tag);
1123
+ }
1124
+ if (typeof input?.limit === "number") {
1125
+ searchParams.set("limit", String(input.limit));
1126
+ }
1127
+ const suffix = searchParams.size > 0 ? `?${searchParams}` : "";
1128
+ const response = await fetchImpl(`${basePath}/traces/groups${suffix}`);
1129
+ if (!response.ok) {
1130
+ throw new Error(await toErrorMessage(response));
1131
+ }
1132
+ const payload = await parseJson(response);
1133
+ if (!payload.ok || !payload.history) {
1134
+ throw new Error(payload.error ?? "RAG search trace group history failed");
1135
+ }
1136
+ return payload.history;
1137
+ },
1138
+ async searchTraceStats(input) {
1139
+ const searchParams = new URLSearchParams;
1140
+ if (input?.tag) {
1141
+ searchParams.set("tag", input.tag);
1142
+ }
1143
+ const suffix = searchParams.size > 0 ? `?${searchParams}` : "";
1144
+ const response = await fetchImpl(`${basePath}/traces/stats${suffix}`);
1145
+ if (!response.ok) {
1146
+ throw new Error(await toErrorMessage(response));
1147
+ }
1148
+ const payload = await parseJson(response);
1149
+ if (!payload.ok || !payload.stats) {
1150
+ throw new Error(payload.error ?? "RAG search trace stats failed");
1151
+ }
1152
+ return payload.stats;
1153
+ },
1154
+ async previewSearchTracePrune(input) {
1155
+ const response = await fetchImpl(`${basePath}/traces/prune/preview`, {
1156
+ body: JSON.stringify(input ?? {}),
1157
+ headers: jsonHeaders,
1158
+ method: "POST"
1159
+ });
1160
+ if (!response.ok) {
1161
+ throw new Error(await toErrorMessage(response));
1162
+ }
1163
+ const payload = await parseJson(response);
1164
+ if (!payload.ok || !payload.preview) {
1165
+ throw new Error(payload.error ?? "RAG search trace prune preview failed");
1166
+ }
1167
+ return payload.preview;
1168
+ },
1169
+ async pruneSearchTraces(input) {
1170
+ const response = await fetchImpl(`${basePath}/traces/prune`, {
1171
+ body: JSON.stringify(input ?? {}),
1172
+ headers: jsonHeaders,
1173
+ method: "POST"
1174
+ });
1175
+ if (!response.ok) {
1176
+ throw new Error(await toErrorMessage(response));
1177
+ }
1178
+ const payload = await parseJson(response);
1179
+ if (!payload.ok || !payload.result) {
1180
+ throw new Error(payload.error ?? "RAG search trace prune failed");
1181
+ }
1182
+ return payload;
1183
+ },
1184
+ async searchTracePruneHistory(input) {
1185
+ const searchParams = new URLSearchParams;
1186
+ if (typeof input?.limit === "number") {
1187
+ searchParams.set("limit", String(input.limit));
1188
+ }
1189
+ if (input?.trigger) {
1190
+ searchParams.set("trigger", input.trigger);
1191
+ }
1192
+ const suffix = searchParams.size > 0 ? `?${searchParams}` : "";
1193
+ const response = await fetchImpl(`${basePath}/traces/prune/history${suffix}`);
1194
+ if (!response.ok) {
1195
+ throw new Error(await toErrorMessage(response));
1196
+ }
1197
+ const payload = await parseJson(response);
1198
+ if (!payload.ok || !payload.runs) {
1199
+ throw new Error(payload.error ?? "RAG search trace prune history failed");
1200
+ }
1201
+ return payload.runs;
1202
+ },
1203
+ async syncSources() {
1204
+ const response = await fetchImpl(`${basePath}/sync`);
1205
+ if (!response.ok) {
1206
+ throw new Error(await toErrorMessage(response));
1207
+ }
1208
+ return parseJson(response);
1209
+ },
1210
+ async syncAllSources(options2) {
1211
+ const response = await fetchImpl(`${basePath}/sync`, {
1212
+ body: options2?.background === true ? JSON.stringify({ background: true }) : undefined,
1213
+ headers: options2?.background === true ? jsonHeaders : undefined,
1214
+ method: "POST"
1215
+ });
1216
+ if (!response.ok) {
1217
+ return {
1218
+ error: await toErrorMessage(response),
1219
+ ok: false
1220
+ };
1221
+ }
1222
+ return parseJson(response);
1223
+ },
1224
+ async syncSource(id, options2) {
1225
+ const response = await fetchImpl(`${basePath}/sync/${encodeURIComponent(id)}`, {
1226
+ body: options2?.background === true ? JSON.stringify({ background: true }) : undefined,
1227
+ headers: options2?.background === true ? jsonHeaders : undefined,
1228
+ method: "POST"
1229
+ });
1230
+ if (!response.ok) {
1231
+ return {
1232
+ error: await toErrorMessage(response),
1233
+ ok: false
1234
+ };
1235
+ }
1236
+ return parseJson(response);
1237
+ },
1238
+ async reindexDocument(id) {
1239
+ const response = await fetchImpl(`${basePath}/reindex/documents/${encodeURIComponent(id)}`, {
1240
+ method: "POST"
1241
+ });
1242
+ if (!response.ok) {
1243
+ return {
1244
+ error: await toErrorMessage(response),
1245
+ ok: false
1246
+ };
1247
+ }
1248
+ return parseJson(response);
1249
+ },
1250
+ async reindexSource(source) {
1251
+ const response = await fetchImpl(`${basePath}/reindex/source`, {
1252
+ body: JSON.stringify({ source }),
1253
+ headers: jsonHeaders,
1254
+ method: "POST"
1255
+ });
1256
+ if (!response.ok) {
1257
+ return {
1258
+ error: await toErrorMessage(response),
1259
+ ok: false
1260
+ };
1261
+ }
1262
+ return parseJson(response);
1263
+ },
1264
+ async reseed() {
1265
+ const response = await fetchImpl(`${basePath}/reseed`, {
1266
+ method: "POST"
1267
+ });
1268
+ if (!response.ok) {
1269
+ return {
1270
+ error: await toErrorMessage(response),
1271
+ ok: false
1272
+ };
1273
+ }
1274
+ return parseJson(response);
1275
+ },
1276
+ async reset() {
1277
+ const response = await fetchImpl(`${basePath}/reset`, {
1278
+ method: "POST"
1279
+ });
1280
+ if (!response.ok) {
1281
+ return {
1282
+ error: await toErrorMessage(response),
1283
+ ok: false
1284
+ };
1285
+ }
1286
+ return parseJson(response);
1287
+ },
1288
+ async rebuildNativeIndex() {
1289
+ const response = await fetchImpl(`${basePath}/backend/reindex-native`, {
1290
+ method: "POST"
1291
+ });
1292
+ if (!response.ok) {
1293
+ return {
1294
+ error: await toErrorMessage(response),
1295
+ ok: false
1296
+ };
1297
+ }
1298
+ return parseJson(response);
1299
+ },
1300
+ search,
1301
+ async searchWithTrace(input) {
1302
+ return search({ ...input, includeTrace: true });
1303
+ },
1304
+ async searchDetailed(input) {
1305
+ return search({ ...input, includeTrace: true });
1306
+ },
1307
+ async status() {
1308
+ const response = await fetchImpl(`${basePath}/status`);
1309
+ if (!response.ok) {
1310
+ throw new Error(await toErrorMessage(response));
1311
+ }
1312
+ return parseJson(response);
1313
+ },
1314
+ async statusMaintenance() {
1315
+ const response = await fetchImpl(`${basePath}/status/maintenance`);
1316
+ if (!response.ok) {
1317
+ throw new Error(await toErrorMessage(response));
1318
+ }
1319
+ return parseJson(response);
1320
+ },
1321
+ async retrievalReleaseStatus() {
1322
+ const response = await fetchImpl(`${basePath}/status/release`);
1323
+ if (!response.ok) {
1324
+ throw new Error(await toErrorMessage(response));
1325
+ }
1326
+ const payload = await parseJson(response);
1327
+ return payload.retrievalComparisons;
1328
+ },
1329
+ async retrievalReleaseDriftStatus() {
1330
+ const response = await fetchImpl(`${basePath}/status/release/drift`);
1331
+ if (!response.ok) {
1332
+ throw new Error(await toErrorMessage(response));
1333
+ }
1334
+ return parseJson(response);
1335
+ },
1336
+ async retrievalLaneHandoffStatus() {
1337
+ const response = await fetchImpl(`${basePath}/status/handoffs`);
1338
+ if (!response.ok) {
1339
+ throw new Error(await toErrorMessage(response));
1340
+ }
1341
+ return parseJson(response);
1342
+ }
1343
+ };
1344
+ };
1345
+
1346
+ // src/ai/rag/grounding.ts
1347
+ var getContextString = (value) => typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
1348
+ var getContextNumber = (value) => typeof value === "number" && Number.isFinite(value) ? value : undefined;
1349
+ var getAttachmentName = (source, title) => {
1350
+ const sourceAttachment = source?.split("/").at(-1);
1351
+ if (sourceAttachment && sourceAttachment.includes(".")) {
1352
+ return sourceAttachment;
1353
+ }
1354
+ const titleAttachment = title?.split(" · ").at(-1);
1355
+ if (titleAttachment && titleAttachment.includes(".")) {
1356
+ return titleAttachment;
1357
+ }
1358
+ return;
1359
+ };
1360
+ var buildContextLabel = (metadata) => {
1361
+ if (!metadata) {
1362
+ return;
1363
+ }
1364
+ const emailKind = getContextString(metadata.emailKind);
1365
+ const officeBlockKindValue = getContextString(metadata.officeBlockKind);
1366
+ const officeBlockKind = officeBlockKindValue === "table" || officeBlockKindValue === "list" || officeBlockKindValue === "paragraph" ? officeBlockKindValue : undefined;
1367
+ if (emailKind === "attachment") {
1368
+ return "Attachment evidence";
1369
+ }
1370
+ if (emailKind === "message") {
1371
+ const from = getContextString(metadata.from);
1372
+ return from ? `Message from ${from}` : "Message evidence";
1373
+ }
1374
+ const page = getContextNumber(metadata.page) ?? getContextNumber(metadata.pageNumber) ?? (typeof metadata.pageIndex === "number" ? metadata.pageIndex + 1 : undefined);
1375
+ const region = getContextNumber(metadata.regionNumber) ?? (typeof metadata.regionIndex === "number" ? metadata.regionIndex + 1 : undefined);
1376
+ if (page && region) {
1377
+ return `Page ${page} region ${region}`;
1378
+ }
1379
+ if (page) {
1380
+ return `Page ${page}`;
1381
+ }
1382
+ const sheet = getContextString(metadata.sheetName) ?? (Array.isArray(metadata.sheetNames) ? getContextString(metadata.sheetNames[0]) : undefined);
1383
+ if (sheet) {
1384
+ return `Sheet ${sheet}`;
1385
+ }
1386
+ const slide = getContextNumber(metadata.slide) ?? getContextNumber(metadata.slideNumber) ?? (typeof metadata.slideIndex === "number" ? metadata.slideIndex + 1 : undefined);
1387
+ const slideTitle = getContextString(metadata.slideTitle);
1388
+ if (slide) {
1389
+ return slideTitle ? `Slide ${slide} · ${slideTitle}` : `Slide ${slide}`;
1390
+ }
1391
+ const archiveEntry = getContextString(metadata.archiveEntryPath) ?? getContextString(metadata.entryPath);
1392
+ if (archiveEntry) {
1393
+ return `Archive entry ${archiveEntry}`;
1394
+ }
1395
+ const threadTopic = getContextString(metadata.threadTopic);
1396
+ if (threadTopic) {
1397
+ return `Thread ${threadTopic}`;
1398
+ }
1399
+ const speaker = getContextString(metadata.speaker);
1400
+ if (speaker) {
1401
+ return `Speaker ${speaker}`;
1402
+ }
1403
+ const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.map((value) => getContextString(value)).filter((value) => typeof value === "string") : [];
1404
+ const sectionTitle = getContextString(metadata.sectionTitle) ?? sectionPath.at(-1);
1405
+ const officeSectionLabel = sectionPath.length > 0 ? sectionPath.join(" > ") : sectionTitle;
1406
+ if (officeBlockKind === "table" && officeSectionLabel) {
1407
+ return `Office table block ${officeSectionLabel}`;
1408
+ }
1409
+ if (officeBlockKind === "list" && officeSectionLabel) {
1410
+ return `Office list block ${officeSectionLabel}`;
1411
+ }
1412
+ if (officeBlockKind === "paragraph" && officeSectionLabel) {
1413
+ return `Office paragraph block ${officeSectionLabel}`;
1414
+ }
1415
+ if (sectionTitle) {
1416
+ return `Section ${sectionTitle}`;
1417
+ }
1418
+ return;
1419
+ };
1420
+ var formatMediaTimestamp = (value) => {
1421
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
1422
+ return;
1423
+ }
1424
+ const totalSeconds = Math.floor(value / 1000);
1425
+ const minutes = Math.floor(totalSeconds / 60);
1426
+ const seconds = totalSeconds % 60;
1427
+ const milliseconds = Math.floor(value % 1000);
1428
+ return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}.${String(milliseconds).padStart(3, "0")}`;
1429
+ };
1430
+ var formatMediaDurationLabel = (value) => {
1431
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
1432
+ return;
1433
+ }
1434
+ return formatMediaTimestamp(value);
1435
+ };
1436
+ var formatOfficeListLevelsLabel = (value) => {
1437
+ if (!Array.isArray(value) || value.length === 0) {
1438
+ return;
1439
+ }
1440
+ const levels = value.map((entry) => getContextNumber(entry)).filter((entry) => typeof entry === "number").sort((left, right) => left - right);
1441
+ if (levels.length === 0) {
1442
+ return;
1443
+ }
1444
+ const minLevel = levels[0];
1445
+ const maxLevel = levels[levels.length - 1];
1446
+ return minLevel === maxLevel ? `Office list level ${minLevel}` : `Office list levels ${minLevel}-${maxLevel}`;
1447
+ };
1448
+ var getOfficeTableCitationScope = (metadata) => {
1449
+ if (!metadata) {
1450
+ return;
1451
+ }
1452
+ const officeBlockKindValue = getContextString(metadata.officeBlockKind);
1453
+ const officeBlockKind = officeBlockKindValue === "table" || officeBlockKindValue === "list" || officeBlockKindValue === "paragraph" ? officeBlockKindValue : undefined;
1454
+ if (officeBlockKind !== "table" && officeBlockKind !== "list" && officeBlockKind !== "paragraph") {
1455
+ return;
1456
+ }
1457
+ const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.map((value) => getContextString(value)).filter((value) => typeof value === "string") : [];
1458
+ const sectionTitle = getContextString(metadata.sectionTitle) ?? sectionPath.at(-1);
1459
+ const officeContextText = officeBlockKind === "table" ? getContextString(metadata.officeTableContextText) : officeBlockKind === "list" ? getContextString(metadata.officeListContextText) : undefined;
1460
+ if (!sectionTitle) {
1461
+ return;
1462
+ }
1463
+ return {
1464
+ blockKind: officeBlockKind,
1465
+ familyPath: (() => {
1466
+ const explicitGenericFamilyPath = Array.isArray(metadata.sectionFamilyPath) ? metadata.sectionFamilyPath.map((value) => getContextString(value)).filter((value) => typeof value === "string") : [];
1467
+ const explicitGenericOrdinalPath = Array.isArray(metadata.sectionOrdinalPath) ? metadata.sectionOrdinalPath.map((value) => getContextNumber(value)).filter((value) => typeof value === "number") : [];
1468
+ if (explicitGenericFamilyPath.length > 0 && explicitGenericFamilyPath.length === explicitGenericOrdinalPath.length) {
1469
+ return explicitGenericFamilyPath;
1470
+ }
1471
+ const explicitOfficeFamilyPath = Array.isArray(metadata.officeFamilyPath) ? metadata.officeFamilyPath.map((value) => getContextString(value)).filter((value) => typeof value === "string") : [];
1472
+ return explicitOfficeFamilyPath.length > 0 ? explicitOfficeFamilyPath : sectionPath.map((value) => value.replace(/\s+\((\d+)\)$/, "").trim());
1473
+ })(),
1474
+ pathDepth: sectionPath.length,
1475
+ ordinalPath: (() => {
1476
+ const explicitGenericFamilyPath = Array.isArray(metadata.sectionFamilyPath) ? metadata.sectionFamilyPath.map((value) => getContextString(value)).filter((value) => typeof value === "string") : [];
1477
+ const explicitGenericOrdinalPath = Array.isArray(metadata.sectionOrdinalPath) ? metadata.sectionOrdinalPath.map((value) => getContextNumber(value)).filter((value) => typeof value === "number") : [];
1478
+ if (explicitGenericFamilyPath.length > 0 && explicitGenericFamilyPath.length === explicitGenericOrdinalPath.length) {
1479
+ return explicitGenericOrdinalPath;
1480
+ }
1481
+ const explicitOfficeOrdinalPath = Array.isArray(metadata.officeOrdinalPath) ? metadata.officeOrdinalPath.map((value) => typeof value === "number" && Number.isFinite(value) ? value : undefined).filter((value) => typeof value === "number") : [];
1482
+ return explicitOfficeOrdinalPath.length > 0 ? explicitOfficeOrdinalPath : sectionPath.map((value) => {
1483
+ const match = value.match(/\((\d+)\)$/);
1484
+ return match ? Number.parseInt(match[1] ?? "1", 10) : 1;
1485
+ });
1486
+ })(),
1487
+ sectionFamilyKey: getContextString(metadata.sectionSiblingFamilyKey) ?? getContextString(metadata.officeSiblingFamilyKey) ?? sectionPath.at(-1)?.replace(/\s+\((\d+)\)$/, "").trim() ?? sectionTitle,
1488
+ sectionOrdinal: getContextNumber(metadata.sectionSiblingOrdinal) ?? getContextNumber(metadata.officeSiblingOrdinal) ?? (() => {
1489
+ const match = sectionTitle.match(/\((\d+)\)$/);
1490
+ return match ? Number.parseInt(match[1] ?? "1", 10) : 1;
1491
+ })(),
1492
+ sectionTitle,
1493
+ hasContext: typeof officeContextText === "string"
1494
+ };
1495
+ };
1496
+ var areOfficeCitationScopesComparable = (left, right) => {
1497
+ if (!left || !right) {
1498
+ return false;
1499
+ }
1500
+ if (left.blockKind !== right.blockKind || left.sectionFamilyKey !== right.sectionFamilyKey || left.sectionOrdinal !== right.sectionOrdinal) {
1501
+ return false;
1502
+ }
1503
+ const leftAncestorFamilyPath = left.familyPath.slice(0, -1);
1504
+ const rightAncestorFamilyPath = right.familyPath.slice(0, -1);
1505
+ const leftAncestorOrdinalPath = left.ordinalPath.slice(0, -1);
1506
+ const rightAncestorOrdinalPath = right.ordinalPath.slice(0, -1);
1507
+ const sharedDepth = Math.min(leftAncestorFamilyPath.length, rightAncestorFamilyPath.length);
1508
+ for (let index = 0;index < sharedDepth; index += 1) {
1509
+ if (leftAncestorFamilyPath[index] !== rightAncestorFamilyPath[index] || leftAncestorOrdinalPath[index] !== rightAncestorOrdinalPath[index]) {
1510
+ return false;
1511
+ }
1512
+ }
1513
+ return true;
1514
+ };
1515
+ var getGenericStructuredCitationScope = (metadata) => {
1516
+ if (!metadata || metadata.officeBlockKind || metadata.pageNumber) {
1517
+ return;
1518
+ }
1519
+ const kind = metadata.sectionKind === "spreadsheet_rows" || metadata.sectionKind === "presentation_slide" ? metadata.sectionKind : undefined;
1520
+ if (!kind) {
1521
+ return;
1522
+ }
1523
+ const explicitFamilyPath = Array.isArray(metadata.sectionFamilyPath) ? metadata.sectionFamilyPath.map((value) => getContextString(value)).filter((value) => typeof value === "string") : [];
1524
+ const explicitOrdinalPath = Array.isArray(metadata.sectionOrdinalPath) ? metadata.sectionOrdinalPath.map((value) => getContextNumber(value)).filter((value) => typeof value === "number") : [];
1525
+ let familyPath = explicitFamilyPath.length > 0 && explicitFamilyPath.length === explicitOrdinalPath.length ? explicitFamilyPath : [];
1526
+ let ordinalPath = explicitFamilyPath.length > 0 && explicitFamilyPath.length === explicitOrdinalPath.length ? explicitOrdinalPath : [];
1527
+ if (familyPath.length === 0) {
1528
+ if (kind === "spreadsheet_rows") {
1529
+ const sheetName = getContextString(metadata.sheetName) ?? "Sheet";
1530
+ const tableIndex = getContextNumber(metadata.spreadsheetTableIndex) ?? 1;
1531
+ familyPath = [sheetName, "Spreadsheet Table"];
1532
+ ordinalPath = [1, tableIndex];
1533
+ } else {
1534
+ const slideFamily = getContextString(metadata.slideTitle) ?? "Slide";
1535
+ const slideOrdinal = getContextNumber(metadata.slideNumber) ?? (typeof metadata.slideIndex === "number" ? metadata.slideIndex + 1 : 1);
1536
+ familyPath = [slideFamily];
1537
+ ordinalPath = [slideOrdinal];
1538
+ }
1539
+ }
1540
+ const sectionFamilyKey = getContextString(metadata.sectionSiblingFamilyKey) ?? familyPath.at(-1);
1541
+ const sectionOrdinal = getContextNumber(metadata.sectionSiblingOrdinal) ?? ordinalPath.at(-1);
1542
+ if (!sectionFamilyKey || typeof sectionOrdinal !== "number") {
1543
+ return;
1544
+ }
1545
+ return {
1546
+ familyPath,
1547
+ kind,
1548
+ ordinalPath,
1549
+ pathDepth: familyPath.length,
1550
+ sectionFamilyKey,
1551
+ sectionOrdinal
1552
+ };
1553
+ };
1554
+ var areGenericStructuredCitationScopesComparable = (left, right) => {
1555
+ if (!left || !right) {
1556
+ return false;
1557
+ }
1558
+ if (left.kind !== right.kind || left.sectionFamilyKey !== right.sectionFamilyKey || left.sectionOrdinal !== right.sectionOrdinal) {
1559
+ return false;
1560
+ }
1561
+ const leftAncestorFamilyPath = left.familyPath.slice(0, -1);
1562
+ const rightAncestorFamilyPath = right.familyPath.slice(0, -1);
1563
+ const leftAncestorOrdinalPath = left.ordinalPath.slice(0, -1);
1564
+ const rightAncestorOrdinalPath = right.ordinalPath.slice(0, -1);
1565
+ const sharedDepth = Math.min(leftAncestorFamilyPath.length, rightAncestorFamilyPath.length);
1566
+ for (let index = 0;index < sharedDepth; index += 1) {
1567
+ if (leftAncestorFamilyPath[index] !== rightAncestorFamilyPath[index] || leftAncestorOrdinalPath[index] !== rightAncestorOrdinalPath[index]) {
1568
+ return false;
1569
+ }
1570
+ }
1571
+ return true;
1572
+ };
1573
+ var getOfficeTableCitationPreference = (metadata) => {
1574
+ const scope = getOfficeTableCitationScope(metadata);
1575
+ if (!scope) {
1576
+ return 0;
1577
+ }
1578
+ return scope.pathDepth * 10 + (scope.hasContext ? 1 : 0) + (scope.blockKind === "list" && typeof metadata?.officeListGroupItemCount === "number" && metadata.officeListGroupItemCount > 1 ? 1 : 0);
1579
+ };
1580
+ var getGenericStructuredCitationPreference = (metadata) => {
1581
+ const scope = getGenericStructuredCitationScope(metadata);
1582
+ if (!scope) {
1583
+ return 0;
1584
+ }
1585
+ return scope.pathDepth * 10 + (scope.kind === "spreadsheet_rows" && typeof metadata?.spreadsheetTableIndex === "number" ? 2 : 0) + (Array.isArray(metadata?.spreadsheetHeaders) && metadata.spreadsheetHeaders.length > 0 ? 1 : 0) + (typeof metadata?.slideNotesText === "string" && metadata.slideNotesText.trim().length > 0 ? 1 : 0);
1586
+ };
1587
+ var buildLocatorLabel = (metadata, source, title) => {
1588
+ if (!metadata) {
1589
+ return;
1590
+ }
1591
+ const page = getContextNumber(metadata.page) ?? getContextNumber(metadata.pageNumber) ?? (typeof metadata.pageIndex === "number" ? metadata.pageIndex + 1 : undefined);
1592
+ const region = getContextNumber(metadata.regionNumber) ?? (typeof metadata.regionIndex === "number" ? metadata.regionIndex + 1 : undefined);
1593
+ if (page && region) {
1594
+ return `Page ${page} · Region ${region}`;
1595
+ }
1596
+ if (page) {
1597
+ return `Page ${page}`;
1598
+ }
1599
+ const sheet = getContextString(metadata.sheetName) ?? (Array.isArray(metadata.sheetNames) ? getContextString(metadata.sheetNames[0]) : undefined);
1600
+ if (sheet) {
1601
+ return `Sheet ${sheet}`;
1602
+ }
1603
+ const slide = getContextNumber(metadata.slide) ?? getContextNumber(metadata.slideNumber) ?? (typeof metadata.slideIndex === "number" ? metadata.slideIndex + 1 : undefined);
1604
+ const slideTitle = getContextString(metadata.slideTitle);
1605
+ if (slide) {
1606
+ return slideTitle ? `Slide ${slide} · ${slideTitle}` : `Slide ${slide}`;
1607
+ }
1608
+ const archiveEntry = getContextString(metadata.archiveEntryPath) ?? getContextString(metadata.entryPath);
1609
+ if (archiveEntry) {
1610
+ return `Archive entry ${archiveEntry}`;
1611
+ }
1612
+ const emailKind = getContextString(metadata.emailKind);
1613
+ const officeBlockKind = getContextString(metadata.officeBlockKind);
1614
+ const officeBlockNumber = getContextNumber(metadata.officeBlockNumber);
1615
+ const officeTableBodyRowStart = getContextNumber(metadata.officeTableBodyRowStart);
1616
+ const officeTableBodyRowEnd = getContextNumber(metadata.officeTableBodyRowEnd);
1617
+ if (emailKind === "attachment") {
1618
+ const attachmentName = getContextString(metadata.attachmentName) ?? getAttachmentName(source, title);
1619
+ return attachmentName ? `Attachment ${attachmentName}` : "Attachment";
1620
+ }
1621
+ const mediaStart = formatMediaTimestamp(metadata.startMs);
1622
+ const mediaEnd = formatMediaTimestamp(metadata.endMs);
1623
+ if (mediaStart && mediaEnd) {
1624
+ return `Timestamp ${mediaStart} - ${mediaEnd}`;
1625
+ }
1626
+ if (mediaStart) {
1627
+ return `Timestamp ${mediaStart}`;
1628
+ }
1629
+ if (officeBlockNumber && officeBlockKind === "table") {
1630
+ if (typeof officeTableBodyRowStart === "number" && typeof officeTableBodyRowEnd === "number") {
1631
+ return officeTableBodyRowStart === officeTableBodyRowEnd ? `Office table block ${officeBlockNumber} · Row ${officeTableBodyRowStart}` : `Office table block ${officeBlockNumber} · Rows ${officeTableBodyRowStart}-${officeTableBodyRowEnd}`;
1632
+ }
1633
+ return `Office table block ${officeBlockNumber}`;
1634
+ }
1635
+ if (officeBlockNumber && officeBlockKind === "list") {
1636
+ return `Office list block ${officeBlockNumber}`;
1637
+ }
1638
+ if (officeBlockNumber && officeBlockKind === "paragraph") {
1639
+ return `Office paragraph block ${officeBlockNumber}`;
1640
+ }
1641
+ const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.map((value) => getContextString(value)).filter((value) => typeof value === "string") : [];
1642
+ if (sectionPath.length > 0) {
1643
+ return `Section ${sectionPath.join(" > ")}`;
1644
+ }
1645
+ return;
1646
+ };
1647
+ var formatTimestampLabel = (value) => {
1648
+ const timestamp = typeof value === "number" && Number.isFinite(value) ? value : typeof value === "string" ? Date.parse(value) : Number.NaN;
1649
+ if (!Number.isFinite(timestamp)) {
1650
+ return;
1651
+ }
1652
+ return new Date(timestamp).toLocaleString("en-US", {
1653
+ dateStyle: "medium",
1654
+ timeStyle: "short"
1655
+ });
1656
+ };
1657
+ var buildProvenanceLabel = (metadata) => {
1658
+ if (!metadata) {
1659
+ return;
1660
+ }
1661
+ const threadTopic = getContextString(metadata.threadTopic);
1662
+ const from = getContextString(metadata.from);
1663
+ const sentAt = formatTimestampLabel(metadata.sentAt) ?? formatTimestampLabel(metadata.receivedAt);
1664
+ const speaker = getContextString(metadata.speaker);
1665
+ const mediaKind = getContextString(metadata.mediaKind);
1666
+ const mediaSegmentCount = getContextNumber(metadata.mediaSegmentCount);
1667
+ const mediaSegmentGroupSize = getContextNumber(metadata.mediaSegmentGroupSize);
1668
+ const mediaSegmentGroupIndex = getContextNumber(metadata.mediaSegmentGroupIndex);
1669
+ const mediaChannel = getContextString(metadata.mediaChannel);
1670
+ const mediaSpeakerCount = getContextNumber(metadata.mediaSpeakerCount);
1671
+ const mediaDurationLabel = formatMediaDurationLabel(metadata.mediaDurationMs);
1672
+ const transcriptSource = getContextString(metadata.transcriptSource);
1673
+ const pdfTextMode = getContextString(metadata.pdfTextMode);
1674
+ const officeBlockKind = getContextString(metadata.officeBlockKind);
1675
+ const officeListContextText = getContextString(metadata.officeListContextText);
1676
+ const officeListGroupItemCount = getContextNumber(metadata.officeListGroupItemCount);
1677
+ const officeListLevelsLabel = formatOfficeListLevelsLabel(metadata.officeListLevels);
1678
+ const officeTableHeaders = Array.isArray(metadata.officeTableHeaders) ? metadata.officeTableHeaders.map((value) => getContextString(value)).filter((value) => typeof value === "string") : [];
1679
+ const officeTableColumnCount = getContextNumber(metadata.officeTableColumnCount);
1680
+ const officeTableBodyRowCount = getContextNumber(metadata.officeTableBodyRowCount);
1681
+ const officeTableBodyRowStart = getContextNumber(metadata.officeTableBodyRowStart);
1682
+ const officeTableBodyRowEnd = getContextNumber(metadata.officeTableBodyRowEnd);
1683
+ const officeTableContextText = getContextString(metadata.officeTableContextText);
1684
+ const officeTableFollowUpText = getContextString(metadata.officeTableFollowUpText);
1685
+ const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.map((value) => getContextString(value)).filter((value) => typeof value === "string") : [];
1686
+ const ocrEngine = getContextString(metadata.ocrEngine);
1687
+ const ocrConfidence = getContextNumber(metadata.ocrRegionConfidence) ?? getContextNumber(metadata.ocrConfidence);
1688
+ const labels = [
1689
+ pdfTextMode ? `PDF ${pdfTextMode}` : "",
1690
+ officeBlockKind ? `Office ${officeBlockKind}` : "",
1691
+ typeof officeListGroupItemCount === "number" ? `Office list ${officeListGroupItemCount} items` : "",
1692
+ officeListLevelsLabel ?? "",
1693
+ sectionPath.length > 0 && officeBlockKind ? `Source-aware office ${officeBlockKind} block ${sectionPath.join(" > ")}` : "",
1694
+ officeListContextText ? `Office list context ${officeListContextText}` : "",
1695
+ officeTableHeaders.length > 0 ? `Office table ${officeTableHeaders.join(", ")}` : "",
1696
+ typeof officeTableColumnCount === "number" ? `Office table ${officeTableColumnCount} cols` : "",
1697
+ typeof officeTableBodyRowCount === "number" ? `Office table ${officeTableBodyRowCount} body rows` : "",
1698
+ typeof officeTableBodyRowStart === "number" && typeof officeTableBodyRowEnd === "number" ? officeTableBodyRowStart === officeTableBodyRowEnd ? `Office table row ${officeTableBodyRowStart}` : `Office table rows ${officeTableBodyRowStart}-${officeTableBodyRowEnd}` : "",
1699
+ officeTableContextText ? `Office table context ${officeTableContextText}` : "",
1700
+ officeTableFollowUpText ? `Office table follow-up ${officeTableFollowUpText}` : "",
1701
+ ocrEngine ? `OCR ${ocrEngine}` : "",
1702
+ typeof ocrConfidence === "number" ? `Confidence ${ocrConfidence.toFixed(2)}` : "",
1703
+ mediaKind ? `Media ${mediaKind}` : "",
1704
+ mediaSegmentCount ? `${mediaSegmentCount} segments` : "",
1705
+ mediaSegmentGroupSize ? `${mediaSegmentGroupSize} grouped segments` : "",
1706
+ mediaSegmentGroupIndex !== undefined ? `Segment group ${mediaSegmentGroupIndex + 1}` : "",
1707
+ mediaChannel ? `Channel ${mediaChannel}` : "",
1708
+ mediaSpeakerCount ? `${mediaSpeakerCount} speakers` : "",
1709
+ mediaDurationLabel ? `Duration ${mediaDurationLabel}` : "",
1710
+ transcriptSource ? `Transcript ${transcriptSource}` : "",
1711
+ threadTopic ? `Thread ${threadTopic}` : "",
1712
+ speaker ? `Speaker ${speaker}` : "",
1713
+ from ? `Sender ${from}` : "",
1714
+ sentAt ? `Sent ${sentAt}` : ""
1715
+ ].filter((value) => value.length > 0);
1716
+ return labels.length > 0 ? labels.join(" · ") : undefined;
1717
+ };
1718
+ var buildSourceLabel = (source) => source.source ?? source.title ?? source.chunkId;
1719
+ var buildExcerpt = (text, maxLength = 160) => {
1720
+ const normalized = text.replaceAll(/\s+/g, " ").trim();
1721
+ if (normalized.length <= maxLength) {
1722
+ return normalized;
1723
+ }
1724
+ return `${normalized.slice(0, Math.max(0, maxLength - 1)).trimEnd()}…`;
1725
+ };
1726
+ var selectPreferredExcerpt = (excerpts, sectionChunkCount) => {
1727
+ if (!excerpts) {
1728
+ return {
1729
+ excerpt: "",
1730
+ mode: "chunk",
1731
+ reason: "single_chunk"
1732
+ };
1733
+ }
1734
+ const chunkExcerpt = excerpts.chunkExcerpt?.trim() ?? "";
1735
+ const windowExcerpt = excerpts.windowExcerpt?.trim() ?? "";
1736
+ const sectionExcerpt = excerpts.sectionExcerpt?.trim() ?? "";
1737
+ if (sectionChunkCount && sectionChunkCount > 1 && chunkExcerpt.length > 0 && chunkExcerpt.length < 72) {
1738
+ if (sectionChunkCount <= 3 && sectionExcerpt) {
1739
+ return {
1740
+ excerpt: sectionExcerpt,
1741
+ mode: "section",
1742
+ reason: "section_small_enough"
1743
+ };
1744
+ }
1745
+ if (windowExcerpt) {
1746
+ return {
1747
+ excerpt: windowExcerpt,
1748
+ mode: "window",
1749
+ reason: "section_too_large_use_window"
1750
+ };
1751
+ }
1752
+ return {
1753
+ excerpt: chunkExcerpt,
1754
+ mode: "chunk",
1755
+ reason: "chunk_too_narrow"
1756
+ };
1757
+ }
1758
+ return {
1759
+ excerpt: chunkExcerpt || windowExcerpt || sectionExcerpt,
1760
+ mode: "chunk",
1761
+ reason: (sectionChunkCount ?? 0) > 1 ? "chunk_too_narrow" : "single_chunk"
1762
+ };
1763
+ };
1764
+ var buildExcerptModeCounts = (references) => references.reduce((counts, reference) => {
1765
+ if (reference?.excerptSelection) {
1766
+ counts[reference.excerptSelection.mode] += 1;
1767
+ }
1768
+ return counts;
1769
+ }, { chunk: 0, section: 0, window: 0 });
1770
+ var buildGroundingChunkExcerpts = (sources, activeChunkId) => {
1771
+ if (sources.length === 0) {
1772
+ return;
1773
+ }
1774
+ const activeSource = (activeChunkId ? sources.find((source) => source.chunkId === activeChunkId) : undefined) ?? sources[0];
1775
+ if (!activeSource) {
1776
+ return;
1777
+ }
1778
+ const chunkMap = new Map(sources.map((source) => [source.chunkId, source]));
1779
+ const activeMetadata = activeSource.metadata ?? {};
1780
+ const previousChunkId = getContextString(activeMetadata.previousChunkId);
1781
+ const nextChunkId = getContextString(activeMetadata.nextChunkId);
1782
+ const sectionChunkId = getContextString(activeMetadata.sectionChunkId);
1783
+ const sectionSources = sectionChunkId ? sources.filter((source) => getContextString(source.metadata?.sectionChunkId) === sectionChunkId).sort((left, right) => {
1784
+ const leftIndex = getContextNumber(left.metadata?.sectionChunkIndex) ?? Number.MAX_SAFE_INTEGER;
1785
+ const rightIndex = getContextNumber(right.metadata?.sectionChunkIndex) ?? Number.MAX_SAFE_INTEGER;
1786
+ if (leftIndex !== rightIndex) {
1787
+ return leftIndex - rightIndex;
1788
+ }
1789
+ return left.chunkId.localeCompare(right.chunkId);
1790
+ }) : [activeSource];
1791
+ const collectText = (chunkIds) => chunkIds.map((chunkId) => chunkMap.get(chunkId)?.text).filter((text) => typeof text === "string").join(`
1792
+
1793
+ `);
1794
+ const orderedWindowIds = [
1795
+ previousChunkId,
1796
+ activeSource.chunkId,
1797
+ nextChunkId
1798
+ ].filter((chunkId, index, values) => Boolean(chunkId) && values.indexOf(chunkId) === index);
1799
+ return {
1800
+ chunkExcerpt: buildExcerpt(activeSource.text, 160),
1801
+ sectionExcerpt: buildExcerpt(sectionSources.map((source) => source.text).join(`
1802
+
1803
+ `), 320),
1804
+ windowExcerpt: buildExcerpt(collectText(orderedWindowIds), 240)
1805
+ };
1806
+ };
1807
+ 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(" · ");
1808
+ var buildGroundingReferenceEvidenceSummary = (reference) => [
1809
+ reference.source ?? reference.title ?? reference.chunkId,
1810
+ reference.locatorLabel,
1811
+ reference.contextLabel,
1812
+ reference.provenanceLabel
1813
+ ].filter((value) => Boolean(value && value.length > 0)).filter((value, index, values) => values.findIndex((entry) => entry === value) === index).join(" · ");
1814
+ var buildGroundingSectionKey = (reference) => reference.contextLabel ?? reference.locatorLabel ?? reference.label ?? reference.source ?? reference.chunkId;
1815
+ var buildGroundingSectionSummaryLine = (reference) => [
1816
+ reference.source ?? reference.title ?? reference.chunkId,
1817
+ reference.locatorLabel,
1818
+ reference.contextLabel,
1819
+ reference.provenanceLabel
1820
+ ].filter((value) => Boolean(value && value.length > 0)).filter((value, index, values) => values.findIndex((entry) => entry === value) === index).join(" · ");
1821
+ var buildGroundedAnswerCitationDetail = (reference) => ({
1822
+ contextLabel: reference.contextLabel,
1823
+ evidenceLabel: buildGroundingReferenceEvidenceLabel(reference),
1824
+ evidenceSummary: buildGroundingReferenceEvidenceSummary(reference),
1825
+ excerpt: selectPreferredExcerpt(reference.excerpts, getContextNumber(reference.metadata?.sectionChunkCount)).excerpt || reference.excerpt,
1826
+ excerpts: reference.excerpts,
1827
+ excerptSelection: reference.excerptSelection,
1828
+ label: reference.label,
1829
+ locatorLabel: reference.locatorLabel,
1830
+ number: reference.number,
1831
+ provenanceLabel: reference.provenanceLabel,
1832
+ source: reference.source,
1833
+ title: reference.title
1834
+ });
1835
+ var buildRAGCitationReferenceMap = (citations) => Object.fromEntries(citations.map((citation, index) => [citation.chunkId, index + 1]));
1836
+ var buildRAGCitations = (sources) => {
1837
+ const unique = new Map;
1838
+ for (const source of sources) {
1839
+ const key = source.chunkId;
1840
+ const existing = unique.get(key);
1841
+ const hasBetterExisting = existing !== undefined && existing.score >= source.score;
1842
+ if (hasBetterExisting)
1843
+ continue;
1844
+ const excerpts = buildGroundingChunkExcerpts(sources, source.chunkId);
1845
+ const excerptSelection = selectPreferredExcerpt(excerpts, getContextNumber(source.metadata?.sectionChunkCount));
1846
+ unique.set(key, {
1847
+ chunkId: source.chunkId,
1848
+ contextLabel: source.labels?.contextLabel ?? buildContextLabel(source.metadata),
1849
+ excerpt: excerptSelection.excerpt || buildExcerpt(source.text),
1850
+ excerpts,
1851
+ excerptSelection,
1852
+ key,
1853
+ label: buildSourceLabel(source),
1854
+ locatorLabel: source.labels?.locatorLabel ?? buildLocatorLabel(source.metadata, source.source, source.title),
1855
+ metadata: source.metadata,
1856
+ provenanceLabel: source.labels?.provenanceLabel ?? buildProvenanceLabel(source.metadata),
1857
+ score: source.score,
1858
+ source: source.source,
1859
+ text: source.text,
1860
+ title: source.title
1861
+ });
1862
+ }
1863
+ return [...unique.values()].sort((left, right) => {
1864
+ const leftOfficeScope = getOfficeTableCitationScope(left.metadata);
1865
+ const rightOfficeScope = getOfficeTableCitationScope(right.metadata);
1866
+ if (left.source === right.source && areOfficeCitationScopesComparable(leftOfficeScope, rightOfficeScope)) {
1867
+ const leftOfficePreference = getOfficeTableCitationPreference(left.metadata);
1868
+ const rightOfficePreference = getOfficeTableCitationPreference(right.metadata);
1869
+ if (rightOfficePreference !== leftOfficePreference) {
1870
+ return rightOfficePreference - leftOfficePreference;
1871
+ }
1872
+ }
1873
+ const leftGenericScope = getGenericStructuredCitationScope(left.metadata);
1874
+ const rightGenericScope = getGenericStructuredCitationScope(right.metadata);
1875
+ if (left.source === right.source && areGenericStructuredCitationScopesComparable(leftGenericScope, rightGenericScope)) {
1876
+ const leftGenericPreference = getGenericStructuredCitationPreference(left.metadata);
1877
+ const rightGenericPreference = getGenericStructuredCitationPreference(right.metadata);
1878
+ if (rightGenericPreference !== leftGenericPreference) {
1879
+ return rightGenericPreference - leftGenericPreference;
1880
+ }
1881
+ }
1882
+ if (right.score !== left.score) {
1883
+ return right.score - left.score;
1884
+ }
1885
+ return left.label.localeCompare(right.label);
1886
+ });
1887
+ };
1888
+ var buildRAGGroundedAnswer = (content, sources) => {
1889
+ const references = buildRAGGroundingReferences(sources);
1890
+ const sectionSummaries = buildRAGGroundedAnswerSectionSummaries(references);
1891
+ const referenceMap = new Map(references.map((reference) => [reference.number, reference]));
1892
+ const parts = [];
1893
+ const ungroundedReferenceNumbers = new Set;
1894
+ const citationPattern = /\[(\d+(?:\s*,\s*\d+)*)\]/g;
1895
+ let cursor = 0;
1896
+ for (const match of content.matchAll(citationPattern)) {
1897
+ const raw = match[0];
1898
+ const numbers = (match[1] ?? "").split(",").map((value) => Number.parseInt(value.trim(), 10)).filter((value) => Number.isInteger(value) && value > 0);
1899
+ const start = match.index ?? cursor;
1900
+ if (start > cursor) {
1901
+ parts.push({
1902
+ text: content.slice(cursor, start),
1903
+ type: "text"
1904
+ });
1905
+ }
1906
+ const resolvedReferences = numbers.map((number) => referenceMap.get(number)).filter((reference) => Boolean(reference));
1907
+ for (const number of numbers) {
1908
+ if (!referenceMap.has(number)) {
1909
+ ungroundedReferenceNumbers.add(number);
1910
+ }
1911
+ }
1912
+ const unresolvedNumbers = numbers.filter((number) => !referenceMap.has(number));
1913
+ parts.push({
1914
+ referenceNumbers: numbers,
1915
+ referenceDetails: resolvedReferences.map(buildGroundedAnswerCitationDetail),
1916
+ references: resolvedReferences,
1917
+ text: raw,
1918
+ type: "citation",
1919
+ unresolvedReferenceNumbers: unresolvedNumbers
1920
+ });
1921
+ cursor = start + raw.length;
1922
+ }
1923
+ if (cursor < content.length || parts.length === 0) {
1924
+ parts.push({
1925
+ text: content.slice(cursor),
1926
+ type: "text"
1927
+ });
1928
+ }
1929
+ const hasCitations = parts.some((part) => part.type === "citation");
1930
+ const coverage = !hasCitations ? "ungrounded" : ungroundedReferenceNumbers.size === 0 ? "grounded" : references.length > 0 ? "partial" : "ungrounded";
1931
+ return {
1932
+ content,
1933
+ coverage,
1934
+ excerptModeCounts: buildExcerptModeCounts([
1935
+ ...references,
1936
+ ...sectionSummaries
1937
+ ]),
1938
+ hasCitations,
1939
+ parts,
1940
+ references,
1941
+ sectionSummaries,
1942
+ ungroundedReferenceNumbers: [...ungroundedReferenceNumbers].sort((left, right) => left - right)
1943
+ };
1944
+ };
1945
+ var buildRAGGroundedAnswerSectionSummaries = (references) => {
1946
+ const groups = new Map;
1947
+ for (const reference of references) {
1948
+ const key = buildGroundingSectionKey(reference);
1949
+ const existing = groups.get(key);
1950
+ if (!existing) {
1951
+ const excerpts = reference.excerpts ? {
1952
+ chunkExcerpt: reference.excerpts.chunkExcerpt,
1953
+ sectionExcerpt: reference.excerpts.sectionExcerpt,
1954
+ windowExcerpt: reference.excerpts.windowExcerpt
1955
+ } : undefined;
1956
+ groups.set(key, {
1957
+ chunkIds: [reference.chunkId],
1958
+ contextLabel: reference.contextLabel,
1959
+ count: 1,
1960
+ excerpt: selectPreferredExcerpt(excerpts, getContextNumber(reference.metadata?.sectionChunkCount)).excerpt || excerpts?.sectionExcerpt || reference.excerpt,
1961
+ excerpts,
1962
+ excerptSelection: reference.excerptSelection,
1963
+ key,
1964
+ label: key,
1965
+ locatorLabel: reference.locatorLabel,
1966
+ provenanceLabel: reference.provenanceLabel,
1967
+ referenceNumbers: [reference.number],
1968
+ references: [reference],
1969
+ summary: buildGroundingSectionSummaryLine(reference) || reference.label || reference.chunkId
1970
+ });
1971
+ continue;
1972
+ }
1973
+ existing.count += 1;
1974
+ if (!existing.chunkIds.includes(reference.chunkId)) {
1975
+ existing.chunkIds.push(reference.chunkId);
1976
+ }
1977
+ if (!existing.referenceNumbers.includes(reference.number)) {
1978
+ existing.referenceNumbers.push(reference.number);
1979
+ }
1980
+ existing.references.push(reference);
1981
+ if (!existing.contextLabel && reference.contextLabel) {
1982
+ existing.contextLabel = reference.contextLabel;
1983
+ }
1984
+ if (!existing.locatorLabel && reference.locatorLabel) {
1985
+ existing.locatorLabel = reference.locatorLabel;
1986
+ }
1987
+ if (!existing.provenanceLabel && reference.provenanceLabel) {
1988
+ existing.provenanceLabel = reference.provenanceLabel;
1989
+ }
1990
+ if (!existing.excerpts && reference.excerpts) {
1991
+ existing.excerpts = {
1992
+ chunkExcerpt: reference.excerpts.chunkExcerpt,
1993
+ sectionExcerpt: reference.excerpts.sectionExcerpt,
1994
+ windowExcerpt: reference.excerpts.windowExcerpt
1995
+ };
1996
+ existing.excerpt = reference.excerpts.sectionExcerpt;
1997
+ }
1998
+ if (!existing.excerptSelection && reference.excerptSelection) {
1999
+ existing.excerptSelection = reference.excerptSelection;
2000
+ }
2001
+ }
2002
+ return [...groups.values()].map((group) => ({
2003
+ ...group,
2004
+ referenceNumbers: [...group.referenceNumbers].sort((left, right) => left - right),
2005
+ references: group.references.slice().sort((left, right) => left.number - right.number)
2006
+ })).sort((left, right) => {
2007
+ const leftFirst = left.referenceNumbers[0] ?? Number.POSITIVE_INFINITY;
2008
+ const rightFirst = right.referenceNumbers[0] ?? Number.POSITIVE_INFINITY;
2009
+ if (leftFirst !== rightFirst) {
2010
+ return leftFirst - rightFirst;
2011
+ }
2012
+ return left.label.localeCompare(right.label);
2013
+ });
2014
+ };
2015
+ var buildRAGGroundingReferences = (sources) => {
2016
+ const citations = buildRAGCitations(sources);
2017
+ const citationReferenceMap = buildRAGCitationReferenceMap(citations);
2018
+ return citations.map((citation) => {
2019
+ const excerpts = buildGroundingChunkExcerpts(sources, citation.chunkId);
2020
+ const excerptSelection = selectPreferredExcerpt(excerpts, getContextNumber(citation.metadata?.sectionChunkCount));
2021
+ return {
2022
+ chunkId: citation.chunkId,
2023
+ contextLabel: citation.contextLabel ?? buildContextLabel(citation.metadata),
2024
+ excerpt: excerptSelection.excerpt || excerpts?.chunkExcerpt || buildExcerpt(citation.text),
2025
+ excerpts,
2026
+ excerptSelection,
2027
+ label: citation.label,
2028
+ locatorLabel: citation.locatorLabel ?? buildLocatorLabel(citation.metadata, citation.source, citation.title),
2029
+ metadata: citation.metadata,
2030
+ number: citationReferenceMap[citation.chunkId] ?? 0,
2031
+ provenanceLabel: citation.provenanceLabel ?? buildProvenanceLabel(citation.metadata),
2032
+ score: citation.score,
2033
+ source: citation.source,
2034
+ text: citation.text,
2035
+ title: citation.title
2036
+ };
2037
+ });
2038
+ };
2039
+
2040
+ // src/ai/rag/quality.ts
2041
+ var {mkdir, readFile, writeFile} = (() => ({}));
2042
+
2043
+ // node:path
2044
+ function assertPath(path) {
2045
+ if (typeof path !== "string")
2046
+ throw TypeError("Path must be a string. Received " + JSON.stringify(path));
2047
+ }
2048
+ function normalizeStringPosix(path, allowAboveRoot) {
2049
+ var res = "", lastSegmentLength = 0, lastSlash = -1, dots = 0, code;
2050
+ for (var i = 0;i <= path.length; ++i) {
2051
+ if (i < path.length)
2052
+ code = path.charCodeAt(i);
2053
+ else if (code === 47)
2054
+ break;
2055
+ else
2056
+ code = 47;
2057
+ if (code === 47) {
2058
+ if (lastSlash === i - 1 || dots === 1)
2059
+ ;
2060
+ else if (lastSlash !== i - 1 && dots === 2) {
2061
+ if (res.length < 2 || lastSegmentLength !== 2 || res.charCodeAt(res.length - 1) !== 46 || res.charCodeAt(res.length - 2) !== 46) {
2062
+ if (res.length > 2) {
2063
+ var lastSlashIndex = res.lastIndexOf("/");
2064
+ if (lastSlashIndex !== res.length - 1) {
2065
+ if (lastSlashIndex === -1)
2066
+ res = "", lastSegmentLength = 0;
2067
+ else
2068
+ res = res.slice(0, lastSlashIndex), lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
2069
+ lastSlash = i, dots = 0;
2070
+ continue;
2071
+ }
2072
+ } else if (res.length === 2 || res.length === 1) {
2073
+ res = "", lastSegmentLength = 0, lastSlash = i, dots = 0;
2074
+ continue;
2075
+ }
2076
+ }
2077
+ if (allowAboveRoot) {
2078
+ if (res.length > 0)
2079
+ res += "/..";
2080
+ else
2081
+ res = "..";
2082
+ lastSegmentLength = 2;
2083
+ }
2084
+ } else {
2085
+ if (res.length > 0)
2086
+ res += "/" + path.slice(lastSlash + 1, i);
2087
+ else
2088
+ res = path.slice(lastSlash + 1, i);
2089
+ lastSegmentLength = i - lastSlash - 1;
2090
+ }
2091
+ lastSlash = i, dots = 0;
2092
+ } else if (code === 46 && dots !== -1)
2093
+ ++dots;
2094
+ else
2095
+ dots = -1;
2096
+ }
2097
+ return res;
2098
+ }
2099
+ function _format(sep, pathObject) {
2100
+ var dir = pathObject.dir || pathObject.root, base = pathObject.base || (pathObject.name || "") + (pathObject.ext || "");
2101
+ if (!dir)
2102
+ return base;
2103
+ if (dir === pathObject.root)
2104
+ return dir + base;
2105
+ return dir + sep + base;
2106
+ }
2107
+ function resolve() {
2108
+ var resolvedPath = "", resolvedAbsolute = false, cwd;
2109
+ for (var i = arguments.length - 1;i >= -1 && !resolvedAbsolute; i--) {
2110
+ var path;
2111
+ if (i >= 0)
2112
+ path = arguments[i];
2113
+ else {
2114
+ if (cwd === undefined)
2115
+ cwd = process.cwd();
2116
+ path = cwd;
2117
+ }
2118
+ if (assertPath(path), path.length === 0)
2119
+ continue;
2120
+ resolvedPath = path + "/" + resolvedPath, resolvedAbsolute = path.charCodeAt(0) === 47;
2121
+ }
2122
+ if (resolvedPath = normalizeStringPosix(resolvedPath, !resolvedAbsolute), resolvedAbsolute)
2123
+ if (resolvedPath.length > 0)
2124
+ return "/" + resolvedPath;
2125
+ else
2126
+ return "/";
2127
+ else if (resolvedPath.length > 0)
2128
+ return resolvedPath;
2129
+ else
2130
+ return ".";
2131
+ }
2132
+ function normalize(path) {
2133
+ if (assertPath(path), path.length === 0)
2134
+ return ".";
2135
+ var isAbsolute = path.charCodeAt(0) === 47, trailingSeparator = path.charCodeAt(path.length - 1) === 47;
2136
+ if (path = normalizeStringPosix(path, !isAbsolute), path.length === 0 && !isAbsolute)
2137
+ path = ".";
2138
+ if (path.length > 0 && trailingSeparator)
2139
+ path += "/";
2140
+ if (isAbsolute)
2141
+ return "/" + path;
2142
+ return path;
2143
+ }
2144
+ function isAbsolute(path) {
2145
+ return assertPath(path), path.length > 0 && path.charCodeAt(0) === 47;
2146
+ }
2147
+ function join() {
2148
+ if (arguments.length === 0)
2149
+ return ".";
2150
+ var joined;
2151
+ for (var i = 0;i < arguments.length; ++i) {
2152
+ var arg = arguments[i];
2153
+ if (assertPath(arg), arg.length > 0)
2154
+ if (joined === undefined)
2155
+ joined = arg;
2156
+ else
2157
+ joined += "/" + arg;
2158
+ }
2159
+ if (joined === undefined)
2160
+ return ".";
2161
+ return normalize(joined);
2162
+ }
2163
+ function relative(from, to) {
2164
+ if (assertPath(from), assertPath(to), from === to)
2165
+ return "";
2166
+ if (from = resolve(from), to = resolve(to), from === to)
2167
+ return "";
2168
+ var fromStart = 1;
2169
+ for (;fromStart < from.length; ++fromStart)
2170
+ if (from.charCodeAt(fromStart) !== 47)
2171
+ break;
2172
+ var fromEnd = from.length, fromLen = fromEnd - fromStart, toStart = 1;
2173
+ for (;toStart < to.length; ++toStart)
2174
+ if (to.charCodeAt(toStart) !== 47)
2175
+ break;
2176
+ var toEnd = to.length, toLen = toEnd - toStart, length = fromLen < toLen ? fromLen : toLen, lastCommonSep = -1, i = 0;
2177
+ for (;i <= length; ++i) {
2178
+ if (i === length) {
2179
+ if (toLen > length) {
2180
+ if (to.charCodeAt(toStart + i) === 47)
2181
+ return to.slice(toStart + i + 1);
2182
+ else if (i === 0)
2183
+ return to.slice(toStart + i);
2184
+ } else if (fromLen > length) {
2185
+ if (from.charCodeAt(fromStart + i) === 47)
2186
+ lastCommonSep = i;
2187
+ else if (i === 0)
2188
+ lastCommonSep = 0;
2189
+ }
2190
+ break;
2191
+ }
2192
+ var fromCode = from.charCodeAt(fromStart + i), toCode = to.charCodeAt(toStart + i);
2193
+ if (fromCode !== toCode)
2194
+ break;
2195
+ else if (fromCode === 47)
2196
+ lastCommonSep = i;
2197
+ }
2198
+ var out = "";
2199
+ for (i = fromStart + lastCommonSep + 1;i <= fromEnd; ++i)
2200
+ if (i === fromEnd || from.charCodeAt(i) === 47)
2201
+ if (out.length === 0)
2202
+ out += "..";
2203
+ else
2204
+ out += "/..";
2205
+ if (out.length > 0)
2206
+ return out + to.slice(toStart + lastCommonSep);
2207
+ else {
2208
+ if (toStart += lastCommonSep, to.charCodeAt(toStart) === 47)
2209
+ ++toStart;
2210
+ return to.slice(toStart);
2211
+ }
2212
+ }
2213
+ function _makeLong(path) {
2214
+ return path;
2215
+ }
2216
+ function dirname(path) {
2217
+ if (assertPath(path), path.length === 0)
2218
+ return ".";
2219
+ var code = path.charCodeAt(0), hasRoot = code === 47, end = -1, matchedSlash = true;
2220
+ for (var i = path.length - 1;i >= 1; --i)
2221
+ if (code = path.charCodeAt(i), code === 47) {
2222
+ if (!matchedSlash) {
2223
+ end = i;
2224
+ break;
2225
+ }
2226
+ } else
2227
+ matchedSlash = false;
2228
+ if (end === -1)
2229
+ return hasRoot ? "/" : ".";
2230
+ if (hasRoot && end === 1)
2231
+ return "//";
2232
+ return path.slice(0, end);
2233
+ }
2234
+ function basename(path, ext) {
2235
+ if (ext !== undefined && typeof ext !== "string")
2236
+ throw TypeError('"ext" argument must be a string');
2237
+ assertPath(path);
2238
+ var start = 0, end = -1, matchedSlash = true, i;
2239
+ if (ext !== undefined && ext.length > 0 && ext.length <= path.length) {
2240
+ if (ext.length === path.length && ext === path)
2241
+ return "";
2242
+ var extIdx = ext.length - 1, firstNonSlashEnd = -1;
2243
+ for (i = path.length - 1;i >= 0; --i) {
2244
+ var code = path.charCodeAt(i);
2245
+ if (code === 47) {
2246
+ if (!matchedSlash) {
2247
+ start = i + 1;
2248
+ break;
2249
+ }
2250
+ } else {
2251
+ if (firstNonSlashEnd === -1)
2252
+ matchedSlash = false, firstNonSlashEnd = i + 1;
2253
+ if (extIdx >= 0)
2254
+ if (code === ext.charCodeAt(extIdx)) {
2255
+ if (--extIdx === -1)
2256
+ end = i;
2257
+ } else
2258
+ extIdx = -1, end = firstNonSlashEnd;
2259
+ }
2260
+ }
2261
+ if (start === end)
2262
+ end = firstNonSlashEnd;
2263
+ else if (end === -1)
2264
+ end = path.length;
2265
+ return path.slice(start, end);
2266
+ } else {
2267
+ for (i = path.length - 1;i >= 0; --i)
2268
+ if (path.charCodeAt(i) === 47) {
2269
+ if (!matchedSlash) {
2270
+ start = i + 1;
2271
+ break;
2272
+ }
2273
+ } else if (end === -1)
2274
+ matchedSlash = false, end = i + 1;
2275
+ if (end === -1)
2276
+ return "";
2277
+ return path.slice(start, end);
2278
+ }
2279
+ }
2280
+ function extname(path) {
2281
+ assertPath(path);
2282
+ var startDot = -1, startPart = 0, end = -1, matchedSlash = true, preDotState = 0;
2283
+ for (var i = path.length - 1;i >= 0; --i) {
2284
+ var code = path.charCodeAt(i);
2285
+ if (code === 47) {
2286
+ if (!matchedSlash) {
2287
+ startPart = i + 1;
2288
+ break;
2289
+ }
2290
+ continue;
2291
+ }
2292
+ if (end === -1)
2293
+ matchedSlash = false, end = i + 1;
2294
+ if (code === 46) {
2295
+ if (startDot === -1)
2296
+ startDot = i;
2297
+ else if (preDotState !== 1)
2298
+ preDotState = 1;
2299
+ } else if (startDot !== -1)
2300
+ preDotState = -1;
2301
+ }
2302
+ if (startDot === -1 || end === -1 || preDotState === 0 || preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)
2303
+ return "";
2304
+ return path.slice(startDot, end);
2305
+ }
2306
+ function format(pathObject) {
2307
+ if (pathObject === null || typeof pathObject !== "object")
2308
+ throw TypeError('The "pathObject" argument must be of type Object. Received type ' + typeof pathObject);
2309
+ return _format("/", pathObject);
2310
+ }
2311
+ function parse(path) {
2312
+ assertPath(path);
2313
+ var ret = { root: "", dir: "", base: "", ext: "", name: "" };
2314
+ if (path.length === 0)
2315
+ return ret;
2316
+ var code = path.charCodeAt(0), isAbsolute2 = code === 47, start;
2317
+ if (isAbsolute2)
2318
+ ret.root = "/", start = 1;
2319
+ else
2320
+ start = 0;
2321
+ var startDot = -1, startPart = 0, end = -1, matchedSlash = true, i = path.length - 1, preDotState = 0;
2322
+ for (;i >= start; --i) {
2323
+ if (code = path.charCodeAt(i), code === 47) {
2324
+ if (!matchedSlash) {
2325
+ startPart = i + 1;
2326
+ break;
2327
+ }
2328
+ continue;
2329
+ }
2330
+ if (end === -1)
2331
+ matchedSlash = false, end = i + 1;
2332
+ if (code === 46) {
2333
+ if (startDot === -1)
2334
+ startDot = i;
2335
+ else if (preDotState !== 1)
2336
+ preDotState = 1;
2337
+ } else if (startDot !== -1)
2338
+ preDotState = -1;
2339
+ }
2340
+ if (startDot === -1 || end === -1 || preDotState === 0 || preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
2341
+ if (end !== -1)
2342
+ if (startPart === 0 && isAbsolute2)
2343
+ ret.base = ret.name = path.slice(1, end);
2344
+ else
2345
+ ret.base = ret.name = path.slice(startPart, end);
2346
+ } else {
2347
+ if (startPart === 0 && isAbsolute2)
2348
+ ret.name = path.slice(1, startDot), ret.base = path.slice(1, end);
2349
+ else
2350
+ ret.name = path.slice(startPart, startDot), ret.base = path.slice(startPart, end);
2351
+ ret.ext = path.slice(startDot, end);
2352
+ }
2353
+ if (startPart > 0)
2354
+ ret.dir = path.slice(0, startPart - 1);
2355
+ else if (isAbsolute2)
2356
+ ret.dir = "/";
2357
+ return ret;
2358
+ }
2359
+ var sep = "/";
2360
+ var delimiter = ":";
2361
+ var posix = ((p) => (p.posix = p, p))({ resolve, normalize, isAbsolute, join, relative, _makeLong, dirname, basename, extname, format, parse, sep, delimiter, win32: null, posix: null });
2362
+
2363
+ // src/ai/rag/quality.ts
2364
+ import { generateId } from "@absolutejs/ai";
2365
+ var buildRAGEvaluationLeaderboard = (runs) => {
2366
+ const sorted = [...runs].sort((left, right) => {
2367
+ if (right.response.passingRate !== left.response.passingRate) {
2368
+ return right.response.passingRate - left.response.passingRate;
2369
+ }
2370
+ if (right.response.summary.averageF1 !== left.response.summary.averageF1) {
2371
+ return right.response.summary.averageF1 - left.response.summary.averageF1;
2372
+ }
2373
+ return left.response.summary.averageLatencyMs - right.response.summary.averageLatencyMs;
2374
+ });
2375
+ return sorted.map((run, index) => ({
2376
+ averageF1: run.response.summary.averageF1,
2377
+ averageLatencyMs: run.response.summary.averageLatencyMs,
2378
+ label: run.label,
2379
+ passingRate: run.response.passingRate,
2380
+ rank: index + 1,
2381
+ runId: run.id,
2382
+ suiteId: run.suiteId,
2383
+ totalCases: run.response.totalCases
2384
+ }));
2385
+ };
2386
+ var runRAGEvaluationSuite = async ({
2387
+ suite,
2388
+ evaluate,
2389
+ overrides,
2390
+ artifacts
2391
+ }) => {
2392
+ const startedAt = Date.now();
2393
+ const response = await evaluate({
2394
+ ...suite.input,
2395
+ ...overrides,
2396
+ cases: overrides?.cases ?? suite.input.cases
2397
+ });
2398
+ const finishedAt = Date.now();
2399
+ return {
2400
+ caseTraceSnapshots: artifacts?.caseTraceSnapshots,
2401
+ elapsedMs: finishedAt - startedAt,
2402
+ finishedAt,
2403
+ id: generateId(),
2404
+ label: suite.label ?? suite.id,
2405
+ metadata: suite.metadata,
2406
+ response,
2407
+ startedAt,
2408
+ suiteId: suite.id,
2409
+ traceSummary: artifacts?.traceSummary
2410
+ };
2411
+ };
2412
+
2413
+ // src/ai/rag/presentation.ts
2414
+ var buildSourceGroupKey = (source) => source.source ?? source.title ?? source.chunkId;
2415
+ var buildSourceLabel2 = (source) => source.source ?? source.title ?? source.chunkId;
2416
+ var getContextNumber2 = (value) => typeof value === "number" && Number.isFinite(value) ? value : undefined;
2417
+ var getContextString2 = (value) => typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
2418
+ var isRAGRetrievalTrace = (value) => {
2419
+ if (!value || typeof value !== "object") {
2420
+ return false;
2421
+ }
2422
+ const candidate = value;
2423
+ return typeof candidate.query === "string" && typeof candidate.transformedQuery === "string" && Array.isArray(candidate.variantQueries) && Array.isArray(candidate.steps);
2424
+ };
2425
+ var formatTimestampLabel2 = (value) => {
2426
+ const timestamp = typeof value === "number" && Number.isFinite(value) ? value : typeof value === "string" ? Date.parse(value) : Number.NaN;
2427
+ if (!Number.isFinite(timestamp)) {
2428
+ return;
2429
+ }
2430
+ return new Date(timestamp).toLocaleString("en-US", {
2431
+ dateStyle: "medium",
2432
+ timeStyle: "short"
2433
+ });
2434
+ };
2435
+ var formatSourceAwareChunkReason = (value) => {
2436
+ const reason = getContextString2(value);
2437
+ if (reason === "section_boundary") {
2438
+ return "Chunk boundary section";
2439
+ }
2440
+ if (reason === "size_limit") {
2441
+ return "Chunk boundary size limit";
2442
+ }
2443
+ if (reason === "source_native_unit") {
2444
+ return "Chunk boundary source-native unit";
2445
+ }
2446
+ return;
2447
+ };
2448
+ var buildSourceAwareUnitScopeLabel = (metadata) => {
2449
+ if (!metadata) {
2450
+ return;
2451
+ }
2452
+ const sectionKind = getContextString2(metadata.sectionKind);
2453
+ const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.map((value) => getContextString2(value)).filter((value) => typeof value === "string") : [];
2454
+ const sectionTitle = getContextString2(metadata.sectionTitle) ?? sectionPath.at(-1);
2455
+ const pdfSemanticRole = getContextString2(metadata.pdfSemanticRole);
2456
+ const pdfTextKind = getContextString2(metadata.pdfTextKind);
2457
+ const officeBlockKindValue = getContextString2(metadata.officeBlockKind);
2458
+ const officeBlockKind = officeBlockKindValue === "table" || officeBlockKindValue === "list" || officeBlockKindValue === "paragraph" ? officeBlockKindValue : undefined;
2459
+ const sheetName = getContextString2(metadata.sheetName);
2460
+ const spreadsheetTableLabel = formatSpreadsheetTableLabel(getContextNumber2(metadata.spreadsheetTableIndex), getContextNumber2(metadata.spreadsheetTableCount));
2461
+ const slideTitle = getContextString2(metadata.slideTitle);
2462
+ const slideNumber = getContextNumber2(metadata.slideNumber) ?? (typeof metadata.slideIndex === "number" ? metadata.slideIndex + 1 : undefined);
2463
+ if (sectionPath.length > 0 && (sectionKind === "markdown_heading" || sectionKind === "html_heading" || sectionKind === "office_heading" || sectionKind === undefined)) {
2464
+ return `Source-aware section ${sectionPath.join(" > ")}`;
2465
+ }
2466
+ if (sectionKind === "pdf_block") {
2467
+ if (pdfSemanticRole === "figure_caption" && sectionTitle) {
2468
+ return `Source-aware PDF figure caption ${sectionTitle}`;
2469
+ }
2470
+ if (pdfSemanticRole === "figure_body" && sectionTitle) {
2471
+ return `Source-aware PDF figure body ${sectionTitle}`;
2472
+ }
2473
+ if (pdfTextKind === "table_like" && sectionTitle) {
2474
+ return `Source-aware PDF table block ${sectionTitle}`;
2475
+ }
2476
+ if (sectionTitle) {
2477
+ return `Source-aware PDF block ${sectionTitle}`;
2478
+ }
2479
+ return "Source-aware PDF block";
2480
+ }
2481
+ if (sectionKind === "office_block") {
2482
+ const officeSectionLabel = sectionPath.length > 0 ? sectionPath.join(" > ") : sectionTitle;
2483
+ if (officeBlockKind && officeSectionLabel) {
2484
+ return `Source-aware office ${officeBlockKind} block ${officeSectionLabel}`;
2485
+ }
2486
+ if (officeSectionLabel) {
2487
+ return `Source-aware office block ${officeSectionLabel}`;
2488
+ }
2489
+ return "Source-aware office block";
2490
+ }
2491
+ if (sectionKind === "spreadsheet_rows" || sectionKind === undefined && (sheetName || spreadsheetTableLabel || getContextNumber2(metadata.spreadsheetRowStart) !== undefined || getContextNumber2(metadata.spreadsheetRowEnd) !== undefined)) {
2492
+ if (sheetName && spreadsheetTableLabel) {
2493
+ return `Source-aware spreadsheet ${sheetName} ${spreadsheetTableLabel}`;
2494
+ }
2495
+ if (sheetName) {
2496
+ return `Source-aware spreadsheet ${sheetName}`;
2497
+ }
2498
+ return "Source-aware spreadsheet";
2499
+ }
2500
+ if (sectionKind === "presentation_slide") {
2501
+ if (slideNumber && slideTitle) {
2502
+ return `Source-aware slide ${slideNumber} ${slideTitle}`;
2503
+ }
2504
+ if (slideTitle) {
2505
+ return `Source-aware slide ${slideTitle}`;
2506
+ }
2507
+ if (slideNumber) {
2508
+ return `Source-aware slide ${slideNumber}`;
2509
+ }
2510
+ return "Source-aware slide";
2511
+ }
2512
+ return;
2513
+ };
2514
+ var formatMediaTimestamp2 = (value) => {
2515
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
2516
+ return;
2517
+ }
2518
+ const totalSeconds = Math.floor(value / 1000);
2519
+ const minutes = Math.floor(totalSeconds / 60);
2520
+ const seconds = totalSeconds % 60;
2521
+ const milliseconds = Math.floor(value % 1000);
2522
+ return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}.${String(milliseconds).padStart(3, "0")}`;
2523
+ };
2524
+ var getAttachmentName2 = (source, title) => {
2525
+ const sourceAttachment = source?.split("/").at(-1);
2526
+ if (sourceAttachment && sourceAttachment.includes(".")) {
2527
+ return sourceAttachment;
2528
+ }
2529
+ const titleAttachment = title?.split(" · ").at(-1);
2530
+ if (titleAttachment && titleAttachment.includes(".")) {
2531
+ return titleAttachment;
2532
+ }
2533
+ return;
2534
+ };
2535
+ var getSpreadsheetHeaders = (metadata) => Array.isArray(metadata?.spreadsheetHeaders) ? metadata.spreadsheetHeaders.map((value) => getContextString2(value)).filter((value) => typeof value === "string") : [];
2536
+ var formatSpreadsheetColumnRange = (columnStart, columnEnd) => {
2537
+ if (typeof columnStart !== "string" || columnStart.length === 0) {
2538
+ return;
2539
+ }
2540
+ if (typeof columnEnd !== "string" || columnEnd.length === 0) {
2541
+ return `Columns ${columnStart}`;
2542
+ }
2543
+ if (columnStart === columnEnd) {
2544
+ return `Columns ${columnStart}`;
2545
+ }
2546
+ return `Columns ${columnStart}-${columnEnd}`;
2547
+ };
2548
+ var formatSpreadsheetRowRange = (rowStart, rowEnd) => {
2549
+ if (typeof rowStart !== "number" || !Number.isFinite(rowStart)) {
2550
+ return;
2551
+ }
2552
+ if (typeof rowEnd !== "number" && typeof rowStart === "number" && Number.isFinite(rowStart)) {
2553
+ return `Rows ${rowStart}`;
2554
+ }
2555
+ if (rowStart === rowEnd) {
2556
+ return `Rows ${rowStart}`;
2557
+ }
2558
+ return `Rows ${rowStart}-${rowEnd}`;
2559
+ };
2560
+ var formatSpreadsheetTableLabel = (tableIndex, tableCount) => {
2561
+ if (typeof tableIndex !== "number" || !Number.isFinite(tableIndex) || tableIndex < 1) {
2562
+ return;
2563
+ }
2564
+ if (typeof tableCount === "number" && Number.isFinite(tableCount) && tableCount >= tableIndex) {
2565
+ return `Table ${tableIndex} of ${tableCount}`;
2566
+ }
2567
+ return `Table ${tableIndex}`;
2568
+ };
2569
+ var formatOfficeListLevelsLabel2 = (value) => {
2570
+ if (!Array.isArray(value) || value.length === 0) {
2571
+ return;
2572
+ }
2573
+ const levels = value.map((entry) => getContextNumber2(entry)).filter((entry) => typeof entry === "number").sort((left, right) => left - right);
2574
+ if (levels.length === 0) {
2575
+ return;
2576
+ }
2577
+ const minLevel = levels[0];
2578
+ const maxLevel = levels[levels.length - 1];
2579
+ return minLevel === maxLevel ? `Office list level ${minLevel}` : `Office list levels ${minLevel}-${maxLevel}`;
2580
+ };
2581
+ var formatMediaDurationLabel2 = (value) => {
2582
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
2583
+ return;
2584
+ }
2585
+ return formatMediaTimestamp2(value);
2586
+ };
2587
+ var buildContextLabel2 = (metadata) => {
2588
+ if (!metadata) {
2589
+ return;
2590
+ }
2591
+ const pdfTextKind = getContextString2(metadata.pdfTextKind);
2592
+ const pdfSemanticRole = getContextString2(metadata.pdfSemanticRole);
2593
+ const pdfTableBodyRowStart = getContextNumber2(metadata.pdfTableBodyRowStart);
2594
+ const pdfTableBodyRowEnd = getContextNumber2(metadata.pdfTableBodyRowEnd);
2595
+ const officeBlockKindValue = getContextString2(metadata.officeBlockKind);
2596
+ const officeBlockKind = officeBlockKindValue === "table" || officeBlockKindValue === "list" || officeBlockKindValue === "paragraph" ? officeBlockKindValue : undefined;
2597
+ const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.map((value) => getContextString2(value)).filter((value) => typeof value === "string") : [];
2598
+ const sectionTitle = getContextString2(metadata.sectionTitle) ?? sectionPath.at(-1);
2599
+ if (pdfSemanticRole === "figure_caption" && sectionTitle) {
2600
+ return `PDF figure caption ${sectionTitle}`;
2601
+ }
2602
+ if (pdfSemanticRole === "figure_body" && sectionTitle) {
2603
+ return `PDF figure body ${sectionTitle}`;
2604
+ }
2605
+ if (pdfTextKind === "table_like" && sectionTitle) {
2606
+ return `PDF table block ${sectionTitle}`;
2607
+ }
2608
+ if (pdfTextKind === "paragraph" && sectionTitle) {
2609
+ return `PDF text block ${sectionTitle}`;
2610
+ }
2611
+ if (officeBlockKind === "table" && sectionTitle) {
2612
+ return `Office table block ${sectionPath.join(" > ") || sectionTitle}`;
2613
+ }
2614
+ if (officeBlockKind === "list" && sectionTitle) {
2615
+ return `Office list block ${sectionPath.join(" > ") || sectionTitle}`;
2616
+ }
2617
+ if (officeBlockKind === "paragraph" && sectionTitle) {
2618
+ return `Office paragraph block ${sectionPath.join(" > ") || sectionTitle}`;
2619
+ }
2620
+ const emailKind = getContextString2(metadata.emailKind);
2621
+ if (emailKind === "attachment") {
2622
+ const attachmentName = getContextString2(metadata.attachmentName);
2623
+ const threadTopic2 = getContextString2(metadata.threadTopic);
2624
+ return attachmentName ? threadTopic2 ? `Attachment evidence ${attachmentName} in ${threadTopic2}` : `Attachment evidence ${attachmentName}` : "Attachment evidence";
2625
+ }
2626
+ if (emailKind === "message") {
2627
+ const threadTopic2 = getContextString2(metadata.threadTopic);
2628
+ const from = getContextString2(metadata.from);
2629
+ if (threadTopic2) {
2630
+ return from ? `Message in ${threadTopic2} from ${from}` : `Message in ${threadTopic2}`;
2631
+ }
2632
+ return from ? `Message from ${from}` : "Message evidence";
2633
+ }
2634
+ const page = getContextNumber2(metadata.page) ?? getContextNumber2(metadata.pageNumber) ?? (typeof metadata.pageIndex === "number" ? metadata.pageIndex + 1 : undefined);
2635
+ const region = getContextNumber2(metadata.regionNumber) ?? (typeof metadata.regionIndex === "number" ? metadata.regionIndex + 1 : undefined);
2636
+ const hasOCRTrace = typeof metadata.ocrRegionConfidence === "number" || typeof metadata.ocrConfidence === "number" || getContextString2(metadata.pdfTextMode) === "ocr" || typeof metadata.ocrRegionCount === "number";
2637
+ const ocrPageStart = getContextNumber2(metadata.ocrPageStart);
2638
+ const ocrPageEnd = getContextNumber2(metadata.ocrPageEnd);
2639
+ if (page && region) {
2640
+ if (hasOCRTrace) {
2641
+ return `OCR page ${page} region ${region}`;
2642
+ }
2643
+ return `Page ${page} region ${region}`;
2644
+ }
2645
+ if (page) {
2646
+ if (hasOCRTrace) {
2647
+ return `OCR page ${page}`;
2648
+ }
2649
+ return `Page ${page}`;
2650
+ }
2651
+ if (hasOCRTrace && typeof ocrPageStart === "number" && typeof ocrPageEnd === "number") {
2652
+ return ocrPageStart === ocrPageEnd ? `OCR page ${ocrPageStart}` : `OCR pages ${ocrPageStart}-${ocrPageEnd}`;
2653
+ }
2654
+ const sheet = getContextString2(metadata.sheetName) ?? (Array.isArray(metadata.sheetNames) ? getContextString2(metadata.sheetNames[0]) : undefined);
2655
+ if (sheet) {
2656
+ const tableLabel = formatSpreadsheetTableLabel(getContextNumber2(metadata.spreadsheetTableIndex), getContextNumber2(metadata.spreadsheetTableCount));
2657
+ const columnRange = formatSpreadsheetColumnRange(getContextString2(metadata.spreadsheetColumnStart), getContextString2(metadata.spreadsheetColumnEnd));
2658
+ const rowRange = formatSpreadsheetRowRange(getContextNumber2(metadata.spreadsheetRowStart), getContextNumber2(metadata.spreadsheetRowEnd));
2659
+ const headers = getSpreadsheetHeaders(metadata);
2660
+ if (tableLabel && rowRange && columnRange) {
2661
+ return `Sheet ${sheet} ${tableLabel} ${rowRange} ${columnRange}`;
2662
+ }
2663
+ if (tableLabel && rowRange) {
2664
+ return `Sheet ${sheet} ${tableLabel} ${rowRange}`;
2665
+ }
2666
+ if (tableLabel && columnRange) {
2667
+ return `Sheet ${sheet} ${tableLabel} ${columnRange}`;
2668
+ }
2669
+ if (tableLabel) {
2670
+ return `Sheet ${sheet} ${tableLabel}`;
2671
+ }
2672
+ if (rowRange && columnRange) {
2673
+ return `Sheet ${sheet} ${rowRange} ${columnRange}`;
2674
+ }
2675
+ if (rowRange) {
2676
+ return `Sheet ${sheet} ${rowRange}`;
2677
+ }
2678
+ if (columnRange) {
2679
+ return `Sheet ${sheet} ${columnRange}`;
2680
+ }
2681
+ if (headers.length > 0) {
2682
+ return `Sheet ${sheet} by ${headers.slice(0, 2).join(", ")}`;
2683
+ }
2684
+ return `Sheet ${sheet}`;
2685
+ }
2686
+ const slide = getContextNumber2(metadata.slide) ?? getContextNumber2(metadata.slideNumber) ?? (typeof metadata.slideIndex === "number" ? metadata.slideIndex + 1 : undefined);
2687
+ const slideTitle = getContextString2(metadata.slideTitle);
2688
+ if (slide) {
2689
+ if (slideTitle) {
2690
+ return `Slide ${slide} ${slideTitle}`;
2691
+ }
2692
+ return `Slide ${slide}`;
2693
+ }
2694
+ const archiveEntry = getContextString2(metadata.archiveFullPath) ?? getContextString2(metadata.archivePath) ?? getContextString2(metadata.archiveEntryPath) ?? getContextString2(metadata.entryPath);
2695
+ if (archiveEntry) {
2696
+ return `Archive entry ${archiveEntry}`;
2697
+ }
2698
+ const threadTopic = getContextString2(metadata.threadTopic);
2699
+ if (threadTopic) {
2700
+ return `Thread ${threadTopic}`;
2701
+ }
2702
+ const speaker = getContextString2(metadata.speaker);
2703
+ if (speaker) {
2704
+ return `Speaker ${speaker}`;
2705
+ }
2706
+ if (sectionTitle) {
2707
+ return `Section ${sectionTitle}`;
2708
+ }
2709
+ return;
2710
+ };
2711
+ var buildLocatorLabel2 = (metadata, source, title) => {
2712
+ if (!metadata) {
2713
+ return;
2714
+ }
2715
+ const pdfTextKind = getContextString2(metadata.pdfTextKind);
2716
+ const pdfSemanticRole = getContextString2(metadata.pdfSemanticRole);
2717
+ const officeBlockKind = getContextString2(metadata.officeBlockKind);
2718
+ const pdfBlockNumber = getContextNumber2(metadata.pdfBlockNumber);
2719
+ const pdfTableBodyRowStart = getContextNumber2(metadata.pdfTableBodyRowStart);
2720
+ const pdfTableBodyRowEnd = getContextNumber2(metadata.pdfTableBodyRowEnd);
2721
+ const officeBlockNumber = getContextNumber2(metadata.officeBlockNumber);
2722
+ const officeTableBodyRowStart = getContextNumber2(metadata.officeTableBodyRowStart);
2723
+ const officeTableBodyRowEnd = getContextNumber2(metadata.officeTableBodyRowEnd);
2724
+ const spreadsheetRowStart = getContextNumber2(metadata.spreadsheetRowStart);
2725
+ const spreadsheetRowEnd = getContextNumber2(metadata.spreadsheetRowEnd);
2726
+ const slideTitle = getContextString2(metadata.slideTitle);
2727
+ const page = getContextNumber2(metadata.page) ?? getContextNumber2(metadata.pageNumber) ?? (typeof metadata.pageIndex === "number" ? metadata.pageIndex + 1 : undefined);
2728
+ const region = getContextNumber2(metadata.regionNumber) ?? (typeof metadata.regionIndex === "number" ? metadata.regionIndex + 1 : undefined);
2729
+ const ocrPageStart = getContextNumber2(metadata.ocrPageStart);
2730
+ const ocrPageEnd = getContextNumber2(metadata.ocrPageEnd);
2731
+ if (page && region) {
2732
+ return `Page ${page} · Region ${region}`;
2733
+ }
2734
+ if (page && pdfBlockNumber && pdfSemanticRole === "figure_caption") {
2735
+ return `Page ${page} · Figure Caption ${pdfBlockNumber}`;
2736
+ }
2737
+ if (page && pdfBlockNumber && pdfSemanticRole === "figure_body") {
2738
+ return `Page ${page} · Figure Body ${pdfBlockNumber}`;
2739
+ }
2740
+ if (page && pdfBlockNumber && pdfTextKind === "table_like") {
2741
+ if (typeof pdfTableBodyRowStart === "number" && typeof pdfTableBodyRowEnd === "number") {
2742
+ return pdfTableBodyRowStart === pdfTableBodyRowEnd ? `Page ${page} · Table Block ${pdfBlockNumber} · Row ${pdfTableBodyRowStart}` : `Page ${page} · Table Block ${pdfBlockNumber} · Rows ${pdfTableBodyRowStart}-${pdfTableBodyRowEnd}`;
2743
+ }
2744
+ return `Page ${page} · Table Block ${pdfBlockNumber}`;
2745
+ }
2746
+ if (page && pdfBlockNumber) {
2747
+ return `Page ${page} · Text Block ${pdfBlockNumber}`;
2748
+ }
2749
+ if (page) {
2750
+ return `Page ${page}`;
2751
+ }
2752
+ if (typeof ocrPageStart === "number" && typeof ocrPageEnd === "number") {
2753
+ return ocrPageStart === ocrPageEnd ? `Page ${ocrPageStart}` : `Pages ${ocrPageStart}-${ocrPageEnd}`;
2754
+ }
2755
+ const sheet = getContextString2(metadata.sheetName) ?? (Array.isArray(metadata.sheetNames) ? getContextString2(metadata.sheetNames[0]) : undefined);
2756
+ if (sheet) {
2757
+ const tableLabel = formatSpreadsheetTableLabel(getContextNumber2(metadata.spreadsheetTableIndex), getContextNumber2(metadata.spreadsheetTableCount));
2758
+ const columnRange = formatSpreadsheetColumnRange(getContextString2(metadata.spreadsheetColumnStart), getContextString2(metadata.spreadsheetColumnEnd));
2759
+ const rowRange = formatSpreadsheetRowRange(spreadsheetRowStart, spreadsheetRowEnd);
2760
+ if (tableLabel && rowRange && columnRange) {
2761
+ return `Sheet ${sheet} · ${tableLabel} · ${rowRange} · ${columnRange}`;
2762
+ }
2763
+ if (tableLabel && rowRange) {
2764
+ return `Sheet ${sheet} · ${tableLabel} · ${rowRange}`;
2765
+ }
2766
+ if (tableLabel && columnRange) {
2767
+ return `Sheet ${sheet} · ${tableLabel} · ${columnRange}`;
2768
+ }
2769
+ if (tableLabel) {
2770
+ return `Sheet ${sheet} · ${tableLabel}`;
2771
+ }
2772
+ if (rowRange && columnRange) {
2773
+ return `Sheet ${sheet} · ${rowRange} · ${columnRange}`;
2774
+ }
2775
+ if (rowRange) {
2776
+ return `Sheet ${sheet} · ${rowRange}`;
2777
+ }
2778
+ return columnRange ? `Sheet ${sheet} · ${columnRange}` : `Sheet ${sheet}`;
2779
+ }
2780
+ const slide = getContextNumber2(metadata.slide) ?? getContextNumber2(metadata.slideNumber) ?? (typeof metadata.slideIndex === "number" ? metadata.slideIndex + 1 : undefined);
2781
+ if (slide) {
2782
+ return slideTitle ? `Slide ${slide} · ${slideTitle}` : `Slide ${slide}`;
2783
+ }
2784
+ const archiveEntry = getContextString2(metadata.archiveFullPath) ?? getContextString2(metadata.archivePath) ?? getContextString2(metadata.archiveEntryPath) ?? getContextString2(metadata.entryPath);
2785
+ if (archiveEntry) {
2786
+ return `Archive entry ${archiveEntry}`;
2787
+ }
2788
+ const emailKind = getContextString2(metadata.emailKind);
2789
+ if (emailKind === "attachment") {
2790
+ const attachmentName = getContextString2(metadata.attachmentName) ?? getAttachmentName2(source, title);
2791
+ const replyDepth = getContextNumber2(metadata.replyDepth);
2792
+ if (attachmentName && replyDepth && replyDepth > 0) {
2793
+ return `Attachment ${attachmentName} · Reply depth ${replyDepth}`;
2794
+ }
2795
+ return attachmentName ? `Attachment ${attachmentName}` : "Attachment";
2796
+ }
2797
+ const mediaStart = formatMediaTimestamp2(metadata.startMs);
2798
+ const mediaEnd = formatMediaTimestamp2(metadata.endMs);
2799
+ if (mediaStart && mediaEnd) {
2800
+ return `Timestamp ${mediaStart} - ${mediaEnd}`;
2801
+ }
2802
+ if (mediaStart) {
2803
+ return `Timestamp ${mediaStart}`;
2804
+ }
2805
+ if (officeBlockNumber && officeBlockKind === "table") {
2806
+ if (typeof officeTableBodyRowStart === "number" && typeof officeTableBodyRowEnd === "number") {
2807
+ return officeTableBodyRowStart === officeTableBodyRowEnd ? `Office table block ${officeBlockNumber} · Row ${officeTableBodyRowStart}` : `Office table block ${officeBlockNumber} · Rows ${officeTableBodyRowStart}-${officeTableBodyRowEnd}`;
2808
+ }
2809
+ return `Office table block ${officeBlockNumber}`;
2810
+ }
2811
+ if (officeBlockNumber && officeBlockKind === "list") {
2812
+ return `Office list block ${officeBlockNumber}`;
2813
+ }
2814
+ if (officeBlockNumber && officeBlockKind === "paragraph") {
2815
+ return `Office paragraph block ${officeBlockNumber}`;
2816
+ }
2817
+ const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.map((value) => getContextString2(value)).filter((value) => typeof value === "string") : [];
2818
+ if (sectionPath.length > 0) {
2819
+ return `Section ${sectionPath.join(" > ")}`;
2820
+ }
2821
+ return;
2822
+ };
2823
+ var buildProvenanceLabel2 = (metadata) => {
2824
+ if (!metadata) {
2825
+ return;
2826
+ }
2827
+ const threadTopic = getContextString2(metadata.threadTopic);
2828
+ const replyDepth = getContextNumber2(metadata.replyDepth);
2829
+ const threadMessageCount = getContextNumber2(metadata.threadMessageCount);
2830
+ const threadRootMessageId = getContextString2(metadata.threadRootMessageId);
2831
+ const from = getContextString2(metadata.from);
2832
+ const sentAt = formatTimestampLabel2(metadata.sentAt) ?? formatTimestampLabel2(metadata.receivedAt);
2833
+ const speaker = getContextString2(metadata.speaker);
2834
+ const mediaKind = getContextString2(metadata.mediaKind);
2835
+ const transcriptSource = getContextString2(metadata.transcriptSource);
2836
+ const mediaSpeakerCount = getContextNumber2(metadata.mediaSpeakerCount);
2837
+ const mediaSegmentCount = getContextNumber2(metadata.mediaSegmentCount);
2838
+ const mediaSegmentGroupSize = getContextNumber2(metadata.mediaSegmentGroupSize);
2839
+ const mediaSegmentGroupIndex = getContextNumber2(metadata.mediaSegmentGroupIndex);
2840
+ const mediaChannel = getContextString2(metadata.mediaChannel);
2841
+ const mediaDurationLabel = formatMediaDurationLabel2(metadata.mediaDurationMs);
2842
+ const mediaSegmentWindowDurationLabel = formatMediaDurationLabel2(metadata.mediaSegmentGroupDurationMs);
2843
+ const mediaSegmentGapLabel = formatMediaDurationLabel2(metadata.mediaSegmentGapFromPreviousMs);
2844
+ const spreadsheetHeaders = getSpreadsheetHeaders(metadata);
2845
+ const pdfTableHeaders = Array.isArray(metadata.pdfTableHeaders) ? metadata.pdfTableHeaders.map((value) => getContextString2(value)).filter((value) => typeof value === "string") : [];
2846
+ const pdfTableColumnCount = getContextNumber2(metadata.pdfTableColumnCount);
2847
+ const pdfTableBodyRowCount = getContextNumber2(metadata.pdfTableBodyRowCount);
2848
+ const spreadsheetColumnRange = formatSpreadsheetColumnRange(getContextString2(metadata.spreadsheetColumnStart), getContextString2(metadata.spreadsheetColumnEnd));
2849
+ const slideNotesText = getContextString2(metadata.slideNotesText);
2850
+ const pdfTextMode = getContextString2(metadata.pdfTextMode);
2851
+ const pdfEvidenceMode = getContextString2(metadata.pdfEvidenceMode);
2852
+ const pdfEvidenceOrigin = getContextString2(metadata.pdfEvidenceOrigin);
2853
+ const pdfEvidenceSupplement = getContextString2(metadata.pdfEvidenceSupplement);
2854
+ const pdfTextKind = getContextString2(metadata.pdfTextKind);
2855
+ const pdfSemanticRole = getContextString2(metadata.pdfSemanticRole);
2856
+ const officeBlockKind = getContextString2(metadata.officeBlockKind);
2857
+ const officeListContextText = getContextString2(metadata.officeListContextText);
2858
+ const officeListGroupItemCount = getContextNumber2(metadata.officeListGroupItemCount);
2859
+ const officeListLevelsLabel = formatOfficeListLevelsLabel2(metadata.officeListLevels);
2860
+ const officeTableHeaders = Array.isArray(metadata.officeTableHeaders) ? metadata.officeTableHeaders.map((value) => getContextString2(value)).filter((value) => typeof value === "string") : [];
2861
+ const officeTableColumnCount = getContextNumber2(metadata.officeTableColumnCount);
2862
+ const officeTableBodyRowCount = getContextNumber2(metadata.officeTableBodyRowCount);
2863
+ const officeTableBodyRowStart = getContextNumber2(metadata.officeTableBodyRowStart);
2864
+ const officeTableBodyRowEnd = getContextNumber2(metadata.officeTableBodyRowEnd);
2865
+ const officeTableContextText = getContextString2(metadata.officeTableContextText);
2866
+ const officeTableFollowUpText = getContextString2(metadata.officeTableFollowUpText);
2867
+ const ocrEngine = getContextString2(metadata.ocrEngine);
2868
+ const extractorRegistryMatch = getContextString2(metadata.extractorRegistryMatch);
2869
+ const chunkingProfile = getContextString2(metadata.chunkingProfile);
2870
+ const archiveDepth = getContextNumber2(metadata.archiveDepth);
2871
+ const archiveNestedDepth = getContextNumber2(metadata.archiveNestedDepth);
2872
+ const archiveContainerPath = getContextString2(metadata.archiveContainerPath);
2873
+ const archiveRootName = getContextString2(metadata.archiveRootName);
2874
+ const sourceAwareChunkReason = formatSourceAwareChunkReason(metadata.sourceAwareChunkReason);
2875
+ const sourceAwareUnitScope = buildSourceAwareUnitScopeLabel(metadata);
2876
+ const spreadsheetTableLabel = formatSpreadsheetTableLabel(getContextNumber2(metadata.spreadsheetTableIndex), getContextNumber2(metadata.spreadsheetTableCount));
2877
+ const ocrConfidence = getContextNumber2(metadata.ocrRegionConfidence) ?? getContextNumber2(metadata.ocrConfidence);
2878
+ const ocrAverageConfidence = getContextNumber2(metadata.ocrPageAverageConfidence) ?? getContextNumber2(metadata.ocrAverageConfidence);
2879
+ const ocrMinConfidence = getContextNumber2(metadata.ocrPageMinConfidence) ?? getContextNumber2(metadata.ocrMinConfidence);
2880
+ const ocrMaxConfidence = getContextNumber2(metadata.ocrPageMaxConfidence) ?? getContextNumber2(metadata.ocrMaxConfidence);
2881
+ const ocrRegionCount = getContextNumber2(metadata.ocrRegionCount);
2882
+ const pdfTableBodyRowStart = getContextNumber2(metadata.pdfTableBodyRowStart);
2883
+ const pdfTableBodyRowEnd = getContextNumber2(metadata.pdfTableBodyRowEnd);
2884
+ const labels = [
2885
+ pdfTextMode ? `PDF ${pdfTextMode}` : "",
2886
+ pdfEvidenceMode ? `PDF evidence ${pdfEvidenceMode}` : "",
2887
+ pdfEvidenceOrigin ? `PDF origin ${pdfEvidenceOrigin}` : "",
2888
+ pdfEvidenceSupplement ? `PDF supplement ${pdfEvidenceSupplement}` : "",
2889
+ pdfSemanticRole === "figure_caption" ? "PDF figure caption" : "",
2890
+ pdfSemanticRole === "figure_body" ? "PDF figure body" : "",
2891
+ pdfSemanticRole === "figure_caption" ? "" : pdfSemanticRole === "figure_body" ? "" : pdfTextKind === "table_like" ? "PDF table block" : pdfTextKind === "paragraph" ? "PDF text block" : "",
2892
+ officeBlockKind ? `Office ${officeBlockKind}` : "",
2893
+ typeof officeListGroupItemCount === "number" ? `Office list ${officeListGroupItemCount} items` : "",
2894
+ officeListLevelsLabel ?? "",
2895
+ ocrEngine ? `OCR ${ocrEngine}` : "",
2896
+ extractorRegistryMatch ? `Extractor ${extractorRegistryMatch}` : "",
2897
+ chunkingProfile ? `Chunking ${chunkingProfile}` : "",
2898
+ sourceAwareChunkReason ?? "",
2899
+ sourceAwareUnitScope ?? "",
2900
+ typeof ocrConfidence === "number" ? `Confidence ${ocrConfidence.toFixed(2)}` : "",
2901
+ typeof ocrAverageConfidence === "number" && ocrAverageConfidence !== ocrConfidence ? `Average ${ocrAverageConfidence.toFixed(2)}` : "",
2902
+ typeof ocrMinConfidence === "number" && typeof ocrMaxConfidence === "number" && ocrMinConfidence !== ocrMaxConfidence ? `Range ${ocrMinConfidence.toFixed(2)}-${ocrMaxConfidence.toFixed(2)}` : "",
2903
+ typeof ocrRegionCount === "number" ? `${ocrRegionCount} regions` : "",
2904
+ pdfTableHeaders.length > 0 ? `PDF table ${pdfTableHeaders.join(", ")}` : "",
2905
+ typeof pdfTableColumnCount === "number" ? `PDF table ${pdfTableColumnCount} cols` : "",
2906
+ typeof pdfTableBodyRowCount === "number" ? `PDF table ${pdfTableBodyRowCount} body rows` : "",
2907
+ typeof pdfTableBodyRowStart === "number" && typeof pdfTableBodyRowEnd === "number" ? pdfTableBodyRowStart === pdfTableBodyRowEnd ? `PDF table row ${pdfTableBodyRowStart}` : `PDF table rows ${pdfTableBodyRowStart}-${pdfTableBodyRowEnd}` : "",
2908
+ officeListContextText ? `Office list context ${officeListContextText}` : "",
2909
+ officeTableHeaders.length > 0 ? `Office table ${officeTableHeaders.join(", ")}` : "",
2910
+ typeof officeTableColumnCount === "number" ? `Office table ${officeTableColumnCount} cols` : "",
2911
+ typeof officeTableBodyRowCount === "number" ? `Office table ${officeTableBodyRowCount} body rows` : "",
2912
+ typeof officeTableBodyRowStart === "number" && typeof officeTableBodyRowEnd === "number" ? officeTableBodyRowStart === officeTableBodyRowEnd ? `Office table row ${officeTableBodyRowStart}` : `Office table rows ${officeTableBodyRowStart}-${officeTableBodyRowEnd}` : "",
2913
+ officeTableContextText ? `Office table context ${officeTableContextText}` : "",
2914
+ officeTableFollowUpText ? `Office table follow-up ${officeTableFollowUpText}` : "",
2915
+ spreadsheetHeaders.length > 0 ? `Spreadsheet ${spreadsheetHeaders.join(", ")}` : "",
2916
+ spreadsheetColumnRange ? `Spreadsheet ${spreadsheetColumnRange}` : "",
2917
+ spreadsheetTableLabel ? `Spreadsheet ${spreadsheetTableLabel}` : "",
2918
+ mediaKind ? `Media ${mediaKind}` : "",
2919
+ mediaSegmentCount ? `${mediaSegmentCount} segments` : "",
2920
+ mediaSegmentGroupSize ? `${mediaSegmentGroupSize} grouped segments` : "",
2921
+ mediaSegmentGroupIndex !== undefined ? `Segment group ${mediaSegmentGroupIndex + 1}` : "",
2922
+ mediaChannel ? `Channel ${mediaChannel}` : "",
2923
+ mediaSpeakerCount ? `${mediaSpeakerCount} speakers` : "",
2924
+ mediaDurationLabel ? `Duration ${mediaDurationLabel}` : "",
2925
+ mediaSegmentWindowDurationLabel ? `Segment window ${mediaSegmentWindowDurationLabel}` : "",
2926
+ mediaSegmentGapLabel ? `Gap ${mediaSegmentGapLabel} from previous window` : "",
2927
+ transcriptSource ? `Transcript ${transcriptSource}` : "",
2928
+ threadTopic ? `Thread ${threadTopic}` : "",
2929
+ threadRootMessageId ? `Thread root ${threadRootMessageId}` : "",
2930
+ threadMessageCount ? `${threadMessageCount} thread messages` : "",
2931
+ replyDepth ? `Reply depth ${replyDepth}` : "",
2932
+ slideNotesText ? "Speaker notes" : "",
2933
+ archiveDepth ? `Archive depth ${archiveDepth}` : "",
2934
+ archiveNestedDepth ? `Archive nested depth ${archiveNestedDepth}` : "",
2935
+ archiveContainerPath ? `Archive container ${archiveContainerPath}` : "",
2936
+ archiveRootName ? `Archive root ${archiveRootName}` : "",
2937
+ speaker ? `Speaker ${speaker}` : "",
2938
+ from ? `Sender ${from}` : "",
2939
+ sentAt ? `Sent ${sentAt}` : ""
2940
+ ].filter((value) => value.length > 0);
2941
+ return labels.length > 0 ? labels.join(" · ") : undefined;
2942
+ };
2943
+ var buildRAGSourceLabels = ({
2944
+ metadata,
2945
+ source,
2946
+ title
2947
+ }) => {
2948
+ const contextLabel = buildContextLabel2(metadata);
2949
+ const locatorLabel = buildLocatorLabel2(metadata, source, title);
2950
+ const provenanceLabel = buildProvenanceLabel2(metadata);
2951
+ if (!contextLabel && !locatorLabel && !provenanceLabel) {
2952
+ return;
2953
+ }
2954
+ return {
2955
+ contextLabel,
2956
+ locatorLabel,
2957
+ provenanceLabel
2958
+ };
2959
+ };
2960
+ var buildRAGChunkStructure = (metadata) => {
2961
+ if (!metadata) {
2962
+ return;
2963
+ }
2964
+ const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.filter((value) => typeof value === "string" && value.trim().length > 0) : undefined;
2965
+ const sectionKind = metadata.sectionKind === "markdown_heading" || metadata.sectionKind === "html_heading" || metadata.sectionKind === "office_heading" || metadata.sectionKind === "office_block" || metadata.sectionKind === "pdf_block" || metadata.sectionKind === "spreadsheet_rows" || metadata.sectionKind === "presentation_slide" ? metadata.sectionKind : undefined;
2966
+ const section = {
2967
+ depth: getContextNumber2(metadata.sectionDepth),
2968
+ kind: sectionKind,
2969
+ path: sectionPath && sectionPath.length > 0 ? sectionPath : undefined,
2970
+ title: getContextString2(metadata.sectionTitle)
2971
+ };
2972
+ const sequence = {
2973
+ nextChunkId: getContextString2(metadata.nextChunkId),
2974
+ previousChunkId: getContextString2(metadata.previousChunkId),
2975
+ sectionChunkCount: getContextNumber2(metadata.sectionChunkCount),
2976
+ sectionChunkId: getContextString2(metadata.sectionChunkId),
2977
+ sectionChunkIndex: getContextNumber2(metadata.sectionChunkIndex)
2978
+ };
2979
+ 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") {
2980
+ return;
2981
+ }
2982
+ return {
2983
+ section: section.title || section.path && section.path.length > 0 || typeof section.depth === "number" || section.kind ? section : undefined,
2984
+ sequence: sequence.nextChunkId || sequence.previousChunkId || typeof sequence.sectionChunkCount === "number" || sequence.sectionChunkId || typeof sequence.sectionChunkIndex === "number" ? sequence : undefined
2985
+ };
2986
+ };
2987
+ var buildExcerpt2 = (text, maxLength = 160) => {
2988
+ const normalized = text.replaceAll(/\s+/g, " ").trim();
2989
+ if (normalized.length <= maxLength) {
2990
+ return normalized;
2991
+ }
2992
+ return `${normalized.slice(0, Math.max(0, maxLength - 1)).trimEnd()}…`;
2993
+ };
2994
+ var buildRAGChunkExcerpts = (chunks, activeChunkId) => {
2995
+ if (chunks.length === 0) {
2996
+ return;
2997
+ }
2998
+ const graph = buildRAGChunkGraph(chunks.map((chunk) => ({
2999
+ chunkId: chunk.chunkId,
3000
+ metadata: chunk.metadata,
3001
+ structure: chunk.structure
3002
+ })));
3003
+ const navigation = buildRAGChunkGraphNavigation(graph, activeChunkId);
3004
+ const activeChunk = chunks.find((chunk) => chunk.chunkId === navigation.activeChunkId) ?? chunks[0];
3005
+ if (!activeChunk) {
3006
+ return;
3007
+ }
3008
+ const chunkMap = new Map(chunks.map((chunk) => [chunk.chunkId, chunk]));
3009
+ const orderedSectionIds = navigation.sectionNodes.length > 0 ? navigation.sectionNodes.map((node) => node.chunkId) : [activeChunk.chunkId];
3010
+ const orderedWindowIds = navigation.sectionNodes.length > 0 ? (() => {
3011
+ const activeIndex = navigation.sectionNodes.findIndex((node) => node.chunkId === activeChunk.chunkId);
3012
+ const startIndex = Math.max(0, activeIndex - 2);
3013
+ const endIndex = Math.min(navigation.sectionNodes.length, activeIndex + 3);
3014
+ return navigation.sectionNodes.slice(startIndex, endIndex).map((node) => node.chunkId);
3015
+ })() : [
3016
+ navigation.previousNode?.chunkId,
3017
+ activeChunk.chunkId,
3018
+ navigation.nextNode?.chunkId
3019
+ ].filter((chunkId, index, ids) => Boolean(chunkId) && ids.indexOf(chunkId) === index);
3020
+ const collectText = (chunkIds) => chunkIds.map((chunkId) => chunkMap.get(chunkId)?.text).filter((text) => typeof text === "string").join(`
3021
+
3022
+ `);
3023
+ return {
3024
+ chunkExcerpt: buildExcerpt2(activeChunk.text, 160),
3025
+ sectionExcerpt: buildExcerpt2(collectText(orderedSectionIds), 320),
3026
+ windowExcerpt: buildExcerpt2(collectText(orderedWindowIds), 240)
3027
+ };
3028
+ };
3029
+ var buildRAGExcerptSelection = (excerpts, structure) => {
3030
+ if (!excerpts) {
3031
+ return {
3032
+ excerpt: "",
3033
+ mode: "chunk",
3034
+ reason: "single_chunk"
3035
+ };
3036
+ }
3037
+ const chunkLength = excerpts.chunkExcerpt.trim().length;
3038
+ const sectionChunkCount = structure?.sequence?.sectionChunkCount ?? 1;
3039
+ if (sectionChunkCount > 1 && chunkLength > 0 && chunkLength < 72) {
3040
+ if (sectionChunkCount <= 3 && excerpts.sectionExcerpt.trim().length > 0) {
3041
+ return {
3042
+ excerpt: excerpts.sectionExcerpt,
3043
+ mode: "section",
3044
+ reason: "section_small_enough"
3045
+ };
3046
+ }
3047
+ if (excerpts.windowExcerpt.trim().length > 0) {
3048
+ return {
3049
+ excerpt: excerpts.windowExcerpt,
3050
+ mode: "window",
3051
+ reason: "section_too_large_use_window"
3052
+ };
3053
+ }
3054
+ return {
3055
+ excerpt: excerpts.chunkExcerpt,
3056
+ mode: "chunk",
3057
+ reason: "chunk_too_narrow"
3058
+ };
3059
+ }
3060
+ return {
3061
+ excerpt: excerpts.chunkExcerpt,
3062
+ mode: "chunk",
3063
+ reason: sectionChunkCount > 1 ? "chunk_too_narrow" : "single_chunk"
3064
+ };
3065
+ };
3066
+ var buildRAGExcerptModeCounts = (selections) => selections.reduce((counts, selection) => {
3067
+ if (selection) {
3068
+ counts[selection.mode] += 1;
3069
+ }
3070
+ return counts;
3071
+ }, { chunk: 0, section: 0, window: 0 });
3072
+ var buildRAGChunkGraph = (chunks) => {
3073
+ const nodes = [];
3074
+ const edges = [];
3075
+ const edgeKeys = new Set;
3076
+ const sections = new Map;
3077
+ for (const chunk of chunks) {
3078
+ const labels = chunk.labels ?? buildRAGSourceLabels({
3079
+ metadata: chunk.metadata,
3080
+ source: chunk.source,
3081
+ title: chunk.title
3082
+ });
3083
+ const structure = chunk.structure ?? buildRAGChunkStructure(chunk.metadata);
3084
+ nodes.push({
3085
+ chunkId: chunk.chunkId,
3086
+ contextLabel: labels?.contextLabel,
3087
+ label: chunk.source ?? chunk.title ?? chunk.chunkId,
3088
+ locatorLabel: labels?.locatorLabel,
3089
+ provenanceLabel: labels?.provenanceLabel,
3090
+ score: chunk.score,
3091
+ source: chunk.source,
3092
+ structure,
3093
+ title: chunk.title
3094
+ });
3095
+ const previousChunkId = structure?.sequence?.previousChunkId;
3096
+ if (previousChunkId) {
3097
+ const key = `previous:${previousChunkId}:${chunk.chunkId}`;
3098
+ if (!edgeKeys.has(key)) {
3099
+ edgeKeys.add(key);
3100
+ edges.push({
3101
+ fromChunkId: previousChunkId,
3102
+ relation: "previous",
3103
+ toChunkId: chunk.chunkId
3104
+ });
3105
+ }
3106
+ }
3107
+ const nextChunkId = structure?.sequence?.nextChunkId;
3108
+ if (nextChunkId) {
3109
+ const key = `next:${chunk.chunkId}:${nextChunkId}`;
3110
+ if (!edgeKeys.has(key)) {
3111
+ edgeKeys.add(key);
3112
+ edges.push({
3113
+ fromChunkId: chunk.chunkId,
3114
+ relation: "next",
3115
+ toChunkId: nextChunkId
3116
+ });
3117
+ }
3118
+ }
3119
+ const sectionId = structure?.sequence?.sectionChunkId;
3120
+ if (sectionId) {
3121
+ const existing = sections.get(sectionId);
3122
+ if (!existing) {
3123
+ sections.set(sectionId, {
3124
+ childSectionIds: [],
3125
+ chunkCount: structure.sequence?.sectionChunkCount ?? 1,
3126
+ chunkIds: [chunk.chunkId],
3127
+ depth: structure.section?.depth,
3128
+ id: sectionId,
3129
+ kind: structure.section?.kind,
3130
+ leadChunkId: chunk.chunkId,
3131
+ path: structure.section?.path,
3132
+ title: structure.section?.title
3133
+ });
3134
+ continue;
3135
+ }
3136
+ if (!existing.chunkIds.includes(chunk.chunkId)) {
3137
+ existing.chunkIds.push(chunk.chunkId);
3138
+ }
3139
+ existing.chunkCount = Math.max(existing.chunkCount, structure.sequence?.sectionChunkCount ?? existing.chunkCount);
3140
+ }
3141
+ }
3142
+ for (const section of sections.values()) {
3143
+ section.chunkIds.sort((left, right) => {
3144
+ const leftNode = nodes.find((node) => node.chunkId === left);
3145
+ const rightNode = nodes.find((node) => node.chunkId === right);
3146
+ const leftIndex = leftNode?.structure?.sequence?.sectionChunkIndex ?? Number.MAX_SAFE_INTEGER;
3147
+ const rightIndex = rightNode?.structure?.sequence?.sectionChunkIndex ?? Number.MAX_SAFE_INTEGER;
3148
+ if (leftIndex !== rightIndex) {
3149
+ return leftIndex - rightIndex;
3150
+ }
3151
+ return left.localeCompare(right);
3152
+ });
3153
+ section.leadChunkId = section.chunkIds[0];
3154
+ }
3155
+ const sectionPathIndex = new Map;
3156
+ for (const section of sections.values()) {
3157
+ const path = section.path && section.path.length > 0 ? section.path : section.title ? [section.title] : undefined;
3158
+ if (path && path.length > 0) {
3159
+ sectionPathIndex.set(path.join("\x00"), section);
3160
+ }
3161
+ }
3162
+ for (const section of sections.values()) {
3163
+ const path = section.path && section.path.length > 0 ? section.path : section.title ? [section.title] : undefined;
3164
+ if (!path || path.length < 2) {
3165
+ continue;
3166
+ }
3167
+ const parent = sectionPathIndex.get(path.slice(0, -1).join("\x00"));
3168
+ if (!parent || parent.id === section.id) {
3169
+ continue;
3170
+ }
3171
+ section.parentSectionId = parent.id;
3172
+ if (!parent.childSectionIds.includes(section.id)) {
3173
+ parent.childSectionIds.push(section.id);
3174
+ }
3175
+ if (parent.leadChunkId && section.leadChunkId) {
3176
+ const parentKey = `section_parent:${section.leadChunkId}:${parent.leadChunkId}`;
3177
+ if (!edgeKeys.has(parentKey)) {
3178
+ edgeKeys.add(parentKey);
3179
+ edges.push({
3180
+ fromChunkId: section.leadChunkId,
3181
+ relation: "section_parent",
3182
+ toChunkId: parent.leadChunkId
3183
+ });
3184
+ }
3185
+ const childKey = `section_child:${parent.leadChunkId}:${section.leadChunkId}`;
3186
+ if (!edgeKeys.has(childKey)) {
3187
+ edgeKeys.add(childKey);
3188
+ edges.push({
3189
+ fromChunkId: parent.leadChunkId,
3190
+ relation: "section_child",
3191
+ toChunkId: section.leadChunkId
3192
+ });
3193
+ }
3194
+ }
3195
+ }
3196
+ nodes.sort((left, right) => {
3197
+ const leftSection = left.structure?.sequence?.sectionChunkIndex ?? Number.MAX_SAFE_INTEGER;
3198
+ const rightSection = right.structure?.sequence?.sectionChunkIndex ?? Number.MAX_SAFE_INTEGER;
3199
+ if (leftSection !== rightSection) {
3200
+ return leftSection - rightSection;
3201
+ }
3202
+ const leftScore = left.score ?? Number.NEGATIVE_INFINITY;
3203
+ const rightScore = right.score ?? Number.NEGATIVE_INFINITY;
3204
+ if (leftScore !== rightScore) {
3205
+ return rightScore - leftScore;
3206
+ }
3207
+ return left.label.localeCompare(right.label);
3208
+ });
3209
+ return {
3210
+ edges,
3211
+ nodes,
3212
+ sections: [...sections.values()].sort((left, right) => (left.title ?? left.id).localeCompare(right.title ?? right.id))
3213
+ };
3214
+ };
3215
+ var buildRAGChunkPreviewGraph = (preview) => buildRAGChunkGraph(preview.chunks.map((chunk) => ({
3216
+ chunkId: chunk.chunkId,
3217
+ labels: chunk.labels,
3218
+ metadata: chunk.metadata,
3219
+ source: chunk.source ?? preview.document.source,
3220
+ structure: chunk.structure,
3221
+ title: chunk.title ?? preview.document.title
3222
+ })));
3223
+ var buildRAGChunkPreviewNavigation = (preview, activeChunkId) => buildRAGChunkGraphNavigation(buildRAGChunkPreviewGraph(preview), activeChunkId);
3224
+ var buildRAGChunkGraphNavigation = (graph, activeChunkId) => {
3225
+ if (graph.nodes.length === 0) {
3226
+ return {
3227
+ activeChunkId,
3228
+ childSections: [],
3229
+ siblingSections: [],
3230
+ sectionNodes: []
3231
+ };
3232
+ }
3233
+ const activeNode = (activeChunkId ? graph.nodes.find((node) => node.chunkId === activeChunkId) : undefined) ?? graph.nodes[0];
3234
+ const resolvedActiveChunkId = activeNode?.chunkId;
3235
+ const previousNode = activeNode?.structure?.sequence?.previousChunkId ? graph.nodes.find((node) => node.chunkId === activeNode.structure?.sequence?.previousChunkId) : undefined;
3236
+ const nextNode = activeNode?.structure?.sequence?.nextChunkId ? graph.nodes.find((node) => node.chunkId === activeNode.structure?.sequence?.nextChunkId) : undefined;
3237
+ const section = activeNode?.structure?.sequence?.sectionChunkId ? graph.sections.find((entry) => entry.id === activeNode.structure?.sequence?.sectionChunkId) : undefined;
3238
+ const parentSection = section?.parentSectionId ? graph.sections.find((entry) => entry.id === section.parentSectionId) : undefined;
3239
+ const childSections = section ? section.childSectionIds.map((sectionId) => graph.sections.find((entry) => entry.id === sectionId)).filter((entry) => Boolean(entry)) : [];
3240
+ const siblingSections = section?.parentSectionId ? graph.sections.filter((entry) => entry.parentSectionId === section.parentSectionId && entry.id !== section.id) : [];
3241
+ const sectionNodes = section ? section.chunkIds.map((chunkId) => graph.nodes.find((node) => node.chunkId === chunkId)).filter((node) => Boolean(node)) : activeNode ? [activeNode] : [];
3242
+ return {
3243
+ activeChunkId: resolvedActiveChunkId,
3244
+ activeNode,
3245
+ childSections,
3246
+ nextNode,
3247
+ parentSection,
3248
+ previousNode,
3249
+ section,
3250
+ siblingSections,
3251
+ sectionNodes
3252
+ };
3253
+ };
3254
+ var buildRAGRetrievedState = (messages) => {
3255
+ const message = getLatestRetrievedMessage(messages);
3256
+ if (!message) {
3257
+ return null;
3258
+ }
3259
+ const sources = message.sources ?? [];
3260
+ const citations = buildRAGCitations(sources);
3261
+ const sectionDiagnostics = buildRAGSectionRetrievalDiagnostics(sources, isRAGRetrievalTrace(message.retrievalTrace) ? message.retrievalTrace : undefined);
3262
+ const sourceSummaries = buildRAGSourceSummaries(sources);
3263
+ const groundedAnswer = buildRAGGroundedAnswer(message.content, sources);
3264
+ return {
3265
+ citationReferenceMap: buildRAGCitationReferenceMap(citations),
3266
+ citations,
3267
+ conversationId: message.conversationId,
3268
+ excerptModeCounts: buildRAGExcerptModeCounts([
3269
+ ...citations.map((citation) => citation.excerptSelection),
3270
+ ...sourceSummaries.map((summary) => summary.excerptSelection)
3271
+ ]),
3272
+ groundedAnswer,
3273
+ messageId: message.id,
3274
+ retrievalDurationMs: message.retrievalDurationMs,
3275
+ retrievalStartedAt: message.retrievalStartedAt,
3276
+ retrievedAt: message.retrievedAt,
3277
+ trace: isRAGRetrievalTrace(message.retrievalTrace) ? message.retrievalTrace : undefined,
3278
+ sectionDiagnostics,
3279
+ sourceGroups: buildRAGSourceGroups(sources),
3280
+ sourceSummaries,
3281
+ sources
3282
+ };
3283
+ };
3284
+ var buildRAGSourceSummaries = (sources) => {
3285
+ const sourceGroups = buildRAGSourceGroups(sources);
3286
+ const citations = buildRAGCitations(sources);
3287
+ const citationReferenceMap = buildRAGCitationReferenceMap(citations);
3288
+ return sourceGroups.map((group) => {
3289
+ const groupCitations = citations.filter((citation) => group.chunks.some((chunk) => chunk.chunkId === citation.chunkId));
3290
+ const leadChunk = getPreferredSourceLeadChunk(group.chunks);
3291
+ const excerpts = leadChunk ? buildRAGChunkExcerpts(group.chunks, leadChunk.chunkId) : undefined;
3292
+ const structure = leadChunk?.structure ?? buildRAGChunkStructure(leadChunk?.metadata);
3293
+ const excerptSelection = buildRAGExcerptSelection(excerpts, structure);
3294
+ return {
3295
+ bestScore: group.bestScore,
3296
+ citationNumbers: groupCitations.map((citation) => citationReferenceMap[citation.chunkId] ?? 0),
3297
+ citations: groupCitations,
3298
+ chunkIds: group.chunks.map((chunk) => chunk.chunkId),
3299
+ contextLabel: leadChunk?.labels?.contextLabel ?? buildContextLabel2(leadChunk?.metadata),
3300
+ count: group.count,
3301
+ excerpt: excerptSelection.excerpt || buildExcerpt2(leadChunk?.text ?? ""),
3302
+ excerpts,
3303
+ excerptSelection,
3304
+ key: group.key,
3305
+ label: group.label,
3306
+ locatorLabel: leadChunk?.labels?.locatorLabel ?? buildLocatorLabel2(leadChunk?.metadata, leadChunk?.source, leadChunk?.title),
3307
+ provenanceLabel: leadChunk?.labels?.provenanceLabel ?? buildProvenanceLabel2(leadChunk?.metadata),
3308
+ structure,
3309
+ source: group.source,
3310
+ title: group.title
3311
+ };
3312
+ });
3313
+ };
3314
+ var getSectionPathFromSource = (source) => {
3315
+ const path = source.structure?.section?.path ?? (Array.isArray(source.metadata?.sectionPath) ? source.metadata.sectionPath.map((value) => getContextString2(value)).filter((value) => typeof value === "string") : []);
3316
+ return path.length > 0 ? path : undefined;
3317
+ };
3318
+ var isBlockAwareContextLabel = (value) => typeof value === "string" && (value.startsWith("PDF ") || value.startsWith("Office ") || value.startsWith("Slide "));
3319
+ var getStructuredSectionScoreWeight = (metadata) => {
3320
+ if (!metadata) {
3321
+ return 1;
3322
+ }
3323
+ const pdfTextKind = getContextString2(metadata.pdfTextKind);
3324
+ const officeBlockKind = getContextString2(metadata.officeBlockKind);
3325
+ const sectionKind = getContextString2(metadata.sectionKind);
3326
+ const slideTitle = getContextString2(metadata.slideTitle);
3327
+ const slideNotesText = getContextString2(metadata.slideNotesText);
3328
+ if (pdfTextKind === "table_like") {
3329
+ return 1.28;
3330
+ }
3331
+ if (officeBlockKind === "table" || officeBlockKind === "list") {
3332
+ return 1.24;
3333
+ }
3334
+ if (sectionKind === "pdf_block" || sectionKind === "office_block" || officeBlockKind === "paragraph" || pdfTextKind === "paragraph") {
3335
+ return 1.12;
3336
+ }
3337
+ if (sectionKind === "presentation_slide" && slideNotesText) {
3338
+ return 1.2;
3339
+ }
3340
+ if (sectionKind === "presentation_slide" && slideTitle) {
3341
+ return 1.14;
3342
+ }
3343
+ return 1;
3344
+ };
3345
+ var getStructuredSourceLeadScore = (source) => source.score * getStructuredSectionScoreWeight(source.metadata);
3346
+ var getPDFLeadEvidencePreference = (metadata) => {
3347
+ if (!metadata) {
3348
+ return 0;
3349
+ }
3350
+ const pdfEvidenceMode = getContextString2(metadata.pdfEvidenceMode);
3351
+ const pdfEvidenceOrigin = getContextString2(metadata.pdfEvidenceOrigin);
3352
+ const pdfEvidenceSupplement = getContextString2(metadata.pdfEvidenceSupplement);
3353
+ if (pdfEvidenceMode === "hybrid" && pdfEvidenceOrigin === "native" && pdfEvidenceSupplement === "ocr") {
3354
+ return 3;
3355
+ }
3356
+ if (pdfEvidenceMode === "native" && pdfEvidenceOrigin === "native") {
3357
+ return 2;
3358
+ }
3359
+ if (pdfEvidenceMode === "ocr" && pdfEvidenceOrigin === "ocr") {
3360
+ return 1;
3361
+ }
3362
+ return 0;
3363
+ };
3364
+ var getPDFLeadScope = (metadata) => {
3365
+ if (!metadata) {
3366
+ return;
3367
+ }
3368
+ const pageNumber = getContextNumber2(metadata.pageNumber) ?? getContextNumber2(metadata.page) ?? (typeof metadata.pageIndex === "number" ? metadata.pageIndex + 1 : undefined);
3369
+ const sectionTitle = getContextString2(metadata.sectionTitle);
3370
+ const sourceNativeKind = getContextString2(metadata.sourceNativeKind);
3371
+ if (typeof pageNumber !== "number" && !sectionTitle && !sourceNativeKind) {
3372
+ return;
3373
+ }
3374
+ return {
3375
+ pageNumber,
3376
+ sectionTitle,
3377
+ sourceNativeKind
3378
+ };
3379
+ };
3380
+ var getOfficeLeadScope = (metadata) => {
3381
+ if (!metadata) {
3382
+ return;
3383
+ }
3384
+ const officeBlockKindValue = getContextString2(metadata.officeBlockKind);
3385
+ const officeBlockKind = officeBlockKindValue === "table" || officeBlockKindValue === "list" || officeBlockKindValue === "paragraph" ? officeBlockKindValue : undefined;
3386
+ if (officeBlockKind !== "table" && officeBlockKind !== "list" && officeBlockKind !== "paragraph") {
3387
+ return;
3388
+ }
3389
+ const sectionPath = Array.isArray(metadata.sectionPath) ? metadata.sectionPath.map((value) => getContextString2(value)).filter((value) => typeof value === "string") : [];
3390
+ const sectionTitle = getContextString2(metadata.sectionTitle) ?? sectionPath.at(-1);
3391
+ const officeContextText = officeBlockKind === "table" ? getContextString2(metadata.officeTableContextText) : officeBlockKind === "list" ? getContextString2(metadata.officeListContextText) : undefined;
3392
+ if (!sectionTitle) {
3393
+ return;
3394
+ }
3395
+ return {
3396
+ blockKind: officeBlockKind,
3397
+ familyPath: (() => {
3398
+ const explicitGenericFamilyPath = Array.isArray(metadata.sectionFamilyPath) ? metadata.sectionFamilyPath.map((value) => getContextString2(value)).filter((value) => typeof value === "string") : [];
3399
+ const explicitGenericOrdinalPath = Array.isArray(metadata.sectionOrdinalPath) ? metadata.sectionOrdinalPath.map((value) => getContextNumber2(value)).filter((value) => typeof value === "number") : [];
3400
+ if (explicitGenericFamilyPath.length > 0 && explicitGenericFamilyPath.length === explicitGenericOrdinalPath.length) {
3401
+ return explicitGenericFamilyPath;
3402
+ }
3403
+ const explicitOfficeFamilyPath = Array.isArray(metadata.officeFamilyPath) ? metadata.officeFamilyPath.map((value) => getContextString2(value)).filter((value) => typeof value === "string") : [];
3404
+ return explicitOfficeFamilyPath.length > 0 ? explicitOfficeFamilyPath : sectionPath.map((value) => value.replace(/\s+\((\d+)\)$/, "").trim());
3405
+ })(),
3406
+ pathDepth: sectionPath.length,
3407
+ ordinalPath: (() => {
3408
+ const explicitGenericFamilyPath = Array.isArray(metadata.sectionFamilyPath) ? metadata.sectionFamilyPath.map((value) => getContextString2(value)).filter((value) => typeof value === "string") : [];
3409
+ const explicitGenericOrdinalPath = Array.isArray(metadata.sectionOrdinalPath) ? metadata.sectionOrdinalPath.map((value) => getContextNumber2(value)).filter((value) => typeof value === "number") : [];
3410
+ if (explicitGenericFamilyPath.length > 0 && explicitGenericFamilyPath.length === explicitGenericOrdinalPath.length) {
3411
+ return explicitGenericOrdinalPath;
3412
+ }
3413
+ const explicitOfficeOrdinalPath = Array.isArray(metadata.officeOrdinalPath) ? metadata.officeOrdinalPath.map((value) => typeof value === "number" && Number.isFinite(value) ? value : undefined).filter((value) => typeof value === "number") : [];
3414
+ return explicitOfficeOrdinalPath.length > 0 ? explicitOfficeOrdinalPath : sectionPath.map((value) => {
3415
+ const match = value.match(/\((\d+)\)$/);
3416
+ return match ? Number.parseInt(match[1] ?? "1", 10) : 1;
3417
+ });
3418
+ })(),
3419
+ sectionFamilyKey: getContextString2(metadata.sectionSiblingFamilyKey) ?? getContextString2(metadata.officeSiblingFamilyKey) ?? sectionPath.at(-1)?.replace(/\s+\((\d+)\)$/, "").trim() ?? sectionTitle,
3420
+ sectionOrdinal: getContextNumber2(metadata.sectionSiblingOrdinal) ?? getContextNumber2(metadata.officeSiblingOrdinal) ?? (() => {
3421
+ const match = sectionTitle.match(/\((\d+)\)$/);
3422
+ return match ? Number.parseInt(match[1] ?? "1", 10) : 1;
3423
+ })(),
3424
+ sectionTitle,
3425
+ hasContext: typeof officeContextText === "string"
3426
+ };
3427
+ };
3428
+ var areOfficeLeadScopesComparable = (left, right) => {
3429
+ if (!left || !right) {
3430
+ return false;
3431
+ }
3432
+ if (left.blockKind !== right.blockKind || left.sectionFamilyKey !== right.sectionFamilyKey || left.sectionOrdinal !== right.sectionOrdinal) {
3433
+ return false;
3434
+ }
3435
+ const leftAncestorFamilyPath = left.familyPath.slice(0, -1);
3436
+ const rightAncestorFamilyPath = right.familyPath.slice(0, -1);
3437
+ const leftAncestorOrdinalPath = left.ordinalPath.slice(0, -1);
3438
+ const rightAncestorOrdinalPath = right.ordinalPath.slice(0, -1);
3439
+ const sharedDepth = Math.min(leftAncestorFamilyPath.length, rightAncestorFamilyPath.length);
3440
+ for (let index = 0;index < sharedDepth; index += 1) {
3441
+ if (leftAncestorFamilyPath[index] !== rightAncestorFamilyPath[index] || leftAncestorOrdinalPath[index] !== rightAncestorOrdinalPath[index]) {
3442
+ return false;
3443
+ }
3444
+ }
3445
+ return true;
3446
+ };
3447
+ var getGenericStructuredLeadScope = (metadata) => {
3448
+ if (!metadata || metadata.officeBlockKind || metadata.pageNumber) {
3449
+ return;
3450
+ }
3451
+ const kind = metadata.sectionKind === "spreadsheet_rows" || metadata.sectionKind === "presentation_slide" ? metadata.sectionKind : undefined;
3452
+ if (!kind) {
3453
+ return;
3454
+ }
3455
+ const explicitFamilyPath = Array.isArray(metadata.sectionFamilyPath) ? metadata.sectionFamilyPath.map((value) => getContextString2(value)).filter((value) => typeof value === "string") : [];
3456
+ const explicitOrdinalPath = Array.isArray(metadata.sectionOrdinalPath) ? metadata.sectionOrdinalPath.map((value) => getContextNumber2(value)).filter((value) => typeof value === "number") : [];
3457
+ let familyPath = explicitFamilyPath.length > 0 && explicitFamilyPath.length === explicitOrdinalPath.length ? explicitFamilyPath : [];
3458
+ let ordinalPath = explicitFamilyPath.length > 0 && explicitFamilyPath.length === explicitOrdinalPath.length ? explicitOrdinalPath : [];
3459
+ if (familyPath.length === 0) {
3460
+ if (kind === "spreadsheet_rows") {
3461
+ const sheetName = getContextString2(metadata.sheetName) ?? "Sheet";
3462
+ const tableIndex = getContextNumber2(metadata.spreadsheetTableIndex) ?? 1;
3463
+ familyPath = [sheetName, "Spreadsheet Table"];
3464
+ ordinalPath = [1, tableIndex];
3465
+ } else {
3466
+ const slideFamily = getContextString2(metadata.slideTitle) ?? "Slide";
3467
+ const slideOrdinal = getContextNumber2(metadata.slideNumber) ?? (typeof metadata.slideIndex === "number" ? metadata.slideIndex + 1 : 1);
3468
+ familyPath = [slideFamily];
3469
+ ordinalPath = [slideOrdinal];
3470
+ }
3471
+ }
3472
+ const sectionFamilyKey = getContextString2(metadata.sectionSiblingFamilyKey) ?? familyPath.at(-1);
3473
+ const sectionOrdinal = getContextNumber2(metadata.sectionSiblingOrdinal) ?? ordinalPath.at(-1);
3474
+ if (!sectionFamilyKey || typeof sectionOrdinal !== "number") {
3475
+ return;
3476
+ }
3477
+ return {
3478
+ familyPath,
3479
+ kind,
3480
+ ordinalPath,
3481
+ pathDepth: familyPath.length,
3482
+ sectionFamilyKey,
3483
+ sectionOrdinal
3484
+ };
3485
+ };
3486
+ var areGenericStructuredLeadScopesComparable = (left, right) => {
3487
+ if (!left || !right) {
3488
+ return false;
3489
+ }
3490
+ if (left.kind !== right.kind || left.sectionFamilyKey !== right.sectionFamilyKey || left.sectionOrdinal !== right.sectionOrdinal) {
3491
+ return false;
3492
+ }
3493
+ const leftAncestorFamilyPath = left.familyPath.slice(0, -1);
3494
+ const rightAncestorFamilyPath = right.familyPath.slice(0, -1);
3495
+ const leftAncestorOrdinalPath = left.ordinalPath.slice(0, -1);
3496
+ const rightAncestorOrdinalPath = right.ordinalPath.slice(0, -1);
3497
+ const sharedDepth = Math.min(leftAncestorFamilyPath.length, rightAncestorFamilyPath.length);
3498
+ for (let index = 0;index < sharedDepth; index += 1) {
3499
+ if (leftAncestorFamilyPath[index] !== rightAncestorFamilyPath[index] || leftAncestorOrdinalPath[index] !== rightAncestorOrdinalPath[index]) {
3500
+ return false;
3501
+ }
3502
+ }
3503
+ return true;
3504
+ };
3505
+ var getOfficeLeadEvidencePreference = (metadata) => {
3506
+ const scope = getOfficeLeadScope(metadata);
3507
+ if (!scope) {
3508
+ return 0;
3509
+ }
3510
+ return scope.pathDepth * 10 + (scope.hasContext ? 1 : 0) + (scope.blockKind === "list" && typeof metadata?.officeListGroupItemCount === "number" && metadata.officeListGroupItemCount > 1 ? 1 : 0);
3511
+ };
3512
+ var getGenericStructuredLeadPreference = (metadata) => {
3513
+ const scope = getGenericStructuredLeadScope(metadata);
3514
+ if (!scope) {
3515
+ return 0;
3516
+ }
3517
+ return scope.pathDepth * 10 + (scope.kind === "spreadsheet_rows" && typeof metadata?.spreadsheetTableIndex === "number" ? 2 : 0) + (Array.isArray(metadata?.spreadsheetHeaders) && metadata.spreadsheetHeaders.length > 0 ? 1 : 0) + (typeof metadata?.slideNotesText === "string" && metadata.slideNotesText.trim().length > 0 ? 1 : 0);
3518
+ };
3519
+ var getPreferredSourceLeadChunk = (chunks) => chunks.slice().sort((left, right) => {
3520
+ const leftOfficeScope = getOfficeLeadScope(left.metadata);
3521
+ const rightOfficeScope = getOfficeLeadScope(right.metadata);
3522
+ if (left.source === right.source && areOfficeLeadScopesComparable(leftOfficeScope, rightOfficeScope)) {
3523
+ const leftOfficePreference = getOfficeLeadEvidencePreference(left.metadata);
3524
+ const rightOfficePreference = getOfficeLeadEvidencePreference(right.metadata);
3525
+ if (rightOfficePreference !== leftOfficePreference) {
3526
+ return rightOfficePreference - leftOfficePreference;
3527
+ }
3528
+ }
3529
+ const leftGenericScope = getGenericStructuredLeadScope(left.metadata);
3530
+ const rightGenericScope = getGenericStructuredLeadScope(right.metadata);
3531
+ if (left.source === right.source && areGenericStructuredLeadScopesComparable(leftGenericScope, rightGenericScope)) {
3532
+ const leftGenericPreference = getGenericStructuredLeadPreference(left.metadata);
3533
+ const rightGenericPreference = getGenericStructuredLeadPreference(right.metadata);
3534
+ if (rightGenericPreference !== leftGenericPreference) {
3535
+ return rightGenericPreference - leftGenericPreference;
3536
+ }
3537
+ }
3538
+ const leftWeightedScore = getStructuredSourceLeadScore(left);
3539
+ const rightWeightedScore = getStructuredSourceLeadScore(right);
3540
+ if (rightWeightedScore !== leftWeightedScore) {
3541
+ return rightWeightedScore - leftWeightedScore;
3542
+ }
3543
+ const leftScope = getPDFLeadScope(left.metadata);
3544
+ const rightScope = getPDFLeadScope(right.metadata);
3545
+ if (left.source === right.source && leftScope && rightScope && (leftScope.sectionTitle && rightScope.sectionTitle && leftScope.sectionTitle === rightScope.sectionTitle || typeof leftScope.pageNumber === "number" && typeof rightScope.pageNumber === "number" && leftScope.pageNumber === rightScope.pageNumber)) {
3546
+ const leftEvidencePreference = getPDFLeadEvidencePreference(left.metadata);
3547
+ const rightEvidencePreference = getPDFLeadEvidencePreference(right.metadata);
3548
+ if (rightEvidencePreference !== leftEvidencePreference) {
3549
+ return rightEvidencePreference - leftEvidencePreference;
3550
+ }
3551
+ }
3552
+ if (right.score !== left.score) {
3553
+ return right.score - left.score;
3554
+ }
3555
+ return left.chunkId.localeCompare(right.chunkId);
3556
+ })[0];
3557
+ var buildRAGSectionRetrievalDiagnostics = (sources, trace) => {
3558
+ const totalScore = sources.reduce((sum, source) => sum + source.score * getStructuredSectionScoreWeight(source.metadata), 0);
3559
+ if (sources.length === 0 || totalScore <= 0) {
3560
+ return [];
3561
+ }
3562
+ const sections = new Map;
3563
+ for (const source of sources) {
3564
+ const structuredScore = source.score * getStructuredSectionScoreWeight(source.metadata);
3565
+ const path = getSectionPathFromSource(source);
3566
+ if (!path) {
3567
+ continue;
3568
+ }
3569
+ const key = path.join(" > ");
3570
+ const label = path.at(-1) ?? key;
3571
+ const parentLabel = path.length > 1 ? path.slice(0, -1).join(" > ") : undefined;
3572
+ const existing = sections.get(key);
3573
+ const channels = Array.isArray(source.metadata?.retrievalChannels) ? source.metadata.retrievalChannels.filter((value) => value === "vector" || value === "lexical") : [];
3574
+ const isHybrid = channels.includes("vector") && channels.includes("lexical");
3575
+ const vectorHits = channels.includes("vector") ? 1 : 0;
3576
+ const lexicalHits = channels.includes("lexical") ? 1 : 0;
3577
+ const hybridHits = isHybrid ? 1 : 0;
3578
+ const queryOrigins = Array.isArray(source.metadata?.retrievalQueryOrigins) ? source.metadata.retrievalQueryOrigins.filter((value) => value === "primary" || value === "transformed" || value === "variant") : source.metadata?.retrievalQueryOrigin === "primary" || source.metadata?.retrievalQueryOrigin === "transformed" || source.metadata?.retrievalQueryOrigin === "variant" ? [source.metadata.retrievalQueryOrigin] : [];
3579
+ const primaryHits = queryOrigins.includes("primary") ? 1 : 0;
3580
+ const transformedHits = queryOrigins.includes("transformed") ? 1 : 0;
3581
+ const variantHits = queryOrigins.includes("variant") ? 1 : 0;
3582
+ if (!existing) {
3583
+ sections.set(key, {
3584
+ bestScore: source.score,
3585
+ count: 1,
3586
+ hybridHits,
3587
+ key,
3588
+ label,
3589
+ lexicalHits,
3590
+ parentLabel,
3591
+ path,
3592
+ primaryHits,
3593
+ sourceSet: new Set(source.source ? [source.source] : []),
3594
+ topChunkId: source.chunkId,
3595
+ topSource: source.source,
3596
+ totalScore: structuredScore,
3597
+ transformedHits,
3598
+ variantHits,
3599
+ vectorHits
3600
+ });
3601
+ continue;
3602
+ }
3603
+ existing.count += 1;
3604
+ existing.totalScore += structuredScore;
3605
+ if (source.source) {
3606
+ existing.sourceSet.add(source.source);
3607
+ }
3608
+ existing.vectorHits += vectorHits;
3609
+ existing.lexicalHits += lexicalHits;
3610
+ existing.hybridHits += hybridHits;
3611
+ existing.primaryHits += primaryHits;
3612
+ existing.transformedHits += transformedHits;
3613
+ existing.variantHits += variantHits;
3614
+ if (source.score > existing.bestScore) {
3615
+ existing.bestScore = source.score;
3616
+ existing.topChunkId = source.chunkId;
3617
+ existing.topSource = source.source;
3618
+ }
3619
+ }
3620
+ const diagnostics = [...sections.values()];
3621
+ const strongestBestHit = diagnostics.reduce((highest, section) => Math.max(highest, section.bestScore), 0);
3622
+ const parentLabelByKey = new Map(diagnostics.map((section) => [section.key, section.parentLabel]));
3623
+ const stageSectionCounts = new Map((trace?.steps ?? []).filter((step) => Array.isArray(step.sectionCounts) && step.sectionCounts.length > 0).map((step) => [step.stage, step.sectionCounts ?? []]));
3624
+ const stageSectionScores = new Map((trace?.steps ?? []).filter((step) => Array.isArray(step.sectionScores) && step.sectionScores.length > 0).map((step) => [step.stage, step.sectionScores ?? []]));
3625
+ return diagnostics.map((section) => {
3626
+ const siblingPool = diagnostics.filter((entry) => entry.parentLabel === section.parentLabel);
3627
+ const siblings = siblingPool.filter((entry) => entry.key !== section.key);
3628
+ const strongestSibling = siblings.slice().sort((left, right) => right.totalScore - left.totalScore)[0];
3629
+ const parentTotal = siblingPool.reduce((sum, entry) => sum + entry.totalScore, 0);
3630
+ const scoreShare = section.totalScore / totalScore;
3631
+ const parentShare = parentTotal > 0 ? section.totalScore / parentTotal : undefined;
3632
+ const topChunk = sources.find((source) => source.chunkId === section.topChunkId);
3633
+ const topContextLabel = topChunk?.labels?.contextLabel ?? buildContextLabel2(topChunk?.metadata);
3634
+ const topLocatorLabel = topChunk?.labels?.locatorLabel ?? buildLocatorLabel2(topChunk?.metadata, topChunk?.source, topChunk?.title);
3635
+ const sourceAwareChunkReason = formatSourceAwareChunkReason(topChunk?.metadata?.sourceAwareChunkReason);
3636
+ const sourceAwareUnitScopeLabel = buildSourceAwareUnitScopeLabel(topChunk?.metadata);
3637
+ const parentDistribution = parentTotal > 0 ? siblingPool.map((entry) => ({
3638
+ count: entry.count,
3639
+ isActive: entry.key === section.key,
3640
+ key: entry.key,
3641
+ label: entry.label,
3642
+ parentShare: entry.totalScore / parentTotal,
3643
+ totalScore: entry.totalScore
3644
+ })).sort((left, right) => right.totalScore - left.totalScore) : [];
3645
+ const reasons = [];
3646
+ const stageCounts = trace?.steps.map((step) => ({
3647
+ count: step.sectionCounts?.find((entry) => entry.key === section.key)?.count ?? 0,
3648
+ stage: step.stage
3649
+ })).filter((entry) => entry.count > 0) ?? [];
3650
+ const stageWeights = stageCounts.map((entry) => {
3651
+ const previousStageEntry = stageCounts[stageCounts.findIndex((candidate) => candidate.stage === entry.stage) - 1];
3652
+ const stageEntries = stageSectionCounts.get(entry.stage)?.filter((candidate) => candidate.count > 0) ?? [];
3653
+ const stageScoreEntries = stageSectionScores.get(entry.stage)?.filter((candidate) => candidate.totalScore > 0) ?? [];
3654
+ const stageTotal = stageEntries.reduce((sum, candidate) => sum + candidate.count, 0);
3655
+ const stageScoreTotal = stageScoreEntries.reduce((sum, candidate) => sum + candidate.totalScore, 0);
3656
+ const siblingStageEntries = stageEntries.filter((candidate) => candidate.key !== section.key && parentLabelByKey.get(candidate.key) === section.parentLabel);
3657
+ const parentStageEntries = stageEntries.filter((candidate) => parentLabelByKey.get(candidate.key) === section.parentLabel);
3658
+ const siblingStageScoreEntries = stageScoreEntries.filter((candidate) => candidate.key !== section.key && parentLabelByKey.get(candidate.key) === section.parentLabel);
3659
+ const parentStageScoreEntries = stageScoreEntries.filter((candidate) => parentLabelByKey.get(candidate.key) === section.parentLabel);
3660
+ const strongestStageSibling = siblingStageEntries.slice().sort((left, right) => right.count - left.count)[0];
3661
+ const parentStageTotal = parentStageEntries.reduce((sum, candidate) => sum + candidate.count, 0);
3662
+ const activeStageScore = stageScoreEntries.find((candidate) => candidate.key === section.key)?.totalScore;
3663
+ const strongestStageScoreSibling = siblingStageScoreEntries.slice().sort((left, right) => right.totalScore - left.totalScore)[0];
3664
+ const parentStageScoreTotal = parentStageScoreEntries.reduce((sum, candidate) => sum + candidate.totalScore, 0);
3665
+ const stageShare = stageTotal > 0 ? entry.count / stageTotal : 0;
3666
+ const retentionRate = typeof previousStageEntry?.count === "number" && previousStageEntry.count > 0 ? entry.count / previousStageEntry.count : undefined;
3667
+ const countDelta = typeof previousStageEntry?.count === "number" ? entry.count - previousStageEntry.count : undefined;
3668
+ const parentStageShare = parentStageTotal > 0 ? entry.count / parentStageTotal : undefined;
3669
+ const stageScoreShare = typeof activeStageScore === "number" && stageScoreTotal > 0 ? activeStageScore / stageScoreTotal : undefined;
3670
+ const parentStageScoreShare = typeof activeStageScore === "number" && parentStageScoreTotal > 0 ? activeStageScore / parentStageScoreTotal : undefined;
3671
+ const stageShareGap = stageTotal > 0 && strongestStageSibling ? entry.count / stageTotal - strongestStageSibling.count / stageTotal : undefined;
3672
+ const parentStageShareGap = parentStageTotal > 0 && strongestStageSibling ? entry.count / parentStageTotal - strongestStageSibling.count / parentStageTotal : undefined;
3673
+ const stageScoreShareGap = typeof activeStageScore === "number" && stageScoreTotal > 0 && strongestStageScoreSibling ? activeStageScore / stageScoreTotal - strongestStageScoreSibling.totalScore / stageScoreTotal : undefined;
3674
+ const parentStageScoreShareGap = typeof activeStageScore === "number" && parentStageScoreTotal > 0 && strongestStageScoreSibling ? activeStageScore / parentStageScoreTotal - strongestStageScoreSibling.totalScore / parentStageScoreTotal : undefined;
3675
+ const reasons2 = [];
3676
+ if (entry.stage === "rerank" && stageShare > 0.5 && (typeof stageShareGap !== "number" || stageShareGap > 0)) {
3677
+ reasons2.push("rerank_preserved_lead");
3678
+ }
3679
+ if (entry.stage === "finalize" && stageShare >= 0.5) {
3680
+ reasons2.push("final_stage_concentration");
3681
+ }
3682
+ if (entry.stage === "finalize" && typeof parentStageShare === "number" && parentStageShare >= 0.6 && (typeof parentStageShareGap !== "number" || parentStageShareGap > 0)) {
3683
+ reasons2.push("final_stage_dominant_within_parent");
3684
+ }
3685
+ if (strongestStageSibling && (typeof stageShareGap === "number" && stageShareGap <= 0.1 || typeof parentStageShareGap === "number" && parentStageShareGap <= 0.1)) {
3686
+ reasons2.push("stage_runner_up_pressure");
3687
+ }
3688
+ if (typeof countDelta === "number") {
3689
+ if (countDelta > 0) {
3690
+ reasons2.push("stage_expanded");
3691
+ } else if (countDelta < 0) {
3692
+ reasons2.push("stage_narrowed");
3693
+ } else {
3694
+ reasons2.push("stage_held");
3695
+ }
3696
+ }
3697
+ return {
3698
+ count: entry.count,
3699
+ countDelta,
3700
+ parentStageScoreShare,
3701
+ parentStageShare,
3702
+ parentStageShareGap,
3703
+ previousCount: previousStageEntry?.count,
3704
+ previousStage: previousStageEntry?.stage,
3705
+ reasons: reasons2,
3706
+ retentionRate,
3707
+ stage: entry.stage,
3708
+ stageScoreShare,
3709
+ stageScoreShareGap,
3710
+ stageShare,
3711
+ stageShareGap,
3712
+ totalScore: activeStageScore,
3713
+ strongestSiblingCount: strongestStageSibling?.count,
3714
+ strongestSiblingLabel: strongestStageSibling ? diagnostics.find((candidate) => candidate.key === strongestStageSibling.key)?.label ?? strongestStageSibling.key : undefined
3715
+ };
3716
+ });
3717
+ const firstSeenStage = stageCounts[0]?.stage;
3718
+ const lastSeenStage = stageCounts.at(-1)?.stage;
3719
+ const peakStageEntry = stageCounts.reduce((highest, entry) => !highest || entry.count > highest.count ? entry : highest, undefined);
3720
+ const finalStageEntry = stageCounts.at(-1);
3721
+ const peakCount = peakStageEntry?.count ?? section.count;
3722
+ const finalCount = finalStageEntry?.count;
3723
+ const finalRetentionRate = typeof finalCount === "number" && peakCount > 0 ? finalCount / peakCount : undefined;
3724
+ const dropFromPeak = typeof finalCount === "number" ? peakCount - finalCount : undefined;
3725
+ const queryAttributionReasons = [];
3726
+ const queryAttributionMode = section.primaryHits > 0 && section.transformedHits === 0 && section.variantHits === 0 ? "primary" : section.transformedHits > 0 && section.primaryHits === 0 && section.variantHits === 0 ? "transformed" : section.variantHits > 0 && section.primaryHits === 0 && section.transformedHits === 0 ? "variant" : "mixed";
3727
+ if (queryAttributionMode === "primary") {
3728
+ queryAttributionReasons.push("base_query_only");
3729
+ }
3730
+ if (queryAttributionMode === "transformed") {
3731
+ queryAttributionReasons.push("transformed_query_only");
3732
+ queryAttributionReasons.push("transform_introduced");
3733
+ }
3734
+ if (queryAttributionMode === "variant") {
3735
+ queryAttributionReasons.push("variant_only");
3736
+ queryAttributionReasons.push("variant_supported");
3737
+ }
3738
+ if (queryAttributionMode === "mixed") {
3739
+ queryAttributionReasons.push("mixed_query_sources");
3740
+ if (section.variantHits > 0) {
3741
+ queryAttributionReasons.push("variant_supported");
3742
+ }
3743
+ if (section.transformedHits > 0 && section.primaryHits === 0) {
3744
+ queryAttributionReasons.push("transform_introduced");
3745
+ }
3746
+ }
3747
+ if (section.bestScore >= strongestBestHit) {
3748
+ reasons.push("best_hit");
3749
+ }
3750
+ if (section.count > 1) {
3751
+ reasons.push("multi_hit_section");
3752
+ }
3753
+ if (siblings.length === 0) {
3754
+ reasons.push("only_section_in_parent");
3755
+ } else if (!strongestSibling || section.totalScore >= strongestSibling.totalScore) {
3756
+ reasons.push("dominant_within_parent");
3757
+ }
3758
+ if (scoreShare >= 0.5 || (parentShare ?? 0) >= 0.6) {
3759
+ reasons.push("concentrated_evidence");
3760
+ }
3761
+ const summaryParts = [
3762
+ isBlockAwareContextLabel(topContextLabel) ? topContextLabel : "",
3763
+ `${section.count} hit${section.count === 1 ? "" : "s"}`,
3764
+ `${(scoreShare * 100).toFixed(0)}% score share`,
3765
+ `vector ${section.vectorHits} · lexical ${section.lexicalHits} · hybrid ${section.hybridHits}`,
3766
+ typeof parentShare === "number" ? `${(parentShare * 100).toFixed(0)}% of parent section set` : "",
3767
+ sourceAwareChunkReason ? `boundary ${sourceAwareChunkReason}` : "",
3768
+ sourceAwareUnitScopeLabel ? `scope ${sourceAwareUnitScopeLabel}` : "",
3769
+ strongestSibling ? `ahead of ${strongestSibling.label} by ${(section.totalScore - strongestSibling.totalScore).toFixed(2)}` : "no sibling competition"
3770
+ ].filter(Boolean);
3771
+ return {
3772
+ averageScore: section.totalScore / section.count,
3773
+ bestScore: section.bestScore,
3774
+ count: section.count,
3775
+ key: section.key,
3776
+ label: section.label,
3777
+ parentLabel: section.parentLabel,
3778
+ parentDistribution,
3779
+ parentShare,
3780
+ parentShareGap: typeof parentShare === "number" && strongestSibling && parentTotal > 0 ? parentShare - strongestSibling.totalScore / parentTotal : undefined,
3781
+ path: section.path,
3782
+ firstSeenStage,
3783
+ finalCount,
3784
+ finalRetentionRate,
3785
+ lastSeenStage,
3786
+ dropFromPeak,
3787
+ peakCount,
3788
+ peakStage: peakStageEntry?.stage,
3789
+ queryAttribution: {
3790
+ mode: queryAttributionMode,
3791
+ primaryHits: section.primaryHits,
3792
+ reasons: queryAttributionReasons,
3793
+ transformedHits: section.transformedHits,
3794
+ variantHits: section.variantHits
3795
+ },
3796
+ requestedMode: trace?.requestedMode,
3797
+ retrievalMode: trace?.mode,
3798
+ routingLabel: trace?.routingLabel,
3799
+ routingProvider: trace?.routingProvider,
3800
+ routingReason: trace?.routingReason,
3801
+ queryTransformLabel: trace?.queryTransformLabel,
3802
+ queryTransformProvider: trace?.queryTransformProvider,
3803
+ queryTransformReason: trace?.queryTransformReason,
3804
+ reasons,
3805
+ evidenceReconcileApplied: trace?.steps.some((step) => step.stage === "evidence_reconcile"),
3806
+ rerankApplied: trace?.steps.some((step) => step.stage === "rerank" && step.metadata?.applied === true),
3807
+ scoreShare,
3808
+ scoreThresholdApplied: trace?.steps.some((step) => step.stage === "score_filter"),
3809
+ stageCounts,
3810
+ stageWeights,
3811
+ siblingCount: siblings.length,
3812
+ siblingScoreGap: strongestSibling ? section.totalScore - strongestSibling.totalScore : undefined,
3813
+ sourceCount: section.sourceSet.size,
3814
+ sourceAwareChunkReasonLabel: sourceAwareChunkReason,
3815
+ sourceAwareUnitScopeLabel,
3816
+ topContextLabel,
3817
+ topLocatorLabel,
3818
+ sourceBalanceApplied: trace?.steps.some((step) => step.stage === "source_balance"),
3819
+ strongestSiblingLabel: strongestSibling?.label,
3820
+ strongestSiblingScore: strongestSibling?.totalScore,
3821
+ summary: summaryParts.join(" · "),
3822
+ topChunkId: section.topChunkId,
3823
+ topSource: section.topSource,
3824
+ totalScore: section.totalScore,
3825
+ hybridHits: section.hybridHits,
3826
+ lexicalHits: section.lexicalHits,
3827
+ vectorHits: section.vectorHits
3828
+ };
3829
+ }).sort((left, right) => {
3830
+ if (right.totalScore !== left.totalScore) {
3831
+ return right.totalScore - left.totalScore;
3832
+ }
3833
+ if (right.bestScore !== left.bestScore) {
3834
+ return right.bestScore - left.bestScore;
3835
+ }
3836
+ return left.label.localeCompare(right.label);
3837
+ });
3838
+ };
3839
+ var buildStreamProgressState = (messages) => {
3840
+ const latestMessage = getLatestAssistantMessage(messages);
3841
+ const retrieved = latestMessage ? buildRAGRetrievedState(messages) : undefined;
3842
+ return {
3843
+ conversationId: latestMessage?.conversationId,
3844
+ latestMessage,
3845
+ messageId: latestMessage?.id,
3846
+ retrieved,
3847
+ sourceCount: retrieved?.sources.length ?? latestMessage?.sources?.length ?? 0
3848
+ };
3849
+ };
3850
+ var buildRAGStreamProgress = ({
3851
+ error,
3852
+ isStreaming,
3853
+ messages
3854
+ }) => {
3855
+ const stage = resolveRAGStreamStage({
3856
+ error,
3857
+ isStreaming,
3858
+ messages
3859
+ });
3860
+ const state = buildStreamProgressState(messages);
3861
+ const hasSources = state.sourceCount > 0;
3862
+ const hasRetrieved = stage === "retrieved" || state.retrieved !== undefined || state.latestMessage?.retrievedAt !== undefined;
3863
+ const hasThinking = typeof state.latestMessage?.thinking === "string" && state.latestMessage.thinking.length > 0;
3864
+ const hasToolCalls = (state.latestMessage?.toolCalls?.length ?? 0) > 0;
3865
+ return {
3866
+ conversationId: state.conversationId,
3867
+ hasContent: typeof state.latestMessage?.content === "string" && state.latestMessage.content.length > 0,
3868
+ hasRetrieved,
3869
+ hasSources,
3870
+ hasThinking,
3871
+ hasToolCalls,
3872
+ isComplete: stage === "complete",
3873
+ isError: stage === "error",
3874
+ isIdle: stage === "idle",
3875
+ isRetrieved: stage === "retrieved",
3876
+ isRetrieving: stage === "submitting" || stage === "retrieving",
3877
+ isStreaming: stage === "streaming",
3878
+ isSubmitting: stage === "submitting",
3879
+ latestMessage: state.latestMessage,
3880
+ messageId: state.messageId,
3881
+ retrievalDurationMs: state.retrieved?.retrievalDurationMs,
3882
+ retrievalStartedAt: state.retrieved?.retrievalStartedAt,
3883
+ retrievedAt: state.retrieved?.retrievedAt,
3884
+ sourceCount: state.sourceCount,
3885
+ stage
3886
+ };
3887
+ };
3888
+ var buildRAGAnswerWorkflowState = ({
3889
+ error,
3890
+ isStreaming,
3891
+ messages
3892
+ }) => {
3893
+ const latestAssistantMessage = getLatestAssistantMessage(messages);
3894
+ const sources = getLatestRAGSources(messages);
3895
+ const sourceGroups = buildRAGSourceGroups(sources);
3896
+ const citations = buildRAGCitations(sources);
3897
+ const citationReferenceMap = buildRAGCitationReferenceMap(citations);
3898
+ const sourceSummaries = buildRAGSourceSummaries(sources);
3899
+ const groundingReferences = buildRAGGroundingReferences(sources);
3900
+ const groundedAnswer = buildRAGGroundedAnswer(latestAssistantMessage?.content ?? "", sources);
3901
+ const retrieval = buildRAGRetrievedState(messages);
3902
+ const sectionDiagnostics = buildRAGSectionRetrievalDiagnostics(sources, retrieval?.trace);
3903
+ const progress = buildRAGStreamProgress({
3904
+ error,
3905
+ isStreaming,
3906
+ messages
3907
+ });
3908
+ return {
3909
+ excerptModeCounts: buildRAGExcerptModeCounts([
3910
+ ...citations.map((citation) => citation.excerptSelection),
3911
+ ...sourceSummaries.map((summary) => summary.excerptSelection),
3912
+ ...groundingReferences.map((reference) => reference.excerptSelection),
3913
+ ...groundedAnswer.sectionSummaries.map((summary) => summary.excerptSelection)
3914
+ ]),
3915
+ citationReferenceMap,
3916
+ citations,
3917
+ coverage: groundedAnswer.coverage,
3918
+ error,
3919
+ groundedAnswer,
3920
+ groundingReferences,
3921
+ hasCitations: groundedAnswer.hasCitations,
3922
+ hasGrounding: groundingReferences.length > 0,
3923
+ hasRetrieved: progress.hasRetrieved,
3924
+ hasSources: sources.length > 0,
3925
+ isAnswerStreaming: progress.isStreaming,
3926
+ isComplete: progress.isComplete,
3927
+ isError: progress.isError,
3928
+ isIdle: progress.isIdle,
3929
+ isRetrieved: progress.isRetrieved,
3930
+ isRetrieving: progress.isRetrieving,
3931
+ isRunning: progress.isSubmitting || progress.isRetrieving || progress.isStreaming,
3932
+ isSubmitting: progress.isSubmitting,
3933
+ latestAssistantMessage,
3934
+ messages,
3935
+ retrieval,
3936
+ retrievalDurationMs: retrieval?.retrievalDurationMs,
3937
+ retrievalStartedAt: retrieval?.retrievalStartedAt,
3938
+ retrievedAt: retrieval?.retrievedAt,
3939
+ sectionDiagnostics,
3940
+ sourceGroups,
3941
+ sourceSummaries,
3942
+ sources,
3943
+ stage: progress.stage,
3944
+ ungroundedReferenceNumbers: groundedAnswer.ungroundedReferenceNumbers
3945
+ };
3946
+ };
3947
+ var buildRAGSourceGroups = (sources) => {
3948
+ const groups = new Map;
3949
+ for (const source of sources) {
3950
+ updateSourceGroup(groups, source);
3951
+ }
3952
+ return [...groups.values()].sort((left, right) => {
3953
+ if (right.bestScore !== left.bestScore) {
3954
+ return right.bestScore - left.bestScore;
3955
+ }
3956
+ return left.label.localeCompare(right.label);
3957
+ });
3958
+ };
3959
+ var buildSourceGroup = (source, key) => ({
3960
+ bestScore: source.score,
3961
+ chunks: [source],
3962
+ count: 1,
3963
+ key,
3964
+ label: buildSourceLabel2(source),
3965
+ labels: source.labels ?? buildRAGSourceLabels({
3966
+ metadata: source.metadata,
3967
+ source: source.source,
3968
+ title: source.title
3969
+ }),
3970
+ structure: source.structure ?? buildRAGChunkStructure(source.metadata),
3971
+ source: source.source,
3972
+ title: source.title
3973
+ });
3974
+ var updateSourceGroup = (groups, source) => {
3975
+ const key = buildSourceGroupKey(source);
3976
+ const existing = groups.get(key);
3977
+ if (!existing) {
3978
+ groups.set(key, buildSourceGroup(source, key));
3979
+ return;
3980
+ }
3981
+ existing.bestScore = Math.max(existing.bestScore, source.score);
3982
+ existing.count += 1;
3983
+ existing.chunks.push(source);
3984
+ const leadChunk = getPreferredSourceLeadChunk(existing.chunks);
3985
+ if (leadChunk) {
3986
+ existing.label = buildSourceLabel2(leadChunk);
3987
+ existing.labels = leadChunk.labels ?? buildRAGSourceLabels({
3988
+ metadata: leadChunk.metadata,
3989
+ source: leadChunk.source,
3990
+ title: leadChunk.title
3991
+ });
3992
+ existing.structure = leadChunk.structure ?? buildRAGChunkStructure(leadChunk.metadata);
3993
+ existing.source = leadChunk.source;
3994
+ existing.title = leadChunk.title;
3995
+ }
3996
+ };
3997
+ var getLatestAssistantMessage = (messages) => {
3998
+ for (let index = messages.length - 1;index >= 0; index -= 1) {
3999
+ const message = messages[index];
4000
+ if (message?.role === "assistant") {
4001
+ return message;
4002
+ }
4003
+ }
4004
+ return;
4005
+ };
4006
+ var getLatestRAGSources = (messages) => getLatestAssistantMessage(messages)?.sources ?? [];
4007
+ var getLatestRetrievedMessage = (messages) => {
4008
+ for (let index = messages.length - 1;index >= 0; index -= 1) {
4009
+ const message = messages[index];
4010
+ if (message?.role === "assistant" && (typeof message.retrievedAt === "number" || (message.sources?.length ?? 0) > 0)) {
4011
+ return message;
4012
+ }
4013
+ }
4014
+ return;
4015
+ };
4016
+ var resolveRAGStreamStage = ({
4017
+ error,
4018
+ isStreaming,
4019
+ messages
4020
+ }) => {
4021
+ if (error) {
4022
+ return "error";
4023
+ }
4024
+ const assistantMessage = getLatestAssistantMessage(messages);
4025
+ if (!assistantMessage) {
4026
+ return isStreaming ? "submitting" : "idle";
4027
+ }
4028
+ const isRetrieving = typeof assistantMessage.retrievalStartedAt === "number" && typeof assistantMessage.retrievedAt !== "number";
4029
+ if (isRetrieving) {
4030
+ return "retrieving";
4031
+ }
4032
+ if (!isStreaming) {
4033
+ return "complete";
4034
+ }
4035
+ const hasRetrieved = typeof assistantMessage.retrievedAt === "number";
4036
+ const hasContent = assistantMessage.content.trim().length > 0 || assistantMessage.thinking?.trim().length || (assistantMessage.toolCalls?.length ?? 0) > 0 || (assistantMessage.images?.length ?? 0) > 0;
4037
+ if (hasRetrieved && !hasContent) {
4038
+ return "retrieved";
4039
+ }
4040
+ return "streaming";
4041
+ };
4042
+ // src/vue/ai/useRAGChunkPreview.ts
4043
+ var useRAGChunkPreview = (path) => {
4044
+ const client = createRAGClient({ path });
4045
+ const preview = ref(null);
4046
+ const activeChunkId = ref(null);
4047
+ const error = ref(null);
4048
+ const isLoading = ref(false);
4049
+ const chunkGraph = computed(() => preview.value ? buildRAGChunkPreviewGraph(preview.value) : null);
4050
+ const navigation = computed(() => preview.value ? buildRAGChunkPreviewNavigation(preview.value, activeChunkId.value ?? undefined) : null);
4051
+ const previewSources = computed(() => preview.value ? preview.value.chunks.map((chunk, index) => ({
4052
+ chunkId: chunk.chunkId,
4053
+ labels: chunk.labels,
4054
+ metadata: chunk.metadata,
4055
+ score: Math.max(0, preview.value.chunks.length - index),
4056
+ source: chunk.source ?? preview.value.document.source,
4057
+ structure: chunk.structure,
4058
+ text: chunk.text,
4059
+ title: chunk.title ?? preview.value.document.title
4060
+ })) : []);
4061
+ const sectionDiagnostics = computed(() => buildRAGSectionRetrievalDiagnostics(previewSources.value));
4062
+ const activeSectionDiagnostic = computed(() => {
4063
+ const sectionKey = navigation.value?.section?.path?.join(" > ");
4064
+ return sectionKey ? sectionDiagnostics.value.find((diagnostic) => diagnostic.key === sectionKey) ?? null : null;
4065
+ });
4066
+ const inspect = async (id) => {
4067
+ isLoading.value = true;
4068
+ error.value = null;
4069
+ try {
4070
+ const response = await client.documentChunks(id);
4071
+ if (!response.ok) {
4072
+ throw new Error(response.error);
4073
+ }
4074
+ preview.value = response;
4075
+ activeChunkId.value = response.chunks[0]?.chunkId ?? null;
4076
+ return response;
4077
+ } catch (caught) {
4078
+ error.value = caught instanceof Error ? caught.message : String(caught);
4079
+ throw caught;
4080
+ } finally {
4081
+ isLoading.value = false;
4082
+ }
4083
+ };
4084
+ const clear = () => {
4085
+ error.value = null;
4086
+ isLoading.value = false;
4087
+ activeChunkId.value = null;
4088
+ preview.value = null;
4089
+ };
4090
+ const selectChunk = (id) => {
4091
+ activeChunkId.value = id;
4092
+ };
4093
+ const selectParentSection = () => {
4094
+ const leadChunkId = navigation.value?.parentSection?.leadChunkId;
4095
+ if (leadChunkId) {
4096
+ activeChunkId.value = leadChunkId;
4097
+ }
4098
+ };
4099
+ const selectChildSection = (sectionId) => {
4100
+ const leadChunkId = navigation.value?.childSections.find((section) => section.id === sectionId)?.leadChunkId;
4101
+ if (leadChunkId) {
4102
+ activeChunkId.value = leadChunkId;
4103
+ }
4104
+ };
4105
+ const selectSiblingSection = (sectionId) => {
4106
+ const leadChunkId = navigation.value?.siblingSections.find((section) => section.id === sectionId)?.leadChunkId;
4107
+ if (leadChunkId) {
4108
+ activeChunkId.value = leadChunkId;
4109
+ }
4110
+ };
4111
+ return {
4112
+ activeChunkId,
4113
+ activeSectionDiagnostic,
4114
+ clear,
4115
+ chunkGraph,
4116
+ error,
4117
+ inspect,
4118
+ isLoading,
4119
+ navigation,
4120
+ preview,
4121
+ sectionDiagnostics,
4122
+ selectChildSection,
4123
+ selectChunk,
4124
+ selectParentSection,
4125
+ selectSiblingSection
4126
+ };
4127
+ };
4128
+ // src/vue/ai/useRAG.ts
4129
+ import { computed as computed9 } from "vue";
4130
+
4131
+ // src/vue/ai/useRAGCitations.ts
4132
+ import { computed as computed2 } from "vue";
4133
+ var useRAGCitations = (sources) => {
4134
+ const citations = computed2(() => buildRAGCitations(sources.value));
4135
+ const citationReferenceMap = computed2(() => buildRAGCitationReferenceMap(citations.value));
4136
+ const sourceGroups = computed2(() => buildRAGSourceGroups(sources.value));
4137
+ const sourceSummaries = computed2(() => buildRAGSourceSummaries(sources.value));
4138
+ const hasCitations = computed2(() => citations.value.length > 0);
4139
+ return {
4140
+ citationReferenceMap,
4141
+ citations,
4142
+ hasCitations,
4143
+ sourceGroups,
4144
+ sourceSummaries
4145
+ };
4146
+ };
4147
+
4148
+ // src/vue/ai/useRAGDocuments.ts
4149
+ import { ref as ref2 } from "vue";
4150
+ var useRAGDocuments = (path) => {
4151
+ const client = createRAGClient({ path });
4152
+ const documents = ref2([]);
4153
+ const error = ref2(null);
4154
+ const isLoading = ref2(false);
4155
+ const lastResponse = ref2(null);
4156
+ const load = async (kind) => {
4157
+ isLoading.value = true;
4158
+ error.value = null;
4159
+ try {
4160
+ const response = await client.documents(kind);
4161
+ documents.value = response.documents;
4162
+ lastResponse.value = response;
4163
+ return response;
4164
+ } catch (caught) {
4165
+ error.value = caught instanceof Error ? caught.message : String(caught);
4166
+ throw caught;
4167
+ } finally {
4168
+ isLoading.value = false;
4169
+ }
4170
+ };
4171
+ const reset = () => {
4172
+ documents.value = [];
4173
+ error.value = null;
4174
+ isLoading.value = false;
4175
+ lastResponse.value = null;
4176
+ };
4177
+ return {
4178
+ documents,
4179
+ error,
4180
+ isLoading,
4181
+ lastResponse,
4182
+ load,
4183
+ reset
4184
+ };
4185
+ };
4186
+
4187
+ // src/vue/ai/useRAGEvaluate.ts
4188
+ import { computed as computed3, ref as ref3 } from "vue";
4189
+ var useRAGEvaluate = (path) => {
4190
+ const client = createRAGClient({ path });
4191
+ const error = ref3(null);
4192
+ const isEvaluating = ref3(false);
4193
+ const suites = ref3([]);
4194
+ const suiteRuns = ref3([]);
4195
+ const lastRequest = ref3(null);
4196
+ const lastResponse = ref3(null);
4197
+ const evaluate = async (input) => {
4198
+ isEvaluating.value = true;
4199
+ error.value = null;
4200
+ lastRequest.value = input;
4201
+ try {
4202
+ const response = await client.evaluate(input);
4203
+ lastResponse.value = response;
4204
+ return response;
4205
+ } catch (caught) {
4206
+ error.value = caught instanceof Error ? caught.message : String(caught);
4207
+ throw caught;
4208
+ } finally {
4209
+ isEvaluating.value = false;
4210
+ }
4211
+ };
4212
+ const saveSuite = (suite) => {
4213
+ suites.value = [
4214
+ ...suites.value.filter((entry) => entry.id !== suite.id),
4215
+ suite
4216
+ ];
4217
+ return suite;
4218
+ };
4219
+ const removeSuite = (id) => {
4220
+ suites.value = suites.value.filter((suite) => suite.id !== id);
4221
+ };
4222
+ const runSuite = async (suite, overrides) => {
4223
+ isEvaluating.value = true;
4224
+ error.value = null;
4225
+ lastRequest.value = overrides ? { ...suite.input, ...overrides } : suite.input;
4226
+ try {
4227
+ const run = await runRAGEvaluationSuite({
4228
+ evaluate: client.evaluate,
4229
+ overrides,
4230
+ suite
4231
+ });
4232
+ lastResponse.value = run.response;
4233
+ suiteRuns.value = [run, ...suiteRuns.value];
4234
+ return run;
4235
+ } catch (caught) {
4236
+ error.value = caught instanceof Error ? caught.message : String(caught);
4237
+ throw caught;
4238
+ } finally {
4239
+ isEvaluating.value = false;
4240
+ }
4241
+ };
4242
+ const clearRuns = () => {
4243
+ suiteRuns.value = [];
4244
+ };
4245
+ const leaderboard = computed3(() => buildRAGEvaluationLeaderboard(suiteRuns.value));
4246
+ const reset = () => {
4247
+ error.value = null;
4248
+ lastRequest.value = null;
4249
+ lastResponse.value = null;
4250
+ suiteRuns.value = [];
4251
+ };
4252
+ return {
4253
+ clearRuns,
4254
+ error,
4255
+ evaluate,
4256
+ isEvaluating,
4257
+ lastRequest,
4258
+ lastResponse,
4259
+ leaderboard,
4260
+ removeSuite,
4261
+ reset,
4262
+ runSuite,
4263
+ saveSuite,
4264
+ suiteRuns,
4265
+ suites
4266
+ };
4267
+ };
4268
+
4269
+ // src/vue/ai/useRAGGrounding.ts
4270
+ import { computed as computed4 } from "vue";
4271
+ var useRAGGrounding = (content, sources) => {
4272
+ const groundedAnswer = computed4(() => buildRAGGroundedAnswer(content.value, sources.value));
4273
+ const references = computed4(() => buildRAGGroundingReferences(sources.value));
4274
+ const hasCitations = computed4(() => groundedAnswer.value.hasCitations);
4275
+ const hasGrounding = computed4(() => references.value.length > 0);
4276
+ const coverage = computed4(() => groundedAnswer.value.coverage);
4277
+ const ungroundedReferenceNumbers = computed4(() => groundedAnswer.value.ungroundedReferenceNumbers);
4278
+ return {
4279
+ coverage,
4280
+ groundedAnswer,
4281
+ hasCitations,
4282
+ hasGrounding,
4283
+ references,
4284
+ ungroundedReferenceNumbers
4285
+ };
4286
+ };
4287
+
4288
+ // src/vue/ai/useRAGIngest.ts
4289
+ import { ref as ref4 } from "vue";
4290
+ var useRAGIngest = (path) => {
4291
+ const client = createRAGClient({ path });
4292
+ const error = ref4(null);
4293
+ const isIngesting = ref4(false);
4294
+ const lastIngestCount = ref4(null);
4295
+ const lastDocumentCount = ref4(null);
4296
+ const lastResponse = ref4(null);
4297
+ const ingestChunks = async (chunks) => {
4298
+ isIngesting.value = true;
4299
+ error.value = null;
4300
+ try {
4301
+ const response = await client.ingest(chunks);
4302
+ if (!response.ok) {
4303
+ throw new Error(response.error ?? "RAG ingest failed");
4304
+ }
4305
+ lastIngestCount.value = response.count ?? chunks.length;
4306
+ lastDocumentCount.value = null;
4307
+ lastResponse.value = response;
4308
+ return response;
4309
+ } catch (caught) {
4310
+ error.value = caught instanceof Error ? caught.message : String(caught);
4311
+ throw caught;
4312
+ } finally {
4313
+ isIngesting.value = false;
4314
+ }
4315
+ };
4316
+ const ingestDocuments = async (input) => {
4317
+ isIngesting.value = true;
4318
+ error.value = null;
4319
+ try {
4320
+ const response = await client.ingestDocuments(input);
4321
+ if (!response.ok) {
4322
+ throw new Error(response.error ?? "RAG document ingest failed");
4323
+ }
4324
+ lastDocumentCount.value = response.documentCount ?? input.documents.length;
4325
+ lastIngestCount.value = response.count ?? null;
4326
+ lastResponse.value = response;
4327
+ return response;
4328
+ } catch (caught) {
4329
+ error.value = caught instanceof Error ? caught.message : String(caught);
4330
+ throw caught;
4331
+ } finally {
4332
+ isIngesting.value = false;
4333
+ }
4334
+ };
4335
+ const ingestUrls = async (input) => {
4336
+ isIngesting.value = true;
4337
+ error.value = null;
4338
+ try {
4339
+ const response = await client.ingestUrls(input);
4340
+ if (!response.ok) {
4341
+ throw new Error(response.error ?? "RAG URL ingest failed");
4342
+ }
4343
+ lastIngestCount.value = response.count ?? null;
4344
+ lastDocumentCount.value = response.documentCount ?? input.urls.length;
4345
+ lastResponse.value = response;
4346
+ return response;
4347
+ } catch (caught) {
4348
+ error.value = caught instanceof Error ? caught.message : String(caught);
4349
+ throw caught;
4350
+ } finally {
4351
+ isIngesting.value = false;
4352
+ }
4353
+ };
4354
+ const ingestUploads = async (input) => {
4355
+ isIngesting.value = true;
4356
+ error.value = null;
4357
+ try {
4358
+ const response = await client.ingestUploads(input);
4359
+ if (!response.ok) {
4360
+ throw new Error(response.error ?? "RAG upload ingest failed");
4361
+ }
4362
+ lastIngestCount.value = response.count ?? null;
4363
+ lastDocumentCount.value = response.documentCount ?? input.uploads.length;
4364
+ lastResponse.value = response;
4365
+ return response;
4366
+ } catch (caught) {
4367
+ error.value = caught instanceof Error ? caught.message : String(caught);
4368
+ throw caught;
4369
+ } finally {
4370
+ isIngesting.value = false;
4371
+ }
4372
+ };
4373
+ const clearIndex = async () => {
4374
+ isIngesting.value = true;
4375
+ error.value = null;
4376
+ try {
4377
+ return await client.clearIndex();
4378
+ } catch (caught) {
4379
+ error.value = caught instanceof Error ? caught.message : String(caught);
4380
+ throw caught;
4381
+ } finally {
4382
+ isIngesting.value = false;
4383
+ }
4384
+ };
4385
+ const reset = () => {
4386
+ error.value = null;
4387
+ isIngesting.value = false;
4388
+ lastDocumentCount.value = null;
4389
+ lastIngestCount.value = null;
4390
+ lastResponse.value = null;
4391
+ };
4392
+ return {
4393
+ clearIndex,
4394
+ error,
4395
+ ingest: ingestChunks,
4396
+ ingestChunks,
4397
+ ingestDocuments,
4398
+ ingestUploads,
4399
+ ingestUrls,
4400
+ isIngesting,
4401
+ lastDocumentCount,
4402
+ lastIngestCount,
4403
+ lastResponse,
4404
+ reset
4405
+ };
4406
+ };
4407
+
4408
+ // src/vue/ai/useRAGIndexAdmin.ts
4409
+ import { ref as ref5 } from "vue";
4410
+ var useRAGIndexAdmin = (path) => {
4411
+ const client = createRAGClient({ path });
4412
+ const isLoading = ref5(false);
4413
+ const error = ref5(null);
4414
+ const lastMutation = ref5(null);
4415
+ const backends = ref5(null);
4416
+ const syncSources = ref5(null);
4417
+ const run = async (operation) => {
4418
+ isLoading.value = true;
4419
+ error.value = null;
4420
+ try {
4421
+ return await operation();
4422
+ } catch (caught) {
4423
+ error.value = caught instanceof Error ? caught.message : String(caught);
4424
+ throw caught;
4425
+ } finally {
4426
+ isLoading.value = false;
4427
+ }
4428
+ };
4429
+ const createDocument = async (input) => run(async () => {
4430
+ const response = await client.createDocument(input);
4431
+ lastMutation.value = response;
4432
+ if (!response.ok) {
4433
+ throw new Error(response.error ?? "Failed to create document");
4434
+ }
4435
+ return response;
4436
+ });
4437
+ const deleteDocument = async (id) => run(async () => {
4438
+ const response = await client.deleteDocument(id);
4439
+ lastMutation.value = response;
4440
+ if (!response.ok) {
4441
+ throw new Error(response.error ?? "Failed to delete document");
4442
+ }
4443
+ return response;
4444
+ });
4445
+ const reseed = async () => run(async () => {
4446
+ const response = await client.reseed();
4447
+ lastMutation.value = response;
4448
+ if (!response.ok) {
4449
+ throw new Error(response.error ?? "Failed to reseed index");
4450
+ }
4451
+ return response;
4452
+ });
4453
+ const analyzeBackend = async () => run(async () => {
4454
+ const response = await client.analyzeBackend();
4455
+ lastMutation.value = response;
4456
+ if (!response.ok) {
4457
+ throw new Error(response.error ?? "Failed to analyze backend");
4458
+ }
4459
+ return response;
4460
+ });
4461
+ const reindexDocument = async (id) => run(async () => {
4462
+ const response = await client.reindexDocument(id);
4463
+ lastMutation.value = response;
4464
+ if (!response.ok) {
4465
+ throw new Error(response.error ?? "Failed to reindex document");
4466
+ }
4467
+ return response;
4468
+ });
4469
+ const reindexSource = async (source) => run(async () => {
4470
+ const response = await client.reindexSource(source);
4471
+ lastMutation.value = response;
4472
+ if (!response.ok) {
4473
+ throw new Error(response.error ?? "Failed to reindex source");
4474
+ }
4475
+ return response;
4476
+ });
4477
+ const rebuildNativeIndex = async () => run(async () => {
4478
+ const response = await client.rebuildNativeIndex();
4479
+ lastMutation.value = response;
4480
+ if (!response.ok) {
4481
+ throw new Error(response.error ?? "Failed to rebuild native index");
4482
+ }
4483
+ return response;
4484
+ });
4485
+ const reset = async () => run(async () => {
4486
+ const response = await client.reset();
4487
+ lastMutation.value = response;
4488
+ if (!response.ok) {
4489
+ throw new Error(response.error ?? "Failed to reset index");
4490
+ }
4491
+ return response;
4492
+ });
4493
+ const loadBackends = async () => run(async () => {
4494
+ const response = await client.backends();
4495
+ backends.value = response;
4496
+ return response;
4497
+ });
4498
+ const loadSyncSources = async () => run(async () => {
4499
+ const response = await client.syncSources();
4500
+ syncSources.value = response;
4501
+ return response;
4502
+ });
4503
+ const syncAllSources = async (options) => run(async () => {
4504
+ const response = await client.syncAllSources(options);
4505
+ syncSources.value = response;
4506
+ if (!response.ok) {
4507
+ throw new Error(response.error ?? "Failed to sync sources");
4508
+ }
4509
+ return response;
4510
+ });
4511
+ const syncSource = async (id, options) => run(async () => {
4512
+ const response = await client.syncSource(id, options);
4513
+ syncSources.value = response;
4514
+ if (!response.ok) {
4515
+ throw new Error(response.error ?? "Failed to sync source");
4516
+ }
4517
+ return response;
4518
+ });
4519
+ const clearIndex = async () => run(async () => {
4520
+ const response = await client.clearIndex();
4521
+ const mutation = { ok: response.ok };
4522
+ lastMutation.value = mutation;
4523
+ return mutation;
4524
+ });
4525
+ const resetState = () => {
4526
+ backends.value = null;
4527
+ error.value = null;
4528
+ isLoading.value = false;
4529
+ lastMutation.value = null;
4530
+ syncSources.value = null;
4531
+ };
4532
+ return {
4533
+ backends,
4534
+ analyzeBackend,
4535
+ clearIndex,
4536
+ createDocument,
4537
+ deleteDocument,
4538
+ error,
4539
+ isLoading,
4540
+ lastMutation,
4541
+ loadBackends,
4542
+ loadSyncSources,
4543
+ rebuildNativeIndex,
4544
+ reindexDocument,
4545
+ reindexSource,
4546
+ reseed,
4547
+ reset,
4548
+ resetState,
4549
+ syncAllSources,
4550
+ syncSource,
4551
+ syncSources
4552
+ };
4553
+ };
4554
+
4555
+ // src/vue/ai/useRAGOps.ts
4556
+ import { onMounted, ref as ref6 } from "vue";
4557
+ var useRAGOps = (path, autoLoad = true) => {
4558
+ const client = createRAGClient({ path });
4559
+ const operations = ref6();
4560
+ const admin = ref6();
4561
+ const adminActions = ref6([]);
4562
+ const adminJobs = ref6([]);
4563
+ const status = ref6();
4564
+ const capabilities = ref6();
4565
+ const maintenance = ref6();
4566
+ const health = ref6();
4567
+ const readiness = ref6();
4568
+ const documents = ref6();
4569
+ const ingestJobs = ref6([]);
4570
+ const syncSources = ref6([]);
4571
+ const error = ref6(null);
4572
+ const isLoading = ref6(autoLoad);
4573
+ const refresh = async () => {
4574
+ isLoading.value = true;
4575
+ error.value = null;
4576
+ try {
4577
+ const response = await client.ops();
4578
+ operations.value = response;
4579
+ admin.value = response.admin;
4580
+ adminActions.value = response.adminActions;
4581
+ adminJobs.value = response.adminJobs ?? [];
4582
+ status.value = response.status;
4583
+ capabilities.value = response.capabilities;
4584
+ maintenance.value = response.maintenance;
4585
+ health.value = response.health;
4586
+ readiness.value = response.readiness;
4587
+ documents.value = response.documents;
4588
+ ingestJobs.value = response.ingestJobs ?? [];
4589
+ syncSources.value = response.syncSources ?? [];
4590
+ return response;
4591
+ } catch (caught) {
4592
+ error.value = caught instanceof Error ? caught.message : String(caught);
4593
+ throw caught;
4594
+ } finally {
4595
+ isLoading.value = false;
4596
+ }
4597
+ };
4598
+ const reset = () => {
4599
+ operations.value = undefined;
4600
+ admin.value = undefined;
4601
+ adminActions.value = [];
4602
+ adminJobs.value = [];
4603
+ capabilities.value = undefined;
4604
+ documents.value = undefined;
4605
+ error.value = null;
4606
+ health.value = undefined;
4607
+ ingestJobs.value = [];
4608
+ isLoading.value = false;
4609
+ maintenance.value = undefined;
4610
+ readiness.value = undefined;
4611
+ syncSources.value = [];
4612
+ status.value = undefined;
4613
+ };
4614
+ onMounted(() => {
4615
+ if (!autoLoad) {
4616
+ isLoading.value = false;
4617
+ return;
4618
+ }
4619
+ refresh();
4620
+ });
4621
+ return {
4622
+ admin,
4623
+ adminActions,
4624
+ adminJobs,
4625
+ capabilities,
4626
+ documents,
4627
+ error,
4628
+ health,
4629
+ ingestJobs,
4630
+ isLoading,
4631
+ maintenance,
4632
+ operations,
4633
+ readiness,
4634
+ refresh,
4635
+ reset,
4636
+ status,
4637
+ syncSources
4638
+ };
4639
+ };
4640
+
4641
+ // src/vue/ai/useRAGSearch.ts
4642
+ import { ref as ref7 } from "vue";
4643
+ var useRAGSearch = (path) => {
4644
+ const client = createRAGClient({ path });
4645
+ const results = ref7([]);
4646
+ const trace = ref7();
4647
+ const error = ref7(null);
4648
+ const isSearching = ref7(false);
4649
+ const hasSearched = ref7(false);
4650
+ const lastRequest = ref7(null);
4651
+ const search = async (input) => {
4652
+ isSearching.value = true;
4653
+ error.value = null;
4654
+ lastRequest.value = input;
4655
+ try {
4656
+ const nextResults = await client.search({
4657
+ ...input,
4658
+ includeTrace: false
4659
+ });
4660
+ results.value = nextResults;
4661
+ trace.value = undefined;
4662
+ hasSearched.value = true;
4663
+ return nextResults;
4664
+ } catch (caught) {
4665
+ error.value = caught instanceof Error ? caught.message : String(caught);
4666
+ throw caught;
4667
+ } finally {
4668
+ isSearching.value = false;
4669
+ }
4670
+ };
4671
+ const searchWithTrace = async (input) => {
4672
+ isSearching.value = true;
4673
+ error.value = null;
4674
+ lastRequest.value = input;
4675
+ try {
4676
+ const response = await client.searchWithTrace(input);
4677
+ results.value = response.results;
4678
+ trace.value = response.trace;
4679
+ hasSearched.value = true;
4680
+ return response;
4681
+ } catch (caught) {
4682
+ error.value = caught instanceof Error ? caught.message : String(caught);
4683
+ throw caught;
4684
+ } finally {
4685
+ isSearching.value = false;
4686
+ }
4687
+ };
4688
+ const reset = () => {
4689
+ error.value = null;
4690
+ hasSearched.value = false;
4691
+ isSearching.value = false;
4692
+ lastRequest.value = null;
4693
+ results.value = [];
4694
+ trace.value = undefined;
4695
+ };
4696
+ return {
4697
+ error,
4698
+ hasSearched,
4699
+ isSearching,
4700
+ lastRequest,
4701
+ reset,
4702
+ results,
4703
+ search,
4704
+ searchWithTrace,
4705
+ trace
4706
+ };
4707
+ };
4708
+
4709
+ // src/vue/ai/useRAGSources.ts
4710
+ import { computed as computed5 } from "vue";
4711
+ var useRAGSources = (messages) => {
4712
+ const latestAssistantMessage = computed5(() => getLatestAssistantMessage(messages.value));
4713
+ const sources = computed5(() => getLatestRAGSources(messages.value));
4714
+ const sourceGroups = computed5(() => buildRAGSourceGroups(sources.value));
4715
+ const sourceSummaries = computed5(() => buildRAGSourceSummaries(sources.value));
4716
+ const sectionDiagnostics = computed5(() => buildRAGSectionRetrievalDiagnostics(sources.value, latestAssistantMessage.value?.retrievalTrace));
4717
+ const chunkGraph = computed5(() => buildRAGChunkGraph(sources.value));
4718
+ const citationReferenceMap = computed5(() => buildRAGCitationReferenceMap(sourceSummaries.value.flatMap((summary) => summary.citations)));
4719
+ const hasSources = computed5(() => sources.value.length > 0);
4720
+ const navigationForChunk = (chunkId) => buildRAGChunkGraphNavigation(chunkGraph.value, chunkId ?? undefined);
4721
+ return {
4722
+ citationReferenceMap,
4723
+ chunkGraph,
4724
+ hasSources,
4725
+ latestAssistantMessage,
4726
+ navigationForChunk,
4727
+ sectionDiagnostics,
4728
+ sourceGroups,
4729
+ sources,
4730
+ sourceSummaries
4731
+ };
4732
+ };
4733
+
4734
+ // src/vue/ai/useRAGStatus.ts
4735
+ import { onMounted as onMounted2, ref as ref8 } from "vue";
4736
+ var useRAGStatus = (path, autoLoad = true) => {
4737
+ const client = createRAGClient({ path });
4738
+ const status = ref8();
4739
+ const capabilities = ref8();
4740
+ const error = ref8(null);
4741
+ const isLoading = ref8(autoLoad);
4742
+ const refresh = async () => {
4743
+ isLoading.value = true;
4744
+ error.value = null;
4745
+ try {
4746
+ const response = await client.status();
4747
+ status.value = response.status;
4748
+ capabilities.value = response.capabilities;
4749
+ return response;
4750
+ } catch (caught) {
4751
+ error.value = caught instanceof Error ? caught.message : String(caught);
4752
+ throw caught;
4753
+ } finally {
4754
+ isLoading.value = false;
4755
+ }
4756
+ };
4757
+ const reset = () => {
4758
+ capabilities.value = undefined;
4759
+ error.value = null;
4760
+ isLoading.value = false;
4761
+ status.value = undefined;
4762
+ };
4763
+ onMounted2(() => {
4764
+ if (!autoLoad) {
4765
+ isLoading.value = false;
4766
+ return;
4767
+ }
4768
+ refresh();
4769
+ });
4770
+ return {
4771
+ capabilities,
4772
+ error,
4773
+ isLoading,
4774
+ refresh,
4775
+ reset,
4776
+ status
4777
+ };
4778
+ };
4779
+
4780
+ // src/vue/ai/useRAGWorkflow.ts
4781
+ import { computed as computed8 } from "vue";
4782
+
4783
+ // src/vue/ai/useRAGStream.ts
4784
+ import { computed as computed7 } from "vue";
4785
+ import { useAIStream } from "@absolutejs/ai/vue";
4786
+
4787
+ // src/vue/ai/useRAGStreamProgress.ts
4788
+ import { computed as computed6 } from "vue";
4789
+ var useRAGStreamProgress = (params) => computed6(() => buildRAGStreamProgress({
4790
+ error: params.error.value,
4791
+ isStreaming: params.isStreaming.value,
4792
+ messages: params.messages.value
4793
+ }));
4794
+
4795
+ // src/vue/ai/useRAGStream.ts
4796
+ var useRAGStream = (path, conversationId) => {
4797
+ const stream = useAIStream(path, conversationId);
4798
+ const workflow = computed7(() => buildRAGAnswerWorkflowState({
4799
+ error: stream.error.value,
4800
+ isStreaming: stream.isStreaming.value,
4801
+ messages: stream.messages.value
4802
+ }));
4803
+ const progress = useRAGStreamProgress({
4804
+ error: stream.error,
4805
+ isStreaming: stream.isStreaming,
4806
+ messages: stream.messages
4807
+ });
4808
+ const query = (content, attachments) => {
4809
+ stream.send(content, attachments);
4810
+ };
4811
+ const hasRetrieved = computed7(() => workflow.value.hasRetrieved);
4812
+ const isRetrieving = computed7(() => workflow.value.isRetrieving);
4813
+ const isRetrieved = computed7(() => workflow.value.isRetrieved);
4814
+ const isAnswerStreaming = computed7(() => workflow.value.isAnswerStreaming);
4815
+ const isComplete = computed7(() => workflow.value.isComplete);
4816
+ const hasSources = computed7(() => workflow.value.hasSources);
4817
+ return {
4818
+ ...stream,
4819
+ citationReferenceMap: computed7(() => workflow.value.citationReferenceMap),
4820
+ citations: computed7(() => workflow.value.citations),
4821
+ coverage: computed7(() => workflow.value.coverage),
4822
+ groundedAnswer: computed7(() => workflow.value.groundedAnswer),
4823
+ groundingReferences: computed7(() => workflow.value.groundingReferences),
4824
+ hasGrounding: computed7(() => workflow.value.hasGrounding),
4825
+ hasRetrieved,
4826
+ hasSources,
4827
+ isAnswerStreaming,
4828
+ isComplete,
4829
+ isError: computed7(() => workflow.value.isError),
4830
+ isRetrieved,
4831
+ isRetrieving,
4832
+ isRunning: computed7(() => workflow.value.isRunning),
4833
+ latestAssistantMessage: computed7(() => workflow.value.latestAssistantMessage),
4834
+ progress,
4835
+ query,
4836
+ retrieval: computed7(() => workflow.value.retrieval),
4837
+ sourceGroups: computed7(() => workflow.value.sourceGroups),
4838
+ sourceSummaries: computed7(() => workflow.value.sourceSummaries),
4839
+ sources: computed7(() => workflow.value.sources),
4840
+ stage: computed7(() => workflow.value.stage),
4841
+ ungroundedReferenceNumbers: computed7(() => workflow.value.ungroundedReferenceNumbers),
4842
+ workflow
4843
+ };
4844
+ };
4845
+
4846
+ // src/vue/ai/useRAGWorkflow.ts
4847
+ var useRAGWorkflow = (path, conversationId) => {
4848
+ const stream = useRAGStream(path, conversationId);
4849
+ const state = computed8(() => stream.workflow.value);
4850
+ return {
4851
+ ...stream,
4852
+ state
4853
+ };
4854
+ };
4855
+
4856
+ // src/vue/ai/useRAG.ts
4857
+ var useRAG = (path, options = {}) => {
4858
+ const search = useRAGSearch(path);
4859
+ const ingest = useRAGIngest(path);
4860
+ const status = useRAGStatus(path, options.autoLoadStatus ?? true);
4861
+ const ops = useRAGOps(path, options.autoLoadOps ?? true);
4862
+ const documents = useRAGDocuments(path);
4863
+ const chunkPreview = useRAGChunkPreview(path);
4864
+ const evaluate = useRAGEvaluate(path);
4865
+ const index = useRAGIndexAdmin(path);
4866
+ const workflow = useRAGWorkflow(options.streamPath ?? path, options.conversationId);
4867
+ const sources = useRAGSources(workflow.messages);
4868
+ const citations = useRAGCitations(sources.sources);
4869
+ const grounding = useRAGGrounding(computed9(() => workflow.latestAssistantMessage.value?.content ?? ""), sources.sources);
4870
+ return {
4871
+ chunkPreview,
4872
+ citations,
4873
+ documents,
4874
+ evaluate,
4875
+ grounding,
4876
+ index,
4877
+ ingest,
4878
+ ops,
4879
+ search,
4880
+ sources,
4881
+ status,
4882
+ stream: workflow,
4883
+ workflow
4884
+ };
4885
+ };
4886
+ export {
4887
+ useRAGWorkflow,
4888
+ useRAGStreamProgress,
4889
+ useRAGStream,
4890
+ useRAGStatus,
4891
+ useRAGSources,
4892
+ useRAGSearch,
4893
+ useRAGOps,
4894
+ useRAGIngest,
4895
+ useRAGIndexAdmin,
4896
+ useRAGGrounding,
4897
+ useRAGEvaluate,
4898
+ useRAGDocuments,
4899
+ useRAGCitations,
4900
+ useRAGChunkPreview,
4901
+ useRAG
4902
+ };
4903
+
4904
+ //# debugId=2E2B39C77DEA7C5864756E2164756E21
4905
+ //# sourceMappingURL=index.js.map