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

import * as z from "zod/v3";
import { safeParse } from "../../lib/schemas.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  AgentHandoffDoneEvent,
  AgentHandoffDoneEvent$inboundSchema,
} from "./agenthandoffdoneevent.js";
import {
  AgentHandoffStartedEvent,
  AgentHandoffStartedEvent$inboundSchema,
} from "./agenthandoffstartedevent.js";
import {
  FunctionCallEvent,
  FunctionCallEvent$inboundSchema,
} from "./functioncallevent.js";
import {
  MessageOutputEvent,
  MessageOutputEvent$inboundSchema,
} from "./messageoutputevent.js";
import {
  ResponseDoneEvent,
  ResponseDoneEvent$inboundSchema,
} from "./responsedoneevent.js";
import {
  ResponseErrorEvent,
  ResponseErrorEvent$inboundSchema,
} from "./responseerrorevent.js";
import {
  ResponseStartedEvent,
  ResponseStartedEvent$inboundSchema,
} from "./responsestartedevent.js";
import { SSETypes, SSETypes$inboundSchema } from "./ssetypes.js";
import {
  ToolExecutionDeltaEvent,
  ToolExecutionDeltaEvent$inboundSchema,
} from "./toolexecutiondeltaevent.js";
import {
  ToolExecutionDoneEvent,
  ToolExecutionDoneEvent$inboundSchema,
} from "./toolexecutiondoneevent.js";
import {
  ToolExecutionStartedEvent,
  ToolExecutionStartedEvent$inboundSchema,
} from "./toolexecutionstartedevent.js";

export type ConversationEventsData =
  | (AgentHandoffDoneEvent & { type: "agent.handoff.done" })
  | (AgentHandoffStartedEvent & { type: "agent.handoff.started" })
  | (ResponseDoneEvent & { type: "conversation.response.done" })
  | (ResponseErrorEvent & { type: "conversation.response.error" })
  | (ResponseStartedEvent & { type: "conversation.response.started" })
  | (FunctionCallEvent & { type: "function.call.delta" })
  | (MessageOutputEvent & { type: "message.output.delta" })
  | (ToolExecutionDeltaEvent & { type: "tool.execution.delta" })
  | (ToolExecutionDoneEvent & { type: "tool.execution.done" })
  | (ToolExecutionStartedEvent & { type: "tool.execution.started" });

export type ConversationEvents = {
  /**
   * Server side events sent when streaming a conversation response.
   */
  event: SSETypes;
  data:
    | (AgentHandoffDoneEvent & { type: "agent.handoff.done" })
    | (AgentHandoffStartedEvent & { type: "agent.handoff.started" })
    | (ResponseDoneEvent & { type: "conversation.response.done" })
    | (ResponseErrorEvent & { type: "conversation.response.error" })
    | (ResponseStartedEvent & { type: "conversation.response.started" })
    | (FunctionCallEvent & { type: "function.call.delta" })
    | (MessageOutputEvent & { type: "message.output.delta" })
    | (ToolExecutionDeltaEvent & { type: "tool.execution.delta" })
    | (ToolExecutionDoneEvent & { type: "tool.execution.done" })
    | (ToolExecutionStartedEvent & { type: "tool.execution.started" });
};

/** @internal */
export const ConversationEventsData$inboundSchema: z.ZodType<
  ConversationEventsData,
  z.ZodTypeDef,
  unknown
> = z.union([
  AgentHandoffDoneEvent$inboundSchema.and(
    z.object({ type: z.literal("agent.handoff.done") }),
  ),
  AgentHandoffStartedEvent$inboundSchema.and(
    z.object({ type: z.literal("agent.handoff.started") }),
  ),
  ResponseDoneEvent$inboundSchema.and(
    z.object({ type: z.literal("conversation.response.done") }),
  ),
  ResponseErrorEvent$inboundSchema.and(
    z.object({ type: z.literal("conversation.response.error") }),
  ),
  ResponseStartedEvent$inboundSchema.and(
    z.object({ type: z.literal("conversation.response.started") }),
  ),
  FunctionCallEvent$inboundSchema.and(
    z.object({ type: z.literal("function.call.delta") }),
  ),
  MessageOutputEvent$inboundSchema.and(
    z.object({ type: z.literal("message.output.delta") }),
  ),
  ToolExecutionDeltaEvent$inboundSchema.and(
    z.object({ type: z.literal("tool.execution.delta") }),
  ),
  ToolExecutionDoneEvent$inboundSchema.and(
    z.object({ type: z.literal("tool.execution.done") }),
  ),
  ToolExecutionStartedEvent$inboundSchema.and(
    z.object({ type: z.literal("tool.execution.started") }),
  ),
]);

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

/** @internal */
export const ConversationEvents$inboundSchema: z.ZodType<
  ConversationEvents,
  z.ZodTypeDef,
  unknown
> = z.object({
  event: SSETypes$inboundSchema,
  data: z.string().transform((v, ctx) => {
    try {
      return JSON.parse(v);
    } catch (err) {
      ctx.addIssue({ code: "custom", message: `malformed json: ${err}` });
      return z.NEVER;
    }
  }).pipe(
    z.union([
      AgentHandoffDoneEvent$inboundSchema.and(
        z.object({ type: z.literal("agent.handoff.done") }),
      ),
      AgentHandoffStartedEvent$inboundSchema.and(
        z.object({ type: z.literal("agent.handoff.started") }),
      ),
      ResponseDoneEvent$inboundSchema.and(
        z.object({ type: z.literal("conversation.response.done") }),
      ),
      ResponseErrorEvent$inboundSchema.and(
        z.object({ type: z.literal("conversation.response.error") }),
      ),
      ResponseStartedEvent$inboundSchema.and(
        z.object({ type: z.literal("conversation.response.started") }),
      ),
      FunctionCallEvent$inboundSchema.and(
        z.object({ type: z.literal("function.call.delta") }),
      ),
      MessageOutputEvent$inboundSchema.and(
        z.object({ type: z.literal("message.output.delta") }),
      ),
      ToolExecutionDeltaEvent$inboundSchema.and(
        z.object({ type: z.literal("tool.execution.delta") }),
      ),
      ToolExecutionDoneEvent$inboundSchema.and(
        z.object({ type: z.literal("tool.execution.done") }),
      ),
      ToolExecutionStartedEvent$inboundSchema.and(
        z.object({ type: z.literal("tool.execution.started") }),
      ),
    ]),
  ),
});

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