letagents 0.12.5 → 0.12.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.
@@ -177,6 +177,71 @@ export function setStoredAgentIdentity(agentIdentity, identityKey) {
177
177
  });
178
178
  return agentIdentity;
179
179
  }
180
+ export function getStoredAgentSession(sessionId) {
181
+ if (!sessionId) {
182
+ return null;
183
+ }
184
+ const state = readLocalState();
185
+ return state.agent_sessions?.[sessionId] ?? null;
186
+ }
187
+ export function getCurrentAgentSession(roomId) {
188
+ const state = readLocalState();
189
+ const sessionIds = state.current_agent_session_ids;
190
+ if (!sessionIds) {
191
+ return null;
192
+ }
193
+ if (roomId) {
194
+ const sessionId = sessionIds[roomId];
195
+ const session = sessionId ? (state.agent_sessions?.[sessionId] ?? null) : null;
196
+ return session && !session.ended_at ? session : null;
197
+ }
198
+ let best = null;
199
+ for (const id of Object.values(sessionIds)) {
200
+ const session = state.agent_sessions?.[id];
201
+ if (session && !session.ended_at && (!best || session.updated_at > best.updated_at)) {
202
+ best = session;
203
+ }
204
+ }
205
+ return best;
206
+ }
207
+ export function saveAgentSession(session, makeCurrent = true) {
208
+ updateLocalState((state) => {
209
+ state.agent_sessions = state.agent_sessions ?? {};
210
+ state.agent_sessions[session.session_id] = session;
211
+ if (makeCurrent) {
212
+ state.current_agent_session_ids = state.current_agent_session_ids ?? {};
213
+ state.current_agent_session_ids[session.room_id] = session.session_id;
214
+ }
215
+ return state;
216
+ });
217
+ return session;
218
+ }
219
+ export function endStoredAgentSession(sessionId, endedAt = new Date().toISOString()) {
220
+ let endedSession = null;
221
+ updateLocalState((state) => {
222
+ const session = state.agent_sessions?.[sessionId];
223
+ if (!session) {
224
+ return state;
225
+ }
226
+ endedSession = {
227
+ ...session,
228
+ ended_at: endedAt,
229
+ updated_at: endedAt,
230
+ last_seen_at: endedAt,
231
+ };
232
+ state.agent_sessions = state.agent_sessions ?? {};
233
+ state.agent_sessions[sessionId] = endedSession;
234
+ if (state.current_agent_session_ids) {
235
+ for (const [roomId, currentSessionId] of Object.entries(state.current_agent_session_ids)) {
236
+ if (currentSessionId === sessionId) {
237
+ delete state.current_agent_session_ids[roomId];
238
+ }
239
+ }
240
+ }
241
+ return state;
242
+ });
243
+ return endedSession;
244
+ }
180
245
  export function getStoredCurrentRoom() {
181
246
  const state = readLocalState();
182
247
  return state.current_room ?? null;