// src/renderer/pages/tools/tools-model.ts // 2급 "도구" 화면(AI 대화·명령어·사전·지식 베이스)이 함께 쓰는 순수 판단 — React·IPC 의존 없음. // 화면이 말하는 상태는 여기서 한 번만 정한다: 조회한 설정에서 나온 사실만 쓰고, 추측을 '정상'으로 쓰지 않는다. import type { TranslationKey } from '@d3ro/i18n' import type { ConversationState, LLMAction, LLMActionSelection, LLMProcessParams } from '@d3ro/core/types' /** 지연 삭제 중인 항목을 목록에서 뺀다 */ export function withoutHidden(items: readonly T[], hidden: ReadonlySet): T[] { if (hidden.size === 0) return [...items] return items.filter((item) => !hidden.has(item.id)) } // ── 명령어: 받아쓰기 후처리 경로 ───────────────────────────── /** 명령어 목록의 "사용 안 함" 선택 값 (지시문 id 와 겹치지 않는다) */ export const COMMAND_NONE = '__none__' interface InstructionLike { id: string name: string prompt: string } /** * 지금 받아쓰기에 실제로 적용되는 후처리 — main 의 resolveVoiceLlmRoute 와 같은 규칙: * 지시문은 기본 액션이 'custom' 일 때만 쓰인다(활성 지시문이 있어도 refine 등을 덮지 않는다). */ export type CommandPipeline = | { kind: 'raw' } | { kind: 'instruction'; instruction: T } /** 'custom' 인데 활성 지시문이 없거나 지워졌다 */ | { kind: 'instruction-missing' } | { kind: 'action'; action: Exclude } export function resolveCommandPipeline( config: { defaultLLMAction: LLMActionSelection; activeInstructionId: string | null }, instructions: readonly T[], ): CommandPipeline { const action = config.defaultLLMAction if (action === 'none') return { kind: 'raw' } if (action === 'custom') { const instruction = config.activeInstructionId ? instructions.find((item) => item.id === config.activeInstructionId) : undefined return instruction ? { kind: 'instruction', instruction } : { kind: 'instruction-missing' } } return { kind: 'action', action } } /** 목록 라디오의 선택 값 — 일반 액션(다듬기 등)이 기본이면 아무것도 고르지 않은 상태다 */ export function commandSelection(pipeline: CommandPipeline): string { if (pipeline.kind === 'raw') return COMMAND_NONE if (pipeline.kind === 'instruction') return pipeline.instruction.id return '' } /** * 지운 명령이 지금도 받아쓰기에 쓰이는가 — 삭제 확정(되돌리기 알림이 닫힐 때) 시점의 설정으로 판단한다. * 삭제를 누른 순간 값으로 판단하면, 그사이 사용자가 고른 다른 명령을 '원문 그대로'로 덮었다. */ export function isInstructionInUse( config: { defaultLLMAction?: unknown; activeInstructionId?: unknown } | null | undefined, instructionId: string, ): boolean { return config?.defaultLLMAction === 'custom' && config.activeInstructionId === instructionId } /** 일반 후처리 액션 이름 */ export function llmActionLabelKey(action: Exclude): TranslationKey { switch (action) { case 'refine': return 'settings.action.refine' case 'translate': return 'settings.action.translate' case 'summarize': return 'settings.action.summarize' case 'grammar': return 'settings.action.grammar' case 'expand': return 'tools.commands.action.expand' case 'chain': return 'tools.commands.action.chain' } } /** * 테스트 실행 요청 — 실제 받아쓰기와 같은 경로를 탄다. 실행할 수 없으면 이유를 돌려준다. * (예전엔 활성 명령이 없으면 무조건 'refine' 을 돌려, 원문을 그대로 넣는 설정에서도 다듬은 결과를 보여 줬다.) */ export function commandTestRequest( pipeline: CommandPipeline, text: string, ): { ok: true; params: LLMProcessParams } | { ok: false; reason: 'raw' | 'chain' | 'missing' | 'empty' } { const trimmed = text.trim() if (!trimmed) return { ok: false, reason: 'empty' } switch (pipeline.kind) { case 'raw': return { ok: false, reason: 'raw' } case 'instruction-missing': return { ok: false, reason: 'missing' } case 'instruction': return { ok: true, params: { text: trimmed, action: 'custom', customPrompt: pipeline.instruction.prompt } } case 'action': // 체인은 여러 단계를 main 의 ChainService 가 실행한다 — 한 번의 LLM 호출로 흉내 내지 않는다 if (pipeline.action === 'chain') return { ok: false, reason: 'chain' } return { ok: true, params: { text: trimmed, action: pipeline.action } } } } // ── AI 대화: 상태 표지 ───────────────────────────────────── export interface ConversationBadge { labelKey: TranslationKey tone: 'default' | 'accent' pulse: boolean } /** * 머리의 상태 표지. 세션이 꺼져 있으면 아무것도 보이지 않는다 — 켜지지 않은 대화를 '대기'로 쓰지 않는다. * 강조색은 듣는 중(녹음)일 때만. */ export function conversationBadge(input: { active: boolean state: ConversationState connecting: boolean }): ConversationBadge | null { if (input.connecting) return { labelKey: 'conversation.realtime.connecting', tone: 'default', pulse: true } if (!input.active) return null switch (input.state) { case 'listening': return { labelKey: 'conversation.listening', tone: 'accent', pulse: true } case 'thinking': return { labelKey: 'conversation.thinking', tone: 'default', pulse: true } case 'speaking': return { labelKey: 'conversation.speaking', tone: 'default', pulse: true } case 'idle': return { labelKey: 'conversation.idle', tone: 'default', pulse: false } } } // ── 지식 베이스 ──────────────────────────────────────────── /** 질문 예시 — 누르면 그 문장으로 바로 질문한다 */ export const KNOWLEDGE_EXAMPLE_KEYS: readonly TranslationKey[] = [ 'tools.knowledge.example.summary', 'tools.knowledge.example.schedule', 'tools.knowledge.example.policy', ] /** 문서 색인 상태 — 진행 중인 문서만 '색인 중', 끝났는데 색인이 없으면 '색인 안 됨'(대기 중이라 쓰지 않는다) */ export function documentIndexLabelKey(doc: { id: string; indexed: boolean }, indexingId: string | null): TranslationKey { if (doc.id === indexingId) return 'rag.indexing' return doc.indexed ? 'rag.indexed' : 'tools.knowledge.notIndexed' } // ── 사전 ────────────────────────────────────────────────── export function dictionaryCategoryLabelKey(category: 'user' | 'auto' | 'technical'): TranslationKey { switch (category) { case 'user': return 'mobile.dictionary.category.user' case 'auto': return 'mobile.dictionary.category.auto' case 'technical': return 'mobile.dictionary.category.technical' } }