From 931e6b72789832666bc5d338a00539a48b65efe1 Mon Sep 17 00:00:00 2001 From: icarus Date: Sat, 23 Aug 2025 15:08:05 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ocr):=20=E9=87=8D=E6=9E=84OCR=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E5=AE=9A=E4=B9=89=E4=BB=A5=E6=94=AF=E6=8C=81=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E5=92=8CAPI=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将OCR提供者配置拆分为独立类型,增加模型能力记录和API配置类型检查 添加OCR处理程序类型定义,为未来扩展提供更好的类型支持 --- src/renderer/src/types/ocr.ts | 72 ++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 10 deletions(-) diff --git a/src/renderer/src/types/ocr.ts b/src/renderer/src/types/ocr.ts index c7e53f24a6..e443e1597f 100644 --- a/src/renderer/src/types/ocr.ts +++ b/src/renderer/src/types/ocr.ts @@ -1,4 +1,4 @@ -import { FileMetadata, ImageFileMetadata, isImageFile, Model } from '.' +import { FileMetadata, ImageFileMetadata, isImageFile } from '.' export const BuiltinOcrProviderIds = { tesseract: 'tesseract' @@ -23,22 +23,70 @@ export const isOcrProviderCapability = (cap: string): cap is OcrProviderCapabili export type OcrProviderCapabilityRecord = Partial> +// OCR models and providers share the same type definition. +// A provider can offer capabilities to process multiple file types, +// while a model belonging to that provider may be limited to processing only one specific file type. +export type OcrModelCapabilityRecord = OcrProviderCapabilityRecord + +export interface OcrModel { + id: string + name: string + providerId: string + capabilities: OcrModelCapabilityRecord +} + +/** + * Extend this type to define provider-specefic config types. + */ +export type OcrProviderApiConfig = { + apiKey: string + apiHost: string + apiVersion?: string +} + +export const isOcrProviderApiConfig = (config: unknown): config is OcrProviderApiConfig => { + return ( + typeof config === 'object' && + config !== null && + 'apiKey' in config && + typeof config.apiKey === 'string' && + 'apiHost' in config && + typeof config.apiHost === 'string' && + (!('apiVersion' in config) || typeof config.apiVersion === 'string') + ) +} + +/** + * For future. Model based ocr, api based ocr. May different api client. + * + * Extend this type to define provider-specific config types. + */ +export type OcrProviderConfig = { + /** Not used for now. Could safely remove. */ + api?: OcrProviderApiConfig + /** Not used for now. Could safely remove. */ + models?: OcrModel[] + /** Not used for now. Could safely remove. */ + enabled?: boolean +} + export type OcrProvider = { id: string name: string capabilities: OcrProviderCapabilityRecord - config?: { - // for future. Model based ocr, api based ocr. May different api client. - api?: { - apiKey: string - apiHost: string - apiVersion?: string - } - models?: Model[] - enabled?: boolean + config?: OcrProviderConfig +} + +export type OcrApiProvider = OcrProvider & { + config: OcrProviderConfig & { + api: OcrProviderApiConfig } } +export const isOcrApiProvider = (p: OcrProvider): p is OcrApiProvider => { + return !!(p.config && p.config.api && isOcrProviderApiConfig(p.config.api)) +} + export type BuiltinOcrProvider = OcrProvider & { id: BuiltinOcrProviderId } @@ -71,3 +119,7 @@ export const isSupportedOcrFile = (file: FileMetadata): file is SupportedOcrFile export type OcrResult = { text: string } + +export type OcrHandler = (file: SupportedOcrFile) => Promise + +export type OcrImageHandler = (file: ImageFileMetadata) => Promise