55 lines
2.2 KiB
TypeScript
55 lines
2.2 KiB
TypeScript
import { Controller, Post, Get, Delete, Patch, Body, Param, Query, UseGuards, Req, ParseIntPipe } from '@nestjs/common';
|
|
import { ApiTags, ApiBearerAuth, ApiBody } from '@nestjs/swagger';
|
|
import { AuthGuard } from '@nestjs/passport';
|
|
import { SandboxService } from './sandbox.service';
|
|
import { ChatOptions } from '../ai/ai-gateway.service';
|
|
|
|
@ApiTags('AI沙箱')
|
|
@Controller('sandbox')
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@ApiBearerAuth()
|
|
export class SandboxController {
|
|
constructor(private sandboxService: SandboxService) {}
|
|
|
|
@Post('chat')
|
|
@ApiBody({ schema: { example: { conversationId: 'uuid', model: 'general', messages: [{ role: 'user', content: 'hi' }], temperature: 0.7, top_p: 1, max_tokens: 2000 } } })
|
|
async chat(@Req() req: any, @Body() body: { conversationId?: string; model: string; messages: { role: string; content: string }[] } & ChatOptions) {
|
|
return this.sandboxService.chat(req.user.userId, body.conversationId, body.model, body.messages, body);
|
|
}
|
|
|
|
@Get('sessions')
|
|
async sessions(@Req() req: any, @Query() query: { page?: number; pageSize?: number; search?: string }) {
|
|
return this.sandboxService.getSessions(req.user.userId, query);
|
|
}
|
|
|
|
@Get('sessions/:id')
|
|
async getSession(@Req() req: any, @Param('id', ParseIntPipe) id: number) {
|
|
return this.sandboxService.getSession(req.user.userId, id);
|
|
}
|
|
|
|
@Patch('sessions/:id/feedback')
|
|
async setFeedback(@Req() req: any, @Param('id', ParseIntPipe) id: number, @Body() body: { feedback: 'LIKE' | 'DISLIKE' | null }) {
|
|
return this.sandboxService.setFeedback(req.user.userId, id, body.feedback);
|
|
}
|
|
|
|
@Delete('sessions/:id')
|
|
async deleteSession(@Req() req: any, @Param('id', ParseIntPipe) id: number) {
|
|
return this.sandboxService.deleteSession(req.user.userId, id);
|
|
}
|
|
|
|
@Get('quota')
|
|
async quota(@Req() req: any) {
|
|
return this.sandboxService.getQuota(req.user.userId);
|
|
}
|
|
|
|
@Post('sessions/:id/share')
|
|
async share(@Req() req: any, @Param('id', ParseIntPipe) id: number) {
|
|
return this.sandboxService.generateShareToken(req.user.userId, id);
|
|
}
|
|
|
|
@Patch('sessions/:id/rename')
|
|
async rename(@Req() req: any, @Param('id', ParseIntPipe) id: number, @Body() body: { title: string }) {
|
|
return this.sandboxService.renameSession(req.user.userId, id, body.title);
|
|
}
|
|
}
|