import {z} from 'zod' export type WorkforceWorkerInput = {source_type?: string | undefined, agent_id?: (number | null) | undefined, alias?: (string | null) | undefined, assignment_instructions: string, enabled?: boolean | undefined, sort_order?: (number | null) | undefined, canvas_position?: ({[key: string]: unknown} | null) | undefined}; export type WorkforceCreateRequest = {name: string, description?: (string | null) | undefined, manager_agent_id: number, manager_instructions?: (string | null) | undefined, canvas_layout?: ({[key: string]: unknown} | null) | undefined, workers?: Array | undefined}; export type CreateApiApiWorkforcesArgs = {body: WorkforceCreateRequest}; export type CreateApiApiWorkforcesOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiWorkforcesResponse = z.record(z.string(), z.unknown()); 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 Workforce * * POST /api/workforces */ export const createApiApiWorkforces = async (args: CreateApiApiWorkforcesArgs, options: CreateApiApiWorkforcesOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/workforces${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(createApiApiWorkforcesResponse, await res.json()) };