Compare commits
No commits in common. "ae7c88d21372d5b5335de78209d7d0e1afffe522" and "51e95be41b94af9ffd55074b55098db0fe4abfbc" have entirely different histories.
ae7c88d213
...
51e95be41b
|
@ -1,8 +0,0 @@
|
||||||
**添加规则文件可帮助模型精准理解你的编码偏好,如框架、代码风格等**
|
|
||||||
**规则文件只对当前工程生效,单文件限制10000字符。如果无需将该文件提交到远程 Git 仓库,请将其添加到 .gitignore**
|
|
||||||
# 技术栈规范
|
|
||||||
|
|
||||||
## 基础环境
|
|
||||||
1. 语言:TypeScript
|
|
||||||
2. nodejs 使用版本v22
|
|
||||||
3. 执行node, npm 相关命令时先执行`nvm use v22`切换到v22版本
|
|
|
@ -1 +0,0 @@
|
||||||
require('./dist/main');
|
|
File diff suppressed because it is too large
Load Diff
|
@ -6,7 +6,6 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "rimraf dist && tsc",
|
"build": "rimraf dist && tsc",
|
||||||
"start": "npm run build && node dist/main.js",
|
"start": "npm run build && node dist/main.js",
|
||||||
"start:dev": "nest start --watch",
|
|
||||||
"dev": "ts-node -r tsconfig-paths/register src/main.ts",
|
"dev": "ts-node -r tsconfig-paths/register src/main.ts",
|
||||||
"typeorma": "typeorm-ts-node-commonjs -d dist/data-source.js",
|
"typeorma": "typeorm-ts-node-commonjs -d dist/data-source.js",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
|
@ -18,14 +17,10 @@
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nestjs/axios": "^4.0.0",
|
|
||||||
"@nestjs/cli": "^11.0.7",
|
|
||||||
"@nestjs/common": "^11.1.3",
|
"@nestjs/common": "^11.1.3",
|
||||||
"@nestjs/config": "^4.0.2",
|
|
||||||
"@nestjs/core": "^11.1.3",
|
"@nestjs/core": "^11.1.3",
|
||||||
"@nestjs/passport": "^11.0.5",
|
"@nestjs/passport": "^11.0.5",
|
||||||
"@nestjs/platform-express": "^11.1.3",
|
"@nestjs/platform-express": "^11.1.3",
|
||||||
"@nestjs/swagger": "^11.2.0",
|
|
||||||
"@nestjs/typeorm": "^11.0.0",
|
"@nestjs/typeorm": "^11.0.0",
|
||||||
"bcrypt": "^6.0.0",
|
"bcrypt": "^6.0.0",
|
||||||
"class-validator": "^0.14.2",
|
"class-validator": "^0.14.2",
|
||||||
|
@ -35,7 +30,6 @@
|
||||||
"pg": "^8.16.0",
|
"pg": "^8.16.0",
|
||||||
"reflect-metadata": "^0.2.2",
|
"reflect-metadata": "^0.2.2",
|
||||||
"rxjs": "^7.8.2",
|
"rxjs": "^7.8.2",
|
||||||
"swagger-ui-express": "^5.0.1",
|
|
||||||
"uid": "^2.0.2"
|
"uid": "^2.0.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
@ -1,37 +0,0 @@
|
||||||
import { Controller, Get, Post, Body, UseGuards, Request } from '@nestjs/common';
|
|
||||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
|
||||||
import { AiConfigService } from './ai-config.service';
|
|
||||||
import { CreateAiConfigDto } from './dto/create-ai-config.dto';
|
|
||||||
import { UnauthorizedException } from '@nestjs/common';
|
|
||||||
|
|
||||||
interface UserRequest extends Express.Request {
|
|
||||||
user?: {
|
|
||||||
email: string;
|
|
||||||
workspaceId: number;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
@Controller('ai/config')
|
|
||||||
@UseGuards(JwtAuthGuard)
|
|
||||||
export class AiConfigController {
|
|
||||||
constructor(private readonly aiConfigService: AiConfigService) {}
|
|
||||||
|
|
||||||
@Get()
|
|
||||||
getAiConfig(@Request() req: UserRequest) {
|
|
||||||
if (!req.user?.workspaceId) {
|
|
||||||
throw new UnauthorizedException('用户未关联工作区');
|
|
||||||
}
|
|
||||||
return this.aiConfigService.getAiConfigByWorkspace(req.user.workspaceId);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Post()
|
|
||||||
createAiConfig(
|
|
||||||
@Request() req: UserRequest,
|
|
||||||
@Body() createAiConfigDto: CreateAiConfigDto,
|
|
||||||
) {
|
|
||||||
if (!req.user?.workspaceId) {
|
|
||||||
throw new UnauthorizedException('用户未关联工作区');
|
|
||||||
}
|
|
||||||
return this.aiConfigService.createAiConfig(createAiConfigDto, req.user.workspaceId);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,5 +0,0 @@
|
||||||
import { EntityRepository, Repository } from 'typeorm';
|
|
||||||
import { AiConfig } from './ai-config.entity';
|
|
||||||
|
|
||||||
@EntityRepository(AiConfig)
|
|
||||||
export class AiConfigRepository extends Repository<AiConfig> {}
|
|
|
@ -1,43 +0,0 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
|
||||||
import { Repository } from 'typeorm';
|
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
|
||||||
import { AiConfig } from './ai-config.entity';
|
|
||||||
import { CreateAiConfigDto } from './dto/create-ai-config.dto';
|
|
||||||
import { Workspace } from '../workspace/workspace.entity';
|
|
||||||
import { NotFoundException } from '@nestjs/common';
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class AiConfigService {
|
|
||||||
constructor(
|
|
||||||
@InjectRepository(AiConfig)
|
|
||||||
private aiConfigRepository: Repository<AiConfig>,
|
|
||||||
@InjectRepository(Workspace)
|
|
||||||
private workspaceRepository: Repository<Workspace>,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
async getAiConfig() {
|
|
||||||
return await this.aiConfigRepository.find();
|
|
||||||
}
|
|
||||||
|
|
||||||
async createAiConfig(createAiConfigDto: CreateAiConfigDto, workspaceId: number = 1) {
|
|
||||||
const workspace = await this.workspaceRepository.findOneBy({ id: workspaceId });
|
|
||||||
if (!workspace) {
|
|
||||||
throw new NotFoundException('工作区不存在');
|
|
||||||
}
|
|
||||||
|
|
||||||
const aiConfig = this.aiConfigRepository.create({
|
|
||||||
...createAiConfigDto,
|
|
||||||
workspace,
|
|
||||||
});
|
|
||||||
|
|
||||||
return await this.aiConfigRepository.save(aiConfig);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 新增根据工作区获取AI配置的方法
|
|
||||||
async getAiConfigByWorkspace(workspaceId: number) {
|
|
||||||
return await this.aiConfigRepository.findOne({
|
|
||||||
where: { workspace: { id: workspaceId } },
|
|
||||||
select: ['id', 'apiUrl', 'apiKey']
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,14 +0,0 @@
|
||||||
import { Controller, Post, Body, UseGuards } from '@nestjs/common';
|
|
||||||
import { AiService } from './ai.service';
|
|
||||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
|
||||||
|
|
||||||
@Controller('ai')
|
|
||||||
@UseGuards(JwtAuthGuard)
|
|
||||||
export class AiController {
|
|
||||||
constructor(private readonly aiService: AiService) {}
|
|
||||||
|
|
||||||
@Post('/generate-code')
|
|
||||||
generateCode(@Body() body: { prompt: string; workspaceId: number }) {
|
|
||||||
return this.aiService.generateCode(body.prompt, body.workspaceId);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,22 +0,0 @@
|
||||||
import { Module } from '@nestjs/common';
|
|
||||||
import { AiConfigService } from './ai-config.service';
|
|
||||||
import { AiConfigController } from './ai-config.controller';
|
|
||||||
import { WorkspaceModule } from '../workspace/workspace.module';
|
|
||||||
import { AiConfigRepository } from './ai-config.repository';
|
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
||||||
import { AiConfig } from './ai-config.entity';
|
|
||||||
import { WorkspaceRepository } from '../workspace/workspace.repository'; // 新增导入
|
|
||||||
import { JwtStrategy } from '@src/auth';
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
imports: [WorkspaceModule, TypeOrmModule.forFeature([AiConfig])],
|
|
||||||
providers: [
|
|
||||||
AiConfigService,
|
|
||||||
AiConfigRepository,
|
|
||||||
WorkspaceRepository, // 添加缺失的依赖
|
|
||||||
JwtStrategy
|
|
||||||
],
|
|
||||||
controllers: [AiConfigController],
|
|
||||||
exports: [AiConfigService]
|
|
||||||
})
|
|
||||||
export class AiModule {}
|
|
|
@ -1,30 +0,0 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
|
||||||
import { HttpService } from '@nestjs/axios';
|
|
||||||
import { ConfigService } from '@nestjs/config';
|
|
||||||
import { AiConfigService } from './ai-config.service';
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class AiService {
|
|
||||||
private readonly defaultModel: string;
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
private readonly httpService: HttpService,
|
|
||||||
private readonly configService: ConfigService,
|
|
||||||
private readonly aiConfigService: AiConfigService,
|
|
||||||
) {
|
|
||||||
this.defaultModel = this.configService.get<string>('AI_DEFAULT_MODEL', 'gpt-3.5-turbo');
|
|
||||||
}
|
|
||||||
|
|
||||||
async generateCode(prompt: string, workspaceId: number) {
|
|
||||||
// 获取工作区的AI配置
|
|
||||||
const aiConfig = await this.aiConfigService.getAiConfigByWorkspace(workspaceId);
|
|
||||||
|
|
||||||
// 这里实现调用外部AI服务的逻辑
|
|
||||||
// 需要根据实际的AI服务API进行实现
|
|
||||||
return {
|
|
||||||
success: true,
|
|
||||||
code: '// 生成的代码示例\nconsole.log("Hello World");',
|
|
||||||
model: this.defaultModel,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,9 +0,0 @@
|
||||||
import { IsString } from 'class-validator';
|
|
||||||
|
|
||||||
export class CreateAiConfigDto {
|
|
||||||
@IsString()
|
|
||||||
apiUrl!: string;
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
apiKey!: string;
|
|
||||||
}
|
|
|
@ -1,4 +0,0 @@
|
||||||
import { Controller } from '@nestjs/common';
|
|
||||||
|
|
||||||
@Controller()
|
|
||||||
export class AppController {}
|
|
|
@ -1,46 +1,25 @@
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
import { UserModule } from './user/user.module';
|
|
||||||
import { WorkspaceModule } from './workspace/workspace.module';
|
|
||||||
import { ProjectModule } from './project/project.module';
|
|
||||||
import { AuthModule } from './auth/auth.module';
|
import { AuthModule } from './auth/auth.module';
|
||||||
import { AiModule } from './ai/ai.module';
|
|
||||||
import { User } from './user/user.entity';
|
import { User } from './user/user.entity';
|
||||||
import { Workspace } from './workspace/workspace.entity';
|
import { Workspace } from './workspace/workspace.entity';
|
||||||
import { Project } from './project/project.entity';
|
import { Project } from './project/project.entity';
|
||||||
import { AiConfig } from './ai/ai-config.entity';
|
import { AiConfig } from './ai/ai-config.entity';
|
||||||
import { PluginConfig } from './plugins/plugin-config.entity';
|
import { PluginConfig } from './plugins/plugin-config.entity';
|
||||||
import { AppController } from './app.controller';
|
|
||||||
import { AppService } from './app.service';
|
|
||||||
|
|
||||||
// 数据库实体
|
|
||||||
const ENTITIES = [User, Workspace, Project, AiConfig, PluginConfig];
|
|
||||||
|
|
||||||
// 数据库配置
|
|
||||||
const DATABASE_CONFIG = TypeOrmModule.forRoot({
|
|
||||||
type: 'postgres',
|
|
||||||
host: 'localhost',
|
|
||||||
port: 5432,
|
|
||||||
username: 'postgres',
|
|
||||||
password: 'postgres',
|
|
||||||
database: 'aiframe',
|
|
||||||
entities: ENTITIES,
|
|
||||||
synchronize: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
// 功能模块
|
|
||||||
const MODULES = [
|
|
||||||
DATABASE_CONFIG,
|
|
||||||
AuthModule,
|
|
||||||
UserModule,
|
|
||||||
WorkspaceModule,
|
|
||||||
ProjectModule,
|
|
||||||
AiModule,
|
|
||||||
];
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: MODULES,
|
imports: [
|
||||||
controllers: [AppController],
|
TypeOrmModule.forRoot({
|
||||||
providers: [AppService],
|
type: 'postgres',
|
||||||
|
host: 'localhost',
|
||||||
|
port: 5432,
|
||||||
|
username: 'postgres',
|
||||||
|
password: 'postgres',
|
||||||
|
database: 'aiframe',
|
||||||
|
entities: [User, Workspace, Project, AiConfig, PluginConfig],
|
||||||
|
synchronize: true,
|
||||||
|
}),
|
||||||
|
AuthModule,
|
||||||
|
],
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule {}
|
|
@ -1,8 +0,0 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class AppService {
|
|
||||||
getHello(): string {
|
|
||||||
return 'Hello World!';
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,27 +0,0 @@
|
||||||
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
|
|
||||||
import { Reflector } from '@nestjs/core';
|
|
||||||
import { JwtStrategy } from './jwt.strategy';
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class JwtAuthGuard implements CanActivate {
|
|
||||||
constructor(private reflector: Reflector, private jwtStrategy: JwtStrategy) {}
|
|
||||||
|
|
||||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
||||||
const req = context.switchToHttp().getRequest();
|
|
||||||
const token = req.headers.authorization?.split(' ')[1];
|
|
||||||
if (!token) return false;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const user = await new Promise((resolve, reject) => {
|
|
||||||
this.jwtStrategy.validate({ sub: 0, email: token }, (err, user) => {
|
|
||||||
if (err) reject(err);
|
|
||||||
resolve(user);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
req.user = user;
|
|
||||||
return true;
|
|
||||||
} catch (e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -12,11 +12,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async validate(payload: { sub: number; email: string }, done: (error: any, user?: any, info?: any) => void) {
|
async validate(payload: any) {
|
||||||
const user = {
|
return { userId: payload.sub, email: payload.email };
|
||||||
sub: payload.sub,
|
|
||||||
email: payload.email,
|
|
||||||
};
|
|
||||||
done(null, user);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,18 +1,8 @@
|
||||||
import { NestFactory } from '@nestjs/core'
|
import { NestFactory } from '@nestjs/core'
|
||||||
import { AppModule } from './app.module'
|
import { AppModule } from './app.module'
|
||||||
import 'reflect-metadata'
|
import 'reflect-metadata'
|
||||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
|
||||||
async function bootstrap () {
|
async function bootstrap () {
|
||||||
const app = await NestFactory.create(AppModule)
|
const app = await NestFactory.create(AppModule)
|
||||||
const config = new DocumentBuilder()
|
await app.listen(3000)
|
||||||
.setTitle('AIFrame API')
|
|
||||||
.setDescription('API for AIFrame')
|
|
||||||
.setVersion('1.0')
|
|
||||||
.build();
|
|
||||||
|
|
||||||
const document = SwaggerModule.createDocument(app, config);
|
|
||||||
SwaggerModule.setup('api-docs', app, document); // 访问路径为 http://localhost:3000/api-docs
|
|
||||||
|
|
||||||
await app.listen(3002)
|
|
||||||
}
|
}
|
||||||
bootstrap()
|
bootstrap()
|
||||||
|
|
|
@ -1,11 +0,0 @@
|
||||||
import { Module } from '@nestjs/common';
|
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
||||||
import { Project } from './project.entity';
|
|
||||||
import { ProjectRepository } from './project.repository'; // 路径已确认为正确相对路径
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
imports: [TypeOrmModule.forFeature([Project])],
|
|
||||||
providers: [ProjectRepository],
|
|
||||||
exports: [ProjectRepository]
|
|
||||||
})
|
|
||||||
export class ProjectModule {}
|
|
|
@ -1,5 +0,0 @@
|
||||||
import { EntityRepository, Repository } from 'typeorm';
|
|
||||||
import { Project } from './project.entity';
|
|
||||||
|
|
||||||
@EntityRepository(Project)
|
|
||||||
export class ProjectRepository extends Repository<Project> {}
|
|
|
@ -1,12 +0,0 @@
|
||||||
import { Module } from '@nestjs/common';
|
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
||||||
import { User } from './user.entity';
|
|
||||||
import { UserRepository } from './user.repository';
|
|
||||||
import { UserService } from './user.service';
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
imports: [TypeOrmModule.forFeature([User])],
|
|
||||||
providers: [UserService, UserRepository],
|
|
||||||
exports: [UserService]
|
|
||||||
})
|
|
||||||
export class UserModule {}
|
|
|
@ -1,11 +0,0 @@
|
||||||
import { Module } from '@nestjs/common';
|
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
||||||
import { Workspace } from './workspace.entity';
|
|
||||||
import { WorkspaceRepository } from './workspace.repository';
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
imports: [TypeOrmModule.forFeature([Workspace])],
|
|
||||||
providers: [WorkspaceRepository],
|
|
||||||
exports: [WorkspaceRepository, TypeOrmModule] // 添加TypeORM模块导出
|
|
||||||
})
|
|
||||||
export class WorkspaceModule {}
|
|
|
@ -1,5 +0,0 @@
|
||||||
import { EntityRepository, Repository } from 'typeorm';
|
|
||||||
import { Workspace } from './workspace.entity';
|
|
||||||
|
|
||||||
@EntityRepository(Workspace)
|
|
||||||
export class WorkspaceRepository extends Repository<Workspace> {}
|
|
Loading…
Reference in New Issue