import {z} from 'zod' export type GetApiApiLibraryPresetsPresetIdArgs = {preset_id: string}; export type GetApiApiLibraryPresetsPresetIdOptions = {baseUrl: string, HTTPBearerJWT?: string | undefined, fetch?: typeof fetch | undefined}; export const credentialsMetaInput = z.object({id: z.string(), title: z.string().nullable().optional(), provider: z.string(), type: z.enum(["api_key", "oauth2", "user_password", "host_scoped", "device_code"])}); export const webhookWithoutSecrets = z.object({id: z.string().optional(), user_id: z.string(), provider: z.string(), credentials_id: z.string(), webhook_type: z.string(), resource: z.string(), events: z.array(z.string()), config: z.record(z.string(), z.unknown()).optional(), organization_id: z.string().nullable().optional(), team_id: z.string().nullable().optional(), url: z.string()}); export const libraryAgentPreset = z.object({graph_id: z.string(), graph_version: z.number().int(), inputs: z.record(z.string(), z.unknown()), credentials: z.record(z.string(), credentialsMetaInput), name: z.string(), description: z.string(), is_active: z.boolean().optional(), id: z.string(), user_id: z.string(), created_at: z.string(), updated_at: z.string(), organization_id: z.string().nullable().optional(), team_id: z.string().nullable().optional(), webhook_id: z.string().nullable().optional(), webhook: webhookWithoutSecrets.nullable(), expert_id: z.string().nullable().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 a specific preset * * GET /api/library/presets/{preset_id} */ export const getApiApiLibraryPresetsPresetId = async (args: GetApiApiLibraryPresetsPresetIdArgs, options: GetApiApiLibraryPresetsPresetIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearerJWT} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HTTPBearerJWT !== undefined) headers.set('authorization', `Bearer ${HTTPBearerJWT}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/library/presets/${encodeURIComponent(String(args.preset_id))}${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(libraryAgentPreset, await res.json()) };