import {z} from 'zod' export type GetApiApiV1SessionsSessionIdPrPrNumberFilesArgs = {sessionId: string, prNumber: number, sourceUrl?: string | undefined}; export type GetApiApiV1SessionsSessionIdPrPrNumberFilesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const workspaceFileSummary = z.object({additions: z.number().int(), binary: z.boolean(), deletions: z.number().int(), editable: z.boolean(), fileFingerprint: z.string(), path: z.string(), previousPath: z.string().optional(), size: z.number().int(), status: z.enum(["unmodified", "modified", "added", "deleted", "renamed"])}); export const workspaceCommitSummary = z.object({author: z.string(), files: z.array(workspaceFileSummary), sha: z.string(), subject: z.string(), timestamp: z.string()}); export const workspaceSummary = z.object({additions: z.number().int(), deletions: z.number().int(), files: z.number().int()}); export const listPRFilesResponse = z.object({commits: z.array(workspaceCommitSummary), commitsTruncated: z.boolean().optional(), files: z.array(workspaceFileSummary), sessionId: z.string(), summary: workspaceSummary, truncated: z.boolean()}); 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 the exact base-to-head changed files for an associated pull request * * GET /api/v1/sessions/{sessionId}/pr/{prNumber}/files */ export const getApiApiV1SessionsSessionIdPrPrNumberFiles = async (args: GetApiApiV1SessionsSessionIdPrPrNumberFilesArgs, options?: GetApiApiV1SessionsSessionIdPrPrNumberFilesOptions) => { const {baseUrl = "http://127.0.0.1:3001", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sourceUrl != null) query.append("sourceUrl", String(args.sourceUrl)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/sessions/${encodeURIComponent(String(args.sessionId))}/pr/${encodeURIComponent(String(args.prNumber))}/files${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(listPRFilesResponse, await res.json()) };