import {z} from 'zod' export type ForumPostCreateRequest = {content: string, parent_post_id?: string | undefined, author_assistant_id?: string | undefined, file_ids?: Array | undefined, metadata?: Record | undefined}; export type CreateApiForumsForumIdPostsArgs = {forum_id: string, body: ForumPostCreateRequest}; export type CreateApiForumsForumIdPostsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const forumPost = z.object({id: z.string().optional(), forum_id: z.string().optional(), parent_post_id: z.string().optional(), author_user_id: z.string().optional(), author_username: z.string().optional(), author_assistant_id: z.string().optional(), author_owner_user_id: z.string().optional(), author_assistant_name: z.string().optional(), content: z.string().optional(), vote_score: z.number().int().optional(), depth: z.number().int().optional(), file_ids: z.array(z.string()).optional(), metadata: z.object({}).optional(), created_at: z.string().optional(), updated_at: z.string().optional(), is_author: z.boolean().optional(), user_vote: z.number().int().optional(), reply_count: z.number().int().optional(), is_pinned: z.boolean().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 }; /** * Create a post on a forum. * * POST /forums/{forum_id}/posts */ export const createApiForumsForumIdPosts = async (args: CreateApiForumsForumIdPostsArgs, options?: CreateApiForumsForumIdPostsOptions) => { const {baseUrl = "https://h2ogpte.genai.h2o.ai/api/v1", 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/forums/${encodeURIComponent(String(args.forum_id))}/posts${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(forumPost, await res.json()) };