-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document content type support + organizing content types in new conte…
…nt package + refactoring
- Loading branch information
Showing
29 changed files
with
574 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.xemantic.anthropic.content | ||
|
||
import com.xemantic.anthropic.cache.CacheControl | ||
import com.xemantic.anthropic.message.Content | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
@SerialName("document") | ||
data class Document( | ||
val source: Source, | ||
@SerialName("cache_control") | ||
override val cacheControl: CacheControl? = null | ||
) : Content() { | ||
|
||
enum class MediaType { | ||
@SerialName("application/pdf") | ||
APPLICATION_PDF | ||
} | ||
|
||
@Serializable | ||
data class Source( | ||
val type: Type = Type.BASE64, | ||
@SerialName("media_type") | ||
val mediaType: MediaType, | ||
val data: String | ||
) { | ||
|
||
enum class Type { | ||
@SerialName("base64") | ||
BASE64 | ||
} | ||
|
||
} | ||
|
||
class Builder { | ||
var data: ByteArray? = null | ||
var mediaType: MediaType? = null | ||
var cacheControl: CacheControl? = null | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
src/commonMain/kotlin/image/Images.kt → src/commonMain/kotlin/content/Image.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/commonMain/kotlin/text/Text.kt → src/commonMain/kotlin/content/Text.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package com.xemantic.anthropic.content | ||
|
||
import com.xemantic.anthropic.anthropicJson | ||
import com.xemantic.anthropic.cache.CacheControl | ||
import com.xemantic.anthropic.message.Content | ||
import com.xemantic.anthropic.message.toNullIfEmpty | ||
import com.xemantic.anthropic.tool.Tool | ||
import com.xemantic.anthropic.tool.ToolInput | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.Transient | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlin.collections.plus | ||
import kotlin.contracts.ExperimentalContracts | ||
import kotlin.contracts.InvocationKind | ||
import kotlin.contracts.contract | ||
|
||
@Serializable | ||
@SerialName("tool_use") | ||
data class ToolUse( | ||
val id: String, | ||
val name: String, | ||
val input: JsonObject, | ||
@SerialName("cache_control") | ||
override val cacheControl: CacheControl? = null | ||
) : Content() { | ||
|
||
@Transient | ||
@PublishedApi | ||
internal lateinit var tool: Tool | ||
|
||
@PublishedApi | ||
internal fun decodeInput() = anthropicJson.decodeFromJsonElement( | ||
deserializer = tool.inputSerializer, | ||
element = input | ||
).apply(tool.inputInitializer) | ||
|
||
inline fun <reified T : ToolInput> input(): T = (decodeInput() as T) | ||
|
||
suspend fun use(): ToolResult { | ||
return try { | ||
if (::tool.isInitialized) { | ||
val toolInput = decodeInput() | ||
toolInput.use(toolUseId = id) | ||
} else { | ||
ToolResult(toolUseId = id) { | ||
error("Cannot use unknown tool: $name") | ||
} | ||
} | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
ToolResult(toolUseId = id) { | ||
error(e.message ?: "Unknown error occurred") | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
@Serializable | ||
@SerialName("tool_result") | ||
data class ToolResult( | ||
@SerialName("tool_use_id") | ||
val toolUseId: String, | ||
val content: List<Content>? = null, | ||
@SerialName("is_error") | ||
val isError: Boolean? = false, | ||
@SerialName("cache_control") | ||
override val cacheControl: CacheControl? = null | ||
) : Content() { | ||
|
||
class Builder { | ||
|
||
var content: List<Content> = emptyList() | ||
var isError: Boolean? = null | ||
var cacheControl: CacheControl? = null | ||
|
||
fun content(message: String) { | ||
content = listOf(Text(text = message)) | ||
} | ||
|
||
fun error(message: String) { | ||
content(message) | ||
isError = true | ||
} | ||
|
||
operator fun List<Content>.unaryPlus() { | ||
content += this | ||
} | ||
|
||
operator fun String.unaryPlus() { | ||
content(this) | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
@OptIn(ExperimentalContracts::class) | ||
fun ToolResult( | ||
toolUseId: String, | ||
block: ToolResult.Builder.() -> Unit = {} | ||
): ToolResult { | ||
contract { | ||
callsInPlace(block, InvocationKind.EXACTLY_ONCE) | ||
} | ||
val builder = ToolResult.Builder() | ||
builder.apply(block) | ||
return ToolResult( | ||
toolUseId = toolUseId, | ||
content = builder.content.toNullIfEmpty(), | ||
isError = if (builder.isError == null) false else null, | ||
cacheControl = builder.cacheControl | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.