@clawchatsai/connector 0.1.24 → 0.1.25

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clawchatsai/connector",
3
- "version": "0.1.24",
3
+ "version": "0.1.25",
4
4
  "type": "module",
5
5
  "description": "ClawChats OpenClaw plugin — P2P tunnel + local API bridge",
6
6
  "main": "dist/index.js",
package/server/index.js CHANGED
@@ -183,6 +183,23 @@ export function createApp(config = {}) {
183
183
  broadcast(JSON.stringify({ type: 'clawchats', event: 'project-styles-changed' }));
184
184
  return send(res, 200, { ok: true });
185
185
  }
186
+ if (method === 'GET' && urlPath === '/api/extras/bookmarks') return send(res, 200, { bookmarks: extras.listBookmarks() });
187
+ if (method === 'POST' && urlPath === '/api/extras/bookmarks') {
188
+ const bookmark = extras.addBookmark(uuid(), await parseBody(req));
189
+ if (!bookmark) return sendError(res, 400, 'sessionKey and messageId are required');
190
+ broadcast(JSON.stringify({ type: 'clawchats', event: 'bookmarks-changed' }));
191
+ return send(res, 200, { bookmark });
192
+ }
193
+ if ((p = matchRoute(method, urlPath, 'PATCH /api/extras/bookmarks/:id'))) {
194
+ const bookmark = extras.renameBookmark(p.id, (await parseBody(req)).label);
195
+ if (!bookmark) return sendError(res, 404, 'Bookmark not found, or empty label');
196
+ broadcast(JSON.stringify({ type: 'clawchats', event: 'bookmarks-changed' }));
197
+ return send(res, 200, { bookmark });
198
+ }
199
+ if ((p = matchRoute(method, urlPath, 'DELETE /api/extras/bookmarks/:id'))) {
200
+ if (extras.deleteBookmark(p.id)) broadcast(JSON.stringify({ type: 'clawchats', event: 'bookmarks-changed' }));
201
+ return send(res, 200, { ok: true });
202
+ }
186
203
 
187
204
 
188
205
  sendError(res, 404, `Not found: ${method} ${urlPath}`);
@@ -4,6 +4,7 @@
4
4
  // project_styles color/icon and new-chat preset per gateway session group (groups are just {name, position})
5
5
  // thread_extras per-session ClawChats data (Intelligence panel etc.), keyed by session key
6
6
  // legacy_created creation time of chats from before the gateway recorded createdAt
7
+ // bookmarks messages the user bookmarked, keyed by id; one per (session key, message id)
7
8
 
8
9
  // New-chat preset: what a new chat in the project starts with. Every field is optional;
9
10
  // a missing field means the gateway default. `cwd` and `projectId` are exclusive places.
@@ -28,6 +29,16 @@ function parsePreset(text) {
28
29
  try { return cleanPreset(JSON.parse(text)); } catch { return null; }
29
30
  }
30
31
 
