Skip to content

Commit b953687

Browse files
committed
Introduced AIAgentStrategy - generic one
1 parent 928fdc1 commit b953687

File tree

6 files changed

+53
-25
lines changed

6 files changed

+53
-25
lines changed

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

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,33 @@ 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
88
import ai.koog.agents.core.agent.entity.AIAgentStorageKey
9+
import ai.koog.agents.core.annotation.InternalAgentsApi
910
import ai.koog.agents.core.environment.AIAgentEnvironment
1011
import ai.koog.agents.core.feature.AIAgentFeature
12+
import ai.koog.agents.core.feature.AIAgentNonGraphPipeline
1113
import ai.koog.prompt.message.Message
1214

15+
/**
16+
* AIAgentLoopContext represents the execution context for an AI agent operating in a loop.
17+
* It provides access to critical components such as the environment, configuration, large language model (LLM) context,
18+
* state management, and storage. Additionally, it enables the agent to store, retrieve, and manage context-specific data
19+
* during its execution lifecycle.
20+
*
21+
* @property environment The environment interface allowing the agent to interact with the external world,
22+
* including executing tools and reporting problems.
23+
* @property agentId A unique identifier for the agent, differentiating it from other agents in the system.
24+
* @property runId A unique identifier for the current run or instance of the agent's operation.
25+
* @property agentInput The input data passed to the agent, which can be of any type, depending on the agent's context.
26+
* @property config The configuration settings for the agent, including its prompt and model details,
27+
* as well as operational constraints like iteration limits.
28+
* @property llm The context for interacting with the large language model used by the agent, enabling message history
29+
* retrieval and processing.
30+
* @property stateManager The state management component responsible for tracking and updating the agent's state during its execution.
31+
* @property storage A storage interface providing persistent storage capabilities for the agent's data.
32+
* @property strategyName The name of the agent's strategic approach or operational method, determining its behavior
33+
* during execution.
34+
*/
35+
@Suppress("UNCHECKED_CAST")
1336
public class AIAgentLoopContext(
1437
override val environment: AIAgentEnvironment,
1538
override val agentId: String,
@@ -19,30 +42,32 @@ public class AIAgentLoopContext(
1942
override val llm: AIAgentLLMContext,
2043
override val stateManager: AIAgentStateManager,
2144
override val storage: AIAgentStorage,
22-
override val strategyName: String
45+
override val strategyName: String,
46+
public val pipeline: AIAgentNonGraphPipeline
2347
) : AIAgentContext {
48+
49+
private val storeMap: MutableMap<AIAgentStorageKey<*>, Any> = mutableMapOf()
50+
2451
override fun store(key: AIAgentStorageKey<*>, value: Any) {
25-
TODO("Not yet implemented")
52+
storeMap[key] = value
2653
}
2754

28-
override fun <T> get(key: AIAgentStorageKey<*>): T? {
29-
TODO("Not yet implemented")
30-
}
55+
override fun <T> get(key: AIAgentStorageKey<*>): T? = storeMap[key] as T?
3156

32-
override fun remove(key: AIAgentStorageKey<*>): Boolean {
33-
TODO("Not yet implemented")
34-
}
57+
override fun remove(key: AIAgentStorageKey<*>): Boolean = storeMap.remove(key) != null
58+
59+
@OptIn(InternalAgentsApi::class)
60+
private val features: Map<AIAgentStorageKey<*>, Any> =
61+
pipeline.getAgentFeatures(this)
3562

3663
override fun <Feature : Any> feature(key: AIAgentStorageKey<Feature>): Feature? {
37-
TODO("Not yet implemented")
64+
@Suppress("UNCHECKED_CAST")
65+
return features[key] as Feature?
3866
}
3967

40-
override fun <Feature : Any> feature(feature: AIAgentFeature<*, Feature>): Feature? {
41-
TODO("Not yet implemented")
42-
}
68+
override fun <Feature : Any> feature(feature: AIAgentFeature<*, Feature>): Feature? = feature(feature.key)
4369

4470
override suspend fun getHistory(): List<Message> {
45-
TODO("Not yet implemented")
71+
return llm.readSession { prompt.messages }
4672
}
47-
48-
}
73+
}

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

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

