@chrysb/alphaclaw 0.8.1-beta.4 → 0.8.1-beta.5

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.
@@ -4,7 +4,11 @@ import {
4
4
  kAgentSessionsCacheKey,
5
5
  kAgentLastSessionKey,
6
6
  } from "../lib/storage-keys.js";
7
- import { getSessionRowKey, isDestinationSessionKey } from "../lib/session-keys.js";
7
+ import {
8
+ getSessionRowKey,
9
+ isDestinationSessionKey,
10
+ sortSessionsByPriority,
11
+ } from "../lib/session-keys.js";
8
12
 
9
13
  const readCachedSessions = () => {
10
14
  try {
@@ -115,7 +119,7 @@ export const useAgentSessions = ({ enabled = false, filter } = {}) => {
115
119
  }, [enabled]);
116
120
 
117
121
  const sessions = useMemo(
118
- () => (filter ? allSessions.filter(filter) : allSessions),
122
+ () => sortSessionsByPriority(filter ? allSessions.filter(filter) : allSessions),
119
123
  [allSessions, filter],
120
124
  );
121
125
 
@@ -24,6 +24,26 @@ export const kDestinationSessionFilter = (sessionRow) =>
24
24
  String(sessionRow?.replyTo || "").trim()
25
25
  ) || isDestinationSessionKey(getSessionRowKey(sessionRow));
26
26
 
27
+ const kSessionPriority = {
28
+ destination: 0,
29
+ other: 1,
30
+ };
31
+
32
+ export const getSessionPriority = (sessionRow = null) =>
33
+ isDestinationSessionKey(getSessionRowKey(sessionRow))
34
+ ? kSessionPriority.destination
35
+ : kSessionPriority.other;
36
+
37
+ export const sortSessionsByPriority = (sessions = []) =>
38
+ [...(Array.isArray(sessions) ? sessions : [])].sort((leftRow, rightRow) => {
39
+ const priorityDiff = getSessionPriority(leftRow) - getSessionPriority(rightRow);
40
+ if (priorityDiff !== 0) return priorityDiff;
41
+ const updatedAtDiff =
42
+ Number(rightRow?.updatedAt || 0) - Number(leftRow?.updatedAt || 0);
43
+ if (updatedAtDiff !== 0) return updatedAtDiff;
44
+ return getSessionRowKey(leftRow).localeCompare(getSessionRowKey(rightRow));
45
+ });
46
+
27
47
  export const getDestinationFromSession = (sessionRow = null) => {
28
48
  const channel = String(sessionRow?.replyChannel || "").trim();
29
49
  const to = String(sessionRow?.replyTo || "").trim();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chrysb/alphaclaw",
3
- "version": "0.8.1-beta.4",
3
+ "version": "0.8.1-beta.5",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },