import {z} from 'zod' export type MyAgentsSortBy = 'most_recent' | 'name'; export type GetApiApiStoreMyUnpublishedAgentsArgs = {page?: number | undefined, page_size?: number | undefined, sort_by?: MyAgentsSortBy | undefined, search_query?: (string | null) | undefined}; export type GetApiApiStoreMyUnpublishedAgentsOptions = {baseUrl: string, HTTPBearerJWT?: string | undefined, fetch?: typeof fetch | undefined}; export const myUnpublishedAgent = z.object({graph_id: z.string(), graph_version: z.number().int(), agent_name: z.string(), agent_image: z.string().nullable().optional(), description: z.string(), last_edited: z.string(), recommended_schedule_cron: z.string().nullable().optional()}); export const pagination = z.object({total_items: z.number().int(), total_pages: z.number().int(), current_page: z.number().int(), page_size: z.number().int()}); export const myUnpublishedAgentsResponse = z.object({agents: z.array(myUnpublishedAgent), pagination: pagination}); 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 my agents * * GET /api/store/my-unpublished-agents */ export const getApiApiStoreMyUnpublishedAgents = async (args: GetApiApiStoreMyUnpublishedAgentsArgs, options: GetApiApiStoreMyUnpublishedAgentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearerJWT} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.search_query != null) query.append("search_query", String(args.search_query)) if (HTTPBearerJWT !== undefined) headers.set('authorization', `Bearer ${HTTPBearerJWT}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/store/my-unpublished-agents${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(myUnpublishedAgentsResponse, await res.json()) };