import {z} from 'zod' export type GetApiSourcesArgs = {type?: ('repository' | 'documentation' | 'research_paper' | 'huggingface_dataset' | 'local_folder' | 'slack' | 'google_drive' | null) | undefined, query?: (string | null) | undefined, status?: (string | null) | undefined, category_id?: (string | null) | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiSourcesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sourceTrustSignals = z.object({trust_level: z.enum(["low", "medium", "high"]).optional(), trust_tier: z.enum(["low", "medium", "high", "verified"]).optional(), scope: z.enum(["private", "organization", "global"]).optional(), is_global: z.boolean().optional(), is_verified: z.boolean().optional(), overlay_available: z.boolean().optional(), annotation_count: z.number().int().optional()}); export const sourceCurationSummary = z.object({trust_signals: sourceTrustSignals.optional(), overlay_summary: z.string().nullable().optional(), recommended_queries: z.array(z.string()).optional()}); export const source = z.object({id: z.string(), type: z.enum(["repository", "documentation", "research_paper", "huggingface_dataset", "local_folder", "slack", "google_drive"]), identifier: z.string().nullable().optional(), display_name: z.string().nullable().optional(), status: z.string().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional(), category_id: z.string().nullable().optional(), is_global: z.boolean().nullable().optional(), global_source_id: z.string().nullable().optional(), global_namespace: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).optional(), curation: sourceCurationSummary.nullable().optional()}); export const paginationInfo = z.object({total: z.number().int(), limit: z.number().int(), offset: z.number().int(), has_more: z.boolean()}); export const sourceListResponse = z.object({items: z.array(source).optional(), pagination: paginationInfo}); 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 Sources * * GET /sources */ export const getApiSources = async (args: GetApiSourcesArgs, options?: GetApiSourcesOptions) => { const {baseUrl = "https://apigcp.trynia.ai/v2", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (args.query != null) query.append("query", String(args.query)) if (args.status != null) query.append("status", String(args.status)) if (args.category_id != null) query.append("category_id", String(args.category_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/sources${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(sourceListResponse, await res.json()) };