@agentskit/memory 0.6.1 → 0.7.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/index.cjs +185 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +56 -2
- package/dist/index.d.ts +56 -2
- package/dist/index.js +183 -7
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -87,6 +87,78 @@ function sqliteChatMemory(config) {
|
|
|
87
87
|
}
|
|
88
88
|
};
|
|
89
89
|
}
|
|
90
|
+
var cachedSdk = null;
|
|
91
|
+
async function loadSdk() {
|
|
92
|
+
if (!cachedSdk) {
|
|
93
|
+
cachedSdk = (async () => {
|
|
94
|
+
try {
|
|
95
|
+
const moduleId = "@libsql/client";
|
|
96
|
+
return await import(
|
|
97
|
+
/* @vite-ignore */
|
|
98
|
+
moduleId
|
|
99
|
+
);
|
|
100
|
+
} catch {
|
|
101
|
+
throw new Error("Install @libsql/client to use tursoChatMemory: npm install @libsql/client");
|
|
102
|
+
}
|
|
103
|
+
})();
|
|
104
|
+
}
|
|
105
|
+
return cachedSdk;
|
|
106
|
+
}
|
|
107
|
+
function encodeMessages2(messages) {
|
|
108
|
+
return JSON.stringify(serializeMessages(messages));
|
|
109
|
+
}
|
|
110
|
+
function decodeMessages2(json) {
|
|
111
|
+
if (!json) return [];
|
|
112
|
+
try {
|
|
113
|
+
return deserializeMessages(JSON.parse(json));
|
|
114
|
+
} catch {
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function tursoChatMemory(config) {
|
|
119
|
+
const conversationId = config.conversationId ?? "default";
|
|
120
|
+
let clientPromise = null;
|
|
121
|
+
const getClient = async () => {
|
|
122
|
+
if (!clientPromise) {
|
|
123
|
+
clientPromise = (async () => {
|
|
124
|
+
const sdk = await loadSdk();
|
|
125
|
+
const client = sdk.createClient({ url: config.url, authToken: config.authToken });
|
|
126
|
+
await client.execute({
|
|
127
|
+
sql: "CREATE TABLE IF NOT EXISTS conversations (id TEXT PRIMARY KEY, messages TEXT NOT NULL)"
|
|
128
|
+
});
|
|
129
|
+
return client;
|
|
130
|
+
})();
|
|
131
|
+
}
|
|
132
|
+
return clientPromise;
|
|
133
|
+
};
|
|
134
|
+
return {
|
|
135
|
+
async load() {
|
|
136
|
+
const client = await getClient();
|
|
137
|
+
const result = await client.execute({
|
|
138
|
+
sql: "SELECT messages FROM conversations WHERE id = ?",
|
|
139
|
+
args: [conversationId]
|
|
140
|
+
});
|
|
141
|
+
const row = result.rows[0];
|
|
142
|
+
return decodeMessages2(row?.messages);
|
|
143
|
+
},
|
|
144
|
+
async save(messages) {
|
|
145
|
+
const client = await getClient();
|
|
146
|
+
const json = encodeMessages2(messages);
|
|
147
|
+
await client.execute({
|
|
148
|
+
sql: `INSERT INTO conversations (id, messages) VALUES (?, ?)
|
|
149
|
+
ON CONFLICT(id) DO UPDATE SET messages = excluded.messages`,
|
|
150
|
+
args: [conversationId, json]
|
|
151
|
+
});
|
|
152
|
+
},
|
|
153
|
+
async clear() {
|
|
154
|
+
const client = await getClient();
|
|
155
|
+
await client.execute({
|
|
156
|
+
sql: "DELETE FROM conversations WHERE id = ?",
|
|
157
|
+
args: [conversationId]
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
}
|
|
90
162
|
|
|
91
163
|
// src/redis-client.ts
|
|
92
164
|
async function createRedisClientAdapter(url) {
|
|
@@ -122,10 +194,10 @@ async function createRedisClientAdapter(url) {
|
|
|
122
194
|
}
|
|
123
195
|
|
|
124
196
|
// src/redis-chat.ts
|
|
125
|
-
function
|
|
197
|
+
function encodeMessages3(messages) {
|
|
126
198
|
return JSON.stringify(serializeMessages(messages));
|
|
127
199
|
}
|
|
128
|
-
function
|
|
200
|
+
function decodeMessages3(json) {
|
|
129
201
|
if (!json) return [];
|
|
130
202
|
try {
|
|
131
203
|
return deserializeMessages(JSON.parse(json));
|
|
@@ -147,11 +219,11 @@ function redisChatMemory(config) {
|
|
|
147
219
|
async load() {
|
|
148
220
|
const client = await getClient();
|
|
149
221
|
const json = await client.get(key);
|
|
150
|
-
return
|
|
222
|
+
return decodeMessages3(json);
|
|
151
223
|
},
|
|
152
224
|
async save(messages) {
|
|
153
225
|
const client = await getClient();
|
|
154
|
-
await client.set(key,
|
|
226
|
+
await client.set(key, encodeMessages3(messages));
|
|
155
227
|
},
|
|
156
228
|
async clear() {
|
|
157
229
|
const client = await getClient();
|
|
@@ -281,6 +353,40 @@ function redisVectorMemory(config) {
|
|
|
281
353
|
};
|
|
282
354
|
}
|
|
283
355
|
|
|
356
|
+
// src/vector/filter.ts
|
|
357
|
+
function isPrimitive(value) {
|
|
358
|
+
const t = typeof value;
|
|
359
|
+
return value === null || t === "string" || t === "number" || t === "boolean";
|
|
360
|
+
}
|
|
361
|
+
function evalOperator(actual, op) {
|
|
362
|
+
if ("$eq" in op) return actual === op.$eq;
|
|
363
|
+
if ("$ne" in op) return actual !== op.$ne;
|
|
364
|
+
if ("$in" in op) return op.$in.includes(actual);
|
|
365
|
+
if ("$nin" in op) return !op.$nin.includes(actual);
|
|
366
|
+
if ("$gt" in op) return actual > op.$gt;
|
|
367
|
+
if ("$gte" in op) return actual >= op.$gte;
|
|
368
|
+
if ("$lt" in op) return actual < op.$lt;
|
|
369
|
+
if ("$lte" in op) return actual <= op.$lte;
|
|
370
|
+
if ("$exists" in op) return actual !== void 0 === op.$exists;
|
|
371
|
+
return false;
|
|
372
|
+
}
|
|
373
|
+
function evalPredicate(actual, predicate) {
|
|
374
|
+
if (isPrimitive(predicate)) return actual === predicate;
|
|
375
|
+
return evalOperator(actual, predicate);
|
|
376
|
+
}
|
|
377
|
+
function matchesFilter(metadata, filter) {
|
|
378
|
+
if (!filter) return true;
|
|
379
|
+
const meta = metadata ?? {};
|
|
380
|
+
const compound = filter;
|
|
381
|
+
if (compound.$and) return compound.$and.every((f) => matchesFilter(meta, f));
|
|
382
|
+
if (compound.$or) return compound.$or.some((f) => matchesFilter(meta, f));
|
|
383
|
+
for (const [field, predicate] of Object.entries(filter)) {
|
|
384
|
+
if (field.startsWith("$")) continue;
|
|
385
|
+
if (!evalPredicate(meta[field], predicate)) return false;
|
|
386
|
+
}
|
|
387
|
+
return true;
|
|
388
|
+
}
|
|
389
|
+
|
|
284
390
|
// src/file-vector.ts
|
|
285
391
|
function requireVectra() {
|
|
286
392
|
try {
|
|
@@ -349,8 +455,9 @@ function fileVectorMemory(config) {
|
|
|
349
455
|
async search(embedding, options) {
|
|
350
456
|
const topK = options?.topK ?? 5;
|
|
351
457
|
const threshold = options?.threshold ?? 0;
|
|
352
|
-
const
|
|
353
|
-
|
|
458
|
+
const fetchK = options?.filter ? Math.max(topK * 4, 50) : topK;
|
|
459
|
+
const results = await store.query(embedding, fetchK);
|
|
460
|
+
return results.filter((r) => r.score >= threshold).filter((r) => matchesFilter(r.metadata, options?.filter)).slice(0, topK).map((r) => ({
|
|
354
461
|
id: r.id,
|
|
355
462
|
content: String(r.metadata.content ?? contentCache.get(r.id) ?? ""),
|
|
356
463
|
score: r.score,
|
|
@@ -728,6 +835,75 @@ function upstashVector(config) {
|
|
|
728
835
|
};
|
|
729
836
|
}
|
|
730
837
|
|
|
838
|
+
// src/vector/supabase.ts
|
|
839
|
+
var cachedSdk2 = null;
|
|
840
|
+
async function loadSdk2() {
|
|
841
|
+
if (!cachedSdk2) {
|
|
842
|
+
cachedSdk2 = (async () => {
|
|
843
|
+
try {
|
|
844
|
+
const moduleId = "@supabase/supabase-js";
|
|
845
|
+
return await import(
|
|
846
|
+
/* @vite-ignore */
|
|
847
|
+
moduleId
|
|
848
|
+
);
|
|
849
|
+
} catch {
|
|
850
|
+
throw new Error(
|
|
851
|
+
"Install @supabase/supabase-js to use supabaseVectorStore: npm install @supabase/supabase-js"
|
|
852
|
+
);
|
|
853
|
+
}
|
|
854
|
+
})();
|
|
855
|
+
}
|
|
856
|
+
return cachedSdk2;
|
|
857
|
+
}
|
|
858
|
+
function buildRunner(client) {
|
|
859
|
+
return {
|
|
860
|
+
async query(sql, params) {
|
|
861
|
+
const result = await client.rpc("agentskit_execute_sql", { sql, params });
|
|
862
|
+
if (result.error) throw new Error(`supabase: ${result.error.message}`);
|
|
863
|
+
return { rows: result.data ?? [] };
|
|
864
|
+
}
|
|
865
|
+
};
|
|
866
|
+
}
|
|
867
|
+
function supabaseVectorStore(config) {
|
|
868
|
+
let runnerPromise = null;
|
|
869
|
+
const getRunner = () => {
|
|
870
|
+
if (!runnerPromise) {
|
|
871
|
+
runnerPromise = (async () => {
|
|
872
|
+
const sdk = await loadSdk2();
|
|
873
|
+
const client = sdk.createClient(config.url, config.serviceRoleKey);
|
|
874
|
+
return buildRunner(client);
|
|
875
|
+
})();
|
|
876
|
+
}
|
|
877
|
+
return runnerPromise;
|
|
878
|
+
};
|
|
879
|
+
let backend = null;
|
|
880
|
+
const getBackend = async () => {
|
|
881
|
+
if (!backend) {
|
|
882
|
+
const runner = await getRunner();
|
|
883
|
+
backend = pgvector({
|
|
884
|
+
runner,
|
|
885
|
+
table: config.table,
|
|
886
|
+
topK: config.topK
|
|
887
|
+
});
|
|
888
|
+
}
|
|
889
|
+
return backend;
|
|
890
|
+
};
|
|
891
|
+
return {
|
|
892
|
+
async store(docs) {
|
|
893
|
+
const b = await getBackend();
|
|
894
|
+
return b.store(docs);
|
|
895
|
+
},
|
|
896
|
+
async search(embedding, options) {
|
|
897
|
+
const b = await getBackend();
|
|
898
|
+
return b.search(embedding, options);
|
|
899
|
+
},
|
|
900
|
+
async delete(ids) {
|
|
901
|
+
const b = await getBackend();
|
|
902
|
+
return b.delete?.(ids);
|
|
903
|
+
}
|
|
904
|
+
};
|
|
905
|
+
}
|
|
906
|
+
|
|
731
907
|
// src/encrypted.ts
|
|
732
908
|
function toBase64(bytes) {
|
|
733
909
|
if (typeof Buffer !== "undefined") return Buffer.from(bytes).toString("base64");
|
|
@@ -870,6 +1046,6 @@ function createHierarchicalMemory(options) {
|
|
|
870
1046
|
};
|
|
871
1047
|
}
|
|
872
1048
|
|
|
873
|
-
export { chroma, createEncryptedMemory, createHierarchicalMemory, createInMemoryGraph, createInMemoryPersonalization, fileChatMemory, fileVectorMemory, pgvector, pinecone, qdrant, redisChatMemory, redisVectorMemory, renderProfileContext, sqliteChatMemory, upstashVector };
|
|
1049
|
+
export { chroma, createEncryptedMemory, createHierarchicalMemory, createInMemoryGraph, createInMemoryPersonalization, fileChatMemory, fileVectorMemory, matchesFilter, pgvector, pinecone, qdrant, redisChatMemory, redisVectorMemory, renderProfileContext, sqliteChatMemory, supabaseVectorStore, tursoChatMemory, upstashVector };
|
|
874
1050
|
//# sourceMappingURL=index.js.map
|
|
875
1051
|
//# sourceMappingURL=index.js.map
|