dsh-bagu 0.7.0 → 0.9.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 +117 -101
- package/client.js +62 -54
- package/dsh-plugin.json +3 -3
- package/lib/ai-bridge.js +64 -0
- package/lib/bridge.js +101 -0
- package/lib/config.js +13 -0
- package/lib/index.js +52 -11
- package/lib/memes.js +20 -2
- package/lib/session-bridge.js +90 -0
- package/package.json +2 -2
- package/test/ai-bridge.test.mjs +27 -0
- package/test/session-bridge.test.mjs +74 -0
package/README.md
CHANGED
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
feat: 优化了用户模块的代码结构,提升了可维护性
|
|
24
24
|
整体实现思路清晰,代码结构合理,建议后续可以进一步优化。
|
|
25
25
|
// 初始化计数器
|
|
26
|
+
注意看,这个男人叫小帅
|
|
26
27
|
```
|
|
27
28
|
|
|
28
29
|
**这个插件在发出去之前拦一道。** 通过就发,没过就打回,附一句怎么改。
|
|
@@ -40,23 +41,21 @@ feat: 优化了用户模块的代码结构,提升了可维护性
|
|
|
40
41
|
|
|
41
42
|
## 三层规则
|
|
42
43
|
|
|
43
|
-
|
|
44
|
+
### 第一层 · 通用(不分场景)
|
|
44
45
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
**空转**——说了动作,没说做了什么。
|
|
46
|
+
**空转** —— 说了动作,没说做了什么。
|
|
48
47
|
|
|
49
48
|
```
|
|
50
49
|
优化了代码结构 / 提升了可维护性 / 修复了若干已知问题 / 进行了必要的处理
|
|
51
50
|
```
|
|
52
51
|
|
|
53
|
-
|
|
52
|
+
**空形容词** —— 能贴到任何产品上的词。
|
|
54
53
|
|
|
55
54
|
```
|
|
56
55
|
先进的技术架构 / 高性能、高可用 / 具备良好的扩展性 / 能够满足业务发展的需要
|
|
57
56
|
```
|
|
58
57
|
|
|
59
|
-
|
|
58
|
+
**万能话** —— 放到哪都通顺。
|
|
60
59
|
|
|
61
60
|
```
|
|
62
61
|
随着人工智能的发展 / 众所周知 / 值得注意的是 / 不仅…更…
|
|
@@ -64,23 +63,10 @@ feat: 优化了用户模块的代码结构,提升了可维护性
|
|
|
64
63
|
|
|
65
64
|
### 第二层 · 套话
|
|
66
65
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
**鸡汤口号**
|
|
74
|
-
|
|
75
|
-
```
|
|
76
|
-
愿你所行皆坦途 / 新的一天新的开始 / 努力不一定会成功,但不努力一定会失败
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
**学习腔**
|
|
80
|
-
|
|
81
|
-
```
|
|
82
|
-
学到了很多 / 涨知识了 / 非常实用 / 希望以后多发这类帖子
|
|
83
|
-
```
|
|
66
|
+
**模板整句**:`这条我认` / `感谢分享` / `受益匪浅` / `说白了就是`
|
|
67
|
+
**鸡汤口号**:`愿你所行皆坦途` / `新的一天新的开始` / `努力不一定会成功`
|
|
68
|
+
**学习腔**:`学到了很多` / `涨知识了` / `希望以后多发这类帖子`
|
|
69
|
+
**视频开场白**:`注意看,这个男人叫小帅` / `话不多说,让我们直接进入正题` / `一键三连`
|
|
84
70
|
|
|
85
71
|
### 第三层 · 场景
|
|
86
72
|
|
|
@@ -93,7 +79,7 @@ feat: 优化了用户模块的代码结构 → 空转
|
|
|
93
79
|
感谢反馈!这个问题我们已经记录 → issue 客服腔
|
|
94
80
|
```
|
|
95
81
|
|
|
96
|
-
|
|
82
|
+
**代码注释** —— 把代码念一遍。
|
|
97
83
|
|
|
98
84
|
```js
|
|
99
85
|
// 初始化计数器 → 打回
|
|
@@ -106,14 +92,87 @@ let total = 0;
|
|
|
106
92
|
|
|
107
93
|
> comments must explain **why**, never **what** the code already expresses.
|
|
108
94
|
|
|
109
|
-
##
|
|
95
|
+
## 引用和使用的区别
|
|
96
|
+
|
|
97
|
+
这是这道门最难判的一件事。
|
|
98
|
+
|
|
99
|
+
```text
|
|
100
|
+
「评论区一水儿「我记下了」」 → 在讨论这句话,不是用它 → 放过
|
|
101
|
+
「我记下了」 → 直接用了 → 拦
|
|
102
|
+
「不要只说「受益匪浅」」 → 在批评,不是在使用 → 放过
|
|
103
|
+
「受益匪浅」 → 真在拍马屁 → 拦
|
|
104
|
+
「一定会火+求互关,雷踩得一个不落」 → 在点评别人的套话 → 放过
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
四条判据:
|
|
108
|
+
|
|
109
|
+
- **引号里**的内容不参与判断
|
|
110
|
+
- 套话前面有**否定词**(不要/别/避免)→ 在批评
|
|
111
|
+
- 套话前面有**指代词**(这句/这个/这条)→ 在指代前文
|
|
112
|
+
- 套话后面跟**评价词**(雷/反面教材/课后题)→ 在拿它当例子
|
|
113
|
+
|
|
114
|
+
## 可选:梗库与接梗
|
|
115
|
+
|
|
116
|
+
梗库判的是另一件事——「这个梗过气了没」「顺着这个能接什么」——
|
|
117
|
+
跟「谁都能说」无关,所以**默认关**。
|
|
118
|
+
|
|
119
|
+
```js
|
|
120
|
+
bagu_meme({ text: "这不就是瞎问虾猜那个系列吗" })
|
|
121
|
+
→ 对方提的是 瞎猜;同族还有 反向空耳、握着我的抱枕、七岁舔地砖、第四种速度
|
|
122
|
+
|
|
123
|
+
bagu_meme({ q: "社死" })
|
|
124
|
+
→ 社死:社死 是「社会性死亡」的简称…
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
数据来自 [梗Wiki](https://cnmeme.wiki) 的公开 API。`data/memes.json` 存了 120 条有完整锚点的词条。
|
|
128
|
+
|
|
129
|
+
**接梗的原理**:梗库的 keywords 在梗之间重叠,形成族。
|
|
130
|
+
|
|
131
|
+
```text
|
|
132
|
+
「瞎问虾猜」→ 反向空耳 / 握着我的抱枕 / 七岁舔地砖 / 第四种速度
|
|
133
|
+
「福本伸行」→ 超智游戏 / 斗牌传说 / 末世路
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### AI 接梗:默认用会话自己的模型,不用填 key
|
|
137
|
+
|
|
138
|
+
梗库只给族,不给具体那句话。要让模型拼一句,开 `aiBridge`。
|
|
139
|
+
|
|
140
|
+
**默认走 DSH 会话自己的模型。**插件拿到当前会话的 provider/model,用宿主已经配好的连接,
|
|
141
|
+
**不用填 API key、不用填接口地址**——跟这个会话本身用的是同一个模型、同一份额度。
|
|
142
|
+
|
|
143
|
+
```text
|
|
144
|
+
对方说:这不就是瞎问虾猜那个系列吗
|
|
145
|
+
这段话涉及这些梗:瞎问虾猜
|
|
146
|
+
同族还可以提到:反向空耳、握着我的抱枕
|
|
147
|
+
→ 会话模型拼一句 25 字以内的接话
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
**另配接口是兜底。**只有在会话模型用不了时才需要开 `aiUseOwn`:
|
|
151
|
+
比如在 QQ 机器人里跑(没有 DSH 会话),或者想换更便宜的模型。
|
|
152
|
+
|
|
153
|
+
**调不通时自动退回本地族提示**(同族还有 A、B、C)。
|
|
154
|
+
没拿到路由、没有 llm 服务、stream 抛错、模型返空、返回超过 80 字,
|
|
155
|
+
任何一条都退回——不会因为模型挂了导致消息发不出去。
|
|
156
|
+
|
|
157
|
+
### 三种成本
|
|
158
|
+
|
|
159
|
+
| 场景 | 配置 | 成本 |
|
|
160
|
+
|---|---|---|
|
|
161
|
+
| 纯浏览 / 日常聊天 | 梗库关 | **0** |
|
|
162
|
+
| 想看能接什么 | 梗库开 | **0**(本地正则) |
|
|
163
|
+
| 要它替你想一句 | 梗库开 + AI 接梗开 | 每条一次会话模型调用 |
|
|
164
|
+
|
|
165
|
+
AI 接梗走会话模型,所以**不需要额外的 key**——用的是这个会话本身那一份额度。
|
|
166
|
+
另配接口那条路才要自己填 key。
|
|
167
|
+
|
|
168
|
+
## 八组数据
|
|
110
169
|
|
|
111
170
|
全部在 `tools/` 里可复现:
|
|
112
171
|
|
|
113
172
|
| 测试 | 样本 | 结果 |
|
|
114
173
|
|---|---|---|
|
|
115
174
|
| **贴吧真人**(孙吧 + v吧原文) | 68 条 | 误拦 0 |
|
|
116
|
-
| **B
|
|
175
|
+
| **B站评论**(两个讨论「AI 味」的视频热评) | 30 条 | 误拦 0 |
|
|
117
176
|
| **全域场景**(18 个场景 × 2 条) | 36 条 | 漏 0,误拦 0 |
|
|
118
177
|
| **真人原话**(DSH + 聊天) | 51 条 | 误拦 0 |
|
|
119
178
|
| **git 真人**(commit + PR + review) | 28 条 | 误拦 0 |
|
|
@@ -122,13 +181,14 @@ let total = 0;
|
|
|
122
181
|
| **对抗绕过**(故意换皮) | 10 种 | 拦住 6(60%) |
|
|
123
182
|
|
|
124
183
|
```sh
|
|
125
|
-
node tools/tieba-real.mjs # 贴吧真人 68
|
|
126
|
-
node tools/
|
|
184
|
+
node tools/tieba-real.mjs # 贴吧真人 68 条
|
|
185
|
+
node tools/bili-real.mjs # B站评论 30 条
|
|
186
|
+
node tools/all-scenes.mjs # 18 个场景
|
|
127
187
|
node tools/falserate.mjs # 真人 51 条
|
|
128
188
|
node tools/git-false.mjs # git 真人 28 条
|
|
129
189
|
node tools/missrate.mjs # AI 八股 35 条
|
|
130
|
-
node tools/evasion.mjs # 对抗绕过
|
|
131
190
|
node tools/audit.mjs # 真实场景 25 条
|
|
191
|
+
node tools/evasion.mjs # 对抗绕过
|
|
132
192
|
```
|
|
133
193
|
|
|
134
194
|
### 贴吧那批语料为什么值钱
|
|
@@ -136,34 +196,15 @@ node tools/audit.mjs # 真实场景 25 条
|
|
|
136
196
|
孙吧、v吧的真人说话充满了「八股规则看来可疑」的形状——
|
|
137
197
|
|
|
138
198
|
```text
|
|
139
|
-
反问:
|
|
140
|
-
|
|
199
|
+
反问: 你确定看不到过去的时间?兄弟
|
|
200
|
+
三段并列: 提瓦特世界是过去、现在、未来同时存在的
|
|
141
201
|
复述后反打:是啊,所以就静悄悄的也忘了
|
|
142
|
-
直接骂:
|
|
143
|
-
|
|
144
|
-
接梗:
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
这些都是真人在说真话。68 条零误拦,说明规则没把这些当成套话。
|
|
148
|
-
|
|
149
|
-
## 引用和使用的区别
|
|
150
|
-
|
|
151
|
-
这是这道门最难判的一件事。
|
|
152
|
-
|
|
153
|
-
```text
|
|
154
|
-
「评论区一水儿「我记下了」」 → 在讨论这句话,不是用它 → 放过
|
|
155
|
-
「我记下了」 → 直接用了 → 拦
|
|
156
|
-
「不要只说「受益匪浅」」 → 在批评,不是在使用 → 放过
|
|
157
|
-
「受益匪浅」 → 真在拍马屁 → 拦
|
|
158
|
-
「一定会火+求互关,雷踩得一个不落」 → 在点评别人的套话 → 放过
|
|
202
|
+
直接骂: 你放屁,分明是天理提供的
|
|
203
|
+
单字回复: 诶嘿 / 草 / 就这?
|
|
204
|
+
接梗: 那维莱特:新鞋,别搞
|
|
159
205
|
```
|
|
160
206
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
- **引号里**的内容不参与判断
|
|
164
|
-
- 套话前面有**否定词**(不要/别/避免)→ 在批评
|
|
165
|
-
- 套话前面有**指代词**(这句/这个/这条)→ 在指代前文
|
|
166
|
-
- 套话后面跟**评价词**(雷/反面教材/课后题)→ 在拿它当例子
|
|
207
|
+
这些全是真人在说真话。68 条零误拦,说明规则没把这些当成套话。
|
|
167
208
|
|
|
168
209
|
## 它拦不住什么
|
|
169
210
|
|
|
@@ -180,7 +221,7 @@ node tools/audit.mjs # 真实场景 25 条
|
|
|
180
221
|
|
|
181
222
|
300 字以上不跳过——长邮件、长周报、长 PR 一样会八股。
|
|
182
223
|
|
|
183
|
-
|
|
224
|
+
但长文的门槛不同:同一个信号要出现**两次以上**才算,一次不算。
|
|
184
225
|
|
|
185
226
|
## 怎么用
|
|
186
227
|
|
|
@@ -192,8 +233,6 @@ bagu_guard({ text: "登录失败时不再清空已填的手机号" })
|
|
|
192
233
|
→ "OK"
|
|
193
234
|
```
|
|
194
235
|
|
|
195
|
-
过了就回 `OK`,两个字。
|
|
196
|
-
|
|
197
236
|
**三种模式:**
|
|
198
237
|
|
|
199
238
|
```js
|
|
@@ -204,28 +243,25 @@ bagu_guard({ text, mode: 'ask' }) // 只要问句
|
|
|
204
243
|
|
|
205
244
|
**输出预算:通过 2 字,打回一行 40 字内。** 按严重度排,只报第一条——报三条 agent 会挑最轻的改。
|
|
206
245
|
|
|
207
|
-
##
|
|
208
|
-
|
|
209
|
-
插件带一个梗库模块,数据来自 [梗Wiki](https://cnmeme.wiki) 的公开 API。
|
|
210
|
-
|
|
211
|
-
**它判的是另一件事**——「这个梗过气了没」「这词什么意思」——跟「谁都能说」不是一回事。
|
|
212
|
-
所以默认关着,要用在设置页开。
|
|
246
|
+
## 省 token
|
|
213
247
|
|
|
214
|
-
|
|
248
|
+
| 项 | 大小 |
|
|
249
|
+
|---|---|
|
|
250
|
+
| 系统提示注入 | **31 字** |
|
|
251
|
+
| 工具描述(1 个) | **54 字** |
|
|
252
|
+
| 守门器输出 | 2 字(通过)/40 字内(打回) |
|
|
215
253
|
|
|
216
|
-
|
|
217
|
-
|
|
254
|
+
固定开销合计 **85 字**。判断全在本地正则里跑完,**不调模型**——
|
|
255
|
+
除了 AI 接梗那个单独开关。
|
|
218
256
|
|
|
219
|
-
|
|
220
|
-
比如「注意看」「这个男人叫小帅」「话不多说」「一键三连」这些视频开场白,
|
|
221
|
-
现在是 `VIDEO_STOCK` 里的一部分,跟梗库开不开无关。
|
|
257
|
+
## 短句也能查
|
|
222
258
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
那是另一个项目的事。
|
|
259
|
+
聊天回复二三十字是常态,密度指标在这个长度上分母太小。
|
|
260
|
+
六十字以下切换成**绝对计数**:不看密度,看命中了什么。
|
|
226
261
|
|
|
227
|
-
```
|
|
228
|
-
|
|
262
|
+
```text
|
|
263
|
+
「这不是技术问题,而是认知问题。」 → 打回
|
|
264
|
+
「卖卡卖完了随便写了」 → 能发
|
|
229
265
|
```
|
|
230
266
|
|
|
231
267
|
## 装
|
|
@@ -238,43 +274,23 @@ dsh plugin --profile web add dsh-bagu
|
|
|
238
274
|
|
|
239
275
|
## 设置页
|
|
240
276
|
|
|
241
|
-
- **挡不挡** —— 总开关
|
|
242
|
-
-
|
|
243
|
-
-
|
|
244
|
-
-
|
|
245
|
-
|
|
246
|
-
## 省 token
|
|
247
|
-
|
|
248
|
-
| 项 | 大小 |
|
|
249
|
-
|---|---|
|
|
250
|
-
| 系统提示注入 | **31 字** |
|
|
251
|
-
| 工具描述(1 个) | **54 字** |
|
|
252
|
-
| 守门器输出 | 2 字(通过)/40 字内(打回) |
|
|
253
|
-
|
|
254
|
-
固定开销合计 **85 字**。判断全在本地正则里跑完,**不调模型**。
|
|
255
|
-
|
|
256
|
-
系统提示只有一句:「要发出去的话,先过 bagu_guard。被打回就改,别硬发。」
|
|
257
|
-
|
|
258
|
-
## 短句也能查
|
|
259
|
-
|
|
260
|
-
聊天回复二三十字是常态,密度指标在这个长度上分母太小。六十字以下切换成**绝对计数**:不看密度,看命中了什么。
|
|
261
|
-
|
|
262
|
-
```text
|
|
263
|
-
「这不是技术问题,而是认知问题。」 → 打回
|
|
264
|
-
「卖卡卖完了随便写了」 → 能发
|
|
265
|
-
```
|
|
277
|
+
- **挡不挡** —— 总开关 / 严格模式 / 提示那一节 / 梗库
|
|
278
|
+
- **AI 接梗** —— 开关 + 显示当前会话的模型 + 另配接口(兜底)
|
|
279
|
+
- **试一段** —— 八种样本,当场看能不能过
|
|
280
|
+
- **阈值** —— 留白词下限、结构腔倍数、具体物下限
|
|
266
281
|
|
|
267
282
|
## 数据从哪来
|
|
268
283
|
|
|
269
284
|
- 两轮对照实验,1169 篇 / 520 万字([lieflat-less-ai-tone](https://github.com/larashero3-dotcom/lieflat-less-ai-tone))
|
|
270
285
|
- 10 万条真人评论 vs 2180 条 AI 评论的密度实测
|
|
271
286
|
- [sloplint](https://github.com/DW-Studio/sloplint) 的评分公式
|
|
272
|
-
- [beagle style-criteria](https://github.com/existential-birds/beagle/blob/main/plugins/beagle-core/skills/llm-artifacts-detection/references/style-criteria.md)
|
|
287
|
+
- [beagle style-criteria](https://github.com/existential-birds/beagle/blob/main/plugins/beagle-core/skills/llm-artifacts-detection/references/style-criteria.md)
|
|
288
|
+
- [梗Wiki](https://cnmeme.wiki) 的公开 API(视频开场白套话与梗库)
|
|
273
289
|
|
|
274
290
|
## 开发
|
|
275
291
|
|
|
276
292
|
```sh
|
|
277
|
-
npm test #
|
|
293
|
+
npm test # 89 个单元测试
|
|
278
294
|
```
|
|
279
295
|
|
|
280
296
|
## 许可
|
package/client.js
CHANGED
|
@@ -66,6 +66,7 @@ window.__ModuleLoader__.load({
|
|
|
66
66
|
[data-bg-sw][data-on]::after{transform:translateX(17px);}
|
|
67
67
|
[data-bg-sw][disabled]{opacity:.45;cursor:default;}
|
|
68
68
|
|
|
69
|
+
[data-bg-num][data-wide]{width:200px;text-align:left;font-variant-numeric:normal;}
|
|
69
70
|
[data-bg-num]{width:88px;padding:6px 9px;border-radius:8px;font:inherit;font-size:13px;
|
|
70
71
|
font-variant-numeric:tabular-nums;text-align:right;color:inherit;
|
|
71
72
|
border:1px solid var(--dsw-alias-border-l,rgba(128,128,128,.28));
|
|
@@ -231,71 +232,78 @@ window.__ModuleLoader__.load({
|
|
|
231
232
|
|
|
232
233
|
h(Row, {
|
|
233
234
|
label: view.memes ? '梗库:开' : '梗库:关',
|
|
234
|
-
note: '开 =
|
|
235
|
+
note: '开 = 多一个接梗工具,看对方说了什么梗,给出同族能接的。' +
|
|
235
236
|
'它判的是「这个梗过气了没」,跟八股无关,所以默认关。',
|
|
236
237
|
}, h(Switch, {
|
|
237
238
|
on: view.memes === true, label: '梗库', disabled: busy === 'save' || !on,
|
|
238
239
|
onChange: () => save({ memes: view.memes !== true }),
|
|
239
240
|
})))),
|
|
240
241
|
|
|
241
|
-
// ──
|
|
242
|
+
// ── AI 接梗(默认走会话模型,不用填 key)──────────
|
|
242
243
|
h('div', null,
|
|
243
|
-
h('div', { 'data-bg-h': '' }, '
|
|
244
|
-
h('div', { 'data-bg-card': '' },
|
|
245
|
-
h('div', { 'data-bg-chips': '' },
|
|
246
|
-
SAMPLES.map((s, i) => h('button', {
|
|
247
|
-
key: i, 'data-bg-chip': '',
|
|
248
|
-
onClick: () => { setText(s.text); setVerdict(null) },
|
|
249
|
-
}, s.label))),
|
|
250
|
-
h('textarea', {
|
|
251
|
-
'data-bg-in': '', value: text, rows: 3,
|
|
252
|
-
placeholder: '粘一段准备发出去的文字。聊天、commit、邮件都行。',
|
|
253
|
-
onChange: (e) => { setText(e.target.value); setVerdict(null) },
|
|
254
|
-
}),
|
|
255
|
-
h('div', { 'data-bg-row': '' },
|
|
256
|
-
h('button', {
|
|
257
|
-
'data-bg-btn': '', 'data-primary': '',
|
|
258
|
-
disabled: busy === 'check', onClick: check,
|
|
259
|
-
}, busy === 'check' ? '过着…' : '过一遍'),
|
|
260
|
-
h('div', { 'data-bg-grow': '' },
|
|
261
|
-
h('div', { 'data-bg-note': '' }, verdict
|
|
262
|
-
? (verdict.pass ? '能发' : '打回')
|
|
263
|
-
: '看这段能不能发出去'))),
|
|
264
|
-
verdict ? h('div', {
|
|
265
|
-
'data-bg-verdict': '',
|
|
266
|
-
'data-pass': verdict.pass ? '' : undefined,
|
|
267
|
-
'data-block': verdict.pass ? undefined : '',
|
|
268
|
-
},
|
|
269
|
-
h('div', { 'data-bg-dot': '' }),
|
|
270
|
-
h('div', { 'data-bg-verdict-text': '' }, verdict.line),
|
|
271
|
-
) : null)),
|
|
272
|
-
|
|
273
|
-
// ── 阈值 ─────────────────────────────
|
|
274
|
-
h('div', null,
|
|
275
|
-
h('div', { 'data-bg-h': '' }, '阈值'),
|
|
244
|
+
h('div', { 'data-bg-h': '' }, 'AI 接梗'),
|
|
276
245
|
h('div', { 'data-bg-card': '' },
|
|
277
246
|
h(Row, {
|
|
278
|
-
label: '
|
|
279
|
-
note: '
|
|
280
|
-
},
|
|
247
|
+
label: view.aiBridge ? 'AI 接梗:开' : 'AI 接梗:关',
|
|
248
|
+
note: '开 = 每条要接梗的消息都调一次模型,拼一句具体的接话。默认走 DSH 会话自己的模型,不用填 key;失败才退回本地族提示。',
|
|
249
|
+
}, h(Switch, {
|
|
250
|
+
on: view.aiBridge === true, label: 'AI 接梗',
|
|
251
|
+
disabled: busy === 'save' || !on || view.memes !== true,
|
|
252
|
+
onChange: () => save({ aiBridge: view.aiBridge !== true }),
|
|
253
|
+
})),
|
|
254
|
+
|
|
281
255
|
h(Row, {
|
|
282
|
-
label: '
|
|
283
|
-
note:
|
|
284
|
-
|
|
256
|
+
label: '用哪个模型',
|
|
257
|
+
note: st.route
|
|
258
|
+
? `当前会话:${st.route.provider} / ${st.route.model}。不用填 key。`
|
|
259
|
+
: '当前会话:还没拿到。DSH 会话里发一句话之后就能拿到。',
|
|
260
|
+
}, h('div', { 'data-bg-note': '', style: { flex: 'none' } },
|
|
261
|
+
st.route ? st.route.model : '—')),
|
|
262
|
+
|
|
285
263
|
h(Row, {
|
|
286
|
-
label: '
|
|
287
|
-
note: '
|
|
288
|
-
},
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
}),
|
|
298
|
-
|
|
264
|
+
label: view.aiUseOwn ? '另配接口:开' : '另配接口:关',
|
|
265
|
+
note: '会话模型用不了时才需要(没有会话、或者想换更便宜的模型)。',
|
|
266
|
+
}, h(Switch, {
|
|
267
|
+
on: view.aiUseOwn === true, label: '另配接口',
|
|
268
|
+
disabled: busy === 'save' || !on || view.aiBridge !== true,
|
|
269
|
+
onChange: () => save({ aiUseOwn: view.aiUseOwn !== true }),
|
|
270
|
+
})),
|
|
271
|
+
|
|
272
|
+
view.aiUseOwn ? h('div', { 'data-bg-row': '' },
|
|
273
|
+
h('div', { 'data-bg-grow': '' },
|
|
274
|
+
h('div', { 'data-bg-label': '' }, '接口地址'),
|
|
275
|
+
h('div', { 'data-bg-note': '' }, 'OpenAI 兼容的 /v1 端点。')),
|
|
276
|
+
h('input', {
|
|
277
|
+
'data-bg-num': '', 'data-wide': '', type: 'text',
|
|
278
|
+
value: view.aiBaseUrl || '', placeholder: 'https://api.openai.com/v1',
|
|
279
|
+
disabled: busy === 'save',
|
|
280
|
+
onChange: (e) => setView({ ...view, aiBaseUrl: e.target.value }),
|
|
281
|
+
onBlur: () => { if (view.aiBaseUrl !== st.config.aiBaseUrl) save({ aiBaseUrl: view.aiBaseUrl }) },
|
|
282
|
+
})) : null,
|
|
283
|
+
|
|
284
|
+
view.aiUseOwn ? h('div', { 'data-bg-row': '' },
|
|
285
|
+
h('div', { 'data-bg-grow': '' },
|
|
286
|
+
h('div', { 'data-bg-label': '' }, '模型名'),
|
|
287
|
+
h('div', { 'data-bg-note': '' }, '比如 gpt-4o-mini、deepseek-chat。')),
|
|
288
|
+
h('input', {
|
|
289
|
+
'data-bg-num': '', 'data-wide': '', type: 'text',
|
|
290
|
+
value: view.aiModel || '', placeholder: 'gpt-4o-mini',
|
|
291
|
+
disabled: busy === 'save',
|
|
292
|
+
onChange: (e) => setView({ ...view, aiModel: e.target.value }),
|
|
293
|
+
onBlur: () => { if (view.aiModel !== st.config.aiModel) save({ aiModel: view.aiModel }) },
|
|
294
|
+
})) : null,
|
|
295
|
+
|
|
296
|
+
view.aiUseOwn ? h('div', { 'data-bg-row': '' },
|
|
297
|
+
h('div', { 'data-bg-grow': '' },
|
|
298
|
+
h('div', { 'data-bg-label': '' }, 'API Key'),
|
|
299
|
+
h('div', { 'data-bg-note': '' }, '明文存在插件目录的 config.json。')),
|
|
300
|
+
h('input', {
|
|
301
|
+
'data-bg-num': '', 'data-wide': '', type: 'password',
|
|
302
|
+
value: view.aiApiKey || '', placeholder: 'sk-...',
|
|
303
|
+
disabled: busy === 'save',
|
|
304
|
+
onChange: (e) => setView({ ...view, aiApiKey: e.target.value }),
|
|
305
|
+
onBlur: () => { if (view.aiApiKey !== st.config.aiApiKey) save({ aiApiKey: view.aiApiKey }) },
|
|
306
|
+
})) : null)),
|
|
299
307
|
|
|
300
308
|
msg ? h('div', {
|
|
301
309
|
'data-bg-msg': '', 'data-err': msg.err ? '' : undefined, 'data-ok': msg.ok ? '' : undefined,
|
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.
|
|
6
|
+
"version": "0.9.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": "
|
|
32
|
+
"description": "所有对外文本发出前过一遍,像八股就打回。接梗默认走会话模型,不用填 key。"
|
|
33
33
|
}
|
|
34
34
|
]
|
|
35
35
|
}
|
package/lib/ai-bridge.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AI 接梗:让模型根据族提示拼一句具体的接话。
|
|
3
|
+
*
|
|
4
|
+
* 这是整个插件里唯一会调模型的地方,所以单独立一个文件,默认关。
|
|
5
|
+
* 关着的时候走 bridge.js 的本地路径,零成本。
|
|
6
|
+
*
|
|
7
|
+
* 成本:每条要发出去的消息,多一次 chat 请求。
|
|
8
|
+
* 养鱼那种要质量的场景值得;日常聊天不值得。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 拼一句接话。
|
|
13
|
+
*
|
|
14
|
+
* @param {string} text 对方说的话
|
|
15
|
+
* @param {{by: string[], same: string[], tags: string[]}} hint bridge() 的结果
|
|
16
|
+
* @param {{baseUrl: string, model: string, apiKey: string}} cfg
|
|
17
|
+
* @param {{timeoutMs?: number}} opts
|
|
18
|
+
* @returns {Promise<string|null>} 接话;失败返回 null,调用方退回本地提示
|
|
19
|
+
*/
|
|
20
|
+
export async function aiBridge(text, hint, cfg, opts = {}) {
|
|
21
|
+
if (!cfg || !cfg.baseUrl || !cfg.model || !cfg.apiKey) return null;
|
|
22
|
+
if (!hint || (!hint.same.length && !hint.by.length)) return null;
|
|
23
|
+
|
|
24
|
+
const fam = hint.by.length ? hint.by : hint.same;
|
|
25
|
+
const prompt = [
|
|
26
|
+
'你在中文互联网的评论区里接话。',
|
|
27
|
+
'',
|
|
28
|
+
`对方说:${text}`,
|
|
29
|
+
`这段话涉及这些梗:${hint.same.join('、') || '(没点名)'}`,
|
|
30
|
+
`同族还可以提到:${fam.join('、')}`,
|
|
31
|
+
hint.tags.length ? `这一类是:${hint.tags.join('/')}` : '',
|
|
32
|
+
'',
|
|
33
|
+
'写一句接话,要求:',
|
|
34
|
+
'- 一句话,25 字以内',
|
|
35
|
+
'- 顺着对方的梗往下接,别解释梗是什么意思',
|
|
36
|
+
'- 不要用「学到了」「受益匪浅」这类套话',
|
|
37
|
+
'- 直接输出那句话,不要引号,不要说明',
|
|
38
|
+
].filter(Boolean).join('\n');
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
const res = await fetch(`${cfg.baseUrl.replace(/\/+$/, '')}/chat/completions`, {
|
|
42
|
+
method: 'POST',
|
|
43
|
+
headers: {
|
|
44
|
+
'content-type': 'application/json',
|
|
45
|
+
authorization: `Bearer ${cfg.apiKey}`,
|
|
46
|
+
},
|
|
47
|
+
body: JSON.stringify({
|
|
48
|
+
model: cfg.model,
|
|
49
|
+
messages: [{ role: 'user', content: prompt }],
|
|
50
|
+
max_tokens: 80,
|
|
51
|
+
temperature: 0.7,
|
|
52
|
+
}),
|
|
53
|
+
signal: AbortSignal.timeout(opts.timeoutMs || 15000),
|
|
54
|
+
});
|
|
55
|
+
if (!res.ok) return null;
|
|
56
|
+
const j = await res.json();
|
|
57
|
+
const line = String(j?.choices?.[0]?.message?.content || '').trim();
|
|
58
|
+
if (!line || line.length > 80) return null;
|
|
59
|
+
// 去掉模型可能加的引号和前缀
|
|
60
|
+
return line.replace(/^[「"'`]+|[」"'`]+$/g, '').replace(/^(接话|回复)[::]\s*/,'').trim() || null;
|
|
61
|
+
} catch {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
}
|
package/lib/bridge.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 接梗:不是查「这是什么意思」,是找「顺着这个能接什么」。
|
|
3
|
+
*
|
|
4
|
+
* 数据里现成的结构是:keywords 在梗之间重叠。
|
|
5
|
+
* 「瞎问虾猜」→ 反向空耳 / 握着我的抱枕 / 七岁舔地砖 / 第四种速度
|
|
6
|
+
* 「福本伸行」→ 超智游戏 / 斗牌传说 / 末世路
|
|
7
|
+
* 这就是族。对方提了一个,从族里挑别的接上去。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** 泛用词,不能当族锚点。和 memes.js 里那份同源。 */
|
|
11
|
+
const STOP = new Set([
|
|
12
|
+
'感谢', '分享', '推荐', '喜欢', '支持', '关注', '点赞', '评论',
|
|
13
|
+
'视频', '内容', '这个', '那个', '什么', '怎么', '可以', '不能',
|
|
14
|
+
'我们', '你们', '他们', '自己', '现在', '以后', '之前', '后来',
|
|
15
|
+
'开始', '结束', '问题', '东西', '时间', '地方', '时候',
|
|
16
|
+
'b站', '网络', '二次创作', '文字梗', '游戏梗', '动画梗', '梗',
|
|
17
|
+
]);
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 找能接的梗。
|
|
21
|
+
*
|
|
22
|
+
* @param {string} text 对方说的话
|
|
23
|
+
* @param {Array} memes 梗库
|
|
24
|
+
* @param {{max?: number}} opts
|
|
25
|
+
* @returns {{by: string[], same: string[], tags: string[]}}
|
|
26
|
+
*/
|
|
27
|
+
export function bridge(text, memes, opts = {}) {
|
|
28
|
+
const src = String(text ?? '');
|
|
29
|
+
const max = opts.max || 4;
|
|
30
|
+
if (!src || !memes || !memes.length) return { by: [], same: [], tags: [] };
|
|
31
|
+
|
|
32
|
+
// 1. 这段话里提到了哪些梗
|
|
33
|
+
const idx = new Map();
|
|
34
|
+
for (const m of memes) {
|
|
35
|
+
for (const k of [m.slug, m.title, ...(m.aliases || []), ...(m.keywords || [])]) {
|
|
36
|
+
if (!k) continue;
|
|
37
|
+
const key = String(k).toLowerCase();
|
|
38
|
+
if (STOP.has(key) || key.length < 2) continue;
|
|
39
|
+
if (!idx.has(key)) idx.set(key, []);
|
|
40
|
+
idx.get(key).push(m);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const hitWords = [];
|
|
45
|
+
for (const k of idx.keys()) {
|
|
46
|
+
if (src.toLowerCase().includes(k)) hitWords.push(k);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 2. 先算「真的提到了」—— 按 slug / title / alias 直接命中,不靠 keyword
|
|
50
|
+
const same = new Set();
|
|
51
|
+
for (const m of memes) {
|
|
52
|
+
const names = [m.slug, m.title, ...(m.aliases || [])]
|
|
53
|
+
.filter(Boolean)
|
|
54
|
+
.map((x) => String(x).toLowerCase());
|
|
55
|
+
if (names.some((n) => n.length >= 2 && src.toLowerCase().includes(n))) same.add(m.slug);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 3. 再算「同族里还有什么」—— 顺着 hitWords 找别的梗,排除已提到的
|
|
59
|
+
const by = new Set();
|
|
60
|
+
for (const k of hitWords) {
|
|
61
|
+
for (const m of idx.get(k) || []) {
|
|
62
|
+
if (same.has(m.slug)) continue;
|
|
63
|
+
by.add(m.slug);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// 4. 标签也带上 —— 有时候接的不是具体梗,是同一类
|
|
68
|
+
const tags = new Set();
|
|
69
|
+
for (const slug of same) {
|
|
70
|
+
const m = memes.find((x) => x.slug === slug);
|
|
71
|
+
for (const t of (m && m.tags) || []) tags.add(t);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
by: [...by].slice(0, max),
|
|
76
|
+
same: [...same].slice(0, max),
|
|
77
|
+
tags: [...tags].slice(0, max),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 给一句人看的接梗提示。
|
|
83
|
+
*
|
|
84
|
+
* 不是替人写回复 —— 是告诉他「对方在说这一族,可以往这个方向接」。
|
|
85
|
+
*/
|
|
86
|
+
export function bridgeHint(text, memes, opts = {}) {
|
|
87
|
+
const r = bridge(text, memes, opts);
|
|
88
|
+
if (!r.same.length && !r.by.length) return null;
|
|
89
|
+
|
|
90
|
+
const parts = [];
|
|
91
|
+
if (r.same.length) {
|
|
92
|
+
parts.push(`对方提的是 ${r.same.join('、')}`);
|
|
93
|
+
if (r.by.length) parts.push(`同族还有 ${r.by.join('、')}`);
|
|
94
|
+
} else if (r.by.length) {
|
|
95
|
+
// 正文没提梗名,但提到了同一族的东西(作者、作品、系列)
|
|
96
|
+
parts.push(`没提具体梗名,但这一族有 ${r.by.join('、')}`);
|
|
97
|
+
}
|
|
98
|
+
if (r.tags.length && !parts.length) parts.push(`这一类是 ${r.tags.slice(0, 3).join('/')}`);
|
|
99
|
+
|
|
100
|
+
return parts.length ? parts.join(';') : null;
|
|
101
|
+
}
|
package/lib/config.js
CHANGED
|
@@ -28,6 +28,19 @@ export const DEFAULTS = {
|
|
|
28
28
|
concreteFloor: 0.6,
|
|
29
29
|
/** 梗库:查这段里有没有网络梗,默认关 —— 它判的是另一件事,不该默认混进来 */
|
|
30
30
|
memes: false,
|
|
31
|
+
/**
|
|
32
|
+
* AI 接梗:让模型根据族提示拼一句具体的接话。
|
|
33
|
+
*
|
|
34
|
+
* 默认关 —— 开了就每条都要调一次模型,要 API key、要付 token。
|
|
35
|
+
* 养鱼那种要质量的场景可以开;日常聊天不值得。
|
|
36
|
+
*/
|
|
37
|
+
aiBridge: false,
|
|
38
|
+
/** 另配接口开关:默认走会话模型,这个开着才用下面三项 */
|
|
39
|
+
aiUseOwn: false,
|
|
40
|
+
/** AI 接梗用的 baseUrl/model/key。aiUseOwn 关着时不看这些。 */
|
|
41
|
+
aiBaseUrl: '',
|
|
42
|
+
aiModel: '',
|
|
43
|
+
aiApiKey: '',
|
|
31
44
|
};
|
|
32
45
|
|
|
33
46
|
export function readConfig() {
|
package/lib/index.js
CHANGED
|
@@ -14,7 +14,10 @@
|
|
|
14
14
|
import { analyze } from './analyze.js';
|
|
15
15
|
import { render, renderBrief } from './render.js';
|
|
16
16
|
import { guard } from './guard.js';
|
|
17
|
-
import { findMemes } from './memes.js';
|
|
17
|
+
import { findMemes, lookup, loadMemes } from './memes.js';
|
|
18
|
+
import { bridge, bridgeHint } from './bridge.js';
|
|
19
|
+
import { aiBridge } from './ai-bridge.js';
|
|
20
|
+
import { captureRoute, sessionBridge } from './session-bridge.js';
|
|
18
21
|
import { readConfig, writeConfig, CONFIG_FILE, DEFAULTS } from './config.js';
|
|
19
22
|
|
|
20
23
|
export const name = 'dsh-bagu';
|
|
@@ -107,27 +110,63 @@ export function apply(root, config = {}) {
|
|
|
107
110
|
}, 'dsh-bagu: 说话的念头');
|
|
108
111
|
});
|
|
109
112
|
|
|
113
|
+
/** 会话自己的路由(provider/model),从 agent/request 抓。抓不到就 null。 */
|
|
114
|
+
let getRoute = () => null;
|
|
115
|
+
root.inject(['llm'], (ctx) => {
|
|
116
|
+
getRoute = captureRoute(ctx);
|
|
117
|
+
});
|
|
118
|
+
|
|
110
119
|
root.inject(['tools'], (ctx) => {
|
|
111
120
|
// 一个工具,三种模式。mode 缺省是守门(日常路径),另外两个是调试用。
|
|
112
121
|
if (live.memes) {
|
|
113
122
|
ctx.tools.register({
|
|
114
123
|
name: 'bagu_meme',
|
|
115
|
-
description: '
|
|
124
|
+
description: '接梗用:看对方说了什么梗,给出同族能接的。也能查单个词什么意思。',
|
|
116
125
|
parameters: {
|
|
117
126
|
type: 'object',
|
|
118
127
|
properties: {
|
|
119
|
-
|
|
128
|
+
text: { type: 'string', description: '对方说的话。给了就找能接的梗。' },
|
|
129
|
+
q: { type: 'string', description: '单个词,查它什么意思。' },
|
|
120
130
|
},
|
|
121
|
-
required: ['q'],
|
|
122
131
|
},
|
|
123
132
|
output: toolOutput(),
|
|
124
133
|
execute: async (args) => {
|
|
125
134
|
const q = String(args?.q ?? '').trim();
|
|
126
|
-
if (
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
135
|
+
if (q) {
|
|
136
|
+
const rows = lookup(q);
|
|
137
|
+
const entry = rows && rows[0];
|
|
138
|
+
if (!entry) return { ok: true, text: `没查到「${q}」` };
|
|
139
|
+
const body = String(entry.summary || '')
|
|
140
|
+
.replace(/\s+/g, ' ')
|
|
141
|
+
.replace(/^(?:含义|来源|用法|简介|解释)\s*/g, '')
|
|
142
|
+
.slice(0, 100);
|
|
143
|
+
return { ok: true, text: `${entry.title}:${body}` };
|
|
144
|
+
}
|
|
145
|
+
const src = String(args?.text ?? '').trim();
|
|
146
|
+
if (!src) return { ok: false, text: '给 text(对方说的话)或 q(要查的词)。' };
|
|
147
|
+
const list = loadMemes();
|
|
148
|
+
const hint = bridgeHint(src, list);
|
|
149
|
+
|
|
150
|
+
// aiBridge 开着就走模型拼一句。两条路,先试会话自己的,再试自己配的 key。
|
|
151
|
+
if (live.aiBridge) {
|
|
152
|
+
const r = bridge(src, list);
|
|
153
|
+
if (r.same.length || r.by.length) {
|
|
154
|
+
// 路子一:DSH 会话自己的模型(不用填 key)
|
|
155
|
+
const llm = ctx.llm || (typeof ctx.get === 'function' ? ctx.get('llm') : undefined);
|
|
156
|
+
if (llm) {
|
|
157
|
+
const viaSession = await sessionBridge(llm, getRoute(), src, r);
|
|
158
|
+
if (viaSession) return { ok: true, text: viaSession };
|
|
159
|
+
}
|
|
160
|
+
// 路子二:另配的 OpenAI 兼容端点(aiUseOwn 开着才走)
|
|
161
|
+
if (live.aiUseOwn && live.aiBaseUrl && live.aiModel && live.aiApiKey) {
|
|
162
|
+
const viaApi = await aiBridge(src, r, {
|
|
163
|
+
baseUrl: live.aiBaseUrl, model: live.aiModel, apiKey: live.aiApiKey,
|
|
164
|
+
});
|
|
165
|
+
if (viaApi) return { ok: true, text: viaApi };
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return { ok: true, text: hint || '没接的。' };
|
|
131
170
|
},
|
|
132
171
|
});
|
|
133
172
|
}
|
|
@@ -173,8 +212,9 @@ export function apply(root, config = {}) {
|
|
|
173
212
|
const g = guard(src, { strict: args?.strict ?? live.strict });
|
|
174
213
|
// 梗库是选装的,默认关。开着就在结果后面附一句,不改判定。
|
|
175
214
|
if (live.memes) {
|
|
176
|
-
|
|
177
|
-
|
|
215
|
+
// 接梗:不是「这段用了哪些梗」,是「顺着这些能接什么」
|
|
216
|
+
const hint = bridgeHint(src, loadMemes());
|
|
217
|
+
if (hint) return { ok: true, text: `${g.line}|${hint}` };
|
|
178
218
|
}
|
|
179
219
|
return { ok: true, text: g.line };
|
|
180
220
|
},
|
|
@@ -192,6 +232,7 @@ export function apply(root, config = {}) {
|
|
|
192
232
|
config: live,
|
|
193
233
|
defaults: DEFAULTS,
|
|
194
234
|
promptActive: live.enabled && live.prompt,
|
|
235
|
+
route: getRoute(),
|
|
195
236
|
configFile: CONFIG_FILE,
|
|
196
237
|
});
|
|
197
238
|
}
|
package/lib/memes.js
CHANGED
|
@@ -19,6 +19,20 @@ const DEFAULT_DATA = join(HERE, '..', 'data', 'memes.json');
|
|
|
19
19
|
let cache = null;
|
|
20
20
|
let cachePath = null;
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* 不能当梗锚点的泛用词。
|
|
24
|
+
*
|
|
25
|
+
* 梗库的 keywords 里混进了这些——「感谢」是某条梗摘要里的词被抽出来的,
|
|
26
|
+
* 结果任何带「感谢」的句子都命中那条梗,完全对不上。
|
|
27
|
+
* 这不是梗,是日常用语。
|
|
28
|
+
*/
|
|
29
|
+
const STOPWORDS = new Set([
|
|
30
|
+
'感谢', '分享', '推荐', '喜欢', '支持', '关注', '点赞', '评论',
|
|
31
|
+
'视频', '内容', '这个', '那个', '什么', '怎么', '可以', '不能',
|
|
32
|
+
'我们', '你们', '他们', '自己', '现在', '以后', '之前', '后来',
|
|
33
|
+
'开始', '结束', '问题', '东西', '时间', '地方', '时候',
|
|
34
|
+
]);
|
|
35
|
+
|
|
22
36
|
/** 读梗库。找不到就返回空,不报错。 */
|
|
23
37
|
export function loadMemes(path = DEFAULT_DATA) {
|
|
24
38
|
if (cache && cachePath === path) return cache;
|
|
@@ -39,7 +53,9 @@ export function loadMemes(path = DEFAULT_DATA) {
|
|
|
39
53
|
export function buildIndex(memes) {
|
|
40
54
|
const idx = new Map();
|
|
41
55
|
for (const m of memes) {
|
|
42
|
-
const keys = new Set([m.slug, m.title, ...(m.aliases || []), ...(m.keywords || [])]
|
|
56
|
+
const keys = new Set([m.slug, m.title, ...(m.aliases || []), ...(m.keywords || [])]
|
|
57
|
+
.filter(Boolean)
|
|
58
|
+
.filter((k) => !STOPWORDS.has(String(k).toLowerCase())));
|
|
43
59
|
for (const k of keys) {
|
|
44
60
|
const key = String(k).toLowerCase();
|
|
45
61
|
if (!idx.has(key)) idx.set(key, []);
|
|
@@ -66,7 +82,9 @@ export function findMemes(text, memes) {
|
|
|
66
82
|
const seen = new Set();
|
|
67
83
|
|
|
68
84
|
// 长词优先:「神龙斗士」比「龙神号」更该先匹配,避免切碎
|
|
69
|
-
const keys = [...idx.keys()]
|
|
85
|
+
const keys = [...idx.keys()]
|
|
86
|
+
.filter((k) => k.length >= 2 && !STOPWORDS.has(k))
|
|
87
|
+
.sort((a, b) => b.length - a.length);
|
|
70
88
|
|
|
71
89
|
for (const k of keys) {
|
|
72
90
|
if (src.toLowerCase().includes(k)) {
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AI 接梗 —— 用 DSH 会话自己的模型,不填 key。
|
|
3
|
+
*
|
|
4
|
+
* 插件在 ctx 里能拿到 llm 服务,用它调模型,走的是宿主已经配好的 provider/model/套餐。
|
|
5
|
+
* 所以不用 API key,也不用另填接口地址。
|
|
6
|
+
*
|
|
7
|
+
* 但要拿到「当前用哪个 provider/model」,得从 agent/request 这个 waterfall 里取。
|
|
8
|
+
* 拿不到就退回纯本地的族提示 —— 不因为链路不通让消息发不出去。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** 从 agent/request 拿到当前路由。拿不到就返回 null。 */
|
|
12
|
+
export function captureRoute(ctx) {
|
|
13
|
+
let route = null;
|
|
14
|
+
try {
|
|
15
|
+
ctx.on('agent/request', async (payload, next) => {
|
|
16
|
+
const resolved = await next();
|
|
17
|
+
if (resolved && resolved.provider && resolved.model) {
|
|
18
|
+
route = { provider: resolved.provider, model: resolved.model };
|
|
19
|
+
}
|
|
20
|
+
return resolved;
|
|
21
|
+
});
|
|
22
|
+
} catch {
|
|
23
|
+
/* 没有这个事件就算了 */
|
|
24
|
+
}
|
|
25
|
+
return () => route;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 调会话自己的模型拼一句接话。
|
|
30
|
+
*
|
|
31
|
+
* @param {object} llm DSH 的 llm 服务
|
|
32
|
+
* @param {{provider:string, model:string}|null} route
|
|
33
|
+
* @param {string} text 对方说的话
|
|
34
|
+
* @param {{same:string[], by:string[], tags:string[]}} hint
|
|
35
|
+
* @param {{timeoutMs?: number}} opts
|
|
36
|
+
* @returns {Promise<string|null>}
|
|
37
|
+
*/
|
|
38
|
+
export async function sessionBridge(llm, route, text, hint, opts = {}) {
|
|
39
|
+
if (!llm || typeof llm.stream !== 'function') return null;
|
|
40
|
+
if (!route || !route.provider || !route.model) return null;
|
|
41
|
+
if (!hint || (!hint.same.length && !hint.by.length)) return null;
|
|
42
|
+
|
|
43
|
+
const fam = hint.by.length ? hint.by : hint.same;
|
|
44
|
+
const prompt = [
|
|
45
|
+
'你在中文互联网的评论区里接话。',
|
|
46
|
+
'',
|
|
47
|
+
`对方说:${text}`,
|
|
48
|
+
`这段话涉及这些梗:${hint.same.join('、') || '(没点名)'}`,
|
|
49
|
+
`同族还可以提到:${fam.join('、')}`,
|
|
50
|
+
hint.tags.length ? `这一类是:${hint.tags.join('/')}` : '',
|
|
51
|
+
'',
|
|
52
|
+
'写一句接话:',
|
|
53
|
+
'- 一句话,25 字以内',
|
|
54
|
+
'- 顺着对方的梗往下接,别解释梗是什么意思',
|
|
55
|
+
'- 不要用「学到了」「受益匪浅」这类套话',
|
|
56
|
+
'- 直接输出那句话,不要引号,不要说明',
|
|
57
|
+
].filter(Boolean).join('\n');
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
const request = {
|
|
61
|
+
provider: route.provider,
|
|
62
|
+
model: route.model,
|
|
63
|
+
messages: [{ role: 'user', content: prompt }],
|
|
64
|
+
maxTokens: 80,
|
|
65
|
+
temperature: 0.7,
|
|
66
|
+
stream: true,
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// 有 prepareCall 就先准备,没有就直接 stream
|
|
70
|
+
const call = typeof llm.prepareCall === 'function'
|
|
71
|
+
? await llm.prepareCall(request, AbortSignal.timeout(opts.timeoutMs || 20000))
|
|
72
|
+
: null;
|
|
73
|
+
|
|
74
|
+
const stream = call && typeof call.stream === 'function'
|
|
75
|
+
? call.stream(request)
|
|
76
|
+
: llm.stream(request);
|
|
77
|
+
|
|
78
|
+
let out = '';
|
|
79
|
+
for await (const chunk of stream) {
|
|
80
|
+
const d = chunk && (chunk.delta || (chunk.content && chunk.content.text));
|
|
81
|
+
if (typeof d === 'string') out += d;
|
|
82
|
+
if (out.length > 200) break;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const line = out.trim().replace(/^[「"'`]+|[」"'`]+$/g, '').replace(/^(接话|回复)[::]\s*/, '').trim();
|
|
86
|
+
return line && line.length <= 80 ? line : null;
|
|
87
|
+
} catch {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { aiBridge } from '../lib/ai-bridge.js';
|
|
4
|
+
|
|
5
|
+
test('没配 key 时直接返回 null,不发请求', async () => {
|
|
6
|
+
const r = await aiBridge('测试', { same: ['a'], by: [], tags: [] }, { baseUrl: '', model: '', apiKey: '' });
|
|
7
|
+
assert.equal(r, null);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test('没梗可接时返回 null', async () => {
|
|
11
|
+
const r = await aiBridge('测试', { same: [], by: [], tags: [] },
|
|
12
|
+
{ baseUrl: 'https://x/v1', model: 'm', apiKey: 'k' });
|
|
13
|
+
assert.equal(r, null);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test('网络挂了返回 null,不抛', async () => {
|
|
17
|
+
const r = await aiBridge('测试', { same: ['a'], by: ['b'], tags: [] },
|
|
18
|
+
{ baseUrl: 'https://127.0.0.1:1/v1', model: 'm', apiKey: 'k' }, { timeoutMs: 3000 });
|
|
19
|
+
assert.equal(r, null);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('缺 baseUrl/model/key 任一项都返回 null', async () => {
|
|
23
|
+
const hint = { same: ['a'], by: ['b'], tags: [] };
|
|
24
|
+
assert.equal(await aiBridge('x', hint, { baseUrl: '', model: 'm', apiKey: 'k' }), null);
|
|
25
|
+
assert.equal(await aiBridge('x', hint, { baseUrl: 'u', model: '', apiKey: 'k' }), null);
|
|
26
|
+
assert.equal(await aiBridge('x', hint, { baseUrl: 'u', model: 'm', apiKey: '' }), null);
|
|
27
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { captureRoute, sessionBridge } from '../lib/session-bridge.js';
|
|
4
|
+
|
|
5
|
+
test('captureRoute:没事件系统时不崩', () => {
|
|
6
|
+
const get = captureRoute({});
|
|
7
|
+
assert.equal(get(), null);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test('captureRoute:注册后能拿到 provider/model', async () => {
|
|
11
|
+
let handler = null;
|
|
12
|
+
const ctx = { on(evt, fn) { if (evt === 'agent/request') handler = fn; } };
|
|
13
|
+
const get = captureRoute(ctx);
|
|
14
|
+
assert.equal(get(), null);
|
|
15
|
+
// 模拟一次 agent/request
|
|
16
|
+
await handler({}, async () => ({ provider: 'deepseek', model: 'deepseek-chat' }));
|
|
17
|
+
assert.deepEqual(get(), { provider: 'deepseek', model: 'deepseek-chat' });
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test('sessionBridge:没有 llm 返回 null', async () => {
|
|
21
|
+
const r = await sessionBridge(null, { provider: 'a', model: 'b' }, 'x', { same: ['y'], by: [], tags: [] });
|
|
22
|
+
assert.equal(r, null);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('sessionBridge:没有 route 返回 null', async () => {
|
|
26
|
+
const llm = { stream() {} };
|
|
27
|
+
const r = await sessionBridge(llm, null, 'x', { same: ['y'], by: [], tags: [] });
|
|
28
|
+
assert.equal(r, null);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('sessionBridge:没梗可接返回 null', async () => {
|
|
32
|
+
const llm = { stream() {} };
|
|
33
|
+
const r = await sessionBridge(llm, { provider: 'a', model: 'b' }, 'x', { same: [], by: [], tags: [] });
|
|
34
|
+
assert.equal(r, null);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('sessionBridge:stream 抛错返回 null,不抛', async () => {
|
|
38
|
+
const llm = { stream() { throw new Error('boom'); } };
|
|
39
|
+
const r = await sessionBridge(llm, { provider: 'a', model: 'b' }, 'x', { same: ['y'], by: [], tags: [] });
|
|
40
|
+
assert.equal(r, null);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('sessionBridge:stream 返回空返回 null', async () => {
|
|
44
|
+
const llm = { stream() { return (async function* () {})(); } };
|
|
45
|
+
const r = await sessionBridge(llm, { provider: 'a', model: 'b' }, 'x', { same: ['y'], by: [], tags: [] });
|
|
46
|
+
assert.equal(r, null);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('sessionBridge:能拼出接话', async () => {
|
|
50
|
+
const llm = {
|
|
51
|
+
stream() {
|
|
52
|
+
return (async function* () {
|
|
53
|
+
yield { delta: '这不就是' };
|
|
54
|
+
yield { delta: '反向空耳那期嘛' };
|
|
55
|
+
})();
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
const r = await sessionBridge(llm, { provider: 'a', model: 'b' }, 'x',
|
|
59
|
+
{ same: ['瞎问虾猜'], by: ['反向空耳'], tags: [] });
|
|
60
|
+
assert.equal(r, '这不就是反向空耳那期嘛');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('sessionBridge:模型加了引号要剥掉', async () => {
|
|
64
|
+
const llm = {
|
|
65
|
+
stream() {
|
|
66
|
+
return (async function* () {
|
|
67
|
+
yield { delta: '「反向空耳那期」' };
|
|
68
|
+
})();
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
const r = await sessionBridge(llm, { provider: 'a', model: 'b' }, 'x',
|
|
72
|
+
{ same: ['瞎问虾猜'], by: ['反向空耳'], tags: [] });
|
|
73
|
+
assert.equal(r, '反向空耳那期');
|
|
74
|
+
});
|