/*
 * 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 { ClosedEnum } from "../../types/enums.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  AgentHandoffEntry,
  AgentHandoffEntry$inboundSchema,
} from "./agenthandoffentry.js";
import {
  FunctionCallEntry,
  FunctionCallEntry$inboundSchema,
} from "./functioncallentry.js";
import {
  FunctionResultEntry,
  FunctionResultEntry$inboundSchema,
} from "./functionresultentry.js";
import {
  MessageInputEntry,
  MessageInputEntry$inboundSchema,
} from "./messageinputentry.js";
import {
  MessageOutputEntry,
  MessageOutputEntry$inboundSchema,
} from "./messageoutputentry.js";
import {
  ToolExecutionEntry,
  ToolExecutionEntry$inboundSchema,
} from "./toolexecutionentry.js";

export const ConversationHistoryObject = {
  ConversationHistory: "conversation.history",
} as const;
export type ConversationHistoryObject = ClosedEnum<
  typeof ConversationHistoryObject
>;

export type Entries =
  | AgentHandoffEntry
  | FunctionCallEntry
  | MessageInputEntry
  | FunctionResultEntry
  | ToolExecutionEntry
  | MessageOutputEntry;

/**
 * Retrieve all entries in a conversation.
 */
export type ConversationHistory = {
  object: ConversationHistoryObject | undefined;
  conversationId: string;
  entries: Array<
    | AgentHandoffEntry
    | FunctionCallEntry
    | MessageInputEntry
    | FunctionResultEntry
    | ToolExecutionEntry
    | MessageOutputEntry
  >;
};

/** @internal */
export const ConversationHistoryObject$inboundSchema: z.ZodNativeEnum<
  typeof ConversationHistoryObject
> = z.nativeEnum(ConversationHistoryObject);

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

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

/** @internal */
export const ConversationHistory$inboundSchema: z.ZodType<
  ConversationHistory,
  z.ZodTypeDef,
  unknown
> = z.object({
  object: ConversationHistoryObject$inboundSchema.default(
    "conversation.history",
  ),
  conversation_id: z.string(),
  entries: z.array(
    z.union([
      AgentHandoffEntry$inboundSchema,
      FunctionCallEntry$inboundSchema,
      MessageInputEntry$inboundSchema,
      FunctionResultEntry$inboundSchema,
      ToolExecutionEntry$inboundSchema,
      MessageOutputEntry$inboundSchema,
    ]),
  ),
}).transform((v) => {
  return remap$(v, {
    "conversation_id": "conversationId",
  });
});

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