@clawchatsai/connector 0.1.16 → 0.1.18

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,108 +0,0 @@
1
- // Config discovery (file reads only) lives in memory-config.js.
2
- // This file contains only provider implementations (network calls, no file reads).
3
- export { discoverMemoryConfig } from './memory-config.js';
4
-
5
- export async function autoDetectQdrantCollection(config) {
6
- if (config.collection) return config.collection;
7
- try {
8
- const r = await fetch(`http://${config.host}:${config.port}/collections`, { signal: AbortSignal.timeout(3000) });
9
- const data = await r.json();
10
- const found = (data.result?.collections || []).map(c => c.name).find(n => !n.includes('migration'));
11
- if (found) { console.log(`Memory: auto-detected Qdrant collection "${found}"`); return found; }
12
- } catch { /* fall through */ }
13
- console.log('Memory: Qdrant unreachable or no collections, falling back to "memories"');
14
- return 'memories';
15
- }
16
-
17
- function createQdrantProvider(config) {
18
- const baseUrl = `http://${config.host}:${config.port}`;
19
- let collection = config.collection;
20
- return {
21
- name: 'qdrant',
22
- config,
23
- async init() { collection = await autoDetectQdrantCollection(config); config.collection = collection; },
24
- async list(limit, offset) {
25
- const body = { limit, with_payload: true, with_vector: false };
26
- if (offset) body.offset = offset;
27
- const r = await fetch(`${baseUrl}/collections/${collection}/points/scroll`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
28
- const data = await r.json();
29
- return { memories: (data.result?.points || []).map(p => ({ id: p.id, ...p.payload })), next_offset: data.result?.next_page_offset || null };
30
- },
31
- async search(query) {
32
- const q = query.toLowerCase();
33
- const matches = [];
34
- let offset = null;
35
- do {
36
- const body = { limit: 100, with_payload: true, with_vector: false };
37
- if (offset) body.offset = offset;
38
- const r = await fetch(`${baseUrl}/collections/${collection}/points/scroll`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
39
- const data = await r.json();
40
- for (const p of (data.result?.points || [])) { if ((p.payload?.data || '').toLowerCase().includes(q)) matches.push({ id: p.id, ...p.payload }); }
41
- offset = data.result?.next_page_offset || null;
42
- } while (offset);
43
- return { memories: matches, next_offset: null };
44
- },
45
- async update(id, newData) {
46
- const r = await fetch(`${baseUrl}/collections/${collection}/points/payload`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ points: [id], payload: { data: newData } }) });
47
- const data = await r.json();
48
- if (data.status?.error) throw new Error(data.status.error);
49
- return data.result;
50
- },
51
- async delete(id) {
52
- const r = await fetch(`${baseUrl}/collections/${collection}/points/delete`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ points: [id] }) });
53
- return (await r.json()).result;
54
- },
55
- async status() {
56
- try {
57
- const r = await fetch(`${baseUrl}/collections/${collection}`, { signal: AbortSignal.timeout(3000) });
58
- const data = await r.json();
59
- return { reachable: true, pointsCount: data.result?.points_count ?? null };
60
- } catch { return { reachable: false }; }
61
- },
62
- };
63
- }
64
-
65
- function createPgProvider(config) {
66
- let _pool = null;
67
- const table = config.collection || 'memories';
68
- async function getPool() {
69
- if (_pool) return _pool;
70
- let pg;
71
- try { pg = await import('pg'); } catch { throw new Error('pg package not installed. Run: npm install pg'); }
72
- const Pool = pg.default?.Pool || pg.Pool;
73
- _pool = config.pgUrl ? new Pool({ connectionString: config.pgUrl }) : new Pool({ host: config.host, port: config.port || 5432, user: config.pgUser || 'mem0', password: config.pgPassword || '', database: config.pgDbName || 'mem0' });
74
- return _pool;
75
- }
76
- return {
77
- name: 'postgres',
78
- config,
79
- async init() { /* pool created lazily */ },
80
- async list(limit, offset) {
81
- const pool = await getPool();
82
- const off = offset ? parseInt(offset, 10) : 0;
83
- const { rows } = await pool.query(`SELECT id, payload FROM ${table} ORDER BY created_at DESC LIMIT $1 OFFSET $2`, [limit, off]);
84
- return { memories: rows.map(r => ({ id: r.id, ...(typeof r.payload === 'string' ? JSON.parse(r.payload) : r.payload) })), next_offset: rows.length === limit ? off + limit : null };
85
- },
86
- async search(query) {
87
- const pool = await getPool();
88
- const { rows } = await pool.query(`SELECT id, payload FROM ${table} WHERE payload->>'data' ILIKE $1 LIMIT 100`, [`%${query}%`]);
89
- return { memories: rows.map(r => ({ id: r.id, ...(typeof r.payload === 'string' ? JSON.parse(r.payload) : r.payload) })), next_offset: null };
90
- },
91
- async update(id, newData) {
92
- const pool = await getPool();
93
- const { rowCount } = await pool.query(`UPDATE ${table} SET payload = jsonb_set(payload, '{data}', $1::jsonb) WHERE id = $2`, [JSON.stringify(newData), id]);
94
- if (rowCount === 0) throw new Error('Memory not found');
95
- return { updated: true };
96
- },
97
- async delete(id) { const pool = await getPool(); await pool.query(`DELETE FROM ${table} WHERE id = $1`, [id]); return { deleted: true }; },
98
- async status() {
99
- try { const pool = await getPool(); const { rows } = await pool.query(`SELECT COUNT(*) as count FROM ${table}`); return { reachable: true, pointsCount: parseInt(rows[0].count, 10) }; }
100
- catch (err) { return { reachable: false, error: err.message }; }
101
- },
102
- };
103
- }
104
-
105
- export function createMemoryProvider(config) {
106
- if (config.provider === 'postgres' || config.provider === 'pgvector') return createPgProvider(config);
107
- return createQdrantProvider(config);
108
- }
@@ -1,47 +0,0 @@
1
- // Move a thread (row + messages + unread markers) between per-project SQLite files.
2
- // Projects are separate databases, so a move is copy-then-delete: the target copy is
3
- // committed before the source row is removed, so a failure never loses the thread.
4
-
5
- const TABLES = ['threads', 'messages', 'unread_messages'];
6
-
7
- function columns(db, table) {
8
- return db.prepare(`PRAGMA table_info(${table})`).all().map(c => c.name);
9
- }
10
-
11
- function inTransaction(db, fn) {
12
- db.exec('BEGIN IMMEDIATE');
13
- try { const r = fn(); db.exec('COMMIT'); return r; }
14
- catch (e) { try { db.exec('ROLLBACK'); } catch { /* already rolled back */ } throw e; }
15
- }
16
-
17
- /**
18
- * @returns {boolean} true when the thread was moved, false when it wasn't in `from`.
19
- */
20
- export function moveThread(getDb, threadId, from, to) {
21
- if (from === to) return false;
22
- const src = getDb(from);
23
- const dst = getDb(to);
24
- const thread = src.prepare('SELECT * FROM threads WHERE id = ?').get(threadId);
25
- if (!thread) return false;
26
-
27
- const rows = {
28
- threads: [thread],
29
- messages: src.prepare('SELECT * FROM messages WHERE thread_id = ? ORDER BY timestamp, rowid').all(threadId),
30
- unread_messages: src.prepare('SELECT * FROM unread_messages WHERE thread_id = ?').all(threadId),
31
- };
32
-
33
- inTransaction(dst, () => {
34
- // A leftover copy from an interrupted earlier move is replaced (cascade clears its children).
35
- dst.prepare('DELETE FROM threads WHERE id = ?').run(threadId);
36
- for (const table of TABLES) {
37
- if (!rows[table].length) continue;
38
- const dstCols = new Set(columns(dst, table));
39
- const cols = Object.keys(rows[table][0]).filter(c => dstCols.has(c));
40
- const stmt = dst.prepare(`INSERT INTO ${table} (${cols.join(', ')}) VALUES (${cols.map(() => '?').join(', ')})`);
41
- for (const row of rows[table]) stmt.run(...cols.map(c => row[c] ?? null));
42
- }
43
- });
44
-
45
- inTransaction(src, () => { src.prepare('DELETE FROM threads WHERE id = ?').run(threadId); });
46
- return true;
47
- }