3+
import ai.koog.agents.core.agent.context.AIAgentGraphContextBase
34
import ai.koog.agents.core.agent.entity.AIAgentStrategy
45

56
/**
67
*
78
* */
89
public class AIAgentLoopStrategy<Input, Output>(
910
override val name: String
10-
): AIAgentStrategy<Input, Output, AIAgentLoopContext> {
11-
11+
) : AIAgentStrategy<Input, Output, AIAgentLoopContext> {
12+
override suspend fun execute(
13+
context: AIAgentGraphContextBase,
14+
input: Input
15+
): Output? {
16+
TODO("Not yet implemented")
17+
}
1218
}
13-

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
package ai.koog.agents.core.agent.entity
44

5-
import ai.koog.agents.core.agent.context.AIAgentContext
65
import ai.koog.agents.core.agent.context.AIAgentGraphContextBase
76
import ai.koog.agents.core.annotation.InternalAgentsApi
87
import ai.koog.agents.core.utils.runCatchingCancellable
98
import kotlinx.serialization.json.Json
109
import kotlinx.serialization.json.JsonElement
1110
import kotlinx.serialization.serializer
1211

13-
1412
/**
1513
* Represents a strategy for managing and executing AI agent workflows built as subgraphs of interconnected nodes.
1614
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import ai.koog.agents.core.agent.context.AIAgentGraphContextBase
1515
* @param Output The type of output data that the strategy will generate.
1616
* @param TContext The type of context in which the strategy is executed, extending [AIAgentContext].
1717
*/
18-
public interface AIAgentStrategy<Input, Output, TContext: AIAgentContext> {
18+
public interface AIAgentStrategy<Input, Output, TContext : AIAgentContext> {
1919
/**
2020
* The name of the AI agent strategy.
2121
*

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/feature/AIAgentPipeline.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public abstract class AIAgentPipeline {
212212
* @param context The context of the strategy execution
213213
*/
214214
@OptIn(InternalAgentsApi::class)
215-
public suspend fun onStrategyStarted(strategy: AIAgentStrategy, context: AIAgentContext) {
215+
public suspend fun onStrategyStarted(strategy: AIAgentStrategy<*, *, *>, context: AIAgentContext) {
216216
strategyHandlers.values.forEach { handler ->
217217
val eventContext =
218218
StrategyStartContext(runId = context.runId, strategy = strategy, feature = handler.feature)
@@ -229,7 +229,7 @@ public abstract class AIAgentPipeline {
229229
*/
230230
@OptIn(InternalAgentsApi::class)
231231
public suspend fun onStrategyFinished(
232-
strategy: AIAgentStrategy,
232+
strategy: AIAgentStrategy<*, *, *>,
233233
context: AIAgentContext,
234234
result: Any?,
235235
resultType: KType,

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/feature/handler/StrategyEventHandlerContext.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public interface StrategyEventHandlerContext : EventHandlerContext
2020
*/
2121
public class StrategyStartContext<TFeature>(
2222
public val runId: String,
23-
public val strategy: AIAgentStrategy,
23+
public val strategy: AIAgentStrategy<*, *, *>,
2424
public val feature: TFeature
2525
) : StrategyEventHandlerContext
2626

@@ -36,7 +36,7 @@ public class StrategyStartContext<TFeature>(
3636
*/
3737
public class StrategyFinishContext<TFeature>(
3838
public val runId: String,
39-
public val strategy: AIAgentStrategy,
39+
public val strategy: AIAgentStrategy<*, *, *>,
4040
public val feature: TFeature,
4141
public val result: Any?,
4242
public val resultType: KType,

0 commit comments

Comments
 (0)