import {z} from 'zod' export type OpenAICompatibleMessage = {role: 'user' | 'assistant', content: string, alias?: (string | null) | undefined, created_at?: (string | null) | undefined}; export type ProactiveTopicRequest = {messages: Array, agent_context?: (string | null) | undefined}; export type CreateApiApiV1UsersRoleplayProactiveUserIdArgs = {user_id: unknown | unknown, topk?: number | undefined, max_token_size?: number | undefined, prefer_topics?: Array | undefined, only_topics?: Array | undefined, max_subtopic_size?: number | undefined, topic_limits_json?: string | undefined, body: ProactiveTopicRequest}; export type CreateApiApiV1UsersRoleplayProactiveUserIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const proactiveTopicData = z.object({action: z.enum(["new_topic", "continue"]), topic_prompt: z.string().nullable().optional()}); export const cODE = z.union([z.literal(0), z.literal(400), z.literal(401), z.literal(403), z.literal(404), z.literal(405), z.literal(409), z.literal(422), z.literal(500), z.literal(501), z.literal(502), z.literal(503), z.literal(504), z.literal(520)]); export const proactiveTopicResponse = z.object({data: proactiveTopicData.nullable().optional(), errno: cODE.optional(), errmsg: 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 }; /** * Infer Proactive Topics * * POST /api/v1/users/roleplay/proactive/{user_id} */ export const createApiApiV1UsersRoleplayProactiveUserId = async (args: CreateApiApiV1UsersRoleplayProactiveUserIdArgs, options?: CreateApiApiV1UsersRoleplayProactiveUserIdOptions) => { const {baseUrl = "https://api.memobase.dev", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.topk != null) query.append("topk", String(args.topk)) if (args.max_token_size != null) query.append("max_token_size", String(args.max_token_size)) for (const item of args.prefer_topics ?? []) query.append("prefer_topics", String(item)) for (const item of args.only_topics ?? []) query.append("only_topics", String(item)) if (args.max_subtopic_size != null) query.append("max_subtopic_size", String(args.max_subtopic_size)) if (args.topic_limits_json != null) query.append("topic_limits_json", String(args.topic_limits_json)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/users/roleplay/proactive/${encodeURIComponent(String(args.user_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(proactiveTopicResponse, await res.json()) };