Skip to content

Commit

Permalink
fix: better fix of AnthropicTool error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
morisil committed Oct 15, 2024
1 parent baf3d1c commit 2f02b61
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
11 changes: 5 additions & 6 deletions src/commonMain/kotlin/tool/Tools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ inline fun <reified T : UsableTool> toolOf(
serializer<T>()
} catch (e: SerializationException) {
throw SerializationException(
nonAnnotatedToolMessage<T>(), e
"Cannot find serializer for class ${T::class.qualifiedName}, " +
"make sure that it is annotated with @AnthropicTool and " +
"kotlin.serialization plugin is enabled for the project",
e
)
}

Expand All @@ -72,7 +75,7 @@ inline fun <reified T : UsableTool> toolOf(
.annotations
.filterIsInstance<AnthropicTool>()
.firstOrNull() ?: throw SerializationException(
nonAnnotatedToolMessage<T>()
"The class ${T::class.qualifiedName} must be annotated with @AnthropicTool"
)

return Tool(
Expand All @@ -82,7 +85,3 @@ inline fun <reified T : UsableTool> toolOf(
cacheControl = cacheControl
)
}

@PublishedApi
internal inline fun <reified T> nonAnnotatedToolMessage() =
"The class ${T::class.qualifiedName} must be annotated with @AnthropicTool"
3 changes: 2 additions & 1 deletion src/commonTest/kotlin/tool/UsableToolTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ class UsableToolTest {
@Test
fun shouldFailToCreateToolWithoutAnthropicToolAnnotation() {
shouldThrowWithMessage<SerializationException>(
"The class com.xemantic.anthropic.tool.UsableToolTest.NoAnnotationTool must be annotated with @AnthropicTool"
"Cannot find serializer for class com.xemantic.anthropic.tool.UsableToolTest.NoAnnotationTool, " +
"make sure that it is annotated with @AnthropicTool and kotlin.serialization plugin is enabled for the project"
) {
toolOf<NoAnnotationTool>()
}
Expand Down

0 comments on commit 2f02b61

Please sign in to comment.