d3ro-voice/apps/desktop/src/main/services/ChainService.ts
Yun Chan 99f06c253c fix(llm): stop leaking instruction prompts instead of processed text
Running a custom instruction (translate, summarise, rewrite, explain code,
free prompt) inserted the instruction's own wording instead of the result.
Two faults stacked:

The instruction was passed as the text to process, leaving the system-prompt
argument empty. `BASE_SYSTEM_PROMPTS` has no `custom` key, so resolution fell
back to `refine` without saying so, and the model dutifully polished the
instruction it had been handed. The transcript never reached it.

And only `{{text}}` was substituted, which none of the five built-in
instructions use — they carry `{{targetLanguage}}`, `{{userPrompt}}`, or no
placeholder at all. The substitution was a no-op from the day it was written:
the presets landed ten hours before the code that expected them.

- Instruction prompts now go to the system-prompt argument and the transcript
  to the text argument. Instructions that spell out `{{text}}` keep their old
  meaning, so hand-written ones still work.
- `renderInstructionPrompt` resolves `{{text}}`, `{{userPrompt}}` and
  `{{targetLanguage}}` in one place, and warns by name when a placeholder is
  left standing rather than letting it reach the model.
- `resolveSystemPrompt` no longer drops silently to `refine` for `custom`.
- Voice shortcuts no longer die at the `defaultLLMAction === 'none'` gate; an
  explicitly named instruction outranks the default. Without one, `none` still
  passes the transcript through untouched.
- `translate` receives its target language instead of relying on a default two
  call frames away. It is still always English — `AppConfig` has no key for it,
  and neither `language` (UI locale) nor `sttLanguage` (source language) can
  stand in. Choosing a target language needs a setting and is not in this fix.
- Chains ran instructions with placeholders intact; they share the same
  resolution now.
- The command screen's pipeline bench called `llm.generate`, which preload does
  not expose, so every run threw and the catch showed the input back as if it
  had succeeded. It uses `llm.process` now, over the same path production
  takes, and a failure reads as a failure.

Present since the feature shipped: the custom-instruction path has never
worked. Plain actions (refine, summarise, grammar, expand) were unaffected and
are now covered by tests so they stay that way.
2026-09-21 14:39:26 +09:00

277 lines
8 KiB
TypeScript

