From 80cc983e4f04e88f0c7060cd0196df5fd0ceb738 Mon Sep 17 00:00:00 2001 From: stlc-bot Date: Fri, 9 Oct 2026 22:02:11 +0000 Subject: [PATCH 01/11] fix(sdk): one FormText model for the forms text node (PROD-10339) Stainless-Generated-From: dd366eec324de39ef90093c3e47929a7c38a0343 --- .../llamacloud/models/parsing/Form.kt | 2991 +--------------- .../llamacloud/models/parsing/FormField.kt | 2995 +--------------- .../llamacloud/models/parsing/FormSection.kt | 2990 +--------------- .../models/parsing/FormTableCellItems.kt | 2996 +---------------- .../llamacloud/models/parsing/FormText.kt | 2855 ++++++++++++++++ .../llamacloud/models/parsing/FormTextTest.kt | 474 +++ 6 files changed, 3377 insertions(+), 11924 deletions(-) create mode 100644 llama-cloud-core/src/main/kotlin/ai/llamaindex/llamacloud/models/parsing/FormText.kt create mode 100644 llama-cloud-core/src/test/kotlin/ai/llamaindex/llamacloud/models/parsing/FormTextTest.kt diff --git a/llama-cloud-core/src/main/kotlin/ai/llamaindex/llamacloud/models/parsing/Form.kt b/llama-cloud-core/src/main/kotlin/ai/llamaindex/llamacloud/models/parsing/Form.kt index 829db40..28f9d8c 100644 --- a/llama-cloud-core/src/main/kotlin/ai/llamaindex/llamacloud/models/parsing/Form.kt +++ b/llama-cloud-core/src/main/kotlin/ai/llamaindex/llamacloud/models/parsing/Form.kt @@ -4,7 +4,6 @@ package ai.llamaindex.llamacloud.models.parsing import ai.llamaindex.llamacloud.core.BaseDeserializer import ai.llamaindex.llamacloud.core.BaseSerializer -import ai.llamaindex.llamacloud.core.Enum import ai.llamaindex.llamacloud.core.ExcludeMissing import ai.llamaindex.llamacloud.core.JsonField import ai.llamaindex.llamacloud.core.JsonMissing @@ -186,19 +185,19 @@ private constructor( addJson(FormTable.builder().type(FormTable.Type.TABLE).rows(rows).build()) /** Alias for calling [addJson] with `Json.ofText(text)`. */ - fun addJson(text: Json.Text) = addJson(Json.ofText(text)) + fun addJson(text: FormText) = addJson(Json.ofText(text)) /** * Alias for calling [addJson] with the following: * ```java - * Json.Text.builder() - * .type(Form.Json.Text.Type.TEXT) + * FormText.builder() + * .type(FormText.Type.TEXT) * .value(value) * .build() * ``` */ fun addTextJson(value: String) = - addJson(Json.Text.builder().type(Form.Json.Text.Type.TEXT).value(value).build()) + addJson(FormText.builder().type(FormText.Type.TEXT).value(value).build()) /** Flattened list representation of the same content */ fun list(list: FormListItem) = list(JsonField.of(list)) @@ -298,7 +297,7 @@ private constructor( private val field: FormField? = null, private val section: FormSection? = null, private val table: FormTable? = null, - private val text: Text? = null, + private val text: FormText? = null, private val _json: JsonValue? = null, ) { @@ -315,7 +314,7 @@ private constructor( * Printed text that is not part of a field, section heading or table: a title, an * instruction, a note. With it the form JSON holds every printed word of its region. */ - fun text(): Optional = Optional.ofNullable(text) + fun text(): Optional = Optional.ofNullable(text) fun isField(): Boolean = field != null @@ -338,7 +337,7 @@ private constructor( * Printed text that is not part of a field, section heading or table: a title, an * instruction, a note. With it the form JSON holds every printed word of its region. */ - fun asText(): Text = text.getOrThrow("text") + fun asText(): FormText = text.getOrThrow("text") fun _json(): Optional = Optional.ofNullable(_json) @@ -410,7 +409,7 @@ private constructor( table.validate() } - override fun visitText(text: Text) { + override fun visitText(text: FormText) { text.validate() } } @@ -442,7 +441,7 @@ private constructor( override fun visitTable(table: FormTable) = table.validity() - override fun visitText(text: Text) = text.validity() + override fun visitText(text: FormText) = text.validity() override fun unknown(json: JsonValue?) = 0 } @@ -487,7 +486,7 @@ private constructor( * Printed text that is not part of a field, section heading or table: a title, an * instruction, a note. With it the form JSON holds every printed word of its region. */ - @JvmStatic fun ofText(text: Text) = Json(text = text) + @JvmStatic fun ofText(text: FormText) = Json(text = text) } /** An interface that defines how to map each variant of [Json] to a value of type [T]. */ @@ -506,7 +505,7 @@ private constructor( * Printed text that is not part of a field, section heading or table: a title, an * instruction, a note. With it the form JSON holds every printed word of its region. */ - fun visitText(text: Text): T + fun visitText(text: FormText): T /** * Maps an unknown variant of [Json] to a value of type [T]. @@ -545,7 +544,7 @@ private constructor( } ?: Json(_json = json) } "text" -> { - return tryDeserialize(node, jacksonTypeRef())?.let { + return tryDeserialize(node, jacksonTypeRef())?.let { Json(text = it, _json = json) } ?: Json(_json = json) } @@ -572,2972 +571,6 @@ private constructor( } } } - - /** - * Printed text that is not part of a field, section heading or table: a title, an - * instruction, a note. With it the form JSON holds every printed word of its region. - */ - class Text - @JsonCreator(mode = JsonCreator.Mode.DISABLED) - private constructor( - private val value: JsonField, - private val bbox: JsonField>, - private val grounding: JsonField, - private val type: JsonField, - private val additionalProperties: MutableMap, - ) { - - @JsonCreator - private constructor( - @JsonProperty("value") @ExcludeMissing value: JsonField = JsonMissing.of(), - @JsonProperty("bbox") - @ExcludeMissing - bbox: JsonField> = JsonMissing.of(), - @JsonProperty("grounding") - @ExcludeMissing - grounding: JsonField = JsonMissing.of(), - @JsonProperty("type") @ExcludeMissing type: JsonField = JsonMissing.of(), - ) : this(value, bbox, grounding, type, mutableMapOf()) - - /** - * The printed text, verbatim - * - * @throws LlamaCloudInvalidDataException if the JSON field has an unexpected type or is - * unexpectedly missing or null (e.g. if the server responded with an unexpected - * value). - */ - fun value(): String = value.getRequired("value") - - /** - * Bounding boxes of the text on the page, if attributed. - * - * @throws LlamaCloudInvalidDataException if the JSON field has an unexpected type (e.g. - * if the server responded with an unexpected value). - */ - fun bbox(): Optional> = bbox.getOptional("bbox") - - /** - * Optional grounding for a field's printed text; boolean states have no text spans. - * - * @throws LlamaCloudInvalidDataException if the JSON field has an unexpected type (e.g. - * if the server responded with an unexpected value). - */ - fun grounding(): Optional = grounding.getOptional("grounding") - - /** - * Form text node - * - * @throws LlamaCloudInvalidDataException if the JSON field has an unexpected type (e.g. - * if the server responded with an unexpected value). - */ - fun type(): Optional = type.getOptional("type") - - /** - * Returns the raw JSON value of [value]. - * - * Unlike [value], this method doesn't throw if the JSON field has an unexpected type. - */ - @JsonProperty("value") @ExcludeMissing fun _value(): JsonField = value - - /** - * Returns the raw JSON value of [bbox]. - * - * Unlike [bbox], this method doesn't throw if the JSON field has an unexpected type. - */ - @JsonProperty("bbox") @ExcludeMissing fun _bbox(): JsonField> = bbox - - /** - * Returns the raw JSON value of [grounding]. - * - * Unlike [grounding], this method doesn't throw if the JSON field has an unexpected - * type. - */ - @JsonProperty("grounding") - @ExcludeMissing - fun _grounding(): JsonField = grounding - - /** - * Returns the raw JSON value of [type]. - * - * Unlike [type], this method doesn't throw if the JSON field has an unexpected type. - */ - @JsonProperty("type") @ExcludeMissing fun _type(): JsonField = type - - @JsonAnySetter - private fun putAdditionalProperty(key: String, value: JsonValue) { - additionalProperties.put(key, value) - } - - @JsonAnyGetter - @ExcludeMissing - fun _additionalProperties(): Map = - Collections.unmodifiableMap(additionalProperties) - - fun toBuilder() = Builder().from(this) - - companion object { - - /** - * Returns a mutable builder for constructing an instance of [Text]. - * - * The following fields are required: - * ```java - * .value() - * ``` - */ - @JvmStatic fun builder() = Builder() - } - - /** A builder for [Text]. */ - class Builder internal constructor() { - - private var value: JsonField? = null - private var bbox: JsonField>? = null - private var grounding: JsonField = JsonMissing.of() - private var type: JsonField = JsonMissing.of() - private var additionalProperties: MutableMap = mutableMapOf() - - @JvmSynthetic - internal fun from(text: Text) = apply { - value = text.value - bbox = text.bbox.map { it.toMutableList() } - grounding = text.grounding - type = text.type - additionalProperties = text.additionalProperties.toMutableMap() - } - - /** The printed text, verbatim */ - fun value(value: String) = value(JsonField.of(value)) - - /** - * Sets [Builder.value] to an arbitrary JSON value. - * - * You should usually call [Builder.value] with a well-typed [String] value instead. - * This method is primarily for setting the field to an undocumented or not yet - * supported value. - */ - fun value(value: JsonField) = apply { this.value = value } - - /** Bounding boxes of the text on the page, if attributed. */ - fun bbox(bbox: List?) = bbox(JsonField.ofNullable(bbox)) - - /** Alias for calling [Builder.bbox] with `bbox.orElse(null)`. */ - fun bbox(bbox: Optional>) = bbox(bbox.getOrNull()) - - /** - * Sets [Builder.bbox] to an arbitrary JSON value. - * - * You should usually call [Builder.bbox] with a well-typed `List` value - * instead. This method is primarily for setting the field to an undocumented or not - * yet supported value. - */ - fun bbox(bbox: JsonField>) = apply { - this.bbox = bbox.map { it.toMutableList() } - } - - /** - * Adds a single [BBox] to [Builder.bbox]. - * - * @throws IllegalStateException if the field was previously set to a non-list. - */ - fun addBbox(bbox: BBox) = apply { - this.bbox = - (this.bbox ?: JsonField.of(mutableListOf())).also { - checkKnown("bbox", it).add(bbox) - } - } - - /** - * Optional grounding for a field's printed text; boolean states have no text spans. - */ - fun grounding(grounding: Grounding?) = grounding(JsonField.ofNullable(grounding)) - - /** Alias for calling [Builder.grounding] with `grounding.orElse(null)`. */ - fun grounding(grounding: Optional) = grounding(grounding.getOrNull()) - - /** - * Sets [Builder.grounding] to an arbitrary JSON value. - * - * You should usually call [Builder.grounding] with a well-typed [Grounding] value - * instead. This method is primarily for setting the field to an undocumented or not - * yet supported value. - */ - fun grounding(grounding: JsonField) = apply { - this.grounding = grounding - } - - /** Form text node */ - fun type(type: Type) = type(JsonField.of(type)) - - /** - * Sets [Builder.type] to an arbitrary JSON value. - * - * You should usually call [Builder.type] with a well-typed [Type] value instead. - * This method is primarily for setting the field to an undocumented or not yet - * supported value. - */ - fun type(type: JsonField) = apply { this.type = type } - - fun additionalProperties(additionalProperties: Map) = apply { - this.additionalProperties.clear() - putAllAdditionalProperties(additionalProperties) - } - - fun putAdditionalProperty(key: String, value: JsonValue) = apply { - additionalProperties.put(key, value) - } - - fun putAllAdditionalProperties(additionalProperties: Map) = - apply { - this.additionalProperties.putAll(additionalProperties) - } - - fun removeAdditionalProperty(key: String) = apply { - additionalProperties.remove(key) - } - - fun removeAllAdditionalProperties(keys: Set) = apply { - keys.forEach(::removeAdditionalProperty) - } - - /** - * Returns an immutable instance of [Text]. - * - * Further updates to this [Builder] will not mutate the returned instance. - * - * The following fields are required: - * ```java - * .value() - * ``` - * - * @throws IllegalStateException if any required field is unset. - */ - fun build(): Text = - Text( - checkRequired("value", value), - (bbox ?: JsonMissing.of()).map { it.toImmutable() }, - grounding, - type, - additionalProperties.toMutableMap(), - ) - } - - private var validated: Boolean = false - - /** - * Validates that the types of all values in this object match their expected types - * recursively. - * - * This method is _not_ forwards compatible with new types from the API for existing - * fields. - * - * @throws LlamaCloudInvalidDataException if any value type in this object doesn't match - * its expected type. - */ - fun validate(): Text = apply { - if (validated) { - return@apply - } - - value() - bbox().ifPresent { it.forEach { it.validate() } } - grounding().ifPresent { it.validate() } - type().ifPresent { it.validate() } - validated = true - } - - fun isValid(): Boolean = - try { - validate() - true - } catch (e: LlamaCloudInvalidDataException) { - false - } - - /** - * Returns a score indicating how many valid values are contained in this object - * recursively. - * - * Used for best match union deserialization. - */ - @JvmSynthetic - internal fun validity(): Int = - (if (value.asKnown().isPresent) 1 else 0) + - (bbox.asKnown().getOrNull()?.sumOf { it.validity().toInt() } ?: 0) + - (grounding.asKnown().getOrNull()?.validity() ?: 0) + - (type.asKnown().getOrNull()?.validity() ?: 0) - - /** Optional grounding for a field's printed text; boolean states have no text spans. */ - class Grounding - @JsonCreator(mode = JsonCreator.Mode.DISABLED) - private constructor( - private val id: JsonField, - private val label: JsonField