From 4a62bb6ad71614d3553511992cf3bed3984cda26 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Fri, 15 Aug 2025 22:48:22 +0800 Subject: [PATCH 1/4] refactor: replace axios and node fetch with electron's net module (#9212) * refactor: replace axios and node fetch with electron's net module for network requests in preprocess providers - Updated Doc2xPreprocessProvider and MineruPreprocessProvider to use net.fetch instead of axios for making HTTP requests. - Improved error handling for network responses across various methods. - Removed unnecessary AxiosRequestConfig and related code to streamline the implementation. * lint * refactor(Doc2xPreprocessProvider): enhance file validation and upload process - Added file size validation to prevent loading files larger than 300MB into memory. - Implemented file size check before reading the PDF to ensure efficient memory usage. - Updated the file upload method to use a stream, setting the 'Content-Length' header for better handling of large files. * refactor(brave-search): update net.fetch calls to use url.toString() - Modified all instances of net.fetch to use url.toString() for better URL handling. - Ensured consistency in how URLs are passed to the fetch method across various functions. * refactor(MCPService): improve URL handling in net.fetch calls - Updated net.fetch to use url.toString() for better type handling of URLs. - Ensured consistent URL processing across the MCPService class. * feat(ProxyManager): integrate axios with fetch proxy support - Added axios as a dependency to enable fetch proxy usage. - Implemented logic to set axios's adapter to 'fetch' for proxy handling. - Preserved original axios adapter for restoration when disabling the proxy. --- .../preprocess/Doc2xPreprocessProvider.ts | 136 ++++++++++++------ .../preprocess/MineruPreprocessProvider.ts | 18 ++- .../knowledge/reranker/GeneralReranker.ts | 14 +- src/main/mcpServers/brave-search.ts | 9 +- src/main/mcpServers/dify-knowledge.ts | 5 +- src/main/mcpServers/fetch.ts | 3 +- src/main/services/AppUpdater.ts | 6 +- src/main/services/CopilotService.ts | 70 +++++---- src/main/services/FileStorage.ts | 3 +- src/main/services/MCPService.ts | 4 +- src/main/services/NutstoreService.ts | 3 +- src/main/utils/ipService.ts | 3 +- 12 files changed, 182 insertions(+), 92 deletions(-) diff --git a/src/main/knowledge/preprocess/Doc2xPreprocessProvider.ts b/src/main/knowledge/preprocess/Doc2xPreprocessProvider.ts index afc8d1ba9b..834ff2f27e 100644 --- a/src/main/knowledge/preprocess/Doc2xPreprocessProvider.ts +++ b/src/main/knowledge/preprocess/Doc2xPreprocessProvider.ts @@ -5,7 +5,7 @@ import { loggerService } from '@logger' import { fileStorage } from '@main/services/FileStorage' import { FileMetadata, PreprocessProvider } from '@types' import AdmZip from 'adm-zip' -import axios, { AxiosRequestConfig } from 'axios' +import { net } from 'electron' import BasePreprocessProvider from './BasePreprocessProvider' @@ -38,19 +38,24 @@ export default class Doc2xPreprocessProvider extends BasePreprocessProvider { } private async validateFile(filePath: string): Promise { - const pdfBuffer = await fs.promises.readFile(filePath) + // 首先检查文件大小,避免读取大文件到内存 + const stats = await fs.promises.stat(filePath) + const fileSizeBytes = stats.size + // 文件大小小于300MB + if (fileSizeBytes >= 300 * 1024 * 1024) { + const fileSizeMB = Math.round(fileSizeBytes / (1024 * 1024)) + throw new Error(`PDF file size (${fileSizeMB}MB) exceeds the limit of 300MB`) + } + + // 只有在文件大小合理的情况下才读取文件内容检查页数 + const pdfBuffer = await fs.promises.readFile(filePath) const doc = await this.readPdf(pdfBuffer) // 文件页数小于1000页 if (doc.numPages >= 1000) { throw new Error(`PDF page count (${doc.numPages}) exceeds the limit of 1000 pages`) } - // 文件大小小于300MB - if (pdfBuffer.length >= 300 * 1024 * 1024) { - const fileSizeMB = Math.round(pdfBuffer.length / (1024 * 1024)) - throw new Error(`PDF file size (${fileSizeMB}MB) exceeds the limit of 300MB`) - } } public async parseFile(sourceId: string, file: FileMetadata): Promise<{ processedFile: FileMetadata }> { @@ -160,11 +165,23 @@ export default class Doc2xPreprocessProvider extends BasePreprocessProvider { * @returns 预上传响应的url和uid */ private async preupload(): Promise { - const config = this.createAuthConfig() const endpoint = `${this.provider.apiHost}/api/v2/parse/preupload` try { - const { data } = await axios.post>(endpoint, null, config) + const response = await net.fetch(endpoint, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${this.provider.apiKey}` + }, + body: null + }) + + if (!response.ok) { + throw new Error(`HTTP ${response.status}: ${response.statusText}`) + } + + const data = (await response.json()) as ApiResponse if (data.code === 'success' && data.data) { return data.data @@ -178,17 +195,29 @@ export default class Doc2xPreprocessProvider extends BasePreprocessProvider { } /** - * 上传文件 + * 上传文件(使用流式上传) * @param filePath 文件路径 * @param url 预上传响应的url */ private async putFile(filePath: string, url: string): Promise { try { - const fileStream = fs.createReadStream(filePath) - const response = await axios.put(url, fileStream) + // 获取文件大小用于设置 Content-Length + const stats = await fs.promises.stat(filePath) + const fileSize = stats.size - if (response.status !== 200) { - throw new Error(`HTTP status ${response.status}: ${response.statusText}`) + // 创建可读流 + const fileStream = fs.createReadStream(filePath) + + const response = await net.fetch(url, { + method: 'PUT', + body: fileStream as any, // TypeScript 类型转换,net.fetch 支持 ReadableStream + headers: { + 'Content-Length': fileSize.toString() + } + }) + + if (!response.ok) { + throw new Error(`HTTP ${response.status}: ${response.statusText}`) } } catch (error) { logger.error(`Failed to upload file ${filePath}: ${error instanceof Error ? error.message : String(error)}`) @@ -197,16 +226,25 @@ export default class Doc2xPreprocessProvider extends BasePreprocessProvider { } private async getStatus(uid: string): Promise { - const config = this.createAuthConfig() const endpoint = `${this.provider.apiHost}/api/v2/parse/status?uid=${uid}` try { - const response = await axios.get>(endpoint, config) + const response = await net.fetch(endpoint, { + method: 'GET', + headers: { + Authorization: `Bearer ${this.provider.apiKey}` + } + }) - if (response.data.code === 'success' && response.data.data) { - return response.data.data + if (!response.ok) { + throw new Error(`HTTP ${response.status}: ${response.statusText}`) + } + + const data = (await response.json()) as ApiResponse + if (data.code === 'success' && data.data) { + return data.data } else { - throw new Error(`API returned error: ${response.data.message || JSON.stringify(response.data)}`) + throw new Error(`API returned error: ${data.message || JSON.stringify(data)}`) } } catch (error) { logger.error(`Failed to get status for uid ${uid}: ${error instanceof Error ? error.message : String(error)}`) @@ -221,13 +259,6 @@ export default class Doc2xPreprocessProvider extends BasePreprocessProvider { */ private async convertFile(uid: string, filePath: string): Promise { const fileName = path.parse(filePath).name - const config = { - ...this.createAuthConfig(), - headers: { - ...this.createAuthConfig().headers, - 'Content-Type': 'application/json' - } - } const payload = { uid, @@ -239,10 +270,22 @@ export default class Doc2xPreprocessProvider extends BasePreprocessProvider { const endpoint = `${this.provider.apiHost}/api/v2/convert/parse` try { - const response = await axios.post>(endpoint, payload, config) + const response = await net.fetch(endpoint, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${this.provider.apiKey}` + }, + body: JSON.stringify(payload) + }) - if (response.data.code !== 'success') { - throw new Error(`API returned error: ${response.data.message || JSON.stringify(response.data)}`) + if (!response.ok) { + throw new Error(`HTTP ${response.status}: ${response.statusText}`) + } + + const data = (await response.json()) as ApiResponse + if (data.code !== 'success') { + throw new Error(`API returned error: ${data.message || JSON.stringify(data)}`) } } catch (error) { logger.error(`Failed to convert file ${filePath}: ${error instanceof Error ? error.message : String(error)}`) @@ -256,16 +299,25 @@ export default class Doc2xPreprocessProvider extends BasePreprocessProvider { * @returns 解析后的文件信息 */ private async getParsedFile(uid: string): Promise { - const config = this.createAuthConfig() const endpoint = `${this.provider.apiHost}/api/v2/convert/parse/result?uid=${uid}` try { - const response = await axios.get>(endpoint, config) + const response = await net.fetch(endpoint, { + method: 'GET', + headers: { + Authorization: `Bearer ${this.provider.apiKey}` + } + }) - if (response.status === 200 && response.data.data) { - return response.data.data + if (!response.ok) { + throw new Error(`HTTP ${response.status}: ${response.statusText}`) + } + + const data = (await response.json()) as ApiResponse + if (data.data) { + return data.data } else { - throw new Error(`HTTP status ${response.status}: ${response.statusText}`) + throw new Error(`No data in response`) } } catch (error) { logger.error( @@ -295,8 +347,12 @@ export default class Doc2xPreprocessProvider extends BasePreprocessProvider { try { // 下载文件 - const response = await axios.get(url, { responseType: 'arraybuffer' }) - fs.writeFileSync(zipPath, response.data) + const response = await net.fetch(url, { method: 'GET' }) + if (!response.ok) { + throw new Error(`HTTP ${response.status}: ${response.statusText}`) + } + const arrayBuffer = await response.arrayBuffer() + fs.writeFileSync(zipPath, Buffer.from(arrayBuffer)) // 确保提取目录存在 if (!fs.existsSync(extractPath)) { @@ -318,14 +374,6 @@ export default class Doc2xPreprocessProvider extends BasePreprocessProvider { } } - private createAuthConfig(): AxiosRequestConfig { - return { - headers: { - Authorization: `Bearer ${this.provider.apiKey}` - } - } - } - public checkQuota(): Promise { throw new Error('Method not implemented.') } diff --git a/src/main/knowledge/preprocess/MineruPreprocessProvider.ts b/src/main/knowledge/preprocess/MineruPreprocessProvider.ts index 0e29a6443f..1976f64c05 100644 --- a/src/main/knowledge/preprocess/MineruPreprocessProvider.ts +++ b/src/main/knowledge/preprocess/MineruPreprocessProvider.ts @@ -5,7 +5,7 @@ import { loggerService } from '@logger' import { fileStorage } from '@main/services/FileStorage' import { FileMetadata, PreprocessProvider } from '@types' import AdmZip from 'adm-zip' -import axios from 'axios' +import { net } from 'electron' import BasePreprocessProvider from './BasePreprocessProvider' @@ -95,7 +95,7 @@ export default class MineruPreprocessProvider extends BasePreprocessProvider { public async checkQuota() { try { - const quota = await fetch(`${this.provider.apiHost}/api/v4/quota`, { + const quota = await net.fetch(`${this.provider.apiHost}/api/v4/quota`, { method: 'GET', headers: { 'Content-Type': 'application/json', @@ -179,8 +179,12 @@ export default class MineruPreprocessProvider extends BasePreprocessProvider { try { // 下载ZIP文件 - const response = await axios.get(zipUrl, { responseType: 'arraybuffer' }) - fs.writeFileSync(zipPath, Buffer.from(response.data)) + const response = await net.fetch(zipUrl, { method: 'GET' }) + if (!response.ok) { + throw new Error(`HTTP ${response.status}: ${response.statusText}`) + } + const arrayBuffer = await response.arrayBuffer() + fs.writeFileSync(zipPath, Buffer.from(arrayBuffer)) logger.info(`Downloaded ZIP file: ${zipPath}`) // 确保提取目录存在 @@ -236,7 +240,7 @@ export default class MineruPreprocessProvider extends BasePreprocessProvider { } try { - const response = await fetch(endpoint, { + const response = await net.fetch(endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -271,7 +275,7 @@ export default class MineruPreprocessProvider extends BasePreprocessProvider { try { const fileBuffer = await fs.promises.readFile(filePath) - const response = await fetch(uploadUrl, { + const response = await net.fetch(uploadUrl, { method: 'PUT', body: fileBuffer, headers: { @@ -316,7 +320,7 @@ export default class MineruPreprocessProvider extends BasePreprocessProvider { const endpoint = `${this.provider.apiHost}/api/v4/extract-results/batch/${batchId}` try { - const response = await fetch(endpoint, { + const response = await net.fetch(endpoint, { method: 'GET', headers: { 'Content-Type': 'application/json', diff --git a/src/main/knowledge/reranker/GeneralReranker.ts b/src/main/knowledge/reranker/GeneralReranker.ts index 1252ecad57..5a0e240a9d 100644 --- a/src/main/knowledge/reranker/GeneralReranker.ts +++ b/src/main/knowledge/reranker/GeneralReranker.ts @@ -1,6 +1,6 @@ import { ExtractChunkData } from '@cherrystudio/embedjs-interfaces' import { KnowledgeBaseParams } from '@types' -import axios from 'axios' +import { net } from 'electron' import BaseReranker from './BaseReranker' @@ -15,7 +15,17 @@ export default class GeneralReranker extends BaseReranker { const requestBody = this.getRerankRequestBody(query, searchResults) try { - const { data } = await axios.post(url, requestBody, { headers: this.defaultHeaders() }) + const response = await net.fetch(url, { + method: 'POST', + headers: this.defaultHeaders(), + body: JSON.stringify(requestBody) + }) + + if (!response.ok) { + throw new Error(`HTTP ${response.status}: ${response.statusText}`) + } + + const data = await response.json() const rerankResults = this.extractRerankResult(data) return this.getRerankResult(searchResults, rerankResults) diff --git a/src/main/mcpServers/brave-search.ts b/src/main/mcpServers/brave-search.ts index 6f219e1eb8..d11a4f2580 100644 --- a/src/main/mcpServers/brave-search.ts +++ b/src/main/mcpServers/brave-search.ts @@ -3,6 +3,7 @@ import { Server } from '@modelcontextprotocol/sdk/server/index.js' import { CallToolRequestSchema, ListToolsRequestSchema, Tool } from '@modelcontextprotocol/sdk/types.js' +import { net } from 'electron' const WEB_SEARCH_TOOL: Tool = { name: 'brave_web_search', @@ -159,7 +160,7 @@ async function performWebSearch(apiKey: string, query: string, count: number = 1 url.searchParams.set('count', Math.min(count, 20).toString()) // API limit url.searchParams.set('offset', offset.toString()) - const response = await fetch(url, { + const response = await net.fetch(url.toString(), { headers: { Accept: 'application/json', 'Accept-Encoding': 'gzip', @@ -192,7 +193,7 @@ async function performLocalSearch(apiKey: string, query: string, count: number = webUrl.searchParams.set('result_filter', 'locations') webUrl.searchParams.set('count', Math.min(count, 20).toString()) - const webResponse = await fetch(webUrl, { + const webResponse = await net.fetch(webUrl.toString(), { headers: { Accept: 'application/json', 'Accept-Encoding': 'gzip', @@ -225,7 +226,7 @@ async function getPoisData(apiKey: string, ids: string[]): Promise url.searchParams.append('ids', id)) - const response = await fetch(url, { + const response = await net.fetch(url.toString(), { headers: { Accept: 'application/json', 'Accept-Encoding': 'gzip', @@ -244,7 +245,7 @@ async function getDescriptionsData(apiKey: string, ids: string[]): Promise url.searchParams.append('ids', id)) - const response = await fetch(url, { + const response = await net.fetch(url.toString(), { headers: { Accept: 'application/json', 'Accept-Encoding': 'gzip', diff --git a/src/main/mcpServers/dify-knowledge.ts b/src/main/mcpServers/dify-knowledge.ts index 2bd2c4adda..83a352fd4f 100644 --- a/src/main/mcpServers/dify-knowledge.ts +++ b/src/main/mcpServers/dify-knowledge.ts @@ -2,6 +2,7 @@ import { loggerService } from '@logger' import { Server } from '@modelcontextprotocol/sdk/server/index.js' import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js' +import { net } from 'electron' import * as z from 'zod/v4' const logger = loggerService.withContext('DifyKnowledgeServer') @@ -134,7 +135,7 @@ class DifyKnowledgeServer { private async performListKnowledges(difyKey: string, apiHost: string): Promise { try { const url = `${apiHost.replace(/\/$/, '')}/datasets` - const response = await fetch(url, { + const response = await net.fetch(url, { method: 'GET', headers: { Authorization: `Bearer ${difyKey}` @@ -186,7 +187,7 @@ class DifyKnowledgeServer { try { const url = `${apiHost.replace(/\/$/, '')}/datasets/${id}/retrieve` - const response = await fetch(url, { + const response = await net.fetch(url, { method: 'POST', headers: { Authorization: `Bearer ${difyKey}`, diff --git a/src/main/mcpServers/fetch.ts b/src/main/mcpServers/fetch.ts index 04839d8a92..e55b114776 100644 --- a/src/main/mcpServers/fetch.ts +++ b/src/main/mcpServers/fetch.ts @@ -2,6 +2,7 @@ import { Server } from '@modelcontextprotocol/sdk/server/index.js' import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js' +import { net } from 'electron' import { JSDOM } from 'jsdom' import TurndownService from 'turndown' import { z } from 'zod' @@ -16,7 +17,7 @@ export type RequestPayload = z.infer export class Fetcher { private static async _fetch({ url, headers }: RequestPayload): Promise { try { - const response = await fetch(url, { + const response = await net.fetch(url, { headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', diff --git a/src/main/services/AppUpdater.ts b/src/main/services/AppUpdater.ts index ea3b1f3f1e..bdfb8e3cc8 100644 --- a/src/main/services/AppUpdater.ts +++ b/src/main/services/AppUpdater.ts @@ -6,7 +6,7 @@ import { generateUserAgent } from '@main/utils/systemInfo' import { FeedUrl, UpgradeChannel } from '@shared/config/constant' import { IpcChannel } from '@shared/IpcChannel' import { CancellationToken, UpdateInfo } from 'builder-util-runtime' -import { app, BrowserWindow, dialog } from 'electron' +import { app, BrowserWindow, dialog, net } from 'electron' import { AppUpdater as _AppUpdater, autoUpdater, Logger, NsisUpdater, UpdateCheckResult } from 'electron-updater' import path from 'path' import semver from 'semver' @@ -75,7 +75,7 @@ export default class AppUpdater { } try { logger.info(`get release version from github: ${channel}`) - const responses = await fetch('https://api.github.com/repos/CherryHQ/cherry-studio/releases?per_page=8', { + const responses = await net.fetch('https://api.github.com/repos/CherryHQ/cherry-studio/releases?per_page=8', { headers }) const data = (await responses.json()) as GithubReleaseInfo[] @@ -99,7 +99,7 @@ export default class AppUpdater { if (mightHaveLatest) { logger.info(`might have latest release, get latest release`) - const latestReleaseResponse = await fetch( + const latestReleaseResponse = await net.fetch( 'https://api.github.com/repos/CherryHQ/cherry-studio/releases/latest', { headers diff --git a/src/main/services/CopilotService.ts b/src/main/services/CopilotService.ts index bb54e74932..f5c773a7cc 100644 --- a/src/main/services/CopilotService.ts +++ b/src/main/services/CopilotService.ts @@ -1,6 +1,5 @@ import { loggerService } from '@logger' -import { AxiosRequestConfig } from 'axios' -import axios from 'axios' +import { net } from 'electron' import { app, safeStorage } from 'electron' import fs from 'fs/promises' import path from 'path' @@ -86,7 +85,8 @@ class CopilotService { */ public getUser = async (_: Electron.IpcMainInvokeEvent, token: string): Promise => { try { - const config: AxiosRequestConfig = { + const response = await net.fetch(CONFIG.API_URLS.GITHUB_USER, { + method: 'GET', headers: { Connection: 'keep-alive', 'user-agent': 'Visual Studio Code (desktop)', @@ -95,12 +95,16 @@ class CopilotService { 'Sec-Fetch-Dest': 'empty', authorization: `token ${token}` } + }) + + if (!response.ok) { + throw new Error(`HTTP ${response.status}: ${response.statusText}`) } - const response = await axios.get(CONFIG.API_URLS.GITHUB_USER, config) + const data = await response.json() return { - login: response.data.login, - avatar: response.data.avatar_url + login: data.login, + avatar: data.avatar_url } } catch (error) { logger.error('Failed to get user information:', error as Error) @@ -118,16 +122,23 @@ class CopilotService { try { this.updateHeaders(headers) - const response = await axios.post( - CONFIG.API_URLS.GITHUB_DEVICE_CODE, - { + const response = await net.fetch(CONFIG.API_URLS.GITHUB_DEVICE_CODE, { + method: 'POST', + headers: { + ...this.headers, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ client_id: CONFIG.GITHUB_CLIENT_ID, scope: 'read:user' - }, - { headers: this.headers } - ) + }) + }) - return response.data + if (!response.ok) { + throw new Error(`HTTP ${response.status}: ${response.statusText}`) + } + + return (await response.json()) as AuthResponse } catch (error) { logger.error('Failed to get auth message:', error as Error) throw new CopilotServiceError('无法获取GitHub授权信息', error) @@ -150,17 +161,25 @@ class CopilotService { await this.delay(currentDelay) try { - const response = await axios.post( - CONFIG.API_URLS.GITHUB_ACCESS_TOKEN, - { + const response = await net.fetch(CONFIG.API_URLS.GITHUB_ACCESS_TOKEN, { + method: 'POST', + headers: { + ...this.headers, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ client_id: CONFIG.GITHUB_CLIENT_ID, device_code, grant_type: 'urn:ietf:params:oauth:grant-type:device_code' - }, - { headers: this.headers } - ) + }) + }) - const { access_token } = response.data + if (!response.ok) { + throw new Error(`HTTP ${response.status}: ${response.statusText}`) + } + + const data = (await response.json()) as TokenResponse + const { access_token } = data if (access_token) { return { access_token } } @@ -205,16 +224,19 @@ class CopilotService { const encryptedToken = await fs.readFile(this.tokenFilePath) const access_token = safeStorage.decryptString(Buffer.from(encryptedToken)) - const config: AxiosRequestConfig = { + const response = await net.fetch(CONFIG.API_URLS.COPILOT_TOKEN, { + method: 'GET', headers: { ...this.headers, authorization: `token ${access_token}` } + }) + + if (!response.ok) { + throw new Error(`HTTP ${response.status}: ${response.statusText}`) } - const response = await axios.get(CONFIG.API_URLS.COPILOT_TOKEN, config) - - return response.data + return (await response.json()) as CopilotTokenResponse } catch (error) { logger.error('Failed to get Copilot token:', error as Error) throw new CopilotServiceError('无法获取Copilot令牌,请重新授权', error) diff --git a/src/main/services/FileStorage.ts b/src/main/services/FileStorage.ts index e34c51f299..f5df9ed3f7 100644 --- a/src/main/services/FileStorage.ts +++ b/src/main/services/FileStorage.ts @@ -5,6 +5,7 @@ import { FileMetadata } from '@types' import * as crypto from 'crypto' import { dialog, + net, OpenDialogOptions, OpenDialogReturnValue, SaveDialogOptions, @@ -509,7 +510,7 @@ class FileStorage { isUseContentType?: boolean ): Promise => { try { - const response = await fetch(url) + const response = await net.fetch(url) if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`) } diff --git a/src/main/services/MCPService.ts b/src/main/services/MCPService.ts index d3909cc86f..a7f907f65f 100644 --- a/src/main/services/MCPService.ts +++ b/src/main/services/MCPService.ts @@ -29,7 +29,7 @@ import { } from '@modelcontextprotocol/sdk/types.js' import { nanoid } from '@reduxjs/toolkit' import type { GetResourceResponse, MCPCallToolResponse, MCPPrompt, MCPResource, MCPServer, MCPTool } from '@types' -import { app } from 'electron' +import { app, net } from 'electron' import { EventEmitter } from 'events' import { memoize } from 'lodash' import { v4 as uuidv4 } from 'uuid' @@ -205,7 +205,7 @@ class McpService { } } - return fetch(url, { ...init, headers }) + return net.fetch(typeof url === 'string' ? url : url.toString(), { ...init, headers }) } }, requestInit: { diff --git a/src/main/services/NutstoreService.ts b/src/main/services/NutstoreService.ts index 4422ea8a07..f4ad5a2c33 100644 --- a/src/main/services/NutstoreService.ts +++ b/src/main/services/NutstoreService.ts @@ -2,6 +2,7 @@ import path from 'node:path' import { loggerService } from '@logger' import { NUTSTORE_HOST } from '@shared/config/nutstore' +import { net } from 'electron' import { XMLParser } from 'fast-xml-parser' import { isNil, partial } from 'lodash' import { type FileStat } from 'webdav' @@ -62,7 +63,7 @@ export async function getDirectoryContents(token: string, target: string): Promi let currentUrl = `${NUTSTORE_HOST}${target}` while (true) { - const response = await fetch(currentUrl, { + const response = await net.fetch(currentUrl, { method: 'PROPFIND', headers: { Authorization: `Basic ${token}`, diff --git a/src/main/utils/ipService.ts b/src/main/utils/ipService.ts index ec5ab78215..3180f9457c 100644 --- a/src/main/utils/ipService.ts +++ b/src/main/utils/ipService.ts @@ -1,4 +1,5 @@ import { loggerService } from '@logger' +import { net } from 'electron' const logger = loggerService.withContext('IpService') @@ -12,7 +13,7 @@ export async function getIpCountry(): Promise { const controller = new AbortController() const timeoutId = setTimeout(() => controller.abort(), 5000) - const ipinfo = await fetch('https://ipinfo.io/json', { + const ipinfo = await net.fetch('https://ipinfo.io/json', { signal: controller.signal, headers: { 'User-Agent': From e0dbd2d2dbea06ca1abf335066964c6bdc34c669 Mon Sep 17 00:00:00 2001 From: SuYao Date: Fri, 15 Aug 2025 22:56:40 +0800 Subject: [PATCH 2/4] fix/9165 (#9194) * fix/9165 * fix: early return --- .../aiCore/clients/openai/OpenAIApiClient.ts | 12 ++++--- src/renderer/src/config/models.ts | 36 ++++++++++--------- src/renderer/src/types/index.ts | 1 + 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/renderer/src/aiCore/clients/openai/OpenAIApiClient.ts b/src/renderer/src/aiCore/clients/openai/OpenAIApiClient.ts index 60173551b4..7568ee69be 100644 --- a/src/renderer/src/aiCore/clients/openai/OpenAIApiClient.ts +++ b/src/renderer/src/aiCore/clients/openai/OpenAIApiClient.ts @@ -9,6 +9,7 @@ import { isGPT5SeriesModel, isGrokReasoningModel, isNotSupportSystemMessageModel, + isOpenAIReasoningModel, isQwenAlwaysThinkModel, isQwenMTModel, isQwenReasoningModel, @@ -146,7 +147,7 @@ export class OpenAIAPIClient extends OpenAIBaseClient< return {} } // Don't disable reasoning for models that require it - if (isGrokReasoningModel(model)) { + if (isGrokReasoningModel(model) || isOpenAIReasoningModel(model)) { return {} } return { reasoning: { enabled: false, exclude: true } } @@ -524,12 +525,13 @@ export class OpenAIAPIClient extends OpenAIBaseClient< } // 1. 处理系统消息 - let systemMessage = { role: 'system', content: assistant.prompt || '' } + const systemMessage = { role: 'system', content: assistant.prompt || '' } if (isSupportedReasoningEffortOpenAIModel(model)) { - systemMessage = { - role: isSupportDeveloperRoleProvider(this.provider) ? 'developer' : 'system', - content: `Formatting re-enabled${systemMessage ? '\n' + systemMessage.content : ''}` + if (isSupportDeveloperRoleProvider(this.provider)) { + systemMessage.role = 'developer' + } else { + systemMessage.role = 'system' } } diff --git a/src/renderer/src/config/models.ts b/src/renderer/src/config/models.ts index b5164dfe5b..e6276cbb98 100644 --- a/src/renderer/src/config/models.ts +++ b/src/renderer/src/config/models.ts @@ -292,6 +292,7 @@ export const CLAUDE_SUPPORTED_WEBSEARCH_REGEX = new RegExp( // 模型类型到支持的reasoning_effort的映射表 export const MODEL_SUPPORTED_REASONING_EFFORT: ReasoningEffortConfig = { default: ['low', 'medium', 'high'] as const, + o: ['low', 'medium', 'high'] as const, gpt5: ['minimal', 'low', 'medium', 'high'] as const, grok: ['low', 'high'] as const, gemini: ['low', 'medium', 'high', 'auto'] as const, @@ -307,7 +308,8 @@ export const MODEL_SUPPORTED_REASONING_EFFORT: ReasoningEffortConfig = { // 模型类型到支持选项的映射表 export const MODEL_SUPPORTED_OPTIONS: ThinkingOptionConfig = { default: ['off', ...MODEL_SUPPORTED_REASONING_EFFORT.default] as const, - gpt5: ['off', ...MODEL_SUPPORTED_REASONING_EFFORT.gpt5] as const, + o: MODEL_SUPPORTED_REASONING_EFFORT.o, + gpt5: [...MODEL_SUPPORTED_REASONING_EFFORT.gpt5] as const, grok: MODEL_SUPPORTED_REASONING_EFFORT.grok, gemini: ['off', ...MODEL_SUPPORTED_REASONING_EFFORT.gemini] as const, gemini_pro: MODEL_SUPPORTED_REASONING_EFFORT.gemini_pro, @@ -320,28 +322,28 @@ export const MODEL_SUPPORTED_OPTIONS: ThinkingOptionConfig = { } as const export const getThinkModelType = (model: Model): ThinkingModelType => { + let thinkingModelType: ThinkingModelType = 'default' if (isGPT5SeriesModel(model)) { - return 'gpt5' - } - if (isSupportedThinkingTokenGeminiModel(model)) { + thinkingModelType = 'gpt5' + } else if (isSupportedReasoningEffortOpenAIModel(model)) { + thinkingModelType = 'o' + } else if (isSupportedThinkingTokenGeminiModel(model)) { if (GEMINI_FLASH_MODEL_REGEX.test(model.id)) { - return 'gemini' + thinkingModelType = 'gemini' } else { - return 'gemini_pro' + thinkingModelType = 'gemini_pro' } - } - if (isSupportedReasoningEffortGrokModel(model)) return 'grok' - if (isSupportedThinkingTokenQwenModel(model)) { + } else if (isSupportedReasoningEffortGrokModel(model)) thinkingModelType = 'grok' + else if (isSupportedThinkingTokenQwenModel(model)) { if (isQwenAlwaysThinkModel(model)) { - return 'qwen_thinking' + thinkingModelType = 'qwen_thinking' } - return 'qwen' - } - if (isSupportedThinkingTokenDoubaoModel(model)) return 'doubao' - if (isSupportedThinkingTokenHunyuanModel(model)) return 'hunyuan' - if (isSupportedReasoningEffortPerplexityModel(model)) return 'perplexity' - if (isSupportedThinkingTokenZhipuModel(model)) return 'zhipu' - return 'default' + thinkingModelType = 'qwen' + } else if (isSupportedThinkingTokenDoubaoModel(model)) thinkingModelType = 'doubao' + else if (isSupportedThinkingTokenHunyuanModel(model)) thinkingModelType = 'hunyuan' + else if (isSupportedReasoningEffortPerplexityModel(model)) thinkingModelType = 'perplexity' + else if (isSupportedThinkingTokenZhipuModel(model)) thinkingModelType = 'zhipu' + return thinkingModelType } export function isFunctionCallingModel(model?: Model): boolean { diff --git a/src/renderer/src/types/index.ts b/src/renderer/src/types/index.ts index ca4a1fe4fd..5f649c5e8f 100644 --- a/src/renderer/src/types/index.ts +++ b/src/renderer/src/types/index.ts @@ -56,6 +56,7 @@ export type ReasoningEffortOption = NonNullable | 'auto' export type ThinkingOption = ReasoningEffortOption | 'off' export type ThinkingModelType = | 'default' + | 'o' | 'gpt5' | 'grok' | 'gemini' From a02b4b3955d742fb5b10455bacf357c9fd9dec82 Mon Sep 17 00:00:00 2001 From: Pleasure1234 <3196812536@qq.com> Date: Sat, 16 Aug 2025 04:00:32 +0800 Subject: [PATCH 3/4] fix: websearch (#9222) Update LocalSearchProvider.ts --- .../providers/WebSearchProvider/LocalSearchProvider.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/renderer/src/providers/WebSearchProvider/LocalSearchProvider.ts b/src/renderer/src/providers/WebSearchProvider/LocalSearchProvider.ts index abdf9fc826..7911646630 100644 --- a/src/renderer/src/providers/WebSearchProvider/LocalSearchProvider.ts +++ b/src/renderer/src/providers/WebSearchProvider/LocalSearchProvider.ts @@ -89,15 +89,9 @@ export default class LocalSearchProvider extends BaseWebSearchProvider { * @returns 带有语言过滤的查询 */ protected applyLanguageFilter(query: string, language: string): string { - if (this.provider.id.includes('local-google')) { + if (this.provider.id.includes('local-google') || this.provider.id.includes('local-bing')) { return `${query} lang:${language.split('-')[0]}` } - if (this.provider.id.includes('local-bing')) { - return `${query} language:${language}` - } - if (this.provider.id.includes('local-baidu')) { - return `${query} language:${language.split('-')[0]}` - } return query } From 04326eba21ddf14fe62199ff11d8b1c50fa51858 Mon Sep 17 00:00:00 2001 From: miro <16189212+miroklarin@users.noreply.github.com> Date: Sat, 16 Aug 2025 04:21:29 +0100 Subject: [PATCH 4/4] feat: Use different window name for Quick Assistant (#9217) Co-authored-by: Miro Klarin --- src/renderer/miniWindow.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderer/miniWindow.html b/src/renderer/miniWindow.html index 83b108b8a4..7f3b936444 100644 --- a/src/renderer/miniWindow.html +++ b/src/renderer/miniWindow.html @@ -6,7 +6,7 @@ - Cherry Studio + Cherry Studio Quick Assistant