d3ro-voice/apps/desktop/src/main/services/DictationTemplateService.ts
윤찬 e55687d298 fix(desktop+supabase): 로컬 ID UUID 통일 + Realtime publication (빅뱅 Phase 5 Part 2)
SaaS [9] — 실증 A/B/C/D 전부 통과, 로컬→클라우드 push 최초 성공(pushed=1).

## Bug 4: 로컬 nanoid PK vs Supabase UUID PK 불일치
- 증상: Push history failed: invalid input syntax for type uuid: "fvy6bIzr..."
- 원인: 로컬 drizzle schema는 text PK + nanoid() 생성, Supabase는 uuid PK.
  빅뱅 사이클 내내 push가 한 번도 성공한 적 없었음 (지난 pushed=0은 데이터 0건이라서).
- 픽스: 로컬을 UUID로 통일 (근본 해결, 땜질 금지).
  14개 서비스 20곳 nanoid() → crypto.randomUUID() 일괄 교체.
  nanoid 의존성 + electron.vite.config exclude 제거.
  drizzle schema는 text PK 그대로 유지 (SQLite는 UUID 문자열 저장 가능).

## Bug 5: supabase_realtime publication 누락
- 증상: 로그인 직후 Realtime 채널 상태: TIMED_OUT
- 원인: initial_schema.sql이 transcripts 테이블만 publication에 추가.
  데스크톱이 구독하는 meetings/history/dictionary는 누락 → postgres_changes 흐르지 않음.
- 픽스: 20260411000002_realtime_publication.sql 신규.
  pg_publication_tables 카탈로그 체크 + 조건부 ADD TABLE (meetings/meeting_memos/
  meeting_documents/history/dictionary 5개). supabase db push 적용.

## Bug 6: persistSession:false에서 realtime.setAuth 자동 전파 안 됨 (부분 픽스)
- 픽스: CloudSyncService.startRealtime()에 client.realtime.setAuth(access_token)
  명시 호출 (채널 구성 이전).
- ⚠️ Bug 5+6 적용 후에도 Realtime 여전히 TIMED_OUT. 후속 조사 필요.
  블로커 아님 — 주기 pull + Phase 3.3 auto push로 최종 일관성 유지.

## 실증 결과
- A 세션 자동 복원: Restored session for yunchan8804@gmail.com → DB 재오픈
- B push 경로: HistoryService created 56a767ac-... → Sync complete pushed=1 errors=0
- C 로그아웃 복귀: Realtime 종료 → users/_local/d3ro.db 복귀 → local mode
- D 재로그인 복원: 실증 A의 restore 경로와 동일, 같은 uuid DB 파일 보존
- E 웹 크로스 디바이스: Phase 3.3 이후로 지연 (Realtime 이슈 별건)

검증: desktop tsc --noEmit , dev 재기동 , push 최초 성공 
2026-04-11 18:46:22 +09:00

306 lines
9.7 KiB
TypeScript

