All files / src/interactive-chat streaming.controller.ts

62.5% Statements 10/16
100% Branches 0/0
33.33% Functions 1/3
57.14% Lines 8/14

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 876x                 6x 6x                         6x 6x   6x                                             6x                                                       6x                  
import {
  Controller,
  Post,
  Param,
  ParseUUIDPipe,
  HttpCode,
  HttpStatus,
  Logger,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiParam } from '@nestjs/swagger';
import { ChatService } from './chat.service';
 
/**
 * Streaming endpoints control real-time LLM response streaming.
 *
 * Use these endpoints to:
 * - Stop an active streaming response (finishes current chunk)
 * - Cancel an ongoing request entirely (abort the LLM call)
 *
 * Both endpoints require the sessionId and return success status.
 */
@ApiTags('streaming')
@Controller('streaming')
export class StreamingController {
  private readonly logger = new Logger(StreamingController.name);
 
  constructor(private readonly chatService: ChatService) {}
 
  @Post(':sessionId/stop')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({
    summary: 'Stop streaming response',
    description:
      'Stops the active LLM streaming response for a session. The current chunk will complete but no new chunks will be sent.',
  })
  @ApiParam({ name: 'sessionId', description: 'Session UUID', format: 'uuid' })
  @ApiResponse({
    status: 200,
    description: 'Streaming stopped',
    schema: {
      properties: {
        success: { type: 'boolean', example: true },
        aborted: {
          type: 'boolean',
          description: 'Whether streaming was active and aborted',
        },
      },
    },
  })
  async stopStreaming(
    @Param('sessionId', ParseUUIDPipe) sessionId: string,
  ): Promise<{ success: boolean; aborted: boolean }> {
    const aborted = this.chatService.abortStreaming(sessionId);
    return { success: true, aborted };
  }
 
  @Post(':sessionId/cancel')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({
    summary: 'Cancel LLM request',
    description:
      'Cancels the ongoing LLM request for a session. More aggressive than stop - aborts the entire request.',
  })
  @ApiParam({ name: 'sessionId', description: 'Session UUID', format: 'uuid' })
  @ApiResponse({
    status: 200,
    description: 'Request cancelled',
    schema: {
      properties: {
        success: { type: 'boolean', example: true },
        cancelled: {
          type: 'boolean',
          description: 'Whether a request was active and cancelled',
        },
      },
    },
  })
  async cancelRequest(
    @Param('sessionId', ParseUUIDPipe) sessionId: string,
  ): Promise<{ success: boolean; cancelled: boolean }> {
    this.logger.log(`Cancel request received for session: ${sessionId}`);
    const cancelled = this.chatService.cancelRequest(sessionId);
    this.logger.log(`Cancel request result: ${cancelled}`);
    return { success: true, cancelled };
  }
}