refactor(data-schemas): introduce new models and types for balance, conversation, message, and session

- Added new model files for Balance, Conversation, Message, and Session, enhancing modularity.
- Created corresponding type definitions for IBalance, IConversation, IMessage, and updated existing types.
- Refactored index files to export models from their individual files for better organization.
This commit is contained in:
Danny Avila 2025-05-30 02:13:35 -04:00
parent f6ca8caf7e
commit fa9177180f
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
14 changed files with 174 additions and 99 deletions

View file

@ -25,7 +25,6 @@ export { default as assistantSchema } from './schema/assistant';
export type { IAssistant } from './schema/assistant';
export { default as balanceSchema } from './schema/balance';
export type { IBalance } from './schema/balance';
export { default as bannerSchema } from './schema/banner';
export type { IBanner } from './schema/banner';
@ -37,7 +36,6 @@ export { default as conversationTagSchema } from './schema/conversationTag';
export type { IConversationTag } from './schema/conversationTag';
export { default as convoSchema } from './schema/convo';
export type { IConversation } from './schema/convo';
export { default as fileSchema } from './schema/file';
export type { IMongoFile } from './schema/file';

View file

@ -0,0 +1,6 @@
import mongoose from 'mongoose';
import balanceSchema from '~/schema/balance';
import type * as t from '~/types';
export const Balance =
mongoose.models.Balance || mongoose.model<t.IBalance>('Balance', balanceSchema);

View file

@ -0,0 +1,17 @@
import mongoose from 'mongoose';
import type * as t from '~/types';
import mongoMeili from '~/models/plugins/mongoMeili';
import convoSchema from '~/schema/convo';
if (process.env.MEILI_HOST && process.env.MEILI_MASTER_KEY) {
convoSchema.plugin(mongoMeili, {
host: process.env.MEILI_HOST,
apiKey: process.env.MEILI_MASTER_KEY,
/** Note: Will get created automatically if it doesn't exist already */
indexName: 'convos',
primaryKey: 'conversationId',
});
}
export const Conversation =
mongoose.models.Conversation || mongoose.model<t.IConversation>('Conversation', convoSchema);

View file

@ -1,25 +1,7 @@
import mongoose from 'mongoose';
// Import schemas
import userSchema from '~/schema/user';
import sessionSchema from '~/schema/session';
import tokenSchema from '~/schema/token';
import balanceSchema from '~/schema/balance';
// Import types
import { IUser, ISession, IToken } from '~/types';
import { IBalance } from '~/schema/balance';
// Create and export model instances
export const User = mongoose.model<IUser>('User', userSchema);
export const Session = mongoose.model<ISession>('Session', sessionSchema);
export const Token = mongoose.model<IToken>('Token', tokenSchema);
export const Balance = mongoose.model<IBalance>('Balance', balanceSchema);
// Default export with all models
export default {
User,
Session,
Token,
Balance,
};
// Export models from their individual files
export { User } from './user';
export { Token } from './token';
export { Session } from './session';
export { Balance } from './balance';
export { Conversation } from './convo';
export { Message } from './message';

View file

@ -0,0 +1,16 @@
import mongoose from 'mongoose';
import mongoMeili from '~/models/plugins/mongoMeili';
import messageSchema from '~/schema/message';
import type * as t from '~/types';
if (process.env.MEILI_HOST && process.env.MEILI_MASTER_KEY) {
messageSchema.plugin(mongoMeili, {
host: process.env.MEILI_HOST,
apiKey: process.env.MEILI_MASTER_KEY,
indexName: 'messages',
primaryKey: 'messageId',
});
}
export const Message =
mongoose.models.Message || mongoose.model<t.IMessage>('Message', messageSchema);

View file

@ -0,0 +1,6 @@
import mongoose from 'mongoose';
import sessionSchema from '~/schema/session';
import type * as t from '~/types';
export const Session =
mongoose.models.Session || mongoose.model<t.ISession>('Session', sessionSchema);

View file

@ -0,0 +1,5 @@
import mongoose from 'mongoose';
import tokenSchema from '~/schema/token';
import type * as t from '~/types';
export const Token = mongoose.models.Token || mongoose.model<t.IToken>('Token', tokenSchema);

View file

@ -0,0 +1,5 @@
import mongoose from 'mongoose';
import userSchema from '~/schema/user';
import type * as t from '~/types';
export const User = mongoose.models.User || mongoose.model<t.IUser>('User', userSchema);

View file

