@wavequery/conductor
Version:
Modular LLM orchestration framework
101 lines • 3.14 kB
JavaScript
export class InMemoryStore {
constructor() {
this.store = new Map();
}
async get(key) {
const item = this.store.get(key);
if (!item)
return null;
if (this.isExpired(item)) {
await this.delete(key);
return null;
}
return item;
}
async set(key, item) {
this.store.set(key, item);
}
async delete(key) {
this.store.delete(key);
}
async clear() {
this.store.clear();
}
async search(query) {
return Array.from(this.store.values()).filter((item) => {
if (query.type && item.metadata.type !== query.type)
return false;
if (query.tags?.length) {
const hasAllTags = query.tags.every((tag) => item.metadata.tags?.includes(tag));
if (!hasAllTags)
return false;
}
if (query.fromDate && item.metadata.timestamp < query.fromDate)
return false;
if (query.toDate && item.metadata.timestamp > query.toDate)
return false;
return !this.isExpired(item);
});
}
isExpired(item) {
if (!item.metadata.ttl)
return false;
const expiryTime = new Date(item.metadata.timestamp).getTime() + item.metadata.ttl * 1000;
return Date.now() > expiryTime;
}
}
export class PersistentStore {
constructor(filename) {
this.filename = filename;
this.store = new Map();
this.load();
}
async load() {
try {
const fs = await import("fs/promises");
const data = await fs.readFile(this.filename, "utf-8");
const items = JSON.parse(data);
this.store = new Map(Object.entries(items));
}
catch (error) {
this.store = new Map();
}
}
async save() {
const fs = await import("fs/promises");
const data = JSON.stringify(Object.fromEntries(this.store));
await fs.writeFile(this.filename, data, "utf-8");
}
async get(key) {
return this.store.get(key) || null;
}
async set(key, item) {
this.store.set(key, item);
await this.save();
}
async delete(key) {
this.store.delete(key);
await this.save();
}
async clear() {
this.store.clear();
await this.save();
}
async search(query) {
return Array.from(this.store.values()).filter((item) => {
if (query.type && item.metadata.type !== query.type)
return false;
if (query.tags?.length) {
const hasAllTags = query.tags.every((tag) => item.metadata.tags?.includes(tag));
if (!hasAllTags)
return false;
}
if (query.fromDate && item.metadata.timestamp < query.fromDate)
return false;
if (query.toDate && item.metadata.timestamp > query.toDate)
return false;
return true;
});
}
}
//# sourceMappingURL=store-provider.js.map