import{createLogger}from"#internal/logging.js";import{createActionResultEvent,createActionsRequestedEvent}from"#protocol/message.js";import{toErrorMessage}from"#shared/errors.js";import{createRuntimeActionRequestFromToolCall}from"#harness/runtime-actions.js";import{createRuntimeToolResultFromValue}from"#harness/action-result-helpers.js";const log=createLogger(`harness.code-mode-lifecycle`);function createCodeModeLifecycle(e){return{onHookError(e,t){log.warn(`code-mode lifecycle hook failed`,{error:e,hook:t.hook})},async onNestedToolCall(t){if(e.skipReplayed===!0&&t.replayed)return;let r={input:t.input,toolCallId:t.toolCallId,toolName:t.toolName,type:`tool-call`};await e.emit(createActionsRequestedEvent({actions:[createRuntimeActionRequestFromToolCall({toolCall:r,tools:e.tools})],sequence:e.emissionState.sequence,stepIndex:e.emissionState.stepIndex,turnId:e.emissionState.turnId}))},async onNestedToolResult(n){if(e.skipReplayed===!0&&n.replayed||n.status===`interrupted`)return;let i=createRuntimeToolResultFromValue({callId:n.toolCallId,output:n.status===`rejected`?toErrorMessage(n.error):n.output,toolName:n.toolName,isError:n.status===`rejected`});await e.emit(createActionResultEvent({result:i,sequence:e.emissionState.sequence,stepIndex:e.emissionState.stepIndex,turnId:e.emissionState.turnId}))}}}export{createCodeModeLifecycle};