dsh-bagu 0.7.0 → 0.8.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 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,71 @@ 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 接梗(要花钱,单独开关)
137
+
138
+ 梗库只给族,不给具体那句话。要让模型拼一句,开 `aiBridge`:
139
+
140
+ ```text
141
+ 对方说:这不就是瞎问虾猜那个系列吗
142
+ 这段话涉及这些梗:瞎问虾猜
143
+ 同族还可以提到:反向空耳、握着我的抱枕
144
+ → 模型拼一句 25 字以内的接话
145
+ ```
146
+
147
+ **开着就每条都要调一次模型**,要 API key、要付 token。
148
+ 养鱼那种要质量的场景值得;日常聊天不值得。
149
+
150
+ **调不通时自动退回本地提示**(同族还有 A、B、C),不会因为模型挂了导致消息发不出去。
151
+
152
+ ## 八组数据
110
153
 
111
154
  全部在 `tools/` 里可复现:
112
155
 
113
156
  | 测试 | 样本 | 结果 |
114
157
  |---|---|---|
115
158
  | **贴吧真人**(孙吧 + v吧原文) | 68 条 | 误拦 0 |
116
- | **B站评论**(猫娘草稿法 + 鱼八股热评) | 50 条 | 误拦 0 |
159
+ | **B站评论**(两个讨论「AI 味」的视频热评) | 30 条 | 误拦 0 |
117
160
  | **全域场景**(18 个场景 × 2 条) | 36 条 | 漏 0,误拦 0 |
118
161
  | **真人原话**(DSH + 聊天) | 51 条 | 误拦 0 |
119
162
  | **git 真人**(commit + PR + review) | 28 条 | 误拦 0 |
@@ -122,13 +165,14 @@ let total = 0;
122
165
  | **对抗绕过**(故意换皮) | 10 种 | 拦住 6(60%) |
123
166
 
124
167
  ```sh
125
- node tools/tieba-real.mjs # 贴吧真人 68 条(孙吧、v吧原文)
126
- node tools/all-scenes.mjs # 18 个场景,36 条
168
+ node tools/tieba-real.mjs # 贴吧真人 68 条
169
+ node tools/bili-real.mjs # B站评论 30 条
170
+ node tools/all-scenes.mjs # 18 个场景
127
171
  node tools/falserate.mjs # 真人 51 条
128
172
  node tools/git-false.mjs # git 真人 28 条
129
173
  node tools/missrate.mjs # AI 八股 35 条
130
- node tools/evasion.mjs # 对抗绕过
131
174
  node tools/audit.mjs # 真实场景 25 条
175
+ node tools/evasion.mjs # 对抗绕过
132
176
  ```
133
177
 
134
178
  ### 贴吧那批语料为什么值钱
@@ -136,34 +180,15 @@ node tools/audit.mjs # 真实场景 25 条
136
180
  孙吧、v吧的真人说话充满了「八股规则看来可疑」的形状——
137
181
 
138
182
  ```text
139
- 反问: 你确定看不到过去的时间?兄弟
140
- 三段并列:提瓦特世界是过去、现在、未来同时存在的
183
+ 反问: 你确定看不到过去的时间?兄弟
184
+ 三段并列: 提瓦特世界是过去、现在、未来同时存在的
141
185
  复述后反打:是啊,所以就静悄悄的也忘了
142
- 直接骂: 你放屁,分明是天理提供的
143
- 单字回复:诶嘿 / 草 / 就这?
144
- 接梗: 那维莱特:新鞋,别搞
186
+ 直接骂: 你放屁,分明是天理提供的
187
+ 单字回复: 诶嘿 / 草 / 就这?
188
+ 接梗: 那维莱特:新鞋,别搞
145
189
  ```
146
190
 
147
- 这些都是真人在说真话。68 条零误拦,说明规则没把这些当成套话。
148
-
149
- ## 引用和使用的区别
150
-
151
- 这是这道门最难判的一件事。
152
-
153
- ```text
154
- 「评论区一水儿「我记下了」」 → 在讨论这句话,不是用它 → 放过
155
- 「我记下了」 → 直接用了 → 拦
156
- 「不要只说「受益匪浅」」 → 在批评,不是在使用 → 放过
157
- 「受益匪浅」 → 真在拍马屁 → 拦
158
- 「一定会火+求互关,雷踩得一个不落」 → 在点评别人的套话 → 放过
159
- ```
160
-
161
- 判据:
162
-
163
- - **引号里**的内容不参与判断
164
- - 套话前面有**否定词**(不要/别/避免)→ 在批评
165
- - 套话前面有**指代词**(这句/这个/这条)→ 在指代前文
166
- - 套话后面跟**评价词**(雷/反面教材/课后题)→ 在拿它当例子
191
+ 这些全是真人在说真话。68 条零误拦,说明规则没把这些当成套话。
167
192
 
