import {z} from 'zod' export type IntegrationMode = 'TEXT' | 'AVATAR'; export type CreateApiModelQuerySharedStreamArgs = {question: string, token: string, conversation_id: string, user_generated_id?: string | undefined, integration_mode?: IntegrationMode | undefined, avatar_id?: string | undefined}; export type CreateApiModelQuerySharedStreamOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createApiModelQuerySharedStreamResponse = z.unknown(); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Make Shared Query Stream * * POST /model/query-shared-stream */ export const createApiModelQuerySharedStream = async (args: CreateApiModelQuerySharedStreamArgs, options: CreateApiModelQuerySharedStreamOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.question != null) query.append("question", String(args.question)) if (args.token != null) query.append("token", String(args.token)) if (args.conversation_id != null) query.append("conversation_id", String(args.conversation_id)) if (args.user_generated_id != null) query.append("user_generated_id", String(args.user_generated_id)) if (args.integration_mode != null) query.append("integration_mode", String(args.integration_mode)) if (args.avatar_id != null) query.append("avatar_id", String(args.avatar_id)) const url = `${baseUrl.replace(/\/+$/, '')}/model/query-shared-stream${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiModelQuerySharedStreamResponse, await res.json()) };