dsh-line-bot 0.10.9 → 0.10.11
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/CHANGELOG.md +9 -0
- package/lib/host.mjs +33 -9
- package/lib/nudge.mjs +82 -14
- package/lib/tieba-sync.mjs +16 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
## 0.10.11
|
|
2
|
+
|
|
3
|
+
- 修正主動傳訊可能重複之前說過的內容:現在會把最近 8 則主動訊息一起給模型參考,送出前再比對一次(開頭、字句重疊),太像就換個說法重寫一次;兩次都重複就跳過這次,不送出重複訊息。
|
|
4
|
+
|
|
5
|
+
## 0.10.10
|
|
6
|
+
|
|
7
|
+
- 相容 dsh-meme 0.1.77:圖庫描述/關鍵詞以簡體為主,現在繁體查詢(例如 `[[meme:貓]]`、`[[meme:傷心]]`)會自動再轉一次簡體查詢。
|
|
8
|
+
- 修正 dsh-persona-dafeiyu 0.8.0 重新載入時偶爾 `init()` 後沒有 fiber、bridge 變 401 的問題:失敗會延遲重試一次。
|
|
9
|
+
|
|
1
10
|
## 0.10.9
|
|
2
11
|
|
|
3
12
|
- 上傳或重設參考音後立即清掉語音偵測快取,面板不會再顯示上一段參考音的秒數。
|
package/lib/host.mjs
CHANGED
|
@@ -51,7 +51,7 @@ const { createVision } = visionMod
|
|
|
51
51
|
const { createTts } = ttsMod
|
|
52
52
|
const { chunkText, clip, inQuietWindow, jsonFail, jsonOk, maskSecret, readJsonBody, safeArray, sleep, readJsonSafe } = utilMod
|
|
53
53
|
|
|
54
|
-
export const VERSION = '0.10.
|
|
54
|
+
export const VERSION = '0.10.11'
|
|
55
55
|
|
|
56
56
|
// 環境變數提供的 Cloudflare API Token(方便用 shell 注入、不用把 token 寫進設定檔)。
|
|
57
57
|
// 設定檔裡的 token 優先;兩者都沒有時才用環境變數。
|
|
@@ -1582,6 +1582,24 @@ export async function create({ ctx, config: pluginConfig, logger: upstream }) {
|
|
|
1582
1582
|
}
|
|
1583
1583
|
}
|
|
1584
1584
|
|
|
1585
|
+
// dsh-meme 的圖庫描述與關鍵詞以簡體為主;模型常照使用者習慣輸出繁體,
|
|
1586
|
+
// 直接拿繁體查會查不到(例如「貓」)。這裡只做常見字的對照,讓查圖多一次機會。
|
|
1587
|
+
const MEME_T2S = Object.fromEntries(
|
|
1588
|
+
('貓猫 傷伤 開开 關关 這这 們们 個个 麼么 樣样 嗎吗 還还 過过 時时 後后 裡里 邊边'
|
|
1589
|
+
+ ' 給给 讓让 覺觉 點点 頭头 發发 現现 聽听 見见 實实 對对 錯错 說说 話话 問问 題题'
|
|
1590
|
+
+ ' 愛爱 會会 來来 買买 賣卖 錢钱 飯饭 魚鱼 龍龙 鳥鸟 豬猪 雞鸡 鴨鸭 車车 機机 腦脑'
|
|
1591
|
+
+ ' 絡络 網网 為为 誰谁 邊边 臉脸 腳脚 體体 氣气 開开 樂乐 傷伤 悲悲 無无 語语'
|
|
1592
|
+
+ ' 醜丑 壞坏 帥帅 專专 業业 夢梦 聲声 響响 電电 視视 書书 讀读 寫写'
|
|
1593
|
+
+ ' 長长 難难 簡简 單单 種种 髮发 鬍胡 鬚须 淚泪 嚇吓 驚惊 訝讶 嗚呜 嚕噜')
|
|
1594
|
+
.split(/\s+/)
|
|
1595
|
+
.filter((pair) => pair.length === 2)
|
|
1596
|
+
.map((pair) => [pair[0], pair[1]]),
|
|
1597
|
+
)
|
|
1598
|
+
|
|
1599
|
+
function memeToSimplified(text) {
|
|
1600
|
+
return Array.from(String(text || '')).map((char) => MEME_T2S[char] || char).join('')
|
|
1601
|
+
}
|
|
1602
|
+
|
|
1585
1603
|
function findMemeChoice(desc, choices = runtime.memeChoices || []) {
|
|
1586
1604
|
const query = String(desc || '').trim()
|
|
1587
1605
|
if (!query || choices.length === 0) return null
|
|
@@ -1623,14 +1641,20 @@ export async function create({ ctx, config: pluginConfig, logger: upstream }) {
|
|
|
1623
1641
|
for (const desc of parsed.memes.slice(0, max)) {
|
|
1624
1642
|
const indexMatch = /^#?(\d{1,3})$/.exec(String(desc).trim())
|
|
1625
1643
|
const index = indexMatch ? Number(indexMatch[1]) - 1 : -1
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1644
|
+
const simplified = memeToSimplified(desc)
|
|
1645
|
+
const queries = simplified && simplified !== desc ? [desc, simplified] : [desc]
|
|
1646
|
+
let file = index >= 0 ? (sample[index]?.path || '') : ''
|
|
1647
|
+
for (const query of queries) {
|
|
1648
|
+
if (file) break
|
|
1649
|
+
file = findMemeChoice(query, sample)?.path || ''
|
|
1650
|
+
if (!file) {
|
|
1651
|
+
const picked = memes.pick({ query })
|
|
1652
|
+
file = picked?.ok && picked.path ? picked.path : ''
|
|
1653
|
+
}
|
|
1654
|
+
if (!file) {
|
|
1655
|
+
const fallback = memes.search({ query, limit: 1 })
|
|
1656
|
+
file = fallback.memes?.[0]?.path || ''
|
|
1657
|
+
}
|
|
1634
1658
|
}
|
|
1635
1659
|
if (!file) {
|
|
1636
1660
|
logger.warn(`找不到表情包:${desc}`)
|
package/lib/nudge.mjs
CHANGED
|
@@ -24,6 +24,10 @@ export function createNudgeScheduler({ getConfig, store, logger, runTurn, sendPr
|
|
|
24
24
|
lastError: typeof n.lastError === 'string' ? n.lastError : '',
|
|
25
25
|
lastReason: typeof n.lastReason === 'string' ? n.lastReason : '',
|
|
26
26
|
nextAt: Number(n.nextAt) || 0,
|
|
27
|
+
recent: (Array.isArray(n.recent) ? n.recent : [])
|
|
28
|
+
.map((item) => ({ text: String(item?.text || '').trim(), at: Number(item?.at) || 0 }))
|
|
29
|
+
.filter((item) => item.text)
|
|
30
|
+
.slice(-8),
|
|
27
31
|
}
|
|
28
32
|
}
|
|
29
33
|
|
|
@@ -160,6 +164,52 @@ export function createNudgeScheduler({ getConfig, store, logger, runTurn, sendPr
|
|
|
160
164
|
return candidate[0].userId
|
|
161
165
|
}
|
|
162
166
|
|
|
167
|
+
function compactForCompare(text) {
|
|
168
|
+
return String(text || '')
|
|
169
|
+
.replace(/\[\[[^\]]*\]\]/g, '')
|
|
170
|
+
.replace(/[\s\p{P}\p{S}]/gu, '')
|
|
171
|
+
.slice(0, 240)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function bigrams(text) {
|
|
175
|
+
const set = new Set()
|
|
176
|
+
for (let i = 0; i < text.length - 1; i += 1) set.add(text.slice(i, i + 2))
|
|
177
|
+
return set
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function looksRepeated(text, recent) {
|
|
181
|
+
const a = compactForCompare(text)
|
|
182
|
+
if (!a) return false
|
|
183
|
+
for (const item of recent) {
|
|
184
|
+
const b = compactForCompare(item.text)
|
|
185
|
+
if (!b) continue
|
|
186
|
+
if (a === b) return true
|
|
187
|
+
// 同一句話的開頭(常見的固定招呼語)也算重複。
|
|
188
|
+
const head = Math.min(8, a.length, b.length)
|
|
189
|
+
if (head >= 6 && a.slice(0, head) === b.slice(0, head)) return true
|
|
190
|
+
const left = bigrams(a)
|
|
191
|
+
const right = bigrams(b)
|
|
192
|
+
if (left.size === 0 || right.size === 0) continue
|
|
193
|
+
let hit = 0
|
|
194
|
+
for (const gram of left) if (right.has(gram)) hit += 1
|
|
195
|
+
const similarity = hit / Math.min(left.size, right.size)
|
|
196
|
+
if (a.length >= 10 && similarity >= 0.55) return true
|
|
197
|
+
if (a.length < 10 && similarity >= 0.8) return true
|
|
198
|
+
}
|
|
199
|
+
return false
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function recentPrompt(recent) {
|
|
203
|
+
if (recent.length === 0) return ''
|
|
204
|
+
return [
|
|
205
|
+
'',
|
|
206
|
+
'【你最近主動傳過的訊息】',
|
|
207
|
+
...recent.map((item, index) => `${index + 1}. ${item.text.replace(/\s+/g, ' ').slice(0, 100)}`),
|
|
208
|
+
'',
|
|
209
|
+
'這次請換一個話題或角度,不要重複上面的句子、開頭或意思。',
|
|
210
|
+
].join('\n')
|
|
211
|
+
}
|
|
212
|
+
|
|
163
213
|
async function tick({ force = false } = {}) {
|
|
164
214
|
if (ticking) return { skipped: 'busy' }
|
|
165
215
|
ticking = true
|
|
@@ -184,22 +234,39 @@ export function createNudgeScheduler({ getConfig, store, logger, runTurn, sendPr
|
|
|
184
234
|
return { skipped: 'not-due', nextAt: nextDueAt(cfg), ...c }
|
|
185
235
|
}
|
|
186
236
|
const user = store.getState().users[userId]
|
|
187
|
-
const
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
237
|
+
const recent = nudgeState().recent
|
|
238
|
+
const basePrompt = (cfg.nudge.prompt || '').trim() || DEFAULT_PROMPT
|
|
239
|
+
let text = ''
|
|
240
|
+
let result = null
|
|
241
|
+
let repeated = false
|
|
242
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
243
|
+
const prompt = attempt === 0
|
|
244
|
+
? basePrompt + recentPrompt(recent)
|
|
245
|
+
: `${basePrompt}${recentPrompt(recent)}\n\n你剛才的草稿是:「${text.slice(0, 80)}」,這和之前說過的話太像了。請換一個完全不同的話題或角度。`
|
|
246
|
+
try {
|
|
247
|
+
result = await runTurn({ sessionId: user.sessionId, text: prompt, nudge: true })
|
|
248
|
+
} catch (error) {
|
|
249
|
+
setUserSchedule(userId, { nudgeNextAt: Date.now() + 5 * 60000 })
|
|
250
|
+
saveNudge({ lastError: error?.message || String(error), lastReason: 'error', nextAt: nextDueAt(cfg) })
|
|
251
|
+
store.updateState((state) => ({ ...state, lastError: error?.message || String(error) }))
|
|
252
|
+
logger?.error?.(`主動傳訊失敗:${error?.message || error}`)
|
|
253
|
+
return { skipped: 'agent-error', error: error?.message || String(error) }
|
|
254
|
+
}
|
|
255
|
+
text = String(result?.text || '').trim()
|
|
256
|
+
if (!text || /^SKIP\b/i.test(text)) {
|
|
257
|
+
setUserSchedule(userId, { nudgeNextAt: Date.now() + randomIntervalMs(cfg) })
|
|
258
|
+
saveNudge({ lastReason: 'skip', nextAt: nextDueAt(cfg) })
|
|
259
|
+
return { skipped: 'skip', ...c }
|
|
260
|
+
}
|
|
261
|
+
repeated = looksRepeated(text, recent)
|
|
262
|
+
if (!repeated) break
|
|
263
|
+
logger?.warn?.(`主動訊息和最近 ${recent.length} 則內容太像,換一個說法(第 ${attempt + 1} 次)`)
|
|
197
264
|
}
|
|
198
|
-
|
|
199
|
-
if (!text || /^SKIP\b/i.test(text)) {
|
|
265
|
+
if (repeated) {
|
|
200
266
|
setUserSchedule(userId, { nudgeNextAt: Date.now() + randomIntervalMs(cfg) })
|
|
201
|
-
saveNudge({ lastReason: '
|
|
202
|
-
|
|
267
|
+
saveNudge({ lastReason: 'repeat', nextAt: nextDueAt(cfg) })
|
|
268
|
+
logger?.warn?.(`主動訊息連續兩次和最近內容重複,這次先跳過`)
|
|
269
|
+
return { skipped: 'repeat', ...c }
|
|
203
270
|
}
|
|
204
271
|
try {
|
|
205
272
|
await sendProactive(userId, text)
|
|
@@ -233,6 +300,7 @@ export function createNudgeScheduler({ getConfig, store, logger, runTurn, sendPr
|
|
|
233
300
|
lastAt: stamp,
|
|
234
301
|
lastReason: force ? 'test' : 'sent',
|
|
235
302
|
lastError: '',
|
|
303
|
+
recent: [...recent, { text, at: stamp }].slice(-8),
|
|
236
304
|
nextAt: nextDueAt(cfg),
|
|
237
305
|
})
|
|
238
306
|
if (paused) logger?.info?.(`主動傳訊:${userId} 已連續 ${unanswered} 次未回覆,暫停到對方下次說話`)
|
package/lib/tieba-sync.mjs
CHANGED
|
@@ -121,11 +121,25 @@ export async function reloadPersonaPlugin(ctx) {
|
|
|
121
121
|
|| String(item.options?.name || '').includes('persona-dafeiyu'))
|
|
122
122
|
if (!entry) return { ok: false, error: '找不到 persona-dafeiyu 外掛 entry' }
|
|
123
123
|
const uidBefore = entry.fiber?.uid ?? null
|
|
124
|
+
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
124
125
|
await entry.fiber?.dispose?.()
|
|
125
126
|
entry.fiber = undefined
|
|
127
|
+
// dsh-persona-dafeiyu 0.8.0 在 dispose 後第一次 init 偶爾不會生成 fiber,
|
|
128
|
+
// 這時 bridge 會變 401;等一下再 init 一次通常就會恢復。
|
|
126
129
|
await entry.init()
|
|
127
|
-
await
|
|
128
|
-
|
|
130
|
+
await wait(700)
|
|
131
|
+
let uidAfter = entry.fiber?.uid ?? null
|
|
132
|
+
if (uidAfter == null) {
|
|
133
|
+
await wait(900)
|
|
134
|
+
uidAfter = entry.fiber?.uid ?? null
|
|
135
|
+
}
|
|
136
|
+
if (uidAfter == null) {
|
|
137
|
+
await entry.init()
|
|
138
|
+
await wait(700)
|
|
139
|
+
uidAfter = entry.fiber?.uid ?? null
|
|
140
|
+
}
|
|
141
|
+
if (uidAfter == null) return { ok: false, uidBefore, uidAfter, error: '重新 init 後仍沒有 fiber' }
|
|
142
|
+
return { ok: true, uidBefore, uidAfter }
|
|
129
143
|
}
|
|
130
144
|
|
|
131
145
|
export function personaOverlayPath(personaId) {
|