@clawchatsai/connector 0.1.15 → 0.1.17

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.
@@ -0,0 +1,27 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { WebSocketServer } from 'ws';
4
+ import { GatewayClient } from './gateway.js';
5
+
6
+ const logger = { logFrame() {} };
7
+
8
+ test('close() shuts the socket and stops reconnecting (plugin reload drain)', async () => {
9
+ const wss = new WebSocketServer({ port: 0 });
10
+ let connections = 0;
11
+ wss.on('connection', () => { connections++; });
12
+ await new Promise(r => wss.once('listening', r));
13
+ const gw = new GatewayClient({ dataDir: '/tmp', debugLogger: logger, gatewayWsUrl: `ws://127.0.0.1:${wss.address().port}`, authToken: '' });
14
+ gw.connect();
15
+ await new Promise(r => setTimeout(r, 150));
16
+ assert.equal(connections, 1);
17
+
18
+ const pending = gw.request('x').catch(e => e.message); // not connected (no handshake) → rejects
19
+ gw.close();
20
+ assert.equal(gw.ws, null);
21
+ assert.match(await pending, /not connected|stopping/);
22
+
23
+ gw.connect(); // ignored after close
24
+ await new Promise(r => setTimeout(r, 1300)); // longer than the first reconnect delay
25
+ assert.equal(connections, 1);
26
+ await new Promise(r => wss.close(r));
27
+ });
@@ -0,0 +1,212 @@
1
+ // `ocplatform clawchats export-history`: writes ClawChats chats that the gateway has no
2
+ // session for into the gateway's legacy session-store format (sessions.json + one JSONL
3
+ // transcript per chat) in each agent's sessions folder. The gateway's own importer
4
+ // (`ocplatform doctor --fix`, gateway stopped) then moves them into its database.
5
+ //
6
+ // Text only: ClawChats stored final reply text plus an activity summary, not full tool
7
+ // calls, so tool calls are not reconstructed (that would feed the model invented history).
8
+
9
+ import fs from 'node:fs';
10
+ import path from 'node:path';
11
+ import crypto from 'node:crypto';
12
+ import { DatabaseSync } from 'node:sqlite';
13
+ import { WebSocket } from 'ws';
14
+ import { loadOrCreateDeviceIdentity, buildDeviceAuth } from './bootstrap/identity.js';
15
+ import { parseSessionKey } from './util/helpers.js';
16
+
17
+ const PLACEHOLDER_TITLES = new Set(['New chat', '']);
18
+
19
+ function withSuffix(base, n) {
20
+ return n <= 1 ? base : `${base} (${n})`;
21
+ }
22
+
23
+ function isPushableTitle(title) {
24
+ return typeof title === 'string' && !PLACEHOLDER_TITLES.has(title.trim());
25
+ }
26
+
27
+ const SKIP_CONTENT = new Set(['[Response interrupted]']);
28
+
29
+ /** One-shot gateway connection (same handshake as the connector's GatewayClient). */
30
+ export async function withGateway({ url, token, identityPath }, fn) {
31
+ const ws = new WebSocket(url);
32
+ const pending = new Map();
33
+ let seq = 0;
34
+ const ready = new Promise((resolve, reject) => {
35
+ const timer = setTimeout(() => reject(new Error(`gateway at ${url} did not answer`)), 15000);
36
+ ws.on('error', e => { clearTimeout(timer); reject(new Error(`cannot reach the gateway at ${url} (${e.message}) — is it running?`)); });
37
+ ws.on('message', data => {
38
+ let msg; try { msg = JSON.parse(data.toString()); } catch { return; }
39
+ if (msg.type === 'event' && msg.event === 'connect.challenge') {
40
+ const identity = loadOrCreateDeviceIdentity(identityPath);
41
+ const scopes = ['operator.read'];
42
+ const device = buildDeviceAuth(identity, { clientId: 'gateway-client', clientMode: 'backend', role: 'operator', scopes, token, nonce: msg.payload?.nonce || '' });
43
+ ws.send(JSON.stringify({ type: 'req', id: 'connect', method: 'connect', params: { minProtocol: 3, maxProtocol: 4, client: { id: 'gateway-client', version: '0.1.0', platform: 'node', mode: 'backend' }, role: 'operator', scopes, device, auth: { token } } }));
44
+ return;
45
+ }
46
+ if (msg.type === 'res' && msg.id === 'connect') {
47
+ clearTimeout(timer);
48
+ return msg.ok ? resolve() : reject(new Error(`gateway refused the connection: ${msg.error?.message || 'unknown error'}`));
49
+ }
50
+ if (msg.type === 'res' && pending.has(msg.id)) {
51
+ const p = pending.get(msg.id); pending.delete(msg.id);
52
+ msg.ok ? p.resolve(msg.payload) : p.reject(new Error(msg.error?.message || 'request failed'));
53
+ }
54
+ });
55
+ });
56
+ const request = (method, params = {}) => new Promise((resolve, reject) => {
57
+ const id = `export-${++seq}`;
58
+ pending.set(id, { resolve, reject });
59
+ ws.send(JSON.stringify({ type: 'req', id, method, params }));
60
+ });
61
+ try { await ready; return await fn(request); }
62
+ finally { ws.close(); }
63
+ }
64
+
65
+ async function listAllSessions(request) {
66
+ const out = [];
67
+ let offset = 0;
68
+ for (let page = 0; page < 50; page++) {
69
+ const res = await request('sessions.list', { archived: 'all', limit: 1000, offset });
70
+ out.push(...(res?.sessions || []));
71
+ if (!res?.hasMore || res.nextOffset == null) break;
72
+ offset = res.nextOffset;
73
+ }
74
+ return out;
75
+ }
76
+
77
+ function readThreads(dataDir, project) {
78
+ const file = path.join(dataDir, `${project.name}.db`);
79
+ if (!fs.existsSync(file)) return [];
80
+ const db = new DatabaseSync(file, { readOnly: true });
81
+ try {
82
+ const cols = db.prepare('PRAGMA table_info(threads)').all().map(c => c.name);
83
+ if (!cols.includes('session_key')) return [];
84
+ const threads = db.prepare('SELECT id, title, session_key FROM threads').all();
85
+ const msgStmt = db.prepare(`SELECT role, content, timestamp FROM messages WHERE thread_id = ? AND role IN ('user','assistant') AND content IS NOT NULL AND content != '' ORDER BY timestamp, rowid`);
86
+ return threads.map(t => ({ ...t, project, messages: msgStmt.all(t.id).filter(m => !SKIP_CONTENT.has(m.content.trim())) }));
87
+ } finally { db.close(); }
88
+ }
89
+
90
+ function transcriptLines(sessionId, cwd, messages) {
91
+ const iso = ms => new Date(ms).toISOString();
92
+ const lines = [{ type: 'session', version: 3, id: sessionId, timestamp: iso(messages[0].timestamp), cwd }];
93
+ let parentId = null;
94
+ for (const m of messages) {
95
+ const id = crypto.randomBytes(4).toString('hex');
96
+ const message = m.role === 'user'
97
+ ? { role: 'user', content: m.content, timestamp: m.timestamp }
98
+ : { role: 'assistant', content: [{ type: 'text', text: m.content }], stopReason: 'stop', timestamp: m.timestamp };
99
+ lines.push({ type: 'message', id, parentId, timestamp: iso(m.timestamp), message });
100
+ parentId = id;
101
+ }
102
+ return lines.map(l => JSON.stringify(l)).join('\n') + '\n';
103
+ }
104
+
105
+ /**
106
+ * @param {object} o
107
+ * @param {string} o.dataDir ClawChats data dir (workspaces.json + <project>.db)
108
+ * @param {string} o.stateDir gateway state dir (contains agents/<id>/sessions)
109
+ * @param {string} o.gatewayUrl
110
+ * @param {string} o.token
111
+ * @param {boolean} [o.check] report only, write nothing
112
+ * @param {(line: string) => void} [o.print]
113
+ * @returns {Promise<{ exported: number, skippedExisting: number, skippedEmpty: number, written: string[] }>}
114
+ */
115
+ export async function exportHistory({ dataDir, stateDir, gatewayUrl, token, check = false, print = console.log }) {
116
+ const workspaces = JSON.parse(fs.readFileSync(path.join(dataDir, 'workspaces.json'), 'utf8'));
117
+ const projects = Object.values(workspaces.workspaces || {}).sort((a, b) => (a.order ?? 999) - (b.order ?? 999));
118
+
119
+ const sessions = await withGateway({ url: gatewayUrl, token, identityPath: path.join(dataDir, 'device-identity.json') }, listAllSessions);
120
+ const existing = new Set(sessions.map(s => s.key));
121
+
122
+ const perAgent = new Map(); // agentId -> [thread]
123
+ const report = [];
124
+ let skippedExisting = 0, skippedEmpty = 0;
125
+ const allThreads = [];
126
+ for (const project of projects) {
127
+ const threads = readThreads(dataDir, project);
128
+ allThreads.push(...threads);
129
+ let exp = 0, have = 0, empty = 0;
130
+ for (const t of threads) {
131
+ const parsed = parseSessionKey(t.session_key);
132
+ if (!parsed) continue;
133
+ if (existing.has(t.session_key)) { have++; continue; }
134
+ if (!t.messages.length) { empty++; continue; }
135
+ if (!perAgent.has(parsed.agent)) perAgent.set(parsed.agent, []);
136
+ perAgent.get(parsed.agent).push(t);
137
+ exp++;
138
+ }
139
+ skippedExisting += have; skippedEmpty += empty;
140
+ report.push({ project: project.label, threads: threads.length, export: exp, inGateway: have, empty });
141
+ }
142
+
143
+ // Titles that will get a " (n)" suffix once synced (gateway names are unique per agent).
144
+ const taken = new Map(sessions.filter(s => s.label).map(s => [s.label, s.key]));
145
+ const dupes = new Map();
146
+ for (const t of allThreads) {
147
+ if (!isPushableTitle(t.title)) continue;
148
+ const owner = taken.get(t.title);
149
+ if (owner === t.session_key) continue;
150
+ if (owner) {
151
+ let n = 2; while (taken.has(withSuffix(t.title, n))) n++;
152
+ taken.set(withSuffix(t.title, n), t.session_key);
153
+ dupes.set(t.title, (dupes.get(t.title) || 0) + 1);
154
+ } else taken.set(t.title, t.session_key);
155
+ }
156
+
157
+ // Refuse before writing anything if an agent already has a pending legacy store.
158
+ const targets = [...perAgent.keys()].map(agent => ({ agent, dir: path.join(stateDir, 'agents', agent, 'sessions') }));
159
+ for (const { agent, dir } of targets) {
160
+ const store = path.join(dir, 'sessions.json');
161
+ if (fs.existsSync(store)) {
162
+ throw new Error(`${store} already exists (an unfinished import for agent "${agent}"). Run \`ocplatform gateway stop && ocplatform doctor --fix && ocplatform gateway start\` first, then run export-history again.`);
163
+ }
164
+ }
165
+
166
+ print('ClawChats history export' + (check ? ' (check only — nothing written)' : ''));
167
+ print('');
168
+ for (const r of report) print(` ${r.project.padEnd(24)} ${String(r.export).padStart(4)} to import ${String(r.inGateway).padStart(4)} already in gateway ${r.empty ? `${r.empty} empty (skipped)` : ''}`);
169
+ const total = [...perAgent.values()].reduce((n, l) => n + l.length, 0);
170
+ print('');
171
+ print(` ${total} chats to import, ${skippedExisting} already in the gateway, ${skippedEmpty} empty.`);
172
+ if (dupes.size) {
173
+ print('');
174
+ print(' Duplicate titles (gateway chat names must be unique; these get " (2)", " (3)"… in ClawChats and the gateway):');
175
+ for (const [title, n] of [...dupes].sort((a, b) => b[1] - a[1])) print(` ${String(n).padStart(4)} × "${title}"`);
176
+ }
177
+
178
+ const written = [];
179
+ if (!check && total > 0) {
180
+ for (const { agent, dir } of targets) {
181
+ fs.mkdirSync(dir, { recursive: true });
182
+ const cwd = path.join(stateDir, agent === 'main' ? 'workspace' : `workspace-${agent}`);
183
+ const store = {};
184
+ for (const t of perAgent.get(agent)) {
185
+ const sessionId = crypto.randomUUID();
186
+ const sessionFile = path.join(dir, `${sessionId}.jsonl`);
187
+ fs.writeFileSync(sessionFile, transcriptLines(sessionId, cwd, t.messages));
188
+ const start = t.messages[0].timestamp, end = t.messages[t.messages.length - 1].timestamp;
189
+ store[t.session_key] = { sessionId, sessionFile, updatedAt: end, sessionStartedAt: start, lastInteractionAt: end, chatType: 'direct', lastChannel: 'webchat' };
190
+ written.push(sessionFile);
191
+ }
192
+ const storePath = path.join(dir, 'sessions.json');
193
+ fs.writeFileSync(storePath, JSON.stringify(store, null, 2));
194
+ written.push(storePath);
195
+ }
196
+ print('');
197
+ print(' Written. Finish the import now (the gateway is offline for a minute or two):');
198
+ print('');
199
+ print(' ocplatform gateway stop');
200
+ print(' ocplatform doctor --fix');
201
+ print(' ocplatform gateway start');
202
+ print('');
203
+ print(' Run these right away: until `doctor --fix` runs, the gateway refuses to restart.');
204
+ } else if (check) {
205
+ print('');
206
+ print(' Run `ocplatform clawchats export-history` (without "check") to write the files.');
207
+ } else {
208
+ print('');
209
+ print(' Nothing to import.');
210
+ }
211
+ return { exported: total, skippedExisting, skippedEmpty, written };
212
+ }
package/server/index.js CHANGED
@@ -1,32 +1,24 @@
1
1
  import http from 'node:http';
