Skip to content

Commit 88b6f8e

Browse files
author
David Motsonashvili
committed
adding support for extra schema properties and the anyOf schema
1 parent f03fe1c commit 88b6f8e

File tree

2 files changed

+115
-18
lines changed

2 files changed

+115
-18
lines changed

firebase-ai/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* [changed] Introduced the `Voice` class, which accepts a voice name, and deprecated the `Voices` class.
66
* [changed] **Breaking Change**: Updated `SpeechConfig` to take in `Voice` class instead of `Voices` class.
77
* **Action Required:** Update all references of `SpeechConfig` initialization to use `Voice` class.
8+
* [feature] Added support for extra schema properties like `title`, `minItems`, `maxItems`, `minimum`
9+
and `maximum`. As well as support for the `anyOf` schema.
810

911

1012
# 16.0.0

firebase-ai/src/main/kotlin/com/google/firebase/ai/type/Schema.kt

Lines changed: 113 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,20 @@ public abstract class StringFormat private constructor(internal val value: Strin
3434
*/
3535
public class Schema
3636
internal constructor(
37-
public val type: String,
37+
public val type: String? = null,
3838
public val description: String? = null,
3939
public val format: String? = null,
4040
public val nullable: Boolean? = null,
4141
public val enum: List<String>? = null,
4242
public val properties: Map<String, Schema>? = null,
4343
public val required: List<String>? = null,
4444
public val items: Schema? = null,
45+
public val title: String? = null,
46+
public val minItems: Int? = null,
47+
public val maxItems: Int? = null,
48+
public val minimum: Double? = null,
49+
public val maximum: Double? = null,
50+
public val anyOf: List<Schema>? = null
4551
) {
4652

4753
public companion object {
@@ -53,12 +59,12 @@ internal constructor(
5359
*/
5460
@JvmStatic
5561
@JvmOverloads
56-
public fun boolean(description: String? = null, nullable: Boolean = false): Schema =
57-
Schema(
58-
description = description,
59-
nullable = nullable,
60-
type = "BOOLEAN",
61-
)
62+
public fun boolean(
63+
description: String? = null,
64+
nullable: Boolean = false,
65+
title: String? = null
66+
): Schema =
67+
Schema(description = description, nullable = nullable, type = "BOOLEAN", title = title)
6268

6369
/**
6470
* Returns a [Schema] for a 32-bit signed integer number.
@@ -73,12 +79,21 @@ internal constructor(
7379
@JvmStatic
7480
@JvmName("numInt")
7581
@JvmOverloads
76-
public fun integer(description: String? = null, nullable: Boolean = false): Schema =
82+
public fun integer(
83+
description: String? = null,
84+
nullable: Boolean = false,
85+
title: String? = null,
86+
minimum: Double? = null,
87+
maximum: Double? = null
88+
): Schema =
7789
Schema(
7890
description = description,
7991
format = "int32",
8092
nullable = nullable,
8193
type = "INTEGER",
94+
title = title,
95+
minimum = minimum,
96+
maximum = maximum,
8297
)
8398

8499
/**
@@ -90,11 +105,20 @@ internal constructor(
90105
@JvmStatic
91106
@JvmName("numLong")
92107
@JvmOverloads
93-
public fun long(description: String? = null, nullable: Boolean = false): Schema =
108+
public fun long(
109+
description: String? = null,
110+
nullable: Boolean = false,
111+
title: String? = null,
112+
minimum: Double? = null,
113+
maximum: Double? = null
114+
): Schema =
94115
Schema(
95116
description = description,
96117
nullable = nullable,
97118
type = "INTEGER",
119+
title = title,
120+
minimum = minimum,
121+
maximum = maximum,
98122
)
99123

100124
/**
@@ -106,8 +130,21 @@ internal constructor(
106130
@JvmStatic
107131
@JvmName("numDouble")
108132
@JvmOverloads
109-
public fun double(description: String? = null, nullable: Boolean = false): Schema =
110-
Schema(description = description, nullable = nullable, type = "NUMBER")
133+
public fun double(
134+
description: String? = null,
135+
nullable: Boolean = false,
136+
title: String? = null,
137+
minimum: Double? = null,
138+
maximum: Double? = null
139+
): Schema =
140+
Schema(
141+
description = description,
142+
nullable = nullable,
143+
type = "NUMBER",
144+
title = title,
145+
minimum = minimum,
146+
maximum = maximum,
147+
)
111148

112149
/**
113150
* Returns a [Schema] for a single-precision floating-point number.
@@ -123,8 +160,22 @@ internal constructor(
123160
@JvmStatic
124161
@JvmName("numFloat")
125162
@JvmOverloads
126-
public fun float(description: String? = null, nullable: Boolean = false): Schema =
127-
Schema(description = description, nullable = nullable, type = "NUMBER", format = "float")
163+
public fun float(
164+
description: String? = null,
165+
nullable: Boolean = false,
166+
title: String? = null,
167+
minimum: Double? = null,
168+
maximum: Double? = null
169+
): Schema =
170+
Schema(
171+
description = description,
172+
nullable = nullable,
173+
type = "NUMBER",
174+
format = "float",
175+
title = title,
176+
minimum = minimum,
177+
maximum = maximum,
178+
)
128179

129180
/**
130181
* Returns a [Schema] for a string.
@@ -139,13 +190,15 @@ internal constructor(
139190
public fun string(
140191
description: String? = null,
141192
nullable: Boolean = false,
142-
format: StringFormat? = null
193+
format: StringFormat? = null,
194+
title: String? = null,
143195
): Schema =
144196
Schema(
145197
description = description,
146198
format = format?.value,
147199
nullable = nullable,
148-
type = "STRING"
200+
type = "STRING",
201+
title = title
149202
)
150203

151204
/**
@@ -176,6 +229,7 @@ internal constructor(
176229
optionalProperties: List<String> = emptyList(),
177230
description: String? = null,
178231
nullable: Boolean = false,
232+
title: String? = null
179233
): Schema {
180234
if (!properties.keys.containsAll(optionalProperties)) {
181235
throw IllegalArgumentException(
@@ -188,6 +242,7 @@ internal constructor(
188242
properties = properties,
189243
required = properties.keys.minus(optionalProperties.toSet()).toList(),
190244
type = "OBJECT",
245+
title = title
191246
)
192247
}
193248

@@ -203,13 +258,19 @@ internal constructor(
203258
public fun array(
204259
items: Schema,
205260
description: String? = null,
206-
nullable: Boolean = false
261+
nullable: Boolean = false,
262+
title: String? = null,
263+
minItems: Int? = null,
264+
maxItems: Int? = null
207265
): Schema =
208266
Schema(
209267
description = description,
210268
nullable = nullable,
211269
items = items,
212270
type = "ARRAY",
271+
title = title,
272+
minItems = minItems,
273+
maxItems = maxItems
213274
)
214275

215276
/**
@@ -230,15 +291,37 @@ internal constructor(
230291
public fun enumeration(
231292
values: List<String>,
232293
description: String? = null,
233-
nullable: Boolean = false
294+
nullable: Boolean = false,
295+
title: String? = null,
234296
): Schema =
235297
Schema(
236298
description = description,
237299
format = "enum",
238300
nullable = nullable,
239301
enum = values,
240302
type = "STRING",
303+
title = title
241304
)
305+
306+
/**
307+
* Returns a [Schema] representing a value that must conform to *any* (one of) the provided
308+
* sub-schema.
309+
*
310+
* Example: A field that can hold either a simple userID or a more detailed user object.
311+
*
312+
* Schema.anyOf( listOf( Schema.integer(description = "User ID"), Schema.obj(mapOf(
313+
* ```
314+
* "userID" to Schema.integer(description = "User ID"),
315+
* "username" to Schema.string(description = "Username")
316+
* ```
317+
* )) )
318+
*
319+
* @param schemas The list of valid schemas which could be here
320+
*/
321+
@JvmStatic
322+
public fun anyOf(
323+
schemas: List<Schema>,
324+
): Schema = Schema(anyOf = schemas)
242325
}
243326

244327
internal fun toInternal(): Internal =
@@ -251,16 +334,28 @@ internal constructor(
251334
properties?.mapValues { it.value.toInternal() },
252335
required,
253336
items?.toInternal(),
337+
title,
338+
minItems,
339+
maxItems,
340+
minimum,
341+
maximum,
342+
anyOf?.map { it.toInternal() },
254343
)
255344
@Serializable
256345
internal data class Internal(
257-
val type: String,
346+
val type: String? = null,
258347
val description: String? = null,
259348
val format: String? = null,
260349
val nullable: Boolean? = false,
261350
val enum: List<String>? = null,
262351
val properties: Map<String, Internal>? = null,
263352
val required: List<String>? = null,
264353
val items: Internal? = null,
354+
val title: String? = null,
355+
val minItems: Int? = null,
356+
val maxItems: Int? = null,
357+
val minimum: Double? = null,
358+
val maximum: Double? = null,
359+
val anyOf: List<Internal>? = null
265360
)
266361
}

0 commit comments

Comments
 (0)