import {z} from 'zod' export type CreateApiV1IntentsArgs = {body: {intentType: 'rpc' | 'transfer' | 'wallet_update' | 'policy_update' | 'policy_rule_create' | 'policy_rule_delete' | 'policy_rule_update' | 'quorum_update' | 'wallet_action' | 'provider-action', intent_type?: ('rpc' | 'transfer' | 'wallet_update' | 'policy_update' | 'policy_rule_create' | 'policy_rule_delete' | 'policy_rule_update' | 'quorum_update' | 'wallet_action' | 'provider-action') | undefined, agentId?: (string | null) | undefined, wallet_id?: (string | null) | undefined, resourceType?: (string | null) | undefined, resource_type?: (string | null) | undefined, resourceId?: (string | null) | undefined, resource_id?: (string | null) | undefined, authorizationDetails?: Array<{[key: string]: unknown}> | undefined, authorization_details?: Array<{[key: string]: unknown}> | undefined, payload?: {[key: string]: unknown} | undefined, createdByDisplayName?: (string | null) | undefined, created_by_display_name?: (string | null) | undefined, expiresAt?: (string | null) | undefined, expires_at?: (string | null) | undefined, ttlSeconds?: number | undefined, ttl_seconds?: number | undefined} | {[key: string]: unknown} | {intentType?: ('rpc' | 'transfer' | 'wallet_update' | 'policy_update' | 'policy_rule_create' | 'policy_rule_delete' | 'policy_rule_update' | 'quorum_update' | 'wallet_action' | 'provider-action') | undefined, intent_type: 'rpc' | 'transfer' | 'wallet_update' | 'policy_update' | 'policy_rule_create' | 'policy_rule_delete' | 'policy_rule_update' | 'quorum_update' | 'wallet_action' | 'provider-action', agentId?: (string | null) | undefined, wallet_id?: (string | null) | undefined, resourceType?: (string | null) | undefined, resource_type?: (string | null) | undefined, resourceId?: (string | null) | undefined, resource_id?: (string | null) | undefined, authorizationDetails?: Array<{[key: string]: unknown}> | undefined, authorization_details?: Array<{[key: string]: unknown}> | undefined, payload?: {[key: string]: unknown} | undefined, createdByDisplayName?: (string | null) | undefined, created_by_display_name?: (string | null) | undefined, expiresAt?: (string | null) | undefined, expires_at?: (string | null) | undefined, ttlSeconds?: number | undefined, ttl_seconds?: number | undefined} | {[key: string]: unknown}}; export type CreateApiV1IntentsOptions = {baseUrl?: string | undefined, tenantApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1IntentsResponse = z.object({ok: z.literal(true), data: z.object({id: z.string(), intent_id: z.string(), tenantId: z.string().optional(), agentId: z.string().nullable().optional(), wallet_id: z.string().nullable().optional(), intentType: z.enum(["rpc", "transfer", "wallet_update", "policy_update", "policy_rule_create", "policy_rule_delete", "policy_rule_update", "quorum_update", "wallet_action", "provider-action"]), intent_type: z.enum(["rpc", "transfer", "wallet_update", "policy_update", "policy_rule_create", "policy_rule_delete", "policy_rule_update", "quorum_update", "wallet_action", "provider-action"]), status: z.enum(["pending", "authorized", "executing", "executed", "failed", "rejected", "canceled", "expired"]), resourceType: z.string().nullable().optional(), resourceId: z.string().nullable().optional(), resource_id: z.string().nullable().optional(), createdByType: z.string().optional(), createdById: z.string().nullable().optional(), created_by_id: z.string().nullable().optional(), createdByDisplayName: z.string().nullable().optional(), created_by_display_name: z.string().nullable().optional(), authorizationDetails: z.array(z.record(z.string(), z.unknown())).optional(), authorization_details: z.array(z.record(z.string(), z.unknown())).optional(), payload: z.record(z.string(), z.unknown()), executionResult: z.record(z.string(), z.unknown()).nullable().optional(), execution_result: z.record(z.string(), z.unknown()).nullable().optional(), expiresAt: z.string().nullable().optional(), expires_at: z.number().int().nullable().optional(), authorizedBy: z.string().nullable().optional(), authorized_by: z.string().nullable().optional(), authorizedAt: z.string().nullable().optional(), executedAt: z.string().nullable().optional(), createdAt: z.string(), created_at: z.number().int().optional(), updatedAt: 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 an intent * * POST /v1/intents * * Also accepts auth: bearerAuth */ export const createApiV1Intents = async (args: CreateApiV1IntentsArgs, options?: CreateApiV1IntentsOptions) => { const {baseUrl = "http://localhost:3200", fetch: fetchFn = fetch, tenantApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (tenantApiKey !== undefined) headers.set("X-Steward-API-Key", tenantApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/intents${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(createApiV1IntentsResponse, await res.json()) };