@aj-archipelago/cortex 1.3.2 → 1.3.4

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aj-archipelago/cortex",
3
- "version": "1.3.2",
3
+ "version": "1.3.4",
4
4
  "description": "Cortex is a GraphQL API for AI. It provides a simple, extensible interface for using AI services from OpenAI, Azure and others.",
5
5
  "private": false,
6
6
  "repository": {
@@ -33,7 +33,7 @@ const modifyText = (text, modifications) => {
33
33
  return modifiedText;
34
34
  };
35
35
 
36
- const enforceTokenLimit = (text, maxTokens = 5000, isTopicsSection = false) => {
36
+ export const enforceTokenLimit = (text, maxTokens = 5000, isTopicsSection = false) => {
37
37
  if (!text) return text;
38
38
 
39
39
  const lines = text.split('\n')
@@ -77,7 +77,7 @@ const enforceTokenLimit = (text, maxTokens = 5000, isTopicsSection = false) => {
77
77
  return true;
78
78
  });
79
79
 
80
- prioritizedLines.sort((a, b) => a.priority - b.priority);
80
+ prioritizedLines.sort((a, b) => b.priority - a.priority);
81
81
 
82
82
  let tokens = encode(prioritizedLines.map(x => x.line).join('\n')).length;
83
83
  let safetyCounter = 0;
@@ -0,0 +1,64 @@
1
+ import test from 'ava';
2
+ import { enforceTokenLimit } from '../pathways/system/entity/memory/sys_memory_update.js';
3
+
4
+ test('enforceTokenLimit preserves priority order correctly', t => {
5
+ const input = `
6
+ [P1] Highest priority item
7
+ [P2] High priority item
8
+ [P3] Medium priority item
9
+ [P4] Low priority item
10
+ [P5] Lowest priority item`.trim();
11
+
12
+ // Enforce trimming
13
+ const result = enforceTokenLimit(input, 20);
14
+
15
+ // Should keep P1, P2 only
16
+ t.true(result.includes('[P1]'));
17
+ t.true(result.includes('[P2]'));
18
+ t.false(result.includes('[P3]'));
19
+ t.false(result.includes('[P4]'));
20
+ t.false(result.includes('[P5]'));
21
+ });
22
+
23
+ test('enforceTokenLimit handles empty input', t => {
24
+ t.is(enforceTokenLimit(''), '');
25
+ t.is(enforceTokenLimit(null), null);
26
+ });
27
+
28
+ test('enforceTokenLimit removes duplicates', t => {
29
+ const input = `
30
+ [P3] Duplicate item
31
+ [P3] Duplicate item
32
+ [P2] Unique item`.trim();
33
+
34
+ const result = enforceTokenLimit(input);
35
+
36
+ // Count occurrences of "Duplicate item"
37
+ const matches = result.match(/Duplicate item/g) || [];
38
+ t.is(matches.length, 1);
39
+ });
40
+
41
+ test('enforceTokenLimit handles topics section differently', t => {
42
+ const input = `
43
+ 2024-03-19T10:00:00Z Discussed AI ethics
44
+ 2024-03-19T11:00:00Z Discussed programming
45
+ 2024-03-19T12:00:00Z Discussed testing`.trim();
46
+
47
+ const result = enforceTokenLimit(input, 20, true);
48
+
49
+ // For topics, should remove oldest entries first regardless of content
50
+ t.false(result.includes('10:00:00Z'));
51
+ t.false(result.includes('11:00:00Z'));
52
+ t.true(result.includes('12:00:00Z'));
53
+ });
54
+
55
+ test('enforceTokenLimit adds P3 to unprioritized lines', t => {
56
+ const input = `
57
+ Item without priority
58
+ [P1] Item with priority`.trim();
59
+
60
+ const result = enforceTokenLimit(input);
61
+
62
+ t.true(result.includes('[P3] Item without priority'));
63
+ t.true(result.includes('[P1] Item with priority'));
64
+ });