Compare commits
2 Commits
51e95be41b
...
ae7c88d213
Author | SHA1 | Date |
---|---|---|
|
ae7c88d213 | |
|
f27d9dd710 |
|
@ -0,0 +1,8 @@
|
|||
**添加规则文件可帮助模型精准理解你的编码偏好,如框架、代码风格等**
|
||||
**规则文件只对当前工程生效,单文件限制10000字符。如果无需将该文件提交到远程 Git 仓库,请将其添加到 .gitignore**
|
||||
# 技术栈规范
|
||||
|
||||
## 基础环境
|
||||
1. 语言:TypeScript
|
||||
2. nodejs 使用版本v22
|
||||
3. 执行node, npm 相关命令时先执行`nvm use v22`切换到v22版本
|
|
@ -0,0 +1 @@
|
|||
require('./dist/main');
|
File diff suppressed because it is too large
Load Diff
|
@ -6,6 +6,7 @@
|
|||
"scripts": {
|
||||
"build": "rimraf dist && tsc",
|
||||
"start": "npm run build && node dist/main.js",
|
||||
"start:dev": "nest start --watch",
|
||||
"dev": "ts-node -r tsconfig-paths/register src/main.ts",
|
||||
"typeorma": "typeorm-ts-node-commonjs -d dist/data-source.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
|
@ -17,10 +18,14 @@
|
|||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@nestjs/axios": "^4.0.0",
|
||||
"@nestjs/cli": "^11.0.7",
|
||||
"@nestjs/common": "^11.1.3",
|
||||
"@nestjs/config": "^4.0.2",
|
||||
"@nestjs/core": "^11.1.3",
|
||||
"@nestjs/passport": "^11.0.5",
|
||||
"@nestjs/platform-express": "^11.1.3",
|
||||
"@nestjs/swagger": "^11.2.0",
|
||||
"@nestjs/typeorm": "^11.0.0",
|
||||
"bcrypt": "^6.0.0",
|
||||
"class-validator": "^0.14.2",
|
||||
|
@ -30,6 +35,7 @@
|
|||
"pg": "^8.16.0",
|
||||
"reflect-metadata": "^0.2.2",
|
||||
"rxjs": "^7.8.2",
|
||||
"swagger-ui-express": "^5.0.1",
|
||||
"uid": "^2.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
import { EntityRepository, Repository } from 'typeorm';
|
||||
import { AiConfig } from './ai-config.entity';
|
||||
|
||||
@EntityRepository(AiConfig)
|
||||
export class AiConfigRepository extends Repository<AiConfig> {}
|
|
@ -0,0 +1,43 @@
|
|||
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']
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
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 {}
|
|
@ -0,0 +1,30 @@
|
|||
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,
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
import { IsString } from 'class-validator';
|
||||
|
||||
export class CreateAiConfigDto {
|
||||
@IsString()
|
||||
apiUrl!: string;
|
||||
|
||||
@IsString()
|
||||
apiKey!: string;
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
import { Controller } from '@nestjs/common';
|
||||
|
||||
@Controller()
|
||||
export class AppController {}
|
|
@ -1,25 +1,46 @@
|
|||
import { Module } from '@nestjs/common';
|
||||
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 { AiModule } from './ai/ai.module';
|
||||
import { User } from './user/user.entity';
|
||||
import { Workspace } from './workspace/workspace.entity';
|
||||
import { Project } from './project/project.entity';
|
||||
import { AiConfig } from './ai/ai-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({
|
||||
imports: [
|
||||
TypeOrmModule.forRoot({
|
||||
type: 'postgres',
|
||||
host: 'localhost',
|
||||
port: 5432,
|
||||
username: 'postgres',
|
||||
password: 'postgres',
|
||||
database: 'aiframe',
|
||||
entities: [User, Workspace, Project, AiConfig, PluginConfig],
|
||||
synchronize: true,
|
||||
}),
|
||||
AuthModule,
|
||||
],
|
||||
imports: MODULES,
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
})
|
||||
export class AppModule {}
|
|
@ -0,0 +1,8 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
getHello(): string {
|
||||
return 'Hello World!';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
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,7 +12,11 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
|||
});
|
||||
}
|
||||
|
||||
async validate(payload: any) {
|
||||
return { userId: payload.sub, email: payload.email };
|
||||
async validate(payload: { sub: number; email: string }, done: (error: any, user?: any, info?: any) => void) {
|
||||
const user = {
|
||||
sub: payload.sub,
|
||||
email: payload.email,
|
||||
};
|
||||
done(null, user);
|
||||
}
|
||||
}
|
|
@ -1,8 +1,18 @@
|
|||
import { NestFactory } from '@nestjs/core'
|
||||
import { AppModule } from './app.module'
|
||||
import 'reflect-metadata'
|
||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
||||
async function bootstrap () {
|
||||
const app = await NestFactory.create(AppModule)
|
||||
await app.listen(3000)
|
||||
const config = new DocumentBuilder()
|
||||
.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()
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
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 {}
|
|
@ -0,0 +1,5 @@
|
|||
import { EntityRepository, Repository } from 'typeorm';
|
||||
import { Project } from './project.entity';
|
||||
|
||||
@EntityRepository(Project)
|
||||
export class ProjectRepository extends Repository<Project> {}
|
|
@ -0,0 +1,12 @@
|
|||
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 {}
|
|
@ -0,0 +1,11 @@
|
|||
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 {}
|
|
@ -0,0 +1,5 @@
|
|||
import { EntityRepository, Repository } from 'typeorm';
|
||||
import { Workspace } from './workspace.entity';
|
||||
|
||||
@EntityRepository(Workspace)
|
||||
export class WorkspaceRepository extends Repository<Workspace> {}
|
Loading…
Reference in New Issue