fix(design): preserve command outcome certainty

This commit is contained in:
2026-09-03 18:25:06 +08:00
parent 4d8b19eeb5
commit a16dfd0d6f
15 changed files with 576 additions and 23 deletions

View File

@@ -112,14 +112,23 @@ async function parseResponse<T>(response: Response): Promise<T> {
if (!response.ok) {
let message = `${response.status} ${response.statusText}`;
let backendCode: string | undefined;
let commandOutcome: string | undefined;
try {
const payload = await response.json() as { error?: string; code?: unknown };
const payload = await response.json() as {
error?: string;
code?: unknown;
commandOutcome?: unknown;
};
if (payload?.error) {
message = payload.error;
}
if (typeof payload?.code === 'string' && /^[a-z][a-z0-9_]{0,63}$/u.test(payload.code)) {
backendCode = payload.code;
}
if (payload?.commandOutcome === 'definitive_failure'
|| payload?.commandOutcome === 'unknown') {
commandOutcome = payload.commandOutcome;
}
} catch {
// ignore body parse failure
}
@@ -127,6 +136,7 @@ async function parseResponse<T>(response: Response): Promise<T> {
source: 'browser-fallback',
status: response.status,
...(backendCode ? { backendCode } : {}),
...(commandOutcome ? { commandOutcome } : {}),
});
}
@@ -155,6 +165,9 @@ function createProxyHttpError(data: HostApiProxyData): Error {
return normalizeAppError(new Error(message), {
status: data.status,
...(payload && typeof payload.code === 'string' ? { backendCode: payload.code } : {}),
...(payload?.commandOutcome === 'definitive_failure' || payload?.commandOutcome === 'unknown'
? { commandOutcome: payload.commandOutcome }
: {}),
});
}

View File

@@ -12,6 +12,7 @@ import {
designAssetDownloadPath,
type DesignAsset,
type DesignAssetSaveResult,
type DesignCommandFailureOutcome,
type DesignCommandInput,
type DesignCommandResult,
type DesignDeleteWorkspaceResult,
@@ -24,6 +25,7 @@ type ImageWorkspaceEnvelope<T> = {
status?: number;
code?: string;
error?: string;
commandOutcome?: DesignCommandFailureOutcome;
data?: T;
};
@@ -32,12 +34,19 @@ export const IMAGE_WORKSPACE_CREATE_PROJECT_EVENT = 'niancode:image-workspace:cr
export class ImageWorkspaceApiError extends Error {
readonly status: number;
readonly code: string;
readonly commandOutcome?: DesignCommandFailureOutcome;
constructor(status: number, code: string, message: string) {
constructor(
status: number,
code: string,
message: string,
commandOutcome?: DesignCommandFailureOutcome,
) {
super(message);
this.name = 'ImageWorkspaceApiError';
this.status = status;
this.code = code;
this.commandOutcome = commandOutcome;
}
}
@@ -66,6 +75,11 @@ function getErrorCode(error: unknown, status: number): string {
return 'IMAGE_WORKSPACE_REQUEST_FAILED';
}
function getCommandOutcome(error: unknown): DesignCommandFailureOutcome | undefined {
const outcome = error instanceof AppError ? error.details?.commandOutcome : undefined;
return outcome === 'definitive_failure' || outcome === 'unknown' ? outcome : undefined;
}
async function requestData<T>(path: string, init: RequestInit = {}): Promise<T> {
let response: ImageWorkspaceEnvelope<T>;
try {
@@ -76,6 +90,7 @@ async function requestData<T>(path: string, init: RequestInit = {}): Promise<T>
status,
getErrorCode(error, status),
error instanceof Error ? error.message : IMAGE_WORKSPACE_UNAVAILABLE_MESSAGE,
getCommandOutcome(error),
);
}
@@ -84,6 +99,7 @@ async function requestData<T>(path: string, init: RequestInit = {}): Promise<T>
response.status ?? 502,
response.code ?? 'IMAGE_WORKSPACE_REQUEST_FAILED',
response.error ?? 'AI 设计请求失败',
response.commandOutcome,
);
}
return response.data;