@agentkai/core
Version:
AgentKai核心包,提供AI助手系统的基础功能
72 lines (71 loc) • 1.7 kB
JavaScript
/**
* 应用错误基类
*/
export class AppError extends Error {
constructor(message, code = 'UNKNOWN_ERROR') {
super(message);
Object.defineProperty(this, "code", {
enumerable: true,
configurable: true,
writable: true,
value: code
});
this.name = this.constructor.name;
}
}
/**
* 工具错误
*/
export class ToolError extends AppError {
constructor(message, code = 'TOOL_ERROR', toolId) {
super(message, code);
Object.defineProperty(this, "toolId", {
enumerable: true,
configurable: true,
writable: true,
value: toolId
});
}
}
/**
* 记忆系统错误
*/
export class MemoryError extends AppError {
constructor(message, code = 'MEMORY_ERROR') {
super(message, code);
}
}
/**
* 目标系统错误
*/
export class GoalError extends AppError {
constructor(message, code = 'GOAL_ERROR', goalId) {
super(message, code);
Object.defineProperty(this, "goalId", {
enumerable: true,
configurable: true,
writable: true,
value: goalId
});
}
}
/**
* 模型错误
*/
export class ModelError extends AppError {
constructor(message, code = 'MODEL_ERROR') {
super(message, code);
}
}
/**
* 包装未知错误为AppError
*/
export function wrapError(error, defaultMessage = '发生未知错误') {
if (error instanceof AppError) {
return error;
}
if (error instanceof Error) {
return new AppError(error.message);
}
return new AppError(typeof error === 'string' ? error : defaultMessage);
}