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

import * as z from "zod/v4";
import { safeParse } from "../../lib/schemas.js";
import * as discriminatedUnionTypes from "../../types/discriminatedUnion.js";
import { discriminatedUnion } from "../../types/discriminatedUnion.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
  | AgentHandoffStartedEvent
  | ResponseDoneEvent
  | ResponseErrorEvent
  | ResponseStartedEvent
  | FunctionCallEvent
  | MessageOutputEvent
  | ToolExecutionDeltaEvent
  | ToolExecutionDoneEvent
  | ToolExecutionStartedEvent
  | discriminatedUnionTypes.Unknown<"type">;

export type ConversationEvents = {
  /**
   * Server side events sent when streaming a conversation response.
   */
  event: SSETypes;
  data:
    | AgentHandoffDoneEvent
    | AgentHandoffStartedEvent
    | ResponseDoneEvent
    | ResponseErrorEvent
    | ResponseStartedEvent
    | FunctionCallEvent
    | MessageOutputEvent
    | ToolExecutionDeltaEvent
    | ToolExecutionDoneEvent
    | ToolExecutionStartedEvent
    | discriminatedUnionTypes.Unknown<"type">;
};

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

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,
  unknown
> = z.object({
  event: SSETypes$inboundSchema,
  data: z.string().transform((v, ctx) => {
    try {
      return JSON.parse(v);
    } catch (err) {
      ctx.addIssue({
        input: v,
        code: "custom",
        message: `malformed json: ${err}`,
      });
      return z.NEVER;
    }
  }).pipe(discriminatedUnion("type", {
    ["agent.handoff.done"]: AgentHandoffDoneEvent$inboundSchema,
    ["agent.handoff.started"]: AgentHandoffStartedEvent$inboundSchema,
    ["conversation.response.done"]: ResponseDoneEvent$inboundSchema,
    ["conversation.response.error"]: ResponseErrorEvent$inboundSchema,
    ["conversation.response.started"]: ResponseStartedEvent$inboundSchema,
    ["function.call.delta"]: FunctionCallEvent$inboundSchema,
    ["message.output.delta"]: MessageOutputEvent$inboundSchema,
    ["tool.execution.delta"]: ToolExecutionDeltaEvent$inboundSchema,
    ["tool.execution.done"]: ToolExecutionDoneEvent$inboundSchema,
    ["tool.execution.started"]: ToolExecutionStartedEvent$inboundSchema,
  })),
});

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