mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-25 03:10:08 +08:00
refactor(ocr): 重构OCR类型定义以支持模型和API配置
将OCR提供者配置拆分为独立类型,增加模型能力记录和API配置类型检查 添加OCR处理程序类型定义,为未来扩展提供更好的类型支持
This commit is contained in:
parent
f3da4a6e36
commit
931e6b7278
@ -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<Record<OcrProviderCapability, boolean>>
|
||||
|
||||
// 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<OcrResult>
|
||||
|
||||
export type OcrImageHandler = (file: ImageFileMetadata) => Promise<OcrResult>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user