All files / src/workspace workspace.controller.ts

68.42% Statements 13/19
100% Branches 0/0
20% Functions 1/5
64.7% Lines 11/17

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 98 99 100 101 102 103 1046x               6x 6x 6x       6x                                 6x 6x                         6x                       6x                             6x                                               6x              
import {
  Controller,
  Get,
  Query,
  Post,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiQuery } from '@nestjs/swagger';
import { WorkspaceService } from './workspace.service';
import {
  SearchWorkspaceQueryDto,
  SearchWorkspaceResponseDto,
} from './dto/search-workspace.dto';
import { FileTreeResponseDto } from './dto/file-tree.dto';
 
interface FileContentResponse {
  content: string;
}
 
/**
 * Workspace endpoints provide file system access for the project directory.
 *
 * Use these endpoints to:
 * - Get the file tree structure (for navigation)
 * - Search files by name or content (for Cmd+P functionality)
 * - Read file contents (for displaying/editing files)
 * - Invalidate cache when files change externally
 */
@ApiTags('workspace')
@Controller('workspace')
export class WorkspaceController {
  constructor(private readonly workspaceService: WorkspaceService) {}
 
  @Get('file-tree')
  @ApiOperation({
    summary: 'Get project file tree',
    description:
      'Returns the hierarchical directory structure for the current project. Used by the frontend for file navigation and tree views.',
  })
  @ApiResponse({
    status: 200,
    description: 'File tree structure',
    type: FileTreeResponseDto,
  })
  async getFileTree(): Promise<FileTreeResponseDto> {
    return this.workspaceService.getFileTree();
  }
 
  @Post('file-tree/invalidate')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({
    summary: 'Invalidate file tree cache',
    description:
      'Clears the cached file tree. Call this when files are changed externally (e.g., git operations, external editors).',
  })
  @ApiResponse({ status: 204, description: 'Cache invalidated' })
  async invalidateFileTreeCache(): Promise<void> {
    return this.workspaceService.invalidateFileTreeCache();
  }
 
  @Get('search')
  @ApiOperation({
    summary: 'Search files in workspace',
    description:
      'Fuzzy search for files by name or content. Used for Cmd+P-style quick file opening. Uses ripgrep for fast searching.',
  })
  @ApiResponse({
    status: 200,
    description: 'Search results with file paths and matches',
    type: SearchWorkspaceResponseDto,
  })
  async search(
    @Query() query: SearchWorkspaceQueryDto,
  ): Promise<SearchWorkspaceResponseDto> {
    const { q, type, page, limit } = query;
    return this.workspaceService.search(q, type, page, limit);
  }
 
  @Get('file-content')
  @ApiOperation({
    summary: 'Get file content',
    description:
      'Read the content of a specific file. Path is relative to project root.',
  })
  @ApiQuery({
    name: 'path',
    description: 'Relative path to the file',
    example: 'src/index.ts',
    required: true,
  })
  @ApiResponse({
    status: 200,
    description: 'File content',
    schema: { properties: { content: { type: 'string' } } },
  })
  async getFileContent(
    @Query('path') path: string,
  ): Promise<FileContentResponse> {
    const content = await this.workspaceService.getFileContent(path);
    return { content };
  }
}