dsh-bagu 0.13.0 → 0.13.2
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/CONTRIBUTORS.md +42 -0
- package/README.md +9 -0
- package/client.js +1 -13
- package/dsh-plugin.json +1 -1
- package/lib/config.js +1 -3
- package/lib/index.js +486 -475
- package/package.json +3 -2
- package/test/ai-bridge-tools.test.mjs +124 -0
- package/test/schedule.test.mjs +46 -0
- package/test/tools-execute.test.mjs +122 -0
- package/test/update.test.mjs +11 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-bagu",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.2",
|
|
4
4
|
"description": "两道门:八股打回(含空指代/拆句弹回/承诺收尾那套 DeepSeek 腔);梗接住(内置梗Wiki 6845 条,每天自动更新)。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
"cordis.patch.yml",
|
|
19
19
|
"README.md",
|
|
20
20
|
"LICENSE",
|
|
21
|
-
"NOTICE"
|
|
21
|
+
"NOTICE",
|
|
22
|
+
"CONTRIBUTORS.md"
|
|
22
23
|
],
|
|
23
24
|
"scripts": {
|
|
24
25
|
"test": "node --test test/*.test.mjs"
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { readFileSync, writeFileSync, existsSync, unlinkSync } from 'node:fs';
|
|
4
|
+
|
|
5
|
+
// 真机反馈(v0.12.1):设置页打开「AI 接梗」后,bagu_guard 一碰到命中梗库的
|
|
6
|
+
// 文本就抛 TypeError: Cannot read properties of undefined (reading 'length')。
|
|
7
|
+
// 根因:index.js 里 import 的 bridge 来自 memes.js,返回 {hits, usage, origin, tags},
|
|
8
|
+
// 但 AI 分支按 bridge.js 同名函数的返回 {same, by, tags} 去读 r.same.length。
|
|
9
|
+
// 顺带:bagu_meme 的 AI 分支还引用了未定义变量 list(ReferenceError)。
|
|
10
|
+
// 这里搭假宿主把两个工具的 execute 真跑一遍,锁住修复。
|
|
11
|
+
|
|
12
|
+
/** 假宿主:收集注册的工具,可注入假 llm,可模拟一次 agent/request 拿路由。 */
|
|
13
|
+
async function boot(cfg, { llm } = {}) {
|
|
14
|
+
const { apply } = await import('../lib/index.js');
|
|
15
|
+
const tools = {};
|
|
16
|
+
const reqHandlers = [];
|
|
17
|
+
const root = {
|
|
18
|
+
inject(deps, fn) {
|
|
19
|
+
fn({
|
|
20
|
+
systemPrompt: {
|
|
21
|
+
section() { return () => {}; },
|
|
22
|
+
context() { return () => {}; },
|
|
23
|
+
},
|
|
24
|
+
tools: { register(t) { tools[t.name] = t; } },
|
|
25
|
+
webServer: { register() { return () => {}; } },
|
|
26
|
+
effect(fn) { return fn(); },
|
|
27
|
+
on(evt, h) { if (evt === 'agent/request') reqHandlers.push(h); },
|
|
28
|
+
llm,
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
const bak = existsSync('./config.json') ? readFileSync('./config.json', 'utf8') : null;
|
|
33
|
+
writeFileSync('./config.json', JSON.stringify(cfg));
|
|
34
|
+
try {
|
|
35
|
+
apply(root, {});
|
|
36
|
+
} finally {
|
|
37
|
+
if (bak !== null) writeFileSync('./config.json', bak);
|
|
38
|
+
else if (existsSync('./config.json')) unlinkSync('./config.json');
|
|
39
|
+
}
|
|
40
|
+
const setRoute = async () => {
|
|
41
|
+
for (const h of reqHandlers) await h({}, async () => ({ provider: 'p', model: 'm' }));
|
|
42
|
+
};
|
|
43
|
+
return { tools, setRoute };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 假 llm:记下请求,吐一句固定接话。 */
|
|
47
|
+
function fakeLlm(line) {
|
|
48
|
+
const seen = [];
|
|
49
|
+
return {
|
|
50
|
+
seen,
|
|
51
|
+
stream(req) {
|
|
52
|
+
seen.push(req);
|
|
53
|
+
return (async function* () { yield { delta: line }; })();
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const CFG = { enabled: true, memes: true, aiBridge: true, autoMeme: false, autoUpdate: false };
|
|
59
|
+
// 「康神开播了」在内置库里是精确标题;「真正的」能片段命中,两个都覆盖。
|
|
60
|
+
const MEME_TEXT = '康神开播了,你才是真正的英雄';
|
|
61
|
+
const PLAIN_TEXT = '帮我看看这个报错';
|
|
62
|
+
|
|
63
|
+
test('bagu_guard:AI 接梗开着 + 命中梗,不抛 TypeError,拼出接话', async () => {
|
|
64
|
+
const llm = fakeLlm('这波是纯路人局');
|
|
65
|
+
const { tools, setRoute } = await boot(CFG, { llm });
|
|
66
|
+
await setRoute();
|
|
67
|
+
const out = await tools.bagu_guard.execute({ text: MEME_TEXT });
|
|
68
|
+
assert.equal(out.ok, true);
|
|
69
|
+
assert.match(out.text, /|接:这波是纯路人局$/, '该带上 AI 拼的接话,实际: ' + out.text);
|
|
70
|
+
// 传给模型的是 {same, by, tags} 映射后的内容:same 里得有命中的梗名
|
|
71
|
+
assert.ok(llm.seen.length >= 1, '该调一次模型');
|
|
72
|
+
const prompt = llm.seen[0].messages[0].content;
|
|
73
|
+
assert.match(prompt, /康神开播了/, 'same 列表没进 prompt,映射没生效');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('bagu_guard:AI 接梗开着 + 没命中梗,正常 OK 不调模型', async () => {
|
|
77
|
+
const llm = fakeLlm('不该被用到');
|
|
78
|
+
const { tools, setRoute } = await boot(CFG, { llm });
|
|
79
|
+
await setRoute();
|
|
80
|
+
const out = await tools.bagu_guard.execute({ text: PLAIN_TEXT });
|
|
81
|
+
assert.equal(out.ok, true);
|
|
82
|
+
assert.equal(out.text, 'OK');
|
|
83
|
+
assert.equal(llm.seen.length, 0, '没梗不该调模型');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('bagu_guard:AI 接梗开着但拿不到 llm/路由,退回族提示不抛错', async () => {
|
|
87
|
+
const { tools } = await boot(CFG, {});
|
|
88
|
+
const out = await tools.bagu_guard.execute({ text: MEME_TEXT });
|
|
89
|
+
assert.equal(out.ok, true);
|
|
90
|
+
assert.match(out.text, /\[梗\] 对方提到:/, '该退回族提示,实际: ' + out.text);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test('bagu_guard:AI 接梗关着,行为不变', async () => {
|
|
94
|
+
const llm = fakeLlm('不该被用到');
|
|
95
|
+
const { tools, setRoute } = await boot({ ...CFG, aiBridge: false }, { llm });
|
|
96
|
+
await setRoute();
|
|
97
|
+
const out = await tools.bagu_guard.execute({ text: MEME_TEXT });
|
|
98
|
+
assert.equal(out.ok, true);
|
|
99
|
+
assert.match(out.text, /^OK|\[梗\] 对方提到:/);
|
|
100
|
+
assert.equal(llm.seen.length, 0, '关着不该调模型');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('bagu_meme:AI 接梗开着 + 命中梗,不抛 ReferenceError(list)', async () => {
|
|
104
|
+
const llm = fakeLlm('康神这波稳了');
|
|
105
|
+
const { tools, setRoute } = await boot(CFG, { llm });
|
|
106
|
+
await setRoute();
|
|
107
|
+
const out = await tools.bagu_meme.execute({ text: MEME_TEXT });
|
|
108
|
+
assert.equal(out.ok, true);
|
|
109
|
+
assert.equal(out.text, '康神这波稳了');
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('bagu_meme:AI 接梗开着但没路由,退回族提示不抛错', async () => {
|
|
113
|
+
const { tools } = await boot(CFG, {});
|
|
114
|
+
const out = await tools.bagu_meme.execute({ text: MEME_TEXT });
|
|
115
|
+
assert.equal(out.ok, true);
|
|
116
|
+
assert.match(out.text, /\[梗\] 对方提到:/, '实际: ' + out.text);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('bagu_meme:q 查词路径不受影响', async () => {
|
|
120
|
+
const { tools } = await boot(CFG, {});
|
|
121
|
+
const out = await tools.bagu_meme.execute({ q: '康神开播了' });
|
|
122
|
+
assert.equal(out.ok, true);
|
|
123
|
+
assert.match(out.text, /^康神开播了:/);
|
|
124
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
|
|
5
|
+
const INDEX = readFileSync('lib/index.js', 'utf8');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 定时更新的判据。
|
|
9
|
+
*
|
|
10
|
+
* 原来写的是「当前小时 === 配置的小时」,DSH 那一刻不在就永远错过。
|
|
11
|
+
* 实测九天一次没跑过(库里 last_check 停在 11-08,今天是 11-17)。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
test('不许死等整点', () => {
|
|
15
|
+
// hour !== target 这种判法,错过那一小时就整天跳过。
|
|
16
|
+
assert.doesNotMatch(INDEX, /hour !== target/, '又改回死等整点了');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test('按「距上次超过 20 小时」判', () => {
|
|
20
|
+
assert.match(INDEX, /20 \* 60 \* 60 \* 1000/, '得有 20 小时的时间闸');
|
|
21
|
+
assert.match(INDEX, /stale/, '得算过不过期');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('不看几点,到点就补', () => {
|
|
25
|
+
// 原来是「当前小时 === 配置的小时」,DSH 那一刻不在就整天跳过 —— 实测九天没跑成。
|
|
26
|
+
// 现在是距上次超过 20 小时就补,跟几点无关。
|
|
27
|
+
assert.doesNotMatch(INDEX, /hour < target/, '不该再卡小时');
|
|
28
|
+
assert.doesNotMatch(INDEX, /getHours\(\)/, '不该再看当前小时');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('定时器要 unref,不能挂住进程', () => {
|
|
32
|
+
// 踩过:加了 setInterval 之后测试从 2.4 秒变卡死 90 秒。
|
|
33
|
+
assert.match(INDEX, /\.unref\(\)/);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('autoUpdate 关掉就一次都不跑', () => {
|
|
37
|
+
assert.match(INDEX, /if \(!live\.autoUpdate\) return/);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('首查留出开机缓冲', () => {
|
|
41
|
+
// 开机瞬间一堆东西抢网络,别第一秒就去翻 229 页。
|
|
42
|
+
const m = INDEX.match(/setTimeout\(tick,\s*(\d+)\)/);
|
|
43
|
+
assert.ok(m, '找不到首次 tick');
|
|
44
|
+
const ms = Number(m[1]);
|
|
45
|
+
assert.ok(ms >= 30000, `首查 ${ms}ms 太急,开机时网络还没稳`);
|
|
46
|
+
});
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { writeFileSync, existsSync, readFileSync, unlinkSync } from 'node:fs';
|
|
4
|
+
import { apply } from '../lib/index.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 真跑两个工具的 execute。
|
|
8
|
+
*
|
|
9
|
+
* 这块之前是测试盲区:130 个测试,没一条调过 bagu_guard.execute / bagu_meme.execute,
|
|
10
|
+
* 所以「AI 接梗一开就 TypeError」这种线上炸 15 次的问题,测试全绿。
|
|
11
|
+
*
|
|
12
|
+
* 教训:测注册、测底层函数,不等于测工具真的能跑。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
function mount(config) {
|
|
16
|
+
const bak = existsSync('./config.json') ? readFileSync('./config.json', 'utf8') : null;
|
|
17
|
+
writeFileSync('./config.json', JSON.stringify(config));
|
|
18
|
+
const tools = {};
|
|
19
|
+
apply({
|
|
20
|
+
inject(deps, fn) {
|
|
21
|
+
if (deps.includes('llm')) return;
|
|
22
|
+
fn({
|
|
23
|
+
systemPrompt: { section() { return () => {} }, context() { return () => {} } },
|
|
24
|
+
tools: { register(t) { tools[t.name] = t } },
|
|
25
|
+
webServer: { register() { return () => {} } },
|
|
26
|
+
effect(fn) { return fn() },
|
|
27
|
+
on() {},
|
|
28
|
+
});
|
|
29
|
+
},
|
|
30
|
+
}, {});
|
|
31
|
+
return {
|
|
32
|
+
tools,
|
|
33
|
+
done() {
|
|
34
|
+
if (bak !== null) writeFileSync('./config.json', bak);
|
|
35
|
+
else if (existsSync('./config.json')) unlinkSync('./config.json');
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const BASE = { enabled: true, memes: true, autoMeme: true };
|
|
41
|
+
|
|
42
|
+
test('aiBridge 关着:命中梗也不崩', async () => {
|
|
43
|
+
const m = mount({ ...BASE, aiBridge: false });
|
|
44
|
+
try {
|
|
45
|
+
const r = await m.tools.bagu_guard.execute({ text: '康神开播了' });
|
|
46
|
+
assert.equal(typeof r.text, 'string');
|
|
47
|
+
assert.ok(r.text.length > 0);
|
|
48
|
+
} finally { m.done(); }
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('aiBridge 开着:命中梗不崩(线上炸的就是这条)', async () => {
|
|
52
|
+
// 根因:bridge() 来自 memes.js,返回 {hits, usage, tags};
|
|
53
|
+
// AI 分支按 bridge.js 的 {same, by} 去读,r.same 是 undefined,
|
|
54
|
+
// 读 .length 就是 TypeError。真机一天炸 15 次。
|
|
55
|
+
const m = mount({ ...BASE, aiBridge: true });
|
|
56
|
+
try {
|
|
57
|
+
const r = await m.tools.bagu_guard.execute({ text: '康神开播了' });
|
|
58
|
+
assert.equal(typeof r.text, 'string', '不该抛,实际: ' + JSON.stringify(r));
|
|
59
|
+
assert.ok(r.text.length > 0);
|
|
60
|
+
} finally { m.done(); }
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('aiBridge 开着:bagu_meme 命中梗不崩', async () => {
|
|
64
|
+
// 伴生问题:那段引用了未定义的 list → ReferenceError
|
|
65
|
+
const m = mount({ ...BASE, aiBridge: true });
|
|
66
|
+
try {
|
|
67
|
+
const r = await m.tools.bagu_meme.execute({ text: '康神开播了' });
|
|
68
|
+
assert.equal(typeof r.text, 'string');
|
|
69
|
+
assert.ok(r.text.length > 0);
|
|
70
|
+
} finally { m.done(); }
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('aiBridge 开着:未命中梗走正常路径', async () => {
|
|
74
|
+
const m = mount({ ...BASE, aiBridge: true });
|
|
75
|
+
try {
|
|
76
|
+
const r = await m.tools.bagu_guard.execute({ text: '帮我看看这个报错' });
|
|
77
|
+
assert.equal(r.text, 'OK');
|
|
78
|
+
const r2 = await m.tools.bagu_meme.execute({ text: '帮我看看这个报错' });
|
|
79
|
+
assert.ok(r2.text.length > 0);
|
|
80
|
+
} finally { m.done(); }
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test('q 查词路径不受 AI 分支影响', async () => {
|
|
84
|
+
const m = mount({ ...BASE, aiBridge: true });
|
|
85
|
+
try {
|
|
86
|
+
const r = await m.tools.bagu_meme.execute({ q: '内卷' });
|
|
87
|
+
assert.match(r.text, /内卷/);
|
|
88
|
+
assert.ok(!r.text.includes('undefined'), '输出里不该出现 undefined: ' + r.text.slice(0, 80));
|
|
89
|
+
} finally { m.done(); }
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('梗库关着时 bagu_meme 明确拒绝', async () => {
|
|
93
|
+
const m = mount({ ...BASE, memes: false });
|
|
94
|
+
try {
|
|
95
|
+
const r = await m.tools.bagu_meme.execute({ text: '康神开播了' });
|
|
96
|
+
assert.equal(r.ok, false);
|
|
97
|
+
assert.match(r.text, /梗库没开/);
|
|
98
|
+
} finally { m.done(); }
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test('空参数不崩', async () => {
|
|
102
|
+
const m = mount({ ...BASE, aiBridge: true });
|
|
103
|
+
try {
|
|
104
|
+
for (const args of [{}, { text: '' }, { text: null }]) {
|
|
105
|
+
await assert.doesNotReject(() => m.tools.bagu_guard.execute(args), JSON.stringify(args));
|
|
106
|
+
await assert.doesNotReject(() => m.tools.bagu_meme.execute(args), JSON.stringify(args));
|
|
107
|
+
}
|
|
108
|
+
} finally { m.done(); }
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('守门判定本身不受 AI 接梗影响', async () => {
|
|
112
|
+
// 开不开 AI 接梗,能不能发出去这件事必须一样。
|
|
113
|
+
const off = mount({ ...BASE, aiBridge: false });
|
|
114
|
+
const a = await off.tools.bagu_guard.execute({ text: '这话我记下了。' });
|
|
115
|
+
off.done();
|
|
116
|
+
|
|
117
|
+
const on = mount({ ...BASE, aiBridge: true });
|
|
118
|
+
const b = await on.tools.bagu_guard.execute({ text: '这话我记下了。' });
|
|
119
|
+
on.done();
|
|
120
|
+
|
|
121
|
+
assert.equal(a.text.split('|')[0], b.text.split('|')[0], '判定该一致');
|
|
122
|
+
});
|
package/test/update.test.mjs
CHANGED
|
@@ -19,15 +19,17 @@ test('备份要轮转,不能只增不减', () => {
|
|
|
19
19
|
assert.match(UPDATE, /pruneBackups\(log\)/, '写完备份要调一下');
|
|
20
20
|
});
|
|
21
21
|
|
|
22
|
-
test('
|
|
23
|
-
//
|
|
24
|
-
assert.match(INDEX, /
|
|
25
|
-
assert.
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test('
|
|
29
|
-
|
|
30
|
-
|
|
22
|
+
test('按「距上次超过 20 小时」判,不跟整点绑', () => {
|
|
23
|
+
// 死在整点上那套在 DSH 上不成立:DSH 不是常驻服务,四点不一定会开着。
|
|
24
|
+
assert.match(INDEX, /20 \* 60 \* 60 \* 1000/, '得有 20 小时的时间闸');
|
|
25
|
+
assert.doesNotMatch(INDEX, /live\.updateHour/, '不该再读小时配置');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('刚跑过就不重复跑', () => {
|
|
29
|
+
// 判据从「同一天」改成「20 小时内」。原来按日期判,跨零点会重跑一次。
|
|
30
|
+
assert.match(INDEX, /stale/, '得算过不过期');
|
|
31
|
+
assert.doesNotMatch(INDEX, /toDateString\(\)/, '不该再按日期判');
|
|
32
|
+
});
|
|
31
33
|
|
|
32
34
|
test('列表接口拿不到总数,翻页要容错', () => {
|
|
33
35
|
// 接口只返回 items / limit / offset / has_more,没有 total。
|