xiaozuoassistant 0.1.71 → 0.1.73

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.
@@ -63,6 +63,8 @@ export class ShortTermMemory {
63
63
  async loadSessionsFromDisk() {
64
64
  try {
65
65
  const data = await fs.readJson(INDEX_FILE);
66
+ // Log loaded data count for debugging
67
+ console.log(`[ShortTermMemory:${this.instanceId}] Loaded ${Array.isArray(data) ? data.length : 0} sessions from disk.`);
66
68
  this.sessions.clear();
67
69
  if (Array.isArray(data)) {
68
70
  for (const meta of data) {
@@ -73,7 +75,7 @@ export class ShortTermMemory {
73
75
  }
74
76
  }
75
77
  catch (e) {
76
- console.error('[ShortTermMemory] Failed to load index.json:', e);
78
+ console.error(`[ShortTermMemory:${this.instanceId}] Failed to load index.json:`, e);
77
79
  // 此时保持 sessions 为空或之前的状态
78
80
  }
79
81
  }
@@ -166,11 +168,13 @@ export class ShortTermMemory {
166
168
  const sessionDir = this.getSessionDir(id);
167
169
  await fs.ensureDir(sessionDir);
168
170
  await fs.writeJson(this.getMessagesFile(id), [], { spaces: 2 });
171
+ // IMPORTANT: Reload from disk BEFORE adding new session to avoid overwriting other processes' changes
172
+ await this.loadSessionsFromDisk();
169
173
  // 更新内存
170
174
  this.sessions.set(id, meta);
171
175
  // 异步持久化
172
176
  await this.persistIndex();
173
- console.log(`[ShortTermMemory] Session created: ${id}`);
177
+ console.log(`[ShortTermMemory:${this.instanceId}] Session created and persisted: ${id}`);
174
178
  return { meta, messages: [] };
175
179
  }
176
180
  async listSessions() {
@@ -286,6 +290,11 @@ export class ShortTermMemory {
286
290
  }
287
291
  async createRun(sessionId, userContent, runId) {
288
292
  await this.ensureReady();
293
+ // Check if session exists, try reload if not
294
+ if (!this.sessions.has(sessionId)) {
295
+ console.log(`[ShortTermMemory:${this.instanceId}] Session ${sessionId} not found for createRun, reloading...`);
296
+ await this.loadSessionsFromDisk();
297
+ }
289
298
  if (!this.sessions.has(sessionId))
290
299
  throw new Error('Session not found');
291
300
  const now = Date.now();
@@ -355,9 +364,17 @@ export class ShortTermMemory {
355
364
  }
356
365
  async addMessage(sessionId, message) {
357
366
  await this.ensureReady();
358
- const sessionMeta = this.sessions.get(sessionId);
359
- if (!sessionMeta)
367
+ let sessionMeta = this.sessions.get(sessionId);
368
+ // 如果内存中没有,尝试重载
369
+ if (!sessionMeta) {
370
+ console.log(`[ShortTermMemory:${this.instanceId}] Session ${sessionId} not found in memory for addMessage, reloading...`);
371
+ await this.loadSessionsFromDisk();
372
+ sessionMeta = this.sessions.get(sessionId);
373
+ }
374
+ if (!sessionMeta) {
375
+ console.error(`[ShortTermMemory:${this.instanceId}] Session ${sessionId} not found even after reload.`);
360
376
  throw new Error(`Session ${sessionId} not found`);
377
+ }
361
378
  const msg = { ...message };
362
379
  if (!msg.timestamp)
363
380
  msg.timestamp = Date.now();
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "xiaozuoassistant",
3
3
  "private": false,
4
4
  "description": "Your personal, locally-hosted AI assistant for office productivity.",
5
- "version": "0.1.71",
5
+ "version": "0.1.73",
6
6
  "author": "mantle.lau",
7
7
  "license": "MIT",
8
8
  "repository": {