dsh-bagu 0.9.0 → 0.10.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/dsh-plugin.json CHANGED
@@ -3,9 +3,9 @@
3
3
  "manifestVersion": "0.15",
4
4
  "id": "dsh-bagu",
5
5
  "name": "dsh-bagu",
6
- "version": "0.9.0",
6
+ "version": "0.10.0",
7
7
  "license": "MIT",
8
- "description": "AI 对外说话前的一道门:像八股就打回,接梗走会话模型。",
8
+ "description": "AI 对外说话两道门:八股打回,梗接住。",
9
9
  "facets": {
10
10
  "host": {
11
11
  "entry": "lib/index.js",
@@ -29,7 +29,7 @@
29
29
  "x-display": [
30
30
  {
31
31
  "title": "dsh-bagu",
32
- "description": "所有对外文本发出前过一遍,像八股就打回。接梗默认走会话模型,不用填 key。"
32
+ "description": "发出去的话像八股就打回;对方抛梗,模型看到消息前就已经把族查好了。"
33
33
  }
34
34
  ]
35
35
  }
package/lib/bridge.js CHANGED
@@ -42,8 +42,9 @@ export function bridge(text, memes, opts = {}) {
42
42
  }
43
43
 
44
44
  const hitWords = [];
45
+ const srcLow = src.toLowerCase();
45
46
  for (const k of idx.keys()) {
46
- if (src.toLowerCase().includes(k)) hitWords.push(k);
47
+ if (srcLow.includes(k)) hitWords.push(k);
47
48
  }
48
49
 
49
50
  // 2. 先算「真的提到了」—— 按 slug / title / alias 直接命中,不靠 keyword
@@ -71,8 +72,10 @@ export function bridge(text, memes, opts = {}) {
71
72
  for (const t of (m && m.tags) || []) tags.add(t);
72
73
  }
73
74
 
75
+ // 再兜一道:by 里不许出现 same 已提到的
76
+ const byClean = [...by].filter((x) => !same.has(x));
74
77
  return {
75
- by: [...by].slice(0, max),
78
+ by: byClean.slice(0, max),
76
79
  same: [...same].slice(0, max),
77
80
  tags: [...tags].slice(0, max),
78
81
  };
package/lib/index.js CHANGED
@@ -30,6 +30,16 @@ const syncers = new Set();
30
30
  /** 每次会话都注入,多一个字都是白烧。 */
31
31
  const PROMPT = `要发出去的话,先过 bagu_guard。被打回就改,别硬发。`;
32
32
 
33
+ /** 梗库开着时的追加。告诉 agent 有个查梗工具,遇到不认识的梗先查再答。 */
34
+ const PROMPT_MEME = `
35
+ 遇到网络梗(谐音、缩写、人名外号、视频台词)拿不准意思,先调 bagu_meme 查,别猜。
36
+ 要接梗就传 text(对方原话),它会给出同族能接的;要查单个词就传 q。
37
+ `;
38
+
39
+ /** AI 接梗开着时再加一句。 */
40
+ const PROMPT_AI = `
41
+ 接梗要给出具体那句话,调 bagu_meme 传 text,会自动调模型拼一句。`;
42
+
33
43
  /** 60 字。 */
34
44
  /** 一个工具,三种模式。描述只占一份。 */
35
45
  const TOOL_GUARD = `发出去之前过一遍。默认只回 OK 或一行改法。mode=report 给详细报告,mode=ask 给问句。`;
@@ -96,10 +106,38 @@ export function apply(root, config = {}) {
96
106
  const sync = () => {
97
107
  if (dispose) { dispose(); dispose = null; }
98
108
  if (!live.enabled || !live.prompt) return;
109
+ // ── 预查注入:用户消息一进来就先查梗,塞进上下文 ──────────
110
+ // 这是「会玩梗」和「能查梗」的分界:前者是模型还没开始想就已经知道,
111
+ // 后者要模型主动调工具(它经常不调)。
112
+ if (live.memes) {
113
+ try {
114
+ ctx.effect(() => ctx.systemPrompt.context({
115
+ name: 'bagu:prefetch',
116
+ order: 950,
117
+ text: (context) => {
118
+ try {
119
+ const text = lastUserText(context);
120
+ if (!text) return '';
121
+ const list = loadMemes();
122
+ const hint = bridgeHint(text, list);
123
+ if (!hint) return '';
124
+ return '[梗]\n' + hint + '\n照这个接,别解释梗是什么意思。';
125
+ } catch {
126
+ return '';
127
+ }
128
+ },
129
+ }), 'dsh-bagu: 预查注入');
130
+ } catch {
131
+ /* 宿主没有这个接口就算了 */
132
+ }
133
+ }
134
+
99
135
  dispose = ctx.systemPrompt.section({
100
136
  name: 'bagu:voice',
101
137
  order: live.order,
102
- text: PROMPT,
138
+ text: PROMPT +
139
+ (live.memes ? PROMPT_MEME : '') +
140
+ (live.aiBridge && live.memes ? PROMPT_AI : ''),
103
141
  });
104
142
  };
105
143
  sync();
@@ -212,9 +250,26 @@ export function apply(root, config = {}) {
212
250
  const g = guard(src, { strict: args?.strict ?? live.strict });
213
251
  // 梗库是选装的,默认关。开着就在结果后面附一句,不改判定。
214
252
  if (live.memes) {
215
- // 接梗:不是「这段用了哪些梗」,是「顺着这些能接什么」
253
+ // 发现梗就直接把接梗答案带上 —— agent 不用再调一次 bagu_meme。
216
254
  const hint = bridgeHint(src, loadMemes());
217
- if (hint) return { ok: true, text: `${g.line}|${hint}` };
255
+ if (hint) {
256
+ // AI 接梗开着就拼具体那句,失败退回族提示
257
+ const r = bridge(src, loadMemes());
258
+ if (live.aiBridge && (r.same.length || r.by.length)) {
259
+ const llm = ctx.llm || (typeof ctx.get === 'function' ? ctx.get('llm') : undefined);
260
+ if (llm) {
261
+ const viaSession = await sessionBridge(llm, getRoute(), src, r);
262
+ if (viaSession) return { ok: true, text: `${g.line}|接:${viaSession}` };
263
+ }
264
+ if (live.aiUseOwn && live.aiBaseUrl && live.aiModel && live.aiApiKey) {
265
+ const viaApi = await aiBridge(src, r, {
266
+ baseUrl: live.aiBaseUrl, model: live.aiModel, apiKey: live.aiApiKey,
267
+ });
268
+ if (viaApi) return { ok: true, text: `${g.line}|接:${viaApi}` };
269
+ }
270
+ }
271
+ return { ok: true, text: `${g.line}|${hint}` };
272
+ }
218
273
  }
219
274
  return { ok: true, text: g.line };
220
275
  },
@@ -278,3 +333,41 @@ export function apply(root, config = {}) {
278
333
  } catch { /* headless 没有 webServer 正常 */ }
279
334
  });
