@clawchatsai/connector 0.2.4 → 0.2.6

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,57 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { documentsOf, parseSelectionComment, commentAttachments } from './text.js';
4
+
5
+ const meta = (id, extra = {}) => ({ __openclaw: { id, ...extra } });
6
+
7
+ function commentBody({ text, comment = '', key = 'agent:main:dashboard:a' }) {
8
+ // Same as frontend/chat/selection-comments.js formatSelectionComment.
9
+ return [`Selected text:\n${text}`, ...(comment.trim() ? [`User comment:\n${comment}`] : []),
10
+ [`Source session: ${key}`, 'Source message: m1', `Selected text UTF-16 length: ${text.length}`, 'DOM text UTF-16 range: [0, 4)'].join('\n')].join('\n\n');
11
+ }
12
+
13
+ test('user and assistant text; tool calls, thinking and directives stay out', () => {
14
+ assert.deepEqual(documentsOf({ role: 'user', content: 'deploy the connector', timestamp: 5, ...meta('u1') }),
15
+ { id: 'u1', ts: 5, truncated: false, docs: [{ kind: 'message', role: 'user', text: 'deploy the connector' }], comments: [] });
16
+ const a = documentsOf({ role: 'assistant', timestamp: '2026-11-20T02:39:32.509Z', ...meta('a1'), content: [
17
+ { type: 'thinking', thinking: 'secret plan' }, { type: 'toolcall', name: 'Bash' }, { type: 'text', text: '[[reply_to_current]] Done, it works.' }] });
18
+ assert.equal(a.docs[0].text, 'Done, it works.');
19
+ assert.equal(a.ts, Date.parse('2026-11-20T02:39:32.509Z'));
20
+ assert.equal(documentsOf({ role: 'toolResult', content: 'output', ...meta('t1') }), null);
21
+ assert.equal(documentsOf({ role: 'assistant', content: [{ type: 'toolcall' }], ...meta('a2') }), null, 'nothing searchable');
22
+ assert.equal(documentsOf({ role: 'user', content: 'no id' }), null);
23
+ });
24
+
25
+ test('a truncated entry is reported so the indexer fetches it whole', () => {
26
+ const e = documentsOf({ role: 'assistant', content: [{ type: 'text', text: 'long…' }], ...meta('a1', { truncated: true, reason: 'display-cap' }) });
27
+ assert.equal(e.truncated, true);
28
+ });
29
+
30
+ test('selection comments: attachments listed, body parsed by its declared length', () => {
31
+ const m = { role: 'user', content: '', ...meta('u1', { media: [
32
+ { url: 'media://inbound/selection-comment---1.txt', fileName: 'selection-comment.txt', contentType: 'text/plain' },
33
+ { url: 'media://inbound/photo.png', fileName: 'photo.png', kind: 'image' },
34
+ { url: 'https://evil/x', fileName: 'selection-comment.txt' }] }) };
35
+ assert.deepEqual(commentAttachments(m), [{ url: 'media://inbound/selection-comment---1.txt' }]);
36
+ const e = documentsOf(m);
37
+ assert.deepEqual(e.docs, []);
38
+ assert.equal(e.comments.length, 1);
39
+
40
+ assert.deepEqual(parseSelectionComment(commentBody({ text: 'Plan\n\nBuild 1', comment: 'work this plan' })), { quote: 'Plan\n\nBuild 1', comment: 'work this plan' });
41
+ // A passage that itself contains the comment marker is cut by length, not by marker.
42
+ const tricky = 'a\n\nUser comment:\nnot a comment';
43
+ assert.deepEqual(parseSelectionComment(commentBody({ text: tricky, comment: 'real' })), { quote: tricky, comment: 'real' });
44
+ assert.deepEqual(parseSelectionComment(commentBody({ text: 'just a quote' })), { quote: 'just a quote', comment: '' });
45
+ assert.equal(parseSelectionComment('random text file'), null);
46
+ assert.equal(parseSelectionComment(null), null);
47
+ });
48
+
49
+ test('team rooms: author map decides the role, label prefix and timeline lines go', () => {
50
+ const team = { e1: { type: 'user' }, e2: { type: 'person', name: 'Kamil' }, e3: { type: 'agent', agentId: 'main' }, e4: { type: 'system' } };
51
+ const entry = (id, text) => ({ role: 'assistant', content: [{ type: 'text', text }], ...meta(id) });
52
+ assert.deepEqual(documentsOf(entry('e1', '[Houman]\n\nship it'), { team }).docs, [{ kind: 'message', role: 'user', text: 'ship it' }]);
53
+ assert.equal(documentsOf(entry('e2', '[Kamil]\n\nhello'), { team }).docs[0].role, 'person');
54
+ assert.equal(documentsOf(entry('e3', '[homiabot]\n\non it'), { team }).docs[0].role, 'assistant');
55
+ assert.equal(documentsOf(entry('e4', '[System]\n\nKamil joined'), { team }), null);
56
+ assert.equal(documentsOf(entry('e9', '[System]\n\nold host line'), { team }), null, 'unknown System line');
57
+ });
@@ -9,26 +9,53 @@ import path from 'node:path';
9
9
  import { Database } from '../bootstrap/native.js';
