fix(suggestion): place overlay in DIPs, size it to content, and harden suggestion sessions
This commit is contained in:
parent
75e053d72f
commit
9e5b94ced2
18 changed files with 2178 additions and 558 deletions
|
|
@ -162,6 +162,16 @@ function suffixPrefixOverlap(prefix: string, candidate: string): number {
|
|||
return 0
|
||||
}
|
||||
|
||||
/** 로컬 기억 후보 하나 — 삽입할 텍스트와, 접두에 붙여 써야 하는지(단어 중간에서 이어짐). */
|
||||
export interface LocalSuggestionCandidate {
|
||||
text: string
|
||||
/**
|
||||
* true 면 접두의 단어 중간에서 이어지는 나머지다(예: "Thank yo" + "u for the update") —
|
||||
* 수락할 때 구분 공백을 넣지 않는다. joinSuggestion 의 joint 'attach' 에 해당한다.
|
||||
*/
|
||||
attachToPrefix: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* 로컬 기억만으로 삽입 가능한 다음 문자열을 만든다.
|
||||
*
|
||||
|
|
@ -175,6 +185,21 @@ export function buildLocalSuggestionCandidates(
|
|||
limit = 3,
|
||||
maxChars = SUGGESTION_MAX_OUTPUT_CHARS
|
||||
): string[] {
|
||||
return buildLocalSuggestionCandidateEntries(prefix, hints, limit, maxChars).map((entry) => entry.text)
|
||||
}
|
||||
|
||||
/**
|
||||
* buildLocalSuggestionCandidates 와 같은 후보를, 접두와의 이음새 정보와 함께 돌려준다.
|
||||
*
|
||||
* 문구 겹침(접두 꼬리 = 문구 머리)으로 잘라 낸 나머지가 공백으로 시작하지 않으면 단어
|
||||
* 중간에서 이어지는 것이다 — 정규화(trim)가 그 정보를 지우기 전에 여기서 기록한다.
|
||||
*/
|
||||
export function buildLocalSuggestionCandidateEntries(
|
||||
prefix: string,
|
||||
hints: LocalSuggestionHints,
|
||||
limit = 3,
|
||||
maxChars = SUGGESTION_MAX_OUTPUT_CHARS
|
||||
): LocalSuggestionCandidate[] {
|
||||
const safeLimit = Math.max(0, Math.floor(limit))
|
||||
const safeMaxChars = Math.max(0, Math.floor(maxChars))
|
||||
if (safeLimit === 0 || safeMaxChars < 2) return []
|
||||
|
|
@ -182,13 +207,13 @@ export function buildLocalSuggestionCandidates(
|
|||
const normalizedPrefix = normalizeRequestPrefix(prefix)
|
||||
const normalizedPrefixLower = normalizedPrefix.toLowerCase()
|
||||
const acceptsWholePhrase = endsSentence(normalizedPrefix)
|
||||
const out: string[] = []
|
||||
const out: LocalSuggestionCandidate[] = []
|
||||
|
||||
const append = (candidate: string | null): void => {
|
||||
const append = (candidate: string | null, attachToPrefix = false): void => {
|
||||
if (candidate === null || candidate.length === 0) return
|
||||
if (candidate.toLowerCase() === normalizedPrefixLower) return
|
||||
if (out.some((existing) => existing.toLowerCase() === candidate.toLowerCase())) return
|
||||
out.push(candidate)
|
||||
if (out.some((existing) => existing.text.toLowerCase() === candidate.toLowerCase())) return
|
||||
out.push({ text: candidate, attachToPrefix })
|
||||
}
|
||||
|
||||
for (const raw of hints.continuationHints) {
|
||||
|
|
@ -206,7 +231,8 @@ export function buildLocalSuggestionCandidates(
|
|||
if (candidate === null) continue
|
||||
const overlap = suffixPrefixOverlap(normalizedPrefix, candidate)
|
||||
if (overlap >= 2) {
|
||||
append(normalizeSuggestionText(candidate.slice(overlap), safeMaxChars))
|
||||
const remainder = candidate.slice(overlap)
|
||||
append(normalizeSuggestionText(remainder, safeMaxChars), !/^\s/u.test(remainder))
|
||||
} else if (acceptsWholePhrase) {
|
||||
append(candidate)
|
||||
}
|
||||
|
|
@ -215,3 +241,53 @@ export function buildLocalSuggestionCandidates(
|
|||
|
||||
return out
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 수락 시 이음새
|
||||
// ============================================================
|
||||
|
||||
/**
|
||||
* 후보가 접두와 어떻게 이어지는가.
|
||||
* - separate: 새 단어/문장이다 — 띄어 쓰는 문자 체계면 공백 하나로 떼어 쓴다.
|
||||
* - attach: 접두의 단어 중간에서 이어지는 나머지다 — 그대로 붙인다.
|
||||
*/
|
||||
export type SuggestionJoint = 'separate' | 'attach'
|
||||
|
||||
/** 이 문자 뒤에는 떼어 쓰지 않는다 (여는 괄호·따옴표, 경로·멘션·해시 기호). */
|
||||
const NO_SPACE_AFTER = /[([{<“‘«‹「『【〔〈《([{/\\@#_-]/u
|
||||
/** 이 문자로 시작하는 후보는 앞 글자에 붙는다 (닫는 괄호·구두점). */
|
||||
const NO_SPACE_BEFORE = /[.,;:!?)\]}>…%’”»›」』】〕〉》、。,.!?:;)]}]/u
|
||||
/** 단어 사이를 띄어 쓰지 않는 문자 체계 + CJK 구두점·전각 기호. */
|
||||
const UNSPACED_JOIN_SCRIPT =
|
||||
/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Thai}\p{Script=Lao}\p{Script=Khmer}\p{Script=Myanmar}\u3000-\u303f\uff00-\uffef]/u
|
||||
|
||||
function lastCodePoint(text: string): string {
|
||||
const chars = Array.from(text)
|
||||
return chars[chars.length - 1] ?? ''
|
||||
}
|
||||
|
||||
function firstCodePoint(text: string): string {
|
||||
return Array.from(text)[0] ?? ''
|
||||
}
|
||||
|
||||
/**
|
||||
* 수락한 후보를 커서 위치에 넣을 문자열로 만든다 (필요하면 앞에 공백 하나).
|
||||
*
|
||||
* 후보는 정제 과정에서 앞 공백이 모두 사라진다. 사용자는 대개 마지막 글자(단어 끝이나
|
||||
* 마침표) 바로 뒤에서 멈추므로, 그대로 붙이면 "report.Let me…", "회의에서논의한…" 처럼
|
||||
* 들러붙는다. 다음 경우에만 공백을 넣는다:
|
||||
* - 커서 앞 원문(rawPrefix)이 비어 있지 않고 공백으로 끝나지 않는다.
|
||||
* - 후보가 접두의 단어 중간에서 이어지는 나머지(joint 'attach')가 아니다.
|
||||
* - 앞 글자가 여는 괄호/따옴표·경로 기호가 아니고, 후보가 닫는 구두점으로 시작하지 않는다.
|
||||
* - 이음새 양쪽 어느 쪽도 띄어 쓰지 않는 문자 체계(중·일·태 등, CJK 구두점)가 아니다.
|
||||
*/
|
||||
export function joinSuggestion(rawPrefix: string, candidate: string, joint: SuggestionJoint = 'separate'): string {
|
||||
if (!candidate || joint === 'attach') return candidate
|
||||
const before = lastCodePoint(rawPrefix)
|
||||
const after = firstCodePoint(candidate)
|
||||
if (!before || !after) return candidate
|
||||
if (/\s/u.test(before) || /\s/u.test(after)) return candidate
|
||||
if (NO_SPACE_AFTER.test(before) || NO_SPACE_BEFORE.test(after)) return candidate
|
||||
if (UNSPACED_JOIN_SCRIPT.test(before) || UNSPACED_JOIN_SCRIPT.test(after)) return candidate
|
||||
return ` ${candidate}`
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue