-
-
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.
* add direct browser access to client headers
* single shared anthropicJson instance for consistency * tool test updated * JsonSchema support
- Loading branch information
Showing
7 changed files
with
301 additions
and
69 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,31 @@ | ||
package com.xemantic.anthropic.schema | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class JsonSchema( | ||
val type: String = "object", | ||
val definitions: Map<String, JsonSchema>? = null, | ||
val properties: Map<String, JsonSchemaProperty>? = null, | ||
val required: List<String>? = null, | ||
@SerialName("\$ref") | ||
var ref: String? = null | ||
) | ||
|
||
@Serializable | ||
data class JsonSchemaProperty( | ||
val type: String? = null, | ||
val items: JsonSchemaProperty? = null, | ||
val enum: List<String>? = null, | ||
val ref: String? = null | ||
) { | ||
|
||
companion object { | ||
val STRING = JsonSchemaProperty("string") | ||
val INTEGER = JsonSchemaProperty("integer") | ||
val NUMBER = JsonSchemaProperty("number") | ||
val BOOLEAN = JsonSchemaProperty("boolean") | ||
} | ||
|
||
} |
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,75 @@ | ||
package com.xemantic.anthropic.schema | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.descriptors.* | ||
import kotlin.collections.set | ||
|
||
inline fun <reified T> jsonSchemaOf(): JsonSchema = generateSchema( | ||
serializer<T>().descriptor | ||
) | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
fun generateSchema(descriptor: SerialDescriptor): JsonSchema { | ||
val properties = mutableMapOf<String, JsonSchemaProperty>() | ||
val required = mutableListOf<String>() | ||
val definitions = mutableMapOf<String, JsonSchema>() | ||
|
||
for (i in 0 until descriptor.elementsCount) { | ||
val name = descriptor.getElementName(i) | ||
val elementDescriptor = descriptor.getElementDescriptor(i) | ||
val property = generateSchemaProperty(elementDescriptor, definitions) | ||
properties[name] = property | ||
if (!descriptor.isElementOptional(i)) { | ||
required.add(name) | ||
} | ||
} | ||
|
||
return JsonSchema( | ||
type = "object", | ||
properties = properties, | ||
required = required, | ||
definitions = if (definitions.isNotEmpty()) definitions else null | ||
) | ||
} | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
private fun generateSchemaProperty( | ||
descriptor: SerialDescriptor, | ||
definitions: MutableMap<String, JsonSchema> | ||
): JsonSchemaProperty { | ||
return when (descriptor.kind) { | ||
PrimitiveKind.STRING -> JsonSchemaProperty.STRING | ||
PrimitiveKind.INT, PrimitiveKind.LONG -> JsonSchemaProperty.INTEGER | ||
PrimitiveKind.FLOAT, PrimitiveKind.DOUBLE -> JsonSchemaProperty.NUMBER | ||
PrimitiveKind.BOOLEAN -> JsonSchemaProperty.BOOLEAN | ||
SerialKind.ENUM -> enumProperty(descriptor) | ||
StructureKind.LIST -> JsonSchemaProperty( | ||
type = "array", | ||
items = generateSchemaProperty( | ||
descriptor.getElementDescriptor(0), | ||
definitions | ||
) | ||
) | ||
StructureKind.MAP -> JsonSchemaProperty("object") | ||
StructureKind.CLASS -> { | ||
val refName = descriptor.serialName.trimEnd('?') | ||
definitions[refName] = generateSchema(descriptor) | ||
JsonSchemaProperty("\$ref", ref = "#/definitions/$refName") | ||
} | ||
else -> JsonSchemaProperty("object") // Default case | ||
} | ||
} | ||
|
||
private fun enumProperty( | ||
descriptor: SerialDescriptor | ||
) = JsonSchemaProperty( | ||
enum = descriptor.elementNames() | ||
) | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
private fun SerialDescriptor.elementNames(): List<String> = buildList { | ||
for (i in 0 until elementsCount) { | ||
val name = getElementName(i) | ||
add(name) | ||
} | ||
} |
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
Oops, something went wrong.