fix(agentrun): 完善重试命令发现与错误输出
This commit is contained in:
@@ -57,9 +57,17 @@ export function agentRunGetKindHelp(kindRaw: string): string {
|
||||
export async function runAgentRunResourceCommand(config: UniDeskConfig | null, verb: AgentRunResourceVerb, action: string | undefined, actionArgs: string[], canonicalArgs: string[]): Promise<RenderedCliResult> {
|
||||
if (isHelpArg(action) || actionArgs.some(isHelpArg)) return renderAgentRunHelp(canonicalArgs);
|
||||
const resourceArgs = action === undefined ? actionArgs : [action, ...actionArgs];
|
||||
const options = parseResourceOptions(resourceArgs);
|
||||
const bridgeActionArgs = stripAgentRunResourceWrapperArgs(actionArgs);
|
||||
const command = `agentrun ${canonicalArgs.join(" ")}`.trim();
|
||||
let options: AgentRunResourceOptions;
|
||||
try {
|
||||
options = parseResourceOptions(resourceArgs);
|
||||
} catch (error) {
|
||||
const validationError = error instanceof AgentRunRestError
|
||||
? error
|
||||
: new AgentRunRestError("validation-failed", error instanceof Error ? error.message : String(error));
|
||||
return renderAgentRunRestError(command, validationError, resourceErrorOutputOptions(resourceArgs));
|
||||
}
|
||||
const bridgeActionArgs = stripAgentRunResourceWrapperArgs(actionArgs);
|
||||
try {
|
||||
return await withAgentRunRestTarget(resolveAgentRunRestTarget(config, options), async () => {
|
||||
if (verb === "explain") return renderedCliResult(true, command, agentRunExplain(action ?? "task", resourceArgs, options));
|
||||
@@ -84,6 +92,21 @@ export async function runAgentRunResourceCommand(config: UniDeskConfig | null, v
|
||||
return renderedCliResult(false, command, `Unsupported AgentRun resource command. Try: bun scripts/cli.ts agentrun --help`);
|
||||
}
|
||||
|
||||
function resourceErrorOutputOptions(args: string[]): AgentRunResourceOptions {
|
||||
const options = parseResourceOptions([]);
|
||||
options.raw = args.includes("--raw");
|
||||
for (let index = 0; index < args.length; index += 1) {
|
||||
const arg = args[index] ?? "";
|
||||
const value = arg === "-o" || arg === "--output"
|
||||
? args[index + 1]
|
||||
: arg.startsWith("--output=") || arg.startsWith("-o=")
|
||||
? arg.slice(arg.indexOf("=") + 1)
|
||||
: null;
|
||||
if (value === "json" || value === "yaml") options.output = value;
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
||||
export function stripAgentRunResourceWrapperArgs(args: string[]): string[] {
|
||||
const result: string[] = [];
|
||||
for (let index = 0; index < args.length; index += 1) {
|
||||
@@ -224,7 +247,8 @@ export async function resourceGet(config: UniDeskConfig | null, command: string,
|
||||
result = await runAgentRunRestCommand(config, "sessions", ["ps", "--limit", String(options.limit)]);
|
||||
return renderResourceResult(command, result, options, "Session", normalizeSessionItems(innerData(result)).slice(0, options.limit));
|
||||
}
|
||||
if (kind === "attempt" && options.taskId !== null) {
|
||||
if (kind === "attempt") {
|
||||
if (options.taskId === null) throw new AgentRunRestError("validation-failed", "get attempts requires --task <taskId>");
|
||||
result = await runAgentRunRestCommand(config, "queue", [
|
||||
"attempts",
|
||||
options.taskId,
|
||||
@@ -255,17 +279,23 @@ export async function resourceGet(config: UniDeskConfig | null, command: string,
|
||||
}
|
||||
|
||||
export async function resourceRetry(config: UniDeskConfig | null, command: string, action: string | undefined, args: string[], options: AgentRunResourceOptions): Promise<RenderedCliResult> {
|
||||
const ref = parseResourceRef(action, args, "task");
|
||||
if (ref.kind !== "task") throw new Error("retry supports task/<taskId>");
|
||||
const idempotencyKey = options.idempotencyKey ?? requiredContext("retry", "--idempotency-key <key>");
|
||||
const reason = options.reason ?? requiredContext("retry", "--reason <text>");
|
||||
if (action === undefined || action.startsWith("-")) throw new AgentRunRestError("validation-failed", "retry requires task/<taskId>");
|
||||
let ref: AgentRunResourceRef;
|
||||
try {
|
||||
ref = parseResourceRef(action, args, "task");
|
||||
} catch {
|
||||
throw new AgentRunRestError("validation-failed", "retry requires task/<taskId>");
|
||||
}
|
||||
if (ref.kind !== "task") throw new AgentRunRestError("validation-failed", "retry supports task/<taskId>");
|
||||
if (options.idempotencyKey === null) throw new AgentRunRestError("validation-failed", "retry requires --idempotency-key <key>");
|
||||
if (options.reason === null) throw new AgentRunRestError("validation-failed", "retry requires --reason <text>");
|
||||
const result = await runAgentRunRestCommand(config, "queue", [
|
||||
"retry",
|
||||
ref.name,
|
||||
"--idempotency-key",
|
||||
idempotencyKey,
|
||||
options.idempotencyKey,
|
||||
"--reason",
|
||||
reason,
|
||||
options.reason,
|
||||
...(options.dryRun ? ["--dry-run"] : []),
|
||||
]);
|
||||
return renderQueueRetrySummary(command, result, options, ref.name);
|
||||
|
||||
Reference in New Issue
Block a user