Skip to content

Commit ba70f36

Browse files
committed
Update agent exceptions
- Extract runtime exceptions; - Extract Agent exceptions.
1 parent 33ea7ed commit ba70f36

File tree

7 files changed

+59
-54
lines changed

7 files changed

+59
-54
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import ai.grazie.code.agents.core.environment.ReceivedToolResult
99
import ai.grazie.code.agents.core.environment.TerminationTool
1010
import ai.grazie.code.agents.core.event.AgentHandlerContext
1111
import ai.grazie.code.agents.core.event.EventHandler
12-
import ai.grazie.code.agents.core.exception.AIAgentEngineException
12+
import ai.grazie.code.agents.core.exception.AgentEngineException
1313
import ai.grazie.code.agents.core.feature.AIAgentPipeline
1414
import ai.grazie.code.agents.core.feature.KotlinAIAgentFeature
1515
import ai.grazie.code.agents.core.feature.config.FeatureConfig
16-
import ai.grazie.code.agents.core.model.AIAgentServiceError
17-
import ai.grazie.code.agents.core.model.AIAgentServiceErrorType
16+
import ai.grazie.code.agents.core.model.AgentServiceError
17+
import ai.grazie.code.agents.core.model.AgentServiceErrorType
1818
import ai.grazie.code.agents.core.model.message.*
1919
import ai.grazie.code.agents.core.tools.*
2020
import ai.grazie.code.agents.core.tools.annotations.InternalAgentToolsApi
@@ -181,8 +181,8 @@ open class AIAgentBase(
181181
override suspend fun reportProblem(exception: Throwable) {
182182
logger.error(exception) { formatLog("Reporting problem: ${exception.message}") }
183183
processError(
184-
AIAgentServiceError(
185-
type = AIAgentServiceErrorType.UNEXPECTED_ERROR,
184+
AgentServiceError(
185+
type = AgentServiceErrorType.UNEXPECTED_ERROR,
186186
message = exception.message ?: "unknown error"
187187
)
188188
)
@@ -331,10 +331,10 @@ open class AIAgentBase(
331331
}
332332
}
333333

334-
private suspend fun processError(error: AIAgentServiceError) {
334+
private suspend fun processError(error: AgentServiceError) {
335335
try {
336336
throw error.asException()
337-
} catch (e: AIAgentEngineException) {
337+
} catch (e: AgentEngineException) {
338338
logger.error(e) { "Execution exception reported by server!" }
339339

340340
with(eventHandler.errorHandler) {

agents/agents-core/src/commonMain/kotlin/ai/grazie/code/agents/core/agent/Exceptions.kt renamed to agents/agents-core/src/commonMain/kotlin/ai/grazie/code/agents/core/agent/AgentException.kt

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

33
import ai.grazie.code.agents.core.agent.entity.LocalAgentNode
44

5-
6-
open class LocalAgentException(problem: String) : Exception("Local AI Agent has run into a problem: $problem")
5+
open class AgentException(problem: String, throwable: Throwable? = null) :
6+
Throwable("AI Agent has run into a problem: $problem", throwable)
77

88
class AgentStuckInTheNodeException(node: LocalAgentNode<*, *>, output: Any?) :
9-
LocalAgentException(
10-
"when executing agent graph, stuck in node ${node.name} " +
9+
AgentException(
10+
"When executing agent graph, stuck in node ${node.name} " +
1111
"because output $output doesn't match any condition on available edges."
1212
)
1313

1414
class AgentMaxNumberOfIterationsReachedException(maxNumberOfIterations: Int) :
15-
LocalAgentException(
16-
"agent couldn't finish in given number of steps ($maxNumberOfIterations). " +
15+
AgentException(
16+
"Agent couldn't finish in given number of steps ($maxNumberOfIterations). " +
1717
"Please, consider increasing `maxAgentIterations` value in agent's configuration"
1818
)
1919

20-
class AgentTerminationByClientException(message: String) : LocalAgentException("agent was canceled by the client ($message)")
20+
class AgentTerminationByClientException(message: String) :
21+
AgentException("Agent was canceled by the client ($message)")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ai.grazie.code.agents.core.exception
2+
3+
/**
4+
* Base class for all agent runtime exceptions.
5+
*/
6+
sealed class AgentRuntimeException(message: String) : RuntimeException(message)
7+
8+
/**
9+
* Thrown when the [ai.grazie.code.agents.core.tools.ToolRegistry] cannot locate the requested [ai.grazie.code.agents.core.tools.Tool] for execution.
10+
*
11+
* @param name Name of the tool that was not found.
12+
*/
13+
class ToolNotRegisteredException(name: String) : AgentRuntimeException("Tool not registered: \"$name\"")
14+
15+
/**
16+
* Base class for representing an [ai.grazie.code.agents.core.model.AgentServiceError] response from the server.
17+
*/
18+
sealed class AgentEngineException(message: String) : AgentRuntimeException(message)
19+
20+
class UnexpectedServerException(message: String) : AgentEngineException(message)
21+
22+
class UnexpectedMessageTypeException(message: String) : AgentEngineException(message)
23+
24+
class MalformedMessageException(message: String) : AgentEngineException(message)
25+
26+
class AgentNotFoundException(message: String) : AgentEngineException(message)

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

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

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ import ai.grazie.code.agents.core.exception.*
44
import kotlinx.serialization.Serializable
55

66
@Serializable
7-
enum class AIAgentServiceErrorType {
7+
enum class AgentServiceErrorType {
88
UNEXPECTED_MESSAGE_TYPE,
99
MALFORMED_MESSAGE,
1010
AGENT_NOT_FOUND,
1111
UNEXPECTED_ERROR,
1212
}
1313

1414
@Serializable
15-
data class AIAgentServiceError(
16-
val type: AIAgentServiceErrorType,
15+
data class AgentServiceError(
16+
val type: AgentServiceErrorType,
1717
val message: String,
1818
) {
19-
fun asException(): AIAgentEngineException {
19+
fun asException(): AgentEngineException {
2020
return when (type) {
21-
AIAgentServiceErrorType.UNEXPECTED_ERROR -> UnexpectedServerException(message)
22-
AIAgentServiceErrorType.UNEXPECTED_MESSAGE_TYPE -> UnexpectedMessageTypeException(message)
23-
AIAgentServiceErrorType.MALFORMED_MESSAGE -> MalformedMessageException(message)
24-
AIAgentServiceErrorType.AGENT_NOT_FOUND -> AgentNotFoundException(message)
21+
AgentServiceErrorType.UNEXPECTED_ERROR -> UnexpectedServerException(message)
22+
AgentServiceErrorType.UNEXPECTED_MESSAGE_TYPE -> UnexpectedMessageTypeException(message)
23+
AgentServiceErrorType.MALFORMED_MESSAGE -> MalformedMessageException(message)
24+
AgentServiceErrorType.AGENT_NOT_FOUND -> AgentNotFoundException(message)
2525
}
2626
}
2727
}

agents/agents-core/src/commonMain/kotlin/ai/grazie/code/agents/core/model/message/AgentToEnvironment.kt

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

3-
import ai.grazie.code.agents.core.model.AIAgentServiceError
3+
import ai.grazie.code.agents.core.model.AgentServiceError
44
import ai.grazie.utils.mpp.UUID
55
import kotlinx.serialization.SerialName
66
import kotlinx.serialization.Serializable
@@ -70,17 +70,17 @@ data class AgentToolCallsToEnvironmentMessage(
7070
data class AgentTerminationToEnvironmentMessage(
7171
override val sessionUuid: UUID,
7272
val content: AgentToolCallToEnvironmentContent? = null,
73-
val error: AIAgentServiceError? = null,
73+
val error: AgentServiceError? = null,
7474
) : AgentToEnvironmentMessage
7575

7676
/**
7777
* Represents an error response from the server.
7878
* These may occur for several reasons:
7979
*
80-
* - [Sending unsupported types of messages][ai.grazie.code.agents.core.model.AIAgentServiceErrorType.UNEXPECTED_MESSAGE_TYPE];
81-
* - [Sending incorrect or incomplete messages][ai.grazie.code.agents.core.model.AIAgentServiceErrorType.MALFORMED_MESSAGE];
82-
* - [Trying to use an agent that is not available][ai.grazie.code.agents.core.model.AIAgentServiceErrorType.AGENT_NOT_FOUND];
83-
* - [Other, unexpected errors][ai.grazie.code.agents.core.model.AIAgentServiceErrorType.UNEXPECTED_ERROR].
80+
* - [Sending unsupported types of messages][ai.grazie.code.agents.core.model.AgentServiceErrorType.UNEXPECTED_MESSAGE_TYPE];
81+
* - [Sending incorrect or incomplete messages][ai.grazie.code.agents.core.model.AgentServiceErrorType.MALFORMED_MESSAGE];
82+
* - [Trying to use an agent that is not available][ai.grazie.code.agents.core.model.AgentServiceErrorType.AGENT_NOT_FOUND];
83+
* - [Other, unexpected errors][ai.grazie.code.agents.core.model.AgentServiceErrorType.UNEXPECTED_ERROR].
8484
*
8585
* @property sessionUuid Unique identifier for the session.
8686
* @property error Error details.
@@ -89,5 +89,5 @@ data class AgentTerminationToEnvironmentMessage(
8989
@SerialName("ERROR")
9090
data class AgentErrorToEnvironmentMessage(
9191
override val sessionUuid: UUID,
92-
val error: AIAgentServiceError
92+
val error: AgentServiceError
9393
) : AgentToEnvironmentMessage

agents/agents-core/src/commonMain/kotlin/ai/grazie/code/agents/core/model/message/EnvironmentToAgent.kt

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

3-
import ai.grazie.code.agents.core.model.AIAgentServiceError
3+
import ai.grazie.code.agents.core.model.AgentServiceError
44
import ai.grazie.utils.mpp.UUID
55
import kotlinx.serialization.EncodeDefault
66
import kotlinx.serialization.SerialName
@@ -117,7 +117,7 @@ data class EnvironmentToAgentTerminationContent(
117117
data class EnvironmentToAgentTerminationMessage(
118118
val sessionUuid: UUID,
119119
val content: EnvironmentToAgentTerminationContent? = null,
120-
val error: AIAgentServiceError? = null,
120+
val error: AgentServiceError? = null,
121121
) : EnvironmentToAgentMessage
122122

123123
/**
@@ -133,5 +133,5 @@ data class EnvironmentToAgentTerminationMessage(
133133
@SerialName("ERROR")
134134
data class EnvironmentToAgentErrorMessage(
135135
val sessionUuid: UUID,
136-
val error: AIAgentServiceError,
136+
val error: AgentServiceError,
137137
) : EnvironmentToAgentMessage

0 commit comments

Comments
 (0)