/* * 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 * as components from "../components/index.js"; import { SDKValidationError } from "../errors/sdkvalidationerror.js"; export type CreateChatCompletionStreamRequest = { /** * Optional external end-user identifier forwarded by the API gateway. */ xOnBehalfOf?: string | undefined; openAIChatCompletionRequestParams: components.OpenAIChatCompletionRequestParams; }; export type Three = { event: "error"; data?: string | null | undefined; id?: string | undefined; retry?: number | undefined; }; export type Two = { event: "done"; data?: string | null | undefined; id?: string | undefined; retry?: number | undefined; }; export type One = { event: "message"; /** * Represents a streamed chunk of a chat completion response */ data: components.OpenAIChatCompletionChunk; id?: string | undefined; retry?: number | undefined; }; /** * OK */ export type CreateChatCompletionStreamResponseBody = One | Two | Three; /** @internal */ export const CreateChatCompletionStreamRequest$inboundSchema: z.ZodType< CreateChatCompletionStreamRequest, z.ZodTypeDef, unknown > = z.object({ "X-On-Behalf-Of": z.string().optional(), OpenAIChatCompletionRequestParams: components.OpenAIChatCompletionRequestParams$inboundSchema, }).transform((v) => { return remap$(v, { "X-On-Behalf-Of": "xOnBehalfOf", "OpenAIChatCompletionRequestParams": "openAIChatCompletionRequestParams", }); }); /** @internal */ export type CreateChatCompletionStreamRequest$Outbound = { "X-On-Behalf-Of"?: string | undefined; OpenAIChatCompletionRequestParams: components.OpenAIChatCompletionRequestParams$Outbound; }; /** @internal */ export const CreateChatCompletionStreamRequest$outboundSchema: z.ZodType< CreateChatCompletionStreamRequest$Outbound, z.ZodTypeDef, CreateChatCompletionStreamRequest > = z.object({ xOnBehalfOf: z.string().optional(), openAIChatCompletionRequestParams: components.OpenAIChatCompletionRequestParams$outboundSchema, }).transform((v) => { return remap$(v, { xOnBehalfOf: "X-On-Behalf-Of", openAIChatCompletionRequestParams: "OpenAIChatCompletionRequestParams", }); }); export function createChatCompletionStreamRequestToJSON( createChatCompletionStreamRequest: CreateChatCompletionStreamRequest, ): string { return JSON.stringify( CreateChatCompletionStreamRequest$outboundSchema.parse( createChatCompletionStreamRequest, ), ); } export function createChatCompletionStreamRequestFromJSON( jsonString: string, ): SafeParseResult { return safeParse( jsonString, (x) => CreateChatCompletionStreamRequest$inboundSchema.parse(JSON.parse(x)), `Failed to parse 'CreateChatCompletionStreamRequest' from JSON`, ); } /** @internal */ export const Three$inboundSchema: z.ZodType = z .object({ event: z.literal("error"), data: z.nullable(z.string()).optional(), id: z.string().optional(), retry: z.number().int().optional(), }); /** @internal */ export type Three$Outbound = { event: "error"; data?: string | null | undefined; id?: string | undefined; retry?: number | undefined; }; /** @internal */ export const Three$outboundSchema: z.ZodType< Three$Outbound, z.ZodTypeDef, Three > = z.object({ event: z.literal("error"), data: z.nullable(z.string()).optional(), id: z.string().optional(), retry: z.number().int().optional(), }); export function threeToJSON(three: Three): string { return JSON.stringify(Three$outboundSchema.parse(three)); } export function threeFromJSON( jsonString: string, ): SafeParseResult { return safeParse( jsonString, (x) => Three$inboundSchema.parse(JSON.parse(x)), `Failed to parse 'Three' from JSON`, ); } /** @internal */ export const Two$inboundSchema: z.ZodType = z .object({ event: z.literal("done"), data: z.nullable(z.string()).optional(), id: z.string().optional(), retry: z.number().int().optional(), }); /** @internal */ export type Two$Outbound = { event: "done"; data?: string | null | undefined; id?: string | undefined; retry?: number | undefined; }; /** @internal */ export const Two$outboundSchema: z.ZodType = z .object({ event: z.literal("done"), data: z.nullable(z.string()).optional(), id: z.string().optional(), retry: z.number().int().optional(), }); export function twoToJSON(two: Two): string { return JSON.stringify(Two$outboundSchema.parse(two)); } export function twoFromJSON( jsonString: string, ): SafeParseResult { return safeParse( jsonString, (x) => Two$inboundSchema.parse(JSON.parse(x)), `Failed to parse 'Two' from JSON`, ); } /** @internal */ export const One$inboundSchema: z.ZodType = z .object({ event: z.literal("message"), data: z.unknown().transform((v, ctx) => { if (typeof v !== "string") return v; try { return JSON.parse(v); } catch (err) { ctx.addIssue({ code: "custom", message: `malformed json: ${err}` }); return z.NEVER; } }).pipe(components.OpenAIChatCompletionChunk$inboundSchema), id: z.string().optional(), retry: z.number().int().optional(), }); /** @internal */ export type One$Outbound = { event: "message"; data: components.OpenAIChatCompletionChunk$Outbound; id?: string | undefined; retry?: number | undefined; }; /** @internal */ export const One$outboundSchema: z.ZodType = z .object({ event: z.literal("message"), data: components.OpenAIChatCompletionChunk$outboundSchema, id: z.string().optional(), retry: z.number().int().optional(), }); export function oneToJSON(one: One): string { return JSON.stringify(One$outboundSchema.parse(one)); } export function oneFromJSON( jsonString: string, ): SafeParseResult { return safeParse( jsonString, (x) => One$inboundSchema.parse(JSON.parse(x)), `Failed to parse 'One' from JSON`, ); } /** @internal */ export const CreateChatCompletionStreamResponseBody$inboundSchema: z.ZodType< CreateChatCompletionStreamResponseBody, z.ZodTypeDef, unknown > = z.union([ z.lazy(() => One$inboundSchema), z.lazy(() => Two$inboundSchema), z.lazy(() => Three$inboundSchema), ]); /** @internal */ export type CreateChatCompletionStreamResponseBody$Outbound = | One$Outbound | Two$Outbound | Three$Outbound; /** @internal */ export const CreateChatCompletionStreamResponseBody$outboundSchema: z.ZodType< CreateChatCompletionStreamResponseBody$Outbound, z.ZodTypeDef, CreateChatCompletionStreamResponseBody > = z.union([ z.lazy(() => One$outboundSchema), z.lazy(() => Two$outboundSchema), z.lazy(() => Three$outboundSchema), ]); export function createChatCompletionStreamResponseBodyToJSON( createChatCompletionStreamResponseBody: CreateChatCompletionStreamResponseBody, ): string { return JSON.stringify( CreateChatCompletionStreamResponseBody$outboundSchema.parse( createChatCompletionStreamResponseBody, ), ); } export function createChatCompletionStreamResponseBodyFromJSON( jsonString: string, ): SafeParseResult { return safeParse( jsonString, (x) => CreateChatCompletionStreamResponseBody$inboundSchema.parse(JSON.parse(x)), `Failed to parse 'CreateChatCompletionStreamResponseBody' from JSON`, ); }