All files / src/entities/files schema-readonly.ts

100% Statements 13/13
100% Branches 0/0
100% Functions 0/0
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 9832x 32x           32x                         32x               32x   32x         32x                               32x 32x     32x                   32x               32x               32x                                
import { z } from 'zod';
import { flexibleBoolean, requiredId, paginationFields } from '../utils';
 
// ============================================================================
// Response schemas for GitLab file API responses
// ============================================================================
 
export const GitLabFileContentSchema = z.object({
  file_name: z.string(),
  file_path: z.string(),
  size: z.number(),
  encoding: z.string(),
  content_sha256: z.string().optional(),
  ref: z.string().optional(),
  blob_id: z.string(),
  commit_id: z.string(),
  last_commit_id: z.string(),
  content: z.string().optional(),
});
 
export const GitLabDirectoryContentSchema = z.object({
  id: z.string(),
  name: z.string(),
  type: z.enum(['tree', 'blob']),
  path: z.string(),
  mode: z.string(),
});
 
export const GitLabContentSchema = z.union([GitLabFileContentSchema, GitLabDirectoryContentSchema]);
 
export const GitLabCreateUpdateFileResponseSchema = z.object({
  file_path: z.string(),
  branch: z.string(),
});
 
export const GitLabTreeSchema = z.object({
  id: z.string(),
  name: z.string(),
  type: z.enum(['tree', 'blob']),
  path: z.string(),
  mode: z.string(),
});
 
// ============================================================================
// browse_files - CQRS Query Tool (discriminated union schema)
// Actions: tree, content
// Uses z.discriminatedUnion() for type-safe action handling.
// Schema pipeline flattens to flat JSON Schema for AI clients that don't support oneOf.
// ============================================================================
 
// --- Shared fields ---
const projectIdField = requiredId.describe('Project ID or URL-encoded path');
const refField = z.string().optional().describe('Branch, tag, or commit SHA');
 
// --- Action: tree ---
const TreeActionSchema = z.object({
  action: z.literal('tree').describe('List files and folders in a directory'),
  project_id: projectIdField,
  ref: refField,
  path: z.string().optional().describe('Directory path to list'),
  recursive: flexibleBoolean.optional().describe('Include nested directories'),
  ...paginationFields(),
});
 
// --- Action: content ---
const ContentActionSchema = z.object({
  action: z.literal('content').describe('Read file contents'),
  project_id: projectIdField,
  ref: refField,
  file_path: z.string().describe('Path to the file to read'),
});
 
// --- Action: download_attachment ---
const DownloadAttachmentActionSchema = z.object({
  action: z.literal('download_attachment').describe('Download a file attachment from issues/MRs'),
  project_id: projectIdField,
  secret: z.string().describe('Security token from the attachment URL.'),
  filename: z.string().describe('Original filename of the attachment.'),
});
 
// --- Discriminated union combining all actions ---
export const BrowseFilesSchema = z.discriminatedUnion('action', [
  TreeActionSchema,
  ContentActionSchema,
  DownloadAttachmentActionSchema,
]);
 
// ============================================================================
// Type exports
// ============================================================================
 
export type GitLabFileContent = z.infer<typeof GitLabFileContentSchema>;
export type GitLabDirectoryContent = z.infer<typeof GitLabDirectoryContentSchema>;
export type GitLabContent = z.infer<typeof GitLabContentSchema>;
export type GitLabCreateUpdateFileResponse = z.infer<typeof GitLabCreateUpdateFileResponseSchema>;
export type GitLabTree = z.infer<typeof GitLabTreeSchema>;
export type BrowseFilesInput = z.infer<typeof BrowseFilesSchema>;