diff --git a/.env.example b/.env.example index 9864a4148209..92477f212ab8 100644 --- a/.env.example +++ b/.env.example @@ -827,3 +827,10 @@ OPENWEATHER_API_KEY= # Skip code challenge method validation (e.g., for AWS Cognito that supports S256 but doesn't advertise it) # When set to true, forces S256 code challenge even if not advertised in .well-known/openid-configuration # MCP_SKIP_CODE_CHALLENGE_CHECK=false + +#======================================# +# Product Feedback # +#======================================# + +# Enable the product feedback feature (thumbs-down stores feedback in MongoDB for review) +# PRODUCT_FEEDBACK_ENABLED=true diff --git a/api/server/index.js b/api/server/index.js index a7ddd47f375d..97fadb72d34b 100644 --- a/api/server/index.js +++ b/api/server/index.js @@ -159,6 +159,7 @@ const startServer = async () => { app.use('/api/tags', routes.tags); app.use('/api/mcp', routes.mcp); + app.use('/api/feedback/issues', routes.feedbackIssues); app.use(ErrorController); diff --git a/api/server/routes/config.js b/api/server/routes/config.js index a2dc5b79d27f..21ba5437b0e2 100644 --- a/api/server/routes/config.js +++ b/api/server/routes/config.js @@ -112,6 +112,7 @@ router.get('/', async function (req, res) { conversationImportMaxFileSize: process.env.CONVERSATION_IMPORT_MAX_FILE_SIZE_BYTES ? parseInt(process.env.CONVERSATION_IMPORT_MAX_FILE_SIZE_BYTES, 10) : 0, + productFeedbackEnabled: isEnabled(process.env.PRODUCT_FEEDBACK_ENABLED), }; const minPasswordLength = parseInt(process.env.MIN_PASSWORD_LENGTH, 10); diff --git a/api/server/routes/feedbackIssues.js b/api/server/routes/feedbackIssues.js new file mode 100644 index 000000000000..0321f2aac8c5 --- /dev/null +++ b/api/server/routes/feedbackIssues.js @@ -0,0 +1,50 @@ +const express = require('express'); +const { v4: uuidv4 } = require('uuid'); +const { logger } = require('@librechat/data-schemas'); +const { requireJwtAuth } = require('~/server/middleware'); +const { ProductFeedback } = require('~/db/models'); + +const router = express.Router(); +router.use(requireJwtAuth); + +router.post('/', async (req, res) => { + const { feedback_reason, feedback_title, feedback_details, feedback_suggested_fix, conversation, metadata, contact } = req.body; + + if (!feedback_reason || !feedback_title) { + return res.status(400).json({ + error: 'feedback_reason and feedback_title are required', + }); + } + + const request_id = req.body.request_id || uuidv4(); + + try { + const record = await ProductFeedback.create({ + request_id, + user: req.user.id, + username: req.user.username || req.user.name || 'unknown', + feedback_reason, + feedback_title, + feedback_details, + feedback_suggested_fix, + conversation, + metadata, + contact, + }); + + logger.info('[feedbackIssues] Feedback saved:', { request_id, id: record._id }); + + return res.json({ + id: record._id.toString(), + request_id, + }); + } catch (error) { + logger.error('[feedbackIssues] Failed to save feedback:', error); + return res.status(500).json({ + error: 'Failed to save feedback', + message: error.message, + }); + } +}); + +module.exports = router; diff --git a/api/server/routes/index.js b/api/server/routes/index.js index f3571099cb5c..624a8d258d91 100644 --- a/api/server/routes/index.js +++ b/api/server/routes/index.js @@ -23,6 +23,7 @@ const tags = require('./tags'); const auth = require('./auth'); const keys = require('./keys'); const user = require('./user'); +const feedbackIssues = require('./feedbackIssues'); const mcp = require('./mcp'); module.exports = { @@ -51,5 +52,6 @@ module.exports = { assistants, categories, staticRoute, + feedbackIssues, accessPermissions, }; diff --git a/client/src/components/Chat/Messages/Feedback.tsx b/client/src/components/Chat/Messages/Feedback.tsx index eea1464e66f3..0b9b3c0a4ba0 100644 --- a/client/src/components/Chat/Messages/Feedback.tsx +++ b/client/src/components/Chat/Messages/Feedback.tsx @@ -1,6 +1,7 @@ import React, { useState, useCallback, useMemo, useEffect } from 'react'; import * as Ariakit from '@ariakit/react'; import { TFeedback, TFeedbackTag, getTagsForRating } from 'librechat-data-provider'; +import { useSubmitProductFeedbackMutation } from 'librechat-data-provider/react-query'; import { Button, OGDialog, @@ -8,6 +9,7 @@ import { OGDialogTitle, ThumbUpIcon, ThumbDownIcon, + useToastContext, } from '@librechat/client'; import { AlertCircle, @@ -19,6 +21,9 @@ import { Lightbulb, Search, } from 'lucide-react'; +import ProductFeedbackModal, { type ProductFeedbackPayload } from './ProductFeedbackModal'; +import { useGetStartupConfig } from '~/data-provider'; +import { NotificationSeverity } from '~/common'; import { useLocalize } from '~/hooks'; import { cn } from '~/utils'; @@ -26,6 +31,11 @@ interface FeedbackProps { handleFeedback: ({ feedback }: { feedback: TFeedback | undefined }) => void; feedback?: TFeedback; isLast?: boolean; + conversationId?: string; + messageId?: string; + endpoint?: string; + model?: string; + agent_id?: string; } const ICONS = { @@ -76,21 +86,22 @@ function FeedbackButtons({ feedback, onFeedback, onOther, + onReportIssue, + productFeedbackEnabled, }: { isLast: boolean; feedback?: TFeedback; onFeedback: (fb: TFeedback | undefined) => void; onOther?: () => void; + onReportIssue?: () => void; + productFeedbackEnabled?: boolean; }) { const localize = useLocalize(); const upStore = Ariakit.usePopoverStore({ placement: 'bottom' }); - const downStore = Ariakit.usePopoverStore({ placement: 'bottom' }); const positiveTags = useMemo(() => getTagsForRating('thumbsUp'), []); - const negativeTags = useMemo(() => getTagsForRating('thumbsDown'), []); const upActive = feedback?.rating === 'thumbsUp' ? feedback.tag?.key : undefined; - const downActive = feedback?.rating === 'thumbsDown' ? feedback.tag?.key : undefined; const handleThumbsUpClick = useCallback( (e: React.MouseEvent) => { @@ -119,26 +130,14 @@ function FeedbackButtons({ const handleThumbsDownClick = useCallback( (e: React.MouseEvent) => { e.preventDefault(); - if (feedback?.rating !== 'thumbsDown') { - downStore.toggle(); - return; - } - - onOther?.(); - }, - [feedback, onOther, downStore], - ); - - const handleDownOption = useCallback( - (tag: TFeedbackTag) => (e: React.MouseEvent) => { - e.preventDefault(); - downStore.hide(); - onFeedback({ rating: 'thumbsDown', tag }); - if (tag.key === 'other') { + if (productFeedbackEnabled) { + onReportIssue?.(); + } else { + onFeedback({ rating: 'thumbsDown' }); onOther?.(); } }, - [onFeedback, onOther, downStore], + [productFeedbackEnabled, onReportIssue, onFeedback, onOther], ); return ( @@ -177,39 +176,15 @@ function FeedbackButtons({ - - - - } - /> - -
- {negativeTags.map((tag) => ( - - ))} -
-
+ + ); } @@ -229,10 +204,20 @@ export default function Feedback({ isLast = false, handleFeedback, feedback: initialFeedback, + conversationId, + messageId, + endpoint, + model, + agent_id, }: FeedbackProps) { const localize = useLocalize(); + const { showToast } = useToastContext(); const [openDialog, setOpenDialog] = useState(false); + const [openProductFeedback, setOpenProductFeedback] = useState(false); const [feedback, setFeedback] = useState(initialFeedback); + const { data: startupConfig } = useGetStartupConfig(); + const productFeedbackEnabled = startupConfig?.productFeedbackEnabled === true; + const submitProductFeedback = useSubmitProductFeedbackMutation(); useEffect(() => { setFeedback(initialFeedback); @@ -257,6 +242,8 @@ export default function Feedback({ const handleOtherOpen = useCallback(() => setOpenDialog(true), []); + const handleReportIssueOpen = useCallback(() => setOpenProductFeedback(true), []); + const handleTextChange = (e: React.ChangeEvent) => { setFeedback((prev) => (prev ? { ...prev, text: e.target.value } : undefined)); }; @@ -275,6 +262,72 @@ export default function Feedback({ setOpenDialog(false); }, [handleFeedback]); + const handleProductFeedbackSubmit = useCallback( + (payload: ProductFeedbackPayload) => { + // Store the full feedback payload as JSON in the existing feedback text field + handleFeedback({ + feedback: { + rating: 'thumbsDown', + text: JSON.stringify({ + request_id: payload.request_id, + feedback_reason: payload.feedback_reason, + feedback_title: payload.feedback_title, + feedback_details: payload.feedback_details, + feedback_suggested_fix: payload.feedback_suggested_fix, + conversation_id: payload.conversation_id, + message_id: payload.message_id, + messages: payload.messages, + endpoint: payload.endpoint, + model: payload.model, + agent_id: payload.agent_id, + }), + }, + }); + + const mutationPayload = { + request_id: payload.request_id, + user: { username: payload.user_email ?? payload.user_id ?? '' }, + timestamp: new Date().toISOString(), + feedback_reason: payload.feedback_reason, + feedback_title: payload.feedback_title, + feedback_details: payload.feedback_details, + feedback_suggested_fix: payload.feedback_suggested_fix, + conversation: { + conversation_id: payload.conversation_id, + message_id: payload.message_id, + last_n_messages: payload.messages, + }, + metadata: { + librechat_version: '', + client: 'web', + endpoint: payload.endpoint, + model: payload.model, + agent_id: payload.agent_id, + }, + ...(payload.user_email ? { contact: { email: payload.user_email } } : {}), + }; + + submitProductFeedback.mutate(mutationPayload, { + onSuccess: () => { + showToast({ + message: localize('com_ui_product_feedback_success' as Parameters[0]), + severity: NotificationSeverity.SUCCESS, + showIcon: true, + duration: 5000, + }); + }, + onError: () => { + showToast({ + message: localize('com_ui_product_feedback_error' as Parameters[0]), + severity: NotificationSeverity.ERROR, + showIcon: true, + }); + }, + }); + }, + [submitProductFeedback, handleFeedback, showToast, localize], + ); + const renderSingleFeedbackButton = () => { if (!feedback) return null; const isThumbsUp = feedback.rating === 'thumbsUp'; @@ -311,6 +364,8 @@ export default function Feedback({ feedback={feedback} onFeedback={handleButtonFeedback} onOther={handleOtherOpen} + onReportIssue={handleReportIssueOpen} + productFeedbackEnabled={productFeedbackEnabled} /> )} @@ -336,6 +391,18 @@ export default function Feedback({ + {productFeedbackEnabled && conversationId && messageId && ( + + )} ); } diff --git a/client/src/components/Chat/Messages/HoverButtons.tsx b/client/src/components/Chat/Messages/HoverButtons.tsx index 5d60223d08a1..0cd42a02cc83 100644 --- a/client/src/components/Chat/Messages/HoverButtons.tsx +++ b/client/src/components/Chat/Messages/HoverButtons.tsx @@ -245,7 +245,16 @@ const HoverButtons = ({ {/* Feedback Buttons */} {!isCreatedByUser && handleFeedback != null && ( - + )} {/* Regenerate Button */} diff --git a/client/src/components/Chat/Messages/ProductFeedbackModal.tsx b/client/src/components/Chat/Messages/ProductFeedbackModal.tsx new file mode 100644 index 000000000000..623c4e691279 --- /dev/null +++ b/client/src/components/Chat/Messages/ProductFeedbackModal.tsx @@ -0,0 +1,277 @@ +import React, { useState, useCallback } from 'react'; +import { useQueryClient } from '@tanstack/react-query'; +import { QueryKeys } from 'librechat-data-provider'; +import type { TMessage } from 'librechat-data-provider'; +import { Button, OGDialog, OGDialogContent, OGDialogTitle } from '@librechat/client'; +import { useLocalize, useAuthContext } from '~/hooks'; +import { cn } from '~/utils'; + +interface ProductFeedbackModalProps { + open: boolean; + onOpenChange: (open: boolean) => void; + conversationId: string; + messageId: string; + endpoint?: string; + model?: string; + agent_id?: string; + onSubmit?: (payload: ProductFeedbackPayload) => void; + onSuccess?: (issueUrl: string) => void; +} + +interface ConversationMessage { + timestamp: string; + is_user: boolean; + text: string; +} + +export interface ProductFeedbackPayload { + request_id: string; + feedback_reason: string; + feedback_title: string; + feedback_details: string; + feedback_suggested_fix: string; + conversation_id: string; + message_id: string; + user_id: string | undefined; + user_email: string | undefined; + messages: ConversationMessage[]; + endpoint?: string; + model?: string; + agent_id?: string; +} + +type FeedbackReason = + | 'incorrect' + | 'unfaithful' + | 'safety_or_legal_concern' + | 'style_tone_conciseness' + | 'other'; + +const FEEDBACK_REASONS: { value: FeedbackReason; labelKey: string }[] = [ + { value: 'incorrect', labelKey: 'com_ui_product_feedback_reason_incorrect' }, + { value: 'unfaithful', labelKey: 'com_ui_product_feedback_reason_unfaithful' }, + { value: 'safety_or_legal_concern', labelKey: 'com_ui_product_feedback_reason_safety' }, + { value: 'style_tone_conciseness', labelKey: 'com_ui_product_feedback_reason_style' }, + { value: 'other', labelKey: 'com_ui_product_feedback_reason_other' }, +]; + +export default function ProductFeedbackModal({ + open, + onOpenChange, + conversationId, + messageId, + endpoint, + model, + agent_id, + onSubmit, + onSuccess, +}: ProductFeedbackModalProps) { + const localize = useLocalize(); + const { user } = useAuthContext(); + const queryClient = useQueryClient(); + + const [feedbackReason, setFeedbackReason] = useState(''); + const [feedbackTitle, setFeedbackTitle] = useState(''); + const [feedbackDetails, setFeedbackDetails] = useState(''); + const [feedbackSuggestedFix, setFeedbackSuggestedFix] = useState(''); + const [error, setError] = useState(null); + const [isSubmitting, setIsSubmitting] = useState(false); + + const isSubmitDisabled = !feedbackReason || !feedbackTitle.trim() || isSubmitting; + + const resetForm = useCallback(() => { + setFeedbackReason(''); + setFeedbackTitle(''); + setFeedbackDetails(''); + setFeedbackSuggestedFix(''); + setError(null); + setIsSubmitting(false); + }, []); + + const handleOpenChange = useCallback( + (isOpen: boolean) => { + if (!isOpen) { + resetForm(); + } + onOpenChange(isOpen); + }, + [onOpenChange, resetForm], + ); + + const handleSubmit = useCallback(async () => { + if (isSubmitDisabled) { + return; + } + + setError(null); + setIsSubmitting(true); + + try { + const messages = + queryClient.getQueryData([QueryKeys.messages, conversationId]) ?? []; + + const lastMessages = messages.slice(-10).map((msg) => ({ + timestamp: msg.createdAt ?? new Date().toISOString(), + is_user: msg.isCreatedByUser, + text: msg.text, + })); + + const requestId = crypto.randomUUID(); + + const payload: ProductFeedbackPayload = { + request_id: requestId, + feedback_reason: feedbackReason, + feedback_title: feedbackTitle.trim(), + feedback_details: feedbackDetails.trim(), + feedback_suggested_fix: feedbackSuggestedFix.trim(), + conversation_id: conversationId, + message_id: messageId, + user_id: user?.id, + user_email: user?.email, + messages: lastMessages, + endpoint, + model, + agent_id, + }; + + if (onSubmit) { + onSubmit(payload); + } + + resetForm(); + onOpenChange(false); + onSuccess?.(''); + } catch { + setError(localize('com_ui_product_feedback_error' as Parameters[0])); + } finally { + setIsSubmitting(false); + } + }, [ + isSubmitDisabled, + queryClient, + conversationId, + feedbackReason, + feedbackTitle, + feedbackDetails, + feedbackSuggestedFix, + messageId, + user, + endpoint, + model, + agent_id, + onSubmit, + onSuccess, + onOpenChange, + resetForm, + localize, + ]); + + return ( + + + + {localize('com_ui_product_feedback_title' as Parameters[0])} + + +
+ {/* Feedback Reason Pills */} +
+ +
+ {FEEDBACK_REASONS.map(({ value, labelKey }) => ( + + ))} +
+
+ + {/* Feedback Title */} +
+ [0], + )} + value={feedbackTitle} + onChange={(e) => setFeedbackTitle(e.target.value)} + maxLength={200} + /> +
+ + {/* Feedback Details */} +
+ +