dsh-line-bot 0.10.10 → 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 +4 -0
- package/lib/host.mjs +1 -1
- package/lib/nudge.mjs +82 -14
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
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 優先;兩者都沒有時才用環境變數。
|
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} 次未回覆,暫停到對方下次說話`)
|