10
10
  import { createExtrasStore } from './extras-store.js';
11
11
 
12
- /** [{ sessionKey, createdAt }] from every legacy project DB in `dataDir`; unreadable files are skipped. */
13
- export function readLegacyThreadDates(dataDir, logger = console) {
14
- const out = [];
12
+ /** `read(db)` over every legacy project DB in `dataDir` that has a threads table; unreadable files are skipped. */
13
+ function eachLegacyDb(dataDir, logger, read) {
15
14
  let files = [];
16
- try { files = fs.readdirSync(dataDir).filter(f => f.endsWith('.db') && f !== 'global.db'); } catch { return out; }
15
+ try { files = fs.readdirSync(dataDir).filter(f => f.endsWith('.db') && f !== 'global.db' && f !== 'search.db'); } catch { return; }
17
16
  for (const f of files) {
18
17
  let db;
19
18
  try {
20
19
  db = new Database(path.join(dataDir, f), { readOnly: true });
21
20
  const hasThreads = db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'threads'").get();
22
- if (!hasThreads) continue;
23
- for (const r of db.prepare('SELECT session_key, created_at FROM threads WHERE session_key IS NOT NULL AND created_at > 0').all()) {
24
- out.push({ sessionKey: r.session_key, createdAt: Number(r.created_at) });
25
- }
21
+ if (hasThreads) read(db);
26
22
  } catch (e) {
27
23
  logger.warn?.(`[legacy] skipped ${f}: ${e.message}`);
28
24
  } finally {
29
25
  db?.close();
30
26
  }
31
27
  }
28
+ }
29
+
30
+ /** [{ sessionKey, createdAt }] from every legacy project DB in `dataDir`. */
31
+ export function readLegacyThreadDates(dataDir, logger = console) {
32
+ const out = [];
33
+ eachLegacyDb(dataDir, logger, db => {
34
+ for (const r of db.prepare('SELECT session_key, created_at FROM threads WHERE session_key IS NOT NULL AND created_at > 0').all()) {
35
+ out.push({ sessionKey: r.session_key, createdAt: Number(r.created_at) });
36
+ }
37
+ });
38
+ return out;
39
+ }
40
+
41
+ /**
42
+ * The old Smart-search summaries ([{ key, summary, keywords, messages, taggedAt }]): the old app wrote
43
+ * `metadata = { summary, topics, entities, … }` per thread. Topics were free phrases, not shared tags,
44
+ * so they become keywords (searchable) and the chat is tagged like any other later.
45
+ */
46
+ export function readLegacyThreadSummaries(dataDir, logger = console) {
47
+ const out = [];
48
+ eachLegacyDb(dataDir, logger, db => {
49
+ if (!db.prepare('PRAGMA table_info(threads)').all().some(c => c.name === 'metadata')) return;
50
+ for (const r of db.prepare("SELECT session_key, metadata FROM threads WHERE session_key IS NOT NULL AND metadata LIKE '%summary%'").all()) {
51
+ let m;
52
+ try { m = JSON.parse(r.metadata); } catch { continue; }
53
+ const summary = typeof m?.summary === 'string' ? m.summary.trim() : '';
54
+ if (!summary) continue;
55
+ const words = [...(Array.isArray(m.topics) ? m.topics : []), ...(Array.isArray(m.entities) ? m.entities : [])].filter(w => typeof w === 'string' && w.trim());
56
+ out.push({ key: r.session_key, summary: summary.slice(0, 200), keywords: [...new Set(words.map(w => w.trim().slice(0, 60)))].slice(0, 10), messages: 0, taggedAt: Number(m.generatedAt) || null });
57
+ }
58
+ });
32
59
  return out;
33
60
  }
34
61
 
@@ -0,0 +1,132 @@
1
+ // Chat tags: pure helpers behind the tagger (tagger.js). No I/O.
2
+ //
3
+ // A chat's tags are a few broad subjects (`proxmox`, `laser-show`) shared across chats, so they can
4
+ // be browsed (#proxmox in search). A tag is lowercase words joined by hyphens: one token to type
5
+ // and click, while the index still finds it by each word. The model is shown the tags already in
6
+ // use and asked to reuse them; normalizeTag() folds what it still gets wrong (case, '#', spaces,
7
+ // a plural of an existing tag) and the user's renames and deletions (aliases).
8
+
9
+ export const MAX_TAGS = 5;
10
+ export const MAX_TAG_CHARS = 32;
11
+ export const MAX_KEYWORDS = 10;
12
+ export const MAX_SUMMARY_CHARS = 200;
13
+ const MAX_TAG_WORDS = 3;
14
+ const PROMPT_CHARS = 12_000; // conversation text sent per chat
15
+ const MESSAGE_CHARS = 600; // per message
16
+ const HEAD_MESSAGES = 4; // always sent: how the chat started
17
+ const VOCABULARY_SHOWN = 80;
18
+
19
+ // Says nothing about a chat's subject.
20
+ const NOISE = new Set(['chat', 'chats', 'conversation', 'discussion', 'question', 'questions', 'help', 'request', 'task', 'tasks',
21
+ 'misc', 'other', 'general', 'user', 'assistant', 'ai', 'update', 'issue', 'info', 'test']);
22
+
23
+ export const SYSTEM_PROMPT = 'You label chat conversations so their owner can find them again later. Reply with one JSON object and nothing else.';
24
+
25
+ /**
26
+ * Raw tag → canonical form, or '' when it carries nothing. `vocabulary` (Set of tags in use) maps a
27
+ * plural or singular variant onto the existing tag; `aliases` (Map tag → target, '' = deleted) applies
28
+ * the user's renames and deletions.
29
+ */
30
+ export function normalizeTag(raw, { vocabulary = null, aliases = null } = {}) {
31
+ let t = String(raw ?? '').normalize('NFKC').toLowerCase().replace(/^#+/, '')
32
+ .replace(/[_\s/]+/g, '-').replace(/[^\p{L}\p{N}+.#-]/gu, '').replace(/-{2,}/g, '-').replace(/^[-.]+|[-.]+$/g, '');
33
+ if (!t || t.split('-').length > MAX_TAG_WORDS || t.length > MAX_TAG_CHARS || NOISE.has(t) || !/\p{L}/u.test(t)) return '';
34
+ if (vocabulary && !vocabulary.has(t)) {
35
+ if (t.endsWith('s') && vocabulary.has(t.slice(0, -1))) t = t.slice(0, -1);
36
+ else if (vocabulary.has(`${t}s`)) t = `${t}s`;
37
+ }
38
+ for (let hops = 0; aliases?.has(t) && hops < 10; hops++) t = aliases.get(t);
39
+ return t;
40
+ }
41
+
42
+ /** Distinct normalized tags, in order, at most `max`. */
43
+ export function normalizeTags(list, opts = {}, max = MAX_TAGS) {
44
+ const out = [];
45
+ for (const raw of Array.isArray(list) ? list : []) {
46
+ const t = normalizeTag(raw, opts);
47
+ if (t && !out.includes(t)) out.push(t);
48
+ if (out.length >= max) break;
49
+ }
50
+ return out;
51
+ }
52
+
53
+ const oneLine = (s, n) => String(s ?? '').replace(/\s+/g, ' ').trim().slice(0, n);
54
+
55
+ /**
56
+ * The conversation as the model sees it: how it started (first HEAD_MESSAGES messages) and as much
57
+ * of the latest part as fits, each message capped. `messages`: [{ role, kind, text, quote }] oldest first.
58
+ */
59
+ export function condense(messages, budget = PROMPT_CHARS) {
60
+ const line = m => {
61
+ const who = m.kind === 'comment' ? 'User comment' : m.role === 'user' ? 'User' : 'AI';
62
+ const quote = m.kind === 'comment' && m.quote ? ` (on "${oneLine(m.quote, 160)}")` : '';
63
+ return `${who}${quote}: ${oneLine(m.text, MESSAGE_CHARS)}`;
64
+ };
65
+ const lines = messages.filter(m => m?.text?.trim()).map(line);
66
+ const head = lines.slice(0, HEAD_MESSAGES);
67
+ let used = head.reduce((n, l) => n + l.length + 2, 0);
68
+ const tail = [];
69
+ for (let i = lines.length - 1; i >= head.length; i--) {
70
+ if (used + lines[i].length + 2 > budget) break;
71
+ tail.unshift(lines[i]);
72
+ used += lines[i].length + 2;
73
+ }
74
+ const skipped = lines.length - head.length - tail.length;
75
+ return [...head, ...(skipped > 0 ? [`[… ${skipped} messages left out …]`] : []), ...tail].join('\n\n');
76
+ }
77
+
78
+ /** The user message for one chat. `vocabulary`: tags in use, most used first. */
79
+ export function buildPrompt({ title, conversation, vocabulary = [] }) {
80
+ const known = vocabulary.slice(0, VOCABULARY_SHOWN);
81
+ return `${known.length ? `Tags already in use (reuse one whenever it fits; add a new tag only when none does): ${known.join(', ')}\n\n` : ''}Chat title: ${title || '(none)'}
82
+
83
+ Conversation:
84
+ ${conversation}
85
+
86
+ Return exactly: {"summary": "...", "tags": ["..."], "keywords": ["..."]}
87
+ - summary: one sentence, at most 160 characters, saying what was done or decided and how it ended. In the conversation's language. Don't start with "The user".
88
+ - tags: 1 to ${MAX_TAGS} broad subjects someone would browse by (a project, product, system, field or kind of work), lowercase, 1 to 3 words joined by hyphens, singular. Never generic words like chat, help, question.
89
+ - keywords: up to ${MAX_KEYWORDS} specific names someone might search for: hosts, files, people, products, error codes, commands.`;
90
+ }
91
+
92
+ /**
93
+ * The model's reply → { summary, tags, keywords } (normalized), or throws when it holds no usable
94
+ * JSON object. Tolerates code fences and prose around the object.
95
+ */
96
+ export function parseReply(text, opts = {}) {
97
+ const s = String(text ?? '');
98
+ const fenced = /```(?:json)?\s*([\s\S]*?)```/.exec(s);
99
+ const body = fenced ? fenced[1] : s;
100
+ const start = body.indexOf('{'), end = body.lastIndexOf('}');
101
+ if (start < 0 || end <= start) throw new Error('reply had no JSON object');
102
+ let obj;
103
+ try { obj = JSON.parse(body.slice(start, end + 1)); } catch { throw new Error('reply JSON did not parse'); }
104
+ if (!obj || typeof obj !== 'object' || Array.isArray(obj)) throw new Error('reply was not an object');
105
+ const summary = oneLine(obj.summary, MAX_SUMMARY_CHARS);
106
+ const tags = normalizeTags(obj.tags, opts);
107
+ const keywords = [...new Set((Array.isArray(obj.keywords) ? obj.keywords : []).map(k => oneLine(k, 60)).filter(Boolean))].slice(0, MAX_KEYWORDS);
108
+ if (!summary && !tags.length) throw new Error('reply had neither a summary nor tags');
109
+ return { summary, tags, keywords };
110
+ }
111
+
112
+ /** A chat's tags as shown: the model's, minus the ones the user removed, plus the ones they added. */
113
+ export function effectiveTags({ auto = [], added = [], removed = [] }) {
114
+ const out = auto.filter(t => !removed.includes(t));
115
+ for (const t of added) if (!out.includes(t)) out.push(t);
116
+ return out;
117
+ }
118
+
119
+ // Per million tokens, for the backfill estimate before any chat was tagged (afterwards: measured cost).
120
+ const PRICES = { 'claude-haiku-4-5': { input: 1, output: 5 } };
121
+
122
+ /** Whether `model` runs on a CLI subscription (no per-token price). */
123
+ export const isSubscription = model => /^claude-cli\//.test(String(model || ''));
124
+
125
+ /** Estimated USD for tagging chats of these sizes (characters of conversation each) on `model`, or null. */
126
+ export function estimateCost(chatChars, model) {
127
+ if (isSubscription(model)) return null;
128
+ const price = Object.entries(PRICES).find(([id]) => String(model || '').includes(id))?.[1];
129
+ if (!price) return null;
130
+ const input = chatChars.reduce((n, c) => n + Math.min(c, PROMPT_CHARS) / 4 + 700, 0);
131
+ return (input * price.input + chatChars.length * 250 * price.output) / 1e6;
132
+ }
@@ -0,0 +1,73 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { normalizeTag, normalizeTags, condense, buildPrompt, parseReply, effectiveTags, estimateCost } from './model.js';
4
+
5
+ test('normalizeTag: one lowercase hyphenated token; noise and junk dropped', () => {
6
+ assert.equal(normalizeTag('#Laser Show'), 'laser-show');
7
+ assert.equal(normalizeTag('home_lab'), 'home-lab');
8
+ assert.equal(normalizeTag(' CI / CD '), 'ci-cd');
9
+ assert.equal(normalizeTag('node.js'), 'node.js');
10
+ assert.equal(normalizeTag('C++'), 'c++');
11
+ assert.equal(normalizeTag('chat'), '');
12
+ assert.equal(normalizeTag('2026'), '');
13
+ assert.equal(normalizeTag('a very long tag with words'), '');
14
+ assert.equal(normalizeTag('x'.repeat(40)), '');
15
+ assert.equal(normalizeTag(null), '');
16
+ });
17
+
18
+ test('normalizeTag: folds onto the vocabulary (plurals) and follows aliases', () => {
19
+ const vocabulary = new Set(['deploy', 'backups']);
20
+ assert.equal(normalizeTag('deploys', { vocabulary }), 'deploy');
21
+ assert.equal(normalizeTag('backup', { vocabulary }), 'backups');
22
+ assert.equal(normalizeTag('kubernetes', { vocabulary }), 'kubernetes');
23
+ const aliases = new Map([['proxmox-ve', 'proxmox'], ['junk', '']]);
24
+ assert.equal(normalizeTag('Proxmox VE', { aliases }), 'proxmox');
25
+ assert.equal(normalizeTag('junk', { aliases }), '');
26
+ assert.equal(normalizeTag('a', { aliases: new Map([['aa', 'bb'], ['bb', 'aa']]) }), 'a');
27
+ assert.equal(normalizeTag('aa', { aliases: new Map([['aa', 'bb'], ['bb', 'aa']]) }).length, 2); // cycle: bounded
28
+ });
29
+
30
+ test('normalizeTags: distinct, capped', () => {
31
+ assert.deepEqual(normalizeTags(['A', 'a', '#a', 'b', 'chat', 'c', 'd', 'e', 'f']), ['a', 'b', 'c', 'd', 'e']);
32
+ assert.deepEqual(normalizeTags('nope'), []);
33
+ });
34
+
35
+ test('condense: the start and the latest messages within budget, a gap marker between', () => {
36
+ const msgs = Array.from({ length: 50 }, (_, i) => ({ role: i % 2 ? 'assistant' : 'user', kind: 'message', text: `message ${i} ${'x'.repeat(200)}` }));
37
+ const out = condense(msgs, 3000);
38
+ assert.ok(out.length <= 3000);
39
+ assert.match(out, /^User: message 0 /);
40
+ assert.match(out, /AI: message 49 x+$/);
41
+ assert.match(out, /\[… \d+ messages left out …\]/);
42
+ assert.doesNotMatch(out, /message 10 /);
43
+ const comment = condense([{ role: 'user', kind: 'comment', text: 'check this', quote: 'the quoted bit' }]);
44
+ assert.equal(comment, 'User comment (on "the quoted bit"): check this');
45
+ assert.equal(condense([{ role: 'user', kind: 'message', text: 'a'.repeat(5000) }]).length, 'User: '.length + 600);
46
+ });
47
+
48
+ test('buildPrompt: vocabulary first when there is one', () => {
49
+ assert.match(buildPrompt({ title: 'T', conversation: 'User: hi', vocabulary: ['proxmox', 'laser-show'] }), /^Tags already in use .*: proxmox, laser-show\n/);
50
+ assert.match(buildPrompt({ title: '', conversation: 'User: hi' }), /^Chat title: \(none\)/);
51
+ });
52
+
53
+ test('parseReply: fenced or bare JSON, normalized; garbage throws', () => {
54
+ const r = parseReply('Sure!\n```json\n{"summary":"Fixed the\\nbackup job.","tags":["Proxmox","Backups","chat"],"keywords":["vm 104","vm 104","pbs"]}\n```');
55
+ assert.deepEqual(r, { summary: 'Fixed the backup job.', tags: ['proxmox', 'backups'], keywords: ['vm 104', 'pbs'] });
56
+ assert.deepEqual(parseReply('{"summary":"s","tags":["deploys"]}', { vocabulary: new Set(['deploy']) }).tags, ['deploy']);
57
+ assert.throws(() => parseReply('no json here'), /no JSON/);
58
+ assert.throws(() => parseReply('{"summary": '), /no JSON|did not parse/);
59
+ assert.throws(() => parseReply('[1,2]'), /no JSON|not an object/);
60
+ assert.throws(() => parseReply('{"summary":"","tags":["chat"]}'), /neither/);
61
+ });
62
+
63
+ test('effectiveTags: user removals and additions win over the model', () => {
64
+ assert.deepEqual(effectiveTags({ auto: ['a', 'b'], added: ['c', 'a'], removed: ['b'] }), ['a', 'c']);
65
+ });
66
+
67
+ test('estimateCost: known cheap model priced, others unknown', () => {
68
+ const usd = estimateCost([4000, 100_000], 'anthropic/claude-haiku-4-5-20251001');
69
+ assert.ok(usd > 0.005 && usd < 0.02, String(usd));
70
+ assert.equal(estimateCost([4000], 'anthropic/claude-opus-5-5'), null);
71
+ assert.equal(estimateCost([4000], null), null);
72
+ assert.equal(estimateCost([4000], 'claude-cli/claude-haiku-4-5-20251001'), null, 'subscription');
73
+ });
@@ -0,0 +1,143 @@
1
+ // TagStore: chat summaries and tags (global.db). Unlike the search index this is not derived data:
2
+ // every row cost a model call or is a user's edit, so a search Rebuild never touches it.
3
+ //
4
+ // chat_tags per chat: the model's summary, tags and keywords; the user's added/removed tags
5
+ // (applied on top, so a re-tag never undoes an edit); when and from how many messages
6
+ // it was tagged; the last failure for the retry backoff
7
+ // tag_aliases user renames and deletions (target '' = deleted), applied to future model output
8
+ // tag_meta settings and progress: enabled, since, backfill, legacy import
9
+
10
+ import { effectiveTags, normalizeTag } from './model.js';
11
+
12
+ const list = v => { try { const a = JSON.parse(v || '[]'); return Array.isArray(a) ? a : []; } catch { return []; } };
13
+
14
+ function rowOut(r) {
15
+ const auto = list(r.auto_tags), added = list(r.added), removed = list(r.removed);
16
+ return {
17
+ key: r.session_key, summary: r.summary, tags: effectiveTags({ auto, added, removed }), auto, added, removed,
18
+ keywords: list(r.keywords), source: r.source, model: r.model, messages: r.messages, taggedAt: r.tagged_at,
19
+ costUsd: r.cost_usd, error: r.error, errorAt: r.error_at, attempts: r.attempts,
20
+ };
21
+ }
22
+
23
+ export function createTagStore(getGlobalDb, { now = () => Date.now() } = {}) {
24
+ let ready = false;
25
+ function db() {
26
+ const g = getGlobalDb();
27
+ if (!ready) {
28
+ g.exec(`CREATE TABLE IF NOT EXISTS chat_tags (
29
+ session_key TEXT PRIMARY KEY, summary TEXT NOT NULL DEFAULT '', auto_tags TEXT NOT NULL DEFAULT '[]',
30
+ added TEXT NOT NULL DEFAULT '[]', removed TEXT NOT NULL DEFAULT '[]', keywords TEXT NOT NULL DEFAULT '[]',
31
+ source TEXT, model TEXT, messages INTEGER NOT NULL DEFAULT 0, tagged_at INTEGER, cost_usd REAL,
32
+ error TEXT, error_at INTEGER, attempts INTEGER NOT NULL DEFAULT 0, updated_at INTEGER NOT NULL)`);
33
+ g.exec('CREATE TABLE IF NOT EXISTS tag_aliases (tag TEXT PRIMARY KEY, target TEXT NOT NULL)');
34
+ g.exec('CREATE TABLE IF NOT EXISTS tag_meta (k TEXT PRIMARY KEY, v TEXT)');
35
+ ready = true;
36
+ }
37
+ return g;
38
+ }
39
+ const tx = fn => { const g = db(); g.exec('BEGIN IMMEDIATE'); try { const r = fn(g); g.exec('COMMIT'); return r; } catch (e) { g.exec('ROLLBACK'); throw e; } };
40
+ const getRow = (g, key) => g.prepare('SELECT * FROM chat_tags WHERE session_key = ?').get(key);
41
+ const ensure = (g, key) => g.prepare('INSERT OR IGNORE INTO chat_tags (session_key, updated_at) VALUES (?, ?)').run(key, now());
42
+
43
+ const store = {
44
+ meta(k) { return db().prepare('SELECT v FROM tag_meta WHERE k = ?').get(k)?.v ?? null; },
45
+ setMeta(k, v) {
46
+ if (v == null) db().prepare('DELETE FROM tag_meta WHERE k = ?').run(k);
47
+ else db().prepare('INSERT INTO tag_meta (k, v) VALUES (?, ?) ON CONFLICT(k) DO UPDATE SET v = excluded.v').run(k, String(v));
48
+ },
49
+
50
+ get(key) { const r = getRow(db(), key); return r ? rowOut(r) : null; },
51
+ all() { return db().prepare('SELECT * FROM chat_tags').all().map(rowOut); },
52
+ keys() { return db().prepare('SELECT session_key FROM chat_tags').all().map(r => r.session_key); },
53
+ delete(key) { return db().prepare('DELETE FROM chat_tags WHERE session_key = ?').run(key).changes > 0; },
54
+
55
+ /** Map tag → target ('' = deleted). */
56
+ aliases() { return new Map(db().prepare('SELECT tag, target FROM tag_aliases').all().map(r => [r.tag, r.target])); },
57
+
58
+ /** Tags in use with how many chats carry each, most used first. */
59
+ vocabulary() {
60
+ const counts = new Map();
61
+ for (const r of store.all()) for (const t of r.tags) counts.set(t, (counts.get(t) || 0) + 1);
62
+ return [...counts].map(([tag, count]) => ({ tag, count })).sort((a, b) => b.count - a.count || a.tag.localeCompare(b.tag));
63
+ },
64
+
65
+ /** The model's result for a chat; the user's added/removed tags stay. */
66
+ saveAuto(key, { summary, tags, keywords, model = null, messages = 0, costUsd = null, source = 'auto' }) {
67
+ tx(g => {
68
+ ensure(g, key);
69
+ g.prepare(`UPDATE chat_tags SET summary = ?, auto_tags = ?, keywords = ?, source = ?, model = ?, messages = ?, tagged_at = ?,
70
+ cost_usd = ?, error = NULL, error_at = NULL, attempts = 0, updated_at = ? WHERE session_key = ?`)
71
+ .run(summary || '', JSON.stringify(tags || []), JSON.stringify(keywords || []), source, model, messages, now(), costUsd, now(), key);
72
+ });
73
+ return store.get(key);
74
+ },
75
+
76
+ markFailed(key, message) {
77
+ tx(g => {
78
+ ensure(g, key);
79
+ g.prepare('UPDATE chat_tags SET error = ?, error_at = ?, attempts = attempts + 1, updated_at = ? WHERE session_key = ?').run(String(message).slice(0, 500), now(), now(), key);
80
+ });
81
+ },
82
+
83
+ /** User edit on one chat: add and/or remove tags (normalized; aliases apply). Returns the chat's row. */
84
+ edit(key, { add = [], remove = [] }) {
85
+ const aliases = store.aliases();
86
+ return tx(g => {
87
+ ensure(g, key);
88
+ const r = rowOut(getRow(g, key));
89
+ const added = new Set(r.added), removed = new Set(r.removed);
90
+ for (const raw of remove) {
91
+ const t = normalizeTag(raw) || String(raw);
92
+ added.delete(t);
93
+ if (r.auto.includes(t)) removed.add(t);
94
+ }
95
+ for (const raw of add) {
96
+ const t = normalizeTag(raw, { aliases });
97
+ if (!t) continue;
98
+ removed.delete(t);
99
+ if (!r.auto.includes(t)) added.add(t);
100
+ }
101
+ g.prepare('UPDATE chat_tags SET added = ?, removed = ?, updated_at = ? WHERE session_key = ?').run(JSON.stringify([...added]), JSON.stringify([...removed]), now(), key);
102
+ return rowOut(getRow(g, key));
103
+ });
104
+ },
105
+
106
+ /**
107
+ * Rename `from` to `to` everywhere (a merge when `to` is already in use), or delete it (`to` = '').
108
+ * Future model output follows (alias). Returns the keys of the chats that changed.
109
+ */
110
+ renameTag(from, to) {
111
+ return tx(g => {
112
+ g.prepare('INSERT INTO tag_aliases (tag, target) VALUES (?, ?) ON CONFLICT(tag) DO UPDATE SET target = excluded.target').run(from, to);
113
+ if (to) {
114
+ g.prepare('DELETE FROM tag_aliases WHERE tag = ?').run(to); // `to` is a live tag again
115
+ g.prepare('UPDATE tag_aliases SET target = ? WHERE target = ?').run(to, from);
116
+ }
117
+ const changed = [];
118
+ const swap = arr => { const out = []; for (const t of arr) { const v = t === from ? to : t; if (v && !out.includes(v)) out.push(v); } return out; };
119
+ for (const r of g.prepare('SELECT * FROM chat_tags').all().map(rowOut)) {
120
+ if (![r.auto, r.added, r.removed].some(a => a.includes(from))) continue;
121
+ const auto = swap(r.auto), removed = swap(r.removed);
122
+ const added = swap(r.added).filter(t => !auto.includes(t));
123
+ g.prepare('UPDATE chat_tags SET auto_tags = ?, added = ?, removed = ?, updated_at = ? WHERE session_key = ?')
124
+ .run(JSON.stringify(auto), JSON.stringify(added), JSON.stringify(removed), now(), r.key);
125
+ changed.push(r.key);
126
+ }
127
+ return changed;
128
+ });
129
+ },
130
+
131
+ /** Legacy (pre-gateway ClawChats) summaries, for chats without a row yet. Returns how many were added. */
132
+ importLegacy(rows) {
133
+ return tx(g => {
134
+ const put = g.prepare(`INSERT OR IGNORE INTO chat_tags (session_key, summary, keywords, source, messages, tagged_at, updated_at)
135
+ VALUES (?, ?, ?, 'legacy', ?, ?, ?)`);
136
+ let n = 0;
137
+ for (const r of rows) n += put.run(r.key, r.summary || '', JSON.stringify(r.keywords || []), r.messages || 0, r.taggedAt || now(), now()).changes;
138
+ return n;
139
+ });
140
+ },
141
+ };
142
+ return store;
143
+ }