refactor: 收口 runtime client 请求骨架

This commit is contained in:
2026-06-03 15:58:59 +08:00
parent 5783bfeea6
commit 4a185ac8c2
7 changed files with 300 additions and 124 deletions

View File

@@ -6,14 +6,11 @@ import type {
} from '../../../packages/shared/src/contracts/squareHoleRuntime';
import {
type ApiRetryOptions,
requestJson,
} from '../apiClient';
import {
buildRuntimeGuestAuthOptions,
buildRuntimeGuestHeaders,
type RuntimeGuestRequestOptions,
} from '../runtimeGuestAuth';
import { type RuntimeGuestRequestOptions } from '../runtimeGuestAuth';
import { buildRuntimeApiPath, requestRuntimeJson } from '../runtimeRequest';
const SQUARE_HOLE_RUNTIME_API_BASE = '/api/runtime/square-hole';
const SQUARE_HOLE_RUNTIME_READ_RETRY: ApiRetryOptions = {
maxRetries: 1,
baseDelayMs: 120,
@@ -34,34 +31,30 @@ export function startSquareHoleRun(
profileId: string,
options: SquareHoleRuntimeRequestOptions = {},
) {
const requestOptions = buildRuntimeGuestAuthOptions(options);
return requestJson<SquareHoleRunResponse>(
`/api/runtime/square-hole/works/${encodeURIComponent(profileId)}/runs`,
{
method: 'POST',
headers: buildRuntimeGuestHeaders(options, {
'Content-Type': 'application/json',
}),
body: JSON.stringify({ profileId }),
},
'启动方洞挑战失败',
{
retry: SQUARE_HOLE_RUNTIME_WRITE_RETRY,
...requestOptions,
},
);
return requestRuntimeJson<SquareHoleRunResponse>({
url: buildRuntimeApiPath(
SQUARE_HOLE_RUNTIME_API_BASE,
'works',
profileId,
'runs',
),
method: 'POST',
jsonBody: { profileId },
fallbackMessage: '启动方洞挑战失败',
retry: SQUARE_HOLE_RUNTIME_WRITE_RETRY,
requestOptions: options,
});
}
/**
* 读取方洞挑战运行态快照。
*/
export function getSquareHoleRun(runId: string) {
return requestJson<SquareHoleRunResponse>(
`/api/runtime/square-hole/runs/${encodeURIComponent(runId)}`,
{ method: 'GET' },
'读取方洞挑战运行快照失败',
{ retry: SQUARE_HOLE_RUNTIME_READ_RETRY },
);
return requestRuntimeJson<SquareHoleRunResponse>({
url: buildRuntimeApiPath(SQUARE_HOLE_RUNTIME_API_BASE, 'runs', runId),
fallbackMessage: '读取方洞挑战运行快照失败',
retry: SQUARE_HOLE_RUNTIME_READ_RETRY,
});
}
/**
@@ -71,19 +64,21 @@ export function dropSquareHoleShape(
runId: string,
payload: DropSquareHoleShapeRequest,
) {
return requestJson<SquareHoleDropResponse>(
`/api/runtime/square-hole/runs/${encodeURIComponent(runId)}/drop`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
...payload,
runId: payload.runId ?? runId,
}),
return requestRuntimeJson<SquareHoleDropResponse>({
url: buildRuntimeApiPath(
SQUARE_HOLE_RUNTIME_API_BASE,
'runs',
runId,
'drop',
),
method: 'POST',
jsonBody: {
...payload,
runId: payload.runId ?? runId,
},
'确认方洞挑战投入失败',
{ retry: SQUARE_HOLE_RUNTIME_WRITE_RETRY },
);
fallbackMessage: '确认方洞挑战投入失败',
retry: SQUARE_HOLE_RUNTIME_WRITE_RETRY,
});
}
/**
@@ -95,40 +90,47 @@ export function stopSquareHoleRun(
clientActionId: `square-hole-stop-${Date.now()}`,
},
) {
return requestJson<SquareHoleRunResponse>(
`/api/runtime/square-hole/runs/${encodeURIComponent(runId)}/stop`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
},
'停止方洞挑战失败',
{ retry: SQUARE_HOLE_RUNTIME_WRITE_RETRY },
);
return requestRuntimeJson<SquareHoleRunResponse>({
url: buildRuntimeApiPath(SQUARE_HOLE_RUNTIME_API_BASE, 'runs', runId, 'stop'),
method: 'POST',
jsonBody: payload,
fallbackMessage: '停止方洞挑战失败',
retry: SQUARE_HOLE_RUNTIME_WRITE_RETRY,
});
}
/**
* 基于当前 run 重开一局。
*/
export function restartSquareHoleRun(runId: string) {
return requestJson<SquareHoleRunResponse>(
`/api/runtime/square-hole/runs/${encodeURIComponent(runId)}/restart`,
{ method: 'POST' },
'重新开始方洞挑战失败',
{ retry: SQUARE_HOLE_RUNTIME_WRITE_RETRY },
);
return requestRuntimeJson<SquareHoleRunResponse>({
url: buildRuntimeApiPath(
SQUARE_HOLE_RUNTIME_API_BASE,
'runs',
runId,
'restart',
),
method: 'POST',
fallbackMessage: '重新开始方洞挑战失败',
retry: SQUARE_HOLE_RUNTIME_WRITE_RETRY,
});
}
/**
* 前端倒计时归零后通知后端确认失败状态。
*/
export function finishSquareHoleTimeUp(runId: string) {
return requestJson<SquareHoleRunResponse>(
`/api/runtime/square-hole/runs/${encodeURIComponent(runId)}/time-up`,
{ method: 'POST' },
'同步方洞挑战倒计时失败',
{ retry: SQUARE_HOLE_RUNTIME_WRITE_RETRY },
);
return requestRuntimeJson<SquareHoleRunResponse>({
url: buildRuntimeApiPath(
SQUARE_HOLE_RUNTIME_API_BASE,
'runs',
runId,
'time-up',
),
method: 'POST',
fallbackMessage: '同步方洞挑战倒计时失败',
retry: SQUARE_HOLE_RUNTIME_WRITE_RETRY,
});
}
export const squareHoleRuntimeClient = {