import {z} from 'zod' export type PoolHealthStatus = 'healthy' | 'partial' | 'inactive'; export type RunnerPoolMatchFilters = {name?: (string | Array) | undefined, status?: (PoolHealthStatus | Array) | undefined, labels?: (string | Array) | undefined}; export type TimestampFilters = {before?: string | undefined, after?: string | undefined}; export type RunnerPoolFilters = {match?: RunnerPoolMatchFilters | undefined, created?: TimestampFilters | undefined, updated?: TimestampFilters | undefined}; export type RunnerPoolSort = {field: 'name' | 'createdAt' | 'updatedAt' | 'runnersCount' | 'assignedProjectsCount' | 'assignedWorkspaceCount' | 'poolStatus', direction: 'asc' | 'desc'}; export type SearchRunnerPoolsRequest = {text?: string | undefined, filters?: RunnerPoolFilters | undefined, sort?: Array | undefined}; export type CreateApiRunnersRunnerPoolsSearchArgs = {pageSize?: number | undefined, offset?: number | undefined, body: SearchRunnerPoolsRequest}; export type CreateApiRunnersRunnerPoolsSearchOptions = {baseUrl?: string | undefined, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const poolHealthStatus = z.enum(["healthy", "partial", "inactive"]); export const runnerHealthStatus = z.enum(["active", "awaiting_first_heartbeat", "timeout"]); export const poolHealth = z.object({poolStatus: poolHealthStatus.optional(), statusByRunner: z.record(z.string(), runnerHealthStatus).optional()}); export const runnerPool = z.object({id: z.string().optional(), name: z.string().optional(), accountId: z.string().optional(), labels: z.array(z.string()).optional(), description: z.string().optional(), runnersCount: z.number().int().gte(0).optional(), assignedProjectsCount: z.number().int().gte(0).optional(), assignedWorkspaceCount: z.number().int().gte(0).optional(), assignedProjects: z.array(z.object({id: z.string().optional(), name: z.string().optional()})).optional(), assignedWorkspaces: z.array(z.object({id: z.string().optional(), name: z.string().optional()})).optional(), health: poolHealth.optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const createApiRunnersRunnerPoolsSearchResponse = z.array(runnerPool); 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 }; /** * Search runner pools * * POST /runners/runner-pools/search */ export const createApiRunnersRunnerPoolsSearch = async (args: CreateApiRunnersRunnerPoolsSearchArgs, options?: CreateApiRunnersRunnerPoolsSearchOptions) => { const {baseUrl = "https://api.firefly.ai/v2", fetch: fetchFn = fetch, JWTAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.offset != null) query.append("offset", String(args.offset)) if (JWTAuth !== undefined) headers.set('authorization', `Bearer ${JWTAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/runners/runner-pools/search${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(createApiRunnersRunnerPoolsSearchResponse, await res.json()) };