Skip to content

Commit 139dcd9

Browse files
committed
JBAI-13783. Minor refactoring in AIAgent base class
1 parent a6be628 commit 139dcd9

File tree

1 file changed

+42
-30
lines changed
  • agents/agents-core/src/commonMain/kotlin/ai/grazie/code/agents/core

1 file changed

+42
-30
lines changed

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

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,36 @@ abstract class AIAgent<TStrategy : AIAgentStrategy<TConfig>, TConfig : AIAgentCo
4545
private val eventHandler: EventHandler,
4646
val agentConfig: TConfig,
4747
) {
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+
4861
private var isRunning = false
4962
private val runningMutex = Mutex()
5063

5164
private val agentResultDeferred: CompletableDeferred<String?> = CompletableDeferred()
5265

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+
5378
/**
5479
* Executes the main agent lifecycle, handling messages between the agent and its environment.
5580
* 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
98123
}
99124
}
100125

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+
101139
private suspend fun processToolCallSingle(message: AgentToolCallSingleToEnvironmentMessage): EnvironmentToolResultSingleToAgentMessage {
102140
return EnvironmentToolResultSingleToAgentMessage(
103141
sessionUuid = message.sessionUuid,
@@ -221,12 +259,12 @@ abstract class AIAgent<TStrategy : AIAgentStrategy<TConfig>, TConfig : AIAgentCo
221259

222260
if (messageError == null) {
223261
logger.debug { "Finished execution chain, processing final result..." }
224-
check(messageContent != null) { Precondition.NO_CONTENT }
262+
check(messageContent != null) { NO_CONTENT }
225263

226-
check(messageContent.toolName == TerminationTool.NAME) { Precondition.INVALID_TOOL }
264+
check(messageContent.toolName == TerminationTool.NAME) { INVALID_TOOL }
227265

228266
val element = messageContent.toolArgs[TerminationTool.ARG]
229-
check(element != null) { Precondition.NO_RESULT }
267+
check(element != null) { NO_RESULT }
230268

231269
val result = element.jsonPrimitive.contentOrNull
232270

@@ -240,31 +278,5 @@ abstract class AIAgent<TStrategy : AIAgentStrategy<TConfig>, TConfig : AIAgentCo
240278
}
241279
}
242280

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
270282
}

0 commit comments

Comments
 (0)