import type { BarkBattleFinishResponse, BarkBattleRunFinishRequest, BarkBattleRunStartRequest, BarkBattleRunStartResponse, BarkBattleRuntimeConfig, } from '../../../packages/shared/src/contracts/barkBattle'; import { type ApiRequestOptions, type ApiRetryOptions, requestJson, } from '../apiClient'; const BARK_BATTLE_RUNTIME_READ_RETRY: ApiRetryOptions = { maxRetries: 1, baseDelayMs: 120, maxDelayMs: 360, }; const BARK_BATTLE_RUNTIME_WRITE_RETRY: ApiRetryOptions = { maxRetries: 1, baseDelayMs: 120, maxDelayMs: 360, retryUnsafeMethods: true, }; export type BarkBattleRuntimeRequestOptions = Pick< ApiRequestOptions, | 'authImpact' | 'skipRefresh' | 'notifyAuthStateChange' | 'clearAuthOnUnauthorized' >; export function getBarkBattleRuntimeConfig( workId: string, options: BarkBattleRuntimeRequestOptions = {}, ) { return requestJson( `/api/runtime/bark-battle/works/${encodeURIComponent(workId)}/config`, { method: 'GET' }, '读取汪汪声浪大作战配置失败', { retry: BARK_BATTLE_RUNTIME_READ_RETRY, authImpact: options.authImpact, skipRefresh: options.skipRefresh, notifyAuthStateChange: options.notifyAuthStateChange, clearAuthOnUnauthorized: options.clearAuthOnUnauthorized, }, ); } export function startBarkBattleRun( workId: string, payload: Partial = {}, options: BarkBattleRuntimeRequestOptions = {}, ) { return requestJson( `/api/runtime/bark-battle/works/${encodeURIComponent(workId)}/runs`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ...payload, workId: payload.workId ?? workId, }), }, '启动汪汪声浪大作战正式局失败', { retry: BARK_BATTLE_RUNTIME_WRITE_RETRY, authImpact: options.authImpact, skipRefresh: options.skipRefresh, notifyAuthStateChange: options.notifyAuthStateChange, clearAuthOnUnauthorized: options.clearAuthOnUnauthorized, }, ); } export function getBarkBattleRun( runId: string, options: BarkBattleRuntimeRequestOptions = {}, ) { return requestJson( `/api/runtime/bark-battle/runs/${encodeURIComponent(runId)}`, { method: 'GET' }, '读取汪汪声浪大作战单局失败', { retry: BARK_BATTLE_RUNTIME_READ_RETRY, authImpact: options.authImpact, skipRefresh: options.skipRefresh, notifyAuthStateChange: options.notifyAuthStateChange, clearAuthOnUnauthorized: options.clearAuthOnUnauthorized, }, ); } export function finishBarkBattleRun( runId: string, payload: BarkBattleRunFinishRequest, options: BarkBattleRuntimeRequestOptions = {}, ) { return requestJson( `/api/runtime/bark-battle/runs/${encodeURIComponent(runId)}/finish`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ...payload, runId: payload.runId ?? runId, }), }, '提交汪汪声浪大作战成绩失败', { retry: BARK_BATTLE_RUNTIME_WRITE_RETRY, authImpact: options.authImpact, skipRefresh: options.skipRefresh, notifyAuthStateChange: options.notifyAuthStateChange, clearAuthOnUnauthorized: options.clearAuthOnUnauthorized, }, ); }