1
+ /*
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2025 https://github.com/aallam/openai-kotlin
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ package cc.unitmesh.devti.llms.call
26
+
27
+ import kotlinx.serialization.KSerializer
28
+ import kotlinx.serialization.SerialName
29
+ import kotlinx.serialization.Serializable
30
+ import kotlinx.serialization.descriptors.SerialDescriptor
31
+ import kotlinx.serialization.encoding.Decoder
32
+ import kotlinx.serialization.encoding.Encoder
33
+ import kotlinx.serialization.json.*
34
+
35
+ @JvmInline
36
+ @Serializable
37
+ public value class ToolType (public val value : String ) {
38
+ public companion object {
39
+ public val Function : ToolType = ToolType (" function" )
40
+ }
41
+ }
42
+
43
+ @Serializable
44
+ data class FunctionToolTool (
45
+ @SerialName(" type" ) val type : ToolType ,
46
+ @SerialName(" function" ) val function : FunctionTool ,
47
+ )
48
+
49
+ @Serializable
50
+ public data class FunctionTool (
51
+ @SerialName(" name" ) val name : String ,
52
+ @SerialName(" parameters" ) val parameters : Parameters ? = null ,
53
+ @SerialName(" description" ) public val description : String? = null
54
+ )
55
+
56
+ @Serializable(with = Parameters .JsonDataSerializer ::class )
57
+ public data class Parameters (public val schema : JsonElement ) {
58
+ public object JsonDataSerializer : KSerializer<Parameters> {
59
+ override val descriptor: SerialDescriptor = JsonElement .serializer().descriptor
60
+
61
+ override fun deserialize (decoder : Decoder ): Parameters {
62
+ require(decoder is JsonDecoder ) { " This decoder is not a JsonDecoder. Cannot deserialize `FunctionParameters`." }
63
+ return Parameters (decoder.decodeJsonElement())
64
+ }
65
+
66
+ override fun serialize (encoder : Encoder , value : Parameters ) {
67
+ require(encoder is JsonEncoder ) { " This encoder is not a JsonEncoder. Cannot serialize `FunctionParameters`." }
68
+ encoder.encodeJsonElement(value.schema)
69
+ }
70
+ }
71
+
72
+ public companion object {
73
+ public fun fromJsonString (json : String ): Parameters = Parameters (Json .parseToJsonElement(json))
74
+
75
+ public fun buildJsonObject (block : JsonObjectBuilder .() -> Unit ): Parameters {
76
+ val json = kotlinx.serialization.json.buildJsonObject(block)
77
+ return Parameters (json)
78
+ }
79
+
80
+ public val Empty : Parameters = buildJsonObject {
81
+ put(" type" , " object" )
82
+ putJsonObject(" properties" ) {}
83
+ }
84
+ }
85
+ }
0 commit comments