dsh-bagu 0.10.0 → 0.10.1
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 +23 -0
- package/dsh-plugin.json +2 -2
- package/lib/index.js +68 -50
- package/package.json +2 -2
- package/test/realtime.test.mjs +50 -0
package/README.md
CHANGED
|
@@ -113,6 +113,29 @@ feat: 优化了用户模块的代码结构,提升了可维护性
|
|
|
113
113
|
|
|
114
114
|
对方提一个大概念,往下钻一层子形态才叫接住;原地重复叫敷衍。
|
|
115
115
|
|
|
116
|
+
### 真机踩的两个坑(0.10.1 修的)
|
|
117
|
+
|
|
118
|
+
第一版发出去,装进 DSH 实测,模型的原话暴露了两个 bug:
|
|
119
|
+
|
|
120
|
+
```text
|
|
121
|
+
「工具列表里有 bagu_guard 和 .... 没有 bagu_meme 工具在函数列表里。
|
|
122
|
+
系统提示提到 bagu_meme,但实际函数定义中没有这个工具。」 ← bug 1
|
|
123
|
+
|
|
124
|
+
「注意系统提示 [梗] 对方提的是 67 —— 这是承接上一条"67"的。 ← bug 2
|
|
125
|
+
但用户现在换了话题问"灭建国是谁"。」
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
**bug 1 · 工具没注册。**`bagu_meme` 原来被 `if (live.memes)` 包着,
|
|
129
|
+
而 `live` 只在 `apply()` 时读一次 —— 设置页打开梗库后,提示里出现了这个工具名,
|
|
130
|
+
函数列表里永远没有。改成**始终注册,开关在 execute 里判**。
|
|
131
|
+
|
|
132
|
+
**bug 2 · 注入的是上一轮消息。**预查从 `session.snapshotEvents()` 翻,
|
|
133
|
+
但那时当轮 `user/message` 还没 append,翻到的是上一句。
|
|
134
|
+
改成在 `agent/pre-step` 里抓当轮消息,预查优先用那份。
|
|
135
|
+
|
|
136
|
+
两个都是**静默失效**:外面看不出错,只是不生效。跟 0.5.1 那个 `ReferenceError` 被吞是一类。
|
|
137
|
+
现在有 7 条测试专门锁这两处(`test/realtime.test.mjs`)。
|
|
138
|
+
|
|
116
139
|
### 四种接法
|
|
117
140
|
|
|
118
141
|
| 接法 | 什么时候 | 例子 |
|
package/dsh-plugin.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"manifestVersion": "0.15",
|
|
4
4
|
"id": "dsh-bagu",
|
|
5
5
|
"name": "dsh-bagu",
|
|
6
|
-
"version": "0.10.
|
|
6
|
+
"version": "0.10.1",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"description": "AI 对外说话两道门:八股打回,梗接住。",
|
|
9
9
|
"facets": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"x-display": [
|
|
30
30
|
{
|
|
31
31
|
"title": "dsh-bagu",
|
|
32
|
-
"description": "
|
|
32
|
+
"description": "发出去的话像八股就打回;对方抛梗,模型开始想之前就已经把族查好塞进上下文。"
|
|
33
33
|
}
|
|
34
34
|
]
|
|
35
35
|
}
|
package/lib/index.js
CHANGED
|
@@ -102,6 +102,22 @@ export function apply(root, config = {}) {
|
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
root.inject(['systemPrompt'], (ctx) => {
|
|
105
|
+
/** 当轮用户消息。agent/pre-step 里抓,预查注入用这份 —— session 里翻到的常是上一轮。 */
|
|
106
|
+
let currentUserText = '';
|
|
107
|
+
try {
|
|
108
|
+
ctx.on('agent/pre-step', async ({ messages }, next) => {
|
|
109
|
+
try {
|
|
110
|
+
const arr = Array.isArray(messages) ? messages : [];
|
|
111
|
+
for (let i = arr.length - 1; i >= 0; i--) {
|
|
112
|
+
const m = arr[i];
|
|
113
|
+
if (!m || m.role !== 'user') continue;
|
|
114
|
+
const t = payloadText(m);
|
|
115
|
+
if (t) { currentUserText = t; break; }
|
|
116
|
+
}
|
|
117
|
+
} catch { /* 抓不到就算了 */ }
|
|
118
|
+
return next();
|
|
119
|
+
});
|
|
120
|
+
} catch { /* 宿主没这个事件 */ }
|
|
105
121
|
let dispose = null;
|
|
106
122
|
const sync = () => {
|
|
107
123
|
if (dispose) { dispose(); dispose = null; }
|
|
@@ -116,8 +132,9 @@ export function apply(root, config = {}) {
|
|
|
116
132
|
order: 950,
|
|
117
133
|
text: (context) => {
|
|
118
134
|
try {
|
|
119
|
-
const text = lastUserText(context);
|
|
135
|
+
const text = currentUserText || lastUserText(context);
|
|
120
136
|
if (!text) return '';
|
|
137
|
+
currentUserText = '';
|
|
121
138
|
const list = loadMemes();
|
|
122
139
|
const hint = bridgeHint(text, list);
|
|
123
140
|
if (!hint) return '';
|
|
@@ -156,58 +173,59 @@ export function apply(root, config = {}) {
|
|
|
156
173
|
|
|
157
174
|
root.inject(['tools'], (ctx) => {
|
|
158
175
|
// 一个工具,三种模式。mode 缺省是守门(日常路径),另外两个是调试用。
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
},
|
|
176
|
+
// 始终注册 —— 配置热更新时不用重注册。开关在 execute 里判。
|
|
177
|
+
ctx.tools.register({
|
|
178
|
+
name: 'bagu_meme',
|
|
179
|
+
description: '接梗用:看对方说了什么梗,给出同族能接的。也能查单个词什么意思。',
|
|
180
|
+
parameters: {
|
|
181
|
+
type: 'object',
|
|
182
|
+
properties: {
|
|
183
|
+
text: { type: 'string', description: '对方说的话。给了就找能接的梗。' },
|
|
184
|
+
q: { type: 'string', description: '单个词,查它什么意思。' },
|
|
169
185
|
},
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
if (
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
186
|
+
},
|
|
187
|
+
output: toolOutput(),
|
|
188
|
+
execute: async (args) => {
|
|
189
|
+
// 这个工具始终注册,但梗库关着时不该被调用
|
|
190
|
+
if (!live.memes) return { ok: false, text: '梗库没开。设置 → 八股检测里打开。' };
|
|
191
|
+
const q = String(args?.q ?? '').trim();
|
|
192
|
+
if (q) {
|
|
193
|
+
const rows = lookup(q);
|
|
194
|
+
const entry = rows && rows[0];
|
|
195
|
+
if (!entry) return { ok: true, text: `没查到「${q}」` };
|
|
196
|
+
const body = String(entry.summary || '')
|
|
197
|
+
.replace(/\s+/g, ' ')
|
|
198
|
+
.replace(/^(?:含义|来源|用法|简介|解释)\s*/g, '')
|
|
199
|
+
.slice(0, 240);
|
|
200
|
+
return { ok: true, text: `${entry.title}:${body}` };
|
|
201
|
+
}
|
|
202
|
+
const src = String(args?.text ?? '').trim();
|
|
203
|
+
if (!src) return { ok: false, text: '给 text(对方说的话)或 q(要查的词)。' };
|
|
204
|
+
const list = loadMemes();
|
|
205
|
+
const hint = bridgeHint(src, list);
|
|
206
|
+
|
|
207
|
+
// aiBridge 开着就走模型拼一句。两条路,先试会话自己的,再试自己配的 key。
|
|
208
|
+
if (live.aiBridge) {
|
|
209
|
+
const r = bridge(src, list);
|
|
210
|
+
if (r.same.length || r.by.length) {
|
|
211
|
+
// 路子一:DSH 会话自己的模型(不用填 key)
|
|
212
|
+
const llm = ctx.llm || (typeof ctx.get === 'function' ? ctx.get('llm') : undefined);
|
|
213
|
+
if (llm) {
|
|
214
|
+
const viaSession = await sessionBridge(llm, getRoute(), src, r);
|
|
215
|
+
if (viaSession) return { ok: true, text: viaSession };
|
|
216
|
+
}
|
|
217
|
+
// 路子二:另配的 OpenAI 兼容端点(aiUseOwn 开着才走)
|
|
218
|
+
if (live.aiUseOwn && live.aiBaseUrl && live.aiModel && live.aiApiKey) {
|
|
219
|
+
const viaApi = await aiBridge(src, r, {
|
|
220
|
+
baseUrl: live.aiBaseUrl, model: live.aiModel, apiKey: live.aiApiKey,
|
|
221
|
+
});
|
|
222
|
+
if (viaApi) return { ok: true, text: viaApi };
|
|
205
223
|
}
|
|
206
224
|
}
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
225
|
+
}
|
|
226
|
+
return { ok: true, text: hint || '没接的。' };
|
|
227
|
+
},
|
|
228
|
+
});
|
|
211
229
|
|
|
212
230
|
ctx.tools.register({
|
|
213
231
|
name: 'bagu_guard',
|
package/package.json
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
|
|
5
|
+
// 真机反馈暴露的两个 bug:
|
|
6
|
+
// 1. bagu_meme 只在 apply 时按 live.memes 注册 → 设置页改了不生效,
|
|
7
|
+
// 提示里说有这个工具,实际函数列表里没有(模型原话:「没有 bagu_meme 工具在函数列表里」)
|
|
8
|
+
// 2. 预查注入从 session 翻消息 → 那时当轮 user/message 还没 append,
|
|
9
|
+
// 拿到的是上一轮的(模型原话:「对方提的是 67 —— 这是承接上一条 67 的」)
|
|
10
|
+
//
|
|
11
|
+
// 这两条都得锁住,不然下次改回去又静默失效。
|
|
12
|
+
|
|
13
|
+
const SRC = readFileSync('lib/index.js', 'utf8');
|
|
14
|
+
|
|
15
|
+
test('bagu_meme 必须无条件注册', () => {
|
|
16
|
+
// 不允许出现 if (live.memes) { ctx.tools.register
|
|
17
|
+
const pat = /if\s*\(\s*live\.memes\s*\)\s*\{\s*ctx\.tools\.register/;
|
|
18
|
+
assert.doesNotMatch(SRC, pat, 'bagu_meme 又被条件包裹了,热更新会失效');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('bagu_meme 的 execute 里有开关检查', () => {
|
|
22
|
+
const i = SRC.indexOf("name: 'bagu_meme'");
|
|
23
|
+
assert.ok(i > 0, '找不到 bagu_meme');
|
|
24
|
+
const tail = SRC.slice(i, i + 600);
|
|
25
|
+
assert.match(tail, /if \(!live\.memes\)/, 'execute 里得判 live.memes,否则关了还能用');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('预查优先用当轮消息,不靠 session 翻', () => {
|
|
29
|
+
assert.match(SRC, /currentUserText \|\| lastUserText/, '预查得优先用 pre-step 抓到的当轮消息');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('注册了 agent/pre-step 钩子', () => {
|
|
33
|
+
assert.match(SRC, /ctx\.on\('agent\/pre-step'/, '没挂 pre-step,当轮消息抓不到');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('pre-step 里取的是 role === user', () => {
|
|
37
|
+
const i = SRC.indexOf("ctx.on('agent/pre-step'");
|
|
38
|
+
assert.ok(i > 0);
|
|
39
|
+
const tail = SRC.slice(i, i + 400);
|
|
40
|
+
assert.match(tail, /role !== 'user'/, '只取 user,别把 assistant 也算进去');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('用完就清,不残留上一轮', () => {
|
|
44
|
+
assert.match(SRC, /currentUserText = '';/, '取完得清掉,不然下一轮没新消息时会复用旧的');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('payloadText 和 lastUserText 都有定义', () => {
|
|
48
|
+
assert.match(SRC, /function payloadText\(/);
|
|
49
|
+
assert.match(SRC, /function lastUserText\(/);
|
|
50
|
+
});
|