Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: Minor refactoring

Blue Mouse012d2a5f7dec
diff --git a/backend/apps/cloud/src/ai/ai-chat.service.ts b/backend/apps/cloud/src/ai/ai-chat.service.ts
index eec0ca528..5d9d1d76e 100644
--- a/backend/apps/cloud/src/ai/ai-chat.service.ts
+++ b/backend/apps/cloud/src/ai/ai-chat.service.ts
@@ -383,13 +383,18 @@ export class AiChatService {
chatId: string,
projectId: string,
): Promise<AiChat | null> {
- return this.aiChatRepository.findOne({
- where: {
- id: chatId,
- project: { id: projectId },
- },
- relations: ['user'],
- })
+ return this.aiChatRepository
+ .createQueryBuilder('chat')
+ .leftJoinAndSelect('chat.user', 'user')
+ .leftJoin(
+ 'chat.parentChat',
+ 'parentChat',
+ 'parentChat.projectId = chat.projectId',
+ )
+ .addSelect(['parentChat.id', 'parentChat.name'])
+ .where('chat.id = :chatId', { chatId })
+ .andWhere('chat.projectId = :projectId', { projectId })
+ .getOne()
}
/**
diff --git a/backend/apps/cloud/src/ai/ai.controller.ts b/backend/apps/cloud/src/ai/ai.controller.ts
index 2750f8f02..663a208a4 100644
--- a/backend/apps/cloud/src/ai/ai.controller.ts
+++ b/backend/apps/cloud/src/ai/ai.controller.ts
@@ -637,8 +637,8 @@ export class AiController {
// Check if the current user is the owner of this chat
const isOwner = uid && chat.user?.id === uid
- const parentChat = chat.parentChatId
- ? await this.aiChatService.findParentSummary(chat.parentChatId, pid)
+ const parentChat = chat.parentChat
+ ? { id: chat.parentChat.id, name: chat.parentChat.name }
: null
return {
diff --git a/backend/apps/cloud/src/ai/dto/chat.dto.ts b/backend/apps/cloud/src/ai/dto/chat.dto.ts
index 3352bf805..5082e377c 100644
--- a/backend/apps/cloud/src/ai/dto/chat.dto.ts
+++ b/backend/apps/cloud/src/ai/dto/chat.dto.ts
@@ -8,6 +8,7 @@ import {
IsOptional,
IsInt,
IsBoolean,
+ IsUUID,
Min,
Max,
ArrayMaxSize,
@@ -208,8 +209,7 @@ export class CreateChatDto {
'ID of the chat this conversation was branched from. Must belong to the same project.',
})
@IsOptional()
- @IsString()
- @MaxLength(36)
+ @IsUUID()
parentChatId?: string
}
@@ -242,7 +242,9 @@ const parseOptionalBool = ({ value }: { value: unknown }) => {
const v = String(value).toLowerCase()
if (v === 'true' || v === '1') return true
if (v === 'false' || v === '0') return false
- return undefined
+ // Preserve the original value so @IsBoolean fails validation (400) instead
+ // of @IsOptional silently treating an invalid input as absent.
+ return value
}
export class GetRecentChatsQueryDto {
@@ -285,6 +287,7 @@ export class GetRecentChatsQueryDto {
description: 'Search query (matches chat names; falls back to messages)',
})
@IsOptional()
+ @Transform(({ value }) => (value === '' ? undefined : value))
@IsString()
@MinLength(2)
@MaxLength(100)
@@ -366,6 +369,7 @@ export class FeedbackDto {
@IsOptional()
@IsInt()
@Min(0)
+ @Max(MAX_MESSAGES_PER_CHAT - 1)
messageIndex?: number
}
diff --git a/backend/apps/cloud/src/ai/entity/ai-chat.entity.ts b/backend/apps/cloud/src/ai/entity/ai-chat.entity.ts
index d6382bc2c..1c183010d 100644
--- a/backend/apps/cloud/src/ai/entity/ai-chat.entity.ts
+++ b/backend/apps/cloud/src/ai/entity/ai-chat.entity.ts
@@ -60,6 +60,11 @@ export class AiChat {
@Column('varchar', { name: 'parent_chat_id', length: 36, nullable: true })
parentChatId: string | null
+ @ApiProperty({ type: () => AiChat, required: false, nullable: true })
+ @ManyToOne(() => AiChat, { nullable: true })
+ @JoinColumn({ name: 'parent_chat_id' })
+ parentChat: AiChat | null
+
@ApiProperty()
@CreateDateColumn()
created: Date
diff --git a/web/app/routes/projects.$id.tsx b/web/app/routes/projects.$id.tsx
index 126871a1e..da74815e8 100644
--- a/web/app/routes/projects.$id.tsx
+++ b/web/app/routes/projects.$id.tsx
@@ -1352,10 +1352,18 @@ export async function action({ request, params }: ActionFunctionArgs) {
case 'submit-ai-chat-feedback': {
const chatId = formData.get('chatId')?.toString()
- const rating = formData.get('rating')?.toString() as 'good' | 'bad'
+ const rawRating = formData.get('rating')?.toString()
const messageIndex = formData.get('messageIndex')?.toString()
const comment = formData.get('comment')?.toString()
+ if (rawRating !== 'good' && rawRating !== 'bad') {
+ return data<ProjectViewActionData>(
+ { intent, error: 'Invalid rating value' },
+ { status: 400 },
+ )
+ }
+ const rating: 'good' | 'bad' = rawRating
+
const body: Record<string, unknown> = { rating }
if (messageIndex !== undefined && messageIndex !== '') {
body.messageIndex = Number(messageIndex)