/*
 * 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 {
  CodeInterpreterTool,
  CodeInterpreterTool$inboundSchema,
} from "./codeinterpretertool.js";
import {
  CompletionArgs,
  CompletionArgs$inboundSchema,
} from "./completionargs.js";
import {
  DocumentLibraryTool,
  DocumentLibraryTool$inboundSchema,
} from "./documentlibrarytool.js";
import { FunctionTool, FunctionTool$inboundSchema } from "./functiontool.js";
import {
  ImageGenerationTool,
  ImageGenerationTool$inboundSchema,
} from "./imagegenerationtool.js";
import {
  WebSearchPremiumTool,
  WebSearchPremiumTool$inboundSchema,
} from "./websearchpremiumtool.js";
import { WebSearchTool, WebSearchTool$inboundSchema } from "./websearchtool.js";

export type AgentTools =
  | (CodeInterpreterTool & { type: "code_interpreter" })
  | (DocumentLibraryTool & { type: "document_library" })
  | (FunctionTool & { type: "function" })
  | (ImageGenerationTool & { type: "image_generation" })
  | (WebSearchTool & { type: "web_search" })
  | (WebSearchPremiumTool & { type: "web_search_premium" });

export type Agent = {
  /**
   * Instruction prompt the model will follow during the conversation.
   */
  instructions?: string | null | undefined;
  /**
   * List of tools which are available to the model during the conversation.
   */
  tools?:
    | Array<
      | (CodeInterpreterTool & { type: "code_interpreter" })
      | (DocumentLibraryTool & { type: "document_library" })
      | (FunctionTool & { type: "function" })
      | (ImageGenerationTool & { type: "image_generation" })
      | (WebSearchTool & { type: "web_search" })
      | (WebSearchPremiumTool & { type: "web_search_premium" })
    >
    | undefined;
  /**
   * White-listed arguments from the completion API
   */
  completionArgs?: CompletionArgs | undefined;
  model: string;
  name: string;
  description?: string | null | undefined;
  handoffs?: Array<string> | null | undefined;
  metadata?: { [k: string]: any } | null | undefined;
  object?: "agent" | undefined;
  id: string;
  version: number;
  versions: Array<number>;
  createdAt: Date;
  updatedAt: Date;
  deploymentChat: boolean;
  source: string;
  versionMessage?: string | null | undefined;
};

/** @internal */
export const AgentTools$inboundSchema: z.ZodType<
  AgentTools,
  z.ZodTypeDef,
  unknown
> = z.union([
  CodeInterpreterTool$inboundSchema.and(
    z.object({ type: z.literal("code_interpreter") }),
  ),
  DocumentLibraryTool$inboundSchema.and(
    z.object({ type: z.literal("document_library") }),
  ),
  FunctionTool$inboundSchema.and(z.object({ type: z.literal("function") })),
  ImageGenerationTool$inboundSchema.and(
    z.object({ type: z.literal("image_generation") }),
  ),
  WebSearchTool$inboundSchema.and(z.object({ type: z.literal("web_search") })),
  WebSearchPremiumTool$inboundSchema.and(
    z.object({ type: z.literal("web_search_premium") }),
  ),
]);

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

/** @internal */
export const Agent$inboundSchema: z.ZodType<Agent, z.ZodTypeDef, unknown> = z
  .object({
    instructions: z.nullable(z.string()).optional(),
    tools: z.array(
      z.union([
        CodeInterpreterTool$inboundSchema.and(
          z.object({ type: z.literal("code_interpreter") }),
        ),
        DocumentLibraryTool$inboundSchema.and(
          z.object({ type: z.literal("document_library") }),
        ),
        FunctionTool$inboundSchema.and(
          z.object({ type: z.literal("function") }),
        ),
        ImageGenerationTool$inboundSchema.and(
          z.object({ type: z.literal("image_generation") }),
        ),
        WebSearchTool$inboundSchema.and(
          z.object({ type: z.literal("web_search") }),
        ),
        WebSearchPremiumTool$inboundSchema.and(
          z.object({ type: z.literal("web_search_premium") }),
        ),
      ]),
    ).optional(),
    completion_args: CompletionArgs$inboundSchema.optional(),
    model: z.string(),
    name: z.string(),
    description: z.nullable(z.string()).optional(),
    handoffs: z.nullable(z.array(z.string())).optional(),
    metadata: z.nullable(z.record(z.any())).optional(),
    object: z.literal("agent").default("agent"),
    id: z.string(),
    version: z.number().int(),
    versions: z.array(z.number().int()),
    created_at: z.string().datetime({ offset: true }).transform(v =>
      new Date(v)
    ),
    updated_at: z.string().datetime({ offset: true }).transform(v =>
      new Date(v)
    ),
    deployment_chat: z.boolean(),
    source: z.string(),
    version_message: z.nullable(z.string()).optional(),
  }).transform((v) => {
    return remap$(v, {
      "completion_args": "completionArgs",
      "created_at": "createdAt",
      "updated_at": "updatedAt",
      "deployment_chat": "deploymentChat",
      "version_message": "versionMessage",
    });
  });

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