// src/main/services/DictationTemplateService.ts
// Phase 12.3: 딕테이션 템플릿 서비스
// 템플릿 CRUD + 세션 상태 머신 (필드별 음성 입력)
import { EventEmitter } from 'events'
import Store from 'electron-store'
import { getLogger } from './LoggerService'
import { getMainWindow } from '../windows/WindowManager'
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
import { D3ROError, ErrorCode } from '@d3ro/core/errors'
import type {
DictationTemplate,
TemplateField,
TemplateSessionState,
TemplateSessionInfo,
TemplateFieldCompletedEvent,
TemplateSessionCompletedEvent,
CreateTemplateParams,
UpdateTemplateParams,
} from '@d3ro/core/types'
const logger = getLogger('DictationTemplateService')
// ── 프리셋 템플릿 ──
const BUILTIN_TEMPLATES: DictationTemplate[] = [
{
id: 'builtin-email',
name: 'Email',
description: 'Email template with recipient, subject, and body',
fields: [
{ id: 'recipient', name: 'recipient', label: 'Recipient', promptText: 'Who is this email for?', required: true, maxDurationSec: 15 },
{ id: 'subject', name: 'subject', label: 'Subject', promptText: 'What is the subject?', required: true, maxDurationSec: 15 },
{ id: 'body', name: 'body', label: 'Body', promptText: 'Please dictate the email body.', required: true, maxDurationSec: 120 },
],
outputFormat: 'To: {{recipient}}\nSubject: {{subject}}\n\n{{body}}',
isBuiltin: true,
createdAt: Date.now(),
updatedAt: Date.now(),
},
{
id: 'builtin-meeting-notes',
name: 'Meeting Notes',
description: 'Meeting notes template',
fields: [
{ id: 'title', name: 'title', label: 'Title', promptText: 'What is the meeting title?', required: true, maxDurationSec: 15 },
{ id: 'attendees', name: 'attendees', label: 'Attendees', promptText: 'Who attended?', required: false, maxDurationSec: 30 },
{ id: 'agenda', name: 'agenda', label: 'Agenda', promptText: 'What was discussed?', required: true, maxDurationSec: 120 },
{ id: 'decisions', name: 'decisions', label: 'Decisions', promptText: 'What decisions were made?', required: false, maxDurationSec: 60 },
],
outputFormat: '# {{title}}\n\nAttendees: {{attendees}}\n\n## Agenda\n{{agenda}}\n\n## Decisions\n{{decisions}}',
isBuiltin: true,
createdAt: Date.now(),
updatedAt: Date.now(),
},
{
id: 'builtin-report',
name: 'Report',
description: 'Simple report template',
fields: [
{ id: 'title', name: 'title', label: 'Title', promptText: 'Report title?', required: true, maxDurationSec: 15 },
{ id: 'summary', name: 'summary', label: 'Summary', promptText: 'Summarize the key points.', required: true, maxDurationSec: 60 },
{ id: 'details', name: 'details', label: 'Details', promptText: 'Provide the details.', required: true, maxDurationSec: 180 },
],
outputFormat: '# {{title}}\n\n## Summary\n{{summary}}\n\n## Details\n{{details}}',
isBuiltin: true,
createdAt: Date.now(),
updatedAt: Date.now(),
},
]
interface TemplateStoreSchema {
templates: DictationTemplate[]
}
class DictationTemplateService extends EventEmitter {
private _store: Store<TemplateStoreSchema>
private _session: TemplateSessionInfo | null = null
constructor() {
super()
this._store = new Store<TemplateStoreSchema>({
name: 'dictation-templates',
defaults: {
templates: [...BUILTIN_TEMPLATES],
},
})
// 프리셋이 없으면 추가
this._ensureBuiltins()
}
// ── CRUD ──
getAll(): DictationTemplate[] {
return this._store.get('templates', [])
}
getById(id: string): DictationTemplate | null {
const templates = this.getAll()
return templates.find((t) => t.id === id) ?? null
}
create(params: CreateTemplateParams): DictationTemplate {
const template: DictationTemplate = {
id: crypto.randomUUID(),
name: params.name,
description: params.description,
fields: params.fields,
outputFormat: params.outputFormat,
isBuiltin: false,
createdAt: Date.now(),
updatedAt: Date.now(),
}
const templates = this.getAll()
templates.push(template)
this._store.set('templates', templates)
return template
}
update(params: UpdateTemplateParams): DictationTemplate {
const templates = this.getAll()
const idx = templates.findIndex((t) => t.id === params.id)
if (idx === -1) {
throw new D3ROError(ErrorCode.TemplateNotFound, `Template not found: ${params.id}`)
}
const existing = templates[idx]
const updated: DictationTemplate = {
...existing,
...(params.name !== undefined && { name: params.name }),
...(params.description !== undefined && { description: params.description }),
...(params.fields !== undefined && { fields: params.fields }),
...(params.outputFormat !== undefined && { outputFormat: params.outputFormat }),
updatedAt: Date.now(),
}
templates[idx] = updated
this._store.set('templates', templates)
return updated
}
delete(id: string): void {
const templates = this.getAll()
const template = templates.find((t) => t.id === id)
if (!template) {
throw new D3ROError(ErrorCode.TemplateNotFound, `Template not found: ${id}`)
}
if (template.isBuiltin) {
throw new D3ROError(ErrorCode.TemplateInvalidFormat, 'Cannot delete builtin template')
}
this._store.set(
'templates',
templates.filter((t) => t.id !== id),
)
}
// ── 세션 관리 ──
getSessionState(): TemplateSessionInfo | null {
return this._session
}
startSession(templateId: string): void {
if (this._session) {
throw new D3ROError(ErrorCode.TemplateSessionAlreadyActive, 'Template session already active')
}
const template = this.getById(templateId)
if (!template) {
throw new D3ROError(ErrorCode.TemplateNotFound, `Template not found: ${templateId}`)
}
if (template.fields.length === 0) {
throw new D3ROError(ErrorCode.TemplateInvalidFormat, 'Template has no fields')
}
this._session = {
templateId,
templateName: template.name,
state: 'field-prompting',
currentFieldIndex: 0,
totalFields: template.fields.length,
currentField: template.fields[0],
fieldValues: {},
}
this._emitSessionState()
logger.info(`Template session started: ${template.name}`)
}
setFieldValue(fieldId: string, value: string): void {
if (!this._session) {
throw new D3ROError(ErrorCode.TemplateSessionNotActive, 'No active template session')
}
const template = this.getById(this._session.templateId)
if (!template) {
throw new D3ROError(ErrorCode.TemplateNotFound, 'Session template not found')
}
this._session.fieldValues[fieldId] = value
const currentField = this._session.currentField
const nextIndex = this._session.currentFieldIndex + 1
const nextField = nextIndex < template.fields.length ? template.fields[nextIndex] : null
// 필드 완료 이벤트
const fieldEvent: TemplateFieldCompletedEvent = {
fieldId,
fieldName: currentField?.name ?? fieldId,
value,
nextField,
}
this._sendToRenderer(IPC_CHANNELS.DICTATION_TEMPLATE.FIELD_COMPLETED, fieldEvent)
this.emit('field-completed', fieldEvent)
if (nextField) {
// 다음 필드로 이동
this._session.currentFieldIndex = nextIndex
this._session.currentField = nextField
this._session.state = 'field-prompting'
this._emitSessionState()
} else {
// 모든 필드 완료 → 출력 생성
this._completeSession(template)
}
}
cancelSession(): void {
if (!this._session) return
logger.info('Template session cancelled')
this._session = null
this._emitSessionState()
}
private _completeSession(template: DictationTemplate): void {
if (!this._session) return
this._session.state = 'completing'
this._emitSessionState()
// 출력 포맷 적용 (mustache-like 치환)
let output = template.outputFormat
for (const [key, value] of Object.entries(this._session.fieldValues)) {
output = output.replace(new RegExp(`\\{\\{${key}\\}\\}`, 'g'), value)
}
// 미입력 필드 자리표시자 제거
output = output.replace(/\{\{[^}]+\}\}/g, '')
const completedEvent: TemplateSessionCompletedEvent = {
templateId: template.id,
outputText: output.trim(),
fieldValues: { ...this._session.fieldValues },
}
this._sendToRenderer(IPC_CHANNELS.DICTATION_TEMPLATE.SESSION_COMPLETED, completedEvent)
this.emit('session-completed', completedEvent)
this._session = null
logger.info(`Template session completed: ${template.name}`)
}
private _emitSessionState(): void {
this._sendToRenderer(IPC_CHANNELS.DICTATION_TEMPLATE.SESSION_STATE_CHANGED, this._session)
this.emit('session-state-changed', this._session)
}
private _sendToRenderer(channel: string, data: unknown): void {
try {
const mainWindow = getMainWindow()
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send(channel, data)
}
} catch {
// 윈도우 없으면 무시
}
}
private _ensureBuiltins(): void {
const templates = this.getAll()
for (const builtin of BUILTIN_TEMPLATES) {
if (!templates.find((t) => t.id === builtin.id)) {
templates.push(builtin)
}
}
this._store.set('templates', templates)
}
dispose(): void {
this.cancelSession()
this.removeAllListeners()
}
}
// ── 싱글톤 ──
let instance: DictationTemplateService | null = null
export function getDictationTemplateService(): DictationTemplateService {
if (!instance) {
instance = new DictationTemplateService()
}
return instance
}