import {z} from 'zod' export type GetApiApiAgentSourcesSidFileLinkArgs = {sid: string}; export type GetApiApiAgentSourcesSidFileLinkOptions = {baseUrl?: string | undefined, BearerJwt?: string | undefined, TeamSid?: string | undefined, fetch?: typeof fetch | undefined}; export const getAgentSourceFileLinkResponse = z.object({success: z.literal(true), operation: z.literal("action"), action: z.string(), item: z.object({sid: z.string(), team_sid: z.string(), agent_sid: z.string(), kind: z.enum(["file", "website"]), title: z.string(), status: z.enum(["reading", "read", "failed"]), instruction: z.string().nullable(), file: z.object({name: z.string(), format: z.enum(["pdf", "docx", "txt", "md"]), mime: z.string(), bytes: z.number().int().gt(0), pages: z.number().int().gt(0).nullable()}).nullable(), website: z.object({url: z.string(), scope: z.string(), refresh: z.enum(["weekly", "daily"]), pages: z.number().int().gte(0).nullable()}).nullable(), progress: z.object({done: z.number().int().gte(0), total: z.number().int().gte(0)}), failure: z.object({reason: z.enum(["unreadable", "no_text", "too_large", "website_unreachable", "nothing_found", "model_failed"]), message: z.string()}).nullable(), read_at: z.string().nullable(), next_refresh_at: z.string().nullable(), created_by: z.object({actor_type: z.enum(["user", "support", "api_key", "system", "agent"]), actor_sid: z.string().nullable(), team_sid: z.string(), actor_name: z.string().nullable().optional(), oauth_client_sid: z.string().nullable().optional(), reason: z.string().nullable().optional(), permissions: z.record(z.string(), z.unknown()), cluster_id: z.number().int().nullable().optional(), trace_id: z.string().nullable().optional()}), created_at: z.string(), updated_at: z.string()}).nullable(), result: z.object({download_url: z.string(), expires_at: z.string()}), meta: z.object({trace_id: z.string(), span_id: z.string(), timestamp: z.string(), duration_ms: z.number().int().gte(0), team_sid: z.string().nullable().optional(), actor_type: z.string().nullable().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 }; /** * Get a Knowledge file link * * GET /api/agent-sources/{sid}/file-link */ export const getApiApiAgentSourcesSidFileLink = async (args: GetApiApiAgentSourcesSidFileLinkArgs, options?: GetApiApiAgentSourcesSidFileLinkOptions) => { const {baseUrl = "https://beta.getsales.io/unibox/v1", fetch: fetchFn = fetch, BearerJwt, TeamSid} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerJwt !== undefined) headers.set('authorization', `Bearer ${BearerJwt}`) if (TeamSid !== undefined) headers.set("Team-SID", TeamSid) const url = `${baseUrl.replace(/\/+$/, '')}/api/agent-sources/${encodeURIComponent(String(args.sid))}/file-link${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(getAgentSourceFileLinkResponse, await res.json()) };