|
| 1 | +// File generated from our OpenAPI spec by Stainless. |
| 2 | + |
| 3 | +package com.openai.models |
| 4 | + |
| 5 | +import com.fasterxml.jackson.core.JsonGenerator |
| 6 | +import com.fasterxml.jackson.core.ObjectCodec |
| 7 | +import com.fasterxml.jackson.databind.JsonNode |
| 8 | +import com.fasterxml.jackson.databind.SerializerProvider |
| 9 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize |
| 10 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize |
| 11 | +import com.fasterxml.jackson.module.kotlin.jacksonTypeRef |
| 12 | +import com.openai.core.BaseDeserializer |
| 13 | +import com.openai.core.BaseSerializer |
| 14 | +import com.openai.core.JsonValue |
| 15 | +import com.openai.core.getOrThrow |
| 16 | +import com.openai.errors.OpenAIInvalidDataException |
| 17 | +import java.util.Objects |
| 18 | +import java.util.Optional |
| 19 | +import kotlin.jvm.optionals.getOrNull |
| 20 | + |
| 21 | +/** |
| 22 | + * A citation within the message that points to a specific quote from a specific File associated |
| 23 | + * with the assistant or the message. Generated when the assistant uses the "file_search" tool to |
| 24 | + * search files. |
| 25 | + */ |
| 26 | +@JsonDeserialize(using = Annotation.Deserializer::class) |
| 27 | +@JsonSerialize(using = Annotation.Serializer::class) |
| 28 | +class Annotation |
| 29 | +private constructor( |
| 30 | + private val fileCitationAnnotation: FileCitationAnnotation? = null, |
| 31 | + private val filePathAnnotation: FilePathAnnotation? = null, |
| 32 | + private val _json: JsonValue? = null, |
| 33 | +) { |
| 34 | + |
| 35 | + private var validated: Boolean = false |
| 36 | + |
| 37 | + /** |
| 38 | + * A citation within the message that points to a specific quote from a specific File associated |
| 39 | + * with the assistant or the message. Generated when the assistant uses the "file_search" tool |
| 40 | + * to search files. |
| 41 | + */ |
| 42 | + fun fileCitationAnnotation(): Optional<FileCitationAnnotation> = |
| 43 | + Optional.ofNullable(fileCitationAnnotation) |
| 44 | + /** |
| 45 | + * A URL for the file that's generated when the assistant used the `code_interpreter` tool to |
| 46 | + * generate a file. |
| 47 | + */ |
| 48 | + fun filePathAnnotation(): Optional<FilePathAnnotation> = Optional.ofNullable(filePathAnnotation) |
| 49 | + |
| 50 | + fun isFileCitationAnnotation(): Boolean = fileCitationAnnotation != null |
| 51 | + |
| 52 | + fun isFilePathAnnotation(): Boolean = filePathAnnotation != null |
| 53 | + |
| 54 | + /** |
| 55 | + * A citation within the message that points to a specific quote from a specific File associated |
| 56 | + * with the assistant or the message. Generated when the assistant uses the "file_search" tool |
| 57 | + * to search files. |
| 58 | + */ |
| 59 | + fun asFileCitationAnnotation(): FileCitationAnnotation = |
| 60 | + fileCitationAnnotation.getOrThrow("fileCitationAnnotation") |
| 61 | + /** |
| 62 | + * A URL for the file that's generated when the assistant used the `code_interpreter` tool to |
| 63 | + * generate a file. |
| 64 | + */ |
| 65 | + fun asFilePathAnnotation(): FilePathAnnotation = |
| 66 | + filePathAnnotation.getOrThrow("filePathAnnotation") |
| 67 | + |
| 68 | + fun _json(): Optional<JsonValue> = Optional.ofNullable(_json) |
| 69 | + |
| 70 | + fun <T> accept(visitor: Visitor<T>): T { |
| 71 | + return when { |
| 72 | + fileCitationAnnotation != null -> |
| 73 | + visitor.visitFileCitationAnnotation(fileCitationAnnotation) |
| 74 | + filePathAnnotation != null -> visitor.visitFilePathAnnotation(filePathAnnotation) |
| 75 | + else -> visitor.unknown(_json) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + fun validate(): Annotation = apply { |
| 80 | + if (!validated) { |
| 81 | + if (fileCitationAnnotation == null && filePathAnnotation == null) { |
| 82 | + throw OpenAIInvalidDataException("Unknown Annotation: $_json") |
| 83 | + } |
| 84 | + fileCitationAnnotation?.validate() |
| 85 | + filePathAnnotation?.validate() |
| 86 | + validated = true |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + override fun equals(other: Any?): Boolean { |
| 91 | + if (this === other) { |
| 92 | + return true |
| 93 | + } |
| 94 | + |
| 95 | + return /* spotless:off */ other is Annotation && fileCitationAnnotation == other.fileCitationAnnotation && filePathAnnotation == other.filePathAnnotation /* spotless:on */ |
| 96 | + } |
| 97 | + |
| 98 | + override fun hashCode(): Int = /* spotless:off */ Objects.hash(fileCitationAnnotation, filePathAnnotation) /* spotless:on */ |
| 99 | + |
| 100 | + override fun toString(): String = |
| 101 | + when { |
| 102 | + fileCitationAnnotation != null -> |
| 103 | + "Annotation{fileCitationAnnotation=$fileCitationAnnotation}" |
| 104 | + filePathAnnotation != null -> "Annotation{filePathAnnotation=$filePathAnnotation}" |
| 105 | + _json != null -> "Annotation{_unknown=$_json}" |
| 106 | + else -> throw IllegalStateException("Invalid Annotation") |
| 107 | + } |
| 108 | + |
| 109 | + companion object { |
| 110 | + |
| 111 | + /** |
| 112 | + * A citation within the message that points to a specific quote from a specific File |
| 113 | + * associated with the assistant or the message. Generated when the assistant uses the |
| 114 | + * "file_search" tool to search files. |
| 115 | + */ |
| 116 | + @JvmStatic |
| 117 | + fun ofFileCitationAnnotation(fileCitationAnnotation: FileCitationAnnotation) = |
| 118 | + Annotation(fileCitationAnnotation = fileCitationAnnotation) |
| 119 | + |
| 120 | + /** |
| 121 | + * A URL for the file that's generated when the assistant used the `code_interpreter` tool |
| 122 | + * to generate a file. |
| 123 | + */ |
| 124 | + @JvmStatic |
| 125 | + fun ofFilePathAnnotation(filePathAnnotation: FilePathAnnotation) = |
| 126 | + Annotation(filePathAnnotation = filePathAnnotation) |
| 127 | + } |
| 128 | + |
| 129 | + interface Visitor<out T> { |
| 130 | + |
| 131 | + fun visitFileCitationAnnotation(fileCitationAnnotation: FileCitationAnnotation): T |
| 132 | + |
| 133 | + fun visitFilePathAnnotation(filePathAnnotation: FilePathAnnotation): T |
| 134 | + |
| 135 | + fun unknown(json: JsonValue?): T { |
| 136 | + throw OpenAIInvalidDataException("Unknown Annotation: $json") |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + class Deserializer : BaseDeserializer<Annotation>(Annotation::class) { |
| 141 | + |
| 142 | + override fun ObjectCodec.deserialize(node: JsonNode): Annotation { |
| 143 | + val json = JsonValue.fromJsonNode(node) |
| 144 | + val type = json.asObject().getOrNull()?.get("type")?.asString()?.getOrNull() |
| 145 | + |
| 146 | + when (type) { |
| 147 | + "file_citation" -> { |
| 148 | + tryDeserialize(node, jacksonTypeRef<FileCitationAnnotation>()) { it.validate() } |
| 149 | + ?.let { |
| 150 | + return Annotation(fileCitationAnnotation = it, _json = json) |
| 151 | + } |
| 152 | + } |
| 153 | + "file_path" -> { |
| 154 | + tryDeserialize(node, jacksonTypeRef<FilePathAnnotation>()) { it.validate() } |
| 155 | + ?.let { |
| 156 | + return Annotation(filePathAnnotation = it, _json = json) |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + return Annotation(_json = json) |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + class Serializer : BaseSerializer<Annotation>(Annotation::class) { |
| 166 | + |
| 167 | + override fun serialize( |
| 168 | + value: Annotation, |
| 169 | + generator: JsonGenerator, |
| 170 | + provider: SerializerProvider |
| 171 | + ) { |
| 172 | + when { |
| 173 | + value.fileCitationAnnotation != null -> |
| 174 | + generator.writeObject(value.fileCitationAnnotation) |
| 175 | + value.filePathAnnotation != null -> generator.writeObject(value.filePathAnnotation) |
| 176 | + value._json != null -> generator.writeObject(value._json) |
| 177 | + else -> throw IllegalStateException("Invalid Annotation") |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments