UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

65 lines (64 loc) • 2.93 kB
import { a as resolveCliParentCommandPath, n as isConfigSetJsonParseOnly } from "./config-output-mode-B_Eb6adL.mjs"; import { n as loggingState } from "./state-Bg-ugveB.mjs"; import { n as withNoteOutput } from "./note-output-8FFGu-hu.mjs"; //#region src/cli/json-output-mode.ts let resolvedJsonOutputMode = null; let stderrRoutingScopes = 0; let stderrRoutingAfterScopes = false; function setConsoleRouting(e) { if (stderrRoutingScopes > 0) stderrRoutingAfterScopes = e; loggingState.forceConsoleToStderr = e || stderrRoutingScopes > 0; } const noteOutputForConsoleRouting = () => loggingState.forceConsoleToStderr ? process.stderr : process.stdout; /** Detects CLI JSON mode before Commander parses options, stopping at the argv sentinel. */ function hasJsonOutputFlag(e) { for (const t of e) { if (t === "--") return false; if (t === "--json" || t.startsWith("--json=")) return true; } return false; } /** Uses Commander-resolved output ownership when available, then falls back to argv. */ function isJsonOutputModeActive(e) { return resolvedJsonOutputMode ?? (hasJsonOutputFlag(e) && !(resolveCliParentCommandPath(e, "config")?.[1] === "set" && isConfigSetJsonParseOnly(e))); } /** Keeps structured JSON stdout clean by routing incidental console logs and notes to stderr. */ async function withConsoleLogsRoutedToStderrForJson(e, t, n = {}) { const r = hasJsonOutputFlag(e) || n.machineOutput; if (!r && !n.restoreChanges) return withNoteOutput(noteOutputForConsoleRouting, t); const i = stderrRoutingScopes > 0 ? stderrRoutingAfterScopes : loggingState.forceConsoleToStderr; const a = loggingState.earlyConsoleRoutingRestore; const o = resolvedJsonOutputMode; resolvedJsonOutputMode = null; if (r) { loggingState.earlyConsoleRoutingRestore = i; setConsoleRouting(true); } try { return await withNoteOutput(noteOutputForConsoleRouting, t); } finally { if (!n.retainRoutingUntilProcessExit) { setConsoleRouting(i); loggingState.earlyConsoleRoutingRestore = a; resolvedJsonOutputMode = o; } } } /** Let resolved command metadata override conservative early literal-flag routing. */ function applyResolvedCommandOutputMode(e, t = e) { resolvedJsonOutputMode = e; const n = loggingState.earlyConsoleRoutingRestore; if (!t && n !== null) setConsoleRouting(n); } /** Route startup diagnostics to stderr while a command's output mode is still being discovered. */ async function withConsoleLogsRoutedToStderr(e) { if (stderrRoutingScopes++ === 0) stderrRoutingAfterScopes = loggingState.forceConsoleToStderr; loggingState.forceConsoleToStderr = true; try { return await e(); } finally { if (--stderrRoutingScopes === 0) loggingState.forceConsoleToStderr = stderrRoutingAfterScopes; } } //#endregion export { withConsoleLogsRoutedToStderrForJson as a, withConsoleLogsRoutedToStderr as i, hasJsonOutputFlag as n, isJsonOutputModeActive as r, applyResolvedCommandOutputMode as t };