import {z} from 'zod' export type GetApiApiV1SessionsSessionIdPrPrNumberFileArgs = {sessionId: string, prNumber: number, path: string, previousPath?: string | undefined, sourceUrl?: string | undefined, commitSha?: string | undefined}; export type GetApiApiV1SessionsSessionIdPrPrNumberFileOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const workspaceFileResponse = z.object({additions: z.number().int(), binary: z.boolean(), compareBaseRef: z.string().optional(), compareBaseSha: z.string().optional(), compareMode: z.enum(["base", "head_fallback"]).optional(), content: z.string(), contentTruncated: z.boolean(), deleted: z.boolean(), deletions: z.number().int(), diff: z.string(), diffTruncated: z.boolean(), editable: z.boolean(), fileFingerprint: z.string(), historical: z.boolean().optional(), imageMediaType: z.string().optional(), path: z.string(), previousPath: z.string().optional(), sessionId: z.string(), size: z.number().int(), status: z.enum(["unmodified", "modified", "added", "deleted", "renamed"]), workspaceVersion: z.string()}); 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 }; /** * Read one file from an associated pull request base-to-head diff * * GET /api/v1/sessions/{sessionId}/pr/{prNumber}/file */ export const getApiApiV1SessionsSessionIdPrPrNumberFile = async (args: GetApiApiV1SessionsSessionIdPrPrNumberFileArgs, options?: GetApiApiV1SessionsSessionIdPrPrNumberFileOptions) => { 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.path != null) query.append("path", String(args.path)) if (args.previousPath != null) query.append("previousPath", String(args.previousPath)) if (args.sourceUrl != null) query.append("sourceUrl", String(args.sourceUrl)) if (args.commitSha != null) query.append("commitSha", String(args.commitSha)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/sessions/${encodeURIComponent(String(args.sessionId))}/pr/${encodeURIComponent(String(args.prNumber))}/file${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(workspaceFileResponse, await res.json()) };