feat: 新增微信图片文字索引与问问微信图片检索能力

This commit is contained in:
电摇小子 committed 2026-09-16 10:42:52 +08:00
1 parent 24399f1d70
commit b8f08d54c8
46 files changed
+6938 -75

No files matched your search

+31 -9
View File
@@ -86,7 +86,8 @@ export class AskWechatService {
const diagnostics = this.diagnostics(
{ provider: '', model: '', modelCallCount: 0, toolCallCount: 0, traces: [] },
startedAt,
'runtime_error'
'runtime_error',
{ question }
)
this.writeLog('error', `Query Agent Runtime 异常(${this.options.entry})`, diagnostics)
return this.fallback(request, 'runtime_error', diagnostics)
@@ -97,13 +98,13 @@ export class AskWechatService {
engine: 'query-agent',
status: 'error',
message: EMPTY_QUESTION_MESSAGE,
diagnostics: this.diagnostics(result, startedAt, 'invalid_question')
diagnostics: this.diagnostics(result, startedAt, 'invalid_question', { question })
}
}
if (result.errorKind === 'provider_unavailable' || result.errorKind === 'provider_failure') {
const outcome: AskWechatOutcome = result.errorKind
const diagnostics = this.diagnostics(result, startedAt, outcome)
const diagnostics = this.diagnostics(result, startedAt, outcome, { question })
this.writeLog('warn', `查询 Provider 不可用(${this.options.entry})`, diagnostics)
return {
engine: 'query-agent',
@@ -114,13 +115,13 @@ export class AskWechatService {
}
if (result.errorKind === 'tool_limit') {
const diagnostics = this.diagnostics(result, startedAt, 'tool_limit')
const diagnostics = this.diagnostics(result, startedAt, 'tool_limit', { question })
this.writeLog('warn', `查询超出工具调用上限(${this.options.entry})`, diagnostics)
return this.fallback(request, 'runtime_error', diagnostics)
}
if (!result.answer?.trim()) {
const diagnostics = this.diagnostics(result, startedAt, 'runtime_error')
const diagnostics = this.diagnostics(result, startedAt, 'runtime_error', { question })
this.writeLog('warn', `Query Agent 未返回回答(${this.options.entry})`, diagnostics)
return this.fallback(request, 'runtime_error', diagnostics)
}
@@ -128,7 +129,7 @@ export class AskWechatService {
const answer = result.answer.trim()
// 澄清回答也记录:下一句("是 BOBO")需要接得上上文。
this.memory.record(conversationKey, question, answer)
const diagnostics = this.diagnostics(result, startedAt, 'answered')
const diagnostics = this.diagnostics(result, startedAt, 'answered', { question, answer })
this.writeLog('info', `Query Agent 回答完成(${this.options.entry})`, diagnostics)
return {
engine: 'query-agent',
@@ -187,17 +188,38 @@ export class AskWechatService {
> &
Partial<Pick<QueryAgentResult, 'totalMs'>>,
startedAt: number,
outcome: AskWechatOutcome
outcome: AskWechatOutcome,
/**
* 问答原文(可选)。只在本地应用日志里用,不上传、不进遥测。
*
* 排查这类"同一问题时对时错"的故障,光有工具名与次数是不够的 ——
* 必须能对着"问题 + 模型回答"回放,否则无法判断是理解错了、链路断了,还是索引没建。
*/
content?: { question?: string; answer?: string }
): QueryAgentDiagnostics {
const traces = result.traces || []
// 图片 OCR 的两条结构化事实:不回读正文,只统计"取到了几条"与"当时覆盖度是多少"。
const imageOcrTextCount = traces.reduce(
(sum, trace) => sum + (trace.imageOcrTextCount || 0),
0
)
const coverageState = traces
.map((trace) => trace.imageOcrCoverageState)
.filter((value): value is string => typeof value === 'string')
.at(-1)
return {
entry: this.options.entry,
provider: result.provider,
model: result.model,
modelCallCount: result.modelCallCount,
toolCallCount: result.toolCallCount,
tools: (result.traces || []).map((trace) => trace.toolName),
tools: traces.map((trace) => trace.toolName),
totalMs: result.totalMs || Date.now() - startedAt,
outcome
outcome,
...(imageOcrTextCount > 0 ? { imageOcrTextCount } : {}),
...(coverageState ? { imageOcrCoverageState: coverageState } : {}),
...(content?.question ? { question: content.question } : {}),
...(content?.answer ? { answer: content.answer } : {})
}
}
+16
View File
@@ -8,9 +8,12 @@ export type { CacheClearScope } from '../../shared/cache'
const BOOTSTRAP_CACHE_DIR = path.join(app.getPath('userData'), 'cache', 'bootstrap')
const KNOWLEDGE_CACHE_DIR = path.join(app.getPath('userData'), 'knowledge')
const IMAGE_TEXT_INDEX_CACHE_DIR = path.join(app.getPath('userData'), 'image-text-index')
export interface CacheClearOptions {
beforeClearKnowledge?: () => Promise<void>
/** 清理图片文字索引前调用:停任务 + 关闭派生库句柄。 */
beforeClearImageTextIndex?: () => Promise<void>
}
function inspectDirectory(directory: string): { sizeBytes: number; fileCount: number } {
@@ -46,6 +49,7 @@ export function getCacheSummary(): CacheSummary {
const bootstrap = inspectDirectory(BOOTSTRAP_CACHE_DIR)
const electron = inspectDirectory(path.join(app.getPath('userData'), 'Cache'))
const knowledge = inspectDirectory(KNOWLEDGE_CACHE_DIR)
const imageTextIndex = inspectDirectory(IMAGE_TEXT_INDEX_CACHE_DIR)
const items: CacheSummaryItem[] = [
{
id: 'bootstrap',
@@ -65,6 +69,13 @@ export function getCacheSummary(): CacheSummary {
description:
'为问问微信建立的所有账号本地检索索引。清理后需手动重新建立,不影响微信原始数据。',
...knowledge
},
{
id: 'image-text-index',
label: '图片文字索引',
description:
'本机从微信图片里识别出的文字及其检索索引。清理后无法搜索图片中的文字,可重新建立;不影响微信原始图片与聊天记录。',
...imageTextIndex
}
]
return {
@@ -89,6 +100,11 @@ export async function clearCache(
await options.beforeClearKnowledge?.()
await fs.remove(KNOWLEDGE_CACHE_DIR)
}
if (scope === 'image-text-index' || scope === 'all') {
// 先停下任务再删库,避免"边写边删"。
await options.beforeClearImageTextIndex?.()
await fs.remove(IMAGE_TEXT_INDEX_CACHE_DIR)
}
return getCacheSummary()
}
+29
View File
@@ -16,6 +16,7 @@ import {
} from '../../shared/windows-runtime'
import { mergeRecallArchiveMessages, recordRecallArchiveMessages } from './recall-archive-service'
import type { ExportImageQuality } from '../../shared/image-quality'
import type { ImageMessageCountProbe } from '../../shared/image-text-index'
import { wcdbDebugLog } from '../wcdb-debug'
import {
buildContactSearchIndex,
@@ -717,6 +718,34 @@ export async function listMessagesForExport(
* batch-selection view, where loading every conversation would make opening
* Settings noticeably slow.
*/
/**
* 图片消息计数探针(SQL 统计,不解密)。
*
* 返回 `count: null` 表示**统计失败**,不是 0 张。调用方必须区分这两件事 ——
* 否则"数不出来"会被显示成"账号里没有图片",用户会因此放弃建立索引。
*/
export async function countImageMessagesAsync(
userMd5: string,
sinceMs?: number
): Promise<ImageMessageCountProbe> {
if (!dbRef) return { count: null, typeColumn: null, error: '微信数据库尚未就绪' }
return dbRef.getWcdb4Client().countImageMessagesAsync(userMd5, sinceMs)
}
/**
* 图片消息的增量水位(条数 + 最大插入序)。
*
* 增量索引**不能只比条数**:召回一张旧图的同时新增一张新图,条数不变但集合变了。
* 返回 null = 当前数据库不支持该统计(调用方须退化成"每轮重扫",宁可慢也不可漏)。
*/
export async function imageConversationWatermarkAsync(
userMd5: string,
sinceMs?: number
): Promise<{ count: number; maxLocalId: number } | null> {
if (!dbRef) return null
return dbRef.getWcdb4Client().imageConversationWatermarkAsync(userMd5, sinceMs)
}
export async function countVoiceMessagesAsync(
userMd5: string,
startTime?: number,
@@ -0,0 +1,970 @@
/**
* 图片文字索引编排服务。
*
* 职责边界(刻意保持单一):
* - 快速统计图片消息数(SQL,**绝不解密**)
* - 按会话 + 批次驱动 OCR;**严格串行(concurrency = 1)**:循环体内只有一次
* `await`,不存在 Promise.all 扇出,且每批之间让出 event loop
* - 维护 checkpoint(可暂停 / 继续 / 取消 / 重启后恢复)
* - 把结果写进派生库,并在**会话完成时**回调,让 Knowledge 重建该会话的索引
*
* 明确不做:
* - 不修改 WCDB / 不写回原始消息 / 不产生任何"OCR 消息"
* - 不实现图片搜索 Agent(检索继续走既有 Query Agent + Knowledge)
* - 不在日志里写 OCR 正文 / 真实图片路径 / wxid / 群名
*/
import { createHash } from 'node:crypto'
import { existsSync } from 'node:fs'
import {
IMAGE_TEXT_INDEX_BATCH_SIZE,
IMAGE_TEXT_INDEX_ENGINE,
IMAGE_OCR_RETRIABLE_FAILURE_STATES,
buildImageOcrArtifactKey,
imageTextProcessedPercent,
isTerminalImageOcrState,
type ImageMessageCountProbe,
type ImageMessageWatermark,
type ImageOcrPersistedState,
type ImageOcrProvenance,
type ImageTextIndexCountResult,
type ImageTextIndexCoverage,
type ImageTextIndexProgress,
type ImageTextIndexRepairResult,
type ImageTextIndexRunState,
type ImageTextIndexStartOptions,
type ImageTextIndexStatus,
type ImageTextIndexStorageStats
} from '../../shared/image-text-index'
import { detectSystemOcrImageFormat, type SystemOcrCapability } from '../../shared/system-ocr'
import type * as chat from './chat-service'
/**
* 消息 id 必须与 Knowledge 写入的 `knowledge_messages.message_id` 完全一致,
* 否则 OCR 文本贴不到消息上、Evidence 也回不到原图。真源见 knowledge/message-identity。
*/
import { sourceMessageId } from '../knowledge/message-identity'
import type { ImageDecryptService } from '../image-decrypt-service'
import {
ImageTextIndexStore,
getImageTextIndexDatabasePath,
removeImageTextIndexDatabase,
type ConversationImageOcrEntry
} from './image-text-index-store'
/** 图片消息的数据 URL 前缀。 */
const MIME_BY_FORMAT: Record<string, string> = {
png: 'image/png',
jpeg: 'image/jpeg',
gif: 'image/gif',
bmp: 'image/bmp',
webp: 'image/webp',
tiff: 'image/tiff'
}
export interface ImageTextIndexServiceDeps {
/** `<userData>/image-text-index`。 */
databaseRoot?: string
/** 当前账号(wxid 优先,退回 accountRoot)。空串 = 微信未就绪。 */
resolveAccountId?: () => string
/** 当前微信数据根目录。 */
resolveAccountRoot?: () => string
listContacts?: () => Promise<Array<{ md5: string; m_nsUsrName: string; type: 'user' | 'group' }>>
listMessages?: (conversationId: string) => Promise<chat.FormattedMessage[]>
/**
* 单个会话的图片消息计数探针(SQL 统计,不解密)。
*
* `count: null` = 统计失败,**不等于 0 张**;调用方必须区分。
*/
countConversationImages?: (
conversationId: string,
sinceMs?: number
) => Promise<ImageMessageCountProbe>
/**
* 单个会话的图片消息增量水位(条数 + 最大插入序),SQL 聚合,不解密。
*
* 返回 null = 当前数据库不支持(调用方必须退化成"每轮重扫",宁可慢也不可漏)。
*/
imageWatermark?: (conversationId: string, sinceMs?: number) => Promise<ImageMessageWatermark | null>
decryptService?: () => ImageDecryptService | null
/** 本地 OCR。 */
recognize?: (imageDataUrl: string) => Promise<{
success: boolean
text: string
language: string | null
errorCode?: string
}>
capability?: () => Promise<SystemOcrCapability>
/** 会话图片全部处理完后回调,用于把 OCR 文本灌进 Knowledge 索引。 */
onConversationIndexed?: (conversationId: string) => Promise<void>
/** 交互查询让路钩子。 */
interactiveIdle?: () => Promise<void>
now?: () => number
}
function sha256Short(value: Uint8Array | string): string {
return createHash('sha256').update(value).digest('hex').slice(0, 32)
}
/** 图片消息判定:与 chat-service 的 `contentData.type === 'image'` 对齐。 */
export function isImageMessage(message: chat.FormattedMessage): boolean {
return message.contentData?.type === 'image'
}
export class ImageTextIndexService {
private deps: ImageTextIndexServiceDeps = {}
private store: ImageTextIndexStore | null = null
private storeAccountId = ''
private accountKey = ''
private running = false
private cancelRequested = false
private pauseRequested = false
private passPromise: Promise<void> | null = null
private counting = false
private listeners = new Set<(status: ImageTextIndexStatus) => void>()
private lastError: string | undefined
private startedAt: number | undefined
/** 上一次清理实际重建(失效)了多少个会话的 Knowledge 索引;用于诊断与测试。 */
lastInvalidatedConversations = 0
/**
* 会话级 OCR 文本缓存。
*
* Knowledge 重建一个会话时会对每条消息问一次 resolver;不加缓存就是每条消息一次 SQL。
* 写入 binding 时精确失效该会话,保证不会读到旧结果。
*/
private conversationOcrCache = new Map<string, Map<string, ConversationImageOcrEntry>>()
/** 本轮 pass 的计数器(内存态;真实来源始终是派生库)。 */
private counters = {
totalImageMessages: 0,
processedThisPass: 0,
indexed: 0,
empty: 0,
missing: 0,
failed: 0
}
private runState: ImageTextIndexRunState = 'idle'
bind(deps: ImageTextIndexServiceDeps): void {
this.deps = { ...this.deps, ...deps }
}
private now(): number {
return this.deps.now ? this.deps.now() : Date.now()
}
// ------------------------------------------------------------- store 生命周期
private resolveAccountId(): string {
return this.deps.resolveAccountId?.() || ''
}
private ensureStore(): ImageTextIndexStore | null {
const root = this.deps.databaseRoot
const accountId = this.resolveAccountId()
if (!root || !accountId) return null
if (this.store && this.storeAccountId === accountId) return this.store
this.store?.close()
const key = getImageTextIndexDatabasePath(root, accountId)
this.store = new ImageTextIndexStore(key, accountId)
this.storeAccountId = accountId
this.accountKey = key
return this.store
}
/**
* 账号切换 / 数据库切换时丢弃句柄。
*
* 派生库按 accountId 分目录,句柄必须跟着换;否则会把 A 账号的 OCR
* 写到 B 账号,或让新库读到旧账号的 coverage。
*/
resetAccount(): void {
this.conversationOcrCache.clear()
this.store?.close()
this.store = null
this.storeAccountId = ''
this.accountKey = ''
this.counters = {
totalImageMessages: 0,
processedThisPass: 0,
indexed: 0,
empty: 0,
missing: 0,
failed: 0
}
this.runState = 'idle'
this.cancelRequested = false
this.pauseRequested = false
this.lastError = undefined
}
// ------------------------------------------------------------------- 只读接口
/** Knowledge 索引时用:把某会话的 OCR 文本贴到消息上(与语音 resolver 同构)。 */
getConversationOcr(conversationId: string): Map<string, ConversationImageOcrEntry> {
const cached = this.conversationOcrCache.get(conversationId)
if (cached) return cached
const store = this.ensureStore()
if (!store) return new Map()
const result = store.getConversationOcr(conversationId)
this.conversationOcrCache.set(conversationId, result)
return result
}
/**
* 覆盖度。
*
* **分母只能来自落盘的 SQL 统计**,不能从派生库自己推:派生库只知道自己处理过什么。
* 如果按 `processed + pending` 反推 total,应用重启后 pending 无处可来,
* total 就会退化成 processed —— 30% 的部分索引会被谎报成"已覆盖全部"。
* 这正是 §18 禁止的"把 partial coverage 当 complete"。
*/
private coverageFromCounts(counts: Record<string, number>): ImageTextIndexCoverage {
const indexed = counts['indexed'] ?? 0
const empty = counts['empty'] ?? 0
const missing = (counts['image_missing'] ?? 0) + (counts['metadata_missing'] ?? 0)
const failed =
(counts['decrypt_failed'] ?? 0) +
(counts['decode_failed'] ?? 0) +
(counts['ocr_failed'] ?? 0) +
(counts['cancelled'] ?? 0)
const runtimeUnavailable = counts['decrypt_unavailable'] ?? 0
// 运行时不可用**不计入 processed**:它不是"这条图片已经处理过了"。
const processed = indexed + empty + missing + failed
const counted = this.store?.readCountedTotal() ?? null
// 内存计数器只在本轮 pass 内比落盘值更新(刚统计完、尚未落盘的窗口)。
const total =
this.counters.totalImageMessages || counted?.total || processed + runtimeUnavailable
/**
* 系统性失败:处理过一批,但一条都没能给出确定结果。
*
* 这正是本次事故的形态(45,479 张全部失败,成功 / 无文字 / 缺失都是 0)。
* 它必须阻断 `complete` —— 否则 Query Agent 会拿着"覆盖完整"去回答"没有"。
*/
const systemicFailure = processed > 0 && indexed === 0 && empty === 0 && missing === 0
return {
totalImageMessages: total,
processed,
indexed,
empty,
missing,
failed,
runtimeUnavailable,
pending: Math.max(0, total - processed - runtimeUnavailable),
// 从未统计过总数 → 不算"已建立":不知道分母就不允许声称覆盖。
established: counted !== null && (processed > 0 || runtimeUnavailable > 0),
// 分母不完整、有 pending、有运行时不可用、或"全军覆没" → 都不算 complete。
complete:
counted !== null &&
counted.complete &&
total > 0 &&
runtimeUnavailable === 0 &&
!systemicFailure &&
processed >= total,
systemicFailure,
countedAt: counted?.countedAt ?? null
}
}
/**
* 覆盖度快照(只读、同步),供 Query Agent 在工具结果里携带图片覆盖度。
*
* 刻意**不建库**:只因为用户问了一句话就凭空创建一个派生库是没道理的。
* 库不存在 = 从未建立过索引 = `not_built`。
*/
getCoverageSnapshot(): ImageTextIndexCoverage | null {
const root = this.deps.databaseRoot
const accountId = this.resolveAccountId()
if (!root || !accountId) return null
if (!this.store && !existsSync(getImageTextIndexDatabasePath(root, accountId))) {
return null
}
const store = this.ensureStore()
if (!store) return null
return this.coverageFromCounts(store.countByState())
}
private progressFromCounts(counts: Record<string, number>): ImageTextIndexProgress {
const coverage = this.coverageFromCounts(counts)
const total = coverage.totalImageMessages
const percent = imageTextProcessedPercent(coverage.processed, total)
return {
state: this.runState,
totalImageMessages: total,
processed: coverage.processed,
indexed: coverage.indexed,
empty: coverage.empty,
missing: coverage.missing,
failed: coverage.failed,
runtimeUnavailable: coverage.runtimeUnavailable,
systemicFailure: coverage.systemicFailure,
pending: coverage.pending,
percent,
processedPercent: percent,
...(this.startedAt ? { startedAt: this.startedAt } : {}),
updatedAt: this.now(),
cancellable: this.running,
paused: this.runState === 'paused',
...(this.lastError ? { lastError: this.lastError } : {})
}
}
private emptyStorage(): ImageTextIndexStorageStats {
return { indexedImages: 0, ocrTextCount: 0, totalBytes: 0, updatedAt: null }
}
async getStatus(): Promise<ImageTextIndexStatus> {
const store = this.ensureStore()
if (!store) {
return {
progress: {
state: this.runState,
totalImageMessages: this.counters.totalImageMessages,
processed: 0,
indexed: 0,
empty: 0,
missing: 0,
failed: 0,
runtimeUnavailable: 0,
systemicFailure: false,
pending: 0,
percent: 0,
processedPercent: 0,
updatedAt: this.now(),
cancellable: false,
paused: false
},
coverage: {
totalImageMessages: 0,
processed: 0,
indexed: 0,
empty: 0,
missing: 0,
failed: 0,
runtimeUnavailable: 0,
pending: 0,
established: false,
complete: false,
systemicFailure: false,
countedAt: null
},
storage: this.emptyStorage(),
counting: this.counting
}
}
const counts = store.countByState()
return {
progress: this.progressFromCounts(counts),
coverage: this.coverageFromCounts(counts),
storage: store.storageStats(),
counting: this.counting
}
}
onStatusChange(listener: (status: ImageTextIndexStatus) => void): () => void {
this.listeners.add(listener)
return () => this.listeners.delete(listener)
}
private async emit(): Promise<void> {
if (!this.listeners.size) return
const status = await this.getStatus()
for (const listener of this.listeners) {
try {
listener(status)
} catch {
// 监听器异常不得影响索引。
}
}
}
// --------------------------------------------------------------------- 统计
/**
* 快速统计当前账号的图片消息数。
*
* 走 SQL COUNT(`local_type & 65535 = 3`),**不解密任何图片** —— 这是
* 「点击索引前先告诉用户有多少张」能够足够快的前提。
*/
async countImageMessages(sinceMs?: number): Promise<ImageTextIndexCountResult> {
const startedAt = this.now()
const contacts = await (this.deps.listContacts?.() ?? Promise.resolve([]))
let total = 0
let scanned = 0
let failed = 0
let typeColumn: string | null = null
let firstError: string | undefined
for (const contact of contacts) {
const probe = await (this.deps.countConversationImages?.(contact.md5, sinceMs) ??
Promise.resolve<ImageMessageCountProbe>({
count: null,
typeColumn: null,
error: '未接入图片消息统计能力'
}))
if (probe.typeColumn && !typeColumn) typeColumn = probe.typeColumn
if (probe.count === null) {
// **统计失败不是 0 张**:必须单独计数,否则 UI 会把"数不出来"说成"没有图片"。
failed += 1
if (!firstError) firstError = probe.error
continue
}
scanned += 1
total += probe.count
}
this.counters.totalImageMessages = total
// 落盘:coverage 的分母必须能被重启后读到(见 coverageFromCounts)。
// 只要有一个会话没数上,分母就是偏小的 → 标记为不完整,coverage 拿不到 complete。
this.ensureStore()?.writeCountedTotal({
total,
countedAt: this.now(),
complete: contacts.length > 0 && failed === 0
})
return {
totalImageMessages: total,
scannedConversations: scanned,
failedConversations: failed,
typeColumn,
...(firstError ? { error: firstError } : {}),
durationMs: this.now() - startedAt
}
}
// ------------------------------------------------------------------ 单张处理
private async processOne(
message: chat.FormattedMessage,
conversationId: string,
provenance: ImageOcrProvenance
): Promise<{ state: ImageOcrPersistedState; text: string; imageIdentity: string | null }> {
const imageContent =
message.contentData?.type === 'image'
? (message.contentData as { md5?: string; datName?: string })
: undefined
const decrypt = this.deps.decryptService?.() ?? null
/**
* 解密服务缺失是**运行时**问题,不是这张图片的问题。
*
* 本次事故就是它:`imageDecryptService` 只在用户点开某张图时才懒加载,
* 于是全量回填 45,479 张全部落成 `decrypt_failed` —— 数字看着像"图片坏了",
* 实际是流水线前置依赖没接上。这里必须用独立状态,绝不能与真正的解密失败混为一谈。
*/
if (!decrypt) return { state: 'decrypt_unavailable', text: '', imageIdentity: null }
// 图片消息缺少定位字段:连"去哪找文件"都不知道,属消息侧缺失而非 OCR 失败。
if (!imageContent?.md5 && !imageContent?.datName) {
return { state: 'metadata_missing', text: '', imageIdentity: null }
}
let datPath: string | null = null
try {
datPath = decrypt.findImageFile(imageContent?.md5, imageContent?.datName, {
accountDir: this.deps.resolveAccountRoot?.() || undefined,
sessionMd5: conversationId,
createTime: message.createTime,
allowThumbnail: true,
preferThumbnail: true
})
} catch {
datPath = null
}
// 微信清理过原图与缩略图 —— 这是正常情况,不是任务级错误。
if (!datPath) return { state: 'image_missing', text: '', imageIdentity: null }
let bytes: Buffer | null = null
try {
bytes = decrypt.decryptImage(datPath)
} catch {
bytes = null
}
if (!bytes || bytes.length === 0) {
return { state: 'decrypt_failed', text: '', imageIdentity: null }
}
const imageIdentity = `sha256:${sha256Short(bytes)}`
const format = detectSystemOcrImageFormat(bytes)
// 解密"没抛错"但产出不是图片 → 解码失败,不是 OCR 失败。
if (!format) return { state: 'decode_failed', text: '', imageIdentity }
const store = this.ensureStore()
const artifactKey = buildImageOcrArtifactKey({ imageIdentity, provenance })
// 同一张图(可能被转发到多个会话)已经算过 → 直接复用,绝不重复 OCR。
const cached = store?.getArtifact(artifactKey) ?? null
if (cached && isTerminalImageOcrState(cached.state)) {
return { state: cached.state, text: cached.text, imageIdentity }
}
const mime = MIME_BY_FORMAT[format] ?? 'image/png'
let state: ImageOcrPersistedState = 'ocr_failed'
let text = ''
let errorCode: string | undefined
try {
const result = await (this.deps.recognize?.(`data:${mime};base64,${bytes.toString('base64')}`) ??
Promise.resolve({ success: false, text: '', language: null, errorCode: 'OCR_FAILED' }))
if (result.success && result.text.trim()) {
state = 'indexed'
text = result.text
} else if (result.success || result.errorCode === 'OCR_EMPTY_RESULT') {
// 表情包 / 风景 / 头像 —— 没有文字是**正常终态**,不重试。
state = 'empty'
} else {
state = 'ocr_failed'
errorCode = result.errorCode
}
} catch {
state = 'ocr_failed'
}
const now = this.now()
if (store) {
store.putArtifact({
accountId: this.storeAccountId,
artifactKey,
imageIdentity,
state,
text,
charCount: text.length,
engine: provenance.engine,
platform: provenance.platform,
runtimeVersion: provenance.runtimeVersion,
language: provenance.language,
...(errorCode ? { errorCode } : {}),
createdAt: now,
updatedAt: now
})
}
return { state, text, imageIdentity }
}
// --------------------------------------------------------------------- pass
/**
* 启动一次 pass。
*
* 「暂停 / 继续」刻意实现为「停止 + 重新跑一次 pass」而不是原地挂起:
* - checkpoint(scan_state)与 artifact 缓存都在库里,重跑会跳过已完成会话、
* 并且命中 artifact 缓存不再重复 OCR,所以恢复成本很低;
* - 与项目既有的「中断后重跑、靠 checkpoint 续做」语义一致,不引入新的挂起状态机。
*/
startPass(options: ImageTextIndexStartOptions = {}): { started: boolean; state: ImageTextIndexRunState } {
if (this.running) return { started: false, state: this.runState }
this.cancelRequested = false
this.pauseRequested = false
this.lastError = undefined
this.startedAt = this.now()
this.runState = 'running'
this.passPromise = this.runPass(options)
.catch((error) => {
this.lastError = error instanceof Error ? error.message : String(error)
this.runState = 'error'
})
.finally(() => {
this.running = false
this.passPromise = null
void this.emit()
})
void this.emit()
return { started: true, state: this.runState }
}
private async runPass(options: ImageTextIndexStartOptions): Promise<void> {
const store = this.ensureStore()
if (!store) {
this.lastError = '微信数据尚未就绪'
this.runState = 'error'
return
}
this.running = true
const capability = (await this.deps.capability?.()) ?? null
if (capability && !capability.available) {
this.lastError = '当前系统不支持本地图片文字识别'
this.runState = 'error'
return
}
/**
* **前置依赖自检(本次事故的根因防线)**:解密服务必须可用。
*
* 没有它,每张图片都会在 `processOne` 的第一步失败。原实现会把"整条流水线
* 根本跑不起来"这件事落成 45,479 条 `decrypt_failed` —— 既污染派生库,
* 又让用户以为自己的图片坏了,还让 coverage 看起来"都处理完了"。
*
* 所以必须在**写任何一条记录之前**停下来:宁可一次都不跑,也不要写一堆假失败。
*/
if (!this.deps.decryptService?.()) {
this.lastError = '图片解密服务尚未就绪,无法读取微信图片;本次未写入任何记录。'
this.runState = 'error'
return
}
const provenance: ImageOcrProvenance = {
engine: capability?.engine ?? IMAGE_TEXT_INDEX_ENGINE,
platform: capability?.platform ?? process.platform,
runtimeVersion: capability?.runtimeVersion ?? null,
language: capability?.language ?? null
}
// 统计一次总数(SQL),进度百分比才有真实分母。
this.counting = true
try {
await this.countImageMessages(options.sinceMs)
} finally {
this.counting = false
}
let contacts = await (this.deps.listContacts?.() ?? Promise.resolve([]))
if (options.conversationLimit && options.conversationLimit > 0) {
contacts = contacts.slice(0, options.conversationLimit)
}
const scanState = store.readScanState()
let budget = options.messageLimit && options.messageLimit > 0 ? options.messageLimit : Infinity
for (const contact of contacts) {
if (this.cancelRequested || this.pauseRequested) break
if (budget <= 0) break
const conversationId = contact.md5
/**
* 是否只处理一个时间窗口(用于小样本验证)。
*
* 带窗口时**不做增量跳过**:checkpoint 是围绕全量集合建立的,
* 窗口内的图片可能从未被处理过,继续按"该会话已完成"跳过会让窗口形同虚设。
*/
const windowed = Boolean(options.sinceMs && options.sinceMs > 0)
// 增量水位 = 条数 + 最大插入序(§2)。只比条数会漏掉「撤回一张旧图 +
// 新增一张新图」这种总数不变、集合却变了的会话。
const watermark = await (this.deps.imageWatermark?.(conversationId, options.sinceMs) ??
Promise.resolve(null))
const imageTotal =
watermark?.count ??
(await this.deps.countConversationImages?.(conversationId, options.sinceMs))?.count ??
0
if (imageTotal === 0) {
store.writeScanState({
conversationId,
state: 'done',
imageTotal: 0,
imageProcessed: 0,
maxLocalId: watermark?.maxLocalId ?? 0
})
continue
}
// 增量:会话已完成且**水位完全未变** → 不读 WCDB、不 OCR。
// 水位不可用时(数据库不支持该聚合)一律重扫:宁可慢,不可漏。
const previous = scanState.get(conversationId)
if (
!windowed &&
watermark &&
previous &&
previous.state === 'done' &&
previous.imageTotal === watermark.count &&
previous.maxLocalId === watermark.maxLocalId
) {
continue
}
await this.deps.interactiveIdle?.()
let messages: chat.FormattedMessage[] = []
try {
messages = await (this.deps.listMessages?.(conversationId) ?? Promise.resolve([]))
} catch {
messages = []
}
const imageMessages = messages
.filter(isImageMessage)
// 时间窗过滤:小样本验证时只看窗口内的图片,不然还是在跑全量。
.filter((message) =>
windowed ? (message.createTime || 0) * 1000 >= (options.sinceMs as number) : true
)
if (!imageMessages.length) {
store.writeScanState({
conversationId,
state: 'done',
imageTotal: 0,
imageProcessed: 0,
maxLocalId: 0
})
continue
}
// 水位取**实际读到的**消息里最大的 local_id,而不是源侧水位:
// 万一在我们查水位之后、读消息之前又落了一条新图,用观测值会让下一轮
// 发现"源水位更高"从而重扫(安全);用源侧水位则会把它永久跳过(漏索引)。
const observedMaxLocalId = imageMessages.reduce(
(max, message) => Math.max(max, Number(message.localId) || 0),
0
)
const ocrByMessage = store.getConversationOcr(conversationId)
let processedInConversation = 0
let interrupted = false
for (let index = 0; index < imageMessages.length; index += IMAGE_TEXT_INDEX_BATCH_SIZE) {
if (this.cancelRequested || this.pauseRequested) {
interrupted = true
break
}
const batch = imageMessages.slice(index, index + IMAGE_TEXT_INDEX_BATCH_SIZE)
for (const message of batch) {
if (budget <= 0) break
const messageId = sourceMessageId(message)
// 派生库里已有终态结果 → 复用(含"无文字"/"图片缺失"),不重复劳动。
const known = ocrByMessage.get(messageId)
if (known && isTerminalImageOcrState(known.state)) {
processedInConversation += 1
continue
}
const outcome = await this.processOne(message, conversationId, provenance)
const now = this.now()
store.putBinding({
accountId: this.storeAccountId,
conversationId,
messageId,
createTime: (message.createTime || 0) * 1000,
...(message.senderId || message.from ? { senderId: message.senderId || message.from } : {}),
...(message.isSender ? { senderName: '我' } : message.name ? { senderName: message.name } : {}),
imageIdentity: outcome.imageIdentity ?? '',
artifactKey: outcome.imageIdentity
? buildImageOcrArtifactKey({ imageIdentity: outcome.imageIdentity, provenance })
: buildImageOcrArtifactKey({ imageIdentity: 'unavailable', provenance }),
state: outcome.state,
updatedAt: now
})
this.conversationOcrCache.delete(conversationId)
processedInConversation += 1
this.counters.processedThisPass += 1
if (outcome.state === 'indexed') this.counters.indexed += 1
else if (outcome.state === 'empty') this.counters.empty += 1
else if (outcome.state === 'image_missing') this.counters.missing += 1
else this.counters.failed += 1
budget -= 1
}
// 批次之间让出 event loop:交互查询 / UI 永远优先于后台历史 OCR。
await new Promise<void>((resolve) => setImmediate(resolve))
await this.emit()
}
if (interrupted) {
store.writeScanState({
conversationId,
state: 'partial',
imageTotal: imageMessages.length,
imageProcessed: processedInConversation,
maxLocalId: observedMaxLocalId
})
break
}
store.writeScanState({
conversationId,
state: 'done',
imageTotal: imageMessages.length,
imageProcessed: processedInConversation,
maxLocalId: observedMaxLocalId
})
// 会话的图片都处理完了 → 让 Knowledge 重建这个会话,OCR 文本才可被搜索。
try {
await this.deps.onConversationIndexed?.(conversationId)
} catch {
// 索引回调失败不应中断 OCR:派生文本已经落库,下一遍还会再灌。
}
await this.emit()
}
if (this.cancelRequested) this.runState = 'cancelled'
else if (this.pauseRequested) this.runState = 'paused'
else this.runState = 'completed'
this.running = false
await this.emit()
}
// --------------------------------------------------------------- 控制接口
pause(): { paused: boolean; state: ImageTextIndexRunState } {
if (!this.running) return { paused: false, state: this.runState }
this.pauseRequested = true
return { paused: true, state: 'paused' }
}
resume(options: ImageTextIndexStartOptions = {}): { started: boolean; state: ImageTextIndexRunState } {
if (this.running) return { started: false, state: this.runState }
return this.startPass(options)
}
async cancel(): Promise<{ cancellable: boolean; cancelled: boolean }> {
if (!this.running) return { cancellable: false, cancelled: false }
this.cancelRequested = true
const pending = this.passPromise
if (pending) await pending.catch(() => undefined)
return { cancellable: true, cancelled: true }
}
isRunning(): boolean {
return this.running
}
// ------------------------------------------------------------------- 清理
/**
* 清理「图片文字索引能力」的全部派生数据。
*
* 只删本能力自己生成的东西:artifact(OCR 文本)、binding、checkpoint、库文件。
* 明确**不碰**:WCDB、微信图片、图片解密密钥、普通文字知识库、语音转写、聊天消息、Agent 配置。
*/
async clear(): Promise<{ removed: boolean; removedBytes: number }> {
await this.cancel()
this.conversationOcrCache.clear()
const store = this.ensureStore()
/**
* **必须在删之前**记下受影响会话。
*
* OCR 派生文本已经通过 normalizer 进了 Knowledge 的 chunks / FTS。
* 只删派生库、不做这一步,用户执行「清理图片文字索引」之后**仍然能搜到图片里的文字** ——
* 那就等于"清理成功"是假的。硬条件:清理图片文字索引 ≠ 只删 OCR SQLite。
*/
const affectedConversations = store?.conversationIdsWithOcr() ?? []
let removedBytes = 0
if (store) {
removedBytes = store.storageStats().totalBytes
store.clearDerivedData()
// 先折 WAL 再关连接,然后才允许删文件(见 store.close / removeImageTextIndexDatabase)。
store.close()
this.store = null
this.storeAccountId = ''
}
const databasePath = this.accountKey
this.accountKey = ''
const removal = databasePath
? removeImageTextIndexDatabase(databasePath)
: { removed: true, leftovers: [] as string[] }
// 总数统计也一并作废:下次回到「未建立」时重新 COUNT(*),
// 否则 UI 会拿着一个已经没有任何派生数据支撑的旧分母。
this.counters = {
totalImageMessages: 0,
processedThisPass: 0,
indexed: 0,
empty: 0,
missing: 0,
failed: 0
}
this.runState = 'idle'
this.startedAt = undefined
await this.emit()
/**
* 逐个重建**受影响的会话**,让 OCR 派生文本从 Knowledge 里消失。
*
* 刻意不做两件更省事但更糟的事:
* - 不清空整个 Knowledge(那会连普通文字消息的索引一起丢掉);
* - 不假装"删了文件就等于清理完成"(chunks/FTS 里还留着旧文字)。
*
* rebuilt 这里已经返回空(派生库已删、resolver 拿不到 OCR),所以重建出来的
* 会话副本天然不含 OCR 文本;`completeSnapshot` 会把旧的 chunk 一起替换掉。
*/
let invalidatedConversations = 0
for (const conversationId of affectedConversations) {
try {
await this.deps.onConversationIndexed?.(conversationId)
invalidatedConversations += 1
} catch {
// 单个会话重建失败不应让清理整体失败:派生数据已经删了,
// 下一遍索引也会因为 resolver 返回空而自然收敛。
}
}
this.lastInvalidatedConversations = invalidatedConversations
/**
* 重建过程中 Knowledge 会通过 resolver 调 `getConversationOcr`,
* 那会把派生库**重新打开**(`ensureStore`)。清理完必须再收干净:
* 否则「清理成功」之后还留着一个空库句柄,Windows 上也会妨碍目录删除。
*/
this.store?.close()
this.store = null
this.storeAccountId = ''
this.accountKey = ''
// `removed: false` = 文件仍被占用没删掉,必须如实上报,不能假装清理成功。
return { removed: removal.removed, removedBytes }
}
/**
* 缓存清理前先停下任务,并**把 Knowledge 里的 OCR 派生文本一起失效**。
*
* 直接复用 `clear()` 而不是只 `cancel()`:缓存清理(含"清理全部")同样会删掉派生库,
* 如果这里不顺手重建 Knowledge,用户会得到一个自相矛盾的状态 ——
* 「问问微信」里搜得到图片文字,但派生库明明已经没了。
*/
async prepareForCacheClear(): Promise<void> {
await this.clear()
}
/**
* 派生索引修复(Derived Index Repair)。
*
* 只重建 **L3(Knowledge 派生条目 / chunks / FTS)**,数据来源是已有的
* L2 binding + L1 artifact。**绝不**读原图、解密或调用 OCR 引擎 ——
* L1 是几万张图片堆出来的昂贵产物,修一个索引问题不该让它重算一遍。
*
* `ocrExecutions: 0` 不是"期望",而是这条路径的定义:类型上写死成字面量 0,
* 任何让它变成非 0 的改动都会直接编译失败。
*/
async repairKnowledgeIndex(
options: { conversationLimit?: number } = {}
): Promise<ImageTextIndexRepairResult> {
const startedAt = this.now()
// 运行中不并发重建:pass 正在写 binding,同时重建会让 Knowledge 读到半程状态。
if (this.running) {
return { conversations: 0, ocrExecutions: 0, durationMs: 0, skipped: true }
}
const store = this.ensureStore()
if (!store) {
return { conversations: 0, ocrExecutions: 0, durationMs: this.now() - startedAt, skipped: false }
}
const limit = options.conversationLimit && options.conversationLimit > 0 ? options.conversationLimit : undefined
const conversationIds = limit
? store.conversationIdsWithIndexedOcr().slice(0, limit)
: store.conversationIdsWithIndexedOcr()
let conversations = 0
for (const conversationId of conversationIds) {
try {
await this.deps.onConversationIndexed?.(conversationId)
conversations += 1
} catch {
// 单个会话重建失败不影响其余:修索引是尽力而为的廉价操作,可重试。
}
}
return {
conversations,
ocrExecutions: 0,
durationMs: this.now() - startedAt,
skipped: false
}
}
/**
* 重置**可重试的失败记录**(代码修好之后重跑用)。
*
* 刻意不做成"清空整个派生库":那会连已经成功的 OCR 记录一起丢掉,
* 用户要为此重新跑几万张图片。这里只删失败绑定 + 它们的 checkpoint,
* `indexed` / `empty` 一条不动,下一轮 pass 自然接上。
*/
async resetRetriableFailures(): Promise<{ reset: number }> {
await this.cancel()
this.conversationOcrCache.clear()
const store = this.ensureStore()
if (!store) return { reset: 0 }
const reset = store.resetFailures([...IMAGE_OCR_RETRIABLE_FAILURE_STATES])
await this.emit()
return { reset }
}
/** 缓存清理前先停下任务,避免边删边写。 */
}
export const imageTextIndexService = new ImageTextIndexService()
+572
View File
@@ -0,0 +1,572 @@
/**
* 图片文字索引的**派生存储**(与 Knowledge 派生库物理分离)。
*
* 为什么单独一个库而不是往 knowledge.sqlite 里加表:
* - 清理语义干净:整个能力 = 三个文件(.sqlite/-wal/-shm),删掉即可,不留残渣。
* - 零迁移风险:不动已发布的 knowledge schema(§26 要求升级不破坏既有派生库)。
* - 去重语义天然:artifact 按「图片内容 + OCR 运行时指纹」唯一,binding 承担多来源。
*
* 账号隔离与 Knowledge 一致:路径按 accountId 摘要分目录 + 库内 account_id 自证。
*/
import { createHash } from 'node:crypto'
import { mkdirSync, rmSync, statSync, existsSync } from 'node:fs'
import { dirname, join, resolve } from 'node:path'
import { DatabaseSync } from 'node:sqlite'
import {
IMAGE_TEXT_INDEX_SCHEMA_VERSION,
type ImageOcrArtifact,
type ImageOcrBinding,
type ImageOcrPersistedState,
type ImageTextIndexStorageStats
} from '../../shared/image-text-index'
const MAX_SAFE_ACCOUNT_SEGMENT = /^[a-f0-9]{32}$/
function sha256Hex(value: string): string {
return createHash('sha256').update(value).digest('hex')
}
/** 派生库目录名不直接暴露 accountId。 */
export function imageTextIndexAccountKey(accountId: string): string {
return sha256Hex(`image-text-index-account-v1:${accountId}`).slice(0, 32)
}
export function getImageTextIndexDatabasePath(databaseRoot: string, accountId: string): string {
const accountKey = imageTextIndexAccountKey(accountId)
if (!MAX_SAFE_ACCOUNT_SEGMENT.test(accountKey)) {
throw new Error('Invalid image text index account key')
}
return join(resolve(databaseRoot), accountKey, 'image-text-index.sqlite')
}
/**
* 精确删除三件套(与 Knowledge 的 removeKnowledgeDatabase 同构)。
*
* **删除后必须回验**:Windows 上只要还有句柄(WAL/SHM 未关干净、别的进程打开了库),
* `rmSync` 可能不报错却没真的删掉 —— 那就是"看似清理成功,实际没删"。
* 这里把没删掉的路径返回给调用方,让上层能如实报告失败,而不是假装成功。
*/
export function removeImageTextIndexDatabase(databasePath: string): {
removed: boolean
leftovers: string[]
} {
const leftovers: string[] = []
for (const suffix of ['', '-wal', '-shm']) {
const target = `${databasePath}${suffix}`
if (!existsSync(target)) continue
try {
rmSync(target, { force: true })
} catch {
// 删除失败(典型原因是文件仍被占用)→ 由下面的回验兜住。
}
if (existsSync(target)) leftovers.push(target)
}
return { removed: leftovers.length === 0, leftovers }
}
function asRows(value: unknown): Record<string, unknown>[] {
return Array.isArray(value) ? (value as Record<string, unknown>[]) : []
}
function artifactFromRow(row: Record<string, unknown>): ImageOcrArtifact {
return {
accountId: String(row.account_id),
artifactKey: String(row.artifact_key),
imageIdentity: String(row.image_identity),
state: String(row.state) as ImageOcrPersistedState,
text: String(row.text ?? ''),
charCount: Number(row.char_count ?? 0),
engine: String(row.engine),
platform: String(row.platform),
runtimeVersion: row.runtime_version ? String(row.runtime_version) : null,
language: row.language ? String(row.language) : null,
...(row.error_code ? { errorCode: String(row.error_code) } : {}),
createdAt: Number(row.created_at),
updatedAt: Number(row.updated_at)
}
}
/** 会话内「消息 → OCR 文本」,供 Knowledge 索引时解析(与语音 resolver 同构)。 */
export interface ConversationImageOcrEntry {
state: ImageOcrPersistedState
text: string
}
export class ImageTextIndexStore {
private readonly database: DatabaseSync
constructor(
private readonly databasePath: string,
private readonly accountId: string
) {
mkdirSync(dirname(databasePath), { recursive: true })
this.database = new DatabaseSync(databasePath)
this.initialize()
}
private initialize(): void {
this.database.exec(`
PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;
PRAGMA busy_timeout = 5000;
CREATE TABLE IF NOT EXISTS image_ocr_meta (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
) STRICT;
CREATE TABLE IF NOT EXISTS image_ocr_artifacts (
artifact_key TEXT PRIMARY KEY,
account_id TEXT NOT NULL,
image_identity TEXT NOT NULL,
state TEXT NOT NULL,
text TEXT NOT NULL,
char_count INTEGER NOT NULL DEFAULT 0,
engine TEXT NOT NULL,
platform TEXT NOT NULL,
runtime_version TEXT,
language TEXT,
error_code TEXT,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
) STRICT;
CREATE INDEX IF NOT EXISTS image_ocr_artifacts_identity
ON image_ocr_artifacts (image_identity);
CREATE TABLE IF NOT EXISTS image_ocr_bindings (
conversation_id TEXT NOT NULL,
message_id TEXT NOT NULL,
account_id TEXT NOT NULL,
create_time INTEGER NOT NULL,
sender_id TEXT,
sender_name TEXT,
image_identity TEXT NOT NULL,
artifact_key TEXT NOT NULL,
state TEXT NOT NULL,
updated_at INTEGER NOT NULL,
PRIMARY KEY (conversation_id, message_id)
) STRICT;
CREATE INDEX IF NOT EXISTS image_ocr_bindings_identity
ON image_ocr_bindings (image_identity);
CREATE INDEX IF NOT EXISTS image_ocr_bindings_state
ON image_ocr_bindings (state);
-- 每个会话的扫描 checkpoint:重启后据此跳过已完成的会话。
CREATE TABLE IF NOT EXISTS image_ocr_scan_state (
conversation_id TEXT PRIMARY KEY,
account_id TEXT NOT NULL,
state TEXT NOT NULL,
image_total INTEGER NOT NULL DEFAULT 0,
image_processed INTEGER NOT NULL DEFAULT 0,
image_max_local_id INTEGER NOT NULL DEFAULT 0,
updated_at INTEGER NOT NULL
) STRICT;
`)
// 探测式加列(与 Knowledge 一致):旧库缺列时补上,不做版本号比较。
const bindingColumns = new Set(
asRows(this.database.prepare('PRAGMA table_info(image_ocr_bindings)').all()).map((row) =>
String(row.name)
)
)
if (!bindingColumns.has('artifact_key')) {
this.database.exec(
"ALTER TABLE image_ocr_bindings ADD COLUMN artifact_key TEXT NOT NULL DEFAULT ''"
)
}
const scanColumns = new Set(
asRows(this.database.prepare('PRAGMA table_info(image_ocr_scan_state)').all()).map((row) =>
String(row.name)
)
)
if (!scanColumns.has('image_max_local_id')) {
// 旧库补列后默认 0:等于「水位未知」,下一次 pass 会重扫该会话并写入真实水位。
this.database.exec(
'ALTER TABLE image_ocr_scan_state ADD COLUMN image_max_local_id INTEGER NOT NULL DEFAULT 0'
)
}
const storedAccount = this.readMeta('account_id')
if (storedAccount && storedAccount !== this.accountId) {
throw new Error('Image text index account isolation check failed')
}
if (!storedAccount) this.writeMeta('account_id', this.accountId)
if (!this.readMeta('schema_version')) {
this.writeMeta('schema_version', String(IMAGE_TEXT_INDEX_SCHEMA_VERSION))
}
}
private readMeta(key: string): string | null {
const row = this.database
.prepare('SELECT value FROM image_ocr_meta WHERE key = ?')
.get(key) as Record<string, unknown> | undefined
return row ? String(row.value) : null
}
private writeMeta(key: string, value: string): void {
this.database
.prepare(
'INSERT INTO image_ocr_meta (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value'
)
.run(key, value)
}
// ---------------------------------------------------------------- artifacts
getArtifact(artifactKey: string): ImageOcrArtifact | null {
const row = this.database
.prepare('SELECT * FROM image_ocr_artifacts WHERE artifact_key = ?')
.get(artifactKey) as Record<string, unknown> | undefined
return row ? artifactFromRow(row) : null
}
putArtifact(artifact: ImageOcrArtifact): void {
if (artifact.accountId !== this.accountId) {
throw new Error('Image OCR artifact account does not match database')
}
const existing = this.getArtifact(artifact.artifactKey)
this.database
.prepare(
`INSERT INTO image_ocr_artifacts (
artifact_key, account_id, image_identity, state, text, char_count,
engine, platform, runtime_version, language, error_code, created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(artifact_key) DO UPDATE SET
state = excluded.state,
text = excluded.text,
char_count = excluded.char_count,
runtime_version = excluded.runtime_version,
language = excluded.language,
error_code = excluded.error_code,
updated_at = excluded.updated_at`
)
.run(
artifact.artifactKey,
artifact.accountId,
artifact.imageIdentity,
artifact.state,
artifact.text,
artifact.charCount,
artifact.engine,
artifact.platform,
artifact.runtimeVersion,
artifact.language,
artifact.errorCode ?? null,
existing?.createdAt ?? artifact.createdAt,
artifact.updatedAt
)
}
// ----------------------------------------------------------------- bindings
/** 写入绑定;同一 OCR 结果可被多个会话/消息引用。 */
putBinding(binding: ImageOcrBinding): void {
if (binding.accountId !== this.accountId) {
throw new Error('Image OCR binding account does not match database')
}
this.database
.prepare(
`INSERT INTO image_ocr_bindings (
conversation_id, message_id, account_id, create_time, sender_id, sender_name,
image_identity, artifact_key, state, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(conversation_id, message_id) DO UPDATE SET
create_time = excluded.create_time,
sender_id = excluded.sender_id,
sender_name = excluded.sender_name,
image_identity = excluded.image_identity,
artifact_key = excluded.artifact_key,
state = excluded.state,
updated_at = excluded.updated_at`
)
.run(
binding.conversationId,
binding.messageId,
binding.accountId,
binding.createTime,
binding.senderId ?? null,
binding.senderName ?? null,
binding.imageIdentity,
binding.artifactKey,
binding.state,
binding.updatedAt
)
}
/**
* 某会话的「消息 → OCR 文本」映射。
*
* 与语音的 `withVoiceTranscript` 同构:在**主进程**把派生文本贴到消息上,
* 再交给 Knowledge 索引,派生库不需要被 worker 打开。
*/
getConversationOcr(
conversationId: string
): Map<string, ConversationImageOcrEntry> {
const rows = asRows(
this.database
.prepare(
`SELECT b.message_id AS message_id, b.state AS binding_state,
a.state AS artifact_state, a.text AS text
FROM image_ocr_bindings b
LEFT JOIN image_ocr_artifacts a ON a.artifact_key = b.artifact_key
WHERE b.conversation_id = ?`
)
.all(conversationId)
)
const result = new Map<string, ConversationImageOcrEntry>()
for (const row of rows) {
result.set(String(row.message_id), {
state: String(row.artifact_state || row.binding_state) as ImageOcrPersistedState,
text: String(row.text ?? '')
})
}
return result
}
// -------------------------------------------------------------- checkpoint
/**
* 已持久化的图片消息总数统计。
*
* 必须落盘:派生库只知道自己**处理过**什么,不知道源数据里**一共**有多少图片。
* 一旦把这个 total 只放在内存里,应用重启后 coverage 就会退化成
* 「processed / processed」→ 把 30% 的部分索引谎报成 100% 完整覆盖。
*/
readCountedTotal(): { total: number; countedAt: number; complete: boolean } | null {
const total = this.readMeta('total_image_messages')
const countedAt = this.readMeta('total_image_counted_at')
if (total === null || countedAt === null) return null
const parsedTotal = Number(total)
const parsedCountedAt = Number(countedAt)
if (!Number.isFinite(parsedTotal) || !Number.isFinite(parsedCountedAt)) return null
return {
total: parsedTotal,
countedAt: parsedCountedAt,
// 统计时若有会话没数上(数据库不支持该统计),分母就是偏小的 →
// 绝不能据此声称"已覆盖全部",否则少数的那些会话会被静默算进"已覆盖"。
complete: this.readMeta('total_image_messages_complete') === '1'
}
}
writeCountedTotal(input: { total: number; countedAt: number; complete: boolean }): void {
this.writeMeta('total_image_messages', String(input.total))
this.writeMeta('total_image_counted_at', String(input.countedAt))
this.writeMeta('total_image_messages_complete', input.complete ? '1' : '0')
}
readScanState(): Map<
string,
{ state: string; imageTotal: number; processed: number; maxLocalId: number }
> {
const rows = asRows(
this.database
.prepare(
'SELECT conversation_id, state, image_total, image_processed, image_max_local_id FROM image_ocr_scan_state'
)
.all()
)
const map = new Map<
string,
{ state: string; imageTotal: number; processed: number; maxLocalId: number }
>()
for (const row of rows) {
map.set(String(row.conversation_id), {
state: String(row.state),
imageTotal: Number(row.image_total ?? 0),
processed: Number(row.image_processed ?? 0),
maxLocalId: Number(row.image_max_local_id ?? 0)
})
}
return map
}
writeScanState(input: {
conversationId: string
state: 'done' | 'partial'
imageTotal: number
imageProcessed: number
/** 本会话图片消息的最大插入序(增量水位)。 */
maxLocalId: number
}): void {
this.database
.prepare(
`INSERT INTO image_ocr_scan_state (
conversation_id, account_id, state, image_total, image_processed,
image_max_local_id, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(conversation_id) DO UPDATE SET
state = excluded.state,
image_total = excluded.image_total,
image_processed = excluded.image_processed,
image_max_local_id = excluded.image_max_local_id,
updated_at = excluded.updated_at`
)
.run(
input.conversationId,
this.accountId,
input.state,
input.imageTotal,
input.imageProcessed,
input.maxLocalId,
Date.now()
)
}
// ------------------------------------------------------------------ 统计
/**
* 有 OCR 派生绑定的会话集合。
*
* 清理时**必须**先拿到它:OCR 文本早已被灌进 Knowledge 的 chunks / FTS,
* 只删派生病不会让那些派生文字失效 —— 用户仍会从旧索引里搜到图片里的文字。
*/
conversationIdsWithOcr(): string[] {
const rows = asRows(
this.database.prepare('SELECT DISTINCT conversation_id FROM image_ocr_bindings').all()
)
return rows.map((row) => String(row.conversation_id))
}
/**
* **有 OCR 派生文本**(artifact 里 char_count > 0)的会话集合。
*
* 派生索引修复只需要重建它们:只有这些会话的 Knowledge 里"应该"存在图片派生文字。
* 全是 `empty` 的会话本来就没有派生文字可修,重建它们只是白读一遍 WCDB。
*/
conversationIdsWithIndexedOcr(): string[] {
const rows = asRows(
this.database
.prepare(
`SELECT DISTINCT b.conversation_id AS conversation_id
FROM image_ocr_bindings b
JOIN image_ocr_artifacts a ON a.artifact_key = b.artifact_key
WHERE a.char_count > 0`
)
.all()
)
return rows.map((row) => String(row.conversation_id))
}
/**
* 重置指定状态的失败记录,让它们可以被下一轮 pass 重新处理。
*
* 用途:**代码修好后**,把上一次 bug 造成的假失败(例如整批 `decrypt_failed`)
* 变成可重试状态,而不是要求用户删掉整个派生库 —— 那会连已经成功的记录一起丢掉。
*
* 三件事一起做,缺一不可:
* 1. 删掉这些失败绑定;
* 2. 删掉它们所在会话的 checkpoint —— 否则 pass 会以"该会话已完成"直接跳过,
* 表现为"点了重试但什么都没发生";
* 3. 删掉因此变成孤儿的 artifact(**没有任何绑定再引用的**才删,成功记录一条不动)。
*/
resetFailures(states: ImageOcrPersistedState[]): number {
if (!states.length) return 0
const placeholders = states.map(() => '?').join(', ')
const affected = asRows(
this.database
.prepare(
`SELECT DISTINCT conversation_id FROM image_ocr_bindings WHERE state IN (${placeholders})`
)
.all(...states)
).map((row) => String(row.conversation_id))
const artifactKeys = asRows(
this.database
.prepare(
`SELECT DISTINCT artifact_key FROM image_ocr_bindings WHERE state IN (${placeholders})`
)
.all(...states)
).map((row) => String(row.artifact_key))
const info = this.database
.prepare(`DELETE FROM image_ocr_bindings WHERE state IN (${placeholders})`)
.run(...states)
const clearScan = this.database.prepare(
'DELETE FROM image_ocr_scan_state WHERE conversation_id = ?'
)
for (const conversationId of affected) clearScan.run(conversationId)
const dropOrphan = this.database.prepare(
`DELETE FROM image_ocr_artifacts
WHERE artifact_key = ?
AND NOT EXISTS (SELECT 1 FROM image_ocr_bindings b WHERE b.artifact_key = ?)`
)
for (const artifactKey of artifactKeys) dropOrphan.run(artifactKey, artifactKey)
return Number(info.changes ?? 0)
}
/** 按状态聚合绑定数 —— 覆盖度与进度都从这里取,保证与库内真实一致。 */
countByState(): Record<string, number> { const rows = asRows(
this.database
.prepare('SELECT state, COUNT(*) AS total FROM image_ocr_bindings GROUP BY state')
.all()
)
const counts: Record<string, number> = {}
for (const row of rows) counts[String(row.state)] = Number(row.total ?? 0)
return counts
}
storageStats(): ImageTextIndexStorageStats {
const counts = this.countByState()
const textRow = this.database
.prepare(
"SELECT COUNT(*) AS total FROM image_ocr_artifacts WHERE state = 'indexed' AND length(text) > 0"
)
.get() as Record<string, unknown> | undefined
const updatedRow = this.database
.prepare('SELECT MAX(updated_at) AS latest FROM image_ocr_bindings')
.get() as Record<string, unknown> | undefined
let totalBytes = 0
for (const suffix of ['', '-wal', '-shm']) {
try {
totalBytes += statSync(`${this.databasePath}${suffix}`).size
} catch {
// 文件可能尚未创建;忽略。
}
}
const latest = updatedRow?.latest
return {
indexedImages: counts['indexed'] ?? 0,
ocrTextCount: Number(textRow?.total ?? 0),
totalBytes,
updatedAt: latest === null || latest === undefined ? null : Number(latest)
}
}
/** 清空全部派生数据(表级清空;文件级删除由 service 负责)。 */
clearAll(): void {
this.database.exec(`
DELETE FROM image_ocr_bindings;
DELETE FROM image_ocr_artifacts;
DELETE FROM image_ocr_scan_state;
DELETE FROM image_ocr_meta WHERE key IN (
'total_image_messages',
'total_image_counted_at',
'total_image_messages_complete'
);
`)
}
/**
* 清空并重置检查点。
*
* 注意:必须同时清 `scan_state`,否则清理后再次索引会因为「会话已完成」
* 而直接跳过 —— UI 会停在「未建立」但实际再也不跑。
*/
clearDerivedData(): void {
this.clearAll()
}
close(): void {
try {
// 先折叠 WAL 再关连接:否则 -wal / -shm 可能仍被持有,
// Windows 上会导致后续 rmSync 静默失败("清理成功"但文件还在)。
this.database.exec('PRAGMA wal_checkpoint(TRUNCATE);')
} catch {
// 库可能已经处于不可写状态;关闭仍然要做。
}
try {
this.database.close()
} catch {
// best effort
}
}
}
+137 -5
View File
@@ -1,5 +1,6 @@
import { listContactsAsync, listMessagesAsync, isReady, type FormattedContact, type FormattedMessage } from './chat-service'
import { resolveContact } from './contact-resolution-service'
import { sourceMessageId } from '../knowledge/message-identity'
import type { KnowledgeSearchService } from '../knowledge/knowledge-search-service'
import { inferAiSearchTimeRange } from '../../shared/ai-search'
import { KNOWLEDGE_FRESHNESS_TOLERANCE_MS } from '../../shared/knowledge'
@@ -13,6 +14,7 @@ import type {
ResolvedCorpusScope,
ResolvedTimeRange,
QueryIndexCoverage,
QueryImageTextCoverage,
QuerySearchTimings,
QueryMessagesRequest,
SearchMessagesRequest,
@@ -26,6 +28,12 @@ import {
decodeMessageRef as fromRef,
normalizeMessageIdentity
} from '../../shared/local-query-api'
import {
describeImageTextCoverage,
imageTextCoverageState,
type ImageTextIndexCoverage
} from '../../shared/image-text-index'
import { toEvidenceDisplayText } from '../../shared/knowledge'
const LIMIT_MAX = 200
const CONTEXT_MAX = 50
@@ -112,6 +120,47 @@ function buildIndexCoverage(
}
}
/**
* 图片文字索引未完成时,必须附加的零结果诚实性约束。
*
* 图片 OCR 是**独立的**覆盖维度:它可能是"未建立"或"只做了 30%"。
* 此时 0 条图片证据只是**索引缺口**,不是**事实空缺**。
*/
const IMAGE_OCR_ZERO_RESULT_CAUTION =
'涉及图片、截图、海报里的文字的问题,当前不能因为没搜到就回答"没有"。'
/**
* 图片文字索引覆盖度 → 可直接引用的结论句。
*
* 与 `buildIndexCoverage` 同思路:只给结构化数字,模型会自己换算、甚至反过来
* 宣称"覆盖完整"。这里由 Engine 给出结论句,模型只需引用。
*/
export function buildImageOcrCoverage(
coverage: ImageTextIndexCoverage | null
): QueryImageTextCoverage | undefined {
if (!coverage) return undefined
const state = imageTextCoverageState(coverage)
const countedNote = coverage.countedAt
? `(图片数量统计于 ${formatLocalMinute(coverage.countedAt)})`
: ''
const base = describeImageTextCoverage(coverage)
return {
state,
totalImageMessages: coverage.totalImageMessages,
processed: coverage.processed,
indexed: coverage.indexed,
empty: coverage.empty,
missing: coverage.missing,
failed: coverage.failed,
pending: coverage.pending,
...(coverage.countedAt ? { countedAtLabel: formatLocalMinute(coverage.countedAt) } : {}),
summary:
state === 'complete'
? `${base}${countedNote}`
: `${base}${countedNote}${IMAGE_OCR_ZERO_RESULT_CAUTION}`
}
}
const KIND_LABELS: Record<QueryMessageType, string> = {
text: '文本',
image: '图片',
@@ -149,7 +198,19 @@ function resolvedTimeRange(input: QueryTimeRange, now = new Date()): ResolvedTim
const range = inferAiSearchTimeRange(phrase[input.kind], map[input.kind], now)
return { kind: input.kind, startTime: range.startTime, endTime: range.endTime, label: range.label }
}
function toQueryMessage(conversationId: string, message: FormattedMessage, target: FormattedContact): QueryMessage {
/**
* 一条消息的展示形态。
*
* `imageOcr` 是可选的**派生文本**(来自本地图片文字索引,只读、不触发 OCR)。
* 图片消息的正文永远是空的 —— 识别出的文字必须走独立字段,
* 否则"图片里的文字"会被伪装成"群友发的文字消息"。
*/
function toQueryMessage(
conversationId: string,
message: FormattedMessage,
target: FormattedContact,
imageOcr?: { state: string; text: string }
): QueryMessage {
const kind = kindOf(message)
const content = message.contentData
const attachment =
@@ -163,7 +224,16 @@ function toQueryMessage(conversationId: string, message: FormattedMessage, targe
? { kind: 'file' as const, name: message.exportMediaName || (content?.type === 'share' ? content.title : undefined), url: content?.type === 'share' ? content.url : undefined }
: undefined
const text = message.content?.trim() || message.voiceTranscript?.trim() || undefined
return { messageRef: toRef(conversationId, message.id), timestamp: (message.createTime || 0) * 1000, datetime: message.datetime, sender: message.isSender ? '我' : (message.name || target.m_nsNickName), direction: message.isSender ? 'to_target' : 'from_target', messageType: kind, sourceKind: kind, ...(attachment ? { attachment } : {}), ...(text ? { text } : {}) }
const derived = kind === 'image' ? imageOcr : undefined
const ocrText = derived && derived.state === 'indexed' ? derived.text.trim() : ''
const imageTextState: QueryMessage['imageTextState'] = !derived
? 'not_indexed'
: ocrText
? 'indexed'
: derived.state === 'empty'
? 'empty'
: 'not_indexed'
return { messageRef: toRef(conversationId, message.id), timestamp: (message.createTime || 0) * 1000, datetime: message.datetime, sender: message.isSender ? '我' : (message.name || target.m_nsNickName), direction: message.isSender ? 'to_target' : 'from_target', messageType: kind, sourceKind: kind, ...(attachment ? { attachment } : {}), ...(text ? { text } : {}), ...(ocrText ? { imageOcrText: ocrText, derivedSource: 'image_ocr' as const } : {}), ...(kind === 'image' ? { imageTextState } : {}) }
}
/**
@@ -216,7 +286,49 @@ function outsideScopeError(scope: ResolvedCorpusScope, actual: string): { status
}
export class LocalQueryApiService {
constructor(private readonly knowledge?: KnowledgeSearchService, private readonly nowProvider: () => Date = () => new Date()) {}
/**
* 图片文字索引覆盖度提供者(同步、只读)。
*
* 刻意不在 Knowledge worker 里算:OCR 派生库(`image-text-index.sqlite`)与
* `knowledge.sqlite` 物理分离,worker 不该为了一个覆盖度数字去开它。
*/
private imageTextCoverage: () => ImageTextIndexCoverage | null = () => null
/**
* 单条图片消息的 OCR 派生文本提供者(同步、只读)。
*
* L4(查询层)**只读** L1(OCR artifact)—— 这里绝不允许触发 OCR、解密或读原图。
* 之前 `query_messages` 缺这一环,导致"图片已经识别出文字"这件事在精确读消息
* 这条路径上完全不可见:模型只拿到一个空的 `attachment`,于是把"索引缺口"
* 说成"图片里没有文字",甚至反过来建议用户去建立已经建好的索引。
*/
private imageOcrEntry:
| ((conversationId: string, messageId: string) => { state: string; text: string } | undefined)
| undefined
constructor(
private readonly knowledge?: KnowledgeSearchService,
private readonly nowProvider: () => Date = () => new Date()
) {}
/**
* 注入图片文字索引覆盖度提供者。
*
* 用 setter 而不是构造参数:避免给第二个带默认值的参数写 `undefined` 占位,
* 也让测试可以直接注入假的覆盖度。
*/
setImageTextCoverageProvider(provider: () => ImageTextIndexCoverage | null): void {
this.imageTextCoverage = provider
}
/** 注入单条图片消息的 OCR 派生文本解析器(只读;见 `imageOcrEntry` 的约束)。 */
setImageOcrEntryProvider(
provider:
| ((conversationId: string, messageId: string) => { state: string; text: string } | undefined)
| undefined
): void {
this.imageOcrEntry = provider
}
capabilities(): QueryCapabilitiesResponse {
return { version: 1, tools: { query_messages: { operation: '读取指定联系人的确定性消息', directions: ['any', 'from_target', 'to_target'], messageTypes: kinds, timeRanges: ['all', 'today', 'yesterday', 'this_week', 'last_7_days', 'this_month', 'previous_month', 'this_year', 'previous_year', 'absolute'], limitMax: LIMIT_MAX }, search_messages: { operation: '受限 Knowledge 关键词检索', timeRanges: ['all', 'today', 'yesterday', 'this_week', 'last_7_days', 'this_month', 'previous_month', 'this_year', 'previous_year', 'absolute'], limitMax: LIMIT_MAX }, message_context: { operation: '读取消息前后文', timeRanges: ['all'], limitMax: CONTEXT_MAX }, conversation_overview: { operation: '按会话时间片提取概览证据', timeRanges: ['all', 'today', 'yesterday', 'this_week', 'last_7_days', 'this_month', 'previous_month', 'this_year', 'previous_year', 'absolute'], limitMax: LIMIT_MAX } } }
}
@@ -282,7 +394,18 @@ export class LocalQueryApiService {
const raw = await listMessagesAsync(contact.md5, range.startTime, range.endTime)
const direction = request.direction || 'any'; const allowed = new Set(request.messageTypes || kinds)
const filtered = raw.filter((message) => !(request.excludeSystem !== false && kindOf(message) === 'system')).filter((message) => allowed.has(kindOf(message))).filter((message) => direction === 'any' || (direction === 'to_target' ? message.isSender : !message.isSender)).sort((a, b) => ((a.createTime || 0) - (b.createTime || 0)) * ((request.order || 'asc') === 'asc' ? 1 : -1)).slice(0, Math.min(LIMIT_MAX, Math.max(1, request.limit || 20)))
return { status: 'completed' as const, target: contactView(contact), query: { direction, messageTypes: request.messageTypes || [], order: request.order || 'asc', limit: Math.min(LIMIT_MAX, Math.max(1, request.limit || 20)), excludeSystem: request.excludeSystem !== false, resolvedTimeRange: range }, coverage: { state: 'complete' as const }, returnedCount: filtered.length, messages: filtered.map((message) => toQueryMessage(contact.md5, message, contact)), scope: corpus.scope }
// 图片 OCR 派生文本:L4 只读 L1,**不触发 OCR / 解密 / 读原图**。
// 键必须用 `sourceMessageId(message)`(binding 主键就是它),不能用裸 `message.id`,
// 否则 `local:` 前缀会让查表静默失配 —— 与 Knowledge 用同一条身份规则。
const messages = filtered.map((message) => {
const ocr =
kindOf(message) === 'image'
? this.imageOcrEntry?.(contact.md5, sourceMessageId(message))
: undefined
return toQueryMessage(contact.md5, message, contact, ocr)
})
const imageOcrCoverage = buildImageOcrCoverage(this.imageTextCoverage())
return { status: 'completed' as const, target: contactView(contact), query: { direction, messageTypes: request.messageTypes || [], order: request.order || 'asc', limit: Math.min(LIMIT_MAX, Math.max(1, request.limit || 20)), excludeSystem: request.excludeSystem !== false, resolvedTimeRange: range }, coverage: { state: 'complete' as const }, returnedCount: filtered.length, messages, scope: corpus.scope, ...(imageOcrCoverage ? { imageOcrCoverage } : {}) }
}
async search(request: SearchMessagesRequest) {
const requestStartedAt = Date.now()
@@ -359,6 +482,8 @@ export class LocalQueryApiService {
const covered = indexCovers(found.indexLatestAt, requestedEnd, found.sourceLatestAt)
const indexCoverage = buildIndexCoverage(found.indexLatestAt, found.sourceLatestAt, covered)
// 图片文字索引是**独立覆盖维度**:文字索引再完整也不代表图片里的文字搜得到。
const imageOcrCoverage = buildImageOcrCoverage(this.imageTextCoverage())
const timings: QuerySearchTimings = {
totalMs: Date.now() - requestStartedAt,
scopeMs,
@@ -381,6 +506,7 @@ export class LocalQueryApiService {
sourceLatestAt: found.sourceLatestAt,
freshness: { catchUp: freshness.catchUp },
...(indexCoverage ? { indexCoverage } : {}),
...(imageOcrCoverage ? { imageOcrCoverage } : {}),
timings
}
}
@@ -454,7 +580,11 @@ export class LocalQueryApiService {
timestamp: item.timestamp,
sender: item.sender,
sourceKind: item.sourceKind,
text: item.text,
// 兜底再剥一次:不管 Knowledge 侧哪条检索路径产出的文本,
// 面向用户与模型的都不允许出现 `图片文字:` 这类引擎内部标签。
text: toEvidenceDisplayText(item.text),
...(item.derivedSource ? { derivedSource: item.derivedSource } : {}),
...(item.imageOcrText ? { imageOcrText: item.imageOcrText } : {}),
conversationName: owner ? contactView(owner).displayName : undefined,
conversationType: owner?.type
} satisfies QueryEvidenceItem
@@ -592,6 +722,7 @@ export class LocalQueryApiService {
const truncated = raw.length > OVERVIEW_SOURCE_CAP
const evidence = selectTemporalCoverageEvidence(contact, messages, OVERVIEW_EVIDENCE_TARGET)
const state: 'complete' | 'partial' = truncated ? 'partial' : 'complete'
const imageOcrCoverage = buildImageOcrCoverage(this.imageTextCoverage())
return {
status: 'completed' as const,
target: contactView(contact),
@@ -603,6 +734,7 @@ export class LocalQueryApiService {
selection: { mode: 'temporal_coverage' as const, selectedEvidenceCount: evidence.length, sampled: truncated || evidence.length < messages.length },
evidence,
scope: corpus.scope,
...(imageOcrCoverage ? { imageOcrCoverage } : {}),
origin: 'wcdb' as const
}
}
+104 -11
View File
@@ -106,6 +106,17 @@ export interface QueryAgentTraceItem {
* 会剥离)。用途:把不透明的 Tool 总耗时拆成 scope / freshness / 每个 probe / 合并 / 证据补全。
*/
searchTimings?: QuerySearchTimings
/**
* 本次 Tool Result 里携带 OCR 派生文本的图片消息/证据条数(诊断用,不进模型上下文)。
*
* 存在的意义是让"图片已经识别出文字、但模型没拿到"这类**链路断点**可以被直接观测:
* 真机上曾经出现过 `query_messages` 返回了图片消息却只带 `attachment`、
* 模型因此回答"没有取得 OCR 文字"。当时从回答文本无法判断是"索引没建"还是"没接上",
* 因为这两件事在日志里长得一模一样。有了这个数字就能一眼分开。
*/
imageOcrTextCount?: number
/** 本次 Tool Result 里图片文字索引的覆盖度状态(`not_built` / `partial` / `complete` / `failed`)。 */
imageOcrCoverageState?: string
}
export interface QueryAgentModelCallDiagnostic {
@@ -207,6 +218,12 @@ const SYSTEM_PROMPT = `你是 TraceMemo 的本地聊天查询助手,只能使
规划原则:
- 先判断问题需要哪种证据,再调用最少的 Tool。每次收到 Tool Result 后都判断“当前 Evidence 是否已经足以给出有边界的回答”;足够就立即回答,不为追求绝对完整继续调查。
- query_messages 是精确事实查询,适用于能用联系人、时间、方向、消息类型、顺序等结构条件表达的问题。earliest/latest 等时间边界也是结构条件,必须使用 order 与 limit 精确查询,不能使用抽样 overview。每次调用都必须如实声明 temporalBasis。结果已经回答问题时,不要追加 conversation_overview。
方向(direction)必须按**说话人是谁**来定,不要按语序猜:
- direction 的参照物是“目标会话”:to_target = **我发出**的(说话人是我自己),from_target = **对方发来**的。没有 other 取值,拿不准就用 any。
- 说话人是我 → to_target:“我给张三发了什么”“我发给张三的”“我发给他的文件”“我之前给他发过什么”“我发出去的图片”“我在这个群里发过什么”。
- 说话人是对方 → from_target:“张三给我发了什么”“他之前给我的图片”“张三发给我的文件”。
- **不许**因为“我”出现在句首就选 from_target;也不要凭昵称是否叫“我”来判断说话人,自我身份以消息自身的发送者标记为准。
- 一旦某次 query_messages 返回 0 条,先回头核对 direction 是否与问题的说话人**冲突**;冲突就属于允许的 substantively different retry,必须直接换方向再查一次,**不要**问用户“是不是方向搞错了/要不要换个方向”,用户已经把话说清楚了。
- 需要绝对时间范围时,startTime/endTime 必须使用带时区偏移的 ISO-8601 字符串(例如 2026-08-01T00:00:00+08:00 或 2026-07-31T16:00:00Z)。不要传 epoch 数字,也不要传没有时区的裸本地时间。
- search_messages 是关键词检索,适用于结构条件无法确定答案的问题。queries 的每一项都是一次独立的字面检索:一项只放一个简短关键词,不要把多个近义词或整句话塞进同一项。首次最多 4 项。检索到 Evidence 后直接判断;只有本次完全没有 Evidence 时,才允许再检索一次,且每一项都必须与上一次实质不同。
- conversation_overview 只用于真正需要理解一个时间范围内整体聊了什么、主要话题或整体互动的 broad summary。它返回 temporal coverage sample,不代表完整聊天,也不是检索不足时的默认 fallback。
@@ -219,6 +236,20 @@ const SYSTEM_PROMPT = `你是 TraceMemo 的本地聊天查询助手,只能使
- indexCoverage.covered 为 false 时,说明这段时间还没进索引:此时即使结果为 0 也只能说"索引尚未覆盖这段时间,暂时无法确认",**绝不能**说成"没有"。必须如实引用结论里的索引更新时间。
- 已经检索到 Evidence 时,只有当这个覆盖边界真的会影响结论时才补一句说明,不要机械附加警告。
- 只有 coverage.state 为 complete(indexCoverage.covered 为 true)且结果为 0,才可以下"没有找到"的结论。不要自己把 partial 说成 complete。
图片文字索引:search_messages 的 imageOcrCoverage 是**独立于文字索引**的覆盖维度,只针对“图片里的文字”(截图、报价图、公告截图、海报)。规则:
- 文字消息索引完整**不代表**图片里的文字搜得到。不要把这两个维度混着说。
- 问题涉及图片里的文字、而 imageOcrCoverage.state 不是 complete 时:即使图片证据为 0,也**绝不能**回答“没有”或“没找到”。必须如实引用 imageOcrCoverage.summary,说明图片文字索引尚未完成、当前无法确认全部历史图片。
- imageOcrCoverage.state 为 not_built 时,明确告诉用户图片文字索引还没建立,图片里的文字目前搜不到,并提示可以在「问问微信」里建立。
- 只有 imageOcrCoverage.state 为 complete 且图片证据为 0,才可以下“没有找到”的结论。
图片消息的文字(query_messages 与 search_messages 都适用):
- 图片消息可能带 imageOcrText / derivedSource=image_ocr —— 那是**这张图片里识别出的文字**(本地 OCR 派生),可以直接用它回答“图片里写了什么”。问法可能是“我今早发的那张图片里写了什么”“那张 ChatGPT 价格截图是什么内容”。
- imageOcrText 是派生内容,**证据永远是那条原始图片消息**:messageRef、sender、conversation、时间都只能用原始图片消息的。描述时说“图片里的文字是…”,**不许**把它说成某人发的一条文字消息,**不许**为了它编造任何不存在的消息。
- imageTextState 是**结构化事实**,三种取值含义不同,不要互相替代:
- indexed:已识别出文字(同时有 imageOcrText)。
- empty:本地识别过,这张图里确实没有文字。此时**只能**回答图片本身,**绝不许**根据 OCR 去猜人物、场景、物体或表情包含义(OCR 不是看图,没有 Vision 能力就不要假装有)。
- not_indexed:这条图片还没进图片文字索引。**不许**把“还没索引”说成“图片里没有文字”;若 imageOcrCoverage 不是 complete,必须说明当前无法确认。
- 图片文字索引状态一律以 Tool Result 的结构化字段为准。**不要**在回答里凭空建议“可以先建立图片文字索引再查”——只有 imageOcrCoverage.state 确实是 not_built 时才可以这么说。
- 区分「图片里确实没有文字」(OCR 结果为空,属于已处理的正常终态)与「图片还没被索引」(覆盖缺口):前者是事实,后者不能当成事实。
缺少必要信息时用自然语言澄清;超出工具能力时说明不能可靠完成,并给出当前工具可以执行的替代方向。`
function toolDefinitions(): AIChatToolDefinition[] {
@@ -545,7 +576,7 @@ function retryNote(name: string, result: QueryAgentToolResult, state: ZeroResult
if (result.status !== 'completed') return undefined
const counts = resultCount(result)
if (name === 'search_messages' && !counts.evidenceCount && state.searchAttempts <= ZERO_RESULT_RETRY_LIMIT) return '本次检索没有任何 Evidence。允许再执行一次 search_messages,但每一项都必须与上一次实质不同;完全相同的检索会被拒绝。'
if (name === 'query_messages' && counts.resultCount === 0 && !result.fallbackLookup && state.queryAttempts <= ZERO_RESULT_RETRY_LIMIT) return '本次精确查询返回 0 条。允许再执行一次 query_messages,用于放宽 direction 或 messageTypes 等非时间条件;改变时间范围会被拒绝。'
if (name === 'query_messages' && counts.resultCount === 0 && !result.fallbackLookup && state.queryAttempts <= ZERO_RESULT_RETRY_LIMIT) return '本次精确查询返回 0 条。只允许放宽 direction 或 messageTypes 等非时间条件(改变时间范围会被拒绝)。**特别注意方向选反这种情况**:如果问题是“我给 X 发 / 我发给 X 的”,而本次用的是 from_target(对方发来),那是方向选反了 —— 直接改用 to_target 重查一次,这属于允许的实质不同重试。不要因为有 0 条就收尾,也不要问用户“是不是方向搞错了 / 要不要换个方向”,用户已经把说话人讲清楚了。'
return undefined
}
@@ -583,10 +614,52 @@ function messageRecordForModel(value: unknown): unknown {
return record.messageType || !record.sourceKind ? record : { ...record, messageType: record.sourceKind }
}
function toolResultForModel(name: string, result: QueryAgentToolResult, callsUsed: number, nextTools: AIChatToolDefinition[], note?: string): QueryAgentToolResult {
const visible: QueryAgentToolResult = { ...result }
/**
* 从 Tool Result 里读出图片 OCR 的两条**结构化事实**(诊断 / 日志用)。
*
* 只看字段存在与否与数量,**不读文本内容**:排查链路断点不需要正文,
* 日志里也不该多留一份聊天内容。
*/
function imageOcrDiagnostics(result: QueryAgentToolResult): {
imageOcrTextCount?: number
imageOcrCoverageState?: string
} {
let count = 0
const collect = (value: unknown): void => {
if (!Array.isArray(value)) return
for (const item of value) {
if (!item || typeof item !== 'object' || Array.isArray(item)) continue
const text = (item as Record<string, unknown>).imageOcrText
if (typeof text === 'string' && text.trim()) count += 1
}
}
collect(result.messages)
collect(result.evidence)
const coverage = result.imageOcrCoverage
const state =
coverage && typeof coverage === 'object' && !Array.isArray(coverage)
? (coverage as Record<string, unknown>).state
: undefined
return {
...(count > 0 ? { imageOcrTextCount: count } : {}),
...(typeof state === 'string' ? { imageOcrCoverageState: state } : {})
}
}
function toolResultForModel(name: string, result: QueryAgentToolResult, callsUsed: number, nextTools: AIChatToolDefinition[], note?: string): QueryAgentToolResult { const visible: QueryAgentToolResult = { ...result }
if (Array.isArray(result.messages)) visible.messages = result.messages.map(messageRecordForModel)
if (Array.isArray(result.evidence)) visible.evidence = result.evidence.map(messageRecordForModel)
if (Array.isArray(result.evidence)) {
visible.evidence = result.evidence.map((item) => {
const record = messageRecordForModel(item)
if (!record || typeof record !== 'object' || Array.isArray(record)) return record
// `imageOcrText` 是给 Evidence UI 做"命中解释"的片段;它的内容已经在 `text` 里,
// 再原样带一份进模型上下文是纯重复。模型侧保留 `derivedSource` 这个语义标记即可,
// 由此知道"这条命中的是图片里的文字"。
const trimmed = { ...(record as Record<string, unknown>) }
delete trimmed.imageOcrText
return trimmed
})
}
if (result.anchor) visible.anchor = messageRecordForModel(result.anchor)
if (Array.isArray(result.before)) visible.before = result.before.map(messageRecordForModel)
if (Array.isArray(result.after)) visible.after = result.after.map(messageRecordForModel)
@@ -616,7 +689,7 @@ function toolResultForModel(name: string, result: QueryAgentToolResult, callsUse
return visible
}
function nextToolDefinitions(name: string, result: QueryAgentToolResult, state: ZeroResultRetryState, rangeWasAll = false): AIChatToolDefinition[] {
function nextToolDefinitions(name: string, result: QueryAgentToolResult, state: ZeroResultRetryState): AIChatToolDefinition[] {
// 重复重试已被拒绝,不再开放工具,避免用有限的 tool budget 反复试同一条件。
if (result.constraint === 'duplicate_retry') return []
if (result.status === 'invalid_tool_arguments') return toolDefinition(name)
@@ -631,10 +704,24 @@ function nextToolDefinitions(name: string, result: QueryAgentToolResult, state:
if (name === 'query_messages') {
// Host 已经自动执行过一次扩大查询:不再开放 retry,避免出现第三次查询。
if (result.fallbackLookup) return []
// 已经查了全部历史且 0 结果:再换时间范围毫无意义(更窄只会更少)。
if (rangeWasAll && counts.resultCount === 0) return []
// 只有 0 结果才开放一次重试;有结果时保持原有 stopping。
return counts.resultCount === 0 && state.queryAttempts <= ZERO_RESULT_RETRY_LIMIT ? toolDefinition('query_messages') : []
/**
* 这里**不能**因为"时间范围已经是全部"就关掉重试。
*
* 原实现是 `if (rangeWasAll && resultCount === 0) return []`,依据是"时间不能再放宽了、
* 更窄只会更少"。但 0 结果的重试本来就不是为了改时间 —— 它是为了放宽
* **direction / messageTypes**:「我给 X 发了什么图片」被错判成 `from_target` 时,
* 换成 `to_target` 会从 0 条变成有结果。
*
* 这个守卫的后果正是真机那个回归:工具没发出去 → 第二次调用被
* `tool_availability` 拒掉 → 模型想改向也调不动 → 只能回头问用户"是不是方向搞错了"。
*
* 时间范围不可变由 `constraint_time_range_immutable` 单独把关,
* 完全相同的重试由 `duplicate_retry` 拦下,次数由 ZERO_RESULT_RETRY_LIMIT 限制,
* 所以这里放开是安全的。
*/
return counts.resultCount === 0 && state.queryAttempts <= ZERO_RESULT_RETRY_LIMIT
? toolDefinition('query_messages')
: []
}
return []
}
@@ -772,6 +859,12 @@ class EvidenceCollector {
? { messageType: record.sourceKind }
: {}),
...(typeof record.text === 'string' && record.text ? { text: record.text } : {}),
// 「靠图片里的文字命中」这个来源语义必须带到 UI:用户要能看出这条答案来自
// 图片 OCR,而不是群友真发了一条文字消息。messageRef 仍然指向原始图片消息。
...(record.derivedSource === 'image_ocr' ? { derivedSource: 'image_ocr' as const } : {}),
...(typeof record.imageOcrText === 'string' && record.imageOcrText
? { imageOcrText: record.imageOcrText }
: {}),
...(attachmentView && Object.keys(attachmentView).length ? { attachment: attachmentView } : {}),
source
}
@@ -967,10 +1060,10 @@ export class QueryAgentService {
rawTimings && typeof rawTimings === 'object' && !Array.isArray(rawTimings)
? (rawTimings as QuerySearchTimings)
: undefined
result.traces.push({ toolName: call.name, input: sanitizeInput(traceInput), durationMs, status: completedToolResult.status, ...counts, ...(temporalBasis ? { temporalBasis } : {}), ...(autoFallback ? { autoFallback } : {}), ...(searchTimings ? { searchTimings } : {}) })
result.traces.push({ toolName: call.name, input: sanitizeInput(traceInput), durationMs, status: completedToolResult.status, ...counts, ...imageOcrDiagnostics(completedToolResult), ...(temporalBasis ? { temporalBasis } : {}), ...(autoFallback ? { autoFallback } : {}), ...(searchTimings ? { searchTimings } : {}) })
const nextTools = completedToolResult.constraint === 'tool_availability'
? tools
: nextToolDefinitions(call.name, completedToolResult, retry, rangeKind(traceInput) === 'all')
: nextToolDefinitions(call.name, completedToolResult, retry)
const note = retryNote(call.name, completedToolResult, retry)
messages.push({ role: 'tool', tool_call_id: call.id, name: call.name, content: JSON.stringify(toolResultForModel(call.name, completedToolResult, result.toolCallCount, nextTools, note)) })
tools = nextTools