/*
 * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
 */

import * as z from "zod/v3";
import { remap as remap$ } from "../../lib/primitives.js";
import { safeParse } from "../../lib/schemas.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  AgentHandoffEntry,
  AgentHandoffEntry$inboundSchema,
} from "./agenthandoffentry.js";
import {
  ConversationUsageInfo,
  ConversationUsageInfo$inboundSchema,
} from "./conversationusageinfo.js";
import {
  FunctionCallEntry,
  FunctionCallEntry$inboundSchema,
} from "./functioncallentry.js";
import {
  MessageOutputEntry,
  MessageOutputEntry$inboundSchema,
} from "./messageoutputentry.js";
import {
  ToolExecutionEntry,
  ToolExecutionEntry$inboundSchema,
} from "./toolexecutionentry.js";

export type Outputs =
  | AgentHandoffEntry
  | FunctionCallEntry
  | ToolExecutionEntry
  | MessageOutputEntry;

/**
 * The response after appending new entries to the conversation.
 */
export type ConversationResponse = {
  object?: "conversation.response" | undefined;
  conversationId: string;
  outputs: Array<
    | AgentHandoffEntry
    | FunctionCallEntry
    | ToolExecutionEntry
    | MessageOutputEntry
  >;
  usage: ConversationUsageInfo;
};

/** @internal */
export const Outputs$inboundSchema: z.ZodType<Outputs, z.ZodTypeDef, unknown> =
  z.union([
    AgentHandoffEntry$inboundSchema,
    FunctionCallEntry$inboundSchema,
    ToolExecutionEntry$inboundSchema,
    MessageOutputEntry$inboundSchema,
  ]);

export function outputsFromJSON(
  jsonString: string,
): SafeParseResult<Outputs, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Outputs$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Outputs' from JSON`,
  );
}

/** @internal */
export const ConversationResponse$inboundSchema: z.ZodType<
  ConversationResponse,
  z.ZodTypeDef,
  unknown
> = z.object({
  object: z.literal("conversation.response").default("conversation.response"),
  conversation_id: z.string(),
  outputs: z.array(
    z.union([
      AgentHandoffEntry$inboundSchema,
      FunctionCallEntry$inboundSchema,
      ToolExecutionEntry$inboundSchema,
      MessageOutputEntry$inboundSchema,
    ]),
  ),
  usage: ConversationUsageInfo$inboundSchema,
}).transform((v) => {
  return remap$(v, {
    "conversation_id": "conversationId",
  });
});

export function conversationResponseFromJSON(
  jsonString: string,
): SafeParseResult<ConversationResponse, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => ConversationResponse$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'ConversationResponse' from JSON`,
  );
}
