mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-26 01:06:23 +02:00
show podcast staleness and suggest user to re-generate
This commit is contained in:
parent
e47b1cb4c5
commit
b02b094a24
5 changed files with 112 additions and 17 deletions
|
|
@ -27,6 +27,8 @@ interface ChatInterfaceContext {
|
|||
setIsChatPannelOpen: (value: boolean) => void;
|
||||
chat_id: string;
|
||||
chatDetails: ChatDetails | null;
|
||||
podcast: PodcastItem | null;
|
||||
setPodcast: (podcast: PodcastItem | null) => void;
|
||||
}
|
||||
|
||||
export const chatInterfaceContext = createContext<ChatInterfaceContext | null>(null);
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
"use client";
|
||||
|
||||
import { Pencil, Podcast } from "lucide-react";
|
||||
import { AlertCircle, Pencil, Podcast, RefreshCw } from "lucide-react";
|
||||
import { useCallback, useContext, useTransition } from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { chatInterfaceContext } from "../ChatInterface";
|
||||
import { getPodcastStalenessMessage, isPodcastStale } from "../PodcastUtils";
|
||||
import type { GeneratePodcastRequest } from "./ChatPanelContainer";
|
||||
import { ConfigModal } from "./ConfigModal";
|
||||
|
||||
|
|
@ -17,9 +18,13 @@ export function ChatPanelView(props: ChatPanelViewProps) {
|
|||
throw new Error("chatInterfaceContext must be used within a ChatProvider");
|
||||
}
|
||||
|
||||
const { isChatPannelOpen, setIsChatPannelOpen, chatDetails } = context;
|
||||
const { isChatPannelOpen, setIsChatPannelOpen, chatDetails, podcast } = context;
|
||||
const { generatePodcast } = props;
|
||||
|
||||
// Check if podcast is stale
|
||||
const podcastIsStale =
|
||||
podcast && chatDetails && isPodcastStale(chatDetails.state_version, podcast.chat_state_version);
|
||||
|
||||
const handleGeneratePost = useCallback(async () => {
|
||||
if (!chatDetails) return;
|
||||
await generatePodcast({
|
||||
|
|
@ -28,7 +33,9 @@ export function ChatPanelView(props: ChatPanelViewProps) {
|
|||
search_space_id: chatDetails.search_space_id,
|
||||
podcast_title: chatDetails.title,
|
||||
});
|
||||
}, [chatDetails]);
|
||||
}, [chatDetails, generatePodcast]);
|
||||
|
||||
console.log("podcastIsStale", podcastIsStale);
|
||||
|
||||
return (
|
||||
<div className="w-full">
|
||||
|
|
@ -37,27 +44,67 @@ export function ChatPanelView(props: ChatPanelViewProps) {
|
|||
"w-full cursor-pointer h-full p-4 border-b",
|
||||
!isChatPannelOpen && "flex items-center justify-center"
|
||||
)}
|
||||
title="Generate Podcast"
|
||||
title={podcastIsStale ? "Regenerate Podcast" : "Generate Podcast"}
|
||||
>
|
||||
{isChatPannelOpen ? (
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleGeneratePost}
|
||||
className=" space-y-3 rounded-xl p-3 bg-gradient-to-r from-slate-400/50 to-slate-200/50 dark:from-slate-400/30 dark:to-slate-800/60"
|
||||
>
|
||||
<div className="w-full flex items-center justify-between">
|
||||
<Podcast strokeWidth={1} />
|
||||
<ConfigModal generatePodcast={generatePodcast} />
|
||||
</div>
|
||||
<p>Generate Podcast</p>
|
||||
</button>
|
||||
<div className="space-y-3">
|
||||
{/* Show stale podcast warning if applicable */}
|
||||
{podcastIsStale && (
|
||||
<div className="rounded-lg p-3 bg-amber-50 dark:bg-amber-950/30 border border-amber-200 dark:border-amber-800">
|
||||
<div className="flex gap-2 items-start">
|
||||
<AlertCircle className="h-4 w-4 text-amber-600 dark:text-amber-500 mt-0.5 flex-shrink-0" />
|
||||
<div className="text-sm text-amber-800 dark:text-amber-200">
|
||||
<p className="font-medium">Podcast is outdated</p>
|
||||
<p className="text-xs mt-1 opacity-90">
|
||||
{getPodcastStalenessMessage(
|
||||
chatDetails?.state_version || 0,
|
||||
podcast?.chat_state_version
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Generate/Regenerate button */}
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleGeneratePost}
|
||||
className={cn(
|
||||
"w-full space-y-3 rounded-xl p-3 transition-colors",
|
||||
podcastIsStale
|
||||
? "bg-gradient-to-r from-amber-400/50 to-orange-300/50 dark:from-amber-500/30 dark:to-orange-600/30 hover:from-amber-400/60 hover:to-orange-300/60"
|
||||
: "bg-gradient-to-r from-slate-400/50 to-slate-200/50 dark:from-slate-400/30 dark:to-slate-800/60 hover:from-slate-400/60 hover:to-slate-200/60"
|
||||
)}
|
||||
>
|
||||
<div className="w-full flex items-center justify-between">
|
||||
{podcastIsStale ? (
|
||||
<RefreshCw strokeWidth={1} className="h-5 w-5" />
|
||||
) : (
|
||||
<Podcast strokeWidth={1} className="h-5 w-5" />
|
||||
)}
|
||||
<ConfigModal generatePodcast={generatePodcast} />
|
||||
</div>
|
||||
<p className="text-sm font-medium text-left">
|
||||
{podcastIsStale ? "Regenerate Podcast" : "Generate Podcast"}
|
||||
</p>
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<button
|
||||
title="Generate Podcast"
|
||||
title={podcastIsStale ? "Regenerate Podcast" : "Generate Podcast"}
|
||||
type="button"
|
||||
onClick={() => setIsChatPannelOpen(!isChatPannelOpen)}
|
||||
className={cn(
|
||||
"p-2 rounded-full hover:bg-muted transition-colors",
|
||||
podcastIsStale && "text-amber-600 dark:text-amber-500"
|
||||
)}
|
||||
>
|
||||
<Podcast strokeWidth={1} />
|
||||
{podcastIsStale ? (
|
||||
<RefreshCw strokeWidth={1} className="h-5 w-5" />
|
||||
) : (
|
||||
<Podcast strokeWidth={1} className="h-5 w-5" />
|
||||
)}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
|
|
|||
43
surfsense_web/components/chat/PodcastUtils.ts
Normal file
43
surfsense_web/components/chat/PodcastUtils.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/**
|
||||
* Determines if a podcast is stale compared to the current chat state.
|
||||
* A podcast is considered stale if:
|
||||
* - The chat's current state_version is greater than the podcast's chat_state_version
|
||||
*
|
||||
* @param chatVersion - The current state_version of the chat
|
||||
* @param podcastVersion - The chat_state_version stored when the podcast was generated (nullable)
|
||||
* @returns true if the podcast is stale, false otherwise
|
||||
*/
|
||||
export function isPodcastStale(
|
||||
chatVersion: number,
|
||||
podcastVersion: number | null | undefined
|
||||
): boolean {
|
||||
// If podcast has no version, it's stale (generated before this feature)
|
||||
if (!podcastVersion) {
|
||||
return true;
|
||||
}
|
||||
// If chat version is greater than podcast version, it's stale : We can change this condition to consider staleness after a huge number of updates
|
||||
return chatVersion > podcastVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a human-readable message about podcast staleness
|
||||
*
|
||||
* @param chatVersion - The current state_version of the chat
|
||||
* @param podcastVersion - The chat_state_version stored when the podcast was generated
|
||||
* @returns A descriptive message about the podcast's staleness status
|
||||
*/
|
||||
export function getPodcastStalenessMessage(
|
||||
chatVersion: number,
|
||||
podcastVersion: number | null | undefined
|
||||
): string {
|
||||
if (!podcastVersion) {
|
||||
return "This podcast was generated before chat updates were tracked. Consider regenerating it.";
|
||||
}
|
||||
|
||||
if (chatVersion > podcastVersion) {
|
||||
const versionDiff = chatVersion - podcastVersion;
|
||||
return `This podcast is outdated. The chat has been updated ${versionDiff} time${versionDiff > 1 ? "s" : ""} since this podcast was generated.`;
|
||||
}
|
||||
|
||||
return "This podcast is up to date with the current chat.";
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue