feat: Add Chat Analysis

This commit is contained in:
Jason
2026-05-17 21:04:14 +08:00
parent ca6c479496
commit 1df4f0e523
14 changed files with 1493 additions and 24 deletions

View File

@@ -1792,6 +1792,7 @@ function registerIpcHandlers() {
sessionId?: string
startTime?: number
endTime?: number
sourceType?: 'insight' | 'message_analysis' | 'all'
limit?: number
offset?: number
}) => {
@@ -1834,6 +1835,21 @@ function registerIpcHandlers() {
return insightService.generateFootprintInsight(payload)
})
ipcMain.handle('insight:generateMessageInsight', async (_, payload: {
sessionId: string
displayName?: string
avatarUrl?: string
targetLocalId?: number
targetCreateTime?: number
targetMessageKey?: string
targetText: string
targetSenderName?: string
contextCount?: number
forceRefresh?: boolean
}) => {
return insightService.generateMessageInsight(payload)
})
ipcMain.handle('social:saveWeiboCookie', async (_, rawInput: string) => {
try {
if (!configService) {

View File

@@ -595,7 +595,19 @@ contextBridge.exposeInMainWorld('electronAPI', {
}
privateSegments?: Array<{ displayName?: string; session_id?: string; incoming_count?: number; outgoing_count?: number; message_count?: number; replied?: boolean }>
mentionGroups?: Array<{ displayName?: string; session_id?: string; count?: number }>
}) => ipcRenderer.invoke('insight:generateFootprintInsight', payload)
}) => ipcRenderer.invoke('insight:generateFootprintInsight', payload),
generateMessageInsight: (payload: {
sessionId: string
displayName?: string
avatarUrl?: string
targetLocalId?: number
targetCreateTime?: number
targetMessageKey?: string
targetText: string
targetSenderName?: string
contextCount?: number
forceRefresh?: boolean
}) => ipcRenderer.invoke('insight:generateMessageInsight', payload)
},
social: {

View File

@@ -2585,6 +2585,93 @@ class ChatService {
}
}
async getMessagesAround(
sessionId: string,
target: { localId?: number; createTime: number; messageKey?: string },
totalContextCount: number = 50
): Promise<{
success: boolean
before: Message[]
after: Message[]
requested: number
error?: string
}> {
const requested = Math.max(1, Math.min(200, Math.floor(Number(totalContextCount) || 50)))
const targetCreateTime = Math.floor(Number(target?.createTime || 0))
if (!sessionId || targetCreateTime <= 0) {
return { success: false, before: [], after: [], requested, error: '无效的目标消息' }
}
const collect = async (ascending: boolean): Promise<Message[]> => {
let cursor: number | undefined
try {
const cursorResult = await wcdbService.openMessageCursorLite(
sessionId,
Math.min(240, Math.max(60, requested + 20)),
ascending,
ascending ? targetCreateTime : 0,
ascending ? 0 : targetCreateTime + 1
)
if (!cursorResult.success || !cursorResult.cursor) {
throw new Error(cursorResult.error || '打开消息游标失败')
}
cursor = cursorResult.cursor
const collected = await this.collectVisibleMessagesFromCursor(sessionId, cursor, requested + 1)
if (!collected.success) {
throw new Error(collected.error || '读取上下文消息失败')
}
const targetLocalId = Math.floor(Number(target?.localId || 0))
const targetMessageKey = String(target?.messageKey || '').trim()
return (collected.messages || []).filter((message) => {
const sameLocalId = targetLocalId > 0 && Number(message.localId || 0) === targetLocalId
const sameCreateTime = Number(message.createTime || 0) === targetCreateTime
const sameKey = Boolean(targetMessageKey && message.messageKey === targetMessageKey)
return !(sameKey || (sameLocalId && sameCreateTime))
})
} finally {
if (cursor) {
await wcdbService.closeMessageCursor(cursor).catch(() => {})
}
}
}
try {
const [beforeCandidatesRaw, afterCandidatesRaw] = await Promise.all([
collect(false),
collect(true)
])
const beforeCandidates = beforeCandidatesRaw
.filter((message) => Number(message.createTime || 0) <= targetCreateTime)
.sort((a, b) => (a.createTime - b.createTime) || (a.sortSeq - b.sortSeq))
const afterCandidates = afterCandidatesRaw
.filter((message) => Number(message.createTime || 0) >= targetCreateTime)
.sort((a, b) => (a.createTime - b.createTime) || (a.sortSeq - b.sortSeq))
const baseBefore = Math.floor(requested / 2)
const baseAfter = requested - baseBefore
const takeAfter = Math.min(baseAfter, afterCandidates.length)
const takeBefore = Math.min(requested - takeAfter, beforeCandidates.length)
const remainingAfter = Math.max(0, requested - takeBefore - takeAfter)
const finalAfter = Math.min(afterCandidates.length, takeAfter + remainingAfter)
const finalBefore = Math.min(beforeCandidates.length, requested - finalAfter)
return {
success: true,
before: beforeCandidates.slice(Math.max(0, beforeCandidates.length - finalBefore)),
after: afterCandidates.slice(0, finalAfter),
requested
}
} catch (error) {
return {
success: false,
before: [],
after: [],
requested,
error: (error as Error).message || String(error)
}
}
}
async getNewMessages(sessionId: string, minTime: number, limit: number = this.messageBatchDefault): Promise<{ success: boolean; messages?: Message[]; error?: string }> {
try {
const connectResult = await this.ensureConnected()

View File

@@ -129,6 +129,9 @@ interface ConfigSchema {
// AI 足迹
aiFootprintEnabled: boolean
aiFootprintSystemPrompt: string
aiMessageInsightEnabled: boolean
aiMessageInsightContextCount: number
aiMessageInsightSystemPrompt: string
/** 是否将 AI 见解调试日志输出到桌面 */
aiInsightDebugLogEnabled: boolean
autoDownloadHighRes: boolean
@@ -252,6 +255,9 @@ export class ConfigService {
aiInsightWeiboBindings: {},
aiFootprintEnabled: false,
aiFootprintSystemPrompt: '',
aiMessageInsightEnabled: false,
aiMessageInsightContextCount: 50,
aiMessageInsightSystemPrompt: '',
aiInsightDebugLogEnabled: false,
autoDownloadHighRes: false,
autoDownloadWhitelist: []

View File

@@ -4,7 +4,24 @@ import path from 'path'
import { createHash, randomUUID } from 'crypto'
import { ConfigService } from './config'
export type InsightRecordTriggerReason = 'activity' | 'silence' | 'test'
export type InsightRecordTriggerReason = 'activity' | 'silence' | 'test' | 'message_analysis'
export type InsightRecordSourceType = 'insight' | 'message_analysis'
export interface MessageInsightAnalysis {
explicitText: string
emotion: string
intent: string
topic: string
}
export interface MessageInsightTarget {
targetLocalId: number
targetCreateTime: number
targetMessageKey: string
targetSenderName: string
targetTextPreview: string
analysis: MessageInsightAnalysis
}
export interface InsightRecordLog {
endpoint: string
@@ -20,11 +37,29 @@ export interface InsightRecordLog {
finalInsight: string
durationMs: number
createdAt: number
responseFormatJson?: boolean
responseFormatFallback?: boolean
responseFormatFallbackReason?: string
targetMessage?: {
localId: number
createTime: number
messageKey: string
senderName: string
textPreview: string
}
contextStats?: {
requested: number
beforeTarget: number
afterTarget: number
readError?: string
}
parsedAnalysis?: MessageInsightAnalysis
}
export interface InsightRecord {
id: string
accountScope: string
sourceType: InsightRecordSourceType
createdAt: number
sessionId: string
displayName: string
@@ -32,11 +67,13 @@ export interface InsightRecord {
triggerReason: InsightRecordTriggerReason
insight: string
read: boolean
messageInsight?: MessageInsightTarget
log: InsightRecordLog
}
export interface InsightRecordSummary {
id: string
sourceType: InsightRecordSourceType
createdAt: number
sessionId: string
displayName: string
@@ -44,6 +81,7 @@ export interface InsightRecordSummary {
triggerReason: InsightRecordTriggerReason
insight: string
read: boolean
messageInsight?: MessageInsightTarget
}
export interface InsightRecordContactFacet {
@@ -58,6 +96,7 @@ export interface InsightRecordFilters {
sessionId?: string
startTime?: number
endTime?: number
sourceType?: InsightRecordSourceType | 'all'
limit?: number
offset?: number
}
@@ -136,13 +175,15 @@ class InsightRecordService {
private toSummary(record: InsightRecord): InsightRecordSummary {
return {
id: record.id,
sourceType: record.sourceType || 'insight',
createdAt: record.createdAt,
sessionId: record.sessionId,
displayName: record.displayName,
avatarUrl: record.avatarUrl,
triggerReason: record.triggerReason,
insight: record.insight,
read: record.read
read: record.read,
messageInsight: record.messageInsight
}
}
@@ -156,8 +197,10 @@ class InsightRecordService {
sessionId: string
displayName: string
avatarUrl?: string
sourceType?: InsightRecordSourceType
triggerReason: InsightRecordTriggerReason
insight: string
messageInsight?: MessageInsightTarget
log: InsightRecordLog
}): InsightRecord {
this.ensureLoaded()
@@ -166,6 +209,7 @@ class InsightRecordService {
const record: InsightRecord = {
id: randomUUID(),
accountScope: scope,
sourceType: input.sourceType || 'insight',
createdAt: now,
sessionId: input.sessionId,
displayName: input.displayName,
@@ -173,6 +217,7 @@ class InsightRecordService {
triggerReason: input.triggerReason,
insight: input.insight,
read: false,
messageInsight: input.messageInsight,
log: input.log
}
@@ -207,6 +252,7 @@ class InsightRecordService {
const keyword = String(filters.keyword || '').trim().toLowerCase()
const sessionId = String(filters.sessionId || '').trim()
const sourceType = String(filters.sourceType || 'all').trim()
const startTime = Number(filters.startTime || 0)
const endTime = Number(filters.endTime || 0)
const offset = Math.max(0, Math.floor(Number(filters.offset || 0)))
@@ -215,10 +261,22 @@ class InsightRecordService {
const filtered = allScoped
.filter((record) => {
if (sessionId && record.sessionId !== sessionId) return false
const recordSourceType = record.sourceType || 'insight'
if (sourceType !== 'all' && sourceType && recordSourceType !== sourceType) return false
if (startTime > 0 && record.createdAt < startTime) return false
if (endTime > 0 && record.createdAt > endTime) return false
if (keyword) {
const haystack = `${record.displayName}\n${record.sessionId}\n${record.insight}`.toLowerCase()
const haystack = [
record.displayName,
record.sessionId,
record.insight,
record.messageInsight?.targetSenderName,
record.messageInsight?.targetTextPreview,
record.messageInsight?.analysis?.explicitText,
record.messageInsight?.analysis?.emotion,
record.messageInsight?.analysis?.intent,
record.messageInsight?.analysis?.topic
].join('\n').toLowerCase()
if (!haystack.includes(keyword)) return false
}
return true
@@ -256,6 +314,36 @@ class InsightRecordService {
return { success: true, record }
}
findLatestMessageAnalysis(input: {
sessionId: string
targetLocalId?: number
targetCreateTime?: number
targetMessageKey?: string
}): InsightRecord | null {
this.ensureLoaded()
const scope = this.getCurrentAccountScope()
const sessionId = String(input.sessionId || '').trim()
if (!sessionId) return null
const targetLocalId = Math.floor(Number(input.targetLocalId || 0))
const targetCreateTime = Math.floor(Number(input.targetCreateTime || 0))
const targetMessageKey = String(input.targetMessageKey || '').trim()
const matches = this.records
.filter((record) => {
if (record.accountScope !== scope) return false
if ((record.sourceType || 'insight') !== 'message_analysis') return false
if (record.sessionId !== sessionId) return false
const target = record.messageInsight
if (!target) return false
if (targetLocalId > 0 && Number(target.targetLocalId || 0) === targetLocalId) {
if (targetCreateTime <= 0 || Number(target.targetCreateTime || 0) === targetCreateTime) return true
}
if (targetMessageKey && target.targetMessageKey === targetMessageKey) return true
return false
})
.sort((a, b) => b.createdAt - a.createdAt)
return matches[0] || null
}
markRecordRead(id: string): { success: boolean; error?: string } {
this.ensureLoaded()
const normalizedId = String(id || '').trim()

View File

@@ -21,7 +21,12 @@ import { chatService, ChatSession, Message } from './chatService'
import { snsService } from './snsService'
import { weiboService } from './social/weiboService'
import { showNotification } from '../windows/notificationWindow'
import { insightRecordService, type InsightRecordLog, type InsightRecordTriggerReason } from './insightRecordService'
import {
insightRecordService,
type InsightRecordLog,
type InsightRecordTriggerReason,
type MessageInsightAnalysis
} from './insightRecordService'
// ─── 常量 ────────────────────────────────────────────────────────────────────
@@ -81,6 +86,18 @@ interface SharedAiModelConfig {
type InsightFilterMode = 'whitelist' | 'blacklist'
class ApiRequestError extends Error {
statusCode?: number
responseBody?: string
constructor(message: string, statusCode?: number, responseBody?: string) {
super(message)
this.name = 'ApiRequestError'
this.statusCode = statusCode
this.responseBody = responseBody
}
}
// ─── 日志 ─────────────────────────────────────────────────────────────────────
type InsightLogLevel = 'INFO' | 'WARN' | 'ERROR'
@@ -161,6 +178,52 @@ function normalizeSessionIdList(value: unknown): string[] {
return Array.from(new Set(value.map((item) => String(item || '').trim()).filter(Boolean)))
}
function clampText(value: unknown, maxLength: number): string {
const text = String(value || '').replace(/\s+/g, ' ').trim()
if (text.length <= maxLength) return text
return `${text.slice(0, Math.max(0, maxLength - 1))}`
}
function stripJsonFence(value: string): string {
const text = String(value || '').trim()
const fenced = text.match(/^```(?:json)?\s*([\s\S]*?)\s*```$/i)
if (fenced) return fenced[1].trim()
const firstBrace = text.indexOf('{')
const lastBrace = text.lastIndexOf('}')
if (firstBrace >= 0 && lastBrace > firstBrace) {
return text.slice(firstBrace, lastBrace + 1).trim()
}
return text
}
function parseMessageInsightAnalysis(rawOutput: string): MessageInsightAnalysis {
let parsed: unknown
try {
parsed = JSON.parse(stripJsonFence(rawOutput))
} catch {
throw new Error('模型输出格式异常:不是合法 JSON')
}
if (!parsed || typeof parsed !== 'object') {
throw new Error('模型输出格式异常JSON 根节点不是对象')
}
const source = parsed as Record<string, unknown>
const explicitText = clampText(source.explicit_text ?? source.explicitText, 120)
const emotion = clampText(source.emotion, 16)
const intent = clampText(source.intent, 20)
const topic = clampText(source.topic, 20)
if (!explicitText || !emotion || !intent || !topic) {
throw new Error('模型输出格式异常:缺少必要字段')
}
return { explicitText, emotion, intent, topic }
}
function shouldFallbackJsonMode(error: unknown): boolean {
const statusCode = Number((error as ApiRequestError)?.statusCode || 0)
if (statusCode === 400 || statusCode === 404 || statusCode === 422) return true
const text = `${(error as Error)?.message || ''}\n${(error as ApiRequestError)?.responseBody || ''}`.toLowerCase()
return text.includes('response_format') || text.includes('json_object') || text.includes('json mode')
}
/**
* 调用 OpenAI 兼容 API非流式返回模型第一条消息内容。
* 使用 Node 原生 https/http 模块,无需任何第三方 SDK。
@@ -171,7 +234,8 @@ function callApi(
model: string,
messages: Array<{ role: string; content: string }>,
timeoutMs: number = API_TIMEOUT_MS,
maxTokens: number = API_MAX_TOKENS_DEFAULT
maxTokens: number = API_MAX_TOKENS_DEFAULT,
options?: { responseFormatJson?: boolean }
): Promise<string> {
return new Promise((resolve, reject) => {
const endpoint = buildApiUrl(apiBaseUrl, '/chat/completions')
@@ -183,13 +247,17 @@ function callApi(
return
}
const body = JSON.stringify({
const payload: Record<string, unknown> = {
model,
messages,
max_tokens: normalizeApiMaxTokens(maxTokens),
temperature: API_TEMPERATURE,
stream: false
})
}
if (options?.responseFormatJson) {
payload.response_format = { type: 'json_object' }
}
const body = JSON.stringify(payload)
const options = {
hostname: urlObj.hostname,
@@ -210,6 +278,10 @@ function callApi(
res.on('data', (chunk) => { data += chunk })
res.on('end', () => {
try {
if (res.statusCode && res.statusCode >= 400) {
reject(new ApiRequestError(`API 请求失败 (${res.statusCode}): ${data.slice(0, 200)}`, res.statusCode, data))
return
}
const parsed = JSON.parse(data)
const content = parsed?.choices?.[0]?.message?.content
if (typeof content === 'string' && content.trim()) {
@@ -590,6 +662,207 @@ ${topMentionText}
}
}
async generateMessageInsight(params: {
sessionId: string
displayName?: string
avatarUrl?: string
targetLocalId?: number
targetCreateTime?: number
targetMessageKey?: string
targetText: string
targetSenderName?: string
contextCount?: number
forceRefresh?: boolean
}): Promise<{ success: boolean; message: string; cached?: boolean; recordId?: string; data?: MessageInsightAnalysis }> {
const enabled = this.config.get('aiMessageInsightEnabled') === true
if (!enabled) {
return { success: false, message: '请先在设置中开启「消息解析」' }
}
const sessionId = String(params?.sessionId || '').trim()
const targetText = clampText(params?.targetText || '', 500)
const targetCreateTime = Math.floor(Number(params?.targetCreateTime || 0))
const targetLocalId = Math.floor(Number(params?.targetLocalId || 0))
const targetMessageKey = String(params?.targetMessageKey || '').trim()
if (!sessionId || !targetText || targetCreateTime <= 0) {
return { success: false, message: '目标消息无效,无法解析' }
}
if (params?.forceRefresh !== true) {
const cached = insightRecordService.findLatestMessageAnalysis({
sessionId,
targetLocalId,
targetCreateTime,
targetMessageKey
})
if (cached?.messageInsight?.analysis) {
return {
success: true,
message: '已读取缓存解析',
cached: true,
recordId: cached.id,
data: cached.messageInsight.analysis
}
}
}
const { apiBaseUrl, apiKey, model, maxTokens } = this.getSharedAiModelConfig()
if (!apiBaseUrl || !apiKey) {
return { success: false, message: '请先填写通用 AI 模型配置API 地址和 Key' }
}
const configuredContextCount = Number(this.config.get('aiMessageInsightContextCount') || 50)
const contextCount = Math.max(1, Math.min(200, Math.floor(Number(params?.contextCount || configuredContextCount) || 50)))
const displayName = await this.resolveInsightSessionDisplayName(sessionId, String(params?.displayName || sessionId))
const targetSenderName = clampText(params?.targetSenderName || displayName, 40) || displayName
const targetTextPreview = clampText(targetText, 120)
let avatarUrl = String(params?.avatarUrl || '').trim() || undefined
if (!avatarUrl) {
try {
const contact = await chatService.getContactAvatar(sessionId)
avatarUrl = String(contact?.avatarUrl || '').trim() || undefined
} catch {
avatarUrl = undefined
}
}
let beforeMessages: Message[] = []
let afterMessages: Message[] = []
let contextReadError = ''
try {
const aroundResult = await chatService.getMessagesAround(
sessionId,
{ localId: targetLocalId, createTime: targetCreateTime, messageKey: targetMessageKey },
contextCount
)
if (aroundResult.success) {
beforeMessages = aroundResult.before || []
afterMessages = aroundResult.after || []
} else {
contextReadError = aroundResult.error || '读取上下文失败'
}
} catch (error) {
contextReadError = (error as Error).message || String(error)
}
const formatLine = (message: Message) => {
const senderName = message.isSend === 1 ? '我' : (message.senderDisplayName || targetSenderName || displayName)
return `${this.formatInsightMessageTimestamp(message.createTime)} ${senderName}${this.formatInsightMessageContent(message)}`
}
const beforeText = beforeMessages.length > 0 ? beforeMessages.map(formatLine).join('\n') : '无'
const afterText = afterMessages.length > 0 ? afterMessages.map(formatLine).join('\n') : '无'
const DEFAULT_MESSAGE_INSIGHT_PROMPT = `你是一个克制、准确的聊天语义分析助手。你的任务是把用户选中的一句聊天消息做深度解析,帮助用户理解对方未明说的含义。
严格要求:
1. 必须且只能输出合法的纯 JSON。
2. 禁止输出解释说明、前言后语,禁止使用 Markdown 或代码块。
3. 不要编造上下文没有支持的信息;不确定时用谨慎表述。
4. explicit_text 用自然中文说明这句话可能想表达的真实含义80字以内。
5. emotion、intent、topic 必须是短标签。
JSON 输出格式:
{
"explicit_text": "暗示转明示80字以内",
"emotion": "2-6字情绪标签",
"intent": "2-8字意图标签",
"topic": "2-8字话题标签"
}`
const customPrompt = String(this.config.get('aiMessageInsightSystemPrompt') || '').trim()
const systemPrompt = customPrompt || DEFAULT_MESSAGE_INSIGHT_PROMPT
const userPromptBase = `会话:${displayName}
目标发送者:${targetSenderName}
目标消息时间:${this.formatInsightMessageTimestamp(targetCreateTime)}
目标消息:
${targetText}
目标消息之前的上下文(${beforeMessages.length} 条):
${beforeText}
目标消息之后的上下文(${afterMessages.length} 条):
${afterText}
请分析目标消息,只输出指定 JSON。`
const userPrompt = appendPromptCurrentTime(userPromptBase)
const endpoint = buildApiUrl(apiBaseUrl, '/chat/completions')
const requestMessages = [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: userPrompt }
]
let rawOutput = ''
let responseFormatJson = true
let responseFormatFallback = false
let responseFormatFallbackReason = ''
const startedAt = Date.now()
try {
try {
rawOutput = await callApi(apiBaseUrl, apiKey, model, requestMessages, API_TIMEOUT_MS, maxTokens, { responseFormatJson: true })
} catch (error) {
if (!shouldFallbackJsonMode(error)) throw error
responseFormatJson = false
responseFormatFallback = true
responseFormatFallbackReason = (error as Error).message || 'response_format 不受支持'
rawOutput = await callApi(apiBaseUrl, apiKey, model, requestMessages, API_TIMEOUT_MS, maxTokens)
}
const analysis = parseMessageInsightAnalysis(rawOutput)
const finalInsight = analysis.explicitText
const log: InsightRecordLog = {
endpoint,
model,
maxTokens,
temperature: API_TEMPERATURE,
triggerReason: 'message_analysis',
allowContext: true,
contextCount,
systemPrompt,
userPrompt,
rawOutput,
finalInsight,
durationMs: Date.now() - startedAt,
createdAt: Date.now(),
responseFormatJson,
responseFormatFallback,
responseFormatFallbackReason,
targetMessage: {
localId: targetLocalId,
createTime: targetCreateTime,
messageKey: targetMessageKey,
senderName: targetSenderName,
textPreview: targetTextPreview
},
contextStats: {
requested: contextCount,
beforeTarget: beforeMessages.length,
afterTarget: afterMessages.length,
readError: contextReadError || undefined
},
parsedAnalysis: analysis
}
const record = insightRecordService.addRecord({
sessionId,
displayName,
avatarUrl,
sourceType: 'message_analysis',
triggerReason: 'message_analysis',
insight: finalInsight,
messageInsight: {
targetLocalId,
targetCreateTime,
targetMessageKey,
targetSenderName,
targetTextPreview,
analysis
},
log
})
return { success: true, message: '解析完成', cached: false, recordId: record.id, data: analysis }
} catch (error) {
return { success: false, message: `解析失败:${(error as Error).message}` }
}
}
// ── 私有方法 ────────────────────────────────────────────────────────────────
private isEnabled(): boolean {