From f943f05cb12b45a41152459894782c2f854bdaf5 Mon Sep 17 00:00:00 2001 From: Phantom Date: Thu, 16 Oct 2025 19:23:09 +0800 Subject: [PATCH 01/27] fix(translate): auto copy failed (#10745) * fix(translate): auto copy failed Because translatedContent may be stale * refactor(translate): improve copy functionality dependency handling Update copy callback dependencies to include setCopied and ensure proper memoization Fix onCopy and translateText dependencies to include copy function --- .../src/pages/translate/TranslatePage.tsx | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/renderer/src/pages/translate/TranslatePage.tsx b/src/renderer/src/pages/translate/TranslatePage.tsx index e883955eb1..76e50ce4b5 100644 --- a/src/renderer/src/pages/translate/TranslatePage.tsx +++ b/src/renderer/src/pages/translate/TranslatePage.tsx @@ -134,15 +134,22 @@ const TranslatePage: FC = () => { ) // 控制复制行为 + const copy = useCallback( + async (text: string) => { + await navigator.clipboard.writeText(text) + setCopied(true) + }, + [setCopied] + ) + const onCopy = useCallback(async () => { try { - await navigator.clipboard.writeText(translatedContent) - setCopied(true) + await copy(translatedContent) } catch (error) { logger.error('Failed to copy text to clipboard:', error as Error) window.toast.error(t('common.copy_failed')) } - }, [setCopied, t, translatedContent]) + }, [copy, t, translatedContent]) /** * 翻译文本并保存历史记录,包含完整的异常处理,不会抛出异常 @@ -183,7 +190,7 @@ const TranslatePage: FC = () => { setTimeoutTimer( 'auto-copy', async () => { - await onCopy() + await copy(translated) }, 100 ) @@ -200,7 +207,7 @@ const TranslatePage: FC = () => { window.toast.error(t('translate.error.unknown') + ': ' + formatErrorMessage(e)) } }, - [autoCopy, dispatch, onCopy, setTimeoutTimer, setTranslatedContent, setTranslating, t, translating] + [autoCopy, copy, dispatch, setTimeoutTimer, setTranslatedContent, setTranslating, t, translating] ) // 控制翻译按钮是否可用 From 5eb2772d53e3e7a4ecda51ef3d00cdd9fc794fd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?George=C2=B7Dong?= <98630204+GeorgeDong32@users.noreply.github.com> Date: Thu, 16 Oct 2025 21:35:37 +0800 Subject: [PATCH 02/27] fix(minapps): can't open links in external broswer when using tab navigation (#10669) * fix(minapps): can't open links in external broswer when using tab navigation * fix(minapps): stabilize webview navigation and add logging * fix(minapps): debounce nav updates and robust webview attach --- .../components/MinApp/WebviewContainer.tsx | 20 +- .../minapps/components/MinimalToolbar.tsx | 174 ++++++++++++++++-- 2 files changed, 181 insertions(+), 13 deletions(-) diff --git a/src/renderer/src/components/MinApp/WebviewContainer.tsx b/src/renderer/src/components/MinApp/WebviewContainer.tsx index dcae087f6d..545772ef08 100644 --- a/src/renderer/src/components/MinApp/WebviewContainer.tsx +++ b/src/renderer/src/components/MinApp/WebviewContainer.tsx @@ -25,7 +25,7 @@ const WebviewContainer = memo( onNavigateCallback: (appid: string, url: string) => void }) => { const webviewRef = useRef(null) - const { enableSpellCheck } = useSettings() + const { enableSpellCheck, minappsOpenLinkExternal } = useSettings() const setRef = (appid: string) => { onSetRefCallback(appid, null) @@ -76,6 +76,8 @@ const WebviewContainer = memo( const webviewId = webviewRef.current?.getWebContentsId() if (webviewId) { window.api?.webview?.setSpellCheckEnabled?.(webviewId, enableSpellCheck) + // Set link opening behavior for this webview + window.api?.webview?.setOpenLinkExternal?.(webviewId, minappsOpenLinkExternal) } } @@ -104,6 +106,22 @@ const WebviewContainer = memo( // eslint-disable-next-line react-hooks/exhaustive-deps }, [appid, url]) + // Update webview settings when they change + useEffect(() => { + if (!webviewRef.current) return + + try { + const webviewId = webviewRef.current.getWebContentsId() + if (webviewId) { + window.api?.webview?.setSpellCheckEnabled?.(webviewId, enableSpellCheck) + window.api?.webview?.setOpenLinkExternal?.(webviewId, minappsOpenLinkExternal) + } + } catch (error) { + // WebView may not be ready yet, settings will be applied in dom-ready event + logger.debug(`WebView ${appid} not ready for settings update`) + } + }, [appid, minappsOpenLinkExternal, enableSpellCheck]) + const WebviewStyle: React.CSSProperties = { width: '100%', height: '100%', diff --git a/src/renderer/src/pages/minapps/components/MinimalToolbar.tsx b/src/renderer/src/pages/minapps/components/MinimalToolbar.tsx index dcbe7adeff..ef89fe6409 100644 --- a/src/renderer/src/pages/minapps/components/MinimalToolbar.tsx +++ b/src/renderer/src/pages/minapps/components/MinimalToolbar.tsx @@ -8,6 +8,7 @@ import { PushpinOutlined, ReloadOutlined } from '@ant-design/icons' +import { loggerService } from '@logger' import { isDev } from '@renderer/config/constant' import { DEFAULT_MIN_APPS } from '@renderer/config/minapps' import { useMinapps } from '@renderer/hooks/useMinapps' @@ -17,11 +18,21 @@ import { setMinappsOpenLinkExternal } from '@renderer/store/settings' import { MinAppType } from '@renderer/types' import { Tooltip } from 'antd' import { WebviewTag } from 'electron' -import { FC, useCallback, useState } from 'react' +import { FC, useCallback, useEffect, useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import { useNavigate } from 'react-router-dom' import styled from 'styled-components' +const logger = loggerService.withContext('MinimalToolbar') + +// Constants for timing delays +const WEBVIEW_CHECK_INITIAL_MS = 100 // Initial check interval +const WEBVIEW_CHECK_MAX_MS = 1000 // Maximum check interval (1 second) +const WEBVIEW_CHECK_MULTIPLIER = 2 // Exponential backoff multiplier +const WEBVIEW_CHECK_MAX_ATTEMPTS = 30 // Stop after ~30 seconds total +const NAVIGATION_UPDATE_DELAY_MS = 50 +const NAVIGATION_COMPLETE_DELAY_MS = 100 + interface Props { app: MinAppType webviewRef: React.RefObject @@ -42,27 +53,166 @@ const MinimalToolbar: FC = ({ app, webviewRef, currentUrl, onReload, onOp const isPinned = pinned.some((item) => item.id === app.id) const canOpenExternalLink = app.url.startsWith('http://') || app.url.startsWith('https://') + // Ref to track navigation update timeout + const navigationUpdateTimeoutRef = useRef(null) + // Update navigation state const updateNavigationState = useCallback(() => { if (webviewRef.current) { - setCanGoBack(webviewRef.current.canGoBack()) - setCanGoForward(webviewRef.current.canGoForward()) + try { + setCanGoBack(webviewRef.current.canGoBack()) + setCanGoForward(webviewRef.current.canGoForward()) + } catch (error) { + logger.debug('WebView not ready for navigation state update', { appId: app.id }) + setCanGoBack(false) + setCanGoForward(false) + } + } else { + setCanGoBack(false) + setCanGoForward(false) } - }, [webviewRef]) + }, [app.id, webviewRef]) + + // Schedule navigation state update with debouncing + const scheduleNavigationUpdate = useCallback( + (delay: number) => { + if (navigationUpdateTimeoutRef.current) { + clearTimeout(navigationUpdateTimeoutRef.current) + } + navigationUpdateTimeoutRef.current = setTimeout(() => { + updateNavigationState() + navigationUpdateTimeoutRef.current = null + }, delay) + }, + [updateNavigationState] + ) + + // Cleanup navigation timeout on unmount + useEffect(() => { + return () => { + if (navigationUpdateTimeoutRef.current) { + clearTimeout(navigationUpdateTimeoutRef.current) + } + } + }, []) + + // Monitor webviewRef changes and update navigation state + useEffect(() => { + let checkTimeout: NodeJS.Timeout | null = null + let navigationListener: (() => void) | null = null + let listenersAttached = false + let currentInterval = WEBVIEW_CHECK_INITIAL_MS + let attemptCount = 0 + + const attachListeners = () => { + if (webviewRef.current && !listenersAttached) { + // Update state immediately + updateNavigationState() + + // Add navigation event listeners + const handleNavigation = () => { + scheduleNavigationUpdate(NAVIGATION_UPDATE_DELAY_MS) + } + + webviewRef.current.addEventListener('did-navigate', handleNavigation) + webviewRef.current.addEventListener('did-navigate-in-page', handleNavigation) + listenersAttached = true + + navigationListener = () => { + if (webviewRef.current) { + webviewRef.current.removeEventListener('did-navigate', handleNavigation) + webviewRef.current.removeEventListener('did-navigate-in-page', handleNavigation) + } + listenersAttached = false + } + + if (checkTimeout) { + clearTimeout(checkTimeout) + checkTimeout = null + } + + logger.debug('Navigation listeners attached', { appId: app.id, attempts: attemptCount }) + return true + } + return false + } + + const scheduleCheck = () => { + checkTimeout = setTimeout(() => { + // Use requestAnimationFrame to avoid blocking the main thread + requestAnimationFrame(() => { + attemptCount++ + if (!attachListeners()) { + // Stop checking after max attempts to prevent infinite loops + if (attemptCount >= WEBVIEW_CHECK_MAX_ATTEMPTS) { + logger.warn('WebView attachment timeout', { + appId: app.id, + attempts: attemptCount, + totalTimeMs: currentInterval * attemptCount + }) + return + } + + // Exponential backoff: double the interval up to the maximum + currentInterval = Math.min(currentInterval * WEBVIEW_CHECK_MULTIPLIER, WEBVIEW_CHECK_MAX_MS) + + // Log only on first few attempts or when interval changes significantly + if (attemptCount <= 3 || attemptCount % 10 === 0) { + logger.debug('WebView not ready, scheduling next check', { + appId: app.id, + nextCheckMs: currentInterval, + attempt: attemptCount + }) + } + + scheduleCheck() + } + }) + }, currentInterval) + } + + // Check for webview attachment + if (!webviewRef.current) { + scheduleCheck() + } else { + attachListeners() + } + + // Cleanup + return () => { + if (checkTimeout) clearTimeout(checkTimeout) + if (navigationListener) navigationListener() + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [app.id, updateNavigationState, scheduleNavigationUpdate]) // webviewRef excluded as it's a ref object const handleGoBack = useCallback(() => { - if (webviewRef.current && webviewRef.current.canGoBack()) { - webviewRef.current.goBack() - updateNavigationState() + if (webviewRef.current) { + try { + if (webviewRef.current.canGoBack()) { + webviewRef.current.goBack() + // Delay update to ensure navigation completes + scheduleNavigationUpdate(NAVIGATION_COMPLETE_DELAY_MS) + } + } catch (error) { + logger.debug('WebView not ready for navigation', { appId: app.id, action: 'goBack' }) + } } - }, [webviewRef, updateNavigationState]) + }, [app.id, webviewRef, scheduleNavigationUpdate]) const handleGoForward = useCallback(() => { - if (webviewRef.current && webviewRef.current.canGoForward()) { - webviewRef.current.goForward() - updateNavigationState() + if (webviewRef.current) { + try { + if (webviewRef.current.canGoForward()) { + webviewRef.current.goForward() + // Delay update to ensure navigation completes + scheduleNavigationUpdate(NAVIGATION_COMPLETE_DELAY_MS) + } + } catch (error) { + logger.debug('WebView not ready for navigation', { appId: app.id, action: 'goForward' }) + } } - }, [webviewRef, updateNavigationState]) + }, [app.id, webviewRef, scheduleNavigationUpdate]) const handleMinimize = useCallback(() => { navigate('/apps') From 9e0ee24fd795d5a813403303160343b546fd2a17 Mon Sep 17 00:00:00 2001 From: kangfenmao Date: Thu, 16 Oct 2025 22:45:41 +0800 Subject: [PATCH 03/27] fix: update Aihubmix auth URL to use console domain --- src/renderer/src/utils/oauth.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderer/src/utils/oauth.ts b/src/renderer/src/utils/oauth.ts index 5d57547f69..00c5493343 100644 --- a/src/renderer/src/utils/oauth.ts +++ b/src/renderer/src/utils/oauth.ts @@ -26,7 +26,7 @@ export const oauthWithSiliconFlow = async (setKey) => { } export const oauthWithAihubmix = async (setKey) => { - const authUrl = ` https://aihubmix.com/token?client_id=cherry_studio_oauth&lang=${getLanguageCode()}&aff=SJyh` + const authUrl = ` https://console.aihubmix.com/token?client_id=cherry_studio_oauth&lang=${getLanguageCode()}&aff=SJyh` const popup = window.open( authUrl, From 76271cbf77731297636a5589b7981d9f0e9950e6 Mon Sep 17 00:00:00 2001 From: Pleasure1234 <3196812536@qq.com> Date: Thu, 16 Oct 2025 17:20:33 +0100 Subject: [PATCH 04/27] fix: ensure API key rotation for each request (#10776) Updated ModernAiProvider to regenerate config on every request, ensuring API key rotation is effective. Refactored BaseApiClient to use an API key getter for dynamic key retrieval, supporting key rotation when multiple keys are configured. --- src/renderer/src/aiCore/index_new.ts | 6 ++---- .../src/aiCore/legacy/clients/BaseApiClient.ts | 10 ++++++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/renderer/src/aiCore/index_new.ts b/src/renderer/src/aiCore/index_new.ts index 81c4aa1df3..b748e3c832 100644 --- a/src/renderer/src/aiCore/index_new.ts +++ b/src/renderer/src/aiCore/index_new.ts @@ -83,10 +83,8 @@ export default class ModernAiProvider { throw new Error('Model is required for completions. Please use constructor with model parameter.') } - // 确保配置存在 - if (!this.config) { - this.config = providerToAiSdkConfig(this.actualProvider, this.model) - } + // 每次请求时重新生成配置以确保API key轮换生效 + this.config = providerToAiSdkConfig(this.actualProvider, this.model) // 准备特殊配置 await prepareSpecialProviderConfig(this.actualProvider, this.config) diff --git a/src/renderer/src/aiCore/legacy/clients/BaseApiClient.ts b/src/renderer/src/aiCore/legacy/clients/BaseApiClient.ts index 9fb687a265..4a41db7c97 100644 --- a/src/renderer/src/aiCore/legacy/clients/BaseApiClient.ts +++ b/src/renderer/src/aiCore/legacy/clients/BaseApiClient.ts @@ -70,13 +70,19 @@ export abstract class BaseApiClient< { public provider: Provider protected host: string - protected apiKey: string protected sdkInstance?: TSdkInstance constructor(provider: Provider) { this.provider = provider this.host = this.getBaseURL() - this.apiKey = this.getApiKey() + } + + /** + * Get the current API key with rotation support + * This getter ensures API keys rotate on each access when multiple keys are configured + */ + protected get apiKey(): string { + return this.getApiKey() } /** From 79c697c34d0d48f216279747e59babca1116946e Mon Sep 17 00:00:00 2001 From: Shemol Date: Fri, 17 Oct 2025 00:50:09 +0800 Subject: [PATCH 05/27] fix: preserve spaces in API keys; update i18n tips to use commas or newlines (#10751) fix(api): preserve spaces in API keys; i18n: clarify tips Tips now say "Use commas to separate multiple keys." Full-width commas are auto-normalized. --- src/renderer/src/i18n/locales/en-us.json | 2 +- src/renderer/src/i18n/locales/zh-cn.json | 2 +- src/renderer/src/i18n/locales/zh-tw.json | 2 +- src/renderer/src/utils/api.ts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/renderer/src/i18n/locales/en-us.json b/src/renderer/src/i18n/locales/en-us.json index 288f0964b0..1f7ec61e2f 100644 --- a/src/renderer/src/i18n/locales/en-us.json +++ b/src/renderer/src/i18n/locales/en-us.json @@ -4079,7 +4079,7 @@ "api_host_tooltip": "Override only when your provider requires a custom OpenAI-compatible endpoint.", "api_key": { "label": "API Key", - "tip": "Multiple keys separated by commas or spaces" + "tip": "Use commas to separate multiple keys" }, "api_version": "API Version", "aws-bedrock": { diff --git a/src/renderer/src/i18n/locales/zh-cn.json b/src/renderer/src/i18n/locales/zh-cn.json index 3e6bac4708..7926e7396d 100644 --- a/src/renderer/src/i18n/locales/zh-cn.json +++ b/src/renderer/src/i18n/locales/zh-cn.json @@ -4079,7 +4079,7 @@ "api_host_tooltip": "仅在服务商需要自定义的 OpenAI 兼容地址时覆盖。", "api_key": { "label": "API 密钥", - "tip": "多个密钥使用逗号或空格分隔" + "tip": "多个密钥使用逗号分隔" }, "api_version": "API 版本", "aws-bedrock": { diff --git a/src/renderer/src/i18n/locales/zh-tw.json b/src/renderer/src/i18n/locales/zh-tw.json index b87e5e3256..ab96bff08a 100644 --- a/src/renderer/src/i18n/locales/zh-tw.json +++ b/src/renderer/src/i18n/locales/zh-tw.json @@ -4079,7 +4079,7 @@ "api_host_tooltip": "僅在服務商需要自訂的 OpenAI 相容端點時才覆蓋。", "api_key": { "label": "API 金鑰", - "tip": "多個金鑰使用逗號或空格分隔" + "tip": "多個金鑰使用逗號分隔" }, "api_version": "API 版本", "aws-bedrock": { diff --git a/src/renderer/src/utils/api.ts b/src/renderer/src/utils/api.ts index 62d0db5623..550de8c7dd 100644 --- a/src/renderer/src/utils/api.ts +++ b/src/renderer/src/utils/api.ts @@ -5,7 +5,7 @@ * @returns {string} 格式化后的 API key 字符串。 */ export function formatApiKeys(value: string): string { - return value.replaceAll(',', ',').replaceAll(' ', ',').replaceAll('\n', ',') + return value.replaceAll(',', ',').replaceAll('\n', ',') } /** From a290ee7f39e322a40826b931ab4ba38b555834eb Mon Sep 17 00:00:00 2001 From: Pleasure1234 <3196812536@qq.com> Date: Fri, 17 Oct 2025 02:40:41 +0100 Subject: [PATCH 06/27] fix: add array checks for knowledge and memories in citations (#10778) Updated formatCitationsFromBlock to verify that 'knowledge' and 'memories' are arrays before accessing their length and mapping over them. This prevents potential runtime errors if these properties are not arrays. --- src/renderer/src/store/messageBlock.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/renderer/src/store/messageBlock.ts b/src/renderer/src/store/messageBlock.ts index 524889bb32..458956e38c 100644 --- a/src/renderer/src/store/messageBlock.ts +++ b/src/renderer/src/store/messageBlock.ts @@ -242,7 +242,7 @@ export const formatCitationsFromBlock = (block: CitationMessageBlock | undefined } } // 3. Handle Knowledge Base References - if (block.knowledge && block.knowledge.length > 0) { + if (block.knowledge && Array.isArray(block.knowledge) && block.knowledge.length > 0) { formattedCitations.push( ...block.knowledge.map((result, index) => { const filePattern = /\[(.*?)]\(http:\/\/file\/(.*?)\)/ @@ -270,7 +270,7 @@ export const formatCitationsFromBlock = (block: CitationMessageBlock | undefined ) } - if (block.memories && block.memories.length > 0) { + if (block.memories && Array.isArray(block.memories) && block.memories.length > 0) { // 5. Handle Memory References formattedCitations.push( ...block.memories.map((memory, index) => ({ From 1c2ce7e0aacc129ffbdd7590de19a12ca94a9370 Mon Sep 17 00:00:00 2001 From: defi-failure <159208748+defi-failure@users.noreply.github.com> Date: Fri, 17 Oct 2025 10:19:48 +0800 Subject: [PATCH 07/27] fix: agents show ChatNavbar in both LeftNavbar and TopNavbar layouts (#10718) * fix: show ChatNavbar in both LeftNavbar and TopNavbar layouts * Revert "fix: show ChatNavbar in both LeftNavbar and TopNavbar layouts" This reverts commit 7f205bf24122682cba78175b1152b85eafe03e96. * refactor: extract ChatNavBarContent from ChatNavBar * fix: add navbar content to top nav in left nav mode * fix: add nodrag to navbar container * fix: lint error * fix: ChatNavbarContainer layout * fix: adjust NavbarLeftContainer min-width for macOS compatibility --------- Co-authored-by: kangfenmao --- src/renderer/src/components/app/Navbar.tsx | 2 +- src/renderer/src/pages/home/ChatNavbar.tsx | 132 +---------------- src/renderer/src/pages/home/HomePage.tsx | 1 + src/renderer/src/pages/home/Navbar.tsx | 51 +++++-- .../home/components/ChatNavbarContent.tsx | 136 ++++++++++++++++++ 5 files changed, 186 insertions(+), 136 deletions(-) create mode 100644 src/renderer/src/pages/home/components/ChatNavbarContent.tsx diff --git a/src/renderer/src/components/app/Navbar.tsx b/src/renderer/src/components/app/Navbar.tsx index 1578e73a7e..5ab2c486b5 100644 --- a/src/renderer/src/components/app/Navbar.tsx +++ b/src/renderer/src/components/app/Navbar.tsx @@ -74,7 +74,7 @@ const NavbarContainer = styled.div<{ $isFullScreen: boolean }>` ` const NavbarLeftContainer = styled.div` - min-width: var(--assistants-width); + min-width: ${isMac ? 'calc(var(--assistants-width) - 20px)' : 'var(--assistants-width)'}; padding: 0 10px; display: flex; flex-direction: row; diff --git a/src/renderer/src/pages/home/ChatNavbar.tsx b/src/renderer/src/pages/home/ChatNavbar.tsx index f232aab91a..0934754139 100644 --- a/src/renderer/src/pages/home/ChatNavbar.tsx +++ b/src/renderer/src/pages/home/ChatNavbar.tsx @@ -1,34 +1,24 @@ -import { BreadcrumbItem, Breadcrumbs, Chip, cn } from '@heroui/react' import { NavbarHeader } from '@renderer/components/app/Navbar' -import HorizontalScrollContainer from '@renderer/components/HorizontalScrollContainer' import { HStack } from '@renderer/components/Layout' import SearchPopup from '@renderer/components/Popups/SearchPopup' -import { permissionModeCards } from '@renderer/constants/permissionModes' -import { useAgent } from '@renderer/hooks/agents/useAgent' -import { useSession } from '@renderer/hooks/agents/useSession' -import { useUpdateAgent } from '@renderer/hooks/agents/useUpdateAgent' import { useAssistant } from '@renderer/hooks/useAssistant' -import { modelGenerating, useRuntime } from '@renderer/hooks/useRuntime' +import { modelGenerating } from '@renderer/hooks/useRuntime' import { useSettings } from '@renderer/hooks/useSettings' import { useShortcut } from '@renderer/hooks/useShortcuts' import { useShowAssistants, useShowTopics } from '@renderer/hooks/useStore' import { EVENT_NAMES, EventEmitter } from '@renderer/services/EventService' import { useAppDispatch } from '@renderer/store' import { setNarrowMode } from '@renderer/store/settings' -import { ApiModel, Assistant, PermissionMode, Topic } from '@renderer/types' -import { formatErrorMessageWithPrefix } from '@renderer/utils/error' +import { Assistant, Topic } from '@renderer/types' import { Tooltip } from 'antd' import { t } from 'i18next' import { Menu, PanelLeftClose, PanelRightClose, Search } from 'lucide-react' import { AnimatePresence, motion } from 'motion/react' -import React, { FC, ReactNode, useCallback } from 'react' +import { FC } from 'react' import styled from 'styled-components' -import { AgentSettingsPopup } from '../settings/AgentSettings' -import { AgentLabel } from '../settings/AgentSettings/shared' import AssistantsDrawer from './components/AssistantsDrawer' -import SelectAgentModelButton from './components/SelectAgentModelButton' -import SelectModelButton from './components/SelectModelButton' +import ChatNavbarContent from './components/ChatNavbarContent' import UpdateAppButton from './components/UpdateAppButton' interface Props { @@ -45,11 +35,6 @@ const HeaderNavbar: FC = ({ activeAssistant, setActiveAssistant, activeTo const { topicPosition, narrowMode } = useSettings() const { showTopics, toggleShowTopics } = useShowTopics() const dispatch = useAppDispatch() - const { chat } = useRuntime() - const { activeTopicOrSession, activeAgentId } = chat - const sessionId = activeAgentId ? (chat.activeSessionId[activeAgentId] ?? null) : null - const { agent } = useAgent(activeAgentId) - const { updateModel } = useUpdateAgent() useShortcut('toggle_show_assistants', toggleShowAssistants) @@ -79,14 +64,6 @@ const HeaderNavbar: FC = ({ activeAssistant, setActiveAssistant, activeTo }) } - const handleUpdateModel = useCallback( - async (model: ApiModel) => { - if (!agent) return - return updateModel(agent.id, model.id, { showSuccessToast: false }) - }, - [agent, updateModel] - ) - return (
@@ -117,38 +94,7 @@ const HeaderNavbar: FC = ({ activeAssistant, setActiveAssistant, activeTo )} - {activeTopicOrSession === 'topic' && } - {activeTopicOrSession === 'session' && agent && ( - - - AgentSettingsPopup.show({ agentId: agent.id })} - classNames={{ - base: 'self-stretch', - item: 'h-full' - }}> - - - - - - - - {activeAgentId && sessionId && ( - - - - )} - - - )} +
@@ -181,74 +127,6 @@ const HeaderNavbar: FC = ({ activeAssistant, setActiveAssistant, activeTo ) } -const SessionWorkspaceMeta: FC<{ agentId: string; sessionId: string }> = ({ agentId, sessionId }) => { - const { agent } = useAgent(agentId) - const { session } = useSession(agentId, sessionId) - if (!session || !agent) { - return null - } - - const firstAccessiblePath = session.accessible_paths?.[0] - const permissionMode = (session.configuration?.permission_mode ?? 'default') as PermissionMode - const permissionModeCard = permissionModeCards.find((card) => card.mode === permissionMode) - const permissionModeLabel = permissionModeCard - ? t(permissionModeCard.titleKey, permissionModeCard.titleFallback) - : permissionMode - - const infoItems: ReactNode[] = [] - - const InfoTag = ({ - text, - className, - onClick - }: { - text: string - className?: string - classNames?: {} - onClick?: (e: React.MouseEvent) => void - }) => ( -
- {text} -
- ) - - // infoItems.push() - - if (firstAccessiblePath) { - infoItems.push( - { - window.api.file - .openPath(firstAccessiblePath) - .catch((e) => - window.toast.error( - formatErrorMessageWithPrefix(e, t('files.error.open_path', { path: firstAccessiblePath })) - ) - ) - }} - /> - ) - } - - infoItems.push() - - if (infoItems.length === 0) { - return null - } - - return
{infoItems}
-} - export const NavbarIcon = styled.div` -webkit-app-region: none; border-radius: 8px; diff --git a/src/renderer/src/pages/home/HomePage.tsx b/src/renderer/src/pages/home/HomePage.tsx index cd18552a3e..32f1ec3975 100644 --- a/src/renderer/src/pages/home/HomePage.tsx +++ b/src/renderer/src/pages/home/HomePage.tsx @@ -130,6 +130,7 @@ const HomePage: FC = () => { setActiveTopic={setActiveTopic} setActiveAssistant={setActiveAssistant} position="left" + activeTopicOrSession={activeTopicOrSession} /> )} diff --git a/src/renderer/src/pages/home/Navbar.tsx b/src/renderer/src/pages/home/Navbar.tsx index 43217d7ca3..6d9ccc2285 100644 --- a/src/renderer/src/pages/home/Navbar.tsx +++ b/src/renderer/src/pages/home/Navbar.tsx @@ -1,4 +1,4 @@ -import { Navbar, NavbarLeft, NavbarRight } from '@renderer/components/app/Navbar' +import { Navbar, NavbarCenter, NavbarLeft, NavbarRight } from '@renderer/components/app/Navbar' import { HStack } from '@renderer/components/Layout' import SearchPopup from '@renderer/components/Popups/SearchPopup' import { isLinux, isMac, isWin } from '@renderer/config/constant' @@ -7,6 +7,8 @@ import { modelGenerating } from '@renderer/hooks/useRuntime' import { useSettings } from '@renderer/hooks/useSettings' import { useShortcut } from '@renderer/hooks/useShortcuts' import { useShowAssistants, useShowTopics } from '@renderer/hooks/useStore' +import { useChatMaxWidth } from '@renderer/pages/home/Chat' +import ChatNavbarContent from '@renderer/pages/home/components/ChatNavbarContent' import { EVENT_NAMES, EventEmitter } from '@renderer/services/EventService' import { useAppDispatch } from '@renderer/store' import { setNarrowMode } from '@renderer/store/settings' @@ -15,7 +17,7 @@ import { Tooltip } from 'antd' import { t } from 'i18next' import { Menu, PanelLeftClose, PanelRightClose, Search } from 'lucide-react' import { AnimatePresence, motion } from 'motion/react' -import { FC } from 'react' +import React, { FC } from 'react' import styled from 'styled-components' import AssistantsDrawer from './components/AssistantsDrawer' @@ -28,13 +30,21 @@ interface Props { setActiveTopic: (topic: Topic) => void setActiveAssistant: (assistant: Assistant) => void position: 'left' | 'right' + activeTopicOrSession?: 'topic' | 'session' } -const HeaderNavbar: FC = ({ activeAssistant, setActiveAssistant, activeTopic, setActiveTopic }) => { +const HeaderNavbar: FC = ({ + activeAssistant, + setActiveAssistant, + activeTopic, + setActiveTopic, + activeTopicOrSession +}) => { const { assistant } = useAssistant(activeAssistant.id) const { showAssistants, toggleShowAssistants } = useShowAssistants() const { topicPosition, narrowMode } = useSettings() const { showTopics, toggleShowTopics } = useShowTopics() + const chatMaxWidth = useChatMaxWidth() const dispatch = useAppDispatch() useShortcut('toggle_show_assistants', toggleShowAssistants) @@ -113,15 +123,29 @@ const HeaderNavbar: FC = ({ activeAssistant, setActiveAssistant, activeTo )} - - - + + {activeTopicOrSession === 'topic' ? ( + + + + ) : ( + + + + )} + + @@ -196,4 +220,15 @@ const NarrowIcon = styled(NavbarIcon)` } ` +const ChatNavbarContainer: React.FC<{ children: React.ReactNode; style?: React.CSSProperties }> = ({ + children, + style +}) => { + return ( +
+ {children} +
+ ) +} + export default HeaderNavbar diff --git a/src/renderer/src/pages/home/components/ChatNavbarContent.tsx b/src/renderer/src/pages/home/components/ChatNavbarContent.tsx new file mode 100644 index 0000000000..5d89bfd94e --- /dev/null +++ b/src/renderer/src/pages/home/components/ChatNavbarContent.tsx @@ -0,0 +1,136 @@ +import { BreadcrumbItem, Breadcrumbs, Chip, cn } from '@heroui/react' +import HorizontalScrollContainer from '@renderer/components/HorizontalScrollContainer' +import { permissionModeCards } from '@renderer/constants/permissionModes' +import { useAgent } from '@renderer/hooks/agents/useAgent' +import { useSession } from '@renderer/hooks/agents/useSession' +import { useUpdateAgent } from '@renderer/hooks/agents/useUpdateAgent' +import { useRuntime } from '@renderer/hooks/useRuntime' +import { ApiModel, Assistant, PermissionMode } from '@renderer/types' +import { formatErrorMessageWithPrefix } from '@renderer/utils/error' +import { t } from 'i18next' +import { FC, ReactNode, useCallback } from 'react' + +import { AgentSettingsPopup } from '../../settings/AgentSettings' +import { AgentLabel } from '../../settings/AgentSettings/shared' +import SelectAgentModelButton from './SelectAgentModelButton' +import SelectModelButton from './SelectModelButton' + +interface Props { + assistant: Assistant +} + +const ChatNavbarContent: FC = ({ assistant }) => { + const { chat } = useRuntime() + const { activeTopicOrSession, activeAgentId } = chat + const sessionId = activeAgentId ? (chat.activeSessionId[activeAgentId] ?? null) : null + const { agent } = useAgent(activeAgentId) + const { updateModel } = useUpdateAgent() + + const handleUpdateModel = useCallback( + async (model: ApiModel) => { + if (!agent) return + return updateModel(agent.id, model.id, { showSuccessToast: false }) + }, + [agent, updateModel] + ) + + return ( + <> + {activeTopicOrSession === 'topic' && } + {activeTopicOrSession === 'session' && agent && ( + + + AgentSettingsPopup.show({ agentId: agent.id })} + classNames={{ base: 'self-stretch', item: 'h-full' }}> + + + + + + + + {activeAgentId && sessionId && ( + + + + )} + + + )} + + ) +} + +const SessionWorkspaceMeta: FC<{ agentId: string; sessionId: string }> = ({ agentId, sessionId }) => { + const { agent } = useAgent(agentId) + const { session } = useSession(agentId, sessionId) + if (!session || !agent) { + return null + } + + const firstAccessiblePath = session.accessible_paths?.[0] + const permissionMode = (session.configuration?.permission_mode ?? 'default') as PermissionMode + const permissionModeCard = permissionModeCards.find((card) => card.mode === permissionMode) + const permissionModeLabel = permissionModeCard + ? t(permissionModeCard.titleKey, permissionModeCard.titleFallback) + : permissionMode + + const infoItems: ReactNode[] = [] + + const InfoTag = ({ + text, + className, + onClick + }: { + text: string + className?: string + classNames?: {} + onClick?: (e: React.MouseEvent) => void + }) => ( +
+ {text} +
+ ) + + // infoItems.push() + + if (firstAccessiblePath) { + infoItems.push( + { + window.api.file + .openPath(firstAccessiblePath) + .catch((e) => + window.toast.error( + formatErrorMessageWithPrefix(e, t('files.error.open_path', { path: firstAccessiblePath })) + ) + ) + }} + /> + ) + } + + infoItems.push() + + if (infoItems.length === 0) { + return null + } + + return
{infoItems}
+} + +export default ChatNavbarContent From dc5bc64040b8e4ddb2293fc0d454543b098f3e12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=A2=E5=A5=8B=E7=8C=AB?= Date: Fri, 17 Oct 2025 10:36:36 +0800 Subject: [PATCH 08/27] =?UTF-8?q?fix:=20update=20default=20enableTopP=20se?= =?UTF-8?q?tting=20to=20false=20in=20AssistantModelSett=E2=80=A6=20(#10754?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix: update default enableTopP setting to false in AssistantModelSettings and DefaultAssistantSettings - Changed default value of enableTopP from true to false in AssistantModelSettings and DefaultAssistantSettings components. - Updated related logic to ensure consistent behavior across settings. --- .../settings/AssistantSettings/AssistantModelSettings.tsx | 6 +++--- .../settings/ModelSettings/DefaultAssistantSettings.tsx | 6 +++--- src/renderer/src/services/AssistantService.ts | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/renderer/src/pages/settings/AssistantSettings/AssistantModelSettings.tsx b/src/renderer/src/pages/settings/AssistantSettings/AssistantModelSettings.tsx index e69f322513..a2efdf387d 100644 --- a/src/renderer/src/pages/settings/AssistantSettings/AssistantModelSettings.tsx +++ b/src/renderer/src/pages/settings/AssistantSettings/AssistantModelSettings.tsx @@ -33,7 +33,7 @@ const AssistantModelSettings: FC = ({ assistant, updateAssistant, updateA const [toolUseMode, setToolUseMode] = useState(assistant?.settings?.toolUseMode ?? 'prompt') const [defaultModel, setDefaultModel] = useState(assistant?.defaultModel) const [topP, setTopP] = useState(assistant?.settings?.topP ?? 1) - const [enableTopP, setEnableTopP] = useState(assistant?.settings?.enableTopP ?? true) + const [enableTopP, setEnableTopP] = useState(assistant?.settings?.enableTopP ?? false) const [customParameters, setCustomParameters] = useState( assistant?.settings?.customParameters ?? [] ) @@ -164,7 +164,7 @@ const AssistantModelSettings: FC = ({ assistant, updateAssistant, updateA setMaxTokens(0) setStreamOutput(true) setTopP(1) - setEnableTopP(true) + setEnableTopP(false) setCustomParameters([]) setToolUseMode('prompt') updateAssistantSettings({ @@ -175,7 +175,7 @@ const AssistantModelSettings: FC = ({ assistant, updateAssistant, updateA maxTokens: 0, streamOutput: true, topP: 1, - enableTopP: true, + enableTopP: false, customParameters: [], toolUseMode: 'prompt' }) diff --git a/src/renderer/src/pages/settings/ModelSettings/DefaultAssistantSettings.tsx b/src/renderer/src/pages/settings/ModelSettings/DefaultAssistantSettings.tsx index b059174644..0963274162 100644 --- a/src/renderer/src/pages/settings/ModelSettings/DefaultAssistantSettings.tsx +++ b/src/renderer/src/pages/settings/ModelSettings/DefaultAssistantSettings.tsx @@ -24,7 +24,7 @@ const AssistantSettings: FC = () => { const [enableMaxTokens, setEnableMaxTokens] = useState(defaultAssistant?.settings?.enableMaxTokens ?? false) const [maxTokens, setMaxTokens] = useState(defaultAssistant?.settings?.maxTokens ?? 0) const [topP, setTopP] = useState(defaultAssistant.settings?.topP ?? 1) - const [enableTopP, setEnableTopP] = useState(defaultAssistant.settings?.enableTopP ?? true) + const [enableTopP, setEnableTopP] = useState(defaultAssistant.settings?.enableTopP ?? false) const [emoji, setEmoji] = useState(defaultAssistant.emoji || getLeadingEmoji(defaultAssistant.name) || '') const [name, setName] = useState( defaultAssistant.name.replace(getLeadingEmoji(defaultAssistant.name) || '', '').trim() @@ -71,7 +71,7 @@ const AssistantSettings: FC = () => { setEnableMaxTokens(false) setMaxTokens(0) setTopP(1) - setEnableTopP(true) + setEnableTopP(false) updateDefaultAssistant({ ...defaultAssistant, settings: { @@ -83,7 +83,7 @@ const AssistantSettings: FC = () => { maxTokens: DEFAULT_MAX_TOKENS, streamOutput: true, topP: 1, - enableTopP: true + enableTopP: false } }) } diff --git a/src/renderer/src/services/AssistantService.ts b/src/renderer/src/services/AssistantService.ts index 2b083f48b9..2bfcde9c78 100644 --- a/src/renderer/src/services/AssistantService.ts +++ b/src/renderer/src/services/AssistantService.ts @@ -35,7 +35,7 @@ export const DEFAULT_ASSISTANT_SETTINGS: AssistantSettings = { maxTokens: 0, streamOutput: true, topP: 1, - enableTopP: true, + enableTopP: false, toolUseMode: 'prompt', customParameters: [] } @@ -169,7 +169,7 @@ export const getAssistantSettings = (assistant: Assistant): AssistantSettings => temperature: assistant?.settings?.temperature ?? DEFAULT_TEMPERATURE, enableTemperature: assistant?.settings?.enableTemperature ?? true, topP: assistant?.settings?.topP ?? 1, - enableTopP: assistant?.settings?.enableTopP ?? true, + enableTopP: assistant?.settings?.enableTopP ?? false, enableMaxTokens: assistant?.settings?.enableMaxTokens ?? false, maxTokens: getAssistantMaxTokens(), streamOutput: assistant?.settings?.streamOutput ?? true, From fb680ce7645bfd1283e90c64b353f009f46a7f15 Mon Sep 17 00:00:00 2001 From: SongSong <32863696+SmallSongSong@users.noreply.github.com> Date: Fri, 17 Oct 2025 10:37:07 +0800 Subject: [PATCH 09/27] feat: add built-in DiDi MCP server integration (#10318) * feat: add built-in DiDi MCP server integration - Add DiDi MCP server implementation with ride-hailing services - Support map search, price estimation, order management, and driver tracking - Add multilingual translations for DiDi MCP server descriptions - Available only in mainland China, requires DIDI_API_KEY environment variable * fix: resolve code formatting issues in DiDi MCP server fixes code formatting issues in the DiDi MCP server implementation to resolve CI format check failures. --------- Co-authored-by: BillySong --- src/main/mcpServers/didi-mcp.ts | 473 +++++++++++++++++++++ src/main/mcpServers/factory.ts | 5 + src/renderer/src/i18n/label.ts | 3 +- src/renderer/src/i18n/locales/en-us.json | 1 + src/renderer/src/i18n/locales/zh-cn.json | 1 + src/renderer/src/i18n/locales/zh-tw.json | 1 + src/renderer/src/i18n/translate/el-gr.json | 1 + src/renderer/src/i18n/translate/es-es.json | 1 + src/renderer/src/i18n/translate/fr-fr.json | 1 + src/renderer/src/i18n/translate/ja-jp.json | 1 + src/renderer/src/i18n/translate/pt-pt.json | 1 + src/renderer/src/i18n/translate/ru-ru.json | 1 + src/renderer/src/store/mcp.ts | 12 + src/renderer/src/types/index.ts | 3 +- 14 files changed, 503 insertions(+), 2 deletions(-) create mode 100644 src/main/mcpServers/didi-mcp.ts diff --git a/src/main/mcpServers/didi-mcp.ts b/src/main/mcpServers/didi-mcp.ts new file mode 100644 index 0000000000..905fc4ff81 --- /dev/null +++ b/src/main/mcpServers/didi-mcp.ts @@ -0,0 +1,473 @@ +/** + * DiDi MCP Server Implementation + * + * Based on official DiDi MCP API capabilities. + * API Documentation: https://mcp.didichuxing.com/api?tap=api + * + * Provides ride-hailing services including map search, price estimation, + * order management, and driver tracking. + * + * Note: Only available in Mainland China. + */ + +import { loggerService } from '@logger' +import { Server } from '@modelcontextprotocol/sdk/server/index.js' +import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js' + +const logger = loggerService.withContext('DiDiMCPServer') + +export class DiDiMcpServer { + private _server: Server + private readonly baseUrl = 'http://mcp.didichuxing.com/mcp-servers' + private apiKey: string + + constructor(apiKey?: string) { + this._server = new Server( + { + name: 'didi-mcp-server', + version: '0.1.0' + }, + { + capabilities: { + tools: {} + } + } + ) + + // Get API key from parameter or environment variables + this.apiKey = apiKey || process.env.DIDI_API_KEY || '' + if (!this.apiKey) { + logger.warn('DIDI_API_KEY environment variable is not set') + } + + this.setupRequestHandlers() + } + + get server(): Server { + return this._server + } + + private setupRequestHandlers() { + // List available tools + this._server.setRequestHandler(ListToolsRequestSchema, async () => { + return { + tools: [ + { + name: 'maps_textsearch', + description: 'Search for POI locations based on keywords and city', + inputSchema: { + type: 'object', + properties: { + city: { + type: 'string', + description: 'Query city' + }, + keywords: { + type: 'string', + description: 'Search keywords' + }, + location: { + type: 'string', + description: 'Location coordinates, format: longitude,latitude' + } + }, + required: ['keywords', 'city'] + } + }, + { + name: 'taxi_cancel_order', + description: 'Cancel a taxi order', + inputSchema: { + type: 'object', + properties: { + order_id: { + type: 'string', + description: 'Order ID from order creation or query results' + }, + reason: { + type: 'string', + description: + 'Cancellation reason (optional). Examples: no longer needed, waiting too long, urgent matter' + } + }, + required: ['order_id'] + } + }, + { + name: 'taxi_create_order', + description: 'Create taxi order directly via API without opening any app interface', + inputSchema: { + type: 'object', + properties: { + caller_car_phone: { + type: 'string', + description: 'Caller phone number (optional)' + }, + estimate_trace_id: { + type: 'string', + description: 'Estimation trace ID from estimation results' + }, + product_category: { + type: 'string', + description: 'Vehicle category ID from estimation results, comma-separated for multiple types' + } + }, + required: ['product_category', 'estimate_trace_id'] + } + }, + { + name: 'taxi_estimate', + description: 'Get available ride-hailing vehicle types and fare estimates', + inputSchema: { + type: 'object', + properties: { + from_lat: { + type: 'string', + description: 'Departure latitude, must be from map tools' + }, + from_lng: { + type: 'string', + description: 'Departure longitude, must be from map tools' + }, + from_name: { + type: 'string', + description: 'Departure location name' + }, + to_lat: { + type: 'string', + description: 'Destination latitude, must be from map tools' + }, + to_lng: { + type: 'string', + description: 'Destination longitude, must be from map tools' + }, + to_name: { + type: 'string', + description: 'Destination name' + } + }, + required: ['from_lng', 'from_lat', 'from_name', 'to_lng', 'to_lat', 'to_name'] + } + }, + { + name: 'taxi_generate_ride_app_link', + description: 'Generate deep links to open ride-hailing apps based on origin, destination and vehicle type', + inputSchema: { + type: 'object', + properties: { + from_lat: { + type: 'string', + description: 'Departure latitude, must be from map tools' + }, + from_lng: { + type: 'string', + description: 'Departure longitude, must be from map tools' + }, + product_category: { + type: 'string', + description: 'Vehicle category IDs from estimation results, comma-separated for multiple types' + }, + to_lat: { + type: 'string', + description: 'Destination latitude, must be from map tools' + }, + to_lng: { + type: 'string', + description: 'Destination longitude, must be from map tools' + } + }, + required: ['from_lng', 'from_lat', 'to_lng', 'to_lat'] + } + }, + { + name: 'taxi_get_driver_location', + description: 'Get real-time driver location for a taxi order', + inputSchema: { + type: 'object', + properties: { + order_id: { + type: 'string', + description: 'Taxi order ID' + } + }, + required: ['order_id'] + } + }, + { + name: 'taxi_query_order', + description: 'Query taxi order status and information such as driver contact, license plate, ETA', + inputSchema: { + type: 'object', + properties: { + order_id: { + type: 'string', + description: 'Order ID from order creation results, if available; otherwise queries incomplete orders' + } + } + } + } + ] + } + }) + + // Handle tool calls + this._server.setRequestHandler(CallToolRequestSchema, async (request) => { + const { name, arguments: args } = request.params + + try { + switch (name) { + case 'maps_textsearch': + return await this.handleMapsTextSearch(args) + case 'taxi_cancel_order': + return await this.handleTaxiCancelOrder(args) + case 'taxi_create_order': + return await this.handleTaxiCreateOrder(args) + case 'taxi_estimate': + return await this.handleTaxiEstimate(args) + case 'taxi_generate_ride_app_link': + return await this.handleTaxiGenerateRideAppLink(args) + case 'taxi_get_driver_location': + return await this.handleTaxiGetDriverLocation(args) + case 'taxi_query_order': + return await this.handleTaxiQueryOrder(args) + default: + throw new Error(`Unknown tool: ${name}`) + } + } catch (error) { + logger.error(`Error calling tool ${name}:`, error as Error) + throw error + } + }) + } + + private async handleMapsTextSearch(args: any) { + const { city, keywords, location } = args + + const params = { + name: 'maps_textsearch', + arguments: { + keywords, + city, + ...(location && { location }) + } + } + + try { + const response = await this.makeRequest('tools/call', params) + return { + content: [ + { + type: 'text', + text: JSON.stringify(response, null, 2) + } + ] + } + } catch (error) { + logger.error('Maps text search error:', error as Error) + throw error + } + } + + private async handleTaxiCancelOrder(args: any) { + const { order_id, reason } = args + + const params = { + name: 'taxi_cancel_order', + arguments: { + order_id, + ...(reason && { reason }) + } + } + + try { + const response = await this.makeRequest('tools/call', params) + return { + content: [ + { + type: 'text', + text: JSON.stringify(response, null, 2) + } + ] + } + } catch (error) { + logger.error('Taxi cancel order error:', error as Error) + throw error + } + } + + private async handleTaxiCreateOrder(args: any) { + const { caller_car_phone, estimate_trace_id, product_category } = args + + const params = { + name: 'taxi_create_order', + arguments: { + product_category, + estimate_trace_id, + ...(caller_car_phone && { caller_car_phone }) + } + } + + try { + const response = await this.makeRequest('tools/call', params) + return { + content: [ + { + type: 'text', + text: JSON.stringify(response, null, 2) + } + ] + } + } catch (error) { + logger.error('Taxi create order error:', error as Error) + throw error + } + } + + private async handleTaxiEstimate(args: any) { + const { from_lng, from_lat, from_name, to_lng, to_lat, to_name } = args + + const params = { + name: 'taxi_estimate', + arguments: { + from_lng, + from_lat, + from_name, + to_lng, + to_lat, + to_name + } + } + + try { + const response = await this.makeRequest('tools/call', params) + return { + content: [ + { + type: 'text', + text: JSON.stringify(response, null, 2) + } + ] + } + } catch (error) { + logger.error('Taxi estimate error:', error as Error) + throw error + } + } + + private async handleTaxiGenerateRideAppLink(args: any) { + const { from_lng, from_lat, to_lng, to_lat, product_category } = args + + const params = { + name: 'taxi_generate_ride_app_link', + arguments: { + from_lng, + from_lat, + to_lng, + to_lat, + ...(product_category && { product_category }) + } + } + + try { + const response = await this.makeRequest('tools/call', params) + return { + content: [ + { + type: 'text', + text: JSON.stringify(response, null, 2) + } + ] + } + } catch (error) { + logger.error('Taxi generate ride app link error:', error as Error) + throw error + } + } + + private async handleTaxiGetDriverLocation(args: any) { + const { order_id } = args + + const params = { + name: 'taxi_get_driver_location', + arguments: { + order_id + } + } + + try { + const response = await this.makeRequest('tools/call', params) + return { + content: [ + { + type: 'text', + text: JSON.stringify(response, null, 2) + } + ] + } + } catch (error) { + logger.error('Taxi get driver location error:', error as Error) + throw error + } + } + + private async handleTaxiQueryOrder(args: any) { + const { order_id } = args + + const params = { + name: 'taxi_query_order', + arguments: { + ...(order_id && { order_id }) + } + } + + try { + const response = await this.makeRequest('tools/call', params) + return { + content: [ + { + type: 'text', + text: JSON.stringify(response, null, 2) + } + ] + } + } catch (error) { + logger.error('Taxi query order error:', error as Error) + throw error + } + } + + private async makeRequest(method: string, params: any): Promise { + const requestData = { + jsonrpc: '2.0', + method: method, + id: Date.now(), + ...(Object.keys(params).length > 0 && { params }) + } + + // API key is passed as URL parameter + const url = `${this.baseUrl}?key=${this.apiKey}` + + const response = await fetch(url, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(requestData) + }) + + if (!response.ok) { + const errorText = await response.text() + throw new Error(`HTTP ${response.status}: ${errorText}`) + } + + const data = await response.json() + + if (data.error) { + throw new Error(`API Error: ${JSON.stringify(data.error)}`) + } + + return data.result + } +} + +export default DiDiMcpServer diff --git a/src/main/mcpServers/factory.ts b/src/main/mcpServers/factory.ts index 46d3bb87d2..74266d7d35 100644 --- a/src/main/mcpServers/factory.ts +++ b/src/main/mcpServers/factory.ts @@ -3,6 +3,7 @@ import { Server } from '@modelcontextprotocol/sdk/server/index.js' import { BuiltinMCPServerName, BuiltinMCPServerNames } from '@types' import BraveSearchServer from './brave-search' +import DiDiMcpServer from './didi-mcp' import DifyKnowledgeServer from './dify-knowledge' import FetchServer from './fetch' import FileSystemServer from './filesystem' @@ -42,6 +43,10 @@ export function createInMemoryMCPServer( case BuiltinMCPServerNames.python: { return new PythonServer().server } + case BuiltinMCPServerNames.didiMCP: { + const apiKey = envs.DIDI_API_KEY + return new DiDiMcpServer(apiKey).server + } default: throw new Error(`Unknown in-memory MCP server: ${name}`) } diff --git a/src/renderer/src/i18n/label.ts b/src/renderer/src/i18n/label.ts index 376aec32e3..c2ac69f1be 100644 --- a/src/renderer/src/i18n/label.ts +++ b/src/renderer/src/i18n/label.ts @@ -329,7 +329,8 @@ const builtInMcpDescriptionKeyMap: Record = { [BuiltinMCPServerNames.fetch]: 'settings.mcp.builtinServersDescriptions.fetch', [BuiltinMCPServerNames.filesystem]: 'settings.mcp.builtinServersDescriptions.filesystem', [BuiltinMCPServerNames.difyKnowledge]: 'settings.mcp.builtinServersDescriptions.dify_knowledge', - [BuiltinMCPServerNames.python]: 'settings.mcp.builtinServersDescriptions.python' + [BuiltinMCPServerNames.python]: 'settings.mcp.builtinServersDescriptions.python', + [BuiltinMCPServerNames.didiMCP]: 'settings.mcp.builtinServersDescriptions.didi_mcp' } as const export const getBuiltInMcpServerDescriptionLabel = (key: string): string => { diff --git a/src/renderer/src/i18n/locales/en-us.json b/src/renderer/src/i18n/locales/en-us.json index 1f7ec61e2f..ee3d0ff110 100644 --- a/src/renderer/src/i18n/locales/en-us.json +++ b/src/renderer/src/i18n/locales/en-us.json @@ -3574,6 +3574,7 @@ "builtinServers": "Builtin Servers", "builtinServersDescriptions": { "brave_search": "An MCP server implementation integrating the Brave Search API, providing both web and local search functionalities. Requires configuring the BRAVE_API_KEY environment variable", + "didi_mcp": "DiDi MCP server providing ride-hailing services including map search, price estimation, order management, and driver tracking. Only available in Mainland China. Requires configuring the DIDI_API_KEY environment variable", "dify_knowledge": "Dify's MCP server implementation provides a simple API to interact with Dify. Requires configuring the Dify Key", "fetch": "MCP server for retrieving URL web content", "filesystem": "A Node.js server implementing the Model Context Protocol (MCP) for file system operations. Requires configuration of directories allowed for access.", diff --git a/src/renderer/src/i18n/locales/zh-cn.json b/src/renderer/src/i18n/locales/zh-cn.json index 7926e7396d..e5573221e7 100644 --- a/src/renderer/src/i18n/locales/zh-cn.json +++ b/src/renderer/src/i18n/locales/zh-cn.json @@ -3574,6 +3574,7 @@ "builtinServers": "内置服务器", "builtinServersDescriptions": { "brave_search": "一个集成了Brave 搜索 API 的 MCP 服务器实现,提供网页与本地搜索双重功能。需要配置 BRAVE_API_KEY 环境变量", + "didi_mcp": "一个集成了滴滴 MCP 服务器实现,提供网约车服务包括地图搜索、价格预估、订单管理和司机跟踪。仅支持中国大陆地区。需要配置 DIDI_API_KEY 环境变量", "dify_knowledge": "Dify 的 MCP 服务器实现,提供了一个简单的 API 来与 Dify 进行交互。需要配置 Dify Key", "fetch": "用于获取 URL 网页内容的 MCP 服务器", "filesystem": "实现文件系统操作的模型上下文协议(MCP)的 Node.js 服务器。需要配置允许访问的目录", diff --git a/src/renderer/src/i18n/locales/zh-tw.json b/src/renderer/src/i18n/locales/zh-tw.json index ab96bff08a..8634254e55 100644 --- a/src/renderer/src/i18n/locales/zh-tw.json +++ b/src/renderer/src/i18n/locales/zh-tw.json @@ -3574,6 +3574,7 @@ "builtinServers": "內置伺服器", "builtinServersDescriptions": { "brave_search": "一個集成了Brave 搜索 API 的 MCP 伺服器實現,提供網頁與本地搜尋雙重功能。需要配置 BRAVE_API_KEY 環境變數", + "didi_mcp": "一個集成了滴滴 MCP 伺服器實現,提供網約車服務包括地圖搜尋、價格預估、訂單管理和司機追蹤。僅支援中國大陸地區。需要配置 DIDI_API_KEY 環境變數", "dify_knowledge": "Dify 的 MCP 伺服器實現,提供了一個簡單的 API 來與 Dify 進行互動。需要配置 Dify Key", "fetch": "用於獲取 URL 網頁內容的 MCP 伺服器", "filesystem": "實現文件系統操作的模型上下文協議(MCP)的 Node.js 伺服器。需要配置允許訪問的目錄", diff --git a/src/renderer/src/i18n/translate/el-gr.json b/src/renderer/src/i18n/translate/el-gr.json index a39c429d1c..370f3ce658 100644 --- a/src/renderer/src/i18n/translate/el-gr.json +++ b/src/renderer/src/i18n/translate/el-gr.json @@ -3574,6 +3574,7 @@ "builtinServers": "Ενσωματωμένοι Διακομιστές", "builtinServersDescriptions": { "brave_search": "μια εφαρμογή διακομιστή MCP που ενσωματώνει το Brave Search API, παρέχοντας δυνατότητες αναζήτησης στον ιστό και τοπικής αναζήτησης. Απαιτείται η ρύθμιση της μεταβλητής περιβάλλοντος BRAVE_API_KEY", + "didi_mcp": "Διακομιστής DiDi MCP που παρέχει υπηρεσίες μεταφοράς συμπεριλαμβανομένης της αναζήτησης χαρτών, εκτίμησης τιμών, διαχείρισης παραγγελιών και παρακολούθησης οδηγών. Διαθέσιμο μόνο στην ηπειρωτική Κίνα. Απαιτεί διαμόρφωση της μεταβλητής περιβάλλοντος DIDI_API_KEY", "dify_knowledge": "Η υλοποίηση του Dify για τον διακομιστή MCP, παρέχει μια απλή API για να αλληλεπιδρά με το Dify. Απαιτείται η ρύθμιση του κλειδιού Dify", "fetch": "Εξυπηρετητής MCP για λήψη περιεχομένου ιστοσελίδας URL", "filesystem": "Εξυπηρετητής Node.js για το πρωτόκολλο περιβάλλοντος μοντέλου (MCP) που εφαρμόζει λειτουργίες συστήματος αρχείων. Απαιτείται διαμόρφωση για την επιτροπή πρόσβασης σε καταλόγους", diff --git a/src/renderer/src/i18n/translate/es-es.json b/src/renderer/src/i18n/translate/es-es.json index 219f809187..0c923c9735 100644 --- a/src/renderer/src/i18n/translate/es-es.json +++ b/src/renderer/src/i18n/translate/es-es.json @@ -3574,6 +3574,7 @@ "builtinServers": "Servidores integrados", "builtinServersDescriptions": { "brave_search": "Una implementación de servidor MCP que integra la API de búsqueda de Brave, proporcionando funciones de búsqueda web y búsqueda local. Requiere configurar la variable de entorno BRAVE_API_KEY", + "didi_mcp": "Servidor DiDi MCP que proporciona servicios de transporte incluyendo búsqueda de mapas, estimación de precios, gestión de pedidos y seguimiento de conductores. Disponible solo en China Continental. Requiere configurar la variable de entorno DIDI_API_KEY", "dify_knowledge": "Implementación del servidor MCP de Dify, que proporciona una API sencilla para interactuar con Dify. Se requiere configurar la clave de Dify.", "fetch": "Servidor MCP para obtener el contenido de la página web de una URL", "filesystem": "Servidor Node.js que implementa el protocolo de contexto del modelo (MCP) para operaciones del sistema de archivos. Requiere configuración del directorio permitido para el acceso", diff --git a/src/renderer/src/i18n/translate/fr-fr.json b/src/renderer/src/i18n/translate/fr-fr.json index 72899c3c13..9e36984cbf 100644 --- a/src/renderer/src/i18n/translate/fr-fr.json +++ b/src/renderer/src/i18n/translate/fr-fr.json @@ -3574,6 +3574,7 @@ "builtinServers": "Serveurs intégrés", "builtinServersDescriptions": { "brave_search": "Une implémentation de serveur MCP intégrant l'API de recherche Brave, offrant des fonctionnalités de recherche web et locale. Nécessite la configuration de la variable d'environnement BRAVE_API_KEY", + "didi_mcp": "Serveur DiDi MCP fournissant des services de transport incluant la recherche de cartes, l'estimation des prix, la gestion des commandes et le suivi des conducteurs. Disponible uniquement en Chine continentale. Nécessite la configuration de la variable d'environnement DIDI_API_KEY", "dify_knowledge": "Implémentation du serveur MCP de Dify, fournissant une API simple pour interagir avec Dify. Nécessite la configuration de la clé Dify", "fetch": "serveur MCP utilisé pour récupérer le contenu des pages web URL", "filesystem": "Serveur Node.js implémentant le protocole de contexte de modèle (MCP) pour les opérations de système de fichiers. Nécessite une configuration des répertoires autorisés à être accédés.", diff --git a/src/renderer/src/i18n/translate/ja-jp.json b/src/renderer/src/i18n/translate/ja-jp.json index 5c7e77400c..d9db953aef 100644 --- a/src/renderer/src/i18n/translate/ja-jp.json +++ b/src/renderer/src/i18n/translate/ja-jp.json @@ -3574,6 +3574,7 @@ "builtinServers": "組み込みサーバー", "builtinServersDescriptions": { "brave_search": "Brave検索APIを統合したMCPサーバーの実装で、ウェブ検索とローカル検索の両機能を提供します。BRAVE_API_KEY環境変数の設定が必要です", + "didi_mcp": "DiDi MCPサーバーは、地図検索、料金見積もり、注文管理、ドライバー追跡を含むライドシェアサービスを提供します。中国本土でのみ利用可能です。DIDI_API_KEY環境変数の設定が必要です", "dify_knowledge": "DifyのMCPサーバー実装は、Difyと対話するためのシンプルなAPIを提供します。Dify Keyの設定が必要です。", "fetch": "URLのウェブページコンテンツを取得するためのMCPサーバー", "filesystem": "Node.jsサーバーによるファイルシステム操作を実現するモデルコンテキストプロトコル(MCP)。アクセスを許可するディレクトリの設定が必要です", diff --git a/src/renderer/src/i18n/translate/pt-pt.json b/src/renderer/src/i18n/translate/pt-pt.json index f10aea90df..8f46afda0c 100644 --- a/src/renderer/src/i18n/translate/pt-pt.json +++ b/src/renderer/src/i18n/translate/pt-pt.json @@ -3574,6 +3574,7 @@ "builtinServers": "Servidores integrados", "builtinServersDescriptions": { "brave_search": "uma implementação de servidor MCP integrada com a API de pesquisa Brave, fornecendo funcionalidades de pesquisa web e local. Requer a configuração da variável de ambiente BRAVE_API_KEY", + "didi_mcp": "Servidor DiDi MCP que fornece serviços de transporte incluindo pesquisa de mapas, estimativa de preços, gestão de pedidos e rastreamento de motoristas. Disponível apenas na China Continental. Requer configuração da variável de ambiente DIDI_API_KEY", "dify_knowledge": "Implementação do servidor MCP do Dify, que fornece uma API simples para interagir com o Dify. Requer a configuração da chave Dify", "fetch": "servidor MCP para obter o conteúdo da página web do URL", "filesystem": "Servidor Node.js do protocolo de contexto de modelo (MCP) para implementar operações de sistema de ficheiros. Requer configuração do diretório permitido para acesso", diff --git a/src/renderer/src/i18n/translate/ru-ru.json b/src/renderer/src/i18n/translate/ru-ru.json index bf4533a4ac..8bf1b823c5 100644 --- a/src/renderer/src/i18n/translate/ru-ru.json +++ b/src/renderer/src/i18n/translate/ru-ru.json @@ -3574,6 +3574,7 @@ "builtinServers": "Встроенные серверы", "builtinServersDescriptions": { "brave_search": "реализация сервера MCP с интеграцией API поиска Brave, обеспечивающая функции веб-поиска и локального поиска. Требуется настройка переменной среды BRAVE_API_KEY", + "didi_mcp": "Сервер DiDi MCP, предоставляющий услуги такси, включая поиск на карте, оценку стоимости, управление заказами и отслеживание водителей. Доступен только в материковом Китае. Требует настройки переменной окружения DIDI_API_KEY", "dify_knowledge": "Реализация сервера MCP Dify, предоставляющая простой API для взаимодействия с Dify. Требуется настройка ключа Dify", "fetch": "MCP-сервер для получения содержимого веб-страниц по URL", "filesystem": "Node.js-сервер протокола контекста модели (MCP) для реализации операций файловой системы. Требуется настройка каталогов, к которым разрешён доступ", diff --git a/src/renderer/src/store/mcp.ts b/src/renderer/src/store/mcp.ts index 2bae82c147..eb659399be 100644 --- a/src/renderer/src/store/mcp.ts +++ b/src/renderer/src/store/mcp.ts @@ -144,6 +144,18 @@ export const builtinMCPServers: BuiltinMCPServer[] = [ type: 'inMemory', isActive: false, provider: 'CherryAI' + }, + { + id: nanoid(), + name: '@cherry/didi-mcp', + reference: 'https://mcp.didichuxing.com/', + type: 'inMemory', + isActive: false, + env: { + DIDI_API_KEY: 'YOUR_DIDI_API_KEY' + }, + shouldConfig: true, + provider: 'CherryAI' } ] as const diff --git a/src/renderer/src/types/index.ts b/src/renderer/src/types/index.ts index 7bdf186e62..82df2c1f21 100644 --- a/src/renderer/src/types/index.ts +++ b/src/renderer/src/types/index.ts @@ -691,7 +691,8 @@ export const BuiltinMCPServerNames = { fetch: '@cherry/fetch', filesystem: '@cherry/filesystem', difyKnowledge: '@cherry/dify-knowledge', - python: '@cherry/python' + python: '@cherry/python', + didiMCP: '@cherry/didi-mcp' } as const export type BuiltinMCPServerName = (typeof BuiltinMCPServerNames)[keyof typeof BuiltinMCPServerNames] From 1f7d2fa93fc6af82eaf94da02c7cea362dfd1156 Mon Sep 17 00:00:00 2001 From: defi-failure <159208748+defi-failure@users.noreply.github.com> Date: Fri, 17 Oct 2025 10:38:52 +0800 Subject: [PATCH 10/27] feat: notes full text search (#10640) * feat: notes full text search initial commit * fix: update highlight overlay when scroll * fix: reset note search result properly * refactor: extract scrollToLine logic from CodeEditor into a custom hook * fix: hide match overlay when overlap * fix: truncate line with ellipsis around search match for better visibility * fix: unified note search match highlight style --- .../src/components/CodeEditor/hooks.ts | 79 ++++- .../src/components/CodeEditor/index.tsx | 8 +- src/renderer/src/components/HighlightText.tsx | 48 +++ .../src/components/RichEditor/constants.ts | 2 + .../src/components/RichEditor/index.tsx | 167 +++++++++ .../src/components/RichEditor/types.ts | 2 + .../components/RichEditor/useRichEditor.ts | 28 ++ src/renderer/src/i18n/locales/en-us.json | 8 + src/renderer/src/i18n/locales/zh-cn.json | 8 + src/renderer/src/i18n/locales/zh-tw.json | 8 + src/renderer/src/pages/notes/NotesEditor.tsx | 6 +- src/renderer/src/pages/notes/NotesPage.tsx | 78 ++++ src/renderer/src/pages/notes/NotesSidebar.tsx | 335 +++++++++++++++++- .../pages/notes/hooks/useFullTextSearch.ts | 159 +++++++++ src/renderer/src/services/EventService.ts | 1 + .../src/services/NotesSearchService.ts | 262 ++++++++++++++ .../utils/__tests__/markdownConverter.test.ts | 93 +++-- src/renderer/src/utils/markdownConverter.ts | 66 +++- 18 files changed, 1306 insertions(+), 52 deletions(-) create mode 100644 src/renderer/src/components/HighlightText.tsx create mode 100644 src/renderer/src/components/RichEditor/constants.ts create mode 100644 src/renderer/src/pages/notes/hooks/useFullTextSearch.ts create mode 100644 src/renderer/src/services/NotesSearchService.ts diff --git a/src/renderer/src/components/CodeEditor/hooks.ts b/src/renderer/src/components/CodeEditor/hooks.ts index b6689644e9..65c18a5a0f 100644 --- a/src/renderer/src/components/CodeEditor/hooks.ts +++ b/src/renderer/src/components/CodeEditor/hooks.ts @@ -2,7 +2,7 @@ import { linter } from '@codemirror/lint' // statically imported by @uiw/codemir import { EditorView } from '@codemirror/view' import { loggerService } from '@logger' import { Extension, keymap } from '@uiw/react-codemirror' -import { useEffect, useMemo, useState } from 'react' +import { useCallback, useEffect, useMemo, useState } from 'react' import { getNormalizedExtension } from './utils' @@ -203,3 +203,80 @@ export function useHeightListener({ onHeightChange }: UseHeightListenerProps) { }) }, [onHeightChange]) } + +interface UseScrollToLineOptions { + highlight?: boolean +} + +export function useScrollToLine(editorViewRef: React.MutableRefObject) { + const findLineElement = useCallback((view: EditorView, position: number): HTMLElement | null => { + const domAtPos = view.domAtPos(position) + let node: Node | null = domAtPos.node + + if (node.nodeType === Node.TEXT_NODE) { + node = node.parentElement + } + + while (node) { + if (node instanceof HTMLElement && node.classList.contains('cm-line')) { + return node + } + node = node.parentElement + } + + return null + }, []) + + const highlightLine = useCallback((view: EditorView, element: HTMLElement) => { + const previousHighlight = view.dom.querySelector('.animation-locate-highlight') as HTMLElement | null + if (previousHighlight) { + previousHighlight.classList.remove('animation-locate-highlight') + } + + element.classList.add('animation-locate-highlight') + + const handleAnimationEnd = () => { + element.classList.remove('animation-locate-highlight') + element.removeEventListener('animationend', handleAnimationEnd) + } + + element.addEventListener('animationend', handleAnimationEnd) + }, []) + + return useCallback( + (lineNumber: number, options?: UseScrollToLineOptions) => { + const view = editorViewRef.current + if (!view) return + + const targetLine = view.state.doc.line(Math.min(lineNumber, view.state.doc.lines)) + + const lineElement = findLineElement(view, targetLine.from) + if (lineElement) { + lineElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' }) + + if (options?.highlight) { + requestAnimationFrame(() => highlightLine(view, lineElement)) + } + return + } + + view.dispatch({ + effects: EditorView.scrollIntoView(targetLine.from, { + y: 'start' + }) + }) + + if (!options?.highlight) { + return + } + + setTimeout(() => { + const fallbackElement = findLineElement(view, targetLine.from) + if (fallbackElement) { + highlightLine(view, fallbackElement) + } + }, 200) + }, + [editorViewRef, findLineElement, highlightLine] + ) +} diff --git a/src/renderer/src/components/CodeEditor/index.tsx b/src/renderer/src/components/CodeEditor/index.tsx index 64c387ffd0..31c4ce798c 100644 --- a/src/renderer/src/components/CodeEditor/index.tsx +++ b/src/renderer/src/components/CodeEditor/index.tsx @@ -5,13 +5,14 @@ import diff from 'fast-diff' import { useCallback, useEffect, useImperativeHandle, useMemo, useRef } from 'react' import { memo } from 'react' -import { useBlurHandler, useHeightListener, useLanguageExtensions, useSaveKeymap } from './hooks' +import { useBlurHandler, useHeightListener, useLanguageExtensions, useSaveKeymap, useScrollToLine } from './hooks' // 标记非用户编辑的变更 const External = Annotation.define() export interface CodeEditorHandles { save?: () => void + scrollToLine?: (lineNumber: number, options?: { highlight?: boolean }) => void } export interface CodeEditorProps { @@ -181,8 +182,11 @@ const CodeEditor = ({ ].flat() }, [extensions, langExtensions, wrapped, saveKeymapExtension, blurExtension, heightListenerExtension]) + const scrollToLine = useScrollToLine(editorViewRef) + useImperativeHandle(ref, () => ({ - save: handleSave + save: handleSave, + scrollToLine })) return ( diff --git a/src/renderer/src/components/HighlightText.tsx b/src/renderer/src/components/HighlightText.tsx new file mode 100644 index 0000000000..debf02c924 --- /dev/null +++ b/src/renderer/src/components/HighlightText.tsx @@ -0,0 +1,48 @@ +import { FC, memo, useMemo } from 'react' + +interface HighlightTextProps { + text: string + keyword: string + caseSensitive?: boolean + className?: string +} + +/** + * Text highlighting component that marks keyword matches + */ +const HighlightText: FC = ({ text, keyword, caseSensitive = false, className }) => { + const highlightedText = useMemo(() => { + if (!keyword || !text) { + return {text} + } + + // Escape regex special characters + const escapedKeyword = keyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + const flags = caseSensitive ? 'g' : 'gi' + const regex = new RegExp(`(${escapedKeyword})`, flags) + + // Split text by keyword matches + const parts = text.split(regex) + + return ( + <> + {parts.map((part, index) => { + // Check if part matches keyword + const isMatch = regex.test(part) + regex.lastIndex = 0 // Reset regex state + + if (isMatch) { + return {part} + } + return {part} + })} + + ) + }, [text, keyword, caseSensitive]) + + const combinedClassName = className ? `ant-typography ${className}` : 'ant-typography' + + return {highlightedText} +} + +export default memo(HighlightText) diff --git a/src/renderer/src/components/RichEditor/constants.ts b/src/renderer/src/components/RichEditor/constants.ts new file mode 100644 index 0000000000..08b0ba7cf2 --- /dev/null +++ b/src/renderer/src/components/RichEditor/constants.ts @@ -0,0 +1,2 @@ +// Attribute used to store the original source line number in markdown editors +export const MARKDOWN_SOURCE_LINE_ATTR = 'data-source-line' diff --git a/src/renderer/src/components/RichEditor/index.tsx b/src/renderer/src/components/RichEditor/index.tsx index 83023dab9a..793ccda1ae 100644 --- a/src/renderer/src/components/RichEditor/index.tsx +++ b/src/renderer/src/components/RichEditor/index.tsx @@ -1,5 +1,6 @@ import { loggerService } from '@logger' import { ContentSearch, type ContentSearchRef } from '@renderer/components/ContentSearch' +import { MARKDOWN_SOURCE_LINE_ATTR } from '@renderer/components/RichEditor/constants' import DragHandle from '@tiptap/extension-drag-handle-react' import { EditorContent } from '@tiptap/react' import { Tooltip } from 'antd' @@ -29,6 +30,156 @@ import type { FormattingCommand, RichEditorProps, RichEditorRef } from './types' import { useRichEditor } from './useRichEditor' const logger = loggerService.withContext('RichEditor') +/** + * Find element by line number with fallback strategies: + * 1. Exact line + content match + * 2. Exact line match + * 3. Closest line <= target + */ +function findElementByLine(editorDom: HTMLElement, lineNumber: number, lineContent?: string): HTMLElement | null { + const allElements = Array.from(editorDom.querySelectorAll(`[${MARKDOWN_SOURCE_LINE_ATTR}]`)) as HTMLElement[] + if (allElements.length === 0) { + logger.warn('No elements with data-source-line attribute found') + return null + } + const exactMatches = editorDom.querySelectorAll( + `[${MARKDOWN_SOURCE_LINE_ATTR}="${lineNumber}"]` + ) as NodeListOf + + // Strategy 1: Exact line + content match + if (exactMatches.length > 1 && lineContent) { + for (const match of Array.from(exactMatches)) { + if (match.textContent?.includes(lineContent)) { + return match + } + } + } + + // Strategy 2: Exact line match + if (exactMatches.length > 0) { + return exactMatches[0] + } + + // Strategy 3: Closest line <= target + let closestElement: HTMLElement | null = null + let closestLine = 0 + + for (const el of allElements) { + const sourceLine = parseInt(el.getAttribute(MARKDOWN_SOURCE_LINE_ATTR) || '0', 10) + if (sourceLine <= lineNumber && sourceLine > closestLine) { + closestLine = sourceLine + closestElement = el + } + } + + return closestElement +} + +/** + * Create fixed-position highlight overlay at element location + * with boundary detection to prevent overflow and toolbar overlap + */ +function createHighlightOverlay(element: HTMLElement, container: HTMLElement): void { + try { + // Remove previous overlay + const previousOverlay = document.body.querySelector('.highlight-overlay') + if (previousOverlay) { + previousOverlay.remove() + } + + const editorWrapper = container.closest('.rich-editor-wrapper') + + // Create overlay at element position + const rect = element.getBoundingClientRect() + const overlay = document.createElement('div') + overlay.className = 'highlight-overlay animation-locate-highlight' + overlay.style.position = 'fixed' + overlay.style.left = `${rect.left}px` + overlay.style.top = `${rect.top}px` + overlay.style.width = `${rect.width}px` + overlay.style.height = `${rect.height}px` + overlay.style.pointerEvents = 'none' + overlay.style.zIndex = '9999' + overlay.style.borderRadius = '4px' + + document.body.appendChild(overlay) + + // Update overlay position and visibility on scroll + const updatePosition = () => { + const newRect = element.getBoundingClientRect() + const newContainerRect = container.getBoundingClientRect() + + // Update position + overlay.style.left = `${newRect.left}px` + overlay.style.top = `${newRect.top}px` + overlay.style.width = `${newRect.width}px` + overlay.style.height = `${newRect.height}px` + + // Get current toolbar bottom (it might change) + const currentToolbar = editorWrapper?.querySelector('[class*="ToolbarWrapper"]') + const currentToolbarRect = currentToolbar?.getBoundingClientRect() + const currentToolbarBottom = currentToolbarRect ? currentToolbarRect.bottom : newContainerRect.top + + // Check if overlay is within visible bounds + const overlayTop = newRect.top + const overlayBottom = newRect.bottom + const visibleTop = currentToolbarBottom // Don't overlap toolbar + const visibleBottom = newContainerRect.bottom + + // Hide overlay if any part is outside the visible container area + if (overlayTop < visibleTop || overlayBottom > visibleBottom) { + overlay.style.opacity = '0' + overlay.style.visibility = 'hidden' + } else { + overlay.style.opacity = '1' + overlay.style.visibility = 'visible' + } + } + + container.addEventListener('scroll', updatePosition) + + // Auto-remove after animation + const handleAnimationEnd = () => { + overlay.remove() + container.removeEventListener('scroll', updatePosition) + overlay.removeEventListener('animationend', handleAnimationEnd) + } + overlay.addEventListener('animationend', handleAnimationEnd) + } catch (error) { + logger.error('Failed to create highlight overlay:', error as Error) + } +} + +/** + * Scroll to element and show highlight after scroll completes + */ +function scrollAndHighlight(element: HTMLElement, container: HTMLElement): void { + element.scrollIntoView({ behavior: 'smooth', block: 'start', inline: 'nearest' }) + + let scrollTimeout: NodeJS.Timeout + const handleScroll = () => { + clearTimeout(scrollTimeout) + scrollTimeout = setTimeout(() => { + container.removeEventListener('scroll', handleScroll) + requestAnimationFrame(() => createHighlightOverlay(element, container)) + }, 150) + } + + container.addEventListener('scroll', handleScroll) + + // Fallback: if element already in view (no scroll happens) + setTimeout(() => { + const initialScrollTop = container.scrollTop + setTimeout(() => { + if (Math.abs(container.scrollTop - initialScrollTop) < 1) { + container.removeEventListener('scroll', handleScroll) + clearTimeout(scrollTimeout) + requestAnimationFrame(() => createHighlightOverlay(element, container)) + } + }, 200) + }, 50) +} + const RichEditor = ({ ref, initialContent = '', @@ -372,6 +523,22 @@ const RichEditor = ({ scrollContainerRef.current.scrollTop = value } }, + scrollToLine: (lineNumber: number, options?: { highlight?: boolean; lineContent?: string }) => { + if (!editor || !scrollContainerRef.current) return + + try { + const element = findElementByLine(editor.view.dom, lineNumber, options?.lineContent) + if (!element) return + + if (options?.highlight) { + scrollAndHighlight(element, scrollContainerRef.current) + } else { + element.scrollIntoView({ behavior: 'smooth', block: 'start', inline: 'nearest' }) + } + } catch (error) { + logger.error('Failed in scrollToLine:', error as Error) + } + }, // Dynamic command management registerCommand, registerToolbarCommand, diff --git a/src/renderer/src/components/RichEditor/types.ts b/src/renderer/src/components/RichEditor/types.ts index 48ae5bb112..15727e1a8d 100644 --- a/src/renderer/src/components/RichEditor/types.ts +++ b/src/renderer/src/components/RichEditor/types.ts @@ -111,6 +111,8 @@ export interface RichEditorRef { getScrollTop: () => number /** Set scrollTop of the editor scroll container */ setScrollTop: (value: number) => void + /** Scroll to specific line number in markdown */ + scrollToLine: (lineNumber: number, options?: { highlight?: boolean; lineContent?: string }) => void // Dynamic command management /** Register a new command/toolbar item */ registerCommand: (cmd: Command) => void diff --git a/src/renderer/src/components/RichEditor/useRichEditor.ts b/src/renderer/src/components/RichEditor/useRichEditor.ts index 1ece36fb00..576162fac7 100644 --- a/src/renderer/src/components/RichEditor/useRichEditor.ts +++ b/src/renderer/src/components/RichEditor/useRichEditor.ts @@ -2,6 +2,7 @@ import 'katex/dist/katex.min.css' import { TableKit } from '@cherrystudio/extension-table-plus' import { loggerService } from '@logger' +import { MARKDOWN_SOURCE_LINE_ATTR } from '@renderer/components/RichEditor/constants' import type { FormattingState } from '@renderer/components/RichEditor/types' import { useCodeStyle } from '@renderer/context/CodeStyleProvider' import { @@ -11,6 +12,7 @@ import { markdownToPreviewText } from '@renderer/utils/markdownConverter' import type { Editor } from '@tiptap/core' +import { Extension } from '@tiptap/core' import { TaskItem, TaskList } from '@tiptap/extension-list' import { migrateMathStrings } from '@tiptap/extension-mathematics' import Mention from '@tiptap/extension-mention' @@ -36,6 +38,31 @@ import { blobToArrayBuffer, compressImage, shouldCompressImage } from './helpers const logger = loggerService.withContext('useRichEditor') +// Create extension to preserve data-source-line attribute +const SourceLineAttribute = Extension.create({ + name: 'sourceLineAttribute', + addGlobalAttributes() { + return [ + { + types: ['paragraph', 'heading', 'blockquote', 'bulletList', 'orderedList', 'listItem', 'horizontalRule'], + attributes: { + dataSourceLine: { + default: null, + parseHTML: (element) => { + const value = element.getAttribute(MARKDOWN_SOURCE_LINE_ATTR) + return value + }, + renderHTML: (attributes) => { + if (!attributes.dataSourceLine) return {} + return { [MARKDOWN_SOURCE_LINE_ATTR]: attributes.dataSourceLine } + } + } + } + } + ] + } +}) + export interface UseRichEditorOptions { /** Initial markdown content */ initialContent?: string @@ -196,6 +223,7 @@ export const useRichEditor = (options: UseRichEditorOptions = {}): UseRichEditor // TipTap editor extensions const extensions = useMemo( () => [ + SourceLineAttribute, StarterKit.configure({ heading: { levels: [1, 2, 3, 4, 5, 6] diff --git a/src/renderer/src/i18n/locales/en-us.json b/src/renderer/src/i18n/locales/en-us.json index ee3d0ff110..6b0dd3c0c7 100644 --- a/src/renderer/src/i18n/locales/en-us.json +++ b/src/renderer/src/i18n/locales/en-us.json @@ -1957,6 +1957,14 @@ "rename": "Rename", "rename_changed": "Due to security policies, the filename has been changed from {{original}} to {{final}}", "save": "Save to Notes", + "search": { + "both": "Name+Content", + "content": "Content", + "found_results": "Found {{count}} results (Name: {{nameCount}}, Content: {{contentCount}})", + "more_matches": "more matches", + "searching": "Searching...", + "show_less": "Show less" + }, "settings": { "data": { "apply": "Apply", diff --git a/src/renderer/src/i18n/locales/zh-cn.json b/src/renderer/src/i18n/locales/zh-cn.json index e5573221e7..15675d265a 100644 --- a/src/renderer/src/i18n/locales/zh-cn.json +++ b/src/renderer/src/i18n/locales/zh-cn.json @@ -1957,6 +1957,14 @@ "rename": "重命名", "rename_changed": "由于安全策略,文件名已从 {{original}} 更改为 {{final}}", "save": "保存到笔记", + "search": { + "both": "名称+内容", + "content": "内容", + "found_results": "找到 {{count}} 个结果 (名称: {{nameCount}}, 内容: {{contentCount}})", + "more_matches": "个匹配", + "searching": "搜索中...", + "show_less": "收起" + }, "settings": { "data": { "apply": "应用", diff --git a/src/renderer/src/i18n/locales/zh-tw.json b/src/renderer/src/i18n/locales/zh-tw.json index 8634254e55..020a0e5a19 100644 --- a/src/renderer/src/i18n/locales/zh-tw.json +++ b/src/renderer/src/i18n/locales/zh-tw.json @@ -1957,6 +1957,14 @@ "rename": "重命名", "rename_changed": "由於安全策略,文件名已從 {{original}} 更改為 {{final}}", "save": "儲存到筆記", + "search": { + "both": "名稱+內容", + "content": "內容", + "found_results": "找到 {{count}} 個結果 (名稱: {{nameCount}}, 內容: {{contentCount}})", + "more_matches": "個匹配", + "searching": "搜索中...", + "show_less": "收起" + }, "settings": { "data": { "apply": "應用", diff --git a/src/renderer/src/pages/notes/NotesEditor.tsx b/src/renderer/src/pages/notes/NotesEditor.tsx index 18c2cfe9d5..66bbf22258 100644 --- a/src/renderer/src/pages/notes/NotesEditor.tsx +++ b/src/renderer/src/pages/notes/NotesEditor.tsx @@ -1,5 +1,5 @@ import ActionIconButton from '@renderer/components/Buttons/ActionIconButton' -import CodeEditor from '@renderer/components/CodeEditor' +import CodeEditor, { CodeEditorHandles } from '@renderer/components/CodeEditor' import { HSpaceBetweenStack } from '@renderer/components/Layout' import RichEditor from '@renderer/components/RichEditor' import { RichEditorRef } from '@renderer/components/RichEditor/types' @@ -20,11 +20,12 @@ interface NotesEditorProps { currentContent: string tokenCount: number editorRef: RefObject + codeEditorRef: RefObject onMarkdownChange: (content: string) => void } const NotesEditor: FC = memo( - ({ activeNodeId, currentContent, tokenCount, onMarkdownChange, editorRef }) => { + ({ activeNodeId, currentContent, tokenCount, onMarkdownChange, editorRef, codeEditorRef }) => { const { t } = useTranslation() const dispatch = useAppDispatch() const { settings } = useNotesSettings() @@ -59,6 +60,7 @@ const NotesEditor: FC = memo( {tmpViewMode === 'source' ? ( { const editorRef = useRef(null) + const codeEditorRef = useRef(null) const { t } = useTranslation() const { showWorkspace } = useShowWorkspace() const dispatch = useAppDispatch() @@ -76,6 +79,7 @@ const NotesPage: FC = () => { const lastFilePathRef = useRef(undefined) const isRenamingRef = useRef(false) const isCreatingNoteRef = useRef(false) + const pendingScrollRef = useRef<{ lineNumber: number; lineContent?: string } | null>(null) const activeFilePathRef = useRef(activeFilePath) const currentContentRef = useRef(currentContent) @@ -366,6 +370,32 @@ const NotesPage: FC = () => { } }, [currentContent, activeFilePath]) + // Execute pending scroll after file switch + useEffect(() => { + if (!pendingScrollRef.current || !currentContent) return + + const { lineNumber, lineContent } = pendingScrollRef.current + pendingScrollRef.current = null + + // Wait for DOM to update before scrolling + requestAnimationFrame(() => { + requestAnimationFrame(() => { + const codeEditor = codeEditorRef.current + const richEditor = editorRef.current + + try { + if (codeEditor?.scrollToLine) { + codeEditor.scrollToLine(lineNumber, { highlight: true }) + } else if (richEditor?.scrollToLine) { + richEditor.scrollToLine(lineNumber, { highlight: true, lineContent }) + } + } catch (error) { + logger.error('Failed to execute pending scroll:', error as Error) + } + }) + }) + }, [activeFilePath, currentContent]) + // 切换文件时的清理工作 useEffect(() => { return () => { @@ -755,6 +785,53 @@ const NotesPage: FC = () => { } }, [currentContent, settings.defaultEditMode]) + // Listen for external requests to locate a specific line in a note + useEffect(() => { + const handleLocateNoteLine = ({ + noteId, + lineNumber, + lineContent + }: { + noteId: string + lineNumber: number + lineContent?: string + }) => { + const targetNode = findNode(notesTree, noteId) + + if (!targetNode || targetNode.type !== 'file') { + logger.warn('Target note not found or not a file', { noteId }) + return + } + + const needsSwitchFile = targetNode.externalPath !== activeFilePath + + if (needsSwitchFile) { + // switch to target note first then scroll to line + pendingScrollRef.current = { lineNumber, lineContent } + dispatch(setActiveFilePath(targetNode.externalPath)) + invalidateFileContent(targetNode.externalPath) + } else { + const richEditor = editorRef.current + const codeEditor = codeEditorRef.current + + try { + if (codeEditor?.scrollToLine) { + codeEditor.scrollToLine(lineNumber, { highlight: true }) + } else if (richEditor?.scrollToLine) { + richEditor.scrollToLine(lineNumber, { highlight: true, lineContent }) + } + } catch (error) { + logger.error('Failed to scroll to line:', error as Error) + } + } + } + + const unsubscribe = EventEmitter.on(EVENT_NAMES.LOCATE_NOTE_LINE, handleLocateNoteLine) + return () => { + unsubscribe() + } + }, [activeNode?.id, activeFilePath, notesTree, dispatch, invalidateFileContent]) + return ( @@ -800,6 +877,7 @@ const NotesPage: FC = () => { tokenCount={tokenCount} onMarkdownChange={handleMarkdownChange} editorRef={editorRef} + codeEditorRef={codeEditorRef} />
diff --git a/src/renderer/src/pages/notes/NotesSidebar.tsx b/src/renderer/src/pages/notes/NotesSidebar.tsx index 47a27030cd..d09c228f68 100644 --- a/src/renderer/src/pages/notes/NotesSidebar.tsx +++ b/src/renderer/src/pages/notes/NotesSidebar.tsx @@ -1,4 +1,5 @@ import { loggerService } from '@logger' +import HighlightText from '@renderer/components/HighlightText' import { DeleteIcon } from '@renderer/components/Icons' import SaveToKnowledgePopup from '@renderer/components/Popups/SaveToKnowledgePopup' import Scrollbar from '@renderer/components/Scrollbar' @@ -7,6 +8,8 @@ import { useKnowledgeBases } from '@renderer/hooks/useKnowledge' import { useActiveNode } from '@renderer/hooks/useNotesQuery' import NotesSidebarHeader from '@renderer/pages/notes/NotesSidebarHeader' import { fetchNoteSummary } from '@renderer/services/ApiService' +import { EVENT_NAMES, EventEmitter } from '@renderer/services/EventService' +import { SearchMatch, SearchResult } from '@renderer/services/NotesSearchService' import { RootState, useAppSelector } from '@renderer/store' import { selectSortType } from '@renderer/store/note' import { NotesSortType, NotesTreeNode } from '@renderer/types/note' @@ -23,16 +26,20 @@ import { FileSearch, Folder, FolderOpen, + Loader2, Sparkles, Star, StarOff, - UploadIcon + UploadIcon, + X } from 'lucide-react' import { FC, memo, Ref, useCallback, useEffect, useMemo, useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import { useSelector } from 'react-redux' import styled from 'styled-components' +import { useFullTextSearch } from './hooks/useFullTextSearch' + interface NotesSidebarProps { onCreateFolder: (name: string, targetFolderId?: string) => void onCreateNote: (name: string, targetFolderId?: string) => void @@ -51,7 +58,7 @@ interface NotesSidebarProps { const logger = loggerService.withContext('NotesSidebar') interface TreeNodeProps { - node: NotesTreeNode + node: NotesTreeNode | SearchResult depth: number selectedFolderId?: string | null activeNodeId?: string @@ -71,6 +78,8 @@ interface TreeNodeProps { onDrop: (e: React.DragEvent, node: NotesTreeNode) => void onDragEnd: () => void renderChildren?: boolean // 控制是否渲染子节点 + searchKeyword?: string // 搜索关键词,用于高亮 + showMatches?: boolean // 是否显示匹配预览 openDropdownKey: string | null onDropdownOpenChange: (key: string | null) => void } @@ -97,10 +106,30 @@ const TreeNode = memo( onDrop, onDragEnd, renderChildren = true, + searchKeyword = '', + showMatches = false, openDropdownKey, onDropdownOpenChange }) => { const { t } = useTranslation() + const [showAllMatches, setShowAllMatches] = useState(false) + + // 检查是否是搜索结果 + const searchResult = 'matchType' in node ? (node as SearchResult) : null + const hasMatches = searchResult && searchResult.matches && searchResult.matches.length > 0 + + // 处理匹配项点击 + const handleMatchClick = useCallback( + (match: SearchMatch) => { + // 发送定位事件 + EventEmitter.emit(EVENT_NAMES.LOCATE_NOTE_LINE, { + noteId: node.id, + lineNumber: match.lineNumber, + lineContent: match.lineContent + }) + }, + [node] + ) const isActive = selectedFolderId ? node.type === 'folder' && node.id === selectedFolderId @@ -121,6 +150,37 @@ const TreeNode = memo( return '' } + const displayName = useMemo(() => { + if (!searchKeyword) { + return node.name + } + + const name = node.name ?? '' + if (!name) { + return name + } + + const keyword = searchKeyword + const nameLower = name.toLowerCase() + const keywordLower = keyword.toLowerCase() + const matchStart = nameLower.indexOf(keywordLower) + + if (matchStart === -1) { + return name + } + + const matchEnd = matchStart + keyword.length + const beforeMatch = Math.min(2, matchStart) + const contextStart = matchStart - beforeMatch + const contextLength = 50 + const contextEnd = Math.min(name.length, matchEnd + contextLength) + + const prefix = contextStart > 0 ? '...' : '' + const suffix = contextEnd < name.length ? '...' : '' + + return prefix + name.substring(contextStart, contextEnd) + suffix + }, [node.name, searchKeyword]) + return (
( size="small" /> ) : ( - {node.name} + + + {searchKeyword ? : node.name} + + {searchResult && searchResult.matchType && searchResult.matchType !== 'filename' && ( + + {searchResult.matchType === 'both' ? t('notes.search.both') : t('notes.search.content')} + + )} + )}
+ {showMatches && hasMatches && ( + + {(showAllMatches ? searchResult!.matches! : searchResult!.matches!.slice(0, 3)).map((match, idx) => ( + handleMatchClick(match)}> + {match.lineNumber} + + + + + ))} + {searchResult!.matches!.length > 3 && ( + { + e.stopPropagation() + setShowAllMatches(!showAllMatches) + }}> + {showAllMatches ? ( + <> + + {t('notes.search.show_less')} + + ) : ( + <> + +{searchResult!.matches!.length - 3}{' '} + {t('notes.search.more_matches')} + + )} + + )} + + )} + {renderChildren && node.type === 'folder' && node.expanded && hasChildren && (
{node.children!.map((child) => ( @@ -257,6 +359,31 @@ const NotesSidebar: FC = ({ const [openDropdownKey, setOpenDropdownKey] = useState(null) const dragNodeRef = useRef(null) const scrollbarRef = useRef(null) + const notesTreeRef = useRef(notesTree) + const trimmedSearchKeyword = useMemo(() => searchKeyword.trim(), [searchKeyword]) + const hasSearchKeyword = trimmedSearchKeyword.length > 0 + + // 全文搜索配置 + const searchOptions = useMemo( + () => ({ + debounceMs: 300, + maxResults: 100, + contextLength: 50, + caseSensitive: false, + maxFileSize: 10 * 1024 * 1024, // 10MB + enabled: isShowSearch + }), + [isShowSearch] + ) + + const { + search, + cancel, + reset, + isSearching, + results: searchResults, + stats: searchStats + } = useFullTextSearch(searchOptions) const inPlaceEdit = useInPlaceEdit({ onSave: (newName: string) => { @@ -514,6 +641,25 @@ const NotesSidebar: FC = ({ setIsShowSearch(!isShowSearch) }, [isShowSearch]) + // 同步 notesTree 到 ref + useEffect(() => { + notesTreeRef.current = notesTree + }, [notesTree]) + + // 触发全文搜索 + useEffect(() => { + if (!isShowSearch) { + reset() + return + } + + if (hasSearchKeyword) { + search(notesTreeRef.current, trimmedSearchKeyword) + } else { + reset() + } + }, [isShowSearch, hasSearchKeyword, trimmedSearchKeyword, search, reset]) + // Flatten tree nodes for virtualization and filtering const flattenedNodes = useMemo(() => { const flattenForVirtualization = ( @@ -537,11 +683,7 @@ const NotesSidebar: FC = ({ let result: NotesTreeNode[] = [] for (const node of nodes) { - if (isShowSearch && searchKeyword) { - if (node.type === 'file' && node.name.toLowerCase().includes(searchKeyword.toLowerCase())) { - result.push(node) - } - } else if (isShowStarred) { + if (isShowStarred) { if (node.type === 'file' && node.isStarred) { result.push(node) } @@ -553,7 +695,14 @@ const NotesSidebar: FC = ({ return result } - if (isShowStarred || isShowSearch) { + if (isShowSearch) { + if (hasSearchKeyword) { + return searchResults.map((result) => ({ node: result, depth: 0 })) + } + return [] // 搜索关键词为空 + } + + if (isShowStarred) { // For filtered views, return flat list without virtualization for simplicity const filteredNodes = flattenForFiltering(notesTree) return filteredNodes.map((node) => ({ node, depth: 0 })) @@ -561,7 +710,7 @@ const NotesSidebar: FC = ({ // For normal tree view, use hierarchical flattening for virtualization return flattenForVirtualization(notesTree) - }, [notesTree, isShowStarred, isShowSearch, searchKeyword]) + }, [notesTree, isShowStarred, isShowSearch, hasSearchKeyword, searchResults]) // Use virtualization only for normal tree view with many items const shouldUseVirtualization = !isShowStarred && !isShowSearch && flattenedNodes.length > 100 @@ -863,6 +1012,26 @@ const NotesSidebar: FC = ({ /> + {isShowSearch && isSearching && ( + + + {t('notes.search.searching')} + + + + + )} + {isShowSearch && !isSearching && hasSearchKeyword && searchStats.total > 0 && ( + + + {t('notes.search.found_results', { + count: searchStats.total, + nameCount: searchStats.fileNameMatches, + contentCount: searchStats.contentMatches + searchStats.bothMatches + })} + + + )} {shouldUseVirtualization ? ( = ({ onDragEnd={handleDragEnd} openDropdownKey={openDropdownKey} onDropdownOpenChange={setOpenDropdownKey} + searchKeyword={isShowSearch ? trimmedSearchKeyword : ''} + showMatches={isShowSearch} /> )) : notesTree.map((node) => ( @@ -1249,4 +1420,148 @@ const DropHintText = styled.div` font-style: italic; ` +// 搜索相关样式 +const SearchStatusBar = styled.div` + display: flex; + align-items: center; + gap: 8px; + padding: 8px 12px; + background-color: var(--color-background-soft); + border-bottom: 0.5px solid var(--color-border); + font-size: 12px; + color: var(--color-text-2); + + .animate-spin { + animation: spin 1s linear infinite; + } + + @keyframes spin { + from { + transform: rotate(0deg); + } + to { + transform: rotate(360deg); + } + } +` + +const CancelButton = styled.button` + margin-left: auto; + display: flex; + align-items: center; + justify-content: center; + width: 20px; + height: 20px; + padding: 0; + border: none; + background-color: transparent; + color: var(--color-text-3); + cursor: pointer; + border-radius: 3px; + transition: all 0.2s ease; + + &:hover { + background-color: var(--color-background-mute); + color: var(--color-text); + } + + &:active { + background-color: var(--color-active); + } +` + +const NodeNameContainer = styled.div` + display: flex; + align-items: center; + gap: 6px; + flex: 1; + min-width: 0; +` + +const MatchBadge = styled.span<{ matchType: string }>` + display: inline-flex; + align-items: center; + padding: 0 4px; + height: 16px; + font-size: 10px; + line-height: 1; + border-radius: 2px; + background-color: ${(props) => + props.matchType === 'both' ? 'var(--color-primary-soft)' : 'var(--color-background-mute)'}; + color: ${(props) => (props.matchType === 'both' ? 'var(--color-primary)' : 'var(--color-text-3)')}; + font-weight: 500; + flex-shrink: 0; +` + +const SearchMatchesContainer = styled.div<{ depth: number }>` + margin-left: ${(props) => props.depth * 16 + 40}px; + margin-top: 4px; + margin-bottom: 8px; + padding: 6px 8px; + background-color: var(--color-background-mute); + border-radius: 4px; + border-left: 2px solid var(--color-primary-soft); +` + +const MatchItem = styled.div` + display: flex; + gap: 8px; + margin-bottom: 4px; + font-size: 12px; + padding: 4px 6px; + margin-left: -6px; + margin-right: -6px; + border-radius: 3px; + cursor: pointer; + transition: all 0.15s ease; + + &:hover { + background-color: var(--color-background-soft); + transform: translateX(2px); + } + + &:active { + background-color: var(--color-active); + } + + &:last-child { + margin-bottom: 0; + } +` + +const MatchLineNumber = styled.span` + color: var(--color-text-3); + font-family: monospace; + flex-shrink: 0; + width: 30px; +` + +const MatchContext = styled.div` + color: var(--color-text-2); + flex: 1; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + font-family: monospace; +` + +const MoreMatches = styled.div<{ depth: number }>` + margin-top: 4px; + padding: 4px 6px; + margin-left: -6px; + margin-right: -6px; + font-size: 11px; + color: var(--color-text-3); + border-radius: 3px; + cursor: pointer; + display: flex; + align-items: center; + transition: all 0.15s ease; + + &:hover { + color: var(--color-text-2); + background-color: var(--color-background-soft); + } +` + export default memo(NotesSidebar) diff --git a/src/renderer/src/pages/notes/hooks/useFullTextSearch.ts b/src/renderer/src/pages/notes/hooks/useFullTextSearch.ts new file mode 100644 index 0000000000..6a2d12c2cf --- /dev/null +++ b/src/renderer/src/pages/notes/hooks/useFullTextSearch.ts @@ -0,0 +1,159 @@ +import { searchAllFiles, SearchOptions, SearchResult } from '@renderer/services/NotesSearchService' +import { NotesTreeNode } from '@renderer/types/note' +import { useCallback, useEffect, useRef, useState } from 'react' + +export interface UseFullTextSearchOptions extends SearchOptions { + debounceMs?: number + maxResults?: number + enabled?: boolean +} + +export interface UseFullTextSearchReturn { + search: (nodes: NotesTreeNode[], keyword: string) => void + cancel: () => void + reset: () => void + isSearching: boolean + results: SearchResult[] + stats: { + total: number + fileNameMatches: number + contentMatches: number + bothMatches: number + } + error: Error | null +} + +/** + * Full-text search hook for notes + */ +export function useFullTextSearch(options: UseFullTextSearchOptions = {}): UseFullTextSearchReturn { + const { debounceMs = 300, maxResults = 100, enabled = true, ...searchOptions } = options + + const [isSearching, setIsSearching] = useState(false) + const [results, setResults] = useState([]) + const [error, setError] = useState(null) + const [stats, setStats] = useState({ + total: 0, + fileNameMatches: 0, + contentMatches: 0, + bothMatches: 0 + }) + + const abortControllerRef = useRef(null) + const debounceTimerRef = useRef(null) + + // Store options in refs to avoid reference changes + const searchOptionsRef = useRef(searchOptions) + const maxResultsRef = useRef(maxResults) + const enabledRef = useRef(enabled) + + useEffect(() => { + searchOptionsRef.current = searchOptions + maxResultsRef.current = maxResults + enabledRef.current = enabled + }, [searchOptions, maxResults, enabled]) + + const cancel = useCallback(() => { + if (abortControllerRef.current) { + abortControllerRef.current.abort() + abortControllerRef.current = null + } + if (debounceTimerRef.current) { + clearTimeout(debounceTimerRef.current) + debounceTimerRef.current = null + } + setIsSearching(false) + }, []) + + const reset = useCallback(() => { + cancel() + setResults([]) + setStats({ total: 0, fileNameMatches: 0, contentMatches: 0, bothMatches: 0 }) + setError(null) + }, [cancel]) + + const performSearch = useCallback( + async (nodes: NotesTreeNode[], keyword: string) => { + if (!enabledRef.current) { + return + } + + cancel() + + if (!keyword) { + setResults([]) + setStats({ total: 0, fileNameMatches: 0, contentMatches: 0, bothMatches: 0 }) + return + } + + setIsSearching(true) + setError(null) + + const abortController = new AbortController() + abortControllerRef.current = abortController + + try { + const searchResults = await searchAllFiles( + nodes, + keyword.trim(), + searchOptionsRef.current, + abortController.signal + ) + + if (abortController.signal.aborted) { + return + } + + const limitedResults = searchResults.slice(0, maxResultsRef.current) + + const newStats = { + total: limitedResults.length, + fileNameMatches: limitedResults.filter((r) => r.matchType === 'filename').length, + contentMatches: limitedResults.filter((r) => r.matchType === 'content').length, + bothMatches: limitedResults.filter((r) => r.matchType === 'both').length + } + + setResults(limitedResults) + setStats(newStats) + } catch (err) { + if (err instanceof Error && err.name !== 'AbortError') { + setError(err) + } + } finally { + if (!abortController.signal.aborted) { + setIsSearching(false) + } + } + }, + [cancel] + ) + + const search = useCallback( + (nodes: NotesTreeNode[], keyword: string) => { + if (debounceTimerRef.current) { + clearTimeout(debounceTimerRef.current) + } + + debounceTimerRef.current = setTimeout(() => { + performSearch(nodes, keyword) + }, debounceMs) + }, + [performSearch, debounceMs] + ) + + useEffect(() => { + return () => { + cancel() + } + }, [cancel]) + + return { + search, + cancel, + reset, + isSearching, + results, + stats, + error + } +} diff --git a/src/renderer/src/services/EventService.ts b/src/renderer/src/services/EventService.ts index 498beed049..ce5895fc36 100644 --- a/src/renderer/src/services/EventService.ts +++ b/src/renderer/src/services/EventService.ts @@ -24,6 +24,7 @@ export const EVENT_NAMES = { COPY_TOPIC_IMAGE: 'COPY_TOPIC_IMAGE', EXPORT_TOPIC_IMAGE: 'EXPORT_TOPIC_IMAGE', LOCATE_MESSAGE: 'LOCATE_MESSAGE', + LOCATE_NOTE_LINE: 'LOCATE_NOTE_LINE', ADD_NEW_TOPIC: 'ADD_NEW_TOPIC', RESEND_MESSAGE: 'RESEND_MESSAGE', SHOW_MODEL_SELECTOR: 'SHOW_MODEL_SELECTOR', diff --git a/src/renderer/src/services/NotesSearchService.ts b/src/renderer/src/services/NotesSearchService.ts new file mode 100644 index 0000000000..f4331ff166 --- /dev/null +++ b/src/renderer/src/services/NotesSearchService.ts @@ -0,0 +1,262 @@ +import { loggerService } from '@logger' +import { NotesTreeNode } from '@renderer/types/note' + +const logger = loggerService.withContext('NotesSearchService') + +/** + * Search match result + */ +export interface SearchMatch { + lineNumber: number + lineContent: string + matchStart: number + matchEnd: number + context: string +} + +/** + * Search result with match information + */ +export interface SearchResult extends NotesTreeNode { + matchType: 'filename' | 'content' | 'both' + matches?: SearchMatch[] + score: number +} + +/** + * Search options + */ +export interface SearchOptions { + caseSensitive?: boolean + useRegex?: boolean + maxFileSize?: number + maxMatchesPerFile?: number + contextLength?: number +} + +/** + * Escape regex special characters + */ +export function escapeRegex(str: string): string { + return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') +} + +/** + * Calculate relevance score + * - Filename match has higher priority + * - More matches increase score + * - More recent updates increase score + */ +export function calculateRelevanceScore(node: NotesTreeNode, keyword: string, matches: SearchMatch[]): number { + let score = 0 + + // Exact filename match (highest weight) + if (node.name.toLowerCase() === keyword.toLowerCase()) { + score += 200 + } + // Filename contains match (high weight) + else if (node.name.toLowerCase().includes(keyword.toLowerCase())) { + score += 100 + } + + // Content match count + score += Math.min(matches.length * 2, 50) + + // Recent updates boost score + const daysSinceUpdate = (Date.now() - new Date(node.updatedAt).getTime()) / (1000 * 60 * 60 * 24) + score += Math.max(0, 10 - daysSinceUpdate) + + return score +} + +/** + * Search file content for keyword matches + */ +export async function searchFileContent( + node: NotesTreeNode, + keyword: string, + options: SearchOptions = {} +): Promise { + const { + caseSensitive = false, + useRegex = false, + maxFileSize = 10 * 1024 * 1024, // 10MB + maxMatchesPerFile = 50, + contextLength = 50 + } = options + + try { + if (node.type !== 'file') { + return null + } + + const content = await window.api.file.readExternal(node.externalPath) + + if (!content) { + return null + } + + if (content.length > maxFileSize) { + logger.warn(`File too large to search: ${node.externalPath} (${content.length} bytes)`) + return null + } + + const flags = caseSensitive ? 'g' : 'gi' + const pattern = useRegex ? new RegExp(keyword, flags) : new RegExp(escapeRegex(keyword), flags) + + const lines = content.split('\n') + const matches: SearchMatch[] = [] + + for (let i = 0; i < lines.length; i++) { + const line = lines[i] + pattern.lastIndex = 0 + + let match: RegExpExecArray | null + while ((match = pattern.exec(line)) !== null) { + const matchStart = match.index + const matchEnd = matchStart + match[0].length + + // Keep context short: only 2 chars before match, more after + const beforeMatch = Math.min(2, matchStart) + const contextStart = matchStart - beforeMatch + const contextEnd = Math.min(line.length, matchEnd + contextLength) + + // Add ellipsis if context doesn't start at line beginning + const prefix = contextStart > 0 ? '...' : '' + const contextText = prefix + line.substring(contextStart, contextEnd) + + matches.push({ + lineNumber: i + 1, + lineContent: line, + matchStart: beforeMatch + prefix.length, + matchEnd: matchEnd - matchStart + beforeMatch + prefix.length, + context: contextText + }) + + if (matches.length >= maxMatchesPerFile) { + break + } + } + + if (matches.length >= maxMatchesPerFile) { + break + } + } + + if (matches.length === 0) { + return null + } + + const score = calculateRelevanceScore(node, keyword, matches) + + return { + ...node, + matchType: 'content', + matches, + score + } + } catch (error) { + logger.error(`Failed to search file content for ${node.externalPath}:`, error as Error) + return null + } +} + +/** + * Check if filename matches keyword + */ +export function matchFileName(node: NotesTreeNode, keyword: string, caseSensitive = false): boolean { + const name = caseSensitive ? node.name : node.name.toLowerCase() + const key = caseSensitive ? keyword : keyword.toLowerCase() + return name.includes(key) +} + +/** + * Flatten tree to extract file nodes + */ +export function flattenTreeToFiles(nodes: NotesTreeNode[]): NotesTreeNode[] { + const result: NotesTreeNode[] = [] + + function traverse(nodes: NotesTreeNode[]) { + for (const node of nodes) { + if (node.type === 'file') { + result.push(node) + } + if (node.children && node.children.length > 0) { + traverse(node.children) + } + } + } + + traverse(nodes) + return result +} + +/** + * Search all files concurrently + */ +export async function searchAllFiles( + nodes: NotesTreeNode[], + keyword: string, + options: SearchOptions = {}, + signal?: AbortSignal +): Promise { + const startTime = performance.now() + const CONCURRENCY = 5 + const results: SearchResult[] = [] + + const fileNodes = flattenTreeToFiles(nodes) + + logger.debug( + `Starting full-text search: keyword="${keyword}", totalFiles=${fileNodes.length}, options=${JSON.stringify(options)}` + ) + + const queue = [...fileNodes] + + const worker = async () => { + while (queue.length > 0) { + if (signal?.aborted) { + break + } + + const node = queue.shift() + if (!node) break + + const nameMatch = matchFileName(node, keyword, options.caseSensitive) + const contentResult = await searchFileContent(node, keyword, options) + + if (nameMatch && contentResult) { + results.push({ + ...contentResult, + matchType: 'both', + score: contentResult.score + 100 + }) + } else if (nameMatch) { + results.push({ + ...node, + matchType: 'filename', + matches: [], + score: 100 + }) + } else if (contentResult) { + results.push(contentResult) + } + } + } + + await Promise.all(Array.from({ length: Math.min(CONCURRENCY, fileNodes.length) }, () => worker())) + + const sortedResults = results.sort((a, b) => b.score - a.score) + + const endTime = performance.now() + const duration = (endTime - startTime).toFixed(2) + + logger.debug( + `Full-text search completed: keyword="${keyword}", duration=${duration}ms, ` + + `totalFiles=${fileNodes.length}, resultsFound=${sortedResults.length}, ` + + `filenameMatches=${sortedResults.filter((r) => r.matchType === 'filename').length}, ` + + `contentMatches=${sortedResults.filter((r) => r.matchType === 'content').length}, ` + + `bothMatches=${sortedResults.filter((r) => r.matchType === 'both').length}` + ) + + return sortedResults +} diff --git a/src/renderer/src/utils/__tests__/markdownConverter.test.ts b/src/renderer/src/utils/__tests__/markdownConverter.test.ts index 44396d76ca..25a330b114 100644 --- a/src/renderer/src/utils/__tests__/markdownConverter.test.ts +++ b/src/renderer/src/utils/__tests__/markdownConverter.test.ts @@ -1,7 +1,18 @@ +import { MARKDOWN_SOURCE_LINE_ATTR } from '@renderer/components/RichEditor/constants' import { describe, expect, it } from 'vitest' import { htmlToMarkdown, markdownToHtml } from '../markdownConverter' +/** + * Strip markdown line number attributes for testing HTML structure + */ + +const LINE_NUMBER_REGEX = new RegExp(`\\s*${MARKDOWN_SOURCE_LINE_ATTR.replace(/-/g, '\\-')}="\\d+"`, 'g') + +function stripLineNumbers(html: string): string { + return html.replace(LINE_NUMBER_REGEX, '') +} + describe('markdownConverter', () => { describe('htmlToMarkdown', () => { it('should convert HTML to Markdown', () => { @@ -104,13 +115,13 @@ describe('markdownConverter', () => { describe('markdownToHtml', () => { it('should convert
to
', () => { const markdown = 'Text with
\nindentation
\nand without indentation' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toBe('

Text with
\nindentation
\nand without indentation

\n') }) it('should handle indentation in blockquotes', () => { const markdown = '> Quote line 1\n> Quote line 2 with indentation' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) // This should preserve indentation within the blockquote expect(result).toContain('Quote line 1') expect(result).toContain('Quote line 2 with indentation') @@ -118,7 +129,7 @@ describe('markdownConverter', () => { it('should preserve indentation in nested lists', () => { const markdown = '- Item 1\n - Nested item\n - Double nested\n with continued line' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) // Should create proper nested list structure expect(result).toContain('
    ') expect(result).toContain('
  • ') @@ -126,13 +137,13 @@ describe('markdownConverter', () => { it('should handle poetry or formatted text with indentation', () => { const markdown = 'Roses are red\n Violets are blue\n Sugar is sweet\n And so are you' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toBe('

    Roses are red\nViolets are blue\nSugar is sweet\nAnd so are you

    \n') }) it('should preserve indentation after line breaks with multiple paragraphs', () => { const markdown = 'First paragraph\n\n with indentation\n\n Second paragraph\n\nwith different indentation' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toBe( '

    First paragraph

    \n
    with indentation\n\nSecond paragraph\n

    with different indentation

    \n' ) @@ -140,14 +151,14 @@ describe('markdownConverter', () => { it('should handle zero-width indentation (just line break)', () => { const markdown = 'Hello\n\nWorld' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toBe('

    Hello

    \n

    World

    \n') }) it('should preserve indentation in mixed content', () => { const markdown = 'Normal text\n Indented continuation\n\n- List item\n List continuation\n\n> Quote\n> Indented quote' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toBe( '

    Normal text\nIndented continuation

    \n
      \n
    • List item\nList continuation
    • \n
    \n
    \n

    Quote\nIndented quote

    \n
    \n' ) @@ -155,19 +166,19 @@ describe('markdownConverter', () => { it('should convert Markdown to HTML', () => { const markdown = '# Hello World' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toContain('

    Hello World

    ') }) it('should convert math block syntax to HTML', () => { const markdown = '$$a+b+c$$' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toContain('
    ') }) it('should convert math inline syntax to HTML', () => { const markdown = '$a+b+c$' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toContain('') }) @@ -181,7 +192,7 @@ describe('markdownConverter', () => { \\nabla \\cdot \\vec{\\mathbf{B}} & = 0 \\end{array}$$` - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toContain( '
    ' ) @@ -189,7 +200,7 @@ describe('markdownConverter', () => { it('should convert task list syntax to proper HTML', () => { const markdown = '- [ ] abcd\n\n- [x] efgh\n\n' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toContain('data-type="taskList"') expect(result).toContain('data-type="taskItem"') expect(result).toContain('data-checked="false"') @@ -202,7 +213,7 @@ describe('markdownConverter', () => { it('should convert mixed task list with checked and unchecked items', () => { const markdown = '- [ ] First task\n\n- [x] Second task\n\n- [ ] Third task' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toContain('data-type="taskList"') expect(result).toContain('First task') expect(result).toContain('Second task') @@ -213,14 +224,14 @@ describe('markdownConverter', () => { it('should NOT convert standalone task syntax to task list', () => { const markdown = '[x] abcd' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toContain('

    [x] abcd

    ') expect(result).not.toContain('data-type="taskList"') }) it('should handle regular list items alongside task lists', () => { const markdown = '- Regular item\n\n- [ ] Task item\n\n- Another regular item' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toContain('data-type="taskList"') expect(result).toContain('Regular item') expect(result).toContain('Task item') @@ -241,25 +252,25 @@ describe('markdownConverter', () => { const markdown = `# 🌠 Screenshot ![](https://example.com/image.png)` - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toBe('

    🌠 Screenshot

    \n

    \n') }) it('should handle heading and paragraph', () => { const markdown = '# Hello\n\nHello' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toBe('

    Hello

    \n

    Hello

    \n') }) it('should convert code block to HTML', () => { const markdown = '```\nconsole.log("Hello, world!");\n```' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toBe('
    console.log("Hello, world!");\n
    ') }) it('should convert code block with language to HTML', () => { const markdown = '```javascript\nconsole.log("Hello, world!");\n```' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toBe( '
    console.log("Hello, world!");\n
    ' ) @@ -267,7 +278,7 @@ describe('markdownConverter', () => { it('should convert table to HTML', () => { const markdown = '| f | | |\n| --- | --- | --- |\n| | f | |\n| | | f |' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toBe( '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
    f
    f
    f
    \n' ) @@ -275,7 +286,7 @@ describe('markdownConverter', () => { it('should escape XML-like tags in code blocks', () => { const markdown = '```jsx\nconst component = <>
    content
    \n```' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toBe( '
    const component = <><div>content</div></>\n
    ' ) @@ -283,13 +294,13 @@ describe('markdownConverter', () => { it('should escape XML-like tags in inline code', () => { const markdown = 'Use `<>` for fragments' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toBe('

    Use <> for fragments

    \n') }) it('shoud convert XML-like tags in paragraph', () => { const markdown = '' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toBe('

    \n') }) }) @@ -297,7 +308,7 @@ describe('markdownConverter', () => { describe('Task List with Labels', () => { it('should wrap task items with labels when label option is true', () => { const markdown = '- [ ] abcd\n\n- [x] efgh' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toBe( '
      \n
    • \n

      \n
    • \n
    • \n

      \n
    • \n
    \n' ) @@ -317,7 +328,7 @@ describe('markdownConverter', () => { const originalHtml = '
    ' const markdown = htmlToMarkdown(originalHtml) - const html = markdownToHtml(markdown) + const html = stripLineNumbers(markdownToHtml(markdown)) expect(html).toBe( '
      \n
    • \n
    \n' @@ -328,7 +339,7 @@ describe('markdownConverter', () => { const originalHtml = '
      \n
    • \n

      123

      \n
    • \n
    • \n

      \n
    • \n
    \n' const markdown = htmlToMarkdown(originalHtml) - const html = markdownToHtml(markdown) + const html = stripLineNumbers(markdownToHtml(markdown)) expect(html).toBe(originalHtml) }) @@ -383,7 +394,7 @@ describe('markdownConverter', () => { describe('markdown image', () => { it('should convert markdown image to HTML img tag', () => { const markdown = '![foo](train.jpg)' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toBe('

    foo

    \n') }) it('should convert markdown image with file:// protocol to HTML img tag', () => { @@ -420,7 +431,7 @@ describe('markdownConverter', () => { it('should handle hardbreak with backslash followed by indented text', () => { const markdown = 'Text with \\\n indentation \\\nand without indentation' - const result = markdownToHtml(markdown) + const result = stripLineNumbers(markdownToHtml(markdown)) expect(result).toBe('

    Text with
    \nindentation
    \nand without indentation

    \n') }) @@ -454,7 +465,7 @@ describe('markdownConverter', () => { it('should preserve custom XML tags mixed with regular markdown', () => { const markdown = '# Heading\n\nWidget content\n\n**Bold text**' - const html = markdownToHtml(markdown) + const html = stripLineNumbers(markdownToHtml(markdown)) const backToMarkdown = htmlToMarkdown(html) expect(html).toContain('

    Heading

    ') @@ -470,7 +481,7 @@ describe('markdownConverter', () => { it('should not add unwanted line breaks during simple text typing', () => { const html = '

    Hello world

    ' const markdown = htmlToMarkdown(html) - const backToHtml = markdownToHtml(markdown) + const backToHtml = stripLineNumbers(markdownToHtml(markdown)) expect(markdown).toBe('Hello world') expect(backToHtml).toBe('

    Hello world

    \n') @@ -479,7 +490,7 @@ describe('markdownConverter', () => { it('should preserve simple paragraph structure during round-trip conversion', () => { const originalHtml = '

    This is a simple paragraph being typed

    ' const markdown = htmlToMarkdown(originalHtml) - const backToHtml = markdownToHtml(markdown) + const backToHtml = stripLineNumbers(markdownToHtml(markdown)) expect(markdown).toBe('This is a simple paragraph being typed') expect(backToHtml).toBe('

    This is a simple paragraph being typed

    \n') }) @@ -520,4 +531,24 @@ cssclasses: expect(backToMarkdown).toBe(markdown) }) }) + + describe('should have markdown line number injected in HTML', () => { + it('should inject line numbers into paragraphs', () => { + const markdown = 'First paragraph\n\nSecond paragraph\n\nThird paragraph' + const result = markdownToHtml(markdown) + expect(result).toContain(`

    First paragraph

    `) + expect(result).toContain(`

    Second paragraph

    `) + expect(result).toContain(`

    Third paragraph

    `) + }) + + it('should inject line numbers into mixed content', () => { + const markdown = 'Text\n\n- List\n\n> Quote' + const result = markdownToHtml(markdown) + expect(result).toContain(`

    Text

    `) + expect(result).toContain(`
      `) + expect(result).toContain(`
    • List
    • `) + expect(result).toContain(`
      `) + expect(result).toContain(`

      Quote

      `) + }) + }) }) diff --git a/src/renderer/src/utils/markdownConverter.ts b/src/renderer/src/utils/markdownConverter.ts index 3b766cd32c..51d684612d 100644 --- a/src/renderer/src/utils/markdownConverter.ts +++ b/src/renderer/src/utils/markdownConverter.ts @@ -1,4 +1,5 @@ import { loggerService } from '@logger' +import { MARKDOWN_SOURCE_LINE_ATTR } from '@renderer/components/RichEditor/constants' import { TurndownPlugin } from '@truto/turndown-plugin-gfm' import he from 'he' import htmlTags, { type HtmlTags } from 'html-tags' @@ -85,12 +86,57 @@ const md = new MarkdownIt({ typographer: false // Enable smartypants and other sweet transforms }) +// Helper function to inject line number data attribute +function injectLineNumber(token: any, openTag: string): string { + if (token.map && token.map.length >= 2) { + const startLine = token.map[0] + 1 // Convert to 1-based line number + // Insert data attribute before the first closing > + // Handle both self-closing tags (e.g.,
      ) and opening tags (e.g.,

      ) + const result = openTag.replace(/(\s*\/?>)/, ` ${MARKDOWN_SOURCE_LINE_ATTR}="${startLine}"$1`) + logger.debug('injectLineNumber', { openTag, result, startLine, hasMap: !!token.map }) + return result + } + return openTag +} + +// Store the original renderer +const defaultRender = md.renderer.render.bind(md.renderer) + +// Override the main render method to inject line numbers +md.renderer.render = function (tokens, options, env) { + return defaultRender(tokens, options, env) +} + +// Override default rendering rules to add line numbers +const defaultBlockRules = [ + 'paragraph_open', + 'heading_open', + 'blockquote_open', + 'bullet_list_open', + 'ordered_list_open', + 'list_item_open', + 'table_open', + 'hr' +] + +defaultBlockRules.forEach((ruleName) => { + const original = md.renderer.rules[ruleName] + md.renderer.rules[ruleName] = function (tokens, idx, options, env, self) { + const token = tokens[idx] + let result = original ? original(tokens, idx, options, env, self) : self.renderToken(tokens, idx, options) + result = injectLineNumber(token, result) + return result + } +}) + // Override the code_block and code_inline renderers to properly escape HTML entities md.renderer.rules.code_block = function (tokens, idx) { const token = tokens[idx] const langName = token.info ? ` class="language-${token.info.trim()}"` : '' const escapedContent = he.encode(token.content, { useNamedReferences: false }) - return `

      ${escapedContent}
      ` + let html = `
      ${escapedContent}
      ` + html = injectLineNumber(token, html) + return html } md.renderer.rules.code_inline = function (tokens, idx) { @@ -103,7 +149,9 @@ md.renderer.rules.fence = function (tokens, idx) { const token = tokens[idx] const langName = token.info ? ` class="language-${token.info.trim()}"` : '' const escapedContent = he.encode(token.content, { useNamedReferences: false }) - return `
      ${escapedContent}
      ` + let html = `
      ${escapedContent}
      ` + html = injectLineNumber(token, html) + return html } // Custom task list plugin for markdown-it @@ -305,8 +353,11 @@ function yamlFrontMatterPlugin(md: MarkdownIt) { // Renderer: output YAML front matter as special HTML element md.renderer.rules.yaml_front_matter = (tokens: Array<{ content?: string }>, idx: number): string => { - const content = tokens[idx]?.content ?? '' - return `
      ${content}
      ` + const token = tokens[idx] + const content = token?.content ?? '' + let html = `
      ${content}
      ` + html = injectLineNumber(token, html) + return html } } @@ -408,9 +459,12 @@ function tipTapKatexPlugin(md: MarkdownIt) { // 2) Renderer: output TipTap-friendly container md.renderer.rules.math_block = (tokens: Array<{ content?: string }>, idx: number): string => { - const content = tokens[idx]?.content ?? '' + const token = tokens[idx] + const content = token?.content ?? '' const latexEscaped = he.encode(content, { useNamedReferences: true }) - return `
      ` + let html = `
      ` + html = injectLineNumber(token, html) + return html } // 3) Inline parser: recognize $...$ on a single line as inline math From 2243bb286207ace993f641687099f738847963e6 Mon Sep 17 00:00:00 2001 From: Kejiang Ma Date: Fri, 17 Oct 2025 13:40:53 +0800 Subject: [PATCH 11/27] =?UTF-8?q?feat:=20update=20and=20download=20ovms=20?= =?UTF-8?q?to=202025.3=20official=20release=20from=20offici=E2=80=A6=20(#1?= =?UTF-8?q?0603)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: update and download ovms to 2025.3 official release from official site. Signed-off-by: Kejiang Ma * fix UI text Signed-off-by: Kejiang Ma --------- Signed-off-by: Kejiang Ma --- resources/scripts/install-ovms.js | 236 ++++++++++++------ src/renderer/src/i18n/locales/en-us.json | 6 +- src/renderer/src/i18n/locales/zh-cn.json | 8 +- src/renderer/src/i18n/locales/zh-tw.json | 8 +- .../ProviderSettings/OVMSSettings.tsx | 6 +- 5 files changed, 179 insertions(+), 85 deletions(-) diff --git a/resources/scripts/install-ovms.js b/resources/scripts/install-ovms.js index 57710e43f6..e4a5cf0444 100644 --- a/resources/scripts/install-ovms.js +++ b/resources/scripts/install-ovms.js @@ -5,105 +5,171 @@ const { execSync } = require('child_process') const { downloadWithPowerShell } = require('./download') // Base URL for downloading OVMS binaries -const OVMS_PKG_NAME = 'ovms250911.zip' -const OVMS_RELEASE_BASE_URL = [`https://gitcode.com/gcw_ggDjjkY3/kjfile/releases/download/download/${OVMS_PKG_NAME}`] +const OVMS_RELEASE_BASE_URL = + 'https://storage.openvinotoolkit.org/repositories/openvino_model_server/packages/2025.3.0/ovms_windows_python_on.zip' +const OVMS_EX_URL = 'https://gitcode.com/gcw_ggDjjkY3/kjfile/releases/download/download/ovms_25.3_ex.zip' /** - * Downloads and extracts the OVMS binary for the specified platform + * error code: + * 101: Unsupported CPU (not Intel Ultra) + * 102: Unsupported platform (not Windows) + * 103: Download failed + * 104: Installation failed + * 105: Failed to create ovdnd.exe + * 106: Failed to create run.bat + * 110: Cleanup of old installation failed */ -async function downloadOvmsBinary() { - // Create output directory structure - OVMS goes into its own subdirectory + +/** + * Clean old OVMS installation if it exists + */ +function cleanOldOvmsInstallation() { + console.log('Cleaning the existing OVMS installation...') const csDir = path.join(os.homedir(), '.cherrystudio') - - // Ensure directories exist - fs.mkdirSync(csDir, { recursive: true }) - const csOvmsDir = path.join(csDir, 'ovms') - // Delete existing OVMS directory if it exists if (fs.existsSync(csOvmsDir)) { - fs.rmSync(csOvmsDir, { recursive: true }) - } - - const tempdir = os.tmpdir() - const tempFilename = path.join(tempdir, 'ovms.zip') - - // Try each URL until one succeeds - let downloadSuccess = false - let lastError = null - - for (let i = 0; i < OVMS_RELEASE_BASE_URL.length; i++) { - const downloadUrl = OVMS_RELEASE_BASE_URL[i] - console.log(`Attempting download from URL ${i + 1}/${OVMS_RELEASE_BASE_URL.length}: ${downloadUrl}`) - try { - console.log(`Downloading OVMS from ${downloadUrl} to ${tempFilename}...`) - - // Try PowerShell download first, fallback to Node.js download if it fails - await downloadWithPowerShell(downloadUrl, tempFilename) - - // If we get here, download was successful - downloadSuccess = true - console.log(`Successfully downloaded from: ${downloadUrl}`) - break + fs.rmSync(csOvmsDir, { recursive: true }) } catch (error) { - console.warn(`Download failed from ${downloadUrl}: ${error.message}`) - lastError = error - - // Clean up failed download file if it exists - if (fs.existsSync(tempFilename)) { - try { - fs.unlinkSync(tempFilename) - } catch (cleanupError) { - console.warn(`Failed to clean up temporary file: ${cleanupError.message}`) - } - } - - // Continue to next URL if this one failed - if (i < OVMS_RELEASE_BASE_URL.length - 1) { - console.log(`Trying next URL...`) - } + console.warn(`Failed to clean up old OVMS installation: ${error.message}`) + return 110 } } - // Check if any download succeeded - if (!downloadSuccess) { - console.error(`All download URLs failed. Last error: ${lastError?.message || 'Unknown error'}`) + return 0 +} + +/** + * Install OVMS Base package + */ +async function installOvmsBase() { + // Download the base package + const tempdir = os.tmpdir() + const tempFilename = path.join(tempdir, 'ovms.zip') + + try { + console.log(`Downloading OVMS Base Package from ${OVMS_RELEASE_BASE_URL} to ${tempFilename}...`) + + // Try PowerShell download first, fallback to Node.js download if it fails + await downloadWithPowerShell(OVMS_RELEASE_BASE_URL, tempFilename) + console.log(`Successfully downloaded from: ${OVMS_RELEASE_BASE_URL}`) + } catch (error) { + console.error(`Download OVMS Base failed: ${error.message}`) + fs.unlinkSync(tempFilename) return 103 } - try { - console.log(`Extracting to ${csDir}...`) + // unzip the base package to the target directory + const csDir = path.join(os.homedir(), '.cherrystudio') + const csOvmsDir = path.join(csDir, 'ovms') + fs.mkdirSync(csOvmsDir, { recursive: true }) + try { // Use tar.exe to extract the ZIP file - console.log(`Extracting OVMS to ${csDir}...`) - execSync(`tar -xf ${tempFilename} -C ${csDir}`, { stdio: 'inherit' }) - console.log(`OVMS extracted to ${csDir}`) + console.log(`Extracting OVMS Base to ${csOvmsDir}...`) + execSync(`tar -xf ${tempFilename} -C ${csOvmsDir}`, { stdio: 'inherit' }) + console.log(`OVMS extracted to ${csOvmsDir}`) // Clean up temporary file fs.unlinkSync(tempFilename) console.log(`Installation directory: ${csDir}`) } catch (error) { console.error(`Error installing OVMS: ${error.message}`) - if (fs.existsSync(tempFilename)) { - fs.unlinkSync(tempFilename) - } - - // Check if ovmsDir is empty and remove it if so - try { - const ovmsDir = path.join(csDir, 'ovms') - const files = fs.readdirSync(ovmsDir) - if (files.length === 0) { - fs.rmSync(ovmsDir, { recursive: true }) - console.log(`Removed empty directory: ${ovmsDir}`) - } - } catch (cleanupError) { - console.warn(`Warning: Failed to clean up directory: ${cleanupError.message}`) - return 105 - } - + fs.unlinkSync(tempFilename) return 104 } + const csOvmsBinDir = path.join(csOvmsDir, 'ovms') + // copy ovms.exe to ovdnd.exe + try { + fs.copyFileSync(path.join(csOvmsBinDir, 'ovms.exe'), path.join(csOvmsBinDir, 'ovdnd.exe')) + console.log('Copied ovms.exe to ovdnd.exe') + } catch (error) { + console.error(`Error copying ovms.exe to ovdnd.exe: ${error.message}`) + return 105 + } + + // copy {csOvmsBinDir}/setupvars.bat to {csOvmsBinDir}/run.bat, and append the following lines to run.bat: + // del %USERPROFILE%\.cherrystudio\ovms_log.log + // ovms.exe --config_path models/config.json --rest_port 8000 --log_level DEBUG --log_path %USERPROFILE%\.cherrystudio\ovms_log.log + const runBatPath = path.join(csOvmsBinDir, 'run.bat') + try { + fs.copyFileSync(path.join(csOvmsBinDir, 'setupvars.bat'), runBatPath) + fs.appendFileSync(runBatPath, '\r\n') + fs.appendFileSync(runBatPath, 'del %USERPROFILE%\\.cherrystudio\\ovms_log.log\r\n') + fs.appendFileSync( + runBatPath, + 'ovms.exe --config_path models/config.json --rest_port 8000 --log_level DEBUG --log_path %USERPROFILE%\\.cherrystudio\\ovms_log.log\r\n' + ) + console.log(`Created run.bat at: ${runBatPath}`) + } catch (error) { + console.error(`Error creating run.bat: ${error.message}`) + return 106 + } + + // create {csOvmsBinDir}/models/config.json with content '{"model_config_list": []}' + const configJsonPath = path.join(csOvmsBinDir, 'models', 'config.json') + fs.mkdirSync(path.dirname(configJsonPath), { recursive: true }) + fs.writeFileSync(configJsonPath, '{"mediapipe_config_list":[],"model_config_list":[]}') + console.log(`Created config file: ${configJsonPath}`) + + return 0 +} + +/** + * Install OVMS Extra package + */ +async function installOvmsExtra() { + // Download the extra package + const tempdir = os.tmpdir() + const tempFilename = path.join(tempdir, 'ovms_ex.zip') + + try { + console.log(`Downloading OVMS Extra Package from ${OVMS_EX_URL} to ${tempFilename}...`) + + // Try PowerShell download first, fallback to Node.js download if it fails + await downloadWithPowerShell(OVMS_EX_URL, tempFilename) + console.log(`Successfully downloaded from: ${OVMS_EX_URL}`) + } catch (error) { + console.error(`Download OVMS Extra failed: ${error.message}`) + fs.unlinkSync(tempFilename) + return 103 + } + + // unzip the extra package to the target directory + const csDir = path.join(os.homedir(), '.cherrystudio') + const csOvmsDir = path.join(csDir, 'ovms') + + try { + // Use tar.exe to extract the ZIP file + console.log(`Extracting OVMS Extra to ${csOvmsDir}...`) + execSync(`tar -xf ${tempFilename} -C ${csOvmsDir}`, { stdio: 'inherit' }) + console.log(`OVMS extracted to ${csOvmsDir}`) + + // Clean up temporary file + fs.unlinkSync(tempFilename) + console.log(`Installation directory: ${csDir}`) + } catch (error) { + console.error(`Error installing OVMS Extra: ${error.message}`) + fs.unlinkSync(tempFilename) + return 104 + } + + // apply ovms patch, copy all files in {csOvmsDir}/patch/ovms to {csOvmsDir}/ovms with overwrite mode + const patchDir = path.join(csOvmsDir, 'patch', 'ovms') + const csOvmsBinDir = path.join(csOvmsDir, 'ovms') + try { + const files = fs.readdirSync(patchDir) + files.forEach((file) => { + const srcPath = path.join(patchDir, file) + const destPath = path.join(csOvmsBinDir, file) + fs.copyFileSync(srcPath, destPath) + console.log(`Applied patch file: ${file}`) + }) + } catch (error) { + console.error(`Error applying OVMS patch: ${error.message}`) + } + return 0 } @@ -158,7 +224,27 @@ async function installOvms() { return 102 } - return await downloadOvmsBinary() + // Clean old installation if it exists + const cleanupCode = cleanOldOvmsInstallation() + if (cleanupCode !== 0) { + console.error(`OVMS cleanup failed with code: ${cleanupCode}`) + return cleanupCode + } + + const installBaseCode = await installOvmsBase() + if (installBaseCode !== 0) { + console.error(`OVMS Base installation failed with code: ${installBaseCode}`) + cleanOldOvmsInstallation() + return installBaseCode + } + + const installExtraCode = await installOvmsExtra() + if (installExtraCode !== 0) { + console.error(`OVMS Extra installation failed with code: ${installExtraCode}`) + return installExtraCode + } + + return 0 } // Run the installation diff --git a/src/renderer/src/i18n/locales/en-us.json b/src/renderer/src/i18n/locales/en-us.json index 6b0dd3c0c7..035420b61c 100644 --- a/src/renderer/src/i18n/locales/en-us.json +++ b/src/renderer/src/i18n/locales/en-us.json @@ -2110,8 +2110,10 @@ "install_code_101": "Only supports Intel(R) Core(TM) Ultra CPU", "install_code_102": "Only supports Windows", "install_code_103": "Download OVMS runtime failed", - "install_code_104": "Uncompress OVMS runtime failed", - "install_code_105": "Clean OVMS runtime failed", + "install_code_104": "Failed to install OVMS runtime", + "install_code_105": "Failed to create ovdnd.exe", + "install_code_106": "Failed to create run.bat", + "install_code_110": "Failed to clean old OVMS runtime", "run": "Run OVMS failed:", "stop": "Stop OVMS failed:" }, diff --git a/src/renderer/src/i18n/locales/zh-cn.json b/src/renderer/src/i18n/locales/zh-cn.json index 15675d265a..071ff09c83 100644 --- a/src/renderer/src/i18n/locales/zh-cn.json +++ b/src/renderer/src/i18n/locales/zh-cn.json @@ -2085,7 +2085,7 @@ "description": "

      1. 下载 OV 模型.

      2. 在 'Manager' 中添加模型.

      仅支持 Windows!

      OVMS 安装路径: '%USERPROFILE%\\.cherrystudio\\ovms' .

      请参考 Intel OVMS 指南

      ", "download": { "button": "下载", - "error": "选择失败", + "error": "下载失败", "model_id": { "label": "模型 ID", "model_id_pattern": "模型 ID 必须以 OpenVINO/ 开头", @@ -2110,8 +2110,10 @@ "install_code_101": "仅支持 Intel(R) Core(TM) Ultra CPU", "install_code_102": "仅支持 Windows", "install_code_103": "下载 OVMS runtime 失败", - "install_code_104": "解压 OVMS runtime 失败", - "install_code_105": "清理 OVMS runtime 失败", + "install_code_104": "安装 OVMS runtime 失败", + "install_code_105": "创建 ovdnd.exe 失败", + "install_code_106": "创建 run.bat 失败", + "install_code_110": "清理旧 OVMS runtime 失败", "run": "运行 OVMS 失败:", "stop": "停止 OVMS 失败:" }, diff --git a/src/renderer/src/i18n/locales/zh-tw.json b/src/renderer/src/i18n/locales/zh-tw.json index 020a0e5a19..fea60d0fde 100644 --- a/src/renderer/src/i18n/locales/zh-tw.json +++ b/src/renderer/src/i18n/locales/zh-tw.json @@ -2085,7 +2085,7 @@ "description": "

      1. 下載 OV 模型。

      2. 在 'Manager' 中新增模型。

      僅支援 Windows!

      OVMS 安裝路徑: '%USERPROFILE%\\.cherrystudio\\ovms' 。

      請參考 Intel OVMS 指南

      ", "download": { "button": "下載", - "error": "選擇失敗", + "error": "下載失敗", "model_id": { "label": "模型 ID", "model_id_pattern": "模型 ID 必須以 OpenVINO/ 開頭", @@ -2110,8 +2110,10 @@ "install_code_101": "僅支援 Intel(R) Core(TM) Ultra CPU", "install_code_102": "僅支援 Windows", "install_code_103": "下載 OVMS runtime 失敗", - "install_code_104": "解壓 OVMS runtime 失敗", - "install_code_105": "清理 OVMS runtime 失敗", + "install_code_104": "安裝 OVMS runtime 失敗", + "install_code_105": "創建 ovdnd.exe 失敗", + "install_code_106": "創建 run.bat 失敗", + "install_code_110": "清理舊 OVMS runtime 失敗", "run": "執行 OVMS 失敗:", "stop": "停止 OVMS 失敗:" }, diff --git a/src/renderer/src/pages/settings/ProviderSettings/OVMSSettings.tsx b/src/renderer/src/pages/settings/ProviderSettings/OVMSSettings.tsx index 827e4097e0..057408375e 100644 --- a/src/renderer/src/pages/settings/ProviderSettings/OVMSSettings.tsx +++ b/src/renderer/src/pages/settings/ProviderSettings/OVMSSettings.tsx @@ -36,7 +36,9 @@ const OVMSSettings: FC = () => { '102': t('ovms.failed.install_code_102'), '103': t('ovms.failed.install_code_103'), '104': t('ovms.failed.install_code_104'), - '105': t('ovms.failed.install_code_105') + '105': t('ovms.failed.install_code_105'), + '106': t('ovms.failed.install_code_106'), + '110': t('ovms.failed.install_code_110') } const match = error.message.match(/code (\d+)/) const code = match ? match[1] : 'unknown' @@ -135,7 +137,7 @@ const OVMSSettings: FC = () => { type="primary" onClick={runOvms} loading={isRunningOvms} - disabled={isRunningOvms} + disabled={isRunningOvms || isInstallingOvms} size="small"> {isRunningOvms ? t('ovms.action.starting') : t('ovms.action.run')} From c4e0a6acfe2d904dfbc8ec6b83c22bd1cb6b2408 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Fri, 17 Oct 2025 15:07:19 +0800 Subject: [PATCH 12/27] fix: prevent default behavior for Cmd/Ctrl+F in WebviewService (#10783) fix: prevent default behavior for Cmd/Ctrl+F in WebviewService (#10800) Updated the keyboard handler in WebviewService to always prevent the default action for the Cmd/Ctrl+F shortcut, ensuring it overrides the guest page's native find dialog. This change allows the renderer to manage the behavior of Escape and Enter keys based on the visibility of the search bar. --- src/main/services/WebviewService.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/services/WebviewService.ts b/src/main/services/WebviewService.ts index 1b60cc6643..fb2049de74 100644 --- a/src/main/services/WebviewService.ts +++ b/src/main/services/WebviewService.ts @@ -60,15 +60,20 @@ const attachKeyboardHandler = (contents: Electron.WebContents) => { if (!isFindShortcut && !isEscape && !isEnter) { return } - // Prevent default to override the guest page's native find dialog - // and keep shortcuts routed to our custom search overlay - event.preventDefault() const host = contents.hostWebContents if (!host || host.isDestroyed()) { return } + // Always prevent Cmd/Ctrl+F to override the guest page's native find dialog + if (isFindShortcut) { + event.preventDefault() + } + + // Send the hotkey event to the renderer + // The renderer will decide whether to preventDefault for Escape and Enter + // based on whether the search bar is visible host.send(IpcChannel.Webview_SearchHotkey, { webviewId: contents.id, key, From 0e5ebcfd004da70eea3b2516bdbb8c0e76a6378b Mon Sep 17 00:00:00 2001 From: Kejiang Ma Date: Fri, 17 Oct 2025 15:18:00 +0800 Subject: [PATCH 13/27] feat: new build-in OCR provider -> intel OV(NPU) OCR (#10737) * new build-in ocr provider intel ov Signed-off-by: Ma, Kejiang Signed-off-by: Kejiang Ma * updated base on PR's commnets Signed-off-by: Kejiang Ma * feat(OcrImageSettings): use swr to fetch available providers Add loading state and error handling when fetching available OCR providers. Display an alert when provider loading fails, showing the error message. Also optimize provider filtering logic using useMemo. * refactor(ocr): rename providers to listProviders for consistency Update method name to better reflect its functionality and maintain naming consistency across the codebase --------- Signed-off-by: Ma, Kejiang Signed-off-by: Kejiang Ma Co-authored-by: icarus --- packages/shared/IpcChannel.ts | 1 + src/main/ipc.ts | 1 + src/main/services/ocr/OcrService.ts | 7 + src/main/services/ocr/builtin/OvOcrService.ts | 128 ++++++++++++++++++ src/preload/index.ts | 3 +- src/renderer/src/config/ocr.ts | 16 ++- src/renderer/src/hooks/useOcrProvider.tsx | 3 + src/renderer/src/i18n/label.ts | 4 +- src/renderer/src/i18n/locales/en-us.json | 1 + src/renderer/src/i18n/locales/zh-cn.json | 1 + src/renderer/src/i18n/locales/zh-tw.json | 5 +- src/renderer/src/i18n/translate/el-gr.json | 1 + src/renderer/src/i18n/translate/es-es.json | 1 + src/renderer/src/i18n/translate/fr-fr.json | 1 + src/renderer/src/i18n/translate/ja-jp.json | 1 + src/renderer/src/i18n/translate/pt-pt.json | 1 + src/renderer/src/i18n/translate/ru-ru.json | 1 + .../DocProcessSettings/OcrImageSettings.tsx | 49 +++++-- .../DocProcessSettings/OcrOVSettings.tsx | 32 +++++ .../OcrProviderSettings.tsx | 3 + src/renderer/src/store/migrate.ts | 9 ++ src/renderer/src/types/ocr.ts | 19 ++- 22 files changed, 270 insertions(+), 18 deletions(-) create mode 100644 src/main/services/ocr/builtin/OvOcrService.ts create mode 100644 src/renderer/src/pages/settings/DocProcessSettings/OcrOVSettings.tsx diff --git a/packages/shared/IpcChannel.ts b/packages/shared/IpcChannel.ts index 93679f5faa..be037f6669 100644 --- a/packages/shared/IpcChannel.ts +++ b/packages/shared/IpcChannel.ts @@ -337,6 +337,7 @@ export enum IpcChannel { // OCR OCR_ocr = 'ocr:ocr', + OCR_ListProviders = 'ocr:list-providers', // OVMS Ovms_AddModel = 'ovms:add-model', diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 55d60fa203..f1a4de6a59 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -875,6 +875,7 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { ipcMain.handle(IpcChannel.OCR_ocr, (_, file: SupportedOcrFile, provider: OcrProvider) => ocrService.ocr(file, provider) ) + ipcMain.handle(IpcChannel.OCR_ListProviders, () => ocrService.listProviderIds()) // OVMS ipcMain.handle(IpcChannel.Ovms_AddModel, (_, modelName: string, modelId: string, modelSource: string, task: string) => diff --git a/src/main/services/ocr/OcrService.ts b/src/main/services/ocr/OcrService.ts index 471d31edce..b2943e30ec 100644 --- a/src/main/services/ocr/OcrService.ts +++ b/src/main/services/ocr/OcrService.ts @@ -2,6 +2,7 @@ import { loggerService } from '@logger' import { isLinux } from '@main/constant' import { BuiltinOcrProviderIds, OcrHandler, OcrProvider, OcrResult, SupportedOcrFile } from '@types' +import { ovOcrService } from './builtin/OvOcrService' import { ppocrService } from './builtin/PpocrService' import { systemOcrService } from './builtin/SystemOcrService' import { tesseractService } from './builtin/TesseractService' @@ -22,6 +23,10 @@ export class OcrService { this.registry.delete(providerId) } + public listProviderIds(): string[] { + return Array.from(this.registry.keys()) + } + public async ocr(file: SupportedOcrFile, provider: OcrProvider): Promise { const handler = this.registry.get(provider.id) if (!handler) { @@ -39,3 +44,5 @@ ocrService.register(BuiltinOcrProviderIds.tesseract, tesseractService.ocr.bind(t !isLinux && ocrService.register(BuiltinOcrProviderIds.system, systemOcrService.ocr.bind(systemOcrService)) ocrService.register(BuiltinOcrProviderIds.paddleocr, ppocrService.ocr.bind(ppocrService)) + +ovOcrService.isAvailable() && ocrService.register(BuiltinOcrProviderIds.ovocr, ovOcrService.ocr.bind(ovOcrService)) diff --git a/src/main/services/ocr/builtin/OvOcrService.ts b/src/main/services/ocr/builtin/OvOcrService.ts new file mode 100644 index 0000000000..1650ca8832 --- /dev/null +++ b/src/main/services/ocr/builtin/OvOcrService.ts @@ -0,0 +1,128 @@ +import { loggerService } from '@logger' +import { isWin } from '@main/constant' +import { isImageFileMetadata, OcrOvConfig, OcrResult, SupportedOcrFile } from '@types' +import { exec } from 'child_process' +import * as fs from 'fs' +import * as os from 'os' +import * as path from 'path' +import { promisify } from 'util' + +import { OcrBaseService } from './OcrBaseService' + +const logger = loggerService.withContext('OvOcrService') +const execAsync = promisify(exec) + +const PATH_BAT_FILE = path.join(os.homedir(), '.cherrystudio', 'ovms', 'ovocr', 'run.npu.bat') + +export class OvOcrService extends OcrBaseService { + constructor() { + super() + } + + public isAvailable(): boolean { + return ( + isWin && + os.cpus()[0].model.toLowerCase().includes('intel') && + os.cpus()[0].model.toLowerCase().includes('ultra') && + fs.existsSync(PATH_BAT_FILE) + ) + } + + private getOvOcrPath(): string { + return path.join(os.homedir(), '.cherrystudio', 'ovms', 'ovocr') + } + + private getImgDir(): string { + return path.join(this.getOvOcrPath(), 'img') + } + + private getOutputDir(): string { + return path.join(this.getOvOcrPath(), 'output') + } + + private async clearDirectory(dirPath: string): Promise { + if (fs.existsSync(dirPath)) { + const files = await fs.promises.readdir(dirPath) + for (const file of files) { + const filePath = path.join(dirPath, file) + const stats = await fs.promises.stat(filePath) + if (stats.isDirectory()) { + await this.clearDirectory(filePath) + await fs.promises.rmdir(filePath) + } else { + await fs.promises.unlink(filePath) + } + } + } else { + // If the directory does not exist, create it + await fs.promises.mkdir(dirPath, { recursive: true }) + } + } + + private async copyFileToImgDir(sourceFilePath: string, targetFileName: string): Promise { + const imgDir = this.getImgDir() + const targetFilePath = path.join(imgDir, targetFileName) + await fs.promises.copyFile(sourceFilePath, targetFilePath) + } + + private async runOcrBatch(): Promise { + const ovOcrPath = this.getOvOcrPath() + + try { + // Execute run.bat in the ov-ocr directory + await execAsync(`"${PATH_BAT_FILE}"`, { + cwd: ovOcrPath, + timeout: 60000 // 60 second timeout + }) + } catch (error) { + logger.error(`Error running ovocr batch: ${error}`) + throw new Error(`Failed to run OCR batch: ${error}`) + } + } + + private async ocrImage(filePath: string, options?: OcrOvConfig): Promise { + logger.info(`OV OCR called on ${filePath} with options ${JSON.stringify(options)}`) + + try { + // 1. Clear img directory and output directory + await this.clearDirectory(this.getImgDir()) + await this.clearDirectory(this.getOutputDir()) + + // 2. Copy file to img directory + const fileName = path.basename(filePath) + await this.copyFileToImgDir(filePath, fileName) + logger.info(`File copied to img directory: ${fileName}`) + + // 3. Run run.bat + logger.info('Running OV OCR batch process...') + await this.runOcrBatch() + + // 4. Check that output/[basename].txt file exists + const baseNameWithoutExt = path.basename(fileName, path.extname(fileName)) + const outputFilePath = path.join(this.getOutputDir(), `${baseNameWithoutExt}.txt`) + if (!fs.existsSync(outputFilePath)) { + throw new Error(`OV OCR output file not found at: ${outputFilePath}`) + } + + // 5. Read output/[basename].txt file content + const ocrText = await fs.promises.readFile(outputFilePath, 'utf-8') + logger.info(`OV OCR text extracted: ${ocrText.substring(0, 100)}...`) + + // 6. Return result + return { text: ocrText } + } catch (error) { + logger.error(`Error during OV OCR process: ${error}`) + throw error + } + } + + public ocr = async (file: SupportedOcrFile, options?: OcrOvConfig): Promise => { + if (isImageFileMetadata(file)) { + return this.ocrImage(file.path, options) + } else { + throw new Error('Unsupported file type, currently only image files are supported') + } + } +} + +export const ovOcrService = new OvOcrService() diff --git a/src/preload/index.ts b/src/preload/index.ts index 34656092b2..9004560045 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -480,7 +480,8 @@ const api = { }, ocr: { ocr: (file: SupportedOcrFile, provider: OcrProvider): Promise => - ipcRenderer.invoke(IpcChannel.OCR_ocr, file, provider) + ipcRenderer.invoke(IpcChannel.OCR_ocr, file, provider), + listProviders: (): Promise => ipcRenderer.invoke(IpcChannel.OCR_ListProviders) }, cherryai: { generateSignature: (params: { method: string; path: string; query: string; body: Record }) => diff --git a/src/renderer/src/config/ocr.ts b/src/renderer/src/config/ocr.ts index 02f0995a82..6eb52f1ad3 100644 --- a/src/renderer/src/config/ocr.ts +++ b/src/renderer/src/config/ocr.ts @@ -1,6 +1,7 @@ import { BuiltinOcrProvider, BuiltinOcrProviderId, + OcrOvProvider, OcrPpocrProvider, OcrProviderCapability, OcrSystemProvider, @@ -50,10 +51,23 @@ const ppocrOcr: OcrPpocrProvider = { } } as const +const ovOcr: OcrOvProvider = { + id: 'ovocr', + name: 'Intel OV(NPU) OCR', + config: { + langs: isWin ? ['en-us', 'zh-cn'] : undefined + }, + capabilities: { + image: true + // pdf: true + } +} as const satisfies OcrOvProvider + export const BUILTIN_OCR_PROVIDERS_MAP = { tesseract, system: systemOcr, - paddleocr: ppocrOcr + paddleocr: ppocrOcr, + ovocr: ovOcr } as const satisfies Record export const BUILTIN_OCR_PROVIDERS: BuiltinOcrProvider[] = Object.values(BUILTIN_OCR_PROVIDERS_MAP) diff --git a/src/renderer/src/hooks/useOcrProvider.tsx b/src/renderer/src/hooks/useOcrProvider.tsx index 0a40f047ff..bd597e2267 100644 --- a/src/renderer/src/hooks/useOcrProvider.tsx +++ b/src/renderer/src/hooks/useOcrProvider.tsx @@ -1,4 +1,5 @@ import { loggerService } from '@logger' +import IntelLogo from '@renderer/assets/images/providers/intel.png' import PaddleocrLogo from '@renderer/assets/images/providers/paddleocr.png' import TesseractLogo from '@renderer/assets/images/providers/Tesseract.js.png' import { BUILTIN_OCR_PROVIDERS_MAP, DEFAULT_OCR_PROVIDER } from '@renderer/config/ocr' @@ -83,6 +84,8 @@ export const useOcrProviders = () => { return case 'paddleocr': return + case 'ovocr': + return } } return diff --git a/src/renderer/src/i18n/label.ts b/src/renderer/src/i18n/label.ts index c2ac69f1be..51edc964b6 100644 --- a/src/renderer/src/i18n/label.ts +++ b/src/renderer/src/i18n/label.ts @@ -340,12 +340,14 @@ export const getBuiltInMcpServerDescriptionLabel = (key: string): string => { const builtinOcrProviderKeyMap = { system: 'ocr.builtin.system', tesseract: '', - paddleocr: '' + paddleocr: '', + ovocr: '' } as const satisfies Record export const getBuiltinOcrProviderLabel = (key: BuiltinOcrProviderId) => { if (key === 'tesseract') return 'Tesseract' else if (key == 'paddleocr') return 'PaddleOCR' + else if (key == 'ovocr') return 'Intel OV(NPU) OCR' else return getLabel(builtinOcrProviderKeyMap, key) } diff --git a/src/renderer/src/i18n/locales/en-us.json b/src/renderer/src/i18n/locales/en-us.json index 035420b61c..98cce3a51e 100644 --- a/src/renderer/src/i18n/locales/en-us.json +++ b/src/renderer/src/i18n/locales/en-us.json @@ -2049,6 +2049,7 @@ "provider": { "cannot_remove_builtin": "Cannot delete built-in provider", "existing": "The provider already exists", + "get_providers": "Failed to get available providers", "not_found": "OCR provider does not exist", "update_failed": "Failed to update configuration" }, diff --git a/src/renderer/src/i18n/locales/zh-cn.json b/src/renderer/src/i18n/locales/zh-cn.json index 071ff09c83..8e5e4ebe38 100644 --- a/src/renderer/src/i18n/locales/zh-cn.json +++ b/src/renderer/src/i18n/locales/zh-cn.json @@ -2049,6 +2049,7 @@ "provider": { "cannot_remove_builtin": "不能删除内置提供商", "existing": "提供商已存在", + "get_providers": "获取可用提供商失败", "not_found": "OCR 提供商不存在", "update_failed": "更新配置失败" }, diff --git a/src/renderer/src/i18n/locales/zh-tw.json b/src/renderer/src/i18n/locales/zh-tw.json index fea60d0fde..0b88424662 100644 --- a/src/renderer/src/i18n/locales/zh-tw.json +++ b/src/renderer/src/i18n/locales/zh-tw.json @@ -2048,8 +2048,9 @@ "error": { "provider": { "cannot_remove_builtin": "不能刪除內建提供者", - "existing": "提供商已存在", - "not_found": "OCR 提供商不存在", + "existing": "提供者已存在", + "get_providers": "取得可用提供者失敗", + "not_found": "OCR 提供者不存在", "update_failed": "更新配置失敗" }, "unknown": "OCR過程發生錯誤" diff --git a/src/renderer/src/i18n/translate/el-gr.json b/src/renderer/src/i18n/translate/el-gr.json index 370f3ce658..c1d289316d 100644 --- a/src/renderer/src/i18n/translate/el-gr.json +++ b/src/renderer/src/i18n/translate/el-gr.json @@ -2041,6 +2041,7 @@ "provider": { "cannot_remove_builtin": "Δεν είναι δυνατή η διαγραφή του ενσωματωμένου παρόχου", "existing": "Ο πάροχος υπηρεσιών υπάρχει ήδη", + "get_providers": "Αποτυχία λήψης διαθέσιμων παρόχων", "not_found": "Ο πάροχος OCR δεν υπάρχει", "update_failed": "Αποτυχία ενημέρωσης της διαμόρφωσης" }, diff --git a/src/renderer/src/i18n/translate/es-es.json b/src/renderer/src/i18n/translate/es-es.json index 0c923c9735..5825c72fd2 100644 --- a/src/renderer/src/i18n/translate/es-es.json +++ b/src/renderer/src/i18n/translate/es-es.json @@ -2041,6 +2041,7 @@ "provider": { "cannot_remove_builtin": "No se puede eliminar el proveedor integrado", "existing": "El proveedor ya existe", + "get_providers": "Error al obtener proveedores disponibles", "not_found": "El proveedor de OCR no existe", "update_failed": "Actualización de la configuración fallida" }, diff --git a/src/renderer/src/i18n/translate/fr-fr.json b/src/renderer/src/i18n/translate/fr-fr.json index 9e36984cbf..e82bbe99e8 100644 --- a/src/renderer/src/i18n/translate/fr-fr.json +++ b/src/renderer/src/i18n/translate/fr-fr.json @@ -2041,6 +2041,7 @@ "provider": { "cannot_remove_builtin": "Impossible de supprimer le fournisseur intégré", "existing": "Le fournisseur existe déjà", + "get_providers": "Échec de l'obtention des fournisseurs disponibles", "not_found": "Le fournisseur OCR n'existe pas", "update_failed": "Échec de la mise à jour de la configuration" }, diff --git a/src/renderer/src/i18n/translate/ja-jp.json b/src/renderer/src/i18n/translate/ja-jp.json index d9db953aef..5600675de2 100644 --- a/src/renderer/src/i18n/translate/ja-jp.json +++ b/src/renderer/src/i18n/translate/ja-jp.json @@ -2041,6 +2041,7 @@ "provider": { "cannot_remove_builtin": "組み込みプロバイダーは削除できません", "existing": "プロバイダーはすでに存在します", + "get_providers": "利用可能なプロバイダーの取得に失敗しました", "not_found": "OCRプロバイダーが存在しません", "update_failed": "更新構成に失敗しました" }, diff --git a/src/renderer/src/i18n/translate/pt-pt.json b/src/renderer/src/i18n/translate/pt-pt.json index 8f46afda0c..c732b90f1a 100644 --- a/src/renderer/src/i18n/translate/pt-pt.json +++ b/src/renderer/src/i18n/translate/pt-pt.json @@ -2041,6 +2041,7 @@ "provider": { "cannot_remove_builtin": "Não é possível excluir o provedor integrado", "existing": "O provedor já existe", + "get_providers": "Falha ao obter provedores disponíveis", "not_found": "O provedor OCR não existe", "update_failed": "Falha ao atualizar a configuração" }, diff --git a/src/renderer/src/i18n/translate/ru-ru.json b/src/renderer/src/i18n/translate/ru-ru.json index 8bf1b823c5..394cc9163a 100644 --- a/src/renderer/src/i18n/translate/ru-ru.json +++ b/src/renderer/src/i18n/translate/ru-ru.json @@ -2041,6 +2041,7 @@ "provider": { "cannot_remove_builtin": "Не удается удалить встроенного поставщика", "existing": "Поставщик уже существует", + "get_providers": "Не удалось получить доступных поставщиков", "not_found": "Поставщик OCR отсутствует", "update_failed": "Обновление конфигурации не удалось" }, diff --git a/src/renderer/src/pages/settings/DocProcessSettings/OcrImageSettings.tsx b/src/renderer/src/pages/settings/DocProcessSettings/OcrImageSettings.tsx index 622d1349d4..9050088d56 100644 --- a/src/renderer/src/pages/settings/DocProcessSettings/OcrImageSettings.tsx +++ b/src/renderer/src/pages/settings/DocProcessSettings/OcrImageSettings.tsx @@ -1,11 +1,14 @@ +import { Alert, Skeleton } from '@heroui/react' import { loggerService } from '@logger' import { ErrorTag } from '@renderer/components/Tags/ErrorTag' import { isMac, isWin } from '@renderer/config/constant' import { useOcrProviders } from '@renderer/hooks/useOcrProvider' import { BuiltinOcrProviderIds, ImageOcrProvider, isImageOcrProvider, OcrProvider } from '@renderer/types' +import { getErrorMessage } from '@renderer/utils' import { Select } from 'antd' -import { useEffect, useMemo } from 'react' +import { useCallback, useEffect, useMemo } from 'react' import { useTranslation } from 'react-i18next' +import useSWRImmutable from 'swr/immutable' import { SettingRow, SettingRowTitle } from '..' @@ -18,10 +21,16 @@ type Props = { const OcrImageSettings = ({ setProvider }: Props) => { const { t } = useTranslation() const { providers, imageProvider, getOcrProviderName, setImageProviderId } = useOcrProviders() + const fetcher = useCallback(() => { + return window.api.ocr.listProviders() + }, []) + + const { data: validProviders, isLoading, error } = useSWRImmutable('ocr/providers', fetcher) const imageProviders = providers.filter((p) => isImageOcrProvider(p)) // 挂载时更新外部状态 + // FIXME: Just keep the imageProvider always valid, so we don't need update it in this component. useEffect(() => { setProvider(imageProvider) }, [imageProvider, setProvider]) @@ -40,12 +49,17 @@ const OcrImageSettings = ({ setProvider }: Props) => { const platformSupport = isMac || isWin const options = useMemo(() => { + if (!validProviders) return [] const platformFilter = platformSupport ? () => true : (p: ImageOcrProvider) => p.id !== BuiltinOcrProviderIds.system - return imageProviders.filter(platformFilter).map((p) => ({ - value: p.id, - label: getOcrProviderName(p) - })) - }, [getOcrProviderName, imageProviders, platformSupport]) + const validFilter = (p: ImageOcrProvider) => validProviders.includes(p.id) + return imageProviders + .filter(platformFilter) + .filter(validFilter) + .map((p) => ({ + value: p.id, + label: getOcrProviderName(p) + })) + }, [getOcrProviderName, imageProviders, platformSupport, validProviders]) const isSystem = imageProvider.id === BuiltinOcrProviderIds.system @@ -55,12 +69,23 @@ const OcrImageSettings = ({ setProvider }: Props) => { {t('settings.tool.ocr.image_provider')}
      {!platformSupport && isSystem && } - setImageProvider(id)} + options={options} + /> + )} + {error && ( + + )} +
      diff --git a/src/renderer/src/pages/settings/DocProcessSettings/OcrOVSettings.tsx b/src/renderer/src/pages/settings/DocProcessSettings/OcrOVSettings.tsx new file mode 100644 index 0000000000..5075766086 --- /dev/null +++ b/src/renderer/src/pages/settings/DocProcessSettings/OcrOVSettings.tsx @@ -0,0 +1,32 @@ +import { useOcrProvider } from '@renderer/hooks/useOcrProvider' +import { BuiltinOcrProviderIds, isOcrOVProvider } from '@renderer/types' +import { Flex, Tag } from 'antd' +import { useTranslation } from 'react-i18next' + +import { SettingRow, SettingRowTitle } from '..' + +export const OcrOVSettings = () => { + const { t } = useTranslation() + const { provider } = useOcrProvider(BuiltinOcrProviderIds.ovocr) + + if (!isOcrOVProvider(provider)) { + throw new Error('Not OV OCR provider.') + } + + return ( + <> + + + + {t('settings.tool.ocr.common.langs')} + + +
      + 🇬🇧 {t('languages.english')} + 🇨🇳 {t('languages.chinese')} + 🇭🇰 {t('languages.chinese-traditional')} +
      +
      + + ) +} diff --git a/src/renderer/src/pages/settings/DocProcessSettings/OcrProviderSettings.tsx b/src/renderer/src/pages/settings/DocProcessSettings/OcrProviderSettings.tsx index 120e5a9e48..482ff2b9e8 100644 --- a/src/renderer/src/pages/settings/DocProcessSettings/OcrProviderSettings.tsx +++ b/src/renderer/src/pages/settings/DocProcessSettings/OcrProviderSettings.tsx @@ -8,6 +8,7 @@ import { Divider, Flex } from 'antd' import styled from 'styled-components' import { SettingGroup, SettingTitle } from '..' +import { OcrOVSettings } from './OcrOVSettings' import { OcrPpocrSettings } from './OcrPpocrSettings' import { OcrSystemSettings } from './OcrSystemSettings' import { OcrTesseractSettings } from './OcrTesseractSettings' @@ -35,6 +36,8 @@ const OcrProviderSettings = ({ provider }: Props) => { return case 'paddleocr': return + case 'ovocr': + return default: return null } diff --git a/src/renderer/src/store/migrate.ts b/src/renderer/src/store/migrate.ts index 362396ca2d..5cca66b47a 100644 --- a/src/renderer/src/store/migrate.ts +++ b/src/renderer/src/store/migrate.ts @@ -2667,6 +2667,15 @@ const migrateConfig = { logger.error('migrate 162 error', error as Error) return state } + }, + '163': (state: RootState) => { + try { + addOcrProvider(state, BUILTIN_OCR_PROVIDERS_MAP.ovocr) + return state + } catch (error) { + logger.error('migrate 163 error', error as Error) + return state + } } } diff --git a/src/renderer/src/types/ocr.ts b/src/renderer/src/types/ocr.ts index d67cba958d..765c4e411c 100644 --- a/src/renderer/src/types/ocr.ts +++ b/src/renderer/src/types/ocr.ts @@ -5,7 +5,8 @@ import { FileMetadata, ImageFileMetadata, isImageFileMetadata, TranslateLanguage export const BuiltinOcrProviderIds = { tesseract: 'tesseract', system: 'system', - paddleocr: 'paddleocr' + paddleocr: 'paddleocr', + ovocr: 'ovocr' } as const export type BuiltinOcrProviderId = keyof typeof BuiltinOcrProviderIds @@ -188,3 +189,19 @@ export type OcrPpocrProvider = { export const isOcrPpocrProvider = (p: OcrProvider): p is OcrPpocrProvider => { return p.id === BuiltinOcrProviderIds.paddleocr } + +// OV OCR Types +export type OcrOvConfig = OcrProviderBaseConfig & { + langs?: TranslateLanguageCode[] +} + +export type OcrOvProvider = { + id: 'ovocr' + config: OcrOvConfig +} & ImageOcrProvider & + // PdfOcrProvider & + BuiltinOcrProvider + +export const isOcrOVProvider = (p: OcrProvider): p is OcrOvProvider => { + return p.id === BuiltinOcrProviderIds.ovocr +} From d1a9dfa3e60b1834e4948c14643247e84f2fde6f Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Fri, 17 Oct 2025 17:19:16 +0800 Subject: [PATCH 14/27] feat: add Greek language option to spell checker options (#10793) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit feat: add Greek language option to GeneralSettings component - Added support for Greek (Ελληνικά) language in the language selection dropdown of the GeneralSettings component. --- src/renderer/src/pages/settings/GeneralSettings.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/renderer/src/pages/settings/GeneralSettings.tsx b/src/renderer/src/pages/settings/GeneralSettings.tsx index 84f54168af..3bfdd5968c 100644 --- a/src/renderer/src/pages/settings/GeneralSettings.tsx +++ b/src/renderer/src/pages/settings/GeneralSettings.tsx @@ -147,7 +147,8 @@ const GeneralSettings: FC = () => { { value: 'pt', label: 'Português', flag: '🇵🇹' }, { value: 'ru', label: 'Русский', flag: '🇷🇺' }, { value: 'nl', label: 'Nederlands', flag: '🇳🇱' }, - { value: 'pl', label: 'Polski', flag: '🇵🇱' } + { value: 'pl', label: 'Polski', flag: '🇵🇱' }, + { value: 'el', label: 'Ελληνικά', flag: '🇬🇷' } ] const handleSpellCheckLanguagesChange = (selectedLanguages: string[]) => { From 4eb3aa31eeedba0fa4d35c20097e149f1f219f64 Mon Sep 17 00:00:00 2001 From: Phantom Date: Fri, 17 Oct 2025 19:44:47 +0800 Subject: [PATCH 15/27] feat: session settings (#10773) * fix(home/Tabs): remove redundant isTopicView check in tab rendering * refactor(runtime): rename activeSessionId to activeSessionIdMap for clarity Update variable name to better reflect its purpose as a mapping structure * refactor(agent): add CreateAgentSessionResponse type and schema Add new type and schema for create session response to better reflect API contract * fix(useSessions): return null instead of undefined on session creation error Returning null provides better type safety and aligns with the response type Promise * refactor(useSessions): add return type to deleteSession callback * fix(useSessions): return session data or null from getSession Ensure getSession callback always returns a value (session data or null) to handle error cases properly and improve type safety * feat(hooks): add useActiveSession hook and handle null agentId in useSessions Add new hook to get active session from runtime and sessions data. Update useSessions to handle null agentId cases by returning early and adding null checks. * feat(hooks): add useActiveAgent hook to get active agent Expose active agent data by combining useRuntime and useAgent hooks * fix(agents): remove fake agent id handling and improve null checks - Replace fake agent id with null in HomePage - Remove fake id check in useAgent hook and throw error for null id - Simplify agent session initialization by removing fake id checks * refactor(hooks): replace useAgent with useActiveAgent for active agent state * feat(home): add session settings tab component Replace AgentSettingsTab with SessionSettingsTab to better handle session-specific settings. The new component includes essential and advanced settings sections with a more settings button. * refactor(settings): consolidate agent and session essential settings into single component Replace AgentEssentialSettings and SessionEssentialSettings with a unified EssentialSettings component that handles both agent and session types. This reduces code duplication and improves maintainability. * style(SelectAgentModelButton): improve model name display with truncation Add overflow-x-hidden to container and truncate to model name span to prevent text overflow * refactor(AgentSettings): replace Ellipsis with truncate for text overflow Use CSS truncate instead of Ellipsis component for better performance and consistency * refactor(chat-navbar): replace useAgent and useSession with useActiveAgent and useActiveSession Simplify component logic by using dedicated hooks for active agent and session * feat(ChatNavbar): add session settings button to breadcrumb Add clickable session label chip that opens session settings popup when active session exists * refactor(agents): improve session update hook and type definitions - Extract UpdateAgentBaseOptions type to shared types file - Update useUpdateSession to return both updateSession and updateModel functions - Modify components to use destructured updateSession from hook - Add null check for agentId in useUpdateSession - Add success toast option to session updates * refactor(components): rename agent prop to agentBase for clarity Update component name and prop to better reflect its purpose and improve code readability * refactor(ChatNavbar): rename SelectAgentModelButton to SelectAgentBaseModelButton and update usage Update component name to better reflect its purpose and adjust props to use activeSession instead of activeAgent for consistency * feat(i18n): add null id error message for agent retrieval Add error message for when agent ID is null across all supported languages * refactor(hooks): simplify agent and session hooks by returning destructured values Remove unnecessary intermediate variables and directly return hook results Update useSession to handle null agentId and sessionId cases * feat(i18n): add null session ID error message for all locales * refactor(home): rename SelectAgentModelButton to SelectAgentBaseModelButton The component was renamed to better reflect its purpose of selecting base models for agents. The functionality remains unchanged. * refactor(session): rename useUpdateAgent to useUpdateSession for clarity * refactor(home-tabs): replace useUpdateAgent with useUpdateSession hook Update session settings tab to use the new useUpdateSession hook which requires activeAgentId * style(AgentSettings): remove unnecessary gap class from ModelSetting * refactor(agents): improve error handling and remove duplicate code - Replace formatErrorMessageWithPrefix with getErrorMessage for better error handling - Move updateSession logic to useUpdateSession hook to avoid duplication * fix(ChatNavbar): prevent model update when activeAgent is missing Add activeAgent to dependency array and check its existence before updating model to avoid potential errors * feat(home/Tabs): add loading and error states for session settings Add Skeleton loader and Alert component to handle loading and error states when fetching session data in the settings tab * fix(home/Tabs): add h-full class to Skeleton for proper height * fix(AssistantsTab): remove weird effect hook for agent selection * refactor(chat-navbar): clean up unused code and update session handling remove commented out code in ChatNavbar.tsx and update ChatNavbarContent to use active agent/session hooks * style(home): remove negative margin from model name span * refactor(Agents): mark Agents component as deprecated * refactor: remove unused Agents and Assistants code --------- Co-authored-by: dev --- src/renderer/src/api/agent.ts | 6 +- .../components/Popups/agent/SessionModal.tsx | 2 +- src/renderer/src/hooks/agents/types.ts | 4 + .../src/hooks/agents/useActiveAgent.ts | 8 + .../src/hooks/agents/useActiveSession.ts | 9 + src/renderer/src/hooks/agents/useAgent.ts | 4 +- .../agents/useAgentSessionInitializer.ts | 14 +- src/renderer/src/hooks/agents/useSession.ts | 28 +-- src/renderer/src/hooks/agents/useSessions.ts | 20 +- .../src/hooks/agents/useUpdateAgent.ts | 10 +- .../src/hooks/agents/useUpdateSession.ts | 36 ++- src/renderer/src/i18n/locales/en-us.json | 6 +- src/renderer/src/i18n/locales/zh-cn.json | 6 +- src/renderer/src/i18n/locales/zh-tw.json | 6 +- src/renderer/src/i18n/translate/el-gr.json | 6 +- src/renderer/src/i18n/translate/es-es.json | 6 +- src/renderer/src/i18n/translate/fr-fr.json | 6 +- src/renderer/src/i18n/translate/ja-jp.json | 6 +- src/renderer/src/i18n/translate/pt-pt.json | 6 +- src/renderer/src/i18n/translate/ru-ru.json | 6 +- src/renderer/src/pages/home/Chat.tsx | 19 +- src/renderer/src/pages/home/ChatNavbar.tsx | 8 + src/renderer/src/pages/home/HomePage.tsx | 2 +- .../src/pages/home/Tabs/AgentSettingsTab.tsx | 40 ---- .../src/pages/home/Tabs/AssistantsTab.tsx | 8 +- .../pages/home/Tabs/SessionSettingsTab.tsx | 43 ++++ .../src/pages/home/Tabs/components/Agents.tsx | 71 ------ .../pages/home/Tabs/components/Assistants.tsx | 208 ------------------ .../home/Tabs/components/SessionItem.tsx | 4 +- .../pages/home/Tabs/components/Sessions.tsx | 14 +- src/renderer/src/pages/home/Tabs/index.tsx | 30 ++- .../home/components/ChatNavbarContent.tsx | 80 ++++--- ...ton.tsx => SelectAgentBaseModelButton.tsx} | 10 +- .../AgentSettings/AdvancedSettings.tsx | 2 +- .../AgentSettings/AgentEssentialSettings.tsx | 47 ---- .../AgentSettings/AgentSettingsPopup.tsx | 4 +- .../AgentSettings/EssentialSettings.tsx | 56 +++++ .../settings/AgentSettings/ModelSetting.tsx | 6 +- .../settings/AgentSettings/PromptSettings.tsx | 2 +- .../SessionEssentialSettings.tsx | 29 --- .../AgentSettings/SessionSettingsPopup.tsx | 6 +- .../pages/settings/AgentSettings/shared.tsx | 9 +- src/renderer/src/store/runtime.ts | 6 +- src/renderer/src/types/agent.ts | 4 + 44 files changed, 348 insertions(+), 555 deletions(-) create mode 100644 src/renderer/src/hooks/agents/types.ts create mode 100644 src/renderer/src/hooks/agents/useActiveAgent.ts create mode 100644 src/renderer/src/hooks/agents/useActiveSession.ts delete mode 100644 src/renderer/src/pages/home/Tabs/AgentSettingsTab.tsx create mode 100644 src/renderer/src/pages/home/Tabs/SessionSettingsTab.tsx delete mode 100644 src/renderer/src/pages/home/Tabs/components/Agents.tsx delete mode 100644 src/renderer/src/pages/home/Tabs/components/Assistants.tsx rename src/renderer/src/pages/home/components/{SelectAgentModelButton.tsx => SelectAgentBaseModelButton.tsx} (87%) delete mode 100644 src/renderer/src/pages/settings/AgentSettings/AgentEssentialSettings.tsx create mode 100644 src/renderer/src/pages/settings/AgentSettings/EssentialSettings.tsx delete mode 100644 src/renderer/src/pages/settings/AgentSettings/SessionEssentialSettings.tsx diff --git a/src/renderer/src/api/agent.ts b/src/renderer/src/api/agent.ts index 2b31873ce1..a16e4eb600 100644 --- a/src/renderer/src/api/agent.ts +++ b/src/renderer/src/api/agent.ts @@ -9,6 +9,8 @@ import { CreateAgentRequest, CreateAgentResponse, CreateAgentResponseSchema, + CreateAgentSessionResponse, + CreateAgentSessionResponseSchema, CreateSessionForm, CreateSessionRequest, GetAgentResponse, @@ -171,12 +173,12 @@ export class AgentApiClient { } } - public async createSession(agentId: string, session: CreateSessionForm): Promise { + public async createSession(agentId: string, session: CreateSessionForm): Promise { const url = this.getSessionPaths(agentId).base try { const payload = session satisfies CreateSessionRequest const response = await this.axios.post(url, payload) - const data = GetAgentSessionResponseSchema.parse(response.data) + const data = CreateAgentSessionResponseSchema.parse(response.data) return data } catch (error) { throw processError(error, 'Failed to add session.') diff --git a/src/renderer/src/components/Popups/agent/SessionModal.tsx b/src/renderer/src/components/Popups/agent/SessionModal.tsx index aaf0029ee7..80ca25f475 100644 --- a/src/renderer/src/components/Popups/agent/SessionModal.tsx +++ b/src/renderer/src/components/Popups/agent/SessionModal.tsx @@ -98,7 +98,7 @@ export const SessionModal: React.FC = ({ const loadingRef = useRef(false) // const { setTimeoutTimer } = useTimer() const { createSession } = useSessions(agentId) - const updateSession = useUpdateSession(agentId) + const { updateSession } = useUpdateSession(agentId) const { agent } = useAgent(agentId) const isEditing = (session?: AgentSessionEntity) => session !== undefined diff --git a/src/renderer/src/hooks/agents/types.ts b/src/renderer/src/hooks/agents/types.ts new file mode 100644 index 0000000000..9cf5769f57 --- /dev/null +++ b/src/renderer/src/hooks/agents/types.ts @@ -0,0 +1,4 @@ +export type UpdateAgentBaseOptions = { + /** Whether to show success toast after updating. Defaults to true. */ + showSuccessToast?: boolean +} diff --git a/src/renderer/src/hooks/agents/useActiveAgent.ts b/src/renderer/src/hooks/agents/useActiveAgent.ts new file mode 100644 index 0000000000..f522ac7f28 --- /dev/null +++ b/src/renderer/src/hooks/agents/useActiveAgent.ts @@ -0,0 +1,8 @@ +import { useRuntime } from '../useRuntime' +import { useAgent } from './useAgent' + +export const useActiveAgent = () => { + const { chat } = useRuntime() + const { activeAgentId } = chat + return useAgent(activeAgentId) +} diff --git a/src/renderer/src/hooks/agents/useActiveSession.ts b/src/renderer/src/hooks/agents/useActiveSession.ts new file mode 100644 index 0000000000..dd744b6a50 --- /dev/null +++ b/src/renderer/src/hooks/agents/useActiveSession.ts @@ -0,0 +1,9 @@ +import { useRuntime } from '../useRuntime' +import { useSession } from './useSession' + +export const useActiveSession = () => { + const { chat } = useRuntime() + const { activeSessionIdMap, activeAgentId } = chat + const activeSessionId = activeAgentId ? activeSessionIdMap[activeAgentId] : null + return useSession(activeAgentId, activeSessionId) +} diff --git a/src/renderer/src/hooks/agents/useAgent.ts b/src/renderer/src/hooks/agents/useAgent.ts index a6755a4293..e1c682046f 100644 --- a/src/renderer/src/hooks/agents/useAgent.ts +++ b/src/renderer/src/hooks/agents/useAgent.ts @@ -11,8 +11,8 @@ export const useAgent = (id: string | null) => { const key = id ? client.agentPaths.withId(id) : null const { apiServerConfig, apiServerRunning } = useApiServer() const fetcher = useCallback(async () => { - if (!id || id === 'fake') { - return null + if (!id) { + throw new Error(t('agent.get.error.null_id')) } if (!apiServerConfig.enabled) { throw new Error(t('apiServer.messages.notEnabled')) diff --git a/src/renderer/src/hooks/agents/useAgentSessionInitializer.ts b/src/renderer/src/hooks/agents/useAgentSessionInitializer.ts index 23161b4223..98609f5b29 100644 --- a/src/renderer/src/hooks/agents/useAgentSessionInitializer.ts +++ b/src/renderer/src/hooks/agents/useAgentSessionInitializer.ts @@ -17,7 +17,7 @@ export const useAgentSessionInitializer = () => { const dispatch = useAppDispatch() const client = useAgentClient() const { chat } = useRuntime() - const { activeAgentId, activeSessionId } = chat + const { activeAgentId, activeSessionIdMap } = chat /** * Initialize session for the given agent by loading its sessions @@ -25,11 +25,11 @@ export const useAgentSessionInitializer = () => { */ const initializeAgentSession = useCallback( async (agentId: string) => { - if (!agentId || agentId === 'fake') return + if (!agentId) return try { // Check if this agent already has an active session - const currentSessionId = activeSessionId[agentId] + const currentSessionId = activeSessionIdMap[agentId] if (currentSessionId) { // Session already exists, just switch to session view dispatch(setActiveTopicOrSessionAction('session')) @@ -58,21 +58,21 @@ export const useAgentSessionInitializer = () => { dispatch(setActiveTopicOrSessionAction('session')) } }, - [client, dispatch, activeSessionId] + [client, dispatch, activeSessionIdMap] ) /** * Auto-initialize when activeAgentId changes */ useEffect(() => { - if (activeAgentId && activeAgentId !== 'fake') { + if (activeAgentId) { // Check if we need to initialize this agent's session - const hasActiveSession = activeSessionId[activeAgentId] + const hasActiveSession = activeSessionIdMap[activeAgentId] if (!hasActiveSession) { initializeAgentSession(activeAgentId) } } - }, [activeAgentId, activeSessionId, initializeAgentSession]) + }, [activeAgentId, activeSessionIdMap, initializeAgentSession]) return { initializeAgentSession diff --git a/src/renderer/src/hooks/agents/useSession.ts b/src/renderer/src/hooks/agents/useSession.ts index ded5b9aabe..1c6ebd608f 100644 --- a/src/renderer/src/hooks/agents/useSession.ts +++ b/src/renderer/src/hooks/agents/useSession.ts @@ -1,21 +1,24 @@ import { useAppDispatch } from '@renderer/store' import { loadTopicMessagesThunk } from '@renderer/store/thunk/messageThunk' -import { UpdateSessionForm } from '@renderer/types' import { buildAgentSessionTopicId } from '@renderer/utils/agentSession' -import { useCallback, useEffect, useMemo } from 'react' +import { useEffect, useMemo } from 'react' import { useTranslation } from 'react-i18next' import useSWR from 'swr' import { useAgentClient } from './useAgentClient' +import { useUpdateSession } from './useUpdateSession' -export const useSession = (agentId: string, sessionId: string) => { +export const useSession = (agentId: string | null, sessionId: string | null) => { const { t } = useTranslation() const client = useAgentClient() - const key = client.getSessionPaths(agentId).withId(sessionId) + const key = agentId && sessionId ? client.getSessionPaths(agentId).withId(sessionId) : null const dispatch = useAppDispatch() - const sessionTopicId = useMemo(() => buildAgentSessionTopicId(sessionId), [sessionId]) + const sessionTopicId = useMemo(() => (sessionId ? buildAgentSessionTopicId(sessionId) : null), [sessionId]) + const { updateSession } = useUpdateSession(agentId) const fetcher = async () => { + if (!agentId) throw new Error(t('agent.get.error.null_id')) + if (!sessionId) throw new Error(t('agent.session.get.error.null_id')) const data = await client.getSession(agentId, sessionId) return data } @@ -24,26 +27,13 @@ export const useSession = (agentId: string, sessionId: string) => { // Use loadTopicMessagesThunk to load messages (with caching mechanism) // This ensures messages are preserved when switching between sessions/tabs useEffect(() => { - if (sessionId) { + if (sessionTopicId) { // loadTopicMessagesThunk will check if messages already exist in Redux // and skip loading if they do (unless forceReload is true) dispatch(loadTopicMessagesThunk(sessionTopicId)) } }, [dispatch, sessionId, sessionTopicId]) - const updateSession = useCallback( - async (form: UpdateSessionForm) => { - if (!agentId) return - try { - const result = await client.updateSession(agentId, form) - mutate(result) - } catch (error) { - window.toast.error(t('agent.session.update.error.failed')) - } - }, - [agentId, client, mutate, t] - ) - return { session: data, error, diff --git a/src/renderer/src/hooks/agents/useSessions.ts b/src/renderer/src/hooks/agents/useSessions.ts index fce29b6bf9..2be85d5eba 100644 --- a/src/renderer/src/hooks/agents/useSessions.ts +++ b/src/renderer/src/hooks/agents/useSessions.ts @@ -1,4 +1,4 @@ -import { CreateSessionForm } from '@renderer/types' +import { CreateAgentSessionResponse, CreateSessionForm, GetAgentSessionResponse } from '@renderer/types' import { formatErrorMessageWithPrefix } from '@renderer/utils/error' import { useCallback } from 'react' import { useTranslation } from 'react-i18next' @@ -6,46 +6,50 @@ import useSWR from 'swr' import { useAgentClient } from './useAgentClient' -export const useSessions = (agentId: string) => { +export const useSessions = (agentId: string | null) => { const { t } = useTranslation() const client = useAgentClient() - const key = client.getSessionPaths(agentId).base + const key = agentId ? client.getSessionPaths(agentId).base : null const fetcher = async () => { + if (!agentId) throw new Error('No active agent.') const data = await client.listSessions(agentId) return data.data } const { data, error, isLoading, mutate } = useSWR(key, fetcher) const createSession = useCallback( - async (form: CreateSessionForm) => { + async (form: CreateSessionForm): Promise => { + if (!agentId) return null try { const result = await client.createSession(agentId, form) await mutate((prev) => [result, ...(prev ?? [])], { revalidate: false }) return result } catch (error) { window.toast.error(formatErrorMessageWithPrefix(error, t('agent.session.create.error.failed'))) - return undefined + return null } }, [agentId, client, mutate, t] ) - // TODO: including messages field const getSession = useCallback( - async (id: string) => { + async (id: string): Promise => { + if (!agentId) return null try { const result = await client.getSession(agentId, id) mutate((prev) => prev?.map((session) => (session.id === result.id ? result : session))) + return result } catch (error) { window.toast.error(formatErrorMessageWithPrefix(error, t('agent.session.get.error.failed'))) + return null } }, [agentId, client, mutate, t] ) const deleteSession = useCallback( - async (id: string) => { + async (id: string): Promise => { if (!agentId) return false try { await client.deleteSession(agentId, id) diff --git a/src/renderer/src/hooks/agents/useUpdateAgent.ts b/src/renderer/src/hooks/agents/useUpdateAgent.ts index b2589095ef..db3825b9bb 100644 --- a/src/renderer/src/hooks/agents/useUpdateAgent.ts +++ b/src/renderer/src/hooks/agents/useUpdateAgent.ts @@ -4,20 +4,16 @@ import { useCallback } from 'react' import { useTranslation } from 'react-i18next' import { mutate } from 'swr' +import { UpdateAgentBaseOptions } from './types' import { useAgentClient } from './useAgentClient' -export type UpdateAgentOptions = { - /** Whether to show success toast after updating. Defaults to true. */ - showSuccessToast?: boolean -} - export const useUpdateAgent = () => { const { t } = useTranslation() const client = useAgentClient() const listKey = client.agentPaths.base const updateAgent = useCallback( - async (form: UpdateAgentForm, options?: UpdateAgentOptions) => { + async (form: UpdateAgentForm, options?: UpdateAgentBaseOptions) => { try { const itemKey = client.agentPaths.withId(form.id) // may change to optimistic update @@ -35,7 +31,7 @@ export const useUpdateAgent = () => { ) const updateModel = useCallback( - async (agentId: string, modelId: string, options?: UpdateAgentOptions) => { + async (agentId: string, modelId: string, options?: UpdateAgentBaseOptions) => { updateAgent({ id: agentId, model: modelId }, options) }, [updateAgent] diff --git a/src/renderer/src/hooks/agents/useUpdateSession.ts b/src/renderer/src/hooks/agents/useUpdateSession.ts index 4e91c2fda9..cf52a64630 100644 --- a/src/renderer/src/hooks/agents/useUpdateSession.ts +++ b/src/renderer/src/hooks/agents/useUpdateSession.ts @@ -1,19 +1,21 @@ import { ListAgentSessionsResponse, UpdateSessionForm } from '@renderer/types' -import { formatErrorMessageWithPrefix } from '@renderer/utils/error' +import { getErrorMessage } from '@renderer/utils/error' import { useCallback } from 'react' import { useTranslation } from 'react-i18next' import { mutate } from 'swr' +import { UpdateAgentBaseOptions } from './types' import { useAgentClient } from './useAgentClient' -export const useUpdateSession = (agentId: string) => { +export const useUpdateSession = (agentId: string | null) => { const { t } = useTranslation() const client = useAgentClient() - const paths = client.getSessionPaths(agentId) - const listKey = paths.base const updateSession = useCallback( - async (form: UpdateSessionForm) => { + async (form: UpdateSessionForm, options?: UpdateAgentBaseOptions) => { + if (!agentId) return + const paths = client.getSessionPaths(agentId) + const listKey = paths.base const sessionId = form.id try { const itemKey = paths.withId(sessionId) @@ -24,13 +26,29 @@ export const useUpdateSession = (agentId: string) => { (prev) => prev?.map((session) => (session.id === result.id ? result : session)) ?? [] ) mutate(itemKey, result) - window.toast.success(t('common.update_success')) + if (options?.showSuccessToast ?? true) { + window.toast.success(t('common.update_success')) + } } catch (error) { - window.toast.error(formatErrorMessageWithPrefix(error, t('agent.update.error.failed'))) + window.toast.error({ title: t('agent.session.update.error.failed'), description: getErrorMessage(error) }) } }, - [agentId, client, listKey, paths, t] + [agentId, client, t] ) - return updateSession + const updateModel = useCallback( + async (sessionId: string, modelId: string, options?: UpdateAgentBaseOptions) => { + if (!agentId) return + return updateSession( + { + id: sessionId, + model: modelId + }, + options + ) + }, + [agentId, updateSession] + ) + + return { updateSession, updateModel } } diff --git a/src/renderer/src/i18n/locales/en-us.json b/src/renderer/src/i18n/locales/en-us.json index 98cce3a51e..9dbdd32dae 100644 --- a/src/renderer/src/i18n/locales/en-us.json +++ b/src/renderer/src/i18n/locales/en-us.json @@ -22,7 +22,8 @@ }, "get": { "error": { - "failed": "Failed to get the agent." + "failed": "Failed to get the agent.", + "null_id": "Agent ID is null." } }, "list": { @@ -73,7 +74,8 @@ }, "get": { "error": { - "failed": "Failed to get the session" + "failed": "Failed to get the session", + "null_id": "Session ID is null" } }, "label_one": "Session", diff --git a/src/renderer/src/i18n/locales/zh-cn.json b/src/renderer/src/i18n/locales/zh-cn.json index 8e5e4ebe38..9b146596fc 100644 --- a/src/renderer/src/i18n/locales/zh-cn.json +++ b/src/renderer/src/i18n/locales/zh-cn.json @@ -22,7 +22,8 @@ }, "get": { "error": { - "failed": "获取智能体失败" + "failed": "获取智能体失败", + "null_id": "智能体 ID 为空。" } }, "list": { @@ -73,7 +74,8 @@ }, "get": { "error": { - "failed": "获取会话失败" + "failed": "获取会话失败", + "null_id": "会话 ID 为空" } }, "label_one": "会话", diff --git a/src/renderer/src/i18n/locales/zh-tw.json b/src/renderer/src/i18n/locales/zh-tw.json index 0b88424662..57dc3480b5 100644 --- a/src/renderer/src/i18n/locales/zh-tw.json +++ b/src/renderer/src/i18n/locales/zh-tw.json @@ -22,7 +22,8 @@ }, "get": { "error": { - "failed": "無法取得代理程式。" + "failed": "無法取得代理程式。", + "null_id": "代理程式 ID 為空。" } }, "list": { @@ -73,7 +74,8 @@ }, "get": { "error": { - "failed": "無法取得工作階段" + "failed": "無法取得工作階段", + "null_id": "工作階段 ID 為空" } }, "label_one": "會議", diff --git a/src/renderer/src/i18n/translate/el-gr.json b/src/renderer/src/i18n/translate/el-gr.json index c1d289316d..259d64c910 100644 --- a/src/renderer/src/i18n/translate/el-gr.json +++ b/src/renderer/src/i18n/translate/el-gr.json @@ -22,7 +22,8 @@ }, "get": { "error": { - "failed": "Αποτυχία λήψης του πράκτορα." + "failed": "Αποτυχία λήψης του πράκτορα.", + "null_id": "Το ID του πράκτορα είναι null." } }, "list": { @@ -73,7 +74,8 @@ }, "get": { "error": { - "failed": "Αποτυχία λήψης της συνεδρίας" + "failed": "Αποτυχία λήψης της συνεδρίας", + "null_id": "Το ID της συνεδρίας είναι null" } }, "label_one": "Συνεδρία", diff --git a/src/renderer/src/i18n/translate/es-es.json b/src/renderer/src/i18n/translate/es-es.json index 5825c72fd2..8b9b863e04 100644 --- a/src/renderer/src/i18n/translate/es-es.json +++ b/src/renderer/src/i18n/translate/es-es.json @@ -22,7 +22,8 @@ }, "get": { "error": { - "failed": "No se pudo obtener el agente." + "failed": "No se pudo obtener el agente.", + "null_id": "El ID del agente es nulo." } }, "list": { @@ -73,7 +74,8 @@ }, "get": { "error": { - "failed": "Error al obtener la sesión" + "failed": "Error al obtener la sesión", + "null_id": "El ID de sesión es nulo" } }, "label_one": "Sesión", diff --git a/src/renderer/src/i18n/translate/fr-fr.json b/src/renderer/src/i18n/translate/fr-fr.json index e82bbe99e8..f40ffc8163 100644 --- a/src/renderer/src/i18n/translate/fr-fr.json +++ b/src/renderer/src/i18n/translate/fr-fr.json @@ -22,7 +22,8 @@ }, "get": { "error": { - "failed": "Échec de l'obtention de l'agent." + "failed": "Échec de l'obtention de l'agent.", + "null_id": "L'ID de l'agent est nul." } }, "list": { @@ -73,7 +74,8 @@ }, "get": { "error": { - "failed": "Échec de l'obtention de la session" + "failed": "Échec de l'obtention de la session", + "null_id": "L'ID de session est nul" } }, "label_one": "Session", diff --git a/src/renderer/src/i18n/translate/ja-jp.json b/src/renderer/src/i18n/translate/ja-jp.json index 5600675de2..03c5218c31 100644 --- a/src/renderer/src/i18n/translate/ja-jp.json +++ b/src/renderer/src/i18n/translate/ja-jp.json @@ -22,7 +22,8 @@ }, "get": { "error": { - "failed": "エージェントの取得に失敗しました。" + "failed": "エージェントの取得に失敗しました。", + "null_id": "エージェント ID が null です。" } }, "list": { @@ -73,7 +74,8 @@ }, "get": { "error": { - "failed": "セッションの取得に失敗しました" + "failed": "セッションの取得に失敗しました", + "null_id": "セッション ID が null です" } }, "label_one": "セッション", diff --git a/src/renderer/src/i18n/translate/pt-pt.json b/src/renderer/src/i18n/translate/pt-pt.json index c732b90f1a..4ea6d25f56 100644 --- a/src/renderer/src/i18n/translate/pt-pt.json +++ b/src/renderer/src/i18n/translate/pt-pt.json @@ -22,7 +22,8 @@ }, "get": { "error": { - "failed": "Falha ao obter o agente." + "failed": "Falha ao obter o agente.", + "null_id": "O ID do agente é nulo." } }, "list": { @@ -73,7 +74,8 @@ }, "get": { "error": { - "failed": "Falha ao obter a sessão" + "failed": "Falha ao obter a sessão", + "null_id": "O ID da sessão é nulo" } }, "label_one": "Sessão", diff --git a/src/renderer/src/i18n/translate/ru-ru.json b/src/renderer/src/i18n/translate/ru-ru.json index 394cc9163a..8f751d8d9d 100644 --- a/src/renderer/src/i18n/translate/ru-ru.json +++ b/src/renderer/src/i18n/translate/ru-ru.json @@ -22,7 +22,8 @@ }, "get": { "error": { - "failed": "Не удалось получить агента." + "failed": "Не удалось получить агента.", + "null_id": "ID агента равен null." } }, "list": { @@ -73,7 +74,8 @@ }, "get": { "error": { - "failed": "Не удалось получить сеанс" + "failed": "Не удалось получить сеанс", + "null_id": "ID сессии равен null" } }, "label_one": "Сессия", diff --git a/src/renderer/src/pages/home/Chat.tsx b/src/renderer/src/pages/home/Chat.tsx index 12ba9d7573..0775a4f718 100644 --- a/src/renderer/src/pages/home/Chat.tsx +++ b/src/renderer/src/pages/home/Chat.tsx @@ -49,7 +49,8 @@ const Chat: FC = (props) => { const { isTopNavbar } = useNavbarPosition() const chatMaxWidth = useChatMaxWidth() const { chat } = useRuntime() - const { activeTopicOrSession, activeAgentId, activeSessionId } = chat + const { activeTopicOrSession, activeAgentId, activeSessionIdMap } = chat + const activeSessionId = activeAgentId ? activeSessionIdMap[activeAgentId] : null const { apiServer } = useSettings() const mainRef = React.useRef(null) @@ -147,8 +148,7 @@ const Chat: FC = (props) => { if (activeAgentId === null) { return () =>
      Active Agent ID is invalid.
      } - const sessionId = activeSessionId[activeAgentId] - if (!sessionId) { + if (!activeSessionId) { return () =>
      Active Session ID is invalid.
      } if (!apiServer.enabled) { @@ -158,18 +158,17 @@ const Chat: FC = (props) => {
      ) } - return () => + return () => }, [activeAgentId, activeSessionId, apiServer.enabled, t]) const SessionInputBar = useMemo(() => { if (activeAgentId === null) { return () =>
      Active Agent ID is invalid.
      } - const sessionId = activeSessionId[activeAgentId] - if (!sessionId) { + if (!activeSessionId) { return () =>
      Active Session ID is invalid.
      } - return () => + return () => }, [activeAgentId, activeSessionId]) // TODO: more info @@ -235,10 +234,8 @@ const Chat: FC = (props) => { )} {activeTopicOrSession === 'session' && !activeAgentId && } - {activeTopicOrSession === 'session' && activeAgentId && !activeSessionId[activeAgentId] && ( - - )} - {activeTopicOrSession === 'session' && activeAgentId && activeSessionId[activeAgentId] && ( + {activeTopicOrSession === 'session' && activeAgentId && !activeSessionId && } + {activeTopicOrSession === 'session' && activeAgentId && activeSessionId && ( <> diff --git a/src/renderer/src/pages/home/ChatNavbar.tsx b/src/renderer/src/pages/home/ChatNavbar.tsx index 0934754139..c44447da8f 100644 --- a/src/renderer/src/pages/home/ChatNavbar.tsx +++ b/src/renderer/src/pages/home/ChatNavbar.tsx @@ -64,6 +64,14 @@ const HeaderNavbar: FC = ({ activeAssistant, setActiveAssistant, activeTo }) } + // const handleUpdateModel = useCallback( + // async (model: ApiModel) => { + // if (!activeSession || !activeAgent) return + // return updateModel(activeSession.id, model.id, { showSuccessToast: false }) + // }, + // [activeAgent, activeSession, updateModel] + // ) + return (
      diff --git a/src/renderer/src/pages/home/HomePage.tsx b/src/renderer/src/pages/home/HomePage.tsx index 32f1ec3975..d387af12ba 100644 --- a/src/renderer/src/pages/home/HomePage.tsx +++ b/src/renderer/src/pages/home/HomePage.tsx @@ -117,7 +117,7 @@ const HomePage: FC = () => { type: 'chat' }) } else if (activeTopicOrSession === 'topic') { - dispatch(setActiveAgentId('fake')) + dispatch(setActiveAgentId(null)) } }, [activeTopicOrSession, dispatch, setActiveAssistant]) diff --git a/src/renderer/src/pages/home/Tabs/AgentSettingsTab.tsx b/src/renderer/src/pages/home/Tabs/AgentSettingsTab.tsx deleted file mode 100644 index 7aab18816d..0000000000 --- a/src/renderer/src/pages/home/Tabs/AgentSettingsTab.tsx +++ /dev/null @@ -1,40 +0,0 @@ -import { Button, Divider } from '@heroui/react' -import { useUpdateAgent } from '@renderer/hooks/agents/useUpdateAgent' -import { AgentSettingsPopup } from '@renderer/pages/settings/AgentSettings' -import AdvancedSettings from '@renderer/pages/settings/AgentSettings/AdvancedSettings' -import AgentEssentialSettings from '@renderer/pages/settings/AgentSettings/AgentEssentialSettings' -import { GetAgentResponse } from '@renderer/types/agent' -import { FC } from 'react' -import { useTranslation } from 'react-i18next' - -interface Props { - agent: GetAgentResponse | undefined | null - update: ReturnType['updateAgent'] -} - -const AgentSettingsTab: FC = ({ agent, update }) => { - const { t } = useTranslation() - - const onMoreSetting = () => { - if (agent?.id) { - AgentSettingsPopup.show({ agentId: agent.id! }) - } - } - - if (!agent) { - return null - } - - return ( -
      - - - - -
      - ) -} - -export default AgentSettingsTab diff --git a/src/renderer/src/pages/home/Tabs/AssistantsTab.tsx b/src/renderer/src/pages/home/Tabs/AssistantsTab.tsx index f4588f60a0..af02796d76 100644 --- a/src/renderer/src/pages/home/Tabs/AssistantsTab.tsx +++ b/src/renderer/src/pages/home/Tabs/AssistantsTab.tsx @@ -11,7 +11,7 @@ import { useAppDispatch } from '@renderer/store' import { addIknowAction } from '@renderer/store/runtime' import { Assistant, AssistantsSortType } from '@renderer/types' import { getErrorMessage } from '@renderer/utils' -import { FC, useCallback, useEffect, useRef, useState } from 'react' +import { FC, useCallback, useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import styled from 'styled-components' @@ -80,12 +80,6 @@ const AssistantsTab: FC = (props) => { updateAssistants }) - useEffect(() => { - if (!agentsLoading && agents.length > 0 && !activeAgentId && apiServerConfig.enabled) { - setActiveAgentId(agents[0].id) - } - }, [agentsLoading, agents, activeAgentId, setActiveAgentId, apiServerConfig.enabled]) - const onDeleteAssistant = useCallback( (assistant: Assistant) => { const remaining = assistants.filter((a) => a.id !== assistant.id) diff --git a/src/renderer/src/pages/home/Tabs/SessionSettingsTab.tsx b/src/renderer/src/pages/home/Tabs/SessionSettingsTab.tsx new file mode 100644 index 0000000000..34b41a560c --- /dev/null +++ b/src/renderer/src/pages/home/Tabs/SessionSettingsTab.tsx @@ -0,0 +1,43 @@ +import { Button, Divider } from '@heroui/react' +import { useUpdateSession } from '@renderer/hooks/agents/useUpdateSession' +import { SessionSettingsPopup } from '@renderer/pages/settings/AgentSettings' +import AdvancedSettings from '@renderer/pages/settings/AgentSettings/AdvancedSettings' +import EssentialSettings from '@renderer/pages/settings/AgentSettings/EssentialSettings' +import { GetAgentSessionResponse } from '@renderer/types' +import { FC } from 'react' +import { useTranslation } from 'react-i18next' + +interface Props { + session: GetAgentSessionResponse | undefined | null + update: ReturnType['updateSession'] +} + +const SessionSettingsTab: FC = ({ session, update }) => { + const { t } = useTranslation() + + const onMoreSetting = () => { + if (session?.id) { + SessionSettingsPopup.show({ + agentId: session.agent_id, + sessionId: session.id + }) + } + } + + if (!session) { + return null + } + + return ( +
      + + + + +
      + ) +} + +export default SessionSettingsTab diff --git a/src/renderer/src/pages/home/Tabs/components/Agents.tsx b/src/renderer/src/pages/home/Tabs/components/Agents.tsx deleted file mode 100644 index 07154aed41..0000000000 --- a/src/renderer/src/pages/home/Tabs/components/Agents.tsx +++ /dev/null @@ -1,71 +0,0 @@ -import { Alert, Button, Spinner } from '@heroui/react' -import { AgentModal } from '@renderer/components/Popups/agent/AgentModal' -import { useAgents } from '@renderer/hooks/agents/useAgents' -import { useAgentSessionInitializer } from '@renderer/hooks/agents/useAgentSessionInitializer' -import { useRuntime } from '@renderer/hooks/useRuntime' -import { useAppDispatch } from '@renderer/store' -import { setActiveAgentId as setActiveAgentIdAction } from '@renderer/store/runtime' -import { Plus } from 'lucide-react' -import { FC, useCallback, useEffect } from 'react' -import { useTranslation } from 'react-i18next' - -import AgentItem from './AgentItem' - -interface AssistantsTabProps {} - -export const Agents: FC = () => { - const { agents, deleteAgent, isLoading, error } = useAgents() - const { t } = useTranslation() - const { chat } = useRuntime() - const { activeAgentId } = chat - const { initializeAgentSession } = useAgentSessionInitializer() - - const dispatch = useAppDispatch() - - const setActiveAgentId = useCallback( - async (id: string) => { - dispatch(setActiveAgentIdAction(id)) - // Initialize the session for this agent - await initializeAgentSession(id) - }, - [dispatch, initializeAgentSession] - ) - - useEffect(() => { - if (!isLoading && agents.length > 0 && !activeAgentId) { - setActiveAgentId(agents[0].id) - } - }, [isLoading, agents, activeAgentId, setActiveAgentId]) - - return ( - <> - {isLoading && } - {error && } - {!isLoading && - !error && - agents.map((agent) => ( - deleteAgent(agent.id)} - onPress={() => { - setActiveAgentId(agent.id) - }} - /> - ))} - e.continuePropagation()} - startContent={} - className="w-full justify-start bg-transparent text-foreground-500 hover:bg-[var(--color-list-item)]"> - {t('agent.add.title')} - - ) - }} - /> - - ) -} diff --git a/src/renderer/src/pages/home/Tabs/components/Assistants.tsx b/src/renderer/src/pages/home/Tabs/components/Assistants.tsx deleted file mode 100644 index c21ec92aeb..0000000000 --- a/src/renderer/src/pages/home/Tabs/components/Assistants.tsx +++ /dev/null @@ -1,208 +0,0 @@ -import { DownOutlined, RightOutlined } from '@ant-design/icons' -import { Button } from '@heroui/react' -import { DraggableList } from '@renderer/components/DraggableList' -import { useAssistants } from '@renderer/hooks/useAssistant' -import { useAssistantPresets } from '@renderer/hooks/useAssistantPresets' -import { useAssistantsTabSortType } from '@renderer/hooks/useStore' -import { useTags } from '@renderer/hooks/useTags' -import { Assistant, AssistantsSortType } from '@renderer/types' -import { Tooltip } from 'antd' -import { Plus } from 'lucide-react' -import { FC, useCallback, useMemo, useState } from 'react' -import { useTranslation } from 'react-i18next' -import styled from 'styled-components' - -import AssistantItem from './AssistantItem' -import { SectionName } from './SectionName' - -interface AssistantsProps { - activeAssistant: Assistant - setActiveAssistant: (assistant: Assistant) => void - onCreateAssistant: () => void - onCreateDefaultAssistant: () => void -} - -const Assistants: FC = ({ - activeAssistant, - setActiveAssistant, - onCreateAssistant, - onCreateDefaultAssistant -}) => { - const { assistants, removeAssistant, copyAssistant, updateAssistants } = useAssistants() - const [dragging, setDragging] = useState(false) - const { addAssistantPreset } = useAssistantPresets() - const { t } = useTranslation() - const { getGroupedAssistants, collapsedTags, toggleTagCollapse } = useTags() - const { assistantsTabSortType = 'list', setAssistantsTabSortType } = useAssistantsTabSortType() - - const onDelete = useCallback( - (assistant: Assistant) => { - const remaining = assistants.filter((a) => a.id !== assistant.id) - if (assistant.id === activeAssistant?.id) { - const newActive = remaining[remaining.length - 1] - newActive ? setActiveAssistant(newActive) : onCreateDefaultAssistant() - } - removeAssistant(assistant.id) - }, - [activeAssistant, assistants, removeAssistant, setActiveAssistant, onCreateDefaultAssistant] - ) - - const handleSortByChange = useCallback( - (sortType: AssistantsSortType) => { - setAssistantsTabSortType(sortType) - }, - [setAssistantsTabSortType] - ) - - const handleGroupReorder = useCallback( - (tag: string, newGroupList: Assistant[]) => { - let insertIndex = 0 - const newGlobal = assistants.map((a) => { - const tags = a.tags?.length ? a.tags : [t('assistants.tags.untagged')] - if (tags.includes(tag)) { - const replaced = newGroupList[insertIndex] - insertIndex += 1 - return replaced - } - return a - }) - updateAssistants(newGlobal) - }, - [assistants, t, updateAssistants] - ) - - const renderAddAssistantButton = useMemo(() => { - return ( - - ) - }, [onCreateAssistant, t]) - - if (assistantsTabSortType === 'tags') { - return ( - <> - -
      - {getGroupedAssistants.map((group) => ( - - {group.tag !== t('assistants.tags.untagged') && ( - toggleTagCollapse(group.tag)}> - - - {collapsedTags[group.tag] ? ( - - ) : ( - - )} - {group.tag} - - - - - )} - {!collapsedTags[group.tag] && ( -
      - handleGroupReorder(group.tag, newList)} - onDragStart={() => setDragging(true)} - onDragEnd={() => setDragging(false)}> - {(assistant) => ( - - )} - -
      - )} -
      - ))} - {renderAddAssistantButton} -
      - - ) - } - - return ( -
      - - setDragging(true)} - onDragEnd={() => setDragging(false)}> - {(assistant) => ( - - )} - - {!dragging && renderAddAssistantButton} -
      -
      - ) -} - -// 样式组件 - -const TagsContainer = styled.div` - display: flex; - flex-direction: column; - gap: 8px; -` - -const GroupTitle = styled.div` - color: var(--color-text-2); - font-size: 12px; - font-weight: 500; - cursor: pointer; - display: flex; - flex-direction: row; - justify-content: space-between; - align-items: center; - height: 24px; - margin: 5px 0; -` - -const GroupTitleName = styled.div` - max-width: 50%; - text-overflow: ellipsis; - white-space: nowrap; - overflow: hidden; - box-sizing: border-box; - padding: 0 4px; - color: var(--color-text); - font-size: 13px; - line-height: 24px; - margin-right: 5px; - display: flex; -` - -const GroupTitleDivider = styled.div` - flex: 1; - border-top: 1px solid var(--color-border); -` - -export default Assistants diff --git a/src/renderer/src/pages/home/Tabs/components/SessionItem.tsx b/src/renderer/src/pages/home/Tabs/components/SessionItem.tsx index 31d05aff44..9e16b1de64 100644 --- a/src/renderer/src/pages/home/Tabs/components/SessionItem.tsx +++ b/src/renderer/src/pages/home/Tabs/components/SessionItem.tsx @@ -33,8 +33,8 @@ interface SessionItemProps { const SessionItem: FC = ({ session, agentId, isDisabled, isLoading, onDelete, onPress }) => { const { t } = useTranslation() const { chat } = useRuntime() - const updateSession = useUpdateSession(agentId) - const activeSessionId = chat.activeSessionId[agentId] + const { updateSession } = useUpdateSession(agentId) + const activeSessionId = chat.activeSessionIdMap[agentId] const [isConfirmingDeletion, setIsConfirmingDeletion] = useState(false) const { setTimeoutTimer } = useTimer() const dispatch = useAppDispatch() diff --git a/src/renderer/src/pages/home/Tabs/components/Sessions.tsx b/src/renderer/src/pages/home/Tabs/components/Sessions.tsx index 8f7d9aa941..50020b168d 100644 --- a/src/renderer/src/pages/home/Tabs/components/Sessions.tsx +++ b/src/renderer/src/pages/home/Tabs/components/Sessions.tsx @@ -30,7 +30,7 @@ const Sessions: React.FC = ({ agentId }) => { const { agent } = useAgent(agentId) const { sessions, isLoading, error, deleteSession, createSession } = useSessions(agentId) const { chat } = useRuntime() - const { activeSessionId, sessionWaiting } = chat + const { activeSessionIdMap, sessionWaiting } = chat const dispatch = useAppDispatch() const setActiveSessionId = useCallback( @@ -75,24 +75,24 @@ const Sessions: React.FC = ({ agentId }) => { [agentId, deleteSession, dispatch, sessions, t] ) - const currentActiveSessionId = activeSessionId[agentId] + const activeSessionId = activeSessionIdMap[agentId] useEffect(() => { - if (!isLoading && sessions.length > 0 && !currentActiveSessionId) { + if (!isLoading && sessions.length > 0 && !activeSessionId) { setActiveSessionId(agentId, sessions[0].id) } - }, [isLoading, sessions, currentActiveSessionId, agentId, setActiveSessionId]) + }, [isLoading, sessions, activeSessionId, agentId, setActiveSessionId]) useEffect(() => { - if (currentActiveSessionId) { + if (activeSessionId) { dispatch( newMessagesActions.setTopicFulfilled({ - topicId: buildAgentSessionTopicId(currentActiveSessionId), + topicId: buildAgentSessionTopicId(activeSessionId), fulfilled: false }) ) } - }, [currentActiveSessionId, dispatch]) + }, [activeSessionId, dispatch]) if (isLoading) { return ( diff --git a/src/renderer/src/pages/home/Tabs/index.tsx b/src/renderer/src/pages/home/Tabs/index.tsx index 1e2559c086..c43c6d93a4 100644 --- a/src/renderer/src/pages/home/Tabs/index.tsx +++ b/src/renderer/src/pages/home/Tabs/index.tsx @@ -1,6 +1,7 @@ +import { Alert, Skeleton } from '@heroui/react' import AddAssistantPopup from '@renderer/components/Popups/AddAssistantPopup' -import { useAgent } from '@renderer/hooks/agents/useAgent' -import { useUpdateAgent } from '@renderer/hooks/agents/useUpdateAgent' +import { useActiveSession } from '@renderer/hooks/agents/useActiveSession' +import { useUpdateSession } from '@renderer/hooks/agents/useUpdateSession' import { useAssistants, useDefaultAssistant } from '@renderer/hooks/useAssistant' import { useRuntime } from '@renderer/hooks/useRuntime' import { useNavbarPosition, useSettings } from '@renderer/hooks/useSettings' @@ -8,13 +9,13 @@ import { useShowTopics } from '@renderer/hooks/useStore' import { EVENT_NAMES, EventEmitter } from '@renderer/services/EventService' import { Assistant, Topic } from '@renderer/types' import { Tab } from '@renderer/types/chat' -import { classNames, uuid } from '@renderer/utils' +import { classNames, getErrorMessage, uuid } from '@renderer/utils' import { FC, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import styled from 'styled-components' -import AgentSettingsTab from './AgentSettingsTab' import Assistants from './AssistantsTab' +import SessionSettingsTab from './SessionSettingsTab' import Settings from './SettingsTab' import Topics from './TopicsTab' @@ -47,8 +48,8 @@ const HomeTabs: FC = ({ const { t } = useTranslation() const { chat } = useRuntime() const { activeTopicOrSession, activeAgentId } = chat - const { agent } = useAgent(activeAgentId) - const { updateAgent } = useUpdateAgent() + const { session, isLoading: isSessionLoading, error: sessionError } = useActiveSession() + const { updateSession } = useUpdateSession(activeAgentId) const isSessionView = activeTopicOrSession === 'session' const isTopicView = activeTopicOrSession === 'topic' @@ -125,7 +126,7 @@ const HomeTabs: FC = ({ )} - {position === 'right' && topicPosition === 'right' && isTopicView && ( + {position === 'right' && topicPosition === 'right' && ( setTab('topic')}> {t('common.topics')} @@ -154,7 +155,20 @@ const HomeTabs: FC = ({ /> )} {tab === 'settings' && isTopicView && } - {tab === 'settings' && isSessionView && } + {tab === 'settings' && isSessionView && !sessionError && ( + + + + )} + {tab === 'settings' && isSessionView && sessionError && ( +
      + +
      + )} ) diff --git a/src/renderer/src/pages/home/components/ChatNavbarContent.tsx b/src/renderer/src/pages/home/components/ChatNavbarContent.tsx index 5d89bfd94e..154226973e 100644 --- a/src/renderer/src/pages/home/components/ChatNavbarContent.tsx +++ b/src/renderer/src/pages/home/components/ChatNavbarContent.tsx @@ -1,18 +1,18 @@ import { BreadcrumbItem, Breadcrumbs, Chip, cn } from '@heroui/react' import HorizontalScrollContainer from '@renderer/components/HorizontalScrollContainer' import { permissionModeCards } from '@renderer/constants/permissionModes' -import { useAgent } from '@renderer/hooks/agents/useAgent' -import { useSession } from '@renderer/hooks/agents/useSession' -import { useUpdateAgent } from '@renderer/hooks/agents/useUpdateAgent' +import { useActiveAgent } from '@renderer/hooks/agents/useActiveAgent' +import { useActiveSession } from '@renderer/hooks/agents/useActiveSession' +import { useUpdateSession } from '@renderer/hooks/agents/useUpdateSession' import { useRuntime } from '@renderer/hooks/useRuntime' -import { ApiModel, Assistant, PermissionMode } from '@renderer/types' +import { AgentEntity, AgentSessionEntity, ApiModel, Assistant, PermissionMode } from '@renderer/types' import { formatErrorMessageWithPrefix } from '@renderer/utils/error' import { t } from 'i18next' import { FC, ReactNode, useCallback } from 'react' -import { AgentSettingsPopup } from '../../settings/AgentSettings' -import { AgentLabel } from '../../settings/AgentSettings/shared' -import SelectAgentModelButton from './SelectAgentModelButton' +import { AgentSettingsPopup, SessionSettingsPopup } from '../../settings/AgentSettings' +import { AgentLabel, SessionLabel } from '../../settings/AgentSettings/shared' +import SelectAgentBaseModelButton from './SelectAgentBaseModelButton' import SelectModelButton from './SelectModelButton' interface Props { @@ -21,41 +21,67 @@ interface Props { const ChatNavbarContent: FC = ({ assistant }) => { const { chat } = useRuntime() - const { activeTopicOrSession, activeAgentId } = chat - const sessionId = activeAgentId ? (chat.activeSessionId[activeAgentId] ?? null) : null - const { agent } = useAgent(activeAgentId) - const { updateModel } = useUpdateAgent() + const { activeTopicOrSession } = chat + const { agent: activeAgent } = useActiveAgent() + const { session: activeSession } = useActiveSession() + const { updateModel } = useUpdateSession(activeAgent?.id ?? null) const handleUpdateModel = useCallback( async (model: ApiModel) => { - if (!agent) return - return updateModel(agent.id, model.id, { showSuccessToast: false }) + if (!activeAgent || !activeSession) return + return updateModel(activeSession.id, model.id, { showSuccessToast: false }) }, - [agent, updateModel] + [activeAgent, activeSession, updateModel] ) return ( <> {activeTopicOrSession === 'topic' && } - {activeTopicOrSession === 'session' && agent && ( + {activeTopicOrSession === 'session' && activeAgent && ( - + AgentSettingsPopup.show({ agentId: agent.id })} - classNames={{ base: 'self-stretch', item: 'h-full' }}> + onPress={() => AgentSettingsPopup.show({ agentId: activeAgent.id })} + classNames={{ + base: 'self-stretch', + item: 'h-full' + }}> - - - - {activeAgentId && sessionId && ( + {activeSession && ( + + SessionSettingsPopup.show({ + agentId: activeAgent.id, + sessionId: activeSession.id + }) + } + classNames={{ + base: 'self-stretch', + item: 'h-full' + }}> + + + + + )} + {activeSession && ( - + + + )} + {activeAgent && activeSession && ( + + )} @@ -65,9 +91,7 @@ const ChatNavbarContent: FC = ({ assistant }) => { ) } -const SessionWorkspaceMeta: FC<{ agentId: string; sessionId: string }> = ({ agentId, sessionId }) => { - const { agent } = useAgent(agentId) - const { session } = useSession(agentId, sessionId) +const SessionWorkspaceMeta: FC<{ agent: AgentEntity; session: AgentSessionEntity }> = ({ agent, session }) => { if (!session || !agent) { return null } diff --git a/src/renderer/src/pages/home/components/SelectAgentModelButton.tsx b/src/renderer/src/pages/home/components/SelectAgentBaseModelButton.tsx similarity index 87% rename from src/renderer/src/pages/home/components/SelectAgentModelButton.tsx rename to src/renderer/src/pages/home/components/SelectAgentBaseModelButton.tsx index 53607cd1d1..7e21d1b47a 100644 --- a/src/renderer/src/pages/home/components/SelectAgentModelButton.tsx +++ b/src/renderer/src/pages/home/components/SelectAgentBaseModelButton.tsx @@ -12,12 +12,12 @@ import { FC } from 'react' import { useTranslation } from 'react-i18next' interface Props { - agent: AgentBaseWithId + agentBase: AgentBaseWithId onSelect: (model: ApiModel) => Promise isDisabled?: boolean } -const SelectAgentModelButton: FC = ({ agent, onSelect, isDisabled }) => { +const SelectAgentBaseModelButton: FC = ({ agentBase: agent, onSelect, isDisabled }) => { const { t } = useTranslation() const model = useApiModel({ id: agent?.model }) @@ -42,9 +42,9 @@ const SelectAgentModelButton: FC = ({ agent, onSelect, isDisabled }) => { className="nodrag rounded-2xl px-1 py-3" onPress={onSelectModel} isDisabled={isDisabled}> -
      +
      - + {model ? model.name : t('button.select_model')} {providerName ? ' | ' + providerName : ''}
      @@ -53,4 +53,4 @@ const SelectAgentModelButton: FC = ({ agent, onSelect, isDisabled }) => { ) } -export default SelectAgentModelButton +export default SelectAgentBaseModelButton diff --git a/src/renderer/src/pages/settings/AgentSettings/AdvancedSettings.tsx b/src/renderer/src/pages/settings/AgentSettings/AdvancedSettings.tsx index d49dac8bcb..209258ce67 100644 --- a/src/renderer/src/pages/settings/AgentSettings/AdvancedSettings.tsx +++ b/src/renderer/src/pages/settings/AgentSettings/AdvancedSettings.tsx @@ -23,7 +23,7 @@ type AdvancedSettingsProps = } | { agentBase: GetAgentSessionResponse | undefined | null - update: ReturnType + update: ReturnType['updateSession'] } const defaultConfiguration: AgentConfigurationState = AgentConfigurationSchema.parse({}) diff --git a/src/renderer/src/pages/settings/AgentSettings/AgentEssentialSettings.tsx b/src/renderer/src/pages/settings/AgentSettings/AgentEssentialSettings.tsx deleted file mode 100644 index e0329404d8..0000000000 --- a/src/renderer/src/pages/settings/AgentSettings/AgentEssentialSettings.tsx +++ /dev/null @@ -1,47 +0,0 @@ -import { Avatar } from '@heroui/react' -import { getAgentTypeAvatar } from '@renderer/config/agent' -import { useUpdateAgent } from '@renderer/hooks/agents/useUpdateAgent' -import { getAgentTypeLabel } from '@renderer/i18n/label' -import { GetAgentResponse } from '@renderer/types' -import { FC } from 'react' -import { useTranslation } from 'react-i18next' - -import { AccessibleDirsSetting } from './AccessibleDirsSetting' -import { AvatarSetting } from './AvatarSetting' -import { DescriptionSetting } from './DescriptionSetting' -import { ModelSetting } from './ModelSetting' -import { NameSetting } from './NameSetting' -import { SettingsContainer, SettingsItem, SettingsTitle } from './shared' - -// const logger = loggerService.withContext('AgentEssentialSettings') - -interface AgentEssentialSettingsProps { - agent: GetAgentResponse | undefined | null - update: ReturnType['updateAgent'] - showModelSetting?: boolean -} - -const AgentEssentialSettings: FC = ({ agent, update, showModelSetting = true }) => { - const { t } = useTranslation() - - if (!agent) return null - - return ( - - - {t('agent.type.label')} -
      - - {(agent?.name ?? agent?.type) ? getAgentTypeLabel(agent.type) : ''} -
      -
      - - - {showModelSetting && } - - -
      - ) -} - -export default AgentEssentialSettings diff --git a/src/renderer/src/pages/settings/AgentSettings/AgentSettingsPopup.tsx b/src/renderer/src/pages/settings/AgentSettings/AgentSettingsPopup.tsx index 400504f8bb..937b72ecaa 100644 --- a/src/renderer/src/pages/settings/AgentSettings/AgentSettingsPopup.tsx +++ b/src/renderer/src/pages/settings/AgentSettings/AgentSettingsPopup.tsx @@ -6,7 +6,7 @@ import { useState } from 'react' import { useTranslation } from 'react-i18next' import AdvancedSettings from './AdvancedSettings' -import AgentEssentialSettings from './AgentEssentialSettings' +import EssentialSettings from './EssentialSettings' import PromptSettings from './PromptSettings' import { AgentLabel, LeftMenu, Settings, StyledMenu, StyledModal } from './shared' import ToolingSettings from './ToolingSettings' @@ -87,7 +87,7 @@ const AgentSettingPopupContainer: React.FC = ({ tab, ag /> - {menu === 'essential' && } + {menu === 'essential' && } {menu === 'prompt' && } {menu === 'tooling' && } {menu === 'advanced' && } diff --git a/src/renderer/src/pages/settings/AgentSettings/EssentialSettings.tsx b/src/renderer/src/pages/settings/AgentSettings/EssentialSettings.tsx new file mode 100644 index 0000000000..0977e1f5cc --- /dev/null +++ b/src/renderer/src/pages/settings/AgentSettings/EssentialSettings.tsx @@ -0,0 +1,56 @@ +import { Avatar } from '@heroui/react' +import { getAgentTypeAvatar } from '@renderer/config/agent' +import { useUpdateAgent } from '@renderer/hooks/agents/useUpdateAgent' +import { useUpdateSession } from '@renderer/hooks/agents/useUpdateSession' +import { getAgentTypeLabel } from '@renderer/i18n/label' +import { GetAgentResponse, GetAgentSessionResponse, isAgentEntity } from '@renderer/types' +import { FC } from 'react' +import { useTranslation } from 'react-i18next' + +import { AccessibleDirsSetting } from './AccessibleDirsSetting' +import { AvatarSetting } from './AvatarSetting' +import { DescriptionSetting } from './DescriptionSetting' +import { ModelSetting } from './ModelSetting' +import { NameSetting } from './NameSetting' +import { SettingsContainer, SettingsItem, SettingsTitle } from './shared' + +// const logger = loggerService.withContext('AgentEssentialSettings') + +type EssentialSettingsProps = + | { + agentBase: GetAgentResponse | undefined | null + update: ReturnType['updateAgent'] + } + | { + agentBase: GetAgentSessionResponse | undefined | null + update: ReturnType['updateSession'] + } + +const EssentialSettings: FC = ({ agentBase, update }) => { + const { t } = useTranslation() + + if (!agentBase) return null + + const isAgent = isAgentEntity(agentBase) + + return ( + + {isAgent && ( + + {t('agent.type.label')} +
      + + {(agentBase?.name ?? agentBase?.type) ? getAgentTypeLabel(agentBase.type) : ''} +
      +
      + )} + {isAgent && } + + + + +
      + ) +} + +export default EssentialSettings diff --git a/src/renderer/src/pages/settings/AgentSettings/ModelSetting.tsx b/src/renderer/src/pages/settings/AgentSettings/ModelSetting.tsx index 09ee7d8235..aed615b860 100644 --- a/src/renderer/src/pages/settings/AgentSettings/ModelSetting.tsx +++ b/src/renderer/src/pages/settings/AgentSettings/ModelSetting.tsx @@ -1,4 +1,4 @@ -import SelectAgentModelButton from '@renderer/pages/home/components/SelectAgentModelButton' +import SelectAgentBaseModelButton from '@renderer/pages/home/components/SelectAgentBaseModelButton' import { AgentBaseWithId, ApiModel, UpdateAgentBaseForm } from '@renderer/types' import { useTranslation } from 'react-i18next' @@ -21,9 +21,9 @@ export const ModelSetting: React.FC = ({ base, update, isDisa if (!base) return null return ( - + {t('common.model')} - + ) } diff --git a/src/renderer/src/pages/settings/AgentSettings/PromptSettings.tsx b/src/renderer/src/pages/settings/AgentSettings/PromptSettings.tsx index 15f2f9be60..f931950128 100644 --- a/src/renderer/src/pages/settings/AgentSettings/PromptSettings.tsx +++ b/src/renderer/src/pages/settings/AgentSettings/PromptSettings.tsx @@ -22,7 +22,7 @@ type AgentPromptSettingsProps = } | { agentBase: AgentSessionEntity | undefined | null - update: ReturnType + update: ReturnType['updateSession'] } const PromptSettings: FC = ({ agentBase, update }) => { diff --git a/src/renderer/src/pages/settings/AgentSettings/SessionEssentialSettings.tsx b/src/renderer/src/pages/settings/AgentSettings/SessionEssentialSettings.tsx deleted file mode 100644 index 67b83dc649..0000000000 --- a/src/renderer/src/pages/settings/AgentSettings/SessionEssentialSettings.tsx +++ /dev/null @@ -1,29 +0,0 @@ -import { useUpdateAgent } from '@renderer/hooks/agents/useUpdateAgent' -import { GetAgentSessionResponse } from '@renderer/types' -import { FC } from 'react' - -import { AccessibleDirsSetting } from './AccessibleDirsSetting' -import { DescriptionSetting } from './DescriptionSetting' -import { NameSetting } from './NameSetting' -import { SettingsContainer } from './shared' - -// const logger = loggerService.withContext('AgentEssentialSettings') - -interface SessionEssentialSettingsProps { - session: GetAgentSessionResponse | undefined | null - update: ReturnType['updateAgent'] -} - -const SessionEssentialSettings: FC = ({ session, update }) => { - if (!session) return null - - return ( - - - - - - ) -} - -export default SessionEssentialSettings diff --git a/src/renderer/src/pages/settings/AgentSettings/SessionSettingsPopup.tsx b/src/renderer/src/pages/settings/AgentSettings/SessionSettingsPopup.tsx index 576f1b1005..af5b2e71d7 100644 --- a/src/renderer/src/pages/settings/AgentSettings/SessionSettingsPopup.tsx +++ b/src/renderer/src/pages/settings/AgentSettings/SessionSettingsPopup.tsx @@ -6,8 +6,8 @@ import { useState } from 'react' import { useTranslation } from 'react-i18next' import AdvancedSettings from './AdvancedSettings' +import EssentialSettings from './EssentialSettings' import PromptSettings from './PromptSettings' -import SessionEssentialSettings from './SessionEssentialSettings' import { LeftMenu, SessionLabel, Settings, StyledMenu, StyledModal } from './shared' import ToolingSettings from './ToolingSettings' @@ -30,7 +30,7 @@ const SessionSettingPopupContainer: React.FC = ({ tab const { session, isLoading, error } = useSession(agentId, sessionId) - const updateSession = useUpdateSession(agentId) + const { updateSession } = useUpdateSession(agentId) const onOk = () => { setOpen(false) @@ -89,7 +89,7 @@ const SessionSettingPopupContainer: React.FC = ({ tab /> - {menu === 'essential' && } + {menu === 'essential' && } {menu === 'prompt' && } {menu === 'tooling' && } {menu === 'advanced' && } diff --git a/src/renderer/src/pages/settings/AgentSettings/shared.tsx b/src/renderer/src/pages/settings/AgentSettings/shared.tsx index eb26fcf0a2..2443ed8f7f 100644 --- a/src/renderer/src/pages/settings/AgentSettings/shared.tsx +++ b/src/renderer/src/pages/settings/AgentSettings/shared.tsx @@ -1,5 +1,4 @@ import { cn } from '@heroui/react' -import Ellipsis from '@renderer/components/Ellipsis' import EmojiIcon from '@renderer/components/EmojiIcon' import { getAgentTypeLabel } from '@renderer/i18n/label' import { AgentEntity, AgentSessionEntity } from '@renderer/types' @@ -35,11 +34,11 @@ export const AgentLabel: React.FC = ({ agent, classNames }) => const emoji = agent?.configuration?.avatar return ( -
      +
      - + {agent?.name ?? (agent?.type ? getAgentTypeLabel(agent.type) : '')} - +
      ) } @@ -53,7 +52,7 @@ export const SessionLabel: React.FC = ({ session, className } const displayName = session?.name ?? session?.id return ( <> - {displayName} + {displayName} ) } diff --git a/src/renderer/src/store/runtime.ts b/src/renderer/src/store/runtime.ts index fe556b9a9e..f86a79cecb 100644 --- a/src/renderer/src/store/runtime.ts +++ b/src/renderer/src/store/runtime.ts @@ -11,7 +11,7 @@ export interface ChatState { activeAgentId: string | null /** UI state. Map agent id to active session id. * null represents no active session */ - activeSessionId: Record + activeSessionIdMap: Record /** meanwhile active Assistants or Agents */ activeTopicOrSession: 'topic' | 'session' /** topic ids that are currently being renamed */ @@ -90,7 +90,7 @@ const initialState: RuntimeState = { activeTopic: null, activeAgentId: null, activeTopicOrSession: 'topic', - activeSessionId: {}, + activeSessionIdMap: {}, renamingTopics: [], newlyRenamedTopics: [], sessionWaiting: {} @@ -163,7 +163,7 @@ const runtimeSlice = createSlice({ }, setActiveSessionIdAction: (state, action: PayloadAction<{ agentId: string; sessionId: string | null }>) => { const { agentId, sessionId } = action.payload - state.chat.activeSessionId[agentId] = sessionId + state.chat.activeSessionIdMap[agentId] = sessionId }, setActiveTopicOrSessionAction: (state, action: PayloadAction<'topic' | 'session'>) => { state.chat.activeTopicOrSession = action.payload diff --git a/src/renderer/src/types/agent.ts b/src/renderer/src/types/agent.ts index 96a8e70b02..ca1e88cbd1 100644 --- a/src/renderer/src/types/agent.ts +++ b/src/renderer/src/types/agent.ts @@ -266,8 +266,12 @@ export const GetAgentSessionResponseSchema = AgentSessionEntitySchema.extend({ slash_commands: z.array(SlashCommandSchema).optional() // Array of slash commands to trigger the agent }) +export const CreateAgentSessionResponseSchema = GetAgentSessionResponseSchema + export type GetAgentSessionResponse = z.infer +export type CreateAgentSessionResponse = GetAgentSessionResponse + export const ListAgentSessionsResponseSchema = z.object({ data: z.array(AgentSessionEntitySchema), total: z.int(), From 33ce41704daa05892cf83d106a250d17f6e9b167 Mon Sep 17 00:00:00 2001 From: Phantom Date: Fri, 17 Oct 2025 20:24:13 +0800 Subject: [PATCH 16/27] fix(message): adjust layout and overflow properties for better display (#10746) * style(CodeBlockView): reduce min-width from 45ch to 35ch to fix layout issues * style(messages): adjust overflow properties and clean up commented code Remove commented overflow properties and adjust overflow behavior for better scroll handling in message containers * style: remove commented overflow css properties --- .../src/components/CodeBlockView/view.tsx | 2 +- .../src/pages/home/Messages/MessageGroup.tsx | 21 +++---------------- 2 files changed, 4 insertions(+), 19 deletions(-) diff --git a/src/renderer/src/components/CodeBlockView/view.tsx b/src/renderer/src/components/CodeBlockView/view.tsx index a89e545bc9..939095262e 100644 --- a/src/renderer/src/components/CodeBlockView/view.tsx +++ b/src/renderer/src/components/CodeBlockView/view.tsx @@ -326,7 +326,7 @@ const CodeBlockWrapper = styled.div<{ $isInSpecialView: boolean }>` * 一是 CodeViewer 在气泡样式下的用户消息中无法撑开气泡, * 二是 代码块内容过少时 toolbar 会和 title 重叠。 */ - min-width: 45ch; + min-width: 35ch; .code-toolbar { background-color: ${(props) => (props.$isInSpecialView ? 'transparent' : 'var(--color-background-mute)')}; diff --git a/src/renderer/src/pages/home/Messages/MessageGroup.tsx b/src/renderer/src/pages/home/Messages/MessageGroup.tsx index 223124f3bf..fde7ccd13a 100644 --- a/src/renderer/src/pages/home/Messages/MessageGroup.tsx +++ b/src/renderer/src/pages/home/Messages/MessageGroup.tsx @@ -337,29 +337,18 @@ const GroupContainer = styled.div` const GridContainer = styled(Scrollbar)<{ $count: number; $gridColumns: number }>` width: 100%; display: grid; + overflow-y: visible; gap: 16px; &.horizontal { padding-bottom: 4px; grid-template-columns: repeat(${({ $count }) => $count}, minmax(420px, 1fr)); - overflow-y: hidden; overflow-x: auto; - &::-webkit-scrollbar { - height: 6px; - } - &::-webkit-scrollbar-thumb { - background: var(--color-scrollbar-thumb); - border-radius: var(--scrollbar-thumb-radius); - } - &::-webkit-scrollbar-thumb:hover { - background: var(--color-scrollbar-thumb-hover); - } } &.fold, &.vertical { grid-template-columns: repeat(1, minmax(0, 1fr)); gap: 8px; - overflow: hidden; } &.grid { grid-template-columns: repeat( @@ -367,15 +356,11 @@ const GridContainer = styled(Scrollbar)<{ $count: number; $gridColumns: number } minmax(0, 1fr) ); grid-template-rows: auto; - overflow-y: auto; - overflow-x: hidden; } &.multi-select-mode { grid-template-columns: repeat(1, minmax(0, 1fr)); gap: 10px; - overflow-y: auto; - overflow-x: hidden; .grid { height: auto; } @@ -401,7 +386,7 @@ interface MessageWrapperProps { const MessageWrapper = styled.div` &.horizontal { padding: 1px; - /* overflow-y: auto; */ + overflow-y: auto; .message { height: 100%; border: 0.5px solid var(--color-border); @@ -423,7 +408,7 @@ const MessageWrapper = styled.div` &.grid { display: block; height: 300px; - overflow: hidden; + overflow-y: hidden; border: 0.5px solid var(--color-border); border-radius: 10px; cursor: pointer; From c8ab0b94286f38cb2773c013a736eb69a09d8de8 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Fri, 17 Oct 2025 20:27:16 +0800 Subject: [PATCH 17/27] fix: resolve gpt-5-codex streaming response issue (#10781) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: resolve gpt-5-codex streaming response issue - Add patch for @opeoginni/github-copilot-openai-compatible to fix text part ID mismatch - Fix text-end event to use currentTextId instead of value.item.id for proper ID matching - Add COPILOT_DEFAULT_HEADERS to OpenAI client for GitHub Copilot compatibility 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude * format code --------- Co-authored-by: Claude --- ...nai-compatible-npm-0.1.18-3f65760532.patch | 44 +++++++++++++++++++ package.json | 2 +- .../legacy/clients/openai/OpenAIBaseClient.ts | 4 +- yarn.lock | 14 +++++- 4 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 .yarn/patches/@opeoginni-github-copilot-openai-compatible-npm-0.1.18-3f65760532.patch diff --git a/.yarn/patches/@opeoginni-github-copilot-openai-compatible-npm-0.1.18-3f65760532.patch b/.yarn/patches/@opeoginni-github-copilot-openai-compatible-npm-0.1.18-3f65760532.patch new file mode 100644 index 0000000000..cebfdd00a5 --- /dev/null +++ b/.yarn/patches/@opeoginni-github-copilot-openai-compatible-npm-0.1.18-3f65760532.patch @@ -0,0 +1,44 @@ +diff --git a/dist/index.js b/dist/index.js +index 53f411e55a4c9a06fd29bb4ab8161c4ad15980cd..71b91f196c8b886ed90dd237dec5625d79d5677e 100644 +--- a/dist/index.js ++++ b/dist/index.js +@@ -12676,10 +12676,13 @@ var OpenAIResponsesLanguageModel = class { + } + }); + } else if (value.item.type === "message") { +- controller.enqueue({ +- type: "text-end", +- id: value.item.id +- }); ++ // Fix for gpt-5-codex: use currentTextId to ensure text-end matches text-start ++ if (currentTextId) { ++ controller.enqueue({ ++ type: "text-end", ++ id: currentTextId ++ }); ++ } + currentTextId = null; + } else if (isResponseOutputItemDoneReasoningChunk(value)) { + const activeReasoningPart = activeReasoning[value.item.id]; +diff --git a/dist/index.mjs b/dist/index.mjs +index 7719264da3c49a66c2626082f6ccaae6e3ef5e89..090fd8cf142674192a826148428ed6a0c4a54e35 100644 +--- a/dist/index.mjs ++++ b/dist/index.mjs +@@ -12670,10 +12670,13 @@ var OpenAIResponsesLanguageModel = class { + } + }); + } else if (value.item.type === "message") { +- controller.enqueue({ +- type: "text-end", +- id: value.item.id +- }); ++ // Fix for gpt-5-codex: use currentTextId to ensure text-end matches text-start ++ if (currentTextId) { ++ controller.enqueue({ ++ type: "text-end", ++ id: currentTextId ++ }); ++ } + currentTextId = null; + } else if (isResponseOutputItemDoneReasoningChunk(value)) { + const activeReasoningPart = activeReasoning[value.item.id]; diff --git a/package.json b/package.json index e82153922c..1dad6a9f5a 100644 --- a/package.json +++ b/package.json @@ -154,7 +154,7 @@ "@opentelemetry/sdk-trace-base": "^2.0.0", "@opentelemetry/sdk-trace-node": "^2.0.0", "@opentelemetry/sdk-trace-web": "^2.0.0", - "@opeoginni/github-copilot-openai-compatible": "0.1.18", + "@opeoginni/github-copilot-openai-compatible": "patch:@opeoginni/github-copilot-openai-compatible@npm%3A0.1.18#~/.yarn/patches/@opeoginni-github-copilot-openai-compatible-npm-0.1.18-3f65760532.patch", "@playwright/test": "^1.52.0", "@radix-ui/react-context-menu": "^2.2.16", "@reduxjs/toolkit": "^2.2.5", diff --git a/src/renderer/src/aiCore/legacy/clients/openai/OpenAIBaseClient.ts b/src/renderer/src/aiCore/legacy/clients/openai/OpenAIBaseClient.ts index 79762e9474..71ed1c95fe 100644 --- a/src/renderer/src/aiCore/legacy/clients/openai/OpenAIBaseClient.ts +++ b/src/renderer/src/aiCore/legacy/clients/openai/OpenAIBaseClient.ts @@ -1,4 +1,5 @@ import { loggerService } from '@logger' +import { COPILOT_DEFAULT_HEADERS } from '@renderer/aiCore/provider/constants' import { isClaudeReasoningModel, isOpenAIReasoningModel, @@ -166,7 +167,8 @@ export abstract class OpenAIBaseClient< baseURL: this.getBaseURL(), defaultHeaders: { ...this.defaultHeaders(), - ...this.provider.extra_headers + ...this.provider.extra_headers, + ...(this.provider.id === 'copilot' ? COPILOT_DEFAULT_HEADERS : {}) } }) as TSdkInstance } diff --git a/yarn.lock b/yarn.lock index 4fcea693ba..2d5bffcfd0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7421,6 +7421,18 @@ __metadata: languageName: node linkType: hard +"@opeoginni/github-copilot-openai-compatible@patch:@opeoginni/github-copilot-openai-compatible@npm%3A0.1.18#~/.yarn/patches/@opeoginni-github-copilot-openai-compatible-npm-0.1.18-3f65760532.patch": + version: 0.1.18 + resolution: "@opeoginni/github-copilot-openai-compatible@patch:@opeoginni/github-copilot-openai-compatible@npm%3A0.1.18#~/.yarn/patches/@opeoginni-github-copilot-openai-compatible-npm-0.1.18-3f65760532.patch::version=0.1.18&hash=1cf9d0" + dependencies: + "@ai-sdk/openai": "npm:^2.0.42" + "@ai-sdk/openai-compatible": "npm:^1.0.19" + "@ai-sdk/provider": "npm:^2.1.0-beta.4" + "@ai-sdk/provider-utils": "npm:^3.0.10" + checksum: 10c0/cfffc031d2742068d20baed0e0ade6e9182c29ee7a425fa64262c04023ae75220b8b944ad2c9554255681e325fa1a70ec5e1f961b5f7370c871e70cbaeac0e79 + languageName: node + linkType: hard + "@oxc-project/runtime@npm:0.71.0": version: 0.71.0 resolution: "@oxc-project/runtime@npm:0.71.0" @@ -13993,7 +14005,7 @@ __metadata: "@opentelemetry/sdk-trace-base": "npm:^2.0.0" "@opentelemetry/sdk-trace-node": "npm:^2.0.0" "@opentelemetry/sdk-trace-web": "npm:^2.0.0" - "@opeoginni/github-copilot-openai-compatible": "npm:0.1.18" + "@opeoginni/github-copilot-openai-compatible": "patch:@opeoginni/github-copilot-openai-compatible@npm%3A0.1.18#~/.yarn/patches/@opeoginni-github-copilot-openai-compatible-npm-0.1.18-3f65760532.patch" "@playwright/test": "npm:^1.52.0" "@radix-ui/react-context-menu": "npm:^2.2.16" "@reduxjs/toolkit": "npm:^2.2.5" From 1e1d5c4a14a724521e30535ee6c95607b824954e Mon Sep 17 00:00:00 2001 From: SuYao Date: Fri, 17 Oct 2025 20:34:53 +0800 Subject: [PATCH 18/27] feat: add Mistral provider configuration to AI Providers (#10795) --- .../src/aiCore/provider/providerInitialization.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/renderer/src/aiCore/provider/providerInitialization.ts b/src/renderer/src/aiCore/provider/providerInitialization.ts index 2a19729d7e..9942ffa405 100644 --- a/src/renderer/src/aiCore/provider/providerInitialization.ts +++ b/src/renderer/src/aiCore/provider/providerInitialization.ts @@ -55,6 +55,14 @@ export const NEW_PROVIDER_CONFIGS: ProviderConfig[] = [ creatorFunctionName: 'createPerplexity', supportsImageGeneration: false, aliases: ['perplexity'] + }, + { + id: 'mistral', + name: 'Mistral', + import: () => import('@ai-sdk/mistral'), + creatorFunctionName: 'createMistral', + supportsImageGeneration: false, + aliases: ['mistral'] } ] as const From ab3083f943182a1102498e4799d7926993a57b06 Mon Sep 17 00:00:00 2001 From: defi-failure <159208748+defi-failure@users.noreply.github.com> Date: Fri, 17 Oct 2025 20:48:40 +0800 Subject: [PATCH 19/27] fix: fail to create assistant (#10796) --- src/renderer/src/store/assistants.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/renderer/src/store/assistants.ts b/src/renderer/src/store/assistants.ts index 6fa570be17..162a8edd48 100644 --- a/src/renderer/src/store/assistants.ts +++ b/src/renderer/src/store/assistants.ts @@ -193,8 +193,7 @@ const assistantsSlice = createSlice({ }) }, addAssistantPreset: (state, action: PayloadAction) => { - // @ts-ignore ts-2589 false positive - state.agents.push(action.payload) + state.presets.push(action.payload) }, removeAssistantPreset: (state, action: PayloadAction<{ id: string }>) => { state.presets = state.presets.filter((c) => c.id !== action.payload.id) From 131444ac52f9ea31a162518daa6460bd18484fbf Mon Sep 17 00:00:00 2001 From: defi-failure <159208748+defi-failure@users.noreply.github.com> Date: Fri, 17 Oct 2025 20:52:15 +0800 Subject: [PATCH 20/27] fix: agent supported model filter (#10788) * Revert "fix: make anthropic model provided by cherryin visible to agent (#10695)" This reverts commit 7b3b73d390d665582cc2cdfab1fd673f56c4b7bd. * fix: agent supported model filter --- src/main/apiServer/services/models.ts | 35 +++++++++---------- src/main/apiServer/utils/index.ts | 24 ++++++++----- .../agents/services/claudecode/index.ts | 20 +++-------- .../components/Popups/agent/AgentModal.tsx | 29 +++++++++------ src/renderer/src/config/models/utils.ts | 7 +++- src/renderer/src/config/providers.ts | 3 +- .../components/SelectAgentBaseModelButton.tsx | 7 ++-- src/renderer/src/store/index.ts | 2 +- src/renderer/src/store/migrate.ts | 5 +++ src/renderer/src/types/apiModels.ts | 1 - src/renderer/src/utils/agentSession.ts | 2 +- 11 files changed, 73 insertions(+), 62 deletions(-) diff --git a/src/main/apiServer/services/models.ts b/src/main/apiServer/services/models.ts index 6c0056b27e..660686ef45 100644 --- a/src/main/apiServer/services/models.ts +++ b/src/main/apiServer/services/models.ts @@ -2,7 +2,12 @@ import { isEmpty } from 'lodash' import { ApiModel, ApiModelsFilter, ApiModelsResponse } from '../../../renderer/src/types/apiModels' import { loggerService } from '../../services/LoggerService' -import { getAvailableProviders, listAllAvailableModels, transformModelToOpenAI } from '../utils' +import { + getAvailableProviders, + getProviderAnthropicModelChecker, + listAllAvailableModels, + transformModelToOpenAI +} from '../utils' const logger = loggerService.withContext('ModelsService') @@ -10,10 +15,6 @@ const logger = loggerService.withContext('ModelsService') export type ModelsFilter = ApiModelsFilter -const isAnthropicProvider = (provider: { type: string; anthropicApiHost?: string }) => { - return provider.type === 'anthropic' || !isEmpty(provider.anthropicApiHost?.trim()) -} - export class ModelsService { async getModels(filter: ModelsFilter): Promise { try { @@ -22,7 +23,7 @@ export class ModelsService { let providers = await getAvailableProviders() if (filter.providerType === 'anthropic') { - providers = providers.filter(isAnthropicProvider) + providers = providers.filter((p) => p.type === 'anthropic' || !isEmpty(p.anthropicApiHost?.trim())) } const models = await listAllAvailableModels(providers) @@ -31,22 +32,18 @@ export class ModelsService { for (const model of models) { const provider = providers.find((p) => p.id === model.provider) - logger.debug(`Processing model ${model.id} from provider ${model.provider}`, { - isAnthropicModel: provider?.isAnthropicModel - }) - if ( - !provider || - (filter.providerType === 'anthropic' && provider.isAnthropicModel && !provider.isAnthropicModel(model)) - ) { - continue - } - // Special case: For "aihubmix", it should be covered by above condition, but just in case - if (provider.id === 'aihubmix' && filter.providerType === 'anthropic' && !model.id.includes('claude')) { + logger.debug(`Processing model ${model.id}`) + if (!provider) { + logger.debug(`Skipping model ${model.id} . Reason: Provider not found.`) continue } - if (filter.supportAnthropic && model.endpoint_type !== 'anthropic' && !isAnthropicProvider(provider)) { - continue + if (filter.providerType === 'anthropic') { + const checker = getProviderAnthropicModelChecker(provider.id) + if (!checker(model)) { + logger.debug(`Skipping model ${model.id} from ${model.provider}. Reason: Not an Anthropic model.`) + continue + } } const openAIModel = transformModelToOpenAI(model, provider) diff --git a/src/main/apiServer/utils/index.ts b/src/main/apiServer/utils/index.ts index 865f961db9..7fb0c3511f 100644 --- a/src/main/apiServer/utils/index.ts +++ b/src/main/apiServer/utils/index.ts @@ -1,7 +1,7 @@ import { CacheService } from '@main/services/CacheService' import { loggerService } from '@main/services/LoggerService' import { reduxService } from '@main/services/ReduxService' -import { ApiModel, EndpointType, Model, Provider } from '@types' +import { ApiModel, Model, Provider } from '@types' const logger = loggerService.withContext('ApiServerUtils') @@ -114,7 +114,6 @@ export async function validateModelId(model: string): Promise<{ error?: ModelValidationError provider?: Provider modelId?: string - modelEndpointType?: EndpointType }> { try { if (!model || typeof model !== 'string') { @@ -167,8 +166,7 @@ export async function validateModelId(model: string): Promise<{ } // Check if model exists in provider - const modelInProvider = provider.models?.find((m) => m.id === modelId) - const modelExists = !!modelInProvider + const modelExists = provider.models?.some((m) => m.id === modelId) if (!modelExists) { const availableModels = provider.models?.map((m) => m.id).join(', ') || 'none' return { @@ -181,13 +179,10 @@ export async function validateModelId(model: string): Promise<{ } } - const modelEndpointType = modelInProvider?.endpoint_type - return { valid: true, provider, - modelId, - modelEndpointType + modelId } } catch (error: any) { logger.error('Error validating model ID', { error, model }) @@ -284,3 +279,16 @@ export function validateProvider(provider: Provider): boolean { return false } } + +export const getProviderAnthropicModelChecker = (providerId: string): ((m: Model) => boolean) => { + switch (providerId) { + case 'cherryin': + case 'new-api': + return (m: Model) => m.endpoint_type === 'anthropic' + case 'aihubmix': + return (m: Model) => m.id.includes('claude') + default: + // allow all models when checker not configured + return () => true + } +} diff --git a/src/main/services/agents/services/claudecode/index.ts b/src/main/services/agents/services/claudecode/index.ts index 7dae2f9e9e..7b2f119afb 100644 --- a/src/main/services/agents/services/claudecode/index.ts +++ b/src/main/services/agents/services/claudecode/index.ts @@ -8,7 +8,6 @@ import { config as apiConfigService } from '@main/apiServer/config' import { validateModelId } from '@main/apiServer/utils' import getLoginShellEnvironment from '@main/utils/shell-env' import { app } from 'electron' -import { isEmpty } from 'lodash' import { GetAgentSessionResponse } from '../..' import { AgentServiceInterface, AgentStream, AgentStreamEvent } from '../../interfaces/AgentStreamInterface' @@ -61,20 +60,11 @@ class ClaudeCodeService implements AgentServiceInterface { }) return aiStream } - - const validateModelInfo: (m: typeof modelInfo) => boolean = (m) => { - const { provider, modelEndpointType } = m - if (!provider) return false - if (isEmpty(provider.apiKey?.trim())) return false - - const isAnthropicType = provider.type === 'anthropic' - const isAnthropicEndpoint = modelEndpointType === 'anthropic' - const hasValidApiHost = !isEmpty(provider.anthropicApiHost?.trim()) - - return !(!isAnthropicType && !isAnthropicEndpoint && !hasValidApiHost) - } - - if (!modelInfo.provider || !validateModelInfo(modelInfo)) { + if ( + (modelInfo.provider?.type !== 'anthropic' && + (modelInfo.provider?.anthropicApiHost === undefined || modelInfo.provider.anthropicApiHost.trim() === '')) || + modelInfo.provider.apiKey === '' + ) { logger.error('Anthropic provider configuration is missing', { modelInfo }) diff --git a/src/renderer/src/components/Popups/agent/AgentModal.tsx b/src/renderer/src/components/Popups/agent/AgentModal.tsx index 63b614944a..ea772fb1e6 100644 --- a/src/renderer/src/components/Popups/agent/AgentModal.tsx +++ b/src/renderer/src/components/Popups/agent/AgentModal.tsx @@ -17,7 +17,7 @@ import { import { loggerService } from '@logger' import type { Selection } from '@react-types/shared' import ClaudeIcon from '@renderer/assets/images/models/claude.png' -import { getModelLogo } from '@renderer/config/models' +import { agentModelFilter, getModelLogo } from '@renderer/config/models' import { permissionModeCards } from '@renderer/constants/permissionModes' import { useAgents } from '@renderer/hooks/agents/useAgents' import { useApiModels } from '@renderer/hooks/agents/useModels' @@ -100,7 +100,7 @@ export const AgentModal: React.FC = ({ agent, trigger, isOpen: _isOpen, o const { addAgent } = useAgents() const { updateAgent } = useUpdateAgent() // hard-coded. We only support anthropic for now. - const { models } = useApiModels({ supportAnthropic: true }) + const { models } = useApiModels({ providerType: 'anthropic' }) const isEditing = (agent?: AgentWithTools) => agent !== undefined const [form, setForm] = useState(() => buildAgentForm(agent)) @@ -245,14 +245,23 @@ export const AgentModal: React.FC = ({ agent, trigger, isOpen: _isOpen, o const modelOptions = useMemo(() => { // mocked data. not final version - return (models ?? []).map((model) => ({ - type: 'model', - key: model.id, - label: model.name, - avatar: getModelLogo(model.id), - providerId: model.provider, - providerName: model.provider_name - })) satisfies ModelOption[] + return (models ?? []) + .filter((m) => + agentModelFilter({ + id: m.id, + provider: m.provider || '', + name: m.name, + group: '' + }) + ) + .map((model) => ({ + type: 'model', + key: model.id, + label: model.name, + avatar: getModelLogo(model.id), + providerId: model.provider, + providerName: model.provider_name + })) satisfies ModelOption[] }, [models]) const onModelChange = useCallback((e: ChangeEvent) => { diff --git a/src/renderer/src/config/models/utils.ts b/src/renderer/src/config/models/utils.ts index 39078e2924..1759a93d18 100644 --- a/src/renderer/src/config/models/utils.ts +++ b/src/renderer/src/config/models/utils.ts @@ -1,3 +1,4 @@ +import { isEmbeddingModel, isRerankModel } from '@renderer/config/models/embedding' import { Model } from '@renderer/types' import { getLowerBaseModelName } from '@renderer/utils' import OpenAI from 'openai' @@ -5,7 +6,7 @@ import OpenAI from 'openai' import { WEB_SEARCH_PROMPT_FOR_OPENROUTER } from '../prompts' import { getWebSearchTools } from '../tools' import { isOpenAIReasoningModel } from './reasoning' -import { isGenerateImageModel, isVisionModel } from './vision' +import { isGenerateImageModel, isTextToImageModel, isVisionModel } from './vision' import { isOpenAIWebSearchChatCompletionOnlyModel } from './websearch' export const NOT_SUPPORTED_REGEX = /(?:^tts|whisper|speech)/i @@ -246,3 +247,7 @@ export const isOpenAIOpenWeightModel = (model: Model) => { // zhipu 视觉推理模型用这组 special token 标记推理结果 export const ZHIPU_RESULT_TOKENS = ['<|begin_of_box|>', '<|end_of_box|>'] as const + +export const agentModelFilter = (model: Model): boolean => { + return !isEmbeddingModel(model) && !isRerankModel(model) && !isTextToImageModel(model) +} diff --git a/src/renderer/src/config/providers.ts b/src/renderer/src/config/providers.ts index 22faf0fb0e..7f8d95dcd1 100644 --- a/src/renderer/src/config/providers.ts +++ b/src/renderer/src/config/providers.ts @@ -58,7 +58,6 @@ import ZhipuProviderLogo from '@renderer/assets/images/providers/zhipu.png' import { AtLeast, isSystemProvider, - Model, OpenAIServiceTiers, Provider, ProviderType, @@ -88,6 +87,7 @@ export const SYSTEM_PROVIDERS_CONFIG: Record = type: 'openai', apiKey: '', apiHost: 'https://open.cherryin.net', + anthropicApiHost: 'https://open.cherryin.net', models: [], isSystem: true, enabled: true @@ -109,7 +109,6 @@ export const SYSTEM_PROVIDERS_CONFIG: Record = apiKey: '', apiHost: 'https://aihubmix.com', anthropicApiHost: 'https://aihubmix.com/anthropic', - isAnthropicModel: (m: Model) => m.id.includes('claude'), models: SYSTEM_MODELS.aihubmix, isSystem: true, enabled: false diff --git a/src/renderer/src/pages/home/components/SelectAgentBaseModelButton.tsx b/src/renderer/src/pages/home/components/SelectAgentBaseModelButton.tsx index 7e21d1b47a..74f69cf15e 100644 --- a/src/renderer/src/pages/home/components/SelectAgentBaseModelButton.tsx +++ b/src/renderer/src/pages/home/components/SelectAgentBaseModelButton.tsx @@ -1,10 +1,10 @@ import { Button } from '@heroui/react' import ModelAvatar from '@renderer/components/Avatar/ModelAvatar' import { SelectApiModelPopup } from '@renderer/components/Popups/SelectModelPopup' -import { isEmbeddingModel, isRerankModel, isTextToImageModel } from '@renderer/config/models' +import { agentModelFilter } from '@renderer/config/models' import { useApiModel } from '@renderer/hooks/agents/useModel' import { getProviderNameById } from '@renderer/services/ProviderService' -import { AgentBaseWithId, ApiModel, isAgentEntity, Model } from '@renderer/types' +import { AgentBaseWithId, ApiModel, isAgentEntity } from '@renderer/types' import { getModelFilterByAgentType } from '@renderer/utils/agentSession' import { apiModelAdapter } from '@renderer/utils/model' import { ChevronsUpDown } from 'lucide-react' @@ -22,12 +22,11 @@ const SelectAgentBaseModelButton: FC = ({ agentBase: agent, onSelect, isD const model = useApiModel({ id: agent?.model }) const apiFilter = isAgentEntity(agent) ? getModelFilterByAgentType(agent.type) : undefined - const modelFilter = (model: Model) => !isEmbeddingModel(model) && !isRerankModel(model) && !isTextToImageModel(model) if (!agent) return null const onSelectModel = async () => { - const selectedModel = await SelectApiModelPopup.show({ model, apiFilter: apiFilter, modelFilter }) + const selectedModel = await SelectApiModelPopup.show({ model, apiFilter: apiFilter, modelFilter: agentModelFilter }) if (selectedModel && selectedModel.id !== agent.model) { onSelect(selectedModel) } diff --git a/src/renderer/src/store/index.ts b/src/renderer/src/store/index.ts index 0d53c2ca5b..0fee3196b2 100644 --- a/src/renderer/src/store/index.ts +++ b/src/renderer/src/store/index.ts @@ -65,7 +65,7 @@ const persistedReducer = persistReducer( { key: 'cherry-studio', storage, - version: 162, + version: 163, blacklist: ['runtime', 'messages', 'messageBlocks', 'tabs'], migrate }, diff --git a/src/renderer/src/store/migrate.ts b/src/renderer/src/store/migrate.ts index 5cca66b47a..46ac128c66 100644 --- a/src/renderer/src/store/migrate.ts +++ b/src/renderer/src/store/migrate.ts @@ -2671,6 +2671,11 @@ const migrateConfig = { '163': (state: RootState) => { try { addOcrProvider(state, BUILTIN_OCR_PROVIDERS_MAP.ovocr) + state.llm.providers.forEach((provider) => { + if (provider.id === 'cherryin') { + provider.anthropicApiHost = 'https://open.cherryin.net' + } + }) return state } catch (error) { logger.error('migrate 163 error', error as Error) diff --git a/src/renderer/src/types/apiModels.ts b/src/renderer/src/types/apiModels.ts index 7b4ec96c9d..68141bf68c 100644 --- a/src/renderer/src/types/apiModels.ts +++ b/src/renderer/src/types/apiModels.ts @@ -6,7 +6,6 @@ import { ProviderTypeSchema } from './provider' // Request schema for /v1/models export const ApiModelsFilterSchema = z.object({ providerType: ProviderTypeSchema.optional(), - supportAnthropic: z.coerce.boolean().optional(), offset: z.coerce.number().min(0).default(0).optional(), limit: z.coerce.number().min(1).default(20).optional() }) diff --git a/src/renderer/src/utils/agentSession.ts b/src/renderer/src/utils/agentSession.ts index b2cf14d174..df34413641 100644 --- a/src/renderer/src/utils/agentSession.ts +++ b/src/renderer/src/utils/agentSession.ts @@ -18,7 +18,7 @@ export const getModelFilterByAgentType = (type: AgentType): ApiModelsFilter => { switch (type) { case 'claude-code': return { - supportAnthropic: true + providerType: 'anthropic' } default: return {} From 8470e252d6cae65e4de3f4989968f9da9995c6bc Mon Sep 17 00:00:00 2001 From: defi-failure <159208748+defi-failure@users.noreply.github.com> Date: Fri, 17 Oct 2025 20:53:08 +0800 Subject: [PATCH 21/27] feat: auto-start API server when agents exist (#10772) * feat: auto-start API server when agents exist * fix: only display not running alert when enabled --- src/main/index.ts | 19 +++++++++++++++++-- src/renderer/src/hooks/agents/useAgents.ts | 3 ++- .../src/pages/home/Tabs/AssistantsTab.tsx | 4 ++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index da9736c2bb..fa83dc72b8 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -159,11 +159,26 @@ if (!app.requestSingleInstanceLock()) { logger.error('Failed to initialize Agent service:', error) } - // Start API server if enabled + // Start API server if enabled or if agents exist try { const config = await apiServerService.getCurrentConfig() logger.info('API server config:', config) - if (config.enabled) { + + // Check if there are any agents + let shouldStart = config.enabled + if (!shouldStart) { + try { + const { total } = await agentService.listAgents({ limit: 1 }) + if (total > 0) { + shouldStart = true + logger.info(`Detected ${total} agent(s), auto-starting API server`) + } + } catch (error: any) { + logger.warn('Failed to check agent count:', error) + } + } + + if (shouldStart) { await apiServerService.start() } } catch (error: any) { diff --git a/src/renderer/src/hooks/agents/useAgents.ts b/src/renderer/src/hooks/agents/useAgents.ts index 6af38228cb..bf23c2c9c8 100644 --- a/src/renderer/src/hooks/agents/useAgents.ts +++ b/src/renderer/src/hooks/agents/useAgents.ts @@ -26,7 +26,8 @@ export const useAgents = () => { const key = client.agentPaths.base const { apiServerConfig, apiServerRunning } = useApiServer() const fetcher = useCallback(async () => { - if (!apiServerConfig.enabled) { + // API server will start on startup if enabled OR there are agents + if (!apiServerConfig.enabled && !apiServerRunning) { throw new Error(t('apiServer.messages.notEnabled')) } if (!apiServerRunning) { diff --git a/src/renderer/src/pages/home/Tabs/AssistantsTab.tsx b/src/renderer/src/pages/home/Tabs/AssistantsTab.tsx index af02796d76..dc272b869a 100644 --- a/src/renderer/src/pages/home/Tabs/AssistantsTab.tsx +++ b/src/renderer/src/pages/home/Tabs/AssistantsTab.tsx @@ -101,7 +101,7 @@ const AssistantsTab: FC = (props) => { return ( - {!apiServerConfig.enabled && !iknow[ALERT_KEY] && ( + {!apiServerConfig.enabled && !apiServerRunning && !iknow[ALERT_KEY] && ( = (props) => { {apiServerConfig.enabled && !apiServerRunning && ( )} - {apiServerConfig.enabled && apiServerRunning && agentsError && ( + {apiServerRunning && agentsError && ( Date: Sat, 18 Oct 2025 00:12:38 +0800 Subject: [PATCH 22/27] fix: adjust Navbar and Chat components for better layout and responsiveness - Updated Navbar styles to improve margin handling for macOS. - Refactored Chat component to streamline layout and enhance responsiveness, including adjustments to main height calculations and navbar integration. - Cleaned up commented code and improved the structure of the ChatNavbar for better clarity and maintainability. - Enhanced styling in various components for consistent appearance and behavior across different screen sizes. --- src/renderer/src/components/app/Navbar.tsx | 4 +- src/renderer/src/pages/home/Chat.tsx | 133 +++++----- src/renderer/src/pages/home/ChatNavbar.tsx | 45 ++-- src/renderer/src/pages/home/Navbar.tsx | 40 +-- .../src/pages/home/Tabs/AssistantsTab.tsx | 2 +- .../pages/home/Tabs/SessionSettingsTab.tsx | 2 +- .../src/pages/home/Tabs/SessionsTab.tsx | 11 +- .../pages/home/Tabs/components/AgentItem.tsx | 23 +- .../home/Tabs/components/SessionItem.tsx | 239 ++++++++++++------ .../pages/home/Tabs/components/Sessions.tsx | 67 ++--- src/renderer/src/pages/home/Tabs/index.tsx | 5 +- .../home/components/ChatNavbarContent.tsx | 53 ++-- .../components/SelectAgentBaseModelButton.tsx | 4 +- .../home/components/SelectModelButton.tsx | 1 + .../AgentSettings/AgentSettingsPopup.tsx | 2 +- .../AgentSettings/EssentialSettings.tsx | 6 +- .../AgentSettings/SessionSettingsPopup.tsx | 2 +- .../pages/settings/AgentSettings/shared.tsx | 4 +- 18 files changed, 343 insertions(+), 300 deletions(-) diff --git a/src/renderer/src/components/app/Navbar.tsx b/src/renderer/src/components/app/Navbar.tsx index 5ab2c486b5..778431a1a7 100644 --- a/src/renderer/src/components/app/Navbar.tsx +++ b/src/renderer/src/components/app/Navbar.tsx @@ -67,14 +67,14 @@ const NavbarContainer = styled.div<{ $isFullScreen: boolean }>` flex-direction: row; min-height: ${isMac ? 'env(titlebar-area-height)' : 'var(--navbar-height)'}; max-height: var(--navbar-height); - margin-left: ${isMac ? 'calc(var(--sidebar-width) * -1)' : 0}; + margin-left: ${isMac ? 'calc(var(--sidebar-width) * -1 + 2px)' : 0}; padding-left: ${({ $isFullScreen }) => isMac ? ($isFullScreen ? 'var(--sidebar-width)' : 'env(titlebar-area-x)') : 0}; -webkit-app-region: drag; ` const NavbarLeftContainer = styled.div` - min-width: ${isMac ? 'calc(var(--assistants-width) - 20px)' : 'var(--assistants-width)'}; + /* min-width: ${isMac ? 'calc(var(--assistants-width) - 20px)' : 'var(--assistants-width)'}; */ padding: 0 10px; display: flex; flex-direction: row; diff --git a/src/renderer/src/pages/home/Chat.tsx b/src/renderer/src/pages/home/Chat.tsx index 0775a4f718..0c2d76a6e6 100644 --- a/src/renderer/src/pages/home/Chat.tsx +++ b/src/renderer/src/pages/home/Chat.tsx @@ -140,9 +140,7 @@ const Chat: FC = (props) => { firstUpdateOrNoFirstUpdateHandler() } - const mainHeight = isTopNavbar - ? 'calc(100vh - var(--navbar-height) - var(--navbar-height) - 12px)' - : 'calc(100vh - var(--navbar-height))' + const mainHeight = isTopNavbar ? 'calc(100vh - var(--navbar-height) - 6px)' : 'calc(100vh - var(--navbar-height))' const SessionMessages = useMemo(() => { if (activeAgentId === null) { @@ -192,66 +190,84 @@ const Chat: FC = (props) => {
      ) }, []) + return ( - {isTopNavbar && ( - - )} -
      - - {activeTopicOrSession === 'topic' && ( - <> - - } - filter={contentSearchFilter} - includeUser={filterIncludeUser} - onIncludeUserChange={userOutlinedItemClickHandler} - /> - {messageNavigation === 'buttons' && } - - - )} - {activeTopicOrSession === 'session' && !activeAgentId && } - {activeTopicOrSession === 'session' && activeAgentId && !activeSessionId && } - {activeTopicOrSession === 'session' && activeAgentId && activeSessionId && ( - <> - - - - )} - {isMultiSelectMode && } - -
      + +
      + + +
      + {activeTopicOrSession === 'topic' && ( + <> + + } + filter={contentSearchFilter} + includeUser={filterIncludeUser} + onIncludeUserChange={userOutlinedItemClickHandler} + /> + {messageNavigation === 'buttons' && } + + + )} + {activeTopicOrSession === 'session' && !activeAgentId && } + {activeTopicOrSession === 'session' && activeAgentId && !activeSessionId && } + {activeTopicOrSession === 'session' && activeAgentId && activeSessionId && ( + <> + + + + )} + {isMultiSelectMode && } +
      +
      +
      +
      {topicPosition === 'right' && showTopics && ( + style={{ + position: 'absolute', + right: 0, + top: isTopNavbar ? 0 : 'calc(var(--navbar-height) + 1px)', + width: 'var(--assistants-width)', + height: '100%', + zIndex: 10 + }}> = (props) => { export const useChatMaxWidth = () => { const { showTopics, topicPosition } = useSettings() - const { isLeftNavbar } = useNavbarPosition() + const { isLeftNavbar, isTopNavbar } = useNavbarPosition() const { showAssistants } = useShowAssistants() const showRightTopics = showTopics && topicPosition === 'right' const minusAssistantsWidth = showAssistants ? '- var(--assistants-width)' : '' const minusRightTopicsWidth = showRightTopics ? '- var(--assistants-width)' : '' + const minusBorderWidth = isTopNavbar ? (showTopics ? '- 12px' : '- 6px') : '' const sidebarWidth = isLeftNavbar ? '- var(--sidebar-width)' : '' - return `calc(100vw ${sidebarWidth} ${minusAssistantsWidth} ${minusRightTopicsWidth})` + return `calc(100vw ${sidebarWidth} ${minusAssistantsWidth} ${minusRightTopicsWidth} ${minusBorderWidth})` } const Container = styled.div` diff --git a/src/renderer/src/pages/home/ChatNavbar.tsx b/src/renderer/src/pages/home/ChatNavbar.tsx index c44447da8f..43d04cd1a4 100644 --- a/src/renderer/src/pages/home/ChatNavbar.tsx +++ b/src/renderer/src/pages/home/ChatNavbar.tsx @@ -3,7 +3,7 @@ import { HStack } from '@renderer/components/Layout' import SearchPopup from '@renderer/components/Popups/SearchPopup' import { useAssistant } from '@renderer/hooks/useAssistant' import { modelGenerating } from '@renderer/hooks/useRuntime' -import { useSettings } from '@renderer/hooks/useSettings' +import { useNavbarPosition, useSettings } from '@renderer/hooks/useSettings' import { useShortcut } from '@renderer/hooks/useShortcuts' import { useShowAssistants, useShowTopics } from '@renderer/hooks/useStore' import { EVENT_NAMES, EventEmitter } from '@renderer/services/EventService' @@ -34,6 +34,7 @@ const HeaderNavbar: FC = ({ activeAssistant, setActiveAssistant, activeTo const { showAssistants, toggleShowAssistants } = useShowAssistants() const { topicPosition, narrowMode } = useSettings() const { showTopics, toggleShowTopics } = useShowTopics() + const { isTopNavbar } = useNavbarPosition() const dispatch = useAppDispatch() useShortcut('toggle_show_assistants', toggleShowAssistants) @@ -73,16 +74,16 @@ const HeaderNavbar: FC = ({ activeAssistant, setActiveAssistant, activeTo // ) return ( - -
      - {showAssistants && ( + +
      + {isTopNavbar && showAssistants && ( )} - {!showAssistants && ( + {isTopNavbar && !showAssistants && ( toggleShowAssistants()} style={{ marginRight: 8 }}> @@ -90,13 +91,13 @@ const HeaderNavbar: FC = ({ activeAssistant, setActiveAssistant, activeTo )} - {!showAssistants && ( + {!showAssistants && isTopNavbar && ( - + @@ -105,25 +106,29 @@ const HeaderNavbar: FC = ({ activeAssistant, setActiveAssistant, activeTo
      - - - - - - - - SearchPopup.show()}> - - - - {topicPosition === 'right' && !showTopics && ( + {isTopNavbar && } + {isTopNavbar && ( + + + + + + )} + {isTopNavbar && ( + + SearchPopup.show()}> + + + + )} + {isTopNavbar && topicPosition === 'right' && !showTopics && ( )} - {topicPosition === 'right' && showTopics && ( + {isTopNavbar && topicPosition === 'right' && showTopics && ( diff --git a/src/renderer/src/pages/home/Navbar.tsx b/src/renderer/src/pages/home/Navbar.tsx index 6d9ccc2285..c643391d8a 100644 --- a/src/renderer/src/pages/home/Navbar.tsx +++ b/src/renderer/src/pages/home/Navbar.tsx @@ -1,14 +1,11 @@ import { Navbar, NavbarCenter, NavbarLeft, NavbarRight } from '@renderer/components/app/Navbar' import { HStack } from '@renderer/components/Layout' import SearchPopup from '@renderer/components/Popups/SearchPopup' -import { isLinux, isMac, isWin } from '@renderer/config/constant' -import { useAssistant } from '@renderer/hooks/useAssistant' +import { isLinux, isWin } from '@renderer/config/constant' import { modelGenerating } from '@renderer/hooks/useRuntime' import { useSettings } from '@renderer/hooks/useSettings' import { useShortcut } from '@renderer/hooks/useShortcuts' import { useShowAssistants, useShowTopics } from '@renderer/hooks/useStore' -import { useChatMaxWidth } from '@renderer/pages/home/Chat' -import ChatNavbarContent from '@renderer/pages/home/components/ChatNavbarContent' import { EVENT_NAMES, EventEmitter } from '@renderer/services/EventService' import { useAppDispatch } from '@renderer/store' import { setNarrowMode } from '@renderer/store/settings' @@ -17,11 +14,10 @@ import { Tooltip } from 'antd' import { t } from 'i18next' import { Menu, PanelLeftClose, PanelRightClose, Search } from 'lucide-react' import { AnimatePresence, motion } from 'motion/react' -import React, { FC } from 'react' +import { FC } from 'react' import styled from 'styled-components' import AssistantsDrawer from './components/AssistantsDrawer' -import SelectModelButton from './components/SelectModelButton' import UpdateAppButton from './components/UpdateAppButton' interface Props { @@ -40,11 +36,9 @@ const HeaderNavbar: FC = ({ setActiveTopic, activeTopicOrSession }) => { - const { assistant } = useAssistant(activeAssistant.id) const { showAssistants, toggleShowAssistants } = useShowAssistants() const { topicPosition, narrowMode } = useSettings() const { showTopics, toggleShowTopics } = useShowTopics() - const chatMaxWidth = useChatMaxWidth() const dispatch = useAppDispatch() useShortcut('toggle_show_assistants', toggleShowAssistants) @@ -101,7 +95,7 @@ const HeaderNavbar: FC = ({ justifyContent: 'flex-start', borderRight: 'none', paddingLeft: 0, - paddingRight: 10, + paddingRight: 0, minWidth: 'auto' }}> @@ -123,22 +117,7 @@ const HeaderNavbar: FC = ({ )} - - {activeTopicOrSession === 'topic' ? ( - - - - ) : ( - - - - )} - - + = ({ - children, - style -}) => { - return ( -
      - {children} -
      - ) -} - export default HeaderNavbar diff --git a/src/renderer/src/pages/home/Tabs/AssistantsTab.tsx b/src/renderer/src/pages/home/Tabs/AssistantsTab.tsx index dc272b869a..952d0a2c07 100644 --- a/src/renderer/src/pages/home/Tabs/AssistantsTab.tsx +++ b/src/renderer/src/pages/home/Tabs/AssistantsTab.tsx @@ -180,7 +180,7 @@ const AssistantsTab: FC = (props) => { const Container = styled(Scrollbar)` display: flex; flex-direction: column; - padding: 10px; + padding: 12px 10px; ` export default AssistantsTab diff --git a/src/renderer/src/pages/home/Tabs/SessionSettingsTab.tsx b/src/renderer/src/pages/home/Tabs/SessionSettingsTab.tsx index 34b41a560c..099d22a72e 100644 --- a/src/renderer/src/pages/home/Tabs/SessionSettingsTab.tsx +++ b/src/renderer/src/pages/home/Tabs/SessionSettingsTab.tsx @@ -30,7 +30,7 @@ const SessionSettingsTab: FC = ({ session, update }) => { return (
      - + - ) -} +const SessionListItem = styled.div` + padding: 7px 12px; + border-radius: var(--list-item-border-radius); + font-size: 13px; + display: flex; + flex-direction: column; + justify-content: space-between; + cursor: pointer; + width: calc(var(--assistants-width) - 20px); + margin-bottom: 8px; -const SessionLabelContainer: React.FC> = ({ className, ...props }) => ( -
      -) + .menu { + opacity: 0; + color: var(--color-text-3); + } + + &:hover { + background-color: var(--color-list-item-hover); + transition: background-color 0.1s; + + .menu { + opacity: 1; + } + } + + &.active { + background-color: var(--color-list-item); + box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05); + .menu { + opacity: 1; + + &:hover { + color: var(--color-text-2); + } + } + } + + &.singlealone { + border-radius: 0 !important; + &:hover { + background-color: var(--color-background-soft); + } + &.active { + border-left: 2px solid var(--color-primary); + box-shadow: none; + } + } +` + +const SessionNameContainer = styled.div` + display: flex; + flex-direction: row; + align-items: center; + gap: 4px; + height: 20px; + justify-content: space-between; +` + +const SessionName = styled.div` + display: -webkit-box; + -webkit-line-clamp: 1; + -webkit-box-orient: vertical; + overflow: hidden; + font-size: 13px; + position: relative; +` + +const SessionEditInput = styled.input` + background: var(--color-background); + border: none; + color: var(--color-text-1); + font-size: 13px; + font-family: inherit; + padding: 2px 6px; + width: 100%; + outline: none; + padding: 0; +` + +const MenuButton = styled.div` + display: flex; + flex-direction: row; + justify-content: center; + align-items: center; + min-width: 20px; + min-height: 20px; + .anticon { + font-size: 12px; + } +` const PendingIndicator = styled.div.attrs({ className: 'animation-pulse' diff --git a/src/renderer/src/pages/home/Tabs/components/Sessions.tsx b/src/renderer/src/pages/home/Tabs/components/Sessions.tsx index 50020b168d..62b1df41de 100644 --- a/src/renderer/src/pages/home/Tabs/components/Sessions.tsx +++ b/src/renderer/src/pages/home/Tabs/components/Sessions.tsx @@ -12,7 +12,7 @@ import { } from '@renderer/store/runtime' import { CreateSessionForm } from '@renderer/types' import { buildAgentSessionTopicId } from '@renderer/utils/agentSession' -import { AnimatePresence, motion } from 'framer-motion' +import { motion } from 'framer-motion' import { memo, useCallback, useEffect } from 'react' import { useTranslation } from 'react-i18next' @@ -30,7 +30,7 @@ const Sessions: React.FC = ({ agentId }) => { const { agent } = useAgent(agentId) const { sessions, isLoading, error, deleteSession, createSession } = useSessions(agentId) const { chat } = useRuntime() - const { activeSessionIdMap, sessionWaiting } = chat + const { activeSessionIdMap } = chat const dispatch = useAppDispatch() const setActiveSessionId = useCallback( @@ -109,45 +109,30 @@ const Sessions: React.FC = ({ agentId }) => { if (error) return return ( - - - - {t('agent.session.add.title')} - - - - {/* h-9 */} - 9 * 4} - scrollerStyle={{ - // FIXME: This component only supports CSSProperties - overflowX: 'hidden' - }} - autoHideScrollbar> - {(session) => ( - - handleDeleteSession(session.id)} - onPress={() => setActiveSessionId(agentId, session.id)} - /> - - )} - - - +
      + + {t('agent.session.add.title')} + + {/* h-9 */} + 9 * 4} + scrollerStyle={{ + // FIXME: This component only supports CSSProperties + overflowX: 'hidden' + }} + autoHideScrollbar> + {(session) => ( + handleDeleteSession(session.id)} + onPress={() => setActiveSessionId(agentId, session.id)} + /> + )} + +
      ) } diff --git a/src/renderer/src/pages/home/Tabs/index.tsx b/src/renderer/src/pages/home/Tabs/index.tsx index c43c6d93a4..33d6473432 100644 --- a/src/renderer/src/pages/home/Tabs/index.tsx +++ b/src/renderer/src/pages/home/Tabs/index.tsx @@ -189,10 +189,7 @@ const Container = styled.div` background-color: var(--color-background); } [navbar-position='top'] & { - height: calc(100vh - var(--navbar-height) - 12px); - &.right { - height: calc(100vh - var(--navbar-height) - var(--navbar-height) - 12px); - } + height: calc(100vh - var(--navbar-height)); } overflow: hidden; .collapsed { diff --git a/src/renderer/src/pages/home/components/ChatNavbarContent.tsx b/src/renderer/src/pages/home/components/ChatNavbarContent.tsx index 154226973e..00b48cf8f4 100644 --- a/src/renderer/src/pages/home/components/ChatNavbarContent.tsx +++ b/src/renderer/src/pages/home/components/ChatNavbarContent.tsx @@ -1,13 +1,13 @@ -import { BreadcrumbItem, Breadcrumbs, Chip, cn } from '@heroui/react' +import { BreadcrumbItem, Breadcrumbs, cn } from '@heroui/react' import HorizontalScrollContainer from '@renderer/components/HorizontalScrollContainer' -import { permissionModeCards } from '@renderer/constants/permissionModes' import { useActiveAgent } from '@renderer/hooks/agents/useActiveAgent' import { useActiveSession } from '@renderer/hooks/agents/useActiveSession' import { useUpdateSession } from '@renderer/hooks/agents/useUpdateSession' import { useRuntime } from '@renderer/hooks/useRuntime' -import { AgentEntity, AgentSessionEntity, ApiModel, Assistant, PermissionMode } from '@renderer/types' +import { AgentEntity, AgentSessionEntity, ApiModel, Assistant } from '@renderer/types' import { formatErrorMessageWithPrefix } from '@renderer/utils/error' import { t } from 'i18next' +import { Folder } from 'lucide-react' import { FC, ReactNode, useCallback } from 'react' import { AgentSettingsPopup, SessionSettingsPopup } from '../../settings/AgentSettings' @@ -38,24 +38,15 @@ const ChatNavbarContent: FC = ({ assistant }) => { <> {activeTopicOrSession === 'topic' && } {activeTopicOrSession === 'session' && activeAgent && ( - - + + AgentSettingsPopup.show({ agentId: activeAgent.id })} - classNames={{ - base: 'self-stretch', - item: 'h-full' - }}> - - - + classNames={{ base: 'self-stretch', item: 'h-full' }}> + {activeSession && ( = ({ assistant }) => { sessionId: activeSession.id }) } - classNames={{ - base: 'self-stretch', - item: 'h-full' - }}> - - - + classNames={{ base: 'self-stretch', item: 'h-full' }}> + )} {activeSession && ( @@ -97,11 +83,11 @@ const SessionWorkspaceMeta: FC<{ agent: AgentEntity; session: AgentSessionEntity } const firstAccessiblePath = session.accessible_paths?.[0] - const permissionMode = (session.configuration?.permission_mode ?? 'default') as PermissionMode - const permissionModeCard = permissionModeCards.find((card) => card.mode === permissionMode) - const permissionModeLabel = permissionModeCard - ? t(permissionModeCard.titleKey, permissionModeCard.titleFallback) - : permissionMode + // const permissionMode = (session.configuration?.permission_mode ?? 'default') as PermissionMode + // const permissionModeCard = permissionModeCards.find((card) => card.mode === permissionMode) + // const permissionModeLabel = permissionModeCard + // ? t(permissionModeCard.titleKey, permissionModeCard.titleFallback) + // : permissionMode const infoItems: ReactNode[] = [] @@ -117,12 +103,13 @@ const SessionWorkspaceMeta: FC<{ agent: AgentEntity; session: AgentSessionEntity }) => (
      + {text}
      ) @@ -148,7 +135,7 @@ const SessionWorkspaceMeta: FC<{ agent: AgentEntity; session: AgentSessionEntity ) } - infoItems.push() + // infoItems.push() if (infoItems.length === 0) { return null diff --git a/src/renderer/src/pages/home/components/SelectAgentBaseModelButton.tsx b/src/renderer/src/pages/home/components/SelectAgentBaseModelButton.tsx index 74f69cf15e..0a2e6c1fbb 100644 --- a/src/renderer/src/pages/home/components/SelectAgentBaseModelButton.tsx +++ b/src/renderer/src/pages/home/components/SelectAgentBaseModelButton.tsx @@ -38,12 +38,12 @@ const SelectAgentBaseModelButton: FC = ({ agentBase: agent, onSelect, isD
      ) } From 9d45991181c8a196c51ebde31d9bf51a5c17b658 Mon Sep 17 00:00:00 2001 From: kangfenmao Date: Sat, 18 Oct 2025 11:14:16 +0800 Subject: [PATCH 24/27] chore: update release notes for v1.7.0-beta.2 - Added new features including session settings management, full-text search for notes, and integration with DiDi MCP server. - Improved agent model selection and added support for Mistral AI and NewAPI providers. - Enhanced UI/UX with navbar layout consistency and chat component responsiveness. - Fixed various bugs related to assistant creation, streaming issues, and message layout. --- electron-builder.yml | 144 ++++++++++++++----------------------------- package.json | 2 +- 2 files changed, 47 insertions(+), 99 deletions(-) diff --git a/electron-builder.yml b/electron-builder.yml index 8577a307a2..f28328eee0 100644 --- a/electron-builder.yml +++ b/electron-builder.yml @@ -126,112 +126,60 @@ artifactBuildCompleted: scripts/artifact-build-completed.js releaseInfo: releaseNotes: | - What's New in v1.7.0-beta.1 + What's New in v1.7.0-beta.2 - Major Features: - - Agent System: Introducing intelligent Agent capabilities alongside Assistants. Agents can autonomously solve complex problems using Claude Code SDK with tool calling, file operations, and multi-turn reasoning - - Agent Management: Create, configure, and manage agents with custom settings including model selection, tool permissions, accessible paths, and MCP server integrations - - Agent Sessions: Dedicated session management for agent interactions with persistent message history and context tracking - - Unified UI: Streamlined interface combining Assistants and Agents tabs with improved navigation and settings management + New Features: + - Session Settings: Manage session-specific settings and model configurations independently + - Notes Full-Text Search: Search across all notes with match highlighting + - Built-in DiDi MCP Server: Integration with DiDi ride-hailing services (China only) + - Intel OV OCR: Hardware-accelerated OCR using Intel NPU + - Auto-start API Server: Automatically starts when agents exist - Agent Features: - - Tool Support: Web search, file operations, bash commands, and custom MCP tools - - Advanced Configuration: Max turns, temperature, token limits - - Permission Control: Configurable tool approval modes (manual, automatic, none) - - Session Persistence: Automatic message saving with optimized streaming and database integration - - Model Selection: API-based model filtering with provider-specific support - - UI/UX Improvements: - - Unified assistant/agent tabs with smooth animations - - In-place session name editing - - Virtual list rendering for improved performance - - Session count indicators for active agents - - Enhanced settings popup with tabbed interface - - Webview keyboard shortcut interception for search functionality - - API & Infrastructure: - - RESTful API for agent and session management - - Drizzle ORM integration for agent database - - OAuth support for Claude Code authentication - - Express validator for request validation - - Comprehensive error handling with Zod schemas - - Model Updates: - - Gemini 2.5 Image Flash support - - Grok 4 Fast with reasoning capabilities - - Qwen3-omni and Qwen3-vl thinking models - - DeepSeek, Claude 4.5, GLM 4.6 support - - GitHub Copilot CLI integration with gpt-5-codex + Improvements: + - Agent model selection now requires explicit user choice + - Added Mistral AI provider support + - Added NewAPI generic provider support + - Improved navbar layout consistency across different modes + - Enhanced chat component responsiveness + - Better code block display on small screens + - Updated OVMS to 2025.3 official release + - Added Greek language support Bug Fixes: - - Fix Swagger UI accessibility issues - - Fix AI SDK error display with syntax highlighting - - Fix webview search shortcut handling - - Fix agent model visibility for CherryIn provider - - Fix session message ordering and persistence - - Fix anthropic model visibility in agent configuration - - Fix knowledge base deletion and web search RAG errors - - Fix migration for missing providers - - Technical Updates: - - React 19.2.0 upgrade - - Enhanced Claude Code service with streaming support - - Improved message transformation and streaming lifecycle - - Database migration system with automatic schema sync - - Optimized bundle size and dependency management + - Fixed GitHub Copilot gpt-5-codex streaming issues + - Fixed assistant creation failures + - Fixed translate auto-copy functionality + - Fixed miniapps external link opening + - Fixed message layout and overflow issues + - Fixed API key parsing to preserve spaces + - Fixed agent display in different navbar layouts - v1.7.0-beta.1 新特性 + v1.7.0-beta.2 新特性 - 核心功能: - - Agent 系统:引入智能 Agent 能力,与助手(Assistant)并存。Agent 基于 Claude Code SDK 构建,具备工具调用、文件操作和多轮推理能力,可自主解决复杂问题 - - Agent 管理:创建、配置和管理 Agent,支持自定义模型选择、工具权限、可访问路径和 MCP 服务器集成 - - Agent 会话:专属会话管理系统,支持持久化消息历史和上下文追踪 - - 统一界面:精简的助手和 Agent 标签页界面,改进导航和设置管理体验 + 新功能: + - 会话设置:独立管理会话特定的设置和模型配置 + - 笔记全文搜索:跨所有笔记搜索并高亮匹配内容 + - 内置滴滴 MCP 服务器:集成滴滴打车服务(仅限中国地区) + - Intel OV OCR:使用 Intel NPU 的硬件加速 OCR + - 自动启动 API 服务器:当存在 Agent 时自动启动 - Agent 功能特性: - - 工具支持:网页搜索、文件操作、Bash 命令执行和自定义 MCP 工具 - - 高级配置:最大轮次、温度、Token 限制 - - 权限控制:可配置的工具批准模式(手动、自动、无需批准) - - 会话持久化:自动消息保存,优化的流式传输和数据库集成 - - 模型选择:基于 API 的模型过滤,支持特定提供商 - - 界面与交互优化: - - 统一的助手/Agent 标签页,带有流畅动画效果 - - 会话名称原地编辑功能 - - 虚拟列表渲染,提升性能表现 - - 活跃 Agent 的会话计数指示器 - - 增强的设置弹窗,采用标签页界面 - - Webview 键盘快捷键拦截,支持搜索功能 - - API 与基础设施: - - RESTful API 用于 Agent 和会话管理 - - 集成 Drizzle ORM 管理 Agent 数据库 - - Claude Code OAuth 认证支持 - - Express validator 请求验证 - - 基于 Zod 模式的完善错误处理 - - 模型更新: - - 支持 Gemini 2.5 Image Flash - - Grok 4 Fast 推理能力 - - Qwen3-omni 和 Qwen3-vl 思考模型 - - DeepSeek、Claude 4.5、GLM 4.6 支持 - - GitHub Copilot CLI 集成 gpt-5-codex + 改进: + - Agent 模型选择现在需要用户显式选择 + - 添加 Mistral AI 提供商支持 + - 添加 NewAPI 通用提供商支持 + - 改进不同模式下的导航栏布局一致性 + - 增强聊天组件响应式设计 + - 优化小屏幕代码块显示 + - 更新 OVMS 至 2025.3 正式版 + - 添加希腊语支持 问题修复: - - 修复 Swagger UI 无法打开 - - 修复 AI SDK 错误显示,添加语法高亮 - - 修复 Webview 搜索快捷键处理 - - 修复 CherryIn 提供商的 Agent 模型可见性 - - 修复会话消息排序和持久化 - - 修复 Anthropic 模型在 Agent 配置中的可见性 - - 修复知识库删除和网页搜索 RAG 错误 - - 修复缺失提供商的迁移问题 - - 技术更新: - - 升级至 React 19.2.0 - - 增强 Claude Code 服务流式传输支持 - - 改进消息转换和流式生命周期 - - 数据库迁移系统,支持自动模式同步 - - 优化打包大小和依赖管理 + - 修复 GitHub Copilot gpt-5-codex 流式传输问题 + - 修复助手创建失败 + - 修复翻译自动复制功能 + - 修复小程序外部链接打开 + - 修复消息布局和溢出问题 + - 修复 API 密钥解析以保留空格 + - 修复不同导航栏布局中的 Agent 显示 diff --git a/package.json b/package.json index 1dad6a9f5a..6d59cfff49 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "CherryStudio", - "version": "1.7.0-beta.1", + "version": "1.7.0-beta.2", "private": true, "description": "A powerful AI assistant for producer.", "main": "./out/main/index.js", From 595fd878a65f78d8505273084306cd301299da9c Mon Sep 17 00:00:00 2001 From: SuYao Date: Sat, 18 Oct 2025 14:10:00 +0800 Subject: [PATCH 25/27] fix: handle AISDKError in chunk processing (#10801) --- .../src/aiCore/chunk/AiSdkToChunkAdapter.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/renderer/src/aiCore/chunk/AiSdkToChunkAdapter.ts b/src/renderer/src/aiCore/chunk/AiSdkToChunkAdapter.ts index f4894a203f..3f27f9440c 100644 --- a/src/renderer/src/aiCore/chunk/AiSdkToChunkAdapter.ts +++ b/src/renderer/src/aiCore/chunk/AiSdkToChunkAdapter.ts @@ -10,7 +10,7 @@ import { ProviderSpecificError } from '@renderer/types/provider-specific-error' import { formatErrorMessage } from '@renderer/utils/error' import { convertLinks, flushLinkConverterBuffer } from '@renderer/utils/linkConverter' import type { ClaudeCodeRawValue } from '@shared/agents/claudecode/types' -import type { TextStreamPart, ToolSet } from 'ai' +import { AISDKError, type TextStreamPart, type ToolSet } from 'ai' import { ToolCallChunkHandler } from './handleToolCallChunk' @@ -357,11 +357,14 @@ export class AiSdkToChunkAdapter { case 'error': this.onChunk({ type: ChunkType.ERROR, - error: new ProviderSpecificError({ - message: formatErrorMessage(chunk.error), - provider: 'unknown', - cause: chunk.error - }) + error: + chunk.error instanceof AISDKError + ? chunk.error + : new ProviderSpecificError({ + message: formatErrorMessage(chunk.error), + provider: 'unknown', + cause: chunk.error + }) }) break From dc0f9c5f0809cbd86ece47941178fc0ed3f34450 Mon Sep 17 00:00:00 2001 From: SuYao Date: Sat, 18 Oct 2025 14:10:50 +0800 Subject: [PATCH 26/27] feat: add Claude Haiku 4.5 model support and update related regex patterns (#10800) * feat: add Claude Haiku 4.5 model support and update related regex patterns * fix: update Claude model token limits for consistency --- src/renderer/src/config/models/default.ts | 6 ++++++ src/renderer/src/config/models/reasoning.ts | 8 +++++--- src/renderer/src/config/models/vision.ts | 1 + src/renderer/src/config/models/websearch.ts | 2 +- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/renderer/src/config/models/default.ts b/src/renderer/src/config/models/default.ts index 34fc3688c8..c3a40e6ef7 100644 --- a/src/renderer/src/config/models/default.ts +++ b/src/renderer/src/config/models/default.ts @@ -430,6 +430,12 @@ export const SYSTEM_MODELS: Record = } ], anthropic: [ + { + id: 'claude-haiku-4-5-20251001', + provider: 'anthropic', + name: 'Claude Haiku 4.5', + group: 'Claude 4.5' + }, { id: 'claude-sonnet-4-5-20250929', provider: 'anthropic', diff --git a/src/renderer/src/config/models/reasoning.ts b/src/renderer/src/config/models/reasoning.ts index 5c89a77ba3..1ef0303f61 100644 --- a/src/renderer/src/config/models/reasoning.ts +++ b/src/renderer/src/config/models/reasoning.ts @@ -335,7 +335,8 @@ export function isClaudeReasoningModel(model?: Model): boolean { modelId.includes('claude-3-7-sonnet') || modelId.includes('claude-3.7-sonnet') || modelId.includes('claude-sonnet-4') || - modelId.includes('claude-opus-4') + modelId.includes('claude-opus-4') || + modelId.includes('claude-haiku-4') ) } @@ -493,8 +494,9 @@ export const THINKING_TOKEN_MAP: Record = 'qwen3-(?!max).*$': { min: 1024, max: 38_912 }, // Claude models - 'claude-3[.-]7.*sonnet.*$': { min: 1024, max: 64000 }, - 'claude-(:?sonnet|opus)-4.*$': { min: 1024, max: 32000 } + 'claude-3[.-]7.*sonnet.*$': { min: 1024, max: 64_000 }, + 'claude-(:?haiku|sonnet)-4.*$': { min: 1024, max: 64_000 }, + 'claude-opus-4-1.*$': { min: 1024, max: 32_000 } } export const findTokenLimit = (modelId: string): { min: number; max: number } | undefined => { diff --git a/src/renderer/src/config/models/vision.ts b/src/renderer/src/config/models/vision.ts index cd09e6e34b..ceff0a10c3 100644 --- a/src/renderer/src/config/models/vision.ts +++ b/src/renderer/src/config/models/vision.ts @@ -15,6 +15,7 @@ const visionAllowedModels = [ 'gemini-(flash|pro|flash-lite)-latest', 'gemini-exp', 'claude-3', + 'claude-haiku-4', 'claude-sonnet-4', 'claude-opus-4', 'vision', diff --git a/src/renderer/src/config/models/websearch.ts b/src/renderer/src/config/models/websearch.ts index ecdf1bec37..f0614f714f 100644 --- a/src/renderer/src/config/models/websearch.ts +++ b/src/renderer/src/config/models/websearch.ts @@ -7,7 +7,7 @@ import { isAnthropicModel } from './utils' import { isPureGenerateImageModel, isTextToImageModel } from './vision' export const CLAUDE_SUPPORTED_WEBSEARCH_REGEX = new RegExp( - `\\b(?:claude-3(-|\\.)(7|5)-sonnet(?:-[\\w-]+)|claude-3(-|\\.)5-haiku(?:-[\\w-]+)|claude-sonnet-4(?:-[\\w-]+)?|claude-opus-4(?:-[\\w-]+)?)\\b`, + `\\b(?:claude-3(-|\\.)(7|5)-sonnet(?:-[\\w-]+)|claude-3(-|\\.)5-haiku(?:-[\\w-]+)|claude-(haiku|sonnet|opus)-4(?:-[\\w-]+)?)\\b`, 'i' ) From b4810bb487b5938b7126d319b4f1bc64ef227c7b Mon Sep 17 00:00:00 2001 From: Pleasure1234 <3196812536@qq.com> Date: Sat, 18 Oct 2025 07:15:08 +0100 Subject: [PATCH 27/27] fix: improve api-server startup and error handling logic (#10794) * fix: improve server startup and error handling logic Refactored ApiServer to clean up failed server instances and ensure proper handling of server state. Updated loggerService import to use shared logger and improved error handling during server startup. * Update server.ts --- src/main/apiServer/server.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/apiServer/server.ts b/src/main/apiServer/server.ts index 0cba77aaa3..3cb81f4124 100644 --- a/src/main/apiServer/server.ts +++ b/src/main/apiServer/server.ts @@ -1,7 +1,8 @@ import { createServer } from 'node:http' +import { loggerService } from '@logger' + import { agentService } from '../services/agents' -import { loggerService } from '../services/LoggerService' import { app } from './app' import { config } from './config' @@ -15,11 +16,17 @@ export class ApiServer { private server: ReturnType | null = null async start(): Promise { - if (this.server) { + if (this.server && this.server.listening) { logger.warn('Server already running') return } + // Clean up any failed server instance + if (this.server && !this.server.listening) { + logger.warn('Cleaning up failed server instance') + this.server = null + } + // Load config const { port, host } = await config.load() @@ -39,7 +46,11 @@ export class ApiServer { resolve() }) - this.server!.on('error', reject) + this.server!.on('error', (error) => { + // Clean up the server instance if listen fails + this.server = null + reject(error) + }) }) }