import {z} from 'zod' export type CreateApiAgentsSaveArgs = {'X-Phantombuster-Org'?: string | undefined, body: {id?: string | undefined, org?: (string | null) | undefined, script?: (string | null) | undefined, branch?: (string | null) | undefined, environment?: ('staging' | 'release' | null) | undefined, name?: (string | null) | undefined, executionTimeLimit?: (number | null) | undefined, fileMgmt?: ('folders' | 'mix' | 'delete' | null) | undefined, fileMgmtMaxFolders?: (number | null) | undefined, maxParallelism?: (number | null) | undefined, maxRetryNumber?: (number | null) | undefined, launchType?: ('manually' | 'repeatedly' | 'once' | 'after agent') | undefined, launchOnceAt?: (number | null) | undefined, launchAfterAgentId?: (string | null) | undefined, repeatedLaunchTimes?: ({simplePreset?: ('Once every other day' | 'Once per day' | 'Once per day, at the start of the day' | 'Twice per day' | 'Thrice per day' | '4 times per day' | '6 times per day' | '8 times per day' | 'Once every other hour' | 'Once per hour' | 'Twice per hour' | 'Thrice per hour' | '4 times per hour' | 'Once every other working hour' | 'Once per working hour' | 'Twice per working hour' | 'Thrice per working hour' | '4 times per working hour' | 'Once every other working hour, excluding weekends' | 'Once per working hour, excluding weekends' | 'Twice per working hour, excluding weekends' | 'Thrice per working hour, excluding weekends' | '4 times per working hour, excluding weekends' | null) | undefined, isSimplePresetEnabled?: boolean | undefined, timezone: string, minute: Array, hour: Array, day: Array, dow: Array<'sun' | 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat'>, month: Array<'jan' | 'feb' | 'mar' | 'apr' | 'may' | 'jun' | 'jul' | 'aug' | 'sep' | 'oct' | 'nov' | 'dec'>} | null) | undefined, notifications?: ({mailAutomaticExitSuccess?: boolean | undefined, mailAutomaticExitError?: boolean | undefined, mailAutomaticLaunchError?: boolean | undefined, mailAutomaticTimeError?: boolean | undefined, mailManualExitSuccess?: boolean | undefined, mailManualExitError?: boolean | undefined, mailManualLaunchError?: boolean | undefined, mailManualTimeError?: boolean | undefined, slackWebHook?: string | undefined, slackAutomaticExitSuccess?: boolean | undefined, slackAutomaticExitError?: boolean | undefined, slackAutomaticLaunchError?: boolean | undefined, slackAutomaticTimeError?: boolean | undefined, slackManualExitSuccess?: boolean | undefined, slackManualExitError?: boolean | undefined, slackManualLaunchError?: boolean | undefined, slackManualTimeError?: boolean | undefined, webhook?: string | undefined} | null) | undefined, proxyType?: ('none' | 'http' | 'pool' | 'squid lease') | undefined, proxyAddress?: (string | null) | undefined, proxyUsername?: (string | null) | undefined, proxyPassword?: (string | null) | undefined, argument?: (string | null | Record | null) | undefined, agentObject?: (string | null) | undefined, repeatedLaunchPreset?: ('Once every other day' | 'Once per day' | 'Once per day, at the start of the day' | 'Twice per day' | 'Thrice per day' | '4 times per day' | '6 times per day' | '8 times per day' | 'Once every other hour' | 'Once per hour' | 'Twice per hour' | 'Thrice per hour' | '4 times per hour' | 'Once every other working hour' | 'Once per working hour' | 'Twice per working hour' | 'Thrice per working hour' | '4 times per working hour' | 'Once every other working hour, excluding weekends' | 'Once per working hour, excluding weekends' | 'Twice per working hour, excluding weekends' | 'Thrice per working hour, excluding weekends' | '4 times per working hour, excluding weekends' | null) | undefined, applyScriptManifestDefaultSettings?: boolean | undefined, masterAgentLaunchAfter?: (number | null) | undefined, masterAgentLaunchOnExitCodes?: (Array | null | 'all' | null) | undefined, squidLeaseIdentifier?: ({groupSlug: string, refreshRate: number, location?: (string | null) | undefined, sublocation?: (string | null) | undefined} | null) | undefined, persistedVolumeKey?: (string | null) | undefined, wasSetupValidWhenSubmittedByTheFrontend?: boolean | undefined, shouldPropagateUpdatedSettingsToWorkersRegardlessOfSessionType?: boolean | undefined, expireAt?: (number | null) | undefined, idempotencyKey?: (string | null) | undefined, internalMetadata?: ({[key: string]: unknown} | null) | undefined, userCustomMetadata?: ({[key: string]: unknown} | null) | undefined}}; export type CreateApiAgentsSaveOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiAgentsSaveResponse = z.object({id: z.string()}); 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 }; /** * Updates or creates a new agent. If an id is provided, the corresponding agent will be updated. Otherwise, a new agent will be created. * * POST /agents/save */ export const createApiAgentsSave = async (args: CreateApiAgentsSaveArgs, options?: CreateApiAgentsSaveOptions) => { const {baseUrl = "https://api.phantombuster.com/api/v2", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Phantombuster-Org'] != null) headers.set("X-Phantombuster-Org", String(args['X-Phantombuster-Org'])) if (apiKeyAuth !== undefined) headers.set("X-Phantombuster-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/agents/save${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(createApiAgentsSaveResponse, await res.json()) };