import { existsSync, readFileSync } from 'node:fs'; import { join } from 'node:path'; import type { AuthProfile } from '../lib/auth.js'; import { getProfileRuntimeStateDir } from '../lib/init-command.js'; import { startOwnerControlServer as startControlServer, sendControlRequest, OWNER_CONTROL_TIMEOUT_MS } from '@myclaw163/game-agent-runtime/owner/control-channel.js'; import type { OwnerControlRequest as CoreRequest, OwnerControlRequestContext, OwnerControlResponse, OwnerControlServerOptions, OwnerControlServer } from '@myclaw163/game-agent-runtime/owner/control-channel.js'; export { OWNER_CONTROL_TIMEOUT_MS }; export type { OwnerControlInfo, OwnerControlRequestContext, OwnerControlResponse, OwnerControlServerOptions, OwnerControlServer } from '@myclaw163/game-agent-runtime/owner/control-channel.js'; const SAFE_JSON_PARSE = JSON.parse.bind(JSON) as typeof JSON.parse; export const GAME_START_RUNTIME_FILE = 'game-start.json'; export type MasterOwnerControlType = 'quit' | 'leave' | 'stop' | 'training_mode' | 'training_role_lock' | 'training_role_clear' | 'switch_strategy' | 'update_strategy_control' | 'snapshot' | 'action' | 'mailbox_claim' | 'mailbox_ack' | 'mailbox_nack' | 'mailbox_events_prepare' | 'mailbox_events_ack' | 'mailbox_events_release' | 'perception_update' | 'perception_status' | 'behavior_watch' | 'behavior_unwatch' | 'behavior_update' | 'behavior_start' | 'behavior_status' | 'behavior_cancel' | 'behavior_fallback' | 'behavior_idle' | 'trigger_configure' | 'intent_start' | 'intent_status' | 'intent_cancel'; export type HostRelayOwnerControlType = 'mailbox_claim' | 'mailbox_ack' | 'mailbox_nack'; export type OwnerControlType = MasterOwnerControlType; export interface OwnerControlRequest extends CoreRequest { type?: OwnerControlType; } export function gameStartRuntimePath(stateDir: string): string { return join(stateDir, GAME_START_RUNTIME_FILE); } export function readGameStartRuntime(stateDir: string): Record | null { const runtimePath = gameStartRuntimePath(stateDir); if (!existsSync(runtimePath)) return null; try { return SAFE_JSON_PARSE(readFileSync(runtimePath, 'utf8')); } catch { } return null; } export function startOwnerControlServer(stateDir: string, onRequest: (request: OwnerControlRequest, context: OwnerControlRequestContext) => Promise | OwnerControlResponse, options: OwnerControlServerOptions = {}): Promise { return startControlServer(stateDir, (request, context) => onRequest(request as OwnerControlRequest, context), { ...options, namespace: 'clawclaw', relayCommands: ['mailbox_claim', 'mailbox_ack', 'mailbox_nack'] }); } /** Master/short-command path. This is the only helper that reads game-start.json. */ export function sendOwnerControlRequest(stateDir: string, type: OwnerControlType, payload: Record = {}, timeoutMs = OWNER_CONTROL_TIMEOUT_MS): Promise { const info = readGameStartRuntime(stateDir); return sendOwnerControlRequestTo(info, type, payload, timeoutMs); } /** Use a captured endpoint when replacing an owner; do not re-read a newer pointer. */ export function sendOwnerControlRequestTo(info: Record | null, type: OwnerControlType, payload: Record = {}, timeoutMs = OWNER_CONTROL_TIMEOUT_MS): Promise { const path = typeof info?.control?.path === 'string' ? info.control.path : ''; const token = typeof info?.control_token === 'string' ? info.control_token : ''; if (!path || !token) return Promise.resolve(null); return sendControlRequest(path, { ...payload, token, type }, timeoutMs); } /** Narrow Host Relay path; this authority cannot execute actions or lifecycle commands. */ export function sendOwnerRelayControlRequest(stateDir: string, type: HostRelayOwnerControlType, payload: Record = {}, timeoutMs = OWNER_CONTROL_TIMEOUT_MS): Promise { const info = readGameStartRuntime(stateDir); const path = typeof info?.control?.path === 'string' ? info.control.path : ''; const relayToken = typeof info?.relay_token === 'string' ? info.relay_token : ''; if (!path || !relayToken) return Promise.resolve(null); return sendControlRequest(path, { ...payload, relay_token: relayToken, type, }, timeoutMs); } /** Stable owner-control lookup for Agent-facing short commands. */ export function sendProfileOwnerControlRequest(profile: Pick, type: OwnerControlType, payload: Record = {}, timeoutMs = OWNER_CONTROL_TIMEOUT_MS): Promise { return sendOwnerControlRequest(getProfileRuntimeStateDir(profile), type, payload, timeoutMs); }