feat: Implement community prompts feature

- Added a new endpoint to fetch community-curated prompts in `search_spaces_routes.py`.
- Enhanced `SetupPromptStep` and `PromptConfigManager` components to display and utilize community prompts.
- Introduced UI elements for selecting and applying prompts, including categories and expandable views.
- Improved state management for selected prompts and custom instructions.
This commit is contained in:
DESKTOP-RTLN3BA\$punk 2025-11-21 16:28:00 -08:00
parent 782f626240
commit 70f3381d7e
5 changed files with 652 additions and 8 deletions

View file

@ -1,16 +1,29 @@
"use client";
import { Info, RotateCcw, Save } from "lucide-react";
import {
ChevronDown,
ChevronUp,
ExternalLink,
Info,
RotateCcw,
Save,
Sparkles,
User,
} from "lucide-react";
import { useEffect, useState } from "react";
import { toast } from "sonner";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Label } from "@/components/ui/label";
import { ScrollArea } from "@/components/ui/scroll-area";
import { Separator } from "@/components/ui/separator";
import { Skeleton } from "@/components/ui/skeleton";
import { Switch } from "@/components/ui/switch";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Textarea } from "@/components/ui/textarea";
import { type CommunityPrompt, useCommunityPrompts } from "@/hooks/use-community-prompts";
import { useSearchSpace } from "@/hooks/use-search-space";
interface PromptConfigManagerProps {
@ -22,11 +35,15 @@ export function PromptConfigManager({ searchSpaceId }: PromptConfigManagerProps)
searchSpaceId,
autoFetch: true,
});
const { prompts, loading: loadingPrompts } = useCommunityPrompts();
const [enableCitations, setEnableCitations] = useState(true);
const [customInstructions, setCustomInstructions] = useState("");
const [saving, setSaving] = useState(false);
const [hasChanges, setHasChanges] = useState(false);
const [selectedPromptKey, setSelectedPromptKey] = useState<string | null>(null);
const [expandedPrompts, setExpandedPrompts] = useState<Set<string>>(new Set());
const [selectedCategory, setSelectedCategory] = useState("all");
// Initialize state from fetched search space
useEffect(() => {
@ -97,10 +114,39 @@ export function PromptConfigManager({ searchSpaceId }: PromptConfigManagerProps)
if (searchSpace) {
setEnableCitations(searchSpace.citations_enabled);
setCustomInstructions(searchSpace.qna_custom_instructions || "");
setSelectedPromptKey(null);
setHasChanges(false);
}
};
const handleSelectCommunityPrompt = (promptKey: string, promptValue: string) => {
setCustomInstructions(promptValue);
setSelectedPromptKey(promptKey);
toast.success("Community prompt applied");
};
const toggleExpand = (promptKey: string) => {
const newExpanded = new Set(expandedPrompts);
if (newExpanded.has(promptKey)) {
newExpanded.delete(promptKey);
} else {
newExpanded.add(promptKey);
}
setExpandedPrompts(newExpanded);
};
// Get unique categories
const categories = Array.from(new Set(prompts.map((p) => p.category || "general")));
const filteredPrompts =
selectedCategory === "all"
? prompts
: prompts.filter((p) => (p.category || "general") === selectedCategory);
const truncateText = (text: string, maxLength: number = 150) => {
if (text.length <= maxLength) return text;
return text.substring(0, maxLength) + "...";
};
if (loading) {
return (
<div className="space-y-6">
@ -188,6 +234,140 @@ export function PromptConfigManager({ searchSpaceId }: PromptConfigManagerProps)
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
{/* Community Prompts Section */}
{!loadingPrompts && prompts.length > 0 && (
<div className="space-y-2">
<Label className="text-base font-medium flex items-center gap-2">
<Sparkles className="h-4 w-4" />
Community Prompts Library
</Label>
<p className="text-sm text-muted-foreground">
Browse {prompts.length} curated prompts from the community
</p>
<Card className="border-dashed">
<CardContent className="pt-4">
<Tabs
value={selectedCategory}
onValueChange={setSelectedCategory}
className="w-full"
>
<TabsList className="grid w-full grid-cols-5 mb-4">
<TabsTrigger value="all" className="text-xs">
All ({prompts.length})
</TabsTrigger>
{categories.map((category) => (
<TabsTrigger key={category} value={category} className="text-xs capitalize">
{category} (
{prompts.filter((p) => (p.category || "general") === category).length})
</TabsTrigger>
))}
</TabsList>
<ScrollArea className="h-[350px] pr-4">
<div className="space-y-3">
{filteredPrompts.map((prompt) => {
const isExpanded = expandedPrompts.has(prompt.key);
const isSelected = selectedPromptKey === prompt.key;
const displayText = isExpanded
? prompt.value
: truncateText(prompt.value, 120);
return (
<div
key={prompt.key}
className={`p-4 rounded-lg border transition-all ${
isSelected
? "border-primary bg-accent/50"
: "border-border hover:border-primary/50 hover:bg-accent/30"
}`}
>
<div className="flex items-start justify-between gap-2 mb-2">
<div className="flex items-center gap-2 flex-wrap flex-1">
<Badge variant="outline" className="text-xs font-medium">
{prompt.key.replace(/_/g, " ")}
</Badge>
{prompt.category && (
<Badge variant="secondary" className="text-xs capitalize">
{prompt.category}
</Badge>
)}
{isSelected && (
<Badge variant="default" className="text-xs">
Selected
</Badge>
)}
</div>
{prompt.link && (
<a
href={prompt.link}
target="_blank"
rel="noopener noreferrer"
className="text-muted-foreground hover:text-primary shrink-0"
title="View source"
>
<ExternalLink className="h-4 w-4" />
</a>
)}
</div>
<p className="text-sm text-foreground mb-3 whitespace-pre-wrap">
{displayText}
</p>
<div className="flex items-center justify-between gap-2">
<div className="flex items-center gap-2 text-xs text-muted-foreground">
<User className="h-3 w-3" />
<span>{prompt.author}</span>
</div>
<div className="flex items-center gap-2">
{prompt.value.length > 120 && (
<Button
type="button"
variant="ghost"
size="sm"
onClick={() => toggleExpand(prompt.key)}
className="h-7 text-xs"
>
{isExpanded ? (
<>
<ChevronUp className="h-3 w-3 mr-1" />
Show less
</>
) : (
<>
<ChevronDown className="h-3 w-3 mr-1" />
Read more
</>
)}
</Button>
)}
<Button
type="button"
variant={isSelected ? "default" : "secondary"}
size="sm"
onClick={() =>
handleSelectCommunityPrompt(prompt.key, prompt.value)
}
className="h-7 text-xs"
>
{isSelected ? "Applied" : "Use This"}
</Button>
</div>
</div>
</div>
);
})}
</div>
</ScrollArea>
</Tabs>
</CardContent>
</Card>
</div>
)}
<Separator />
<div className="space-y-2">
<Label htmlFor="custom-instructions-settings" className="text-base font-medium">
Your System Instructions
@ -200,7 +380,10 @@ export function PromptConfigManager({ searchSpaceId }: PromptConfigManagerProps)
id="custom-instructions-settings"
placeholder="E.g., Always provide practical examples, be concise, focus on technical details, use simple language..."
value={customInstructions}
onChange={(e) => setCustomInstructions(e.target.value)}
onChange={(e) => {
setCustomInstructions(e.target.value);
setSelectedPromptKey(null);
}}
rows={8}
className="resize-none font-mono text-sm"
/>
@ -212,7 +395,10 @@ export function PromptConfigManager({ searchSpaceId }: PromptConfigManagerProps)
<Button
variant="ghost"
size="sm"
onClick={() => setCustomInstructions("")}
onClick={() => {
setCustomInstructions("");
setSelectedPromptKey(null);
}}
className="h-auto py-1 px-2 text-xs"
>
Clear