import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, } from 'typeorm'; import { AIProvider } from './ai-config.entity'; export enum AIModelType { CHAT = 'chat', COMPLETION = 'completion', EMBEDDING = 'embedding', IMAGE = 'image', } export enum UsageStatus { PENDING = 'pending', COMPLETED = 'completed', FAILED = 'failed', CANCELLED = 'cancelled', } @Entity({ name: 'usage', schema: 'ai' }) export class AIUsage { @PrimaryGeneratedColumn('uuid') id: string; @Column({ type: 'uuid' }) tenant_id: string; @Column({ type: 'uuid' }) user_id: string; @Column({ type: 'enum', enum: AIProvider }) provider: AIProvider; @Column({ type: 'varchar', length: 100 }) model: string; @Column({ type: 'enum', enum: AIModelType, default: AIModelType.CHAT }) model_type: AIModelType; @Column({ type: 'enum', enum: UsageStatus, default: UsageStatus.PENDING }) status: UsageStatus; @Column({ type: 'int', default: 0 }) input_tokens: number; @Column({ type: 'int', default: 0 }) output_tokens: number; // total_tokens is computed in DB, but we can add it for convenience get total_tokens(): number { return this.input_tokens + this.output_tokens; } @Column({ type: 'numeric', precision: 12, scale: 6, default: 0 }) cost_input: number; @Column({ type: 'numeric', precision: 12, scale: 6, default: 0 }) cost_output: number; get cost_total(): number { return Number(this.cost_input) + Number(this.cost_output); } @Column({ type: 'int', nullable: true }) latency_ms: number; @Column({ type: 'timestamptz', default: () => 'NOW()' }) started_at: Date; @Column({ type: 'timestamptz', nullable: true }) completed_at: Date; @Column({ type: 'varchar', length: 100, nullable: true }) request_id: string; @Column({ type: 'varchar', length: 50, nullable: true }) endpoint: string; @Column({ type: 'text', nullable: true }) error_message: string; @Column({ type: 'jsonb', default: {} }) metadata: Record; @CreateDateColumn({ type: 'timestamptz' }) created_at: Date; }