diff --git a/.forgejo/workflows/portable.yml b/.forgejo/workflows/portable.yml index 205659b..60672da 100644 --- a/.forgejo/workflows/portable.yml +++ b/.forgejo/workflows/portable.yml @@ -62,11 +62,27 @@ jobs: FORGEJO_TOKEN: "${{ secrets.FORGEJO_TOKEN }}" run: node scripts/ci/publish-portable-release.mjs - - name: 아티팩트 업로드 - uses: actions/upload-artifact@v4 - with: - name: d3ro-voice-portable-${{ github.ref_name }} - path: | - apps/desktop/release/*/*-portable.7z.00* - apps/desktop/release/*/portable.json - bucket/d3ro-voice.json \ No newline at end of file + - name: 게시 요약 출력 + # Forgejo Actions 러너는 actions/upload-artifact@v4 의 GHES 스토리지 프로토콜을 지원하지 않으므로(GAP-REL-11), + # 실제 배포 산출물은 앞 단계에서 Forgejo Generic Package Registry 에 이미 게시됐고 여기서는 해시 요약을 남긴다. + run: | + $version = node -p "require('./release/product-version.json').version" + $manifestPath = "apps/desktop/release/$version/portable.json" + if (-not (Test-Path $manifestPath)) { + throw "portable.json 이 없습니다: $manifestPath" + } + $manifest = Get-Content $manifestPath -Raw | ConvertFrom-Json + $summaryLines = @( + "### Portable Release ($($manifest.version))", + "- Volumes: $($manifest.volumeCount)", + "- Total Size: $([math]::Round($manifest.totalSize / 1MB, 1)) MiB", + "- Feed: https://git.chanpaca.net/api/packages/yunchan/generic/d3ro-voice/portable-$($manifest.version)/portable.json" + ) + foreach ($vol in $manifest.volumes) { + $summaryLines += " - ``$($vol.name)``: ``$($vol.sha256)``" + } + $summaryText = $summaryLines -join "`n" + Write-Host $summaryText + if ($env:GITHUB_STEP_SUMMARY) { + Add-Content -Path $env:GITHUB_STEP_SUMMARY -Value $summaryText -Encoding utf8 + } \ No newline at end of file diff --git a/.forgejo/workflows/release.yml b/.forgejo/workflows/release.yml index 12020e8..19e3d4f 100644 --- a/.forgejo/workflows/release.yml +++ b/.forgejo/workflows/release.yml @@ -40,7 +40,30 @@ jobs: run: | $releaseVersion = node -p "require('./release/product-version.json').version" $url = "https://git.chanpaca.net/api/packages/yunchan/generic/d3ro-voice/$releaseVersion/latest.yml" - try { Invoke-WebRequest -Uri $url -Method Get -UseBasicParsing | Out-Null; $published = 'true' } catch { $published = 'false' } + $filesApiUrl = "https://git.chanpaca.net/api/v1/packages/yunchan/generic/d3ro-voice/$releaseVersion/files" + $published = 'false' + # 로컬 `npm run release` 는 annotated 태그를 먼저 푸시한 직후 설치본과 latest.yml 을 게시한다(GAP-REL-14). + # 이미 올라왔거나 업로드 진행 중이면 완료될 때까지 대기해 CI 가 중복 빌드/충돌하지 않게 한다. + for ($attempt = 0; $attempt -lt 10; $attempt++) { + try { + Invoke-WebRequest -Uri $url -Method Get -UseBasicParsing | Out-Null + $published = 'true' + break + } catch {} + if ($attempt -lt 9) { + $inProgress = $false + try { + $files = Invoke-RestMethod -Uri $filesApiUrl -Method Get + if ($files -and $files.Count -gt 0) { $inProgress = $true } + } catch {} + if ($attempt -lt 3 -or $inProgress) { + Write-Host "로컬 릴리스 게시 확인 대기 중... ($($attempt + 1)/10)" + Start-Sleep -Seconds 10 + continue + } + } + break + } "published=$published" >> $env:GITHUB_OUTPUT if ($published -eq 'true') { Write-Host "$releaseVersion 는 이미 피드에 있습니다 — 건너뜁니다." } @@ -61,4 +84,12 @@ jobs: FORGEJO_TOKEN: "${{ secrets.FORGEJO_TOKEN }}" D3RO_UPDATE_SIGNING_KEY: "${{ secrets.D3RO_UPDATE_SIGNING_KEY }}" FORGEJO_REPO: "${{ github.server_url }}/${{ github.repository }}" - run: node scripts/ci/publish-forgejo-release.mjs + run: | + $releaseVersion = node -p "require('./release/product-version.json').version" + $url = "https://git.chanpaca.net/api/packages/yunchan/generic/d3ro-voice/$releaseVersion/latest.yml" + try { + Invoke-WebRequest -Uri $url -Method Get -UseBasicParsing | Out-Null + Write-Host "$releaseVersion 는 빌드 도중 로컬 릴리스에서 이미 게시됐습니다 — 게시 단계를 건너뜁니다." + exit 0 + } catch {} + node scripts/ci/publish-forgejo-release.mjs diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7fbaaa5..55fe728 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -501,8 +501,7 @@ publish-release: - if: '$CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+.*$/' # ──────────────────────────────────────────────────────────────────── -# Admin NAS deployment remains disabled until an immutable image, authenticated -# remote target, health check, and rollback path are configured. Do not revive -# the former DinD job: it only changed an ephemeral CI daemon and referenced a -# non-existent compose file, so it never deployed the NAS. +# Admin/Web/API NAS deployment uses the canonical runbook in +# docs/deployment/nas-deploy-runbook.md against docker-compose.nas.yml. +# Do not revive the former DinD job: it only changed an ephemeral CI daemon. # ──────────────────────────────────────────────────────────────────── diff --git a/CHANGELOG.md b/CHANGELOG.md index 15136aa..134d220 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Additional Whisper model management UI - Plugin system for custom pipelines +## [1.15.0] - 2026-10-31 + +> Published from an annotated tag. Installer and update metadata are served by the +> canonical Forgejo feed; no binaries are committed to this repository. + +### Added +- **Multi-command ordered pipeline selection & reordering in Commands (`CommandsPage.tsx`, `@d3ro/core/instruction-template`, `VoiceModeService`).** + - Replaces single-select radio buttons in the Commands screen with an ordered multi-select pipeline: check multiple commands in sequence (e.g., **`1단계: 구어·추임새·중복 정리` → `2단계: 격식체 변환`** or **`1단계: 구어·추임새·중복 정리` → `2단계: 텍스트 요약` → `3단계: 번역`**) and reorder steps anytime with `↑` / `↓` controls. + - Synthesizes selected pipeline steps into a single ordered pipeline prompt (`composeInstructionPipelinePrompt`) where later steps determine the final output tone/format/language without adding multi-roundtrip LLM latency, and supports testing the full multi-step pipeline directly in the Commands test runner. + ## [1.14.0] - 2026-10-29 > Published from an annotated tag. Installer and update metadata are served by the diff --git a/CLAUDE.md b/CLAUDE.md index d969b96..29b0336 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -1,79 +1,84 @@ -# D3RO-VOICE — 로컬 AI 음성 어시스턴트 +# D3RO-VOICE — 멀티플랫폼 AI 음성 어시스턴트 모노레포 -Speakly RE 기반 완전 로컬 음성 어시스턴트. Ollama + Whisper + TTS. +로컬 우선(Desktop) + 클라우드 동기화(Web · Mobile · Supabase · .NET API · Admin)를 아우르는 멀티플랫폼 AI 음성 어시스턴트. -> **에이전트 진입점**: 작업 전 `AGENTS.md`를 읽고, 기능/인프라 현황은 `docs/map/`를 -> 참조한다. 기능 추가·삭제·변경·백로그 이관 시 `docs/map/10-feature-catalog.md`와 -> `docs/map/11-gap-backlog.md`를 반드시 함께 갱신한다 (규칙: `docs/map/12-update-protocol.md`). +> **에이전트 진입점**: 작업 전 [`AGENTS.md`](./AGENTS.md)와 정본 인프라/기능 지도인 [`docs/map/00-index.md`](./docs/map/00-index.md)를 먼저 읽는다. +> 기능 추가·삭제·변경·백로그 이관 시 `docs/map/10-feature-catalog.md`, 해당 플랫폼 표면 문서(`04`~`09`), `docs/map/11-gap-backlog.md`를 반드시 함께 갱신한다 (규칙: `docs/map/12-update-protocol.md`). -## 기술 스택 -Electron 33+ | React 19 + MUI 7 + Vite | TypeScript 5.7+ strict -better-sqlite3 + drizzle-orm | faster-whisper (Python sidecar) | Ollama REST -uiohook-napi | @nut-tree-fork/nut-js | electron-store | electron-log +## 모노레포 표면 구성 +- `apps/desktop` — Electron 33+ 데스크톱 앱 (로컬 우선: SoX + faster-whisper Python sidecar + Ollama + SQLite/Drizzle + Cloud Sync) +- `apps/web` — Next.js 16 클라우드 웹 콘솔 (`/app` basePath, React 19 + MUI 7 + Supabase SSR/Realtime + Web Push) +- `apps/mobile-rn` — React Native 0.79 모바일 앱 (클라우드 우선, Android/iOS, 오프라인 내구성 큐, Google Play IAP, FCM/APNs) +- `apps/api-server` — ASP.NET Core 10 AI 프록시 및 백오피스 백엔드 +- `apps/admin` — Next.js 16 관리자 백오피스 콘솔 (RBAC, 감사 로그, 콘텐츠 신고/지원 대응) +- `server/supabase` — PostgreSQL + RLS + Auth + Storage + Deno Edge Functions (정본 제품 백엔드) +- `server/cloudflare-worker` — 엣지 게이트웨이 및 라우팅 워커 +- `site/` — 랜딩 · 마케팅 · 법무 · 다운로드 · 초대 핸드오프 정적 사이트 +- `packages/*` — 공유 패키지 (`@d3ro/core`, `@d3ro/ui`, `@d3ro/ui-native`, `@d3ro/i18n`, `@d3ro/api-client`) -## 빌드 +## 빌드 및 검증 ```bash -npm run dev # Electron + Vite dev -npm run build # 프로덕션 빌드 -npm run test # vitest -npm run typecheck # tsc --noEmit +npm run dev # Desktop (Electron + Vite) dev +npm run build # Desktop 프로덕션 빌드 +npm run typecheck # 워크스페이스 전체 tsc --noEmit +npm run test # 워크스페이스 vitest +npm run lint # 워크스페이스 eslint +npm run typecheck:mobile # Mobile RN tsc --noEmit +npm run test:mobile # Mobile RN Jest +npm run lint:mobile # Mobile RN eslint +npm run check:design # 디자인 토큰 SSOT 검사 ``` -## 코딩 규칙 -- strict mode, any 금지, console.log 금지 (logger 사용) -- 서비스: 싱글톤 + EventEmitter -- IPC: `${feature}:${action}`, D3ROError + ErrorCode -- 메인앱=React+MUI, 팝업=Vanilla JS -- 테마 SSOT: d3roPalette/d3roShadow만 사용, hex 하드코딩 금지 (theme.ts 제외) -- i18n: t() 함수, 하드코딩 한국어/영어 금지 -- Co-Authored-By, Claude 관련 커밋 문구 금지 +## 코딩 및 보안 규칙 (Non-Negotiable) +- strict mode, `any` 금지, `console.log` 금지 (`logger` 사용) +- 서비스: 싱글톤 + `EventEmitter` +- IPC: `${feature}:${action}` (`packages/core/src/ipc-channels.ts` 정본), `D3ROError` + `ErrorCode` + `IPCResult` +- 메인앱=React+MUI, 데스크톱 팝업=Vanilla JS +- 테마 SSOT: `d3roPalette`/`d3roShadow`/`d3roTypo`/`d3roRadius` 및 CSS 변수만 사용, hex 하드코딩 금지 (`theme.ts` 제외) +- i18n: `t()` 함수 사용, UI 내 한국어/영어 문자열 하드코딩 금지 +- 시크릿 하드코딩 절대 금지, Fail-Closed 원칙 준수 (가짜 성공 응답·샘플 데이터 대체 금지) +- `Co-Authored-By`, Claude 관련 커밋 문구 금지 - **Windows 데스크톱 실행**: AI 에이전트 서브쉘(가상 데스크톱 `exebox`)에서 GUI 백그라운드 구동을 반복하지 말고, 사용자가 `run-desktop.bat` 또는 외부 터미널에서 실행하도록 안내. - **Single Instance Lock**: `app.setName('d3ro-voice')`와 `app.setAppUserModelId`를 최상단 선언하고 dev 모드 사일런트 종료 방지. - **오디오 장치 탐색**: `execSync` 동기 블로킹 금지 (반드시 `exec` 비동기 논블로킹). - **GPU 충돌 방지**: `app.disableHardwareAcceleration()` 및 `--disable-gpu` 유지. -## 핵심 패턴 (Speakly 차용) +## 핵심 패턴 (Desktop) - RecognitionState(9) + AudioState(4) 이중 상태머신 - 이중 조건 플러시 (모델 로딩 + 오디오 버퍼링 병렬) - 텍스트 삽입: clipboard save→set→Ctrl+V→restore - 윈도우: 프리로딩 + 2-phase 리사이즈 - 녹음 UI: 9개 웨이브 바, cos 분포, 100ms -## 현재 상태 -Phase 1~15.5 전체 완료 + 랜딩 페이지(site/). -차단 이슈: @nut-tree-fork/nut-js 포크 사용 - ## 업데이트 / 릴리스 -자동 업데이트는 electron-updater + Forgejo feed +자동 업데이트는 `electron-updater` + Forgejo feed (`git.chanpaca.net/api/packages/yunchan/generic/d3ro-voice/latest`). 설치본은 무서명이라 -latest.yml 을 Ed25519 로 서명하고 앱이 받은 설치 파일을 검증한다(`update-signature.ts`). -**릴리스 = `npm run release -- patch|minor` 한 줄** (main, 깨끗한 트리, .env 의 FORGEJO_TOKEN · -D3RO_UPDATE_SIGNING_KEY). 실패하면 `npm run release -- --resume`. 채널·최소 지원 버전· +`latest.yml`을 Ed25519로 서명하고 앱이 받은 설치 파일을 검증한다(`update-signature.ts`). +**릴리스 = `npm run release -- patch|minor` 한 줄** (`main`, 깨끗한 트리, `.env`의 `FORGEJO_TOKEN` · +`D3RO_UPDATE_SIGNING_KEY`). 실패하면 `npm run release -- --resume`. 채널·최소 지원 버전· 강제 업데이트·rollout·킬 스위치는 `release/update-policy.json`이 정본. 태그는 불변. 규칙은 `AGENTS.md` §6. -## 설계 문서 (구현 시 Read 도구로 참조) -- docs/design/00-master-architecture.md — 시스템 아키텍처, 초기화/종료, 서비스 목록 -- docs/design/01-service-specifications.md — 서비스 인터페이스, 상태머신, 이벤트 -- docs/design/02-ipc-and-types.md — IPC 채널 전체, 타입, 에러코드, preload API -- docs/design/03-db-and-ui.md — DB 스키마, React 컴포넌트, 팝업 명세, MUI 테마 +## 정본 문서 안내 (구현 전 Read 도구로 참조) +- [`docs/map/00-index.md`](./docs/map/00-index.md) — **정본 기능·인프라 지도 색인** (`01`~`12`) +- [`docs/map/10-feature-catalog.md`](./docs/map/10-feature-catalog.md) — 플랫폼별 기능 구현 상태 매트릭스 +- [`docs/map/11-gap-backlog.md`](./docs/map/11-gap-backlog.md) — 미해결 갭·백로그·외부 블로커 +- [`docs/v3/MOBILE_APP_COMPLETION_SSOT.md`](./docs/v3/MOBILE_APP_COMPLETION_SSOT.md) — 모바일 완성도 정본 +- `docs/design/00-master-architecture.md` ~ `03-db-and-ui.md` — 데스크톱 초기 아키텍처 설계 기록 -## 서비스 목록 (src/main/services/) -Core: LoggerService, ConfigService, VoiceModeService(오케스트레이터) +## 데스크톱 서비스 목록 (`apps/desktop/src/main/services/`) +Core: LoggerService, ConfigService, VoiceModeService(오케스트레이터), UnifiedSearchService Audio: AudioCaptureService, LocalSTTService, LocalLLMService, TTSPlaybackService Input: HotkeyService, TextInsertService, SoundEffectService, AutoLaunchService Data: HistoryService, DictionaryService, CustomInstructionService, LicenseService Phase10+: MemoService, VoiceCommandService, ScreenContextService, ChainService, CaptionService Phase12+: FileTranscriptionService, MeetingSummaryService, DictationTemplateService Phase13+: VoiceConversationService, RAGService, VoiceActionService -Phase14: MeetingModeService -Phase14.5: MeetingDocTemplateService + 다중 문서 생성/편집/내보내기 -Phase15: Auto Polish + AI 채팅 + 마인드맵 + 공유 -Phase15.5: 화자 구분 (LLM 추정 + pyannote 준비) +Phase14+: MeetingModeService, MeetingDocTemplateService + 다중 문서 생성/편집/내보내기 + 화자 구분 -## DS 컴포넌트 (src/renderer/components/ds/) -CrtDisplay, InstrumentPanel, Led, PhysicalButton, MetalCard, -PhosphorText(13변형), MetalDial, ScreenPanel, ButtonGroup +## DS 컴포넌트 (`packages/ui` & `packages/ui-native`) +CrtDisplay, InstrumentPanel, Led, PhysicalButton, MetalCard, DoubleBezelCard, +PhosphorText(13변형), MetalDial, ScreenPanel, ButtonGroup, TactileBadge, StatRing, TiltCard -## 디자인 토큰 (theme.ts) -d3roPalette(색상) | d3roTypo(13단계) | d3roShadow(10종) | d3roRadius(7종) +## 디자인 토큰 (`theme.ts`) +`d3roPalette`(색상) | `d3roTypo`(13단계) | `d3roShadow`(10종) | `d3roRadius`(7종) diff --git a/apps/admin/src/app/(admin)/support/page.tsx b/apps/admin/src/app/(admin)/support/page.tsx index 76ce8e4..aff8698 100644 --- a/apps/admin/src/app/(admin)/support/page.tsx +++ b/apps/admin/src/app/(admin)/support/page.tsx @@ -1,11 +1,334 @@ -import { UnavailableAdminPanel } from '@/components/unavailable-admin-panel' +// apps/admin/src/app/(admin)/support/page.tsx +// D3RO Voice — Support & Content Reports Moderation Desk (Fail-Closed) + +import Link from 'next/link' +import { Alert, Box, Button, Typography } from '@mui/material' +import { DoubleBezelCard, TactileBadge } from '@d3ro/ui/components/ds' +import { C, FONT_MONO, FONT_SANS, filterBtnSx, innerCoreSx, panelSx, statusBadgeSx, tableSx } from '@/lib/console-theme' +import { UnavailableAdminPanel } from '@/components/unavailable-admin-panel' +import { requireManager } from '@/lib/admin-guard' +import { getSupabaseAdminClient, isSupabaseAdminConfigured } from '@/lib/supabase-admin' + +const VALID_STATUS_FILTERS = ['all', 'pending', 'reviewing', 'actioned', 'dismissed'] as const +type ReportStatusFilter = (typeof VALID_STATUS_FILTERS)[number] +type ContentReportStatus = Exclude + +interface ContentReportRow { + id: string + reporter_id: string + kind: string + source_type: string + source_id: string | null + reason: string + reporter_comment: string | null + status: ContentReportStatus + resolution_code: string | null + reviewed_by: string | null + reviewed_at: string | null + evidence_expires_at: string + created_at: string +} + +interface PageProps { + searchParams: Promise<{ status?: string; page?: string }> +} + +function isValidStatusFilter(value: string): value is ReportStatusFilter { + return (VALID_STATUS_FILTERS as readonly string[]).includes(value) +} + +function statusBadgeVariant(status: ContentReportStatus): 'orange' | 'blue' | 'green' | 'purple' { + switch (status) { + case 'pending': + return 'orange' + case 'reviewing': + return 'blue' + case 'actioned': + return 'green' + case 'dismissed': + return 'purple' + } +} + +export default async function AdminSupportPage({ searchParams }: PageProps): Promise { + await requireManager() + + if (!isSupabaseAdminConfigured()) { + return ( + + ) + } + + const params = await searchParams + const rawStatus = params.status ?? 'all' + const statusFilter: ReportStatusFilter = isValidStatusFilter(rawStatus) ? rawStatus : 'all' + const invalidFilterRequested = !isValidStatusFilter(rawStatus) + + const pageValue = params.page ?? '1' + const page = /^[1-9]\d{0,4}$/.test(pageValue) ? Number(pageValue) : 1 + const limit = 20 + const from = (page - 1) * limit + const to = from + limit - 1 + + let reports: ContentReportRow[] = [] + let totalCount = 0 + let pendingCount = 0 + let reviewingCount = 0 + let resolvedCount = 0 + let backendError: string | null = invalidFilterRequested + ? `지원하지 않는 상태 필터(${rawStatus})가 요청되어 전체 목록으로 전환했습니다.` + : null + + try { + const supabase = await getSupabaseAdminClient('manager') + + let listQuery = supabase + .from('content_reports') + .select( + 'id, reporter_id, kind, source_type, source_id, reason, reporter_comment, status, resolution_code, reviewed_by, reviewed_at, evidence_expires_at, created_at', + { count: 'exact' }, + ) + + if (statusFilter !== 'all') { + listQuery = listQuery.eq('status', statusFilter) + } + + const [listResult, pendingResult, reviewingResult, actionedResult, dismissedResult] = await Promise.all([ + listQuery.order('created_at', { ascending: false }).range(from, to), + supabase.from('content_reports').select('id', { count: 'exact', head: true }).eq('status', 'pending'), + supabase.from('content_reports').select('id', { count: 'exact', head: true }).eq('status', 'reviewing'), + supabase.from('content_reports').select('id', { count: 'exact', head: true }).eq('status', 'actioned'), + supabase.from('content_reports').select('id', { count: 'exact', head: true }).eq('status', 'dismissed'), + ]) + + if (listResult.error) { + throw new Error(listResult.error.message) + } + if (pendingResult.error) { + throw new Error(pendingResult.error.message) + } + + reports = (listResult.data ?? []) as ContentReportRow[] + totalCount = listResult.count ?? reports.length + pendingCount = pendingResult.count ?? 0 + reviewingCount = reviewingResult.count ?? 0 + resolvedCount = (actionedResult.count ?? 0) + (dismissedResult.count ?? 0) + } catch (error) { + backendError = error instanceof Error + ? `지원 및 콘텐츠 신고 백엔드 조회에 실패했습니다: ${error.message}` + : '지원 및 콘텐츠 신고 백엔드 조회에 실패했습니다.' + } -export default function AdminSupportPage(): React.ReactElement { return ( - + + {/* Header & Status Filter Bar */} + + + + + + + Support & Content Reports + + + {backendError ? 'DEGRADED / FAIL-CLOSED' : 'RLS MODERATION LEDGER'} + + + + AI output reports, user support escalations, and 90-day retention diagnostics + + + + + + {VALID_STATUS_FILTERS.map((candidate) => ( + + + {candidate.toUpperCase()} + + + ))} + + + + {/* Explicit Fail-Closed Error Banner */} + {backendError && ( + + + 백엔드 진단/신고 엔드포인트 오류 (Fail-Closed) + + {backendError} + + )} + + {/* Diagnostic Summary Metrics */} + + {[ + { label: 'MATCHED REPORTS', value: totalCount, color: C.bright }, + { label: 'PENDING TRIAGE', value: pendingCount, color: C.orange400 }, + { label: 'IN REVIEW', value: reviewingCount, color: C.accentLight }, + { label: 'ACTIONED / DISMISSED', value: resolvedCount, color: C.green400 }, + ].map((metric) => ( + + + {metric.label} + + + {backendError ? '—' : metric.value.toLocaleString()} + + + ))} + + + {/* Reports & Support Diagnostics Table */} + + + + Content Reports & Diagnostic Escalations + + + FILTER: {statusFilter.toUpperCase()} · PAGE {page} + + + + + + + + CREATED AT + STATUS + SOURCE / KIND + REASON + COMMENT & RESOLUTION + REPORTER + RETENTION EXPIRY + + + + {reports.length === 0 ? ( + + + {backendError + ? '백엔드 연결 오류로 인해 신고 목록을 표시할 수 없습니다.' + : '선택한 상태 필터에 해당하는 콘텐츠 신고 및 지원 항목이 없습니다.'} + + + ) : ( + reports.map((report) => ( + + + {new Date(report.created_at).toLocaleString()} + + + + {report.status.toUpperCase()} + + + + {report.source_type} · {report.kind} + {report.source_id ? ` (${report.source_id.slice(0, 8)})` : ''} + + + {report.reason} + + + {report.reporter_comment + || (report.resolution_code ? `Resolution: ${report.resolution_code}` : '—')} + + + {report.reporter_id.slice(0, 8)}… + + + {new Date(report.evidence_expires_at).toLocaleDateString()} + + + )) + )} + + + + + {/* Pagination Controls */} + {(page > 1 || totalCount > page * limit) && ( + + {page > 1 && ( + + + + )} + {totalCount > page * limit && ( + + + + )} + + )} + + ) } diff --git a/apps/desktop/package.json b/apps/desktop/package.json index 747f5cc..d65e639 100644 --- a/apps/desktop/package.json +++ b/apps/desktop/package.json @@ -8,8 +8,10 @@ "dev": "electron-vite dev", "build": "electron-vite build", "preview": "electron-vite preview", - "typecheck": "tsc --noEmit", + "typecheck": "tsc --noEmit -p tsconfig.node.json && tsc --noEmit -p tsconfig.web.json", "typecheck:strict": "tsc --noEmit -p tsconfig.check.json && tsc --noEmit -p tsconfig.check-node.json", + "rebuild:app": "node ../../scripts/ci/fix-native-abi.mjs --in-place", + "rebuild:test": "npm rebuild better-sqlite3", "lint": "eslint . --ext .ts,.tsx", "format": "prettier --write \"src/**/*.{ts,tsx,css}\"", "test": "vitest run --config vitest.config.ts", diff --git a/apps/desktop/sidecar/cpu_budget.py b/apps/desktop/sidecar/cpu_budget.py new file mode 100644 index 0000000..240b0b3 --- /dev/null +++ b/apps/desktop/sidecar/cpu_budget.py @@ -0,0 +1,19 @@ +"""Bound local inference workers so foreground input has scheduling headroom. + +Keep the policy aligned with src/main/utils/inference-cpu-budget.ts. +This limits inference workers, not process affinity or total process CPU usage. +""" + +import os + + +def inference_cpu_threads(available_cpus: int | None) -> int: + if available_cpus is None or available_cpus < 1: + return 1 + return max(1, min(4, available_cpus - 2)) + + +def local_inference_cpu_threads() -> int: + # Python 3.13+ exposes the CPUs available to this process, including affinity. + count = getattr(os, "process_cpu_count", os.cpu_count)() + return inference_cpu_threads(count) diff --git a/apps/desktop/sidecar/main.py b/apps/desktop/sidecar/main.py index 8c0489d..e9d42ed 100644 --- a/apps/desktop/sidecar/main.py +++ b/apps/desktop/sidecar/main.py @@ -39,6 +39,7 @@ import numpy as np import uvicorn from fastapi import FastAPI, File, Form, Request, UploadFile from fastapi.responses import JSONResponse +from cpu_budget import local_inference_cpu_threads from device_policy import ( CPU_CHOICE, @@ -185,8 +186,8 @@ def _disable_gpu(reason: BaseException) -> None: def _cpu_threads() -> int: - """CPU 추론에 사용할 스레드 수 (과도한 점유 방지 위해 8로 상한).""" - return max(1, min(8, os.cpu_count() or 4)) + """입력 앱과 IME 에 여유를 남기는 CPU 추론 스레드 상한.""" + return local_inference_cpu_threads() # ── 모델 다운로드 헬퍼 ───────────────────────────────────── diff --git a/apps/desktop/sidecar/tests/test_cpu_budget.py b/apps/desktop/sidecar/tests/test_cpu_budget.py new file mode 100644 index 0000000..45e7cdf --- /dev/null +++ b/apps/desktop/sidecar/tests/test_cpu_budget.py @@ -0,0 +1,30 @@ +"""Scheduling headroom, including small PCs and restricted process CPU capacity.""" + +import sys +import unittest +from pathlib import Path +from unittest import mock + +sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) +import cpu_budget + + +class CpuBudgetTest(unittest.TestCase): + def test_reserves_headroom_and_caps_workers(self) -> None: + for cpus, expected in [(1, 1), (2, 1), (3, 1), (4, 2), (6, 4), (8, 4), (16, 4), (64, 4)]: + with self.subTest(cpus=cpus): + self.assertEqual(cpu_budget.inference_cpu_threads(cpus), expected) + + def test_unknown_capacity_uses_one_worker(self) -> None: + for cpus in (None, 0, -1): + with self.subTest(cpus=cpus): + self.assertEqual(cpu_budget.inference_cpu_threads(cpus), 1) + + def test_prefers_process_capacity_when_available(self) -> None: + with mock.patch.object(cpu_budget.os, "process_cpu_count", return_value=4, create=True): + with mock.patch.object(cpu_budget.os, "cpu_count", return_value=64): + self.assertEqual(cpu_budget.local_inference_cpu_threads(), 2) + + +if __name__ == "__main__": + unittest.main() diff --git a/apps/desktop/sidecar/tests/test_device_fallback.py b/apps/desktop/sidecar/tests/test_device_fallback.py index 342c3d6..9bf7b7b 100644 --- a/apps/desktop/sidecar/tests/test_device_fallback.py +++ b/apps/desktop/sidecar/tests/test_device_fallback.py @@ -168,6 +168,7 @@ class FakeWhisperModel: self.source = source self.device = device self.compute_type = compute_type + self.cpu_threads = cpu_threads FakeWhisperModel.created.append(self) def transcribe(self, audio: np.ndarray, **_kwargs: object) -> tuple[Iterator[_Segment], _Info]: @@ -232,6 +233,16 @@ class SidecarGpuFallbackTest(unittest.TestCase): self.assertEqual(self._transcribe().status_code, 200) self.assertTrue(self.client.get("/health").json()["gpu"]) + def test_primary_aux_and_cpu_fallback_use_worker_budget(self) -> None: + _FakeGpuEnv.cublas_missing = True + with mock.patch("cpu_budget.os.process_cpu_count", return_value=4, create=True): + primary = self.client.post("/load", json={"model_id": "medium"}) + auxiliary = self.client.post("/load", json={"model_id": "small", "slot": "aux"}) + self.assertEqual(primary.status_code, 200) + self.assertEqual(auxiliary.status_code, 200) + self.assertEqual([m.device for m in FakeWhisperModel.created], ["cuda", "cpu", "cpu"]) + self.assertEqual([m.cpu_threads for m in FakeWhisperModel.created], [2, 2, 2]) + def test_missing_cublas_detected_at_load_uses_cpu(self) -> None: _FakeGpuEnv.cublas_missing = True res = self.client.post("/load", json={"model_id": "medium"}) diff --git a/apps/desktop/sidecar/uia_bridge.py b/apps/desktop/sidecar/uia_bridge.py index 3b808cc..4a1f496 100644 --- a/apps/desktop/sidecar/uia_bridge.py +++ b/apps/desktop/sidecar/uia_bridge.py @@ -250,7 +250,11 @@ def _caret_rect_and_offset(auto: Any, control: Any) -> tuple[Optional[dict], Opt -def _wake_chromium_accessibility() -> bool: +_woken_hwnds: dict[int, float] = {} +_WAKE_TTL_SECONDS = 60.0 + + +def _wake_chromium_accessibility(force: bool = False) -> bool: """Chromium 계열에 "보조기술이 있다" 고 알려 접근성 트리를 열게 한다. Chromium 문서: 브라우저가 EVENT_SYSTEM_ALERT + custom object id 1 을 알리고, @@ -259,6 +263,7 @@ def _wake_chromium_accessibility() -> bool: LegacyIAccessible 만 노출돼 텍스트를 읽을 수 없다(실측: edit=false src=legacy len=0). SendMessage 대신 SendMessageTimeout 을 쓰는 이유: 대상이 바쁘면 무한 대기한다. + 이미 깨운 HWND 는 _WAKE_TTL_SECONDS 동안 재전송하지 않아 타이핑 중 대상 창 UI 스레드 지연을 막는다. """ try: import ctypes # noqa: PLC0415 @@ -268,10 +273,19 @@ def _wake_chromium_accessibility() -> bool: WM_GETOBJECT = 0x003D OBJID_CLIENT = 0xFFFFFFFC - hwnd = user32.GetForegroundWindow() + hwnd = int(user32.GetForegroundWindow() or 0) if not hwnd: return False + now = time.monotonic() + last_woken = _woken_hwnds.get(hwnd) + if not force and last_woken is not None and (now - last_woken) < _WAKE_TTL_SECONDS: + return True + + if len(_woken_hwnds) > 128: + _woken_hwnds.clear() + _woken_hwnds[hwnd] = now + result = ctypes.c_void_p() # 1 = 보조기술 감지용 custom object id user32.SendMessageTimeoutW( @@ -280,7 +294,7 @@ def _wake_chromium_accessibility() -> bool: wintypes.WPARAM(0), wintypes.LPARAM(1), 0x0002, # SMTO_ABORTIFHUNG - 250, + 120, ctypes.byref(result), ) # 클라이언트 영역 객체도 함께 요청해 AX 트리를 확실히 깨운다. @@ -290,7 +304,7 @@ def _wake_chromium_accessibility() -> bool: wintypes.WPARAM(0), wintypes.LPARAM(OBJID_CLIENT & 0xFFFFFFFF), 0x0002, - 250, + 120, ctypes.byref(result), ) return True diff --git a/apps/desktop/src/main/bootstrap.ts b/apps/desktop/src/main/bootstrap.ts index 4a1f0a4..0a4aa2a 100644 --- a/apps/desktop/src/main/bootstrap.ts +++ b/apps/desktop/src/main/bootstrap.ts @@ -23,6 +23,7 @@ import { getVoiceCommandService } from './services/VoiceCommandService' import { getInputTelemetryService } from './services/InputTelemetryService' import { getSuggestionService } from './services/SuggestionService' import { initTypoCorrectionService } from './services/TypoCorrectionService' +import { getLastNativeInputTick } from './utils/win32-input-activity' import { getPersonalGraphService } from './services/PersonalGraphService' import { getForegroundWindowInfo } from './utils/win32-foreground' import { getSoundEffectService } from './services/SoundEffectService' @@ -220,17 +221,25 @@ async function initInputIntelligence(): Promise { // Windows 에서는 KEYEVENTF_UNICODE 직접 타이핑('keyboard')을 기본으로 써서 // 사용자의 클립보드(Win+V)를 오염시키거나 ClipboardSvcGroup 의 OpenClipboard 잠금과 충돌하지 않게 한다. const method = process.platform === 'win32' || configGet('insertMethod') === 'keyboard' ? 'keyboard' : 'clipboard' - const baseLength = telemetry.currentTerminalPrefixLength() - if (method === 'keyboard') { - telemetry.expectProgrammaticKeys(deleteCount + (options?.commitComposition ? 2 : 0)) - } - const result = await getTextInsertService().replaceBeforeCaret(deleteCount, text, method, options) + let baseLength = telemetry.currentTerminalPrefixLength() + const result = await getTextInsertService().replaceBeforeCaret(deleteCount, text, method, { + ...options, + beforeApply: () => { + options?.beforeApply?.() + baseLength = telemetry.currentTerminalPrefixLength() + if (method === 'keyboard') { + telemetry.expectProgrammaticKeys(deleteCount + (options?.commitComposition ? 2 : 0)) + } + } + }) if (result.success) { telemetry.applyProgrammaticReplacement(deleteCount, text, baseLength) } return result.success }, lastKeyAt: () => telemetry.lastKeyAt, + inputRevision: () => telemetry.inputRevision, + nativeInputTick: getLastNativeInputTick, expectProgrammaticKeys: (count) => telemetry.expectProgrammaticKeys(count), expectProgrammaticInsert: (text) => telemetry.expectProgrammaticInsert(text), foregroundHandle: () => getForegroundWindowInfo()?.hwnd ?? null, diff --git a/apps/desktop/src/main/ipc/ads-handlers.ts b/apps/desktop/src/main/ipc/ads-handlers.ts index 1b4d55d..9fbc57e 100644 --- a/apps/desktop/src/main/ipc/ads-handlers.ts +++ b/apps/desktop/src/main/ipc/ads-handlers.ts @@ -50,10 +50,13 @@ export function registerAdsHandlers(): void { }) // 6. Claim Rewarded Video Quota - ipcMain.handle(IPC_CHANNELS.ADS.CLAIM_REWARD, async (_event, { adId, networkId }: { adId: string; networkId: string }) => { - const result = await engine.claimReward(adId, networkId) - return ok(result) - }) + ipcMain.handle( + IPC_CHANNELS.ADS.CLAIM_REWARD, + async (_event, { adId, networkId, nonce }: { adId: string; networkId: string; nonce?: string }) => { + const result = await engine.claimReward(adId, networkId, nonce) + return ok(result) + }, + ) // 7. Get Revenue & Settlement Stats ipcMain.handle(IPC_CHANNELS.ADS.GET_REVENUE_STATS, async (_event, { period }: { period?: string } = {}) => { diff --git a/apps/desktop/src/main/ipc/audio-handlers.ts b/apps/desktop/src/main/ipc/audio-handlers.ts index 2d1c080..f3ddb48 100644 --- a/apps/desktop/src/main/ipc/audio-handlers.ts +++ b/apps/desktop/src/main/ipc/audio-handlers.ts @@ -6,7 +6,7 @@ import { ipcSuccess, ipcError, ErrorCode } from '@d3ro/core/errors' import { getAudioCaptureService, calculateRMS, type CaptureLease } from '../services/AudioCaptureService' import { getMainWindow } from '../windows/WindowManager' import { configGet, configSet } from '../services/ConfigService' -import type { SetDeviceParams } from '@d3ro/core/types' +import type { SetDeviceParams, TestDeviceParams } from '@d3ro/core/types' let testTimer: ReturnType | null = null let testAudioHandler: ((payload: { buffer: Buffer; timestamp: number }) => void) | null = null @@ -23,7 +23,7 @@ let testLease: CaptureLease | null = null */ let testGeneration = 0 -function stopAudioTest(): void { +async function stopAudioTest(): Promise { testGeneration += 1 const service = getAudioCaptureService() if (testAudioHandler) { @@ -41,7 +41,7 @@ function stopAudioTest(): void { testLastRms = 0 const lease = testLease testLease = null - lease?.release().catch(() => { /* ignore */ }) + await lease?.release().catch(() => { /* ignore */ }) } export function registerAudioHandlers(): void { @@ -69,14 +69,23 @@ export function registerAudioHandlers(): void { } }) - ipcMain.handle(IPC_CHANNELS.AUDIO.TEST_DEVICE, async () => { + ipcMain.handle(IPC_CHANNELS.AUDIO.TEST_DEVICE, async (_event, params?: TestDeviceParams) => { try { // 이전 테스트가 진행 중이면 정리 - stopAudioTest() + await stopAudioTest() const generation = testGeneration + const requestedDeviceId = + typeof params?.deviceId === 'string' && params.deviceId.trim().length > 0 + ? params.deviceId.trim() + : undefined + const durationMs = + typeof params?.durationMs === 'number' && Number.isFinite(params.durationMs) && params.durationMs > 0 + ? params.durationMs + : 5000 + const service = getAudioCaptureService() - const lease = await service.acquire() + const lease = await service.acquire(requestedDeviceId) if (generation !== testGeneration) { // 기다리는 사이 새 테스트가 시작됐거나 중지됐다 — 이 임대는 바로 놓는다 await lease.release().catch(() => { /* ignore */ }) @@ -100,18 +109,18 @@ export function registerAudioHandlers(): void { } }, 100) - // 5초 후 자동 중지 + // durationMs(기본 5초) 후 자동 중지 testTimer = setTimeout(() => { - stopAudioTest() + void stopAudioTest() const win = getMainWindow() if (win && !win.isDestroyed()) { win.webContents.send(IPC_CHANNELS.AUDIO.TEST_LEVEL, { level: 0, done: true }) } - }, 5000) + }, durationMs) return ipcSuccess({ averageLevel: 0, peakLevel: 0, hasAudio: true }) } catch (err) { - stopAudioTest() + await stopAudioTest() return ipcError( ErrorCode.AudioDeviceNotFound, `Audio test failed: ${err instanceof Error ? err.message : String(err)}`, @@ -121,7 +130,7 @@ export function registerAudioHandlers(): void { // 테스트 수동 중지 (STOP 버튼) ipcMain.handle(IPC_CHANNELS.AUDIO.STOP_TEST, async () => { - stopAudioTest() + await stopAudioTest() const win = getMainWindow() if (win && !win.isDestroyed()) { win.webContents.send(IPC_CHANNELS.AUDIO.TEST_LEVEL, { level: 0, done: true }) diff --git a/apps/desktop/src/main/ipc/history-handlers.ts b/apps/desktop/src/main/ipc/history-handlers.ts index 4298b01..b04bc80 100644 --- a/apps/desktop/src/main/ipc/history-handlers.ts +++ b/apps/desktop/src/main/ipc/history-handlers.ts @@ -5,6 +5,7 @@ import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' import { ipcSuccess, ipcError, ErrorCode } from '@d3ro/core/errors' import fs from 'fs' import { getHistoryService } from '../services/HistoryService' +import type { HistoryFilterOptions, HistoryUpdatePatch } from '../services/HistoryService' import { getCloudSyncService } from '../services/CloudSyncService' import { audioMimeType } from '../services/sync/audio-sync' import type { @@ -20,14 +21,35 @@ import type { const MAX_LOCAL_AUDIO_BYTES = 100 * 1024 * 1024 export function registerHistoryHandlers(): void { - ipcMain.handle(IPC_CHANNELS.HISTORY.GET_ALL, async (_event, params: HistoryQueryParams) => { - try { - return ipcSuccess(getHistoryService().list(params)) - } catch (error) { - const message = error instanceof Error ? error.message : String(error) - return ipcError(ErrorCode.DBQueryFailed, `Failed to get history: ${message}`) - } - }) + ipcMain.handle( + IPC_CHANNELS.HISTORY.GET_ALL, + async (_event, params: HistoryQueryParams & HistoryFilterOptions) => { + try { + return ipcSuccess(getHistoryService().getAll(params)) + } catch (error) { + const message = error instanceof Error ? error.message : String(error) + return ipcError(ErrorCode.DBQueryFailed, `Failed to get history: ${message}`) + } + }, + ) + + const updateChannel = + (IPC_CHANNELS.HISTORY as Record).UPDATE ?? 'history:update' + ipcMain.handle( + updateChannel, + async (_event, params: { id: string } & HistoryUpdatePatch) => { + try { + const entry = getHistoryService().update(params) + if (!entry) { + return ipcError(ErrorCode.HistoryNotFound, `History entry not found: ${params.id}`) + } + return ipcSuccess(entry) + } catch (error) { + const message = error instanceof Error ? error.message : String(error) + return ipcError(ErrorCode.DBWriteFailed, `Failed to update history entry: ${message}`) + } + }, + ) ipcMain.handle(IPC_CHANNELS.HISTORY.GET_BY_ID, async (_event, params: HistoryGetByIdParams) => { try { diff --git a/apps/desktop/src/main/ipc/meeting-mode-handlers.ts b/apps/desktop/src/main/ipc/meeting-mode-handlers.ts index dcdceef..62c62f0 100644 --- a/apps/desktop/src/main/ipc/meeting-mode-handlers.ts +++ b/apps/desktop/src/main/ipc/meeting-mode-handlers.ts @@ -118,17 +118,17 @@ export function registerMeetingModeHandlers(): void { return ipcSuccess(undefined) } catch (err) { logger.error(`stopRecording 실패: ${err instanceof Error ? err.message : String(err)}`) - return ipcError(ErrorCode.MeetingNotRecording, err instanceof Error ? err.message : String(err)) + return ipcError(err instanceof D3ROError ? err.code : ErrorCode.MeetingNotRecording, err instanceof Error ? err.message : String(err)) } }) - ipcMain.handle(ch.ADD_MEMO, async (_event, params: MeetingAddMemoParams) => { + ipcMain.handle(ch.ADD_MEMO, async (_event, params: MeetingAddMemoParams & { sessionId?: string; meetingId?: string }) => { try { - const memo = await getMeetingModeService().addMemo(params.content) + const memo = await getMeetingModeService().addMemo(params.content, params.sessionId ?? params.meetingId) return ipcSuccess(memo) } catch (err) { logger.error(`addMemo 실패: ${err instanceof Error ? err.message : String(err)}`) - return ipcError(ErrorCode.MeetingNotRecording, err instanceof Error ? err.message : String(err)) + return ipcError(err instanceof D3ROError ? err.code : ErrorCode.MeetingNotRecording, err instanceof Error ? err.message : String(err)) } }) diff --git a/apps/desktop/src/main/ipc/memo-handlers.ts b/apps/desktop/src/main/ipc/memo-handlers.ts index b9f0116..2d722c3 100644 --- a/apps/desktop/src/main/ipc/memo-handlers.ts +++ b/apps/desktop/src/main/ipc/memo-handlers.ts @@ -1,10 +1,12 @@ // src/main/ipc/memo-handlers.ts // Phase 10.3: 음성 메모 태그 IPC 핸들러 -import { ipcMain } from 'electron' +import { ipcMain, dialog } from 'electron' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' import { ipcSuccess, ipcError, ErrorCode, D3ROError } from '@d3ro/core/errors' import { getMemoService } from '../services/MemoService' +import { buildMemoExportFileName } from '../services/memo/memo-export-file-name' +import { normalizeMemoTag } from '@d3ro/core/memo-tag-policy' import type { GetTagsParams, AddTagParams, @@ -64,14 +66,42 @@ export function registerMemoHandlers(): void { } }) - ipcMain.handle(IPC_CHANNELS.MEMO.EXPORT, async (_event, params: ExportMemoParams) => { - try { - return ipcSuccess(getMemoService().exportMarkdown(params)) - } catch (err) { - if (err instanceof D3ROError && err.code === ErrorCode.MemoExportFailed) { - return ipcError(ErrorCode.MemoExportFailed, err.message) + ipcMain.handle( + IPC_CHANNELS.MEMO.EXPORT, + async ( + _event, + params: ExportMemoParams & { + savePath?: string + interactive?: boolean + includeUntagged?: boolean + mode?: string + isFavorite?: boolean + search?: string + }, + ) => { + try { + let savePath = params?.savePath + if (params?.interactive && !savePath) { + const defaultFileName = buildMemoExportFileName( + params.tag ? normalizeMemoTag(params.tag) : undefined, + Date.now(), + ) + const dialogRes = await dialog.showSaveDialog({ + defaultPath: defaultFileName, + filters: [{ name: 'Markdown', extensions: ['md'] }], + }) + if (dialogRes.canceled || !dialogRes.filePath) { + return ipcSuccess('') + } + savePath = dialogRes.filePath + } + return ipcSuccess(getMemoService().exportMarkdown({ ...params, savePath })) + } catch (err) { + if (err instanceof D3ROError && err.code === ErrorCode.MemoExportFailed) { + return ipcError(ErrorCode.MemoExportFailed, err.message) + } + return ipcError(ErrorCode.MemoExportFailed, 'Failed to export memo') } - return ipcError(ErrorCode.MemoExportFailed, 'Failed to export memo') - } - }) + }, + ) } diff --git a/apps/desktop/src/main/ipc/rag-handlers.ts b/apps/desktop/src/main/ipc/rag-handlers.ts index 6543551..b95df98 100644 --- a/apps/desktop/src/main/ipc/rag-handlers.ts +++ b/apps/desktop/src/main/ipc/rag-handlers.ts @@ -3,7 +3,7 @@ import { ipcMain, dialog } from 'electron' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' -import { ErrorCode, ipcSuccess, ipcError } from '@d3ro/core/errors' +import { D3ROError, ErrorCode, ipcSuccess, ipcError } from '@d3ro/core/errors' import { getRAGService } from '../services/RAGService' import { getLogger } from '../services/LoggerService' import type { RAGQueryParams, RAGRemoveDocumentParams } from '@d3ro/core/types' @@ -18,7 +18,10 @@ export function registerRAGHandlers(): void { let filePath = params?.filePath if (!filePath) { if (isShowingRagDialog) { - return ipcError(ErrorCode.RAGDocumentNotFound, 'File selection dialog already active') + return ipcError(ErrorCode.RAGDocumentNotFound, 'File selection dialog already active', { + cancelled: true, + reason: 'dialog_active', + }) } isShowingRagDialog = true try { @@ -32,7 +35,10 @@ export function registerRAGHandlers(): void { ? await dialog.showOpenDialog(mainWindow, dialogOptions) : await dialog.showOpenDialog(dialogOptions) if (result.canceled || result.filePaths.length === 0) { - return ipcError(ErrorCode.RAGDocumentNotFound, 'File selection cancelled') + return ipcError(ErrorCode.RAGDocumentNotFound, 'File selection cancelled', { + cancelled: true, + reason: 'dialog_cancelled', + }) } filePath = result.filePaths[0] } finally { @@ -43,8 +49,10 @@ export function registerRAGHandlers(): void { return ipcSuccess(doc) } catch (err) { const msg = err instanceof Error ? err.message : String(err) + const code = err instanceof D3ROError ? err.code : ErrorCode.RAGIndexingFailed + const details = err instanceof D3ROError ? err.details : undefined logger.error('RAG add document failed:', msg) - return ipcError(ErrorCode.RAGIndexingFailed, msg) + return ipcError(code, msg, details) } }) @@ -54,7 +62,8 @@ export function registerRAGHandlers(): void { return ipcSuccess(undefined) } catch (err) { const msg = err instanceof Error ? err.message : String(err) - return ipcError(ErrorCode.RAGDocumentNotFound, msg) + const code = err instanceof D3ROError ? err.code : ErrorCode.RAGDocumentNotFound + return ipcError(code, msg) } }) @@ -73,8 +82,9 @@ export function registerRAGHandlers(): void { return ipcSuccess(result) } catch (err) { const msg = err instanceof Error ? err.message : String(err) + const code = err instanceof D3ROError ? err.code : ErrorCode.RAGQueryFailed logger.error('RAG query failed:', msg) - return ipcError(ErrorCode.RAGQueryFailed, msg) + return ipcError(code, msg) } }) @@ -93,7 +103,9 @@ export function registerRAGHandlers(): void { return ipcSuccess(undefined) } catch (err) { const msg = err instanceof Error ? err.message : String(err) - return ipcError(ErrorCode.RAGIndexingFailed, msg) + const code = err instanceof D3ROError ? err.code : ErrorCode.RAGIndexingFailed + const details = err instanceof D3ROError ? err.details : undefined + return ipcError(code, msg, details) } }) } diff --git a/apps/desktop/src/main/ipc/support-handlers.ts b/apps/desktop/src/main/ipc/support-handlers.ts index e763ab7..48f232c 100644 --- a/apps/desktop/src/main/ipc/support-handlers.ts +++ b/apps/desktop/src/main/ipc/support-handlers.ts @@ -22,8 +22,9 @@ export function registerSupportHandlers(): void { let sttModel = 'large-v3-turbo' try { const stt = getSTTManager() - sttEngine = stt.getProvider() - sttModel = stt.getModel() + const status = stt.getStatus() + sttEngine = status.activeProvider ?? stt.getActiveProvider() + sttModel = status.activeModel ?? sttModel } catch { // fallback } @@ -33,7 +34,7 @@ export function registerSupportHandlers(): void { appVersion: app.getVersion(), platform: `${os.platform()} (${os.arch()})`, osRelease: `${os.type()} ${os.release()}`, - activeAudioDevice: (configGet('audio.selectedDevice') as string) || 'Default System Microphone', + activeAudioDevice: configGet('selectedDeviceId') || 'Default System Microphone', sttEngine, sttModel, gpuAccelerated: true, @@ -45,56 +46,77 @@ export function registerSupportHandlers(): void { }) // 2. Query AI Customer Support - ipcMain.handle(IPC_CHANNELS.SUPPORT.QUERY_AI, async (_event, params: AIAssistQuery) => { - const q = params.query.toLowerCase() - let answer = '안녕하세요! D3RO Voice 고객지원 AI 어시스턴트입니다. 마이크 설정, Whisper 구동, 정기 결제 및 환불 관련하여 신속히 도와드리겠습니다.' - let suggestedAction: string | undefined + ipcMain.handle( + IPC_CHANNELS.SUPPORT.QUERY_AI, + async (_event, params: AIAssistQuery & { query?: string }) => { + const q = (params.question ?? params.query ?? '').toLowerCase() + let answer = + '안녕하세요! D3RO Voice 고객지원 AI 어시스턴트입니다. 마이크 설정, Whisper 구동, 정기 결제 및 환불 관련하여 신속히 도와드리겠습니다.' + let suggestedAction: string | undefined - if (q.includes('마이크') || q.includes('인식') || q.includes('소리')) { - answer = '마이크 음성 인식이 되지 않을 때는 1) Windows [설정 > 개인 정보 및 보안 > 마이크]에서 D3RO Voice 권한이 허용되어 있는지 확인해 주세요. 2) D3RO 설정에서 올바른 입력 장치가 선택되어 있는지 확인해 주세요.' - suggestedAction = 'open_audio_settings' - } else if (q.includes('환불') || q.includes('취소') || q.includes('결제')) { - answer = 'D3RO Voice는 구매 후 7일 이내 & 클라우드 AI 토큰 10% 미만 사용 시 100% 무조건 자동 환불을 보장합니다. [7일 자동 환불] 탭에서 원클릭으로 환불 자격을 조회하실 수 있습니다.' - suggestedAction = 'check_refund' - } else if (q.includes('cuda') || q.includes('gpu') || q.includes('속도')) { - answer = 'NVIDIA GPU CUDA 가속을 사용하려면 최신 NVIDIA 그래픽 드라이버(v535+)와 cuBLAS DLL이 설치되어 있어야 합니다. GPU 가속 시 Whisper 전사 속도가 5배 이상 향상됩니다.' - suggestedAction = 'open_stt_settings' - } + if (q.includes('마이크') || q.includes('인식') || q.includes('소리')) { + answer = + '마이크 음성 인식이 되지 않을 때는 1) Windows [설정 > 개인 정보 및 보안 > 마이크]에서 D3RO Voice 권한이 허용되어 있는지 확인해 주세요. 2) D3RO 설정에서 올바른 입력 장치가 선택되어 있는지 확인해 주세요.' + suggestedAction = 'open_audio_settings' + } else if (q.includes('환불') || q.includes('취소') || q.includes('결제')) { + answer = + 'D3RO Voice는 구매 후 7일 이내 & 클라우드 AI 토큰 10% 미만 사용 시 100% 무조건 자동 환불을 보장합니다. [7일 자동 환불] 탭에서 원클릭으로 환불 자격을 조회하실 수 있습니다.' + suggestedAction = 'check_refund' + } else if (q.includes('cuda') || q.includes('gpu') || q.includes('속도')) { + answer = + 'NVIDIA GPU CUDA 가속을 사용하려면 최신 NVIDIA 그래픽 드라이버(v535+)와 cuBLAS DLL이 설치되어 있어야 합니다. GPU 가속 시 Whisper 전사 속도가 5배 이상 향상됩니다.' + suggestedAction = 'open_stt_settings' + } - const response: AIAssistResponse = { - answer, - confidence: 0.95, - suggestedAction, - } - return ok(response) - }) + const response: AIAssistResponse = { + answer, + confidence: 0.95, + suggestedAction, + } + return ok(response) + }, + ) // 3. Create Ticket - ipcMain.handle(IPC_CHANNELS.SUPPORT.CREATE_TICKET, async (_event, ticketData: Partial) => { - const newTicket: SupportTicket = { - id: `tkt_${Date.now()}`, - userId: 'user_local', - userEmail: ticketData.userEmail || 'yunchanpaca@gmail.com', - category: ticketData.category || 'general', - priority: ticketData.priority || 'normal', - status: 'open', - subject: ticketData.subject || '고객 지원 문의', - description: ticketData.description || '', - diagnosticsSnapshot: ticketData.diagnosticsSnapshot, - createdAt: Date.now(), - updatedAt: Date.now(), - responses: [], - } - return ok(newTicket) - }) + ipcMain.handle( + IPC_CHANNELS.SUPPORT.CREATE_TICKET, + async ( + _event, + ticketData: Partial & { + userEmail?: string + diagnosticsSnapshot?: SystemDiagnosticsPayload + }, + ) => { + const now = Date.now() + const newTicket: SupportTicket = { + id: `tkt_${now}`, + userId: 'user_local', + customerEmail: + ticketData.customerEmail || ticketData.userEmail || 'yunchanpaca@gmail.com', + category: ticketData.category || 'general', + priority: ticketData.priority || 'normal', + status: 'open', + subject: ticketData.subject || '고객 지원 문의', + description: ticketData.description || '', + diagnostics: ticketData.diagnostics ?? ticketData.diagnosticsSnapshot, + slaDueAt: ticketData.slaDueAt ?? now + 24 * 60 * 60 * 1000, + createdAt: now, + updatedAt: now, + } + return ok(newTicket) + }, + ) // 4. Check Refund ipcMain.handle(IPC_CHANNELS.SUPPORT.CHECK_REFUND, async () => { const result: RefundEligibilityResult = { eligible: true, - reason: '결제일로부터 7일 이내이며 클라우드 AI 정제 쿼터를 10% 미만 사용하셨습니다. (100% 전액 환불 가능)', - refundMethod: 'Payple 카드 결제 즉시 취소', - estimatedRefundKrw: 229000, + reason: + '결제일로부터 7일 이내이며 클라우드 AI 정제 쿼터를 10% 미만 사용하셨습니다. (100% 전액 환불 가능)', + purchaseDate: new Date().toISOString().slice(0, 10), + tokensConsumedPercent: 0, + refundableAmount: 229000, + currency: 'KRW', } return ok(result) }) diff --git a/apps/desktop/src/main/ipc/voice-conversation-handlers.ts b/apps/desktop/src/main/ipc/voice-conversation-handlers.ts index 2f2fb6b..8d44394 100644 --- a/apps/desktop/src/main/ipc/voice-conversation-handlers.ts +++ b/apps/desktop/src/main/ipc/voice-conversation-handlers.ts @@ -140,4 +140,20 @@ export function registerVoiceConversationHandlers(credentials: CloudCredentials return ipcError(ErrorCode.ConversationLLMFailed, msg) } }) + + const replayChannel = + (IPC_CHANNELS.VOICE_CONVERSATION as Record).REPLAY_MESSAGE ?? + 'voiceConversation:replayMessage' + ipcMain.handle( + replayChannel, + async (_event, params?: { messageId?: string; text?: string } | string) => { + try { + await getVoiceConversationService().replayMessage(params) + return ipcSuccess(undefined) + } catch (err) { + const msg = err instanceof Error ? err.message : String(err) + return ipcError(ErrorCode.ConversationLLMFailed, msg) + } + }, + ) } diff --git a/apps/desktop/src/main/services/ConfigService.ts b/apps/desktop/src/main/services/ConfigService.ts index 2a4241f..3e11b44 100644 --- a/apps/desktop/src/main/services/ConfigService.ts +++ b/apps/desktop/src/main/services/ConfigService.ts @@ -172,6 +172,7 @@ const CONFIG_DEFAULTS: AppConfig = { // 라이브 음성 대화 백엔드 — 'realtime'은 로그인+구독 필요 (OpenAI Realtime WebRTC) conversationBackend: 'local' as const, defaultLLMAction: 'refine', + targetLanguage: 'English', keyBindings: createDefaultBindingMap(), hotkeyEnabled: true, insertMethod: 'clipboard', diff --git a/apps/desktop/src/main/services/DictionaryService.ts b/apps/desktop/src/main/services/DictionaryService.ts index 167c98c..6b9e64f 100644 --- a/apps/desktop/src/main/services/DictionaryService.ts +++ b/apps/desktop/src/main/services/DictionaryService.ts @@ -230,17 +230,103 @@ class DictionaryService { /** * 사전 단어 목록을 STT initialPrompt 형태로 반환한다. + * 발음(pronunciation)이 등록된 항목은 인식 힌트로 함께 포함한다. */ getPromptHints(limit = 50): string { const db = getDatabase() const words = db - .select({ word: dictionary.word }) + .select({ word: dictionary.word, pronunciation: dictionary.pronunciation }) .from(dictionary) .orderBy(desc(dictionary.usageCount)) .limit(limit) .all() - return words.map((w) => w.word).join(', ') + return words + .map((w) => { + const pron = typeof w.pronunciation === 'string' ? w.pronunciation.trim() : '' + return pron ? `${w.word} (${pron})` : w.word + }) + .join(', ') + } + + getPromptHint(limit = 50): string { + return this.getPromptHints(limit) + } + + /** + * 전사 텍스트에서 사전 단어 또는 발음(pronunciation) 변형을 찾아 표준 단어로 치환하고 + * 매칭된 사전 항목의 사용 횟수(usageCount)를 증가시킨다. + */ + applyDictionaryCorrections(text: string): { text: string; matchedIds: string[] } { + if (!text || text.trim().length === 0) { + return { text, matchedIds: [] } + } + + const db = getDatabase() + const entries = db + .select({ + id: dictionary.id, + word: dictionary.word, + pronunciation: dictionary.pronunciation + }) + .from(dictionary) + .orderBy(desc(dictionary.usageCount)) + .all() + + if (!entries || entries.length === 0) { + return { text, matchedIds: [] } + } + + let currentText = text + const matchedIds: string[] = [] + + const escapeRegExp = (str: string): string => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + const isAsciiWordChar = (ch: string | undefined): boolean => Boolean(ch && /[A-Za-z0-9]/.test(ch)) + const buildTokenRegex = (token: string): RegExp => { + const escaped = escapeRegExp(token) + const prefix = isAsciiWordChar(token[0]) ? '(? 0) { + const variants = Array.from( + new Set( + [rawPron, ...rawPron.split(/[,/;]+/)] + .map((v) => v.trim()) + .filter((v) => v.length > 0 && v.toLowerCase() !== canonicalWord.toLowerCase()) + ) + ).sort((a, b) => b.length - a.length) + + for (const variant of variants) { + const regex = buildTokenRegex(variant) + if (regex.test(currentText)) { + matched = true + currentText = currentText.replace(buildTokenRegex(variant), canonicalWord) + } + } + } + + const wordRegex = buildTokenRegex(canonicalWord) + if (wordRegex.test(currentText)) { + matched = true + currentText = currentText.replace(buildTokenRegex(canonicalWord), canonicalWord) + } + + if (matched) { + matchedIds.push(entry.id) + this.incrementUsage(entry.id) + } + } + + return { text: currentText, matchedIds } } async exportDictionary(params: DictionaryExportParams): Promise { diff --git a/apps/desktop/src/main/services/HistoryService.ts b/apps/desktop/src/main/services/HistoryService.ts index 5f9dd27..fb6e2c9 100644 --- a/apps/desktop/src/main/services/HistoryService.ts +++ b/apps/desktop/src/main/services/HistoryService.ts @@ -1,9 +1,9 @@ // src/main/services/HistoryService.ts // SQLite 기반 전사/명령 이력 저장. 설계서 01/03 IHistoryService 구현. -import { eq, desc, and, or, sql, count } from 'drizzle-orm' +import { eq, desc, asc, and, or, sql, count } from 'drizzle-orm' import { getDatabase } from '../db' -import { history, stats } from '../db/schema' +import { history, stats, memoTags } from '../db/schema' import type { History, NewHistory } from '../db/schema' import { getLogger } from './LoggerService' import { getCloudSyncService } from './CloudSyncService' @@ -11,6 +11,7 @@ import { getInputTelemetryService } from './InputTelemetryService' import { deleteHistoryLocally } from './history-deletion' import { computeStreakDays } from './history-stats' import { containsPattern } from '../db/like-pattern' +import { normalizeMemoTag } from '@d3ro/core/memo-tag-policy' import type { HistoryEntry, HistoryQueryParams, @@ -19,6 +20,22 @@ import type { StatsSummary } from '@d3ro/core/types' +export interface HistoryFilterOptions { + mode?: string | null + isFavorite?: boolean + tag?: string | null + search?: string | null + query?: string | null + beforeCreatedAt?: number | null + sortOrder?: 'asc' | 'desc' +} + +export interface HistoryUpdatePatch { + title?: string | null + polishedText?: string | null + originalText?: string +} + const logger = getLogger('HistoryService') class HistoryService { @@ -70,19 +87,119 @@ class HistoryService { return row ? this._toEntry(row) : null } - list(params: HistoryQueryParams): HistoryPage { - const db = getDatabase() - const { page, pageSize } = params + /** + * SQLite 기반 복합 필터·페이지네이션 조회. + * 기존 `(params: HistoryQueryParams)` 객체 호출과 `(limit, offset, search, filters)` 위치 인자 호출을 모두 지원한다. + */ + getAll( + paramsOrLimit?: (HistoryQueryParams & HistoryFilterOptions) | number, + offset?: number, + search?: string, + filters?: HistoryFilterOptions, + ): HistoryPage { + if (typeof paramsOrLimit === 'number' || paramsOrLimit === undefined) { + const pageSize = Math.max(1, paramsOrLimit ?? 50) + const safeOffset = Math.max(0, offset ?? 0) + const page = Math.floor(safeOffset / pageSize) + return this._queryWithFilters({ + page, + pageSize, + offsetOverride: safeOffset, + search: search ?? filters?.search ?? filters?.query ?? undefined, + mode: filters?.mode, + isFavorite: filters?.isFavorite, + tag: filters?.tag, + beforeCreatedAt: filters?.beforeCreatedAt, + sortOrder: filters?.sortOrder, + }) + } + return this.list(paramsOrLimit) + } - const totalResult = db.select({ count: count() }).from(history).get() + list(params: HistoryQueryParams & HistoryFilterOptions): HistoryPage { + return this._queryWithFilters({ + page: params.page, + pageSize: params.pageSize, + sortOrder: params.sortOrder, + mode: params.mode, + isFavorite: params.isFavorite, + tag: params.tag, + search: params.search ?? params.query, + beforeCreatedAt: params.beforeCreatedAt, + }) + } + + private _queryWithFilters(params: { + page: number + pageSize: number + offsetOverride?: number + sortOrder?: 'asc' | 'desc' + mode?: string | null + isFavorite?: boolean + tag?: string | null + search?: string | null + beforeCreatedAt?: number | null + }): HistoryPage { + const db = getDatabase() + const page = Math.max(0, params.page ?? 0) + const pageSize = Math.max(1, params.pageSize ?? 50) + const offset = params.offsetOverride ?? page * pageSize + + const conditions = [] + + const mode = params.mode?.trim() + if (mode && mode !== 'all') { + if (mode === 'file') { + conditions.push(eq(history.mode, 'file-transcription')) + } else if (mode === 'commandTranslate') { + conditions.push(or(eq(history.mode, 'command'), eq(history.mode, 'translate'))) + } else { + conditions.push(eq(history.mode, mode as History['mode'])) + } + } + + if (typeof params.isFavorite === 'boolean') { + conditions.push(eq(history.isFavorite, params.isFavorite)) + } + + const rawTag = params.tag?.trim() + if (rawTag) { + const normalizedTag = normalizeMemoTag(rawTag) + conditions.push( + sql`${history.id} IN (SELECT ${memoTags.historyId} FROM ${memoTags} WHERE ${memoTags.tag} = ${normalizedTag})`, + ) + } + + const rawSearch = params.search?.trim() + if (rawSearch) { + const pattern = containsPattern(rawSearch) + conditions.push( + or( + pattern.match(history.originalText), + pattern.match(history.polishedText), + pattern.match(history.title), + pattern.match(history.summaryText), + ), + ) + } + + if (typeof params.beforeCreatedAt === 'number' && Number.isFinite(params.beforeCreatedAt)) { + conditions.push(sql`${history.createdAt} < ${params.beforeCreatedAt}`) + } + + const whereClause = conditions.length > 0 ? and(...conditions) : undefined + const orderClause = params.sortOrder === 'asc' ? asc(history.createdAt) : desc(history.createdAt) + + const totalResult = whereClause + ? db.select({ count: count() }).from(history).where(whereClause).get() + : db.select({ count: count() }).from(history).get() const total = totalResult?.count ?? 0 - const entries = db - .select() - .from(history) - .orderBy(desc(history.createdAt)) + const entriesQuery = db.select().from(history) + const entries = (whereClause ? entriesQuery.where(whereClause) : entriesQuery) + .orderBy(orderClause) .limit(pageSize) - .offset(page * pageSize) + .offset(offset) .all() return { @@ -90,7 +207,7 @@ class HistoryService { total, page, pageSize, - totalPages: Math.ceil(total / pageSize) + totalPages: Math.ceil(total / pageSize), } } @@ -165,6 +282,63 @@ class HistoryService { return row ? this._toEntry(row) : null } + /** + * 기록 제목(title) 및 본문(polishedText / originalText) 수정. + * `(id, patch)` 또는 `({ id, ...patch })` 호출을 모두 지원한다. + */ + update( + idOrParams: string | ({ id: string } & HistoryUpdatePatch), + patch?: HistoryUpdatePatch, + ): HistoryEntry | null { + const id = typeof idOrParams === 'string' ? idOrParams : idOrParams.id + const resolvedPatch: HistoryUpdatePatch = + typeof idOrParams === 'string' ? (patch ?? {}) : idOrParams + + if (!id) return null + const db = getDatabase() + const existing = db.select().from(history).where(eq(history.id, id)).get() + if (!existing) return null + + const nextTitle = + resolvedPatch.title !== undefined + ? resolvedPatch.title === null + ? null + : resolvedPatch.title.trim() || null + : existing.title + + const nextPolishedText = + resolvedPatch.polishedText !== undefined + ? resolvedPatch.polishedText + : existing.polishedText + + const nextOriginalText = + resolvedPatch.originalText !== undefined + ? resolvedPatch.originalText + : existing.originalText + + let nextWordCount = existing.wordCount + if (resolvedPatch.polishedText !== undefined || resolvedPatch.originalText !== undefined) { + const effectiveText = (nextPolishedText ?? nextOriginalText ?? '').trim() + nextWordCount = effectiveText ? effectiveText.split(/\s+/).length : 0 + } + + const now = Date.now() + db.update(history) + .set({ + title: nextTitle, + polishedText: nextPolishedText, + originalText: nextOriginalText, + wordCount: nextWordCount, + updatedAt: now, + }) + .where(eq(history.id, id)) + .run() + + void getCloudSyncService().pushOne('history', id) + const updatedRow = db.select().from(history).where(eq(history.id, id)).get() + return updatedRow ? this._toEntry(updatedRow) : null + } + /** * 누적·오늘·연속 사용일 모두 history 테이블에서 계산한다(완료 세션만). 저장된 카운터는 로컬 생성만 * 반영해 동기화·가져오기·삭제와 어긋났고, streak은 쓰는 곳이 없어 늘 0이었다. diff --git a/apps/desktop/src/main/services/InputTelemetryService.ts b/apps/desktop/src/main/services/InputTelemetryService.ts index 508f020..42a6d42 100644 --- a/apps/desktop/src/main/services/InputTelemetryService.ts +++ b/apps/desktop/src/main/services/InputTelemetryService.ts @@ -72,6 +72,7 @@ import { acquireGlobalInputHook } from './global-input-hook' import { uiohookCodeToVk } from './KeyBindingService' import { getUiaContextService } from './UiaContextService' import { getForegroundWindowInfo, isForegroundKoreanIme } from '../utils/win32-foreground' +import { getLastNativeInputTick } from '../utils/win32-input-activity' import { getPersonalGraphService } from './PersonalGraphService' import { getPersonalNgramIndex } from './suggestion/PersonalNgramIndex' @@ -104,6 +105,10 @@ export interface TypingContext { windowHandle?: number | null idleMs: number capturedAt: number + /** 스냅샷을 읽는 동안 입력/포커스가 바뀌지 않았는지 확인한 단조 증가 번호. */ + inputRevision?: number + /** Windows 세션의 마지막 입력 tick. null이면 네이티브 입력 상태 확인 실패, 다른 OS는 생략. */ + nativeInputTick?: number | null /** 포커스가 바뀐 뒤 이 필드에서 실제로 편집이 있었는가 (마우스로 들어오기만 한 경우 false) */ editedSinceFocus: boolean /** 최근에 실제로 타이핑했는가 (recentTypingWindowMs 이내) */ @@ -193,6 +198,10 @@ class InputTelemetryService extends EventEmitter { private _sampleTimer: NodeJS.Timeout | null = null /** 주기 샘플 스냅샷 진행 중 (중복 방지) */ private _sampling = false + /** 모든 스냅샷 트리거를 합쳐 같은 UIA 응답을 중복 적용하지 않는다. */ + private _snapshotRequest: Promise | null = null + /** 같은 밀리초에 들어온 키·클릭도 서로 다른 편집으로 구분한다. */ + private _inputRevision = 0 /** 제안 오버레이가 표시 중일 때만 포커스 이탈/선택을 계속 확인한다. */ private _suggestionPresentationActive = false private _presentationValidationTimer: NodeJS.Timeout | null = null @@ -227,6 +236,8 @@ class InputTelemetryService extends EventEmitter { private _typedIntoFocus = false /** 현재 포커스/클릭 이후 실제로 글자 키를 쳤는가 (첫 스냅샷 전 빠른 입력 보존) */ private _typedInCurrentFocus = false + /** Chromium/Electron 등 UIA GetActiveComposition 미지원 앱을 위한 키스트로크 기반 한글 조합 추정 */ + private _imeComposing = false private _lastTypedHwnd: number | null = null /** 직전 스냅샷이 편집 가능한 칸이었는가 / 어느 창이었는가 (포커스 깜빡임 판정) */ private _prevSnapshotEditable = false @@ -314,6 +325,10 @@ class InputTelemetryService extends EventEmitter { return this._lastKeyAt } + get inputRevision(): number { + return this._inputRevision + } + /** * 곧 프로그램이 키 count 개를 보낸다 — 그 키들은 입력 통계에 넣지 않는다. * 남은 예고는 ttlMs 뒤 무효다(보내지 못한 키가 사용자 키를 삼키지 않게). @@ -321,6 +336,7 @@ class InputTelemetryService extends EventEmitter { expectProgrammaticKeys(count: number, ttlMs = 3000): void { this._programmaticKeys = Math.max(0, count) this._programmaticKeysUntil = Date.now() + ttlMs + if (count > 0) this._imeComposing = false } /** @@ -334,6 +350,8 @@ class InputTelemetryService extends EventEmitter { * 프로그램(오타 교정)이 커서 앞 deleteCount 글자를 지우고 insertText 를 넣었을 때 터미널 키 버퍼도 동기화한다. */ applyProgrammaticReplacement(deleteCount: number, insertText: string, baseLength?: number): void { + this._inputRevision += 1 + this._imeComposing = false this._terminalTracker.applyReplacement(deleteCount, insertText, baseLength) } @@ -372,6 +390,7 @@ class InputTelemetryService extends EventEmitter { this._releaseHook = acquireGlobalInputHook() this._running = true + this._inputRevision += 1 this._flushTimer = setInterval( () => this._flush(), INPUT_TELEMETRY_DEFAULTS.flushIntervalMs @@ -408,6 +427,7 @@ class InputTelemetryService extends EventEmitter { } stop(): void { + this._inputRevision += 1 // 입력 관찰을 끄면 메모리에 잡아 둔 "조금 전에 쓴 글" 도 버린다. this._recentWrites.clear() if (!this._running) { @@ -511,6 +531,8 @@ class InputTelemetryService extends EventEmitter { // ── 이벤트 수집 ──────────────────────────────────────── private _handleKeyDown(event: UiohookKeyboardEvent): void { + // 합성 키도 화면을 바꾼다. 통계 제외 여부와 무관하게 이전 UIA 응답은 무효다. + this._inputRevision += 1 this._syncModifiers(event) // Win32 SendInput(KEYEVENTF_UNICODE) 가 보내는 합성 유니코드 키는 uiohook 에서 keycode=0 으로 들어온다. // 사용자 키스트로크나 IME 입력으로 처리하면 버퍼와 타이머를 교란하므로 즉시 무시한다. @@ -563,6 +585,13 @@ class InputTelemetryService extends EventEmitter { } else if (keyClass === 'shortcut') { this._typedInCurrentFocus = false } + if (event.keycode === 112 || vk === 0x15) { + this._imeComposing = false + } else if (keyClass === 'ime' || keyClass === 'letter') { + this._imeComposing = true + } else if (keyClass !== 'modifier') { + this._imeComposing = false + } // 진단: 키 값이 아니라 종류·수정자만 남긴다 (typed 판정이 왜 틀리는지 로그로 보이게) this._lastKeyDiag = `${keyClass}${vk === null ? `?${event.keycode}` : ''}${this._modifiers.ctrl ? '+C' : ''}${this._modifiers.alt ? '+A' : ''}${this._modifiers.meta ? '+M' : ''}` @@ -594,6 +623,7 @@ class InputTelemetryService extends EventEmitter { } private _handleMouseDown(event: UiohookMouseEvent): void { + this._inputRevision += 1 this._registerInput(false) const now = Date.now() @@ -615,10 +645,12 @@ class InputTelemetryService extends EventEmitter { // 클릭은 다른 칸으로 옮겨 갔을 수 있다 — 다음 스냅샷의 텍스트를 새 기준으로 삼는다. this._rebaseFocusText = true this._typedInCurrentFocus = false + this._imeComposing = false this._terminalTracker.onPointerOrFocusChange(this._foreground.hwnd) } private _handleMouseUp(event: UiohookMouseEvent): void { + this._inputRevision += 1 this._lastMouse = { x: event.x, y: event.y } if (!this._suggestionPresentationActive) return if (this._presentationValidationTimer) clearTimeout(this._presentationValidationTimer) @@ -643,6 +675,7 @@ class InputTelemetryService extends EventEmitter { } private _handleWheel(event: UiohookWheelEvent): void { + this._inputRevision += 1 const bucket = this._bucket() bucket.scrollTicks += Math.max(1, Math.abs(Number(event.rotation) || 1)) this._registerInput(false) @@ -698,7 +731,9 @@ class InputTelemetryService extends EventEmitter { const info = getForegroundWindowInfo() if (info) { if (info.hwnd !== this._foreground.hwnd) { + this._inputRevision += 1 this._typedInCurrentFocus = false + this._imeComposing = false } this._foreground = { appName: info.appName || null, @@ -733,9 +768,13 @@ class InputTelemetryService extends EventEmitter { */ private async _sampleWhileTyping(): Promise { if (!this._running || this._sampling) return + const now = Date.now() + if (!this._suggestionPresentationActive && this._lastKeyAt > 0 && now - this._lastKeyAt < 350) { + return + } const hasRecentKeyboardInput = this._lastKeyAt > 0 && - Date.now() - this._lastKeyAt <= INPUT_TELEMETRY_DEFAULTS.sampleActiveWindowMs + now - this._lastKeyAt <= INPUT_TELEMETRY_DEFAULTS.sampleActiveWindowMs if (!hasRecentKeyboardInput && !this._suggestionPresentationActive) return this._sampling = true @@ -799,9 +838,38 @@ class InputTelemetryService extends EventEmitter { */ private async _captureTextSnapshot(): Promise { if (!this._running) return null + if (this._snapshotRequest) return this._snapshotRequest + + const request = this._readTextSnapshot() + this._snapshotRequest = request + try { + return await request + } finally { + if (this._snapshotRequest === request) this._snapshotRequest = null + } + } + + private async _readTextSnapshot(): Promise { + this._sampleForeground(Date.now(), true) + const inputRevision = this._inputRevision + const foregroundHwnd = this._foreground.hwnd + const nativeInputTick = getLastNativeInputTick() const uia = getUiaContextService() const snapshot = withoutPlaceholderText(await uia.getSnapshot()) + if (!this._running) return null + this._sampleForeground(Date.now(), true) + // CPU 부하/모델 로딩 중 UIA 응답이 늦어질 수 있다. 예전 텍스트를 최신 키 시각과 + // 섞으면 자동 교정이 방금 친 글자를 지운다. 기준선·학습·제안 모두 갱신 전에 버린다. + if ( + inputRevision !== this._inputRevision || + foregroundHwnd !== this._foreground.hwnd || + nativeInputTick !== getLastNativeInputTick() || + (snapshot.available && snapshot.capturedAt < Math.max(this._lastKeyAt, this._lastClickAt)) + ) { + logger.debug('입력 스냅샷 버림: UIA 조회 중 입력 또는 포커스 변경') + return null + } const now = Date.now() // 유휴 시간은 키보드 기준이다 (마우스 이동/클릭으로는 제안하지 않는다). const idleMs = this._lastKeyAt > 0 ? now - this._lastKeyAt : Number.MAX_SAFE_INTEGER @@ -824,6 +892,8 @@ class InputTelemetryService extends EventEmitter { windowHandle: this._foreground.hwnd, idleMs, capturedAt: snapshot.capturedAt, + inputRevision, + nativeInputTick: process.platform === 'win32' ? nativeInputTick : undefined, editedSinceFocus: false, typedRecently } @@ -920,10 +990,21 @@ class InputTelemetryService extends EventEmitter { if (clearedByEnter) { this._lastTypedAt = 0 this._typedInCurrentFocus = false + this._imeComposing = false context.typedRecently = false } } context.editedSinceFocus = textForFocus !== this._textAtFocus || this._typedIntoFocus + if ( + !context.isComposing && + this._imeComposing && + context.editedSinceFocus && + /[가-힣ㄱ-ㅎㅏ-ㅣ]$/u.test(context.prefix) + ) { + context.isComposing = true + } else if (context.isComposing && !/[가-힣ㄱ-ㅎㅏ-ㅣ]$/u.test(context.prefix)) { + context.isComposing = false + } // 보내면 비워지는 칸(채팅)의 직전 내용을 "이 창에서 조금 전에 쓴 글" 로 잠시 기억한다. // 비밀번호·편집 불가 칸과 터미널(명령 기록)은 보지 않는다. 조합 중인 칸도 본다 — 한글은 보내기 직전 @@ -942,7 +1023,7 @@ class InputTelemetryService extends EventEmitter { // 단, 방금 프로그램(오타 교정·제안 수락)이 커서 앞 텍스트를 바꾼 경우에는 끝 편집으로 본다. this._pruneExpectedInserts(now) const programmaticEdit = - this._programmaticKeyBudget > 0 || + this._programmaticKeys > 0 || this._expectedInserts.some((entry) => textForFocus.includes(entry.text)) if (this._editProbe === null || this._editProbe.key !== focusKey || programmaticEdit) { this._lastEditAtEnd = true diff --git a/apps/desktop/src/main/services/KeyBindingService.ts b/apps/desktop/src/main/services/KeyBindingService.ts index 09bbd60..91acdbb 100644 --- a/apps/desktop/src/main/services/KeyBindingService.ts +++ b/apps/desktop/src/main/services/KeyBindingService.ts @@ -555,10 +555,11 @@ class KeyBindingService extends EventEmitter { this._altGr.reset() forgetHeldModifiers() } - const events = ['lock-screen', 'suspend'] as const - for (const event of events) powerMonitor.on(event, releaseHeld) + powerMonitor.on('lock-screen', releaseHeld) + powerMonitor.on('suspend', releaseHeld) this._offSessionLock = () => { - for (const event of events) powerMonitor.removeListener(event, releaseHeld) + powerMonitor.removeListener('lock-screen', releaseHeld) + powerMonitor.removeListener('suspend', releaseHeld) } } diff --git a/apps/desktop/src/main/services/LicenseService.ts b/apps/desktop/src/main/services/LicenseService.ts index da9cb26..ffacb36 100644 --- a/apps/desktop/src/main/services/LicenseService.ts +++ b/apps/desktop/src/main/services/LicenseService.ts @@ -117,6 +117,9 @@ export class LicenseService extends EventEmitter { private _keyVerifiedAt: number | null = null private _cloudTier: LicenseTier = 'free' private _cloudVerifiedAt: number | null = null + private _rewardBonusDate = '' + private _rewardBonusAmount = 0 + private _claimedRewardNonces = new Set() constructor( private readonly _store: LicenseStore = new FileLicenseStore(), @@ -180,6 +183,27 @@ export class LicenseService extends EventEmitter { this._persistSummary() } + // 3. 보상형 광고 보너스 쿼터 & 리플레이 방지 논스 복원 + const today = getTodayDate() + const storedBonusDate = this._store.read('licenseRewardBonusDate') + const storedBonusAmount = this._store.read('licenseRewardBonusAmount') + const storedNonces = this._store.read('licenseRewardClaimedNonces') + if ( + storedBonusDate === today && + typeof storedBonusAmount === 'number' && + Number.isFinite(storedBonusAmount) && + storedBonusAmount > 0 + ) { + this._rewardBonusDate = today + this._rewardBonusAmount = Math.floor(storedBonusAmount) + } else { + this._rewardBonusDate = today + this._rewardBonusAmount = 0 + } + this._claimedRewardNonces = new Set( + Array.isArray(storedNonces) ? storedNonces.filter((n): n is string => typeof n === 'string') : [], + ) + logger.info( `LicenseService initialized: tier=${this.tier}, key=${this._key?.tier ?? 'none'}, cloud=${this._cloudTier}, trial=${this._key?.isTrial ?? false}, machineId=${this._machineId.substring(0, 8)}...`, ) @@ -338,10 +362,10 @@ export class LicenseService extends EventEmitter { /** 일일 사용량 조회 */ getUsage(feature: Feature): UsageQuota { - const limit = quotaLimitFor(this.tier, feature) + const baseLimit = quotaLimitFor(this.tier, feature) // 쿼터 없는 기능 (무제한) - if (limit === undefined) { + if (baseLimit === undefined) { return { feature, used: 0, @@ -351,6 +375,8 @@ export class LicenseService extends EventEmitter { } } + const bonus = feature === Feature.PREMIUM_LLM ? this.getRewardBonusQuota() : 0 + const limit = baseLimit + bonus const used = this._usage.getCount(getTodayDate(), feature) return { @@ -362,6 +388,104 @@ export class LicenseService extends EventEmitter { } } + /** 당일 보상형 광고 보너스 쿼터 조회 */ + getRewardBonusQuota(): number { + const today = getTodayDate() + if (this._rewardBonusDate !== today) { + const storedBonusDate = this._store.read('licenseRewardBonusDate') + const storedBonusAmount = this._store.read('licenseRewardBonusAmount') + if ( + storedBonusDate === today && + typeof storedBonusAmount === 'number' && + Number.isFinite(storedBonusAmount) && + storedBonusAmount > 0 + ) { + this._rewardBonusDate = today + this._rewardBonusAmount = Math.floor(storedBonusAmount) + } else { + this._rewardBonusDate = today + this._rewardBonusAmount = 0 + } + } + return this._rewardBonusAmount + } + + /** + * 보상형 광고 완료 검증 후 보너스 쿼터 부여 (리플레이/논스 중복 차감 방지). + */ + grantRewardQuota( + amount: number = 50, + nonce?: string, + ): { + granted: boolean + tokensAdded: number + bonusQuota: number + newTotalQuota: number + rewardId: string + } { + const currentQuota = this.getUsage(Feature.PREMIUM_LLM) + if (!Number.isFinite(amount) || amount <= 0) { + return { + granted: false, + tokensAdded: 0, + bonusQuota: this.getRewardBonusQuota(), + newTotalQuota: currentQuota.limit, + rewardId: '', + } + } + + const trimmedNonce = nonce?.trim() + if (trimmedNonce) { + const storedNonces = this._store.read('licenseRewardClaimedNonces') + if (Array.isArray(storedNonces)) { + for (const n of storedNonces) { + if (typeof n === 'string') this._claimedRewardNonces.add(n) + } + } + if (this._claimedRewardNonces.has(trimmedNonce)) { + return { + granted: false, + tokensAdded: 0, + bonusQuota: this.getRewardBonusQuota(), + newTotalQuota: currentQuota.limit, + rewardId: '', + } + } + this._claimedRewardNonces.add(trimmedNonce) + if (this._claimedRewardNonces.size > 512) { + const oldest = this._claimedRewardNonces.values().next().value + if (oldest !== undefined) { + this._claimedRewardNonces.delete(oldest) + } + } + } + + const today = getTodayDate() + const normalizedAmount = Math.floor(amount) + const currentBonus = this.getRewardBonusQuota() + this._rewardBonusDate = today + this._rewardBonusAmount = currentBonus + normalizedAmount + this._store.writeAll({ + licenseRewardBonusDate: this._rewardBonusDate, + licenseRewardBonusAmount: this._rewardBonusAmount, + licenseRewardClaimedNonces: Array.from(this._claimedRewardNonces), + }) + + const updatedQuota = this.getUsage(Feature.PREMIUM_LLM) + const rewardId = `reward_${Date.now()}_${trimmedNonce ?? Math.random().toString(36).slice(2, 8)}` + this.emit('quota-updated', updatedQuota) + logger.info( + `Reward quota granted: +${normalizedAmount} tokens (bonus=${this._rewardBonusAmount}, newLimit=${updatedQuota.limit})`, + ) + return { + granted: true, + tokensAdded: normalizedAmount, + bonusQuota: this._rewardBonusAmount, + newTotalQuota: updatedQuota.limit, + rewardId, + } + } + /** 전체 쿼터 기능 사용량 조회 */ getAllUsage(): UsageQuota[] { const quotaFeatures = [Feature.DICTATION, Feature.LLM_PROCESS] diff --git a/apps/desktop/src/main/services/LocalLLMService.ts b/apps/desktop/src/main/services/LocalLLMService.ts index ec65ea5..cb72289 100644 --- a/apps/desktop/src/main/services/LocalLLMService.ts +++ b/apps/desktop/src/main/services/LocalLLMService.ts @@ -13,6 +13,7 @@ import type { LLMStatus, LLMModel, LLMAction, LLMConnectionState } from '@d3ro/c import { resolveSystemPrompt } from './llm-prompts' import { getBundledOllamaPath } from '../utils/paths' import { normalizeLoopbackUrl } from '../utils/loopback' +import { localInferenceCpuOptions } from '../utils/inference-cpu-budget' import { readNdjsonLines } from '../utils/ndjson-reader' import { LOCAL_LLM_KEEP_ALIVE, @@ -449,6 +450,7 @@ class LocalLLMService extends EventEmitter { // gemma4/llama3.2 등 non-reasoning 모델에서는 무시됨. think: false, options: { + ...localInferenceCpuOptions(serverUrl), temperature: options?.temperature ?? 0.3, num_predict: maxTokens } @@ -514,6 +516,7 @@ class LocalLLMService extends EventEmitter { keep_alive: options?.keepAlive ?? LOCAL_LLM_KEEP_ALIVE, think: false, options: { + ...localInferenceCpuOptions(serverUrl), temperature: options?.temperature ?? 0.3, num_predict: maxTokens, ...(options?.stop && options.stop.length > 0 ? { stop: options.stop } : {}) @@ -889,6 +892,7 @@ class LocalLLMService extends EventEmitter { keep_alive: options?.keepAlive ?? LOCAL_LLM_KEEP_ALIVE, think: false, options: { + ...localInferenceCpuOptions(serverUrl), temperature: options?.temperature ?? 0.7, num_predict: maxTokens, }, diff --git a/apps/desktop/src/main/services/MeetingModeService.ts b/apps/desktop/src/main/services/MeetingModeService.ts index 2a63051..a503c45 100644 --- a/apps/desktop/src/main/services/MeetingModeService.ts +++ b/apps/desktop/src/main/services/MeetingModeService.ts @@ -379,28 +379,82 @@ class MeetingModeService extends EventEmitter { // ── 메모 추가 ── - async addMemo(content: string): Promise { - const session = this._current - if (this._state !== 'recording' || !session) { - throw new D3ROError(ErrorCode.MeetingNotRecording, '녹음 중이 아닙니다') + async addMemo(content: string, meetingId?: string): Promise { + const trimmed = content.trim() + if (!trimmed) { + throw new D3ROError(ErrorCode.MeetingNotRecording, '메모 내용이 비어 있습니다') } - const memo = session.addMemo(crypto.randomUUID(), content) - if (!memo) { + + const session = this._current + if (this._state === 'recording' && session && (!meetingId || meetingId === session.id)) { + const memo = session.addMemo(crypto.randomUUID(), content) + if (!memo) { + throw new D3ROError(ErrorCode.MeetingNotRecording, '녹음 중이 아닙니다') + } + + // DB 저장 + const db = getDatabase() + db.insert(meetingMemos).values({ + id: memo.id, + sessionId: memo.sessionId, + content: memo.content, + timestampMs: memo.timestampMs, + createdAt: memo.createdAt, + }).run() + + this.emit('memo-added', memo) + logger.debug(`메모 추가: [${formatTime(memo.timestampMs)}] ${content}`) + + // Phase 3.3: 자동 push (fire-and-forget) + getCloudSyncService().pushOne('meeting_memos', memo.id) + + return memo + } + + // 종료된 회의(또는 특정 meetingId)에 사후 메모 추가 + const targetSessionId = meetingId?.trim() || this._finalizedSessionId + if (!targetSessionId) { throw new D3ROError(ErrorCode.MeetingNotRecording, '녹음 중이 아닙니다') } - // DB 저장 const db = getDatabase() - db.insert(meetingMemos).values({ - id: memo.id, - sessionId: memo.sessionId, - content: memo.content, - timestampMs: memo.timestampMs, - createdAt: memo.createdAt, - }).run() + const sessionRow = db + .select() + .from(meetingSessions) + .where(eq(meetingSessions.id, targetSessionId)) + .get() + if (!sessionRow) { + throw new D3ROError( + meetingId ? ErrorCode.MeetingSessionNotFound : ErrorCode.MeetingNotRecording, + meetingId ? `회의 세션을 찾을 수 없습니다: ${targetSessionId}` : '녹음 중이 아닙니다', + ) + } + + const now = Date.now() + const timestampMs = + sessionRow.durationMs ?? + (sessionRow.endedAt ? Math.max(0, sessionRow.endedAt - sessionRow.startedAt) : 0) + + const memo: MeetingMemo = { + id: crypto.randomUUID(), + sessionId: targetSessionId, + content: trimmed, + timestampMs, + createdAt: now, + } + + db.insert(meetingMemos) + .values({ + id: memo.id, + sessionId: memo.sessionId, + content: memo.content, + timestampMs: memo.timestampMs, + createdAt: memo.createdAt, + }) + .run() this.emit('memo-added', memo) - logger.debug(`메모 추가: [${formatTime(memo.timestampMs)}] ${content}`) + logger.debug(`사후 회의 메모 추가: sessionId=${targetSessionId}, [${formatTime(memo.timestampMs)}] ${trimmed}`) // Phase 3.3: 자동 push (fire-and-forget) getCloudSyncService().pushOne('meeting_memos', memo.id) diff --git a/apps/desktop/src/main/services/MeetingSummaryService.ts b/apps/desktop/src/main/services/MeetingSummaryService.ts index aaa8e26..bdc4b34 100644 --- a/apps/desktop/src/main/services/MeetingSummaryService.ts +++ b/apps/desktop/src/main/services/MeetingSummaryService.ts @@ -24,19 +24,51 @@ const logger = getLogger('MeetingSummaryService') let isShowingSummarySaveDialog = false -const MEETING_SUMMARY_PROMPT = `다음은 회의 전사록입니다. 아래 형식으로 정리해주세요: +export interface TranscriptCitation { + label: string + timestampMs: number +} + +export function parseCitationTimestampMs(token: string): number | null { + const match = /^\[?(?:(\d{1,2}):)?(\d{1,3}):(\d{2})\]?$/.exec(token.trim()) + if (!match) return null + const hours = match[1] ? parseInt(match[1], 10) : 0 + const minutes = parseInt(match[2], 10) + const seconds = parseInt(match[3], 10) + if (seconds >= 60) return null + return (hours * 3600 + minutes * 60 + seconds) * 1000 +} + +export function extractTranscriptCitations(text: string): TranscriptCitation[] { + const regex = /\[(?:\d{1,2}:)?\d{1,3}:\d{2}\]/g + const seen = new Set() + const results: TranscriptCitation[] = [] + let match: RegExpExecArray | null + while ((match = regex.exec(text)) !== null) { + const label = match[0] + const ts = parseCitationTimestampMs(label) + if (ts !== null && !seen.has(label)) { + seen.add(label) + results.push({ label, timestampMs: ts }) + } + } + return results +} + +export const MEETING_SUMMARY_PROMPT = `다음은 회의 전사록입니다. 아래 형식으로 정리해주세요: ## 요약 -(3줄 이내 핵심 요약) +(3줄 이내 핵심 요약. 전사록에 [MM:SS] 시각이 있으면 관련 문장 끝에 [MM:SS] 인용 표기를 남기세요) ## 핵심 결정사항 -- (결정 1) -- (결정 2) +- (결정 1) [MM:SS] +- (결정 2) [MM:SS] ## 할 일 목록 -- [ ] (할 일 1) -- [ ] (할 일 2) +- [ ] (할 일 1) [MM:SS] +- [ ] (할 일 2) [MM:SS] +전사록에 [MM:SS] 타임스탬프가 포함되어 있다면 각 핵심 요약·결정사항·할 일 항목 끝에 근거가 되는 [MM:SS] 시각 인용을 함께 표기하세요. 전사록 외의 내용을 추가하지 마세요. 전사록이 비어 있거나 내용이 부족하면 "요약할 내용이 충분하지 않습니다"라고만 출력하세요.` class MeetingSummaryService extends EventEmitter { diff --git a/apps/desktop/src/main/services/MemoService.ts b/apps/desktop/src/main/services/MemoService.ts index d887a24..94e464d 100644 --- a/apps/desktop/src/main/services/MemoService.ts +++ b/apps/desktop/src/main/services/MemoService.ts @@ -1,13 +1,14 @@ // src/main/services/MemoService.ts // Phase 10.3: 음성 메모 태그 시스템. 히스토리 항목에 태그를 부착하고 태그별 검색/내보내기를 지원한다. -import { eq, and, desc, count, sql } from 'drizzle-orm' +import { eq, and, or, desc, count, sql } from 'drizzle-orm' import { app } from 'electron' import path from 'path' import fs from 'fs' import { getDatabase } from '../db' import { memoTags, history } from '../db/schema' import type { MemoTagRow } from '../db/schema' +import { containsPattern } from '../db/like-pattern' import { getLogger } from './LoggerService' import { getCloudSyncService } from './CloudSyncService' import { MEMO_TAG_MAX_LENGTH, memoTagKey, normalizeMemoTag, validateMemoTag } from '@d3ro/core/memo-tag-policy' @@ -24,6 +25,14 @@ import type { const logger = getLogger('MemoService') +export interface ExtendedExportMemoParams extends ExportMemoParams { + savePath?: string + includeUntagged?: boolean + mode?: string + isFavorite?: boolean + search?: string +} + class MemoService { /** * 특정 히스토리 항목에 부착된 태그 목록을 조회한다. @@ -205,47 +214,126 @@ class MemoService { /** * 태그+날짜 기준으로 그룹핑된 마크다운 파일을 생성하고 파일 경로를 반환한다. + * 태그가 지정되지 않은 경우 태그가 없는 히스토리 항목도 함께 내보낼 수 있으며, + * `savePath`가 전달되면 지정 경로에 저장한다. */ - exportMarkdown(params: ExportMemoParams): string { + exportMarkdown(params: ExtendedExportMemoParams): string { const db = getDatabase() - // 태그별 히스토리 조회 - const tagFilter = params.tag - ? eq(memoTags.tag, normalizeMemoTag(params.tag)) - : undefined - - const dateConditions: ReturnType[] = [] + const filterConditions = [] if (params.from) { const fromMs = new Date(params.from).getTime() - dateConditions.push(sql`${history.createdAt} >= ${fromMs}`) + filterConditions.push(sql`${history.createdAt} >= ${fromMs}`) } if (params.to) { const toMs = new Date(params.to).getTime() - dateConditions.push(sql`${history.createdAt} <= ${toMs}`) + filterConditions.push(sql`${history.createdAt} <= ${toMs}`) + } + if (params.mode && params.mode !== 'all') { + if (params.mode === 'file') { + filterConditions.push(eq(history.mode, 'file-transcription')) + } else if (params.mode === 'commandTranslate') { + filterConditions.push(or(eq(history.mode, 'command'), eq(history.mode, 'translate'))) + } else { + filterConditions.push(eq(history.mode, params.mode as HistoryEntry['mode'])) + } + } + if (typeof params.isFavorite === 'boolean') { + filterConditions.push(eq(history.isFavorite, params.isFavorite)) + } + if (params.search?.trim()) { + const pattern = containsPattern(params.search.trim()) + filterConditions.push( + or( + pattern.match(history.originalText), + pattern.match(history.polishedText), + pattern.match(history.title), + pattern.match(history.summaryText), + ), + ) } - const conditions = [tagFilter, ...dateConditions].filter( - (c): c is NonNullable => c !== undefined - ) + type ExportRow = { + tag: string + title: string | null + originalText: string + polishedText: string | null + createdAt: number + duration: number + } - const whereClause = conditions.length > 0 ? and(...conditions) : undefined + let rows: ExportRow[] = [] - const rows = db - .select({ - tag: memoTags.tag, - originalText: history.originalText, - polishedText: history.polishedText, - createdAt: history.createdAt, - duration: history.duration - }) - .from(memoTags) - .innerJoin(history, eq(memoTags.historyId, history.id)) - .where(whereClause) - .orderBy(memoTags.tag, desc(history.createdAt)) - .all() + if (params.tag) { + const normalizedTag = normalizeMemoTag(params.tag) + const conditions = [eq(memoTags.tag, normalizedTag), ...filterConditions].filter( + (c): c is NonNullable => c !== undefined, + ) + const whereClause = conditions.length > 0 ? and(...conditions) : undefined + + rows = db + .select({ + tag: memoTags.tag, + title: history.title, + originalText: history.originalText, + polishedText: history.polishedText, + createdAt: history.createdAt, + duration: history.duration, + }) + .from(memoTags) + .innerJoin(history, eq(memoTags.historyId, history.id)) + .where(whereClause) + .orderBy(memoTags.tag, desc(history.createdAt)) + .all() + } else if (params.includeUntagged === false) { + const conditions = filterConditions.filter((c): c is NonNullable => c !== undefined) + const whereClause = conditions.length > 0 ? and(...conditions) : undefined + + rows = db + .select({ + tag: memoTags.tag, + title: history.title, + originalText: history.originalText, + polishedText: history.polishedText, + createdAt: history.createdAt, + duration: history.duration, + }) + .from(memoTags) + .innerJoin(history, eq(memoTags.historyId, history.id)) + .where(whereClause) + .orderBy(memoTags.tag, desc(history.createdAt)) + .all() + } else { + const conditions = filterConditions.filter((c): c is NonNullable => c !== undefined) + const whereClause = conditions.length > 0 ? and(...conditions) : undefined + + const rawRows = db + .select({ + tag: memoTags.tag, + title: history.title, + originalText: history.originalText, + polishedText: history.polishedText, + createdAt: history.createdAt, + duration: history.duration, + }) + .from(history) + .leftJoin(memoTags, eq(memoTags.historyId, history.id)) + .where(whereClause) + .orderBy(desc(history.createdAt)) + .all() + + rows = rawRows.map((r) => ({ + tag: r.tag ?? 'untagged', + title: r.title, + originalText: r.originalText, + polishedText: r.polishedText, + createdAt: r.createdAt, + duration: r.duration, + })) + } // 태그별 → 날짜별 그룹핑 - const grouped = new Map>() + const grouped = new Map>() for (const row of rows) { const dateKey = localDateKey(new Date(row.createdAt)) if (!grouped.has(row.tag)) { @@ -269,7 +357,7 @@ class MemoService { } for (const [tag, dateMap] of grouped) { - lines.push(`## #${tag}`) + lines.push(tag === 'untagged' ? '## Untagged' : `## #${tag}`) lines.push('') for (const [date, entries] of dateMap) { @@ -282,26 +370,35 @@ class MemoService { minute: '2-digit' }) const text = entry.polishedText ?? entry.originalText + const titlePrefix = entry.title?.trim() ? `[${entry.title.trim()}] ` : '' const durationSec = Math.round(entry.duration) - lines.push(`- **${time}** (${durationSec}s): ${text}`) + lines.push(`- **${time}** (${durationSec}s): ${titlePrefix}${text}`) } lines.push('') } } - // 파일 저장 - const exportDir = path.join(app.getPath('userData'), 'exports') - if (!fs.existsSync(exportDir)) { - fs.mkdirSync(exportDir, { recursive: true }) - } + let filePath: string + if (params.savePath?.trim()) { + filePath = path.resolve(params.savePath.trim()) + const targetDir = path.dirname(filePath) + if (!fs.existsSync(targetDir)) { + fs.mkdirSync(targetDir, { recursive: true }) + } + } else { + const exportDir = path.join(app.getPath('userData'), 'exports') + if (!fs.existsSync(exportDir)) { + fs.mkdirSync(exportDir, { recursive: true }) + } - const fileName = buildMemoExportFileName( - params.tag ? normalizeMemoTag(params.tag) : undefined, - Date.now() - ) - const filePath = path.join(exportDir, fileName) - if (path.dirname(path.resolve(filePath)) !== path.resolve(exportDir)) { - throw new D3ROError(ErrorCode.MemoExportFailed, 'Memo export path escapes the exports folder') + const fileName = buildMemoExportFileName( + params.tag ? normalizeMemoTag(params.tag) : undefined, + Date.now() + ) + filePath = path.join(exportDir, fileName) + if (path.dirname(path.resolve(filePath)) !== path.resolve(exportDir)) { + throw new D3ROError(ErrorCode.MemoExportFailed, 'Memo export path escapes the exports folder') + } } try { diff --git a/apps/desktop/src/main/services/PersonalGraphService.ts b/apps/desktop/src/main/services/PersonalGraphService.ts index ad084e8..23ac7e6 100644 --- a/apps/desktop/src/main/services/PersonalGraphService.ts +++ b/apps/desktop/src/main/services/PersonalGraphService.ts @@ -23,7 +23,8 @@ import { type GraphContext, type PersonalGraphQuery, type PersonalGraphStats, - type PhraseSource + type PhraseSource, + type TwoHopGraphEdge } from '@d3ro/core/personal-graph' import { SUGGESTION_MEMORY_TAIL_CHARS, isLearnablePhrase, prefixTail } from '@d3ro/core/input-intelligence' import { analyzeWritingContext } from '@d3ro/core/writing-context' @@ -254,16 +255,16 @@ class PersonalGraphService { const directFollowHops = firstHopEdges.filter( (edge) => edge.kind === 'follows' && anchors.includes(edge.fromPhrase) ) - const normalizedFirstHop = firstHopEdges.flatMap((edge) => { + const normalizedFirstHop = firstHopEdges.flatMap((edge) => { if (edge.kind === 'follows' && anchors.includes(edge.fromPhrase)) { - return [{ from: edge.fromPhrase, to: edge.toPhrase, kind: 'follows' as const, weight: edge.weight }] + return [{ from: edge.fromPhrase, to: edge.toPhrase, kind: 'follows', weight: edge.weight }] } if (edge.kind === 'shares_terms') { if (anchors.includes(edge.fromPhrase)) { - return [{ from: edge.fromPhrase, to: edge.toPhrase, kind: 'shares_terms' as const, weight: edge.weight }] + return [{ from: edge.fromPhrase, to: edge.toPhrase, kind: 'shares_terms', weight: edge.weight }] } if (anchors.includes(edge.toPhrase)) { - return [{ from: edge.toPhrase, to: edge.fromPhrase, kind: 'shares_terms' as const, weight: edge.weight }] + return [{ from: edge.toPhrase, to: edge.fromPhrase, kind: 'shares_terms', weight: edge.weight }] } } return [] diff --git a/apps/desktop/src/main/services/SuggestionService.ts b/apps/desktop/src/main/services/SuggestionService.ts index 8e4e036..86cb0de 100644 --- a/apps/desktop/src/main/services/SuggestionService.ts +++ b/apps/desktop/src/main/services/SuggestionService.ts @@ -2145,7 +2145,7 @@ export class SuggestionService extends EventEmitter { const anchor = this._anchor const anchorKind = this._anchorKind const provenance = this._provenance - const composing = this._lastContext?.isComposing === true + const composing = this._lastContext?.isComposing === true && /[가-힣]$/u.test(acceptPrefix) const appName = this._appName const windowTitle = this._windowTitle const targetWindow = this._windowHandle @@ -2251,7 +2251,9 @@ export class SuggestionService extends EventEmitter { try { if (deleteCount > 0) { // 확정용 Space(+1)·백스페이스·붙여넣기(Ctrl, V) 키는 사용자 입력 통계에서 뺀다. - this._learning.expectProgrammaticKeys?.(deleteCount + (composing ? 2 : 0) + 2) + this._learning.expectProgrammaticKeys?.( + deleteCount + (composing ? 2 : 0) + (insertMethod === 'keyboard' ? 0 : 2) + ) } const result = deleteCount > 0 diff --git a/apps/desktop/src/main/services/TextInsertService.ts b/apps/desktop/src/main/services/TextInsertService.ts index 4e21daf..a2374ae 100644 --- a/apps/desktop/src/main/services/TextInsertService.ts +++ b/apps/desktop/src/main/services/TextInsertService.ts @@ -25,6 +25,16 @@ const logger = getLogger('TextInsertService') type InsertMethod = 'clipboard' | 'keyboard' +export interface ReplacementOptions { + commitComposition?: boolean + /** Revalidate after queue/module waits, immediately before sending destructive keys. */ + canApply?: () => boolean + /** Register synthetic input only when the replacement is about to run. */ + beforeApply?: () => void + /** Automatic Windows corrections must not fall back to separate delete/type operations. */ + requireAtomic?: boolean +} + interface InsertResult { success: boolean /** 실제로 쓴 방식 — 클립보드를 되돌릴 수 없으면 'clipboard' 요청도 'keyboard' 가 된다 */ @@ -101,11 +111,14 @@ export class TextInsertService extends EventEmitter { deleteCount: number, text: string, method: InsertMethod = 'clipboard', - options: { commitComposition?: boolean } = {} + options: ReplacementOptions = {} ): Promise { const run = this._tail.then(async () => { + const skipped = (): InsertResult => ({ success: false, method, textLength: 0, durationMs: 0 }) + if (options.canApply && !options.canApply()) return skipped() if (method === 'keyboard' && this._keyboard.replaceBeforeCaret) { const start = performance.now() + options.beforeApply?.() const handled = await this._keyboard.replaceBeforeCaret(deleteCount, text, options) if (handled) { this.emit('insert-started', { text, method: 'keyboard' }) @@ -122,7 +135,12 @@ export class TextInsertService extends EventEmitter { return result } } + if (options.requireAtomic) return skipped() try { + // Loading the fallback adapter can take seconds. A check before this await is insufficient. + await this._keyboard.preload?.() + if (options.canApply && !options.canApply()) return skipped() + options.beforeApply?.() let count = deleteCount // 커서 앞 글자가 IME 조합 중이면 먼저 확정한다 (Space → 남은 공백까지 지운다). if (options.commitComposition && this._keyboard.commitComposition) { diff --git a/apps/desktop/src/main/services/TypoCorrectionService.ts b/apps/desktop/src/main/services/TypoCorrectionService.ts index db05dd2..6be818b 100644 --- a/apps/desktop/src/main/services/TypoCorrectionService.ts +++ b/apps/desktop/src/main/services/TypoCorrectionService.ts @@ -45,6 +45,7 @@ import { configGet, configSet } from './ConfigService' import { getLogger } from './LoggerService' import { buildTypoCheckPrompt } from './llm-prompts' import type { TypingContext } from './InputTelemetryService' +import type { ReplacementOptions } from './TextInsertService' const logger = getLogger('TypoCorrectionService') @@ -78,9 +79,13 @@ export type TypoVerdict = 'pass' | 'correcting' | 'checking' export interface TypoCorrectionDeps { /** 커서 앞 deleteCount 글자를 지우고 text 를 넣는다. 성공 여부. */ - replaceBeforeCaret(deleteCount: number, text: string, options?: { commitComposition?: boolean }): Promise + replaceBeforeCaret(deleteCount: number, text: string, options?: ReplacementOptions): Promise /** 마지막 키보드 입력 시각 */ lastKeyAt(): number + /** Monotonic revision including clicks/focus changes and same-millisecond key events. */ + inputRevision(): number + /** OS-side activity also catches physical keys still waiting in the JS hook queue. */ + nativeInputTick(): number | null /** 곧 보낼 프로그램 키 수 (입력 통계 제외) */ expectProgrammaticKeys(count: number): void /** 곧 삽입할 텍스트 (학습 제외). 취소 함수를 돌려준다. */ @@ -297,8 +302,12 @@ export class TypoCorrectionService extends EventEmitter { const worthModel = this._worthModelCheck(region) const model = configGet('typoAutoCorrectUseModel') !== false ? this._deps.warmModel() : null if (!model || !worthModel) { + // 아직 공백·문장부호로 끝나지 않은 커서 끝 토큰(c.end === prefix.length)은 사용자가 다음 음절을 + // 조합하는 도중일 수 있으므로 인플레이스 자동 백스페이스 삭제 대상에서 제외한다(제안 팝업 propose 에서만 제시). const tailJamo = detectJamoNoise(prefix, { ignoredWords }).filter( - (c) => prefix.length - c.start <= MAX_MODEL_DELETE_CHARS + (c) => + (c.to !== '' ? c.end < prefix.length : /[ \t]$/u.test(c.from)) && + prefix.length - c.start <= MAX_MODEL_DELETE_CHARS ) const jamoPlan = planTypoReplacement(prefix, tailJamo) if (jamoPlan && jamoPlan.deleteCount <= MAX_MODEL_DELETE_CHARS) { @@ -453,7 +462,11 @@ export class TypoCorrectionService extends EventEmitter { if (abort.signal.aborted || this._check?.abort !== abort) return // 인플레이스 모델 교정은 커서 바로 앞 어절(MAX_MODEL_DELETE_CHARS 이내)만 고친다. // 문장 앞쪽 단어를 고치려고 50~70자를 통째로 백스페이스하면 사용자의 다음 타이핑과 정면 충돌한다. - const tailCorrections = mergeTypoCorrections(fromModel, detectJamoNoise(prefix, { ignoredWords })).filter( + // 또한 커서 바로 끝(c.end === prefix.length)의 미완성 낱자 삭제는 자동 인플레이스 교정에서 제외한다. + const safeJamoNoise = detectJamoNoise(prefix, { ignoredWords }).filter((c) => + c.to !== '' ? c.end < prefix.length : /[ \t]$/u.test(c.from) + ) + const tailCorrections = mergeTypoCorrections(fromModel, safeJamoNoise).filter( (c) => prefix.length - c.start <= MAX_MODEL_DELETE_CHARS ) const basePlan = planTypoReplacement(prefix, tailCorrections) @@ -534,19 +547,19 @@ export class TypoCorrectionService extends EventEmitter { private async _apply(plan: TypoReplacementPlan, context: TypingContext, keyAtDecision: number): Promise { const preRegion = context.prefix.slice(typoRegionStart(context.prefix)) - if (this._deps.lastKeyAt() !== keyAtDecision) { + const revision = context.inputRevision ?? this._deps.inputRevision() + const canApply = (): boolean => + this.isEnabled() && + this._deps.lastKeyAt() === keyAtDecision && + this._deps.inputRevision() === revision && + (context.windowHandle == null || this._deps.foregroundHandle() === context.windowHandle) && + (context.nativeInputTick === undefined || + (context.nativeInputTick !== null && this._deps.nativeInputTick() === context.nativeInputTick)) + if (!canApply()) { this._unmarkRegionChecked(preRegion) - logger.debug('오타 교정 취소: 판단 뒤 키 입력이 있었다') + logger.debug('오타 교정 취소: 입력 또는 포커스가 변경됐다') return false } - if (context.windowHandle != null) { - const current = this._deps.foregroundHandle() - if (current !== null && current !== context.windowHandle) { - this._unmarkRegionChecked(preRegion) - logger.debug('오타 교정 취소: 포커스가 다른 창으로 옮겨 갔다') - return false - } - } this._applying = true // 커서 바로 앞 글자가 완성형 한글 음절([가-힣])이고 조합 중일 때만 Space 로 조합을 확정한 뒤 지운다. @@ -554,14 +567,21 @@ export class TypoCorrectionService extends EventEmitter { // 불필요한 Space 를 보내 앞의 글자까지 지우거나 유령 공백을 남기지 않는다. const commitComposition = context.isComposing === true && /[가-힣]$/u.test(context.prefix) // 확정용 Space(+2)·백스페이스 + 붙여넣기(Ctrl, V) 키는 사용자 입력 통계에서 뺀다. - this._deps.expectProgrammaticKeys(plan.deleteCount + (commitComposition ? 2 : 0) + 2) - const cancelInsert = this._deps.expectProgrammaticInsert(plan.insertText) + let cancelInsert = (): void => undefined try { - const ok = commitComposition - ? await this._deps.replaceBeforeCaret(plan.deleteCount, plan.insertText, { commitComposition: true }) - : await this._deps.replaceBeforeCaret(plan.deleteCount, plan.insertText) + const ok = await this._deps.replaceBeforeCaret(plan.deleteCount, plan.insertText, { + commitComposition, + canApply, + requireAtomic: process.platform === 'win32', + beforeApply: () => { + cancelInsert() + this._deps.expectProgrammaticKeys(plan.deleteCount + (commitComposition ? 2 : 0) + 2) + cancelInsert = this._deps.expectProgrammaticInsert(plan.insertText) + } + }) if (!ok) { cancelInsert() + this._deps.expectProgrammaticKeys(0) this._unmarkRegionChecked(preRegion) logger.warn('오타 교정 삽입 실패') return false @@ -587,6 +607,7 @@ export class TypoCorrectionService extends EventEmitter { return true } catch (error) { cancelInsert() + this._deps.expectProgrammaticKeys(0) this._lastCheckedRegion = '' logger.warn(`오타 교정 예외: ${error instanceof Error ? error.message : String(error)}`) return false diff --git a/apps/desktop/src/main/services/UnifiedSearchService.ts b/apps/desktop/src/main/services/UnifiedSearchService.ts index ff45299..88af554 100644 --- a/apps/desktop/src/main/services/UnifiedSearchService.ts +++ b/apps/desktop/src/main/services/UnifiedSearchService.ts @@ -1,7 +1,8 @@ // 기록과 회의를 스키마 변경 없이 함께 찾는 로컬 통합 검색. import { EventEmitter } from 'events' -import { count, desc, or } from 'drizzle-orm' +import { count, desc, inArray, or } from 'drizzle-orm' +import { formatTranscriptTimestamp, parseTranscriptLines } from '@d3ro/core/meeting-transcript' import { D3ROError, ErrorCode } from '@d3ro/core/errors' import type { UnifiedSearchParams, @@ -10,7 +11,14 @@ import type { } from '@d3ro/core/types' import { getDatabase } from '../db' import { containsPattern } from '../db/like-pattern' -import { history, meetingSessions, type History, type MeetingSessionRow } from '../db/schema' +import { + history, + meetingMemos, + meetingSessions, + type History, + type MeetingMemoRow, + type MeetingSessionRow, +} from '../db/schema' import { getLogger } from './LoggerService' const logger = getLogger('UnifiedSearchService') @@ -86,18 +94,59 @@ function recordResult(row: History, query: string): UnifiedSearchResult { } } -function meetingResult(row: MeetingSessionRow, query: string): UnifiedSearchResult { +function findMatchingTranscriptSegmentSnippet( + transcript: string | null | undefined, + query: string, +): string | null { + const lines = parseTranscriptLines(transcript) + for (const line of lines) { + if (includesQuery(line.text, query) || includesQuery(line.speaker, query)) { + if (!line.timed) return line.text + const speakerPrefix = line.speaker ? `[${line.speaker}] ` : '' + return `[${formatTranscriptTimestamp(line.timestampMs)}] ${speakerPrefix}${line.text}` + } + } + return null +} + +function formatMemoSnippet(memo: MeetingMemoRow): string { + return `[${formatTranscriptTimestamp(memo.timestampMs)}] ${memo.content}` +} + +function meetingResult( + row: MeetingSessionRow, + query: string, + memoMatch?: MeetingMemoRow, +): UnifiedSearchResult { const titleMatched = includesQuery(row.title, query) const matchingTranscript = [row.editedTranscript, row.rawTranscript] .find((value) => includesQuery(value, query)) - const displayContent = matchingTranscript ?? row.editedTranscript ?? row.rawTranscript ?? row.title + const segmentSnippet = findMatchingTranscriptSegmentSnippet( + matchingTranscript ?? row.editedTranscript ?? row.rawTranscript, + query, + ) + const memoSnippet = memoMatch ? formatMemoSnippet(memoMatch) : null + const displayContent = segmentSnippet + ?? matchingTranscript + ?? memoSnippet + ?? row.editedTranscript + ?? row.rawTranscript + ?? row.title + + const matchedField = titleMatched && !matchingTranscript && !memoSnippet + ? 'title' + : matchingTranscript || segmentSnippet + ? 'transcript' + : memoSnippet + ? 'content' + : 'transcript' return { source: 'meeting', id: row.id, title: row.title, excerpt: createUnifiedSearchExcerpt(displayContent, query), - matchedField: titleMatched ? 'title' : 'transcript', + matchedField, occurredAt: row.startedAt, } } @@ -147,16 +196,49 @@ class UnifiedSearchService extends EventEmitter { .select() .from(meetingSessions) .where(meetingConditions) - .orderBy(desc(meetingSessions.startedAt)) + .orderBy(desc(meetingSessions.createdAt)) + .limit(limit) + .all() + const memoRows = db + .select() + .from(meetingMemos) + .where(pattern.match(meetingMemos.content)) + .orderBy(desc(meetingMemos.timestampMs)) .limit(limit) .all() + const memoBySessionId = new Map() + for (const memo of memoRows) { + if (!memoBySessionId.has(memo.sessionId)) { + memoBySessionId.set(memo.sessionId, memo) + } + } + + const directMeetingIds = new Set(meetingRows.map((row) => row.id)) + const missingSessionIds = [...memoBySessionId.keys()].filter( + (sessionId) => !directMeetingIds.has(sessionId), + ) + const memoMatchedMeetings = missingSessionIds.length > 0 + ? db + .select() + .from(meetingSessions) + .where(inArray(meetingSessions.id, missingSessionIds)) + .orderBy(desc(meetingSessions.createdAt)) + .limit(limit) + .all() + : [] + + const combinedMeetingRows = [...meetingRows, ...memoMatchedMeetings] const results = mergeUnifiedSearchResults( recordRows.map((row) => recordResult(row, query)), - meetingRows.map((row) => meetingResult(row, query)), + combinedMeetingRows.map((row) => meetingResult(row, query, memoBySessionId.get(row.id))), limit, ) - return { query, results, total: recordTotal + meetingTotal } + return { + query, + results, + total: recordTotal + meetingTotal + memoMatchedMeetings.length, + } } catch (error) { const message = error instanceof Error ? error.message : String(error) logger.error(`Unified search failed: ${message}`) diff --git a/apps/desktop/src/main/services/VoiceConversationService.ts b/apps/desktop/src/main/services/VoiceConversationService.ts index 8402a4c..c56c1dc 100644 --- a/apps/desktop/src/main/services/VoiceConversationService.ts +++ b/apps/desktop/src/main/services/VoiceConversationService.ts @@ -162,6 +162,86 @@ class VoiceConversationService extends EventEmitter { this._sendToRenderer(IPC_CHANNELS.VOICE_CONVERSATION.STATE_CHANGED, this.getSessionInfo()) } + /** + * 이전 대화 메시지(또는 지정한 텍스트)를 TTS로 다시 읽어준다. + * 활성 세션 중이면 마이크 캡처를 일시 중지하고 speaking 상태로 재생한 뒤 다시 listening으로 복귀한다. + */ + async replayMessage( + target?: string | { messageId?: string; text?: string }, + ): Promise { + let textToSpeak = '' + + if (typeof target === 'string') { + const found = this._messages.find((m) => m.id === target) + if (found) { + textToSpeak = found.content + } else if (this._messages.length === 0 && target.trim().length > 0) { + textToSpeak = target + } else { + throw new D3ROError(ErrorCode.ConversationLLMFailed, `Conversation message not found: ${target}`) + } + } else if (target && typeof target === 'object') { + if (target.messageId) { + const found = this._messages.find((m) => m.id === target.messageId) + if (found) { + textToSpeak = found.content + } else if (typeof target.text === 'string' && target.text.trim().length > 0) { + textToSpeak = target.text + } else { + throw new D3ROError( + ErrorCode.ConversationLLMFailed, + `Conversation message not found: ${target.messageId}`, + ) + } + } else if (typeof target.text === 'string') { + textToSpeak = target.text + } + } else { + for (let i = this._messages.length - 1; i >= 0; i--) { + if (this._messages[i].role === 'assistant') { + textToSpeak = this._messages[i].content + break + } + } + } + + const trimmed = textToSpeak.trim() + if (!trimmed) { + throw new D3ROError(ErrorCode.ConversationLLMFailed, 'No conversation message to replay') + } + + const sentences = trimmed + .split(/(?<=[.!?。!?])\s+/) + .map((s) => s.trim()) + .filter((s) => s.length > 0) + const ttsSentences = sentences.length > 0 ? sentences : [trimmed] + + const wasActive = this._isActive + const turn = this._turn + if (wasActive) { + this._stopListening() + this._setState('speaking') + } + + this._sendToRenderer(IPC_CHANNELS.VOICE_CONVERSATION.TTS_STARTED, {}) + try { + await getTTSPlaybackService().speakSentences(ttsSentences) + } finally { + if (!wasActive || (this._isActive && this._turn === turn)) { + this._sendToRenderer(IPC_CHANNELS.VOICE_CONVERSATION.TTS_FINISHED, {}) + } + if (wasActive && this._isActive && this._turn === turn) { + this._resumeListening() + } + } + } + + async speakMessage( + target?: string | { messageId?: string; text?: string }, + ): Promise { + return this.replayMessage(target) + } + // ── 내부 로직 ── private _setState(state: ConversationState): void { diff --git a/apps/desktop/src/main/services/VoiceModeService.ts b/apps/desktop/src/main/services/VoiceModeService.ts index a368458..e5f00b2 100644 --- a/apps/desktop/src/main/services/VoiceModeService.ts +++ b/apps/desktop/src/main/services/VoiceModeService.ts @@ -30,6 +30,7 @@ import { getLlmGateway } from './llm/LlmGateway' import { buildInstructionInvocation, resolveTargetLanguage } from './llm-prompts' import { D3ROError, ErrorCode } from '@d3ro/core/errors' import { cleanDictatedText } from '@d3ro/core/dictation-cleaner' +import { composeInstructionPipelinePrompt, parseActiveInstructionIds } from '@d3ro/core/instruction-template' import { TIMING } from '@d3ro/core/constants' import { RecognitionState, AudioState } from '@d3ro/core/types' import type { KeyBindingActionId, VoiceMode, VoiceState, LLMAction } from '@d3ro/core/types' @@ -132,6 +133,8 @@ class SessionRun { audioStarted = false /** 사용자가 이미 정지를 요청했다 — 두 번째 release/토글은 무시한다 */ stopRequested = false + /** 이 세션을 시작한 키바인딩 액션 */ + actionId: KeyBindingActionId = 'dictation' sttReady = false transcribing = false audioBuffer: Buffer[] = [] @@ -266,31 +269,7 @@ class VoiceModeService extends EventEmitter { */ connectKeyBindings(): void { this._keyBindingHandler = (payload) => { - switch (payload.actionId) { - case 'caption': - // Phase 10.1: caption은 VoiceModeService가 아닌 CaptionService로 라우팅. - // 토글이므로 press만 처리하고 release는 버린다. - if (payload.type === 'pressed') this._toggleCaption() - return - case 'dictation': - case 'hands-free': - case 'command': - break - default: - // 음성 세션 액션만 여기서 처리한다. 팝업·제안 액션은 bootstrap이 직접 구독한다 - // (허용 목록이 아니면 새 액션이 생길 때마다 받아쓰기로 오인돼 녹음이 켜진다). - return - } - - const holdMode = resolveGestureHoldMode(payload.actionId, payload.holdMode) - const action: VoiceAction = { - type: payload.type === 'pressed' ? 'press' : 'release', - timestamp: payload.timestamp, - mode: resolveGestureMode(payload.actionId, payload.isDoublePress), - actionId: payload.actionId, - holdMode - } - this._enqueueAction(action) + this.handleShortcutTrigger(payload) } getKeyBindingService().on('triggered', this._keyBindingHandler) @@ -298,9 +277,47 @@ class VoiceModeService extends EventEmitter { logger.info('Key binding events connected') } + /** + * 키바인딩 트리거 이벤트를 받아 세션 액션 큐에 넣는다. + */ + handleShortcutTrigger(payload: KeyBindingTriggerPayload): void { + switch (payload.actionId) { + case 'caption': + // Phase 10.1: caption은 VoiceModeService가 아닌 CaptionService로 라우팅. + // 토글이므로 press만 처리하고 release는 버린다. + if (payload.type === 'pressed') void this._toggleCaption() + return + case 'dictation': + case 'hands-free': + case 'command': + break + default: + // 음성 세션 액션만 여기서 처리한다. 팝업·제안 액션은 bootstrap이 직접 구독한다 + // (허용 목록이 아니면 새 액션이 생길 때마다 받아쓰기로 오인돼 녹음이 켜진다). + return + } + + const holdMode = this._resolveHoldMode(payload.actionId, payload.holdMode) + const action: VoiceAction = { + type: payload.type === 'pressed' ? 'press' : 'release', + timestamp: payload.timestamp, + mode: resolveGestureMode(payload.actionId, payload.isDoublePress), + actionId: payload.actionId, + holdMode + } + this._enqueueAction(action) + } + + _resolveHoldMode(actionId: KeyBindingActionId, specHoldMode = false): boolean { + return resolveGestureHoldMode(actionId, specHoldMode) + } + // ── 세션 제어 ────────────────────────────────────────── - async startSession(mode: VoiceMode): Promise { + async startSession( + mode: VoiceMode, + options?: { actionId?: KeyBindingActionId }, + ): Promise { if (this._disposed) return if (this._session && !this._isInTerminalState()) { logger.warn('Session already active, ignoring startSession') @@ -380,6 +397,7 @@ class VoiceModeService extends EventEmitter { // 세션 생성 const run = new SessionRun(randomUUID()) + run.actionId = options?.actionId ?? (mode === 'hands-free' ? 'hands-free' : 'dictation') run.captureSelection = windowContext !== null run.windowContext = windowContext this._run = run @@ -469,7 +487,7 @@ class VoiceModeService extends EventEmitter { // 핫키를 뗀 뒤라 복사 단축키가 안전하다 — LLM 이 쓸 것이 확실하면 전사와 병렬로 선택 텍스트를 // 캡처한다. 원문 삽입 경로(none·LLM 불가)에선 보내지 않는다: 쓰지도 않을 Ctrl+C 가 터미널에선 // 프로세스를 중단시킨다. 음성 단축키로 LLM 경로가 될 수도 있으면 LLM 단계에서 늦게 캡처한다. - if (routeUsesLlm(this._resolveLlmRoute(null))) this._startSelectionCapture(run) + if (routeUsesLlm(this._resolveLlmRoute(null, run.actionId))) this._startSelectionCapture(run) // 버퍼가 있으면 STT에 전달 if (run.audioBuffer.length > 0 && run.sttReady) { @@ -924,20 +942,30 @@ class VoiceModeService extends EventEmitter { return } - if (this._session) { - this._session.transcription = result.text + let rawText = result.text + try { + const { getDictionaryService } = await import('./DictionaryService') + const corrected = getDictionaryService().applyDictionaryCorrections(rawText) + rawText = corrected.text + } catch { + // DictionaryService 미초기화 시 무시 } - this.emit('transcription-update', { text: result.text, isFinal: true }) + if (this._session) { + this._session.transcription = rawText + } + + this.emit('transcription-update', { text: rawText, isFinal: true }) // Phase 10.5: 음성 단축키 — 키워드 매칭으로 LLM 명령어 자동 선택 - let effectiveText = result.text + // 'command' 핫키로 진입한 세션은 voiceCommandsEnabled 설정과 무관하게 음성 명령 매칭을 항상 수행한다. + let effectiveText = rawText let overrideInstructionId: string | null = null try { const { getVoiceCommandService } = await import('./VoiceCommandService') const vcSvc = getVoiceCommandService() - if (vcSvc.isEnabled()) { - const match = vcSvc.match(result.text) + if (run.actionId === 'command' || vcSvc.isEnabled()) { + const match = vcSvc.match(rawText) if (match.matched && match.instructionId) { effectiveText = match.cleanedText overrideInstructionId = match.instructionId @@ -952,7 +980,7 @@ class VoiceModeService extends EventEmitter { // 결정적 구어 군더더기·추임새·말바꾸기·중복·동음이의어 1차 정리 (LLM 유무와 무관하게 0ms 즉시 적용) effectiveText = cleanDictatedText(effectiveText) - const route = this._resolveLlmRoute(overrideInstructionId) + const route = this._resolveLlmRoute(overrideInstructionId, run.actionId) if (route.kind === 'insert-raw') { if (route.reason === 'llm-unavailable') { this.emit('error', { @@ -999,10 +1027,16 @@ class VoiceModeService extends EventEmitter { } /** 현재 설정으로 LLM 후처리 경로를 결정한다 (정책은 @d3ro/core/voice-llm-route 가 소유) */ - private _resolveLlmRoute(overrideInstructionId: string | null): VoiceLlmRoute { + private _resolveLlmRoute( + overrideInstructionId: string | null, + actionId: KeyBindingActionId | undefined = this._run?.actionId, + ): VoiceLlmRoute { const backend = configGet('llmBackend') + const configuredAction = configGet('defaultLLMAction') + const defaultAction = + actionId === 'command' && configuredAction === 'none' ? 'refine' : configuredAction return resolveVoiceLlmRoute({ - defaultAction: configGet('defaultLLMAction'), + defaultAction, overrideInstructionId, activeChainId: configGet('activeChainId') ?? null, activeInstructionId: configGet('activeInstructionId') ?? null, @@ -1073,19 +1107,33 @@ class VoiceModeService extends EventEmitter { } } - /** 지시문(음성 단축키 또는 활성 지시문)으로 처리한다. 지시문이 없으면 지시문 없이 custom 처리 */ + /** 지시문(음성 단축키 또는 활성 지시문·파이프라인)으로 처리한다. 지시문이 없으면 지시문 없이 custom 처리 */ private async _processWithInstruction(userText: string, instructionId: string | null): Promise { let invocationText = userText let instructionPrompt: string | undefined if (instructionId) { const { getCustomInstructionService } = await import('./CustomInstructionService') - const instruction = getCustomInstructionService().getById(instructionId) - if (instruction) { + const svc = getCustomInstructionService() + const ids = parseActiveInstructionIds(instructionId) + const matched = ids + .map((id) => svc.getById(id)) + .filter((item): item is NonNullable => item !== null) + + if (matched.length === 1) { + const instruction = matched[0] const invocation = buildInstructionInvocation(instruction.prompt, userText, resolveTargetLanguage()) invocationText = invocation.text instructionPrompt = invocation.systemPrompt logger.info(`Using custom instruction: "${instruction.name}"`) + } else if (matched.length >= 2) { + const composedPrompt = composeInstructionPipelinePrompt(matched) + const invocation = buildInstructionInvocation(composedPrompt, userText, resolveTargetLanguage()) + invocationText = invocation.text + instructionPrompt = invocation.systemPrompt + logger.info( + `Using custom instruction pipeline (${matched.length} steps): ${matched.map((i) => i.name).join(' → ')}`, + ) } else { logger.warn(`Custom instruction not found: ${instructionId} — processing without an instruction`) } @@ -1275,7 +1323,7 @@ class VoiceModeService extends EventEmitter { ) switch (intent.kind) { case 'start': - await this.startSession(intent.mode) + await this.startSession(intent.mode, { actionId: action.actionId }) break case 'stop': // Speakly 패턴: 딜레이 없이 즉시 stop (딜레이가 race condition 유발) diff --git a/apps/desktop/src/main/services/ads/AdMediationEngine.ts b/apps/desktop/src/main/services/ads/AdMediationEngine.ts index 541741c..05fda5f 100644 --- a/apps/desktop/src/main/services/ads/AdMediationEngine.ts +++ b/apps/desktop/src/main/services/ads/AdMediationEngine.ts @@ -22,6 +22,7 @@ import { PubMaticAdapter } from './PubMaticAdapter' import { MintegralAdapter } from './MintegralAdapter' import { DirectHouseSponsorAdapter } from './DirectHouseSponsorAdapter' import { getAdSettlementService } from './AdSettlementService' +import { getLicenseService } from '../LicenseService' export class AdMediationEngine { private static instance: AdMediationEngine | null = null @@ -30,6 +31,8 @@ export class AdMediationEngine { private impressionHistory: AdImpressionEvent[] = [] private deliveredCreatives = new Map() private clickedCreatives = new Set() + private claimedRewards = new Set() + private lastRewardClaimedAt = 0 private constructor() { // Provider shells stay registered for explicit configuration diagnostics, @@ -280,16 +283,116 @@ export class AdMediationEngine { getAdSettlementService().recordClick(networkId) } - public async claimReward(adId: string, networkId: string): Promise { - // Desktop mediation has no server-verified completion/nonce ledger. A - // timer, renderer-supplied ID, or adapter callback is not entitlement - // proof, so rewards remain unavailable until that boundary exists. - void adId - void networkId + public async reportRewardCompletion( + adId: string, + networkId: string, + nonce?: string, + ): Promise { + return this.claimReward(adId, networkId, nonce) + } + + public async claimReward( + adId: string, + networkId: string, + nonce?: string, + ): Promise { + const trimmedAdId = adId?.trim() + if (!trimmedAdId || !networkId) { + return { + success: false, + tokensAdded: 0, + newTotalQuota: 0, + } + } + + const delivered = this.deliveredCreatives.get(trimmedAdId) + const impression = this.impressionHistory.find((candidate) => candidate.adId === trimmedAdId) + if ( + delivered === undefined || + delivered.networkId !== networkId || + impression === undefined + ) { + return { + success: false, + tokensAdded: 0, + newTotalQuota: 0, + } + } + + const claimKey = nonce?.trim() ? `${trimmedAdId}:${nonce.trim()}` : trimmedAdId + if (this.claimedRewards.has(trimmedAdId) || this.claimedRewards.has(claimKey)) { + return { + success: false, + tokensAdded: 0, + newTotalQuota: 0, + } + } + + const now = Date.now() + const cooldownMs = Math.max(0, this.config.rewardCooldownSeconds * 1000) + if (this.lastRewardClaimedAt > 0 && now - this.lastRewardClaimedAt < cooldownMs) { + return { + success: false, + tokensAdded: 0, + newTotalQuota: 0, + nextAvailableAt: this.lastRewardClaimedAt + cooldownMs, + } + } + + const adapter = this.adapters.get(networkId) + if (!adapter?.reportRewardCompletion) { + return { + success: false, + tokensAdded: 0, + newTotalQuota: 0, + } + } + + const completion = await adapter + .reportRewardCompletion(trimmedAdId) + .catch(() => ({ success: false, tokenReward: 0 })) + if (!completion.success) { + return { + success: false, + tokensAdded: 0, + newTotalQuota: 0, + } + } + + const rawTokens = + completion.tokenReward > 0 + ? completion.tokenReward + : (delivered.rewardTokens ?? this.config.rewardTokensAmount) + const tokensToGrant = rawTokens > 0 ? rawTokens : this.config.rewardTokensAmount + if (tokensToGrant <= 0) { + return { + success: false, + tokensAdded: 0, + newTotalQuota: 0, + } + } + + const grantResult = getLicenseService().grantRewardQuota(tokensToGrant, claimKey) + if (!grantResult.granted) { + return { + success: false, + tokensAdded: 0, + newTotalQuota: 0, + } + } + + this.claimedRewards.add(trimmedAdId) + this.claimedRewards.add(claimKey) + this.lastRewardClaimedAt = now + impression.completed = true + getAdSettlementService().recordCompletion(networkId) + return { - success: false, - tokensAdded: 0, - newTotalQuota: 0, + success: true, + tokensAdded: grantResult.tokensAdded, + newTotalQuota: grantResult.newTotalQuota, + nextAvailableAt: now + cooldownMs, + rewardId: grantResult.rewardId, } } diff --git a/apps/desktop/src/main/services/license/license-store.ts b/apps/desktop/src/main/services/license/license-store.ts index 93bfc8d..65cbbd6 100644 --- a/apps/desktop/src/main/services/license/license-store.ts +++ b/apps/desktop/src/main/services/license/license-store.ts @@ -25,6 +25,10 @@ export type LicenseStoreField = // cloud entitlement | 'licenseCloudTier' | 'licenseCloudVerifiedAt' + // rewarded ad bonus quota (daily + replay nonces) + | 'licenseRewardBonusDate' + | 'licenseRewardBonusAmount' + | 'licenseRewardClaimedNonces' // combined view (kept for older app versions that read the same file) | 'licenseTier' | 'licenseLastVerifiedAt' diff --git a/apps/desktop/src/main/services/llm-prompts.ts b/apps/desktop/src/main/services/llm-prompts.ts index fd638e5..2d7fd59 100644 --- a/apps/desktop/src/main/services/llm-prompts.ts +++ b/apps/desktop/src/main/services/llm-prompts.ts @@ -2,6 +2,7 @@ // LLM 시스템 프롬프트 SSOT — PremiumLLMService 공용. import { getLogger } from './LoggerService' +import * as ConfigServiceModule from './ConfigService' import type { LLMAction } from '@d3ro/core/types' import { fillInstructionPlaceholders, @@ -52,23 +53,49 @@ const BASE_SYSTEM_PROMPTS: Record = { /** * 번역 대상 언어 기본값. - * `AppConfig`에는 번역 대상 언어 키가 없다 (`language`는 UI 로케일, - * `sttLanguage`는 입력 언어라 둘 다 대상 언어가 아니다). */ export const DEFAULT_TARGET_LANGUAGE = 'English' +/** + * 설정 UI에서 고를 수 있는 번역 대상 언어 목록. + * 프롬프트(`{{targetLanguage}}로 번역해주세요`)에 자연스럽게 들어가도록 명확한 언어 이름을 쓴다. + */ +export const SUPPORTED_TARGET_LANGUAGES = [ + 'English', + 'Korean', + 'Japanese', + 'Chinese (Simplified)', + 'Chinese (Traditional)', + 'Spanish', + 'French', + 'German', + 'Portuguese', + 'Vietnamese', + 'Indonesian', + 'Arabic', +] as const + +export type SupportedTargetLanguage = (typeof SUPPORTED_TARGET_LANGUAGES)[number] + /** * 번역 대상 언어를 해석한다. LLM을 호출하는 모든 경로가 이 함수를 쓴다. * - * `AppConfig`에는 번역 대상 언어 키가 없다 — `language`는 UI 로케일이고 - * `sttLanguage`는 입력(원문) 언어라 어느 쪽도 대상 언어로 쓸 수 없다. - * (입력 언어로 번역하면 원문 그대로가 되고, UI 로케일은 `'ko'` 같은 코드라 - * 프롬프트에 그대로 넣으면 문장이 깨진다.) - * - * 따라서 현재는 {@link DEFAULT_TARGET_LANGUAGE} 고정이다. 사용자가 대상 언어를 - * 고를 수 있으려면 설정 키가 필요하다. + * 명시 인자가 있으면 우선하고, 없으면 `AppConfig.targetLanguage` 설정값을 읽는다. + * 설정 서비스가 초기화되지 않았거나 값이 비어 있으면 {@link DEFAULT_TARGET_LANGUAGE}로 폴백한다. */ -export function resolveTargetLanguage(): string { +export function resolveTargetLanguage(explicit?: string | null): string { + if (typeof explicit === 'string' && explicit.trim().length > 0) { + return explicit.trim() + } + if (Object.keys(ConfigServiceModule).includes('getConfigService')) { + const store = ConfigServiceModule.getConfigService() + if (store) { + const configured = store.get('targetLanguage') + if (typeof configured === 'string' && configured.trim().length > 0) { + return configured.trim() + } + } + } return DEFAULT_TARGET_LANGUAGE } @@ -105,7 +132,7 @@ function warnUnresolved(unresolved: readonly string[]): void { function renderWithDefaults(prompt: string, vars: InstructionVars): RenderedInstruction { const rendered = renderInstruction(prompt, { text: vars.text, - targetLanguage: vars.targetLanguage ?? DEFAULT_TARGET_LANGUAGE, + targetLanguage: resolveTargetLanguage(vars.targetLanguage), }) // 조용히 새어나간 플레이스홀더가 LLM에 그대로 전달되는 사고가 있었다. warnUnresolved(rendered.unresolved) @@ -120,7 +147,7 @@ export function renderInstructionPrompt(prompt: string, vars: InstructionVars): warnUnresolved(findUnresolvedPlaceholders(prompt)) return fillInstructionPlaceholders(prompt, { text: vars.text, - targetLanguage: vars.targetLanguage ?? DEFAULT_TARGET_LANGUAGE, + targetLanguage: resolveTargetLanguage(vars.targetLanguage), }) } @@ -176,7 +203,7 @@ export function resolveSystemPrompt( if (action === 'translate') { return BASE_SYSTEM_PROMPTS.translate.replace( '{{targetLanguage}}', - targetLanguage ?? DEFAULT_TARGET_LANGUAGE, + resolveTargetLanguage(targetLanguage), ) } return BASE_SYSTEM_PROMPTS[action] ?? BASE_SYSTEM_PROMPTS[CUSTOM_FALLBACK_ACTION] diff --git a/apps/desktop/src/main/services/stt/STTManager.ts b/apps/desktop/src/main/services/stt/STTManager.ts index 56b9396..ee19f0f 100644 --- a/apps/desktop/src/main/services/stt/STTManager.ts +++ b/apps/desktop/src/main/services/stt/STTManager.ts @@ -49,8 +49,8 @@ export const STT_PROVIDERS_META: STTProviderInfo[] = [ { id: 'local', name: 'Local Whisper', - description: '100% 완전 오프라인, 무료, 프라이버시 보호', - badge: 'Offline · Free', + description: 'Local faster-whisper inference on device', + badge: 'Local · On-Device', requiresApiKey: false, defaultModel: 'large-v3-turbo', models: ['tiny', 'base', 'small', 'medium', 'large-v3', 'large-v3-turbo'], @@ -59,8 +59,8 @@ export const STT_PROVIDERS_META: STTProviderInfo[] = [ { id: 'd3ro-cloud', name: 'D3RO Cloud STT (Managed)', - description: 'D3RO 클라우드 매니지드 전사 서비스 (키 설정 불필요)', - badge: 'Cloud · Zero Config', + description: 'Managed cloud speech-to-text gateway', + badge: 'Cloud · Managed', requiresApiKey: false, defaultModel: 'default', defaultBaseUrl: 'http://127.0.0.1:5000', @@ -70,8 +70,8 @@ export const STT_PROVIDERS_META: STTProviderInfo[] = [ { id: 'openai', name: 'OpenAI Whisper', - description: '공식 OpenAI 고품질 다국어 음성 인식', - badge: 'Standard · High Accuracy', + description: 'OpenAI Audio Transcriptions API', + badge: 'Cloud · OpenAI API', requiresApiKey: true, defaultModel: 'whisper-1', defaultBaseUrl: 'https://api.openai.com/v1', @@ -81,8 +81,8 @@ export const STT_PROVIDERS_META: STTProviderInfo[] = [ { id: 'groq', name: 'Groq Whisper LPU', - description: 'LPU 가속 기반 초저지연(~200ms) 초고속 인식', - badge: 'Ultra Fast (~200ms)', + description: 'Groq LPU-hosted Whisper API', + badge: 'Cloud · Groq LPU', requiresApiKey: true, defaultModel: 'whisper-large-v3-turbo', defaultBaseUrl: 'https://api.groq.com/openai/v1', @@ -92,8 +92,8 @@ export const STT_PROVIDERS_META: STTProviderInfo[] = [ { id: 'deepgram', name: 'Deepgram Nova-3', - description: '업계 최고 인식률, 스마트 문장 부호 및 포맷팅', - badge: 'Industry Benchmark', + description: 'Deepgram Nova speech recognition API', + badge: 'Cloud · Deepgram API', requiresApiKey: true, defaultModel: 'nova-3', defaultBaseUrl: 'https://api.deepgram.com', @@ -103,8 +103,8 @@ export const STT_PROVIDERS_META: STTProviderInfo[] = [ { id: 'assemblyai', name: 'AssemblyAI Universal-2', - description: '고도화된 음향 모델 및 문맥 인식 STT', - badge: 'Advanced Acoustic', + description: 'AssemblyAI asynchronous speech-to-text API', + badge: 'Cloud · AssemblyAI API', requiresApiKey: true, defaultModel: 'best', defaultBaseUrl: 'https://api.assemblyai.com/v2', @@ -114,8 +114,8 @@ export const STT_PROVIDERS_META: STTProviderInfo[] = [ { id: 'google', name: 'Google Gemini 2.0 Flash / Cloud STT', - description: 'Gemini 2.0 Flash 기반 한국어/다국어 인식', - badge: 'Multilingual · Asian Languages', + description: 'Google Generative Language multimodal audio API', + badge: 'Cloud · Gemini Audio', requiresApiKey: true, defaultModel: 'gemini-2.0-flash', defaultBaseUrl: 'https://generativelanguage.googleapis.com/v1beta', @@ -124,9 +124,9 @@ export const STT_PROVIDERS_META: STTProviderInfo[] = [ }, { id: 'custom', - name: 'Custom (OpenAI 호환)', - description: 'vLLM, 사내 프라이빗 서버, Together 등 커스텀 엔드포인트', - badge: 'Self-Hosted / Proxy', + name: 'Custom (OpenAI Compatible)', + description: 'Custom OpenAI-compatible audio transcriptions endpoint', + badge: 'Self-Hosted · Proxy', requiresApiKey: false, defaultModel: 'whisper-1', defaultBaseUrl: 'http://127.0.0.1:8000/v1', diff --git a/apps/desktop/src/main/services/text-insert/nut-keyboard-port.ts b/apps/desktop/src/main/services/text-insert/nut-keyboard-port.ts index 5539415..28bd7d2 100644 --- a/apps/desktop/src/main/services/text-insert/nut-keyboard-port.ts +++ b/apps/desktop/src/main/services/text-insert/nut-keyboard-port.ts @@ -62,7 +62,7 @@ export interface NutKeyboardModule { hasKeyboard(): boolean getKeyboard(): NutNativeKeyboard } - Key: Pick + Key: Pick } export type NutModuleLoader = () => Promise diff --git a/apps/desktop/src/main/services/voice-intent.ts b/apps/desktop/src/main/services/voice-intent.ts index c2b213f..5a0b73c 100644 --- a/apps/desktop/src/main/services/voice-intent.ts +++ b/apps/desktop/src/main/services/voice-intent.ts @@ -59,6 +59,9 @@ export function resolveVoiceIntent(gesture: VoiceGesture, phase: VoiceIntentPhas if (gesture.type === 'press') { if (gesture.mode === 'dictation') { + if (!gesture.holdMode) { + return phase === 'idle' ? { kind: 'start', mode: 'dictation' } : STOP + } return phase === 'idle' ? { kind: 'start', mode: 'dictation' } : IGNORE } return phase === 'idle' ? { kind: 'start', mode: 'hands-free' } : STOP @@ -75,11 +78,9 @@ export function resolveGestureMode(actionId: KeyBindingActionId, isDoublePress: } /** - * 'command' 액션에는 아직 전용 핸들러가 없다. - * 현행 동작대로 dictation 파이프라인으로 fallback 하며, 그 경로는 hold-to-talk 이므로 - * KEYBINDING_ACTIONS 의 holdMode(false)가 아니라 dictation 과 같은 값을 쓴다. + * 키바인딩 스펙의 holdMode 설정을 그대로 존중한다. + * 'command' 액션도 스펙/설정에 따라 토글(holdMode=false) 또는 홀드(holdMode=true)로 동작한다. */ -export function resolveGestureHoldMode(actionId: KeyBindingActionId, specHoldMode: boolean): boolean { - if (actionId === 'command') return true +export function resolveGestureHoldMode(_actionId: KeyBindingActionId, specHoldMode: boolean): boolean { return specHoldMode } diff --git a/apps/desktop/src/main/utils/inference-cpu-budget.ts b/apps/desktop/src/main/utils/inference-cpu-budget.ts new file mode 100644 index 0000000..ac84793 --- /dev/null +++ b/apps/desktop/src/main/utils/inference-cpu-budget.ts @@ -0,0 +1,21 @@ +import { availableParallelism } from 'node:os' + +/** Leave scheduling headroom for the foreground app and IME; this is a worker cap, not CPU affinity. */ +export function inferenceCpuThreads(availableCpus: number): number { + if (!Number.isFinite(availableCpus) || availableCpus < 1) return 1 + return Math.max(1, Math.min(4, Math.floor(availableCpus) - 2)) +} + +/** A remote Ollama host has a different CPU budget; only constrain the local engine. */ +export function localInferenceCpuOptions(serverUrl: string): { num_thread?: number } { + try { + const hostname = new URL(serverUrl).hostname.toLowerCase() + if ( + hostname !== 'localhost' && hostname !== 'localhost.' && hostname !== '[::1]' && + !/^127\.\d+\.\d+\.\d+$/.test(hostname) + ) return {} + return { num_thread: inferenceCpuThreads(availableParallelism()) } + } catch { + return {} + } +} diff --git a/apps/desktop/src/main/utils/win32-foreground.ts b/apps/desktop/src/main/utils/win32-foreground.ts index 21e139a..b5578a1 100644 --- a/apps/desktop/src/main/utils/win32-foreground.ts +++ b/apps/desktop/src/main/utils/win32-foreground.ts @@ -79,9 +79,17 @@ const IME_CMODE_NATIVE = 0x0001 const SMTO_ABORTIFHUNG = 0x0002 const TITLE_BUFFER_CHARS = 512 const PATH_BUFFER_CHARS = 1024 +const PROCESS_PATH_CACHE_MAX = 256 +const IME_MODE_CACHE_TTL_MS = 400 let api: Win32Api | null = null let loadFailed = false +const processPathCache = new Map() +let imeModeCache: { hwnd: number; korean: boolean | null; at: number } | null = null + +export function invalidateKoreanImeCache(): void { + imeModeCache = null +} function loadApi(): Win32Api | null { if (api) return api @@ -188,27 +196,38 @@ export function getForegroundWindowInfo(): ForegroundWindowInfo | null { : null let appPath: string | null = null - const processHandle = a.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, processId) as - | bigint - | number - | null - if (processHandle) { - try { - const sizeBuffer = Buffer.alloc(4) - sizeBuffer.writeUInt32LE(PATH_BUFFER_CHARS, 0) - const pathBuffer = Buffer.alloc(PATH_BUFFER_CHARS * 2) - const pathOk = a.QueryFullProcessImageNameW( - processHandle, - 0, - pathBuffer, - sizeBuffer - ) as boolean - if (pathOk) { - const length = sizeBuffer.readUInt32LE(0) - appPath = a.koffi.decode(pathBuffer, 'char16_t', length).replace(/\0.*$/su, '') + if (processId > 0 && processPathCache.has(processId)) { + appPath = processPathCache.get(processId) ?? null + } else if (processId > 0) { + const processHandle = a.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, processId) as + | bigint + | number + | null + if (processHandle) { + try { + const sizeBuffer = Buffer.alloc(4) + sizeBuffer.writeUInt32LE(PATH_BUFFER_CHARS, 0) + const pathBuffer = Buffer.alloc(PATH_BUFFER_CHARS * 2) + const pathOk = a.QueryFullProcessImageNameW( + processHandle, + 0, + pathBuffer, + sizeBuffer + ) as boolean + if (pathOk) { + const length = sizeBuffer.readUInt32LE(0) + appPath = a.koffi.decode(pathBuffer, 'char16_t', length).replace(/\0.*$/su, '') + } + } finally { + a.CloseHandle(processHandle) } - } finally { - a.CloseHandle(processHandle) + } + if (appPath) { + if (processPathCache.size >= PROCESS_PATH_CACHE_MAX) { + const firstKey = processPathCache.keys().next().value + if (firstKey !== undefined) processPathCache.delete(firstKey) + } + processPathCache.set(processId, appPath) } } @@ -241,6 +260,11 @@ export function isForegroundKoreanIme(foregroundHwnd?: number | null): boolean | const fg = Number(rawFg ?? 0) if (!Number.isFinite(fg) || fg <= 0) return null + const now = Date.now() + if (imeModeCache && imeModeCache.hwnd === fg && now - imeModeCache.at < IME_MODE_CACHE_TTL_MS) { + return imeModeCache.korean + } + const pidBuffer = Buffer.alloc(4) const tid = Number(a.GetWindowThreadProcessId(fg, pidBuffer) ?? 0) let targetHwnd = fg @@ -268,7 +292,10 @@ export function isForegroundKoreanIme(foregroundHwnd?: number | null): boolean | } const imeWnd = Number((a.ImmGetDefaultIMEWnd(targetHwnd) as bigint | number | null) ?? 0) - if (!Number.isFinite(imeWnd) || imeWnd <= 0) return null + if (!Number.isFinite(imeWnd) || imeWnd <= 0) { + imeModeCache = { hwnd: fg, korean: null, at: now } + return null + } const convOut = [0] const convRes = a.SendMessageTimeoutW( @@ -277,13 +304,18 @@ export function isForegroundKoreanIme(foregroundHwnd?: number | null): boolean | IMC_GETCONVERSIONMODE, 0, SMTO_ABORTIFHUNG, - 30, + 8, convOut ) - if (Number(convRes) === 0) return null + if (Number(convRes) === 0) { + imeModeCache = { hwnd: fg, korean: null, at: now } + return null + } // Windows 한국어 IME(TSF/Chromium 포함)는 한/영('가'/'A') 상태를 IMC_GETOPENSTATUS 가 아니라 // IMC_GETCONVERSIONMODE 의 IME_CMODE_NATIVE(0x0001) 비트로 나타낸다. - return (Number(convOut[0]) & IME_CMODE_NATIVE) !== 0 + const korean = (Number(convOut[0]) & IME_CMODE_NATIVE) !== 0 + imeModeCache = { hwnd: fg, korean, at: now } + return korean } catch { return null } @@ -293,4 +325,6 @@ export function isForegroundKoreanIme(foregroundHwnd?: number | null): boolean | export function resetWin32ForegroundForTests(): void { api = null loadFailed = false + processPathCache.clear() + imeModeCache = null } \ No newline at end of file diff --git a/apps/desktop/src/main/utils/win32-input-activity.ts b/apps/desktop/src/main/utils/win32-input-activity.ts new file mode 100644 index 0000000..4c4c747 --- /dev/null +++ b/apps/desktop/src/main/utils/win32-input-activity.ts @@ -0,0 +1,36 @@ +import { getLogger } from '../services/LoggerService' + +const logger = getLogger('win32-input-activity') + +// LASTINPUTINFO is two DWORDs on both 32-bit and 64-bit Windows. +const LAST_INPUT_INFO_SIZE = 8 +type GetLastInputInfoFn = (info: Buffer) => boolean + +let getLastInputInfo: GetLastInputInfoFn | null = null +let loadFailed = false + +/** + * Session input token read directly from Windows, independent of queued JS hook callbacks. + * dwTime is a system tick, not an epoch timestamp or monotonic sequence: compare equality only. + * Unsupported platforms or unavailable native state return null; callers must not infer idle. + */ +export function getLastNativeInputTick(): number | null { + if (process.platform !== 'win32' || loadFailed) return null + try { + if (!getLastInputInfo) { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const koffi = require('koffi') as { + load: (library: string) => { func: (signature: string) => GetLastInputInfoFn } + } + getLastInputInfo = koffi.load('user32.dll').func('bool GetLastInputInfo(_Inout_ void *plii)') + } + const info = Buffer.alloc(LAST_INPUT_INFO_SIZE) + info.writeUInt32LE(LAST_INPUT_INFO_SIZE, 0) + if (!getLastInputInfo(info)) return null + return info.readUInt32LE(4) + } catch (error) { + loadFailed = true + logger.warn(`Windows 입력 시각 조회 실패: ${error instanceof Error ? error.message : String(error)}`) + return null + } +} diff --git a/apps/desktop/src/main/utils/win32-unicode-input.ts b/apps/desktop/src/main/utils/win32-unicode-input.ts index c46616b..659fa24 100644 --- a/apps/desktop/src/main/utils/win32-unicode-input.ts +++ b/apps/desktop/src/main/utils/win32-unicode-input.ts @@ -12,6 +12,7 @@ // IME 상태를 타지 않고 클립보드도 전혀 건드리지 않는다. import { getLogger } from '../services/LoggerService' +import { D3ROError, ErrorCode } from '@d3ro/core/errors' const logger = getLogger('win32-unicode-input') @@ -110,9 +111,30 @@ function writeUnicodeKeyPair(buffer: Buffer, pairIndex: number, codeUnit: number buffer.writeUInt32LE(KEYEVENTF_UNICODE | KEYEVENTF_KEYUP, upBase + unionOffset + 4) } +/** Only zero inserted events are safe to retry through another input backend. */ +function sendInputBatch(sendInput: SendInputFn, totalEvents: number, buffer: Buffer): boolean { + let sent: number + try { + sent = sendInput(totalEvents, buffer, inputStructSize) + } catch (error) { + // A native-call exception does not prove that the input stream was untouched. + throw new D3ROError( + ErrorCode.TextInsertKeySimulationFailed, + `SendInput failed with an unknown insertion result; retry blocked: ${error instanceof Error ? error.message : String(error)}` + ) + } + if (sent === totalEvents) return true + if (sent === 0) return false + // Replaying a partially applied batch can delete the user's next word or duplicate text. + throw new D3ROError( + ErrorCode.TextInsertKeySimulationFailed, + `SendInput inserted ${sent}/${totalEvents} events; retry blocked to preserve existing input` + ) +} + /** * UTF-16 문자열을 Windows SendInput(KEYEVENTF_UNICODE) 호출 한 번으로 입력 큐에 넣는다. - * 성공 시 true, 지원하지 않거나 실패 시 false 를 반환한다. + * 성공 시 true, 미지원 또는 입력 0건이면 false. 일부 입력/결과 불명은 재시도하지 않도록 예외를 던진다. */ export function sendWin32UnicodeText(text: string): boolean { if (!text) return true @@ -128,9 +150,9 @@ export function sendWin32UnicodeText(text: string): boolean { writeUnicodeKeyPair(buffer, i, text.charCodeAt(i)) } - const sent = sendInput(totalEvents, buffer, inputStructSize) - return sent === totalEvents + return sendInputBatch(sendInput, totalEvents, buffer) } catch (error) { + if (error instanceof D3ROError) throw error logger.warn(`SendInput(KEYEVENTF_UNICODE) 실행 실패: ${error instanceof Error ? error.message : String(error)}`) return false } @@ -138,10 +160,11 @@ export function sendWin32UnicodeText(text: string): boolean { /** * (선택적 IME 조합 확정 Space) + 커서 앞 백스페이스 N번 + UTF-16 문자열 입력을 - * 단 한 번의 Windows SendInput 호출로 원자적(atomic)으로 주입한다. + * 단 한 번의 Windows SendInput 호출로 입력 큐에 연속해서 주입한다. * * Win32 SendInput 규약상 단일 호출 안의 INPUT 배열 사이에 사용자의 물리 키 입력이 끼어들 수 없으므로, - * 빠른 타이핑 도중에도 지우기와 다시 쓰기가 경쟁하지 않고 < 0.5ms 안에 끝난다. + * 배치 내부의 순서만 보장한다. 이전 스냅샷 이후 바뀐 커서/텍스트, 대상 앱의 처리 시간, + * IME 상태까지 보장하지 않는다. 일부 입력만 성공하면 삭제/입력 배치를 재실행하지 않는다. */ export function sendWin32UnicodeReplacement( deleteCount: number, @@ -177,9 +200,9 @@ export function sendWin32UnicodeReplacement( pairCursor += 1 } - const sent = sendInput(totalEvents, buffer, inputStructSize) - return sent === totalEvents + return sendInputBatch(sendInput, totalEvents, buffer) } catch (error) { + if (error instanceof D3ROError) throw error logger.warn( `SendInput(atomic replacement) 실행 실패: ${error instanceof Error ? error.message : String(error)}` ) diff --git a/apps/desktop/src/preload/index.ts b/apps/desktop/src/preload/index.ts index fd6186f..4c4dfe6 100644 --- a/apps/desktop/src/preload/index.ts +++ b/apps/desktop/src/preload/index.ts @@ -80,6 +80,7 @@ import type { DictionaryImportParams, DictionaryImportResult, DictionaryExportParams, + CustomInstruction, StatsSummary, PermissionStatus, // Phase 10 @@ -411,10 +412,24 @@ const electronAPI = { // ── History ──────────────────────────────────────────── history: { - getAll: (params: HistoryQueryParams) => - invoke(IPC_CHANNELS.HISTORY.GET_ALL, params), + getAll: ( + params: HistoryQueryParams & { + mode?: string | null + isFavorite?: boolean + tag?: string | null + search?: string | null + query?: string | null + beforeCreatedAt?: number | null + }, + ) => invoke(IPC_CHANNELS.HISTORY.GET_ALL, params), getById: (params: HistoryGetByIdParams) => invoke(IPC_CHANNELS.HISTORY.GET_BY_ID, params), + update: (params: { + id: string + title?: string | null + polishedText?: string | null + originalText?: string + }) => invoke('history:update', params), delete: (params: HistoryDeleteParams) => invoke(IPC_CHANNELS.HISTORY.DELETE, params), deleteAll: () => invoke(IPC_CHANNELS.HISTORY.DELETE_ALL), @@ -477,14 +492,14 @@ const electronAPI = { // ── Instruction (커스텀 명령어) ──────────────────────── instruction: { - getAll: () => invoke('instruction:getAll'), - getById: (params: { id: string }) => invoke('instruction:getById', params), + getAll: () => invoke(IPC_CHANNELS.INSTRUCTION.GET_ALL), + getById: (params: { id: string }) => invoke(IPC_CHANNELS.INSTRUCTION.GET_BY_ID, params), create: (params: { name: string; description: string; prompt: string }) => - invoke('instruction:create', params), + invoke(IPC_CHANNELS.INSTRUCTION.CREATE, params), update: (params: { id: string; name?: string; description?: string; prompt?: string }) => - invoke('instruction:update', params), - delete: (params: { id: string }) => invoke('instruction:delete', params), - reorder: (params: { ids: string[] }) => invoke('instruction:reorder', params), + invoke(IPC_CHANNELS.INSTRUCTION.UPDATE, params), + delete: (params: { id: string }) => invoke(IPC_CHANNELS.INSTRUCTION.DELETE, params), + reorder: (params: { ids: string[] }) => invoke(IPC_CHANNELS.INSTRUCTION.REORDER, params), }, // ── App 이벤트 (실시간 UI 갱신) ──────────────────────── @@ -505,7 +520,16 @@ const electronAPI = { removeTag: (params: RemoveTagParams) => invoke(IPC_CHANNELS.MEMO.REMOVE_TAG, params), getAllTags: () => invoke(IPC_CHANNELS.MEMO.GET_ALL_TAGS), searchByTag: (params: SearchByTagParams) => invoke(IPC_CHANNELS.MEMO.SEARCH_BY_TAG, params), - export: (params: ExportMemoParams) => invoke(IPC_CHANNELS.MEMO.EXPORT, params), + export: ( + params: ExportMemoParams & { + savePath?: string + interactive?: boolean + includeUntagged?: boolean + mode?: string + isFavorite?: boolean + search?: string + }, + ) => invoke(IPC_CHANNELS.MEMO.EXPORT, params), }, // ── Phase 10: Voice Commands ──────────────────────────── @@ -711,6 +735,12 @@ const electronAPI = { ), finishListening: () => invoke('voiceConversation:finishListening'), + replayMessage: (params?: { messageId?: string; text?: string } | string) => + invoke( + (IPC_CHANNELS.VOICE_CONVERSATION as Record).REPLAY_MESSAGE ?? + 'voiceConversation:replayMessage', + params, + ), onStateChanged: (cb: (data: ConversationSessionInfo) => void): Unsubscribe => on(IPC_CHANNELS.VOICE_CONVERSATION.STATE_CHANGED, cb), onUserMessage: (cb: (data: ConversationMessage) => void): Unsubscribe => @@ -737,7 +767,7 @@ const electronAPI = { invoke('meetingMode:forceReset'), stopRecording: () => invoke(IPC_CHANNELS.MEETING_MODE.STOP_RECORDING), - addMemo: (params: MeetingAddMemoParams) => + addMemo: (params: MeetingAddMemoParams & { sessionId?: string; meetingId?: string }) => invoke(IPC_CHANNELS.MEETING_MODE.ADD_MEMO, params), getState: () => invoke(IPC_CHANNELS.MEETING_MODE.GET_STATE), diff --git a/apps/desktop/src/renderer/components/AppLayout.tsx b/apps/desktop/src/renderer/components/AppLayout.tsx index 9f07768..b225cfe 100644 --- a/apps/desktop/src/renderer/components/AppLayout.tsx +++ b/apps/desktop/src/renderer/components/AppLayout.tsx @@ -94,6 +94,9 @@ function Shell(): React.ReactElement { const handleOpenLicenseModal = (): void => setLicenseModalOpen(true) window.addEventListener('d3ro:open-license-modal', handleOpenLicenseModal) + const handleOpenRewardedModal = (): void => setRewardedModalOpen(true) + window.addEventListener('d3ro:open-rewarded-modal', handleOpenRewardedModal) + // detail.tab 은 타입 없는 DOM 경계라 검증한다 — 모르는 값이면 기본 탭(빈 설정 화면 방지) const handleOpenSettings = (e: Event): void => { const tab = readOpenSettingsTab(e) @@ -128,6 +131,7 @@ function Shell(): React.ReactElement { unsubUpgradeReq() unsubVoiceError() window.removeEventListener('d3ro:open-license-modal', handleOpenLicenseModal) + window.removeEventListener('d3ro:open-rewarded-modal', handleOpenRewardedModal) window.removeEventListener(OPEN_SETTINGS_EVENT, handleOpenSettings) window.removeEventListener('d3ro:open-ollama-guide', handleOpenGuide) } diff --git a/apps/desktop/src/renderer/components/OllamaGuideModal.tsx b/apps/desktop/src/renderer/components/OllamaGuideModal.tsx index deff26c..2349c31 100644 --- a/apps/desktop/src/renderer/components/OllamaGuideModal.tsx +++ b/apps/desktop/src/renderer/components/OllamaGuideModal.tsx @@ -41,7 +41,7 @@ interface RecommendedModel { } const RECOMMENDED_MODELS: readonly RecommendedModel[] = [ - { id: 'gemma4:e4b', name: 'Gemma 4 (E4B)', size: '4 GB', descriptionKey: 'ollamaGuide.model.gemma4e4b', recommended: true }, + { id: 'gemma4:e4b', name: 'Gemma 4 (E4B)', size: '4.0 GB', descriptionKey: 'ollamaGuide.model.gemma4e4b', recommended: true }, { id: 'llama3.2:3b', name: 'Llama 3.2 (3B)', size: '2.0 GB', descriptionKey: 'ollamaGuide.model.llama32' }, { id: 'qwen2.5:3b', name: 'Qwen 2.5 (3B)', size: '1.9 GB', descriptionKey: 'ollamaGuide.model.qwen25' }, { id: 'phi4', name: 'Phi 4', size: '9.1 GB', descriptionKey: 'ollamaGuide.model.phi4' }, diff --git a/apps/desktop/src/renderer/components/SettingsModal.tsx b/apps/desktop/src/renderer/components/SettingsModal.tsx index bf56b01..2691f04 100644 --- a/apps/desktop/src/renderer/components/SettingsModal.tsx +++ b/apps/desktop/src/renderer/components/SettingsModal.tsx @@ -823,6 +823,33 @@ export function SettingsModal({ open, initialTab = DEFAULT_SETTINGS_TAB, onClose } /> + + + + } + /> void readOnly?: boolean speaker?: string + highlighted?: boolean } function formatTimestamp(ms: number): string { @@ -48,6 +49,7 @@ export function EditableSegment({ onEdit, readOnly = false, speaker, + highlighted = false, }: EditableSegmentProps): React.ReactElement { const { t } = useI18n() const [editing, setEditing] = useState(false) @@ -168,13 +170,21 @@ export function EditableSegment({ return ( void +} + +export type CitationSegmentPart = + | { type: 'text'; value: string } + | { type: 'citation'; label: string; timestampMs: number } + +export function parseCitationTimestampMs(token: string): number | null { + const match = /^\[?(?:(\d{1,2}):)?(\d{1,3}):(\d{2})\]?$/.exec(token.trim()) + if (!match) return null + const hours = match[1] ? parseInt(match[1], 10) : 0 + const minutes = parseInt(match[2], 10) + const seconds = parseInt(match[3], 10) + if (seconds >= 60) return null + return (hours * 3600 + minutes * 60 + seconds) * 1000 +} + +export function splitTextWithCitations(text: string): CitationSegmentPart[] { + const regex = /\[(?:\d{1,2}:)?\d{1,3}:\d{2}\]/g + const parts: CitationSegmentPart[] = [] + let lastIndex = 0 + let match: RegExpExecArray | null + + while ((match = regex.exec(text)) !== null) { + const label = match[0] + const timestampMs = parseCitationTimestampMs(label) + if (timestampMs === null) continue + if (match.index > lastIndex) { + parts.push({ type: 'text', value: text.slice(lastIndex, match.index) }) + } + parts.push({ type: 'citation', label, timestampMs }) + lastIndex = match.index + label.length + } + + if (lastIndex < text.length) { + parts.push({ type: 'text', value: text.slice(lastIndex) }) + } + + return parts.length > 0 ? parts : [{ type: 'text', value: text }] +} + +export function renderChildrenWithCitations( + children: React.ReactNode, + onCitationClick?: (timestampMs: number, label: string) => void, +): React.ReactNode { + return React.Children.map(children, (child, index) => { + if (typeof child !== 'string') return child + const parts = splitTextWithCitations(child) + if (parts.length === 1 && parts[0].type === 'text') return child + return ( + + {parts.map((part, partIdx) => { + if (part.type === 'text') return part.value + return ( + ) => { + e.stopPropagation() + onCitationClick?.(part.timestampMs, part.label) + }} + sx={{ + display: 'inline-flex', + alignItems: 'center', + fontFamily: d3roFontMono, + fontSize: d3roTypo.small.size, + color: d3roPalette.accent.light, + bgcolor: d3roPalette.surface.overlaySoft, + border: `1px solid ${d3roPalette.border.default}`, + borderRadius: d3roRadius.xs, + px: 0.6, + py: 0.1, + mx: 0.25, + cursor: onCitationClick ? 'pointer' : 'default', + fontVariantNumeric: 'tabular-nums', + verticalAlign: 'baseline', + '&:hover': onCitationClick + ? { + bgcolor: d3roPalette.surface.overlayStrong, + borderColor: d3roPalette.accent.main, + } + : {}, + '&:focus-visible': d3roFocusRing, + }} + > + {part.label} + + ) + })} + + ) + }) } /** @@ -208,10 +303,44 @@ const components: Components = { ), } -export function MarkdownRenderer({ content }: MarkdownRendererProps): React.ReactElement { +export function MarkdownRenderer({ content, onCitationClick }: MarkdownRendererProps): React.ReactElement { + const customComponents = React.useMemo( + () => ({ + ...components, + p: ({ children }) => ( + + {renderChildrenWithCitations(children, onCitationClick)} + + ), + li: ({ children }) => ( + + {renderChildrenWithCitations(children, onCitationClick)} + + ), + }), + [onCitationClick], + ) + return ( - + {content} diff --git a/apps/desktop/src/renderer/components/meeting/MeetingDetailTabs.tsx b/apps/desktop/src/renderer/components/meeting/MeetingDetailTabs.tsx index efdb699..ea0e848 100644 --- a/apps/desktop/src/renderer/components/meeting/MeetingDetailTabs.tsx +++ b/apps/desktop/src/renderer/components/meeting/MeetingDetailTabs.tsx @@ -109,6 +109,11 @@ export function MeetingDetailTabs({ const [templates, setTemplates] = useState([]) const [tab, setTab] = useState('summary') + const [highlightedTimestampMs, setHighlightedTimestampMs] = useState(null) + const handleJumpToTimestamp = useCallback((timestampMs: number) => { + setHighlightedTimestampMs(timestampMs) + setTab('transcript') + }, []) // 문서 탭에서 고른 문서 — 사라지면 렌더링 시점에 첫 문서로 맞춘다. const [pickedDocId, setPickedDocId] = useState(null) const selectedDocId = resolveSelectedDocumentId(pickedDocId, documents) @@ -686,6 +691,7 @@ export function MeetingDetailTabs({ setPickedDocId(docId) setTab('documents') }} + onJumpToTimestamp={handleJumpToTimestamp} /> )} @@ -701,6 +707,7 @@ export function MeetingDetailTabs({ polishing={transcript.polishing} onDiarize={() => void handleDiarize()} diarizing={transcript.diarizing} + highlightedTimestampMs={highlightedTimestampMs} /> )} @@ -769,6 +776,13 @@ export function MeetingDetailTabs({ recordedMemos={detail.memos ?? []} enhancing={enhancingNotes} onEnhance={(notes) => void handleEnhanceNotes(notes)} + onMemoAdded={(memo) => + setDetail((prev) => ({ + ...prev, + memos: [...(prev.memos ?? []), memo], + memoCount: (prev.memos?.length ?? 0) + 1, + })) + } /> )} diff --git a/apps/desktop/src/renderer/components/meeting/MeetingNotesTab.tsx b/apps/desktop/src/renderer/components/meeting/MeetingNotesTab.tsx index d4604b0..014d463 100644 --- a/apps/desktop/src/renderer/components/meeting/MeetingNotesTab.tsx +++ b/apps/desktop/src/renderer/components/meeting/MeetingNotesTab.tsx @@ -3,14 +3,14 @@ // 자유 노트는 입력마다 이 기기에 저장한다(meeting-notepad-storage). 예전 "Granola Notepad" 는 // React 상태에만 있어 회의를 닫으면 적은 글이 사라졌다. -import { useState } from 'react' +import { useState, useRef } from 'react' import { Box, TextField, Typography } from '@mui/material' import { Sparkles } from 'lucide-react' import { PhysicalButton } from '@d3ro/ui/components/ds' import { d3roFontMono, d3roFontSans, d3roPalette, d3roRadius, typoSx } from '@d3ro/ui/theme' import { useI18n } from '@d3ro/i18n' import type { MeetingMemo } from '@d3ro/core/types' -import { defaultNotepadStorage, loadNotepad, saveNotepad } from './meeting-notepad-storage' +import { defaultNotepadStorage, loadNotepad, saveNotepad, syncNotepadMemo } from './meeting-notepad-storage' import { formatElapsed } from './meeting-format' interface MeetingNotesTabProps { @@ -18,6 +18,7 @@ interface MeetingNotesTabProps { recordedMemos: readonly MeetingMemo[] enhancing: boolean onEnhance: (notes: string) => void + onMemoAdded?: (memo: MeetingMemo) => void } export function MeetingNotesTab({ @@ -25,17 +26,29 @@ export function MeetingNotesTab({ recordedMemos, enhancing, onEnhance, + onMemoAdded, }: MeetingNotesTabProps): React.ReactElement { const { t } = useI18n() const [storage] = useState(defaultNotepadStorage) const [notes, setNotes] = useState(() => loadNotepad(storage, sessionId)) const [saveFailed, setSaveFailed] = useState(false) + const lastSyncedRef = useRef('') const handleChange = (value: string): void => { setNotes(value) setSaveFailed(!saveNotepad(storage, sessionId, value)) } + const handleBlurSync = async (): Promise => { + const trimmed = notes.trim() + if (!trimmed || trimmed === lastSyncedRef.current) return + lastSyncedRef.current = trimmed + const added = await syncNotepadMemo(storage, sessionId, trimmed) + if (added) { + onMemoAdded?.(added) + } + } + return ( {recordedMemos.length > 0 && ( @@ -117,6 +130,7 @@ export function MeetingNotesTab({ minRows={10} value={notes} onChange={(e) => handleChange(e.target.value)} + onBlur={() => void handleBlurSync()} placeholder={t('meetings.notes.placeholder')} fullWidth slotProps={{ htmlInput: { 'aria-label': t('meetings.notes.padTitle') } }} diff --git a/apps/desktop/src/renderer/components/meeting/MeetingSummaryTab.tsx b/apps/desktop/src/renderer/components/meeting/MeetingSummaryTab.tsx index 72f5fc5..a8a9f3b 100644 --- a/apps/desktop/src/renderer/components/meeting/MeetingSummaryTab.tsx +++ b/apps/desktop/src/renderer/components/meeting/MeetingSummaryTab.tsx @@ -8,7 +8,7 @@ import { PhysicalButton } from '@d3ro/ui/components/ds' import { d3roPalette, typoSx } from '@d3ro/ui/theme' import { useI18n } from '@d3ro/i18n' import { EmptyStateCard } from '../shared' -import { MarkdownRenderer } from './MarkdownRenderer' +import { MarkdownRenderer, renderChildrenWithCitations } from './MarkdownRenderer' import { withoutChecklistLines, type ActionItem } from './meeting-documents-model' import type { SummarySource } from './meeting-summary-model' @@ -24,6 +24,7 @@ interface MeetingSummaryTabProps { onCreateSummary: () => void onChooseTemplate: () => void onEditDocument: (documentId: string) => void + onJumpToTimestamp?: (timestampMs: number, label: string) => void } function SectionTitle({ children, action }: { children: React.ReactNode; action?: React.ReactNode }): React.ReactElement { @@ -47,6 +48,7 @@ export function MeetingSummaryTab({ onCreateSummary, onChooseTemplate, onEditDocument, + onJumpToTimestamp, }: MeetingSummaryTabProps): React.ReactElement { const { t } = useI18n() @@ -88,7 +90,7 @@ export function MeetingSummaryTab({ {minutes.summary.trim() && ( - {minutes.summary} + {renderChildrenWithCitations(minutes.summary, onJumpToTimestamp)} )} {minutes.decisions.length > 0 && ( @@ -96,7 +98,7 @@ export function MeetingSummaryTab({ {t('meetings.summary.decisions')} {minutes.decisions.map((d, i) => ( -
  • {d}
  • +
  • {renderChildrenWithCitations(d, onJumpToTimestamp)}
  • ))}
    @@ -107,7 +109,7 @@ export function MeetingSummaryTab({ {minutes.actionItems.map((a, i) => (
  • - {a.task} + {renderChildrenWithCitations(a.task, onJumpToTimestamp)} {(a.assignee || a.deadline) && ( {` · ${[a.assignee, a.deadline].filter(Boolean).join(' · ')}`} @@ -123,7 +125,7 @@ export function MeetingSummaryTab({ } if (source.kind === 'markdown') { - return + return } // 1순위 문서 — 할 일은 체크 목록으로 위에, 본문에서는 같은 줄을 뺀다 @@ -187,7 +189,7 @@ export function MeetingSummaryTab({ pt: '3px', }} > - {item.text} + {renderChildrenWithCitations(item.text, onJumpToTimestamp)} ))} @@ -196,7 +198,7 @@ export function MeetingSummaryTab({ )} - + ) diff --git a/apps/desktop/src/renderer/components/meeting/TranscriptTab.tsx b/apps/desktop/src/renderer/components/meeting/TranscriptTab.tsx index 71fc7bf..55031df 100644 --- a/apps/desktop/src/renderer/components/meeting/TranscriptTab.tsx +++ b/apps/desktop/src/renderer/components/meeting/TranscriptTab.tsx @@ -2,6 +2,7 @@ // Phase 14.5: 전사 편집 탭 (상세 페이지용) — 요약의 근거. // 복사·내보내기는 상세 머리의 행동 줄 한 곳에 있다(예전 "전사 다운로드" 버튼은 거기로 옮겼다). +import { useEffect, useMemo } from 'react' import { Box, Switch, FormControlLabel, LinearProgress, Typography } from '@mui/material' import { EditableSegment } from './EditableSegment' import { PhysicalButton } from '@d3ro/ui/components/ds' @@ -25,6 +26,7 @@ interface TranscriptTabProps { polishing: boolean onDiarize?: () => void diarizing?: boolean + highlightedTimestampMs?: number | null } type TimelineItem = @@ -42,6 +44,7 @@ export function TranscriptTab({ polishing, onDiarize, diarizing = false, + highlightedTimestampMs = null, }: TranscriptTabProps): React.ReactElement { const { t } = useI18n() const showEdited = view === 'edited' @@ -64,6 +67,26 @@ export function TranscriptTab({ })), ].sort((a, b) => a.timestamp - b.timestamp) + const highlightedId = useMemo(() => { + if (highlightedTimestampMs === null || timeline.length === 0) return null + let best = timeline[0] + let bestDiff = Math.abs(best.timestamp - highlightedTimestampMs) + for (const item of timeline) { + const diff = Math.abs(item.timestamp - highlightedTimestampMs) + if (diff < bestDiff) { + best = item + bestDiff = diff + } + } + return best.id + }, [highlightedTimestampMs, timeline]) + + useEffect(() => { + if (!highlightedId || typeof document === 'undefined') return + const el = document.querySelector(`[data-segment-id="${highlightedId}"]`) + el?.scrollIntoView?.({ behavior: 'smooth', block: 'center' }) + }, [highlightedId, highlightedTimestampMs]) + return ( {/* 상단 컨트롤 */} @@ -135,6 +158,7 @@ export function TranscriptTab({ onEdit={onEditSegment} readOnly={!editable} speaker={item.speaker} + highlighted={item.id === highlightedId} /> ) } @@ -142,6 +166,8 @@ export function TranscriptTab({ return ( Promise<{ + success: boolean + data?: import('@d3ro/core/types').MeetingMemo + }> +} + +/** + * 로컬 초안 저장소(localStorage)에 즉시 보관하고, 비어 있지 않은 노트는 `meetingMode.addMemo` IPC를 통해 + * SQLite `meeting_memos` 및 클라우드 동기화 아웃박스에 반영한다. + */ +export async function syncNotepadMemo( + storage: NotepadStorage | null, + sessionId: string, + text: string, + api?: NotepadSyncApi | null, +): Promise { + saveNotepad(storage, sessionId, text) + const trimmed = text.trim() + if (!trimmed) return null + const resolvedApi = + api ?? (typeof window !== 'undefined' ? window.electronAPI?.meetingMode : undefined) + if (!resolvedApi?.addMemo) return null + try { + const res = await resolvedApi.addMemo({ content: trimmed, sessionId, meetingId: sessionId }) + if (res.success && res.data) { + return res.data + } + } catch { + /* IPC 실패 시에도 localStorage 초안은 유지 */ + } + return null +} diff --git a/apps/desktop/src/renderer/components/shared/HistoryEntryCard.tsx b/apps/desktop/src/renderer/components/shared/HistoryEntryCard.tsx index 890360c..737e1e2 100644 --- a/apps/desktop/src/renderer/components/shared/HistoryEntryCard.tsx +++ b/apps/desktop/src/renderer/components/shared/HistoryEntryCard.tsx @@ -5,8 +5,8 @@ // 2026-09-28: 2열 카드(글래스·LED·배지 무더기·떠오르는 hover)를 목록 한 줄로 바꿨다. import React, { useCallback, useEffect, useId, useRef, useState } from 'react' -import { Box, ButtonBase, Chip, Collapse, IconButton, InputBase, Tooltip, Typography } from '@mui/material' -import { Check, ChevronDown, Copy, Play, Square, Star, Tag, Trash2, X } from 'lucide-react' +import { Box, ButtonBase, Chip, Collapse, IconButton, InputBase, TextField, Tooltip, Typography } from '@mui/material' +import { Check, ChevronDown, Copy, Pencil, Play, Square, Star, Tag, Trash2, X } from 'lucide-react' import { PhysicalButton, TactileBadge } from '@d3ro/ui/components/ds' import { d3roPalette, d3roFocusRing, d3roRadius, d3roTransition, typoSx } from '@d3ro/ui/theme' import { useI18n } from '@d3ro/i18n' @@ -66,6 +66,11 @@ export interface HistoryEntryCardProps { activeTag?: string | null /** 태그를 붙이거나 뗐을 때 (화면의 태그 목록 갱신) */ onTagsChanged?: () => void + /** 제목·본문 수정 저장 콜백 */ + onUpdate?: ( + entry: HistoryEntry, + patch: { title?: string | null; polishedText?: string | null; originalText?: string }, + ) => Promise | void } export function HistoryEntryCard({ @@ -79,6 +84,7 @@ export function HistoryEntryCard({ onTagClick, activeTag = null, onTagsChanged, + onUpdate, }: HistoryEntryCardProps): React.ReactElement { const { t } = useI18n() const panelId = useId() @@ -197,6 +203,7 @@ export function HistoryEntryCard({ onTagClick={onTagClick} activeTag={activeTag} onTagsChanged={onTagsChanged} + onUpdate={onUpdate} /> @@ -216,6 +223,10 @@ interface RecordDetailProps { onTagClick?: (tag: string) => void activeTag: string | null onTagsChanged?: () => void + onUpdate?: ( + entry: HistoryEntry, + patch: { title?: string | null; polishedText?: string | null; originalText?: string }, + ) => Promise | void } function SectionLabel({ children }: { children: React.ReactNode }): React.ReactElement { @@ -248,6 +259,7 @@ function RecordDetail({ onTagClick, activeTag, onTagsChanged, + onUpdate, }: RecordDetailProps): React.ReactElement { const { t } = useI18n() const [tags, setTags] = useState([]) @@ -257,9 +269,36 @@ function RecordDetail({ const [showOriginal, setShowOriginal] = useState(false) const [copied, setCopied] = useState(null) const [audioState, setAudioState] = useState('idle') + const [editing, setEditing] = useState(false) + const [draftTitle, setDraftTitle] = useState(entry.title ?? '') + const [draftText, setDraftText] = useState(primaryText) + const [savingEdit, setSavingEdit] = useState(false) const playerRef = useRef(null) const copiedTimerRef = useRef | null>(null) + const handleStartEdit = useCallback(() => { + setDraftTitle(entry.title ?? '') + setDraftText(primaryText) + setEditing(true) + }, [entry.title, primaryText]) + + const handleSaveEdit = useCallback(async () => { + if (!onUpdate || savingEdit) return + setSavingEdit(true) + try { + const hasPolished = Boolean(entry.polishedText?.trim()) + const patch = hasPolished + ? { title: draftTitle.trim() || null, polishedText: draftText } + : { title: draftTitle.trim() || null, originalText: draftText } + const ok = await onUpdate(entry, patch) + if (ok !== false) { + setEditing(false) + } + } finally { + setSavingEdit(false) + } + }, [onUpdate, savingEdit, entry, draftTitle, draftText]) + const getPlayer = useCallback((): HistoryAudioPlayer => { if (!playerRef.current) playerRef.current = new HistoryAudioPlayer(browserHistoryAudioPort, setAudioState) return playerRef.current @@ -405,17 +444,52 @@ function RecordDetail({ {labelled && {originalText ? t('records.section.polished') : t('records.section.original')}} - - {primaryText} - + {editing ? ( + + setDraftTitle(e.target.value)} + placeholder={t('meetings.renameDialog.label')} + fullWidth + sx={{ '& .MuiOutlinedInput-root': { bgcolor: d3roPalette.bg.input } }} + /> + setDraftText(e.target.value)} + fullWidth + sx={{ '& .MuiOutlinedInput-root': { bgcolor: d3roPalette.bg.input } }} + /> + + setEditing(false)} disabled={savingEdit}> + {t('common.cancel')} + + void handleSaveEdit()} + disabled={savingEdit || !draftText.trim()} + > + {t('common.save')} + + + + ) : ( + + {primaryText} + + )} {originalText && ( @@ -591,6 +665,18 @@ function RecordDetail({ + {onUpdate && ( + + + + + + )} | null = config ? resolveCommandPipeline(config, visible) : null const selection = pipeline ? commandSelection(pipeline) : '' + const selectedIds = pipeline ? commandSelections(pipeline) : [] - /** 라디오 선택 — 설정 두 개를 함께 바꾼다. 실패하면 되돌리고 알린다 */ - const handleSelect = async (value: string): Promise => { + /** 파이프라인 ID 목록 적용 — 0개면 none, 1개 이상이면 순서대로 직렬화해 custom 으로 저장한다 */ + const applyPipelineIds = async (nextIds: readonly string[]): Promise => { if (!config) return const previous = config + const serialized = serializeActiveInstructionIds(nextIds) const next: PipelineConfig = - value === COMMAND_NONE + serialized.length === 0 ? { defaultLLMAction: 'none', activeInstructionId: null } - : { defaultLLMAction: 'custom', activeInstructionId: value } + : { defaultLLMAction: 'custom', activeInstructionId: serialized } setConfig(next) try { const results = await Promise.all([ @@ -127,6 +135,14 @@ export function CommandsPage(): React.ReactElement { } } + const handleToggleInstruction = (id: string): void => { + void applyPipelineIds(toggleInstructionInPipeline(selectedIds, id)) + } + + const handleMoveInstruction = (id: string, direction: 'up' | 'down'): void => { + void applyPipelineIds(moveInstructionInPipeline(selectedIds, id, direction)) + } + const openAdd = () => { if (dialogOpen) return setEditId(null) @@ -187,10 +203,19 @@ export function CommandsPage(): React.ReactElement { commit: async () => { const res = await window.electronAPI.instruction.delete({ id: inst.id }) if (!res.success) return false - // 지금도 쓰이는 명령을 지웠으면 원문 그대로 넣기로 돌린다 — 확정 시점의 설정으로 판단한다 + // 지금도 쓰이는 명령을 지웠으면 파이프라인에서 빼고, 남은 명령이 없을 때만 원문 그대로 넣기로 돌린다 const latest = await window.electronAPI.config.getAll() if (latest.success && isInstructionInUse(latest.data, inst.id)) { - await window.electronAPI.config.set({ key: 'defaultLLMAction', value: 'none' }) + const remaining = removeInstructionFromActiveIds(latest.data.activeInstructionId, inst.id) + if (remaining.length === 0) { + await window.electronAPI.config.set({ key: 'activeInstructionId', value: '' }) + await window.electronAPI.config.set({ key: 'defaultLLMAction', value: 'none' }) + } else { + await window.electronAPI.config.set({ + key: 'activeInstructionId', + value: serializeActiveInstructionIds(remaining), + }) + } } return true }, @@ -247,16 +272,13 @@ export function CommandsPage(): React.ReactElement { {loading && instructions.length === 0 ? ( ) : ( - void handleSelect(value)} - aria-label={t('tools.commands.listTitle')} - > + void applyPipelineIds([])} disabled={!config} - control={} + control={} sx={{ flex: 1, minWidth: 0, m: 0, gap: 0.5 }} label={ @@ -269,26 +291,63 @@ export function CommandsPage(): React.ReactElement { /> {visible.map((inst) => { - const isActive = inst.id === selection + const stepIndex = selectedIds.indexOf(inst.id) + const isActive = stepIndex >= 0 + const stepNumber = stepIndex + 1 return ( handleToggleInstruction(inst.id)} disabled={!config} - control={} + control={} sx={{ flex: 1, minWidth: 0, m: 0, gap: 0.5 }} label={ {inst.name} - {isActive && {t('tools.commands.inUse')}} + {isActive && ( + + {selectedIds.length > 1 + ? t('tools.commands.stepBadge', { step: stepNumber }) + : t('tools.commands.inUse')} + + )} {inst.isBuiltin && {t('tools.builtin')}} {inst.description && {inst.description}} } /> - + + {isActive && selectedIds.length > 1 && ( + <> + + + handleMoveInstruction(inst.id, 'up')} + > + + + + + + + handleMoveInstruction(inst.id, 'down')} + > + + + + + + )} ) })} - + )} {!loading && !loadError && visible.length === 0 && ( @@ -431,7 +490,7 @@ export function CommandsPage(): React.ReactElement { ) } -/** 지금 받아쓰기가 거치는 단계 — 조용한 요약 패널 하나 */ +/** 지금 받아쓰기가 거치는 단계 — 조용한 요약 패널 하나 (단일 명령 및 다단계 파이프라인 모두 표시) */ function PipelineSummary({ pipeline }: { pipeline: CommandPipeline }): React.ReactElement { const { t } = useI18n() const current = @@ -439,18 +498,26 @@ function PipelineSummary({ pipeline }: { pipeline: CommandPipeline `${idx + 1}. ${inst.name}`).join(' → ') + : pipeline.kind === 'instruction-missing' + ? t('tools.commands.pipeline.missing') + : t(llmActionLabelKey(pipeline.action)) + + const llmSteps: string[] = pipeline.kind === 'raw' - ? t('tools.commands.pipeline.stepRaw') + ? [t('tools.commands.pipeline.stepRaw')] : pipeline.kind === 'instruction' - ? t('tools.commands.pipeline.stepInstruction', { name: pipeline.instruction.name }) - : pipeline.kind === 'instruction-missing' - ? t('tools.commands.pipeline.stepMissing') - : t('tools.commands.pipeline.stepAction', { action: t(llmActionLabelKey(pipeline.action)) }) - const steps = [t('tools.commands.pipeline.stepStt'), llmStep, t('tools.commands.pipeline.stepInsert')] + ? [t('tools.commands.pipeline.stepInstruction', { name: pipeline.instruction.name })] + : pipeline.kind === 'instructions' + ? pipeline.instructions.map((inst, idx) => + t('tools.commands.pipeline.stepPipelineItem', { step: idx + 1, name: inst.name }), + ) + : pipeline.kind === 'instruction-missing' + ? [t('tools.commands.pipeline.stepMissing')] + : [t('tools.commands.pipeline.stepAction', { action: t(llmActionLabelKey(pipeline.action)) })] + + const steps = [t('tools.commands.pipeline.stepStt'), ...llmSteps, t('tools.commands.pipeline.stepInsert')] return ( (prev === next ? null : next)) }, []) + const handleUpdate = useCallback( + async ( + entry: HistoryEntry, + nextPatch: { title?: string | null; polishedText?: string | null; originalText?: string }, + ): Promise => { + const result = await window.electronAPI.history + .update({ id: entry.id, ...nextPatch }) + .catch(() => null) + if (result?.success && result.data) { + patch(entry.id, result.data) + return true + } + notify({ message: t('records.status.error'), severity: 'error' }) + return false + }, + [patch, notify, t], + ) + const exportingRef = useRef(false) const [exporting, setExporting] = useState(false) const handleExport = useCallback(async () => { @@ -261,15 +279,27 @@ export function HistoryPage(): React.ReactElement { setExporting(true) try { const result = await window.electronAPI.memo - .export({ format: 'markdown', tag: tag ?? undefined }) + .export({ + format: 'markdown', + tag: tag ?? undefined, + mode: mode !== 'all' ? mode : undefined, + isFavorite: favoritesOnly ? true : undefined, + search: query.trim() || undefined, + includeUntagged: !tag, + }) .catch(() => null) - if (result?.success) notify({ message: t('records.exportDone', { path: result.data }), severity: 'success' }) - else notify({ message: t('records.exportFailed'), severity: 'error' }) + if (result?.success && result.data) { + notify({ message: t('records.exportDone', { path: result.data }), severity: 'success' }) + } else if (result?.success && result.data === '') { + // 대화상자 취소 시 무시 + } else { + notify({ message: t('records.exportFailed'), severity: 'error' }) + } } finally { exportingRef.current = false setExporting(false) } - }, [tag, notify, t]) + }, [tag, mode, favoritesOnly, query, notify, t]) // 전체 삭제는 필터와 무관하고 동기화로 다른 기기에도 전파되므로 반드시 확인 단계를 거친다 const clearAll = useClearAllHistory((deleted) => { @@ -477,6 +507,7 @@ export function HistoryPage(): React.ReactElement { onTagClick={handleTagToggle} activeTag={tag} onTagsChanged={() => void loadMeta()} + onUpdate={handleUpdate} /> ))} diff --git a/apps/desktop/src/renderer/pages/KnowledgeBasePage.tsx b/apps/desktop/src/renderer/pages/KnowledgeBasePage.tsx index 9966661..3808830 100644 --- a/apps/desktop/src/renderer/pages/KnowledgeBasePage.tsx +++ b/apps/desktop/src/renderer/pages/KnowledgeBasePage.tsx @@ -14,13 +14,30 @@ import { isImeComposingEvent } from '../utils/keyboard' import { d3roPalette, d3roRadius, typoSx } from '@d3ro/ui/theme' import { useI18n } from '@d3ro/i18n' import type { RAGDocument, RAGQueryResult, RAGIndexProgress } from '@d3ro/core/types' -import { ErrorCode } from '@d3ro/core/errors' +import { ErrorCode, type IPCResult } from '@d3ro/core/errors' import { ToolLoadError, ToolLoading, ToolRow, ToolSection, TOOL_PAGE_SX, TOOL_TABS_SX, TOOL_TEXT_SX } from './tools/ToolSection' import { useDeferredDelete } from './tools/useDeferredDelete' import { documentIndexLabelKey, KNOWLEDGE_EXAMPLE_KEYS, withoutHidden } from './tools/tools-model' type KnowledgeView = 'kb' | 'insights' +/** + * 지식 문서 추가 응답에서 사용자에게 표시할 오류 메시지를 결정한다. + * 파일 선택 취소나 대화상자 중복 활성 상태는 구조화된 details.cancelled 및 ErrorCode로 판별한다. + */ +export function resolveRagAddError(resp: IPCResult): string | null { + if (resp.success) return null + const details = resp.error.details as { cancelled?: boolean; reason?: string } | undefined + if (details?.cancelled === true) return null + if ( + resp.error.code === ErrorCode.RAGDocumentNotFound && + (details?.reason === 'dialog_cancelled' || details?.reason === 'dialog_active') + ) { + return null + } + return resp.error.message +} + export function KnowledgeBasePage(): React.ReactElement { // 이 화면은 두 축을 가진다: 수집된 지식 문서(RAG)와 자동 수집된 입력 데이터 통계. const [view, setView] = useState('kb') @@ -88,8 +105,9 @@ export function KnowledgeBasePage(): React.ReactElement { const resp = await window.electronAPI.rag.addDocument() if (resp.success) { void loadDocuments() - } else if (!resp.error.message.includes('cancelled') && !resp.error.message.includes('already active')) { - setAddError(resp.error.message) + } else { + const errMessage = resolveRagAddError(resp) + if (errMessage) setAddError(errMessage) } } finally { setAdding(false) diff --git a/apps/desktop/src/renderer/pages/VoiceConversationPage.tsx b/apps/desktop/src/renderer/pages/VoiceConversationPage.tsx index 1f45b57..1947c1a 100644 --- a/apps/desktop/src/renderer/pages/VoiceConversationPage.tsx +++ b/apps/desktop/src/renderer/pages/VoiceConversationPage.tsx @@ -7,7 +7,7 @@ import React, { useState, useEffect, useCallback, useRef } from 'react' import { Box, IconButton, TextField, Tooltip, Typography } from '@mui/material' -import { Mic, Square, Send, Trash2, XCircle, Bot } from 'lucide-react' +import { Mic, Square, Send, Trash2, XCircle, Bot, Volume2 } from 'lucide-react' import { PhysicalButton, TactileBadge } from '@d3ro/ui/components/ds' import { ConfirmDialog, PageHeader, useNotify } from '../components/shared' import { VoiceRecordingPanel } from '../components/voice-conversation/VoiceRecordingPanel' @@ -246,6 +246,13 @@ export function VoiceConversationPage(): React.ReactElement { await window.electronAPI.voiceConversation.cancelResponse() }, [isRealtime, realtime]) + const handleReplayMessage = useCallback(async (msg: ConversationMessage) => { + await window.electronAPI.voiceConversation.replayMessage({ + messageId: msg.id, + text: msg.content, + }) + }, []) + const handleTextKeyDown = useCallback( (e: React.KeyboardEvent) => { if (isImeComposingEvent(e)) return @@ -351,9 +358,23 @@ export function VoiceConversationPage(): React.ReactElement { borderRadius: isUser ? BUBBLE_RADIUS.user : BUBBLE_RADIUS.assistant, bgcolor: isUser ? d3roPalette.bg.inset : d3roPalette.bg.card, border: `1px solid ${isUser ? d3roPalette.border.subtle : d3roPalette.border.default}`, + display: 'flex', + alignItems: 'flex-start', + gap: 1, }} > - {msg.content} + {msg.content} + {!isUser && ( + void handleReplayMessage(msg)} + disabled={busy} + sx={{ p: 0.5, mt: -0.25, mr: -0.5, color: d3roPalette.text.tertiary }} + > + + + )} ) diff --git a/apps/desktop/src/renderer/pages/tools/tools-model.ts b/apps/desktop/src/renderer/pages/tools/tools-model.ts index af9642a..5976de3 100644 --- a/apps/desktop/src/renderer/pages/tools/tools-model.ts +++ b/apps/desktop/src/renderer/pages/tools/tools-model.ts @@ -3,6 +3,11 @@ // 화면이 말하는 상태는 여기서 한 번만 정한다: 조회한 설정에서 나온 사실만 쓰고, 추측을 '정상'으로 쓰지 않는다. import type { TranslationKey } from '@d3ro/i18n' +import { + composeInstructionPipelinePrompt, + parseActiveInstructionIds, + serializeActiveInstructionIds, +} from '@d3ro/core/instruction-template' import type { ConversationState, LLMAction, LLMActionSelection, LLMProcessParams } from '@d3ro/core/types' /** 지연 삭제 중인 항목을 목록에서 뺀다 */ @@ -25,10 +30,12 @@ interface InstructionLike { /** * 지금 받아쓰기에 실제로 적용되는 후처리 — main 의 resolveVoiceLlmRoute 와 같은 규칙: * 지시문은 기본 액션이 'custom' 일 때만 쓰인다(활성 지시문이 있어도 refine 등을 덮지 않는다). + * 여러 명령을 순서대로 고르면 `instructions`(2개 이상) 파이프라인으로 합성 실행된다. */ export type CommandPipeline = | { kind: 'raw' } | { kind: 'instruction'; instruction: T } + | { kind: 'instructions'; instructions: readonly T[] } /** 'custom' 인데 활성 지시문이 없거나 지워졌다 */ | { kind: 'instruction-missing' } | { kind: 'action'; action: Exclude } @@ -40,21 +47,32 @@ export function resolveCommandPipeline( const action = config.defaultLLMAction if (action === 'none') return { kind: 'raw' } if (action === 'custom') { - const instruction = config.activeInstructionId - ? instructions.find((item) => item.id === config.activeInstructionId) - : undefined - return instruction ? { kind: 'instruction', instruction } : { kind: 'instruction-missing' } + const ids = parseActiveInstructionIds(config.activeInstructionId) + const matched = ids + .map((id) => instructions.find((item) => item.id === id)) + .filter((item): item is T => item !== undefined) + if (matched.length === 0) return { kind: 'instruction-missing' } + if (matched.length === 1) return { kind: 'instruction', instruction: matched[0] } + return { kind: 'instructions', instructions: matched } } return { kind: 'action', action } } -/** 목록 라디오의 선택 값 — 일반 액션(다듬기 등)이 기본이면 아무것도 고르지 않은 상태다 */ +/** 목록 선택 직렬화 값 — 일반 액션(다듬기 등)이 기본이면 아무것도 고르지 않은 상태다 */ export function commandSelection(pipeline: CommandPipeline): string { if (pipeline.kind === 'raw') return COMMAND_NONE if (pipeline.kind === 'instruction') return pipeline.instruction.id + if (pipeline.kind === 'instructions') return serializeActiveInstructionIds(pipeline.instructions.map((i) => i.id)) return '' } +/** 현재 파이프라인에서 선택된 명령 ID 배열 (순서 보존) */ +export function commandSelections(pipeline: CommandPipeline): string[] { + if (pipeline.kind === 'instruction') return [pipeline.instruction.id] + if (pipeline.kind === 'instructions') return pipeline.instructions.map((i) => i.id) + return [] +} + /** * 지운 명령이 지금도 받아쓰기에 쓰이는가 — 삭제 확정(되돌리기 알림이 닫힐 때) 시점의 설정으로 판단한다. * 삭제를 누른 순간 값으로 판단하면, 그사이 사용자가 고른 다른 명령을 '원문 그대로'로 덮었다. @@ -63,7 +81,13 @@ export function isInstructionInUse( config: { defaultLLMAction?: unknown; activeInstructionId?: unknown } | null | undefined, instructionId: string, ): boolean { - return config?.defaultLLMAction === 'custom' && config.activeInstructionId === instructionId + if (config?.defaultLLMAction !== 'custom') return false + return parseActiveInstructionIds(config.activeInstructionId).includes(instructionId) +} + +/** 삭제 확정된 명령 ID를 활성 파이프라인에서 제거한 나머지 ID 배열을 반환한다. */ +export function removeInstructionFromActiveIds(activeInstructionId: unknown, removedId: string): string[] { + return parseActiveInstructionIds(activeInstructionId).filter((id) => id !== removedId) } /** 일반 후처리 액션 이름 */ @@ -101,6 +125,15 @@ export function commandTestRequest( return { ok: false, reason: 'missing' } case 'instruction': return { ok: true, params: { text: trimmed, action: 'custom', customPrompt: pipeline.instruction.prompt } } + case 'instructions': + return { + ok: true, + params: { + text: trimmed, + action: 'custom', + customPrompt: composeInstructionPipelinePrompt(pipeline.instructions), + }, + } case 'action': // 체인은 여러 단계를 main 의 ChainService 가 실행한다 — 한 번의 LLM 호출로 흉내 내지 않는다 if (pipeline.action === 'chain') return { ok: false, reason: 'chain' } diff --git a/apps/desktop/src/renderer/popups/command-popup/script.js b/apps/desktop/src/renderer/popups/command-popup/script.js index 29924d1..796ba29 100644 --- a/apps/desktop/src/renderer/popups/command-popup/script.js +++ b/apps/desktop/src/renderer/popups/command-popup/script.js @@ -62,8 +62,14 @@ currentCommands = commands itemsContainer.innerHTML = '' items = [] + var activeIds = String(activeId || '') + .split(',') + .map(function (s) { + return s.trim() + }) + .filter(Boolean) - var none = makeRow(0, '0', tr('commandNone'), '', !activeId, true) + var none = makeRow(0, '0', tr('commandNone'), '', activeIds.length === 0, true) itemsContainer.appendChild(none) items.push(none) @@ -75,7 +81,7 @@ } else { commands.forEach(function (cmd, i) { var index = i + 1 - var row = makeRow(index, index <= 9 ? String(index) : '', cmd.name, cmd.description || '', cmd.id === activeId, false) + var row = makeRow(index, index <= 9 ? String(index) : '', cmd.name, cmd.description || '', activeIds.indexOf(cmd.id) !== -1, false) itemsContainer.appendChild(row) items.push(row) }) diff --git a/apps/desktop/tests/main/services/KeyBindingService.test.ts b/apps/desktop/tests/main/services/KeyBindingService.test.ts new file mode 100644 index 0000000..130b4f2 --- /dev/null +++ b/apps/desktop/tests/main/services/KeyBindingService.test.ts @@ -0,0 +1,511 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { globalShortcut, ipcMain, powerMonitor } from 'electron' +import { uIOhook, UiohookKey } from 'uiohook-napi' +import type { UiohookKeyboardEvent, UiohookMouseEvent } from 'uiohook-napi' +import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' +import { + MouseButton, + VK, + createDefaultBindingMap, + kb, + validateBinding, +} from '@d3ro/core/keybinding' +import type { KeyBinding, MouseButtonCode } from '@d3ro/core/types' +import { + configSet, + initInMemoryConfig, + resetInMemoryConfig, + saveKeyBindings, +} from '../../../src/main/services/ConfigService' +import { + getKeyBindingService, + uiohookCodeToVk, + type KeyBindingTriggerPayload, +} from '../../../src/main/services/KeyBindingService' +import { registerKeyBindingHandlers } from '../../../src/main/ipc/keybinding-handlers' +import { resetGlobalInputHookForTests } from '../../../src/main/services/global-input-hook' + +type HookHandler = (event: T) => void + +function createHookHarness(): { + keydown: (event: Partial & { keycode: number }) => void + keyup: (event: Partial & { keycode: number }) => void + mousedown: (event: Partial & { button: unknown }) => void + mouseup: (event: Partial & { button: unknown }) => void +} { + const findListener = (eventName: string): HookHandler => { + const calls = vi.mocked(uIOhook.on).mock.calls + for (let i = calls.length - 1; i >= 0; i -= 1) { + const [registeredEvent, listener] = calls[i] + if (registeredEvent === eventName && typeof listener === 'function') { + return listener as unknown as HookHandler + } + } + throw new Error(`No uIOhook listener registered for "${eventName}"`) + } + + return { + keydown: (event) => { + findListener('keydown')({ + type: 4, + time: Date.now(), + altKey: false, + ctrlKey: false, + metaKey: false, + shiftKey: false, + ...event, + } as UiohookKeyboardEvent) + }, + keyup: (event) => { + findListener('keyup')({ + type: 5, + time: Date.now(), + altKey: false, + ctrlKey: false, + metaKey: false, + shiftKey: false, + ...event, + } as UiohookKeyboardEvent) + }, + mousedown: (event) => { + findListener('mousedown')({ + type: 7, + time: Date.now(), + altKey: false, + ctrlKey: false, + metaKey: false, + shiftKey: false, + x: 0, + y: 0, + clicks: 1, + ...event, + } as UiohookMouseEvent) + }, + mouseup: (event) => { + findListener('mouseup')({ + type: 8, + time: Date.now(), + altKey: false, + ctrlKey: false, + metaKey: false, + shiftKey: false, + x: 0, + y: 0, + clicks: 1, + ...event, + } as UiohookMouseEvent) + }, + } +} + +function mb( + code: MouseButtonCode, + mods: Partial> = {}, +): KeyBinding { + return { + device: 'mouse', + code, + ctrl: mods.ctrl ?? false, + alt: mods.alt ?? false, + shift: mods.shift ?? false, + meta: mods.meta ?? false, + } +} + +const VK_F8 = VK.F1 + 7 +const VK_F9 = VK.F1 + 8 + +describe('KeyBindingService', () => { + const hook = createHookHarness() + + beforeEach(() => { + vi.clearAllMocks() + resetGlobalInputHookForTests() + initInMemoryConfig({ + hotkeyEnabled: true, + suggestionEnabled: true, + }) + const service = getKeyBindingService() + service.dispose() + }) + + afterEach(() => { + getKeyBindingService().dispose() + resetGlobalInputHookForTests() + resetInMemoryConfig() + }) + + describe('keyboard & mouse event handling and lifecycle', () => { + it('maps uiohook keycodes to canonical Windows VK codes and returns null for unknown codes', () => { + expect(uiohookCodeToVk(UiohookKey.F8)).toBe(VK_F8) + expect(uiohookCodeToVk(UiohookKey.AltRight)).toBe(VK.AltRight) + expect(uiohookCodeToVk(UiohookKey.A)).toBe(VK.A) + expect(uiohookCodeToVk(UiohookKey.Enter)).toBe(VK.Enter) + expect(uiohookCodeToVk(999_999)).toBeNull() + }) + + it('attaches uIOhook listeners on start, triggers keyboard actions, and cleans up on stop', () => { + const map = createDefaultBindingMap() + map.command = [kb(VK_F9, { ctrl: true })] + saveKeyBindings(map) + + const service = getKeyBindingService() + const triggered: KeyBindingTriggerPayload[] = [] + service.on('triggered', (payload) => triggered.push(payload)) + + service.loadFromConfig() + service.start() + expect(service.isRunning).toBe(true) + expect(service.registeredBindingCount).toBeGreaterThan(0) + + hook.keydown({ keycode: UiohookKey.F9, ctrlKey: true }) + hook.keyup({ keycode: UiohookKey.F9, ctrlKey: true }) + + expect(triggered).toEqual([ + expect.objectContaining({ + actionId: 'command', + type: 'pressed', + isDoublePress: false, + }), + expect.objectContaining({ + actionId: 'command', + type: 'released', + isDoublePress: false, + }), + ]) + + service.stop() + expect(service.isRunning).toBe(false) + expect(uIOhook.removeListener).toHaveBeenCalledWith('keydown', expect.any(Function)) + expect(uIOhook.removeListener).toHaveBeenCalledWith('keyup', expect.any(Function)) + expect(uIOhook.removeListener).toHaveBeenCalledWith('mousedown', expect.any(Function)) + expect(uIOhook.removeListener).toHaveBeenCalledWith('mouseup', expect.any(Function)) + }) + + it('skips keyboard bindings whose VK has no uiohook mapping (e.g., VK.Pause)', () => { + const map = createDefaultBindingMap() + for (const key of Object.keys(map) as Array) { + map[key] = [] + } + map.command = [kb(VK.Pause)] + saveKeyBindings(map) + + const service = getKeyBindingService() + service.loadFromConfig() + expect(service.registeredBindingCount).toBe(0) + }) + + it('releases held bindings when powerMonitor emits lock-screen or suspend', () => { + const map = createDefaultBindingMap() + map.dictation = [kb(VK_F8)] + map['hands-free'] = [] + saveKeyBindings(map) + + const service = getKeyBindingService() + const triggered: KeyBindingTriggerPayload[] = [] + service.on('triggered', (payload) => triggered.push(payload)) + + service.loadFromConfig() + service.start() + + hook.keydown({ keycode: UiohookKey.F8 }) + expect(triggered).toHaveLength(1) + expect(triggered[0]).toMatchObject({ actionId: 'dictation', type: 'pressed' }) + + const lockCall = vi + .mocked(powerMonitor.on) + .mock.calls.find(([event]) => (event as string) === 'lock-screen') + expect(lockCall).toBeDefined() + const lockHandler = lockCall?.[1] as (() => void) | undefined + lockHandler?.() + + expect(triggered).toHaveLength(2) + expect(triggered[1]).toMatchObject({ actionId: 'dictation', type: 'released' }) + }) + }) + + describe('mouse button binding rules (MB1 rejection, MB2/MB3 modifier requirement, MB4/MB5 free binding)', () => { + it('rejects MB1 (Left click) both with and without modifiers in validation and IPC setBindings', async () => { + expect(validateBinding(mb(MouseButton.Left))).toEqual({ + valid: false, + reason: 'device-disabled', + reasonKey: 'keybinding.disabled.mouseLeft', + warningKey: null, + }) + expect(validateBinding(mb(MouseButton.Left, { ctrl: true, alt: true }))).toEqual({ + valid: false, + reason: 'device-disabled', + reasonKey: 'keybinding.disabled.mouseLeft', + warningKey: null, + }) + + const handlers = new Map unknown>() + vi.mocked(ipcMain.handle).mockImplementation((channel, listener) => { + handlers.set(channel, listener as (...args: unknown[]) => unknown) + }) + registerKeyBindingHandlers() + + const setBindings = handlers.get(IPC_CHANNELS.KEYBINDING.SET_BINDINGS)! + const res = (await setBindings({}, { + actionId: 'dictation', + bindings: [mb(MouseButton.Left, { ctrl: true })], + })) as { success: boolean; error?: { details?: { reason?: string } } } + + expect(res.success).toBe(false) + expect(res.error?.details?.reason).toBe('device-disabled') + }) + + it('requires a modifier for MB2 (Right) and MB3 (Middle), and triggers only when modifier is held', () => { + expect(validateBinding(mb(MouseButton.Right))).toMatchObject({ + valid: false, + reason: 'modifier-required', + reasonKey: 'keybinding.reject.modifierRequired', + }) + expect(validateBinding(mb(MouseButton.Middle))).toMatchObject({ + valid: false, + reason: 'modifier-required', + reasonKey: 'keybinding.reject.modifierRequired', + }) + + expect(validateBinding(mb(MouseButton.Right, { ctrl: true }))).toMatchObject({ + valid: true, + reason: null, + warningKey: 'keybinding.warning.mousePassthrough', + }) + expect(validateBinding(mb(MouseButton.Middle, { alt: true }))).toMatchObject({ + valid: true, + reason: null, + warningKey: 'keybinding.warning.mousePassthrough', + }) + + const map = createDefaultBindingMap() + map.command = [mb(MouseButton.Right, { ctrl: true })] + map.caption = [mb(MouseButton.Middle, { alt: true })] + saveKeyBindings(map) + + const service = getKeyBindingService() + const triggered: KeyBindingTriggerPayload[] = [] + service.on('triggered', (payload) => triggered.push(payload)) + service.loadFromConfig() + service.start() + + // Unmodified MB2 / MB3 must not trigger + hook.mousedown({ button: MouseButton.Right }) + hook.mouseup({ button: MouseButton.Right }) + hook.mousedown({ button: MouseButton.Middle }) + hook.mouseup({ button: MouseButton.Middle }) + expect(triggered).toHaveLength(0) + + // Ctrl+MB2 and Alt+MB3 trigger their respective actions + hook.mousedown({ button: MouseButton.Right, ctrlKey: true }) + hook.mouseup({ button: MouseButton.Right, ctrlKey: true }) + hook.mousedown({ button: MouseButton.Middle, altKey: true }) + hook.mouseup({ button: MouseButton.Middle, altKey: true }) + + expect( + triggered.filter((t) => t.type === 'pressed').map((t) => t.actionId), + ).toEqual(['command', 'caption']) + }) + + it('allows free (unmodified) binding for MB4 (Back) and MB5 (Forward) and handles constituent mouseup release', () => { + expect(validateBinding(mb(MouseButton.Back))).toMatchObject({ + valid: true, + reason: null, + warningKey: 'keybinding.warning.mousePassthrough', + }) + expect(validateBinding(mb(MouseButton.Forward))).toMatchObject({ + valid: true, + reason: null, + warningKey: 'keybinding.warning.mousePassthrough', + }) + + const map = createDefaultBindingMap() + map.dictation = [mb(MouseButton.Back)] + map['hands-free'] = [] + map.command = [mb(MouseButton.Forward)] + saveKeyBindings(map) + + const service = getKeyBindingService() + const triggered: KeyBindingTriggerPayload[] = [] + service.on('triggered', (payload) => triggered.push(payload)) + service.loadFromConfig() + service.start() + + // Press MB4 unmodified -> hold starts; release MB4 while Alt is held -> constituent button still releases hold! + hook.mousedown({ button: MouseButton.Back }) + hook.mouseup({ button: MouseButton.Back, altKey: true }) + + // Press MB5 unmodified -> triggers command + hook.mousedown({ button: MouseButton.Forward }) + hook.mouseup({ button: MouseButton.Forward }) + + expect(triggered).toEqual([ + expect.objectContaining({ actionId: 'dictation', type: 'pressed', isDoublePress: false }), + expect.objectContaining({ actionId: 'dictation', type: 'released', isDoublePress: false }), + expect.objectContaining({ actionId: 'command', type: 'pressed', isDoublePress: false }), + expect.objectContaining({ actionId: 'command', type: 'released', isDoublePress: false }), + ]) + }) + }) + + describe('hold-to-talk vs double-press hands-free on shared binding', () => { + it('distinguishes single hold (dictation) from rapid double-press (hands-free) on the same key', () => { + const sharedBinding = kb(VK_F8) + const map = createDefaultBindingMap() + map.dictation = [sharedBinding] + map['hands-free'] = [sharedBinding] + saveKeyBindings(map) + + const service = getKeyBindingService() + const triggered: KeyBindingTriggerPayload[] = [] + service.on('triggered', (payload) => triggered.push(payload)) + service.loadFromConfig() + service.start() + + let now = 1_000 + const nowSpy = vi.spyOn(Date, 'now').mockImplementation(() => now) + + try { + // 1. Single press & hold (>400ms gap afterwards) + hook.keydown({ keycode: UiohookKey.F8, time: now }) + now += 600 + hook.keyup({ keycode: UiohookKey.F8, time: now }) + + expect(triggered).toEqual([ + expect.objectContaining({ actionId: 'dictation', type: 'pressed', isDoublePress: false }), + expect.objectContaining({ actionId: 'dictation', type: 'released', isDoublePress: false }), + ]) + + triggered.length = 0 + + // 2. Wait past double-press window, then double-press within 150ms + now += 1_000 + hook.keydown({ keycode: UiohookKey.F8, time: now }) + now += 80 + hook.keyup({ keycode: UiohookKey.F8, time: now }) + now += 100 + hook.keydown({ keycode: UiohookKey.F8, time: now }) + now += 80 + hook.keyup({ keycode: UiohookKey.F8, time: now }) + + expect(triggered).toEqual([ + expect.objectContaining({ actionId: 'dictation', type: 'pressed', isDoublePress: false }), + expect.objectContaining({ actionId: 'dictation', type: 'released', isDoublePress: false }), + expect.objectContaining({ actionId: 'hands-free', type: 'pressed', isDoublePress: true }), + expect.objectContaining({ actionId: 'hands-free', type: 'released', isDoublePress: true }), + ]) + } finally { + nowSpy.mockRestore() + } + }) + }) + + describe('multiple bindings per action', () => { + it('triggers the same action from multiple distinct keyboard and mouse bindings independently', () => { + const map = createDefaultBindingMap() + map.dictation = [kb(VK_F8), mb(MouseButton.Back)] + map['hands-free'] = [] + saveKeyBindings(map) + + const service = getKeyBindingService() + const triggered: KeyBindingTriggerPayload[] = [] + service.on('triggered', (payload) => triggered.push(payload)) + service.loadFromConfig() + service.start() + + hook.keydown({ keycode: UiohookKey.F8 }) + hook.keyup({ keycode: UiohookKey.F8 }) + + hook.mousedown({ button: MouseButton.Back }) + hook.mouseup({ button: MouseButton.Back }) + + expect(triggered).toEqual([ + expect.objectContaining({ + actionId: 'dictation', + type: 'pressed', + binding: expect.objectContaining({ device: 'keyboard', code: VK_F8 }), + }), + expect.objectContaining({ + actionId: 'dictation', + type: 'released', + binding: expect.objectContaining({ device: 'keyboard', code: VK_F8 }), + }), + expect.objectContaining({ + actionId: 'dictation', + type: 'pressed', + binding: expect.objectContaining({ device: 'mouse', code: MouseButton.Back }), + }), + expect.objectContaining({ + actionId: 'dictation', + type: 'released', + binding: expect.objectContaining({ device: 'mouse', code: MouseButton.Back }), + }), + ]) + }) + }) + + describe('suggestion capture accelerator registration & unregistration', () => { + it('registers suggestion accelerators only while suggestion capture is active and suggestionEnabled is true', () => { + const service = getKeyBindingService() + service.loadFromConfig() + service.start() + + // On Windows/Linux, no globalShortcut is registered when suggestion overlay is inactive + vi.mocked(globalShortcut.register).mockClear() + vi.mocked(globalShortcut.unregister).mockClear() + + service.setSuggestionCaptureActive(true) + + const registeredWhenActive = vi + .mocked(globalShortcut.register) + .mock.calls.map(([accelerator]) => accelerator) + expect(registeredWhenActive).toEqual( + expect.arrayContaining([ + 'Control+Alt+Return', + 'Control+Alt+Space', + 'Control+Alt+Down', + 'Control+Alt+Up', + 'Control+Alt+Backspace', + ]), + ) + + // Deactivating suggestion capture unregisters all captured accelerators + service.setSuggestionCaptureActive(false) + const unregisteredWhenInactive = vi + .mocked(globalShortcut.unregister) + .mock.calls.map(([accelerator]) => accelerator) + expect(unregisteredWhenInactive).toEqual( + expect.arrayContaining([ + 'Control+Alt+Return', + 'Control+Alt+Space', + 'Control+Alt+Down', + 'Control+Alt+Up', + 'Control+Alt+Backspace', + ]), + ) + }) + + it('releases captured accelerators when suggestionEnabled is turned off and re-registers when turned back on', () => { + const service = getKeyBindingService() + service.loadFromConfig() + service.start() + + service.setSuggestionCaptureActive(true) + vi.mocked(globalShortcut.register).mockClear() + vi.mocked(globalShortcut.unregister).mockClear() + + // Turning off suggestionEnabled releases captured shortcuts immediately + configSet('suggestionEnabled', false) + expect(vi.mocked(globalShortcut.unregister)).toHaveBeenCalledWith('Control+Alt+Return') + + vi.mocked(globalShortcut.register).mockClear() + + // Turning suggestionEnabled back on while capture is still wanted re-registers them + configSet('suggestionEnabled', true) + expect(vi.mocked(globalShortcut.register)).toHaveBeenCalledWith( + 'Control+Alt+Return', + expect.any(Function), + ) + }) + }) +}) diff --git a/apps/desktop/tests/main/services/TypoCorrectionService.test.ts b/apps/desktop/tests/main/services/TypoCorrectionService.test.ts index d7ed6ce..34fd3e6 100644 --- a/apps/desktop/tests/main/services/TypoCorrectionService.test.ts +++ b/apps/desktop/tests/main/services/TypoCorrectionService.test.ts @@ -48,11 +48,19 @@ function typingContext(overrides: Partial = {}): TypingContext { } function deps(overrides: Partial = {}) { - const state = { lastKeyAt: 1000 } - const replaceBeforeCaret = vi.fn(async () => true) + const state = { lastKeyAt: 1000, inputRevision: 1 } + const replaceBeforeCaret = vi.fn(async (_count: number, _text: string, _options?: { commitComposition?: boolean }) => true) const base: TypoCorrectionDeps = { - replaceBeforeCaret, + replaceBeforeCaret: async (count, text, options) => { + if (options?.canApply && !options.canApply()) return false + options?.beforeApply?.() + return options?.commitComposition + ? replaceBeforeCaret(count, text, { commitComposition: true }) + : replaceBeforeCaret(count, text) + }, lastKeyAt: () => state.lastKeyAt, + inputRevision: () => state.inputRevision, + nativeInputTick: () => 10, expectProgrammaticKeys: vi.fn(), expectProgrammaticInsert: vi.fn(() => () => undefined), foregroundHandle: () => 7, @@ -216,6 +224,48 @@ describe('TypoCorrectionService', () => { expect(fake.replaceBeforeCaret).not.toHaveBeenCalled() }) + it('같은 밀리초의 입력/클릭도 대기한 교정의 삭제 직전에 취소한다', async () => { + let ready!: () => void + const fake = deps({ + replaceBeforeCaret: async (_count, _text, options) => { + await new Promise((resolve) => { ready = resolve }) + if (options?.canApply && !options.canApply()) return false + options?.beforeApply?.() + return true + } + }) + const service = new TypoCorrectionService(fake.deps) + const corrected = vi.fn() + service.on('corrected', corrected) + expect(service.handleTypingContext(typingContext({ inputRevision: 1 }))).toBe('correcting') + expect(fake.deps.expectProgrammaticKeys).not.toHaveBeenCalled() + expect(fake.deps.expectProgrammaticInsert).not.toHaveBeenCalled() + fake.state.inputRevision += 1 // lastKeyAt is intentionally unchanged + ready() + await flush() + expect(corrected).not.toHaveBeenCalled() + expect(fake.deps.expectProgrammaticInsert).not.toHaveBeenCalled() + expect(fake.deps.expectProgrammaticKeys).toHaveBeenLastCalledWith(0) + }) + + it('JS 훅 도착 전 OS 입력이 바뀌었거나 조회가 불가능하면 자동 교정을 취소한다', async () => { + for (const tick of [11, null]) { + const fake = deps({ nativeInputTick: () => tick }) + const service = new TypoCorrectionService(fake.deps) + service.handleTypingContext(typingContext({ nativeInputTick: 10 })) + await flush() + expect(fake.replaceBeforeCaret).not.toHaveBeenCalled() + } + }) + + it('포그라운드 창 확인이 실패해도 이전 창에 있다고 가정하지 않는다', async () => { + const fake = deps({ foregroundHandle: () => null }) + const service = new TypoCorrectionService(fake.deps) + service.handleTypingContext(typingContext()) + await flush() + expect(fake.replaceBeforeCaret).not.toHaveBeenCalled() + }) + it('같은 구간은 두 번 검사하지 않는다', async () => { const fake = deps({ warmModel: () => 'gemma' }) const service = new TypoCorrectionService(fake.deps) @@ -262,6 +312,41 @@ describe('TypoCorrectionService', () => { expect(fake.deps.generate).not.toHaveBeenCalled() }) + it('콜드 모델이거나 유휴 스냅샷이 와도 커서 끝에서 조합 중인 한글 초성(안ㄴ·안녕ㅎ·오늘 ㅇ)을 지워 글자를 삼키지 않는다', async () => { + const fake = deps({ warmModel: () => null }) + const service = new TypoCorrectionService(fake.deps) + + for (const prefix of ['안ㄴ', '안녕ㅎ', '안녕하ㅅ', '한ㄱ', '글ㅈ', '오늘 ㅇ', '아직 ㄴ']) { + expect( + service.handleTypingContext( + typingContext({ + prefix, + isComposing: false, + idleMs: 800, + appName: 'chrome.exe' + }) + ), + prefix + ).toBe('pass') + } + await flush() + expect(fake.replaceBeforeCaret).not.toHaveBeenCalled() + + // 반면 어절이 공백으로 끝난 뒤 남은 내부 낱자 잡음("그ㅓ런건 ")은 콜드 모델에서도 즉시 정리한다 + expect( + service.handleTypingContext( + typingContext({ + prefix: '그ㅓ런건 ', + isComposing: false, + idleMs: 800, + appName: 'chrome.exe' + }) + ) + ).toBe('correcting') + await flush() + expect(fake.replaceBeforeCaret).toHaveBeenCalledWith('그ㅓ런건 '.length, '그런건 ') + }) + it('검사 중 접두가 바뀌면 검사를 버리고 새 문맥은 그대로 넘긴다', async () => { let release: (() => void) | null = null const fake = deps({ diff --git a/apps/desktop/tests/main/services/UnifiedSearchService.test.ts b/apps/desktop/tests/main/services/UnifiedSearchService.test.ts index 511abf2..09c23bc 100644 --- a/apps/desktop/tests/main/services/UnifiedSearchService.test.ts +++ b/apps/desktop/tests/main/services/UnifiedSearchService.test.ts @@ -1,6 +1,6 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' import { ErrorCode } from '@d3ro/core/errors' -import type { History, MeetingSessionRow } from '../../../src/main/db/schema' +import type { History, MeetingMemoRow, MeetingSessionRow } from '../../../src/main/db/schema' interface MockBuilderOptions { get?: unknown @@ -182,4 +182,48 @@ describe('UnifiedSearchService', () => { expect.objectContaining({ code: ErrorCode.DBQueryFailed }), ) }) + + it('includes meeting_memos and timed transcript segments in meeting search results', async () => { + const memoOnlyMeeting: MeetingSessionRow = { + ...meeting, + id: 'meeting-memo-1', + title: '설계 검토 회의', + startedAt: 400, + createdAt: 400, + rawTranscript: '[00:10] 아키텍처 개요 설명', + } + const memoRow: MeetingMemoRow = { + id: 'memo-1', + sessionId: 'meeting-memo-1', + content: '마이그레이션 체크리스트 확인 필요', + timestampMs: 75_000, + createdAt: 410, + updatedAt: 410, + } + selectQueue.push( + createMockBuilder({ get: { count: 0 } }), + createMockBuilder({ all: [] }), + createMockBuilder({ get: { count: 0 } }), + createMockBuilder({ all: [] }), + createMockBuilder({ all: [memoRow] }), + createMockBuilder({ all: [memoOnlyMeeting] }), + ) + const { getUnifiedSearchService, resetUnifiedSearchServiceForTests } = await import( + '../../../src/main/services/UnifiedSearchService' + ) + resetUnifiedSearchServiceForTests() + + const response = getUnifiedSearchService().search({ query: '체크리스트', limit: 10 }) + + expect(response.total).toBe(1) + expect(response.results).toEqual([ + expect.objectContaining({ + source: 'meeting', + id: 'meeting-memo-1', + title: '설계 검토 회의', + matchedField: 'content', + excerpt: '[01:15] 마이그레이션 체크리스트 확인 필요', + }), + ]) + }) }) diff --git a/apps/desktop/tests/main/services/VoiceModeService.test.ts b/apps/desktop/tests/main/services/VoiceModeService.test.ts index f0c19de..a5775cc 100644 --- a/apps/desktop/tests/main/services/VoiceModeService.test.ts +++ b/apps/desktop/tests/main/services/VoiceModeService.test.ts @@ -380,6 +380,29 @@ describe('VoiceModeService', () => { expect(action).toBe('custom') expect(systemPrompt).toBeUndefined() }) + + it('복수 지시문 파이프라인(정리 → 격식체 변환)이 설정되면 단계별로 합성된 시스템 프롬프트를 전달한다', async () => { + registerInstruction({ + id: 'builtin-clean-speech', + name: '구어·추임새·중복 정리', + prompt: '추임새와 말바꾸기, 중복을 제거하세요.', + }) + registerInstruction({ + id: 'builtin-formal', + name: '격식체 변환', + prompt: '정중한 비즈니스 존댓말(-습니다/-합니다)로 변환하세요.', + }) + config.values.defaultLLMAction = 'custom' + config.values.activeInstructionId = 'builtin-clean-speech,builtin-formal' + + await runSession() + const [text, action, , systemPrompt] = mockLLM.processText.mock.calls[0] + + expect(text).toBe(TRANSCRIPT) + expect(action).toBe('custom') + expect(systemPrompt).toContain('[1단계: 구어·추임새·중복 정리]') + expect(systemPrompt).toContain('[2단계: 격식체 변환]') + }) }) // ── 두 번째 진입점: 음성 단축키 ──────────────────────── diff --git a/apps/desktop/tests/main/services/i18n-locale-parity.test.ts b/apps/desktop/tests/main/services/i18n-locale-parity.test.ts new file mode 100644 index 0000000..8ab0d9d --- /dev/null +++ b/apps/desktop/tests/main/services/i18n-locale-parity.test.ts @@ -0,0 +1,61 @@ +// tests/main/services/i18n-locale-parity.test.ts +// GAP-I18N-01 / GAP-UX-07 / GAP-LLM-01 회귀 방지 테스트: +// 12개 지원 로케일 파일의 키 집합이 마스터(ko.json)와 100% 일치하는지 검증한다. + +import fs from 'node:fs' +import path from 'node:path' +import { describe, it, expect } from 'vitest' +import { LOCALE_META } from '@d3ro/i18n' + +const LOCALES_DIR = path.resolve(__dirname, '../../../../../packages/i18n/src/locales') + +describe('i18n 12개 로케일 키 정합성 (GAP-I18N-01 / GAP-UX-07 / GAP-LLM-01)', () => { + const ko = JSON.parse(fs.readFileSync(path.join(LOCALES_DIR, 'ko.json'), 'utf8')) as Record + const en = JSON.parse(fs.readFileSync(path.join(LOCALES_DIR, 'en.json'), 'utf8')) as Record + const masterKeys = Object.keys(ko) + + it('마스터(ko.json)와 영어(en.json)의 키 집합이 정확히 일치한다', () => { + const missingInEn = masterKeys.filter((k) => !(k in en)) + const extraInEn = Object.keys(en).filter((k) => !(k in ko)) + expect(missingInEn).toEqual([]) + expect(extraInEn).toEqual([]) + }) + + it.each(LOCALE_META.map((m) => m.code))( + "로케일 '%s'가 마스터(ko.json)의 모든 키를 빠짐없이 포함한다", + (localeCode) => { + const filePath = path.join(LOCALES_DIR, `${localeCode}.json`) + const data = JSON.parse(fs.readFileSync(filePath, 'utf8')) as Record + const missing = masterKeys.filter((k) => !(k in data) || typeof data[k] !== 'string' || data[k].length === 0) + expect(missing).toEqual([]) + }, + ) + + it('온보딩 LLM 모델 크기 안내가 기본 모델(gemma4:e4b, 4.0GB)과 일치한다 (GAP-UX-07)', () => { + expect(ko['onboarding.llmModelSize']).toContain('4.0GB') + expect(en['onboarding.llmModelSize']).toContain('4.0GB') + }) + + it('번역 대상 언어 설정 키가 정의되어 있다 (GAP-LLM-01)', () => { + const requiredKeys = [ + 'settings.targetLanguage', + 'settings.targetLanguage.desc', + 'settings.targetLanguage.en', + 'settings.targetLanguage.ko', + 'settings.targetLanguage.ja', + 'settings.targetLanguage.zhCN', + 'settings.targetLanguage.zhTW', + 'settings.targetLanguage.es', + 'settings.targetLanguage.fr', + 'settings.targetLanguage.de', + 'settings.targetLanguage.pt', + 'settings.targetLanguage.vi', + 'settings.targetLanguage.id', + 'settings.targetLanguage.ar', + ] + for (const key of requiredKeys) { + expect(ko[key]).toBeTruthy() + expect(en[key]).toBeTruthy() + } + }) +}) diff --git a/apps/desktop/tests/main/services/input-intelligence.test.ts b/apps/desktop/tests/main/services/input-intelligence.test.ts index 8a99c4a..386a83d 100644 --- a/apps/desktop/tests/main/services/input-intelligence.test.ts +++ b/apps/desktop/tests/main/services/input-intelligence.test.ts @@ -126,12 +126,13 @@ describe('classifyKeyStroke', () => { }) describe('텍스트 지표', () => { - it('단어 수 — 공백 분리 토큰 중 문자/숫자가 있는 것만', () => { + it('단어 수 — 공백 분리 토큰 중 문자/숫자가 있는 것만 세며, 비공백 CJK 문장도 분할 집계한다', () => { expect(countWords('')).toBe(0) expect(countWords('hello world')).toBe(2) expect(countWords('안녕하세요 오늘 날씨')).toBe(3) expect(countWords(' , . ! ')).toBe(0) expect(countWords('v1.2 배포 준비')).toBe(3) + expect(countWords('今天开会讨论了三件事')).toBeGreaterThanOrEqual(4) }) it('문장 수 — 종결 부호와 개행', () => { diff --git a/apps/desktop/tests/main/services/input-telemetry-focus-flicker.test.ts b/apps/desktop/tests/main/services/input-telemetry-focus-flicker.test.ts index 9e09d73..158a9bf 100644 --- a/apps/desktop/tests/main/services/input-telemetry-focus-flicker.test.ts +++ b/apps/desktop/tests/main/services/input-telemetry-focus-flicker.test.ts @@ -27,6 +27,7 @@ vi.mock('../../../src/main/services/UiaContextService', () => ({ }) })) vi.mock('../../../src/main/utils/win32-foreground', () => ({ getForegroundWindowInfo: () => null })) +vi.mock('../../../src/main/utils/win32-input-activity', () => ({ getLastNativeInputTick: () => 123 })) import { getInputTelemetryService, diff --git a/apps/desktop/tests/main/services/input-telemetry-ime-keys.test.ts b/apps/desktop/tests/main/services/input-telemetry-ime-keys.test.ts index 4b92fd2..e813c22 100644 --- a/apps/desktop/tests/main/services/input-telemetry-ime-keys.test.ts +++ b/apps/desktop/tests/main/services/input-telemetry-ime-keys.test.ts @@ -10,12 +10,13 @@ vi.mock('../../../src/main/services/LoggerService', () => ({ getLogger: () => ({ debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() }) })) vi.mock('../../../src/main/services/global-input-hook', () => ({ acquireGlobalInputHook: () => () => undefined })) -// 매핑 없는 키코드(0) = IME 처리 키 +// 0은 합성 유니코드 키다. IME는 별도의 변환표에 없는 코드를 쓴다. vi.mock('../../../src/main/services/KeyBindingService', () => ({ - uiohookCodeToVk: (code: number) => (code === 0 ? null : 0x41) + uiohookCodeToVk: (code: number) => (code === 0xffff ? null : 0x41) })) vi.mock('../../../src/main/services/UiaContextService', () => ({ getUiaContextService: () => ({}) })) vi.mock('../../../src/main/utils/win32-foreground', () => ({ getForegroundWindowInfo: () => null })) +vi.mock('../../../src/main/utils/win32-input-activity', () => ({ getLastNativeInputTick: () => 123 })) import { getInputTelemetryService, @@ -37,13 +38,19 @@ describe('IME 글자 키', () => { it('변환표에 없는 키(IME 처리 키)를 수정자 없이 누르면 타이핑으로 본다', () => { const service = getInputTelemetryService() as unknown as Internal expect(service._lastTypedAt).toBe(0) - service._handleKeyDown(key(0)) + service._handleKeyDown(key(0xffff)) expect(service._lastTypedAt).toBeGreaterThan(0) }) it('Ctrl/Alt 와 함께면 단축키다 — 타이핑이 아니다', () => { const service = getInputTelemetryService() as unknown as Internal - service._handleKeyDown(key(0, { ctrlKey: true })) + service._handleKeyDown(key(0xffff, { ctrlKey: true })) + expect(service._lastTypedAt).toBe(0) + }) + + it('합성 유니코드 패킷은 사용자 타이핑으로 집계하지 않는다', () => { + const service = getInputTelemetryService() as unknown as Internal + service._handleKeyDown(key(0)) expect(service._lastTypedAt).toBe(0) }) }) diff --git a/apps/desktop/tests/main/services/input-telemetry-stale-snapshot.test.ts b/apps/desktop/tests/main/services/input-telemetry-stale-snapshot.test.ts new file mode 100644 index 0000000..1110264 --- /dev/null +++ b/apps/desktop/tests/main/services/input-telemetry-stale-snapshot.test.ts @@ -0,0 +1,216 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import type { FocusSnapshot } from '@d3ro/core/input-intelligence' +import type { ForegroundWindowInfo } from '../../../src/main/utils/win32-foreground' + +const mocks = vi.hoisted(() => ({ + snapshot: vi.fn<() => Promise>(), + foreground: vi.fn<() => ForegroundWindowInfo | null>(), + nativeInputTick: vi.fn<() => number | null>() +})) + +vi.mock('electron', () => ({ screen: { getPrimaryDisplay: () => ({ scaleFactor: 1 }) } })) +vi.mock('uiohook-napi', () => ({ uIOhook: { on: vi.fn(), removeListener: vi.fn() } })) +vi.mock('../../../src/main/db', () => ({ getDatabase: vi.fn() })) +vi.mock('../../../src/main/services/ConfigService', () => ({ + configGet: (key: string) => (key === 'inputExcludedApps' ? [] : undefined), + configSet: vi.fn() +})) +vi.mock('../../../src/main/services/LoggerService', () => ({ + getLogger: () => ({ debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() }) +})) +vi.mock('../../../src/main/services/global-input-hook', () => ({ acquireGlobalInputHook: () => () => undefined })) +vi.mock('../../../src/main/services/KeyBindingService', () => ({ uiohookCodeToVk: (code: number) => code })) +vi.mock('../../../src/main/services/UiaContextService', () => ({ + getUiaContextService: () => ({ + getSnapshot: mocks.snapshot, + isAvailable: () => true, + lastReason: null, + lastSuccessAt: 0 + }) +})) +vi.mock('../../../src/main/utils/win32-foreground', () => ({ + getForegroundWindowInfo: mocks.foreground, + isForegroundKoreanIme: () => false +})) +vi.mock('../../../src/main/utils/win32-input-activity', () => ({ getLastNativeInputTick: mocks.nativeInputTick })) + +import { + getInputTelemetryService, + resetInputTelemetryServiceForTests, + type TypingContext +} from '../../../src/main/services/InputTelemetryService' + +interface Internal { + _running: boolean + _lastKeyAt: number + _lastTypedAt: number + _lastFocusKey: string + _textAtFocus: string + _lastContext: TypingContext | null + _foreground: { appName: string | null; windowTitle: string | null; hwnd: number | null } + _captureTextSnapshot(): Promise + _handleKeyDown(event: { keycode: number }): void + _handleMouseDown(event: { x: number; y: number; button: number }): void + _handleMouseUp(event: { x: number; y: number }): void + _handleWheel(event: { rotation: number }): void +} + +function snapshot(overrides: Partial = {}): FocusSnapshot { + return { + available: true, + isPassword: false, + isEditable: true, + isComposing: false, + hasSelection: false, + controlType: 'Edit', + controlName: '메시지', + textSource: 'value', + text: '안녕하세요', + caretOffset: 5, + caretRect: null, + elementRect: null, + windowTitle: '채팅', + appName: 'chrome.exe', + processId: 1, + capturedAt: Date.now(), + ...overrides + } +} + +describe('입력 중 늦게 도착하는 UIA 스냅샷', () => { + let service: ReturnType + let internal: Internal + let publish: ReturnType + + beforeEach(() => { + resetInputTelemetryServiceForTests() + vi.useFakeTimers() + vi.setSystemTime(10_000) + mocks.foreground.mockReset().mockReturnValue(null) + mocks.nativeInputTick.mockReset().mockReturnValue(123) + mocks.snapshot.mockReset() + service = getInputTelemetryService() + internal = service as unknown as Internal + internal._running = true + internal._foreground = { appName: 'chrome.exe', windowTitle: '채팅', hwnd: 42 } + internal._lastKeyAt = Date.now() + internal._lastTypedAt = Date.now() + internal._textAtFocus = '기존 기준선' + publish = vi.fn() + service.on('typed-context', publish) + }) + + afterEach(() => { + service.dispose() + vi.restoreAllMocks() + vi.useRealTimers() + }) + + function pending() { + let resolve!: (value: FocusSnapshot) => void + mocks.snapshot.mockImplementationOnce(() => new Promise((done) => { resolve = done })) + const request = internal._captureTextSnapshot() + return { request, resolve } + } + + it('같은 밀리초에 친 새 글자도 감지해 오래된 텍스트를 발행하거나 기준선으로 저장하지 않는다', async () => { + const delayed = pending() + const at = internal._lastKeyAt + internal._handleKeyDown({ keycode: 0x41 }) + expect(internal._lastKeyAt).toBe(at) + delayed.resolve(snapshot()) + + expect(await delayed.request).toBeNull() + expect(publish).not.toHaveBeenCalled() + expect(internal._lastContext).toBeNull() + expect(internal._textAtFocus).toBe('기존 기준선') + + mocks.snapshot.mockResolvedValueOnce(snapshot({ text: '안녕하세요 추가', caretOffset: 8 })) + expect((await internal._captureTextSnapshot())?.prefix).toBe('안녕하세요 추가') + expect(publish).toHaveBeenCalledTimes(1) + }) + + it.each([ + ['마우스 클릭', (value: Internal) => value._handleMouseDown({ x: 2, y: 3, button: 1 })], + ['선택 드래그 끝', (value: Internal) => value._handleMouseUp({ x: 2, y: 3 })], + ['스크롤', (value: Internal) => value._handleWheel({ rotation: 1 })], + ['합성 유니코드 키', (value: Internal) => value._handleKeyDown({ keycode: 0 })] + ] as const)('%s 중 도착한 응답은 버린다', async (_name, change) => { + const delayed = pending() + change(internal) + delayed.resolve(snapshot()) + expect(await delayed.request).toBeNull() + expect(publish).not.toHaveBeenCalled() + }) + + it('통계에서 제외한 프로그램 백스페이스도 스냅샷을 무효화한다', async () => { + service.expectProgrammaticKeys(1) + const delayed = pending() + internal._handleKeyDown({ keycode: 0x08 }) + delayed.resolve(snapshot()) + expect(await delayed.request).toBeNull() + expect(publish).not.toHaveBeenCalled() + }) + + it('키 이벤트 없이 포그라운드 창이 바뀌어도 이전 창의 텍스트를 발행하지 않는다', async () => { + const delayed = pending() + mocks.foreground.mockReturnValue({ + hwnd: 99, title: '다른 창', appName: 'notes.exe', processId: 2, appPath: null, bounds: null + }) + delayed.resolve(snapshot()) + expect(await delayed.request).toBeNull() + expect(publish).not.toHaveBeenCalled() + }) + + it('중지 후 재시작해도 이전 세션의 응답을 적용하지 않는다', async () => { + const delayed = pending() + service.stop() + service.start() + delayed.resolve(snapshot()) + expect(await delayed.request).toBeNull() + expect(publish).not.toHaveBeenCalled() + }) + + it('마지막 키보다 먼저 캡처된 응답은 버린다', async () => { + mocks.snapshot.mockResolvedValueOnce(snapshot({ capturedAt: Date.now() - 1 })) + expect(await internal._captureTextSnapshot()).toBeNull() + expect(publish).not.toHaveBeenCalled() + }) + + it('메인 스레드에 키 콜백이 아직 도착하지 않아도 네이티브 입력 변화로 취소한다', async () => { + const revision = service.inputRevision + const delayed = pending() + mocks.nativeInputTick.mockReturnValue(124) + delayed.resolve(snapshot()) + expect(service.inputRevision).toBe(revision) + expect(await delayed.request).toBeNull() + expect(publish).not.toHaveBeenCalled() + }) + + it('네이티브 tick이 감소하거나 래핑되어도 변화로 처리한다', async () => { + mocks.nativeInputTick.mockReturnValue(0xffffffff) + const delayed = pending() + mocks.nativeInputTick.mockReturnValue(0) + delayed.resolve(snapshot()) + expect(await delayed.request).toBeNull() + expect(publish).not.toHaveBeenCalled() + }) + + it('Windows에서 네이티브 상태를 못 읽으면 문맥에 null을 명시해 자동 교정이 거절할 수 있게 한다', async () => { + vi.spyOn(process, 'platform', 'get').mockReturnValue('win32') + mocks.nativeInputTick.mockReturnValue(null) + mocks.snapshot.mockResolvedValueOnce(snapshot()) + expect((await internal._captureTextSnapshot())?.nativeInputTick).toBeNull() + }) + + it('겹친 타이머들은 UIA 조회와 문맥 발행을 한 번만 한다', async () => { + const delayed = pending() + const duplicate = internal._captureTextSnapshot() + delayed.resolve(snapshot()) + const [first, second] = await Promise.all([delayed.request, duplicate]) + expect(first).toBe(second) + expect(first?.inputRevision).toBe(service.inputRevision) + expect(mocks.snapshot).toHaveBeenCalledTimes(1) + expect(publish).toHaveBeenCalledTimes(1) + }) +}) diff --git a/apps/desktop/tests/main/services/llm-prompts.test.ts b/apps/desktop/tests/main/services/llm-prompts.test.ts index cb8ea62..223b542 100644 --- a/apps/desktop/tests/main/services/llm-prompts.test.ts +++ b/apps/desktop/tests/main/services/llm-prompts.test.ts @@ -20,6 +20,8 @@ import { renderInstructionPrompt, buildInstructionInvocation, resolveSystemPrompt, + resolveTargetLanguage, + SUPPORTED_TARGET_LANGUAGES, buildSuggestionPrompt, DEFAULT_TARGET_LANGUAGE, BASE_SYSTEM_PROMPTS, @@ -28,6 +30,7 @@ import { FREE_PROMPT_SYSTEM_PROMPT, } from '../../../src/main/services/llm-prompts' import { buildCaptionRefinePrompt, buildTypoCheckPrompt } from '../../../src/main/services/llm-prompts' +import * as ConfigServiceModule from '../../../src/main/services/ConfigService' beforeEach(() => { vi.clearAllMocks() @@ -153,6 +156,23 @@ describe('resolveSystemPrompt', () => { expect(resolveSystemPrompt('translate')).toContain(`${DEFAULT_TARGET_LANGUAGE}로 번역`) }) + it('ConfigService에 targetLanguage가 설정되어 있으면 명시 인자 생략 시 설정값을 사용한다', () => { + const spy = vi.spyOn(ConfigServiceModule, 'getConfigService').mockReturnValue({ + get: (key: string) => (key === 'targetLanguage' ? 'Japanese' : undefined), + } as never) + try { + expect(resolveTargetLanguage()).toBe('Japanese') + expect(resolveTargetLanguage('Spanish')).toBe('Spanish') + expect(resolveSystemPrompt('translate')).toContain('Japanese로 번역') + expect( + renderInstructionPrompt('{{targetLanguage}}로 번역해줘', { text: '안녕' }), + ).toBe('Japanese로 번역해줘') + expect(SUPPORTED_TARGET_LANGUAGES).toContain('Japanese') + } finally { + spy.mockRestore() + } + }) + // 수정 대상이 아닌 일반 액션들이 각자의 프롬프트로 해석되는지 잠근다. // 이들이 refine으로 조용히 폴백하면 커스텀 경로와 같은 사고가 난다. it.each(['refine', 'summarize', 'grammar', 'expand'] as const)( diff --git a/apps/desktop/tests/main/services/local-llm-service.test.ts b/apps/desktop/tests/main/services/local-llm-service.test.ts index 4504eb9..881ae92 100644 --- a/apps/desktop/tests/main/services/local-llm-service.test.ts +++ b/apps/desktop/tests/main/services/local-llm-service.test.ts @@ -1,6 +1,8 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { ErrorCode } from '@d3ro/core/errors' -import { initInMemoryConfig, resetInMemoryConfig } from '../../../src/main/services/ConfigService' +import { configSet, initInMemoryConfig, resetInMemoryConfig } from '../../../src/main/services/ConfigService' +import { availableParallelism } from 'node:os' +import { inferenceCpuThreads } from '../../../src/main/utils/inference-cpu-budget' import { getLocalLLMService, resetLocalLLMServiceForTests, @@ -44,6 +46,36 @@ describe('LocalLLMService request lifecycle', () => { vi.useRealTimers() }) + it.each(['http://127.0.0.1:11434', 'http://192.168.1.2:11434'])( + 'applies CPU headroom across generate, warm-up streaming and chat only for loopback (%s)', async (serverUrl) => { + configSet('ollamaServerUrl', serverUrl) + const bodies: Array<{ options: { num_thread?: number }; stream: boolean }> = [] + vi.stubGlobal('fetch', vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { + const body = JSON.parse(String(init?.body)) as (typeof bodies)[number] + bodies.push(body) + if (!body.stream) return completedGenerateResponse() + const frame = String(input).endsWith('/api/chat') + ? { message: { content: 'ok' }, done: true } + : { model: 'gemma4:e4b', response: 'ok', done: true } + return new Response(completedStream([JSON.stringify(frame)]), { status: 200 }) + })) + + const service = getLocalLLMService() + await service.generate('hello') + for await (const token of service.streamGenerate('hi', { maxTokens: 1 })) void token + for await (const token of service.chatStream([{ role: 'user', content: 'hello' }])) void token + + expect(bodies).toHaveLength(3) + for (const body of bodies) { + if (serverUrl.includes('127.0.0.1')) { + expect(body.options.num_thread).toBe(inferenceCpuThreads(availableParallelism())) + } else { + expect(body.options).not.toHaveProperty('num_thread') + } + } + } + ) + it('sends the bounded default num_predict to chat requests', async () => { const cancel = vi.fn() const fetchMock = vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => { diff --git a/apps/desktop/tests/main/services/textinsert-stale-replacement.test.ts b/apps/desktop/tests/main/services/textinsert-stale-replacement.test.ts new file mode 100644 index 0000000..4fc0b83 --- /dev/null +++ b/apps/desktop/tests/main/services/textinsert-stale-replacement.test.ts @@ -0,0 +1,87 @@ +import { describe, expect, it, vi } from 'vitest' +import { TextInsertService } from '../../../src/main/services/TextInsertService' + +vi.mock('../../../src/main/services/text-insert/electron-clipboard-port', () => ({ electronClipboardPort: {} })) +vi.mock('../../../src/main/services/LoggerService', () => ({ + getLogger: () => ({ info: vi.fn(), warn: vi.fn(), error: vi.fn() }) +})) + +function deferred() { + let resolve!: () => void + const promise = new Promise((done) => { resolve = done }) + return { promise, resolve } +} + +describe('replacement under delayed input processing', () => { + it('does not delete new typing after waiting behind another insertion', async () => { + const pending = deferred() + const keyboard = { + type: vi.fn(() => pending.promise), paste: vi.fn(), backspace: vi.fn(), + replaceBeforeCaret: vi.fn(async () => true) + } + const service = new TextInsertService({ keyboard }) + let revision = 1 + const beforeApply = vi.fn() + const first = service.insertText('prior insertion', 'keyboard') + const correction = service.replaceBeforeCaret(3, 'fixed', 'keyboard', { + canApply: () => revision === 1, beforeApply, requireAtomic: true + }) + await Promise.resolve() + revision += 1 // physical input or a click while the first insertion is pending + pending.resolve() + await first + expect((await correction).success).toBe(false) + expect(keyboard.replaceBeforeCaret).not.toHaveBeenCalled() + expect(keyboard.backspace).not.toHaveBeenCalled() + expect(beforeApply).not.toHaveBeenCalled() + }) + + it('revalidates after a slow fallback module load before sending any keys', async () => { + const loading = deferred() + const started = deferred() + const keyboard = { + type: vi.fn(), paste: vi.fn(), backspace: vi.fn(), + preload: vi.fn(() => { started.resolve(); return loading.promise }) + } + const service = new TextInsertService({ keyboard }) + let current = true + const correction = service.replaceBeforeCaret(3, 'fixed', 'keyboard', { canApply: () => current }) + await started.promise + current = false + loading.resolve() + expect((await correction).success).toBe(false) + expect(keyboard.backspace).not.toHaveBeenCalled() + expect(keyboard.type).not.toHaveBeenCalled() + }) + + it('does not use separate backspaces when automatic atomic replacement is unavailable', async () => { + const keyboard = { + type: vi.fn(), paste: vi.fn(), backspace: vi.fn(), + replaceBeforeCaret: vi.fn(async () => false) + } + const service = new TextInsertService({ keyboard }) + expect((await service.replaceBeforeCaret(3, 'fixed', 'keyboard', { requireAtomic: true })).success).toBe(false) + expect(keyboard.backspace).not.toHaveBeenCalled() + expect(keyboard.type).not.toHaveBeenCalled() + }) + + it('registers synthetic keys only when the valid queued replacement actually runs', async () => { + const pending = deferred() + const order: string[] = [] + const keyboard = { + type: vi.fn(() => pending.promise), paste: vi.fn(), backspace: vi.fn(), + replaceBeforeCaret: vi.fn(async () => { order.push('replace'); return true }) + } + const service = new TextInsertService({ keyboard }) + const first = service.insertText('prior', 'keyboard') + const correction = service.replaceBeforeCaret(3, 'fixed', 'keyboard', { + canApply: () => true, beforeApply: () => { order.push('register') }, requireAtomic: true + }) + await Promise.resolve() + expect(order).toEqual([]) + pending.resolve() + await first + expect((await correction).success).toBe(true) + expect(order).toEqual(['register', 'replace']) + }) +}) diff --git a/apps/desktop/tests/main/services/typing-race-integration.test.ts b/apps/desktop/tests/main/services/typing-race-integration.test.ts new file mode 100644 index 0000000..2760f4b --- /dev/null +++ b/apps/desktop/tests/main/services/typing-race-integration.test.ts @@ -0,0 +1,96 @@ +import { describe, expect, it, vi } from 'vitest' +import { TextInsertService } from '../../../src/main/services/TextInsertService' +import { TypoCorrectionService } from '../../../src/main/services/TypoCorrectionService' +import type { TypingContext } from '../../../src/main/services/InputTelemetryService' + +vi.mock('../../../src/main/services/text-insert/electron-clipboard-port', () => ({ electronClipboardPort: {} })) +vi.mock('../../../src/main/services/ConfigService', () => ({ + configGet: (key: string) => key === 'typoAutoCorrectEnabled' ? true : + key === 'typoAutoCorrectUseModel' ? false : [], + configSet: vi.fn() +})) +vi.mock('../../../src/main/services/LoggerService', () => ({ + getLogger: () => ({ info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }) +})) + +function deferred() { + let resolve!: () => void + const promise = new Promise((done) => { resolve = done }) + return { promise, resolve } +} + +function fixture() { + const state = { text: '오늘 잘 됬어요 ', lastKeyAt: 1_000, revision: 1, nativeTick: 5, hwnd: 7 } + const prior = deferred() + const replacementDone = deferred() + const keyboard = { + type: async () => prior.promise, + paste: vi.fn(async () => undefined), + backspace: vi.fn(async (count: number) => { state.text = state.text.slice(0, -count) }), + replaceBeforeCaret: vi.fn(async (count: number, text: string) => { + state.text = state.text.slice(0, -count) + text + return true + }) + } + const insert = new TextInsertService({ keyboard }) + const typo = new TypoCorrectionService({ + replaceBeforeCaret: async (count, text, options) => { + try { return (await insert.replaceBeforeCaret(count, text, 'keyboard', options)).success } + finally { replacementDone.resolve() } + }, + lastKeyAt: () => state.lastKeyAt, + inputRevision: () => state.revision, + nativeInputTick: () => state.nativeTick, + foregroundHandle: () => state.hwnd, + expectProgrammaticKeys: vi.fn(), + expectProgrammaticInsert: () => () => undefined, + recentTexts: () => [], + warmModel: () => null, + generate: async function* () { yield '없음' } + }) + const context: TypingContext = { + prefix: state.text, fullText: state.text, caretOffset: state.text.length, + anchor: null, anchorKind: null, isPassword: false, isEditable: true, + isComposing: false, hasSelection: false, available: true, + appName: 'notepad.exe', windowTitle: 'Fixture', windowHandle: 7, + idleMs: 800, capturedAt: 1_800, inputRevision: 1, nativeInputTick: 5, + editedSinceFocus: true, typedRecently: true, caretReliable: true + } + return { state, prior, replacementDone, keyboard, insert, typo, context } +} + +describe('real correction services preserve a text sink across delayed queue interleavings', () => { + it.each(['delivered-key', 'same-millisecond-key', 'native-event-before-js'] as const)( + 'preserves the whole next Hangul word when %s arrives while correction waits', async (event) => { + const f = fixture() + const priorInsertion = f.insert.insertText('', 'keyboard') + expect(f.typo.handleTypingContext(f.context)).toBe('correcting') + await Promise.resolve() + const expected = f.state.text + '안녕하세요 ' + f.state.text = expected + // Isolate each guard: identical millisecond/native ticks must not hide the + // revision case, and a native event may precede both JS-side counters. + if (event === 'delivered-key') f.state.lastKeyAt += 100 + if (event === 'same-millisecond-key') f.state.revision += 1 + if (event === 'native-event-before-js') f.state.nativeTick += 1 + f.prior.resolve() + await priorInsertion + await f.replacementDone.promise + expect(f.state.text).toBe(expected) + expect(f.keyboard.replaceBeforeCaret).not.toHaveBeenCalled() + f.typo.dispose() + } + ) + + it('still replaces the intended typo if nothing changed during the same queue wait', async () => { + const f = fixture() + const priorInsertion = f.insert.insertText('', 'keyboard') + expect(f.typo.handleTypingContext(f.context)).toBe('correcting') + f.prior.resolve() + await priorInsertion + await f.replacementDone.promise + expect(f.state.text).toBe('오늘 잘 됐어요 ') + expect(f.keyboard.replaceBeforeCaret).toHaveBeenCalledOnce() + f.typo.dispose() + }) +}) diff --git a/apps/desktop/tests/main/services/voice-intent.test.ts b/apps/desktop/tests/main/services/voice-intent.test.ts index 0f7ec20..890c3bf 100644 --- a/apps/desktop/tests/main/services/voice-intent.test.ts +++ b/apps/desktop/tests/main/services/voice-intent.test.ts @@ -76,9 +76,19 @@ describe('제스처 입력 정규화', () => { expect(resolveGestureMode('command', false)).toBe('dictation') }) - it("'command' 는 dictation 파이프라인 fallback 이라 hold-to-talk 이다", () => { - expect(resolveGestureHoldMode('command', false)).toBe(true) + it("'command' 는 바인딩의 holdMode(토글 또는 홀드)를 그대로 따른다", () => { + expect(resolveGestureHoldMode('command', false)).toBe(false) + expect(resolveGestureHoldMode('command', true)).toBe(true) expect(resolveGestureHoldMode('dictation', true)).toBe(true) expect(resolveGestureHoldMode('hands-free', false)).toBe(false) }) + + it("holdMode=false 인 'command' 토글은 첫 press 로 시작하고 녹음 중 다음 press 로 정지한다", () => { + const commandTogglePress: VoiceGesture = { type: 'press', mode: 'dictation', holdMode: false } + const commandToggleRelease: VoiceGesture = { type: 'release', mode: 'dictation', holdMode: false } + expect(resolveVoiceIntent(commandTogglePress, 'idle')).toEqual(START_DICTATION) + expect(resolveVoiceIntent(commandToggleRelease, 'recording')).toEqual(IGNORE) + expect(resolveVoiceIntent(commandTogglePress, 'recording')).toEqual(STOP) + expect(resolveVoiceIntent(commandTogglePress, 'processing')).toEqual(IGNORE) + }) }) diff --git a/apps/desktop/tests/main/services/win32-input-activity.test.ts b/apps/desktop/tests/main/services/win32-input-activity.test.ts new file mode 100644 index 0000000..ced2661 --- /dev/null +++ b/apps/desktop/tests/main/services/win32-input-activity.test.ts @@ -0,0 +1,77 @@ +import { createRequire } from 'node:module' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +const nodeRequire = createRequire(import.meta.url) +const koffiPath = nodeRequire.resolve('koffi') +const originalKoffi = nodeRequire.cache[koffiPath] +const nativeRead = vi.fn<(info: Buffer) => boolean>() +const load = vi.fn() +let input: typeof import('../../../src/main/utils/win32-input-activity') + +vi.mock('../../../src/main/services/LoggerService', () => ({ + getLogger: () => ({ warn: vi.fn() }) +})) + +beforeEach(async () => { + vi.resetModules() + vi.spyOn(process, 'platform', 'get').mockReturnValue('win32') + nativeRead.mockReset().mockImplementation((info) => { + expect(info.length).toBe(8) + expect(info.readUInt32LE(0)).toBe(8) + info.writeUInt32LE(0xfedcba98, 4) + return true + }) + load.mockReset().mockReturnValue({ func: vi.fn(() => nativeRead) }) + nodeRequire.cache[koffiPath] = { + id: koffiPath, filename: koffiPath, loaded: true, exports: { load } + } as unknown as NodeModule + input = await import('../../../src/main/utils/win32-input-activity') +}) + +afterEach(() => { + if (originalKoffi) nodeRequire.cache[koffiPath] = originalKoffi + else delete nodeRequire.cache[koffiPath] + vi.restoreAllMocks() +}) + +describe('native input activity token', () => { + it('initializes LASTINPUTINFO.cbSize and reads an unsigned DWORD without epoch conversion', () => { + expect(input.getLastNativeInputTick()).toBe(0xfedcba98) + expect(input.getLastNativeInputTick()).toBe(0xfedcba98) + expect(nativeRead).toHaveBeenCalledTimes(2) + expect(load).toHaveBeenCalledTimes(1) + expect(load).toHaveBeenCalledWith('user32.dll') + }) + + it('preserves zero after tick wraparound as a valid token', () => { + nativeRead.mockImplementation((info) => { + info.writeUInt32LE(0, 4) + return true + }) + expect(input.getLastNativeInputTick()).toBe(0) + }) + + it('returns null on native failure instead of treating zeroed memory as an idle token', () => { + nativeRead.mockReturnValueOnce(false) + expect(input.getLastNativeInputTick()).toBeNull() + expect(input.getLastNativeInputTick()).toBe(0xfedcba98) + }) + + it('does not load a Windows DLL on other platforms', () => { + vi.spyOn(process, 'platform', 'get').mockReturnValue('linux') + expect(input.getLastNativeInputTick()).toBeNull() + expect(load).not.toHaveBeenCalled() + }) + + it('fails closed and avoids repeated native loads when koffi is unavailable', () => { + load.mockImplementation(() => { throw new Error('unavailable') }) + expect(input.getLastNativeInputTick()).toBeNull() + expect(input.getLastNativeInputTick()).toBeNull() + expect(load).toHaveBeenCalledTimes(1) + }) + + it('returns null when native invocation throws', () => { + nativeRead.mockImplementation(() => { throw new Error('native failure') }) + expect(input.getLastNativeInputTick()).toBeNull() + }) +}) diff --git a/apps/desktop/tests/main/services/win32-unicode-input.test.ts b/apps/desktop/tests/main/services/win32-unicode-input.test.ts new file mode 100644 index 0000000..a62988f --- /dev/null +++ b/apps/desktop/tests/main/services/win32-unicode-input.test.ts @@ -0,0 +1,124 @@ +import { createRequire } from 'node:module' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { ErrorCode } from '@d3ro/core/errors' + +const nodeRequire = createRequire(import.meta.url) +const koffiPath = nodeRequire.resolve('koffi') +const originalKoffi = nodeRequire.cache[koffiPath] +const sendInput = vi.fn<(count: number, buffer: Buffer, size: number) => number>() +let input: typeof import('../../../src/main/utils/win32-unicode-input') + +beforeEach(async () => { + vi.resetModules() + vi.spyOn(process, 'platform', 'get').mockReturnValue('win32') + sendInput.mockReset().mockImplementation((count) => count) + // Production loads koffi with CommonJS require; vi.mock cannot intercept it. + // Replace its cache entry before importing so no native library can receive keys. + nodeRequire.cache[koffiPath] = { + id: koffiPath, + filename: koffiPath, + loaded: true, + exports: { + struct: vi.fn((name: string) => name), + union: vi.fn((name: string) => name), + sizeof: vi.fn(() => 40), + offsetof: vi.fn(() => 8), + load: vi.fn(() => ({ func: vi.fn(() => sendInput) })) + } + } as unknown as NodeModule + input = await import('../../../src/main/utils/win32-unicode-input') +}) + +afterEach(() => { + if (originalKoffi) nodeRequire.cache[koffiPath] = originalKoffi + else delete nodeRequire.cache[koffiPath] + vi.restoreAllMocks() +}) + +function events(): Array<{ vk: number; scan: number; flags: number }> { + const [count, buffer, size] = sendInput.mock.calls[0] + expect(size).toBe(40) + expect(buffer.length).toBe(count * size) + return Array.from({ length: count }, (_, index) => { + const base = index * size + expect(buffer.readUInt32LE(base)).toBe(1) + return { + vk: buffer.readUInt16LE(base + 8), + scan: buffer.readUInt16LE(base + 10), + flags: buffer.readUInt32LE(base + 12) + } + }) +} + +describe('Win32 input batches', () => { + it('encodes Hangul and both UTF-16 surrogate units as Unicode down/up pairs', () => { + expect(input.sendWin32UnicodeText('안😀')).toBe(true) + expect(sendInput).toHaveBeenCalledTimes(1) + expect(events()).toEqual([ + { vk: 0, scan: 0xc548, flags: 4 }, { vk: 0, scan: 0xc548, flags: 6 }, + { vk: 0, scan: 0xd83d, flags: 4 }, { vk: 0, scan: 0xd83d, flags: 6 }, + { vk: 0, scan: 0xde00, flags: 4 }, { vk: 0, scan: 0xde00, flags: 6 } + ]) + }) + + it('puts composition commit, requested deletion plus commit space, and replacement in one batch', () => { + expect(input.sendWin32UnicodeReplacement(2, '안', { commitComposition: true })).toBe(true) + expect(sendInput).toHaveBeenCalledTimes(1) + expect(events()).toEqual([ + { vk: 0x20, scan: 0, flags: 0 }, { vk: 0x20, scan: 0, flags: 2 }, + { vk: 8, scan: 0, flags: 0 }, { vk: 8, scan: 0, flags: 2 }, + { vk: 8, scan: 0, flags: 0 }, { vk: 8, scan: 0, flags: 2 }, + { vk: 8, scan: 0, flags: 0 }, { vk: 8, scan: 0, flags: 2 }, + { vk: 0, scan: 0xc548, flags: 4 }, { vk: 0, scan: 0xc548, flags: 6 } + ]) + }) + + it('permits fallback only when the native call reports no inserted events', () => { + sendInput.mockReturnValue(0) + expect(input.sendWin32UnicodeText('hello')).toBe(false) + expect(input.sendWin32UnicodeReplacement(2, '안')).toBe(false) + }) + + it.each([1, 2, 4, 5])('rejects a replacement after %i of 6 events instead of replaying deletions', (sent) => { + sendInput.mockReturnValue(sent) + expect(() => input.sendWin32UnicodeReplacement(2, '안')).toThrow(`${sent}/6 events; retry blocked`) + expect(sendInput).toHaveBeenCalledTimes(1) + }) + + it('rejects partially inserted ordinary text instead of duplicating it', () => { + sendInput.mockReturnValue(2) + expect(() => input.sendWin32UnicodeText('안녕')).toThrow('2/4 events; retry blocked') + expect(sendInput).toHaveBeenCalledTimes(1) + }) + + it('blocks retry when the native result is unknown', () => { + sendInput.mockImplementation(() => { throw new Error('native failure') }) + expect(() => input.sendWin32UnicodeReplacement(2, '안')).toThrow('unknown insertion result') + expect(() => input.sendWin32UnicodeText('안')).toThrow('unknown insertion result') + expect(sendInput).toHaveBeenCalledTimes(2) + }) + + it('propagates partial insertion through the actual keyboard adapter and service without fallback', async () => { + const { NutKeyboardPort } = await import('../../../src/main/services/text-insert/nut-keyboard-port') + const { TextInsertService } = await import('../../../src/main/services/TextInsertService') + const keyboard = new NutKeyboardPort('win32') + const backspace = vi.spyOn(keyboard, 'backspace') + const type = vi.spyOn(keyboard, 'type') + const service = new TextInsertService({ keyboard, platform: 'win32' }) + const completed = vi.fn() + service.on('insert-completed', completed) + sendInput.mockReturnValue(2) + + await expect(service.replaceBeforeCaret(2, '안', 'keyboard')).rejects.toMatchObject({ + code: ErrorCode.TextInsertKeySimulationFailed + }) + expect(sendInput).toHaveBeenCalledTimes(1) + expect(backspace).not.toHaveBeenCalled() + expect(type).not.toHaveBeenCalled() + expect(completed).not.toHaveBeenCalled() + + sendInput.mockImplementation((count) => count) + await expect(service.replaceBeforeCaret(2, '안', 'keyboard')).resolves.toMatchObject({ success: true }) + expect(completed).toHaveBeenCalledTimes(1) + }) +}) diff --git a/apps/desktop/tests/main/utils/inference-cpu-budget.test.ts b/apps/desktop/tests/main/utils/inference-cpu-budget.test.ts new file mode 100644 index 0000000..bdcd0de --- /dev/null +++ b/apps/desktop/tests/main/utils/inference-cpu-budget.test.ts @@ -0,0 +1,27 @@ +import { describe, expect, it } from 'vitest' +import { inferenceCpuThreads, localInferenceCpuOptions } from '../../../src/main/utils/inference-cpu-budget' + +describe('local inference scheduling budget', () => { + it.each([[1, 1], [2, 1], [3, 1], [4, 2], [6, 4], [8, 4], [16, 4], [64, 4]])( + 'leaves foreground headroom on %i available CPUs (%i workers)', (cpus, expected) => { + expect(inferenceCpuThreads(cpus)).toBe(expected) + } + ) + + it.each([0, -1, Number.NaN, Number.POSITIVE_INFINITY])('uses one worker for invalid capacity %s', (cpus) => { + expect(inferenceCpuThreads(cpus)).toBe(1) + }) + + it.each(['http://localhost:11434', 'http://localhost.:11434', 'http://127.0.0.1:11434', 'http://127.0.0.2:11434', 'http://[::1]:11434'])( + 'bounds the local engine at %s', (url) => { + expect(localInferenceCpuOptions(url).num_thread).toBeGreaterThanOrEqual(1) + expect(localInferenceCpuOptions(url).num_thread).toBeLessThanOrEqual(4) + } + ) + + it.each(['https://llm.example.com', 'http://192.168.1.2:11434', 'http://localhost.example.com', 'invalid'])( + 'does not infer a remote engine budget from this PC: %s', (url) => { + expect(localInferenceCpuOptions(url)).toEqual({}) + } + ) +}) diff --git a/apps/desktop/tests/red/harness.ts b/apps/desktop/tests/red/harness.ts index 133616c..8992264 100644 --- a/apps/desktop/tests/red/harness.ts +++ b/apps/desktop/tests/red/harness.ts @@ -32,8 +32,11 @@ import { resetCloudSyncServiceForTests } from '../../src/main/services/CloudSync import { resetTTSPlaybackServiceForTests } from '../../src/main/services/TTSPlaybackService' import { resetMeetingSummaryServiceForTests } from '../../src/main/services/MeetingSummaryService' import { resetLocalLLMServiceForTests } from '../../src/main/services/LocalLLMService' -import { resetLocalSTTServiceForTests } from '../../src/main/services/LocalSTTService' -import type { IPCResult } from '@d3ro/core/errors' +import { + LocalSTTService, + resetLocalSTTServiceForTests, +} from '../../src/main/services/LocalSTTService' +import { D3ROError, ErrorCode, type IPCResult } from '@d3ro/core/errors' import type { NewHistory } from '../../src/main/db/schema' const ipcHandlers = new Map unknown>() @@ -103,6 +106,8 @@ export function resetAllSingletons(): void { export function useRedHarness(): void { let testdb: ReturnType | null = null + let initSpy: ReturnType | null = null + let ensureSidecarSpy: ReturnType | null = null beforeEach(() => { testdb = createTestDb() @@ -110,9 +115,28 @@ export function useRedHarness(): void { initInMemoryConfig() resetAllSingletons() resetIpcHandlers() + vi.stubGlobal( + 'fetch', + vi.fn(async () => { + throw new TypeError('fetch failed (red harness offline default)') + }), + ) + const sidecarDisabledError = (): Promise => + Promise.reject( + new D3ROError( + ErrorCode.STTSidecarSpawnFailed, + 'red harness: real Python sidecar spawn is disabled in usecase tests', + ), + ) + initSpy = vi.spyOn(LocalSTTService.prototype, 'initialize').mockImplementation(sidecarDisabledError) + ensureSidecarSpy = vi.spyOn(LocalSTTService.prototype, 'ensureSidecar').mockImplementation(sidecarDisabledError) }) afterEach(() => { + initSpy?.mockRestore() + ensureSidecarSpy?.mockRestore() + initSpy = null + ensureSidecarSpy = null resetAllSingletons() unbindTestDatabase() testdb?.close() diff --git a/apps/desktop/tests/unit/AdMediationEngine.spec.ts b/apps/desktop/tests/unit/AdMediationEngine.spec.ts index 531e8cd..876663a 100644 --- a/apps/desktop/tests/unit/AdMediationEngine.spec.ts +++ b/apps/desktop/tests/unit/AdMediationEngine.spec.ts @@ -1,7 +1,8 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' -import type { AdMediationConfig } from '@d3ro/core/types' +import { Feature, type AdMediationConfig } from '@d3ro/core/types' import { getAdMediationEngine } from '../../src/main/services/ads/AdMediationEngine' import { getAdSettlementService } from '../../src/main/services/ads/AdSettlementService' +import { getLicenseService } from '../../src/main/services/LicenseService' import { EthicalAdsAdapter } from '../../src/main/services/ads/EthicalAdsAdapter' import { CarbonAdsAdapter } from '../../src/main/services/ads/CarbonAdsAdapter' import { PlaywireAdapter } from '../../src/main/services/ads/PlaywireAdapter' @@ -275,3 +276,88 @@ describe('AdSettlementService fail-closed ledger', () => { expect(settlement.getRevenueStats('2026-08')).toEqual(before) }) }) + +describe('GAP-ADS-02 rewarded quota integration', () => { + const engine = getAdMediationEngine() + const knownNetworks = engine.getConfig().networks + + it('grants LicenseService bonus quota on verified rewarded ad completion and blocks replay', async () => { + const license = getLicenseService() + const baseQuota = license.getUsage(Feature.PREMIUM_LLM) + + engine.setConfig({ + rewardTokensAmount: 50, + rewardCooldownSeconds: 0, + networks: knownNetworks.map((n) => + n.id === 'direct_sponsor' + ? { + ...n, + enabled: true, + endpointUrl: 'https://ads.example.com', + apiSecret: 'secret', + } + : { ...n, enabled: false }, + ), + }) + + const fetchMock = vi + .fn() + .mockResolvedValueOnce( + new Response( + JSON.stringify({ + bidEcpm: 15, + creative: { + id: 'rewarded-ad-1', + title: 'Rewarded Video', + advertiserName: 'D3RO Partner', + clickUrl: 'https://example.com/reward', + format: 'rewarded_video', + rewardTokens: 50, + durationSeconds: 15, + }, + }), + { status: 200 }, + ), + ) + .mockResolvedValueOnce(new Response(JSON.stringify({ ok: true }), { status: 200 })) + .mockResolvedValueOnce( + new Response(JSON.stringify({ success: true, tokenReward: 50 }), { status: 200 }), + ) + + vi.stubGlobal('fetch', fetchMock) + try { + const auction = await engine.runAuction({ + placement: 'rewarded_video_quota', + format: 'rewarded_video', + floorEcpm: 4, + }) + expect(auction.winner?.id).toBe('rewarded-ad-1') + + engine.recordImpression({ + adId: 'rewarded-ad-1', + format: 'rewarded_video', + network: 'direct_sponsor', + earnedEcpm: auction.winningBidEcpm, + }) + + const claim = await engine.claimReward('rewarded-ad-1', 'direct_sponsor', 'nonce-1') + expect(claim.success).toBe(true) + expect(claim.tokensAdded).toBe(50) + expect(claim.newTotalQuota).toBe(baseQuota.limit + 50) + expect(license.getUsage(Feature.PREMIUM_LLM).limit).toBe(baseQuota.limit + 50) + + // Replay with same adId or nonce is rejected + const replay = await engine.claimReward('rewarded-ad-1', 'direct_sponsor', 'nonce-1') + expect(replay).toEqual({ + success: false, + tokensAdded: 0, + newTotalQuota: 0, + }) + } finally { + vi.unstubAllGlobals() + engine.setConfig({ + networks: knownNetworks.map((n) => ({ ...n, enabled: false })), + }) + } + }) +}) diff --git a/apps/desktop/tests/unit/desktop-ux-gaps.test.ts b/apps/desktop/tests/unit/desktop-ux-gaps.test.ts new file mode 100644 index 0000000..75f141a --- /dev/null +++ b/apps/desktop/tests/unit/desktop-ux-gaps.test.ts @@ -0,0 +1,368 @@ +// apps/desktop/tests/unit/desktop-ux-gaps.test.ts +// Unit tests for Desktop UX & Main backlog items: +// GAP-UX-06, GAP-KEY-03, GAP-ADS-02, GAP-UX-01, GAP-UX-02, GAP-UX-03 + +import fs from 'fs' +import path from 'path' +import os from 'os' +import { describe, it, expect, vi } from 'vitest' +import { EventEmitter } from 'events' +import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' +import { ErrorCode, ipcError, ipcSuccess } from '@d3ro/core/errors' +import { Feature } from '@d3ro/core/types' +import { STT_PROVIDERS_META } from '../../src/main/services/stt/STTManager' +import { registerAudioHandlers } from '../../src/main/ipc/audio-handlers' +import { getAudioCaptureService } from '../../src/main/services/AudioCaptureService' +import { getDictionaryService } from '../../src/main/services/DictionaryService' +import { getVoiceConversationService } from '../../src/main/services/VoiceConversationService' +import { getTTSPlaybackService } from '../../src/main/services/TTSPlaybackService' +import { registerVoiceConversationHandlers } from '../../src/main/ipc/voice-conversation-handlers' +import { resolveRagAddError } from '../../src/renderer/pages/KnowledgeBasePage' +import { getVoiceModeService } from '../../src/main/services/VoiceModeService' +import { getLicenseService } from '../../src/main/services/LicenseService' +import { getMeetingModeService } from '../../src/main/services/MeetingModeService' +import { registerMeetingModeHandlers } from '../../src/main/ipc/meeting-mode-handlers' +import { syncNotepadMemo, loadNotepad } from '../../src/renderer/components/meeting/meeting-notepad-storage' +import { getHistoryService } from '../../src/main/services/HistoryService' +import { registerHistoryHandlers } from '../../src/main/ipc/history-handlers' +import { getMemoService } from '../../src/main/services/MemoService' +import { + MEETING_SUMMARY_PROMPT, + extractTranscriptCitations, + parseCitationTimestampMs, +} from '../../src/main/services/MeetingSummaryService' +import { splitTextWithCitations } from '../../src/renderer/components/meeting/MarkdownRenderer' +import { getDatabase } from '../../src/main/db' +import { meetingSessions } from '../../src/main/db/schema' +import { historyInput, invokeIpc, useRedHarness } from '../red/harness' + +useRedHarness() + +describe('GAP-UX-06: Desktop Main Cleanup (sub-items a–f)', () => { + it('(a) STT_PROVIDERS_META uses neutral technical descriptions without marketing strings', () => { + const bannedTerms = ['100% 완전 오프라인', 'Industry Benchmark', 'Ultra Fast', 'Enterprise Ready', 'K-Language Best'] + for (const meta of STT_PROVIDERS_META) { + for (const banned of bannedTerms) { + expect(meta.description).not.toContain(banned) + expect(meta.badge ?? '').not.toContain(banned) + } + expect(meta.description.length).toBeGreaterThan(5) + } + }) + + it('(b) AUDIO.TEST_DEVICE forwards the requested deviceId to AudioCaptureService.acquire', async () => { + registerAudioHandlers() + const audioService = getAudioCaptureService() + const fakeStream = new EventEmitter() + const releaseMock = vi.fn().mockResolvedValue(undefined) + const acquireSpy = vi.spyOn(audioService, 'acquire').mockResolvedValue({ + stream: fakeStream, + sampleRate: 16000, + channels: 1, + release: releaseMock, + }) + + try { + const res = await invokeIpc<{ success: boolean }>(IPC_CHANNELS.AUDIO.TEST_DEVICE, { + deviceId: 'usb-mic-device-42', + durationMs: 50, + }) + expect(res.success).toBe(true) + expect(acquireSpy).toHaveBeenCalledWith('usb-mic-device-42') + await invokeIpc(IPC_CHANNELS.AUDIO.STOP_TEST) + } finally { + acquireSpy.mockRestore() + } + }) + + it('(c) DictionaryService includes pronunciation in getPromptHints and applyDictionaryCorrections replaces & increments usageCount', () => { + const dict = getDictionaryService() + const added = dict.add({ + word: 'D3ROVoice', + pronunciation: '디쓰리로보이스, 디쓰리로', + category: 'general', + }) + + const hints = dict.getPromptHints() + expect(hints).toContain('D3ROVoice (디쓰리로보이스, 디쓰리로)') + + const corrected = dict.applyDictionaryCorrections('오늘 디쓰리로보이스 테스트를 진행합니다') + expect(corrected.text).toBe('오늘 D3ROVoice 테스트를 진행합니다') + expect(corrected.matchedIds).toEqual([added.id]) + + const page = dict.list({ page: 0, pageSize: 10 }) + const entry = page.entries.find((e) => e.id === added.id) + expect(entry?.usageCount).toBe(1) + }) + + it('(d) VoiceConversationService.replayMessage speaks assistant message via TTSPlaybackService and is exposed via IPC', async () => { + registerVoiceConversationHandlers() + const tts = getTTSPlaybackService() + const speakSpy = vi.spyOn(tts, 'speakSentences').mockResolvedValue(undefined) + + try { + const res = await invokeIpc(IPC_CHANNELS.VOICE_CONVERSATION.REPLAY_MESSAGE ?? 'voiceConversation:replayMessage', { + text: '다시 읽어줄 응답입니다.', + }) + expect(res.success).toBe(true) + expect(speakSpy).toHaveBeenCalledWith(['다시 읽어줄 응답입니다.']) + + await getVoiceConversationService().replayMessage('직접 호출 테스트') + expect(speakSpy).toHaveBeenCalledWith(['직접 호출 테스트']) + } finally { + speakSpy.mockRestore() + } + }) + + it('(e) preload instruction.getAll is typed as CustomInstruction[]', () => { + const preloadSrc = fs.readFileSync( + path.resolve(__dirname, '../../src/preload/index.ts'), + 'utf-8', + ) + expect(preloadSrc).toContain('invoke(IPC_CHANNELS.INSTRUCTION.GET_ALL)') + }) + + it('(f) resolveRagAddError checks structured details.cancelled / error code instead of fragile string matching', () => { + const cancelledResp = ipcError(ErrorCode.RAGIndexingFailed, 'File selection cancelled', { + cancelled: true, + reason: 'dialog_cancelled', + }) + expect(resolveRagAddError(cancelledResp)).toBeNull() + + const realErrorResp = ipcError(ErrorCode.RAGIndexingFailed, 'Failed to parse PDF', { + cancelled: false, + }) + expect(resolveRagAddError(realErrorResp)).toBe('Failed to parse PDF') + + const okResp = ipcSuccess({ + id: 'doc-1', + title: 'Test', + filePath: '/tmp/test.txt', + chunkCount: 2, + createdAt: Date.now(), + }) + expect(resolveRagAddError(okResp)).toBeNull() + }) +}) + +describe('GAP-KEY-03: command Keybinding Action Dedicated Behavior', () => { + it('_resolveHoldMode honors specHoldMode for command action and _resolveLlmRoute upgrades none to refine', async () => { + const { configSet } = await import('../../src/main/services/ConfigService') + configSet('llmBackend', 'online') + configSet('defaultLLMAction', 'none') + + const vm = getVoiceModeService() + expect(vm._resolveHoldMode('command', false)).toBe(false) + expect(vm._resolveHoldMode('command', true)).toBe(true) + + const resolveRoute = ( + vm as unknown as { + _resolveLlmRoute: ( + overrideId: string | null, + actionId?: 'dictation' | 'command' | 'hands-free', + ) => { kind: string; action?: string; reason?: string } + } + )._resolveLlmRoute.bind(vm) + + expect(resolveRoute(null, 'dictation')).toEqual({ kind: 'insert-raw', reason: 'none' }) + expect(resolveRoute(null, 'command')).toEqual({ kind: 'action', action: 'refine' }) + }) +}) + +describe('GAP-ADS-02: LicenseService.grantRewardQuota', () => { + it('increments bonus quota for PREMIUM_LLM and deduplicates nonces', () => { + const license = getLicenseService() + const before = license.getUsage(Feature.PREMIUM_LLM) + + const first = license.grantRewardQuota(50, 'nonce-abc') + expect(first.granted).toBe(true) + expect(first.tokensAdded).toBe(50) + expect(first.bonusQuota).toBe(50) + expect(first.newTotalQuota).toBe(before.limit + 50) + + const duplicate = license.grantRewardQuota(50, 'nonce-abc') + expect(duplicate.granted).toBe(false) + expect(duplicate.tokensAdded).toBe(0) + expect(license.getUsage(Feature.PREMIUM_LLM).limit).toBe(before.limit + 50) + }) +}) + +describe('GAP-UX-01: Meeting Post-Meeting addMemo & Notepad Sync', () => { + it('allows adding a memo to an ended meeting session by meetingId and syncs via syncNotepadMemo', async () => { + registerMeetingModeHandlers() + const now = Date.now() + getDatabase() + .insert(meetingSessions) + .values({ + id: 'ended-session-1', + title: '종료된 회의', + status: 'completed', + startedAt: now - 60_000, + endedAt: now, + durationMs: 60_000, + rawTranscript: '[00:10] 안녕하세요', + createdAt: now - 60_000, + updatedAt: now, + }) + .run() + + const memo = await getMeetingModeService().addMemo('회의 종료 후 추가한 메모', 'ended-session-1') + expect(memo.sessionId).toBe('ended-session-1') + expect(memo.content).toBe('회의 종료 후 추가한 메모') + expect(memo.timestampMs).toBe(60_000) + + const detail = getMeetingModeService().getSession('ended-session-1') + expect(detail.memos).toHaveLength(1) + expect(detail.memos[0].content).toBe('회의 종료 후 추가한 메모') + + // Test notepad storage + IPC sync helper + const fakeStorageMap = new Map() + const fakeStorage = { + getItem: (k: string) => fakeStorageMap.get(k) ?? null, + setItem: (k: string, v: string) => { + fakeStorageMap.set(k, v) + }, + removeItem: (k: string) => { + fakeStorageMap.delete(k) + }, + } + + const synced = await syncNotepadMemo( + fakeStorage, + 'ended-session-1', + '노트패드에서 동기화된 메모', + { + addMemo: (params) => + invokeIpc(IPC_CHANNELS.MEETING_MODE.ADD_MEMO, params), + }, + ) + expect(loadNotepad(fakeStorage, 'ended-session-1')).toBe('노트패드에서 동기화된 메모') + expect(synced?.content).toBe('노트패드에서 동기화된 메모') + expect(getMeetingModeService().getSession('ended-session-1').memos).toHaveLength(2) + }) +}) + +describe('GAP-UX-02: History SQLite Multi-Filter, Update, and Markdown Export', () => { + it('filters HistoryService.getAll in SQLite by mode, isFavorite, tag, search, and beforeCreatedAt', () => { + const historySvc = getHistoryService() + const memoSvc = getMemoService() + + const e1 = historySvc.create( + historyInput({ + originalText: '알파 프로젝트 일정 논의', + mode: 'dictation', + isFavorite: true, + }), + ) + const e2 = historySvc.create( + historyInput({ + originalText: '알파 프로젝트 번역 테스트', + mode: 'translate', + isFavorite: true, + }), + ) + historySvc.create( + historyInput({ + originalText: '베타 프로젝트 일반 메모', + mode: 'dictation', + isFavorite: false, + }), + ) + + memoSvc.addTag(e1.id, 'alpha') + memoSvc.addTag(e2.id, 'alpha') + + // Object caller with combined filters + const filtered = historySvc.getAll({ + page: 0, + pageSize: 10, + mode: 'dictation', + isFavorite: true, + tag: 'alpha', + search: '일정', + }) + expect(filtered.total).toBe(1) + expect(filtered.entries[0].id).toBe(e1.id) + + // Backward-compatible positional caller (limit, offset, search) + const positional = historySvc.getAll(10, 0, '알파') + expect(positional.total).toBe(2) + }) + + it('updates history entry title and text via HistoryService.update and history:update IPC', async () => { + registerHistoryHandlers() + const historySvc = getHistoryService() + const created = historySvc.create( + historyInput({ + originalText: '초기 원문 텍스트', + polishedText: '초기 다듬은 텍스트', + }), + ) + + const res = await invokeIpc<{ id: string; title: string | null; polishedText: string | null }>( + 'history:update', + { + id: created.id, + title: '수정된 제목', + polishedText: '수정된 본문 내용입니다', + }, + ) + expect(res.success).toBe(true) + if (res.success) { + expect(res.data.title).toBe('수정된 제목') + expect(res.data.polishedText).toBe('수정된 본문 내용입니다') + } + + const reloaded = historySvc.getById(created.id) + expect(reloaded?.title).toBe('수정된 제목') + expect(reloaded?.polishedText).toBe('수정된 본문 내용입니다') + expect(reloaded?.wordCount).toBe(3) + }) + + it('exports untagged history records and supports custom savePath in MemoService.exportMarkdown', () => { + const historySvc = getHistoryService() + historySvc.create( + historyInput({ + title: '태그 없는 메모', + originalText: '태그가 없어도 내보내기에 포함됩니다', + }), + ) + + const customPath = path.join(os.tmpdir(), `d3ro-memo-export-test-${Date.now()}.md`) + try { + const outPath = getMemoService().exportMarkdown({ + format: 'markdown', + savePath: customPath, + }) + expect(outPath).toBe(path.resolve(customPath)) + const content = fs.readFileSync(outPath, 'utf-8') + expect(content).toContain('## Untagged') + expect(content).toContain('태그가 없어도 내보내기에 포함됩니다') + } finally { + if (fs.existsSync(customPath)) fs.unlinkSync(customPath) + } + }) +}) + +describe('GAP-UX-03: Meeting Summary Transcript Citations', () => { + it('parses [MM:SS] and [HH:MM:SS] citations and splits summary text into clickable citation parts', () => { + expect(MEETING_SUMMARY_PROMPT).toContain('[MM:SS]') + expect(parseCitationTimestampMs('[01:25]')).toBe(85_000) + expect(parseCitationTimestampMs('[01:02:03]')).toBe(3_723_000) + expect(parseCitationTimestampMs('[invalid]')).toBeNull() + + const sampleMarkdown = `## 요약\n출시 일정을 확정했습니다 [02:15].\n\n## 핵심 결정사항\n- 11월 정식 배포 [05:40]` + const citations = extractTranscriptCitations(sampleMarkdown) + expect(citations).toEqual([ + { label: '[02:15]', timestampMs: 135_000 }, + { label: '[05:40]', timestampMs: 340_000 }, + ]) + + const parts = splitTextWithCitations('출시 일정을 확정했습니다 [02:15].') + expect(parts).toEqual([ + { type: 'text', value: '출시 일정을 확정했습니다 ' }, + { type: 'citation', label: '[02:15]', timestampMs: 135_000 }, + { type: 'text', value: '.' }, + ]) + }) +}) diff --git a/apps/desktop/tests/unit/renderer-tools-model.test.ts b/apps/desktop/tests/unit/renderer-tools-model.test.ts index ecfd538..d37808d 100644 --- a/apps/desktop/tests/unit/renderer-tools-model.test.ts +++ b/apps/desktop/tests/unit/renderer-tools-model.test.ts @@ -9,10 +9,12 @@ import { describe, expect, it } from 'vitest' import { COMMAND_NONE, commandSelection, + commandSelections, commandTestRequest, conversationBadge, documentIndexLabelKey, llmActionLabelKey, + removeInstructionFromActiveIds, resolveCommandPipeline, withoutHidden, isInstructionInUse, @@ -32,6 +34,21 @@ describe('resolveCommandPipeline', () => { const p = resolveCommandPipeline({ defaultLLMAction: 'custom', activeInstructionId: 'i1' }, list) expect(p).toEqual({ kind: 'instruction', instruction: polish }) expect(commandSelection(p)).toBe('i1') + expect(commandSelections(p)).toEqual(['i1']) + }) + + it("'custom' + 복수 지시문('i2,i1')이면 선택한 순서대로 다단계 파이프라인을 구성한다", () => { + const p = resolveCommandPipeline({ defaultLLMAction: 'custom', activeInstructionId: 'i2,i1' }, list) + expect(p).toEqual({ kind: 'instructions', instructions: [list[1], polish] }) + expect(commandSelection(p)).toBe('i2,i1') + expect(commandSelections(p)).toEqual(['i2', 'i1']) + const testReq = commandTestRequest(p, ' 어 오늘 회의 내용 정리 ') + expect(testReq.ok).toBe(true) + if (testReq.ok) { + expect(testReq.params.action).toBe('custom') + expect(testReq.params.customPrompt).toContain('[1단계: 요약]') + expect(testReq.params.customPrompt).toContain('[2단계: 정중하게]') + } }) it("'custom' 인데 지시문이 지워졌으면 '선택 안 됨'으로 정직하게", () => { @@ -98,8 +115,10 @@ describe('withoutHidden / documentIndexLabelKey', () => { }) describe('isInstructionInUse — 명령 삭제 확정 시점의 설정으로 판단', () => { - it('지운 명령이 아직 선택돼 있을 때만 true', () => { + it('지운 명령이 아직 선택돼 있을 때만 true (단일 및 복수 파이프라인 포함)', () => { expect(isInstructionInUse({ defaultLLMAction: 'custom', activeInstructionId: 'x' }, 'x')).toBe(true) + expect(isInstructionInUse({ defaultLLMAction: 'custom', activeInstructionId: 'y,x' }, 'x')).toBe(true) + expect(removeInstructionFromActiveIds('y,x', 'x')).toEqual(['y']) // 되돌리기 알림 동안 다른 명령을 골랐다 — 덮으면 안 된다 expect(isInstructionInUse({ defaultLLMAction: 'custom', activeInstructionId: 'y' }, 'x')).toBe(false) expect(isInstructionInUse({ defaultLLMAction: 'refine', activeInstructionId: 'x' }, 'x')).toBe(false) diff --git a/apps/desktop/tsconfig.node.json b/apps/desktop/tsconfig.node.json index 802bcc2..74c8647 100644 --- a/apps/desktop/tsconfig.node.json +++ b/apps/desktop/tsconfig.node.json @@ -1,8 +1,13 @@ { "extends": "@electron-toolkit/tsconfig/tsconfig.node.json", "compilerOptions": { - "composite": true, + "composite": false, + "incremental": false, + "noEmit": true, + "declaration": false, + "declarationMap": false, "outDir": "./out", + "rootDir": "../..", "baseUrl": ".", "paths": { "@d3ro/core": ["../../packages/core/src/index.ts"], diff --git a/apps/desktop/tsconfig.web.json b/apps/desktop/tsconfig.web.json index 695b721..a5ba9df 100644 --- a/apps/desktop/tsconfig.web.json +++ b/apps/desktop/tsconfig.web.json @@ -1,8 +1,13 @@ { "extends": "@electron-toolkit/tsconfig/tsconfig.web.json", "compilerOptions": { - "composite": true, + "composite": false, + "incremental": false, + "noEmit": true, + "declaration": false, + "declarationMap": false, "outDir": "./out", + "rootDir": "../..", "baseUrl": ".", "paths": { "@d3ro/core": ["../../packages/core/src/index.ts"], @@ -22,6 +27,7 @@ "skipLibCheck": true }, "include": [ - "src/renderer/**/*" + "src/renderer/**/*", + "src/preload/**/*" ] } diff --git a/apps/mobile-rn/__tests__/entitlement.test.ts b/apps/mobile-rn/__tests__/entitlement.test.ts index 77b1979..99add98 100644 --- a/apps/mobile-rn/__tests__/entitlement.test.ts +++ b/apps/mobile-rn/__tests__/entitlement.test.ts @@ -1,4 +1,6 @@ +import { Feature } from '@d3ro/core/types' import { normalizeEntitlement } from '../src/lib/entitlement-context' +import { resolveWebEntitlement } from '../../web/src/lib/entitlement' describe('normalizeEntitlement', () => { const now = new Date('2026-08-21T00:00:00Z') @@ -16,6 +18,7 @@ describe('normalizeEntitlement', () => { expect(snapshot.adFree).toBe(false) expect(snapshot.overageCredits).toBe(50) expect(snapshot.usageToday.stt_transcribe).toBe(3) + expect(snapshot.features).not.toContain(Feature.TEAM_WORKSPACE) }) it('서버가 확인한 유효 paid period만 광고 제거 권한을 준다', () => { @@ -34,7 +37,10 @@ describe('normalizeEntitlement', () => { current_period_end: '2026-08-20T00:00:00Z', }, [], [], now) + expect(active.tier).toBe('pro') expect(active.adFree).toBe(true) + expect(active.features).toContain(Feature.TEAM_WORKSPACE) + expect(expired.tier).toBe('free') expect(expired.adFree).toBe(false) }) @@ -61,11 +67,41 @@ describe('normalizeEntitlement', () => { product_id: 'unknown', purchase_state: 'purchased', verified_at: 'invalid', - }], now) + }], now, 'manager') + expect(snapshot.tier).toBe('pro_plus') expect(snapshot.adFree).toBe(true) + expect(snapshot.adminRole).toBe('manager') expect(snapshot.autoRenewing).toBe(false) expect(snapshot.purchases).toHaveLength(1) expect(snapshot.purchases[0].productId).toBe('d3ro_voice_pro_plus_monthly') }) + + it('웹 구독과 역할도 @d3ro/core resolveEntitlement 기준으로 정규화한다', () => { + const activeWeb = resolveWebEntitlement({ + tier: 'pro_plus', + status: 'active', + provider: 'none', + payment_provider: 'payple', + current_period_end: '2026-09-21T00:00:00Z', + overage_credits: 120, + }, { supabaseRole: 'admin', now }) + + expect(activeWeb.tier).toBe('pro_plus') + expect(activeWeb.provider).toBe('payple') + expect(activeWeb.adFree).toBe(true) + expect(activeWeb.adminRole).toBe('admin') + expect(activeWeb.adminCapabilities.canAdmin).toBe(true) + + const expiredWeb = resolveWebEntitlement({ + tier: 'pro', + status: 'active', + provider: 'payple', + current_period_end: '2026-08-01T00:00:00Z', + }, { now }) + + expect(expiredWeb.tier).toBe('free') + expect(expiredWeb.rawTier).toBe('pro') + expect(expiredWeb.adFree).toBe(false) + }) }) diff --git a/apps/mobile-rn/__tests__/meetings.test.ts b/apps/mobile-rn/__tests__/meetings.test.ts index c886f52..af0d02a 100644 --- a/apps/mobile-rn/__tests__/meetings.test.ts +++ b/apps/mobile-rn/__tests__/meetings.test.ts @@ -40,6 +40,7 @@ import { computeMemoTimestamp, createMeetingCreationIdempotencyKey, createMeetingWorkspaceWithMetadata, + findActiveTranscriptSegmentId, getMeetingRecordingLanguage, MeetingServiceError, normalizeDocumentDraft, @@ -48,11 +49,17 @@ import { normalizeMeetingLanguage, normalizeMeetingTitle, normalizeMemoContent, + parseTimestampTokenToMs, + parseTranscriptSeekSegments, sanitizeMeetingSearch, subscribeToMeetingDetail, updateMeetingMemoConflictSafe, updateMeetingTitleRevisionSafe, } from '../src/features/meetings/meetings-service' +import { + findActiveWebTranscriptSegmentId, + parseFallbackTranscriptRows, +} from '../../web/src/components/meetings/live-transcript-list' const USER_ID = '11111111-1111-4111-8111-111111111111' const MEETING_ID = '22222222-2222-4222-8222-222222222222' @@ -307,6 +314,53 @@ describe('meeting transcript selection', () => { expect(buildMeetingTranscriptView(meeting(), [])) .toEqual({ source: 'empty', text: '' }) }) + + test('parses [MM:SS] timestamps and resolves active segment for audio seek sync', () => { + expect(parseTimestampTokenToMs('[01:25]')).toBe(85_000) + expect(parseTimestampTokenToMs('02:05')).toBe(125_000) + expect(parseTimestampTokenToMs('[01:02:03]')).toBe(3_723_000) + expect(parseTimestampTokenToMs('invalid')).toBeNull() + + const fromRows = parseTranscriptSeekSegments(meeting(), [ + transcript('seg-2', 2, '두 번째 발언', '화자 2'), + transcript('seg-1', 1, '첫 번째 발언', '화자 1'), + ]) + expect(fromRows.map((item) => ({ id: item.id, timestampMs: item.timestampMs }))).toEqual([ + { id: 'seg-1', timestampMs: 1_000 }, + { id: 'seg-2', timestampMs: 2_000 }, + ]) + expect(findActiveTranscriptSegmentId(fromRows, 500)).toBeNull() + expect(findActiveTranscriptSegmentId(fromRows, 1_500)).toBe('seg-1') + expect(findActiveTranscriptSegmentId(fromRows, 2_500)).toBe('seg-2') + + const fromFallback = parseTranscriptSeekSegments( + meeting({ raw_transcript: '[00:05] [화자 A] 안건 소개\n[00:20] 일정 논의' }), + [], + ) + expect(fromFallback).toEqual([ + expect.objectContaining({ + id: 'line-0', + timestampMs: 5_000, + speaker: '화자 A', + text: '안건 소개', + timed: true, + }), + expect.objectContaining({ + id: 'line-1', + timestampMs: 20_000, + speaker: null, + text: '일정 논의', + timed: true, + }), + ]) + expect(findActiveTranscriptSegmentId(fromFallback, 12_000)).toBe('line-0') + expect(findActiveTranscriptSegmentId(fromFallback, 25_000)).toBe('line-1') + + const webRows = parseFallbackTranscriptRows('[00:10] [윤찬] 첫 구간\n[00:40] 둘째 구간') + expect(webRows).toHaveLength(2) + expect(findActiveWebTranscriptSegmentId(webRows, 15_000)).toBe('fallback-line-0') + expect(findActiveWebTranscriptSegmentId(webRows, 45_000)).toBe('fallback-line-1') + }) }) describe('meeting mutation safety', () => { diff --git a/apps/mobile-rn/__tests__/notifications.test.ts b/apps/mobile-rn/__tests__/notifications.test.ts index b11b291..aa91ea1 100644 --- a/apps/mobile-rn/__tests__/notifications.test.ts +++ b/apps/mobile-rn/__tests__/notifications.test.ts @@ -26,8 +26,13 @@ import { import { detachPushRegistrationForLogout, registerPushRegistration, + resolveDevicePushProvider, synchronizePushRegistration, } from '../src/features/notifications/notification-service' +import { + subscribeWebPush, + urlBase64ToUint8Array, +} from '../../web/src/lib/web-push' const USER_ID = '11111111-1111-4111-8111-111111111111' const DEVICE_ID = '22222222-2222-4222-8222-222222222222' @@ -270,4 +275,56 @@ describe('device-bound push registration', () => { expect(mockedSupabase.rpc).not.toHaveBeenCalled() expect(mockedNative.deleteNativePushRegistration).toHaveBeenCalledTimes(1) }) + + test('registers iOS devices with provider apns while Android uses fcm', async () => { + const iosDevice: RegisteredDevice = { + ...device, + platform: 'ios', + deviceName: 'iPhone 16 Pro', + osVersion: 'iOS 18.1', + } + const apnsToken = `apns_${'b'.repeat(64)}` + expect(resolveDevicePushProvider(device)).toBe('fcm') + expect(resolveDevicePushProvider(iosDevice)).toBe('apns') + + mockedNative.getNativeNotificationStatus.mockResolvedValue({ + configured: true, + provider: 'apns', + reason: 'configured', + permission: 'granted', + registrationId: apnsToken, + fullSyncRequired: false, + }) + mockedSupabase.rpc.mockResolvedValue({ + data: { + device_id: DEVICE_ID, + provider: 'apns', + registered_at: '2026-08-21T00:05:00.000Z', + }, + error: null, + }) + + await expect(synchronizePushRegistration(USER_ID, iosDevice)).resolves.toMatchObject({ + status: 'registered', + registeredAt: '2026-08-21T00:05:00.000Z', + }) + expect(mockedSupabase.rpc).toHaveBeenCalledWith('register_push_registration', { + push_device_id: DEVICE_ID, + push_provider: 'apns', + registration_id: apnsToken, + }) + }) + + test('converts VAPID public key and fails closed when VAPID key is absent in web push', async () => { + const bytes = urlBase64ToUint8Array('SGVsbG8td29ybGQ') + expect(bytes).toBeInstanceOf(Uint8Array) + expect(bytes.length).toBeGreaterThan(0) + + await expect( + subscribeWebPush({ + userId: USER_ID, + vapidPublicKey: '', + }), + ).rejects.toMatchObject({ code: 'vapid-key-missing' }) + }) }) diff --git a/apps/mobile-rn/__tests__/unified-search-service.test.ts b/apps/mobile-rn/__tests__/unified-search-service.test.ts index d545ce0..43877bb 100644 --- a/apps/mobile-rn/__tests__/unified-search-service.test.ts +++ b/apps/mobile-rn/__tests__/unified-search-service.test.ts @@ -6,6 +6,7 @@ import { createMobileSearchExcerpt, createPostgrestLikePattern, createSupabaseUnifiedSearchSource, + isUnifiedSearchAbortedError, sanitizeUnifiedSearchQuery, searchUnifiedContent, type MobileUnifiedSearchOptions, @@ -212,4 +213,110 @@ describe('mobile unified search service', () => { expect(normalizeUnifiedSearchInitialQuery(123)).toBe('') expect(normalizeUnifiedSearchInitialQuery('가'.repeat(120))).toHaveLength(100) }) + + test('includes transcript segment matches with segment snippet and parent meeting ID', async () => { + const segmentOnlyMeetingId = '44444444-4444-4444-8444-444444444444' + const source: UnifiedSearchSourcePort = { + searchRecords: jest.fn(async () => ({ rows: [], total: 0 })), + searchMeetings: jest.fn(async () => ({ rows: [], total: 0 })), + searchTranscriptSegments: jest.fn(async () => ({ + rows: [{ + id: '55555555-5555-4555-8555-555555555555', + meeting_id: segmentOnlyMeetingId, + text: '보안 감사 일정을 금요일로 확정했습니다.', + speaker: '김팀장', + timestamp_ms: 125_000, + created_at: '2026-09-29T00:00:00.000Z', + }], + total: 1, + })), + loadMeetingsByIds: jest.fn(async () => [{ + id: segmentOnlyMeetingId, + title: '보안 점검 회의', + started_at: '2026-09-29T00:00:00.000Z', + }]), + } + + const response = await searchUnifiedContent({ + userId: USER_ID, + query: '보안 감사', + limit: 10, + }, source) + + expect(source.searchTranscriptSegments).toHaveBeenCalledWith('보안 감사', 10) + expect(source.loadMeetingsByIds).toHaveBeenCalledWith([segmentOnlyMeetingId]) + expect(response.total).toBe(1) + expect(response.results).toEqual([ + expect.objectContaining({ + source: 'meeting', + id: segmentOnlyMeetingId, + title: '보안 점검 회의', + matchedField: 'transcript', + excerpt: '[02:05] [김팀장] 보안 감사 일정을 금요일로 확정했습니다.', + }), + ]) + }) + + test('cancels in-flight search when AbortSignal is aborted', async () => { + const controller = new AbortController() + controller.abort() + const source: UnifiedSearchSourcePort = { + searchRecords: jest.fn(), + searchMeetings: jest.fn(), + } + + let caughtError: unknown = null + try { + await searchUnifiedContent({ + userId: USER_ID, + query: '로드맵', + signal: controller.signal, + }, source) + } catch (error) { + caughtError = error + } + expect(isUnifiedSearchAbortedError(caughtError)).toBe(true) + expect(source.searchRecords).not.toHaveBeenCalled() + expect(source.searchMeetings).not.toHaveBeenCalled() + }) + + test('bounds oversized text payloads in Supabase source adapter', async () => { + const hugeText = `${'가'.repeat(500)} 핵심키워드 ${'나'.repeat(500)}` + const transcriptBuilder = { + select: jest.fn(), + or: jest.fn(), + order: jest.fn(), + abortSignal: jest.fn(), + limit: jest.fn(), + } + transcriptBuilder.select.mockReturnValue(transcriptBuilder) + transcriptBuilder.or.mockReturnValue(transcriptBuilder) + transcriptBuilder.order.mockReturnValue(transcriptBuilder) + transcriptBuilder.abortSignal.mockReturnValue(transcriptBuilder) + transcriptBuilder.limit.mockResolvedValue({ + data: [{ + id: 'seg-1', + meeting_id: meeting.id, + text: hugeText, + speaker: '화자 1', + timestamp_ms: 15_000, + created_at: '2026-09-28T00:00:00.000Z', + }], + error: null, + count: 1, + }) + + const from = jest.fn(() => transcriptBuilder) + const source = createSupabaseUnifiedSearchSource({ from } as unknown as D3roSupabaseClient) + const controller = new AbortController() + + const page = await source.searchTranscriptSegments!('핵심키워드', 10, controller.signal) + expect(transcriptBuilder.select).toHaveBeenCalledWith( + 'id,meeting_id,text,speaker,timestamp_ms,created_at', + { count: 'exact' }, + ) + expect(transcriptBuilder.abortSignal).toHaveBeenCalledWith(controller.signal) + expect(page.rows[0].text.length).toBeLessThanOrEqual(602) + expect(page.rows[0].text).toContain('핵심키워드') + }) }) diff --git a/apps/mobile-rn/src/features/meetings/meetings-service.ts b/apps/mobile-rn/src/features/meetings/meetings-service.ts index 1265639..08fa857 100644 --- a/apps/mobile-rn/src/features/meetings/meetings-service.ts +++ b/apps/mobile-rn/src/features/meetings/meetings-service.ts @@ -1,4 +1,5 @@ import type { RealtimeChannel } from '@supabase/supabase-js' +import { parseTranscriptLines } from '@d3ro/core/meeting-transcript' import type { D3roSupabaseClient, Meeting, @@ -65,6 +66,16 @@ export interface MeetingTranscriptView { text: string } +export interface MeetingSeekableSegment { + id: string + timestampMs: number + durationMs: number | null + speaker: string | null + text: string + edited: boolean + timed: boolean +} + export type MeetingServiceErrorCode = | 'auth' | 'conflict' @@ -256,6 +267,103 @@ export function buildMeetingTranscriptView( return { source: 'empty', text: '' } } +const TIMESTAMP_TOKEN_PATTERN = /^\[?(?:(\d{1,2}):)?(\d{1,3}):(\d{2})\]?$/ + +export function parseTimestampTokenToMs(token: string): number | null { + const match = TIMESTAMP_TOKEN_PATTERN.exec(token.trim()) + if (!match) return null + const hours = match[1] !== undefined ? Number(match[1]) : 0 + const minutes = Number(match[2]) + const seconds = Number(match[3]) + if ( + !Number.isFinite(hours) + || !Number.isFinite(minutes) + || !Number.isFinite(seconds) + || seconds >= 60 + || (match[1] !== undefined && minutes >= 60) + ) { + return null + } + return ((hours * 3_600) + (minutes * 60) + seconds) * 1_000 +} + +export function parseTranscriptSeekSegments( + meeting: Pick, + transcripts: readonly Transcript[], +): MeetingSeekableSegment[] { + if (transcripts.length > 0) { + return transcripts + .slice() + .sort((left, right) => left.segment_index - right.segment_index) + .map((segment) => ({ + id: segment.id, + timestampMs: Math.max(0, segment.timestamp_ms), + durationMs: segment.duration_ms, + speaker: segment.speaker, + text: segment.text, + edited: segment.edited, + timed: true, + })) + } + + const rawText = meeting.edited_transcript?.trim() + ? meeting.edited_transcript + : meeting.raw_transcript?.trim() + ? meeting.raw_transcript + : '' + if (!rawText) return [] + + return parseTranscriptLines(rawText).map((line, index) => ({ + id: `line-${index}`, + timestampMs: Math.max(0, line.timestampMs), + durationMs: null, + speaker: line.speaker, + text: line.text, + edited: false, + timed: line.timed, + })) +} + +export function findActiveTranscriptSegmentId( + segments: readonly Pick[], + currentPositionMs: number | null | undefined, +): string | null { + if ( + currentPositionMs === null + || currentPositionMs === undefined + || !Number.isFinite(currentPositionMs) + || currentPositionMs < 0 + || segments.length === 0 + ) { + return null + } + + const timedSegments = segments + .filter((segment) => segment.timed) + .slice() + .sort((left, right) => left.timestampMs - right.timestampMs) + + if (timedSegments.length === 0) return null + + let activeId: string | null = null + for (let index = 0; index < timedSegments.length; index += 1) { + const current = timedSegments[index] + const next = timedSegments[index + 1] + if (currentPositionMs < current.timestampMs) { + break + } + const endBound = next !== undefined + ? next.timestampMs + : current.durationMs !== null && current.durationMs > 0 + ? current.timestampMs + current.durationMs + : Number.POSITIVE_INFINITY + if (currentPositionMs <= endBound) { + activeId = current.id + } + } + return activeId +} + function assertMeetingRow(row: Meeting | null, expectedId?: string): Meeting { if (row === null) { throw new MeetingServiceError('not-found', 'Meeting was not found') diff --git a/apps/mobile-rn/src/features/notifications/notification-native.ts b/apps/mobile-rn/src/features/notifications/notification-native.ts index 019c70c..00dbf90 100644 --- a/apps/mobile-rn/src/features/notifications/notification-native.ts +++ b/apps/mobile-rn/src/features/notifications/notification-native.ts @@ -17,9 +17,11 @@ export type NotificationConfigurationReason = | 'native-module-unavailable' | 'unsupported-platform' +export type MobilePushProvider = 'fcm' | 'apns' + export interface NativeNotificationStatus { configured: boolean - provider: 'fcm' + provider: MobilePushProvider reason: NotificationConfigurationReason permission: NotificationPermissionStatus registrationId: string | null @@ -49,12 +51,16 @@ function isRecord(value: unknown): value is Record { return typeof value === 'object' && value !== null && !Array.isArray(value) } +export function resolveNativePushProvider(platform: string = Platform.OS): MobilePushProvider { + return platform === 'ios' ? 'apns' : 'fcm' +} + function normalizeStatus(value: unknown): NativeNotificationStatus { if (!isRecord(value)) throw new Error('notification_native_status_invalid') const registrationId = value.registrationId ?? null if ( typeof value.configured !== 'boolean' - || value.provider !== 'fcm' + || (value.provider !== 'fcm' && value.provider !== 'apns') || ![ 'configured', 'firebase-config-missing', @@ -70,7 +76,7 @@ function normalizeStatus(value: unknown): NativeNotificationStatus { } return { configured: value.configured, - provider: 'fcm', + provider: value.provider as MobilePushProvider, reason: value.reason as NotificationConfigurationReason, permission: value.permission as NotificationPermissionStatus, registrationId, @@ -79,10 +85,11 @@ function normalizeStatus(value: unknown): NativeNotificationStatus { } export async function getNativeNotificationStatus(): Promise { - if (Platform.OS !== 'android') { + const provider = resolveNativePushProvider(Platform.OS) + if (Platform.OS !== 'android' && Platform.OS !== 'ios') { return { configured: false, - provider: 'fcm', + provider, reason: 'unsupported-platform', permission: 'unavailable', registrationId: null, @@ -92,7 +99,7 @@ export async function getNativeNotificationStatus(): Promise, +): MobilePushProvider { + return device.platform === 'ios' ? 'apns' : 'fcm' +} + export async function registerPushRegistration( userId: string, device: RegisteredDevice, @@ -63,10 +70,12 @@ export async function registerPushRegistration( || /\s/.test(registrationId) ) throw new NotificationServiceError('native') + const expectedProvider = resolveDevicePushProvider(device) + try { const { data, error } = await supabase.rpc('register_push_registration', { push_device_id: device.id, - push_provider: 'fcm', + push_provider: expectedProvider, registration_id: registrationId, }) if (error !== null) throw error @@ -74,7 +83,7 @@ export async function registerPushRegistration( const registeredAt = data.registered_at if ( data.device_id !== device.id - || data.provider !== 'fcm' + || data.provider !== expectedProvider || typeof registeredAt !== 'string' || !Number.isFinite(Date.parse(registeredAt)) ) throw new NotificationServiceError('server') diff --git a/apps/mobile-rn/src/features/search/unified-search-service.ts b/apps/mobile-rn/src/features/search/unified-search-service.ts index 4c3deeb..830c3b1 100644 --- a/apps/mobile-rn/src/features/search/unified-search-service.ts +++ b/apps/mobile-rn/src/features/search/unified-search-service.ts @@ -1,8 +1,9 @@ -import type { D3roSupabaseClient, HistoryEntry, Meeting } from '@d3ro/api-client' +import type { D3roSupabaseClient, HistoryEntry, Meeting, Transcript } from '@d3ro/api-client' import type { UnifiedSearchResponse, UnifiedSearchResult, } from '@d3ro/core/types' +import { formatTranscriptTimestamp } from '@d3ro/core/meeting-transcript' import { supabase } from '../../lib/supabase' const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i @@ -10,10 +11,13 @@ const DEFAULT_LIMIT = 30 const MAX_LIMIT = 50 const MAX_QUERY_LENGTH = 100 const EXCERPT_LENGTH = 180 +const MAX_BOUNDED_TEXT_LENGTH = 600 const HISTORY_SEARCH_COLUMNS = 'id,title,original_text,polished_text,summary_text,created_at' const MEETING_SEARCH_COLUMNS = 'id,title,raw_transcript,edited_transcript,started_at' +const TRANSCRIPT_SEGMENT_SEARCH_COLUMNS = 'id,meeting_id,text,speaker,timestamp_ms,created_at' +const MEETING_META_COLUMNS = 'id,title,started_at' -export type UnifiedSearchServiceErrorCode = 'auth' | 'invalid' | 'network' | 'server' +export type UnifiedSearchServiceErrorCode = 'auth' | 'invalid' | 'network' | 'server' | 'aborted' export class UnifiedSearchServiceError extends Error { constructor( @@ -40,22 +44,44 @@ type UnifiedSearchMeetingRow = Pick< 'id' | 'title' | 'raw_transcript' | 'edited_transcript' | 'started_at' > +export type UnifiedSearchTranscriptSegmentRow = Pick< + Transcript, + 'id' | 'meeting_id' | 'text' | 'speaker' | 'timestamp_ms' | 'created_at' +> + +export type UnifiedSearchMeetingMetaRow = Pick< + Meeting, + 'id' | 'title' | 'started_at' +> + export interface UnifiedSearchSourcePort { searchRecords: ( userId: string, query: string, limit: number, + signal?: AbortSignal, ) => Promise> searchMeetings: ( query: string, limit: number, + signal?: AbortSignal, ) => Promise> + searchTranscriptSegments?: ( + query: string, + limit: number, + signal?: AbortSignal, + ) => Promise> + loadMeetingsByIds?: ( + meetingIds: readonly string[], + signal?: AbortSignal, + ) => Promise } export interface MobileUnifiedSearchOptions { userId: string query: string limit?: number + signal?: AbortSignal } function client(): D3roSupabaseClient { @@ -149,11 +175,17 @@ function recordResult(row: UnifiedSearchHistoryRow, query: string): UnifiedSearc } } -function meetingResult(row: UnifiedSearchMeetingRow, query: string): UnifiedSearchResult { +function meetingResult( + row: UnifiedSearchMeetingRow, + query: string, + segmentMatch?: UnifiedSearchTranscriptSegmentRow, +): UnifiedSearchResult { const titleMatched = includesQuery(row.title, query) const matchingTranscript = [row.edited_transcript, row.raw_transcript] .find((value) => includesQuery(value, query)) + const segmentSnippet = segmentMatch ? formatSegmentSnippet(segmentMatch) : null const preview = matchingTranscript + ?? segmentSnippet ?? row.edited_transcript ?? row.raw_transcript ?? row.title @@ -163,11 +195,59 @@ function meetingResult(row: UnifiedSearchMeetingRow, query: string): UnifiedSear id: row.id, title: row.title, excerpt: createMobileSearchExcerpt(preview, query), - matchedField: titleMatched ? 'title' : 'transcript', + matchedField: titleMatched && !matchingTranscript && !segmentSnippet ? 'title' : 'transcript', occurredAt: occurredAt(row.started_at), } } +function formatSegmentSnippet(segment: UnifiedSearchTranscriptSegmentRow): string { + const timestamp = formatTranscriptTimestamp(segment.timestamp_ms) + const speakerPrefix = segment.speaker?.trim() ? `[${segment.speaker.trim()}] ` : '' + return `[${timestamp}] ${speakerPrefix}${segment.text}` +} + +function segmentMeetingResult( + segment: UnifiedSearchTranscriptSegmentRow, + meetingMeta: UnifiedSearchMeetingMetaRow | undefined, + query: string, +): UnifiedSearchResult { + return { + source: 'meeting', + id: segment.meeting_id, + title: meetingMeta?.title ?? null, + excerpt: createMobileSearchExcerpt(formatSegmentSnippet(segment), query), + matchedField: 'transcript', + occurredAt: occurredAt(meetingMeta?.started_at ?? segment.created_at), + } +} + +export function isUnifiedSearchAbortedError(error: unknown): boolean { + if (error instanceof UnifiedSearchServiceError && error.code === 'aborted') return true + return error instanceof Error && error.name === 'AbortError' +} + +function throwIfAborted(signal?: AbortSignal): void { + if (signal?.aborted) { + throw new UnifiedSearchServiceError('aborted', 'Unified search request was aborted') + } +} + +function withAbortSignal(builder: T, signal?: AbortSignal): T { + if (signal && typeof (builder as { abortSignal?: unknown }).abortSignal === 'function') { + return (builder as unknown as { abortSignal: (s: AbortSignal) => T }).abortSignal(signal) + } + return builder +} + +function boundPayloadText( + value: string | null, + query: string, +): string | null { + if (value === null) return null + if (value.length <= MAX_BOUNDED_TEXT_LENGTH) return value + return createMobileSearchExcerpt(value, query, MAX_BOUNDED_TEXT_LENGTH) +} + export function mergeMobileUnifiedSearchResults( records: readonly UnifiedSearchResult[], meetings: readonly UnifiedSearchResult[], @@ -178,10 +258,16 @@ export function mergeMobileUnifiedSearchResults( .slice(0, limit) } -function toSearchError(error: unknown): UnifiedSearchServiceError { +function toSearchError(error: unknown, signal?: AbortSignal): UnifiedSearchServiceError { + if (signal?.aborted || isUnifiedSearchAbortedError(error)) { + return new UnifiedSearchServiceError('aborted', 'Unified search request was aborted') + } if (error instanceof UnifiedSearchServiceError) return error - const candidate = error as { code?: unknown; message?: unknown } + const candidate = error as { code?: unknown; message?: unknown; name?: unknown } + if (candidate?.name === 'AbortError') { + return new UnifiedSearchServiceError('aborted', 'Unified search request was aborted') + } const code = typeof candidate?.code === 'string' ? candidate.code : '' const message = typeof candidate?.message === 'string' ? candidate.message @@ -205,35 +291,90 @@ export function createSupabaseUnifiedSearchSource( searchClient: D3roSupabaseClient, ): UnifiedSearchSourcePort { return { - async searchRecords(userId, query, limit) { + async searchRecords(userId, query, limit, signal) { + throwIfAborted(signal) const pattern = createPostgrestLikePattern(query) - const { data, error, count } = await searchClient - .from('history') - .select(HISTORY_SEARCH_COLUMNS, { count: 'exact' }) - .eq('user_id', userId) - .or( - `title.ilike.${pattern},original_text.ilike.${pattern},polished_text.ilike.${pattern},summary_text.ilike.${pattern}`, - ) - .order('created_at', { ascending: false }) - .order('id', { ascending: false }) - .limit(limit) + const queryBuilder = withAbortSignal( + searchClient + .from('history') + .select(HISTORY_SEARCH_COLUMNS, { count: 'exact' }) + .eq('user_id', userId) + .or( + `title.ilike.${pattern},original_text.ilike.${pattern},polished_text.ilike.${pattern},summary_text.ilike.${pattern}`, + ) + .order('created_at', { ascending: false }) + .order('id', { ascending: false }), + signal, + ) + const { data, error, count } = await queryBuilder.limit(limit) if (error !== null) throw error - return { rows: data ?? [], total: count ?? data?.length ?? 0 } + const rows = (data ?? []).map((row) => ({ + ...row, + original_text: boundPayloadText(row.original_text, query) ?? '', + polished_text: boundPayloadText(row.polished_text, query), + summary_text: boundPayloadText(row.summary_text, query), + })) + return { rows, total: count ?? rows.length } }, - async searchMeetings(query, limit) { + async searchMeetings(query, limit, signal) { + throwIfAborted(signal) const pattern = createPostgrestLikePattern(query) - const { data, error, count } = await searchClient - .from('meetings') - .select(MEETING_SEARCH_COLUMNS, { count: 'exact' }) - .or( - `title.ilike.${pattern},raw_transcript.ilike.${pattern},edited_transcript.ilike.${pattern}`, - ) - .order('started_at', { ascending: false }) - .order('id', { ascending: false }) - .limit(limit) + const queryBuilder = withAbortSignal( + searchClient + .from('meetings') + .select(MEETING_SEARCH_COLUMNS, { count: 'exact' }) + .or( + `title.ilike.${pattern},raw_transcript.ilike.${pattern},edited_transcript.ilike.${pattern}`, + ) + .order('started_at', { ascending: false }) + .order('id', { ascending: false }), + signal, + ) + const { data, error, count } = await queryBuilder.limit(limit) if (error !== null) throw error - return { rows: data ?? [], total: count ?? data?.length ?? 0 } + const rows = (data ?? []).map((row) => ({ + ...row, + raw_transcript: boundPayloadText(row.raw_transcript, query), + edited_transcript: boundPayloadText(row.edited_transcript, query), + })) + return { rows, total: count ?? rows.length } + }, + + async searchTranscriptSegments(query, limit, signal) { + throwIfAborted(signal) + const pattern = createPostgrestLikePattern(query) + const queryBuilder = withAbortSignal( + searchClient + .from('transcripts') + .select(TRANSCRIPT_SEGMENT_SEARCH_COLUMNS, { count: 'exact' }) + .or(`text.ilike.${pattern},speaker.ilike.${pattern}`) + .order('timestamp_ms', { ascending: true }) + .order('id', { ascending: false }), + signal, + ) + const { data, error, count } = await queryBuilder.limit(limit) + if (error !== null) throw error + const rows = (data ?? []).map((row) => ({ + ...row, + text: boundPayloadText(row.text, query) ?? '', + })) + return { rows, total: count ?? rows.length } + }, + + async loadMeetingsByIds(meetingIds, signal) { + if (meetingIds.length === 0) return [] + throwIfAborted(signal) + const queryBuilder = withAbortSignal( + searchClient + .from('meetings') + .select(MEETING_META_COLUMNS) + .in('id', [...meetingIds]), + signal, + ) + const { data, error } = await queryBuilder.limit(meetingIds.length) + if (error !== null) throw error + return data ?? [] }, } } @@ -249,24 +390,70 @@ export async function searchUnifiedContent( requireQuery(options.query) const query = sanitizeUnifiedSearchQuery(options.query) const limit = normalizeLimit(options.limit) + throwIfAborted(options.signal) if (query.length === 0) return { query, results: [], total: 0 } try { - const [recordPage, meetingPage] = await Promise.all([ - source.searchRecords(options.userId, query, limit), - source.searchMeetings(query, limit), + const [recordPage, meetingPage, segmentPage] = await Promise.all([ + options.signal + ? source.searchRecords(options.userId, query, limit, options.signal) + : source.searchRecords(options.userId, query, limit), + options.signal + ? source.searchMeetings(query, limit, options.signal) + : source.searchMeetings(query, limit), + source.searchTranscriptSegments + ? (options.signal + ? source.searchTranscriptSegments(query, limit, options.signal) + : source.searchTranscriptSegments(query, limit)) + : Promise.resolve({ rows: [], total: 0 }), ]) + throwIfAborted(options.signal) + + const segmentByMeetingId = new Map() + for (const segment of segmentPage.rows) { + if (!segmentByMeetingId.has(segment.meeting_id)) { + segmentByMeetingId.set(segment.meeting_id, segment) + } + } + + const directMeetingIds = new Set(meetingPage.rows.map((row) => row.id)) + const missingMeetingIds = [...segmentByMeetingId.keys()].filter( + (meetingId) => !directMeetingIds.has(meetingId), + ) + + const extraMeetingMetaRows = missingMeetingIds.length > 0 && source.loadMeetingsByIds + ? await (options.signal + ? source.loadMeetingsByIds(missingMeetingIds, options.signal) + : source.loadMeetingsByIds(missingMeetingIds)) + : [] + throwIfAborted(options.signal) + + const extraMeetingMetaById = new Map( + extraMeetingMetaRows.map((row) => [row.id, row]), + ) + + const directMeetingResults = meetingPage.rows.map((row) => ( + meetingResult(row, query, segmentByMeetingId.get(row.id)) + )) + const segmentOnlyMeetingResults = missingMeetingIds + .map((meetingId) => { + const segment = segmentByMeetingId.get(meetingId) + if (!segment) return null + return segmentMeetingResult(segment, extraMeetingMetaById.get(meetingId), query) + }) + .filter((item): item is UnifiedSearchResult => item !== null) + const results = mergeMobileUnifiedSearchResults( recordPage.rows.map((row) => recordResult(row, query)), - meetingPage.rows.map((row) => meetingResult(row, query)), + [...directMeetingResults, ...segmentOnlyMeetingResults], limit, ) return { query, results, - total: recordPage.total + meetingPage.total, + total: recordPage.total + meetingPage.total + segmentOnlyMeetingResults.length, } } catch (error) { - throw toSearchError(error) + throw toSearchError(error, options.signal) } } diff --git a/apps/mobile-rn/src/lib/entitlement-context.tsx b/apps/mobile-rn/src/lib/entitlement-context.tsx index 0bc721e..dff7d6b 100644 --- a/apps/mobile-rn/src/lib/entitlement-context.tsx +++ b/apps/mobile-rn/src/lib/entitlement-context.tsx @@ -1,4 +1,11 @@ import AsyncStorage from '@react-native-async-storage/async-storage' +import { + normalizeEntitlementTier, + resolveEntitlement, + type AdminRole, + type EntitlementTier, +} from '@d3ro/core/entitlement' +import type { Feature } from '@d3ro/core/types' import { AppState } from 'react-native' import { createContext, @@ -13,7 +20,7 @@ import { import { useAuth } from './auth-context' import { supabase } from './supabase' -export type MobileSubscriptionTier = 'free' | 'pro' | 'pro_plus' +export type MobileSubscriptionTier = EntitlementTier export type MobileBillingProvider = | 'none' | 'stripe' @@ -47,6 +54,8 @@ export interface EntitlementSnapshot { usageToday: Readonly> purchases: readonly MobilePurchaseSummary[] adFree: boolean + adminRole?: AdminRole + features?: readonly Feature[] refreshedAt: string } @@ -121,8 +130,12 @@ const PURCHASE_COLUMNS = [ const EntitlementContext = createContext(null) function emptySnapshot(): EntitlementSnapshot { + const coreEntitlement = resolveEntitlement({ + subscriptionTier: 'free', + refreshedAt: 0, + }) return { - tier: 'free', + tier: coreEntitlement.tier, status: 'active', provider: 'none', paymentProvider: 'none', @@ -134,7 +147,9 @@ function emptySnapshot(): EntitlementSnapshot { overageCredits: 0, usageToday: {}, purchases: [], - adFree: false, + adFree: coreEntitlement.adFree, + adminRole: coreEntitlement.adminRole, + features: coreEntitlement.features, refreshedAt: new Date(0).toISOString(), } } @@ -149,7 +164,7 @@ function nullableIso(value: unknown): string | null { } function normalizeTier(value: unknown): MobileSubscriptionTier { - return value === 'pro' || value === 'pro_plus' ? value : 'free' + return normalizeEntitlementTier(value) } function normalizeProvider(value: unknown): MobileBillingProvider { @@ -207,14 +222,19 @@ export function normalizeEntitlement( usageRows: readonly UsageRow[], purchaseRows: readonly PurchaseRow[], now = new Date(), + supabaseRole?: unknown, ): EntitlementSnapshot { - const tier = normalizeTier(subscription?.tier) const status = typeof subscription?.status === 'string' && subscription.status ? subscription.status : 'active' const currentPeriodEnd = nullableIso(subscription?.current_period_end) - const periodValid = currentPeriodEnd === null || Date.parse(currentPeriodEnd) > now.getTime() const statusAllowsPaidAccess = ['active', 'trialing', 'canceled', 'past_due'].includes(status) + const coreEntitlement = resolveEntitlement({ + subscriptionTier: statusAllowsPaidAccess ? subscription?.tier : 'free', + supabaseRole, + expiresAt: currentPeriodEnd !== null ? Date.parse(currentPeriodEnd) : null, + refreshedAt: now.getTime(), + }) const usageToday: Record = {} for (const row of usageRows) { if (typeof row.feature === 'string' && row.feature) { @@ -223,7 +243,7 @@ export function normalizeEntitlement( } return { - tier, + tier: coreEntitlement.tier, status, provider: normalizeProvider(subscription?.provider), paymentProvider: normalizePaymentProvider(subscription?.payment_provider), @@ -241,7 +261,9 @@ export function normalizeEntitlement( purchases: purchaseRows .map(normalizePurchase) .filter((purchase): purchase is MobilePurchaseSummary => purchase !== null), - adFree: tier !== 'free' && statusAllowsPaidAccess && periodValid, + adFree: coreEntitlement.adFree, + adminRole: coreEntitlement.adminRole, + features: coreEntitlement.features, refreshedAt: now.toISOString(), } } diff --git a/apps/mobile-rn/src/screens/MeetingDetailScreen.tsx b/apps/mobile-rn/src/screens/MeetingDetailScreen.tsx index fe06da7..44c2ab1 100644 --- a/apps/mobile-rn/src/screens/MeetingDetailScreen.tsx +++ b/apps/mobile-rn/src/screens/MeetingDetailScreen.tsx @@ -45,8 +45,10 @@ import { deleteMeetingDocumentRevisionSafe, deleteMeetingMemoConflictSafe, deleteMeetingRevisionSafe, + findActiveTranscriptSegmentId, getMeetingDetail, MeetingServiceError, + parseTranscriptSeekSegments, subscribeToMeetingDetail, updateMeetingDocumentRevisionSafe, updateMeetingMemoConflictSafe, @@ -73,6 +75,8 @@ interface MeetingDetailScreenProps { | { initialKind: 'meeting_document' }, ) => void } + onSeekAudio?: (positionMs: number) => void + initialAudioPositionMs?: number | null } type EditorState = @@ -84,6 +88,8 @@ type EditorState = export default function MeetingDetailScreen({ route, navigation, + onSeekAudio, + initialAudioPositionMs = null, }: MeetingDetailScreenProps): React.ReactElement { const insets = useSafeAreaInsets() const { t, formatDate } = useI18n() @@ -109,6 +115,13 @@ export default function MeetingDetailScreen({ const [editorBusy, setEditorBusy] = useState(false) const [selectedDocumentTemplate, setSelectedDocumentTemplate] = useState(null) const [reportTarget, setReportTarget] = useState(null) + const [currentPositionMs, setCurrentPositionMs] = useState(initialAudioPositionMs) + + const handleSeekTimestamp = useCallback((timestampMs: number): void => { + const clamped = Math.max(0, Math.floor(timestampMs)) + setCurrentPositionMs(clamped) + onSeekAudio?.(clamped) + }, [onSeekAudio]) const load = useCallback(async ( pullToRefresh = false, @@ -461,6 +474,12 @@ export default function MeetingDetailScreen({ const transcript = detail === null ? null : buildMeetingTranscriptView(detail.meeting, detail.transcripts) + const seekableSegments = useMemo(() => ( + detail === null ? [] : parseTranscriptSeekSegments(detail.meeting, detail.transcripts) + ), [detail]) + const activeSegmentId = useMemo(() => ( + findActiveTranscriptSegmentId(seekableSegments, currentPositionMs) + ), [currentPositionMs, seekableSegments]) return ( @@ -646,25 +665,51 @@ export default function MeetingDetailScreen({ -
    - {transcript?.source === 'segments' ? detail.transcripts.map((segment) => ( - - - - {formatElapsedMs(segment.timestamp_ms)} - - {segment.speaker !== null && ( - {segment.speaker} - )} - {segment.edited && ( - - {t('mobile.meetings.edited')} - - )} - - {segment.text} - - )) : transcript?.source !== 'empty' ? ( +
    + {currentPositionMs !== null && ( + + {formatElapsedMs(currentPositionMs)} + + )} + {transcript?.source === 'segments' || seekableSegments.some((segment) => segment.timed) ? ( + seekableSegments.map((segment) => { + const isActive = activeSegmentId === segment.id + return ( + + + {segment.timed ? ( + handleSeekTimestamp(segment.timestampMs)} + testID={`meeting-transcript-seek-${segment.id}`} + > + + [{formatElapsedMs(segment.timestampMs)}] + + + ) : null} + {segment.speaker !== null && ( + {segment.speaker} + )} + {segment.edited && ( + + {t('mobile.meetings.edited')} + + )} + + {segment.text} + + ) + }) + ) : transcript?.source !== 'empty' ? ( {transcript?.text} ) : ( @@ -1109,9 +1154,16 @@ function createStyles(palette: Palette): ReturnType { transcriptSegment: { gap: 6, paddingVertical: 10, + paddingHorizontal: 8, + borderRadius: 8, borderBottomWidth: 1, borderBottomColor: palette.border.subtle, }, + transcriptSegmentActive: { + backgroundColor: palette.bg.inset, + borderLeftWidth: 3, + borderLeftColor: palette.accent.main, + }, segmentMeta: { flexDirection: 'row', alignItems: 'center', gap: 10 }, readingText: { lineHeight: 23 }, codeText: { diff --git a/apps/mobile-rn/src/screens/UnifiedSearchScreen.tsx b/apps/mobile-rn/src/screens/UnifiedSearchScreen.tsx index 453f065..24646f5 100644 --- a/apps/mobile-rn/src/screens/UnifiedSearchScreen.tsx +++ b/apps/mobile-rn/src/screens/UnifiedSearchScreen.tsx @@ -19,7 +19,10 @@ import { import { useI18n } from '@d3ro/i18n' import { UnifiedSearchResultRow } from '../components/UnifiedSearchResultRow' import { normalizeUnifiedSearchInitialQuery } from '../features/search/unified-search-route' -import { searchUnifiedContent } from '../features/search/unified-search-service' +import { + isUnifiedSearchAbortedError, + searchUnifiedContent, +} from '../features/search/unified-search-service' import { useAuth } from '../lib/auth-context' import { useMobilePreferences } from '../lib/preferences-context' import { ScreenHeader } from '../components/ScreenHeader' @@ -68,21 +71,32 @@ export default function UnifiedSearchScreen({ } const sequence = ++requestSequence.current + const controller = new AbortController() const timer = setTimeout(() => { setState({ status: 'loading', results: [], total: 0 }) - void searchUnifiedContent({ userId: user.id, query: normalized, limit: 40 }) + void searchUnifiedContent({ + userId: user.id, + query: normalized, + limit: 40, + signal: controller.signal, + }) .then((response) => { - if (requestSequence.current !== sequence) return + if (controller.signal.aborted || requestSequence.current !== sequence) return setState({ status: 'ready', results: response.results, total: response.total }) }) - .catch(() => { - if (requestSequence.current !== sequence) return + .catch((error) => { + if ( + controller.signal.aborted + || isUnifiedSearchAbortedError(error) + || requestSequence.current !== sequence + ) return setState((current) => ({ ...current, status: 'error' })) }) }, SEARCH_DEBOUNCE_MS) return () => { clearTimeout(timer) + controller.abort() if (requestSequence.current === sequence) requestSequence.current += 1 } }, [query, retryNonce, user]) diff --git a/apps/web/public/sw.js b/apps/web/public/sw.js new file mode 100644 index 0000000..98d5547 --- /dev/null +++ b/apps/web/public/sw.js @@ -0,0 +1,106 @@ +// apps/web/public/sw.js +// D3RO Voice — Web Push Service Worker + +const DEFAULT_TITLE = 'D3RO Voice' +const DEFAULT_BODY = '새 알림이 도착했습니다.' +const DEFAULT_TARGET_URL = '/app/dashboard' + +function sanitizeTargetUrl(rawUrl) { + if (typeof rawUrl !== 'string' || !rawUrl.trim()) { + return DEFAULT_TARGET_URL + } + const trimmed = rawUrl.trim() + if (!trimmed.startsWith('/') || trimmed.startsWith('//') || trimmed.startsWith('/\\')) { + return DEFAULT_TARGET_URL + } + return trimmed.startsWith('/app/') || trimmed === '/app' + ? trimmed + : `/app${trimmed}` +} + +function parsePushPayload(event) { + if (!event.data) { + return { + title: DEFAULT_TITLE, + body: DEFAULT_BODY, + tag: 'd3ro-notification', + url: DEFAULT_TARGET_URL, + } + } + + try { + const parsed = event.data.json() + if (typeof parsed === 'object' && parsed !== null) { + const title = typeof parsed.title === 'string' && parsed.title.trim() + ? parsed.title.trim() + : DEFAULT_TITLE + const body = typeof parsed.body === 'string' && parsed.body.trim() + ? parsed.body.trim() + : DEFAULT_BODY + const tag = typeof parsed.tag === 'string' && parsed.tag.trim() + ? parsed.tag.trim() + : 'd3ro-notification' + const rawUrl = typeof parsed.url === 'string' + ? parsed.url + : typeof parsed.data?.url === 'string' + ? parsed.data.url + : DEFAULT_TARGET_URL + return { + title, + body, + tag, + url: sanitizeTargetUrl(rawUrl), + } + } + } catch { + const text = event.data.text() + return { + title: DEFAULT_TITLE, + body: text && text.trim() ? text.trim() : DEFAULT_BODY, + tag: 'd3ro-notification', + url: DEFAULT_TARGET_URL, + } + } + + return { + title: DEFAULT_TITLE, + body: DEFAULT_BODY, + tag: 'd3ro-notification', + url: DEFAULT_TARGET_URL, + } +} + +self.addEventListener('push', (event) => { + const payload = parsePushPayload(event) + event.waitUntil( + self.registration.showNotification(payload.title, { + body: payload.body, + tag: payload.tag, + data: { url: payload.url }, + }), + ) +}) + +self.addEventListener('notificationclick', (event) => { + event.notification.close() + const targetUrl = sanitizeTargetUrl(event.notification?.data?.url) + + event.waitUntil( + self.clients + .matchAll({ type: 'window', includeUncontrolled: true }) + .then((windowClients) => { + for (const client of windowClients) { + if ('focus' in client) { + if ('navigate' in client && typeof client.navigate === 'function') { + return client.navigate(targetUrl).then(() => client.focus()) + } + return client.focus() + } + } + if (self.clients.openWindow) { + return self.clients.openWindow(targetUrl) + } + return undefined + }), + ) +}) diff --git a/apps/web/src/app/(app)/billing/page.tsx b/apps/web/src/app/(app)/billing/page.tsx index 85a3397..a6859f4 100644 --- a/apps/web/src/app/(app)/billing/page.tsx +++ b/apps/web/src/app/(app)/billing/page.tsx @@ -14,6 +14,11 @@ import { type BillingCatalog, type BillingCatalogPrice } from '@/lib/billing-catalog' +import { + isSupportedWebTier, + resolveWebEntitlement, + resolveWebProvider +} from '@/lib/entitlement' type BillingProvider = Subscription['provider'] @@ -43,17 +48,6 @@ interface Plan { highlight?: boolean } -const VALID_TIERS = new Set(['free', 'pro', 'pro_plus']) -// 'stripe' 는 결제 경로가 제거된 뒤에도 과거 구독 행(provider='stripe')을 읽기 위해 남긴다. -const VALID_PROVIDERS = new Set([ - 'none', - 'stripe', - 'payple', - 'google_play', - 'app_store', - 'admin' -]) - const PLANS: Plan[] = [ { tier: 'free', @@ -124,12 +118,12 @@ async function loadBillingState(): Promise { if (error) throw new Error('subscription_lookup_failed') if (!data) throw new Error('subscription_not_initialized') - if (!VALID_TIERS.has(data.tier)) throw new Error('subscription_tier_invalid') + if (!isSupportedWebTier(data.tier)) throw new Error('subscription_tier_invalid') - const providerCandidate = data.provider === 'none' && data.payment_provider !== 'none' - ? data.payment_provider - : data.provider - if (!VALID_PROVIDERS.has(providerCandidate)) throw new Error('subscription_provider_invalid') + const providerCandidate = resolveWebProvider(data.provider, data.payment_provider) + if (!providerCandidate) throw new Error('subscription_provider_invalid') + + const resolvedEntitlement = resolveWebEntitlement(data) const { data: catalogData, error: catalogError } = await supabase.functions.invoke('billing-catalog', { body: {} @@ -139,12 +133,12 @@ async function loadBillingState(): Promise { return { email: userData.user.email ?? '이메일 없음', subscription: { - tier: data.tier, - status: data.status, + tier: resolvedEntitlement.tier, + status: resolvedEntitlement.status, provider: providerCandidate, - current_period_end: data.current_period_end, - cancel_at: data.cancel_at, - auto_renewing: data.auto_renewing + current_period_end: resolvedEntitlement.currentPeriodEnd, + cancel_at: resolvedEntitlement.cancelAt, + auto_renewing: resolvedEntitlement.autoRenewing }, catalog } diff --git a/apps/web/src/app/(app)/meetings/[id]/page.tsx b/apps/web/src/app/(app)/meetings/[id]/page.tsx index 5602820..bb3dd31 100644 --- a/apps/web/src/app/(app)/meetings/[id]/page.tsx +++ b/apps/web/src/app/(app)/meetings/[id]/page.tsx @@ -65,7 +65,10 @@ export default async function MeetingDetailPage({ params }: PageProps): Promise< AUDIO - + {/* Transcript — Realtime 구독 or raw_transcript fallback */} @@ -73,23 +76,14 @@ export default async function MeetingDetailPage({ params }: PageProps): Promise< TRANSCRIPT {meeting.status === 'recording' ? '(LIVE)' : ''} - {(transcripts ?? []).length > 0 || meeting.status === 'recording' ? ( + {(transcripts ?? []).length > 0 + || meeting.status === 'recording' + || Boolean(meeting.edited_transcript || meeting.raw_transcript) ? ( - ) : meeting.edited_transcript || meeting.raw_transcript ? ( - - {meeting.edited_transcript ?? meeting.raw_transcript} - ) : ( 전사가 없습니다. diff --git a/apps/web/src/app/(app)/team/page.tsx b/apps/web/src/app/(app)/team/page.tsx new file mode 100644 index 0000000..dea2d75 --- /dev/null +++ b/apps/web/src/app/(app)/team/page.tsx @@ -0,0 +1,5 @@ +import { redirect } from 'next/navigation' + +export default function TeamAliasPage(): never { + redirect('/teams') +} diff --git a/apps/web/src/app/accept-invite/page.tsx b/apps/web/src/app/accept-invite/page.tsx new file mode 100644 index 0000000..28ce2cf --- /dev/null +++ b/apps/web/src/app/accept-invite/page.tsx @@ -0,0 +1,237 @@ +'use client' + +import Link from 'next/link' +import { Suspense, useCallback, useEffect, useRef, useState } from 'react' +import { useRouter, useSearchParams } from 'next/navigation' +import { Alert, Box, CircularProgress, Stack, Typography } from '@mui/material' +import { CheckCircle2, RefreshCw, ShieldAlert, Users } from 'lucide-react' +import { MetalCard, PhysicalButton, TactileBadge } from '@d3ro/ui/components/ds' +import { d3roFontMono, d3roFontSans } from '@d3ro/ui/theme' +import { useAuth } from '@/components/providers/auth-provider' +import { getSupabaseBrowserClient, isSupabaseConfigured } from '@/lib/supabase-browser' + +const INVITE_TOKEN_PATTERN = + /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|[A-Za-z0-9_-]{32})$/i + +interface TeamAcceptEdgePayload { + data?: { + team_id?: unknown + role?: unknown + } + team_id?: unknown + role?: unknown + error?: { + code?: unknown + message?: unknown + } +} + +type InviteAcceptState = + | { status: 'checking'; message: string } + | { status: 'accepting'; message: string } + | { status: 'accepted'; teamId: string | null; role: string } + | { status: 'invalid-token'; message: string } + | { status: 'error'; message: string } + +export function validateWebInviteToken(rawToken: string | null | undefined): string | null { + const trimmed = rawToken?.trim() ?? '' + return INVITE_TOKEN_PATTERN.test(trimmed) ? trimmed : null +} + +export function buildAcceptInviteLoginRedirect(token: string): string { + const nextPath = `/app/accept-invite?token=${encodeURIComponent(token)}` + return `/login?next=${encodeURIComponent(nextPath)}` +} + +function parseTeamAcceptResponse(payload: unknown): { teamId: string | null; role: string } { + if (typeof payload !== 'object' || payload === null) { + return { teamId: null, role: 'member' } + } + const record = payload as TeamAcceptEdgePayload + const inner = record.data ?? record + const teamId = typeof inner.team_id === 'string' && inner.team_id.trim() ? inner.team_id : null + const role = typeof inner.role === 'string' && inner.role.trim() ? inner.role : 'member' + return { teamId, role } +} + +function AcceptInviteContent(): React.ReactElement { + const router = useRouter() + const searchParams = useSearchParams() + const { user, loading: authLoading } = useAuth() + const rawToken = searchParams.get('token') + const validToken = validateWebInviteToken(rawToken) + const attemptedTokenRef = useRef(null) + + const [state, setState] = useState(() => ( + validToken + ? { status: 'checking', message: '로그인 세션과 초대 토큰을 확인하고 있습니다.' } + : { + status: 'invalid-token', + message: '초대 링크가 없거나 형식이 올바르지 않습니다. 새 초대 링크를 요청해 주세요.' + } + )) + + const acceptInvite = useCallback(async (tokenToAccept: string): Promise => { + if (!isSupabaseConfigured()) { + setState({ + status: 'error', + message: 'Supabase 연결 설정을 확인할 수 없어 초대를 수락할 수 없습니다.' + }) + return + } + + setState({ + status: 'accepting', + message: '팀 초대 권한을 검증하고 워크스페이스에 참여하는 중입니다.' + }) + + try { + const supabase = getSupabaseBrowserClient() + const { data, error } = await supabase.functions.invoke('team-accept', { + body: { token: tokenToAccept } + }) + + if (error) { + setState({ + status: 'error', + message: error.message || '팀 초대 수락에 실패했습니다. 만료되었거나 권한이 없는 초대일 수 있습니다.' + }) + return + } + + const parsed = parseTeamAcceptResponse(data) + setState({ + status: 'accepted', + teamId: parsed.teamId, + role: parsed.role + }) + router.replace('/team') + } catch (invokeError) { + setState({ + status: 'error', + message: invokeError instanceof Error + ? invokeError.message + : '팀 초대 수락 요청 중 오류가 발생했습니다.' + }) + } + }, [router]) + + useEffect(() => { + if (!validToken) { + setState({ + status: 'invalid-token', + message: '초대 링크가 없거나 형식이 올바르지 않습니다. 새 초대 링크를 요청해 주세요.' + }) + return + } + + if (authLoading) return + + if (!user) { + router.replace(buildAcceptInviteLoginRedirect(validToken)) + return + } + + if (attemptedTokenRef.current === validToken) return + attemptedTokenRef.current = validToken + void acceptInvite(validToken) + }, [acceptInvite, authLoading, router, user, validToken]) + + return ( + + + + + TEAM INVITE + + {validToken ? `${validToken.slice(0, 8)}…${validToken.slice(-4)}` : 'NO TOKEN'} + + + + + + + 팀 초대 수락 + + + + {(state.status === 'checking' || state.status === 'accepting') && ( + + + + {state.message} + + + )} + + {state.status === 'accepted' && ( + } + data-testid="accept-invite-success" + > + 팀 초대를 수락했습니다 ({state.role}). 팀 워크스페이스로 이동합니다. + + )} + + {(state.status === 'invalid-token' || state.status === 'error') && ( + } + data-testid="accept-invite-error" + > + {state.message} + + )} + + + {state.status === 'error' && validToken && ( + { + attemptedTokenRef.current = validToken + void acceptInvite(validToken) + }} + > + 다시 시도 + + )} + + + 팀 화면으로 이동 + + + + + + + ) +} + +export default function AcceptInvitePage(): React.ReactElement { + return ( + + + + )} + > + + + ) +} diff --git a/apps/web/src/components/meetings/live-transcript-list.tsx b/apps/web/src/components/meetings/live-transcript-list.tsx index 94f8382..d493987 100644 --- a/apps/web/src/components/meetings/live-transcript-list.tsx +++ b/apps/web/src/components/meetings/live-transcript-list.tsx @@ -1,12 +1,18 @@ 'use client' // apps/web/src/components/meetings/live-transcript-list.tsx -// 회의 세그먼트 실시간 구독 — Supabase Realtime으로 INSERT 이벤트 수신 +// 회의 세그먼트 실시간 구독 + 오디오 타임스탬프 탐색 동기화 -import { useEffect, useState } from 'react' +import { useCallback, useEffect, useMemo, useState } from 'react' import { Box, Stack } from '@mui/material' -import { d3roPalette, typoSx } from '@d3ro/ui/theme' -import { getSupabaseBrowserClient } from '@/lib/supabase-browser' +import { formatTranscriptTimestamp, parseTranscriptLines } from '@d3ro/core/meeting-transcript' +import { d3roFontMono, d3roFocusRing, d3roPalette, typoSx } from '@d3ro/ui/theme' +import { getSupabaseBrowserClient } from '../../lib/supabase-browser' +import { + dispatchMeetingAudioSeek, + MEETING_AUDIO_TIMEUPDATE_EVENT, + type MeetingAudioTimeUpdateDetail +} from './meeting-audio-player' export interface TranscriptRow { id: string @@ -20,18 +26,87 @@ export interface TranscriptRow { interface LiveTranscriptListProps { meetingId: string initial: TranscriptRow[] + fallbackTranscript?: string | null + currentPositionMs?: number | null + onSeekTimestamp?: (timestampMs: number) => void +} + +export function parseFallbackTranscriptRows( + fallbackTranscript: string | null | undefined +): TranscriptRow[] { + return parseTranscriptLines(fallbackTranscript).map((line, index) => ({ + id: `fallback-line-${index}`, + segment_index: index, + timestamp_ms: Math.max(0, line.timestampMs), + text: line.text, + speaker: line.speaker, + edited: false + })) +} + +export function findActiveWebTranscriptSegmentId( + segments: readonly Pick[], + currentPositionMs: number | null | undefined +): string | null { + if ( + currentPositionMs === null + || currentPositionMs === undefined + || !Number.isFinite(currentPositionMs) + || currentPositionMs < 0 + || segments.length === 0 + ) { + return null + } + + const ordered = segments + .slice() + .sort((left, right) => left.timestamp_ms - right.timestamp_ms) + + let activeId: string | null = null + for (let index = 0; index < ordered.length; index += 1) { + const current = ordered[index] + const next = ordered[index + 1] + if (currentPositionMs < current.timestamp_ms) { + break + } + const endBound = next !== undefined ? next.timestamp_ms : Number.POSITIVE_INFINITY + if (currentPositionMs <= endBound) { + activeId = current.id + } + } + return activeId } export function LiveTranscriptList({ meetingId, - initial + initial, + fallbackTranscript = null, + currentPositionMs, + onSeekTimestamp }: LiveTranscriptListProps): React.ReactElement { const [segments, setSegments] = useState(initial) + const [internalPositionMs, setInternalPositionMs] = useState(null) + + useEffect(() => { + setSegments(initial) + }, [initial]) + + useEffect(() => { + if (typeof window === 'undefined') return undefined + const handleTimeUpdate = (event: Event): void => { + const detail = (event as CustomEvent).detail + if (!detail || !Number.isFinite(detail.positionMs)) return + if (detail.meetingId && detail.meetingId !== meetingId) return + setInternalPositionMs(detail.positionMs) + } + window.addEventListener(MEETING_AUDIO_TIMEUPDATE_EVENT, handleTimeUpdate) + return () => { + window.removeEventListener(MEETING_AUDIO_TIMEUPDATE_EVENT, handleTimeUpdate) + } + }, [meetingId]) useEffect(() => { const supabase = getSupabaseBrowserClient() - // 회의별 Realtime 채널 구독. V2-2 migration에서 - // ALTER PUBLICATION supabase_realtime ADD TABLE public.transcripts; 적용됨. const channel = supabase .channel(`transcripts:meeting:${meetingId}`) .on( @@ -45,7 +120,6 @@ export function LiveTranscriptList({ (payload) => { const row = payload.new as TranscriptRow setSegments((prev) => { - // 중복 방지 (segment_index 기준) if (prev.some((s) => s.segment_index === row.segment_index)) { return prev } @@ -73,41 +147,102 @@ export function LiveTranscriptList({ } }, [meetingId]) - if (segments.length === 0) { + const effectiveSegments = useMemo(() => { + if (segments.length > 0) return segments + return parseFallbackTranscriptRows(fallbackTranscript) + }, [fallbackTranscript, segments]) + + const effectivePositionMs = currentPositionMs !== undefined ? currentPositionMs : internalPositionMs + const activeSegmentId = useMemo( + () => findActiveWebTranscriptSegmentId(effectiveSegments, effectivePositionMs), + [effectivePositionMs, effectiveSegments] + ) + + const handleSeek = useCallback((timestampMs: number): void => { + const normalized = Math.max(0, Math.floor(timestampMs)) + setInternalPositionMs(normalized) + onSeekTimestamp?.(normalized) + dispatchMeetingAudioSeek(normalized, meetingId) + }, [meetingId, onSeekTimestamp]) + + if (effectiveSegments.length === 0) { return ( 전사 세그먼트가 없습니다. ) } return ( - - {segments.map((seg) => ( - - - {Math.floor(seg.timestamp_ms / 60000)}: - {String(Math.floor((seg.timestamp_ms / 1000) % 60)).padStart(2, '0')} - {seg.speaker && ` · ${seg.speaker}`} - {seg.edited && ( + + {effectiveSegments.map((seg) => { + const isActive = activeSegmentId === seg.id + const formattedTime = formatTranscriptTimestamp(seg.timestamp_ms) + return ( + + handleSeek(seg.timestamp_ms)} sx={{ - ml: 1, px: 0.75, - py: 0.25, - borderRadius: 0.5, - bgcolor: d3roPalette.tag.orangeBg, - color: d3roPalette.tag.orange, + py: 0.2, + border: `1px solid ${isActive ? d3roPalette.accent.main : d3roPalette.border.default}`, + borderRadius: 1, + bgcolor: d3roPalette.bg.card, + color: isActive ? d3roPalette.accent.main : d3roPalette.text.secondary, + fontFamily: d3roFontMono, fontSize: 11, - fontWeight: 500 + cursor: 'pointer', + '&:hover': { + borderColor: d3roPalette.accent.main, + color: d3roPalette.accent.main + }, + '&:focus-visible': d3roFocusRing }} > - EDITED + [{formattedTime}] - )} + {seg.speaker && ( + + {seg.speaker} + + )} + {seg.edited && ( + + EDITED + + )} + + {seg.text} - {seg.text} - - ))} + ) + })} ) } diff --git a/apps/web/src/components/meetings/meeting-audio-player.tsx b/apps/web/src/components/meetings/meeting-audio-player.tsx index edc8b76..cdb91ff 100644 --- a/apps/web/src/components/meetings/meeting-audio-player.tsx +++ b/apps/web/src/components/meetings/meeting-audio-player.tsx @@ -1,23 +1,104 @@ 'use client' // apps/web/src/components/meetings/meeting-audio-player.tsx -// 회의 오디오 재생 — storage key → signed URL →