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

import * as z from "zod/v4";
import { remap as remap$ } from "../../lib/primitives.js";
import { smartUnion } from "../../types/smartUnion.js";
import {
  AssistantMessage,
  AssistantMessage$Outbound,
  AssistantMessage$outboundSchema,
} from "./assistantmessage.js";
import {
  CodeInterpreterTool,
  CodeInterpreterTool$Outbound,
  CodeInterpreterTool$outboundSchema,
} from "./codeinterpretertool.js";
import {
  CustomConnector,
  CustomConnector$Outbound,
  CustomConnector$outboundSchema,
} from "./customconnector.js";
import {
  DocumentLibraryTool,
  DocumentLibraryTool$Outbound,
  DocumentLibraryTool$outboundSchema,
} from "./documentlibrarytool.js";
import {
  GuardrailConfig,
  GuardrailConfig$Outbound,
  GuardrailConfig$outboundSchema,
} from "./guardrailconfig.js";
import {
  ImageGenerationTool,
  ImageGenerationTool$Outbound,
  ImageGenerationTool$outboundSchema,
} from "./imagegenerationtool.js";
import {
  MistralPromptMode,
  MistralPromptMode$outboundSchema,
} from "./mistralpromptmode.js";
import {
  Prediction,
  Prediction$Outbound,
  Prediction$outboundSchema,
} from "./prediction.js";
import {
  ReasoningEffort,
  ReasoningEffort$outboundSchema,
} from "./reasoningeffort.js";
import {
  ResponseFormat,
  ResponseFormat$Outbound,
  ResponseFormat$outboundSchema,
} from "./responseformat.js";
import {
  SystemMessage,
  SystemMessage$Outbound,
  SystemMessage$outboundSchema,
} from "./systemmessage.js";
import { Tool, Tool$Outbound, Tool$outboundSchema } from "./tool.js";
import {
  ToolChoice,
  ToolChoice$Outbound,
  ToolChoice$outboundSchema,
} from "./toolchoice.js";
import {
  ToolChoiceEnum,
  ToolChoiceEnum$outboundSchema,
} from "./toolchoiceenum.js";
import {
  ToolMessage,
  ToolMessage$Outbound,
  ToolMessage$outboundSchema,
} from "./toolmessage.js";
import {
  UserMessage,
  UserMessage$Outbound,
  UserMessage$outboundSchema,
} from "./usermessage.js";
import {
  WebSearchPremiumTool,
  WebSearchPremiumTool$Outbound,
  WebSearchPremiumTool$outboundSchema,
} from "./websearchpremiumtool.js";
import {
  WebSearchTool,
  WebSearchTool$Outbound,
  WebSearchTool$outboundSchema,
} from "./websearchtool.js";

/**
 * Stop generation if this token is detected. Or if one of these tokens is detected when providing an array
 */
export type ChatCompletionStreamRequestStop = string | Array<string>;

export type ChatCompletionStreamRequestMessage =
  | (AssistantMessage & { role: "assistant" })
  | SystemMessage
  | ToolMessage
  | UserMessage;

export type ChatCompletionStreamRequestTool =
  | Tool
  | WebSearchTool
  | WebSearchPremiumTool
  | CodeInterpreterTool
  | ImageGenerationTool
  | DocumentLibraryTool
  | CustomConnector;

/**
 * Controls which (if any) tool is called by the model. `none` means the model will not call any tool and instead generates a message. `auto` means the model can pick between generating a message or calling one or more tools. `any` or `required` means the model must call one or more tools. Specifying a particular tool via `{"type": "function", "function": {"name": "my_function"}}` forces the model to call that tool.
 */
export type ChatCompletionStreamRequestToolChoice = ToolChoice | ToolChoiceEnum;