280
335
  }
336
+
337
+ /** 从 context 里取最后一条用户消息。抄 thawline/dsh-memewiki 的做法。 */
338
+ function lastUserText(context) {
339
+ try {
340
+ const session = context && context.agent && context.agent.session;
341
+ if (!session || typeof session.snapshotEvents !== 'function') return '';
342
+ const events = session.snapshotEvents() || [];
343
+ for (let i = events.length - 1; i >= 0; i--) {
344
+ const e = events[i];
345
+ const kind = e && (e.type || e.kind);
346
+ if (kind !== 'user/message') continue;
347
+ const payload = e.payload !== undefined ? e.payload : e.data;
348
+ const src = payload && payload.source && payload.source.kind ? String(payload.source.kind) : '';
349
+ if (src && src !== 'user') continue;
350
+ const txt = payloadText(payload);
351
+ if (txt) return txt;
352
+ }
353
+ } catch { /* 取不到就算了 */ }
354
+ return '';
355
+ }
356
+
357
+ /** 从消息 payload 里抠文本。 */
358
+ function payloadText(payload) {
359
+ if (!payload) return '';
360
+ if (typeof payload === 'string') return payload;
361
+ const parts = payload.parts || payload.content || payload.message;
362
+ if (typeof parts === 'string') return parts;
363
+ if (Array.isArray(parts)) {
364
+ return parts
365
+ .map((p) => {
366
+ if (typeof p === 'string') return p;
367
+ if (p && typeof p.text === 'string') return p.text;
368
+ return '';
369
+ })
370
+ .join('\n');
371
+ }
372
+ return '';
373
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-bagu",
3
- "version": "0.9.0",
4
- "description": "给 AI 对外说话的一道门:像八股就打回。接梗走会话自己的模型,不用填 key。",
3
+ "version": "0.10.0",
4
+ "description": "两道门:发出去的话像八股就打回,对方抛梗就接住。接梗在模型看到消息前就查好。",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "exports": {
@@ -0,0 +1,141 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { readFileSync } from 'node:fs';
4
+
5
+ // 预查注入是「会玩梗」的关键:用户消息一进来就先查好塞进上下文,
6
+ // 不等模型主动调工具。这里测的是「注册了、能算出来、没梗不算」。
7
+ //
8
+ // lastUserText / payloadText 是 index.js 里的内部函数,不导出。
9
+ // 所以这里用搭假宿主 + 读注册出来的 context 函数来测。
10
+
11
+ const SRC = readFileSync('lib/index.js', 'utf8');
12
+
13
+ test('lastUserText 和 payloadText 都有定义', () => {
14
+ // 曾经踩过的坑:函数被引用但没定义,抛 ReferenceError 被 try/catch 吞成空,
15
+ // 外面看不出来,预查永远不命中。
16
+ assert.match(SRC, /function lastUserText\(/, 'lastUserText 没定义');
17
+ assert.match(SRC, /function payloadText\(/, 'payloadText 没定义');
18
+ const refs = (SRC.match(/lastUserText\(/g) || []).length;
19
+ const defs = (SRC.match(/function lastUserText\(/g) || []).length;
20
+ assert.ok(defs >= 1, '有引用 ' + refs + ' 处,定义 ' + defs + ' 处');
21
+ });
22
+
23
+ test('注册了 bagu:prefetch', async () => {
24
+ const { apply } = await import('../lib/index.js');
25
+ const ctxs = [];
26
+ const root = {
27
+ inject(deps, fn) {
28
+ if (deps.includes('llm')) return;
29
+ fn({
30
+ systemPrompt: {
31
+ section() { return () => {} },
32
+ context(o) { ctxs.push(o); return () => {} },
33
+ },
34
+ tools: { register() {} },
35
+ webServer: { register() { return () => {} } },
36
+ effect(fn) { return fn() },
37
+ });
38
+ },
39
+ };
40
+ // memes 默认关,先开
41
+ const fs = await import('node:fs');
42
+ const had = fs.existsSync('./config.json');
43
+ const bak = had ? fs.readFileSync('./config.json', 'utf8') : null;
44
+ fs.writeFileSync('./config.json', JSON.stringify({ enabled: true, memes: true }));
45
+ try {
46
+ apply(root, {});
47
+ } finally {
48
+ if (bak !== null) fs.writeFileSync('./config.json', bak);
49
+ else fs.unlinkSync('./config.json');
50
+ }
51
+ assert.ok(ctxs.length > 0, '没注册 context');
52
+ const c = ctxs[0];
53
+ assert.equal(c.name, 'bagu:prefetch');
54
+ assert.equal(typeof c.text, 'function', 'text 得是函数,才能每轮现算');
55
+ });
56
+
57
+ test('有梗的消息能算出注入文本', async () => {
58
+ const { apply } = await import('../lib/index.js');
59
+ const ctxs = [];
60
+ const root = {
61
+ inject(deps, fn) {
62
+ if (deps.includes('llm')) return;
63
+ fn({
64
+ systemPrompt: {
65
+ section() { return () => {} },
66
+ context(o) { ctxs.push(o); return () => {} },
67
+ },
68
+ tools: { register() {} },
69
+ webServer: { register() { return () => {} } },
70
+ effect(fn) { return fn() },
71
+ });
72
+ },
73
+ };
74
+ const fs = await import('node:fs');
75
+ const bak = fs.existsSync('./config.json') ? fs.readFileSync('./config.json', 'utf8') : null;
76
+ fs.writeFileSync('./config.json', JSON.stringify({ enabled: true, memes: true }));
77
+ try {
78
+ apply(root, {});
79
+ const c = ctxs[0];
80
+ const fake = {
81
+ agent: {
82
+ session: {
83
+ snapshotEvents: () => [
84
+ { type: 'user/message', payload: { content: '这不就是瞎问虾猜那个系列吗' } },
85
+ ],
86
+ },
87
+ },
88
+ };
89
+ const out = c.text(fake);
90
+ assert.ok(out.length > 0, '含梗的消息该算出注入文本');
91
+ assert.match(out, /瞎猜/, '该提到摸到的梗');
92
+
93
+ // 不含梗的不注入
94
+ const fake2 = {
95
+ agent: {
96
+ session: {
97
+ snapshotEvents: () => [
98
+ { type: 'user/message', payload: { content: '帮我看看这个报错' } },
99
+ ],
100
+ },
101
+ },
102
+ };
103
+ assert.equal(c.text(fake2), '', '不含梗不该注入');
104
+ } finally {
105
+ if (bak !== null) fs.writeFileSync('./config.json', bak);
106
+ else if (fs.existsSync('./config.json')) fs.unlinkSync('./config.json');
107
+ }
108
+ });
109
+
110
+ test('取不到 session 时返回空,不崩', async () => {
111
+ const { apply } = await import('../lib/index.js');
112
+ const ctxs = [];
113
+ const root = {
114
+ inject(deps, fn) {
115
+ if (deps.includes('llm')) return;
116
+ fn({
117
+ systemPrompt: {
118
+ section() { return () => {} },
119
+ context(o) { ctxs.push(o); return () => {} },
120
+ },
121
+ tools: { register() {} },
122
+ webServer: { register() { return () => {} } },
123
+ effect(fn) { return fn() },
124
+ });
125
+ },
126
+ };
127
+ const fs = await import('node:fs');
128
+ const bak = fs.existsSync('./config.json') ? fs.readFileSync('./config.json', 'utf8') : null;
129
+ fs.writeFileSync('./config.json', JSON.stringify({ enabled: true, memes: true }));
130
+ try {
131
+ apply(root, {});
132
+ const c = ctxs[0];
133
+ for (const bad of [null, {}, { agent: {} }, { agent: { session: {} } }]) {
134
+ assert.doesNotThrow(() => c.text(bad), '崩在 ' + JSON.stringify(bad));
135
+ assert.equal(c.text(bad), '');
136
+ }
137
+ } finally {
138
+ if (bak !== null) fs.writeFileSync('./config.json', bak);
139
+ else if (fs.existsSync('./config.json')) fs.unlinkSync('./config.json');
140
+ }
141
+ });