mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-11-30 16:02:58 +08:00
feat: 完善问问微信查询理解与检索链路
This commit is contained in:
1 parent
7554de7d72
commit
facc292443
14 files changed
+1107
-66
No files matched your search
@@ -81,6 +81,10 @@ function sourceMessageId(message: chat.FormattedMessage): string {
|
||||
|
||||
function sourceKind(message: chat.FormattedMessage): KnowledgeMessageKind {
|
||||
if (message.voiceTranscript || message.type === '语音') return 'voice'
|
||||
if (message.exportMediaType === 'image' || message.exportMediaType === 'video' || message.exportMediaType === 'sticker') {
|
||||
return message.exportMediaType
|
||||
}
|
||||
if (message.exportMediaType === 'file') return 'file'
|
||||
if (message.contentData?.type === 'share' || message.contentData?.type === 'miniProgram') {
|
||||
return message.contentData.type === 'share' && message.contentData.typeVal === '6'
|
||||
? 'file'
|
||||
@@ -329,6 +333,7 @@ export class KnowledgeSearchService {
|
||||
senderIds: request.senderIds,
|
||||
startTime: request.startTime === undefined ? undefined : request.startTime * 1000,
|
||||
endTime: request.endTime === undefined ? undefined : request.endTime * 1000
|
||||
,conversationBoundary: request.conversationBoundary
|
||||
}
|
||||
const result = await this.searchKnowledge(searchRequest)
|
||||
// An existing derived database can answer while its next incremental pass is running.
|
||||
@@ -481,12 +486,14 @@ export class KnowledgeSearchService {
|
||||
.filter(({ message, score }) => {
|
||||
const senderMatches = !senderIds.size || senderIds.has(message.senderId || message.from)
|
||||
const termMatches = !terms.length || score > 0
|
||||
return senderMatches && termMatches
|
||||
const boundaryMatches = !request.conversationBoundary || message.contentData?.type !== 'system'
|
||||
return senderMatches && termMatches && boundaryMatches
|
||||
})
|
||||
.sort(
|
||||
(left, right) =>
|
||||
right.score - left.score ||
|
||||
(right.message.createTime || 0) - (left.message.createTime || 0)
|
||||
(request.conversationBoundary === 'first' ? -1 : 1) *
|
||||
((right.message.createTime || 0) - (left.message.createTime || 0))
|
||||
)
|
||||
.slice(0, Math.max(1, Math.min(request.limit || FALLBACK_LIMIT, FALLBACK_LIMIT)))
|
||||
const result: KnowledgeSearchIpcResult = {
|
||||
|
||||
@@ -358,6 +358,36 @@ export class KnowledgeStore {
|
||||
])
|
||||
).filter(Boolean)
|
||||
const senderIds = new Set(query.senderIds || [])
|
||||
if (query.conversationBoundary && conversationIds.length === 1) {
|
||||
const direction = query.conversationBoundary === 'first' ? 'ASC' : 'DESC'
|
||||
const row = this.database
|
||||
.prepare(
|
||||
`SELECT conversation_id, message_id, create_time, searchable_text, kind, sender_id, sender_name
|
||||
FROM knowledge_messages
|
||||
WHERE conversation_id = ? AND kind <> 'system'
|
||||
ORDER BY create_time ${direction}, message_id ${direction}
|
||||
LIMIT 1`
|
||||
)
|
||||
.get(conversationIds[0]) as DbRow | undefined
|
||||
const count = this.database
|
||||
.prepare('SELECT COUNT(*) AS total FROM knowledge_messages WHERE conversation_id = ?')
|
||||
.get(conversationIds[0]) as DbRow | undefined
|
||||
const indexState = this.database
|
||||
.prepare('SELECT state, complete_snapshot FROM knowledge_index_state WHERE conversation_id = ?')
|
||||
.get(conversationIds[0]) as DbRow | undefined
|
||||
return {
|
||||
evidence: row ? [this.metadataEvidence(row)] : [],
|
||||
conversationRetrieval: {
|
||||
conversationId: conversationIds[0],
|
||||
totalMessages: Number(count?.total || 0),
|
||||
chunkCount: 0,
|
||||
candidateMessages: row ? 1 : 0,
|
||||
systemMessagesDeprioritized: 0,
|
||||
complete: String(indexState?.state || '') === 'ready' && Number(indexState?.complete_snapshot || 0) === 1
|
||||
},
|
||||
timings: { ...emptyKnowledgeSearchTimings(), messageLoadMs: Date.now() - startedAt, totalMs: Date.now() - startedAt }
|
||||
}
|
||||
}
|
||||
if (!terms.length) {
|
||||
const messageLoadStartedAt = Date.now()
|
||||
const metadata = this.searchByMetadata(query, conversationIds, senderIds)
|
||||
@@ -832,10 +862,15 @@ export class KnowledgeStore {
|
||||
state TEXT NOT NULL,
|
||||
high_water_time INTEGER,
|
||||
indexed_message_count INTEGER NOT NULL DEFAULT 0,
|
||||
complete_snapshot INTEGER NOT NULL DEFAULT 0,
|
||||
last_error TEXT,
|
||||
updated_at INTEGER NOT NULL
|
||||
) STRICT;
|
||||
`)
|
||||
const stateColumns = new Set(asRows(this.database.prepare('PRAGMA table_info(knowledge_index_state)').all()).map((row) => String(row.name)))
|
||||
if (!stateColumns.has('complete_snapshot')) {
|
||||
this.database.exec('ALTER TABLE knowledge_index_state ADD COLUMN complete_snapshot INTEGER NOT NULL DEFAULT 0')
|
||||
}
|
||||
const messageColumns = new Set(
|
||||
asRows(this.database.prepare('PRAGMA table_info(knowledge_messages)').all()).map((row) =>
|
||||
String(row.name)
|
||||
@@ -935,7 +970,9 @@ export class KnowledgeStore {
|
||||
chunker.version,
|
||||
'indexing',
|
||||
null,
|
||||
normalized.length
|
||||
normalized.length,
|
||||
null,
|
||||
conversation.completeSnapshot
|
||||
)
|
||||
await this.writeMessageLedger(
|
||||
conversation.conversationId,
|
||||
@@ -982,7 +1019,9 @@ export class KnowledgeStore {
|
||||
chunker.version,
|
||||
'ready',
|
||||
highWater,
|
||||
normalized.length
|
||||
normalized.length,
|
||||
null,
|
||||
conversation.completeSnapshot
|
||||
)
|
||||
this.database.exec('COMMIT')
|
||||
return { chunkCount: chunks.length, updatedChunks: chunks.length }
|
||||
@@ -1122,20 +1161,22 @@ export class KnowledgeStore {
|
||||
state: string,
|
||||
highWater: number | null,
|
||||
messageCount: number,
|
||||
error: string | null = null
|
||||
error: string | null = null,
|
||||
completeSnapshot = false
|
||||
): void {
|
||||
this.database
|
||||
.prepare(
|
||||
`INSERT INTO knowledge_index_state (
|
||||
conversation_id, account_id, chunker_version, state, high_water_time,
|
||||
indexed_message_count, last_error, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
indexed_message_count, complete_snapshot, last_error, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(conversation_id) DO UPDATE SET
|
||||
account_id = excluded.account_id,
|
||||
chunker_version = excluded.chunker_version,
|
||||
state = excluded.state,
|
||||
high_water_time = excluded.high_water_time,
|
||||
indexed_message_count = excluded.indexed_message_count,
|
||||
complete_snapshot = excluded.complete_snapshot,
|
||||
last_error = excluded.last_error,
|
||||
updated_at = excluded.updated_at`
|
||||
)
|
||||
@@ -1146,6 +1187,7 @@ export class KnowledgeStore {
|
||||
state,
|
||||
highWater,
|
||||
messageCount,
|
||||
completeSnapshot ? 1 : 0,
|
||||
error,
|
||||
Date.now()
|
||||
)
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
buildLocalAiSearchPlan,
|
||||
inferAiSearchTimeRange,
|
||||
mergeAiSearchPlans,
|
||||
parseAiQueryUnderstanding,
|
||||
parseAiSearchPlan,
|
||||
type AiSearchAgentRun,
|
||||
type AiSearchAgentTraceItem,
|
||||
@@ -26,6 +27,7 @@ import { runControlledSearchAgent, type AgentAction, type AgentToolResult } from
|
||||
import { AIProviderService } from './ai-provider-service'
|
||||
import { KnowledgeSearchService } from '../knowledge/knowledge-search-service'
|
||||
import { resolveContact, type ContactResolutionScope } from './contact-resolution-service'
|
||||
import type { ContactResolutionResult } from '../../shared/contact-resolution'
|
||||
|
||||
const DISPLAY_EVIDENCE_LIMIT = 8
|
||||
const AGENT_MESSAGE_LIMIT = 100
|
||||
@@ -55,14 +57,32 @@ const contactScopeForIntent = (intent: AiSearchPlan['intent']): ContactResolutio
|
||||
: 'person'
|
||||
|
||||
const isIdentityIntent = (intent: AiSearchPlan['intent']): boolean =>
|
||||
intent === 'conversation_boundary' ||
|
||||
intent === 'conversation_recall' ||
|
||||
intent === 'conversation_topic_search' ||
|
||||
intent === 'conversation_name_search'
|
||||
|
||||
const isIdentityPlan = (plan: AiSearchPlan): boolean =>
|
||||
isIdentityIntent(plan.intent) || (plan.mode === 'semantic' && Boolean(plan.contactQuery || plan.targetQuery))
|
||||
|
||||
export const isSuspiciousLocalPlan = (query: string, plan: AiSearchPlan): boolean => {
|
||||
const hasRelationalShape = /我\s*(?:和|跟|与)|(?:和|跟|与)\s*我|第一次|最早|最后一次|最近一次/.test(query)
|
||||
const hasBoundaryShape = /第一次|最早|最后一次|最近一次|上一次|什么时候开始/.test(query)
|
||||
const hasInterpretiveShape = /熟起来|刚认识|答应|是不是|提过|后来|那个|事情/.test(query)
|
||||
return (
|
||||
(plan.intent === 'global_topic_search' && hasRelationalShape) ||
|
||||
(hasInterpretiveShape && plan.intent !== 'conversation_boundary') ||
|
||||
(hasBoundaryShape && plan.intent !== 'conversation_boundary') ||
|
||||
(hasRelationalShape && !plan.contactQuery)
|
||||
)
|
||||
}
|
||||
|
||||
const retrievalModeForIntent = (
|
||||
intent: AiSearchPlan['intent']
|
||||
): AiSearchRetrievalContract['retrievalMode'] =>
|
||||
intent === 'conversation_recall'
|
||||
intent === 'conversation_boundary'
|
||||
? 'conversation_boundary'
|
||||
: intent === 'conversation_recall'
|
||||
? 'conversation_metadata'
|
||||
: intent === 'conversation_topic_search'
|
||||
? 'conversation_topic_fts'
|
||||
@@ -97,6 +117,11 @@ const conversationIdsForContacts = (contacts: Contact[]): string[] =>
|
||||
const messageTime = (timestamp: number): string =>
|
||||
new Date(timestamp).toLocaleString('zh-CN', { hour12: false })
|
||||
|
||||
const queryDate = (timestamp: number): string => {
|
||||
const date = new Date(timestamp * 1000)
|
||||
return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`
|
||||
}
|
||||
|
||||
const estimateTokens = (value: string): number => Math.ceil(value.length / 2)
|
||||
|
||||
const emptyAggregation = (): AiSearchAggregation => ({
|
||||
@@ -259,6 +284,7 @@ export class AiSearchPipelineService {
|
||||
const localPlan = buildLocalAiSearchPlan(request.text)
|
||||
let plan: AiSearchPlan = {
|
||||
...localPlan,
|
||||
mode: 'structured',
|
||||
scopeLabel: aiSearchScopeLabel(
|
||||
localPlan.intent === 'global_group_topic_search' && request.scope === 'global'
|
||||
? 'groups'
|
||||
@@ -288,14 +314,106 @@ export class AiSearchPipelineService {
|
||||
const contactResolutionStartedAt = Date.now()
|
||||
const contacts = chat.isReady() ? await chat.listContactsAsync() : []
|
||||
signal.throwIfAborted()
|
||||
const shouldUseQueryUnderstanding =
|
||||
localPlan.intent === 'general' || isSuspiciousLocalPlan(request.text, plan)
|
||||
let queryUnderstandingSource: 'local' | 'ai' = 'local'
|
||||
let queryUnderstandingError: string | undefined
|
||||
if (shouldUseQueryUnderstanding && aiSearchAvailable) {
|
||||
let parserResult: Awaited<ReturnType<AiSearchPipelineService['chatForSearchRequest']>>
|
||||
let parserException: string | undefined
|
||||
try {
|
||||
parserResult = await this.chatForSearchRequest(
|
||||
request.requestId,
|
||||
aiConfig.providerId,
|
||||
aiConfig.model,
|
||||
[
|
||||
{
|
||||
role: 'system',
|
||||
content:
|
||||
'你是 TraceMemo Query Compiler。只输出严格 JSON,不回答问题、不搜索聊天、不编造联系人。mode 只能是 structured、semantic、clarification。structured 复用已有 intent;conversation_boundary 允许 boundary=first|last 和 projection=time|content|time_and_content。semantic 用 semanticQuery(1-200 字)、queryVariants(最多 4 个)、可选 targetQuery(联系人显示名)和 answerMode=extract|synthesis;clarification 仅用于确实缺少必要上下文且 requiresClarification=true。只允许输出 schema 中字段:mode、intent、contactQuery、topicQuery、boundary、projection、targetQuery、semanticQuery、queryVariants、answerMode、confidence、requiresClarification、clarificationReason。禁止 conversationId、wxid、messageId、SQL、路径、Evidence、时间戳、Tool、Provider。',
|
||||
},
|
||||
{
|
||||
role: 'user',
|
||||
content: `用户问题:${request.text}\n当前界面范围:${request.scope}`
|
||||
}
|
||||
],
|
||||
signal
|
||||
)
|
||||
} catch (error) {
|
||||
parserException = error instanceof Error ? error.message : '查询语义解析器执行失败'
|
||||
parserResult = {
|
||||
success: false,
|
||||
error: parserException
|
||||
}
|
||||
}
|
||||
const understanding = parserResult.success && parserResult.data
|
||||
? parseAiQueryUnderstanding(parserResult.data)
|
||||
: null
|
||||
if (understanding && understanding.confidence >= 0.6 && !understanding.requiresClarification) {
|
||||
queryUnderstandingSource = 'ai'
|
||||
plan = {
|
||||
...plan,
|
||||
intent: understanding.intent,
|
||||
mode: understanding.mode || 'structured',
|
||||
contactQuery: understanding.contactQuery || understanding.targetQuery,
|
||||
targetQuery: understanding.targetQuery,
|
||||
semanticQuery: understanding.semanticQuery,
|
||||
queryVariants: understanding.queryVariants,
|
||||
answerMode: understanding.answerMode,
|
||||
topicQuery: understanding.topicQuery,
|
||||
boundary: understanding.boundary,
|
||||
projection: understanding.projection,
|
||||
keywords: understanding.topicQuery ? [understanding.topicQuery] : [],
|
||||
variants: understanding.topicQuery ? [understanding.topicQuery] : [],
|
||||
source: 'hybrid'
|
||||
}
|
||||
} else {
|
||||
queryUnderstandingError =
|
||||
understanding?.clarificationReason || parserException || parserResult.error || '无法可靠理解查询意图'
|
||||
}
|
||||
} else if (shouldUseQueryUnderstanding) {
|
||||
queryUnderstandingError = aiConfig.configured ? '查询语义解析器暂时不可用' : '尚未配置可用 AI 模型'
|
||||
}
|
||||
const selectedContact =
|
||||
request.scope === 'conversation' && request.conversationId
|
||||
? contacts.find((contact) => contact.md5 === request.conversationId)
|
||||
: undefined
|
||||
const sourceContacts = this.scopeContacts(contacts, request, selectedContact, plan.intent)
|
||||
if (!sourceContacts.length) throw new Error('当前搜索范围没有可用会话')
|
||||
const contactResolution = plan.contactQuery
|
||||
? resolveContact(plan.contactQuery, sourceContacts, contactScopeForIntent(plan.intent))
|
||||
if (!sourceContacts.length && !queryUnderstandingError) throw new Error('当前搜索范围没有可用会话')
|
||||
if (queryUnderstandingError) {
|
||||
const retrieval: AiSearchRetrievalContract = {
|
||||
intent: plan.intent,
|
||||
timeRange: plan.timeRange,
|
||||
retrievalMode: 'unresolved_identity',
|
||||
candidateCount: 0,
|
||||
uniqueCandidateCount: 0,
|
||||
sourceCoverage: 'unknown',
|
||||
isComplete: false,
|
||||
fallbackUsed: false,
|
||||
suspicious: true
|
||||
}
|
||||
const error = `我没有完全理解你想怎么查:${queryUnderstandingError}`
|
||||
emit({ stage: 'query_understanding', status: 'error', message: error, plan, error })
|
||||
return {
|
||||
requestId: request.requestId,
|
||||
status: 'understanding_failed',
|
||||
plan,
|
||||
knowledge: { source: 'knowledge', state: 'unavailable', indexedMessageCount: 0, indexedChunkCount: 0, totalMessages: 0 },
|
||||
candidateEvidenceCount: 0,
|
||||
retrieval,
|
||||
evidence: [],
|
||||
evidenceCollection: [],
|
||||
contextEvidenceCount: 0,
|
||||
aggregation: emptyAggregation(),
|
||||
agent: { mode: 'fallback', toolCalls: 0, trace: [], fallbackReason: error },
|
||||
timings: snapshotTimings(),
|
||||
error,
|
||||
errorStage: 'query_understanding',
|
||||
elapsedMs: Date.now() - startedAt
|
||||
}
|
||||
}
|
||||
const contactResolution = (plan.contactQuery || plan.targetQuery)
|
||||
? resolveContact(plan.contactQuery || plan.targetQuery || '', sourceContacts, contactScopeForIntent(plan.intent))
|
||||
: undefined
|
||||
const resolvedContact =
|
||||
selectedContact ||
|
||||
@@ -313,7 +431,7 @@ export class AiSearchPipelineService {
|
||||
contactNames: resolvedContact ? [contactLabel(resolvedContact)] : []
|
||||
}
|
||||
const conversationIds =
|
||||
isIdentityIntent(plan.intent) && resolvedContact
|
||||
isIdentityPlan(plan) && resolvedContact
|
||||
? [resolvedContact.md5]
|
||||
: request.scope === 'global' && plan.intent !== 'global_group_topic_search'
|
||||
? undefined
|
||||
@@ -323,7 +441,7 @@ export class AiSearchPipelineService {
|
||||
let agent: AiSearchAgentRun = { mode: 'fallback', toolCalls: 0, trace: [] }
|
||||
let candidateEvidence: AiSearchPipelineEvidence[]
|
||||
let searchResult: KnowledgeSearchIpcResult
|
||||
const agentOutcome = aiSearchAvailable
|
||||
const agentOutcome = aiSearchAvailable && plan.intent !== 'conversation_boundary' && plan.mode !== 'semantic'
|
||||
? await this.runAgentSearch(
|
||||
request,
|
||||
plan,
|
||||
@@ -387,8 +505,8 @@ export class AiSearchPipelineService {
|
||||
timings.rankingMs += agentOutcome.searchTimings.rankingMs
|
||||
} else {
|
||||
const deterministicIdentityRetrieval =
|
||||
Boolean(resolvedContact) && (isIdentityIntent(plan.intent) || Boolean(selectedContact))
|
||||
const unresolvedIdentity = isIdentityIntent(plan.intent) && !resolvedContact
|
||||
Boolean(resolvedContact) && (isIdentityPlan(plan) || Boolean(selectedContact))
|
||||
const unresolvedIdentity = isIdentityPlan(plan) && !resolvedContact
|
||||
const fallbackReason = confirmedConversationNeedsFallback
|
||||
? selectedContact
|
||||
? '已选择会话的 Agent 未产生可读取消息,已按该会话执行确定性检索'
|
||||
@@ -423,7 +541,7 @@ export class AiSearchPipelineService {
|
||||
agentTrace: agent.trace[0],
|
||||
timings: snapshotTimings()
|
||||
})
|
||||
if (aiSearchAvailable && !deterministicIdentityRetrieval && !unresolvedIdentity) {
|
||||
if (aiSearchAvailable && plan.mode !== 'semantic' && !deterministicIdentityRetrieval && !unresolvedIdentity) {
|
||||
const planningStartedAt = Date.now()
|
||||
const planning = await this.chatForSearchRequest(
|
||||
request.requestId,
|
||||
@@ -476,22 +594,46 @@ export class AiSearchPipelineService {
|
||||
} else {
|
||||
const knowledgeSearchStartedAt = Date.now()
|
||||
const deterministicTerms =
|
||||
plan.intent === 'conversation_recall' || plan.intent === 'conversation_name_search'
|
||||
? []
|
||||
: plan.intent === 'conversation_topic_search'
|
||||
? plan.topicQuery
|
||||
? [plan.topicQuery]
|
||||
: []
|
||||
: Array.from(new Set([...plan.keywords, ...plan.variants]))
|
||||
searchResult = await this.knowledge.search({
|
||||
text: request.text,
|
||||
terms: deterministicTerms,
|
||||
retrievalSessionId: request.requestId,
|
||||
conversationIds,
|
||||
startTime: plan.timeRange.startTime,
|
||||
endTime: plan.timeRange.endTime,
|
||||
limit: 240
|
||||
})
|
||||
plan.mode === 'semantic'
|
||||
? Array.from(new Set([plan.semanticQuery, ...(plan.queryVariants || [])].filter((term): term is string => Boolean(term)))).slice(0, 5)
|
||||
: plan.intent === 'conversation_recall' || plan.intent === 'conversation_name_search'
|
||||
? []
|
||||
: plan.intent === 'conversation_topic_search'
|
||||
? plan.topicQuery
|
||||
? [plan.topicQuery]
|
||||
: []
|
||||
: Array.from(new Set([...plan.keywords, ...plan.variants]))
|
||||
const semanticResults: KnowledgeSearchIpcResult[] = []
|
||||
const termsToSearch: string[][] =
|
||||
plan.mode === 'semantic' ? deterministicTerms.map((term) => [term]) : [deterministicTerms]
|
||||
for (const terms of termsToSearch) {
|
||||
semanticResults.push(await this.knowledge.search({
|
||||
text: request.text,
|
||||
terms,
|
||||
retrievalSessionId: request.requestId,
|
||||
conversationIds,
|
||||
startTime: plan.timeRange.startTime,
|
||||
endTime: plan.timeRange.endTime,
|
||||
conversationBoundary: plan.boundary,
|
||||
limit: 240
|
||||
}))
|
||||
}
|
||||
const firstResult = semanticResults[0]
|
||||
const mergedEvidence = Array.from(
|
||||
new Map(semanticResults.flatMap((result) => result.evidence).map((item) => [`${item.conversationId}\u0000${item.messageId}`, item])).values()
|
||||
)
|
||||
searchResult = {
|
||||
...(firstResult || {
|
||||
source: 'knowledge', state: 'ready', indexedMessageCount: 0, indexedChunkCount: 0,
|
||||
totalMessages: 0, evidence: [], timings: emptyKnowledgeSearchTimings()
|
||||
}),
|
||||
evidence: mergedEvidence,
|
||||
indexedMessageCount: Math.max(...semanticResults.map((result) => result.indexedMessageCount), 0),
|
||||
indexedChunkCount: Math.max(...semanticResults.map((result) => result.indexedChunkCount), 0),
|
||||
totalMessages: Math.max(...semanticResults.map((result) => result.totalMessages), 0),
|
||||
conversationRetrieval: semanticResults.find((result) => result.conversationRetrieval)?.conversationRetrieval,
|
||||
voiceCoverage: semanticResults.find((result) => result.voiceCoverage)?.voiceCoverage
|
||||
}
|
||||
signal.throwIfAborted()
|
||||
timings.knowledgeSearchMs += Date.now() - knowledgeSearchStartedAt
|
||||
candidateEvidence = this.toPipelineEvidence(searchResult, contacts)
|
||||
@@ -516,7 +658,7 @@ export class AiSearchPipelineService {
|
||||
emit({
|
||||
stage: 'query_understanding',
|
||||
status: 'completed',
|
||||
message: '已理解搜索条件',
|
||||
message: queryUnderstandingSource === 'ai' ? '已通过查询语义解析器理解搜索条件' : '已理解搜索条件',
|
||||
plan,
|
||||
timings: snapshotTimings()
|
||||
})
|
||||
@@ -580,7 +722,8 @@ export class AiSearchPipelineService {
|
||||
resolvedContact,
|
||||
searchResult,
|
||||
candidateEvidence,
|
||||
agent
|
||||
agent,
|
||||
contactResolution
|
||||
)
|
||||
if (retrieval.suspicious && resolvedContact) {
|
||||
// An identity route that somehow yielded 0/1 records is never allowed
|
||||
@@ -593,6 +736,7 @@ export class AiSearchPipelineService {
|
||||
conversationIds: [resolvedContact.md5],
|
||||
startTime: plan.timeRange.startTime,
|
||||
endTime: plan.timeRange.endTime,
|
||||
conversationBoundary: plan.boundary,
|
||||
limit: 240
|
||||
})
|
||||
signal.throwIfAborted()
|
||||
@@ -603,7 +747,8 @@ export class AiSearchPipelineService {
|
||||
resolvedContact,
|
||||
searchResult,
|
||||
candidateEvidence,
|
||||
agent
|
||||
agent,
|
||||
contactResolution
|
||||
)
|
||||
}
|
||||
|
||||
@@ -717,7 +862,30 @@ export class AiSearchPipelineService {
|
||||
timings: snapshotTimings(),
|
||||
elapsedMs: Date.now() - startedAt
|
||||
}
|
||||
if (plan.intent === 'conversation_boundary' && retrieval.identityResolution !== 'resolved') {
|
||||
const ambiguous = retrieval.identityResolution === 'ambiguous'
|
||||
const error = ambiguous
|
||||
? `联系人“${plan.contactQuery || ''}”存在多个匹配,请先确认具体联系人。`
|
||||
: `没有确认联系人“${plan.contactQuery || ''}”,无法查询会话边界。`
|
||||
return {
|
||||
...baseResult,
|
||||
status: ambiguous ? 'ambiguous_contact' : 'contact_not_found',
|
||||
error,
|
||||
timings: snapshotTimings(),
|
||||
elapsedMs: Date.now() - startedAt
|
||||
}
|
||||
}
|
||||
if (!evidence.length) {
|
||||
if (plan.intent === 'conversation_boundary') {
|
||||
const error = `已确认联系人“${plan.contactQuery || ''}”,但当前知识库没有可读取的聊天消息。`
|
||||
return {
|
||||
...baseResult,
|
||||
status: 'no_messages',
|
||||
error,
|
||||
timings: snapshotTimings(),
|
||||
elapsedMs: Date.now() - startedAt
|
||||
}
|
||||
}
|
||||
emit({
|
||||
stage: 'completed',
|
||||
status: 'completed',
|
||||
@@ -734,6 +902,51 @@ export class AiSearchPipelineService {
|
||||
}
|
||||
}
|
||||
|
||||
if (plan.intent === 'conversation_boundary') {
|
||||
const item = evidence[0]
|
||||
const boundaryLabel = plan.boundary === 'first' ? '最早' : '最后'
|
||||
const coverageNote = retrieval.isComplete
|
||||
? '当前知识库已完成该会话的可读取记录覆盖。'
|
||||
: '当前知识库覆盖不完整,不能据此确认历史上的第一次或最后一次交流。'
|
||||
const projection = plan.projection || 'time'
|
||||
const content = item.sourceKind === 'text'
|
||||
? item.text || '(文本消息为空)'
|
||||
: item.sourceKind === 'voice'
|
||||
? item.text ? `语音转写:${item.text}` : '语音消息(没有可用转写)'
|
||||
: item.sourceKind === 'file'
|
||||
? '文件消息(没有可展示文本)'
|
||||
: item.sourceKind === 'image'
|
||||
? '图片消息(没有可展示文本)'
|
||||
: item.sourceKind === 'video'
|
||||
? '视频消息(没有可展示文本)'
|
||||
: item.sourceKind === 'sticker'
|
||||
? '表情消息(没有可展示文本)'
|
||||
: item.sourceKind === 'link'
|
||||
? '链接消息(没有可展示文本)'
|
||||
: '非文本消息(没有可展示文本)'
|
||||
const timeLine = `在 TraceMemo 当前可读取的聊天记录中,你和${plan.contactQuery || item.conversationName} ${boundaryLabel}的一条聊天记录出现在 ${messageTime(item.timestamp)}。`
|
||||
const contentLine = `${item.sender}:${content} [E1]`
|
||||
const answer = projection === 'content'
|
||||
? `在 TraceMemo 当前可读取的聊天记录中,你和${plan.contactQuery || item.conversationName} ${boundaryLabel}的一条聊天记录内容是:\n${contentLine}\n${coverageNote}`
|
||||
: `${timeLine}\n${contentLine}\n${coverageNote}`
|
||||
emit({
|
||||
stage: 'completed',
|
||||
status: 'completed',
|
||||
message: '已确定会话边界并生成可追溯答案',
|
||||
plan,
|
||||
stats: { matchedMessages: 1, evidenceCount: 1 },
|
||||
timings: snapshotTimings()
|
||||
})
|
||||
return {
|
||||
...baseResult,
|
||||
status: 'completed',
|
||||
answer,
|
||||
citationValidation: { status: 'valid', invalidCitationIds: [] },
|
||||
timings: snapshotTimings(),
|
||||
elapsedMs: Date.now() - startedAt
|
||||
}
|
||||
}
|
||||
|
||||
if (retrieval.suspicious) {
|
||||
const error = '已找到目标会话,但当前检索未完整覆盖聊天记录,未生成总结。'
|
||||
emit({
|
||||
@@ -1681,6 +1894,7 @@ export class AiSearchPipelineService {
|
||||
: ''
|
||||
return `检索范围:${plan.scopeLabel},时间:${plan.rangeLabel}
|
||||
用户问题:${query}
|
||||
当前查询实际时间范围:${plan.timeRange.startTime === undefined ? '未限定开始日期' : queryDate(plan.timeRange.startTime)} 至 ${plan.timeRange.endTime === undefined ? '当前时刻' : queryDate(plan.timeRange.endTime)}。回答只能基于这个程序确定的时间范围,不得根据“上个月”“去年”等原句自行推算其他年份。
|
||||
检索意图:${aiSearchIntentLabel(plan.intent)}
|
||||
检索关键词:${plan.keywords.join('、') || '未提取到主题关键词'}
|
||||
检索范围消息总数:${totalMessages}
|
||||
@@ -1701,9 +1915,10 @@ ${context}`
|
||||
resolvedContact: Contact | undefined,
|
||||
result: KnowledgeSearchIpcResult,
|
||||
candidates: AiSearchPipelineEvidence[],
|
||||
agent: AiSearchAgentRun
|
||||
agent: AiSearchAgentRun,
|
||||
contactResolution?: ContactResolutionResult
|
||||
): AiSearchRetrievalContract {
|
||||
const identity = isIdentityIntent(plan.intent)
|
||||
const identity = isIdentityPlan(plan)
|
||||
const conversationRetrieval = result.conversationRetrieval
|
||||
const sourceMessageCount =
|
||||
conversationRetrieval?.totalMessages ??
|
||||
@@ -1743,6 +1958,14 @@ ${context}`
|
||||
fallbackUsed: agent.mode === 'fallback' || result.source === 'fallback',
|
||||
fallbackReason: agent.fallbackReason || result.fallbackReason,
|
||||
voiceCoverage: result.voiceCoverage,
|
||||
identityResolution: identity
|
||||
? resolvedContact
|
||||
? 'resolved'
|
||||
: contactResolution?.ambiguous
|
||||
? 'ambiguous'
|
||||
: 'not_found'
|
||||
: 'not_required',
|
||||
boundary: plan.boundary,
|
||||
suspicious:
|
||||
plan.intent === 'conversation_recall' &&
|
||||
Boolean(resolvedContact) &&
|
||||
|
||||
@@ -236,6 +236,9 @@ export function AISearchWorkspace({
|
||||
range,
|
||||
timeRangeOverride,
|
||||
activeContactMd5: activeContact?.md5,
|
||||
knowledgeGeneration: knowledgeStatus
|
||||
? `${knowledgeStatus.state}:${knowledgeStatus.indexedMessageCount}:${knowledgeStatus.indexedChunkCount}:${knowledgeStatus.processedMessages}`
|
||||
: undefined,
|
||||
retry
|
||||
})
|
||||
if (!normalizedQuery) {
|
||||
@@ -672,8 +675,12 @@ export function AISearchWorkspace({
|
||||
<span className="ai-search-kicker">✓ 已完成</span>
|
||||
<h2>{resultQuery || query}</h2>
|
||||
<p>
|
||||
知识库已收录 {messageCount.toLocaleString()} 条消息 → 找到{' '}
|
||||
{searchTrace?.retrievedEvidence || 0} 条相关消息 → {evidence.length} 条 Evidence →
|
||||
知识库已收录 {messageCount.toLocaleString()} 条消息 →{' '}
|
||||
{cachedAt
|
||||
? `缓存中保留 ${evidenceCollection.length} 条 Evidence`
|
||||
: searchTrace?.retrievedEvidence !== undefined
|
||||
? `读取 ${searchTrace.retrievedEvidence} 条范围消息`
|
||||
: `读取 ${evidence.length} 条消息`}{' '}→ {evidence.length} 条 Evidence →
|
||||
已生成回答{cachedAt ? ' · 已使用缓存' : ''}
|
||||
</p>
|
||||
{searchTrace &&
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useEffect, useRef, useState, type Dispatch, type SetStateAction } from 'react'
|
||||
import { aiSearchRangeStart } from '../../../../../shared/ai-search'
|
||||
import { aiSearchRangeStart, inferAiSearchTimeRange } from '../../../../../shared/ai-search'
|
||||
import type { AiSearchTimeRange } from '../../../../../shared/ai-search'
|
||||
import {
|
||||
RANGE_LABELS,
|
||||
@@ -9,7 +9,6 @@ import {
|
||||
buildSearchCacheKey,
|
||||
parseSearchCacheKey,
|
||||
readSearchCache,
|
||||
readSearchCacheByQuery,
|
||||
writeSearchCache
|
||||
} from '../searchUtils'
|
||||
import { createSearchCacheRecord, mapCacheRecordToResult } from '../searchMappers'
|
||||
@@ -178,13 +177,15 @@ export function useSearchHistory({
|
||||
setQuery(historyQuery)
|
||||
setSelectedEvidence(0)
|
||||
setHistoryOpen(false)
|
||||
const resolvedTimeRange = inferAiSearchTimeRange(historyQuery, range, new Date())
|
||||
const cacheKey = buildSearchCacheKey(
|
||||
scope,
|
||||
scope === 'conversation' ? conversationContactMd5 : '',
|
||||
range,
|
||||
historyQuery
|
||||
historyQuery,
|
||||
resolvedTimeRange
|
||||
)
|
||||
const cached = readSearchCache(cacheKey) || readSearchCacheByQuery(historyQuery)?.record || null
|
||||
const cached = readSearchCache(cacheKey)
|
||||
if (!cached) {
|
||||
setAnswer('')
|
||||
setEvidence([])
|
||||
|
||||
@@ -58,7 +58,10 @@ export const mapSearchResultToTrace = (
|
||||
finalEvidenceCount: number
|
||||
): SearchTrace => ({
|
||||
knowledgeMessages: result.knowledge.indexedMessageCount,
|
||||
retrievedEvidence: result.candidateEvidenceCount,
|
||||
retrievedEvidence:
|
||||
result.retrieval.intent === 'conversation_recall'
|
||||
? result.retrieval.sourceMessageCount ?? result.candidateEvidenceCount
|
||||
: result.candidateEvidenceCount,
|
||||
finalEvidence: finalEvidenceCount,
|
||||
timings: result.timings,
|
||||
contextEvidence: result.contextEvidenceCount,
|
||||
|
||||
@@ -51,6 +51,30 @@ export const resolveSearchResultViewTransition = (
|
||||
result: AiSearchPipelineResult,
|
||||
range: SearchRange
|
||||
): SearchResultViewTransition => {
|
||||
if (result.status === 'understanding_failed') {
|
||||
return {
|
||||
stage: 'insufficient',
|
||||
analysisError: result.error || '我没有完全理解你想怎么查,可以换一种说法。'
|
||||
}
|
||||
}
|
||||
if (result.status === 'contact_not_found') {
|
||||
return {
|
||||
stage: 'insufficient',
|
||||
analysisError: result.error || '我理解你在问这个联系人,但没有在当前通讯录中确认到对应联系人。'
|
||||
}
|
||||
}
|
||||
if (result.status === 'ambiguous_contact') {
|
||||
return {
|
||||
stage: 'insufficient',
|
||||
analysisError: result.error || '找到多个可能的联系人,暂时无法确定你指的是哪一个。'
|
||||
}
|
||||
}
|
||||
if (result.status === 'no_messages') {
|
||||
return {
|
||||
stage: 'insufficient',
|
||||
analysisError: result.error || '已经确认联系人,但当前可读取记录里没有对应聊天消息。'
|
||||
}
|
||||
}
|
||||
if (result.status === 'no_evidence') {
|
||||
return {
|
||||
stage: 'insufficient',
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { Contact, Message } from '../../../../shared/types'
|
||||
import type { AiSearchTimeRange } from '../../../../shared/ai-search'
|
||||
import { inferAiSearchTimeRange, type AiSearchTimeRange } from '../../../../shared/ai-search'
|
||||
import type {
|
||||
AISearchCacheRecord,
|
||||
EvidenceItem,
|
||||
@@ -315,8 +315,23 @@ export const buildSearchCacheKey = (
|
||||
scope: SearchScope,
|
||||
contactMd5: string,
|
||||
range: SearchRange,
|
||||
query: string
|
||||
): string => JSON.stringify([scope, contactMd5, range, query.trim().toLowerCase()])
|
||||
query: string,
|
||||
resolvedTimeRange?: Pick<AiSearchTimeRange, 'startTime' | 'endTime'>,
|
||||
knowledgeGeneration?: string
|
||||
): string => {
|
||||
const base = [scope, contactMd5, range, query.trim().toLowerCase()]
|
||||
const hasResolvedTime = Boolean(
|
||||
resolvedTimeRange &&
|
||||
(resolvedTimeRange.startTime !== undefined || resolvedTimeRange.endTime !== undefined)
|
||||
)
|
||||
if (!hasResolvedTime && !knowledgeGeneration) return JSON.stringify(base)
|
||||
return JSON.stringify([
|
||||
...base,
|
||||
hasResolvedTime ? resolvedTimeRange?.startTime ?? null : null,
|
||||
hasResolvedTime ? resolvedTimeRange?.endTime ?? null : null,
|
||||
knowledgeGeneration || null
|
||||
])
|
||||
}
|
||||
|
||||
export type CreateSearchRequestContextInput = {
|
||||
query: string
|
||||
@@ -324,6 +339,8 @@ export type CreateSearchRequestContextInput = {
|
||||
range: SearchRange
|
||||
timeRangeOverride?: AiSearchTimeRange
|
||||
activeContactMd5?: string
|
||||
now?: Date
|
||||
knowledgeGeneration?: string
|
||||
retry?: {
|
||||
range: SearchRange
|
||||
timeRangeOverride?: AiSearchTimeRange
|
||||
@@ -335,6 +352,7 @@ export type SearchRequestContext = {
|
||||
effectiveRange: SearchRange
|
||||
effectiveTimeRangeOverride?: AiSearchTimeRange
|
||||
conversationId?: string
|
||||
resolvedTimeRange: AiSearchTimeRange
|
||||
cacheKey: string
|
||||
}
|
||||
|
||||
@@ -344,25 +362,41 @@ export const createSearchRequestContext = ({
|
||||
range,
|
||||
timeRangeOverride,
|
||||
activeContactMd5,
|
||||
now,
|
||||
knowledgeGeneration,
|
||||
retry
|
||||
}: CreateSearchRequestContextInput): SearchRequestContext => {
|
||||
const normalizedQuery = query.trim()
|
||||
const effectiveRange = retry?.range || range
|
||||
const effectiveTimeRangeOverride = retry?.timeRangeOverride || timeRangeOverride
|
||||
const conversationId = scope === 'conversation' ? activeContactMd5 : undefined
|
||||
const resolvedTimeRange = inferAiSearchTimeRange(
|
||||
normalizedQuery,
|
||||
effectiveRange,
|
||||
now || new Date(),
|
||||
effectiveTimeRangeOverride
|
||||
)
|
||||
|
||||
return {
|
||||
normalizedQuery,
|
||||
effectiveRange,
|
||||
effectiveTimeRangeOverride,
|
||||
conversationId,
|
||||
cacheKey: buildSearchCacheKey(scope, conversationId || '', effectiveRange, normalizedQuery)
|
||||
resolvedTimeRange,
|
||||
cacheKey: buildSearchCacheKey(
|
||||
scope,
|
||||
conversationId || '',
|
||||
effectiveRange,
|
||||
normalizedQuery,
|
||||
resolvedTimeRange,
|
||||
knowledgeGeneration
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export const parseSearchCacheKey = (
|
||||
key: string
|
||||
): { scope: SearchScope; contactMd5: string; range: SearchRange; query: string } | null => {
|
||||
): { scope: SearchScope; contactMd5: string; range: SearchRange; query: string; startTime?: number; endTime?: number } | null => {
|
||||
try {
|
||||
const parts = JSON.parse(key) as unknown
|
||||
if (
|
||||
@@ -377,7 +411,9 @@ export const parseSearchCacheKey = (
|
||||
scope: parts[0] as SearchScope,
|
||||
contactMd5: typeof parts[1] === 'string' ? parts[1] : '',
|
||||
range: parts[2] as SearchRange,
|
||||
query: parts[3]
|
||||
query: parts[3],
|
||||
startTime: typeof parts[4] === 'number' ? parts[4] : undefined,
|
||||
endTime: typeof parts[5] === 'number' ? parts[5] : undefined
|
||||
}
|
||||
} catch {
|
||||
return null
|
||||
|
||||
+195
-8
@@ -6,12 +6,16 @@ import type {
|
||||
|
||||
export type AiSearchScope = 'global' | 'groups' | 'contacts' | 'conversation'
|
||||
export type AiSearchRange = 'today' | '7d' | '30d' | 'all'
|
||||
export type AiSearchProjection = 'time' | 'content' | 'time_and_content'
|
||||
export type AiSearchQueryMode = 'structured' | 'semantic' | 'clarification'
|
||||
export type AiSearchAnswerMode = 'extract' | 'synthesis'
|
||||
/**
|
||||
* Retrieval semantics, not presentation labels. Each intent has a constrained
|
||||
* execution path in the main process; a model must not be able to quietly turn
|
||||
* an identity lookup into a generic message-keyword search.
|
||||
*/
|
||||
export type AiSearchIntent =
|
||||
| 'conversation_boundary'
|
||||
| 'conversation_recall'
|
||||
| 'conversation_topic_search'
|
||||
| 'global_sender_topic_search'
|
||||
@@ -44,6 +48,7 @@ export type AiSearchProgressStage =
|
||||
export type AiSearchProgressStatus = 'running' | 'completed' | 'error'
|
||||
|
||||
export interface AiSearchPlan {
|
||||
mode?: AiSearchQueryMode
|
||||
intent: AiSearchIntent
|
||||
keywords: string[]
|
||||
variants: string[]
|
||||
@@ -56,6 +61,28 @@ export interface AiSearchPlan {
|
||||
contactQuery?: string
|
||||
/** The message-content query, never a contact display name. */
|
||||
topicQuery?: string
|
||||
boundary?: 'first' | 'last'
|
||||
projection?: AiSearchProjection
|
||||
targetQuery?: string
|
||||
semanticQuery?: string
|
||||
queryVariants?: string[]
|
||||
answerMode?: AiSearchAnswerMode
|
||||
}
|
||||
|
||||
export interface AiQueryUnderstanding {
|
||||
mode?: AiSearchQueryMode
|
||||
intent: AiSearchIntent
|
||||
contactQuery?: string
|
||||
topicQuery?: string
|
||||
boundary?: 'first' | 'last'
|
||||
projection?: AiSearchProjection
|
||||
targetQuery?: string
|
||||
semanticQuery?: string
|
||||
queryVariants?: string[]
|
||||
answerMode?: AiSearchAnswerMode
|
||||
confidence: number
|
||||
requiresClarification: boolean
|
||||
clarificationReason?: string
|
||||
}
|
||||
|
||||
export interface AiSearchPipelineRequest {
|
||||
@@ -222,6 +249,7 @@ export interface AiSearchRetrievalContract {
|
||||
conversationId?: string
|
||||
timeRange: AiSearchTimeRange
|
||||
retrievalMode:
|
||||
| 'conversation_boundary'
|
||||
| 'conversation_metadata'
|
||||
| 'conversation_topic_fts'
|
||||
| 'global_fts'
|
||||
@@ -236,12 +264,18 @@ export interface AiSearchRetrievalContract {
|
||||
fallbackReason?: string
|
||||
suspicious: boolean
|
||||
voiceCoverage?: KnowledgeVoiceCoverage
|
||||
identityResolution?: 'not_required' | 'resolved' | 'not_found' | 'ambiguous'
|
||||
boundary?: 'first' | 'last'
|
||||
}
|
||||
|
||||
export interface AiSearchPipelineResult {
|
||||
requestId: string
|
||||
status:
|
||||
| 'completed'
|
||||
| 'understanding_failed'
|
||||
| 'contact_not_found'
|
||||
| 'ambiguous_contact'
|
||||
| 'no_messages'
|
||||
| 'no_evidence'
|
||||
| 'retrieval_incomplete'
|
||||
| 'ai_failed'
|
||||
@@ -434,16 +468,39 @@ export const inferAiSearchTimeRange = (
|
||||
}
|
||||
if (/这个月|本月/.test(query)) return fromQuery(currentMonthStart(now), '本月', '用户说“这个月”')
|
||||
if (/上个月/.test(query)) {
|
||||
const start = Math.floor(new Date(now.getFullYear(), now.getMonth() - 1, 1).getTime() / 1000)
|
||||
const startDate = new Date(now.getFullYear(), now.getMonth() - 1, 1)
|
||||
const end = Math.floor(new Date(now.getFullYear(), now.getMonth(), 1).getTime() / 1000) - 1
|
||||
const start = Math.floor(startDate.getTime() / 1000)
|
||||
return {
|
||||
startTime: start,
|
||||
endTime: end,
|
||||
label: '上个月',
|
||||
label: `${startDate.getFullYear()}年${startDate.getMonth() + 1}月`,
|
||||
reason: '用户说“上个月”',
|
||||
source: 'query'
|
||||
}
|
||||
}
|
||||
if (/今天/.test(query)) return fromQuery(dayStart(now), '今天', '用户说“今天”')
|
||||
if (/昨天|前天/.test(query)) {
|
||||
const daysAgo = query.includes('前天') ? 2 : 1
|
||||
const start = dayStart(now) - daysAgo * 86400
|
||||
return {
|
||||
startTime: start,
|
||||
endTime: dayStart(now) - (daysAgo - 1) * 86400 - 1,
|
||||
label: daysAgo === 1 ? '昨天' : '前天',
|
||||
reason: `用户说“${daysAgo === 1 ? '昨天' : '前天'}”`,
|
||||
source: 'query'
|
||||
}
|
||||
}
|
||||
if (/去年/.test(query)) {
|
||||
const startDate = new Date(now.getFullYear() - 1, 0, 1)
|
||||
return {
|
||||
startTime: Math.floor(startDate.getTime() / 1000),
|
||||
endTime: Math.floor(new Date(now.getFullYear(), 0, 1).getTime() / 1000) - 1,
|
||||
label: `${startDate.getFullYear()}年`,
|
||||
reason: '用户说“去年”',
|
||||
source: 'query'
|
||||
}
|
||||
}
|
||||
if (/今年/.test(query)) return fromQuery(currentYearStart(now), '今年', '用户说“今年”')
|
||||
if (/最近/.test(query)) return fromQuery(nowSeconds - 30 * 86400, '近 30 天', '用户说“最近”')
|
||||
return {
|
||||
@@ -456,6 +513,7 @@ export const inferAiSearchTimeRange = (
|
||||
}
|
||||
|
||||
export const aiSearchIntentLabel = (intent: AiSearchIntent): string => {
|
||||
if (intent === 'conversation_boundary') return '查询会话边界'
|
||||
if (intent === 'conversation_recall') return '回顾最近聊天'
|
||||
if (intent === 'conversation_topic_search') return '在指定聊天中查找话题'
|
||||
if (intent === 'global_sender_topic_search') return '按人物查找'
|
||||
@@ -516,10 +574,27 @@ export const buildLocalAiSearchPlan = (
|
||||
query: string
|
||||
): Pick<
|
||||
AiSearchPlan,
|
||||
'intent' | 'keywords' | 'variants' | 'source' | 'contactQuery' | 'topicQuery'
|
||||
'intent' | 'keywords' | 'variants' | 'source' | 'contactQuery' | 'topicQuery' | 'boundary' | 'projection'
|
||||
> => {
|
||||
const keywords = extractKeywords(query)
|
||||
const normalized = query.replace(/[“”"'‘’「」『』]/g, '').trim()
|
||||
const boundary =
|
||||
normalized.match(/^(?:我和|我跟|我与)\s*【?(.+?)】?\s*(第一次聊天|第一次说话|最早(?:一次)?(?:聊天|说话|聊过)?|最后一次聊天|最后一次说话|最近一次(?:聊天|说话))是什么时候[??。!!]*$/) ||
|
||||
normalized.match(/^我(最早|最后|最近一次)\s*什么时候\s*(?:和|跟|与)\s*【?(.+?)】?\s*(?:聊过|聊天|说话)[??。!!]*$/) ||
|
||||
normalized.match(/^我(最早|最后|最近一次)\s*(?:和|跟|与)\s*【?(.+?)】?\s*(?:聊过|聊天|说话)是什么时候[??。!!]*$/)
|
||||
if (boundary) {
|
||||
const relationalFirst = /^(?:我和|我跟|我与)/.test(normalized)
|
||||
const phrase = relationalFirst ? boundary[2] : boundary[1]
|
||||
const first = /第一次|最早/.test(phrase)
|
||||
const contact = (relationalFirst ? boundary[1] : boundary[2]).replace(/[【】]/g, '').trim()
|
||||
const asksContent = /说了什么|说的什么|聊了什么|聊的什么/.test(normalized)
|
||||
const asksTime = /什么时候|哪天/.test(normalized)
|
||||
return {
|
||||
intent: 'conversation_boundary', keywords: [], variants: [], source: 'local',
|
||||
contactQuery: contact, boundary: first ? 'first' : 'last', topicQuery: undefined,
|
||||
projection: asksContent && asksTime ? 'time_and_content' : asksContent ? 'content' : 'time'
|
||||
}
|
||||
}
|
||||
const recall = normalized.match(
|
||||
new RegExp(
|
||||
`(?:我和|我跟|我与)\\s*(.+?)\\s*(?:最近|这几天|本周|这个月|本月|今年|上个月|刚刚|刚才)?\\s*(?:${RECALL_QUESTION})`
|
||||
@@ -603,7 +678,7 @@ export const parseAiSearchPlan = (
|
||||
const parsed = JSON.parse(jsonMatch[0]) as Record<string, unknown>
|
||||
const intent = [
|
||||
'general',
|
||||
'conversation_recall',
|
||||
'conversation_recall', 'conversation_boundary',
|
||||
'conversation_topic_search',
|
||||
'global_sender_topic_search',
|
||||
'global_group_topic_search',
|
||||
@@ -626,15 +701,125 @@ export const parseAiSearchPlan = (
|
||||
}
|
||||
}
|
||||
|
||||
const QUERY_UNDERSTANDING_INTENTS: AiSearchIntent[] = [
|
||||
'conversation_recall',
|
||||
'conversation_boundary',
|
||||
'conversation_topic_search',
|
||||
'global_sender_topic_search',
|
||||
'global_group_topic_search',
|
||||
'global_topic_search',
|
||||
'conversation_name_search',
|
||||
'general'
|
||||
]
|
||||
|
||||
/** Strict host-side validation for the small semantic parser contract. */
|
||||
export const parseAiQueryUnderstanding = (value: string): AiQueryUnderstanding | null => {
|
||||
const raw = value.trim()
|
||||
if (!raw.startsWith('{') || !raw.endsWith('}')) return null
|
||||
let parsed: Record<string, unknown>
|
||||
try {
|
||||
parsed = JSON.parse(raw) as Record<string, unknown>
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
const allowed = new Set([
|
||||
'mode',
|
||||
'intent',
|
||||
'contactQuery',
|
||||
'topicQuery',
|
||||
'boundary',
|
||||
'projection',
|
||||
'targetQuery',
|
||||
'semanticQuery',
|
||||
'queryVariants',
|
||||
'answerMode',
|
||||
'confidence',
|
||||
'requiresClarification',
|
||||
'clarificationReason'
|
||||
])
|
||||
if (Object.keys(parsed).some((key) => !allowed.has(key))) return null
|
||||
const intent = String(parsed.intent) as AiSearchIntent
|
||||
if (!QUERY_UNDERSTANDING_INTENTS.includes(intent)) return null
|
||||
const mode = parsed.mode === null || parsed.mode === undefined ? 'structured' : parsed.mode
|
||||
if (mode !== 'structured' && mode !== 'semantic' && mode !== 'clarification') return null
|
||||
if (typeof parsed.confidence !== 'number' || !Number.isFinite(parsed.confidence)) return null
|
||||
const confidence = Math.max(0, Math.min(1, parsed.confidence))
|
||||
if (typeof parsed.requiresClarification !== 'boolean') return null
|
||||
const textField = (key: 'contactQuery' | 'topicQuery'): string | undefined | null => {
|
||||
const candidate = parsed[key]
|
||||
if (candidate === null || candidate === undefined) return undefined
|
||||
if (typeof candidate !== 'string') return null
|
||||
const normalized = candidate.replace(/[【】]/g, '').trim()
|
||||
if (!normalized || normalized.length > 64 || /conversationId|messageId|wxid_|SELECT\s|INSERT\s/i.test(normalized)) return null
|
||||
return normalized
|
||||
}
|
||||
const contactQuery = textField('contactQuery')
|
||||
const topicQuery = textField('topicQuery')
|
||||
if (contactQuery === null || topicQuery === null) return null
|
||||
const boundedField = (key: 'targetQuery' | 'semanticQuery', max: number): string | undefined | null => {
|
||||
const candidate = parsed[key]
|
||||
if (candidate === null || candidate === undefined) return undefined
|
||||
if (typeof candidate !== 'string') return null
|
||||
const normalized = candidate.trim()
|
||||
if (!normalized || normalized.length > max || /conversationId|messageId|wxid_|SELECT\s|INSERT\s/i.test(normalized)) return null
|
||||
return normalized
|
||||
}
|
||||
const targetQuery = boundedField('targetQuery', 64)
|
||||
const semanticQuery = boundedField('semanticQuery', 200)
|
||||
if (targetQuery === null || semanticQuery === null) return null
|
||||
const queryVariants = parsed.queryVariants === null || parsed.queryVariants === undefined ? [] : parsed.queryVariants
|
||||
if (!Array.isArray(queryVariants) || queryVariants.length > 4) return null
|
||||
const normalizedVariants: string[] = []
|
||||
for (const variant of queryVariants) {
|
||||
if (typeof variant !== 'string') return null
|
||||
const normalized = variant.trim()
|
||||
if (!normalized || normalized.length > 80 || /conversationId|messageId|wxid_|SELECT\s|INSERT\s/i.test(normalized)) return null
|
||||
normalizedVariants.push(normalized)
|
||||
}
|
||||
const answerMode = parsed.answerMode === null || parsed.answerMode === undefined ? undefined : parsed.answerMode
|
||||
if (answerMode !== undefined && answerMode !== 'extract' && answerMode !== 'synthesis') return null
|
||||
const boundary = parsed.boundary === null || parsed.boundary === undefined ? undefined : parsed.boundary
|
||||
if (boundary !== undefined && boundary !== 'first' && boundary !== 'last') return null
|
||||
const projection = parsed.projection === null || parsed.projection === undefined ? undefined : parsed.projection
|
||||
if (projection !== undefined && projection !== 'time' && projection !== 'content' && projection !== 'time_and_content') return null
|
||||
if (intent === 'conversation_boundary' && !boundary) return null
|
||||
if (intent === 'conversation_boundary' && topicQuery !== undefined) return null
|
||||
if (intent !== 'conversation_boundary' && boundary !== undefined) return null
|
||||
if (intent !== 'conversation_boundary' && projection !== undefined) return null
|
||||
if (intent === 'conversation_boundary' && !contactQuery) return null
|
||||
if (mode === 'structured' && (targetQuery !== undefined || semanticQuery !== undefined || normalizedVariants.length || answerMode !== undefined)) return null
|
||||
if (mode === 'semantic' && !semanticQuery) return null
|
||||
if (mode === 'clarification' && !parsed.requiresClarification) return null
|
||||
if (mode === 'semantic' && parsed.requiresClarification) return null
|
||||
return {
|
||||
mode,
|
||||
intent,
|
||||
contactQuery,
|
||||
topicQuery,
|
||||
boundary,
|
||||
projection: intent === 'conversation_boundary' ? projection || 'time' : undefined,
|
||||
targetQuery,
|
||||
semanticQuery,
|
||||
queryVariants: normalizedVariants,
|
||||
answerMode: mode === 'semantic' ? answerMode || 'synthesis' : undefined,
|
||||
confidence,
|
||||
requiresClarification: parsed.requiresClarification,
|
||||
clarificationReason:
|
||||
typeof parsed.clarificationReason === 'string'
|
||||
? parsed.clarificationReason.slice(0, 160)
|
||||
: undefined
|
||||
}
|
||||
}
|
||||
|
||||
export const mergeAiSearchPlans = (
|
||||
local: Pick<
|
||||
AiSearchPlan,
|
||||
'intent' | 'keywords' | 'variants' | 'source' | 'contactQuery' | 'topicQuery'
|
||||
'intent' | 'keywords' | 'variants' | 'source' | 'contactQuery' | 'topicQuery' | 'boundary' | 'projection'
|
||||
>,
|
||||
ai: Partial<Pick<AiSearchPlan, 'intent' | 'keywords' | 'variants' | 'topicQuery'>> | null
|
||||
ai: Partial<Pick<AiSearchPlan, 'intent' | 'keywords' | 'variants' | 'topicQuery' | 'projection'>> | null
|
||||
): Pick<
|
||||
AiSearchPlan,
|
||||
'intent' | 'keywords' | 'variants' | 'source' | 'contactQuery' | 'topicQuery'
|
||||
'intent' | 'keywords' | 'variants' | 'source' | 'contactQuery' | 'topicQuery' | 'boundary' | 'projection'
|
||||
> => {
|
||||
if (!ai) return local
|
||||
const keywords = normalizeTerms([...local.keywords, ...(ai.keywords || [])])
|
||||
@@ -657,7 +842,9 @@ export const mergeAiSearchPlans = (
|
||||
variants,
|
||||
source: 'hybrid',
|
||||
contactQuery: local.contactQuery,
|
||||
topicQuery: local.topicQuery || ai.topicQuery
|
||||
topicQuery: local.topicQuery || ai.topicQuery,
|
||||
boundary: local.boundary,
|
||||
projection: local.projection || ai.projection
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
export const KNOWLEDGE_SCHEMA_VERSION = 1
|
||||
export const DEFAULT_CHUNKER_VERSION = 'conversation-v1'
|
||||
|
||||
export type KnowledgeMessageKind = 'text' | 'voice' | 'file' | 'link' | 'system' | 'other'
|
||||
export type KnowledgeMessageKind = 'text' | 'voice' | 'file' | 'link' | 'image' | 'video' | 'sticker' | 'system' | 'other'
|
||||
export type KnowledgeIndexPhase =
|
||||
| 'idle'
|
||||
| 'preflight'
|
||||
@@ -210,6 +210,7 @@ export interface KnowledgeQuery {
|
||||
/** Unix epoch milliseconds. */
|
||||
endTime?: number
|
||||
temporalIntent?: KnowledgeTemporalIntent
|
||||
conversationBoundary?: 'first' | 'last'
|
||||
}
|
||||
|
||||
export interface KnowledgeSearchRequest extends KnowledgeQuery {
|
||||
@@ -299,6 +300,7 @@ export interface KnowledgeSearchIpcRequest {
|
||||
senderIds?: string[]
|
||||
startTime?: number
|
||||
endTime?: number
|
||||
conversationBoundary?: 'first' | 'last'
|
||||
limit?: number
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user