feat(desktop): ✨ 实现一些功能
1. 实现任务暂停功能 2. 实现页面的国际化功能 3.优化项目的结构以及BUG 4. 优化系统架构 5. 实现一大堆的功能
This commit is contained in:
61
src/main/utils/concurrencyManager.ts
Normal file
61
src/main/utils/concurrencyManager.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import pLimit from 'p-limit' // 建议使用 v2.2.0 以兼容 CJS
|
||||
|
||||
/**
|
||||
* 并发管理器,负责动态调整并发数
|
||||
*/
|
||||
export class ConcurrencyManager {
|
||||
private baseConcurrency: number
|
||||
private currentConcurrency: number
|
||||
private limit: ReturnType<typeof pLimit>
|
||||
|
||||
constructor(baseConcurrency: number = 2) {
|
||||
this.baseConcurrency = baseConcurrency
|
||||
this.currentConcurrency = baseConcurrency
|
||||
this.limit = pLimit(this.currentConcurrency)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前的并发限制器
|
||||
*/
|
||||
getLimit() {
|
||||
return this.limit
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加并发数
|
||||
*/
|
||||
increaseConcurrency(): void {
|
||||
this.currentConcurrency++
|
||||
this.limit = pLimit(this.currentConcurrency)
|
||||
}
|
||||
|
||||
/**
|
||||
* 减少并发数
|
||||
*/
|
||||
decreaseConcurrency(): void {
|
||||
if (this.currentConcurrency > 1) {
|
||||
this.currentConcurrency--
|
||||
this.limit = pLimit(this.currentConcurrency)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置并发数为基准值
|
||||
*/
|
||||
resetConcurrency(): void {
|
||||
this.currentConcurrency = this.baseConcurrency
|
||||
this.limit = pLimit(this.currentConcurrency)
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据系统资源动态调整并发数
|
||||
*/
|
||||
adjustConcurrency(): void {
|
||||
// 这里可以添加根据系统资源(如CPU、内存使用率)动态调整并发数的逻辑
|
||||
// 目前实现一个简单的基于任务类型的调整策略
|
||||
this.resetConcurrency()
|
||||
}
|
||||
}
|
||||
|
||||
// 导出一个单例实例
|
||||
export const concurrencyManager = new ConcurrencyManager()
|
||||
76
src/main/utils/errorHandler.ts
Normal file
76
src/main/utils/errorHandler.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { BaseError } from './errors/baseError'
|
||||
import logger from '@shared/utils/logger'
|
||||
|
||||
/**
|
||||
* 错误处理工具类
|
||||
*/
|
||||
export class ErrorHandler {
|
||||
/**
|
||||
* 处理错误
|
||||
*/
|
||||
static handleError(error: any): void {
|
||||
if (error instanceof BaseError) {
|
||||
// 处理自定义错误
|
||||
this.handleCustomError(error)
|
||||
} else {
|
||||
// 处理原生错误
|
||||
this.handleNativeError(error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理自定义错误
|
||||
*/
|
||||
private static handleCustomError(error: BaseError): void {
|
||||
logger.error(error.code, {
|
||||
message: error.message,
|
||||
details: error.details,
|
||||
stack: error.stack
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理原生错误
|
||||
*/
|
||||
private static handleNativeError(error: Error): void {
|
||||
logger.error('UNHANDLED_ERROR', {
|
||||
message: error.message,
|
||||
stack: error.stack
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 重试函数
|
||||
*/
|
||||
static async retry<T>(
|
||||
fn: () => Promise<T>,
|
||||
maxAttempts: number = 3,
|
||||
delay: number = 1000,
|
||||
retryableErrors: Array<new (...args: any[]) => Error> = []
|
||||
): Promise<T> {
|
||||
let attempts = 0
|
||||
|
||||
while (attempts < maxAttempts) {
|
||||
try {
|
||||
return await fn()
|
||||
} catch (error: any) {
|
||||
attempts++
|
||||
|
||||
// 检查是否可以重试
|
||||
const isRetryable = retryableErrors.some(
|
||||
(ErrorClass) => error instanceof ErrorClass
|
||||
)
|
||||
|
||||
if (attempts >= maxAttempts || !isRetryable) {
|
||||
throw error
|
||||
}
|
||||
|
||||
// 等待一段时间后重试
|
||||
await new Promise((resolve) => setTimeout(resolve, delay * Math.pow(2, attempts - 1)))
|
||||
}
|
||||
}
|
||||
|
||||
// 理论上不会执行到这里,因为上面的循环会抛出错误
|
||||
throw new Error('重试次数已达上限')
|
||||
}
|
||||
}
|
||||
46
src/main/utils/errors/aiError.ts
Normal file
46
src/main/utils/errors/aiError.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { BaseError } from './baseError'
|
||||
|
||||
/**
|
||||
* AI服务相关错误
|
||||
*/
|
||||
export class AIError extends BaseError {
|
||||
constructor(message: string, details?: any, options?: ErrorOptions) {
|
||||
super(message, 'AI_ERROR', details, options)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AI模型调用错误
|
||||
*/
|
||||
export class AIModelCallError extends AIError {
|
||||
constructor(message: string = 'AI模型调用失败', details?: any, options?: ErrorOptions) {
|
||||
super(message, details, options)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AI生成内容错误
|
||||
*/
|
||||
export class AIGenerationError extends AIError {
|
||||
constructor(message: string = 'AI生成内容失败', details?: any, options?: ErrorOptions) {
|
||||
super(message, details, options)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AI图执行错误
|
||||
*/
|
||||
export class AIGraphError extends AIError {
|
||||
constructor(message: string = 'AI图执行失败', details?: any, options?: ErrorOptions) {
|
||||
super(message, details, options)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AI提示词错误
|
||||
*/
|
||||
export class AIPromptError extends AIError {
|
||||
constructor(message: string = 'AI提示词错误', details?: any, options?: ErrorOptions) {
|
||||
super(message, details, options)
|
||||
}
|
||||
}
|
||||
25
src/main/utils/errors/baseError.ts
Normal file
25
src/main/utils/errors/baseError.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* 基础错误类,所有自定义错误的基类
|
||||
*/
|
||||
export class BaseError extends Error {
|
||||
public readonly name: string
|
||||
public readonly code: string
|
||||
public readonly details?: any
|
||||
public readonly timestamp: Date
|
||||
|
||||
constructor(
|
||||
message: string,
|
||||
code: string,
|
||||
details?: any,
|
||||
options?: ErrorOptions
|
||||
) {
|
||||
super(message, options)
|
||||
this.name = this.constructor.name
|
||||
this.code = code
|
||||
this.details = details
|
||||
this.timestamp = new Date()
|
||||
|
||||
// 设置原型链,确保 instanceof 正常工作
|
||||
Object.setPrototypeOf(this, new.target.prototype)
|
||||
}
|
||||
}
|
||||
46
src/main/utils/errors/databaseError.ts
Normal file
46
src/main/utils/errors/databaseError.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { BaseError } from './baseError'
|
||||
|
||||
/**
|
||||
* 数据库操作相关错误
|
||||
*/
|
||||
export class DatabaseError extends BaseError {
|
||||
constructor(message: string, details?: any, options?: ErrorOptions) {
|
||||
super(message, 'DATABASE_ERROR', details, options)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据库连接错误
|
||||
*/
|
||||
export class DatabaseConnectionError extends DatabaseError {
|
||||
constructor(message: string = '数据库连接失败', details?: any, options?: ErrorOptions) {
|
||||
super(message, details, options)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据库查询错误
|
||||
*/
|
||||
export class DatabaseQueryError extends DatabaseError {
|
||||
constructor(message: string = '数据库查询失败', details?: any, options?: ErrorOptions) {
|
||||
super(message, details, options)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据库更新错误
|
||||
*/
|
||||
export class DatabaseUpdateError extends DatabaseError {
|
||||
constructor(message: string = '数据库更新失败', details?: any, options?: ErrorOptions) {
|
||||
super(message, details, options)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据库插入错误
|
||||
*/
|
||||
export class DatabaseInsertError extends DatabaseError {
|
||||
constructor(message: string = '数据库插入失败', details?: any, options?: ErrorOptions) {
|
||||
super(message, details, options)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user