mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-27 04:31:27 +08:00
refactor(CopilotService): streamline token file handling and improve error management (#9552)
* refactor(CopilotService): streamline token file handling and improve error management - Consolidated token file path retrieval into a dedicated method for better clarity and maintainability. - Updated file system operations to ensure directory existence before writing the token file. - Enhanced error handling during token save and read operations, ensuring robust logging and user feedback. * lint * build: add sharp dependency for image processing - Added sharp as a dependency in package.json to support image processing functionalities. - Removed sharp from the devDependencies section to ensure it is available in production.
This commit is contained in:
parent
f9869ef453
commit
e956a9ad35
@ -79,6 +79,7 @@
|
||||
"officeparser": "^4.2.0",
|
||||
"os-proxy-config": "^1.1.2",
|
||||
"selection-hook": "^1.0.11",
|
||||
"sharp": "^0.34.3",
|
||||
"tesseract.js": "patch:tesseract.js@npm%3A6.0.1#~/.yarn/patches/tesseract.js-npm-6.0.1-2562a7e46d.patch",
|
||||
"turndown": "7.2.0"
|
||||
},
|
||||
@ -258,7 +259,6 @@
|
||||
"remove-markdown": "^0.6.2",
|
||||
"rollup-plugin-visualizer": "^5.12.0",
|
||||
"sass": "^1.88.0",
|
||||
"sharp": "^0.34.3",
|
||||
"shiki": "^3.9.1",
|
||||
"strict-url-sanitise": "^0.0.1",
|
||||
"string-width": "^7.2.0",
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
import { loggerService } from '@logger'
|
||||
import { net } from 'electron'
|
||||
import { app, safeStorage } from 'electron'
|
||||
import fs from 'fs/promises'
|
||||
import { app, net, safeStorage } from 'electron'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
import { getConfigDir } from '../utils/file'
|
||||
|
||||
const logger = loggerService.withContext('CopilotService')
|
||||
|
||||
// 配置常量,集中管理
|
||||
@ -28,7 +29,8 @@ const CONFIG = {
|
||||
GITHUB_DEVICE_CODE: 'https://github.com/login/device/code',
|
||||
GITHUB_ACCESS_TOKEN: 'https://github.com/login/oauth/access_token',
|
||||
COPILOT_TOKEN: 'https://api.github.com/copilot_internal/v2/token'
|
||||
}
|
||||
},
|
||||
TOKEN_FILE_NAME: '.copilot_token'
|
||||
}
|
||||
|
||||
// 接口定义移到顶部,便于查阅
|
||||
@ -67,7 +69,7 @@ class CopilotService {
|
||||
private headers: Record<string, string>
|
||||
|
||||
constructor() {
|
||||
this.tokenFilePath = path.join(app.getPath('userData'), '.copilot_token')
|
||||
this.tokenFilePath = this.getTokenFilePath()
|
||||
this.headers = {
|
||||
...CONFIG.DEFAULT_HEADERS,
|
||||
accept: 'application/json',
|
||||
@ -75,6 +77,14 @@ class CopilotService {
|
||||
}
|
||||
}
|
||||
|
||||
private getTokenFilePath = (): string => {
|
||||
const oldTokenFilePath = path.join(app.getPath('userData'), CONFIG.TOKEN_FILE_NAME)
|
||||
if (fs.existsSync(oldTokenFilePath)) {
|
||||
return oldTokenFilePath
|
||||
}
|
||||
return path.join(getConfigDir(), CONFIG.TOKEN_FILE_NAME)
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置自定义请求头
|
||||
*/
|
||||
@ -209,7 +219,13 @@ class CopilotService {
|
||||
public saveCopilotToken = async (_: Electron.IpcMainInvokeEvent, token: string): Promise<void> => {
|
||||
try {
|
||||
const encryptedToken = safeStorage.encryptString(token)
|
||||
await fs.writeFile(this.tokenFilePath, encryptedToken)
|
||||
// 确保目录存在
|
||||
const dir = path.dirname(this.tokenFilePath)
|
||||
if (!fs.existsSync(dir)) {
|
||||
await fs.promises.mkdir(dir, { recursive: true })
|
||||
}
|
||||
|
||||
await fs.promises.writeFile(this.tokenFilePath, encryptedToken)
|
||||
} catch (error) {
|
||||
logger.error('Failed to save token:', error as Error)
|
||||
throw new CopilotServiceError('无法保存访问令牌', error)
|
||||
@ -226,7 +242,7 @@ class CopilotService {
|
||||
try {
|
||||
this.updateHeaders(headers)
|
||||
|
||||
const encryptedToken = await fs.readFile(this.tokenFilePath)
|
||||
const encryptedToken = await fs.promises.readFile(this.tokenFilePath)
|
||||
const access_token = safeStorage.decryptString(Buffer.from(encryptedToken))
|
||||
|
||||
const response = await net.fetch(CONFIG.API_URLS.COPILOT_TOKEN, {
|
||||
@ -254,8 +270,8 @@ class CopilotService {
|
||||
public logout = async (): Promise<void> => {
|
||||
try {
|
||||
try {
|
||||
await fs.access(this.tokenFilePath)
|
||||
await fs.unlink(this.tokenFilePath)
|
||||
await fs.promises.access(this.tokenFilePath)
|
||||
await fs.promises.unlink(this.tokenFilePath)
|
||||
logger.debug('Successfully logged out from Copilot')
|
||||
} catch (error) {
|
||||
// 文件不存在不是错误,只是记录一下
|
||||
|
||||
Loading…
Reference in New Issue
Block a user