@agent-diaries/core 0.1.41 → 1.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,14 +1,16 @@
1
1
  import { StorageAdapter } from '../storage';
2
2
  import { Collection } from 'mongodb';
3
+ interface MongoStorageOptions {
4
+ collection: Collection;
5
+ }
3
6
  export declare class MongoStorage<T> implements StorageAdapter<T> {
4
7
  private collection;
5
- private initialized;
6
- constructor(config: {
7
- collection: Collection;
8
- });
8
+ private static indexedCollections;
9
+ constructor(options: MongoStorageOptions);
9
10
  private ensureIndex;
10
11
  private hashString;
11
12
  get(key: string): Promise<T | null>;
12
13
  set(key: string, data: T): Promise<void>;
13
14
  withLock<R>(key: string, fn: () => Promise<R>): Promise<R>;
14
15
  }
16
+ export {};
@@ -7,20 +7,21 @@ exports.MongoStorage = void 0;
7
7
  const crypto_1 = __importDefault(require("crypto"));
8
8
  class MongoStorage {
9
9
  collection;
10
- initialized = false;
11
- constructor(config) {
12
- this.collection = config.collection;
10
+ static indexedCollections = new Set();
11
+ constructor(options) {
12
+ this.collection = options.collection;
13
13
  }
14
14
  async ensureIndex() {
15
- if (this.initialized)
15
+ const ns = this.collection.namespace;
16
+ if (MongoStorage.indexedCollections.has(ns))
16
17
  return;
17
18
  try {
18
- await this.collection.createIndex({ lockedAt: 1 }, { expireAfterSeconds: 30, partialFilterExpression: { lockedAt: { $exists: true } } });
19
+ await this.collection.createIndex({ lockedAt: 1 }, { expireAfterSeconds: 10, partialFilterExpression: { lockedAt: { $exists: true } } });
19
20
  }
20
21
  catch (e) {
21
22
  console.warn('[MongoStorage] Failed to create TTL index:', e);
22
23
  }
23
- this.initialized = true;
24
+ MongoStorage.indexedCollections.add(ns);
24
25
  }
25
26
  hashString(str) {
26
27
  return crypto_1.default.createHash('sha256').update(str).digest('hex');
@@ -57,8 +58,9 @@ class MongoStorage {
57
58
  const jitter = Math.random() * 50;
58
59
  await new Promise(resolve => setTimeout(resolve, backoff + jitter));
59
60
  attempt++;
60
- if (attempt > 60)
61
+ if (attempt > 150) {
61
62
  throw new Error(`[MongoStorage] Lock timeout on key: ${key}`);
63
+ }
62
64
  }
63
65
  try {
64
66
  return await fn();
package/dist/diary.js CHANGED
@@ -107,13 +107,8 @@ class AgentDiary {
107
107
  await this.storage.set(`diary_${this.agentId}`, state);
108
108
  }
109
109
  else {
110
- // If not claimed first, we insert it
111
- const record = { title, signature, result, timestamp: Date.now() };
112
- state.history = [record, ...state.history].slice(0, this.maxHistory);
113
- state.seenSignatures = state.history.map(r => r.signature);
114
- state.runCount += 1;
115
- state.lastRun = Date.now();
116
- await this.storage.set(`diary_${this.agentId}`, state);
110
+ // If not claimed first, we throw a loud error
111
+ throw new Error(`[AgentDiary] Task "${title}" was not claimed. Call claimTask() before writeTaskResult().`);
117
112
  }
118
113
  });
119
114
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agent-diaries/core",
3
- "version": "0.1.41",
3
+ "version": "1.0.0",
4
4
  "description": "The lightweight, framework-agnostic memory layer for edge AI agents.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",