fix(agentrun): 完善重试命令发现与错误输出

This commit is contained in:
Codex
2026-07-12 02:55:57 +02:00
parent 9276e751f5
commit c4f3724430
4 changed files with 219 additions and 17 deletions
+31 -6
View File
@@ -1059,8 +1059,10 @@ export async function withAgentRunRestTarget<T>(target: AgentRunRestTarget | nul
}
export function renderAgentRunRestError(command: string, error: AgentRunRestError, options: AgentRunResourceOptions): RenderedCliResult {
const payload = error.toPayload(command);
if (options.raw || options.output === "json" || options.output === "yaml") return renderMachine(command, payload, options.output === "yaml" ? "yaml" : "json", false);
const rawPayload = error.toPayload(command);
if (options.raw) return renderMachine(command, rawPayload, "json", false);
const payload = boundedAgentRunRestErrorPayload(rawPayload);
if (options.output === "json" || options.output === "yaml") return renderMachine(command, payload, options.output === "yaml" ? "yaml" : "json", false);
const laneHint = record(record(payload.agentrun).laneAwareLookup);
const currentEndpoint = record(laneHint.currentEndpoint);
const nextCommands = Array.isArray(laneHint.nextCommands)
@@ -1075,12 +1077,18 @@ export function renderAgentRunRestError(command: string, error: AgentRunRestErro
` Config: ${String(currentEndpoint.configPath ?? "(unknown)")}`,
` ${String(laneHint.summary ?? "The resource was not found on the currently configured AgentRun manager endpoint.")}`,
];
const validationHelp = `bun scripts/cli.ts agentrun ${command.split(/\s+/u)[1] ?? "--help"} --help`;
const nextLines = nextCommands.length > 0
? nextCommands
: [
"Check config/agentrun.yaml manager.baseUrl and the AgentRun API route.",
"Verify HWLAB_API_KEY is present in env or in the configured auth.file without printing the key.",
];
: error.failureKind === "validation-failed"
? [
"Review the AgentRun validation message and correct the request; do not bypass the formal resource API.",
validationHelp,
]
: [
"Check config/agentrun.yaml manager.baseUrl and the AgentRun API route.",
"Verify HWLAB_API_KEY is present in env or in the configured auth.file without printing the key.",
];
return renderedCliResult(false, command, [
`Error: ${payload.failureKind}`,
String(payload.message ?? ""),
@@ -1091,6 +1099,23 @@ export function renderAgentRunRestError(command: string, error: AgentRunRestErro
].join("\n"));
}
export function boundedAgentRunRestErrorPayload(payload: Record<string, unknown>): Record<string, unknown> {
return boundedAgentRunRestErrorValue(payload, 0) as Record<string, unknown>;
}
function boundedAgentRunRestErrorValue(value: unknown, depth: number): unknown {
if (typeof value === "string") return truncateOneLine(value, 400);
if (value === null || typeof value === "number" || typeof value === "boolean") return value;
if (Array.isArray(value)) return value.slice(0, 12).map((item) => boundedAgentRunRestErrorValue(item, depth + 1));
if (typeof value !== "object") return String(value);
if (depth >= 5) return "[nested details omitted]";
const entries = Object.entries(value as Record<string, unknown>);
const result: Record<string, unknown> = {};
for (const [key, item] of entries.slice(0, 20)) result[key] = boundedAgentRunRestErrorValue(item, depth + 1);
if (entries.length > 20) result.omittedFieldCount = entries.length - 20;
return result;
}
export class AgentRunRestError extends Error {
readonly failureKind: AgentRunFailureKind;
readonly bridge: Record<string, unknown> | null;