dsh-bagu 0.8.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 +23 -7
- package/client.js +24 -68
- package/dsh-plugin.json +3 -3
- package/lib/config.js +3 -1
- package/lib/index.js +22 -5
- package/lib/session-bridge.js +90 -0
- package/package.json +2 -2
- package/test/session-bridge.test.mjs +74 -0
package/README.md
CHANGED
|
@@ -133,21 +133,37 @@ bagu_meme({ q: "社死" })
|
|
|
133
133
|
「福本伸行」→ 超智游戏 / 斗牌传说 / 末世路
|
|
134
134
|
```
|
|
135
135
|
|
|
136
|
-
### AI
|
|
136
|
+
### AI 接梗:默认用会话自己的模型,不用填 key
|
|
137
137
|
|
|
138
|
-
梗库只给族,不给具体那句话。要让模型拼一句,开 `aiBridge
|
|
138
|
+
梗库只给族,不给具体那句话。要让模型拼一句,开 `aiBridge`。
|
|
139
|
+
|
|
140
|
+
**默认走 DSH 会话自己的模型。**插件拿到当前会话的 provider/model,用宿主已经配好的连接,
|
|
141
|
+
**不用填 API key、不用填接口地址**——跟这个会话本身用的是同一个模型、同一份额度。
|
|
139
142
|
|
|
140
143
|
```text
|
|
141
144
|
对方说:这不就是瞎问虾猜那个系列吗
|
|
142
145
|
这段话涉及这些梗:瞎问虾猜
|
|
143
146
|
同族还可以提到:反向空耳、握着我的抱枕
|
|
144
|
-
→
|
|
147
|
+
→ 会话模型拼一句 25 字以内的接话
|
|
145
148
|
```
|
|
146
149
|
|
|
147
|
-
|
|
148
|
-
|
|
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 接梗开 | 每条一次会话模型调用 |
|
|
149
164
|
|
|
150
|
-
|
|
165
|
+
AI 接梗走会话模型,所以**不需要额外的 key**——用的是这个会话本身那一份额度。
|
|
166
|
+
另配接口那条路才要自己填 key。
|
|
151
167
|
|
|
152
168
|
## 八组数据
|
|
153
169
|
|
|
@@ -259,7 +275,7 @@ dsh plugin --profile web add dsh-bagu
|
|
|
259
275
|
## 设置页
|
|
260
276
|
|
|
261
277
|
- **挡不挡** —— 总开关 / 严格模式 / 提示那一节 / 梗库
|
|
262
|
-
- **AI 接梗** —— 开关 +
|
|
278
|
+
- **AI 接梗** —— 开关 + 显示当前会话的模型 + 另配接口(兜底)
|
|
263
279
|
- **试一段** —— 八种样本,当场看能不能过
|
|
264
280
|
- **阈值** —— 留白词下限、结构腔倍数、具体物下限
|
|
265
281
|
|
package/client.js
CHANGED
|
@@ -239,24 +239,40 @@ window.__ModuleLoader__.load({
|
|
|
239
239
|
onChange: () => save({ memes: view.memes !== true }),
|
|
240
240
|
})))),
|
|
241
241
|
|
|
242
|
-
// ── AI
|
|
242
|
+
// ── AI 接梗(默认走会话模型,不用填 key)──────────
|
|
243
243
|
h('div', null,
|
|
244
244
|
h('div', { 'data-bg-h': '' }, 'AI 接梗'),
|
|
245
245
|
h('div', { 'data-bg-card': '' },
|
|
246
246
|
h(Row, {
|
|
247
247
|
label: view.aiBridge ? 'AI 接梗:开' : 'AI 接梗:关',
|
|
248
|
-
note: '开 =
|
|
249
|
-
'要 API key、要付 token —— 养鱼那种要质量的场景值得,日常聊天不值得。',
|
|
248
|
+
note: '开 = 每条要接梗的消息都调一次模型,拼一句具体的接话。默认走 DSH 会话自己的模型,不用填 key;失败才退回本地族提示。',
|
|
250
249
|
}, h(Switch, {
|
|
251
250
|
on: view.aiBridge === true, label: 'AI 接梗',
|
|
252
251
|
disabled: busy === 'save' || !on || view.memes !== true,
|
|
253
252
|
onChange: () => save({ aiBridge: view.aiBridge !== true }),
|
|
254
253
|
})),
|
|
255
254
|
|
|
256
|
-
|
|
255
|
+
h(Row, {
|
|
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
|
+
|
|
263
|
+
h(Row, {
|
|
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': '' },
|
|
257
273
|
h('div', { 'data-bg-grow': '' },
|
|
258
274
|
h('div', { 'data-bg-label': '' }, '接口地址'),
|
|
259
|
-
h('div', { 'data-bg-note': '' }, 'OpenAI 兼容的 /v1
|
|
275
|
+
h('div', { 'data-bg-note': '' }, 'OpenAI 兼容的 /v1 端点。')),
|
|
260
276
|
h('input', {
|
|
261
277
|
'data-bg-num': '', 'data-wide': '', type: 'text',
|
|
262
278
|
value: view.aiBaseUrl || '', placeholder: 'https://api.openai.com/v1',
|
|
@@ -265,7 +281,7 @@ window.__ModuleLoader__.load({
|
|
|
265
281
|
onBlur: () => { if (view.aiBaseUrl !== st.config.aiBaseUrl) save({ aiBaseUrl: view.aiBaseUrl }) },
|
|
266
282
|
})) : null,
|
|
267
283
|
|
|
268
|
-
view.
|
|
284
|
+
view.aiUseOwn ? h('div', { 'data-bg-row': '' },
|
|
269
285
|
h('div', { 'data-bg-grow': '' },
|
|
270
286
|
h('div', { 'data-bg-label': '' }, '模型名'),
|
|
271
287
|
h('div', { 'data-bg-note': '' }, '比如 gpt-4o-mini、deepseek-chat。')),
|
|
@@ -277,11 +293,10 @@ window.__ModuleLoader__.load({
|
|
|
277
293
|
onBlur: () => { if (view.aiModel !== st.config.aiModel) save({ aiModel: view.aiModel }) },
|
|
278
294
|
})) : null,
|
|
279
295
|
|
|
280
|
-
view.
|
|
296
|
+
view.aiUseOwn ? h('div', { 'data-bg-row': '' },
|
|
281
297
|
h('div', { 'data-bg-grow': '' },
|
|
282
298
|
h('div', { 'data-bg-label': '' }, 'API Key'),
|
|
283
|
-
h('div', { 'data-bg-note': '' },
|
|
284
|
-
'存在插件目录的 config.json 里,明文。不想存这儿就留空、改用宿主那把。')),
|
|
299
|
+
h('div', { 'data-bg-note': '' }, '明文存在插件目录的 config.json。')),
|
|
285
300
|
h('input', {
|
|
286
301
|
'data-bg-num': '', 'data-wide': '', type: 'password',
|
|
287
302
|
value: view.aiApiKey || '', placeholder: 'sk-...',
|
|
@@ -290,65 +305,6 @@ window.__ModuleLoader__.load({
|
|
|
290
305
|
onBlur: () => { if (view.aiApiKey !== st.config.aiApiKey) save({ aiApiKey: view.aiApiKey }) },
|
|
291
306
|
})) : null)),
|
|
292
307
|
|
|
293
|
-
// ── 试一段 ───────────────────────────
|
|
294
|
-
h('div', null,
|
|
295
|
-
h('div', { 'data-bg-h': '' }, '试一段'),
|
|
296
|
-
h('div', { 'data-bg-card': '' },
|
|
297
|
-
h('div', { 'data-bg-chips': '' },
|
|
298
|
-
SAMPLES.map((s, i) => h('button', {
|
|
299
|
-
key: i, 'data-bg-chip': '',
|
|
300
|
-
onClick: () => { setText(s.text); setVerdict(null) },
|
|
301
|
-
}, s.label))),
|
|
302
|
-
h('textarea', {
|
|
303
|
-
'data-bg-in': '', value: text, rows: 3,
|
|
304
|
-
placeholder: '粘一段准备发出去的文字。聊天、commit、邮件都行。',
|
|
305
|
-
onChange: (e) => { setText(e.target.value); setVerdict(null) },
|
|
306
|
-
}),
|
|
307
|
-
h('div', { 'data-bg-row': '' },
|
|
308
|
-
h('button', {
|
|
309
|
-
'data-bg-btn': '', 'data-primary': '',
|
|
310
|
-
disabled: busy === 'check', onClick: check,
|
|
311
|
-
}, busy === 'check' ? '过着…' : '过一遍'),
|
|
312
|
-
h('div', { 'data-bg-grow': '' },
|
|
313
|
-
h('div', { 'data-bg-note': '' }, verdict
|
|
314
|
-
? (verdict.pass ? '能发' : '打回')
|
|
315
|
-
: '看这段能不能发出去'))),
|
|
316
|
-
verdict ? h('div', {
|
|
317
|
-
'data-bg-verdict': '',
|
|
318
|
-
'data-pass': verdict.pass ? '' : undefined,
|
|
319
|
-
'data-block': verdict.pass ? undefined : '',
|
|
320
|
-
},
|
|
321
|
-
h('div', { 'data-bg-dot': '' }),
|
|
322
|
-
h('div', { 'data-bg-verdict-text': '' }, verdict.line),
|
|
323
|
-
) : null)),
|
|
324
|
-
|
|
325
|
-
// ── 阈值 ─────────────────────────────
|
|
326
|
-
h('div', null,
|
|
327
|
-
h('div', { 'data-bg-h': '' }, '阈值'),
|
|
328
|
-
h('div', { 'data-bg-card': '' },
|
|
329
|
-
h(Row, {
|
|
330
|
-
label: '留白词下限',
|
|
331
|
-
note: '每千字几个「好像 / 应该 / 大概」。低于这个就报句句在下断言。',
|
|
332
|
-
}, num('hedgeFloor')),
|
|
333
|
-
h(Row, {
|
|
334
|
-
label: '结构腔倍数',
|
|
335
|
-
note: '超过同文体基线的几倍才报。调大更宽容。',
|
|
336
|
-
}, num('structureTimes')),
|
|
337
|
-
h(Row, {
|
|
338
|
-
label: '具体物下限',
|
|
339
|
-
note: '每百字至少几个数字、时间、经历、原话、名字。',
|
|
340
|
-
}, num('concreteFloor')),
|
|
341
|
-
h('div', { 'data-bg-row': '' },
|
|
342
|
-
h('div', { 'data-bg-grow': '' }, h('div', { 'data-bg-note': '' }, '恢复默认。')),
|
|
343
|
-
h('button', {
|
|
344
|
-
'data-bg-btn': '', disabled: busy === 'save',
|
|
345
|
-
onClick: () => save({
|
|
346
|
-
hedgeFloor: st.defaults.hedgeFloor,
|
|
347
|
-
structureTimes: st.defaults.structureTimes,
|
|
348
|
-
concreteFloor: st.defaults.concreteFloor,
|
|
349
|
-
}),
|
|
350
|
-
}, '恢复默认')))),
|
|
351
|
-
|
|
352
308
|
msg ? h('div', {
|
|
353
309
|
'data-bg-msg': '', 'data-err': msg.err ? '' : undefined, 'data-ok': msg.ok ? '' : undefined,
|
|
354
310
|
}, msg.text) : null,
|
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/config.js
CHANGED
|
@@ -35,7 +35,9 @@ export const DEFAULTS = {
|
|
|
35
35
|
* 养鱼那种要质量的场景可以开;日常聊天不值得。
|
|
36
36
|
*/
|
|
37
37
|
aiBridge: false,
|
|
38
|
-
/**
|
|
38
|
+
/** 另配接口开关:默认走会话模型,这个开着才用下面三项 */
|
|
39
|
+
aiUseOwn: false,
|
|
40
|
+
/** AI 接梗用的 baseUrl/model/key。aiUseOwn 关着时不看这些。 */
|
|
39
41
|
aiBaseUrl: '',
|
|
40
42
|
aiModel: '',
|
|
41
43
|
aiApiKey: '',
|
package/lib/index.js
CHANGED
|
@@ -17,6 +17,7 @@ import { guard } from './guard.js';
|
|
|
17
17
|
import { findMemes, lookup, loadMemes } from './memes.js';
|
|
18
18
|
import { bridge, bridgeHint } from './bridge.js';
|
|
19
19
|
import { aiBridge } from './ai-bridge.js';
|
|
20
|
+
import { captureRoute, sessionBridge } from './session-bridge.js';
|
|
20
21
|
import { readConfig, writeConfig, CONFIG_FILE, DEFAULTS } from './config.js';
|
|
21
22
|
|
|
22
23
|
export const name = 'dsh-bagu';
|
|
@@ -109,6 +110,12 @@ export function apply(root, config = {}) {
|
|
|
109
110
|
}, 'dsh-bagu: 说话的念头');
|
|
110
111
|
});
|
|
111
112
|
|
|
113
|
+
/** 会话自己的路由(provider/model),从 agent/request 抓。抓不到就 null。 */
|
|
114
|
+
let getRoute = () => null;
|
|
115
|
+
root.inject(['llm'], (ctx) => {
|
|
116
|
+
getRoute = captureRoute(ctx);
|
|
117
|
+
});
|
|
118
|
+
|
|
112
119
|
root.inject(['tools'], (ctx) => {
|
|
113
120
|
// 一个工具,三种模式。mode 缺省是守门(日常路径),另外两个是调试用。
|
|
114
121
|
if (live.memes) {
|
|
@@ -140,14 +147,23 @@ export function apply(root, config = {}) {
|
|
|
140
147
|
const list = loadMemes();
|
|
141
148
|
const hint = bridgeHint(src, list);
|
|
142
149
|
|
|
143
|
-
// aiBridge
|
|
150
|
+
// aiBridge 开着就走模型拼一句。两条路,先试会话自己的,再试自己配的 key。
|
|
144
151
|
if (live.aiBridge) {
|
|
145
152
|
const r = bridge(src, list);
|
|
146
153
|
if (r.same.length || r.by.length) {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
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
|
+
}
|
|
151
167
|
}
|
|
152
168
|
}
|
|
153
169
|
return { ok: true, text: hint || '没接的。' };
|
|
@@ -216,6 +232,7 @@ export function apply(root, config = {}) {
|
|
|
216
232
|
config: live,
|
|
217
233
|
defaults: DEFAULTS,
|
|
218
234
|
promptActive: live.enabled && live.prompt,
|
|
235
|
+
route: getRoute(),
|
|
219
236
|
configFile: CONFIG_FILE,
|
|
220
237
|
});
|
|
221
238
|
}
|
|
@@ -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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-bagu",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "给 AI
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"description": "给 AI 对外说话的一道门:像八股就打回。接梗走会话自己的模型,不用填 key。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"exports": {
|
|
@@ -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
|
+
});
|