// File generated from our OpenAPI spec by Stainless.

package com.openai.models.responses

import com.fasterxml.jackson.annotation.JsonAnyGetter
import com.fasterxml.jackson.annotation.JsonAnySetter
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.ObjectCodec
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
import com.openai.core.BaseDeserializer
import com.openai.core.BaseSerializer
import com.openai.core.Enum
import com.openai.core.ExcludeMissing
import com.openai.core.JsonField
import com.openai.core.JsonMissing
import com.openai.core.JsonValue
import com.openai.core.checkKnown
import com.openai.core.checkRequired
import com.openai.core.getOrThrow
import com.openai.core.toImmutable
import com.openai.errors.OpenAIInvalidDataException
import java.util.Collections
import java.util.Objects
import java.util.Optional
import kotlin.jvm.optionals.getOrNull

/** A click action. */
@JsonDeserialize(using = ComputerAction.Deserializer::class)
@JsonSerialize(using = ComputerAction.Serializer::class)
class ComputerAction
private constructor(
    private val click: Click? = null,
    private val doubleClick: DoubleClick? = null,
    private val drag: Drag? = null,
    private val keypress: Keypress? = null,
    private val move: Move? = null,
    private val screenshot: JsonValue? = null,
    private val scroll: Scroll? = null,
    private val type: Type? = null,
    private val wait: JsonValue? = null,
    private val _json: JsonValue? = null,
) {

    /** A click action. */
    fun click(): Optional<Click> = Optional.ofNullable(click)

    /** A double click action. */
    fun doubleClick(): Optional<DoubleClick> = Optional.ofNullable(doubleClick)

    /** A drag action. */
    fun drag(): Optional<Drag> = Optional.ofNullable(drag)

    /** A collection of keypresses the model would like to perform. */
    fun keypress(): Optional<Keypress> = Optional.ofNullable(keypress)

    /** A mouse move action. */
    fun move(): Optional<Move> = Optional.ofNullable(move)

    /** A screenshot action. */
    fun screenshot(): Optional<JsonValue> = Optional.ofNullable(screenshot)

    /** A scroll action. */
    fun scroll(): Optional<Scroll> = Optional.ofNullable(scroll)

    /** An action to type in text. */
    fun type(): Optional<Type> = Optional.ofNullable(type)

    /** A wait action. */
    fun wait(): Optional<JsonValue> = Optional.ofNullable(wait)

    fun isClick(): Boolean = click != null

    fun isDoubleClick(): Boolean = doubleClick != null

    fun isDrag(): Boolean = drag != null

    fun isKeypress(): Boolean = keypress != null

    fun isMove(): Boolean = move != null

    fun isScreenshot(): Boolean = screenshot != null

    fun isScroll(): Boolean = scroll != null

    fun isType(): Boolean = type != null

    fun isWait(): Boolean = wait != null

    /** A click action. */
    fun asClick(): Click = click.getOrThrow("click")

    /** A double click action. */
    fun asDoubleClick(): DoubleClick = doubleClick.getOrThrow("doubleClick")

    /** A drag action. */
    fun asDrag(): Drag = drag.getOrThrow("drag")

    /** A collection of keypresses the model would like to perform. */
    fun asKeypress(): Keypress = keypress.getOrThrow("keypress")

    /** A mouse move action. */
    fun asMove(): Move = move.getOrThrow("move")

    /** A screenshot action. */
    fun asScreenshot(): JsonValue = screenshot.getOrThrow("screenshot")

    /** A scroll action. */
    fun asScroll(): Scroll = scroll.getOrThrow("scroll")

    /** An action to type in text. */
    fun asType(): Type = type.getOrThrow("type")

    /** A wait action. */
    fun asWait(): JsonValue = wait.getOrThrow("wait")

    fun _json(): Optional<JsonValue> = Optional.ofNullable(_json)

    /**
     * Maps this instance's current variant to a value of type [T] using the given [visitor].
     *
     * Note that this method is _not_ forwards compatible with new variants from the API, unless
     * [visitor] overrides [Visitor.unknown]. To handle variants not known to this version of the
     * SDK gracefully, consider overriding [Visitor.unknown]:
     * ```java
     * import com.openai.core.JsonValue;
     * import java.util.Optional;
     *
     * Optional<String> result = computerAction.accept(new ComputerAction.Visitor<Optional<String>>() {
     *     @Override
     *     public Optional<String> visitClick(Click click) {
     *         return Optional.of(click.toString());
     *     }
     *
     *     // ...
     *
     *     @Override
     *     public Optional<String> unknown(JsonValue json) {
     *         // Or inspect the `json`.
     *         return Optional.empty();
     *     }
     * });
     * ```
     *
     * @throws OpenAIInvalidDataException if [Visitor.unknown] is not overridden in [visitor] and
     *   the current variant is unknown.
     */
    fun <T> accept(visitor: Visitor<T>): T =
        when {
            click != null -> visitor.visitClick(click)
            doubleClick != null -> visitor.visitDoubleClick(doubleClick)
            drag != null -> visitor.visitDrag(drag)
            keypress != null -> visitor.visitKeypress(keypress)
            move != null -> visitor.visitMove(move)
            screenshot != null -> visitor.visitScreenshot(screenshot)
            scroll != null -> visitor.visitScroll(scroll)
            type != null -> visitor.visitType(type)
            wait != null -> visitor.visitWait(wait)
            else -> visitor.unknown(_json)
        }

    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 OpenAIInvalidDataException if any value type in this object doesn't match its
     *   expected type.
     */
    fun validate(): ComputerAction = apply {
        if (validated) {
            return@apply
        }

        accept(
            object : Visitor<Unit> {
                override fun visitClick(click: Click) {
                    click.validate()
                }

                override fun visitDoubleClick(doubleClick: DoubleClick) {
                    doubleClick.validate()
                }

                override fun visitDrag(drag: Drag) {
                    drag.validate()
                }

                override fun visitKeypress(keypress: Keypress) {
                    keypress.validate()
                }

                override fun visitMove(move: Move) {
                    move.validate()
                }

                override fun visitScreenshot(screenshot: JsonValue) {
                    screenshot.let {
                        if (it != JsonValue.from(mapOf("type" to "screenshot"))) {
                            throw OpenAIInvalidDataException(
                                "'screenshot' is invalid, received $it"
                            )
                        }
                    }
                }

                override fun visitScroll(scroll: Scroll) {
                    scroll.validate()
                }

                override fun visitType(type: Type) {
                    type.validate()
                }

                override fun visitWait(wait: JsonValue) {
                    wait.let {
                        if (it != JsonValue.from(mapOf("type" to "wait"))) {
                            throw OpenAIInvalidDataException("'wait' is invalid, received $it")
                        }
                    }
                }
            }
        )
        validated = true
    }

    fun isValid(): Boolean =
        try {
            validate()
            true
        } catch (e: OpenAIInvalidDataException) {
            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 =
        accept(
            object : Visitor<Int> {
                override fun visitClick(click: Click) = click.validity()

                override fun visitDoubleClick(doubleClick: DoubleClick) = doubleClick.validity()

                override fun visitDrag(drag: Drag) = drag.validity()

                override fun visitKeypress(keypress: Keypress) = keypress.validity()

                override fun visitMove(move: Move) = move.validity()

                override fun visitScreenshot(screenshot: JsonValue) =
                    screenshot.let {
                        if (it == JsonValue.from(mapOf("type" to "screenshot"))) 1 else 0
                    }

                override fun visitScroll(scroll: Scroll) = scroll.validity()

                override fun visitType(type: Type) = type.validity()

                override fun visitWait(wait: JsonValue) =
                    wait.let { if (it == JsonValue.from(mapOf("type" to "wait"))) 1 else 0 }

                override fun unknown(json: JsonValue?) = 0
            }
        )

    override fun equals(other: Any?): Boolean {
        if (this === other) {
            return true
        }

        return other is ComputerAction &&
            click == other.click &&
            doubleClick == other.doubleClick &&
            drag == other.drag &&
            keypress == other.keypress &&
            move == other.move &&
            screenshot == other.screenshot &&
            scroll == other.scroll &&
            type == other.type &&
            wait == other.wait
    }

    override fun hashCode(): Int =
        Objects.hash(click, doubleClick, drag, keypress, move, screenshot, scroll, type, wait)

    override fun toString(): String =
        when {
            click != null -> "ComputerAction{click=$click}"
            doubleClick != null -> "ComputerAction{doubleClick=$doubleClick}"
            drag != null -> "ComputerAction{drag=$drag}"
            keypress != null -> "ComputerAction{keypress=$keypress}"
            move != null -> "ComputerAction{move=$move}"
            screenshot != null -> "ComputerAction{screenshot=$screenshot}"
            scroll != null -> "ComputerAction{scroll=$scroll}"
            type != null -> "ComputerAction{type=$type}"
            wait != null -> "ComputerAction{wait=$wait}"
            _json != null -> "ComputerAction{_unknown=$_json}"
            else -> throw IllegalStateException("Invalid ComputerAction")
        }

    companion object {

        /** A click action. */
        @JvmStatic fun ofClick(click: Click) = ComputerAction(click = click)

        /** A double click action. */
        @JvmStatic
        fun ofDoubleClick(doubleClick: DoubleClick) = ComputerAction(doubleClick = doubleClick)

        /** A drag action. */
        @JvmStatic fun ofDrag(drag: Drag) = ComputerAction(drag = drag)

        /** A collection of keypresses the model would like to perform. */
        @JvmStatic fun ofKeypress(keypress: Keypress) = ComputerAction(keypress = keypress)

        /** A mouse move action. */
        @JvmStatic fun ofMove(move: Move) = ComputerAction(move = move)

        /** A screenshot action. */
        @JvmStatic
        fun ofScreenshot() =
            ComputerAction(screenshot = JsonValue.from(mapOf("type" to "screenshot")))

        /** A scroll action. */
        @JvmStatic fun ofScroll(scroll: Scroll) = ComputerAction(scroll = scroll)

        /** An action to type in text. */
        @JvmStatic fun ofType(type: Type) = ComputerAction(type = type)

        /** A wait action. */
        @JvmStatic fun ofWait() = ComputerAction(wait = JsonValue.from(mapOf("type" to "wait")))
    }

    /**
     * An interface that defines how to map each variant of [ComputerAction] to a value of type [T].
     */
    interface Visitor<out T> {

        /** A click action. */
        fun visitClick(click: Click): T

        /** A double click action. */
        fun visitDoubleClick(doubleClick: DoubleClick): T

        /** A drag action. */
        fun visitDrag(drag: Drag): T

        /** A collection of keypresses the model would like to perform. */
        fun visitKeypress(keypress: Keypress): T

        /** A mouse move action. */
        fun visitMove(move: Move): T

        /** A screenshot action. */
        fun visitScreenshot(screenshot: JsonValue): T

        /** A scroll action. */
        fun visitScroll(scroll: Scroll): T

        /** An action to type in text. */
        fun visitType(type: Type): T

        /** A wait action. */
        fun visitWait(wait: JsonValue): T

        /**
         * Maps an unknown variant of [ComputerAction] to a value of type [T].
         *
         * An instance of [ComputerAction] can contain an unknown variant if it was deserialized
         * from data that doesn't match any known variant. For example, if the SDK is on an older
         * version than the API, then the API may respond with new variants that the SDK is unaware
         * of.
         *
         * @throws OpenAIInvalidDataException in the default implementation.
         */
        fun unknown(json: JsonValue?): T {
            throw OpenAIInvalidDataException("Unknown ComputerAction: $json")
        }
    }

    internal class Deserializer : BaseDeserializer<ComputerAction>(ComputerAction::class) {

        override fun ObjectCodec.deserialize(node: JsonNode): ComputerAction {
            val json = JsonValue.fromJsonNode(node)
            val type = json.asObject().getOrNull()?.get("type")?.asString()?.getOrNull()

            when (type) {
                "click" -> {
                    return tryDeserialize(node, jacksonTypeRef<Click>())?.let {
                        ComputerAction(click = it, _json = json)
                    } ?: ComputerAction(_json = json)
                }
                "double_click" -> {
                    return tryDeserialize(node, jacksonTypeRef<DoubleClick>())?.let {
                        ComputerAction(doubleClick = it, _json = json)
                    } ?: ComputerAction(_json = json)
                }
                "drag" -> {
                    return tryDeserialize(node, jacksonTypeRef<Drag>())?.let {
                        ComputerAction(drag = it, _json = json)
                    } ?: ComputerAction(_json = json)
                }
                "keypress" -> {
                    return tryDeserialize(node, jacksonTypeRef<Keypress>())?.let {
                        ComputerAction(keypress = it, _json = json)
                    } ?: ComputerAction(_json = json)
                }
                "move" -> {
                    return tryDeserialize(node, jacksonTypeRef<Move>())?.let {
                        ComputerAction(move = it, _json = json)
                    } ?: ComputerAction(_json = json)
                }
                "screenshot" -> {
                    return tryDeserialize(node, jacksonTypeRef<JsonValue>())
                        ?.let { ComputerAction(screenshot = it, _json = json) }
                        ?.takeIf { it.isValid() } ?: ComputerAction(_json = json)
                }
                "scroll" -> {
                    return tryDeserialize(node, jacksonTypeRef<Scroll>())?.let {
                        ComputerAction(scroll = it, _json = json)
                    } ?: ComputerAction(_json = json)
                }
                "type" -> {
                    return tryDeserialize(node, jacksonTypeRef<Type>())?.let {
                        ComputerAction(type = it, _json = json)
                    } ?: ComputerAction(_json = json)
                }
                "wait" -> {
                    return tryDeserialize(node, jacksonTypeRef<JsonValue>())
                        ?.let { ComputerAction(wait = it, _json = json) }
                        ?.takeIf { it.isValid() } ?: ComputerAction(_json = json)
                }
            }

            return ComputerAction(_json = json)
        }
    }

    internal class Serializer : BaseSerializer<ComputerAction>(ComputerAction::class) {

        override fun serialize(
            value: ComputerAction,
            generator: JsonGenerator,
            provider: SerializerProvider,
        ) {
            when {
                value.click != null -> generator.writeObject(value.click)
                value.doubleClick != null -> generator.writeObject(value.doubleClick)
                value.drag != null -> generator.writeObject(value.drag)
                value.keypress != null -> generator.writeObject(value.keypress)
                value.move != null -> generator.writeObject(value.move)
                value.screenshot != null -> generator.writeObject(value.screenshot)
                value.scroll != null -> generator.writeObject(value.scroll)
                value.type != null -> generator.writeObject(value.type)
                value.wait != null -> generator.writeObject(value.wait)
                value._json != null -> generator.writeObject(value._json)
                else -> throw IllegalStateException("Invalid ComputerAction")
            }
        }
    }

    /** A click action. */
    class Click
    @JsonCreator(mode = JsonCreator.Mode.DISABLED)
    private constructor(
        private val button: JsonField<Button>,
        private val type: JsonValue,
        private val x: JsonField<Long>,
        private val y: JsonField<Long>,
        private val keys: JsonField<List<String>>,
        private val additionalProperties: MutableMap<String, JsonValue>,
    ) {

        @JsonCreator
        private constructor(
            @JsonProperty("button") @ExcludeMissing button: JsonField<Button> = JsonMissing.of(),
            @JsonProperty("type") @ExcludeMissing type: JsonValue = JsonMissing.of(),
            @JsonProperty("x") @ExcludeMissing x: JsonField<Long> = JsonMissing.of(),
            @JsonProperty("y") @ExcludeMissing y: JsonField<Long> = JsonMissing.of(),
            @JsonProperty("keys") @ExcludeMissing keys: JsonField<List<String>> = JsonMissing.of(),
        ) : this(button, type, x, y, keys, mutableMapOf())

        /**
         * Indicates which mouse button was pressed during the click. One of `left`, `right`,
         * `wheel`, `back`, or `forward`.
         *
         * @throws OpenAIInvalidDataException 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 button(): Button = button.getRequired("button")

        /**
         * Specifies the event type. For a click action, this property is always `click`.
         *
         * Expected to always return the following:
         * ```java
         * JsonValue.from("click")
         * ```
         *
         * However, this method can be useful for debugging and logging (e.g. if the server
         * responded with an unexpected value).
         */
        @JsonProperty("type") @ExcludeMissing fun _type(): JsonValue = type

        /**
         * The x-coordinate where the click occurred.
         *
         * @throws OpenAIInvalidDataException 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 x(): Long = x.getRequired("x")

        /**
         * The y-coordinate where the click occurred.
         *
         * @throws OpenAIInvalidDataException 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 y(): Long = y.getRequired("y")

        /**
         * The keys being held while clicking.
         *
         * @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
         *   server responded with an unexpected value).
         */
        fun keys(): Optional<List<String>> = keys.getOptional("keys")

        /**
         * Returns the raw JSON value of [button].
         *
         * Unlike [button], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("button") @ExcludeMissing fun _button(): JsonField<Button> = button

        /**
         * Returns the raw JSON value of [x].
         *
         * Unlike [x], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("x") @ExcludeMissing fun _x(): JsonField<Long> = x

        /**
         * Returns the raw JSON value of [y].
         *
         * Unlike [y], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("y") @ExcludeMissing fun _y(): JsonField<Long> = y

        /**
         * Returns the raw JSON value of [keys].
         *
         * Unlike [keys], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("keys") @ExcludeMissing fun _keys(): JsonField<List<String>> = keys

        @JsonAnySetter
        private fun putAdditionalProperty(key: String, value: JsonValue) {
            additionalProperties.put(key, value)
        }

        @JsonAnyGetter
        @ExcludeMissing
        fun _additionalProperties(): Map<String, JsonValue> =
            Collections.unmodifiableMap(additionalProperties)

        fun toBuilder() = Builder().from(this)

        companion object {

            /**
             * Returns a mutable builder for constructing an instance of [Click].
             *
             * The following fields are required:
             * ```java
             * .button()
             * .x()
             * .y()
             * ```
             */
            @JvmStatic fun builder() = Builder()
        }

        /** A builder for [Click]. */
        class Builder internal constructor() {

            private var button: JsonField<Button>? = null
            private var type: JsonValue = JsonValue.from("click")
            private var x: JsonField<Long>? = null
            private var y: JsonField<Long>? = null
            private var keys: JsonField<MutableList<String>>? = null
            private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()

            @JvmSynthetic
            internal fun from(click: Click) = apply {
                button = click.button
                type = click.type
                x = click.x
                y = click.y
                keys = click.keys.map { it.toMutableList() }
                additionalProperties = click.additionalProperties.toMutableMap()
            }

            /**
             * Indicates which mouse button was pressed during the click. One of `left`, `right`,
             * `wheel`, `back`, or `forward`.
             */
            fun button(button: Button) = button(JsonField.of(button))

            /**
             * Sets [Builder.button] to an arbitrary JSON value.
             *
             * You should usually call [Builder.button] with a well-typed [Button] value instead.
             * This method is primarily for setting the field to an undocumented or not yet
             * supported value.
             */
            fun button(button: JsonField<Button>) = apply { this.button = button }

            /**
             * Sets the field to an arbitrary JSON value.
             *
             * It is usually unnecessary to call this method because the field defaults to the
             * following:
             * ```java
             * JsonValue.from("click")
             * ```
             *
             * This method is primarily for setting the field to an undocumented or not yet
             * supported value.
             */
            fun type(type: JsonValue) = apply { this.type = type }

            /** The x-coordinate where the click occurred. */
            fun x(x: Long) = x(JsonField.of(x))

            /**
             * Sets [Builder.x] to an arbitrary JSON value.
             *
             * You should usually call [Builder.x] with a well-typed [Long] value instead. This
             * method is primarily for setting the field to an undocumented or not yet supported
             * value.
             */
            fun x(x: JsonField<Long>) = apply { this.x = x }

            /** The y-coordinate where the click occurred. */
            fun y(y: Long) = y(JsonField.of(y))

            /**
             * Sets [Builder.y] to an arbitrary JSON value.
             *
             * You should usually call [Builder.y] with a well-typed [Long] value instead. This
             * method is primarily for setting the field to an undocumented or not yet supported
             * value.
             */
            fun y(y: JsonField<Long>) = apply { this.y = y }

            /** The keys being held while clicking. */
            fun keys(keys: List<String>?) = keys(JsonField.ofNullable(keys))

            /** Alias for calling [Builder.keys] with `keys.orElse(null)`. */
            fun keys(keys: Optional<List<String>>) = keys(keys.getOrNull())

            /**
             * Sets [Builder.keys] to an arbitrary JSON value.
             *
             * You should usually call [Builder.keys] with a well-typed `List<String>` value
             * instead. This method is primarily for setting the field to an undocumented or not yet
             * supported value.
             */
            fun keys(keys: JsonField<List<String>>) = apply {
                this.keys = keys.map { it.toMutableList() }
            }

            /**
             * Adds a single [String] to [keys].
             *
             * @throws IllegalStateException if the field was previously set to a non-list.
             */
            fun addKey(key: String) = apply {
                keys =
                    (keys ?: JsonField.of(mutableListOf())).also { checkKnown("keys", it).add(key) }
            }

            fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
                this.additionalProperties.clear()
                putAllAdditionalProperties(additionalProperties)
            }

            fun putAdditionalProperty(key: String, value: JsonValue) = apply {
                additionalProperties.put(key, value)
            }

            fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
                this.additionalProperties.putAll(additionalProperties)
            }

            fun removeAdditionalProperty(key: String) = apply { additionalProperties.remove(key) }

            fun removeAllAdditionalProperties(keys: Set<String>) = apply {
                keys.forEach(::removeAdditionalProperty)
            }

            /**
             * Returns an immutable instance of [Click].
             *
             * Further updates to this [Builder] will not mutate the returned instance.
             *
             * The following fields are required:
             * ```java
             * .button()
             * .x()
             * .y()
             * ```
             *
             * @throws IllegalStateException if any required field is unset.
             */
            fun build(): Click =
                Click(
                    checkRequired("button", button),
                    type,
                    checkRequired("x", x),
                    checkRequired("y", y),
                    (keys ?: JsonMissing.of()).map { it.toImmutable() },
                    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 OpenAIInvalidDataException if any value type in this object doesn't match its
         *   expected type.
         */
        fun validate(): Click = apply {
            if (validated) {
                return@apply
            }

            button().validate()
            _type().let {
                if (it != JsonValue.from("click")) {
                    throw OpenAIInvalidDataException("'type' is invalid, received $it")
                }
            }
            x()
            y()
            keys()
            validated = true
        }

        fun isValid(): Boolean =
            try {
                validate()
                true
            } catch (e: OpenAIInvalidDataException) {
                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 =
            (button.asKnown().getOrNull()?.validity() ?: 0) +
                type.let { if (it == JsonValue.from("click")) 1 else 0 } +
                (if (x.asKnown().isPresent) 1 else 0) +
                (if (y.asKnown().isPresent) 1 else 0) +
                (keys.asKnown().getOrNull()?.size ?: 0)

        /**
         * Indicates which mouse button was pressed during the click. One of `left`, `right`,
         * `wheel`, `back`, or `forward`.
         */
        class Button @JsonCreator private constructor(private val value: JsonField<String>) : Enum {

            /**
             * Returns this class instance's raw value.
             *
             * This is usually only useful if this instance was deserialized from data that doesn't
             * match any known member, and you want to know that value. For example, if the SDK is
             * on an older version than the API, then the API may respond with new members that the
             * SDK is unaware of.
             */
            @com.fasterxml.jackson.annotation.JsonValue fun _value(): JsonField<String> = value

            companion object {

                @JvmField val LEFT = of("left")

                @JvmField val RIGHT = of("right")

                @JvmField val WHEEL = of("wheel")

                @JvmField val BACK = of("back")

                @JvmField val FORWARD = of("forward")

                @JvmStatic fun of(value: String) = Button(JsonField.of(value))
            }

            /** An enum containing [Button]'s known values. */
            enum class Known {
                LEFT,
                RIGHT,
                WHEEL,
                BACK,
                FORWARD,
            }

            /**
             * An enum containing [Button]'s known values, as well as an [_UNKNOWN] member.
             *
             * An instance of [Button] can contain an unknown value in a couple of cases:
             * - It was deserialized from data that doesn't match any known member. For example, if
             *   the SDK is on an older version than the API, then the API may respond with new
             *   members that the SDK is unaware of.
             * - It was constructed with an arbitrary value using the [of] method.
             */
            enum class Value {
                LEFT,
                RIGHT,
                WHEEL,
                BACK,
                FORWARD,
                /**
                 * An enum member indicating that [Button] was instantiated with an unknown value.
                 */
                _UNKNOWN,
            }

            /**
             * Returns an enum member corresponding to this class instance's value, or
             * [Value._UNKNOWN] if the class was instantiated with an unknown value.
             *
             * Use the [known] method instead if you're certain the value is always known or if you
             * want to throw for the unknown case.
             */
            fun value(): Value =
                when (this) {
                    LEFT -> Value.LEFT
                    RIGHT -> Value.RIGHT
                    WHEEL -> Value.WHEEL
                    BACK -> Value.BACK
                    FORWARD -> Value.FORWARD
                    else -> Value._UNKNOWN
                }

            /**
             * Returns an enum member corresponding to this class instance's value.
             *
             * Use the [value] method instead if you're uncertain the value is always known and
             * don't want to throw for the unknown case.
             *
             * @throws OpenAIInvalidDataException if this class instance's value is a not a known
             *   member.
             */
            fun known(): Known =
                when (this) {
                    LEFT -> Known.LEFT
                    RIGHT -> Known.RIGHT
                    WHEEL -> Known.WHEEL
                    BACK -> Known.BACK
                    FORWARD -> Known.FORWARD
                    else -> throw OpenAIInvalidDataException("Unknown Button: $value")
                }

            /**
             * Returns this class instance's primitive wire representation.
             *
             * This differs from the [toString] method because that method is primarily for
             * debugging and generally doesn't throw.
             *
             * @throws OpenAIInvalidDataException if this class instance's value does not have the
             *   expected primitive type.
             */
            fun asString(): String =
                _value().asString().orElseThrow {
                    OpenAIInvalidDataException("Value is not a String")
                }

            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 OpenAIInvalidDataException if any value type in this object doesn't match its
             *   expected type.
             */
            fun validate(): Button = apply {
                if (validated) {
                    return@apply
                }

                known()
                validated = true
            }

            fun isValid(): Boolean =
                try {
                    validate()
                    true
                } catch (e: OpenAIInvalidDataException) {
                    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() == Value._UNKNOWN) 0 else 1

            override fun equals(other: Any?): Boolean {
                if (this === other) {
                    return true
                }

                return other is Button && value == other.value
            }

            override fun hashCode() = value.hashCode()

            override fun toString() = value.toString()
        }

        override fun equals(other: Any?): Boolean {
            if (this === other) {
                return true
            }

            return other is Click &&
                button == other.button &&
                type == other.type &&
                x == other.x &&
                y == other.y &&
                keys == other.keys &&
                additionalProperties == other.additionalProperties
        }

        private val hashCode: Int by lazy {
            Objects.hash(button, type, x, y, keys, additionalProperties)
        }

        override fun hashCode(): Int = hashCode

        override fun toString() =
            "Click{button=$button, type=$type, x=$x, y=$y, keys=$keys, additionalProperties=$additionalProperties}"
    }

    /** A double click action. */
    class DoubleClick
    @JsonCreator(mode = JsonCreator.Mode.DISABLED)
    private constructor(
        private val keys: JsonField<List<String>>,
        private val type: JsonValue,
        private val x: JsonField<Long>,
        private val y: JsonField<Long>,
        private val additionalProperties: MutableMap<String, JsonValue>,
    ) {

        @JsonCreator
        private constructor(
            @JsonProperty("keys") @ExcludeMissing keys: JsonField<List<String>> = JsonMissing.of(),
            @JsonProperty("type") @ExcludeMissing type: JsonValue = JsonMissing.of(),
            @JsonProperty("x") @ExcludeMissing x: JsonField<Long> = JsonMissing.of(),
            @JsonProperty("y") @ExcludeMissing y: JsonField<Long> = JsonMissing.of(),
        ) : this(keys, type, x, y, mutableMapOf())

        /**
         * The keys being held while double-clicking.
         *
         * @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
         *   server responded with an unexpected value).
         */
        fun keys(): Optional<List<String>> = keys.getOptional("keys")

        /**
         * Specifies the event type. For a double click action, this property is always set to
         * `double_click`.
         *
         * Expected to always return the following:
         * ```java
         * JsonValue.from("double_click")
         * ```
         *
         * However, this method can be useful for debugging and logging (e.g. if the server
         * responded with an unexpected value).
         */
        @JsonProperty("type") @ExcludeMissing fun _type(): JsonValue = type

        /**
         * The x-coordinate where the double click occurred.
         *
         * @throws OpenAIInvalidDataException 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 x(): Long = x.getRequired("x")

        /**
         * The y-coordinate where the double click occurred.
         *
         * @throws OpenAIInvalidDataException 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 y(): Long = y.getRequired("y")

        /**
         * Returns the raw JSON value of [keys].
         *
         * Unlike [keys], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("keys") @ExcludeMissing fun _keys(): JsonField<List<String>> = keys

        /**
         * Returns the raw JSON value of [x].
         *
         * Unlike [x], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("x") @ExcludeMissing fun _x(): JsonField<Long> = x

        /**
         * Returns the raw JSON value of [y].
         *
         * Unlike [y], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("y") @ExcludeMissing fun _y(): JsonField<Long> = y

        @JsonAnySetter
        private fun putAdditionalProperty(key: String, value: JsonValue) {
            additionalProperties.put(key, value)
        }

        @JsonAnyGetter
        @ExcludeMissing
        fun _additionalProperties(): Map<String, JsonValue> =
            Collections.unmodifiableMap(additionalProperties)

        fun toBuilder() = Builder().from(this)

        companion object {

            /**
             * Returns a mutable builder for constructing an instance of [DoubleClick].
             *
             * The following fields are required:
             * ```java
             * .keys()
             * .x()
             * .y()
             * ```
             */
            @JvmStatic fun builder() = Builder()
        }

        /** A builder for [DoubleClick]. */
        class Builder internal constructor() {

            private var keys: JsonField<MutableList<String>>? = null
            private var type: JsonValue = JsonValue.from("double_click")
            private var x: JsonField<Long>? = null
            private var y: JsonField<Long>? = null
            private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()

            @JvmSynthetic
            internal fun from(doubleClick: DoubleClick) = apply {
                keys = doubleClick.keys.map { it.toMutableList() }
                type = doubleClick.type
                x = doubleClick.x
                y = doubleClick.y
                additionalProperties = doubleClick.additionalProperties.toMutableMap()
            }

            /** The keys being held while double-clicking. */
            fun keys(keys: List<String>?) = keys(JsonField.ofNullable(keys))

            /** Alias for calling [Builder.keys] with `keys.orElse(null)`. */
            fun keys(keys: Optional<List<String>>) = keys(keys.getOrNull())

            /**
             * Sets [Builder.keys] to an arbitrary JSON value.
             *
             * You should usually call [Builder.keys] with a well-typed `List<String>` value
             * instead. This method is primarily for setting the field to an undocumented or not yet
             * supported value.
             */
            fun keys(keys: JsonField<List<String>>) = apply {
                this.keys = keys.map { it.toMutableList() }
            }

            /**
             * Adds a single [String] to [keys].
             *
             * @throws IllegalStateException if the field was previously set to a non-list.
             */
            fun addKey(key: String) = apply {
                keys =
                    (keys ?: JsonField.of(mutableListOf())).also { checkKnown("keys", it).add(key) }
            }

            /**
             * Sets the field to an arbitrary JSON value.
             *
             * It is usually unnecessary to call this method because the field defaults to the
             * following:
             * ```java
             * JsonValue.from("double_click")
             * ```
             *
             * This method is primarily for setting the field to an undocumented or not yet
             * supported value.
             */
            fun type(type: JsonValue) = apply { this.type = type }

            /** The x-coordinate where the double click occurred. */
            fun x(x: Long) = x(JsonField.of(x))

            /**
             * Sets [Builder.x] to an arbitrary JSON value.
             *
             * You should usually call [Builder.x] with a well-typed [Long] value instead. This
             * method is primarily for setting the field to an undocumented or not yet supported
             * value.
             */
            fun x(x: JsonField<Long>) = apply { this.x = x }

            /** The y-coordinate where the double click occurred. */
            fun y(y: Long) = y(JsonField.of(y))

            /**
             * Sets [Builder.y] to an arbitrary JSON value.
             *
             * You should usually call [Builder.y] with a well-typed [Long] value instead. This
             * method is primarily for setting the field to an undocumented or not yet supported
             * value.
             */
            fun y(y: JsonField<Long>) = apply { this.y = y }

            fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
                this.additionalProperties.clear()
                putAllAdditionalProperties(additionalProperties)
            }

            fun putAdditionalProperty(key: String, value: JsonValue) = apply {
                additionalProperties.put(key, value)
            }

            fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
                this.additionalProperties.putAll(additionalProperties)
            }

            fun removeAdditionalProperty(key: String) = apply { additionalProperties.remove(key) }

            fun removeAllAdditionalProperties(keys: Set<String>) = apply {
                keys.forEach(::removeAdditionalProperty)
            }

            /**
             * Returns an immutable instance of [DoubleClick].
             *
             * Further updates to this [Builder] will not mutate the returned instance.
             *
             * The following fields are required:
             * ```java
             * .keys()
             * .x()
             * .y()
             * ```
             *
             * @throws IllegalStateException if any required field is unset.
             */
            fun build(): DoubleClick =
                DoubleClick(
                    checkRequired("keys", keys).map { it.toImmutable() },
                    type,
                    checkRequired("x", x),
                    checkRequired("y", y),
                    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 OpenAIInvalidDataException if any value type in this object doesn't match its
         *   expected type.
         */
        fun validate(): DoubleClick = apply {
            if (validated) {
                return@apply
            }

            keys()
            _type().let {
                if (it != JsonValue.from("double_click")) {
                    throw OpenAIInvalidDataException("'type' is invalid, received $it")
                }
            }
            x()
            y()
            validated = true
        }

        fun isValid(): Boolean =
            try {
                validate()
                true
            } catch (e: OpenAIInvalidDataException) {
                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 =
            (keys.asKnown().getOrNull()?.size ?: 0) +
                type.let { if (it == JsonValue.from("double_click")) 1 else 0 } +
                (if (x.asKnown().isPresent) 1 else 0) +
                (if (y.asKnown().isPresent) 1 else 0)

        override fun equals(other: Any?): Boolean {
            if (this === other) {
                return true
            }

            return other is DoubleClick &&
                keys == other.keys &&
                type == other.type &&
                x == other.x &&
                y == other.y &&
                additionalProperties == other.additionalProperties
        }

        private val hashCode: Int by lazy { Objects.hash(keys, type, x, y, additionalProperties) }

        override fun hashCode(): Int = hashCode

        override fun toString() =
            "DoubleClick{keys=$keys, type=$type, x=$x, y=$y, additionalProperties=$additionalProperties}"
    }

    /** A drag action. */
    class Drag
    @JsonCreator(mode = JsonCreator.Mode.DISABLED)
    private constructor(
        private val path: JsonField<List<Path>>,
        private val type: JsonValue,
        private val keys: JsonField<List<String>>,
        private val additionalProperties: MutableMap<String, JsonValue>,
    ) {

        @JsonCreator
        private constructor(
            @JsonProperty("path") @ExcludeMissing path: JsonField<List<Path>> = JsonMissing.of(),
            @JsonProperty("type") @ExcludeMissing type: JsonValue = JsonMissing.of(),
            @JsonProperty("keys") @ExcludeMissing keys: JsonField<List<String>> = JsonMissing.of(),
        ) : this(path, type, keys, mutableMapOf())

        /**
         * An array of coordinates representing the path of the drag action. Coordinates will appear
         * as an array of objects, eg
         *
         * ```
         * [
         *   { x: 100, y: 200 },
         *   { x: 200, y: 300 }
         * ]
         * ```
         *
         * @throws OpenAIInvalidDataException 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 path(): List<Path> = path.getRequired("path")

        /**
         * Specifies the event type. For a drag action, this property is always set to `drag`.
         *
         * Expected to always return the following:
         * ```java
         * JsonValue.from("drag")
         * ```
         *
         * However, this method can be useful for debugging and logging (e.g. if the server
         * responded with an unexpected value).
         */
        @JsonProperty("type") @ExcludeMissing fun _type(): JsonValue = type

        /**
         * The keys being held while dragging the mouse.
         *
         * @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
         *   server responded with an unexpected value).
         */
        fun keys(): Optional<List<String>> = keys.getOptional("keys")

        /**
         * Returns the raw JSON value of [path].
         *
         * Unlike [path], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("path") @ExcludeMissing fun _path(): JsonField<List<Path>> = path

        /**
         * Returns the raw JSON value of [keys].
         *
         * Unlike [keys], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("keys") @ExcludeMissing fun _keys(): JsonField<List<String>> = keys

        @JsonAnySetter
        private fun putAdditionalProperty(key: String, value: JsonValue) {
            additionalProperties.put(key, value)
        }

        @JsonAnyGetter
        @ExcludeMissing
        fun _additionalProperties(): Map<String, JsonValue> =
            Collections.unmodifiableMap(additionalProperties)

        fun toBuilder() = Builder().from(this)

        companion object {

            /**
             * Returns a mutable builder for constructing an instance of [Drag].
             *
             * The following fields are required:
             * ```java
             * .path()
             * ```
             */
            @JvmStatic fun builder() = Builder()
        }

        /** A builder for [Drag]. */
        class Builder internal constructor() {

            private var path: JsonField<MutableList<Path>>? = null
            private var type: JsonValue = JsonValue.from("drag")
            private var keys: JsonField<MutableList<String>>? = null
            private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()

            @JvmSynthetic
            internal fun from(drag: Drag) = apply {
                path = drag.path.map { it.toMutableList() }
                type = drag.type
                keys = drag.keys.map { it.toMutableList() }
                additionalProperties = drag.additionalProperties.toMutableMap()
            }

            /**
             * An array of coordinates representing the path of the drag action. Coordinates will
             * appear as an array of objects, eg
             *
             * ```
             * [
             *   { x: 100, y: 200 },
             *   { x: 200, y: 300 }
             * ]
             * ```
             */
            fun path(path: List<Path>) = path(JsonField.of(path))

            /**
             * Sets [Builder.path] to an arbitrary JSON value.
             *
             * You should usually call [Builder.path] with a well-typed `List<Path>` value instead.
             * This method is primarily for setting the field to an undocumented or not yet
             * supported value.
             */
            fun path(path: JsonField<List<Path>>) = apply {
                this.path = path.map { it.toMutableList() }
            }

            /**
             * Adds a single [Path] to [Builder.path].
             *
             * @throws IllegalStateException if the field was previously set to a non-list.
             */
            fun addPath(path: Path) = apply {
                this.path =
                    (this.path ?: JsonField.of(mutableListOf())).also {
                        checkKnown("path", it).add(path)
                    }
            }

            /**
             * Sets the field to an arbitrary JSON value.
             *
             * It is usually unnecessary to call this method because the field defaults to the
             * following:
             * ```java
             * JsonValue.from("drag")
             * ```
             *
             * This method is primarily for setting the field to an undocumented or not yet
             * supported value.
             */
            fun type(type: JsonValue) = apply { this.type = type }

            /** The keys being held while dragging the mouse. */
            fun keys(keys: List<String>?) = keys(JsonField.ofNullable(keys))

            /** Alias for calling [Builder.keys] with `keys.orElse(null)`. */
            fun keys(keys: Optional<List<String>>) = keys(keys.getOrNull())

            /**
             * Sets [Builder.keys] to an arbitrary JSON value.
             *
             * You should usually call [Builder.keys] with a well-typed `List<String>` value
             * instead. This method is primarily for setting the field to an undocumented or not yet
             * supported value.
             */
            fun keys(keys: JsonField<List<String>>) = apply {
                this.keys = keys.map { it.toMutableList() }
            }

            /**
             * Adds a single [String] to [keys].
             *
             * @throws IllegalStateException if the field was previously set to a non-list.
             */
            fun addKey(key: String) = apply {
                keys =
                    (keys ?: JsonField.of(mutableListOf())).also { checkKnown("keys", it).add(key) }
            }

            fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
                this.additionalProperties.clear()
                putAllAdditionalProperties(additionalProperties)
            }

            fun putAdditionalProperty(key: String, value: JsonValue) = apply {
                additionalProperties.put(key, value)
            }

            fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
                this.additionalProperties.putAll(additionalProperties)
            }

            fun removeAdditionalProperty(key: String) = apply { additionalProperties.remove(key) }

            fun removeAllAdditionalProperties(keys: Set<String>) = apply {
                keys.forEach(::removeAdditionalProperty)
            }

            /**
             * Returns an immutable instance of [Drag].
             *
             * Further updates to this [Builder] will not mutate the returned instance.
             *
             * The following fields are required:
             * ```java
             * .path()
             * ```
             *
             * @throws IllegalStateException if any required field is unset.
             */
            fun build(): Drag =
                Drag(
                    checkRequired("path", path).map { it.toImmutable() },
                    type,
                    (keys ?: JsonMissing.of()).map { it.toImmutable() },
                    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 OpenAIInvalidDataException if any value type in this object doesn't match its
         *   expected type.
         */
        fun validate(): Drag = apply {
            if (validated) {
                return@apply
            }

            path().forEach { it.validate() }
            _type().let {
                if (it != JsonValue.from("drag")) {
                    throw OpenAIInvalidDataException("'type' is invalid, received $it")
                }
            }
            keys()
            validated = true
        }

        fun isValid(): Boolean =
            try {
                validate()
                true
            } catch (e: OpenAIInvalidDataException) {
                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 =
            (path.asKnown().getOrNull()?.sumOf { it.validity().toInt() } ?: 0) +
                type.let { if (it == JsonValue.from("drag")) 1 else 0 } +
                (keys.asKnown().getOrNull()?.size ?: 0)

        /** An x/y coordinate pair, e.g. `{ x: 100, y: 200 }`. */
        class Path
        @JsonCreator(mode = JsonCreator.Mode.DISABLED)
        private constructor(
            private val x: JsonField<Long>,
            private val y: JsonField<Long>,
            private val additionalProperties: MutableMap<String, JsonValue>,
        ) {

            @JsonCreator
            private constructor(
                @JsonProperty("x") @ExcludeMissing x: JsonField<Long> = JsonMissing.of(),
                @JsonProperty("y") @ExcludeMissing y: JsonField<Long> = JsonMissing.of(),
            ) : this(x, y, mutableMapOf())

            /**
             * The x-coordinate.
             *
             * @throws OpenAIInvalidDataException 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 x(): Long = x.getRequired("x")

            /**
             * The y-coordinate.
             *
             * @throws OpenAIInvalidDataException 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 y(): Long = y.getRequired("y")

            /**
             * Returns the raw JSON value of [x].
             *
             * Unlike [x], this method doesn't throw if the JSON field has an unexpected type.
             */
            @JsonProperty("x") @ExcludeMissing fun _x(): JsonField<Long> = x

            /**
             * Returns the raw JSON value of [y].
             *
             * Unlike [y], this method doesn't throw if the JSON field has an unexpected type.
             */
            @JsonProperty("y") @ExcludeMissing fun _y(): JsonField<Long> = y

            @JsonAnySetter
            private fun putAdditionalProperty(key: String, value: JsonValue) {
                additionalProperties.put(key, value)
            }

            @JsonAnyGetter
            @ExcludeMissing
            fun _additionalProperties(): Map<String, JsonValue> =
                Collections.unmodifiableMap(additionalProperties)

            fun toBuilder() = Builder().from(this)

            companion object {

                /**
                 * Returns a mutable builder for constructing an instance of [Path].
                 *
                 * The following fields are required:
                 * ```java
                 * .x()
                 * .y()
                 * ```
                 */
                @JvmStatic fun builder() = Builder()
            }

            /** A builder for [Path]. */
            class Builder internal constructor() {

                private var x: JsonField<Long>? = null
                private var y: JsonField<Long>? = null
                private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()

                @JvmSynthetic
                internal fun from(path: Path) = apply {
                    x = path.x
                    y = path.y
                    additionalProperties = path.additionalProperties.toMutableMap()
                }

                /** The x-coordinate. */
                fun x(x: Long) = x(JsonField.of(x))

                /**
                 * Sets [Builder.x] to an arbitrary JSON value.
                 *
                 * You should usually call [Builder.x] with a well-typed [Long] value instead. This
                 * method is primarily for setting the field to an undocumented or not yet supported
                 * value.
                 */
                fun x(x: JsonField<Long>) = apply { this.x = x }

                /** The y-coordinate. */
                fun y(y: Long) = y(JsonField.of(y))

                /**
                 * Sets [Builder.y] to an arbitrary JSON value.
                 *
                 * You should usually call [Builder.y] with a well-typed [Long] value instead. This
                 * method is primarily for setting the field to an undocumented or not yet supported
                 * value.
                 */
                fun y(y: JsonField<Long>) = apply { this.y = y }

                fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
                    this.additionalProperties.clear()
                    putAllAdditionalProperties(additionalProperties)
                }

                fun putAdditionalProperty(key: String, value: JsonValue) = apply {
                    additionalProperties.put(key, value)
                }

                fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) =
                    apply {
                        this.additionalProperties.putAll(additionalProperties)
                    }

                fun removeAdditionalProperty(key: String) = apply {
                    additionalProperties.remove(key)
                }

                fun removeAllAdditionalProperties(keys: Set<String>) = apply {
                    keys.forEach(::removeAdditionalProperty)
                }

                /**
                 * Returns an immutable instance of [Path].
                 *
                 * Further updates to this [Builder] will not mutate the returned instance.
                 *
                 * The following fields are required:
                 * ```java
                 * .x()
                 * .y()
                 * ```
                 *
                 * @throws IllegalStateException if any required field is unset.
                 */
                fun build(): Path =
                    Path(
                        checkRequired("x", x),
                        checkRequired("y", y),
                        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 OpenAIInvalidDataException if any value type in this object doesn't match its
             *   expected type.
             */
            fun validate(): Path = apply {
                if (validated) {
                    return@apply
                }

                x()
                y()
                validated = true
            }

            fun isValid(): Boolean =
                try {
                    validate()
                    true
                } catch (e: OpenAIInvalidDataException) {
                    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 (x.asKnown().isPresent) 1 else 0) + (if (y.asKnown().isPresent) 1 else 0)

            override fun equals(other: Any?): Boolean {
                if (this === other) {
                    return true
                }

                return other is Path &&
                    x == other.x &&
                    y == other.y &&
                    additionalProperties == other.additionalProperties
            }

            private val hashCode: Int by lazy { Objects.hash(x, y, additionalProperties) }

            override fun hashCode(): Int = hashCode

            override fun toString() = "Path{x=$x, y=$y, additionalProperties=$additionalProperties}"
        }

        override fun equals(other: Any?): Boolean {
            if (this === other) {
                return true
            }

            return other is Drag &&
                path == other.path &&
                type == other.type &&
                keys == other.keys &&
                additionalProperties == other.additionalProperties
        }

        private val hashCode: Int by lazy { Objects.hash(path, type, keys, additionalProperties) }

        override fun hashCode(): Int = hashCode

        override fun toString() =
            "Drag{path=$path, type=$type, keys=$keys, additionalProperties=$additionalProperties}"
    }

    /** A collection of keypresses the model would like to perform. */
    class Keypress
    @JsonCreator(mode = JsonCreator.Mode.DISABLED)
    private constructor(
        private val keys: JsonField<List<String>>,
        private val type: JsonValue,
        private val additionalProperties: MutableMap<String, JsonValue>,
    ) {

        @JsonCreator
        private constructor(
            @JsonProperty("keys") @ExcludeMissing keys: JsonField<List<String>> = JsonMissing.of(),
            @JsonProperty("type") @ExcludeMissing type: JsonValue = JsonMissing.of(),
        ) : this(keys, type, mutableMapOf())

        /**
         * The combination of keys the model is requesting to be pressed. This is an array of
         * strings, each representing a key.
         *
         * @throws OpenAIInvalidDataException 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 keys(): List<String> = keys.getRequired("keys")

        /**
         * Specifies the event type. For a keypress action, this property is always set to
         * `keypress`.
         *
         * Expected to always return the following:
         * ```java
         * JsonValue.from("keypress")
         * ```
         *
         * However, this method can be useful for debugging and logging (e.g. if the server
         * responded with an unexpected value).
         */
        @JsonProperty("type") @ExcludeMissing fun _type(): JsonValue = type

        /**
         * Returns the raw JSON value of [keys].
         *
         * Unlike [keys], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("keys") @ExcludeMissing fun _keys(): JsonField<List<String>> = keys

        @JsonAnySetter
        private fun putAdditionalProperty(key: String, value: JsonValue) {
            additionalProperties.put(key, value)
        }

        @JsonAnyGetter
        @ExcludeMissing
        fun _additionalProperties(): Map<String, JsonValue> =
            Collections.unmodifiableMap(additionalProperties)

        fun toBuilder() = Builder().from(this)

        companion object {

            /**
             * Returns a mutable builder for constructing an instance of [Keypress].
             *
             * The following fields are required:
             * ```java
             * .keys()
             * ```
             */
            @JvmStatic fun builder() = Builder()
        }

        /** A builder for [Keypress]. */
        class Builder internal constructor() {

            private var keys: JsonField<MutableList<String>>? = null
            private var type: JsonValue = JsonValue.from("keypress")
            private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()

            @JvmSynthetic
            internal fun from(keypress: Keypress) = apply {
                keys = keypress.keys.map { it.toMutableList() }
                type = keypress.type
                additionalProperties = keypress.additionalProperties.toMutableMap()
            }

            /**
             * The combination of keys the model is requesting to be pressed. This is an array of
             * strings, each representing a key.
             */
            fun keys(keys: List<String>) = keys(JsonField.of(keys))

            /**
             * Sets [Builder.keys] to an arbitrary JSON value.
             *
             * You should usually call [Builder.keys] with a well-typed `List<String>` value
             * instead. This method is primarily for setting the field to an undocumented or not yet
             * supported value.
             */
            fun keys(keys: JsonField<List<String>>) = apply {
                this.keys = keys.map { it.toMutableList() }
            }

            /**
             * Adds a single [String] to [keys].
             *
             * @throws IllegalStateException if the field was previously set to a non-list.
             */
            fun addKey(key: String) = apply {
                keys =
                    (keys ?: JsonField.of(mutableListOf())).also { checkKnown("keys", it).add(key) }
            }

            /**
             * Sets the field to an arbitrary JSON value.
             *
             * It is usually unnecessary to call this method because the field defaults to the
             * following:
             * ```java
             * JsonValue.from("keypress")
             * ```
             *
             * This method is primarily for setting the field to an undocumented or not yet
             * supported value.
             */
            fun type(type: JsonValue) = apply { this.type = type }

            fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
                this.additionalProperties.clear()
                putAllAdditionalProperties(additionalProperties)
            }

            fun putAdditionalProperty(key: String, value: JsonValue) = apply {
                additionalProperties.put(key, value)
            }

            fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
                this.additionalProperties.putAll(additionalProperties)
            }

            fun removeAdditionalProperty(key: String) = apply { additionalProperties.remove(key) }

            fun removeAllAdditionalProperties(keys: Set<String>) = apply {
                keys.forEach(::removeAdditionalProperty)
            }

            /**
             * Returns an immutable instance of [Keypress].
             *
             * Further updates to this [Builder] will not mutate the returned instance.
             *
             * The following fields are required:
             * ```java
             * .keys()
             * ```
             *
             * @throws IllegalStateException if any required field is unset.
             */
            fun build(): Keypress =
                Keypress(
                    checkRequired("keys", keys).map { it.toImmutable() },
                    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 OpenAIInvalidDataException if any value type in this object doesn't match its
         *   expected type.
         */
        fun validate(): Keypress = apply {
            if (validated) {
                return@apply
            }

            keys()
            _type().let {
                if (it != JsonValue.from("keypress")) {
                    throw OpenAIInvalidDataException("'type' is invalid, received $it")
                }
            }
            validated = true
        }

        fun isValid(): Boolean =
            try {
                validate()
                true
            } catch (e: OpenAIInvalidDataException) {
                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 =
            (keys.asKnown().getOrNull()?.size ?: 0) +
                type.let { if (it == JsonValue.from("keypress")) 1 else 0 }

        override fun equals(other: Any?): Boolean {
            if (this === other) {
                return true
            }

            return other is Keypress &&
                keys == other.keys &&
                type == other.type &&
                additionalProperties == other.additionalProperties
        }

        private val hashCode: Int by lazy { Objects.hash(keys, type, additionalProperties) }

        override fun hashCode(): Int = hashCode

        override fun toString() =
            "Keypress{keys=$keys, type=$type, additionalProperties=$additionalProperties}"
    }

    /** A mouse move action. */
    class Move
    @JsonCreator(mode = JsonCreator.Mode.DISABLED)
    private constructor(
        private val type: JsonValue,
        private val x: JsonField<Long>,
        private val y: JsonField<Long>,
        private val keys: JsonField<List<String>>,
        private val additionalProperties: MutableMap<String, JsonValue>,
    ) {

        @JsonCreator
        private constructor(
            @JsonProperty("type") @ExcludeMissing type: JsonValue = JsonMissing.of(),
            @JsonProperty("x") @ExcludeMissing x: JsonField<Long> = JsonMissing.of(),
            @JsonProperty("y") @ExcludeMissing y: JsonField<Long> = JsonMissing.of(),
            @JsonProperty("keys") @ExcludeMissing keys: JsonField<List<String>> = JsonMissing.of(),
        ) : this(type, x, y, keys, mutableMapOf())

        /**
         * Specifies the event type. For a move action, this property is always set to `move`.
         *
         * Expected to always return the following:
         * ```java
         * JsonValue.from("move")
         * ```
         *
         * However, this method can be useful for debugging and logging (e.g. if the server
         * responded with an unexpected value).
         */
        @JsonProperty("type") @ExcludeMissing fun _type(): JsonValue = type

        /**
         * The x-coordinate to move to.
         *
         * @throws OpenAIInvalidDataException 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 x(): Long = x.getRequired("x")

        /**
         * The y-coordinate to move to.
         *
         * @throws OpenAIInvalidDataException 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 y(): Long = y.getRequired("y")

        /**
         * The keys being held while moving the mouse.
         *
         * @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
         *   server responded with an unexpected value).
         */
        fun keys(): Optional<List<String>> = keys.getOptional("keys")

        /**
         * Returns the raw JSON value of [x].
         *
         * Unlike [x], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("x") @ExcludeMissing fun _x(): JsonField<Long> = x

        /**
         * Returns the raw JSON value of [y].
         *
         * Unlike [y], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("y") @ExcludeMissing fun _y(): JsonField<Long> = y

        /**
         * Returns the raw JSON value of [keys].
         *
         * Unlike [keys], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("keys") @ExcludeMissing fun _keys(): JsonField<List<String>> = keys

        @JsonAnySetter
        private fun putAdditionalProperty(key: String, value: JsonValue) {
            additionalProperties.put(key, value)
        }

        @JsonAnyGetter
        @ExcludeMissing
        fun _additionalProperties(): Map<String, JsonValue> =
            Collections.unmodifiableMap(additionalProperties)

        fun toBuilder() = Builder().from(this)

        companion object {

            /**
             * Returns a mutable builder for constructing an instance of [Move].
             *
             * The following fields are required:
             * ```java
             * .x()
             * .y()
             * ```
             */
            @JvmStatic fun builder() = Builder()
        }

        /** A builder for [Move]. */
        class Builder internal constructor() {

            private var type: JsonValue = JsonValue.from("move")
            private var x: JsonField<Long>? = null
            private var y: JsonField<Long>? = null
            private var keys: JsonField<MutableList<String>>? = null
            private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()

            @JvmSynthetic
            internal fun from(move: Move) = apply {
                type = move.type
                x = move.x
                y = move.y
                keys = move.keys.map { it.toMutableList() }
                additionalProperties = move.additionalProperties.toMutableMap()
            }

            /**
             * Sets the field to an arbitrary JSON value.
             *
             * It is usually unnecessary to call this method because the field defaults to the
             * following:
             * ```java
             * JsonValue.from("move")
             * ```
             *
             * This method is primarily for setting the field to an undocumented or not yet
             * supported value.
             */
            fun type(type: JsonValue) = apply { this.type = type }

            /** The x-coordinate to move to. */
            fun x(x: Long) = x(JsonField.of(x))

            /**
             * Sets [Builder.x] to an arbitrary JSON value.
             *
             * You should usually call [Builder.x] with a well-typed [Long] value instead. This
             * method is primarily for setting the field to an undocumented or not yet supported
             * value.
             */
            fun x(x: JsonField<Long>) = apply { this.x = x }

            /** The y-coordinate to move to. */
            fun y(y: Long) = y(JsonField.of(y))

            /**
             * Sets [Builder.y] to an arbitrary JSON value.
             *
             * You should usually call [Builder.y] with a well-typed [Long] value instead. This
             * method is primarily for setting the field to an undocumented or not yet supported
             * value.
             */
            fun y(y: JsonField<Long>) = apply { this.y = y }

            /** The keys being held while moving the mouse. */
            fun keys(keys: List<String>?) = keys(JsonField.ofNullable(keys))

            /** Alias for calling [Builder.keys] with `keys.orElse(null)`. */
            fun keys(keys: Optional<List<String>>) = keys(keys.getOrNull())

            /**
             * Sets [Builder.keys] to an arbitrary JSON value.
             *
             * You should usually call [Builder.keys] with a well-typed `List<String>` value
             * instead. This method is primarily for setting the field to an undocumented or not yet
             * supported value.
             */
            fun keys(keys: JsonField<List<String>>) = apply {
                this.keys = keys.map { it.toMutableList() }
            }

            /**
             * Adds a single [String] to [keys].
             *
             * @throws IllegalStateException if the field was previously set to a non-list.
             */
            fun addKey(key: String) = apply {
                keys =
                    (keys ?: JsonField.of(mutableListOf())).also { checkKnown("keys", it).add(key) }
            }

            fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
                this.additionalProperties.clear()
                putAllAdditionalProperties(additionalProperties)
            }

            fun putAdditionalProperty(key: String, value: JsonValue) = apply {
                additionalProperties.put(key, value)
            }

            fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
                this.additionalProperties.putAll(additionalProperties)
            }

            fun removeAdditionalProperty(key: String) = apply { additionalProperties.remove(key) }

            fun removeAllAdditionalProperties(keys: Set<String>) = apply {
                keys.forEach(::removeAdditionalProperty)
            }

            /**
             * Returns an immutable instance of [Move].
             *
             * Further updates to this [Builder] will not mutate the returned instance.
             *
             * The following fields are required:
             * ```java
             * .x()
             * .y()
             * ```
             *
             * @throws IllegalStateException if any required field is unset.
             */
            fun build(): Move =
                Move(
                    type,
                    checkRequired("x", x),
                    checkRequired("y", y),
                    (keys ?: JsonMissing.of()).map { it.toImmutable() },
                    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 OpenAIInvalidDataException if any value type in this object doesn't match its
         *   expected type.
         */
        fun validate(): Move = apply {
            if (validated) {
                return@apply
            }

            _type().let {
                if (it != JsonValue.from("move")) {
                    throw OpenAIInvalidDataException("'type' is invalid, received $it")
                }
            }
            x()
            y()
            keys()
            validated = true
        }

        fun isValid(): Boolean =
            try {
                validate()
                true
            } catch (e: OpenAIInvalidDataException) {
                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 =
            type.let { if (it == JsonValue.from("move")) 1 else 0 } +
                (if (x.asKnown().isPresent) 1 else 0) +
                (if (y.asKnown().isPresent) 1 else 0) +
                (keys.asKnown().getOrNull()?.size ?: 0)

        override fun equals(other: Any?): Boolean {
            if (this === other) {
                return true
            }

            return other is Move &&
                type == other.type &&
                x == other.x &&
                y == other.y &&
                keys == other.keys &&
                additionalProperties == other.additionalProperties
        }

        private val hashCode: Int by lazy { Objects.hash(type, x, y, keys, additionalProperties) }

        override fun hashCode(): Int = hashCode

        override fun toString() =
            "Move{type=$type, x=$x, y=$y, keys=$keys, additionalProperties=$additionalProperties}"
    }

    /** A scroll action. */
    class Scroll
    @JsonCreator(mode = JsonCreator.Mode.DISABLED)
    private constructor(
        private val scrollX: JsonField<Long>,
        private val scrollY: JsonField<Long>,
        private val type: JsonValue,
        private val x: JsonField<Long>,
        private val y: JsonField<Long>,
        private val keys: JsonField<List<String>>,
        private val additionalProperties: MutableMap<String, JsonValue>,
    ) {

        @JsonCreator
        private constructor(
            @JsonProperty("scroll_x") @ExcludeMissing scrollX: JsonField<Long> = JsonMissing.of(),
            @JsonProperty("scroll_y") @ExcludeMissing scrollY: JsonField<Long> = JsonMissing.of(),
            @JsonProperty("type") @ExcludeMissing type: JsonValue = JsonMissing.of(),
            @JsonProperty("x") @ExcludeMissing x: JsonField<Long> = JsonMissing.of(),
            @JsonProperty("y") @ExcludeMissing y: JsonField<Long> = JsonMissing.of(),
            @JsonProperty("keys") @ExcludeMissing keys: JsonField<List<String>> = JsonMissing.of(),
        ) : this(scrollX, scrollY, type, x, y, keys, mutableMapOf())

        /**
         * The horizontal scroll distance.
         *
         * @throws OpenAIInvalidDataException 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 scrollX(): Long = scrollX.getRequired("scroll_x")

        /**
         * The vertical scroll distance.
         *
         * @throws OpenAIInvalidDataException 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 scrollY(): Long = scrollY.getRequired("scroll_y")

        /**
         * Specifies the event type. For a scroll action, this property is always set to `scroll`.
         *
         * Expected to always return the following:
         * ```java
         * JsonValue.from("scroll")
         * ```
         *
         * However, this method can be useful for debugging and logging (e.g. if the server
         * responded with an unexpected value).
         */
        @JsonProperty("type") @ExcludeMissing fun _type(): JsonValue = type

        /**
         * The x-coordinate where the scroll occurred.
         *
         * @throws OpenAIInvalidDataException 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 x(): Long = x.getRequired("x")

        /**
         * The y-coordinate where the scroll occurred.
         *
         * @throws OpenAIInvalidDataException 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 y(): Long = y.getRequired("y")

        /**
         * The keys being held while scrolling.
         *
         * @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
         *   server responded with an unexpected value).
         */
        fun keys(): Optional<List<String>> = keys.getOptional("keys")

        /**
         * Returns the raw JSON value of [scrollX].
         *
         * Unlike [scrollX], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("scroll_x") @ExcludeMissing fun _scrollX(): JsonField<Long> = scrollX

        /**
         * Returns the raw JSON value of [scrollY].
         *
         * Unlike [scrollY], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("scroll_y") @ExcludeMissing fun _scrollY(): JsonField<Long> = scrollY

        /**
         * Returns the raw JSON value of [x].
         *
         * Unlike [x], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("x") @ExcludeMissing fun _x(): JsonField<Long> = x

        /**
         * Returns the raw JSON value of [y].
         *
         * Unlike [y], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("y") @ExcludeMissing fun _y(): JsonField<Long> = y

        /**
         * Returns the raw JSON value of [keys].
         *
         * Unlike [keys], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("keys") @ExcludeMissing fun _keys(): JsonField<List<String>> = keys

        @JsonAnySetter
        private fun putAdditionalProperty(key: String, value: JsonValue) {
            additionalProperties.put(key, value)
        }

        @JsonAnyGetter
        @ExcludeMissing
        fun _additionalProperties(): Map<String, JsonValue> =
            Collections.unmodifiableMap(additionalProperties)

        fun toBuilder() = Builder().from(this)

        companion object {

            /**
             * Returns a mutable builder for constructing an instance of [Scroll].
             *
             * The following fields are required:
             * ```java
             * .scrollX()
             * .scrollY()
             * .x()
             * .y()
             * ```
             */
            @JvmStatic fun builder() = Builder()
        }

        /** A builder for [Scroll]. */
        class Builder internal constructor() {

            private var scrollX: JsonField<Long>? = null
            private var scrollY: JsonField<Long>? = null
            private var type: JsonValue = JsonValue.from("scroll")
            private var x: JsonField<Long>? = null
            private var y: JsonField<Long>? = null
            private var keys: JsonField<MutableList<String>>? = null
            private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()

            @JvmSynthetic
            internal fun from(scroll: Scroll) = apply {
                scrollX = scroll.scrollX
                scrollY = scroll.scrollY
                type = scroll.type
                x = scroll.x
                y = scroll.y
                keys = scroll.keys.map { it.toMutableList() }
                additionalProperties = scroll.additionalProperties.toMutableMap()
            }

            /** The horizontal scroll distance. */
            fun scrollX(scrollX: Long) = scrollX(JsonField.of(scrollX))

            /**
             * Sets [Builder.scrollX] to an arbitrary JSON value.
             *
             * You should usually call [Builder.scrollX] with a well-typed [Long] value instead.
             * This method is primarily for setting the field to an undocumented or not yet
             * supported value.
             */
            fun scrollX(scrollX: JsonField<Long>) = apply { this.scrollX = scrollX }

            /** The vertical scroll distance. */
            fun scrollY(scrollY: Long) = scrollY(JsonField.of(scrollY))

            /**
             * Sets [Builder.scrollY] to an arbitrary JSON value.
             *
             * You should usually call [Builder.scrollY] with a well-typed [Long] value instead.
             * This method is primarily for setting the field to an undocumented or not yet
             * supported value.
             */
            fun scrollY(scrollY: JsonField<Long>) = apply { this.scrollY = scrollY }

            /**
             * Sets the field to an arbitrary JSON value.
             *
             * It is usually unnecessary to call this method because the field defaults to the
             * following:
             * ```java
             * JsonValue.from("scroll")
             * ```
             *
             * This method is primarily for setting the field to an undocumented or not yet
             * supported value.
             */
            fun type(type: JsonValue) = apply { this.type = type }

            /** The x-coordinate where the scroll occurred. */
            fun x(x: Long) = x(JsonField.of(x))

            /**
             * Sets [Builder.x] to an arbitrary JSON value.
             *
             * You should usually call [Builder.x] with a well-typed [Long] value instead. This
             * method is primarily for setting the field to an undocumented or not yet supported
             * value.
             */
            fun x(x: JsonField<Long>) = apply { this.x = x }

            /** The y-coordinate where the scroll occurred. */
            fun y(y: Long) = y(JsonField.of(y))

            /**
             * Sets [Builder.y] to an arbitrary JSON value.
             *
             * You should usually call [Builder.y] with a well-typed [Long] value instead. This
             * method is primarily for setting the field to an undocumented or not yet supported
             * value.
             */
            fun y(y: JsonField<Long>) = apply { this.y = y }

            /** The keys being held while scrolling. */
            fun keys(keys: List<String>?) = keys(JsonField.ofNullable(keys))

            /** Alias for calling [Builder.keys] with `keys.orElse(null)`. */
            fun keys(keys: Optional<List<String>>) = keys(keys.getOrNull())

            /**
             * Sets [Builder.keys] to an arbitrary JSON value.
             *
             * You should usually call [Builder.keys] with a well-typed `List<String>` value
             * instead. This method is primarily for setting the field to an undocumented or not yet
             * supported value.
             */
            fun keys(keys: JsonField<List<String>>) = apply {
                this.keys = keys.map { it.toMutableList() }
            }

            /**
             * Adds a single [String] to [keys].
             *
             * @throws IllegalStateException if the field was previously set to a non-list.
             */
            fun addKey(key: String) = apply {
                keys =
                    (keys ?: JsonField.of(mutableListOf())).also { checkKnown("keys", it).add(key) }
            }

            fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
                this.additionalProperties.clear()
                putAllAdditionalProperties(additionalProperties)
            }

            fun putAdditionalProperty(key: String, value: JsonValue) = apply {
                additionalProperties.put(key, value)
            }

            fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
                this.additionalProperties.putAll(additionalProperties)
            }

            fun removeAdditionalProperty(key: String) = apply { additionalProperties.remove(key) }

            fun removeAllAdditionalProperties(keys: Set<String>) = apply {
                keys.forEach(::removeAdditionalProperty)
            }

            /**
             * Returns an immutable instance of [Scroll].
             *
             * Further updates to this [Builder] will not mutate the returned instance.
             *
             * The following fields are required:
             * ```java
             * .scrollX()
             * .scrollY()
             * .x()
             * .y()
             * ```
             *
             * @throws IllegalStateException if any required field is unset.
             */
            fun build(): Scroll =
                Scroll(
                    checkRequired("scrollX", scrollX),
                    checkRequired("scrollY", scrollY),
                    type,
                    checkRequired("x", x),
                    checkRequired("y", y),
                    (keys ?: JsonMissing.of()).map { it.toImmutable() },
                    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 OpenAIInvalidDataException if any value type in this object doesn't match its
         *   expected type.
         */
        fun validate(): Scroll = apply {
            if (validated) {
                return@apply
            }

            scrollX()
            scrollY()
            _type().let {
                if (it != JsonValue.from("scroll")) {
                    throw OpenAIInvalidDataException("'type' is invalid, received $it")
                }
            }
            x()
            y()
            keys()
            validated = true
        }

        fun isValid(): Boolean =
            try {
                validate()
                true
            } catch (e: OpenAIInvalidDataException) {
                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 (scrollX.asKnown().isPresent) 1 else 0) +
                (if (scrollY.asKnown().isPresent) 1 else 0) +
                type.let { if (it == JsonValue.from("scroll")) 1 else 0 } +
                (if (x.asKnown().isPresent) 1 else 0) +
                (if (y.asKnown().isPresent) 1 else 0) +
                (keys.asKnown().getOrNull()?.size ?: 0)

        override fun equals(other: Any?): Boolean {
            if (this === other) {
                return true
            }

            return other is Scroll &&
                scrollX == other.scrollX &&
                scrollY == other.scrollY &&
                type == other.type &&
                x == other.x &&
                y == other.y &&
                keys == other.keys &&
                additionalProperties == other.additionalProperties
        }

        private val hashCode: Int by lazy {
            Objects.hash(scrollX, scrollY, type, x, y, keys, additionalProperties)
        }

        override fun hashCode(): Int = hashCode

        override fun toString() =
            "Scroll{scrollX=$scrollX, scrollY=$scrollY, type=$type, x=$x, y=$y, keys=$keys, additionalProperties=$additionalProperties}"
    }

    /** An action to type in text. */
    class Type
    @JsonCreator(mode = JsonCreator.Mode.DISABLED)
    private constructor(
        private val text: JsonField<String>,
        private val type: JsonValue,
        private val additionalProperties: MutableMap<String, JsonValue>,
    ) {

        @JsonCreator
        private constructor(
            @JsonProperty("text") @ExcludeMissing text: JsonField<String> = JsonMissing.of(),
            @JsonProperty("type") @ExcludeMissing type: JsonValue = JsonMissing.of(),
        ) : this(text, type, mutableMapOf())

        /**
         * The text to type.
         *
         * @throws OpenAIInvalidDataException 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 text(): String = text.getRequired("text")

        /**
         * Specifies the event type. For a type action, this property is always set to `type`.
         *
         * Expected to always return the following:
         * ```java
         * JsonValue.from("type")
         * ```
         *
         * However, this method can be useful for debugging and logging (e.g. if the server
         * responded with an unexpected value).
         */
        @JsonProperty("type") @ExcludeMissing fun _type(): JsonValue = type

        /**
         * Returns the raw JSON value of [text].
         *
         * Unlike [text], this method doesn't throw if the JSON field has an unexpected type.
         */
        @JsonProperty("text") @ExcludeMissing fun _text(): JsonField<String> = text

        @JsonAnySetter
        private fun putAdditionalProperty(key: String, value: JsonValue) {
            additionalProperties.put(key, value)
        }

        @JsonAnyGetter
        @ExcludeMissing
        fun _additionalProperties(): Map<String, JsonValue> =
            Collections.unmodifiableMap(additionalProperties)

        fun toBuilder() = Builder().from(this)

        companion object {

            /**
             * Returns a mutable builder for constructing an instance of [Type].
             *
             * The following fields are required:
             * ```java
             * .text()
             * ```
             */
            @JvmStatic fun builder() = Builder()
        }

        /** A builder for [Type]. */
        class Builder internal constructor() {

            private var text: JsonField<String>? = null
            private var type: JsonValue = JsonValue.from("type")
            private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()

            @JvmSynthetic
            internal fun from(type: Type) = apply {
                text = type.text
                this.type = type.type
                additionalProperties = type.additionalProperties.toMutableMap()
            }

            /** The text to type. */
            fun text(text: String) = text(JsonField.of(text))

            /**
             * Sets [Builder.text] to an arbitrary JSON value.
             *
             * You should usually call [Builder.text] with a well-typed [String] value instead. This
             * method is primarily for setting the field to an undocumented or not yet supported
             * value.
             */
            fun text(text: JsonField<String>) = apply { this.text = text }

            /**
             * Sets the field to an arbitrary JSON value.
             *
             * It is usually unnecessary to call this method because the field defaults to the
             * following:
             * ```java
             * JsonValue.from("type")
             * ```
             *
             * This method is primarily for setting the field to an undocumented or not yet
             * supported value.
             */
            fun type(type: JsonValue) = apply { this.type = type }

            fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
                this.additionalProperties.clear()
                putAllAdditionalProperties(additionalProperties)
            }

            fun putAdditionalProperty(key: String, value: JsonValue) = apply {
                additionalProperties.put(key, value)
            }

            fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
                this.additionalProperties.putAll(additionalProperties)
            }

            fun removeAdditionalProperty(key: String) = apply { additionalProperties.remove(key) }

            fun removeAllAdditionalProperties(keys: Set<String>) = apply {
                keys.forEach(::removeAdditionalProperty)
            }

            /**
             * Returns an immutable instance of [Type].
             *
             * Further updates to this [Builder] will not mutate the returned instance.
             *
             * The following fields are required:
             * ```java
             * .text()
             * ```
             *
             * @throws IllegalStateException if any required field is unset.
             */
            fun build(): Type =
                Type(checkRequired("text", text), 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 OpenAIInvalidDataException if any value type in this object doesn't match its
         *   expected type.
         */
        fun validate(): Type = apply {
            if (validated) {
                return@apply
            }

            text()
            _type().let {
                if (it != JsonValue.from("type")) {
                    throw OpenAIInvalidDataException("'type' is invalid, received $it")
                }
            }
            validated = true
        }

        fun isValid(): Boolean =
            try {
                validate()
                true
            } catch (e: OpenAIInvalidDataException) {
                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 (text.asKnown().isPresent) 1 else 0) +
                type.let { if (it == JsonValue.from("type")) 1 else 0 }

        override fun equals(other: Any?): Boolean {
            if (this === other) {
                return true
            }

            return other is Type &&
                text == other.text &&
                type == other.type &&
                additionalProperties == other.additionalProperties
        }

        private val hashCode: Int by lazy { Objects.hash(text, type, additionalProperties) }

        override fun hashCode(): Int = hashCode

        override fun toString() =
            "Type{text=$text, type=$type, additionalProperties=$additionalProperties}"
    }
}