2
2
  import fs from 'node:fs';
3
3
  import path from 'node:path';
4
- import os from 'node:os';
5
4
  import { fileURLToPath } from 'node:url';
6
- import { WebSocket as WS, WebSocketServer } from 'ws';
5
+ import { WebSocketServer } from 'ws';
7
6
 
8
- import { Database, requestDbStore } from './bootstrap/native.js';
9
- import { GATEWAY_WS_URL, AUTH_TOKEN, getSessionsDirForAgent } from './config.js';
7
+ import { Database } from './bootstrap/native.js';
8
+ import { GATEWAY_WS_URL, AUTH_TOKEN, discoverWorkspaceDir } from './config.js';
10
9
  import { DebugLogger } from './debug.js';
11
10
  import { GatewayClient } from './gateway.js';
12
- import { discoverMemoryConfig } from './providers/memory-config.js';
13
- import { createMemoryProvider } from './providers/memory.js';
14
- import { WorkspaceController } from './controllers/workspaces.js';
15
- import { ThreadController } from './controllers/threads.js';
16
- import { MessageController } from './controllers/messages.js';
17
- import { FileController } from './controllers/files.js';
18
- import { MemoryController } from './controllers/memory.js';
19
11
  import { handleServeFile, handleWorkspaceList, handleWorkspaceFileRead, handleWorkspaceFileWrite, handleWorkspaceFileDelete, handleWorkspaceUpload } from './controllers/filesystem.js';
