import {z} from 'zod' export type InputBlockType = 'time-series' | 'image' | 'features'; export type ImageInputResizeMode = 'squash' | 'fit-short' | 'fit-long' | 'crop'; export type ImpulseInputBlockDatasetSubset = {includePercentage: number, seed?: number | undefined}; export type ImpulseInputBlock = {id: number, type: InputBlockType, name: string, title: string, axes?: Array | undefined, windowSizeMs?: number | undefined, windowIncreaseMs?: number | undefined, frequencyHz?: number | undefined, classificationWindowIncreaseMs?: number | undefined, padZeros?: boolean | undefined, labelingMethodMultiLabel?: ({type: 'end-of-window' | 'anywhere-in-window' | 'anywhere-in-window-min-percentage' | 'majority-in-window', labels?: Array | undefined, minPercentage?: number | undefined}) | undefined, imageWidth?: number | undefined, imageHeight?: number | undefined, resizeMode?: ImageInputResizeMode | undefined, resizeMethod?: ('lanczos3' | 'nearest') | undefined, cropAnchor?: ('top-left' | 'top-center' | 'top-right' | 'middle-left' | 'middle-center' | 'middle-right' | 'bottom-left' | 'bottom-center' | 'bottom-right') | undefined, createdBy?: string | undefined, createdAt?: string | undefined, datasetSubset?: ImpulseInputBlockDatasetSubset | undefined}; export type ImpulseDspBlock = {id: number, type: string, name: string, axes: Array, title: string, valuesPerAxis?: number | undefined, input?: number | undefined, createdBy?: string | undefined, createdAt?: string | undefined, implementationVersion: number, organization?: {id: number, dspId: number} | undefined, customUrl?: string | undefined, namedAxes?: (Array<{name: string, description?: string | undefined, required?: boolean | undefined, selectedAxis?: string | undefined}>) | undefined}; export type LearnBlockType = 'anomaly' | 'anomaly-gmm' | 'keras' | 'keras-transfer-image' | 'keras-transfer-kws' | 'keras-object-detection' | 'keras-regression' | 'keras-akida' | 'keras-akida-transfer-image' | 'keras-akida-object-detection' | 'keras-visual-anomaly' | 'keras-freeform' | 'keras-self-contained-freeform' | 'keras-anomaly' | 'vlm'; export type ImpulseSpecificGpuTrainingProcessor = 'cpu' | 'gpu'; export type ImpulseLearnBlock = {id: number, type: LearnBlockType, name: string, dsp: Array, title: string, createdBy?: string | undefined, createdAt?: string | undefined, trainingProcessor?: ImpulseSpecificGpuTrainingProcessor | undefined}; export type PostProcessingBlockType = 'object-tracking' | 'sort-object-tracking'; export type ImpulsePostProcessingBlock = {id: number, type: PostProcessingBlockType, name: string, title: string, createdBy?: string | undefined, createdAt?: string | undefined, implementationVersion: number}; export type ImpulseType = 'default' | 'BYOM' | 'VLM' | 'no_training_data'; export type CreateImpulseRequest = {name?: string | undefined, inputBlocks: Array, dspBlocks: Array, learnBlocks: Array, postProcessingBlocks?: Array | undefined, type?: ImpulseType | undefined, datasetVersionId?: number | undefined}; export type CreateApiApiProjectIdImpulseArgs = {projectId: number, impulseId?: number | undefined, body: CreateImpulseRequest}; export type CreateApiApiProjectIdImpulseOptions = {baseUrl?: string | undefined, ApiKeyAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const createImpulseResponse = z.object({success: z.boolean(), error: z.string().optional(), id: z.number().int()}); 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 impulse * * POST /api/{projectId}/impulse * * Also accepts auth: JWTAuthentication, JWTHttpHeaderAuthentication, OAuth2 */ export const createApiApiProjectIdImpulse = async (args: CreateApiApiProjectIdImpulseArgs, options?: CreateApiApiProjectIdImpulseOptions) => { const {baseUrl = "https://studio.edgeimpulse.com/v1", fetch: fetchFn = fetch, ApiKeyAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.impulseId != null) query.append("impulseId", String(args.impulseId)) if (ApiKeyAuthentication !== undefined) headers.set("x-api-key", ApiKeyAuthentication) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.projectId))}/impulse${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(createImpulseResponse, await res.json()) };