import {z} from 'zod' export type GetApiApiProjectIdOptimizeAllBlocksArgs = {projectId: number}; export type GetApiApiProjectIdOptimizeAllBlocksOptions = {baseUrl?: string | undefined, ApiKeyAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const blockType = z.enum(["official", "personal", "enterprise", "community"]); export const dSPGroupItem = z.object({name: z.string(), value: z.string().optional(), defaultValue: z.string(), type: z.string(), help: z.string().optional(), param: z.string(), selectOptions: z.array(z.object({value: z.string().optional(), selected: z.boolean().optional(), optionLabel: z.string().optional(), priority: z.number().optional(), romEstimate: z.number().optional(), needsOps: z.array(z.string()).optional(), needsFeatures: z.array(z.string()).optional()})).optional(), readonly: z.boolean(), shouldShow: z.boolean(), showIf: z.object({parameter: z.string(), operator: z.enum(["eq", "neq"]), value: z.string()}).optional(), invalidText: z.string().optional(), section: z.enum(["advanced", "augmentation", "modelProfiling"]).optional(), multiline: z.boolean().optional(), required: z.boolean(), hint: z.string().optional(), placeholder: z.string().optional(), showClickToSet: z.boolean(), valid: z.array(z.object({})).optional(), items: z.object({}).optional(), properties: z.object({}).optional(), minVal: z.number().optional(), maxVal: z.number().optional()}); export const tunerBlock = z.object({type: z.string(), title: z.string(), blockType: blockType, author: z.string().optional(), description: z.string().optional(), name: z.string().optional(), recommended: z.boolean().optional(), organizationModelId: z.number().optional(), params: z.record(z.string(), dSPGroupItem).optional(), defaultConfig: z.object({}).optional(), variants: z.record(z.string(), z.object({title: z.string().optional(), key: z.string().optional(), regression: z.boolean().optional(), objectDetection: z.boolean().optional(), projectDataType: z.string().optional(), searchSpaceTemplates: z.array(z.object({template: z.string().optional(), group: z.string().optional(), blocks: z.object({input: z.object({type: z.string().optional(), variant: z.string().optional(), params: z.object({}).optional()}).optional(), dsp: z.object({type: z.string().optional(), variant: z.string().optional(), params: z.object({}).optional()}).optional(), learn: z.object({type: z.string().optional(), variant: z.string().optional(), params: z.object({}).optional()}).optional()}).optional()})).optional(), params: z.record(z.string(), z.array(z.object({}))).optional()})).optional()}); export const allBlocksResponse = z.object({success: z.boolean(), error: z.string().optional(), input: z.array(tunerBlock), dsp: z.array(tunerBlock), learn: z.array(tunerBlock)}); 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 impulse blocks * * GET /api/{projectId}/optimize/all-blocks * * Also accepts auth: JWTAuthentication, JWTHttpHeaderAuthentication, OAuth2 */ export const getApiApiProjectIdOptimizeAllBlocks = async (args: GetApiApiProjectIdOptimizeAllBlocksArgs, options?: GetApiApiProjectIdOptimizeAllBlocksOptions) => { const {baseUrl = "https://studio.edgeimpulse.com/v1", fetch: fetchFn = fetch, ApiKeyAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuthentication !== undefined) headers.set("x-api-key", ApiKeyAuthentication) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.projectId))}/optimize/all-blocks${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(allBlocksResponse, await res.json()) };