- Add PromptFormatterService for formatting system prompts based on personality - Support context variable interpolation (userName, workspaceName, etc.) - Add formality level modifiers (VERY_CASUAL to VERY_FORMAL) - Add template validation for custom variables - Add preview endpoint for formatted prompts - Fix UpdatePersonalityDto to avoid @nestjs/mapped-types dependency - Update PersonalitiesController with new endpoints - Add comprehensive tests (33 passing tests) Closes #82
90 lines
2.6 KiB
TypeScript
90 lines
2.6 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Put,
|
|
Delete,
|
|
Body,
|
|
Param,
|
|
Query,
|
|
UseGuards,
|
|
Req,
|
|
ParseBoolPipe,
|
|
HttpCode,
|
|
HttpStatus,
|
|
} from "@nestjs/common";
|
|
import { AuthGuard } from "../auth/guards/auth.guard";
|
|
import { PersonalitiesService } from "./personalities.service";
|
|
import { PromptFormatterService, PromptContext } from "./services/prompt-formatter.service";
|
|
import { CreatePersonalityDto, UpdatePersonalityDto } from "./dto";
|
|
import { Personality } from "./entities/personality.entity";
|
|
|
|
interface AuthenticatedRequest {
|
|
user: { id: string };
|
|
workspaceId: string;
|
|
}
|
|
|
|
@Controller("personalities")
|
|
@UseGuards(AuthGuard)
|
|
export class PersonalitiesController {
|
|
constructor(
|
|
private readonly personalitiesService: PersonalitiesService,
|
|
private readonly promptFormatter: PromptFormatterService,
|
|
) {}
|
|
|
|
@Get()
|
|
async findAll(
|
|
@Req() req: AuthenticatedRequest,
|
|
@Query("isActive", new ParseBoolPipe({ optional: true })) isActive?: boolean,
|
|
): Promise<Personality[]> {
|
|
return this.personalitiesService.findAll(req.workspaceId, isActive);
|
|
}
|
|
|
|
@Get("default")
|
|
async findDefault(@Req() req: AuthenticatedRequest): Promise<Personality> {
|
|
return this.personalitiesService.findDefault(req.workspaceId);
|
|
}
|
|
|
|
@Get("formality-levels")
|
|
getFormalityLevels(): Array<{ level: string; description: string }> {
|
|
return this.promptFormatter.getFormalityLevels();
|
|
}
|
|
|
|
@Get(":id")
|
|
async findOne(@Req() req: AuthenticatedRequest, @Param("id") id: string): Promise<Personality> {
|
|
return this.personalitiesService.findOne(req.workspaceId, id);
|
|
}
|
|
|
|
@Post()
|
|
@HttpCode(HttpStatus.CREATED)
|
|
async create(@Req() req: AuthenticatedRequest, @Body() dto: CreatePersonalityDto): Promise<Personality> {
|
|
return this.personalitiesService.create(req.workspaceId, dto);
|
|
}
|
|
|
|
@Put(":id")
|
|
async update(
|
|
@Req() req: AuthenticatedRequest,
|
|
@Param("id") id: string,
|
|
@Body() dto: UpdatePersonalityDto,
|
|
): Promise<Personality> {
|
|
return this.personalitiesService.update(req.workspaceId, id, dto);
|
|
}
|
|
|
|
@Delete(":id")
|
|
@HttpCode(HttpStatus.OK)
|
|
async remove(@Req() req: AuthenticatedRequest, @Param("id") id: string): Promise<Personality> {
|
|
return this.personalitiesService.remove(req.workspaceId, id);
|
|
}
|
|
|
|
@Post(":id/preview")
|
|
async previewPrompt(
|
|
@Req() req: AuthenticatedRequest,
|
|
@Param("id") id: string,
|
|
@Body() context?: PromptContext,
|
|
): Promise<{ systemPrompt: string }> {
|
|
const personality = await this.personalitiesService.findOne(req.workspaceId, id);
|
|
const { systemPrompt } = this.promptFormatter.formatPrompt(personality, context);
|
|
return { systemPrompt };
|
|
}
|
|
}
|