修复见解意外启动的问题

This commit is contained in:
cc
2026-04-08 19:32:44 +08:00
parent 33188485b7
commit 5b6117ec28
2 changed files with 60 additions and 6 deletions

View File

@@ -182,7 +182,6 @@ const applyAutoUpdateChannel = (reason: 'startup' | 'settings' = 'startup') => {
autoUpdater.channel = nextUpdaterChannel autoUpdater.channel = nextUpdaterChannel
lastAppliedUpdaterChannel = nextUpdaterChannel lastAppliedUpdaterChannel = nextUpdaterChannel
lastAppliedUpdaterFeedUrl = nextFeedUrl lastAppliedUpdaterFeedUrl = nextFeedUrl
console.log(`[Update](${reason}) 当前版本 ${appVersion},当前轨道: ${currentTrack},渠道偏好: ${track},更新通道: ${autoUpdater.channel}feed=${nextFeedUrl}allowDowngrade=${autoUpdater.allowDowngrade}`)
} }
applyAutoUpdateChannel('startup') applyAutoUpdateChannel('startup')
@@ -1619,6 +1618,7 @@ function registerIpcHandlers() {
applyAutoUpdateChannel('settings') applyAutoUpdateChannel('settings')
} }
void messagePushService.handleConfigChanged(key) void messagePushService.handleConfigChanged(key)
void insightService.handleConfigChanged(key)
return result return result
}) })
@@ -1644,6 +1644,7 @@ function registerIpcHandlers() {
} }
configService?.clear() configService?.clear()
messagePushService.handleConfigCleared() messagePushService.handleConfigCleared()
insightService.handleConfigCleared()
return true return true
}) })

View File

@@ -38,6 +38,13 @@ const API_TIMEOUT_MS = 45_000
/** 沉默天数阈值默认值 */ /** 沉默天数阈值默认值 */
const DEFAULT_SILENCE_DAYS = 3 const DEFAULT_SILENCE_DAYS = 3
const INSIGHT_CONFIG_KEYS = new Set([
'aiInsightEnabled',
'aiInsightScanIntervalHours',
'dbPath',
'decryptKey',
'myWxid'
])
// ─── 类型 ──────────────────────────────────────────────────────────────────── // ─── 类型 ────────────────────────────────────────────────────────────────────
@@ -234,15 +241,57 @@ class InsightService {
start(): void { start(): void {
if (this.started) return if (this.started) return
this.started = true this.started = true
insightLog('INFO', '已启动') void this.refreshConfiguration('startup')
this.scheduleSilenceScan()
} }
stop(): void { stop(): void {
this.started = false this.started = false
this.clearTimers()
this.clearRuntimeCache()
this.processing = false
insightLog('INFO', '已停止')
}
async handleConfigChanged(key: string): Promise<void> {
const normalizedKey = String(key || '').trim()
if (!INSIGHT_CONFIG_KEYS.has(normalizedKey)) return
// 数据库相关配置变更后,丢弃缓存并强制下次重连
if (normalizedKey === 'dbPath' || normalizedKey === 'decryptKey' || normalizedKey === 'myWxid') {
this.clearRuntimeCache()
}
await this.refreshConfiguration(`config:${normalizedKey}`)
}
handleConfigCleared(): void {
this.clearTimers()
this.clearRuntimeCache()
this.processing = false
}
private async refreshConfiguration(_reason: string): Promise<void> {
if (!this.started) return
if (!this.isEnabled()) {
this.clearTimers()
this.clearRuntimeCache()
this.processing = false
return
}
this.scheduleSilenceScan()
}
private clearRuntimeCache(): void {
this.dbConnected = false this.dbConnected = false
this.sessionCache = null this.sessionCache = null
this.sessionCacheAt = 0 this.sessionCacheAt = 0
this.lastActivityAnalysis.clear()
this.lastSeenTimestamp.clear()
this.todayTriggers.clear()
this.todayDate = getStartOfDay()
}
private clearTimers(): void {
if (this.dbDebounceTimer !== null) { if (this.dbDebounceTimer !== null) {
clearTimeout(this.dbDebounceTimer) clearTimeout(this.dbDebounceTimer)
this.dbDebounceTimer = null this.dbDebounceTimer = null
@@ -255,7 +304,6 @@ class InsightService {
clearTimeout(this.silenceInitialDelayTimer) clearTimeout(this.silenceInitialDelayTimer)
this.silenceInitialDelayTimer = null this.silenceInitialDelayTimer = null
} }
insightLog('INFO', '已停止')
} }
/** /**
@@ -452,9 +500,12 @@ class InsightService {
// ── 沉默联系人扫描 ────────────────────────────────────────────────────────── // ── 沉默联系人扫描 ──────────────────────────────────────────────────────────
private scheduleSilenceScan(): void { private scheduleSilenceScan(): void {
this.clearTimers()
if (!this.started || !this.isEnabled()) return
// 等待扫描完成后再安排下一次,避免并发堆积 // 等待扫描完成后再安排下一次,避免并发堆积
const scheduleNext = () => { const scheduleNext = () => {
if (!this.started) return if (!this.started || !this.isEnabled()) return
const intervalHours = (this.config.get('aiInsightScanIntervalHours') as number) || 4 const intervalHours = (this.config.get('aiInsightScanIntervalHours') as number) || 4
const intervalMs = Math.max(0.1, intervalHours) * 60 * 60 * 1000 const intervalMs = Math.max(0.1, intervalHours) * 60 * 60 * 1000
insightLog('INFO', `下次沉默扫描将在 ${intervalHours} 小时后执行`) insightLog('INFO', `下次沉默扫描将在 ${intervalHours} 小时后执行`)
@@ -474,7 +525,6 @@ class InsightService {
private async runSilenceScan(): Promise<void> { private async runSilenceScan(): Promise<void> {
if (!this.isEnabled()) { if (!this.isEnabled()) {
insightLog('INFO', '沉默扫描AI 见解未启用,跳过')
return return
} }
if (this.processing) { if (this.processing) {
@@ -502,6 +552,7 @@ class InsightService {
let silentCount = 0 let silentCount = 0
for (const session of sessions) { for (const session of sessions) {
if (!this.isEnabled()) return
const sessionId = session.username?.trim() || '' const sessionId = session.username?.trim() || ''
if (!sessionId || sessionId.endsWith('@chatroom')) continue if (!sessionId || sessionId.endsWith('@chatroom')) continue
if (sessionId.toLowerCase().includes('placeholder')) continue if (sessionId.toLowerCase().includes('placeholder')) continue
@@ -654,6 +705,7 @@ class InsightService {
}): Promise<void> { }): Promise<void> {
const { sessionId, displayName, triggerReason, silentDays } = params const { sessionId, displayName, triggerReason, silentDays } = params
if (!sessionId) return if (!sessionId) return
if (!this.isEnabled()) return
const apiBaseUrl = this.config.get('aiInsightApiBaseUrl') as string const apiBaseUrl = this.config.get('aiInsightApiBaseUrl') as string
const apiKey = this.config.get('aiInsightApiKey') as string const apiKey = this.config.get('aiInsightApiKey') as string
@@ -747,6 +799,7 @@ class InsightService {
insightLog('INFO', `模型选择跳过 ${displayName}`) insightLog('INFO', `模型选择跳过 ${displayName}`)
return return
} }
if (!this.isEnabled()) return
const insight = result.slice(0, 120) const insight = result.slice(0, 120)
const notifTitle = `见解 · ${displayName}` const notifTitle = `见解 · ${displayName}`