feat: M0-08 — Domain Event publish on AI usage + EventBus injection
Some checks failed
Deploy API Server / build-and-deploy (push) Failing after 17s

This commit is contained in:
WangDL 2026-05-23 09:42:29 +08:00
parent 908ffc7041
commit e7cbe6da26

View File

@ -5,6 +5,8 @@ import { PromptTemplateService } from '../prompts/prompt-template.service';
import { AiCostCalculatorService } from '../usage/ai-cost-calculator.service';
import { AiUsageLogService } from '../usage/ai-usage-log.service';
import { ContentSafetyService } from '../../content-safety/content-safety.service';
import { EventBusService } from '../../../common/event-bus/event-bus.service';
import { BaseDomainEvent } from '../../../common/events/base-domain.event';
import type { AiProvider } from '../providers/ai-provider.interface';
import type { GatewayRequest, GatewayResponse, ModelTier } from './ai-gateway.types';
@ -20,6 +22,7 @@ export class AiGatewayService {
private readonly usageLog: AiUsageLogService,
private readonly providers: Map<string, AiProvider>,
private readonly contentSafety?: ContentSafetyService,
private readonly eventBus?: EventBusService,
) {}
async generate(request: GatewayRequest, timeoutMs = this.DEFAULT_TIMEOUT_MS): Promise<GatewayResponse> {
@ -60,6 +63,8 @@ export class AiGatewayService {
output.usage.outputTokens,
);
this.eventBus?.publish(new (class extends BaseDomainEvent { eventType = 'ai.usage.recorded'; data: any; constructor(d: any) { super(); this.data = d; } })({ userId: request.userId, provider: target.provider, model: target.model, tier: request.tier, inputTokens: output.usage.inputTokens, outputTokens: output.usage.outputTokens, estimatedCost, latencyMs: output.latencyMs }));
this.usageLog.log({
userId: request.userId,
feature: request.feature,
@ -96,6 +101,8 @@ export class AiGatewayService {
}
}
this.eventBus?.publish(new (class extends BaseDomainEvent { eventType = 'ai.usage.recorded'; data: any; constructor(d: any) { super(); this.data = d; } })({ userId: request.userId, provider: target.provider, model: target.model, tier: request.tier, inputTokens: output.usage.inputTokens, outputTokens: output.usage.outputTokens, estimatedCost, latencyMs: output.latencyMs }));
this.usageLog.log({
userId: request.userId,
feature: request.feature,