fix(desktop): add precise local OCR reading

This commit is contained in:
Yun Chan 2026-11-21 10:15:49 +09:00
parent 59cdddbecb
commit 25fd3ac004
30 changed files with 626 additions and 15 deletions

View file

@ -47,6 +47,7 @@ interface HarnessOptions {
/** 인식을 곧바로 끝낼지 (기본 true). false 면 finishRecognition 을 부를 때까지 걸려 있다. */
autoRecognize?: boolean
openExternal?: (url: string) => Promise<boolean>
precision?: ScreenOcrDeps['precision']['recognize']
translate?: ScreenOcrDeps['llm']['translate']
renderPdf?: ScreenOcrDeps['renderPdf']
clipboardError?: Error
@ -93,6 +94,7 @@ function createHarness(options: HarnessOptions = {}): Harness {
})
}
},
precision: { recognize: options.precision ?? (async () => '정밀 결과') },
overlay: {
show: (bounds, payload) => shown.push({ bounds, payload }),
sendRecognized: (payload) => recognized.push(payload),
@ -472,6 +474,77 @@ describe('ScreenOcrService 요청 처리', () => {
})
})
describe('GLM-OCR 선택 영역 정밀 읽기', () => {
it('정밀읽기_선택영역_크롭만모델에볼내고임시파일을정리한다', async () => {
const h = createHarness({
precision: async (request) => {
expect(h.temp.get(request.imagePath)?.toString()).toBe('crop-10-20-100-40')
expect(request.maxOutputTokens).toBe(256)
return '정확한 결과'
}
})
await open(h)
await h.service.handleRequest(h.sessionId(), 1, {
type: 'refine-text',
text: '원문',
rect: { x: 10, y: 20, width: 100, height: 40 }
})
expect(h.responses).toEqual([{ requestId: 1, ok: true, text: '정확한 결과' }])
expect(h.temp.size).toBe(0)
expect(h.clipboard.text).toBeNull()
})
it('정밀읽기_모델이추가한바깥코드펜스_본문과원문에펜스가없으면제거한다', async () => {
const h = createHarness({ precision: async () => '```javascript\nconst x = 1;\n```' })
await open(h)
await h.service.handleRequest(h.sessionId(), 1, { type: 'refine-text', text: 'const x = 1;', rect: null })
expect(h.responses).toEqual([{ requestId: 1, ok: true, text: 'const x = 1;' }])
})
it('정밀읽기_원문에코드펜스가있으면_제거하지않는다', async () => {
const text = '```javascript\nconst x = 1;\n```'
const h = createHarness({ precision: async () => text })
await open(h)
await h.service.handleRequest(h.sessionId(), 1, { type: 'refine-text', text, rect: null })
expect(h.responses).toEqual([{ requestId: 1, ok: true, text }])
})
it('정밀읽기_비정상적으로긴 반복결과_실패로처리하고임시파일을정리한다', async () => {
const h = createHarness({ precision: async () => 'x'.repeat(4001) })
await open(h)
await h.service.handleRequest(h.sessionId(), 1, { type: 'refine-text', text: '짧음', rect: null })
expect(h.responses).toEqual([{ requestId: 1, ok: false, error: 'recognition' }])
expect(h.temp.size).toBe(0)
})
it('정밀읽기_요청이진행중일때_중복요청을거부한다', async () => {
let finish: ((text: string) => void) | undefined
const precision = vi.fn(() => new Promise<string>((resolve) => { finish = resolve }))
const h = createHarness({ precision })
await open(h)
const first = h.service.handleRequest(h.sessionId(), 1, { type: 'refine-text', text: '첫 요청', rect: null })
const second = h.service.handleRequest(h.sessionId(), 2, { type: 'refine-text', text: '둘째 요청', rect: null })
await second
finish?.('첫 결과')
await first
expect(precision).toHaveBeenCalledTimes(1)
expect(h.responses).toContainEqual({ requestId: 2, ok: false, error: 'invalid' })
expect(h.responses).toContainEqual({ requestId: 1, ok: true, text: '첫 결과' })
})
it('정밀읽기_진행중닫기_응답을버리고임시파일을정리한다', async () => {
let finish: ((text: string) => void) | undefined
const h = createHarness({ precision: () => new Promise<string>((resolve) => { finish = resolve }) })
await open(h)
const pending = h.service.handleRequest(h.sessionId(), 1, { type: 'refine-text', text: '원문', rect: null })
h.service.close()
finish?.('늦은 결과')
await pending
expect(h.responses).toEqual([])
expect(h.temp.size).toBe(0)
})
})
describe('Windows OCR 엔진 어댑터', () => {
it('엔진인식_정상출력_정본결과로옮기고입력은환경변수로만넘긴다', async () => {
const calls: Array<Record<string, string>> = []