fix: 修复 CLI repo 与状态别名解析
This commit is contained in:
+40
-3
@@ -34,13 +34,50 @@ export function emitJson<T>(command: string, data: T, ok = true): void {
|
||||
}
|
||||
|
||||
export function emitError(command: string, error: unknown): void {
|
||||
const payload = error instanceof Error
|
||||
? { name: error.name, message: error.message, stack: error.stack ?? null }
|
||||
: { message: String(error) };
|
||||
const payload = normalizeErrorPayload(command, error);
|
||||
const envelope: JsonEnvelope<never> = { ok: false, command, error: payload };
|
||||
safeStdoutWrite(renderEnvelope(command, envelope));
|
||||
}
|
||||
|
||||
function normalizeErrorPayload(command: string, error: unknown): Record<string, unknown> {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
const parsed = parseStructuredErrorMessage(command, message);
|
||||
if (parsed !== null) return parsed;
|
||||
if (shouldSuppressStack(command) || shouldSuppressStack(commandPrefixFromMessage(message))) {
|
||||
return { message };
|
||||
}
|
||||
if (error instanceof Error) {
|
||||
const debug = process.env.UNIDESK_CLI_DEBUG === "1" || process.env.UNIDESK_CLI_FULL_ERROR === "1" || process.env.UNIDESK_CLI_RAW_ERROR === "1";
|
||||
return { name: error.name, message, stack: error.stack ?? null, ...(debug ? { debug: true } : {}) };
|
||||
}
|
||||
return { message };
|
||||
}
|
||||
|
||||
function parseStructuredErrorMessage(command: string, message: string): Record<string, unknown> | null {
|
||||
if (!shouldSuppressStack(command) && !shouldSuppressStack(commandPrefixFromMessage(message))) return null;
|
||||
const jsonStart = message.indexOf("{");
|
||||
if (jsonStart === -1) return null;
|
||||
try {
|
||||
const parsed = JSON.parse(message.slice(jsonStart)) as unknown;
|
||||
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
|
||||
const record = parsed as Record<string, unknown>;
|
||||
return { message: message.slice(0, jsonStart).trim().replace(/[;:]$/u, ""), ...record };
|
||||
}
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function commandPrefixFromMessage(message: string): string {
|
||||
return message.split(/\s+/u).slice(0, 2).join(" ");
|
||||
}
|
||||
|
||||
function shouldSuppressStack(prefix: string): boolean {
|
||||
if (process.env.UNIDESK_CLI_DEBUG === "1" || process.env.UNIDESK_CLI_FULL_ERROR === "1" || process.env.UNIDESK_CLI_RAW_ERROR === "1") return false;
|
||||
return prefix === "codex tasks" || prefix.startsWith("codex tasks ") || prefix === "codex unread" || prefix.startsWith("codex unread ");
|
||||
}
|
||||
|
||||
function renderEnvelope<T>(command: string, envelope: JsonEnvelope<T>): string {
|
||||
const fullText = `${JSON.stringify(envelope, null, 2)}\n`;
|
||||
if (!shouldDumpLargeOutput(command, fullText)) return fullText;
|
||||
|
||||
Reference in New Issue
Block a user