import {z} from 'zod' export type CreateApiV2510BetaAgenticLogicSourcesNameRefreshArgs = {name: string}; export type CreateApiV2510BetaAgenticLogicSourcesNameRefreshOptions = {baseUrl?: string | undefined, intelligemsAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2510BetaAgenticLogicSourcesNameRefreshResponse = z.union([z.object({source: z.object({name: z.string().max(128), description: z.string().min(1).max(2048), definition: z.union([z.object({kind: z.literal("bestSellers"), args: z.object({timeWindow: z.enum(["24h", "7d", "30d", "90d"]).optional(), metric: z.enum(["units", "revenue"]).optional(), scope: z.object({collectionIds: z.array(z.string().min(1)).optional(), tags: z.array(z.string().min(1)).optional()}).optional(), limit: z.number().int().gt(0).lte(250), excludeOutOfStock: z.boolean().optional()})}), z.object({kind: z.literal("newArrivals"), args: z.object({timeWindow: z.enum(["24h", "7d", "30d", "90d"]).optional(), dateField: z.enum(["created", "published"]).optional(), scope: z.object({collectionIds: z.array(z.string().min(1)).optional(), tags: z.array(z.string().min(1)).optional()}).optional(), limit: z.number().int().gt(0).lte(250), excludeOutOfStock: z.boolean().optional()})}), z.object({kind: z.literal("inStock"), args: z.object({minQuantity: z.number().int().gt(0).lte(9007199254740991).optional(), scope: z.object({collectionIds: z.array(z.string().min(1)).optional(), tags: z.array(z.string().min(1)).optional()}).optional(), orderBy: z.object({field: z.enum(["bestSelling", "price", "createdAt", "title", "relevance", "inventoryTotal"]), direction: z.enum(["asc", "desc"]).optional()}).optional(), limit: z.number().int().gt(0).lte(250)})}), z.object({kind: z.literal("collectionProducts"), args: z.object({orderBy: z.object({field: z.enum(["bestSelling", "price", "createdAt", "title", "relevance"]), direction: z.enum(["asc", "desc"]).optional()}).optional(), limit: z.number().int().gt(0).lte(250), include: z.object({collectionIds: z.union([z.boolean(), z.object({first: z.number().int().gt(0).lte(250)})]).optional(), metafields: z.array(z.object({namespace: z.string(), key: z.string()})).optional()}).optional(), collectionId: z.string().min(1)})}), z.object({kind: z.literal("storefrontGraphql"), args: z.object({gql: z.string().min(1).max(10000), variables: z.record(z.string(), z.unknown())})}), z.object({kind: z.literal("adminGraphql"), args: z.object({gql: z.string().min(1).max(10000), variables: z.record(z.string(), z.unknown())})}), z.object({kind: z.literal("script"), args: z.object({workspace: z.object({entry: z.literal("source.ts"), files: z.record(z.string(), z.string())}), outputSchema: z.record(z.string(), z.unknown()).optional()})})]), summary: z.string(), freshMs: z.number().int().gt(0).lte(9007199254740991), freshMsOverride: z.number().int().gt(0).lte(9007199254740991).nullable(), status: z.enum(["warming", "ready", "stale", "error"]), fetchedAt: z.number().nullable(), lastRefreshedAt: z.number().nullable(), lastRefreshStatus: z.enum(["ok", "error"]).nullable(), lastError: z.string().nullable(), lastBuildLog: z.string().nullable(), lastReadAt: z.number().nullable(), lastUpdateTs: z.number()}), queued: z.literal(true)}), z.object({source: z.object({name: z.string().max(128), description: z.string().min(1).max(2048), definition: z.union([z.object({kind: z.literal("bestSellers"), args: z.object({timeWindow: z.enum(["24h", "7d", "30d", "90d"]).optional(), metric: z.enum(["units", "revenue"]).optional(), scope: z.object({collectionIds: z.array(z.string().min(1)).optional(), tags: z.array(z.string().min(1)).optional()}).optional(), limit: z.number().int().gt(0).lte(250), excludeOutOfStock: z.boolean().optional()})}), z.object({kind: z.literal("newArrivals"), args: z.object({timeWindow: z.enum(["24h", "7d", "30d", "90d"]).optional(), dateField: z.enum(["created", "published"]).optional(), scope: z.object({collectionIds: z.array(z.string().min(1)).optional(), tags: z.array(z.string().min(1)).optional()}).optional(), limit: z.number().int().gt(0).lte(250), excludeOutOfStock: z.boolean().optional()})}), z.object({kind: z.literal("inStock"), args: z.object({minQuantity: z.number().int().gt(0).lte(9007199254740991).optional(), scope: z.object({collectionIds: z.array(z.string().min(1)).optional(), tags: z.array(z.string().min(1)).optional()}).optional(), orderBy: z.object({field: z.enum(["bestSelling", "price", "createdAt", "title", "relevance", "inventoryTotal"]), direction: z.enum(["asc", "desc"]).optional()}).optional(), limit: z.number().int().gt(0).lte(250)})}), z.object({kind: z.literal("collectionProducts"), args: z.object({orderBy: z.object({field: z.enum(["bestSelling", "price", "createdAt", "title", "relevance"]), direction: z.enum(["asc", "desc"]).optional()}).optional(), limit: z.number().int().gt(0).lte(250), include: z.object({collectionIds: z.union([z.boolean(), z.object({first: z.number().int().gt(0).lte(250)})]).optional(), metafields: z.array(z.object({namespace: z.string(), key: z.string()})).optional()}).optional(), collectionId: z.string().min(1)})}), z.object({kind: z.literal("storefrontGraphql"), args: z.object({gql: z.string().min(1).max(10000), variables: z.record(z.string(), z.unknown())})}), z.object({kind: z.literal("adminGraphql"), args: z.object({gql: z.string().min(1).max(10000), variables: z.record(z.string(), z.unknown())})}), z.object({kind: z.literal("script"), args: z.object({workspace: z.object({entry: z.literal("source.ts"), files: z.record(z.string(), z.string())}), outputSchema: z.record(z.string(), z.unknown()).optional()})})]), summary: z.string(), freshMs: z.number().int().gt(0).lte(9007199254740991), freshMsOverride: z.number().int().gt(0).lte(9007199254740991).nullable(), status: z.enum(["warming", "ready", "stale", "error"]), fetchedAt: z.number().nullable(), lastRefreshedAt: z.number().nullable(), lastRefreshStatus: z.enum(["ok", "error"]).nullable(), lastError: z.string().nullable(), lastBuildLog: z.string().nullable(), lastReadAt: z.number().nullable(), lastUpdateTs: z.number()}), queued: z.literal(false), notQueuedReason: z.enum(["in_flight", "no_market", "admin_budget", "cooldown"])})]); 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 }; /** * Refresh a Source * * POST /v25-10-beta/agentic-logic/sources/{name}/refresh */ export const createApiV2510BetaAgenticLogicSourcesNameRefresh = async (args: CreateApiV2510BetaAgenticLogicSourcesNameRefreshArgs, options?: CreateApiV2510BetaAgenticLogicSourcesNameRefreshOptions) => { const {baseUrl = "https://api.intelligems.io", fetch: fetchFn = fetch, intelligemsAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (intelligemsAccessToken !== undefined) headers.set("intelligems-access-token", intelligemsAccessToken) const url = `${baseUrl.replace(/\/+$/, '')}/v25-10-beta/agentic-logic/sources/${encodeURIComponent(String(args.name))}/refresh${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV2510BetaAgenticLogicSourcesNameRefreshResponse, await res.json()) };