mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-09 23:10:20 +08:00
feat(ocr): implement PATCH endpoint for OCR provider updates
Add PATCH handler for OCR provider updates with request/response schemas Implement patchProvider method in OcrService to update provider data
This commit is contained in:
parent
1e9014b080
commit
4e7a67df59
@ -1,6 +1,11 @@
|
||||
// NOTE: Types are defined inline in the schema for simplicity
|
||||
// If needed, specific types can be imported from './apiModels'
|
||||
import type { GetOcrProviderResponse, ListOcrProvidersResponse } from '@types'
|
||||
import type {
|
||||
GetOcrProviderResponse,
|
||||
ListOcrProvidersResponse,
|
||||
PatchOcrProviderRequest,
|
||||
PatchOcrProviderResponse
|
||||
} from '@types'
|
||||
|
||||
import type { BodyForPath, ConcreteApiPaths, QueryParamsForPath, ResponseForPath } from './apiPaths'
|
||||
import type { HttpMethod, PaginatedResponse, PaginationParams } from './apiTypes'
|
||||
@ -364,7 +369,8 @@ export interface ApiSchemas {
|
||||
response: GetOcrProviderResponse
|
||||
}
|
||||
PATCH: {
|
||||
// TODO
|
||||
body: PatchOcrProviderRequest
|
||||
response: PatchOcrProviderResponse
|
||||
}
|
||||
PUT: {
|
||||
// TODO
|
||||
|
||||
@ -224,8 +224,8 @@ export const apiHandlers: ApiImplementation = {
|
||||
GET: async () => {
|
||||
throw new Error('Not implemented')
|
||||
},
|
||||
PATCH: async () => {
|
||||
throw new Error('Not implemented')
|
||||
PATCH: async ({ body }) => {
|
||||
return ocrService.patchProvider(body)
|
||||
},
|
||||
PUT: async () => {
|
||||
throw new Error('Not implemented')
|
||||
|
||||
3
src/main/data/db/schemas/index.ts
Normal file
3
src/main/data/db/schemas/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export { appStateTable } from './appState'
|
||||
export { ocrProviderTable } from './ocr/provider'
|
||||
export { preferenceTable } from './preference'
|
||||
@ -1,8 +1,16 @@
|
||||
import { dbService } from '@data/db/DbService'
|
||||
import { ocrProviderTable } from '@data/db/schemas/ocr/provider'
|
||||
import { loggerService } from '@logger'
|
||||
import type { ListOcrProvidersResponse, OcrParams, OcrResult, SupportedOcrFile } from '@types'
|
||||
import type {
|
||||
ListOcrProvidersResponse,
|
||||
OcrParams,
|
||||
OcrResult,
|
||||
PatchOcrProviderRequest,
|
||||
PatchOcrProviderResponse,
|
||||
SupportedOcrFile
|
||||
} from '@types'
|
||||
import { BuiltinOcrProviderIds } from '@types'
|
||||
import { eq } from 'drizzle-orm'
|
||||
|
||||
import type { OcrBaseService } from './builtin/OcrBaseService'
|
||||
import { ovOcrService } from './builtin/OvOcrService'
|
||||
@ -50,6 +58,19 @@ export class OcrService {
|
||||
return { data: providers.filter((p) => registeredKeys.includes(p.id)) }
|
||||
}
|
||||
|
||||
public async patchProvider(update: PatchOcrProviderRequest): Promise<PatchOcrProviderResponse> {
|
||||
const providers = await dbService
|
||||
.getDb()
|
||||
.select()
|
||||
.from(ocrProviderTable)
|
||||
.where(eq(ocrProviderTable.id, update.id))
|
||||
.limit(1)
|
||||
if (providers.length == 0) {
|
||||
throw new Error(`OCR provider ${update.id} not found`)
|
||||
}
|
||||
return { data: providers[0] }
|
||||
}
|
||||
|
||||
public async ocr(file: SupportedOcrFile, params: OcrParams): Promise<OcrResult> {
|
||||
const service = this.registry.get(params.providerId)
|
||||
if (!service) {
|
||||
|
||||
@ -81,9 +81,12 @@ export const OcrProviderConfigSchema = OcrProviderBaseConfigSchema.loose()
|
||||
|
||||
export type OcrProviderConfig = z.infer<typeof OcrProviderConfigSchema>
|
||||
|
||||
const OcrProviderIdSchema = z.string()
|
||||
const OcrProviderNameSchema = z.string()
|
||||
|
||||
export const OcrProviderSchema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
id: OcrProviderIdSchema,
|
||||
name: OcrProviderNameSchema,
|
||||
capabilities: OcrProviderCapabilityRecordSchema,
|
||||
config: OcrProviderConfigSchema
|
||||
})
|
||||
@ -271,3 +274,17 @@ export const GetOcrProviderResponseSchema = z.object({
|
||||
})
|
||||
|
||||
export type GetOcrProviderResponse = z.infer<typeof GetOcrProviderResponseSchema>
|
||||
|
||||
export const PatchOcrProviderRequestSchema = z.object({
|
||||
id: OcrProviderIdSchema,
|
||||
name: OcrProviderNameSchema.optional(),
|
||||
config: OcrProviderConfigSchema.partial().optional()
|
||||
})
|
||||
|
||||
export type PatchOcrProviderRequest = z.infer<typeof PatchOcrProviderRequestSchema>
|
||||
|
||||
export const PatchOcrProviderResponseSchema = z.object({
|
||||
data: DbOcrProviderSchema
|
||||
})
|
||||
|
||||
export type PatchOcrProviderResponse = z.infer<typeof PatchOcrProviderResponseSchema>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user