import {z} from 'zod' export type PromptConfig = {model?: (string | null) | undefined, params: unknown, responseFormat: unknown, tools: unknown}; export type CreateApiApiOrgIdPromptsMatchArgs = {org_id: string, folder?: string | undefined, body: {config?: PromptConfig | undefined, payload: unknown, type: string}}; export type CreateApiApiOrgIdPromptsMatchOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const promptMatchResponseBody = z.object({contentHash: z.string(), entityId: z.string(), id: z.string(), name: z.string(), version: z.number().int()}); export const createApiApiOrgIdPromptsMatchResponse = z.object({matches: z.array(promptMatchResponseBody)}); 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 }; /** * POST /api/{org_id}/prompts/match */ export const createApiApiOrgIdPromptsMatch = async (args: CreateApiApiOrgIdPromptsMatchArgs, options: CreateApiApiOrgIdPromptsMatchOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.folder != null) query.append("folder", String(args.folder)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/prompts/match${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(createApiApiOrgIdPromptsMatchResponse, await res.json()) };