@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.
- package/dist/adapters/mongo.d.ts +6 -4
- package/dist/adapters/mongo.js +9 -7
- package/dist/diary.js +2 -7
- package/package.json +1 -1
package/dist/adapters/mongo.d.ts
CHANGED
|
@@ -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
|
|
6
|
-
constructor(
|
|
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 {};
|
package/dist/adapters/mongo.js
CHANGED
|
@@ -7,20 +7,21 @@ exports.MongoStorage = void 0;
|
|
|
7
7
|
const crypto_1 = __importDefault(require("crypto"));
|
|
8
8
|
class MongoStorage {
|
|
9
9
|
collection;
|
|
10
|
-
|
|
11
|
-
constructor(
|
|
12
|
-
this.collection =
|
|
10
|
+
static indexedCollections = new Set();
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.collection = options.collection;
|
|
13
13
|
}
|
|
14
14
|
async ensureIndex() {
|
|
15
|
-
|
|
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:
|
|
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
|
-
|
|
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 >
|
|
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
|
|
111
|
-
|
|
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
|
}
|