Skip to content

Commit f6adae5

Browse files
committed
Introduced AIAgentStrategy
1 parent 38b9b5e commit f6adae5

File tree

28 files changed

+131
-152
lines changed

28 files changed

+131
-152
lines changed

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/agent/EssentialAIAgentContext.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ai.koog.agents.core.agent
22

33
import ai.koog.agents.core.agent.config.AIAgentConfigBase
4-
import ai.koog.agents.core.agent.context.AIAgentContextBase
4+
import ai.koog.agents.core.agent.context.AIAgentContext
55
import ai.koog.agents.core.agent.context.AIAgentLLMContext
66
import ai.koog.agents.core.agent.entity.AIAgentStateManager
77
import ai.koog.agents.core.agent.entity.AIAgentStorage
@@ -22,7 +22,7 @@ public class EssentialAIAgentContext(
2222
override val stateManager: AIAgentStateManager,
2323
override val storage: AIAgentStorage,
2424
override val strategyName: String
25-
) : AIAgentContextBase {
25+
) : AIAgentContext {
2626

2727
private val storeMap: MutableMap<AIAgentStorageKey<*>, Any> = mutableMapOf()
2828

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import ai.koog.agents.core.feature.AIAgentFeature
99
import ai.koog.prompt.message.Message
1010

1111
/**
12-
* The [AIAgentContextBase] interface represents the context of an AI agent in the lifecycle.
12+
* The [AIAgentContext] interface represents the context of an AI agent in the lifecycle.
1313
* It provides access to the environment, configuration, LLM context, state management, storage, and other
1414
* metadata necessary for the operation of the agent.
1515
* Additionally, it supports features for custom workflows and extensibility.
1616
*/
17-
public interface AIAgentContextBase {
17+
public interface AIAgentContext {
1818

1919
/**
2020
* Represents the environment in which the agent operates.
@@ -153,9 +153,9 @@ public interface AIAgentContextBase {
153153
}
154154

155155
/**
156-
* Utility function to get [AIAgentContextBase.agentInput] and try to cast it to some expected type.
156+
* Utility function to get [AIAgentContext.agentInput] and try to cast it to some expected type.
157157
*
158158
* @throws ClassCastException If agent input can't be cast to [T]
159159
*/
160-
public inline fun <reified T> AIAgentContextBase.agentInput(): T =
160+
public inline fun <reified T> AIAgentContext.agentInput(): T =
161161
agentInput as? T ?: throw ClassCastException("Can't cast agent input to ${T::class}. Agent input: $agentInput")

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/agent/context/AIAgentGraphContext.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import kotlin.reflect.KType
2929
* to handle graph-specific logic, such as node traversal, input/output management,
3030
* and handling complex dependencies between graph nodes.
3131
*/
32-
public interface AIAgentGraphContextBase : AIAgentContextBase {
32+
public interface AIAgentGraphContextBase : AIAgentContext {
3333
/**
3434
* Represents the execution pipeline associated with the AI agent operating
3535
* in a graph-based context.
@@ -97,7 +97,7 @@ public interface AIAgentGraphContextBase : AIAgentContextBase {
9797
*
9898
* @param context The context to replace the current context with.
9999
*/
100-
public suspend fun replace(context: AIAgentContextBase)
100+
public suspend fun replace(context: AIAgentContext)
101101
}
102102

103103
/**
@@ -232,14 +232,14 @@ public class AIAgentGraphContext(
232232
}
233233

234234
/**
235-
* Creates a new instance of [AIAgentContextBase] with an updated list of tools, replacing the current tools
235+
* Creates a new instance of [AIAgentContext] with an updated list of tools, replacing the current tools
236236
* in the LLM context with the provided list.
237237
*
238238
* @param tools The new list of tools to be used in the LLM context, represented as [ToolDescriptor] objects.
239-
* @return A new instance of [AIAgentContextBase] with the updated tools configuration.
239+
* @return A new instance of [AIAgentContext] with the updated tools configuration.
240240
*/
241241
@InternalAgentsApi
242-
public fun copyWithTools(tools: List<ToolDescriptor>): AIAgentContextBase {
242+
public fun copyWithTools(tools: List<ToolDescriptor>): AIAgentContext {
243243
return this.copy(llm = llm.copy(tools = tools))
244244
}
245245

@@ -249,7 +249,7 @@ public class AIAgentGraphContext(
249249
stateManager = this.stateManager.copy(),
250250
)
251251

252-
override suspend fun replace(context: AIAgentContextBase) {
252+
override suspend fun replace(context: AIAgentContext) {
253253
mutableAIAgentContext.replace(
254254
context.llm,
255255
context.stateManager,
@@ -279,7 +279,7 @@ public val agentContextDataAdditionalKey: AIAgentStorageKey<AgentContextData> =
279279
* @param data The context-specific data to be stored for later retrieval or use within the agent context.
280280
*/
281281
@InternalAgentsApi
282-
public fun AIAgentContextBase.store(data: AgentContextData) {
282+
public fun AIAgentContext.store(data: AgentContextData) {
283283
this.store(agentContextDataAdditionalKey, data)
284284
}
285285

@@ -295,7 +295,7 @@ public fun AIAgentContextBase.store(data: AgentContextData) {
295295
* @return The agent context data, or null if no context data is associated.
296296
*/
297297
@InternalAgentsApi
298-
public fun AIAgentContextBase.getAgentContextData(): AgentContextData? {
298+
public fun AIAgentContext.getAgentContextData(): AgentContextData? {
299299
return this.get(agentContextDataAdditionalKey)
300300
}
301301

@@ -307,6 +307,6 @@ public fun AIAgentContextBase.getAgentContextData(): AgentContextData? {
307307
* @return `true` if the agent context data was successfully removed, or `false` if no data was found to remove.
308308
*/
309309
@OptIn(InternalAgentsApi::class)
310-
public fun AIAgentContextBase.removeAgentContextData(): Boolean {
310+
public fun AIAgentContext.removeAgentContextData(): Boolean {
311311
return this.remove(agentContextDataAdditionalKey)
312312
}

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/agent/entity/AIAgentEdge.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ai.koog.agents.core.agent.entity
22

3-
import ai.koog.agents.core.agent.context.AIAgentContextBase
3+
import ai.koog.agents.core.agent.context.AIAgentContext
44
import ai.koog.agents.core.utils.Option
55

66
/**
@@ -17,9 +17,9 @@ import ai.koog.agents.core.utils.Option
1717
*/
1818
public class AIAgentEdge<IncomingOutput, OutgoingInput> internal constructor(
1919
public val toNode: AIAgentNodeBase<OutgoingInput, *>,
20-
internal val forwardOutput: suspend (context: AIAgentContextBase, output: IncomingOutput) -> Option<OutgoingInput>,
20+
internal val forwardOutput: suspend (context: AIAgentContext, output: IncomingOutput) -> Option<OutgoingInput>,
2121
) {
2222
@Suppress("UNCHECKED_CAST")
23-
internal suspend fun forwardOutputUnsafe(output: Any?, context: AIAgentContextBase): Option<OutgoingInput> =
23+
internal suspend fun forwardOutputUnsafe(output: Any?, context: AIAgentContext): Option<OutgoingInput> =
2424
forwardOutput(context, output as IncomingOutput)
2525
}

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/agent/entity/AIAgentGraphStrategy.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public class AIAgentGraphStrategy<Input, Output>(
2727
nodeStart,
2828
nodeFinish,
2929
toolSelectionStrategy
30-
) {
30+
),
31+
AIAgentStrategy {
3132
/**
3233
* Represents the metadata of the subgraph associated with the AI agent strategy.
3334
*
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ai.koog.agents.core.agent.entity
2+
3+
import ai.koog.agents.core.agent.context.AIAgentContext
4+
5+
/**
6+
* An interface representing the execution strategy of an AI agent.
7+
*
8+
* This strategy defines how the AI agent processes input to produce
9+
* an output within the context of its operation. It serves as the primary
10+
* mechanism to encapsulate various decision-making, learning, or processing
11+
* approaches used by the agent in a flexible manner.
12+
*
13+
* @param Input The type of input data that the strategy will process.
14+
* @param Output The type of output data that the strategy will generate.
15+
* @param TContext The type of context in which the strategy is executed, extending [AIAgentContext].
16+
*/
17+
public interface AIAgentStrategy {
18+
/**
19+
* The name of the AI agent strategy.
20+
*
21+
* This property provides a human-readable identifier for the strategy,
22+
* which can be used for logging, debugging, or distinguishing between
23+
* multiple strategies within the system.
24+
*/
25+
public val name: String
26+
}

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/agent/entity/AIAgentSubgraph.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package ai.koog.agents.core.agent.entity
22

33
import ai.koog.agents.core.agent.AIAgentMaxNumberOfIterationsReachedException
44
import ai.koog.agents.core.agent.AIAgentStuckInTheNodeException
5-
import ai.koog.agents.core.agent.context.AIAgentContextBase
5+
import ai.koog.agents.core.agent.context.AIAgentContext
66
import ai.koog.agents.core.agent.context.AIAgentGraphContextBase
77
import ai.koog.agents.core.agent.context.DetachedPromptExecutorAPI
88
import ai.koog.agents.core.agent.context.getAgentContextData
@@ -84,7 +84,7 @@ public open class AIAgentSubgraph<Input, Output>(
8484
)
8585

8686
@OptIn(DetachedPromptExecutorAPI::class)
87-
private suspend fun selectTools(context: AIAgentContextBase) = when (toolSelectionStrategy) {
87+
private suspend fun selectTools(context: AIAgentContext) = when (toolSelectionStrategy) {
8888
is ToolSelectionStrategy.ALL -> context.llm.tools
8989
is ToolSelectionStrategy.NONE -> emptyList()
9090
is ToolSelectionStrategy.Tools -> toolSelectionStrategy.tools
@@ -222,7 +222,7 @@ public open class AIAgentSubgraph<Input, Output>(
222222
return result
223223
}
224224

225-
private fun formatLog(context: AIAgentContextBase, message: String): String =
225+
private fun formatLog(context: AIAgentContext, message: String): String =
226226
"$message [$name, ${context.strategyName}, ${context.runId}]"
227227
}
228228

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/dsl/builder/AIAgentEdgeBuilder.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ai.koog.agents.core.dsl.builder
22

3-
import ai.koog.agents.core.agent.context.AIAgentContextBase
3+
import ai.koog.agents.core.agent.context.AIAgentContext
44
import ai.koog.agents.core.agent.entity.AIAgentEdge
55
import ai.koog.agents.core.agent.entity.AIAgentNodeBase
66
import ai.koog.agents.core.utils.Option
@@ -57,7 +57,7 @@ public class AIAgentEdgeBuilder<IncomingOutput, OutgoingInput, CompatibleOutput
5757
public class AIAgentEdgeBuilderIntermediate<IncomingOutput, IntermediateOutput, OutgoingInput> internal constructor(
5858
internal val fromNode: AIAgentNodeBase<*, IncomingOutput>,
5959
internal val toNode: AIAgentNodeBase<OutgoingInput, *>,
60-
internal val forwardOutputComposition: suspend (AIAgentContextBase, IncomingOutput) -> Option<IntermediateOutput>
60+
internal val forwardOutputComposition: suspend (AIAgentContext, IncomingOutput) -> Option<IntermediateOutput>
6161
) {
6262
/**
6363
* Filters the intermediate outputs of the [ai.koog.agents.core.agent.entity.AIAgentNode] based on a specified condition.
@@ -69,7 +69,7 @@ public class AIAgentEdgeBuilderIntermediate<IncomingOutput, IntermediateOutput,
6969
*/
7070
@EdgeTransformationDslMarker
7171
public infix fun onCondition(
72-
block: suspend AIAgentContextBase.(output: IntermediateOutput) -> Boolean
72+
block: suspend AIAgentContext.(output: IntermediateOutput) -> Boolean
7373
): AIAgentEdgeBuilderIntermediate<IncomingOutput, IntermediateOutput, OutgoingInput> {
7474
return AIAgentEdgeBuilderIntermediate(
7575
fromNode = fromNode,
@@ -90,7 +90,7 @@ public class AIAgentEdgeBuilderIntermediate<IncomingOutput, IntermediateOutput,
9090
*/
9191
@EdgeTransformationDslMarker
9292
public infix fun <NewIntermediateOutput> transformed(
93-
block: suspend AIAgentContextBase.(IntermediateOutput) -> NewIntermediateOutput
93+
block: suspend AIAgentContext.(IntermediateOutput) -> NewIntermediateOutput
9494
): AIAgentEdgeBuilderIntermediate<IncomingOutput, NewIntermediateOutput, OutgoingInput> {
9595
return AIAgentEdgeBuilderIntermediate(
9696
fromNode = fromNode,

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/dsl/builder/AIAgentNodeBuilder.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ai.koog.agents.core.dsl.builder
22

3-
import ai.koog.agents.core.agent.context.AIAgentContextBase
3+
import ai.koog.agents.core.agent.context.AIAgentContext
44
import ai.koog.agents.core.agent.context.AIAgentGraphContextBase
55
import ai.koog.agents.core.agent.entity.AIAgentNode
66
import ai.koog.agents.core.agent.entity.AIAgentNodeBase
@@ -21,7 +21,7 @@ import kotlin.reflect.KType
2121
* @param outputType [KType] of the [Output]
2222
* @constructor Used internally to create a new builder with the provided execution logic.
2323
* @param execute A suspending function to define the execution logic of the node. This function will be called
24-
* in the scope of [AIAgentContextBase], where it has access to the AI agent's context and tools relevant
24+
* in the scope of [AIAgentContext], where it has access to the AI agent's context and tools relevant
2525
* to its operation.
2626
*/
2727
public open class AIAgentNodeBuilder<Input, Output>(

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/dsl/builder/AIAgentParallelNodesMergeContext.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ai.koog.agents.core.dsl.builder
22

33
import ai.koog.agents.core.agent.config.AIAgentConfigBase
4-
import ai.koog.agents.core.agent.context.AIAgentContextBase
4+
import ai.koog.agents.core.agent.context.AIAgentContext
55
import ai.koog.agents.core.agent.context.AIAgentGraphContextBase
66
import ai.koog.agents.core.agent.context.AIAgentLLMContext
77
import ai.koog.agents.core.agent.entity.AIAgentStateManager
@@ -109,7 +109,7 @@ public class AIAgentParallelNodesMergeContext<Input, Output>(
109109
* @param context The new context to replace the current one in the underlying context base.
110110
* @return Unit
111111
*/
112-
override suspend fun replace(context: AIAgentContextBase): Unit = underlyingContextBase.replace(context)
112+
override suspend fun replace(context: AIAgentContext): Unit = underlyingContextBase.replace(context)
113113

114114
/**
115115
* Selects a result based on a predicate.

0 commit comments

Comments
 (0)