diff --git a/src/renderer/src/config/ocr.ts b/src/renderer/src/config/ocr.ts index 2f5141c0f0..3432dbba73 100644 --- a/src/renderer/src/config/ocr.ts +++ b/src/renderer/src/config/ocr.ts @@ -1,10 +1,14 @@ import type { BuiltinOcrProvider, BuiltinOcrProviderId, + OcrOvConfig, OcrOvProvider, + OcrPpocrConfig, OcrPpocrProvider, OcrProviderCapability, + OcrSystemConfig, OcrSystemProvider, + OcrTesseractConfig, OcrTesseractProvider, TesseractLangCode, TranslateLanguageCode @@ -17,22 +21,12 @@ const tesseract: OcrTesseractProvider = { name: 'Tesseract', capabilities: { image: true - }, - config: { - langs: { - chi_sim: true, - chi_tra: true, - eng: true - } } } as const const systemOcr: OcrSystemProvider = { id: 'system', name: 'System', - config: { - langs: isWin ? ['en-us'] : undefined - }, capabilities: { image: true // pdf: true @@ -42,9 +36,6 @@ const systemOcr: OcrSystemProvider = { const ppocrOcr: OcrPpocrProvider = { id: 'paddleocr', name: 'PaddleOCR', - config: { - apiUrl: '' - }, capabilities: { image: true // pdf: true @@ -54,15 +45,31 @@ const ppocrOcr: OcrPpocrProvider = { const ovOcr: OcrOvProvider = { id: 'ovocr', name: 'Intel OV(NPU) OCR', - config: { - langs: isWin ? ['en-us', 'zh-cn'] : undefined - }, capabilities: { image: true // pdf: true } } as const satisfies OcrOvProvider +export const BUILTIN_OCR_PROVIDER_CONFIG_MAP = { + tesseract: { + langs: { + chi_sim: true, + chi_tra: true, + eng: true + } + } satisfies OcrTesseractConfig, + system: { + langs: isWin ? ['en-us'] : undefined + } satisfies OcrSystemConfig, + paddleocr: { + apiUrl: '' + } satisfies OcrPpocrConfig, + ovocr: { + langs: isWin ? ['en-us', 'zh-cn'] : undefined + } satisfies OcrOvConfig +} as const satisfies Record + export const BUILTIN_OCR_PROVIDERS_MAP = { tesseract, system: systemOcr, diff --git a/src/renderer/src/store/index.ts b/src/renderer/src/store/index.ts index 30d5dfe309..402b444a23 100644 --- a/src/renderer/src/store/index.ts +++ b/src/renderer/src/store/index.ts @@ -69,7 +69,7 @@ const persistedReducer = persistReducer( { key: 'cherry-studio', storage, - version: 163, + version: 164, blacklist: ['runtime', 'messages', 'messageBlocks', 'tabs'], migrate }, diff --git a/src/renderer/src/store/migrate.ts b/src/renderer/src/store/migrate.ts index 0a63f09672..43ef87d914 100644 --- a/src/renderer/src/store/migrate.ts +++ b/src/renderer/src/store/migrate.ts @@ -11,7 +11,12 @@ import { isNotSupportedTextDelta, SYSTEM_MODELS } from '@renderer/config/models' -import { BUILTIN_OCR_PROVIDERS, BUILTIN_OCR_PROVIDERS_MAP, DEFAULT_OCR_PROVIDER } from '@renderer/config/ocr' +import { + BUILTIN_OCR_PROVIDER_CONFIG_MAP, + BUILTIN_OCR_PROVIDERS, + BUILTIN_OCR_PROVIDERS_MAP, + DEFAULT_OCR_PROVIDER +} from '@renderer/config/ocr' import { isSupportArrayContentProvider, isSupportDeveloperRoleProvider, @@ -31,7 +36,7 @@ import type { TranslateLanguageCode, WebSearchProvider } from '@renderer/types' -import { isSystemProvider, SystemProviderIds } from '@renderer/types' +import { isBuiltinOcrProvider, isSystemProvider, SystemProviderIds } from '@renderer/types' import { getDefaultGroupName, getLeadingEmoji, runAsyncFunction, uuid } from '@renderer/utils' import { defaultByPassRules } from '@shared/config/constant' import { TRANSLATE_PROMPT } from '@shared/config/prompts' @@ -2233,6 +2238,7 @@ const migrateConfig = { }, '137': (state: RootState) => { try { + // @ts-expect-error old migration state.ocr = { providers: BUILTIN_OCR_PROVIDERS, imageProviderId: DEFAULT_OCR_PROVIDER.image.id @@ -2685,6 +2691,34 @@ const migrateConfig = { logger.error('migrate 163 error', error as Error) return state } + }, + '164': (state: RootState) => { + try { + state.ocr.providers.forEach((p) => { + if (isBuiltinOcrProvider(p)) { + switch (p.id) { + case 'ovocr': + state.ocr.configs.ovocr = p.config ?? BUILTIN_OCR_PROVIDER_CONFIG_MAP.ovocr + break + case 'paddleocr': + state.ocr.configs.paddleocr = p.config ?? BUILTIN_OCR_PROVIDER_CONFIG_MAP.paddleocr + break + case 'system': + state.ocr.configs.system = p.config ?? BUILTIN_OCR_PROVIDER_CONFIG_MAP.system + break + case 'tesseract': + state.ocr.configs.tesseract = p.config ?? BUILTIN_OCR_PROVIDER_CONFIG_MAP.tesseract + break + default: + logger.warn(`Unknown ocr provider ${p.id}. Skipped.`) + } + } + }) + return state + } catch (error) { + logger.error('migrate 164 error', error as Error) + return state + } } } diff --git a/src/renderer/src/store/ocr.ts b/src/renderer/src/store/ocr.ts index 8e997bd6d5..308fe5c830 100644 --- a/src/renderer/src/store/ocr.ts +++ b/src/renderer/src/store/ocr.ts @@ -1,15 +1,22 @@ import type { PayloadAction } from '@reduxjs/toolkit' import { createSlice } from '@reduxjs/toolkit' -import { BUILTIN_OCR_PROVIDERS, DEFAULT_OCR_PROVIDER } from '@renderer/config/ocr' -import type { OcrProvider, OcrProviderConfig } from '@renderer/types' +import { BUILTIN_OCR_PROVIDER_CONFIG_MAP, BUILTIN_OCR_PROVIDERS, DEFAULT_OCR_PROVIDER } from '@renderer/config/ocr' +import type { BuiltinOcrProviderId, OcrProvider, OcrProviderConfig } from '@renderer/types' export interface OcrState { providers: OcrProvider[] + configs: Record imageProviderId: string } const initialState: OcrState = { providers: BUILTIN_OCR_PROVIDERS, + configs: { + tesseract: BUILTIN_OCR_PROVIDER_CONFIG_MAP.tesseract, + system: BUILTIN_OCR_PROVIDER_CONFIG_MAP.system, + paddleocr: BUILTIN_OCR_PROVIDER_CONFIG_MAP.paddleocr, + ovocr: BUILTIN_OCR_PROVIDER_CONFIG_MAP.ovocr + }, imageProviderId: DEFAULT_OCR_PROVIDER.image.id } diff --git a/src/renderer/src/types/ocr.ts b/src/renderer/src/types/ocr.ts index 08d196cc12..f4d4f0a4ac 100644 --- a/src/renderer/src/types/ocr.ts +++ b/src/renderer/src/types/ocr.ts @@ -162,7 +162,6 @@ export type OcrTesseractConfig = OcrProviderBaseConfig & { export type OcrTesseractProvider = { id: 'tesseract' - config: OcrTesseractConfig } & ImageOcrProvider & BuiltinOcrProvider @@ -179,7 +178,6 @@ export type OcrSystemConfig = OcrProviderBaseConfig & { export type OcrSystemProvider = { id: 'system' - config: OcrSystemConfig } & ImageOcrProvider & // PdfOcrProvider & BuiltinOcrProvider @@ -196,7 +194,6 @@ export type OcrPpocrConfig = OcrProviderBaseConfig & { export type OcrPpocrProvider = { id: 'paddleocr' - config: OcrPpocrConfig } & ImageOcrProvider & // PdfOcrProvider & BuiltinOcrProvider @@ -212,7 +209,6 @@ export type OcrOvConfig = OcrProviderBaseConfig & { export type OcrOvProvider = { id: 'ovocr' - config: OcrOvConfig } & ImageOcrProvider & // PdfOcrProvider & BuiltinOcrProvider