import {z} from 'zod' export type AdapterType = 'github' | 'slack' | 'uploaded_file'; export type SourceVisibility = 'private' | 'public'; export type GitHubSourceConfig = {repo: string, branch: string}; export type SlackSourceConfig = {slack_team_id: string, channel_id: string}; export type UploadedFileSourceConfig = {file_id: string}; export type GitHubSourceMetadata = {installation_id: number, commit_sha: string}; export type SlackSourceMetadata = {team_name?: (string | null) | undefined, channel_name: string, is_private?: boolean | undefined}; export type UploadedFileSourceMetadata = {filename: string, content_type?: (string | null) | undefined, size_bytes?: (number | null) | undefined}; export type ConnectedSource = {id?: (string | null) | undefined, linked_by_user_id: string, adapter: AdapterType, visibility?: SourceVisibility | undefined, config: GitHubSourceConfig | SlackSourceConfig | UploadedFileSourceConfig, metadata: GitHubSourceMetadata | SlackSourceMetadata | UploadedFileSourceMetadata}; export type CreateApiAgentLoopSyncChatConnectedSourcesRetrievalDetailsArgs = {authorization?: (string | null) | undefined, body: ConnectedSource}; export type CreateApiAgentLoopSyncChatConnectedSourcesRetrievalDetailsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const adapterType = z.enum(["github", "slack", "uploaded_file"]); export const retrievalKind = z.enum(["git", "file"]); export const gitRetrievalDetails = z.object({kind: retrievalKind.optional(), clone_url: z.string(), token: z.string(), repo: z.string(), branch: z.string(), commit_sha: z.string()}); export const fileRetrievalDetails = z.object({kind: retrievalKind.optional(), download_url: z.string(), filename: z.string(), content_type: z.string().nullable().optional(), size_bytes: z.number().int().nullable().optional()}); export const retrievalResult = z.object({adapter: adapterType, retrieval: z.union([gitRetrievalDetails, fileRetrievalDetails])}); 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 }; /** * Sync Chat Connected Source Retrieval Details * * POST /agent-loop/sync-chat/connected-sources/retrieval-details */ export const createApiAgentLoopSyncChatConnectedSourcesRetrievalDetails = async (args: CreateApiAgentLoopSyncChatConnectedSourcesRetrievalDetailsArgs, options: CreateApiAgentLoopSyncChatConnectedSourcesRetrievalDetailsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/agent-loop/sync-chat/connected-sources/retrieval-details${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(retrievalResult, await res.json()) };