@absolutejs/rag 0.0.7 → 0.0.9

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.
@@ -1,6 +1,23 @@
1
1
  // @bun
2
2
  // src/ai/rag/quality.ts
3
- import { mkdir, readFile, writeFile } from "fs/promises";
3
+ import { mkdir, readFile } from "fs/promises";
4
+
5
+ // src/ai/rag/atomicWrite.ts
6
+ import { rename, unlink, writeFile } from "fs/promises";
7
+ var writeFileAtomic = async (path, data, encoding = "utf8") => {
8
+ const tmpPath = `${path}.tmp.${process.pid}.${Math.random().toString(36).slice(2, 10)}`;
9
+ try {
10
+ await writeFile(tmpPath, data, encoding);
11
+ await rename(tmpPath, path);
12
+ } catch (error) {
13
+ try {
14
+ await unlink(tmpPath);
15
+ } catch {}
16
+ throw error;
17
+ }
18
+ };
19
+
20
+ // src/ai/rag/quality.ts
4
21
  import { dirname } from "path";
5
22
  import { generateId } from "@absolutejs/ai";
6
23
 
@@ -6505,7 +6522,7 @@ var createRAGFileEvaluationHistoryStore = (path) => ({
6505
6522
  ...existing.filter((entry) => entry.id !== run.id)
6506
6523
  ]);
6507
6524
  await mkdir(dirname(path), { recursive: true });
6508
- await writeFile(path, JSON.stringify(next, null, "\t") + `
6525
+ await writeFileAtomic(path, JSON.stringify(next, null, "\t") + `
6509
6526
  `, "utf8");
6510
6527
  },
6511
6528
  pruneRuns: async (input) => {
@@ -6525,7 +6542,7 @@ var createRAGFileEvaluationHistoryStore = (path) => ({
6525
6542
  sort: normalizeHistoryRuns
6526
6543
  });
6527
6544
  await mkdir(dirname(path), { recursive: true });
6528
- await writeFile(path, JSON.stringify(pruned.next, null, "\t") + `
6545
+ await writeFileAtomic(path, JSON.stringify(pruned.next, null, "\t") + `
6529
6546
  `, "utf8");
6530
6547
  return {
6531
6548
  keptCount: pruned.keptCount,
@@ -6576,7 +6593,7 @@ var createRAGFileEvaluationSuiteSnapshotHistoryStore = (path) => ({
6576
6593
  ...existing.filter((entry) => entry.id !== snapshot.id)
6577
6594
  ]);
6578
6595
  await mkdir(dirname(path), { recursive: true });
6579
- await writeFile(path, JSON.stringify(next, null, "\t") + `
6596
+ await writeFileAtomic(path, JSON.stringify(next, null, "\t") + `
6580
6597
  `, "utf8");
6581
6598
  },
6582
6599
  pruneSnapshots: async (input) => {
@@ -6596,7 +6613,7 @@ var createRAGFileEvaluationSuiteSnapshotHistoryStore = (path) => ({
6596
6613
  sort: normalizeEvaluationSuiteSnapshots
6597
6614
  });
6598
6615
  await mkdir(dirname(path), { recursive: true });
6599
- await writeFile(path, JSON.stringify(pruned.next, null, "\t") + `
6616
+ await writeFileAtomic(path, JSON.stringify(pruned.next, null, "\t") + `
6600
6617
  `, "utf8");
6601
6618
  return {
6602
6619
  keptCount: pruned.keptCount,
@@ -6639,7 +6656,7 @@ var createRAGFileRetrievalComparisonHistoryStore = (path) => ({
6639
6656
  ...existing.filter((entry) => entry.id !== run.id)
6640
6657
  ]);
6641
6658
  await mkdir(dirname(path), { recursive: true });
6642
- await writeFile(path, JSON.stringify(next, null, "\t") + `
6659
+ await writeFileAtomic(path, JSON.stringify(next, null, "\t") + `
6643
6660
  `, "utf8");
6644
6661
  }
6645
6662
  });
@@ -6693,7 +6710,7 @@ var createRAGFileRetrievalBaselineStore = (path) => {
6693
6710
  ...existing.map((entry) => entry.groupKey === record.groupKey && (entry.rolloutLabel ?? undefined) === (record.rolloutLabel ?? undefined) ? { ...entry, status: "superseded" } : entry)
6694
6711
  ]);
6695
6712
  await mkdir(dirname(path), { recursive: true });
6696
- await writeFile(path, JSON.stringify(next, null, "\t") + `
6713
+ await writeFileAtomic(path, JSON.stringify(next, null, "\t") + `
6697
6714
  `, "utf8");
6698
6715
  }
6699
6716
  };
@@ -6732,7 +6749,7 @@ var createRAGFileRetrievalReleaseDecisionStore = (path) => ({
6732
6749
  ...existing.filter((entry) => entry.id !== record.id)
6733
6750
  ]);
6734
6751
  await mkdir(dirname(path), { recursive: true });
6735
- await writeFile(path, JSON.stringify(next, null, "\t") + `
6752
+ await writeFileAtomic(path, JSON.stringify(next, null, "\t") + `
6736
6753
  `, "utf8");
6737
6754
  }
6738
6755
  });
@@ -6776,7 +6793,7 @@ var createRAGFileRetrievalLaneHandoffDecisionStore = (path) => ({
6776
6793
  ...existing.filter((entry) => entry.id !== record.id)
6777
6794
  ]);
6778
6795
  await mkdir(dirname(path), { recursive: true });
6779
- await writeFile(path, JSON.stringify(next, null, "\t") + `
6796
+ await writeFileAtomic(path, JSON.stringify(next, null, "\t") + `
6780
6797
  `, "utf8");
6781
6798
  }
6782
6799
  });
@@ -6821,7 +6838,7 @@ var createRAGFileRetrievalReleaseIncidentStore = (path) => ({
6821
6838
  ...existing.filter((entry) => entry.id !== record.id)
6822
6839
  ]);
6823
6840
  await mkdir(dirname(path), { recursive: true });
6824
- await writeFile(path, JSON.stringify(next, null, "\t") + `
6841
+ await writeFileAtomic(path, JSON.stringify(next, null, "\t") + `
6825
6842
  `, "utf8");
6826
6843
  }
6827
6844
  });
@@ -6865,7 +6882,7 @@ var createRAGFileRetrievalLaneHandoffIncidentStore = (path) => ({
6865
6882
  ...existing.filter((entry) => entry.id !== record.id)
6866
6883
  ]);
6867
6884
  await mkdir(dirname(path), { recursive: true });
6868
- await writeFile(path, JSON.stringify(next, null, "\t") + `
6885
+ await writeFileAtomic(path, JSON.stringify(next, null, "\t") + `
6869
6886
  `, "utf8");
6870
6887
  }
6871
6888
  });
