001package com.box.sdk;
002
003import com.eclipsesource.json.Json;
004import com.eclipsesource.json.JsonObject;
005import com.eclipsesource.json.JsonValue;
006import java.text.ParseException;
007import java.util.Date;
008
009/** AI response to a user request. */
010public class BoxAIExtractStructuredResponse extends BoxJSONObject {
011  private final JsonObject sourceJson;
012  private JsonObject answer;
013  private String completionReason;
014  private Date createdAt;
015
016  /** Constructs a BoxAIResponse object. */
017  public BoxAIExtractStructuredResponse(String json) {
018    super(json);
019    this.sourceJson = Json.parse(json).asObject();
020  }
021
022  /**
023   * Constructs an BoxAIResponse object using an already parsed JSON object.
024   *
025   * @param jsonObject the parsed JSON object.
026   */
027  BoxAIExtractStructuredResponse(JsonObject jsonObject) {
028    super(jsonObject);
029    this.sourceJson = jsonObject;
030  }
031
032  /**
033   * Gets the source JSON of the AI response.
034   *
035   * @return the source JSON of the AI response.
036   */
037  public JsonObject getSourceJson() {
038    return sourceJson;
039  }
040
041  /**
042   * Gets the answer of the AI.
043   *
044   * @return the answer of the AI.
045   */
046  public JsonObject getAnswer() {
047    return answer;
048  }
049
050  /**
051   * Gets reason the response finishes.
052   *
053   * @return the reason the response finishes.
054   */
055  public String getCompletionReason() {
056    return completionReason;
057  }
058
059  /**
060   * Gets the ISO date formatted timestamp of when the answer to the prompt was created.
061   *
062   * @return The ISO date formatted timestamp of when the answer to the prompt was created.
063   */
064  public Date getCreatedAt() {
065    return createdAt;
066  }
067
068  /** {@inheritDoc} */
069  @Override
070  void parseJSONMember(JsonObject.Member member) {
071    JsonValue value = member.getValue();
072    String memberName = member.getName();
073    try {
074      if (memberName.equals("answer")) {
075        this.answer = value.asObject();
076      } else if (memberName.equals("completion_reason")) {
077        this.completionReason = value.asString();
078      } else if (memberName.equals("created_at")) {
079        this.createdAt = BoxDateFormat.parse(value.asString());
080      }
081    } catch (ParseException e) {
082      assert false : "A ParseException indicates a bug in the SDK.";
083    }
084  }
085}