refactor(server): config system (#11081)

This commit is contained in:
forehalo
2025-03-27 12:32:28 +00:00
parent 7091111f85
commit 0ea38680fa
274 changed files with 7583 additions and 5841 deletions
@@ -2,7 +2,6 @@ import { Inject, Logger } from '@nestjs/common';
import { TransactionHost } from '@nestjs-cls/transactional';
import type { TransactionalAdapterPrisma } from '@nestjs-cls/transactional-adapter-prisma';
import { Config } from '../base';
import type { Models } from '.';
import { MODELS_SYMBOL } from './provider';
@@ -12,9 +11,6 @@ export class BaseModel {
@Inject(MODELS_SYMBOL)
protected readonly models!: Models;
@Inject(Config)
protected readonly config!: Config;
@Inject(TransactionHost)
private readonly txHost!: TransactionHost<TransactionalAdapterPrisma>;
@@ -0,0 +1,24 @@
import { Injectable } from '@nestjs/common';
import { Transactional } from '@nestjs-cls/transactional';
import { BaseModel } from './base';
@Injectable()
export class AppConfigModel extends BaseModel {
async load() {
return this.db.appConfig.findMany();
}
@Transactional()
async save(user: string, updates: Array<{ key: string; value: any }>) {
return await Promise.allSettled(
updates.map(async update => {
return this.db.appConfig.upsert({
where: { id: update.key },
update: { value: update.value, lastUpdatedBy: user },
create: { id: update.key, value: update.value, lastUpdatedBy: user },
});
})
);
}
}
@@ -133,7 +133,7 @@ export class FeatureModel extends BaseModel {
const name = key as FeatureName;
const def = FeatureConfigs[name];
// self-hosted instance will use pro plan as free plan
if (name === 'free_plan_v1' && this.config.isSelfhosted) {
if (name === 'free_plan_v1' && env.selfhosted) {
await this.upsert(
name,
FeatureConfigs['pro_plan_v1'].configs,
@@ -7,6 +7,7 @@ import {
import { ModuleRef } from '@nestjs/core';
import { ApplyType } from '../base';
import { AppConfigModel } from './config';
import { CopilotContextModel } from './copilot-context';
import { CopilotJobModel } from './copilot-job';
import { CopilotSessionModel } from './copilot-session';
@@ -44,6 +45,7 @@ const MODELS = {
copilotSession: CopilotSessionModel,
copilotContext: CopilotContextModel,
copilotJob: CopilotJobModel,
appConfig: AppConfigModel,
};
type ModelsType = {
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import {
Prisma,
type Session,
@@ -6,6 +6,7 @@ import {
type UserSession,
} from '@prisma/client';
import { Config } from '../base';
import { BaseModel } from './base';
export type { Session, UserSession };
@@ -13,6 +14,9 @@ export type UserSessionWithUser = UserSession & { user: User };
@Injectable()
export class SessionModel extends BaseModel {
@Inject(Config)
private readonly config!: Config;
async createSession() {
return await this.db.session.create({
data: {},