@@ -6905,7 +6922,7 @@ var createRAGFileRetrievalLaneHandoffIncidentHistoryStore = (path) => ({
6905
6922
  })();
6906
6923
  const next = [record, ...existing].sort((left, right) => right.recordedAt - left.recordedAt);
6907
6924
  await mkdir(dirname(path), { recursive: true });
6908
- await writeFile(path, JSON.stringify(next, null, "\t") + `
6925
+ await writeFileAtomic(path, JSON.stringify(next, null, "\t") + `
6909
6926
  `, "utf8");
6910
6927
  }
6911
6928
  });
@@ -6949,7 +6966,7 @@ var createRAGFileRetrievalIncidentRemediationDecisionStore = (path) => ({
6949
6966
  ...existing
6950
6967
  ]);
6951
6968
  await mkdir(dirname(path), { recursive: true });
6952
- await writeFile(path, JSON.stringify(next, null, "\t") + `
6969
+ await writeFileAtomic(path, JSON.stringify(next, null, "\t") + `
6953
6970
  `, "utf8");
6954
6971
  }
6955
6972
  });
@@ -6995,7 +7012,7 @@ var createRAGFileRetrievalIncidentRemediationExecutionHistoryStore = (path) => (
6995
7012
  ...existing
6996
7013
  ]);
6997
7014
  await mkdir(dirname(path), { recursive: true });
6998
- await writeFile(path, JSON.stringify(next, null, "\t") + `
7015
+ await writeFileAtomic(path, JSON.stringify(next, null, "\t") + `
6999
7016
  `, "utf8");
7000
7017
  }
7001
7018
  });
@@ -7029,7 +7046,7 @@ var createRAGFileRetrievalLaneHandoffAutoCompletePolicyHistoryStore = (path) =>
7029
7046
  })();
7030
7047
  const next = [record, ...existing].sort((left, right) => right.recordedAt - left.recordedAt);
7031
7048
  await mkdir(dirname(path), { recursive: true });
7032
- await writeFile(path, JSON.stringify(next, null, "\t") + `
7049
+ await writeFileAtomic(path, JSON.stringify(next, null, "\t") + `
7033
7050
  `, "utf8");
7034
7051
  }
