feat(#82): add prompt formatter service to personality module
- 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
This commit is contained in:
@@ -9,69 +9,81 @@ import {
|
||||
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) {}
|
||||
constructor(
|
||||
private readonly personalitiesService: PersonalitiesService,
|
||||
private readonly promptFormatter: PromptFormatterService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Get all personalities for the current workspace
|
||||
*/
|
||||
@Get()
|
||||
async findAll(
|
||||
@Req() req: any,
|
||||
@Query("isActive") isActive: boolean = true,
|
||||
@Req() req: AuthenticatedRequest,
|
||||
@Query("isActive", new ParseBoolPipe({ optional: true })) isActive?: boolean,
|
||||
): Promise<Personality[]> {
|
||||
return this.personalitiesService.findAll(req.workspaceId, isActive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default personality for the current workspace
|
||||
*/
|
||||
@Get("default")
|
||||
async findDefault(@Req() req: any): Promise<Personality> {
|
||||
async findDefault(@Req() req: AuthenticatedRequest): Promise<Personality> {
|
||||
return this.personalitiesService.findDefault(req.workspaceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific personality by ID
|
||||
*/
|
||||
@Get("formality-levels")
|
||||
getFormalityLevels(): Array<{ level: string; description: string }> {
|
||||
return this.promptFormatter.getFormalityLevels();
|
||||
}
|
||||
|
||||
@Get(":id")
|
||||
async findOne(@Req() req: any, @Param("id") id: string): Promise<Personality> {
|
||||
async findOne(@Req() req: AuthenticatedRequest, @Param("id") id: string): Promise<Personality> {
|
||||
return this.personalitiesService.findOne(req.workspaceId, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new personality
|
||||
*/
|
||||
@Post()
|
||||
async create(@Req() req: any, @Body() dto: CreatePersonalityDto): Promise<Personality> {
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
async create(@Req() req: AuthenticatedRequest, @Body() dto: CreatePersonalityDto): Promise<Personality> {
|
||||
return this.personalitiesService.create(req.workspaceId, dto);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing personality
|
||||
*/
|
||||
@Put(":id")
|
||||
async update(
|
||||
@Req() req: any,
|
||||
@Req() req: AuthenticatedRequest,
|
||||
@Param("id") id: string,
|
||||
@Body() dto: UpdatePersonalityDto,
|
||||
): Promise<Personality> {
|
||||
return this.personalitiesService.update(req.workspaceId, id, dto);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a personality
|
||||
*/
|
||||
@Delete(":id")
|
||||
async remove(@Req() req: any, @Param("id") id: string): Promise<Personality> {
|
||||
@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 };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user