// src/main/services/ChainService.ts
// Phase 10.4: Multi-LLM Chain 서비스.
// LLMChain을 electron-store에 저장하고, 체인을 순차 실행한다.
import { getLogger } from './LoggerService'
import { configGet, configSet } from './ConfigService'
import { getCustomInstructionService } from './CustomInstructionService'
import { getPremiumLLMService } from './PremiumLLMService'
import { buildInstructionInvocation, resolveTargetLanguage } from './llm-prompts'
import { getMainWindow } from '../windows/WindowManager'
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
import { D3ROError, ErrorCode } from '@d3ro/core/errors'
import type {
LLMChain,
ChainStep,
CreateChainParams,
UpdateChainParams,
ChainProgress,
ChainExecutionResult
} from '@d3ro/core/types'
const logger = getLogger('chain-service')
// ============================================================
// 저장소 (electron-store, CustomInstructionService 패턴)
// ============================================================
let chains: LLMChain[] = []
let initialized = false
function loadChains(): LLMChain[] {
try {
const stored = configGet('llmChains') as LLMChain[] | undefined
if (Array.isArray(stored) && stored.length > 0) {
return stored
}
} catch {
// 첫 실행 시 키가 없을 수 있음
}
return []
}
function saveChains(): void {
try {
configSet('llmChains', chains)
} catch (error) {
logger.warn(
`Failed to save chains: ${error instanceof Error ? error.message : String(error)}`
)
}
}
/** 메인 윈도우 렌더러에 IPC 이벤트 전송 */
function sendProgressToRenderer(progress: ChainProgress): void {
const win = getMainWindow()
if (win && !win.isDestroyed()) {
win.webContents.send(IPC_CHANNELS.CHAIN.PROGRESS, progress)
}
}
// ============================================================
// ChainService
// ============================================================
class ChainService {
private _cancelRequested = false
initialize(): void {
if (initialized) return
chains = loadChains()
initialized = true
logger.info(`ChainService initialized (${chains.length} chains)`)
}
getAll(): LLMChain[] {
return [...chains]
}
getById(id: string): LLMChain | null {
return chains.find((c) => c.id === id) ?? null
}
create(params: CreateChainParams): LLMChain {
if (!params.name.trim()) {
throw new D3ROError(ErrorCode.ConfigInvalidValue, 'Chain name is empty')
}
const now = Date.now()
const chain: LLMChain = {
id: crypto.randomUUID(),
name: params.name.trim(),
steps: params.steps,
createdAt: now,
updatedAt: now
}
chains.push(chain)
saveChains()
logger.info(`Chain created: "${chain.name}" (${chain.steps.length} steps)`)
return chain
}
update(params: UpdateChainParams): LLMChain {
const index = chains.findIndex((c) => c.id === params.id)
if (index === -1) {
throw new D3ROError(ErrorCode.ChainNotFound, `Chain not found: ${params.id}`)
}
const existing = chains[index]
const updated: LLMChain = {
...existing,
name: params.name ?? existing.name,
steps: params.steps ?? existing.steps,
updatedAt: Date.now()
}
chains[index] = updated
saveChains()
logger.info(`Chain updated: "${updated.name}"`)
return updated
}
delete(id: string): void {
const index = chains.findIndex((c) => c.id === id)
if (index === -1) {
throw new D3ROError(ErrorCode.ChainNotFound, `Chain not found: ${id}`)
}
const removed = chains.splice(index, 1)[0]
saveChains()
logger.info(`Chain deleted: "${removed.name}"`)
}
/**
* 체인을 순차 실행한다.
* 각 단계마다 CustomInstruction의 프롬프트로 LLM을 호출하고,
* 이전 단계 결과를 다음 단계의 입력으로 전달한다.
*/
async execute(chainId: string, inputText: string): Promise<ChainExecutionResult> {
const chain = this.getById(chainId)
if (!chain) {
throw new D3ROError(ErrorCode.ChainNotFound, `Chain not found: ${chainId}`)
}
if (chain.steps.length === 0) {
throw new D3ROError(ErrorCode.ChainExecutionFailed, 'Chain has no steps')
}
this._cancelRequested = false
const startTime = Date.now()
const stepResults: Array<{ instructionId: string; output: string; durationMs: number }> = []
let previousOutput = inputText
const llm = getPremiumLLMService()
const instructionService = getCustomInstructionService()
const targetLanguage = resolveTargetLanguage()
logger.info(
`Executing chain "${chain.name}" (${chain.steps.length} steps) with input length ${inputText.length}`
)
for (let i = 0; i < chain.steps.length; i++) {
// 취소 확인
if (this._cancelRequested) {
logger.info(`Chain execution cancelled at step ${i + 1}/${chain.steps.length}`)
throw new D3ROError(ErrorCode.ChainCancelled, 'Chain execution cancelled')
}
const step: ChainStep = chain.steps[i]
const instruction = instructionService.getById(step.instructionId)
if (!instruction) {
throw new D3ROError(
ErrorCode.ChainStepFailed,
`Instruction not found for step ${i + 1}: ${step.instructionId}`
)
}
// 입력 소스 결정
const stepInput = step.inputSource === 'original' ? inputText : previousOutput
// 진행 상황 전송
const progress: ChainProgress = {
chainId,
currentStep: i + 1,
totalSteps: chain.steps.length,
stepName: instruction.name,
intermediateText: previousOutput
}
sendProgressToRenderer(progress)
// LLM 호출
const stepStart = Date.now()
try {
// 지시문은 시스템 프롬프트, 스텝 입력(이전 스텝 출력)은 처리 대상 텍스트.
// {{text}}를 쓰는 지시문은 치환 결과가 처리 대상 텍스트가 된다.
const invocation = buildInstructionInvocation(
instruction.prompt,
stepInput,
targetLanguage,
)
const result = await llm.processText(
invocation.text,
'custom',
undefined,
invocation.systemPrompt,
)
const stepDuration = Date.now() - stepStart
stepResults.push({
instructionId: step.instructionId,
output: result,
durationMs: stepDuration
})
previousOutput = result
logger.info(
`Chain step ${i + 1}/${chain.steps.length} ("${instruction.name}") completed in ${stepDuration}ms`
)
} catch (error) {
if (error instanceof D3ROError && error.code === ErrorCode.ChainCancelled) {
throw error
}
const msg =
error instanceof Error ? error.message : String(error)
throw new D3ROError(
ErrorCode.ChainStepFailed,
`Step ${i + 1} ("${instruction.name}") failed: ${msg}`
)
}
}
const totalDuration = Date.now() - startTime
logger.info(`Chain "${chain.name}" completed in ${totalDuration}ms`)
return {
chainId,
finalText: previousOutput,
steps: stepResults,
totalDurationMs: totalDuration
}
}
/**
* 진행 중인 체인 실행을 취소한다.
* 현재 단계가 완료된 후 다음 단계 시작 전에 중단된다.
*/
cancelExecution(): void {
this._cancelRequested = true
// LLM 생성도 취소
getPremiumLLMService().cancelGeneration()
logger.info('Chain execution cancel requested')
}
dispose(): void {
this._cancelRequested = true
logger.info('ChainService disposed')
}
}
// ============================================================
// 싱글톤
// ============================================================
let instance: ChainService | null = null
export function getChainService(): ChainService {
if (!instance) {
instance = new ChainService()
}
return instance
}
export function resetChainServiceForTests(): void {
instance = null
initialized = false
chains = []
}