Skip to content

The open alternative to Salesforce, designed for AI.

3.0 KB
Raw Blame
import { assertUnreachable } from 'twenty-shared/utils';

import {
  ConflictError,
  ForbiddenError,
  InternalServerError,
  NotFoundError,
  UserInputError,
} from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
import {
  AiException,
  AiExceptionCode,
} from 'src/engine/metadata-modules/ai/ai.exception';

export const aiGraphqlApiExceptionHandler = (error: Error) => {
  if (error instanceof AiException) {
    switch (error.code) {
      case AiExceptionCode.AGENT_NOT_FOUND:
      case AiExceptionCode.THREAD_NOT_FOUND:
      case AiExceptionCode.RECORD_NOT_FOUND:
      case AiExceptionCode.WORKSPACE_NOT_FOUND:
      case AiExceptionCode.MESSAGE_NOT_FOUND:
      case AiExceptionCode.TOOL_CALL_NOT_FOUND:
      case AiExceptionCode.ROLE_NOT_FOUND:
      case AiExceptionCode.RUN_AS_WORKSPACE_MEMBER_NOT_FOUND:
      case AiExceptionCode.EVALUATION_MODEL_NOT_FOUND:
      case AiExceptionCode.CHAT_CHANNEL_NOT_FOUND:
        throw new NotFoundError(error);
      case AiExceptionCode.CONTEXT_WINDOW_EXCEEDED:
      case AiExceptionCode.INVALID_AGENT_INPUT:
      case AiExceptionCode.INVALID_CHAT_THREAD_SNOOZE_TIME:
      case AiExceptionCode.INVALID_CHAT_MESSAGE_REACTION_EMOJI:
      case AiExceptionCode.INVALID_TOOL_CALL_OUTPUT:
      case AiExceptionCode.TOOL_CALL_NOT_PENDING:
      case AiExceptionCode.EVALUATION_QUESTION_UNSUPPORTED:
      case AiExceptionCode.INVALID_EVALUATION_REQUEST:
      case AiExceptionCode.CHAT_THREAD_ASSIGNEE_CANNOT_REPLY:
      case AiExceptionCode.CHAT_THREAD_NOT_IN_CHANNEL:
      case AiExceptionCode.CHAT_THREAD_CANNOT_LEAVE_CHANNEL:
      case AiExceptionCode.INVALID_CHAT_CHANNEL_NAME:
      case AiExceptionCode.INVALID_CHAT_CHANNEL_ROLE:
      case AiExceptionCode.INVALID_CHAT_CHANNEL_DESTINATION:
      case AiExceptionCode.INVALID_CHAT_CHANNEL_THREAD_CURSOR:
        throw new UserInputError(error);
      case AiExceptionCode.AGENT_ALREADY_EXISTS:
      case AiExceptionCode.NO_FAILED_TURN_TO_RETRY:
      case AiExceptionCode.CHAT_CHANNELS_UNAVAILABLE:
      case AiExceptionCode.CHAT_UPGRADE_IN_PROGRESS:
      case AiExceptionCode.THREAD_AWAITING_ANSWER:
      case AiExceptionCode.CHAT_THREAD_ASSIGNEE_CANNOT_UNSUBSCRIBE:
      case AiExceptionCode.CHAT_CHANNEL_NOT_EMPTY:
      case AiExceptionCode.CHAT_CHANNEL_BUSY:
        throw new ConflictError(error);
      case AiExceptionCode.AGENT_IS_STANDARD:
      case AiExceptionCode.AGENT_IS_INACTIVE:
      case AiExceptionCode.ROLE_CANNOT_BE_ASSIGNED_TO_AGENTS:
      case AiExceptionCode.RUN_AS_WORKSPACE_MEMBER_NOT_ALLOWED:
      case AiExceptionCode.RUN_AGENT_NOT_ALLOWED:
      case AiExceptionCode.TOOL_CALL_RESOLUTION_FORBIDDEN:
      case AiExceptionCode.CHAT_CHANNEL_IS_STANDARD:
        throw new ForbiddenError(error);
      case AiExceptionCode.AGENT_EXECUTION_FAILED:
      case AiExceptionCode.API_KEY_NOT_CONFIGURED:
      case AiExceptionCode.STREAM_INTERRUPTED:
        throw new InternalServerError(error);
      default: {
        return assertUnreachable(error.code);
      }
    }
  }

  throw error;
};