7035
7052
  });
@@ -7063,7 +7080,7 @@ var createRAGFileRetrievalReleaseLanePolicyHistoryStore = (path) => ({
7063
7080
  })();
7064
7081
  const next = [record, ...existing].sort((left, right) => right.recordedAt - left.recordedAt);
7065
7082
  await mkdir(dirname(path), { recursive: true });
7066
- await writeFile(path, JSON.stringify(next, null, "\t") + `
7083
+ await writeFileAtomic(path, JSON.stringify(next, null, "\t") + `
7067
7084
  `, "utf8");
7068
7085
  }
7069
7086
  });
@@ -7097,7 +7114,7 @@ var createRAGFileRetrievalBaselineGatePolicyHistoryStore = (path) => ({
7097
7114
  })();
7098
7115
  const next = [record, ...existing].sort((left, right) => right.recordedAt - left.recordedAt);
7099
7116
  await mkdir(dirname(path), { recursive: true });
7100
- await writeFile(path, JSON.stringify(next, null, "\t") + `
7117
+ await writeFileAtomic(path, JSON.stringify(next, null, "\t") + `
7101
7118
  `, "utf8");
7102
7119
  }
7103
7120
  });
@@ -7131,7 +7148,7 @@ var createRAGFileRetrievalReleaseLaneEscalationPolicyHistoryStore = (path) => ({
7131
7148
  })();
7132
7149
  const next = [record, ...existing].sort((left, right) => right.recordedAt - left.recordedAt);
7133
7150
  await mkdir(dirname(path), { recursive: true });
7134
- await writeFile(path, JSON.stringify(next, null, "\t") + `
7151
+ await writeFileAtomic(path, JSON.stringify(next, null, "\t") + `
7135
7152
  `, "utf8");
7136
7153
  }
7137
7154
  });
@@ -7177,7 +7194,7 @@ var createRAGFileSearchTraceStore = (path) => ({
7177
7194
  ...traces.filter((entry) => entry.id !== trace.id)
7178
7195
  ]);
7179
7196
  await mkdir(dirname(path), { recursive: true });
7180
- await writeFile(path, JSON.stringify({
7197
+ await writeFileAtomic(path, JSON.stringify({
7181
7198
  traces: nextTraces
7182
7199
  }, null, 2));
7183
7200
  },
@@ -7197,7 +7214,7 @@ var createRAGFileSearchTraceStore = (path) => ({
7197
7214
  traces
7198
7215
  });
7199
7216
  await mkdir(dirname(path), { recursive: true });
7200
- await writeFile(path, JSON.stringify({
7217
+ await writeFileAtomic(path, JSON.stringify({
7201
7218
  traces: pruned.next
7202
7219
  }, null, 2));
7203
7220
  return {
@@ -7240,7 +7257,7 @@ var createRAGFileSearchTracePruneHistoryStore = (path) => ({
7240
7257
  ...existing.filter((entry) => entry.id !== run.id)
7241
7258
  ]);
7242
7259
  await mkdir(dirname(path), { recursive: true });
7243
- await writeFile(path, JSON.stringify(next, null, "\t") + `
7260
+ await writeFileAtomic(path, JSON.stringify(next, null, "\t") + `
7244
7261
  `, "utf8");
7245
7262
  }
7246
7263
  });
@@ -8869,7 +8886,7 @@ var createRAGFileAnswerGroundingEvaluationHistoryStore = (path) => ({
8869
8886
  ...runs.filter((entry) => entry.id !== run.id)
8870
8887
  ]);
8871
8888
  await mkdir(dirname(path), { recursive: true });
