import {z} from 'zod' export type GetApiAgentIdContainersArgs = {id: number}; export type GetApiAgentIdContainersOptions = {baseUrl?: string | undefined, internalApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAgentIdContainersResponse = z.object({status: z.string().optional(), data: z.array(z.object({id: z.number().optional(), queueDate: z.number().optional(), launchDate: z.number().optional(), launchType: z.string().optional(), launchNumber: z.number().optional(), endDate: z.number().optional(), lastEndMessage: z.string().optional(), lastEndStatus: z.string().optional(), exitCode: z.number().optional()})).optional()}); 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 a list of ended containers for an agent, ordered by date. Useful for listing the last available output logs from an agent. * * GET /agent/{id}/containers */ export const getApiAgentIdContainers = async (args: GetApiAgentIdContainersArgs, options?: GetApiAgentIdContainersOptions) => { const {baseUrl = "https://api.phantombuster.com/api/v2", fetch: fetchFn = fetch, internalApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (internalApiKey !== undefined) headers.set("X-Phantombuster-Key-1", internalApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/agent/${encodeURIComponent(String(args.id))}/containers${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(getApiAgentIdContainersResponse, await res.json()) };