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