Files
FastGPT/packages/service/support/wallet/usage/schema.ts
Archer 50bf7f9a3b V4.8.17 feature (#3493)
* split tokens into input and output (#3477)

* split tokens into input and output

* query extension & tool call & question guide

* fix

* perf: input and output tokens

* perf: tool call if else

* perf: remove code

* fix: extract usage count

* fix: qa usage count

---------

Co-authored-by: heheer <heheer@sealos.io>
2024-12-30 10:13:25 +08:00

74 lines
1.7 KiB
TypeScript

import { connectionMongo, getMongoModel, type Model } from '../../../common/mongo';
const { Schema } = connectionMongo;
import { UsageSchemaType } from '@fastgpt/global/support/wallet/usage/type';
import { UsageSourceEnum } from '@fastgpt/global/support/wallet/usage/constants';
import {
TeamCollectionName,
TeamMemberCollectionName
} from '@fastgpt/global/support/user/team/constant';
export const UsageCollectionName = 'usages';
const UsageSchema = new Schema({
teamId: {
type: Schema.Types.ObjectId,
ref: TeamCollectionName,
required: true
},
tmbId: {
type: Schema.Types.ObjectId,
ref: TeamMemberCollectionName,
required: true
},
source: {
type: String,
enum: Object.values(UsageSourceEnum),
required: true
},
appName: {
// usage name
type: String,
default: ''
},
appId: {
type: Schema.Types.ObjectId,
ref: 'apps',
required: false
},
pluginId: {
type: Schema.Types.ObjectId,
ref: 'plugins',
required: false
},
time: {
type: Date,
default: () => new Date()
},
totalPoints: {
// total points
type: Number,
required: true
},
// total: {
// // total points
// type: Number,
// required: true
// },
list: {
type: Array,
default: []
}
});
try {
UsageSchema.index({ teamId: 1, tmbId: 1, source: 1, time: -1 }, { background: true });
// timer task. clear dead team
// UsageSchema.index({ teamId: 1, time: -1 }, { background: true });
UsageSchema.index({ time: 1 }, { background: true, expireAfterSeconds: 360 * 24 * 60 * 60 });
} catch (error) {
console.log(error);
}
export const MongoUsage = getMongoModel<UsageSchemaType>(UsageCollectionName, UsageSchema);