Compare commits

..

136 Commits

Author SHA1 Message Date
suyao a1f61b0d2e fix: ci 2025-07-18 01:29:06 +08:00
suyao 155dc1c578 fix: conflict 2025-07-18 01:21:53 +08:00
suyao 5e33a91154 Merge branch 'main' into feat/openai-deepsearch 2025-07-18 01:21:23 +08:00
George·Dong ee32942f71 feat(minapp): add Google login tip for untrusted browser issue (#8230)
* feat(minapp): add Google login tip for untrusted browser issue

* feat(miniapp): add open Google button for Google Login popup

* feat(minapp): replace custom alert with Ant Design Alert component
2025-07-18 00:22:55 +08:00
fullex 4a4d861592 docs: Update SECURITY.md
change email report to Security page report
2025-07-17 23:00:14 +08:00
kangfenmao 42c66552c8 chore(version): 1.5.1 2025-07-17 19:53:06 +08:00
kangfenmao dc6ec2ba78 refactor(ThinkingEffect): adjust container height and padding for improved layout 2025-07-17 19:43:57 +08:00
Teo 7fb7061ca7 refactor(ThinkingEffect): improve thinking effect logic and styles 2025-07-17 19:43:13 +08:00
Teo e85ea1ff28 refactor(ThinkingEffect): optimize thinking effect (#8232)
* refactor(ThinkingEffect): optimize message rendering and adjust styles

- Removed unnecessary motion components and simplified message rendering logic.
- Updated line height and container dimensions for better layout consistency.
- Adjusted padding and width in styled components for improved visual appearance.

* fix(ThinkingBlock): remove margin-top from snapshot for consistent styling

* refactor(Message): remove unnecessary padding adjustments for assistant messages
2025-07-17 17:40:56 +08:00
SuYao fe91d4b56a fix(OpenAIResponseAPIClient): refine client selection logic for non-chat models (#8238)
- Updated the getClient method to ensure the OpenAIResponseAPIClient is only returned for non-chat completion models, improving model compatibility checks.
2025-07-17 17:38:20 +08:00
Jason Young 9218ac237b test: add comprehensive tests for ApiClientFactory (#8124)
* test: add comprehensive tests for ApiClientFactory

- Test all special ID client mappings (aihubmix, new-api, ppio)
- Test all standard provider type mappings
- Test edge cases and default behavior
- Test isOpenAIProvider utility function
- Achieve full coverage of factory logic

* test: fix ApiClientFactory test for OpenAIResponseAPIClient changes

- Add getClient mock method to OpenAIResponseAPIClient mock
- Fix provider id from 'azure' to 'azure-openai' to match actual configuration
- Ensure tests properly reflect the new OpenAIResponseAPIClient implementation

* test: refactor ApiClientFactory tests and move isOpenAIProvider to utils

- Simplify test data creation with createTestProvider helper
- Move isOpenAIProvider to utils and fix vertexai handling
- Update related imports
2025-07-17 16:59:18 +08:00
SuYao 6560369b98 refactor(ActionUtils): streamline message processing logic (#8226)
* refactor(ActionUtils): streamline message processing logic

- Removed unnecessary content accumulation for thinking and text blocks.
- Updated handling of message chunks to directly use incoming text for updates.
- Improved state management for thinking and text blocks during streaming.
- Enhanced the logic for creating and updating message blocks to ensure proper status and content handling.

* chore: remove log

* feat(ActionUtils): update message block instruction during processing

- Added dispatch to update the message block instruction with the current block ID when processing messages.
- Enhanced state management for message updates to ensure accurate tracking of block instructions during streaming.

* feat(ActionUtils): enhance message processing with text block content tracking

- Introduced a new variable to store the content of the text block during message processing.
- Updated the logic to dispatch the current text block content upon completion of message chunks, improving state management and accuracy in message updates.

* feat(ActionUtils): refine message processing error handling and status updates

- Enhanced the logic to update the message status based on error conditions, ensuring accurate representation of message states.
- Improved handling of text block content during processing, allowing for better state management and completion tracking.
- Streamlined the dispatch of updates for message blocks, particularly in error scenarios, to maintain consistency in message processing.

* feat(messageThunk): export throttled block update functions for improved message processing

- Changed the visibility of `throttledBlockUpdate` and `cancelThrottledBlockUpdate` functions to export, allowing their use in other modules.
- Updated `processMessages` in ActionUtils to utilize the newly exported functions for handling message updates, enhancing the efficiency of block updates during message processing.

* fix(ActionUtils): correct text block content handling in message processing

- Changed the declaration of `textBlockContent` to a constant to prevent unintended modifications.
- Updated the logic in `processMessages` to use the text block ID for throttled updates instead of the content, ensuring accurate message updates during processing.

* feat(HomeWindow): improve message processing with throttled updates

- Integrated `throttledBlockUpdate` and `cancelThrottledBlockUpdate` for managing thinking and text block updates, enhancing performance during message streaming.
- Updated logic to handle chunk types more effectively, ensuring accurate content updates and status management for message blocks.
- Streamlined the dispatch of message updates upon completion and error handling, improving overall state management.
2025-07-17 16:09:43 +08:00
kangfenmao 30b080efbd fix: handle optional list length in DraggableVirtualList and update padding in QuickPanel 2025-07-17 13:49:11 +08:00
kangfenmao f01b7075fb refactor: replace message with window.message 2025-07-17 13:21:00 +08:00
beyondkmp ff72c007c0 feat: add data parsing functionality in handleProvidersProtocolUrl (#8218)
* feat: add data parsing functionality in handleProvidersProtocolUrl

- Introduced a new ParseData function to decode and parse base64 encoded data from the URL parameters.
- Added error handling to log when data is null or invalid, improving robustness of the handleProvidersProtocolUrl function.

* fix: update data parsing in handleProvidersProtocolUrl and ProvidersList

- Modified ParseData function to return a JSON string instead of an object for consistency.
- Simplified data extraction in ProvidersList by directly parsing the addProviderData without base64 decoding, improving readability and performance.

* fix: improve data parsing in handleProvidersProtocolUrl

- Updated ParseData function to log the parsed result for better debugging.
- Enhanced data extraction by replacing URL-safe characters back to their original form before parsing, ensuring accurate data retrieval.

* fix: enhance error logging in ParseData function

- Updated the ParseData function to log errors when parsing fails, improving debugging capabilities and robustness in handling invalid data.

* format code
2025-07-17 13:15:29 +08:00
Konv Suu 6bdb157af3 fix: set os attribute correctly to body (#8225)
* feat: add os constants

* update
2025-07-17 12:03:21 +08:00
fullex 04afa61d55 fix(SelectionService): actionWindow show in center screen when in multi screen (#8133)
fix(SelectionService): round center coordinates for action window positioning
2025-07-17 11:50:37 +08:00
SuYao 7549972048 hotfix: enhance assistant topic validation in useActiveTopic hook (#8213)
fix: enhance assistant topic validation in useActiveTopic hook

Updated the useActiveTopic hook to ensure that the assistant and its topics are properly validated before accessing properties. This prevents potential errors when data is not fully loaded.
2025-07-17 11:48:25 +08:00
SuYao 720c5d6080 fix: thinking not display (#8222)
* feat(ThinkingTagExtraction): accumulate thinking content for improved processing

- Introduced an `accumulatedThinkingContent` variable to gather content from multiple chunks before enqueuing.
- Updated the `ThinkingDeltaChunk` to use the accumulated content instead of individual extraction results, enhancing the coherence of thinking messages.

* feat(OpenAIAPIClient): enhance chunk processing for reasoning and content extraction

- Updated the OpenAIAPIClient to handle additional fields in response chunks, including `reasoning_content` and `reasoning`, improving the extraction of relevant information.
- Introduced a new mock implementation for testing OpenAI completions, ensuring accurate handling of thinking and text chunks in the response.
- Enhanced unit tests to validate the processing of OpenAI thinking chunks, ensuring expected behavior and output.
2025-07-17 11:40:15 +08:00
dependabot[bot] e7d38d340f chore(deps): bump tar-fs from 2.1.2 to 2.1.3 (#8221)
---
updated-dependencies:
- dependency-name: tar-fs
  dependency-version: 2.1.3
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-07-17 10:46:15 +08:00
Phantom d750f1ceed feat(Knowledge): show dimensions (#8169)
* feat(知识设置): 添加维度显示并调整弹窗高度

在知识设置弹窗中添加维度显示字段,并将弹窗高度从450px调整为550px以适应新增内容

* fix(知识设置): 将维度输入框改为显示未设置时的默认文本
2025-07-17 10:04:16 +08:00
MyPrototypeWhat 7e471bfea4 feat: implement BlockManager and associated callbacks for message str… (#8167)
* feat: implement BlockManager and associated callbacks for message streaming

- Introduced BlockManager to manage message blocks with smart update strategies.
- Added various callback handlers for different message types including text, image, citation, and tool responses.
- Enhanced state management for active blocks and transitions between different message types.
- Created utility functions for handling block updates and transitions, improving overall message processing flow.
- Refactored message thunk to utilize BlockManager for better organization and maintainability.

This implementation lays the groundwork for more efficient message streaming and processing in the application.

* refactor: clean up BlockManager and callback implementations

- Removed redundant assignments of lastBlockType in various callback files.
- Updated error handling logic to ensure correct message status updates.
- Added console logs for debugging purposes in BlockManager and citation callbacks.
- Enhanced smartBlockUpdate method call in citation callbacks for better state management.

* refactor: streamline BlockManager and callback logic

- Removed unnecessary accumulated content variables in text and thinking callbacks.
- Updated content handling in callbacks to directly use incoming text instead of accumulating.
- Enhanced smartBlockUpdate calls for better state management in message streaming.
- Cleaned up console log statements for improved readability and debugging.
2025-07-17 10:03:14 +08:00
one aa254a3772 refactor(Markdown): disable single-tilde strikethrough (#8209) 2025-07-17 09:57:37 +08:00
2h0ng ff0994e1c7 Create SECURITY.md (#8158) 2025-07-17 09:53:05 +08:00
beyondkmp 3cd1dece52 chore: update package dependencies and add undici version 6.21.2 (#8215)
* chore: update package dependencies and add undici version 7.10.0

* chore: downgrade undici version from 7.10.0 to 6.21.2 in package.json and yarn.lock

* chore: update yarn.lock to reflect dependency version changes and removals
2025-07-17 09:30:21 +08:00
one 9ac2b70df3 fix: repect multi-model style on model mentioning (#8204) 2025-07-17 09:28:13 +08:00
beyondkmp 2d6c05e962 feat: enhance proxy management and configuration (#8164)
* feat: enhance proxy management and configuration

- Added support for new proxy modes and improved proxy configuration handling.
- Replaced AxiosProxy with direct axios usage for HTTP requests.
- Introduced fetch-socks and undici for better proxy handling.
- Updated IPC and ConfigManager to accommodate new proxy settings.
- Removed deprecated AxiosProxy service to streamline codebase.

* format code

* feat: improve proxy configuration and monitoring

- Introduced a new mechanism to monitor system proxy changes and update configurations accordingly.
- Enhanced the configureProxy method to prevent concurrent executions and added error logging with electron-log.
- Refactored proxy handling logic to streamline the setting of global and session proxies.
- Removed deprecated methods related to proxy management for cleaner code.

* update yarn.lock

* fix: update proxy configuration logic to handle direct mode

- Modified the app's ready event to check for 'direct' mode before configuring the proxy.
- Ensured that the proxy configuration is only applied when necessary, improving efficiency.

* feat: enhance proxy configuration to support authentication

- Added userId and password fields to the proxy configuration for SOCKS connections.
- Improved handling of proxy credentials to allow for authenticated proxy usage.

* refactor: remove deprecated proxy methods and streamline configuration logic

- Eliminated the setProxy and getProxy methods from ConfigManager to simplify the proxy configuration process.
- Updated ProxyManager to initialize with a default proxy configuration and removed unnecessary checks for 'direct' mode during initialization.
- Enhanced logging for proxy configuration changes to improve traceability.

* format code

* feat: enhance WebDav and ProxyManager for self-signed certificate support

- Added handling for self-signed certificates in ProxyManager to allow secure connections with custom agents.
- Updated WebDav configuration to include an https.Agent with rejectUnauthorized set to false, facilitating connections to servers with self-signed certificates.

* delete global setting for rejectUnauthorized
2025-07-16 21:46:06 +08:00
Phantom 8384bbfc0a fix: handle mentions when resending message (#7819)
* fix(messageThunk): 修复重置消息时模型未正确继承的问题

* fix(消息重发): 修复重发消息时模型选择逻辑

确保当原始消息模型被提及时才使用该模型,否则使用助手默认模型

* style(PasteService): 统一文件换行符为LF格式

* Revert "style(PasteService): 统一文件换行符为LF格式"

This reverts commit 37a1443b73.

* refactor(messageThunk): 优化消息重发逻辑,分离新旧消息处理

将消息重发逻辑拆分为处理已有消息和新增提及模型消息两部分
简化条件判断,移除冗余代码

* style(messageThunk): 移除多余的空行

* fix(消息重传): 单条无提及消息重传时使用助手模型

当重传单条无提及消息时,使用助手模型进行重传,其他情况保持原有逻辑

* Revert "fix(消息重传): 单条无提及消息重传时使用助手模型"

This reverts commit 2e369174e7.

* fix(消息重发): 修改重发消息时模型设置逻辑
2025-07-16 19:36:45 +08:00
kangfenmao 31ee7a2e9a chore(version): 1.5.0 2025-07-16 17:55:45 +08:00
one f84509c824 chore: update check-i18n scripts and remove duplicate keys (#8203) 2025-07-16 17:44:07 +08:00
⌞L⌝ f0d86cbaec feat: add support for 302AI provider in MCP settings (#7755)
* feat: add support for 302AI provider in MCP settings

- Introduced new provider for 302AI, including token management and server synchronization functionality.
- Updated SyncServersPopup to integrate 302AI provider.
- Added new file for 302AI provider utilities, including token storage and server fetching logic.

* fix: re-merge main
2025-07-16 17:40:30 +08:00
fullex 3132150fb8 Revert "feat: optimize minapp cache with LRU (#8160)" (#8205)
This reverts commit f0043b4be5.
2025-07-16 17:34:50 +08:00
自由的世界人 24f7bac3ea refactor: custom mini app loading logic (#8181)
* refactor: custom mini app loading logic

Replaces try-catch with an explicit file existence check before reading 'custom-minapps.json'. Ensures the file is created with an empty array if it does not exist, improving clarity and error handling.

* refactor: custom mini app loading logic

Simplifies the loading of custom mini apps by removing the explicit file existence check and handling the read failure case directly. If reading the file fails, an empty array is written and returned.

* fix: improve error handling in file reading for custom mini apps
2025-07-16 15:36:20 +08:00
SuYao 0930201e5d Fix/mcp bug (#8189)
* feat(models): enhance function calling model detection and update migration logic

- Added support for 'gemini-1' in FUNCTION_CALLING_EXCLUDED_MODELS.
- Updated isFunctionCallingModel to handle optional model input.
- Modified migration logic to change tool use mode for assistants using function calling models.

* feat(models): add new models to vision and function calling lists

- Added 'kimi-thinking-preview' to visionAllowedModels.
- Added 'kimi-k2' to FUNCTION_CALLING_MODELS.
- Updated migration logic to ensure compatibility with new model settings.

* refactor(TextChunkMiddleware): streamline text accumulation logic and improve response handling

- Simplified the logic for accumulating text content and updating the internal state.
- Ensured that the final text is consistently used in response callbacks.
- Removed redundant code for handling text completion in the ToolUseExtractionMiddleware.
- Added mock state for MCP tools in tests to enhance coverage for tool use extraction.

* refactor(BaseApiClient): remove unused content extraction utility

- Replaced the usage of getContentWithTools with getMainTextContent in the getMessageContent method.
- Cleaned up imports by removing the unused getContentWithTools function.
2025-07-16 15:04:19 +08:00
one df218ee6c8 hotfix: error on deleting assistant (#8190)
fix: error on deleting assistant
2025-07-16 14:16:08 +08:00
happyZYM 27c39415c2 fix: add compatibility for webdav servers that do not support streaming (#7992)
* fix: add compatibility for webdav servers that do not support streaming

* fix: fix grammar error

* fix: fix linter error

* fix: remove unnecessary changes

* revert: restore tolerance for failing to remove temp file after webdav backup failed

* fix: add migration support
2025-07-16 09:53:51 +08:00
luoxu1314 f155b98a92 feat(MCPService):Add notification handlers and clear cache for MCPService (#8179)
* feat(MCPService):Add notification handlers MCPService

添加了MCPService 接收通知更新对应list和其他追踪

* Update MCPService.ts:合并清理缓存
2025-07-16 09:39:10 +08:00
Konv Suu f0043b4be5 feat: optimize minapp cache with LRU (#8160) 2025-07-15 22:56:34 +08:00
自由的世界人 a6db53873a fix: add channel property to notifications for backup and assistant messages (#8120)
* fix: add channel property to notifications for backup and assistant messages

* Add notification tip and improve assistant notification logic

Added a tooltip in the notification settings UI to clarify that only messages exceeding 30 seconds will trigger a reminder. Updated i18n files for all supported languages with the new tip. Modified notification logic to only send notifications for assistant responses or errors if the message duration exceeds 30 seconds and the user is not on the home page or the window is not focused.

* Remove duplicate InfoCircleOutlined import

Consolidated the import of InfoCircleOutlined from '@ant-design/icons' to avoid redundancy in GeneralSettings.tsx.

* fix: add isFocused mock and simplify createMockStore

Added a mock for isFocused in the window utility and refactored createMockStore to return the configured store directly. This improves test setup clarity and ensures all necessary window utilities are mocked.
2025-07-15 19:25:55 +08:00
fullex 397965f6e9 fix(WindowService): miniWindow should show in current screen (#8132)
feat(WindowService): enhance mini window behavior and resizing logic

- Introduced dynamic resizing for the mini window based on user adjustments.
- Implemented positioning logic to ensure the mini window appears centered on the cursor's screen.
- Added opacity handling to improve user experience during window state changes.
- Refactored mini window creation to utilize predefined size constants for better maintainability.
2025-07-15 18:10:30 +08:00
SuYao 76de357cbf test: add integration test for message thunk and fix some bugs (#8148)
* test: add integration test for message thunk and fix some bugs

* fix: ci
2025-07-15 15:39:40 +08:00
SuYao 40724ad877 fix(AihubmixAPIClient): enhance ID validation logic to exclude 'embed… (#8157)
fix(AihubmixAPIClient): enhance ID validation logic to exclude 'embedding' (#8148)
2025-07-15 14:08:38 +08:00
kangfenmao be6ecbe0b1 refactor(SettingsPage): Remove redundant menu items and reorganize memory settings link 2025-07-15 12:53:01 +08:00
LiuVaayne 72ae105166 [1.5.0-rc] Feat/memory (#7689)
* Merge memory into main

* Improvement/memory UI (#7655)

* feat: add auto-dimension detection to memory settings

- Add automatic embedding dimension detection for memory configuration
- Add toggle switch to enable/disable auto-detection (enabled by default)
- Detect dimensions by making test API call to embedding provider
- Show dimension input field only when auto-detection is disabled
- Add loading state and error handling during dimension detection
- Maintain consistency with knowledge base dimension handling

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

*  feat: implement unified embedding dimensions for memory service

- Add jaison dependency for robust JSON parsing
- Normalize all embeddings to 1536 dimensions for consistency
- Improve embedding dimension logging
- Update memory processor to use jaison for better error handling
- Handle various JSON response formats in fact extraction

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* feat: refactor MemoriesPage layout with new styled components and improved user management features

---------

Co-authored-by: Claude <noreply@anthropic.com>

* Improvement/memory UI (#7656)

Co-authored-by: Claude <noreply@anthropic.com>

*  feat: add memory icon to sidebar for existing users

- Add migration version 118 to enable memory feature visibility
- Adds 'memory' icon to sidebar visible icons if not already present
- Updates store version to trigger migration for existing users

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(memory): include last user message ID in processor config

*  feat(memory): enhance memory settings UI and add new translations

* Enhance memory management UI: Added settings, statistics, search, actions, and user management sections to the memory page. Updated translations for multiple languages to include new UI elements. Refactored component structure for improved layout and readability.

* feat: add i18n

* ui: Enhance memory modals and UI

* refactor(memory): replace direct message calls with window.message for error and success notifications

* fix: eslint error

* feat(memory): enhance memory restoration logic and queries

- Updated MemoryService to restore deleted memories instead of inserting new ones if a memory with the same hash exists.
- Added new SQL queries to check for deleted memories and restore them.
- Improved logging for memory restoration and embedding generation.
- Refactored related API service methods to handle updated memory processing logic.

* refactor: update memory configuration to use ApiClient structure

- Refactored memory-related services and components to utilize the new ApiClient structure for embedding and reranking models.
- Updated constructors and method signatures across multiple files to accept embedApiClient and rerankApiClient parameters.
- Enhanced memory settings UI to reflect changes in memory configuration management.
- Improved type definitions for KnowledgeBaseParams and MemoryConfig to align with the new structure.

* ui: improve user interface for adding new users in memory page

- Enhanced the button for adding new users by incorporating an icon and adjusting padding for better alignment.
- Updated the user selection options to ensure consistent alignment of avatars and user names.
- Refactored layout to improve overall user experience and visual consistency.

* refactor(memory): streamline MemoryProcessor usage in ApiService

- Removed the singleton instance of MemoryProcessor and instantiated it directly within the ApiService methods.
- Updated relevant methods to utilize the new instance for searching and processing memories, improving clarity and encapsulation of memory handling logic.

* chore: move knowledge dir

* fix: correct import paths in KnowledgeService.ts

* fix(Memory): memory deduplicate

* fix(Memory): memory llm provider

* fix: ci error

* fix(Memory): update fact extraction prompt to focus on personal information

* feat: Refactor memory fom sidebar to settings page

- Removed MemoryStick icon from Sidebar component.
- Updated navigation to point to the new memory settings page.
- Introduced MemoriesSettingsModal for managing memory configurations.
- Created MemorySettings component for comprehensive memory management.
- Added user management features including adding, editing, and deleting users.
- Implemented pagination and search functionality for memory items.
- Updated sidebar settings to remove memory icon and ensure proper migration.
- Adjusted Redux store settings to reflect changes in sidebar icons.

* feat: redesign memory settings page with improved UI and layout

* fix i18n

* fix: update citation titles to include memory hash and increment version number

* fix: remove unnecessary prop from KnowledgeCitation component

* feat: enhance fact extraction prompt with clearer guidelines and examples

* 🔧 feat: disable global memory by default and improve UI

- Set globalMemoryEnabled default to false for better user experience
- Remove manual localStorage handling to rely on redux-persist
- Add Beta badge to memory settings section
- Improve layout and styling of memory settings UI components

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Simplify external tool completion handling

* Fix whitespace in migrate config

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: suyao <sy20010504@gmail.com>
Co-authored-by: eeee0717 <chentao020717Work@outlook.com>
Co-authored-by: kangfenmao <kangfenmao@qq.com>
Co-authored-by: 自由的世界人 <3196812536@qq.com>
2025-07-15 10:24:41 +08:00
SuYao 06baaa1522 fix: openai api client (#8154) 2025-07-15 10:10:55 +08:00
SuYao fa17c70d85 chore: update .gitignore to include .claude-code-router directory (#8156) 2025-07-15 09:13:49 +08:00
fullex c606972f0a fix: global shortcut keys (#8084)
* refactor: shortcut keys

* fix:  backward compatibility with old data
2025-07-15 02:23:39 +08:00
luoxu1314 d4dde58e13 fix(OpenAIResponseAPIClient):ensure openai-response providers always use Response API (#8145)
Update OpenAIResponseAPIClient.ts
2025-07-14 23:57:16 +08:00
ous50 | ousfifty | 欧式fifty 71917eb0ec Feat: url context for Gemini models (#7931)
* feat: Add URL Context ability for Gemini Models

* feat: Adding URL Context Button to tool bar and make it visible only when gemini models selected.
It is not working (adding urlContext tools) for now.

* fix: trying to force enable UrlContext function

* fix: enableUrlContext indication reverted

* feat: migration script for refreshing tool order to add URL Context button.

* fix: optimize migrate.ts

* fix: upgrade version

---------

Co-authored-by: suyao <sy20010504@gmail.com>
2025-07-14 23:52:19 +08:00
luoxu1314 1b129636ed chore(OpenAIApiClient): fallback to message when delta.content is empty (#8101)
* chore(OpenAIApiClient): fallback to message when delta.content is empty, fix missing content issue

Signed-off-by: luoxu1314 <xiaoluoxu@163.com>

* Update OpenAIApiClient.ts

* Update OpenAIApiClient.ts

---------

Signed-off-by: luoxu1314 <xiaoluoxu@163.com>
Co-authored-by: one <wangan.cs@gmail.com>
2025-07-14 23:28:27 +08:00
Phantom c2d438fba3 fix(openai): add compatibility mode for handling tool call responses (#7983)
fix(openai): 添加兼容模式处理工具调用响应

在兼容模式下处理工具调用响应时,添加对数组内容的特殊处理逻辑。当isCompatibleMode为true时,将响应内容转换为特定格式的字符串输出,包括对文本、图片和音频等不同类型内容的处理。
2025-07-14 22:44:51 +08:00
LiuVaayne ee4553130b [1.5.0-rc] feat(MCP): Add DXT format support for MCP server installation (#7618)
* feat(MCP): Add DXT format support for MCP server installation

- Add comprehensive DXT package upload and extraction functionality
- Support for DXT manifest validation and MCP server configuration
- Hierarchical UI structure: Quick Add | JSON Import | DXT Import
- Variable substitution for DXT args (${__dirname} replacement)
- Automatic cleanup of DXT server directories on removal
- Enhanced error handling and connectivity checks
- Full internationalization support (EN/CN)
- Uses existing node-stream-zip for efficient extraction
- Proper working directory setup for DXT-based servers

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* 🐛 fix(MCP): Fix DXT server installation and deletion issues

- Replace fs.renameSync with cross-filesystem compatible moveDirectory method to handle temp->mcp directory moves across different mount points
- Add recursive copy fallback when rename fails (ENOENT error fix)
- Sanitize server names with slashes to prevent subdirectory creation during installation
- Improve cleanupDxtServer to handle sanitized names and provide fallback lookup
- Add proper error logging and directory existence warnings

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* feat(MCP): Implement comprehensive DXT MCP configuration support

- Add platform_overrides support to DXT manifest interface
- Implement complete variable substitution system (${__dirname}, ${HOME}, ${DESKTOP}, ${DOCUMENTS}, ${pathSeparator}, ${user_config.KEY})
- Add platform detection utilities (getPlatformIdentifier)
- Create resolved MCP configuration system with applyPlatformOverrides
- Export ResolvedMcpConfig interface and utility functions
- Integrate DXT configuration resolution into MCPService runtime
- Support platform-specific command, args, and environment overrides
- Add comprehensive logging for configuration resolution

Addresses DXT MANIFEST.md mcp_configuration requirements:
- Platform-specific configuration variations
- Cross-platform variable substitution
- Flexible command and environment management

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* feat: add downloads directory variable substitution and simplify platform detection

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-07-14 22:37:56 +08:00
Jason Young bf6ccea1e2 test: add unit tests for getPotentialIndex and input utils (#7947)
* test: add unit tests for getPotentialIndex and input utils

- Add tests for getPotentialIndex function covering streaming text tag detection scenarios
- Add tests for input utils including file drop and keyboard shortcut detection

* test: refactor test structure to comply with TEST_UTILS.md guidelines

- Add file-level describe blocks for both test files
- Fix mock cleanup in input.test.ts:
  - Add vi.clearAllMocks() in beforeEach
  - Replace vi.clearAllMocks() with vi.restoreAllMocks() in afterEach
- Maintain two-layer describe structure as per project standards
2025-07-14 21:52:52 +08:00
karl e0eac6ab7e Fix/7973 (#8059)
* fix: 7973 查看原始数据的按钮没有了

* refactor(MessageTools): replace PreviewBlock with CollapsedContent for improved preview rendering

---------

Co-authored-by: suyao <sy20010504@gmail.com>
2025-07-14 21:50:55 +08:00
Teo 094eb5c17e refactor(ThinkingEffect): Enhance thinking effect (#8147)
refactor(ThinkingEffect): simplify opacity calculation and enhance background styling
2025-07-14 20:47:34 +08:00
SuYao 3d3182095d fix: enhance OpenAIResponseAPIClient for Azure API version (#8108)
* feat: enhance OpenAIResponseAPIClient and update localization for Azure API version

- Added a new method `formatApiHost` in OpenAIResponseAPIClient to ensure correct API host formatting.
- Updated localization files for English, Japanese, Russian, and Chinese to include tips for Azure OpenAI API version usage.
- Modified ProviderSetting component to display the new Azure API version tip in the settings interface.

* chore: clean log
2025-07-14 20:15:14 +08:00
EastlingWoo 706f8e1482 Fix: message tool button cannot click on grid mode (#8123)
* Enable pointer events for grid popover message

* Enable pointer events for grid popover message
2025-07-14 15:15:02 +08:00
fullex ea7e07034a fix: serif font in markdown title styles (#8129)
refactor: update font-family usage in markdown styles
2025-07-14 14:51:01 +08:00
Phantom 1fd92d6a5d feat(constant): add .fxml file extension (#8125)
feat(常量配置): 添加对JavaFX XML文件扩展名.fxml的支持
2025-07-14 14:49:40 +08:00
cnJasonZ 810ebad9ba Fix/ppio links (#8131)
* fix: fix ppio oauth links

* fix: fix ppio links

---------

Co-authored-by: one <wangan.cs@gmail.com>
2025-07-14 13:45:15 +08:00
one c6554c8f80 perf: prevent unnecessary topic rerendering (#8116) 2025-07-14 13:43:01 +08:00
SuYao 19a8d9e9b3 fix(OpenAIApiClient): refine grok4 check for OpenRouter (#8074)
fix(OpenAIApiClient): refine model ID check for reasoning effort
2025-07-14 13:11:02 +08:00
kangfenmao a490287b4a refactor: streamline Vite configuration and enhance CitationsList component
- Updated Vite configuration to remove conditional output settings, ensuring consistent build behavior.
- Refactored CitationsList component to use a Scrollbar for improved UI, encapsulating popover content within a styled container.
2025-07-14 12:57:32 +08:00
Konv Suu 90b0c91b2f fix: set source language when checking history item (#8130)
feat: set source language when checking history item
2025-07-14 12:19:35 +08:00
Phantom 1493132974 fix: cannot paste images when mentioned visual models (#7817)
* refactor(paste): 优化粘贴功能逻辑,移除模型类型依赖

重构粘贴服务处理逻辑,将文件类型支持判断移至组件层
简化handlePaste接口,移除isVisionModel和isGenerateImageModel参数

* fix(MessageEditor): 支持生成图片模型的视觉消息检查

* refactor(Inputbar): 移除调试用的console.log语句

* refactor(PasteService): 移除调试用的console.log语句
2025-07-14 11:41:54 +08:00
kangfenmao 6a4468193b refactor(vite): set legalComments none in prod mode 2025-07-14 10:51:36 +08:00
SuYao 4dd99b5240 feat: enhance Anthropic and OpenAI API clients with incremental output support (#8104)
- Added support for incremental output in AnthropicAPIClient by introducing TEXT_START and THINKING_START chunk types.
- Updated OpenAIAPIClient to conditionally enable incremental output for specific models.
- Modified messageThunk to handle updated smartBlockUpdate calls with an isComplete parameter for better state management.
- Introduced incremental_output parameter in ReasoningEffortOptionalParams type for enhanced configuration options.
2025-07-14 10:30:51 +08:00
Teo 7961ba87ed feat: thinking effect (#8081)
* feat(i18n): add smooth stream output translations for multiple languages

* feat(ThinkingBlock): integrate MarqueeComponent for enhanced message display

* refactor(i18n): remove smooth stream output references from translations and components

* refactor(typingOutput): enhance typing output logic and add debugging information

* refactor(Markdown): consolidate markdown utility imports for cleaner code

* feat(styles): add new styles for dropdown menus, popovers, and modals

* test(ThinkingBlock): enhance tests for streaming status and content collapse behavior

* refactor(typingOutput): remove debugging console log from outputNextChar function

* refactor(MarqueeComponent): comment out blur effect for last marquee item and adjust ThinkingBlock margin

* style(ThinkingBlock): update snapshot to include margin-top for improved layout

* refactor(typingOutput): 修改流式输出逻辑以支持队列长度检查

* refactor(Markdown): simplify useTypingOutput by removing isStreaming parameter

* test(Markdown): comment out re-render tests for content changes

* test(Markdown): remove commented-out re-render tests for content changes

* feat(ThinkingEffect): implement ThinkingEffect component for dynamic message display

- Introduced ThinkingEffect component to enhance the visual representation of thinking states.
- Integrated the new component into ThinkingBlock, replacing MarqueeComponent for improved functionality.
- Added animations and dynamic height adjustments based on message content and expansion state.

* test(ThinkingBlock): update mocks for ThinkingEffect and motion components in tests

* fix: Delete unnecessary comments
2025-07-14 10:08:09 +08:00
one 6952bea6e1 fix: table resizing in mcp tool setting (#8057)
- remove sticky headers
- add missing i18n keys
2025-07-13 23:02:13 +08:00
Caelan 53600175b9 Feature/dmxapi painting add model (#7851)
* 新增图片模型

* 新增图片生成模型

* 新增模型和调整提示语
2025-07-13 21:59:35 +08:00
Konv Suu e5956d4039 feat: improve translate history style (#8060) 2025-07-13 21:11:15 +08:00
one 1f9850c04d chore(gitignore): exclude more AI editor settings (#8102) 2025-07-13 21:07:45 +08:00
one df43cb7a90 fix(Knowledge): pass searchResultCount to embed-js (#8118) 2025-07-13 20:22:02 +08:00
kangfenmao bea664af0f refactor: simplify HtmlArtifactsPopup component and improve preview functionality
- Removed unnecessary extracted components and integrated their logic directly into HtmlArtifactsPopup.
- Enhanced preview functionality with a debounced update mechanism for HTML content.
- Updated styling for better layout and responsiveness, including fullscreen handling.
- Adjusted view mode management for clearer code structure and improved user experience.
2025-07-13 10:18:39 +08:00
kangfenmao b265c640ca refactor: improve environment variable handling in electron.vite.config.ts
- Introduced `isDev` and `isProd` constants for clearer environment checks.
- Simplified sourcemap and noDiscovery settings based on environment.
- Enhanced esbuild configuration for production to drop console and debugger statements.
2025-07-13 10:18:39 +08:00
beyondkmp a3d6f32202 fix: replace Select component with custom Selector in LocalBackupSetting (#8055)
* fix: replace Select component with custom Selector in LocalBackupSettings

- Updated LocalBackupSettings to use a custom Selector component for better styling and functionality.
- Enhanced the options for auto-sync interval and max backups with improved structure and internationalization support.
- Added success notification handling in restoreFromLocalBackup function in BackupService for better user feedback.

* refactor: streamline backup service and settings management

- Removed local backup auto-sync functionality and integrated its logic into the general auto-sync mechanism.
- Updated backup service methods to handle specific backup types (webdav, s3, local) more efficiently.
- Renamed backup functions for consistency and clarity.
- Enhanced local backup management in settings to utilize the new auto-sync structure.
- Improved error handling and logging for backup operations.

* refactor: replace Select component with custom Selector in S3Settings

- Updated S3Settings to utilize a custom Selector component for improved styling and functionality.
- Enhanced the options for auto-sync interval and max backups with better structure and internationalization support.
- Removed deprecated Select component to streamline the settings interface.
2025-07-13 07:09:23 +08:00
fullex 16e65d39be fix: [Linux] support Linux Wayland global shortcuts (#8080)
feat: support Linux Wayland global shortcuts
2025-07-13 00:30:01 +08:00
luoxu1314 186bdb486f fix: 修复从未打开过的话题导出markdown为空的问题 (#8103)
* feat: 优化导出功能使用 TopicManager 确保消息正确加载

- 移除对 db 的直接依赖,改用 TopicManager.getTopicMessages
- 修复从未打开过的话题导出为空的问题

Signed-off-by: luoxu1314 <xiaoluoxu@163.com>

* Update export.test.ts

修复相关测试mock使用TopicManager而非db.topics.get,确保测试与新的消息加载方式兼容

* Update export.ts

* Update export.test.ts

完善测试

* style(test): 移除多余空行

---------

Signed-off-by: luoxu1314 <xiaoluoxu@163.com>
Co-authored-by: GeorgeDong32 <georgedong32@qq.com>
2025-07-12 22:45:01 +08:00
fullex ea40cc7692 fix(install): update return codes for bun and uv installation scripts (#8039) 2025-07-12 22:22:07 +08:00
LiuVaayne 16ca373c55 feat: add MCP server version display with badges (#8097) 2025-07-12 15:35:59 +08:00
cnJasonZ 38cf3869bc fix: ppio oauth links (#8073) 2025-07-11 18:00:35 +08:00
kangfenmao 60e3431b36 chore(version): 1.4.11 2025-07-11 12:18:38 +08:00
kangfenmao 84a6c2da59 feat: add HTML code detection utility and integrate into CodeBlockView
- Introduced `isHtmlCode` function to identify HTML content based on DOCTYPE and tag presence.
- Updated `CodeBlockView` to utilize `isHtmlCode` for conditional rendering of HTML artifacts.
- Added comprehensive tests for `isHtmlCode` to ensure accurate detection of HTML structures.
2025-07-11 12:16:44 +08:00
kangfenmao 5b9ff3053b refactor: update styles and layout in markdown and message components
- Removed unnecessary letter and word spacing in markdown styles.
- Adjusted padding in Inputbar for improved layout.
- Modified margin properties in CitationsList and Message components for consistency.
- Enhanced MessageHeader logic to conditionally hide based on message type.
- Updated icon sizes in MessageMenubar for better alignment.
- Added margin adjustments in ThinkingBlock for improved spacing.
2025-07-11 11:33:20 +08:00
SuYao 8340922263 fix: smartblock update not persist to db (#8046)
* chore(version): 1.4.10

* feat: enhance ThinkingTagExtractionMiddleware and update smartBlockUpdate function

- Added support for THINKING_START and TEXT_START chunk types in ThinkingTagExtractionMiddleware.
- Updated smartBlockUpdate function to include an isComplete parameter for better block state management.
- Ensured proper handling of block updates based on completion status across various message types.

* fix: refine block update logic in messageThunk

- Adjusted conditions for canceling throttled block updates based on block type changes and completion status.
- Improved handling of block updates to ensure accurate state management during message processing.

* chore: add comment

* fix: update message block status handling

- Changed the status of image blocks from STREAMING to PENDING to better reflect the processing state.
- Refined logic in OpenAIResponseAPIClient to ensure user messages are correctly handled based on assistant message content.
- Improved rendering conditions in ImageBlock component for better user experience during image loading.

---------

Co-authored-by: kangfenmao <kangfenmao@qq.com>
2025-07-11 11:33:05 +08:00
one a93cab6b43 fix(CodePreview): revert to absolute positioning (#7980)
* fix(CodePreview): revert to absolute positioning

* fix: add min width to codeblockview
2025-07-11 11:06:21 +08:00
one 9a81c400ab fix: sticky code toolbar (#8012)
fix: sticky code toolbar for single-model message
2025-07-11 11:05:08 +08:00
西街工坊 808a22d5c6 fix(Doc2xPreprocessProvider): replace filePath split with path.parse… (#8042) 2025-07-11 11:03:05 +08:00
kangfenmao 10e512f32e chore(version): 1.4.10 2025-07-10 23:31:03 +08:00
one 4d75515bd6 refactor: raise the max count of document chunks from 30 to 50 (#7863)
* refactor: raise the max count of document chunks from 30 to 70

- Raise the max count of document chunks count
- Update i18n for websearch rag for consistency

* refactor: lower the count to 50
2025-07-10 22:52:18 +08:00
kangfenmao 3d6c84de6d refactor: improve styling and layout in MessageTools and Prompt components
- Adjusted spacing and border styles in MessageTools for better alignment.
- Updated margin and border properties in Prompt for consistent UI.
- Enhanced background color handling in ToolContentWrapper based on status.
2025-07-10 22:36:48 +08:00
SuYao 3dd393b840 fix: azure-openai (#7978) 2025-07-10 22:17:20 +08:00
LiuVaayne 8f86c53941 feat: implement MCP tool auto-approve functionality (#8007)
*  feat: implement MCP tool auto-approve functionality

- Add auto-approve toggle for MCP tools in settings
- Add improved UI for tool approval with Run/Cancel/Auto-approve buttons
- Add internationalization support for tool approval interface
- Update tool confirmation logic to support auto-approved tools
- Enhance tool status indicators and button styling
- Add disabledAutoApproveTools configuration for MCP servers

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* refactor: use table for mcp tools setting

* refactor: improve styles, add missing i18n

* refactor: extract renderStatusIndicator, reuse colors

* refactor: simplify the table

* feat: auto approve same tool in a turn

* feat(i18n): add confirmation tooltip for auto-approve tool in multiple languages

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: one <wangan.cs@gmail.com>
Co-authored-by: suyao <sy20010504@gmail.com>
Co-authored-by: Teo <cheesen.xu@gmail.com>
2025-07-10 22:14:52 +08:00
Phantom a7b78c547a fix(encoding): encoding detection and decoding logic (#8024) 2025-07-10 22:13:40 +08:00
Chen Tao bcc1046cdf feat: add upload file (#8035) 2025-07-10 22:06:09 +08:00
Phantom c05c06b7a1 fix: VoyageEmbeddings (#8034)
* fix(embeddings): 修复VoyageAI嵌入格式和模型验证错误

修复OpenAIBaseClient中VoyageAI提供商的embedding格式设置问题
完善VoyageEmbeddings模型验证的错误提示信息

* refactor(embeddings): 移除VoyageEmbeddings的模型维度限制检查

简化VoyageEmbeddings的创建逻辑,不再对支持的模型维度进行校验

* fix(embeddings): 修复VoyageEmbeddings模型维度设置问题

修复VoyageEmbeddings中未正确校验模型是否支持设置outputDimension的问题
当provider为voyageai且模型不支持设置dimensions时,自动忽略传入的dimensions参数

* refactor(embeddings): 集中管理支持设置维度的模型列表

将各嵌入模型支持设置维度的模型列表集中到utils模块
不再让VoyageEmbeddings中getDimensions抛出错误,而是自动修复
2025-07-10 21:53:37 +08:00
Phantom 446ebae175 feat(ui): better infinite context (#8021)
* feat(上下文): 添加最大上下文数量限制及显示组件

- 在常量配置中添加 MAX_CONTEXT_COUNT
- 创建 MaxContextCount 组件用于显示无限上下文标识
- 在相关组件中替换硬编码的上下文最大值
- 优化 TokenCount 组件的上下文计数显示样式

* refactor(常量): 添加UNLIMITED_CONTEXT_COUNT常量并替换硬编码值

使用UNLIMITED_CONTEXT_COUNT常量替代多处硬编码的100000值,提高代码可维护性

* refactor(Inputbar): 使用 SlashSeparatorSpan 组件替换内联样式

将 TokenCount.tsx 中的斜杠分隔符内联样式替换为 SlashSeparatorSpan 组件,提高代码可维护性

* fix: 为 InfinityIcon 添加 aria-label 并统一样式
2025-07-10 21:51:31 +08:00
one ba742b7b1f feat: save to knowledge (#7528)
* feat: save to knowledge

* refactor: simplify checkbox

* feat(i18n): add 'Save to Local File' translation key for multiple languages

---------

Co-authored-by: suyao <sy20010504@gmail.com>
2025-07-10 21:34:01 +08:00
fullex 7c6db809bb fix(SelectionAssistant): [macOS] show actionWindow on fullscreen app (#8004)
* feat(SelectionService): enhance action window handling for macOS fullscreen mode

- Updated processAction and showActionWindow methods to support fullscreen mode on macOS.
- Added isFullScreen parameter to manage action window visibility and positioning.
- Improved action window positioning logic to ensure it remains within screen boundaries.
- Adjusted IPC channel to pass fullscreen state from the renderer to the service.
- Updated SelectionToolbar to track fullscreen state and pass it to the action processing function.

* chore(deps): update selection-hook to version 1.0.6 in package.json and yarn.lock

* fix(SelectionService): improve macOS fullscreen handling and action window focus

- Added app import to manage dock visibility on macOS.
- Enhanced fullscreen handling logic to ensure the dock icon is restored correctly.
- Updated action window focus behavior to prevent unintended hiding when blurred.
- Refactored SelectionActionApp to streamline auto pinning logic and remove redundant useEffect.
- Cleaned up SelectionToolbar by removing unnecessary window size updates when demo is false.

* refactor(SelectionService): remove commented-out code for clarity

* refactor(SelectionService): streamline macOS handling and improve code clarity
2025-07-10 20:41:01 +08:00
Konv Suu 855499681f feat: add confirm for unsaved content in creating agent (#7965) 2025-07-10 19:37:18 +08:00
one 92be3c0f56 chore: update vscode settings (#7974)
* chore: update vscode settings

* refactor: add editorconfig to extensions
2025-07-10 19:34:57 +08:00
one 2a72f391b7 feat: codeblock dot language (#6783)
* feat(CodeBlock): support dot language in code block

- render DOT using @viz-js/viz
- highlight DOT using @viz-js/lang-dot (CodeEditor only)
- extract a special view map, update file structure
- extract and reuse the PreviewError component across special views
- update dependencies, fix peer dependencies

* chore: prepare for merge
2025-07-10 19:32:51 +08:00
SuYao db642f0837 feat(models): support Grok4 (#8032)
refactor(models): rename and enhance reasoning model functions for clarity and functionality
2025-07-10 19:27:53 +08:00
kangfenmao fca93b6c51 style: update various component styles for improved layout and readability
- Adjusted color for list items in color.scss for better contrast.
- Modified line-height and margins in markdown.scss for improved text readability.
- Changed height property in FloatingSidebar.tsx for consistent layout.
- Increased padding in AgentsPage.tsx for better spacing.
- Updated padding and border-radius in Inputbar.tsx for enhanced aesthetics.
- Reduced margin in MessageHeader.tsx for tighter layout.
- Refactored GroupTitle styles in AssistantsTab.tsx for better alignment and spacing.
2025-07-10 18:59:00 +08:00
one 7e672d86e7 refactor: do not jump on enabling content search (#7922)
* fix: content search count on enable

* refactor(ContentSearch): do not jump on enabling content search

* refactor: simplify result count
2025-07-10 17:29:43 +08:00
SuYao e9112cad0f fix(McpToolChunkMiddleware): add logging for tool calls and enhance l… (#8028)
fix(McpToolChunkMiddleware): add logging for tool calls and enhance lookup logic
2025-07-10 17:26:57 +08:00
one ffbd6445df refactor(Inputbar): make button tooltips disappear faster (#8011) 2025-07-10 17:26:38 +08:00
Alaina Hardie dff44f2721 Fix: Require typechecking for Mac and Linux target builds (#7219)
fix: Mac builds do not auto-run typecheck, but Windows builds do. This requires an extra manual step when building for Mac.

Update build scripts in package.json to use `npm run build` directly for Mac and Linux targets..
2025-07-10 17:01:31 +08:00
SuYao 3afa81eb5d fix(Anthropic): content truncation (#7942)
* fix(Anthropic): content truncation

* feat: add start event and fix content truncation

* fix (gemini): some event

* revert: index.tsx

* revert(messageThunk): error block

* fix: ci

* chore: unuse log
2025-07-10 16:58:35 +08:00
SuYao 3350c3e2e5 fix(GeminiAPIClient, mcp-tools): enhance tool call handling and lookup logic (#8009)
* fix(GeminiAPIClient, mcp-tools): enhance tool call handling and lookup logic

* fix: unuse log
2025-07-10 15:16:23 +08:00
SuYao f85f46c248 fix(middleware): ollama qwen think (#8026)
refactor(AiProvider): comment out unnecessary middleware removal for performance optimization

- Commented out the removal of ThinkingTagExtractionMiddlewareName to prevent potential performance degradation while maintaining existing functionality.
- Retained the removal of ThinkChunkMiddlewareName as part of the existing logic for non-reasoning scenarios.
2025-07-10 15:15:38 +08:00
SuYao 05f3b88f30 fix(Inputbar): update resizeTextArea call to improve functionality (#8010) 2025-07-10 15:15:13 +08:00
自由的世界人 f8c6b5c05f Fix translation key for unlimited backups label (#7987)
Updated the translation key for the 'unlimited' backups option in WebDavSettings to use the correct namespace.
2025-07-10 15:09:59 +08:00
Jason Young 97dbfe492e test: enhance download and fetch utility test coverage with bug fix (#7891)
* test: enhance download and fetch utility test coverage

- Add MIME type handling tests for data URLs in download.test.ts
- Add timestamp generation tests for blob and network downloads
- Add Content-Type header handling test for extensionless files
- Add format parameter tests (markdown/html/text) for fetchWebContent
- Add timeout signal handling tests for fetch operations
- Add combined signal (user + timeout) test for AbortSignal.any

These tests improve coverage of edge cases and ensure critical functionality
is properly tested.

* fix: add missing error handling for fetch in download utility

- Add .catch() handler for network request failures in download()
- Use window.message.error() for user-friendly error notifications
- Update tests to verify error handling behavior
- Ensure proper error messages are shown to users

This fixes a missing error handler that was discovered during test development.

* refactor: improve test structure and add i18n support for download utility

- Unified test structure with two-layer describe blocks (filename -> function name)
- Added afterEach with restoreAllMocks for consistent mock cleanup
- Removed individual mockRestore calls in favor of centralized cleanup
- Added i18n support to download.ts for error messages
- Updated error handling logic to avoid duplicate messages
- Updated test expectations to match new i18n error messages

* test: fix react-i18next mock for Markdown test

Add missing initReactI18next to mock to resolve test failures caused by i18n initialization when download utility imports i18n module.
2025-07-10 14:35:40 +08:00
kangfenmao 186f0ed06f feat(MCPSettings): enhance MCP server management and localization
- Added BuiltinMCPServersSection and McpResourcesSection components to display available MCP servers and resources.
- Updated navigation logic to redirect users to the MCP settings upon adding a server.
- Enhanced localization by adding new keys for built-in servers in multiple languages.
- Improved the SettingsPage layout by reordering menu items for better accessibility.
2025-07-10 12:39:01 +08:00
kangfenmao daf134f331 refactor(HtmlArtifacts): enhance HTML validation and rendering logic
- Added checks for complete HTML documents based on presence of critical tags.
- Updated unmatched tag detection to include a comprehensive list of HTML5 void elements.
- Improved HTML content rendering with a fixed interval update mechanism.
- Adjusted modal header styles for better layout consistency.
- Enabled editing capabilities in the CodeEditor component for HTML content.
2025-07-10 12:28:25 +08:00
kangfenmao 3f7f78da15 fix(release.yml): add missing environment variables for build jobs 2025-07-10 10:49:10 +08:00
kangfenmao 1d289621fc style(markdown): enhance typography and spacing for improved readability
- Increased line height and adjusted margins for headers and paragraphs to enhance text clarity.
- Added letter and word spacing for better text presentation.
- Updated blockquote and table styles for a more visually appealing layout.
- Improved hover effect for table rows to enhance user interaction.
2025-07-10 10:49:10 +08:00
kangfenmao d7002cda11 refactor: quick panel remove multi-select mode 2025-07-10 02:45:41 +08:00
kangfenmao 559fcecf77 refactor(CodeBlockView): replace HtmlArtifacts component with HtmlArtifactsCard
- Removed the obsolete HtmlArtifacts component and its associated logic.
- Introduced the new HtmlArtifactsCard component to enhance the rendering of HTML artifacts.
- Updated the CodeBlockView to utilize HtmlArtifactsCard, improving maintainability and user experience.
- Added a new HtmlArtifactsPopup component for better HTML content preview and editing capabilities.
- Enhanced localization by adding translation keys for HTML artifacts in multiple languages.
2025-07-10 02:45:32 +08:00
kangfenmao 1d854c232e refactor(Messages): update message styling and structure for improved clarity
- Simplified the message header and footer components by removing unnecessary props and logic.
- Adjusted the message container styles for better alignment and spacing.
- Enhanced the message tokens display logic and corrected the component name for consistency.
- Removed unused translation keys related to token usage from multiple language files to streamline localization.
2025-07-10 02:45:32 +08:00
kangfenmao 8c6684cbdf refactor(WebSearchButton): simplify web search button logic and improve tooltip behavior
- Removed unused imports and streamlined the logic for enabling web search.
- Updated the tooltip title to reflect the current state of web search functionality.
- Enhanced the handling of quick panel opening based on the assistant's web search settings.
2025-07-10 02:45:32 +08:00
kangfenmao c7ab71f01f refactor(OpenAISettingsGroup): simplify component structure and remove styled components
- Removed unused imports and the StyledSelect component, replacing it with a standard Selector for improved clarity.
- Streamlined the layout by eliminating unnecessary styles, enhancing maintainability and readability of the code.
2025-07-10 00:42:36 +08:00
SuYao 9b57351d1e fix(McpToolChunkMiddleware): enhance tool call confirmation logic (#8005)
* fix(McpToolChunkMiddleware): enhance tool call confirmation logic

- Added additional condition to confirm tool calls by checking the toolCallId in the confirmed object.
- Included a console log for confirmed tool calls to aid in debugging and tracking tool call execution.

* chore: unuse log
2025-07-09 23:39:58 +08:00
kangfenmao f9e88fb6ee refactor(Navbar): remove MinAppsPopover component
- Deleted the MinAppsPopover component to streamline the Navbar.
- Updated Navbar to remove references to MinAppsPopover, enhancing code maintainability.
2025-07-09 19:32:36 +08:00
kangfenmao 074ba0ae05 feat(i18n): add "Open Logs" button translations for multiple languages
- Introduced new translation keys for the "Open Logs" button in various languages (en-us, ja-jp, ru-ru, zh-cn, zh-tw, el-gr, es-es, fr-fr, pt-pt).
- Updated the DataSettings component to include a button for opening application logs, enhancing user accessibility to log files.
2025-07-09 19:22:57 +08:00
kangfenmao 4a8a5e8428 feat(i18n): enhance localization for GitHub Copilot settings
- Added new translation keys for error messages and steps in the GitHub Copilot authentication process across multiple languages (en-us, ja-jp, ru-ru, zh-cn, zh-tw).
- Updated the GitHubCopilotSettings component to reflect the new steps for user guidance during the authentication process.
- Improved user experience by providing detailed descriptions and success/error messages related to the authorization flow.
2025-07-09 19:07:32 +08:00
kangfenmao f7fa665f3a feat(CustomHeaderPopup): add custom headers management for providers
- Introduced a new CustomHeaderPopup component for managing extra headers for providers.
- Integrated the popup into the ProviderSetting component, allowing users to edit headers via a modal.
- Refactored ApiKeyListPopup to use a styled container for improved layout.
2025-07-09 18:15:42 +08:00
beyondkmp e273ddcfb0 fix(LocalBackupSettings): update input and select styles for better responsiveness (#7977)
refactor(LocalBackupSettings): update input and select styles for better responsiveness

- Adjusted the input field to have a flexible width between 200 and 400 pixels.
- Modified select components to use a minimum width of 120 pixels for improved layout consistency.
- Enhanced onChange handlers for select components to ensure proper value handling.
2025-07-09 18:05:32 +08:00
kangfenmao 41d3a1fd55 refactor(SettingsPage): reorder menu items for improved organization 2025-07-09 17:50:57 +08:00
kangfenmao 7237ba34db docs: short i18n keys 2025-07-09 17:26:42 +08:00
Phantom fbf89b3f0a fix(translate): prevent translation from being triggered unexpectedly during IME composition (#7968)
fix(translate): 修复在输入法组合文字时意外触发翻译的问题
2025-07-09 13:46:39 +08:00
kangfenmao 8f38422e7f chore(version): 1.4.9 2025-07-09 10:17:40 +08:00
自由的世界人 79a64f0118 fix: Improve model filtering and group handling (#7950)
Expanded the text-to-image model regex to include more identifiers. Removed the getModelGroup function and now use the model's group property directly. Updated model selection in ModelSettings and TranslatePage to also filter out rerank and text-to-image models, ensuring only appropriate models are shown in dropdowns.
2025-07-09 00:15:47 +08:00
suyao 0d60b34c17 refactor(messageThunk): clean up imports and remove unused code
- Removed duplicate Logger import and unused DeepResearchMessageBlock type.
- Organized imports for better readability and maintainability.
2025-06-30 09:51:52 +08:00
suyao 60a89998fe feat(DeepResearch): implement deep research functionality and UI components
- Added DeepResearchCard component for user interaction during deep research tasks.
- Introduced DeepResearchBlock to handle deep research message blocks.
- Enhanced API client and service to support deep research model checks and processing.
- Updated models and prompts for deep research clarification and instruction generation.
- Integrated deep research confirmation mechanism for user input handling.
- Added necessary translations for deep research UI elements in multiple languages.
2025-06-30 09:44:39 +08:00
279 changed files with 27578 additions and 11723 deletions
+6 -3
View File
@@ -77,9 +77,10 @@ jobs:
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RENDERER_VITE_AIHUBMIX_SECRET: ${{ vars.RENDERER_VITE_AIHUBMIX_SECRET }}
NODE_OPTIONS: --max-old-space-size=8192
MAIN_VITE_MINERU_API_KEY: ${{ vars.MAIN_VITE_MINERU_API_KEY }}
RENDERER_VITE_AIHUBMIX_SECRET: ${{ vars.RENDERER_VITE_AIHUBMIX_SECRET }}
RENDERER_VITE_PPIO_APP_SECRET: ${{ vars.RENDERER_VITE_PPIO_APP_SECRET }}
- name: Build Mac
if: matrix.os == 'macos-latest'
@@ -93,10 +94,11 @@ jobs:
APPLE_ID: ${{ vars.APPLE_ID }}
APPLE_APP_SPECIFIC_PASSWORD: ${{ vars.APPLE_APP_SPECIFIC_PASSWORD }}
APPLE_TEAM_ID: ${{ vars.APPLE_TEAM_ID }}
RENDERER_VITE_AIHUBMIX_SECRET: ${{ vars.RENDERER_VITE_AIHUBMIX_SECRET }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NODE_OPTIONS: --max-old-space-size=8192
MAIN_VITE_MINERU_API_KEY: ${{ vars.MAIN_VITE_MINERU_API_KEY }}
RENDERER_VITE_AIHUBMIX_SECRET: ${{ vars.RENDERER_VITE_AIHUBMIX_SECRET }}
RENDERER_VITE_PPIO_APP_SECRET: ${{ vars.RENDERER_VITE_PPIO_APP_SECRET }}
- name: Build Windows
if: matrix.os == 'windows-latest'
@@ -105,9 +107,10 @@ jobs:
yarn build:win
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RENDERER_VITE_AIHUBMIX_SECRET: ${{ vars.RENDERER_VITE_AIHUBMIX_SECRET }}
NODE_OPTIONS: --max-old-space-size=8192
MAIN_VITE_MINERU_API_KEY: ${{ vars.MAIN_VITE_MINERU_API_KEY }}
RENDERER_VITE_AIHUBMIX_SECRET: ${{ vars.RENDERER_VITE_AIHUBMIX_SECRET }}
RENDERER_VITE_PPIO_APP_SECRET: ${{ vars.RENDERER_VITE_PPIO_APP_SECRET }}
- name: Release
uses: ncipollo/release-action@v1
+4
View File
@@ -46,6 +46,10 @@ local
.aider*
.cursorrules
.cursor/*
.claude/*
.gemini/*
.trae/*
.claude-code-router/*
# vitest
coverage
+1 -1
View File
@@ -1,3 +1,3 @@
{
"recommendations": ["dbaeumer.vscode-eslint"]
"recommendations": ["dbaeumer.vscode-eslint", "esbenp.prettier-vscode", "editorconfig.editorconfig"]
}
+2 -2
View File
@@ -10,7 +10,7 @@
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron-vite.cmd"
},
"runtimeArgs": ["--sourcemap"],
"runtimeArgs": ["--inspect", "--sourcemap"],
"env": {
"REMOTE_DEBUGGING_PORT": "9222"
}
@@ -21,7 +21,7 @@
"request": "attach",
"type": "chrome",
"webRoot": "${workspaceFolder}/src/renderer",
"timeout": 60000,
"timeout": 3000000,
"presentation": {
"hidden": true
}
+1
View File
@@ -4,6 +4,7 @@
"source.fixAll.eslint": "explicit",
"source.organizeImports": "never"
},
"files.eol": "\n",
"search.exclude": {
"**/dist/**": true,
".yarn/releases/**": true
+64
View File
@@ -0,0 +1,64 @@
# Security Policy
## 📢 Reporting a Vulnerability
At Cherry Studio, we take security seriously and appreciate your efforts to responsibly disclose vulnerabilities. If you discover a security issue, please report it as soon as possible.
**Please do not create public issues for security-related reports.**
- To report a security issue, please use the GitHub Security Advisories tab to "[Open a draft security advisory](https://github.com/CherryHQ/cherry-studio/security/advisories/new)".
- Include a detailed description of the issue, steps to reproduce, potential impact, and any possible mitigations.
- If applicable, please also attach proof-of-concept code or screenshots.
We will acknowledge your report within **72 hours** and provide a status update as we investigate.
---
## 🔒 Supported Versions
We aim to support the latest released version and one previous minor release.
| Version | Supported |
|-----------------|--------------------|
| Latest (`main`) | ✅ Supported |
| Previous minor | ✅ Supported |
| Older versions | ❌ Not supported |
If you are using an unsupported version, we strongly recommend updating to the latest release to receive security fixes.
---
## 💡 Security Measures
Cherry Studio integrates several security best practices, including:
- Strict dependency updates and regular vulnerability scanning.
- TypeScript strict mode and linting to reduce potential injection or runtime issues.
- Enforced code formatting and pre-commit hooks.
- Internal security reviews before releases.
- Dedicated MCP (Model Context Protocol) safeguards for model interactions and data privacy.
---
## 🛡️ Disclosure Policy
- We follow a **coordinated disclosure** approach.
- We will not publicly disclose vulnerabilities until a fix has been developed and released.
- Credit will be given to researchers who responsibly disclose vulnerabilities, if requested.
---
## 🤝 Acknowledgements
We greatly appreciate contributions from the security community and strive to recognize all researchers who help keep Cherry Studio safe.
---
## 🌟 Questions?
For any security-related questions not involving vulnerabilities, please reach out to:
**security@cherry-ai.com**
---
Thank you for helping keep Cherry Studio and its users secure!
+222
View File
@@ -0,0 +1,222 @@
# Cherry Studio 记忆功能指南
## 功能介绍
Cherry Studio 的记忆功能是一个强大的工具,能够帮助 AI 助手记住对话中的重要信息、用户偏好和上下文。通过记忆功能,您的 AI 助手可以:
- 📝 **记住重要信息**:自动从对话中提取并存储关键事实和信息
- 🧠 **个性化响应**:基于存储的记忆提供更加个性化和相关的回答
- 🔍 **智能检索**:在需要时自动搜索相关记忆,增强对话的连贯性
- 👥 **多用户支持**:为不同用户维护独立的记忆上下文
记忆功能特别适用于需要长期保持上下文的场景,例如个人助手、客户服务、教育辅导等。
## 如何启用记忆功能
### 1. 全局配置(首次设置)
在使用记忆功能之前,您需要先进行全局配置:
1. 点击侧边栏的 **记忆** 图标(记忆棒图标)进入记忆管理页面
2. 点击右上角的 **更多** 按钮(三个点),选择 **设置**
3. 在设置弹窗中配置以下必要项:
- **LLM 模型**:选择用于处理记忆的语言模型(推荐使用 GPT-4 或 Claude 等高级模型)
- **嵌入模型**:选择用于生成向量嵌入的模型(如 text-embedding-3-small
- **嵌入维度**:输入嵌入模型的维度(通常为 1536)
4. 点击 **确定** 保存配置
> ⚠️ **注意**:嵌入模型和维度一旦设置后无法更改,请谨慎选择。
### 2. 为助手启用记忆
完成全局配置后,您可以为特定助手启用记忆功能:
1. 进入 **助手** 页面
2. 选择要启用记忆的助手,点击 **编辑**
3. 在助手设置中找到 **记忆** 部分
4. 打开记忆功能开关
5. 保存助手设置
启用后,该助手将在对话过程中自动提取和使用记忆。
## 使用方法
### 查看记忆
1. 点击侧边栏的 **记忆** 图标进入记忆管理页面
2. 您可以看到所有存储的记忆卡片,包括:
- 记忆内容
- 创建时间
- 所属用户
### 添加记忆
手动添加记忆有两种方式:
**方式一:在记忆管理页面添加**
1. 点击右上角的 **添加记忆** 按钮
2. 在弹窗中输入记忆内容
3. 点击 **添加** 保存
**方式二:在对话中自动提取**
- 当助手启用记忆功能后,系统会自动从对话中提取重要信息并存储为记忆
### 编辑记忆
1. 在记忆卡片上点击 **更多** 按钮(三个点)
2. 选择 **编辑**
3. 修改记忆内容
4. 点击 **保存**
### 删除记忆
1. 在记忆卡片上点击 **更多** 按钮
2. 选择 **删除**
3. 确认删除操作
## 记忆搜索
记忆管理页面提供了强大的搜索功能:
1. 在页面顶部的搜索框中输入关键词
2. 系统会实时过滤显示匹配的记忆
3. 搜索支持模糊匹配,可以搜索记忆内容的任何部分
## 用户管理
记忆功能支持多用户,您可以为不同的用户维护独立的记忆库:
### 切换用户
1. 在记忆管理页面,点击右上角的用户选择器
2. 选择要切换到的用户
3. 页面会自动加载该用户的记忆
### 添加新用户
1. 点击用户选择器
2. 选择 **添加新用户**
3. 输入用户 ID(支持字母、数字、下划线和连字符)
4. 点击 **添加**
### 删除用户
1. 切换到要删除的用户
2. 点击右上角的 **更多** 按钮
3. 选择 **删除用户**
4. 确认删除(注意:这将删除该用户的所有记忆)
> 💡 **提示**:默认用户(default-user)无法删除。
## 设置说明
### LLM 模型
- 用于处理记忆提取和更新的语言模型
- 建议选择能力较强的模型以获得更好的记忆提取效果
- 可随时更改
### 嵌入模型
- 用于将文本转换为向量,支持语义搜索
- 一旦设置后无法更改(为了保证现有记忆的兼容性)
- 推荐使用 OpenAI 的 text-embedding 系列模型
### 嵌入维度
- 嵌入向量的维度,需要与选择的嵌入模型匹配
- 常见维度:
- text-embedding-3-small: 1536
- text-embedding-3-large: 3072
- text-embedding-ada-002: 1536
### 自定义提示词(可选)
- **事实提取提示词**:自定义如何从对话中提取信息
- **记忆更新提示词**:自定义如何更新现有记忆
## 最佳实践
### 1. 合理组织记忆
- 保持记忆简洁明了,每条记忆专注于一个具体信息
- 使用清晰的语言描述事实,避免模糊表达
- 定期审查和清理过时或不准确的记忆
### 2. 多用户场景
- 为不同的使用场景创建独立用户(如工作、个人、学习等)
- 使用有意义的用户 ID,便于识别和管理
- 定期备份重要用户的记忆数据
### 3. 模型选择建议
- **LLM 模型**GPT-4、Claude 3 等高级模型能更准确地提取和理解信息
- **嵌入模型**:选择与您的主要使用语言匹配的模型
### 4. 性能优化
- 避免存储过多冗余记忆,这可能影响搜索性能
- 定期整理和合并相似的记忆
- 对于大量记忆的场景,考虑按主题或时间进行分类管理
## 常见问题
### Q: 为什么我无法启用记忆功能?
A: 请确保您已经完成全局配置,包括选择 LLM 模型和嵌入模型。
### Q: 记忆会自动同步到所有助手吗?
A: 不会。每个助手的记忆功能需要单独启用,且记忆是按用户隔离的。
### Q: 如何导出我的记忆数据?
A: 目前系统暂不支持直接导出功能,但所有记忆都存储在本地数据库中。
### Q: 删除的记忆可以恢复吗?
A: 删除操作是永久的,无法恢复。建议在删除前仔细确认。
### Q: 记忆功能会影响对话速度吗?
A: 记忆功能在后台异步处理,不会明显影响对话响应速度。但过多的记忆可能会略微增加搜索时间。
### Q: 如何清空所有记忆?
A: 您可以删除当前用户并重新创建,或者手动删除所有记忆条目。
## 注意事项
### 隐私保护
- 所有记忆数据都存储在您的本地设备上,不会上传到云端
- 请勿在记忆中存储敏感信息(如密码、私钥等)
- 定期审查记忆内容,确保没有意外存储的隐私信息
### 数据安全
- 记忆数据存储在本地数据库中
- 建议定期备份重要数据
- 更换设备时请注意迁移记忆数据
### 使用限制
- 单条记忆的长度建议不超过 500 字
- 每个用户的记忆数量建议控制在 1000 条以内
- 过多的记忆可能影响系统性能
## 技术细节
记忆功能使用了先进的 RAG(检索增强生成)技术:
1. **信息提取**:使用 LLM 从对话中智能提取关键信息
2. **向量化存储**:通过嵌入模型将文本转换为向量,支持语义搜索
3. **智能检索**:在对话时自动搜索相关记忆,提供给 AI 作为上下文
4. **持续学习**:随着对话进行,不断更新和完善记忆库
---
💡 **提示**:记忆功能是 Cherry Studio 的高级特性,合理使用可以大大提升 AI 助手的智能程度和用户体验。如有更多问题,欢迎查阅文档或联系支持团队。
+5 -6
View File
@@ -117,9 +117,8 @@ afterSign: scripts/notarize.js
artifactBuildCompleted: scripts/artifact-build-completed.js
releaseInfo:
releaseNotes: |
划词助手:支持 macOS 系统
文档处理:增加 MinerU、Doc2xMistral 等服务商支持
知识库:新的知识库界面,增加扫描版 PDF 支持
OCRmacOS 增加系统 OCR 支持
服务商:支持一键添加服务商,新增 PH8 大模型开放平台, 支持 PPIO OAuth 登录
修复:Linux下数据目录移动问题
新增全局记忆功能
MCP 支持 DXT 格式导入
全局快捷键支持 Linux 系统
模型思考过程增加动画效果
错误修复和性能优化
+12 -16
View File
@@ -8,6 +8,9 @@ const visualizerPlugin = (type: 'renderer' | 'main') => {
return process.env[`VISUALIZER_${type.toUpperCase()}`] ? [visualizer({ open: true })] : []
}
const isDev = process.env.NODE_ENV === 'development'
const isProd = process.env.NODE_ENV === 'production'
export default defineConfig({
main: {
plugins: [externalizeDepsPlugin(), ...visualizerPlugin('main')],
@@ -22,16 +25,15 @@ export default defineConfig({
rollupOptions: {
external: ['@libsql/client', 'bufferutil', 'utf-8-validate', '@cherrystudio/mac-system-ocr'],
output: {
// 彻底禁用代码分割 - 返回 null 强制单文件打包
manualChunks: undefined,
// 内联所有动态导入,这是关键配置
inlineDynamicImports: true
manualChunks: undefined, // 彻底禁用代码分割 - 返回 null 强制单文件打包
inlineDynamicImports: true // 内联所有动态导入,这是关键配置
}
},
sourcemap: process.env.NODE_ENV === 'development'
sourcemap: isDev
},
esbuild: isProd ? { legalComments: 'none' } : {},
optimizeDeps: {
noDiscovery: process.env.NODE_ENV === 'development'
noDiscovery: isDev
}
},
preload: {
@@ -42,7 +44,7 @@ export default defineConfig({
}
},
build: {
sourcemap: process.env.NODE_ENV === 'development'
sourcemap: isDev
}
},
renderer: {
@@ -60,14 +62,7 @@ export default defineConfig({
]
]
}),
// 只在开发环境下启用 CodeInspectorPlugin
...(process.env.NODE_ENV === 'development'
? [
CodeInspectorPlugin({
bundler: 'vite'
})
]
: []),
...(isDev ? [CodeInspectorPlugin({ bundler: 'vite' })] : []), // 只在开发环境下启用 CodeInspectorPlugin
...visualizerPlugin('renderer')
],
resolve: {
@@ -95,6 +90,7 @@ export default defineConfig({
selectionAction: resolve(__dirname, 'src/renderer/selectionAction.html')
}
}
}
},
esbuild: isProd ? { legalComments: 'none' } : {}
}
})
+17 -9
View File
@@ -1,6 +1,6 @@
{
"name": "CherryStudio",
"version": "1.4.8",
"version": "1.5.1",
"private": true,
"description": "A powerful AI assistant for producer.",
"main": "./out/main/index.js",
@@ -27,12 +27,12 @@
"build:win": "dotenv npm run build && electron-builder --win --x64 --arm64",
"build:win:x64": "dotenv npm run build && electron-builder --win --x64",
"build:win:arm64": "dotenv npm run build && electron-builder --win --arm64",
"build:mac": "dotenv electron-vite build && electron-builder --mac --arm64 --x64",
"build:mac:arm64": "dotenv electron-vite build && electron-builder --mac --arm64",
"build:mac:x64": "dotenv electron-vite build && electron-builder --mac --x64",
"build:linux": "dotenv electron-vite build && electron-builder --linux --x64 --arm64",
"build:linux:arm64": "dotenv electron-vite build && electron-builder --linux --arm64",
"build:linux:x64": "dotenv electron-vite build && electron-builder --linux --x64",
"build:mac": "dotenv npm run build && electron-builder --mac --arm64 --x64",
"build:mac:arm64": "dotenv npm run build && electron-builder --mac --arm64",
"build:mac:x64": "dotenv npm run build && electron-builder --mac --x64",
"build:linux": "dotenv npm run build && electron-builder --linux --x64 --arm64",
"build:linux:arm64": "dotenv npm run build && electron-builder --linux --arm64",
"build:linux:x64": "dotenv npm run build && electron-builder --linux --x64",
"build:npm": "node scripts/build-npm.js",
"release": "node scripts/version.js",
"publish": "yarn build:check && yarn release patch push",
@@ -64,6 +64,7 @@
"@libsql/win32-x64-msvc": "^0.4.7",
"@strongtz/win32-arm64-msvc": "^0.4.7",
"iconv-lite": "^0.6.3",
"jaison": "^2.0.2",
"jschardet": "^3.1.4",
"jsdom": "26.1.0",
"macos-release": "^3.4.0",
@@ -71,7 +72,7 @@
"notion-helper": "^1.3.22",
"os-proxy-config": "^1.1.2",
"pdfjs-dist": "4.10.38",
"selection-hook": "^1.0.5",
"selection-hook": "^1.0.6",
"turndown": "7.2.0"
},
"devDependencies": {
@@ -92,6 +93,7 @@
"@cherrystudio/embedjs-loader-xml": "^0.1.31",
"@cherrystudio/embedjs-ollama": "^0.1.31",
"@cherrystudio/embedjs-openai": "^0.1.31",
"@codemirror/view": "^6.0.0",
"@electron-toolkit/eslint-config-prettier": "^3.0.0",
"@electron-toolkit/eslint-config-ts": "^3.0.0",
"@electron-toolkit/preload": "^3.0.0",
@@ -141,6 +143,8 @@
"@vitest/coverage-v8": "^3.1.4",
"@vitest/ui": "^3.1.4",
"@vitest/web-worker": "^3.1.4",
"@viz-js/lang-dot": "^1.0.5",
"@viz-js/viz": "^3.14.0",
"@xyflow/react": "^12.4.4",
"antd": "patch:antd@npm%3A5.24.7#~/.yarn/patches/antd-npm-5.24.7-356a553ae5.patch",
"archiver": "^7.0.1",
@@ -173,6 +177,7 @@
"eslint-plugin-unused-imports": "^4.1.4",
"fast-diff": "^1.3.0",
"fast-xml-parser": "^5.2.0",
"fetch-socks": "1.3.2",
"franc-min": "^6.2.0",
"fs-extra": "^11.2.0",
"google-auth-library": "^9.15.1",
@@ -225,6 +230,8 @@
"tiny-pinyin": "^1.3.2",
"tokenx": "^1.1.0",
"typescript": "^5.6.2",
"undici": "6.21.2",
"unified": "^11.0.5",
"uuid": "^10.0.0",
"vite": "6.2.6",
"vitest": "^3.1.4",
@@ -245,7 +252,8 @@
"app-builder-lib@npm:26.0.13": "patch:app-builder-lib@npm%3A26.0.13#~/.yarn/patches/app-builder-lib-npm-26.0.13-a064c9e1d0.patch",
"openai@npm:^4.87.3": "patch:openai@npm%3A5.1.0#~/.yarn/patches/openai-npm-5.1.0-0e7b3ccb07.patch",
"app-builder-lib@npm:26.0.15": "patch:app-builder-lib@npm%3A26.0.15#~/.yarn/patches/app-builder-lib-npm-26.0.15-360e5b0476.patch",
"@langchain/core@npm:^0.3.26": "patch:@langchain/core@npm%3A0.3.44#~/.yarn/patches/@langchain-core-npm-0.3.44-41d5c3cb0a.patch"
"@langchain/core@npm:^0.3.26": "patch:@langchain/core@npm%3A0.3.44#~/.yarn/patches/@langchain-core-npm-0.3.44-41d5c3cb0a.patch",
"undici": "6.21.2"
},
"packageManager": "yarn@4.9.1",
"lint-staged": {
+16 -1
View File
@@ -74,8 +74,10 @@ export enum IpcChannel {
Mcp_ServersChanged = 'mcp:servers-changed',
Mcp_ServersUpdated = 'mcp:servers-updated',
Mcp_CheckConnectivity = 'mcp:check-connectivity',
Mcp_UploadDxt = 'mcp:upload-dxt',
Mcp_SetProgress = 'mcp:set-progress',
Mcp_AbortTool = 'mcp:abort-tool',
Mcp_GetServerVersion = 'mcp:get-server-version',
// Python
Python_Execute = 'python:execute',
@@ -147,6 +149,7 @@ export enum IpcChannel {
File_Base64File = 'file:base64File',
File_GetPdfInfo = 'file:getPdfInfo',
Fs_Read = 'fs:read',
File_OpenWithRelativePath = 'file:openWithRelativePath',
// file service
FileService_Upload = 'file-service:upload',
@@ -241,5 +244,17 @@ export enum IpcChannel {
Selection_ActionWindowMinimize = 'selection:action-window-minimize',
Selection_ActionWindowPin = 'selection:action-window-pin',
Selection_ProcessAction = 'selection:process-action',
Selection_UpdateActionData = 'selection:update-action-data'
Selection_UpdateActionData = 'selection:update-action-data',
// Memory
Memory_Add = 'memory:add',
Memory_Search = 'memory:search',
Memory_List = 'memory:list',
Memory_Delete = 'memory:delete',
Memory_Update = 'memory:update',
Memory_Get = 'memory:get',
Memory_SetConfig = 'memory:set-config',
Memory_DeleteUser = 'memory:delete-user',
Memory_DeleteAllMemoriesForUser = 'memory:delete-all-memories-for-user',
Memory_GetUsersList = 'memory:get-users-list'
}
+1
View File
@@ -193,6 +193,7 @@ const textExtsByCategory = new Map([
'.htm',
'.xhtml', // HTML
'.xml', // XML
'.fxml', // JavaFX XML
'.org', // Org-mode
'.wiki', // Wiki
'.tex',
+17 -9
View File
@@ -43,7 +43,7 @@ async function downloadBunBinary(platform, arch, version = DEFAULT_BUN_VERSION,
if (!packageName) {
console.error(`No binary available for ${platformKey}`)
return false
return 101
}
// Create output directory structure
@@ -86,7 +86,7 @@ async function downloadBunBinary(platform, arch, version = DEFAULT_BUN_VERSION,
fs.chmodSync(outputPath, 0o755)
} catch (chmodError) {
console.error(`Warning: Failed to set executable permissions on ${filename}`)
return false
return 102
}
}
console.log(`Extracted ${entry.name} -> ${outputPath}`)
@@ -97,8 +97,10 @@ async function downloadBunBinary(platform, arch, version = DEFAULT_BUN_VERSION,
// Clean up
fs.unlinkSync(tempFilename)
console.log(`Successfully installed bun ${version} for ${platformKey}`)
return true
return 0
} catch (error) {
let retCode = 103
console.error(`Error installing bun for ${platformKey}: ${error.message}`)
// Clean up temporary file if it exists
if (fs.existsSync(tempFilename)) {
@@ -114,9 +116,10 @@ async function downloadBunBinary(platform, arch, version = DEFAULT_BUN_VERSION,
}
} catch (cleanupError) {
console.warn(`Warning: Failed to clean up directory: ${cleanupError.message}`)
retCode = 104
}
return false
return retCode
}
}
@@ -159,16 +162,21 @@ async function installBun() {
`Installing bun ${version} for ${platform}-${arch}${isMusl ? ' (MUSL)' : ''}${isBaseline ? ' (baseline)' : ''}...`
)
await downloadBunBinary(platform, arch, version, isMusl, isBaseline)
return await downloadBunBinary(platform, arch, version, isMusl, isBaseline)
}
// Run the installation
installBun()
.then(() => {
console.log('Installation successful')
process.exit(0)
.then((retCode) => {
if (retCode === 0) {
console.log('Installation successful')
process.exit(0)
} else {
console.error('Installation failed')
process.exit(retCode)
}
})
.catch((error) => {
console.error('Installation failed:', error)
process.exit(1)
process.exit(100)
})
+17 -9
View File
@@ -44,7 +44,7 @@ async function downloadUvBinary(platform, arch, version = DEFAULT_UV_VERSION, is
if (!packageName) {
console.error(`No binary available for ${platformKey}`)
return false
return 101
}
// Create output directory structure
@@ -85,7 +85,7 @@ async function downloadUvBinary(platform, arch, version = DEFAULT_UV_VERSION, is
fs.chmodSync(outputPath, 0o755)
} catch (chmodError) {
console.error(`Warning: Failed to set executable permissions on ${filename}`)
return false
return 102
}
}
console.log(`Extracted ${entry.name} -> ${outputPath}`)
@@ -95,8 +95,10 @@ async function downloadUvBinary(platform, arch, version = DEFAULT_UV_VERSION, is
await zip.close()
fs.unlinkSync(tempFilename)
console.log(`Successfully installed uv ${version} for ${platform}-${arch}`)
return true
return 0
} catch (error) {
let retCode = 103
console.error(`Error installing uv for ${platformKey}: ${error.message}`)
if (fs.existsSync(tempFilename)) {
@@ -112,9 +114,10 @@ async function downloadUvBinary(platform, arch, version = DEFAULT_UV_VERSION, is
}
} catch (cleanupError) {
console.warn(`Warning: Failed to clean up directory: ${cleanupError.message}`)
retCode = 104
}
return false
return retCode
}
}
@@ -154,16 +157,21 @@ async function installUv() {
console.log(`Installing uv ${version} for ${platform}-${arch}${isMusl ? ' (MUSL)' : ''}...`)
await downloadUvBinary(platform, arch, version, isMusl)
return await downloadUvBinary(platform, arch, version, isMusl)
}
// Run the installation
installUv()
.then(() => {
console.log('Installation successful')
process.exit(0)
.then((retCode) => {
if (retCode === 0) {
console.log('Installation successful')
process.exit(0)
} else {
console.error('Installation failed')
process.exit(retCode)
}
})
.catch((error) => {
console.error('Installation failed:', error)
process.exit(1)
process.exit(100)
})
+101 -18
View File
@@ -1,9 +1,60 @@
'use strict'
var __createBinding =
(this && this.__createBinding) ||
(Object.create
? function (o, m, k, k2) {
if (k2 === undefined) k2 = k
var desc = Object.getOwnPropertyDescriptor(m, k)
if (!desc || ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = {
enumerable: true,
get: function () {
return m[k]
}
}
}
Object.defineProperty(o, k2, desc)
}
: function (o, m, k, k2) {
if (k2 === undefined) k2 = k
o[k2] = m[k]
})
var __setModuleDefault =
(this && this.__setModuleDefault) ||
(Object.create
? function (o, v) {
Object.defineProperty(o, 'default', { enumerable: true, value: v })
}
: function (o, v) {
o['default'] = v
})
var __importStar =
(this && this.__importStar) ||
(function () {
var ownKeys = function (o) {
ownKeys =
Object.getOwnPropertyNames ||
function (o) {
var ar = []
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k
return ar
}
return ownKeys(o)
}
return function (mod) {
if (mod && mod.__esModule) return mod
var result = {}
if (mod != null)
for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== 'default') __createBinding(result, mod, k[i])
__setModuleDefault(result, mod)
return result
}
})()
Object.defineProperty(exports, '__esModule', { value: true })
var fs = require('fs')
var path = require('path')
var fs = __importStar(require('fs'))
var path = __importStar(require('path'))
var translationsDir = path.join(__dirname, '../src/renderer/src/i18n/locales')
var baseLocale = 'en-us'
var baseLocale = 'zh-cn'
var baseFileName = ''.concat(baseLocale, '.json')
var baseFilePath = path.join(translationsDir, baseFileName)
/**
@@ -48,12 +99,43 @@ function syncRecursively(target, template) {
}
return isUpdated
}
/**
* 检查 JSON 对象中是否存在重复键,并收集所有重复键
* @param obj 要检查的对象
* @returns 返回重复键的数组(若无重复则返回空数组)
*/
function checkDuplicateKeys(obj) {
var keys = new Set()
var duplicateKeys = []
var checkObject = function (obj, path) {
if (path === void 0) {
path = ''
}
for (var key in obj) {
var fullPath = path ? ''.concat(path, '.').concat(key) : key
if (keys.has(fullPath)) {
// 发现重复键时,添加到数组中(避免重复添加)
if (!duplicateKeys.includes(fullPath)) {
duplicateKeys.push(fullPath)
}
} else {
keys.add(fullPath)
}
// 递归检查子对象
if (typeof obj[key] === 'object' && obj[key] !== null) {
checkObject(obj[key], fullPath)
}
}
}
checkObject(obj)
return duplicateKeys
}
function syncTranslations() {
if (!fs.existsSync(baseFilePath)) {
console.error(
'\u4E3B\u6A21\u677F\u6587\u4EF6 '.concat(
baseFileName,
' \u4E0D\u5B58\u5728\uFF0C\u8BF7\u68C0\u67E5\u8DEF\u5F84\u6216\u6587\u4EF6\u540D\u3002'
' \u4E0D\u5B58\u5728\uFF0C\u8BF7\u68C0\u67E5\u8DEF\u5F84\u6216\u6587\u4EF6\u540D'
)
)
return
@@ -63,9 +145,18 @@ function syncTranslations() {
try {
baseJson = JSON.parse(baseContent)
} catch (error) {
console.error('\u89E3\u6790 '.concat(baseFileName, ' \u51FA\u9519:'), error)
console.error('\u89E3\u6790 '.concat(baseFileName, ' \u51FA\u9519\u3002').concat(error))
return
}
// 检查主模板是否存在重复键
var duplicateKeys = checkDuplicateKeys(baseJson)
if (duplicateKeys.length > 0) {
throw new Error(
'\u4E3B\u6A21\u677F\u6587\u4EF6 '
.concat(baseFileName, ' \u5B58\u5728\u4EE5\u4E0B\u91CD\u590D\u952E\uFF1A\n')
.concat(duplicateKeys.join('\n'))
)
}
var files = fs.readdirSync(translationsDir).filter(function (file) {
return file.endsWith('.json') && file !== baseFileName
})
@@ -77,27 +168,19 @@ function syncTranslations() {
var fileContent = fs.readFileSync(filePath, 'utf-8')
targetJson = JSON.parse(fileContent)
} catch (error) {
console.error(
'\u89E3\u6790 '.concat(
file,
' \u51FA\u9519\uFF0C\u8DF3\u8FC7\u6B64\u6587\u4EF6\u3002\u9519\u8BEF\u4FE1\u606F:'
),
error
)
console.error('\u89E3\u6790 '.concat(file, ' \u51FA\u9519\uFF0C\u8DF3\u8FC7\u6B64\u6587\u4EF6\u3002'), error)
continue
}
var isUpdated = syncRecursively(targetJson, baseJson)
if (isUpdated) {
try {
fs.writeFileSync(filePath, JSON.stringify(targetJson, null, 2), 'utf-8')
console.log(
'\u6587\u4EF6 '.concat(file, ' \u5DF2\u66F4\u65B0\u540C\u6B65\u4E3B\u6A21\u677F\u7684\u5185\u5BB9\u3002')
)
fs.writeFileSync(filePath, JSON.stringify(targetJson, null, 2) + '\n', 'utf-8')
console.log('\u6587\u4EF6 '.concat(file, ' \u5DF2\u66F4\u65B0\u540C\u6B65\u4E3B\u6A21\u677F\u7684\u5185\u5BB9'))
} catch (error) {
console.error('\u5199\u5165 '.concat(file, ' \u51FA\u9519:'), error)
console.error('\u5199\u5165 '.concat(file, ' \u51FA\u9519\u3002').concat(error))
}
} else {
console.log('\u6587\u4EF6 '.concat(file, ' \u65E0\u9700\u66F4\u65B0\u3002'))
console.log('\u6587\u4EF6 '.concat(file, ' \u65E0\u9700\u66F4\u65B0'))
}
}
}
+43 -4
View File
@@ -2,7 +2,7 @@ import * as fs from 'fs'
import * as path from 'path'
const translationsDir = path.join(__dirname, '../src/renderer/src/i18n/locales')
const baseLocale = 'zh-CN'
const baseLocale = 'zh-cn'
const baseFileName = `${baseLocale}.json`
const baseFilePath = path.join(translationsDir, baseFileName)
@@ -52,6 +52,39 @@ function syncRecursively(target: any, template: any): boolean {
return isUpdated
}
/**
* 检查 JSON 对象中是否存在重复键,并收集所有重复键
* @param obj 要检查的对象
* @returns 返回重复键的数组(若无重复则返回空数组)
*/
function checkDuplicateKeys(obj: Record<string, any>): string[] {
const keys = new Set<string>()
const duplicateKeys: string[] = []
const checkObject = (obj: Record<string, any>, path: string = '') => {
for (const key in obj) {
const fullPath = path ? `${path}.${key}` : key
if (keys.has(fullPath)) {
// 发现重复键时,添加到数组中(避免重复添加)
if (!duplicateKeys.includes(fullPath)) {
duplicateKeys.push(fullPath)
}
} else {
keys.add(fullPath)
}
// 递归检查子对象
if (typeof obj[key] === 'object' && obj[key] !== null) {
checkObject(obj[key], fullPath)
}
}
}
checkObject(obj)
return duplicateKeys
}
function syncTranslations() {
if (!fs.existsSync(baseFilePath)) {
console.error(`主模板文件 ${baseFileName} 不存在,请检查路径或文件名`)
@@ -63,10 +96,16 @@ function syncTranslations() {
try {
baseJson = JSON.parse(baseContent)
} catch (error) {
console.error(`解析 ${baseFileName} 出错:`, error)
console.error(`解析 ${baseFileName} 出错${error}`)
return
}
// 检查主模板是否存在重复键
const duplicateKeys = checkDuplicateKeys(baseJson)
if (duplicateKeys.length > 0) {
throw new Error(`主模板文件 ${baseFileName} 存在以下重复键:\n${duplicateKeys.join('\n')}`)
}
const files = fs.readdirSync(translationsDir).filter((file) => file.endsWith('.json') && file !== baseFileName)
for (const file of files) {
@@ -76,7 +115,7 @@ function syncTranslations() {
const fileContent = fs.readFileSync(filePath, 'utf-8')
targetJson = JSON.parse(fileContent)
} catch (error) {
console.error(`解析 ${file} 出错,跳过此文件。错误信息:`, error)
console.error(`解析 ${file} 出错,跳过此文件。`, error)
continue
}
@@ -87,7 +126,7 @@ function syncTranslations() {
fs.writeFileSync(filePath, JSON.stringify(targetJson, null, 2) + '\n', 'utf-8')
console.log(`文件 ${file} 已更新同步主模板的内容`)
} catch (error) {
console.error(`写入 ${file} 出错:`, error)
console.error(`写入 ${file} 出错${error}`)
}
} else {
console.log(`文件 ${file} 无需更新`)
+9 -1
View File
@@ -11,7 +11,7 @@ import { app } from 'electron'
import installExtension, { REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS } from 'electron-devtools-installer'
import Logger from 'electron-log'
import { isDev, isWin } from './constant'
import { isDev, isWin, isLinux } from './constant'
import { registerIpc } from './ipc'
import { configManager } from './services/ConfigManager'
import mcpService from './services/MCPService'
@@ -46,6 +46,14 @@ if (isWin) {
app.commandLine.appendSwitch('wm-window-animations-disabled')
}
/**
* Enable GlobalShortcutsPortal for Linux Wayland Protocol
* see: https://www.electronjs.org/docs/latest/api/global-shortcut
*/
if (isLinux && process.env.XDG_SESSION_TYPE === 'wayland') {
app.commandLine.appendSwitch('enable-features', 'GlobalShortcutsPortal')
}
// Enable features for unresponsive renderer js call stacks
app.commandLine.appendSwitch('enable-features', 'DocumentPolicyIncludeJSCallStacksInCrashReports')
app.on('web-contents-created', (_, webContents) => {
+60 -4
View File
@@ -8,7 +8,7 @@ import { handleZoomFactor } from '@main/utils/zoom'
import { UpgradeChannel } from '@shared/config/constant'
import { IpcChannel } from '@shared/IpcChannel'
import { FileMetadata, Provider, Shortcut, ThemeMode } from '@types'
import { BrowserWindow, dialog, ipcMain, session, shell, systemPreferences, webContents } from 'electron'
import { BrowserWindow, dialog, ipcMain, ProxyConfig, session, shell, systemPreferences, webContents } from 'electron'
import log from 'electron-log'
import { Notification } from 'src/renderer/src/types/notification'
@@ -17,15 +17,17 @@ import AppUpdater from './services/AppUpdater'
import BackupManager from './services/BackupManager'
import { configManager } from './services/ConfigManager'
import CopilotService from './services/CopilotService'
import DxtService from './services/DxtService'
import { ExportService } from './services/ExportService'
import FileStorage from './services/FileStorage'
import FileService from './services/FileSystemService'
import KnowledgeService from './services/KnowledgeService'
import mcpService from './services/MCPService'
import MemoryService from './services/memory/MemoryService'
import NotificationService from './services/NotificationService'
import * as NutstoreService from './services/NutstoreService'
import ObsidianVaultService from './services/ObsidianVaultService'
import { ProxyConfig, proxyManager } from './services/ProxyManager'
import { proxyManager } from './services/ProxyManager'
import { pythonService } from './services/PythonService'
import { FileServiceManager } from './services/remotefile/FileServiceManager'
import { searchService } from './services/SearchService'
@@ -46,6 +48,8 @@ const backupManager = new BackupManager()
const exportService = new ExportService(fileManager)
const obsidianVaultService = new ObsidianVaultService()
const vertexAIService = VertexAIService.getInstance()
const memoryService = MemoryService.getInstance()
const dxtService = new DxtService()
export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) {
const appUpdater = new AppUpdater(mainWindow)
@@ -74,9 +78,9 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) {
if (proxy === 'system') {
proxyConfig = { mode: 'system' }
} else if (proxy) {
proxyConfig = { mode: 'custom', url: proxy }
proxyConfig = { mode: 'fixed_servers', proxyRules: proxy }
} else {
proxyConfig = { mode: 'none' }
proxyConfig = { mode: 'direct' }
}
await proxyManager.configureProxy(proxyConfig)
@@ -399,6 +403,7 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) {
ipcMain.handle(IpcChannel.File_Download, fileManager.downloadFile)
ipcMain.handle(IpcChannel.File_Copy, fileManager.copyFile)
ipcMain.handle(IpcChannel.File_BinaryImage, fileManager.binaryImage)
ipcMain.handle(IpcChannel.File_OpenWithRelativePath, fileManager.openFileWithRelativePath)
// file service
ipcMain.handle(IpcChannel.FileService_Upload, async (_, provider: Provider, file: FileMetadata) => {
@@ -452,6 +457,38 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) {
ipcMain.handle(IpcChannel.KnowledgeBase_Rerank, KnowledgeService.rerank)
ipcMain.handle(IpcChannel.KnowledgeBase_Check_Quota, KnowledgeService.checkQuota)
// memory
ipcMain.handle(IpcChannel.Memory_Add, async (_, messages, config) => {
return await memoryService.add(messages, config)
})
ipcMain.handle(IpcChannel.Memory_Search, async (_, query, config) => {
return await memoryService.search(query, config)
})
ipcMain.handle(IpcChannel.Memory_List, async (_, config) => {
return await memoryService.list(config)
})
ipcMain.handle(IpcChannel.Memory_Delete, async (_, id) => {
return await memoryService.delete(id)
})
ipcMain.handle(IpcChannel.Memory_Update, async (_, id, memory, metadata) => {
return await memoryService.update(id, memory, metadata)
})
ipcMain.handle(IpcChannel.Memory_Get, async (_, memoryId) => {
return await memoryService.get(memoryId)
})
ipcMain.handle(IpcChannel.Memory_SetConfig, async (_, config) => {
memoryService.setConfig(config)
})
ipcMain.handle(IpcChannel.Memory_DeleteUser, async (_, userId) => {
return await memoryService.deleteUser(userId)
})
ipcMain.handle(IpcChannel.Memory_DeleteAllMemoriesForUser, async (_, userId) => {
return await memoryService.deleteAllMemoriesForUser(userId)
})
ipcMain.handle(IpcChannel.Memory_GetUsersList, async () => {
return await memoryService.getUsersList()
})
// window
ipcMain.handle(IpcChannel.Windows_SetMinimumSize, (_, width: number, height: number) => {
mainWindow?.setMinimumSize(width, height)
@@ -502,10 +539,29 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) {
ipcMain.handle(IpcChannel.Mcp_GetInstallInfo, mcpService.getInstallInfo)
ipcMain.handle(IpcChannel.Mcp_CheckConnectivity, mcpService.checkMcpConnectivity)
ipcMain.handle(IpcChannel.Mcp_AbortTool, mcpService.abortTool)
ipcMain.handle(IpcChannel.Mcp_GetServerVersion, mcpService.getServerVersion)
ipcMain.handle(IpcChannel.Mcp_SetProgress, (_, progress: number) => {
mainWindow.webContents.send('mcp-progress', progress)
})
// DXT upload handler
ipcMain.handle(IpcChannel.Mcp_UploadDxt, async (event, fileBuffer: ArrayBuffer, fileName: string) => {
try {
// Create a temporary file with the uploaded content
const tempPath = await fileManager.createTempFile(event, fileName)
await fileManager.writeFile(event, tempPath, Buffer.from(fileBuffer))
// Process DXT file using the temporary path
return await dxtService.uploadDxt(event, tempPath)
} catch (error) {
log.error('[IPC] DXT upload error:', error)
return {
success: false,
error: error instanceof Error ? error.message : 'Failed to upload DXT file'
}
}
})
// Register Python execution handler
ipcMain.handle(
IpcChannel.Python_Execute,
@@ -217,7 +217,7 @@ export default class Doc2xPreprocessProvider extends BasePreprocessProvider {
* @param filePath 文件路径
*/
private async convertFile(uid: string, filePath: string): Promise<void> {
const fileName = path.basename(filePath).split('.')[0]
const fileName = path.parse(filePath).name
const config = {
...this.createAuthConfig(),
headers: {
@@ -1,19 +1,15 @@
import type { BaseEmbeddings } from '@cherrystudio/embedjs-interfaces'
import { KnowledgeBaseParams } from '@types'
import { ApiClient } from '@types'
import EmbeddingsFactory from './EmbeddingsFactory'
export default class Embeddings {
private sdk: BaseEmbeddings
constructor({ model, provider, apiKey, apiVersion, baseURL, dimensions }: KnowledgeBaseParams) {
constructor({ embedApiClient, dimensions }: { embedApiClient: ApiClient; dimensions?: number }) {
this.sdk = EmbeddingsFactory.create({
model,
provider,
apiKey,
apiVersion,
baseURL,
embedApiClient,
dimensions
} as KnowledgeBaseParams)
})
}
public async init(): Promise<void> {
return this.sdk.init()
@@ -3,28 +3,22 @@ import { OllamaEmbeddings } from '@cherrystudio/embedjs-ollama'
import { OpenAiEmbeddings } from '@cherrystudio/embedjs-openai'
import { AzureOpenAiEmbeddings } from '@cherrystudio/embedjs-openai/src/azure-openai-embeddings'
import { getInstanceName } from '@main/utils'
import { KnowledgeBaseParams } from '@types'
import { ApiClient } from '@types'
import { SUPPORTED_DIM_MODELS as VOYAGE_SUPPORTED_DIM_MODELS, VoyageEmbeddings } from './VoyageEmbeddings'
import { VOYAGE_SUPPORTED_DIM_MODELS } from './utils'
import { VoyageEmbeddings } from './VoyageEmbeddings'
export default class EmbeddingsFactory {
static create({ model, provider, apiKey, apiVersion, baseURL, dimensions }: KnowledgeBaseParams): BaseEmbeddings {
static create({ embedApiClient, dimensions }: { embedApiClient: ApiClient; dimensions?: number }): BaseEmbeddings {
const batchSize = 10
const { model, provider, apiKey, apiVersion, baseURL } = embedApiClient
if (provider === 'voyageai') {
if (VOYAGE_SUPPORTED_DIM_MODELS.includes(model)) {
return new VoyageEmbeddings({
modelName: model,
apiKey,
outputDimension: dimensions,
batchSize: 8
})
} else {
return new VoyageEmbeddings({
modelName: model,
apiKey,
batchSize: 8
})
}
return new VoyageEmbeddings({
modelName: model,
apiKey,
outputDimension: VOYAGE_SUPPORTED_DIM_MODELS.includes(model) ? dimensions : undefined,
batchSize: 8
})
}
if (provider === 'ollama') {
if (baseURL.includes('v1/')) {
@@ -1,27 +1,29 @@
import { BaseEmbeddings } from '@cherrystudio/embedjs-interfaces'
import { VoyageEmbeddings as _VoyageEmbeddings } from '@langchain/community/embeddings/voyage'
import { VOYAGE_SUPPORTED_DIM_MODELS } from './utils'
/**
*
*/
export const SUPPORTED_DIM_MODELS = ['voyage-3-large', 'voyage-3.5', 'voyage-3.5-lite', 'voyage-code-3']
export class VoyageEmbeddings extends BaseEmbeddings {
private model: _VoyageEmbeddings
constructor(private readonly configuration?: ConstructorParameters<typeof _VoyageEmbeddings>[0]) {
super()
if (!this.configuration) this.configuration = {}
if (!this.configuration.modelName) this.configuration.modelName = 'voyage-3'
if (!SUPPORTED_DIM_MODELS.includes(this.configuration.modelName) && this.configuration.outputDimension) {
throw new Error(`VoyageEmbeddings only supports ${SUPPORTED_DIM_MODELS.join(', ')}`)
if (!this.configuration) {
throw new Error('Pass in a configuration.')
}
if (!this.configuration.modelName) this.configuration.modelName = 'voyage-3'
this.model = new _VoyageEmbeddings(this.configuration)
if (!VOYAGE_SUPPORTED_DIM_MODELS.includes(this.configuration.modelName) && this.configuration.outputDimension) {
console.error(`VoyageEmbeddings only supports ${VOYAGE_SUPPORTED_DIM_MODELS.join(', ')} to set outputDimension.`)
this.model = new _VoyageEmbeddings({ ...this.configuration, outputDimension: undefined })
} else {
this.model = new _VoyageEmbeddings(this.configuration)
}
}
override async getDimensions(): Promise<number> {
if (!this.configuration?.outputDimension) {
throw new Error('You need to pass in the optional dimensions parameter for this model')
}
return this.configuration?.outputDimension
return this.configuration?.outputDimension ?? (this.configuration?.modelName === 'voyage-code-2' ? 1536 : 1024)
}
override async embedDocuments(texts: string[]): Promise<number[][]> {
+45
View File
@@ -0,0 +1,45 @@
export const VOYAGE_SUPPORTED_DIM_MODELS = ['voyage-3-large', 'voyage-3.5', 'voyage-3.5-lite', 'voyage-code-3']
// NOTE: 下面的暂时没用上,但先留着吧
export const OPENAI_SUPPORTED_DIM_MODELS = ['text-embedding-3-small', 'text-embedding-3-large']
export const DASHSCOPE_SUPPORTED_DIM_MODELS = ['text-embedding-v3', 'text-embedding-v4']
export const OPENSOURCE_SUPPORTED_DIM_MODELS = ['qwen3-embedding-0.6B', 'qwen3-embedding-4B', 'qwen3-embedding-8B']
export const GOOGLE_SUPPORTED_DIM_MODELS = ['gemini-embedding-exp-03-07', 'gemini-embedding-exp']
export const SUPPORTED_DIM_MODELS = [
...VOYAGE_SUPPORTED_DIM_MODELS,
...OPENAI_SUPPORTED_DIM_MODELS,
...DASHSCOPE_SUPPORTED_DIM_MODELS,
...OPENSOURCE_SUPPORTED_DIM_MODELS,
...GOOGLE_SUPPORTED_DIM_MODELS
]
/**
* 从模型 ID 中提取基础名称。
* 例如:
* - 'deepseek/deepseek-r1' => 'deepseek-r1'
* - 'deepseek-ai/deepseek/deepseek-r1' => 'deepseek-r1'
* @param {string} id 模型 ID
* @param {string} [delimiter='/'] 分隔符,默认为 '/'
* @returns {string} 基础名称
*/
export const getBaseModelName = (id: string, delimiter: string = '/'): string => {
const parts = id.split(delimiter)
return parts[parts.length - 1]
}
/**
* 从模型 ID 中提取基础名称并转换为小写。
* 例如:
* - 'deepseek/DeepSeek-R1' => 'deepseek-r1'
* - 'deepseek-ai/deepseek/DeepSeek-R1' => 'deepseek-r1'
* @param {string} id 模型 ID
* @param {string} [delimiter='/'] 分隔符,默认为 '/'
* @returns {string} 小写的基础名称
*/
export const getLowerBaseModelName = (id: string, delimiter: string = '/'): string => {
return getBaseModelName(id, delimiter).toLowerCase()
}
@@ -114,7 +114,7 @@ export async function addFileLoader(
// HTML类型处理
loaderReturn = await ragApplication.addLoader(
new WebLoader({
urlOrContent: readTextFileWithAutoEncoding(file.path),
urlOrContent: await readTextFileWithAutoEncoding(file.path),
chunkSize: base.chunkSize,
chunkOverlap: base.chunkOverlap
}) as any,
@@ -124,7 +124,7 @@ export async function addFileLoader(
case 'json':
try {
jsonObject = JSON.parse(readTextFileWithAutoEncoding(file.path))
jsonObject = JSON.parse(await readTextFileWithAutoEncoding(file.path))
} catch (error) {
jsonParsed = false
Logger.warn('[KnowledgeBase] failed parsing json file, falling back to text processing:', file.path, error)
@@ -140,7 +140,7 @@ export async function addFileLoader(
// 如果是其他文本类型且尚未读取文件,则读取文件
loaderReturn = await ragApplication.addLoader(
new TextLoader({
text: readTextFileWithAutoEncoding(file.path),
text: await readTextFileWithAutoEncoding(file.path),
chunkSize: base.chunkSize,
chunkOverlap: base.chunkOverlap
}) as any,
@@ -5,7 +5,7 @@ export default abstract class BaseReranker {
protected base: KnowledgeBaseParams
constructor(base: KnowledgeBaseParams) {
if (!base.rerankModel) {
if (!base.rerankApiClient) {
throw new Error('Rerank model is required')
}
this.base = base
@@ -17,11 +17,11 @@ export default abstract class BaseReranker {
* Get Rerank Request Url
*/
protected getRerankUrl() {
if (this.base.rerankModelProvider === 'bailian') {
if (this.base.rerankApiClient?.provider === 'bailian') {
return 'https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank'
}
let baseURL = this.base.rerankBaseURL
let baseURL = this.base.rerankApiClient?.baseURL
if (baseURL && baseURL.endsWith('/')) {
// `/` 结尾强制使用rerankBaseURL
@@ -39,20 +39,20 @@ export default abstract class BaseReranker {
* Get Rerank Request Body
*/
protected getRerankRequestBody(query: string, searchResults: ExtractChunkData[]) {
const provider = this.base.rerankModelProvider
const provider = this.base.rerankApiClient?.provider
const documents = searchResults.map((doc) => doc.pageContent)
const topN = this.base.documentCount
if (provider === 'voyageai') {
return {
model: this.base.rerankModel,
model: this.base.rerankApiClient?.model,
query,
documents,
top_k: topN
}
} else if (provider === 'bailian') {
return {
model: this.base.rerankModel,
model: this.base.rerankApiClient?.model,
input: {
query,
documents
@@ -69,7 +69,7 @@ export default abstract class BaseReranker {
}
} else {
return {
model: this.base.rerankModel,
model: this.base.rerankApiClient?.model,
query,
documents,
top_n: topN
@@ -81,7 +81,7 @@ export default abstract class BaseReranker {
* Extract Rerank Result
*/
protected extractRerankResult(data: any) {
const provider = this.base.rerankModelProvider
const provider = this.base.rerankApiClient?.provider
if (provider === 'bailian') {
return data.output.results
} else if (provider === 'voyageai') {
@@ -129,7 +129,7 @@ export default abstract class BaseReranker {
public defaultHeaders() {
return {
Authorization: `Bearer ${this.base.rerankApiKey}`,
Authorization: `Bearer ${this.base.rerankApiClient?.apiKey}`,
'Content-Type': 'application/json'
}
}
@@ -1,6 +1,6 @@
import { ExtractChunkData } from '@cherrystudio/embedjs-interfaces'
import AxiosProxy from '@main/services/AxiosProxy'
import { KnowledgeBaseParams } from '@types'
import axios from 'axios'
import BaseReranker from './BaseReranker'
@@ -15,7 +15,7 @@ export default class GeneralReranker extends BaseReranker {
const requestBody = this.getRerankRequestBody(query, searchResults)
try {
const { data } = await AxiosProxy.axios.post(url, requestBody, { headers: this.defaultHeaders() })
const { data } = await axios.post(url, requestBody, { headers: this.defaultHeaders() })
const rerankResults = this.extractRerankResult(data)
return this.getRerankResult(searchResults, rerankResults)
-29
View File
@@ -1,29 +0,0 @@
import { AxiosInstance, default as axios_ } from 'axios'
import { ProxyAgent } from 'proxy-agent'
import { proxyManager } from './ProxyManager'
class AxiosProxy {
private cacheAxios: AxiosInstance | null = null
private proxyAgent: ProxyAgent | null = null
get axios(): AxiosInstance {
const currentProxyAgent = proxyManager.getProxyAgent()
// 如果代理发生变化或尚未初始化,则重新创建 axios 实例
if (this.cacheAxios === null || (currentProxyAgent !== null && this.proxyAgent !== currentProxyAgent)) {
this.proxyAgent = currentProxyAgent
// 创建带有代理配置的 axios 实例
this.cacheAxios = axios_.create({
proxy: false,
httpAgent: currentProxyAgent || undefined,
httpsAgent: currentProxyAgent || undefined
})
}
return this.cacheAxios
}
}
export default new AxiosProxy()
+14 -6
View File
@@ -321,14 +321,22 @@ class BackupManager {
async backupToWebdav(_: Electron.IpcMainInvokeEvent, data: string, webdavConfig: WebDavConfig) {
const filename = webdavConfig.fileName || 'cherry-studio.backup.zip'
const backupedFilePath = await this.backup(_, filename, data, undefined, webdavConfig.skipBackupFile)
const contentLength = (await fs.stat(backupedFilePath)).size
const webdavClient = new WebDav(webdavConfig)
try {
const result = await webdavClient.putFileContents(filename, fs.createReadStream(backupedFilePath), {
overwrite: true,
contentLength
})
// 上传成功后删除本地备份文件
let result
if (webdavConfig.disableStream) {
const fileContent = await fs.readFile(backupedFilePath)
result = await webdavClient.putFileContents(filename, fileContent, {
overwrite: true
})
} else {
const contentLength = (await fs.stat(backupedFilePath)).size
result = await webdavClient.putFileContents(filename, fs.createReadStream(backupedFilePath), {
overwrite: true,
contentLength
})
}
await fs.remove(backupedFilePath)
return result
} catch (error) {
+2 -1
View File
@@ -25,7 +25,8 @@ export enum ConfigKeys {
SelectionAssistantRemeberWinSize = 'selectionAssistantRemeberWinSize',
SelectionAssistantFilterMode = 'selectionAssistantFilterMode',
SelectionAssistantFilterList = 'selectionAssistantFilterList',
DisableHardwareAcceleration = 'disableHardwareAcceleration'
DisableHardwareAcceleration = 'disableHardwareAcceleration',
Proxy = 'proxy'
}
export class ConfigManager {
+5 -6
View File
@@ -1,11 +1,10 @@
import { AxiosRequestConfig } from 'axios'
import axios from 'axios'
import { app, safeStorage } from 'electron'
import Logger from 'electron-log'
import fs from 'fs/promises'
import path from 'path'
import aoxisProxy from './AxiosProxy'
// 配置常量,集中管理
const CONFIG = {
GITHUB_CLIENT_ID: 'Iv1.b507a08c87ecfe98',
@@ -96,7 +95,7 @@ class CopilotService {
}
}
const response = await aoxisProxy.axios.get(CONFIG.API_URLS.GITHUB_USER, config)
const response = await axios.get(CONFIG.API_URLS.GITHUB_USER, config)
return {
login: response.data.login,
avatar: response.data.avatar_url
@@ -117,7 +116,7 @@ class CopilotService {
try {
this.updateHeaders(headers)
const response = await aoxisProxy.axios.post<AuthResponse>(
const response = await axios.post<AuthResponse>(
CONFIG.API_URLS.GITHUB_DEVICE_CODE,
{
client_id: CONFIG.GITHUB_CLIENT_ID,
@@ -149,7 +148,7 @@ class CopilotService {
await this.delay(currentDelay)
try {
const response = await aoxisProxy.axios.post<TokenResponse>(
const response = await axios.post<TokenResponse>(
CONFIG.API_URLS.GITHUB_ACCESS_TOKEN,
{
client_id: CONFIG.GITHUB_CLIENT_ID,
@@ -211,7 +210,7 @@ class CopilotService {
}
}
const response = await aoxisProxy.axios.get<CopilotTokenResponse>(CONFIG.API_URLS.COPILOT_TOKEN, config)
const response = await axios.get<CopilotTokenResponse>(CONFIG.API_URLS.COPILOT_TOKEN, config)
return response.data
} catch (error) {
+396
View File
@@ -0,0 +1,396 @@
import { getMcpDir, getTempDir } from '@main/utils/file'
import logger from 'electron-log'
import * as fs from 'fs'
import StreamZip from 'node-stream-zip'
import * as os from 'os'
import * as path from 'path'
import { v4 as uuidv4 } from 'uuid'
// Type definitions
export interface DxtManifest {
dxt_version: string
name: string
display_name?: string
version: string
description?: string
long_description?: string
author?: {
name?: string
email?: string
url?: string
}
repository?: {
type?: string
url?: string
}
homepage?: string
documentation?: string
support?: string
icon?: string
server: {
type: string
entry_point: string
mcp_config: {
command: string
args: string[]
env?: Record<string, string>
platform_overrides?: {
[platform: string]: {
command?: string
args?: string[]
env?: Record<string, string>
}
}
}
}
tools?: Array<{
name: string
description: string
}>
keywords?: string[]
license?: string
user_config?: Record<string, any>
compatibility?: {
claude_desktop?: string
platforms?: string[]
runtimes?: Record<string, string>
}
}
export interface DxtUploadResult {
success: boolean
data?: {
manifest: DxtManifest
extractDir: string
}
error?: string
}
export function performVariableSubstitution(
value: string,
extractDir: string,
userConfig?: Record<string, any>
): string {
let result = value
// Replace ${__dirname} with the extraction directory
result = result.replace(/\$\{__dirname\}/g, extractDir)
// Replace ${HOME} with user's home directory
result = result.replace(/\$\{HOME\}/g, os.homedir())
// Replace ${DESKTOP} with user's desktop directory
const desktopDir = path.join(os.homedir(), 'Desktop')
result = result.replace(/\$\{DESKTOP\}/g, desktopDir)
// Replace ${DOCUMENTS} with user's documents directory
const documentsDir = path.join(os.homedir(), 'Documents')
result = result.replace(/\$\{DOCUMENTS\}/g, documentsDir)
// Replace ${DOWNLOADS} with user's downloads directory
const downloadsDir = path.join(os.homedir(), 'Downloads')
result = result.replace(/\$\{DOWNLOADS\}/g, downloadsDir)
// Replace ${pathSeparator} or ${/} with the platform-specific path separator
result = result.replace(/\$\{pathSeparator\}/g, path.sep)
result = result.replace(/\$\{\/\}/g, path.sep)
// Replace ${user_config.KEY} with user-configured values
if (userConfig) {
result = result.replace(/\$\{user_config\.([^}]+)\}/g, (match, key) => {
return userConfig[key] || match // Keep original if not found
})
}
return result
}
export function applyPlatformOverrides(mcpConfig: any, extractDir: string, userConfig?: Record<string, any>): any {
const platform = process.platform
const resolvedConfig = { ...mcpConfig }
// Apply platform-specific overrides
if (mcpConfig.platform_overrides && mcpConfig.platform_overrides[platform]) {
const override = mcpConfig.platform_overrides[platform]
// Override command if specified
if (override.command) {
resolvedConfig.command = override.command
}
// Override args if specified
if (override.args) {
resolvedConfig.args = override.args
}
// Merge environment variables
if (override.env) {
resolvedConfig.env = { ...resolvedConfig.env, ...override.env }
}
}
// Apply variable substitution to all string values
if (resolvedConfig.command) {
resolvedConfig.command = performVariableSubstitution(resolvedConfig.command, extractDir, userConfig)
}
if (resolvedConfig.args) {
resolvedConfig.args = resolvedConfig.args.map((arg: string) =>
performVariableSubstitution(arg, extractDir, userConfig)
)
}
if (resolvedConfig.env) {
for (const [key, value] of Object.entries(resolvedConfig.env)) {
resolvedConfig.env[key] = performVariableSubstitution(value as string, extractDir, userConfig)
}
}
return resolvedConfig
}
export interface ResolvedMcpConfig {
command: string
args: string[]
env?: Record<string, string>
}
class DxtService {
private tempDir = path.join(getTempDir(), 'dxt_uploads')
private mcpDir = getMcpDir()
constructor() {
this.ensureDirectories()
}
private ensureDirectories() {
try {
// Create temp directory
if (!fs.existsSync(this.tempDir)) {
fs.mkdirSync(this.tempDir, { recursive: true })
}
// Create MCP directory
if (!fs.existsSync(this.mcpDir)) {
fs.mkdirSync(this.mcpDir, { recursive: true })
}
} catch (error) {
logger.error('[DxtService] Failed to create directories:', error)
}
}
private async moveDirectory(source: string, destination: string): Promise<void> {
try {
// Try rename first (works if on same filesystem)
fs.renameSync(source, destination)
} catch (error) {
// If rename fails (cross-filesystem), use copy + remove
logger.info('[DxtService] Cross-filesystem move detected, using copy + remove')
// Ensure parent directory exists
const parentDir = path.dirname(destination)
if (!fs.existsSync(parentDir)) {
fs.mkdirSync(parentDir, { recursive: true })
}
// Recursively copy directory
await this.copyDirectory(source, destination)
// Remove source directory
fs.rmSync(source, { recursive: true, force: true })
}
}
private async copyDirectory(source: string, destination: string): Promise<void> {
// Create destination directory
fs.mkdirSync(destination, { recursive: true })
// Read source directory
const entries = fs.readdirSync(source, { withFileTypes: true })
// Copy each entry
for (const entry of entries) {
const sourcePath = path.join(source, entry.name)
const destPath = path.join(destination, entry.name)
if (entry.isDirectory()) {
await this.copyDirectory(sourcePath, destPath)
} else {
fs.copyFileSync(sourcePath, destPath)
}
}
}
public async uploadDxt(_: Electron.IpcMainInvokeEvent, filePath: string): Promise<DxtUploadResult> {
const tempExtractDir = path.join(this.tempDir, `dxt_${uuidv4()}`)
try {
// Validate file exists
if (!fs.existsSync(filePath)) {
throw new Error('DXT file not found')
}
// Extract the DXT file (which is a ZIP archive) to a temporary directory
logger.info('[DxtService] Extracting DXT file:', filePath)
const zip = new StreamZip.async({ file: filePath })
await zip.extract(null, tempExtractDir)
await zip.close()
// Read and validate the manifest.json
const manifestPath = path.join(tempExtractDir, 'manifest.json')
if (!fs.existsSync(manifestPath)) {
throw new Error('manifest.json not found in DXT file')
}
const manifestContent = fs.readFileSync(manifestPath, 'utf-8')
const manifest: DxtManifest = JSON.parse(manifestContent)
// Validate required fields in manifest
if (!manifest.dxt_version) {
throw new Error('Invalid manifest: missing dxt_version')
}
if (!manifest.name) {
throw new Error('Invalid manifest: missing name')
}
if (!manifest.version) {
throw new Error('Invalid manifest: missing version')
}
if (!manifest.server) {
throw new Error('Invalid manifest: missing server configuration')
}
if (!manifest.server.mcp_config) {
throw new Error('Invalid manifest: missing server.mcp_config')
}
if (!manifest.server.mcp_config.command) {
throw new Error('Invalid manifest: missing server.mcp_config.command')
}
if (!Array.isArray(manifest.server.mcp_config.args)) {
throw new Error('Invalid manifest: server.mcp_config.args must be an array')
}
// Use server name as the final extract directory for automatic version management
// Sanitize the name to prevent creating subdirectories
const sanitizedName = manifest.name.replace(/\//g, '-')
const serverDirName = `server-${sanitizedName}`
const finalExtractDir = path.join(this.mcpDir, serverDirName)
// Clean up any existing version of this server
if (fs.existsSync(finalExtractDir)) {
logger.info('[DxtService] Removing existing server directory:', finalExtractDir)
fs.rmSync(finalExtractDir, { recursive: true, force: true })
}
// Move the temporary directory to the final location
// Use recursive copy + remove instead of rename to handle cross-filesystem moves
await this.moveDirectory(tempExtractDir, finalExtractDir)
logger.info('[DxtService] DXT server extracted to:', finalExtractDir)
// Clean up the uploaded DXT file if it's in temp directory
if (filePath.startsWith(this.tempDir)) {
fs.unlinkSync(filePath)
}
// Return success with manifest and extraction path
return {
success: true,
data: {
manifest,
extractDir: finalExtractDir
}
}
} catch (error) {
// Clean up on error
if (fs.existsSync(tempExtractDir)) {
fs.rmSync(tempExtractDir, { recursive: true, force: true })
}
const errorMessage = error instanceof Error ? error.message : 'Failed to process DXT file'
logger.error('[DxtService] DXT upload error:', error)
return {
success: false,
error: errorMessage
}
}
}
/**
* Get resolved MCP configuration for a DXT server with platform overrides and variable substitution
*/
public getResolvedMcpConfig(dxtPath: string, userConfig?: Record<string, any>): ResolvedMcpConfig | null {
try {
// Read the manifest from the DXT server directory
const manifestPath = path.join(dxtPath, 'manifest.json')
if (!fs.existsSync(manifestPath)) {
logger.error('[DxtService] Manifest not found:', manifestPath)
return null
}
const manifestContent = fs.readFileSync(manifestPath, 'utf-8')
const manifest: DxtManifest = JSON.parse(manifestContent)
if (!manifest.server?.mcp_config) {
logger.error('[DxtService] No mcp_config found in manifest')
return null
}
// Apply platform overrides and variable substitution
const resolvedConfig = applyPlatformOverrides(manifest.server.mcp_config, dxtPath, userConfig)
logger.info('[DxtService] Resolved MCP config:', {
command: resolvedConfig.command,
args: resolvedConfig.args,
env: resolvedConfig.env ? Object.keys(resolvedConfig.env) : undefined
})
return resolvedConfig
} catch (error) {
logger.error('[DxtService] Failed to resolve MCP config:', error)
return null
}
}
public cleanupDxtServer(serverName: string): boolean {
try {
// Handle server names that might contain slashes (e.g., "anthropic/sequential-thinking")
// by replacing slashes with the same separator used during installation
const sanitizedName = serverName.replace(/\//g, '-')
const serverDirName = `server-${sanitizedName}`
const serverDir = path.join(this.mcpDir, serverDirName)
// First try the sanitized path
if (fs.existsSync(serverDir)) {
logger.info('[DxtService] Removing DXT server directory:', serverDir)
fs.rmSync(serverDir, { recursive: true, force: true })
return true
}
// Fallback: try with original name in case it was stored differently
const originalServerDir = path.join(this.mcpDir, `server-${serverName}`)
if (fs.existsSync(originalServerDir)) {
logger.info('[DxtService] Removing DXT server directory:', originalServerDir)
fs.rmSync(originalServerDir, { recursive: true, force: true })
return true
}
logger.warn('[DxtService] Server directory not found:', serverDir)
return false
} catch (error) {
logger.error('[DxtService] Failed to cleanup DXT server:', error)
return false
}
}
public cleanup() {
try {
// Clean up temp directory
if (fs.existsSync(this.tempDir)) {
fs.rmSync(this.tempDir, { recursive: true, force: true })
}
} catch (error) {
logger.error('[DxtService] Cleanup error:', error)
}
}
}
export default DxtService
+24 -4
View File
@@ -231,7 +231,11 @@ class FileStorage {
await fs.promises.rm(path.join(this.storageDir, id), { recursive: true })
}
public readFile = async (_: Electron.IpcMainInvokeEvent, id: string): Promise<string> => {
public readFile = async (
_: Electron.IpcMainInvokeEvent,
id: string,
detectEncoding: boolean = false
): Promise<string> => {
const filePath = path.join(this.storageDir, id)
const fileExtension = path.extname(filePath)
@@ -259,11 +263,14 @@ class FileStorage {
}
try {
const result = readTextFileWithAutoEncoding(filePath)
return result
if (detectEncoding) {
return readTextFileWithAutoEncoding(filePath)
} else {
return fs.readFileSync(filePath, 'utf-8')
}
} catch (error) {
logger.error(error)
return 'failed to read file'
throw new Error(`Failed to read file: ${filePath}.`)
}
}
@@ -417,6 +424,19 @@ class FileStorage {
shell.openPath(path).catch((err) => logger.error('[IPC - Error] Failed to open file:', err))
}
/**
* 通过相对路径打开文件,跨设备时使用
* @param file
*/
public openFileWithRelativePath = async (_: Electron.IpcMainInvokeEvent, file: FileMetadata): Promise<void> => {
const filePath = path.join(this.storageDir, file.name)
if (fs.existsSync(filePath)) {
shell.openPath(filePath).catch((err) => logger.error('[IPC - Error] Failed to open file:', err))
} else {
logger.warn('[IPC - Warning] File does not exist:', filePath)
}
}
public save = async (
_: Electron.IpcMainInvokeEvent,
fileName: string,
+10 -16
View File
@@ -21,12 +21,12 @@ import type { ExtractChunkData } from '@cherrystudio/embedjs-interfaces'
import { LibSqlDb } from '@cherrystudio/embedjs-libsql'
import { SitemapLoader } from '@cherrystudio/embedjs-loader-sitemap'
import { WebLoader } from '@cherrystudio/embedjs-loader-web'
import Embeddings from '@main/knowledage/embeddings/Embeddings'
import { addFileLoader } from '@main/knowledage/loader'
import { NoteLoader } from '@main/knowledage/loader/noteLoader'
import OcrProvider from '@main/knowledage/ocr/OcrProvider'
import PreprocessProvider from '@main/knowledage/preprocess/PreprocessProvider'
import Reranker from '@main/knowledage/reranker/Reranker'
import Embeddings from '@main/knowledge/embeddings/Embeddings'
import { addFileLoader } from '@main/knowledge/loader'
import { NoteLoader } from '@main/knowledge/loader/noteLoader'
import Reranker from '@main/knowledge/reranker/Reranker'
import { windowService } from '@main/services/WindowService'
import { getDataPath } from '@main/utils'
import { getAllFiles } from '@main/utils/file'
@@ -120,27 +120,21 @@ class KnowledgeService {
private getRagApplication = async ({
id,
model,
provider,
apiKey,
apiVersion,
baseURL,
dimensions
embedApiClient,
dimensions,
documentCount
}: KnowledgeBaseParams): Promise<RAGApplication> => {
let ragApplication: RAGApplication
const embeddings = new Embeddings({
model,
provider,
apiKey,
apiVersion,
baseURL,
embedApiClient,
dimensions
} as KnowledgeBaseParams)
})
try {
ragApplication = await new RAGApplicationBuilder()
.setModel('NO_MODEL')
.setEmbeddingModel(embeddings)
.setVectorDatabase(new LibSqlDb({ path: path.join(this.storageDir, id) }))
.setSearchResultCount(documentCount || 30)
.build()
} catch (e) {
Logger.error(e)
+168 -5
View File
@@ -14,6 +14,16 @@ import {
type StreamableHTTPClientTransportOptions
} from '@modelcontextprotocol/sdk/client/streamableHttp'
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory'
// Import notification schemas from MCP SDK
import {
CancelledNotificationSchema,
LoggingMessageNotificationSchema,
ProgressNotificationSchema,
PromptListChangedNotificationSchema,
ResourceListChangedNotificationSchema,
ResourceUpdatedNotificationSchema,
ToolListChangedNotificationSchema
} from '@modelcontextprotocol/sdk/types.js'
import { nanoid } from '@reduxjs/toolkit'
import {
GetMCPPromptResponse,
@@ -31,6 +41,7 @@ import { memoize } from 'lodash'
import { v4 as uuidv4 } from 'uuid'
import { CacheService } from './CacheService'
import DxtService from './DxtService'
import { CallBackServer } from './mcp/oauth/callback'
import { McpOAuthClientProvider } from './mcp/oauth/provider'
import getLoginShellEnvironment from './mcp/shell-env'
@@ -72,6 +83,7 @@ function withCache<T extends unknown[], R>(
class McpService {
private clients: Map<string, Client> = new Map()
private pendingClients: Map<string, Promise<Client>> = new Map()
private dxtService = new DxtService()
private activeToolCalls: Map<string, AbortController> = new Map()
constructor() {
@@ -88,6 +100,8 @@ class McpService {
this.stopServer = this.stopServer.bind(this)
this.abortTool = this.abortTool.bind(this)
this.cleanup = this.cleanup.bind(this)
this.checkMcpConnectivity = this.checkMcpConnectivity.bind(this)
this.getServerVersion = this.getServerVersion.bind(this)
}
private getServerKey(server: MCPServer): string {
@@ -136,7 +150,7 @@ class McpService {
// Create new client instance for each connection
const client = new Client({ name: 'Cherry Studio', version: app.getVersion() }, { capabilities: {} })
const args = [...(server.args || [])]
let args = [...(server.args || [])]
// let transport: StdioClientTransport | SSEClientTransport | InMemoryTransport | StreamableHTTPClientTransport
const authProvider = new McpOAuthClientProvider({
@@ -206,6 +220,23 @@ class McpService {
} else if (server.command) {
let cmd = server.command
// For DXT servers, use resolved configuration with platform overrides and variable substitution
if (server.dxtPath) {
const resolvedConfig = this.dxtService.getResolvedMcpConfig(server.dxtPath)
if (resolvedConfig) {
cmd = resolvedConfig.command
args = resolvedConfig.args
// Merge resolved environment variables with existing ones
server.env = {
...server.env,
...resolvedConfig.env
}
Logger.info(`[MCP] Using resolved DXT config - command: ${cmd}, args: ${args?.join(' ')}`)
} else {
Logger.warn(`[MCP] Failed to resolve DXT config for ${server.name}, falling back to manifest values`)
}
}
if (server.command === 'npx') {
cmd = await getBinaryPath('bun')
Logger.info(`[MCP] Using command: ${cmd}`)
@@ -252,7 +283,7 @@ class McpService {
this.removeProxyEnv(loginShellEnv)
}
const stdioTransport = new StdioClientTransport({
const transportOptions: any = {
command: cmd,
args,
env: {
@@ -260,7 +291,15 @@ class McpService {
...server.env
},
stderr: 'pipe'
})
}
// For DXT servers, set the working directory to the extracted path
if (server.dxtPath) {
transportOptions.cwd = server.dxtPath
Logger.info(`[MCP] Setting working directory for DXT server: ${server.dxtPath}`)
}
const stdioTransport = new StdioClientTransport(transportOptions)
stdioTransport.stderr?.on('data', (data) =>
Logger.info(`[MCP] Stdio stderr for server: ${server.name} `, data.toString())
)
@@ -334,6 +373,12 @@ class McpService {
// Store the new client in the cache
this.clients.set(serverKey, client)
// Set up notification handlers
this.setupNotificationHandlers(client, server)
// Clear existing cache to ensure fresh data
this.clearServerCache(serverKey)
Logger.info(`[MCP] Activated server: ${server.name}`)
return client
} catch (error: any) {
@@ -352,6 +397,79 @@ class McpService {
return initPromise
}
/**
* Set up notification handlers for MCP client
*/
private setupNotificationHandlers(client: Client, server: MCPServer) {
const serverKey = this.getServerKey(server)
try {
// Set up tools list changed notification handler
client.setNotificationHandler(ToolListChangedNotificationSchema, async () => {
Logger.info(`[MCP] Tools list changed for server: ${server.name}`)
// Clear tools cache
CacheService.remove(`mcp:list_tool:${serverKey}`)
})
// Set up resources list changed notification handler
client.setNotificationHandler(ResourceListChangedNotificationSchema, async () => {
Logger.info(`[MCP] Resources list changed for server: ${server.name}`)
// Clear resources cache
CacheService.remove(`mcp:list_resources:${serverKey}`)
})
// Set up prompts list changed notification handler
client.setNotificationHandler(PromptListChangedNotificationSchema, async () => {
Logger.info(`[MCP] Prompts list changed for server: ${server.name}`)
// Clear prompts cache
CacheService.remove(`mcp:list_prompts:${serverKey}`)
})
// Set up resource updated notification handler
client.setNotificationHandler(ResourceUpdatedNotificationSchema, async () => {
Logger.info(`[MCP] Resource updated for server: ${server.name}`)
// Clear resource-specific caches
this.clearResourceCaches(serverKey)
})
// Set up progress notification handler
client.setNotificationHandler(ProgressNotificationSchema, async (notification) => {
Logger.info(`[MCP] Progress notification received for server: ${server.name}`, notification.params)
})
// Set up cancelled notification handler
client.setNotificationHandler(CancelledNotificationSchema, async (notification) => {
Logger.info(`[MCP] Operation cancelled for server: ${server.name}`, notification.params)
})
// Set up logging message notification handler
client.setNotificationHandler(LoggingMessageNotificationSchema, async (notification) => {
Logger.info(`[MCP] Message from server ${server.name}:`, notification.params)
})
Logger.info(`[MCP] Set up notification handlers for server: ${server.name}`)
} catch (error) {
Logger.error(`[MCP] Failed to set up notification handlers for server ${server.name}:`, error)
}
}
/**
* Clear resource-specific caches for a server
*/
private clearResourceCaches(serverKey: string) {
CacheService.remove(`mcp:list_resources:${serverKey}`)
}
/**
* Clear all caches for a specific server
*/
private clearServerCache(serverKey: string) {
CacheService.remove(`mcp:list_tool:${serverKey}`)
CacheService.remove(`mcp:list_prompts:${serverKey}`)
CacheService.remove(`mcp:list_resources:${serverKey}`)
Logger.info(`[MCP] Cleared all caches for server: ${serverKey}`)
}
async closeClient(serverKey: string) {
const client = this.clients.get(serverKey)
if (client) {
@@ -359,8 +477,8 @@ class McpService {
await client.close()
Logger.info(`[MCP] Closed server: ${serverKey}`)
this.clients.delete(serverKey)
CacheService.remove(`mcp:list_tool:${serverKey}`)
Logger.info(`[MCP] Cleared cache for server: ${serverKey}`)
// Clear all caches for this server
this.clearServerCache(serverKey)
} else {
Logger.warn(`[MCP] No client found for server: ${serverKey}`)
}
@@ -378,12 +496,26 @@ class McpService {
if (existingClient) {
await this.closeClient(serverKey)
}
// If this is a DXT server, cleanup its directory
if (server.dxtPath) {
try {
const cleaned = this.dxtService.cleanupDxtServer(server.name)
if (cleaned) {
Logger.info(`[MCP] Cleaned up DXT server directory for: ${server.name}`)
}
} catch (error) {
Logger.error(`[MCP] Failed to cleanup DXT server: ${server.name}`, error)
}
}
}
async restartServer(_: Electron.IpcMainInvokeEvent, server: MCPServer) {
Logger.info(`[MCP] Restarting server: ${server.name}`)
const serverKey = this.getServerKey(server)
await this.closeClient(serverKey)
// Clear cache before restarting to ensure fresh data
this.clearServerCache(serverKey)
await this.initClient(server)
}
@@ -403,6 +535,12 @@ class McpService {
public async checkMcpConnectivity(_: Electron.IpcMainInvokeEvent, server: MCPServer): Promise<boolean> {
Logger.info(`[MCP] Checking connectivity for server: ${server.name}`)
try {
Logger.info(`[MCP] About to call initClient for server: ${server.name}`, { hasInitClient: !!this.initClient })
if (!this.initClient) {
throw new Error('initClient method is not available')
}
const client = await this.initClient(server)
// Attempt to list tools as a way to check connectivity
await client.listTools()
@@ -692,6 +830,31 @@ class McpService {
return false
}
}
/**
* Get the server version information
*/
public async getServerVersion(_: Electron.IpcMainInvokeEvent, server: MCPServer): Promise<string | null> {
try {
Logger.info(`[MCP] Getting server version for: ${server.name}`)
const client = await this.initClient(server)
// Try to get server information which may include version
const serverInfo = client.getServerVersion()
Logger.info(`[MCP] Server info for ${server.name}:`, serverInfo)
if (serverInfo && serverInfo.version) {
Logger.info(`[MCP] Server version for ${server.name}: ${serverInfo.version}`)
return serverInfo.version
}
Logger.warn(`[MCP] No version information available for server: ${server.name}`)
return null
} catch (error: any) {
Logger.error(`[MCP] Failed to get server version for ${server.name}:`, error?.message)
return null
}
}
}
export default new McpService()
+1 -4
View File
@@ -1,8 +1,6 @@
import { BrowserWindow, Notification as ElectronNotification } from 'electron'
import { Notification } from 'src/renderer/src/types/notification'
import icon from '../../../build/icon.png?asset'
class NotificationService {
private window: BrowserWindow
@@ -15,8 +13,7 @@ class NotificationService {
// 使用 Electron Notification API
const electronNotification = new ElectronNotification({
title: notification.title,
body: notification.message,
icon: icon
body: notification.message
})
electronNotification.on('click', () => {
+187 -88
View File
@@ -1,38 +1,54 @@
import { ProxyConfig as _ProxyConfig, session } from 'electron'
import axios from 'axios'
import { app, ProxyConfig, session } from 'electron'
import Logger from 'electron-log'
import { socksDispatcher } from 'fetch-socks'
import http from 'http'
import https from 'https'
import { getSystemProxy } from 'os-proxy-config'
import { ProxyAgent as GeneralProxyAgent } from 'proxy-agent'
// import { ProxyAgent, setGlobalDispatcher } from 'undici'
type ProxyMode = 'system' | 'custom' | 'none'
export interface ProxyConfig {
mode: ProxyMode
url?: string
}
import { ProxyAgent } from 'proxy-agent'
import { Dispatcher, EnvHttpProxyAgent, getGlobalDispatcher, setGlobalDispatcher } from 'undici'
export class ProxyManager {
private config: ProxyConfig
private proxyAgent: GeneralProxyAgent | null = null
private config: ProxyConfig = { mode: 'direct' }
private systemProxyInterval: NodeJS.Timeout | null = null
private isSettingProxy = false
private originalGlobalDispatcher: Dispatcher
private originalSocksDispatcher: Dispatcher
// for http and https
private originalHttpGet: typeof http.get
private originalHttpRequest: typeof http.request
private originalHttpsGet: typeof https.get
private originalHttpsRequest: typeof https.request
constructor() {
this.config = {
mode: 'none'
}
}
private async setSessionsProxy(config: _ProxyConfig): Promise<void> {
const sessions = [session.defaultSession, session.fromPartition('persist:webview')]
await Promise.all(sessions.map((session) => session.setProxy(config)))
this.originalGlobalDispatcher = getGlobalDispatcher()
this.originalSocksDispatcher = global[Symbol.for('undici.globalDispatcher.1')]
this.originalHttpGet = http.get
this.originalHttpRequest = http.request
this.originalHttpsGet = https.get
this.originalHttpsRequest = https.request
}
private async monitorSystemProxy(): Promise<void> {
// Clear any existing interval first
this.clearSystemProxyMonitor()
// Set new interval
this.systemProxyInterval = setInterval(async () => {
await this.setSystemProxy()
}, 10000)
this.systemProxyInterval = setInterval(
async () => {
const currentProxy = await getSystemProxy()
if (currentProxy && currentProxy.proxyUrl.toLowerCase() === this.config.proxyRules) {
return
}
await this.configureProxy({
mode: 'system',
proxyRules: currentProxy?.proxyUrl.toLowerCase()
})
},
// 1 minutes
1000 * 60
)
}
private clearSystemProxyMonitor(): void {
@@ -43,99 +59,182 @@ export class ProxyManager {
}
async configureProxy(config: ProxyConfig): Promise<void> {
Logger.info('configureProxy', config.mode, config.proxyRules)
if (this.isSettingProxy) {
return
}
this.isSettingProxy = true
try {
if (config?.mode === this.config?.mode && config?.proxyRules === this.config?.proxyRules) {
Logger.info('proxy config is the same, skip configure')
return
}
this.config = config
this.clearSystemProxyMonitor()
if (this.config.mode === 'system') {
await this.setSystemProxy()
this.monitorSystemProxy()
} else if (this.config.mode === 'custom') {
await this.setCustomProxy()
} else {
await this.clearProxy()
if (config.mode === 'system') {
const currentProxy = await getSystemProxy()
if (currentProxy) {
Logger.info('current system proxy', currentProxy.proxyUrl)
this.config.proxyRules = currentProxy.proxyUrl.toLowerCase()
this.monitorSystemProxy()
} else {
// no system proxy, use direct mode
this.config.mode = 'direct'
}
}
this.setGlobalProxy()
} catch (error) {
console.error('Failed to config proxy:', error)
Logger.error('Failed to config proxy:', error)
throw error
} finally {
this.isSettingProxy = false
}
}
private setEnvironment(url: string): void {
if (url === '') {
delete process.env.HTTP_PROXY
delete process.env.HTTPS_PROXY
delete process.env.grpc_proxy
delete process.env.http_proxy
delete process.env.https_proxy
delete process.env.SOCKS_PROXY
delete process.env.ALL_PROXY
return
}
process.env.grpc_proxy = url
process.env.HTTP_PROXY = url
process.env.HTTPS_PROXY = url
process.env.http_proxy = url
process.env.https_proxy = url
}
private async setSystemProxy(): Promise<void> {
try {
const currentProxy = await getSystemProxy()
if (!currentProxy || currentProxy.proxyUrl === this.config.url) {
return
}
await this.setSessionsProxy({ mode: 'system' })
this.config.url = currentProxy.proxyUrl.toLowerCase()
this.setEnvironment(this.config.url)
this.proxyAgent = new GeneralProxyAgent()
} catch (error) {
console.error('Failed to set system proxy:', error)
throw error
if (url.startsWith('socks')) {
process.env.SOCKS_PROXY = url
process.env.ALL_PROXY = url
}
}
private async setCustomProxy(): Promise<void> {
try {
if (this.config.url) {
this.setEnvironment(this.config.url)
this.proxyAgent = new GeneralProxyAgent()
await this.setSessionsProxy({ proxyRules: this.config.url })
private setGlobalProxy() {
this.setEnvironment(this.config.proxyRules || '')
this.setGlobalFetchProxy(this.config)
this.setSessionsProxy(this.config)
this.setGlobalHttpProxy(this.config)
}
private setGlobalHttpProxy(config: ProxyConfig) {
const proxyUrl = config.proxyRules
if (config.mode === 'direct' || !proxyUrl) {
http.get = this.originalHttpGet
http.request = this.originalHttpRequest
https.get = this.originalHttpsGet
https.request = this.originalHttpsRequest
axios.defaults.proxy = undefined
axios.defaults.httpAgent = undefined
axios.defaults.httpsAgent = undefined
return
}
// ProxyAgent 从环境变量读取代理配置
const agent = new ProxyAgent()
// axios 使用代理
axios.defaults.proxy = false
axios.defaults.httpAgent = agent
axios.defaults.httpsAgent = agent
http.get = this.bindHttpMethod(this.originalHttpGet, agent)
http.request = this.bindHttpMethod(this.originalHttpRequest, agent)
https.get = this.bindHttpMethod(this.originalHttpsGet, agent)
https.request = this.bindHttpMethod(this.originalHttpsRequest, agent)
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
private bindHttpMethod(originalMethod: Function, agent: http.Agent | https.Agent) {
return (...args: any[]) => {
let url: string | URL | undefined
let options: http.RequestOptions | https.RequestOptions
let callback: (res: http.IncomingMessage) => void
if (typeof args[0] === 'string' || args[0] instanceof URL) {
url = args[0]
if (typeof args[1] === 'function') {
options = {}
callback = args[1]
} else {
options = {
...args[1]
}
callback = args[2]
}
} else {
options = {
...args[0]
}
callback = args[1]
}
} catch (error) {
console.error('Failed to set custom proxy:', error)
throw error
// for webdav https self-signed certificate
if (options.agent instanceof https.Agent) {
;(agent as https.Agent).options.rejectUnauthorized = options.agent.options.rejectUnauthorized
}
// 确保只设置 agent,不修改其他网络选项
if (!options.agent) {
options.agent = agent
}
if (url) {
return originalMethod(url, options, callback)
}
return originalMethod(options, callback)
}
}
private clearEnvironment(): void {
delete process.env.HTTP_PROXY
delete process.env.HTTPS_PROXY
delete process.env.grpc_proxy
delete process.env.http_proxy
delete process.env.https_proxy
private setGlobalFetchProxy(config: ProxyConfig) {
const proxyUrl = config.proxyRules
if (config.mode === 'direct' || !proxyUrl) {
setGlobalDispatcher(this.originalGlobalDispatcher)
global[Symbol.for('undici.globalDispatcher.1')] = this.originalSocksDispatcher
return
}
const url = new URL(proxyUrl)
if (url.protocol === 'http:' || url.protocol === 'https:') {
setGlobalDispatcher(new EnvHttpProxyAgent())
return
}
global[Symbol.for('undici.globalDispatcher.1')] = socksDispatcher({
port: parseInt(url.port),
type: url.protocol === 'socks4:' ? 4 : 5,
host: url.hostname,
userId: url.username || undefined,
password: url.password || undefined
})
}
private async clearProxy(): Promise<void> {
this.clearEnvironment()
await this.setSessionsProxy({ mode: 'direct' })
this.config = { mode: 'none' }
this.proxyAgent = null
}
private async setSessionsProxy(config: ProxyConfig): Promise<void> {
let c = config
getProxyAgent(): GeneralProxyAgent | null {
return this.proxyAgent
}
if (config.mode === 'direct' || !config.proxyRules) {
c = { mode: 'direct' }
}
getProxyUrl(): string {
return this.config.url || ''
}
const sessions = [session.defaultSession, session.fromPartition('persist:webview')]
await Promise.all(sessions.map((session) => session.setProxy(c)))
// setGlobalProxy() {
// const proxyUrl = this.config.url
// if (proxyUrl) {
// const [protocol, address] = proxyUrl.split('://')
// const [host, port] = address.split(':')
// if (!protocol.includes('socks')) {
// setGlobalDispatcher(new ProxyAgent(proxyUrl))
// } else {
// global[Symbol.for('undici.globalDispatcher.1')] = socksDispatcher({
// port: parseInt(port),
// type: protocol === 'socks5' ? 5 : 4,
// host: host
// })
// }
// }
// }
// set proxy for electron
app.setProxy(c)
}
}
export const proxyManager = new ProxyManager()
+155 -89
View File
@@ -1,7 +1,7 @@
import { SELECTION_FINETUNED_LIST, SELECTION_PREDEFINED_BLACKLIST } from '@main/configs/SelectionConfig'
import { isDev, isMac, isWin } from '@main/constant'
import { IpcChannel } from '@shared/IpcChannel'
import { BrowserWindow, ipcMain, screen, systemPreferences } from 'electron'
import { app, BrowserWindow, ipcMain, screen, systemPreferences } from 'electron'
import Logger from 'electron-log'
import { join } from 'path'
import type {
@@ -509,54 +509,55 @@ export class SelectionService {
//should set every time the window is shown
this.toolbarWindow!.setAlwaysOnTop(true, 'screen-saver')
// [macOS] a series of hacky ways only for macOS
if (isMac) {
// [macOS] a hacky way
// when set `skipTransformProcessType: true`, if the selection is in self app, it will make the selection canceled after toolbar showing
// so we just don't set `skipTransformProcessType: true` when in self app
const isSelf = ['com.github.Electron', 'com.kangfenmao.CherryStudio'].includes(programName)
if (!isSelf) {
// [macOS] an ugly hacky way
// `focusable: true` will make mainWindow disappeared when `setVisibleOnAllWorkspaces`
// so we set `focusable: true` before showing, and then set false after showing
this.toolbarWindow!.setFocusable(false)
// [macOS]
// force `setVisibleOnAllWorkspaces: true` to let toolbar show in all workspaces. And we MUST not set it to false again
// set `skipTransformProcessType: true` to avoid dock icon spinning when `setVisibleOnAllWorkspaces`
this.toolbarWindow!.setVisibleOnAllWorkspaces(true, {
visibleOnFullScreen: true,
skipTransformProcessType: true
})
}
// [macOS] MUST use `showInactive()` to prevent other windows bring to front together
// [Windows] is OK for both `show()` and `showInactive()` because of `focusable: false`
this.toolbarWindow!.showInactive()
// [macOS] restore the focusable status
this.toolbarWindow!.setFocusable(true)
if (!isMac) {
this.toolbarWindow!.show()
/**
* [Windows]
* In Windows 10, setOpacity(1) will make the window completely transparent
* It's a strange behavior, so we don't use it for compatibility
*/
// this.toolbarWindow!.setOpacity(1)
this.startHideByMouseKeyListener()
return
}
/**
* The following is for Windows
*/
/************************************************
* [macOS] the following code is only for macOS
*
* WARNING:
* DO NOT MODIFY THESE CODES, UNLESS YOU REALLY KNOW WHAT YOU ARE DOING!!!!
*************************************************/
this.toolbarWindow!.show()
// [macOS] a hacky way
// when set `skipTransformProcessType: true`, if the selection is in self app, it will make the selection canceled after toolbar showing
// so we just don't set `skipTransformProcessType: true` when in self app
const isSelf = ['com.github.Electron', 'com.kangfenmao.CherryStudio'].includes(programName)
/**
* [Windows]
* In Windows 10, setOpacity(1) will make the window completely transparent
* It's a strange behavior, so we don't use it for compatibility
*/
// this.toolbarWindow!.setOpacity(1)
if (!isSelf) {
// [macOS] an ugly hacky way
// `focusable: true` will make mainWindow disappeared when `setVisibleOnAllWorkspaces`
// so we set `focusable: true` before showing, and then set false after showing
this.toolbarWindow!.setFocusable(false)
// [macOS]
// force `setVisibleOnAllWorkspaces: true` to let toolbar show in all workspaces. And we MUST not set it to false again
// set `skipTransformProcessType: true` to avoid dock icon spinning when `setVisibleOnAllWorkspaces`
this.toolbarWindow!.setVisibleOnAllWorkspaces(true, {
visibleOnFullScreen: true,
skipTransformProcessType: true
})
}
// [macOS] MUST use `showInactive()` to prevent other windows bring to front together
// [Windows] is OK for both `show()` and `showInactive()` because of `focusable: false`
this.toolbarWindow!.showInactive()
// [macOS] restore the focusable status
this.toolbarWindow!.setFocusable(true)
this.startHideByMouseKeyListener()
return
}
/**
@@ -911,6 +912,7 @@ export class SelectionService {
refPoint = { x: Math.round(refPoint.x), y: Math.round(refPoint.y) }
}
// [macOS] isFullscreen is only available on macOS
this.showToolbarAtPosition(refPoint, refOrientation, selectionData.programName)
this.toolbarWindow!.webContents.send(IpcChannel.Selection_TextSelected, selectionData)
}
@@ -1218,20 +1220,26 @@ export class SelectionService {
return actionWindow
}
public processAction(actionItem: ActionItem): void {
/**
* Process action item
* @param actionItem Action item to process
* @param isFullScreen [macOS] only macOS has the available isFullscreen mode
*/
public processAction(actionItem: ActionItem, isFullScreen: boolean = false): void {
const actionWindow = this.popActionWindow()
actionWindow.webContents.send(IpcChannel.Selection_UpdateActionData, actionItem)
this.showActionWindow(actionWindow)
this.showActionWindow(actionWindow, isFullScreen)
}
/**
* Show action window with proper positioning relative to toolbar
* Ensures window stays within screen boundaries
* @param actionWindow Window to position and show
* @param isFullScreen [macOS] only macOS has the available isFullscreen mode
*/
private showActionWindow(actionWindow: BrowserWindow): void {
private showActionWindow(actionWindow: BrowserWindow, isFullScreen: boolean = false): void {
let actionWindowWidth = this.ACTION_WINDOW_WIDTH
let actionWindowHeight = this.ACTION_WINDOW_HEIGHT
@@ -1241,68 +1249,125 @@ export class SelectionService {
actionWindowHeight = this.lastActionWindowSize.height
}
//center way
/********************************************
* Setting the position of the action window
********************************************/
const display = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
const workArea = display.workArea
// Center of the screen
if (!this.isFollowToolbar || !this.toolbarWindow) {
const display = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
const workArea = display.workArea
const centerX = workArea.x + (workArea.width - actionWindowWidth) / 2
const centerY = workArea.y + (workArea.height - actionWindowHeight) / 2
const centerX = Math.round(workArea.x + (workArea.width - actionWindowWidth) / 2)
const centerY = Math.round(workArea.y + (workArea.height - actionWindowHeight) / 2)
actionWindow.setPosition(centerX, centerY, false)
actionWindow.setBounds({
width: actionWindowWidth,
height: actionWindowHeight,
x: Math.round(centerX),
y: Math.round(centerY)
x: centerX,
y: centerY
})
} else {
// Follow toolbar position
const toolbarBounds = this.toolbarWindow!.getBounds()
const GAP = 6 // 6px gap from screen edges
//make sure action window is inside screen
if (actionWindowWidth > workArea.width - 2 * GAP) {
actionWindowWidth = workArea.width - 2 * GAP
}
if (actionWindowHeight > workArea.height - 2 * GAP) {
actionWindowHeight = workArea.height - 2 * GAP
}
// Calculate initial position to center action window horizontally below toolbar
let posX = Math.round(toolbarBounds.x + (toolbarBounds.width - actionWindowWidth) / 2)
let posY = Math.round(toolbarBounds.y)
// Ensure action window stays within screen boundaries with a small gap
if (posX + actionWindowWidth > workArea.x + workArea.width) {
posX = workArea.x + workArea.width - actionWindowWidth - GAP
} else if (posX < workArea.x) {
posX = workArea.x + GAP
}
if (posY + actionWindowHeight > workArea.y + workArea.height) {
// If window would go below screen, try to position it above toolbar
posY = workArea.y + workArea.height - actionWindowHeight - GAP
} else if (posY < workArea.y) {
posY = workArea.y + GAP
}
actionWindow.setPosition(posX, posY, false)
//KEY to make window not resize
actionWindow.setBounds({
width: actionWindowWidth,
height: actionWindowHeight,
x: posX,
y: posY
})
}
if (!isMac) {
actionWindow.show()
return
}
//follow toolbar
const toolbarBounds = this.toolbarWindow!.getBounds()
const display = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
const workArea = display.workArea
const GAP = 6 // 6px gap from screen edges
/************************************************
* [macOS] the following code is only for macOS
*
* WARNING:
* DO NOT MODIFY THESE CODES, UNLESS YOU REALLY KNOW WHAT YOU ARE DOING!!!!
*************************************************/
//make sure action window is inside screen
if (actionWindowWidth > workArea.width - 2 * GAP) {
actionWindowWidth = workArea.width - 2 * GAP
// act normally when the app is not in fullscreen mode
if (!isFullScreen) {
actionWindow.show()
return
}
if (actionWindowHeight > workArea.height - 2 * GAP) {
actionWindowHeight = workArea.height - 2 * GAP
}
// [macOS] an UGLY HACKY way for fullscreen override settings
// Calculate initial position to center action window horizontally below toolbar
let posX = Math.round(toolbarBounds.x + (toolbarBounds.width - actionWindowWidth) / 2)
let posY = Math.round(toolbarBounds.y)
// FIXME sometimes the dock will be shown when the action window is shown
// FIXME if actionWindow show on the fullscreen app, switch to other space will cause the mainWindow to be shown
// FIXME When setVisibleOnAllWorkspaces is true, docker icon disappeared when the first action window is shown on the fullscreen app
// use app.dock.show() to show the dock again will cause the action window to be closed when auto hide on blur is enabled
// Ensure action window stays within screen boundaries with a small gap
if (posX + actionWindowWidth > workArea.x + workArea.width) {
posX = workArea.x + workArea.width - actionWindowWidth - GAP
} else if (posX < workArea.x) {
posX = workArea.x + GAP
}
if (posY + actionWindowHeight > workArea.y + workArea.height) {
// If window would go below screen, try to position it above toolbar
posY = workArea.y + workArea.height - actionWindowHeight - GAP
} else if (posY < workArea.y) {
posY = workArea.y + GAP
}
// setFocusable(false) to prevent the action window hide when blur (if auto hide on blur is enabled)
actionWindow.setFocusable(false)
actionWindow.setAlwaysOnTop(true, 'floating')
actionWindow.setPosition(posX, posY, false)
//KEY to make window not resize
actionWindow.setBounds({
width: actionWindowWidth,
height: actionWindowHeight,
x: posX,
y: posY
// `setVisibleOnAllWorkspaces(true)` will cause the dock icon disappeared
// just store the dock icon status, and show it again
const isDockShown = app.dock?.isVisible()
// DO NOT set `skipTransformProcessType: true`,
// it will cause the action window to be shown on other space
actionWindow.setVisibleOnAllWorkspaces(true, {
visibleOnFullScreen: true
})
actionWindow.show()
actionWindow.showInactive()
// show the dock again if last time it was shown
// do not put it after `actionWindow.focus()`, will cause the action window to be closed when auto hide on blur is enabled
if (!app.dock?.isVisible() && isDockShown) {
app.dock?.show()
}
// unset everything
setTimeout(() => {
actionWindow.setVisibleOnAllWorkspaces(false, {
visibleOnFullScreen: true,
skipTransformProcessType: true
})
actionWindow.setAlwaysOnTop(false)
actionWindow.setFocusable(true)
// regain the focus when all the works done
actionWindow.focus()
}, 50)
}
public closeActionWindow(actionWindow: BrowserWindow): void {
@@ -1408,8 +1473,9 @@ export class SelectionService {
configManager.setSelectionAssistantFilterList(filterList)
})
ipcMain.handle(IpcChannel.Selection_ProcessAction, (_, actionItem: ActionItem) => {
selectionService?.processAction(actionItem)
// [macOS] only macOS has the available isFullscreen mode
ipcMain.handle(IpcChannel.Selection_ProcessAction, (_, actionItem: ActionItem, isFullScreen: boolean = false) => {
selectionService?.processAction(actionItem, isFullScreen)
})
ipcMain.handle(IpcChannel.Selection_ActionWindowClose, (event) => {
+28 -5
View File
@@ -55,7 +55,8 @@ function formatShortcutKey(shortcut: string[]): string {
return shortcut.join('+')
}
// convert the shortcut recorded by keyboard event key value to electron global shortcut format
// convert the shortcut recorded by JS keyboard event key value to electron global shortcut format
// see: https://www.electronjs.org/zh/docs/latest/api/accelerator
const convertShortcutFormat = (shortcut: string | string[]): string => {
const accelerator = (() => {
if (Array.isArray(shortcut)) {
@@ -68,12 +69,34 @@ const convertShortcutFormat = (shortcut: string | string[]): string => {
return accelerator
.map((key) => {
switch (key) {
// OLD WAY FOR MODIFIER KEYS, KEEP THEM HERE FOR REFERENCE
// case 'Command':
// return 'CommandOrControl'
// case 'Control':
// return 'Control'
// case 'Ctrl':
// return 'Control'
// NEW WAY FOR MODIFIER KEYS
// you can see all the modifier keys in the same
case 'CommandOrControl':
return 'CommandOrControl'
case 'Ctrl':
return 'Ctrl'
case 'Alt':
return 'Alt' // Use `Alt` instead of `Option`. The `Option` key only exists on macOS, whereas the `Alt` key is available on all platforms.
case 'Meta':
return 'Meta' // `Meta` key is mapped to the Windows key on Windows and Linux, `Cmd` on macOS.
case 'Shift':
return 'Shift'
// For backward compatibility with old data
case 'Command':
case 'Cmd':
return 'CommandOrControl'
case 'Control':
return 'Control'
case 'Ctrl':
return 'Control'
return 'Ctrl'
case 'ArrowUp':
return 'Up'
case 'ArrowDown':
@@ -83,7 +106,7 @@ const convertShortcutFormat = (shortcut: string | string[]): string => {
case 'ArrowRight':
return 'Right'
case 'AltGraph':
return 'Alt'
return 'AltGr'
case 'Slash':
return '/'
case 'Semicolon':
+3 -1
View File
@@ -23,7 +23,9 @@ export default class WebDav {
password: params.webdavPass,
maxBodyLength: Infinity,
maxContentLength: Infinity,
httpsAgent: new https.Agent({ rejectUnauthorized: false })
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
})
this.putFileContents = this.putFileContents.bind(this)
+71 -13
View File
@@ -5,7 +5,7 @@ import { is } from '@electron-toolkit/utils'
import { isDev, isLinux, isMac, isWin } from '@main/constant'
import { getFilesDir } from '@main/utils/file'
import { IpcChannel } from '@shared/IpcChannel'
import { app, BrowserWindow, nativeTheme, shell } from 'electron'
import { app, BrowserWindow, nativeTheme, screen, shell } from 'electron'
import Logger from 'electron-log'
import windowStateKeeper from 'electron-window-state'
import { join } from 'path'
@@ -16,6 +16,9 @@ import { configManager } from './ConfigManager'
import { contextMenu } from './ContextMenu'
import { initSessionUserAgent } from './WebviewService'
const DEFAULT_MINIWINDOW_WIDTH = 550
const DEFAULT_MINIWINDOW_HEIGHT = 400
export class WindowService {
private static instance: WindowService | null = null
private mainWindow: BrowserWindow | null = null
@@ -26,6 +29,11 @@ export class WindowService {
private wasMainWindowFocused: boolean = false
private lastRendererProcessCrashTime: number = 0
private miniWindowSize: { width: number; height: number } = {
width: DEFAULT_MINIWINDOW_WIDTH,
height: DEFAULT_MINIWINDOW_HEIGHT
}
public static getInstance(): WindowService {
if (!WindowService.instance) {
WindowService.instance = new WindowService()
@@ -426,8 +434,8 @@ export class WindowService {
public createMiniWindow(isPreload: boolean = false): BrowserWindow {
this.miniWindow = new BrowserWindow({
width: 550,
height: 400,
width: this.miniWindowSize.width,
height: this.miniWindowSize.height,
minWidth: 350,
minHeight: 380,
maxWidth: 1024,
@@ -437,13 +445,12 @@ export class WindowService {
transparent: isMac,
vibrancy: 'under-window',
visualEffectState: 'followWindow',
center: true,
frame: false,
alwaysOnTop: true,
resizable: true,
useContentSize: true,
...(isMac ? { type: 'panel' } : {}),
skipTaskbar: true,
resizable: true,
minimizable: false,
maximizable: false,
fullscreenable: false,
@@ -485,6 +492,13 @@ export class WindowService {
this.miniWindow?.webContents.send(IpcChannel.HideMiniWindow)
})
this.miniWindow.on('resized', () => {
this.miniWindowSize = this.miniWindow?.getBounds() || {
width: DEFAULT_MINIWINDOW_WIDTH,
height: DEFAULT_MINIWINDOW_HEIGHT
}
})
this.miniWindow.on('show', () => {
this.miniWindow?.webContents.send(IpcChannel.ShowMiniWindow)
})
@@ -508,10 +522,48 @@ export class WindowService {
if (this.miniWindow && !this.miniWindow.isDestroyed()) {
this.wasMainWindowFocused = this.mainWindow?.isFocused() || false
if (this.miniWindow.isMinimized()) {
this.miniWindow.restore()
// [Windows] hacky fix
// the window is minimized only when in Windows platform
// because it's a workround for Windows, see `hideMiniWindow()`
if (this.miniWindow?.isMinimized()) {
// don't let the window being seen before we finish adusting the position across screens
this.miniWindow?.setOpacity(0)
// DO NOT use `restore()` here, Electron has the bug with screens of different scale factor
// We have to use `show()` here, then set the position and bounds
this.miniWindow?.show()
}
this.miniWindow.show()
const miniWindowBounds = this.miniWindow.getBounds()
// Check if miniWindow is on the same screen as mouse cursor
const cursorDisplay = screen.getDisplayNearestPoint(screen.getCursorScreenPoint())
const miniWindowDisplay = screen.getDisplayNearestPoint(miniWindowBounds)
// Show the miniWindow on the cursor's screen center
// If miniWindow is not on the same screen as cursor, move it to cursor's screen center
if (cursorDisplay.id !== miniWindowDisplay.id) {
const workArea = cursorDisplay.bounds
// use remembered size to avoid the bug of Electron with screens of different scale factor
const miniWindowWidth = this.miniWindowSize.width
const miniWindowHeight = this.miniWindowSize.height
// move to the center of the cursor's screen
const miniWindowX = Math.round(workArea.x + (workArea.width - miniWindowWidth) / 2)
const miniWindowY = Math.round(workArea.y + (workArea.height - miniWindowHeight) / 2)
this.miniWindow.setPosition(miniWindowX, miniWindowY, false)
this.miniWindow.setBounds({
x: miniWindowX,
y: miniWindowY,
width: miniWindowWidth,
height: miniWindowHeight
})
}
this.miniWindow?.setOpacity(1)
this.miniWindow?.show()
return
}
@@ -519,20 +571,26 @@ export class WindowService {
}
public hideMiniWindow() {
//hacky-fix:[mac/win] previous window(not self-app) should be focused again after miniWindow hide
if (!this.miniWindow || this.miniWindow.isDestroyed()) {
return
}
//[macOs/Windows] hacky fix
// previous window(not self-app) should be focused again after miniWindow hide
// this workaround is to make previous window focused again after miniWindow hide
if (isWin) {
this.miniWindow?.minimize()
this.miniWindow?.hide()
this.miniWindow.setOpacity(0) // don't show the minimizing animation
this.miniWindow.minimize()
return
} else if (isMac) {
this.miniWindow?.hide()
this.miniWindow.hide()
if (!this.wasMainWindowFocused) {
app.hide()
}
return
}
this.miniWindow?.hide()
this.miniWindow.hide()
}
public closeMiniWindow() {
+829
View File
@@ -0,0 +1,829 @@
import { Client, createClient } from '@libsql/client'
import Embeddings from '@main/knowledge/embeddings/Embeddings'
import type {
AddMemoryOptions,
AssistantMessage,
MemoryConfig,
MemoryHistoryItem,
MemoryItem,
MemoryListOptions,
MemorySearchOptions
} from '@types'
import crypto from 'crypto'
import { app } from 'electron'
import Logger from 'electron-log'
import path from 'path'
import { MemoryQueries } from './queries'
export interface EmbeddingOptions {
model: string
provider: string
apiKey: string
apiVersion?: string
baseURL: string
dimensions?: number
batchSize?: number
}
export interface VectorSearchOptions {
limit?: number
threshold?: number
userId?: string
agentId?: string
filters?: Record<string, any>
}
export interface SearchResult {
memories: MemoryItem[]
count: number
error?: string
}
export class MemoryService {
private static instance: MemoryService | null = null
private db: Client | null = null
private isInitialized = false
private embeddings: Embeddings | null = null
private config: MemoryConfig | null = null
private static readonly UNIFIED_DIMENSION = 1536
private static readonly SIMILARITY_THRESHOLD = 0.85
private constructor() {
// Private constructor to enforce singleton pattern
}
public static getInstance(): MemoryService {
if (!MemoryService.instance) {
MemoryService.instance = new MemoryService()
}
return MemoryService.instance
}
public static reload(): MemoryService {
if (MemoryService.instance) {
MemoryService.instance.close()
}
MemoryService.instance = new MemoryService()
return MemoryService.instance
}
/**
* Initialize the database connection and create tables
*/
private async init(): Promise<void> {
if (this.isInitialized && this.db) {
return
}
try {
const userDataPath = app.getPath('userData')
const dbPath = path.join(userDataPath, 'memories.db')
this.db = createClient({
url: `file:${dbPath}`,
intMode: 'number'
})
// Create tables
await this.createTables()
this.isInitialized = true
Logger.info('Memory database initialized successfully')
} catch (error) {
Logger.error('Failed to initialize memory database:', error)
throw new Error(
`Memory database initialization failed: ${error instanceof Error ? error.message : 'Unknown error'}`
)
}
}
private async createTables(): Promise<void> {
if (!this.db) throw new Error('Database not initialized')
// Create memories table with native vector support
await this.db.execute(MemoryQueries.createTables.memories)
// Create memory history table
await this.db.execute(MemoryQueries.createTables.memoryHistory)
// Create indexes
await this.db.execute(MemoryQueries.createIndexes.userId)
await this.db.execute(MemoryQueries.createIndexes.agentId)
await this.db.execute(MemoryQueries.createIndexes.createdAt)
await this.db.execute(MemoryQueries.createIndexes.hash)
await this.db.execute(MemoryQueries.createIndexes.memoryHistory)
// Create vector index for similarity search
try {
await this.db.execute(MemoryQueries.createIndexes.vector)
} catch (error) {
// Vector index might not be supported in all versions
Logger.warn('Failed to create vector index, falling back to non-indexed search:', error)
}
}
/**
* Add new memories from messages
*/
public async add(messages: string | AssistantMessage[], options: AddMemoryOptions): Promise<SearchResult> {
await this.init()
if (!this.db) throw new Error('Database not initialized')
const { userId, agentId, runId, metadata } = options
try {
// Convert messages to memory strings
const memoryStrings = Array.isArray(messages)
? messages.map((m) => (typeof m === 'string' ? m : m.content))
: [messages]
const addedMemories: MemoryItem[] = []
for (const memory of memoryStrings) {
const trimmedMemory = memory.trim()
if (!trimmedMemory) continue
// Generate hash for deduplication
const hash = crypto.createHash('sha256').update(trimmedMemory).digest('hex')
// Check if memory already exists
const existing = await this.db.execute({
sql: MemoryQueries.memory.checkExistsIncludeDeleted,
args: [hash]
})
if (existing.rows.length > 0) {
const existingRecord = existing.rows[0] as any
const isDeleted = existingRecord.is_deleted === 1
if (!isDeleted) {
// Active record exists, skip insertion
Logger.info(`Memory already exists with hash: ${hash}`)
continue
} else {
// Deleted record exists, restore it instead of inserting new one
Logger.info(`Restoring deleted memory with hash: ${hash}`)
// Generate embedding if model is configured
let embedding: number[] | null = null
const embedderApiClient = this.config?.embedderApiClient
if (embedderApiClient) {
try {
embedding = await this.generateEmbedding(trimmedMemory)
Logger.info(
`Generated embedding for restored memory with dimension: ${embedding.length} (target: ${this.config?.embedderDimensions || MemoryService.UNIFIED_DIMENSION})`
)
} catch (error) {
Logger.error('Failed to generate embedding for restored memory:', error)
}
}
const now = new Date().toISOString()
// Restore the deleted record
await this.db.execute({
sql: MemoryQueries.memory.restoreDeleted,
args: [
trimmedMemory,
embedding ? this.embeddingToVector(embedding) : null,
metadata ? JSON.stringify(metadata) : null,
now,
existingRecord.id
]
})
// Add to history
await this.addHistory(existingRecord.id, null, trimmedMemory, 'ADD')
addedMemories.push({
id: existingRecord.id,
memory: trimmedMemory,
hash,
createdAt: now,
updatedAt: now,
metadata
})
continue
}
}
// Generate embedding if model is configured
let embedding: number[] | null = null
if (this.config?.embedderApiClient) {
try {
embedding = await this.generateEmbedding(trimmedMemory)
Logger.info(
`Generated embedding with dimension: ${embedding.length} (target: ${this.config?.embedderDimensions || MemoryService.UNIFIED_DIMENSION})`
)
// Check for similar memories using vector similarity
const similarMemories = await this.hybridSearch(trimmedMemory, embedding, {
limit: 5,
threshold: 0.1, // Lower threshold to get more candidates
userId,
agentId
})
// Check if any similar memory exceeds the similarity threshold
if (similarMemories.memories.length > 0) {
const highestSimilarity = Math.max(...similarMemories.memories.map((m) => m.score || 0))
if (highestSimilarity >= MemoryService.SIMILARITY_THRESHOLD) {
Logger.info(
`Skipping memory addition due to high similarity: ${highestSimilarity.toFixed(3)} >= ${MemoryService.SIMILARITY_THRESHOLD}`
)
Logger.info(`Similar memory found: "${similarMemories.memories[0].memory}"`)
continue
}
}
} catch (error) {
Logger.error('Failed to generate embedding:', error)
}
}
// Insert new memory
const id = crypto.randomUUID()
const now = new Date().toISOString()
await this.db.execute({
sql: MemoryQueries.memory.insert,
args: [
id,
trimmedMemory,
hash,
embedding ? this.embeddingToVector(embedding) : null,
metadata ? JSON.stringify(metadata) : null,
userId || null,
agentId || null,
runId || null,
now,
now
]
})
// Add to history
await this.addHistory(id, null, trimmedMemory, 'ADD')
addedMemories.push({
id,
memory: trimmedMemory,
hash,
createdAt: now,
updatedAt: now,
metadata
})
}
return {
memories: addedMemories,
count: addedMemories.length
}
} catch (error) {
Logger.error('Failed to add memories:', error)
return {
memories: [],
count: 0,
error: error instanceof Error ? error.message : 'Unknown error'
}
}
}
/**
* Search memories using text or vector similarity
*/
public async search(query: string, options: MemorySearchOptions = {}): Promise<SearchResult> {
await this.init()
if (!this.db) throw new Error('Database not initialized')
const { limit = 10, userId, agentId, filters = {} } = options
try {
// If we have an embedder model configured, use vector search
if (this.config?.embedderApiClient) {
try {
const queryEmbedding = await this.generateEmbedding(query)
return await this.hybridSearch(query, queryEmbedding, { limit, userId, agentId, filters })
} catch (error) {
Logger.error('Vector search failed, falling back to text search:', error)
}
}
// Fallback to text search
const conditions: string[] = ['m.is_deleted = 0']
const params: any[] = []
// Add search conditions
conditions.push('(m.memory LIKE ? OR m.memory LIKE ?)')
params.push(`%${query}%`, `%${query.split(' ').join('%')}%`)
if (userId) {
conditions.push('m.user_id = ?')
params.push(userId)
}
if (agentId) {
conditions.push('m.agent_id = ?')
params.push(agentId)
}
// Add custom filters
for (const [key, value] of Object.entries(filters)) {
if (value !== undefined && value !== null) {
conditions.push(`json_extract(m.metadata, '$.${key}') = ?`)
params.push(value)
}
}
const whereClause = conditions.join(' AND ')
params.push(limit)
const result = await this.db.execute({
sql: `${MemoryQueries.memory.list} ${whereClause}
ORDER BY m.created_at DESC
LIMIT ?
`,
args: params
})
const memories: MemoryItem[] = result.rows.map((row: any) => ({
id: row.id as string,
memory: row.memory as string,
hash: (row.hash as string) || undefined,
metadata: row.metadata ? JSON.parse(row.metadata as string) : undefined,
createdAt: row.created_at as string,
updatedAt: row.updated_at as string
}))
return {
memories,
count: memories.length
}
} catch (error) {
Logger.error('Search failed:', error)
return {
memories: [],
count: 0,
error: error instanceof Error ? error.message : 'Unknown error'
}
}
}
/**
* List all memories with optional filters
*/
public async list(options: MemoryListOptions = {}): Promise<SearchResult> {
await this.init()
if (!this.db) throw new Error('Database not initialized')
const { userId, agentId, limit = 100, offset = 0 } = options
try {
const conditions: string[] = ['m.is_deleted = 0']
const params: any[] = []
if (userId) {
conditions.push('m.user_id = ?')
params.push(userId)
}
if (agentId) {
conditions.push('m.agent_id = ?')
params.push(agentId)
}
const whereClause = conditions.join(' AND ')
// Get total count
const countResult = await this.db.execute({
sql: `${MemoryQueries.memory.count} ${whereClause}`,
args: params
})
const totalCount = (countResult.rows[0] as any).total as number
// Get paginated results
params.push(limit, offset)
const result = await this.db.execute({
sql: `${MemoryQueries.memory.list} ${whereClause}
ORDER BY m.created_at DESC
LIMIT ? OFFSET ?
`,
args: params
})
const memories: MemoryItem[] = result.rows.map((row: any) => ({
id: row.id as string,
memory: row.memory as string,
hash: (row.hash as string) || undefined,
metadata: row.metadata ? JSON.parse(row.metadata as string) : undefined,
createdAt: row.created_at as string,
updatedAt: row.updated_at as string
}))
return {
memories,
count: totalCount
}
} catch (error) {
Logger.error('List failed:', error)
return {
memories: [],
count: 0,
error: error instanceof Error ? error.message : 'Unknown error'
}
}
}
/**
* Delete a memory (soft delete)
*/
public async delete(id: string): Promise<void> {
await this.init()
if (!this.db) throw new Error('Database not initialized')
try {
// Get current memory value for history
const current = await this.db.execute({
sql: MemoryQueries.memory.getForDelete,
args: [id]
})
if (current.rows.length === 0) {
throw new Error('Memory not found')
}
const currentMemory = (current.rows[0] as any).memory as string
// Soft delete
await this.db.execute({
sql: MemoryQueries.memory.softDelete,
args: [new Date().toISOString(), id]
})
// Add to history
await this.addHistory(id, currentMemory, null, 'DELETE')
Logger.info(`Memory deleted: ${id}`)
} catch (error) {
Logger.error('Delete failed:', error)
throw new Error(`Failed to delete memory: ${error instanceof Error ? error.message : 'Unknown error'}`)
}
}
/**
* Update a memory
*/
public async update(id: string, memory: string, metadata?: Record<string, any>): Promise<void> {
await this.init()
if (!this.db) throw new Error('Database not initialized')
try {
// Get current memory
const current = await this.db.execute({
sql: MemoryQueries.memory.getForUpdate,
args: [id]
})
if (current.rows.length === 0) {
throw new Error('Memory not found')
}
const row = current.rows[0] as any
const previousMemory = row.memory as string
const previousMetadata = row.metadata ? JSON.parse(row.metadata as string) : {}
// Generate new hash
const hash = crypto.createHash('sha256').update(memory.trim()).digest('hex')
// Generate new embedding if model is configured
let embedding: number[] | null = null
if (this.config?.embedderApiClient) {
try {
embedding = await this.generateEmbedding(memory)
Logger.info(
`Updated embedding with dimension: ${embedding.length} (target: ${this.config?.embedderDimensions || MemoryService.UNIFIED_DIMENSION})`
)
} catch (error) {
Logger.error('Failed to generate embedding for update:', error)
}
}
// Merge metadata
const mergedMetadata = { ...previousMetadata, ...metadata }
// Update memory
await this.db.execute({
sql: MemoryQueries.memory.update,
args: [
memory.trim(),
hash,
embedding ? this.embeddingToVector(embedding) : null,
JSON.stringify(mergedMetadata),
new Date().toISOString(),
id
]
})
// Add to history
await this.addHistory(id, previousMemory, memory, 'UPDATE')
Logger.info(`Memory updated: ${id}`)
} catch (error) {
Logger.error('Update failed:', error)
throw new Error(`Failed to update memory: ${error instanceof Error ? error.message : 'Unknown error'}`)
}
}
/**
* Get memory history
*/
public async get(memoryId: string): Promise<MemoryHistoryItem[]> {
await this.init()
if (!this.db) throw new Error('Database not initialized')
try {
const result = await this.db.execute({
sql: MemoryQueries.history.getByMemoryId,
args: [memoryId]
})
return result.rows.map((row: any) => ({
id: row.id as number,
memoryId: row.memory_id as string,
previousValue: row.previous_value as string | undefined,
newValue: row.new_value as string,
action: row.action as 'ADD' | 'UPDATE' | 'DELETE',
createdAt: row.created_at as string,
updatedAt: row.updated_at as string,
isDeleted: row.is_deleted === 1
}))
} catch (error) {
Logger.error('Get history failed:', error)
throw new Error(`Failed to get memory history: ${error instanceof Error ? error.message : 'Unknown error'}`)
}
}
/**
* Delete all memories for a user without deleting the user (hard delete)
*/
public async deleteAllMemoriesForUser(userId: string): Promise<void> {
await this.init()
if (!this.db) throw new Error('Database not initialized')
if (!userId) {
throw new Error('User ID is required')
}
try {
// Get count of memories to be deleted
const countResult = await this.db.execute({
sql: MemoryQueries.users.countMemoriesForUser,
args: [userId]
})
const totalCount = (countResult.rows[0] as any).total as number
// Delete history entries for this user's memories
await this.db.execute({
sql: MemoryQueries.users.deleteHistoryForUser,
args: [userId]
})
// Hard delete all memories for this user
await this.db.execute({
sql: MemoryQueries.users.deleteAllMemoriesForUser,
args: [userId]
})
Logger.info(`Reset all memories for user ${userId} (${totalCount} memories deleted)`)
} catch (error) {
Logger.error('Reset user memories failed:', error)
throw new Error(`Failed to reset user memories: ${error instanceof Error ? error.message : 'Unknown error'}`)
}
}
/**
* Delete a user and all their memories (hard delete)
*/
public async deleteUser(userId: string): Promise<void> {
await this.init()
if (!this.db) throw new Error('Database not initialized')
if (!userId) {
throw new Error('User ID is required')
}
if (userId === 'default-user') {
throw new Error('Cannot delete the default user')
}
try {
// Get count of memories to be deleted
const countResult = await this.db.execute({
sql: `SELECT COUNT(*) as total FROM memories WHERE user_id = ?`,
args: [userId]
})
const totalCount = (countResult.rows[0] as any).total as number
// Delete history entries for this user's memories
await this.db.execute({
sql: `DELETE FROM memory_history WHERE memory_id IN (SELECT id FROM memories WHERE user_id = ?)`,
args: [userId]
})
// Delete all memories for this user (hard delete)
await this.db.execute({
sql: `DELETE FROM memories WHERE user_id = ?`,
args: [userId]
})
Logger.info(`Deleted user ${userId} and ${totalCount} memories`)
} catch (error) {
Logger.error('Delete user failed:', error)
throw new Error(`Failed to delete user: ${error instanceof Error ? error.message : 'Unknown error'}`)
}
}
/**
* Get list of unique user IDs with their memory counts
*/
public async getUsersList(): Promise<{ userId: string; memoryCount: number; lastMemoryDate: string }[]> {
await this.init()
if (!this.db) throw new Error('Database not initialized')
try {
const result = await this.db.execute({
sql: MemoryQueries.users.getUniqueUsers,
args: []
})
return result.rows.map((row: any) => ({
userId: row.user_id as string,
memoryCount: row.memory_count as number,
lastMemoryDate: row.last_memory_date as string
}))
} catch (error) {
Logger.error('Get users list failed:', error)
throw new Error(`Failed to get users list: ${error instanceof Error ? error.message : 'Unknown error'}`)
}
}
/**
* Update configuration
*/
public setConfig(config: MemoryConfig): void {
this.config = config
// Reset embeddings instance when config changes
this.embeddings = null
}
/**
* Close database connection
*/
public async close(): Promise<void> {
if (this.db) {
await this.db.close()
this.db = null
this.isInitialized = false
}
}
// ========== EMBEDDING OPERATIONS (Previously EmbeddingService) ==========
/**
* Normalize embedding dimensions to unified size
*/
private normalizeEmbedding(embedding: number[]): number[] {
if (embedding.length === MemoryService.UNIFIED_DIMENSION) {
return embedding
}
if (embedding.length < MemoryService.UNIFIED_DIMENSION) {
// Pad with zeros
return [...embedding, ...new Array(MemoryService.UNIFIED_DIMENSION - embedding.length).fill(0)]
} else {
// Truncate
return embedding.slice(0, MemoryService.UNIFIED_DIMENSION)
}
}
/**
* Generate embedding for text
*/
private async generateEmbedding(text: string): Promise<number[]> {
if (!this.config?.embedderApiClient) {
throw new Error('Embedder model not configured')
}
try {
// Initialize embeddings instance if needed
if (!this.embeddings) {
if (!this.config.embedderApiClient) {
throw new Error('Embedder provider not configured')
}
this.embeddings = new Embeddings({
embedApiClient: this.config.embedderApiClient,
dimensions: this.config.embedderDimensions
})
await this.embeddings.init()
}
const embedding = await this.embeddings.embedQuery(text)
// Normalize to unified dimension
return this.normalizeEmbedding(embedding)
} catch (error) {
Logger.error('Embedding generation failed:', error)
throw new Error(`Failed to generate embedding: ${error instanceof Error ? error.message : 'Unknown error'}`)
}
}
// ========== VECTOR SEARCH OPERATIONS (Previously VectorSearch) ==========
/**
* Convert embedding array to libsql vector format
*/
private embeddingToVector(embedding: number[]): string {
return `[${embedding.join(',')}]`
}
/**
* Hybrid search combining text and vector similarity (currently vector-only)
*/
private async hybridSearch(
_: string,
queryEmbedding: number[],
options: VectorSearchOptions = {}
): Promise<SearchResult> {
if (!this.db) throw new Error('Database not initialized')
const { limit = 10, threshold = 0.5, userId } = options
try {
const queryVector = this.embeddingToVector(queryEmbedding)
const conditions: string[] = ['m.is_deleted = 0']
const params: any[] = []
// Vector search only - three vector parameters for distance, vector_similarity, and combined_score
params.push(queryVector, queryVector, queryVector)
if (userId) {
conditions.push('m.user_id = ?')
params.push(userId)
}
const whereClause = conditions.join(' AND ')
const hybridQuery = `${MemoryQueries.search.hybridSearch} ${whereClause}
) AS results
WHERE vector_similarity >= ?
ORDER BY vector_similarity DESC
LIMIT ?`
params.push(threshold, limit)
const result = await this.db.execute({
sql: hybridQuery,
args: params
})
const memories: MemoryItem[] = result.rows.map((row: any) => ({
id: row.id as string,
memory: row.memory as string,
hash: (row.hash as string) || undefined,
metadata: row.metadata ? JSON.parse(row.metadata as string) : undefined,
createdAt: row.created_at as string,
updatedAt: row.updated_at as string,
score: row.vector_similarity as number
}))
return {
memories,
count: memories.length
}
} catch (error) {
Logger.error('Hybrid search failed:', error)
throw new Error(`Hybrid search failed: ${error instanceof Error ? error.message : 'Unknown error'}`)
}
}
// ========== HELPER METHODS ==========
/**
* Add entry to memory history
*/
private async addHistory(
memoryId: string,
previousValue: string | null,
newValue: string | null,
action: 'ADD' | 'UPDATE' | 'DELETE'
): Promise<void> {
if (!this.db) throw new Error('Database not initialized')
const now = new Date().toISOString()
await this.db.execute({
sql: MemoryQueries.history.insert,
args: [memoryId, previousValue, newValue, action, now, now]
})
}
}
export default MemoryService
+164
View File
@@ -0,0 +1,164 @@
/**
* SQL queries for MemoryService
* All SQL queries are centralized here for better maintainability
*/
export const MemoryQueries = {
// Table creation queries
createTables: {
memories: `
CREATE TABLE IF NOT EXISTS memories (
id TEXT PRIMARY KEY,
memory TEXT NOT NULL,
hash TEXT UNIQUE,
embedding F32_BLOB(1536), -- Native vector column (1536 dimensions for OpenAI embeddings)
metadata TEXT, -- JSON string
user_id TEXT,
agent_id TEXT,
run_id TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
is_deleted INTEGER DEFAULT 0
)
`,
memoryHistory: `
CREATE TABLE IF NOT EXISTS memory_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
memory_id TEXT NOT NULL,
previous_value TEXT,
new_value TEXT,
action TEXT NOT NULL, -- ADD, UPDATE, DELETE
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
is_deleted INTEGER DEFAULT 0,
FOREIGN KEY (memory_id) REFERENCES memories (id)
)
`
},
// Index creation queries
createIndexes: {
userId: 'CREATE INDEX IF NOT EXISTS idx_memories_user_id ON memories(user_id)',
agentId: 'CREATE INDEX IF NOT EXISTS idx_memories_agent_id ON memories(agent_id)',
createdAt: 'CREATE INDEX IF NOT EXISTS idx_memories_created_at ON memories(created_at)',
hash: 'CREATE INDEX IF NOT EXISTS idx_memories_hash ON memories(hash)',
memoryHistory: 'CREATE INDEX IF NOT EXISTS idx_memory_history_memory_id ON memory_history(memory_id)',
vector: 'CREATE INDEX IF NOT EXISTS idx_memories_vector ON memories (libsql_vector_idx(embedding))'
},
// Memory operations
memory: {
checkExists: 'SELECT id FROM memories WHERE hash = ? AND is_deleted = 0',
checkExistsIncludeDeleted: 'SELECT id, is_deleted FROM memories WHERE hash = ?',
restoreDeleted: `
UPDATE memories
SET is_deleted = 0, memory = ?, embedding = ?, metadata = ?, updated_at = ?
WHERE id = ?
`,
insert: `
INSERT INTO memories (id, memory, hash, embedding, metadata, user_id, agent_id, run_id, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`,
getForDelete: 'SELECT memory FROM memories WHERE id = ? AND is_deleted = 0',
softDelete: 'UPDATE memories SET is_deleted = 1, updated_at = ? WHERE id = ?',
getForUpdate: 'SELECT memory, metadata FROM memories WHERE id = ? AND is_deleted = 0',
update: `
UPDATE memories
SET memory = ?, hash = ?, embedding = ?, metadata = ?, updated_at = ?
WHERE id = ?
`,
count: 'SELECT COUNT(*) as total FROM memories m WHERE',
list: `
SELECT
m.id,
m.memory,
m.hash,
m.metadata,
m.user_id,
m.agent_id,
m.run_id,
m.created_at,
m.updated_at
FROM memories m
WHERE
`
},
// History operations
history: {
insert: `
INSERT INTO memory_history (memory_id, previous_value, new_value, action, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?)
`,
getByMemoryId: `
SELECT * FROM memory_history
WHERE memory_id = ? AND is_deleted = 0
ORDER BY created_at DESC
`
},
// Search operations
search: {
hybridSearch: `
SELECT * FROM (
SELECT
m.id,
m.memory,
m.hash,
m.metadata,
m.user_id,
m.agent_id,
m.run_id,
m.created_at,
m.updated_at,
CASE
WHEN m.embedding IS NULL THEN 2.0
ELSE vector_distance_cos(m.embedding, vector32(?))
END as distance,
CASE
WHEN m.embedding IS NULL THEN 0.0
ELSE (1 - vector_distance_cos(m.embedding, vector32(?)))
END as vector_similarity,
0.0 as text_similarity,
(
CASE
WHEN m.embedding IS NULL THEN 0.0
ELSE (1 - vector_distance_cos(m.embedding, vector32(?)))
END
) as combined_score
FROM memories m
WHERE
`
},
// User operations
users: {
getUniqueUsers: `
SELECT DISTINCT
user_id,
COUNT(*) as memory_count,
MAX(created_at) as last_memory_date
FROM memories
WHERE user_id IS NOT NULL AND is_deleted = 0
GROUP BY user_id
ORDER BY last_memory_date DESC
`,
countMemoriesForUser: 'SELECT COUNT(*) as total FROM memories WHERE user_id = ?',
deleteAllMemoriesForUser: 'DELETE FROM memories WHERE user_id = ?',
deleteHistoryForUser: 'DELETE FROM memory_history WHERE memory_id IN (SELECT id FROM memories WHERE user_id = ?)'
}
} as const
@@ -3,6 +3,17 @@ import Logger from 'electron-log'
import { windowService } from '../WindowService'
function ParseData(data: string) {
try {
const result = JSON.parse(Buffer.from(data, 'base64').toString('utf-8'))
return JSON.stringify(result)
} catch (error) {
Logger.error('ParseData error:', { error })
return null
}
}
export async function handleProvidersProtocolUrl(url: URL) {
switch (url.pathname) {
case '/api-keys': {
@@ -19,7 +30,13 @@ export async function handleProvidersProtocolUrl(url: URL) {
// replace + and / to _ and - because + and / are processed by URLSearchParams
const processedSearch = url.search.replaceAll('+', '_').replaceAll('/', '-')
const params = new URLSearchParams(processedSearch)
const data = params.get('data')
const data = ParseData(params.get('data')?.replaceAll('_', '+').replaceAll('-', '/') || '')
if (!data) {
Logger.error('handleProvidersProtocolUrl data is null or invalid')
return
}
const mainWindow = windowService.getMainWindow()
const version = params.get('v')
if (version == '1') {
@@ -33,7 +50,9 @@ export async function handleProvidersProtocolUrl(url: URL) {
!mainWindow.isDestroyed() &&
(await mainWindow.webContents.executeJavaScript(`typeof window.navigate === 'function'`))
) {
mainWindow.webContents.executeJavaScript(`window.navigate('/settings/provider?addProviderData=${data}')`)
mainWindow.webContents.executeJavaScript(
`window.navigate('/settings/provider?addProviderData=${encodeURIComponent(data)}')`
)
if (isMac) {
windowService.showMainWindow()
+4 -4
View File
@@ -44,7 +44,9 @@ export function handleMcpProtocolUrl(url: URL) {
// }
// }
// cherrystudio://mcp/install?servers={base64Encode(JSON.stringify(jsonConfig))}
const data = params.get('servers')
if (data) {
const stringify = Buffer.from(data, 'base64').toString('utf8')
Logger.info('install MCP servers from urlschema: ', stringify)
@@ -63,10 +65,8 @@ export function handleMcpProtocolUrl(url: URL) {
}
}
const mainWindow = windowService.getMainWindow()
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.executeJavaScript("window.navigate('/settings/mcp')")
}
windowService.getMainWindow()?.show()
break
}
default:
+39 -86
View File
@@ -1,16 +1,19 @@
import * as fs from 'node:fs'
import * as fsPromises from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
import { FileTypes } from '@types'
import iconv from 'iconv-lite'
import { detectAll as detectEncodingAll } from 'jschardet'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { detectEncoding, readTextFileWithAutoEncoding } from '../file'
import { readTextFileWithAutoEncoding } from '../file'
import { getAllFiles, getAppConfigDir, getConfigDir, getFilesDir, getFileType, getTempDir } from '../file'
// Mock dependencies
vi.mock('node:fs')
vi.mock('node:fs/promises')
vi.mock('node:os')
vi.mock('node:path')
vi.mock('uuid', () => ({
@@ -244,102 +247,52 @@ describe('file', () => {
})
})
// 在 describe('file') 块内部添加新的 describe 块
describe('detectEncoding', () => {
const mockFilePath = '/path/to/mock/file.txt'
beforeEach(() => {
vi.mocked(fs.openSync).mockReturnValue(123)
vi.mocked(fs.closeSync).mockImplementation(() => {})
})
it('should correctly detect UTF-8 encoding', () => {
// 准备UTF-8编码的Buffer
const content = '这是UTF-8测试内容'
const buffer = Buffer.from(content, 'utf-8')
// 模拟文件读取
vi.mocked(fs.readSync).mockImplementation((_, buf) => {
const targetBuffer = new Uint8Array(buf.buffer)
const sourceBuffer = new Uint8Array(buffer)
targetBuffer.set(sourceBuffer)
return 1024
})
const encoding = detectEncoding(mockFilePath)
expect(encoding).toBe('UTF-8')
})
it('should correctly detect GB2312 encoding', () => {
// 使用iconv创建GB2312编码内容
const content = '这是一段GB2312编码的测试内容'
const gb2312Buffer = iconv.encode(content, 'GB2312')
// 模拟文件读取
vi.mocked(fs.readSync).mockImplementation((_, buf) => {
const targetBuffer = new Uint8Array(buf.buffer)
const sourceBuffer = new Uint8Array(gb2312Buffer)
targetBuffer.set(sourceBuffer)
return gb2312Buffer.length
})
const encoding = detectEncoding(mockFilePath)
expect(encoding).toMatch(/GB2312|GB18030/i)
})
it('should correctly detect ASCII encoding', () => {
// 准备ASCII编码内容
const content = 'ASCII content'
const buffer = Buffer.from(content, 'ascii')
// 模拟文件读取
vi.mocked(fs.readSync).mockImplementation((_, buf) => {
const targetBuffer = new Uint8Array(buf.buffer)
const sourceBuffer = new Uint8Array(buffer)
targetBuffer.set(sourceBuffer)
return buffer.length
})
const encoding = detectEncoding(mockFilePath)
expect(encoding.toLowerCase()).toBe('ascii')
})
})
describe('readTextFileWithAutoEncoding', () => {
const mockFilePath = '/path/to/mock/file.txt'
beforeEach(() => {
vi.mocked(fs.openSync).mockReturnValue(123)
vi.mocked(fs.closeSync).mockImplementation(() => {})
})
it('should read file with auto encoding', () => {
it('should read file with auto encoding', async () => {
const content = '这是一段GB2312编码的测试内容'
const buffer = iconv.encode(content, 'GB2312')
vi.mocked(fs.readSync).mockImplementation((_, buf) => {
const targetBuffer = new Uint8Array(buf.buffer)
const sourceBuffer = new Uint8Array(buffer)
targetBuffer.set(sourceBuffer)
return buffer.length
})
vi.mocked(fs.readFileSync).mockReturnValue(buffer)
const result = readTextFileWithAutoEncoding(mockFilePath)
// 创建模拟的 FileHandle 对象
const mockFileHandle = {
read: vi.fn().mockResolvedValue({
bytesRead: buffer.byteLength,
buffer: buffer
}),
close: vi.fn().mockResolvedValue(undefined)
}
// 模拟 open 方法
vi.spyOn(fsPromises, 'open').mockResolvedValue(mockFileHandle as any)
vi.spyOn(fsPromises, 'readFile').mockResolvedValue(buffer)
const result = await readTextFileWithAutoEncoding(mockFilePath)
expect(result).toBe(content)
})
it('should try to fix bad detected encoding', () => {
it('should try to fix bad detected encoding', async () => {
const content = '这是一段GB2312编码的测试内容'
const buffer = iconv.encode(content, 'GB2312')
vi.mocked(fs.readSync).mockImplementation((_, buf) => {
const targetBuffer = new Uint8Array(buf.buffer)
const sourceBuffer = new Uint8Array(buffer)
targetBuffer.set(sourceBuffer)
return buffer.length
})
vi.mocked(fs.readFileSync).mockReturnValue(buffer)
vi.mocked(vi.fn(detectEncoding)).mockReturnValue('UTF-8')
const result = readTextFileWithAutoEncoding(mockFilePath)
// 创建模拟的 FileHandle 对象
const mockFileHandle = {
read: vi.fn().mockResolvedValue({
bytesRead: buffer.byteLength,
buffer: buffer
}),
close: vi.fn().mockResolvedValue(undefined)
}
// 模拟 fs.open 方法
vi.spyOn(fsPromises, 'open').mockResolvedValue(mockFileHandle as any)
vi.spyOn(fsPromises, 'readFile').mockResolvedValue(buffer)
vi.mocked(vi.fn(detectEncodingAll)).mockReturnValue([
{ encoding: 'UTF-8', confidence: 0.9 },
{ encoding: 'GB2312', confidence: 0.8 }
])
const result = await readTextFileWithAutoEncoding(mockFilePath)
expect(result).toBe(content)
})
})
+41 -44
View File
@@ -1,14 +1,15 @@
import * as fs from 'node:fs'
import { open, readFile } from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
import { isLinux, isPortable } from '@main/constant'
import { audioExts, documentExts, imageExts, textExts, videoExts } from '@shared/config/constant'
import { audioExts, documentExts, imageExts, MB, textExts, videoExts } from '@shared/config/constant'
import { FileMetadata, FileTypes } from '@types'
import { app } from 'electron'
import Logger from 'electron-log'
import iconv from 'iconv-lite'
import { detect as detectEncoding_, detectAll as detectEncodingAll } from 'jschardet'
import * as jschardet from 'jschardet'
import { v4 as uuidv4 } from 'uuid'
export function initAppDataDir() {
@@ -206,19 +207,8 @@ export function getAppConfigDir(name: string) {
return path.join(getConfigDir(), name)
}
/**
* 使 jschardet
* @param filePath -
* @returns UTF-8, ascii, GB2312
*/
export function detectEncoding(filePath: string): string {
// 读取文件前1KB来检测编码
const buffer = Buffer.alloc(1024)
const fd = fs.openSync(filePath, 'r')
fs.readSync(fd, buffer, 0, 1024, 0)
fs.closeSync(fd)
const { encoding } = detectEncoding_(buffer)
return encoding
export function getMcpDir() {
return path.join(os.homedir(), '.cherrystudio', 'mcp')
}
/**
@@ -226,36 +216,43 @@ export function detectEncoding(filePath: string): string {
* @param filePath -
* @returns
*/
export function readTextFileWithAutoEncoding(filePath: string) {
const encoding = detectEncoding(filePath)
const data = fs.readFileSync(filePath)
const content = iconv.decode(data, encoding)
export async function readTextFileWithAutoEncoding(filePath: string): Promise<string> {
// 读取前1MB以检测编码
const buffer = Buffer.alloc(1 * MB)
const fh = await open(filePath, 'r')
const { buffer: bufferRead } = await fh.read(buffer, 0, 1 * MB, 0)
await fh.close()
if (content.includes('\uFFFD') && encoding !== 'UTF-8') {
Logger.error(`文件 ${filePath} 自动识别编码为 ${encoding},但包含错误字符。尝试其他编码`)
const buffer = Buffer.alloc(1024)
const fd = fs.openSync(filePath, 'r')
fs.readSync(fd, buffer, 0, 1024, 0)
fs.closeSync(fd)
const encodings = detectEncodingAll(buffer)
if (encodings.length > 0) {
for (const item of encodings) {
if (item.encoding === encoding) {
continue
}
Logger.log(`尝试使用 ${item.encoding} 解码文件 ${filePath}`)
const content = iconv.decode(buffer, item.encoding)
if (!content.includes('\uFFFD')) {
Logger.log(`文件 ${filePath} 解码成功,编码为 ${item.encoding}`)
return content
} else {
Logger.error(`文件 ${filePath} 使用 ${item.encoding} 解码失败,尝试下一个编码`)
}
}
}
Logger.error(`文件 ${filePath} 所有可能的编码均解码失败,尝试使用 UTF-8 解码`)
return iconv.decode(buffer, 'UTF-8')
// 获取文件编码格式,最多取前两个可能的编码
const encodings = jschardet
.detectAll(bufferRead)
.map((item) => ({
...item,
encoding: item.encoding === 'ascii' ? 'UTF-8' : item.encoding
}))
.filter((item, index, array) => array.findIndex((prevItem) => prevItem.encoding === item.encoding) === index)
.slice(0, 2)
if (encodings.length === 0) {
Logger.error('Failed to detect encoding. Use utf-8 to decode.')
const data = await readFile(filePath)
return iconv.decode(data, 'UTF-8')
}
return content
const data = await readFile(filePath)
for (const item of encodings) {
const encoding = item.encoding
const content = iconv.decode(data, encoding)
if (content.includes('\uFFFD')) {
Logger.error(
`File ${filePath} was auto-detected as ${encoding} encoding, but contains invalid characters. Trying other encodings`
)
} else {
return content
}
}
Logger.error(`File ${filePath} failed to decode with all possible encodings, trying UTF-8 encoding`)
return iconv.decode(data, 'UTF-8')
}
+33 -4
View File
@@ -3,12 +3,17 @@ import { electronAPI } from '@electron-toolkit/preload'
import { UpgradeChannel } from '@shared/config/constant'
import { IpcChannel } from '@shared/IpcChannel'
import {
AddMemoryOptions,
AssistantMessage,
FileListResponse,
FileMetadata,
FileUploadResponse,
KnowledgeBaseParams,
KnowledgeItem,
MCPServer,
MemoryConfig,
MemoryListOptions,
MemorySearchOptions,
Provider,
S3Config,
Shortcut,
@@ -115,7 +120,8 @@ const api = {
upload: (file: FileMetadata) => ipcRenderer.invoke(IpcChannel.File_Upload, file),
delete: (fileId: string) => ipcRenderer.invoke(IpcChannel.File_Delete, fileId),
deleteDir: (dirPath: string) => ipcRenderer.invoke(IpcChannel.File_DeleteDir, dirPath),
read: (fileId: string) => ipcRenderer.invoke(IpcChannel.File_Read, fileId),
read: (fileId: string, detectEncoding?: boolean) =>
ipcRenderer.invoke(IpcChannel.File_Read, fileId, detectEncoding),
clear: () => ipcRenderer.invoke(IpcChannel.File_Clear),
get: (filePath: string) => ipcRenderer.invoke(IpcChannel.File_Get, filePath),
/**
@@ -146,7 +152,8 @@ const api = {
copy: (fileId: string, destPath: string) => ipcRenderer.invoke(IpcChannel.File_Copy, fileId, destPath),
base64File: (fileId: string) => ipcRenderer.invoke(IpcChannel.File_Base64File, fileId),
pdfInfo: (fileId: string) => ipcRenderer.invoke(IpcChannel.File_GetPdfInfo, fileId),
getPathForFile: (file: File) => webUtils.getPathForFile(file)
getPathForFile: (file: File) => webUtils.getPathForFile(file),
openFileWithRelativePath: (file: FileMetadata) => ipcRenderer.invoke(IpcChannel.File_OpenWithRelativePath, file)
},
fs: {
read: (pathOrUrl: string, encoding?: BufferEncoding) => ipcRenderer.invoke(IpcChannel.Fs_Read, pathOrUrl, encoding)
@@ -182,6 +189,22 @@ const api = {
checkQuota: ({ base, userId }: { base: KnowledgeBaseParams; userId: string }) =>
ipcRenderer.invoke(IpcChannel.KnowledgeBase_Check_Quota, base, userId)
},
memory: {
add: (messages: string | AssistantMessage[], options?: AddMemoryOptions) =>
ipcRenderer.invoke(IpcChannel.Memory_Add, messages, options),
search: (query: string, options: MemorySearchOptions) =>
ipcRenderer.invoke(IpcChannel.Memory_Search, query, options),
list: (options?: MemoryListOptions) => ipcRenderer.invoke(IpcChannel.Memory_List, options),
delete: (id: string) => ipcRenderer.invoke(IpcChannel.Memory_Delete, id),
update: (id: string, memory: string, metadata?: Record<string, any>) =>
ipcRenderer.invoke(IpcChannel.Memory_Update, id, memory, metadata),
get: (id: string) => ipcRenderer.invoke(IpcChannel.Memory_Get, id),
setConfig: (config: MemoryConfig) => ipcRenderer.invoke(IpcChannel.Memory_SetConfig, config),
deleteUser: (userId: string) => ipcRenderer.invoke(IpcChannel.Memory_DeleteUser, userId),
deleteAllMemoriesForUser: (userId: string) =>
ipcRenderer.invoke(IpcChannel.Memory_DeleteAllMemoriesForUser, userId),
getUsersList: () => ipcRenderer.invoke(IpcChannel.Memory_GetUsersList)
},
window: {
setMinimumSize: (width: number, height: number) =>
ipcRenderer.invoke(IpcChannel.Windows_SetMinimumSize, width, height),
@@ -238,8 +261,13 @@ const api = {
ipcRenderer.invoke(IpcChannel.Mcp_GetResource, { server, uri }),
getInstallInfo: () => ipcRenderer.invoke(IpcChannel.Mcp_GetInstallInfo),
checkMcpConnectivity: (server: any) => ipcRenderer.invoke(IpcChannel.Mcp_CheckConnectivity, server),
uploadDxt: async (file: File) => {
const buffer = await file.arrayBuffer()
return ipcRenderer.invoke(IpcChannel.Mcp_UploadDxt, buffer, file.name)
},
abortTool: (callId: string) => ipcRenderer.invoke(IpcChannel.Mcp_AbortTool, callId),
setProgress: (progress: number) => ipcRenderer.invoke(IpcChannel.Mcp_SetProgress, progress)
setProgress: (progress: number) => ipcRenderer.invoke(IpcChannel.Mcp_SetProgress, progress),
getServerVersion: (server: MCPServer) => ipcRenderer.invoke(IpcChannel.Mcp_GetServerVersion, server)
},
python: {
execute: (script: string, context?: Record<string, any>, timeout?: number) =>
@@ -309,7 +337,8 @@ const api = {
ipcRenderer.invoke(IpcChannel.Selection_SetRemeberWinSize, isRemeberWinSize),
setFilterMode: (filterMode: string) => ipcRenderer.invoke(IpcChannel.Selection_SetFilterMode, filterMode),
setFilterList: (filterList: string[]) => ipcRenderer.invoke(IpcChannel.Selection_SetFilterList, filterList),
processAction: (actionItem: ActionItem) => ipcRenderer.invoke(IpcChannel.Selection_ProcessAction, actionItem),
processAction: (actionItem: ActionItem, isFullScreen: boolean = false) =>
ipcRenderer.invoke(IpcChannel.Selection_ProcessAction, actionItem, isFullScreen),
closeActionWindow: () => ipcRenderer.invoke(IpcChannel.Selection_ActionWindowClose),
minimizeActionWindow: () => ipcRenderer.invoke(IpcChannel.Selection_ActionWindowMinimize),
pinActionWindow: (isPinned: boolean) => ipcRenderer.invoke(IpcChannel.Selection_ActionWindowPin, isPinned)
@@ -103,7 +103,12 @@ export class AihubmixAPIClient extends BaseApiClient {
}
// gemini开头 且不以-nothink、-search结尾
if ((id.startsWith('gemini') || id.startsWith('imagen')) && !id.endsWith('-nothink') && !id.endsWith('-search')) {
if (
(id.startsWith('gemini') || id.startsWith('imagen')) &&
!id.endsWith('-nothink') &&
!id.endsWith('-search') &&
!id.includes('embedding')
) {
const client = this.clients.get('gemini')
if (!client || !this.isValidClient(client)) {
throw new Error('Gemini client not properly initialized')
@@ -72,6 +72,7 @@ export class ApiClientFactory {
}
}
export function isOpenAIProvider(provider: Provider) {
return !['anthropic', 'gemini'].includes(provider.type)
}
// 移除这个函数,它已经移动到 utils/index.ts
// export function isOpenAIProvider(provider: Provider) {
// return !['anthropic', 'gemini'].includes(provider.type)
// }
@@ -1,6 +1,7 @@
import {
isFunctionCallingModel,
isNotSupportTemperatureAndTopP,
isOpenAIDeepResearchModel,
isOpenAIModel,
isSupportedFlexServiceTier
} from '@renderer/config/models'
@@ -16,6 +17,7 @@ import {
MCPCallToolResponse,
MCPTool,
MCPToolResponse,
MemoryItem,
Model,
OpenAIServiceTier,
Provider,
@@ -37,7 +39,7 @@ import {
} from '@renderer/types/sdk'
import { isJSON, parseJSON } from '@renderer/utils'
import { addAbortController, removeAbortController } from '@renderer/utils/abortController'
import { findFileBlocks, getContentWithTools, getMainTextContent } from '@renderer/utils/messageUtils/find'
import { findFileBlocks, getMainTextContent } from '@renderer/utils/messageUtils/find'
import { defaultTimeout } from '@shared/config/constant'
import Logger from 'electron-log/renderer'
import { isEmpty } from 'lodash'
@@ -205,11 +207,14 @@ export abstract class BaseApiClient<
if (isSupportedFlexServiceTier(model)) {
return 15 * 1000 * 60
}
if (isOpenAIDeepResearchModel(model)) {
return 60 * 1000 * 60
}
return defaultTimeout
}
public async getMessageContent(message: Message): Promise<string> {
const content = getContentWithTools(message)
const content = getMainTextContent(message)
if (isEmpty(content)) {
return ''
@@ -217,6 +222,7 @@ export abstract class BaseApiClient<
const webSearchReferences = await this.getWebSearchReferencesFromCache(message)
const knowledgeReferences = await this.getKnowledgeBaseReferencesFromCache(message)
const memoryReferences = this.getMemoryReferencesFromCache(message)
// 添加偏移量以避免ID冲突
const reindexedKnowledgeReferences = knowledgeReferences.map((ref) => ({
@@ -224,7 +230,7 @@ export abstract class BaseApiClient<
id: ref.id + webSearchReferences.length // 为知识库引用的ID添加网络搜索引用的数量作为偏移量
}))
const allReferences = [...webSearchReferences, ...reindexedKnowledgeReferences]
const allReferences = [...webSearchReferences, ...reindexedKnowledgeReferences, ...memoryReferences]
Logger.log(`Found ${allReferences.length} references for ID: ${message.id}`, allReferences)
@@ -254,7 +260,7 @@ export abstract class BaseApiClient<
for (const fileBlock of textFileBlocks) {
const file = fileBlock.file
const fileContent = (await window.api.file.read(file.id + file.ext)).trim()
const fileContent = (await window.api.file.read(file.id + file.ext, true)).trim()
const fileNameRow = 'file: ' + file.origin_name + '\n\n'
text = text + fileNameRow + fileContent + divider
}
@@ -266,6 +272,20 @@ export abstract class BaseApiClient<
return ''
}
private getMemoryReferencesFromCache(message: Message) {
const memories = window.keyv.get(`memory-search-${message.id}`) as MemoryItem[] | undefined
if (memories) {
const memoryReferences: KnowledgeReference[] = memories.map((mem, index) => ({
id: index + 1,
content: `${mem.memory} -- Created at: ${mem.createdAt}`,
sourceUrl: '',
type: 'memory'
}))
return memoryReferences
}
return []
}
private async getWebSearchReferencesFromCache(message: Message) {
const content = getMainTextContent(message)
if (isEmpty(content)) {
@@ -0,0 +1,208 @@
import { Provider } from '@renderer/types'
import { isOpenAIProvider } from '@renderer/utils'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { AihubmixAPIClient } from '../AihubmixAPIClient'
import { AnthropicAPIClient } from '../anthropic/AnthropicAPIClient'
import { ApiClientFactory } from '../ApiClientFactory'
import { GeminiAPIClient } from '../gemini/GeminiAPIClient'
import { VertexAPIClient } from '../gemini/VertexAPIClient'
import { NewAPIClient } from '../NewAPIClient'
import { OpenAIAPIClient } from '../openai/OpenAIApiClient'
import { OpenAIResponseAPIClient } from '../openai/OpenAIResponseAPIClient'
import { PPIOAPIClient } from '../ppio/PPIOAPIClient'
// 为工厂测试创建最小化 provider 的辅助函数
// ApiClientFactory 只使用 'id' 和 'type' 字段来决定创建哪个客户端
// 其他字段会传递给客户端构造函数,但不影响工厂逻辑
const createTestProvider = (id: string, type: string): Provider => ({
id,
type: type as Provider['type'],
name: '',
apiKey: '',
apiHost: '',
models: []
})
// Mock 所有客户端模块
vi.mock('../AihubmixAPIClient', () => ({
AihubmixAPIClient: vi.fn().mockImplementation(() => ({}))
}))
vi.mock('../anthropic/AnthropicAPIClient', () => ({
AnthropicAPIClient: vi.fn().mockImplementation(() => ({}))
}))
vi.mock('../gemini/GeminiAPIClient', () => ({
GeminiAPIClient: vi.fn().mockImplementation(() => ({}))
}))
vi.mock('../gemini/VertexAPIClient', () => ({
VertexAPIClient: vi.fn().mockImplementation(() => ({}))
}))
vi.mock('../NewAPIClient', () => ({
NewAPIClient: vi.fn().mockImplementation(() => ({}))
}))
vi.mock('../openai/OpenAIApiClient', () => ({
OpenAIAPIClient: vi.fn().mockImplementation(() => ({}))
}))
vi.mock('../openai/OpenAIResponseAPIClient', () => ({
OpenAIResponseAPIClient: vi.fn().mockImplementation(() => ({
getClient: vi.fn().mockReturnThis()
}))
}))
vi.mock('../ppio/PPIOAPIClient', () => ({
PPIOAPIClient: vi.fn().mockImplementation(() => ({}))
}))
describe('ApiClientFactory', () => {
beforeEach(() => {
vi.clearAllMocks()
})
describe('create', () => {
// 测试特殊 ID 的客户端创建
it('should create AihubmixAPIClient for aihubmix provider', () => {
const provider = createTestProvider('aihubmix', 'openai')
const client = ApiClientFactory.create(provider)
expect(AihubmixAPIClient).toHaveBeenCalledWith(provider)
expect(client).toBeDefined()
})
it('should create NewAPIClient for new-api provider', () => {
const provider = createTestProvider('new-api', 'openai')
const client = ApiClientFactory.create(provider)
expect(NewAPIClient).toHaveBeenCalledWith(provider)
expect(client).toBeDefined()
})
it('should create PPIOAPIClient for ppio provider', () => {
const provider = createTestProvider('ppio', 'openai')
const client = ApiClientFactory.create(provider)
expect(PPIOAPIClient).toHaveBeenCalledWith(provider)
expect(client).toBeDefined()
})
// 测试标准类型的客户端创建
it('should create OpenAIAPIClient for openai type', () => {
const provider = createTestProvider('custom-openai', 'openai')
const client = ApiClientFactory.create(provider)
expect(OpenAIAPIClient).toHaveBeenCalledWith(provider)
expect(client).toBeDefined()
})
it('should create OpenAIResponseAPIClient for azure-openai type', () => {
const provider = createTestProvider('azure-openai', 'azure-openai')
const client = ApiClientFactory.create(provider)
expect(OpenAIResponseAPIClient).toHaveBeenCalledWith(provider)
expect(client).toBeDefined()
})
it('should create OpenAIResponseAPIClient for openai-response type', () => {
const provider = createTestProvider('response', 'openai-response')
const client = ApiClientFactory.create(provider)
expect(OpenAIResponseAPIClient).toHaveBeenCalledWith(provider)
expect(client).toBeDefined()
})
it('should create GeminiAPIClient for gemini type', () => {
const provider = createTestProvider('gemini', 'gemini')
const client = ApiClientFactory.create(provider)
expect(GeminiAPIClient).toHaveBeenCalledWith(provider)
expect(client).toBeDefined()
})
it('should create VertexAPIClient for vertexai type', () => {
const provider = createTestProvider('vertex', 'vertexai')
const client = ApiClientFactory.create(provider)
expect(VertexAPIClient).toHaveBeenCalledWith(provider)
expect(client).toBeDefined()
})
it('should create AnthropicAPIClient for anthropic type', () => {
const provider = createTestProvider('anthropic', 'anthropic')
const client = ApiClientFactory.create(provider)
expect(AnthropicAPIClient).toHaveBeenCalledWith(provider)
expect(client).toBeDefined()
})
// 测试默认情况
it('should create OpenAIAPIClient as default for unknown type', () => {
const provider = createTestProvider('unknown', 'unknown-type')
const client = ApiClientFactory.create(provider)
expect(OpenAIAPIClient).toHaveBeenCalledWith(provider)
expect(client).toBeDefined()
})
// 测试边界条件
it('should handle provider with minimal configuration', () => {
const provider = createTestProvider('minimal', 'openai')
const client = ApiClientFactory.create(provider)
expect(OpenAIAPIClient).toHaveBeenCalledWith(provider)
expect(client).toBeDefined()
})
// 测试特殊 ID 优先级高于类型
it('should prioritize special ID over type', () => {
const provider = createTestProvider('aihubmix', 'anthropic') // 即使类型是 anthropic
const client = ApiClientFactory.create(provider)
// 应该创建 AihubmixAPIClient 而不是 AnthropicAPIClient
expect(AihubmixAPIClient).toHaveBeenCalledWith(provider)
expect(AnthropicAPIClient).not.toHaveBeenCalled()
expect(client).toBeDefined()
})
})
describe('isOpenAIProvider', () => {
it('should return true for openai type', () => {
const provider = createTestProvider('openai', 'openai')
expect(isOpenAIProvider(provider)).toBe(true)
})
it('should return true for azure-openai type', () => {
const provider = createTestProvider('azure-openai', 'azure-openai')
expect(isOpenAIProvider(provider)).toBe(true)
})
it('should return true for unknown type (fallback to OpenAI)', () => {
const provider = createTestProvider('unknown', 'unknown')
expect(isOpenAIProvider(provider)).toBe(true)
})
it('should return false for vertexai type', () => {
const provider = createTestProvider('vertex', 'vertexai')
expect(isOpenAIProvider(provider)).toBe(false)
})
it('should return false for anthropic type', () => {
const provider = createTestProvider('anthropic', 'anthropic')
expect(isOpenAIProvider(provider)).toBe(false)
})
it('should return false for gemini type', () => {
const provider = createTestProvider('gemini', 'gemini')
expect(isOpenAIProvider(provider)).toBe(false)
})
})
})
@@ -49,10 +49,10 @@ import {
LLMWebSearchCompleteChunk,
LLMWebSearchInProgressChunk,
MCPToolCreatedChunk,
TextCompleteChunk,
TextDeltaChunk,
ThinkingCompleteChunk,
ThinkingDeltaChunk
TextStartChunk,
ThinkingDeltaChunk,
ThinkingStartChunk
} from '@renderer/types/chunk'
import { type Message } from '@renderer/types/newMessage'
import {
@@ -231,7 +231,7 @@ export class AnthropicAPIClient extends BaseApiClient<
}
})
} else {
const fileContent = await (await window.api.file.read(file.id + file.ext)).trim()
const fileContent = await (await window.api.file.read(file.id + file.ext, true)).trim()
parts.push({
type: 'text',
text: file.origin_name + '\n' + fileContent
@@ -519,15 +519,23 @@ export class AnthropicAPIClient extends BaseApiClient<
return () => {
let accumulatedJson = ''
const toolCalls: Record<number, ToolUseBlock> = {}
const ChunkIdTypeMap: Record<number, ChunkType> = {}
return {
async transform(rawChunk: AnthropicSdkRawChunk, controller: TransformStreamDefaultController<GenericChunk>) {
switch (rawChunk.type) {
case 'message': {
let i = 0
let hasTextContent = false
let hasThinkingContent = false
for (const content of rawChunk.content) {
switch (content.type) {
case 'text': {
if (!hasTextContent) {
controller.enqueue({
type: ChunkType.TEXT_START
} as TextStartChunk)
hasTextContent = true
}
controller.enqueue({
type: ChunkType.TEXT_DELTA,
text: content.text
@@ -540,6 +548,12 @@ export class AnthropicAPIClient extends BaseApiClient<
break
}
case 'thinking': {
if (!hasThinkingContent) {
controller.enqueue({
type: ChunkType.THINKING_START
} as ThinkingStartChunk)
hasThinkingContent = true
}
controller.enqueue({
type: ChunkType.THINKING_DELTA,
text: content.thinking
@@ -615,16 +629,16 @@ export class AnthropicAPIClient extends BaseApiClient<
break
}
case 'text': {
if (!ChunkIdTypeMap[rawChunk.index]) {
ChunkIdTypeMap[rawChunk.index] = ChunkType.TEXT_DELTA // 用textdelta代表文本块
}
controller.enqueue({
type: ChunkType.TEXT_START
} as TextStartChunk)
break
}
case 'thinking':
case 'redacted_thinking': {
if (!ChunkIdTypeMap[rawChunk.index]) {
ChunkIdTypeMap[rawChunk.index] = ChunkType.THINKING_DELTA // 用thinkingdelta代表思考块
}
controller.enqueue({
type: ChunkType.THINKING_START
} as ThinkingStartChunk)
break
}
}
@@ -661,15 +675,6 @@ export class AnthropicAPIClient extends BaseApiClient<
break
}
case 'content_block_stop': {
if (ChunkIdTypeMap[rawChunk.index] === ChunkType.TEXT_DELTA) {
controller.enqueue({
type: ChunkType.TEXT_COMPLETE
} as TextCompleteChunk)
} else if (ChunkIdTypeMap[rawChunk.index] === ChunkType.THINKING_DELTA) {
controller.enqueue({
type: ChunkType.THINKING_COMPLETE
} as ThinkingCompleteChunk)
}
const toolCall = toolCalls[rawChunk.index]
if (toolCall) {
try {
@@ -41,7 +41,7 @@ import {
ToolCallResponse,
WebSearchSource
} from '@renderer/types'
import { ChunkType, LLMWebSearchCompleteChunk } from '@renderer/types/chunk'
import { ChunkType, LLMWebSearchCompleteChunk, TextStartChunk, ThinkingStartChunk } from '@renderer/types/chunk'
import { Message } from '@renderer/types/newMessage'
import {
GeminiOptions,
@@ -288,7 +288,7 @@ export class GeminiAPIClient extends BaseApiClient<
continue
}
if ([FileTypes.TEXT, FileTypes.DOCUMENT].includes(file.type)) {
const fileContent = await (await window.api.file.read(file.id + file.ext)).trim()
const fileContent = await (await window.api.file.read(file.id + file.ext, true)).trim()
parts.push({
text: file.origin_name + '\n' + fileContent
})
@@ -443,7 +443,7 @@ export class GeminiAPIClient extends BaseApiClient<
messages: GeminiSdkMessageParam[]
metadata: Record<string, any>
}> => {
const { messages, mcpTools, maxTokens, enableWebSearch, enableGenerateImage } = coreRequest
const { messages, mcpTools, maxTokens, enableWebSearch, enableUrlContext, enableGenerateImage } = coreRequest
// 1. 处理系统消息
let systemInstruction = assistant.prompt
@@ -483,6 +483,12 @@ export class GeminiAPIClient extends BaseApiClient<
})
}
if (enableUrlContext) {
tools.push({
urlContext: {}
})
}
if (isGemmaModel(model) && assistant.prompt) {
const isFirstMessage = history.length === 0
if (isFirstMessage && messageContents) {
@@ -547,20 +553,34 @@ export class GeminiAPIClient extends BaseApiClient<
}
getResponseChunkTransformer(): ResponseChunkTransformer<GeminiSdkRawChunk> {
const toolCalls: FunctionCall[] = []
let isFirstTextChunk = true
let isFirstThinkingChunk = true
return () => ({
async transform(chunk: GeminiSdkRawChunk, controller: TransformStreamDefaultController<GenericChunk>) {
const toolCalls: FunctionCall[] = []
if (chunk.candidates && chunk.candidates.length > 0) {
for (const candidate of chunk.candidates) {
if (candidate.content) {
candidate.content.parts?.forEach((part) => {
const text = part.text || ''
if (part.thought) {
if (isFirstThinkingChunk) {
controller.enqueue({
type: ChunkType.THINKING_START
} as ThinkingStartChunk)
isFirstThinkingChunk = false
}
controller.enqueue({
type: ChunkType.THINKING_DELTA,
text: text
})
} else if (part.text) {
if (isFirstTextChunk) {
controller.enqueue({
type: ChunkType.TEXT_START
} as TextStartChunk)
isFirstTextChunk = false
}
controller.enqueue({
type: ChunkType.TEXT_DELTA,
text: text
@@ -593,6 +613,13 @@ export class GeminiAPIClient extends BaseApiClient<
}
} as LLMWebSearchCompleteChunk)
}
if (toolCalls.length > 0) {
controller.enqueue({
type: ChunkType.MCP_TOOL_CREATED,
tool_calls: [...toolCalls]
})
toolCalls.length = 0
}
controller.enqueue({
type: ChunkType.LLM_RESPONSE_COMPLETE,
response: {
@@ -5,6 +5,7 @@ import {
GEMINI_FLASH_MODEL_REGEX,
getOpenAIWebSearchParams,
isDoubaoThinkingAutoModel,
isQwenReasoningModel,
isReasoningModel,
isSupportedReasoningEffortGrokModel,
isSupportedReasoningEffortModel,
@@ -31,7 +32,7 @@ import {
ToolCallResponse,
WebSearchSource
} from '@renderer/types'
import { ChunkType } from '@renderer/types/chunk'
import { ChunkType, TextStartChunk, ThinkingStartChunk } from '@renderer/types/chunk'
import { Message } from '@renderer/types/newMessage'
import {
OpenAISdkMessageParam,
@@ -114,7 +115,11 @@ export class OpenAIAPIClient extends OpenAIBaseClient<
if (!reasoningEffort) {
if (model.provider === 'openrouter') {
if (isSupportedThinkingTokenGeminiModel(model) && !GEMINI_FLASH_MODEL_REGEX.test(model.id)) {
if (
isSupportedThinkingTokenGeminiModel(model) &&
!GEMINI_FLASH_MODEL_REGEX.test(model.id) &&
model.id.includes('grok-4')
) {
return {}
}
return { reasoning: { enabled: false, exclude: true } }
@@ -166,10 +171,17 @@ export class OpenAIAPIClient extends OpenAIBaseClient<
// Qwen models
if (isSupportedThinkingTokenQwenModel(model)) {
return {
const thinkConfig = {
enable_thinking: true,
thinking_budget: budgetTokens
}
if (this.provider.id === 'dashscope') {
return {
...thinkConfig,
incremental_output: true
}
}
return thinkConfig
}
// Grok models
@@ -307,7 +319,7 @@ export class OpenAIAPIClient extends OpenAIBaseClient<
}
if ([FileTypes.TEXT, FileTypes.DOCUMENT].includes(file.type)) {
const fileContent = await (await window.api.file.read(file.id + file.ext)).trim()
const fileContent = await (await window.api.file.read(file.id + file.ext, true)).trim()
parts.push({
type: 'text',
text: file.origin_name + '\n' + fileContent
@@ -359,7 +371,12 @@ export class OpenAIAPIClient extends OpenAIBaseClient<
if ('toolUseId' in mcpToolResponse && mcpToolResponse.toolUseId) {
// This case is for Anthropic/Claude like tool usage, OpenAI uses tool_call_id
// For OpenAI, we primarily expect toolCallId. This might need adjustment if mixing provider concepts.
return mcpToolCallResponseToOpenAICompatibleMessage(mcpToolResponse, resp, isVisionModel(model))
return mcpToolCallResponseToOpenAICompatibleMessage(
mcpToolResponse,
resp,
isVisionModel(model),
this.provider.isNotSupportArrayContent ?? false
)
} else if ('toolCallId' in mcpToolResponse && mcpToolResponse.toolCallId) {
return {
role: 'tool',
@@ -436,7 +453,14 @@ export class OpenAIAPIClient extends OpenAIBaseClient<
messages: OpenAISdkMessageParam[]
metadata: Record<string, any>
}> => {
const { messages, mcpTools, maxTokens, streamOutput, enableWebSearch } = coreRequest
const { messages, mcpTools, maxTokens, enableWebSearch } = coreRequest
let { streamOutput } = coreRequest
// Qwen3商业版(思考模式)、Qwen3开源版、QwQ、QVQ只支持流式输出。
if (isQwenReasoningModel(model)) {
streamOutput = true
}
// 1. 处理系统消息
let systemMessage = { role: 'system', content: assistant.prompt || '' }
@@ -659,6 +683,8 @@ export class OpenAIAPIClient extends OpenAIBaseClient<
isFinished = true
}
let isFirstThinkingChunk = true
let isFirstTextChunk = true
return (context: ResponseChunkTransformerContext) => ({
async transform(chunk: OpenAISdkRawChunk, controller: TransformStreamDefaultController<GenericChunk>) {
// 持续更新usage信息
@@ -677,15 +703,29 @@ export class OpenAIAPIClient extends OpenAIBaseClient<
// 对于流式响应,使用 delta;对于非流式响应,使用 message。
// 然而某些 OpenAI 兼容平台在非流式请求时会错误地返回一个空对象的 delta 字段。
// 如果 delta 为空对象,应当忽略它并回退到 message,避免造成内容缺失。
// 如果 delta 为空对象或content为空,应当忽略它并回退到 message,避免造成内容缺失。
let contentSource: OpenAISdkRawContentSource | null = null
if ('delta' in choice && choice.delta && Object.keys(choice.delta).length > 0) {
if (
'delta' in choice &&
choice.delta &&
Object.keys(choice.delta).length > 0 &&
(!('content' in choice.delta) ||
(typeof choice.delta.content === 'string' && choice.delta.content !== '') ||
(typeof (choice.delta as any).reasoning_content === 'string' &&
(choice.delta as any).reasoning_content !== '') ||
(typeof (choice.delta as any).reasoning === 'string' && (choice.delta as any).reasoning !== ''))
) {
contentSource = choice.delta
} else if ('message' in choice) {
contentSource = choice.message
}
if (!contentSource) continue
if (!contentSource) {
if ('finish_reason' in choice && choice.finish_reason) {
emitCompletionSignals(controller)
}
continue
}
const webSearchData = collectWebSearchData(chunk, contentSource, context)
if (webSearchData) {
@@ -699,6 +739,12 @@ export class OpenAIAPIClient extends OpenAIBaseClient<
// @ts-ignore - reasoning_content is not in standard OpenAI types but some providers use it
const reasoningText = contentSource.reasoning_content || contentSource.reasoning
if (reasoningText) {
if (isFirstThinkingChunk) {
controller.enqueue({
type: ChunkType.THINKING_START
} as ThinkingStartChunk)
isFirstThinkingChunk = false
}
controller.enqueue({
type: ChunkType.THINKING_DELTA,
text: reasoningText
@@ -707,6 +753,12 @@ export class OpenAIAPIClient extends OpenAIBaseClient<
// 处理文本内容
if (contentSource.content) {
if (isFirstTextChunk) {
controller.enqueue({
type: ChunkType.TEXT_START
} as TextStartChunk)
isFirstTextChunk = false
}
controller.enqueue({
type: ChunkType.TEXT_DELTA,
text: contentSource.content
@@ -89,7 +89,7 @@ export abstract class OpenAIBaseClient<
const data = await sdk.embeddings.create({
model: model.id,
input: model?.provider === 'baidu-cloud' ? ['hi'] : 'hi',
encoding_format: 'float'
encoding_format: this.provider.id === 'voyageai' ? undefined : 'float'
})
return data.data[0].embedding.length
}
@@ -2,6 +2,7 @@ import { GenericChunk } from '@renderer/aiCore/middleware/schemas'
import { CompletionsContext } from '@renderer/aiCore/middleware/types'
import {
isOpenAIChatCompletionOnlyModel,
isOpenAIDeepResearchModel,
isOpenAILLMModel,
isSupportedReasoningEffortOpenAIModel,
isVisionModel
@@ -39,7 +40,7 @@ import { findFileBlocks, findImageBlocks } from '@renderer/utils/messageUtils/fi
import { buildSystemPrompt } from '@renderer/utils/prompt'
import { MB } from '@shared/config/constant'
import { isEmpty } from 'lodash'
import OpenAI from 'openai'
import OpenAI, { AzureOpenAI } from 'openai'
import { ResponseInput } from 'openai/resources/responses/responses'
import { RequestTransformer, ResponseChunkTransformer } from '../types'
@@ -61,11 +62,35 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient<
this.client = new OpenAIAPIClient(provider)
}
private formatApiHost() {
const host = this.provider.apiHost
if (host.endsWith('/openai/v1')) {
return host
} else {
if (host.endsWith('/')) {
return host + 'openai/v1'
} else {
return host + '/openai/v1'
}
}
}
/**
*
*/
public getClient(model: Model) {
if (this.provider.type === 'openai-response' && !isOpenAIChatCompletionOnlyModel(model)) {
return this
}
if (isOpenAILLMModel(model) && !isOpenAIChatCompletionOnlyModel(model)) {
if (this.provider.id === 'azure-openai' || this.provider.type === 'azure-openai') {
this.provider = { ...this.provider, apiHost: this.formatApiHost() }
if (this.provider.apiVersion === 'preview') {
return this
} else {
return this.client
}
}
return this
} else {
return this.client
@@ -77,15 +102,24 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient<
return this.sdkInstance
}
return new OpenAI({
dangerouslyAllowBrowser: true,
apiKey: this.apiKey,
baseURL: this.getBaseURL(),
defaultHeaders: {
...this.defaultHeaders(),
...this.provider.extra_headers
}
})
if (this.provider.id === 'azure-openai' || this.provider.type === 'azure-openai') {
return new AzureOpenAI({
dangerouslyAllowBrowser: true,
apiKey: this.apiKey,
apiVersion: this.provider.apiVersion,
baseURL: this.provider.apiHost
})
} else {
return new OpenAI({
dangerouslyAllowBrowser: true,
apiKey: this.apiKey,
baseURL: this.getBaseURL(),
defaultHeaders: {
...this.defaultHeaders(),
...this.provider.extra_headers
}
})
}
}
override async createCompletions(
@@ -173,7 +207,7 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient<
}
if ([FileTypes.TEXT, FileTypes.DOCUMENT].includes(file.type)) {
const fileContent = (await window.api.file.read(file.id + file.ext)).trim()
const fileContent = (await window.api.file.read(file.id + file.ext, true)).trim()
parts.push({
type: 'input_text',
text: file.origin_name + '\n' + fileContent
@@ -354,16 +388,15 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient<
(m) => (m as OpenAI.Responses.EasyInputMessage).role === 'assistant'
) as OpenAI.Responses.EasyInputMessage
const finalUserMessage = userMessage.pop() as OpenAI.Responses.EasyInputMessage
if (
finalAssistantMessage &&
Array.isArray(finalAssistantMessage.content) &&
finalUserMessage &&
Array.isArray(finalUserMessage.content)
) {
finalAssistantMessage.content = [...finalAssistantMessage.content, ...finalUserMessage.content]
if (finalUserMessage && Array.isArray(finalUserMessage.content)) {
if (finalAssistantMessage && Array.isArray(finalAssistantMessage.content)) {
finalAssistantMessage.content = [...finalAssistantMessage.content, ...finalUserMessage.content]
// 这里是故意将上条助手消息的内容(包含图片和文件)作为用户消息发送
userMessage = [{ ...finalAssistantMessage, role: 'user' } as OpenAI.Responses.EasyInputMessage]
} else {
userMessage.push(finalUserMessage)
}
}
// 这里是故意将上条助手消息的内容(包含图片和文件)作为用户消息发送
userMessage = [{ ...finalAssistantMessage, role: 'user' } as OpenAI.Responses.EasyInputMessage]
}
// 4. 最终请求消息
@@ -374,7 +407,7 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient<
reqMessages = [systemMessage, ...userMessage].filter(Boolean) as OpenAI.Responses.EasyInputMessage[]
}
if (enableWebSearch) {
if (enableWebSearch || isOpenAIDeepResearchModel(model)) {
tools.push({
type: 'web_search_preview'
})
@@ -424,6 +457,8 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient<
const outputItems: OpenAI.Responses.ResponseOutputItem[] = []
let hasBeenCollectedToolCalls = false
let hasReasoningSummary = false
let isFirstThinkingChunk = true
let isFirstTextChunk = true
return () => ({
async transform(chunk: OpenAIResponseSdkRawChunk, controller: TransformStreamDefaultController<GenericChunk>) {
// 处理chunk
@@ -435,6 +470,12 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient<
switch (output.type) {
case 'message':
if (output.content[0].type === 'output_text') {
if (isFirstTextChunk) {
controller.enqueue({
type: ChunkType.TEXT_START
})
isFirstTextChunk = false
}
controller.enqueue({
type: ChunkType.TEXT_DELTA,
text: output.content[0].text
@@ -451,6 +492,12 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient<
}
break
case 'reasoning':
if (isFirstThinkingChunk) {
controller.enqueue({
type: ChunkType.THINKING_START
})
isFirstThinkingChunk = false
}
controller.enqueue({
type: ChunkType.THINKING_DELTA,
text: output.summary.map((s) => s.text).join('\n')
@@ -510,6 +557,12 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient<
hasReasoningSummary = true
break
case 'response.reasoning_summary_text.delta':
if (isFirstThinkingChunk) {
controller.enqueue({
type: ChunkType.THINKING_START
})
isFirstThinkingChunk = false
}
controller.enqueue({
type: ChunkType.THINKING_DELTA,
text: chunk.delta
@@ -535,6 +588,12 @@ export class OpenAIResponseAPIClient extends OpenAIBaseClient<
})
break
case 'response.output_text.delta': {
if (isFirstTextChunk) {
controller.enqueue({
type: ChunkType.TEXT_START
})
isFirstTextChunk = false
}
controller.enqueue({
type: ChunkType.TEXT_DELTA,
text: chunk.delta
+1
View File
@@ -84,6 +84,7 @@ export interface ResponseChunkTransformerContext {
isStreaming: boolean
isEnabledToolCalling: boolean
isEnabledWebSearch: boolean
isEnabledUrlContext: boolean
isEnabledReasoning: boolean
mcpTools: MCPTool[]
provider: Provider
+7 -2
View File
@@ -75,11 +75,12 @@ export default class AiProvider {
} else {
// Existing logic for other models
if (!params.enableReasoning) {
builder.remove(ThinkingTagExtractionMiddlewareName)
// 这里注释掉不会影响正常的关闭思考,可忽略不计的性能下降
// builder.remove(ThinkingTagExtractionMiddlewareName)
builder.remove(ThinkChunkMiddlewareName)
}
// 注意:用client判断会导致typescript类型收窄
if (!(this.apiClient instanceof OpenAIAPIClient)) {
if (!(this.apiClient instanceof OpenAIAPIClient) && !(this.apiClient instanceof OpenAIResponseAPIClient)) {
builder.remove(ThinkingTagExtractionMiddlewareName)
}
if (!(this.apiClient instanceof AnthropicAPIClient) && !(this.apiClient instanceof OpenAIResponseAPIClient)) {
@@ -98,6 +99,10 @@ export default class AiProvider {
if (params.callType !== 'chat') {
builder.remove(AbortHandlerMiddlewareName)
}
if (params.callType === 'test') {
builder.remove(ErrorHandlerMiddlewareName)
builder.remove(FinalChunkConsumerMiddlewareName)
}
}
const middlewares = builder.build()
@@ -252,7 +252,9 @@ async function executeToolCalls(
('name' in toolCall &&
(toolCall.name?.includes(confirmed.tool.name) || toolCall.name?.includes(confirmed.tool.id))) ||
confirmed.tool.name === toolCall.id ||
confirmed.tool.id === toolCall.id
confirmed.tool.id === toolCall.id ||
('toolCallId' in confirmed && confirmed.toolCallId === toolCall.id) ||
('function' in toolCall && toolCall.function.name.toLowerCase().includes(confirmed.tool.name.toLowerCase()))
)
})
})
@@ -55,6 +55,7 @@ export const ResponseTransformMiddleware: CompletionsMiddleware =
isStreaming: params.streamOutput || false,
isEnabledToolCalling: (params.mcpTools && params.mcpTools.length > 0) || false,
isEnabledWebSearch: params.enableWebSearch || false,
isEnabledUrlContext: params.enableUrlContext || false,
isEnabledReasoning: params.enableReasoning || false,
mcpTools: params.mcpTools || [],
provider: ctx.apiClientInstance?.provider
@@ -1,5 +1,5 @@
import Logger from '@renderer/config/logger'
import { ChunkType, TextCompleteChunk, TextDeltaChunk } from '@renderer/types/chunk'
import { ChunkType } from '@renderer/types/chunk'
import { CompletionsParams, CompletionsResult, GenericChunk } from '../schemas'
import { CompletionsContext, CompletionsMiddleware } from '../types'
@@ -38,48 +38,36 @@ export const TextChunkMiddleware: CompletionsMiddleware =
// 用于跨chunk的状态管理
let accumulatedTextContent = ''
let hasTextCompleteEventEnqueue = false
const enhancedTextStream = resultFromUpstream.pipeThrough(
new TransformStream<GenericChunk, GenericChunk>({
transform(chunk: GenericChunk, controller) {
if (chunk.type === ChunkType.TEXT_DELTA) {
const textChunk = chunk as TextDeltaChunk
accumulatedTextContent += textChunk.text
accumulatedTextContent += chunk.text
// 处理 onResponse 回调 - 发送增量文本更新
if (params.onResponse) {
params.onResponse(accumulatedTextContent, false)
}
// 创建新的chunk,包含处理后的文本
controller.enqueue(chunk)
} else if (chunk.type === ChunkType.TEXT_COMPLETE) {
const textChunk = chunk as TextCompleteChunk
controller.enqueue({
...textChunk,
text: accumulatedTextContent
...chunk,
text: accumulatedTextContent // 增量更新
})
if (params.onResponse) {
params.onResponse(accumulatedTextContent, true)
} else if (accumulatedTextContent && chunk.type !== ChunkType.TEXT_START) {
ctx._internal.customState!.accumulatedText = accumulatedTextContent
if (ctx._internal.toolProcessingState && !ctx._internal.toolProcessingState?.output) {
ctx._internal.toolProcessingState.output = accumulatedTextContent
}
hasTextCompleteEventEnqueue = true
accumulatedTextContent = ''
} else if (accumulatedTextContent && !hasTextCompleteEventEnqueue) {
if (chunk.type === ChunkType.LLM_RESPONSE_COMPLETE) {
const finalText = accumulatedTextContent
ctx._internal.customState!.accumulatedText = finalText
if (ctx._internal.toolProcessingState && !ctx._internal.toolProcessingState?.output) {
ctx._internal.toolProcessingState.output = finalText
}
if (chunk.type === ChunkType.LLM_RESPONSE_COMPLETE) {
// 处理 onResponse 回调 - 发送最终完整文本
if (params.onResponse) {
params.onResponse(finalText, true)
params.onResponse(accumulatedTextContent, true)
}
controller.enqueue({
type: ChunkType.TEXT_COMPLETE,
text: finalText
text: accumulatedTextContent
})
controller.enqueue(chunk)
} else {
@@ -89,7 +77,6 @@ export const TextChunkMiddleware: CompletionsMiddleware =
})
controller.enqueue(chunk)
}
hasTextCompleteEventEnqueue = true
accumulatedTextContent = ''
} else {
// 其他类型的chunk直接传递
@@ -62,20 +62,11 @@ export const ThinkChunkMiddleware: CompletionsMiddleware =
// 更新思考时间并传递
const enhancedChunk: ThinkingDeltaChunk = {
...thinkingChunk,
text: accumulatedThinkingContent,
thinking_millsec: thinkingStartTime > 0 ? Date.now() - thinkingStartTime : 0
}
controller.enqueue(enhancedChunk)
} else if (chunk.type === ChunkType.THINKING_COMPLETE) {
const thinkingCompleteChunk = chunk as ThinkingCompleteChunk
controller.enqueue({
...thinkingCompleteChunk,
text: accumulatedThinkingContent,
thinking_millsec: thinkingStartTime > 0 ? Date.now() - thinkingStartTime : 0
})
hasThinkingContent = false
accumulatedThinkingContent = ''
thinkingStartTime = 0
} else if (hasThinkingContent && thinkingStartTime > 0) {
} else if (hasThinkingContent && thinkingStartTime > 0 && chunk.type !== ChunkType.THINKING_START) {
// 收到任何非THINKING_DELTA的chunk时,如果有累积的思考内容,生成THINKING_COMPLETE
const thinkingCompleteChunk: ThinkingCompleteChunk = {
type: ChunkType.THINKING_COMPLETE,
@@ -1,5 +1,11 @@
import { Model } from '@renderer/types'
import { ChunkType, TextDeltaChunk, ThinkingCompleteChunk, ThinkingDeltaChunk } from '@renderer/types/chunk'
import {
ChunkType,
TextDeltaChunk,
ThinkingCompleteChunk,
ThinkingDeltaChunk,
ThinkingStartChunk
} from '@renderer/types/chunk'
import { TagConfig, TagExtractor } from '@renderer/utils/tagExtraction'
import Logger from 'electron-log/renderer'
@@ -59,6 +65,8 @@ export const ThinkingTagExtractionMiddleware: CompletionsMiddleware =
let hasThinkingContent = false
let thinkingStartTime = 0
let isFirstTextChunk = true
let accumulatedThinkingContent = ''
const processedStream = resultFromUpstream.pipeThrough(
new TransformStream<GenericChunk, GenericChunk>({
transform(chunk: GenericChunk, controller) {
@@ -87,17 +95,27 @@ export const ThinkingTagExtractionMiddleware: CompletionsMiddleware =
if (!hasThinkingContent) {
hasThinkingContent = true
thinkingStartTime = Date.now()
controller.enqueue({
type: ChunkType.THINKING_START
} as ThinkingStartChunk)
}
if (extractionResult.content?.trim()) {
accumulatedThinkingContent += extractionResult.content
const thinkingDeltaChunk: ThinkingDeltaChunk = {
type: ChunkType.THINKING_DELTA,
text: extractionResult.content,
text: accumulatedThinkingContent,
thinking_millsec: thinkingStartTime > 0 ? Date.now() - thinkingStartTime : 0
}
controller.enqueue(thinkingDeltaChunk)
}
} else {
if (isFirstTextChunk) {
controller.enqueue({
type: ChunkType.TEXT_START
})
isFirstTextChunk = false
}
// 发送清理后的文本内容
const cleanTextChunk: TextDeltaChunk = {
...textChunk,
@@ -107,7 +125,7 @@ export const ThinkingTagExtractionMiddleware: CompletionsMiddleware =
}
}
}
} else {
} else if (chunk.type !== ChunkType.TEXT_START) {
// 其他类型的chunk直接传递(包括 THINKING_DELTA, THINKING_COMPLETE 等)
controller.enqueue(chunk)
}
@@ -23,7 +23,7 @@ export interface CompletionsParams {
* 'generate':
* 'check': API检查
*/
callType?: 'chat' | 'translate' | 'summary' | 'search' | 'generate' | 'check'
callType?: 'chat' | 'translate' | 'summary' | 'search' | 'generate' | 'check' | 'test'
// 基础对话数据
messages: Message[] | string // 联合类型方便判断是否为空
@@ -49,6 +49,7 @@ export interface CompletionsParams {
// 功能开关
streamOutput: boolean
enableWebSearch?: boolean
enableUrlContext?: boolean
enableReasoning?: boolean
enableGenerateImage?: boolean
+6 -7
View File
@@ -136,17 +136,16 @@
}
}
.ant-collapse {
.ant-collapse:not(.ant-collapse-ghost) {
border: 1px solid var(--color-border);
.ant-color-picker & {
border: none;
}
}
.ant-collapse-content {
border-top: 0.5px solid var(--color-border) !important;
.ant-color-picker & {
border-top: none !important;
.ant-collapse-content {
border-top: 0.5px solid var(--color-border) !important;
.ant-color-picker & {
border-top: none !important;
}
}
}
+1 -1
View File
@@ -44,7 +44,7 @@
--color-reference-text: #ffffff;
--color-reference-background: #0b0e12;
--color-list-item: #222;
--color-list-item: #252525;
--color-list-item-hover: #1e1e1e;
--modal-background: #111111;
+1 -1
View File
@@ -139,7 +139,7 @@ ul {
}
}
.message-content-container {
border-radius: 10px 0 10px 10px;
border-radius: 10px;
padding: 10px 16px 10px 16px;
background-color: var(--chat-background-user);
align-self: self-end;
+25 -16
View File
@@ -19,12 +19,13 @@
h4,
h5,
h6 {
margin: 1em 0 1em 0;
margin: 1.5em 0 1em 0;
line-height: 1.3;
font-weight: bold;
font-family: var(--font-family);
}
h1 {
margin-top: 0;
font-size: 2em;
border-bottom: 0.5px solid var(--color-border);
padding-bottom: 0.3em;
@@ -53,8 +54,9 @@
}
p {
margin: 1em 0;
margin: 1.3em 0;
white-space: pre-wrap;
line-height: 1.6;
&:last-child {
margin-bottom: 5px;
@@ -82,7 +84,7 @@
li {
margin-bottom: 0.5em;
pre {
margin: 1.5em 0;
margin: 1.5em 0 !important;
}
&::marker {
color: var(--color-text-3);
@@ -108,6 +110,7 @@
li code {
background: var(--color-background-mute);
padding: 3px 5px;
margin: 0 2px;
border-radius: 5px;
word-break: keep-all;
white-space: pre;
@@ -120,11 +123,9 @@
pre {
border-radius: 8px;
overflow-x: auto;
font-family: 'Fira Code', 'Courier New', Courier, monospace;
font-family: var(--code-font-family);
background-color: var(--color-background-mute);
&:has(.mermaid),
&:has(.plantuml-preview),
&:has(.svg-preview) {
&:has(.special-preview) {
background-color: transparent;
}
&:not(pre pre) {
@@ -148,16 +149,19 @@
}
blockquote {
margin: 1em 0;
padding-left: 1em;
color: var(--color-text-light);
border-left: 4px solid var(--color-border);
font-family: var(--font-family);
margin: 1.5em 0;
padding: 1em 1.5em;
background-color: var(--color-background-soft);
border-left: 4px solid var(--color-primary);
border-radius: 0 8px 8px 0;
font-style: italic;
position: relative;
}
table {
--table-border-radius: 8px;
margin: 1em 0;
margin: 2em 0;
font-size: 0.9em;
width: 100%;
border-radius: var(--table-border-radius);
overflow: hidden;
@@ -182,13 +186,18 @@
th {
background-color: var(--color-background-mute);
font-weight: bold;
font-family: var(--font-family);
font-weight: 600;
text-align: left;
}
tr:hover {
background-color: var(--color-background-soft);
}
img {
max-width: 100%;
height: auto;
margin: 10px 0;
}
a,
@@ -1,24 +1,28 @@
import { Alert } from 'antd'
import { t } from 'i18next'
import { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
const LOCALSTORAGE_KEY = 'openai_alert_closed'
const OpenAIAlert = () => {
const { t } = useTranslation()
interface Props {
message?: string
key?: string
}
const OpenAIAlert = ({ message = t('settings.provider.openai.alert'), key = LOCALSTORAGE_KEY }: Props) => {
const [visible, setVisible] = useState(false)
useEffect(() => {
const closed = localStorage.getItem(LOCALSTORAGE_KEY)
const closed = localStorage.getItem(key)
setVisible(!closed)
}, [])
}, [key])
if (!visible) return null
return (
<Alert
style={{ width: '100%', marginTop: 5, marginBottom: 5 }}
message={t('settings.provider.openai.alert')}
message={message}
closable
afterClose={() => {
localStorage.setItem(LOCALSTORAGE_KEY, '1')
@@ -1,4 +1,4 @@
import { CodeTool, TOOL_SPECS, useCodeTool } from '@renderer/components/CodeToolbar'
import { TOOL_SPECS, useCodeTool } from '@renderer/components/CodeToolbar'
import { useCodeStyle } from '@renderer/context/CodeStyleProvider'
import { useCodeHighlight } from '@renderer/hooks/useCodeHighlight'
import { useSettings } from '@renderer/hooks/useSettings'
@@ -12,10 +12,10 @@ import { useTranslation } from 'react-i18next'
import { ThemedToken } from 'shiki/core'
import styled from 'styled-components'
interface CodePreviewProps {
children: string
import { BasicPreviewProps } from './types'
interface CodePreviewProps extends BasicPreviewProps {
language: string
setTools?: (value: React.SetStateAction<CodeTool[]>) => void
}
const MAX_COLLAPSE_HEIGHT = 350
@@ -164,19 +164,11 @@ const CodePreview = ({ children, language, setTools }: CodePreviewProps) => {
}}>
<div
style={{
/*
* FIXME: @tanstack/react-virtual 使
* `self-end`
*
*
*
*/
position: 'relative',
position: 'absolute',
top: 0,
left: 0,
width: '100%',
transform: `translateY(${virtualItems[0]?.start ?? 0}px)`,
willChange: 'transform'
transform: `translateY(${virtualItems[0]?.start ?? 0}px)`
}}>
{virtualizer.getVirtualItems().map((virtualItem) => (
<div key={virtualItem.key} data-index={virtualItem.index} ref={virtualizer.measureElement}>
@@ -0,0 +1,102 @@
import { usePreviewToolHandlers, usePreviewTools } from '@renderer/components/CodeToolbar'
import SvgSpinners180Ring from '@renderer/components/Icons/SvgSpinners180Ring'
import { AsyncInitializer } from '@renderer/utils/asyncInitializer'
import { Flex, Spin } from 'antd'
import { debounce } from 'lodash'
import React, { memo, startTransition, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import styled from 'styled-components'
import PreviewError from './PreviewError'
import { BasicPreviewProps } from './types'
// 管理 viz 实例
const vizInitializer = new AsyncInitializer(async () => {
const module = await import('@viz-js/viz')
return await module.instance()
})
/** Graphviz
*
*/
const GraphvizPreview: React.FC<BasicPreviewProps> = ({ children, setTools }) => {
const graphvizRef = useRef<HTMLDivElement>(null)
const [error, setError] = useState<string | null>(null)
const [isLoading, setIsLoading] = useState(false)
// 使用通用图像工具
const { handleZoom, handleCopyImage, handleDownload } = usePreviewToolHandlers(graphvizRef, {
imgSelector: 'svg',
prefix: 'graphviz',
enableWheelZoom: true
})
// 使用工具栏
usePreviewTools({
setTools,
handleZoom,
handleCopyImage,
handleDownload
})
// 实际的渲染函数
const renderGraphviz = useCallback(async (content: string) => {
if (!content || !graphvizRef.current) return
try {
setIsLoading(true)
const viz = await vizInitializer.get()
const svgElement = viz.renderSVGElement(content)
// 清空容器并添加新的 SVG
graphvizRef.current.innerHTML = ''
graphvizRef.current.appendChild(svgElement)
// 渲染成功,清除错误记录
setError(null)
} catch (error) {
setError((error as Error).message || 'DOT syntax error or rendering failed')
} finally {
setIsLoading(false)
}
}, [])
// debounce 渲染
const debouncedRender = useMemo(
() =>
debounce((content: string) => {
startTransition(() => renderGraphviz(content))
}, 300),
[renderGraphviz]
)
// 触发渲染
useEffect(() => {
if (children) {
setIsLoading(true)
debouncedRender(children)
} else {
debouncedRender.cancel()
setIsLoading(false)
}
return () => {
debouncedRender.cancel()
}
}, [children, debouncedRender])
return (
<Spin spinning={isLoading} indicator={<SvgSpinners180Ring color="var(--color-text-2)" />}>
<Flex vertical style={{ minHeight: isLoading ? '2rem' : 'auto' }}>
{error && <PreviewError>{error}</PreviewError>}
<StyledGraphviz ref={graphvizRef} className="graphviz special-preview" />
</Flex>
</Spin>
)
}
const StyledGraphviz = styled.div`
overflow: auto;
`
export default memo(GraphvizPreview)
@@ -1,70 +0,0 @@
import { ExpandOutlined, LinkOutlined } from '@ant-design/icons'
import { AppLogo } from '@renderer/config/env'
import { useMinappPopup } from '@renderer/hooks/useMinappPopup'
import { extractTitle } from '@renderer/utils/formats'
import { Button } from 'antd'
import { FC } from 'react'
import { useTranslation } from 'react-i18next'
import styled from 'styled-components'
interface Props {
html: string
}
const Artifacts: FC<Props> = ({ html }) => {
const { t } = useTranslation()
const { openMinapp } = useMinappPopup()
/**
*
*/
const handleOpenInApp = async () => {
const path = await window.api.file.createTempFile('artifacts-preview.html')
await window.api.file.write(path, html)
const filePath = `file://${path}`
const title = extractTitle(html) || 'Artifacts ' + t('chat.artifacts.button.preview')
openMinapp({
id: 'artifacts-preview',
name: title,
logo: AppLogo,
url: filePath
})
}
/**
*
*/
const handleOpenExternal = async () => {
const path = await window.api.file.createTempFile('artifacts-preview.html')
await window.api.file.write(path, html)
const filePath = `file://${path}`
if (window.api.shell && window.api.shell.openExternal) {
window.api.shell.openExternal(filePath)
} else {
console.error(t('artifacts.preview.openExternal.error.content'))
}
}
return (
<Container>
<Button icon={<ExpandOutlined />} onClick={handleOpenInApp}>
{t('chat.artifacts.button.preview')}
</Button>
<Button icon={<LinkOutlined />} onClick={handleOpenExternal}>
{t('chat.artifacts.button.openExternal')}
</Button>
</Container>
)
}
const Container = styled.div`
margin: 10px;
display: flex;
flex-direction: row;
gap: 8px;
padding-bottom: 10px;
`
export default Artifacts
@@ -0,0 +1,338 @@
import { CodeOutlined, LinkOutlined } from '@ant-design/icons'
import { useTheme } from '@renderer/context/ThemeProvider'
import { ThemeMode } from '@renderer/types'
import { extractTitle } from '@renderer/utils/formats'
import { Button } from 'antd'
import { Code, Download, Globe, Sparkles } from 'lucide-react'
import { FC, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { ClipLoader } from 'react-spinners'
import styled, { keyframes } from 'styled-components'
import HtmlArtifactsPopup from './HtmlArtifactsPopup'
const HTML_VOID_ELEMENTS = new Set([
'area',
'base',
'br',
'col',
'embed',
'hr',
'img',
'input',
'link',
'meta',
'param',
'source',
'track',
'wbr'
])
const HTML_COMPLETION_PATTERNS = [
/<\/html\s*>/i,
/<!DOCTYPE\s+html/i,
/<\/body\s*>/i,
/<\/div\s*>/i,
/<\/script\s*>/i,
/<\/style\s*>/i
]
interface Props {
html: string
}
function hasUnmatchedTags(html: string): boolean {
const stack: string[] = []
const tagRegex = /<\/?([a-zA-Z][a-zA-Z0-9]*)[^>]*>/g
let match
while ((match = tagRegex.exec(html)) !== null) {
const [fullTag, tagName] = match
const isClosing = fullTag.startsWith('</')
const isSelfClosing = fullTag.endsWith('/>') || HTML_VOID_ELEMENTS.has(tagName.toLowerCase())
if (isSelfClosing) continue
if (isClosing) {
if (stack.length === 0 || stack.pop() !== tagName.toLowerCase()) {
return true
}
} else {
stack.push(tagName.toLowerCase())
}
}
return stack.length > 0
}
function checkIsStreaming(html: string): boolean {
if (!html?.trim()) return false
const trimmed = html.trim()
// 快速检查:如果有明显的完成标志,直接返回false
for (const pattern of HTML_COMPLETION_PATTERNS) {
if (pattern.test(trimmed)) {
// 特殊情况:同时有DOCTYPE和</body>
if (trimmed.includes('<!DOCTYPE') && /<\/body\s*>/i.test(trimmed)) {
return false
}
// 如果只是以</html>结尾,也认为是完成的
if (/<\/html\s*>$/i.test(trimmed)) {
return false
}
}
}
// 检查未完成的标志
const hasIncompleteTag = /<[^>]*$/.test(trimmed)
const hasUnmatched = hasUnmatchedTags(trimmed)
if (hasIncompleteTag || hasUnmatched) return true
// 对于简单片段,如果长度较短且没有明显结束标志,可能还在生成
const hasStructureTags = /<(html|body|head)[^>]*>/i.test(trimmed)
if (!hasStructureTags && trimmed.length < 500) {
return !HTML_COMPLETION_PATTERNS.some((pattern) => pattern.test(trimmed))
}
return false
}
const getTerminalStyles = (theme: ThemeMode) => ({
background: theme === 'dark' ? '#1e1e1e' : '#f0f0f0',
color: theme === 'dark' ? '#cccccc' : '#333333',
promptColor: theme === 'dark' ? '#00ff00' : '#007700'
})
const HtmlArtifactsCard: FC<Props> = ({ html }) => {
const { t } = useTranslation()
const title = extractTitle(html) || 'HTML Artifacts'
const [isPopupOpen, setIsPopupOpen] = useState(false)
const { theme } = useTheme()
const htmlContent = html || ''
const hasContent = htmlContent.trim().length > 0
const isStreaming = useMemo(() => checkIsStreaming(htmlContent), [htmlContent])
const handleOpenExternal = async () => {
const path = await window.api.file.createTempFile('artifacts-preview.html')
await window.api.file.write(path, htmlContent)
const filePath = `file://${path}`
if (window.api.shell?.openExternal) {
window.api.shell.openExternal(filePath)
} else {
console.error(t('artifacts.preview.openExternal.error.content'))
}
}
const handleDownload = async () => {
const fileName = `${title.replace(/[^a-zA-Z0-9\s]/g, '').replace(/\s+/g, '-') || 'html-artifact'}.html`
await window.api.file.save(fileName, htmlContent)
window.message.success({ content: t('message.download.success'), key: 'download' })
}
return (
<>
<Container $isStreaming={isStreaming}>
<Header>
<IconWrapper $isStreaming={isStreaming}>
{isStreaming ? <Sparkles size={20} color="white" /> : <Globe size={20} color="white" />}
</IconWrapper>
<TitleSection>
<Title>{title}</Title>
<TypeBadge>
<Code size={12} />
<span>HTML</span>
</TypeBadge>
</TitleSection>
</Header>
<Content>
{isStreaming && !hasContent ? (
<GeneratingContainer>
<ClipLoader size={20} color="var(--color-primary)" />
<GeneratingText>{t('html_artifacts.generating_content', 'Generating content...')}</GeneratingText>
</GeneratingContainer>
) : isStreaming && hasContent ? (
<>
<TerminalPreview $theme={theme}>
<TerminalContent $theme={theme}>
<TerminalLine>
<TerminalPrompt $theme={theme}>$</TerminalPrompt>
<TerminalCodeLine $theme={theme}>
{htmlContent.trim().split('\n').slice(-3).join('\n')}
<TerminalCursor $theme={theme} />
</TerminalCodeLine>
</TerminalLine>
</TerminalContent>
</TerminalPreview>
<ButtonContainer>
<Button icon={<CodeOutlined />} onClick={() => setIsPopupOpen(true)} type="primary">
{t('chat.artifacts.button.preview')}
</Button>
</ButtonContainer>
</>
) : (
<ButtonContainer>
<Button icon={<CodeOutlined />} onClick={() => setIsPopupOpen(true)} type="text" disabled={!hasContent}>
{t('chat.artifacts.button.preview')}
</Button>
<Button icon={<LinkOutlined />} onClick={handleOpenExternal} type="text" disabled={!hasContent}>
{t('chat.artifacts.button.openExternal')}
</Button>
<Button icon={<Download size={16} />} onClick={handleDownload} type="text" disabled={!hasContent}>
{t('code_block.download')}
</Button>
</ButtonContainer>
)}
</Content>
</Container>
<HtmlArtifactsPopup open={isPopupOpen} title={title} html={htmlContent} onClose={() => setIsPopupOpen(false)} />
</>
)
}
const Container = styled.div<{ $isStreaming: boolean }>`
background: var(--color-background);
border: 1px solid var(--color-border);
border-radius: 8px;
overflow: hidden;
margin: 10px 0;
`
const GeneratingContainer = styled.div`
display: flex;
justify-content: center;
align-items: center;
gap: 8px;
padding: 20px;
min-height: 78px;
`
const GeneratingText = styled.div`
font-size: 14px;
color: var(--color-text-secondary);
`
const Header = styled.div`
display: flex;
align-items: center;
gap: 12px;
padding: 20px 24px 16px;
background: var(--color-background-soft);
border-bottom: 1px solid var(--color-border);
border-radius: 8px 8px 0 0;
`
const IconWrapper = styled.div<{ $isStreaming: boolean }>`
display: flex;
align-items: center;
justify-content: center;
width: 40px;
height: 40px;
background: ${(props) =>
props.$isStreaming
? 'linear-gradient(135deg, #f59e0b 0%, #d97706 100%)'
: 'linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%)'};
border-radius: 12px;
color: white;
box-shadow: ${(props) =>
props.$isStreaming ? '0 4px 6px -1px rgba(245, 158, 11, 0.3)' : '0 4px 6px -1px rgba(59, 130, 246, 0.3)'};
transition: background 0.3s ease;
`
const TitleSection = styled.div`
flex: 1;
display: flex;
flex-direction: column;
gap: 6px;
`
const Title = styled.h3`
margin: 0 !important;
font-size: 16px;
font-weight: 600;
color: var(--color-text);
line-height: 1.4;
`
const TypeBadge = styled.div`
display: inline-flex;
align-items: center;
gap: 4px;
padding: 3px 6px;
background: var(--color-background-mute);
border: 1px solid var(--color-border);
border-radius: 6px;
font-size: 10px;
font-weight: 500;
color: var(--color-text-secondary);
width: fit-content;
`
const Content = styled.div`
padding: 0;
background: var(--color-background);
`
const ButtonContainer = styled.div`
margin: 10px 16px !important;
display: flex;
flex-direction: row;
gap: 8px;
`
const TerminalPreview = styled.div<{ $theme: ThemeMode }>`
margin: 16px;
background: ${(props) => getTerminalStyles(props.$theme).background};
border-radius: 8px;
overflow: hidden;
font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Roboto Mono', monospace;
`
const TerminalContent = styled.div<{ $theme: ThemeMode }>`
padding: 12px;
background: ${(props) => getTerminalStyles(props.$theme).background};
color: ${(props) => getTerminalStyles(props.$theme).color};
font-size: 13px;
line-height: 1.4;
min-height: 80px;
`
const TerminalLine = styled.div`
display: flex;
align-items: flex-start;
gap: 8px;
`
const TerminalCodeLine = styled.span<{ $theme: ThemeMode }>`
flex: 1;
white-space: pre-wrap;
word-break: break-word;
color: ${(props) => getTerminalStyles(props.$theme).color};
background-color: transparent !important;
`
const TerminalPrompt = styled.span<{ $theme: ThemeMode }>`
color: ${(props) => getTerminalStyles(props.$theme).promptColor};
font-weight: bold;
flex-shrink: 0;
`
const blinkAnimation = keyframes`
0%, 50% { opacity: 1; }
51%, 100% { opacity: 0; }
`
const TerminalCursor = styled.span<{ $theme: ThemeMode }>`
display: inline-block;
width: 2px;
height: 16px;
background: ${(props) => getTerminalStyles(props.$theme).promptColor};
animation: ${blinkAnimation} 1s infinite;
margin-left: 2px;
`
export default HtmlArtifactsCard
@@ -0,0 +1,352 @@
import CodeEditor from '@renderer/components/CodeEditor'
import { isLinux, isMac, isWin } from '@renderer/config/constant'
import { classNames } from '@renderer/utils'
import { Button, Modal } from 'antd'
import { Code, Maximize2, Minimize2, Monitor, MonitorSpeaker, X } from 'lucide-react'
import { useEffect, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import styled from 'styled-components'
interface HtmlArtifactsPopupProps {
open: boolean
title: string
html: string
onClose: () => void
}
type ViewMode = 'split' | 'code' | 'preview'
const HtmlArtifactsPopup: React.FC<HtmlArtifactsPopupProps> = ({ open, title, html, onClose }) => {
const { t } = useTranslation()
const [viewMode, setViewMode] = useState<ViewMode>('split')
const [currentHtml, setCurrentHtml] = useState(html)
const [isFullscreen, setIsFullscreen] = useState(false)
// 预览刷新相关状态
const [previewHtml, setPreviewHtml] = useState(html)
const intervalRef = useRef<NodeJS.Timeout | null>(null)
const latestHtmlRef = useRef(html)
// 当外部html更新时,同步更新内部状态
useEffect(() => {
setCurrentHtml(html)
latestHtmlRef.current = html
}, [html])
// 当内部编辑的html更新时,更新引用
useEffect(() => {
latestHtmlRef.current = currentHtml
}, [currentHtml])
// 2秒定时检查并刷新预览(仅在内容变化时)
useEffect(() => {
if (!open) return
// 立即设置初始预览内容
setPreviewHtml(currentHtml)
// 设置定时器,每2秒检查一次内容是否有变化
intervalRef.current = setInterval(() => {
if (latestHtmlRef.current !== previewHtml) {
setPreviewHtml(latestHtmlRef.current)
}
}, 2000)
// 清理函数
return () => {
if (intervalRef.current) {
clearInterval(intervalRef.current)
}
}
}, [open, previewHtml])
// 全屏时防止 body 滚动
useEffect(() => {
if (!open || !isFullscreen) return
const body = document.body
const originalOverflow = body.style.overflow
body.style.overflow = 'hidden'
return () => {
body.style.overflow = originalOverflow
}
}, [isFullscreen, open])
const showCode = viewMode === 'split' || viewMode === 'code'
const showPreview = viewMode === 'split' || viewMode === 'preview'
const renderHeader = () => (
<ModalHeader onDoubleClick={() => setIsFullscreen(!isFullscreen)} className={classNames({ drag: isFullscreen })}>
<HeaderLeft $isFullscreen={isFullscreen}>
<TitleText>{title}</TitleText>
</HeaderLeft>
<HeaderCenter>
<ViewControls>
<ViewButton
size="small"
type={viewMode === 'split' ? 'primary' : 'default'}
icon={<MonitorSpeaker size={14} />}
onClick={() => setViewMode('split')}>
{t('html_artifacts.split')}
</ViewButton>
<ViewButton
size="small"
type={viewMode === 'code' ? 'primary' : 'default'}
icon={<Code size={14} />}
onClick={() => setViewMode('code')}>
{t('html_artifacts.code')}
</ViewButton>
<ViewButton
size="small"
type={viewMode === 'preview' ? 'primary' : 'default'}
icon={<Monitor size={14} />}
onClick={() => setViewMode('preview')}>
{t('html_artifacts.preview')}
</ViewButton>
</ViewControls>
</HeaderCenter>
<HeaderRight $isFullscreen={isFullscreen}>
<Button
onClick={() => setIsFullscreen(!isFullscreen)}
type="text"
icon={isFullscreen ? <Minimize2 size={16} /> : <Maximize2 size={16} />}
className="nodrag"
/>
<Button onClick={onClose} type="text" icon={<X size={16} />} className="nodrag" />
</HeaderRight>
</ModalHeader>
)
return (
<StyledModal
$isFullscreen={isFullscreen}
title={renderHeader()}
open={open}
afterClose={onClose}
centered={!isFullscreen}
destroyOnClose
mask={!isFullscreen}
maskClosable={false}
width={isFullscreen ? '100vw' : '90vw'}
style={{
maxWidth: isFullscreen ? '100vw' : '1400px',
height: isFullscreen ? '100vh' : 'auto'
}}
zIndex={isFullscreen ? 10000 : 1000}
footer={null}
closable={false}>
<Container>
{showCode && (
<CodeSection>
<CodeEditor
value={currentHtml}
language="html"
editable={true}
onSave={setCurrentHtml}
style={{ height: '100%' }}
options={{
stream: false,
collapsible: false
}}
/>
</CodeSection>
)}
{showPreview && (
<PreviewSection>
{previewHtml.trim() ? (
<PreviewFrame
key={previewHtml} // 强制重新创建iframe当预览内容变化时
srcDoc={previewHtml}
title="HTML Preview"
sandbox="allow-scripts allow-same-origin allow-forms"
/>
) : (
<EmptyPreview>
<p>{t('html_artifacts.empty_preview', 'No content to preview')}</p>
</EmptyPreview>
)}
</PreviewSection>
)}
</Container>
</StyledModal>
)
}
// 简化的样式组件
const StyledModal = styled(Modal)<{ $isFullscreen?: boolean }>`
${(props) =>
props.$isFullscreen
? `
position: fixed !important;
top: 0 !important;
left: 0 !important;
z-index: 10000 !important;
.ant-modal-wrap {
padding: 0 !important;
position: fixed !important;
inset: 0 !important;
}
.ant-modal {
margin: 0 !important;
padding: 0 !important;
max-width: none !important;
position: fixed !important;
inset: 0 !important;
}
.ant-modal-body {
height: calc(100vh - 45px) !important;
}
`
: `
.ant-modal-body {
height: 80vh !important;
min-height: 600px !important;
}
`}
.ant-modal-body {
padding: 0 !important;
display: flex !important;
flex-direction: column !important;
max-height: initial !important;
}
.ant-modal-content {
border-radius: ${(props) => (props.$isFullscreen ? '0px' : '12px')};
overflow: hidden;
height: ${(props) => (props.$isFullscreen ? '100vh' : 'auto')};
padding: 0 !important;
}
.ant-modal-header {
padding: 10px !important;
border-bottom: 1px solid var(--color-border);
background: var(--color-background);
margin-bottom: 0 !important;
border-radius: 0 !important;
}
`
const ModalHeader = styled.div`
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
position: relative;
`
const HeaderLeft = styled.div<{ $isFullscreen?: boolean }>`
flex: 1;
min-width: 0;
padding-left: ${(props) => (props.$isFullscreen && isMac ? '65px' : '12px')};
`
const HeaderCenter = styled.div`
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
`
const HeaderRight = styled.div<{ $isFullscreen?: boolean }>`
flex: 1;
display: flex;
align-items: center;
justify-content: flex-end;
gap: 8px;
padding-right: ${({ $isFullscreen }) => ($isFullscreen ? (isWin ? '136px' : isLinux ? '120px' : '12px') : '12px')};
`
const TitleText = styled.span`
font-size: 16px;
font-weight: 600;
color: var(--color-text);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
`
const ViewControls = styled.div`
display: flex;
gap: 8px;
padding: 4px;
background: var(--color-background-mute);
border-radius: 8px;
border: 1px solid var(--color-border);
-webkit-app-region: no-drag;
`
const ViewButton = styled(Button)`
border: none;
box-shadow: none;
&.ant-btn-primary {
background: var(--color-primary);
color: white;
}
&.ant-btn-default {
background: transparent;
color: var(--color-text-secondary);
&:hover {
background: var(--color-background);
color: var(--color-text);
}
}
`
const Container = styled.div`
display: flex;
height: 100%;
width: 100%;
flex: 1;
background: var(--color-background);
`
const CodeSection = styled.div`
flex: 1;
min-width: 300px;
border-right: 1px solid var(--color-border);
overflow: hidden;
.monaco-editor,
.cm-editor,
.cm-scroller {
height: 100% !important;
}
`
const PreviewSection = styled.div`
flex: 1;
min-width: 300px;
background: white;
overflow: hidden;
`
const PreviewFrame = styled.iframe`
width: 100%;
height: 100%;
border: none;
background: white;
`
const EmptyPreview = styled.div`
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
background: var(--color-background-soft);
color: var(--color-text-secondary);
font-size: 14px;
`
export default HtmlArtifactsPopup
@@ -1,5 +1,5 @@
import { nanoid } from '@reduxjs/toolkit'
import { CodeTool, usePreviewToolHandlers, usePreviewTools } from '@renderer/components/CodeToolbar'
import { usePreviewToolHandlers, usePreviewTools } from '@renderer/components/CodeToolbar'
import SvgSpinners180Ring from '@renderer/components/Icons/SvgSpinners180Ring'
import { useMermaid } from '@renderer/hooks/useMermaid'
import { Flex, Spin } from 'antd'
@@ -7,16 +7,14 @@ import { debounce } from 'lodash'
import React, { memo, startTransition, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import styled from 'styled-components'
interface Props {
children: string
setTools?: (value: React.SetStateAction<CodeTool[]>) => void
}
import PreviewError from './PreviewError'
import { BasicPreviewProps } from './types'
/** Mermaid
*
* FIXME: 等将来容易判断代码块结束位置时再重构
*/
const MermaidPreview: React.FC<Props> = ({ children, setTools }) => {
const MermaidPreview: React.FC<BasicPreviewProps> = ({ children, setTools }) => {
const { mermaid, isLoading: isLoadingMermaid, error: mermaidError } = useMermaid()
const mermaidRef = useRef<HTMLDivElement>(null)
const diagramId = useRef<string>(`mermaid-${nanoid(6)}`).current
@@ -143,8 +141,8 @@ const MermaidPreview: React.FC<Props> = ({ children, setTools }) => {
return (
<Spin spinning={isLoading} indicator={<SvgSpinners180Ring color="var(--color-text-2)" />}>
<Flex vertical style={{ minHeight: isLoading ? '2rem' : 'auto' }}>
{(mermaidError || error) && <StyledError>{mermaidError || error}</StyledError>}
<StyledMermaid ref={mermaidRef} className="mermaid" />
{(mermaidError || error) && <PreviewError>{mermaidError || error}</PreviewError>}
<StyledMermaid ref={mermaidRef} className="mermaid special-preview" />
</Flex>
</Spin>
)
@@ -154,14 +152,4 @@ const StyledMermaid = styled.div`
overflow: auto;
`
const StyledError = styled.div`
overflow: auto;
padding: 16px;
color: #ff4d4f;
border: 1px solid #ff4d4f;
border-radius: 4px;
word-wrap: break-word;
white-space: pre-wrap;
`
export default memo(MermaidPreview)
@@ -1,11 +1,13 @@
import { LoadingOutlined } from '@ant-design/icons'
import { CodeTool, usePreviewToolHandlers, usePreviewTools } from '@renderer/components/CodeToolbar'
import { usePreviewToolHandlers, usePreviewTools } from '@renderer/components/CodeToolbar'
import { Spin } from 'antd'
import pako from 'pako'
import React, { memo, useCallback, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import styled from 'styled-components'
import { BasicPreviewProps } from './types'
const PlantUMLServer = 'https://www.plantuml.com/plantuml'
function encode64(data: Uint8Array) {
let r = ''
@@ -132,12 +134,7 @@ const PlantUMLServerImage: React.FC<PlantUMLServerImageProps> = ({ format, diagr
)
}
interface PlantUMLProps {
children: string
setTools?: (value: React.SetStateAction<CodeTool[]>) => void
}
const PlantUmlPreview: React.FC<PlantUMLProps> = ({ children, setTools }) => {
const PlantUmlPreview: React.FC<BasicPreviewProps> = ({ children, setTools }) => {
const { t } = useTranslation()
const containerRef = useRef<HTMLDivElement>(null)
@@ -174,7 +171,7 @@ const PlantUmlPreview: React.FC<PlantUMLProps> = ({ children, setTools }) => {
return (
<div ref={containerRef}>
<PlantUMLServerImage format="svg" diagram={children} className="plantuml-preview" />
<PlantUMLServerImage format="svg" diagram={children} className="plantuml-preview special-preview" />
</div>
)
}
@@ -0,0 +1,14 @@
import { memo } from 'react'
import { styled } from 'styled-components'
const PreviewError = styled.div`
overflow: auto;
padding: 16px;
color: #ff4d4f;
border: 1px solid #ff4d4f;
border-radius: 4px;
word-wrap: break-word;
white-space: pre-wrap;
`
export default memo(PreviewError)
@@ -1,15 +1,12 @@
import { CodeTool, usePreviewToolHandlers, usePreviewTools } from '@renderer/components/CodeToolbar'
import { usePreviewToolHandlers, usePreviewTools } from '@renderer/components/CodeToolbar'
import { memo, useEffect, useRef } from 'react'
interface Props {
children: string
setTools?: (value: React.SetStateAction<CodeTool[]>) => void
}
import { BasicPreviewProps } from './types'
/**
* 使 Shadow DOM SVG
*/
const SvgPreview: React.FC<Props> = ({ children, setTools }) => {
const SvgPreview: React.FC<BasicPreviewProps> = ({ children, setTools }) => {
const svgContainerRef = useRef<HTMLDivElement>(null)
useEffect(() => {
@@ -58,7 +55,7 @@ const SvgPreview: React.FC<Props> = ({ children, setTools }) => {
handleDownload
})
return <div ref={svgContainerRef} className="svg-preview" />
return <div ref={svgContainerRef} className="svg-preview special-preview" />
}
export default memo(SvgPreview)
@@ -0,0 +1,20 @@
import GraphvizPreview from './GraphvizPreview'
import MermaidPreview from './MermaidPreview'
import PlantUmlPreview from './PlantUmlPreview'
import SvgPreview from './SvgPreview'
/**
*
*/
export const SPECIAL_VIEWS = ['mermaid', 'plantuml', 'svg', 'dot', 'graphviz']
/**
*
*/
export const SPECIAL_VIEW_COMPONENTS = {
mermaid: MermaidPreview,
plantuml: PlantUmlPreview,
svg: SvgPreview,
dot: GraphvizPreview,
graphviz: GraphvizPreview
} as const
@@ -0,0 +1,2 @@
export * from './types'
export * from './view'
@@ -0,0 +1,14 @@
import { CodeTool } from '@renderer/components/CodeToolbar'
/**
* props
*/
export interface BasicPreviewProps {
children: string
setTools?: (value: React.SetStateAction<CodeTool[]>) => void
}
/**
*
*/
export type ViewMode = 'source' | 'special' | 'split'
@@ -4,7 +4,7 @@ import { CodeTool, CodeToolbar, TOOL_SPECS, useCodeTool } from '@renderer/compon
import { useSettings } from '@renderer/hooks/useSettings'
import { pyodideService } from '@renderer/services/PyodideService'
import { extractTitle } from '@renderer/utils/formats'
import { getExtensionByLanguage, isValidPlantUML } from '@renderer/utils/markdown'
import { getExtensionByLanguage, isHtmlCode, isValidPlantUML } from '@renderer/utils/markdown'
import dayjs from 'dayjs'
import { CirclePlay, CodeXml, Copy, Download, Eye, Square, SquarePen, SquareSplitHorizontal } from 'lucide-react'
import React, { memo, useCallback, useEffect, useMemo, useState } from 'react'
@@ -12,13 +12,10 @@ import { useTranslation } from 'react-i18next'
import styled from 'styled-components'
import CodePreview from './CodePreview'
import HtmlArtifacts from './HtmlArtifacts'
import MermaidPreview from './MermaidPreview'
import PlantUmlPreview from './PlantUmlPreview'
import { SPECIAL_VIEW_COMPONENTS, SPECIAL_VIEWS } from './constants'
import HtmlArtifactsCard from './HtmlArtifactsCard'
import StatusBar from './StatusBar'
import SvgPreview from './SvgPreview'
type ViewMode = 'source' | 'special' | 'split'
import { ViewMode } from './types'
interface Props {
children: string
@@ -42,9 +39,10 @@ interface Props {
* - quick
* - core
*/
const CodeBlockView: React.FC<Props> = ({ children, language, onSave }) => {
export const CodeBlockView: React.FC<Props> = memo(({ children, language, onSave }) => {
const { t } = useTranslation()
const { codeEditor, codeExecution } = useSettings()
const [viewMode, setViewMode] = useState<ViewMode>('special')
const [isRunning, setIsRunning] = useState(false)
const [output, setOutput] = useState('')
@@ -56,7 +54,7 @@ const CodeBlockView: React.FC<Props> = ({ children, language, onSave }) => {
return codeExecution.enabled && language === 'python'
}, [codeExecution.enabled, language])
const hasSpecialView = useMemo(() => ['mermaid', 'plantuml', 'svg'].includes(language), [language])
const hasSpecialView = useMemo(() => SPECIAL_VIEWS.includes(language), [language])
const isInSpecialView = useMemo(() => {
return hasSpecialView && viewMode === 'special'
@@ -200,14 +198,16 @@ const CodeBlockView: React.FC<Props> = ({ children, language, onSave }) => {
// 特殊视图组件映射
const specialView = useMemo(() => {
if (language === 'mermaid') {
return <MermaidPreview setTools={setTools}>{children}</MermaidPreview>
} else if (language === 'plantuml' && isValidPlantUML(children)) {
return <PlantUmlPreview setTools={setTools}>{children}</PlantUmlPreview>
} else if (language === 'svg') {
return <SvgPreview setTools={setTools}>{children}</SvgPreview>
const SpecialView = SPECIAL_VIEW_COMPONENTS[language as keyof typeof SPECIAL_VIEW_COMPONENTS]
if (!SpecialView) return null
// PlantUML 语法验证
if (language === 'plantuml' && !isValidPlantUML(children)) {
return null
}
return null
return <SpecialView setTools={setTools}>{children}</SpecialView>
}, [children, language])
const renderHeader = useMemo(() => {
@@ -228,27 +228,29 @@ const CodeBlockView: React.FC<Props> = ({ children, language, onSave }) => {
)
}, [specialView, sourceView, viewMode])
const renderArtifacts = useMemo(() => {
if (language === 'html') {
return <HtmlArtifacts html={children} />
}
return null
}, [children, language])
// HTML 代码块特殊处理 - 在所有 hooks 调用之后
if (language === 'html' && isHtmlCode(children)) {
return <HtmlArtifactsCard html={children} />
}
return (
<CodeBlockWrapper className="code-block" $isInSpecialView={isInSpecialView}>
{renderHeader}
<CodeToolbar tools={tools} />
{renderContent}
{renderArtifacts}
{isExecutable && output && <StatusBar>{output}</StatusBar>}
</CodeBlockWrapper>
)
}
})
const CodeBlockWrapper = styled.div<{ $isInSpecialView: boolean }>`
position: relative;
width: 100%;
/* FIXME:
* CodePreview
* toolbar title
*/
min-width: 45ch;
.code-toolbar {
background-color: ${(props) => (props.$isInSpecialView ? 'transparent' : 'var(--color-background-mute)')};
@@ -292,7 +294,6 @@ const SplitViewWrapper = styled.div`
&:not(:has(+ [class*='Container'])) {
border-radius: 0 0 8px 8px;
overflow: hidden;
}
`
export default memo(CodeBlockView)
+94 -28
View File
@@ -12,45 +12,111 @@ const linterLoaders: Record<string, () => Promise<any>> = {
}
}
/**
*
*/
const specialLanguageLoaders: Record<string, () => Promise<Extension>> = {
dot: async () => {
const mod = await import('@viz-js/lang-dot')
return mod.dot()
}
}
/**
*
*/
async function loadLanguageExtension(language: string, languageMap: Record<string, string>): Promise<Extension | null> {
let normalizedLang = languageMap[language as keyof typeof languageMap] || language.toLowerCase()
// 如果语言名包含 `-`,转换为驼峰命名法
if (normalizedLang.includes('-')) {
normalizedLang = normalizedLang.replace(/-([a-z])/g, (_, char) => char.toUpperCase())
}
// 尝试加载特殊语言
const specialLoader = specialLanguageLoaders[normalizedLang]
if (specialLoader) {
try {
return await specialLoader()
} catch (error) {
console.debug(`Failed to load language ${normalizedLang}`, error)
return null
}
}
// 回退到 uiw/codemirror 包含的语言
try {
const { loadLanguage } = await import('@uiw/codemirror-extensions-langs')
const extension = loadLanguage(normalizedLang as any)
return extension || null
} catch (error) {
console.debug(`Failed to load language ${normalizedLang}`, error)
return null
}
}
/**
* linter
*/
async function loadLinterExtension(language: string): Promise<Extension | null> {
const loader = linterLoaders[language]
if (!loader) return null
try {
return await loader()
} catch (error) {
console.debug(`Failed to load linter for ${language}`, error)
return null
}
}
/**
*
*/
export const useLanguageExtensions = (language: string, lint?: boolean) => {
const { languageMap } = useCodeStyle()
const [extensions, setExtensions] = useState<Extension[]>([])
// 加载语言
useEffect(() => {
let normalizedLang = languageMap[language as keyof typeof languageMap] || language.toLowerCase()
let cancelled = false
// 如果语言名包含 `-`,转换为驼峰命名法
if (normalizedLang.includes('-')) {
normalizedLang = normalizedLang.replace(/-([a-z])/g, (_, char) => char.toUpperCase())
}
const loadAllExtensions = async () => {
try {
// 加载所有扩展
const [languageResult, linterResult] = await Promise.allSettled([
loadLanguageExtension(language, languageMap),
lint ? loadLinterExtension(language) : Promise.resolve(null)
])
import('@uiw/codemirror-extensions-langs')
.then(({ loadLanguage }) => {
const extension = loadLanguage(normalizedLang as any)
if (extension) {
setExtensions((prev) => [...prev, extension])
if (cancelled) return
const results: Extension[] = []
// 语言扩展
if (languageResult.status === 'fulfilled' && languageResult.value) {
results.push(languageResult.value)
}
})
.catch((error) => {
console.debug(`Failed to load language: ${normalizedLang}`, error)
})
}, [language, languageMap])
useEffect(() => {
if (!lint) return
// linter 扩展
if (linterResult.status === 'fulfilled' && linterResult.value) {
results.push(linterResult.value)
}
const loader = linterLoaders[language]
if (loader) {
loader()
.then((extension) => {
setExtensions((prev) => [...prev, extension])
})
.catch((error) => {
console.error(`Failed to load linter for ${language}`, error)
})
setExtensions(results)
} catch (error) {
if (!cancelled) {
console.debug('Failed to load language extensions:', error)
setExtensions([])
}
}
}
}, [language, lint])
loadAllExtensions()
return () => {
cancelled = true
}
}, [language, lint, languageMap])
return extensions
}
@@ -42,6 +42,7 @@ interface Props {
extensions?: Extension[]
/** 用于覆写编辑器的样式,会直接传给 CodeMirror 的 style 属性 */
style?: React.CSSProperties
editable?: boolean
}
/**
@@ -62,7 +63,8 @@ const CodeEditor = ({
maxHeight,
options,
extensions,
style
style,
editable = true
}: Props) => {
const {
fontSize,
@@ -190,7 +192,7 @@ const CodeEditor = ({
height={height}
minHeight={minHeight}
maxHeight={collapsible && !isExpanded ? (maxHeight ?? '350px') : 'none'}
editable={true}
editable={editable}
// @ts-ignore 强制使用,见 react-codemirror 的 Example.tsx
theme={activeCmTheme}
extensions={customExtensions}
+23 -28
View File
@@ -140,7 +140,7 @@ export const ContentSearch = React.forwardRef<ContentSearchRef, Props>(
const [isCaseSensitive, setIsCaseSensitive] = useState(false)
const [isWholeWord, setIsWholeWord] = useState(false)
const [allRanges, setAllRanges] = useState<Range[]>([])
const [currentIndex, setCurrentIndex] = useState(0)
const [currentIndex, setCurrentIndex] = useState(-1)
const prevSearchText = useRef('')
const { t } = useTranslation()
@@ -182,15 +182,18 @@ export const ContentSearch = React.forwardRef<ContentSearchRef, Props>(
[allRanges, currentIndex]
)
const search = useCallback(() => {
const searchText = searchInputRef.current?.value.trim() ?? null
setSearchCompleted(SearchCompletedState.Searched)
if (target && searchText !== null && searchText !== '') {
const ranges = findRangesInTarget(target, filter, searchText, isCaseSensitive, isWholeWord)
setAllRanges(ranges)
setCurrentIndex(0)
}
}, [target, filter, isCaseSensitive, isWholeWord])
const search = useCallback(
(jump = false) => {
const searchText = searchInputRef.current?.value.trim() ?? null
setSearchCompleted(SearchCompletedState.Searched)
if (target && searchText !== null && searchText !== '') {
const ranges = findRangesInTarget(target, filter, searchText, isCaseSensitive, isWholeWord)
setAllRanges(ranges)
setCurrentIndex(jump && ranges.length > 0 ? 0 : -1)
}
},
[target, filter, isCaseSensitive, isWholeWord]
)
const implementation = useMemo(
() => ({
@@ -207,7 +210,7 @@ export const ContentSearch = React.forwardRef<ContentSearchRef, Props>(
requestAnimationFrame(() => {
inputEl.focus()
inputEl.select()
search()
search(false)
})
} else {
requestAnimationFrame(() => {
@@ -231,11 +234,11 @@ export const ContentSearch = React.forwardRef<ContentSearchRef, Props>(
setSearchCompleted(SearchCompletedState.NotSearched)
},
search: () => {
search()
search(true)
locateByIndex(true)
},
silentSearch: () => {
search()
search(false)
locateByIndex(false)
},
focus: () => {
@@ -302,7 +305,7 @@ export const ContentSearch = React.forwardRef<ContentSearchRef, Props>(
useEffect(() => {
if (enableContentSearch && searchInputRef.current?.value.trim()) {
search()
search(true)
}
}, [isCaseSensitive, isWholeWord, enableContentSearch, search])
@@ -365,16 +368,12 @@ export const ContentSearch = React.forwardRef<ContentSearchRef, Props>(
</InputWrapper>
<Separator></Separator>
<SearchResults>
{searchCompleted !== SearchCompletedState.NotSearched ? (
allRanges.length > 0 ? (
<>
<SearchResultCount>{currentIndex + 1}</SearchResultCount>
<SearchResultSeparator>/</SearchResultSeparator>
<SearchResultTotalCount>{allRanges.length}</SearchResultTotalCount>
</>
) : (
<NoResults>{t('common.no_results')}</NoResults>
)
{searchCompleted !== SearchCompletedState.NotSearched && allRanges.length > 0 ? (
<>
<SearchResultCount>{currentIndex + 1}</SearchResultCount>
<SearchResultSeparator>/</SearchResultSeparator>
<SearchResultTotalCount>{allRanges.length}</SearchResultTotalCount>
</>
) : (
<SearchResultsPlaceholder>0/0</SearchResultsPlaceholder>
)}
@@ -477,10 +476,6 @@ const SearchResultsPlaceholder = styled.span`
opacity: 0.5;
`
const NoResults = styled.span`
color: var(--color-text-1);
`
const SearchResultCount = styled.span`
color: var(--color-text);
`
@@ -0,0 +1,184 @@
import { getTopicByMessageId } from '@renderer/hooks/useMessageOperations'
import Markdown from '@renderer/pages/home/Markdown/Markdown'
import { useAppDispatch } from '@renderer/store'
import { retryDeepResearchClarificationThunk } from '@renderer/store/thunk/messageThunk'
import { DeepResearchMessageBlock, MessageBlockStatus } from '@renderer/types/newMessage'
import { deepResearchConfirmation } from '@renderer/utils/deepResearchConfirmation'
import { Button, Input } from 'antd'
import { Brain, RotateCcw } from 'lucide-react'
import { FC, useState } from 'react'
import { useTranslation } from 'react-i18next'
import styled from 'styled-components'
import SvgSpinners180Ring from './Icons/SvgSpinners180Ring'
const { TextArea } = Input
interface DeepResearchCardProps {
block: DeepResearchMessageBlock
}
const DeepResearchCard: FC<DeepResearchCardProps> = ({ block }) => {
const { t } = useTranslation()
const dispatch = useAppDispatch()
const [isRetrying, setIsRetrying] = useState(false)
const [userSupplementInfo, setUserSupplementInfo] = useState('')
const {
metadata: { deepResearchState }
} = block
const isWaitingForContinue = deepResearchState.phase === 'waiting_confirmation'
const onContinueResearch = () => {
try {
const success = deepResearchConfirmation.triggerResolver(block.id, userSupplementInfo)
if (!success) {
console.error('[continueDeepResearchThunk] No continue resolver found for message', block.id)
return
}
// resolver会在fetchDeepResearch的onResearchStarted中处理后续的研究阶段逻辑
} catch (error) {
console.error('[continueDeepResearchThunk] Error:', error)
}
}
const onRetryResearch = async () => {
try {
setIsRetrying(true)
const topic = await getTopicByMessageId(block.messageId)
if (!topic) {
console.error('[onRetryResearch] Topic not found for message', block.messageId)
return
}
// 重试时清空补全信息
setUserSupplementInfo('')
dispatch(retryDeepResearchClarificationThunk(topic.id, block.messageId))
} catch (error) {
console.error('[onRetryResearch] Error:', error)
} finally {
setIsRetrying(false)
}
}
return (
<>
{block.status === MessageBlockStatus.PENDING ? (
<SvgSpinners180Ring color="var(--color-text-2)" style={{ marginBottom: 15 }} />
) : (
<CardContainer>
<ClarificationSection>
<SectionTitle>
<Brain size={16} />
{t('research.clarification.title')}
</SectionTitle>
{block.content ? (
<Markdown block={block} />
) : deepResearchState.phase === 'clarification' && block.status === MessageBlockStatus.STREAMING ? (
<SvgSpinners180Ring color="var(--color-text-2)" style={{ marginBottom: 15 }} />
) : null}
</ClarificationSection>
{isWaitingForContinue && (
<ActionSection>
<ActionTitle>{t('research.ready_to_start')}</ActionTitle>
<SupplementSection>
<SupplementLabel>{t('research.supplement_info_label')}</SupplementLabel>
<StyledTextArea
value={userSupplementInfo}
onChange={(e) => setUserSupplementInfo(e.target.value)}
placeholder={t('research.supplement_info_placeholder')}
rows={3}
maxLength={500}
/>
</SupplementSection>
<ButtonGroup>
<RetryButton
type="default"
icon={<RotateCcw size={16} />}
onClick={onRetryResearch}
loading={isRetrying}
disabled={isRetrying}>
{t('research.retry')}
</RetryButton>
<ContinueButton type="primary" icon={<Brain size={16} />} onClick={onContinueResearch}>
{t('research.continue_research')}
</ContinueButton>
</ButtonGroup>
</ActionSection>
)}
</CardContainer>
)}
</>
)
}
const CardContainer = styled.div`
border: 1px solid var(--color-border);
border-radius: 8px;
background: var(--color-background);
margin: 12px 0;
overflow: hidden;
`
const ClarificationSection = styled.div`
padding: 16px;
border-bottom: 1px solid var(--color-border-soft);
`
const SectionTitle = styled.div`
display: flex;
align-items: center;
gap: 8px;
font-size: 14px;
font-weight: 500;
color: var(--color-text);
margin-bottom: 12px;
`
const ActionSection = styled.div`
padding: 16px;
background: var(--color-background-soft);
`
const ActionTitle = styled.div`
font-size: 14px;
font-weight: 500;
color: var(--color-text);
margin-bottom: 12px;
`
const ButtonGroup = styled.div`
display: flex;
gap: 8px;
`
const RetryButton = styled(Button)`
display: flex;
align-items: center;
gap: 4px;
`
const ContinueButton = styled(Button)`
display: flex;
align-items: center;
gap: 4px;
`
const SupplementSection = styled.div`
margin-bottom: 12px;
`
const SupplementLabel = styled.div`
font-size: 14px;
font-weight: 500;
color: var(--color-text);
margin-bottom: 8px;
`
const StyledTextArea = styled(TextArea)`
width: 100%;
`
export default DeepResearchCard
@@ -82,7 +82,7 @@ function DraggableVirtualList<T>({
const parentRef = useRef<HTMLDivElement>(null)
const virtualizer = useVirtualizer({
count: list.length,
count: list?.length ?? 0,
getScrollElement: useCallback(() => parentRef.current, []),
getItemKey: itemKey,
estimateSize: useCallback(() => 50, []),
@@ -1,5 +1,5 @@
import { DeleteOutlined, ExclamationCircleOutlined, ReloadOutlined } from '@ant-design/icons'
import { restoreFromLocalBackup } from '@renderer/services/BackupService'
import { restoreFromLocal } from '@renderer/services/BackupService'
import { formatFileSize } from '@renderer/utils'
import { Button, message, Modal, Table, Tooltip } from 'antd'
import dayjs from 'dayjs'
@@ -46,7 +46,7 @@ export function LocalBackupManager({ visible, onClose, localBackupDir, restoreMe
total: files.length
}))
} catch (error: any) {
message.error(`${t('settings.data.local.backup.manager.fetch.error')}: ${error.message}`)
window.message.error(`${t('settings.data.local.backup.manager.fetch.error')}: ${error.message}`)
} finally {
setLoading(false)
}
@@ -91,13 +91,13 @@ export function LocalBackupManager({ visible, onClose, localBackupDir, restoreMe
for (const key of selectedRowKeys) {
await window.api.backup.deleteLocalBackupFile(key.toString(), localBackupDir)
}
message.success(
window.message.success(
t('settings.data.local.backup.manager.delete.success.multiple', { count: selectedRowKeys.length })
)
setSelectedRowKeys([])
await fetchBackupFiles()
} catch (error: any) {
message.error(`${t('settings.data.local.backup.manager.delete.error')}: ${error.message}`)
window.message.error(`${t('settings.data.local.backup.manager.delete.error')}: ${error.message}`)
} finally {
setDeleting(false)
}
@@ -124,7 +124,7 @@ export function LocalBackupManager({ visible, onClose, localBackupDir, restoreMe
message.success(t('settings.data.local.backup.manager.delete.success.single'))
await fetchBackupFiles()
} catch (error: any) {
message.error(`${t('settings.data.local.backup.manager.delete.error')}: ${error.message}`)
window.message.error(`${t('settings.data.local.backup.manager.delete.error')}: ${error.message}`)
} finally {
setDeleting(false)
}
@@ -147,11 +147,11 @@ export function LocalBackupManager({ visible, onClose, localBackupDir, restoreMe
onOk: async () => {
setRestoring(true)
try {
await (restoreMethod || restoreFromLocalBackup)(fileName)
await (restoreMethod || restoreFromLocal)(fileName)
message.success(t('settings.data.local.backup.manager.restore.success'))
onClose() // Close the modal
} catch (error: any) {
message.error(`${t('settings.data.local.backup.manager.restore.error')}: ${error.message}`)
window.message.error(`${t('settings.data.local.backup.manager.restore.error')}: ${error.message}`)
} finally {
setRestoring(false)
}
@@ -1,4 +1,4 @@
import { backupToLocalDir } from '@renderer/services/BackupService'
import { backupToLocal } from '@renderer/services/BackupService'
import { Button, Input, Modal } from 'antd'
import dayjs from 'dayjs'
import { useCallback, useState } from 'react'
@@ -74,9 +74,9 @@ export function useLocalBackupModal(localBackupDir: string | undefined) {
setBackuping(true)
try {
await backupToLocalDir({
await backupToLocal({
showMessage: true,
customFileName
customFileName: customFileName || undefined
})
setIsModalVisible(false)
} catch (error) {
@@ -0,0 +1,17 @@
import { MAX_CONTEXT_COUNT } from '@renderer/config/constant'
import { Infinity as InfinityIcon } from 'lucide-react'
import { CSSProperties } from 'react'
type Props = {
maxContext: number
style?: CSSProperties
size?: number
}
export default function MaxContextCount({ maxContext, style, size = 14 }: Props) {
return maxContext === MAX_CONTEXT_COUNT ? (
<InfinityIcon size={size} style={style} aria-label="infinity" />
) : (
<span style={style}>{maxContext.toString()}</span>
)
}
@@ -22,7 +22,7 @@ import { useAppDispatch } from '@renderer/store'
import { setMinappsOpenLinkExternal } from '@renderer/store/settings'
import { MinAppType } from '@renderer/types'
import { delay } from '@renderer/utils'
import { Avatar, Drawer, Tooltip } from 'antd'
import { Alert, Avatar, Button, Drawer, Tooltip } from 'antd'
import { WebviewTag } from 'electron'
import { useEffect, useMemo, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
@@ -39,6 +39,100 @@ interface AppExtraInfo {
type AppInfo = MinAppType & AppExtraInfo
/** Google login tip component */
const GoogleLoginTip = ({
isReady,
currentUrl,
currentAppId
}: {
appId?: string | null
isReady: boolean
currentUrl: string | null
currentAppId: string | null
}) => {
const { t } = useTranslation()
const [visible, setVisible] = useState(false)
const { openMinappById } = useMinappPopup()
// 判断当前URL是否涉及Google登录
const needsGoogleLogin = useMemo(() => {
// 如果当前已经在Google小程序中,不需要显示提示
if (currentAppId === 'google') return false
if (!currentUrl) return false
const googleLoginPatterns = [
'accounts.google.com',
'signin/oauth',
'auth/google',
'login/google',
'sign-in/google',
'google.com/signin',
'gsi/client'
]
return googleLoginPatterns.some((pattern) => currentUrl.toLowerCase().includes(pattern.toLowerCase()))
}, [currentUrl, currentAppId])
// 在URL更新时检查是否需要显示提示
useEffect(() => {
let showTimer: NodeJS.Timeout | null = null
let hideTimer: NodeJS.Timeout | null = null
// 如果是Google登录相关URL且小程序已加载完成,则延迟显示提示
if (needsGoogleLogin && isReady) {
showTimer = setTimeout(() => {
setVisible(true)
hideTimer = setTimeout(() => {
setVisible(false)
}, 30000)
}, 500)
} else {
setVisible(false)
}
return () => {
if (showTimer) clearTimeout(showTimer)
if (hideTimer) clearTimeout(hideTimer)
}
}, [needsGoogleLogin, isReady, currentUrl])
// 处理关闭提示
const handleClose = () => {
setVisible(false)
}
// 跳转到Google小程序
const openGoogleMinApp = () => {
// 使用openMinappById方法打开Google小程序
openMinappById('google', true)
// 关闭提示
setVisible(false)
}
// 只在需要Google登录时显示提示
if (!needsGoogleLogin || !visible) return null
// 使用直接的消息文本
const message = t('miniwindow.alert.google_login')
return (
<Alert
message={message}
type="warning"
showIcon
closable
onClose={handleClose}
action={
<Button type="primary" size="small" onClick={openGoogleMinApp}>
{t('common.open')} Google
</Button>
}
style={{ zIndex: 10, animation: 'fadeIn 0.3s ease-in-out' }}
/>
)
}
/** The main container for MinApp popup */
const MinappPopupContainer: React.FC = () => {
const { openedKeepAliveMinapps, openedOneOffMinapp, currentMinappId, minappShow } = useRuntime()
@@ -198,9 +292,11 @@ const MinappPopupContainer: React.FC = () => {
}
}
/** the callback function to handle the webview navigate to new url */
/** the callback function to handle webview navigation */
const handleWebviewNavigate = (appid: string, url: string) => {
// 记录当前URL,用于GoogleLoginTip判断
if (appid === currentMinappId) {
console.log('URL changed:', url)
setCurrentUrl(url)
}
}
@@ -297,36 +393,36 @@ const MinappPopupContainer: React.FC = () => {
</Tooltip>
{appInfo.canOpenExternalLink && (
<Tooltip title={t('minapp.popup.openExternal')} mouseEnterDelay={0.8} placement="bottom">
<Button onClick={() => handleOpenLink(url ?? appInfo.url)}>
<TitleButton onClick={() => handleOpenLink(url ?? appInfo.url)}>
<ExportOutlined />
</Button>
</TitleButton>
</Tooltip>
)}
<Spacer />
<ButtonsGroup className={isWin || isLinux ? 'windows' : ''}>
<Tooltip title={t('minapp.popup.goBack')} mouseEnterDelay={0.8} placement="bottom">
<Button onClick={() => handleGoBack(appInfo.id)}>
<TitleButton onClick={() => handleGoBack(appInfo.id)}>
<ArrowLeftOutlined />
</Button>
</TitleButton>
</Tooltip>
<Tooltip title={t('minapp.popup.goForward')} mouseEnterDelay={0.8} placement="bottom">
<Button onClick={() => handleGoForward(appInfo.id)}>
<TitleButton onClick={() => handleGoForward(appInfo.id)}>
<ArrowRightOutlined />
</Button>
</TitleButton>
</Tooltip>
<Tooltip title={t('minapp.popup.refresh')} mouseEnterDelay={0.8} placement="bottom">
<Button onClick={() => handleReload(appInfo.id)}>
<TitleButton onClick={() => handleReload(appInfo.id)}>
<ReloadOutlined />
</Button>
</TitleButton>
</Tooltip>
{appInfo.canPinned && (
<Tooltip
title={appInfo.isPinned ? t('minapp.sidebar.remove.title') : t('minapp.sidebar.add.title')}
mouseEnterDelay={0.8}
placement="bottom">
<Button onClick={() => handleTogglePin(appInfo.id)} className={appInfo.isPinned ? 'pinned' : ''}>
<TitleButton onClick={() => handleTogglePin(appInfo.id)} className={appInfo.isPinned ? 'pinned' : ''}>
<PushpinOutlined style={{ fontSize: 16 }} />
</Button>
</TitleButton>
</Tooltip>
)}
<Tooltip
@@ -337,28 +433,28 @@ const MinappPopupContainer: React.FC = () => {
}
mouseEnterDelay={0.8}
placement="bottom">
<Button onClick={handleToggleOpenExternal} className={minappsOpenLinkExternal ? 'open-external' : ''}>
<TitleButton onClick={handleToggleOpenExternal} className={minappsOpenLinkExternal ? 'open-external' : ''}>
<LinkOutlined />
</Button>
</TitleButton>
</Tooltip>
{isInDevelopment && (
<Tooltip title={t('minapp.popup.devtools')} mouseEnterDelay={0.8} placement="bottom">
<Button onClick={() => handleOpenDevTools(appInfo.id)}>
<TitleButton onClick={() => handleOpenDevTools(appInfo.id)}>
<CodeOutlined />
</Button>
</TitleButton>
</Tooltip>
)}
{canMinimize && (
<Tooltip title={t('minapp.popup.minimize')} mouseEnterDelay={0.8} placement="bottom">
<Button onClick={() => handlePopupMinimize()}>
<TitleButton onClick={() => handlePopupMinimize()}>
<MinusOutlined />
</Button>
</TitleButton>
</Tooltip>
)}
<Tooltip title={t('minapp.popup.close')} mouseEnterDelay={0.8} placement="bottom">
<Button onClick={() => handlePopupClose(appInfo.id)}>
<TitleButton onClick={() => handlePopupClose(appInfo.id)}>
<CloseOutlined />
</Button>
</TitleButton>
</Tooltip>
</ButtonsGroup>
</TitleContainer>
@@ -399,6 +495,8 @@ const MinappPopupContainer: React.FC = () => {
marginLeft: 'var(--sidebar-width)',
backgroundColor: window.root.style.background
}}>
{/* 在所有小程序中显示GoogleLoginTip */}
<GoogleLoginTip isReady={isReady} currentUrl={currentUrl} currentAppId={currentMinappId} />
{!isReady && (
<EmptyView>
<Avatar
@@ -460,7 +558,7 @@ const ButtonsGroup = styled.div`
}
`
const Button = styled.div`
const TitleButton = styled.div`
cursor: pointer;
width: 30px;
height: 30px;
@@ -10,6 +10,7 @@ import { Button, Card, Flex, List, Popconfirm, Space, Tooltip, Typography } from
import { Trash } from 'lucide-react'
import { FC, useState } from 'react'
import { useTranslation } from 'react-i18next'
import styled from 'styled-components'
import { isLlmProvider, useApiKeys } from './hook'
import ApiKeyItem from './item'
@@ -87,7 +88,7 @@ export const ApiKeyList: FC<ApiKeyListProps> = ({ provider, updateProvider, prov
: keys
return (
<>
<ListContainer>
{/* Keys 列表 */}
<Card
size="small"
@@ -122,7 +123,7 @@ export const ApiKeyList: FC<ApiKeyListProps> = ({ provider, updateProvider, prov
)}
</Card>
<Flex align="center" justify="space-between" style={{ marginTop: '0.5rem' }}>
<Flex dir="row" align="center" justify="space-between" style={{ marginTop: 15 }}>
{/* 帮助文本 */}
<SettingHelpText>{t('settings.provider.api_key.tip')}</SettingHelpText>
@@ -166,7 +167,7 @@ export const ApiKeyList: FC<ApiKeyListProps> = ({ provider, updateProvider, prov
</Button>
</Space>
</Flex>
</>
</ListContainer>
)
}
@@ -222,3 +223,8 @@ export const DocPreprocessApiKeyList: FC<SpecificApiKeyListProps> = ({
/>
)
}
const ListContainer = styled.div`
padding-top: 15px;
padding-bottom: 15px;
`
@@ -54,7 +54,7 @@ const FloatingSidebar: FC<Props> = ({
style={{
background: 'transparent',
border: 'none',
maxHeight: maxHeight
height: '100%'
}}
/>
</PopoverContent>
@@ -82,6 +82,9 @@ const FloatingSidebar: FC<Props> = ({
const PopoverContent = styled.div<{ maxHeight: number }>`
max-height: ${(props) => props.maxHeight}px;
&.ant-popover-inner-content {
overflow-y: hidden;
}
`
export default FloatingSidebar
@@ -1,82 +0,0 @@
import { Center } from '@renderer/components/Layout'
import { useMinapps } from '@renderer/hooks/useMinapps'
import App from '@renderer/pages/apps/App'
import { Popover } from 'antd'
import { Empty } from 'antd'
import { isEmpty } from 'lodash'
import { FC, useEffect, useState } from 'react'
import { useHotkeys } from 'react-hotkeys-hook'
import styled from 'styled-components'
import Scrollbar from '../Scrollbar'
interface Props {
children: React.ReactNode
}
const MinAppsPopover: FC<Props> = ({ children }) => {
const [open, setOpen] = useState(false)
const { minapps } = useMinapps()
useHotkeys('esc', () => {
setOpen(false)
})
const handleClose = () => {
setOpen(false)
}
const [maxHeight, setMaxHeight] = useState(window.innerHeight - 100)
useEffect(() => {
const handleResize = () => {
setMaxHeight(window.innerHeight - 100)
}
window.addEventListener('resize', handleResize)
return () => {
window.removeEventListener('resize', handleResize)
}
}, [])
const content = (
<PopoverContent maxHeight={maxHeight}>
<AppsContainer>
{minapps.map((app) => (
<App key={app.id} app={app} onClick={handleClose} size={50} />
))}
{isEmpty(minapps) && (
<Center>
<Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
</Center>
)}
</AppsContainer>
</PopoverContent>
)
return (
<Popover
open={open}
onOpenChange={setOpen}
content={content}
trigger="click"
placement="bottomRight"
styles={{ body: { padding: 25 } }}>
{children}
</Popover>
)
}
const PopoverContent = styled(Scrollbar)<{ maxHeight: number }>`
max-height: ${(props) => props.maxHeight}px;
overflow-y: auto;
`
const AppsContainer = styled.div`
display: grid;
grid-template-columns: repeat(8, minmax(90px, 1fr));
gap: 18px;
`
export default MinAppsPopover
@@ -0,0 +1,353 @@
import CustomTag from '@renderer/components/CustomTag'
import { TopView } from '@renderer/components/TopView'
import Logger from '@renderer/config/logger'
import { useKnowledge, useKnowledgeBases } from '@renderer/hooks/useKnowledge'
import { Message } from '@renderer/types/newMessage'
import {
analyzeMessageContent,
CONTENT_TYPES,
ContentType,
MessageContentStats,
processMessageContent
} from '@renderer/utils/knowledge'
import { Flex, Form, Modal, Select, Tooltip, Typography } from 'antd'
import { Check, CircleHelp } from 'lucide-react'
import { useEffect, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import styled from 'styled-components'
const { Text } = Typography
// 内容类型配置
const CONTENT_TYPE_CONFIG = {
[CONTENT_TYPES.TEXT]: {
label: 'chat.save.knowledge.content.maintext.title',
description: 'chat.save.knowledge.content.maintext.description'
},
[CONTENT_TYPES.CODE]: {
label: 'chat.save.knowledge.content.code.title',
description: 'chat.save.knowledge.content.code.description'
},
[CONTENT_TYPES.THINKING]: {
label: 'chat.save.knowledge.content.thinking.title',
description: 'chat.save.knowledge.content.thinking.description'
},
[CONTENT_TYPES.TOOL_USE]: {
label: 'chat.save.knowledge.content.tool_use.title',
description: 'chat.save.knowledge.content.tool_use.description'
},
[CONTENT_TYPES.CITATION]: {
label: 'chat.save.knowledge.content.citation.title',
description: 'chat.save.knowledge.content.citation.description'
},
[CONTENT_TYPES.TRANSLATION]: {
label: 'chat.save.knowledge.content.translation.title',
description: 'chat.save.knowledge.content.translation.description'
},
[CONTENT_TYPES.ERROR]: {
label: 'chat.save.knowledge.content.error.title',
description: 'chat.save.knowledge.content.error.description'
},
[CONTENT_TYPES.FILE]: {
label: 'chat.save.knowledge.content.file.title',
description: 'chat.save.knowledge.content.file.description'
}
} as const
// Tag 颜色常量
const TAG_COLORS = {
SELECTED: '#008001',
UNSELECTED: '#8c8c8c'
} as const
interface ContentTypeOption {
type: ContentType
label: string
count: number
enabled: boolean
description?: string
}
interface ShowParams {
message: Message
title?: string
}
interface SaveResult {
success: boolean
savedCount: number
}
interface Props extends ShowParams {
resolve: (data: SaveResult | null) => void
}
const PopupContainer: React.FC<Props> = ({ message, title, resolve }) => {
const [open, setOpen] = useState(true)
const [loading, setLoading] = useState(false)
const [selectedBaseId, setSelectedBaseId] = useState<string>()
const [selectedTypes, setSelectedTypes] = useState<ContentType[]>([])
const [hasInitialized, setHasInitialized] = useState(false)
const { bases } = useKnowledgeBases()
const { addNote, addFiles } = useKnowledge(selectedBaseId || '')
const { t } = useTranslation()
// 分析消息内容统计
const contentStats = useMemo(() => analyzeMessageContent(message), [message])
// 生成内容类型选项(只显示有内容的类型)
const contentTypeOptions: ContentTypeOption[] = useMemo(() => {
return Object.entries(CONTENT_TYPE_CONFIG)
.map(([type, config]) => {
const contentType = type as ContentType
const count = contentStats[contentType as keyof MessageContentStats] || 0
return {
type: contentType,
count,
enabled: count > 0,
label: t(config.label),
description: t(config.description)
}
})
.filter((option) => option.enabled) // 只显示有内容的类型
}, [contentStats, t])
// 知识库选项
const knowledgeBaseOptions = useMemo(
() =>
bases.map((base) => ({
label: base.name,
value: base.id,
disabled: !base.version // 如果知识库没有配置好就禁用
})),
[bases]
)
// 合并状态计算
const formState = useMemo(() => {
const hasValidBase = selectedBaseId && bases.find((base) => base.id === selectedBaseId)?.version
const hasContent = contentTypeOptions.length > 0
const selectedCount = contentTypeOptions
.filter((option) => selectedTypes.includes(option.type))
.reduce((sum, option) => sum + option.count, 0)
return {
hasValidBase,
hasContent,
canSubmit: hasValidBase && selectedTypes.length > 0 && hasContent,
selectedCount,
hasNoSelection: selectedTypes.length === 0 && hasContent
}
}, [selectedBaseId, bases, contentTypeOptions, selectedTypes])
// 默认选择第一个可用的知识库
useEffect(() => {
if (!selectedBaseId) {
const firstAvailableBase = bases.find((base) => base.version)
if (firstAvailableBase) {
setSelectedBaseId(firstAvailableBase.id)
}
}
}, [bases, selectedBaseId])
// 默认选择所有可用的内容类型(仅在初始化时)
useEffect(() => {
if (!hasInitialized && contentTypeOptions.length > 0) {
const availableTypes = contentTypeOptions.map((option) => option.type)
setSelectedTypes(availableTypes)
setHasInitialized(true)
}
}, [contentTypeOptions, hasInitialized])
// 计算UI状态
const uiState = useMemo(() => {
if (!formState.hasContent) {
return { type: 'empty', message: t('chat.save.knowledge.empty.no_content') }
}
if (bases.length === 0) {
return { type: 'empty', message: t('chat.save.knowledge.empty.no_knowledge_base') }
}
return { type: 'form' }
}, [formState.hasContent, bases.length, t])
// 处理内容类型选择切换
const handleContentTypeToggle = (type: ContentType) => {
setSelectedTypes((prev) => (prev.includes(type) ? prev.filter((t) => t !== type) : [...prev, type]))
}
const onOk = async () => {
if (!formState.canSubmit) {
return
}
setLoading(true)
let savedCount = 0
try {
const result = processMessageContent(message, selectedTypes)
// 保存文本内容
if (result.text.trim() && selectedTypes.some((type) => type !== CONTENT_TYPES.FILE)) {
await addNote(result.text)
savedCount++
}
// 保存文件
if (result.files.length > 0 && selectedTypes.includes(CONTENT_TYPES.FILE)) {
addFiles(result.files)
savedCount += result.files.length
}
setOpen(false)
resolve({ success: true, savedCount })
} catch (error) {
Logger.error('[SaveToKnowledgePopup] save failed:', error)
window.message.error(t('chat.save.knowledge.error.save_failed'))
setLoading(false)
}
}
const onCancel = () => {
setOpen(false)
}
const onClose = () => {
resolve(null)
}
// 渲染空状态
const renderEmptyState = () => (
<EmptyContainer>
<Text type="secondary">{uiState.message}</Text>
</EmptyContainer>
)
// 渲染表单内容
const renderFormContent = () => (
<>
<Form layout="vertical">
<Form.Item
label={t('chat.save.knowledge.select.base.title')}
help={!formState.hasValidBase && selectedBaseId ? t('chat.save.knowledge.error.invalid_base') : undefined}
validateStatus={!formState.hasValidBase && selectedBaseId ? 'error' : undefined}>
<Select
value={selectedBaseId}
onChange={setSelectedBaseId}
options={knowledgeBaseOptions}
placeholder={t('chat.save.knowledge.select.base.placeholder')}
showSearch
/>
</Form.Item>
<Form.Item label={t('chat.save.knowledge.select.content.title')}>
<Flex gap={8} style={{ flexDirection: 'column' }}>
{contentTypeOptions.map((option) => (
<ContentTypeItem
key={option.type}
align="center"
justify="space-between"
onClick={() => handleContentTypeToggle(option.type)}>
<Flex align="center" gap={8}>
<CustomTag
color={selectedTypes.includes(option.type) ? TAG_COLORS.SELECTED : TAG_COLORS.UNSELECTED}
size={12}>
{option.count}
</CustomTag>
<span>{option.label}</span>
<Tooltip title={option.description} mouseLeaveDelay={0}>
<CircleHelp size={16} style={{ cursor: 'help' }} />
</Tooltip>
</Flex>
{selectedTypes.includes(option.type) && <Check size={16} color={TAG_COLORS.SELECTED} />}
</ContentTypeItem>
))}
</Flex>
</Form.Item>
</Form>
{formState.selectedCount > 0 && (
<InfoContainer>
<Text type="secondary" style={{ fontSize: '12px' }}>
{t('chat.save.knowledge.select.content.tip', { count: formState.selectedCount })}
</Text>
</InfoContainer>
)}
{formState.hasNoSelection && (
<InfoContainer>
<Text type="warning" style={{ fontSize: '12px' }}>
{t('chat.save.knowledge.error.no_content_selected')}
</Text>
</InfoContainer>
)}
</>
)
return (
<Modal
title={title || t('chat.save.knowledge.title')}
open={open}
onOk={onOk}
onCancel={onCancel}
afterClose={onClose}
destroyOnClose
centered
width={500}
okText={t('common.save')}
cancelText={t('common.cancel')}
okButtonProps={{
loading,
disabled: !formState.canSubmit
}}>
{uiState.type === 'empty' ? renderEmptyState() : renderFormContent()}
</Modal>
)
}
const TopViewKey = 'SaveToKnowledgePopup'
export default class SaveToKnowledgePopup {
static hide() {
TopView.hide(TopViewKey)
}
static show(props: ShowParams): Promise<SaveResult | null> {
return new Promise<SaveResult | null>((resolve) => {
TopView.show(
<PopupContainer
{...props}
resolve={(result) => {
resolve(result)
this.hide()
}}
/>,
TopViewKey
)
})
}
}
const EmptyContainer = styled.div`
text-align: center;
padding: 40px 20px;
`
const ContentTypeItem = styled(Flex)`
padding: 12px;
border: 1px solid var(--color-border);
border-radius: 6px;
cursor: pointer;
transition: border-color 0.2s;
position: relative;
&:hover {
border-color: var(--color-primary);
}
`
const InfoContainer = styled.div`
background: var(--color-background-soft);
padding: 12px;
border-radius: 6px;
margin-top: 16px;
`

Some files were not shown because too many files have changed in this diff Show More