All files / src/platform/ai/services ai-gateway.service.ts

0% Statements 0/53
0% Branches 0/40
0% Functions 0/10
0% Lines 0/51

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                                                                                                                                                                                                                                                                                                                                                                                                                                                             
import {
  Injectable,
  Logger,
  ForbiddenException,
  BadRequestException,
  NotFoundException,
} from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import {
  AiGatewayProvider,
  AiRoutingPolicy,
  AiCreditLedger,
  AiConversation,
  AiMessage,
} from '../schemas/ai-gateway.schema';
import { AiService } from './ai.service';
 
@Injectable()
export class AiGatewayService {
  private readonly logger = new Logger(AiGatewayService.name);
 
  constructor(
    @InjectModel(AiGatewayProvider.name)
    private readonly providerModel: Model<AiGatewayProvider>,
    @InjectModel(AiRoutingPolicy.name)
    private readonly routingModel: Model<AiRoutingPolicy>,
    @InjectModel(AiCreditLedger.name)
    private readonly creditModel: Model<AiCreditLedger>,
    @InjectModel(AiConversation.name)
    private readonly conversationModel: Model<AiConversation>,
    @InjectModel(AiMessage.name)
    private readonly messageModel: Model<AiMessage>,
    private readonly baseAiService: AiService,
  ) {}
 
  async validateCredits(tenantId: string, estimatedCost = 1): Promise<void> {
    const totalCredits = await this.creditModel.aggregate([
      { $match: { tenantId } },
      { $group: { _id: null, balance: { $sum: '$amount' } } },
    ]);
 
    const balance = totalCredits[0]?.balance || 0;
    if (balance < estimatedCost) {
      throw new ForbiddenException(
        'Insufficient AI credits to perform this request',
      );
    }
  }
 
  async deductCredits(
    tenantId: string,
    amount: number,
    reason: string,
  ): Promise<void> {
    await this.creditModel.create({
      tenantId,
      amount: -amount,
      transactionType: 'debit',
      description: reason,
    });
  }
 
  async addCredits(
    tenantId: string,
    amount: number,
    reason: string,
  ): Promise<void> {
    await this.creditModel.create({
      tenantId,
      amount,
      transactionType: 'credit',
      description: reason,
    });
  }
 
  async getModelForCapability(capability: string): Promise<string> {
    const policy = await this.routingModel
      .findOne({ capability, isActive: true })
      .exec();
    if (!policy) {
      // Default fallback
      return 'mock';
    }
    return policy.primaryModelKey;
  }
 
  async generateTextWithRouting(
    tenantId: string,
    capability: string,
    promptKey: string,
    variables: Record<string, string> = {},
  ): Promise<string> {
    // 1. Credit check
    await this.validateCredits(tenantId, 1); // 1 credit per generation estimate
 
    // 2. Resolve routed model
    const routedModel = await this.getModelForCapability(capability);
 
    try {
      const responseText = await this.baseAiService.generateText(
        tenantId,
        promptKey,
        variables,
        routedModel,
      );
 
      // 3. Deduct usage credits
      await this.deductCredits(
        tenantId,
        1,
        `Generation trigger using model ${routedModel} for prompt ${promptKey}`,
      );
      return responseText;
    } catch (err: any) {
      this.logger.error(
        `AI Gateway failover trigger: primary model ${routedModel} failed: ${err.message}`,
      );
 
      // Fallback chain
      const policy = await this.routingModel
        .findOne({ capability, isActive: true })
        .exec();
      if (policy && policy.fallbackModelKeys.length > 0) {
        for (const fallbackModel of policy.fallbackModelKeys) {
          try {
            this.logger.log(`Attempting fallback model: ${fallbackModel}`);
            const responseText = await this.baseAiService.generateText(
              tenantId,
              promptKey,
              variables,
              fallbackModel,
            );
            await this.deductCredits(
              tenantId,
              1,
              `Fallback model generation: ${fallbackModel}`,
            );
            return responseText;
          } catch (fallbackErr: any) {
            this.logger.error(
              `Fallback model ${fallbackModel} also failed: ${fallbackErr.message}`,
            );
          }
        }
      }
 
      throw new BadRequestException(
        'AI Gateway: All routed models failed to generate response',
      );
    }
  }
 
  async startConversation(
    tenantId: string,
    userId: string,
    title?: string,
    systemPrompt?: string,
  ): Promise<AiConversation> {
    return this.conversationModel.create({
      tenantId,
      userId,
      title: title || 'New Chat Session',
      systemPrompt,
    });
  }
 
  async addChatMessage(
    conversationId: string,
    role: 'user' | 'assistant' | 'system',
    content: string,
  ): Promise<AiMessage> {
    const chat = await this.conversationModel.findById(conversationId).exec();
    if (!chat) {
      throw new NotFoundException('Conversation session not found');
    }
 
    const msg = await this.messageModel.create({
      conversationId,
      role,
      content: this.baseAiService.redactPii(content), // PII redaction check
    });
 
    return msg;
  }
 
  async getChatHistory(conversationId: string): Promise<AiMessage[]> {
    return this.messageModel
      .find({ conversationId })
      .sort({ createdAt: 1 })
      .exec();
  }
 
  async executeVisionTask(params: {
    tenantId: string;
    imageBuffer: Buffer;
    prompt: string;
  }): Promise<{ content: string; rawContent: string; confidence: number }> {
    // Deduct credits for vision processing
    await this.validateCredits(params.tenantId, 2);
    await this.deductCredits(
      params.tenantId,
      2,
      `Vision OCR task trigger: ${params.prompt.substring(0, 30)}...`,
    );
 
    // Mock response matching what OCR Service expects
    const mockContentObj = {
      name: 'Mock Extracted Entity',
      documentType: 'Invoice',
      amount: 250.75,
      date: new Date().toISOString().split('T')[0],
      referenceNumber: 'INV-2026-001',
    };
 
    return {
      content: JSON.stringify(mockContentObj),
      rawContent: `Mock OCR raw text: invoice amount 250.75 date ${mockContentObj.date}`,
      confidence: 0.95,
    };
  }
}