Skip to content

Commit

Permalink
pushing forward
Browse files Browse the repository at this point in the history
  • Loading branch information
morisil committed Oct 9, 2024
1 parent 7338ee3 commit 94efe63
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 18 deletions.
19 changes: 4 additions & 15 deletions src/commonMain/kotlin/message/Messages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ package com.xemantic.anthropic.message

import com.xemantic.anthropic.schema.JsonSchema
import com.xemantic.anthropic.tool.UsableTool
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerializationException
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.PolymorphicKind
import kotlinx.serialization.descriptors.SerialDescriptor
Expand All @@ -16,7 +11,7 @@ import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonClassDiscriminator
import kotlinx.serialization.json.JsonDecoder
import kotlinx.serialization.serializerOrNull
import kotlinx.serialization.json.JsonObject
import kotlin.collections.mutableListOf
import kotlin.reflect.KClass

Expand Down Expand Up @@ -282,14 +277,8 @@ data class ToolUse(
override val cacheControl: CacheControl? = null,
val id: String,
val name: String,
val input: UsableTool
) : Content() {

fun use(): ToolResult = input.use(
toolUseId = id
)

}
val input: JsonObject
) : Content()

@Serializable
@SerialName("tool_result")
Expand Down
4 changes: 4 additions & 0 deletions src/commonMain/kotlin/schema/JsonSchemaGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import kotlinx.serialization.descriptors.*
import kotlin.collections.set
import kotlin.reflect.KClass

inline fun <reified T> jsonSchemaOf(): JsonSchema = generateSchema(
serializer<T>().descriptor
)

@OptIn(InternalSerializationApi::class)
fun KClass<*>.toJsonSchema(): JsonSchema = generateSchema(
serializer().descriptor
Expand Down
18 changes: 17 additions & 1 deletion src/commonMain/kotlin/tool/Tools.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.xemantic.anthropic.tool

import com.xemantic.anthropic.anthropicJson
import com.xemantic.anthropic.message.CacheControl
import com.xemantic.anthropic.message.Tool
import com.xemantic.anthropic.message.ToolResult
import com.xemantic.anthropic.message.ToolUse
import com.xemantic.anthropic.schema.toJsonSchema
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.JsonClassDiscriminator
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
Expand All @@ -16,7 +19,7 @@ annotation class Description(
val value: String
)

@JsonClassDiscriminator("type")
@JsonClassDiscriminator("name")
@OptIn(ExperimentalSerializationApi::class)
//@Serializable(with = UsableToolSerializer::class)
interface UsableTool {
Expand All @@ -27,6 +30,10 @@ interface UsableTool {

}

class ToolContentSerializer() {

}

@OptIn(InternalSerializationApi::class, ExperimentalSerializationApi::class)
fun <T : UsableTool> KClass<T>.verify() {
// TODO how to get class serializer correctly?
Expand Down Expand Up @@ -72,3 +79,12 @@ fun <T : UsableTool> List<KClass<T>>.toSerializersModule(): SerializersModule =
// inputSchema = jsonSchemaOf<T>(),
// cacheControl = cacheControl
//)


fun <T : UsableTool> ToolUse.use(
map: Map<String, KSerializer<T>>
): ToolResult {
val serializer = map[name]!!
val tool = anthropicJson.decodeFromJsonElement(serializer, input)
return tool.use(toolUseId = id)
}
1 change: 1 addition & 0 deletions src/commonTest/kotlin/AnthropicTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.xemantic.anthropic.message.Role
import com.xemantic.anthropic.message.StopReason
import com.xemantic.anthropic.message.Text
import com.xemantic.anthropic.message.ToolUse
import com.xemantic.anthropic.tool.use
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.toList
Expand Down
8 changes: 6 additions & 2 deletions src/commonTest/kotlin/message/MessagesTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.xemantic.anthropic.message

import com.xemantic.anthropic.Calculator
import com.xemantic.anthropic.anthropicJson
import com.xemantic.anthropic.testToolsSerializersModule
import com.xemantic.anthropic.tool.toSerializersModule
import io.kotest.assertions.json.shouldEqualJson
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.encodeToString
Expand All @@ -20,7 +21,7 @@ class MessagesTest {
prettyPrint = true
@OptIn(ExperimentalSerializationApi::class)
prettyPrintIndent = " "
serializersModule = testToolsSerializersModule
//serializersModule = testToolsSerializersModule
}

@Test
Expand Down Expand Up @@ -59,6 +60,9 @@ class MessagesTest {

@Test
fun shouldDeserializeToolUseRequest() {
val json = Json(from = json) {
serializersModule = listOf(Calculator::class).toSerializersModule()
}
val request = """
{
"id": "msg_01PspkNzNG3nrf5upeTsmWLF",
Expand Down

0 comments on commit 94efe63

Please sign in to comment.