override suspend fun prepareContext()

in agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/agent/FunctionalAIAgent.kt [81:142]


    override suspend fun prepareContext(agentInput: Input, runId: String, eventId: String): AIAgentFunctionalContext {
        val environment = GenericAgentEnvironment(
            agentId = id,
            logger = logger,
            toolRegistry = toolRegistry,
        )

        val initialLLMContext = AIAgentLLMContext(
            tools = toolRegistry.tools.map { it.descriptor },
            toolRegistry = toolRegistry,
            prompt = agentConfig.prompt,
            model = agentConfig.model,
            responseProcessor = agentConfig.responseProcessor,
            promptExecutor = promptExecutor,
            environment = environment,
            config = agentConfig,
            clock = clock
        )

        val executionInfo = AgentExecutionInfo(parent = null, partName = id)
        val preparedEnvironment = prepareEnvironment()

        // Context
        val initialAgentContext = AIAgentFunctionalContext(
            environment = preparedEnvironment,
            agentId = id,
            runId = runId,
            agentInput = agentInput,
            config = agentConfig,
            llm = initialLLMContext,
            stateManager = AIAgentStateManager(),
            storage = AIAgentStorage(),
            strategyName = strategy.name,
            pipeline = pipeline,
            executionInfo = executionInfo,
            parentContext = null
        )

        // Updated environment
        val contextualEnvironment = ContextualAgentEnvironment(
            environment = preparedEnvironment,
            context = initialAgentContext,
        )

        val contextualPromptExecutor = ContextualPromptExecutor(
            executor = promptExecutor,
            context = initialAgentContext,
        )

        val updatedLLMContext = initialAgentContext.llm.copy(
            environment = contextualEnvironment,
            promptExecutor = contextualPromptExecutor,
        )

        val updatedAgentContext = initialAgentContext.copy(
            llm = updatedLLMContext,
            environment = contextualEnvironment,
            parentRootContext = initialAgentContext.parentContext, // Keep the original parent context
        )

        return updatedAgentContext
    }