fix(agent): handle task failure by displaying user-friendly error messages in conversation flow

This commit is contained in:
HouYunFei
2026-07-29 13:20:31 +08:00
parent 0f5eb1fbcb
commit ed32e44e69
6 changed files with 34 additions and 7 deletions
+11
View File
@@ -26,6 +26,7 @@ export function threadMessages(thread: unknown, planUpdates: CodexPlanUpdate[] =
const messages: AgentHistoryMessage[] = [];
turns.forEach((turn, turnIndex) => {
const turnId = String(field(turn, "id") || turnIndex);
const turnError = String(field(field(turn, "error"), "message") || "").trim();
const planMessage = structuredPlanMessage(plansByTurn.get(turnId) || { threadId: "", turnId, explanation: stringOrNull(field(turn, "explanation")), plan: arrayValue(field(turn, "plan")) as CodexPlanUpdate["plan"], turnStatus: String(field(turn, "status") || "") });
let planAdded = false;
arrayValue(field(turn, "items")).forEach((item, itemIndex) => {
@@ -72,6 +73,10 @@ export function threadMessages(thread: unknown, planUpdates: CodexPlanUpdate[] =
if (type === "collabToolCall") messages.push({ id, role: "tool", title: "协作处理", text: "已完成协作任务", detail: { kind: "tool", status: field(item, "status") } });
});
if (planMessage && !planAdded) messages.push(planMessage);
if (turnError) {
const error = userFacingCodexError(turnError);
messages.push({ id: `error-${turnId}`, role: "error", title: error.title, text: error.text });
}
});
return messages.filter((item) => item.text).slice(-120);
}
@@ -101,6 +106,12 @@ function planStatus(tasks: Array<{ status: string }>, turnStatus?: string) {
return turnStatus === "completed" ? "finished" : "inProgress";
}
/** 将常见 Codex 错误转换为普通用户可理解的提示。 */
function userFacingCodexError(message: string) {
if (/selected model is at capacity/i.test(message)) return { title: "模型暂时繁忙", text: "当前选择的模型请求量过大,暂时无法处理。请稍后重试,或切换其他模型后再试。" };
return { title: "任务失败", text: message || "Codex 未能完成本次任务,请稍后重试。" };
}
/** 提取用户输入条目中的文本与附件占位信息。 */
function userInputText(content: unknown) {
return arrayValue(content)