diff --git a/apps/desktop/src/main/services/SuggestionService.ts b/apps/desktop/src/main/services/SuggestionService.ts index 5c940f7..36b6a1d 100644 --- a/apps/desktop/src/main/services/SuggestionService.ts +++ b/apps/desktop/src/main/services/SuggestionService.ts @@ -44,6 +44,7 @@ import { import { renderRawChatPrompt, rawChatStopSequences } from '@d3ro/core/chat-template' import { buildQueryTerms, splitSentences } from '@d3ro/core/personal-graph' import { analyzeWritingContext, findUnsupportedFacts } from '@d3ro/core/writing-context' +import { alignCorrectionToPrefix } from '@d3ro/core/typo-correction' import { configGet, configSet } from './ConfigService' import { getLogger } from './LoggerService' import { getLocalLLMService } from './LocalLLMService' @@ -1920,7 +1921,7 @@ export class SuggestionService extends EventEmitter { // 교정이 있으면 커서 앞 오타 구간을 지우고 고친 글 + 후보를 함께 넣는다. 그사이 원문 끝이 달라졌으면 // (IME 마지막 글자 교체 등) 교정은 버리고 후보만 넣는다 — 엉뚱한 글자를 지우지 않는다. const acceptPrefix = this._acceptPrefix() - const correction = this._correction && acceptPrefix.endsWith(this._correction.from) ? this._correction : null + const correction = this._correction ? alignCorrectionToPrefix(acceptPrefix, this._correction) : null const basePrefix = correction ? acceptPrefix.slice(0, acceptPrefix.length - correction.from.length) + correction.to : acceptPrefix diff --git a/packages/core/__tests__/typo-correction.test.ts b/packages/core/__tests__/typo-correction.test.ts index 6d820be..28c76e4 100644 --- a/packages/core/__tests__/typo-correction.test.ts +++ b/packages/core/__tests__/typo-correction.test.ts @@ -18,6 +18,7 @@ import { latinToHangul, parseModelTypoPairs, planTypoReplacement, + alignCorrectionToPrefix, validateModelCorrections, typoRegionStart, validateModelCorrections @@ -255,3 +256,25 @@ describe('낱자 잡음 (2026-10-17 실측 입력)', () => { expect(planTypoReplacement(text, mergeTypoCorrections(model, detectJamoNoise(text)))?.insertText).toBe('안녕하세요.?') }) }) + +describe('alignCorrectionToPrefix — 교정이 뜬 뒤 끝 공백만 달라져도 수락한다', () => { + const correction = { from: '그ㅓ런', to: '그런', edits: [] } + + it('원문이 그대로면 교정을 그대로 돌려준다', () => { + expect(alignCorrectionToPrefix('그ㅓ런', correction)).toBe(correction) + }) + + it('공백을 더 쳤으면 그 공백까지 지우고 다시 붙인다', () => { + expect(alignCorrectionToPrefix('음 그ㅓ런 ', correction)).toEqual({ ...correction, from: '그ㅓ런 ', to: '그런 ' }) + }) + + it('교정 뒤 공백을 지웠으면 지금 원문에 맞춘다', () => { + const spaced = { from: '그ㅓ런 ', to: '그런 ', edits: [] } + expect(alignCorrectionToPrefix('그ㅓ런', spaced)).toEqual({ ...spaced, from: '그ㅓ런', to: '그런' }) + }) + + it('글자가 바뀌었거나 줄바꿈이 끼면 버린다', () => { + expect(alignCorrectionToPrefix('그ㅓ러', correction)).toBeNull() + expect(alignCorrectionToPrefix('그ㅓ런\n', correction)).toBeNull() + }) +}) diff --git a/packages/core/src/typo-correction.ts b/packages/core/src/typo-correction.ts index ad08b11..95354c5 100644 --- a/packages/core/src/typo-correction.ts +++ b/packages/core/src/typo-correction.ts @@ -824,3 +824,23 @@ export function detectTypoRevert(currentPrefix: string, applied: { from: string; const tokens = tokenizeWords(window).map((token) => token.text) return tokens.includes(applied.from) && !tokens.includes(applied.to) } + +/** + * 표시된 교정을 수락 시점의 커서 앞 원문에 맞춘다. 맞출 수 없으면 null. + * + * 교정 구간(from)은 만들 때의 끝 공백을 그대로 담지만, 제안 세션은 끝 공백만 달라진 글을 + * 같은 세션으로 본다. 교정이 뜬 뒤 공백을 하나 더 치거나 지우면 from 이 원문 끝과 어긋나 + * 화면에 보이던 교정이 조용히 버려졌다 — 끝 공백을 빼고 비교하고, 지금의 끝 공백을 붙여 돌려준다. + */ +export function alignCorrectionToPrefix( + prefix: string, + correction: T +): T | null { + if (prefix.endsWith(correction.from)) return correction + const fromCore = correction.from.trimEnd() + const prefixCore = prefix.trimEnd() + const trailing = prefix.slice(prefixCore.length) + // 줄바꿈까지 지우고 다시 쓰는 건 앱마다 결과가 달라 위험하다. + if (fromCore.length === 0 || /[\r\n]/u.test(trailing) || !prefixCore.endsWith(fromCore)) return null + return { ...correction, from: fromCore + trailing, to: correction.to.trimEnd() + trailing } +}