diff --git a/firebase-ai-ksp-processor/README.md b/firebase-ai-ksp-processor/README.md new file mode 100644 index 00000000000..6460d9ff410 --- /dev/null +++ b/firebase-ai-ksp-processor/README.md @@ -0,0 +1,13 @@ +To build run `./gradlew :publishToMavenLocal` + +To integrate: add the following to your app's gradle file: + +```kotlin +plugins { + id("com.google.devtools.ksp") +} +dependencies { + implementation("com.google.firebase:firebase-ai:") + ksp("com.google.firebase:firebase-ai-processor:1.0.0") +} +``` diff --git a/firebase-ai-ksp-processor/build.gradle.kts b/firebase-ai-ksp-processor/build.gradle.kts new file mode 100644 index 00000000000..16df3455759 --- /dev/null +++ b/firebase-ai-ksp-processor/build.gradle.kts @@ -0,0 +1,46 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +plugins { + kotlin("jvm") + id("java-library") + id("maven-publish") +} + +dependencies { + testImplementation(kotlin("test")) + implementation(libs.symbol.processing.api) + implementation(libs.kotlinpoet.ksp) +} + +tasks.test { useJUnitPlatform() } + +kotlin { jvmToolchain(21) } + +publishing { + publications { + create("mavenKotlin") { + from(components["kotlin"]) + groupId = "com.google.firebase" + artifactId = "firebase-ai-processor" + version = "1.0.0" + } + } + repositories { + maven { url = uri("m2/") } + mavenLocal() + } +} diff --git a/firebase-ai-ksp-processor/gradle.properties b/firebase-ai-ksp-processor/gradle.properties new file mode 100644 index 00000000000..7fc6f1ff272 --- /dev/null +++ b/firebase-ai-ksp-processor/gradle.properties @@ -0,0 +1 @@ +kotlin.code.style=official diff --git a/firebase-ai-ksp-processor/src/main/kotlin/com/google/firebase/ai/ksp/SchemaSymbolProcessor.kt b/firebase-ai-ksp-processor/src/main/kotlin/com/google/firebase/ai/ksp/SchemaSymbolProcessor.kt new file mode 100644 index 00000000000..99d9c6083dd --- /dev/null +++ b/firebase-ai-ksp-processor/src/main/kotlin/com/google/firebase/ai/ksp/SchemaSymbolProcessor.kt @@ -0,0 +1,337 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.firebase.ai.ksp + +import com.google.devtools.ksp.KspExperimental +import com.google.devtools.ksp.processing.CodeGenerator +import com.google.devtools.ksp.processing.Dependencies +import com.google.devtools.ksp.processing.KSPLogger +import com.google.devtools.ksp.processing.Resolver +import com.google.devtools.ksp.processing.SymbolProcessor +import com.google.devtools.ksp.symbol.ClassKind +import com.google.devtools.ksp.symbol.KSAnnotated +import com.google.devtools.ksp.symbol.KSAnnotation +import com.google.devtools.ksp.symbol.KSClassDeclaration +import com.google.devtools.ksp.symbol.KSType +import com.google.devtools.ksp.symbol.KSVisitorVoid +import com.google.devtools.ksp.symbol.Modifier +import com.squareup.kotlinpoet.ClassName +import com.squareup.kotlinpoet.CodeBlock +import com.squareup.kotlinpoet.FileSpec +import com.squareup.kotlinpoet.KModifier +import com.squareup.kotlinpoet.ParameterizedTypeName +import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy +import com.squareup.kotlinpoet.PropertySpec +import com.squareup.kotlinpoet.TypeSpec +import com.squareup.kotlinpoet.ksp.toClassName +import com.squareup.kotlinpoet.ksp.toTypeName +import com.squareup.kotlinpoet.ksp.writeTo +import javax.annotation.processing.Generated + +public class SchemaSymbolProcessor( + private val codeGenerator: CodeGenerator, + private val logger: KSPLogger, +) : SymbolProcessor { + override fun process(resolver: Resolver): List { + resolver + .getSymbolsWithAnnotation("com.google.firebase.ai.annotations.Generable") + .filterIsInstance() + .map { it to SchemaSymbolProcessorVisitor(it, resolver) } + .forEach { it.second.visitClassDeclaration(it.first, Unit) } + + return emptyList() + } + + private inner class SchemaSymbolProcessorVisitor( + private val klass: KSClassDeclaration, + private val resolver: Resolver, + ) : KSVisitorVoid() { + private val numberTypes = setOf("kotlin.Int", "kotlin.Long", "kotlin.Double", "kotlin.Float") + private val baseKdocRegex = Regex("^\\s*(.*?)((@\\w* .*)|\\z)", RegexOption.DOT_MATCHES_ALL) + private val propertyKdocRegex = + Regex("\\s*@property (\\w*) (.*?)(?=@\\w*|\\z)", RegexOption.DOT_MATCHES_ALL) + + override fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: Unit) { + val isDataClass = classDeclaration.modifiers.contains(Modifier.DATA) + if (!isDataClass) { + logger.error("${classDeclaration.qualifiedName} is not a data class") + } + val generatedSchemaFile = generateFileSpec(classDeclaration) + generatedSchemaFile.writeTo( + codeGenerator, + Dependencies(true, classDeclaration.containingFile!!), + ) + } + + fun generateFileSpec(classDeclaration: KSClassDeclaration): FileSpec { + return FileSpec.builder( + classDeclaration.packageName.asString(), + "${classDeclaration.simpleName.asString()}GeneratedSchema", + ) + .addImport("com.google.firebase.ai.type", "JsonSchema") + .addType( + TypeSpec.classBuilder("${classDeclaration.simpleName.asString()}GeneratedSchema") + .addAnnotation(Generated::class) + .addType( + TypeSpec.companionObjectBuilder() + .addProperty( + PropertySpec.builder( + "SCHEMA", + ClassName("com.google.firebase.ai.type", "JsonSchema") + .parameterizedBy( + ClassName( + classDeclaration.packageName.asString(), + classDeclaration.simpleName.asString() + ) + ), + KModifier.PUBLIC, + ) + .mutable(false) + .initializer( + CodeBlock.builder() + .add( + generateCodeBlockForSchema(type = classDeclaration.asType(emptyList())) + ) + .build() + ) + .build() + ) + .build() + ) + .build() + ) + .build() + } + + @OptIn(KspExperimental::class) + fun generateCodeBlockForSchema( + name: String? = null, + description: String? = null, + type: KSType, + parentType: KSType? = null, + guideAnnotation: KSAnnotation? = null, + ): CodeBlock { + val parameterizedName = type.toTypeName() as? ParameterizedTypeName + val className = parameterizedName?.rawType ?: type.toClassName() + val kdocString = type.declaration.docString ?: "" + val baseKdoc = extractBaseKdoc(kdocString) + val propertyDocs = extractPropertyKdocs(kdocString) + val guideClassAnnotation = + type.annotations.firstOrNull() { it.shortName.getShortName() == "Guide" } + val description = + getDescriptionFromAnnotations(guideAnnotation, guideClassAnnotation, description, baseKdoc) + val minimum = getDoubleFromAnnotation(guideAnnotation, "minimum") + val maximum = getDoubleFromAnnotation(guideAnnotation, "maximum") + val minItems = getIntFromAnnotation(guideAnnotation, "minItems") + val maxItems = getIntFromAnnotation(guideAnnotation, "maxItems") + val format = getStringFromAnnotation(guideAnnotation, "format") + val pattern = getStringFromAnnotation(guideAnnotation, "pattern") + val builder = CodeBlock.builder() + when (className.canonicalName) { + "kotlin.Int" -> { + builder.addStatement("JsonSchema.integer(").indent() + } + "kotlin.Long" -> { + builder.addStatement("JsonSchema.long(").indent() + } + "kotlin.Boolean" -> { + builder.addStatement("JsonSchema.boolean(").indent() + } + "kotlin.Float" -> { + builder.addStatement("JsonSchema.float(").indent() + } + "kotlin.Double" -> { + builder.addStatement("JsonSchema.double(").indent() + } + "kotlin.String" -> { + builder.addStatement("JsonSchema.string(").indent() + } + "kotlin.collections.List" -> { + val listTypeParam = type.arguments.first().type!!.resolve() + val listParamCodeBlock = + generateCodeBlockForSchema(type = listTypeParam, parentType = type) + builder + .addStatement("JsonSchema.array(") + .indent() + .addStatement("items = ") + .add(listParamCodeBlock) + .addStatement(",") + } + else -> { + if ((type.declaration as? KSClassDeclaration)?.classKind == ClassKind.ENUM_CLASS) { + val enumValues = + (type.declaration as KSClassDeclaration) + .declarations + .filterIsInstance(KSClassDeclaration::class.java) + .map { it.simpleName.asString() } + .toList() + builder + .addStatement("JsonSchema.enumeration(") + .indent() + .addStatement("clazz = ${type.declaration.qualifiedName!!.asString()}::class.java,") + .addStatement("values = listOf(") + .indent() + .addStatement(enumValues.joinToString { "\"$it\"" }) + .unindent() + .addStatement("),") + } else { + builder + .addStatement("JsonSchema.obj(") + .indent() + .addStatement("clazz = ${type.declaration.qualifiedName!!.asString()}::class.java,") + .addStatement("properties = mapOf(") + .indent() + val properties = + (type.declaration as KSClassDeclaration).getAllProperties().associate { property -> + val propertyName = property.simpleName.asString() + propertyName to + generateCodeBlockForSchema( + type = property.type.resolve(), + parentType = type, + description = propertyDocs[propertyName], + name = propertyName, + guideAnnotation = + property.annotations.firstOrNull() { it.shortName.getShortName() == "Guide" }, + ) + } + properties.entries.forEach { + builder + .addStatement("%S to ", it.key) + .indent() + .add(it.value) + .unindent() + .addStatement(", ") + } + builder.unindent().addStatement("),") + } + } + } + if (name != null) { + builder.addStatement("title = %S,", name) + } + if (description != null) { + builder.addStatement("description = %S,", description) + } + if ((minimum != null || maximum != null) && !numberTypes.contains(className.canonicalName)) { + logger.warn( + "${parentType?.toClassName()?.simpleName?.let { "$it." }}$name is not a number type, minimum and maximum are not valid parameters to specify in @Guide" + ) + } + if ( + (minItems != null || maxItems != null) && + className.canonicalName != "kotlin.collections.List" + ) { + logger.warn( + "${parentType?.toClassName()?.simpleName?.let { "$it." }}$name is not a List type, minItems and maxItems are not valid parameters to specify in @Guide" + ) + } + if ((format != null || pattern != null) && className.canonicalName != "kotlin.String") { + logger.warn( + "${parentType?.toClassName()?.simpleName?.let { "$it." }}$name is not a String type, format and pattern are not a valid parameter to specify in @Guide" + ) + } + if (minimum != null) { + builder.addStatement("minimum = %L,", minimum) + } + if (maximum != null) { + builder.addStatement("maximum = %L,", maximum) + } + if (minItems != null) { + builder.addStatement("minItems = %L,", minItems) + } + if (maxItems != null) { + builder.addStatement("maxItems = %L,", maxItems) + } + if (format != null) { + builder.addStatement("format = %S,", format) + } + if (pattern != null) { + builder.addStatement("pattern = %S,", pattern) + } + builder.addStatement("nullable = %L)", className.isNullable).unindent() + return builder.build() + } + + private fun getDescriptionFromAnnotations( + guideAnnotation: KSAnnotation?, + guideClassAnnotation: KSAnnotation?, + description: String?, + baseKdoc: String?, + ): String? { + val guidePropertyDescription = getStringFromAnnotation(guideAnnotation, "description") + + val guideClassDescription = getStringFromAnnotation(guideClassAnnotation, "description") + + return guidePropertyDescription ?: guideClassDescription ?: description ?: baseKdoc + } + + private fun getDoubleFromAnnotation( + guideAnnotation: KSAnnotation?, + doubleName: String, + ): Double? { + val guidePropertyDoubleValue = + guideAnnotation + ?.arguments + ?.firstOrNull { it.name?.getShortName()?.equals(doubleName) == true } + ?.value as? Double + if (guidePropertyDoubleValue == null || guidePropertyDoubleValue == -1.0) { + return null + } + return guidePropertyDoubleValue + } + + private fun getIntFromAnnotation(guideAnnotation: KSAnnotation?, intName: String): Int? { + val guidePropertyIntValue = + guideAnnotation + ?.arguments + ?.firstOrNull { it.name?.getShortName()?.equals(intName) == true } + ?.value as? Int + if (guidePropertyIntValue == null || guidePropertyIntValue == -1) { + return null + } + return guidePropertyIntValue + } + + private fun getStringFromAnnotation( + guideAnnotation: KSAnnotation?, + stringName: String, + ): String? { + val guidePropertyStringValue = + guideAnnotation + ?.arguments + ?.firstOrNull { it.name?.getShortName()?.equals(stringName) == true } + ?.value as? String + if (guidePropertyStringValue.isNullOrEmpty()) { + return null + } + return guidePropertyStringValue + } + + private fun extractBaseKdoc(kdoc: String): String? { + return baseKdocRegex.matchEntire(kdoc)?.groups?.get(1)?.value?.trim().let { + if (it.isNullOrEmpty()) null else it + } + } + + private fun extractPropertyKdocs(kdoc: String): Map { + return propertyKdocRegex + .findAll(kdoc) + .map { it.groups[1]!!.value to it.groups[2]!!.value.replace("\n", "").trim() } + .toMap() + } + } +} diff --git a/firebase-ai-ksp-processor/src/main/kotlin/com/google/firebase/ai/ksp/SchemaSymbolProcessorProvider.kt b/firebase-ai-ksp-processor/src/main/kotlin/com/google/firebase/ai/ksp/SchemaSymbolProcessorProvider.kt new file mode 100644 index 00000000000..2c8015bc8a9 --- /dev/null +++ b/firebase-ai-ksp-processor/src/main/kotlin/com/google/firebase/ai/ksp/SchemaSymbolProcessorProvider.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.firebase.ai.ksp + +import com.google.devtools.ksp.processing.SymbolProcessor +import com.google.devtools.ksp.processing.SymbolProcessorEnvironment +import com.google.devtools.ksp.processing.SymbolProcessorProvider + +public class SchemaSymbolProcessorProvider : SymbolProcessorProvider { + override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor { + return SchemaSymbolProcessor(environment.codeGenerator, environment.logger) + } +} diff --git a/firebase-ai-ksp-processor/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider b/firebase-ai-ksp-processor/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider new file mode 100644 index 00000000000..83d92f28c7e --- /dev/null +++ b/firebase-ai-ksp-processor/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider @@ -0,0 +1 @@ +com.google.firebase.ai.ksp.SchemaSymbolProcessorProvider \ No newline at end of file diff --git a/firebase-ai/CHANGELOG.md b/firebase-ai/CHANGELOG.md index 21c55237ecf..fa57a850988 100644 --- a/firebase-ai/CHANGELOG.md +++ b/firebase-ai/CHANGELOG.md @@ -1,4 +1,5 @@ # Unreleased + - [feature] Added support for server templates via `TemplateGenerativeModel` and `TemplateImagenModel`. (#7503) diff --git a/firebase-ai/api.txt b/firebase-ai/api.txt index b5932bf9b0e..2b184a11b79 100644 --- a/firebase-ai/api.txt +++ b/firebase-ai/api.txt @@ -98,6 +98,28 @@ package com.google.firebase.ai { } +package com.google.firebase.ai.annotations { + + @kotlin.annotation.Retention(kotlin.annotation.AnnotationRetention.SOURCE) @kotlin.annotation.Target(allowedTargets=kotlin.annotation.AnnotationTarget.CLASS) public @interface Generable { + } + + @kotlin.annotation.Retention(kotlin.annotation.AnnotationRetention.SOURCE) @kotlin.annotation.Target(allowedTargets={kotlin.annotation.AnnotationTarget.CLASS, kotlin.annotation.AnnotationTarget.PROPERTY}) public @interface Guide { + method public abstract String description() default ""; + method public abstract String format() default ""; + method public abstract int maxItems() default -1; + method public abstract double maximum() default -1.0; + method public abstract int minItems() default -1; + method public abstract double minimum() default -1.0; + property public abstract String description; + property public abstract String format; + property public abstract int maxItems; + property public abstract double maximum; + property public abstract int minItems; + property public abstract double minimum; + } + +} + package com.google.firebase.ai.java { public abstract class ChatFutures { diff --git a/firebase-ai/src/main/kotlin/com/google/firebase/ai/annotations/Generable.kt b/firebase-ai/src/main/kotlin/com/google/firebase/ai/annotations/Generable.kt new file mode 100644 index 00000000000..b4a5e652ae5 --- /dev/null +++ b/firebase-ai/src/main/kotlin/com/google/firebase/ai/annotations/Generable.kt @@ -0,0 +1,21 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.firebase.ai.annotations + +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.SOURCE) +public annotation class Generable diff --git a/firebase-ai/src/main/kotlin/com/google/firebase/ai/annotations/Guide.kt b/firebase-ai/src/main/kotlin/com/google/firebase/ai/annotations/Guide.kt new file mode 100644 index 00000000000..c86237ecec9 --- /dev/null +++ b/firebase-ai/src/main/kotlin/com/google/firebase/ai/annotations/Guide.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.firebase.ai.annotations + +@Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY) +@Retention(AnnotationRetention.SOURCE) +public annotation class Guide( + public val description: String = "", + public val minimum: Double = -1.0, + public val maximum: Double = -1.0, + public val minItems: Int = -1, + public val maxItems: Int = -1, + public val format: String = "", + public val pattern: String = "", +) diff --git a/firebase-ai/src/main/kotlin/com/google/firebase/ai/type/JsonSchema.kt b/firebase-ai/src/main/kotlin/com/google/firebase/ai/type/JsonSchema.kt new file mode 100644 index 00000000000..b4dce4074d6 --- /dev/null +++ b/firebase-ai/src/main/kotlin/com/google/firebase/ai/type/JsonSchema.kt @@ -0,0 +1,493 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.firebase.ai.type + +import kotlinx.serialization.json.JsonObject + +/** + * Definition of a data type. + * + * These types can be objects, but also primitives and arrays. Represents a select subset of an + * [JsonSchema object](https://json-schema.org/specification). + * + * **Note:** While optional, including a `description` field in your `JsonSchema` is strongly + * encouraged. The more information the model has about what it's expected to generate, the better + * the results. + */ +public class JsonSchema +internal constructor( + public val type: String, + public val clazz: Class, + public val description: String? = null, + public val format: String? = null, + public val pattern: String? = null, + public val nullable: Boolean? = null, + public val enum: List? = null, + public val properties: Map>? = null, + public val required: List? = null, + public val items: JsonSchema<*>? = null, + public val title: String? = null, + public val minItems: Int? = null, + public val maxItems: Int? = null, + public val minimum: Double? = null, + public val maximum: Double? = null, + public val anyOf: List>? = null, +) { + + public companion object { + /** + * Returns a [JsonSchema] representing a boolean value. + * + * @param description An optional description of what the boolean should contain or represent. + * @param nullable Indicates whether the value can be `null`. Defaults to `false`. + */ + @JvmStatic + @JvmOverloads + public fun boolean( + description: String? = null, + nullable: Boolean = false, + title: String? = null, + ): JsonSchema = + JsonSchema( + description = description, + nullable = nullable, + type = "BOOLEAN", + title = title, + clazz = Boolean::class.java + ) + + /** + * Returns a [JsonSchema] for a 32-bit signed integer number. + * + * **Important:** This [JsonSchema] provides a hint to the model that it should generate a + * 32-bit integer, but only guarantees that the value will be an integer. Therefore it's + * *possible* that decoding it as an `Int` variable (or `int` in Java) could overflow. + * + * @param description An optional description of what the integer should contain or represent. + * @param nullable Indicates whether the value can be `null`. Defaults to `false`. + */ + @JvmStatic + @JvmName("numInt") + @JvmOverloads + public fun integer( + description: String? = null, + nullable: Boolean = false, + title: String? = null, + minimum: Double? = null, + maximum: Double? = null, + ): JsonSchema = + JsonSchema( + description = description, + format = "int32", + nullable = nullable, + type = "INTEGER", + title = title, + minimum = minimum, + maximum = maximum, + clazz = Integer::class.java + ) + + /** + * Returns a [JsonSchema] for a 64-bit signed integer number. + * + * @param description An optional description of what the number should contain or represent. + * @param nullable Indicates whether the value can be `null`. Defaults to `false`. + */ + @JvmStatic + @JvmName("numLong") + @JvmOverloads + public fun long( + description: String? = null, + nullable: Boolean = false, + title: String? = null, + minimum: Double? = null, + maximum: Double? = null, + ): JsonSchema = + JsonSchema( + description = description, + nullable = nullable, + type = "INTEGER", + title = title, + minimum = minimum, + maximum = maximum, + clazz = Long::class.java + ) + + /** + * Returns a [JsonSchema] for a double-precision floating-point number. + * + * @param description An optional description of what the number should contain or represent. + * @param nullable Indicates whether the value can be `null`. Defaults to `false`. + */ + @JvmStatic + @JvmName("numDouble") + @JvmOverloads + public fun double( + description: String? = null, + nullable: Boolean = false, + title: String? = null, + minimum: Double? = null, + maximum: Double? = null, + ): JsonSchema = + JsonSchema( + description = description, + nullable = nullable, + type = "NUMBER", + title = title, + minimum = minimum, + maximum = maximum, + clazz = Double::class.java + ) + + /** + * Returns a [JsonSchema] for a single-precision floating-point number. + * + * **Important:** This [JsonSchema] provides a hint to the model that it should generate a + * single-precision floating-point number, but only guarantees that the value will be a number. + * Therefore it's *possible* that decoding it as a `Float` variable (or `float` in Java) could + * overflow. + * + * @param description An optional description of what the number should contain or represent. + * @param nullable Indicates whether the value can be `null`. Defaults to `false`. + */ + @JvmStatic + @JvmName("numFloat") + @JvmOverloads + public fun float( + description: String? = null, + nullable: Boolean = false, + title: String? = null, + minimum: Double? = null, + maximum: Double? = null, + ): JsonSchema = + JsonSchema( + description = description, + nullable = nullable, + type = "NUMBER", + format = "float", + title = title, + minimum = minimum, + maximum = maximum, + clazz = Float::class.java + ) + + /** + * Returns a [JsonSchema] for a string. + * + * @param description An optional description of what the string should contain or represent. + * @param nullable Indicates whether the value can be `null`. Defaults to `false`. + * @param format An optional pattern that values need to adhere to. + */ + @JvmStatic + @JvmName("str") + @JvmOverloads + public fun string( + description: String? = null, + nullable: Boolean = false, + format: StringFormat? = null, + pattern: String? = null, + title: String? = null, + ): JsonSchema = + JsonSchema( + description = description, + format = format?.value, + nullable = nullable, + type = "STRING", + title = title, + clazz = String::class.java, + pattern = pattern + ) + + /** + * Returns a [JsonSchema] for a complex data type. + * + * This schema instructs the model to produce data of type object, which has keys of type + * `String` and values of type [JsonSchema]. + * + * **Example:** A `city` could be represented with the following object `JsonSchema`. + * + * ``` + * JsonSchema.obj(mapOf( + * "name" to JsonSchema.string(), + * "population" to JsonSchema.integer() + * )) + * ``` + * + * @param properties The map of the object's property names to their [JsonSchema]s. + * @param optionalProperties The list of optional properties. They must correspond to the keys + * provided in the `properties` map. By default it's empty, signaling the model that all + * properties are to be included. + * @param description An optional description of what the object represents. + * @param nullable Indicates whether the value can be `null`. Defaults to `false`. + */ + @JvmStatic + @JvmOverloads + public fun obj( + properties: Map>, + optionalProperties: List = emptyList(), + description: String? = null, + nullable: Boolean = false, + title: String? = null, + ): JsonSchema { + if (!properties.keys.containsAll(optionalProperties)) { + throw IllegalArgumentException( + "All optional properties must be present in properties. Missing: ${optionalProperties.minus(properties.keys)}" + ) + } + return JsonSchema( + description = description, + nullable = nullable, + properties = properties, + required = properties.keys.minus(optionalProperties.toSet()).toList(), + type = "OBJECT", + title = title, + clazz = JsonObject::class.java + ) + } + + /** + * Returns a [JsonSchema] for a complex data type. + * + * This schema instructs the model to produce data of type object, which has keys of type + * `String` and values of type [JsonSchema]. + * + * **Example:** A `city` could be represented with the following object `JsonSchema`. + * + * ``` + * JsonSchema.obj(mapOf( + * "name" to JsonSchema.string(), + * "population" to JsonSchema.integer() + * ), + * City::class.java + * ) + * ``` + * + * @param clazz the real class that this schema represents + * @param properties The map of the object's property names to their [JsonSchema]s. + * @param optionalProperties The list of optional properties. They must correspond to the keys + * provided in the `properties` map. By default it's empty, signaling the model that all + * properties are to be included. + * @param description An optional description of what the object represents. + * @param nullable Indicates whether the value can be `null`. Defaults to `false`. + */ + @JvmStatic + @JvmOverloads + public fun obj( + clazz: Class, + properties: Map>, + optionalProperties: List = emptyList(), + description: String? = null, + nullable: Boolean = false, + title: String? = null, + ): JsonSchema { + if (!properties.keys.containsAll(optionalProperties)) { + throw IllegalArgumentException( + "All optional properties must be present in properties. Missing: ${optionalProperties.minus(properties.keys)}" + ) + } + return JsonSchema( + description = description, + nullable = nullable, + properties = properties, + required = properties.keys.minus(optionalProperties.toSet()).toList(), + type = "OBJECT", + title = title, + clazz = clazz + ) + } + + /** + * Returns a [JsonSchema] for an array. + * + * @param items The [JsonSchema] of the elements stored in the array. + * @param description An optional description of what the array represents. + * @param nullable Indicates whether the value can be `null`. Defaults to `false`. + */ + @JvmStatic + @JvmOverloads + public fun array( + items: JsonSchema<*>, + description: String? = null, + nullable: Boolean = false, + title: String? = null, + minItems: Int? = null, + maxItems: Int? = null, + ): JsonSchema> = + JsonSchema( + description = description, + nullable = nullable, + items = items, + type = "ARRAY", + title = title, + minItems = minItems, + maxItems = maxItems, + clazz = List::class.java + ) + + /** + * Returns a [JsonSchema] for an enumeration. + * + * For example, the cardinal directions can be represented as: + * ``` + * JsonSchema.enumeration(listOf("north", "east", "south", "west"), "Cardinal directions") + * ``` + * + * @param values The list of valid values for this enumeration + * @param description The description of what the parameter should contain or represent + * @param nullable Indicates whether the value can be `null`. Defaults to `false`. + */ + @JvmStatic + @JvmOverloads + public fun enumeration( + values: List, + description: String? = null, + nullable: Boolean = false, + title: String? = null, + ): JsonSchema = + JsonSchema( + description = description, + format = "enum", + nullable = nullable, + enum = values, + type = "STRING", + title = title, + clazz = String::class.java + ) + + /** + * Returns a [JsonSchema] for an enumeration. + * + * For example, the cardinal directions can be represented as: + * ``` + * JsonSchema.enumeration( + * listOf("north", "east", "south", "west"), + * Direction::class.java, + * "Cardinal directions" + * ) + * ``` + * + * @param clazz the real class that this schema represents + * @param values The list of valid values for this enumeration + * @param description The description of what the parameter should contain or represent + * @param nullable Indicates whether the value can be `null`. Defaults to `false`. + */ + @JvmStatic + @JvmOverloads + public fun enumeration( + clazz: Class, + values: List, + description: String? = null, + nullable: Boolean = false, + title: String? = null, + ): JsonSchema = + JsonSchema( + description = description, + format = "enum", + nullable = nullable, + enum = values, + type = "STRING", + title = title, + clazz = clazz + ) + + /** + * Returns a [JsonSchema] representing a value that must conform to *any* (one of) the provided + * sub-schema. + * + * Example: A field that can hold either a simple userID or a more detailed user object. + * + * ``` + * JsonSchema.anyOf( listOf( JsonSchema.integer(description = "User ID"), JsonSchema.obj( mapOf( + * "userID" to JsonSchema.integer(description = "User ID"), + * "username" to JsonSchema.string(description = "Username") + * ))) + * ``` + * + * @param schemas The list of valid schemas which could be here + */ + @JvmStatic + public fun anyOf(schemas: List>): JsonSchema = + JsonSchema(type = "ANYOF", anyOf = schemas, clazz = String::class.java) + } + + internal fun toInternalJson(): Schema.InternalJson { + val outType = + if (type == "ANYOF" || (type == "STRING" && format == "enum")) { + null + } else { + type.lowercase() + } + + val (outMinimum, outMaximum) = + if (outType == "integer" && format == "int32") { + (minimum ?: Integer.MIN_VALUE.toDouble()) to (maximum ?: Integer.MAX_VALUE.toDouble()) + } else { + minimum to maximum + } + + val outFormat = + if ( + (outType == "integer" && format == "int32") || + (outType == "number" && format == "float") || + format == "enum" + ) { + null + } else { + format + } + + if (nullable == true) { + return Schema.InternalJsonNullable( + outType?.let { listOf(it, "null") }, + description, + outFormat, + pattern, + enum?.let { + buildList { + addAll(it) + add("null") + } + }, + properties?.mapValues { it.value.toInternalJson() }, + required, + items?.toInternalJson(), + title, + minItems, + maxItems, + outMinimum, + outMaximum, + anyOf?.map { it.toInternalJson() }, + ) + } + return Schema.InternalJsonNonNull( + outType, + description, + outFormat, + pattern, + enum, + properties?.mapValues { it.value.toInternalJson() }, + required, + items?.toInternalJson(), + title, + minItems, + maxItems, + outMinimum, + outMaximum, + anyOf?.map { it.toInternalJson() }, + ) + } +} diff --git a/firebase-ai/src/main/kotlin/com/google/firebase/ai/type/Schema.kt b/firebase-ai/src/main/kotlin/com/google/firebase/ai/type/Schema.kt index 1dfa4ddecb0..9f728adbbd4 100644 --- a/firebase-ai/src/main/kotlin/com/google/firebase/ai/type/Schema.kt +++ b/firebase-ai/src/main/kotlin/com/google/firebase/ai/type/Schema.kt @@ -378,6 +378,7 @@ internal constructor( outType?.let { listOf(it, "null") }, description, outFormat, + null, enum?.let { buildList { addAll(it) @@ -399,6 +400,7 @@ internal constructor( outType, description, outFormat, + null, enum, properties?.mapValues { it.value.toInternalJson() }, required, @@ -437,6 +439,7 @@ internal constructor( val type: String? = null, val description: String? = null, val format: String? = null, + val pattern: String? = null, val enum: List? = null, val properties: Map? = null, val required: List? = null, @@ -454,6 +457,7 @@ internal constructor( val type: List? = null, val description: String? = null, val format: String? = null, + val pattern: String? = null, val enum: List? = null, val properties: Map? = null, val required: List? = null, diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index e8636054f4e..71e540e1676 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -45,6 +45,7 @@ jsonassert = "1.5.0" kotest = "5.9.0" # Do not use 5.9.1 because it reverts the fix for https://github.com/kotest/kotest/issues/3981 kotestAssertionsCore = "5.8.1" kotlin = "2.0.21" +kotlinpoetKsp = "2.2.0" ktorVersion = "3.0.3" legacySupportV4 = "1.0.0" lifecycleProcess = "2.3.1" @@ -69,6 +70,7 @@ rxjava = "2.2.21" serialization = "1.7.3" slf4jNop = "2.0.17" spotless = "7.0.4" +symbolProcessingApi = "2.2.10-2.0.2" testServices = "1.6.0" truth = "1.4.4" truthProtoExtension = "1.0" @@ -142,6 +144,7 @@ kotlin-bom = { module = "org.jetbrains.kotlin:kotlin-bom", version.ref = "kotlin kotlin-coroutines-tasks = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-play-services", version.ref = "coroutines" } kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" } kotlin-stdlib-jdk8 = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8", version.ref = "kotlin" } +kotlinpoet-ksp = { module = "com.squareup:kotlinpoet-ksp", version.ref = "kotlinpoetKsp" } kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" } kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" } kotlinx-coroutines-reactive = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-reactive", version.ref = "coroutines" } @@ -203,6 +206,7 @@ rxandroid = { module = "io.reactivex.rxjava2:rxandroid", version.ref = "rxandroi rxjava = { module = "io.reactivex.rxjava2:rxjava", version.ref = "rxjava" } slf4j-nop = { module = "org.slf4j:slf4j-nop", version.ref = "slf4jNop" } spotless-plugin-gradle = { module = "com.diffplug.spotless:spotless-plugin-gradle", version.ref = "spotless" } +symbol-processing-api = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "symbolProcessingApi" } truth = { module = "com.google.truth:truth", version.ref = "truth" } truth-liteproto-extension = { module = "com.google.truth.extensions:truth-liteproto-extension", version.ref = "truth" } truth-proto-extension = { module = "com.google.truth.extensions:truth-proto-extension", version.ref = "truthProtoExtension" } diff --git a/subprojects.cfg b/subprojects.cfg index f8505ecf8e7..a167e6eb58c 100644 --- a/subprojects.cfg +++ b/subprojects.cfg @@ -74,3 +74,5 @@ transport:transport-runtime-testing # sdk #firebase-storage:test-app #appcheck:firebase-appcheck:test-app #firebase-appdistribution:test-app + +firebase-ai-ksp-processor # buildtools