168
193
  ## 它拦不住什么
169
194
 
@@ -180,7 +205,7 @@ node tools/audit.mjs # 真实场景 25 条
180
205
 
181
206
  300 字以上不跳过——长邮件、长周报、长 PR 一样会八股。
182
207
 
183
- 但长文的门槛不同:同一个信号要出现**两次以上**才算,一次不算。这样长文里偶尔一个「优化了」不会被打回,反复出现才被拦。
208
+ 但长文的门槛不同:同一个信号要出现**两次以上**才算,一次不算。
184
209
 
185
210
  ## 怎么用
186
211
 
@@ -192,8 +217,6 @@ bagu_guard({ text: "登录失败时不再清空已填的手机号" })
192
217
  → "OK"
193
218
  ```
194
219
 
195
- 过了就回 `OK`,两个字。
196
-
197
220
  **三种模式:**
198
221
 
199
222
  ```js
@@ -204,28 +227,25 @@ bagu_guard({ text, mode: 'ask' }) // 只要问句
204
227
 
205
228
  **输出预算:通过 2 字,打回一行 40 字内。** 按严重度排,只报第一条——报三条 agent 会挑最轻的改。
206
229
 
207
- ## 梗库(选装,默认关)
208
-
209
- 插件带一个梗库模块,数据来自 [梗Wiki](https://cnmeme.wiki) 的公开 API。
210
-
211
- **它判的是另一件事**——「这个梗过气了没」「这词什么意思」——跟「谁都能说」不是一回事。
212
- 所以默认关着,要用在设置页开。
230
+ ## 省 token
213
231
 
214
- 开之后多两样:
232
+ | 项 | 大小 |
233
+ |---|---|
234
+ | 系统提示注入 | **31 字** |
235
+ | 工具描述(1 个) | **54 字** |
236
+ | 守门器输出 | 2 字(通过)/40 字内(打回) |
215
237
 
216
- - `bagu_meme({ q })` 查一个词是什么意思
217
- - `bagu_guard` 的结果后面会附一句「梗:XXX」,不改判定
238
+ 固定开销合计 **85 字**。判断全在本地正则里跑完,**不调模型**——
239
+ 除了 AI 接梗那个单独开关。
218
240
 
219
- **守卫器本身不依赖梗库。** 从梗库提炼出来的东西已经写进规则了——
220
- 比如「注意看」「这个男人叫小帅」「话不多说」「一键三连」这些视频开场白,
221
- 现在是 `VIDEO_STOCK` 里的一部分,跟梗库开不开无关。
241
+ ## 短句也能查
222
242
 
223
- 数据文件在 `data/memes.json`,120 条有完整锚点的词条(59KB)。
224
- 全量是 3180 条,但列表接口不返回 keywords,要全得逐条抓详情(3060 次请求),
225
- 那是另一个项目的事。
243
+ 聊天回复二三十字是常态,密度指标在这个长度上分母太小。
244
+ 六十字以下切换成**绝对计数**:不看密度,看命中了什么。
226
245
 
227
- ```sh
228
- node tools/fetch-memes.mjs data/memes.json 20 # 抓梗库
246
+ ```text
247
+ 「这不是技术问题,而是认知问题。」 → 打回
248
+ 「卖卡卖完了随便写了」 → 能发
229
249
  ```
230
250
 
231
251
  ## 装
@@ -238,43 +258,23 @@ dsh plugin --profile web add dsh-bagu
238
258
 
239
259
  ## 设置页
240
260
 
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
- ```
261
+ - **挡不挡** —— 总开关 / 严格模式 / 提示那一节 / 梗库
262
+ - **AI 接梗** —— 开关 + 接口地址 + 模型名 + API Key
263
+ - **试一段** —— 八种样本,当场看能不能过
264
+ - **阈值** —— 留白词下限、结构腔倍数、具体物下限
266
265
 
267
266
  ## 数据从哪来
268
267
 
269
268
  - 两轮对照实验,1169 篇 / 520 万字([lieflat-less-ai-tone](https://github.com/larashero3-dotcom/lieflat-less-ai-tone))
270
269
  - 10 万条真人评论 vs 2180 条 AI 评论的密度实测
271
270
  - [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) 和 [voku/code-slop](https://github.com/voku/agent-skills/blob/main/skills/code-slop/rules/comments-hygiene.md) 的注释判据
271
+ - [beagle style-criteria](https://github.com/existential-birds/beagle/blob/main/plugins/beagle-core/skills/llm-artifacts-detection/references/style-criteria.md)
272
+ - [梗Wiki](https://cnmeme.wiki) 的公开 API(视频开场白套话与梗库)
273
273
 
274
274
  ## 开发
275
275
 
276
276
  ```sh
277
- npm test # 70 个单元测试
277
+ npm test # 89 个单元测试
278
278
  ```
279
279
 
280
280
  ## 许可
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,13 +232,64 @@ 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
 
242
+ // ── AI 接梗(要花钱,单独一块)──────────────
243
+ h('div', null,
244
+ h('div', { 'data-bg-h': '' }, 'AI 接梗'),
245
+ h('div', { 'data-bg-card': '' },
246
+ h(Row, {
247
+ label: view.aiBridge ? 'AI 接梗:开' : 'AI 接梗:关',
248
+ note: '开 = 每条要接梗的消息都调一次模型,拼一句具体的接话。' +
249
+ '要 API key、要付 token —— 养鱼那种要质量的场景值得,日常聊天不值得。',
250
+ }, h(Switch, {
251
+ on: view.aiBridge === true, label: 'AI 接梗',
252
+ disabled: busy === 'save' || !on || view.memes !== true,
253
+ onChange: () => save({ aiBridge: view.aiBridge !== true }),
254
+ })),
255
+
256
+ view.aiBridge ? h('div', { 'data-bg-row': '' },
257
+ h('div', { 'data-bg-grow': '' },
258
+ h('div', { 'data-bg-label': '' }, '接口地址'),
259
+ h('div', { 'data-bg-note': '' }, 'OpenAI 兼容的 /v1 端点。留空就沿用宿主已配的。')),
260
+ h('input', {
261
+ 'data-bg-num': '', 'data-wide': '', type: 'text',
262
+ value: view.aiBaseUrl || '', placeholder: 'https://api.openai.com/v1',
263
+ disabled: busy === 'save',
264
+ onChange: (e) => setView({ ...view, aiBaseUrl: e.target.value }),
265
+ onBlur: () => { if (view.aiBaseUrl !== st.config.aiBaseUrl) save({ aiBaseUrl: view.aiBaseUrl }) },
266
+ })) : null,
267
+
268
+ view.aiBridge ? h('div', { 'data-bg-row': '' },
269
+ h('div', { 'data-bg-grow': '' },
270
+ h('div', { 'data-bg-label': '' }, '模型名'),
271
+ h('div', { 'data-bg-note': '' }, '比如 gpt-4o-mini、deepseek-chat。')),
272
+ h('input', {
273
+ 'data-bg-num': '', 'data-wide': '', type: 'text',
274
+ value: view.aiModel || '', placeholder: 'gpt-4o-mini',
275
+ disabled: busy === 'save',
276
+ onChange: (e) => setView({ ...view, aiModel: e.target.value }),
277
+ onBlur: () => { if (view.aiModel !== st.config.aiModel) save({ aiModel: view.aiModel }) },
278
+ })) : null,
279
+
280
+ view.aiBridge ? h('div', { 'data-bg-row': '' },
281
+ h('div', { 'data-bg-grow': '' },
282
+ h('div', { 'data-bg-label': '' }, 'API Key'),
283
+ h('div', { 'data-bg-note': '' },
284
+ '存在插件目录的 config.json 里,明文。不想存这儿就留空、改用宿主那把。')),
285
+ h('input', {
286
+ 'data-bg-num': '', 'data-wide': '', type: 'password',
287
+ value: view.aiApiKey || '', placeholder: 'sk-...',
288
+ disabled: busy === 'save',
289
+ onChange: (e) => setView({ ...view, aiApiKey: e.target.value }),
290
+ onBlur: () => { if (view.aiApiKey !== st.config.aiApiKey) save({ aiApiKey: view.aiApiKey }) },
291
+ })) : null)),
292
+
241
293
  // ── 试一段 ───────────────────────────
242
294
  h('div', null,
243
295
  h('div', { 'data-bg-h': '' }, '试一段'),
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.7.0",
6
+ "version": "0.8.0",
7
7
  "license": "MIT",
8
- "description": "AI 对外说话前的一道门:聊天、评论、邮件、commit、PR、文档、注释都管。",
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": "所有对外文本发出前过一遍,像八股就打回。只回 OK 或一行改法。"
32
+ "description": "所有对外文本发出前过一遍,像八股就打回。可选接梗与 AI 补充。"
33
33
  }
34
34
  ]
35
35
  }
@@ -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,17 @@ 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
+ /** AI 接梗用的 provider/baseUrl/model/key。留空就沿用宿主已配的那个。 */
39
+ aiBaseUrl: '',
40
+ aiModel: '',
41
+ aiApiKey: '',
31
42
  };
32
43
 
33
44
  export function readConfig() {
package/lib/index.js CHANGED
@@ -14,7 +14,9 @@
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';
18
20
  import { readConfig, writeConfig, CONFIG_FILE, DEFAULTS } from './config.js';
19
21
 
20
22
  export const name = 'dsh-bagu';
@@ -112,22 +114,43 @@ export function apply(root, config = {}) {
112
114
  if (live.memes) {
113
115
  ctx.tools.register({
114
116
  name: 'bagu_meme',
115
- description: '查一个网络梗是什么意思。数据来自 cnmeme.wiki 的公开词条。',
117
+ description: '接梗用:看对方说了什么梗,给出同族能接的。也能查单个词什么意思。',
116
118
  parameters: {
117
119
  type: 'object',
118
120
  properties: {
119
- q: { type: 'string', description: '要查的词。' },
121
+ text: { type: 'string', description: '对方说的话。给了就找能接的梗。' },
122
+ q: { type: 'string', description: '单个词,查它什么意思。' },
120
123
  },
121
- required: ['q'],
122
124
  },
123
125
  output: toolOutput(),
124
126
  execute: async (args) => {
125
127
  const q = String(args?.q ?? '').trim();
126
- if (!q) return { ok: false, text: '没有词。' };
127
- const hits = findMemes(q).filter((h) => h.word === q.toLowerCase() || h.title === q);
128
- if (!hits.length) return { ok: true, text: `没查到「${q}」` };
129
- const m = hits[0];
130
- return { ok: true, text: `${m.title}|${m.slug}` };
128
+ if (q) {
129
+ const rows = lookup(q);
130
+ const entry = rows && rows[0];
131
+ if (!entry) return { ok: true, text: `没查到「${q}」` };
132
+ const body = String(entry.summary || '')
133
+ .replace(/\s+/g, ' ')
134
+ .replace(/^(?:含义|来源|用法|简介|解释)\s*/g, '')
135
+ .slice(0, 100);
136
+ return { ok: true, text: `${entry.title}:${body}` };
137
+ }
138
+ const src = String(args?.text ?? '').trim();
139
+ if (!src) return { ok: false, text: '给 text(对方说的话)或 q(要查的词)。' };
140
+ const list = loadMemes();
141
+ const hint = bridgeHint(src, list);
142
+
143
+ // aiBridge 开着就走模型拼一句具体的接话;失败退回本地提示
144
+ if (live.aiBridge) {
145
+ const r = bridge(src, list);
146
+ if (r.same.length || r.by.length) {
147
+ const line = await aiBridge(src, r, {
148
+ baseUrl: live.aiBaseUrl, model: live.aiModel, apiKey: live.aiApiKey,
149
+ });
150
+ if (line) return { ok: true, text: line };
151
+ }
152
+ }
153
+ return { ok: true, text: hint || '没接的。' };
131
154
  },
132
155
  });
133
156
  }
@@ -173,8 +196,9 @@ export function apply(root, config = {}) {
173
196
  const g = guard(src, { strict: args?.strict ?? live.strict });
174
197
  // 梗库是选装的,默认关。开着就在结果后面附一句,不改判定。
175
198
  if (live.memes) {
176
- const hits = findMemes(src);
177
- if (hits.length) return { ok: true, text: `${g.line}|梗:${hits.slice(0, 3).map((h) => h.title).join('、')}` };
199
+ // 接梗:不是「这段用了哪些梗」,是「顺着这些能接什么」
200
+ const hint = bridgeHint(src, loadMemes());
201
+ if (hint) return { ok: true, text: `${g.line}|${hint}` };
178
202
  }
179
203
  return { ok: true, text: g.line };
180
204
  },
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 || [])].filter(Boolean));
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()].filter((k) => k.length >= 2).sort((a, b) => b.length - a.length);
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)) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-bagu",
3
- "version": "0.7.0",
4
- "description": "AI 对外说话前的一道门。聊天、评论、邮件、commit、PR、文档、注释都管:过了就发,没过就打回。",
3
+ "version": "0.8.0",
4
+ "description": "给 AI 对外说话的一道门:聊天、评论、邮件、commit、PR、文档、注释都管,像八股就打回。可选接梗与 AI 补充。",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "exports": {
@@ -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
+ });