import {z} from 'zod' export type GetApiV2RolePlayGetScenarioArgs = {Authorization: string, role: string, lesson_id?: number | undefined, code: string}; export type GetApiV2RolePlayGetScenarioOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiV2RolePlayGetScenarioResponse = z.object({code: z.number().int().optional(), data: z.object({ai_role: z.string().optional(), context: z.string().optional(), keywords: z.string().optional(), learn_word: z.array(z.number().int()).optional(), location: z.string().optional(), raw_msgs: z.array(z.string()).optional(), starting_text: z.string().optional(), starting_text_audio: z.string().optional(), user_task: z.string().optional()}).optional(), message: z.string().optional()}); 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 }; /** * Get role play practice for linear * * GET /v2/role_play/get_scenario */ export const getApiV2RolePlayGetScenario = async (args: GetApiV2RolePlayGetScenarioArgs, options: GetApiV2RolePlayGetScenarioOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args.role != null) query.append("role", String(args.role)) if (args.lesson_id != null) query.append("lesson_id", String(args.lesson_id)) if (args.code != null) query.append("code", String(args.code)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/role_play/get_scenario${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV2RolePlayGetScenarioResponse, await res.json()) };