@@ -45,11 +45,36 @@ abstract class AIAgent<TStrategy : AIAgentStrategy<TConfig>, TConfig : AIAgentCo
45
45
private val eventHandler : EventHandler ,
46
46
val agentConfig : TConfig ,
47
47
) {
48
+
49
+ private companion object {
50
+ private val logger = LoggerFactory .create(" ai.grazie.code.agents.core.${AIAgent ::class .simpleName!! } " )
51
+
52
+ private const val INVALID_TOOL = " Can not call tools beside \" ${TerminationTool .NAME } \" !"
53
+
54
+ private const val NO_CONTENT = " Could not find \" content\" , but \" error\" is also absent!"
55
+
56
+ private const val NO_RESULT = " Required tool argument value not found: \" ${TerminationTool .ARG } \" !"
57
+
58
+ private const val CONNECTION_REQUIRED = " Can not send messages without a connection to Agentic Engine!"
59
+ }
60
+
48
61
private var isRunning = false
49
62
private val runningMutex = Mutex ()
50
63
51
64
private val agentResultDeferred: CompletableDeferred <String ?> = CompletableDeferred ()
52
65
66
+ protected abstract suspend fun init (prompt : String ): AgentToEnvironmentMessage
67
+
68
+ protected abstract suspend fun toolResult (
69
+ toolCallId : String? ,
70
+ toolName : String ,
71
+ agentId : String ,
72
+ message : String ,
73
+ result : ToolResult ? = null
74
+ ): EnvironmentToolResultToAgentContent
75
+
76
+ protected abstract suspend fun sendToAgent (message : EnvironmentToAgentMessage ): AgentToEnvironmentMessage
77
+
53
78
/* *
54
79
* Executes the main agent lifecycle, handling messages between the agent and its environment.
55
80
* This method is allowed to be called several times, but only one execution will be active at a time.
@@ -98,6 +123,19 @@ abstract class AIAgent<TStrategy : AIAgentStrategy<TConfig>, TConfig : AIAgentCo
98
123
}
99
124
}
100
125
126
+ /* *
127
+ * Runs the agent with the given user input and retrieves the final result after the agent's execution.
128
+ *
129
+ * @param userPrompt The input string used to initialize and run the agent.
130
+ * @return A string containing the result of the agent's execution, or null if the result could not be retrieved.
131
+ */
132
+ suspend fun runAndGetResult (userPrompt : String ): String? {
133
+ run (userPrompt)
134
+ return agentResultDeferred.await()
135
+ }
136
+
137
+ // region Private Methods
138
+
101
139
private suspend fun processToolCallSingle (message : AgentToolCallSingleToEnvironmentMessage ): EnvironmentToolResultSingleToAgentMessage {
102
140
return EnvironmentToolResultSingleToAgentMessage (
103
141
sessionUuid = message.sessionUuid,
@@ -221,12 +259,12 @@ abstract class AIAgent<TStrategy : AIAgentStrategy<TConfig>, TConfig : AIAgentCo
221
259
222
260
if (messageError == null ) {
223
261
logger.debug { " Finished execution chain, processing final result..." }
224
- check(messageContent != null ) { Precondition . NO_CONTENT }
262
+ check(messageContent != null ) { NO_CONTENT }
225
263
226
- check(messageContent.toolName == TerminationTool .NAME ) { Precondition . INVALID_TOOL }
264
+ check(messageContent.toolName == TerminationTool .NAME ) { INVALID_TOOL }
227
265
228
266
val element = messageContent.toolArgs[TerminationTool .ARG ]
229
- check(element != null ) { Precondition . NO_RESULT }
267
+ check(element != null ) { NO_RESULT }
230
268
231
269
val result = element.jsonPrimitive.contentOrNull
232
270
@@ -240,31 +278,5 @@ abstract class AIAgent<TStrategy : AIAgentStrategy<TConfig>, TConfig : AIAgentCo
240
278
}
241
279
}
242
280
243
- suspend fun runAndGetResult (userPrompt : String ): String? {
244
- run (userPrompt)
245
- return agentResultDeferred.await()
246
- }
247
-
248
- protected abstract suspend fun init (prompt : String ): AgentToEnvironmentMessage
249
-
250
- protected abstract suspend fun toolResult (
251
- toolCallId : String? ,
252
- toolName : String ,
253
- agentId : String ,
254
- message : String ,
255
- result : ToolResult ? = null
256
- ): EnvironmentToolResultToAgentContent
257
-
258
- protected abstract suspend fun sendToAgent (message : EnvironmentToAgentMessage ): AgentToEnvironmentMessage
259
-
260
- private companion object {
261
- private val logger = LoggerFactory .create(" ai.grazie.code.agents.core.${AIAgent ::class .simpleName!! } " )
262
- }
263
-
264
- private object Precondition {
265
- const val INVALID_TOOL = " Can not call tools beside \" ${TerminationTool .NAME } \" !"
266
- const val NO_CONTENT = " Could not find \" content\" , but \" error\" is also absent!"
267
- const val NO_RESULT = " Required tool argument value not found: \" ${TerminationTool .ARG } \" !"
268
- const val CONNECTION_REQUIRED = " Can not send messages without a connection to Agentic Engine!"
269
- }
281
+ // endregion Private Methods
270
282
}
0 commit comments