@@ -3,8 +3,6 @@ package ai.koog.agents.core.agent.entity
33import ai.koog.agents.core.agent.context.AIAgentContext
44import ai.koog.agents.core.agent.context.AIAgentGraphContextBase
55import ai.koog.agents.core.agent.context.DetachedPromptExecutorAPI
6- import ai.koog.agents.core.agent.context.element.NodeInfoContextElement
7- import ai.koog.agents.core.agent.context.element.getNodeInfoElement
86import ai.koog.agents.core.agent.context.getAgentContextData
97import ai.koog.agents.core.agent.context.store
108import ai.koog.agents.core.agent.context.with
@@ -26,11 +24,9 @@ import ai.koog.prompt.structure.json.JsonStructure
2624import ai.koog.prompt.structure.json.generator.StandardJsonSchemaGenerator
2725import io.github.oshai.kotlinlogging.KotlinLogging
2826import kotlinx.coroutines.CancellationException
29- import kotlinx.coroutines.withContext
3027import kotlinx.serialization.Serializable
3128import kotlin.reflect.KType
3229import kotlin.uuid.ExperimentalUuidApi
33- import kotlin.uuid.Uuid
3430
3531/* *
3632 * [AIAgentSubgraph] represents a structured subgraph within an AI agent workflow. It serves as a logical
@@ -163,61 +159,59 @@ public open class AIAgentSubgraph<TInput, TOutput>(
163159 @OptIn(InternalAgentsApi ::class , DetachedPromptExecutorAPI ::class , ExperimentalUuidApi ::class )
164160 override suspend fun execute (context : AIAgentGraphContextBase , input : TInput ): TOutput ? =
165161 context.with { executionInfo, eventId ->
166- withContext(NodeInfoContextElement (Uuid .random().toString(), getNodeInfoElement()?.id, name, input, inputType)) {
167- val newTools = selectTools(context)
168-
169- // Copy inner context with new tools, model and LLM params.
170- val initialLLMContext = context.llm
171-
172- context.replace(
173- context.copy(
174- llm = context.llm.copy(
175- tools = newTools,
176- model = llmModel ? : context.llm.model,
177- prompt = context.llm.prompt.copy(params = llmParams ? : context.llm.prompt.params),
178- responseProcessor = responseProcessor
179- ),
162+ val newTools = selectTools(context)
163+
164+ // Copy inner context with new tools, model and LLM params.
165+ val initialLLMContext = context.llm
166+
167+ context.replace(
168+ context.copy(
169+ llm = context.llm.copy(
170+ tools = newTools,
171+ model = llmModel ? : context.llm.model,
172+ prompt = context.llm.prompt.copy(params = llmParams ? : context.llm.prompt.params),
173+ responseProcessor = responseProcessor
180174 ),
181- )
175+ ),
176+ )
182177
183- runIfNotStrategy(context) {
184- pipeline.onSubgraphExecutionStarting(eventId, executionInfo, this @AIAgentSubgraph, context, input, inputType)
185- }
178+ runIfNotStrategy(context) {
179+ pipeline.onSubgraphExecutionStarting(eventId, executionInfo, this @AIAgentSubgraph, context, input, inputType)
180+ }
186181
187- // Execute the subgraph with an inner context and get the result and updated prompt.
188- val result = try {
189- executeWithInnerContext(context, input)
190- } catch (e: CancellationException ) {
191- throw e
192- } catch (e: Exception ) {
193- logger.error(e) { " Exception during executing subgraph '$name ': ${e.message} " }
194- runIfNotStrategy(context) {
195- pipeline.onSubgraphExecutionFailed(eventId, executionInfo, this @AIAgentSubgraph, context, input, inputType, e)
196- }
197- throw e
182+ // Execute the subgraph with an inner context and get the result and updated prompt.
183+ val result = try {
184+ executeWithInnerContext(context, input)
185+ } catch (e: CancellationException ) {
186+ throw e
187+ } catch (e: Exception ) {
188+ logger.error(e) { " Exception during executing subgraph '$name ': ${e.message} " }
189+ runIfNotStrategy(context) {
190+ pipeline.onSubgraphExecutionFailed(eventId, executionInfo, this @AIAgentSubgraph, context, input, inputType, e)
198191 }
192+ throw e
193+ }
199194
200- // Restore original LLM context with updated message history.
201- context.replace(
202- context.copy(
203- llm = initialLLMContext.copy(
204- prompt = context.llm.prompt.copy(params = initialLLMContext.prompt.params)
205- ),
195+ // Restore original LLM context with updated message history.
196+ context.replace(
197+ context.copy(
198+ llm = initialLLMContext.copy(
199+ prompt = context.llm.prompt.copy(params = initialLLMContext.prompt.params)
206200 ),
207- )
201+ ),
202+ )
208203
209- val innerForcedData = context.getAgentContextData()
210-
211- if (innerForcedData != null ) {
212- context.store(innerForcedData)
213- }
204+ val innerForcedData = context.getAgentContextData()
214205
215- runIfNotStrategy(context ) {
216- pipeline.onSubgraphExecutionCompleted(eventId, executionInfo, this @AIAgentSubgraph, context, input, inputType, result, outputType )
217- }
206+ if (innerForcedData != null ) {
207+ context.store(innerForcedData )
208+ }
218209
219- result
210+ runIfNotStrategy(context) {
211+ pipeline.onSubgraphExecutionCompleted(eventId, executionInfo, this @AIAgentSubgraph, context, input, inputType, result, outputType)
220212 }
213+
214+ result
221215 }
222216
223217 @OptIn(InternalAgentsApi ::class )
0 commit comments