8872
- await writeFile(path, JSON.stringify({
8889
+ await writeFileAtomic(path, JSON.stringify({
8873
8890
  runs: nextRuns
8874
8891
  }, null, 2));
8875
8892
  },
@@ -8890,7 +8907,7 @@ var createRAGFileAnswerGroundingEvaluationHistoryStore = (path) => ({
8890
8907
  sort: normalizeGroundingHistoryRuns
8891
8908
  });
8892
8909
  await mkdir(dirname(path), { recursive: true });
8893
- await writeFile(path, JSON.stringify({
8910
+ await writeFileAtomic(path, JSON.stringify({
8894
8911
  runs: pruned.next
8895
8912
  }, null, 2));
8896
8913
  return {
@@ -8930,7 +8947,7 @@ var createRAGFileAnswerGroundingCaseDifficultyHistoryStore = (path) => ({
8930
8947
  ...runs.filter((entry) => entry.id !== run.id)
8931
8948
  ]);
8932
8949
  await mkdir(dirname(path), { recursive: true });
8933
- await writeFile(path, JSON.stringify({
8950
+ await writeFileAtomic(path, JSON.stringify({
8934
8951
  runs: nextRuns
8935
8952
  }, null, 2));
8936
8953
  }
@@ -21676,6 +21693,7 @@ var MAX_INGEST_JOBS = 20;
21676
21693
  var MAX_ADMIN_ACTIONS = 20;
21677
21694
  var MAX_ADMIN_JOBS = 20;
21678
21695
  var DEFAULT_STALE_AFTER_MS = 1000 * 60 * 60 * 24 * 7;
21696
+ var REQUEST_USER_SUB_HEADER = "x-absolutejs-user-sub";
21679
21697
  var HTML_HEADERS = { "Content-Type": "text/html; charset=utf-8" };
21680
21698
  var defaultParseProvider = (content) => {
21681
21699
  const colonIdx = content.indexOf(":");
@@ -21689,6 +21707,10 @@ var defaultParseProvider = (content) => {
21689
21707
  var normalizeScore = (value) => Number.isFinite(value) ? value : 0;
21690
21708
  var isHTMXRequest = (request) => request.headers.get("HX-Request") === "true";
21691
21709
  var isObjectRecord3 = (value) => Boolean(value) && typeof value === "object";
21710
+ var resolveRequestUserSub = (request) => {
21711
+ const value = request?.headers.get(REQUEST_USER_SUB_HEADER)?.trim();
21712
+ return value ? value : undefined;
21713
+ };
21692
21714
  var getStringProperty = (value, key) => {
21693
21715
  if (!isObjectRecord3(value)) {
21694
21716
  return;
@@ -22528,11 +22550,12 @@ var ragChat = (config) => {
22528
22550
  throw error;
22529
22551
  }
22530
22552
  };
22531
- const buildSyncSources = async (scope) => {
22553
+ const buildSyncSources = async (scope, request) => {
22532
22554
  if (!indexManager?.listSyncSources) {
22533
22555
  return [];
22534
22556
  }
22535
- const sources = await indexManager.listSyncSources();
22557
+ const userSub = resolveRequestUserSub(request);
22558
+ const sources = await indexManager.listSyncSources(userSub ? { userSub } : undefined);
22536
22559
  return sources.filter((source) => matchesSyncSourceScope(scope, source));
22537
22560
  };
22538
22561
  const toHTMXResponse = (html, status, extraHeaders) => new Response(html, {
@@ -28746,7 +28769,7 @@ var ragChat = (config) => {
28746
28769
  stats: traceStats
28747
28770
  },
28748
28771
  status,
28749
- syncSources: await buildSyncSources(accessScope)
28772
+ syncSources: await buildSyncSources(accessScope, request)
28750
28773
  };
28751
28774
  };
28752
28775
  const handleStatus = async (request) => buildOperationsPayload(request);
@@ -29208,7 +29231,7 @@ var ragChat = (config) => {
29208
29231
  const accessScope = await loadAccessScope(request);
29209
29232
  return {
29210
29233
  ok: true,
29211
- sources: await buildSyncSources(accessScope)
29234
+ sources: await buildSyncSources(accessScope, request)
29212
29235
  };
29213
29236
  };
29214
29237
  const handleSyncAllSources = async (request, options) => {
@@ -29228,7 +29251,10 @@ var ragChat = (config) => {
29228
29251
  const job = createAdminJob("sync_all_sources", undefined, syncJobs);
29229
29252
  const action = createAdminAction("sync_all_sources");
29230
29253
  try {
29231
- const result = await indexManager.syncAllSources(options);
29254
+ const result = await indexManager.syncAllSources({
29255
+ ...options ?? {},
29256
+ userSub: resolveRequestUserSub(request)
29257
+ });
29232
29258
  if (result && "ok" in result) {
29233
29259
  if (!result.ok) {
29234
29260
  failAdminJob(job, result.error);
@@ -29250,7 +29276,7 @@ var ragChat = (config) => {
29250
29276
  completeAdminAction(action);
29251
29277
  return {
29252
29278
  ok: true,
29253
- sources: await buildSyncSources(accessScope)
29279
+ sources: await buildSyncSources(accessScope, request)
29254
29280
  };
29255
29281
  } catch (caught) {
29256
29282
  const message = caught instanceof Error ? caught.message : String(caught);
@@ -29282,7 +29308,10 @@ var ragChat = (config) => {
29282
29308
  const job = createAdminJob("sync_source", id, syncJobs);
29283
29309
  const action = createAdminAction("sync_source", undefined, id);
29284
29310
  try {
29285
- const result = await indexManager.syncSource(id, options);
29311
+ const result = await indexManager.syncSource(id, {
29312
+ ...options ?? {},
29313
+ userSub: resolveRequestUserSub(request)
29314
+ });
29286
29315
  if (result && "ok" in result) {
29287
29316
  if (!result.ok) {
29288
29317
  failAdminJob(job, result.error);
@@ -29295,7 +29324,7 @@ var ragChat = (config) => {
29295
29324
  }
29296
29325
  completeAdminJob(job);
29297
29326
  completeAdminAction(action);
29298
- const source = (await buildSyncSources(accessScope)).find((record) => record.id === id);
29327
+ const source = (await buildSyncSources(accessScope, request)).find((record) => record.id === id);
29299
29328
  return source ? { ok: true, source } : {
29300
29329
  error: "sync source not found",
29301
29330
  ok: false
@@ -32125,7 +32154,7 @@ var createRAGInstagramBusinessConnector = (input) => ({
32125
32154
  // src/ai/rag/sync.ts
32126
32155
  var {S3Client } = globalThis.Bun;
32127
32156
  import { createHash } from "crypto";
32128
- import { mkdir as mkdir2, readFile as readFile3, readdir as readdir2, writeFile as writeFile2 } from "fs/promises";
32157
+ import { mkdir as mkdir2, readFile as readFile3, readdir as readdir2 } from "fs/promises";
32129
32158
  import { basename as basename2, dirname as dirname2, extname as extname2, join as join2, relative as relative2, resolve as resolve2 } from "path";
32130
32159
  var toSyncError = (caught) => caught instanceof Error ? caught.message : String(caught);
32131
32160
  var wait = async (delayMs) => {
@@ -34624,7 +34653,7 @@ var createRAGFileSyncStateStore = (path) => {
34624
34653
  },
34625
34654
  save: async (records) => {
34626
34655
  await mkdir2(dirname2(resolvedPath), { recursive: true });
34627
- await writeFile2(resolvedPath, JSON.stringify(records, null, 2), "utf8");
34656
+ await writeFileAtomic(resolvedPath, JSON.stringify(records, null, 2), "utf8");
34628
34657
  }
34629
34658
  };
34630
34659
  };
@@ -34671,7 +34700,7 @@ var createRAGSyncScheduler = (input) => {
34671
34700
  };
34672
34701
  };
34673
34702
  // src/ai/rag/jobState.ts
34674
- import { mkdir as mkdir3, readFile as readFile4, writeFile as writeFile3 } from "fs/promises";
34703
+ import { mkdir as mkdir3, readFile as readFile4 } from "fs/promises";
34675
34704
  import { dirname as dirname3, resolve as resolve3 } from "path";
34676
34705
  var parseJobState = (content) => {
34677
34706
  try {
@@ -34708,7 +34737,7 @@ var createRAGFileJobStateStore = (path) => {
34708
34737
  },
34709
34738
  save: async (state) => {
34710
34739
  await mkdir3(dirname3(resolvedPath), { recursive: true });
34711
- await writeFile3(resolvedPath, JSON.stringify(state, null, 2), "utf8");
34740
+ await writeFileAtomic(resolvedPath, JSON.stringify(state, null, 2), "utf8");
34712
34741
  }
34713
34742
  };
34714
34743
  };
@@ -37462,5 +37491,5 @@ export {
37462
37491
  addRAGEvaluationSuiteCase
37463
37492
  };
37464
37493
 
37465
- //# debugId=8A6927B24E3DD10164756E2164756E21
37494
+ //# debugId=26E70B5CD574E08C64756E2164756E21
37466
37495
  //# sourceMappingURL=index.js.map