All files / src/ai-actions ai-actions.controller.ts

84.37% Statements 54/64
64.28% Branches 9/14
76.47% Functions 13/17
83.87% Lines 52/62

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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 2746x                     6x 6x         6x 6x                                       6x   6x 6x                     6x                           6x                     6x                       6x     5x                 6x     1x                         6x     1x                 6x     1x                 6x                           6x         4x   3x 4x   3x 4x     3x 1x       1x     2x 1x 1x 1x                   6x         1x   1x             1x               6x         1x   1x             1x                             6x         3x   2x                 2x 2x   2x 2x     2x 1x 1x 1x            
import {
  Controller,
  Get,
  Param,
  ParseUUIDPipe,
  Post,
  HttpCode,
  HttpStatus,
  Res,
} from '@nestjs/common';
import { Response } from 'express';
import { ApiTags, ApiOperation, ApiResponse, ApiParam } from '@nestjs/swagger';
import {
  AIActionsService,
  AIActionStatus,
  ActionResult,
} from './ai-actions.service';
import { AIAction, ExecutionLog } from '../core-entities';
import { AIActionBatchService } from './ai-action-batch.service';
 
interface ActionResultResponse {
  action_id: string;
  status: string;
  logs: ExecutionLog[];
}
 
interface DiscardedActionInfoResponse {
  action_id: string;
  status: string;
}
 
interface ConfirmedActionInfoResponse {
  action_id: string;
  status: string;
}
 
@ApiTags('ai-actions')
@Controller()
export class AIActionsController {
  constructor(
    private readonly aiActionsService: AIActionsService,
    private readonly aiActionBatchService: AIActionBatchService,
  ) {}
 
  @Post('ai-actions/:inputId/revert')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({
    summary: 'Revert all actions for an input to AI Studio state',
  })
  @ApiParam({ name: 'inputId', description: 'Input UUID', format: 'uuid' })
  @ApiResponse({ status: 204, description: 'Actions reverted successfully' })
  @ApiResponse({ status: 404, description: 'Input not found' })
  async revertToStudioState(
    @Param('inputId', ParseUUIDPipe) inputId: string,
  ): Promise<void> {
    return this.aiActionsService.revertToStudioState(inputId);
  }
 
  @Get('inputs/:inputId/actions')
  @ApiOperation({ summary: 'List all AI actions for an input' })
  @ApiParam({ name: 'inputId', description: 'Input UUID', format: 'uuid' })
  @ApiResponse({
    status: 200,
    description: 'List of AI actions',
    type: [AIAction],
  })
  async findAllByInputId(
    @Param('inputId', ParseUUIDPipe) inputId: string,
  ): Promise<AIAction[]> {
    return this.aiActionsService.findAllByInputId(inputId);
  }
 
  @Get('actions/:actionId')
  @ApiOperation({ summary: 'Get a single AI action by ID' })
  @ApiParam({ name: 'actionId', description: 'Action UUID', format: 'uuid' })
  @ApiResponse({ status: 200, description: 'Action found', type: AIAction })
  @ApiResponse({ status: 404, description: 'Action not found' })
  async findOne(
    @Param('actionId', ParseUUIDPipe) actionId: string,
  ): Promise<AIAction> {
    return this.aiActionsService.findOne(actionId);
  }
 
  @Post('actions/:actionId/approve')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Mark action as approved for execution' })
  @ApiParam({ name: 'actionId', description: 'Action UUID', format: 'uuid' })
  @ApiResponse({ status: 200, description: 'Action approved', type: AIAction })
  @ApiResponse({ status: 404, description: 'Action not found' })
  async approveAction(
    @Param('actionId', ParseUUIDPipe) actionId: string,
  ): Promise<AIAction> {
    return this.aiActionsService.approveAction(actionId);
  }
 
  @Post('actions/:actionId/discard')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Discard a proposed action' })
  @ApiParam({ name: 'actionId', description: 'Action UUID', format: 'uuid' })
  @ApiResponse({ status: 200, description: 'Action discarded', type: AIAction })
  @ApiResponse({ status: 404, description: 'Action not found' })
  async discardAction(
    @Param('actionId', ParseUUIDPipe) actionId: string,
  ): Promise<AIAction> {
    return this.aiActionsService.discardAction(actionId);
  }
 
  @Post('actions/:actionId/confirm-keep')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Confirm to keep an applied action (permanent)' })
  @ApiParam({ name: 'actionId', description: 'Action UUID', format: 'uuid' })
  @ApiResponse({
    status: 200,
    description: 'Action confirmed as kept',
    type: AIAction,
  })
  @ApiResponse({ status: 404, description: 'Action not found' })
  async confirmKeepAction(
    @Param('actionId', ParseUUIDPipe) actionId: string,
  ): Promise<AIAction> {
    return this.aiActionsService.confirmKeepAction(actionId);
  }
 
  @Post('actions/:actionId/revert')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Revert an applied action to original state' })
  @ApiParam({ name: 'actionId', description: 'Action UUID', format: 'uuid' })
  @ApiResponse({ status: 200, description: 'Action reverted', type: AIAction })
  @ApiResponse({ status: 404, description: 'Action not found' })
  async revertAction(
    @Param('actionId', ParseUUIDPipe) actionId: string,
  ): Promise<AIAction> {
    return this.aiActionsService.revertAction(actionId);
  }
 