32
+ const str = (v) => typeof v === 'string' && v.trim() ? v.trim() : '';
33
+ const clip = (v, n) => str(v).slice(0, n);
34
+
35
+ function bookmarkRow(r) {
36
+ return {
37
+ id: r.id, sessionKey: r.session_key, messageId: r.message_id, role: r.role, label: r.label,
38
+ snippet: r.snippet, chatTitle: r.chat_title, createdAt: r.created_at, updatedAt: r.updated_at,
39
+ };
40
+ }
41
+
31
42
  export function createExtrasStore(getGlobalDb) {
32
43
  let ready = false;
33
44
  function db() {
@@ -37,6 +48,7 @@ export function createExtrasStore(getGlobalDb) {
37
48
  if (!g.prepare('PRAGMA table_info(project_styles)').all().some(c => c.name === 'preset')) g.exec('ALTER TABLE project_styles ADD COLUMN preset TEXT');
38
49
  g.exec(`CREATE TABLE IF NOT EXISTS legacy_created (session_key TEXT PRIMARY KEY, created_at INTEGER NOT NULL)`);
39
50
  g.exec(`CREATE TABLE IF NOT EXISTS thread_extras (session_key TEXT NOT NULL, kind TEXT NOT NULL, data TEXT NOT NULL, updated_at INTEGER NOT NULL, PRIMARY KEY (session_key, kind))`);
51
+ g.exec(`CREATE TABLE IF NOT EXISTS bookmarks (id TEXT PRIMARY KEY, session_key TEXT NOT NULL, message_id TEXT NOT NULL, role TEXT, label TEXT NOT NULL, snippet TEXT NOT NULL, chat_title TEXT, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, UNIQUE (session_key, message_id))`);
40
52
  ready = true;
41
53
  }
42
54
  return g;
@@ -113,7 +125,36 @@ export function createExtrasStore(getGlobalDb) {
113
125
  },
114
126
 
115
127
  deleteThreadExtras(sessionKey) {
116
- if (sessionKey) db().prepare('DELETE FROM thread_extras WHERE session_key = ?').run(sessionKey);
128
+ if (!sessionKey) return;
129
+ db().prepare('DELETE FROM thread_extras WHERE session_key = ?').run(sessionKey);
130
+ db().prepare('DELETE FROM bookmarks WHERE session_key = ?').run(sessionKey);
131
+ },
132
+
133
+ /** All bookmarks, newest first. */
134
+ listBookmarks() {
135
+ return db().prepare('SELECT * FROM bookmarks ORDER BY created_at DESC').all().map(bookmarkRow);
136
+ },
137
+
138
+ /** Bookmark a message; bookmarking it again returns the existing bookmark unchanged. */
139
+ addBookmark(id, { sessionKey, messageId, role, label, snippet, chatTitle } = {}) {
140
+ if (!id || !str(sessionKey) || !str(messageId)) return null;
141
+ const now = Date.now();
142
+ db().prepare('INSERT INTO bookmarks (id, session_key, message_id, role, label, snippet, chat_title, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(session_key, message_id) DO NOTHING')
143
+ .run(id, sessionKey, messageId, role === 'user' ? 'user' : 'assistant', clip(label, 200) || 'Bookmark', clip(snippet, 600), clip(chatTitle, 200) || null, now, now);
144
+ return bookmarkRow(db().prepare('SELECT * FROM bookmarks WHERE session_key = ? AND message_id = ?').get(sessionKey, messageId));
145
+ },
146
+
147
+ /** Rename; returns the updated bookmark, or null if it doesn't exist. */
148
+ renameBookmark(id, label) {
149
+ const l = clip(label, 200);
150
+ if (!l) return null;
151
+ db().prepare('UPDATE bookmarks SET label = ?, updated_at = ? WHERE id = ?').run(l, Date.now(), id);
152
+ const r = db().prepare('SELECT * FROM bookmarks WHERE id = ?').get(id);
153
+ return r ? bookmarkRow(r) : null;
154
+ },
155
+
156
+ deleteBookmark(id) {
157
+ return db().prepare('DELETE FROM bookmarks WHERE id = ?').run(id).changes > 0;
117
158
  },
118
159
  };
119
160
  }
@@ -43,3 +43,27 @@ test('cleanPreset: known fields only, valid permission mode, projectId wins over
43
43
  assert.deepEqual(cleanPreset({ cwd: '/a', projectLabel: 'Repo' }), { cwd: '/a' });
44
44
  assert.deepEqual(cleanPreset({ fastMode: true }), { fastMode: true });
45
45
  });
46
+
47
+ test('bookmarks: add is idempotent per message, rename, delete, dropped with the session', () => {
48
+ const db = new Database(':memory:');
49
+ const extras = createExtrasStore(() => db);
50
+ const a = extras.addBookmark('b1', { sessionKey: 'agent:main:x', messageId: 'm1', role: 'assistant', label: ' Plan ', snippet: 'The plan…', chatTitle: 'Chat X' });
51
+ assert.equal(a.label, 'Plan');
52
+ assert.equal(a.chatTitle, 'Chat X');
53
+ // Same message again: the existing bookmark comes back unchanged.
54
+ const again = extras.addBookmark('b2', { sessionKey: 'agent:main:x', messageId: 'm1', label: 'Other' });
55
+ assert.equal(again.id, 'b1');
56
+ assert.equal(again.label, 'Plan');
57
+ assert.equal(extras.addBookmark('b3', { sessionKey: 'agent:main:x' }), null);
58
+ extras.addBookmark('b4', { sessionKey: 'agent:main:y', messageId: 'm9', role: 'user', label: '', snippet: 'hi' });
59
+ assert.deepEqual(extras.listBookmarks().map(b => [b.id, b.label]).sort(), [['b1', 'Plan'], ['b4', 'Bookmark']]);
60
+
61
+ assert.equal(extras.renameBookmark('b1', 'Bulk export plan').label, 'Bulk export plan');
62
+ assert.equal(extras.renameBookmark('b1', ' '), null);
63
+ assert.equal(extras.renameBookmark('nope', 'x'), null);
64
+
65
+ assert.equal(extras.deleteBookmark('b4'), true);
66
+ assert.equal(extras.deleteBookmark('b4'), false);
67
+ extras.deleteThreadExtras('agent:main:x');
68
+ assert.deepEqual(extras.listBookmarks(), []);
69
+ });