fix(desktop): harden session, meeting, caption and LLM lifecycles; route LLM calls through the gateway

This commit is contained in:
Yun Chan 2026-09-28 02:16:15 +09:00
parent 3a46437f28
commit ddc78546f0
62 changed files with 4786 additions and 648 deletions

View file

@ -1,7 +1,7 @@
// src/main/services/DictionaryService.ts
// 사용자 커스텀 단어 사전. 설계서 01/03 IDictionaryService 구현.
import { eq, and, like, desc, count, sql } from 'drizzle-orm'
import { eq, and, desc, count, sql } from 'drizzle-orm'
import path from 'path'
import fs from 'fs'
import { app, dialog } from 'electron'
@ -12,8 +12,11 @@ import { getLogger } from './LoggerService'
import { getCloudSyncService } from './CloudSyncService'
import { getMainWindow } from '../windows/WindowManager'
import { D3ROError, ErrorCode } from '@d3ro/core/errors'
import { containsPattern } from '../db/like-pattern'
import {
buildDictionaryUpdatePatch,
dictionaryIdentityKey,
findDictionaryIdentityConflict,
isUniqueConstraintViolation,
normalizeDictionaryPronunciation,
normalizeDictionaryWord
@ -118,6 +121,43 @@ function normalizeCategory(value: string | null): DictionaryEntry['category'] {
return 'user'
}
type DictionaryDb = Pick<ReturnType<typeof getDatabase>, 'select'>
/**
* 같은 카테고리에 대소문자·앞뒤 공백만 다른 단어가 있으면 그 행. 서버 유일성(lower(btrim(word)))과 같은
* 기준으로 로컬 입력 단계에서 거절한다 — 로컬 인덱스는 대소문자를 구분해 그대로 받아들이고, 동기화가
* 나중에 사용자의 항목을 서버 행으로 조용히 대체했다. (Unicode 대소문자까지 JS 로 비교한다)
*/
function findCaseInsensitiveDuplicate(
db: DictionaryDb,
word: string,
category: DictionaryEntry['category'],
excludeId?: string
): { id: string; word: string } | null {
const candidates = db
.select({ id: dictionary.id, word: dictionary.word })
.from(dictionary)
.where(eq(dictionary.category, category))
.all()
return findDictionaryIdentityConflict(word, candidates, excludeId)
}
/**
* 가져오기용 한 번 조회 — 대량 행을 JS 로 비교하지 않고 SQL 에서 (lower(trim(word)), category) 로 찾는다.
* SQLite lower() 는 ASCII 만 접으므로 비 ASCII 대소문자 변형은 추가·수정 경로(JS 비교)만큼 엄밀하지 않다.
*/
function findImportDuplicate(
db: DictionaryDb,
word: string,
category: DictionaryEntry['category']
): { id: string } | undefined {
return db
.select({ id: dictionary.id })
.from(dictionary)
.where(and(sql`lower(trim(${dictionary.word})) = ${dictionaryIdentityKey(word)}`, eq(dictionary.category, category)))
.get()
}
class DictionaryService {
add(params: DictionaryAddParams): DictionaryEntry {
const word = normalizeDictionaryWord(params.word)
@ -128,12 +168,17 @@ class DictionaryService {
const db = getDatabase()
const now = Date.now()
const id = crypto.randomUUID()
const category = params.category ?? 'user'
const duplicate = findCaseInsensitiveDuplicate(db, word, category)
if (duplicate) {
throw new D3ROError(ErrorCode.DictionaryDuplicate, `Duplicate word: ${word} (existing: ${duplicate.word})`)
}
const entry: NewDictionary = {
id,
word,
pronunciation: normalizeDictionaryPronunciation(params.pronunciation) ?? null,
category: params.category ?? 'user',
category,
usageCount: 0,
lastUsedAt: null,
createdAt: now,
@ -164,6 +209,15 @@ class DictionaryService {
const existing = db.select().from(dictionary).where(eq(dictionary.id, params.id)).get()
if (!existing) return null
if (result.patch.word !== undefined || result.patch.category !== undefined) {
const nextWord = result.patch.word ?? existing.word
const nextCategory = result.patch.category ?? (existing.category as DictionaryEntry['category'])
const duplicate = findCaseInsensitiveDuplicate(db, nextWord, nextCategory, existing.id)
if (duplicate) {
throw new D3ROError(ErrorCode.DictionaryDuplicate, `Duplicate word: ${nextWord} (existing: ${duplicate.word})`)
}
}
const updates: Partial<NewDictionary> = { ...result.patch, updatedAt: Date.now() }
try {
db.update(dictionary).set(updates).where(eq(dictionary.id, params.id)).run()
@ -217,19 +271,20 @@ class DictionaryService {
search(params: DictionarySearchParams): DictionaryPage {
const db = getDatabase()
const { query, page, pageSize } = params
const pattern = `%${query}%`
// 검색어의 %·_ 는 글자 그대로 찾는다(와일드카드로 해석하지 않는다)
const condition = containsPattern(query).match(dictionary.word)
const totalResult = db
.select({ count: count() })
.from(dictionary)
.where(like(dictionary.word, pattern))
.where(condition)
.get()
const total = totalResult?.count ?? 0
const entries = db
.select()
.from(dictionary)
.where(like(dictionary.word, pattern))
.where(condition)
.orderBy(desc(dictionary.usageCount))
.limit(pageSize)
.offset(page * pageSize)
@ -365,11 +420,8 @@ class DictionaryService {
continue
}
const category = normalizeCategory(pickString(row, ['category']))
const existing = tx
.select({ id: dictionary.id })
.from(dictionary)
.where(and(eq(dictionary.word, word), eq(dictionary.category, category)))
.get()
// 대소문자만 다른 단어도 같은 단어로 본다(서버 유일성과 같은 기준)
const existing = findImportDuplicate(tx, word, category)
if (existing) {
outcome.skipped += 1
continue