@agenr/agenr-plugin 1.9.3 → 2.0.0

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,176 +1,3 @@
1
- // src/core/recall/scoring.ts
2
- var DAY_IN_MILLISECONDS = 1e3 * 60 * 60 * 24;
3
- var IMPORTANCE_FLOOR = 0.4;
4
- var RELEVANCE_WEIGHT = 0.5;
5
- var RECENCY_WEIGHT = 0.25;
6
- var IMPORTANCE_WEIGHT = 0.25;
7
- function recencyScore(createdAt, expiry, now = /* @__PURE__ */ new Date()) {
8
- if (expiry === "core") {
9
- return 1;
10
- }
11
- const createdDate = asValidDate(createdAt);
12
- const nowDate = asValidDate(now);
13
- if (!createdDate || !nowDate) {
14
- return 0;
15
- }
16
- const halfLifeDays = expiry === "permanent" ? 365 : 30;
17
- const daysOld = Math.max(0, (nowDate.getTime() - createdDate.getTime()) / DAY_IN_MILLISECONDS);
18
- return clampUnit(Math.pow(0.5, daysOld / halfLifeDays));
19
- }
20
- function gaussianRecency(createdAt, aroundDate, radiusDays) {
21
- const createdDate = asValidDate(createdAt);
22
- const anchorDate = asValidDate(aroundDate);
23
- const normalizedRadius = sanitizeNonNegative(radiusDays);
24
- if (!createdDate || !anchorDate) {
25
- return 0;
26
- }
27
- if (normalizedRadius <= 0) {
28
- return createdDate.getTime() === anchorDate.getTime() ? 1 : 0;
29
- }
30
- const daysDelta = Math.abs(createdDate.getTime() - anchorDate.getTime()) / DAY_IN_MILLISECONDS;
31
- return clampUnit(Math.exp(-0.5 * (daysDelta / normalizedRadius) ** 2));
32
- }
33
- function importanceScore(importance) {
34
- const clampedImportance = clampRange(sanitizeNonNegative(importance), 1, 10);
35
- return clampUnit(IMPORTANCE_FLOOR + (clampedImportance - 1) / 9 * (1 - IMPORTANCE_FLOOR));
36
- }
37
- function combinedRelevance(vectorSim, lexical) {
38
- const normalizedVector = clampUnit(sanitizeNonNegative(vectorSim));
39
- const normalizedLexical = clampUnit(sanitizeNonNegative(lexical));
40
- if (normalizedVector > 0 && normalizedLexical > 0) {
41
- return clampUnit(normalizedVector * 0.6 + normalizedLexical * 0.4);
42
- }
43
- return Math.max(normalizedVector, normalizedLexical);
44
- }
45
- function scoreCandidate(params) {
46
- const vector = clampUnit(sanitizeNonNegative(params.vectorSim));
47
- const lexical = clampUnit(sanitizeNonNegative(params.lexical));
48
- const recency = clampUnit(sanitizeNonNegative(params.recency));
49
- const importance = clampUnit(sanitizeNonNegative(params.importance));
50
- const relevance = combinedRelevance(vector, lexical);
51
- const score = clampUnit(relevance * RELEVANCE_WEIGHT + recency * RECENCY_WEIGHT + importance * IMPORTANCE_WEIGHT);
52
- return {
53
- score,
54
- scores: {
55
- relevance,
56
- vector,
57
- lexical,
58
- recency,
59
- importance
60
- }
61
- };
62
- }
63
- function cosineSimilarity(left, right) {
64
- const size = Math.min(left.length, right.length);
65
- if (size === 0) {
66
- return 0;
67
- }
68
- let dot = 0;
69
- let leftNorm = 0;
70
- let rightNorm = 0;
71
- for (let index = 0; index < size; index += 1) {
72
- const leftValue = sanitizeFinite(left[index]);
73
- const rightValue = sanitizeFinite(right[index]);
74
- dot += leftValue * rightValue;
75
- leftNorm += leftValue * leftValue;
76
- rightNorm += rightValue * rightValue;
77
- }
78
- if (leftNorm <= 0 || rightNorm <= 0) {
79
- return 0;
80
- }
81
- return clampUnit(dot / (Math.sqrt(leftNorm) * Math.sqrt(rightNorm)));
82
- }
83
- var asValidDate = (value) => {
84
- const date = value instanceof Date ? new Date(value.getTime()) : new Date(value);
85
- return Number.isNaN(date.getTime()) ? null : date;
86
- };
87
- var clampUnit = (value) => clampRange(sanitizeNonNegative(value), 0, 1);
88
- var clampRange = (value, min, max) => Math.min(max, Math.max(min, value));
89
- var sanitizeFinite = (value) => typeof value === "number" && Number.isFinite(value) ? value : 0;
90
- var sanitizeNonNegative = (value) => Math.max(0, sanitizeFinite(value));
91
-
92
- // src/core/recall/temporal.ts
93
- var DAY_IN_MILLISECONDS2 = 1e3 * 60 * 60 * 24;
94
- var MONTH_INDEX = /* @__PURE__ */ new Map([
95
- ["january", 0],
96
- ["february", 1],
97
- ["march", 2],
98
- ["april", 3],
99
- ["may", 4],
100
- ["june", 5],
101
- ["july", 6],
102
- ["august", 7],
103
- ["september", 8],
104
- ["october", 9],
105
- ["november", 10],
106
- ["december", 11]
107
- ]);
108
- function inferAroundDate(text, now = /* @__PURE__ */ new Date()) {
109
- const normalized = text.trim().toLowerCase();
110
- const referenceNow = asValidDate2(now);
111
- if (normalized.length === 0 || !referenceNow) {
112
- return null;
113
- }
114
- let inferred = null;
115
- if (/\byesterday\b/.test(normalized)) {
116
- inferred = offsetDays(referenceNow, 1);
117
- } else if (/\blast week\b/.test(normalized)) {
118
- inferred = offsetDays(referenceNow, 7);
119
- } else if (/\blast month\b/.test(normalized)) {
120
- inferred = offsetDays(referenceNow, 30);
121
- } else if (/\blast year\b/.test(normalized)) {
122
- inferred = offsetDays(referenceNow, 365);
123
- } else if (/\bthis week\b/.test(normalized)) {
124
- inferred = offsetDays(referenceNow, 3);
125
- } else if (/\bthis month\b/.test(normalized)) {
126
- inferred = offsetDays(referenceNow, 15);
127
- } else {
128
- const relativeMatch = normalized.match(/\b(\d+)\s+(day|days|week|weeks|month|months)\s+ago\b/);
129
- if (relativeMatch) {
130
- const amount = Number(relativeMatch[1]);
131
- const unit = relativeMatch[2];
132
- const multiplier = unit?.startsWith("week") ? 7 : unit?.startsWith("month") ? 30 : 1;
133
- inferred = Number.isFinite(amount) ? offsetDays(referenceNow, amount * multiplier) : null;
134
- } else {
135
- const monthMatch = normalized.match(/\bin\s+(january|february|march|april|may|june|july|august|september|october|november|december)\b/);
136
- if (monthMatch?.[1]) {
137
- inferred = inferMonthAnchor(monthMatch[1], referenceNow);
138
- }
139
- }
140
- }
141
- if (!inferred) {
142
- return null;
143
- }
144
- return inferred.getTime() > referenceNow.getTime() ? new Date(referenceNow.getTime()) : inferred;
145
- }
146
- function parseRelativeDate(input, now = /* @__PURE__ */ new Date()) {
147
- const trimmed = input.trim();
148
- const referenceNow = asValidDate2(now);
149
- if (trimmed.length === 0 || !referenceNow) {
150
- return null;
151
- }
152
- const durationMatch = trimmed.match(/^(\d+)d$/i);
153
- if (durationMatch?.[1]) {
154
- const days = Number(durationMatch[1]);
155
- return Number.isFinite(days) ? offsetDays(referenceNow, days) : null;
156
- }
157
- const parsed = new Date(trimmed);
158
- return Number.isNaN(parsed.getTime()) ? null : parsed;
159
- }
160
- var asValidDate2 = (value) => {
161
- const date = new Date(value.getTime());
162
- return Number.isNaN(date.getTime()) ? null : date;
163
- };
164
- var offsetDays = (date, days) => new Date(date.getTime() - days * DAY_IN_MILLISECONDS2);
165
- var inferMonthAnchor = (monthName, now) => {
166
- const monthIndex = MONTH_INDEX.get(monthName);
167
- if (monthIndex === void 0) {
168
- return null;
169
- }
170
- const year = monthIndex <= now.getUTCMonth() ? now.getUTCFullYear() : now.getUTCFullYear() - 1;
171
- return new Date(Date.UTC(year, monthIndex, 15));
172
- };
173
-
174
1
  // src/core/recall/lexical.ts
175
2
  var STOP_WORDS = /* @__PURE__ */ new Set([
176
3
  "the",
@@ -361,6 +188,97 @@ var hasConsecutivePhrase = (haystack, needle) => {
361
188
  return false;
362
189
  };
363
190
 
191
+ // src/core/recall/scoring.ts
192
+ var DAY_IN_MILLISECONDS = 1e3 * 60 * 60 * 24;
193
+ var IMPORTANCE_FLOOR = 0.4;
194
+ var RELEVANCE_WEIGHT = 0.5;
195
+ var RECENCY_WEIGHT = 0.25;
196
+ var IMPORTANCE_WEIGHT = 0.25;
197
+ function recencyScore(createdAt, expiry, now = /* @__PURE__ */ new Date()) {
198
+ if (expiry === "core") {
199
+ return 1;
200
+ }
201
+ const createdDate = asValidDate(createdAt);
202
+ const nowDate = asValidDate(now);
203
+ if (!createdDate || !nowDate) {
204
+ return 0;
205
+ }
206
+ const halfLifeDays = expiry === "permanent" ? 365 : 30;
207
+ const daysOld = Math.max(0, (nowDate.getTime() - createdDate.getTime()) / DAY_IN_MILLISECONDS);
208
+ return clampUnit(Math.pow(0.5, daysOld / halfLifeDays));
209
+ }
210
+ function gaussianRecency(createdAt, aroundDate, radiusDays) {
211
+ const createdDate = asValidDate(createdAt);
212
+ const anchorDate = asValidDate(aroundDate);
213
+ const normalizedRadius = sanitizeNonNegative(radiusDays);
214
+ if (!createdDate || !anchorDate) {
215
+ return 0;
216
+ }
217
+ if (normalizedRadius <= 0) {
218
+ return createdDate.getTime() === anchorDate.getTime() ? 1 : 0;
219
+ }
220
+ const daysDelta = Math.abs(createdDate.getTime() - anchorDate.getTime()) / DAY_IN_MILLISECONDS;
221
+ return clampUnit(Math.exp(-0.5 * (daysDelta / normalizedRadius) ** 2));
222
+ }
223
+ function importanceScore(importance) {
224
+ const clampedImportance = clampRange(sanitizeNonNegative(importance), 1, 10);
225
+ return clampUnit(IMPORTANCE_FLOOR + (clampedImportance - 1) / 9 * (1 - IMPORTANCE_FLOOR));
226
+ }
227
+ function combinedRelevance(vectorSim, lexical) {
228
+ const normalizedVector = clampUnit(sanitizeNonNegative(vectorSim));
229
+ const normalizedLexical = clampUnit(sanitizeNonNegative(lexical));
230
+ if (normalizedVector > 0 && normalizedLexical > 0) {
231
+ return clampUnit(normalizedVector * 0.6 + normalizedLexical * 0.4);
232
+ }
233
+ return Math.max(normalizedVector, normalizedLexical);
234
+ }
235
+ function scoreCandidate(params) {
236
+ const vector = clampUnit(sanitizeNonNegative(params.vectorSim));
237
+ const lexical = clampUnit(sanitizeNonNegative(params.lexical));
238
+ const recency = clampUnit(sanitizeNonNegative(params.recency));
239
+ const importance = clampUnit(sanitizeNonNegative(params.importance));
240
+ const relevance = combinedRelevance(vector, lexical);
241
+ const score = clampUnit(relevance * RELEVANCE_WEIGHT + recency * RECENCY_WEIGHT + importance * IMPORTANCE_WEIGHT);
242
+ return {
243
+ score,
244
+ scores: {
245
+ relevance,
246
+ vector,
247
+ lexical,
248
+ recency,
249
+ importance
250
+ }
251
+ };
252
+ }
253
+ function cosineSimilarity(left, right) {
254
+ const size = Math.min(left.length, right.length);
255
+ if (size === 0) {
256
+ return 0;
257
+ }
258
+ let dot = 0;
259
+ let leftNorm = 0;
260
+ let rightNorm = 0;
261
+ for (let index = 0; index < size; index += 1) {
262
+ const leftValue = sanitizeFinite(left[index]);
263
+ const rightValue = sanitizeFinite(right[index]);
264
+ dot += leftValue * rightValue;
265
+ leftNorm += leftValue * leftValue;
266
+ rightNorm += rightValue * rightValue;
267
+ }
268
+ if (leftNorm <= 0 || rightNorm <= 0) {
269
+ return 0;
270
+ }
271
+ return clampUnit(dot / (Math.sqrt(leftNorm) * Math.sqrt(rightNorm)));
272
+ }
273
+ var asValidDate = (value) => {
274
+ const date = value instanceof Date ? new Date(value.getTime()) : new Date(value);
275
+ return Number.isNaN(date.getTime()) ? null : date;
276
+ };
277
+ var clampUnit = (value) => clampRange(sanitizeNonNegative(value), 0, 1);
278
+ var clampRange = (value, min, max) => Math.min(max, Math.max(min, value));
279
+ var sanitizeFinite = (value) => typeof value === "number" && Number.isFinite(value) ? value : 0;
280
+ var sanitizeNonNegative = (value) => Math.max(0, sanitizeFinite(value));
281
+
364
282
  // src/core/claim-key.ts
365
283
  var UNKNOWN_SEGMENT = "unknown";
366
284
  var SELF_REFERENTIAL_ENTITIES = /* @__PURE__ */ new Set(["i", "me", "myself", "the_user", "user", "we", "our_team", "the_project", "this_project"]);
@@ -877,6 +795,88 @@ function resolveConfiguredAttributeHeadPolicy(attributeHead, config) {
877
795
  return config?.attributeHeads?.[loweredAttributeHead];
878
796
  }
879
797
 
798
+ // src/core/recall/temporal.ts
799
+ var DAY_IN_MILLISECONDS2 = 1e3 * 60 * 60 * 24;
800
+ var MONTH_INDEX = /* @__PURE__ */ new Map([
801
+ ["january", 0],
802
+ ["february", 1],
803
+ ["march", 2],
804
+ ["april", 3],
805
+ ["may", 4],
806
+ ["june", 5],
807
+ ["july", 6],
808
+ ["august", 7],
809
+ ["september", 8],
810
+ ["october", 9],
811
+ ["november", 10],
812
+ ["december", 11]
813
+ ]);
814
+ function inferAroundDate(text, now = /* @__PURE__ */ new Date()) {
815
+ const normalized = text.trim().toLowerCase();
816
+ const referenceNow = asValidDate2(now);
817
+ if (normalized.length === 0 || !referenceNow) {
818
+ return null;
819
+ }
820
+ let inferred = null;
821
+ if (/\byesterday\b/.test(normalized)) {
822
+ inferred = offsetDays(referenceNow, 1);
823
+ } else if (/\blast week\b/.test(normalized)) {
824
+ inferred = offsetDays(referenceNow, 7);
825
+ } else if (/\blast month\b/.test(normalized)) {
826
+ inferred = offsetDays(referenceNow, 30);
827
+ } else if (/\blast year\b/.test(normalized)) {
828
+ inferred = offsetDays(referenceNow, 365);
829
+ } else if (/\bthis week\b/.test(normalized)) {
830
+ inferred = offsetDays(referenceNow, 3);
831
+ } else if (/\bthis month\b/.test(normalized)) {
832
+ inferred = offsetDays(referenceNow, 15);
833
+ } else {
834
+ const relativeMatch = normalized.match(/\b(\d+)\s+(day|days|week|weeks|month|months)\s+ago\b/);
835
+ if (relativeMatch) {
836
+ const amount = Number(relativeMatch[1]);
837
+ const unit = relativeMatch[2];
838
+ const multiplier = unit?.startsWith("week") ? 7 : unit?.startsWith("month") ? 30 : 1;
839
+ inferred = Number.isFinite(amount) ? offsetDays(referenceNow, amount * multiplier) : null;
840
+ } else {
841
+ const monthMatch = normalized.match(/\bin\s+(january|february|march|april|may|june|july|august|september|october|november|december)\b/);
842
+ if (monthMatch?.[1]) {
843
+ inferred = inferMonthAnchor(monthMatch[1], referenceNow);
844
+ }
845
+ }
846
+ }
847
+ if (!inferred) {
848
+ return null;
849
+ }
850
+ return inferred.getTime() > referenceNow.getTime() ? new Date(referenceNow.getTime()) : inferred;
851
+ }
852
+ function parseRelativeDate(input, now = /* @__PURE__ */ new Date()) {
853
+ const trimmed = input.trim();
854
+ const referenceNow = asValidDate2(now);
855
+ if (trimmed.length === 0 || !referenceNow) {
856
+ return null;
857
+ }
858
+ const durationMatch = trimmed.match(/^(\d+)d$/i);
859
+ if (durationMatch?.[1]) {
860
+ const days = Number(durationMatch[1]);
861
+ return Number.isFinite(days) ? offsetDays(referenceNow, days) : null;
862
+ }
863
+ const parsed = new Date(trimmed);
864
+ return Number.isNaN(parsed.getTime()) ? null : parsed;
865
+ }
866
+ var asValidDate2 = (value) => {
867
+ const date = new Date(value.getTime());
868
+ return Number.isNaN(date.getTime()) ? null : date;
869
+ };
870
+ var offsetDays = (date, days) => new Date(date.getTime() - days * DAY_IN_MILLISECONDS2);
871
+ var inferMonthAnchor = (monthName, now) => {
872
+ const monthIndex = MONTH_INDEX.get(monthName);
873
+ if (monthIndex === void 0) {
874
+ return null;
875
+ }
876
+ const year = monthIndex <= now.getUTCMonth() ? now.getUTCFullYear() : now.getUTCFullYear() - 1;
877
+ return new Date(Date.UTC(year, monthIndex, 15));
878
+ };
879
+
880
880
  // src/core/recall/trace.ts
881
881
  var NOOP_RECALL_TRACE_SINK = {
882
882
  reportSummary() {
@@ -1507,11 +1507,11 @@ export {
1507
1507
  describeClaimKeyNormalizationFailure,
1508
1508
  describeExtractedClaimKeyRejection,
1509
1509
  describeClaimKeySuspicion,
1510
- inferAroundDate,
1511
- parseRelativeDate,
1512
1510
  tokenize,
1513
1511
  buildLexicalPlan,
1514
1512
  computeLexicalScore,
1513
+ inferAroundDate,
1514
+ parseRelativeDate,
1515
1515
  resolveClaimSlotPolicy,
1516
1516
  recall
1517
1517
  };