cherry-studio/src/main/services/ProtocolClient.ts
fullex 40f9601379
refactor: Unified Logger / 统一日志管理 (#8207)
* Revert "feat: optimize minapp cache with LRU (#8160)"

This reverts commit f0043b4be5.

* feat: integrate logger service and enhance logging throughout the application

- Added a new LoggerService to standardize logging across the application.
- Replaced console.error and console.warn calls with logger methods for improved consistency and error tracking.
- Introduced a new IPC channel for logging messages to the main process.
- Updated various components and services to utilize the new logging system, enhancing error handling and debugging capabilities.

* refactor: enhance logging and error handling across various components

- Integrated the LoggerService for consistent logging throughout the application.
- Updated multiple components and services to utilize the new logging system, improving error tracking and debugging capabilities.
- Refactored file handling and error management in several services to enhance reliability and clarity.
- Improved the structure and readability of the codebase by removing redundant checks and simplifying logic.

* chore: update TypeScript configuration and enhance test setup

- Added test mock paths to tsconfig.web.json for improved test coverage.
- Configured Vitest to include a setup file for main tests, ensuring consistent test environment.
- Updated IPC logger context for better clarity in logging.
- Enhanced LoggerService to handle undefined values gracefully.
- Mocked LoggerService globally in renderer tests to streamline testing process.

* refactor: standardize logging across ProxyManager and ReduxService

- Replaced instances of Logger with logger for consistent logging implementation.
- Improved logging clarity in ProxyManager's configureProxy method and ReduxService's state handling.
- Enhanced error logging in ReduxService to align with the new logging system.

* refactor: reorganize LoggerService for improved clarity and consistency

- Moved the definition of SYSTEM_INFO, APP_VERSION, and DEFAULT_LEVEL to enhance code organization.
- Simplified the getIsDev function in the renderer LoggerService for better readability.
- Updated logging conditions to ensure messages are logged correctly based on context.

* docs: add usage instructions for LoggerService and clean up logging code

- Included important usage instructions for LoggerService in both English and Chinese.
- Commented out the console transport in LoggerService to streamline logging.
- Improved logging message formatting in MCPService for clarity.
- Removed redundant logging statements in SelectionService to enhance code cleanliness.

* refactor: update LoggerService documentation paths and enhance logging implementation

- Changed the documentation paths for LoggerService usage instructions to `docs/technical/how-to-use-logger-en.md` and `docs/technical/how-to-use-logger-zh.md`.
- Replaced console logging with the loggerService in various components, including `MCPSettings`, `BlockManager`, and multiple callback files, to ensure consistent logging practices across the application.
- Improved the clarity and context of log messages for better debugging and monitoring.

* docs: emphasize logger usage guidelines in documentation

- Added a note in both English and Chinese documentation to discourage the use of `console.xxx` for logging unless necessary, promoting consistent logging practices across the application.
2025-07-18 09:40:56 +08:00

130 lines
4.1 KiB
TypeScript

import { exec } from 'node:child_process'
import fs from 'node:fs/promises'
import path from 'node:path'
import { promisify } from 'node:util'
import { loggerService } from '@logger'
import { app } from 'electron'
import { handleProvidersProtocolUrl } from './urlschema/handle-providers'
import { handleMcpProtocolUrl } from './urlschema/mcp-install'
import { windowService } from './WindowService'
const logger = loggerService.withContext('ProtocolClient')
export const CHERRY_STUDIO_PROTOCOL = 'cherrystudio'
export function registerProtocolClient(app: Electron.App) {
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient(CHERRY_STUDIO_PROTOCOL, process.execPath, [process.argv[1]])
}
}
app.setAsDefaultProtocolClient(CHERRY_STUDIO_PROTOCOL)
}
export function handleProtocolUrl(url: string) {
if (!url) return
// Process the URL that was used to open the app
// The url will be in the format: cherrystudio://data?param1=value1&param2=value2
// Parse the URL and extract parameters
const urlObj = new URL(url)
const params = new URLSearchParams(urlObj.search)
switch (urlObj.hostname.toLowerCase()) {
case 'mcp':
handleMcpProtocolUrl(urlObj)
return
case 'providers':
handleProvidersProtocolUrl(urlObj)
return
}
// You can send the data to your renderer process
const mainWindow = windowService.getMainWindow()
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('protocol-data', {
url,
params: Object.fromEntries(params.entries())
})
}
}
const execAsync = promisify(exec)
const DESKTOP_FILE_NAME = 'cherrystudio-url-handler.desktop'
/**
* Sets up deep linking for the AppImage build on Linux by creating a .desktop file.
* This allows the OS to open cherrystudio:// URLs with this App.
*/
export async function setupAppImageDeepLink(): Promise<void> {
// Only run on Linux and when packaged as an AppImage
if (process.platform !== 'linux' || !process.env.APPIMAGE) {
return
}
logger.debug('AppImage environment detected on Linux, setting up deep link.')
try {
const appPath = app.getPath('exe')
if (!appPath) {
logger.error('Could not determine App path.')
return
}
const homeDir = app.getPath('home')
const applicationsDir = path.join(homeDir, '.local', 'share', 'applications')
const desktopFilePath = path.join(applicationsDir, DESKTOP_FILE_NAME)
// Ensure the applications directory exists
await fs.mkdir(applicationsDir, { recursive: true })
// Content of the .desktop file
// %U allows passing the URL to the application
// NoDisplay=true hides it from the regular application menu
const desktopFileContent = `[Desktop Entry]
Name=Cherry Studio
Exec=${escapePathForExec(appPath)} %U
Terminal=false
Type=Application
MimeType=x-scheme-handler/${CHERRY_STUDIO_PROTOCOL};
NoDisplay=true
`
// Write the .desktop file (overwrite if exists)
await fs.writeFile(desktopFilePath, desktopFileContent, 'utf-8')
logger.debug(`Created/Updated desktop file: ${desktopFilePath}`)
// Update the desktop database
// It's important to update the database for the changes to take effect
try {
const { stdout, stderr } = await execAsync(`update-desktop-database ${escapePathForExec(applicationsDir)}`)
if (stderr) {
logger.warn(`update-desktop-database stderr: ${stderr}`)
}
logger.debug(`update-desktop-database stdout: ${stdout}`)
logger.debug('Desktop database updated successfully.')
} catch (updateError) {
logger.error('Failed to update desktop database:', updateError)
// Continue even if update fails, as the file is still created.
}
} catch (error) {
// Log the error but don't prevent the app from starting
logger.error('Failed to setup AppImage deep link:', error)
}
}
/**
* Escapes a path for safe use within the Exec field of a .desktop file
* and for shell commands. Handles spaces and potentially other special characters
* by quoting.
*/
function escapePathForExec(filePath: string): string {
// Simple quoting for paths with spaces.
return `'${filePath.replace(/'/g, "'\\''")}'`
}