import {z} from 'zod' export type GetApiApiActionsArgs = {'filter[parent_segment_id]'?: number | undefined, 'filter[project_id]'?: string | undefined, 'filter[prompt_id]'?: string | undefined, 'filter[service_type]'?: string | undefined, 'filter[skip_managed]'?: ('auto' | 'true' | 'false') | undefined, 'filter[ui_tags]'?: string | undefined, 'page[limit]'?: number | undefined}; export type GetApiApiActionsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiActionsResponse = z.object({data: z.array(z.object({id: z.string(), type: z.string(), links: z.object({self: z.string(), chat: z.string().nullable().optional()}), attributes: z.object({integrationId: z.string(), promptId: z.string(), chatWidgetType: z.string().nullable(), chatWidgetLabel: z.string().nullable(), webhookTextUrl: z.string().nullable(), slackRequestUrl: z.string().nullable(), uiTags: z.array(z.string())})})), meta: z.object({total: z.number().int()}), links: z.object({first: z.string(), last: z.string(), next: z.string().optional(), prev: 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 }; /** * index * * GET /api/actions */ export const getApiApiActions = async (args: GetApiApiActionsArgs, options?: GetApiApiActionsOptions) => { const {baseUrl = "https://llm-api.treasuredata.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args['filter[parent_segment_id]'] != null) query.append("filter[parent_segment_id]", String(args['filter[parent_segment_id]'])) if (args['filter[project_id]'] != null) query.append("filter[project_id]", String(args['filter[project_id]'])) if (args['filter[prompt_id]'] != null) query.append("filter[prompt_id]", String(args['filter[prompt_id]'])) if (args['filter[service_type]'] != null) query.append("filter[service_type]", String(args['filter[service_type]'])) if (args['filter[skip_managed]'] != null) query.append("filter[skip_managed]", String(args['filter[skip_managed]'])) if (args['filter[ui_tags]'] != null) query.append("filter[ui_tags]", String(args['filter[ui_tags]'])) if (args['page[limit]'] != null) query.append("page[limit]", String(args['page[limit]'])) if (ApiKeyAuth !== undefined) headers.set('authorization', `TD1 ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/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(getApiApiActionsResponse, await res.json()) };