/** 배치 대화형 실행을 공통 세션에 연결한다. docs/specs/interactive-execution.md v1.0.0 */ const path = require('path'); const { prepareLaunch } = require('./utils/interactive-cli-launch'); const { runInteractive } = require('./utils/interactive-cli-session'); const { addUsage } = require('./utils/run-usage'); async function runBatchInteractive(config, prompt, cwd, logStream, owner) { // 같은 대화형 세션의 explicit→auto 전환은 helper가 좁은 언어 재설정 지시로 처리한다. // 설명 문서: /docs/specs/bada-bong-language-settings.md (v0.3.0) prompt = require('./utils/agent-reply-language').interactiveAppend(prompt, owner, {id:require('./workflow/node-context').current(owner)?.nodeId}); const conf = require('./config-manager').getConfig(); const launch = prepareLaunch({ cliCommand: config.cli_engine || require('./context').getActiveCli() || 'claude', cliArguments: config.cliArguments || [], model: config.model, profile: config.executionProfile, permissionBypass: config.permission_mode === 'bypass', maxTurns: config.max_turns, cwd, env: { ...process.env, LANGFUSE_HOST: conf.host || '', LANGFUSE_PUBLIC_KEY: conf.publicKey || '', LANGFUSE_SECRET_KEY: conf.secretKey || '' } }); const output = await runInteractive({ ...launch, owner, profile: { ...config, prompt: undefined }, prompt, signal: owner.lifecycleController?.signal || owner.abortController?.signal, isCancelled: () => owner.aborted === true, timeoutMs: (config.timeout ?? 600) * 1000, readyTimeoutMs: (config.ready_timeout ?? 30) * 1000, maxBytes: config.maxOutputBytes ?? 2 * 1024 * 1024, // 설명 문서: /docs/specs/bada-bong-agent-sources.md (v0.1.0) — 재사용 PTY도 실제 담당 연결만 관측한다. register: child => { owner.currentProcess = child; if (child?.pid) { const conductor = require('./services/agent-telemetry-conductor').current(owner); if (conductor) conductor.assigned({...config.executionProfile,name:config.executionProfile?.name || config.model || 'CLI'}); else require('./services/agent-telemetry-batch').assigned(owner, config.executionProfile); } }, unregister: child => { if (owner.currentProcess === child) owner.currentProcess = null; }, onOutput: data => logStream.write(data), onResult: ({ submitted, complete, requestId }) => { // 대화형 화면으로 provider 청구 토큰을 확정하지 않는다. 실패한 제출도 미상 호출로 기록한다. if (submitted) addUsage(owner, 0, 1, { id: requestId, costSource: 'unknown', tokenSource: 'unknown', totalTokens: null, complete }); else if (requestId) addUsage(owner, 0, 0, { id: requestId, complete, notSubmitted: true, costSource: 'not_submitted', tokenSource: 'not_submitted', totalTokens: 0 }); } }); const hint = output.match(/(?:Wrote to|Edited|Wrote|Created)\s+([^\s\r\n]+)/i)?.[1].replace(/['"`]/g, ''); return { result: 'success', output, data: {}, file_path: hint ? path.resolve(cwd, hint) : '', usageRecorded: true }; } module.exports = { runBatchInteractive };