fix(world): harden world task create external-marble fallback
This commit is contained in:
parent
fd49843c5a
commit
e601f8079f
@ -200,6 +200,22 @@ function buildExternalWorldTaskPayload(
|
|||||||
return payload;
|
return payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function buildOfficialWorldTaskPayload(
|
||||||
|
displayName: string,
|
||||||
|
worldPrompt: Record<string, unknown>,
|
||||||
|
officialMarbleModel: string,
|
||||||
|
body: CreateWorldTaskRequest
|
||||||
|
) {
|
||||||
|
return {
|
||||||
|
display_name: displayName,
|
||||||
|
model: officialMarbleModel,
|
||||||
|
permission: {
|
||||||
|
public: body.makePublic === true,
|
||||||
|
},
|
||||||
|
world_prompt: worldPrompt,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function buildWorldPrompt(body: CreateWorldTaskRequest, mode: WorldTaskMode) {
|
function buildWorldPrompt(body: CreateWorldTaskRequest, mode: WorldTaskMode) {
|
||||||
const textPrompt = readOptionalTrimmedText(body.prompt);
|
const textPrompt = readOptionalTrimmedText(body.prompt);
|
||||||
|
|
||||||
@ -404,6 +420,7 @@ export async function POST(request: Request) {
|
|||||||
try {
|
try {
|
||||||
const useExternalTaskApi = shouldUseExternalTaskCreateApi(candidate, mode);
|
const useExternalTaskApi = shouldUseExternalTaskCreateApi(candidate, mode);
|
||||||
assertWorldModeSupportedByCandidate(candidate, mode);
|
assertWorldModeSupportedByCandidate(candidate, mode);
|
||||||
|
let requestUsedExternalTaskApi = false;
|
||||||
if (useExternalTaskApi) {
|
if (useExternalTaskApi) {
|
||||||
const payload = buildExternalWorldTaskPayload(
|
const payload = buildExternalWorldTaskPayload(
|
||||||
mode,
|
mode,
|
||||||
@ -411,32 +428,64 @@ export async function POST(request: Request) {
|
|||||||
readOptionalTrimmedText(body.prompt),
|
readOptionalTrimmedText(body.prompt),
|
||||||
displayName,
|
displayName,
|
||||||
);
|
);
|
||||||
const externalResult = await worldLabsRequest<
|
try {
|
||||||
WorldLabsExternalTaskResponse<WorldLabsExternalTaskCreateData>
|
const externalResult = await worldLabsRequest<
|
||||||
>(candidate, {
|
WorldLabsExternalTaskResponse<WorldLabsExternalTaskCreateData>
|
||||||
method: "POST",
|
>(candidate, {
|
||||||
path: "/api/external/task/generate",
|
method: "POST",
|
||||||
timeoutMs: resolveWorldLabsCreateTimeoutMs(),
|
path: "/api/external/task/generate",
|
||||||
json: payload,
|
timeoutMs: resolveWorldLabsCreateTimeoutMs(),
|
||||||
});
|
json: payload,
|
||||||
result = normalizeExternalTaskCreateOperation(externalResult);
|
});
|
||||||
|
result = normalizeExternalTaskCreateOperation(externalResult);
|
||||||
|
requestUsedExternalTaskApi = true;
|
||||||
|
} catch (error) {
|
||||||
|
const externalError = error instanceof Error ? error.message : String(error);
|
||||||
|
logger.warn("external world create failed, fallback to marble endpoint", {
|
||||||
|
providerId: candidate.providerId || null,
|
||||||
|
error: externalError,
|
||||||
|
});
|
||||||
|
if (isCredentialOrBillingProviderError(error)) {
|
||||||
|
await reportWorldLabsProviderRuntimeFailure(
|
||||||
|
candidate.providerId,
|
||||||
|
externalError
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
result = await worldLabsRequest<WorldLabsOperation>(candidate, {
|
||||||
|
method: "POST",
|
||||||
|
path: "/marble/v1/worlds:generate",
|
||||||
|
timeoutMs: resolveWorldLabsCreateTimeoutMs(),
|
||||||
|
json: buildOfficialWorldTaskPayload(
|
||||||
|
displayName,
|
||||||
|
worldPrompt,
|
||||||
|
officialMarbleModel,
|
||||||
|
body
|
||||||
|
),
|
||||||
|
});
|
||||||
|
requestUsedExternalTaskApi = false;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
requestUsedExternalTaskApi = false;
|
||||||
result = await worldLabsRequest<WorldLabsOperation>(candidate, {
|
result = await worldLabsRequest<WorldLabsOperation>(candidate, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
path: "/marble/v1/worlds:generate",
|
path: "/marble/v1/worlds:generate",
|
||||||
timeoutMs: resolveWorldLabsCreateTimeoutMs(),
|
timeoutMs: resolveWorldLabsCreateTimeoutMs(),
|
||||||
json: {
|
json: buildOfficialWorldTaskPayload(
|
||||||
display_name: displayName,
|
displayName,
|
||||||
model: officialMarbleModel,
|
worldPrompt,
|
||||||
permission: {
|
officialMarbleModel,
|
||||||
public: body.makePublic === true,
|
body
|
||||||
},
|
),
|
||||||
world_prompt: worldPrompt,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!result?.operation_id) {
|
||||||
|
throw new Error("创建 World Labs 世界任务返回缺少 operation_id");
|
||||||
|
}
|
||||||
|
|
||||||
usedProvider = candidate;
|
usedProvider = candidate;
|
||||||
usedExternalTaskApi = useExternalTaskApi;
|
usedExternalTaskApi = requestUsedExternalTaskApi;
|
||||||
providerForLog = candidate.providerId || null;
|
providerForLog = candidate.providerId || null;
|
||||||
break;
|
break;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user