@kadaliao/dsh-bots 0.0.0-stage → 0.3.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/README.md +142 -2
- package/README.zh-CN.md +123 -0
- package/client.js +5506 -0
- package/cordis.patch.yml +7 -0
- package/icon.svg +1 -0
- package/index.js +1286 -0
- package/lib/abilities.mjs +87 -0
- package/lib/apps.mjs +474 -0
- package/lib/mcp-import.mjs +137 -0
- package/lib/prompt.mjs +174 -0
- package/lib/skills.mjs +146 -0
- package/lib/store.mjs +376 -0
- package/lib/teams.mjs +300 -0
- package/lib/unfurl.mjs +394 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +49 -4
package/lib/store.mjs
ADDED
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The bot roster and the message log, as JSON files under the DSH home.
|
|
3
|
+
*
|
|
4
|
+
* `bots.json` holds every bot and the session map; `messages.jsonl` is an
|
|
5
|
+
* append-only timeline of what was said to, by and between bots. Both are
|
|
6
|
+
* re-read when another process changed them.
|
|
7
|
+
*
|
|
8
|
+
* @module @kadaliao/dsh-bots/lib/store
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { appendFileSync, mkdirSync, readFileSync, statSync, writeFileSync, renameSync } from 'node:fs';
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
|
|
14
|
+
/** Messages kept in the log; past this the file is compacted to the newest. */
|
|
15
|
+
export const KEEP_MESSAGES = 5000;
|
|
16
|
+
/** Longest text one logged message keeps. */
|
|
17
|
+
export const MAX_TEXT = 8000;
|
|
18
|
+
|
|
19
|
+
export const HANDLE_RE = /^[a-z0-9](?:[a-z0-9-]{0,30}[a-z0-9])?$/;
|
|
20
|
+
export const SERVER_RE = /^[A-Za-z0-9_-]{1,32}$/;
|
|
21
|
+
export const SKILL_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
22
|
+
|
|
23
|
+
export const PALETTE = ['#7c5cff', '#2f80ed', '#00a3a3', '#27ae60', '#f2994a', '#eb5757', '#d6409f', '#9b51e0', '#56ccf2', '#828282'];
|
|
24
|
+
|
|
25
|
+
/** A short, sortable, unique id. */
|
|
26
|
+
export function newId(prefix, now = Date.now()) {
|
|
27
|
+
return `${prefix}-${now.toString(36)}${Math.random().toString(36).slice(2, 7)}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** A handle from a display name: lowercase ascii, dashes, or `bot` + digits. */
|
|
31
|
+
export function slugify(text, fallback = 'bot') {
|
|
32
|
+
const s = String(text ?? '')
|
|
33
|
+
.normalize('NFKD')
|
|
34
|
+
.replace(/[\u0300-\u036f]/g, '')
|
|
35
|
+
.toLowerCase()
|
|
36
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
37
|
+
.replace(/^-+|-+$/g, '')
|
|
38
|
+
.slice(0, 32)
|
|
39
|
+
.replace(/-+$/g, '');
|
|
40
|
+
return s || fallback;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const str = (v, max = 100_000) => (typeof v === 'string' ? v.slice(0, max) : '');
|
|
44
|
+
const bool = (v, d) => (typeof v === 'boolean' ? v : d);
|
|
45
|
+
|
|
46
|
+
const PINYIN_BOUNDS = '阿八嚓哒妸发旮哈丌咔垃妈拏噢妑七呥仨他屲夕丫帀';
|
|
47
|
+
const PINYIN_LETTERS = 'ABCDEFGHJKLMNOPQRSTWXYZ';
|
|
48
|
+
let pinyinCollator = null;
|
|
49
|
+
/** The pinyin initial of one Han character, by collation against the first character of each letter. */
|
|
50
|
+
export function pinyinInitial(ch) {
|
|
51
|
+
try {
|
|
52
|
+
pinyinCollator ??= new Intl.Collator('zh-Hans-u-co-pinyin');
|
|
53
|
+
} catch {
|
|
54
|
+
return '';
|
|
55
|
+
}
|
|
56
|
+
let letter = '';
|
|
57
|
+
for (let i = 0; i < PINYIN_BOUNDS.length; i += 1) {
|
|
58
|
+
if (pinyinCollator.compare(ch, PINYIN_BOUNDS[i]) >= 0) letter = PINYIN_LETTERS[i];
|
|
59
|
+
else break;
|
|
60
|
+
}
|
|
61
|
+
return letter;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Up to two letters for an avatar: first letters of the first two words
|
|
66
|
+
* ("Data Scout" → DS, "Atlas" → AT), or pinyin initials of a Chinese name
|
|
67
|
+
* ("研究员" → YJ). Other scripts keep their first character.
|
|
68
|
+
*/
|
|
69
|
+
export function initialsOf(name) {
|
|
70
|
+
const text = String(name ?? '').trim();
|
|
71
|
+
if (text === '') return '?';
|
|
72
|
+
const chars = [...text];
|
|
73
|
+
if (/[\u4e00-\u9fff]/.test(chars[0])) {
|
|
74
|
+
const han = chars.filter((c) => /[\u4e00-\u9fff]/.test(c)).slice(0, 2);
|
|
75
|
+
const letters = han.map((c) => pinyinInitial(c)).join('');
|
|
76
|
+
return letters.length === han.length && letters !== '' ? letters : han.join('');
|
|
77
|
+
}
|
|
78
|
+
const words = text.split(/[\s_\-·.]+/).filter(Boolean);
|
|
79
|
+
const first = (c) => (/[\u4e00-\u9fff]/.test(c) ? pinyinInitial(c) || c : c.toUpperCase());
|
|
80
|
+
if (words.length >= 2) return first([...words[0]][0]) + first([...words[1]][0]);
|
|
81
|
+
const w = [...words[0]];
|
|
82
|
+
if (/^[A-Za-z]/.test(w[0])) {
|
|
83
|
+
// camelCase gives its capitals ("DataBot" → DB); else the first two letters.
|
|
84
|
+
const caps = w.filter((c, i) => i > 0 && /[A-Z]/.test(c));
|
|
85
|
+
return (w[0] + (caps[0] ?? w[1] ?? '')).toUpperCase();
|
|
86
|
+
}
|
|
87
|
+
return w[0];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* An avatar: an uploaded or generated picture, or initials on a colour. Emoji
|
|
92
|
+
* avatars from earlier versions become initials.
|
|
93
|
+
*/
|
|
94
|
+
function normalizeAvatar(a, seed = 0) {
|
|
95
|
+
const color = typeof a?.color === 'string' && /^#[0-9a-f]{3,8}$|^(rgb|hsl)a?\(/i.test(a.color.trim()) ? a.color.trim().slice(0, 40) : PALETTE[seed % PALETTE.length];
|
|
96
|
+
if (a?.kind === 'image' && typeof a.url === 'string' && a.url.startsWith('/__dsh_bots/avatar/')) return { kind: 'image', url: a.url, color, ...(a.source === 'ai' ? { source: 'ai' } : {}) };
|
|
97
|
+
const text = typeof a?.text === 'string' && a.text.trim() !== '' ? [...a.text.trim()].slice(0, 2).join('').toUpperCase() : '';
|
|
98
|
+
return { kind: 'initials', ...(text ? { text } : {}), color };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* One skill of a bot:
|
|
103
|
+
* - `own`: written for this bot (content kept here);
|
|
104
|
+
* - `dsh`: an installed Harness skill, by name (content read live from DSH);
|
|
105
|
+
* - `import`: copied from GitHub or a URL; `dir` holds its files on disk.
|
|
106
|
+
*/
|
|
107
|
+
function normalizeSkill(s) {
|
|
108
|
+
const name = str(s?.name, 80).trim();
|
|
109
|
+
if (name === '') return null;
|
|
110
|
+
const source = ['own', 'dsh', 'import'].includes(s?.source) ? s.source : 'own';
|
|
111
|
+
return {
|
|
112
|
+
id: str(s?.id, 40) || newId('skill'),
|
|
113
|
+
name,
|
|
114
|
+
source,
|
|
115
|
+
description: str(s?.description, 1000).trim(),
|
|
116
|
+
content: source === 'dsh' ? '' : str(s?.content, 200_000),
|
|
117
|
+
enabled: bool(s?.enabled, true),
|
|
118
|
+
...(source === 'import' && typeof s?.dir === 'string' ? { dir: s.dir } : {}),
|
|
119
|
+
...(typeof s?.origin === 'string' && s.origin ? { origin: str(s.origin, 500) } : {}),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const kv = (o) => {
|
|
124
|
+
const out = {};
|
|
125
|
+
if (o && typeof o === 'object' && !Array.isArray(o)) {
|
|
126
|
+
for (const [k, v] of Object.entries(o)) if (typeof k === 'string' && k.trim() !== '' && v !== undefined && v !== null) out[k.trim()] = String(v);
|
|
127
|
+
}
|
|
128
|
+
return out;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
function normalizeApp(a) {
|
|
132
|
+
const template = typeof a?.template === 'string' && /^[a-z0-9-]{1,40}$/.test(a.template) ? a.template : 'mcp-stdio';
|
|
133
|
+
const serverName = str(a?.serverName, 32).trim();
|
|
134
|
+
if (!SERVER_RE.test(serverName)) return null;
|
|
135
|
+
const c = a?.config && typeof a.config === 'object' ? a.config : {};
|
|
136
|
+
const config = {};
|
|
137
|
+
if (typeof c.url === 'string') config.url = str(c.url, 2000).trim();
|
|
138
|
+
if (c.headers) config.headers = kv(c.headers);
|
|
139
|
+
if (typeof c.command === 'string') config.command = str(c.command, 2000).trim();
|
|
140
|
+
if (Array.isArray(c.args)) config.args = c.args.map((x) => String(x)).slice(0, 64);
|
|
141
|
+
if (c.env) config.env = kv(c.env);
|
|
142
|
+
return {
|
|
143
|
+
id: str(a?.id, 40) || newId('app'),
|
|
144
|
+
template,
|
|
145
|
+
name: str(a?.name, 80).trim() || serverName,
|
|
146
|
+
serverName,
|
|
147
|
+
enabled: bool(a?.enabled, true),
|
|
148
|
+
values: kv(a?.values),
|
|
149
|
+
config,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* One bot, normalized: every field present, unknown ones dropped. `prev` is the
|
|
155
|
+
* stored bot an update applies to.
|
|
156
|
+
*/
|
|
157
|
+
export function normalizeBot(input, prev, seed = 0) {
|
|
158
|
+
const b = { ...(prev ?? {}), ...(input ?? {}) };
|
|
159
|
+
const now = Date.now();
|
|
160
|
+
const name = str(b.name, 60).trim() || prev?.name || 'New bot';
|
|
161
|
+
const skills = (Array.isArray(b.skills) ? b.skills : []).map(normalizeSkill).filter(Boolean).slice(0, 50);
|
|
162
|
+
const apps = (Array.isArray(b.apps) ? b.apps : []).map(normalizeApp).filter(Boolean).slice(0, 20);
|
|
163
|
+
const deny = Array.isArray(b.tools?.deny) ? [...new Set(b.tools.deny.map(String).filter(Boolean))].slice(0, 200) : [];
|
|
164
|
+
const model = b.model && typeof b.model === 'object' && str(b.model.provider) && str(b.model.model) ? { provider: str(b.model.provider, 200), model: str(b.model.model, 200) } : null;
|
|
165
|
+
return {
|
|
166
|
+
id: prev?.id ?? (str(b.id, 40) || newId('bot', now)),
|
|
167
|
+
name,
|
|
168
|
+
handle: str(b.handle, 32).trim().toLowerCase(),
|
|
169
|
+
role: b.role === 'primary' ? 'primary' : 'bot',
|
|
170
|
+
avatar: normalizeAvatar(b.avatar, seed),
|
|
171
|
+
bio: str(b.bio, 300).trim(),
|
|
172
|
+
persona: str(b.persona, 20_000),
|
|
173
|
+
skills,
|
|
174
|
+
apps,
|
|
175
|
+
tools: { deny },
|
|
176
|
+
model,
|
|
177
|
+
sessionId: typeof b.sessionId === 'string' && b.sessionId !== '' ? b.sessionId : null,
|
|
178
|
+
createdAt: prev?.createdAt ?? now,
|
|
179
|
+
updatedAt: now,
|
|
180
|
+
createdBy: prev?.createdBy ?? (str(b.createdBy, 60) || 'user'),
|
|
181
|
+
...(typeof b.team === 'string' && /^[a-z0-9-]{1,40}$/.test(b.team) ? { team: b.team } : {}),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* The store under `dir`.
|
|
187
|
+
* @param {string} dir
|
|
188
|
+
*/
|
|
189
|
+
export function createStore(dir) {
|
|
190
|
+
const file = join(dir, 'bots.json');
|
|
191
|
+
const log = join(dir, 'messages.jsonl');
|
|
192
|
+
let state = { version: 0, bots: [] };
|
|
193
|
+
let seen = -1;
|
|
194
|
+
let messages = null;
|
|
195
|
+
let logSeen = -1;
|
|
196
|
+
const listeners = new Set();
|
|
197
|
+
|
|
198
|
+
const mtime = (p) => {
|
|
199
|
+
try {
|
|
200
|
+
return statSync(p).mtimeMs;
|
|
201
|
+
} catch {
|
|
202
|
+
return -1;
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
function reload() {
|
|
206
|
+
const now = mtime(file);
|
|
207
|
+
if (now === seen) return;
|
|
208
|
+
seen = now;
|
|
209
|
+
try {
|
|
210
|
+
const parsed = JSON.parse(readFileSync(file, 'utf8'));
|
|
211
|
+
if (parsed && Array.isArray(parsed.bots)) state = { version: Number(parsed.version) || 0, bots: parsed.bots.map((b, i) => normalizeBot(b, b, i)).map((b, i) => ({ ...b, updatedAt: parsed.bots[i].updatedAt ?? b.updatedAt })) };
|
|
212
|
+
} catch {
|
|
213
|
+
// First run.
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
function write() {
|
|
217
|
+
mkdirSync(dir, { recursive: true });
|
|
218
|
+
state.version += 1;
|
|
219
|
+
const tmp = `${file}.tmp`;
|
|
220
|
+
writeFileSync(tmp, JSON.stringify(state, null, 2), 'utf8');
|
|
221
|
+
renameSync(tmp, file);
|
|
222
|
+
seen = mtime(file);
|
|
223
|
+
for (const fn of listeners) {
|
|
224
|
+
try {
|
|
225
|
+
fn();
|
|
226
|
+
} catch {
|
|
227
|
+
// A listener must not break a write.
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
function readLog() {
|
|
232
|
+
const now = mtime(log);
|
|
233
|
+
if (messages !== null && now === logSeen) return messages;
|
|
234
|
+
logSeen = now;
|
|
235
|
+
messages = [];
|
|
236
|
+
try {
|
|
237
|
+
for (const line of readFileSync(log, 'utf8').split('\n')) {
|
|
238
|
+
if (!line) continue;
|
|
239
|
+
try {
|
|
240
|
+
const m = JSON.parse(line);
|
|
241
|
+
if (m && typeof m.id === 'string') messages.push(m);
|
|
242
|
+
} catch {
|
|
243
|
+
// Skip a torn line.
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
} catch {
|
|
247
|
+
// No log yet.
|
|
248
|
+
}
|
|
249
|
+
return messages;
|
|
250
|
+
}
|
|
251
|
+
reload();
|
|
252
|
+
|
|
253
|
+
return {
|
|
254
|
+
dir,
|
|
255
|
+
onChange(fn) {
|
|
256
|
+
listeners.add(fn);
|
|
257
|
+
return () => listeners.delete(fn);
|
|
258
|
+
},
|
|
259
|
+
version() {
|
|
260
|
+
reload();
|
|
261
|
+
return state.version;
|
|
262
|
+
},
|
|
263
|
+
list() {
|
|
264
|
+
reload();
|
|
265
|
+
return state.bots.slice();
|
|
266
|
+
},
|
|
267
|
+
get(id) {
|
|
268
|
+
reload();
|
|
269
|
+
return state.bots.find((b) => b.id === id);
|
|
270
|
+
},
|
|
271
|
+
/** By id, `@handle`, handle or (case-insensitive) display name. */
|
|
272
|
+
find(ref) {
|
|
273
|
+
reload();
|
|
274
|
+
const r = String(ref ?? '').trim().replace(/^@/, '');
|
|
275
|
+
if (r === '') return undefined;
|
|
276
|
+
const lower = r.toLowerCase();
|
|
277
|
+
return state.bots.find((b) => b.id === r) ?? state.bots.find((b) => b.handle === lower) ?? state.bots.find((b) => b.name.toLowerCase() === lower);
|
|
278
|
+
},
|
|
279
|
+
bySession(sessionId) {
|
|
280
|
+
reload();
|
|
281
|
+
return sessionId ? state.bots.find((b) => b.sessionId === sessionId) : undefined;
|
|
282
|
+
},
|
|
283
|
+
primary() {
|
|
284
|
+
reload();
|
|
285
|
+
return state.bots.find((b) => b.role === 'primary');
|
|
286
|
+
},
|
|
287
|
+
/** Create or update; returns the stored bot. Throws a readable error on a bad value. */
|
|
288
|
+
save(input, { createdBy } = {}) {
|
|
289
|
+
reload();
|
|
290
|
+
const prev = input?.id ? state.bots.find((b) => b.id === input.id) : undefined;
|
|
291
|
+
if (input?.id && !prev) throw new Error(`no bot with id ${input.id}`);
|
|
292
|
+
const bot = normalizeBot({ ...input, ...(prev ? {} : { createdBy: createdBy ?? input?.createdBy }) }, prev, state.bots.length);
|
|
293
|
+
if (bot.handle === '') bot.handle = slugify(bot.name);
|
|
294
|
+
if (!HANDLE_RE.test(bot.handle)) throw new Error(`handle "${bot.handle}" must be lowercase letters, digits and dashes`);
|
|
295
|
+
if (!prev && state.bots.some((b) => b.handle === bot.handle)) {
|
|
296
|
+
// A fresh bot whose name collides gets a numbered handle when the caller left it out.
|
|
297
|
+
if (input?.handle) throw new Error(`handle @${bot.handle} is taken`);
|
|
298
|
+
let n = 2;
|
|
299
|
+
while (state.bots.some((b) => b.handle === `${bot.handle}-${n}`)) n += 1;
|
|
300
|
+
bot.handle = `${bot.handle}-${n}`;
|
|
301
|
+
} else if (state.bots.some((b) => b.handle === bot.handle && b.id !== bot.id)) {
|
|
302
|
+
throw new Error(`handle @${bot.handle} is taken`);
|
|
303
|
+
}
|
|
304
|
+
const names = new Set();
|
|
305
|
+
for (const app of bot.apps) {
|
|
306
|
+
if (names.has(app.serverName)) throw new Error(`two apps share the server name "${app.serverName}"`);
|
|
307
|
+
names.add(app.serverName);
|
|
308
|
+
}
|
|
309
|
+
if (bot.role === 'primary') for (const b of state.bots) if (b.id !== bot.id && b.role === 'primary') b.role = 'bot';
|
|
310
|
+
if (prev) state.bots[state.bots.indexOf(prev)] = bot;
|
|
311
|
+
else state.bots.push(bot);
|
|
312
|
+
if (!state.bots.some((b) => b.role === 'primary')) state.bots[0].role = 'primary';
|
|
313
|
+
write();
|
|
314
|
+
return bot;
|
|
315
|
+
},
|
|
316
|
+
/** Set fields without normalizing the rest (session ids, avatar urls). */
|
|
317
|
+
patch(id, fields) {
|
|
318
|
+
reload();
|
|
319
|
+
const bot = state.bots.find((b) => b.id === id);
|
|
320
|
+
if (!bot) return undefined;
|
|
321
|
+
Object.assign(bot, fields, { updatedAt: Date.now() });
|
|
322
|
+
write();
|
|
323
|
+
return bot;
|
|
324
|
+
},
|
|
325
|
+
setPrimary(id) {
|
|
326
|
+
reload();
|
|
327
|
+
if (!state.bots.some((b) => b.id === id)) return false;
|
|
328
|
+
for (const b of state.bots) b.role = b.id === id ? 'primary' : 'bot';
|
|
329
|
+
write();
|
|
330
|
+
return true;
|
|
331
|
+
},
|
|
332
|
+
remove(id) {
|
|
333
|
+
reload();
|
|
334
|
+
const at = state.bots.findIndex((b) => b.id === id);
|
|
335
|
+
if (at === -1) return undefined;
|
|
336
|
+
const [gone] = state.bots.splice(at, 1);
|
|
337
|
+
if (gone.role === 'primary' && state.bots.length > 0) state.bots[0].role = 'primary';
|
|
338
|
+
write();
|
|
339
|
+
return gone;
|
|
340
|
+
},
|
|
341
|
+
/** Append one message to the timeline. */
|
|
342
|
+
log(message) {
|
|
343
|
+
const m = {
|
|
344
|
+
id: newId('msg'),
|
|
345
|
+
at: Date.now(),
|
|
346
|
+
kind: 'dm',
|
|
347
|
+
...message,
|
|
348
|
+
text: String(message.text ?? '').slice(0, MAX_TEXT),
|
|
349
|
+
};
|
|
350
|
+
const all = readLog();
|
|
351
|
+
all.push(m);
|
|
352
|
+
try {
|
|
353
|
+
mkdirSync(dir, { recursive: true });
|
|
354
|
+
if (all.length > KEEP_MESSAGES * 1.2) {
|
|
355
|
+
messages = all.slice(-KEEP_MESSAGES);
|
|
356
|
+
writeFileSync(log, messages.map((x) => JSON.stringify(x)).join('\n') + '\n', 'utf8');
|
|
357
|
+
} else {
|
|
358
|
+
appendFileSync(log, JSON.stringify(m) + '\n', 'utf8');
|
|
359
|
+
}
|
|
360
|
+
logSeen = mtime(log);
|
|
361
|
+
} catch {
|
|
362
|
+
// The timeline is a convenience.
|
|
363
|
+
}
|
|
364
|
+
return m;
|
|
365
|
+
},
|
|
366
|
+
/** Oldest first; `bot` limits to messages from or to that bot. */
|
|
367
|
+
messages({ bot = '', limit = 200 } = {}) {
|
|
368
|
+
const all = readLog();
|
|
369
|
+
const rows = bot ? all.filter((m) => m.from === bot || m.to === bot) : all;
|
|
370
|
+
return rows.slice(-Math.max(1, Math.min(2000, limit)));
|
|
371
|
+
},
|
|
372
|
+
messageCount() {
|
|
373
|
+
return readLog().length;
|
|
374
|
+
},
|
|
375
|
+
};
|
|
376
|
+
}
|