export type ChatCompletionStreamRequest = {
  /**
   * ID of the model to use. You can use the [List Available Models](/api/#tag/models/operation/list_models_v1_models_get) API to see all of your available models, or see our [Model overview](/models) for model descriptions.
   */
  model: string;
  /**
   * What sampling temperature to use, we recommend between 0.0 and 0.7. Higher values like 0.7 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. We generally recommend altering this or `top_p` but not both. The default value varies depending on the model you are targeting. Call the `/models` endpoint to retrieve the appropriate value.
   */
  temperature?: number | null | undefined;
  /**
   * Nucleus sampling, where the model considers the results of the tokens with `top_p` probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or `temperature` but not both.
   */
  topP?: number | null | undefined;
  /**
   * The maximum number of tokens to generate in the completion. The token count of your prompt plus `max_tokens` cannot exceed the model's context length.
   */
  maxTokens?: number | null | undefined;
  stream?: boolean | undefined;
  /**
   * Stop generation if this token is detected. Or if one of these tokens is detected when providing an array
   */
  stop?: string | Array<string> | null | undefined;
  /**
   * The seed to use for random sampling. If set, different calls will generate deterministic results.
   */
  randomSeed?: number | null | undefined;
  metadata?: { [k: string]: any } | null | undefined;
  /**
   * The prompt(s) to generate completions for, encoded as a list of dict with role and content.
   */
  messages: Array<
    | (AssistantMessage & { role: "assistant" })
    | SystemMessage
    | ToolMessage
    | UserMessage
  >;
  /**
   * Specify the format that the model must output. By default it will use `{ "type": "text" }`. Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the message the model generates is in JSON. When using JSON mode you MUST also instruct the model to produce JSON yourself with a system or a user message. Setting to `{ "type": "json_schema" }` enables JSON schema mode, which guarantees the message the model generates is in JSON and follows the schema you provide.
   */
  responseFormat?: ResponseFormat | undefined;
  /**
   * A list of tools the model may call. Use this to provide a list of functions the model may generate JSON inputs for.
   */
  tools?:
    | Array<
      | Tool
      | WebSearchTool
      | WebSearchPremiumTool
      | CodeInterpreterTool
      | ImageGenerationTool
      | DocumentLibraryTool
      | CustomConnector
    >
    | null
    | undefined;
  /**
   * Controls which (if any) tool is called by the model. `none` means the model will not call any tool and instead generates a message. `auto` means the model can pick between generating a message or calling one or more tools. `any` or `required` means the model must call one or more tools. Specifying a particular tool via `{"type": "function", "function": {"name": "my_function"}}` forces the model to call that tool.
   */
  toolChoice?: ToolChoice | ToolChoiceEnum | undefined;
  /**
   * The `presence_penalty` determines how much the model penalizes the repetition of words or phrases. A higher presence penalty encourages the model to use a wider variety of words and phrases, making the output more diverse and creative.
   */
  presencePenalty?: number | null | undefined;
  /**
   * The `frequency_penalty` penalizes the repetition of words based on their frequency in the generated text. A higher frequency penalty discourages the model from repeating words that have already appeared frequently in the output, promoting diversity and reducing repetition.
   */
  frequencyPenalty?: number | null | undefined;
  /**
   * Number of completions to return for each request, input tokens are only billed once.
   */
  n?: number | null | undefined;
  /**
   * Enable users to specify an expected completion, optimizing response times by leveraging known or predictable content.
   */
  prediction?: Prediction | undefined;
  /**
   * Whether to enable parallel function calling during tool use, when enabled the model can call multiple tools in parallel.
   */
  parallelToolCalls?: boolean | undefined;
  reasoningEffort?: ReasoningEffort | null | undefined;
  /**
   * Allows toggling between the reasoning mode and no system prompt. When set to `reasoning` the system prompt for reasoning models will be used.
   */
  promptMode?: MistralPromptMode | null | undefined;
  guardrails?: Array<GuardrailConfig> | null | undefined;
  promptCacheKey?: string | null | undefined;
  /**
   * Whether to inject a safety prompt before all conversations.
   */
  safePrompt?: boolean | undefined;
};

/** @internal */
export type ChatCompletionStreamRequestStop$Outbound = string | Array<string>;

/** @internal */
export const ChatCompletionStreamRequestStop$outboundSchema: z.ZodType<
  ChatCompletionStreamRequestStop$Outbound,
  ChatCompletionStreamRequestStop
