From e528367e8b89ffb9936b395e187c96df9b1dd025 Mon Sep 17 00:00:00 2001 From: Innei Date: Tue, 12 Aug 2025 20:37:48 +0800 Subject: [PATCH] feat(ai): support AI chat close float panel - Introduced a new interface for WebAISettings, extending AISettings to include panel style options. - Updated default settings creation to include a fixed panel style. - Refactored AI chat panel visibility management, allowing dynamic visibility based on panel style. - Implemented a new hook for managing AI panel visibility and integrated it into the floating panel component. - Removed deprecated context bar search service and replaced it with a unified feed entry search service for improved search functionality. These changes collectively enhance the configurability and usability of the AI chat interface, providing users with better control over their settings and layout preferences. Signed-off-by: Innei --- .../layer/renderer/src/atoms/settings/ai.ts | 35 ++++++++-- .../context-bar/blocks/ContextBlock.tsx | 17 +++-- .../ai-chat/components/layouts/ChatHeader.tsx | 13 +++- .../components/layouts/ChatMoreDropdown.tsx | 8 --- .../mention/hooks/useMentionSearchService.ts | 2 +- .../useFeedEntrySearchService.ts} | 0 .../services/contextBarSearchService.ts | 69 ------------------- .../ai-chat/store/slices/block.slice.ts | 8 ++- .../app-layout/ai/AIChatFloatingPanel.tsx | 46 +++++++------ .../src/modules/command/commands/global.tsx | 19 ----- .../src/modules/command/commands/id.ts | 1 - .../src/modules/customize-toolbar/dnd.tsx | 2 +- .../accessories/ContainerToc.tsx | 5 +- locales/shortcuts/en.json | 2 - locales/shortcuts/ja.json | 2 - 15 files changed, 84 insertions(+), 145 deletions(-) rename apps/desktop/layer/renderer/src/modules/ai-chat/{services/feedEntrySearchService.ts => hooks/useFeedEntrySearchService.ts} (100%) delete mode 100644 apps/desktop/layer/renderer/src/modules/ai-chat/services/contextBarSearchService.ts diff --git a/apps/desktop/layer/renderer/src/atoms/settings/ai.ts b/apps/desktop/layer/renderer/src/atoms/settings/ai.ts index 6b8591f7b..c7a70d733 100644 --- a/apps/desktop/layer/renderer/src/atoms/settings/ai.ts +++ b/apps/desktop/layer/renderer/src/atoms/settings/ai.ts @@ -4,32 +4,38 @@ import type { AISettings } from "@follow/shared/settings/interface" import { jotaiStore } from "@follow/utils" import { atom, useAtomValue } from "jotai" -export const createDefaultSettings = (): AISettings => defaultAISettings +export interface WebAISettings extends AISettings { + panelStyle: AIChatPanelStyle +} + +export const createDefaultSettings = (): WebAISettings => ({ + ...defaultAISettings, + panelStyle: AIChatPanelStyle.Fixed, +}) export const { useSettingKey: useAISettingKey, useSettingSelector: useAISettingSelector, setSetting: setAISetting, clearSettings: clearAISettings, - initializeDefaultSettings: initializeDefaultAISettings, + initializeDefaultSettings, getSettings: getAISettings, useSettingValue: useAISettingValue, settingAtom: __aiSettingAtom, } = createSettingAtom("ai", createDefaultSettings) export const aiServerSyncWhiteListKeys = [] -// Local Setting for ai +////////// AI Panel Style export enum AIChatPanelStyle { Fixed = "fixed", Floating = "floating", } -const aiChatPanelStyleAtom = atom(AIChatPanelStyle.Floating) -export const useAIChatPanelStyle = () => useAtomValue(aiChatPanelStyleAtom) +export const useAIChatPanelStyle = () => useAISettingKey("panelStyle") export const setAIChatPanelStyle = (style: AIChatPanelStyle) => { - jotaiStore.set(aiChatPanelStyleAtom, style) + setAISetting("panelStyle", style) } -export const getAIChatPanelStyle = () => jotaiStore.get(aiChatPanelStyleAtom) +export const getAIChatPanelStyle = () => getAISettings().panelStyle // Floating panel state atoms interface FloatingPanelState { @@ -54,3 +60,18 @@ export const setFloatingPanelState = (state: Partial) => { jotaiStore.set(floatingPanelStateAtom, { ...currentState, ...state }) } export const getFloatingPanelState = () => jotaiStore.get(floatingPanelStateAtom) + +////////// AI Panel Visibility + +const aiPanelVisibilityAtom = atom(false) +export const useAIPanelVisibility = () => useAtomValue(aiPanelVisibilityAtom) +export const setAIPanelVisibility = (visibility: boolean) => { + jotaiStore.set(aiPanelVisibilityAtom, visibility) +} +export const getAIPanelVisibility = () => jotaiStore.get(aiPanelVisibilityAtom) + +//// Enhance Init Ai Settings +export const initializeDefaultAISettings = () => { + initializeDefaultSettings() + if (getAISettings().panelStyle === AIChatPanelStyle.Fixed) setAIPanelVisibility(true) +} diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/context-bar/blocks/ContextBlock.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/context-bar/blocks/ContextBlock.tsx index 6551f1415..fc587a07f 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/context-bar/blocks/ContextBlock.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/context-bar/blocks/ContextBlock.tsx @@ -1,4 +1,4 @@ -import { cn } from "@follow/utils/utils" +import { clsx, cn } from "@follow/utils/utils" import type { FC } from "react" import { memo } from "react" @@ -171,16 +171,17 @@ export const ContextBlock: FC<{ block: AIChatContextBlock }> = memo(({ block }) return (
@@ -188,9 +189,7 @@ export const ContextBlock: FC<{ block: AIChatContextBlock }> = memo(({ block }) {getBlockLabel()}
- - {getDisplayContent()} - + {getDisplayContent()}
diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatHeader.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatHeader.tsx index 7f4861778..dadd10107 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatHeader.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatHeader.tsx @@ -2,6 +2,7 @@ import { ActionButton } from "@follow/components/ui/button/index.js" import { useCallback } from "react" import { useTranslation } from "react-i18next" +import { AIChatPanelStyle, setAIPanelVisibility, useAIChatPanelStyle } from "~/atoms/settings/ai" import { useDialog } from "~/components/ui/modal/stacked/hooks" import { useBlockActions, useChatActions, useCurrentTitle } from "~/modules/ai-chat/store/hooks" import { useSettingModal } from "~/modules/settings/modal/use-setting-modal-hack" @@ -30,7 +31,7 @@ export const ChatHeader = () => { variant: "danger", onConfirm: () => { chatActions.newChat() - blockActions.clearBlocks() + blockActions.clearBlocks({ keepSpecialTypes: true }) }, }) }, [chatActions, currentTitle, ask, t, blockActions]) @@ -42,6 +43,7 @@ export const ChatHeader = () => { [chatActions], ) + const panelStyle = useAIChatPanelStyle() const maskImage = `linear-gradient(to bottom, black 0%, black 75%, transparent 100%)` return (
@@ -73,6 +75,15 @@ export const ChatHeader = () => { + + {panelStyle === AIChatPanelStyle.Floating && ( + <> +
+ setAIPanelVisibility(false)}> + + + + )}
diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatMoreDropdown.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatMoreDropdown.tsx index 3c66e3476..5102cc38a 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatMoreDropdown.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatMoreDropdown.tsx @@ -155,14 +155,6 @@ export const ChatMoreDropdown = () => { ))} - - - - New Chat - ) : (
diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/hooks/useMentionSearchService.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/hooks/useMentionSearchService.ts index 91603a943..a9c7525d4 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/hooks/useMentionSearchService.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/editor/plugins/mention/hooks/useMentionSearchService.ts @@ -1,6 +1,6 @@ import { useMemo } from "react" -import { useFeedEntrySearchService } from "~/modules/ai-chat/services/feedEntrySearchService" +import { useFeedEntrySearchService } from "~/modules/ai-chat/hooks/useFeedEntrySearchService" import type { MentionData, MentionType } from "../types" diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/services/feedEntrySearchService.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useFeedEntrySearchService.ts similarity index 100% rename from apps/desktop/layer/renderer/src/modules/ai-chat/services/feedEntrySearchService.ts rename to apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useFeedEntrySearchService.ts diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/services/contextBarSearchService.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/services/contextBarSearchService.ts deleted file mode 100644 index 22b4b92b4..000000000 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/services/contextBarSearchService.ts +++ /dev/null @@ -1,69 +0,0 @@ -import { useMemo } from "react" - -import type { SearchItem } from "./feedEntrySearchService" -import { useFeedEntrySearchService } from "./feedEntrySearchService" - -/** - * Picker item interface for context bar compatibility - */ -export interface PickerItem { - id: string - title: string -} - -/** - * Hook that provides search functionality for context bar - * Uses the shared feed/entry search service and converts results to PickerItem format - */ -export const useContextBarSearchService = () => { - const { search, feedItems, entryItems } = useFeedEntrySearchService({ - maxRecentEntries: 50, - fuseOptions: { - keys: ["title", "id"], - threshold: 0.3, - }, - }) - - // Convert search items to picker items - const convertToPickerItems = useMemo(() => { - return (items: SearchItem[]): PickerItem[] => { - return items.map((item) => ({ - id: item.id, - title: item.title, - })) - } - }, []) - - // Search function for feeds - const searchFeeds = useMemo(() => { - return (query: string): PickerItem[] => { - const results = search(query, "feed", 20) - return convertToPickerItems(results) - } - }, [search, convertToPickerItems]) - - // Search function for entries - const searchEntries = useMemo(() => { - return (query: string): PickerItem[] => { - const results = search(query, "entry", 20) - return convertToPickerItems(results) - } - }, [search, convertToPickerItems]) - - // Get all feeds as picker items - const allFeeds = useMemo(() => { - return convertToPickerItems(feedItems) - }, [feedItems, convertToPickerItems]) - - // Get all recent entries as picker items - const allRecentEntries = useMemo(() => { - return convertToPickerItems(entryItems) - }, [entryItems, convertToPickerItems]) - - return { - searchFeeds, - searchEntries, - allFeeds, - allRecentEntries, - } -} diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/store/slices/block.slice.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/store/slices/block.slice.ts index 4b003b514..8a60c6691 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/store/slices/block.slice.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/store/slices/block.slice.ts @@ -98,7 +98,7 @@ export class BlockSliceAction { } } - clearBlocks() { + clearBlocks({ keepSpecialTypes = false }: { keepSpecialTypes?: boolean } = {}) { this.set( produce((state: BlockSlice) => { // Clean up file attachments before clearing @@ -107,7 +107,11 @@ export class BlockSliceAction { cleanupFileAttachment(block.attachment) } }) - state.blocks = [] + state.blocks = keepSpecialTypes + ? state.blocks.filter((b) => + Object.values(BlockSliceAction.SPECIAL_TYPES).includes(b.type), + ) + : [] }), ) } diff --git a/apps/desktop/layer/renderer/src/modules/app-layout/ai/AIChatFloatingPanel.tsx b/apps/desktop/layer/renderer/src/modules/app-layout/ai/AIChatFloatingPanel.tsx index 025f0cc61..0ad967563 100644 --- a/apps/desktop/layer/renderer/src/modules/app-layout/ai/AIChatFloatingPanel.tsx +++ b/apps/desktop/layer/renderer/src/modules/app-layout/ai/AIChatFloatingPanel.tsx @@ -1,6 +1,6 @@ import { Spring } from "@follow/components/constants/spring.js" import { cn, computeAdjustedTopLeftPosition } from "@follow/utils" -import { m } from "motion/react" +import { AnimatePresence, m } from "motion/react" import type { ResizeDirection } from "re-resizable" import { Resizable } from "re-resizable" import type { FC } from "react" @@ -9,6 +9,7 @@ import { useEffect, useRef } from "react" import { getFloatingPanelState, setFloatingPanelState, + useAIPanelVisibility, useFloatingPanelState, } from "~/atoms/settings/ai" import { Focusable } from "~/components/common/Focusable" @@ -19,7 +20,7 @@ import { ChatInterface } from "~/modules/ai-chat/components/layouts/ChatInterfac export interface AIChatFloatingPanelProps extends React.DetailedHTMLProps, HTMLDivElement> {} -export const AIChatFloatingPanel: FC = ({ className, ...props }) => { +const AIChatFloatingPanelInner: FC = ({ className, ...props }) => { const floatingState = useFloatingPanelState() // Preserve right/bottom margins to keep panel anchored to bottom-right on window resize @@ -59,25 +60,11 @@ export const AIChatFloatingPanel: FC = ({ className, . return () => window.removeEventListener("resize", handleWindowResize) }, [floatingState.width, floatingState.height]) - const chatContent = ( - - - - - ) - return ( = ({ className, . topLeft: true, }} > - {chatContent} + + + +
) } + +export const AIChatFloatingPanel: FC = (props) => { + const visibility = useAIPanelVisibility() + return ( + + {visibility && } + + ) +} diff --git a/apps/desktop/layer/renderer/src/modules/command/commands/global.tsx b/apps/desktop/layer/renderer/src/modules/command/commands/global.tsx index b046c1f43..fe9da0826 100644 --- a/apps/desktop/layer/renderer/src/modules/command/commands/global.tsx +++ b/apps/desktop/layer/renderer/src/modules/command/commands/global.tsx @@ -12,7 +12,6 @@ declare module "@follow/utils/event-bus" { "global:toggle-corner-play": void "global:quick-add": void "global:toggle-ai-chat": void - "global:toggle-ai-chat-pinned": void } } @@ -72,18 +71,6 @@ export const useRegisterGlobalCommands = () => { icon: aiIcon, category, }, - { - id: COMMAND_ID.global.toggleAIChatPinned, - label: { - title: t("command.global.toggle_ai_chat_pinned.title"), - description: t("command.global.toggle_ai_chat_pinned.description"), - }, - run: () => { - EventBus.dispatch(COMMAND_ID.global.toggleAIChatPinned) - }, - icon: aiIcon, - category, - }, ]) } @@ -107,14 +94,8 @@ export type ToggleAIChatCommand = Command<{ fn: (ctx?: { entryId?: string }) => void }> -export type ToggleAIChatPinnedCommand = Command<{ - id: typeof COMMAND_ID.global.toggleAIChatPinned - fn: (ctx?: { entryId?: string }) => void -}> - export type GlobalCommand = | ShowShortcutsCommand | ToggleCornerPlayCommand | QuickAddCommand | ToggleAIChatCommand - | ToggleAIChatPinnedCommand diff --git a/apps/desktop/layer/renderer/src/modules/command/commands/id.ts b/apps/desktop/layer/renderer/src/modules/command/commands/id.ts index 853dc78c9..e1b03ccc6 100644 --- a/apps/desktop/layer/renderer/src/modules/command/commands/id.ts +++ b/apps/desktop/layer/renderer/src/modules/command/commands/id.ts @@ -50,7 +50,6 @@ export const COMMAND_ID = { toggleCornerPlay: "global:toggle-corner-play", quickAdd: "global:quick-add", toggleAIChat: "global:toggle-ai-chat", - toggleAIChatPinned: "global:toggle-ai-chat-pinned", }, layout: { toggleSubscriptionColumn: "layout:toggle-subscription-column", diff --git a/apps/desktop/layer/renderer/src/modules/customize-toolbar/dnd.tsx b/apps/desktop/layer/renderer/src/modules/customize-toolbar/dnd.tsx index 41b9336e9..e89f3f3de 100644 --- a/apps/desktop/layer/renderer/src/modules/customize-toolbar/dnd.tsx +++ b/apps/desktop/layer/renderer/src/modules/customize-toolbar/dnd.tsx @@ -62,7 +62,7 @@ const warningActionButton: Partial< show: !IN_ELECTRON, info: "entry_actions.warn_info_for_desktop", }, - [COMMAND_ID.global.toggleAIChatPinned]: { + [COMMAND_ID.global.toggleAIChat]: { show: true, info: "entry_actions.warn_info_ai_chat_pinned_tip", }, diff --git a/apps/desktop/layer/renderer/src/modules/entry-content/components/entry-content/accessories/ContainerToc.tsx b/apps/desktop/layer/renderer/src/modules/entry-content/components/entry-content/accessories/ContainerToc.tsx index 29391844f..1b7de027c 100644 --- a/apps/desktop/layer/renderer/src/modules/entry-content/components/entry-content/accessories/ContainerToc.tsx +++ b/apps/desktop/layer/renderer/src/modules/entry-content/components/entry-content/accessories/ContainerToc.tsx @@ -3,13 +3,12 @@ import { CircleProgress } from "@follow/components/icons/Progress.js" import { MotionButtonBase } from "@follow/components/ui/button/index.js" import { RootPortal } from "@follow/components/ui/portal/index.jsx" import { useScrollViewElement } from "@follow/components/ui/scroll-area/hooks.js" -import { EventBus } from "@follow/utils/event-bus" import { springScrollTo } from "@follow/utils/scroller" import { cn } from "@follow/utils/utils" import { useStore } from "jotai" import { memo, useEffect, useMemo, useState } from "react" -import { AIChatPanelStyle, useAIChatPanelStyle } from "~/atoms/settings/ai" +import { AIChatPanelStyle, setAIPanelVisibility, useAIChatPanelStyle } from "~/atoms/settings/ai" import type { TocRef } from "~/components/ui/markdown/components/Toc" import { Toc } from "~/components/ui/markdown/components/Toc" import { useFeature } from "~/hooks/biz/useFeature" @@ -69,7 +68,7 @@ const BackTopIndicator: Component = memo(({ className }) => { {aiEnabled && !isAiPanelOpen && ( { - EventBus.dispatch("global:toggle-ai-chat-pinned") + setAIPanelVisibility(true) }} className={cn( "mt-1 flex flex-nowrap items-center gap-2 text-sm opacity-50 transition-all duration-500 hover:opacity-100", diff --git a/locales/shortcuts/en.json b/locales/shortcuts/en.json index 2ce350a4f..914f68c36 100644 --- a/locales/shortcuts/en.json +++ b/locales/shortcuts/en.json @@ -22,8 +22,6 @@ "command.global.show_shortcuts.title": "Show Shortcuts", "command.global.toggle_ai_chat.description": "Toggle the AI chat panel", "command.global.toggle_ai_chat.title": "AI Chat", - "command.global.toggle_ai_chat_pinned.description": "Toggle the AI chat panel pinned", - "command.global.toggle_ai_chat_pinned.title": "AI Chat(Pinned)", "command.global.toggle_corner_play.description": "Play/Pause playing status(If there is currently playing audio in the background)", "command.global.toggle_corner_play.title": "Toggle Corner Play", "command.layout.focus_to_entry_render.title": "Focus to Entry Render", diff --git a/locales/shortcuts/ja.json b/locales/shortcuts/ja.json index b50dfc749..d69e69b73 100644 --- a/locales/shortcuts/ja.json +++ b/locales/shortcuts/ja.json @@ -22,8 +22,6 @@ "command.global.show_shortcuts.title": "ショートカットを表示", "command.global.toggle_ai_chat.description": "AI チャットパネルを切り替えます", "command.global.toggle_ai_chat.title": "AI チャット", - "command.global.toggle_ai_chat_pinned.description": "AI チャットパネルをピン留めします", - "command.global.toggle_ai_chat_pinned.title": "AI チャット (ピン留め)", "command.global.toggle_corner_play.description": "再生中のオーディオがある場合、再生/一時停止の状態を切り替えます", "command.global.toggle_corner_play.title": "コーナー再生の切り替え", "command.layout.focus_to_entry_render.title": "エントリー レンダリングにフォーカス",