@ -1,17 +1,7 @@
import { Schema, Document, Types } from 'mongoose';
import { Schema } from 'mongoose';
import type * as t from '~/types';
export interface IBalance extends Document {
user: Types.ObjectId;
tokenCredits: number;
// Automatic refill settings
autoRefillEnabled: boolean;
refillIntervalValue: number;
refillIntervalUnit: 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months';
lastRefill: Date;
refillAmount: number;
}
const balanceSchema = new Schema<IBalance>({
const balanceSchema = new Schema<t.IBalance>({
user: {
type: Schema.Types.ObjectId,
ref: 'User',

View file

@ -1,57 +1,6 @@
import mongoose, { Schema, Document, Types } from 'mongoose';
import mongoose, { Schema } from 'mongoose';
import { conversationPreset } from './defaults';
// @ts-ignore
export interface IConversation extends Document {
conversationId: string;
title?: string;
user?: string;
messages?: Types.ObjectId[];
agentOptions?: unknown;
// Fields provided by conversationPreset (adjust types as needed)
endpoint?: string;
endpointType?: string;
model?: string;
region?: string;
chatGptLabel?: string;
examples?: unknown[];
modelLabel?: string;
promptPrefix?: string;
temperature?: number;
top_p?: number;
topP?: number;
topK?: number;
maxOutputTokens?: number;
maxTokens?: number;
presence_penalty?: number;
frequency_penalty?: number;
file_ids?: string[];
resendImages?: boolean;
promptCache?: boolean;
thinking?: boolean;
thinkingBudget?: number;
system?: string;
resendFiles?: boolean;
imageDetail?: string;
agent_id?: string;
assistant_id?: string;
instructions?: string;
stop?: string[];
isArchived?: boolean;
iconURL?: string;
greeting?: string;
spec?: string;
tags?: string[];
tools?: string[];
maxContextTokens?: number;
max_tokens?: number;
reasoning_effort?: string;
// Additional fields
files?: string[];
expiredAt?: Date;
createdAt?: Date;
updatedAt?: Date;
}
import { IConversation } from '~/types';
const convoSchema: Schema<IConversation> = new Schema(
{

View file

@ -0,0 +1,12 @@
import { Document, Types } from 'mongoose';
export interface IBalance extends Document {
user: Types.ObjectId;
tokenCredits: number;
// Automatic refill settings
autoRefillEnabled: boolean;
refillIntervalValue: number;
refillIntervalUnit: 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months';
lastRefill: Date;
refillAmount: number;
}

View file

@ -0,0 +1,53 @@
import type { Document, Types } from 'mongoose';
// @ts-ignore
export interface IConversation extends Document {
conversationId: string;
title?: string;
user?: string;
messages?: Types.ObjectId[];
agentOptions?: unknown;
// Fields provided by conversationPreset (adjust types as needed)
endpoint?: string;
endpointType?: string;
model?: string;
region?: string;
chatGptLabel?: string;
examples?: unknown[];
modelLabel?: string;
promptPrefix?: string;
temperature?: number;
top_p?: number;
topP?: number;
topK?: number;
maxOutputTokens?: number;
maxTokens?: number;
presence_penalty?: number;
frequency_penalty?: number;
file_ids?: string[];
resendImages?: boolean;
promptCache?: boolean;
thinking?: boolean;
thinkingBudget?: number;
system?: string;
resendFiles?: boolean;
imageDetail?: string;
agent_id?: string;
assistant_id?: string;
instructions?: string;
stop?: string[];
isArchived?: boolean;
iconURL?: string;
greeting?: string;
spec?: string;
tags?: string[];
tools?: string[];
maxContextTokens?: number;
max_tokens?: number;
reasoning_effort?: string;
// Additional fields
files?: string[];
expiredAt?: Date;
createdAt?: Date;
updatedAt?: Date;
}

View file

@ -1,8 +1,6 @@
// User types
export * from './user';
// Session types
export * from './session';
// Token types
export * from './token';
export * from './convo';
export * from './session';
export * from './balance';
export * from './message';

View file

@ -0,0 +1,38 @@
import type { Document } from 'mongoose';
// @ts-ignore
export interface IMessage extends Document {
messageId: string;
conversationId: string;
user: string;
model?: string;
endpoint?: string;
conversationSignature?: string;
clientId?: string;
invocationId?: number;
parentMessageId?: string;
tokenCount?: number;
summaryTokenCount?: number;
sender?: string;
text?: string;
summary?: string;
isCreatedByUser: boolean;
unfinished?: boolean;
error?: boolean;
finish_reason?: string;
_meiliIndex?: boolean;
files?: unknown[];
plugin?: {
latest?: string;
inputs?: unknown[];
outputs?: string;
};
plugins?: unknown[];
content?: unknown[];
thread_id?: string;
iconURL?: string;
attachments?: unknown[];
expiredAt?: Date;
createdAt?: Date;
updatedAt?: Date;
}