import { accountRequiredMessage } from '../lib/desktop-mode.js'; import { AuthStore, type AuthProfile } from '../lib/auth.js'; import { InvalidArgumentError } from 'commander'; import { sendProfileOwnerControlRequest, type OwnerControlResponse, type OwnerControlType } from '../runtime/owner-control.js'; import { ShortCommandRunner, type CommandIo, type ShortCommandOperationResult } from '../short-command/runner.js'; export interface ModuleRuntimeCommandDependencies { activeProfile?: () => AuthProfile | null; sendOwnerControlRequest?: typeof sendProfileOwnerControlRequest; io?: CommandIo; } const OWNER_CONTROL_TIMEOUT_MS = 30000; const OWNER_RUNTIME_GUIDANCE = 'Attach the foreground game Runtime with ccl join before changing runtime module settings.'; const processIo: CommandIo = { writeStdout: text => { process.stdout.write(text); }, setExitCode: code => { process.exitCode = code; } }; export function parseModuleInput(value: string): Record { let input: unknown; try { input = JSON.parse(value); } catch { throw new InvalidArgumentError('input must be a JSON object'); } if (!input || typeof input !== 'object' || Array.isArray(input)) throw new InvalidArgumentError('input must be a JSON object'); return input as Record; } function ownerFailure( response: OwnerControlResponse | null, code: string, fallbackMessage: string, ): ShortCommandOperationResult { return { kind: 'local_failure', source: 'owner', error: { code: String(response?.code ?? response?.error ?? code), message: String(response?.message ?? response?.error ?? fallbackMessage), ...(response?.details === undefined ? {} : { details: response.details }), }, presentation: { guidance: response === null ? OWNER_RUNTIME_GUIDANCE : undefined }, }; } export async function runOwnerCommand( command: string, type: OwnerControlType, payload: Readonly>, scoped: boolean, dependencies: ModuleRuntimeCommandDependencies, ): Promise { const runner = new ShortCommandRunner(); const profile = (dependencies.activeProfile ?? (() => new AuthStore().getActive()))(); const send = dependencies.sendOwnerControlRequest ?? sendProfileOwnerControlRequest; const io = dependencies.io ?? processIo; await runner.run({ descriptor: { command, service: 'clawclaw-game-owner', operation: type, sensitivePolicy: { sensitiveValues: profile ? [profile.apiKey] : [], sensitiveFieldNames: ['api_key', 'apiKey', 'token', 'authorization'], }, }, io, operation: async (): Promise => { if (!profile) { return { kind: 'local_failure', source: 'local', error: { code: 'NO_ACTIVE_ACCOUNT', message: accountRequiredMessage('No active account. Run `ccl account register` or switch accounts first.'), }, }; } let requestPayload: Record = { ...payload }; if (scoped) { let snapshot: OwnerControlResponse | null; try { snapshot = await send(profile, 'snapshot'); } catch (error) { return { kind: 'transport_failure', error: { code: 'OWNER_UNAVAILABLE', message: error instanceof Error ? error.message : String(error), }, presentation: { guidance: OWNER_RUNTIME_GUIDANCE }, }; } if (!snapshot?.ok || snapshot.scope === undefined) { return ownerFailure(snapshot, 'NO_GAME_START_OWNER', OWNER_RUNTIME_GUIDANCE); } requestPayload = { expected_scope: snapshot.scope, ...requestPayload }; } let response: OwnerControlResponse | null; try { response = await send(profile, type, requestPayload, OWNER_CONTROL_TIMEOUT_MS); } catch (error) { return { kind: 'transport_failure', error: { code: 'OWNER_UNAVAILABLE', message: error instanceof Error ? error.message : String(error), }, presentation: { guidance: OWNER_RUNTIME_GUIDANCE }, }; } if (!response?.ok) { return ownerFailure(response, `${type.toUpperCase()}_REJECTED`, `The owner rejected ${type}.`); } const { ok: _ok, type: _type, ...value } = response; return { kind: 'local_success', source: 'owner', value: Object.keys(value).length > 0 ? value : { status: 'ok' }, }; }, }); }