import {z} from 'zod' export type UpdateApiV2510BetaAgenticLogicSourcesNameArgs = {name: string, body: {description: string, definition: {kind: 'bestSellers', args: {timeWindow?: ('24h' | '7d' | '30d' | '90d') | undefined, metric?: ('units' | 'revenue') | undefined, scope?: ({collectionIds?: Array | undefined, tags?: Array | undefined}) | undefined, limit: number, excludeOutOfStock?: boolean | undefined}} | {kind: 'newArrivals', args: {timeWindow?: ('24h' | '7d' | '30d' | '90d') | undefined, dateField?: ('created' | 'published') | undefined, scope?: ({collectionIds?: Array | undefined, tags?: Array | undefined}) | undefined, limit: number, excludeOutOfStock?: boolean | undefined}} | {kind: 'inStock', args: {minQuantity?: number | undefined, scope?: ({collectionIds?: Array | undefined, tags?: Array | undefined}) | undefined, orderBy?: ({field: 'bestSelling' | 'price' | 'createdAt' | 'title' | 'relevance' | 'inventoryTotal', direction?: ('asc' | 'desc') | undefined}) | undefined, limit: number}} | {kind: 'collectionProducts', args: {orderBy?: ({field: 'bestSelling' | 'price' | 'createdAt' | 'title' | 'relevance', direction?: ('asc' | 'desc') | undefined}) | undefined, limit: number, include?: ({collectionIds?: (boolean | {first: number}) | undefined, metafields?: Array<{namespace: string, key: string}> | undefined}) | undefined, collectionId: string}} | {kind: 'storefrontGraphql', args: {gql: string, variables: {[key: string]: unknown}}} | {kind: 'adminGraphql', args: {gql: string, variables: {[key: string]: unknown}}} | {kind: 'script', args: {workspace: {entry?: 'source.ts' | undefined, files: {[key: string]: string}}, outputSchema?: {[key: string]: unknown} | undefined}}, freshMsOverride?: number | undefined}}; export type UpdateApiV2510BetaAgenticLogicSourcesNameOptions = {baseUrl?: string | undefined, intelligemsAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const updateApiV2510BetaAgenticLogicSourcesNameResponse = 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()}), warnings: z.array(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 }; /** * Create or replace a Source * * PUT /v25-10-beta/agentic-logic/sources/{name} */ export const updateApiV2510BetaAgenticLogicSourcesName = async (args: UpdateApiV2510BetaAgenticLogicSourcesNameArgs, options?: UpdateApiV2510BetaAgenticLogicSourcesNameOptions) => { const {baseUrl = "https://api.intelligems.io", fetch: fetchFn = fetch, intelligemsAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiV2510BetaAgenticLogicSourcesNameResponse, await res.json()) };