> = smartUnion([z.string(), z.array(z.string())]);

export function chatCompletionStreamRequestStopToJSON(
  chatCompletionStreamRequestStop: ChatCompletionStreamRequestStop,
): string {
  return JSON.stringify(
    ChatCompletionStreamRequestStop$outboundSchema.parse(
      chatCompletionStreamRequestStop,
    ),
  );
}

/** @internal */
export type ChatCompletionStreamRequestMessage$Outbound =
  | (AssistantMessage$Outbound & { role: "assistant" })
  | SystemMessage$Outbound
  | ToolMessage$Outbound
  | UserMessage$Outbound;

/** @internal */
export const ChatCompletionStreamRequestMessage$outboundSchema: z.ZodType<
  ChatCompletionStreamRequestMessage$Outbound,
  ChatCompletionStreamRequestMessage
> = z.union([
  AssistantMessage$outboundSchema.and(
    z.object({ role: z.literal("assistant") }),
  ),
  SystemMessage$outboundSchema,
  ToolMessage$outboundSchema,
  UserMessage$outboundSchema,
]);

export function chatCompletionStreamRequestMessageToJSON(
  chatCompletionStreamRequestMessage: ChatCompletionStreamRequestMessage,
): string {
  return JSON.stringify(
    ChatCompletionStreamRequestMessage$outboundSchema.parse(
      chatCompletionStreamRequestMessage,
    ),
  );
}

/** @internal */
export type ChatCompletionStreamRequestTool$Outbound =
  | Tool$Outbound
  | WebSearchTool$Outbound
  | WebSearchPremiumTool$Outbound
  | CodeInterpreterTool$Outbound
  | ImageGenerationTool$Outbound
  | DocumentLibraryTool$Outbound
  | CustomConnector$Outbound;

/** @internal */
export const ChatCompletionStreamRequestTool$outboundSchema: z.ZodType<
  ChatCompletionStreamRequestTool$Outbound,
  ChatCompletionStreamRequestTool
> = z.union([
  Tool$outboundSchema,
  WebSearchTool$outboundSchema,
  WebSearchPremiumTool$outboundSchema,
  CodeInterpreterTool$outboundSchema,
  ImageGenerationTool$outboundSchema,
  DocumentLibraryTool$outboundSchema,
  CustomConnector$outboundSchema,
]);

export function chatCompletionStreamRequestToolToJSON(
  chatCompletionStreamRequestTool: ChatCompletionStreamRequestTool,
): string {
  return JSON.stringify(
    ChatCompletionStreamRequestTool$outboundSchema.parse(
      chatCompletionStreamRequestTool,
    ),
  );
}

/** @internal */
export type ChatCompletionStreamRequestToolChoice$Outbound =
  | ToolChoice$Outbound
  | string;

/** @internal */
export const ChatCompletionStreamRequestToolChoice$outboundSchema: z.ZodType<
  ChatCompletionStreamRequestToolChoice$Outbound,
  ChatCompletionStreamRequestToolChoice
> = smartUnion([ToolChoice$outboundSchema, ToolChoiceEnum$outboundSchema]);

export function chatCompletionStreamRequestToolChoiceToJSON(
  chatCompletionStreamRequestToolChoice: ChatCompletionStreamRequestToolChoice,
): string {
  return JSON.stringify(
    ChatCompletionStreamRequestToolChoice$outboundSchema.parse(
      chatCompletionStreamRequestToolChoice,
    ),
  );
}

