import {z} from 'zod' export type CliReviewRulesDto = {security?: boolean | undefined, performance?: boolean | undefined, style?: boolean | undefined, bestPractices?: boolean | undefined}; export type CliFileInputDto = {path: string, content: string, status: 'added' | 'modified' | 'deleted' | 'renamed', diff: string}; export type CliConfigDto = {severity?: string | undefined, rules?: CliReviewRulesDto | undefined, rulesOnly?: boolean | undefined, fast?: boolean | undefined, files?: Array | undefined}; export type PlatformType = 'INTERNAL' | 'GITHUB' | 'GITLAB' | 'JIRA' | 'SLACK' | 'NOTION' | 'MSTEAMS' | 'DISCORD' | 'AZURE_BOARDS' | 'AZURE_REPOS' | 'KODUS_WEB' | 'BITBUCKET'; export type CliReviewRequestDto = {diff: string, config?: CliConfigDto | undefined, userEmail?: string | undefined, gitRemote?: string | undefined, branch?: string | undefined, commitSha?: string | undefined, inferredPlatform?: PlatformType | undefined, cliVersion?: string | undefined}; export type CreateApiCliReviewArgs = {'x-team-key'?: string | undefined, authorization: string, body: CliReviewRequestDto}; export type CreateApiCliReviewOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const cliReviewIssueFixRangeDto = z.object({start: z.number(), end: z.number()}); export const cliReviewIssueFixDto = z.object({range: cliReviewIssueFixRangeDto, replacement: z.string()}); export const cliReviewIssueDto = z.object({file: z.string(), line: z.number(), endLine: z.number().optional(), severity: z.string(), category: z.string().optional(), message: z.string(), suggestion: z.string().optional(), recommendation: z.string().optional(), ruleId: z.string().optional(), fixable: z.boolean().optional(), fix: cliReviewIssueFixDto.optional()}); export const cliReviewResponseDto = z.object({summary: z.string(), issues: z.array(cliReviewIssueDto), filesAnalyzed: z.number(), duration: z.number()}); 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 }; /** * Run CLI code review * * POST /cli/review */ export const createApiCliReview = async (args: CreateApiCliReviewArgs, options?: CreateApiCliReviewOptions) => { const {baseUrl = "http://localhost:3001", 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['x-team-key'] != null) headers.set("x-team-key", String(args['x-team-key'])) if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/cli/review${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(cliReviewResponseDto, await res.json()) };