feat: ship Wave 5-6 multi-surface hygiene, optimization, and bug fixes

This commit is contained in:
Yun Chan 2026-11-14 11:51:41 +09:00
parent 01ef3293cf
commit 66b6b45ba9
399 changed files with 17828 additions and 11330 deletions

View file

@ -224,5 +224,8 @@ Node로 vitest를 돌리면 better-sqlite3 ABI 불일치로 수백 건이 가짜
| Wave 1 | 2026-07-22 | b820c78(WS1)·aef4428(WS2)·078304d(WS3)·87f2dac(WS4) | `docs/REFACTOR_WAVE1_REPORT.md` | | Wave 1 | 2026-07-22 | b820c78(WS1)·aef4428(WS2)·078304d(WS3)·87f2dac(WS4) | `docs/REFACTOR_WAVE1_REPORT.md` |
| Wave 2 | 2026-07-22 | efac690(AMBER)·660e622(NATIVE)·00a99e4(POPUP)·4c25620(DEPS)·5b6e7aa(PATTERN) | `docs/REFACTOR_WAVE2_REPORT.md` | | Wave 2 | 2026-07-22 | efac690(AMBER)·660e622(NATIVE)·00a99e4(POPUP)·4c25620(DEPS)·5b6e7aa(PATTERN) | `docs/REFACTOR_WAVE2_REPORT.md` |
| Wave 3 | 2026-09-26 | 88f24d8(WS-A 계약)·b6fe588(WS-B 웹앱)·cd9d199(WS-C 중복 삭제)·dc43884(WS-D CI)·9297860(사이트) | `docs/REFACTOR_WAVE3_REPORT.md` | | Wave 3 | 2026-09-26 | 88f24d8(WS-A 계약)·b6fe588(WS-B 웹앱)·cd9d199(WS-C 중복 삭제)·dc43884(WS-D CI)·9297860(사이트) | `docs/REFACTOR_WAVE3_REPORT.md` |
| Wave 4 | 2026-11-04 | — (보고서 참조) | `docs/REFACTOR_WAVE4_REPORT.md` |
| Wave 5 | 2026-11-13 | f95bc9c 외 (보고서 참조) | `docs/REFACTOR_WAVE5_REPORT.md` |
| Wave 6 | 2026-11-14 | 워킹 트리 (미커밋) | `docs/REFACTOR_WAVE6_REPORT.md` |
다음 wave 추가 시 본 표 갱신. 다음 wave 추가 시 본 표 갱신.

View file

@ -12,8 +12,9 @@ build/
!apps/desktop/build/entitlements.mac.plist !apps/desktop/build/entitlements.mac.plist
*.tsbuildinfo *.tsbuildinfo
# 별개 서브 프로젝트 (자체 lint 설정 사용) # 별개 서브 프로젝트 (Deno 런타임)
site/ site/dist/
site/dist-ssr/
server/supabase/functions/ server/supabase/functions/
# 환경 # 환경

View file

@ -85,12 +85,26 @@ jobs:
npm run test:hygiene:test npm run test:hygiene:test
npm run test:hygiene npm run test:hygiene
- name: 보호 수용 테스트·품질 래칫 - name: 보호 수용 테스트·품질 래칫·디자인/i18n 계약
run: | run: |
npm run check:protected-tests:test npm run check:protected-tests:test
npm run check:protected-tests -- --base HEAD^ npm run check:protected-tests -- --base HEAD^
npm run test:quality:test npm run test:quality:test
npm run test:quality npm run test:quality
npm run check:i18n:test
npm run check:i18n
npm run check:ds-contract:test
npm run check:ds-contract
npm run check:design:test
npm run check:design
npm run test:affected:test
npm run release:installer:smoke:test
- name: 마케팅 사이트(site/) 정적 검사
run: |
npm --prefix site ci --workspaces=false
npm run typecheck:site
npm run lint:site
- name: 린트 - name: 린트
run: npm run lint run: npm run lint
@ -138,8 +152,17 @@ jobs:
- name: 어드민 빌드 - name: 어드민 빌드
run: npm run build --workspace=@d3ro/admin run: npm run build --workspace=@d3ro/admin
- name: 웹앱 프로덕션 빌드 - name: 웹앱 프로덕션 빌드 + 스모크(Chromium 설치 시)
run: npm run build --workspace=@d3ro/web run: |
npm run build --workspace=@d3ro/web
if [ -d "${PLAYWRIGHT_BROWSERS_PATH:-$HOME/.cache/ms-playwright}" ]; then
npm run test:acceptance:web
fi
- name: 마케팅 사이트(site/) 프로덕션 빌드
run: |
npm --prefix site ci --workspaces=false
npm --prefix site run build
mobile-quality: mobile-quality:
name: 모바일 린트·타입·Jest name: 모바일 린트·타입·Jest

View file

@ -76,7 +76,13 @@ jobs:
env: env:
WIN_CSC_LINK: "${{ secrets.WIN_CSC_LINK }}" WIN_CSC_LINK: "${{ secrets.WIN_CSC_LINK }}"
WIN_CSC_KEY_PASSWORD: "${{ secrets.WIN_CSC_KEY_PASSWORD }}" WIN_CSC_KEY_PASSWORD: "${{ secrets.WIN_CSC_KEY_PASSWORD }}"
run: node scripts/ci/build-desktop-installer.mjs run: |
node scripts/ci/build-desktop-installer.mjs
node scripts/ci/verify-desktop-installer-smoke.mjs --self-test
node scripts/ci/verify-desktop-installer-smoke.mjs
if ($env:D3RO_RUN_WINDOWS_E2E -eq '1') {
npm run test:acceptance:desktop
}
- name: Forgejo 피드 게시 (latest.yml Ed25519 서명 + 공개 URL 검증) - name: Forgejo 피드 게시 (latest.yml Ed25519 서명 + 공개 URL 검증)
if: steps.feed.outputs.published != 'true' if: steps.feed.outputs.published != 'true'

5
.gitignore vendored
View file

@ -33,9 +33,7 @@ apps/desktop/build/sidecar.spec
!apps/desktop/build/entitlements.mac.plist !apps/desktop/build/entitlements.mac.plist
sidecar-dist/ sidecar-dist/
# Bundled binaries (download via scripts; SoX는 직접 커밋) # Bundled binaries (SoX는 apps/desktop/resources/sox에 필수 런타임만 커밋)
resources/sox/*.exe
resources/sox/*.dll
apps/desktop/resources/ollama/ apps/desktop/resources/ollama/
apps/desktop/build/installer.nsh apps/desktop/build/installer.nsh
!apps/desktop/build/installer.nsh !apps/desktop/build/installer.nsh
@ -135,3 +133,4 @@ site/dist-ssr/
# Local reference clones # Local reference clones
/araso/ /araso/
/.codex/ /.codex/
/.pi/

View file

@ -13,6 +13,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Plugin system for custom pipelines - Plugin system for custom pipelines
## [1.17.0] - 2026-11-14
> Multi-surface quality, 12-locale parity, speaker diarization, tablet responsive layout, and stability hardening across Desktop, Mobile, Web, Admin, and Cloud services.
### Added
- 12-locale product switcher across Desktop, Web (`LocaleSwitcher`), and Mobile (`index.native.tsx` + Onboarding/Settings/Account) backed by unified `2,500 keys × 12 locales` parity contracts.
- Speaker diarization across Desktop sidecar (`POST /diarize`), Web, and Mobile meeting transcripts with speaker badges and multi-speaker filter chips.
- Tablet and foldable responsive layouts (`@d3ro/ui-native/responsive`) with bounded content columns, 2-column grids, and fixed `FreeTierBanner` ad slot reservations.
- Monotonic `meeting_memos.revision` column and compare-and-swap conflict protection against A→B→A stale overwrites.
- Automated TDD 2.0 test-quality and protected-journey CI gates (`test:hygiene`, `test:quality`, `check:protected-tests`).
### Changed
- Consolidated shared validation, formatting, IPC emission (`safeSendToRenderer`), mobile service/cache guards, and Supabase Edge JSON helpers (`jsonResponse`, `readJsonBody`) while deduplicating `@d3ro/core`, `@d3ro/ui`, and `.NET` helpers.
- Cached canvas wave gradients in `@d3ro/ui` and tuned search/chunk caches across Desktop and Mobile.
### Fixed
- Desktop `OnlineLLMService.chatStream` abort, 401 token refresh, and malformed SSE handling; `UnifiedSearchService` tie-breakers and FTS5 fallbacks; `SqliteChunkStore` cache invalidation; `DiskWavAudioSink` file descriptor and size bounds; timer and sidecar lifecycle leaks.
- `@d3ro/i18n` async locale load race and unmount cleanup; `@d3ro/core` PII restoration, CSV BOM/CR escaping, timestamp, and search edge cases; renderer IPC/timer cleanup and Vanilla JS popup hardening.
- Mobile durable upload queue premature discard, audio recording lease cleanup, and unmount playback/locale races.
- Web `MarkdownPreview` DOM XSS/race protection and `WebPush` reentry guards; Admin cookie `sameSite`, CSRF, UUID validation, and cost bounds; `.NET` proxy Base64 bounds and `CancellationToken` propagation; Cloudflare Worker SSRF/timeout hardening; sidecar `UploadFile` handle and `/shutdown` thread cleanup.
## [1.16.5] - 2026-11-06 ## [1.16.5] - 2026-11-06
> Transcript polish now guards what the model must not break, measured against the local model. > Transcript polish now guards what the model must not break, measured against the local model.

View file

@ -10,6 +10,7 @@ import { requireManager } from '@/lib/admin-guard'
import Link from 'next/link' import Link from 'next/link'
import { notFound } from 'next/navigation' import { notFound } from 'next/navigation'
import { AuditDiffViewer } from '@/components/audit-diff-viewer' import { AuditDiffViewer } from '@/components/audit-diff-viewer'
import { DetailRow } from '@/components/detail-row'
import { sanitizeAuditSnapshot } from '@/lib/audit-sanitize' import { sanitizeAuditSnapshot } from '@/lib/audit-sanitize'
interface PageProps { interface PageProps {
@ -114,11 +115,11 @@ export default async function AuditLogDetailPage({ params }: PageProps): Promise
Transaction Metadata Transaction Metadata
</Typography> </Typography>
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 1.5, fontFamily: FONT_SANS, fontSize: '13px' }}> <Box sx={{ display: 'flex', flexDirection: 'column', gap: 1.5, fontFamily: FONT_SANS, fontSize: '13px' }}>
<Row label="EXECUTING ADMIN" value={adminName} /> <DetailRow label="EXECUTING ADMIN" value={adminName} />
<Row label="ADMIN UID" value={typedLog.admin_id as string} isMono /> <DetailRow label="ADMIN UID" value={typedLog.admin_id as string} isMono />
<Row label="TARGET CLASSIFIER" value={typedLog.target_type as string} isMono /> <DetailRow label="TARGET CLASSIFIER" value={typedLog.target_type as string} isMono />
<Row label="TARGET ID" value={typedLog.target_id as string} isMono /> <DetailRow label="TARGET ID" value={typedLog.target_id as string} isMono />
<Row label="LOGGED TIMESTAMP" value={new Date(typedLog.created_at as string).toLocaleString()} /> <DetailRow label="LOGGED TIMESTAMP" value={new Date(typedLog.created_at as string).toLocaleString()} />
</Box> </Box>
</DoubleBezelCard> </DoubleBezelCard>
@ -164,14 +165,3 @@ export default async function AuditLogDetailPage({ params }: PageProps): Promise
</> </>
) )
} }
function Row({ label, value, isMono }: { label: string; value: string; isMono?: boolean }): React.ReactElement {
return (
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', py: 0.5, borderBottom: `1px solid ${C.border}` }}>
<Typography sx={{ fontFamily: FONT_SANS, fontSize: '12px', color: C.dim }}>{label}</Typography>
<Typography sx={{ fontFamily: isMono ? FONT_MONO : FONT_SANS, fontSize: '12px', fontWeight: 600, color: C.bright }}>
{value}
</Typography>
</Box>
)
}

View file

@ -3,7 +3,7 @@
// apps/admin/src/app/(admin)/models/page.tsx // apps/admin/src/app/(admin)/models/page.tsx
// D3RO Voice — Dynamic AI Models & Cloud STT Transcription Provider Manager (Midnight Glass v2) // D3RO Voice — Dynamic AI Models & Cloud STT Transcription Provider Manager (Midnight Glass v2)
import React, { useState, useEffect } from 'react' import React, { useState, useEffect, useRef } from 'react'
import { import {
Box, Box,
Typography, Typography,
@ -111,19 +111,29 @@ export default function ServiceModelsPage(): React.ReactElement {
const [llmCompletionCost, setLlmCompletionCost] = useState('0') const [llmCompletionCost, setLlmCompletionCost] = useState('0')
const [llmMemo, setLlmMemo] = useState('') const [llmMemo, setLlmMemo] = useState('')
const mountedRef = useRef(true)
const loadSeqRef = useRef(0)
const loadData = async () => { const loadData = async () => {
const seq = ++loadSeqRef.current
setDataError(null) setDataError(null)
try { try {
const [sttData, llmData] = await Promise.all([fetchSttEndpointsClient(), fetchModelEndpointsClient()]) const [sttData, llmData] = await Promise.all([fetchSttEndpointsClient(), fetchModelEndpointsClient()])
if (!mountedRef.current || loadSeqRef.current !== seq) return
setSttEndpoints(sttData) setSttEndpoints(sttData)
setLlmEndpoints(llmData) setLlmEndpoints(llmData)
} catch (error) { } catch (error) {
if (!mountedRef.current || loadSeqRef.current !== seq) return
setDataError(error instanceof Error ? error.message : '관리자 백엔드에서 엔드포인트를 불러오지 못했습니다.') setDataError(error instanceof Error ? error.message : '관리자 백엔드에서 엔드포인트를 불러오지 못했습니다.')
} }
} }
useEffect(() => { useEffect(() => {
loadData() mountedRef.current = true
void loadData()
return () => {
mountedRef.current = false
}
}, []) }, [])
// ── STT Handlers ────────────────────────────────────────────────────────── // ── STT Handlers ──────────────────────────────────────────────────────────
@ -160,11 +170,17 @@ export default function ServiceModelsPage(): React.ReactElement {
setSttFallbackPriority(ep.fallbackPriority) setSttFallbackPriority(ep.fallbackPriority)
setSttIsDefault(ep.isDefault) setSttIsDefault(ep.isDefault)
setSttTestModalResult(null) setSttTestModalResult(null)
setSttMemo('')
setSttModalOpen(true) setSttModalOpen(true)
} }
const handleSaveSttEndpoint = async (e: React.FormEvent) => { const handleSaveSttEndpoint = async (e: React.FormEvent) => {
e.preventDefault() e.preventDefault()
const parsedCostPerMinute = parseFloat(sttCostPerMinute)
if (!Number.isFinite(parsedCostPerMinute) || parsedCostPerMinute < 0) {
alert('분당 비용은 0 이상의 유효한 숫자여야 합니다.')
return
}
setSttLoading(true) setSttLoading(true)
try { try {
@ -178,8 +194,8 @@ export default function ServiceModelsPage(): React.ReactElement {
language: sttLanguage, language: sttLanguage,
prompt: sttPrompt ? sttPrompt : undefined, prompt: sttPrompt ? sttPrompt : undefined,
temperature: 0.0, temperature: 0.0,
costPerMinute: parseFloat(sttCostPerMinute), costPerMinute: parsedCostPerMinute,
costPerSecond: parseFloat(sttCostPerMinute) / 60, costPerSecond: parsedCostPerMinute / 60,
isDefault: sttIsDefault, isDefault: sttIsDefault,
isActive: true, isActive: true,
fallbackPriority: sttFallbackPriority, fallbackPriority: sttFallbackPriority,
@ -193,11 +209,13 @@ export default function ServiceModelsPage(): React.ReactElement {
} }
await loadData() await loadData()
setSttModalOpen(false) if (mountedRef.current) setSttModalOpen(false)
} catch (err) { } catch (err) {
alert('Error saving STT endpoint: ' + (err instanceof Error ? err.message : String(err))) if (mountedRef.current) {
alert('Error saving STT endpoint: ' + (err instanceof Error ? err.message : String(err)))
}
} finally { } finally {
setSttLoading(false) if (mountedRef.current) setSttLoading(false)
} }
} }
@ -206,6 +224,7 @@ export default function ServiceModelsPage(): React.ReactElement {
if (memo.length < 3) return if (memo.length < 3) return
try { try {
await setDefaultSttEndpointClient(id, memo) await setDefaultSttEndpointClient(id, memo)
if (!mountedRef.current) return
setSttEndpoints((prev) => setSttEndpoints((prev) =>
prev.map((ep) => ({ prev.map((ep) => ({
...ep, ...ep,
@ -213,7 +232,9 @@ export default function ServiceModelsPage(): React.ReactElement {
})) }))
) )
} catch (err) { } catch (err) {
alert('Error: ' + (err instanceof Error ? err.message : String(err))) if (mountedRef.current) {
alert('Error: ' + (err instanceof Error ? err.message : String(err)))
}
} }
} }
@ -221,11 +242,13 @@ export default function ServiceModelsPage(): React.ReactElement {
setSttPingStatus((prev) => ({ ...prev, [id]: 'Testing...' })) setSttPingStatus((prev) => ({ ...prev, [id]: 'Testing...' }))
try { try {
const result = await testSttEndpointClient(id) const result = await testSttEndpointClient(id)
if (!mountedRef.current) return
setSttPingStatus((prev) => ({ setSttPingStatus((prev) => ({
...prev, ...prev,
[id]: result.success ? `⚡ OK • ${result.latencyMs}ms` : `❌ ${result.message}` [id]: result.success ? `⚡ OK • ${result.latencyMs}ms` : `❌ ${result.message}`
})) }))
} catch (error) { } catch (error) {
if (!mountedRef.current) return
setSttPingStatus((prev) => ({ setSttPingStatus((prev) => ({
...prev, ...prev,
[id]: `❌ ${error instanceof Error ? error.message : 'Test failed'}` [id]: `❌ ${error instanceof Error ? error.message : 'Test failed'}`
@ -238,19 +261,21 @@ export default function ServiceModelsPage(): React.ReactElement {
setSttTestModalResult(null) setSttTestModalResult(null)
try { try {
const result = await testSttEndpointClient(sttEditingId ?? 0, sttApiKey, sttEndpointUrl) const result = await testSttEndpointClient(sttEditingId ?? 0, sttApiKey, sttEndpointUrl)
if (!mountedRef.current) return
setSttTestModalResult({ setSttTestModalResult({
success: result.success, success: result.success,
message: result.message, message: result.message,
ms: result.latencyMs, ms: result.latencyMs,
}) })
} catch (err) { } catch (err) {
if (!mountedRef.current) return
setSttTestModalResult({ setSttTestModalResult({
success: false, success: false,
message: err instanceof Error ? err.message : 'Test failed', message: err instanceof Error ? err.message : 'Test failed',
ms: 0, ms: 0,
}) })
} finally { } finally {
setTestingInModal(false) if (mountedRef.current) setTestingInModal(false)
} }
} }
@ -260,9 +285,13 @@ export default function ServiceModelsPage(): React.ReactElement {
if (memo.length < 3) return if (memo.length < 3) return
try { try {
await deleteSttEndpointClient(id, memo) await deleteSttEndpointClient(id, memo)
setSttEndpoints((prev) => prev.filter((ep) => ep.id !== id)) if (mountedRef.current) {
setSttEndpoints((prev) => prev.filter((ep) => ep.id !== id))
}
} catch (err) { } catch (err) {
alert('Error: ' + (err instanceof Error ? err.message : String(err))) if (mountedRef.current) {
alert('Error: ' + (err instanceof Error ? err.message : String(err)))
}
} }
} }
@ -282,6 +311,15 @@ export default function ServiceModelsPage(): React.ReactElement {
const handleAddLlmEndpoint = async (e: React.FormEvent) => { const handleAddLlmEndpoint = async (e: React.FormEvent) => {
e.preventDefault() e.preventDefault()
const parsedPromptCost = parseFloat(llmPromptCost)
const parsedCompletionCost = parseFloat(llmCompletionCost)
if (
!Number.isFinite(parsedPromptCost) || parsedPromptCost < 0
|| !Number.isFinite(parsedCompletionCost) || parsedCompletionCost < 0
) {
alert('토큰 비용은 0 이상의 유효한 숫자여야 합니다.')
return
}
setLlmLoading(true) setLlmLoading(true)
try { try {
@ -291,11 +329,12 @@ export default function ServiceModelsPage(): React.ReactElement {
provider: llmProvider, provider: llmProvider,
endpointUrl: llmEndpointUrl, endpointUrl: llmEndpointUrl,
apiKey: llmApiKey, apiKey: llmApiKey,
costPer1kPromptTokens: parseFloat(llmPromptCost), costPer1kPromptTokens: parsedPromptCost,
costPer1kCompletionTokens: parseFloat(llmCompletionCost), costPer1kCompletionTokens: parsedCompletionCost,
memo: llmMemo.trim(), memo: llmMemo.trim(),
}) })
await loadData() await loadData()
if (!mountedRef.current) return
setLlmModalOpen(false) setLlmModalOpen(false)
setLlmModelId('') setLlmModelId('')
setLlmModelName('') setLlmModelName('')
@ -303,9 +342,11 @@ export default function ServiceModelsPage(): React.ReactElement {
setLlmApiKey('') setLlmApiKey('')
setLlmMemo('') setLlmMemo('')
} catch (err) { } catch (err) {
alert('Error: ' + (err instanceof Error ? err.message : String(err))) if (mountedRef.current) {
alert('Error: ' + (err instanceof Error ? err.message : String(err)))
}
} finally { } finally {
setLlmLoading(false) if (mountedRef.current) setLlmLoading(false)
} }
} }
@ -315,9 +356,13 @@ export default function ServiceModelsPage(): React.ReactElement {
if (memo.length < 3) return if (memo.length < 3) return
try { try {
await deleteModelEndpointClient(id, memo) await deleteModelEndpointClient(id, memo)
setLlmEndpoints((current) => current.filter((endpoint) => endpoint.id !== id)) if (mountedRef.current) {
setLlmEndpoints((current) => current.filter((endpoint) => endpoint.id !== id))
}
} catch (error) { } catch (error) {
alert('Error: ' + (error instanceof Error ? error.message : String(error))) if (mountedRef.current) {
alert('Error: ' + (error instanceof Error ? error.message : String(error)))
}
} }
} }

View file

@ -7,6 +7,8 @@ import { DoubleBezelCard, TactileBadge } from '@d3ro/ui/components/ds'
import { getSupabaseAdminClient, isSupabaseAdminConfigured } from '@/lib/supabase-admin' import { getSupabaseAdminClient, isSupabaseAdminConfigured } from '@/lib/supabase-admin'
import { UnavailableAdminPanel } from '@/components/unavailable-admin-panel' import { UnavailableAdminPanel } from '@/components/unavailable-admin-panel'
import { requireManager, hasMinRole } from '@/lib/admin-guard' import { requireManager, hasMinRole } from '@/lib/admin-guard'
import { ADMIN_UUID_PATTERN } from '@/lib/request-security'
import { DetailRow } from '@/components/detail-row'
import Link from 'next/link' import Link from 'next/link'
import { notFound } from 'next/navigation' import { notFound } from 'next/navigation'
import { SubscriptionDetailClient } from './client' import { SubscriptionDetailClient } from './client'
@ -16,6 +18,7 @@ interface PageProps {
} }
export default async function SubscriptionDetailPage({ params }: PageProps): Promise<React.ReactElement> { export default async function SubscriptionDetailPage({ params }: PageProps): Promise<React.ReactElement> {
const admin = await requireManager()
if (!isSupabaseAdminConfigured()) { if (!isSupabaseAdminConfigured()) {
return ( return (
<UnavailableAdminPanel <UnavailableAdminPanel
@ -26,7 +29,7 @@ export default async function SubscriptionDetailPage({ params }: PageProps): Pro
) )
} }
const { id: userId } = await params const { id: userId } = await params
const admin = await requireManager() if (!ADMIN_UUID_PATTERN.test(userId)) notFound()
const supabase = await getSupabaseAdminClient() const supabase = await getSupabaseAdminClient()
const [subRes, profileRes, auditRes] = await Promise.all([ const [subRes, profileRes, auditRes] = await Promise.all([
@ -123,9 +126,9 @@ export default async function SubscriptionDetailPage({ params }: PageProps): Pro
Account Identifiers Account Identifiers
</Typography> </Typography>
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 1.5, fontFamily: FONT_SANS, fontSize: '13px' }}> <Box sx={{ display: 'flex', flexDirection: 'column', gap: 1.5, fontFamily: FONT_SANS, fontSize: '13px' }}>
<Row label="Customer Name" value={(profile.name as string) ?? '-'} /> <DetailRow label="Customer Name" value={(profile.name as string) ?? '-'} />
<Row label="Account UID" value={userId} isMono /> <DetailRow label="Account UID" value={userId} isMono />
<Row label="Assigned Role" value={((profile.role as string) ?? 'user').toUpperCase()} isMono /> <DetailRow label="Assigned Role" value={((profile.role as string) ?? 'user').toUpperCase()} isMono />
</Box> </Box>
</DoubleBezelCard> </DoubleBezelCard>
@ -135,11 +138,11 @@ export default async function SubscriptionDetailPage({ params }: PageProps): Pro
Contract Status & Pricing Contract Status & Pricing
</Typography> </Typography>
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 1.5, fontFamily: FONT_SANS, fontSize: '13px' }}> <Box sx={{ display: 'flex', flexDirection: 'column', gap: 1.5, fontFamily: FONT_SANS, fontSize: '13px' }}>
<Row label="Plan Level" value={isProPlus ? 'PRO+ VIP' : isPro ? 'PRO' : 'FREE'} /> <DetailRow label="Plan Level" value={isProPlus ? 'PRO+ VIP' : isPro ? 'PRO' : 'FREE'} />
<Row label="Current Status" value={sub?.status ? String(sub.status).toUpperCase() : 'NO SUBSCRIPTION'} /> <DetailRow label="Current Status" value={sub?.status ? String(sub.status).toUpperCase() : 'NO SUBSCRIPTION'} />
<Row label="Payment Gateway" value={sub?.payment_provider ? String(sub.payment_provider).toUpperCase() : 'NOT ASSIGNED'} /> <DetailRow label="Payment Gateway" value={sub?.payment_provider ? String(sub.payment_provider).toUpperCase() : 'NOT ASSIGNED'} />
<Row label="Contract Expires / Renews" value={sub?.current_period_end ? new Date(sub.current_period_end as string).toLocaleDateString() : 'NOT SET'} /> <DetailRow label="Contract Expires / Renews" value={sub?.current_period_end ? new Date(sub.current_period_end as string).toLocaleDateString() : 'NOT SET'} />
<Row label="Overage Credits" value={sub ? String(sub.overage_credits) : 'NO SUBSCRIPTION'} isMono /> <DetailRow label="Overage Credits" value={sub ? String(sub.overage_credits) : 'NO SUBSCRIPTION'} isMono />
</Box> </Box>
</DoubleBezelCard> </DoubleBezelCard>
</Box> </Box>
@ -215,14 +218,3 @@ export default async function SubscriptionDetailPage({ params }: PageProps): Pro
</> </>
) )
} }
function Row({ label, value, isMono }: { label: string; value: string; isMono?: boolean }): React.ReactElement {
return (
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', py: 0.5, borderBottom: `1px solid ${C.border}` }}>
<Typography sx={{ fontFamily: FONT_SANS, fontSize: '12px', color: C.dim }}>{label}</Typography>
<Typography sx={{ fontFamily: isMono ? FONT_MONO : FONT_SANS, fontSize: '12px', fontWeight: 600, color: C.bright }}>
{value}
</Typography>
</Box>
)
}

View file

@ -7,6 +7,8 @@ import { DoubleBezelCard, TactileBadge } from '@d3ro/ui/components/ds'
import { getSupabaseAdminClient, isSupabaseAdminConfigured } from '@/lib/supabase-admin' import { getSupabaseAdminClient, isSupabaseAdminConfigured } from '@/lib/supabase-admin'
import { UnavailableAdminPanel } from '@/components/unavailable-admin-panel' import { UnavailableAdminPanel } from '@/components/unavailable-admin-panel'
import { requireManager, isAdmin } from '@/lib/admin-guard' import { requireManager, isAdmin } from '@/lib/admin-guard'
import { ADMIN_UUID_PATTERN } from '@/lib/request-security'
import { DetailRow } from '@/components/detail-row'
import Link from 'next/link' import Link from 'next/link'
import { notFound } from 'next/navigation' import { notFound } from 'next/navigation'
import { RoleChangeButton } from './role-change-button' import { RoleChangeButton } from './role-change-button'
@ -17,6 +19,7 @@ interface PageProps {
} }
export default async function AdminUserDetailPage({ params }: PageProps): Promise<React.ReactElement> { export default async function AdminUserDetailPage({ params }: PageProps): Promise<React.ReactElement> {
const admin = await requireManager()
if (!isSupabaseAdminConfigured()) { if (!isSupabaseAdminConfigured()) {
return ( return (
<UnavailableAdminPanel <UnavailableAdminPanel
@ -27,7 +30,7 @@ export default async function AdminUserDetailPage({ params }: PageProps): Promis
) )
} }
const { id } = await params const { id } = await params
const admin = await requireManager() if (!ADMIN_UUID_PATTERN.test(id)) notFound()
const supabase = await getSupabaseAdminClient() const supabase = await getSupabaseAdminClient()
const [authRes, profileRes, subRes, usageRes] = await Promise.all([ const [authRes, profileRes, subRes, usageRes] = await Promise.all([
@ -161,11 +164,11 @@ export default async function AdminUserDetailPage({ params }: PageProps): Promis
</Box> </Box>
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 1.5, fontFamily: FONT_SANS, fontSize: '13px' }}> <Box sx={{ display: 'flex', flexDirection: 'column', gap: 1.5, fontFamily: FONT_SANS, fontSize: '13px' }}>
<Row label="Account UID" value={(profile.id as string) || id} isMono /> <DetailRow label="Account UID" value={(profile.id as string) || id} isMono />
<Row label="Assigned Role" value={userRole.toUpperCase()} valueColor={userRole === 'super_admin' ? C.purple400 : C.cyanLight} isMono /> <DetailRow label="Assigned Role" value={userRole.toUpperCase()} valueColor={userRole === 'super_admin' ? C.purple400 : C.cyanLight} isMono />
<Row label="Language Locale" value={(profile.locale as string) || 'Not reported'} isMono /> <DetailRow label="Language Locale" value={(profile.locale as string) || 'Not reported'} isMono />
<Row label="Created Date" value={new Date(profile.created_at as string).toLocaleDateString()} /> <DetailRow label="Created Date" value={new Date(profile.created_at as string).toLocaleDateString()} />
<Row label="Last Active Session" value={profile.last_login ? new Date(profile.last_login as string).toLocaleString() : 'Never signed in'} /> <DetailRow label="Last Active Session" value={profile.last_login ? new Date(profile.last_login as string).toLocaleString() : 'Never signed in'} />
</Box> </Box>
</DoubleBezelCard> </DoubleBezelCard>
@ -181,10 +184,10 @@ export default async function AdminUserDetailPage({ params }: PageProps): Promis
</Box> </Box>
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 1.5, mb: 3 }}> <Box sx={{ display: 'flex', flexDirection: 'column', gap: 1.5, mb: 3 }}>
<Row label="Plan Tier" value={isProPlus ? 'PRO+ VIP' : isPro ? 'PRO' : tier === 'free' ? 'FREE' : 'Unavailable'} /> <DetailRow label="Plan Tier" value={isProPlus ? 'PRO+ VIP' : isPro ? 'PRO' : tier === 'free' ? 'FREE' : 'Unavailable'} />
<Row label="Billing Provider" value={sub?.payment_provider ? String(sub.payment_provider) : 'Not assigned'} /> <DetailRow label="Billing Provider" value={sub?.payment_provider ? String(sub.payment_provider) : 'Not assigned'} />
<Row label="Current Period End" value={sub?.current_period_end ? new Date(sub.current_period_end as string).toLocaleDateString() : 'Not set'} /> <DetailRow label="Current Period End" value={sub?.current_period_end ? new Date(sub.current_period_end as string).toLocaleDateString() : 'Not set'} />
<Row label="Cancel Scheduled" value={sub?.cancel_at ? new Date(sub.cancel_at as string).toLocaleDateString() : 'Not scheduled'} /> <DetailRow label="Cancel Scheduled" value={sub?.cancel_at ? new Date(sub.cancel_at as string).toLocaleDateString() : 'Not scheduled'} />
</Box> </Box>
<Box sx={{ p: 2, borderRadius: '12px', bgcolor: 'var(--d3-bg-inset-soft)', border: `1px solid ${C.border}` }}> <Box sx={{ p: 2, borderRadius: '12px', bgcolor: 'var(--d3-bg-inset-soft)', border: `1px solid ${C.border}` }}>
@ -280,19 +283,3 @@ export default async function AdminUserDetailPage({ params }: PageProps): Promis
</> </>
) )
} }
function Row({ label, value, valueColor, isMono }: {
label: string
value: string
valueColor?: string
isMono?: boolean
}): React.ReactElement {
return (
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', py: 0.5, borderBottom: `1px solid ${C.border}` }}>
<Typography sx={{ fontFamily: FONT_SANS, fontSize: '12px', color: C.dim }}>{label}</Typography>
<Typography sx={{ fontFamily: isMono ? FONT_MONO : FONT_SANS, fontSize: '12px', fontWeight: 600, color: valueColor ?? C.bright }}>
{value}
</Typography>
</Box>
)
}

View file

@ -1,6 +1,7 @@
import { NextRequest, NextResponse } from 'next/server' import { NextRequest, NextResponse } from 'next/server'
import { AdminBackendError, fetchAdminBackend } from '@/lib/backend-session' import { AdminBackendError, fetchAdminBackend } from '@/lib/backend-session'
import type { AdminRole } from '@/lib/admin-session' import type { AdminRole } from '@/lib/admin-session'
import { isSameOriginRequest, readBoundedTextBody } from '@/lib/request-security'
export const runtime = 'nodejs' export const runtime = 'nodejs'
@ -40,26 +41,6 @@ function requiredRole(method: string, path: string): AdminRole | null {
return null return null
} }
function isSameOrigin(request: NextRequest): boolean {
const origin = request.headers.get('origin')
if (!origin) return request.headers.get('sec-fetch-site') !== 'cross-site'
try {
const requestOrigin = new URL(origin).origin
if (requestOrigin === request.nextUrl.origin) return true
const forwardedProto = request.headers.get('x-forwarded-proto')?.split(',')[0]?.trim().toLowerCase()
const forwardedHost = request.headers.get('x-forwarded-host')?.split(',')[0]?.trim()
?? request.headers.get('host')?.trim()
if ((forwardedProto !== 'http' && forwardedProto !== 'https') ||
!forwardedHost || !/^[A-Za-z0-9.:[\]-]+$/.test(forwardedHost)) {
return false
}
return requestOrigin === `${forwardedProto}://${forwardedHost}`
} catch {
return false
}
}
async function proxy( async function proxy(
request: NextRequest, request: NextRequest,
context: { params: Promise<{ segments: string[] }> } context: { params: Promise<{ segments: string[] }> }
@ -68,15 +49,19 @@ async function proxy(
const path = pathFor(segments) const path = pathFor(segments)
const role = path ? requiredRole(request.method, path) : null const role = path ? requiredRole(request.method, path) : null
if (!path || !role) return NextResponse.json({ error: 'admin_route_not_allowed' }, { status: 404 }) if (!path || !role) return NextResponse.json({ error: 'admin_route_not_allowed' }, { status: 404 })
if (request.method !== 'GET' && !isSameOrigin(request)) { if (request.method !== 'GET' && !isSameOriginRequest(request)) {
return NextResponse.json({ error: 'cross_site_request_rejected' }, { status: 403 }) return NextResponse.json({ error: 'cross_site_request_rejected' }, { status: 403 })
} }
let body: string | undefined let body: string | undefined
if (request.method !== 'GET') { if (request.method !== 'GET') {
body = await request.text() try {
if (new TextEncoder().encode(body).byteLength > 64 * 1024) { body = await readBoundedTextBody(request, 64 * 1024)
return NextResponse.json({ error: 'request_too_large' }, { status: 413 }) } catch (error) {
if (error instanceof AdminBackendError) {
return NextResponse.json({ error: error.message }, { status: error.status })
}
return NextResponse.json({ error: 'invalid_request' }, { status: 400 })
} }
} }

View file

@ -6,6 +6,7 @@ import { NextRequest, NextResponse } from 'next/server'
import { issueSignedLicenseKey, type SignedLicensePayload } from '@d3ro/core/utils/crypto-license' import { issueSignedLicenseKey, type SignedLicensePayload } from '@d3ro/core/utils/crypto-license'
import type { LicenseTier } from '@d3ro/core/types' import type { LicenseTier } from '@d3ro/core/types'
import { AdminBackendError, fetchAdminBackend, requireVerifiedBackendSession } from '@/lib/backend-session' import { AdminBackendError, fetchAdminBackend, requireVerifiedBackendSession } from '@/lib/backend-session'
import { isSameOriginRequest, readBoundedJsonObject } from '@/lib/request-security'
export const runtime = 'nodejs' export const runtime = 'nodejs'
@ -32,19 +33,12 @@ function resolveSigningKey(): string | null {
return null return null
} }
function isSameOrigin(request: NextRequest): boolean {
const origin = request.headers.get('origin')
if (!origin) return request.headers.get('sec-fetch-site') !== 'cross-site'
try {
return new URL(origin).origin === request.nextUrl.origin
} catch {
return false
}
}
export async function POST(request: NextRequest): Promise<NextResponse> { export async function POST(request: NextRequest): Promise<NextResponse> {
if (!isSameOrigin(request)) { if (!isSameOriginRequest(request)) {
return NextResponse.json({ error: 'cross_site_request_rejected' }, { status: 403 }) return NextResponse.json(
{ error: 'cross_site_request_rejected' },
{ status: 403, headers: { 'Cache-Control': 'no-store' } }
)
} }
try { try {
@ -52,20 +46,24 @@ export async function POST(request: NextRequest): Promise<NextResponse> {
} catch (error) { } catch (error) {
const status = error instanceof AdminBackendError ? error.status : 401 const status = error instanceof AdminBackendError ? error.status : 401
const message = error instanceof AdminBackendError ? error.message : 'admin_session_invalid' const message = error instanceof AdminBackendError ? error.message : 'admin_session_invalid'
return NextResponse.json({ error: message }, { status }) return NextResponse.json({ error: message }, { status, headers: { 'Cache-Control': 'no-store' } })
} }
let body: unknown let candidate: Record<string, unknown>
try { try {
body = await request.json() candidate = await readBoundedJsonObject(request, 16 * 1024)
} catch { } catch (error) {
return NextResponse.json({ error: 'invalid_request' }, { status: 400 }) if (error instanceof AdminBackendError) {
return NextResponse.json(
{ error: error.message },
{ status: error.status, headers: { 'Cache-Control': 'no-store' } }
)
}
return NextResponse.json(
{ error: 'invalid_request' },
{ status: 400, headers: { 'Cache-Control': 'no-store' } }
)
} }
if (!body || typeof body !== 'object' || Array.isArray(body)) {
return NextResponse.json({ error: 'invalid_request' }, { status: 400 })
}
const candidate = body as Record<string, unknown>
const customerEmail = typeof candidate.customerEmail === 'string' ? candidate.customerEmail.trim().toLowerCase() : '' const customerEmail = typeof candidate.customerEmail === 'string' ? candidate.customerEmail.trim().toLowerCase() : ''
const tier = candidate.tier const tier = candidate.tier
const validity = typeof candidate.validity === 'string' ? candidate.validity : '' const validity = typeof candidate.validity === 'string' ? candidate.validity : ''
@ -139,5 +137,5 @@ export async function POST(request: NextRequest): Promise<NextResponse> {
licenseId: payload.licenseId, licenseId: payload.licenseId,
expiresAt: payload.expiresAt, expiresAt: payload.expiresAt,
auditRecorded auditRecorded
}) }, { headers: { 'Cache-Control': 'no-store' } })
} }

View file

@ -1,46 +1,17 @@
import { NextRequest, NextResponse } from 'next/server' import { NextRequest, NextResponse } from 'next/server'
import { AdminBackendError, requireVerifiedBackendSession } from '@/lib/backend-session' import { AdminBackendError, requireVerifiedBackendSession } from '@/lib/backend-session'
import { ADMIN_UUID_PATTERN, isSameOriginRequest, readBoundedJsonObject } from '@/lib/request-security'
import { getSupabaseAdminClient } from '@/lib/supabase-admin' import { getSupabaseAdminClient } from '@/lib/supabase-admin'
export const runtime = 'nodejs' export const runtime = 'nodejs'
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i const UUID = ADMIN_UUID_PATTERN
const ROLES = new Set(['user', 'manager', 'admin', 'super_admin']) const ROLES = new Set(['user', 'manager', 'admin', 'super_admin'])
const TIERS = new Set(['free', 'pro', 'pro_plus']) const TIERS = new Set(['free', 'pro', 'pro_plus'])
const STATUSES = new Set(['active', 'canceled', 'past_due', 'expired']) const STATUSES = new Set(['active', 'canceled', 'past_due', 'expired'])
function sameOrigin(request: NextRequest): boolean {
const origin = request.headers.get('origin')
if (!origin) return request.headers.get('sec-fetch-site') !== 'cross-site'
try {
const requestOrigin = new URL(origin).origin
if (requestOrigin === request.nextUrl.origin) return true
const forwardedProto = request.headers.get('x-forwarded-proto')?.split(',')[0]?.trim().toLowerCase()
const forwardedHost = request.headers.get('x-forwarded-host')?.split(',')[0]?.trim()
?? request.headers.get('host')?.trim()
if ((forwardedProto !== 'http' && forwardedProto !== 'https') ||
!forwardedHost || !/^[A-Za-z0-9.:[\]-]+$/.test(forwardedHost)) {
return false
}
return requestOrigin === `${forwardedProto}://${forwardedHost}`
} catch {
return false
}
}
async function bodyObject(request: NextRequest): Promise<Record<string, unknown>> { async function bodyObject(request: NextRequest): Promise<Record<string, unknown>> {
if (request.headers.get('content-type')?.split(';', 1)[0]?.trim().toLowerCase() !== 'application/json') { return await readBoundedJsonObject(request, 32 * 1024, { requireJsonContentType: true })
throw new AdminBackendError('application_json_required', 415)
}
const text = await request.text()
if (new TextEncoder().encode(text).byteLength > 32 * 1024) throw new AdminBackendError('request_too_large', 413)
try {
const parsed = JSON.parse(text) as unknown
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) throw new Error()
return parsed as Record<string, unknown>
} catch {
throw new AdminBackendError('invalid_request', 400)
}
} }
function onlyKeys(body: Record<string, unknown>, allowed: readonly string[]): void { function onlyKeys(body: Record<string, unknown>, allowed: readonly string[]): void {
@ -220,7 +191,7 @@ async function handlePayments(request: NextRequest): Promise<NextResponse> {
} }
async function route(request: NextRequest, context: { params: Promise<{ operation: string }> }): Promise<NextResponse> { async function route(request: NextRequest, context: { params: Promise<{ operation: string }> }): Promise<NextResponse> {
if (request.method !== 'GET' && !sameOrigin(request)) { if (request.method !== 'GET' && !isSameOriginRequest(request)) {
return NextResponse.json({ error: 'cross_site_request_rejected' }, { status: 403 }) return NextResponse.json({ error: 'cross_site_request_rejected' }, { status: 403 })
} }
try { try {

View file

@ -1,4 +1,6 @@
import { NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { AdminBackendError } from '@/lib/backend-session'
import { readBoundedJsonObject } from '@/lib/request-security'
import { import {
checkRateLimit, checkRateLimit,
recordFailedAttempt, recordFailedAttempt,
@ -52,23 +54,24 @@ function failure(key: string, status: number, retryAfterSeconds?: number): NextR
}, },
{ {
status, status,
headers: retryAfterSeconds ? { 'Retry-After': String(retryAfterSeconds) } : undefined headers: {
'Cache-Control': 'no-store',
...(retryAfterSeconds ? { 'Retry-After': String(retryAfterSeconds) } : {})
}
} }
) )
} }
export async function POST(request: Request): Promise<NextResponse> { export async function POST(request: Request): Promise<NextResponse> {
let body: unknown let candidate: Record<string, unknown>
try { try {
body = await request.json() candidate = await readBoundedJsonObject(request, 16 * 1024)
} catch { } catch (error) {
if (error instanceof AdminBackendError) {
return failure(error.message, error.status)
}
return failure('invalid_request', 400) return failure('invalid_request', 400)
} }
if (!body || typeof body !== 'object' || Array.isArray(body)) {
return failure('invalid_request', 400)
}
const candidate = body as Record<string, unknown>
if (candidate.trap) return failure('access_denied', 403) if (candidate.trap) return failure('access_denied', 403)
const email = const email =
typeof candidate.usernameOrEmail === 'string' typeof candidate.usernameOrEmail === 'string'
@ -132,10 +135,13 @@ export async function POST(request: Request): Promise<NextResponse> {
expiresAt expiresAt
}) })
const response = NextResponse.json({ const response = NextResponse.json(
success: true, {
user: { email: authenticatedEmail, role } success: true,
}) user: { email: authenticatedEmail, role }
},
{ headers: { 'Cache-Control': 'no-store' } }
)
response.cookies.set({ response.cookies.set({
name: 'd3ro_admin_session', name: 'd3ro_admin_session',
value: signedCookieValue, value: signedCookieValue,

View file

@ -1,40 +1,49 @@
// apps/admin/src/app/api/auth/logout/route.ts // apps/admin/src/app/api/auth/logout/route.ts
// D3RO Voice — Admin Session Logout Handler // D3RO Voice — Admin Session Logout Handler
import { NextResponse } from 'next/server' import { NextRequest, NextResponse } from 'next/server'
import { adminCookieSecure } from '@/lib/admin-session' import { adminCookieSecure } from '@/lib/admin-session'
import { isSameOriginRequest } from '@/lib/request-security'
export async function POST(): Promise<NextResponse> { function clearAdminSessionCookie(response: NextResponse): NextResponse {
const response = NextResponse.json({ success: true, message: 'Logged out successfully' })
response.cookies.set({ response.cookies.set({
name: 'd3ro_admin_session', name: 'd3ro_admin_session',
value: '', value: '',
httpOnly: true, httpOnly: true,
secure: adminCookieSecure(), secure: adminCookieSecure(),
sameSite: 'lax', sameSite: 'strict',
path: '/', path: '/',
maxAge: 0, maxAge: 0,
expires: new Date(0), expires: new Date(0),
}) })
return response return response
} }
export async function GET(request: Request): Promise<NextResponse> { export async function POST(request: NextRequest): Promise<NextResponse> {
if (!isSameOriginRequest(request)) {
return NextResponse.json(
{ error: 'cross_site_request_rejected' },
{ status: 403, headers: { 'Cache-Control': 'no-store' } }
)
}
const response = NextResponse.json(
{ success: true, message: 'Logged out successfully' },
{ headers: { 'Cache-Control': 'no-store' } }
)
return clearAdminSessionCookie(response)
}
export async function GET(request: NextRequest): Promise<NextResponse> {
if (request.headers.get('sec-fetch-site') === 'cross-site') {
return NextResponse.json(
{ error: 'cross_site_request_rejected' },
{ status: 403, headers: { 'Cache-Control': 'no-store' } }
)
}
const url = new URL(request.url) const url = new URL(request.url)
const response = NextResponse.redirect(new URL('/login', url.origin)) const response = NextResponse.redirect(new URL('/login', url.origin))
response.headers.set('Cache-Control', 'no-store')
response.cookies.set({ return clearAdminSessionCookie(response)
name: 'd3ro_admin_session',
value: '',
httpOnly: true,
secure: adminCookieSecure(),
sameSite: 'lax',
path: '/',
maxAge: 0,
expires: new Date(0),
})
return response
} }

View file

@ -1,6 +1,6 @@
'use client' 'use client'
import { useState } from 'react' import { useEffect, useRef, useState } from 'react'
import { Box, Tooltip } from '@mui/material' import { Box, Tooltip } from '@mui/material'
import { Check, Copy } from 'lucide-react' import { Check, Copy } from 'lucide-react'
import { C, FONT_MONO } from '@/lib/console-theme' import { C, FONT_MONO } from '@/lib/console-theme'
@ -11,12 +11,33 @@ interface ChecksumCopyProps {
export function ChecksumCopy({ value }: ChecksumCopyProps): React.ReactElement { export function ChecksumCopy({ value }: ChecksumCopyProps): React.ReactElement {
const [copied, setCopied] = useState(false) const [copied, setCopied] = useState(false)
const mountedRef = useRef(true)
const resetTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
useEffect(() => {
mountedRef.current = true
return () => {
mountedRef.current = false
if (resetTimerRef.current !== null) {
clearTimeout(resetTimerRef.current)
resetTimerRef.current = null
}
}
}, [])
const handleCopy = (): void => { const handleCopy = (): void => {
void navigator.clipboard.writeText(value).then(() => { if (!navigator.clipboard?.writeText) return
setCopied(true) void navigator.clipboard.writeText(value)
setTimeout(() => setCopied(false), 2000) .then(() => {
}) if (!mountedRef.current) return
setCopied(true)
if (resetTimerRef.current !== null) clearTimeout(resetTimerRef.current)
resetTimerRef.current = setTimeout(() => {
resetTimerRef.current = null
if (mountedRef.current) setCopied(false)
}, 2000)
})
.catch(() => undefined)
} }
return ( return (

View file

@ -0,0 +1,37 @@
import { Box, Typography } from '@mui/material'
import { C, FONT_MONO, FONT_SANS } from '@/lib/console-theme'
interface DetailRowProps {
label: string
value: string
valueColor?: string
isMono?: boolean
}
export function DetailRow({ label, value, valueColor, isMono }: DetailRowProps): React.ReactElement {
return (
<Box
sx={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
py: 0.5,
borderBottom: `1px solid ${C.border}`,
}}
>
<Typography sx={{ fontFamily: FONT_SANS, fontSize: '12px', color: C.dim }}>
{label}
</Typography>
<Typography
sx={{
fontFamily: isMono ? FONT_MONO : FONT_SANS,
fontSize: '12px',
fontWeight: 600,
color: valueColor ?? C.bright,
}}
>
{value}
</Typography>
</Box>
)
}

View file

@ -3,7 +3,7 @@
// apps/admin/src/components/license-issuer-dialog.tsx // apps/admin/src/components/license-issuer-dialog.tsx
// Ed25519 라이선스 발급 다이얼로그 — 서명은 서버(/api/admin/license)에서만 수행한다. // Ed25519 라이선스 발급 다이얼로그 — 서명은 서버(/api/admin/license)에서만 수행한다.
import { useState } from 'react' import { useEffect, useRef, useState } from 'react'
import { import {
Dialog, Dialog,
DialogTitle, DialogTitle,
@ -52,6 +52,19 @@ export function LicenseIssuerDialog({ open, onClose }: LicenseIssuerDialogProps)
const [loading, setLoading] = useState(false) const [loading, setLoading] = useState(false)
const [copied, setCopied] = useState(false) const [copied, setCopied] = useState(false)
const [error, setError] = useState<string | null>(null) const [error, setError] = useState<string | null>(null)
const mountedRef = useRef(true)
const copyTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
useEffect(() => {
mountedRef.current = true
return () => {
mountedRef.current = false
if (copyTimerRef.current !== null) {
clearTimeout(copyTimerRef.current)
copyTimerRef.current = null
}
}
}, [])
const handleGenerate = async (): Promise<void> => { const handleGenerate = async (): Promise<void> => {
setError(null) setError(null)
@ -78,20 +91,31 @@ export function LicenseIssuerDialog({ open, onClose }: LicenseIssuerDialogProps)
if (!response.ok || data.success !== true || typeof data.licenseKey !== 'string') { if (!response.ok || data.success !== true || typeof data.licenseKey !== 'string') {
throw new Error(describeIssueError(data.error)) throw new Error(describeIssueError(data.error))
} }
if (!mountedRef.current) return
setGeneratedKey(data.licenseKey) setGeneratedKey(data.licenseKey)
setAuditRecorded(data.auditRecorded !== false) setAuditRecorded(data.auditRecorded !== false)
} catch (err) { } catch (err) {
setError(err instanceof Error ? err.message : 'Failed to generate license key') if (mountedRef.current) {
setError(err instanceof Error ? err.message : 'Failed to generate license key')
}
} finally { } finally {
setLoading(false) if (mountedRef.current) setLoading(false)
} }
} }
const handleCopy = (): void => { const handleCopy = (): void => {
if (!generatedKey) return if (!generatedKey || !navigator.clipboard?.writeText) return
void navigator.clipboard.writeText(generatedKey) void navigator.clipboard.writeText(generatedKey)
setCopied(true) .then(() => {
setTimeout(() => setCopied(false), 3000) if (!mountedRef.current) return
setCopied(true)
if (copyTimerRef.current !== null) clearTimeout(copyTimerRef.current)
copyTimerRef.current = setTimeout(() => {
copyTimerRef.current = null
if (mountedRef.current) setCopied(false)
}, 3000)
})
.catch(() => undefined)
} }
const inputSx = { const inputSx = {

View file

@ -36,17 +36,25 @@ export function PaymentHistory({ userId }: PaymentHistoryProps): React.ReactElem
const [error, setError] = useState<string | null>(null) const [error, setError] = useState<string | null>(null)
useEffect(() => { useEffect(() => {
let active = true
setLoading(true)
setError(null)
const load = async (): Promise<void> => { const load = async (): Promise<void> => {
try { try {
const result = await callAdminApi<PaymentData>(`admin-payments?userId=${userId}`) const result = await callAdminApi<PaymentData>(`admin-payments?userId=${encodeURIComponent(userId)}`)
if (!active) return
setData(result) setData(result)
} catch (caught) { } catch (caught) {
if (!active) return
setError(caught instanceof Error ? caught.message : 'Failed to load payment data') setError(caught instanceof Error ? caught.message : 'Failed to load payment data')
} finally { } finally {
setLoading(false) if (active) setLoading(false)
} }
} }
void load() void load()
return () => {
active = false
}
}, [userId]) }, [userId])
if (loading) { if (loading) {

View file

@ -3,6 +3,7 @@
import { cookies } from 'next/headers' import { cookies } from 'next/headers'
import { redirect } from 'next/navigation' import { redirect } from 'next/navigation'
import { hasMinimumAdminRole } from '@d3ro/core/admin-roles'
import { verifySession } from './security' import { verifySession } from './security'
import type { AdminRole } from './admin-session' import type { AdminRole } from './admin-session'
@ -57,9 +58,7 @@ export async function requireSuperAdmin(): Promise<AdminUser> {
export function hasMinRole(user?: AdminUser | null, minRole: AdminRole = 'manager'): boolean { export function hasMinRole(user?: AdminUser | null, minRole: AdminRole = 'manager'): boolean {
if (!user) return false if (!user) return false
if (user.role === 'super_admin') return true return hasMinimumAdminRole(user.role, minRole)
if (user.role === 'admin' && minRole !== 'super_admin') return true
return user.role === minRole
} }
export function isSuperAdmin(user?: AdminUser | null): boolean { export function isSuperAdmin(user?: AdminUser | null): boolean {

View file

@ -0,0 +1,94 @@
import 'server-only'
import type { NextRequest } from 'next/server'
import { AdminBackendError } from '@/lib/backend-session'
export const ADMIN_UUID_PATTERN =
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
export function isSameOriginRequest(request: Pick<NextRequest, 'headers' | 'nextUrl'>): boolean {
const origin = request.headers.get('origin')
if (!origin) return request.headers.get('sec-fetch-site') !== 'cross-site'
try {
const requestOrigin = new URL(origin).origin
if (requestOrigin === request.nextUrl.origin) return true
const forwardedProto = request.headers.get('x-forwarded-proto')?.split(',')[0]?.trim().toLowerCase()
const forwardedHost = request.headers.get('x-forwarded-host')?.split(',')[0]?.trim()
?? request.headers.get('host')?.trim()
if (
(forwardedProto !== 'http' && forwardedProto !== 'https')
|| !forwardedHost
|| !/^[A-Za-z0-9.:[\]-]+$/.test(forwardedHost)
) {
return false
}
return requestOrigin === `${forwardedProto}://${forwardedHost}`
} catch {
return false
}
}
export async function readBoundedTextBody(request: Request, maxBytes: number): Promise<string> {
const contentLengthHeader = request.headers.get('content-length')?.trim()
if (contentLengthHeader) {
const declaredLength = Number(contentLengthHeader)
if (Number.isFinite(declaredLength) && declaredLength > maxBytes) {
throw new AdminBackendError('request_too_large', 413)
}
}
if (!request.body) {
return ''
}
const reader = request.body.getReader()
const chunks: Uint8Array[] = []
let totalBytes = 0
try {
for (let next = await reader.read(); !next.done; next = await reader.read()) {
const { value } = next
totalBytes += value.byteLength
if (totalBytes > maxBytes) {
await reader.cancel().catch(() => undefined)
throw new AdminBackendError('request_too_large', 413)
}
chunks.push(value)
}
} finally {
reader.releaseLock()
}
const combined = new Uint8Array(totalBytes)
let offset = 0
for (const chunk of chunks) {
combined.set(chunk, offset)
offset += chunk.byteLength
}
return new TextDecoder().decode(combined)
}
export async function readBoundedJsonObject(
request: Request,
maxBytes: number,
options: { requireJsonContentType?: boolean } = {},
): Promise<Record<string, unknown>> {
if (options.requireJsonContentType) {
const contentType = request.headers.get('content-type')?.split(';', 1)[0]?.trim().toLowerCase()
if (contentType !== 'application/json') {
throw new AdminBackendError('application_json_required', 415)
}
}
const text = await readBoundedTextBody(request, maxBytes)
try {
const parsed = JSON.parse(text) as unknown
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
throw new Error('non_object_json')
}
return parsed as Record<string, unknown>
} catch {
throw new AdminBackendError('invalid_request', 400)
}
}

View file

@ -91,6 +91,24 @@ public sealed class SttControllerSecurityTests
Assert.Equal("edge-internal", service.LastUserEmail); Assert.Equal("edge-internal", service.LastUserEmail);
} }
[Fact]
public async Task InternalGateway_PropagatesRequestAbortedTokenToSttService()
{
var service = new RecordingSttService();
using var cts = new CancellationTokenSource();
var controller = CreateController(
service,
null,
isAuthenticated: false,
suppliedGatewayToken: GatewayToken,
includeAudioForm: true,
requestAborted: cts.Token);
await controller.TranscribeFromQuotaGateway();
Assert.Equal(cts.Token, service.LastCancellationToken);
}
private static SttController CreateController( private static SttController CreateController(
RecordingSttService service, RecordingSttService service,
string? userId, string? userId,
@ -98,7 +116,8 @@ public sealed class SttControllerSecurityTests
string? email = null, string? email = null,
string configuredGatewayToken = GatewayToken, string configuredGatewayToken = GatewayToken,
string? suppliedGatewayToken = null, string? suppliedGatewayToken = null,
bool includeAudioForm = false) bool includeAudioForm = false,
CancellationToken requestAborted = default)
{ {
var claims = new List<Claim>(); var claims = new List<Claim>();
if (userId != null) claims.Add(new Claim(ClaimTypes.NameIdentifier, userId)); if (userId != null) claims.Add(new Claim(ClaimTypes.NameIdentifier, userId));
@ -106,7 +125,8 @@ public sealed class SttControllerSecurityTests
var identity = new ClaimsIdentity(claims, isAuthenticated ? "test-auth" : null); var identity = new ClaimsIdentity(claims, isAuthenticated ? "test-auth" : null);
var context = new DefaultHttpContext var context = new DefaultHttpContext
{ {
User = new ClaimsPrincipal(identity) User = new ClaimsPrincipal(identity),
RequestAborted = requestAborted
}; };
if (includeAudioForm) if (includeAudioForm)
{ {
@ -152,6 +172,7 @@ public sealed class SttControllerSecurityTests
public int LastUserId { get; private set; } public int LastUserId { get; private set; }
public string? LastUserEmail { get; private set; } public string? LastUserEmail { get; private set; }
public bool LastRecordUsage { get; private set; } = true; public bool LastRecordUsage { get; private set; } = true;
public CancellationToken LastCancellationToken { get; private set; }
public Task<SttTranscribeResponse> TranscribeAsync( public Task<SttTranscribeResponse> TranscribeAsync(
int userId, int userId,
@ -160,19 +181,25 @@ public sealed class SttControllerSecurityTests
byte[]? audioBytes = null, byte[]? audioBytes = null,
string? contentType = null, string? contentType = null,
string? fileName = null, string? fileName = null,
bool recordUsage = true) bool recordUsage = true,
CancellationToken cancellationToken = default)
{ {
TranscribeCalls++; TranscribeCalls++;
LastUserId = userId; LastUserId = userId;
LastUserEmail = userEmail; LastUserEmail = userEmail;
LastRecordUsage = recordUsage; LastRecordUsage = recordUsage;
LastCancellationToken = cancellationToken;
if (Exception != null) throw Exception; if (Exception != null) throw Exception;
return Task.FromResult(new SttTranscribeResponse( return Task.FromResult(new SttTranscribeResponse(
"real transcript", 0.99, "ko", 1, "test", "test-model", 1, 0)); "real transcript", 0.99, "ko", 1, "test", "test-model", 1, 0));
} }
public Task<SttTestResultDto> TestEndpointAsync(int endpointId, string? testApiKey = null, string? testEndpointUrl = null) => public Task<SttTestResultDto> TestEndpointAsync(
int endpointId,
string? testApiKey = null,
string? testEndpointUrl = null,
CancellationToken cancellationToken = default) =>
throw new NotSupportedException(); throw new NotSupportedException();
public Task<List<SttProviderEndpointDto>> GetAllEndpointsAsync() => public Task<List<SttProviderEndpointDto>> GetAllEndpointsAsync() =>

View file

@ -121,6 +121,108 @@ public sealed class SttFailClosedTests
Assert.DoesNotContain(apiKey, fixture.Handler.LastRequestUri, StringComparison.Ordinal); Assert.DoesNotContain(apiKey, fixture.Handler.LastRequestUri, StringComparison.Ordinal);
} }
[Fact]
public async Task OversizedAudioBase64_IsRejectedBeforeDecodingOrCallingProvider()
{
await using var fixture = await SttFixture.CreateAsync(
apiKey: "configured-key",
_ => throw new InvalidOperationException("HTTP should not be called"));
// Over 26 MiB base64 payload (~36.4M chars), prefixed like a browser data URL.
var oversizedPayload = "data:audio/wav;base64," + new string('A', 36_400_000);
await Assert.ThrowsAsync<ArgumentException>(() =>
fixture.Service.TranscribeAsync(
13,
"user@example.test",
new SttTranscribeRequest(AudioBase64: oversizedPayload, Language: "ko")));
Assert.Equal(0, fixture.Handler.CallCount);
Assert.Empty(await fixture.Db.SttUsageLogs.ToListAsync());
}
[Fact]
public async Task ClientCancellation_StopsFailoverChainBeforeCallingFallbackProvider()
{
using var cts = new CancellationTokenSource();
await using var fixture = await SttFixture.CreateAsync(
apiKey: "configured-key",
req =>
{
cts.Cancel();
throw new OperationCanceledException(cts.Token);
},
providerType: "groq",
endpointUrl: "https://primary.example.test/transcriptions");
fixture.Db.SttProviderEndpoints.Add(new SttProviderEndpoint
{
Name = "Backup provider",
ProviderType = "openai",
EndpointUrl = "https://fallback.example.test/transcriptions",
ApiKey = "backup-key",
ModelId = "whisper-backup",
Method = "multipart",
Language = "ko",
IsDefault = false,
IsActive = true,
FallbackPriority = 2
});
await fixture.Db.SaveChangesAsync();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() =>
fixture.Service.TranscribeAsync(
14,
"user@example.test",
new SttTranscribeRequest(Language: "ko"),
new byte[] { 1, 2, 3 },
"audio/webm",
"recording.webm",
cancellationToken: cts.Token));
Assert.Equal(1, fixture.Handler.CallCount);
Assert.Empty(await fixture.Db.ErrorLogs.ToListAsync());
Assert.Empty(await fixture.Db.SttUsageLogs.ToListAsync());
}
[Fact]
public async Task LlmProxyService_ClientCancellation_PropagatesWithoutMockFallback()
{
await using var fixture = await SttFixture.CreateAsync(
apiKey: "unused",
_ => throw new OperationCanceledException());
fixture.Db.ModelEndpoints.Add(new ServiceModelEndpoint
{
ModelId = "remote-llm",
ModelName = "Remote LLM",
Provider = "OpenAI",
EndpointUrl = "https://llm.example.test/v1/chat/completions",
ApiKey = "configured-llm-key",
CostPer1kPromptTokens = 0.001m,
CostPer1kCompletionTokens = 0.002m,
IsActive = true
});
await fixture.Db.SaveChangesAsync();
using var cts = new CancellationTokenSource();
cts.Cancel();
var llmService = new LlmProxyService(
fixture.Db,
new StubHttpClientFactory(fixture.Handler),
NullLogger<LlmProxyService>.Instance);
await Assert.ThrowsAnyAsync<OperationCanceledException>(() =>
llmService.GenerateAsync(
15,
"user@example.test",
new LlmGenerateRequest("Hello", Model: "remote-llm"),
cts.Token));
Assert.Equal(0, fixture.Handler.CallCount);
Assert.Empty(await fixture.Db.UsageLogs.ToListAsync());
Assert.Empty(await fixture.Db.ErrorLogs.ToListAsync());
}
private sealed class SttFixture : IAsyncDisposable private sealed class SttFixture : IAsyncDisposable
{ {
private readonly SqliteConnection _connection; private readonly SqliteConnection _connection;

View file

@ -303,7 +303,7 @@ public class AdminController : ControllerBase
[HttpPost("stt-endpoints/{id}/test")] [HttpPost("stt-endpoints/{id}/test")]
public async Task<IActionResult> TestSttEndpoint(int id) public async Task<IActionResult> TestSttEndpoint(int id)
{ {
var result = await _sttService.TestEndpointAsync(id); var result = await _sttService.TestEndpointAsync(id, cancellationToken: HttpContext.RequestAborted);
return Ok(result); return Ok(result);
} }
@ -316,7 +316,7 @@ public class AdminController : ControllerBase
return BadRequest(new { message = "유효한 HTTP(S) Endpoint URL이 필요합니다." }); return BadRequest(new { message = "유효한 HTTP(S) Endpoint URL이 필요합니다." });
} }
var result = await _sttService.TestEndpointAsync(0, dto.ApiKey, endpointUri.AbsoluteUri); var result = await _sttService.TestEndpointAsync(0, dto.ApiKey, endpointUri.AbsoluteUri, HttpContext.RequestAborted);
return Ok(result); return Ok(result);
} }

View file

@ -32,7 +32,7 @@ public class LlmController : ControllerBase
return BadRequest(new { message = "Prompt는 필수 항목입니다." }); return BadRequest(new { message = "Prompt는 필수 항목입니다." });
} }
var result = await _llmService.GenerateAsync(userId, userEmail, request); var result = await _llmService.GenerateAsync(userId, userEmail, request, HttpContext.RequestAborted);
return Ok(result); return Ok(result);
} }
@ -43,7 +43,7 @@ public class LlmController : ControllerBase
var userEmail = User.FindFirstValue(ClaimTypes.Email) ?? "unknown@user"; var userEmail = User.FindFirstValue(ClaimTypes.Email) ?? "unknown@user";
int userId = int.TryParse(userIdStr, out var id) ? id : 0; int userId = int.TryParse(userIdStr, out var id) ? id : 0;
var result = await _llmService.ChatAsync(userId, userEmail, request); var result = await _llmService.ChatAsync(userId, userEmail, request, HttpContext.RequestAborted);
return Ok(result); return Ok(result);
} }
} }

View file

@ -62,7 +62,7 @@ public class SttController : ControllerBase
return StatusCode(StatusCodes.Status415UnsupportedMediaType, new { error = "unsupported_media_type" }); return StatusCode(StatusCodes.Status415UnsupportedMediaType, new { error = "unsupported_media_type" });
} }
var form = await Request.ReadFormAsync(); var form = await Request.ReadFormAsync(HttpContext.RequestAborted);
var file = form.Files.GetFile("file") ?? form.Files.GetFile("audio"); var file = form.Files.GetFile("file") ?? form.Files.GetFile("audio");
if (file == null || file.Length == 0) if (file == null || file.Length == 0)
{ {
@ -70,7 +70,7 @@ public class SttController : ControllerBase
} }
await using var memoryStream = new MemoryStream(); await using var memoryStream = new MemoryStream();
await file.CopyToAsync(memoryStream); await file.CopyToAsync(memoryStream, HttpContext.RequestAborted);
var language = form["language"].ToString(); var language = form["language"].ToString();
var prompt = form["prompt"].ToString(); var prompt = form["prompt"].ToString();
var request = new SttTranscribeRequest( var request = new SttTranscribeRequest(
@ -89,9 +89,14 @@ public class SttController : ControllerBase
memoryStream.ToArray(), memoryStream.ToArray(),
file.ContentType ?? "application/octet-stream", file.ContentType ?? "application/octet-stream",
file.FileName ?? "recording.bin", file.FileName ?? "recording.bin",
recordUsage: false); recordUsage: false,
cancellationToken: HttpContext.RequestAborted);
return Ok(result); return Ok(result);
} }
catch (OperationCanceledException) when (HttpContext.RequestAborted.IsCancellationRequested)
{
return StatusCode(499);
}
catch (SttProviderUnavailableException) catch (SttProviderUnavailableException)
{ {
return StatusCode(StatusCodes.Status503ServiceUnavailable, new { error = "stt_provider_unavailable" }); return StatusCode(StatusCodes.Status503ServiceUnavailable, new { error = "stt_provider_unavailable" });
@ -112,20 +117,4 @@ public class SttController : ControllerBase
var suppliedDigest = SHA256.HashData(Encoding.UTF8.GetBytes(supplied)); var suppliedDigest = SHA256.HashData(Encoding.UTF8.GetBytes(supplied));
return CryptographicOperations.FixedTimeEquals(configuredDigest, suppliedDigest); return CryptographicOperations.FixedTimeEquals(configuredDigest, suppliedDigest);
} }
[HttpGet("providers")]
[Authorize(Policy = "ManagerOrAbove")]
public async Task<IActionResult> GetActiveProviders()
{
var endpoints = await _sttService.GetAllEndpointsAsync();
return Ok(endpoints);
}
[HttpPost("test")]
[Authorize(Policy = "ManagerOrAbove")]
public async Task<IActionResult> TestConnection([FromQuery] int endpointId = 0)
{
var result = await _sttService.TestEndpointAsync(endpointId);
return Ok(result);
}
} }

View file

@ -4,6 +4,7 @@ using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using D3ROVoice.Api.Data; using D3ROVoice.Api.Data;
using D3ROVoice.Api.Dtos; using D3ROVoice.Api.Dtos;
@ -14,8 +15,8 @@ namespace D3ROVoice.Api.Services;
public interface ILlmProxyService public interface ILlmProxyService
{ {
Task<LlmGenerateResponse> GenerateAsync(int userId, string userEmail, LlmGenerateRequest request); Task<LlmGenerateResponse> GenerateAsync(int userId, string userEmail, LlmGenerateRequest request, CancellationToken cancellationToken = default);
Task<LlmGenerateResponse> ChatAsync(int userId, string userEmail, LlmChatRequest request); Task<LlmGenerateResponse> ChatAsync(int userId, string userEmail, LlmChatRequest request, CancellationToken cancellationToken = default);
} }
public class LlmProxyService : ILlmProxyService public class LlmProxyService : ILlmProxyService
@ -31,15 +32,16 @@ public class LlmProxyService : ILlmProxyService
_logger = logger; _logger = logger;
} }
public async Task<LlmGenerateResponse> GenerateAsync(int userId, string userEmail, LlmGenerateRequest request) public async Task<LlmGenerateResponse> GenerateAsync(int userId, string userEmail, LlmGenerateRequest request, CancellationToken cancellationToken = default)
{ {
cancellationToken.ThrowIfCancellationRequested();
var modelId = string.IsNullOrWhiteSpace(request.Model) ? "d3ro-gpt4o-mini" : request.Model; var modelId = string.IsNullOrWhiteSpace(request.Model) ? "d3ro-gpt4o-mini" : request.Model;
var endpoint = await _db.ModelEndpoints.FirstOrDefaultAsync(m => m.ModelId == modelId && m.IsActive); var endpoint = await _db.ModelEndpoints.FirstOrDefaultAsync(m => m.ModelId == modelId && m.IsActive, cancellationToken);
if (endpoint == null) if (endpoint == null)
{ {
// fallback: first active model endpoint or create default mockup endpoint // fallback: first active model endpoint or create default mockup endpoint
endpoint = await _db.ModelEndpoints.FirstOrDefaultAsync(m => m.IsActive); endpoint = await _db.ModelEndpoints.FirstOrDefaultAsync(m => m.IsActive, cancellationToken);
if (endpoint == null) if (endpoint == null)
{ {
endpoint = new ServiceModelEndpoint endpoint = new ServiceModelEndpoint
@ -71,7 +73,7 @@ public class LlmProxyService : ILlmProxyService
{ {
try try
{ {
var client = _httpClientFactory.CreateClient(); using var client = _httpClientFactory.CreateClient();
client.Timeout = TimeSpan.FromSeconds(60); client.Timeout = TimeSpan.FromSeconds(60);
var payload = new var payload = new
@ -86,7 +88,7 @@ public class LlmProxyService : ILlmProxyService
max_tokens = request.MaxTokens max_tokens = request.MaxTokens
}; };
var httpRequest = new HttpRequestMessage(HttpMethod.Post, endpoint.EndpointUrl) using var httpRequest = new HttpRequestMessage(HttpMethod.Post, endpoint.EndpointUrl)
{ {
Content = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json") Content = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json")
}; };
@ -96,12 +98,12 @@ public class LlmProxyService : ILlmProxyService
httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", endpoint.ApiKey); httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", endpoint.ApiKey);
} }
var httpResponse = await client.SendAsync(httpRequest); using var httpResponse = await client.SendAsync(httpRequest, cancellationToken);
sw.Stop(); sw.Stop();
if (httpResponse.IsSuccessStatusCode) if (httpResponse.IsSuccessStatusCode)
{ {
var jsonStr = await httpResponse.Content.ReadAsStringAsync(); var jsonStr = await httpResponse.Content.ReadAsStringAsync(cancellationToken);
using var doc = JsonDocument.Parse(jsonStr); using var doc = JsonDocument.Parse(jsonStr);
var root = doc.RootElement; var root = doc.RootElement;
@ -124,6 +126,10 @@ public class LlmProxyService : ILlmProxyService
completionTokens = Math.Max(15, responseText.Length / 4); completionTokens = Math.Max(15, responseText.Length / 4);
} }
} }
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception ex) catch (Exception ex)
{ {
sw.Stop(); sw.Stop();
@ -165,7 +171,7 @@ public class LlmProxyService : ILlmProxyService
}; };
_db.UsageLogs.Add(usageLog); _db.UsageLogs.Add(usageLog);
await _db.SaveChangesAsync(); await _db.SaveChangesAsync(cancellationToken);
return new LlmGenerateResponse( return new LlmGenerateResponse(
responseText, responseText,
@ -177,9 +183,9 @@ public class LlmProxyService : ILlmProxyService
); );
} }
public async Task<LlmGenerateResponse> ChatAsync(int userId, string userEmail, LlmChatRequest request) public async Task<LlmGenerateResponse> ChatAsync(int userId, string userEmail, LlmChatRequest request, CancellationToken cancellationToken = default)
{ {
var lastMsg = request.Messages.Count > 0 ? request.Messages[^1].Content : ""; var lastMsg = request.Messages.Count > 0 ? request.Messages[^1].Content : "";
return await GenerateAsync(userId, userEmail, new LlmGenerateRequest(lastMsg, request.Model, null, request.Temperature, request.MaxTokens)); return await GenerateAsync(userId, userEmail, new LlmGenerateRequest(lastMsg, request.Model, null, request.Temperature, request.MaxTokens), cancellationToken);
} }
} }

View file

@ -7,6 +7,7 @@ using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using D3ROVoice.Api.Data; using D3ROVoice.Api.Data;
using D3ROVoice.Api.Dtos; using D3ROVoice.Api.Dtos;
@ -24,10 +25,15 @@ public interface ISttProxyService
byte[]? audioBytes = null, byte[]? audioBytes = null,
string? contentType = null, string? contentType = null,
string? fileName = null, string? fileName = null,
bool recordUsage = true bool recordUsage = true,
CancellationToken cancellationToken = default
); );
Task<SttTestResultDto> TestEndpointAsync(int endpointId, string? testApiKey = null, string? testEndpointUrl = null); Task<SttTestResultDto> TestEndpointAsync(
int endpointId,
string? testApiKey = null,
string? testEndpointUrl = null,
CancellationToken cancellationToken = default);
Task<List<SttProviderEndpointDto>> GetAllEndpointsAsync(); Task<List<SttProviderEndpointDto>> GetAllEndpointsAsync();
Task<SttProviderEndpointDto> CreateEndpointAsync(CreateSttEndpointDto dto); Task<SttProviderEndpointDto> CreateEndpointAsync(CreateSttEndpointDto dto);
Task<SttProviderEndpointDto> UpdateEndpointAsync(int id, UpdateSttEndpointDto dto); Task<SttProviderEndpointDto> UpdateEndpointAsync(int id, UpdateSttEndpointDto dto);
@ -49,6 +55,9 @@ public sealed class SttProviderUnavailableException : Exception
public class SttProxyService : ISttProxyService public class SttProxyService : ISttProxyService
{ {
private const int MaxAudioBytes = 26 * 1024 * 1024;
private const int MaxBase64Length = ((MaxAudioBytes + 2) / 3) * 4;
private readonly AppDbContext _db; private readonly AppDbContext _db;
private readonly IHttpClientFactory _httpClientFactory; private readonly IHttpClientFactory _httpClientFactory;
private readonly ILogger<SttProxyService> _logger; private readonly ILogger<SttProxyService> _logger;
@ -67,32 +76,25 @@ public class SttProxyService : ISttProxyService
byte[]? audioBytes = null, byte[]? audioBytes = null,
string? contentType = null, string? contentType = null,
string? fileName = null, string? fileName = null,
bool recordUsage = true bool recordUsage = true,
CancellationToken cancellationToken = default
) )
{ {
cancellationToken.ThrowIfCancellationRequested();
// 1. Resolve Audio Bytes // 1. Resolve Audio Bytes
byte[] finalAudioBytes; byte[] finalAudioBytes;
if (audioBytes != null && audioBytes.Length > 0) if (audioBytes != null && audioBytes.Length > 0)
{ {
if (audioBytes.Length > MaxAudioBytes)
{
throw new ArgumentException("Audio payload exceeds maximum allowed size.");
}
finalAudioBytes = audioBytes; finalAudioBytes = audioBytes;
} }
else if (!string.IsNullOrWhiteSpace(request.AudioBase64)) else if (!string.IsNullOrWhiteSpace(request.AudioBase64))
{ {
try finalAudioBytes = DecodeAudioBase64(request.AudioBase64);
{
var cleanBase64 = request.AudioBase64;
var commaIdx = cleanBase64.IndexOf(',');
if (commaIdx >= 0)
{
cleanBase64 = cleanBase64.Substring(commaIdx + 1);
}
finalAudioBytes = Convert.FromBase64String(cleanBase64);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to decode base64 audio data");
throw new ArgumentException("Invalid AudioBase64 data format.", ex);
}
} }
else else
{ {
@ -104,7 +106,7 @@ public class SttProxyService : ISttProxyService
var durationSeconds = EstimateAudioDuration(finalAudioBytes, effectiveContentType); var durationSeconds = EstimateAudioDuration(finalAudioBytes, effectiveContentType);
// 2. Resolve Candidate Endpoints (Primary + Fallbacks) // 2. Resolve Candidate Endpoints (Primary + Fallbacks)
var candidates = await GetCandidateEndpointsAsync(request.Provider, request.ModelId); var candidates = await GetCandidateEndpointsAsync(request.Provider, request.ModelId, cancellationToken);
if (candidates.Count == 0) if (candidates.Count == 0)
{ {
throw new SttProviderUnavailableException(providerAttempted: false); throw new SttProviderUnavailableException(providerAttempted: false);
@ -117,6 +119,7 @@ public class SttProxyService : ISttProxyService
// 3. Failover Execution Chain // 3. Failover Execution Chain
foreach (var endpoint in candidates) foreach (var endpoint in candidates)
{ {
cancellationToken.ThrowIfCancellationRequested();
var epSw = Stopwatch.StartNew(); var epSw = Stopwatch.StartNew();
try try
{ {
@ -136,7 +139,8 @@ public class SttProxyService : ISttProxyService
finalAudioBytes, finalAudioBytes,
effectiveContentType, effectiveContentType,
effectiveFileName, effectiveFileName,
request request,
cancellationToken
); );
epSw.Stop(); epSw.Stop();
@ -167,7 +171,11 @@ public class SttProxyService : ISttProxyService
CreatedAt = DateTime.UtcNow CreatedAt = DateTime.UtcNow
}; };
_db.SttUsageLogs.Add(usageLog); _db.SttUsageLogs.Add(usageLog);
await _db.SaveChangesAsync(); await _db.SaveChangesAsync(cancellationToken);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
} }
catch (Exception dbEx) catch (Exception dbEx)
{ {
@ -186,6 +194,10 @@ public class SttProxyService : ISttProxyService
Cost: cost Cost: cost
); );
} }
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception ex) catch (Exception ex)
{ {
epSw.Stop(); epSw.Stop();
@ -205,7 +217,11 @@ public class SttProxyService : ISttProxyService
Endpoint = null, Endpoint = null,
CreatedAt = DateTime.UtcNow CreatedAt = DateTime.UtcNow
}); });
await _db.SaveChangesAsync(); await _db.SaveChangesAsync(cancellationToken);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
} }
catch catch
{ {
@ -220,15 +236,67 @@ public class SttProxyService : ISttProxyService
throw new SttProviderUnavailableException(providerAttempted); throw new SttProviderUnavailableException(providerAttempted);
} }
private byte[] DecodeAudioBase64(string audioBase64)
{
var prefixScanLength = Math.Min(audioBase64.Length, 128);
var commaIdx = audioBase64.IndexOf(',', 0, prefixScanLength);
var payloadStart = commaIdx >= 0 ? commaIdx + 1 : 0;
var payloadLength = audioBase64.Length - payloadStart;
if (payloadLength > MaxBase64Length)
{
throw new ArgumentException("AudioBase64 exceeds maximum allowed length.");
}
try
{
var cleanBase64 = (payloadStart > 0 ? audioBase64.Substring(payloadStart) : audioBase64).Trim();
if (cleanBase64.Length == 0 || cleanBase64.Length > MaxBase64Length)
{
throw new ArgumentException("Invalid AudioBase64 payload length.");
}
var decoded = Convert.FromBase64String(cleanBase64);
if (decoded.Length == 0 || decoded.Length > MaxAudioBytes)
{
throw new ArgumentException("Invalid decoded AudioBase64 payload size.");
}
return decoded;
}
catch (ArgumentException)
{
throw;
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to decode base64 audio data");
throw new ArgumentException("Invalid AudioBase64 data format.", ex);
}
}
private static async Task<string> SendForStringAsync(
HttpClient client,
HttpRequestMessage httpRequest,
CancellationToken cancellationToken)
{
using var response = await client.SendAsync(httpRequest, cancellationToken);
var responseBody = await response.Content.ReadAsStringAsync(cancellationToken);
if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException($"STT upstream returned HTTP {response.StatusCode}.");
}
return responseBody;
}
private async Task<(string Transcript, double Confidence, string? Language, double Duration)> ExecuteProviderTranscriptionAsync( private async Task<(string Transcript, double Confidence, string? Language, double Duration)> ExecuteProviderTranscriptionAsync(
SttProviderEndpoint endpoint, SttProviderEndpoint endpoint,
byte[] audioBytes, byte[] audioBytes,
string contentType, string contentType,
string fileName, string fileName,
SttTranscribeRequest request SttTranscribeRequest request,
CancellationToken cancellationToken
) )
{ {
var client = _httpClientFactory.CreateClient(); using var client = _httpClientFactory.CreateClient();
client.Timeout = TimeSpan.FromSeconds(90); client.Timeout = TimeSpan.FromSeconds(90);
var providerType = endpoint.ProviderType.ToLowerInvariant(); var providerType = endpoint.ProviderType.ToLowerInvariant();
@ -245,31 +313,31 @@ public class SttProxyService : ISttProxyService
case "openai": case "openai":
case "custom": case "custom":
{ {
return await CallOpenAiCompatibleSttAsync(client, endpoint, audioBytes, contentType, fileName, request); return await CallOpenAiCompatibleSttAsync(client, endpoint, audioBytes, contentType, fileName, request, cancellationToken);
} }
case "deepgram": case "deepgram":
{ {
return await CallDeepgramSttAsync(client, endpoint, audioBytes, contentType, request); return await CallDeepgramSttAsync(client, endpoint, audioBytes, contentType, request, cancellationToken);
} }
case "google": case "google":
{ {
return await CallGoogleSttAsync(client, endpoint, audioBytes, contentType, request); return await CallGoogleSttAsync(client, endpoint, audioBytes, contentType, request, cancellationToken);
} }
case "assemblyai": case "assemblyai":
{ {
return await CallAssemblyAiSttAsync(client, endpoint, audioBytes, contentType, request); return await CallAssemblyAiSttAsync(client, endpoint, audioBytes, contentType, request, cancellationToken);
} }
case "azure": case "azure":
{ {
return await CallAzureSttAsync(client, endpoint, audioBytes, contentType, request); return await CallAzureSttAsync(client, endpoint, audioBytes, contentType, request, cancellationToken);
} }
case "local-sidecar": case "local-sidecar":
{ {
return await CallLocalSidecarSttAsync(client, endpoint, audioBytes, contentType, fileName, request); return await CallLocalSidecarSttAsync(client, endpoint, audioBytes, contentType, fileName, request, cancellationToken);
} }
default: default:
{ {
return await CallOpenAiCompatibleSttAsync(client, endpoint, audioBytes, contentType, fileName, request); return await CallOpenAiCompatibleSttAsync(client, endpoint, audioBytes, contentType, fileName, request, cancellationToken);
} }
} }
} }
@ -280,7 +348,8 @@ public class SttProxyService : ISttProxyService
byte[] audioBytes, byte[] audioBytes,
string contentType, string contentType,
string fileName, string fileName,
SttTranscribeRequest request SttTranscribeRequest request,
CancellationToken cancellationToken
) )
{ {
using var form = new MultipartFormDataContent(); using var form = new MultipartFormDataContent();
@ -307,7 +376,7 @@ public class SttProxyService : ISttProxyService
form.Add(new StringContent(temp.ToString("0.0")), "temperature"); form.Add(new StringContent(temp.ToString("0.0")), "temperature");
form.Add(new StringContent("verbose_json"), "response_format"); form.Add(new StringContent("verbose_json"), "response_format");
var httpRequest = new HttpRequestMessage(HttpMethod.Post, endpoint.EndpointUrl) using var httpRequest = new HttpRequestMessage(HttpMethod.Post, endpoint.EndpointUrl)
{ {
Content = form Content = form
}; };
@ -319,13 +388,7 @@ public class SttProxyService : ISttProxyService
ApplyExtraHeaders(httpRequest, endpoint.ExtraHeadersJson); ApplyExtraHeaders(httpRequest, endpoint.ExtraHeadersJson);
var httpResponse = await client.SendAsync(httpRequest); var responseBody = await SendForStringAsync(client, httpRequest, cancellationToken);
var responseBody = await httpResponse.Content.ReadAsStringAsync();
if (!httpResponse.IsSuccessStatusCode)
{
throw new HttpRequestException($"STT upstream returned HTTP {httpResponse.StatusCode}.");
}
using var doc = JsonDocument.Parse(responseBody); using var doc = JsonDocument.Parse(responseBody);
var root = doc.RootElement; var root = doc.RootElement;
@ -356,7 +419,8 @@ public class SttProxyService : ISttProxyService
SttProviderEndpoint endpoint, SttProviderEndpoint endpoint,
byte[] audioBytes, byte[] audioBytes,
string contentType, string contentType,
SttTranscribeRequest request SttTranscribeRequest request,
CancellationToken cancellationToken
) )
{ {
var lang = request.Language ?? endpoint.Language ?? "ko"; var lang = request.Language ?? endpoint.Language ?? "ko";
@ -366,7 +430,7 @@ public class SttProxyService : ISttProxyService
? $"{baseUrl}&model={model}&language={lang}&smart_format=true&punctuate=true" ? $"{baseUrl}&model={model}&language={lang}&smart_format=true&punctuate=true"
: $"{baseUrl}?model={model}&language={lang}&smart_format=true&punctuate=true"; : $"{baseUrl}?model={model}&language={lang}&smart_format=true&punctuate=true";
var httpRequest = new HttpRequestMessage(HttpMethod.Post, url) using var httpRequest = new HttpRequestMessage(HttpMethod.Post, url)
{ {
Content = new ByteArrayContent(audioBytes) Content = new ByteArrayContent(audioBytes)
}; };
@ -379,13 +443,7 @@ public class SttProxyService : ISttProxyService
ApplyExtraHeaders(httpRequest, endpoint.ExtraHeadersJson); ApplyExtraHeaders(httpRequest, endpoint.ExtraHeadersJson);
var response = await client.SendAsync(httpRequest); var responseBody = await SendForStringAsync(client, httpRequest, cancellationToken);
var responseBody = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException($"STT upstream returned HTTP {response.StatusCode}.");
}
using var doc = JsonDocument.Parse(responseBody); using var doc = JsonDocument.Parse(responseBody);
var root = doc.RootElement; var root = doc.RootElement;
@ -421,7 +479,8 @@ public class SttProxyService : ISttProxyService
SttProviderEndpoint endpoint, SttProviderEndpoint endpoint,
byte[] audioBytes, byte[] audioBytes,
string contentType, string contentType,
SttTranscribeRequest request SttTranscribeRequest request,
CancellationToken cancellationToken
) )
{ {
var base64Audio = Convert.ToBase64String(audioBytes); var base64Audio = Convert.ToBase64String(audioBytes);
@ -457,19 +516,13 @@ public class SttProxyService : ISttProxyService
} }
}; };
var httpRequest = new HttpRequestMessage(HttpMethod.Post, endpoint.EndpointUrl) using var httpRequest = new HttpRequestMessage(HttpMethod.Post, endpoint.EndpointUrl)
{ {
Content = new StringContent(JsonSerializer.Serialize(geminiPayload), Encoding.UTF8, "application/json") Content = new StringContent(JsonSerializer.Serialize(geminiPayload), Encoding.UTF8, "application/json")
}; };
httpRequest.Headers.Add("X-Goog-Api-Key", apiKey); httpRequest.Headers.Add("X-Goog-Api-Key", apiKey);
var response = await client.SendAsync(httpRequest); var responseBody = await SendForStringAsync(client, httpRequest, cancellationToken);
var responseBody = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException($"STT upstream returned HTTP {response.StatusCode}.");
}
using var doc = JsonDocument.Parse(responseBody); using var doc = JsonDocument.Parse(responseBody);
var root = doc.RootElement; var root = doc.RootElement;
@ -507,19 +560,13 @@ public class SttProxyService : ISttProxyService
} }
}; };
var httpRequest = new HttpRequestMessage(HttpMethod.Post, endpoint.EndpointUrl) using var httpRequest = new HttpRequestMessage(HttpMethod.Post, endpoint.EndpointUrl)
{ {
Content = new StringContent(JsonSerializer.Serialize(gcpPayload), Encoding.UTF8, "application/json") Content = new StringContent(JsonSerializer.Serialize(gcpPayload), Encoding.UTF8, "application/json")
}; };
httpRequest.Headers.Add("X-Goog-Api-Key", apiKey); httpRequest.Headers.Add("X-Goog-Api-Key", apiKey);
var response = await client.SendAsync(httpRequest); var responseBody = await SendForStringAsync(client, httpRequest, cancellationToken);
var responseBody = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException($"STT upstream returned HTTP {response.StatusCode}.");
}
using var doc = JsonDocument.Parse(responseBody); using var doc = JsonDocument.Parse(responseBody);
var root = doc.RootElement; var root = doc.RootElement;
@ -555,22 +602,18 @@ public class SttProxyService : ISttProxyService
SttProviderEndpoint endpoint, SttProviderEndpoint endpoint,
byte[] audioBytes, byte[] audioBytes,
string contentType, string contentType,
SttTranscribeRequest request SttTranscribeRequest request,
CancellationToken cancellationToken
) )
{ {
// 1. Upload audio // 1. Upload audio
var uploadRequest = new HttpRequestMessage(HttpMethod.Post, "https://api.assemblyai.com/v2/upload") using var uploadRequest = new HttpRequestMessage(HttpMethod.Post, "https://api.assemblyai.com/v2/upload")
{ {
Content = new ByteArrayContent(audioBytes) Content = new ByteArrayContent(audioBytes)
}; };
uploadRequest.Headers.Add("Authorization", endpoint.ApiKey); uploadRequest.Headers.Add("Authorization", endpoint.ApiKey);
var uploadResp = await client.SendAsync(uploadRequest); var uploadBody = await SendForStringAsync(client, uploadRequest, cancellationToken);
var uploadBody = await uploadResp.Content.ReadAsStringAsync();
if (!uploadResp.IsSuccessStatusCode)
{
throw new HttpRequestException($"STT upstream upload returned HTTP {uploadResp.StatusCode}.");
}
using var uploadDoc = JsonDocument.Parse(uploadBody); using var uploadDoc = JsonDocument.Parse(uploadBody);
var uploadUrl = uploadDoc.RootElement.GetProperty("upload_url").GetString()!; var uploadUrl = uploadDoc.RootElement.GetProperty("upload_url").GetString()!;
@ -585,18 +628,13 @@ public class SttProxyService : ISttProxyService
format_text = true format_text = true
}; };
var transRequest = new HttpRequestMessage(HttpMethod.Post, "https://api.assemblyai.com/v2/transcript") using var transRequest = new HttpRequestMessage(HttpMethod.Post, "https://api.assemblyai.com/v2/transcript")
{ {
Content = new StringContent(JsonSerializer.Serialize(transcriptPayload), Encoding.UTF8, "application/json") Content = new StringContent(JsonSerializer.Serialize(transcriptPayload), Encoding.UTF8, "application/json")
}; };
transRequest.Headers.Add("Authorization", endpoint.ApiKey); transRequest.Headers.Add("Authorization", endpoint.ApiKey);
var transResp = await client.SendAsync(transRequest); var transBody = await SendForStringAsync(client, transRequest, cancellationToken);
var transBody = await transResp.Content.ReadAsStringAsync();
if (!transResp.IsSuccessStatusCode)
{
throw new HttpRequestException($"STT upstream job returned HTTP {transResp.StatusCode}.");
}
using var transDoc = JsonDocument.Parse(transBody); using var transDoc = JsonDocument.Parse(transBody);
var id = transDoc.RootElement.GetProperty("id").GetString()!; var id = transDoc.RootElement.GetProperty("id").GetString()!;
@ -604,17 +642,11 @@ public class SttProxyService : ISttProxyService
// 3. Poll for result (up to 20 seconds) // 3. Poll for result (up to 20 seconds)
for (int i = 0; i < 20; i++) for (int i = 0; i < 20; i++)
{ {
await Task.Delay(1000); await Task.Delay(1000, cancellationToken);
var pollReq = new HttpRequestMessage(HttpMethod.Get, $"https://api.assemblyai.com/v2/transcript/{id}"); using var pollReq = new HttpRequestMessage(HttpMethod.Get, $"https://api.assemblyai.com/v2/transcript/{id}");
pollReq.Headers.Add("Authorization", endpoint.ApiKey); pollReq.Headers.Add("Authorization", endpoint.ApiKey);
var pollResp = await client.SendAsync(pollReq); var pollBody = await SendForStringAsync(client, pollReq, cancellationToken);
var pollBody = await pollResp.Content.ReadAsStringAsync();
if (!pollResp.IsSuccessStatusCode)
{
throw new HttpRequestException($"STT upstream poll returned HTTP {pollResp.StatusCode}.");
}
using var pollDoc = JsonDocument.Parse(pollBody); using var pollDoc = JsonDocument.Parse(pollBody);
var status = pollDoc.RootElement.GetProperty("status").GetString(); var status = pollDoc.RootElement.GetProperty("status").GetString();
@ -647,7 +679,8 @@ public class SttProxyService : ISttProxyService
SttProviderEndpoint endpoint, SttProviderEndpoint endpoint,
byte[] audioBytes, byte[] audioBytes,
string contentType, string contentType,
SttTranscribeRequest request SttTranscribeRequest request,
CancellationToken cancellationToken
) )
{ {
var lang = request.Language ?? endpoint.Language ?? "ko-KR"; var lang = request.Language ?? endpoint.Language ?? "ko-KR";
@ -657,7 +690,7 @@ public class SttProxyService : ISttProxyService
? $"{endpoint.EndpointUrl}&language={lang}&format=detailed" ? $"{endpoint.EndpointUrl}&language={lang}&format=detailed"
: $"{endpoint.EndpointUrl}?language={lang}&format=detailed"; : $"{endpoint.EndpointUrl}?language={lang}&format=detailed";
var httpRequest = new HttpRequestMessage(HttpMethod.Post, url) using var httpRequest = new HttpRequestMessage(HttpMethod.Post, url)
{ {
Content = new ByteArrayContent(audioBytes) Content = new ByteArrayContent(audioBytes)
}; };
@ -668,13 +701,7 @@ public class SttProxyService : ISttProxyService
httpRequest.Headers.Add("Ocp-Apim-Subscription-Key", endpoint.ApiKey); httpRequest.Headers.Add("Ocp-Apim-Subscription-Key", endpoint.ApiKey);
} }
var response = await client.SendAsync(httpRequest); var responseBody = await SendForStringAsync(client, httpRequest, cancellationToken);
var responseBody = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException($"STT upstream returned HTTP {response.StatusCode}.");
}
using var doc = JsonDocument.Parse(responseBody); using var doc = JsonDocument.Parse(responseBody);
var root = doc.RootElement; var root = doc.RootElement;
@ -709,7 +736,8 @@ public class SttProxyService : ISttProxyService
byte[] audioBytes, byte[] audioBytes,
string contentType, string contentType,
string fileName, string fileName,
SttTranscribeRequest request SttTranscribeRequest request,
CancellationToken cancellationToken
) )
{ {
using var form = new MultipartFormDataContent(); using var form = new MultipartFormDataContent();
@ -720,7 +748,7 @@ public class SttProxyService : ISttProxyService
form.Add(new StringContent(endpoint.ModelId), "model"); form.Add(new StringContent(endpoint.ModelId), "model");
form.Add(new StringContent(request.Language ?? endpoint.Language ?? "ko"), "language"); form.Add(new StringContent(request.Language ?? endpoint.Language ?? "ko"), "language");
var httpRequest = new HttpRequestMessage(HttpMethod.Post, endpoint.EndpointUrl) using var httpRequest = new HttpRequestMessage(HttpMethod.Post, endpoint.EndpointUrl)
{ {
Content = form Content = form
}; };
@ -730,13 +758,7 @@ public class SttProxyService : ISttProxyService
httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", endpoint.ApiKey); httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", endpoint.ApiKey);
} }
var response = await client.SendAsync(httpRequest); var responseBody = await SendForStringAsync(client, httpRequest, cancellationToken);
var responseBody = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException($"STT upstream returned HTTP {response.StatusCode}.");
}
using var doc = JsonDocument.Parse(responseBody); using var doc = JsonDocument.Parse(responseBody);
var root = doc.RootElement; var root = doc.RootElement;
@ -752,12 +774,16 @@ public class SttProxyService : ISttProxyService
return (text, 0.99, endpoint.Language, duration); return (text, 0.99, endpoint.Language, duration);
} }
public async Task<SttTestResultDto> TestEndpointAsync(int endpointId, string? testApiKey = null, string? testEndpointUrl = null) public async Task<SttTestResultDto> TestEndpointAsync(
int endpointId,
string? testApiKey = null,
string? testEndpointUrl = null,
CancellationToken cancellationToken = default)
{ {
SttProviderEndpoint? endpoint = null; SttProviderEndpoint? endpoint = null;
if (endpointId > 0) if (endpointId > 0)
{ {
endpoint = await _db.SttProviderEndpoints.FindAsync(endpointId); endpoint = await _db.SttProviderEndpoints.FindAsync(new object[] { endpointId }, cancellationToken);
} }
if (endpoint == null) if (endpoint == null)
@ -792,7 +818,8 @@ public class SttProxyService : ISttProxyService
syntheticWav, syntheticWav,
"audio/wav", "audio/wav",
"test_audio.wav", "test_audio.wav",
new SttTranscribeRequest(Language: endpoint.Language ?? "ko") new SttTranscribeRequest(Language: endpoint.Language ?? "ko"),
cancellationToken
); );
sw.Stop(); sw.Stop();
@ -805,6 +832,10 @@ public class SttProxyService : ISttProxyService
ModelId: endpoint.ModelId ModelId: endpoint.ModelId
); );
} }
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception ex) catch (Exception ex)
{ {
sw.Stop(); sw.Stop();
@ -968,16 +999,19 @@ public class SttProxyService : ISttProxyService
); );
} }
private async Task<List<SttProviderEndpoint>> GetCandidateEndpointsAsync(string? requestedProvider, string? requestedModel) private async Task<List<SttProviderEndpoint>> GetCandidateEndpointsAsync(
string? requestedProvider,
string? requestedModel,
CancellationToken cancellationToken)
{ {
var query = _db.SttProviderEndpoints.Where(e => e.IsActive); var query = _db.SttProviderEndpoints.Where(e => e.IsActive);
if (!string.IsNullOrWhiteSpace(requestedProvider)) if (!string.IsNullOrWhiteSpace(requestedProvider))
{ {
var match = await query.FirstOrDefaultAsync(e => e.ProviderType.ToLower() == requestedProvider.ToLower()); var match = await query.FirstOrDefaultAsync(e => e.ProviderType.ToLower() == requestedProvider.ToLower(), cancellationToken);
if (match != null) if (match != null)
{ {
var others = await query.Where(e => e.Id != match.Id).OrderBy(e => e.FallbackPriority).ToListAsync(); var others = await query.Where(e => e.Id != match.Id).OrderBy(e => e.FallbackPriority).ToListAsync(cancellationToken);
others.Insert(0, match); others.Insert(0, match);
return others; return others;
} }
@ -985,10 +1019,10 @@ public class SttProxyService : ISttProxyService
if (!string.IsNullOrWhiteSpace(requestedModel)) if (!string.IsNullOrWhiteSpace(requestedModel))
{ {
var match = await query.FirstOrDefaultAsync(e => e.ModelId.ToLower() == requestedModel.ToLower()); var match = await query.FirstOrDefaultAsync(e => e.ModelId.ToLower() == requestedModel.ToLower(), cancellationToken);
if (match != null) if (match != null)
{ {
var others = await query.Where(e => e.Id != match.Id).OrderBy(e => e.FallbackPriority).ToListAsync(); var others = await query.Where(e => e.Id != match.Id).OrderBy(e => e.FallbackPriority).ToListAsync(cancellationToken);
others.Insert(0, match); others.Insert(0, match);
return others; return others;
} }
@ -998,7 +1032,7 @@ public class SttProxyService : ISttProxyService
return await query return await query
.OrderByDescending(e => e.IsDefault) .OrderByDescending(e => e.IsDefault)
.ThenBy(e => e.FallbackPriority) .ThenBy(e => e.FallbackPriority)
.ToListAsync(); .ToListAsync(cancellationToken);
} }
private static bool HasRequiredProviderConfiguration(SttProviderEndpoint endpoint) private static bool HasRequiredProviderConfiguration(SttProviderEndpoint endpoint)

View file

@ -22,8 +22,6 @@ files:
- "!out/**/*.map" - "!out/**/*.map"
# ffmpeg 정적 바이너리(61MB)는 설치본에 넣지 않는다 — 필요할 때 런타임으로 내려받는다 # ffmpeg 정적 바이너리(61MB)는 설치본에 넣지 않는다 — 필요할 때 런타임으로 내려받는다
- "!node_modules/@ffmpeg-installer/**" - "!node_modules/@ffmpeg-installer/**"
# ffmpeg 정적 바이너리(61MB)는 설치본에 넣지 않는다 — 필요할 때 런타임으로 내려받는다
- "!node_modules/@ffmpeg-installer/**"
# ──────────────────────────────────────────────────────────────────── # ────────────────────────────────────────────────────────────────────
# 자동 업데이트 feed — 이 설정이 있어야 electron-builder가 # 자동 업데이트 feed — 이 설정이 있어야 electron-builder가
@ -41,12 +39,11 @@ publish:
# electron-updater(기본 채널 latest)가 latest.yml을 찾으므로 항상 latest 채널로 고정. # electron-updater(기본 채널 latest)가 latest.yml을 찾으므로 항상 latest 채널로 고정.
detectUpdateChannel: false detectUpdateChannel: false
# native 모듈은 asar 외부에 배치 # native 모듈은 asar 외부에 배치 (Windows 는 koffi SendInput 으로 단일화되어 @nut-tree-fork 불필요)
asarUnpack: asarUnpack:
- "node_modules/better-sqlite3/**" - "node_modules/better-sqlite3/**"
- "node_modules/uiohook-napi/**" - "node_modules/uiohook-napi/**"
- "node_modules/@nut-tree-fork/**" # koffi — 포그라운드 창 제목/프로세스 조회 및 SendInput(User32 FFI). prebuilt 바이너리 포함.
# koffi — 포그라운드 창 제목/프로세스 조회(User32 FFI). prebuilt 바이너리 포함.
- "node_modules/koffi/**" - "node_modules/koffi/**"
- "node_modules/@koromix/**" - "node_modules/@koromix/**"
@ -68,6 +65,9 @@ win:
# macOS # macOS
# ──────────────────────────────────────────────────────────────────── # ────────────────────────────────────────────────────────────────────
mac: mac:
# macOS 키 입력 시뮬레이션(⌘+V / ⌘+C)은 @nut-tree-fork 네이티브 어댑터를 사용한다.
asarUnpack:
- "node_modules/@nut-tree-fork/**"
# icns는 electron-builder가 build/icon.png(1024)에서 자동 생성 # icns는 electron-builder가 build/icon.png(1024)에서 자동 생성
icon: build/icon.png icon: build/icon.png
category: public.app-category.productivity category: public.app-category.productivity
@ -120,11 +120,17 @@ extraResources:
filter: filter:
- "*.wav" - "*.wav"
# SoX 바이너리 # SoX 바이너리 (평면 파일만 허용 — wget.exe/PDF/문서 등 불필요 파일 유입 차단.
# 주의: filter에 "**/*"를 쓰면 하위 디렉토리가 통째로 누락된다(실측). # 주의: "**/*" 패턴은 하위 디렉토리 누락 버그가 있으므로 평면 글롭만 사용한다)
# 디렉토리를 그대로 복사할 때는 filter를 지정하지 않는다.
- from: resources/sox/ - from: resources/sox/
to: sox/ to: sox/
filter:
- "sox.exe"
- "zlib1.dll"
- "LICENSE.GPL.txt"
- "sox"
- "*.dylib"
- "*.so*"
# 주의: 로컬 AI 런타임(사이드카 엔진, ffmpeg)은 여기에 넣지 않는다. # 주의: 로컬 AI 런타임(사이드카 엔진, ffmpeg)은 여기에 넣지 않는다.
# 포함하면 설치본이 Cloudflare 업로드 한도(100MiB)를 넘어 자동 업데이트 메타데이터를 # 포함하면 설치본이 Cloudflare 업로드 한도(100MiB)를 넘어 자동 업데이트 메타데이터를

View file

@ -1,13 +1,29 @@
import { createPublicKey } from 'crypto' import { createPublicKey } from 'crypto'
import { readFileSync } from 'fs' import { copyFileSync, mkdirSync, readdirSync, readFileSync } from 'fs'
import { resolve } from 'path' import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin } from 'electron-vite' import { defineConfig, externalizeDepsPlugin } from 'electron-vite'
import react from '@vitejs/plugin-react' import react from '@vitejs/plugin-react'
const sharedAlias = { const sharedAlias = [
'@d3ro/core': resolve(__dirname, '../../packages/core/src'), { find: /^@d3ro\/i18n\/main$/, replacement: resolve(__dirname, '../../packages/i18n/src/main.ts') },
'@d3ro/ui': resolve(__dirname, '../../packages/ui/src'), { find: /^@d3ro\/i18n$/, replacement: resolve(__dirname, '../../packages/i18n/src/index.tsx') },
'@d3ro/i18n': resolve(__dirname, '../../packages/i18n/src/index.tsx') { find: /^@d3ro\/core(\/.*)?$/, replacement: `${resolve(__dirname, '../../packages/core/src')}$1` },
{ find: /^@d3ro\/ui(\/.*)?$/, replacement: `${resolve(__dirname, '../../packages/ui/src')}$1` },
]
const copyMainLocalesPlugin = {
name: 'copy-main-locales',
writeBundle(options: { dir?: string }): void {
const outDir = options.dir ?? resolve(__dirname, 'out/main')
const srcDir = resolve(__dirname, '../../packages/i18n/src/locales')
const destDir = resolve(outDir, 'locales')
mkdirSync(destDir, { recursive: true })
for (const file of readdirSync(srcDir)) {
if (file.endsWith('.json')) {
copyFileSync(resolve(srcDir, file), resolve(destDir, file))
}
}
},
} }
const workspaceExclude = ['@d3ro/core', '@d3ro/ui', '@d3ro/i18n'] const workspaceExclude = ['@d3ro/core', '@d3ro/ui', '@d3ro/i18n']
@ -27,7 +43,8 @@ export default defineConfig({
plugins: [ plugins: [
externalizeDepsPlugin({ externalizeDepsPlugin({
exclude: ['electron-store', ...workspaceExclude] exclude: ['electron-store', ...workspaceExclude]
}) }),
copyMainLocalesPlugin
], ],
resolve: { alias: sharedAlias } resolve: { alias: sharedAlias }
}, },

File diff suppressed because it is too large Load diff

View file

@ -1,197 +0,0 @@
SoX: Sound eXchange
===================
SoX (Sound eXchange) is the Swiss Army knife of sound processing tools: it
can convert sound files between many different file formats & audio devices,
and can apply many sound effects & transformations, as well as doing basic
analysis and providing input to more capable analysis and plotting tools.
SoX is licensed under the GNU GPL and GNU LGPL. To be precise, the 'sox'
and 'soxi' programs are distributed under the GPL, while the library
'libsox' (in which most of SoX's functionality resides) is dual-licensed.
Note that some optional components of libsox are GPL only: if you use these,
you must use libsox under the GPL. See INSTALL for the list of optional
components and their licences.
If this distribution is of source code (as opposed to pre-built binaries),
then you will need to compile and install SoX as described in the 'INSTALL'
file.
Changes between this release and previous releases of SoX can be found in
the 'ChangeLog' file; a summary of the file formats and effects supported in
this release can be found below. Detailed documentation for using SoX can
be found in the distributed 'man' pages:
o sox(1)
o soxi(1)
o soxformat(7)
o libsox(3)
or in plain text or PDF files for those systems without man.
The majority of SoX features and fixes are contributed by SoX users - thank
you very much for making SoX a success! There are several new features
wanted for SoX, listed on the feature request tracker at the SoX project
home-page:
http://sourceforge.net/projects/sox
users are encouraged to implement them!
Please submit bug reports, new feature requests, and patches to the relevant
tracker at the above address, or by email:
mailto:sox-devel@lists.sourceforge.net
Also accessible via the project home-page is the SoX users' discussion
mailing list which you can join to discuss all matters SoX with other SoX
users; the mail address for this list is:
mailto:sox-users@lists.sourceforge.net
The current release handles the following audio file formats:
o Raw files in various binary formats
o Raw textual data
o Amiga 8svx files
o Apple/SGI AIFF files
o SUN .au files
o PCM, u-law, A-law
o G7xx ADPCM files (read only)
o mutant DEC .au files
o NeXT .snd files
o AVR files
o CDDA (Compact Disc Digital Audio format)
o CVS and VMS files (continuous variable slope)
o Grandstream ring-tone files
o GSM files
o HTK files
o LPC-10 files
o Macintosh HCOM files
o Amiga MAUD files
o AMR-WB & AMR-NB (with optional libamrwb & libamrnb libraries)
o MP2/MP3 (with optional libmad, libtwolame and libmp3lame libraries)
o MP4, AAC, AC3, WAVPACK, AMR-NB files (with optional ffmpeg library)
o AVI, WMV, Ogg Theora, MPEG video files (with optional ffmpeg library)
o Ogg Vorbis files (with optional Ogg Vorbis libraries)
o FLAC files (with optional libFLAC)
o IRCAM SoundFile files
o NIST SPHERE files
o Turtle beach SampleVision files
o Sounder & Soundtool (DOS) files
o Yamaha TX-16W sampler files
o SoundBlaster .VOC files
o Dialogic/OKI ADPCM files (.VOX)
o Microsoft .WAV files
o PCM, floating point
o u-law, A-law, MS ADPCM, IMA (DMI) ADPCM
o GSM
o RIFX (big endian)
o WavPack files (with optional libwavpack library)
o Psion (palmtop) A-law WVE files and Record voice notes
o Maxis XA Audio files
o EA ADPCM (read support only, for now)
o Pseudo formats that allow direct playing/recording from most audio devices
o The "null" pseudo-file that reads and writes from/to nowhere
The audio effects/tools included in this release are as follows:
o Tone/filter effects
o allpass: RBJ all-pass biquad IIR filter
o bandpass: RBJ band-pass biquad IIR filter
o bandreject: RBJ band-reject biquad IIR filter
o band: SPKit resonator band-pass IIR filter
o bass: Tone control: RBJ shelving biquad IIR filter
o equalizer: RBJ peaking equalisation biquad IIR filter
o firfit+: FFT convolution FIR filter using given freq. response (W.I.P.)
o highpass: High-pass filter: Single pole or RBJ biquad IIR
o hilbert: Hilbert transform filter (90 degrees phase shift)
o lowpass: Low-pass filter: single pole or RBJ biquad IIR
o sinc: Sinc-windowed low/high-pass/band-pass/reject FIR
o treble: Tone control: RBJ shelving biquad IIR filter
o Production effects
o chorus: Make a single instrument sound like many
o delay: Delay one or more channels
o echo: Add an echo
o echos: Add a sequence of echos
o flanger: Stereo flanger
o overdrive: Non-linear distortion
o phaser: Phase shifter
o repeat: Loop the audio a number of times
o reverb: Add reverberation
o reverse: Reverse the audio (to search for Satanic messages ;-)
o tremolo: Sinusoidal volume modulation
o Volume/level effects
o compand: Signal level compression/expansion/limiting
o contrast: Phase contrast volume enhancement
o dcshift: Apply or remove DC offset
o fade: Apply a fade-in and/or fade-out to the audio
o gain: Apply gain or attenuation; normalise/equalise/balance/headroom
o loudness: Gain control with ISO 226 loudness compensation
o mcompand: Multi-band compression/expansion/limiting
o norm: Normalise to 0dB (or other)
o vol: Adjust audio volume
o Editing effects
o pad: Pad (usually) the ends of the audio with silence
o silence: Remove portions of silence from the audio
o splice: Perform the equivalent of a cross-faded tape splice
o trim: Cuts portions out of the audio
o vad: Voice activity detector
o Mixing effects
o channels: Auto mix or duplicate to change number of channels
o divide+: Divide sample values by those in the 1st channel (W.I.P.)
o remix: Produce arbitrarily mixed output channels
o swap: Swap stereo channels
o Pitch/tempo effects
o bend: Bend pitch at given times without changing tempo
o pitch: Adjust pitch (= key) without changing tempo
o speed: Adjust pitch & tempo together
o stretch: Adjust tempo without changing pitch (simple alg.)
o tempo: Adjust tempo without changing pitch (WSOLA alg.)
o Mastering effects
o dither: Add dither noise to increase quantisation SNR
o rate: Change audio sampling rate
o Specialised filters/mixers
o deemph: ISO 908 CD de-emphasis (shelving) IIR filter
o earwax: Process CD audio to best effect for headphone use
o noisered: Filter out noise from the audio
o oops: Out Of Phase Stereo (or `Karaoke') effect
o riaa: RIAA vinyl playback equalisation
o Analysis `effects'
o noiseprof: Produce a DFT profile of the audio (use with noisered)
o spectrogram: graph signal level vs. frequency & time (needs `libpng')
o stat: Enumerate audio peak & RMS levels, approx. freq., etc.
o stats: Multichannel aware `stat'
o Miscellaneous effects
o ladspa: Apply LADSPA plug-in effects e.g. CMT (Computer Music Toolkit)
o synth: Synthesise/modulate audio tones or noise signals
o newfile: Create a new output file when an effects chain ends.
o restart: Restart 1st effects chain when multiple chains exist.
o Low-level signal processing effects
o biquad: 2nd-order IIR filter using externally provided coefficients
o downsample: Reduce sample rate by discarding samples
o fir: FFT convolution FIR filter using externally provided coefficients
o upsample: Increase sample rate by zero stuffing
+ Experimental or incomplete effect; may change in future.
Multiple audio files can be combined (and then further processed with
effects) using any one of the following combiner methods:
o concatenate
o mix
o merge: E.g. two mono files to one stereo file
o sequence: For playing multiple audio files/streams

View file

@ -1,167 +0,0 @@
SoX
---
This file contains information specific to the Win32 version of SoX.
Please refer to the README file for general information.
The binary SOX.EXE can be installed anywhere you desire. The only
restriction is that the included ZLIB1..DLL and LIBGOMP-1.DLL must be
located in the same directory as SOX.EXE or somewhere within your PATH.
SoX Helper Applications
-----------------------
SoX also includes support for SOXI.EXE, PLAY.EXE and REC.EXE and their
behaviors are documented in included PDF's. They have the same
install restrictions as SOX.EXE.
SOXI.EXE, PLAY.EXE, and REC.EXE are not distributed with this package to
reduce size requirements. They are, in fact, only copies of the original
SOX.EXE binary which changes SOX.EXE's behavior based on the
executable's filename.
If you wish to make use of these utils then you can create them
yourself.
copy sox.exe soxi.exe
copy sox.exe play.exe
copy sox.exe rec.exe
If you are concerned with disk space, the play and record
functionality can be equated using the "-d" option with SOX.EXE. soxi
functionality can be equated using the "--info" option with SOX.EXE. The
rough syntax is:
play: sox [input files and options] -d [effects]
rec: sox -d [output file and options] [effects]
soxi: sox --info [input files and options]
Acknowledgements
----------------
SOX.EXE included in this package makes use of the following projects.
See the cygbuild script included with the source code package for
further information on how it was compiled and packaged.
SoX - http://sox.sourceforge.net
FLAC - http://flac.sourceforge.net
LADSPA - http://www.ladspa.org
libid3tag - http://www.underbit.com/products/mad
libsndfile - http://www.mega-nerd.com/libsndfile
Ogg Vorbis - http://www.vorbis.com
PNG - http://www.libpng.org/pub/png
WavPack - http://www.wavpack.com
wget - http://www.gnu.org/software/wget
Enjoy,
The SoX Development Team
Appendix - wget Support
-----------------------
SoX can make use of the wget command line utility to load files over
the internet. A binary copy of wget has been included with this
package of SoX for your convience.
For SoX to make use of wget, it must be located either in your PATH or
within the same directory that SoX is ran from.
Custom configuration of wget can be made by editing the file wget.ini
contained in the same directory as wget.exe.
Please consult wget's homepage for access to source code as well as
further instructions on configuring.
http://www.gnu.org/software/wget
Appendix - MP3 Support
----------------------
SoX contains support for reading and writing MP3 files but does not ship
with the DLL's that perform decoding and encoding of MP3 data because
of patent restrictions. For further details, refer to:
http://en.wikipedia.org/wiki/MP3#Licensing_and_patent_issues
MP3 support can be enabled by placing Lame encoding DLL and/or
MAD decoding DLL into the same directory as SOX.EXE. These
can be compiled yourself, they may turn up on searches of the internet
or may be included with other MP3 applications already installed
on your system. For encoding/writing, try searching for lame-enc.dll,
libmp3lame-0.dll, libmp3lame.dll, or cygmp3lame-0.dll. For
decoding/reading, try searching for libmad-0.dll, libmad.dll or cygmad-0.dll.
Instructions are included here for using MSYS to create the DLL's.
It is assumed you already have MSYS installed on your system
with a working gcc compiler. The commands are ran from MSYS
bash shell.
Obtain the latest Lame and MAD source code from approprate locations.
Lame MP3 encoder http://lame.sourceforge.net
MAD MP3 decoder http://www.underbit.com/products/mad
cd lame-398-2
./configure --disabled-static --enable-shared
make
cp libmp3lame/.libs/libmp3lame-0.dll /path/to/sox
MAD libraries up to 0.15.1b have a bug in configure that will not allow
building DLL under mingw. This can be resolved by adding LDFLAGS
to configure and editing the generated Makefile to remove an invalid
option.
cd libmad-0.15.1b
./configure --enable-shared --disable-static LDFLAGS="-no-undefined"
[edit Makefile, search for "-fforce-mem" and delete it.]
make
cp libmad-0.dll /path/to/sox/
Appendix - AMR-NB/AMR-WB Support
--------------------------------
SoX contains support for reading and writing AMR-NB and AMR-WB files but
does not ship with the DLL's that perform decoding and encoding of AMR
data because of patent restrictions.
AMR-NB/AMR-WB support can be enabled by placing required DLL's
into the same directory as SOX.EXE. These can be compiled yourself,
they may turn up on searches of the internet or may be included with other
MP3 applications already installed on your system. For AMR-NB support,
try searching for libamrnb-3.dll, libopencore-amrnb-0.dll, or
libopencore-amrnb.dll. For AMR-WB support, try searching for libamrwb-3.dll,
libopencore-amrwb-0.dll, or libopencore-amrwb.dll.
Instructions are included here for using MSYS to create the DLL's.
It is assumed you already have MSYS installed on your system with
working gcc compiler. These commands are ran from MSYS bash shell.
Obtain the latest amrnb and amrwb source code from
http://sourceforge.net/projects/opencore-amr .
cd opencore-amr-0.1.2
./configure --enable-shared --disable-static LDFLAGS="-no-undefined"
make
cp amrnb/.libs/libopencore-amrnb-0.dll /path/to/sox
cp amrwb/.libs/libopencore-amrwb-0.dll /path/to/sox
Appendix - LADSPA Plugins
-------------------------
SoX has built in support for LADSPA Plugins. These plugins are
mostly built for Linux but some are available for Windows.
The Audacity GUI application has a page that points to a collection
of Windows LADSPA plugins.
http://audacity.sourceforge.net/download/plugins
SoX will search for these plugins based on LADSPA_PATH
enviornment variable. See sox.txt for further information.

View file

@ -1,14 +0,0 @@
rem Example of how to do batch processing with SoX on MS-Windows.
rem
rem Place this file in the same folder as sox.exe (& rename it as appropriate).
rem You can then drag and drop a selection of files onto the batch file (or
rem onto a `short-cut' to it).
rem
rem In this example, the converted files end up in a folder called `converted',
rem but this, of course, can be changed, as can the parameters to the sox
rem command.
cd %~dp0
mkdir converted
FOR %%A IN (%*) DO sox %%A "converted/%%~nxA" rate -v 44100
pause

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -1,112 +0,0 @@
###
### Sample Wget initialization file .wgetrc
###
## You can use this file to change the default behaviour of wget or to
## avoid having to type many many command-line options. This file does
## not contain a comprehensive list of commands -- look at the manual
## to find out what you can put into this file.
##
## Wget initialization file can reside in /usr/local/etc/wgetrc
## (global, for all users) or $HOME/.wgetrc (for a single user).
##
## To use the settings in this file, you will have to uncomment them,
## as well as change them, in most cases, as the values on the
## commented-out lines are the default values (e.g. "off").
##
## Global settings (useful for setting up in /usr/local/etc/wgetrc).
## Think well before you change them, since they may reduce wget's
## functionality, and make it behave contrary to the documentation:
##
# You can set retrieve quota for beginners by specifying a value
# optionally followed by 'K' (kilobytes) or 'M' (megabytes). The
# default quota is unlimited.
#quota = inf
# You can lower (or raise) the default number of retries when
# downloading a file (default is 20).
#tries = 20
# Lowering the maximum depth of the recursive retrieval is handy to
# prevent newbies from going too "deep" when they unwittingly start
# the recursive retrieval. The default is 5.
#reclevel = 5
# By default Wget uses "passive FTP" transfer where the client
# initiates the data connection to the server rather than the other
# way around. That is required on systems behind NAT where the client
# computer cannot be easily reached from the Internet. However, some
# firewalls software explicitly supports active FTP and in fact has
# problems supporting passive transfer. If you are in such
# environment, use "passive_ftp = off" to revert to active FTP.
#passive_ftp = off
# The "wait" command below makes Wget wait between every connection.
# If, instead, you want Wget to wait only between retries of failed
# downloads, set waitretry to maximum number of seconds to wait (Wget
# will use "linear backoff", waiting 1 second after the first failure
# on a file, 2 seconds after the second failure, etc. up to this max).
waitretry = 10
##
## Local settings (for a user to set in his $HOME/.wgetrc). It is
## *highly* undesirable to put these settings in the global file, since
## they are potentially dangerous to "normal" users.
##
## Even when setting up your own ~/.wgetrc, you should know what you
## are doing before doing so.
##
# Set this to on to use timestamping by default:
#timestamping = off
# It is a good idea to make Wget send your email address in a `From:'
# header with your request (so that server administrators can contact
# you in case of errors). Wget does *not* send `From:' by default.
#header = From: Your Name <username@site.domain>
# You can set up other headers, like Accept-Language. Accept-Language
# is *not* sent by default.
#header = Accept-Language: en
# You can set the default proxies for Wget to use for http and ftp.
# They will override the value in the environment.
#http_proxy = http://proxy.yoyodyne.com:18023/
#ftp_proxy = http://proxy.yoyodyne.com:18023/
# If you do not want to use proxy at all, set this to off.
#use_proxy = on
# You can customize the retrieval outlook. Valid options are default,
# binary, mega and micro.
#dot_style = default
# Setting this to off makes Wget not download /robots.txt. Be sure to
# know *exactly* what /robots.txt is and how it is used before changing
# the default!
#robots = on
# It can be useful to make Wget wait between connections. Set this to
# the number of seconds you want Wget to wait.
#wait = 0
# You can force creating directory structure, even if a single is being
# retrieved, by setting this to on.
#dirstruct = off
# You can turn on recursive retrieving by default (don't do this if
# you are not sure you know what it means) by setting this to on.
#recursive = off
# To always back up file X as X.orig before converting its links (due
# to -k / --convert-links / convert_links = on having been specified),
# set this variable to on:
#backup_converted = off
# To have Wget follow FTP links from HTML files by default, set this
# to on:
#follow_ftp = off

View file

@ -1,4 +1,4 @@
# scripts/download-sox.ps1 # scripts/download-sox.ps1
# SoX Windows 바이너리를 resources/sox/에 다운로드한다. # SoX Windows 바이너리를 resources/sox/에 다운로드한다.
# 실행: powershell -ExecutionPolicy Bypass -File scripts/download-sox.ps1 # 실행: powershell -ExecutionPolicy Bypass -File scripts/download-sox.ps1
@ -36,7 +36,7 @@ try {
Write-Host " https://sourceforge.net/projects/sox/files/sox/14.4.1/sox-14.4.1-win32.zip/download" Write-Host " https://sourceforge.net/projects/sox/files/sox/14.4.1/sox-14.4.1-win32.zip/download"
Write-Host "" Write-Host ""
Write-Host "2. 다운로드한 zip에서 아래 파일들을 복사:" -ForegroundColor White Write-Host "2. 다운로드한 zip에서 아래 파일들을 복사:" -ForegroundColor White
Write-Host " sox.exe, rec.exe, libmad-0.dll, libmp3lame-0.dll, libsox-3.dll" Write-Host " sox.exe, zlib1.dll, LICENSE.GPL.txt"
Write-Host "" Write-Host ""
Write-Host "3. 복사 위치:" -ForegroundColor White Write-Host "3. 복사 위치:" -ForegroundColor White
Write-Host " $DEST_DIR" Write-Host " $DEST_DIR"
@ -54,7 +54,7 @@ if ($fileSize -lt 100000) {
Write-Host " https://sourceforge.net/projects/sox/files/sox/14.4.1/sox-14.4.1-win32.zip/download" Write-Host " https://sourceforge.net/projects/sox/files/sox/14.4.1/sox-14.4.1-win32.zip/download"
Write-Host "" Write-Host ""
Write-Host "2. 다운로드한 zip 압축 해제 후 아래 파일들을 복사:" -ForegroundColor White Write-Host "2. 다운로드한 zip 압축 해제 후 아래 파일들을 복사:" -ForegroundColor White
Write-Host " sox.exe, rec.exe, libmad-0.dll, libmp3lame-0.dll, libsox-3.dll" Write-Host " sox.exe, zlib1.dll, LICENSE.GPL.txt"
Write-Host "" Write-Host ""
Write-Host "3. 복사 위치:" -ForegroundColor White Write-Host "3. 복사 위치:" -ForegroundColor White
Write-Host " $DEST_DIR" Write-Host " $DEST_DIR"
@ -82,7 +82,7 @@ if (-not $SOX_EXTRACTED) {
$SOX_EXTRACTED = Get-Item $TEMP_DIR $SOX_EXTRACTED = Get-Item $TEMP_DIR
} }
$filesToCopy = @("sox.exe", "rec.exe", "libmad-0.dll", "libmp3lame-0.dll", "libsox-3.dll") $filesToCopy = @("sox.exe", "zlib1.dll", "LICENSE.GPL.txt")
New-Item -ItemType Directory -Force -Path $DEST_DIR | Out-Null New-Item -ItemType Directory -Force -Path $DEST_DIR | Out-Null
$copied = 0 $copied = 0

View file

@ -29,9 +29,10 @@ import logging
import os import os
import signal import signal
import sys import sys
import tempfile
import threading import threading
import time import time
from contextlib import asynccontextmanager from contextlib import asynccontextmanager, suppress
from pathlib import Path from pathlib import Path
from typing import AsyncGenerator from typing import AsyncGenerator
@ -599,28 +600,40 @@ async def load_model(body: dict) -> JSONResponse: # noqa: ANN001
@app.post("/unload") @app.post("/unload")
async def unload_model(request: Request) -> JSONResponse: async def unload_model(request: Request) -> JSONResponse:
"""보조 자리 모델을 내려 VRAM/RAM 을 돌려준다. """보조 자리 또는 기본(primary) 자리 모델을 내려 VRAM/RAM 을 돌려준다.
자막·회의가 끝난 뒤에도 보조 모델(예: large-v3-turbo)이 받아쓰기 모델과 함께 상주하면 사이드카 HTTP 프로세스 자체는 UIA 브리지(/uia/focus)가 계속 쓰므로 살려 둔 채,
GPU 여유가 줄어 Ollama 모델 로드가 느려지거나 OOM 이 난다. 기본(primary) 모델은 받아쓰기 클라우드 STT 전환·장시간 유휴·자막 종료 시 Whisper CTranslate2 모델 메모리만 회수한다.
지연을 막기 위해 내리지 않는다.
Request body: { "slot": "aux" } Request body: { "slot": "aux" | "primary" | "all" }
""" """
global _model, _model_id
body = await request.json() body = await request.json()
slot: str = body.get("slot", "aux") slot: str = body.get("slot", "aux")
if slot != "aux": if slot not in ("aux", "primary", "all"):
return JSONResponse( return JSONResponse(
status_code=400, status_code=400,
content={"status": "error", "message": "only the aux slot can be unloaded"}, content={"status": "error", "message": "slot must be aux, primary, or all"},
) )
released: list[str] = []
async with _inference_lock: async with _inference_lock:
released = list(_aux_models) if slot in ("primary", "all") and _model is not None:
_aux_models.clear() if _model_id:
released.append(_model_id)
_model_devices.pop(_model_id, None)
_model = None
_model_id = None
if slot in ("aux", "all") and _aux_models:
for aux_id in list(_aux_models):
released.append(aux_id)
_model_devices.pop(aux_id, None)
_aux_models.clear()
if released: if released:
gc.collect() gc.collect()
logger.info("보조 모델 언로드: %s", ", ".join(released)) logger.info("모델 언로드(%s): %s", slot, ", ".join(released))
return JSONResponse(content={"status": "unloaded", "models": released}) return JSONResponse(content={"status": "unloaded", "models": released})
@ -708,14 +721,21 @@ async def transcribe(
start_time = time.monotonic() start_time = time.monotonic()
try: try:
pcm_bytes = await audio.read() try:
pcm_bytes = await audio.read()
finally:
await audio.close()
if len(pcm_bytes) == 0: if len(pcm_bytes) < 2:
return JSONResponse( return JSONResponse(
status_code=400, status_code=400,
content={"status": "error", "message": "오디오 데이터가 비어있습니다"}, content={"status": "error", "message": "오디오 데이터가 비어있습니다"},
) )
if len(pcm_bytes) % 2 != 0:
logger.warning("홀수 바이트 PCM 입력(%dB) — 마지막 1바이트를 버립니다", len(pcm_bytes))
pcm_bytes = pcm_bytes[:-1]
audio_array = ( audio_array = (
np.frombuffer(pcm_bytes, dtype=np.int16).astype(np.float32) / 32768.0 np.frombuffer(pcm_bytes, dtype=np.int16).astype(np.float32) / 32768.0
) )
@ -756,6 +776,8 @@ async def transcribe(
finally: finally:
cancel.set() cancel.set()
watcher.cancel() watcher.cancel()
with suppress(asyncio.CancelledError, Exception):
await watcher
processing_time = int((time.monotonic() - start_time) * 1000) processing_time = int((time.monotonic() - start_time) * 1000)
@ -872,11 +894,134 @@ async def uia_focus(timeoutMs: int = 1500) -> JSONResponse:
return JSONResponse(content=snapshot) return JSONResponse(content=snapshot)
def _run_pyannote_diarization(
audio_bytes: bytes,
hf_token: str,
num_speakers: int | None,
) -> dict[str, object]:
"""선택 설치된 pyannote.audio 파이프라인으로 화자 구간을 추출한다."""
try:
from pyannote.audio import Pipeline # type: ignore[import-not-found]
except Exception as exc:
raise ModuleNotFoundError(str(exc)) from exc
fd, temp_path_str = tempfile.mkstemp(suffix=".wav", prefix="d3ro-diarize-")
temp_path = Path(temp_path_str)
try:
with os.fdopen(fd, "wb") as handle:
handle.write(audio_bytes)
try:
pipeline = Pipeline.from_pretrained(
"pyannote/speaker-diarization-3.1",
token=hf_token,
)
except TypeError:
pipeline = Pipeline.from_pretrained(
"pyannote/speaker-diarization-3.1",
use_auth_token=hf_token,
)
call_kwargs: dict[str, object] = {}
if num_speakers is not None and num_speakers > 0:
call_kwargs["num_speakers"] = num_speakers
annotation = pipeline(str(temp_path), **call_kwargs)
segments: list[dict[str, object]] = []
speakers: set[str] = set()
for turn, _track, speaker in annotation.itertracks(yield_label=True):
speaker_label = str(speaker)
speakers.add(speaker_label)
segments.append(
{
"speaker": speaker_label,
"start": round(float(turn.start), 3),
"end": round(float(turn.end), 3),
}
)
return {
"status": "ok",
"segments": segments,
"num_speakers": len(speakers),
}
finally:
try:
temp_path.unlink(missing_ok=True)
except OSError:
pass
@app.post("/diarize")
async def diarize(
audio: UploadFile = File(...),
hf_token: str = Form(""),
num_speakers: int | None = Form(None),
) -> JSONResponse:
"""pyannote.audio 기반 화자 분리 엔드포인트 (미설치 시 503 반환으로 LLM 폴백 유도)."""
token = hf_token.strip()
if not token:
return JSONResponse(
status_code=400,
content={
"status": "error",
"code": "missing_hf_token",
"message": "Hugging Face 토큰이 비어 있습니다",
},
)
try:
audio_bytes = await audio.read()
finally:
await audio.close()
if not audio_bytes:
return JSONResponse(
status_code=400,
content={
"status": "error",
"code": "empty_audio",
"message": "오디오 파일이 비어 있습니다",
},
)
async with _inference_lock:
try:
result = await asyncio.to_thread(
_run_pyannote_diarization,
audio_bytes,
token,
num_speakers,
)
return JSONResponse(content=result)
except ModuleNotFoundError:
return JSONResponse(
status_code=503,
content={
"status": "unavailable",
"code": "pyannote_not_installed",
"message": "pyannote.audio 패키지가 설치되어 있지 않습니다",
},
)
except Exception as exc:
logger.warning("pyannote 화자 분리 실패: %s", exc)
return JSONResponse(
status_code=500,
content={
"status": "error",
"code": "diarization_failed",
"message": f"화자 분리 실행 실패: {exc}",
},
)
@app.post("/shutdown") @app.post("/shutdown")
async def shutdown() -> JSONResponse: async def shutdown() -> JSONResponse:
"""서버를 graceful하게 종료한다.""" """서버를 graceful하게 종료한다."""
logger.info("종료 요청 수신") logger.info("종료 요청 수신")
if _download_thread is not None and _download_thread.is_alive():
_download_cancel.set()
await asyncio.to_thread(_download_thread.join, 2.0)
try: try:
import uia_bridge import uia_bridge

View file

@ -0,0 +1,115 @@
"""
사이드카 /diarize 엔드포인트 계약 테스트 (GAP-MTG-02).
"""
from __future__ import annotations
import sys
import types
import unittest
from pathlib import Path
from unittest import mock
SIDECAR_DIR = Path(__file__).resolve().parent.parent
if str(SIDECAR_DIR) not in sys.path:
sys.path.insert(0, str(SIDECAR_DIR))
from fastapi.testclient import TestClient # noqa: E402
import main # noqa: E402
class _FakeTurn:
def __init__(self, start: float, end: float) -> None:
self.start = start
self.end = end
class _FakeAnnotation:
def itertracks(self, yield_label: bool = False):
tracks = [
(_FakeTurn(0.2, 4.5), "A", "SPEAKER_00"),
(_FakeTurn(5.0, 9.8), "B", "SPEAKER_01"),
(_FakeTurn(10.1, 13.0), "C", "SPEAKER_00"),
]
if yield_label:
return tracks
return [(t[0], t[1]) for t in tracks]
class DiarizeEndpointTest(unittest.TestCase):
def setUp(self) -> None:
self.client = TestClient(main.app)
def test_diarize_rejects_empty_hf_token(self) -> None:
response = self.client.post(
"/diarize",
files={"audio": ("audio.wav", b"RIFF0000WAVEfmt ", "audio/wav")},
data={"hf_token": " "},
)
self.assertEqual(response.status_code, 400)
body = response.json()
self.assertEqual(body["code"], "missing_hf_token")
def test_diarize_returns_503_when_pyannote_not_installed(self) -> None:
with mock.patch.dict(sys.modules, {"pyannote": None, "pyannote.audio": None}):
response = self.client.post(
"/diarize",
files={"audio": ("audio.wav", b"RIFF0000WAVEfmt ", "audio/wav")},
data={"hf_token": "hf_test_token"},
)
self.assertEqual(response.status_code, 503)
body = response.json()
self.assertEqual(body["code"], "pyannote_not_installed")
def test_diarize_returns_segments_and_cleans_temp_file(self) -> None:
observed_paths: list[Path] = []
observed_kwargs: list[dict[str, object]] = []
class _FakePipeline:
@classmethod
def from_pretrained(cls, model_id: str, **kwargs: object) -> "_FakePipeline":
self_inst = cls()
self_inst.model_id = model_id
self_inst.init_kwargs = kwargs
return self_inst
def __call__(self, audio_path: str, **kwargs: object) -> _FakeAnnotation:
observed_paths.append(Path(audio_path))
observed_kwargs.append(dict(kwargs))
self.temp_existed_during_call = Path(audio_path).exists()
return _FakeAnnotation()
fake_pyannote = types.ModuleType("pyannote")
fake_pyannote_audio = types.ModuleType("pyannote.audio")
fake_pyannote_audio.Pipeline = _FakePipeline # type: ignore[attr-defined]
with mock.patch.dict(
sys.modules,
{"pyannote": fake_pyannote, "pyannote.audio": fake_pyannote_audio},
):
response = self.client.post(
"/diarize",
files={"audio": ("audio.wav", b"RIFF1234WAVEfmt ", "audio/wav")},
data={"hf_token": "hf_valid_token", "num_speakers": "2"},
)
self.assertEqual(response.status_code, 200)
body = response.json()
self.assertEqual(body["status"], "ok")
self.assertEqual(body["num_speakers"], 2)
self.assertEqual(
body["segments"],
[
{"speaker": "SPEAKER_00", "start": 0.2, "end": 4.5},
{"speaker": "SPEAKER_01", "start": 5.0, "end": 9.8},
{"speaker": "SPEAKER_00", "start": 10.1, "end": 13.0},
],
)
self.assertEqual(observed_kwargs, [{"num_speakers": 2}])
self.assertEqual(len(observed_paths), 1)
self.assertFalse(observed_paths[0].exists())
if __name__ == "__main__":
unittest.main()

View file

@ -0,0 +1,87 @@
"""Sidecar /transcribe & /shutdown hygiene regression tests."""
from __future__ import annotations
import sys
import threading
import unittest
from pathlib import Path
from unittest import mock
SIDECAR_DIR = Path(__file__).resolve().parent.parent
if str(SIDECAR_DIR) not in sys.path:
sys.path.insert(0, str(SIDECAR_DIR))
from fastapi.testclient import TestClient # noqa: E402
import main # noqa: E402
class _FakeModel:
pass
class TranscribeAndShutdownHygieneTest(unittest.TestCase):
def setUp(self) -> None:
self._orig_model = main._model
self._orig_model_id = main._model_id
self._orig_thread = main._download_thread
main._model = _FakeModel() # type: ignore[assignment]
main._model_id = "tiny"
self.client = TestClient(main.app)
def tearDown(self) -> None:
main._model = self._orig_model
main._model_id = self._orig_model_id
main._download_thread = self._orig_thread
main._download_cancel.clear()
def test_transcribe_single_byte_pcm_returns_400_without_frombuffer_crash(self) -> None:
res = self.client.post(
"/transcribe",
files={"audio": ("chunk.pcm", b"\x01", "application/octet-stream")},
)
self.assertEqual(res.status_code, 400)
self.assertEqual(res.json()["status"], "error")
def test_transcribe_odd_byte_pcm_truncates_trailing_byte_and_succeeds(self) -> None:
observed_samples: list[int] = []
def fake_transcribe(_model, audio_array, _kwargs, _resolved_id, _is_primary, _cancel):
observed_samples.append(len(audio_array))
return [{"text": "ok", "start": 0.0, "end": 0.1}], "ok", "ko"
with mock.patch.object(main, "_transcribe_blocking", side_effect=fake_transcribe):
res = self.client.post(
"/transcribe",
files={"audio": ("chunk.pcm", b"\x00\x01\x02\x03\x99", "application/octet-stream")},
)
self.assertEqual(res.status_code, 200)
self.assertEqual(observed_samples, [2])
self.assertEqual(res.json()["text"], "ok")
def test_shutdown_cancels_active_download_thread(self) -> None:
stopped = threading.Event()
def fake_worker() -> None:
main._download_cancel.wait(timeout=5.0)
stopped.set()
main._download_cancel.clear()
worker = threading.Thread(target=fake_worker, daemon=True)
main._download_thread = worker
worker.start()
with mock.patch.object(main, "_server", None):
res = self.client.post("/shutdown")
self.assertEqual(res.status_code, 200)
self.assertEqual(res.json()["status"], "shutting_down")
self.assertTrue(main._download_cancel.is_set())
self.assertTrue(stopped.is_set())
self.assertFalse(worker.is_alive())
if __name__ == "__main__":
unittest.main()

View file

@ -35,7 +35,6 @@ import { openStartupDatabase } from './db/startup-database'
import { import {
createMainWindow, createMainWindow,
getMainWindow, getMainWindow,
preloadPopupWindows,
showHistoryPopup, showHistoryPopup,
hideHistoryPopup, hideHistoryPopup,
sendKeyToHistoryPopup, sendKeyToHistoryPopup,
@ -44,8 +43,8 @@ import {
hideCommandPopup, hideCommandPopup,
sendKeyToCommandPopup, sendKeyToCommandPopup,
isCommandPopupVisible, isCommandPopupVisible,
getHistoryPopupWindow, peekHistoryPopupWindow,
getCommandPopupWindow, peekCommandPopupWindow,
reapplyThemeToAllPopups, reapplyThemeToAllPopups,
} from './windows/WindowManager' } from './windows/WindowManager'
import { createTray } from './windows/TrayManager' import { createTray } from './windows/TrayManager'
@ -57,6 +56,8 @@ import { setupLifecycle } from './lifecycle'
import { getShutdownRegistry } from './shutdown-registry' import { getShutdownRegistry } from './shutdown-registry'
import { shouldDismissOnEscape } from './suggestion-overlay-policy' import { shouldDismissOnEscape } from './suggestion-overlay-policy'
import { registerAllIpcHandlers } from './ipc' import { registerAllIpcHandlers } from './ipc'
import { disposeAudioHandlers } from './ipc/audio-handlers'
import { safeSendToRenderer } from './ipc/renderer-broadcast'
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
import type { IPCChannel } from '@d3ro/core/ipc-channels' import type { IPCChannel } from '@d3ro/core/ipc-channels'
@ -128,6 +129,7 @@ export async function bootstrap(): Promise<void> {
*/ */
function registerCoreShutdownSteps(): void { function registerCoreShutdownSteps(): void {
const registry = getShutdownRegistry() const registry = getShutdownRegistry()
registry.register({ name: 'audio-test', dispose: () => disposeAudioHandlers() })
registry.register({ name: 'local-llm', dispose: () => disposeLocalLLMService() }) registry.register({ name: 'local-llm', dispose: () => disposeLocalLLMService() })
registry.register({ name: 'local-stt', dispose: () => disposeLocalSTTServiceIfStarted() }) registry.register({ name: 'local-stt', dispose: () => disposeLocalSTTServiceIfStarted() })
} }
@ -169,7 +171,9 @@ async function initConfigEffects(): Promise<void> {
effects.register('theme', () => reapplyThemeToAllPopups()) effects.register('theme', () => reapplyThemeToAllPopups())
// 팝업 문서 lang 도 같은 재주입에서 맞춘다 // 팝업 문서 lang 도 같은 재주입에서 맞춘다
effects.register('language', () => reapplyThemeToAllPopups()) effects.register('language', () => reapplyThemeToAllPopups())
// 자동 한영·오타 교정이나 제안 설정이 바뀌면 입력 문맥 관찰기 실행 여부도 동기화한다 // 자동 한영·오타 교정이나 제안·입력 텔레메트리 설정이 바뀌면 관찰기 실행/마우스 훅 상태도 즉시 동기화한다
effects.register('inputTelemetryEnabled', () => getInputTelemetryService().applyConfig())
effects.register('inputTelemetryPaused', () => getInputTelemetryService().applyConfig())
effects.register('typoAutoCorrectEnabled', () => getInputTelemetryService().applyConfig()) effects.register('typoAutoCorrectEnabled', () => getInputTelemetryService().applyConfig())
effects.register('suggestionEnabled', () => getInputTelemetryService().applyConfig()) effects.register('suggestionEnabled', () => getInputTelemetryService().applyConfig())
effects.start() effects.start()
@ -208,10 +212,12 @@ async function initInputIntelligence(): Promise<void> {
const telemetry = getInputTelemetryService() const telemetry = getInputTelemetryService()
const suggestion = getSuggestionService() const suggestion = getSuggestionService()
// 첫 제안 수락·받아쓰기 삽입이 키 입력 모듈 로딩(수 초)을 기다리지 않게, 부팅이 끝난 뒤 미리 불러 둔다. // macOS 등 nut-js 를 쓰는 환경에서만 부팅 뒤 미리 불러 둔다 (Windows 는 koffi SendInput 즉시 로드).
setTimeout(() => { if (process.platform !== 'win32') {
void getTextInsertService().preload() setTimeout(() => {
}, 5_000).unref?.() void getTextInsertService().preload()
}, 5_000).unref?.()
}
// 오타 교정이 먼저 본다 — 고치는 중이면 텍스트가 곧 바뀌니 제안으로 넘기지 않고(교정 뒤 새 // 오타 교정이 먼저 본다 — 고치는 중이면 텍스트가 곧 바뀌니 제안으로 넘기지 않고(교정 뒤 새
// 스냅샷이 온다), 모델로 검사 중이면 끝날 때('settled') 넘긴다. 제안이 고친 문장을 기준으로 // 스냅샷이 온다), 모델로 검사 중이면 끝날 때('settled') 넘긴다. 제안이 고친 문장을 기준으로
@ -384,7 +390,8 @@ async function initAutoUpdate(): Promise<void> {
} }
async function initPopupWindows(): Promise<void> { async function initPopupWindows(): Promise<void> {
preloadPopupWindows() // 팝업 창(녹음 팁·결과·기록·명령)은 부팅 시 숨긴 Chromium 프로세스 4개를 선점하지 않고,
// 실제 핫키/이벤트가 발생해 표시할 때 처음 생성한 뒤 재사용한다 (~180-260MB 초기 메모리 절감).
// 시스템 테마를 따르는 설정이면 OS 다크/라이트 전환 때 팝업도 다시 칠한다(언어가 바뀌면 lang 도 함께) // 시스템 테마를 따르는 설정이면 OS 다크/라이트 전환 때 팝업도 다시 칠한다(언어가 바뀌면 lang 도 함께)
const onNativeThemeUpdated = (): void => { const onNativeThemeUpdated = (): void => {
@ -444,7 +451,10 @@ function createPopupNavigationController(): PopupNavigationController {
hide: () => hideHistoryPopup(), hide: () => hideHistoryPopup(),
isVisible: () => isHistoryPopupVisible(), isVisible: () => isHistoryPopupVisible(),
sendKey: (key) => sendKeyToHistoryPopup(key), sendKey: (key) => sendKeyToHistoryPopup(key),
containsPoint: (point) => windowContains(getHistoryPopupWindow(), point), containsPoint: (point) => {
const win = peekHistoryPopupWindow()
return win ? windowContains(win, point) : false
},
}, },
command: { command: {
show: () => { show: () => {
@ -455,7 +465,10 @@ function createPopupNavigationController(): PopupNavigationController {
hide: () => hideCommandPopup(), hide: () => hideCommandPopup(),
isVisible: () => isCommandPopupVisible(), isVisible: () => isCommandPopupVisible(),
sendKey: (key) => sendKeyToCommandPopup(key), sendKey: (key) => sendKeyToCommandPopup(key),
containsPoint: (point) => windowContains(getCommandPopupWindow(), point), containsPoint: (point) => {
const win = peekCommandPopupWindow()
return win ? windowContains(win, point) : false
},
}, },
}, },
shortcuts: { shortcuts: {
@ -551,10 +564,7 @@ async function initVoiceMode(): Promise<void> {
/** 메인 윈도우 렌더러에 UI 갱신 이벤트 전송 */ /** 메인 윈도우 렌더러에 UI 갱신 이벤트 전송 */
function notifyRenderer(channel: IPCChannel, data?: Record<string, unknown>): void { function notifyRenderer(channel: IPCChannel, data?: Record<string, unknown>): void {
const win = getMainWindow() safeSendToRenderer(channel, data ?? {})
if (win && !win.isDestroyed()) {
win.webContents.send(channel, data ?? {})
}
} }
async function initLLMPolling(): Promise<void> { async function initLLMPolling(): Promise<void> {
@ -568,6 +578,11 @@ async function initLLMPolling(): Promise<void> {
*/ */
async function initSTTWarmup(): Promise<void> { async function initSTTWarmup(): Promise<void> {
try { try {
const activeProvider = (configGet('sttProvider') as string | undefined) || 'local'
if (activeProvider !== 'local') {
logger.info(`[bootstrap] skipping local STT warmup (active provider: ${activeProvider})`)
return
}
const { getSTTManager } = await import('./services/stt/STTManager') const { getSTTManager } = await import('./services/stt/STTManager')
void getSTTManager().warmUpLocal() void getSTTManager().warmUpLocal()
} catch (err) { } catch (err) {

View file

@ -15,6 +15,7 @@ import * as schema from './schema'
import { getLogger } from '../services/LoggerService' import { getLogger } from '../services/LoggerService'
import { D3ROError, ErrorCode } from '@d3ro/core/errors' import { D3ROError, ErrorCode } from '@d3ro/core/errors'
import { emitAccountScopeChanged, LOCAL_SCOPE_ID } from '../services/account-scope' import { emitAccountScopeChanged, LOCAL_SCOPE_ID } from '../services/account-scope'
import { invalidateEmbeddedChunksCache } from '../services/rag/chunk-store'
const logger = getLogger('database') const logger = getLogger('database')
@ -88,8 +89,12 @@ function archiveLegacyDbIfExists(): void {
export function applySchema(s: Database.Database): void { export function applySchema(s: Database.Database): void {
s.pragma('journal_mode = WAL') s.pragma('journal_mode = WAL')
s.pragma('synchronous = NORMAL')
s.pragma('foreign_keys = ON') s.pragma('foreign_keys = ON')
s.pragma('busy_timeout = 5000') s.pragma('busy_timeout = 5000')
s.pragma('temp_store = MEMORY')
s.pragma('cache_size = -16000')
s.pragma('mmap_size = 67108864')
s.exec(` s.exec(`
CREATE TABLE IF NOT EXISTS history ( CREATE TABLE IF NOT EXISTS history (
@ -187,11 +192,12 @@ export function applySchema(s: Database.Database): void {
CREATE INDEX IF NOT EXISTS idx_rag_documents_added_at ON rag_documents(added_at); CREATE INDEX IF NOT EXISTS idx_rag_documents_added_at ON rag_documents(added_at);
CREATE TABLE IF NOT EXISTS rag_chunks ( CREATE TABLE IF NOT EXISTS rag_chunks (
id TEXT PRIMARY KEY, id TEXT PRIMARY KEY,
document_id TEXT NOT NULL, document_id TEXT NOT NULL,
content TEXT NOT NULL, content TEXT NOT NULL,
embedding TEXT NOT NULL, embedding TEXT NOT NULL,
chunk_index INTEGER NOT NULL embedding_blob BLOB,
chunk_index INTEGER NOT NULL
); );
CREATE INDEX IF NOT EXISTS idx_rag_chunks_document_id ON rag_chunks(document_id); CREATE INDEX IF NOT EXISTS idx_rag_chunks_document_id ON rag_chunks(document_id);
`) `)
@ -217,6 +223,7 @@ export function applySchema(s: Database.Database): void {
updated_at INTEGER NOT NULL updated_at INTEGER NOT NULL
); );
CREATE INDEX IF NOT EXISTS idx_meeting_sessions_created_at ON meeting_sessions(created_at DESC); CREATE INDEX IF NOT EXISTS idx_meeting_sessions_created_at ON meeting_sessions(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_meeting_sessions_started_at ON meeting_sessions(started_at DESC);
CREATE INDEX IF NOT EXISTS idx_meeting_sessions_status ON meeting_sessions(status); CREATE INDEX IF NOT EXISTS idx_meeting_sessions_status ON meeting_sessions(status);
CREATE TABLE IF NOT EXISTS meeting_memos ( CREATE TABLE IF NOT EXISTS meeting_memos (
@ -228,6 +235,7 @@ export function applySchema(s: Database.Database): void {
); );
CREATE INDEX IF NOT EXISTS idx_meeting_memos_session_id ON meeting_memos(session_id); CREATE INDEX IF NOT EXISTS idx_meeting_memos_session_id ON meeting_memos(session_id);
CREATE INDEX IF NOT EXISTS idx_meeting_memos_timestamp ON meeting_memos(timestamp_ms); CREATE INDEX IF NOT EXISTS idx_meeting_memos_timestamp ON meeting_memos(timestamp_ms);
CREATE INDEX IF NOT EXISTS idx_meeting_memos_created_at ON meeting_memos(created_at DESC);
`) `)
// Phase 14.5: meeting_documents 테이블 // Phase 14.5: meeting_documents 테이블
@ -383,6 +391,8 @@ export function applySchema(s: Database.Database): void {
// 기기 간 동기화: 모바일과 같은 필드(즐겨찾기, 메모 수정 시각) + 로컬 outbox/커서 // 기기 간 동기화: 모바일과 같은 필드(즐겨찾기, 메모 수정 시각) + 로컬 outbox/커서
addColumnIfMissing(s, 'history', 'is_favorite', 'INTEGER NOT NULL DEFAULT 0') addColumnIfMissing(s, 'history', 'is_favorite', 'INTEGER NOT NULL DEFAULT 0')
addColumnIfMissing(s, 'meeting_memos', 'updated_at', 'INTEGER') addColumnIfMissing(s, 'meeting_memos', 'updated_at', 'INTEGER')
addColumnIfMissing(s, 'meeting_memos', 'revision', 'INTEGER NOT NULL DEFAULT 1')
addColumnIfMissing(s, 'rag_chunks', 'embedding_blob', 'BLOB')
s.exec(` s.exec(`
CREATE TABLE IF NOT EXISTS sync_outbox ( CREATE TABLE IF NOT EXISTS sync_outbox (
entity TEXT NOT NULL, entity TEXT NOT NULL,
@ -403,6 +413,150 @@ export function applySchema(s: Database.Database): void {
updated_at INTEGER NOT NULL updated_at INTEGER NOT NULL
); );
`) `)
applySearchIndexSchema(s)
}
const FTS5_BACKFILL_KEY = 'fts5Backfill:v1'
/**
* 로컬 통합 검색용 FTS5 trigram 가상 테이블 + 동기화 트리거 + 기존 DB 1회 백필.
* 한글/영문/혼합 부분 문자열(3자 이상) 검색에서 전체 테이블 스캔을 제거한다.
*/
function applySearchIndexSchema(s: Database.Database): void {
try {
s.exec(`
CREATE VIRTUAL TABLE IF NOT EXISTS history_fts USING fts5(
row_id UNINDEXED,
search_text,
tokenize = 'trigram'
);
CREATE TRIGGER IF NOT EXISTS trg_history_fts_ai AFTER INSERT ON history BEGIN
INSERT INTO history_fts(row_id, search_text)
VALUES (
new.id,
coalesce(new.title, '') || char(10) ||
coalesce(new.original_text, '') || char(10) ||
coalesce(new.polished_text, '') || char(10) ||
coalesce(new.summary_text, '')
);
END;
CREATE TRIGGER IF NOT EXISTS trg_history_fts_ad AFTER DELETE ON history BEGIN
DELETE FROM history_fts WHERE row_id = old.id;
END;
CREATE TRIGGER IF NOT EXISTS trg_history_fts_au
AFTER UPDATE OF title, original_text, polished_text, summary_text ON history BEGIN
DELETE FROM history_fts WHERE row_id = old.id;
INSERT INTO history_fts(row_id, search_text)
VALUES (
new.id,
coalesce(new.title, '') || char(10) ||
coalesce(new.original_text, '') || char(10) ||
coalesce(new.polished_text, '') || char(10) ||
coalesce(new.summary_text, '')
);
END;
CREATE VIRTUAL TABLE IF NOT EXISTS meeting_sessions_fts USING fts5(
row_id UNINDEXED,
search_text,
tokenize = 'trigram'
);
CREATE TRIGGER IF NOT EXISTS trg_meeting_sessions_fts_ai AFTER INSERT ON meeting_sessions BEGIN
INSERT INTO meeting_sessions_fts(row_id, search_text)
VALUES (
new.id,
coalesce(new.title, '') || char(10) ||
coalesce(new.edited_transcript, '') || char(10) ||
coalesce(new.raw_transcript, '')
);
END;
CREATE TRIGGER IF NOT EXISTS trg_meeting_sessions_fts_ad AFTER DELETE ON meeting_sessions BEGIN
DELETE FROM meeting_sessions_fts WHERE row_id = old.id;
END;
CREATE TRIGGER IF NOT EXISTS trg_meeting_sessions_fts_au
AFTER UPDATE OF title, edited_transcript, raw_transcript ON meeting_sessions BEGIN
DELETE FROM meeting_sessions_fts WHERE row_id = old.id;
INSERT INTO meeting_sessions_fts(row_id, search_text)
VALUES (
new.id,
coalesce(new.title, '') || char(10) ||
coalesce(new.edited_transcript, '') || char(10) ||
coalesce(new.raw_transcript, '')
);
END;
CREATE VIRTUAL TABLE IF NOT EXISTS meeting_memos_fts USING fts5(
row_id UNINDEXED,
search_text,
tokenize = 'trigram'
);
CREATE TRIGGER IF NOT EXISTS trg_meeting_memos_fts_ai AFTER INSERT ON meeting_memos BEGIN
INSERT INTO meeting_memos_fts(row_id, search_text)
VALUES (new.id, coalesce(new.content, ''));
END;
CREATE TRIGGER IF NOT EXISTS trg_meeting_memos_fts_ad AFTER DELETE ON meeting_memos BEGIN
DELETE FROM meeting_memos_fts WHERE row_id = old.id;
END;
CREATE TRIGGER IF NOT EXISTS trg_meeting_memos_fts_au
AFTER UPDATE OF content ON meeting_memos BEGIN
DELETE FROM meeting_memos_fts WHERE row_id = old.id;
INSERT INTO meeting_memos_fts(row_id, search_text)
VALUES (new.id, coalesce(new.content, ''));
END;
`)
const backfilled = s
.prepare('SELECT value FROM sync_state WHERE key = ?')
.get(FTS5_BACKFILL_KEY) as { value: string } | undefined
if (!backfilled) {
const now = Date.now()
const backfill = s.transaction(() => {
s.exec(`
DELETE FROM history_fts;
INSERT INTO history_fts(row_id, search_text)
SELECT
id,
coalesce(title, '') || char(10) ||
coalesce(original_text, '') || char(10) ||
coalesce(polished_text, '') || char(10) ||
coalesce(summary_text, '')
FROM history;
DELETE FROM meeting_sessions_fts;
INSERT INTO meeting_sessions_fts(row_id, search_text)
SELECT
id,
coalesce(title, '') || char(10) ||
coalesce(edited_transcript, '') || char(10) ||
coalesce(raw_transcript, '')
FROM meeting_sessions;
DELETE FROM meeting_memos_fts;
INSERT INTO meeting_memos_fts(row_id, search_text)
SELECT id, coalesce(content, '')
FROM meeting_memos;
`)
s.prepare(
'INSERT OR REPLACE INTO sync_state(key, value, updated_at) VALUES (?, ?, ?)'
).run(FTS5_BACKFILL_KEY, String(now), now)
})
backfill()
}
} catch (err) {
logger.warn(
`FTS5 search index setup skipped: ${err instanceof Error ? err.message : String(err)}`
)
}
} }
function addColumnIfMissing( function addColumnIfMissing(
@ -508,7 +662,15 @@ export function openLocal(): OpenForUserResult {
* 현재 열려있는 DB 닫기. 아무 것도 안 열려 있으면 no-op. * 현재 열려있는 DB 닫기. 아무 것도 안 열려 있으면 no-op.
*/ */
export function closeCurrent(): void { export function closeCurrent(): void {
if (!sqlite) return const prevDb = db
if (!sqlite) {
if (prevDb) {
invalidateEmbeddedChunksCache(prevDb)
db = null
currentUserId = null
}
return
}
const prevUserId = currentUserId const prevUserId = currentUserId
try { try {
@ -518,6 +680,7 @@ export function closeCurrent(): void {
`Database close error: ${err instanceof Error ? err.message : String(err)}` `Database close error: ${err instanceof Error ? err.message : String(err)}`
) )
} }
if (prevDb) invalidateEmbeddedChunksCache(prevDb)
sqlite = null sqlite = null
db = null db = null
currentUserId = null currentUserId = null
@ -631,6 +794,9 @@ export function importLocalModeData(): LocalImportResult | null {
markDone() markDone()
}) })
copy() copy()
if (result.imported > 0 && db) {
invalidateEmbeddedChunksCache(db)
}
logger.info(`Imported ${result.imported} local-mode row(s) into ${userId}: ${JSON.stringify(result.tables)}`) logger.info(`Imported ${result.imported} local-mode row(s) into ${userId}: ${JSON.stringify(result.tables)}`)
return result return result
} finally { } finally {
@ -653,6 +819,8 @@ export function bindTestDatabase(
testDb: BetterSQLite3Database<typeof schema>, testDb: BetterSQLite3Database<typeof schema>,
userId: string = LOCAL_USER_ID, userId: string = LOCAL_USER_ID,
): void { ): void {
if (db) invalidateEmbeddedChunksCache(db)
invalidateEmbeddedChunksCache(testDb)
db = testDb db = testDb
currentUserId = userId currentUserId = userId
emitAccountScopeChanged(userId) emitAccountScopeChanged(userId)
@ -660,6 +828,7 @@ export function bindTestDatabase(
/** Vitest 전용: bindTestDatabase 해제. */ /** Vitest 전용: bindTestDatabase 해제. */
export function unbindTestDatabase(): void { export function unbindTestDatabase(): void {
if (db) invalidateEmbeddedChunksCache(db)
db = null db = null
currentUserId = null currentUserId = null
} }

View file

@ -5,13 +5,11 @@
// '100%' 는 '100' 이 들어간 모든 행과 일치했다. 메타문자를 이스케이프하고 ESCAPE 절을 붙인다. // '100%' 는 '100' 이 들어간 모든 행과 일치했다. 메타문자를 이스케이프하고 ESCAPE 절을 붙인다.
import { sql, type AnyColumn, type SQL } from 'drizzle-orm' import { sql, type AnyColumn, type SQL } from 'drizzle-orm'
import { escapeLikePattern } from '@d3ro/core/guards'
const LIKE_ESCAPE_CHAR = '\\' const LIKE_ESCAPE_CHAR = '\\'
/** LIKE 메타문자(%, _, 이스케이프 문자 자체)를 글자 그대로 찾도록 이스케이프한다. */ export { escapeLikePattern }
export function escapeLikePattern(text: string): string {
return text.replace(/[\\%_]/g, (char) => `${LIKE_ESCAPE_CHAR}${char}`)
}
export interface ContainsPattern { export interface ContainsPattern {
/** 바인딩될 LIKE 패턴 (`%…%`, 이스케이프됨) */ /** 바인딩될 LIKE 패턴 (`%…%`, 이스케이프됨) */

View file

@ -1,7 +1,7 @@
// src/main/db/schema.ts // src/main/db/schema.ts
// 설계서 03의 drizzle-orm 스키마 정의 // 설계서 03의 drizzle-orm 스키마 정의
import { sqliteTable, text, integer, real, index, uniqueIndex, primaryKey } from 'drizzle-orm/sqlite-core' import { sqliteTable, text, integer, real, blob, index, uniqueIndex, primaryKey } from 'drizzle-orm/sqlite-core'
// ── history ────────────────────────────────────────────── // ── history ──────────────────────────────────────────────
export const history = sqliteTable( export const history = sqliteTable(
@ -141,8 +141,10 @@ export const ragChunks = sqliteTable(
id: text('id').primaryKey(), id: text('id').primaryKey(),
documentId: text('document_id').notNull(), documentId: text('document_id').notNull(),
content: text('content').notNull(), content: text('content').notNull(),
/** 임베딩 벡터 (JSON 직렬화 float[]) */ /** 임베딩 벡터 (JSON 직렬화 float[] — 호환/동기화용) */
embedding: text('embedding').notNull(), embedding: text('embedding').notNull(),
/** 임베딩 벡터 (Float32 리틀엔디언 바이너리 — 고속 무파싱 로드용) */
embeddingBlob: blob('embedding_blob', { mode: 'buffer' }),
chunkIndex: integer('chunk_index').notNull(), chunkIndex: integer('chunk_index').notNull(),
}, },
(table) => [ (table) => [
@ -181,6 +183,7 @@ export const meetingSessions = sqliteTable(
}, },
(table) => [ (table) => [
index('idx_meeting_sessions_created_at').on(table.createdAt), index('idx_meeting_sessions_created_at').on(table.createdAt),
index('idx_meeting_sessions_started_at').on(table.startedAt),
index('idx_meeting_sessions_status').on(table.status), index('idx_meeting_sessions_status').on(table.status),
] ]
) )
@ -193,6 +196,7 @@ export const meetingMemos = sqliteTable(
sessionId: text('session_id').notNull(), sessionId: text('session_id').notNull(),
content: text('content').notNull(), content: text('content').notNull(),
timestampMs: integer('timestamp_ms').notNull(), timestampMs: integer('timestamp_ms').notNull(),
revision: integer('revision').notNull().default(1),
createdAt: integer('created_at').notNull(), createdAt: integer('created_at').notNull(),
/** 모바일에서 수정한 메모를 커서로 가져오기 위한 수정 시각 (없으면 createdAt) */ /** 모바일에서 수정한 메모를 커서로 가져오기 위한 수정 시각 (없으면 createdAt) */
updatedAt: integer('updated_at'), updatedAt: integer('updated_at'),
@ -200,6 +204,7 @@ export const meetingMemos = sqliteTable(
(table) => [ (table) => [
index('idx_meeting_memos_session_id').on(table.sessionId), index('idx_meeting_memos_session_id').on(table.sessionId),
index('idx_meeting_memos_timestamp').on(table.timestampMs), index('idx_meeting_memos_timestamp').on(table.timestampMs),
index('idx_meeting_memos_created_at').on(table.createdAt),
] ]
) )

View file

@ -4,8 +4,8 @@ import { ipcMain } from 'electron'
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
import { ipcSuccess, ipcError, ErrorCode } from '@d3ro/core/errors' import { ipcSuccess, ipcError, ErrorCode } from '@d3ro/core/errors'
import { getAudioCaptureService, calculateRMS, type CaptureLease } from '../services/AudioCaptureService' import { getAudioCaptureService, calculateRMS, type CaptureLease } from '../services/AudioCaptureService'
import { getMainWindow } from '../windows/WindowManager'
import { configGet, configSet } from '../services/ConfigService' import { configGet, configSet } from '../services/ConfigService'
import { safeSendToRenderer } from './renderer-broadcast'
import type { SetDeviceParams, TestDeviceParams } from '@d3ro/core/types' import type { SetDeviceParams, TestDeviceParams } from '@d3ro/core/types'
let testTimer: ReturnType<typeof setTimeout> | null = null let testTimer: ReturnType<typeof setTimeout> | null = null
@ -44,6 +44,10 @@ async function stopAudioTest(): Promise<void> {
await lease?.release().catch(() => { /* ignore */ }) await lease?.release().catch(() => { /* ignore */ })
} }
export async function disposeAudioHandlers(): Promise<void> {
await stopAudioTest()
}
export function registerAudioHandlers(): void { export function registerAudioHandlers(): void {
ipcMain.handle(IPC_CHANNELS.AUDIO.GET_DEVICES, async () => { ipcMain.handle(IPC_CHANNELS.AUDIO.GET_DEVICES, async () => {
try { try {
@ -103,19 +107,13 @@ export function registerAudioHandlers(): void {
// level 0은 "무음/초기화 중"일 뿐 종료가 아님 — 종료는 done:true로만 알린다. // level 0은 "무음/초기화 중"일 뿐 종료가 아님 — 종료는 done:true로만 알린다.
testLevelInterval = setInterval(() => { testLevelInterval = setInterval(() => {
const level = testLastRms > 0 ? Math.min(1.0, Math.pow(testLastRms, 0.28)) : 0 const level = testLastRms > 0 ? Math.min(1.0, Math.pow(testLastRms, 0.28)) : 0
const win = getMainWindow() safeSendToRenderer(IPC_CHANNELS.AUDIO.TEST_LEVEL, { level, done: false })
if (win && !win.isDestroyed()) {
win.webContents.send(IPC_CHANNELS.AUDIO.TEST_LEVEL, { level, done: false })
}
}, 100) }, 100)
// durationMs(기본 5초) 후 자동 중지 // durationMs(기본 5초) 후 자동 중지
testTimer = setTimeout(() => { testTimer = setTimeout(() => {
void stopAudioTest() void stopAudioTest()
const win = getMainWindow() safeSendToRenderer(IPC_CHANNELS.AUDIO.TEST_LEVEL, { level: 0, done: true })
if (win && !win.isDestroyed()) {
win.webContents.send(IPC_CHANNELS.AUDIO.TEST_LEVEL, { level: 0, done: true })
}
}, durationMs) }, durationMs)
return ipcSuccess({ averageLevel: 0, peakLevel: 0, hasAudio: true }) return ipcSuccess({ averageLevel: 0, peakLevel: 0, hasAudio: true })
@ -131,10 +129,7 @@ export function registerAudioHandlers(): void {
// 테스트 수동 중지 (STOP 버튼) // 테스트 수동 중지 (STOP 버튼)
ipcMain.handle(IPC_CHANNELS.AUDIO.STOP_TEST, async () => { ipcMain.handle(IPC_CHANNELS.AUDIO.STOP_TEST, async () => {
await stopAudioTest() await stopAudioTest()
const win = getMainWindow() safeSendToRenderer(IPC_CHANNELS.AUDIO.TEST_LEVEL, { level: 0, done: true })
if (win && !win.isDestroyed()) {
win.webContents.send(IPC_CHANNELS.AUDIO.TEST_LEVEL, { level: 0, done: true })
}
return ipcSuccess(undefined) return ipcSuccess(undefined)
}) })
} }

View file

@ -1,13 +1,14 @@
// src/main/ipc/cloud-sync-handlers.ts // src/main/ipc/cloud-sync-handlers.ts
// Phase V2-4: CloudSync IPC 핸들러 // Phase V2-4: CloudSync IPC 핸들러
import { ipcMain, BrowserWindow } from 'electron' import { ipcMain } from 'electron'
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
import { ipcSuccess, ipcError, ErrorCode, D3ROError } from '@d3ro/core/errors' import { ipcSuccess, ipcError, ErrorCode, D3ROError } from '@d3ro/core/errors'
import { getCloudSyncService } from '../services/CloudSyncService' import { getCloudSyncService } from '../services/CloudSyncService'
import { getLogger } from '../services/LoggerService' import { getLogger } from '../services/LoggerService'
import { onConfigChanged } from '../services/ConfigService' import { onConfigChanged } from '../services/ConfigService'
import { isApplyingRemoteSettings } from '../services/sync/settings-sync' import { isApplyingRemoteSettings } from '../services/sync/settings-sync'
import { broadcastToAllWindows } from './renderer-broadcast'
const logger = getLogger('cloud-sync-handlers') const logger = getLogger('cloud-sync-handlers')
@ -95,37 +96,28 @@ export function registerCloudSyncHandlers(): void {
} }
}) })
// 이벤트 → renderer 브로드캐스트
function broadcast(channel: string, payload: unknown): void {
BrowserWindow.getAllWindows().forEach((win) => {
if (!win.isDestroyed()) {
win.webContents.send(channel, payload)
}
})
}
sync.on('auth-changed', (payload) => { sync.on('auth-changed', (payload) => {
broadcast(IPC_CHANNELS.CLOUD_SYNC.AUTH_CHANGED, payload) broadcastToAllWindows(IPC_CHANNELS.CLOUD_SYNC.AUTH_CHANGED, payload)
}) })
sync.on('sync-progress', (payload) => { sync.on('sync-progress', (payload) => {
broadcast(IPC_CHANNELS.CLOUD_SYNC.SYNC_PROGRESS, payload) broadcastToAllWindows(IPC_CHANNELS.CLOUD_SYNC.SYNC_PROGRESS, payload)
}) })
sync.on('sync-complete', (payload) => { sync.on('sync-complete', (payload) => {
broadcast(IPC_CHANNELS.CLOUD_SYNC.SYNC_COMPLETE, payload) broadcastToAllWindows(IPC_CHANNELS.CLOUD_SYNC.SYNC_COMPLETE, payload)
}) })
sync.on('sync-error', (payload) => { sync.on('sync-error', (payload) => {
broadcast(IPC_CHANNELS.CLOUD_SYNC.SYNC_ERROR, payload) broadcastToAllWindows(IPC_CHANNELS.CLOUD_SYNC.SYNC_ERROR, payload)
}) })
// 다른 기기의 변경이 반영되면 앱 공통 새로고침 신호로 알린다 (이력·사전·회의·명령 화면이 구독). // 다른 기기의 변경이 반영되면 앱 공통 새로고침 신호로 알린다 (이력·사전·회의·명령 화면이 구독).
// 동기화가 메인에서 바꾼 설정(언어·테마 등)을 화면에도 알린다 — IPC로 바꿀 때만 방송되던 경로. // 동기화가 메인에서 바꾼 설정(언어·테마 등)을 화면에도 알린다 — IPC로 바꿀 때만 방송되던 경로.
// (팝업 테마 등 키별 부수효과는 config-effects 레지스트리가 모든 경로에 한 번씩 적용한다) // (팝업 테마 등 키별 부수효과는 config-effects 레지스트리가 모든 경로에 한 번씩 적용한다)
onConfigChanged((event) => { onConfigChanged((event) => {
if (!isApplyingRemoteSettings()) return if (!isApplyingRemoteSettings()) return
broadcast(IPC_CHANNELS.CONFIG.CHANGED, event) broadcastToAllWindows(IPC_CHANNELS.CONFIG.CHANGED, event)
}) })
sync.on('data-changed', (payload) => { sync.on('data-changed', (payload) => {
broadcast(IPC_CHANNELS.APP.DATA_CHANGED, { type: 'cloud-sync', entities: payload.entities }) broadcastToAllWindows(IPC_CHANNELS.APP.DATA_CHANGED, { type: 'cloud-sync', entities: payload.entities })
}) })
logger.info('Cloud Sync IPC handlers registered') logger.info('Cloud Sync IPC handlers registered')

View file

@ -1,15 +1,14 @@
// src/main/ipc/config-handlers.ts // src/main/ipc/config-handlers.ts
import { ipcMain, BrowserWindow } from 'electron' import { ipcMain } from 'electron'
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
import { ipcSuccess, ipcError, ErrorCode } from '@d3ro/core/errors' import { ipcSuccess, ipcError, ErrorCode } from '@d3ro/core/errors'
import { configGet, configSet, configGetAll, configReset } from '../services/ConfigService' import { configGet, configSet, configGetAll, configReset } from '../services/ConfigService'
import { broadcastToAllWindows } from './renderer-broadcast'
/** 설정 변경 시 모든 렌더러에 broadcast */ /** 설정 변경 시 모든 렌더러에 broadcast */
function broadcastConfigChanged(key: string, value: unknown, previousValue?: unknown): void { function broadcastConfigChanged(key: string, value: unknown, previousValue?: unknown): void {
for (const win of BrowserWindow.getAllWindows()) { broadcastToAllWindows(IPC_CHANNELS.CONFIG.CHANGED, { key, value, previousValue })
win.webContents.send(IPC_CHANNELS.CONFIG.CHANGED, { key, value, previousValue })
}
} }
import type { import type {
ConfigGetParams, ConfigGetParams,

View file

@ -3,8 +3,9 @@
// 키바인딩 IPC. 액션을 파라미터로 받는 단일 채널 집합이며, // 키바인딩 IPC. 액션을 파라미터로 받는 단일 채널 집합이며,
// 검증·충돌 판정은 전부 `packages/core/src/keybinding.ts` 의 함수를 쓴다. // 검증·충돌 판정은 전부 `packages/core/src/keybinding.ts` 의 함수를 쓴다.
import { ipcMain, BrowserWindow } from 'electron' import { ipcMain } from 'electron'
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
import { broadcastToAllWindows } from './renderer-broadcast'
import { ipcSuccess, ipcError, ErrorCode } from '@d3ro/core/errors' import { ipcSuccess, ipcError, ErrorCode } from '@d3ro/core/errors'
import { import {
bindingKey, bindingKey,
@ -37,9 +38,7 @@ import type {
} from '@d3ro/core/types' } from '@d3ro/core/types'
function broadcast(channel: string, payload: unknown): void { function broadcast(channel: string, payload: unknown): void {
for (const win of BrowserWindow.getAllWindows()) { broadcastToAllWindows(channel, payload)
win.webContents.send(channel, payload)
}
} }
/** 바인딩 맵 변경을 모든 창에 알린다 (창 간 동기화). */ /** 바인딩 맵 변경을 모든 창에 알린다 (창 간 동기화). */

View file

@ -1,7 +1,7 @@
// src/main/ipc/license-handlers.ts // src/main/ipc/license-handlers.ts
// Phase 11: 라이센스 IPC 핸들러 // Phase 11: 라이센스 IPC 핸들러
import { app, ipcMain, BrowserWindow } from 'electron' import { app, ipcMain } from 'electron'
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
import { ipcSuccess, ipcError, ErrorCode } from '@d3ro/core/errors' import { ipcSuccess, ipcError, ErrorCode } from '@d3ro/core/errors'
import { getLicenseService } from '../services/LicenseService' import { getLicenseService } from '../services/LicenseService'
@ -13,22 +13,19 @@ import type {
import { Feature } from '@d3ro/core/types' import { Feature } from '@d3ro/core/types'
import { billingUrl, PUBLIC_SITE_ORIGIN } from '@d3ro/core/web-urls' import { billingUrl, PUBLIC_SITE_ORIGIN } from '@d3ro/core/web-urls'
import { openExternalSafe } from '../windows/web-contents-hardening' import { openExternalSafe } from '../windows/web-contents-hardening'
import { broadcastToAllWindows } from './renderer-broadcast'
/** 개발 빌드는 로컬 웹앱(`npm run dev --workspace=@d3ro/web`)의 결제 페이지를 연다. */ /** 개발 빌드는 로컬 웹앱(`npm run dev --workspace=@d3ro/web`)의 결제 페이지를 연다. */
const DEV_WEB_ORIGIN = 'http://localhost:3000' const DEV_WEB_ORIGIN = 'http://localhost:3000'
/** 업그레이드 유도 이벤트를 모든 렌더러에 broadcast */ /** 업그레이드 유도 이벤트를 모든 렌더러에 broadcast */
function broadcastUpgradePrompt(event: UpgradePromptEvent): void { function broadcastUpgradePrompt(event: UpgradePromptEvent): void {
for (const win of BrowserWindow.getAllWindows()) { broadcastToAllWindows(IPC_CHANNELS.LICENSE.UPGRADE_PROMPT, event)
win.webContents.send(IPC_CHANNELS.LICENSE.UPGRADE_PROMPT, event)
}
} }
/** 티어 변경을 모든 렌더러에 broadcast */ /** 티어 변경을 모든 렌더러에 broadcast */
function broadcastTierChanged(info: LicenseInfo): void { function broadcastTierChanged(info: LicenseInfo): void {
for (const win of BrowserWindow.getAllWindows()) { broadcastToAllWindows(IPC_CHANNELS.LICENSE.TIER_CHANGED, info)
win.webContents.send(IPC_CHANNELS.LICENSE.TIER_CHANGED, info)
}
} }
export function registerLicenseHandlers(): void { export function registerLicenseHandlers(): void {

View file

@ -10,7 +10,7 @@ import { getLlmGateway } from '../services/llm/LlmGateway'
import { configGet, configSet } from '../services/ConfigService' import { configGet, configSet } from '../services/ConfigService'
import { buildInstructionInvocation, resolveTargetLanguage } from '../services/llm-prompts' import { buildInstructionInvocation, resolveTargetLanguage } from '../services/llm-prompts'
import { normalizeLoopbackUrl } from '../utils/loopback' import { normalizeLoopbackUrl } from '../utils/loopback'
import { getMainWindow } from '../windows/WindowManager' import { safeSendToRenderer } from './renderer-broadcast'
import type { import type {
SetLLMModelParams, SetLLMModelParams,
SetServerUrlParams, SetServerUrlParams,
@ -18,13 +18,6 @@ import type {
PullModelParams PullModelParams
} from '@d3ro/core/types' } from '@d3ro/core/types'
function safeSendToRenderer(channel: string, data: unknown): void {
const win = getMainWindow()
if (win && !win.isDestroyed()) {
win.webContents.send(channel, data)
}
}
export function registerLLMHandlers(): void { export function registerLLMHandlers(): void {
const localLlm = getLocalLLMService() const localLlm = getLocalLLMService()
localLlm.on('availability-changed', () => { localLlm.on('availability-changed', () => {

View file

@ -0,0 +1,29 @@
// src/main/ipc/renderer-broadcast.ts
// 메인/전체 렌더러 창으로 안전하게 IPC 이벤트를 송신하는 공용 헬퍼.
import { BrowserWindow } from 'electron'
import { getMainWindow } from '../windows/WindowManager'
function canSendToWindow(win: BrowserWindow | null | undefined): win is BrowserWindow {
if (!win || win.isDestroyed()) return false
const contents = win.webContents as { isDestroyed?: () => boolean } | undefined
if (!contents) return false
return typeof contents.isDestroyed === 'function' ? !contents.isDestroyed() : true
}
/** 메인 창이 살아 있을 때만 IPC 채널로 데이터를 전송한다. */
export function safeSendToRenderer(channel: string, data: unknown): void {
const win = getMainWindow()
if (canSendToWindow(win)) {
win.webContents.send(channel, data)
}
}
/** 파괴되지 않은 모든 BrowserWindow 렌더러에 IPC 이벤트를 브로드캐스트한다. */
export function broadcastToAllWindows(channel: string, payload: unknown): void {
for (const win of BrowserWindow.getAllWindows()) {
if (canSendToWindow(win)) {
win.webContents.send(channel, payload)
}
}
}

View file

@ -10,7 +10,7 @@ import type { RuntimeComponent } from '../services/RuntimeProvisioner'
import { parseRuntimeEnsureRequest } from './runtime-ensure-request' import { parseRuntimeEnsureRequest } from './runtime-ensure-request'
import { getSTTManager } from '../services/stt/STTManager' import { getSTTManager } from '../services/stt/STTManager'
import { configGet, configSet } from '../services/ConfigService' import { configGet, configSet } from '../services/ConfigService'
import { getMainWindow } from '../windows/WindowManager' import { safeSendToRenderer } from './renderer-broadcast'
import type { import type {
SetSTTModelParams, SetSTTModelParams,
SetSTTLanguageParams, SetSTTLanguageParams,
@ -21,13 +21,6 @@ import type {
STTProviderType, STTProviderType,
} from '@d3ro/core/types' } from '@d3ro/core/types'
function safeSendToRenderer(channel: string, data: unknown): void {
const win = getMainWindow()
if (win && !win.isDestroyed()) {
win.webContents.send(channel, data)
}
}
/** /**
* 던져진 오류를 IPC 오류로 바꾼다. D3ROError 면 그 코드를 보존하고, 아니면 fallback 코드를 쓴다. * 던져진 오류를 IPC 오류로 바꾼다. D3ROError 면 그 코드를 보존하고, 아니면 fallback 코드를 쓴다.
* (예전 런타임 핸들러는 `ipcError(error, code)` 로 인자 순서가 뒤바뀌어 code 자리에 Error 객체가, * (예전 런타임 핸들러는 `ipcError(error, code)` 로 인자 순서가 뒤바뀌어 code 자리에 Error 객체가,

View file

@ -5,6 +5,7 @@ import { app, ipcMain } from 'electron'
import os from 'os' import os from 'os'
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
import { ok } from '@d3ro/core/errors' import { ok } from '@d3ro/core/errors'
import { redactPII } from '@d3ro/core/utils/pii-redactor'
import type { import type {
SystemDiagnosticsPayload, SystemDiagnosticsPayload,
AIAssistQuery, AIAssistQuery,
@ -88,6 +89,16 @@ export function registerSupportHandlers(): void {
}, },
) => { ) => {
const now = Date.now() const now = Date.now()
const rawDiagnostics = ticketData.diagnostics ?? ticketData.diagnosticsSnapshot
const sanitizedDiagnostics: SystemDiagnosticsPayload | undefined = rawDiagnostics
? {
...rawDiagnostics,
recentErrors: (rawDiagnostics.recentErrors ?? []).map((entry) => ({
...entry,
message: redactPII(entry.message, 'mask').redactedText,
})),
}
: undefined
const newTicket: SupportTicket = { const newTicket: SupportTicket = {
id: `tkt_${now}`, id: `tkt_${now}`,
userId: 'user_local', userId: 'user_local',
@ -96,9 +107,9 @@ export function registerSupportHandlers(): void {
category: ticketData.category || 'general', category: ticketData.category || 'general',
priority: ticketData.priority || 'normal', priority: ticketData.priority || 'normal',
status: 'open', status: 'open',
subject: ticketData.subject || '고객 지원 문의', subject: redactPII(ticketData.subject || '고객 지원 문의', 'mask').redactedText,
description: ticketData.description || '', description: redactPII(ticketData.description || '', 'mask').redactedText,
diagnostics: ticketData.diagnostics ?? ticketData.diagnosticsSnapshot, diagnostics: sanitizedDiagnostics,
slaDueAt: ticketData.slaDueAt ?? now + 24 * 60 * 60 * 1000, slaDueAt: ticketData.slaDueAt ?? now + 24 * 60 * 60 * 1000,
createdAt: now, createdAt: now,
updatedAt: now, updatedAt: now,

View file

@ -4,16 +4,9 @@ import { ipcMain } from 'electron'
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
import { ipcSuccess, ipcError, ErrorCode } from '@d3ro/core/errors' import { ipcSuccess, ipcError, ErrorCode } from '@d3ro/core/errors'
import { getVoiceModeService } from '../services/VoiceModeService' import { getVoiceModeService } from '../services/VoiceModeService'
import { getMainWindow } from '../windows/WindowManager' import { safeSendToRenderer } from './renderer-broadcast'
import type { StartRecordingParams, StopRecordingParams, CancelRecordingParams, SetVoiceModeParams, VoiceErrorEvent } from '@d3ro/core/types' import type { StartRecordingParams, StopRecordingParams, CancelRecordingParams, SetVoiceModeParams, VoiceErrorEvent } from '@d3ro/core/types'
function safeSendToRenderer(channel: string, data: unknown): void {
const win = getMainWindow()
if (win && !win.isDestroyed()) {
win.webContents.send(channel, data)
}
}
/** 두 IPC 전송 경로(invoke/send)가 공유하는 단일 취소 동작 */ /** 두 IPC 전송 경로(invoke/send)가 공유하는 단일 취소 동작 */
function cancelVoiceSession(): void { function cancelVoiceSession(): void {
getVoiceModeService().cancelSession() getVoiceModeService().cancelSession()

View file

@ -12,6 +12,7 @@ import { getSoxPath } from '../utils/paths'
import type { AudioDevice } from '@d3ro/core/types' import type { AudioDevice } from '@d3ro/core/types'
import { AUDIO_FORMAT, TIMING } from '@d3ro/core/constants' import { AUDIO_FORMAT, TIMING } from '@d3ro/core/constants'
import { D3ROError, ErrorCode } from '@d3ro/core/errors' import { D3ROError, ErrorCode } from '@d3ro/core/errors'
import { secureWipeAudioChunks, secureZeroBuffer } from '@d3ro/core/utils/secure-memory'
const logger = getLogger('AudioCaptureService') const logger = getLogger('AudioCaptureService')
@ -522,15 +523,22 @@ class AudioCaptureService extends EventEmitter {
if (rms > peakRms) peakRms = rms if (rms > peakRms) peakRms = rms
}) })
proc.on('error', (err) => { let settled = false
reject(new D3ROError(ErrorCode.AudioCaptureStartFailed, `SoX 실행 실패: ${err.message}`)) const durationTimer = setTimeout(() => {
})
setTimeout(() => {
proc.kill() proc.kill()
}, durationMs) }, durationMs)
proc.on('error', (err) => {
if (settled) return
settled = true
clearTimeout(durationTimer)
reject(new D3ROError(ErrorCode.AudioCaptureStartFailed, `SoX 실행 실패: ${err.message}`))
})
proc.on('close', () => { proc.on('close', () => {
if (settled) return
settled = true
clearTimeout(durationTimer)
if (buffers.length === 0) { if (buffers.length === 0) {
resolve({ averageLevel: 0, peakLevel: 0, hasAudio: false }) resolve({ averageLevel: 0, peakLevel: 0, hasAudio: false })
return return
@ -645,8 +653,9 @@ class AudioCaptureService extends EventEmitter {
} }
} }
secureZeroBuffer(this._residualBuffer)
secureWipeAudioChunks(this._levelAccumulator)
this._residualBuffer = Buffer.alloc(0) this._residualBuffer = Buffer.alloc(0)
this._levelAccumulator = []
} }
// EventEmitter 타입 오버라이드 // EventEmitter 타입 오버라이드

View file

@ -380,24 +380,16 @@ class InputTelemetryService extends EventEmitter {
this._onKeyUp = (e: UiohookKeyboardEvent) => this._handleKeyUp(e) this._onKeyUp = (e: UiohookKeyboardEvent) => this._handleKeyUp(e)
this._onMouseDown = (e: UiohookMouseEvent) => this._handleMouseDown(e) this._onMouseDown = (e: UiohookMouseEvent) => this._handleMouseDown(e)
this._onMouseUp = (e: UiohookMouseEvent) => this._handleMouseUp(e) this._onMouseUp = (e: UiohookMouseEvent) => this._handleMouseUp(e)
this._onMouseMove = (e: UiohookMouseEvent) => this._handleMouseMove(e)
this._onWheel = (e: UiohookWheelEvent) => this._handleWheel(e)
uIOhook.on('keydown', this._onKeyDown) uIOhook.on('keydown', this._onKeyDown)
uIOhook.on('keyup', this._onKeyUp) uIOhook.on('keyup', this._onKeyUp)
uIOhook.on('mousedown', this._onMouseDown) uIOhook.on('mousedown', this._onMouseDown)
uIOhook.on('mouseup', this._onMouseUp) uIOhook.on('mouseup', this._onMouseUp)
uIOhook.on('mousemove', this._onMouseMove)
uIOhook.on('wheel', this._onWheel)
this._releaseHook = acquireGlobalInputHook() this._releaseHook = acquireGlobalInputHook()
this._running = true this._running = true
this._inputRevision += 1 this._inputRevision += 1
this._flushTimer = setInterval( this._syncCollectionMode()
() => this._flush(),
INPUT_TELEMETRY_DEFAULTS.flushIntervalMs
)
this._flushTimer.unref?.()
// 주기 샘플러. // 주기 샘플러.
// //
@ -411,9 +403,6 @@ class InputTelemetryService extends EventEmitter {
) )
this._sampleTimer.unref?.() this._sampleTimer.unref?.()
if (this._isTelemetryCollectionEnabled()) {
this._pruneOldData()
}
logger.info( logger.info(
this._isTelemetryCollectionEnabled() this._isTelemetryCollectionEnabled()
? '입력 텔레메트리 시작 (키/마우스 집계)' ? '입력 텔레메트리 시작 (키/마우스 집계)'
@ -476,9 +465,52 @@ class InputTelemetryService extends EventEmitter {
const shouldRun = this._isEnabledByConfig() const shouldRun = this._isEnabledByConfig()
if (shouldRun && !this._running) this.start() if (shouldRun && !this._running) this.start()
else if (!shouldRun && this._running) this.stop() else if (!shouldRun && this._running) this.stop()
else if (this._running) this._syncCollectionMode()
this._emitState() this._emitState()
} }
/**
* 입력 텔레메트리 수집 동의가 켜진 동안에만 고빈도 마우스(mousemove 1,000Hz / wheel) 훅과
* 5초 DB 플러시 타이머를 가동한다. 기본 모드(오타 교정·제안만 활성)에서는 N-API 호출 폭풍을 원천 차단한다.
*/
private _syncCollectionMode(): void {
if (this._running && this._isTelemetryCollectionEnabled()) {
if (!this._onMouseMove) {
this._onMouseMove = (e: UiohookMouseEvent) => this._handleMouseMove(e)
uIOhook.on('mousemove', this._onMouseMove)
}
if (!this._onWheel) {
this._onWheel = (e: UiohookWheelEvent) => this._handleWheel(e)
uIOhook.on('wheel', this._onWheel)
}
if (!this._flushTimer) {
this._flushTimer = setInterval(
() => this._flush(),
INPUT_TELEMETRY_DEFAULTS.flushIntervalMs
)
this._flushTimer.unref?.()
}
if (this._prunedAt === 0) {
this._pruneOldData()
}
return
}
if (this._onMouseMove) {
uIOhook.removeListener('mousemove', this._onMouseMove)
this._onMouseMove = null
}
if (this._onWheel) {
uIOhook.removeListener('wheel', this._onWheel)
this._onWheel = null
}
if (this._flushTimer) {
clearInterval(this._flushTimer)
this._flushTimer = null
}
this._pending.clear()
}
dispose(): void { dispose(): void {
this.stop() this.stop()
this._recentWrites.clear() this._recentWrites.clear()
@ -579,11 +611,6 @@ class InputTelemetryService extends EventEmitter {
// 실제 키 입력은 화면을 바꾼다 — 이전 UIA 응답은 무효다. // 실제 키 입력은 화면을 바꾼다 — 이전 UIA 응답은 무효다.
this._inputRevision += 1 this._inputRevision += 1
this._registerInput(true) this._registerInput(true)
const bucket = this._bucket()
// 한글·일본어 IME 가 켜져 있으면 Windows 는 글자 키를 VK_PROCESSKEY 로 넘기고, uiohook 은 그것을
// 변환표에 없는 키코드로 보낸다 — 그대로 'other' 로 두면 한글 타이핑 전체가 "타이핑 아님" 이 되어
// 제안이 영영 뜨지 않았다(2026-10-03 실측: 글자 키가 전부 key=other). 수정자 없이 눌린 미지의
// 키는 IME 조합 키로 본다. 실제 텍스트 변화(editedSinceFocus)가 함께 있어야 제안이 열린다.
const keyClass = const keyClass =
vk === null vk === null
? this._modifiers.ctrl || this._modifiers.alt || this._modifiers.meta ? this._modifiers.ctrl || this._modifiers.alt || this._modifiers.meta
@ -591,9 +618,12 @@ class InputTelemetryService extends EventEmitter {
: 'ime' : 'ime'
: classifyKeyStroke(vk, this._modifiers) : classifyKeyStroke(vk, this._modifiers)
if (keyClass === 'shortcut') bucket.shortcuts += 1 if (this._isTelemetryCollectionEnabled()) {
else bucket.keystrokes += 1 const bucket = this._bucket()
if (keyClass === 'backspace') bucket.backspaces += 1 if (keyClass === 'shortcut') bucket.shortcuts += 1
else bucket.keystrokes += 1
if (keyClass === 'backspace') bucket.backspaces += 1
}
this._lastKeyAt = Date.now() this._lastKeyAt = Date.now()
this._lastKeyClass = keyClass this._lastKeyClass = keyClass
@ -650,9 +680,11 @@ class InputTelemetryService extends EventEmitter {
this._lastClickPos !== null && this._lastClickPos !== null &&
manhattanDistance(this._lastClickPos, position) < 6 manhattanDistance(this._lastClickPos, position) < 6
const bucket = this._bucket() if (this._isTelemetryCollectionEnabled()) {
if (isDoubleClick) bucket.doubleClicks += 1 const bucket = this._bucket()
else bucket.clicks += 1 if (isDoubleClick) bucket.doubleClicks += 1
else bucket.clicks += 1
}
this._lastClickAt = now this._lastClickAt = now
this._lastClickButton = button this._lastClickButton = button
@ -706,10 +738,12 @@ class InputTelemetryService extends EventEmitter {
const now = Date.now() const now = Date.now()
const gap = this._lastEventAt === 0 ? 0 : now - this._lastEventAt const gap = this._lastEventAt === 0 ? 0 : now - this._lastEventAt
if (gap > 0 && gap <= INPUT_TELEMETRY_DEFAULTS.activeIdleTimeoutMs) { if (this._isTelemetryCollectionEnabled()) {
this._bucket().activeMs += gap if (gap > 0 && gap <= INPUT_TELEMETRY_DEFAULTS.activeIdleTimeoutMs) {
} else if (isKeyboard && this._lastActiveTickAt > 0) { this._bucket().activeMs += gap
this._bucket().activeMs += 250 } else if (isKeyboard && this._lastActiveTickAt > 0) {
this._bucket().activeMs += 250
}
} }
if (isKeyboard) this._lastActiveTickAt = now if (isKeyboard) this._lastActiveTickAt = now
@ -1240,10 +1274,12 @@ class InputTelemetryService extends EventEmitter {
return return
} }
const bucket = this._bucket() if (this._isTelemetryCollectionEnabled()) {
bucket.chars += delta.insertedChars const bucket = this._bucket()
bucket.words += delta.insertedWords bucket.chars += delta.insertedChars
bucket.sentences += delta.insertedSentences bucket.words += delta.insertedWords
bucket.sentences += delta.insertedSentences
}
if (!delta.insertedText) return if (!delta.insertedText) return
const typedText = this._withoutExpectedInsert(delta.insertedText) const typedText = this._withoutExpectedInsert(delta.insertedText)
@ -1493,41 +1529,43 @@ class InputTelemetryService extends EventEmitter {
try { try {
const db = getDatabase() const db = getDatabase()
for (const [key, bucket] of entries) { db.transaction((tx) => {
const [date, hourRaw, appName] = key.split('|') for (const [key, bucket] of entries) {
const hour = Number(hourRaw) const [date, hourRaw, appName] = key.split('|')
if (!date || Number.isNaN(hour)) continue const hour = Number(hourRaw)
if (!date || Number.isNaN(hour)) continue
mergeActivityBucket(this._sessionTotals, bucket) mergeActivityBucket(this._sessionTotals, bucket)
db.insert(inputActivity) tx.insert(inputActivity)
.values({ .values({
id: crypto.randomUUID(), id: crypto.randomUUID(),
date, date,
hour, hour,
appName: appName ?? '', appName: appName ?? '',
...bucket, ...bucket,
updatedAt: now
})
.onConflictDoUpdate({
target: [inputActivity.date, inputActivity.hour, inputActivity.appName],
set: {
keystrokes: sql`${inputActivity.keystrokes} + ${bucket.keystrokes}`,
shortcuts: sql`${inputActivity.shortcuts} + ${bucket.shortcuts}`,
backspaces: sql`${inputActivity.backspaces} + ${bucket.backspaces}`,
clicks: sql`${inputActivity.clicks} + ${bucket.clicks}`,
doubleClicks: sql`${inputActivity.doubleClicks} + ${bucket.doubleClicks}`,
scrollTicks: sql`${inputActivity.scrollTicks} + ${bucket.scrollTicks}`,
mouseDistancePx: sql`${inputActivity.mouseDistancePx} + ${bucket.mouseDistancePx}`,
chars: sql`${inputActivity.chars} + ${bucket.chars}`,
words: sql`${inputActivity.words} + ${bucket.words}`,
sentences: sql`${inputActivity.sentences} + ${bucket.sentences}`,
activeMs: sql`${inputActivity.activeMs} + ${bucket.activeMs}`,
updatedAt: now updatedAt: now
} })
}) .onConflictDoUpdate({
.run() target: [inputActivity.date, inputActivity.hour, inputActivity.appName],
} set: {
keystrokes: sql`${inputActivity.keystrokes} + ${bucket.keystrokes}`,
shortcuts: sql`${inputActivity.shortcuts} + ${bucket.shortcuts}`,
backspaces: sql`${inputActivity.backspaces} + ${bucket.backspaces}`,
clicks: sql`${inputActivity.clicks} + ${bucket.clicks}`,
doubleClicks: sql`${inputActivity.doubleClicks} + ${bucket.doubleClicks}`,
scrollTicks: sql`${inputActivity.scrollTicks} + ${bucket.scrollTicks}`,
mouseDistancePx: sql`${inputActivity.mouseDistancePx} + ${bucket.mouseDistancePx}`,
chars: sql`${inputActivity.chars} + ${bucket.chars}`,
words: sql`${inputActivity.words} + ${bucket.words}`,
sentences: sql`${inputActivity.sentences} + ${bucket.sentences}`,
activeMs: sql`${inputActivity.activeMs} + ${bucket.activeMs}`,
updatedAt: now
}
})
.run()
}
})
this.emit('activity', { this.emit('activity', {
totals: { ...this._sessionTotals }, totals: { ...this._sessionTotals },
appName: this._foreground.appName appName: this._foreground.appName

View file

@ -185,6 +185,7 @@ class LocalLLMService extends EventEmitter {
private _serverVersion: string | null = null private _serverVersion: string | null = null
private _activeRequests = new Set<ActiveRequest>() private _activeRequests = new Set<ActiveRequest>()
private _ensureRunningPromise: Promise<'running' | 'starting' | 'not-installed' | 'failed'> | null = null private _ensureRunningPromise: Promise<'running' | 'starting' | 'not-installed' | 'failed'> | null = null
private _startCheckTimers = new Set<ReturnType<typeof setTimeout>>()
private _disposed = false private _disposed = false
get state(): LLMState { get state(): LLMState {
@ -200,10 +201,17 @@ class LocalLLMService extends EventEmitter {
*/ */
async startServer(): Promise<'running' | 'starting' | 'not-installed' | 'failed'> { async startServer(): Promise<'running' | 'starting' | 'not-installed' | 'failed'> {
const result = await this.ensureRunning() const result = await this.ensureRunning()
if (result === 'starting' || result === 'running') { if ((result === 'starting' || result === 'running') && !this._disposed) {
// 1초 뒤 빠른 재확인 // 1초·3초 뒤 빠른 재확인 (dispose 시 정리될 수 있도록 핸들을 추적한다)
setTimeout(() => this._checkAvailability(), 1000) for (const delayMs of [1000, 3000]) {
setTimeout(() => this._checkAvailability(), 3000) const timer = setTimeout(() => {
this._startCheckTimers.delete(timer)
if (!this._disposed) {
void this._checkAvailability()
}
}, delayMs)
this._startCheckTimers.add(timer)
}
} }
return result return result
} }
@ -977,6 +985,10 @@ class LocalLLMService extends EventEmitter {
dispose(): void { dispose(): void {
this._disposed = true this._disposed = true
for (const timer of this._startCheckTimers) {
clearTimeout(timer)
}
this._startCheckTimers.clear()
this.stopPolling() this.stopPolling()
this.cancelGeneration() this.cancelGeneration()
this._refreshState() this._refreshState()

View file

@ -128,6 +128,8 @@ const SIDECAR_REQUEST_TIMEOUT_MS = 120000
const SIDECAR_TRANSCRIBE_MS_PER_AUDIO_SECOND = 3000 const SIDECAR_TRANSCRIBE_MS_PER_AUDIO_SECOND = 3000
/** 부분 전사(미리보기) 타임아웃 — 실패해도 무시되므로 짧게 잡는다 */ /** 부분 전사(미리보기) 타임아웃 — 실패해도 무시되므로 짧게 잡는다 */
const SIDECAR_PARTIAL_TIMEOUT_MS = 15000 const SIDECAR_PARTIAL_TIMEOUT_MS = 15000
/** 장시간 받아쓰기가 없을 때 Whisper 기본 모델(1.6~3.1GB)만 내려 RAM/VRAM 을 돌려주는 유휴 한도(15분). */
const IDLE_MODEL_UNLOAD_MS = 15 * 60_000
/** 알려진 Whisper 모델 카탈로그 */ /** 알려진 Whisper 모델 카탈로그 */
const MODEL_CATALOG: STTModel[] = [ const MODEL_CATALOG: STTModel[] = [
@ -240,6 +242,7 @@ class LocalSTTService extends EventEmitter {
private _initializing: { modelId: string; promise: Promise<void> } | null = null private _initializing: { modelId: string; promise: Promise<void> } | null = null
// errorEmitted 플래그로 이벤트 중복 방지 // errorEmitted 플래그로 이벤트 중복 방지
private _errorEmitted: boolean = false private _errorEmitted: boolean = false
private _idleUnloadTimer: NodeJS.Timeout | null = null
// ── 상태 접근자 ── // ── 상태 접근자 ──
@ -256,6 +259,11 @@ class LocalSTTService extends EventEmitter {
return this._supervisor.baseUrl return this._supervisor.baseUrl
} }
/** 실행 중인 sidecar HTTP 기본 URL (기동되지 않았으면 null) */
get sidecarBaseUrl(): string | null {
return this._supervisor.isHealthy ? this._supervisor.baseUrl : null
}
// ── 공개 메서드 ── // ── 공개 메서드 ──
/** /**
@ -302,6 +310,7 @@ class LocalSTTService extends EventEmitter {
} }
private async _loadInitial(targetModel: string): Promise<void> { private async _loadInitial(targetModel: string): Promise<void> {
this._clearIdleUnload()
this._setState(STTState.Loading) this._setState(STTState.Loading)
this._errorEmitted = false this._errorEmitted = false
@ -316,6 +325,7 @@ class LocalSTTService extends EventEmitter {
this._setState(STTState.Ready) this._setState(STTState.Ready)
// 모델까지 올라갔으면 사이드카는 안정적이다 — 크래시 재시작 카운트 초기화 // 모델까지 올라갔으면 사이드카는 안정적이다 — 크래시 재시작 카운트 초기화
this._supervisor.markStable() this._supervisor.markStable()
this._scheduleIdleUnload()
logger.info(`STT 초기화 완료: 모델=${targetModel}`) logger.info(`STT 초기화 완료: 모델=${targetModel}`)
} catch (err) { } catch (err) {
@ -626,6 +636,7 @@ class LocalSTTService extends EventEmitter {
logger.info('LocalSTTService dispose 시작') logger.info('LocalSTTService dispose 시작')
this._clearIdleUnload()
this._initializing = null this._initializing = null
this._modelReady = false this._modelReady = false
this._auxModelId = null this._auxModelId = null
@ -636,6 +647,60 @@ class LocalSTTService extends EventEmitter {
logger.info('LocalSTTService dispose 완료') logger.info('LocalSTTService dispose 완료')
} }
private _clearIdleUnload(): void {
if (this._idleUnloadTimer) {
clearTimeout(this._idleUnloadTimer)
this._idleUnloadTimer = null
}
}
private _scheduleIdleUnload(): void {
this._clearIdleUnload()
if (this._disposed || !this._modelReady) return
this._idleUnloadTimer = setTimeout(() => {
this._idleUnloadTimer = null
void this.releasePrimaryModel()
}, IDLE_MODEL_UNLOAD_MS)
this._idleUnloadTimer.unref?.()
}
/**
* 기본(primary) Whisper 모델만 메모리에서 내리고 사이드카 HTTP 프로세스(UIA 브리지)는 유지한다.
* 클라우드 STT 전환이나 장시간 유휴 시 1.6~3.1GB RAM/VRAM 을 즉시 회수하며,
* 다음 로컬 전사 요청이 들어오면 transcribe() 가 다시 자동으로 모델을 올린다.
*/
async releasePrimaryModel(): Promise<void> {
this._clearIdleUnload()
if (this._disposed || (!this._currentModelId && !this._modelReady)) return
if (this._state === STTState.Transcribing || this._initializing) {
this._scheduleIdleUnload()
return
}
const modelId = this._currentModelId
this._modelReady = false
this._currentModelId = null
this._setState(STTState.Uninitialized)
if (!this._supervisor.isHealthy) return
try {
const response = await fetch(`${this._baseUrl}/unload`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ slot: 'primary' }),
signal: AbortSignal.timeout(SIDECAR_REQUEST_TIMEOUT_MS),
})
await response.body?.cancel()
if (response.ok) {
logger.info(`기본 STT 모델 언로드: ${modelId ?? 'unknown'}`)
}
} catch (error) {
logger.debug(
`기본 STT 모델 언로드 통신 무시: ${error instanceof Error ? error.message : String(error)}`,
)
}
}
private _emitDownloadProgress( private _emitDownloadProgress(
modelId: string, modelId: string,
percent: number, percent: number,
@ -738,6 +803,7 @@ class LocalSTTService extends EventEmitter {
} }
const isPartial = options?.partial === true const isPartial = options?.partial === true
this._clearIdleUnload()
if (!isPartial) { if (!isPartial) {
this._setState(STTState.Transcribing) this._setState(STTState.Transcribing)
@ -857,6 +923,10 @@ class LocalSTTService extends EventEmitter {
ErrorCode.STTTranscriptionFailed, ErrorCode.STTTranscriptionFailed,
`전사 실패: ${message}`, `전사 실패: ${message}`,
) )
} finally {
if (this._modelReady) {
this._scheduleIdleUnload()
}
} }
} }
@ -864,6 +934,7 @@ class LocalSTTService extends EventEmitter {
/** 사이드카가 죽으면 올려 둔 모델도 함께 사라진다 */ /** 사이드카가 죽으면 올려 둔 모델도 함께 사라진다 */
private _onSidecarExit(): void { private _onSidecarExit(): void {
this._clearIdleUnload()
this._modelReady = false this._modelReady = false
this._auxModelId = null this._auxModelId = null
} }

View file

@ -14,7 +14,10 @@ import { getMainWindow } from '../windows/WindowManager'
import { getDatabase } from '../db' import { getDatabase } from '../db'
import { onAccountScopeChanged } from './account-scope' import { onAccountScopeChanged } from './account-scope'
import { getLlmGateway } from './llm/LlmGateway' import { getLlmGateway } from './llm/LlmGateway'
import { getLocalSTTService } from './LocalSTTService'
import { MeetingRecordingSession, type RecordingSnapshot } from './meeting/MeetingRecordingSession' import { MeetingRecordingSession, type RecordingSnapshot } from './meeting/MeetingRecordingSession'
import { DiskWavAudioSink, writeWavHeader16kMonoPcm16 } from './meeting/disk-wav-audio-sink'
import { getSidecarBaseUrl } from '../utils/paths'
import { import {
isRecoverableMeeting, isRecoverableMeeting,
syncStateRecordingLedger, syncStateRecordingLedger,
@ -51,6 +54,7 @@ import {
applySpeakerLabels, applySpeakerLabels,
buildTranscriptFromSegments, buildTranscriptFromSegments,
chunkTranscriptByLines, chunkTranscriptByLines,
formatPyannoteSpeakerLabel,
formatTranscriptLines, formatTranscriptLines,
looksTruncatedRewrite, looksTruncatedRewrite,
parseTranscriptLines, parseTranscriptLines,
@ -389,6 +393,10 @@ class MeetingModeService extends EventEmitter {
return () => { audioCaptureService.off('audio-data', handler) } return () => { audioCaptureService.off('audio-data', handler) }
}, },
}, },
audioSink: new DiskWavAudioSink(meetingAudioFilePathSync(sessionId)),
onAudioSinkError: (err) => {
logger.warn(`회의 오디오 디스크 스트리밍 실패: ${err instanceof Error ? err.message : String(err)}`)
},
onSegment: (segment) => { onSegment: (segment) => {
this.emit('segment', segment) this.emit('segment', segment)
this._sendToRenderer(IPC_CHANNELS.MEETING_MODE.SEGMENT, segment) this._sendToRenderer(IPC_CHANNELS.MEETING_MODE.SEGMENT, segment)
@ -747,7 +755,11 @@ class MeetingModeService extends EventEmitter {
this._captionFailures.delete(snapshot.id) this._captionFailures.delete(snapshot.id)
this._clearLocalRecording(snapshot.id) this._clearLocalRecording(snapshot.id)
if (snapshot.pcm.length > 0) { if (snapshot.audioSavedToDisk) {
const wavPath = meetingAudioFilePathSync(snapshot.id)
getCloudSyncService().pushOne('meeting_audio', snapshot.id)
logger.info(`회의 오디오 저장(디스크 스트리밍): ${wavPath} (${snapshot.pcmByteLength}B)`)
} else if (snapshot.pcm.length > 0) {
try { try {
const wavPath = meetingAudioFilePathSync(snapshot.id) const wavPath = meetingAudioFilePathSync(snapshot.id)
fs.mkdirSync(pathDirname(wavPath), { recursive: true }) fs.mkdirSync(pathDirname(wavPath), { recursive: true })
@ -999,27 +1011,7 @@ class MeetingModeService extends EventEmitter {
/** PCM16 버퍼를 WAV 파일로 저장 (16kHz mono 16-bit) */ /** PCM16 버퍼를 WAV 파일로 저장 (16kHz mono 16-bit) */
private _saveWav(filePath: string, pcmBuffer: Buffer): void { private _saveWav(filePath: string, pcmBuffer: Buffer): void {
const sampleRate = 16000 const header = writeWavHeader16kMonoPcm16(Buffer.alloc(44), pcmBuffer.length)
const numChannels = 1
const bitsPerSample = 16
const byteRate = sampleRate * numChannels * (bitsPerSample / 8)
const blockAlign = numChannels * (bitsPerSample / 8)
const dataSize = pcmBuffer.length
const header = Buffer.alloc(44)
header.write('RIFF', 0)
header.writeUInt32LE(36 + dataSize, 4)
header.write('WAVE', 8)
header.write('fmt ', 12)
header.writeUInt32LE(16, 16) // fmt chunk size
header.writeUInt16LE(1, 20) // PCM
header.writeUInt16LE(numChannels, 22)
header.writeUInt32LE(sampleRate, 24)
header.writeUInt32LE(byteRate, 28)
header.writeUInt16LE(blockAlign, 32)
header.writeUInt16LE(bitsPerSample, 34)
header.write('data', 36)
header.writeUInt32LE(dataSize, 40)
// 헤더와 PCM 을 이어 붙이지 않고 따로 쓴다 — 3시간 회의면 concat 사본만 350MB 가 더 생긴다 // 헤더와 PCM 을 이어 붙이지 않고 따로 쓴다 — 3시간 회의면 concat 사본만 350MB 가 더 생긴다
const fd = fs.openSync(filePath, 'w') const fd = fs.openSync(filePath, 'w')
@ -1488,8 +1480,11 @@ class MeetingModeService extends EventEmitter {
maxTokens: REWRITE_CHUNK_MAX_TOKENS, maxTokens: REWRITE_CHUNK_MAX_TOKENS,
}) })
const rawText = carry ? stripEchoedContext(result.text, previousTail) : result.text.trim() const rawText = carry ? stripEchoedContext(result.text, previousTail) : result.text.trim()
// 모델이 원문에 없던 시각·화자 표기를 붙였으면 지우고, 지운 한정 부사(거의 등)는 되돌린다. // 교열(!carry)에서는 모델이 원문에 없던 시각·화자 표기를 붙였으면 지우고, 화자 구분(carry)에서는 화자 표기를 허용한다.
const text = restoreProtectedQualifiers(chunk, stripInventedMarkers(chunk, rawText)) const text = restoreProtectedQualifiers(
chunk,
stripInventedMarkers(chunk, rawText, { allowSpeakerMarkers: carry }),
)
if (looksTruncatedRewrite(chunk, text)) { if (looksTruncatedRewrite(chunk, text)) {
throw new D3ROError( throw new D3ROError(
failureCode, failureCode,
@ -1641,7 +1636,7 @@ ${excerpt.text}`
try { try {
const audioBuffer = fs.readFileSync(audioPath) const audioBuffer = fs.readFileSync(audioPath)
const serverUrl = `http://127.0.0.1:18765` const serverUrl = getLocalSTTService().sidecarBaseUrl ?? getSidecarBaseUrl(18765)
const formData = new FormData() const formData = new FormData()
formData.append('audio', new Blob([audioBuffer]), 'audio.wav') formData.append('audio', new Blob([audioBuffer]), 'audio.wav')
@ -1675,7 +1670,7 @@ ${excerpt.text}`
const labeledLines = applySpeakerLabels( const labeledLines = applySpeakerLabels(
parseTranscriptLines(transcript), parseTranscriptLines(transcript),
diarResult.segments, diarResult.segments,
(raw) => raw.replace('SPEAKER_', '화자 '), (raw) => formatPyannoteSpeakerLabel(raw),
) )
this._sendToRenderer(IPC_CHANNELS.MEETING_MODE.DIARIZATION_PROGRESS, { sessionId, percent: 85 }) this._sendToRenderer(IPC_CHANNELS.MEETING_MODE.DIARIZATION_PROGRESS, { sessionId, percent: 85 })

View file

@ -7,6 +7,7 @@ import { configGet, configSet } from './ConfigService'
import { normalizeLoopbackUrl } from '../utils/loopback' import { normalizeLoopbackUrl } from '../utils/loopback'
import { D3ROError, ErrorCode } from '@d3ro/core/errors' import { D3ROError, ErrorCode } from '@d3ro/core/errors'
import type { LLMAction } from '@d3ro/core/types' import type { LLMAction } from '@d3ro/core/types'
import { redactPII, redactPIIMessages, rehydrateText } from '@d3ro/core/utils/pii-redactor'
import { resolveSystemPrompt } from './llm-prompts' import { resolveSystemPrompt } from './llm-prompts'
interface OnlineGenerateResponse { interface OnlineGenerateResponse {
@ -41,35 +42,38 @@ class OnlineLLMService extends EventEmitter {
return token return token
} }
async processText( private async _postOnlineRequest(
text: string, endpoint: string,
action: LLMAction, payload: Record<string, unknown>,
targetLanguage?: string, externalSignal?: AbortSignal
customPrompt?: string
): Promise<string> { ): Promise<string> {
const token = this._ensureAuth() const token = this._ensureAuth()
const apiUrl = normalizeLoopbackUrl(configGet('onlineApiUrl') ?? 'http://127.0.0.1:5000') const apiUrl = normalizeLoopbackUrl(configGet('onlineApiUrl') ?? 'http://127.0.0.1:5000')
const systemPrompt = resolveSystemPrompt(action, targetLanguage, customPrompt)
// cancelGeneration() 이 실제로 이 요청을 끊도록 컨트롤러를 등록한다(예전엔 한 번도 할당되지 않아 no-op). // cancelGeneration() 이 실제로 이 요청을 끊도록 컨트롤러를 등록한다(예전엔 한 번도 할당되지 않아 no-op).
this._abortController?.abort() this._abortController?.abort()
const controller = new AbortController() const controller = new AbortController()
this._abortController = controller this._abortController = controller
const onExternalAbort = (): void => {
controller.abort()
}
if (externalSignal) {
if (externalSignal.aborted) {
controller.abort()
} else {
externalSignal.addEventListener('abort', onExternalAbort, { once: true })
}
}
try { try {
const response = await fetch(`${apiUrl}/api/llm/generate`, { const response = await fetch(`${apiUrl}${endpoint}`, {
signal: controller.signal, signal: controller.signal,
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Authorization: `Bearer ${token}` Authorization: `Bearer ${token}`
}, },
body: JSON.stringify({ body: JSON.stringify(payload)
prompt: text,
systemPrompt,
model: configGet('llmModelId') ?? 'd3ro-gpt4o-mini',
temperature: 0.7,
maxTokens: 2048
})
}) })
if (response.status === 401) { if (response.status === 401) {
@ -87,11 +91,11 @@ class OnlineLLMService extends EventEmitter {
) )
} }
const data = (await response.json()) as OnlineGenerateResponse const data = (await response.json()) as Partial<OnlineGenerateResponse> | null
if (!data.text || !data.text.trim()) { if (typeof data?.text !== 'string' || data.text.trim().length === 0) {
throw new D3ROError(ErrorCode.LLMProcessingFailed, 'Online LLM returned empty text') throw new D3ROError(ErrorCode.LLMProcessingFailed, 'Online LLM returned empty text')
} }
return data.text.trim() return data.text
} catch (err) { } catch (err) {
if (err instanceof D3ROError) throw err if (err instanceof D3ROError) throw err
if (controller.signal.aborted) { if (controller.signal.aborted) {
@ -102,37 +106,48 @@ class OnlineLLMService extends EventEmitter {
`온라인 API 서버 연결 실패: ${err instanceof Error ? err.message : String(err)}` `온라인 API 서버 연결 실패: ${err instanceof Error ? err.message : String(err)}`
) )
} finally { } finally {
externalSignal?.removeEventListener('abort', onExternalAbort)
if (this._abortController === controller) this._abortController = null if (this._abortController === controller) this._abortController = null
} }
} }
async processText(
text: string,
action: LLMAction,
targetLanguage?: string,
customPrompt?: string
): Promise<string> {
this._ensureAuth()
const systemPrompt = resolveSystemPrompt(action, targetLanguage, customPrompt)
const redaction = redactPII(text, 'tokenize')
const rawText = await this._postOnlineRequest('/api/llm/generate', {
prompt: redaction.redactedText,
systemPrompt,
model: configGet('llmModelId') ?? 'd3ro-gpt4o-mini',
temperature: 0.7,
maxTokens: 2048
})
return rehydrateText(rawText.trim(), redaction.tokens)
}
async *chatStream( async *chatStream(
messages: Array<{ role: string; content: string }>, messages: Array<{ role: string; content: string }>,
options?: { model?: string; temperature?: number } options?: { model?: string; temperature?: number; signal?: AbortSignal }
): AsyncGenerator<string, string> { ): AsyncGenerator<string, string> {
const token = this._ensureAuth() this._ensureAuth()
const apiUrl = normalizeLoopbackUrl(configGet('onlineApiUrl') ?? 'http://127.0.0.1:5000') const redactedBatch = redactPIIMessages(messages)
const rawText = await this._postOnlineRequest(
const response = await fetch(`${apiUrl}/api/llm/chat`, { '/api/llm/chat',
method: 'POST', {
headers: { messages: redactedBatch.messages.map((m) => ({ role: m.role, content: m.content })),
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
},
body: JSON.stringify({
messages: messages.map((m) => ({ role: m.role, content: m.content })),
model: options?.model ?? configGet('llmModelId') ?? 'd3ro-gpt4o-mini', model: options?.model ?? configGet('llmModelId') ?? 'd3ro-gpt4o-mini',
temperature: options?.temperature ?? 0.7 temperature: options?.temperature ?? 0.7
}) },
}) options?.signal
)
if (!response.ok) { const restored = rehydrateText(rawText, redactedBatch.tokens)
throw new D3ROError(ErrorCode.LLMProcessingFailed, `Online API Error: ${response.status}`) yield restored
} return restored
const data = (await response.json()) as OnlineGenerateResponse
yield data.text
return data.text
} }
cancelGeneration(): void { cancelGeneration(): void {
@ -152,7 +167,7 @@ class OnlineLLMService extends EventEmitter {
let instance: OnlineLLMService | null = null let instance: OnlineLLMService | null = null
export function resetOnlineLLMServiceForTests(): void { export function resetOnlineLLMServiceForTests(): void {
if (instance) instance.removeAllListeners() if (instance) instance.dispose()
instance = null instance = null
} }

View file

@ -76,82 +76,101 @@ class PersonalGraphService {
.orderBy(desc(personalPhrases.lastUsedAt)) .orderBy(desc(personalPhrases.lastUsedAt))
.limit(INCREMENTAL_SHARES_SCAN_LIMIT) .limit(INCREMENTAL_SHARES_SCAN_LIMIT)
.all() .all()
.map((existing) => ({
phrase: existing.phrase,
terms: parseTerms(existing.terms),
}))
.filter((existing) => existing.terms.length > 0)
for (const sentence of sentences) { const preparedSentences = sentences.map((sentence) => {
const terms = extractTerms(sentence) const terms = extractTerms(sentence)
db.insert(personalPhrases) return { sentence, terms, termsJson: JSON.stringify(terms) }
.values({ })
id: crypto.randomUUID(),
phrase: sentence,
count: 1,
source: meta.source,
lastUsedAt: at,
createdAt: at,
terms: JSON.stringify(terms),
appName: meta.appName ?? null
})
.onConflictDoUpdate({
target: personalPhrases.phrase,
set: {
count: sql`${personalPhrases.count} + 1`,
lastUsedAt: at,
terms: JSON.stringify(terms),
appName: meta.appName ?? null
}
})
.run()
if (terms.length > 0) { db.transaction((tx) => {
for (const existing of recentExisting) { for (const entry of preparedSentences) {
if (existing.phrase === sentence) continue tx.insert(personalPhrases)
const existingTerms = parseTerms(existing.terms) .values({
if (existingTerms.length === 0) continue id: crypto.randomUUID(),
if (jaccard(terms, existingTerms) >= SHARES_TERMS_MIN_SIMILARITY) { phrase: entry.sentence,
this._upsertEdge(existing.phrase, sentence, 'shares_terms') count: 1,
source: meta.source,
lastUsedAt: at,
createdAt: at,
terms: entry.termsJson,
appName: meta.appName ?? null
})
.onConflictDoUpdate({
target: personalPhrases.phrase,
set: {
count: sql`${personalPhrases.count} + 1`,
lastUsedAt: at,
terms: entry.termsJson,
appName: meta.appName ?? null
}
})
.run()
if (entry.terms.length > 0) {
for (const existing of recentExisting) {
if (existing.phrase === entry.sentence) continue
if (jaccard(entry.terms, existing.terms) >= SHARES_TERMS_MIN_SIMILARITY) {
this._upsertEdge(existing.phrase, entry.sentence, 'shares_terms', tx, at)
}
} }
} }
} }
}
// 같은 앱에서 직전 세션(10분 이내)에 쓴 문장과 첫 문장을 follows 로 연결 // 같은 앱에서 직전 세션(10분 이내)에 쓴 문장과 첫 문장을 follows 로 연결
if (scopeKey) { if (scopeKey) {
const previousInScope = this._lastIndexedByScope.get(scopeKey) const previousInScope = this._lastIndexedByScope.get(scopeKey)
if ( if (
previousInScope && previousInScope &&
previousInScope.phrase !== sentences[0] && previousInScope.phrase !== sentences[0] &&
Math.abs(at - previousInScope.at) <= SESSION_FOLLOWS_WINDOW_MS Math.abs(at - previousInScope.at) <= SESSION_FOLLOWS_WINDOW_MS
) { ) {
this._upsertEdge(previousInScope.phrase, sentences[0], 'follows') this._upsertEdge(previousInScope.phrase, sentences[0], 'follows', tx, at)
}
} }
// follows 엣지 (같은 텍스트 내 인접 문장)
for (const edge of buildSequenceEdges(sentences)) {
this._upsertEdge(edge.from, edge.to, 'follows', tx, at)
}
// 같은 텍스트 안에서 용어를 많이 공유하는 쌍 (사전 계산한 terms 재사용)
for (let i = 0; i < preparedSentences.length; i += 1) {
const termsA = preparedSentences[i].terms
if (termsA.length === 0) continue
for (let j = i + 1; j < preparedSentences.length; j += 1) {
const termsB = preparedSentences[j].terms
if (termsB.length === 0) continue
if (jaccard(termsA, termsB) < SHARES_TERMS_MIN_SIMILARITY) continue
this._upsertEdge(preparedSentences[i].sentence, preparedSentences[j].sentence, 'shares_terms', tx, at)
}
}
})
if (scopeKey) {
this._lastIndexedByScope.set(scopeKey, { this._lastIndexedByScope.set(scopeKey, {
phrase: sentences[sentences.length - 1], phrase: sentences[sentences.length - 1],
at at
}) })
} }
// follows 엣지 (같은 텍스트 내 인접 문장)
for (const edge of buildSequenceEdges(sentences)) {
this._upsertEdge(edge.from, edge.to, 'follows')
}
// 같은 텍스트 안에서 용어를 많이 공유하는 쌍
for (let i = 0; i < sentences.length; i += 1) {
const termsA = extractTerms(sentences[i])
for (let j = i + 1; j < sentences.length; j += 1) {
const termsB = extractTerms(sentences[j])
if (jaccard(termsA, termsB) < SHARES_TERMS_MIN_SIMILARITY) continue
this._upsertEdge(sentences[i], sentences[j], 'shares_terms')
}
}
} catch (error) { } catch (error) {
logger.warn(`그래프 반영 실패: ${error instanceof Error ? error.message : String(error)}`) logger.warn(`그래프 반영 실패: ${error instanceof Error ? error.message : String(error)}`)
} }
} }
private _upsertEdge(from: string, to: string, kind: 'follows' | 'shares_terms'): void { private _upsertEdge(
const db = getDatabase() from: string,
const now = Date.now() to: string,
db.insert(phraseEdges) kind: 'follows' | 'shares_terms',
executor: Pick<ReturnType<typeof getDatabase>, 'insert'> = getDatabase(),
now = Date.now()
): void {
executor
.insert(phraseEdges)
.values({ .values({
id: crypto.randomUUID(), id: crypto.randomUUID(),
fromPhrase: from, fromPhrase: from,
@ -421,26 +440,29 @@ class PersonalGraphService {
runMaintenance(limit = MAINTENANCE_NODE_LIMIT): number { runMaintenance(limit = MAINTENANCE_NODE_LIMIT): number {
try { try {
const db = getDatabase() const db = getDatabase()
const nodes = db const parsedNodes = db
.select() .select({ phrase: personalPhrases.phrase, terms: personalPhrases.terms })
.from(personalPhrases) .from(personalPhrases)
.orderBy(desc(personalPhrases.lastUsedAt)) .orderBy(desc(personalPhrases.lastUsedAt))
.limit(limit) .limit(limit)
.all() .all()
.map((node) => ({ phrase: node.phrase, terms: parseTerms(node.terms) }))
.filter((node) => node.terms.length > 0)
let created = 0 let created = 0
for (let i = 0; i < nodes.length; i += 1) { const now = Date.now()
const termsA = parseTerms(nodes[i].terms) db.transaction((tx) => {
if (termsA.length === 0) continue for (let i = 0; i < parsedNodes.length; i += 1) {
for (let j = i + 1; j < nodes.length; j += 1) { const termsA = parsedNodes[i].terms
const termsB = parseTerms(nodes[j].terms) for (let j = i + 1; j < parsedNodes.length; j += 1) {
if (termsB.length === 0) continue const termsB = parsedNodes[j].terms
if (jaccard(termsA, termsB) < SHARES_TERMS_MIN_SIMILARITY) continue if (jaccard(termsA, termsB) < SHARES_TERMS_MIN_SIMILARITY) continue
this._upsertEdge(nodes[i].phrase, nodes[j].phrase, 'shares_terms') this._upsertEdge(parsedNodes[i].phrase, parsedNodes[j].phrase, 'shares_terms', tx, now)
created += 1 created += 1
if (created >= 200) return created if (created >= 200) return
}
} }
} })
if (created > 0) logger.info(`그래프 유지보수: shares_terms 엣지 ${created}개 생성`) if (created > 0) logger.info(`그래프 유지보수: shares_terms 엣지 ${created}개 생성`)
return created return created
} catch (error) { } catch (error) {

View file

@ -16,6 +16,7 @@ import {
isTerminalHost, isTerminalHost,
type ForegroundWindowPort, type ForegroundWindowPort,
} from './foreground-window-port' } from './foreground-window-port'
import { sendWin32CopyChord } from '../utils/win32-unicode-input'
const logger = getLogger('screen-context') const logger = getLogger('screen-context')
@ -43,6 +44,8 @@ export interface ScreenContextServiceDeps {
foreground?: ForegroundWindowPort foreground?: ForegroundWindowPort
/** 복사 단축키 수정자 결정용 플랫폼 (기본: process.platform) */ /** 복사 단축키 수정자 결정용 플랫폼 (기본: process.platform) */
platform?: NodeJS.Platform platform?: NodeJS.Platform
/** 네이티브 복사 단축키 주입기 (성공 시 true 반환하여 nut-js 로드를 생략) */
sendCopyShortcut?: () => Promise<boolean>
} }
// ============================================================ // ============================================================
@ -54,10 +57,16 @@ export class ScreenContextService {
private _nutLoadPromise: Promise<void> | null = null private _nutLoadPromise: Promise<void> | null = null
private readonly _foreground: ForegroundWindowPort private readonly _foreground: ForegroundWindowPort
private readonly _platform: NodeJS.Platform private readonly _platform: NodeJS.Platform
private readonly _sendCopyShortcut: (() => Promise<boolean>) | null
constructor(deps: ScreenContextServiceDeps = {}) { constructor(deps: ScreenContextServiceDeps = {}) {
this._platform = deps.platform ?? process.platform this._platform = deps.platform ?? process.platform
this._foreground = deps.foreground ?? createDefaultForegroundWindowPort(this._platform) this._foreground = deps.foreground ?? createDefaultForegroundWindowPort(this._platform)
this._sendCopyShortcut =
deps.sendCopyShortcut ??
(!process.env.VITEST && this._platform === 'win32'
? async () => sendWin32CopyChord()
: null)
} }
/** /**
@ -234,7 +243,6 @@ export class ScreenContextService {
* TextInsertService의 역방향: clipboard save → 복사 단축키 simulate → clipboard read → clipboard restore * TextInsertService의 역방향: clipboard save → 복사 단축키 simulate → clipboard read → clipboard restore
*/ */
private async _captureSelectedText(): Promise<string | null> { private async _captureSelectedText(): Promise<string | null> {
const nut = await this._ensureNut()
const port = electronClipboardPort const port = electronClipboardPort
// 1. 기존 클립보드 저장 + 온전히 되돌릴 수 있는지 판정 (삽입과 같은 정책) // 1. 기존 클립보드 저장 + 온전히 되돌릴 수 있는지 판정 (삽입과 같은 정책)
@ -250,10 +258,14 @@ export class ScreenContextService {
// 2. 클립보드 비우기 (이전 내용이 남아있으면 "선택 없음"을 감지할 수 없으므로) // 2. 클립보드 비우기 (이전 내용이 남아있으면 "선택 없음"을 감지할 수 없으므로)
port.clear() port.clear()
// 3. 복사 단축키 시뮬레이션 (macOS ⌘+C, 그 외 Ctrl+C) // 3. 복사 단축키 시뮬레이션 (Windows 프로덕션은 koffi SendInput, macOS/폴백은 nut-js)
const modifier = this._platform === 'darwin' ? nut.Key.LeftSuper : nut.Key.LeftControl const copiedByNative = this._sendCopyShortcut ? await this._sendCopyShortcut() : false
await nut.pressKey(modifier, nut.Key.C) if (!copiedByNative) {
await nut.releaseKey(modifier, nut.Key.C) const nut = await this._ensureNut()
const modifier = this._platform === 'darwin' ? nut.Key.LeftSuper : nut.Key.LeftControl
await nut.pressKey(modifier, nut.Key.C)
await nut.releaseKey(modifier, nut.Key.C)
}
// 4. 클립보드에 텍스트가 복사될 때까지 대기 // 4. 클립보드에 텍스트가 복사될 때까지 대기
await this._sleep(150) await this._sleep(150)

View file

@ -15,7 +15,6 @@ import {
COMPOSING_IDLE_MULTIPLIER, COMPOSING_IDLE_MULTIPLIER,
isAppScopedLearning, isAppScopedLearning,
SUGGESTION_DEFAULTS, SUGGESTION_DEFAULTS,
SUGGESTION_MAX_OUTPUT_CHARS,
buildLocalSuggestionCandidateEntries, buildLocalSuggestionCandidateEntries,
decideInFlightGeneration, decideInFlightGeneration,
decideSuggestion, decideSuggestion,
@ -28,7 +27,6 @@ import {
selectPhraseHints, selectPhraseHints,
shouldPersistSuggestionPrefix, shouldPersistSuggestionPrefix,
isTerminalApp, isTerminalApp,
extractTerminalPromptPrefix,
type AnchorKind, type AnchorKind,
type PhysicalRect, type PhysicalRect,
type SuggestionCandidate, type SuggestionCandidate,
@ -39,7 +37,7 @@ import {
type SuggestionState, type SuggestionState,
type SuggestionStep type SuggestionStep
} from '@d3ro/core/input-intelligence' } from '@d3ro/core/input-intelligence'
import { buildQueryTerms, scoreCandidatePersonalization } from '@d3ro/core/personal-graph' import { buildQueryTerms } from '@d3ro/core/personal-graph'
import { splitFirstWord } from '@d3ro/core/suggestion-text' import { splitFirstWord } from '@d3ro/core/suggestion-text'
import { predictPersonalContinuation } from '@d3ro/core/personal-ngram' import { predictPersonalContinuation } from '@d3ro/core/personal-ngram'
import { analyzeWritingContext } from '@d3ro/core/writing-context' import { analyzeWritingContext } from '@d3ro/core/writing-context'
@ -67,6 +65,17 @@ import {
type SuggestionModelRequest, type SuggestionModelRequest,
type SuggestionModelRequestInput type SuggestionModelRequestInput
} from './suggestion/SuggestionModelRequest' } from './suggestion/SuggestionModelRequest'
import {
applyCorrectionToContext,
buildDecisionInputSignature,
isDuplicateCandidateText,
normalizeSuggestionPolicyConfig,
rerankFilledCandidates,
resolveTerminalTypingContext,
sameCandidateText,
toSuggestionCorrectionFromPlan,
type SuggestionPolicyConfig
} from './suggestion/suggestion-policy-helpers'
import { LOCAL_LLM_KEEP_ALIVE, LOCAL_LLM_KEEP_ALIVE_MS } from './llm/model-residency' import { LOCAL_LLM_KEEP_ALIVE, LOCAL_LLM_KEEP_ALIVE_MS } from './llm/model-residency'
import { getForegroundWindowInfo } from '../utils/win32-foreground' import { getForegroundWindowInfo } from '../utils/win32-foreground'
@ -103,17 +112,6 @@ export interface SuggestionTypoProposer {
): Promise<{ originalText: string; insertText: string; corrections: Array<{ start: number; end: number; to: string }> } | null> ): Promise<{ originalText: string; insertText: string; corrections: Array<{ start: number; end: number; to: string }> } | null>
} }
/** 교정을 적용한 문맥 — 모델에는 고친 글을 보여 주고, 세션 비교는 원문으로 한다. */
function applyCorrectionToContext(context: TypingContext, correction: SuggestionCorrection): TypingContext {
const replaceTail = (text: string): string =>
text.endsWith(correction.from) ? text.slice(0, text.length - correction.from.length) + correction.to : text
return {
...context,
prefix: replaceTail(context.prefix),
documentBefore: context.documentBefore == null ? context.documentBefore : replaceTail(context.documentBefore)
}
}
/** 서비스 협력자 — 기본값은 getSuggestionService() 가 연결한다 (DIP). */ /** 서비스 협력자 — 기본값은 getSuggestionService() 가 연결한다 (DIP). */
export interface SuggestionServiceDeps { export interface SuggestionServiceDeps {
repository: SuggestionRepository repository: SuggestionRepository
@ -143,16 +141,7 @@ export function createDefaultSuggestionServiceDeps(): SuggestionServiceDeps {
} }
} }
/** 설정에서 읽은 정책 값 (SSOT 기본값과 상한 적용 후). */ type PolicyConfig = SuggestionPolicyConfig
interface PolicyConfig {
triggerDelayMs: number
minPrefixChars: number
maxRequestsPerMinute: number
dailyBudget: number
maxCandidates: number
maxChars: number
excludedApps: string[]
}
export interface SuggestionRequestResult { export interface SuggestionRequestResult {
ok: boolean ok: boolean
@ -233,25 +222,6 @@ interface FirstCandidatePublication extends SuggestionGenerationRequest {
timing: GenerationTiming timing: GenerationTiming
} }
/**
* 스냅샷 한 번(약 0.8초) 사이에 사람이 칠 수 있는 글자 수 상한. 빈 터미널 입력칸이 이보다 크게
* 한 번에 채워지면 타이핑이 아니다(Claude Code 의 회색 추천 문구).
*/
const TERMINAL_TYPING_STEP_CHARS = 8
/** 공백·끝 문장부호를 무시하고 같은 후보인가 */
function sameCandidateText(a: string, b: string): boolean {
const norm = (text: string) => text.replace(/\s+/gu, ' ').replace(/[.!?。!?…,]+$/u, '').trim()
return norm(a) === norm(b)
}
function commonPrefixLength(a: string, b: string): number {
const max = Math.min(a.length, b.length)
let index = 0
while (index < max && a[index] === b[index]) index += 1
return index
}
/** 이 길이를 넘는 모델 출력은 버린다 (스트리밍 폭주 방지) */ /** 이 길이를 넘는 모델 출력은 버린다 (스트리밍 폭주 방지) */
const MAX_RAW_OUTPUT_CHARS = 1200 const MAX_RAW_OUTPUT_CHARS = 1200
/** "제안 사항 없음" 을 보여 주는 시간 */ /** "제안 사항 없음" 을 보여 주는 시간 */
@ -995,38 +965,13 @@ export class SuggestionService extends EventEmitter {
* (끝에서 치거나 지움) 커서는 입력 줄 끝이다. 중간을 고친 모양이면 다음 입력까지 막는다. * (끝에서 치거나 지움) 커서는 입력 줄 끝이다. 중간을 고친 모양이면 다음 입력까지 막는다.
*/ */
private _terminalContext(input: TypingContext): TypingContext { private _terminalContext(input: TypingContext): TypingContext {
if (input.vimMode === 'normal' || input.vimMode === 'command') { const { context, nextState } = resolveTerminalTypingContext(input, {
this._lastTerminalPrompt = '' lastPrompt: this._lastTerminalPrompt,
this._terminalPromptTyped = false promptTyped: this._terminalPromptTyped
return { ...input, prefix: '', documentBefore: null, caretReliable: false } })
} this._lastTerminalPrompt = nextState.lastPrompt
const extracted = extractTerminalPromptPrefix(input.fullText ?? input.prefix) this._terminalPromptTyped = nextState.promptTyped
const prompt = extracted ?? (input.terminalMode ? input.prefix : null) return context
const previous = this._lastTerminalPrompt
this._lastTerminalPrompt = prompt
let typed: boolean
if (prompt === null) typed = false
else if (prompt.length === 0) typed = true
else if (previous === null) typed = prompt.length <= TERMINAL_TYPING_STEP_CHARS
else if (prompt === previous) typed = this._terminalPromptTyped
else if (previous.length === 0 && prompt.length > TERMINAL_TYPING_STEP_CHARS) {
// 빈 입력칸이 한 번에 긴 글로 바뀌었다 — Claude Code 의 회색 "추천 문구" 다. UIA 는 그것도
// 입력 글자로 준다(실측 2026-10-03: 빈 칸 → 28·29자, 그 뒤에 이어 쓴 ", ready for next test
// cycle." 이 사용자 메시지 앞에 삽입됐다). 사용자가 치기 시작하면 다음 판정에서 다시 믿는다.
typed = false
} else {
// 바뀐 곳이 끝부분뿐이면 끝에서 치는 중이다: 늘어남·줄어듦, 그리고 한글 조합처럼 마지막
// 한두 글자가 바뀌는 것("자ㄹ" → "자료"). 조합을 "중간 편집" 으로 보던 탓에 멈춘 뒤에도
// 막혀 있었다(실측: 30초 동안 치는데 내내 caret-unknown).
typed = commonPrefixLength(previous, prompt) >= Math.min(previous.length, prompt.length) - 2
}
this._terminalPromptTyped = typed
return {
...input,
prefix: prompt ?? '',
documentBefore: null,
caretReliable: extracted !== null ? typed : typed && input.caretReliable !== false
}
} }
/** 로컬 기억은 모델과 무관하다 — 같은 정책을 "모델 있음 · 표시 없음" 으로 평가한다. */ /** 로컬 기억은 모델과 무관하다 — 같은 정책을 "모델 있음 · 표시 없음" 으로 평가한다. */
@ -1604,13 +1549,7 @@ export class SuggestionService extends EventEmitter {
if (!this._typoProposer) return null if (!this._typoProposer) return null
try { try {
const plan = await this._typoProposer.propose(context, signal, { thorough }) const plan = await this._typoProposer.propose(context, signal, { thorough })
if (!plan || !context.prefix.endsWith(plan.originalText)) return null return toSuggestionCorrectionFromPlan(context.prefix, plan) ?? null
const base = plan.corrections[0]?.start ?? 0
return {
from: plan.originalText,
to: plan.insertText,
edits: plan.corrections.map((edit) => ({ start: edit.start - base, end: edit.end - base, to: edit.to }))
}
} catch (error) { } catch (error) {
logger.debug(`제안용 오타 교정 실패 — 교정 없이 제안한다: ${error instanceof Error ? error.message : String(error)}`) logger.debug(`제안용 오타 교정 실패 — 교정 없이 제안한다: ${error instanceof Error ? error.message : String(error)}`)
return null return null
@ -1720,30 +1659,13 @@ export class SuggestionService extends EventEmitter {
private _appendFilledCandidate(candidate: SuggestionCandidate, prefix: string, memory: LocalMemoryContext): void { private _appendFilledCandidate(candidate: SuggestionCandidate, prefix: string, memory: LocalMemoryContext): void {
this._candidates.push({ ...candidate, rank: this._candidates.length }) this._candidates.push({ ...candidate, rank: this._candidates.length })
if (this._activeIndex === 0 && this._candidates.length >= 2) { if (this._activeIndex === 0 && this._candidates.length >= 2) {
const personalContext = { this._candidates = rerankFilledCandidates(this._candidates, candidate.text, {
prefix, prefix,
continuationHints: memory.continuationHints, continuationHints: memory.continuationHints,
followUpHints: memory.followUpHints, followUpHints: memory.followUpHints,
relatedHints: memory.relatedHints, relatedHints: memory.relatedHints,
phraseHints: memory.phraseHints phraseHints: memory.phraseHints
} })
const newScore = scoreCandidatePersonalization(candidate.text, personalContext)
if (newScore >= 1.5) {
const personalItems = this._candidates.filter((item) => item.source === 'personal')
const modelItems = this._candidates
.filter((item) => item.source !== 'personal')
.map((item, idx) => ({
item,
idx,
score: scoreCandidatePersonalization(item.text, personalContext)
}))
.sort((a, b) => (Math.abs(b.score - a.score) >= 0.8 ? b.score - a.score : a.idx - b.idx))
.map((entry) => entry.item)
this._candidates = [...personalItems, ...modelItems].map((item, index) => ({
...item,
rank: index
}))
}
} }
logger.debug(`제안 후보 추가 ${this._candidates.length}/${SUGGESTION_DEFAULTS.maxCandidatesTotal}`) logger.debug(`제안 후보 추가 ${this._candidates.length}/${SUGGESTION_DEFAULTS.maxCandidatesTotal}`)
this._armVisibleTtl() this._armVisibleTtl()
@ -1791,15 +1713,7 @@ export class SuggestionService extends EventEmitter {
/** 정규화 후 완전 중복이거나 기존 후보의 접두/확장이면 중복으로 본다. */ /** 정규화 후 완전 중복이거나 기존 후보의 접두/확장이면 중복으로 본다. */
private _isDuplicateCandidate(candidate: string): boolean { private _isDuplicateCandidate(candidate: string): boolean {
const normalized = candidate.replace(/\s+/gu, ' ').trim().toLowerCase() return isDuplicateCandidateText(candidate, this._candidates)
return this._candidates.some((existing) => {
const existingNormalized = existing.text.replace(/\s+/gu, ' ').trim().toLowerCase()
return (
existingNormalized === normalized ||
existingNormalized.startsWith(normalized) ||
normalized.startsWith(existingNormalized)
)
})
} }
/** /**
@ -1833,17 +1747,9 @@ export class SuggestionService extends EventEmitter {
/** 판단 입력을 서명이 바뀔 때만 남긴다. */ /** 판단 입력을 서명이 바뀔 때만 남긴다. */
private _logDecisionInputs(context: TypingContext): void { private _logDecisionInputs(context: TypingContext): void {
const signature = [ const modelAvailable = getLocalLLMService().isAvailable()
context.available, const enabled = this.isEnabled()
context.isEditable, const signature = buildDecisionInputSignature(context, { modelAvailable, enabled })
context.isPassword,
context.isComposing,
context.anchor ? 'A' : '-',
context.editedSinceFocus,
context.typedRecently,
getLocalLLMService().isAvailable(),
this.isEnabled()
].join('|')
if (signature === this._lastDecisionSignature) return if (signature === this._lastDecisionSignature) return
this._lastDecisionSignature = signature this._lastDecisionSignature = signature
@ -1851,8 +1757,8 @@ export class SuggestionService extends EventEmitter {
`제안 판단 입력: avail=${context.available} edit=${context.isEditable} pw=${context.isPassword} ` + `제안 판단 입력: avail=${context.available} edit=${context.isEditable} pw=${context.isPassword} ` +
`comp=${context.isComposing} edited=${context.editedSinceFocus} typed=${context.typedRecently} ` + `comp=${context.isComposing} edited=${context.editedSinceFocus} typed=${context.typedRecently} ` +
`prefixLen=${context.prefix.length} idle=${context.idleMs}ms ` + `prefixLen=${context.prefix.length} idle=${context.idleMs}ms ` +
`app=${context.appName ?? '-'} enabled=${this.isEnabled()} ` + `app=${context.appName ?? '-'} enabled=${enabled} ` +
`model=${getLocalLLMService().isAvailable()} anchor=${context.anchor ? 'yes' : 'no'}` `model=${modelAvailable} anchor=${context.anchor ? 'yes' : 'no'}`
) )
} }
@ -2328,24 +2234,13 @@ export class SuggestionService extends EventEmitter {
// ── 내부 ────────────────────────────────────────────── // ── 내부 ──────────────────────────────────────────────
private readPolicyConfig(): PolicyConfig { private readPolicyConfig(): PolicyConfig {
return { return normalizeSuggestionPolicyConfig({
triggerDelayMs: Math.max( triggerDelayMs: configGet('suggestionTriggerDelayMs'),
SUGGESTION_DEFAULTS.minTriggerDelayMs, minPrefixChars: configGet('suggestionMinPrefixChars'),
configGet('suggestionTriggerDelayMs') || SUGGESTION_DEFAULTS.triggerDelayMs maxRequestsPerMinute: configGet('suggestionMaxRequestsPerMinute'),
), dailyBudget: configGet('suggestionDailyBudget'),
minPrefixChars: configGet('suggestionMinPrefixChars') || SUGGESTION_DEFAULTS.minPrefixChars, excludedApps: configGet('inputExcludedApps')
maxRequestsPerMinute: })
Math.min(
configGet('suggestionMaxRequestsPerMinute') || SUGGESTION_DEFAULTS.maxRequestsPerMinute,
30
),
dailyBudget: configGet('suggestionDailyBudget') || SUGGESTION_DEFAULTS.dailyBudget,
maxCandidates: SUGGESTION_DEFAULTS.maxCandidates,
maxChars: SUGGESTION_MAX_OUTPUT_CHARS,
// 터미널은 셸 프롬프트라 문장 제안이 의미 없다 — 사용자 목록과 무관하게 뺀다.
// 터미널도 제안한다(입력 줄만 추출). 학습 제외는 InputTelemetryService 가 따로 한다.
excludedApps: [...configGet('inputExcludedApps')]
}
} }
/** /**

View file

@ -1,8 +1,14 @@
// 기록과 회의를 스키마 변경 없이 함께 찾는 로컬 통합 검색. // 기록과 회의를 FTS5 trigram 역색인(3자 이상) + 정확한 부분 문자열 검증으로 함께 찾는 로컬 통합 검색.
import { EventEmitter } from 'events' import { EventEmitter } from 'events'
import { count, desc, inArray, or } from 'drizzle-orm' import { and, count, desc, inArray, or, sql } from 'drizzle-orm'
import { formatTranscriptTimestamp, parseTranscriptLines } from '@d3ro/core/meeting-transcript' import { formatTranscriptTimestamp, parseTranscriptLines } from '@d3ro/core/meeting-transcript'
import { isRecord } from '@d3ro/core/guards'
import {
createUnifiedSearchExcerpt,
includesSearchQuery,
mergeUnifiedSearchResults,
} from '@d3ro/core/unified-search'
import { D3ROError, ErrorCode } from '@d3ro/core/errors' import { D3ROError, ErrorCode } from '@d3ro/core/errors'
import type { import type {
UnifiedSearchParams, UnifiedSearchParams,
@ -21,18 +27,19 @@ import {
} from '../db/schema' } from '../db/schema'
import { getLogger } from './LoggerService' import { getLogger } from './LoggerService'
export { createUnifiedSearchExcerpt, mergeUnifiedSearchResults }
const logger = getLogger('UnifiedSearchService') const logger = getLogger('UnifiedSearchService')
const DEFAULT_LIMIT = 30 const DEFAULT_LIMIT = 30
const MAX_LIMIT = 50 const MAX_LIMIT = 50
const MAX_QUERY_LENGTH = 200 const MAX_QUERY_LENGTH = 200
const EXCERPT_LENGTH = 180
export function validateUnifiedSearchParams(value: unknown): UnifiedSearchParams { export function validateUnifiedSearchParams(value: unknown): UnifiedSearchParams {
if (typeof value !== 'object' || value === null || Array.isArray(value)) { if (!isRecord(value)) {
throw new D3ROError(ErrorCode.ConfigInvalidValue, 'Unified search parameters are required') throw new D3ROError(ErrorCode.ConfigInvalidValue, 'Unified search parameters are required')
} }
const candidate = value as Record<string, unknown> const candidate = value
if (typeof candidate.query !== 'string') { if (typeof candidate.query !== 'string') {
throw new D3ROError(ErrorCode.ConfigInvalidValue, 'Unified search query must be a string', { throw new D3ROError(ErrorCode.ConfigInvalidValue, 'Unified search query must be a string', {
field: 'query', field: 'query',
@ -56,32 +63,45 @@ export function normalizeUnifiedSearchQuery(value: string): string {
return value.slice(0, MAX_QUERY_LENGTH).trim().replace(/\s+/g, ' ') return value.slice(0, MAX_QUERY_LENGTH).trim().replace(/\s+/g, ' ')
} }
function includesQuery(value: string | null | undefined, query: string): boolean { /**
return value?.toLocaleLowerCase().includes(query.toLocaleLowerCase()) === true * SQLite FTS5 trigram 구문 리터럴 생성.
* trigram 토크나이저는 유니코드 코드포인트 3글자 이상부터 역색인을 탄다.
*/
function toFtsTrigramPhrase(query: string): string | null {
if (query.includes('\0') || Array.from(query).length < 3) return null
return `"${query.replaceAll('"', '""')}"`
} }
export function createUnifiedSearchExcerpt( interface SqliteMasterClient {
value: string | null | undefined, prepare?: (sql: string) => { all: (...args: unknown[]) => Array<{ name: string }> }
query: string, }
maxLength = EXCERPT_LENGTH,
): string {
const text = (value ?? '').replace(/\s+/g, ' ').trim()
if (text.length <= maxLength) return text
const matchAt = text.toLocaleLowerCase().indexOf(query.toLocaleLowerCase()) const REQUIRED_FTS_TABLES = [
if (matchAt < 0) return `${text.slice(0, maxLength - 1).trimEnd()}…` 'history_fts',
'meeting_sessions_fts',
'meeting_memos_fts',
] as const
const context = Math.max(20, Math.floor((maxLength - query.length) / 2)) function hasAllFtsTables(db: unknown): boolean {
const start = Math.max(0, matchAt - context) const client = (db as { $client?: SqliteMasterClient } | null | undefined)?.$client
const end = Math.min(text.length, start + maxLength) if (!client || typeof client.prepare !== 'function') return false
const body = text.slice(start, end).trim() try {
return `${start > 0 ? '…' : ''}${body}${end < text.length ? '…' : ''}` const rows = client
.prepare(
"SELECT name FROM sqlite_master WHERE type = 'table' AND name IN ('history_fts', 'meeting_sessions_fts', 'meeting_memos_fts')",
)
.all()
const present = new Set(rows.map((row) => row.name))
return REQUIRED_FTS_TABLES.every((name) => present.has(name))
} catch {
return false
}
} }
function recordResult(row: History, query: string): UnifiedSearchResult { function recordResult(row: History, query: string): UnifiedSearchResult {
const titleMatched = includesQuery(row.title, query) const titleMatched = includesSearchQuery(row.title, query)
const matchingContent = [row.polishedText, row.originalText, row.summaryText] const matchingContent = [row.polishedText, row.originalText, row.summaryText]
.find((value) => includesQuery(value, query)) .find((value) => includesSearchQuery(value, query))
const displayContent = matchingContent ?? row.polishedText ?? row.originalText ?? row.summaryText ?? row.title const displayContent = matchingContent ?? row.polishedText ?? row.originalText ?? row.summaryText ?? row.title
return { return {
@ -100,7 +120,7 @@ function findMatchingTranscriptSegmentSnippet(
): string | null { ): string | null {
const lines = parseTranscriptLines(transcript) const lines = parseTranscriptLines(transcript)
for (const line of lines) { for (const line of lines) {
if (includesQuery(line.text, query) || includesQuery(line.speaker, query)) { if (includesSearchQuery(line.text, query) || includesSearchQuery(line.speaker, query)) {
if (!line.timed) return line.text if (!line.timed) return line.text
const speakerPrefix = line.speaker ? `[${line.speaker}] ` : '' const speakerPrefix = line.speaker ? `[${line.speaker}] ` : ''
return `[${formatTranscriptTimestamp(line.timestampMs)}] ${speakerPrefix}${line.text}` return `[${formatTranscriptTimestamp(line.timestampMs)}] ${speakerPrefix}${line.text}`
@ -118,9 +138,9 @@ function meetingResult(
query: string, query: string,
memoMatch?: MeetingMemoRow, memoMatch?: MeetingMemoRow,
): UnifiedSearchResult { ): UnifiedSearchResult {
const titleMatched = includesQuery(row.title, query) const titleMatched = includesSearchQuery(row.title, query)
const matchingTranscript = [row.editedTranscript, row.rawTranscript] const matchingTranscript = [row.editedTranscript, row.rawTranscript]
.find((value) => includesQuery(value, query)) .find((value) => includesSearchQuery(value, query))
const segmentSnippet = findMatchingTranscriptSegmentSnippet( const segmentSnippet = findMatchingTranscriptSegmentSnippet(
matchingTranscript ?? row.editedTranscript ?? row.rawTranscript, matchingTranscript ?? row.editedTranscript ?? row.rawTranscript,
query, query,
@ -151,14 +171,78 @@ function meetingResult(
} }
} }
export function mergeUnifiedSearchResults( interface SearchConditions {
records: readonly UnifiedSearchResult[], recordConditions: ReturnType<typeof or>
meetings: readonly UnifiedSearchResult[], meetingConditions: ReturnType<typeof or>
memoBaseCondition: ReturnType<ReturnType<typeof containsPattern>['match']>
}
interface SearchQueryRows {
recordTotal: number
recordRows: History[]
meetingTotal: number
meetingRows: MeetingSessionRow[]
memoRows: MeetingMemoRow[]
}
function executeUnifiedSearchQueries(
db: ReturnType<typeof getDatabase>,
query: string,
limit: number, limit: number,
): UnifiedSearchResult[] { conditions: SearchConditions,
return [...records, ...meetings] useFts: boolean,
.sort((left, right) => right.occurredAt - left.occurredAt || left.id.localeCompare(right.id)) ): SearchQueryRows {
.slice(0, limit) const ftsPhrase = useFts ? toFtsTrigramPhrase(query) : null
const recordWhere = ftsPhrase
? and(
sql`${history.id} IN (SELECT row_id FROM history_fts WHERE history_fts MATCH ${ftsPhrase})`,
conditions.recordConditions,
)
: conditions.recordConditions
const meetingWhere = ftsPhrase
? and(
sql`${meetingSessions.id} IN (SELECT row_id FROM meeting_sessions_fts WHERE meeting_sessions_fts MATCH ${ftsPhrase})`,
conditions.meetingConditions,
)
: conditions.meetingConditions
const memoWhere = ftsPhrase
? and(
sql`${meetingMemos.id} IN (SELECT row_id FROM meeting_memos_fts WHERE meeting_memos_fts MATCH ${ftsPhrase})`,
conditions.memoBaseCondition,
)
: conditions.memoBaseCondition
const memoSessionMatch = sql`${meetingSessions.id} IN (SELECT ${meetingMemos.sessionId} FROM meeting_memos WHERE ${memoWhere})`
const anyMeetingWhere = or(meetingWhere, memoSessionMatch)
const recordTotal = db.select({ count: count() }).from(history).where(recordWhere).get()?.count ?? 0
const recordRows = db
.select()
.from(history)
.where(recordWhere)
.orderBy(desc(history.createdAt))
.limit(limit)
.all()
const meetingTotal = db.select({ count: count() }).from(meetingSessions).where(anyMeetingWhere).get()?.count ?? 0
const meetingRows = db
.select()
.from(meetingSessions)
.where(anyMeetingWhere)
.orderBy(desc(meetingSessions.startedAt))
.limit(limit)
.all()
const meetingIds = meetingRows.map((row) => row.id)
const memoRows = meetingIds.length > 0
? db
.select()
.from(meetingMemos)
.where(and(inArray(meetingMemos.sessionId, meetingIds), memoWhere))
.orderBy(desc(meetingMemos.createdAt))
.all()
: []
return { recordTotal, recordRows, meetingTotal, meetingRows, memoRows }
} }
class UnifiedSearchService extends EventEmitter { class UnifiedSearchService extends EventEmitter {
@ -171,73 +255,51 @@ class UnifiedSearchService extends EventEmitter {
try { try {
const db = getDatabase() const db = getDatabase()
const pattern = containsPattern(query) const pattern = containsPattern(query)
const recordConditions = or( const conditions: SearchConditions = {
pattern.match(history.title), recordConditions: or(
pattern.match(history.originalText), pattern.match(history.title),
pattern.match(history.polishedText), pattern.match(history.originalText),
pattern.match(history.summaryText), pattern.match(history.polishedText),
) pattern.match(history.summaryText),
const meetingConditions = or( ),
pattern.match(meetingSessions.title), meetingConditions: or(
pattern.match(meetingSessions.editedTranscript), pattern.match(meetingSessions.title),
pattern.match(meetingSessions.rawTranscript), pattern.match(meetingSessions.editedTranscript),
) pattern.match(meetingSessions.rawTranscript),
),
memoBaseCondition: pattern.match(meetingMemos.content),
}
const recordTotal = db.select({ count: count() }).from(history).where(recordConditions).get()?.count ?? 0 const canTryFts = hasAllFtsTables(db) && toFtsTrigramPhrase(query) !== null
const recordRows = db let queryRows: SearchQueryRows
.select() if (canTryFts) {
.from(history) try {
.where(recordConditions) queryRows = executeUnifiedSearchQueries(db, query, limit, conditions, true)
.orderBy(desc(history.createdAt)) } catch (ftsError) {
.limit(limit) const message = ftsError instanceof Error ? ftsError.message : String(ftsError)
.all() logger.warn(`Unified search FTS5 query failed, falling back to LIKE: ${message}`)
const meetingTotal = db.select({ count: count() }).from(meetingSessions).where(meetingConditions).get()?.count ?? 0 queryRows = executeUnifiedSearchQueries(db, query, limit, conditions, false)
const meetingRows = db }
.select() } else {
.from(meetingSessions) queryRows = executeUnifiedSearchQueries(db, query, limit, conditions, false)
.where(meetingConditions) }
.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<string, MeetingMemoRow>() const memoBySessionId = new Map<string, MeetingMemoRow>()
for (const memo of memoRows) { for (const memo of queryRows.memoRows) {
if (!memoBySessionId.has(memo.sessionId)) { if (!memoBySessionId.has(memo.sessionId)) {
memoBySessionId.set(memo.sessionId, memo) 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( const results = mergeUnifiedSearchResults(
recordRows.map((row) => recordResult(row, query)), queryRows.recordRows.map((row) => recordResult(row, query)),
combinedMeetingRows.map((row) => meetingResult(row, query, memoBySessionId.get(row.id))), queryRows.meetingRows.map((row) => meetingResult(row, query, memoBySessionId.get(row.id))),
limit, limit,
) )
return { return {
query, query,
results, results,
total: recordTotal + meetingTotal + memoMatchedMeetings.length, total: queryRows.recordTotal + queryRows.meetingTotal,
} }
} catch (error) { } catch (error) {
const message = error instanceof Error ? error.message : String(error) const message = error instanceof Error ? error.message : String(error)

View file

@ -31,6 +31,7 @@ import { getTranscriptGlossaryService } from './TranscriptGlossaryService'
import { buildInstructionInvocation, resolveTargetLanguage } from './llm-prompts' import { buildInstructionInvocation, resolveTargetLanguage } from './llm-prompts'
import { D3ROError, ErrorCode } from '@d3ro/core/errors' import { D3ROError, ErrorCode } from '@d3ro/core/errors'
import { cleanDictatedText } from '@d3ro/core/dictation-cleaner' import { cleanDictatedText } from '@d3ro/core/dictation-cleaner'
import { secureWipeAudioChunks, secureZeroBuffer } from '@d3ro/core/utils/secure-memory'
import { composeInstructionPipelinePrompt, parseActiveInstructionIds } from '@d3ro/core/instruction-template' import { composeInstructionPipelinePrompt, parseActiveInstructionIds } from '@d3ro/core/instruction-template'
import { TIMING } from '@d3ro/core/constants' import { TIMING } from '@d3ro/core/constants'
import { RecognitionState, AudioState } from '@d3ro/core/types' import { RecognitionState, AudioState } from '@d3ro/core/types'
@ -887,7 +888,7 @@ class VoiceModeService extends EventEmitter {
const merged = Buffer.concat(run.audioBuffer) const merged = Buffer.concat(run.audioBuffer)
run.lastAudio = merged // WAV 저장용 (run 소유) run.lastAudio = merged // WAV 저장용 (run 소유)
run.audioBuffer = [] secureWipeAudioChunks(run.audioBuffer)
run.audioBufferBytes = 0 run.audioBufferBytes = 0
// Bug 13: 너무 짧은 오디오는 STT 호출 전에 에러 피드백. // Bug 13: 너무 짧은 오디오는 STT 호출 전에 에러 피드백.
@ -1247,6 +1248,9 @@ class VoiceModeService extends EventEmitter {
this._clearSttWaitTimer(run) this._clearSttWaitTimer(run)
this._stopPartialLoop(run) this._stopPartialLoop(run)
run.partialInFlight = null run.partialInFlight = null
secureWipeAudioChunks(run.audioBuffer)
secureZeroBuffer(run.lastAudio)
run.lastAudio = null
void this._releaseAudio(run) void this._releaseAudio(run)
if (this._run === run) this._resetToIdle() if (this._run === run) this._resetToIdle()
} }

View file

@ -11,16 +11,8 @@ import type {
AdRevenueStats, AdRevenueStats,
} from '@d3ro/core/types' } from '@d3ro/core/types'
import type { IAdNetworkAdapter } from './BaseAdAdapter' import type { IAdNetworkAdapter } from './BaseAdAdapter'
import { EthicalAdsAdapter } from './EthicalAdsAdapter'
import { CarbonAdsAdapter } from './CarbonAdsAdapter'
import { PlaywireAdapter } from './PlaywireAdapter'
import { UnityAdsAdapter } from './UnityAdsAdapter'
import { AppLovinAdapter } from './AppLovinAdapter'
import { GoogleAdManagerAdapter } from './GoogleAdManagerAdapter'
import { InMobiAdapter } from './InMobiAdapter'
import { PubMaticAdapter } from './PubMaticAdapter'
import { MintegralAdapter } from './MintegralAdapter'
import { DirectHouseSponsorAdapter } from './DirectHouseSponsorAdapter' import { DirectHouseSponsorAdapter } from './DirectHouseSponsorAdapter'
import { createUnavailableAdAdapters } from './UnavailableAdAdapter'
import { getAdSettlementService } from './AdSettlementService' import { getAdSettlementService } from './AdSettlementService'
import { getLicenseService } from '../LicenseService' import { getLicenseService } from '../LicenseService'
@ -40,15 +32,7 @@ export class AdMediationEngine {
// integrated. No adapter may fabricate a bid or creative. // integrated. No adapter may fabricate a bid or creative.
const adapterList: IAdNetworkAdapter[] = [ const adapterList: IAdNetworkAdapter[] = [
new DirectHouseSponsorAdapter(), new DirectHouseSponsorAdapter(),
new PlaywireAdapter(), ...createUnavailableAdAdapters(),
new EthicalAdsAdapter(),
new CarbonAdsAdapter(),
new UnityAdsAdapter(),
new AppLovinAdapter(),
new GoogleAdManagerAdapter(),
new InMobiAdapter(),
new PubMaticAdapter(),
new MintegralAdapter(),
] ]
for (const adapter of adapterList) { for (const adapter of adapterList) {
@ -152,12 +136,15 @@ export class AdMediationEngine {
const bidPromises = enabledAdapters.map(async (adapter) => { const bidPromises = enabledAdapters.map(async (adapter) => {
const adapterStart = Date.now() const adapterStart = Date.now()
try { try {
let bidTimer: ReturnType<typeof setTimeout> | undefined
const bidResult = await Promise.race([ const bidResult = await Promise.race([
adapter.requestBid(request), adapter.requestBid(request),
new Promise<never>((_, reject) => new Promise<never>((_, reject) => {
setTimeout(() => reject(new Error('Bid Timeout')), timeoutMs) bidTimer = setTimeout(() => reject(new Error('Bid Timeout')), timeoutMs)
), }),
]) ]).finally(() => {
if (bidTimer !== undefined) clearTimeout(bidTimer)
})
if ( if (
bidResult.hasBid bidResult.hasBid

View file

@ -1,12 +0,0 @@
import { UnavailableAdAdapter } from './UnavailableAdAdapter'
export class AppLovinAdapter extends UnavailableAdAdapter {
constructor() {
super(
'applovin_max',
'AppLovin MAX',
['rewarded_video', 'banner_dock', 'export_sponsor'],
5.5,
)
}
}

View file

@ -1,7 +0,0 @@
import { UnavailableAdAdapter } from './UnavailableAdAdapter'
export class CarbonAdsAdapter extends UnavailableAdAdapter {
constructor() {
super('carbon_ads', 'Carbon Ads', ['banner_dock'], 3.5)
}
}

View file

@ -1,7 +0,0 @@
import { UnavailableAdAdapter } from './UnavailableAdAdapter'
export class EthicalAdsAdapter extends UnavailableAdAdapter {
constructor() {
super('ethical_ads', 'EthicalAds', ['banner_dock', 'export_sponsor'], 3.2)
}
}

View file

@ -1,12 +0,0 @@
import { UnavailableAdAdapter } from './UnavailableAdAdapter'
export class GoogleAdManagerAdapter extends UnavailableAdAdapter {
constructor() {
super(
'google_ad_manager',
'Google Ad Manager',
['banner_dock', 'rewarded_video', 'export_sponsor'],
2,
)
}
}

View file

@ -1,7 +0,0 @@
import { UnavailableAdAdapter } from './UnavailableAdAdapter'
export class InMobiAdapter extends UnavailableAdAdapter {
constructor() {
super('inmobi', 'InMobi', ['banner_dock', 'rewarded_video'], 2.8)
}
}

View file

@ -1,7 +0,0 @@
import { UnavailableAdAdapter } from './UnavailableAdAdapter'
export class MintegralAdapter extends UnavailableAdAdapter {
constructor() {
super('mintegral', 'Mintegral', ['rewarded_video', 'banner_dock'], 4)
}
}

View file

@ -1,12 +0,0 @@
import { UnavailableAdAdapter } from './UnavailableAdAdapter'
export class PlaywireAdapter extends UnavailableAdAdapter {
constructor() {
super(
'playwire',
'Playwire RAMP',
['banner_dock', 'rewarded_video', 'export_sponsor'],
4.5,
)
}
}

View file

@ -1,7 +0,0 @@
import { UnavailableAdAdapter } from './UnavailableAdAdapter'
export class PubMaticAdapter extends UnavailableAdAdapter {
constructor() {
super('pubmatic', 'PubMatic OpenWrap', ['banner_dock', 'export_sponsor'], 3)
}
}

View file

@ -1,6 +1,70 @@
import type { AdFormat, AdMediationAuctionRequest, AdNetworkId } from '@d3ro/core/types' import type { AdFormat, AdMediationAuctionRequest, AdNetworkId } from '@d3ro/core/types'
import type { AdBidResponse, IAdNetworkAdapter } from './BaseAdAdapter' import type { AdBidResponse, IAdNetworkAdapter } from './BaseAdAdapter'
export interface UnavailableAdNetworkSpec {
readonly networkId: AdNetworkId
readonly networkName: string
readonly supportedFormats: AdFormat[]
readonly defaultFloorEcpm: number
}
export const UNAVAILABLE_AD_NETWORK_SPECS: readonly UnavailableAdNetworkSpec[] = [
{
networkId: 'playwire',
networkName: 'Playwire RAMP',
supportedFormats: ['banner_dock', 'rewarded_video', 'export_sponsor'],
defaultFloorEcpm: 4.5,
},
{
networkId: 'ethical_ads',
networkName: 'EthicalAds',
supportedFormats: ['banner_dock', 'export_sponsor'],
defaultFloorEcpm: 3.2,
},
{
networkId: 'carbon_ads',
networkName: 'Carbon Ads',
supportedFormats: ['banner_dock'],
defaultFloorEcpm: 3.5,
},
{
networkId: 'unity_ads',
networkName: 'Unity LevelPlay',
supportedFormats: ['rewarded_video', 'banner_dock'],
defaultFloorEcpm: 6,
},
{
networkId: 'applovin_max',
networkName: 'AppLovin MAX',
supportedFormats: ['rewarded_video', 'banner_dock', 'export_sponsor'],
defaultFloorEcpm: 5.5,
},
{
networkId: 'google_ad_manager',
networkName: 'Google Ad Manager',
supportedFormats: ['banner_dock', 'rewarded_video', 'export_sponsor'],
defaultFloorEcpm: 2,
},
{
networkId: 'inmobi',
networkName: 'InMobi',
supportedFormats: ['banner_dock', 'rewarded_video'],
defaultFloorEcpm: 2.8,
},
{
networkId: 'pubmatic',
networkName: 'PubMatic OpenWrap',
supportedFormats: ['banner_dock', 'export_sponsor'],
defaultFloorEcpm: 3,
},
{
networkId: 'mintegral',
networkName: 'Mintegral',
supportedFormats: ['rewarded_video', 'banner_dock'],
defaultFloorEcpm: 4,
},
]
/** /**
* Fail-closed boundary for providers whose official desktop SDK or * Fail-closed boundary for providers whose official desktop SDK or
* authenticated decision endpoint is not integrated. Demo creatives are not * authenticated decision endpoint is not integrated. Demo creatives are not
@ -35,3 +99,15 @@ export class UnavailableAdAdapter implements IAdNetworkAdapter {
return { success: false, tokenReward: 0 } return { success: false, tokenReward: 0 }
} }
} }
export function createUnavailableAdAdapters(): UnavailableAdAdapter[] {
return UNAVAILABLE_AD_NETWORK_SPECS.map(
(spec) =>
new UnavailableAdAdapter(
spec.networkId,
spec.networkName,
[...spec.supportedFormats],
spec.defaultFloorEcpm,
),
)
}

View file

@ -1,7 +0,0 @@
import { UnavailableAdAdapter } from './UnavailableAdAdapter'
export class UnityAdsAdapter extends UnavailableAdAdapter {
constructor() {
super('unity_ads', 'Unity LevelPlay', ['rewarded_video', 'banner_dock'], 6)
}
}

View file

@ -2,6 +2,8 @@
// 사전 내보내기/가져오기 파일 형식(CSV·JSON) — 순수 함수만 둔다 (fs/dialog/DB/동기화 의존 없음). // 사전 내보내기/가져오기 파일 형식(CSV·JSON) — 순수 함수만 둔다 (fs/dialog/DB/동기화 의존 없음).
// DictionaryService 는 파일 IO·DB 쓰기·동기화 push 만 맡고, 형식 변환과 행 검증은 여기서 한다. // DictionaryService 는 파일 IO·DB 쓰기·동기화 push 만 맡고, 형식 변환과 행 검증은 여기서 한다.
import { csvCell, parseCsvRows } from '@d3ro/core/csv'
import { isRecord } from '@d3ro/core/guards'
import { validateDictionaryDraft, type DictionaryWriteProblem } from '@d3ro/core/dictionary-policy' import { validateDictionaryDraft, type DictionaryWriteProblem } from '@d3ro/core/dictionary-policy'
import type { DictionaryEntry } from '@d3ro/core/types' import type { DictionaryEntry } from '@d3ro/core/types'
@ -16,56 +18,6 @@ export const DICTIONARY_CSV_HEADER = [
'updatedAt' 'updatedAt'
] as const ] as const
function csvCell(value: unknown): string {
const text = value === null || value === undefined ? '' : String(value)
const escaped = text.replace(/"/g, '""')
const needsQuotes = /[",\r\n]/.test(escaped) || /^[=+\-@]/.test(escaped)
return needsQuotes ? `"${escaped}"` : escaped
}
function parseCsvRows(input: string): string[][] {
const rows: string[][] = []
let row: string[] = []
let field = ''
let inQuotes = false
for (let i = 0; i < input.length; i += 1) {
const char = input[i]
if (inQuotes) {
if (char === '"') {
if (input[i + 1] === '"') {
field += '"'
i += 1
} else {
inQuotes = false
}
} else {
field += char
}
continue
}
if (char === '"') {
inQuotes = true
} else if (char === ',') {
row.push(field)
field = ''
} else if (char === '\n') {
row.push(field)
rows.push(row)
row = []
field = ''
} else if (char === '\r') {
// skip CR; LF terminates the row
} else {
field += char
}
}
if (field.length > 0 || row.length > 0) {
row.push(field)
rows.push(row)
}
return rows.filter((candidate) => candidate.some((cell) => cell.trim().length > 0))
}
function pickString(source: Record<string, unknown>, keys: string[]): string | null { function pickString(source: Record<string, unknown>, keys: string[]): string | null {
for (const key of keys) { for (const key of keys) {
const value = source[key] const value = source[key]
@ -134,9 +86,7 @@ function parseJson(raw: string): Array<Record<string, unknown>> {
} else { } else {
throw new Error('expected an array or an object with an "entries" array') throw new Error('expected an array or an object with an "entries" array')
} }
return (list as unknown[]).filter( return (list as unknown[]).filter(isRecord)
(item): item is Record<string, unknown> => !!item && typeof item === 'object'
)
} }
function parseCsv(raw: string): Array<Record<string, unknown>> { function parseCsv(raw: string): Array<Record<string, unknown>> {

View file

@ -11,6 +11,7 @@
// Electron·DB 를 모른다. 자막/오디오는 포트로 받는다(테스트는 가짜 포트). // Electron·DB 를 모른다. 자막/오디오는 포트로 받는다(테스트는 가짜 포트).
import type { CaptionSegment, MeetingMemo } from '@d3ro/core/types' import type { CaptionSegment, MeetingMemo } from '@d3ro/core/types'
import type { RecordingAudioSinkPort } from './disk-wav-audio-sink'
/** 자막 엔진(CaptionService)에 대한 최소 계약 */ /** 자막 엔진(CaptionService)에 대한 최소 계약 */
export interface RecordingCaptionPort { export interface RecordingCaptionPort {
@ -30,6 +31,8 @@ export interface RecordingAudioPort {
export interface MeetingRecordingSessionPorts { export interface MeetingRecordingSessionPorts {
caption: RecordingCaptionPort caption: RecordingCaptionPort
audio: RecordingAudioPort audio: RecordingAudioPort
/** 디스크 스트리밍 싱크 — 주입 시 PCM 을 메모리에 쌓지 않고 임시 파일로 곧장 기록한다 */
audioSink?: RecordingAudioSinkPort
now?: () => number now?: () => number
/** 세그먼트가 이 녹음에 추가될 때(구독으로 받은 것만) */ /** 세그먼트가 이 녹음에 추가될 때(구독으로 받은 것만) */
onSegment?: (segment: CaptionSegment) => void onSegment?: (segment: CaptionSegment) => void
@ -37,6 +40,8 @@ export interface MeetingRecordingSessionPorts {
onSegmentUpdated?: (update: { id: string; text: string }) => void onSegmentUpdated?: (update: { id: string; text: string }) => void
/** 자막 정지 실패 — 정지는 계속 진행된다(스냅샷은 그때까지의 세그먼트로 만든다) */ /** 자막 정지 실패 — 정지는 계속 진행된다(스냅샷은 그때까지의 세그먼트로 만든다) */
onStopError?: (error: unknown) => void onStopError?: (error: unknown) => void
/** 디스크 스트리밍 쓰기/마무리 오류 — 전사 저장을 막지 않고 경고만 남긴다 */
onAudioSinkError?: (error: unknown) => void
} }
/** 정지 시점의 불변 스냅샷 — 후처리·저장은 이것만 읽는다 */ /** 정지 시점의 불변 스냅샷 — 후처리·저장은 이것만 읽는다 */
@ -47,8 +52,12 @@ export interface RecordingSnapshot {
readonly durationMs: number readonly durationMs: number
readonly segments: readonly CaptionSegment[] readonly segments: readonly CaptionSegment[]
readonly memos: readonly MeetingMemo[] readonly memos: readonly MeetingMemo[]
/** 녹음 PCM (없으면 길이 0) */ /** 녹음 PCM (디스크 싱크 사용 시 길이 0) */
readonly pcm: Buffer readonly pcm: Buffer
/** 디스크 싱크가 최종 WAV 파일로 저장을 마쳤는지 */
readonly audioSavedToDisk: boolean
/** 총 녹음 PCM 바이트 길이 (메모리 또는 디스크 싱크 기준) */
readonly pcmByteLength: number
} }
export class MeetingRecordingSession { export class MeetingRecordingSession {
@ -56,6 +65,7 @@ export class MeetingRecordingSession {
private _segments: CaptionSegment[] = [] private _segments: CaptionSegment[] = []
private _memos: MeetingMemo[] = [] private _memos: MeetingMemo[] = []
private _audio: Buffer[] = [] private _audio: Buffer[] = []
private _audioSinkBroken = false
private _lastRms = 0 private _lastRms = 0
private _unsubscribeSegment: (() => void) | null = null private _unsubscribeSegment: (() => void) | null = null
private _unsubscribeSegmentUpdated: (() => void) | null = null private _unsubscribeSegmentUpdated: (() => void) | null = null
@ -93,7 +103,18 @@ export class MeetingRecordingSession {
if (this._unsubscribeAudio || this._stopPromise) return if (this._unsubscribeAudio || this._stopPromise) return
this._unsubscribeAudio = this._ports.audio.onChunk((buffer) => { this._unsubscribeAudio = this._ports.audio.onChunk((buffer) => {
this._lastRms = this._measureLevel(buffer) this._lastRms = this._measureLevel(buffer)
this._audio.push(Buffer.from(buffer)) if (this._ports.audioSink) {
if (this._audioSinkBroken) return
try {
this._ports.audioSink.append(buffer)
} catch (err) {
this._audioSinkBroken = true
this._ports.audioSink.discard()
this._ports.onAudioSinkError?.(err)
}
} else {
this._audio.push(Buffer.from(buffer))
}
}) })
} }
@ -105,6 +126,9 @@ export class MeetingRecordingSession {
this._unsubscribeSegmentUpdated = null this._unsubscribeSegmentUpdated = null
this._unsubscribeAudio?.() this._unsubscribeAudio?.()
this._unsubscribeAudio = null this._unsubscribeAudio = null
if (!this._stopPromise) {
this._ports.audioSink?.discard()
}
} }
get isStopping(): boolean { get isStopping(): boolean {
@ -188,6 +212,31 @@ export class MeetingRecordingSession {
} }
private _freeze(endedAt: number, includeAudio = true): RecordingSnapshot { private _freeze(endedAt: number, includeAudio = true): RecordingSnapshot {
let audioSavedToDisk = false
let pcmByteLength = 0
let pcm = Buffer.alloc(0)
if (this._ports.audioSink) {
if (includeAudio) {
// shutdown() 처럼 stop() 없이 최종 오디오를 마무리할 때도 추가 쓰기를 먼저 끊는다.
this._unsubscribeAudio?.()
this._unsubscribeAudio = null
try {
const finalized = this._ports.audioSink.finalize()
audioSavedToDisk = finalized.saved
pcmByteLength = finalized.pcmByteLength
} catch (err) {
this._ports.audioSink.discard()
this._ports.onAudioSinkError?.(err)
}
} else {
pcmByteLength = this._ports.audioSink.pcmByteLength
}
} else if (includeAudio && this._audio.length > 0) {
pcm = Buffer.concat(this._audio)
pcmByteLength = pcm.length
}
return Object.freeze({ return Object.freeze({
id: this.id, id: this.id,
startedAt: this.startedAt, startedAt: this.startedAt,
@ -195,7 +244,9 @@ export class MeetingRecordingSession {
durationMs: Math.max(0, endedAt - this.startedAt), durationMs: Math.max(0, endedAt - this.startedAt),
segments: Object.freeze(this._segments.map((segment) => ({ ...segment }))), segments: Object.freeze(this._segments.map((segment) => ({ ...segment }))),
memos: Object.freeze(this._memos.map((memo) => ({ ...memo }))), memos: Object.freeze(this._memos.map((memo) => ({ ...memo }))),
pcm: includeAudio && this._audio.length > 0 ? Buffer.concat(this._audio) : Buffer.alloc(0), pcm,
audioSavedToDisk,
pcmByteLength,
}) })
} }
} }

View file

@ -0,0 +1,131 @@
// src/main/services/meeting/disk-wav-audio-sink.ts
// GAP-PERF-01 잔여: 장시간 회의 녹음 시 PCM 청크를 메모리에 누적하지 않고
// 임시 `.wav.part` 디스크 파일로 즉시 스트리밍한 뒤 종료 시 44B RIFF/WAVE 헤더를 기록한다.
import fs from 'fs'
import path from 'path'
export interface RecordingAudioSinkResult {
readonly saved: boolean
readonly pcmByteLength: number
}
export interface RecordingAudioSinkPort {
readonly pcmByteLength: number
append(chunk: Buffer): void
finalize(): RecordingAudioSinkResult
discard(): void
}
const WAV_HEADER_BYTES = 44
const SAMPLE_RATE = 16000
const NUM_CHANNELS = 1
const BITS_PER_SAMPLE = 16
export const MAX_WAV_DATA_BYTES = 0xffffffff - 36
/** 16kHz mono 16-bit PCM WAV 헤더(44바이트)를 채운다. */
export function writeWavHeader16kMonoPcm16(header: Buffer, dataSize: number): Buffer {
if (!Number.isSafeInteger(dataSize) || dataSize < 0 || dataSize > MAX_WAV_DATA_BYTES) {
throw new RangeError(
`WAV data size exceeds uint32 RIFF limit (0..${MAX_WAV_DATA_BYTES}): ${dataSize}`,
)
}
const byteRate = SAMPLE_RATE * NUM_CHANNELS * (BITS_PER_SAMPLE / 8)
const blockAlign = NUM_CHANNELS * (BITS_PER_SAMPLE / 8)
header.write('RIFF', 0)
header.writeUInt32LE(36 + dataSize, 4)
header.write('WAVE', 8)
header.write('fmt ', 12)
header.writeUInt32LE(16, 16)
header.writeUInt16LE(1, 20)
header.writeUInt16LE(NUM_CHANNELS, 22)
header.writeUInt32LE(SAMPLE_RATE, 24)
header.writeUInt32LE(byteRate, 28)
header.writeUInt16LE(blockAlign, 32)
header.writeUInt16LE(BITS_PER_SAMPLE, 34)
header.write('data', 36)
header.writeUInt32LE(dataSize, 40)
return header
}
export class DiskWavAudioSink implements RecordingAudioSinkPort {
readonly tempPartPath: string
private _fd: number | null = null
private _pcmByteLength = 0
private _finalized: RecordingAudioSinkResult | null = null
private _discarded = false
constructor(readonly targetWavPath: string) {
this.tempPartPath = `${targetWavPath}.part`
}
get pcmByteLength(): number {
return this._pcmByteLength
}
append(chunk: Buffer): void {
if (this._discarded || this._finalized !== null || chunk.length === 0) return
const nextByteLength = this._pcmByteLength + chunk.length
if (nextByteLength > MAX_WAV_DATA_BYTES) {
throw new RangeError(
`WAV data size exceeds uint32 RIFF limit (${nextByteLength} > ${MAX_WAV_DATA_BYTES})`,
)
}
if (this._fd === null) {
fs.mkdirSync(path.dirname(this.targetWavPath), { recursive: true })
this._fd = fs.openSync(this.tempPartPath, 'w+')
try {
fs.writeSync(this._fd, Buffer.alloc(WAV_HEADER_BYTES), 0, WAV_HEADER_BYTES, 0)
} catch (err) {
this._closeAndRemovePart()
throw err
}
}
fs.writeSync(this._fd, chunk, 0, chunk.length, WAV_HEADER_BYTES + this._pcmByteLength)
this._pcmByteLength = nextByteLength
}
finalize(): RecordingAudioSinkResult {
if (this._finalized !== null) return this._finalized
if (this._discarded || this._fd === null || this._pcmByteLength === 0) {
this._closeAndRemovePart()
this._finalized = Object.freeze({ saved: false, pcmByteLength: 0 })
return this._finalized
}
const header = writeWavHeader16kMonoPcm16(Buffer.alloc(WAV_HEADER_BYTES), this._pcmByteLength)
try {
fs.writeSync(this._fd, header, 0, WAV_HEADER_BYTES, 0)
} finally {
fs.closeSync(this._fd)
this._fd = null
}
fs.rmSync(this.targetWavPath, { force: true })
fs.renameSync(this.tempPartPath, this.targetWavPath)
this._finalized = Object.freeze({ saved: true, pcmByteLength: this._pcmByteLength })
return this._finalized
}
discard(): void {
if (this._discarded || this._finalized !== null) return
this._discarded = true
this._closeAndRemovePart()
}
private _closeAndRemovePart(): void {
if (this._fd !== null) {
try {
fs.closeSync(this._fd)
} catch {
// 이미 닫혔거나 디스크 핸들 정리 실패는 무시
}
this._fd = null
}
try {
fs.rmSync(this.tempPartPath, { force: true })
} catch {
// 임시 파일 제거 실패는 무시
}
}
}

View file

@ -1,11 +1,15 @@
// src/main/services/rag/chunk-store.ts // src/main/services/rag/chunk-store.ts
// 지식 문서·청크 저장소 포트와 SQLite(drizzle) 구현. RAGService는 getDatabase()를 직접 부르지 않는다. // 지식 문서·청크 저장소 포트와 SQLite(drizzle) 구현.
// 임베딩은 Float32 바이너리 BLOB 으로 직렬화하고 DB 인스턴스별 WeakMap 에 캐시해 질의 시 JSON.parse 를 0회로 줄인다.
import { asc, eq } from 'drizzle-orm' import { asc, eq, isNotNull, ne, or, sql } from 'drizzle-orm'
import { getDatabase } from '../../db' import { getDatabase, isDatabaseOpen } from '../../db'
import { ragChunks, ragDocuments } from '../../db/schema' import { ragChunks, ragDocuments } from '../../db/schema'
import { getLogger } from '../LoggerService'
import type { RAGDocument } from '@d3ro/core/types' import type { RAGDocument } from '@d3ro/core/types'
const logger = getLogger('SqliteChunkStore')
export type StoredDocument = RAGDocument export type StoredDocument = RAGDocument
export interface StoredChunk { export interface StoredChunk {
@ -15,6 +19,8 @@ export interface StoredChunk {
/** JSON 직렬화 float[]. 아직 임베딩하지 않았으면 빈 문자열. */ /** JSON 직렬화 float[]. 아직 임베딩하지 않았으면 빈 문자열. */
embedding: string embedding: string
chunkIndex: number chunkIndex: number
/** 디코딩된 Float32 벡터 (listEmbeddedChunks 캐시에서 채워진다) */
vector?: Float32Array
} }
export interface ChunkStore { export interface ChunkStore {
@ -36,6 +42,68 @@ export interface ChunkStore {
counts(): { documentCount: number; totalChunks: number } counts(): { documentCount: number; totalChunks: number }
} }
/** DB 인스턴스(계정별·테스트별)마다 자동 격리되는 임베딩 청크 메모리 캐시 */
const embeddedChunksCache = new WeakMap<object, StoredChunk[]>()
/**
* 외부 경로(동기화 어댑터, DB 초기화·임포트·종료 등)에서 `rag_documents`/`rag_chunks`를
* 변경·삭제한 뒤 stale 임베딩 캐시를 비운다.
*/
export function invalidateEmbeddedChunksCache(dbInstance?: object): void {
if (dbInstance) {
embeddedChunksCache.delete(dbInstance)
return
}
if (typeof isDatabaseOpen === 'function' && !isDatabaseOpen()) {
return
}
try {
embeddedChunksCache.delete(getDatabase())
} catch {
// DB가 열려 있지 않거나 테스트 partial mock 환경이면 조용히 건너뛴다.
}
}
function encodeFloat32Buffer(embedding: readonly number[]): Buffer {
const vector = Float32Array.from(embedding)
return Buffer.from(vector.buffer, vector.byteOffset, vector.byteLength)
}
export function decodeFloat32Buffer(
blob: Buffer | null | undefined,
jsonFallback: string,
): Float32Array | undefined {
if (blob && blob.byteLength >= 4 && blob.byteLength % 4 === 0) {
const aligned = blob.buffer.slice(blob.byteOffset, blob.byteOffset + blob.byteLength)
return new Float32Array(aligned)
}
if (jsonFallback.length > 0) {
try {
const parsed: unknown = JSON.parse(jsonFallback)
if (!Array.isArray(parsed) || parsed.length === 0) {
logger.warn('Skipping RAG chunk with non-array or empty JSON embedding fallback')
return undefined
}
const vec = new Float32Array(parsed.length)
for (let i = 0; i < parsed.length; i++) {
const value = parsed[i]
if (typeof value !== 'number' || !Number.isFinite(value)) {
logger.warn(`Skipping RAG chunk with non-finite embedding value at index ${i}`)
return undefined
}
vec[i] = value
}
return vec
} catch (err) {
logger.warn(
`Skipping RAG chunk with malformed JSON embedding fallback: ${err instanceof Error ? err.message : String(err)}`,
)
return undefined
}
}
return undefined
}
function toDocument(r: typeof ragDocuments.$inferSelect): StoredDocument { function toDocument(r: typeof ragDocuments.$inferSelect): StoredDocument {
return { return {
id: r.id, id: r.id,
@ -73,24 +141,48 @@ export class SqliteChunkStore implements ChunkStore {
removeDocument(id: string): boolean { removeDocument(id: string): boolean {
const db = getDatabase() const db = getDatabase()
db.delete(ragChunks).where(eq(ragChunks.documentId, id)).run() try {
return db.delete(ragDocuments).where(eq(ragDocuments.id, id)).run().changes > 0 return db.transaction((tx) => {
tx.delete(ragChunks).where(eq(ragChunks.documentId, id)).run()
return tx.delete(ragDocuments).where(eq(ragDocuments.id, id)).run().changes > 0
})
} finally {
invalidateEmbeddedChunksCache(db)
}
} }
replaceChunks(documentId: string, chunks: readonly string[]): void { replaceChunks(documentId: string, chunks: readonly string[]): void {
getDatabase().transaction((tx) => { const db = getDatabase()
tx.delete(ragChunks).where(eq(ragChunks.documentId, documentId)).run() try {
chunks.forEach((content, index) => { db.transaction((tx) => {
tx.insert(ragChunks) tx.delete(ragChunks).where(eq(ragChunks.documentId, documentId)).run()
.values({ id: crypto.randomUUID(), documentId, content, embedding: '', chunkIndex: index }) chunks.forEach((content, index) => {
.run() tx.insert(ragChunks)
.values({
id: crypto.randomUUID(),
documentId,
content,
embedding: '',
embeddingBlob: null,
chunkIndex: index,
})
.run()
})
}) })
}) } finally {
invalidateEmbeddedChunksCache(db)
}
} }
listChunks(documentId: string): StoredChunk[] { listChunks(documentId: string): StoredChunk[] {
return getDatabase() return getDatabase()
.select() .select({
id: ragChunks.id,
documentId: ragChunks.documentId,
content: ragChunks.content,
embedding: ragChunks.embedding,
chunkIndex: ragChunks.chunkIndex,
})
.from(ragChunks) .from(ragChunks)
.where(eq(ragChunks.documentId, documentId)) .where(eq(ragChunks.documentId, documentId))
.orderBy(asc(ragChunks.chunkIndex)) .orderBy(asc(ragChunks.chunkIndex))
@ -98,18 +190,56 @@ export class SqliteChunkStore implements ChunkStore {
} }
setEmbedding(chunkId: string, embedding: readonly number[]): void { setEmbedding(chunkId: string, embedding: readonly number[]): void {
getDatabase().update(ragChunks).set({ embedding: JSON.stringify(embedding) }).where(eq(ragChunks.id, chunkId)).run() const db = getDatabase()
try {
db.update(ragChunks)
.set({
embedding: JSON.stringify(embedding),
embeddingBlob: encodeFloat32Buffer(embedding),
})
.where(eq(ragChunks.id, chunkId))
.run()
} finally {
invalidateEmbeddedChunksCache(db)
}
} }
listEmbeddedChunks(): StoredChunk[] { listEmbeddedChunks(): StoredChunk[] {
return getDatabase().select().from(ragChunks).all().filter((c) => c.embedding.length > 0) const db = getDatabase()
const cached = embeddedChunksCache.get(db)
if (cached) return cached
const rows = db
.select()
.from(ragChunks)
.where(or(ne(ragChunks.embedding, ''), isNotNull(ragChunks.embeddingBlob)))
.all()
const loaded: StoredChunk[] = []
for (const row of rows) {
const vector = decodeFloat32Buffer(row.embeddingBlob, row.embedding)
if (!vector) continue
// Float32Array 로 디코딩한 뒤 다중 KB JSON 문자열은 캐시에 중복 보관하지 않는다.
loaded.push({
id: row.id,
documentId: row.documentId,
content: row.content,
embedding: '',
chunkIndex: row.chunkIndex,
vector,
})
}
embeddedChunksCache.set(db, loaded)
return loaded
} }
counts(): { documentCount: number; totalChunks: number } { counts(): { documentCount: number; totalChunks: number } {
const db = getDatabase() const db = getDatabase()
const docRow = db.select({ value: sql<number>`count(*)` }).from(ragDocuments).get()
const chunkRow = db.select({ value: sql<number>`count(*)` }).from(ragChunks).get()
return { return {
documentCount: db.select({ id: ragDocuments.id }).from(ragDocuments).all().length, documentCount: Number(docRow?.value ?? 0),
totalChunks: db.select({ id: ragChunks.id }).from(ragChunks).all().length, totalChunks: Number(chunkRow?.value ?? 0),
} }
} }
} }

View file

@ -1,5 +1,5 @@
// src/main/services/rag/retrieval.ts // src/main/services/rag/retrieval.ts
// 벡터 검색과 답변 프롬프트 조립 — 순수 함수(RAGService에서 옮김, 동작 동일). // 벡터 검색과 답변 프롬프트 조립 — 순수 함수(Float32Array 직접 연산 지원으로 JSON.parse 제거).
import type { RAGQueryResult } from '@d3ro/core/types' import type { RAGQueryResult } from '@d3ro/core/types'
@ -8,24 +8,43 @@ export type RetrievedChunk = RAGQueryResult['results'][number]
export interface EmbeddedChunk { export interface EmbeddedChunk {
documentId: string documentId: string
content: string content: string
/** JSON 직렬화 float[] */ /** JSON 직렬화 float[] (호환/폴백용) */
embedding: string embedding: string
/** 미리 디코딩된 Float32Array 또는 수치 배열 (있으면 JSON.parse 생략) */
vector?: ArrayLike<number>
} }
export function cosineSimilarity(a: readonly number[], b: readonly number[]): number { export function cosineSimilarity(a: ArrayLike<number>, b: ArrayLike<number>): number {
if (a.length !== b.length) return 0 if (a.length !== b.length) return 0
let dotProduct = 0 let dotProduct = 0
let normA = 0 let normA = 0
let normB = 0 let normB = 0
for (let i = 0; i < a.length; i++) { for (let i = 0; i < a.length; i++) {
dotProduct += a[i] * b[i] const ai = a[i]
normA += a[i] * a[i] const bi = b[i]
normB += b[i] * b[i] dotProduct += ai * bi
normA += ai * ai
normB += bi * bi
} }
const denom = Math.sqrt(normA) * Math.sqrt(normB) const denom = Math.sqrt(normA) * Math.sqrt(normB)
return denom === 0 ? 0 : dotProduct / denom return denom === 0 ? 0 : dotProduct / denom
} }
function resolveChunkVector(chunk: EmbeddedChunk): ArrayLike<number> | undefined {
if (chunk.vector && chunk.vector.length > 0) return chunk.vector
if (!chunk.embedding) return undefined
try {
const parsed: unknown = JSON.parse(chunk.embedding)
if (!Array.isArray(parsed) || parsed.length === 0) return undefined
for (let i = 0; i < parsed.length; i++) {
if (typeof parsed[i] !== 'number' || !Number.isFinite(parsed[i])) return undefined
}
return parsed as number[]
} catch {
return undefined
}
}
/** 질의 임베딩과 가장 가까운 청크 topK개 (유사도 내림차순). */ /** 질의 임베딩과 가장 가까운 청크 topK개 (유사도 내림차순). */
export function rankChunks( export function rankChunks(
queryEmbedding: readonly number[], queryEmbedding: readonly number[],
@ -33,12 +52,17 @@ export function rankChunks(
fileNames: ReadonlyMap<string, string>, fileNames: ReadonlyMap<string, string>,
topK: number topK: number
): RetrievedChunk[] { ): RetrievedChunk[] {
const scored = chunks.map((chunk) => ({ const scored: RetrievedChunk[] = []
documentId: chunk.documentId, for (const chunk of chunks) {
fileName: fileNames.get(chunk.documentId) ?? 'unknown', const vector = resolveChunkVector(chunk)
content: chunk.content, if (!vector) continue
similarity: cosineSimilarity(queryEmbedding, JSON.parse(chunk.embedding) as number[]), scored.push({
})) documentId: chunk.documentId,
fileName: fileNames.get(chunk.documentId) ?? 'unknown',
content: chunk.content,
similarity: cosineSimilarity(queryEmbedding, vector),
})
}
scored.sort((a, b) => b.similarity - a.similarity) scored.sort((a, b) => b.similarity - a.similarity)
return scored.slice(0, topK) return scored.slice(0, topK)
} }

View file

@ -7,6 +7,7 @@
// 부품 이름도 검증하지 않아 `..\` 같은 이름이 임시 폴더 밖 경로로 이어질 수 있었다. // 부품 이름도 검증하지 않아 `..\` 같은 이름이 임시 폴더 밖 경로로 이어질 수 있었다.
// 여기서 한 번에 검증하고, 검증을 통과한 값만 설치 단계로 넘긴다. // 여기서 한 번에 검증하고, 검증을 통과한 값만 설치 단계로 넘긴다.
import { isRecord } from '@d3ro/core/guards'
import { D3ROError, ErrorCode } from '@d3ro/core/errors' import { D3ROError, ErrorCode } from '@d3ro/core/errors'
import { compareVersions } from '../../update-policy' import { compareVersions } from '../../update-policy'
@ -118,9 +119,6 @@ function invalid(detail: string): D3ROError {
return new D3ROError(ErrorCode.ConfigReadFailed, `런타임 인덱스 형식이 올바르지 않습니다: ${detail}`) return new D3ROError(ErrorCode.ConfigReadFailed, `런타임 인덱스 형식이 올바르지 않습니다: ${detail}`)
} }
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value)
}
/** 임시 폴더 안에만 놓일 수 있는 단순 파일 이름인지 (경로 구분자·`..` 금지) */ /** 임시 폴더 안에만 놓일 수 있는 단순 파일 이름인지 (경로 구분자·`..` 금지) */
function isSafeFileName(value: unknown): value is string { function isSafeFileName(value: unknown): value is string {

View file

@ -165,6 +165,9 @@ export class STTManager extends EventEmitter implements STTEngine {
setProvider(provider: STTProviderType): void { setProvider(provider: STTProviderType): void {
logger.info(`STT provider changed to: ${provider}`) logger.info(`STT provider changed to: ${provider}`)
configSet('sttProvider', provider) configSet('sttProvider', provider)
if (provider !== 'local') {
void getLocalSTTService().releasePrimaryModel()
}
this.emit('provider-changed', { provider }) this.emit('provider-changed', { provider })
} }

View file

@ -0,0 +1,220 @@
// src/main/services/suggestion/suggestion-policy-helpers.ts
// SuggestionService에서 사용하는 순수 정책·후보 중복/재정렬·오타교정·터미널 문맥 계산기.
import {
SUGGESTION_DEFAULTS,
SUGGESTION_MAX_OUTPUT_CHARS,
extractTerminalPromptPrefix,
type SuggestionCandidate,
type SuggestionCorrection
} from '@d3ro/core/input-intelligence'
import { scoreCandidatePersonalization } from '@d3ro/core/personal-graph'
import type { TypingContext } from '../InputTelemetryService'
/** 설정에서 읽은 정책 값 (SSOT 기본값과 상한 적용 후). */
export interface SuggestionPolicyConfig {
triggerDelayMs: number
minPrefixChars: number
maxRequestsPerMinute: number
dailyBudget: number
maxCandidates: number
maxChars: number
excludedApps: string[]
}
export interface RawSuggestionPolicyConfigInput {
triggerDelayMs?: number | null
minPrefixChars?: number | null
maxRequestsPerMinute?: number | null
dailyBudget?: number | null
excludedApps?: readonly string[] | null
}
export interface CandidatePersonalizationInput {
prefix: string
continuationHints: string[]
followUpHints: string[]
relatedHints: string[]
phraseHints: string[]
}
export interface TypoCorrectionPlanLike {
originalText: string
insertText: string
corrections: ReadonlyArray<{ start: number; end: number; to: string }>
}
export interface TerminalPromptTrackingState {
lastPrompt: string | null
promptTyped: boolean
}
/**
* 스냅샷 한 번(약 0.8초) 사이에 사람이 칠 수 있는 글자 수 상한. 빈 터미널 입력칸이 이보다 크게
* 한 번에 채워지면 타이핑이 아니다(Claude Code 의 회색 추천 문구).
*/
export const TERMINAL_TYPING_STEP_CHARS = 8
/** 교정을 적용한 문맥 — 모델에는 고친 글을 보여 주고, 세션 비교는 원문으로 한다. */
export function applyCorrectionToContext(
context: TypingContext,
correction: SuggestionCorrection
): TypingContext {
const replaceTail = (text: string): string =>
text.endsWith(correction.from) ? text.slice(0, text.length - correction.from.length) + correction.to : text
return {
...context,
prefix: replaceTail(context.prefix),
documentBefore: context.documentBefore == null ? context.documentBefore : replaceTail(context.documentBefore)
}
}
/** 공백·끝 문장부호를 무시하고 같은 후보인가 */
export function sameCandidateText(a: string, b: string): boolean {
const norm = (text: string): string => text.replace(/\s+/gu, ' ').replace(/[.!?。!?…,]+$/u, '').trim()
return norm(a) === norm(b)
}
export function commonPrefixLength(a: string, b: string): number {
const max = Math.min(a.length, b.length)
let index = 0
while (index < max && a[index] === b[index]) index += 1
return index
}
/** 정규화 후 완전 중복이거나 기존 후보의 접두/확장이면 중복으로 본다. */
export function isDuplicateCandidateText(
candidate: string,
existingCandidates: ReadonlyArray<Pick<SuggestionCandidate, 'text'>>
): boolean {
const normalized = candidate.replace(/\s+/gu, ' ').trim().toLowerCase()
return existingCandidates.some((existing) => {
const existingNormalized = existing.text.replace(/\s+/gu, ' ').trim().toLowerCase()
return (
existingNormalized === normalized ||
existingNormalized.startsWith(normalized) ||
normalized.startsWith(existingNormalized)
)
})
}
/** 채우기 후보가 개인화 점수 임계(>=1.5)를 넘으면 개인 후보 뒤에 개인화 점수순으로 재정렬한다. */
export function rerankFilledCandidates(
candidates: readonly SuggestionCandidate[],
newCandidateText: string,
personalContext: CandidatePersonalizationInput
): SuggestionCandidate[] {
if (candidates.length < 2) {
return candidates.map((item, index) => ({ ...item, rank: index }))
}
const newScore = scoreCandidatePersonalization(newCandidateText, personalContext)
if (newScore < 1.5) {
return candidates.map((item, index) => ({ ...item, rank: index }))
}
const personalItems = candidates.filter((item) => item.source === 'personal')
const modelItems = candidates
.filter((item) => item.source !== 'personal')
.map((item, idx) => ({
item,
idx,
score: scoreCandidatePersonalization(item.text, personalContext)
}))
.sort((a, b) => (Math.abs(b.score - a.score) >= 0.8 ? b.score - a.score : a.idx - b.idx))
.map((entry) => entry.item)
return [...personalItems, ...modelItems].map((item, index) => ({
...item,
rank: index
}))
}
export function normalizeSuggestionPolicyConfig(
raw: RawSuggestionPolicyConfigInput
): SuggestionPolicyConfig {
return {
triggerDelayMs: Math.max(
SUGGESTION_DEFAULTS.minTriggerDelayMs,
raw.triggerDelayMs || SUGGESTION_DEFAULTS.triggerDelayMs
),
minPrefixChars: raw.minPrefixChars || SUGGESTION_DEFAULTS.minPrefixChars,
maxRequestsPerMinute: Math.min(
raw.maxRequestsPerMinute || SUGGESTION_DEFAULTS.maxRequestsPerMinute,
30
),
dailyBudget: raw.dailyBudget || SUGGESTION_DEFAULTS.dailyBudget,
maxCandidates: SUGGESTION_DEFAULTS.maxCandidates,
maxChars: SUGGESTION_MAX_OUTPUT_CHARS,
excludedApps: raw.excludedApps ? [...raw.excludedApps] : []
}
}
export function toSuggestionCorrectionFromPlan(
prefix: string,
plan: TypoCorrectionPlanLike | null | undefined
): SuggestionCorrection | undefined {
if (!plan || !prefix.endsWith(plan.originalText)) return undefined
const base = plan.corrections[0]?.start ?? 0
return {
from: plan.originalText,
to: plan.insertText,
edits: plan.corrections.map((edit) => ({
start: edit.start - base,
end: edit.end - base,
to: edit.to
}))
}
}
export function buildDecisionInputSignature(
context: TypingContext,
runtime: { modelAvailable: boolean; enabled: boolean }
): string {
return [
context.available,
context.isEditable,
context.isPassword,
context.isComposing,
context.anchor ? 'A' : '-',
context.editedSinceFocus,
context.typedRecently,
runtime.modelAvailable,
runtime.enabled
].join('|')
}
export function resolveTerminalTypingContext(
input: TypingContext,
state: TerminalPromptTrackingState
): { context: TypingContext; nextState: TerminalPromptTrackingState } {
if (input.vimMode === 'normal' || input.vimMode === 'command') {
return {
context: { ...input, prefix: '', documentBefore: null, caretReliable: false },
nextState: { lastPrompt: '', promptTyped: false }
}
}
const extracted = extractTerminalPromptPrefix(input.fullText ?? input.prefix)
const prompt = extracted ?? (input.terminalMode ? input.prefix : null)
const previous = state.lastPrompt
let typed: boolean
if (prompt === null) typed = false
else if (prompt.length === 0) typed = true
else if (previous === null) typed = prompt.length <= TERMINAL_TYPING_STEP_CHARS
else if (prompt === previous) typed = state.promptTyped
else if (previous.length === 0 && prompt.length > TERMINAL_TYPING_STEP_CHARS) {
typed = false
} else {
typed = commonPrefixLength(previous, prompt) >= Math.min(previous.length, prompt.length) - 2
}
return {
context: {
...input,
prefix: prompt ?? '',
documentBefore: null,
caretReliable: extracted !== null ? typed : typed && input.caretReliable !== false
},
nextState: {
lastPrompt: prompt,
promptTyped: typed
}
}
}

View file

@ -16,6 +16,7 @@ import { getCustomInstructionService, instructionLimitViolation } from '../Custo
import { getDictationTemplateService } from '../DictationTemplateService' import { getDictationTemplateService } from '../DictationTemplateService'
import { getMeetingDocTemplateService } from '../MeetingDocTemplateService' import { getMeetingDocTemplateService } from '../MeetingDocTemplateService'
import { getRAGService } from '../RAGService' import { getRAGService } from '../RAGService'
import { invalidateEmbeddedChunksCache } from '../rag/chunk-store'
import { deleteHistoryLocally } from '../history-deletion' import { deleteHistoryLocally } from '../history-deletion'
import { purgeRemoteAudio } from './audio-sync' import { purgeRemoteAudio } from './audio-sync'
import { pushMeetingSegments } from './transcript-sync' import { pushMeetingSegments } from './transcript-sync'
@ -578,14 +579,25 @@ const meetingMemosAdapter: SyncAdapter = {
// 부모 회의가 아직 로컬에 없으면 고아 행을 만들지 않고 미룬다(회의 pull이 실패했을 수 있다). // 부모 회의가 아직 로컬에 없으면 고아 행을 만들지 않고 미룬다(회의 pull이 실패했을 수 있다).
// 남의 팀 회의에 단 내 메모는 끝내 부모가 오지 않는다 — 엔진이 몇 번 재시도 후 포기한다. // 남의 팀 회의에 단 내 메모는 끝내 부모가 오지 않는다 — 엔진이 몇 번 재시도 후 포기한다.
if (!hasLocalMeeting(meetingId)) return 'deferred' if (!hasLocalMeeting(meetingId)) return 'deferred'
const db = getDatabase()
const createdAt = ms(row.created_at, Date.now()) const createdAt = ms(row.created_at, Date.now())
const remoteRevision = Math.max(1, int(num(row.revision)) ?? 1)
const existing = db
.select({ revision: meetingMemos.revision })
.from(meetingMemos)
.where(eq(meetingMemos.id, id))
.get()
if (existing && row.revision !== undefined && remoteRevision < existing.revision) {
return true
}
const values = { const values = {
sessionId: meetingId, sessionId: meetingId,
content, content,
timestampMs: int(num(row.timestamp_ms)) ?? 0, timestampMs: int(num(row.timestamp_ms)) ?? 0,
revision: existing ? Math.max(existing.revision, remoteRevision) : remoteRevision,
updatedAt: ms(row.updated_at, createdAt), updatedAt: ms(row.updated_at, createdAt),
} }
getDatabase() db
.insert(meetingMemos) .insert(meetingMemos)
.values({ id, ...values, createdAt }) .values({ id, ...values, createdAt })
.onConflictDoUpdate({ target: meetingMemos.id, set: values }) .onConflictDoUpdate({ target: meetingMemos.id, set: values })
@ -1025,16 +1037,20 @@ const knowledgeAdapter: SyncAdapter = {
// (새 문서든 재색인된 문서든) 완성본을 영영 놓친다. 잘린 청크로 로컬을 덮지도 않는다. // (새 문서든 재색인된 문서든) 완성본을 영영 놓친다. 잘린 청크로 로컬을 덮지도 않는다.
const chunks = completeKnowledgeChunks(chunkRows, num(row.chunk_count)) const chunks = completeKnowledgeChunks(chunkRows, num(row.chunk_count))
if (chunks === null) return 'deferred' if (chunks === null) return 'deferred'
return getRAGService().applyRemoteDocument({ const applied = getRAGService().applyRemoteDocument({
id, id,
fileName: str(row.file_name) ?? str(row.title) ?? 'document', fileName: str(row.file_name) ?? str(row.title) ?? 'document',
fileType: oneOf(row.file_type, KNOWLEDGE_TYPES) ?? 'txt', fileType: oneOf(row.file_type, KNOWLEDGE_TYPES) ?? 'txt',
chunks, chunks,
addedAt: ms(row.created_at, Date.now()), addedAt: ms(row.created_at, Date.now()),
}) })
invalidateEmbeddedChunksCache()
return applied
}, },
deleteLocal(id) { deleteLocal(id) {
return getRAGService().removeRemote(id) const removed = getRAGService().removeRemote(id)
invalidateEmbeddedChunksCache()
return removed
}, },
} }

View file

@ -4,7 +4,14 @@
import type { Key as NutKey } from '@nut-tree-fork/nut-js' import type { Key as NutKey } from '@nut-tree-fork/nut-js'
import { getLogger } from '../LoggerService' import { getLogger } from '../LoggerService'
import { sendWin32UnicodeReplacement, sendWin32UnicodeText } from '../../utils/win32-unicode-input' import {
isWin32SendInputAvailable,
sendWin32Backspaces,
sendWin32PasteChord,
sendWin32SpaceKey,
sendWin32UnicodeReplacement,
sendWin32UnicodeText,
} from '../../utils/win32-unicode-input'
import { D3ROError, ErrorCode } from '@d3ro/core/errors' import { D3ROError, ErrorCode } from '@d3ro/core/errors'
const logger = getLogger('TextInsertService') const logger = getLogger('TextInsertService')
@ -99,8 +106,15 @@ export class NutKeyboardPort implements KeyboardPort {
private readonly _load: NutModuleLoader = loadNutModule private readonly _load: NutModuleLoader = loadNutModule
) {} ) {}
private _useWin32NativeSendInput(): boolean {
return this._platform === 'win32' && this._load === loadNutModule
}
/** 붙여넣기 단축키를 한 번의 코드(chord)로 보내 await 사이에 modifier 가 눌린 채 남지 않게 한다. */ /** 붙여넣기 단축키를 한 번의 코드(chord)로 보내 await 사이에 modifier 가 눌린 채 남지 않게 한다. */
async paste(): Promise<void> { async paste(): Promise<void> {
if (this._useWin32NativeSendInput() && sendWin32PasteChord()) {
return
}
const nut = await this._ensureNut() const nut = await this._ensureNut()
const pasteModKey = this._platform === 'darwin' ? nut.Key.LeftSuper : nut.Key.LeftControl const pasteModKey = this._platform === 'darwin' ? nut.Key.LeftSuper : nut.Key.LeftControl
await nut.native.click(pasteModKey, nut.Key.V) await nut.native.click(pasteModKey, nut.Key.V)
@ -109,7 +123,7 @@ export class NutKeyboardPort implements KeyboardPort {
/** 텍스트 전체를 네이티브 호출 한 번으로 타이핑한다 (글자 사이 JS 대기 없음). */ /** 텍스트 전체를 네이티브 호출 한 번으로 타이핑한다 (글자 사이 JS 대기 없음). */
async type(text: string): Promise<void> { async type(text: string): Promise<void> {
if (!text) return if (!text) return
if (this._platform === 'win32' && this._load === loadNutModule && sendWin32UnicodeText(text)) { if (this._useWin32NativeSendInput() && sendWin32UnicodeText(text)) {
return return
} }
const nut = await this._ensureNut() const nut = await this._ensureNut()
@ -117,6 +131,9 @@ export class NutKeyboardPort implements KeyboardPort {
} }
async commitComposition(): Promise<void> { async commitComposition(): Promise<void> {
if (this._useWin32NativeSendInput() && sendWin32SpaceKey()) {
return
}
const nut = await this._ensureNut() const nut = await this._ensureNut()
await nut.native.click(nut.Key.Space) await nut.native.click(nut.Key.Space)
} }
@ -127,8 +144,7 @@ export class NutKeyboardPort implements KeyboardPort {
options: { commitComposition?: boolean } = {} options: { commitComposition?: boolean } = {}
): Promise<boolean> { ): Promise<boolean> {
if ( if (
this._platform === 'win32' && this._useWin32NativeSendInput() &&
this._load === loadNutModule &&
sendWin32UnicodeReplacement(deleteCount, text, options) sendWin32UnicodeReplacement(deleteCount, text, options)
) { ) {
return true return true
@ -136,9 +152,12 @@ export class NutKeyboardPort implements KeyboardPort {
return false return false
} }
/** 백스페이스를 count 번 — 키마다 네이티브 keyTap 한 번이다. */ /** 백스페이스를 count 번 — Windows 는 단일 SendInput 배치로, 그 외 OS 는 네이티브 keyTap 으로 전송한다. */
async backspace(count: number): Promise<void> { async backspace(count: number): Promise<void> {
if (count <= 0) return if (count <= 0) return
if (this._useWin32NativeSendInput() && sendWin32Backspaces(count)) {
return
}
const nut = await this._ensureNut() const nut = await this._ensureNut()
for (let index = 0; index < count; index += 1) { for (let index = 0; index < count; index += 1) {
await nut.native.click(nut.Key.Backspace) await nut.native.click(nut.Key.Backspace)
@ -146,10 +165,13 @@ export class NutKeyboardPort implements KeyboardPort {
} }
/** /**
* nut-js 를 미리 불러 둔다. 첫 로딩이 수 초 걸려(실측 2026-10-03: 재시작 뒤 첫 제안 수락이 4.9초, * 키 입력 백엔드를 미리 불러 둔다.
* 그중 nut-js 로딩 4.8초) 사용자가 수락하고 한참 기다렸다. 실패는 여기서 삼키고 실제 삽입 때 다시 알린다. * Windows 에서는 이미 로드된 koffi user32 SendInput 을 그대로 쓰므로 무거운 nut-js 를 로드하지 않는다.
*/ */
async preload(): Promise<void> { async preload(): Promise<void> {
if (this._useWin32NativeSendInput() && isWin32SendInputAvailable()) {
return
}
try { try {
await this._ensureNut() await this._ensureNut()
} catch { } catch {

View file

@ -20,7 +20,10 @@ const INPUT_KEYBOARD = 1
const KEYEVENTF_KEYUP = 0x0002 const KEYEVENTF_KEYUP = 0x0002
const KEYEVENTF_UNICODE = 0x0004 const KEYEVENTF_UNICODE = 0x0004
const VK_BACK = 0x08 const VK_BACK = 0x08
const VK_CONTROL = 0x11
const VK_SPACE = 0x20 const VK_SPACE = 0x20
const VK_C = 0x43
const VK_V = 0x56
type SendInputFn = (cInputs: number, pInputs: Buffer, cbSize: number) => number type SendInputFn = (cInputs: number, pInputs: Buffer, cbSize: number) => number
@ -83,18 +86,89 @@ function ensureLoaded(): SendInputFn | null {
} }
} }
function writeVirtualKeyPair(buffer: Buffer, pairIndex: number, vk: number): void { function writeKeyEvent(buffer: Buffer, eventIndex: number, vk: number, keyUp: boolean): void {
const downBase = pairIndex * 2 * inputStructSize const base = eventIndex * inputStructSize
buffer.writeUInt32LE(INPUT_KEYBOARD, downBase) buffer.writeUInt32LE(INPUT_KEYBOARD, base)
buffer.writeUInt16LE(vk, downBase + unionOffset) buffer.writeUInt16LE(vk, base + unionOffset)
buffer.writeUInt16LE(0, downBase + unionOffset + 2) buffer.writeUInt16LE(0, base + unionOffset + 2)
buffer.writeUInt32LE(0, downBase + unionOffset + 4) buffer.writeUInt32LE(keyUp ? KEYEVENTF_KEYUP : 0, base + unionOffset + 4)
}
const upBase = (pairIndex * 2 + 1) * inputStructSize function writeVirtualKeyPair(buffer: Buffer, pairIndex: number, vk: number): void {
buffer.writeUInt32LE(INPUT_KEYBOARD, upBase) writeKeyEvent(buffer, pairIndex * 2, vk, false)
buffer.writeUInt16LE(vk, upBase + unionOffset) writeKeyEvent(buffer, pairIndex * 2 + 1, vk, true)
buffer.writeUInt16LE(0, upBase + unionOffset + 2) }
buffer.writeUInt32LE(KEYEVENTF_KEYUP, upBase + unionOffset + 4)
/** Windows SendInput(koffi) 백엔드를 즉시 사용할 수 있는지 확인한다. */
export function isWin32SendInputAvailable(): boolean {
const sendInput = ensureLoaded()
return Boolean(sendInput && inputStructSize > 0)
}
/**
* 수정자 + 일반 키 조합(예: Ctrl+V, Ctrl+C)을 단일 SendInput 원자 배치(4 이벤트)로 전송한다.
* await 사이에 Ctrl 이 눌린 채 남지 않는다.
*/
export function sendWin32KeyChord(modifierVk: number, keyVk: number): boolean {
const sendInput = ensureLoaded()
if (!sendInput || inputStructSize <= 0) return false
try {
const totalEvents = 4
const buffer = Buffer.alloc(totalEvents * inputStructSize)
writeKeyEvent(buffer, 0, modifierVk, false)
writeKeyEvent(buffer, 1, keyVk, false)
writeKeyEvent(buffer, 2, keyVk, true)
writeKeyEvent(buffer, 3, modifierVk, true)
return sendInputBatch(sendInput, totalEvents, buffer)
} catch (error) {
if (error instanceof D3ROError) throw error
logger.warn(`SendInput(chord) 실행 실패: ${error instanceof Error ? error.message : String(error)}`)
return false
}
}
export function sendWin32PasteChord(): boolean {
return sendWin32KeyChord(VK_CONTROL, VK_V)
}
export function sendWin32CopyChord(): boolean {
return sendWin32KeyChord(VK_CONTROL, VK_C)
}
export function sendWin32SpaceKey(): boolean {
const sendInput = ensureLoaded()
if (!sendInput || inputStructSize <= 0) return false
try {
const buffer = Buffer.alloc(2 * inputStructSize)
writeVirtualKeyPair(buffer, 0, VK_SPACE)
return sendInputBatch(sendInput, 2, buffer)
} catch (error) {
if (error instanceof D3ROError) throw error
logger.warn(`SendInput(space) 실행 실패: ${error instanceof Error ? error.message : String(error)}`)
return false
}
}
export function sendWin32Backspaces(count: number): boolean {
const safeCount = Math.max(0, Math.trunc(count))
if (safeCount === 0) return true
const sendInput = ensureLoaded()
if (!sendInput || inputStructSize <= 0) return false
try {
const totalEvents = safeCount * 2
const buffer = Buffer.alloc(totalEvents * inputStructSize)
for (let i = 0; i < safeCount; i += 1) {
writeVirtualKeyPair(buffer, i, VK_BACK)
}
return sendInputBatch(sendInput, totalEvents, buffer)
} catch (error) {
if (error instanceof D3ROError) throw error
logger.warn(`SendInput(backspace) 실행 실패: ${error instanceof Error ? error.message : String(error)}`)
return false
}
} }
function writeUnicodeKeyPair(buffer: Buffer, pairIndex: number, codeUnit: number): void { function writeUnicodeKeyPair(buffer: Buffer, pairIndex: number, codeUnit: number): void {

View file

@ -30,7 +30,7 @@ import { getIsQuitting, setIsQuitting } from '../lifecycle'
import { hardenWebContents } from './web-contents-hardening' import { hardenWebContents } from './web-contents-hardening'
import { commandPopupSize, historyPopupSize } from './popup-list-geometry' import { commandPopupSize, historyPopupSize } from './popup-list-geometry'
import { configGet, configSet } from '../services/ConfigService' import { configGet, configSet } from '../services/ConfigService'
import { getI18n } from '@d3ro/i18n' import { getI18n } from '@d3ro/i18n/main'
import { import {
createPopupWindow, createPopupWindow,
injectPopupTheme, injectPopupTheme,
@ -223,6 +223,9 @@ let historyPopupWindow: BrowserWindow | null = null
let commandPopupWindow: BrowserWindow | null = null let commandPopupWindow: BrowserWindow | null = null
let captionOverlayWindow: BrowserWindow | null = null let captionOverlayWindow: BrowserWindow | null = null
let suggestionOverlayWindow: BrowserWindow | null = null let suggestionOverlayWindow: BrowserWindow | null = null
let mainAlwaysOnTopTimer: ReturnType<typeof setTimeout> | null = null
let historyHideTimer: ReturnType<typeof setTimeout> | null = null
let commandHideTimer: ReturnType<typeof setTimeout> | null = null
// ── 메인 윈도우 ─────────────────────────────────────── // ── 메인 윈도우 ───────────────────────────────────────
@ -324,6 +327,10 @@ export function createMainWindow(): BrowserWindow {
}) })
mainWindow.on('closed', () => { mainWindow.on('closed', () => {
if (mainAlwaysOnTopTimer) {
clearTimeout(mainAlwaysOnTopTimer)
mainAlwaysOnTopTimer = null
}
mainWindow = null mainWindow = null
}) })
@ -346,7 +353,9 @@ export function createMainWindow(): BrowserWindow {
mainWindow.show() mainWindow.show()
mainWindow.focus() mainWindow.focus()
mainWindow.setAlwaysOnTop(true) mainWindow.setAlwaysOnTop(true)
setTimeout(() => { if (mainAlwaysOnTopTimer) clearTimeout(mainAlwaysOnTopTimer)
mainAlwaysOnTopTimer = setTimeout(() => {
mainAlwaysOnTopTimer = null
try { try {
mainWindow?.setAlwaysOnTop(false) mainWindow?.setAlwaysOnTop(false)
} catch { } catch {
@ -570,6 +579,10 @@ function createHistoryPopupWindow(): BrowserWindow {
name: 'history-popup', name: 'history-popup',
bounds: { width: 360, height: 400 }, bounds: { width: 360, height: 400 },
onClosed: () => { onClosed: () => {
if (historyHideTimer) {
clearTimeout(historyHideTimer)
historyHideTimer = null
}
historyPopupWindow = null historyPopupWindow = null
} }
}) })
@ -583,7 +596,16 @@ export function getHistoryPopupWindow(): BrowserWindow {
return historyPopupWindow return historyPopupWindow
} }
/** 창을 새로 만들지 않고 현재 살아있는 HistoryPopup 인스턴스만 조회한다. */
export function peekHistoryPopupWindow(): BrowserWindow | null {
return historyPopupWindow && !historyPopupWindow.isDestroyed() ? historyPopupWindow : null
}
export function showHistoryPopup(entries: Array<Record<string, unknown>>): void { export function showHistoryPopup(entries: Array<Record<string, unknown>>): void {
if (historyHideTimer) {
clearTimeout(historyHideTimer)
historyHideTimer = null
}
const win = getHistoryPopupWindow() const win = getHistoryPopupWindow()
// 커서 위치에 즉시 배치+표시 (2-phase 제거 — 숨겨진 윈도우 렌더러 비활성 문제 방지) // 커서 위치에 즉시 배치+표시 (2-phase 제거 — 숨겨진 윈도우 렌더러 비활성 문제 방지)
@ -598,8 +620,12 @@ export function showHistoryPopup(entries: Array<Record<string, unknown>>): void
export function hideHistoryPopup(): void { export function hideHistoryPopup(): void {
if (historyPopupWindow && !historyPopupWindow.isDestroyed()) { if (historyPopupWindow && !historyPopupWindow.isDestroyed()) {
if (historyHideTimer) {
clearTimeout(historyHideTimer)
}
sendToPopupWindow(historyPopupWindow, IPC_CHANNELS.POPUP_HISTORY.HIDE, {}) sendToPopupWindow(historyPopupWindow, IPC_CHANNELS.POPUP_HISTORY.HIDE, {})
setTimeout(() => { historyHideTimer = setTimeout(() => {
historyHideTimer = null
if (historyPopupWindow && !historyPopupWindow.isDestroyed()) { if (historyPopupWindow && !historyPopupWindow.isDestroyed()) {
historyPopupWindow.hide() historyPopupWindow.hide()
} }
@ -624,6 +650,10 @@ function createCommandPopupWindow(): BrowserWindow {
name: 'command-popup', name: 'command-popup',
bounds: commandPopupSize(0), bounds: commandPopupSize(0),
onClosed: () => { onClosed: () => {
if (commandHideTimer) {
clearTimeout(commandHideTimer)
commandHideTimer = null
}
commandPopupWindow = null commandPopupWindow = null
} }
}) })
@ -636,7 +666,16 @@ export function getCommandPopupWindow(): BrowserWindow {
return commandPopupWindow return commandPopupWindow
} }
/** 창을 새로 만들지 않고 현재 살아있는 CommandPopup 인스턴스만 조회한다. */
export function peekCommandPopupWindow(): BrowserWindow | null {
return commandPopupWindow && !commandPopupWindow.isDestroyed() ? commandPopupWindow : null
}
export function showCommandPopup(commands: Array<Record<string, unknown>>, activeId: string | null): void { export function showCommandPopup(commands: Array<Record<string, unknown>>, activeId: string | null): void {
if (commandHideTimer) {
clearTimeout(commandHideTimer)
commandHideTimer = null
}
const win = getCommandPopupWindow() const win = getCommandPopupWindow()
placePopupNearCursor(win, commandPopupSize(commands.length)) placePopupNearCursor(win, commandPopupSize(commands.length))
sendToPopupWindow(win, IPC_CHANNELS.POPUP_COMMAND.SHOW_ITEMS, { commands, activeId, _i18n: getPopupI18nStrings() }) sendToPopupWindow(win, IPC_CHANNELS.POPUP_COMMAND.SHOW_ITEMS, { commands, activeId, _i18n: getPopupI18nStrings() })
@ -647,8 +686,12 @@ export function showCommandPopup(commands: Array<Record<string, unknown>>, activ
export function hideCommandPopup(): void { export function hideCommandPopup(): void {
if (commandPopupWindow && !commandPopupWindow.isDestroyed()) { if (commandPopupWindow && !commandPopupWindow.isDestroyed()) {
if (commandHideTimer) {
clearTimeout(commandHideTimer)
}
sendToPopupWindow(commandPopupWindow, IPC_CHANNELS.POPUP_COMMAND.HIDE, {}) sendToPopupWindow(commandPopupWindow, IPC_CHANNELS.POPUP_COMMAND.HIDE, {})
setTimeout(() => { commandHideTimer = setTimeout(() => {
commandHideTimer = null
if (commandPopupWindow && !commandPopupWindow.isDestroyed()) { if (commandPopupWindow && !commandPopupWindow.isDestroyed()) {
commandPopupWindow.hide() commandPopupWindow.hide()
} }

View file

@ -7,7 +7,7 @@
// 분류되지 않는 코드는 기존 동작대로 원문을 그린다. // 분류되지 않는 코드는 기존 동작대로 원문을 그린다.
import { classifyVoiceError, type VoiceErrorMessageId } from '@d3ro/core/voice-error-message' import { classifyVoiceError, type VoiceErrorMessageId } from '@d3ro/core/voice-error-message'
import type { TranslationKey } from '@d3ro/i18n' import type { TranslationKey } from '@d3ro/i18n/main'
export type RecordingTipTranslate = (key: TranslationKey, params?: Record<string, string | number>) => string export type RecordingTipTranslate = (key: TranslationKey, params?: Record<string, string | number>) => string

View file

@ -9,7 +9,7 @@
import { useState, useEffect, useMemo } from 'react' import { useState, useEffect, useMemo } from 'react'
import { ThemeProvider, CssBaseline, useMediaQuery } from '@mui/material' import { ThemeProvider, CssBaseline, useMediaQuery } from '@mui/material'
import { getTheme } from '@d3ro/ui/theme' import { getTheme } from '@d3ro/ui/theme'
import { I18nProvider, useI18n, type I18nStorage, type Locale } from '@d3ro/i18n' import { I18nProvider, isValidLocale, useI18n, type I18nStorage, type Locale } from '@d3ro/i18n'
import { AppLayout } from './components/AppLayout' import { AppLayout } from './components/AppLayout'
import { UpgradePromptModal } from './components/UpgradePromptModal' import { UpgradePromptModal } from './components/UpgradePromptModal'
import { createSystemAudioCaptureController } from './utils/systemAudioCapture' import { createSystemAudioCaptureController } from './utils/systemAudioCapture'
@ -32,8 +32,8 @@ function SyncedLocale(): null {
useEffect( useEffect(
() => () =>
window.electronAPI.config.onChanged((e: ConfigChangedEvent) => { window.electronAPI.config.onChanged((e: ConfigChangedEvent) => {
if (e.key === 'language' && typeof e.value === 'string' && e.value !== locale) { if (e.key === 'language' && typeof e.value === 'string' && isValidLocale(e.value) && e.value !== locale) {
setLocale(e.value as Locale) setLocale(e.value)
} }
}), }),
[locale, setLocale], [locale, setLocale],
@ -41,24 +41,35 @@ function SyncedLocale(): null {
return null return null
} }
function isThemeMode(value: unknown): value is ThemeMode {
return value === 'auto' || value === 'dark' || value === 'light'
}
export function App(): React.ReactElement { export function App(): React.ReactElement {
const [themeMode, setThemeMode] = useState<ThemeMode>('auto') const [themeMode, setThemeMode] = useState<ThemeMode>('auto')
const prefersDark = useMediaQuery('(prefers-color-scheme: dark)') const prefersDark = useMediaQuery('(prefers-color-scheme: dark)')
// 설정에서 테마 로드 + 변경 감지 // 설정에서 테마 로드 + 변경 감지
useEffect(() => { useEffect(() => {
window.electronAPI.config.getTheme().then((result) => { let cancelled = false
if (result.success) { window.electronAPI.config
setThemeMode(result.data) .getTheme()
} .then((result) => {
}) if (!cancelled && result.success && isThemeMode(result.data)) {
setThemeMode(result.data)
}
})
.catch(() => {})
const unsub = window.electronAPI.config.onChanged((e: ConfigChangedEvent) => { const unsub = window.electronAPI.config.onChanged((e: ConfigChangedEvent) => {
if (e.key === 'theme') { if (e.key === 'theme' && isThemeMode(e.value)) {
setThemeMode(e.value as ThemeMode) setThemeMode(e.value)
} }
}) })
return unsub return () => {
cancelled = true
unsub()
}
}, []) }, [])
// 시스템 오디오 캡처: 메인 프로세스의 시작/중지 요청에 응답. // 시스템 오디오 캡처: 메인 프로세스의 시작/중지 요청에 응답.

View file

@ -4,7 +4,7 @@
// 버전·모델은 설정에, 시계는 OS 에 있고, "100% LOCAL PRIVACY · ZERO CLOUD SYNC"는 기기 간 동기화와 모순된 문구였다. // 버전·모델은 설정에, 시계는 OS 에 있고, "100% LOCAL PRIVACY · ZERO CLOUD SYNC"는 기기 간 동기화와 모순된 문구였다.
// 의미 있는 상태(로컬 AI 연결·자막·회의 녹음 중)는 사이드바 아래 한 줄로 옮겼다. // 의미 있는 상태(로컬 AI 연결·자막·회의 녹음 중)는 사이드바 아래 한 줄로 옮겼다.
import React, { useState, useEffect, useCallback } from 'react' import React, { Suspense, lazy, useState, useEffect, useCallback } from 'react'
import { Alert, Box, Button, IconButton, Snackbar } from '@mui/material' import { Alert, Box, Button, IconButton, Snackbar } from '@mui/material'
import { X } from 'lucide-react' import { X } from 'lucide-react'
import { OverlayScrollbarsComponent } from 'overlayscrollbars-react' import { OverlayScrollbarsComponent } from 'overlayscrollbars-react'
@ -13,28 +13,29 @@ import { d3roPalette } from '@d3ro/ui/theme'
import { useI18n } from '@d3ro/i18n' import { useI18n } from '@d3ro/i18n'
import type { LicenseTier } from '@d3ro/core/types' import type { LicenseTier } from '@d3ro/core/types'
import { HomePage } from '../pages/HomePage' import { HomePage } from '../pages/HomePage'
import { HistoryPage } from '../pages/HistoryPage'
import { DictionaryPage } from '../pages/DictionaryPage'
import { CommandsPage } from '../pages/CommandsPage'
import { VoiceConversationPage } from '../pages/VoiceConversationPage'
import { KnowledgeBasePage } from '../pages/KnowledgeBasePage'
import { MeetingModePage } from '../pages/MeetingModePage'
import { UnifiedSearchPage } from '../pages/UnifiedSearchPage'
import { SettingsModal } from './SettingsModal'
import { OPEN_SETTINGS_EVENT, readOpenSettingsTab, DEFAULT_SETTINGS_TAB } from '../navigation/settingsTabs' import { OPEN_SETTINGS_EVENT, readOpenSettingsTab, DEFAULT_SETTINGS_TAB } from '../navigation/settingsTabs'
import type { SettingsTabId } from '../navigation/settingsTabs' import type { SettingsTabId } from '../navigation/settingsTabs'
import { presentError, settingsTabOf } from '../navigation/error-recovery' import { presentError, settingsTabOf } from '../navigation/error-recovery'
import { AppNavigationProvider, PRIMARY_ROUTES, useAppNavigation, useRouteRequest } from '../navigation/app-navigation' import { AppNavigationProvider, PRIMARY_ROUTES, useAppNavigation, useRouteRequest } from '../navigation/app-navigation'
import { LicenseModal } from './LicenseModal'
import { OnboardingModal } from './OnboardingModal'
import { OllamaGuideModal } from './OllamaGuideModal'
import { AdBanner } from './ads/AdBanner' import { AdBanner } from './ads/AdBanner'
import { RewardedQuotaModal } from './ads/RewardedQuotaModal'
import { SupportModal } from './support/SupportModal'
import { TitleBar } from './TitleBar' import { TitleBar } from './TitleBar'
import { AppSidebar } from './shell/AppSidebar' import { AppSidebar } from './shell/AppSidebar'
import { NotifyProvider, useNotify } from './shared' import { NotifyProvider, useNotify } from './shared'
const HistoryPage = lazy(() => import('../pages/HistoryPage').then((m) => ({ default: m.HistoryPage })))
const DictionaryPage = lazy(() => import('../pages/DictionaryPage').then((m) => ({ default: m.DictionaryPage })))
const CommandsPage = lazy(() => import('../pages/CommandsPage').then((m) => ({ default: m.CommandsPage })))
const VoiceConversationPage = lazy(() => import('../pages/VoiceConversationPage').then((m) => ({ default: m.VoiceConversationPage })))
const KnowledgeBasePage = lazy(() => import('../pages/KnowledgeBasePage').then((m) => ({ default: m.KnowledgeBasePage })))
const MeetingModePage = lazy(() => import('../pages/MeetingModePage').then((m) => ({ default: m.MeetingModePage })))
const UnifiedSearchPage = lazy(() => import('../pages/UnifiedSearchPage').then((m) => ({ default: m.UnifiedSearchPage })))
const SettingsModal = lazy(() => import('./SettingsModal').then((m) => ({ default: m.SettingsModal })))
const LicenseModal = lazy(() => import('./LicenseModal').then((m) => ({ default: m.LicenseModal })))
const OnboardingModal = lazy(() => import('./OnboardingModal').then((m) => ({ default: m.OnboardingModal })))
const OllamaGuideModal = lazy(() => import('./OllamaGuideModal').then((m) => ({ default: m.OllamaGuideModal })))
const RewardedQuotaModal = lazy(() => import('./ads/RewardedQuotaModal').then((m) => ({ default: m.RewardedQuotaModal })))
const SupportModal = lazy(() => import('./support/SupportModal').then((m) => ({ default: m.SupportModal })))
/** 입력 요소에 포커스가 있으면 전역 단축키를 무시한다 — 타자 중에 화면이 바뀌면 안 된다. */ /** 입력 요소에 포커스가 있으면 전역 단축키를 무시한다 — 타자 중에 화면이 바뀌면 안 된다. */
function isTypingTarget(target: EventTarget | null): boolean { function isTypingTarget(target: EventTarget | null): boolean {
if (!(target instanceof HTMLElement)) return false if (!(target instanceof HTMLElement)) return false
@ -71,17 +72,28 @@ function Shell(): React.ReactElement {
}) })
useEffect(() => { useEffect(() => {
window.electronAPI.config.getAll().then((r) => { let cancelled = false
if (r.success && !r.data.onboardingCompleted) { window.electronAPI.config
setOnboardingOpen(true) .getAll()
} .then((r) => {
}) if (!cancelled && r.success && !r.data.onboardingCompleted) {
setOnboardingOpen(true)
}
})
.catch(() => {})
return () => {
cancelled = true
}
}, []) }, [])
useEffect(() => { useEffect(() => {
window.electronAPI.license.getInfo().then((r) => { let cancelled = false
if (r.success) setCurrentTier(r.data.tier) window.electronAPI.license
}) .getInfo()
.then((r) => {
if (!cancelled && r.success) setCurrentTier(r.data.tier)
})
.catch(() => {})
const unsubTier = window.electronAPI.license.onTierChanged((info) => { const unsubTier = window.electronAPI.license.onTierChanged((info) => {
setCurrentTier(info.tier) setCurrentTier(info.tier)
@ -125,6 +137,7 @@ function Shell(): React.ReactElement {
}) })
return () => { return () => {
cancelled = true
unsubTier() unsubTier()
unsubUpgrade() unsubUpgrade()
unsubFallback() unsubFallback()
@ -196,15 +209,17 @@ function Shell(): React.ReactElement {
}} }}
style={{ height: '100%', width: '100%' }} style={{ height: '100%', width: '100%' }}
> >
{route === 'home' && <HomePage />} <Suspense fallback={null}>
{route === 'search' && <UnifiedSearchPage />} {route === 'home' && <HomePage />}
{route === 'records' && <HistoryPage />} {route === 'search' && <UnifiedSearchPage />}
{route === 'meetings' && <MeetingModePage />} {route === 'records' && <HistoryPage />}
{route === 'conversation' && <VoiceConversationPage />} {route === 'meetings' && <MeetingModePage />}
{route === 'commands' && <CommandsPage />} {route === 'conversation' && <VoiceConversationPage />}
{route === 'dictionary' && <DictionaryPage />} {route === 'commands' && <CommandsPage />}
{route === 'knowledge' && <KnowledgeBasePage />} {route === 'dictionary' && <DictionaryPage />}
{route === 'settings' && <SettingsModal open initialTab={settingsTab} onClose={back} />} {route === 'knowledge' && <KnowledgeBasePage />}
{route === 'settings' && <SettingsModal open initialTab={settingsTab} onClose={back} />}
</Suspense>
</OverlayScrollbarsComponent> </OverlayScrollbarsComponent>
</Box> </Box>
@ -219,15 +234,19 @@ function Shell(): React.ReactElement {
</Box> </Box>
</Box> </Box>
<LicenseModal open={licenseModalOpen} onClose={() => setLicenseModalOpen(false)} /> <Suspense fallback={null}>
<OnboardingModal open={onboardingOpen} onClose={() => setOnboardingOpen(false)} /> {licenseModalOpen && <LicenseModal open={licenseModalOpen} onClose={() => setLicenseModalOpen(false)} />}
<SupportModal open={supportModalOpen} onClose={() => setSupportModalOpen(false)} /> {onboardingOpen && <OnboardingModal open={onboardingOpen} onClose={() => setOnboardingOpen(false)} />}
<OllamaGuideModal open={ollamaGuideOpen} onClose={() => setOllamaGuideOpen(false)} /> {supportModalOpen && <SupportModal open={supportModalOpen} onClose={() => setSupportModalOpen(false)} />}
<RewardedQuotaModal {ollamaGuideOpen && <OllamaGuideModal open={ollamaGuideOpen} onClose={() => setOllamaGuideOpen(false)} />}
open={rewardedModalOpen} {rewardedModalOpen && (
onClose={() => setRewardedModalOpen(false)} <RewardedQuotaModal
onRewardClaimed={(tokens) => notify({ message: t('shell.rewardClaimed', { tokens }), severity: 'success' })} open={rewardedModalOpen}
/> onClose={() => setRewardedModalOpen(false)}
onRewardClaimed={(tokens) => notify({ message: t('shell.rewardClaimed', { tokens }), severity: 'success' })}
/>
)}
</Suspense>
<Snackbar <Snackbar
open={fallbackMsg !== null} open={fallbackMsg !== null}

View file

@ -3,11 +3,12 @@
// 로그인 상태 / 지금 동기화 / 마지막 동기화 / 올릴 변경·거부된 변경 건수 // 로그인 상태 / 지금 동기화 / 마지막 동기화 / 올릴 변경·거부된 변경 건수
// v4(2026-09-28): 설정 행 문법 — 계정 · 동기화 상태 · 녹음 파일 동기화. 강조 카드 배경 제거. // v4(2026-09-28): 설정 행 문법 — 계정 · 동기화 상태 · 녹음 파일 동기화. 강조 카드 배경 제거.
import { useCallback, useEffect, useState } from 'react' import { useCallback, useEffect, useRef, useState } from 'react'
import { Box, Button, Alert, CircularProgress, Switch } from '@mui/material' import { Box, Button, Alert, CircularProgress, Switch } from '@mui/material'
import { Globe, GitBranch, RefreshCw } from 'lucide-react' import { Globe, GitBranch, RefreshCw } from 'lucide-react'
import { d3roPalette } from '@d3ro/ui/theme' import { d3roPalette } from '@d3ro/ui/theme'
import { useI18n } from '@d3ro/i18n' import { useI18n } from '@d3ro/i18n'
import { toErrorMessage } from '../utils/error-message'
import { SettingRow, SettingsSection } from './settings/SettingsRows' import { SettingRow, SettingsSection } from './settings/SettingsRows'
interface CloudSyncState { interface CloudSyncState {
@ -35,18 +36,30 @@ export function CloudSyncSection(): React.ReactElement {
const [info, setInfo] = useState<string | null>(null) const [info, setInfo] = useState<string | null>(null)
const [busy, setBusy] = useState(false) const [busy, setBusy] = useState(false)
const [audioSync, setAudioSync] = useState(true) const [audioSync, setAudioSync] = useState(true)
const mountedRef = useRef(true)
const refreshSeqRef = useRef(0)
const refreshState = useCallback(async () => { const refreshState = useCallback(async () => {
const r = await window.electronAPI.cloudSync.getState() const seq = ++refreshSeqRef.current
if (r.success) setState(r.data) try {
const r = await window.electronAPI.cloudSync.getState()
if (mountedRef.current && refreshSeqRef.current === seq && r.success) setState(r.data)
} catch {
// ignore IPC failure
}
}, []) }, [])
// 초기 상태 로드 + 이벤트 구독 // 초기 상태 로드 + 이벤트 구독
useEffect(() => { useEffect(() => {
mountedRef.current = true
let cancelled = false
void refreshState() void refreshState()
void window.electronAPI.config.get({ key: 'cloudSyncAudio' }).then((r) => { void window.electronAPI.config
if (r.success) setAudioSync(r.data !== false) .get({ key: 'cloudSyncAudio' })
}) .then((r) => {
if (!cancelled && r.success) setAudioSync(r.data !== false)
})
.catch(() => {})
const unsubAuth = window.electronAPI.cloudSync.onAuthChanged(() => { const unsubAuth = window.electronAPI.cloudSync.onAuthChanged(() => {
void refreshState() void refreshState()
@ -60,6 +73,8 @@ export function CloudSyncSection(): React.ReactElement {
void refreshState() void refreshState()
}) })
return () => { return () => {
cancelled = true
mountedRef.current = false
unsubAuth() unsubAuth()
unsubComplete() unsubComplete()
unsubError() unsubError()
@ -76,13 +91,16 @@ export function CloudSyncSection(): React.ReactElement {
setBusy(true) setBusy(true)
try { try {
const r = await window.electronAPI.cloudSync.signIn({ provider }) const r = await window.electronAPI.cloudSync.signIn({ provider })
if (!mountedRef.current) return
if (!r.success) { if (!r.success) {
setError(r.error.message) setError(r.error.message)
} else { } else {
setInfo(t('cloudSync.browserPending')) setInfo(t('cloudSync.browserPending'))
} }
} catch (err: unknown) {
if (mountedRef.current) setError(toErrorMessage(err))
} finally { } finally {
setBusy(false) if (mountedRef.current) setBusy(false)
} }
} }
@ -90,10 +108,14 @@ export function CloudSyncSection(): React.ReactElement {
setBusy(true) setBusy(true)
try { try {
await window.electronAPI.cloudSync.signOut() await window.electronAPI.cloudSync.signOut()
setInfo(t('cloudSync.signedOut')) if (mountedRef.current) setInfo(t('cloudSync.signedOut'))
} catch {
// ignore sign-out IPC rejection
} finally { } finally {
setBusy(false) if (mountedRef.current) {
void refreshState() setBusy(false)
void refreshState()
}
} }
} }
@ -105,14 +127,19 @@ export function CloudSyncSection(): React.ReactElement {
try { try {
const pushed = await window.electronAPI.cloudSync.pushAll() const pushed = await window.electronAPI.cloudSync.pushAll()
const pulled = await window.electronAPI.cloudSync.pullAll() const pulled = await window.electronAPI.cloudSync.pullAll()
if (!mountedRef.current) return
const count = (pushed.success ? pushed.data.pushed : 0) + (pulled.success ? pulled.data.pushed : 0) const count = (pushed.success ? pushed.data.pushed : 0) + (pulled.success ? pulled.data.pushed : 0)
const errors = (pushed.success ? pushed.data.errors.length : 1) + (pulled.success ? pulled.data.errors.length : 1) const errors = (pushed.success ? pushed.data.errors.length : 1) + (pulled.success ? pulled.data.errors.length : 1)
if (!pushed.success) setError(pushed.error.message) if (!pushed.success) setError(pushed.error.message)
else if (!pulled.success) setError(pulled.error.message) else if (!pulled.success) setError(pulled.error.message)
setInfo(errors > 0 ? t('cloudSync.doneWithErrors', { count, errors }) : t('cloudSync.done', { count })) setInfo(errors > 0 ? t('cloudSync.doneWithErrors', { count, errors }) : t('cloudSync.done', { count }))
} catch (err: unknown) {
if (mountedRef.current) setError(toErrorMessage(err))
} finally { } finally {
setBusy(false) if (mountedRef.current) {
void refreshState() setBusy(false)
void refreshState()
}
} }
} }

View file

@ -2,7 +2,7 @@
// OpenAI API 키 · Codex OAuth(ChatGPT 구독) · 다른 STT 서비스 키 발급 안내 모달. STT 설정 탭에서 연다. // OpenAI API 키 · Codex OAuth(ChatGPT 구독) · 다른 STT 서비스 키 발급 안내 모달. STT 설정 탭에서 연다.
// v4(2026-09-28): 평면 다이얼로그 — 제목(h2) + 탭 + 번호 단계(소제목 + 설명, 1px 선). 근거 없는 성능·가격 수식어 제거. // v4(2026-09-28): 평면 다이얼로그 — 제목(h2) + 탭 + 번호 단계(소제목 + 설명, 1px 선). 근거 없는 성능·가격 수식어 제거.
import { useState } from 'react' import { useEffect, useRef, useState } from 'react'
import { import {
Dialog, Dialog,
DialogTitle, DialogTitle,
@ -78,11 +78,25 @@ export function CodexOAuthGuideModal({ open, onClose }: CodexOAuthGuideModalProp
const { t } = useI18n() const { t } = useI18n()
const [tab, setTab] = useState(0) const [tab, setTab] = useState(0)
const [copied, setCopied] = useState(false) const [copied, setCopied] = useState(false)
const copyTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
useEffect(() => {
return () => {
if (copyTimerRef.current) {
clearTimeout(copyTimerRef.current)
copyTimerRef.current = null
}
}
}, [])
const handleCopy = (text: string) => { const handleCopy = (text: string) => {
navigator.clipboard.writeText(text) void navigator.clipboard.writeText(text).catch(() => {})
setCopied(true) setCopied(true)
setTimeout(() => setCopied(false), 2000) if (copyTimerRef.current) clearTimeout(copyTimerRef.current)
copyTimerRef.current = setTimeout(() => {
copyTimerRef.current = null
setCopied(false)
}, 2000)
} }
const handleOpenLink = (url: string) => { const handleOpenLink = (url: string) => {

View file

@ -28,6 +28,16 @@ export function FileDropZone(): React.ReactElement {
const [error, setError] = useState<string | null>(null) const [error, setError] = useState<string | null>(null)
const [copied, setCopied] = useState(false) const [copied, setCopied] = useState(false)
const dropRef = useRef<HTMLButtonElement>(null) const dropRef = useRef<HTMLButtonElement>(null)
const copyTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
useEffect(() => {
return () => {
if (copyTimerRef.current) {
clearTimeout(copyTimerRef.current)
copyTimerRef.current = null
}
}
}, [])
useEffect(() => { useEffect(() => {
const unsubProgress = window.electronAPI.fileTranscription.onProgress((data) => { const unsubProgress = window.electronAPI.fileTranscription.onProgress((data) => {
@ -136,9 +146,13 @@ export function FileDropZone(): React.ReactElement {
const handleCopy = useCallback(() => { const handleCopy = useCallback(() => {
if (result?.fullText) { if (result?.fullText) {
navigator.clipboard.writeText(result.fullText) void navigator.clipboard.writeText(result.fullText).catch(() => {})
setCopied(true) setCopied(true)
setTimeout(() => setCopied(false), 2000) if (copyTimerRef.current) clearTimeout(copyTimerRef.current)
copyTimerRef.current = setTimeout(() => {
copyTimerRef.current = null
setCopied(false)
}, 2000)
} }
}, [result]) }, [result])

View file

@ -2,7 +2,7 @@
// Ollama 설치·실행·모델 받기·동작 확인 안내 모달. AppLayout 이 'd3ro:open-ollama-guide' 이벤트로 연다. // Ollama 설치·실행·모델 받기·동작 확인 안내 모달. AppLayout 이 'd3ro:open-ollama-guide' 이벤트로 연다.
// v4(2026-09-28): 평면 다이얼로그 — 제목(h2) + 번호 붙은 소제목 3개 + 1px 선. 연결 상태는 실제 조회 값만 쓴다. // v4(2026-09-28): 평면 다이얼로그 — 제목(h2) + 번호 붙은 소제목 3개 + 1px 선. 연결 상태는 실제 조회 값만 쓴다.
import { useState, useEffect, useCallback } from 'react' import { useState, useEffect, useCallback, useRef } from 'react'
import { import {
Dialog, Dialog,
DialogTitle, DialogTitle,
@ -52,11 +52,25 @@ const DEFAULT_SERVER_URL = 'http://localhost:11434'
function CodeBlock({ code }: { code: string }): React.ReactElement { function CodeBlock({ code }: { code: string }): React.ReactElement {
const { t } = useI18n() const { t } = useI18n()
const [copied, setCopied] = useState(false) const [copied, setCopied] = useState(false)
const copyTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
useEffect(() => {
return () => {
if (copyTimerRef.current) {
clearTimeout(copyTimerRef.current)
copyTimerRef.current = null
}
}
}, [])
const handleCopy = useCallback(() => { const handleCopy = useCallback(() => {
navigator.clipboard.writeText(code) void navigator.clipboard.writeText(code).catch(() => {})
setCopied(true) setCopied(true)
setTimeout(() => setCopied(false), 2000) if (copyTimerRef.current) clearTimeout(copyTimerRef.current)
copyTimerRef.current = setTimeout(() => {
copyTimerRef.current = null
setCopied(false)
}, 2000)
}, [code]) }, [code])
return ( return (
@ -115,24 +129,45 @@ export function OllamaGuideModal({ open, onClose }: OllamaGuideModalProps): Reac
const [testResult, setTestResult] = useState<string | null>(null) const [testResult, setTestResult] = useState<string | null>(null)
const [testFailed, setTestFailed] = useState(false) const [testFailed, setTestFailed] = useState(false)
const [testing, setTesting] = useState(false) const [testing, setTesting] = useState(false)
const mountedRef = useRef(true)
const refreshSeqRef = useRef(0)
const startTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
useEffect(() => {
mountedRef.current = true
return () => {
mountedRef.current = false
if (startTimerRef.current) {
clearTimeout(startTimerRef.current)
startTimerRef.current = null
}
}
}, [])
const refreshStatus = useCallback(async () => { const refreshStatus = useCallback(async () => {
const seq = ++refreshSeqRef.current
const isFresh = (): boolean => mountedRef.current && refreshSeqRef.current === seq
setChecking(true) setChecking(true)
try { try {
const res = await window.electronAPI.llm.checkConnection() const res = await window.electronAPI.llm.checkConnection()
if (!isFresh()) return
if (res.success && res.data) { if (res.success && res.data) {
setInstalledModels(res.data.models) setInstalledModels(res.data.models)
} }
const st = await window.electronAPI.llm.getStatus() const st = await window.electronAPI.llm.getStatus()
if (!isFresh()) return
if (st.success) { if (st.success) {
setLlmStatus(st.data) setLlmStatus(st.data)
} }
const active = await window.electronAPI.llm.getActiveModel() const active = await window.electronAPI.llm.getActiveModel()
if (!isFresh()) return
if (active.success && active.data) { if (active.success && active.data) {
setActiveModel(active.data) setActiveModel(active.data)
} }
} catch {
// ignore IPC failure
} finally { } finally {
setChecking(false) if (isFresh()) setChecking(false)
} }
}, []) }, [])
@ -167,20 +202,27 @@ export function OllamaGuideModal({ open, onClose }: OllamaGuideModalProps): Reac
setStartMessage(null) setStartMessage(null)
try { try {
const res = await window.electronAPI.llm.startServer() const res = await window.electronAPI.llm.startServer()
if (!mountedRef.current) return
if (res.success) { if (res.success) {
if (res.data === 'running') { if (res.data === 'running') {
setStartMessage(t('ollamaGuide.start.running')) setStartMessage(t('ollamaGuide.start.running'))
} else if (res.data === 'starting') { } else if (res.data === 'starting') {
setStartMessage(t('ollamaGuide.start.starting')) setStartMessage(t('ollamaGuide.start.starting'))
setTimeout(() => refreshStatus(), 2000) if (startTimerRef.current) clearTimeout(startTimerRef.current)
startTimerRef.current = setTimeout(() => {
startTimerRef.current = null
if (mountedRef.current) void refreshStatus()
}, 2000)
} else if (res.data === 'not-installed') { } else if (res.data === 'not-installed') {
setStartMessage(t('ollamaGuide.start.notInstalled')) setStartMessage(t('ollamaGuide.start.notInstalled'))
} else { } else {
setStartMessage(t('ollamaGuide.start.failed')) setStartMessage(t('ollamaGuide.start.failed'))
} }
} }
} catch {
if (mountedRef.current) setStartMessage(t('ollamaGuide.start.failed'))
} finally { } finally {
setStarting(false) if (mountedRef.current) setStarting(false)
} }
}, [refreshStatus, t]) }, [refreshStatus, t])

View file

@ -3,7 +3,7 @@
// v4(2026-09-28): 평면 다이얼로그 + 단계 표시(n/3, 진행 막대) + "나중에 설정" 건너뛰기. // v4(2026-09-28): 평면 다이얼로그 + 단계 표시(n/3, 진행 막대) + "나중에 설정" 건너뛰기.
// 첫 실행은 Esc·배경 클릭으로 닫히지 않는다(onboarding-dismiss 정책) — 대신 명시적인 건너뛰기 버튼이 있다. // 첫 실행은 Esc·배경 클릭으로 닫히지 않는다(onboarding-dismiss 정책) — 대신 명시적인 건너뛰기 버튼이 있다.
import { useState, useEffect, useCallback } from 'react' import { useState, useEffect, useCallback, useRef } from 'react'
import { import {
Dialog, Dialog,
DialogTitle, DialogTitle,
@ -139,32 +139,58 @@ export function OnboardingModal({ open, onClose }: OnboardingModalProps): React.
const errorMsg = '' const errorMsg = ''
const isVisible = open || internalOpen const isVisible = open || internalOpen
const mountedRef = useRef(true)
const ollamaSeqRef = useRef(0)
const ollamaTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
useEffect(() => {
mountedRef.current = true
return () => {
mountedRef.current = false
if (ollamaTimerRef.current) {
clearTimeout(ollamaTimerRef.current)
ollamaTimerRef.current = null
}
}
}, [])
const checkOllama = useCallback(async () => { const checkOllama = useCallback(async () => {
const seq = ++ollamaSeqRef.current
setCheckingOllama(true) setCheckingOllama(true)
try { try {
const res = await window.electronAPI.llm.checkConnection() const res = await window.electronAPI.llm.checkConnection()
if (!mountedRef.current || ollamaSeqRef.current !== seq) return
if (res.success && res.data) { if (res.success && res.data) {
setInstalledModels(res.data.models) setInstalledModels(res.data.models)
} }
const st = await window.electronAPI.llm.getStatus() const st = await window.electronAPI.llm.getStatus()
if (!mountedRef.current || ollamaSeqRef.current !== seq) return
if (st.success) { if (st.success) {
setOllamaStatus(st.data) setOllamaStatus(st.data)
} }
} catch {
// ignore IPC failure
} finally { } finally {
setCheckingOllama(false) if (mountedRef.current && ollamaSeqRef.current === seq) setCheckingOllama(false)
} }
}, []) }, [])
useEffect(() => { useEffect(() => {
window.electronAPI.config.getAll().then((res) => { let cancelled = false
if (!res.success) return window.electronAPI.config
setOnboardingCompleted(res.data.onboardingCompleted) .getAll()
if (!res.data.onboardingCompleted) { .then((res) => {
setInternalOpen(true) if (cancelled || !res.success) return
setPhase('select_mode') setOnboardingCompleted(res.data.onboardingCompleted)
} if (!res.data.onboardingCompleted) {
}) setInternalOpen(true)
setPhase('select_mode')
}
})
.catch(() => {})
return () => {
cancelled = true
}
}, []) }, [])
// 모델 받기 — 받기에 성공한 경우에만 활성 모델로 저장 (autoSelect) // 모델 받기 — 받기에 성공한 경우에만 활성 모델로 저장 (autoSelect)
@ -192,10 +218,17 @@ export function OnboardingModal({ open, onClose }: OnboardingModalProps): React.
await window.electronAPI.config.set({ key: 'appUsageMode', value: 'local' }) await window.electronAPI.config.set({ key: 'appUsageMode', value: 'local' })
await window.electronAPI.config.set({ key: 'llmBackend', value: 'local' }) await window.electronAPI.config.set({ key: 'llmBackend', value: 'local' })
await window.electronAPI.config.set({ key: 'sttProvider', value: 'local' }) await window.electronAPI.config.set({ key: 'sttProvider', value: 'local' })
if (!mountedRef.current) return
await checkOllama() await checkOllama()
if (!mountedRef.current) return
// 자동으로 Ollama 기동 시도 // 자동으로 Ollama 기동 시도
await window.electronAPI.llm.startServer() await window.electronAPI.llm.startServer()
setTimeout(() => checkOllama(), 1500) if (!mountedRef.current) return
if (ollamaTimerRef.current) clearTimeout(ollamaTimerRef.current)
ollamaTimerRef.current = setTimeout(() => {
ollamaTimerRef.current = null
if (mountedRef.current) void checkOllama()
}, 1500)
} }
const handleStartOllama = async () => { const handleStartOllama = async () => {
@ -203,20 +236,27 @@ export function OnboardingModal({ open, onClose }: OnboardingModalProps): React.
setOllamaMsg(null) setOllamaMsg(null)
try { try {
const res = await window.electronAPI.llm.startServer() const res = await window.electronAPI.llm.startServer()
if (!mountedRef.current) return
if (res.success) { if (res.success) {
if (res.data === 'running') { if (res.data === 'running') {
setOllamaMsg(t('ollamaGuide.start.running')) setOllamaMsg(t('ollamaGuide.start.running'))
} else if (res.data === 'starting') { } else if (res.data === 'starting') {
setOllamaMsg(t('ollamaGuide.start.starting')) setOllamaMsg(t('ollamaGuide.start.starting'))
setTimeout(() => checkOllama(), 2000) if (ollamaTimerRef.current) clearTimeout(ollamaTimerRef.current)
ollamaTimerRef.current = setTimeout(() => {
ollamaTimerRef.current = null
if (mountedRef.current) void checkOllama()
}, 2000)
} else if (res.data === 'not-installed') { } else if (res.data === 'not-installed') {
setOllamaMsg(t('ollamaGuide.start.notInstalled')) setOllamaMsg(t('ollamaGuide.start.notInstalled'))
} else { } else {
setOllamaMsg(t('ollamaGuide.start.failed')) setOllamaMsg(t('ollamaGuide.start.failed'))
} }
} }
} catch {
if (mountedRef.current) setOllamaMsg(t('ollamaGuide.start.failed'))
} finally { } finally {
setStartingOllama(false) if (mountedRef.current) setStartingOllama(false)
} }
} }

Some files were not shown because too many files have changed in this diff Show more