  @Post('actions/:actionId/execute')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Execute/re-execute an action' })
  @ApiParam({ name: 'actionId', description: 'Action UUID', format: 'uuid' })
  @ApiResponse({ status: 200, description: 'Action executed', type: AIAction })
  @ApiResponse({ status: 404, description: 'Action not found' })
  async executeAction(
    @Param('actionId', ParseUUIDPipe) actionId: string,
  ): Promise<AIAction> {
    return this.aiActionsService.reExecuteAction(actionId);
  }
 
  @Post('inputs/:inputId/apply-approved-actions')
  @ApiOperation({ summary: 'Execute all approved actions for input' })
  @ApiParam({ name: 'inputId', description: 'Input UUID', format: 'uuid' })
  @ApiResponse({ status: 200, description: 'All actions applied successfully' })
  @ApiResponse({
    status: 207,
    description: 'Multi-Status: some actions failed',
  })
  async applyApprovedActions(
    @Param('inputId', ParseUUIDPipe) inputId: string,
    @Res() response: Response,
  ): Promise<void> {
    const results: ActionResultResponse[] =
      await this.aiActionBatchService.applyApprovedActions(inputId);
 
    const allSucceeded = results.every(
      (r) => r.status === AIActionStatus.CONFIRMED_KEPT,
    );
    const someFailed = results.some(
      (r) => r.status === AIActionStatus.EXECUTION_FAILED,
    );
 
    if (results.length === 0) {
      response.status(HttpStatus.OK).json({
        message: 'No actions were in "approved_for_apply" state to process.',
        results: [],
      });
      return;
    }
 
    if (allSucceeded) {
      response.status(HttpStatus.OK).json({ results });
    } else if (someFailed) {
      response.status(207).json({ results });
    } else E{
      response.status(HttpStatus.OK).json({ results });
    }
  }
 
  @Post('inputs/:inputId/discard-actions')
  @ApiOperation({ summary: 'Discard all pending actions for input' })
  @ApiParam({ name: 'inputId', description: 'Input UUID', format: 'uuid' })
  @ApiResponse({ status: 200, description: 'Actions discarded' })
  async discardAllActions(
    @Param('inputId', ParseUUIDPipe) inputId: string,
    @Res() response: Response,
  ): Promise<void> {
    const results: DiscardedActionInfoResponse[] =
      await this.aiActionBatchService.discardAllActionsForInput(inputId);
 
    Iif (results.length === 0) {
      response.status(HttpStatus.OK).json({
        message:
          'No actions found in "proposed" or "approved_for_apply" state to discard for this input.',
        results: [],
      });
    } else {
      response.status(HttpStatus.OK).json({ results });
    }
  }
 
  @Post('inputs/:inputId/confirm-all-actions')
  @ApiOperation({ summary: 'Confirm all applied actions as permanent' })
  @ApiParam({ name: 'inputId', description: 'Input UUID', format: 'uuid' })
  @ApiResponse({ status: 200, description: 'Actions confirmed' })
  async confirmAllActions(
    @Param('inputId', ParseUUIDPipe) inputId: string,
    @Res() response: Response,
  ): Promise<void> {
    const results: ConfirmedActionInfoResponse[] =
      await this.aiActionBatchService.confirmAllAppliedActionsForInput(inputId);
 
    Iif (results.length === 0) {
      response.status(HttpStatus.OK).json({
        message:
          'No actions found in "applied_pending_review" state to confirm for this input.',
        results: [],
      });
    } else {
      response.status(HttpStatus.OK).json({ results });
    }
  }
 
  @Post('inputs/:inputId/revert-all-actions')
  @ApiOperation({ summary: 'Revert all applied actions for input' })
  @ApiParam({ name: 'inputId', description: 'Input UUID', format: 'uuid' })
  @ApiResponse({
    status: 200,
    description: 'All actions reverted successfully',
  })
  @ApiResponse({
    status: 207,
    description: 'Multi-Status: some reverts failed',
  })
  async revertAllActions(
    @Param('inputId', ParseUUIDPipe) inputId: string,
    @Res() response: Response,
  ): Promise<void> {
    const results: ActionResult[] =
      await this.aiActionBatchService.revertAllAppliedActionsForInput(inputId);
 
    Iif (results.length === 0) {
      response.status(HttpStatus.OK).json({
        message:
          'No actions in "applied_pending_review" state to revert for this input.',
        results: [],
      });
      return;
    }
 
    const allRevertedSuccessfully = results.every(
      (r) => r.status === AIActionStatus.CONFIRMED_REVERTED,
    );
    const someRevertsFailed = results.some(
      (r) => r.status === AIActionStatus.REVERT_FAILED,
    );
 
    if (allRevertedSuccessfully) {
      response.status(HttpStatus.OK).json({ results });
    } else if (someRevertsFailed) {
      response.status(207).json({ results });
    } else E{
      response.status(HttpStatus.OK).json({ results });
    }
  }
}