feat(ai): talk about ai timeline summary
Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { Checkbox } from "@follow/components/ui/checkbox/index.jsx"
|
||||
import type { LexicalRichEditorRef } from "@follow/components/ui/lexical-rich-editor/index.js"
|
||||
import { LexicalRichEditor } from "@follow/components/ui/lexical-rich-editor/index.js"
|
||||
import { ScrollArea } from "@follow/components/ui/scroll-area/ScrollArea.js"
|
||||
import { cn, stopPropagation } from "@follow/utils"
|
||||
import { cn, nextFrame, stopPropagation } from "@follow/utils"
|
||||
import type { VariantProps } from "class-variance-authority"
|
||||
import { cva } from "class-variance-authority"
|
||||
import { noop } from "es-toolkit"
|
||||
@@ -13,6 +14,7 @@ import { memo, use, useCallback, useImperativeHandle, useRef, useState } from "r
|
||||
import { AIChatContextBar } from "~/modules/ai-chat/components/layouts/AIChatContextBar"
|
||||
|
||||
import { FileUploadPlugin, MentionPlugin } from "../../editor"
|
||||
import { useTimelineSummarySession } from "../../hooks/useTimelineSummarySession"
|
||||
import { AIPanelRefsContext } from "../../store/AIChatContext"
|
||||
import { useChatActions, useChatScene, useChatStatus } from "../../store/hooks"
|
||||
import { AIChatSendButton } from "./AIChatSendButton"
|
||||
@@ -40,115 +42,158 @@ const chatInputVariants = cva(
|
||||
interface ChatInputProps extends VariantProps<typeof chatInputVariants> {
|
||||
onSend: (message: EditorState | string, editor: LexicalEditor | null) => void
|
||||
ref?: Ref<LexicalRichEditorRef | null>
|
||||
isWelcomeScreen?: boolean
|
||||
}
|
||||
|
||||
export const ChatInput = memo(({ onSend, variant, ref: forwardedRef }: ChatInputProps) => {
|
||||
const status = useChatStatus()
|
||||
const chatActions = useChatActions()
|
||||
export const ChatInput = memo(
|
||||
({ onSend, variant, ref: forwardedRef, isWelcomeScreen = false }: ChatInputProps) => {
|
||||
const status = useChatStatus()
|
||||
const chatActions = useChatActions()
|
||||
|
||||
const stop = useCallback(() => {
|
||||
chatActions.stop()
|
||||
}, [chatActions])
|
||||
const stop = useCallback(() => {
|
||||
chatActions.stop()
|
||||
}, [chatActions])
|
||||
|
||||
const editorRef = useRef<LexicalRichEditorRef | null>(null)
|
||||
const editorRef = useRef<LexicalRichEditorRef | null>(null)
|
||||
|
||||
useImperativeHandle<LexicalRichEditorRef | null, LexicalRichEditorRef | null>(
|
||||
forwardedRef,
|
||||
() => editorRef.current,
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
[editorRef.current],
|
||||
)
|
||||
useImperativeHandle<LexicalRichEditorRef | null, LexicalRichEditorRef | null>(
|
||||
forwardedRef,
|
||||
() => editorRef.current,
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
[editorRef.current],
|
||||
)
|
||||
|
||||
const aiPanelRefs = use(AIPanelRefsContext)
|
||||
if (editorRef.current) {
|
||||
aiPanelRefs.inputRef.current = editorRef.current
|
||||
}
|
||||
|
||||
const [isEmpty, setIsEmpty] = useState(true)
|
||||
const [currentEditor, setCurrentEditor] = useState<LexicalEditor | null>(null)
|
||||
|
||||
const isProcessing = status === "submitted" || status === "streaming"
|
||||
|
||||
const handleSend = useCallback(() => {
|
||||
if (currentEditor && editorRef.current && !editorRef.current.isEmpty()) {
|
||||
onSend(currentEditor.getEditorState(), currentEditor)
|
||||
editorRef.current.clear()
|
||||
const aiPanelRefs = use(AIPanelRefsContext)
|
||||
if (editorRef.current) {
|
||||
aiPanelRefs.inputRef.current = editorRef.current
|
||||
}
|
||||
}, [currentEditor, onSend])
|
||||
|
||||
const handleKeyDown = useCallback(
|
||||
(event: KeyboardEvent) => {
|
||||
if (event.key === "Enter" && !event.shiftKey) {
|
||||
event.preventDefault()
|
||||
if (isProcessing) {
|
||||
return false
|
||||
const [isEmpty, setIsEmpty] = useState(true)
|
||||
const [currentEditor, setCurrentEditor] = useState<LexicalEditor | null>(null)
|
||||
|
||||
const isProcessing = status === "submitted" || status === "streaming"
|
||||
|
||||
const handleEditorChange = useCallback((editorState: EditorState, editor: LexicalEditor) => {
|
||||
setCurrentEditor(editor)
|
||||
// Update isEmpty state based on editor content
|
||||
editorState.read(() => {
|
||||
const root = $getRoot()
|
||||
const textContent = root.getTextContent().trim()
|
||||
setIsEmpty(textContent === "")
|
||||
})
|
||||
}, [])
|
||||
|
||||
const scene = useChatScene()
|
||||
|
||||
// Determine if timeline summary can be reused and get today's session id
|
||||
const { canReuseTimelineSummary, todayTimelineSummaryId } = useTimelineSummarySession()
|
||||
|
||||
const [reuseSummary, setReuseSummary] = useState(true)
|
||||
|
||||
const handleSend = useCallback(async () => {
|
||||
if (currentEditor && editorRef.current && !editorRef.current.isEmpty()) {
|
||||
if (isWelcomeScreen && canReuseTimelineSummary && reuseSummary) {
|
||||
try {
|
||||
await chatActions.switchToChat(todayTimelineSummaryId)
|
||||
} catch {
|
||||
// ignore switch errors
|
||||
}
|
||||
}
|
||||
handleSend()
|
||||
return true
|
||||
|
||||
const editorState = currentEditor?.getEditorState()
|
||||
nextFrame(() => {
|
||||
onSend(editorState, currentEditor)
|
||||
})
|
||||
editorRef.current.clear()
|
||||
}
|
||||
return false
|
||||
},
|
||||
[handleSend, isProcessing],
|
||||
)
|
||||
}, [
|
||||
currentEditor,
|
||||
onSend,
|
||||
isWelcomeScreen,
|
||||
canReuseTimelineSummary,
|
||||
reuseSummary,
|
||||
chatActions,
|
||||
todayTimelineSummaryId,
|
||||
])
|
||||
|
||||
const handleEditorChange = useCallback((editorState: EditorState, editor: LexicalEditor) => {
|
||||
setCurrentEditor(editor)
|
||||
// Update isEmpty state based on editor content
|
||||
editorState.read(() => {
|
||||
const root = $getRoot()
|
||||
const textContent = root.getTextContent().trim()
|
||||
setIsEmpty(textContent === "")
|
||||
})
|
||||
}, [])
|
||||
const handleSendClick = useCallback(() => {
|
||||
void handleSend()
|
||||
}, [handleSend])
|
||||
|
||||
const scene = useChatScene()
|
||||
const handleKeyDown = useCallback(
|
||||
(event: KeyboardEvent) => {
|
||||
if (event.key === "Enter" && !event.shiftKey) {
|
||||
event.preventDefault()
|
||||
if (isProcessing) {
|
||||
return false
|
||||
}
|
||||
void handleSend()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
},
|
||||
[handleSend, isProcessing],
|
||||
)
|
||||
|
||||
return (
|
||||
<div className={cn(chatInputVariants({ variant }))}>
|
||||
{/* Input Area */}
|
||||
<div className="relative z-10 flex items-end" onContextMenu={stopPropagation}>
|
||||
<ScrollArea rootClassName="mx-5 my-3.5 mr-14 flex-1 overflow-auto">
|
||||
<LexicalRichEditor
|
||||
ref={editorRef}
|
||||
placeholder={scene === "onboarding" ? "Enter your message" : "Message, @ for context"}
|
||||
className="w-full"
|
||||
onChange={handleEditorChange}
|
||||
onKeyDown={handleKeyDown}
|
||||
autoFocus
|
||||
plugins={scene === "onboarding" ? [] : [MentionPlugin, FileUploadPlugin]}
|
||||
namespace="AIChatRichEditor"
|
||||
/>
|
||||
</ScrollArea>
|
||||
<div className="absolute right-3 top-3">
|
||||
<AIChatSendButton
|
||||
onClick={isProcessing ? stop : handleSend}
|
||||
disabled={!isProcessing && isEmpty}
|
||||
isProcessing={isProcessing}
|
||||
size="sm"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Context Bar - only shown in non-onboarding scene, positioned below the input area */}
|
||||
{scene !== "onboarding" && (
|
||||
<div className="border-border/20 relative z-10 border-t bg-transparent">
|
||||
<div className="flex items-center justify-between px-4 py-2.5">
|
||||
<div className="min-w-0 flex-1 shrink">
|
||||
<AIChatContextBar
|
||||
className="border-0 bg-transparent p-0"
|
||||
onSendShortcut={(prompt) => onSend(prompt, null)}
|
||||
/>
|
||||
</div>
|
||||
<AIModelIndicator
|
||||
className="-mr-1.5 ml-3 translate-y-[2px] self-start"
|
||||
// Current not support switch model, will open this feature later
|
||||
onModelChange={noop}
|
||||
return (
|
||||
<div className={cn(chatInputVariants({ variant }))}>
|
||||
{/* Input Area */}
|
||||
<div className="relative z-10 flex items-end" onContextMenu={stopPropagation}>
|
||||
<ScrollArea rootClassName="mx-5 my-3.5 mr-14 flex-1 overflow-auto">
|
||||
<LexicalRichEditor
|
||||
ref={editorRef}
|
||||
placeholder={scene === "onboarding" ? "Enter your message" : "Message, @ for context"}
|
||||
className="w-full"
|
||||
onChange={handleEditorChange}
|
||||
onKeyDown={handleKeyDown}
|
||||
autoFocus
|
||||
plugins={scene === "onboarding" ? [] : [MentionPlugin, FileUploadPlugin]}
|
||||
namespace="AIChatRichEditor"
|
||||
/>
|
||||
</ScrollArea>
|
||||
<div className="absolute right-3 top-3">
|
||||
<AIChatSendButton
|
||||
onClick={isProcessing ? stop : handleSendClick}
|
||||
disabled={!isProcessing && isEmpty}
|
||||
isProcessing={isProcessing}
|
||||
size="sm"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})
|
||||
|
||||
{/* Context Bar - only shown in non-onboarding scene, positioned below the input area */}
|
||||
{scene !== "onboarding" && (
|
||||
<div className="border-border/20 relative z-10 border-t bg-transparent">
|
||||
<div className="flex items-center justify-between px-4 py-2.5">
|
||||
<div className="min-w-0 flex-1 shrink">
|
||||
<AIChatContextBar
|
||||
className="border-0 bg-transparent p-0"
|
||||
onSendShortcut={(prompt) => onSend(prompt, null)}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex items-center gap-3">
|
||||
{isWelcomeScreen && canReuseTimelineSummary && (
|
||||
<label className="text-text-secondary flex select-none items-center gap-2 text-xs">
|
||||
<Checkbox
|
||||
checked={reuseSummary}
|
||||
size="sm"
|
||||
onCheckedChange={(v) => setReuseSummary(!!v)}
|
||||
/>
|
||||
Ask about summary
|
||||
</label>
|
||||
)}
|
||||
<AIModelIndicator
|
||||
className="-mr-1.5 ml-1 translate-y-[2px] self-start"
|
||||
// Current not support switch model, will open this feature later
|
||||
onModelChange={noop}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
ChatInput.displayName = "ChatInput"
|
||||
|
||||
+5
-1
@@ -371,7 +371,11 @@ const ChatInterfaceContent = ({ centerInputOnEmpty }: ChatInterfaceProps) => {
|
||||
)}
|
||||
>
|
||||
{error && <CollapsibleError error={error} />}
|
||||
<ChatInput onSend={handleSendMessage} variant={!hasMessages ? "minimal" : "default"} />
|
||||
<ChatInput
|
||||
onSend={handleSendMessage}
|
||||
variant={!hasMessages ? "minimal" : "default"}
|
||||
isWelcomeScreen={!hasMessages && !isLoadingHistory}
|
||||
/>
|
||||
<div className="text-text-secondary relative z-[1] -mb-4 mt-2 pl-2 text-xs">
|
||||
AI can make mistakes, please verify critical information.
|
||||
</div>
|
||||
|
||||
+7
-26
@@ -1,21 +1,18 @@
|
||||
import { Folo } from "@follow/components/icons/folo.js"
|
||||
import { ScrollArea } from "@follow/components/ui/scroll-area/ScrollArea.js"
|
||||
import { FeedViewType } from "@follow/constants"
|
||||
import { useElementWidth } from "@follow/hooks"
|
||||
import { clsx } from "@follow/utils"
|
||||
import type { EditorState } from "lexical"
|
||||
import { AnimatePresence, m } from "motion/react"
|
||||
import { useEffect, useMemo, useRef } from "react"
|
||||
import { useEffect, useRef } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
import { useAISettingValue } from "~/atoms/settings/ai"
|
||||
import { ROUTE_ENTRY_PENDING } from "~/constants"
|
||||
import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams"
|
||||
import { AISpline } from "~/modules/ai-chat/components/3d-models/AISpline"
|
||||
|
||||
import { AI_CHAT_SPECIAL_ID_PREFIX } from "../../constants"
|
||||
import { useAttachScrollBeyond } from "../../hooks/useAttachScrollBeyond"
|
||||
import { useMainEntryId } from "../../hooks/useMainEntryId"
|
||||
import { useTimelineSummarySession } from "../../hooks/useTimelineSummarySession"
|
||||
import { useChatActions, useChatError, useChatStatus, useMessages } from "../../store/hooks"
|
||||
import { AIMessageParts } from "../message/AIMessageParts"
|
||||
import { DefaultWelcomeContent, EntrySummaryCard } from "../welcome"
|
||||
@@ -30,31 +27,15 @@ export const WelcomeScreen = ({ onSend, centerInputOnEmpty }: WelcomeScreenProps
|
||||
const { t } = useTranslation("ai")
|
||||
const aiSettings = useAISettingValue()
|
||||
const mainEntryId = useMainEntryId()
|
||||
const { view, isAllFeeds, entryId } = useRouteParamsSelector((s) => ({
|
||||
view: s.view,
|
||||
isAllFeeds: s.isAllFeeds,
|
||||
entryId: s.entryId,
|
||||
}))
|
||||
|
||||
const realEntryId = entryId === ROUTE_ENTRY_PENDING ? "" : entryId
|
||||
const isAllView = view === FeedViewType.All && isAllFeeds && !realEntryId
|
||||
|
||||
const hasEntryContext = !!mainEntryId
|
||||
const { todayTimelineSummaryId, canReuseTimelineSummary, hasEntryContext } =
|
||||
useTimelineSummarySession()
|
||||
const enabledShortcuts = aiSettings.shortcuts?.filter((shortcut) => shortcut.enabled) || []
|
||||
const shouldFetchTimelineSummary = isAllView && !hasEntryContext
|
||||
const shouldFetchTimelineSummary = canReuseTimelineSummary
|
||||
|
||||
const { handleScroll } = useAttachScrollBeyond()
|
||||
const showTimelineSummary = shouldFetchTimelineSummary
|
||||
|
||||
const todayTimelineSummaryId = useMemo(() => {
|
||||
const now = new Date()
|
||||
|
||||
const day = now.getDate()
|
||||
const month = now.getMonth() + 1
|
||||
const year = now.getFullYear()
|
||||
|
||||
return `${AI_CHAT_SPECIAL_ID_PREFIX.TIMELINE_SUMMARY}${year}-${month}-${day}`
|
||||
}, [])
|
||||
// todayTimelineSummaryId from hook
|
||||
|
||||
return (
|
||||
<ScrollArea
|
||||
@@ -85,7 +66,7 @@ export const WelcomeScreen = ({ onSend, centerInputOnEmpty }: WelcomeScreenProps
|
||||
)}
|
||||
>
|
||||
<AnimatePresence mode="wait">
|
||||
{hasEntryContext ? (
|
||||
{hasEntryContext && mainEntryId ? (
|
||||
<EntrySummaryCard key="entry-summary" entryId={mainEntryId} />
|
||||
) : (
|
||||
<DefaultWelcomeContent
|
||||
|
||||
+2
-8
@@ -27,15 +27,9 @@ export const ToolInvocationComponent: React.FC<ToolInvocationComponentProps> = R
|
||||
<div className={clsx("relative pl-8 last:pb-0", variant === "tight" ? "pb-0" : "pb-3")}>
|
||||
<div
|
||||
aria-hidden
|
||||
className={`absolute left-2 top-2 size-2 -translate-x-1/2 rounded-full border ${
|
||||
hasError ? "border-red bg-red" : "border-fill bg-fill-vibrant"
|
||||
}`}
|
||||
className={`border-fill bg-fill-vibrant absolute left-2 top-2 size-2 -translate-x-1/2 rounded-full border ${hasError ? "text-red" : ""}`}
|
||||
>
|
||||
<i
|
||||
className={`absolute top-1/2 -translate-x-1/4 -translate-y-1/2 ${
|
||||
hasError ? "i-mgc-close-cute-re" : "i-mgc-tool-cute-re"
|
||||
}`}
|
||||
/>
|
||||
<i className={`i-mgc-tool-cute-re absolute top-1/2 -translate-x-1/4 -translate-y-1/2`} />
|
||||
</div>
|
||||
|
||||
<CollapseCssGroup>
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import { FeedViewType } from "@follow/constants"
|
||||
import { useMemo } from "react"
|
||||
|
||||
import { ROUTE_ENTRY_PENDING } from "~/constants"
|
||||
import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams"
|
||||
|
||||
import { AI_CHAT_SPECIAL_ID_PREFIX } from "../constants"
|
||||
import { useMainEntryId } from "./useMainEntryId"
|
||||
|
||||
export const getTodayTimelineSummaryId = () => {
|
||||
const now = new Date()
|
||||
const day = now.getDate()
|
||||
const month = now.getMonth() + 1
|
||||
const year = now.getFullYear()
|
||||
return `${AI_CHAT_SPECIAL_ID_PREFIX.TIMELINE_SUMMARY}${year}-${month}-${day}`
|
||||
}
|
||||
|
||||
export const useTimelineSummarySession = () => {
|
||||
const mainEntryId = useMainEntryId()
|
||||
const { view, isAllFeeds, entryId } = useRouteParamsSelector((s) => ({
|
||||
view: s.view,
|
||||
isAllFeeds: s.isAllFeeds,
|
||||
entryId: s.entryId,
|
||||
}))
|
||||
|
||||
const realEntryId = entryId === ROUTE_ENTRY_PENDING ? "" : entryId
|
||||
const isAllView = view === FeedViewType.All && isAllFeeds && !realEntryId
|
||||
const hasEntryContext = !!mainEntryId
|
||||
const canReuseTimelineSummary = isAllView && !hasEntryContext
|
||||
|
||||
const todayTimelineSummaryId = useMemo(() => getTodayTimelineSummaryId(), [])
|
||||
|
||||
return {
|
||||
todayTimelineSummaryId,
|
||||
canReuseTimelineSummary,
|
||||
isAllView,
|
||||
hasEntryContext,
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,16 @@ import * as React from "react"
|
||||
type CheckboxProps = React.ComponentProps<typeof CheckboxPrimitive.Root> &
|
||||
HTMLMotionProps<"button"> & {
|
||||
indeterminate?: boolean
|
||||
size?: "sm" | "md"
|
||||
}
|
||||
|
||||
function Checkbox({ className, onCheckedChange, indeterminate, ...props }: CheckboxProps) {
|
||||
function Checkbox({
|
||||
className,
|
||||
onCheckedChange,
|
||||
indeterminate,
|
||||
size = "md",
|
||||
...props
|
||||
}: CheckboxProps) {
|
||||
const [isChecked, setIsChecked] = React.useState(props?.checked ?? props?.defaultChecked ?? false)
|
||||
|
||||
React.useEffect(() => {
|
||||
@@ -31,7 +38,8 @@ function Checkbox({ className, onCheckedChange, indeterminate, ...props }: Check
|
||||
<m.button
|
||||
data-slot="checkbox"
|
||||
className={cn(
|
||||
"bg-fill cursor-checkbox focus-visible:ring-border data-[state=checked]:bg-accent peer flex size-5 shrink-0 items-center justify-center rounded-sm transition-colors duration-500 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:text-white",
|
||||
"bg-fill cursor-checkbox focus-visible:ring-border data-[state=checked]:bg-accent peer flex shrink-0 items-center justify-center rounded-sm transition-colors duration-500 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:text-white",
|
||||
size === "sm" ? "size-4" : "size-5",
|
||||
indeterminate && "bg-accent text-white",
|
||||
className,
|
||||
)}
|
||||
@@ -48,7 +56,7 @@ function Checkbox({ className, onCheckedChange, indeterminate, ...props }: Check
|
||||
viewBox="0 0 24 24"
|
||||
strokeWidth="3.5"
|
||||
stroke="currentColor"
|
||||
className="size-3.5"
|
||||
className={size === "sm" ? "size-3" : "size-3.5"}
|
||||
initial="hidden"
|
||||
animate="visible"
|
||||
>
|
||||
@@ -83,7 +91,7 @@ function Checkbox({ className, onCheckedChange, indeterminate, ...props }: Check
|
||||
viewBox="0 0 24 24"
|
||||
strokeWidth="3.5"
|
||||
stroke="currentColor"
|
||||
className="size-3.5"
|
||||
className={size === "sm" ? "size-3" : "size-3.5"}
|
||||
initial="unchecked"
|
||||
animate={isChecked ? "checked" : "unchecked"}
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user