Skip to content

Commit 551962d

Browse files
authored
Kotlin AI Agent simplification (#20)
* going away from session * Cleanup + Removing KotlinAIAgent + renames * Mute ToneAgentTest * CR Fixes
1 parent 3e4f286 commit 551962d

File tree

33 files changed

+412
-634
lines changed

33 files changed

+412
-634
lines changed

agents/agents-core/src/commonMain/kotlin/ai/grazie/code/agents/core/agent/AIAgentBase.kt

Lines changed: 168 additions & 110 deletions
Large diffs are not rendered by default.

agents/agents-core/src/commonMain/kotlin/ai/grazie/code/agents/core/agent/AIAgentTool.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ package ai.grazie.code.agents.core.agent
22

33
import ai.grazie.code.agents.core.agent.AIAgentTool.AgentToolArgs
44
import ai.grazie.code.agents.core.agent.AIAgentTool.AgentToolResult
5-
import ai.grazie.code.agents.core.model.agent.AIAgentConfig
6-
import ai.grazie.code.agents.core.model.agent.AIAgentStrategy
5+
import ai.grazie.code.agents.core.api.AIAgent
76
import ai.grazie.code.agents.core.tools.*
87
import kotlinx.serialization.KSerializer
98
import kotlinx.serialization.Serializable
109
import kotlinx.serialization.json.Json
1110

12-
fun <TStrategy : AIAgentStrategy<TConfig>, TConfig : AIAgentConfig> AIAgentBase<TStrategy, TConfig>.asTool(
11+
fun AIAgent.asTool(
1312
agentDescription: String,
1413
requestDescription: String = "Input for the task"
15-
) = AIAgentTool<TStrategy, TConfig>(this, requestDescription, agentDescription)
14+
) = AIAgentTool(this, requestDescription, agentDescription)
1615

1716

18-
class AIAgentTool<TStrategy : AIAgentStrategy<TConfig>, TConfig : AIAgentConfig>(
19-
val agent: AIAgentBase<TStrategy, TConfig>,
17+
class AIAgentTool(
18+
val agent: AIAgent,
2019
agentName: String,
2120
agentDescription: String,
2221
requestDescription: String = "Input for the task"

agents/agents-core/src/commonMain/kotlin/ai/grazie/code/agents/core/agent/KotlinAIAgent.kt

Lines changed: 0 additions & 141 deletions
This file was deleted.

agents/agents-core/src/commonMain/kotlin/ai/grazie/code/agents/core/api/LocalAIAgentExt.kt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ai.grazie.code.agents.core.api
22

3-
import ai.grazie.code.agents.core.agent.KotlinAIAgent
3+
import ai.grazie.code.agents.core.agent.AIAgentBase
44
import ai.grazie.code.agents.core.agent.config.LocalAgentConfig
55
import ai.grazie.code.agents.core.event.EventHandler
66
import ai.grazie.code.agents.core.tools.ToolRegistry
@@ -34,7 +34,7 @@ fun simpleChatAgent(
3434
eventHandler: EventHandler = EventHandler.NO_HANDLER,
3535
toolRegistry: ToolRegistry? = null,
3636
maxIterations: Int = 50,
37-
): KotlinAIAgent {
37+
): AIAgentBase {
3838

3939
val agentConfig = LocalAgentConfig(
4040
prompt = prompt(llmModel, "chat", params = LLMParams(temperature = temperature)) {
@@ -56,13 +56,13 @@ fun simpleChatAgent(
5656
}
5757
} with toolRegistry
5858

59-
return KotlinAIAgent(
60-
toolRegistry = resultingToolRegistry,
59+
return AIAgentBase(
60+
promptExecutor = executor,
6161
strategy = chatAgentStrategy(),
62-
eventHandler = eventHandler,
62+
cs = cs,
6363
agentConfig = agentConfig,
64-
promptExecutor = executor,
65-
cs = cs
64+
toolRegistry = resultingToolRegistry,
65+
eventHandler = eventHandler
6666
)
6767
}
6868

@@ -89,8 +89,8 @@ fun simpleSingleRunAgent(
8989
eventHandler: EventHandler = EventHandler.NO_HANDLER,
9090
toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
9191
maxIterations: Int = 50,
92-
installFeatures: suspend KotlinAIAgent.FeatureContext.() -> Unit= {}
93-
): KotlinAIAgent {
92+
installFeatures: suspend AIAgentBase.FeatureContext.() -> Unit= {}
93+
): AIAgentBase {
9494

9595
val agentConfig = LocalAgentConfig(
9696
prompt = prompt(llmModel, "chat", params = LLMParams(temperature = temperature)) {
@@ -99,13 +99,13 @@ fun simpleSingleRunAgent(
9999
maxAgentIterations = maxIterations,
100100
)
101101

102-
return KotlinAIAgent(
103-
toolRegistry = toolRegistry,
104-
strategy = singleRunStrategy(),
105-
eventHandler = eventHandler,
106-
agentConfig = agentConfig,
102+
return AIAgentBase(
107103
promptExecutor = executor,
104+
strategy = singleRunStrategy(),
108105
cs = cs,
106+
agentConfig = agentConfig,
107+
toolRegistry = toolRegistry,
108+
eventHandler = eventHandler,
109109
installFeatures = installFeatures
110110
)
111111
}

agents/agents-core/src/commonMain/kotlin/ai/grazie/code/agents/core/engine/LocalAgentSession.kt

Lines changed: 0 additions & 47 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ai.grazie.code.agents.core.environment
2+
3+
import ai.grazie.code.agents.core.agent.AgentTerminationByClientException
4+
import ai.grazie.code.agents.core.engine.UnexpectedAgentMessageException
5+
import ai.grazie.code.agents.core.engine.UnexpectedDoubleInitializationException
6+
import ai.grazie.code.agents.core.model.message.EnvironmentToAgentErrorMessage
7+
import ai.grazie.code.agents.core.model.message.EnvironmentToAgentMessage
8+
import ai.grazie.code.agents.core.model.message.EnvironmentToAgentTerminationMessage
9+
import ai.grazie.code.agents.core.model.message.EnvironmentToolResultMultipleToAgentMessage
10+
import ai.grazie.code.agents.core.model.message.EnvironmentToolResultSingleToAgentMessage
11+
import ai.grazie.code.agents.core.model.message.LocalAgentEnvironmentToAgentInitializeMessage
12+
13+
object AgentEnvironmentUtils {
14+
fun EnvironmentToAgentMessage.mapToToolResult(): List<ReceivedToolResult> {
15+
return when (this) {
16+
is EnvironmentToolResultSingleToAgentMessage -> {
17+
listOf(this.content.toResult())
18+
}
19+
20+
is EnvironmentToolResultMultipleToAgentMessage -> {
21+
this.content.map { it.toResult() }
22+
}
23+
24+
is EnvironmentToAgentErrorMessage -> {
25+
throw AgentTerminationByClientException(this.error.message)
26+
}
27+
28+
is EnvironmentToAgentTerminationMessage -> {
29+
throw AgentTerminationByClientException(
30+
this.content?.message ?: this.error?.message ?: ""
31+
)
32+
}
33+
34+
is LocalAgentEnvironmentToAgentInitializeMessage -> {
35+
throw UnexpectedDoubleInitializationException()
36+
}
37+
38+
else -> {
39+
throw UnexpectedAgentMessageException()
40+
}
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)