/*
 * 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 { ContentChunk, ContentChunk$inboundSchema } from "./contentchunk.js";
import { ToolCall, ToolCall$inboundSchema } from "./toolcall.js";

export type Content = string | Array<ContentChunk>;

export type DeltaMessage = {
  role?: string | null | undefined;
  content?: string | Array<ContentChunk> | null | undefined;
  toolCalls?: Array<ToolCall> | null | undefined;
};

/** @internal */
export const Content$inboundSchema: z.ZodType<Content, z.ZodTypeDef, unknown> =
  z.union([z.string(), z.array(ContentChunk$inboundSchema)]);

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

/** @internal */
export const DeltaMessage$inboundSchema: z.ZodType<
  DeltaMessage,
  z.ZodTypeDef,
  unknown
> = z.object({
  role: z.nullable(z.string()).optional(),
  content: z.nullable(
    z.union([z.string(), z.array(ContentChunk$inboundSchema)]),
  ).optional(),
  tool_calls: z.nullable(z.array(ToolCall$inboundSchema)).optional(),
}).transform((v) => {
  return remap$(v, {
    "tool_calls": "toolCalls",
  });
});

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