import {z} from 'zod' export type GetApiActionsArgs = {owner_uuid?: string | undefined, status?: (Array<'pending' | 'drafted' | 'sent' | 'dismissed' | 'expired'>) | undefined, cursor?: string | undefined, size?: number | undefined}; export type GetApiActionsOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const proactiveActionKeyQuote = z.object({text: z.string().optional(), speaker: z.string().optional(), source_label: z.string().optional(), source_url: z.string().optional()}); export const proactiveActionEvidence = z.object({label: z.string().optional(), description: z.string().optional(), impact: z.string().optional(), source_label: z.string().optional(), source_url: z.string().optional()}); export const proactiveActionWhy = z.object({rationale: z.string().optional(), key_quote: proactiveActionKeyQuote.optional(), evidence: z.array(proactiveActionEvidence).optional()}); export const proactiveAction = z.object({uuid: z.string(), entity_type: z.string(), entity_id: z.string(), owner_user_uuid: z.string(), action_type: z.string(), title: z.string(), signal_type: z.string(), severity: z.string(), display_group: z.string(), subject: z.string(), draft_body: z.string(), proposed_recipients: z.array(z.string()), proposed_slots: z.array(z.string()), status: z.string(), external_url: z.string(), why: proactiveActionWhy, generated_at: z.string(), updated_at: z.string(), expires_at: z.string()}); export const listProactiveActionsResponse = z.object({data: z.array(proactiveAction).optional(), next_cursor: z.string().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 }; /** * List Actions * * GET /actions */ export const getApiActions = async (args: GetApiActionsArgs, options?: GetApiActionsOptions) => { const {baseUrl = "https://api.attention.tech/v2", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.owner_uuid != null) query.append("owner_uuid", String(args.owner_uuid)) for (const item of args.status ?? []) query.append("status", String(item)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.size != null) query.append("size", String(args.size)) if (APIKeyHeader !== undefined) headers.set("Authorization", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/actions${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(listProactiveActionsResponse, await res.json()) };