Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/core/src/extension/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Config } from "../config";
import type { LoggerService } from "../logger";
import { PromptService } from "../prompt";
import { sanitizeDiagnostic } from "../shared/diagnostic-sanitizer";
import { normalizeAppError, fromAppError } from "../shared/errors";
import { getInstanceDirectory, getInstanceScope } from "../shared/instance-scope";
import { YesImBotService } from "../shared/service";
import { isEmpty, stringify, truncate } from "../shared/utils";
Expand Down Expand Up @@ -794,9 +795,10 @@ export class ToolService extends YesImBotService<Config> {
}
} catch (error) {
assertReplyTurn();
this._logger.error(`💥 异常 | 调用 ${functionName} 时出错`, error.message);
this._logger.debug(error.stack);
lastResult = Failed(`Exception: ${error.message}`);
const normalized = normalizeAppError(error);
this._logger.error(`💥 异常 | 调用 ${functionName} 时出错`, { code: normalized.code, errorId: normalized.errorId });
this._logger.debug(sanitizeDiagnostic(normalized));
lastResult = Failed(fromAppError(normalized));
return lastResult;
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/extension/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import { Context, Schema, Session } from "koishi";

import type { ErrorCategory } from "../shared/errors/definitions";

export interface Param {
type: string;
description?: string;
Expand Down Expand Up @@ -77,6 +79,10 @@
message: string;
/** 错误是否可重试 */
retryable?: boolean;
code?: string;
errorId?: string;
category?: ErrorCategory;
suggestion?: string;
}

/**
Expand All @@ -103,7 +109,7 @@
/**
* 扩展包实例需要实现的接口。
*/
export interface IExtension<TConfig = any> extends Object {

Check warning on line 112 in packages/core/src/extension/types.ts

View workflow job for this annotation

GitHub Actions / verify (22)

typescript(no-wrapper-object-types)

packages/core/src/extension/types.ts:112:52: Do not use wrapper object types.

Check warning on line 112 in packages/core/src/extension/types.ts

View workflow job for this annotation

GitHub Actions / verify (24)

typescript(no-wrapper-object-types)

packages/core/src/extension/types.ts:112:52: Do not use wrapper object types.
ctx: Context;
config: TConfig;
metadata: ExtensionMetadata;
Expand Down
48 changes: 48 additions & 0 deletions packages/core/src/shared/errors/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/** Coarse public classification; it does not authorize retries. */
export type ErrorCategory =
| "invalid_input"
| "configuration"
| "permission_denied"
| "not_found"
| "conflict"
| "source_invalid"
| "cancelled"
| "timeout"
| "rate_limited"
| "upstream_failure"
| "internal"
| "outcome_unknown";

/**
* @description 应用程序的统一错误定义
* 每个定义都包含 code (错误码)、message (错误信息) 和给用户的 suggestion (建议)
Expand All @@ -8,48 +23,56 @@ export const ErrorDefinitions = {
LLM: {
BAD_REQUEST: {
code: "LLM.BAD_REQUEST",
category: "invalid_input",
message: "LLM API 请求因格式错误而失败",
suggestion: "请检查您的请求参数,确保它们符合 API 的要求并已正确格式化",
},

INVALID_API_KEY: {
code: "LLM.INVALID_API_KEY",
category: "configuration",
message: "提供了无效的 LLM API 密钥",
suggestion: "请仔细检查您的 API 密钥,确保其在插件配置中已正确设置。如果您使用的是云服务,请确保您有权访问指定的模型",
},

RATE_LIMIT_EXCEEDED: {
code: "LLM.RATE_LIMIT_EXCEEDED",
category: "rate_limited",
message: "LLM API 的请求频率超限",
suggestion: "请稍等片刻再发起请求。如果您使用的是云服务,请考虑升级您的套餐或将请求分散在更长的时间段内",
},

PROVIDER_ERROR: {
code: "LLM.PROVIDER_ERROR",
category: "upstream_failure",
message: "LLM 服务提供商内部发生错误",
suggestion: "请检查服务商的文档以确保其设置正确。如果问题仍然存在,请考虑报告此问题",
},

REQUEST_FAILED: {
code: "LLM.REQUEST_FAILED",
category: "upstream_failure",
message: (details: string) => `LLM API 请求失败:${details}`,
suggestion: "请检查您的网络、API 密钥以及模型提供商的状态页面。这可能是由于频率限制、密钥无效或暂时的服务中断所致",
},

OUTPUT_PARSING_FAILED: {
code: "LLM.OUTPUT_PARSING_FAILED",
category: "upstream_failure",
message: "解析 LLM 响应失败,输出不是有效的 JSON 格式",
suggestion: "这通常是暂时的模型问题,请重试。如果问题持续存在,可能是模型不稳定或系统提示词需要调整以确保生成有效的 JSON",
},

OUTPUT_EMPTY_CONTENT: {
code: "LLM.OUTPUT_EMPTY_CONTENT",
category: "upstream_failure",
message: "LLM 响应为空",
suggestion: "这可能是上游API故障导致,建议联系API提供商检查服务状态",
},

TIMEOUT: {
code: "LLM.TIMEOUT",
category: "timeout",
message: (duration?: number) =>
typeof duration === "number" && Number.isFinite(duration) && duration > 0
? `对 LLM 的请求在 ${duration} 秒后超时`
Expand All @@ -61,21 +84,25 @@ export const ErrorDefinitions = {
CONFIG: {
MISSING: {
code: "CONFIG.MISSING",
category: "configuration",
message: (service: string, component: string) => `服务 '${service}' 中缺少 '${component}' 的配置`,
suggestion: (component: string) => `请确保在插件设置中已正确配置 '${component}'`,
},
MISSING_MODEL_GROUP: {
code: "CONFIG.MISSING_MODEL_GROUP",
category: "configuration",
message: "未给 '聊天 (Chat)' 任务类型配置任何模型组",
suggestion: "代理需要一个聊天模型才能运作。请前往“模型服务”设置,并为 '聊天' 任务类型至少配置一个模型",
},
INVALID: {
code: "CONFIG.INVALID",
category: "configuration",
message: (details: string) => `发现无效配置:${details}`,
suggestion: "请检查插件配置并更正指定的字段。有关有效值,请参阅文档",
},
PROVIDER_INIT_FAILED: {
code: "CONFIG.PROVIDER_INIT_FAILED",
category: "configuration",
message: (providerId: string) => `初始化提供商失败:${providerId}`,
suggestion: "请确保提供商的配置(如 API 密钥和基础 URL)正确无误,并检查日志中是否有相关的错误信息",
},
Expand All @@ -84,6 +111,7 @@ export const ErrorDefinitions = {
TASK: {
EXECUTION_FAILED: {
code: "TASK.EXECUTION_FAILED",
category: "internal",
message: "执行计划任务时发生错误",
suggestion: "这表明代理的处理周期内存在内部错误。请检查详细日志以获取更多信息",
},
Expand All @@ -92,14 +120,25 @@ export const ErrorDefinitions = {
WILLINGNESS: {
CALCULATION_FAILED: {
code: "WILLINGNESS.CALCULATION_FAILED",
category: "internal",
message: "意愿计算失败",
suggestion: "在决定是否回复时发生内部错误。请检查日志以获取更多详情",
},
},
PERSON_MEMORY: {
VERSION_CONFLICT: {
code: "PERSON_MEMORY.VERSION_CONFLICT",
category: "conflict",
retryable: false,
message: (details = "身份或画像版本已变化") => details,
suggestion: "请重新读取人物及账号关联版本,核对后重新提交候选",
},
},
// --- 系统未知错误 ---
SYSTEM: {
UNKNOWN: {
code: "SYSTEM.UNKNOWN",
category: "internal",
message: "发生未知错误",
suggestion: "捕获到意外错误。请检查日志并考虑报告此问题",
},
Expand All @@ -108,26 +147,31 @@ export const ErrorDefinitions = {
MODEL: {
UNAVAILABLE: {
code: "MODEL.UNAVAILABLE",
category: "configuration",
message: (modelId: string) => `无法找到或加载请求的模型 '${modelId}'`,
suggestion: "请验证模型 ID 是否正确,以及对应的提供商是否已启用并正确配置",
},
GROUP_INIT_FAILED: {
code: "MODEL.GROUP_INIT_FAILED",
category: "configuration",
message: (groupName: string) => `模型组 '${groupName}' 初始化失败,因为它不包含任何可用的模型`,
suggestion: "请检查模型组的配置。确保所列模型存在、其提供商已启用,并且它们具备所需的能力(例如 '聊天')",
},
ALL_FAILED_IN_GROUP: {
code: "MODEL.ALL_FAILED_IN_GROUP",
category: "upstream_failure",
message: (groupName: string) => `模型组 '${groupName}' 中的所有模型都未能处理请求`,
suggestion: "这表明存在普遍性问题。请检查错误报告中的 'cause' 以了解单个模型的失败原因。这可能是网络问题或影响组内所有模型的问题",
},
RETRY_EXHAUSTED: {
code: "MODEL.RETRY_EXHAUSTED",
category: "upstream_failure",
message: (modelId: string) => `模型 '${modelId}' 的所有重试次数已用尽`,
suggestion: "该模型反复失败。请检查错误日志以找出根本原因(例如,网络问题、持续的 API 错误)",
},
NO_SUITABLE_MODEL: {
code: "MODEL.NO_SUITABLE_MODEL",
category: "configuration",
message: (groupName: string) => `在模型组 '${groupName}' 中未找到合适的模型`,
suggestion: "请检查模型组的配置。确保所列模型存在、其提供商已启用,并且它们具备所需的能力(例如 '聊天')",
},
Expand All @@ -136,6 +180,7 @@ export const ErrorDefinitions = {
NETWORK: {
REQUEST_FAILED: {
code: "NETWORK.REQUEST_FAILED",
category: "upstream_failure",
message: "网络请求失败",
suggestion: "请检查您服务器的互联网连接和 DNS 设置。如果您正在使用代理,请确保其配置正确且正在运行",
},
Expand All @@ -144,16 +189,19 @@ export const ErrorDefinitions = {
MEMORY: {
PROVIDER_ERROR: {
code: "MEMORY.PROVIDER_ERROR",
category: "upstream_failure",
message: "记忆提供商发生错误",
suggestion: "请检查记忆提供商的配置并确保其设置正确。如果问题仍然存在,请考虑报告此问题",
},
SEARCH_FAILED: {
code: "MEMORY.SEARCH_FAILED",
category: "internal",
message: "搜索记忆失败",
suggestion: "这可能是由于内部错误。请检查日志以获取更多详情。如果问题仍然存在,请考虑报告此问题",
},
EMBEDDING_FAILED: {
code: "MEMORY.EMBEDDING_FAILED",
category: "upstream_failure",
message: "为记忆生成嵌入向量失败",
suggestion: "这可能是由于内部错误。请检查日志以获取更多详情。如果问题仍然存在,请考虑报告此问题",
},
Expand Down
104 changes: 81 additions & 23 deletions packages/core/src/shared/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Logger, Schema } from "koishi";
import { v4 as uuidv4 } from "uuid";

import type { ToolError } from "../../extension/types";
import { sanitizeDiagnostic, redactDiagnosticText } from "../../shared/diagnostic-sanitizer";
import { truncate } from "../../shared/utils";
import { ErrorDefinitions } from "./definitions";
import { ErrorDefinitions, type ErrorCategory } from "./definitions";
import { diagnosticId, diagnosticCode, summarizeContext, summarizeError } from "./report-summary";

// --- 错误上报模块 ---
Expand Down Expand Up @@ -120,7 +121,7 @@ export class ErrorReporter {
}

private formatErrorDump(context: ReportContext): string {
const error = context.error instanceof AppError ? context.error : new AppError(ErrorDefinitions.SYSTEM.UNKNOWN, { cause: context.error });
const error = normalizeAppError(context.error);
// Credential filtering includes nested Error fields and additionalInfo before
// applying the stricter outbound content policy. Neither step mutates inputs.
const detailed = !!this.config.includeSensitiveInfo;
Expand Down Expand Up @@ -178,24 +179,29 @@ export function initializeErrorReporter(
globalErrorReporter = new ErrorReporter(config, logger, saveLocal, knownSecrets);
}

type ErrorDomains = keyof typeof ErrorDefinitions;

export type ErrorDefinitionValue = {
[K in ErrorDomains]: (typeof ErrorDefinitions)[K][keyof (typeof ErrorDefinitions)[K]];
}[ErrorDomains];
export interface ErrorDefinitionValue {
code: string;
message: string | ((...args: any[]) => string);
suggestion: string | ((...args: any[]) => string);
category?: ErrorCategory;
retryable?: boolean;
}

export class AppError extends Error {
public readonly code: string;
public readonly suggestion: string;
public readonly errorId: string;
public readonly category: ErrorCategory;
public readonly retryable: boolean;

public context?: Record<string, any>;

constructor(
definition: ErrorDefinitionValue,
options?: {
context?: Record<string, any>;
cause?: Error;
cause?: unknown;
errorId?: string;
args?: any[];
},
) {
Expand All @@ -220,7 +226,9 @@ export class AppError extends Error {
this.code = definition.code;
this.suggestion = suggestion;
this.context = options?.context;
this.errorId = uuidv4();
this.errorId = options?.errorId ?? uuidv4();
this.category = definition.category ?? "internal";
this.retryable = definition.retryable ?? false;

Object.setPrototypeOf(this, AppError.prototype);
}
Expand All @@ -239,20 +247,7 @@ export class AppError extends Error {
* @returns 返回生成的唯一错误 ID
*/
export function handleError(logger: Logger, error: unknown, contextDescription: string): string {
let appError: AppError;

// 步骤 1: 确保错误是 AppError 类型
// 如果捕获到的不是 AppError,则将其包装成一个通用的系统未知错误,以便统一处理
if (error instanceof AppError) {
appError = error;
} else {
// 保留原始错误信息作为排查线索
const cause = error instanceof Error ? error : undefined;
appError = new AppError(ErrorDefinitions.SYSTEM.UNKNOWN, {
cause,
context: { capturedValue: error },
});
}
const appError = normalizeAppError(error);

const { errorId, message, suggestion, context, stack } = appError;

Expand Down Expand Up @@ -301,3 +296,66 @@ export function handleError(logger: Logger, error: unknown, contextDescription:
}

export * from "./definitions";

const errorCatalog = new Map<string, ErrorDefinitionValue>(
Object.values(ErrorDefinitions).flatMap((domain) => Object.values(domain).map((definition) => [definition.code, definition] as const)),
);
const boundedText = (value: unknown, limit: number): value is string => typeof value === "string" && value.length > 0 && value.length <= limit;

/** The validated fields available across package copies or serialized transport. */
export interface AppErrorEnvelope {
name: "AppError";
code: string;
message: string;
suggestion: string;
errorId: string;
category?: ErrorCategory;
retryable?: boolean;
}

/** Validates a transported/duplicate-package AppError envelope, never a bare code. */
export function isAppError(value: unknown): value is AppError | AppErrorEnvelope {
if (value instanceof AppError) return true;
if (!value || typeof value !== "object") return false;
const error = value as Record<string, unknown>;
const definition = typeof error.code === "string" ? errorCatalog.get(error.code) : undefined;
return (
!!definition &&
error.name === "AppError" &&
boundedText(error.message, 4096) &&
boundedText(error.suggestion, 4096) &&
typeof error.errorId === "string" &&
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(error.errorId) &&
(error.category === undefined || error.category === definition.category) &&
(error.retryable === undefined || error.retryable === ("retryable" in definition ? definition.retryable : false))
);
}

/** Internal normalization retains the original thrown value as cause. */
export function normalizeAppError(error: unknown): AppError {
if (isAppError(error)) {
if (error instanceof AppError) return error;
return new AppError(errorCatalog.get(error.code)!, { errorId: error.errorId, cause: error });
}
return new AppError(ErrorDefinitions.SYSTEM.UNKNOWN, { cause: error });
}

/** Public allowlist projection. Dynamic diagnostic arguments never cross this boundary. */
export function fromAppError(error: AppError): ToolError {
const normalized = normalizeAppError(error);
const definition = errorCatalog.get(normalized.code) ?? ErrorDefinitions.SYSTEM.UNKNOWN;
return {
name: "AppError",
code: definition.code,
message: typeof definition.message === "string" ? definition.message : `操作失败(${definition.code})`,
suggestion: typeof definition.suggestion === "string" ? definition.suggestion : "请检查配置与日志后重试",
errorId: normalized.errorId,
category: definition.category,
retryable: "retryable" in definition ? definition.retryable : false,
};
}

/** Normalize a thrown value; explicit legacy ToolError results continue through Failed unchanged. */
export function toToolError(error: unknown): ToolError {
return fromAppError(normalizeAppError(error));
}
Loading
Loading