/** @internal */
export type ChatCompletionStreamRequest$Outbound = {
  model: string;
  temperature?: number | null | undefined;
  top_p?: number | null | undefined;
  max_tokens?: number | null | undefined;
  stream: boolean;
  stop?: string | Array<string> | null | undefined;
  random_seed?: number | null | undefined;
  metadata?: { [k: string]: any } | null | undefined;
  messages: Array<
    | (AssistantMessage$Outbound & { role: "assistant" })
    | SystemMessage$Outbound
    | ToolMessage$Outbound
    | UserMessage$Outbound
  >;
  response_format?: ResponseFormat$Outbound | undefined;
  tools?:
    | Array<
      | Tool$Outbound
      | WebSearchTool$Outbound
      | WebSearchPremiumTool$Outbound
      | CodeInterpreterTool$Outbound
      | ImageGenerationTool$Outbound
      | DocumentLibraryTool$Outbound
      | CustomConnector$Outbound
    >
    | null
    | undefined;
  tool_choice?: ToolChoice$Outbound | string | undefined;
  presence_penalty?: number | null | undefined;
  frequency_penalty?: number | null | undefined;
  n?: number | null | undefined;
  prediction?: Prediction$Outbound | undefined;
  parallel_tool_calls?: boolean | undefined;
  reasoning_effort?: string | null | undefined;
  prompt_mode?: string | null | undefined;
  guardrails?: Array<GuardrailConfig$Outbound> | null | undefined;
  prompt_cache_key?: string | null | undefined;
  safe_prompt?: boolean | undefined;
};

/** @internal */
export const ChatCompletionStreamRequest$outboundSchema: z.ZodType<
  ChatCompletionStreamRequest$Outbound,
  ChatCompletionStreamRequest
> = z.object({
  model: z.string(),
  temperature: z.nullable(z.number()).optional(),
  topP: z.nullable(z.number()).optional(),
  maxTokens: z.nullable(z.int()).optional(),
  stream: z.boolean().default(true),
  stop: z.nullable(smartUnion([z.string(), z.array(z.string())])).optional(),
  randomSeed: z.nullable(z.int()).optional(),
  metadata: z.nullable(z.record(z.string(), z.any())).optional(),
  messages: z.array(
    z.union([
      AssistantMessage$outboundSchema.and(
        z.object({ role: z.literal("assistant") }),
      ),
      SystemMessage$outboundSchema,
      ToolMessage$outboundSchema,
      UserMessage$outboundSchema,
    ]),
  ),
  responseFormat: ResponseFormat$outboundSchema.optional(),
  tools: z.nullable(
    z.array(
      z.union([
        Tool$outboundSchema,
        WebSearchTool$outboundSchema,
        WebSearchPremiumTool$outboundSchema,
        CodeInterpreterTool$outboundSchema,
        ImageGenerationTool$outboundSchema,
        DocumentLibraryTool$outboundSchema,
        CustomConnector$outboundSchema,
      ]),
    ),
  ).optional(),
  toolChoice: smartUnion([
    ToolChoice$outboundSchema,
    ToolChoiceEnum$outboundSchema,
  ]).optional(),
  presencePenalty: z.nullable(z.number()).optional(),
  frequencyPenalty: z.nullable(z.number()).optional(),
  n: z.nullable(z.int()).optional(),
  prediction: Prediction$outboundSchema.optional(),
  parallelToolCalls: z.boolean().optional(),
  reasoningEffort: z.nullable(ReasoningEffort$outboundSchema).optional(),
  promptMode: z.nullable(MistralPromptMode$outboundSchema).optional(),
  guardrails: z.nullable(z.array(GuardrailConfig$outboundSchema)).optional(),
  promptCacheKey: z.nullable(z.string()).optional(),
  safePrompt: z.boolean().optional(),
}).transform((v) => {
  return remap$(v, {
    topP: "top_p",
    maxTokens: "max_tokens",
    randomSeed: "random_seed",
    responseFormat: "response_format",
    toolChoice: "tool_choice",
    presencePenalty: "presence_penalty",
    frequencyPenalty: "frequency_penalty",
    parallelToolCalls: "parallel_tool_calls",
    reasoningEffort: "reasoning_effort",
    promptMode: "prompt_mode",
    promptCacheKey: "prompt_cache_key",
    safePrompt: "safe_prompt",
  });
});

export function chatCompletionStreamRequestToJSON(
  chatCompletionStreamRequest: ChatCompletionStreamRequest,
): string {
  return JSON.stringify(
    ChatCompletionStreamRequest$outboundSchema.parse(
      chatCompletionStreamRequest,
    ),
  );
}
