// Code generated by sidekick. DO NOT EDIT.
//
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import GoogleCloudWkt

/// Output for the session.
public struct SessionOutput: Codable, Equatable, GoogleCloudWkt._AnyPackable,
  Sendable
{
  /// Indicates the sequential order of conversation turn to which this output
  /// belongs to, starting from 1.
  public var turnIndex: Swift.Int32 = Swift.Int32()

  /// If true, the CES agent has detected the end of the current conversation
  /// turn and will provide no further output for this turn.
  public var turnCompleted: Swift.Bool = Swift.Bool()

  /// Optional. Diagnostic information contains execution details during the
  /// processing of the input. Only populated in the last SessionOutput (with
  /// `turn_completed=true`) for each turn.
  public var diagnosticInfo: SessionOutput.DiagnosticInfo? = nil

  /// Context messages for external supervision guardrails.
  public var context: [GoogleCloudWkt.`Any`] = []

  /// The type of the output.
  public var outputType: OneOf_OutputType? = nil

  /// Initialize a new instance of `SessionOutput`.
  public init() {}

  /// Use `config` to return a new instance of this object, with some fields updated.
  ///
  /// Commonly used to initialize the value, for example:
  ///
  /// ```
  /// let value = SessionOutput().with { $0.text = ... }
  /// ```
  public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
    var copy = self
    try config(&copy)
    return copy
  }

  private enum CodingKeys: Swift.String, CodingKey {
    case text = "text"
    case audio = "audio"
    case toolCalls = "toolCalls"
    case citations = "citations"
    case googleSearchSuggestions = "googleSearchSuggestions"
    case endSession = "endSession"
    case payload = "payload"
    case turnIndex = "turnIndex"
    case turnCompleted = "turnCompleted"
    case diagnosticInfo = "diagnosticInfo"
    case context = "context"
  }

  public init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    self.turnIndex = try container.decode(Swift.Int32.self, forKey: .turnIndex)
    self.turnCompleted = try container.decode(Swift.Bool.self, forKey: .turnCompleted)
    self.diagnosticInfo = try container.decodeIfPresent(
      SessionOutput.DiagnosticInfo.self, forKey: .diagnosticInfo)
    self.context = try container.decode([GoogleCloudWkt.`Any`].self, forKey: .context)

    var outputType: OneOf_OutputType? = nil
    let outputTypeCheckAndSet = {
      if outputType != nil {
        throw DecodingError.dataCorrupted(
          DecodingError.Context(
            codingPath: decoder.codingPath,
            debugDescription: "Multiple values set for oneof 'outputType'"))
      }
      outputType = $0
    }
    if let text = try container.decodeIfPresent(Swift.String.self, forKey: .text) {
      try outputTypeCheckAndSet(.text(text))
    }
    if let audio = try container.decodeIfPresent(Foundation.Data.self, forKey: .audio) {
      try outputTypeCheckAndSet(.audio(audio))
    }
    if let toolCalls = try container.decodeIfPresent(ToolCalls?.self, forKey: .toolCalls) {
      try outputTypeCheckAndSet(.toolCalls(toolCalls))
    }
    if let citations = try container.decodeIfPresent(Citations?.self, forKey: .citations) {
      try outputTypeCheckAndSet(.citations(citations))
    }
    if let googleSearchSuggestions = try container.decodeIfPresent(
      GoogleSearchSuggestions?.self, forKey: .googleSearchSuggestions)
    {
      try outputTypeCheckAndSet(.googleSearchSuggestions(googleSearchSuggestions))
    }
    if let endSession = try container.decodeIfPresent(EndSession?.self, forKey: .endSession) {
      try outputTypeCheckAndSet(.endSession(endSession))
    }
    if let payload = try container.decodeIfPresent(GoogleCloudWkt.Struct?.self, forKey: .payload) {
      try outputTypeCheckAndSet(.payload(payload))
    }
    self.outputType = outputType
  }

  public func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(self.turnIndex, forKey: .turnIndex)
    try container.encode(self.turnCompleted, forKey: .turnCompleted)
    try container.encode(self.diagnosticInfo, forKey: .diagnosticInfo)
    try container.encode(self.context, forKey: .context)

    if let choice = self.outputType {
      switch choice {
      case .text(let value):
        try container.encode(value, forKey: .text)
      case .audio(let value):
        try container.encode(value, forKey: .audio)
      case .toolCalls(let value):
        try container.encode(value, forKey: .toolCalls)
      case .citations(let value):
        try container.encode(value, forKey: .citations)
      case .googleSearchSuggestions(let value):
        try container.encode(value, forKey: .googleSearchSuggestions)
      case .endSession(let value):
        try container.encode(value, forKey: .endSession)
      case .payload(let value):
        try container.encode(value, forKey: .payload)
      }
    }
  }

  /// Contains execution details during the processing.
  public struct DiagnosticInfo: Codable, Equatable, GoogleCloudWkt._AnyPackable,
    Sendable
  {
    /// List of the messages that happened during the processing.
    public var messages: [Message] = []

    /// A trace of the entire request processing, represented as a root span.
    /// This span can contain nested child spans for specific operations.
    public var rootSpan: Span? = nil

    /// Initialize a new instance of `DiagnosticInfo`.
    public init() {}

    /// Use `config` to return a new instance of this object, with some fields updated.
    ///
    /// Commonly used to initialize the value, for example:
    ///
    /// ```
    /// let value = DiagnosticInfo().with { $0.messages = ... }
    /// ```
    public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
      var copy = self
      try config(&copy)
      return copy
    }

    public static var _anyTypeUrl: Swift.String {
      return "type.googleapis.com/google.cloud.ces.v1.SessionOutput.DiagnosticInfo"
    }
    public init(fromAny any: GoogleCloudWkt.`Any`) throws {
      self = try GoogleCloudWkt._slowAnyDeserialize(Self.self, from: any)
    }
    public func _pack() throws -> GoogleCloudWkt.Struct {
      return try GoogleCloudWkt._slowAnySerialize(message: self)
    }
  }

  /// The type of the output.
  public enum OneOf_OutputType: Codable, Equatable, Sendable {
    /// Output text from the CES agent.
    case text(Swift.String)
    /// Output audio from the CES agent.
    case audio(Foundation.Data)
    /// Request for the client to execute the tools.
    indirect case toolCalls(ToolCalls?)
    /// Citations that provide the source information for the agent's generated
    /// text.
    indirect case citations(Citations?)
    /// The suggestions returned from Google Search as a result of invoking the
    /// [GoogleSearchTool][google.cloud.ces.v1.GoogleSearchTool].
    ///
    /// [google.cloud.ces.v1.GoogleSearchTool]: <doc:GoogleSearchTool>
    indirect case googleSearchSuggestions(GoogleSearchSuggestions?)
    /// Indicates the session has ended.
    indirect case endSession(EndSession?)
    /// Custom payload with structured output from the CES agent.
    indirect case payload(GoogleCloudWkt.Struct?)
  }

  public static var _anyTypeUrl: Swift.String {
    return "type.googleapis.com/google.cloud.ces.v1.SessionOutput"
  }
  public init(fromAny any: GoogleCloudWkt.`Any`) throws {
    self = try GoogleCloudWkt._slowAnyDeserialize(Self.self, from: any)
  }
  public func _pack() throws -> GoogleCloudWkt.Struct {
    return try GoogleCloudWkt._slowAnySerialize(message: self)
  }
}
