import {z} from 'zod' export type GetApiApiV1ToolsArgs = {'filter.summary'?: boolean | undefined, 'filter.provider'?: string | undefined, 'filter.identifier'?: string | undefined, 'filter.tool_name'?: Array | undefined, 'filter.query'?: string | undefined, 'filter.connector'?: string | undefined, 'filter.organization_id'?: string | undefined, 'filter.user_id'?: string | undefined, 'filter.connected_account_id'?: string | undefined, page_size?: number | undefined, page_token?: string | undefined}; export type GetApiApiV1ToolsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const toolsTool = z.object({definition: z.object({}).optional(), id: z.string().optional(), is_default: z.boolean().optional(), metadata: z.object({}).optional(), provider: z.string().optional(), tags: z.array(z.string()).optional(), updated_at: z.string().optional()}); export const toolsListToolsResponse = z.object({next_page_token: z.string().optional(), prev_page_token: z.string().optional(), tool_names: z.array(z.string()).optional(), tools: z.array(toolsTool).optional(), total_size: z.number().int().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 tools * * GET /api/v1/tools */ export const getApiApiV1Tools = async (args: GetApiApiV1ToolsArgs, options?: GetApiApiV1ToolsOptions) => { const {baseUrl = "https://$SCALEKIT_ENVIRONMENT_URL", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['filter.summary'] != null) query.append("filter.summary", String(args['filter.summary'])) if (args['filter.provider'] != null) query.append("filter.provider", String(args['filter.provider'])) if (args['filter.identifier'] != null) query.append("filter.identifier", String(args['filter.identifier'])) for (const item of args['filter.tool_name'] ?? []) query.append("filter.tool_name", String(item)) if (args['filter.query'] != null) query.append("filter.query", String(args['filter.query'])) if (args['filter.connector'] != null) query.append("filter.connector", String(args['filter.connector'])) if (args['filter.organization_id'] != null) query.append("filter.organization_id", String(args['filter.organization_id'])) if (args['filter.user_id'] != null) query.append("filter.user_id", String(args['filter.user_id'])) if (args['filter.connected_account_id'] != null) query.append("filter.connected_account_id", String(args['filter.connected_account_id'])) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.page_token != null) query.append("page_token", String(args.page_token)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/tools${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(toolsListToolsResponse, await res.json()) };