tryaii-mcp-server
Version:
TryAII MCP Server - 15+ AI models with comparison, cost tracking, and collective intelligence
85 lines • 3 kB
JavaScript
import mongoose, { Schema } from 'mongoose';
const ChatSessionSchema = new Schema({
userId: { type: String, required: true, index: true },
apiKeyId: { type: String, required: true, index: true },
request: {
modelId: { type: String, required: true },
message: { type: String, required: true },
temperature: Number,
maxTokens: Number,
enableWebSearch: Boolean,
conversationHistory: [mongoose.Schema.Types.Mixed]
},
modelInteraction: {
modelId: { type: String, required: true },
provider: { type: String, required: true },
inputTokens: { type: Number, required: true, min: 0 },
outputTokens: { type: Number, required: true, min: 0 },
thinkingTokens: { type: Number, default: 0, min: 0 }, // 🆕 Reasoning tokens
totalTokens: { type: Number, default: 0, min: 0 }, // 🆕 Total tokens
cost: {
type: Number,
required: true,
min: 0,
get: (v) => parseFloat(v.toFixed(6))
},
costBreakdown: {
inputCost: { type: Number, default: 0, min: 0 },
outputCost: { type: Number, default: 0, min: 0 },
thinkingCost: { type: Number, default: 0, min: 0 } // Same as output pricing
},
latency: { type: Number, required: true, min: 0 },
success: { type: Boolean, required: true },
errorMessage: String,
fullResponse: String,
responsePreview: { type: String, maxlength: 200 },
timestamp: { type: String },
requestMetadata: mongoose.Schema.Types.Mixed
},
clientMetadata: {
ipAddress: String,
userAgent: String,
requestId: String
},
balanceUpdate: {
attempted: { type: Boolean, default: false },
success: { type: Boolean, default: false },
errorMessage: String,
transactionId: String,
balanceAfter: {
type: Number,
get: (v) => v ? parseFloat(v.toFixed(6)) : v
}
},
totalCost: {
type: Number,
required: true,
min: 0,
get: (v) => parseFloat(v.toFixed(6))
},
duration: {
type: Number,
required: true,
min: 0
},
status: {
type: String,
enum: ['processing', 'success', 'error', 'timeout'],
required: true,
default: 'processing'
}
}, {
timestamps: true,
versionKey: false
});
// Add indexes
ChatSessionSchema.index({ userId: 1, createdAt: -1 });
ChatSessionSchema.index({ apiKeyId: 1, createdAt: -1 });
ChatSessionSchema.index({ totalCost: 1, createdAt: -1 });
ChatSessionSchema.index({ status: 1 });
ChatSessionSchema.index({ 'modelInteraction.modelId': 1, createdAt: -1 });
ChatSessionSchema.index({ 'request.modelId': 1, createdAt: -1 });
export function createChatSessionModel(connection) {
return connection.model('MCP_ChatSession', ChatSessionSchema);
}
//# sourceMappingURL=ChatSession.js.map