20
12
  import { handleTranscribe } from './controllers/transcribe.js';
13
+ import { createGatewayMediaHandler } from './controllers/gateway-media.js';
21
14
  import { handleStatic } from './controllers/static.js';
22
15
  import { handleAgents } from './controllers/agents.js';
23
16
  import { createSettingsHandlers } from './controllers/settings.js';
24
17
  import { createWorkspaceStore } from './store/workspace-store.js';
25
- import { cleanGatewaySession, setGatewayClient } from './gateway-cleanup.js';
26
- import { parseSessionKey } from './util/helpers.js';
18
+ import { createExtrasStore } from './store/extras-store.js';
19
+ import { SessionLens } from './session-lens.js';
27
20
  import { send, sendError, parseBody, uuid, matchRoute, setCors } from './util/http.js';
28
21
 
29
- const HOME = os.homedir();
30
22
  // PORT is passed via createApp(config.port); env var is read by plugin host (src/index.ts).
31
23
  const DEFAULT_PORT = 3001;
32
24
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -37,10 +29,8 @@ const PLUGIN_DIR = path.resolve(__dirname, '..');
37
29
  export function createApp(config = {}) {
38
30
  const PORT = config.port || DEFAULT_PORT;
39
31
  const DATA_DIR = config.dataDir || path.join(PLUGIN_DIR, 'data');
40
- const UPLOADS_DIR = config.uploadsDir || path.join(PLUGIN_DIR, 'uploads');
41
32
  const WORKSPACES_FILE = path.join(DATA_DIR, 'workspaces.json');
42
33
  const SETTINGS_FILE = path.join(DATA_DIR, 'settings.json');
43
- const INTELLIGENCE_DIR = path.join(DATA_DIR, 'intelligence');
44
34
 
45
35
  const authToken = config.authToken !== undefined ? config.authToken : AUTH_TOKEN;
46
36
  const gatewayToken = config.gatewayToken !== undefined ? config.gatewayToken : authToken;
@@ -48,22 +38,8 @@ export function createApp(config = {}) {
48
38
  const openaiApiKey = config.openaiApiKey || null;
49
39
 
50
40
  fs.mkdirSync(DATA_DIR, { recursive: true });
51
- fs.mkdirSync(UPLOADS_DIR, { recursive: true });
52
-
53
- // Per-workspace SQLite databases
54
- const dbCache = new Map();
55
- function getDb(workspaceName) {
56
- if (dbCache.has(workspaceName)) return dbCache.get(workspaceName);
57
- const db = new Database(path.join(DATA_DIR, `${workspaceName}.db`));
58
- db.exec('PRAGMA journal_mode = WAL');
59
- db.exec('PRAGMA foreign_keys = ON');
60
- migrate(db);
61
- dbCache.set(workspaceName, db);
62
- return db;
63
- }
64
- function getActiveDb() { return requestDbStore.getStore() || getDb(getWorkspaces().active); }
65
- function closeDb(name) { const db = dbCache.get(name); if (db) { db.close(); dbCache.delete(name); } }
66
- function closeAll() { for (const db of dbCache.values()) db.close(); dbCache.clear(); globalDbCache.close?.(); }
41
+
42
+ function closeAll() { gatewayClient?.close(); globalDbCache.close?.(); }
67
43
 
68
44
  // Global DB (custom emojis, cross-workspace data)
69
45
  let _globalDb = null;
@@ -73,7 +49,6 @@ export function createApp(config = {}) {
73
49
  _globalDb = new Database(path.join(DATA_DIR, 'global.db'));
74
50
  _globalDb.exec('PRAGMA journal_mode = WAL');
75
51
  _globalDb.exec(`CREATE TABLE IF NOT EXISTS custom_emojis (name TEXT NOT NULL, pack TEXT NOT NULL DEFAULT 'slackmojis', url TEXT NOT NULL, mime_type TEXT, created_at INTEGER DEFAULT (strftime('%s','now')), PRIMARY KEY (name, pack))`);
76
- _globalDb.exec(`CREATE TABLE IF NOT EXISTS prompts (id TEXT PRIMARY KEY, title TEXT NOT NULL, content TEXT NOT NULL, category TEXT DEFAULT '', variables TEXT DEFAULT '[]', created_at INTEGER, updated_at INTEGER)`);
77
52
  return _globalDb;
78
53
  },
79
54
  close() { if (_globalDb) { _globalDb.close(); _globalDb = null; } }
@@ -84,22 +59,19 @@ export function createApp(config = {}) {
84
59
 
85
60
  const debugLogger = new DebugLogger(DATA_DIR);
86
61
 
87
- const memoryConfig = discoverMemoryConfig(config.memoryEnv || {});
88
- const memoryProvider = createMemoryProvider(memoryConfig);
89
- memoryProvider.init().catch(err => console.error('[createApp] Memory provider init error:', err.message));
90
- const MEMORY_FILES_DIR = path.join(memoryConfig.workspaceDir, 'memory');
62
+ const workspaceDir = discoverWorkspaceDir();
91
63
 
92
64
  // Instantiate the gateway client with all dependencies injected
93
- const gatewayClient = new GatewayClient({ getDb, getWorkspaces, dataDir: DATA_DIR, debugLogger, gatewayWsUrl: gatewayUrl, authToken: gatewayToken });
65
+ const gatewayClient = new GatewayClient({ dataDir: DATA_DIR, debugLogger, gatewayWsUrl: gatewayUrl, authToken: gatewayToken });
94
66
  const broadcast = msg => gatewayClient.broadcastToBrowsers(msg);
95
- setGatewayClient(gatewayClient);
67
+ // ClawChats-only data + the gateway↔browser session filter (gateway-native architecture).
68
+ const extras = createExtrasStore(() => globalDbCache.get());
69
+ try { const n = extras.seedFromWorkspaces(getWorkspaces()); if (n) console.log(`[extras] seeded ${n} project style(s) from workspaces.json`); }
70
+ catch (e) { console.error('[extras] style seed failed:', e.message); }
71
+ gatewayClient.lens = new SessionLens({ broadcast, request: (m, p, t) => gatewayClient.request(m, p, t), extras });
96
72
 
97
73
  // Instantiate controllers
98
- const workspaces = new WorkspaceController({ getDb, closeDb, getWorkspaces, setWorkspaces, dataDir: DATA_DIR, broadcast });
99
- const threads = new ThreadController({ getActiveDb, getWorkspaces, uploadsDir: UPLOADS_DIR, broadcast });
100
- const messages = new MessageController({ getActiveDb, getWorkspaces, broadcast });
101
- const files = new FileController({ getActiveDb, getWorkspaces, uploadsDir: UPLOADS_DIR, intelligenceDir: INTELLIGENCE_DIR });
102
- const memory = new MemoryController({ memoryProvider, memoryFilesDir: MEMORY_FILES_DIR, memoryConfig });
74
+ const handleGatewayMedia = createGatewayMediaHandler({ gatewayWsUrl: gatewayUrl, gatewayToken });
103
75
 
104
76
  // Settings — file I/O lives in settings.js
105
77
  const { handleGetSettings, handleSaveSettings } = createSettingsHandlers(SETTINGS_FILE);
@@ -115,9 +87,7 @@ export function createApp(config = {}) {
115
87
 
116
88
  // Request handler
117
89
  async function handleRequest(req, res) {
118
- const wsName = req.headers?.['x-workspace'];
119
- const db = wsName ? getDb(wsName) : getActiveDb();
120
- return requestDbStore.run(db, () => route(req, res));
90
+ return route(req, res);
121
91
  }
122
92
 
123
93
  async function route(req, res) {
@@ -135,7 +105,6 @@ export function createApp(config = {}) {
135
105
  }
136
106
 
137
107
  // Unauthenticated routes
138
- if ((p = matchRoute(method, urlPath, 'GET /api/uploads/:threadId/:fileId'))) return files.serveUpload(req, res, p);
139
108
 
140
109
  if (method === 'GET' && urlPath === '/api/emoji') {
141
110
  try { const rows = globalDbCache.get().prepare('SELECT name, pack, url, mime_type FROM custom_emojis ORDER BY created_at DESC').all(); res.writeHead(200, { 'Content-Type': 'application/json', 'Cache-Control': 'public, max-age=300' }); return res.end(JSON.stringify(rows)); }
@@ -179,38 +148,14 @@ export function createApp(config = {}) {
179
148
  }
180
149
 
181
150
  // File serving & workspace browser
182
- if (method === 'GET' && urlPath === '/api/file') return handleServeFile(req, res, query, memoryConfig);
151
+ if (method === 'GET' && urlPath === '/api/file') return handleServeFile(req, res, query, workspaceDir);
152
+ if (method === 'GET' && urlPath === '/api/gw-media') return handleGatewayMedia(req, res, query);
183
153
  if (method === 'GET' && urlPath === '/api/workspace') return handleWorkspaceList(req, res, query);
184
154
  if (method === 'GET' && urlPath === '/api/workspace/file') return handleWorkspaceFileRead(req, res, query);
185
155
  if (method === 'PUT' && urlPath === '/api/workspace/file') return await handleWorkspaceFileWrite(req, res, query);
186
156
  if (method === 'DELETE' && urlPath === '/api/workspace/file') return handleWorkspaceFileDelete(req, res, query);
187
157
  if (method === 'POST' && urlPath === '/api/workspace/upload') return await handleWorkspaceUpload(req, res, query);
188
158
 
189
- // Memory
190
- if (method === 'GET' && urlPath === '/api/memory/status') return await memory.status(req, res);
191
- if (method === 'GET' && urlPath === '/api/memory/list') return await memory.list(req, res, query);
192
- if (method === 'GET' && urlPath === '/api/memory/search') return await memory.search(req, res, query);
193
- if (method === 'GET' && urlPath === '/api/memory/files') return memory.files(req, res, query);
194
- if ((p = matchRoute(method, urlPath, 'PUT /api/memory/:id'))) return await memory.update(req, res, p);
195
- if ((p = matchRoute(method, urlPath, 'DELETE /api/memory/:id'))) return await memory.delete(req, res, p);
196
-
197
- // Prompt library
198
- if (method === 'GET' && urlPath === '/api/prompts') {
199
- const rows = globalDbCache.get().prepare('SELECT * FROM prompts ORDER BY created_at ASC').all();
200
- return send(res, 200, rows.map(r => ({ id: r.id, title: r.title, content: r.content, category: r.category, variables: JSON.parse(r.variables || '[]'), createdAt: r.created_at, updatedAt: r.updated_at })));
201
- }
202
- if (method === 'PUT' && urlPath === '/api/prompts') {
203
- const body = await parseBody(req);
204
- const prompts = Array.isArray(body) ? body : [];
205
- const db = globalDbCache.get();
206
- const upsert = db.prepare('INSERT OR REPLACE INTO prompts (id, title, content, category, variables, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)');
207
- db.transaction(() => {
208
- db.prepare('DELETE FROM prompts').run();
209
- for (const p of prompts) upsert.run(p.id, p.title, p.content, p.category || '', JSON.stringify(p.variables || []), p.createdAt || Date.now(), p.updatedAt || Date.now());
210
- })();
211
- return send(res, 200, { ok: true });
212
- }
213
-
214
159
  // Settings & misc
215
160
  if (method === 'GET' && urlPath === '/api/settings') return handleGetSettings(req, res);
216
161
  if (method === 'PUT' && urlPath === '/api/settings') return await handleSaveSettings(req, res, parseBody);
@@ -218,59 +163,29 @@ export function createApp(config = {}) {
218
163
  if (method === 'GET' && urlPath === '/api/health') return send(res, 200, { ok: true, workspace: getWorkspaces().active, uptime: process.uptime() });
219
164
  if (method === 'GET' && urlPath === '/api/agents') return handleAgents(req, res);
220
165
 
221
- // Workspaces
222
- if (method === 'GET' && urlPath === '/api/workspaces') return workspaces.getAll(req, res);
223
- if (method === 'POST' && urlPath === '/api/workspaces') return await workspaces.create(req, res);
224
- if ((p = matchRoute(method, urlPath, 'PATCH /api/workspaces/:name'))) return await workspaces.update(req, res, p);
225
- if ((p = matchRoute(method, urlPath, 'DELETE /api/workspaces/:name'))) return workspaces.delete(req, res, p);
226
- if (method === 'POST' && urlPath === '/api/workspaces/reorder') return await workspaces.reorder(req, res);
227
- if ((p = matchRoute(method, urlPath, 'POST /api/workspaces/:name/activate'))) return workspaces.activate(req, res, p);
228
-
229
- // Threads
230
- if (method === 'GET' && urlPath === '/api/threads') return threads.getAll(req, res, {}, query);
231
- if (method === 'GET' && urlPath === '/api/threads/unread') return threads.getUnread(req, res);
232
- if (method === 'POST' && urlPath === '/api/threads') return await threads.create(req, res);
233
- if ((p = matchRoute(method, urlPath, 'POST /api/threads/:id/mark-read'))) return await threads.markRead(req, res, p);
234
- if ((p = matchRoute(method, urlPath, 'GET /api/threads/:id/messages'))) return messages.getAll(req, res, p, query);
235
- if ((p = matchRoute(method, urlPath, 'POST /api/threads/:id/messages'))) return await messages.create(req, res, p);
236
- if ((p = matchRoute(method, urlPath, 'DELETE /api/threads/:id/messages/:messageId'))) return messages.delete(req, res, p);
237
- if ((p = matchRoute(method, urlPath, 'POST /api/threads/:id/context-fill'))) return messages.contextFill(req, res, p);
238
- if ((p = matchRoute(method, urlPath, 'POST /api/threads/:id/generate-title'))) {
239
- const db = getActiveDb();
240
- const thread = db.prepare('SELECT * FROM threads WHERE id = ?').get(p.id);
241
- if (!thread) return sendError(res, 404, 'Thread not found');
242
- gatewayClient.generateThreadTitle(db, p.id, getWorkspaces().active);
243
- return send(res, 200, { ok: true });
166
+ // ClawChats-only extras (see EXTRAS.md)
167
+ if (method === 'GET' && urlPath === '/api/extras/project-styles') {
168
+ extras.seedFromWorkspaces(getWorkspaces()); // legacy projects renamed by gateway sync keep their style
169
+ return send(res, 200, { styles: extras.getProjectStyles() });
244
170
  }
245
- if ((p = matchRoute(method, urlPath, 'POST /api/threads/:id/upload'))) return await files.upload(req, res, p);
246
- if ((p = matchRoute(method, urlPath, 'GET /api/threads/:id/intelligence'))) return files.getIntelligence(req, res, p);
247
- if ((p = matchRoute(method, urlPath, 'POST /api/threads/:id/intelligence'))) return await files.saveIntelligence(req, res, p);
248
- if ((p = matchRoute(method, urlPath, 'GET /api/threads/:id'))) return threads.get(req, res, p);
249
- if ((p = matchRoute(method, urlPath, 'PATCH /api/threads/:id'))) return await threads.update(req, res, p);
250
- if ((p = matchRoute(method, urlPath, 'DELETE /api/threads/:id'))) return threads.delete(req, res, p);
251
-
252
- // Search / export / import
253
- if (method === 'GET' && urlPath === '/api/search') return messages.search(req, res, {}, query);
254
- if (method === 'GET' && urlPath === '/api/export') return messages.export(req, res);
255
- if (method === 'POST' && urlPath === '/api/import') return await messages.import(req, res);
256
-
257
- if (method === 'POST' && urlPath === '/api/active-thread') {
171
+ if ((p = matchRoute(method, urlPath, 'PUT /api/extras/project-styles/:name'))) {
258
172
  const body = await parseBody(req);
259
- if (body.threadId && body.workspace) gatewayClient.setActiveThread(null, body.workspace, body.threadId);
260
- return send(res, 200, { ok: true });
173
+ extras.setProjectStyle(p.name, { color: body.color, icon: body.icon });
174
+ broadcast(JSON.stringify({ type: 'clawchats', event: 'project-styles-changed' }));
175
+ return send(res, 200, { styles: extras.getProjectStyles() });
261
176
  }
262
-
263
- if (method === 'POST' && urlPath === '/api/incognito/cleanup') {
264
- const { sessionKey, threadId } = await parseBody(req);
265
- if (sessionKey) cleanGatewaySession(sessionKey);
266
- if (threadId) { try { fs.rmSync(path.join(UPLOADS_DIR, threadId), { recursive: true }); } catch { /* ok */ } }
177
+ if ((p = matchRoute(method, urlPath, 'DELETE /api/extras/project-styles/:name'))) {
178
+ extras.deleteProjectStyle(p.name);
179
+ broadcast(JSON.stringify({ type: 'clawchats', event: 'project-styles-changed' }));
267
180
  return send(res, 200, { ok: true });
268
181
  }
269
182
 
183
+
270
184
  sendError(res, 404, `Not found: ${method} ${urlPath}`);
271
185
  } catch (err) {
272
186
  console.error(`Error handling ${method} ${urlPath}:`, err);
273
- if (err.message?.includes('UNIQUE constraint')) sendError(res, 409, 'Conflict: ' + err.message);
187
+ if (err.gatewayError) sendError(res, 502, `Gateway: ${err.message}`);
188
+ else if (err.message?.includes('UNIQUE constraint')) sendError(res, 409, 'Conflict: ' + err.message);
274
189
  else sendError(res, 500, err.message || 'Internal server error');
275
190
  }
276
191
  }
@@ -285,7 +200,6 @@ export function createApp(config = {}) {
285
200
  ws.on('message', async data => {
286
201
  const msgStr = data.toString();
287
202
  debugLogger.logFrame('BR→SRV', msgStr);
288
- let msgToForward = msgStr;
289
203
  try {
290
204
  const msg = JSON.parse(msgStr);
291
205
  if (msg.type === 'req' && msg.method === 'connect') {
@@ -299,34 +213,12 @@ export function createApp(config = {}) {
299
213
  return;
300
214
  }
301
215
  if (msg.type === 'clawchats' || msg.type === 'shellchat') {
302
- if (msg.action === 'active-thread') { gatewayClient.setActiveThread(ws, msg.workspace, msg.threadId); return; }
216
+ if (msg.action === 'active-thread') return; // sent by pre-gateway-native frontends; nothing to track now
303
217
  if (msg.action === 'debug-start') { const r = debugLogger.start(msg.ts, ws); ws.send(JSON.stringify(r.error === 'already-active' ? { type: 'clawchats', event: 'debug-error', error: 'Recording already active in another tab', sessionId: r.sessionId } : { type: 'clawchats', event: 'debug-started', sessionId: r.sessionId })); return; }
304
218
  if (msg.action === 'debug-dump') { const r = debugLogger.saveDump(msg); ws.send(JSON.stringify({ type: 'clawchats', event: 'debug-saved', sessionId: r.sessionId, files: r.files })); return; }
305
219
  }
306
- // Save inline attachments to disk before forwarding to gateway
307
- if (msg.type === 'req' && msg.method === 'chat.send' && msg.params?.attachments?.length > 0) {
308
- const parsed = parseSessionKey(msg.params.sessionKey || '');
309
- const threadId = parsed?.threadId || 'misc';
310
- const uploadDir = path.join(UPLOADS_DIR, threadId);
311
- fs.mkdirSync(uploadDir, { recursive: true });
312
- const extMap = { jpeg: 'jpg', jpg: 'jpg', png: 'png', gif: 'gif', webp: 'webp', pdf: 'pdf', 'svg+xml': 'svg', mp3: 'mp3', mp4: 'mp4', wav: 'wav', webm: 'webm' };
313
- const savedPaths = [];
314
- for (const att of msg.params.attachments) {
315
- if (!att.content || !att.mimeType) continue;
316
- try {
317
- const rawExt = att.mimeType.split('/')[1]?.split(';')[0] || 'bin';
318
- const filePath = path.join(uploadDir, `${Date.now()}_${Math.random().toString(36).slice(2, 6)}.${extMap[rawExt] || rawExt}`);
319
- fs.writeFileSync(filePath, Buffer.from(att.content, 'base64'));
320
- savedPaths.push(filePath);
321
- } catch (err) { console.error('[upload] Failed to save attachment:', err.message); }
322
- }
323
- if (savedPaths.length > 0) {
324
- const note = `\n\n[${savedPaths.length === 1 ? 'Attached file saved on disk' : 'Attached files saved on disk'}:\n${savedPaths.map(p => `- ${p}`).join('\n')}]`;
325
- msgToForward = JSON.stringify({ ...msg, params: { ...msg.params, message: (msg.params.message || '') + note } });
326
- }
327
- }
328
220
  } catch { /* not JSON or not a ClawChats message, forward as-is */ }
329
- gatewayClient.sendToGateway(msgToForward);
221
+ gatewayClient.forwardFromBrowser(msgStr);
330
222
  });
331
223
 
332
224
  ws.on('close', () => { console.log('Browser client disconnected'); debugLogger.handleClientDisconnect(ws); gatewayClient.removeBrowserClient(ws); });
@@ -336,77 +228,21 @@ export function createApp(config = {}) {
336
228
 
337
229
  return {
338
230
  handleRequest,
339
- getDb,
340
- getActiveDb,
341
231
  getWorkspaces,
342
232
  setWorkspaces,
343
233
  shutdown: closeAll,
344
- closeAllDbs: closeAll,
345
234
  gatewayClient,
235
+ extras,
346
236
  setupBrowserWs,
347
237
  debugLogger,
348
238
  dataDir: DATA_DIR,
349
239
  };
350
240
  }
351
241
 
352
- // Migration — kept here as it references Database-level constructs shared across modules
353
- function migrate(db) {
354
- db.exec(`
355
- CREATE TABLE IF NOT EXISTS threads (
356
- id TEXT PRIMARY KEY, session_key TEXT UNIQUE NOT NULL, title TEXT DEFAULT 'New chat',
357
- pinned INTEGER DEFAULT 0, pin_order INTEGER DEFAULT 0, model TEXT,
358
- last_session_id TEXT, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL
359
- );
360
- CREATE TABLE IF NOT EXISTS messages (
361
- id TEXT PRIMARY KEY, thread_id TEXT NOT NULL, role TEXT NOT NULL, content TEXT NOT NULL,
362
- status TEXT DEFAULT 'sent', metadata TEXT, seq INTEGER, timestamp INTEGER NOT NULL, created_at INTEGER NOT NULL,
363
- FOREIGN KEY (thread_id) REFERENCES threads(id) ON DELETE CASCADE
364
- );
365
- CREATE INDEX IF NOT EXISTS idx_messages_thread ON messages(thread_id, timestamp);
366
- CREATE INDEX IF NOT EXISTS idx_messages_dedup ON messages(thread_id, role, timestamp);
367
- `);
368
- try { db.exec('ALTER TABLE threads ADD COLUMN sort_order INTEGER DEFAULT 0'); } catch { /* exists */ }
369
- try { db.exec('ALTER TABLE threads ADD COLUMN unread_count INTEGER DEFAULT 0'); } catch { /* exists */ }
370
- try { db.prepare('ALTER TABLE threads ADD COLUMN metadata TEXT DEFAULT NULL').run(); } catch {}
371
- db.exec(`CREATE TABLE IF NOT EXISTS unread_messages (thread_id TEXT NOT NULL, message_id TEXT NOT NULL, created_at INTEGER NOT NULL, PRIMARY KEY (thread_id, message_id), FOREIGN KEY (thread_id) REFERENCES threads(id) ON DELETE CASCADE)`);
372
- db.exec('CREATE INDEX IF NOT EXISTS idx_unread_thread ON unread_messages(thread_id)');
373
- ensureFts(db);
374
- }
375
-
376
- function createFts(db) {
377
- db.exec(`CREATE VIRTUAL TABLE messages_fts USING fts5(content, content=messages, content_rowid=rowid, tokenize='porter unicode61 tokenchars x27')`);
378
- db.exec(`CREATE TRIGGER messages_ai AFTER INSERT ON messages BEGIN INSERT INTO messages_fts(rowid, content) VALUES (new.rowid, new.content); END`);
379
- db.exec(`CREATE TRIGGER messages_ad AFTER DELETE ON messages BEGIN INSERT INTO messages_fts(messages_fts, rowid, content) VALUES('delete', old.rowid, old.content); END`);
380
- db.exec(`CREATE TRIGGER messages_au AFTER UPDATE ON messages BEGIN INSERT INTO messages_fts(messages_fts, rowid, content) VALUES('delete', old.rowid, old.content); INSERT INTO messages_fts(rowid, content) VALUES (new.rowid, new.content); END`);
381
- }
382
-
383
- function dropFts(db) {
384
- db.exec('DROP TABLE IF EXISTS messages_fts; DROP TRIGGER IF EXISTS messages_ai; DROP TRIGGER IF EXISTS messages_ad; DROP TRIGGER IF EXISTS messages_au;');
385
- }
386
-
387
- function ensureFts(db) {
388
- const hasFts = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='messages_fts'").get();
389
- if (!hasFts) {
390
- try { createFts(db); db.prepare("INSERT INTO messages_fts(messages_fts) VALUES('rebuild')").run(); }
391
- catch (e) { console.error('[DB] messages_fts creation failed:', e.message); }
392
- return;
393
- }
394
- const schema = db.prepare("SELECT sql FROM sqlite_master WHERE type='table' AND name='messages_fts'").get();
395
- if (schema && !schema.sql.includes('tokenchars')) {
396
- console.log('[DB] Upgrading messages_fts tokenizer...');
397
- try { dropFts(db); createFts(db); db.prepare("INSERT INTO messages_fts(messages_fts) VALUES('rebuild')").run(); console.log('[DB] Upgrade complete'); }
398
- catch (e) { console.error('[DB] Upgrade failed:', e.message); dropFts(db); }
399
- } else {
400
- try { db.prepare("INSERT INTO messages_fts(messages_fts) VALUES('integrity-check')").run(); }
401
- catch { try { db.prepare("INSERT INTO messages_fts(messages_fts) VALUES('rebuild')").run(); } catch (e) { console.error('[DB] FTS rebuild failed:', e.message); dropFts(db); } }
402
- }
403
- }
404
-
405
242
  // Standalone mode (node server/index.js or via isDirectRun check in bundle)
406
243
  const isDirectRun = import.meta.url === `file://${process.argv[1]}`;
407
244
  if (isDirectRun) {
408
245
  const app = createApp();
409
- app.getActiveDb();
410
246
 
411
247
  const server = http.createServer(app.handleRequest);
412
248
  const wss = new WebSocketServer({ noServer: true });